PRISMS-PF Manual
Loading...
Searching...
No Matches
dependencies.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: © 2025 PRISMS Center at the University of Michigan
2// SPDX-License-Identifier: GNU Lesser General Public Version 2.1
3
4#pragma once
5
6#include <deal.II/base/exceptions.h>
7#include <deal.II/base/utilities.h>
8#include <deal.II/matrix_free/evaluation_flags.h>
9
13#include <prismspf/core/types.h>
14
15#include <prismspf/config.h>
16
17#include <array>
18#include <set>
19#include <string>
20#include <vector>
21
23
24// NOLINTBEGIN(misc-non-private-member-variables-in-classes, hicpp-explicit-conversions)
25
30{
31 using EvalFlags = dealii::EvaluationFlags::EvaluationFlags;
32
36 EvalFlags flag = EvalFlags::nothing;
37
42 EvalFlags src_flag = EvalFlags::nothing;
43
47 std::vector<EvalFlags> old_flags;
48
52 Dependency(EvalFlags _flag = EvalFlags::nothing,
53 EvalFlags _src_flag = EvalFlags::nothing,
54 const std::vector<EvalFlags> &_old_flags = {})
55 : flag(_flag)
56 , src_flag(_src_flag)
57 , old_flags(_old_flags)
58 {}
59
64 operator|(const Dependency &other) const
65 {
66 Dependency result(flag | other.flag, src_flag | other.src_flag);
67 for (unsigned int i = 0; i < std::max(old_flags.size(), other.old_flags.size()); ++i)
68 {
69 result.old_flags.push_back(
70 (i < old_flags.size() ? old_flags.at(i) : EvalFlags::nothing) |
71 (i < other.old_flags.size() ? other.old_flags.at(i) : EvalFlags::nothing));
72 }
73 return result;
74 }
75
80 operator&(const Dependency &other) const
81 {
82 Dependency result(flag & other.flag, src_flag & other.src_flag);
83 for (unsigned int i = 0; i < std::min(old_flags.size(), other.old_flags.size()); ++i)
84 {
85 result.old_flags.push_back(old_flags.at(i) & (other.old_flags.at(i)));
86 }
87 return result;
88 }
89
94 operator|=(const Dependency &other)
95 {
96 flag = flag | other.flag;
97 src_flag = src_flag | other.src_flag;
98 if (other.old_flags.size() > old_flags.size())
99 {
100 old_flags.resize(other.old_flags.size());
101 }
102 for (unsigned int i = 0; i < old_flags.size(); ++i)
103 {
104 old_flags.at(i) = old_flags.at(i) | other.old_flags.at(i);
105 }
106 return *this;
107 }
108
112 Dependency &
113 operator&=(const Dependency &other)
114 {
115 flag = flag & other.flag;
116 src_flag = src_flag & other.src_flag;
117 old_flags.resize(std::min(old_flags.size(), other.old_flags.size()));
118 for (unsigned int i = 0; i < old_flags.size(); ++i)
119 {
120 old_flags.at(i) = old_flags.at(i) & other.old_flags.at(i);
121 }
122
123 return *this;
124 }
125};
126
127// NOLINTEND(misc-non-private-member-variables-in-classes, hicpp-explicit-conversions)
128
129using DependencyMap = std::map<Types::Index, Dependency>;
130
131constexpr unsigned int default_max_checked_age = 6;
132
133inline DependencyMap
134make_dependency_set(const std::vector<FieldAttributes> &field_attributes,
135 std::set<std::string> dependency_strings,
136 unsigned int max_checked_age = default_max_checked_age)
137{
138 static const std::set<std::pair<std::pair<std::string, std::string>, EvalFlags>>
139 reg_delimiters = {
140 {{"", ""}, EvalFlags::values },
141 {{"grad(", ")"}, EvalFlags::gradients},
142 {{"hess(", ")"}, EvalFlags::hessians },
143 {{"hessdiag(", ")"}, EvalFlags::hessians },
144 {{"lap(", ")"}, EvalFlags::hessians },
145 {{"div(", ")"}, EvalFlags::gradients},
146 {{"symgrad(", ")"}, EvalFlags::gradients},
147 {{"curl(", ")"}, EvalFlags::gradients},
148 };
149
150 static const std::set<std::pair<std::string, std::string>> src_delimiters = {
151 {"src(", ")"},
152 {"change(", ")"},
153 {"trial(", ")"},
154 {"lhs(", ")"}
155 };
156
157 DependencyMap result;
158 for (unsigned int i = 0; i < field_attributes.size(); ++i)
159 {
160 const auto &attr = field_attributes[i];
161 for (const auto &[delimiter, flag] : reg_delimiters)
162 {
163 std::string potential_match = delimiter.first + attr.name + delimiter.second;
164 auto iter = dependency_strings.find(potential_match);
165 if (iter != dependency_strings.end())
166 {
167 result[i].flag |= flag;
168 dependency_strings.erase(iter);
169 }
170 for (const auto &[src1, src2] : src_delimiters)
171 {
172 potential_match =
173 delimiter.first + src1 + attr.name + src2 + delimiter.second;
174 iter = dependency_strings.find(potential_match);
175 if (iter != dependency_strings.end())
176 {
177 result[i].src_flag |= flag;
178 dependency_strings.erase(iter);
179 }
180 }
181 for (unsigned int old_index = 0; old_index < max_checked_age; ++old_index)
182 {
183 potential_match = delimiter.first + "old_" + std::to_string(old_index + 1) +
184 "(" + attr.name + ")" + delimiter.second;
185 iter = dependency_strings.find(potential_match);
186 if (iter != dependency_strings.end())
187 {
188 if (result[i].old_flags.size() < old_index + 1)
189 {
190 result[i].old_flags.resize(old_index + 1, EvalFlags::nothing);
191 }
192 result[i].old_flags.at(old_index) |= flag;
193 dependency_strings.erase(iter);
194 }
195 }
196 }
197 }
198 if (!dependency_strings.empty())
199 {
200 std::string error_message = "The following dependencies were not recognized: ";
201 for (const auto &dep : dependency_strings)
202 {
203 error_message += dep + ", ";
204 }
205 error_message.pop_back();
206 error_message.pop_back();
207#ifndef DEBUG
208 ConditionalOStreams::pout_base() << "Warning: " << error_message << std::endl;
209#endif
210 Assert(false, dealii::ExcMessage(error_message));
211 }
212 return result;
213}
214
215PRISMS_PF_END_NAMESPACE
static dealii::ConditionalOStream & pout_base()
Generic parallel output stream. Used for essential information in release and debug mode.
Definition conditional_ostreams.cc:43
DependencyMap make_dependency_set(const std::vector< FieldAttributes > &field_attributes, std::set< std::string > dependency_strings, unsigned int max_checked_age=default_max_checked_age)
Definition dependencies.h:134
std::map< Types::Index, Dependency > DependencyMap
Definition dependencies.h:129
constexpr unsigned int default_max_checked_age
Definition dependencies.h:131
Definition conditional_ostreams.cc:20
Dependency struct containing evaluation flags for each field.
Definition dependencies.h:30
Dependency operator|(const Dependency &other) const
Bitwise or construction operator.
Definition dependencies.h:64
Dependency & operator&=(const Dependency &other)
Bitwise and assignment operator.
Definition dependencies.h:113
EvalFlags src_flag
Evaluation flags for the DependencyType::SRC solution.
Definition dependencies.h:42
dealii::EvaluationFlags::EvaluationFlags EvalFlags
Definition dependencies.h:31
std::vector< EvalFlags > old_flags
Collection of evaluation flags for the old solutions.
Definition dependencies.h:47
Dependency operator&(const Dependency &other) const
Bitwise and construction operator.
Definition dependencies.h:80
Dependency & operator|=(const Dependency &other)
Bitwise or assignment operator.
Definition dependencies.h:94
EvalFlags flag
Evaluation flags for the current solution.
Definition dependencies.h:36
Dependency(EvalFlags _flag=EvalFlags::nothing, EvalFlags _src_flag=EvalFlags::nothing, const std::vector< EvalFlags > &_old_flags={})
Construct with given flags.
Definition dependencies.h:52
dealii::EvaluationFlags::EvaluationFlags EvalFlags
Definition types.h:86