Chi-Tech
chi_runtime.h
Go to the documentation of this file.
1#ifndef chi_runtime_h
2#define chi_runtime_h
3
4#include <utility>
5#include <vector>
6#include <string>
7#include <stdexcept>
8#include <memory>
9
10#include "chi_mpi.h"
11
12namespace chi_mesh
13{
14class MeshHandler;
15typedef std::shared_ptr<MeshHandler> MeshHandlerPtr;
16
17class SurfaceMesh;
18typedef std::shared_ptr<SurfaceMesh> SurfaceMeshPtr;
19
22typedef std::shared_ptr<FFInterp> FFInterpPtr;
23
25typedef std::shared_ptr<UnpartitionedMesh> UnpartitionedMeshPtr;
27} // namespace chi_mesh
28
29namespace chi_physics
30{
31class Solver;
32class Material;
33class MultiGroupXS;
34class FieldFunction;
35
36typedef std::shared_ptr<Material> MaterialPtr;
37typedef std::shared_ptr<MultiGroupXS> MultiGroupXSPtr;
38typedef std::shared_ptr<FieldFunction> FieldFunctionPtr;
39} // namespace chi_physics
40
41namespace chi_math
42{
45
46typedef std::shared_ptr<AngularQuadrature> AngularQuadraturePtr;
47typedef std::shared_ptr<SpatialDiscretization> SpatialDiscretizationPtr;
48
49class UnknownManager;
50} // namespace chi_math
51
52namespace chi
53{
54
55class Timer;
56class Console;
57class ChiLog;
58class PostProcessor;
59typedef std::shared_ptr<PostProcessor> PostProcessorPtr;
60/**Stores all the keys currently in the registries.*/
62{
63 std::vector<std::string> objfactory_keys_;
64 std::vector<std::string> console_lua_func_keys_;
65 std::vector<std::string> console_lua_wrapper_keys_;
66};
67} // namespace chi
68
69
70class ChiObject;
71typedef std::shared_ptr<ChiObject> ChiObjectPtr;
72
73// ###################################################################
74/**General utilities in ChiTech*/
75class Chi
76{
77public:
82
83 static std::vector<chi_mesh::MeshHandlerPtr> meshhandler_stack;
85
86 static std::vector<chi_mesh::SurfaceMeshPtr> surface_mesh_stack;
87 static std::vector<chi_mesh::FFInterpPtr> field_func_interpolation_stack;
88 static std::vector<chi_mesh::UnpartMeshPtr> unpartitionedmesh_stack;
89
90 static std::vector<chi_physics::MaterialPtr> material_stack;
91 static std::vector<chi_physics::MultiGroupXSPtr> multigroup_xs_stack;
92 static std::vector<chi_physics::FieldFunctionPtr> field_function_stack;
93
94 static std::vector<chi_math::AngularQuadraturePtr> angular_quadrature_stack;
95
96 static std::vector<ChiObjectPtr> object_stack;
97 static std::vector<chi_math::SpatialDiscretizationPtr> sdm_stack;
98 static std::vector<chi::PostProcessorPtr> postprocessor_stack;
99
100 static const size_t SIZE_T_INVALID = ((size_t)-1);
101
102 // #######################################################
103 /**Data block for run-time quantities.*/
105 {
106 public:
108 static std::string input_file_name_;
112 static bool suppress_color_;
113 static bool dump_registry_;
114
115 static const std::string command_line_help_string_;
116
117 private:
118 friend class Chi;
119 static void ParseArguments(int argc, char** argv);
120 static int InitPetSc(int argc, char** argv);
121
122 public:
123 public:
124 run_time() = delete; // Deleted constructor
125 run_time(const run_time&) = delete; // Deleted copy constructor
126 run_time operator=(const run_time&) = delete; // Deleted assigment operator
127 };
128
129 // #######################################################
130 /**Customized exceptions.*/
131 class RecoverableException : public std::runtime_error
132 {
133 public:
134 explicit RecoverableException(const char* message)
135 : std::runtime_error(std::string("RecoverableException: ") +
136 std::string(message))
137 {
138 }
139 explicit RecoverableException(const std::string& message)
140 : std::runtime_error(std::string("RecoverableException: ") + message)
141 {
142 }
143 RecoverableException(const std::string& prefix, const std::string& message)
144 : std::runtime_error(prefix + message)
145 {
146 }
147
148 ~RecoverableException() noexcept override = default;
149 };
150
151public:
152 Chi() = delete; // Deleted constructor
153 Chi(const Chi&) = delete; // Deleted copy constructor
154 Chi operator=(const Chi&) = delete; // Deleted assigment operator
155
156public:
157 static int RunInteractive(int argc, char** argv);
158 static int RunBatch(int argc, char** argv);
159 static int Initialize(int argc, char** argv, MPI_Comm communicator);
160 static void Finalize();
161 static void Exit(int error_code);
162
163 /**Builds a `RegistryStatuses` structure*/
164 static chi::RegistryStatuses GetStatusOfRegistries();
165
166 static std::string GetVersionStr();
167
168public:
169 /**Attempts to retrieve an object of base-type `shared_ptr<T>` at the given
170 * handle. It then attempts to cast it to type `shared_ptr<R>` and, if
171 * successful, will return a reference of type R&.
172 * \n
173 * \n
174 * Example usage:
175 *
176 * \code
177 * const auto& surf_mesh = Chi::GetStackItem<chi_mesh::SurfaceMesh>(
178 Chi::object_stack, surface_hndl);
179 // Returns chi_mesh::SurfaceMesh&
180 * \endcode
181 * */
182 template <class R, class T>
183 static R& GetStackItem(std::vector<std::shared_ptr<T>>& stack,
184 const size_t handle,
185 const std::string& calling_function_name = "Unknown")
186 {
187 try
188 {
189 std::shared_ptr<T>& item = stack.at(handle);
190 std::shared_ptr<R> ret_item = std::dynamic_pointer_cast<R>(item);
191 if (not ret_item)
192 throw std::logic_error("chi::GetStackItem: Invalid return type used. "
193 "Calling function: " +
194 calling_function_name);
195 return *ret_item;
196 }
197 catch (const std::out_of_range& oor)
198 {
199 throw std::out_of_range("chi::GetStackItem: Invalid handle used. "
200 "Calling function: " +
201 calling_function_name);
202 }
203 }
204
205 /**Attempts to obtain object of type `shared_ptr<T>` at the given
206 * handle of a stack with parent type P.
207 * \n
208 * \n
209 * Example usage:
210 *
211 * \code
212 * auto surf_mesh_ptr =
213 * Chi::GetStackItemPtrAsType<chi_mesh::SurfaceMesh>(
214 Chi::object_stack, surf_mesh_hndle, fname);
215 // Returns std::shared_ptr<chi_mesh::SurfaceMesh>
216 * \endcode
217 * */
218 template <class T, class P>
219 static std::shared_ptr<T>
220 GetStackItemPtrAsType(std::vector<std::shared_ptr<P>>& stack,
221 const size_t handle,
222 const std::string& calling_function_name = "Unknown")
223 {
224 std::shared_ptr<P> item_type_P;
225 try
226 {
227 item_type_P = stack.at(handle);
228 }
229 catch (const std::out_of_range& oor)
230 {
231 throw std::out_of_range("chi::GetStackItem: Invalid handle used. "
232 "Calling function: " +
233 calling_function_name);
234 }
235
236 auto item_type_T = std::dynamic_pointer_cast<T>(item_type_P);
237 if (not item_type_T)
238 throw std::logic_error(calling_function_name +
239 "Failed to cast to requested type");
240
241 return item_type_T;
242 }
243
244 /**Attempts to obtain object of type `shared_ptr<T>` at the given
245 * handle of a stack ALSO OF TYPE T.
246 * \n
247 * \n
248 * Example usage:
249 *
250 * \code
251 * auto surf_mesh_ptr = Chi::GetStackItemPtr(
252 Chi::object_stack, surf_mesh_hndle, fname);
253 // Returns std::shared_ptr<ChiObject>
254 * \endcode
255 * */
256 template <class T>
257 static std::shared_ptr<T>&
258 GetStackItemPtr(std::vector<std::shared_ptr<T>>& stack,
259 const size_t handle,
260 const std::string& calling_function_name = "Unknown")
261 {
262 try
263 {
264 std::shared_ptr<T>& item = stack.at(handle);
265 return item;
266 }
267 catch (const std::out_of_range& oor)
268 {
269 throw std::out_of_range("chi::GetStackItem: Invalid handle used. "
270 "Calling function: " +
271 calling_function_name);
272 }
273 }
274};
275
276#endif
std::shared_ptr< ChiObject > ChiObjectPtr
Definition: chi_runtime.h:70
RecoverableException(const std::string &message)
Definition: chi_runtime.h:139
RecoverableException(const char *message)
Definition: chi_runtime.h:134
~RecoverableException() noexcept override=default
RecoverableException(const std::string &prefix, const std::string &message)
Definition: chi_runtime.h:143
static bool termination_posted_
Definition: chi_runtime.h:107
run_time operator=(const run_time &)=delete
static void ParseArguments(int argc, char **argv)
Definition: chi_runtime.cc:82
static bool supress_beg_end_timelog_
Definition: chi_runtime.h:111
static bool sim_option_interactive_
Definition: chi_runtime.h:109
static bool dump_registry_
Definition: chi_runtime.h:113
run_time()=delete
static const std::string command_line_help_string_
Definition: chi_runtime.h:115
run_time(const run_time &)=delete
static bool allow_petsc_error_handler_
Definition: chi_runtime.h:110
static int InitPetSc(int argc, char **argv)
Definition: chi_runtime.cc:201
static std::string input_file_name_
Definition: chi_runtime.h:108
static bool suppress_color_
Definition: chi_runtime.h:112
static int RunBatch(int argc, char **argv)
Definition: chi_runtime.cc:285
static R & GetStackItem(std::vector< std::shared_ptr< T > > &stack, const size_t handle, const std::string &calling_function_name="Unknown")
Definition: chi_runtime.h:183
static std::vector< chi_mesh::FFInterpPtr > field_func_interpolation_stack
Definition: chi_runtime.h:87
static std::vector< chi_physics::MaterialPtr > material_stack
Definition: chi_runtime.h:90
static std::vector< chi_physics::FieldFunctionPtr > field_function_stack
Definition: chi_runtime.h:92
static chi::Timer program_timer
Definition: chi_runtime.h:79
static std::vector< chi_physics::MultiGroupXSPtr > multigroup_xs_stack
Definition: chi_runtime.h:91
static std::vector< chi_mesh::MeshHandlerPtr > meshhandler_stack
Definition: chi_runtime.h:83
static std::vector< chi_math::AngularQuadraturePtr > angular_quadrature_stack
Definition: chi_runtime.h:94
static std::vector< chi_mesh::UnpartMeshPtr > unpartitionedmesh_stack
Definition: chi_runtime.h:88
static void Exit(int error_code)
Definition: chi_runtime.cc:342
static std::vector< ChiObjectPtr > object_stack
Definition: chi_runtime.h:96
static int RunInteractive(int argc, char **argv)
Definition: chi_runtime.cc:239
static chi::RegistryStatuses GetStatusOfRegistries()
Definition: chi_runtime.cc:350
static std::shared_ptr< T > GetStackItemPtrAsType(std::vector< std::shared_ptr< P > > &stack, const size_t handle, const std::string &calling_function_name="Unknown")
Definition: chi_runtime.h:220
static const size_t SIZE_T_INVALID
Definition: chi_runtime.h:100
static chi::ChiLog & log
Definition: chi_runtime.h:81
static std::vector< chi_mesh::SurfaceMeshPtr > surface_mesh_stack
Definition: chi_runtime.h:86
static std::shared_ptr< T > & GetStackItemPtr(std::vector< std::shared_ptr< T > > &stack, const size_t handle, const std::string &calling_function_name="Unknown")
Definition: chi_runtime.h:258
static std::string GetVersionStr()
Definition: chi_runtime.cc:346
static int Initialize(int argc, char **argv, MPI_Comm communicator)
Definition: chi_runtime.cc:173
static int current_mesh_handler
Definition: chi_runtime.h:84
static void Finalize()
Definition: chi_runtime.cc:216
static std::vector< chi_math::SpatialDiscretizationPtr > sdm_stack
Definition: chi_runtime.h:97
static chi::MPI_Info & mpi
Definition: chi_runtime.h:78
static chi::Console & console
Definition: chi_runtime.h:80
static std::vector< chi::PostProcessorPtr > postprocessor_stack
Definition: chi_runtime.h:98
Definition: PostProcessor.h:28
std::shared_ptr< AngularQuadrature > AngularQuadraturePtr
Definition: chi_runtime.h:44
std::shared_ptr< SpatialDiscretization > SpatialDiscretizationPtr
Definition: chi_runtime.h:47
std::shared_ptr< MeshHandler > MeshHandlerPtr
Definition: chi_runtime.h:14
FieldFunctionInterpolation FFInterp
Definition: chi_runtime.h:20
std::shared_ptr< UnpartitionedMesh > UnpartitionedMeshPtr
Definition: chi_runtime.h:24
std::shared_ptr< FFInterp > FFInterpPtr
Definition: chi_runtime.h:22
std::shared_ptr< SurfaceMesh > SurfaceMeshPtr
Definition: chi_runtime.h:17
UnpartitionedMeshPtr UnpartMeshPtr
Definition: chi_runtime.h:26
std::shared_ptr< FieldFunction > FieldFunctionPtr
Definition: chi_runtime.h:38
std::shared_ptr< MultiGroupXS > MultiGroupXSPtr
Definition: chi_runtime.h:37
std::shared_ptr< Material > MaterialPtr
Definition: chi_runtime.h:34
std::shared_ptr< PostProcessor > PostProcessorPtr
Definition: chi_runtime.h:58
std::vector< std::string > console_lua_func_keys_
Definition: chi_runtime.h:64
std::vector< std::string > objfactory_keys_
Definition: chi_runtime.h:63
std::vector< std::string > console_lua_wrapper_keys_
Definition: chi_runtime.h:65