Mimetic Operators Library Enhanced 4.0
Loading...
Searching...
No Matches
AddScalarBC Namespace Reference

Classes

struct  BC1D
 Structure to hold boundary condition data for 1D problems. More...
struct  BC2D
 Structure to hold boundary condition data for 2D problems. More...
struct  BC3D
 Structure to hold boundary condition data for 3D problems. More...

Functions

void addScalarBClhs (u16 k, u32 m, Real dx, const vec &dc, const vec &nc, sp_mat &Al, sp_mat &Ar)
 Compute LHS boundary condition matrices for a 1D problem.
void addScalarBClhs (u16 k, u32 m, Real dx, u32 n, Real dy, const vec &dc, const vec &nc, sp_mat &Al, sp_mat &Ar, sp_mat &Ab, sp_mat &At)
void addScalarBClhs (u16 k, u32 m, Real dx, u32 n, Real dy, u32 o, Real dz, const vec &dc, const vec &nc, sp_mat &Al, sp_mat &Ar, sp_mat &Ab, sp_mat &At, sp_mat &Af, sp_mat &Ak)
void addScalarBCrhs (vec &b, const vec &v, const uvec &indices)
 Apply boundary values to the RHS vector for a 1D problem.
void addScalarBCrhs (vec &b, const vec &dc, const vec &nc, const std::vector< vec > &v, const uvec &rl, const uvec &rr, const uvec &rb, const uvec &rt)
void addScalarBCrhs (vec &b, const vec &dc, const vec &nc, const std::vector< vec > &v, const uvec &rl, const uvec &rr, const uvec &rb, const uvec &rt, const uvec &rf, const uvec &rk)
 Apply boundary values to the RHS vector for a 3D problem.
void addScalarBC (sp_mat &A, vec &b, u16 k, u32 m, Real dx, const BC1D &bc)
 Apply boundary conditions to a 1D discrete operator and RHS.
void addScalarBC (sp_mat &A, vec &b, u16 k, u32 m, Real dx, u32 n, Real dy, const BC2D &bc)
 Apply boundary conditions to a 2D discrete operator and RHS.
void addScalarBC (sp_mat &A, vec &b, u16 k, u32 m, Real dx, u32 n, Real dy, u32 o, Real dz, const BC3D &bc)
 Apply boundary conditions to a 3D discrete operator and RHS.

Detailed Description

file addscalarbc.cpp

brief Implementation of boundary condition application for scalar PDEs

date 2025/01/26 Last Modified: 2026/03/04

Function Documentation

◆ addScalarBC() [1/3]

void AddScalarBC::addScalarBC ( sp_mat & A,
vec & b,
u16 k,
u32 m,
Real dx,
const BC1D & bc )

Apply boundary conditions to a 1D discrete operator and RHS.

Modifies the linear system A*u = b to enforce boundary conditions.

Parameters
ALinear operator (modified in place)
bRight-hand side vector (modified in place)
kOrder of accuracy
mNumber of cells
dxCell spacing
bcBoundary condition data

Definition at line 286 of file addscalarbc.cpp.

286 {
287 assert(bc.dc.n_elem == 2 && "dc must be a 2x1 vector");
288 assert(bc.nc.n_elem == 2 && "nc must be a 2x1 vector");
289 assert(A.n_rows == A.n_cols && "A must be square");
290 assert(A.n_cols == b.n_elem && "b size must equal A columns");
291
292 if (boundaryNorm(bc.dc, bc.nc, 0, 1) == 0.0) return;
293
294 assert(bc.v.n_elem == 2 && "v must be a 2x1 vector");
295
296 uvec indices = {0, (uword)(A.n_rows - 1)};
297 zeroRows(A, indices);
298 b(indices).zeros();
299
300 sp_mat Al, Ar;
301 addScalarBClhs(k, m, dx, bc.dc, bc.nc, Al, Ar);
302 A = A + Al + Ar;
303
304 addScalarBCrhs(b, bc.v, indices);
305}
void addScalarBCrhs(vec &b, const vec &v, const uvec &indices)
Apply boundary values to the RHS vector for a 1D problem.
void addScalarBClhs(u16 k, u32 m, Real dx, const vec &dc, const vec &nc, sp_mat &Al, sp_mat &Ar)
Compute LHS boundary condition matrices for a 1D problem.

◆ addScalarBC() [2/3]

void AddScalarBC::addScalarBC ( sp_mat & A,
vec & b,
u16 k,
u32 m,
Real dx,
u32 n,
Real dy,
const BC2D & bc )

Apply boundary conditions to a 2D discrete operator and RHS.

Modifies the linear system A*u = b to enforce boundary conditions.

Parameters
ALinear operator (modified in place)
bRight-hand side vector (modified in place)
kOrder of accuracy
mNumber of cells in x-direction
dxCell spacing in x-direction
nNumber of cells in y-direction
dyCell spacing in y-direction
bcBoundary condition data

Definition at line 307 of file addscalarbc.cpp.

308 {
309 assert(bc.dc.n_elem == 4 && "dc must be a 4x1 vector");
310 assert(bc.nc.n_elem == 4 && "nc must be a 4x1 vector");
311 assert(bc.v.size() == 4 && "v must have 4 vectors");
312 assert(A.n_rows == A.n_cols && "A must be square");
313 assert(A.n_cols == b.n_elem && "b size must equal A columns");
314
315 sp_mat Al, Ar, Ab, At;
316 addScalarBClhs(k, m, dx, n, dy, bc.dc, bc.nc, Al, Ar, Ab, At);
317
318 uvec rl, rr, rb, rt;
319
320 BCPairLhs pairs[] = {
321 {boundaryNorm(bc.dc, bc.nc, 0, 1), Al, Ar, rl, rr}, // Left / Right
322 {boundaryNorm(bc.dc, bc.nc, 2, 3), Ab, At, rb, rt}, // Bottom / Top
323 };
324 for (auto &p : pairs) applyBCPair(A, b, p);
325
326 addScalarBCrhs(b, bc.dc, bc.nc, bc.v, rl, rr, rb, rt);
327}
std::vector< vec > v
Definition addscalarbc.h:58

◆ addScalarBC() [3/3]

void AddScalarBC::addScalarBC ( sp_mat & A,
vec & b,
u16 k,
u32 m,
Real dx,
u32 n,
Real dy,
u32 o,
Real dz,
const BC3D & bc )

Apply boundary conditions to a 3D discrete operator and RHS.

Modifies the linear system A*u = b to enforce boundary conditions.

Parameters
ALinear operator (modified in place)
bRight-hand side vector (modified in place)
kOrder of accuracy
mNumber of cells in x-direction
dxCell spacing in x-direction
nNumber of cells in y-direction
dyCell spacing in y-direction
oNumber of cells in z-direction
dzCell spacing in z-direction
bcBoundary condition data

Definition at line 329 of file addscalarbc.cpp.

330 {
331 assert(bc.dc.n_elem == 6 && "dc must be a 6x1 vector");
332 assert(bc.nc.n_elem == 6 && "nc must be a 6x1 vector");
333 assert(bc.v.size() == 6 && "v must have 6 vectors");
334 assert(A.n_rows == A.n_cols && "A must be square");
335 assert(A.n_cols == b.n_elem && "b size must equal A columns");
336
337 sp_mat Al, Ar, Ab, At, Af, Ak;
338 addScalarBClhs(k, m, dx, n, dy, o, dz, bc.dc, bc.nc,
339 Al, Ar, Ab, At, Af, Ak);
340
341 uvec rl, rr, rb, rt, rf, rk;
342
343 BCPairLhs pairs[] = {
344 {boundaryNorm(bc.dc, bc.nc, 0, 1), Al, Ar, rl, rr}, // Left / Right
345 {boundaryNorm(bc.dc, bc.nc, 2, 3), Ab, At, rb, rt}, // Bottom / Top
346 {boundaryNorm(bc.dc, bc.nc, 4, 5), Af, Ak, rf, rk}, // Front / Back
347 };
348 for (auto &p : pairs) applyBCPair(A, b, p);
349
350 addScalarBCrhs(b, bc.dc, bc.nc, bc.v, rl, rr, rb, rt, rf, rk);
351}
std::vector< vec > v
Definition addscalarbc.h:71

◆ addScalarBClhs() [1/3]

void AddScalarBC::addScalarBClhs ( u16 k,
u32 m,
Real dx,
const vec & dc,
const vec & nc,
sp_mat & Al,
sp_mat & Ar )

Compute LHS boundary condition matrices for a 1D problem.

Parameters
kOrder of accuracy
mNumber of cells
dxCell spacing
dcDirichlet coefficients (2x1: left, right)
ncNeumann coefficients (2x1: left, right)
AlOutput: left boundary modification matrix
ArOutput: right boundary modification matrix

Definition at line 145 of file addscalarbc.cpp.

147 {
148 Al = sp_mat(m+2, m+2);
149 Ar = sp_mat(m+2, m+2);
150
151 if (dc(0) != 0.0) Al(0, 0) = dc(0);
152 if (dc(1) != 0.0) Ar(m+1, m+1) = dc(1);
153
154 if (nc(0) != 0.0 || nc(1) != 0.0) {
155 Gradient G(k, m, dx);
156 sp_mat G_mat = sp_mat(G);
157
158 if (nc(0) != 0.0) {
159 sp_mat Bl(m+2, m+1);
160 Bl(0, 0) = -nc(0);
161 Al = Al + Bl * G_mat;
162 }
163 if (nc(1) != 0.0) {
164 sp_mat Br(m+2, m+1);
165 Br(m+1, m) = nc(1);
166 Ar = Ar + Br * G_mat;
167 }
168 }
169}
Mimetic Gradient operator.
Definition gradient.h:27

◆ addScalarBClhs() [2/3]

void AddScalarBC::addScalarBClhs ( u16 k,
u32 m,
Real dx,
u32 n,
Real dy,
const vec & dc,
const vec & nc,
sp_mat & Al,
sp_mat & Ar,
sp_mat & Ab,
sp_mat & At )

brief Compute LHS boundary condition matrices for a 2D problem

Parameters
kOrder of accuracy
mNumber of cells in x-direction
dxCell spacing in x-direction
nNumber of cells in y-direction
dyCell spacing in y-direction
dcDirichlet coefficients (4x1: left, right, bottom, top)
ncNeumann coefficients (4x1: left, right, bottom, top)
AlOutput: left edge modification matrix
ArOutput: right edge modification matrix
AbOutput: bottom edge modification matrix
AtOutput: top edge modification matrix

Definition at line 171 of file addscalarbc.cpp.

173 {
174 Al.set_size(0, 0); Ar.set_size(0, 0);
175 Ab.set_size(0, 0); At.set_size(0, 0);
176
177 Real qrl = boundaryNorm(dc, nc, 0, 1);
178 Real qbt = boundaryNorm(dc, nc, 2, 3);
179
180 if (qrl > 0) {
181 sp_mat Al0, Ar0;
182 addScalarBClhs(k, m, dx, {dc(0), dc(1)}, {nc(0), nc(1)}, Al0, Ar0);
183
184 sp_mat In = (qbt == 0) ? buildIdentity(n)
185 : buildIdentity(n+2, /*zeroCorners=*/true);
186 Al = kron(In, Al0);
187 Ar = kron(In, Ar0);
188 }
189
190 if (qbt > 0) {
191 sp_mat Ab0, At0;
192 addScalarBClhs(k, n, dy, {dc(2), dc(3)}, {nc(2), nc(3)}, Ab0, At0);
193
194 sp_mat Im = (qrl == 0) ? buildIdentity(m)
195 : buildIdentity(m+2);
196 Ab = kron(Ab0, Im);
197 At = kron(At0, Im);
198 }
199}
double Real
Definition utils.h:21

◆ addScalarBClhs() [3/3]

void AddScalarBC::addScalarBClhs ( u16 k,
u32 m,
Real dx,
u32 n,
Real dy,
u32 o,
Real dz,
const vec & dc,
const vec & nc,
sp_mat & Al,
sp_mat & Ar,
sp_mat & Ab,
sp_mat & At,
sp_mat & Af,
sp_mat & Ak )

brief Compute LHS boundary condition matrices for a 3D problem

Parameters
kOrder of accuracy
mNumber of cells in x-direction
dxCell spacing in x-direction
nNumber of cells in y-direction
dyCell spacing in y-direction
oNumber of cells in z-direction
dzCell spacing in z-direction
dcDirichlet coefficients (6x1: left, right, bottom, top, front, back)
ncNeumann coefficients (6x1: left, right, bottom, top, front, back)
AlOutput: left face modification matrix
ArOutput: right face modification matrix
AbOutput: bottom face modification matrix
AtOutput: top face modification matrix
AfOutput: front face modification matrix
AkOutput: back face modification matrix

Definition at line 201 of file addscalarbc.cpp.

204 {
205 Al.set_size(0,0); Ar.set_size(0,0);
206 Ab.set_size(0,0); At.set_size(0,0);
207 Af.set_size(0,0); Ak.set_size(0,0);
208
209 Real qrl = boundaryNorm(dc, nc, 0, 1);
210 Real qbt = boundaryNorm(dc, nc, 2, 3);
211 Real qfk = boundaryNorm(dc, nc, 4, 5);
212
213 auto makeId = [](u32 sz, Real q, bool excludeCorners) -> sp_mat {
214 return (q == 0) ? buildIdentity(sz)
215 : buildIdentity(sz + 2, excludeCorners);
216 };
217
218 if (qrl > 0) {
219 sp_mat Al0, Ar0;
220 addScalarBClhs(k, m, dx, {dc(0), dc(1)}, {nc(0), nc(1)}, Al0, Ar0);
221
222 sp_mat In = makeId(n, qbt, /*excludeCorners=*/true);
223 sp_mat Io = makeId(o, qfk, /*excludeCorners=*/true);
224 Al = kron(kron(Io, In), Al0);
225 Ar = kron(kron(Io, In), Ar0);
226 }
227
228 if (qbt > 0) {
229 sp_mat Ab0, At0;
230 addScalarBClhs(k, n, dy, {dc(2), dc(3)}, {nc(2), nc(3)}, Ab0, At0);
231
232 sp_mat Im = makeId(m, qrl, /*excludeCorners=*/false);
233 sp_mat Io = makeId(o, qfk, /*excludeCorners=*/true);
234 Ab = kron(kron(Io, Ab0), Im);
235 At = kron(kron(Io, At0), Im);
236 }
237
238 if (qfk > 0) {
239 sp_mat Af0, Ak0;
240 addScalarBClhs(k, o, dz, {dc(4), dc(5)}, {nc(4), nc(5)}, Af0, Ak0);
241
242 sp_mat Im = makeId(m, qrl, /*excludeCorners=*/false);
243 sp_mat In = makeId(n, qbt, /*excludeCorners=*/false);
244 Af = kron(kron(Af0, In), Im);
245 Ak = kron(kron(Ak0, In), Im);
246 }
247}

◆ addScalarBCrhs() [1/3]

void AddScalarBC::addScalarBCrhs ( vec & b,
const vec & dc,
const vec & nc,
const std::vector< vec > & v,
const uvec & rl,
const uvec & rr,
const uvec & rb,
const uvec & rt )

brief Apply boundary values to the RHS vector for a 2D problem

Parameters
bRight-hand side vector (modified in place)
dcDirichlet coefficients (4x1: left, right, bottom, top)
ncNeumann coefficients (4x1: left, right, bottom, top)
vBoundary values (4 vectors: left, right, bottom, top)
rlLeft edge row indices
rrRight edge row indices
rbBottom edge row indices
rtTop edge row indices

Definition at line 259 of file addscalarbc.cpp.

262 {
263 const BCPairRhs pairs[] = {
264 {0, 1, rl, rr, 0, 1}, // Left / Right
265 {2, 3, rb, rt, 2, 3}, // Bottom / Top
266 };
267 for (const auto &p : pairs) applyBCPairRhs(b, dc, nc, v, p);
268}

◆ addScalarBCrhs() [2/3]

void AddScalarBC::addScalarBCrhs ( vec & b,
const vec & dc,
const vec & nc,
const std::vector< vec > & v,
const uvec & rl,
const uvec & rr,
const uvec & rb,
const uvec & rt,
const uvec & rf,
const uvec & rk )

Apply boundary values to the RHS vector for a 3D problem.

Parameters
bRight-hand side vector (modified in place)
dcDirichlet coefficients (6x1: left, right, bottom, top, front, back)
ncNeumann coefficients (6x1: left, right, bottom, top, front, back)
vBoundary values (6 vectors: left, right, bottom, top, front, back)
rlLeft face row indices
rrRight face row indices
rbBottom face row indices
rtTop face row indices
rfFront face row indices
rkBack face row indices

Definition at line 270 of file addscalarbc.cpp.

273 {
274 const BCPairRhs pairs[] = {
275 {0, 1, rl, rr, 0, 1}, // Left / Right
276 {2, 3, rb, rt, 2, 3}, // Bottom / Top
277 {4, 5, rf, rk, 4, 5}, // Front / Back
278 };
279 for (const auto &p : pairs) applyBCPairRhs(b, dc, nc, v, p);
280}

◆ addScalarBCrhs() [3/3]

void AddScalarBC::addScalarBCrhs ( vec & b,
const vec & v,
const uvec & indices )

Apply boundary values to the RHS vector for a 1D problem.

Parameters
bRight-hand side vector (modified in place)
vBoundary values (2x1: left, right)
indicesRow indices to update

Definition at line 253 of file addscalarbc.cpp.

253 {
254 for (uword i = 0; i < indices.n_elem; i++) {
255 b(indices(i)) = v(i);
256 }
257}