Skip to content

Commit e117fa5

Browse files
authored
Merge pull request #3056 from boutproject/fix-after-reactoring-communicate
Fixes to address failing tests after changes to refactor-coordinates
2 parents 47cff9e + 834b090 commit e117fa5

7 files changed

Lines changed: 86 additions & 45 deletions

File tree

include/bout/coordinates.hxx

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,9 @@ public:
108108
const BoutReal& J(int x, int y) const { return J()(x, y); }
109109
#endif
110110

111-
void setDx(FieldMetric dx);
112-
void setDy(FieldMetric dy);
113-
void setDz(FieldMetric dz);
111+
void setDx(FieldMetric dx, const bool communicate = true);
112+
void setDy(FieldMetric dy, const bool communicate = true);
113+
void setDz(FieldMetric dz, const bool communicate = true);
114114

115115
void setD1_dx(FieldMetric d1_dx) { d1_dx_ = std::move(d1_dx); }
116116
void setD1_dy(FieldMetric d1_dy) { d1_dy_ = std::move(d1_dy); }
@@ -235,15 +235,19 @@ public:
235235
void setMetricTensor(const ContravariantMetricTensor& contravariant_metric_tensor,
236236
const CovariantMetricTensor& covariant_metric_tensor);
237237

238+
void communicateMetricTensor();
239+
240+
void communicateDz();
241+
238242
///< Coordinate system Jacobian, so volume of cell is J*dx*dy*dz
239243
FieldMetric& J() const;
240244

241245
///< Magnitude of B = nabla z times nabla x
242246
const FieldMetric& Bxy() const { return Bxy_; }
243247

244-
void setJ(const FieldMetric& J);
248+
void setJ(const FieldMetric& J, const bool communicate = true);
245249

246-
void setBxy(FieldMetric Bxy);
250+
void setBxy(FieldMetric Bxy, const bool communicate = true);
247251

248252
/// d pitch angle / dx. Needed for vector differentials (Curl)
249253
const FieldMetric& ShiftTorsion() const { return ShiftTorsion_; }

include/bout/mesh.hxx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,9 @@ public:
653653
inserted.first->second->recalculateAndReset(recalculate_staggered,
654654
force_interpolate_from_centre);
655655

656+
inserted.first->second->communicateMetricTensor();
657+
inserted.first->second->communicateDz();
658+
656659
return inserted.first->second;
657660
}
658661

include/bout/metric_tensor.hxx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ public:
6161
g23_m = metric_tensor.g23();
6262
}
6363

64-
MetricTensor inverse(const std::string& region = "RGN_ALL");
64+
MetricTensor inverse(const std::string& region = "RGN_ALL",
65+
const bool communicate = true);
6566

6667
// Transforms the MetricTensor by applying the given function to every component
6768
template <class F>
@@ -74,6 +75,8 @@ public:
7475
g23_m = function(g23_m);
7576
}
7677

78+
void communicate() const;
79+
7780
private:
7881
FieldMetric g11_m, g22_m, g33_m, g12_m, g13_m, g23_m;
7982
};

src/mesh/coordinates.cxx

Lines changed: 43 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -501,9 +501,11 @@ void Coordinates::interpolateFromCoordinates(Options* mesh_options,
501501
checkCovariant();
502502

503503
setJ(interpolateAndExtrapolate(coords_in->J(), location, true, true, false,
504-
transform.get()));
504+
transform.get()),
505+
false);
505506
setBxy(interpolateAndExtrapolate(coords_in->Bxy(), location, true, true, false,
506-
transform.get()));
507+
transform.get()),
508+
false);
507509

508510
bout::checkFinite(J(), "The Jacobian", "RGN_NOCORNERS");
509511
bout::checkPositive(J(), "The Jacobian", "RGN_NOCORNERS");
@@ -567,9 +569,9 @@ void Coordinates::readFromMesh(Options* mesh_options, const std::string& suffix)
567569
dy_ = interpolateAndExtrapolate(dy_, location, extrapolate_x, extrapolate_y, false,
568570
transform.get());
569571

570-
setDx(dx_);
571-
setDy(dy_);
572-
setDz(dz_);
572+
setDx(dx_, false);
573+
setDy(dy_, false);
574+
setDz(dz_, false);
573575

574576
// grid data source has staggered fields, so read instead of interpolating
575577
// Diagonal components of metric tensor g^{ij} (default to 1)
@@ -618,7 +620,8 @@ void Coordinates::readFromMesh(Options* mesh_options, const std::string& suffix)
618620
output_warn.write("\tWARNING! Covariant components of metric tensor set manually. "
619621
"Contravariant components NOT recalculated\n");
620622
} else {
621-
covariantMetricTensor.setMetricTensor(contravariantMetricTensor.inverse());
623+
covariantMetricTensor.setMetricTensor(
624+
contravariantMetricTensor.inverse("RGN_ALL", false));
622625
output_warn.write("Not all covariant components of metric tensor found. "
623626
"Calculating all from the contravariant tensor\n");
624627
}
@@ -639,15 +642,16 @@ void Coordinates::readFromMesh(Options* mesh_options, const std::string& suffix)
639642
const auto J_from_file = getAtLoc(localmesh, "J", suffix, location);
640643
// Compare calculated and loaded values
641644
output_warn.write("\tMaximum difference in J is {:e}\n", max(abs(J() - J_from_file)));
642-
setJ(J_from_file);
645+
setJ(J_from_file, false);
643646

644647
communicate(J());
645648
}
646649

647650
// More robust to extrapolate derived quantities directly, rather than
648651
// deriving from extrapolated covariant metric components
649652
setJ(interpolateAndExtrapolate(J(), location, extrapolate_x, extrapolate_y, false,
650-
transform.get()));
653+
transform.get()),
654+
false);
651655

652656
// Check jacobian
653657
bout::checkFinite(J(), "J" + suffix, "RGN_NOCORNERS");
@@ -662,15 +666,16 @@ void Coordinates::readFromMesh(Options* mesh_options, const std::string& suffix)
662666
"Calculating from metric tensor\n",
663667
suffix);
664668
// Re-evaluate Bxy using new J
665-
setBxy(recalculateBxy());
669+
setBxy(recalculateBxy(), false);
666670
} else {
667671
const auto Bcalc = getAtLoc(localmesh, "Bxy", suffix, location);
668-
setBxy(Bcalc);
672+
setBxy(Bcalc, false);
669673
output_warn.write("\tMaximum difference in Bxy is {:e}\n", max(abs(Bxy() - Bcalc)));
670674
}
671675

672676
setBxy(interpolateAndExtrapolate(Bxy(), location, extrapolate_x, extrapolate_y, false,
673-
transform.get()));
677+
transform.get()),
678+
false);
674679

675680
// Check Bxy
676681
bout::checkFinite(Bxy(), "Bxy" + suffix, "RGN_NOCORNERS");
@@ -762,31 +767,34 @@ const Field2D& Coordinates::zlength() const {
762767
return *zlength_cache;
763768
}
764769

765-
void Coordinates::setDx(FieldMetric dx) {
770+
void Coordinates::setDx(FieldMetric dx, const bool communicate) {
766771
if (min(abs(dx)) < 1e-8) {
767772
throw BoutException("dx magnitude less than 1e-8");
768773
}
769-
770774
dx_ = std::move(dx);
771-
localmesh->communicate(dx_);
775+
if (communicate) {
776+
localmesh->communicate(dx_);
777+
}
772778
}
773779

774-
void Coordinates::setDy(FieldMetric dy) {
780+
void Coordinates::setDy(FieldMetric dy, const bool communicate) {
775781
if (min(abs(dy)) < 1e-8) {
776782
throw BoutException("dy magnitude less than 1e-8");
777783
}
778-
779784
dy_ = std::move(dy);
780-
localmesh->communicate(dy_);
785+
if (communicate) {
786+
localmesh->communicate(dy_);
787+
}
781788
}
782789

783-
void Coordinates::setDz(FieldMetric dz) {
790+
void Coordinates::setDz(FieldMetric dz, const bool communicate) {
784791
if (min(abs(dz)) < 1e-8) {
785792
throw BoutException("dz magnitude less than 1e-8");
786793
}
787-
788794
dz_ = std::move(dz);
789-
localmesh->communicate(dz_);
795+
if (communicate) {
796+
localmesh->communicate(dz_);
797+
}
790798
}
791799

792800
void Coordinates::recalculateAndReset(bool recalculate_staggered,
@@ -1491,19 +1499,23 @@ FieldMetric& Coordinates::J() const {
14911499
return *jacobian_cache;
14921500
}
14931501

1494-
void Coordinates::setJ(const FieldMetric& J) {
1502+
void Coordinates::setJ(const FieldMetric& J, const bool communicate) {
14951503
bout::checkFinite(J, "J", "RGN_NOCORNERS");
14961504
bout::checkPositive(J, "J", "RGN_NOCORNERS");
14971505

14981506
//TODO: Calculate J and check value is close
14991507
jacobian_cache = std::make_unique<FieldMetric>(J);
1500-
localmesh->communicate(*jacobian_cache);
1508+
if (communicate) {
1509+
localmesh->communicate(*jacobian_cache);
1510+
}
15011511
}
15021512

1503-
void Coordinates::setBxy(FieldMetric Bxy) {
1513+
void Coordinates::setBxy(FieldMetric Bxy, const bool communicate) {
15041514
//TODO: Calculate Bxy and check value is close
15051515
Bxy_ = std::move(Bxy);
1506-
localmesh->communicate(Bxy_);
1516+
if (communicate) {
1517+
localmesh->communicate(Bxy_);
1518+
}
15071519
}
15081520

15091521
void Coordinates::setContravariantMetricTensor(
@@ -1529,3 +1541,10 @@ void Coordinates::setMetricTensor(
15291541
contravariantMetricTensor.setMetricTensor(contravariant_metric_tensor);
15301542
covariantMetricTensor.setMetricTensor(covariant_metric_tensor);
15311543
}
1544+
1545+
void Coordinates::communicateMetricTensor() {
1546+
contravariantMetricTensor.communicate();
1547+
covariantMetricTensor.communicate();
1548+
}
1549+
1550+
void Coordinates::communicateDz() { localmesh->communicate(dz_); }

src/mesh/metric_tensor.cxx

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ void MetricTensor::check(int ystart) {
7272
}
7373
}
7474

75-
MetricTensor MetricTensor::inverse(const std::string& region) {
75+
MetricTensor MetricTensor::inverse(const std::string& region, const bool communicate) {
7676

7777
TRACE("MetricTensor::inverse");
7878

@@ -124,7 +124,19 @@ MetricTensor MetricTensor::inverse(const std::string& region) {
124124

125125
output_info.write("\tMaximum error in off-diagonal inversion is {:e}\n",
126126
off_diagonal_maxerr);
127-
128-
g_11.getMesh()->communicate(g_11, g_22, g_33, g_12, g_13, g_23);
127+
if (communicate) {
128+
MetricTensor::communicate();
129+
}
129130
return MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23);
130131
}
132+
133+
void MetricTensor::communicate() const {
134+
// copy to non-const variables (because communicate() cannot take const references)
135+
auto tmp = g11_m;
136+
auto tmp2 = g22_m;
137+
auto tmp3 = g33_m;
138+
auto tmp4 = g12_m;
139+
auto tmp5 = g13_m;
140+
auto tmp6 = g23_m;
141+
g11_m.getMesh()->communicate(tmp, tmp2, tmp3, tmp4, tmp5, tmp6);
142+
}

tests/integrated/test-snb/test_snb.cxx

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -209,20 +209,18 @@ int main(int argc, char** argv) {
209209
// Change the mesh spacing and cell volume (Jdy)
210210
Coordinates* coord = Te.getCoordinates();
211211

212-
{
213-
auto dy = emptyFrom(coord->dx());
214-
auto J = emptyFrom(coord->J());
215-
for (int x = mesh->xstart; x <= mesh->xend; x++) {
216-
for (int y = mesh->ystart; y <= mesh->yend; y++) {
217-
const double y_n = (double(y) + 0.5) / double(mesh->yend + 1);
218-
219-
dy(x, y) = 1. - 0.9 * y_n;
220-
J(x, y) = 1. + y_n * y_n;
221-
}
212+
auto dy_copy = coord->dy();
213+
auto J_copy = coord->J();
214+
for (int x = mesh->xstart; x <= mesh->xend; x++) {
215+
for (int y = mesh->ystart; y <= mesh->yend; y++) {
216+
const double y_n = (double(y) + 0.5) / double(mesh->yend + 1);
217+
218+
dy_copy(x, y) = 1. - 0.9 * y_n;
219+
J_copy(x, y) = 1. + y_n * y_n;
222220
}
223-
coord->setDy(dy);
224-
coord->setJ(J);
225221
}
222+
coord->setDy(dy_copy);
223+
coord->setJ(J_copy);
226224

227225
HeatFluxSNB snb;
228226

tests/unit/test_extras.hxx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,7 @@ public:
440440
Field2D{1.0}, Field2D{1.0}, Field2D{1.0}, Field2D{1.0}, Field2D{0.0},
441441
Field2D{0.0}, Field2D{0.0}, Field2D{1.0}, Field2D{1.0}, Field2D{1.0},
442442
Field2D{0.0}, Field2D{0.0}, Field2D{0.0}, Field2D{0.0}, Field2D{0.0});
443+
static_cast<FakeMesh*>(bout::globals::mesh)->setCoordinates(test_coords);
443444

444445
// Set nonuniform corrections
445446
test_coords->setNon_uniform(true);
@@ -488,6 +489,7 @@ public:
488489
Field2D{0.0, mesh_staggered}, Field2D{0.0, mesh_staggered},
489490
Field2D{0.0, mesh_staggered}, Field2D{0.0, mesh_staggered},
490491
Field2D{0.0, mesh_staggered});
492+
static_cast<FakeMesh*>(mesh_staggered)->setCoordinates(test_coords_staggered);
491493

492494
// Set nonuniform corrections
493495
test_coords_staggered->setNon_uniform(true);

0 commit comments

Comments
 (0)