MAIA bb96820c
Multiphysics at AIA
Loading...
Searching...
No Matches
sbpcartesianmortar.h
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#ifndef SBPMORTAR_H_
8#define SBPMORTAR_H_
9
10#include "INCLUDE/maiatypes.h"
11#include "UTIL/tensor.h"
13
14#include <string>
15
16namespace maia {
17namespace sbp {
18namespace mortar {
19
20
21const MBool forward = true;
22const MBool reverse = false;
23const MInt lower = 0;
24const MInt upper = 1;
25
37inline MBool readCSV(std::string path, MFloat* mat) {
38 std::vector<std::vector<MFloat>> data;
39 std::ifstream stream(path);
40
41 if(stream.fail()) {
42 std::cerr << "File doesn't exist: " << path << "\n";
43 return false;
44 }
45
46 MInt l = 0;
47 while(stream) {
48 l++;
49 std::string s;
50 if(!getline(stream, s)) break;
51 if(s[0] != '#') {
52 std::istringstream ss(s);
53 std::vector<MFloat> record;
54
55 while(ss) {
56 std::string line;
57 if(!getline(ss, line, ',')) break;
58 record.push_back(std::stof(line));
59 }
60 data.push_back(record);
61 }
62 }
63
64 if(!stream.eof()) {
65 std::cerr << "Could not read file " << path << "\n";
66 return false;
67 }
68
69 const MInt m = data.size();
70 const MInt n = data[0].size();
71
72 MFloatMatrix matrix(mat, m, n);
73
74 for(MInt i = 0; i < m; i++) {
75 for(MInt j = 0; j < n; j++) {
76 matrix(i, j) = data[i][j];
77 }
78 }
79
80 return true;
81}
82
93inline void readProjectionCoeffs(std::string path, MFloatMatrix& projBn, MFloatVector& projIn) {
94 projIn = MFloatVector(2);
95 projIn.set(0.2);
96
97 std::string bnFileName = path + "bn.csv";
98 std::string inFileName = path + "in.csv";
99
100 std::vector<std::vector<MFloat>> bnData;
101 std::ifstream bnFile(bnFileName);
102 MInt l = 0;
103
104 while(bnFile) {
105 l++;
106 std::string s;
107 if(!getline(bnFile, s)) break;
108 if(s[0] != '#') {
109 std::istringstream ss(s);
110 std::vector<MFloat> record;
111
112 while(ss) {
113 std::string line;
114 if(!getline(ss, line, ',')) break;
115 record.push_back(std::stof(line));
116 }
117 bnData.push_back(record);
118 }
119 }
120
121 if(!bnFile.eof()) {
122 std::cerr << "Could not read file " << bnFileName << "\n";
123 std::__throw_invalid_argument("File not found.");
124 }
125
126 std::vector<std::vector<MFloat>> inData;
127 std::ifstream inFile(inFileName);
128 l = 0;
129
130 while(inFile) {
131 l++;
132 std::string s;
133 if(!getline(inFile, s)) break;
134 if(s[0] != '#') {
135 std::istringstream ss(s);
136 std::vector<MFloat> record;
137
138 while(ss) {
139 std::string line;
140 if(!getline(ss, line, ',')) break;
141 record.push_back(std::stof(line));
142 }
143 inData.push_back(record);
144 }
145 }
146
147 if(!inFile.eof()) {
148 std::cerr << "Could not read file " << inFileName << "\n";
149 std::__throw_invalid_argument("File not found.");
150 }
151
152 const MInt q = bnData.size();
153 const MInt r = bnData[0].size();
154
155 projBn = MFloatMatrix(q, r);
156
157 for(MInt i = 0; i < q; i++) {
158 for(MInt j = 0; j < r; j++) {
159 projBn(i, j) = bnData[i][j];
160 }
161 }
162
163 const MInt numInternal = inData.size();
164
165 projIn = MFloatVector(numInternal);
166
167 for(MInt i = 0; i < numInternal; i++) {
168 projIn(i) = inData[i][0];
169 }
170}
171
185inline void calcMortarProjectionMatrixPSBP(const MString sourceOp,
186 const MString targetOp,
187 const MInt sourceNoNodes,
188 const MInt targetNoNodes,
189 MFloat* const f,
190 MFloat* const b) {
191 std::string dir = "./operatorsSBP/projection/p/";
192 std::string name = sourceOp.substr(sourceOp.find('/') + 1) + targetOp.substr(targetOp.find('/') + 1);
193
194 std::string pathF = dir + name + "/" + std::to_string(sourceNoNodes) + "_" + std::to_string(targetNoNodes) + "_f.csv";
195 std::string pathB = dir + name + "/" + std::to_string(sourceNoNodes) + "_" + std::to_string(targetNoNodes) + "_b.csv";
196
197 auto fRead = readCSV(pathF, f);
198 auto bRead = readCSV(pathB, b);
199
200 if(!fRead || !bRead) {
201 std::cerr << "P projection matrix for " << sourceOp << " with " << sourceNoNodes << " nodes to " << targetOp
202 << " with " << targetNoNodes << " nodes NOT CALCULATED" << std::endl;
203 MFloatMatrix fMatrix(f, targetNoNodes, sourceNoNodes);
204 MFloatMatrix bMatrix(b, sourceNoNodes, targetNoNodes);
205 fMatrix.set(0);
206 bMatrix.set(0);
207 } else {
208 MFloatMatrix fMatrix(f, targetNoNodes, sourceNoNodes);
209 MFloatMatrix bMatrix(b, sourceNoNodes, targetNoNodes);
210 }
211}
212
227 const MFloat* const sourceIntWeights,
228 MFloat* const forwardLower,
229 MFloat* const forwardUpper,
230 MFloat* const backwardLower,
231 MFloat* const backwardUpper) {
232 // Create targetIntWeight by stacking up sourceIntWeight
233 // Since wInt is unscaled we don't need to apply any factor here
234 MFloatVector targetIntWeights(2 * noNodes);
235
236 for(MInt i = 0; i < noNodes; i++) {
237 targetIntWeights(i) = sourceIntWeights[i];
238 targetIntWeights(i + noNodes) = sourceIntWeights[i];
239 }
240
241 MFloatMatrix stenBn;
242 MFloatVector coeffsIn;
243 std::string path = "./operatorsSBP/projection/s204/";
244 readProjectionCoeffs(path, stenBn, coeffsIn);
245
246 // TODO labels:DG s = #coeffs, carpenter s + 1 = #coeffs
247 const MInt s = coeffsIn.dim0();
248 const MInt q = stenBn.dim0();
249 const MInt r = stenBn.dim1();
250
251 // Create inner stencil
252 // Coeffs: 1 2 3
253 // Stencil 3 2 1 2 3
254 MFloatVector stenIn(2 * s - 1);
255 for(MInt i = 0; i < s; i++) {
256 stenIn(s - 1 + i) = coeffsIn(i);
257 stenIn(s - 1 - i) = coeffsIn(i);
258 }
259
260 MFloatMatrix f(2 * noNodes, noNodes);
261 MFloatMatrix b(noNodes, 2 * noNodes);
262
263 b.set(0.0);
264 f.set(0.0);
265
266 if(r > 2 * noNodes || 2 * q > noNodes || 2 * s - 1 > 2 * noNodes) {
267 return;
268 }
269
270 // Set upper boundary solver
271 for(MInt i = 0; i < q; i++) {
272 for(MInt j = 0; j < r; j++) {
273 b(i, j) = stenBn(i, j);
274 }
275 }
276
277 // Set inner stencil
278 MInt k = 2 * q - s + 1;
279 for(MInt i = q; i < noNodes / 2; i++) {
280 for(MInt j = 0; j < 2 * s - 1; j++) {
281 b(i, j + k) = stenIn(j);
282 }
283 k += 2;
284 }
285
286 // Mirror
287 for(MInt i = 0; i < noNodes; i++) {
288 for(MInt j = 0; j < 2 * noNodes; j++) {
289 b(noNodes - i - 1, 2 * noNodes - j - 1) = b(i, j);
290 }
291 }
292
293 // Create foward operator by applying the H-Compatibility condition eq.
294 // Apply dx scale factor since wInt is unscaled.
295 // For the 1:2 href case it's always 2
296 // TODO labels:DG scale beforehand
297 for(MInt i = 0; i < noNodes; i++) {
298 for(MInt j = 0; j < 2 * noNodes; j++) {
299 f(j, i) = b(i, j) * sourceIntWeights[i] / targetIntWeights(j) * 2;
300 }
301 }
302
303 // Split operators into upper and lower part
304 MFloatMatrix fl(forwardLower, noNodes, noNodes);
305 MFloatMatrix fu(forwardUpper, noNodes, noNodes);
306 MFloatMatrix bl(backwardLower, noNodes, noNodes);
307 MFloatMatrix bu(backwardUpper, noNodes, noNodes);
308
309 for(MInt i = 0; i < noNodes; i++) {
310 for(MInt j = 0; j < noNodes; j++) {
311 fl(i, j) = f(i, j);
312 fu(i, j) = f(i + noNodes, j);
313 bl(i, j) = b(i, j);
314 bu(i, j) = b(i, j + noNodes);
315 }
316 }
317}
318
331inline void calcMortarProjectionMatrixHSBP(const MInt noNodes, const MString sbpOperator, MFloat* const forwardLower,
332 MFloat* const forwardUpper, MFloat* const backwardLower,
333 MFloat* const backwardUpper) {
334 std::string path = "./operatorsSBP/projection/h/";
335
336 std::string pathF = path + sbpOperator + "/" + std::to_string(noNodes) + "_f.csv";
337 std::string pathB = path + sbpOperator + "/" + std::to_string(noNodes) + "_b.csv";
338
339 MFloatMatrix f(2 * noNodes, noNodes);
340 MFloatMatrix b(noNodes, 2 * noNodes);
341
342 auto fRead = readCSV(pathF, &f[0]);
343 auto bRead = readCSV(pathB, &b[0]);
344
345 if(!fRead || !bRead) {
346 std::cerr << "H projection matrix for " << noNodes << " nodes NOT CALCULATED" << std::endl;
347 f.set(0);
348 b.set(0);
349 }
350
351 // Split operators into upper and lower part
352 MFloatMatrix fl(forwardLower, noNodes, noNodes);
353 MFloatMatrix fu(forwardUpper, noNodes, noNodes);
354 MFloatMatrix bl(backwardLower, noNodes, noNodes);
355 MFloatMatrix bu(backwardUpper, noNodes, noNodes);
356
357 for(MInt i = 0; i < noNodes; i++) {
358 for(MInt j = 0; j < noNodes; j++) {
359 fl(i, j) = f(i, j);
360 fu(i, j) = f(i + noNodes, j);
361 bl(i, j) = b(i, j);
362 bu(i, j) = b(i, j + noNodes);
363 }
364 }
365}
366
367} // namespace mortar
368} // namespace sbp
369} // namespace maia
370
371#endif // define DGMORTAR_H_
size_type dim0() const
Return the size of dimension 0.
Definition: tensor.h:366
size_type dim1() const
Return the size of dimension 1.
Definition: tensor.h:380
void set(const T &value)
Initializes tensor to constant value.
Definition: tensor.h:271
size_type size() const
Returns the size of the array as product of all five dimensions (i.e. not the actual array size but t...
Definition: tensor.h:206
int32_t MInt
Definition: maiatypes.h:62
std::basic_string< char > MString
Definition: maiatypes.h:55
double MFloat
Definition: maiatypes.h:52
bool MBool
Definition: maiatypes.h:58
MBool readCSV(std::string path, MFloat *mat)
Reads csv from path to MFloatMatrix.
void calcMortarProjectionMatrixHSBP(const MInt noNodes, const MString sbpOperator, MFloat *const forwardLower, MFloat *const forwardUpper, MFloat *const backwardLower, MFloat *const backwardUpper)
Reads and constructs h-refinement projection operator.
void readProjectionCoeffs(std::string path, MFloatMatrix &projBn, MFloatVector &projIn)
Reads coeffficients from csv files for Carpenter operators.
void calcMortarProjectionMatrixHSBPCarpenter(const MInt noNodes, const MFloat *const sourceIntWeights, MFloat *const forwardLower, MFloat *const forwardUpper, MFloat *const backwardLower, MFloat *const backwardUpper)
Constructs h-refinement projection operator according to Carpenter.
void calcMortarProjectionMatrixPSBP(const MString sourceOp, const MString targetOp, const MInt sourceNoNodes, const MInt targetNoNodes, MFloat *const f, MFloat *const b)
Reads projection coefficients and stores them.
Namespace for auxiliary functions/classes.
maia::tensor::Tensor< MFloat > MFloatMatrix
Definition: tensor.h:1098
maia::tensor::Tensor< MFloat > MFloatVector
Definition: tensor.h:1095