Advertisement
Guest User

Untitled

a guest
Apr 6th, 2013
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.14 KB | None | 0 0
  1.  
  2. #include <dolfin.h>
  3. #include "bl.h"
  4.  
  5. using namespace dolfin;
  6.  
  7. class FreestreamBoundary : public SubDomain {
  8. public:
  9.     bool inside ( const Array<double> & x, bool on_boundary ) const override {
  10.         return x[1] >= 1.0 - DOLFIN_EPS;
  11.     }
  12. };
  13.  
  14. class WallBoundary : public SubDomain {
  15.     bool inside ( const Array<double> & x, bool on_boundary ) const override {
  16.         return x[1] <= DOLFIN_EPS;
  17.     }
  18. };
  19.  
  20. class InflowBoundary : public SubDomain {
  21.     bool inside ( const Array<double> & x, bool on_boundary ) const override {
  22.         return x[0] <= DOLFIN_EPS;
  23.     }
  24. };
  25.  
  26. class PeriodicBoundary : public SubDomain {
  27.     bool inside ( const Array<double> & x, bool on_boundary ) const override {
  28.         return x[2] < DOLFIN_EPS;
  29.     }
  30.    
  31.     void map(const Array<double>& x, Array<double>& y) const override {
  32.         y[0] = x[0];
  33.         y[1] = x[1];
  34.         y[2] = x[2] - 1.0;
  35.     }
  36. };
  37.  
  38. class InputV :  public Expression {
  39. public:
  40.     InputV() : Expression(3) {}
  41.     void eval(Array<double> & values, const Array<double> & x) const override {
  42.         values[0] = 1.0;
  43.         values[1] = 0;
  44.         values[2] = 0;
  45.     }
  46. };
  47.  
  48. class ExtPressure : public Expression {
  49. public:
  50.     void eval(Array<double> & values, const Array<double> & x) const override {
  51.         values[0] = 0;//x[0];                                        //NOTE: Must not use x[1]
  52.     }
  53. };
  54.  
  55. Mesh make_mesh(int cells_x,int cells_y,int cells_z,double k = 2.0 )
  56. {
  57.     UnitCubeMesh ans (cells_x, cells_y, cells_z);
  58.     std::vector<double> & V = ans.geometry().x();
  59.  
  60.     for (size_t i = 1;i < V.size();i+=3)
  61.         V[i] =  1.0 - std::log ( 1  + ( 1.0 - V[i] ) * ( std::exp ( k ) - 1.0 ) ) / k;
  62.     return ans;
  63. }
  64.  
  65. int main()
  66. {
  67.     Mesh mesh = make_mesh(8,16,8);
  68.     PeriodicBoundary periodic_boundary;
  69.    
  70.     set_log_level(INFO);
  71.    
  72.     //Load function space and subspaces
  73.     bl::FunctionSpace V ( mesh , periodic_boundary);
  74.    
  75.     SubSpace VUx (V,0);
  76.     SubSpace VUy (V,1);
  77.     SubSpace VUz (V,2);
  78.    
  79.     //Setup boundary conditions
  80.     std::vector<const BoundaryCondition *> bcs;
  81.    
  82.     //Inflow condition
  83.     InflowBoundary inflow_boundary;
  84.     InputV u0;
  85.     bcs.push_back( new DirichletBC( V, u0, inflow_boundary));
  86.    
  87.     //Freestream condition
  88.     FreestreamBoundary freestream_boundary;
  89.     Constant u_freestream(1.0);
  90.     Constant w_freestream(0.0);
  91.  
  92.     bcs.push_back( new DirichletBC( VUx, u_freestream, freestream_boundary) );
  93.     bcs.push_back( new DirichletBC( VUz, w_freestream, freestream_boundary) );
  94.  
  95.     //Wall condition
  96.     WallBoundary wall_boundary;
  97.     Constant u_noslip(0.0,0.0,0.0);
  98.     bcs.push_back( new DirichletBC( V, u_noslip, wall_boundary )) ;
  99.  
  100.     //Setups forms
  101.     bl::ResidualForm F ( V );
  102.     bl::JacobianForm J ( V, V );
  103.    
  104.     //Pressure is a constant parameter
  105.     ExtPressure p_freestream;
  106.     F.p = p_freestream;
  107.  
  108.     //Setup u
  109.     Function u(V);
  110.     u = u0;
  111.     F.u = u;
  112.     J.u = u;
  113.    
  114.     NonlinearVariationalProblem bl_problem(F, u, bcs, J);
  115.     NonlinearVariationalSolver solver(bl_problem);
  116.     solver.solve();                                            //< crash
  117.  
  118.     //Setup solver parameters
  119.     solve( F == 0,u, bcs,J);                                   //< fine
  120.    
  121.     //Save to file
  122.     File pvd("u.pvd");
  123.     pvd << u;
  124.    
  125.     //Plot
  126.     plot ( u,"velocity" );
  127.     plot ( p_freestream,mesh,  "pressure");
  128.    
  129.     interactive();
  130.  
  131.     return 0;
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement