Plugins
Loading...
Searching...
No Matches
sohle.h
Go to the documentation of this file.
1//
2// Created by julian on 9/20/24.
3//
4
5
6
7#ifndef D3F_UG4_SUBMODULE_SOHLE_H
8#define D3F_UG4_SUBMODULE_SOHLE_H
9#include <sstream>
10
13namespace ug {namespace d3f{
19template <int dim>
20class Sohle : public StdGlobPosData<Sohle<dim>, number, dim> {
21public:
22 virtual void operator() (number& value, const MathVector<dim>& globIP, number time, int si) const {
23 evaluate(value, globIP, time, si);
24 }
25
26 virtual void operator()(number vValue[], const MathVector<dim> vGlobIP[],
27 number time, int si, const size_t nip) const
28 {
29 for (size_t ip = 0; ip < nip; ++ip) {
30 vValue[ip] = vGlobIP[ip][dim - 1]; // Grab the highest dimension coordinate
31 }
32 }
33
34 inline void evaluate(number& value, const MathVector<dim>& globIP, number time, int si) const {
35 value = globIP[dim - 1];
36 }
37
38 template <int refDim>
39 inline void evaluate(number vValue[],
40 const MathVector<dim> vGlobIP[],
41 number time, int si,
42 GridObject* elem,
43 const MathVector<dim> vCornerCoords[],
44 const MathVector<refDim> vLocIP[],
45 const size_t nip,
46 LocalVector* u,
47 const MathMatrix<refDim, dim>* vJT = NULL) const
48 {
49 for (size_t ip = 0; ip < nip; ++ip) {
50 vValue[ip] = vGlobIP[ip][dim - 1];
51 }
52 }
53};
54
60template<typename TDomain>
61class ConstantSohlGradient : public StdDataLinker<ConstantSohlGradient<TDomain>, number, TDomain::dim> {
62public:
63 static const int dim = TDomain::dim;
64 typedef typename TDomain::grid_type TGrid;
65 typedef typename TGrid::template EdgeAttachmentAccessor<ANumber> accessor_type;
66 typedef typename TGrid::template VertexAttachmentAccessor<ANumber> v_accessor_type;
67
72 auto grid = dom->grid();
73 auto sh = dom->subset_handler();
74 auto vAcc = dom->position_accessor();
75
76 // 1. Attach the elevation data to the grid vertices
77 grid->attach_to_vertices(m_aSohle);
78 m_aaSohle.access(*grid, m_aSohle);
79
80 // 2. Parse the starting subsets (comma-separated string)
81 std::stringstream ss(subset);
82 std::string s;
83 std::vector<int> subsets;
84 while (getline(ss, s, ',')) {
85 subsets.push_back(sh->get_subset_index(s.c_str()));
86 }
87
88 // 3. Initialize BFS structures
89 BoolMarker visited(*grid);
90 visited.clear();
91
92 // Queue stores pairs of: {VertexPointer, AccumulatedDistance}
93 std::queue<std::pair<Vertex*, number>> queue;
94
95 // Add all vertices in the starting subsets to the queue
96 for (int subset_id : subsets) {
97 auto vIt = sh->template begin<Vertex>(subset_id, 0);
98 auto vEnd = sh->template end<Vertex>(subset_id, 0);
99 for (; vIt != vEnd; ++vIt) {
100 queue.push({*vIt, 0.0});
101 visited.mark(*vIt);
102 }
103 }
104
105 // 4. Perform BFS Traversal
106 std::vector<Vertex*> neighborhood;
107 while (!queue.empty()) {
108 Vertex* v = queue.front().first;
109 number curr_dist = queue.front().second;
110 queue.pop();
111
112 // Set the bed elevation at this vertex
113 m_aaSohle[v] = slope * curr_dist;
114
115 // Find all connected neighbors
116 CollectNeighbors(neighborhood, *grid, v);
117
118 for (Vertex* neighbor : neighborhood) {
119 if (!visited.is_marked(neighbor)) {
120 visited.mark(neighbor);
121
122 // Get horizontal coordinates (ignore current Z to calculate pure 2D path length)
123 MathVector<dim> v0 = vAcc[v];
124 MathVector<dim> v1 = vAcc[neighbor];
125 v0[dim - 1] = 0.0;
126 v1[dim - 1] = 0.0;
127
128 // Calculate 2D distance and push to queue
129 number step_dist = VecDistance(v0, v1);
130 queue.push({neighbor, curr_dist + step_dist});
131 }
132 }
133 }
134 }
135
136 inline void evaluate(number& value, const MathVector<dim>& globIP, number time, int si) const {
137 UG_THROW("ConstantSohlGradient: Not implemented for single global positions.\n");
138 }
139
143 template <int refDim>
144 inline void evaluate(number vValue[],
145 const MathVector<dim> vGlobIP[],
146 number time, int si,
147 GridObject* elem,
148 const MathVector<dim> vCornerCoords[],
149 const MathVector<refDim> vLocIP[],
150 const size_t nip,
151 LocalVector* u,
152 const MathMatrix<refDim, dim>* vJT = NULL) const
153 {
154 const size_t numSH = 2; // Linear 1D elements have 2 shape functions
155 const ReferenceObjectID roid = elem->reference_object_id();
156
157 const LocalShapeFunctionSet<refDim>& rTrialSpace =
158 LocalFiniteElementProvider::get<refDim>(roid, LFEID(LFEID::LAGRANGE, refDim, 1));
159
160 std::vector<number> vShape;
161
162
163 Edge* edgeElem = static_cast<Edge*>(elem);
164
165 // Loop over integration points
166 for (size_t ip = 0; ip < nip; ++ip) {
167 rTrialSpace.shapes(vShape, vLocIP[ip]);
168 vValue[ip] = 0.0;
169
170 // Loop over shape functions to interpolate the vertex data
171 for (size_t sh = 0; sh < numSH; ++sh) {
172 vValue[ip] += m_aaSohle[edgeElem->vertex(sh)] * vShape[sh];
173 }
174 }
175 }
176
182 template <int refDim>
183 void eval_and_deriv(number vValue[],
184 const MathVector<dim> vGlobIP[],
185 number time, int si,
186 GridObject* elem,
187 const MathVector<dim> vCornerCoords[],
188 const MathVector<refDim> vLocIP[],
189 const size_t nip,
190 LocalVector* u,
191 bool bDeriv,
192 int s,
193 std::vector<std::vector<number>> vvvDeriv[],
194 const MathMatrix<refDim, dim>* vJT = NULL) const
195 {
196 evaluate<refDim>(vValue, vGlobIP, time, si, elem, vCornerCoords, vLocIP, nip, u, vJT);
197 // If bDeriv is true, the vvvDeriv matrices remain zeroed out by the caller, which is correct.
198 }
199
200 bool requires_grid_fct() const { return true; }
201
202private:
205};
206
207
208} // namespace ug
209}
210#endif //UG4_SOHLE_H
parameterString s
Definition Biogas.lua:2
bool is_marked(Edge *e) const
void mark(Edge *e, bool mark=true)
TData & value(size_t s, size_t ip)
virtual Vertex * vertex(size_t index) const
virtual ReferenceObjectID reference_object_id() const=0
int subset() const
number time() const
const MathVector< dim > & ip(size_t s, size_t ip) const
virtual void shapes(std::vector< std::vector< shape_type > > &vvShape, const std::vector< MathVector< dim > > &vLocPos) const=0
Calculates a river bed profile by applying a constant slope starting from a specific boundary.
Definition sohle.h:61
TDomain::grid_type TGrid
Definition sohle.h:64
TGrid::template VertexAttachmentAccessor< ANumber > v_accessor_type
Definition sohle.h:66
void evaluate(number vValue[], const MathVector< dim > vGlobIP[], number time, int si, GridObject *elem, const MathVector< dim > vCornerCoords[], const MathVector< refDim > vLocIP[], const size_t nip, LocalVector *u, const MathMatrix< refDim, dim > *vJT=NULL) const
Interpolates the pre-calculated vertex bed elevations at the integration points.
Definition sohle.h:144
TGrid::template EdgeAttachmentAccessor< ANumber > accessor_type
Definition sohle.h:65
static const int dim
Definition sohle.h:63
bool requires_grid_fct() const
Definition sohle.h:200
void eval_and_deriv(number vValue[], const MathVector< dim > vGlobIP[], number time, int si, GridObject *elem, const MathVector< dim > vCornerCoords[], const MathVector< refDim > vLocIP[], const size_t nip, LocalVector *u, bool bDeriv, int s, std::vector< std::vector< number > > vvvDeriv[], const MathMatrix< refDim, dim > *vJT=NULL) const
Derivatives for the Newton solver. Because bed elevation is constant and doesn't depend on water dept...
Definition sohle.h:183
ConstantSohlGradient(const char *subset, number slope, SmartPtr< TDomain > dom)
Constructor that performs the BFS grid traversal to pre-calculate bed elevations.
Definition sohle.h:71
ANumber m_aSohle
Definition sohle.h:203
void evaluate(number &value, const MathVector< dim > &globIP, number time, int si) const
Definition sohle.h:136
v_accessor_type m_aaSohle
Definition sohle.h:204
Reads the river bed elevation directly from the spatial z-coordinate.
Definition sohle.h:20
void evaluate(number &value, const MathVector< dim > &globIP, number time, int si) const
Definition sohle.h:34
void evaluate(number vValue[], const MathVector< dim > vGlobIP[], number time, int si, GridObject *elem, const MathVector< dim > vCornerCoords[], const MathVector< refDim > vLocIP[], const size_t nip, LocalVector *u, const MathMatrix< refDim, dim > *vJT=NULL) const
Definition sohle.h:39
virtual void operator()(number &value, const MathVector< dim > &globIP, number time, int si) const
Definition sohle.h:22
virtual void operator()(number vValue[], const MathVector< dim > vGlobIP[], number time, int si, const size_t nip) const
Definition sohle.h:26
SmartPtr< TGrid > grid()
void CollectNeighbors(std::vector< Edge * > &vNeighborsOut, Edge *e, Grid &grid, NeighborhoodType nbhType=NHT_VERTEX_NEIGHBORS)
#define UG_THROW(msg)
double number
vector_t::value_type VecDistance(const vector_t &v1, const vector_t &v2)
ReferenceObjectID