chombo-discharge
Loading...
Searching...
No Matches
CD_ItoKMCTaggerImplem.H
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2021-2026 SINTEF Energy Research
3 *
4 * SPDX-License-Identifier: GPL-3.0-or-later
5 */
6
13#ifndef CD_ITOKMCTAGGERIMPLEM_H
14#define CD_ITOKMCTAGGERIMPLEM_H
15
16// Chombo includes
17#include <ParmParse.H>
18
19// Our includes
20#include <CD_ItoKMCTagger.H>
21#include <CD_DataOps.H>
22#include <CD_NamespaceHeader.H>
23
24using namespace Physics::ItoKMC;
25
26template <typename S>
28{
29 CH_TIME("ItoKMCTagger::ItoKMCTagger");
30 m_verbosity = -1;
31 if (m_verbosity > 5) {
32 pout() << "ItoKMCTagger::ItoKMCTagger" << endl;
33 }
34
35 m_name = "ItoKMCTagger";
38 m_isDefined = false;
39 m_plotData = false;
40}
41
42template <typename S>
50
51template <typename S>
54
55template <typename S>
56void
59 const RefCountedPtr<AmrMesh>& a_amr) noexcept
60
61{
62 CH_TIME("ItoKMCTagger::define");
63 if (m_verbosity > 5) {
64 pout() << m_name + "::define" << endl;
65 }
66
67 m_physics = a_physics;
68 m_timeStepper = a_timeStepper;
69 m_amr = a_amr;
70 m_isDefined = true;
71}
72
73template <typename S>
74void
76{
77 CH_TIME("ItoKMCTagger::regrid");
78 if (m_verbosity > 5) {
79 pout() << m_name + "::regrid" << endl;
80 }
81
82 CH_assert(m_isDefined);
83
84 if (m_numTagFields > 0) {
85
86 m_tagFields.resize(m_numTagFields);
87 m_gradTagFields.resize(m_numTagFields);
88
89 for (int i = 0; i < m_numTagFields; i++) {
90 m_amr->allocate(m_tagFields[i], m_realm, m_phase, 1);
91 m_amr->allocate(m_gradTagFields[i], m_realm, m_phase, SpaceDim);
92 }
93 }
94}
95
96template <typename S>
97void
99{
100 CH_TIME("ItoKMCTagger::setPhase");
101 if (m_verbosity > 5) {
102 pout() << m_name + "::setPhase" << endl;
103 }
104
105 m_phase = a_phase;
106}
107
108template <typename S>
109int
111{
112 CH_TIME("ItoKMCTagger::getNumberOfPlotVariables");
113 if (m_verbosity > 5) {
114 pout() << m_name + "::getNumberOfPlotVariables" << endl;
115 }
116
117 return m_plotData ? m_numTagFields : 0;
118}
119
120template <typename S>
123{
124 CH_TIME("ItoKMCTagger::getPlotVariableNames");
125 if (m_verbosity > 5) {
126 pout() << m_name + "::getPlotVariableNames" << endl;
127 }
128
130
131 if (m_plotData) {
132 for (int i = 0; i < m_numTagFields; i++) {
133 plotVars.push_back("Tag field-" + std::to_string(i));
134 }
135 }
136
137 return plotVars;
138}
139
140template <typename S>
143{
144 CH_TIME("ItoKMCTagger::getTagFields");
145 if (m_verbosity > 5) {
146 pout() << m_name + "::getTagFields" << endl;
147 }
148
149 CH_assert(m_isDefined);
150
151 return m_tagFields;
152}
153
154template <typename S>
155void
157 int& a_icomp,
159 const int a_level) const noexcept
160{
161 CH_TIME("ItoKMCTagger::writePlotData");
162 if (m_verbosity > 5) {
163 pout() << m_name + "::writePlotData" << endl;
164 }
165
166 CH_assert(m_isDefined);
167
168 if (m_plotData) {
169 this->computeTagFields();
170
171 for (int i = 0; i < m_numTagFields; i++) {
172 const EBAMRCellData& tagField = m_tagFields[i];
173
174 const Interval srcInterv(0, 0);
176
177 m_amr->copyData(a_output, *tagField[a_level], a_level, a_outputRealm, tagField.getRealm(), dstInterv, srcInterv);
178
179 DataOps::setCoveredValue(a_output, *m_amr->getCoveredCells(a_outputRealm, m_phase)[a_level], a_icomp, 0.0);
180
181 a_icomp++;
182 }
183 }
184}
185
186template <typename S>
187bool
189{
190 CH_TIME("ItoKMCTagger::tagCells");
191 if (m_verbosity > 5) {
192 pout() << m_name + "::tagCells" << endl;
193 }
194
195 CH_assert(m_isDefined);
196
197 int gotNewTags = 0;
198
199 const RealVect probLo = m_amr->getProbLo();
200 const Real curTime = m_timeStepper->getTime();
201 const int finestLevel = m_amr->getFinestLevel();
202 const int maxAmrDepth = m_amr->getMaxAmrDepth();
204
205 if (m_numTagFields > 0) {
206 this->computeTagFields();
207
208 for (int lvl = 0; lvl <= finestTagLevel; lvl++) {
209 const DisjointBoxLayout& dbl = m_amr->getGrids(m_realm)[lvl];
210 const DataIterator& dit = dbl.dataIterator();
211 const EBISLayout& ebisl = m_amr->getEBISLayout(m_realm, m_phase)[lvl];
212 const Real dx = m_amr->getDx()[lvl];
213
214 const int nbox = dit.size();
215
216#pragma omp parallel for schedule(runtime) reduction(max : gotNewTags)
217 for (int mybox = 0; mybox < nbox; mybox++) {
218 const DataIndex& din = dit[mybox];
219
220 const Box box = dbl.get(din);
221 const EBISBox& ebisbox = ebisl[din];
222
223 const IntVectSet irregCells = ebisbox.getIrregIVS(box);
225
226 DenseIntVectSet coarsenCells(box, false);
227 DenseIntVectSet refinedCells(box, false);
228
231
232 for (int i = 0; i < m_numTagFields; i++) {
233 tagFields.push_back(&((*m_tagFields[i][lvl])[din]));
234 gradTagFields.push_back(&((*m_gradTagFields[i][lvl])[din]));
235 }
236
237 this->tagCellsBox(refinedCells,
239 tagFields,
241 lvl,
242 din,
243 box,
244 ebisbox,
245 curTime,
246 dx,
247 probLo);
248
249 // Check if we got any new tags, or we are just recycling old tags.
250 // Basically we will check if (current_tags + refined_tags - coarsenCells) == current_tags
252
257
258 cpy2 -= cpy1; // = new tags minus old tags. If nonzero, we got some new tags.
259 cpy1 -= tags; // = old_tags minus new tags. If nonzero, we got some new tags
260 if (cpy1.numPts() != 0 || cpy2.numPts() != 0) {
261 gotNewTags = 1;
262 }
263 }
264 }
265 }
266
267 return (ParallelOps::max(gotNewTags) > 0);
268}
269
270template <typename S>
271void
276 const int a_lvl,
277 const DataIndex a_dit,
278 const Box a_box,
279 const EBISBox& a_ebisbox,
280 const Real a_time,
281 const Real a_dx,
282 const RealVect a_probLo) const noexcept
283{
284 CH_TIME("ItoKMCTagger::refineCellsBox");
285 if (m_verbosity > 5) {
286 pout() << "ItoKMCTagger::refineCellsBox" << endl;
287 }
288
289 CH_assert(m_isDefined);
290
293
294 for (int i = 0; i < m_numTagFields; i++) {
295 tagFieldsReg.push_back(&(a_tagFields[i]->getFArrayBox()));
297 }
298
299 // Regular kernel.
300 auto regularKernel = [&](const IntVect& iv) -> void {
301 const RealVect pos = a_probLo + (0.5 * RealVect::Unit + RealVect(iv)) * a_dx;
302
303 if (this->insideTagBox(pos) && a_ebisbox.isRegular(iv)) {
304
305 Vector<Real> tr(m_numTagFields);
306 Vector<RealVect> gt(m_numTagFields);
307
308 for (int i = 0; i < m_numTagFields; i++) {
309 tr[i] = (*tagFieldsReg[i])(iv, 0);
310 gt[i] = RealVect(
311 D_DECL((*gradTagFieldsReg[i])(iv, 0), (*gradTagFieldsReg[i])(iv, 1), (*gradTagFieldsReg[i])(iv, 2)));
312 }
313
314 if (this->refineCell(pos, a_time, a_dx, a_lvl, tr, gt)) {
316 }
317
318 if (this->coarsenCell(pos, a_time, a_dx, a_lvl, tr, gt)) {
320 }
321 }
322 };
323
324 // Irregular box loop
325 auto irregularKernel = [&](const VolIndex& vof) -> void {
326 const RealVect pos = a_probLo + Location::position(Location::Cell::Center, vof, a_ebisbox, a_dx);
327
328 if (this->insideTagBox(pos)) {
329
330 Vector<Real> tr(m_numTagFields);
331 Vector<RealVect> gt(m_numTagFields);
332
333 for (int i = 0; i < m_numTagFields; i++) {
334 tr[i] = (*a_tagFields[i])(vof, 0);
335 gt[i] = RealVect(
336 D_DECL((*a_gradTagFields[i])(vof, 0), (*a_gradTagFields[i])(vof, 1), (*a_gradTagFields[i])(vof, 2)));
337 }
338
339 if (this->refineCell(pos, a_time, a_dx, a_lvl, tr, gt)) {
340 a_refinedCells |= vof.gridIndex();
341 }
342
343 if (this->coarsenCell(pos, a_time, a_dx, a_lvl, tr, gt)) {
344 a_coarsenedCells |= vof.gridIndex();
345 }
346 }
347 };
348
349 // Run the kernels. Not vectorizable: the regular kernel calls the virtual refineCell/coarsenCell per cell
350 // + DenseIntVectSet insertion + control flow. One-time tagging (per regrid).
351 VoFIterator& vofit = (*m_amr->getVofIterator(m_realm, m_phase)[a_lvl])[a_dit];
352
353 BoxLoops::loop<D_DECL(1, 1, 1)>(a_box, regularKernel);
355}
356
357#include <CD_NamespaceFooter.H>
358
359#endif
Agglomeration of useful data operations.
Vector< RefCountedPtr< LayoutData< DenseIntVectSet > > > EBAMRTags
Declaration of cell tags.
Definition CD_EBAMRTags.H:24
Declaration of the Physics::ItoKMC::ItoKMCTagger abstract CellTagger.
static void setCoveredValue(EBAMRCellData &a_lhs, const EBAMRCellData &a_coveredMask, const int a_comp, const Real a_value)
Set value in covered cells. Does specified component.
Definition CD_DataOps.cpp:2655
ItoKMCTagger()
Weak constructor. User MUST subsequently call the define function.
Definition CD_ItoKMCTaggerImplem.H:27
virtual void setPhase(const phase::which_phase a_phase) noexcept
Set the phase where we do the tagging.
Definition CD_ItoKMCTaggerImplem.H:98
virtual bool tagCells(EBAMRTags &a_tags) noexcept override
Tag cells for refinement and coarsening.
Definition CD_ItoKMCTaggerImplem.H:188
virtual Vector< EBAMRCellData > & getTagFields() noexcept
Get the tagging fields (mutable reference).
Definition CD_ItoKMCTaggerImplem.H:142
virtual ~ItoKMCTagger() noexcept
Destructor.
Definition CD_ItoKMCTaggerImplem.H:52
virtual void tagCellsBox(DenseIntVectSet &a_refinedCells, DenseIntVectSet &a_coarsenedCells, const Vector< EBCellFAB * > &a_tagFields, const Vector< EBCellFAB * > &a_gradTagFields, const int a_lvl, const DataIndex a_dit, const Box a_box, const EBISBox &a_ebisbox, const Real a_time, const Real a_dx, const RealVect a_probLo) const noexcept
Per-box refinement tagging routine.
Definition CD_ItoKMCTaggerImplem.H:272
virtual void writePlotData(LevelData< EBCellFAB > &a_output, int &a_icomp, const std::string &a_outputRealm, const int a_level) const noexcept override
Write plot data.
Definition CD_ItoKMCTaggerImplem.H:156
virtual int getNumberOfPlotVariables() const noexcept override
Get number of plot variables that will be written to file (by Driver).
Definition CD_ItoKMCTaggerImplem.H:110
virtual void define(const RefCountedPtr< ItoKMCPhysics > &a_physics, const RefCountedPtr< S > &a_timeStepper, const RefCountedPtr< AmrMesh > &a_amr) noexcept
Define function.
Definition CD_ItoKMCTaggerImplem.H:57
virtual Vector< std::string > getPlotVariableNames() const noexcept override
Get plot variable names.
Definition CD_ItoKMCTaggerImplem.H:122
virtual void regrid() noexcept override
Regrid this class. Note that there is no preRegrid method.
Definition CD_ItoKMCTaggerImplem.H:75
static const std::string Primal
Identifier for perimal realm.
Definition CD_Realm.H:39
Base class for a tracer particle solver. This solver can advance particles in a pre-defined velocity ...
Definition CD_TracerParticleSolver.H:38
phase::which_phase m_phase
Phase where this solver lives.
Definition CD_TracerParticleSolver.H:367
TracerParticleSolver()
Default constructor.
Definition CD_TracerParticleSolverImplem.H:26
std::string m_realm
Realm where this solver lives.
Definition CD_TracerParticleSolver.H:352
int m_verbosity
Verbosity level.
Definition CD_TracerParticleSolver.H:387
RefCountedPtr< AmrMesh > m_amr
Handle to AMR mesh.
Definition CD_TracerParticleSolver.H:327
virtual void allocate()
Allocate storage for this solver.
Definition CD_TracerParticleSolverImplem.H:195
std::string m_name
Solver name.
Definition CD_TracerParticleSolver.H:357
ALWAYS_INLINE void loop(const Box &a_computeBox, Functor &&kernel)
Launch a C++ kernel over a regular grid with compile-time per-dimension strides.
Definition CD_BoxLoopsImplem.H:39
RealVect position(Location::Cell a_location, const VolIndex &a_vof, const EBISBox &a_ebisbox, const Real &a_dx)
Compute the position (ignoring the "origin) of a Vof.
Definition CD_LocationImplem.H:21
Real max(const Real &a_input) noexcept
Get the maximum of the input, reduced over MPI ranks (in the Chombo communicator)
Definition CD_ParallelOpsImplem.H:177
which_phase
Enumeration of supported phases.
Definition CD_MultiFluidIndexSpace.H:38
@ gas
Gas phase.
Definition CD_MultiFluidIndexSpace.H:39