-
Notifications
You must be signed in to change notification settings - Fork 34
[Ramses] draft new setup routine for grid solver #1922
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
tdavidcl
wants to merge
20
commits into
Shamrock-code:main
Choose a base branch
from
tdavidcl:better_ramses_init
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
fbf4e21
[Ramses] draft new setup method for grid solver
tdavidcl bcbd47e
Update examples/ramses/run_advect.py
tdavidcl ce09e0a
Merge branch 'main' into better_ramses_init
mergify[bot] 0218d07
Merge branch 'main' into better_ramses_init
mergify[bot] fcd1ee7
Merge branch 'main' into better_ramses_init
mergify[bot] fb04735
Merge branch 'main' into better_ramses_init
mergify[bot] 335fe34
Merge branch 'main' into better_ramses_init
mergify[bot] 1dc1b85
Merge branch 'main' into better_ramses_init
mergify[bot] 21b1ce3
Merge branch 'main' into better_ramses_init
mergify[bot] f35074b
Merge branch 'main' into better_ramses_init
mergify[bot] 381aa15
Merge branch 'main' into better_ramses_init
mergify[bot] 46b5c03
Merge branch 'main' into better_ramses_init
mergify[bot] ed5e8d4
Merge branch 'main' into better_ramses_init
mergify[bot] fe5f775
Merge branch 'main' into better_ramses_init
mergify[bot] 8be23a5
Merge branch 'main' into better_ramses_init
mergify[bot] a7b14f8
Merge branch 'main' into better_ramses_init
mergify[bot] 0cf5dec
Merge branch 'main' into better_ramses_init
mergify[bot] 818a93b
Merge branch 'main' into better_ramses_init
mergify[bot] 1b2b817
Merge branch 'main' into better_ramses_init
mergify[bot] 51d90f7
Merge branch 'main' into better_ramses_init
mergify[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| // -------------------------------------------------------// | ||
| // | ||
| // SHAMROCK code for hydrodynamics | ||
| // Copyright (c) 2021-2026 Timothée David--Cléris <tim.shamrock@proton.me> | ||
| // SPDX-License-Identifier: CeCILL Free Software License Agreement v2.1 | ||
| // Shamrock is licensed under the CeCILL 2.1 License, see LICENSE for more information | ||
| // | ||
| // -------------------------------------------------------// | ||
|
|
||
| #pragma once | ||
|
|
||
| /** | ||
| * @file PatchDataSetup.hpp | ||
| * @author Timothée David--Cléris (tim.shamrock@proton.me) | ||
| * @brief Proxy for patch field get/set during Python IC / field setup. | ||
| */ | ||
|
|
||
| #include "shambase/exception.hpp" | ||
| #include "shambase/string.hpp" | ||
| #include "shambackends/typeAliasVec.hpp" | ||
| #include <pybind11/numpy.h> | ||
| #include <pybind11/pybind11.h> | ||
| #include <unordered_map> | ||
| #include <functional> | ||
| #include <string> | ||
|
|
||
| namespace py = pybind11; | ||
|
|
||
| namespace shamrock { | ||
|
|
||
| /** | ||
| * @brief Thin proxy over named field getters/setters as float64 numpy arrays. | ||
| * | ||
| * Does not own PatchData. Real fields and virtual (computed) fields share the | ||
| * same get/set path via registered lambdas. | ||
| */ | ||
| class PatchDataSetup { | ||
| std::unordered_map<std::string, std::function<py::array_t<f64>()>> getters; | ||
| std::unordered_map<std::string, std::function<void(py::array_t<f64>)>> setters; | ||
|
|
||
| public: | ||
| void register_getter(std::string name, std::function<py::array_t<f64>()> fn) { | ||
| getters[std::move(name)] = std::move(fn); | ||
| } | ||
|
|
||
| void register_setter(std::string name, std::function<void(py::array_t<f64>)> fn) { | ||
| setters[std::move(name)] = std::move(fn); | ||
| } | ||
|
|
||
| py::array_t<f64> get(const std::string &name) const { | ||
| auto it = getters.find(name); | ||
| if (it == getters.end()) { | ||
| throw shambase::make_except_with_loc<std::invalid_argument>(shambase::format( | ||
| "PatchDataSetup: no getter registered for field \"{}\"", name)); | ||
| } | ||
| return it->second(); | ||
| } | ||
|
|
||
| void set(const std::string &name, py::array_t<f64> value) const { | ||
| auto it = setters.find(name); | ||
| if (it == setters.end()) { | ||
| throw shambase::make_except_with_loc<std::invalid_argument>(shambase::format( | ||
| "PatchDataSetup: no setter registered for field \"{}\"", name)); | ||
| } | ||
| it->second(std::move(value)); | ||
| } | ||
| }; | ||
|
|
||
| } // namespace shamrock |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,8 +15,11 @@ | |
| * @brief | ||
| */ | ||
|
|
||
| #include "shambase/exception.hpp" | ||
| #include "shambase/string.hpp" | ||
| #include "shambindings/pybind11_stl.hpp" | ||
| #include "shambindings/pybindaliases.hpp" | ||
| #include "shampylib/PatchDataSetup.hpp" | ||
| #include "shamrock/patch/PatchDataLayer.hpp" | ||
| #include <pybind11/numpy.h> | ||
| #include <pybind11/pybind11.h> | ||
|
|
@@ -248,4 +251,89 @@ namespace shamrock { | |
|
|
||
| return dic_out; | ||
| } | ||
|
|
||
| template<class T> | ||
| class NumpyToVec; | ||
|
|
||
| template<> | ||
| class NumpyToVec<f64> { | ||
| public: | ||
| static std::vector<f64> convert(py::array_t<f64> arr) { | ||
| if (arr.ndim() != 1) { | ||
| throw shambase::make_except_with_loc<std::invalid_argument>( | ||
| shambase::format("expected 1D array for f64 field, got ndim={}", arr.ndim())); | ||
| } | ||
|
|
||
| auto r = arr.unchecked<1>(); | ||
| std::vector<f64> vec(static_cast<size_t>(r.shape(0))); | ||
| for (py::ssize_t i = 0; i < r.shape(0); i++) { | ||
| vec[static_cast<size_t>(i)] = r(i); | ||
| } | ||
| return vec; | ||
| } | ||
| }; | ||
|
|
||
| template<> | ||
| class NumpyToVec<f64_3> { | ||
| public: | ||
| static std::vector<f64_3> convert(py::array_t<f64> arr) { | ||
| if (arr.ndim() != 2 || arr.shape(1) != 3) { | ||
| throw shambase::make_except_with_loc<std::invalid_argument>( | ||
| "expected (N, 3) array for f64_3 field"); | ||
| } | ||
|
|
||
| auto r = arr.unchecked<2>(); | ||
| std::vector<f64_3> vec(static_cast<size_t>(r.shape(0))); | ||
| for (py::ssize_t i = 0; i < r.shape(0); i++) { | ||
| vec[static_cast<size_t>(i)] = f64_3{r(i, 0), r(i, 1), r(i, 2)}; | ||
| } | ||
| return vec; | ||
| } | ||
| }; | ||
|
|
||
| inline void register_field_io_f64(PatchDataSetup &setup, PatchDataField<f64> &field) { | ||
| std::string name = field.get_name(); | ||
| setup.register_getter(name, [&field]() -> py::array_t<f64> { | ||
| return VecToNumpy<f64>::convert(field.get_buf().copy_to_stdvec()); | ||
| }); | ||
| setup.register_setter(name, [&field](py::array_t<f64> arr) { | ||
| auto vec = NumpyToVec<f64>::convert(arr); | ||
| if (vec.size() != field.get_val_cnt()) { | ||
| throw shambase::make_except_with_loc<std::invalid_argument>(shambase::format( | ||
| "field \"{}\": array size {} does not match field val_cnt {}", | ||
| field.get_name(), | ||
| vec.size(), | ||
| field.get_val_cnt())); | ||
| } | ||
| field.get_buf().copy_from_stdvec(vec); | ||
| }); | ||
| } | ||
|
|
||
| inline void register_field_io_f64_3(PatchDataSetup &setup, PatchDataField<f64_3> &field) { | ||
| std::string name = field.get_name(); | ||
| setup.register_getter(name, [&field]() -> py::array_t<f64> { | ||
| return VecToNumpy<f64_3>::convert(field.get_buf().copy_to_stdvec()); | ||
| }); | ||
| setup.register_setter(name, [&field](py::array_t<f64> arr) { | ||
| auto vec = NumpyToVec<f64_3>::convert(arr); | ||
| if (vec.size() != field.get_val_cnt()) { | ||
| throw shambase::make_except_with_loc<std::invalid_argument>(shambase::format( | ||
| "field \"{}\": array size {} does not match field val_cnt {}", | ||
| field.get_name(), | ||
| vec.size(), | ||
| field.get_val_cnt())); | ||
| } | ||
| field.get_buf().copy_from_stdvec(vec); | ||
| }); | ||
| } | ||
|
|
||
| inline void register_f64_layout_fields( | ||
| PatchDataSetup &setup, shamrock::patch::PatchDataLayer &pdat) { | ||
| pdat.for_each_field<f64>([&](auto &field) { | ||
| register_field_io_f64(setup, field); | ||
| }); | ||
| pdat.for_each_field<f64_3>([&](auto &field) { | ||
| register_field_io_f64_3(setup, field); | ||
| }); | ||
| } | ||
|
Comment on lines
+294
to
+338
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The logic for registering template<typename T>
inline void register_field_io(PatchDataSetup &setup, PatchDataField<T> &field) {
std::string name = field.get_name();
setup.register_getter(name, [&field]() -> py::array_t<f64> {
return VecToNumpy<T>::convert(field.get_buf().copy_to_stdvec());
});
setup.register_setter(name, [&field](py::array_t<f64> arr) {
auto vec = NumpyToVec<T>::convert(arr);
if (vec.size() != field.get_val_cnt()) {
throw shambase::make_except_with_loc<std::invalid_argument>(shambase::format(
"field \"{}\": array size {} does not match field val_cnt {}",
field.get_name(),
vec.size(),
field.get_val_cnt()));
}
field.get_buf().copy_from_stdvec(vec);
});
}
inline void register_f64_layout_fields(
PatchDataSetup &setup, shamrock::patch::PatchDataLayer &pdat) {
pdat.for_each_field<f64>([&](auto &field) {
register_field_io<f64>(setup, field);
});
pdat.for_each_field<f64_3>([&](auto &field) {
register_field_io<f64_3>(setup, field);
});
}References
|
||
| } // namespace shamrock | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| // -------------------------------------------------------// | ||
| // | ||
| // SHAMROCK code for hydrodynamics | ||
| // Copyright (c) 2021-2026 Timothée David--Cléris <tim.shamrock@proton.me> | ||
| // SPDX-License-Identifier: CeCILL Free Software License Agreement v2.1 | ||
| // Shamrock is licensed under the CeCILL 2.1 License, see LICENSE for more information | ||
| // | ||
| // -------------------------------------------------------// | ||
|
|
||
| /** | ||
| * @file pyPatchDataSetup.cpp | ||
| * @author Timothée David--Cléris (tim.shamrock@proton.me) | ||
| * @brief Python bindings for PatchDataSetup | ||
| */ | ||
|
|
||
| #include "shambindings/pybindaliases.hpp" | ||
| #include "shamcomm/logs.hpp" | ||
| #include "shampylib/PatchDataSetup.hpp" | ||
|
|
||
| ON_PYTHON_INIT { | ||
| auto &m = root_module; | ||
|
|
||
| shamlog_debug_ln("[Py]", "registering shamrock.PatchDataSetup"); | ||
|
|
||
| py::class_<shamrock::PatchDataSetup>(m, "PatchDataSetup") | ||
| .def("get", &shamrock::PatchDataSetup::get, py::arg("name")) | ||
| .def("set", &shamrock::PatchDataSetup::set, py::arg("name"), py::arg("value")); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.