Advertisement
bolverk

cylindrical_parabolic_bow_shock

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