Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
FieldRefs.hpp
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
10#pragma once
11
24#include <functional>
25
26namespace shamrock::solvergraph {
27
28 template<class T>
29 class FieldRefs : public IFieldRefs<T> {
30
31 DDPatchDataFieldRef<T> field_refs;
32
34
35 void sync_spans() {
36 this->spans = field_refs.template map<shamrock::PatchDataFieldSpanPointer<T>>(
37 [&](u64 id, std::reference_wrapper<PatchDataField<T>> &pdf) {
38 return pdf.get().get_pointer_span();
39 });
40 }
41
42 public:
43 using IFieldRefs<T>::IFieldRefs;
44
45 virtual DDPatchDataFieldRef<T> &get_refs() { return field_refs; }
46
47 virtual const DDPatchDataFieldRef<T> &get_refs() const { return field_refs; }
48
49 virtual DDPatchDataFieldSpanPointer<T> &get_spans() { return spans; }
50
51 virtual const DDPatchDataFieldSpanPointer<T> &get_spans() const { return spans; }
52
53 inline virtual void check_sizes(const shambase::DistributedData<u32> &sizes) const {
54 on_distributeddata_diff(
55 field_refs,
56 sizes,
57 [&](u64 id) {
59 "Missing field ref in distributed data at id {}\n"
60 "Field name: {}\n"
61 "Field texsymbol: {}",
62 id,
63 this->get_label(),
64 this->get_tex_symbol()));
65 },
66 [](u64 id) {
67 // TODO
68 },
69 [&](u64 id) {
71 "Extra field ref in distributed data at id {}\n"
72 "Field name: {}\n"
73 "Field texsymbol: {}",
74 id,
75 this->get_label(),
76 this->get_tex_symbol()));
77 });
78 }
79
80 inline virtual void ensure_sizes(const shambase::DistributedData<u32> &sizes) {
81 check_sizes(sizes);
82 }
83
84 void set_refs(DDPatchDataFieldRef<T> refs) {
85 field_refs = refs;
86 sync_spans();
87 }
88
89 DDPatchDataFieldRef<T> extract() {
90 DDPatchDataFieldRef<T> refs = std::exchange(field_refs, {});
91 sync_spans();
92 return refs;
93 }
94
95 inline virtual PatchDataField<T> &get(u64 id_patch) { return field_refs.get(id_patch); }
96 inline virtual const PatchDataField<T> &get(u64 id_patch) const {
97 return field_refs.get(id_patch);
98 }
99
100 inline virtual void free_alloc() { set_refs({}); }
101
102 static std::shared_ptr<FieldRefs<T>> make_shared(std::string name, std::string texsymbol) {
103 return std::make_shared<FieldRefs<T>>(name, texsymbol);
104 }
105 };
106} // namespace shamrock::solvergraph
std::uint64_t u64
64 bit unsigned integer
Represents a collection of objects distributed across patches identified by a u64 id.
T & get(u64 id)
Returns a reference to an object in the collection.
virtual const DDPatchDataFieldSpanPointer< T > & get_spans() const
Const variant of get_spans.
Definition FieldRefs.hpp:51
virtual const DDPatchDataFieldRef< T > & get_refs() const
Const variant of get_refs.
Definition FieldRefs.hpp:47
virtual void ensure_sizes(const shambase::DistributedData< u32 > &sizes)
Ensure that the sizes of the patches in the field match the given sizes (Can resize the underlying fi...
Definition FieldRefs.hpp:80
virtual void free_alloc()
Free allocated memory.
virtual void check_sizes(const shambase::DistributedData< u32 > &sizes) const
Check that the sizes of the patches in the field match the given sizes.
Definition FieldRefs.hpp:53
virtual DDPatchDataFieldSpanPointer< T > & get_spans()
Get the DistributedData of spans attached to the underlying field.
Definition FieldRefs.hpp:49
virtual DDPatchDataFieldRef< T > & get_refs()
Get the DistributedData of PatchDataFieldRefs.
Definition FieldRefs.hpp:45
Interface for a solver graph edge representing a field as references to the underlying patch fields.
void throw_with_loc(std::string message, SourceLocation loc=SourceLocation{})
Throw an exception and append the source location to it.