Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Field3D::setBoundaryTo parallel boundary conditions #2962

Open
wants to merge 8 commits into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions include/bout/field3d.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,11 @@ public:
/// This uses 2nd order central differences to set the value
/// on the boundary to the value on the boundary in field \p f3d.
/// Note: does not just copy values in boundary region.
///
/// - If f3d has parallel slices then this field must also have them.
/// - If f3d has no parallel slices then this field's parallel slices
/// (if any) will be cleared, and to/fromFieldAligned will be used
/// to set parallel boundary conditions.
void setBoundaryTo(const Field3D& f3d);

void applyParallelBoundary() override;
Expand Down
84 changes: 73 additions & 11 deletions src/field/field3d.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -451,19 +451,81 @@ void Field3D::setBoundaryTo(const Field3D& f3d) {

allocate(); // Make sure data allocated

/// Loop over boundary regions
for (const auto& reg : fieldmesh->getBoundaries()) {
/// Loop within each region
for (reg->first(); !reg->isDone(); reg->next()) {
for (int z = 0; z < nz; z++) {
// Get value half-way between cells
BoutReal val =
0.5 * (f3d(reg->x, reg->y, z) + f3d(reg->x - reg->bx, reg->y - reg->by, z));
// Set to this value
(*this)(reg->x, reg->y, z) =
2. * val - (*this)(reg->x - reg->bx, reg->y - reg->by, z);
if (!fieldmesh->periodicX) {
// X boundaries
if (fieldmesh->firstX()) {
for (int jy = fieldmesh->ystart; jy <= fieldmesh->yend; ++jy) {
for (int jz = 0; jz < fieldmesh->LocalNz; jz++) {
BoutReal val =
bendudson marked this conversation as resolved.
Show resolved Hide resolved
0.5 * (f3d(fieldmesh->xstart, jy, jz) + f3d(fieldmesh->xstart - 1, jy, jz));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: expression result unused [clang-diagnostic-unused-value]

              0.5 * (f3d(fieldmesh->xstart, jy, jz) + f3d(fieldmesh->xstart - 1, jy, jz));
                  ^

(*this)(fieldmesh->xstart - 1, jy, jz) =
2. * val - (*this)(fieldmesh->xstart, jy, jz);
}
}
}
if (fieldmesh->lastX()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: use of undeclared identifier 'jy' [clang-diagnostic-error]

          BoutReal const val = 0.5 * (f3d(fieldmesh->xend, jy, jz) + f3d(fieldmesh->xend + 1, jy, jz));
                                                           ^

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: use of undeclared identifier 'jz' [clang-diagnostic-error]

          BoutReal const val = 0.5 * (f3d(fieldmesh->xend, jy, jz) + f3d(fieldmesh->xend + 1, jy, jz));
                                                               ^

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: use of undeclared identifier 'jy' [clang-diagnostic-error]

          BoutReal const val = 0.5 * (f3d(fieldmesh->xend, jy, jz) + f3d(fieldmesh->xend + 1, jy, jz));
                                                                                              ^

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: use of undeclared identifier 'jz' [clang-diagnostic-error]

          BoutReal const val = 0.5 * (f3d(fieldmesh->xend, jy, jz) + f3d(fieldmesh->xend + 1, jy, jz));
                                                                                                  ^

for (int jy = fieldmesh->ystart; jy <= fieldmesh->yend; ++jy) {
bendudson marked this conversation as resolved.
Show resolved Hide resolved
for (int jz = 0; jz < fieldmesh->LocalNz; jz++) {
BoutReal val =
0.5 * (f3d(fieldmesh->xend, jy, jz) + f3d(fieldmesh->xend + 1, jy, jz));
(*this)(fieldmesh->xend + 1, jy, jz) =
2. * val - (*this)(fieldmesh->xend, jy, jz);
}
}
}
}

// Y boundaries

if (f3d.hasParallelSlices()) {
// Argument has parallel slices, so this field must as well.
ASSERT1(hasParallelSlices());

for (auto& bndry : fieldmesh->getBoundariesPar()) {
const Field3D& f3d_next = f3d.ynext(bndry->dir);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: no member named 'x' in 'BoundaryRegionPar' [clang-diagnostic-error]

        const int x = bndry->x;
                             ^

Field3D& this_next = ynext(bndry->dir);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: no member named 'y' in 'BoundaryRegionPar' [clang-diagnostic-error]

        const int y = bndry->y;
                             ^


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: no member named 'z' in 'BoundaryRegionPar' [clang-diagnostic-error]

        const int z = bndry->z;
                             ^

for (bndry->first(); !bndry->isDone(); bndry->next()) {
const int x = bndry->x;
const int y = bndry->y;
const int z = bndry->z;
BoutReal val = 0.5 * (f3d(x, y, z) + f3d_next(x, y, z));
bendudson marked this conversation as resolved.
Show resolved Hide resolved
this_next(x, y, z) = 2. * val - (*this)(x, y, z);
bendudson marked this conversation as resolved.
Show resolved Hide resolved
}
}
Copy link
Contributor

@dschwoerer dschwoerer Aug 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be roughly the new syntax:

Suggested change
for (auto& bndry : fieldmesh->getBoundariesPar()) {
const Field3D& f3d_next = f3d.ynext(bndry->dir);
Field3D& this_next = ynext(bndry->dir);
for (bndry->first(); !bndry->isDone(); bndry->next()) {
const int x = bndry->x;
const int y = bndry->y;
const int z = bndry->z;
BoutReal val = 0.5 * (f3d(x, y, z) + f3d_next(x, y, z));
this_next(x, y, z) = 2. * val - (*this)(x, y, z);
}
}
for (auto& region : fieldmesh->getBoundariesPar()) {
for (auto& pnt: region) {
pnt.dirichlet_o1(*this, pnt.interpolate_sheath_o1(f3d));
}
}


} else {
// Argument does not have parallel slices
// Make sure that this field also doesn't have them because in that case
// the parallel derivative operators would erroneously use parallel slices
clearParallelSlices();

// Shift into field-aligned coordinates to apply Y boundary conditions.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: loop variable name 'r' is too short, expected at least 2 characters [readability-identifier-length]

    for (RangeIterator r = fieldmesh->iterateBndryLowerY(); !r.isDone(); r++) {
                       ^

const Field3D f3d_fa = toFieldAligned(f3d);
(*this) = toFieldAligned(*this);
bendudson marked this conversation as resolved.
Show resolved Hide resolved

for (RangeIterator r = fieldmesh->iterateBndryLowerY(); !r.isDone(); r++) {
for (int jz = 0; jz < fieldmesh->LocalNz; jz++) {
BoutReal val = 0.5
* (f3d_fa(r.ind, fieldmesh->ystart, jz)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: loop variable name 'r' is too short, expected at least 2 characters [readability-identifier-length]

    for (RangeIterator r = fieldmesh->iterateBndryUpperY(); !r.isDone(); r++) {
                       ^

+ f3d_fa(r.ind, fieldmesh->ystart - 1, jz));
(*this)(r.ind, fieldmesh->ystart - 1, jz) =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: variable 'val' of type 'BoutReal' (aka 'double') can be declared 'const' [misc-const-correctness]

Suggested change
(*this)(r.ind, fieldmesh->ystart - 1, jz) =
BoutReal const val = 0.5 * (f3d_fa(r.ind, fieldmesh->yend, jz) + f3d_fa(r.ind, fieldmesh->yend + 1, jz));

2. * val - (*this)(r.ind, fieldmesh->ystart, jz);
}
}

for (RangeIterator r = fieldmesh->iterateBndryUpperY(); !r.isDone(); r++) {
for (int jz = 0; jz < fieldmesh->LocalNz; jz++) {
BoutReal val = 0.5
* (f3d_fa(r.ind, fieldmesh->yend, jz)
+ f3d_fa(r.ind, fieldmesh->yend + 1, jz));
(*this)(r.ind, fieldmesh->yend + 1, jz) =
2. * val - (*this)(r.ind, fieldmesh->yend, jz);
}
}

(*this) = fromFieldAligned(*this);
}
}

Expand Down