MAIA bb96820c
Multiphysics at AIA
Loading...
Searching...
No Matches
couplerlblb.cpp
Go to the documentation of this file.
1// Copyright (C) 2024 The m-AIA AUTHORS
2//
3// This file is part of m-AIA (https://git.rwth-aachen.de/aia/m-AIA/m-AIA)
4//
5// SPDX-License-Identifier: LGPL-3.0-only
6
7#include "couplerlblb.h"
8#include "coupling.h"
9
10template <MInt nDim, MInt nDist, class SysEqn>
11CouplerLbLb<nDim, nDist, SysEqn>::CouplerLbLb(const MInt couplingId, std::vector<LbSolver*> solvers)
12 : Coupling(couplingId), CouplingLB<nDim, nDist, SysEqn>(couplingId, solvers) {
13 initData();
15}
16
17template <MInt nDim, MInt nDist, class SysEqn>
19 mDeallocate(m_sourceBaseAddresses);
20 mDeallocate(m_destBaseAddresses);
21 mDeallocate(m_dataBlockSizes);
22}
23
24template <MInt nDim, MInt nDist, class SysEqn>
26 m_sourceSolverId = sourceSolver().solverId();
27 m_destSolverId = destSolver().solverId();
28}
29
30template <MInt nDim, MInt nDist, class SysEqn>
32 // check if solvers are compatible
33 if(sourceSolver().m_updateMacroscopicLocation != destSolver().m_updateMacroscopicLocation) {
34 mTerm(1, AT_, "updateAfterPropagation has to be consistent between solvers");
35 }
36 if(sourceSolver().m_isThermal != destSolver().m_isThermal) {
37 mTerm(1, AT_, "isThermal has to be consistent between solvers");
38 }
39
40 // make a list of cells that are located within the transfer box. Does not work with adaptation!
41 for(MInt sourceId = 0; sourceId < sourceSolver().a_noCells(); sourceId++) {
42 if(source2DestId(sourceId) < 0) continue;
43 MBool isInside = true;
44 for(MInt i = 0; i < nDim; i++) {
45 MFloat coordinate = sourceSolver().a_coordinate(sourceId, i);
46 if(coordinate < m_transferBox[i] || coordinate > m_transferBox[i + nDim]) {
47 isInside = false;
48 break;
49 }
50 }
51 if(isInside) {
52 m_transferCellIds.push_back(sourceId);
53 }
54 }
55
56 if(sourceSolver().m_updateMacroscopicLocation == POSTPROPAGATION) {
57 if(sourceSolver().m_isThermal) {
58 m_noVarsTransfer = 4;
59 mAlloc(m_dataBlockSizes, m_noVarsTransfer, "m_dataBlockSizes", 0, AT_);
60 mAlloc(m_sourceBaseAddresses, m_noVarsTransfer, "m_sourceBaseAddresses", AT_);
61 mAlloc(m_destBaseAddresses, m_noVarsTransfer, "m_destBaseAddresses", AT_);
62
63 m_dataBlockSizes[0] = nDim + 1;
64 m_dataBlockSizes[1] = nDim + 1;
65 m_dataBlockSizes[2] = nDist;
66 m_dataBlockSizes[3] = nDist;
67
68 m_sourceBaseAddresses[0] = &sourceSolver().a_variable(0, 0);
69 m_sourceBaseAddresses[1] = &sourceSolver().a_oldVariable(0, 0);
70 m_sourceBaseAddresses[2] = &sourceSolver().a_oldDistribution(0, 0);
71 m_sourceBaseAddresses[3] = &sourceSolver().a_oldDistributionThermal(0, 0);
72 m_destBaseAddresses[0] = &destSolver().a_variable(0, 0);
73 m_destBaseAddresses[1] = &destSolver().a_oldVariable(0, 0);
74 m_destBaseAddresses[2] = &destSolver().a_oldDistribution(0, 0);
75 m_destBaseAddresses[3] = &destSolver().a_oldDistributionThermal(0, 0);
76 } else {
77 m_noVarsTransfer = 3;
78 mAlloc(m_dataBlockSizes, m_noVarsTransfer, "m_noDataBlockSizes", 0, AT_);
79 mAlloc(m_sourceBaseAddresses, m_noVarsTransfer, "m_sourceBaseAddresses", AT_);
80 mAlloc(m_destBaseAddresses, m_noVarsTransfer, "m_destBaseAddresses", AT_);
81 m_dataBlockSizes[0] = nDim + 1;
82 m_dataBlockSizes[1] = nDim + 1;
83 m_dataBlockSizes[2] = nDist;
84
85 m_sourceBaseAddresses[0] = &sourceSolver().a_variable(0, 0);
86 m_sourceBaseAddresses[1] = &sourceSolver().a_oldVariable(0, 0);
87 m_sourceBaseAddresses[2] = &sourceSolver().a_oldDistribution(0, 0);
88 m_destBaseAddresses[0] = &destSolver().a_variable(0, 0);
89 m_destBaseAddresses[1] = &destSolver().a_oldVariable(0, 0);
90 m_destBaseAddresses[2] = &destSolver().a_oldDistribution(0, 0);
91 }
92 } else {
93 if(sourceSolver().m_isThermal) {
94 m_noVarsTransfer = 3;
95 mAlloc(m_dataBlockSizes, m_noVarsTransfer, "m_noDataBlockSizes", 0, AT_);
96 mAlloc(m_sourceBaseAddresses, m_noVarsTransfer, "m_sourceBaseAddresses", AT_);
97 mAlloc(m_destBaseAddresses, m_noVarsTransfer, "m_destBaseAddresses", AT_);
98
99 m_dataBlockSizes[0] = nDim + 1;
100 m_dataBlockSizes[1] = nDist;
101 m_dataBlockSizes[2] = nDist;
102
103 m_sourceBaseAddresses[0] = &sourceSolver().a_variable(0, 0);
104 m_sourceBaseAddresses[1] = &sourceSolver().a_oldDistribution(0, 0);
105 m_sourceBaseAddresses[2] = &sourceSolver().a_oldDistributionThermal(0, 0);
106 m_destBaseAddresses[0] = &destSolver().a_variable(0, 0);
107 m_destBaseAddresses[1] = &destSolver().a_oldDistribution(0, 0);
108 m_destBaseAddresses[2] = &destSolver().a_oldDistributionThermal(0, 0);
109 } else {
110 m_noVarsTransfer = 2;
111 mAlloc(m_dataBlockSizes, m_noVarsTransfer, "m_noDataBlockSizes", 0, AT_);
112 mAlloc(m_sourceBaseAddresses, m_noVarsTransfer, "m_sourceBaseAddresses", AT_);
113 mAlloc(m_destBaseAddresses, m_noVarsTransfer, "m_destBaseAddresses", AT_);
114
115 m_dataBlockSizes[0] = nDim + 1;
116 m_dataBlockSizes[1] = nDist;
117
118 m_sourceBaseAddresses[0] = &sourceSolver().a_variable(0, 0);
119 m_sourceBaseAddresses[1] = &sourceSolver().a_oldDistribution(0, 0);
120 m_destBaseAddresses[0] = &destSolver().a_variable(0, 0);
121 m_destBaseAddresses[1] = &destSolver().a_oldDistribution(0, 0);
122 }
123 }
124
125#ifndef NDEBUG
126 // debug checks
127 for(const auto& sourceId : m_transferCellIds) {
128 const MInt destId = source2DestId(sourceId);
129 MBool isCorrect = true;
130 for(MInt i = 0; i < nDim; i++) {
131 if(fabs(sourceSolver().a_coordinate(sourceId, i) - destSolver().a_coordinate(destId, i)) > 1e-10) {
132 isCorrect = false;
133 break;
134 }
135 if(!isCorrect) {
136 std::cerr << "source:" << sourceId << " " << sourceSolver().a_coordinate(sourceId, 0) << " "
137 << sourceSolver().a_coordinate(sourceId, 1) << " " << sourceSolver().a_coordinate(sourceId, 2) << " "
138 << "dest:" << destId << " " << destSolver().a_coordinate(destId, 0) << " "
139 << destSolver().a_coordinate(destId, 1) << " " << destSolver().a_coordinate(destId, 2) << std::endl;
140 }
141 }
142 }
143 for(MInt destId = 0; destId < destSolver().a_noCells(); destId++) {
144 MBool isInside = true;
145 for(MInt i = 0; i < nDim; i++) {
146 MFloat coordinate = destSolver().a_coordinate(destId, i);
147 if(coordinate < m_transferBox[i] || coordinate > m_transferBox[i + nDim]) {
148 isInside = false;
149 break;
150 }
151 }
152 if(isInside) {
153 const MInt sourceId = dest2SourceId(destId);
154 if(std::find(m_transferCellIds.begin(), m_transferCellIds.end(), sourceId) == m_transferCellIds.end()) {
155 std::cerr << "missed cell dest:" << destId << " " << destSolver().a_coordinate(destId, 0) << " "
156 << destSolver().a_coordinate(destId, 1) << " " << destSolver().a_coordinate(destId, 2) << " "
157 << "source:" << sourceId << " " << sourceSolver().a_coordinate(sourceId, 0) << " "
158 << sourceSolver().a_coordinate(sourceId, 1) << " " << sourceSolver().a_coordinate(sourceId, 2)
159 << std::endl;
160 }
161 }
162 }
163#endif
164}
165
166template <MInt nDim, MInt nDist, class SysEqn>
168 if(Context::propertyExists("lbBox", 0)) {
169 for(MInt i = 0; i < nDim * 2; i++) {
176 m_transferBox[i] = Context::getSolverProperty<MFloat>("lbBox", 0, AT_, i);
177 }
178 } else {
179 mTerm(1, AT_, "Property lbBox required for coupler LBLb!");
180 }
181}
182
183template <MInt nDim, MInt nDist, class SysEqn>
185 for(const auto& sourceId : m_transferCellIds) {
186 const MInt destId = source2DestId(sourceId);
187 for(MInt var = 0; var < m_noVarsTransfer; var++) {
188 memcpy(m_destBaseAddresses[var] + (m_dataBlockSizes[var] * destId),
189 m_sourceBaseAddresses[var] + (m_dataBlockSizes[var] * sourceId),
190 m_dataBlockSizes[var] * sizeof(MFloat));
191 }
192 }
193}
194
void mAlloc(T *&a, const MLong N, const MString &objectName, MString function)
allocates memory for one-dimensional array 'a' of size N
Definition: alloc.h:173
MBool mDeallocate(T *&a)
deallocates the memory previously allocated for element 'a'
Definition: alloc.h:544
static MBool propertyExists(const MString &name, MInt solver=m_noSolvers)
This function checks if a property exists in general.
Definition: context.cpp:494
virtual void init()
Definition: couplerlblb.cpp:31
CouplerLbLb(const MInt couplingId, std::vector< LbSolver * > solvers)
Definition: couplerlblb.cpp:11
void initData()
Definition: couplerlblb.cpp:25
virtual void readProperties()
virtual void postCouple(MInt)
void mTerm(const MInt errorCode, const MString &location, const MString &message)
Definition: functions.cpp:29
@ POSTPROPAGATION
Definition: lbsolver.h:34
int32_t MInt
Definition: maiatypes.h:62
double MFloat
Definition: maiatypes.h:52
MInt * solvers
Definition: maiatypes.h:72
bool MBool
Definition: maiatypes.h:58