Canoe
Comprehensive Atmosphere N' Ocean Engine
mesh_setup.cpp
Go to the documentation of this file.
1 // athena
2 #include <athena/mesh/mesh.hpp>
3 #include <athena/outputs/io_wrapper.hpp>
4 #include <athena/parameter_input.hpp>
5 
6 // application
7 #include <application/command_line.hpp>
8 #include <application/exceptions.hpp>
9 
10 // canoe
11 #include <configure.hpp>
12 
13 #include "impl.hpp"
14 #include "index_map.hpp"
15 
16 // snap
18 
19 // n-body
20 #include "nbody/particle_data.hpp"
21 
22 // MPI headers
23 #ifdef MPI_PARALLEL
24 #include <mpi.h>
25 #endif
26 
27 void mesh_setup(ParameterInput*& pinput, Mesh*& pmesh) {
28  IOWrapper infile, restartfile;
29  auto cli = CommandLine::GetInstance();
30 
31  try {
32  pinput = new ParameterInput;
33 
34  if (cli->res_flag == 1) {
35  restartfile.Open(cli->restart_filename, IOWrapper::FileMode::read);
36  pinput->LoadFromFile(restartfile);
37  // If both -r and -i are specified, make sure next_time gets corrected.
38  // This needs to be corrected on the restart file because we need the old
39  // dt.
40  if (cli->iarg_flag == 1) pinput->RollbackNextTime();
41  // leave the restart file open for later use
42  }
43 
44  if (cli->iarg_flag == 1) {
45  // if both -r and -i are specified, override the parameters using the
46  // input file
47  infile.Open(cli->input_filename, IOWrapper::FileMode::read);
48  pinput->LoadFromFile(infile);
49  infile.Close();
50  }
51  pinput->ModifyFromCmdline(cli->argc, cli->argv);
52  } catch (std::bad_alloc& ba) {
53  if (cli->res_flag == 1) restartfile.Close();
54  throw RuntimeError(
55  "main", "memory allocation failed initializing class ParameterInput:");
56  } catch (std::exception const& ex) {
57  if (cli->res_flag == 1) restartfile.Close();
58  throw RuntimeError("main", ex.what());
59  }
60 
61  // index map
63 
64  // thermodynamics
66 
67  // n-body
69 
70  try {
71  if (cli->res_flag == 0) {
72  pmesh = new Mesh(pinput, cli->mesh_flag);
73  } else {
74  pmesh = new Mesh(pinput, restartfile, cli->mesh_flag);
75  }
76  } catch (std::bad_alloc& ba) {
77  if (cli->res_flag == 1) restartfile.Close();
78  throw RuntimeError("main",
79  "memory allocation failed initializing class Mesh:");
80  } catch (std::exception const& ex) {
81  if (cli->res_flag == 1) restartfile.Close();
82  throw RuntimeError("main", ex.what());
83  }
84 
85  // With current mesh time possibly read from restart file, correct next_time
86  // for outputs
87  if (cli->iarg_flag == 1 && cli->res_flag == 1) {
88  // if both -r and -i are specified, ensure that next_time >= mesh_time - dt
89  pinput->ForwardNextTime(pmesh->time);
90  }
91 
92  // Dump input parameters and quit if code was run with -n option.
93  if (cli->narg_flag) {
94  if (Globals::my_rank == 0) pinput->ParameterDump(std::cout);
95  if (cli->res_flag == 1) restartfile.Close();
96 #ifdef MPI_PARALLEL
97  MPI_Finalize();
98 #endif
99  exit(0);
100  }
101 
102  // everything works fine here, close the restart file
103  if (cli->res_flag == 1) restartfile.Close();
104 
105  // Quit if -m was on cmdline. This option builds and outputs mesh structure.
106  if (cli->mesh_flag > 0) {
107 #ifdef MPI_PARALLEL
108  MPI_Finalize();
109 #endif
110  exit(0);
111  }
112 
113  // set up additional components
114  for (int b = 0; b < pmesh->nblocal; ++b) {
115  MeshBlock* pmb = pmesh->my_blocks(b);
116  pmb->pimpl = std::make_shared<MeshBlock::Impl>(pmb, pinput);
117  }
118 
119  // initialize mesh
120  pmesh->Initialize(cli->res_flag, pinput);
121 }
static IndexMap const * InitFromAthenaInput(ParameterInput *pin)
Definition: index_map.cpp:40
static Thermodynamics const * InitFromAthenaInput(ParameterInput *pin)
void mesh_setup(ParameterInput *&pinput, Mesh *&pmesh)
Definition: mesh_setup.cpp:27
void commit_mpi_particle_data()