-
Notifications
You must be signed in to change notification settings - Fork 25
[GRSPH] Conservative to primitive variable swap module #1657
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
Open
y-lapeyre
wants to merge
14
commits into
Shamrock-code:main
Choose a base branch
from
y-lapeyre:GR/cons2prime
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.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
8e5898b
baseline
y-lapeyre 8b6fcf5
enthalpy iterator
y-lapeyre 6a2996c
fix types and typos
y-lapeyre c91b588
from PR1
y-lapeyre c71fcc3
compiling first version
y-lapeyre e1bffc1
Merge branch 'main' into GR/cons2prime
y-lapeyre 9d8aab0
[autofix.ci] automatic fix: pre-commit hooks
autofix-ci[bot] db029f5
[gh-action] trigger CI with empty commit
github-actions[bot] 840323c
new solvergraph edges intro
y-lapeyre b42c17d
finished implementation of enthalpy iterator
y-lapeyre 6f4bd2f
debugged
y-lapeyre d38d045
utils update
y-lapeyre 2a4ae22
add metric as edge
y-lapeyre dcafee7
add the contravariant metric to edges
y-lapeyre 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
Some comments aren't visible on the classic Files Changed page.
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
63 changes: 63 additions & 0 deletions
63
src/shammodels/sph/include/shammodels/sph/modules/ConsToPrim.hpp
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,63 @@ | ||
| // -------------------------------------------------------// | ||
| // | ||
| // 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 ConsToPrim.hpp | ||
| * @author Yona Lapeyre (yona.lapeyre@ens-lyon.fr) | ||
| @brief get primitive variables (rho, vel, internal energy) from conserved variables (rho*, | ||
| momentum, entropy) | ||
| */ | ||
|
|
||
| #include "shambackends/vec.hpp" | ||
| #include "shamrock/solvergraph/Field.hpp" | ||
| #include "shamrock/solvergraph/IFieldSpan.hpp" | ||
| #include "shamrock/solvergraph/INode.hpp" | ||
| #include "shamrock/solvergraph/Indexes.hpp" | ||
| #include "shamrock/solvergraph/ScalarsEdge.hpp" | ||
| #include <experimental/mdspan> | ||
|
|
||
| namespace shammodels::sph::modules { | ||
| template<class Tvec, class SizeType, class Layout, class Accessor> | ||
| class NodeConsToPrim : public shamrock::solvergraph::INode { | ||
| using Tscal = shambase::VecComponent<Tvec>; | ||
| Tscal gamma; | ||
|
|
||
| public: | ||
| NodeConsToPrim(Tscal gamma) : gamma(gamma) {} | ||
|
|
||
| using GcovEdge = std::mdspan<Tscal, std::extents<SizeType, 4, 4>, Layout, Accessor>; | ||
|
|
||
| // X_RO(std::mdspan<Tscal, std::extents<SizeType, 4, 4>, Layout, Accessor>, gcov) | ||
| #define NODE_CONS_TO_PRIM(X_RO, X_RW) \ | ||
| /* inputs */ \ | ||
| X_RO(shamrock::solvergraph::Indexes<u32>, sizes) \ | ||
| X_RO(shamrock::solvergraph::IFieldSpan<Tscal>, spans_rhostar) \ | ||
| X_RO(shamrock::solvergraph::IFieldSpan<Tvec>, spans_momentum) \ | ||
| X_RO(shamrock::solvergraph::IFieldSpan<Tscal>, spans_K) \ | ||
| X_RO(GcovEdge, gcon) \ | ||
| X_RO(GcovEdge, gcov) \ | ||
| \ | ||
| /* outputs */ \ | ||
| X_RW(shamrock::solvergraph::IFieldSpan<Tscal>, spans_rho) \ | ||
| X_RW(shamrock::solvergraph::IFieldSpan<Tvec>, spans_vel) \ | ||
| X_RW(shamrock::solvergraph::IFieldSpan<Tscal>, spans_u) \ | ||
| X_RW(shamrock::solvergraph::IFieldSpan<Tscal>, spans_P) | ||
|
|
||
| EXPAND_NODE_EDGES(NODE_CONS_TO_PRIM) | ||
| #undef NODE_CONS_TO_PRIM | ||
|
|
||
| void _impl_evaluate_internal(); | ||
|
|
||
| inline virtual std::string _impl_get_label() const { return "ConsToPrim"; }; | ||
|
|
||
| virtual std::string _impl_get_tex() const; | ||
| }; | ||
| } // namespace shammodels::sph::modules | ||
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,176 @@ | ||
| // -------------------------------------------------------// | ||
| // | ||
| // 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 ConsToPrim.cpp | ||
| * @author Yona Lapeyre (yona.lapeyre@ens-lyon.fr) | ||
| * @brief | ||
| * | ||
| */ | ||
|
|
||
| #include "shammodels/sph/modules/ConsToPrim.hpp" | ||
| #include "shambackends/kernel_call_distrib.hpp" | ||
| #include "shamcomm/logs.hpp" | ||
| #include "shamphys/GRUtils.hpp" | ||
| #include "shamphys/metrics.hpp" | ||
| #include "shamrock/patch/PatchDataField.hpp" | ||
| #include "shamsys/NodeInstance.hpp" | ||
|
|
||
| namespace { | ||
|
|
||
| template<class Tvec> | ||
| struct KernelConsToPrim { | ||
| using Tscal = shambase::VecComponent<Tvec>; | ||
| }; | ||
|
|
||
| } // namespace | ||
|
|
||
| namespace shammodels::sph::modules { | ||
|
|
||
| template<class Tvec, class SizeType, class Layout, class Accessor> | ||
| void NodeConsToPrim<Tvec, SizeType, Layout, Accessor>::_impl_evaluate_internal() { | ||
| auto edges = get_edges(); | ||
| auto &thread_counts = edges.sizes.indexes; | ||
|
|
||
| edges.spans_rhostar.check_sizes(thread_counts); | ||
| edges.spans_momentum.check_sizes(thread_counts); | ||
| edges.spans_K.check_sizes(thread_counts); | ||
|
|
||
| edges.spans_rho.check_sizes(thread_counts); | ||
| edges.spans_vel.check_sizes(thread_counts); | ||
| edges.spans_u.check_sizes(thread_counts); | ||
| edges.spans_P.check_sizes(thread_counts); | ||
|
|
||
| auto &rhostar = edges.spans_rhostar.get_spans(); | ||
| auto &momentum = edges.spans_momentum.get_spans(); | ||
| auto &K = edges.spans_K.get_spans(); | ||
|
|
||
| auto &rho = edges.spans_rho.get_spans(); | ||
| auto &vel = edges.spans_vel.get_spans(); | ||
| auto &u = edges.spans_u.get_spans(); | ||
| auto &P = edges.spans_P.get_spans(); | ||
|
|
||
| auto &gcov = edges.gcov; | ||
| auto &gcon = edges.gcon; | ||
|
|
||
| auto dev_sched = shamsys::instance::get_compute_scheduler_ptr(); | ||
|
|
||
| sham::distributed_data_kernel_call( | ||
| dev_sched, | ||
| sham::DDMultiRef{rhostar, momentum, K}, | ||
| sham::DDMultiRef{rho, vel, u, P}, | ||
| thread_counts, | ||
| [gamma = this->gamma, gcov = edges.gcov]( | ||
| u32 id_a, | ||
| Tscal *__restrict rhostar, | ||
| Tvec *__restrict momentum, | ||
| Tscal *__restrict K, | ||
|
|
||
| Tscal *__restrict rho, | ||
| Tvec *__restrict vel, | ||
| Tscal *__restrict u, | ||
| Tscal *__restrict P) { | ||
| // on patch, no need of neighbours | ||
|
|
||
| // get metric quantities | ||
| const Tscal sqrt_g = get_sqrt_g(vel[id_a], gcov); | ||
| const Tscal inv_sqrt_g = 1. / sqrt_g; | ||
| const Tscal alpha = get_alpha(gcon); | ||
| const Tscal sqrt_gamma_inv = alpha * inv_sqrt_g; | ||
| const Tvec betaUP = get_betaUP(gcon); | ||
| const Tvec betaDOWN = get_betaDOWN(gcon); | ||
| const std::mdspan<Tscal, std::extents<SizeType, 3, 3>, Layout, Accessor> gammaijUP | ||
| = get_gammaijUP(gcov); | ||
|
|
||
| Tscal rho_a = rho[id_a]; | ||
| const Tscal gamfac = gamma / (gamma - 1.); | ||
| Tscal w = 1 + gamfac * P[id_a] / rho[id_a]; // initial guess | ||
| Tscal pp = 0; | ||
| for (u32 i = 0; i < 3; i++) { | ||
| for (u32 j = 0; j < 3; j++) { | ||
| pp += momentum[id_a][i] * gammaijUP(i, j) * momentum[id_a][j]; | ||
| } | ||
| } | ||
|
|
||
| Tscal lorentz_factor = get_lorentz_factor(momentum[id_a], w, gcov); | ||
|
|
||
| // Tscal Kstar = K[id_a] + 0.5 * pp / rhostar[id_a]; | ||
|
|
||
| bool converged = false; | ||
| constexpr Tscal tol = Tscal(1e-12); | ||
| constexpr u32 Nitermax = 100; | ||
| // compute u | ||
| // iterate | ||
| u32 Niter = 0; | ||
|
|
||
| do { | ||
| const Tscal w_old = w; | ||
|
|
||
| lorentz_factor = get_lorentz_factor(momentum[id_a], w, gcov); | ||
|
|
||
| // eq 97 | ||
| rho_a = rhostar[id_a] * sqrt_gamma_inv / lorentz_factor; | ||
|
|
||
| // eq 62 | ||
| P[id_a] = K[id_a] * sycl::pow(rho_a, gamma); | ||
|
|
||
| // eq B4 | ||
| const Tscal f = (1. + gamfac * P[id_a] / rho_a) - w_old; | ||
|
|
||
| // eq B5 ... maybe should use B6 | ||
| const Tscal df = -1 | ||
| + gamfac | ||
| * (1. | ||
| - pp * P[id_a] | ||
| / (lorentz_factor * lorentz_factor * w_old | ||
| * w_old * w_old * rho_a)); | ||
|
|
||
| w = w_old - f / df; | ||
|
|
||
| converged = (sycl::fabs(w - w_old) / w < tol); | ||
| Niter++; | ||
| } while (Niter < Nitermax and !converged); | ||
|
|
||
| if (converged) { | ||
| // compute P, v, u with the last expression of enthalpy | ||
| lorentz_factor = get_lorentz_factor(momentum[id_a], w, gcov); | ||
| rho_a = rhostar[id_a] * sqrt_gamma_inv / lorentz_factor; | ||
| rho[id_a] = rho_a; | ||
| P[id_a] = K[id_a] * sycl::pow(rho_a, gamma); | ||
|
|
||
| Tvec v3d = alpha * momentum[id_a] / (w * lorentz_factor) - betaDOWN; | ||
|
|
||
| // Raise index from down to up | ||
| for (u32 i = 0; i < 3; i++) { | ||
| Tscal vi = 0.; | ||
| for (u32 j = 0; j < 3; j++) { | ||
| vi += gammaijUP(i, j) * v3d[j]; | ||
| } | ||
| vel[id_a][i] = vi; | ||
| } | ||
|
|
||
| u[id_a] = (P[id_a] < Tscal(1e-30)) ? Tscal(0) | ||
| : P[id_a] / (rho_a * (gamma - Tscal(1))); | ||
| } else { | ||
| logger::err_ln( | ||
| "GRSPH", | ||
| "the enthalpy iterator is not converged after", | ||
| Niter, | ||
| "iterations"); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| template<class Tvec, class SizeType, class Layout, class Accessor> | ||
| std::string NodeConsToPrim<Tvec, SizeType, Layout, Accessor>::_impl_get_tex() const { | ||
|
|
||
| return "TODO"; | ||
| } | ||
|
|
||
| } // namespace shammodels::sph::modules |
Oops, something went wrong.
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.