Advertisement
bolverk

obstacle oblique shock

Apr 21st, 2015
414
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.90 KB | None | 0 0
  1. #ifdef RICH_MPI
  2. #include <mpi.h>
  3. #endif
  4. #include <iostream>
  5. #include <fstream>
  6. #include <cmath>
  7. #include <vector>
  8. #include <string>
  9. #include "source/tessellation/geometry.hpp"
  10. #include "source/newtonian/two_dimensional/hdsim2d.hpp"
  11. #include "source/tessellation/tessellation.hpp"
  12. #include "source/newtonian/common/hllc.hpp"
  13. #include "source/newtonian/common/ideal_gas.hpp"
  14. #include "source/tessellation/VoronoiMesh.hpp"
  15. #include "source/newtonian/two_dimensional/interpolations/pcm2d.hpp"
  16. #include "source/newtonian/two_dimensional/spatial_distributions/uniform2d.hpp"
  17. #include "source/newtonian/two_dimensional/point_motions/eulerian.hpp"
  18. #include "source/newtonian/two_dimensional/point_motions/lagrangian.hpp"
  19. #include "source/newtonian/two_dimensional/point_motions/round_cells.hpp"
  20. #include "source/newtonian/two_dimensional/source_terms/zero_force.hpp"
  21. #include "source/newtonian/two_dimensional/geometric_outer_boundaries/SquareBox.hpp"
  22. #include "source/newtonian/two_dimensional/hydro_boundary_conditions/RigidWallHydro.hpp"
  23. #include "source/newtonian/test_2d/random_pert.hpp"
  24. #include "source/newtonian/two_dimensional/diagnostics.hpp"
  25. #include "source/misc/simple_io.hpp"
  26. #include "source/misc/mesh_generator.hpp"
  27. #include "source/newtonian/test_2d/main_loop_2d.hpp"
  28. #include "source/newtonian/two_dimensional/hdf5_diagnostics.hpp"
  29. #include "source/mpi/mpi_macro.hpp"
  30. #include "source/mpi/MeshPointsMPI.hpp"
  31. #include "source/tessellation/shape_2d.hpp"
  32. #include "source/newtonian/test_2d/piecewise.hpp"
  33. #include "source/newtonian/two_dimensional/simple_flux_calculator.hpp"
  34. #include "source/newtonian/two_dimensional/simple_cell_updater.hpp"
  35. #include "source/newtonian/two_dimensional/simple_extensive_updater.hpp"
  36. #include "source/newtonian/test_2d/consecutive_snapshots.hpp"
  37. #include "source/newtonian/test_2d/multiple_diagnostics.hpp"
  38.  
  39. using namespace std;
  40. using namespace simulation2d;
  41.  
  42. namespace {
  43.  
  44. #ifdef RICH_MPI
  45.   vector<Vector2D> process_positions(const SquareBox& boundary)
  46.   {
  47.     const Vector2D lower_left = boundary.getBoundary().first;
  48.     const Vector2D upper_right = boundary.getBoundary().second;
  49.     vector<Vector2D> res(get_mpi_size());
  50.     if(get_mpi_rank()==0){
  51.       res = RandSquare(get_mpi_size(),
  52.                lower_left.x,upper_right.x,
  53.                lower_left.y,upper_right.y);
  54.     }
  55.     MPI_VectorBcast_Vector2D(res,0,MPI_COMM_WORLD,get_mpi_rank());
  56.     return res;
  57.   }
  58. #endif
  59.  
  60.   vector<ComputationalCell> calc_init_cond(const Tessellation& tess)
  61.   {
  62.     vector<ComputationalCell> res(static_cast<size_t>(tess.GetPointNo()));
  63.     const Circle obstacle(Vector2D(0,-1),0.05);
  64.     for(size_t i=0;i<res.size();++i){
  65.       const Vector2D r = tess.GetMeshPoint(static_cast<int>(i));
  66.       res[i].density = 1e-6;;
  67.       res[i].pressure = 1e-9;
  68.       res[i].velocity = Vector2D(1,0);
  69.       res[i].stickers["obstacle"] = false;
  70.       if(r.y<0)
  71.     res[i].density = max(res[i].density, pow(-r.y,1));
  72.       if(obstacle(r)){
  73.     res[i].velocity = Vector2D(0,0);
  74.     res[i].stickers["obstacle"] = true;
  75.       }
  76.     }
  77.     return res;
  78.   }
  79.  
  80.   class CustomCellUpdater: public CellUpdater
  81.   {
  82.   public:
  83.  
  84.     CustomCellUpdater(void) {}
  85.  
  86.     vector<ComputationalCell> operator()
  87.     (const Tessellation& /*tess*/,
  88.      const PhysicalGeometry& /*pg*/,
  89.      const EquationOfState& eos,
  90.      const vector<Extensive>& extensives,
  91.      const vector<ComputationalCell>& old,
  92.      const CacheData& cd) const
  93.     {
  94.       vector<ComputationalCell> res = old;
  95.       for(size_t i=0;i<extensives.size();++i){
  96.     if(old[i].stickers.find("obstacle")->second)
  97.       continue;
  98.     const double volume = cd.volumes[i];
  99.     res[i].density = extensives[i].mass/volume;
  100.     res[i].velocity = extensives[i].momentum / extensives[i].mass;
  101.     const double total_energy = extensives[i].energy/extensives[i].mass;
  102.     const double kinetic_energy =
  103.       0.5*ScalarProd(res[i].velocity, res[i].velocity);
  104.     const double energy = total_energy - kinetic_energy;
  105.     res[i].pressure = eos.de2p(res[i].density, energy);
  106.     for(std::map<std::string,double>::const_iterator it =
  107.           extensives[i].tracers.begin();
  108.         it!=extensives[i].tracers.end();++it)
  109.       res[i].tracers[it->first] = it->second/extensives[i].mass;
  110.       }
  111.       return res;
  112.     }
  113.   };
  114.  
  115.   double calc_tracer_flux(const Edge& edge,
  116.               const Tessellation& tess,
  117.               const vector<ComputationalCell>& cells,
  118.               const string& name,
  119.               const Conserved& hf)
  120.   {
  121.     if(hf.Mass>0 &&
  122.        edge.neighbors.first>0 &&
  123.        edge.neighbors.first<tess.GetPointNo())
  124.       return hf.Mass*
  125.     cells[static_cast<size_t>(edge.neighbors.first)].tracers.find(name)->second;
  126.     if(hf.Mass<0 &&
  127.        edge.neighbors.second>0 &&
  128.        edge.neighbors.second<tess.GetPointNo())
  129.       return hf.Mass*
  130.     cells[static_cast<size_t>(edge.neighbors.second)].tracers.find(name)->second;
  131.     return 0;        
  132.   }
  133.  
  134.   class CustomFluxCalculator: public FluxCalculator
  135.   {
  136.   public:
  137.  
  138.     CustomFluxCalculator(const RiemannSolver& rs):
  139.       rs_(rs) {}
  140.  
  141.     vector<Extensive> operator()
  142.     (const Tessellation& tess,
  143.      const vector<Vector2D>& point_velocities,
  144.      const vector<ComputationalCell>& cells,
  145.      const EquationOfState& eos,
  146.      const double /*time*/,
  147.      const double /*dt*/) const
  148.     {
  149.       vector<Extensive> res(tess.getAllEdges().size());
  150.       for(size_t i=0;i<tess.getAllEdges().size();++i){
  151.     const Conserved hydro_flux =
  152.       calcHydroFlux(tess, point_velocities,
  153.             cells, eos, i);
  154.     res[i].mass = hydro_flux.Mass;
  155.     res[i].momentum = hydro_flux.Momentum;
  156.     res[i].energy = hydro_flux.Energy;
  157.     for(std::map<std::string,double>::const_iterator it =
  158.           cells.front().tracers.begin();
  159.         it!=cells.front().tracers.end();++it)
  160.       res[i].tracers[it->first] =
  161.         calc_tracer_flux(tess.getAllEdges()[i],
  162.                  tess,cells,it->first,hydro_flux);
  163.       }
  164.       return res;
  165.     }
  166.  
  167.   private:
  168.     const RiemannSolver& rs_;
  169.  
  170.     const Conserved calcHydroFlux
  171.     (const Tessellation& tess,
  172.      const vector<Vector2D>& point_velocities,
  173.      const vector<ComputationalCell>& cells,
  174.      const EquationOfState& eos,
  175.      const size_t i) const
  176.     {
  177.       const Edge& edge = tess.GetEdge(static_cast<int>(i));
  178.       const std::pair<bool,bool> flags
  179.     (edge.neighbors.first>=0 && edge.neighbors.first<tess.GetPointNo(),
  180.      edge.neighbors.second>=0 && edge.neighbors.second<tess.GetPointNo());
  181.       assert(flags.first || flags.second);
  182.       if(!flags.first){
  183.     const size_t right_index =
  184.       static_cast<size_t>(edge.neighbors.second);
  185.     const ComputationalCell& right_cell = cells[right_index];
  186.     if(right_cell.stickers.find("obstacle")->second)
  187.       return Conserved();
  188.     const Vector2D p = Parallel(edge);
  189.     const Primitive right = convert_to_primitive(right_cell,eos);
  190.     const Primitive left = right;
  191.     const Vector2D n = remove_parallel_component
  192.       (tess.GetMeshPoint(edge.neighbors.second) -
  193.        edge.vertices.first, p);
  194.     return rotate_solve_rotate_back
  195.       (rs_, left, right, 0, n, p);
  196.       }
  197.       if(!flags.second){
  198.     const size_t left_index =
  199.       static_cast<size_t>(edge.neighbors.first);
  200.     const ComputationalCell& left_cell = cells[left_index];
  201.     if(left_cell.stickers.find("obstacle")->second)
  202.       return Conserved();
  203.     const Primitive left = convert_to_primitive(left_cell, eos);
  204.     const Vector2D p = Parallel(edge);
  205.     const Primitive right = left;
  206.     const Vector2D n = remove_parallel_component
  207.       (edge.vertices.second -
  208.        tess.GetMeshPoint(edge.neighbors.first),p);
  209.     return rotate_solve_rotate_back
  210.       (rs_,left,right,0,n,p);
  211.       }
  212.       const size_t left_index =
  213.     static_cast<size_t>(edge.neighbors.first);
  214.       const size_t right_index =
  215.     static_cast<size_t>(edge.neighbors.second);
  216.       const ComputationalCell& left_cell = cells[left_index];
  217.       const ComputationalCell& right_cell = cells[right_index];
  218.       if(left_cell.stickers.find("obstacle")->second &&
  219.      right_cell.stickers.find("obstacle")->second)
  220.     return Conserved();
  221.       const Vector2D p = Parallel(edge);
  222.       const Vector2D n =
  223.     tess.GetMeshPoint(edge.neighbors.second) -
  224.     tess.GetMeshPoint(edge.neighbors.first);
  225.       const double velocity = Projection
  226.     (tess.CalcFaceVelocity
  227.      (point_velocities[left_index],
  228.       point_velocities[right_index],
  229.       tess.GetCellCM(edge.neighbors.first),
  230.       tess.GetCellCM(edge.neighbors.second),
  231.       calc_centroid(edge)),n);
  232.       if(left_cell.stickers.find("obstacle")->second){
  233.     const Primitive right = convert_to_primitive(right_cell,eos);
  234.     const Primitive left = reflect(right,p);
  235.     return rotate_solve_rotate_back
  236.       (rs_,left,right,velocity,n,p);
  237.       }
  238.       if(right_cell.stickers.find("obstacle")->second){
  239.     const Primitive left = convert_to_primitive(left_cell,eos);
  240.     const Primitive right = reflect(left,p);
  241.     return rotate_solve_rotate_back
  242.       (rs_,left,right,velocity,n,p);
  243.       }
  244.       const Primitive left =
  245.     convert_to_primitive(left_cell,eos);
  246.       const Primitive right =
  247.     convert_to_primitive(right_cell,eos);
  248.       return rotate_solve_rotate_back
  249.     (rs_,left,right,velocity,n,p);
  250.     }
  251.   };
  252.  
  253.   class SimData
  254.   {
  255.   public:
  256.  
  257.     SimData(void):
  258.       pg_(),
  259.       outer_(Vector2D(-1,-1),
  260.          Vector2D(1,1)),
  261.       init_points_(cartesian_mesh(500,500,
  262.                   outer_.getBoundary().first,
  263.                   outer_.getBoundary().second)),
  264.       tess_(init_points_, outer_),
  265.       eos_(5./3.),
  266.       point_motion_(),
  267.       rs_(),
  268.       force_(),
  269.       tsf_(0.3),
  270.       fc_(rs_),
  271.       eu_(),
  272.       cu_(),
  273.       sim_(tess_,
  274.        outer_,
  275.        pg_,
  276.        calc_init_cond(tess_),
  277.        eos_,
  278.        point_motion_,
  279.        force_,
  280.        tsf_,
  281.        fc_,
  282.        eu_,
  283.        cu_) {}
  284.  
  285.     hdsim& getSim(void)
  286.     {
  287.       return sim_;
  288.     }
  289.  
  290.   private:
  291.     const SlabSymmetry pg_;
  292.     const SquareBox outer_;
  293.     const vector<Vector2D> init_points_;
  294.     VoronoiMesh tess_;
  295.     PCM2D interp_method_;
  296.     const IdealGas eos_;
  297.     Eulerian point_motion_;
  298.     const Hllc rs_;
  299.     ZeroForce force_;
  300.     const SimpleCFL tsf_;
  301.     //    const SimpleFluxCalculator fc_;
  302.     const CustomFluxCalculator fc_;
  303.     const SimpleExtensiveUpdater eu_;
  304.     //    const SimpleCellUpdater cu_;
  305.     const CustomCellUpdater cu_;
  306.     hdsim sim_;
  307.   };
  308.  
  309.   void my_main_loop(hdsim& sim)
  310.   {
  311.     const double tf = 10;
  312.     SafeTimeTermination term_cond(tf,1e6);
  313.     WriteTime diag1("time.txt");
  314.     ConsecutiveSnapshots diag2(new ConstantTimeInterval(tf/1000),
  315.                    new Rubric("snapshot_",".h5"));
  316.     MultipleDiagnostics diag;
  317.     diag.diag_list.push_back(&diag1);
  318.     diag.diag_list.push_back(&diag2);
  319.     main_loop(sim,
  320.           term_cond,
  321.           &hdsim::TimeAdvance,
  322.           &diag);
  323.   }
  324. }
  325.  
  326. int main(void)
  327. {
  328. #ifdef RICH_MPI
  329.   MPI_Init(NULL, NULL);
  330. #endif
  331.  
  332.   SimData sim_data;
  333.   hdsim& sim = sim_data.getSim();
  334.  
  335.   my_main_loop(sim);
  336.  
  337. #ifdef RICH_MPI
  338.   write_snapshot_to_hdf5(sim, "process_"+int2str(get_mpi_rank())+"_final.h5");
  339. #else
  340.   write_snapshot_to_hdf5(sim, "final.h5");
  341. #endif
  342.  
  343.  
  344. #ifdef RICH_MPI
  345.   MPI_Finalize();
  346. #endif
  347.  
  348.   return 0;
  349. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement