Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
ComputeEos.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
18
21#include "shambase/memory.hpp"
29#include "shamphys/eos.hpp"
38#include <memory>
39
40#define NODE_EDGES(X_RO, X_RW, X_RO_OPTIONAL, X_RW_OPTIONAL) \
41 /* ------------------- inputs ------------------- */ \
42 X_RO(shamrock::solvergraph::IDataEdge<Tscal>, cs) \
43 X_RO(shamrock::solvergraph::IDataEdge<Tscal>, hfactd) \
44 X_RO(shamrock::solvergraph::IDataEdge<Tscal>, pmass) \
45 X_RO_OPTIONAL(shamrock::solvergraph::IFieldSpan<Tscal>, spans_rho) \
46 X_RO_OPTIONAL(shamrock::solvergraph::IFieldSpan<Tscal>, spans_h) \
47 X_RO(shamrock::solvergraph::Indexes<u32>, sizes) \
48 \
49 /* ------------------- outputs ------------------- */ \
50 X_RW(shamrock::solvergraph::IFieldSpan<Tscal>, spans_pressure) \
51 X_RW(shamrock::solvergraph::IFieldSpan<Tscal>, spans_soundspeed)
52
53namespace shammodels::common::modules {
54 template<class Tvec>
55 class ComputeEOSIsothermal : public shamrock::solvergraph::INode {
56
57 using Tscal = shambase::VecComponent<Tvec>;
58
59 public:
60 ComputeEOSIsothermal() = default;
61
62 EXPAND_NODE_EDGES_OPTIONAL(NODE_EDGES)
63
64 inline static void internal_eos(
65 const Tscal &cs, const Tscal &rho, Tscal &pressure, Tscal &soundspeed) noexcept {
67 Tscal P_a = EOS::pressure(cs, rho);
68 pressure = P_a;
69 soundspeed = cs;
70 }
71
73
75
76 auto edges = get_edges();
77
78 bool has_rho = edges.spans_rho.has_value();
79 bool has_h = edges.spans_h.has_value();
80
81 // must have either rho or h
82 if ((has_rho && has_h) || (!has_rho && !has_h)) {
84 "Must have either rho or h");
85 }
86
87 edges.spans_pressure.ensure_sizes(edges.sizes.indexes);
88 edges.spans_soundspeed.ensure_sizes(edges.sizes.indexes);
89
90 Tscal cs = edges.cs.data;
91 Tscal pmass = edges.pmass.data;
92 Tscal hfactd = edges.hfactd.data;
93
94 auto dev_sched = shamsys::instance::get_compute_scheduler_ptr();
95
96 auto out_refs = sham::DDMultiRef{
97 edges.spans_pressure.get_spans(), edges.spans_soundspeed.get_spans()};
98
99 if (has_rho) {
100 auto &spans_rho = edges.spans_rho.value().get();
101 spans_rho.check_sizes(edges.sizes.indexes);
102
104 dev_sched,
105 sham::DDMultiRef{spans_rho.get_spans()},
106 out_refs,
107 edges.sizes.indexes,
108 [cs](u32 gid, const Tscal *rho, Tscal *pressure, Tscal *soundspeed) {
109 Tscal rho_a = rho[gid];
110 internal_eos(cs, rho_a, pressure[gid], soundspeed[gid]);
111 });
112 } else if (has_h) {
113 auto &spans_h = edges.spans_h.value().get();
114 spans_h.check_sizes(edges.sizes.indexes);
115
117 dev_sched,
118 sham::DDMultiRef{spans_h.get_spans()},
119 out_refs,
120 edges.sizes.indexes,
121 [cs, pmass, hfactd](
122 u32 gid, const Tscal *h, Tscal *pressure, Tscal *soundspeed) {
123 using namespace shamrock::sph;
124 Tscal rho = rho_h(pmass, h[gid], hfactd);
125 internal_eos(cs, rho, pressure[gid], soundspeed[gid]);
126 });
127 }
128 }
129
130 inline virtual std::string _impl_get_label() const { return "ComputeEOSIsothermal"; };
131
132 inline virtual std::string _impl_get_tex() const { return "TODO"; };
133 };
134} // namespace shammodels::common::modules
135
136#undef NODE_EDGES
137
138#define NODE_EDGES(X_RO, X_RW, X_RO_OPTIONAL, X_RW_OPTIONAL) \
139 /* ------------------- inputs ------------------- */ \
140 X_RO(shamrock::solvergraph::IDataEdge<Tscal>, gamma) \
141 X_RO(shamrock::solvergraph::IDataEdge<Tscal>, hfactd) \
142 X_RO(shamrock::solvergraph::IDataEdge<Tscal>, pmass) \
143 X_RO_OPTIONAL(shamrock::solvergraph::IFieldSpan<Tscal>, spans_rho) \
144 X_RO_OPTIONAL(shamrock::solvergraph::IFieldSpan<Tscal>, spans_h) \
145 X_RO(shamrock::solvergraph::IFieldSpan<Tscal>, spans_uint) \
146 X_RO(shamrock::solvergraph::Indexes<u32>, sizes) \
147 \
148 /* ------------------- outputs ------------------- */ \
149 X_RW(shamrock::solvergraph::IFieldSpan<Tscal>, spans_pressure) \
150 X_RW(shamrock::solvergraph::IFieldSpan<Tscal>, spans_soundspeed)
151
152namespace shammodels::common::modules {
153 template<class Tvec>
154 class ComputeEOSAdiabatic : public shamrock::solvergraph::INode {
155
156 using Tscal = shambase::VecComponent<Tvec>;
157
158 public:
159 ComputeEOSAdiabatic() = default;
160
161 EXPAND_NODE_EDGES_OPTIONAL(NODE_EDGES)
162
163 inline static void internal_eos(
164 const Tscal &gamma,
165 const Tscal &rho,
166 const Tscal &uint,
167 Tscal &pressure,
168 Tscal &soundspeed) noexcept {
170 Tscal P_a = EOS::pressure(gamma, rho, uint);
171 Tscal cs_a = EOS::cs_from_p(gamma, rho, P_a);
172 pressure = P_a;
173 soundspeed = cs_a;
174 }
175
177
179
180 auto edges = get_edges();
181
182 bool has_rho = edges.spans_rho.has_value();
183 bool has_h = edges.spans_h.has_value();
184
185 // must have either rho or h
186 if ((has_rho && has_h) || (!has_rho && !has_h)) {
188 "Must have either rho or h");
189 }
190
191 edges.spans_pressure.ensure_sizes(edges.sizes.indexes);
192 edges.spans_soundspeed.ensure_sizes(edges.sizes.indexes);
193
194 Tscal gamma = edges.gamma.data;
195 Tscal pmass = edges.pmass.data;
196 Tscal hfactd = edges.hfactd.data;
197
198 auto dev_sched = shamsys::instance::get_compute_scheduler_ptr();
199
200 auto out_refs = sham::DDMultiRef{
201 edges.spans_pressure.get_spans(), edges.spans_soundspeed.get_spans()};
202
203 if (has_rho) {
204 auto &spans_rho = edges.spans_rho.value().get();
205 spans_rho.check_sizes(edges.sizes.indexes);
206
208 dev_sched,
209 sham::DDMultiRef{spans_rho.get_spans(), edges.spans_uint.get_spans()},
210 out_refs,
211 edges.sizes.indexes,
212 [gamma](
213 u32 gid,
214 const Tscal *rho,
215 const Tscal *uint,
216 Tscal *pressure,
217 Tscal *soundspeed) {
218 Tscal rho_a = rho[gid];
219 Tscal uint_a = uint[gid];
220 internal_eos(gamma, rho_a, uint_a, pressure[gid], soundspeed[gid]);
221 });
222 } else if (has_h) {
223 auto &spans_h = edges.spans_h.value().get();
224 spans_h.check_sizes(edges.sizes.indexes);
225
227 dev_sched,
228 sham::DDMultiRef{spans_h.get_spans(), edges.spans_uint.get_spans()},
229 out_refs,
230 edges.sizes.indexes,
231 [gamma, pmass, hfactd](
232 u32 gid,
233 const Tscal *h,
234 const Tscal *uint,
235 Tscal *pressure,
236 Tscal *soundspeed) {
237 using namespace shamrock::sph;
238 Tscal rho = rho_h(pmass, h[gid], hfactd);
239 Tscal uint_a = uint[gid];
240 internal_eos(gamma, rho, uint_a, pressure[gid], soundspeed[gid]);
241 });
242 }
243 }
244
245 inline virtual std::string _impl_get_label() const { return "ComputeEOSAdiabatic"; };
246
247 inline virtual std::string _impl_get_tex() const { return "TODO"; };
248 };
249} // namespace shammodels::common::modules
250
251#undef NODE_EDGES
252
253#define NODE_EDGES(X_RO, X_RW, X_RO_OPTIONAL, X_RW_OPTIONAL) \
254 /* ------------------- inputs ------------------- */ \
255 X_RO(shamrock::solvergraph::IDataEdge<Tscal>, K) \
256 X_RO(shamrock::solvergraph::IDataEdge<Tscal>, gamma) \
257 X_RO(shamrock::solvergraph::IDataEdge<Tscal>, hfactd) \
258 X_RO(shamrock::solvergraph::IDataEdge<Tscal>, pmass) \
259 X_RO_OPTIONAL(shamrock::solvergraph::IFieldSpan<Tscal>, spans_rho) \
260 X_RO_OPTIONAL(shamrock::solvergraph::IFieldSpan<Tscal>, spans_h) \
261 X_RO(shamrock::solvergraph::Indexes<u32>, sizes) \
262 \
263 /* ------------------- outputs ------------------- */ \
264 X_RW(shamrock::solvergraph::IFieldSpan<Tscal>, spans_pressure) \
265 X_RW(shamrock::solvergraph::IFieldSpan<Tscal>, spans_soundspeed)
266
267namespace shammodels::common::modules {
268 template<class Tvec>
269 class ComputeEOSPolytropic : public shamrock::solvergraph::INode {
270
271 using Tscal = shambase::VecComponent<Tvec>;
272
273 public:
274 ComputeEOSPolytropic() = default;
275
276 EXPAND_NODE_EDGES_OPTIONAL(NODE_EDGES)
277
278 inline static void internal_eos(
279 const Tscal &K,
280 const Tscal &gamma,
281 const Tscal &rho,
282 Tscal &pressure,
283 Tscal &soundspeed) noexcept {
285 Tscal P_a = EOS::pressure(gamma, K, rho);
286 Tscal cs_a = EOS::soundspeed(gamma, K, rho);
287 pressure = P_a;
288 soundspeed = cs_a;
289 }
290
292
294
295 auto edges = get_edges();
296
297 bool has_rho = edges.spans_rho.has_value();
298 bool has_h = edges.spans_h.has_value();
299
300 // must have either rho or h
301 if ((has_rho && has_h) || (!has_rho && !has_h)) {
303 "Must have either rho or h");
304 }
305
306 edges.spans_pressure.ensure_sizes(edges.sizes.indexes);
307 edges.spans_soundspeed.ensure_sizes(edges.sizes.indexes);
308
309 Tscal K = edges.K.data;
310 Tscal gamma = edges.gamma.data;
311 Tscal pmass = edges.pmass.data;
312 Tscal hfactd = edges.hfactd.data;
313
314 auto dev_sched = shamsys::instance::get_compute_scheduler_ptr();
315
316 auto out_refs = sham::DDMultiRef{
317 edges.spans_pressure.get_spans(), edges.spans_soundspeed.get_spans()};
318
319 if (has_rho) {
320 auto &spans_rho = edges.spans_rho.value().get();
321 spans_rho.check_sizes(edges.sizes.indexes);
322
324 dev_sched,
325 sham::DDMultiRef{spans_rho.get_spans()},
326 out_refs,
327 edges.sizes.indexes,
328 [K, gamma](u32 gid, const Tscal *rho, Tscal *pressure, Tscal *soundspeed) {
329 Tscal rho_a = rho[gid];
330 internal_eos(K, gamma, rho_a, pressure[gid], soundspeed[gid]);
331 });
332 } else if (has_h) {
333 auto &spans_h = edges.spans_h.value().get();
334 spans_h.check_sizes(edges.sizes.indexes);
335
337 dev_sched,
338 sham::DDMultiRef{spans_h.get_spans()},
339 out_refs,
340 edges.sizes.indexes,
341 [K, gamma, pmass, hfactd](
342 u32 gid, const Tscal *h, Tscal *pressure, Tscal *soundspeed) {
343 using namespace shamrock::sph;
344 Tscal rho = rho_h(pmass, h[gid], hfactd);
345 internal_eos(K, gamma, rho, pressure[gid], soundspeed[gid]);
346 });
347 }
348 }
349
350 inline virtual std::string _impl_get_label() const { return "ComputeEOSPolytropic"; };
351
352 inline virtual std::string _impl_get_tex() const { return "TODO"; };
353 };
354} // namespace shammodels::common::modules
355
356#undef NODE_EDGES
357
358#define NODE_EDGES(X_RO, X_RW, X_RO_OPTIONAL, X_RW_OPTIONAL) \
359 /* ------------------- inputs ------------------- */ \
360 X_RO(shamrock::solvergraph::IDataEdge<Tscal>, cs0) \
361 X_RO(shamrock::solvergraph::IDataEdge<Tscal>, rho0) \
362 X_RO(shamrock::solvergraph::IDataEdge<Tscal>, gamma0) \
363 X_RO(shamrock::solvergraph::IDataEdge<Tscal>, hfactd) \
364 X_RO(shamrock::solvergraph::IDataEdge<Tscal>, pmass) \
365 X_RO_OPTIONAL(shamrock::solvergraph::IFieldSpan<Tscal>, spans_rho) \
366 X_RO_OPTIONAL(shamrock::solvergraph::IFieldSpan<Tscal>, spans_h) \
367 X_RO(shamrock::solvergraph::Indexes<u32>, sizes) \
368 \
369 /* ------------------- outputs ------------------- */ \
370 X_RW(shamrock::solvergraph::IFieldSpan<Tscal>, spans_pressure) \
371 X_RW(shamrock::solvergraph::IFieldSpan<Tscal>, spans_soundspeed)
372
373namespace shammodels::common::modules {
374
375 template<class Tvec>
376 class ComputeEOSBarotropic : public shamrock::solvergraph::INode {
377
378 using Tscal = shambase::VecComponent<Tvec>;
379
380 public:
381 ComputeEOSBarotropic() = default;
382
383 EXPAND_NODE_EDGES_OPTIONAL(NODE_EDGES)
384
385 inline static void internal_eos(
386 const Tscal &cs0,
387 const Tscal &rho0,
388 const Tscal &gamma0,
389 const Tscal &rho,
390 Tscal &pressure,
391 Tscal &soundspeed) noexcept {
393 pressure = EOS::pressure(cs0, rho0, gamma0, rho);
394 soundspeed = EOS::soundspeed(cs0, rho0, gamma0, rho);
395 }
396
398
400
401 auto edges = get_edges();
402
403 bool has_rho = edges.spans_rho.has_value();
404 bool has_h = edges.spans_h.has_value();
405
406 // must have either rho or h
407 if ((has_rho && has_h) || (!has_rho && !has_h)) {
409 "Must have either rho or h");
410 }
411
412 edges.spans_pressure.ensure_sizes(edges.sizes.indexes);
413 edges.spans_soundspeed.ensure_sizes(edges.sizes.indexes);
414
415 Tscal cs0 = edges.cs0.data;
416 Tscal rho0 = edges.rho0.data;
417 Tscal gamma0 = edges.gamma0.data;
418 Tscal pmass = edges.pmass.data;
419 Tscal hfactd = edges.hfactd.data;
420
421 auto dev_sched = shamsys::instance::get_compute_scheduler_ptr();
422
423 auto out_refs = sham::DDMultiRef{
424 edges.spans_pressure.get_spans(), edges.spans_soundspeed.get_spans()};
425
426 if (has_rho) {
427 auto &spans_rho = edges.spans_rho.value().get();
428 spans_rho.check_sizes(edges.sizes.indexes);
429
431 dev_sched,
432 sham::DDMultiRef{spans_rho.get_spans()},
433 out_refs,
434 edges.sizes.indexes,
435 [cs0, rho0, gamma0](
436 u32 gid, const Tscal *rho, Tscal *pressure, Tscal *soundspeed) {
437 internal_eos(cs0, rho0, gamma0, rho[gid], pressure[gid], soundspeed[gid]);
438 });
439 } else if (has_h) {
440 auto &spans_h = edges.spans_h.value().get();
441 spans_h.check_sizes(edges.sizes.indexes);
442
444 dev_sched,
445 sham::DDMultiRef{spans_h.get_spans()},
446 out_refs,
447 edges.sizes.indexes,
448 [cs0, rho0, gamma0, pmass, hfactd](
449 u32 gid, const Tscal *h, Tscal *pressure, Tscal *soundspeed) {
450 using namespace shamrock::sph;
451 Tscal rho = rho_h(pmass, h[gid], hfactd);
452 internal_eos(cs0, rho0, gamma0, rho, pressure[gid], soundspeed[gid]);
453 });
454 }
455 }
456
457 inline virtual std::string _impl_get_label() const { return "ComputeEOSBarotropic"; };
458
459 inline virtual std::string _impl_get_tex() const { return "TODO"; };
460 };
461} // namespace shammodels::common::modules
462
463#undef NODE_EDGES
464
465#define NODE_EDGES(X_RO, X_RW, X_RO_OPTIONAL, X_RW_OPTIONAL) \
466 /* ------------------- inputs ------------------- */ \
467 X_RO(shamrock::solvergraph::IDataEdge<Tscal>, hfactd) \
468 X_RO(shamrock::solvergraph::IDataEdge<Tscal>, pmass) \
469 X_RO_OPTIONAL(shamrock::solvergraph::IFieldSpan<Tscal>, spans_rho) \
470 X_RO_OPTIONAL(shamrock::solvergraph::IFieldSpan<Tscal>, spans_h) \
471 X_RO(shamrock::solvergraph::IFieldSpan<Tscal>, spans_cs0) \
472 X_RO(shamrock::solvergraph::Indexes<u32>, sizes) \
473 \
474 /* ------------------- outputs ------------------- */ \
475 X_RW(shamrock::solvergraph::IFieldSpan<Tscal>, spans_pressure) \
476 X_RW(shamrock::solvergraph::IFieldSpan<Tscal>, spans_soundspeed)
477
478namespace shammodels::common::modules {
479 template<class Tvec>
480 class ComputeEOSLocallyIsothermal : public shamrock::solvergraph::INode {
481
482 using Tscal = shambase::VecComponent<Tvec>;
483
484 public:
485 ComputeEOSLocallyIsothermal() = default;
486
487 EXPAND_NODE_EDGES_OPTIONAL(NODE_EDGES)
488
489 inline static void internal_eos(
490 const Tscal &cs0, const Tscal &rho, Tscal &pressure, Tscal &soundspeed) noexcept {
492 pressure = EOS::pressure_from_cs(cs0 * cs0, rho);
493 soundspeed = cs0;
494 }
495
497
499
500 auto edges = get_edges();
501
502 bool has_rho = edges.spans_rho.has_value();
503 bool has_h = edges.spans_h.has_value();
504
505 // must have either rho or h
506 if ((has_rho && has_h) || (!has_rho && !has_h)) {
508 "Must have either rho or h");
509 }
510
511 edges.spans_pressure.ensure_sizes(edges.sizes.indexes);
512 edges.spans_soundspeed.ensure_sizes(edges.sizes.indexes);
513
514 Tscal pmass = edges.pmass.data;
515 Tscal hfactd = edges.hfactd.data;
516
517 auto dev_sched = shamsys::instance::get_compute_scheduler_ptr();
518
519 auto out_refs = sham::DDMultiRef{
520 edges.spans_pressure.get_spans(), edges.spans_soundspeed.get_spans()};
521
522 if (has_rho) {
523 auto &spans_rho = edges.spans_rho.value().get();
524 spans_rho.check_sizes(edges.sizes.indexes);
525
527 dev_sched,
528 sham::DDMultiRef{spans_rho.get_spans(), edges.spans_cs0.get_spans()},
529 out_refs,
530 edges.sizes.indexes,
531 [](u32 gid,
532 const Tscal *rho,
533 const Tscal *cs0,
534 Tscal *pressure,
535 Tscal *soundspeed) {
536 Tscal rho_a = rho[gid];
537 Tscal cs0_a = cs0[gid];
538 internal_eos(cs0_a, rho_a, pressure[gid], soundspeed[gid]);
539 });
540 } else if (has_h) {
541 auto &spans_h = edges.spans_h.value().get();
542 spans_h.check_sizes(edges.sizes.indexes);
543
545 dev_sched,
546 sham::DDMultiRef{spans_h.get_spans(), edges.spans_cs0.get_spans()},
547 out_refs,
548 edges.sizes.indexes,
549 [pmass, hfactd](
550 u32 gid,
551 const Tscal *h,
552 const Tscal *cs0,
553 Tscal *pressure,
554 Tscal *soundspeed) {
555 using namespace shamrock::sph;
556 Tscal rho = rho_h(pmass, h[gid], hfactd);
557 Tscal cs0_a = cs0[gid];
558 internal_eos(cs0_a, rho, pressure[gid], soundspeed[gid]);
559 });
560 }
561 }
562
563 inline virtual std::string _impl_get_label() const {
564 return "ComputeEOSLocallyIsothermal";
565 };
566
567 inline virtual std::string _impl_get_tex() const { return "TODO"; };
568 };
569} // namespace shammodels::common::modules
570
571#undef NODE_EDGES
572
573#define NODE_EDGES(X_RO, X_RW, X_RO_OPTIONAL, X_RW_OPTIONAL) \
574 /* ------------------- inputs ------------------- */ \
575 X_RO(shamrock::solvergraph::IDataEdge<Tscal>, mu_e) \
576 X_RO(shamrock::solvergraph::IDataEdge<Tscal>, density_unit) \
577 X_RO(shamrock::solvergraph::IDataEdge<Tscal>, pressure_unit) \
578 X_RO(shamrock::solvergraph::IDataEdge<Tscal>, velocity_unit) \
579 X_RO(shamrock::solvergraph::IDataEdge<Tscal>, hfactd) \
580 X_RO(shamrock::solvergraph::IDataEdge<Tscal>, pmass) \
581 X_RO_OPTIONAL(shamrock::solvergraph::IFieldSpan<Tscal>, spans_rho) \
582 X_RO_OPTIONAL(shamrock::solvergraph::IFieldSpan<Tscal>, spans_h) \
583 X_RO(shamrock::solvergraph::Indexes<u32>, sizes) \
584 \
585 /* ------------------- outputs ------------------- */ \
586 X_RW(shamrock::solvergraph::IFieldSpan<Tscal>, spans_pressure) \
587 X_RW(shamrock::solvergraph::IFieldSpan<Tscal>, spans_soundspeed)
588
589namespace shammodels::common::modules {
590 template<class Tvec>
591 class ComputeEOSFermi : public shamrock::solvergraph::INode {
592
593 using Tscal = shambase::VecComponent<Tvec>;
594
595 public:
596 ComputeEOSFermi() = default;
597
598 EXPAND_NODE_EDGES_OPTIONAL(NODE_EDGES)
599
600 inline static void internal_eos(
601 const Tscal &mu_e,
602 const Tscal &density_unit,
603 const Tscal &pressure_unit,
604 const Tscal &velocity_unit,
605 const Tscal &rho,
606 Tscal &pressure,
607 Tscal &soundspeed) noexcept {
608 using EOS = shamphys::EOS_Fermi<Tscal>;
609 auto const res = EOS::pressure_and_soundspeed(mu_e, rho * density_unit);
610 pressure = res.pressure / pressure_unit;
611 soundspeed = res.soundspeed / velocity_unit;
612 }
613
615
617
618 auto edges = get_edges();
619
620 bool has_rho = edges.spans_rho.has_value();
621 bool has_h = edges.spans_h.has_value();
622
623 // must have either rho or h
624 if ((has_rho && has_h) || (!has_rho && !has_h)) {
626 "Must have either rho or h");
627 }
628
629 edges.spans_pressure.ensure_sizes(edges.sizes.indexes);
630 edges.spans_soundspeed.ensure_sizes(edges.sizes.indexes);
631
632 Tscal mu_e = edges.mu_e.data;
633 Tscal density_unit = edges.density_unit.data;
634 Tscal pressure_unit = edges.pressure_unit.data;
635 Tscal velocity_unit = edges.velocity_unit.data;
636 Tscal pmass = edges.pmass.data;
637 Tscal hfactd = edges.hfactd.data;
638
639 auto dev_sched = shamsys::instance::get_compute_scheduler_ptr();
640
641 auto out_refs = sham::DDMultiRef{
642 edges.spans_pressure.get_spans(), edges.spans_soundspeed.get_spans()};
643
644 if (has_rho) {
645 auto &spans_rho = edges.spans_rho.value().get();
646 spans_rho.check_sizes(edges.sizes.indexes);
647
649 dev_sched,
650 sham::DDMultiRef{spans_rho.get_spans()},
651 out_refs,
652 edges.sizes.indexes,
653 [mu_e, density_unit, pressure_unit, velocity_unit](
654 u32 gid, const Tscal *rho, Tscal *pressure, Tscal *soundspeed) {
655 Tscal rho_a = rho[gid];
656 internal_eos(
657 mu_e,
658 density_unit,
659 pressure_unit,
660 velocity_unit,
661 rho_a,
662 pressure[gid],
663 soundspeed[gid]);
664 });
665 } else if (has_h) {
666 auto &spans_h = edges.spans_h.value().get();
667 spans_h.check_sizes(edges.sizes.indexes);
668
670 dev_sched,
671 sham::DDMultiRef{spans_h.get_spans()},
672 out_refs,
673 edges.sizes.indexes,
674 [mu_e, density_unit, pressure_unit, velocity_unit, pmass, hfactd](
675 u32 gid, const Tscal *h, Tscal *pressure, Tscal *soundspeed) {
676 using namespace shamrock::sph;
677 Tscal rho = rho_h(pmass, h[gid], hfactd);
678 internal_eos(
679 mu_e,
680 density_unit,
681 pressure_unit,
682 velocity_unit,
683 rho,
684 pressure[gid],
685 soundspeed[gid]);
686 });
687 }
688 }
689
690 inline virtual std::string _impl_get_label() const { return "ComputeEOSFermi"; };
691
692 inline virtual std::string _impl_get_tex() const { return "TODO"; };
693 };
694} // namespace shammodels::common::modules
695
696#undef NODE_EDGES
697
698template<class Tvec, template<class> class SPHKernel>
699void shammodels::sph::modules::ComputeEos<Tvec, SPHKernel>::compute_eos_internal(
700 const std::shared_ptr<shamrock::solvergraph::IDataEdge<Tscal>> &hfactd,
701 const std::shared_ptr<shamrock::solvergraph::IDataEdge<Tscal>> &pmass,
702 const std::optional<std::shared_ptr<shamrock::solvergraph::IFieldSpan<Tscal>>> &spans_rho,
703 const std::optional<std::shared_ptr<shamrock::solvergraph::IFieldSpan<Tscal>>> &spans_h,
704 const std::optional<std::shared_ptr<shamrock::solvergraph::IFieldSpan<Tscal>>> &spans_uint,
705 const std::shared_ptr<shamrock::solvergraph::Indexes<u32>> &sizes,
706 const std::shared_ptr<shamrock::solvergraph::IFieldSpan<Tscal>> &spans_pressure,
707 const std::shared_ptr<shamrock::solvergraph::IFieldSpan<Tscal>> &spans_soundspeed) {
708
710 = shambase::get_check_ref(storage.ghost_layout.get());
711
712 using namespace shamrock;
713 using namespace shamrock::patch;
714
715 using SolverConfigEOS = typename Config::EOSConfig;
716 using SolverEOS_Isothermal = typename SolverConfigEOS::Isothermal;
717 using SolverEOS_Adiabatic = typename SolverConfigEOS::Adiabatic;
718 using SolverEOS_Polytropic = typename SolverConfigEOS::Polytropic;
719 using SolverEOS_LocallyIsothermal = typename SolverConfigEOS::LocallyIsothermal;
720 using SolverEOS_LocallyIsothermalLP07 = typename SolverConfigEOS::LocallyIsothermalLP07;
721 using SolverEOS_LocallyIsothermalFA2014 = typename SolverConfigEOS::LocallyIsothermalFA2014;
722 using SolverEOS_LocallyIsothermalFA2014Extended =
723 typename SolverConfigEOS::LocallyIsothermalFA2014Extended;
724 using SolverEOS_Fermi = typename SolverConfigEOS::Fermi;
725 using SolverEOS_Barotropic = typename SolverConfigEOS::Barotropic;
726
727 sham::DeviceQueue &q = shamsys::instance::get_compute_scheduler().get_queue();
728 auto dev_sched = shamsys::instance::get_compute_scheduler_ptr();
729
730 const auto &sizes_indexes = shambase::get_check_ref(sizes).indexes;
731
732 shambase::get_check_ref(storage.pressure).ensure_sizes(sizes_indexes);
733 shambase::get_check_ref(storage.soundspeed).ensure_sizes(sizes_indexes);
734
735 bool has_rho = spans_rho.has_value();
736 bool has_h = spans_h.has_value();
737
738 // must have either rho or h
739 if ((!has_rho || has_h) && (has_rho || !has_h)) {
740 throw shambase::make_except_with_loc<std::invalid_argument>("Must have either rho or h");
741 }
742
743 auto map_opt_span
744 = [](const std::optional<std::shared_ptr<shamrock::solvergraph::IFieldSpan<Tscal>>> &opt)
745 -> std::optional<std::reference_wrapper<shamrock::solvergraph::IFieldSpan<Tscal>>> {
746 if (!opt.has_value()) {
747 return std::nullopt;
748 }
749 return std::ref(shambase::get_check_ref(opt.value()));
750 };
751
752 struct Edges {
753 const std::optional<std::reference_wrapper<shamrock::solvergraph::IFieldSpan<Tscal>>>
754 spans_rho;
755 const std::optional<std::reference_wrapper<shamrock::solvergraph::IFieldSpan<Tscal>>>
756 spans_h;
757 const std::optional<std::reference_wrapper<shamrock::solvergraph::IFieldSpan<Tscal>>>
758 spans_uint;
759 } edges{map_opt_span(spans_rho), map_opt_span(spans_h), map_opt_span(spans_uint)};
760
761 auto out_refs = sham::DDMultiRef{
762 shambase::get_check_ref(spans_pressure).get_spans(),
763 shambase::get_check_ref(spans_soundspeed).get_spans()};
764
765 if (SolverEOS_Isothermal *eos_config
766 = std::get_if<SolverEOS_Isothermal>(&solver_config.eos_config.config)) {
767
768 auto cs = shamrock::solvergraph::IDataEdge<Tscal>::make_shared("cs", "c_s");
769 cs->data = eos_config->cs;
770
772 node.set_edges(
773 cs, hfactd, pmass, spans_rho, spans_h, sizes, spans_pressure, spans_soundspeed);
774 node.evaluate();
775 } else if (
776 SolverEOS_Adiabatic *eos_config
777 = std::get_if<SolverEOS_Adiabatic>(&solver_config.eos_config.config)) {
778
779 auto gamma = shamrock::solvergraph::IDataEdge<Tscal>::make_shared("gamma", "\\gamma");
780 gamma->data = eos_config->gamma;
781
783 node.set_edges(
784 gamma,
785 hfactd,
786 pmass,
787 spans_rho,
788 spans_h,
789 spans_uint.value(),
790 sizes,
791 spans_pressure,
792 spans_soundspeed);
793 node.evaluate();
794 } else if (
795 SolverEOS_Polytropic *eos_config
796 = std::get_if<SolverEOS_Polytropic>(&solver_config.eos_config.config)) {
797
798 auto K = shamrock::solvergraph::IDataEdge<Tscal>::make_shared("K", "K");
799 auto gamma = shamrock::solvergraph::IDataEdge<Tscal>::make_shared("gamma", "\\gamma");
800 K->data = eos_config->K;
801 gamma->data = eos_config->gamma;
802
804 node.set_edges(
805 K, gamma, hfactd, pmass, spans_rho, spans_h, sizes, spans_pressure, spans_soundspeed);
806 node.evaluate();
807 } else if (
808 [[maybe_unused]] SolverEOS_LocallyIsothermal *eos_config
809 = std::get_if<SolverEOS_LocallyIsothermal>(&solver_config.eos_config.config)) {
810
811 u32 isoundspeed_interf = ghost_layout.get_field_idx<Tscal>("soundspeed");
812
813 auto soundspeed_refs
814 = shamrock::solvergraph::FieldRefs<Tscal>::make_shared("cs0", "c_{s,0}");
815 auto refs = storage.merged_patchdata_ghost.get()
816 .template map<shamrock::solvergraph::PatchDataFieldRef<Tscal>>(
817 [&](u64 id, PatchDataLayer &mpdat)
819 return mpdat.get_field<Tscal>(isoundspeed_interf);
820 });
821 soundspeed_refs->set_refs(refs);
822
824 node.set_edges(
825 hfactd,
826 pmass,
827 spans_rho,
828 spans_h,
829 soundspeed_refs,
830 sizes,
831 spans_pressure,
832 spans_soundspeed);
833 node.evaluate();
834 } else if (
835 SolverEOS_LocallyIsothermalLP07 *eos_config
836 = std::get_if<SolverEOS_LocallyIsothermalLP07>(&solver_config.eos_config.config)) {
837
838 Tscal cs0 = eos_config->cs0;
839 Tscal r0sq = eos_config->r0 * eos_config->r0;
840 Tscal mq = -eos_config->q;
841
842 Tscal pmass_ = shambase::get_check_ref(pmass).data;
843 Tscal hfactd_ = shambase::get_check_ref(hfactd).data;
844
846 auto refs
847 = storage.merged_xyzh.get()
848 .template map<shamrock::solvergraph::PatchDataFieldRef<Tvec>>(
849 [&](u64 id,
851 return mpdat.get_field<Tvec>(0);
852 });
853 xyz_refs.set_refs(refs);
854
856
857 auto eos_internal = [](Tvec R,
858 Tscal cs0,
859 Tscal r0sq,
860 Tscal mq,
861 Tscal rho_a,
862 Tscal &pressure,
863 Tscal &soundspeed) {
864 Tscal Rsq = sycl::dot(R, R);
865 Tscal cs_sq = EOS::soundspeed_sq(cs0 * cs0, Rsq / r0sq, mq);
866 Tscal cs_out = sycl::sqrt(cs_sq);
867
868 Tscal P_a = EOS::pressure_from_cs(cs_sq, rho_a);
869
870 pressure = P_a;
871 soundspeed = cs_out;
872 };
873
874 if (has_rho) {
875 auto &spans_rho_ = edges.spans_rho.value().get();
876 spans_rho_.check_sizes(sizes_indexes);
878 dev_sched,
879 sham::DDMultiRef{spans_rho_.get_spans(), xyz_refs.get_spans()},
880 out_refs,
881 sizes_indexes,
882 [cs0, r0sq, mq, eos_internal](
883 u32 gid,
884 const Tscal *rho,
885 const Tvec *xyz,
886 Tscal *pressure,
887 Tscal *soundspeed) {
888 Tvec R_a = xyz[gid];
889 Tscal rho_a = rho[gid];
890 eos_internal(R_a, cs0, r0sq, mq, rho_a, pressure[gid], soundspeed[gid]);
891 });
892 } else if (has_h) {
893 auto &spans_h_ = edges.spans_h.value().get();
894 spans_h_.check_sizes(sizes_indexes);
896 dev_sched,
897 sham::DDMultiRef{spans_h_.get_spans(), xyz_refs.get_spans()},
898 out_refs,
899 sizes_indexes,
900 [cs0, r0sq, mq, pmass_, hfactd_, eos_internal](
901 u32 gid, const Tscal *h, const Tvec *xyz, Tscal *pressure, Tscal *soundspeed) {
902 using namespace shamrock::sph;
903 Tvec R_a = xyz[gid];
904 Tscal rho_a = rho_h(pmass_, h[gid], hfactd_);
905 eos_internal(R_a, cs0, r0sq, mq, rho_a, pressure[gid], soundspeed[gid]);
906 });
907 }
908
909 } else if (
910 SolverEOS_LocallyIsothermalFA2014 *eos_config
911 = std::get_if<SolverEOS_LocallyIsothermalFA2014>(&solver_config.eos_config.config)) {
912
913 Tscal G = solver_config.get_constant_G();
914 Tscal h_over_r = eos_config->h_over_r;
915 Tscal pmass_ = shambase::get_check_ref(pmass).data;
916 Tscal hfactd_ = shambase::get_check_ref(hfactd).data;
917
919
920 auto &sink_pos = get_sink_pos<Tvec>(scheduler().synchronized_data);
921 auto &sink_mass = get_sink_mass<Tvec>(scheduler().synchronized_data);
922 u32 sink_cnt = shambase::narrow_or_throw<u32>(sink_pos.size());
923
924 if (sink_cnt == 0) {
926 "No sinks found for the equation of state");
927 }
928
930 auto refs
931 = storage.merged_xyzh.get()
932 .template map<shamrock::solvergraph::PatchDataFieldRef<Tvec>>(
933 [&](u64 id,
935 return mpdat.get_field<Tvec>(0);
936 });
937 xyz_refs.set_refs(refs);
938
939 sham::DeviceBuffer<Tvec> sink_pos_buf(sink_pos.size(), dev_sched);
940 sham::DeviceBuffer<Tscal> sink_mass_buf(sink_mass.size(), dev_sched);
941
942 sink_pos_buf.copy_from_stdvec(sink_pos);
943 sink_mass_buf.copy_from_stdvec(sink_mass);
944
945 auto eos_internal = [](Tvec R,
946 Tscal rho_a,
947 u32 scount,
948 auto spos,
949 auto smass,
950 Tscal G,
951 Tscal h_over_r,
952 Tscal &pressure,
953 Tscal &soundspeed) {
954 Tscal mpotential = 0;
955 for (u32 i = 0; i < scount; i++) {
956 Tvec s_r = spos[i] - R;
957 Tscal s_m = smass[i];
958 Tscal s_r_abs = sycl::length(s_r);
959 mpotential += G * s_m / s_r_abs;
960 }
961
962 Tscal cs_out = h_over_r * sycl::sqrt(mpotential);
963 Tscal P_a = EOS::pressure_from_cs(cs_out * cs_out, rho_a);
964
965 pressure = P_a;
966 soundspeed = cs_out;
967 };
968
969 if (has_rho) {
970 auto &spans_rho_ = edges.spans_rho.value().get();
971 spans_rho_.check_sizes(sizes_indexes);
972
973 sizes_indexes.for_each([&](u64 id, u32 count) {
975 q,
977 spans_rho_.get_spans().get(id),
978 xyz_refs.get_spans().get(id),
979 sink_pos_buf,
980 sink_mass_buf},
981 out_refs.get(id),
982 count,
983 [G, h_over_r, sink_cnt, eos_internal](
984 u32 gid,
985 const Tscal *rho,
986 const Tvec *xyz,
987 const Tvec *spos,
988 const Tscal *smass,
989 Tscal *pressure,
990 Tscal *soundspeed) {
991 Tvec R_a = xyz[gid];
992 Tscal rho_a = rho[gid];
993 eos_internal(
994 R_a,
995 rho_a,
996 sink_cnt,
997 spos,
998 smass,
999 G,
1000 h_over_r,
1001 pressure[gid],
1002 soundspeed[gid]);
1003 });
1004 });
1005
1006 } else if (has_h) {
1007 auto &spans_h_ = edges.spans_h.value().get();
1008 spans_h_.check_sizes(sizes_indexes);
1009
1010 sizes_indexes.for_each([&](u64 id, u32 count) {
1012 q,
1014 spans_h_.get_spans().get(id),
1015 xyz_refs.get_spans().get(id),
1016 sink_pos_buf,
1017 sink_mass_buf},
1018 out_refs.get(id),
1019 count,
1020 [G, h_over_r, sink_cnt, pmass_, hfactd_, eos_internal](
1021 u32 gid,
1022 const Tscal *h,
1023 const Tvec *xyz,
1024 const Tvec *spos,
1025 const Tscal *smass,
1026 Tscal *pressure,
1027 Tscal *soundspeed) {
1028 using namespace shamrock::sph;
1029 Tvec R_a = xyz[gid];
1030 Tscal rho_a = rho_h(pmass_, h[gid], hfactd_);
1031 eos_internal(
1032 R_a,
1033 rho_a,
1034 sink_cnt,
1035 spos,
1036 smass,
1037 G,
1038 h_over_r,
1039 pressure[gid],
1040 soundspeed[gid]);
1041 });
1042 });
1043 }
1044
1045 } else if (
1046 SolverEOS_LocallyIsothermalFA2014Extended *eos_config
1047 = std::get_if<SolverEOS_LocallyIsothermalFA2014Extended>(
1048 &solver_config.eos_config.config)) {
1049
1050 Tscal cs0 = eos_config->cs0;
1051 Tscal r0 = eos_config->r0;
1052 Tscal q_ = eos_config->q;
1053 Tscal pmass_ = shambase::get_check_ref(pmass).data;
1054 Tscal hfactd_ = shambase::get_check_ref(hfactd).data;
1055 u32 n_sinks = eos_config->n_sinks;
1056
1057 // cs = cs0 * (r0 * phi / M)^q, with phi = sum_i m_i / |r - r_i| and M = sum_i m_i, so that
1058 // a single sink gives cs = cs0 * (r / r0)^-q, i.e. cs0 at r0 as for LP07
1059 Tscal r0_q = sycl::pow(r0, q_);
1060
1062
1063 auto &all_sink_pos = get_sink_pos<Tvec>(scheduler().synchronized_data);
1064 auto &all_sink_mass = get_sink_mass<Tvec>(scheduler().synchronized_data);
1065 std::vector<Tvec> sink_pos;
1066 std::vector<Tscal> sink_mass;
1067 u32 sink_cnt = 0;
1068
1069 for (size_t i = 0; i < all_sink_pos.size(); i++) {
1070 sink_pos.push_back(all_sink_pos[i]);
1071 sink_mass.push_back(all_sink_mass[i]);
1072 sink_cnt++;
1073 if (sink_pos.size() >= n_sinks) { // We only consider the first n_sinks sinks
1074 break;
1075 }
1076 }
1077
1078 if (sink_cnt == 0) {
1080 "No sinks found for the equation of state");
1081 }
1082
1084 auto refs
1085 = storage.merged_xyzh.get()
1086 .template map<shamrock::solvergraph::PatchDataFieldRef<Tvec>>(
1087 [&](u64 id,
1089 return mpdat.get_field<Tvec>(0);
1090 });
1091 xyz_refs.set_refs(refs);
1092
1093 sham::DeviceBuffer<Tvec> sink_pos_buf(sink_pos.size(), dev_sched);
1094 sham::DeviceBuffer<Tscal> sink_mass_buf(sink_mass.size(), dev_sched);
1095
1096 sink_pos_buf.copy_from_stdvec(sink_pos);
1097 sink_mass_buf.copy_from_stdvec(sink_mass);
1098
1099 auto eos_internal = [](Tvec R,
1100 Tscal rho_a,
1101 u32 scount,
1102 auto spos,
1103 auto smass,
1104 Tscal cs0,
1105 Tscal r0_q,
1106 Tscal q,
1107 Tscal &pressure,
1108 Tscal &soundspeed) {
1109 Tscal sink_mass_sum = 0;
1110 Tscal pot_sum = 0;
1111 for (u32 i = 0; i < scount; i++) {
1112 Tvec s_r = spos[i] - R;
1113 Tscal s_m = smass[i];
1114 Tscal s_r_abs = sycl::length(s_r);
1115 sink_mass_sum += s_m;
1116 pot_sum += s_m / s_r_abs;
1117 }
1118
1119 Tscal cs_out = cs0 * r0_q * sycl::pow(pot_sum / sink_mass_sum, q);
1120 Tscal P_a = EOS::pressure_from_cs(cs_out * cs_out, rho_a);
1121
1122 pressure = P_a;
1123 soundspeed = cs_out;
1124 };
1125
1126 if (has_rho) {
1127 auto &spans_rho_ = edges.spans_rho.value().get();
1128 spans_rho_.check_sizes(sizes_indexes);
1129
1130 sizes_indexes.for_each([&](u64 id, u32 count) {
1132 q,
1134 spans_rho_.get_spans().get(id),
1135 xyz_refs.get_spans().get(id),
1136 sink_pos_buf,
1137 sink_mass_buf},
1138 out_refs.get(id),
1139 count,
1140 [cs0, r0_q, q_, sink_cnt, eos_internal](
1141 u32 gid,
1142 const Tscal *rho,
1143 const Tvec *xyz,
1144 const Tvec *spos,
1145 const Tscal *smass,
1146 Tscal *pressure,
1147 Tscal *soundspeed) {
1148 Tvec R_a = xyz[gid];
1149 Tscal rho_a = rho[gid];
1150 eos_internal(
1151 R_a,
1152 rho_a,
1153 sink_cnt,
1154 spos,
1155 smass,
1156 cs0,
1157 r0_q,
1158 q_,
1159 pressure[gid],
1160 soundspeed[gid]);
1161 });
1162 });
1163 } else if (has_h) {
1164 auto &spans_h_ = edges.spans_h.value().get();
1165 spans_h_.check_sizes(sizes_indexes);
1166
1167 sizes_indexes.for_each([&](u64 id, u32 count) {
1169 q,
1171 spans_h_.get_spans().get(id),
1172 xyz_refs.get_spans().get(id),
1173 sink_pos_buf,
1174 sink_mass_buf},
1175 out_refs.get(id),
1176 count,
1177 [cs0, r0_q, q_, sink_cnt, pmass_, hfactd_, eos_internal](
1178 u32 gid,
1179 const Tscal *h,
1180 const Tvec *xyz,
1181 const Tvec *spos,
1182 const Tscal *smass,
1183 Tscal *pressure,
1184 Tscal *soundspeed) {
1185 using namespace shamrock::sph;
1186 Tvec R_a = xyz[gid];
1187 Tscal rho_a = rho_h(pmass_, h[gid], hfactd_);
1188 eos_internal(
1189 R_a,
1190 rho_a,
1191 sink_cnt,
1192 spos,
1193 smass,
1194 cs0,
1195 r0_q,
1196 q_,
1197 pressure[gid],
1198 soundspeed[gid]);
1199 });
1200 });
1201 }
1202
1203 } else if (
1204 SolverEOS_Fermi *eos_config
1205 = std::get_if<SolverEOS_Fermi>(&solver_config.eos_config.config)) {
1206
1207 auto unit_sys = *solver_config.unit_sys;
1208
1209 Tscal mass = unit_sys.template to<shamunits::units::kilogram>();
1210 Tscal length = unit_sys.template to<shamunits::units::metre>();
1211 Tscal time = unit_sys.template to<shamunits::units::second>();
1212
1213 auto mu_e = shamrock::solvergraph::IDataEdge<Tscal>::make_shared("mu_e", "\\mu_e");
1214 mu_e->data = eos_config->mu_e;
1215
1216 auto density_unit
1217 = shamrock::solvergraph::IDataEdge<Tscal>::make_shared("density_unit", "\\rho_u");
1218 density_unit->data = mass / (length * length * length);
1219
1220 auto pressure_unit
1221 = shamrock::solvergraph::IDataEdge<Tscal>::make_shared("pressure_unit", "P_u");
1222 pressure_unit->data = mass / length / (time * time);
1223
1224 auto velocity_unit
1225 = shamrock::solvergraph::IDataEdge<Tscal>::make_shared("velocity_unit", "v_u");
1226 velocity_unit->data = length / time;
1227
1229 node.set_edges(
1230 mu_e,
1231 density_unit,
1232 pressure_unit,
1233 velocity_unit,
1234 hfactd,
1235 pmass,
1236 spans_rho,
1237 spans_h,
1238 sizes,
1239 spans_pressure,
1240 spans_soundspeed);
1241 node.evaluate();
1242 } else if (
1243 SolverEOS_Barotropic *eos_config
1244 = std::get_if<SolverEOS_Barotropic>(&solver_config.eos_config.config)) {
1245
1246 auto cs0 = shamrock::solvergraph::IDataEdge<Tscal>::make_shared("cs0", "c_{s,0}");
1247 auto rho0 = shamrock::solvergraph::IDataEdge<Tscal>::make_shared("rho0", "\\rho_0");
1248 auto gamma0 = shamrock::solvergraph::IDataEdge<Tscal>::make_shared("gamma0", "\\gamma_0");
1249 cs0->data = eos_config->cs0;
1250 rho0->data = eos_config->rho0;
1251 gamma0->data = eos_config->gamma0;
1252
1254 node.set_edges(
1255 cs0,
1256 rho0,
1257 gamma0,
1258 hfactd,
1259 pmass,
1260 spans_rho,
1261 spans_h,
1262 sizes,
1263 spans_pressure,
1264 spans_soundspeed);
1265 node.evaluate();
1266 } else {
1268 }
1269}
1270
1271template<class Tvec, template<class> class SPHKernel>
1273
1274 NamedStackEntry stack_loc{"compute eos"};
1275
1276 Tscal gpart_mass = solver_config.gpart_mass;
1277
1278 using namespace shamrock;
1279 using namespace shamrock::patch;
1280
1282 = shambase::get_check_ref(storage.ghost_layout.get());
1283 u32 ihpart_interf = ghost_layout.get_field_idx<Tscal>("hpart");
1284 u32 iuint_interf = ghost_layout.get_field_idx<Tscal>("uint");
1285
1286 auto hfactd = shamrock::solvergraph::IDataEdge<Tscal>::make_shared("hfactd", "hfactd");
1287 auto pmass = shamrock::solvergraph::IDataEdge<Tscal>::make_shared("pmass", "pmass");
1288
1289 hfactd->data = Kernel::hfactd;
1290 pmass->data = gpart_mass;
1291
1292 auto sizes = storage.part_counts_with_ghost;
1293
1294 auto h_refs = shamrock::solvergraph::FieldRefs<Tscal>::make_shared("", "");
1295 {
1296 auto refs = storage.merged_patchdata_ghost.get()
1297 .template map<shamrock::solvergraph::PatchDataFieldRef<Tscal>>(
1298 [&](u64 id, PatchDataLayer &mpdat)
1300 return mpdat.get_field<Tscal>(ihpart_interf);
1301 });
1302 h_refs->set_refs(refs);
1303 }
1304
1305 auto uint_refs = shamrock::solvergraph::FieldRefs<Tscal>::make_shared("", "");
1306 {
1307 auto refs = storage.merged_patchdata_ghost.get()
1308 .template map<shamrock::solvergraph::PatchDataFieldRef<Tscal>>(
1309 [&](u64 id, PatchDataLayer &mpdat)
1311 return mpdat.get_field<Tscal>(iuint_interf);
1312 });
1313 uint_refs->set_refs(refs);
1314 }
1315
1316 auto dev_sched = shamsys::instance::get_compute_scheduler_ptr();
1317
1318 if (solver_config.dust_config.has_epsilon_field()) {
1319
1320 u32 iepsilon_interf = ghost_layout.get_field_idx<Tscal>("epsilon");
1321 u32 nvar_dust = solver_config.dust_config.get_dust_nvar();
1322
1323 auto rho_g = std::make_shared<shamrock::solvergraph::Field<Tscal>>(1, "rho_g", "rho_g");
1324 auto uint_g = std::make_shared<shamrock::solvergraph::Field<Tscal>>(1, "uint_g", "uint_g");
1325
1326 rho_g->ensure_sizes(shambase::get_check_ref(sizes).indexes);
1327 uint_g->ensure_sizes(shambase::get_check_ref(sizes).indexes);
1328
1329 shamrock::solvergraph::FieldRefs<Tscal> epsilon_refs{"", ""};
1330 auto refs = storage.merged_patchdata_ghost.get()
1331 .template map<shamrock::solvergraph::PatchDataFieldRef<Tscal>>(
1332 [&](u64 id, PatchDataLayer &mpdat)
1334 return mpdat.get_field<Tscal>(iepsilon_interf);
1335 });
1336 epsilon_refs.set_refs(refs);
1337
1339 dev_sched,
1340 sham::DDMultiRef{h_refs->get_spans(), uint_refs->get_spans(), epsilon_refs.get_spans()},
1341 sham::DDMultiRef{rho_g->get_spans(), uint_g->get_spans()},
1342 shambase::get_check_ref(sizes).indexes,
1343 [pmass = pmass->data, hfactd = hfactd->data, nvar_dust](
1344 u32 gid,
1345 const Tscal *h,
1346 const Tscal *uint,
1347 const Tscal *epsilon,
1348 Tscal *rho_g,
1349 Tscal *uint_g) {
1350 using namespace shamrock::sph;
1351 Tscal rho_a = rho_h(pmass, h[gid], hfactd);
1352 Tscal uint_a = uint[gid];
1353
1354 Tscal epsilon_sum = 0;
1355 for (u32 j = 0; j < nvar_dust; j++) {
1356 epsilon_sum += epsilon[gid * nvar_dust + j];
1357 }
1358
1359 Tscal rho_g_a = rho_a * (1 - epsilon_sum);
1360 Tscal uint_g_a = uint_a / (1 - epsilon_sum);
1361
1362 rho_g[gid] = rho_g_a;
1363 uint_g[gid] = uint_g_a;
1364 });
1365
1366 compute_eos_internal(
1367 hfactd,
1368 pmass,
1369 rho_g,
1370 std::nullopt,
1371 uint_g,
1372 sizes,
1373 storage.pressure,
1374 storage.soundspeed);
1375 } else if (solver_config.dust_config.has_s_j_field()) {
1376
1377 u32 is_j_interf = ghost_layout.get_field_idx<Tscal>("s_j");
1378 u32 nvar_dust = solver_config.dust_config.get_dust_nvar();
1379
1380 auto rho_g = std::make_shared<shamrock::solvergraph::Field<Tscal>>(1, "rho_g", "rho_g");
1381 auto uint_g = std::make_shared<shamrock::solvergraph::Field<Tscal>>(1, "uint_g", "uint_g");
1382
1383 rho_g->ensure_sizes(shambase::get_check_ref(sizes).indexes);
1384 uint_g->ensure_sizes(shambase::get_check_ref(sizes).indexes);
1385
1387 auto refs = storage.merged_patchdata_ghost.get()
1388 .template map<shamrock::solvergraph::PatchDataFieldRef<Tscal>>(
1389 [&](u64 id, PatchDataLayer &mpdat)
1391 return mpdat.get_field<Tscal>(is_j_interf);
1392 });
1393 s_j_refs.set_refs(refs);
1394
1396 dev_sched,
1397 sham::DDMultiRef{h_refs->get_spans(), uint_refs->get_spans(), s_j_refs.get_spans()},
1398 sham::DDMultiRef{rho_g->get_spans(), uint_g->get_spans()},
1399 shambase::get_check_ref(sizes).indexes,
1400 [pmass = pmass->data, hfactd = hfactd->data, nvar_dust](
1401 u32 gid,
1402 const Tscal *h,
1403 const Tscal *uint,
1404 const Tscal *s_j,
1405 Tscal *rho_g,
1406 Tscal *uint_g) {
1407 using namespace shamrock::sph;
1408 Tscal rho_a = rho_h(pmass, h[gid], hfactd);
1409 Tscal uint_a = uint[gid];
1410
1411 Tscal epsilon_sum = 0;
1412 for (u32 j = 0; j < nvar_dust; j++) {
1413 Tscal s = s_j[gid * nvar_dust + j];
1414 epsilon_sum += s * s / rho_a;
1415 }
1416
1417 Tscal rho_g_a = rho_a * (1 - epsilon_sum);
1418 Tscal uint_g_a = uint_a / (1 - epsilon_sum);
1419
1420 rho_g[gid] = rho_g_a;
1421 uint_g[gid] = uint_g_a;
1422 });
1423
1424 compute_eos_internal(
1425 hfactd,
1426 pmass,
1427 rho_g,
1428 std::nullopt,
1429 uint_g,
1430 sizes,
1431 storage.pressure,
1432 storage.soundspeed);
1433 } else {
1434
1435 compute_eos_internal(
1436 hfactd,
1437 pmass,
1438 std::nullopt,
1439 h_refs,
1440 uint_refs,
1441 sizes,
1442 storage.pressure,
1443 storage.soundspeed);
1444 }
1445}
1446
1447using namespace shammath;
1451
constexpr const char * xyz
Position field (3D coordinates).
constexpr const char * soundspeed
Sound speed c_s (derived from EOS).
constexpr const char * pressure
Pressure P (derived from EOS).
std::reference_wrapper< PatchDataField< T > > PatchDataFieldRef
Alias for a reference to a PatchDataField.
std::uint32_t u32
32 bit unsigned integer
std::uint64_t u64
64 bit unsigned integer
A buffer allocated in USM (Unified Shared Memory).
A SYCL queue associated with a device and a context.
virtual std::string _impl_get_label() const
get the label of the node
virtual std::string _impl_get_tex() const
get the tex of the node
virtual std::string _impl_get_label() const
get the label of the node
virtual std::string _impl_get_tex() const
get the tex of the node
virtual std::string _impl_get_label() const
get the label of the node
virtual std::string _impl_get_tex() const
get the tex of the node
void _impl_evaluate_internal()
evaluate the node
virtual std::string _impl_get_label() const
get the label of the node
virtual std::string _impl_get_tex() const
get the tex of the node
virtual std::string _impl_get_tex() const
get the tex of the node
virtual std::string _impl_get_label() const
get the label of the node
virtual std::string _impl_get_label() const
get the label of the node
virtual std::string _impl_get_tex() const
get the tex of the node
Module for computing equation of state quantities.
void compute_eos()
Computes pressure and sound speed from equation of state.
u32 get_field_idx(const std::string &field_name) const
Get the field id if matching name & type.
PatchDataLayer container class, the layout is described in patchdata_layout.
virtual DDPatchDataFieldSpanPointer< T > & get_spans()
Get the DistributedData of spans attached to the underlying field.
Definition FieldRefs.hpp:49
Interface for a solver graph edge representing a field as spans.
Inode is node between data edges, takes multiple inputs, multiple outputs.
Definition INode.hpp:31
void evaluate()
Evaluate the node.
Definition INode.hpp:156
constexpr Unit h
Planck constant.
This header file contains utility functions related to exception handling in the code.
void distributed_data_kernel_call(sham::DeviceScheduler_ptr dev_sched, RefIn in, RefOut in_out, const shambase::DistributedData< index_t > &thread_counts, Functor &&func)
A variant of sham::kernel_call for distributed data.
void kernel_call(sham::DeviceQueue &q, RefIn in, RefOut in_out, u32 n, Functor &&func, SourceLocation &&callsite=SourceLocation{})
Submit a kernel to a SYCL queue.
T & get_check_ref(const std::unique_ptr< T > &ptr, SourceLocation loc=SourceLocation())
Takes a std::unique_ptr and returns a reference to the object it holds. It throws a std::runtime_erro...
Definition memory.hpp:112
ExcptTypes make_except_with_loc(std::string message, SourceLocation loc=SourceLocation{})
Create an exception with a message and a location.
void throw_unimplemented(SourceLocation loc=SourceLocation{})
Throw a std::runtime_error saying that the function is unimplemented.
namespace for math utility
Definition AABB.hpp:26
namespace for the main framework
Definition __init__.py:1
Utilities for safe type narrowing conversions.
Helpers to access SPH sink particles stored as SoA synchronized data edges.
sph kernels
#define __shamrock_stack_entry()
Macro to create a stack entry.
shambase::details::NamedBasicStackEntry NamedStackEntry
Alias for shambase::details::NamedBasicStackEntry.
A variant of sham::MultiRef for distributed data.
A class that references multiple buffers or similar objects.
Definition MultiRef.hpp:33
Adiabatic equation of state.
Definition eos.hpp:43
Barotropic equation of state, isothermal at low density and adiabatic at high density (e....
Definition eos.hpp:89
Fermi Gas EoS.
Definition eos.hpp:224
Isothermal equation of state.
Definition eos.hpp:30
Locally isothermal equation of state with radial dependence.
Definition eos.hpp:115
Polytropic equation of state.
Definition eos.hpp:64
The table of units: one constexpr Unit per unit, plus its name registry.