Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
pyAMRZeusModel.cpp
Go to the documentation of this file.
1// -------------------------------------------------------//
2//
3// SHAMROCK code for hydrodynamics
4// Copyright (c) 2021-2026 Timothée David--Cléris <tim.shamrock@proton.me>
5// SPDX-License-Identifier: CeCILL Free Software License Agreement v2.1
6// Shamrock is licensed under the CeCILL 2.1 License, see LICENSE for more information
7//
8// -------------------------------------------------------//
9
20#include <pybind11/functional.h>
21#include <memory>
22
23namespace shammodels::zeus {
24 template<class Tvec, class TgridVec>
25 void add_instance(py::module &m, std::string name_config, std::string name_model) {
26
27 using Tscal = shambase::VecComponent<Tvec>;
28 using Tgridscal = shambase::VecComponent<TgridVec>;
29
30 using T = Model<Tvec, TgridVec>;
31 using TConfig = typename T::Solver::Config;
33
34 shamlog_debug_ln("[Py]", "registering class :", name_config, typeid(T).name());
35 shamlog_debug_ln("[Py]", "registering class :", name_model, typeid(T).name());
36
37 py::class_<TConfig>(m, name_config.c_str())
38 .def(
39 "set_scale_factor",
40 [](TConfig &self, Tscal scale_factor) {
41 self.grid_coord_to_pos_fact = scale_factor;
42 })
43 .def(
44 "set_eos_gamma",
45 [](TConfig &self, Tscal eos_gamma) {
46 self.set_eos_gamma(eos_gamma);
47 })
48 .def(
49 "set_consistent_transport",
50 [](TConfig &self, bool enable) {
51 self.use_consistent_transport = enable;
52 })
53 .def(
54 "set_van_leer",
55 [](TConfig &self, bool enable) {
56 self.use_van_leer = enable;
57 })
58 .def(
59 "set_scheduler_config",
60 [](TConfig &self, u64 split_crit, u64 merge_crit) {
61 self.scheduler_conf.split_load_value = split_crit;
62 self.scheduler_conf.merge_load_value = merge_crit;
63 },
64 py::kw_only(),
65 py::arg("split_load_value"),
66 py::arg("merge_load_value"));
67
68 std::string sod_tube_analysis_name = name_model + "_AnalysisSodTube";
69 py::class_<TAnalysisSodTube>(m, sod_tube_analysis_name.c_str())
70 .def("compute_L2_dist", [](TAnalysisSodTube &self) -> std::tuple<Tscal, Tvec, Tscal> {
71 auto ret = self.compute_L2_dist();
72 return {ret.rho, ret.v, ret.P};
73 });
74 py::class_<T>(m, name_model.c_str())
75 .def("init", &T::init)
76 .def("init_scheduler", &T::init_scheduler)
77 .def("make_base_grid", &T::make_base_grid)
78 .def("dump_vtk", &T::dump_vtk)
79 .def("evolve_once", &T::evolve_once)
80 .def("set_field_value_lambda_f64", &T::template set_field_value_lambda<f64>)
81 .def("set_field_value_lambda_f64_3", &T::template set_field_value_lambda<f64_3>)
82 .def(
83 "gen_default_config",
84 [](T &self) -> TConfig {
85 return TConfig();
86 })
87 .def(
88 "set_solver_config",
89 [](T &self, TConfig cfg) {
90 if (self.ctx.is_scheduler_initialized()) {
92 "Cannot change solver config after scheduler is initialized");
93 }
94 cfg.check_config();
95 self.solver.solver_config = cfg;
96 })
97 .def(
98 "get_cell_coords",
99 [](T &self, std::pair<TgridVec, TgridVec> block_coord, u32 cell_local_id) {
100 return self.get_cell_coords(block_coord, cell_local_id);
101 })
102 .def(
103 "make_analysis_sodtube",
104 [](T &self,
106 Tvec direction,
107 Tscal time_val,
108 Tscal x_ref,
109 Tscal x_min,
110 Tscal x_max) {
111 return std::make_unique<TAnalysisSodTube>(
112 self.ctx,
113 self.solver.solver_config,
114 self.solver.storage,
115 sod,
116 direction,
117 time_val,
118 x_ref,
119 x_min,
120 x_max);
121 });
122 }
123} // namespace shammodels::zeus
124
125Register_pymod(pyamrzeusmodel) {
126
127 py::module mzeus = m.def_submodule("model_zeus", "Shamrock Zeus solver");
128
129 std::string base_name = "ZeusModel";
130 using namespace shammodels::zeus;
131
132 add_instance<f64_3, i64_3>(
133 mzeus, base_name + "_f64_3_i64_3_SolverConfig", base_name + "_f64_3_i64_3_Model");
134
135 using VariantAMRZeusBind = std::variant<std::unique_ptr<Model<f64_3, i64_3>>>;
136
137 m.def(
138 "get_Model_Zeus",
139 [](ShamrockCtx &ctx, std::string vector_type, std::string grid_repr) -> VariantAMRZeusBind {
140 VariantAMRZeusBind ret;
141
142 if (vector_type == "f64_3" && grid_repr == "i64_3") {
143 ret = std::make_unique<Model<f64_3, i64_3>>(ctx);
144 } else {
146 "unknown combination of representation and grid_repr");
147 }
148
149 return ret;
150 },
151 py::kw_only(),
152 py::arg("context"),
153 py::arg("vector_type"),
154 py::arg("grid_repr"));
155}
std::uint32_t u32
32 bit unsigned integer
std::uint64_t u64
64 bit unsigned integer
void throw_with_loc(std::string message, SourceLocation loc=SourceLocation{})
Throw an exception and append the source location to it.
namespace for the zeus model
Pybind11 include and definitions.
#define Register_pymod(placeholdername)
Register a python module init function using static initialisation.