Advertisement
bolverk

planar_relativistic_breakout

Apr 12th, 2015
443
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.02 KB | None | 0 0
  1. #include <cassert>
  2. #include <iostream>
  3. #include <fstream>
  4. #include <vector>
  5. #include "srhd_sim.hpp"
  6. #include "spatial_distribution.hpp"
  7. #include "ideal_gas.hpp"
  8. #include "hll.hpp"
  9. #include "pcm.hpp"
  10. #include "rigid_wall.hpp"
  11. #include "main_loop.hpp"
  12. #include "hdf5_snapshot.hpp"
  13. #include "imgrs.hpp"
  14. #include "rubric.hpp"
  15. //#include <fenv.h>
  16.  
  17. using namespace std;
  18.  
  19. namespace {
  20.  
  21.   vector<double> generate_grid(double left, double right, double rat)
  22.   {
  23.     assert(left<0 && right<0);
  24.     vector<double> res;
  25.     for(double x=left*(1-0.5*rat);
  26.     x<right*(1+0.5*rat);x-=rat*x)
  27.       res.push_back(x);
  28.     return res;
  29.   }
  30.  
  31.   /*
  32.   class Gaussian: public SpatialDistribution
  33.   {
  34.   public:
  35.  
  36.     Gaussian(double prefactor,
  37.          double mean,
  38.          double std,
  39.          double minval):
  40.       prefactor_(prefactor),
  41.       mean_(mean), std_(std), minval_(minval) {}
  42.  
  43.     double operator()(double x) const
  44.     {
  45.       if(abs(x-mean_)/std_>20)
  46.     return 0;
  47.       return max(prefactor_*exp(-pow((x-mean_)/std_,2)),
  48.          minval_);
  49.     }
  50.  
  51.   private:
  52.     const double prefactor_;
  53.     const double mean_;
  54.     const double std_;
  55.     const double minval_;
  56.   };
  57.   */
  58.  
  59.   class PowerLaw: public SpatialDistribution
  60.   {
  61.   public:
  62.  
  63.     PowerLaw(double prefactor, double index):
  64.       prefactor_(prefactor), index_(index) {}
  65.  
  66.     double operator()(double x) const
  67.     {
  68.       return prefactor_*pow(-x,index_);
  69.     }
  70.  
  71.   private:
  72.     const double prefactor_;
  73.     const double index_;
  74.   };
  75.  
  76.   class MockVacuum: public BoundaryCondition
  77.   {
  78.   public:
  79.  
  80.     MockVacuum(const RiemannSolver& rs):
  81.       rs_(rs) {}
  82.  
  83.     RiemannSolution CalcRS
  84.     (size_t idx, const vector<Primitive>& cells) const
  85.     {
  86.       assert(0==idx || cells.size()==idx);
  87.       if(0==idx){
  88.     const Primitive right = cells.front();
  89.     const Primitive left(right.Density*0.99,
  90.                  right.Pressure*0.99,
  91.                  right.Celerity);
  92.     return rs_(left,right);
  93.       }
  94.       const Primitive left = cells.back();
  95.       const Primitive right(left.Density/100,
  96.                 left.Pressure/100,
  97.                 left.Celerity);
  98.       return rs_(left, right);
  99.     }
  100.  
  101.   private:
  102.     const RiemannSolver& rs_;
  103.   };
  104.  
  105.   /*
  106.   class IdealGasVacuum: public BoundaryCondition
  107.   {
  108.   public:
  109.  
  110.     IdealGasVacuum(double g): g_(g) {}
  111.  
  112.     RiemannSolution CalcRS
  113.     (size_t idx, const vector<Primitive>& cells) const
  114.     {
  115.       assert(0==idx || cells.size()==idx);
  116.       return RiemannSolution
  117.     (0, calc_vacuum_velocity(idx==cells.size() ?
  118.                  cells.back() : cells.front(),
  119.                  idx==cells.size()));
  120.     }
  121.  
  122.   private:
  123.  
  124.     double calc_vacuum_velocity(const Primitive& cell,
  125.                 bool dir) const
  126.     {
  127.       const double ba = IdealGas(g_).dp2ba(cell.Density, cell.Pressure);
  128.       if(dir)
  129.     return tanh(atanh(celerity2velocity(cell.Celerity))+
  130.             (2./sqrt(g_-1))*
  131.             atanh(ba/sqrt(g_-1)));
  132.       else
  133.     return tanh(atanh(celerity2velocity(cell.Celerity))-
  134.             (2./sqrt(g_-1))*
  135.             atanh(ba/sqrt(g_-1)));
  136.     }
  137.  
  138.     const double g_;
  139.   };
  140.   */
  141.  
  142.   /*
  143.   class ConstGhost: public BoundaryCondition
  144.   {
  145.   public:
  146.  
  147.     ConstGhost(const Primitive& ghost,
  148.            const RiemannSolver& rs):
  149.       ghost_(ghost), rs_(rs) {}
  150.  
  151.     RiemannSolution CalcRS
  152.     (size_t idx, const vector<Primitive>& cells) const
  153.     {
  154.       assert(0==idx || cells.size()==idx);
  155.       return idx==0 ? rs_(ghost_, cells.front()) :
  156.     rs_(cells.back(), ghost_);
  157.     }
  158.  
  159.   private:
  160.     const Primitive ghost_;
  161.     const RiemannSolver& rs_;
  162.   };
  163.   */
  164.  
  165.   class SimData
  166.   {
  167.   public:
  168.  
  169.     SimData(void):
  170.       eos_(4./3.),
  171.       rs_(eos_),
  172.       //rs_(eos_.getAdiabaticIndex()),
  173.       //bc_(Primitive(1e-14,1e-14,0), rs_),
  174.       //      bc_(eos_.getAdiabaticIndex()),
  175.       bc_left_(rs_),
  176.       bc_right_(rs_),
  177.       sr_(),
  178.       geometry_(),
  179.       sim_(generate_grid(-1,-2e-3,0.01),
  180.        PowerLaw(1,1.5),
  181.        //Gaussian(1e2,-1,0.05,1e-14),
  182.        //      TwoSteps(1e-30,-1.01,1e3,-0.99,1e-30),
  183.        Step(1e2,1e-30,-0.99),
  184.        Uniform(0),
  185.        //      bc_, bc_,
  186.        bc_left_,
  187.        bc_right_,
  188.        eos_,
  189.        rs_,
  190.        sr_,
  191.        geometry_) {}
  192.  
  193.     SRHDSimulation& getSim(void)
  194.     {
  195.       return sim_;
  196.     }
  197.  
  198.  
  199.   private:
  200.     IdealGas eos_;
  201.     HLL rs_;
  202.     //    IdealGasRiemannSolver rs_;
  203.     RigidWall bc_left_;
  204.     MockVacuum bc_right_;
  205.     //    ConstGhost bc_;
  206.     //    IdealGasVacuum bc_;
  207.     PCM sr_;
  208.     const Planar geometry_;
  209.     SRHDSimulation sim_;
  210.   };
  211.  
  212.   /*
  213.   class ConditionalSnapshot: public DiagnosticFunction
  214.   {
  215.   public:
  216.  
  217.     ConditionalSnapshot(void):
  218.       rubric_("snapshot_",".h5") {}
  219.  
  220.     void operator()(const SRHDSimulation& sim) const
  221.     {
  222.       if(sim.getHydroSnapshot().edges.back()<0)
  223.     write_hdf5_snapshot(sim, rubric_(sim.GetCycle()));
  224.     }
  225.  
  226.   private:
  227.     mutable Rubric rubric_;
  228.   };
  229.   */
  230.  
  231.   class MyTimeTermination: public TerminationCondition
  232.   {
  233.   public:
  234.    
  235.     MyTimeTermination(void) {}
  236.    
  237.     bool operator()(const SRHDSimulation& sim) const
  238.     {
  239.       //      return sim.getHydroSnapshot().edges.back() > -1e-9;
  240.       //      const size_t n = sim.getHydroSnapshot().cells.size();
  241.       return sim.getHydroSnapshot().cells.back().Celerity >= 2;
  242.       //      return sim.GetTime()>0.1;
  243.     }    
  244.   };
  245.  
  246.   void my_main_loop(SRHDSimulation& sim)
  247.   {
  248.     //    WriteTime diag("time.txt");
  249.     MultipleDiagnostics diag;
  250.     diag.diag_list.push_back(new WriteTime("time.txt"));
  251.     //    diag.diag_list.push_back(new ConditionalSnapshot());
  252.     write_hdf5_snapshot(sim,"initial.h5");
  253.     main_loop(sim, MyTimeTermination(),
  254.           &SRHDSimulation::TimeAdvance, diag);
  255.     write_hdf5_snapshot(sim,"mid.h5");
  256.     for(int tf=10;tf<=1e6;tf*=10){
  257.       SafeTimeTermination stt(static_cast<double>(tf),1e9);
  258.       main_loop(sim, stt,
  259.         &SRHDSimulation::TimeAdvance, diag);
  260.       write_hdf5_snapshot(sim,"milestone_"+int2str(tf)+".h5");
  261.     }
  262.   }
  263. }
  264.  
  265. int main()
  266. {
  267.   /*
  268.   feenableexcept(FE_INVALID   |
  269.          FE_DIVBYZERO |
  270.          FE_OVERFLOW  |
  271.          FE_UNDERFLOW);
  272.   */
  273.  
  274.   my_main_loop(SimData().getSim());
  275.  
  276.   return 0;
  277. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement