From e2ecb16b73151b6289895553f949d4bf72bee73d Mon Sep 17 00:00:00 2001 From: tomc271 Date: Mon, 28 Aug 2023 21:03:27 +0100 Subject: [PATCH 001/491] Encapsulate g11, g22, g33, g12, g13, g23 on coordinates class. --- include/bout/coordinates.hxx | 24 ++++ src/field/vector2d.cxx | 29 ++-- src/field/vector3d.cxx | 37 ++--- .../impls/multigrid/multigrid_laplace.cxx | 16 ++- .../laplace/impls/naulin/naulin_laplace.cxx | 5 +- .../laplace/impls/serial_band/serial_band.cxx | 56 ++++---- src/invert/laplace/invert_laplace.cxx | 23 +-- .../impls/cyclic/laplacexz-cyclic.cxx | 9 +- src/mesh/boundary_standard.cxx | 63 ++++---- src/mesh/fv_ops.cxx | 28 ++-- .../invert/laplace/test_laplace_cyclic.cxx | 3 +- tests/unit/mesh/data/test_gridfromoptions.cxx | 134 ++++++++++-------- tests/unit/mesh/test_coordinates.cxx | 55 +++---- 13 files changed, 276 insertions(+), 206 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index 49feffa0a7..4f92bdecfb 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -97,11 +97,35 @@ public: FieldMetric Bxy; ///< Magnitude of B = nabla z times nabla x /// Contravariant metric tensor (g^{ij}) +private: FieldMetric g11, g22, g33, g12, g13, g23; +public: /// Covariant metric tensor FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; + struct MetricTensor + { + FieldMetric g11, g22, g33, g12, g13, g23; + }; + + MetricTensor getContravariantMetricTensor() + { + MetricTensor g_contravariant = { g11, g22, g33, g12, g13, g23 }; + return g_contravariant; + } + + void setContravariantMetricTensor(MetricTensor g) + { + g11 = g.g11; + g22 = g.g22; + g33 = g.g33; + g12 = g.g12; + g13 = g.g13; + g23 = g.g23; + calcContravariant(); + } + /// Christoffel symbol of the second kind (connection coefficients) FieldMetric G1_11, G1_22, G1_33, G1_12, G1_13, G1_23; FieldMetric G2_11, G2_22, G2_33, G2_12, G2_13, G2_23; diff --git a/src/field/vector2d.cxx b/src/field/vector2d.cxx index 74d3a88a22..e1d541004f 100644 --- a/src/field/vector2d.cxx +++ b/src/field/vector2d.cxx @@ -140,12 +140,12 @@ void Vector2D::toContravariant() { // multiply by g_{ij} BOUT_FOR(i, x.getRegion("RGN_ALL")) { - x[i] = metric_x->g11[i] * x[i] + metric_x->g12[i] * y_at_x[i] - + metric_x->g13[i] * z_at_x[i]; - y[i] = metric_y->g22[i] * y[i] + metric_y->g12[i] * x_at_y[i] - + metric_y->g23[i] * z_at_y[i]; - z[i] = metric_z->g33[i] * z[i] + metric_z->g13[i] * x_at_z[i] - + metric_z->g23[i] * y_at_z[i]; + Coordinates::MetricTensor g_x = metric_x->getContravariantMetricTensor(); + Coordinates::MetricTensor g_y = metric_y->getContravariantMetricTensor(); + Coordinates::MetricTensor g_z = metric_z->getContravariantMetricTensor(); + x[i] = g_x.g11[i] * x[i] + g_x.g12[i] * y_at_x[i] + g_x.g13[i] * z_at_x[i]; + y[i] = g_y.g22[i] * y[i] + g_y.g12[i] * x_at_y[i] + g_y.g23[i] * z_at_y[i]; + z[i] = g_z.g33[i] * z[i] + g_z.g13[i] * x_at_z[i] + g_z.g23[i] * y_at_z[i]; }; } else { @@ -155,9 +155,10 @@ void Vector2D::toContravariant() { Coordinates::FieldMetric gx{emptyFrom(x)}, gy{emptyFrom(y)}, gz{emptyFrom(z)}; BOUT_FOR(i, x.getRegion("RGN_ALL")) { - gx[i] = metric->g11[i] * x[i] + metric->g12[i] * y[i] + metric->g13[i] * z[i]; - gy[i] = metric->g22[i] * y[i] + metric->g12[i] * x[i] + metric->g23[i] * z[i]; - gz[i] = metric->g33[i] * z[i] + metric->g13[i] * x[i] + metric->g23[i] * y[i]; + Coordinates::MetricTensor g = metric->getContravariantMetricTensor(); + gx[i] = g.g11[i] * x[i] + g.g12[i] * y[i] + g.g13[i] * z[i]; + gy[i] = g.g22[i] * y[i] + g.g12[i] * x[i] + g.g23[i] * z[i]; + gz[i] = g.g33[i] * z[i] + g.g13[i] * x[i] + g.g23[i] * y[i]; }; x = gx; @@ -390,11 +391,13 @@ const Coordinates::FieldMetric Vector2D::operator*(const Vector2D& rhs) const { if (covariant) { // Both covariant + + Coordinates::MetricTensor g = metric->getContravariantMetricTensor(); result = - x * rhs.x * metric->g11 + y * rhs.y * metric->g22 + z * rhs.z * metric->g33; - result += (x * rhs.y + y * rhs.x) * metric->g12 - + (x * rhs.z + z * rhs.x) * metric->g13 - + (y * rhs.z + z * rhs.y) * metric->g23; + x * rhs.x * g.g11 + y * rhs.y * g.g22 + z * rhs.z * g.g33; + result += (x * rhs.y + y * rhs.x) * g.g12 + + (x * rhs.z + z * rhs.x) * g.g13 + + (y * rhs.z + z * rhs.y) * g.g23; } else { // Both contravariant result = diff --git a/src/field/vector3d.cxx b/src/field/vector3d.cxx index 56124595b3..6ea927a8df 100644 --- a/src/field/vector3d.cxx +++ b/src/field/vector3d.cxx @@ -141,12 +141,12 @@ void Vector3D::toContravariant() { // multiply by g_{ij} BOUT_FOR(i, localmesh->getRegion3D("RGN_ALL")) { - x[i] = metric_x->g11[i] * x[i] + metric_x->g12[i] * y_at_x[i] - + metric_x->g13[i] * z_at_x[i]; - y[i] = metric_y->g22[i] * y[i] + metric_y->g12[i] * x_at_y[i] - + metric_y->g23[i] * z_at_y[i]; - z[i] = metric_z->g33[i] * z[i] + metric_z->g13[i] * x_at_z[i] - + metric_z->g23[i] * y_at_z[i]; + Coordinates::MetricTensor g_x = metric_x->getContravariantMetricTensor(); + Coordinates::MetricTensor g_y = metric_y->getContravariantMetricTensor(); + Coordinates::MetricTensor g_z = metric_z->getContravariantMetricTensor(); + x[i] = g_x.g11[i] * x[i] + g_x.g12[i] * y_at_x[i] + g_x.g13[i] * z_at_x[i]; + y[i] = g_y.g22[i] * y[i] + g_y.g12[i] * x_at_y[i] + g_y.g23[i] * z_at_y[i]; + z[i] = g_z.g33[i] * z[i] + g_z.g13[i] * x_at_z[i] + g_z.g23[i] * y_at_z[i]; }; } else { @@ -156,9 +156,10 @@ void Vector3D::toContravariant() { Field3D gx{emptyFrom(x)}, gy{emptyFrom(y)}, gz{emptyFrom(z)}; BOUT_FOR(i, localmesh->getRegion3D("RGN_ALL")) { - gx[i] = metric->g11[i] * x[i] + metric->g12[i] * y[i] + metric->g13[i] * z[i]; - gy[i] = metric->g22[i] * y[i] + metric->g12[i] * x[i] + metric->g23[i] * z[i]; - gz[i] = metric->g33[i] * z[i] + metric->g13[i] * x[i] + metric->g23[i] * y[i]; + Coordinates::MetricTensor g = metric->getContravariantMetricTensor(); + gx[i] = g.g11[i] * x[i] + g.g12[i] * y[i] + g.g13[i] * z[i]; + gy[i] = g.g22[i] * y[i] + g.g12[i] * x[i] + g.g23[i] * z[i]; + gz[i] = g.g33[i] * z[i] + g.g13[i] * x[i] + g.g23[i] * y[i]; }; x = gx; @@ -482,11 +483,12 @@ const Field3D Vector3D::operator*(const Vector3D& rhs) const { if (covariant) { // Both covariant + Coordinates::MetricTensor g = metric->getContravariantMetricTensor(); result = - x * rhs.x * metric->g11 + y * rhs.y * metric->g22 + z * rhs.z * metric->g33; - result += (x * rhs.y + y * rhs.x) * metric->g12 - + (x * rhs.z + z * rhs.x) * metric->g13 - + (y * rhs.z + z * rhs.y) * metric->g23; + x * rhs.x * g.g11 + y * rhs.y * g.g22 + z * rhs.z * g.g33; + result += (x * rhs.y + y * rhs.x) * g.g12 + + (x * rhs.z + z * rhs.x) * g.g13 + + (y * rhs.z + z * rhs.y) * g.g23; } else { // Both contravariant result = @@ -514,11 +516,12 @@ const Field3D Vector3D::operator*(const Vector2D& rhs) const { Coordinates* metric = x.getCoordinates(location); if (covariant) { // Both covariant + Coordinates::MetricTensor g = metric->getContravariantMetricTensor(); result = - x * rhs.x * metric->g11 + y * rhs.y * metric->g22 + z * rhs.z * metric->g33; - result += (x * rhs.y + y * rhs.x) * metric->g12 - + (x * rhs.z + z * rhs.x) * metric->g13 - + (y * rhs.z + z * rhs.y) * metric->g23; + x * rhs.x * g.g11 + y * rhs.y * g.g22 + z * rhs.z * g.g33; + result += (x * rhs.y + y * rhs.x) * g.g12 + + (x * rhs.z + z * rhs.x) * g.g13 + + (y * rhs.z + z * rhs.y) * g.g23; } else { // Both contravariant result = diff --git a/src/invert/laplace/impls/multigrid/multigrid_laplace.cxx b/src/invert/laplace/impls/multigrid/multigrid_laplace.cxx index 82273ee7ad..7e6fcdb8c9 100644 --- a/src/invert/laplace/impls/multigrid/multigrid_laplace.cxx +++ b/src/invert/laplace/impls/multigrid/multigrid_laplace.cxx @@ -597,27 +597,29 @@ void LaplaceMultigrid::generateMatrixF(int level) { int k2p = (k2 + 1) % Nz_global; int k2m = (k2 + Nz_global - 1) % Nz_global; + Coordinates::MetricTensor g = coords->getContravariantMetricTensor(); + BoutReal dz = coords->dz(i2, yindex); BoutReal ddx_C = (C2(i2 + 1, yindex, k2) - C2(i2 - 1, yindex, k2)) / 2. / coords->dx(i2, yindex) / C1(i2, yindex, k2); BoutReal ddz_C = (C2(i2, yindex, k2p) - C2(i2, yindex, k2m)) / 2. / dz / C1(i2, yindex, k2); - BoutReal ddx = D(i2, yindex, k2) * coords->g11(i2, yindex) / coords->dx(i2, yindex) + BoutReal ddx = D(i2, yindex, k2) * g.g11(i2, yindex) / coords->dx(i2, yindex) / coords->dx(i2, yindex); // coefficient of 2nd derivative stencil (x-direction) - BoutReal ddz = D(i2, yindex, k2) * coords->g33(i2, yindex) / SQ(dz); + BoutReal ddz = D(i2, yindex, k2) * g.g33(i2, yindex) / SQ(dz); // coefficient of 2nd derivative stencil (z-direction) BoutReal dxdz = - D(i2, yindex, k2) * 2. * coords->g13(i2, yindex) / coords->dx(i2, yindex) / dz; + D(i2, yindex, k2) * 2. * g.g13(i2, yindex) / coords->dx(i2, yindex) / dz; // coefficient of mixed derivative stencil (could assume zero, at least initially, // if easier; then check this is true in constructor) BoutReal dxd = - (D(i2, yindex, k2) * coords->G1(i2, yindex) + coords->g11(i2, yindex) * ddx_C - + coords->g13(i2, yindex) + (D(i2, yindex, k2) * coords->G1(i2, yindex) + g.g11(i2, yindex) * ddx_C + + g.g13(i2, yindex) * ddz_C // (could assume zero, at least initially, if easier; then check this is true in constructor) ) / coords->dx(i2, yindex); // coefficient of 1st derivative stencil (x-direction) @@ -627,8 +629,8 @@ void LaplaceMultigrid::generateMatrixF(int level) { } BoutReal dzd = - (D(i2, yindex, k2) * coords->G3(i2, yindex) + coords->g33(i2, yindex) * ddz_C - + coords->g13(i2, yindex) + (D(i2, yindex, k2) * coords->G3(i2, yindex) + g.g33(i2, yindex) * ddz_C + + g.g13(i2, yindex) * ddx_C // (could assume zero, at least initially, if easier; then check // this is true in constructor) ) diff --git a/src/invert/laplace/impls/naulin/naulin_laplace.cxx b/src/invert/laplace/impls/naulin/naulin_laplace.cxx index d82f874cbb..8bcb7af624 100644 --- a/src/invert/laplace/impls/naulin/naulin_laplace.cxx +++ b/src/invert/laplace/impls/naulin/naulin_laplace.cxx @@ -248,9 +248,10 @@ Field3D LaplaceNaulin::solve(const Field3D& rhs, const Field3D& x0) { // Derivatives of x Field3D ddx_x = DDX(x_in, location, "C2"); Field3D ddz_x = DDZ(x_in, location, "FFT"); + Coordinates::MetricTensor g = coords->getContravariantMetricTensor(); return rhsOverD - - (coords->g11 * coef_x_AC * ddx_x + coords->g33 * coef_z * ddz_x - + coords->g13 * (coef_x_AC * ddz_x + coef_z * ddx_x)) + - (g.g11 * coef_x_AC * ddx_x + g.g33 * coef_z * ddz_x + + g.g13 * (coef_x_AC * ddz_x + coef_z * ddx_x)) - AOverD_AC * x_in; }; diff --git a/src/invert/laplace/impls/serial_band/serial_band.cxx b/src/invert/laplace/impls/serial_band/serial_band.cxx index eda76498fc..4ec05e97c1 100644 --- a/src/invert/laplace/impls/serial_band/serial_band.cxx +++ b/src/invert/laplace/impls/serial_band/serial_band.cxx @@ -158,9 +158,10 @@ FieldPerp LaplaceSerialBand::solve(const FieldPerp& b, const FieldPerp& x0) { A(ix, 4) = 0.; #else // Set coefficients - coef1 = coords->g11(ix, jy); // X 2nd derivative - coef2 = coords->g33(ix, jy); // Z 2nd derivative - coef3 = coords->g13(ix, jy); // X-Z mixed derivatives + Coordinates::MetricTensor g = coords->getContravariantMetricTensor(); + coef1 = g.g11(ix, jy); // X 2nd derivative + coef2 = g.g33(ix, jy); // Z 2nd derivative + coef3 = g.g13(ix, jy); // X-Z mixed derivatives coef4 = 0.0; // X 1st derivative coef5 = 0.0; // Z 1st derivative coef6 = Acoef(ix, jy); // Constant @@ -178,7 +179,7 @@ FieldPerp LaplaceSerialBand::solve(const FieldPerp& b, const FieldPerp& x0) { if (nonuniform) { // non-uniform localmesh correction if ((ix != 0) && (ix != ncx)) { - coef4 += coords->g11(ix, jy) + coef4 += g.g11(ix, jy) * ((1.0 / coords->dx(ix + 1, jy)) - (1.0 / coords->dx(ix - 1, jy))) / (2.0 * coords->dx(ix, jy)); } @@ -187,7 +188,7 @@ FieldPerp LaplaceSerialBand::solve(const FieldPerp& b, const FieldPerp& x0) { // A first order derivative term (1/c)\nabla_perp c\cdot\nabla_\perp x if ((ix > 1) && (ix < (localmesh->LocalNx - 2))) { - coef4 += coords->g11(ix, jy) + coef4 += g.g11(ix, jy) * (Ccoef(ix - 2, jy) - 8. * Ccoef(ix - 1, jy) + 8. * Ccoef(ix + 1, jy) - Ccoef(ix + 2, jy)) / (12. * coords->dx(ix, jy) * (Ccoef(ix, jy))); @@ -214,9 +215,10 @@ FieldPerp LaplaceSerialBand::solve(const FieldPerp& b, const FieldPerp& x0) { int ix = 1; auto kwave = kwave_(ix, jy); - coef1 = coords->g11(ix, jy) / (SQ(coords->dx(ix, jy))); - coef2 = coords->g33(ix, jy); - coef3 = kwave * coords->g13(ix, jy) / (2. * coords->dx(ix, jy)); + Coordinates::MetricTensor g = coords->getContravariantMetricTensor(); + coef1 = g.g11(ix, jy) / (SQ(coords->dx(ix, jy))); + coef2 = g.g33(ix, jy); + coef3 = kwave * g.g13(ix, jy) / (2. * coords->dx(ix, jy)); // Multiply Delp2 component by a factor coef1 *= Dcoef(ix, jy); @@ -231,9 +233,9 @@ FieldPerp LaplaceSerialBand::solve(const FieldPerp& b, const FieldPerp& x0) { ix = ncx - 1; - coef1 = coords->g11(ix, jy) / (SQ(coords->dx(ix, jy))); - coef2 = coords->g33(ix, jy); - coef3 = kwave * coords->g13(ix, jy) / (2. * coords->dx(ix, jy)); + coef1 = g.g11(ix, jy) / (SQ(coords->dx(ix, jy))); + coef2 = g.g33(ix, jy); + coef3 = kwave * g.g13(ix, jy) / (2. * coords->dx(ix, jy)); A(ix, 0) = 0.0; A(ix, 1) = dcomplex(coef1, -coef3); @@ -337,11 +339,12 @@ FieldPerp LaplaceSerialBand::solve(const FieldPerp& b, const FieldPerp& x0) { int ix = 1; auto kwave = kwave_(ix, jy); - coef1 = coords->g11(ix, jy) / (12. * SQ(coords->dx(ix, jy))); + Coordinates::MetricTensor g = coords->getContravariantMetricTensor(); + coef1 = g.g11(ix, jy) / (12. * SQ(coords->dx(ix, jy))); - coef2 = coords->g33(ix, jy); + coef2 = g.g33(ix, jy); - coef3 = kwave * coords->g13(ix, jy) / (2. * coords->dx(ix, jy)); + coef3 = kwave * g.g13(ix, jy) / (2. * coords->dx(ix, jy)); coef4 = Acoef(ix, jy); @@ -349,16 +352,16 @@ FieldPerp LaplaceSerialBand::solve(const FieldPerp& b, const FieldPerp& x0) { A(1, 0) = 0.0; // Not used A(1, 1) = dcomplex( (14. - - SQ(coords->dx(0, jy) * kwave) * coords->g33(0, jy) / coords->g11(0, jy)) + - SQ(coords->dx(0, jy) * kwave) * g.g33(0, jy) / g.g11(0, jy)) * coef1, -coef3); A(1, 2) = dcomplex(-29. * coef1 - SQ(kwave) * coef2 + coef4, 0.0); A(1, 3) = dcomplex(16. * coef1, coef3); A(1, 4) = dcomplex(-coef1, 0.0); - coef1 = coords->g11(ix, jy) / (SQ(coords->dx(ix, jy))); - coef2 = coords->g33(ix, jy); - coef3 = kwave * coords->g13(ix, jy) / (2. * coords->dx(ix, jy)); + coef1 = g.g11(ix, jy) / (SQ(coords->dx(ix, jy))); + coef2 = g.g33(ix, jy); + coef3 = kwave * g.g13(ix, jy) / (2. * coords->dx(ix, jy)); // Use 2nd order at 1 A(0, 0) = 0.0; // Should never be used @@ -381,12 +384,13 @@ FieldPerp LaplaceSerialBand::solve(const FieldPerp& b, const FieldPerp& x0) { int ix = ncx - 1; - coef1 = coords->g11(ix, jy) / (12. * SQ(coords->dx(ix, jy))); + Coordinates::MetricTensor g = coords->getContravariantMetricTensor(); + coef1 = g.g11(ix, jy) / (12. * SQ(coords->dx(ix, jy))); - coef2 = coords->g33(ix, jy); + coef2 = g.g33(ix, jy); auto kwave = kwave_(ix, jy); - coef3 = kwave * coords->g13(ix, jy) / (2. * coords->dx(ix, jy)); + coef3 = kwave * g.g13(ix, jy) / (2. * coords->dx(ix, jy)); coef4 = Acoef(ix, jy); @@ -395,15 +399,15 @@ FieldPerp LaplaceSerialBand::solve(const FieldPerp& b, const FieldPerp& x0) { A(ix, 1) = dcomplex(16. * coef1, -coef3); A(ix, 2) = dcomplex(-29. * coef1 - SQ(kwave) * coef2 + coef4, 0.0); A(ix, 3) = dcomplex((14. - - SQ(coords->dx(ncx, jy) * kwave) * coords->g33(ncx, jy) - / coords->g11(ncx, jy)) + - SQ(coords->dx(ncx, jy) * kwave) * g.g33(ncx, jy) + / g.g11(ncx, jy)) * coef1, coef3); A(ix, 4) = 0.0; // Not used - coef1 = coords->g11(ix, jy) / (SQ(coords->dx(ix, jy))); - coef2 = coords->g33(ix, jy); - coef3 = kwave * coords->g13(ix, jy) / (2. * coords->dx(ix, jy)); + coef1 = g.g11(ix, jy) / (SQ(coords->dx(ix, jy))); + coef2 = g.g33(ix, jy); + coef3 = kwave * g.g13(ix, jy) / (2. * coords->dx(ix, jy)); // Use 2nd order at ncx - 1 A(ncx, 0) = dcomplex(coef1, -coef3); diff --git a/src/invert/laplace/invert_laplace.cxx b/src/invert/laplace/invert_laplace.cxx index 505b04cc4f..c22b6947b1 100644 --- a/src/invert/laplace/invert_laplace.cxx +++ b/src/invert/laplace/invert_laplace.cxx @@ -324,9 +324,11 @@ void Laplacian::tridagCoefs(int jx, int jy, BoutReal kwave, dcomplex& a, dcomple BoutReal coef1, coef2, coef3, coef4, coef5; - coef1 = localcoords->g11(jx, jy); ///< X 2nd derivative coefficient - coef2 = localcoords->g33(jx, jy); ///< Z 2nd derivative coefficient - coef3 = 2. * localcoords->g13(jx, jy); ///< X-Z mixed derivative coefficient + Coordinates::MetricTensor g = localcoords->getContravariantMetricTensor(); + + coef1 = g.g11(jx, jy); ///< X 2nd derivative coefficient + coef2 = g.g33(jx, jy); ///< Z 2nd derivative coefficient + coef3 = 2. * g.g13(jx, jy); ///< X-Z mixed derivative coefficient coef4 = 0.0; coef5 = 0.0; @@ -360,14 +362,14 @@ void Laplacian::tridagCoefs(int jx, int jy, BoutReal kwave, dcomplex& a, dcomple if ((jx > 0) && (jx < (localmesh->LocalNx - 1))) { BoutReal dc2dx_over_c1 = ((*c2coef)(jx + 1, jy) - (*c2coef)(jx - 1, jy)) / (2. * localcoords->dx(jx, jy) * ((*c1coef)(jx, jy))); - coef4 += localcoords->g11(jx, jy) * dc2dx_over_c1; - coef5 += localcoords->g13(jx, jy) * dc2dx_over_c1; + coef4 += g.g11(jx, jy) * dc2dx_over_c1; + coef5 += g.g13(jx, jy) * dc2dx_over_c1; } } if (localmesh->IncIntShear) { // d2dz2 term - coef2 += localcoords->g11(jx, jy) * localcoords->IntShiftTorsion(jx, jy) + coef2 += g.g11(jx, jy) * localcoords->IntShiftTorsion(jx, jy) * localcoords->IntShiftTorsion(jx, jy); // Mixed derivative coef3 = 0.0; // This cancels out @@ -492,6 +494,7 @@ void Laplacian::tridagMatrix(dcomplex* avec, dcomplex* bvec, dcomplex* cvec, dco // Set the boundary conditions if x is not periodic if (!localmesh->periodicX) { + Coordinates::MetricTensor g = coords->getContravariantMetricTensor(); if (localmesh->firstX()) { // INNER BOUNDARY ON THIS PROCESSOR @@ -546,7 +549,7 @@ void Laplacian::tridagMatrix(dcomplex* avec, dcomplex* bvec, dcomplex* cvec, dco for (int ix = 0; ix < inbndry; ix++) { avec[ix] = 0.; bvec[ix] = 1.; - cvec[ix] = -exp(-k * coords->dx(ix, jy) / sqrt(coords->g11(ix, jy))); + cvec[ix] = -exp(-k * coords->dx(ix, jy) / sqrt(g.g11(ix, jy))); } } else if (inner_boundary_flags & INVERT_IN_CYLINDER) { // Condition for inner radial boundary for cylindrical coordinates @@ -623,7 +626,7 @@ void Laplacian::tridagMatrix(dcomplex* avec, dcomplex* bvec, dcomplex* cvec, dco for (int ix = 0; ix < inbndry; ix++) { avec[ix] = 0.0; bvec[ix] = 1.0; - cvec[ix] = -exp(-1.0 * sqrt(coords->g33(ix, jy) / coords->g11(ix, jy)) * kwave + cvec[ix] = -exp(-1.0 * sqrt(g.g33(ix, jy) / g.g11(ix, jy)) * kwave * coords->dx(ix, jy)); } } else if (inner_boundary_flags & INVERT_IN_CYLINDER) { @@ -707,7 +710,7 @@ void Laplacian::tridagMatrix(dcomplex* avec, dcomplex* bvec, dcomplex* cvec, dco cvec[ncx - ix] = 0.; bvec[ncx - ix] = 1.; avec[ncx - ix] = - -exp(-k * coords->dx(ncx - ix, jy) / sqrt(coords->g11(ncx - ix, jy))); + -exp(-k * coords->dx(ncx - ix, jy) / sqrt(g.g11(ncx - ix, jy))); } } else { // Order 2 dirichlet BC (boundary half between points) @@ -743,7 +746,7 @@ void Laplacian::tridagMatrix(dcomplex* avec, dcomplex* bvec, dcomplex* cvec, dco // Use decaying zero-Laplacian solution in the boundary for (int ix = 0; ix < outbndry; ix++) { avec[ncx - ix] = - -exp(-1.0 * sqrt(coords->g33(xe - ix, jy) / coords->g11(xe - ix, jy)) + -exp(-1.0 * sqrt(g.g33(xe - ix, jy) / g.g11(xe - ix, jy)) * kwave * coords->dx(xe - ix, jy)); bvec[ncx - ix] = 1.0; cvec[ncx - ix] = 0.0; diff --git a/src/invert/laplacexz/impls/cyclic/laplacexz-cyclic.cxx b/src/invert/laplacexz/impls/cyclic/laplacexz-cyclic.cxx index fbe22241d8..97a0a72c16 100644 --- a/src/invert/laplacexz/impls/cyclic/laplacexz-cyclic.cxx +++ b/src/invert/laplacexz/impls/cyclic/laplacexz-cyclic.cxx @@ -72,9 +72,10 @@ void LaplaceXZcyclic::setCoefs(const Field2D& A2D, const Field2D& B2D) { // Set coefficients Coordinates* coord = localmesh->getCoordinates(location); + Coordinates::MetricTensor g = coord->getContravariantMetricTensor(); // NOTE: For now the X-Z terms are omitted, so check that they are small - ASSERT2(max(abs(coord->g13)) < 1e-5); + ASSERT2(max(abs(g.g13)) < 1e-5); int ind = 0; const BoutReal zlength = getUniform(coord->zlength()); @@ -117,7 +118,7 @@ void LaplaceXZcyclic::setCoefs(const Field2D& A2D, const Field2D& B2D) { // Metrics on x+1/2 boundary BoutReal J = 0.5 * (coord->J(x, y) + coord->J(x + 1, y)); - BoutReal g11 = 0.5 * (coord->g11(x, y) + coord->g11(x + 1, y)); + BoutReal g11 = 0.5 * (g.g11(x, y) + g.g11(x + 1, y)); BoutReal dx = 0.5 * (coord->dx(x, y) + coord->dx(x + 1, y)); BoutReal A = 0.5 * (A2D(x, y) + A2D(x + 1, y)); @@ -128,7 +129,7 @@ void LaplaceXZcyclic::setCoefs(const Field2D& A2D, const Field2D& B2D) { // Metrics on x-1/2 boundary J = 0.5 * (coord->J(x, y) + coord->J(x - 1, y)); - g11 = 0.5 * (coord->g11(x, y) + coord->g11(x - 1, y)); + g11 = 0.5 * (g.g11(x, y) + g.g11(x - 1, y)); dx = 0.5 * (coord->dx(x, y) + coord->dx(x - 1, y)); A = 0.5 * (A2D(x, y) + A2D(x - 1, y)); @@ -137,7 +138,7 @@ void LaplaceXZcyclic::setCoefs(const Field2D& A2D, const Field2D& B2D) { bcoef(ind, x - xstart) -= val; // ZZ component - bcoef(ind, x - xstart) -= A2D(x, y) * SQ(kwave) * coord->g33(x, y); + bcoef(ind, x - xstart) -= A2D(x, y) * SQ(kwave) * g.g33(x, y); } // Outer X boundary diff --git a/src/mesh/boundary_standard.cxx b/src/mesh/boundary_standard.cxx index 80c2053f39..5618af0af9 100644 --- a/src/mesh/boundary_standard.cxx +++ b/src/mesh/boundary_standard.cxx @@ -1602,15 +1602,16 @@ void BoundaryNeumann_NonOrthogonal::apply(Field2D& f) { // Calculate derivatives for metric use mesh->communicate(f); Field2D dfdy = DDY(f); + Coordinates::MetricTensor g = metric->getContravariantMetricTensor(); // Loop over all elements and set equal to the next point in for (bndry->first(); !bndry->isDone(); bndry->next1d()) { // Interpolate (linearly) metrics to halfway between last cell and boundary cell BoutReal g11shift = 0.5 - * (metric->g11(bndry->x, bndry->y) + metric->g11(bndry->x - bndry->bx, bndry->y)); + * (g.g11(bndry->x, bndry->y) + g.g11(bndry->x - bndry->bx, bndry->y)); BoutReal g12shift = 0.5 - * (metric->g12(bndry->x, bndry->y) + metric->g12(bndry->x - bndry->bx, bndry->y)); + * (g.g12(bndry->x, bndry->y) + g.g12(bndry->x - bndry->bx, bndry->y)); // Have to use derivatives at last gridpoint instead of derivatives on boundary layer // because derivative values don't exist in boundary region // NOTE: should be fixed to interpolate to boundary line @@ -1657,6 +1658,7 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { mesh->communicate(f); Field3D dfdy = DDY(f); Field3D dfdz = DDZ(f); + Coordinates::MetricTensor g = metric->getContravariantMetricTensor(); // Loop over all elements and set equal to the next point in for (bndry->first(); !bndry->isDone(); bndry->next1d()) { #if BOUT_USE_METRIC_3D @@ -1666,14 +1668,14 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { #endif // Interpolate (linearly) metrics to halfway between last cell and boundary cell BoutReal g11shift = 0.5 - * (metric->g11(bndry->x, bndry->y, z) - + metric->g11(bndry->x - bndry->bx, bndry->y, z)); + * (g.g11(bndry->x, bndry->y, z) + + g.g11(bndry->x - bndry->bx, bndry->y, z)); BoutReal g12shift = 0.5 - * (metric->g12(bndry->x, bndry->y, z) - + metric->g12(bndry->x - bndry->bx, bndry->y, z)); + * (g.g12(bndry->x, bndry->y, z) + + g.g12(bndry->x - bndry->bx, bndry->y, z)); BoutReal g13shift = 0.5 - * (metric->g13(bndry->x, bndry->y, z) - + metric->g13(bndry->x - bndry->bx, bndry->y, z)); + * (g.g13(bndry->x, bndry->y, z) + + g.g13(bndry->x - bndry->bx, bndry->y, z)); // Have to use derivatives at last gridpoint instead of derivatives on boundary // layer // because derivative values don't exist in boundary region @@ -2643,6 +2645,7 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { } int bx = bndry->bx; + Coordinates::MetricTensor g = metric->getContravariantMetricTensor(); // Loop over the Y dimension for (bndry->first(); !bndry->isDone(); bndry->nextY()) { // bndry->(x,y) is the first point in the boundary @@ -2666,7 +2669,7 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { // kz != 0 solution BoutReal coef = - -1.0 * sqrt(metric->g33(x, y) / metric->g11(x, y)) * metric->dx(x, y); + -1.0 * sqrt(g.g33(x, y) / g.g11(x, y)) * metric->dx(x, y); for (int jz = 1; jz <= ncz / 2; jz++) { BoutReal kwave = jz * 2.0 * PI / metric->zlength()(x, y); // wavenumber in [rad^-1] @@ -2849,6 +2852,7 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { Array c0(ncz / 2 + 1), c1(ncz / 2 + 1), c2(ncz / 2 + 1); int bx = bndry->bx; + Coordinates::MetricTensor g = metric->getContravariantMetricTensor(); // Loop over the Y dimension for (bndry->first(); !bndry->isDone(); bndry->nextY()) { int x = bndry->x; @@ -2877,16 +2881,16 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { do { // kz = 0 solution xpos -= metric->dx(x, y); - c2[0] = c0[0] + k0lin * xpos + 0.5 * c1[0] * xpos * xpos / metric->g11(x - bx, y); + c2[0] = c0[0] + k0lin * xpos + 0.5 * c1[0] * xpos * xpos / g.g11(x - bx, y); // kz != 0 solution - BoutReal coef = -1.0 * sqrt(metric->g33(x - bx, y) / metric->g11(x - bx, y)) + BoutReal coef = -1.0 * sqrt(g.g33(x - bx, y) / g.g11(x - bx, y)) * metric->dx(x - bx, y); for (int jz = 1; jz <= ncz / 2; jz++) { BoutReal kwave = jz * 2.0 * PI / getUniform(metric->zlength()); // wavenumber in [rad^-1] c0[jz] *= exp(coef * kwave); // The decaying solution only // Add the particular solution - c2[jz] = c0[jz] - c1[jz] / (metric->g33(x - bx, y) * kwave * kwave); + c2[jz] = c0[jz] - c1[jz] / (g.g33(x - bx, y) * kwave * kwave); } // Reverse FFT irfft(c2.begin(), ncz, f(x, y)); @@ -2939,6 +2943,7 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { } jx = mesh->xend + 1; + Coordinates::MetricTensor g = metric->getContravariantMetricTensor(); for (jy = 1; jy < mesh->LocalNy - 1; jy++) { for (jz = 0; jz < ncz; jz++) { jzp = (jz + 1) % ncz; @@ -2972,42 +2977,42 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { // - d/dy( JB^y ) - d/dz( JB^z ) tmp = - -(metric->J(jx, jy) * metric->g12(jx, jy) * var.y(jx, jy, jz) - + metric->J(jx, jy) * metric->g13(jx, jy) * var.z(jx, jy, jz) - - metric->J(jx - 2, jy) * metric->g12(jx - 2, jy) * var.y(jx - 2, jy, jz) - + metric->J(jx - 2, jy) * metric->g13(jx - 2, jy) * var.z(jx - 2, jy, jz)) + -(metric->J(jx, jy) * g.g12(jx, jy) * var.y(jx, jy, jz) + + metric->J(jx, jy) * g.g13(jx, jy) * var.z(jx, jy, jz) + - metric->J(jx - 2, jy) * g.g12(jx - 2, jy) * var.y(jx - 2, jy, jz) + + metric->J(jx - 2, jy) * g.g13(jx - 2, jy) * var.z(jx - 2, jy, jz)) / (metric->dx(jx - 2, jy) + metric->dx(jx - 1, jy)); // First term (d/dx) using vals calculated above - tmp -= (metric->J(jx - 1, jy + 1) * metric->g12(jx - 1, jy + 1) + tmp -= (metric->J(jx - 1, jy + 1) * g.g12(jx - 1, jy + 1) * var.x(jx - 1, jy + 1, jz) - - metric->J(jx - 1, jy - 1) * metric->g12(jx - 1, jy - 1) + - metric->J(jx - 1, jy - 1) * g.g12(jx - 1, jy - 1) * var.x(jx - 1, jy - 1, jz) - + metric->J(jx - 1, jy + 1) * metric->g22(jx - 1, jy + 1) + + metric->J(jx - 1, jy + 1) * g.g22(jx - 1, jy + 1) * var.y(jx - 1, jy + 1, jz) - - metric->J(jx - 1, jy - 1) * metric->g22(jx - 1, jy - 1) + - metric->J(jx - 1, jy - 1) * g.g22(jx - 1, jy - 1) * var.y(jx - 1, jy - 1, jz) - + metric->J(jx - 1, jy + 1) * metric->g23(jx - 1, jy + 1) + + metric->J(jx - 1, jy + 1) * g.g23(jx - 1, jy + 1) * var.z(jx - 1, jy + 1, jz) - - metric->J(jx - 1, jy - 1) * metric->g23(jx - 1, jy - 1) + - metric->J(jx - 1, jy - 1) * g.g23(jx - 1, jy - 1) * var.z(jx - 1, jy - 1, jz)) / (metric->dy(jx - 1, jy - 1) + metric->dy(jx - 1, jy)); // second (d/dy) - tmp -= (metric->J(jx - 1, jy) * metric->g13(jx - 1, jy) + tmp -= (metric->J(jx - 1, jy) * g.g13(jx - 1, jy) * (var.x(jx - 1, jy, jzp) - var.x(jx - 1, jy, jzm)) - + metric->J(jx - 1, jy) * metric->g23(jx - 1, jy) + + metric->J(jx - 1, jy) * g.g23(jx - 1, jy) * (var.y(jx - 1, jy, jzp) - var.y(jx - 1, jy, jzm)) - + metric->J(jx - 1, jy) * metric->g33(jx - 1, jy) + + metric->J(jx - 1, jy) * g.g33(jx - 1, jy) * (var.z(jx - 1, jy, jzp) - var.z(jx - 1, jy, jzm))) / (2. * metric->dz(jx - 1, jy)); var.x(jx, jy, jz) = - (metric->J(jx - 2, jy) * metric->g11(jx - 2, jy) * var.x(jx - 2, jy, jz) + (metric->J(jx - 2, jy) * g.g11(jx - 2, jy) * var.x(jx - 2, jy, jz) + (metric->dx(jx - 2, jy) + metric->dx(jx - 1, jy)) * tmp) - / metric->J(jx, jy) * metric->g11(jx, jy); + / metric->J(jx, jy) * g.g11(jx, jy); if (mesh->xstart == 2) { var.x(jx + 1, jy, jz) = - (metric->J(jx - 3, jy) * metric->g11(jx - 3, jy) * var.x(jx - 3, jy, jz) + (metric->J(jx - 3, jy) * g.g11(jx - 3, jy) * var.x(jx - 3, jy, jz) + 4. * metric->dx(jx, jy) * tmp) - / metric->J(jx + 1, jy) * metric->g11(jx + 1, jy); + / metric->J(jx + 1, jy) * g.g11(jx + 1, jy); } } } diff --git a/src/mesh/fv_ops.cxx b/src/mesh/fv_ops.cxx index 0a5d5f9624..c4d6b22281 100644 --- a/src/mesh/fv_ops.cxx +++ b/src/mesh/fv_ops.cxx @@ -31,6 +31,7 @@ Field3D Div_a_Grad_perp(const Field3D& a, const Field3D& f) { Field3D result{zeroFrom(f)}; Coordinates* coord = f.getCoordinates(); + Coordinates::MetricTensor g = coord->getContravariantMetricTensor(); // Flux in x @@ -52,8 +53,8 @@ Field3D Div_a_Grad_perp(const Field3D& a, const Field3D& f) { // Calculate flux from i to i+1 BoutReal fout = 0.5 * (a(i, j, k) + a(i + 1, j, k)) - * (coord->J(i, j, k) * coord->g11(i, j, k) - + coord->J(i + 1, j, k) * coord->g11(i + 1, j, k)) + * (coord->J(i, j, k) * g.g11(i, j, k) + + coord->J(i + 1, j, k) * g.g11(i + 1, j, k)) * (f(i + 1, j, k) - f(i, j, k)) / (coord->dx(i, j, k) + coord->dx(i + 1, j, k)); @@ -69,7 +70,7 @@ Field3D Div_a_Grad_perp(const Field3D& a, const Field3D& f) { // 3D Metric, need yup/ydown fields. // Requires previous communication of metrics // -- should insert communication here? - if (!coord->g23.hasParallelSlices() || !coord->g_23.hasParallelSlices() + if (!g.g23.hasParallelSlices() || !coord->g_23.hasParallelSlices() || !coord->dy.hasParallelSlices() || !coord->dz.hasParallelSlices() || !coord->Bxy.hasParallelSlices() || !coord->J.hasParallelSlices()) { throw BoutException("metrics have no yup/down: Maybe communicate in init?"); @@ -87,7 +88,7 @@ Field3D Div_a_Grad_perp(const Field3D& a, const Field3D& f) { // Only in 3D case with FCI do the metrics have parallel slices const bool metric_fci = fci and bout::build::use_metric_3d; - const auto g23 = makeslices(metric_fci, coord->g23); + const auto g23 = makeslices(metric_fci, g.g23); const auto g_23 = makeslices(metric_fci, coord->g_23); const auto J = makeslices(metric_fci, coord->J); const auto dy = makeslices(metric_fci, coord->dy); @@ -156,7 +157,7 @@ Field3D Div_a_Grad_perp(const Field3D& a, const Field3D& f) { const BoutReal fout = 0.25 * (a_slice.c[i] + a_slice.c[ikp]) - * (J.c[i] * coord->g33[i] + J.c[ikp] * coord->g33[ikp]) + * (J.c[i] * g.g33[i] + J.c[ikp] * g.g33[ikp]) * ( // df/dz (f_slice.c[ikp] - f_slice.c[i]) / dz.c[i] // - g_yz * df/dy / SQ(J*B) @@ -498,6 +499,7 @@ Field3D Div_Perp_Lap(const Field3D& a, const Field3D& f, CELL_LOC outloc) { // Coordinates* coords = a.getCoordinates(outloc); Mesh* mesh = f.getMesh(); + Coordinates::MetricTensor g = coords->getContravariantMetricTensor(); for (int i = mesh->xstart; i <= mesh->xend; i++) { for (int j = mesh->ystart; j <= mesh->yend; j++) { @@ -510,32 +512,32 @@ Field3D Div_Perp_Lap(const Field3D& a, const Field3D& f, CELL_LOC outloc) { // Calculate gradients on cell faces -- assumes constant grid spacing BoutReal gR = - (coords->g11(i, j, k) + coords->g11(i + 1, j, k)) + (g.g11(i, j, k) + g.g11(i + 1, j, k)) * (f(i + 1, j, k) - f(i, j, k)) / (coords->dx(i + 1, j, k) + coords->dx(i, j, k)) - + 0.5 * (coords->g13(i, j, k) + coords->g13(i + 1, j, k)) + + 0.5 * (g.g13(i, j, k) + g.g13(i + 1, j, k)) * (f(i + 1, j, kp) - f(i + 1, j, km) + f(i, j, kp) - f(i, j, km)) / (4. * coords->dz(i, j, k)); BoutReal gL = - (coords->g11(i - 1, j, k) + coords->g11(i, j, k)) + (g.g11(i - 1, j, k) + g.g11(i, j, k)) * (f(i, j, k) - f(i - 1, j, k)) / (coords->dx(i - 1, j, k) + coords->dx(i, j, k)) - + 0.5 * (coords->g13(i - 1, j, k) + coords->g13(i, j, k)) + + 0.5 * (g.g13(i - 1, j, k) + g.g13(i, j, k)) * (f(i - 1, j, kp) - f(i - 1, j, km) + f(i, j, kp) - f(i, j, km)) / (4 * coords->dz(i, j, k)); BoutReal gD = - coords->g13(i, j, k) + g.g13(i, j, k) * (f(i + 1, j, km) - f(i - 1, j, km) + f(i + 1, j, k) - f(i - 1, j, k)) / (4. * coords->dx(i, j, k)) - + coords->g33(i, j, k) * (f(i, j, k) - f(i, j, km)) / coords->dz(i, j, k); + + g.g33(i, j, k) * (f(i, j, k) - f(i, j, km)) / coords->dz(i, j, k); BoutReal gU = - coords->g13(i, j, k) + g.g13(i, j, k) * (f(i + 1, j, kp) - f(i - 1, j, kp) + f(i + 1, j, k) - f(i - 1, j, k)) / (4. * coords->dx(i, j, k)) - + coords->g33(i, j, k) * (f(i, j, kp) - f(i, j, k)) / coords->dz(i, j, k); + + g.g33(i, j, k) * (f(i, j, kp) - f(i, j, k)) / coords->dz(i, j, k); // Flow right BoutReal flux = gR * 0.25 * (coords->J(i + 1, j, k) + coords->J(i, j, k)) diff --git a/tests/unit/invert/laplace/test_laplace_cyclic.cxx b/tests/unit/invert/laplace/test_laplace_cyclic.cxx index 252c1f21c9..f25a23f897 100644 --- a/tests/unit/invert/laplace/test_laplace_cyclic.cxx +++ b/tests/unit/invert/laplace/test_laplace_cyclic.cxx @@ -39,8 +39,9 @@ class CyclicForwardOperator { } const Field3D operator()(Field3D& f) { + Coordinates::MetricTensor g = coords->getContravariantMetricTensor(); auto result = d * Delp2(f) - + (coords->g11 * DDX(f) + coords->g13 * DDZ(f)) * DDX(c2) / c1 + a * f + + (g.g11 * DDX(f) + g.g13 * DDZ(f)) * DDX(c2) / c1 + a * f + ex * DDX(f) + ez * DDZ(f); applyBoundaries(result, f); return result; diff --git a/tests/unit/mesh/data/test_gridfromoptions.cxx b/tests/unit/mesh/data/test_gridfromoptions.cxx index 84f08ff47b..00293f4e4f 100644 --- a/tests/unit/mesh/data/test_gridfromoptions.cxx +++ b/tests/unit/mesh/data/test_gridfromoptions.cxx @@ -359,12 +359,14 @@ TEST_F(GridFromOptionsTest, CoordinatesCentre) { mesh_from_options.communicate(expected_2d); - EXPECT_TRUE(IsFieldEqual(coords->g11, expected_metric + 5.)); - EXPECT_TRUE(IsFieldEqual(coords->g22, expected_metric + 4.)); - EXPECT_TRUE(IsFieldEqual(coords->g33, expected_metric + 3.)); - EXPECT_TRUE(IsFieldEqual(coords->g12, expected_metric + 2.)); - EXPECT_TRUE(IsFieldEqual(coords->g13, expected_metric + 1.)); - EXPECT_TRUE(IsFieldEqual(coords->g23, expected_metric)); + Coordinates::MetricTensor g = coords->getContravariantMetricTensor(); + + EXPECT_TRUE(IsFieldEqual(g.g11, expected_metric + 5.)); + EXPECT_TRUE(IsFieldEqual(g.g22, expected_metric + 4.)); + EXPECT_TRUE(IsFieldEqual(g.g33, expected_metric + 3.)); + EXPECT_TRUE(IsFieldEqual(g.g12, expected_metric + 2.)); + EXPECT_TRUE(IsFieldEqual(g.g13, expected_metric + 1.)); + EXPECT_TRUE(IsFieldEqual(g.g23, expected_metric)); } #if not(BOUT_USE_METRIC_3D) @@ -373,12 +375,14 @@ TEST_F(GridFromOptionsTest, CoordinatesZlow) { mesh_from_options.communicate(expected_2d); - EXPECT_TRUE(IsFieldEqual(coords->g11, expected_metric + 5.)); - EXPECT_TRUE(IsFieldEqual(coords->g22, expected_metric + 4.)); - EXPECT_TRUE(IsFieldEqual(coords->g33, expected_metric + 3.)); - EXPECT_TRUE(IsFieldEqual(coords->g12, expected_metric + 2.)); - EXPECT_TRUE(IsFieldEqual(coords->g13, expected_metric + 1.)); - EXPECT_TRUE(IsFieldEqual(coords->g23, expected_metric)); + Coordinates::MetricTensor g = coords->getContravariantMetricTensor(); + + EXPECT_TRUE(IsFieldEqual(g.g11, expected_metric + 5.)); + EXPECT_TRUE(IsFieldEqual(g.g22, expected_metric + 4.)); + EXPECT_TRUE(IsFieldEqual(g.g33, expected_metric + 3.)); + EXPECT_TRUE(IsFieldEqual(g.g12, expected_metric + 2.)); + EXPECT_TRUE(IsFieldEqual(g.g13, expected_metric + 1.)); + EXPECT_TRUE(IsFieldEqual(g.g23, expected_metric)); } #else // Maybe replace by MMS test, because we need a periodic function in z. @@ -401,17 +405,19 @@ TEST_F(GridFromOptionsTest, CoordinatesXlowInterp) { mesh_from_options.communicate(expected_xlow); + Coordinates::MetricTensor g = coords->getContravariantMetricTensor(); + EXPECT_TRUE( - IsFieldEqual(coords->g11, expected_xlow + 5., "RGN_NOBNDRY", this_tolerance)); + IsFieldEqual(g.g11, expected_xlow + 5., "RGN_NOBNDRY", this_tolerance)); EXPECT_TRUE( - IsFieldEqual(coords->g22, expected_xlow + 4., "RGN_NOBNDRY", this_tolerance)); + IsFieldEqual(g.g22, expected_xlow + 4., "RGN_NOBNDRY", this_tolerance)); EXPECT_TRUE( - IsFieldEqual(coords->g33, expected_xlow + 3., "RGN_NOBNDRY", this_tolerance)); + IsFieldEqual(g.g33, expected_xlow + 3., "RGN_NOBNDRY", this_tolerance)); EXPECT_TRUE( - IsFieldEqual(coords->g12, expected_xlow + 2., "RGN_NOBNDRY", this_tolerance)); + IsFieldEqual(g.g12, expected_xlow + 2., "RGN_NOBNDRY", this_tolerance)); EXPECT_TRUE( - IsFieldEqual(coords->g13, expected_xlow + 1., "RGN_NOBNDRY", this_tolerance)); - EXPECT_TRUE(IsFieldEqual(coords->g23, expected_xlow, "RGN_NOBNDRY", this_tolerance)); + IsFieldEqual(g.g13, expected_xlow + 1., "RGN_NOBNDRY", this_tolerance)); + EXPECT_TRUE(IsFieldEqual(g.g23, expected_xlow, "RGN_NOBNDRY", this_tolerance)); } TEST_F(GridFromOptionsTest, CoordinatesXlowRead) { @@ -443,18 +449,20 @@ TEST_F(GridFromOptionsTest, CoordinatesXlowRead) { mesh_from_options.communicate(expected_xlow); - EXPECT_TRUE(IsFieldEqual(coords->g11, expected_xlow + 5.)); - EXPECT_TRUE(coords->g11.getLocation() == CELL_XLOW); - EXPECT_TRUE(IsFieldEqual(coords->g22, expected_xlow + 4.)); - EXPECT_TRUE(coords->g22.getLocation() == CELL_XLOW); - EXPECT_TRUE(IsFieldEqual(coords->g33, expected_xlow + 3.)); - EXPECT_TRUE(coords->g33.getLocation() == CELL_XLOW); - EXPECT_TRUE(IsFieldEqual(coords->g12, expected_xlow + 2.)); - EXPECT_TRUE(coords->g12.getLocation() == CELL_XLOW); - EXPECT_TRUE(IsFieldEqual(coords->g13, expected_xlow + 1.)); - EXPECT_TRUE(coords->g13.getLocation() == CELL_XLOW); - EXPECT_TRUE(IsFieldEqual(coords->g23, expected_xlow)); - EXPECT_TRUE(coords->g23.getLocation() == CELL_XLOW); + Coordinates::MetricTensor g = coords->getContravariantMetricTensor(); + + EXPECT_TRUE(IsFieldEqual(g.g11, expected_xlow + 5.)); + EXPECT_TRUE(g.g11.getLocation() == CELL_XLOW); + EXPECT_TRUE(IsFieldEqual(g.g22, expected_xlow + 4.)); + EXPECT_TRUE(g.g22.getLocation() == CELL_XLOW); + EXPECT_TRUE(IsFieldEqual(g.g33, expected_xlow + 3.)); + EXPECT_TRUE(g.g33.getLocation() == CELL_XLOW); + EXPECT_TRUE(IsFieldEqual(g.g12, expected_xlow + 2.)); + EXPECT_TRUE(g.g12.getLocation() == CELL_XLOW); + EXPECT_TRUE(IsFieldEqual(g.g13, expected_xlow + 1.)); + EXPECT_TRUE(g.g13.getLocation() == CELL_XLOW); + EXPECT_TRUE(IsFieldEqual(g.g23, expected_xlow)); + EXPECT_TRUE(g.g23.getLocation() == CELL_XLOW); } TEST_F(GridFromOptionsTest, CoordinatesYlowInterp) { @@ -475,23 +483,25 @@ TEST_F(GridFromOptionsTest, CoordinatesYlowInterp) { mesh_from_options.communicate(expected_ylow); + Coordinates::MetricTensor g = coords->getContravariantMetricTensor(); + EXPECT_TRUE( - IsFieldEqual(coords->g11, expected_ylow + 5., "RGN_NOBNDRY", this_tolerance)); - EXPECT_TRUE(coords->g11.getLocation() == CELL_YLOW); + IsFieldEqual(g.g11, expected_ylow + 5., "RGN_NOBNDRY", this_tolerance)); + EXPECT_TRUE(g.g11.getLocation() == CELL_YLOW); EXPECT_TRUE( - IsFieldEqual(coords->g22, expected_ylow + 4., "RGN_NOBNDRY", this_tolerance)); - EXPECT_TRUE(coords->g22.getLocation() == CELL_YLOW); + IsFieldEqual(g.g22, expected_ylow + 4., "RGN_NOBNDRY", this_tolerance)); + EXPECT_TRUE(g.g22.getLocation() == CELL_YLOW); EXPECT_TRUE( - IsFieldEqual(coords->g33, expected_ylow + 3., "RGN_NOBNDRY", this_tolerance)); - EXPECT_TRUE(coords->g33.getLocation() == CELL_YLOW); + IsFieldEqual(g.g33, expected_ylow + 3., "RGN_NOBNDRY", this_tolerance)); + EXPECT_TRUE(g.g33.getLocation() == CELL_YLOW); EXPECT_TRUE( - IsFieldEqual(coords->g12, expected_ylow + 2., "RGN_NOBNDRY", this_tolerance)); - EXPECT_TRUE(coords->g12.getLocation() == CELL_YLOW); + IsFieldEqual(g.g12, expected_ylow + 2., "RGN_NOBNDRY", this_tolerance)); + EXPECT_TRUE(g.g12.getLocation() == CELL_YLOW); EXPECT_TRUE( - IsFieldEqual(coords->g13, expected_ylow + 1., "RGN_NOBNDRY", this_tolerance)); - EXPECT_TRUE(coords->g13.getLocation() == CELL_YLOW); - EXPECT_TRUE(IsFieldEqual(coords->g23, expected_ylow, "RGN_NOBNDRY", this_tolerance)); - EXPECT_TRUE(coords->g23.getLocation() == CELL_YLOW); + IsFieldEqual(g.g13, expected_ylow + 1., "RGN_NOBNDRY", this_tolerance)); + EXPECT_TRUE(g.g13.getLocation() == CELL_YLOW); + EXPECT_TRUE(IsFieldEqual(g.g23, expected_ylow, "RGN_NOBNDRY", this_tolerance)); + EXPECT_TRUE(g.g23.getLocation() == CELL_YLOW); #endif } @@ -526,18 +536,20 @@ TEST_F(GridFromOptionsTest, CoordinatesYlowRead) { mesh_from_options.communicate(expected_ylow); - EXPECT_TRUE(IsFieldEqual(coords->g11, expected_ylow + 5., "RGN_ALL", this_tolerance)); - EXPECT_TRUE(coords->g11.getLocation() == CELL_YLOW); - EXPECT_TRUE(IsFieldEqual(coords->g22, expected_ylow + 4., "RGN_ALL", this_tolerance)); - EXPECT_TRUE(coords->g22.getLocation() == CELL_YLOW); - EXPECT_TRUE(IsFieldEqual(coords->g33, expected_ylow + 3., "RGN_ALL", this_tolerance)); - EXPECT_TRUE(coords->g33.getLocation() == CELL_YLOW); - EXPECT_TRUE(IsFieldEqual(coords->g12, expected_ylow + 2., "RGN_ALL", this_tolerance)); - EXPECT_TRUE(coords->g12.getLocation() == CELL_YLOW); - EXPECT_TRUE(IsFieldEqual(coords->g13, expected_ylow + 1., "RGN_ALL", this_tolerance)); - EXPECT_TRUE(coords->g13.getLocation() == CELL_YLOW); - EXPECT_TRUE(IsFieldEqual(coords->g23, expected_ylow, "RGN_ALL", this_tolerance)); - EXPECT_TRUE(coords->g23.getLocation() == CELL_YLOW); + Coordinates::MetricTensor g = coords->getContravariantMetricTensor(); + + EXPECT_TRUE(IsFieldEqual(g.g11, expected_ylow + 5., "RGN_ALL", this_tolerance)); + EXPECT_TRUE(g.g11.getLocation() == CELL_YLOW); + EXPECT_TRUE(IsFieldEqual(g.g22, expected_ylow + 4., "RGN_ALL", this_tolerance)); + EXPECT_TRUE(g.g22.getLocation() == CELL_YLOW); + EXPECT_TRUE(IsFieldEqual(g.g33, expected_ylow + 3., "RGN_ALL", this_tolerance)); + EXPECT_TRUE(g.g33.getLocation() == CELL_YLOW); + EXPECT_TRUE(IsFieldEqual(g.g12, expected_ylow + 2., "RGN_ALL", this_tolerance)); + EXPECT_TRUE(g.g12.getLocation() == CELL_YLOW); + EXPECT_TRUE(IsFieldEqual(g.g13, expected_ylow + 1., "RGN_ALL", this_tolerance)); + EXPECT_TRUE(g.g13.getLocation() == CELL_YLOW); + EXPECT_TRUE(IsFieldEqual(g.g23, expected_ylow, "RGN_ALL", this_tolerance)); + EXPECT_TRUE(g.g23.getLocation() == CELL_YLOW); #endif } @@ -548,11 +560,13 @@ TEST_F(GridFromOptionsTest, CoordinatesZlowRead) { auto coords = mesh_from_options.getCoordinates(CELL_ZLOW); - EXPECT_TRUE(IsFieldEqual(coords->g11, expected_2d + 5.)); - EXPECT_TRUE(IsFieldEqual(coords->g22, expected_2d + 4.)); - EXPECT_TRUE(IsFieldEqual(coords->g33, expected_2d + 3.)); - EXPECT_TRUE(IsFieldEqual(coords->g12, expected_2d + 2.)); - EXPECT_TRUE(IsFieldEqual(coords->g13, expected_2d + 1.)); - EXPECT_TRUE(IsFieldEqual(coords->g23, expected_2d)); + Coordinates::MetricTensor g = coords->getContravariantMetricTensor(); + + EXPECT_TRUE(IsFieldEqual(g.g11, expected_2d + 5.)); + EXPECT_TRUE(IsFieldEqual(g.g22, expected_2d + 4.)); + EXPECT_TRUE(IsFieldEqual(g.g33, expected_2d + 3.)); + EXPECT_TRUE(IsFieldEqual(g.g12, expected_2d + 2.)); + EXPECT_TRUE(IsFieldEqual(g.g13, expected_2d + 1.)); + EXPECT_TRUE(IsFieldEqual(g.g23, expected_2d)); #endif } diff --git a/tests/unit/mesh/test_coordinates.cxx b/tests/unit/mesh/test_coordinates.cxx index f862e24321..b4fa2d61e0 100644 --- a/tests/unit/mesh/test_coordinates.cxx +++ b/tests/unit/mesh/test_coordinates.cxx @@ -178,12 +178,14 @@ TEST_F(CoordinatesTest, CalcCovariant) { coords.calcContravariant(); output_info.enable(); - EXPECT_TRUE(IsFieldEqual(coords.g11, 1.0)); - EXPECT_TRUE(IsFieldEqual(coords.g22, 1.0)); - EXPECT_TRUE(IsFieldEqual(coords.g33, 1.0)); - EXPECT_TRUE(IsFieldEqual(coords.g12, 0.0)); - EXPECT_TRUE(IsFieldEqual(coords.g13, 0.0)); - EXPECT_TRUE(IsFieldEqual(coords.g23, 0.0)); + Coordinates::MetricTensor g = coords.getContravariantMetricTensor(); + + EXPECT_TRUE(IsFieldEqual(g.g11, 1.0)); + EXPECT_TRUE(IsFieldEqual(g.g22, 1.0)); + EXPECT_TRUE(IsFieldEqual(g.g33, 1.0)); + EXPECT_TRUE(IsFieldEqual(g.g12, 0.0)); + EXPECT_TRUE(IsFieldEqual(g.g13, 0.0)); + EXPECT_TRUE(IsFieldEqual(g.g23, 0.0)); } // #endif @@ -198,12 +200,14 @@ TEST_F(CoordinatesTest, DefaultConstructor) { EXPECT_TRUE(IsFieldEqual(coords.dy, 1.0)); EXPECT_TRUE(IsFieldEqual(coords.dz, default_dz)); - EXPECT_TRUE(IsFieldEqual(coords.g11, 1.0)); - EXPECT_TRUE(IsFieldEqual(coords.g22, 1.0)); - EXPECT_TRUE(IsFieldEqual(coords.g33, 1.0)); - EXPECT_TRUE(IsFieldEqual(coords.g12, 0.0)); - EXPECT_TRUE(IsFieldEqual(coords.g13, 0.0)); - EXPECT_TRUE(IsFieldEqual(coords.g23, 0.0)); + Coordinates::MetricTensor g = coords.getContravariantMetricTensor(); + + EXPECT_TRUE(IsFieldEqual(g.g11, 1.0)); + EXPECT_TRUE(IsFieldEqual(g.g22, 1.0)); + EXPECT_TRUE(IsFieldEqual(g.g33, 1.0)); + EXPECT_TRUE(IsFieldEqual(g.g12, 0.0)); + EXPECT_TRUE(IsFieldEqual(g.g13, 0.0)); + EXPECT_TRUE(IsFieldEqual(g.g23, 0.0)); EXPECT_TRUE(IsFieldEqual(coords.J, 1.0)); EXPECT_TRUE(IsFieldEqual(coords.Bxy, 1.0)); @@ -224,12 +228,14 @@ TEST_F(CoordinatesTest, ConstructWithMeshSpacing) { EXPECT_TRUE(IsFieldEqual(coords.dy, 3.2)); EXPECT_TRUE(IsFieldEqual(coords.dz, 42.)); - EXPECT_TRUE(IsFieldEqual(coords.g11, 1.0)); - EXPECT_TRUE(IsFieldEqual(coords.g22, 1.0)); - EXPECT_TRUE(IsFieldEqual(coords.g33, 1.0)); - EXPECT_TRUE(IsFieldEqual(coords.g12, 0.0)); - EXPECT_TRUE(IsFieldEqual(coords.g13, 0.0)); - EXPECT_TRUE(IsFieldEqual(coords.g23, 0.0)); + Coordinates::MetricTensor g = coords.getContravariantMetricTensor(); + + EXPECT_TRUE(IsFieldEqual(g.g11, 1.0)); + EXPECT_TRUE(IsFieldEqual(g.g22, 1.0)); + EXPECT_TRUE(IsFieldEqual(g.g33, 1.0)); + EXPECT_TRUE(IsFieldEqual(g.g12, 0.0)); + EXPECT_TRUE(IsFieldEqual(g.g13, 0.0)); + EXPECT_TRUE(IsFieldEqual(g.g23, 0.0)); EXPECT_TRUE(IsFieldEqual(coords.J, 1.0)); EXPECT_TRUE(IsFieldEqual(coords.Bxy, 1.0)); @@ -265,12 +271,13 @@ TEST_F(CoordinatesTest, ConstructWithDiagonalContravariantMetric) { EXPECT_TRUE(IsFieldEqual(coords.dz, default_dz)); // Diagonal contravariant metric - EXPECT_TRUE(IsFieldEqual(coords.g11, 2.0)); - EXPECT_TRUE(IsFieldEqual(coords.g22, 3.2)); - EXPECT_TRUE(IsFieldEqual(coords.g33, 42)); - EXPECT_TRUE(IsFieldEqual(coords.g12, 0.0)); - EXPECT_TRUE(IsFieldEqual(coords.g13, 0.0)); - EXPECT_TRUE(IsFieldEqual(coords.g23, 0.0)); + Coordinates::MetricTensor g = coords.getContravariantMetricTensor(); + EXPECT_TRUE(IsFieldEqual(g.g11, 2.0)); + EXPECT_TRUE(IsFieldEqual(g.g22, 3.2)); + EXPECT_TRUE(IsFieldEqual(g.g33, 42)); + EXPECT_TRUE(IsFieldEqual(g.g12, 0.0)); + EXPECT_TRUE(IsFieldEqual(g.g13, 0.0)); + EXPECT_TRUE(IsFieldEqual(g.g23, 0.0)); // Covariant metric should be inverse // Note: Not calculated in corners From bbee0abb9ce5bd87e24d34826f919f5b43141731 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Tue, 5 Sep 2023 09:01:29 +0100 Subject: [PATCH 002/491] Move implementation out of header file. --- include/bout/coordinates.hxx | 17 ++--------------- src/mesh/coordinates.cxx | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index 4f92bdecfb..1906cd64b4 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -109,22 +109,9 @@ public: FieldMetric g11, g22, g33, g12, g13, g23; }; - MetricTensor getContravariantMetricTensor() - { - MetricTensor g_contravariant = { g11, g22, g33, g12, g13, g23 }; - return g_contravariant; - } + MetricTensor getContravariantMetricTensor() const; - void setContravariantMetricTensor(MetricTensor g) - { - g11 = g.g11; - g22 = g.g22; - g33 = g.g33; - g12 = g.g12; - g13 = g.g13; - g23 = g.g23; - calcContravariant(); - } + void setContravariantMetricTensor(MetricTensor g); /// Christoffel symbol of the second kind (connection coefficients) FieldMetric G1_11, G1_22, G1_33, G1_12, G1_13, G1_23; diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 01f0fe46ca..4b40cac083 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -2020,3 +2020,20 @@ void Coordinates::checkContravariant() { } } } + +Coordinates::MetricTensor Coordinates::getContravariantMetricTensor() const { + MetricTensor g_contravariant = { g11, g22, g33, g12, g13, g23 }; + return g_contravariant; +} + +void Coordinates::setContravariantMetricTensor(Coordinates::MetricTensor g) { + g11 = g.g11; + g22 = g.g22; + g33 = g.g33; + g12 = g.g12; + g13 = g.g13; + g23 = g.g23; + calcContravariant(); +} + + From 24a4b05c859fc44aae522e4906a7093e902f0a79 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Tue, 5 Sep 2023 09:02:00 +0100 Subject: [PATCH 003/491] Updated coordinates_accessor to use getContravariantMetricTensor(). --- src/mesh/coordinates_accessor.cxx | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/mesh/coordinates_accessor.cxx b/src/mesh/coordinates_accessor.cxx index aff546c2b0..96c9488967 100644 --- a/src/mesh/coordinates_accessor.cxx +++ b/src/mesh/coordinates_accessor.cxx @@ -60,7 +60,15 @@ CoordinatesAccessor::CoordinatesAccessor(const Coordinates* coords) { coords->Bxy.ydown()[ind]; COPY_STRIPE(G1, G3); - COPY_STRIPE(g11, g12, g13, g22, g23, g33); + Coordinates::MetricTensor g = coords->getContravariantMetricTensor(); +// COPY_STRIPE(g11, g12, g13, g22, g23, g33); + data[stripe_size * ind.ind + static_cast(Offset::g11)] = g.g11[ind]; + data[stripe_size * ind.ind + static_cast(Offset::g12)] = g.g12[ind]; + data[stripe_size * ind.ind + static_cast(Offset::g13)] = g.g13[ind]; + data[stripe_size * ind.ind + static_cast(Offset::g22)] = g.g22[ind]; + data[stripe_size * ind.ind + static_cast(Offset::g23)] = g.g23[ind]; + data[stripe_size * ind.ind + static_cast(Offset::g33)] = g.g33[ind]; + COPY_STRIPE(g_11, g_12, g_13, g_22, g_23, g_33); } } From 203479e10ab3e24531c3e8829f4da780d057c0c1 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 6 Sep 2023 19:45:12 +0100 Subject: [PATCH 004/491] Update the MMS tests to access the contravariant components of the metric tensor through Coordinates->getContravariantMetricTensor(). --- tests/MMS/GBS/gbs.cxx | 24 +++++++++++++----------- tests/MMS/diffusion/diffusion.cxx | 13 +++++++------ tests/MMS/elm-pb/elm_pb.cxx | 17 +++++++++-------- tests/MMS/fieldalign/fieldalign.cxx | 11 ++++++----- tests/MMS/tokamak/tokamak.cxx | 15 ++++++++------- tests/MMS/wave-1d/wave.cxx | 13 +++++++------ 6 files changed, 50 insertions(+), 43 deletions(-) diff --git a/tests/MMS/GBS/gbs.cxx b/tests/MMS/GBS/gbs.cxx index d0d1fcb039..27d224f3cb 100644 --- a/tests/MMS/GBS/gbs.cxx +++ b/tests/MMS/GBS/gbs.cxx @@ -295,12 +295,14 @@ int GBS::init(bool restarting) { dz4 = SQ(SQ(coords->dz)); SAVE_REPEAT(Ve); - + + Coordinates::MetricTensor metric_tensor = coords->getContravariantMetricTensor(); + output.write("dx = {:e}, dy = {:e}, dz = {:e}\n", coords->dx(2, 2), coords->dy(2, 2), coords->dz); - output.write("g11 = {:e}, g22 = {:e}, g33 = {:e}\n", coords->g11(2, 2), - coords->g22(2, 2), coords->g33(2, 2)); - output.write("g12 = {:e}, g23 = {:e}\n", coords->g12(2, 2), coords->g23(2, 2)); + output.write("g11 = {:e}, g22 = {:e}, g33 = {:e}\n", metric_tensor.g11(2, 2), + metric_tensor.g22(2, 2), metric_tensor.g33(2, 2)); + output.write("g12 = {:e}, g23 = {:e}\n", metric_tensor.g12(2, 2), metric_tensor.g23(2, 2)); output.write("g_11 = {:e}, g_22 = {:e}, g_33 = {:e}\n", coords->g_11(2, 2), coords->g_22(2, 2), coords->g_33(2, 2)); output.write("g_12 = {:e}, g_23 = {:e}\n", coords->g_12(2, 2), coords->g_23(2, 2)); @@ -348,16 +350,16 @@ void GBS::LoadMetric(BoutReal Lnorm, BoutReal Bnorm) { sbp = -1.0; } - coords->g11 = SQ(Rxy * Bpxy); - coords->g22 = 1.0 / SQ(hthe); - coords->g33 = SQ(sinty) * coords->g11 + SQ(coords->Bxy) / coords->g11; - coords->g12 = 0.0; - coords->g13 = -sinty * coords->g11; - coords->g23 = -sbp * Btxy / (hthe * Bpxy * Rxy); + metric_tensor.g11 = SQ(Rxy * Bpxy); + metric_tensor.g22 = 1.0 / SQ(hthe); + metric_tensor.g33 = SQ(sinty) * metric_tensor.g11 + SQ(coords->Bxy) / metric_tensor.g11; + metric_tensor.g12 = 0.0; + metric_tensor.g13 = -sinty * metric_tensor.g11; + metric_tensor.g23 = -sbp * Btxy / (hthe * Bpxy * Rxy); coords->J = hthe / Bpxy; - coords->g_11 = 1.0 / coords->g11 + SQ(sinty * Rxy); + coords->g_11 = 1.0 / metric_tensor.g11 + SQ(sinty * Rxy); coords->g_22 = SQ(coords->Bxy * hthe / Bpxy); coords->g_33 = Rxy * Rxy; coords->g_12 = sbp * Btxy * hthe * sinty * Rxy / Bpxy; diff --git a/tests/MMS/diffusion/diffusion.cxx b/tests/MMS/diffusion/diffusion.cxx index bd969d4d86..84f1db6e42 100644 --- a/tests/MMS/diffusion/diffusion.cxx +++ b/tests/MMS/diffusion/diffusion.cxx @@ -43,12 +43,13 @@ int Diffusion::init(bool UNUSED(restarting)) { SAVE_ONCE(mu_N); //set mesh - coord->g11 = 1.0; - coord->g22 = 1.0; - coord->g33 = 1.0; - coord->g12 = 0.0; - coord->g13 = 0.0; - coord->g23 = 0.0; + Coordinates::MetricTensor metric_tensor = coord->getContravariantMetricTensor(); + metric_tensor.g11 = 1.0; + metric_tensor.g22 = 1.0; + metric_tensor.g33 = 1.0; + metric_tensor.g12 = 0.0; + metric_tensor.g13 = 0.0; + metric_tensor.g23 = 0.0; coord->g_11 = 1.0; coord->g_22 = 1.0; diff --git a/tests/MMS/elm-pb/elm_pb.cxx b/tests/MMS/elm-pb/elm_pb.cxx index 3ad8f70b85..512e045466 100644 --- a/tests/MMS/elm-pb/elm_pb.cxx +++ b/tests/MMS/elm-pb/elm_pb.cxx @@ -416,18 +416,19 @@ class ELMpb : public PhysicsModel { dump.add(eta, "eta", 0); /**************** CALCULATE METRICS ******************/ - - coords->g11 = SQ(Rxy * Bpxy); - coords->g22 = 1.0 / SQ(hthe); - coords->g33 = SQ(I) * coords->g11 + SQ(B0) / coords->g11; - coords->g12 = 0.0; - coords->g13 = -I * coords->g11; - coords->g23 = -Btxy / (hthe * Bpxy * Rxy); + + Coordinates::MetricTensor metric_tensor = coords->getContravariantMetricTensor(); + metric_tensor.g11 = SQ(Rxy * Bpxy); + metric_tensor.g22 = 1.0 / SQ(hthe); + metric_tensor.g33 = SQ(I) * metric_tensor.g11 + SQ(B0) / metric_tensor.g11; + metric_tensor.g12 = 0.0; + metric_tensor.g13 = -I * metric_tensor.g11; + metric_tensor.g23 = -Btxy / (hthe * Bpxy * Rxy); coords->J = hthe / Bpxy; coords->Bxy = B0; - coords->g_11 = 1.0 / coords->g11 + (SQ(I * Rxy)); + coords->g_11 = 1.0 / metric_tensor.g11 + (SQ(I * Rxy)); coords->g_22 = SQ(B0 * hthe / Bpxy); coords->g_33 = Rxy * Rxy; coords->g_12 = Btxy * hthe * I * Rxy / Bpxy; diff --git a/tests/MMS/fieldalign/fieldalign.cxx b/tests/MMS/fieldalign/fieldalign.cxx index 75481dee9a..6aa3c2705e 100644 --- a/tests/MMS/fieldalign/fieldalign.cxx +++ b/tests/MMS/fieldalign/fieldalign.cxx @@ -18,14 +18,15 @@ class FieldAlign : public PhysicsModel { f.applyBoundary(t); // df/dt = df/dtheta + df/dphi - + + Coordinates::MetricTensor metric_tensor = metric->getContravariantMetricTensor(); ddt(f) = - vx / G * (metric->g11 * DDX(f) + metric->g12 * DDY(f) + metric->g13 * DDZ(f)) - + vy / G * (metric->g12 * DDX(f) + metric->g22 * DDY(f) + metric->g23 * DDZ(f)) + vx / G * (metric_tensor.g11 * DDX(f) + metric_tensor.g12 * DDY(f) + metric_tensor.g13 * DDZ(f)) + + vy / G * (metric_tensor.g12 * DDX(f) + metric_tensor.g22 * DDY(f) + metric_tensor.g23 * DDZ(f)) + // Upwinding with second-order central differencing vz / G - * (metric->g13 * DDX(f) + metric->g23 * DDY(f) - + metric->g33 * DDZ(f)); // (unstable without additional dissipation) + * (metric_tensor.g13 * DDX(f) + metric_tensor.g23 * DDY(f) + + metric_tensor.g33 * DDZ(f)); // (unstable without additional dissipation) -SQ(SQ(metric->dx)) * D4DX4(f) /*- SQ(SQ(metric->dy))*D4DY4(f)*/ - SQ(SQ(metric->dz)) * D4DZ4(f); // Numerical dissipation terms diff --git a/tests/MMS/tokamak/tokamak.cxx b/tests/MMS/tokamak/tokamak.cxx index a8bf4b685e..6a2c17aaa6 100644 --- a/tests/MMS/tokamak/tokamak.cxx +++ b/tests/MMS/tokamak/tokamak.cxx @@ -80,16 +80,17 @@ class TokamakMMS : public PhysicsModel { sbp = -1.0; } - coords->g11 = SQ(Rxy * Bpxy); - coords->g22 = 1.0 / SQ(hthe); - coords->g33 = SQ(sinty) * coords->g11 + SQ(coords->Bxy) / coords->g11; - coords->g12 = 0.0; - coords->g13 = -sinty * coords->g11; - coords->g23 = -sbp * Btxy / (hthe * Bpxy * Rxy); + Coordinates::MetricTensor metric_tensor = coords->getContravariantMetricTensor(); + metric_tensor.g11 = SQ(Rxy * Bpxy); + metric_tensor.g22 = 1.0 / SQ(hthe); + metric_tensor.g33 = SQ(sinty) * metric_tensor.g11 + SQ(coords->Bxy) / metric_tensor.g11; + metric_tensor.g12 = 0.0; + metric_tensor.g13 = -sinty * metric_tensor.g11; + metric_tensor.g23 = -sbp * Btxy / (hthe * Bpxy * Rxy); coords->J = hthe / Bpxy; - coords->g_11 = 1.0 / coords->g11 + SQ(sinty * Rxy); + coords->g_11 = 1.0 / metric_tensor.g11 + SQ(sinty * Rxy); coords->g_22 = SQ(coords->Bxy * hthe / Bpxy); coords->g_33 = Rxy * Rxy; coords->g_12 = sbp * Btxy * hthe * sinty * Rxy / Bpxy; diff --git a/tests/MMS/wave-1d/wave.cxx b/tests/MMS/wave-1d/wave.cxx index 4f53d098c6..385abc7af5 100644 --- a/tests/MMS/wave-1d/wave.cxx +++ b/tests/MMS/wave-1d/wave.cxx @@ -32,12 +32,13 @@ class Wave1D : public PhysicsModel { SAVE_ONCE(Lx, Ly); //set mesh - coord->g11 = 1.0; - coord->g22 = 1.0; - coord->g33 = 1.0; - coord->g12 = 0.0; - coord->g13 = 0.0; - coord->g23 = 0.0; + Coordinates::MetricTensor metric_tensor = coord->getContravariantMetricTensor(); + metric_tensor.g11 = 1.0; + metric_tensor.g22 = 1.0; + metric_tensor.g33 = 1.0; + metric_tensor.g12 = 0.0; + metric_tensor.g13 = 0.0; + metric_tensor.g23 = 0.0; coord->g_11 = 1.0; coord->g_22 = 1.0; From 5454f8d742d9360b28fad1a4429d3db45e72c5b9 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 6 Sep 2023 20:55:59 +0100 Subject: [PATCH 005/491] Use getContravariantMetricTensor() in Coordinates constructor. --- src/mesh/coordinates.cxx | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 4b40cac083..dc2f351111 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -834,20 +834,22 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, dz = interpolateAndExtrapolate(coords_in->dz, location, true, true, false, transform.get()); + Coordinates::MetricTensor metric_tensor = coords_in->getContravariantMetricTensor(); + // Diagonal components of metric tensor g^{ij} - g11 = interpolateAndExtrapolate(coords_in->g11, location, true, true, false, + g11 = interpolateAndExtrapolate(metric_tensor.g11, location, true, true, false, transform.get()); - g22 = interpolateAndExtrapolate(coords_in->g22, location, true, true, false, + g22 = interpolateAndExtrapolate(metric_tensor.g22, location, true, true, false, transform.get()); - g33 = interpolateAndExtrapolate(coords_in->g33, location, true, true, false, + g33 = interpolateAndExtrapolate(metric_tensor.g33, location, true, true, false, transform.get()); // Off-diagonal elements. - g12 = interpolateAndExtrapolate(coords_in->g12, location, true, true, false, + g12 = interpolateAndExtrapolate(metric_tensor.g12, location, true, true, false, transform.get()); - g13 = interpolateAndExtrapolate(coords_in->g13, location, true, true, false, + g13 = interpolateAndExtrapolate(metric_tensor.g13, location, true, true, false, transform.get()); - g23 = interpolateAndExtrapolate(coords_in->g23, location, true, true, false, + g23 = interpolateAndExtrapolate(metric_tensor.g23, location, true, true, false, transform.get()); // 3x3 matrix inversion can exaggerate small interpolation errors, so it is From 8d4fff9e53bb9b1d217579ec8d9cf6b2b961fc45 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Thu, 7 Sep 2023 10:14:30 +0100 Subject: [PATCH 006/491] Make contravariant metric tensor components private in cython interface. --- tools/pylib/_boutpp_build/boutpp.pyx.jinja | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/pylib/_boutpp_build/boutpp.pyx.jinja b/tools/pylib/_boutpp_build/boutpp.pyx.jinja index 3aeb1428eb..349b32b6bd 100644 --- a/tools/pylib/_boutpp_build/boutpp.pyx.jinja +++ b/tools/pylib/_boutpp_build/boutpp.pyx.jinja @@ -831,7 +831,7 @@ cdef class Coordinates: cdef public {{ metric_field }} dx, dy, dz cdef public {{ metric_field }} J cdef public {{ metric_field }} Bxy - cdef public {{ metric_field }} g11, g22, g33, g12, g13, g23 + cdef {{ metric_field }} g11, g22, g33, g12, g13, g23 cdef public {{ metric_field }} g_11, g_22, g_33, g_12, g_13, g_23 cdef public {{ metric_field }} G1_11, G1_22, G1_33, G1_12, G1_13, G1_23 cdef public {{ metric_field }} G2_11, G2_22, G2_33, G2_12, G2_13, G2_23 @@ -845,7 +845,7 @@ cdef class Coordinates: self.isSelfOwned = False def _setmembers(self): -{% for f in "dx", "dy", "dz", "J", "Bxy", "g11", "g22", "g33", "g12", "g13", "g23", "g_11", "g_22", "g_33", "g_12", "g_13", "g_23", "G1_11", "G1_22", "G1_33", "G1_12", "G1_13", "G1_23", "G2_11", "G2_22", "G2_33", "G2_12", "G2_13", "G2_23", "G3_11", "G3_22", "G3_33", "G3_12", "G3_13", "G3_23", "G1", "G2", "G3", "ShiftTorsion", "IntShiftTorsion" %} +{% for f in "dx", "dy", "dz", "J", "Bxy", "g_11", "g_22", "g_33", "g_12", "g_13", "g_23", "G1_11", "G1_22", "G1_33", "G1_12", "G1_13", "G1_23", "G2_11", "G2_22", "G2_33", "G2_12", "G2_13", "G2_23", "G3_11", "G3_22", "G3_33", "G3_12", "G3_13", "G3_23", "G1", "G2", "G3", "ShiftTorsion", "IntShiftTorsion" %} self.{{f}} = {{ metric_field.fdd }}FromPtr(&self.cobj.{{f}}) {% endfor %} From b61ba1514fb247a852295e12685b563508a1c63f Mon Sep 17 00:00:00 2001 From: tomc271 Date: Thu, 7 Sep 2023 10:51:15 +0100 Subject: [PATCH 007/491] Update two more MMS tests to access the contravariant components of the metric tensor through Coordinates->getContravariantMetricTensor(). --- tests/MMS/diffusion2/diffusion.cxx | 13 +++++++------ tests/MMS/spatial/diffusion/diffusion.cxx | 13 +++++++------ 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/tests/MMS/diffusion2/diffusion.cxx b/tests/MMS/diffusion2/diffusion.cxx index bd2cd72a08..9c8adf4870 100644 --- a/tests/MMS/diffusion2/diffusion.cxx +++ b/tests/MMS/diffusion2/diffusion.cxx @@ -38,12 +38,13 @@ class Diffusion : public PhysicsModel { SAVE_ONCE3(Dx, Dy, Dz); // set mesh - coords->g11 = 1.0; - coords->g22 = 1.0; - coords->g33 = 1.0; - coords->g12 = 0.0; - coords->g13 = 0.0; - coords->g23 = 0.0; + Coordinates::MetricTensor metric_tensor = coords->getContravariantMetricTensor(); + metric_tensor.g11 = 1.0; + metric_tensor.g22 = 1.0; + metric_tensor.g33 = 1.0; + metric_tensor.g12 = 0.0; + metric_tensor.g13 = 0.0; + metric_tensor.g23 = 0.0; coords->g_11 = 1.0; coords->g_22 = 1.0; diff --git a/tests/MMS/spatial/diffusion/diffusion.cxx b/tests/MMS/spatial/diffusion/diffusion.cxx index 45e516751a..c00e21d4a6 100644 --- a/tests/MMS/spatial/diffusion/diffusion.cxx +++ b/tests/MMS/spatial/diffusion/diffusion.cxx @@ -39,12 +39,13 @@ class Diffusion : public PhysicsModel { SAVE_ONCE3(Dx, Dy, Dz); // set mesh - coords->g11 = 1.0; - coords->g22 = 1.0; - coords->g33 = 1.0; - coords->g12 = 0.0; - coords->g13 = 0.0; - coords->g23 = 0.0; + Coordinates::MetricTensor metric_tensor = coords->getContravariantMetricTensor(); + metric_tensor.g11 = 1.0; + metric_tensor.g22 = 1.0; + metric_tensor.g33 = 1.0; + metric_tensor.g12 = 0.0; + metric_tensor.g13 = 0.0; + metric_tensor.g23 = 0.0; coords->g_11 = 1.0; coords->g_22 = 1.0; From f10b8decc276ca81e8a165ec8a959c3f756a1c83 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Thu, 7 Sep 2023 10:51:42 +0100 Subject: [PATCH 008/491] Update the integrated tests to access the contravariant components of the metric tensor through Coordinates->getContravariantMetricTensor(). --- .../integrated/test-drift-instability/2fluid.cxx | 15 ++++++++------- .../test-interchange-instability/2fluid.cxx | 15 ++++++++------- .../test-laplacexy-short/test-laplacexy.cxx | 6 ++++-- tests/integrated/test-laplacexy/loadmetric.cxx | 15 ++++++++------- .../integrated/test-laplacexy/test-laplacexy.cxx | 8 +++++--- .../integrated/test-laplacexz/test-laplacexz.cxx | 7 ++++--- .../test_multigrid_laplace.cxx | 6 +++--- .../test-naulin-laplace/test_naulin_laplace.cxx | 5 +++-- 8 files changed, 43 insertions(+), 34 deletions(-) diff --git a/tests/integrated/test-drift-instability/2fluid.cxx b/tests/integrated/test-drift-instability/2fluid.cxx index 89105e970c..665a593d75 100644 --- a/tests/integrated/test-drift-instability/2fluid.cxx +++ b/tests/integrated/test-drift-instability/2fluid.cxx @@ -217,16 +217,17 @@ class TwoFluid : public PhysicsModel { /**************** CALCULATE METRICS ******************/ - coord->g11 = SQ(Rxy * Bpxy); - coord->g22 = 1.0 / SQ(hthe); - coord->g33 = SQ(I) * coord->g11 + SQ(coord->Bxy) / coord->g11; - coord->g12 = 0.0; - coord->g13 = -I * coord->g11; - coord->g23 = -Btxy / (hthe * Bpxy * Rxy); + Coordinates::MetricTensor metric_tensor = coord->getContravariantMetricTensor(); + metric_tensor.g11 = SQ(Rxy * Bpxy); + metric_tensor.g22 = 1.0 / SQ(hthe); + metric_tensor.g33 = SQ(I) * metric_tensor.g11 + SQ(coord->Bxy) / metric_tensor.g11; + metric_tensor.g12 = 0.0; + metric_tensor.g13 = -I * metric_tensor.g11; + metric_tensor.g23 = -Btxy / (hthe * Bpxy * Rxy); coord->J = hthe / Bpxy; - coord->g_11 = 1.0 / coord->g11 + SQ(I * Rxy); + coord->g_11 = 1.0 / metric_tensor.g11 + SQ(I * Rxy); coord->g_22 = SQ(coord->Bxy * hthe / Bpxy); coord->g_33 = Rxy * Rxy; coord->g_12 = Btxy * hthe * I * Rxy / Bpxy; diff --git a/tests/integrated/test-interchange-instability/2fluid.cxx b/tests/integrated/test-interchange-instability/2fluid.cxx index 5e57e166c4..633a22f74a 100644 --- a/tests/integrated/test-interchange-instability/2fluid.cxx +++ b/tests/integrated/test-interchange-instability/2fluid.cxx @@ -139,16 +139,17 @@ class Interchange : public PhysicsModel { /**************** CALCULATE METRICS ******************/ - coord->g11 = SQ(Rxy * Bpxy); - coord->g22 = 1.0 / SQ(hthe); - coord->g33 = SQ(I) * coord->g11 + SQ(coord->Bxy) / coord->g11; - coord->g12 = 0.0; - coord->g13 = -I * coord->g11; - coord->g23 = -Btxy / (hthe * Bpxy * Rxy); + Coordinates::MetricTensor metric_tensor = coord->getContravariantMetricTensor(); + metric_tensor.g11 = SQ(Rxy * Bpxy); + metric_tensor.g22 = 1.0 / SQ(hthe); + metric_tensor.g33 = SQ(I) * metric_tensor.g11 + SQ(coord->Bxy) / metric_tensor.g11; + metric_tensor.g12 = 0.0; + metric_tensor.g13 = -I * metric_tensor.g11; + metric_tensor.g23 = -Btxy / (hthe * Bpxy * Rxy); coord->J = hthe / Bpxy; - coord->g_11 = 1.0 / coord->g11 + SQ(I * Rxy); + coord->g_11 = 1.0 / metric_tensor.g11 + SQ(I * Rxy); coord->g_22 = SQ(coord->Bxy * hthe / Bpxy); coord->g_33 = Rxy * Rxy; coord->g_12 = Btxy * hthe * I * Rxy / Bpxy; diff --git a/tests/integrated/test-laplacexy-short/test-laplacexy.cxx b/tests/integrated/test-laplacexy-short/test-laplacexy.cxx index 3486760810..d0727ec97f 100644 --- a/tests/integrated/test-laplacexy-short/test-laplacexy.cxx +++ b/tests/integrated/test-laplacexy-short/test-laplacexy.cxx @@ -65,8 +65,9 @@ int main(int argc, char** argv) { if (include_y_derivs) { rhs = a * DC(Laplace_perp(f)) + DC(Grad_perp(a) * Grad_perp(f)) + b * f; } else { + Coordinates::MetricTensor metric_tensor = coords->getContravariantMetricTensor(); rhs = - a * DC(Delp2(f, CELL_DEFAULT, false)) + DC(coords->g11 * DDX(a) * DDX(f)) + b * f; + a * DC(Delp2(f, CELL_DEFAULT, false)) + DC(metric_tensor.g11 * DDX(a) * DDX(f)) + b * f; } laplacexy.setCoefs(a, b); @@ -82,8 +83,9 @@ int main(int argc, char** argv) { if (include_y_derivs) { rhs_check = a * DC(Laplace_perp(sol)) + DC(Grad_perp(a) * Grad_perp(sol)) + b * sol; } else { + Coordinates::MetricTensor metric_tensor = coords->getContravariantMetricTensor(); rhs_check = a * DC(Delp2(sol, CELL_DEFAULT, false)) - + DC(coords->g11 * DDX(a) * DDX(sol)) + b * sol; + + DC(metric_tensor.g11 * DDX(a) * DDX(sol)) + b * sol; } Options dump; diff --git a/tests/integrated/test-laplacexy/loadmetric.cxx b/tests/integrated/test-laplacexy/loadmetric.cxx index 6d8afad750..8ac3275f05 100644 --- a/tests/integrated/test-laplacexy/loadmetric.cxx +++ b/tests/integrated/test-laplacexy/loadmetric.cxx @@ -49,16 +49,17 @@ void LoadMetric(BoutReal Lnorm, BoutReal Bnorm) { sbp = -1.0; } - coords->g11 = pow(Rxy * Bpxy, 2); - coords->g22 = 1.0 / pow(hthe, 2); - coords->g33 = pow(sinty, 2) * coords->g11 + pow(coords->Bxy, 2) / coords->g11; - coords->g12 = 0.0; - coords->g13 = -sinty * coords->g11; - coords->g23 = -sbp * Btxy / (hthe * Bpxy * Rxy); + Coordinates::MetricTensor metric_tensor = coords->getContravariantMetricTensor(); + metric_tensor.g11 = pow(Rxy * Bpxy, 2); + metric_tensor.g22 = 1.0 / pow(hthe, 2); + metric_tensor.g33 = pow(sinty, 2) * metric_tensor.g11 + pow(coords->Bxy, 2) / metric_tensor.g11; + metric_tensor.g12 = 0.0; + metric_tensor.g13 = -sinty * metric_tensor.g11; + metric_tensor.g23 = -sbp * Btxy / (hthe * Bpxy * Rxy); coords->J = hthe / Bpxy; - coords->g_11 = 1.0 / coords->g11 + pow(sinty * Rxy, 2); + coords->g_11 = 1.0 / metric_tensor.g11 + pow(sinty * Rxy, 2); coords->g_22 = pow(coords->Bxy * hthe / Bpxy, 2); coords->g_33 = Rxy * Rxy; coords->g_12 = sbp * Btxy * hthe * sinty * Rxy / Bpxy; diff --git a/tests/integrated/test-laplacexy/test-laplacexy.cxx b/tests/integrated/test-laplacexy/test-laplacexy.cxx index 3142d359b1..5e310346a8 100644 --- a/tests/integrated/test-laplacexy/test-laplacexy.cxx +++ b/tests/integrated/test-laplacexy/test-laplacexy.cxx @@ -61,7 +61,8 @@ int main(int argc, char** argv) { if (include_y_derivs) { rhs = a * Laplace_perp(f) + Grad_perp(a) * Grad_perp(f) + b * f; } else { - rhs = a * Delp2(f, CELL_DEFAULT, false) + coords->g11 * DDX(a) * DDX(f) + b * f; + Coordinates::MetricTensor metric_tensor = coords->getContravariantMetricTensor(); + rhs = a * Delp2(f, CELL_DEFAULT, false) + metric_tensor.g11 * DDX(a) * DDX(f) + b * f; } LaplaceXY laplacexy; @@ -77,11 +78,12 @@ int main(int argc, char** argv) { mesh->communicate(solution); Field2D rhs_check; if (include_y_derivs) { - rhs_check = + rhs_check = a * Laplace_perp(solution) + Grad_perp(a) * Grad_perp(solution) + b * solution; } else { + Coordinates::MetricTensor metric_tensor = coords->getContravariantMetricTensor(); rhs_check = a * Delp2(solution, CELL_DEFAULT, false) - + coords->g11 * DDX(a) * DDX(solution) + b * solution; + + metric_tensor.g11 * DDX(a) * DDX(solution) + b * solution; } Options dump; diff --git a/tests/integrated/test-laplacexz/test-laplacexz.cxx b/tests/integrated/test-laplacexz/test-laplacexz.cxx index 6e43d2f3f7..8d1b733488 100644 --- a/tests/integrated/test-laplacexz/test-laplacexz.cxx +++ b/tests/integrated/test-laplacexz/test-laplacexz.cxx @@ -20,19 +20,20 @@ int main(int argc, char** argv) { auto inv = LaplaceXZ::create(bout::globals::mesh); auto coord = bout::globals::mesh->getCoordinates(); - coord->g13 = 1.8; // test off-diagonal components with nonzero value + Coordinates::MetricTensor metric_tensor = coord->getContravariantMetricTensor(); + metric_tensor.g13 = 1.8; // test off-diagonal components with nonzero value // create some input field Field3D f = FieldFactory::get()->create3D("f", Options::getRoot(), bout::globals::mesh); // Calculate the Laplacian with non-zero g13 - Field3D g = coord->g11 * D2DX2(f) + coord->g13 * D2DXDZ(f) + coord->g33 * D2DZ2(f); + Field3D g = metric_tensor.g11 * D2DX2(f) + metric_tensor.g13 * D2DXDZ(f) + metric_tensor.g33 * D2DZ2(f); inv->setCoefs(Field2D(1.0), Field2D(0.0)); Field3D f2 = inv->solve(g, 0.0); // Invert the Laplacian. - coord->g13 = 0.0; // reset to 0.0 for original laplacexz test + metric_tensor.g13 = 0.0; // reset to 0.0 for original laplacexz test // Now the normal test. output.write("Setting coefficients\n"); diff --git a/tests/integrated/test-multigrid_laplace/test_multigrid_laplace.cxx b/tests/integrated/test-multigrid_laplace/test_multigrid_laplace.cxx index 2b03f78049..7b2e4df465 100644 --- a/tests/integrated/test-multigrid_laplace/test_multigrid_laplace.cxx +++ b/tests/integrated/test-multigrid_laplace/test_multigrid_laplace.cxx @@ -298,9 +298,9 @@ int main(int argc, char** argv) { Field3D this_Grad_perp_dot_Grad_perp(const Field3D& f, const Field3D& g) { auto* mesh = f.getMesh(); - Field3D result = mesh->getCoordinates()->g11 * ::DDX(f) * ::DDX(g) - + mesh->getCoordinates()->g33 * ::DDZ(f) * ::DDZ(g) - + mesh->getCoordinates()->g13 * (DDX(f) * DDZ(g) + DDZ(f) * DDX(g)); + Field3D result = mesh->getCoordinates()->getContravariantMetricTensor().g11 * ::DDX(f) * ::DDX(g) + + mesh->getCoordinates()->getContravariantMetricTensor().g33 * ::DDZ(f) * ::DDZ(g) + + mesh->getCoordinates()->getContravariantMetricTensor().g13 * (DDX(f) * DDZ(g) + DDZ(f) * DDX(g)); return result; } diff --git a/tests/integrated/test-naulin-laplace/test_naulin_laplace.cxx b/tests/integrated/test-naulin-laplace/test_naulin_laplace.cxx index 07a403e2e2..3cc2283fdb 100644 --- a/tests/integrated/test-naulin-laplace/test_naulin_laplace.cxx +++ b/tests/integrated/test-naulin-laplace/test_naulin_laplace.cxx @@ -304,8 +304,9 @@ int main(int argc, char** argv) { Field3D this_Grad_perp_dot_Grad_perp(const Field3D& f, const Field3D& g) { const auto* coords = f.getCoordinates(); - Field3D result = coords->g11 * ::DDX(f) * ::DDX(g) + coords->g33 * ::DDZ(f) * ::DDZ(g) - + coords->g13 * (DDX(f) * DDZ(g) + DDZ(f) * DDX(g)); + Coordinates::MetricTensor metric_tensor = coords->getContravariantMetricTensor(); + Field3D result = metric_tensor.g11 * ::DDX(f) * ::DDX(g) + metric_tensor.g33 * ::DDZ(f) * ::DDZ(g) + + metric_tensor.g13 * (DDX(f) * DDZ(g) + DDZ(f) * DDX(g)); return result; } From b002c999c6f659cd32d705a10cb2d11873a88477 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Tue, 12 Sep 2023 14:47:43 +0100 Subject: [PATCH 009/491] Set, not get, metric tensor. Make MetricTensor readonly. --- include/bout/coordinates.hxx | 2 +- src/mesh/coordinates.cxx | 18 ++++++++---------- .../test-drift-instability/2fluid.cxx | 3 ++- 3 files changed, 11 insertions(+), 12 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index 1906cd64b4..0528c2fec1 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -111,7 +111,7 @@ public: MetricTensor getContravariantMetricTensor() const; - void setContravariantMetricTensor(MetricTensor g); + void setContravariantMetricTensor(const MetricTensor& metric_tensor); /// Christoffel symbol of the second kind (connection coefficients) FieldMetric G1_11, G1_22, G1_33, G1_12, G1_13, G1_23; diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index dc2f351111..433a89a84b 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -2027,15 +2027,13 @@ Coordinates::MetricTensor Coordinates::getContravariantMetricTensor() const { MetricTensor g_contravariant = { g11, g22, g33, g12, g13, g23 }; return g_contravariant; } - -void Coordinates::setContravariantMetricTensor(Coordinates::MetricTensor g) { - g11 = g.g11; - g22 = g.g22; - g33 = g.g33; - g12 = g.g12; - g13 = g.g13; - g23 = g.g23; +void Coordinates::setContravariantMetricTensor( + const Coordinates::MetricTensor& metric_tensor) { + g11 = metric_tensor.g11; + g22 = metric_tensor.g22; + g33 = metric_tensor.g33; + g12 = metric_tensor.g12; + g13 = metric_tensor.g13; + g23 = metric_tensor.g23; calcContravariant(); } - - diff --git a/tests/integrated/test-drift-instability/2fluid.cxx b/tests/integrated/test-drift-instability/2fluid.cxx index 665a593d75..e33cf0f703 100644 --- a/tests/integrated/test-drift-instability/2fluid.cxx +++ b/tests/integrated/test-drift-instability/2fluid.cxx @@ -217,13 +217,14 @@ class TwoFluid : public PhysicsModel { /**************** CALCULATE METRICS ******************/ - Coordinates::MetricTensor metric_tensor = coord->getContravariantMetricTensor(); + Coordinates::MetricTensor metric_tensor; metric_tensor.g11 = SQ(Rxy * Bpxy); metric_tensor.g22 = 1.0 / SQ(hthe); metric_tensor.g33 = SQ(I) * metric_tensor.g11 + SQ(coord->Bxy) / metric_tensor.g11; metric_tensor.g12 = 0.0; metric_tensor.g13 = -I * metric_tensor.g11; metric_tensor.g23 = -Btxy / (hthe * Bpxy * Rxy); + coord->setContravariantMetricTensor(metric_tensor); coord->J = hthe / Bpxy; From fb1ce8ca6e8e6abf60e43f4e9d69fd90b5575706 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Tue, 12 Sep 2023 22:39:49 +0100 Subject: [PATCH 010/491] Fix setContravariantMetricTensor() - recalculate covariant, not contravariant, components --- src/mesh/coordinates.cxx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 433a89a84b..62b60046bb 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -2035,5 +2035,5 @@ void Coordinates::setContravariantMetricTensor( g12 = metric_tensor.g12; g13 = metric_tensor.g13; g23 = metric_tensor.g23; - calcContravariant(); + calcCovariant(); } From 040d63fe7a5bcf93ccc09412db9072cbf7766199 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Tue, 12 Sep 2023 22:38:01 +0100 Subject: [PATCH 011/491] Add unit tests for new getter and setter --- tests/unit/mesh/test_coordinates.cxx | 75 ++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/tests/unit/mesh/test_coordinates.cxx b/tests/unit/mesh/test_coordinates.cxx index b4fa2d61e0..2fe861915c 100644 --- a/tests/unit/mesh/test_coordinates.cxx +++ b/tests/unit/mesh/test_coordinates.cxx @@ -310,3 +310,78 @@ TEST_F(CoordinatesTest, NegativeB) { output_warn.enable(); output_info.enable(); } + +TEST_F(CoordinatesTest, GetCovariantMetricTensor) { + Coordinates coords{mesh, + FieldMetric{1.0}, // dx + FieldMetric{1.0}, // dy + FieldMetric{1.0}, // dz + FieldMetric{0.0}, // J + FieldMetric{0.0}, // Bxy + FieldMetric{1.2}, // g11 + FieldMetric{2.3}, // g22 + FieldMetric{3.4}, // g33 + FieldMetric{4.5}, // g12 + FieldMetric{5.6}, // g13 + FieldMetric{6.7}, // g23 + FieldMetric{1.0}, // g_11 + FieldMetric{1.0}, // g_22 + FieldMetric{1.0}, // g_23 + FieldMetric{0.0}, // g_12 + FieldMetric{0.0}, // g_13 + FieldMetric{0.0}, // g_23 + FieldMetric{0.0}, // ShiftTorsion + FieldMetric{0.0}}; // IntShiftTorsion + + Coordinates::MetricTensor const contravariant_components = coords.getContravariantMetricTensor(); + + EXPECT_TRUE(IsFieldEqual(contravariant_components.g11, 1.2)); + EXPECT_TRUE(IsFieldEqual(contravariant_components.g22, 2.3)); + EXPECT_TRUE(IsFieldEqual(contravariant_components.g33, 3.4)); + EXPECT_TRUE(IsFieldEqual(contravariant_components.g12, 4.5)); + EXPECT_TRUE(IsFieldEqual(contravariant_components.g13, 5.6)); + EXPECT_TRUE(IsFieldEqual(contravariant_components.g23, 6.7)); +} + +TEST_F(CoordinatesTest, SetCovariantMetricTensor) { + // Set initial values for the metric tensor in the Coordinates constructor + Coordinates coords{mesh, + FieldMetric{1.0}, // dx + FieldMetric{1.0}, // dy + FieldMetric{1.0}, // dz + FieldMetric{0.0}, // J + FieldMetric{0.0}, // Bxy + FieldMetric{0.0}, // g11 + FieldMetric{0.0}, // g22 + FieldMetric{0.0}, // g33 + FieldMetric{0.0}, // g12 + FieldMetric{0.0}, // g13 + FieldMetric{0.0}, // g23 + FieldMetric{1.0}, // g_11 + FieldMetric{1.0}, // g_22 + FieldMetric{1.0}, // g_23 + FieldMetric{0.0}, // g_12 + FieldMetric{0.0}, // g_13 + FieldMetric{0.0}, // g_23 + FieldMetric{0.0}, // ShiftTorsion + FieldMetric{0.0}}; // IntShiftTorsion + + // Modify with setter + Coordinates::MetricTensor updated_metric_tensor; + updated_metric_tensor.g11 = 1.7; + updated_metric_tensor.g22 = 2.3; + updated_metric_tensor.g33 = 3.1; + updated_metric_tensor.g12 = 0.9; + updated_metric_tensor.g13 = 5.7; + updated_metric_tensor.g23 = 1.9; + coords.setContravariantMetricTensor(updated_metric_tensor); + + // Get values with getter and check they have been modified as expected + Coordinates::MetricTensor g = coords.getContravariantMetricTensor(); + EXPECT_TRUE(IsFieldEqual(g.g11, 1.7)); + EXPECT_TRUE(IsFieldEqual(g.g22, 2.3)); + EXPECT_TRUE(IsFieldEqual(g.g33, 3.1)); + EXPECT_TRUE(IsFieldEqual(g.g12, 0.9)); + EXPECT_TRUE(IsFieldEqual(g.g13, 5.7)); + EXPECT_TRUE(IsFieldEqual(g.g23, 1.9)); +} \ No newline at end of file From 00196cf98c954284fda8933ffa39a3fd793b1133 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Sun, 17 Sep 2023 12:20:54 +0100 Subject: [PATCH 012/491] Fix 2fluid test - set, not get, metric tensor. --- tests/integrated/test-interchange-instability/2fluid.cxx | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/integrated/test-interchange-instability/2fluid.cxx b/tests/integrated/test-interchange-instability/2fluid.cxx index 633a22f74a..e4d287480a 100644 --- a/tests/integrated/test-interchange-instability/2fluid.cxx +++ b/tests/integrated/test-interchange-instability/2fluid.cxx @@ -139,13 +139,14 @@ class Interchange : public PhysicsModel { /**************** CALCULATE METRICS ******************/ - Coordinates::MetricTensor metric_tensor = coord->getContravariantMetricTensor(); + Coordinates::MetricTensor metric_tensor; metric_tensor.g11 = SQ(Rxy * Bpxy); metric_tensor.g22 = 1.0 / SQ(hthe); metric_tensor.g33 = SQ(I) * metric_tensor.g11 + SQ(coord->Bxy) / metric_tensor.g11; metric_tensor.g12 = 0.0; metric_tensor.g13 = -I * metric_tensor.g11; metric_tensor.g23 = -Btxy / (hthe * Bpxy * Rxy); + coord->setContravariantMetricTensor(metric_tensor); coord->J = hthe / Bpxy; From c8e06416ea2d6674058a241672ab9c74c81d48df Mon Sep 17 00:00:00 2001 From: tomc271 Date: Mon, 18 Sep 2023 09:09:36 +0100 Subject: [PATCH 013/491] Make covariant metric tensor components private. Added getter and setter. --- include/bout/coordinates.hxx | 10 ++++++++-- src/mesh/coordinates.cxx | 17 +++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index 0528c2fec1..13aa3cc2c8 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -96,14 +96,16 @@ public: FieldMetric Bxy; ///< Magnitude of B = nabla z times nabla x - /// Contravariant metric tensor (g^{ij}) private: + + /// Contravariant metric tensor (g^{ij}) FieldMetric g11, g22, g33, g12, g13, g23; -public: /// Covariant metric tensor FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; +public: + struct MetricTensor { FieldMetric g11, g22, g33, g12, g13, g23; @@ -111,8 +113,12 @@ public: MetricTensor getContravariantMetricTensor() const; + MetricTensor getCovariantMetricTensor() const; + void setContravariantMetricTensor(const MetricTensor& metric_tensor); + void setCovariantMetricTensor(const MetricTensor& metric_tensor); + /// Christoffel symbol of the second kind (connection coefficients) FieldMetric G1_11, G1_22, G1_33, G1_12, G1_13, G1_23; FieldMetric G2_11, G2_22, G2_33, G2_12, G2_13, G2_23; diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 62b60046bb..583c019938 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -2027,6 +2027,12 @@ Coordinates::MetricTensor Coordinates::getContravariantMetricTensor() const { MetricTensor g_contravariant = { g11, g22, g33, g12, g13, g23 }; return g_contravariant; } + +Coordinates::MetricTensor Coordinates::getCovariantMetricTensor() const { + MetricTensor g_covariant = { g_11, g_22, g_33, g_12, g_13, g_23 }; + return g_covariant; +} + void Coordinates::setContravariantMetricTensor( const Coordinates::MetricTensor& metric_tensor) { g11 = metric_tensor.g11; @@ -2037,3 +2043,14 @@ void Coordinates::setContravariantMetricTensor( g23 = metric_tensor.g23; calcCovariant(); } + +void Coordinates::setCovariantMetricTensor( + const Coordinates::MetricTensor& metric_tensor) { + g_11 = metric_tensor.g11; + g_22 = metric_tensor.g22; + g_33 = metric_tensor.g33; + g_12 = metric_tensor.g12; + g_13 = metric_tensor.g13; + g_23 = metric_tensor.g23; + calcContravariant(); +} From 9df54e3e6cb02096ae9a8a434dfcf7af271f4ffb Mon Sep 17 00:00:00 2001 From: tomc271 Date: Mon, 18 Sep 2023 09:29:14 +0100 Subject: [PATCH 014/491] Update vecops and vector2d to access the contravariant components of the metric tensor through Coordinates->getCovariantMetricTensor(). --- src/field/vecops.cxx | 10 ++++++---- src/field/vector2d.cxx | 32 +++++++++++++++++++------------- 2 files changed, 25 insertions(+), 17 deletions(-) diff --git a/src/field/vecops.cxx b/src/field/vecops.cxx index 5f34e2af02..67c97171d7 100644 --- a/src/field/vecops.cxx +++ b/src/field/vecops.cxx @@ -105,11 +105,12 @@ Vector3D Grad_perp(const Field3D& f, CELL_LOC outloc, const std::string& method) Vector3D result(f.getMesh()); + Coordinates::MetricTensor covariant_components = metric->getCovariantMetricTensor(); result.x = DDX(f, outloc, method) - - metric->g_12 * DDY(f, outloc, method) / SQ(metric->J * metric->Bxy); + - covariant_components.g12 * DDY(f, outloc, method) / SQ(metric->J * metric->Bxy); result.y = 0.0; result.z = DDZ(f, outloc, method) - - metric->g_23 * DDY(f, outloc, method) / SQ(metric->J * metric->Bxy); + - covariant_components.g23 * DDY(f, outloc, method) / SQ(metric->J * metric->Bxy); result.setLocation(result.x.getLocation()); @@ -127,10 +128,11 @@ Vector2D Grad_perp(const Field2D& f, CELL_LOC outloc, const std::string& method) Vector2D result(f.getMesh()); + Coordinates::MetricTensor covariant_components = metric->getCovariantMetricTensor(); result.x = DDX(f, outloc, method) - - metric->g_12 * DDY(f, outloc, method) / SQ(metric->J * metric->Bxy); + - covariant_components.g12 * DDY(f, outloc, method) / SQ(metric->J * metric->Bxy); result.y = 0.0; - result.z = -metric->g_23 * DDY(f, outloc, method) / SQ(metric->J * metric->Bxy); + result.z = - covariant_components.g23 * DDY(f, outloc, method) / SQ(metric->J * metric->Bxy); result.setLocation(result.x.getLocation()); diff --git a/src/field/vector2d.cxx b/src/field/vector2d.cxx index e1d541004f..a92ad8b5cd 100644 --- a/src/field/vector2d.cxx +++ b/src/field/vector2d.cxx @@ -84,14 +84,18 @@ void Vector2D::toCovariant() { const auto x_at_z = interp_to(x, z.getLocation()); const auto y_at_z = interp_to(y, z.getLocation()); + Coordinates::MetricTensor covariant_components_x = metric_x->getCovariantMetricTensor(); + Coordinates::MetricTensor covariant_components_y = metric_y->getCovariantMetricTensor(); + Coordinates::MetricTensor covariant_components_z = metric_z->getCovariantMetricTensor(); + // multiply by g_{ij} BOUT_FOR(i, x.getRegion("RGN_ALL")) { - x[i] = metric_x->g_11[i] * x[i] + metric_x->g_12[i] * y_at_x[i] - + metric_x->g_13[i] * z_at_x[i]; - y[i] = metric_y->g_22[i] * y[i] + metric_y->g_12[i] * x_at_y[i] - + metric_y->g_23[i] * z_at_y[i]; - z[i] = metric_z->g_33[i] * z[i] + metric_z->g_13[i] * x_at_z[i] - + metric_z->g_23[i] * y_at_z[i]; + x[i] = covariant_components_x.g11[i] * x[i] + covariant_components_x.g12[i] * y_at_x[i] + + covariant_components_x.g13[i] * z_at_x[i]; + y[i] = covariant_components_y.g22[i] * y[i] + covariant_components_y.g12[i] * x_at_y[i] + + covariant_components_y.g23[i] * z_at_y[i]; + z[i] = covariant_components_z.g33[i] * z[i] + covariant_components_z.g13[i] * x_at_z[i] + + covariant_components_z.g23[i] * y_at_z[i]; }; } else { const auto metric = localmesh->getCoordinates(location); @@ -99,10 +103,11 @@ void Vector2D::toCovariant() { // Need to use temporary arrays to store result Coordinates::FieldMetric gx{emptyFrom(x)}, gy{emptyFrom(y)}, gz{emptyFrom(z)}; + Coordinates::MetricTensor covariant_components = metric->getCovariantMetricTensor(); BOUT_FOR(i, x.getRegion("RGN_ALL")) { - gx[i] = metric->g_11[i] * x[i] + metric->g_12[i] * y[i] + metric->g_13[i] * z[i]; - gy[i] = metric->g_22[i] * y[i] + metric->g_12[i] * x[i] + metric->g_23[i] * z[i]; - gz[i] = metric->g_33[i] * z[i] + metric->g_13[i] * x[i] + metric->g_23[i] * y[i]; + gx[i] = covariant_components.g11[i] * x[i] + covariant_components.g12[i] * y[i] + covariant_components.g13[i] * z[i]; + gy[i] = covariant_components.g22[i] * y[i] + covariant_components.g12[i] * x[i] + covariant_components.g23[i] * z[i]; + gz[i] = covariant_components.g33[i] * z[i] + covariant_components.g13[i] * x[i] + covariant_components.g23[i] * y[i]; }; x = gx; @@ -400,11 +405,12 @@ const Coordinates::FieldMetric Vector2D::operator*(const Vector2D& rhs) const { + (y * rhs.z + z * rhs.y) * g.g23; } else { // Both contravariant + Coordinates::MetricTensor covariant_components = metric->getCovariantMetricTensor(); result = - x * rhs.x * metric->g_11 + y * rhs.y * metric->g_22 + z * rhs.z * metric->g_33; - result += (x * rhs.y + y * rhs.x) * metric->g_12 - + (x * rhs.z + z * rhs.x) * metric->g_13 - + (y * rhs.z + z * rhs.y) * metric->g_23; + x * rhs.x * covariant_components.g11 + y * rhs.y * covariant_components.g22 + z * rhs.z * covariant_components.g33; + result += (x * rhs.y + y * rhs.x) * covariant_components.g12 + + (x * rhs.z + z * rhs.x) * covariant_components.g13 + + (y * rhs.z + z * rhs.y) * covariant_components.g23; } } From e2dd5459c8bd33caf560a591c3a3e53ca81b35e7 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Mon, 18 Sep 2023 09:37:58 +0100 Subject: [PATCH 015/491] Move outside loop --- src/field/vector2d.cxx | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/field/vector2d.cxx b/src/field/vector2d.cxx index a92ad8b5cd..de5fc4c812 100644 --- a/src/field/vector2d.cxx +++ b/src/field/vector2d.cxx @@ -143,11 +143,12 @@ void Vector2D::toContravariant() { const auto x_at_z = interp_to(x, z.getLocation()); const auto y_at_z = interp_to(y, z.getLocation()); + Coordinates::MetricTensor g_x = metric_x->getContravariantMetricTensor(); + Coordinates::MetricTensor g_y = metric_y->getContravariantMetricTensor(); + Coordinates::MetricTensor g_z = metric_z->getContravariantMetricTensor(); + // multiply by g_{ij} BOUT_FOR(i, x.getRegion("RGN_ALL")) { - Coordinates::MetricTensor g_x = metric_x->getContravariantMetricTensor(); - Coordinates::MetricTensor g_y = metric_y->getContravariantMetricTensor(); - Coordinates::MetricTensor g_z = metric_z->getContravariantMetricTensor(); x[i] = g_x.g11[i] * x[i] + g_x.g12[i] * y_at_x[i] + g_x.g13[i] * z_at_x[i]; y[i] = g_y.g22[i] * y[i] + g_y.g12[i] * x_at_y[i] + g_y.g23[i] * z_at_y[i]; z[i] = g_z.g33[i] * z[i] + g_z.g13[i] * x_at_z[i] + g_z.g23[i] * y_at_z[i]; @@ -159,8 +160,9 @@ void Vector2D::toContravariant() { // Need to use temporary arrays to store result Coordinates::FieldMetric gx{emptyFrom(x)}, gy{emptyFrom(y)}, gz{emptyFrom(z)}; + Coordinates::MetricTensor g = metric->getContravariantMetricTensor(); + BOUT_FOR(i, x.getRegion("RGN_ALL")) { - Coordinates::MetricTensor g = metric->getContravariantMetricTensor(); gx[i] = g.g11[i] * x[i] + g.g12[i] * y[i] + g.g13[i] * z[i]; gy[i] = g.g22[i] * y[i] + g.g12[i] * x[i] + g.g23[i] * z[i]; gz[i] = g.g33[i] * z[i] + g.g13[i] * x[i] + g.g23[i] * y[i]; From 6b3c1ef626b9271dca815d26540d1e6bd44bafc2 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Mon, 18 Sep 2023 09:39:54 +0100 Subject: [PATCH 016/491] Renaming. --- src/field/vector2d.cxx | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/field/vector2d.cxx b/src/field/vector2d.cxx index de5fc4c812..1184a26452 100644 --- a/src/field/vector2d.cxx +++ b/src/field/vector2d.cxx @@ -133,7 +133,7 @@ void Vector2D::toContravariant() { // Fields at different locations so we need to interpolate // Note : Could reduce peak memory requirement here by just - // dealing with the three components seperately. This would + // dealing with the three components separately. This would // require the use of temporary fields to hold the intermediate // result so would likely only reduce memory usage by one field const auto y_at_x = interp_to(y, x.getLocation()); @@ -160,12 +160,12 @@ void Vector2D::toContravariant() { // Need to use temporary arrays to store result Coordinates::FieldMetric gx{emptyFrom(x)}, gy{emptyFrom(y)}, gz{emptyFrom(z)}; - Coordinates::MetricTensor g = metric->getContravariantMetricTensor(); + Coordinates::MetricTensor contravariant_components = metric->getContravariantMetricTensor(); BOUT_FOR(i, x.getRegion("RGN_ALL")) { - gx[i] = g.g11[i] * x[i] + g.g12[i] * y[i] + g.g13[i] * z[i]; - gy[i] = g.g22[i] * y[i] + g.g12[i] * x[i] + g.g23[i] * z[i]; - gz[i] = g.g33[i] * z[i] + g.g13[i] * x[i] + g.g23[i] * y[i]; + gx[i] = contravariant_components.g11[i] * x[i] + contravariant_components.g12[i] * y[i] + contravariant_components.g13[i] * z[i]; + gy[i] = contravariant_components.g22[i] * y[i] + contravariant_components.g12[i] * x[i] + contravariant_components.g23[i] * z[i]; + gz[i] = contravariant_components.g33[i] * z[i] + contravariant_components.g13[i] * x[i] + contravariant_components.g23[i] * y[i]; }; x = gx; @@ -399,12 +399,12 @@ const Coordinates::FieldMetric Vector2D::operator*(const Vector2D& rhs) const { if (covariant) { // Both covariant - Coordinates::MetricTensor g = metric->getContravariantMetricTensor(); + Coordinates::MetricTensor contravariant_components = metric->getContravariantMetricTensor(); result = - x * rhs.x * g.g11 + y * rhs.y * g.g22 + z * rhs.z * g.g33; - result += (x * rhs.y + y * rhs.x) * g.g12 - + (x * rhs.z + z * rhs.x) * g.g13 - + (y * rhs.z + z * rhs.y) * g.g23; + x * rhs.x * contravariant_components.g11 + y * rhs.y * contravariant_components.g22 + z * rhs.z * contravariant_components.g33; + result += (x * rhs.y + y * rhs.x) * contravariant_components.g12 + + (x * rhs.z + z * rhs.x) * contravariant_components.g13 + + (y * rhs.z + z * rhs.y) * contravariant_components.g23; } else { // Both contravariant Coordinates::MetricTensor covariant_components = metric->getCovariantMetricTensor(); From 81398ca858c343c4597a37dcc5ba5cf93e0703c7 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Mon, 18 Sep 2023 09:59:17 +0100 Subject: [PATCH 017/491] Update vector3d to access the contravariant components of the metric tensor through Coordinates->getCovariantMetricTensor(). --- src/field/vector3d.cxx | 50 ++++++++++++++++++++++++------------------ 1 file changed, 29 insertions(+), 21 deletions(-) diff --git a/src/field/vector3d.cxx b/src/field/vector3d.cxx index 6ea927a8df..3a9a730994 100644 --- a/src/field/vector3d.cxx +++ b/src/field/vector3d.cxx @@ -85,14 +85,18 @@ void Vector3D::toCovariant() { const auto x_at_z = interp_to(x, z.getLocation()); const auto y_at_z = interp_to(y, z.getLocation()); + Coordinates::MetricTensor g_x = metric_x->getCovariantMetricTensor(); + Coordinates::MetricTensor g_y = metric_y->getCovariantMetricTensor(); + Coordinates::MetricTensor g_z = metric_z->getCovariantMetricTensor(); + // multiply by g_{ij} BOUT_FOR(i, localmesh->getRegion3D("RGN_ALL")) { - x[i] = metric_x->g_11[i] * x[i] + metric_x->g_12[i] * y_at_x[i] - + metric_x->g_13[i] * z_at_x[i]; - y[i] = metric_y->g_22[i] * y[i] + metric_y->g_12[i] * x_at_y[i] - + metric_y->g_23[i] * z_at_y[i]; - z[i] = metric_z->g_33[i] * z[i] + metric_z->g_13[i] * x_at_z[i] - + metric_z->g_23[i] * y_at_z[i]; + x[i] = g_x.g11[i] * x[i] + g_x.g12[i] * y_at_x[i] + + g_x.g13[i] * z_at_x[i]; + y[i] = g_y.g22[i] * y[i] + g_y.g12[i] * x_at_y[i] + + g_y.g23[i] * z_at_y[i]; + z[i] = g_z.g33[i] * z[i] + g_z.g13[i] * x_at_z[i] + + g_z.g23[i] * y_at_z[i]; }; } else { const auto metric = localmesh->getCoordinates(location); @@ -100,10 +104,12 @@ void Vector3D::toCovariant() { // Need to use temporary arrays to store result Field3D gx{emptyFrom(x)}, gy{emptyFrom(y)}, gz{emptyFrom(z)}; + Coordinates::MetricTensor covariant_components = metric->getCovariantMetricTensor(); + BOUT_FOR(i, localmesh->getRegion3D("RGN_ALL")) { - gx[i] = metric->g_11[i] * x[i] + metric->g_12[i] * y[i] + metric->g_13[i] * z[i]; - gy[i] = metric->g_22[i] * y[i] + metric->g_12[i] * x[i] + metric->g_23[i] * z[i]; - gz[i] = metric->g_33[i] * z[i] + metric->g_13[i] * x[i] + metric->g_23[i] * y[i]; + gx[i] = covariant_components.g11[i] * x[i] + covariant_components.g12[i] * y[i] + covariant_components.g13[i] * z[i]; + gy[i] = covariant_components.g22[i] * y[i] + covariant_components.g12[i] * x[i] + covariant_components.g23[i] * z[i]; + gz[i] = covariant_components.g33[i] * z[i] + covariant_components.g13[i] * x[i] + covariant_components.g23[i] * y[i]; }; x = gx; @@ -129,7 +135,7 @@ void Vector3D::toContravariant() { // Fields at different locations so we need to interpolate // Note : Could reduce peak memory requirement here by just - // dealing with the three components seperately. This would + // dealing with the three components separately. This would // require the use of temporary fields to hold the intermediate // result so would likely only reduce memory usage by one field const auto y_at_x = interp_to(y, x.getLocation()); @@ -141,9 +147,9 @@ void Vector3D::toContravariant() { // multiply by g_{ij} BOUT_FOR(i, localmesh->getRegion3D("RGN_ALL")) { - Coordinates::MetricTensor g_x = metric_x->getContravariantMetricTensor(); - Coordinates::MetricTensor g_y = metric_y->getContravariantMetricTensor(); - Coordinates::MetricTensor g_z = metric_z->getContravariantMetricTensor(); + Coordinates::MetricTensor g_x = metric_x->getContravariantMetricTensor(); + Coordinates::MetricTensor g_y = metric_y->getContravariantMetricTensor(); + Coordinates::MetricTensor g_z = metric_z->getContravariantMetricTensor(); x[i] = g_x.g11[i] * x[i] + g_x.g12[i] * y_at_x[i] + g_x.g13[i] * z_at_x[i]; y[i] = g_y.g22[i] * y[i] + g_y.g12[i] * x_at_y[i] + g_y.g23[i] * z_at_y[i]; z[i] = g_z.g33[i] * z[i] + g_z.g13[i] * x_at_z[i] + g_z.g23[i] * y_at_z[i]; @@ -491,11 +497,12 @@ const Field3D Vector3D::operator*(const Vector3D& rhs) const { + (y * rhs.z + z * rhs.y) * g.g23; } else { // Both contravariant + Coordinates::MetricTensor covariant_components = metric->getCovariantMetricTensor(); result = - x * rhs.x * metric->g_11 + y * rhs.y * metric->g_22 + z * rhs.z * metric->g_33; - result += (x * rhs.y + y * rhs.x) * metric->g_12 - + (x * rhs.z + z * rhs.x) * metric->g_13 - + (y * rhs.z + z * rhs.y) * metric->g_23; + x * rhs.x * covariant_components.g11 + y * rhs.y * covariant_components.g22 + z * rhs.z * covariant_components.g33; + result += (x * rhs.y + y * rhs.x) * covariant_components.g12 + + (x * rhs.z + z * rhs.x) * covariant_components.g13 + + (y * rhs.z + z * rhs.y) * covariant_components.g23; } } @@ -524,11 +531,12 @@ const Field3D Vector3D::operator*(const Vector2D& rhs) const { + (y * rhs.z + z * rhs.y) * g.g23; } else { // Both contravariant + Coordinates::MetricTensor covariant_components = metric->getCovariantMetricTensor(); result = - x * rhs.x * metric->g_11 + y * rhs.y * metric->g_22 + z * rhs.z * metric->g_33; - result += (x * rhs.y + y * rhs.x) * metric->g_12 - + (x * rhs.z + z * rhs.x) * metric->g_13 - + (y * rhs.z + z * rhs.y) * metric->g_23; + x * rhs.x * covariant_components.g11 + y * rhs.y * covariant_components.g22 + z * rhs.z * covariant_components.g33; + result += (x * rhs.y + y * rhs.x) * covariant_components.g12 + + (x * rhs.z + z * rhs.x) * covariant_components.g13 + + (y * rhs.z + z * rhs.y) * covariant_components.g23; } } From d72cbcf49265ce9effa832c173243801171ba20a Mon Sep 17 00:00:00 2001 From: tomc271 Date: Mon, 18 Sep 2023 10:00:15 +0100 Subject: [PATCH 018/491] Move outside loop --- src/field/vector3d.cxx | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/field/vector3d.cxx b/src/field/vector3d.cxx index 3a9a730994..27e8f664ab 100644 --- a/src/field/vector3d.cxx +++ b/src/field/vector3d.cxx @@ -145,11 +145,12 @@ void Vector3D::toContravariant() { const auto x_at_z = interp_to(x, z.getLocation()); const auto y_at_z = interp_to(y, z.getLocation()); - // multiply by g_{ij} - BOUT_FOR(i, localmesh->getRegion3D("RGN_ALL")) { Coordinates::MetricTensor g_x = metric_x->getContravariantMetricTensor(); Coordinates::MetricTensor g_y = metric_y->getContravariantMetricTensor(); Coordinates::MetricTensor g_z = metric_z->getContravariantMetricTensor(); + + // multiply by g_{ij} + BOUT_FOR(i, localmesh->getRegion3D("RGN_ALL")) { x[i] = g_x.g11[i] * x[i] + g_x.g12[i] * y_at_x[i] + g_x.g13[i] * z_at_x[i]; y[i] = g_y.g22[i] * y[i] + g_y.g12[i] * x_at_y[i] + g_y.g23[i] * z_at_y[i]; z[i] = g_z.g33[i] * z[i] + g_z.g13[i] * x_at_z[i] + g_z.g23[i] * y_at_z[i]; @@ -161,8 +162,9 @@ void Vector3D::toContravariant() { // Need to use temporary arrays to store result Field3D gx{emptyFrom(x)}, gy{emptyFrom(y)}, gz{emptyFrom(z)}; + Coordinates::MetricTensor g = metric->getContravariantMetricTensor(); + BOUT_FOR(i, localmesh->getRegion3D("RGN_ALL")) { - Coordinates::MetricTensor g = metric->getContravariantMetricTensor(); gx[i] = g.g11[i] * x[i] + g.g12[i] * y[i] + g.g13[i] * z[i]; gy[i] = g.g22[i] * y[i] + g.g12[i] * x[i] + g.g23[i] * z[i]; gz[i] = g.g33[i] * z[i] + g.g13[i] * x[i] + g.g23[i] * y[i]; From fbd12c5ba79f8e6bd35cd6ef5f2b57c20c7a6678 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Mon, 18 Sep 2023 10:06:56 +0100 Subject: [PATCH 019/491] Update multigrid_laplace to access the contravariant components of the metric tensor through Coordinates->getCovariantMetricTensor(). --- .../laplace/impls/multigrid/multigrid_laplace.cxx | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/invert/laplace/impls/multigrid/multigrid_laplace.cxx b/src/invert/laplace/impls/multigrid/multigrid_laplace.cxx index 7e6fcdb8c9..c12f98b24c 100644 --- a/src/invert/laplace/impls/multigrid/multigrid_laplace.cxx +++ b/src/invert/laplace/impls/multigrid/multigrid_laplace.cxx @@ -280,12 +280,13 @@ FieldPerp LaplaceMultigrid::solve(const FieldPerp& b_in, const FieldPerp& x0) { // Neumann boundary condition if (inner_boundary_flags & INVERT_SET) { // guard cells of x0 specify gradient to set at inner boundary + Coordinates::MetricTensor covariant_components = coords->getCovariantMetricTensor(); BOUT_OMP_PERF(parallel default(shared)) BOUT_OMP_PERF(for) for (int k = 1; k < lzz + 1; k++) { int k2 = k - 1; x[k] = -x0(localmesh->xstart - 1, k2) - * sqrt(coords->g_11(localmesh->xstart, yindex)) + * sqrt(covariant_components.g11(localmesh->xstart, yindex)) * coords->dx(localmesh->xstart, yindex); } } else { @@ -324,12 +325,13 @@ FieldPerp LaplaceMultigrid::solve(const FieldPerp& b_in, const FieldPerp& x0) { // Neumann boundary condition if (inner_boundary_flags & INVERT_SET) { // guard cells of x0 specify gradient to set at outer boundary + Coordinates::MetricTensor covariant_components = coords->getCovariantMetricTensor(); BOUT_OMP_PERF(parallel default(shared)) BOUT_OMP_PERF(for) for (int k = 1; k < lzz + 1; k++) { int k2 = k - 1; x[(lxx + 1) * lz2 + k] = x0(localmesh->xend + 1, k2) - * sqrt(coords->g_11(localmesh->xend, yindex)) + * sqrt(covariant_components.g11(localmesh->xend, yindex)) * coords->dx(localmesh->xend, yindex); // this is the value to set the gradient to at the outer boundary } @@ -481,6 +483,7 @@ FieldPerp LaplaceMultigrid::solve(const FieldPerp& b_in, const FieldPerp& x0) { // Neumann boundary condition if (inner_boundary_flags & INVERT_SET) { // guard cells of x0 specify gradient to set at inner boundary + Coordinates::MetricTensor covariant_components = coords->getCovariantMetricTensor(); int i2 = -1 + localmesh->xstart; BOUT_OMP_PERF(parallel default(shared)) BOUT_OMP_PERF(for) @@ -488,7 +491,7 @@ FieldPerp LaplaceMultigrid::solve(const FieldPerp& b_in, const FieldPerp& x0) { int k2 = k - 1; result(i2, k2) = x[lz2 + k] - x0(localmesh->xstart - 1, k2) - * sqrt(coords->g_11(localmesh->xstart, yindex)) + * sqrt(covariant_components.g11(localmesh->xstart, yindex)) * coords->dx(localmesh->xstart, yindex); } } else { @@ -529,6 +532,7 @@ FieldPerp LaplaceMultigrid::solve(const FieldPerp& b_in, const FieldPerp& x0) { // Neumann boundary condition if (inner_boundary_flags & INVERT_SET) { // guard cells of x0 specify gradient to set at outer boundary + Coordinates::MetricTensor covariant_components = coords->getCovariantMetricTensor(); int i2 = lxx + localmesh->xstart; BOUT_OMP_PERF(parallel default(shared)) BOUT_OMP_PERF(for) @@ -536,7 +540,7 @@ FieldPerp LaplaceMultigrid::solve(const FieldPerp& b_in, const FieldPerp& x0) { int k2 = k - 1; result(i2, k2) = x[lxx * lz2 + k] + x0(localmesh->xend + 1, k2) - * sqrt(coords->g_11(localmesh->xend, yindex)) + * sqrt(covariant_components.g11(localmesh->xend, yindex)) * coords->dx(localmesh->xend, yindex); } } else { From 1eeed1401d20b45f0d7e96866528cec3cb9ae0be Mon Sep 17 00:00:00 2001 From: tomc271 Date: Mon, 18 Sep 2023 10:24:16 +0100 Subject: [PATCH 020/491] Update serial_band to access the contravariant components of the metric tensor through Coordinates->getCovariantMetricTensor(). --- .../laplace/impls/serial_band/serial_band.cxx | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/invert/laplace/impls/serial_band/serial_band.cxx b/src/invert/laplace/impls/serial_band/serial_band.cxx index 4ec05e97c1..55178f8b34 100644 --- a/src/invert/laplace/impls/serial_band/serial_band.cxx +++ b/src/invert/laplace/impls/serial_band/serial_band.cxx @@ -266,6 +266,8 @@ FieldPerp LaplaceSerialBand::solve(const FieldPerp& b, const FieldPerp& x0) { if (iz == 0) { // DC + Coordinates::MetricTensor covariant_components = coords->getCovariantMetricTensor(); + // Inner boundary if (inner_boundary_flags & (INVERT_DC_GRAD + INVERT_SET) || inner_boundary_flags & (INVERT_DC_GRAD + INVERT_RHS)) { @@ -274,8 +276,8 @@ FieldPerp LaplaceSerialBand::solve(const FieldPerp& b, const FieldPerp& x0) { for (int ix = 0; ix < xbndry; ix++) { A(ix, 0) = 0.; A(ix, 1) = 0.; - A(ix, 2) = -.5 / sqrt(coords->g_11(ix, jy)) / coords->dx(ix, jy); - A(ix, 3) = .5 / sqrt(coords->g_11(ix, jy)) / coords->dx(ix, jy); + A(ix, 2) = -.5 / sqrt(covariant_components.g11(ix, jy)) / coords->dx(ix, jy); + A(ix, 3) = .5 / sqrt(covariant_components.g11(ix, jy)) / coords->dx(ix, jy); A(ix, 4) = 0.; } @@ -294,17 +296,17 @@ FieldPerp LaplaceSerialBand::solve(const FieldPerp& b, const FieldPerp& x0) { for (int ix = 0; ix < xbndry; ix++) { A(ix, 0) = 0.; A(ix, 1) = 0.; - A(ix, 2) = -3. / sqrt(coords->g_22(ix, jy)); - A(ix, 3) = 4. / sqrt(coords->g_22(ix + 1, jy)); - A(ix, 4) = -1. / sqrt(coords->g_22(ix + 2, jy)); + A(ix, 2) = -3. / sqrt(covariant_components.g22(ix, jy)); + A(ix, 3) = 4. / sqrt(covariant_components.g22(ix + 1, jy)); + A(ix, 4) = -1. / sqrt(covariant_components.g22(ix + 2, jy)); } } else if (inner_boundary_flags & INVERT_DC_GRADPARINV) { for (int ix = 0; ix < xbndry; ix++) { A(ix, 0) = 0.; A(ix, 1) = 0.; - A(ix, 2) = -3. * sqrt(coords->g_22(ix, jy)); - A(ix, 3) = 4. * sqrt(coords->g_22(ix + 1, jy)); - A(ix, 4) = -sqrt(coords->g_22(ix + 2, jy)); + A(ix, 2) = -3. * sqrt(covariant_components.g22(ix, jy)); + A(ix, 3) = 4. * sqrt(covariant_components.g22(ix + 1, jy)); + A(ix, 4) = -sqrt(covariant_components.g22(ix + 2, jy)); } } else if (inner_boundary_flags & INVERT_DC_LAP) { for (int ix = 0; ix < xbndry; ix++) { From b11c2b55097846c2247987357bc2d3893cc9ac8d Mon Sep 17 00:00:00 2001 From: tomc271 Date: Mon, 18 Sep 2023 10:31:41 +0100 Subject: [PATCH 021/491] Update invert_laplace to access the contravariant components of the metric tensor through Coordinates->getCovariantMetricTensor(). --- src/invert/laplace/invert_laplace.cxx | 33 ++++++++++++++------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/src/invert/laplace/invert_laplace.cxx b/src/invert/laplace/invert_laplace.cxx index c22b6947b1..648923ee52 100644 --- a/src/invert/laplace/invert_laplace.cxx +++ b/src/invert/laplace/invert_laplace.cxx @@ -495,6 +495,7 @@ void Laplacian::tridagMatrix(dcomplex* avec, dcomplex* bvec, dcomplex* cvec, dco // Set the boundary conditions if x is not periodic if (!localmesh->periodicX) { Coordinates::MetricTensor g = coords->getContravariantMetricTensor(); + Coordinates::MetricTensor covariant_components = coords->getCovariantMetricTensor(); if (localmesh->firstX()) { // INNER BOUNDARY ON THIS PROCESSOR @@ -514,8 +515,8 @@ void Laplacian::tridagMatrix(dcomplex* avec, dcomplex* bvec, dcomplex* cvec, dco // Zero gradient at inner boundary for (int ix = 0; ix < inbndry; ix++) { avec[ix] = 0.; - bvec[ix] = -1. / sqrt(coords->g_11(ix, jy)) / coords->dx(ix, jy); - cvec[ix] = 1. / sqrt(coords->g_11(ix, jy)) / coords->dx(ix, jy); + bvec[ix] = -1. / sqrt(covariant_components.g11(ix, jy)) / coords->dx(ix, jy); + cvec[ix] = 1. / sqrt(covariant_components.g11(ix, jy)) / coords->dx(ix, jy); } } else if (inner_boundary_flags & INVERT_DC_GRAD) { // Zero gradient at inner boundary @@ -527,14 +528,14 @@ void Laplacian::tridagMatrix(dcomplex* avec, dcomplex* bvec, dcomplex* cvec, dco } else if (inner_boundary_flags & INVERT_DC_GRADPAR) { for (int ix = 0; ix < inbndry; ix++) { avec[ix] = 0.0; - bvec[ix] = 1.0 / sqrt(coords->g_22(ix, jy)); - cvec[ix] = -1.0 / sqrt(coords->g_22(ix + 1, jy)); + bvec[ix] = 1.0 / sqrt(covariant_components.g22(ix, jy)); + cvec[ix] = -1.0 / sqrt(covariant_components.g22(ix + 1, jy)); } } else if (inner_boundary_flags & INVERT_DC_GRADPARINV) { for (int ix = 0; ix < inbndry; ix++) { avec[ix] = 0.0; - bvec[ix] = sqrt(coords->g_22(ix, jy)); - cvec[ix] = -sqrt(coords->g_22(ix + 1, jy)); + bvec[ix] = sqrt(covariant_components.g22(ix, jy)); + cvec[ix] = -sqrt(covariant_components.g22(ix + 1, jy)); } } else if (inner_boundary_flags & INVERT_DC_LAP) { // Decaying boundary conditions @@ -611,8 +612,8 @@ void Laplacian::tridagMatrix(dcomplex* avec, dcomplex* bvec, dcomplex* cvec, dco for (int ix = 0; ix < inbndry; ix++) { avec[ix] = dcomplex(0., 0.); bvec[ix] = - dcomplex(-1., 0.) / sqrt(coords->g_11(ix, jy)) / coords->dx(ix, jy); - cvec[ix] = dcomplex(1., 0.) / sqrt(coords->g_11(ix, jy)) / coords->dx(ix, jy); + dcomplex(-1., 0.) / sqrt(covariant_components.g11(ix, jy)) / coords->dx(ix, jy); + cvec[ix] = dcomplex(1., 0.) / sqrt(covariant_components.g11(ix, jy)) / coords->dx(ix, jy); } } else if (inner_boundary_flags & INVERT_AC_GRAD) { // Zero gradient at inner boundary @@ -671,9 +672,9 @@ void Laplacian::tridagMatrix(dcomplex* avec, dcomplex* bvec, dcomplex* cvec, dco && (outer_boundary_flags & INVERT_SET || outer_boundary_flags & INVERT_RHS)) { // Zero gradient at outer boundary for (int ix = 0; ix < outbndry; ix++) { - avec[ncx - ix] = dcomplex(-1., 0.) / sqrt(coords->g_11(ncx - ix, jy)) + avec[ncx - ix] = dcomplex(-1., 0.) / sqrt(covariant_components.g11(ncx - ix, jy)) / coords->dx(ncx - ix, jy); - bvec[ncx - ix] = dcomplex(1., 0.) / sqrt(coords->g_11(ncx - ix, jy)) + bvec[ncx - ix] = dcomplex(1., 0.) / sqrt(covariant_components.g11(ncx - ix, jy)) / coords->dx(ncx - ix, jy); cvec[ncx - ix] = dcomplex(0., 0.); } @@ -686,14 +687,14 @@ void Laplacian::tridagMatrix(dcomplex* avec, dcomplex* bvec, dcomplex* cvec, dco } } else if (inner_boundary_flags & INVERT_DC_GRADPAR) { for (int ix = 0; ix < inbndry; ix++) { - avec[ncx - ix] = 1.0 / sqrt(coords->g_22(ncx - ix + 1, jy)); - bvec[ncx - ix] = -1.0 / sqrt(coords->g_22(ncx - ix, jy)); + avec[ncx - ix] = 1.0 / sqrt(covariant_components.g22(ncx - ix + 1, jy)); + bvec[ncx - ix] = -1.0 / sqrt(covariant_components.g22(ncx - ix, jy)); cvec[ncx - ix] = 0.0; } } else if (inner_boundary_flags & INVERT_DC_GRADPARINV) { for (int ix = 0; ix < inbndry; ix++) { - avec[ncx - ix] = sqrt(coords->g_22(ncx - ix - 1, jy)); - bvec[ncx - ix] = -sqrt(coords->g_22(ncx - ix, jy)); + avec[ncx - ix] = sqrt(covariant_components.g22(ncx - ix - 1, jy)); + bvec[ncx - ix] = -sqrt(covariant_components.g22(ncx - ix, jy)); cvec[ncx - ix] = 0.0; } } else if (inner_boundary_flags & INVERT_DC_LAP) { @@ -729,9 +730,9 @@ void Laplacian::tridagMatrix(dcomplex* avec, dcomplex* bvec, dcomplex* cvec, dco && (outer_boundary_flags & INVERT_SET || outer_boundary_flags & INVERT_RHS)) { // Zero gradient at outer boundary for (int ix = 0; ix < outbndry; ix++) { - avec[ncx - ix] = dcomplex(-1., 0.) / sqrt(coords->g_11(ncx - ix, jy)) + avec[ncx - ix] = dcomplex(-1., 0.) / sqrt(covariant_components.g11(ncx - ix, jy)) / coords->dx(ncx - ix, jy); - bvec[ncx - ix] = dcomplex(1., 0.) / sqrt(coords->g_11(ncx - ix, jy)) + bvec[ncx - ix] = dcomplex(1., 0.) / sqrt(covariant_components.g11(ncx - ix, jy)) / coords->dx(ncx - ix, jy); cvec[ncx - ix] = dcomplex(0., 0.); } From 7234b63889e6ffca67426effc4c6fff7348e97b8 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Mon, 18 Sep 2023 10:44:59 +0100 Subject: [PATCH 022/491] Update cyclic to access the contravariant components of the metric tensor through Coordinates->getCovariantMetricTensor(). --- src/invert/parderiv/impls/cyclic/cyclic.cxx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/invert/parderiv/impls/cyclic/cyclic.cxx b/src/invert/parderiv/impls/cyclic/cyclic.cxx index 004b4f777a..94098e8aa0 100644 --- a/src/invert/parderiv/impls/cyclic/cyclic.cxx +++ b/src/invert/parderiv/impls/cyclic/cyclic.cxx @@ -58,7 +58,7 @@ InvertParCR::InvertParCR(Options* opt, CELL_LOC location, Mesh* mesh_in) // Number of k equations to solve for each x location nsys = 1 + (localmesh->LocalNz) / 2; - sg = sqrt(localmesh->getCoordinates(location)->g_22); + sg = sqrt(localmesh->getCoordinates(location)->getCovariantMetricTensor().g22); sg = DDY(1. / sg) / sg; } @@ -154,7 +154,7 @@ const Field3D InvertParCR::solve(const Field3D& f) { BoutReal acoef = A(x, y + local_ystart); // Constant BoutReal bcoef = - B(x, y + local_ystart) / coord->g_22(x, y + local_ystart); // d2dy2 + B(x, y + local_ystart) / coord->getCovariantMetricTensor().g22(x, y + local_ystart); // d2dy2 BoutReal ccoef = C(x, y + local_ystart); // d2dydz BoutReal dcoef = D(x, y + local_ystart); // d2dz2 BoutReal ecoef = E(x, y + local_ystart) From 6bcb0fe3fb8ac70c96276d35575b2951ca57e285 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Mon, 18 Sep 2023 10:52:31 +0100 Subject: [PATCH 023/491] Update pardiv_cyclic to access the contravariant components of the metric tensor through Coordinates->getCovariantMetricTensor(). --- src/invert/pardiv/impls/cyclic/pardiv_cyclic.cxx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/invert/pardiv/impls/cyclic/pardiv_cyclic.cxx b/src/invert/pardiv/impls/cyclic/pardiv_cyclic.cxx index c17e5af64d..3b43205eab 100644 --- a/src/invert/pardiv/impls/cyclic/pardiv_cyclic.cxx +++ b/src/invert/pardiv/impls/cyclic/pardiv_cyclic.cxx @@ -106,7 +106,7 @@ Field3D InvertParDivCR::solve(const Field3D& f) { const Field2D dy = coord->dy; const Field2D J = coord->J; - const Field2D g_22 = coord->g_22; + const Field2D g_22 = coord->getCovariantMetricTensor().g22; const auto zlength = getUniform(coord->zlength()); // Loop over flux-surfaces From d86e42ecf6fa42a1372d6027ae40ef3fe2e1c996 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Mon, 18 Sep 2023 12:20:12 +0100 Subject: [PATCH 024/491] Renaming. --- .../impls/multigrid/multigrid_laplace.cxx | 16 +++---- src/invert/laplace/invert_laplace.cxx | 24 +++++----- src/mesh/boundary_standard.cxx | 44 ++++++++++--------- 3 files changed, 43 insertions(+), 41 deletions(-) diff --git a/src/invert/laplace/impls/multigrid/multigrid_laplace.cxx b/src/invert/laplace/impls/multigrid/multigrid_laplace.cxx index c12f98b24c..6f5e0a4c80 100644 --- a/src/invert/laplace/impls/multigrid/multigrid_laplace.cxx +++ b/src/invert/laplace/impls/multigrid/multigrid_laplace.cxx @@ -601,7 +601,7 @@ void LaplaceMultigrid::generateMatrixF(int level) { int k2p = (k2 + 1) % Nz_global; int k2m = (k2 + Nz_global - 1) % Nz_global; - Coordinates::MetricTensor g = coords->getContravariantMetricTensor(); + Coordinates::MetricTensor contravariant_components = coords->getContravariantMetricTensor(); BoutReal dz = coords->dz(i2, yindex); BoutReal ddx_C = (C2(i2 + 1, yindex, k2) - C2(i2 - 1, yindex, k2)) / 2. @@ -609,21 +609,21 @@ void LaplaceMultigrid::generateMatrixF(int level) { BoutReal ddz_C = (C2(i2, yindex, k2p) - C2(i2, yindex, k2m)) / 2. / dz / C1(i2, yindex, k2); - BoutReal ddx = D(i2, yindex, k2) * g.g11(i2, yindex) / coords->dx(i2, yindex) + BoutReal ddx = D(i2, yindex, k2) * contravariant_components.g11(i2, yindex) / coords->dx(i2, yindex) / coords->dx(i2, yindex); // coefficient of 2nd derivative stencil (x-direction) - BoutReal ddz = D(i2, yindex, k2) * g.g33(i2, yindex) / SQ(dz); + BoutReal ddz = D(i2, yindex, k2) * contravariant_components.g33(i2, yindex) / SQ(dz); // coefficient of 2nd derivative stencil (z-direction) BoutReal dxdz = - D(i2, yindex, k2) * 2. * g.g13(i2, yindex) / coords->dx(i2, yindex) / dz; + D(i2, yindex, k2) * 2. * contravariant_components.g13(i2, yindex) / coords->dx(i2, yindex) / dz; // coefficient of mixed derivative stencil (could assume zero, at least initially, // if easier; then check this is true in constructor) BoutReal dxd = - (D(i2, yindex, k2) * coords->G1(i2, yindex) + g.g11(i2, yindex) * ddx_C - + g.g13(i2, yindex) + (D(i2, yindex, k2) * coords->G1(i2, yindex) + contravariant_components.g11(i2, yindex) * ddx_C + + contravariant_components.g13(i2, yindex) * ddz_C // (could assume zero, at least initially, if easier; then check this is true in constructor) ) / coords->dx(i2, yindex); // coefficient of 1st derivative stencil (x-direction) @@ -633,8 +633,8 @@ void LaplaceMultigrid::generateMatrixF(int level) { } BoutReal dzd = - (D(i2, yindex, k2) * coords->G3(i2, yindex) + g.g33(i2, yindex) * ddz_C - + g.g13(i2, yindex) + (D(i2, yindex, k2) * coords->G3(i2, yindex) + contravariant_components.g33(i2, yindex) * ddz_C + + contravariant_components.g13(i2, yindex) * ddx_C // (could assume zero, at least initially, if easier; then check // this is true in constructor) ) diff --git a/src/invert/laplace/invert_laplace.cxx b/src/invert/laplace/invert_laplace.cxx index 648923ee52..ca1bb87b22 100644 --- a/src/invert/laplace/invert_laplace.cxx +++ b/src/invert/laplace/invert_laplace.cxx @@ -324,11 +324,11 @@ void Laplacian::tridagCoefs(int jx, int jy, BoutReal kwave, dcomplex& a, dcomple BoutReal coef1, coef2, coef3, coef4, coef5; - Coordinates::MetricTensor g = localcoords->getContravariantMetricTensor(); + Coordinates::MetricTensor contravariant_components = localcoords->getContravariantMetricTensor(); - coef1 = g.g11(jx, jy); ///< X 2nd derivative coefficient - coef2 = g.g33(jx, jy); ///< Z 2nd derivative coefficient - coef3 = 2. * g.g13(jx, jy); ///< X-Z mixed derivative coefficient + coef1 = contravariant_components.g11(jx, jy); ///< X 2nd derivative coefficient + coef2 = contravariant_components.g33(jx, jy); ///< Z 2nd derivative coefficient + coef3 = 2. * contravariant_components.g13(jx, jy); ///< X-Z mixed derivative coefficient coef4 = 0.0; coef5 = 0.0; @@ -362,14 +362,14 @@ void Laplacian::tridagCoefs(int jx, int jy, BoutReal kwave, dcomplex& a, dcomple if ((jx > 0) && (jx < (localmesh->LocalNx - 1))) { BoutReal dc2dx_over_c1 = ((*c2coef)(jx + 1, jy) - (*c2coef)(jx - 1, jy)) / (2. * localcoords->dx(jx, jy) * ((*c1coef)(jx, jy))); - coef4 += g.g11(jx, jy) * dc2dx_over_c1; - coef5 += g.g13(jx, jy) * dc2dx_over_c1; + coef4 += contravariant_components.g11(jx, jy) * dc2dx_over_c1; + coef5 += contravariant_components.g13(jx, jy) * dc2dx_over_c1; } } if (localmesh->IncIntShear) { // d2dz2 term - coef2 += g.g11(jx, jy) * localcoords->IntShiftTorsion(jx, jy) + coef2 += contravariant_components.g11(jx, jy) * localcoords->IntShiftTorsion(jx, jy) * localcoords->IntShiftTorsion(jx, jy); // Mixed derivative coef3 = 0.0; // This cancels out @@ -494,7 +494,7 @@ void Laplacian::tridagMatrix(dcomplex* avec, dcomplex* bvec, dcomplex* cvec, dco // Set the boundary conditions if x is not periodic if (!localmesh->periodicX) { - Coordinates::MetricTensor g = coords->getContravariantMetricTensor(); + Coordinates::MetricTensor contravariant_components = coords->getContravariantMetricTensor(); Coordinates::MetricTensor covariant_components = coords->getCovariantMetricTensor(); if (localmesh->firstX()) { // INNER BOUNDARY ON THIS PROCESSOR @@ -550,7 +550,7 @@ void Laplacian::tridagMatrix(dcomplex* avec, dcomplex* bvec, dcomplex* cvec, dco for (int ix = 0; ix < inbndry; ix++) { avec[ix] = 0.; bvec[ix] = 1.; - cvec[ix] = -exp(-k * coords->dx(ix, jy) / sqrt(g.g11(ix, jy))); + cvec[ix] = -exp(-k * coords->dx(ix, jy) / sqrt(contravariant_components.g11(ix, jy))); } } else if (inner_boundary_flags & INVERT_IN_CYLINDER) { // Condition for inner radial boundary for cylindrical coordinates @@ -627,7 +627,7 @@ void Laplacian::tridagMatrix(dcomplex* avec, dcomplex* bvec, dcomplex* cvec, dco for (int ix = 0; ix < inbndry; ix++) { avec[ix] = 0.0; bvec[ix] = 1.0; - cvec[ix] = -exp(-1.0 * sqrt(g.g33(ix, jy) / g.g11(ix, jy)) * kwave + cvec[ix] = -exp(-1.0 * sqrt(contravariant_components.g33(ix, jy) / contravariant_components.g11(ix, jy)) * kwave * coords->dx(ix, jy)); } } else if (inner_boundary_flags & INVERT_IN_CYLINDER) { @@ -711,7 +711,7 @@ void Laplacian::tridagMatrix(dcomplex* avec, dcomplex* bvec, dcomplex* cvec, dco cvec[ncx - ix] = 0.; bvec[ncx - ix] = 1.; avec[ncx - ix] = - -exp(-k * coords->dx(ncx - ix, jy) / sqrt(g.g11(ncx - ix, jy))); + -exp(-k * coords->dx(ncx - ix, jy) / sqrt(contravariant_components.g11(ncx - ix, jy))); } } else { // Order 2 dirichlet BC (boundary half between points) @@ -747,7 +747,7 @@ void Laplacian::tridagMatrix(dcomplex* avec, dcomplex* bvec, dcomplex* cvec, dco // Use decaying zero-Laplacian solution in the boundary for (int ix = 0; ix < outbndry; ix++) { avec[ncx - ix] = - -exp(-1.0 * sqrt(g.g33(xe - ix, jy) / g.g11(xe - ix, jy)) + -exp(-1.0 * sqrt(contravariant_components.g33(xe - ix, jy) / contravariant_components.g11(xe - ix, jy)) * kwave * coords->dx(xe - ix, jy)); bvec[ncx - ix] = 1.0; cvec[ncx - ix] = 0.0; diff --git a/src/mesh/boundary_standard.cxx b/src/mesh/boundary_standard.cxx index 5618af0af9..6835b5351c 100644 --- a/src/mesh/boundary_standard.cxx +++ b/src/mesh/boundary_standard.cxx @@ -1602,16 +1602,18 @@ void BoundaryNeumann_NonOrthogonal::apply(Field2D& f) { // Calculate derivatives for metric use mesh->communicate(f); Field2D dfdy = DDY(f); - Coordinates::MetricTensor g = metric->getContravariantMetricTensor(); + Coordinates::MetricTensor contravariant_components = metric->getContravariantMetricTensor(); // Loop over all elements and set equal to the next point in for (bndry->first(); !bndry->isDone(); bndry->next1d()) { // Interpolate (linearly) metrics to halfway between last cell and boundary cell BoutReal g11shift = 0.5 - * (g.g11(bndry->x, bndry->y) + g.g11(bndry->x - bndry->bx, bndry->y)); + * (contravariant_components.g11(bndry->x, bndry->y) + + contravariant_components.g11(bndry->x - bndry->bx, bndry->y)); BoutReal g12shift = 0.5 - * (g.g12(bndry->x, bndry->y) + g.g12(bndry->x - bndry->bx, bndry->y)); + * (contravariant_components.g12(bndry->x, bndry->y) + + contravariant_components.g12(bndry->x - bndry->bx, bndry->y)); // Have to use derivatives at last gridpoint instead of derivatives on boundary layer // because derivative values don't exist in boundary region // NOTE: should be fixed to interpolate to boundary line @@ -2943,7 +2945,7 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { } jx = mesh->xend + 1; - Coordinates::MetricTensor g = metric->getContravariantMetricTensor(); + Coordinates::MetricTensor contravariant_components = metric->getContravariantMetricTensor(); for (jy = 1; jy < mesh->LocalNy - 1; jy++) { for (jz = 0; jz < ncz; jz++) { jzp = (jz + 1) % ncz; @@ -2977,42 +2979,42 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { // - d/dy( JB^y ) - d/dz( JB^z ) tmp = - -(metric->J(jx, jy) * g.g12(jx, jy) * var.y(jx, jy, jz) - + metric->J(jx, jy) * g.g13(jx, jy) * var.z(jx, jy, jz) - - metric->J(jx - 2, jy) * g.g12(jx - 2, jy) * var.y(jx - 2, jy, jz) - + metric->J(jx - 2, jy) * g.g13(jx - 2, jy) * var.z(jx - 2, jy, jz)) + -(metric->J(jx, jy) * contravariant_components.g12(jx, jy) * var.y(jx, jy, jz) + + metric->J(jx, jy) * contravariant_components.g13(jx, jy) * var.z(jx, jy, jz) + - metric->J(jx - 2, jy) * contravariant_components.g12(jx - 2, jy) * var.y(jx - 2, jy, jz) + + metric->J(jx - 2, jy) * contravariant_components.g13(jx - 2, jy) * var.z(jx - 2, jy, jz)) / (metric->dx(jx - 2, jy) + metric->dx(jx - 1, jy)); // First term (d/dx) using vals calculated above - tmp -= (metric->J(jx - 1, jy + 1) * g.g12(jx - 1, jy + 1) + tmp -= (metric->J(jx - 1, jy + 1) * contravariant_components.g12(jx - 1, jy + 1) * var.x(jx - 1, jy + 1, jz) - - metric->J(jx - 1, jy - 1) * g.g12(jx - 1, jy - 1) + - metric->J(jx - 1, jy - 1) * contravariant_components.g12(jx - 1, jy - 1) * var.x(jx - 1, jy - 1, jz) - + metric->J(jx - 1, jy + 1) * g.g22(jx - 1, jy + 1) + + metric->J(jx - 1, jy + 1) * contravariant_components.g22(jx - 1, jy + 1) * var.y(jx - 1, jy + 1, jz) - - metric->J(jx - 1, jy - 1) * g.g22(jx - 1, jy - 1) + - metric->J(jx - 1, jy - 1) * contravariant_components.g22(jx - 1, jy - 1) * var.y(jx - 1, jy - 1, jz) - + metric->J(jx - 1, jy + 1) * g.g23(jx - 1, jy + 1) + + metric->J(jx - 1, jy + 1) * contravariant_components.g23(jx - 1, jy + 1) * var.z(jx - 1, jy + 1, jz) - - metric->J(jx - 1, jy - 1) * g.g23(jx - 1, jy - 1) + - metric->J(jx - 1, jy - 1) * contravariant_components.g23(jx - 1, jy - 1) * var.z(jx - 1, jy - 1, jz)) / (metric->dy(jx - 1, jy - 1) + metric->dy(jx - 1, jy)); // second (d/dy) - tmp -= (metric->J(jx - 1, jy) * g.g13(jx - 1, jy) + tmp -= (metric->J(jx - 1, jy) * contravariant_components.g13(jx - 1, jy) * (var.x(jx - 1, jy, jzp) - var.x(jx - 1, jy, jzm)) - + metric->J(jx - 1, jy) * g.g23(jx - 1, jy) + + metric->J(jx - 1, jy) * contravariant_components.g23(jx - 1, jy) * (var.y(jx - 1, jy, jzp) - var.y(jx - 1, jy, jzm)) - + metric->J(jx - 1, jy) * g.g33(jx - 1, jy) + + metric->J(jx - 1, jy) * contravariant_components.g33(jx - 1, jy) * (var.z(jx - 1, jy, jzp) - var.z(jx - 1, jy, jzm))) / (2. * metric->dz(jx - 1, jy)); var.x(jx, jy, jz) = - (metric->J(jx - 2, jy) * g.g11(jx - 2, jy) * var.x(jx - 2, jy, jz) + (metric->J(jx - 2, jy) * contravariant_components.g11(jx - 2, jy) * var.x(jx - 2, jy, jz) + (metric->dx(jx - 2, jy) + metric->dx(jx - 1, jy)) * tmp) - / metric->J(jx, jy) * g.g11(jx, jy); + / metric->J(jx, jy) * contravariant_components.g11(jx, jy); if (mesh->xstart == 2) { var.x(jx + 1, jy, jz) = - (metric->J(jx - 3, jy) * g.g11(jx - 3, jy) * var.x(jx - 3, jy, jz) + (metric->J(jx - 3, jy) * contravariant_components.g11(jx - 3, jy) * var.x(jx - 3, jy, jz) + 4. * metric->dx(jx, jy) * tmp) - / metric->J(jx + 1, jy) * g.g11(jx + 1, jy); + / metric->J(jx + 1, jy) * contravariant_components.g11(jx + 1, jy); } } } From 7300f390310c28e016873b234364cfdaed5e4a38 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Mon, 18 Sep 2023 12:25:19 +0100 Subject: [PATCH 025/491] Update boundary_standard to access the contravariant components of the metric tensor through Coordinates->getCovariantMetricTensor(). --- src/mesh/boundary_standard.cxx | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/mesh/boundary_standard.cxx b/src/mesh/boundary_standard.cxx index 6835b5351c..fa1b81fe76 100644 --- a/src/mesh/boundary_standard.cxx +++ b/src/mesh/boundary_standard.cxx @@ -2451,12 +2451,13 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { void BoundaryNeumannPar::apply(Field2D & f) { #if not(BOUT_USE_METRIC_3D) Coordinates* metric = f.getCoordinates(); + Coordinates::MetricTensor covariant_components = metric->getCovariantMetricTensor(); // Loop over all elements and set equal to the next point in for (bndry->first(); !bndry->isDone(); bndry->next()) { f(bndry->x, bndry->y) = f(bndry->x - bndry->bx, bndry->y - bndry->by) - * sqrt(metric->g_22(bndry->x, bndry->y) - / metric->g_22(bndry->x - bndry->bx, bndry->y - bndry->by)); + * sqrt(covariant_components.g22(bndry->x, bndry->y) + / covariant_components.g22(bndry->x - bndry->bx, bndry->y - bndry->by)); } #else throw BoutException("Applying boundary condition 'neumannpar' to Field2D not " @@ -2468,12 +2469,13 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { Mesh* mesh = bndry->localmesh; ASSERT1(mesh == f.getMesh()); Coordinates* metric = f.getCoordinates(); + Coordinates::MetricTensor covariant_components = metric->getCovariantMetricTensor(); for (bndry->first(); !bndry->isDone(); bndry->next()) { for (int z = 0; z < mesh->LocalNz; z++) { f(bndry->x, bndry->y, z) = f(bndry->x - bndry->bx, bndry->y - bndry->by, z) - * sqrt(metric->g_22(bndry->x, bndry->y, z) - / metric->g_22(bndry->x - bndry->bx, bndry->y - bndry->by, z)); + * sqrt(covariant_components.g22(bndry->x, bndry->y, z) + / covariant_components.g22(bndry->x - bndry->bx, bndry->y - bndry->by, z)); } } } From 10f170653b6f258377467090f8b1b2f21e4c0d3c Mon Sep 17 00:00:00 2001 From: tomc271 Date: Mon, 18 Sep 2023 12:32:40 +0100 Subject: [PATCH 026/491] Update coordinates_accessor to access the contravariant components of the metric tensor through Coordinates->getCovariantMetricTensor(). --- src/mesh/coordinates_accessor.cxx | 40 +++++++++++++++++++++++-------- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/src/mesh/coordinates_accessor.cxx b/src/mesh/coordinates_accessor.cxx index 96c9488967..fd6449e3f2 100644 --- a/src/mesh/coordinates_accessor.cxx +++ b/src/mesh/coordinates_accessor.cxx @@ -60,16 +60,36 @@ CoordinatesAccessor::CoordinatesAccessor(const Coordinates* coords) { coords->Bxy.ydown()[ind]; COPY_STRIPE(G1, G3); - Coordinates::MetricTensor g = coords->getContravariantMetricTensor(); -// COPY_STRIPE(g11, g12, g13, g22, g23, g33); - data[stripe_size * ind.ind + static_cast(Offset::g11)] = g.g11[ind]; - data[stripe_size * ind.ind + static_cast(Offset::g12)] = g.g12[ind]; - data[stripe_size * ind.ind + static_cast(Offset::g13)] = g.g13[ind]; - data[stripe_size * ind.ind + static_cast(Offset::g22)] = g.g22[ind]; - data[stripe_size * ind.ind + static_cast(Offset::g23)] = g.g23[ind]; - data[stripe_size * ind.ind + static_cast(Offset::g33)] = g.g33[ind]; - - COPY_STRIPE(g_11, g_12, g_13, g_22, g_23, g_33); + Coordinates::MetricTensor contravariant_components = + coords->getContravariantMetricTensor(); + // COPY_STRIPE(g11, g12, g13, g22, g23, g33); + data[stripe_size * ind.ind + static_cast(Offset::g11)] = + contravariant_components.g11[ind]; + data[stripe_size * ind.ind + static_cast(Offset::g12)] = + contravariant_components.g12[ind]; + data[stripe_size * ind.ind + static_cast(Offset::g13)] = + contravariant_components.g13[ind]; + data[stripe_size * ind.ind + static_cast(Offset::g22)] = + contravariant_components.g22[ind]; + data[stripe_size * ind.ind + static_cast(Offset::g23)] = + contravariant_components.g23[ind]; + data[stripe_size * ind.ind + static_cast(Offset::g33)] = + contravariant_components.g33[ind]; + + // COPY_STRIPE(g_11, g_12, g_13, g_22, g_23, g_33); + Coordinates::MetricTensor covariant_components = coords->getCovariantMetricTensor(); + data[stripe_size * ind.ind + static_cast(Offset::g_11)] = + covariant_components.g11[ind]; + data[stripe_size * ind.ind + static_cast(Offset::g_12)] = + covariant_components.g12[ind]; + data[stripe_size * ind.ind + static_cast(Offset::g_13)] = + covariant_components.g13[ind]; + data[stripe_size * ind.ind + static_cast(Offset::g_22)] = + covariant_components.g22[ind]; + data[stripe_size * ind.ind + static_cast(Offset::g_23)] = + covariant_components.g23[ind]; + data[stripe_size * ind.ind + static_cast(Offset::g_33)] = + covariant_components.g33[ind]; } } From b7f3abc4d361962187632f6d966bcb565bf9a433 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Mon, 18 Sep 2023 13:42:55 +0100 Subject: [PATCH 027/491] Update difops to access the contravariant components of the metric tensor through Coordinates->getCovariantMetricTensor(). --- src/mesh/difops.cxx | 53 +++++++++++++++++++++++++++++---------------- 1 file changed, 34 insertions(+), 19 deletions(-) diff --git a/src/mesh/difops.cxx b/src/mesh/difops.cxx index 2e25dfeedb..cb5c9ced42 100644 --- a/src/mesh/difops.cxx +++ b/src/mesh/difops.cxx @@ -101,10 +101,11 @@ Field3D Grad_parP(const Field3D& apar, const Field3D& f) { } } + Coordinates::MetricTensor covariant_components = metric->getCovariantMetricTensor(); for (int x = 1; x <= mesh->LocalNx - 2; x++) { for (int y = mesh->ystart; y <= mesh->yend; y++) { for (int z = 0; z < ncz; z++) { - BoutReal by = 1. / sqrt(metric->g_22(x, y, z)); + BoutReal by = 1. / sqrt(covariant_components.g22(x, y, z)); // Z indices zm and zp int zm = (z - 1 + ncz) % ncz; int zp = (z + 1) % ncz; @@ -247,6 +248,8 @@ Field3D Div_par(const Field3D& f, const Field3D& v) { Coordinates* coord = f.getCoordinates(); + Coordinates::MetricTensor covariant_components = coord->getCovariantMetricTensor(); + for (int i = mesh->xstart; i <= mesh->xend; i++) { for (int j = mesh->ystart; j <= mesh->yend; j++) { for (int k = mesh->zstart; k <= mesh->zend; k++) { @@ -260,12 +263,12 @@ Field3D Div_par(const Field3D& f, const Field3D& v) { // Calculate flux at right boundary (y+1/2) BoutReal fluxRight = fR * vR * (coord->J(i, j, k) + coord->J(i, j + 1, k)) - / (sqrt(coord->g_22(i, j, k)) + sqrt(coord->g_22(i, j + 1, k))); + / (sqrt(covariant_components.g22(i, j, k)) + sqrt(covariant_components.g22(i, j + 1, k))); // Calculate at left boundary (y-1/2) BoutReal fluxLeft = fL * vL * (coord->J(i, j, k) + coord->J(i, j - 1, k)) - / (sqrt(coord->g_22(i, j, k)) + sqrt(coord->g_22(i, j - 1, k))); + / (sqrt(covariant_components.g22(i, j, k)) + sqrt(covariant_components.g22(i, j - 1, k))); result(i, j, k) = (fluxRight - fluxLeft) / (coord->dy(i, j, k) * coord->J(i, j, k)); @@ -285,7 +288,9 @@ Field3D Div_par_flux(const Field3D& v, const Field3D& f, CELL_LOC outloc, auto Bxy_floc = f.getCoordinates()->Bxy; if (!f.hasParallelSlices()) { - return metric->Bxy * FDDY(v, f / Bxy_floc, outloc, method) / sqrt(metric->g_22); + return metric->Bxy + * FDDY(v, f / Bxy_floc, outloc, method) + / sqrt(metric->getCovariantMetricTensor().g22); } // Need to modify yup and ydown fields @@ -294,7 +299,9 @@ Field3D Div_par_flux(const Field3D& v, const Field3D& f, CELL_LOC outloc, f_B.splitParallelSlices(); f_B.yup() = f.yup() / Bxy_floc; f_B.ydown() = f.ydown() / Bxy_floc; - return metric->Bxy * FDDY(v, f_B, outloc, method) / sqrt(metric->g_22); + return metric->Bxy + * FDDY(v, f_B, outloc, method) + / sqrt(metric->getCovariantMetricTensor().g22); } Field3D Div_par_flux(const Field3D& v, const Field3D& f, const std::string& method, @@ -472,13 +479,15 @@ Coordinates::FieldMetric b0xGrad_dot_Grad(const Field2D& phi, const Field2D& A, Coordinates::FieldMetric dpdx = DDX(phi, outloc); Coordinates::FieldMetric dpdy = DDY(phi, outloc); + Coordinates::MetricTensor covariant_components = metric->getCovariantMetricTensor(); + // Calculate advection velocity - Coordinates::FieldMetric vx = -metric->g_23 * dpdy; - Coordinates::FieldMetric vy = metric->g_23 * dpdx; + Coordinates::FieldMetric vx = -covariant_components.g23 * dpdy; + Coordinates::FieldMetric vy = covariant_components.g23 * dpdx; // Upwind A using these velocities Coordinates::FieldMetric result = VDDX(vx, A, outloc) + VDDY(vy, A, outloc); - result /= metric->J * sqrt(metric->g_22); + result /= metric->J * sqrt(covariant_components.g22); ASSERT1(result.getLocation() == outloc); @@ -505,10 +514,12 @@ Field3D b0xGrad_dot_Grad(const Field2D& phi, const Field3D& A, CELL_LOC outloc) Coordinates::FieldMetric dpdx = DDX(phi, outloc); Coordinates::FieldMetric dpdy = DDY(phi, outloc); + Coordinates::MetricTensor covariant_components = metric->getCovariantMetricTensor(); + // Calculate advection velocity - Coordinates::FieldMetric vx = -metric->g_23 * dpdy; - Coordinates::FieldMetric vy = metric->g_23 * dpdx; - Coordinates::FieldMetric vz = metric->g_12 * dpdy - metric->g_22 * dpdx; + Coordinates::FieldMetric vx = -covariant_components.g23 * dpdy; + Coordinates::FieldMetric vy = covariant_components.g23 * dpdx; + Coordinates::FieldMetric vz = covariant_components.g12 * dpdy - covariant_components.g22 * dpdx; if (mesh->IncIntShear) { // BOUT-06 style differencing @@ -519,7 +530,7 @@ Field3D b0xGrad_dot_Grad(const Field2D& phi, const Field3D& A, CELL_LOC outloc) Field3D result = VDDX(vx, A, outloc) + VDDY(vy, A, outloc) + VDDZ(vz, A, outloc); - result /= (metric->J * sqrt(metric->g_22)); + result /= (metric->J * sqrt(covariant_components.g22)); #if BOUT_USE_TRACK result.name = "b0xGrad_dot_Grad(" + phi.name + "," + A.name + ")"; @@ -546,15 +557,17 @@ Field3D b0xGrad_dot_Grad(const Field3D& p, const Field2D& A, CELL_LOC outloc) { Field3D dpdy = DDY(p, outloc); Field3D dpdz = DDZ(p, outloc); + Coordinates::MetricTensor covariant_components = metric->getCovariantMetricTensor(); + // Calculate advection velocity - Field3D vx = metric->g_22 * dpdz - metric->g_23 * dpdy; - Field3D vy = metric->g_23 * dpdx - metric->g_12 * dpdz; + Field3D vx = covariant_components.g22 * dpdz - covariant_components.g23 * dpdy; + Field3D vy = covariant_components.g23 * dpdx - covariant_components.g12 * dpdz; // Upwind A using these velocities Field3D result = VDDX(vx, A, outloc) + VDDY(vy, A, outloc); - result /= (metric->J * sqrt(metric->g_22)); + result /= (metric->J * sqrt(covariant_components.g22)); #if BOUT_USE_TRACK result.name = "b0xGrad_dot_Grad(" + p.name + "," + A.name + ")"; @@ -583,10 +596,12 @@ Field3D b0xGrad_dot_Grad(const Field3D& phi, const Field3D& A, CELL_LOC outloc) Field3D dpdy = DDY(phi, outloc); Field3D dpdz = DDZ(phi, outloc); + Coordinates::MetricTensor covariant_components = metric->getCovariantMetricTensor(); + // Calculate advection velocity - Field3D vx = metric->g_22 * dpdz - metric->g_23 * dpdy; - Field3D vy = metric->g_23 * dpdx - metric->g_12 * dpdz; - Field3D vz = metric->g_12 * dpdy - metric->g_22 * dpdx; + Field3D vx = covariant_components.g22 * dpdz - covariant_components.g23 * dpdy; + Field3D vy = covariant_components.g23 * dpdx - covariant_components.g12 * dpdz; + Field3D vz = covariant_components.g12 * dpdy - covariant_components.g22 * dpdx; if (mesh->IncIntShear) { // BOUT-06 style differencing @@ -595,7 +610,7 @@ Field3D b0xGrad_dot_Grad(const Field3D& phi, const Field3D& A, CELL_LOC outloc) Field3D result = VDDX(vx, A, outloc) + VDDY(vy, A, outloc) + VDDZ(vz, A, outloc); - result /= (metric->J * sqrt(metric->g_22)); + result /= (metric->J * sqrt(covariant_components.g22)); #if BOUT_USE_TRACK result.name = "b0xGrad_dot_Grad(" + phi.name + "," + A.name + ")"; From f74c974c079767f2e754f5ab26c280553469f6c3 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Mon, 18 Sep 2023 13:50:10 +0100 Subject: [PATCH 028/491] Update fv_ops to access the contravariant components of the metric tensor through Coordinates->getCovariantMetricTensor(). --- src/mesh/fv_ops.cxx | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/mesh/fv_ops.cxx b/src/mesh/fv_ops.cxx index c4d6b22281..516efc9a6b 100644 --- a/src/mesh/fv_ops.cxx +++ b/src/mesh/fv_ops.cxx @@ -70,7 +70,8 @@ Field3D Div_a_Grad_perp(const Field3D& a, const Field3D& f) { // 3D Metric, need yup/ydown fields. // Requires previous communication of metrics // -- should insert communication here? - if (!g.g23.hasParallelSlices() || !coord->g_23.hasParallelSlices() + Coordinates::MetricTensor covariant_components = coord->getCovariantMetricTensor(); + if (!g.g23.hasParallelSlices() || !covariant_components.g23.hasParallelSlices() || !coord->dy.hasParallelSlices() || !coord->dz.hasParallelSlices() || !coord->Bxy.hasParallelSlices() || !coord->J.hasParallelSlices()) { throw BoutException("metrics have no yup/down: Maybe communicate in init?"); @@ -89,7 +90,8 @@ Field3D Div_a_Grad_perp(const Field3D& a, const Field3D& f) { // Only in 3D case with FCI do the metrics have parallel slices const bool metric_fci = fci and bout::build::use_metric_3d; const auto g23 = makeslices(metric_fci, g.g23); - const auto g_23 = makeslices(metric_fci, coord->g_23); + const auto g_23 = makeslices(metric_fci, + coord->getCovariantMetricTensor().g23); const auto J = makeslices(metric_fci, coord->J); const auto dy = makeslices(metric_fci, coord->dy); const auto dz = makeslices(metric_fci, coord->dz); @@ -209,12 +211,14 @@ const Field3D Div_par_K_Grad_par(const Field3D& Kin, const Field3D& fin, const auto iyp = i.yp(); const auto iym = i.ym(); + Coordinates::MetricTensor covariant_components = coord->getCovariantMetricTensor(); + if (bndry_flux || mesh->periodicY(i.x()) || !mesh->lastY(i.x()) || (i.y() != mesh->yend)) { BoutReal c = 0.5 * (K[i] + Kup[iyp]); // K at the upper boundary BoutReal J = 0.5 * (coord->J[i] + coord->J[iyp]); // Jacobian at boundary - BoutReal g_22 = 0.5 * (coord->g_22[i] + coord->g_22[iyp]); + BoutReal g_22 = 0.5 * (covariant_components.g22[i] + covariant_components.g22[iyp]); BoutReal gradient = 2. * (fup[iyp] - f[i]) / (coord->dy[i] + coord->dy[iyp]); @@ -229,7 +233,7 @@ const Field3D Div_par_K_Grad_par(const Field3D& Kin, const Field3D& fin, BoutReal c = 0.5 * (K[i] + Kdown[iym]); // K at the lower boundary BoutReal J = 0.5 * (coord->J[i] + coord->J[iym]); // Jacobian at boundary - BoutReal g_22 = 0.5 * (coord->g_22[i] + coord->g_22[iym]); + BoutReal g_22 = 0.5 * (covariant_components.g22[i] + covariant_components.g22[iym]); BoutReal gradient = 2. * (f[i] - fdown[iym]) / (coord->dy[i] + coord->dy[iym]); From e29669f6855c8e24b7d642b2cf024f1cba59acb8 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Mon, 18 Sep 2023 13:57:54 +0100 Subject: [PATCH 029/491] Make covariant metric tensor components private in cython interface. --- tools/pylib/_boutpp_build/boutpp.pyx.jinja | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/pylib/_boutpp_build/boutpp.pyx.jinja b/tools/pylib/_boutpp_build/boutpp.pyx.jinja index 349b32b6bd..de979087c1 100644 --- a/tools/pylib/_boutpp_build/boutpp.pyx.jinja +++ b/tools/pylib/_boutpp_build/boutpp.pyx.jinja @@ -832,7 +832,7 @@ cdef class Coordinates: cdef public {{ metric_field }} J cdef public {{ metric_field }} Bxy cdef {{ metric_field }} g11, g22, g33, g12, g13, g23 - cdef public {{ metric_field }} g_11, g_22, g_33, g_12, g_13, g_23 + cdef {{ metric_field }} g_11, g_22, g_33, g_12, g_13, g_23 cdef public {{ metric_field }} G1_11, G1_22, G1_33, G1_12, G1_13, G1_23 cdef public {{ metric_field }} G2_11, G2_22, G2_33, G2_12, G2_13, G2_23 cdef public {{ metric_field }} G3_11, G3_22, G3_33, G3_12, G3_13, G3_23 @@ -845,7 +845,7 @@ cdef class Coordinates: self.isSelfOwned = False def _setmembers(self): -{% for f in "dx", "dy", "dz", "J", "Bxy", "g_11", "g_22", "g_33", "g_12", "g_13", "g_23", "G1_11", "G1_22", "G1_33", "G1_12", "G1_13", "G1_23", "G2_11", "G2_22", "G2_33", "G2_12", "G2_13", "G2_23", "G3_11", "G3_22", "G3_33", "G3_12", "G3_13", "G3_23", "G1", "G2", "G3", "ShiftTorsion", "IntShiftTorsion" %} +{% for f in "dx", "dy", "dz", "J", "Bxy", "G1_11", "G1_22", "G1_33", "G1_12", "G1_13", "G1_23", "G2_11", "G2_22", "G2_33", "G2_12", "G2_13", "G2_23", "G3_11", "G3_22", "G3_33", "G3_12", "G3_13", "G3_23", "G1", "G2", "G3", "ShiftTorsion", "IntShiftTorsion" %} self.{{f}} = {{ metric_field.fdd }}FromPtr(&self.cobj.{{f}}) {% endfor %} From 94c6414a541b2b5b1289a36515810793a06dbb08 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Mon, 18 Sep 2023 14:06:09 +0100 Subject: [PATCH 030/491] Fix diffusion test - set, not get, metric tensor. --- tests/MMS/diffusion/diffusion.cxx | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/MMS/diffusion/diffusion.cxx b/tests/MMS/diffusion/diffusion.cxx index 84f1db6e42..f44f32abe8 100644 --- a/tests/MMS/diffusion/diffusion.cxx +++ b/tests/MMS/diffusion/diffusion.cxx @@ -43,14 +43,15 @@ int Diffusion::init(bool UNUSED(restarting)) { SAVE_ONCE(mu_N); //set mesh - Coordinates::MetricTensor metric_tensor = coord->getContravariantMetricTensor(); + Coordinates::MetricTensor metric_tensor; metric_tensor.g11 = 1.0; metric_tensor.g22 = 1.0; metric_tensor.g33 = 1.0; metric_tensor.g12 = 0.0; metric_tensor.g13 = 0.0; metric_tensor.g23 = 0.0; - + coord->setContravariantMetricTensor(metric_tensor); + coord->g_11 = 1.0; coord->g_22 = 1.0; coord->g_33 = 1.0; From 0475371020fd0cb28adbeb1fa597795bcbafc435 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Mon, 18 Sep 2023 14:10:01 +0100 Subject: [PATCH 031/491] Update diffusion test to set the covariant components of the metric tensor through Coordinates->setCovariantMetricTensor(). --- tests/MMS/diffusion/diffusion.cxx | 33 +++++++++++++++++-------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/tests/MMS/diffusion/diffusion.cxx b/tests/MMS/diffusion/diffusion.cxx index f44f32abe8..a885603834 100644 --- a/tests/MMS/diffusion/diffusion.cxx +++ b/tests/MMS/diffusion/diffusion.cxx @@ -43,21 +43,24 @@ int Diffusion::init(bool UNUSED(restarting)) { SAVE_ONCE(mu_N); //set mesh - Coordinates::MetricTensor metric_tensor; - metric_tensor.g11 = 1.0; - metric_tensor.g22 = 1.0; - metric_tensor.g33 = 1.0; - metric_tensor.g12 = 0.0; - metric_tensor.g13 = 0.0; - metric_tensor.g23 = 0.0; - coord->setContravariantMetricTensor(metric_tensor); - - coord->g_11 = 1.0; - coord->g_22 = 1.0; - coord->g_33 = 1.0; - coord->g_12 = 0.0; - coord->g_13 = 0.0; - coord->g_23 = 0.0; + Coordinates::MetricTensor contravariant_components; + contravariant_components.g11 = 1.0; + contravariant_components.g22 = 1.0; + contravariant_components.g33 = 1.0; + contravariant_components.g12 = 0.0; + contravariant_components.g13 = 0.0; + contravariant_components.g23 = 0.0; + coord->setContravariantMetricTensor(contravariant_components); + + Coordinates::MetricTensor covariant_components; + covariant_components.g11 = 1.0; + covariant_components.g22 = 1.0; + covariant_components.g33 = 1.0; + covariant_components.g12 = 0.0; + covariant_components.g13 = 0.0; + covariant_components.g23 = 0.0; + coord->setCovariantMetricTensor(covariant_components); + coord->geometry(); // Tell BOUT++ to solve N From eeab57c18d9068be83c2c58e655639eb896d6270 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Mon, 18 Sep 2023 14:19:37 +0100 Subject: [PATCH 032/491] Fix other MMS tests - set, not get, metric tensor. --- tests/MMS/GBS/gbs.cxx | 16 +++++++++------- tests/MMS/diffusion2/diffusion.cxx | 15 ++++++++------- tests/MMS/spatial/diffusion/diffusion.cxx | 15 ++++++++------- tests/MMS/tokamak/tokamak.cxx | 17 +++++++++-------- tests/MMS/wave-1d/wave.cxx | 15 ++++++++------- 5 files changed, 42 insertions(+), 36 deletions(-) diff --git a/tests/MMS/GBS/gbs.cxx b/tests/MMS/GBS/gbs.cxx index 27d224f3cb..6613694da3 100644 --- a/tests/MMS/GBS/gbs.cxx +++ b/tests/MMS/GBS/gbs.cxx @@ -350,16 +350,18 @@ void GBS::LoadMetric(BoutReal Lnorm, BoutReal Bnorm) { sbp = -1.0; } - metric_tensor.g11 = SQ(Rxy * Bpxy); - metric_tensor.g22 = 1.0 / SQ(hthe); - metric_tensor.g33 = SQ(sinty) * metric_tensor.g11 + SQ(coords->Bxy) / metric_tensor.g11; - metric_tensor.g12 = 0.0; - metric_tensor.g13 = -sinty * metric_tensor.g11; - metric_tensor.g23 = -sbp * Btxy / (hthe * Bpxy * Rxy); + Coordinates::MetricTensor contravariant_components = coords->getContravariantMetricTensor(); + contravariant_components.g11 = SQ(Rxy * Bpxy); + contravariant_components.g22 = 1.0 / SQ(hthe); + contravariant_components.g33 = SQ(sinty) * contravariant_components.g11 + SQ(coords->Bxy) / contravariant_components.g11; + contravariant_components.g12 = 0.0; + contravariant_components.g13 = -sinty * contravariant_components.g11; + contravariant_components.g23 = -sbp * Btxy / (hthe * Bpxy * Rxy); + coords->setContravariantMetricTensor(contravariant_components); coords->J = hthe / Bpxy; - coords->g_11 = 1.0 / metric_tensor.g11 + SQ(sinty * Rxy); + coords->g_11 = 1.0 / contravariant_components.g11 + SQ(sinty * Rxy); coords->g_22 = SQ(coords->Bxy * hthe / Bpxy); coords->g_33 = Rxy * Rxy; coords->g_12 = sbp * Btxy * hthe * sinty * Rxy / Bpxy; diff --git a/tests/MMS/diffusion2/diffusion.cxx b/tests/MMS/diffusion2/diffusion.cxx index 9c8adf4870..757c2e62b6 100644 --- a/tests/MMS/diffusion2/diffusion.cxx +++ b/tests/MMS/diffusion2/diffusion.cxx @@ -38,13 +38,14 @@ class Diffusion : public PhysicsModel { SAVE_ONCE3(Dx, Dy, Dz); // set mesh - Coordinates::MetricTensor metric_tensor = coords->getContravariantMetricTensor(); - metric_tensor.g11 = 1.0; - metric_tensor.g22 = 1.0; - metric_tensor.g33 = 1.0; - metric_tensor.g12 = 0.0; - metric_tensor.g13 = 0.0; - metric_tensor.g23 = 0.0; + Coordinates::MetricTensor contravariant_components = coords->getContravariantMetricTensor(); + contravariant_components.g11 = 1.0; + contravariant_components.g22 = 1.0; + contravariant_components.g33 = 1.0; + contravariant_components.g12 = 0.0; + contravariant_components.g13 = 0.0; + contravariant_components.g23 = 0.0; + coords->setContravariantMetricTensor(contravariant_components); coords->g_11 = 1.0; coords->g_22 = 1.0; diff --git a/tests/MMS/spatial/diffusion/diffusion.cxx b/tests/MMS/spatial/diffusion/diffusion.cxx index c00e21d4a6..0514d851d9 100644 --- a/tests/MMS/spatial/diffusion/diffusion.cxx +++ b/tests/MMS/spatial/diffusion/diffusion.cxx @@ -39,13 +39,14 @@ class Diffusion : public PhysicsModel { SAVE_ONCE3(Dx, Dy, Dz); // set mesh - Coordinates::MetricTensor metric_tensor = coords->getContravariantMetricTensor(); - metric_tensor.g11 = 1.0; - metric_tensor.g22 = 1.0; - metric_tensor.g33 = 1.0; - metric_tensor.g12 = 0.0; - metric_tensor.g13 = 0.0; - metric_tensor.g23 = 0.0; + Coordinates::MetricTensor contravariant_components = coords->getContravariantMetricTensor(); + contravariant_components.g11 = 1.0; + contravariant_components.g22 = 1.0; + contravariant_components.g33 = 1.0; + contravariant_components.g12 = 0.0; + contravariant_components.g13 = 0.0; + contravariant_components.g23 = 0.0; + coords->setContravariantMetricTensor(contravariant_components); coords->g_11 = 1.0; coords->g_22 = 1.0; diff --git a/tests/MMS/tokamak/tokamak.cxx b/tests/MMS/tokamak/tokamak.cxx index 6a2c17aaa6..653a938c64 100644 --- a/tests/MMS/tokamak/tokamak.cxx +++ b/tests/MMS/tokamak/tokamak.cxx @@ -80,17 +80,18 @@ class TokamakMMS : public PhysicsModel { sbp = -1.0; } - Coordinates::MetricTensor metric_tensor = coords->getContravariantMetricTensor(); - metric_tensor.g11 = SQ(Rxy * Bpxy); - metric_tensor.g22 = 1.0 / SQ(hthe); - metric_tensor.g33 = SQ(sinty) * metric_tensor.g11 + SQ(coords->Bxy) / metric_tensor.g11; - metric_tensor.g12 = 0.0; - metric_tensor.g13 = -sinty * metric_tensor.g11; - metric_tensor.g23 = -sbp * Btxy / (hthe * Bpxy * Rxy); + Coordinates::MetricTensor contravariant_components = coords->getContravariantMetricTensor(); + contravariant_components.g11 = SQ(Rxy * Bpxy); + contravariant_components.g22 = 1.0 / SQ(hthe); + contravariant_components.g33 = SQ(sinty) * contravariant_components.g11 + SQ(coords->Bxy) / contravariant_components.g11; + contravariant_components.g12 = 0.0; + contravariant_components.g13 = -sinty * contravariant_components.g11; + contravariant_components.g23 = -sbp * Btxy / (hthe * Bpxy * Rxy); + coords->setContravariantMetricTensor(contravariant_components); coords->J = hthe / Bpxy; - coords->g_11 = 1.0 / metric_tensor.g11 + SQ(sinty * Rxy); + coords->g_11 = 1.0 / contravariant_components.g11 + SQ(sinty * Rxy); coords->g_22 = SQ(coords->Bxy * hthe / Bpxy); coords->g_33 = Rxy * Rxy; coords->g_12 = sbp * Btxy * hthe * sinty * Rxy / Bpxy; diff --git a/tests/MMS/wave-1d/wave.cxx b/tests/MMS/wave-1d/wave.cxx index 385abc7af5..bd1be3cf82 100644 --- a/tests/MMS/wave-1d/wave.cxx +++ b/tests/MMS/wave-1d/wave.cxx @@ -32,13 +32,14 @@ class Wave1D : public PhysicsModel { SAVE_ONCE(Lx, Ly); //set mesh - Coordinates::MetricTensor metric_tensor = coord->getContravariantMetricTensor(); - metric_tensor.g11 = 1.0; - metric_tensor.g22 = 1.0; - metric_tensor.g33 = 1.0; - metric_tensor.g12 = 0.0; - metric_tensor.g13 = 0.0; - metric_tensor.g23 = 0.0; + Coordinates::MetricTensor contravariant_components = coord->getContravariantMetricTensor(); + contravariant_components.g11 = 1.0; + contravariant_components.g22 = 1.0; + contravariant_components.g33 = 1.0; + contravariant_components.g12 = 0.0; + contravariant_components.g13 = 0.0; + contravariant_components.g23 = 0.0; + coord->setContravariantMetricTensor(contravariant_components); coord->g_11 = 1.0; coord->g_22 = 1.0; From d04d4ed5630bc9326022958a814a32a7a787620c Mon Sep 17 00:00:00 2001 From: tomc271 Date: Mon, 18 Sep 2023 14:42:56 +0100 Subject: [PATCH 033/491] Update unit tests ConstructWithDiagonalContravariantMetric and CalcContravariant to use getCovariantMetricTensor(). --- tests/unit/mesh/test_coordinates.cxx | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/tests/unit/mesh/test_coordinates.cxx b/tests/unit/mesh/test_coordinates.cxx index 2fe861915c..6c0f1a5b7b 100644 --- a/tests/unit/mesh/test_coordinates.cxx +++ b/tests/unit/mesh/test_coordinates.cxx @@ -143,12 +143,14 @@ TEST_F(CoordinatesTest, CalcContravariant) { coords.calcCovariant(); output_info.enable(); - EXPECT_TRUE(IsFieldEqual(coords.g_11, 1.0)); - EXPECT_TRUE(IsFieldEqual(coords.g_22, 1.0)); - EXPECT_TRUE(IsFieldEqual(coords.g_33, 1.0)); - EXPECT_TRUE(IsFieldEqual(coords.g_12, 0.0)); - EXPECT_TRUE(IsFieldEqual(coords.g_13, 0.0)); - EXPECT_TRUE(IsFieldEqual(coords.g_23, 0.0)); + Coordinates::MetricTensor updated_coords = coords.getCovariantMetricTensor(); + + EXPECT_TRUE(IsFieldEqual(updated_coords.g11, 1.0)); + EXPECT_TRUE(IsFieldEqual(updated_coords.g22, 1.0)); + EXPECT_TRUE(IsFieldEqual(updated_coords.g33, 1.0)); + EXPECT_TRUE(IsFieldEqual(updated_coords.g12, 0.0)); + EXPECT_TRUE(IsFieldEqual(updated_coords.g13, 0.0)); + EXPECT_TRUE(IsFieldEqual(updated_coords.g23, 0.0)); } TEST_F(CoordinatesTest, CalcCovariant) { @@ -281,9 +283,10 @@ TEST_F(CoordinatesTest, ConstructWithDiagonalContravariantMetric) { // Covariant metric should be inverse // Note: Not calculated in corners - EXPECT_TRUE(IsFieldEqual(coords.g_11, 1. / 2.0, "RGN_NOCORNERS")); - EXPECT_TRUE(IsFieldEqual(coords.g_22, 1. / 3.2, "RGN_NOCORNERS")); - EXPECT_TRUE(IsFieldEqual(coords.g_33, 1. / 42, "RGN_NOCORNERS")); + Coordinates::MetricTensor updated_coords = coords.getCovariantMetricTensor(); + EXPECT_TRUE(IsFieldEqual(updated_coords.g11, 1. / 2.0, "RGN_NOCORNERS")); + EXPECT_TRUE(IsFieldEqual(updated_coords.g22, 1. / 3.2, "RGN_NOCORNERS")); + EXPECT_TRUE(IsFieldEqual(updated_coords.g33, 1. / 42, "RGN_NOCORNERS")); EXPECT_TRUE(IsFieldEqual(coords.J, 1. / sqrt(2.0 * 3.2 * 42), "RGN_NOCORNERS")); EXPECT_TRUE(IsFieldEqual(coords.Bxy, sqrt(2.0 * 42), "RGN_NOCORNERS", 1e-10)); From dc0666a6291ef0ed67f81405d0d347149143ed36 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Mon, 18 Sep 2023 14:43:51 +0100 Subject: [PATCH 034/491] Correct unit tests names. --- tests/unit/mesh/test_coordinates.cxx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/unit/mesh/test_coordinates.cxx b/tests/unit/mesh/test_coordinates.cxx index 6c0f1a5b7b..cc40d50a51 100644 --- a/tests/unit/mesh/test_coordinates.cxx +++ b/tests/unit/mesh/test_coordinates.cxx @@ -314,7 +314,7 @@ TEST_F(CoordinatesTest, NegativeB) { output_info.enable(); } -TEST_F(CoordinatesTest, GetCovariantMetricTensor) { +TEST_F(CoordinatesTest, GetContravariantMetricTensor) { Coordinates coords{mesh, FieldMetric{1.0}, // dx FieldMetric{1.0}, // dy @@ -346,7 +346,7 @@ TEST_F(CoordinatesTest, GetCovariantMetricTensor) { EXPECT_TRUE(IsFieldEqual(contravariant_components.g23, 6.7)); } -TEST_F(CoordinatesTest, SetCovariantMetricTensor) { +TEST_F(CoordinatesTest, SetContravariantMetricTensor) { // Set initial values for the metric tensor in the Coordinates constructor Coordinates coords{mesh, FieldMetric{1.0}, // dx From f2be6decdc6d8e02fd04bf28d3b247fd0d20a6dd Mon Sep 17 00:00:00 2001 From: tomc271 Date: Mon, 18 Sep 2023 14:45:09 +0100 Subject: [PATCH 035/491] Add unit tests for getCovariantMetricTensor() and setCovariantMetricTensor(). --- tests/unit/mesh/test_coordinates.cxx | 77 ++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) diff --git a/tests/unit/mesh/test_coordinates.cxx b/tests/unit/mesh/test_coordinates.cxx index cc40d50a51..89a8123dc4 100644 --- a/tests/unit/mesh/test_coordinates.cxx +++ b/tests/unit/mesh/test_coordinates.cxx @@ -387,4 +387,81 @@ TEST_F(CoordinatesTest, SetContravariantMetricTensor) { EXPECT_TRUE(IsFieldEqual(g.g12, 0.9)); EXPECT_TRUE(IsFieldEqual(g.g13, 5.7)); EXPECT_TRUE(IsFieldEqual(g.g23, 1.9)); +} + +TEST_F(CoordinatesTest, GetCovariantMetricTensor) { + Coordinates coords{mesh, + FieldMetric{1.0}, // dx + FieldMetric{1.0}, // dy + FieldMetric{1.0}, // dz + FieldMetric{0.0}, // J + FieldMetric{0.0}, // Bxy + FieldMetric{1.2}, // g11 + FieldMetric{2.3}, // g22 + FieldMetric{3.4}, // g33 + FieldMetric{4.5}, // g12 + FieldMetric{5.6}, // g13 + FieldMetric{6.7}, // g23 + FieldMetric{9.7}, // g_11 + FieldMetric{7.5}, // g_22 + FieldMetric{4.7}, // g_23 + FieldMetric{3.9}, // g_12 + FieldMetric{1.7}, // g_13 + FieldMetric{5.3}, // g_23 + FieldMetric{0.0}, // ShiftTorsion + FieldMetric{0.0}}; // IntShiftTorsion + + Coordinates::MetricTensor const covariant_components = coords.getCovariantMetricTensor(); + + EXPECT_TRUE(IsFieldEqual(covariant_components.g11, 9.7)); + EXPECT_TRUE(IsFieldEqual(covariant_components.g22, 7.5)); + EXPECT_TRUE(IsFieldEqual(covariant_components.g33, 4.7)); + EXPECT_TRUE(IsFieldEqual(covariant_components.g12, 3.9)); + EXPECT_TRUE(IsFieldEqual(covariant_components.g13, 1.7)); + EXPECT_TRUE(IsFieldEqual(covariant_components.g23, 5.3)); +} + +TEST_F(CoordinatesTest, SetCovariantMetricTensor) { + { + // Set initial values for the metric tensor in the Coordinates constructor + Coordinates coords{mesh, + FieldMetric{1.0}, // dx + FieldMetric{1.0}, // dy + FieldMetric{1.0}, // dz + FieldMetric{0.0}, // J + FieldMetric{0.0}, // Bxy + FieldMetric{0.0}, // g11 + FieldMetric{0.0}, // g22 + FieldMetric{0.0}, // g33 + FieldMetric{0.0}, // g12 + FieldMetric{0.0}, // g13 + FieldMetric{0.0}, // g23 + FieldMetric{1.0}, // g_11 + FieldMetric{1.0}, // g_22 + FieldMetric{1.0}, // g_23 + FieldMetric{0.0}, // g_12 + FieldMetric{0.0}, // g_13 + FieldMetric{0.0}, // g_23 + FieldMetric{0.0}, // ShiftTorsion + FieldMetric{0.0}}; // IntShiftTorsion + + // Modify with setter + Coordinates::MetricTensor updated_metric_tensor; + updated_metric_tensor.g11 = 1.7; + updated_metric_tensor.g22 = 2.3; + updated_metric_tensor.g33 = 3.1; + updated_metric_tensor.g12 = 0.9; + updated_metric_tensor.g13 = 5.7; + updated_metric_tensor.g23 = 1.9; + coords.setCovariantMetricTensor(updated_metric_tensor); + + // Get values with getter and check they have been modified as expected + Coordinates::MetricTensor g = coords.getCovariantMetricTensor(); + EXPECT_TRUE(IsFieldEqual(g.g11, 1.7)); + EXPECT_TRUE(IsFieldEqual(g.g22, 2.3)); + EXPECT_TRUE(IsFieldEqual(g.g33, 3.1)); + EXPECT_TRUE(IsFieldEqual(g.g12, 0.9)); + EXPECT_TRUE(IsFieldEqual(g.g13, 5.7)); + EXPECT_TRUE(IsFieldEqual(g.g23, 1.9)); + } } \ No newline at end of file From bf39b2d4fbed4cc7adbc5e590d4b4fcf1ae3c586 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Mon, 18 Sep 2023 14:46:01 +0100 Subject: [PATCH 036/491] Update MMS and integrated tests to use setCovariantMetricTensor(). --- tests/MMS/GBS/gbs.cxx | 14 +++++---- tests/MMS/diffusion2/diffusion.cxx | 15 ++++++---- tests/MMS/wave-1d/wave.cxx | 15 ++++++---- .../test-interchange-instability/2fluid.cxx | 30 ++++++++++--------- 4 files changed, 42 insertions(+), 32 deletions(-) diff --git a/tests/MMS/GBS/gbs.cxx b/tests/MMS/GBS/gbs.cxx index 6613694da3..ea564f5286 100644 --- a/tests/MMS/GBS/gbs.cxx +++ b/tests/MMS/GBS/gbs.cxx @@ -361,12 +361,14 @@ void GBS::LoadMetric(BoutReal Lnorm, BoutReal Bnorm) { coords->J = hthe / Bpxy; - coords->g_11 = 1.0 / contravariant_components.g11 + SQ(sinty * Rxy); - coords->g_22 = SQ(coords->Bxy * hthe / Bpxy); - coords->g_33 = Rxy * Rxy; - coords->g_12 = sbp * Btxy * hthe * sinty * Rxy / Bpxy; - coords->g_13 = sinty * Rxy * Rxy; - coords->g_23 = sbp * Btxy * hthe * Rxy / Bpxy; + Coordinates::MetricTensor covariant_components; + covariant_components.g11 = 1.0 / contravariant_components.g11 + SQ(sinty * Rxy); + covariant_components.g22 = SQ(coords->Bxy * hthe / Bpxy); + covariant_components.g33 = Rxy * Rxy; + covariant_components.g12 = sbp * Btxy * hthe * sinty * Rxy / Bpxy; + covariant_components.g13 = sinty * Rxy * Rxy; + covariant_components.g23 = sbp * Btxy * hthe * Rxy / Bpxy; + coords->setCovariantMetricTensor(covariant_components); coords->geometry(); } diff --git a/tests/MMS/diffusion2/diffusion.cxx b/tests/MMS/diffusion2/diffusion.cxx index 757c2e62b6..3dd77c06f8 100644 --- a/tests/MMS/diffusion2/diffusion.cxx +++ b/tests/MMS/diffusion2/diffusion.cxx @@ -47,12 +47,15 @@ class Diffusion : public PhysicsModel { contravariant_components.g23 = 0.0; coords->setContravariantMetricTensor(contravariant_components); - coords->g_11 = 1.0; - coords->g_22 = 1.0; - coords->g_33 = 1.0; - coords->g_12 = 0.0; - coords->g_13 = 0.0; - coords->g_23 = 0.0; + Coordinates::MetricTensor covariant_components; + covariant_components.g11 = 1.0; + covariant_components.g22 = 1.0; + covariant_components.g33 = 1.0; + covariant_components.g12 = 0.0; + covariant_components.g13 = 0.0; + covariant_components.g23 = 0.0; + coords->setCovariantMetricTensor(covariant_components); + coords->geometry(); // Tell BOUT++ to solve N diff --git a/tests/MMS/wave-1d/wave.cxx b/tests/MMS/wave-1d/wave.cxx index bd1be3cf82..69ea122160 100644 --- a/tests/MMS/wave-1d/wave.cxx +++ b/tests/MMS/wave-1d/wave.cxx @@ -41,12 +41,15 @@ class Wave1D : public PhysicsModel { contravariant_components.g23 = 0.0; coord->setContravariantMetricTensor(contravariant_components); - coord->g_11 = 1.0; - coord->g_22 = 1.0; - coord->g_33 = 1.0; - coord->g_12 = 0.0; - coord->g_13 = 0.0; - coord->g_23 = 0.0; + Coordinates::MetricTensor covariant_components; + covariant_components.g11 = 1.0; + covariant_components.g22 = 1.0; + covariant_components.g33 = 1.0; + covariant_components.g12 = 0.0; + covariant_components.g13 = 0.0; + covariant_components.g23 = 0.0; + coord->setCovariantMetricTensor(covariant_components); + coord->geometry(); g.setLocation(CELL_XLOW); // g staggered to the left of f diff --git a/tests/integrated/test-interchange-instability/2fluid.cxx b/tests/integrated/test-interchange-instability/2fluid.cxx index e4d287480a..139fd8a361 100644 --- a/tests/integrated/test-interchange-instability/2fluid.cxx +++ b/tests/integrated/test-interchange-instability/2fluid.cxx @@ -139,23 +139,25 @@ class Interchange : public PhysicsModel { /**************** CALCULATE METRICS ******************/ - Coordinates::MetricTensor metric_tensor; - metric_tensor.g11 = SQ(Rxy * Bpxy); - metric_tensor.g22 = 1.0 / SQ(hthe); - metric_tensor.g33 = SQ(I) * metric_tensor.g11 + SQ(coord->Bxy) / metric_tensor.g11; - metric_tensor.g12 = 0.0; - metric_tensor.g13 = -I * metric_tensor.g11; - metric_tensor.g23 = -Btxy / (hthe * Bpxy * Rxy); - coord->setContravariantMetricTensor(metric_tensor); + Coordinates::MetricTensor contravariant_components; + contravariant_components.g11 = SQ(Rxy * Bpxy); + contravariant_components.g22 = 1.0 / SQ(hthe); + contravariant_components.g33 = SQ(I) * contravariant_components.g11 + SQ(coord->Bxy) / contravariant_components.g11; + contravariant_components.g12 = 0.0; + contravariant_components.g13 = -I * contravariant_components.g11; + contravariant_components.g23 = -Btxy / (hthe * Bpxy * Rxy); + coord->setContravariantMetricTensor(contravariant_components); coord->J = hthe / Bpxy; - coord->g_11 = 1.0 / metric_tensor.g11 + SQ(I * Rxy); - coord->g_22 = SQ(coord->Bxy * hthe / Bpxy); - coord->g_33 = Rxy * Rxy; - coord->g_12 = Btxy * hthe * I * Rxy / Bpxy; - coord->g_13 = I * Rxy * Rxy; - coord->g_23 = Btxy * hthe * Rxy / Bpxy; + Coordinates::MetricTensor covariant_components; + covariant_components.g11 = 1.0 / contravariant_components.g11 + SQ(I * Rxy); + covariant_components.g22 = SQ(coord->Bxy * hthe / Bpxy); + covariant_components.g33 = Rxy * Rxy; + covariant_components.g12 = Btxy * hthe * I * Rxy / Bpxy; + covariant_components.g13 = I * Rxy * Rxy; + covariant_components.g23 = Btxy * hthe * Rxy / Bpxy; + coord->setCovariantMetricTensor(covariant_components); coord->geometry(); From 122c6b4208999e216b743b5f295f3ef0c42dd116 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Mon, 18 Sep 2023 19:39:49 +0100 Subject: [PATCH 037/491] Update test_laplace_cyclic to access the covariant components of the metric tensor through Coordinates->getCovariantMetricTensor(). --- tests/unit/invert/laplace/test_laplace_cyclic.cxx | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/tests/unit/invert/laplace/test_laplace_cyclic.cxx b/tests/unit/invert/laplace/test_laplace_cyclic.cxx index f25a23f897..279e124030 100644 --- a/tests/unit/invert/laplace/test_laplace_cyclic.cxx +++ b/tests/unit/invert/laplace/test_laplace_cyclic.cxx @@ -39,9 +39,9 @@ class CyclicForwardOperator { } const Field3D operator()(Field3D& f) { - Coordinates::MetricTensor g = coords->getContravariantMetricTensor(); + Coordinates::MetricTensor contravariant_components = coords->getContravariantMetricTensor(); auto result = d * Delp2(f) - + (g.g11 * DDX(f) + g.g13 * DDZ(f)) * DDX(c2) / c1 + a * f + + (contravariant_components.g11 * DDX(f) + contravariant_components.g13 * DDZ(f)) * DDX(c2) / c1 + a * f + ex * DDX(f) + ez * DDZ(f); applyBoundaries(result, f); return result; @@ -52,9 +52,10 @@ class CyclicForwardOperator { bool inner_x_neumann, outer_x_neumann; // If false then use Dirichlet conditions void applyBoundaries(Field3D& newF, const Field3D& f) { + Coordinates::MetricTensor covariant_components = coords->getCovariantMetricTensor(); BOUT_FOR(i, f.getMesh()->getRegion3D("RGN_INNER_X")) { if (inner_x_neumann) { - newF[i] = (f[i.xp()] - f[i]) / coords->dx[i] / sqrt(coords->g_11[i]); + newF[i] = (f[i.xp()] - f[i]) / coords->dx[i] / sqrt(covariant_components.g11[i]); } else { newF[i] = 0.5 * (f[i] + f[i.xp()]); } @@ -62,7 +63,7 @@ class CyclicForwardOperator { BOUT_FOR(i, f.getMesh()->getRegion3D("RGN_OUTER_X")) { if (outer_x_neumann) { - newF[i] = (f[i] - f[i.xm()]) / coords->dx[i] / sqrt(coords->g_11[i]); + newF[i] = (f[i] - f[i.xm()]) / coords->dx[i] / sqrt(covariant_components.g11[i]); } else { newF[i] = 0.5 * (f[i.xm()] + f[i]); } From 36d75300a0abe6b7478cb658de16282a5af34c79 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Mon, 18 Sep 2023 19:41:31 +0100 Subject: [PATCH 038/491] Update test-drift-instability/2fluid to set the covariant components of the metric tensor through Coordinates->setCovariantMetricTensor(). --- .../test-drift-instability/2fluid.cxx | 30 ++++++++++--------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/tests/integrated/test-drift-instability/2fluid.cxx b/tests/integrated/test-drift-instability/2fluid.cxx index e33cf0f703..8c85f0f410 100644 --- a/tests/integrated/test-drift-instability/2fluid.cxx +++ b/tests/integrated/test-drift-instability/2fluid.cxx @@ -217,23 +217,25 @@ class TwoFluid : public PhysicsModel { /**************** CALCULATE METRICS ******************/ - Coordinates::MetricTensor metric_tensor; - metric_tensor.g11 = SQ(Rxy * Bpxy); - metric_tensor.g22 = 1.0 / SQ(hthe); - metric_tensor.g33 = SQ(I) * metric_tensor.g11 + SQ(coord->Bxy) / metric_tensor.g11; - metric_tensor.g12 = 0.0; - metric_tensor.g13 = -I * metric_tensor.g11; - metric_tensor.g23 = -Btxy / (hthe * Bpxy * Rxy); - coord->setContravariantMetricTensor(metric_tensor); + Coordinates::MetricTensor contravariant_components; + contravariant_components.g11 = SQ(Rxy * Bpxy); + contravariant_components.g22 = 1.0 / SQ(hthe); + contravariant_components.g33 = SQ(I) * contravariant_components.g11 + SQ(coord->Bxy) / contravariant_components.g11; + contravariant_components.g12 = 0.0; + contravariant_components.g13 = -I * contravariant_components.g11; + contravariant_components.g23 = -Btxy / (hthe * Bpxy * Rxy); + coord->setContravariantMetricTensor(contravariant_components); coord->J = hthe / Bpxy; - coord->g_11 = 1.0 / metric_tensor.g11 + SQ(I * Rxy); - coord->g_22 = SQ(coord->Bxy * hthe / Bpxy); - coord->g_33 = Rxy * Rxy; - coord->g_12 = Btxy * hthe * I * Rxy / Bpxy; - coord->g_13 = I * Rxy * Rxy; - coord->g_23 = Btxy * hthe * Rxy / Bpxy; + Coordinates::MetricTensor covariant_components; + covariant_components.g11 = 1.0 / contravariant_components.g11 + SQ(I * Rxy); + covariant_components.g22 = SQ(coord->Bxy * hthe / Bpxy); + covariant_components.g33 = Rxy * Rxy; + covariant_components.g12 = Btxy * hthe * I * Rxy / Bpxy; + covariant_components.g13 = I * Rxy * Rxy; + covariant_components.g23 = Btxy * hthe * Rxy / Bpxy; + coord->setCovariantMetricTensor(covariant_components); coord->geometry(); From ec99056c91d81805a2eaefc2ecfa5433815a92ba Mon Sep 17 00:00:00 2001 From: tomc271 Date: Mon, 18 Sep 2023 19:51:18 +0100 Subject: [PATCH 039/491] Update test_multigrid_laplace to access the covariant components of the metric tensor through Coordinates->getCovariantMetricTensor(). --- .../test-multigrid_laplace/test_multigrid_laplace.cxx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/integrated/test-multigrid_laplace/test_multigrid_laplace.cxx b/tests/integrated/test-multigrid_laplace/test_multigrid_laplace.cxx index 7b2e4df465..e984087514 100644 --- a/tests/integrated/test-multigrid_laplace/test_multigrid_laplace.cxx +++ b/tests/integrated/test-multigrid_laplace/test_multigrid_laplace.cxx @@ -242,7 +242,7 @@ int main(int argc, char** argv) { x0(mesh->xstart - 1, mesh->ystart, k) = (f4(mesh->xstart, mesh->ystart, k) - f4(mesh->xstart - 1, mesh->ystart, k)) / mesh->getCoordinates()->dx(mesh->xstart, mesh->ystart, k) - / sqrt(mesh->getCoordinates()->g_11(mesh->xstart, mesh->ystart, k)); + / sqrt(mesh->getCoordinates()->getCovariantMetricTensor().g11(mesh->xstart, mesh->ystart, k)); } } if (mesh->lastX()) { @@ -250,7 +250,7 @@ int main(int argc, char** argv) { x0(mesh->xend + 1, mesh->ystart, k) = (f4(mesh->xend + 1, mesh->ystart, k) - f4(mesh->xend, mesh->ystart, k)) / mesh->getCoordinates()->dx(mesh->xend, mesh->ystart, k) - / sqrt(mesh->getCoordinates()->g_11(mesh->xend, mesh->ystart, k)); + / sqrt(mesh->getCoordinates()->getCovariantMetricTensor().g11(mesh->xend, mesh->ystart, k)); } } From 07333422092038edd595928f788e80c78bf0ca72 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Mon, 18 Sep 2023 19:55:49 +0100 Subject: [PATCH 040/491] Update test_naulin_laplace to access the covariant components of the metric tensor through Coordinates->getCovariantMetricTensor(). --- tests/integrated/test-naulin-laplace/test_naulin_laplace.cxx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/integrated/test-naulin-laplace/test_naulin_laplace.cxx b/tests/integrated/test-naulin-laplace/test_naulin_laplace.cxx index 3cc2283fdb..9eab3f9697 100644 --- a/tests/integrated/test-naulin-laplace/test_naulin_laplace.cxx +++ b/tests/integrated/test-naulin-laplace/test_naulin_laplace.cxx @@ -246,7 +246,7 @@ int main(int argc, char** argv) { x0(mesh->xstart - 1, mesh->ystart, k) = (f4(mesh->xstart, mesh->ystart, k) - f4(mesh->xstart - 1, mesh->ystart, k)) / mesh->getCoordinates()->dx(mesh->xstart, mesh->ystart, k) - / sqrt(mesh->getCoordinates()->g_11(mesh->xstart, mesh->ystart, k)); + / sqrt(mesh->getCoordinates()->getCovariantMetricTensor().g11(mesh->xstart, mesh->ystart, k)); } } if (mesh->lastX()) { @@ -254,7 +254,7 @@ int main(int argc, char** argv) { x0(mesh->xend + 1, mesh->ystart, k) = (f4(mesh->xend + 1, mesh->ystart, k) - f4(mesh->xend, mesh->ystart, k)) / mesh->getCoordinates()->dx(mesh->xend, mesh->ystart, k) - / sqrt(mesh->getCoordinates()->g_11(mesh->xend, mesh->ystart, k)); + / sqrt(mesh->getCoordinates()->getCovariantMetricTensor().g11(mesh->xend, mesh->ystart, k)); } } From c1669cc74a9c9902aecbe0666256fb1aa9a48824 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Mon, 18 Sep 2023 20:20:34 +0100 Subject: [PATCH 041/491] Extract method CalculateChristoffelSymbols(). --- include/bout/coordinates.hxx | 5 +- src/mesh/coordinates.cxx | 127 ++++++++++++++++++----------------- 2 files changed, 68 insertions(+), 64 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index 13aa3cc2c8..7b619b0cc3 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -134,12 +134,13 @@ public: /// Calculate differential geometry quantities from the metric tensor int geometry(bool recalculate_staggered = true, bool force_interpolate_from_centre = false); - /// Invert contravatiant metric to get covariant components + /// Invert contravariant metric to get covariant components int calcCovariant(const std::string& region = "RGN_ALL"); /// Invert covariant metric to get contravariant components int calcContravariant(const std::string& region = "RGN_ALL"); int jacobian(); ///< Calculate J and Bxy - + void CalculateChristoffelSymbols(); /// Calculate Christoffel symbol terms + /////////////////////////////////////////////////////////// // Parallel transforms /////////////////////////////////////////////////////////// diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 583c019938..9b1f3f3278 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -964,68 +964,7 @@ int Coordinates::geometry(bool recalculate_staggered, // Check input metrics checkContravariant(); checkCovariant(); - - // Calculate Christoffel symbol terms (18 independent values) - // Note: This calculation is completely general: metric - // tensor can be 2D or 3D. For 2D, all DDZ terms are zero - - G1_11 = 0.5 * g11 * DDX(g_11) + g12 * (DDX(g_12) - 0.5 * DDY(g_11)) - + g13 * (DDX(g_13) - 0.5 * DDZ(g_11)); - G1_22 = g11 * (DDY(g_12) - 0.5 * DDX(g_22)) + 0.5 * g12 * DDY(g_22) - + g13 * (DDY(g_23) - 0.5 * DDZ(g_22)); - G1_33 = g11 * (DDZ(g_13) - 0.5 * DDX(g_33)) + g12 * (DDZ(g_23) - 0.5 * DDY(g_33)) - + 0.5 * g13 * DDZ(g_33); - G1_12 = 0.5 * g11 * DDY(g_11) + 0.5 * g12 * DDX(g_22) - + 0.5 * g13 * (DDY(g_13) + DDX(g_23) - DDZ(g_12)); - G1_13 = 0.5 * g11 * DDZ(g_11) + 0.5 * g12 * (DDZ(g_12) + DDX(g_23) - DDY(g_13)) - + 0.5 * g13 * DDX(g_33); - G1_23 = 0.5 * g11 * (DDZ(g_12) + DDY(g_13) - DDX(g_23)) - + 0.5 * g12 * (DDZ(g_22) + DDY(g_23) - DDY(g_23)) - // + 0.5 *g13*(DDZ(g_32) + DDY(g_33) - DDZ(g_23)); - // which equals - + 0.5 * g13 * DDY(g_33); - - G2_11 = 0.5 * g12 * DDX(g_11) + g22 * (DDX(g_12) - 0.5 * DDY(g_11)) - + g23 * (DDX(g_13) - 0.5 * DDZ(g_11)); - G2_22 = g12 * (DDY(g_12) - 0.5 * DDX(g_22)) + 0.5 * g22 * DDY(g_22) - + g23 * (DDY(g23) - 0.5 * DDZ(g_22)); - G2_33 = g12 * (DDZ(g_13) - 0.5 * DDX(g_33)) + g22 * (DDZ(g_23) - 0.5 * DDY(g_33)) - + 0.5 * g23 * DDZ(g_33); - G2_12 = 0.5 * g12 * DDY(g_11) + 0.5 * g22 * DDX(g_22) - + 0.5 * g23 * (DDY(g_13) + DDX(g_23) - DDZ(g_12)); - G2_13 = - // 0.5 *g21*(DDZ(g_11) + DDX(g_13) - DDX(g_13)) - // which equals - 0.5 * g12 * (DDZ(g_11) + DDX(g_13) - DDX(g_13)) - // + 0.5 *g22*(DDZ(g_21) + DDX(g_23) - DDY(g_13)) - // which equals - + 0.5 * g22 * (DDZ(g_12) + DDX(g_23) - DDY(g_13)) - // + 0.5 *g23*(DDZ(g_31) + DDX(g_33) - DDZ(g_13)); - // which equals - + 0.5 * g23 * DDX(g_33); - G2_23 = 0.5 * g12 * (DDZ(g_12) + DDY(g_13) - DDX(g_23)) + 0.5 * g22 * DDZ(g_22) - + 0.5 * g23 * DDY(g_33); - - G3_11 = 0.5 * g13 * DDX(g_11) + g23 * (DDX(g_12) - 0.5 * DDY(g_11)) - + g33 * (DDX(g_13) - 0.5 * DDZ(g_11)); - G3_22 = g13 * (DDY(g_12) - 0.5 * DDX(g_22)) + 0.5 * g23 * DDY(g_22) - + g33 * (DDY(g_23) - 0.5 * DDZ(g_22)); - G3_33 = g13 * (DDZ(g_13) - 0.5 * DDX(g_33)) + g23 * (DDZ(g_23) - 0.5 * DDY(g_33)) - + 0.5 * g33 * DDZ(g_33); - G3_12 = - // 0.5 *g31*(DDY(g_11) + DDX(g_12) - DDX(g_12)) - // which equals to - 0.5 * g13 * DDY(g_11) - // + 0.5 *g32*(DDY(g_21) + DDX(g_22) - DDY(g_12)) - // which equals to - + 0.5 * g23 * DDX(g_22) - //+ 0.5 *g33*(DDY(g_31) + DDX(g_32) - DDZ(g_12)); - // which equals to - + 0.5 * g33 * (DDY(g_13) + DDX(g_23) - DDZ(g_12)); - G3_13 = 0.5 * g13 * DDZ(g_11) + 0.5 * g23 * (DDZ(g_12) + DDX(g_23) - DDY(g_13)) - + 0.5 * g33 * DDX(g_33); - G3_23 = 0.5 * g13 * (DDZ(g_12) + DDY(g_13) - DDX(g_23)) + 0.5 * g23 * DDZ(g_22) - + 0.5 * g33 * DDY(g_33); + CalculateChristoffelSymbols(); auto tmp = J * g12; communicate(tmp); @@ -1211,6 +1150,70 @@ int Coordinates::geometry(bool recalculate_staggered, return 0; } +void Coordinates::CalculateChristoffelSymbols() { + // Calculate Christoffel symbol terms (18 independent values) + // Note: This calculation is completely general: metric + // tensor can be 2D or 3D. For 2D, all DDZ terms are zero + + G1_11 = 0.5 * g11 * DDX(g_11) + g12 * (DDX(g_12) - 0.5 * DDY(g_11)) + + g13 * (DDX(g_13) - 0.5 * DDZ(g_11)); + G1_22 = g11 * (DDY(g_12) - 0.5 * DDX(g_22)) + 0.5 * g12 * DDY(g_22) + + g13 * (DDY(g_23) - 0.5 * DDZ(g_22)); + G1_33 = g11 * (DDZ(g_13) - 0.5 * DDX(g_33)) + g12 * (DDZ(g_23) - 0.5 * DDY(g_33)) + + 0.5 * g13 * DDZ(g_33); + G1_12 = 0.5 * g11 * DDY(g_11) + 0.5 * g12 * DDX(g_22) + + 0.5 * g13 * (DDY(g_13) + DDX(g_23) - DDZ(g_12)); + G1_13 = 0.5 * g11 * DDZ(g_11) + 0.5 * g12 * (DDZ(g_12) + DDX(g_23) - DDY(g_13)) + + 0.5 * g13 * DDX(g_33); + G1_23 = 0.5 * g11 * (DDZ(g_12) + DDY(g_13) - DDX(g_23)) + + 0.5 * g12 * (DDZ(g_22) + DDY(g_23) - DDY(g_23)) + // + 0.5 *g13*(DDZ(g_32) + DDY(g_33) - DDZ(g_23)); + // which equals + + 0.5 * g13 * DDY(g_33); + + G2_11 = 0.5 * g12 * DDX(g_11) + g22 * (DDX(g_12) - 0.5 * DDY(g_11)) + + g23 * (DDX(g_13) - 0.5 * DDZ(g_11)); + G2_22 = g12 * (DDY(g_12) - 0.5 * DDX(g_22)) + 0.5 * g22 * DDY(g_22) + + g23 * (DDY(g23) - 0.5 * DDZ(g_22)); + G2_33 = g12 * (DDZ(g_13) - 0.5 * DDX(g_33)) + g22 * (DDZ(g_23) - 0.5 * DDY(g_33)) + + 0.5 * g23 * DDZ(g_33); + G2_12 = 0.5 * g12 * DDY(g_11) + 0.5 * g22 * DDX(g_22) + + 0.5 * g23 * (DDY(g_13) + DDX(g_23) - DDZ(g_12)); + G2_13 = + // 0.5 *g21*(DDZ(g_11) + DDX(g_13) - DDX(g_13)) + // which equals + 0.5 * g12 * (DDZ(g_11) + DDX(g_13) - DDX(g_13)) + // + 0.5 *g22*(DDZ(g_21) + DDX(g_23) - DDY(g_13)) + // which equals + + 0.5 * g22 * (DDZ(g_12) + DDX(g_23) - DDY(g_13)) + // + 0.5 *g23*(DDZ(g_31) + DDX(g_33) - DDZ(g_13)); + // which equals + + 0.5 * g23 * DDX(g_33); + G2_23 = 0.5 * g12 * (DDZ(g_12) + DDY(g_13) - DDX(g_23)) + 0.5 * g22 * DDZ(g_22) + + 0.5 * g23 * DDY(g_33); + + G3_11 = 0.5 * g13 * DDX(g_11) + g23 * (DDX(g_12) - 0.5 * DDY(g_11)) + + g33 * (DDX(g_13) - 0.5 * DDZ(g_11)); + G3_22 = g13 * (DDY(g_12) - 0.5 * DDX(g_22)) + 0.5 * g23 * DDY(g_22) + + g33 * (DDY(g_23) - 0.5 * DDZ(g_22)); + G3_33 = g13 * (DDZ(g_13) - 0.5 * DDX(g_33)) + g23 * (DDZ(g_23) - 0.5 * DDY(g_33)) + + 0.5 * g33 * DDZ(g_33); + G3_12 = + // 0.5 *g31*(DDY(g_11) + DDX(g_12) - DDX(g_12)) + // which equals to + 0.5 * g13 * DDY(g_11) + // + 0.5 *g32*(DDY(g_21) + DDX(g_22) - DDY(g_12)) + // which equals to + + 0.5 * g23 * DDX(g_22) + //+ 0.5 *g33*(DDY(g_31) + DDX(g_32) - DDZ(g_12)); + // which equals to + + 0.5 * g33 * (DDY(g_13) + DDX(g_23) - DDZ(g_12)); + G3_13 = 0.5 * g13 * DDZ(g_11) + 0.5 * g23 * (DDZ(g_12) + DDX(g_23) - DDY(g_13)) + + 0.5 * g33 * DDX(g_33); + G3_23 = 0.5 * g13 * (DDZ(g_12) + DDY(g_13) - DDX(g_23)) + 0.5 * g23 * DDZ(g_22) + + 0.5 * g33 * DDY(g_33); +} + int Coordinates::calcCovariant(const std::string& region) { TRACE("Coordinates::calcCovariant"); From 49c10033c3582c9182fc47ec056ed6b4ae0fde17 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Tue, 19 Sep 2023 20:07:18 +0100 Subject: [PATCH 042/491] Use const auto instead of specific type name. --- tests/unit/mesh/test_coordinates.cxx | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/tests/unit/mesh/test_coordinates.cxx b/tests/unit/mesh/test_coordinates.cxx index 89a8123dc4..95fdc2e8e9 100644 --- a/tests/unit/mesh/test_coordinates.cxx +++ b/tests/unit/mesh/test_coordinates.cxx @@ -143,7 +143,7 @@ TEST_F(CoordinatesTest, CalcContravariant) { coords.calcCovariant(); output_info.enable(); - Coordinates::MetricTensor updated_coords = coords.getCovariantMetricTensor(); + const auto updated_coords = coords.getCovariantMetricTensor(); EXPECT_TRUE(IsFieldEqual(updated_coords.g11, 1.0)); EXPECT_TRUE(IsFieldEqual(updated_coords.g22, 1.0)); @@ -180,7 +180,7 @@ TEST_F(CoordinatesTest, CalcCovariant) { coords.calcContravariant(); output_info.enable(); - Coordinates::MetricTensor g = coords.getContravariantMetricTensor(); + const auto g = coords.getContravariantMetricTensor(); EXPECT_TRUE(IsFieldEqual(g.g11, 1.0)); EXPECT_TRUE(IsFieldEqual(g.g22, 1.0)); @@ -202,7 +202,7 @@ TEST_F(CoordinatesTest, DefaultConstructor) { EXPECT_TRUE(IsFieldEqual(coords.dy, 1.0)); EXPECT_TRUE(IsFieldEqual(coords.dz, default_dz)); - Coordinates::MetricTensor g = coords.getContravariantMetricTensor(); + const auto g = coords.getContravariantMetricTensor(); EXPECT_TRUE(IsFieldEqual(g.g11, 1.0)); EXPECT_TRUE(IsFieldEqual(g.g22, 1.0)); @@ -230,7 +230,7 @@ TEST_F(CoordinatesTest, ConstructWithMeshSpacing) { EXPECT_TRUE(IsFieldEqual(coords.dy, 3.2)); EXPECT_TRUE(IsFieldEqual(coords.dz, 42.)); - Coordinates::MetricTensor g = coords.getContravariantMetricTensor(); + const auto g = coords.getContravariantMetricTensor(); EXPECT_TRUE(IsFieldEqual(g.g11, 1.0)); EXPECT_TRUE(IsFieldEqual(g.g22, 1.0)); @@ -273,7 +273,7 @@ TEST_F(CoordinatesTest, ConstructWithDiagonalContravariantMetric) { EXPECT_TRUE(IsFieldEqual(coords.dz, default_dz)); // Diagonal contravariant metric - Coordinates::MetricTensor g = coords.getContravariantMetricTensor(); + const auto g = coords.getContravariantMetricTensor(); EXPECT_TRUE(IsFieldEqual(g.g11, 2.0)); EXPECT_TRUE(IsFieldEqual(g.g22, 3.2)); EXPECT_TRUE(IsFieldEqual(g.g33, 42)); @@ -283,7 +283,7 @@ TEST_F(CoordinatesTest, ConstructWithDiagonalContravariantMetric) { // Covariant metric should be inverse // Note: Not calculated in corners - Coordinates::MetricTensor updated_coords = coords.getCovariantMetricTensor(); + const auto updated_coords = coords.getCovariantMetricTensor(); EXPECT_TRUE(IsFieldEqual(updated_coords.g11, 1. / 2.0, "RGN_NOCORNERS")); EXPECT_TRUE(IsFieldEqual(updated_coords.g22, 1. / 3.2, "RGN_NOCORNERS")); EXPECT_TRUE(IsFieldEqual(updated_coords.g33, 1. / 42, "RGN_NOCORNERS")); @@ -336,7 +336,7 @@ TEST_F(CoordinatesTest, GetContravariantMetricTensor) { FieldMetric{0.0}, // ShiftTorsion FieldMetric{0.0}}; // IntShiftTorsion - Coordinates::MetricTensor const contravariant_components = coords.getContravariantMetricTensor(); + const auto contravariant_components = coords.getContravariantMetricTensor(); EXPECT_TRUE(IsFieldEqual(contravariant_components.g11, 1.2)); EXPECT_TRUE(IsFieldEqual(contravariant_components.g22, 2.3)); @@ -380,7 +380,7 @@ TEST_F(CoordinatesTest, SetContravariantMetricTensor) { coords.setContravariantMetricTensor(updated_metric_tensor); // Get values with getter and check they have been modified as expected - Coordinates::MetricTensor g = coords.getContravariantMetricTensor(); + const auto g = coords.getContravariantMetricTensor(); EXPECT_TRUE(IsFieldEqual(g.g11, 1.7)); EXPECT_TRUE(IsFieldEqual(g.g22, 2.3)); EXPECT_TRUE(IsFieldEqual(g.g33, 3.1)); @@ -411,7 +411,7 @@ TEST_F(CoordinatesTest, GetCovariantMetricTensor) { FieldMetric{0.0}, // ShiftTorsion FieldMetric{0.0}}; // IntShiftTorsion - Coordinates::MetricTensor const covariant_components = coords.getCovariantMetricTensor(); + const auto covariant_components = coords.getCovariantMetricTensor(); EXPECT_TRUE(IsFieldEqual(covariant_components.g11, 9.7)); EXPECT_TRUE(IsFieldEqual(covariant_components.g22, 7.5)); @@ -456,7 +456,7 @@ TEST_F(CoordinatesTest, SetCovariantMetricTensor) { coords.setCovariantMetricTensor(updated_metric_tensor); // Get values with getter and check they have been modified as expected - Coordinates::MetricTensor g = coords.getCovariantMetricTensor(); + const auto g = coords.getCovariantMetricTensor(); EXPECT_TRUE(IsFieldEqual(g.g11, 1.7)); EXPECT_TRUE(IsFieldEqual(g.g22, 2.3)); EXPECT_TRUE(IsFieldEqual(g.g33, 3.1)); From b74b25c482e4d55d78d951f300dddb18728f4b1b Mon Sep 17 00:00:00 2001 From: tomc271 Date: Mon, 25 Sep 2023 09:34:44 +0100 Subject: [PATCH 043/491] Split MetricTensor struct into ContravariantMetricTensor and CovariantMetricTensor. --- include/bout/coordinates.hxx | 16 +++-- src/field/vecops.cxx | 12 ++-- src/field/vector2d.cxx | 46 +++++++-------- src/field/vector3d.cxx | 58 +++++++++---------- .../impls/multigrid/multigrid_laplace.cxx | 18 +++--- .../laplace/impls/naulin/naulin_laplace.cxx | 2 +- .../laplace/impls/serial_band/serial_band.cxx | 26 ++++----- src/invert/laplace/invert_laplace.cxx | 38 ++++++------ .../impls/cyclic/laplacexz-cyclic.cxx | 2 +- src/invert/parderiv/impls/cyclic/cyclic.cxx | 4 +- .../pardiv/impls/cyclic/pardiv_cyclic.cxx | 2 +- src/mesh/boundary_standard.cxx | 22 +++---- src/mesh/coordinates.cxx | 26 ++++----- src/mesh/coordinates_accessor.cxx | 16 ++--- src/mesh/difops.cxx | 50 ++++++++-------- src/mesh/fv_ops.cxx | 16 ++--- tests/MMS/GBS/gbs.cxx | 12 ++-- tests/MMS/diffusion/diffusion.cxx | 16 ++--- tests/MMS/diffusion2/diffusion.cxx | 12 ++-- tests/MMS/wave-1d/wave.cxx | 16 ++--- .../test-drift-instability/2fluid.cxx | 16 ++--- .../test-interchange-instability/2fluid.cxx | 16 ++--- .../test_multigrid_laplace.cxx | 4 +- .../test_naulin_laplace.cxx | 6 +- .../invert/laplace/test_laplace_cyclic.cxx | 8 +-- tests/unit/mesh/data/test_gridfromoptions.cxx | 14 ++--- tests/unit/mesh/test_coordinates.cxx | 28 ++++----- 27 files changed, 253 insertions(+), 249 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index 7b619b0cc3..73f3df7963 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -106,18 +106,22 @@ private: public: - struct MetricTensor - { + struct ContravariantMetricTensor { FieldMetric g11, g22, g33, g12, g13, g23; }; - MetricTensor getContravariantMetricTensor() const; + struct CovariantMetricTensor + { + FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; + }; + + ContravariantMetricTensor getContravariantMetricTensor() const; - MetricTensor getCovariantMetricTensor() const; + CovariantMetricTensor getCovariantMetricTensor() const; - void setContravariantMetricTensor(const MetricTensor& metric_tensor); + void setContravariantMetricTensor(const ContravariantMetricTensor& metric_tensor); - void setCovariantMetricTensor(const MetricTensor& metric_tensor); + void setCovariantMetricTensor(const CovariantMetricTensor& metric_tensor); /// Christoffel symbol of the second kind (connection coefficients) FieldMetric G1_11, G1_22, G1_33, G1_12, G1_13, G1_23; diff --git a/src/field/vecops.cxx b/src/field/vecops.cxx index 67c97171d7..5fb9744931 100644 --- a/src/field/vecops.cxx +++ b/src/field/vecops.cxx @@ -105,12 +105,12 @@ Vector3D Grad_perp(const Field3D& f, CELL_LOC outloc, const std::string& method) Vector3D result(f.getMesh()); - Coordinates::MetricTensor covariant_components = metric->getCovariantMetricTensor(); + Coordinates::CovariantMetricTensor covariant_components = metric->getCovariantMetricTensor(); result.x = DDX(f, outloc, method) - - covariant_components.g12 * DDY(f, outloc, method) / SQ(metric->J * metric->Bxy); + - covariant_components.g_12 * DDY(f, outloc, method) / SQ(metric->J * metric->Bxy); result.y = 0.0; result.z = DDZ(f, outloc, method) - - covariant_components.g23 * DDY(f, outloc, method) / SQ(metric->J * metric->Bxy); + - covariant_components.g_23 * DDY(f, outloc, method) / SQ(metric->J * metric->Bxy); result.setLocation(result.x.getLocation()); @@ -128,11 +128,11 @@ Vector2D Grad_perp(const Field2D& f, CELL_LOC outloc, const std::string& method) Vector2D result(f.getMesh()); - Coordinates::MetricTensor covariant_components = metric->getCovariantMetricTensor(); + Coordinates::CovariantMetricTensor covariant_components = metric->getCovariantMetricTensor(); result.x = DDX(f, outloc, method) - - covariant_components.g12 * DDY(f, outloc, method) / SQ(metric->J * metric->Bxy); + - covariant_components.g_12 * DDY(f, outloc, method) / SQ(metric->J * metric->Bxy); result.y = 0.0; - result.z = - covariant_components.g23 * DDY(f, outloc, method) / SQ(metric->J * metric->Bxy); + result.z = - covariant_components.g_23 * DDY(f, outloc, method) / SQ(metric->J * metric->Bxy); result.setLocation(result.x.getLocation()); diff --git a/src/field/vector2d.cxx b/src/field/vector2d.cxx index 1184a26452..eba1a86418 100644 --- a/src/field/vector2d.cxx +++ b/src/field/vector2d.cxx @@ -84,18 +84,18 @@ void Vector2D::toCovariant() { const auto x_at_z = interp_to(x, z.getLocation()); const auto y_at_z = interp_to(y, z.getLocation()); - Coordinates::MetricTensor covariant_components_x = metric_x->getCovariantMetricTensor(); - Coordinates::MetricTensor covariant_components_y = metric_y->getCovariantMetricTensor(); - Coordinates::MetricTensor covariant_components_z = metric_z->getCovariantMetricTensor(); + Coordinates::CovariantMetricTensor covariant_components_x = metric_x->getCovariantMetricTensor(); + Coordinates::CovariantMetricTensor covariant_components_y = metric_y->getCovariantMetricTensor(); + Coordinates::CovariantMetricTensor covariant_components_z = metric_z->getCovariantMetricTensor(); // multiply by g_{ij} BOUT_FOR(i, x.getRegion("RGN_ALL")) { - x[i] = covariant_components_x.g11[i] * x[i] + covariant_components_x.g12[i] * y_at_x[i] - + covariant_components_x.g13[i] * z_at_x[i]; - y[i] = covariant_components_y.g22[i] * y[i] + covariant_components_y.g12[i] * x_at_y[i] - + covariant_components_y.g23[i] * z_at_y[i]; - z[i] = covariant_components_z.g33[i] * z[i] + covariant_components_z.g13[i] * x_at_z[i] - + covariant_components_z.g23[i] * y_at_z[i]; + x[i] = covariant_components_x.g_11[i] * x[i] + covariant_components_x.g_12[i] * y_at_x[i] + + covariant_components_x.g_13[i] * z_at_x[i]; + y[i] = covariant_components_y.g_22[i] * y[i] + covariant_components_y.g_12[i] * x_at_y[i] + + covariant_components_y.g_23[i] * z_at_y[i]; + z[i] = covariant_components_z.g_33[i] * z[i] + covariant_components_z.g_13[i] * x_at_z[i] + + covariant_components_z.g_23[i] * y_at_z[i]; }; } else { const auto metric = localmesh->getCoordinates(location); @@ -103,11 +103,11 @@ void Vector2D::toCovariant() { // Need to use temporary arrays to store result Coordinates::FieldMetric gx{emptyFrom(x)}, gy{emptyFrom(y)}, gz{emptyFrom(z)}; - Coordinates::MetricTensor covariant_components = metric->getCovariantMetricTensor(); + Coordinates::CovariantMetricTensor covariant_components = metric->getCovariantMetricTensor(); BOUT_FOR(i, x.getRegion("RGN_ALL")) { - gx[i] = covariant_components.g11[i] * x[i] + covariant_components.g12[i] * y[i] + covariant_components.g13[i] * z[i]; - gy[i] = covariant_components.g22[i] * y[i] + covariant_components.g12[i] * x[i] + covariant_components.g23[i] * z[i]; - gz[i] = covariant_components.g33[i] * z[i] + covariant_components.g13[i] * x[i] + covariant_components.g23[i] * y[i]; + gx[i] = covariant_components.g_11[i] * x[i] + covariant_components.g_12[i] * y[i] + covariant_components.g_13[i] * z[i]; + gy[i] = covariant_components.g_22[i] * y[i] + covariant_components.g_12[i] * x[i] + covariant_components.g_23[i] * z[i]; + gz[i] = covariant_components.g_33[i] * z[i] + covariant_components.g_13[i] * x[i] + covariant_components.g_23[i] * y[i]; }; x = gx; @@ -143,9 +143,9 @@ void Vector2D::toContravariant() { const auto x_at_z = interp_to(x, z.getLocation()); const auto y_at_z = interp_to(y, z.getLocation()); - Coordinates::MetricTensor g_x = metric_x->getContravariantMetricTensor(); - Coordinates::MetricTensor g_y = metric_y->getContravariantMetricTensor(); - Coordinates::MetricTensor g_z = metric_z->getContravariantMetricTensor(); + Coordinates::ContravariantMetricTensor g_x = metric_x->getContravariantMetricTensor(); + Coordinates::ContravariantMetricTensor g_y = metric_y->getContravariantMetricTensor(); + Coordinates::ContravariantMetricTensor g_z = metric_z->getContravariantMetricTensor(); // multiply by g_{ij} BOUT_FOR(i, x.getRegion("RGN_ALL")) { @@ -160,7 +160,7 @@ void Vector2D::toContravariant() { // Need to use temporary arrays to store result Coordinates::FieldMetric gx{emptyFrom(x)}, gy{emptyFrom(y)}, gz{emptyFrom(z)}; - Coordinates::MetricTensor contravariant_components = metric->getContravariantMetricTensor(); + Coordinates::ContravariantMetricTensor contravariant_components = metric->getContravariantMetricTensor(); BOUT_FOR(i, x.getRegion("RGN_ALL")) { gx[i] = contravariant_components.g11[i] * x[i] + contravariant_components.g12[i] * y[i] + contravariant_components.g13[i] * z[i]; @@ -399,7 +399,7 @@ const Coordinates::FieldMetric Vector2D::operator*(const Vector2D& rhs) const { if (covariant) { // Both covariant - Coordinates::MetricTensor contravariant_components = metric->getContravariantMetricTensor(); + Coordinates::ContravariantMetricTensor contravariant_components = metric->getContravariantMetricTensor(); result = x * rhs.x * contravariant_components.g11 + y * rhs.y * contravariant_components.g22 + z * rhs.z * contravariant_components.g33; result += (x * rhs.y + y * rhs.x) * contravariant_components.g12 @@ -407,12 +407,12 @@ const Coordinates::FieldMetric Vector2D::operator*(const Vector2D& rhs) const { + (y * rhs.z + z * rhs.y) * contravariant_components.g23; } else { // Both contravariant - Coordinates::MetricTensor covariant_components = metric->getCovariantMetricTensor(); + Coordinates::CovariantMetricTensor covariant_components = metric->getCovariantMetricTensor(); result = - x * rhs.x * covariant_components.g11 + y * rhs.y * covariant_components.g22 + z * rhs.z * covariant_components.g33; - result += (x * rhs.y + y * rhs.x) * covariant_components.g12 - + (x * rhs.z + z * rhs.x) * covariant_components.g13 - + (y * rhs.z + z * rhs.y) * covariant_components.g23; + x * rhs.x * covariant_components.g_11 + y * rhs.y * covariant_components.g_22 + z * rhs.z * covariant_components.g_33; + result += (x * rhs.y + y * rhs.x) * covariant_components.g_12 + + (x * rhs.z + z * rhs.x) * covariant_components.g_13 + + (y * rhs.z + z * rhs.y) * covariant_components.g_23; } } diff --git a/src/field/vector3d.cxx b/src/field/vector3d.cxx index 27e8f664ab..7db305e4bc 100644 --- a/src/field/vector3d.cxx +++ b/src/field/vector3d.cxx @@ -85,18 +85,18 @@ void Vector3D::toCovariant() { const auto x_at_z = interp_to(x, z.getLocation()); const auto y_at_z = interp_to(y, z.getLocation()); - Coordinates::MetricTensor g_x = metric_x->getCovariantMetricTensor(); - Coordinates::MetricTensor g_y = metric_y->getCovariantMetricTensor(); - Coordinates::MetricTensor g_z = metric_z->getCovariantMetricTensor(); + Coordinates::CovariantMetricTensor g_x = metric_x->getCovariantMetricTensor(); + Coordinates::CovariantMetricTensor g_y = metric_y->getCovariantMetricTensor(); + Coordinates::CovariantMetricTensor g_z = metric_z->getCovariantMetricTensor(); // multiply by g_{ij} BOUT_FOR(i, localmesh->getRegion3D("RGN_ALL")) { - x[i] = g_x.g11[i] * x[i] + g_x.g12[i] * y_at_x[i] - + g_x.g13[i] * z_at_x[i]; - y[i] = g_y.g22[i] * y[i] + g_y.g12[i] * x_at_y[i] - + g_y.g23[i] * z_at_y[i]; - z[i] = g_z.g33[i] * z[i] + g_z.g13[i] * x_at_z[i] - + g_z.g23[i] * y_at_z[i]; + x[i] = g_x.g_11[i] * x[i] + g_x.g_12[i] * y_at_x[i] + + g_x.g_13[i] * z_at_x[i]; + y[i] = g_y.g_22[i] * y[i] + g_y.g_12[i] * x_at_y[i] + + g_y.g_23[i] * z_at_y[i]; + z[i] = g_z.g_33[i] * z[i] + g_z.g_13[i] * x_at_z[i] + + g_z.g_23[i] * y_at_z[i]; }; } else { const auto metric = localmesh->getCoordinates(location); @@ -104,12 +104,12 @@ void Vector3D::toCovariant() { // Need to use temporary arrays to store result Field3D gx{emptyFrom(x)}, gy{emptyFrom(y)}, gz{emptyFrom(z)}; - Coordinates::MetricTensor covariant_components = metric->getCovariantMetricTensor(); + Coordinates::CovariantMetricTensor covariant_components = metric->getCovariantMetricTensor(); BOUT_FOR(i, localmesh->getRegion3D("RGN_ALL")) { - gx[i] = covariant_components.g11[i] * x[i] + covariant_components.g12[i] * y[i] + covariant_components.g13[i] * z[i]; - gy[i] = covariant_components.g22[i] * y[i] + covariant_components.g12[i] * x[i] + covariant_components.g23[i] * z[i]; - gz[i] = covariant_components.g33[i] * z[i] + covariant_components.g13[i] * x[i] + covariant_components.g23[i] * y[i]; + gx[i] = covariant_components.g_11[i] * x[i] + covariant_components.g_12[i] * y[i] + covariant_components.g_13[i] * z[i]; + gy[i] = covariant_components.g_22[i] * y[i] + covariant_components.g_12[i] * x[i] + covariant_components.g_23[i] * z[i]; + gz[i] = covariant_components.g_33[i] * z[i] + covariant_components.g_13[i] * x[i] + covariant_components.g_23[i] * y[i]; }; x = gx; @@ -145,9 +145,9 @@ void Vector3D::toContravariant() { const auto x_at_z = interp_to(x, z.getLocation()); const auto y_at_z = interp_to(y, z.getLocation()); - Coordinates::MetricTensor g_x = metric_x->getContravariantMetricTensor(); - Coordinates::MetricTensor g_y = metric_y->getContravariantMetricTensor(); - Coordinates::MetricTensor g_z = metric_z->getContravariantMetricTensor(); + Coordinates::ContravariantMetricTensor g_x = metric_x->getContravariantMetricTensor(); + Coordinates::ContravariantMetricTensor g_y = metric_y->getContravariantMetricTensor(); + Coordinates::ContravariantMetricTensor g_z = metric_z->getContravariantMetricTensor(); // multiply by g_{ij} BOUT_FOR(i, localmesh->getRegion3D("RGN_ALL")) { @@ -162,7 +162,7 @@ void Vector3D::toContravariant() { // Need to use temporary arrays to store result Field3D gx{emptyFrom(x)}, gy{emptyFrom(y)}, gz{emptyFrom(z)}; - Coordinates::MetricTensor g = metric->getContravariantMetricTensor(); + Coordinates::ContravariantMetricTensor g = metric->getContravariantMetricTensor(); BOUT_FOR(i, localmesh->getRegion3D("RGN_ALL")) { gx[i] = g.g11[i] * x[i] + g.g12[i] * y[i] + g.g13[i] * z[i]; @@ -491,7 +491,7 @@ const Field3D Vector3D::operator*(const Vector3D& rhs) const { if (covariant) { // Both covariant - Coordinates::MetricTensor g = metric->getContravariantMetricTensor(); + Coordinates::ContravariantMetricTensor g = metric->getContravariantMetricTensor(); result = x * rhs.x * g.g11 + y * rhs.y * g.g22 + z * rhs.z * g.g33; result += (x * rhs.y + y * rhs.x) * g.g12 @@ -499,12 +499,12 @@ const Field3D Vector3D::operator*(const Vector3D& rhs) const { + (y * rhs.z + z * rhs.y) * g.g23; } else { // Both contravariant - Coordinates::MetricTensor covariant_components = metric->getCovariantMetricTensor(); + Coordinates::CovariantMetricTensor covariant_components = metric->getCovariantMetricTensor(); result = - x * rhs.x * covariant_components.g11 + y * rhs.y * covariant_components.g22 + z * rhs.z * covariant_components.g33; - result += (x * rhs.y + y * rhs.x) * covariant_components.g12 - + (x * rhs.z + z * rhs.x) * covariant_components.g13 - + (y * rhs.z + z * rhs.y) * covariant_components.g23; + x * rhs.x * covariant_components.g_11 + y * rhs.y * covariant_components.g_22 + z * rhs.z * covariant_components.g_33; + result += (x * rhs.y + y * rhs.x) * covariant_components.g_12 + + (x * rhs.z + z * rhs.x) * covariant_components.g_13 + + (y * rhs.z + z * rhs.y) * covariant_components.g_23; } } @@ -525,7 +525,7 @@ const Field3D Vector3D::operator*(const Vector2D& rhs) const { Coordinates* metric = x.getCoordinates(location); if (covariant) { // Both covariant - Coordinates::MetricTensor g = metric->getContravariantMetricTensor(); + Coordinates::ContravariantMetricTensor g = metric->getContravariantMetricTensor(); result = x * rhs.x * g.g11 + y * rhs.y * g.g22 + z * rhs.z * g.g33; result += (x * rhs.y + y * rhs.x) * g.g12 @@ -533,12 +533,12 @@ const Field3D Vector3D::operator*(const Vector2D& rhs) const { + (y * rhs.z + z * rhs.y) * g.g23; } else { // Both contravariant - Coordinates::MetricTensor covariant_components = metric->getCovariantMetricTensor(); + Coordinates::CovariantMetricTensor covariant_components = metric->getCovariantMetricTensor(); result = - x * rhs.x * covariant_components.g11 + y * rhs.y * covariant_components.g22 + z * rhs.z * covariant_components.g33; - result += (x * rhs.y + y * rhs.x) * covariant_components.g12 - + (x * rhs.z + z * rhs.x) * covariant_components.g13 - + (y * rhs.z + z * rhs.y) * covariant_components.g23; + x * rhs.x * covariant_components.g_11 + y * rhs.y * covariant_components.g_22 + z * rhs.z * covariant_components.g_33; + result += (x * rhs.y + y * rhs.x) * covariant_components.g_12 + + (x * rhs.z + z * rhs.x) * covariant_components.g_13 + + (y * rhs.z + z * rhs.y) * covariant_components.g_23; } } diff --git a/src/invert/laplace/impls/multigrid/multigrid_laplace.cxx b/src/invert/laplace/impls/multigrid/multigrid_laplace.cxx index 6f5e0a4c80..838cad4c11 100644 --- a/src/invert/laplace/impls/multigrid/multigrid_laplace.cxx +++ b/src/invert/laplace/impls/multigrid/multigrid_laplace.cxx @@ -280,13 +280,13 @@ FieldPerp LaplaceMultigrid::solve(const FieldPerp& b_in, const FieldPerp& x0) { // Neumann boundary condition if (inner_boundary_flags & INVERT_SET) { // guard cells of x0 specify gradient to set at inner boundary - Coordinates::MetricTensor covariant_components = coords->getCovariantMetricTensor(); + Coordinates::CovariantMetricTensor covariant_components = coords->getCovariantMetricTensor(); BOUT_OMP_PERF(parallel default(shared)) BOUT_OMP_PERF(for) for (int k = 1; k < lzz + 1; k++) { int k2 = k - 1; x[k] = -x0(localmesh->xstart - 1, k2) - * sqrt(covariant_components.g11(localmesh->xstart, yindex)) + * sqrt(covariant_components.g_11(localmesh->xstart, yindex)) * coords->dx(localmesh->xstart, yindex); } } else { @@ -325,13 +325,13 @@ FieldPerp LaplaceMultigrid::solve(const FieldPerp& b_in, const FieldPerp& x0) { // Neumann boundary condition if (inner_boundary_flags & INVERT_SET) { // guard cells of x0 specify gradient to set at outer boundary - Coordinates::MetricTensor covariant_components = coords->getCovariantMetricTensor(); + Coordinates::CovariantMetricTensor covariant_components = coords->getCovariantMetricTensor(); BOUT_OMP_PERF(parallel default(shared)) BOUT_OMP_PERF(for) for (int k = 1; k < lzz + 1; k++) { int k2 = k - 1; x[(lxx + 1) * lz2 + k] = x0(localmesh->xend + 1, k2) - * sqrt(covariant_components.g11(localmesh->xend, yindex)) + * sqrt(covariant_components.g_11(localmesh->xend, yindex)) * coords->dx(localmesh->xend, yindex); // this is the value to set the gradient to at the outer boundary } @@ -483,7 +483,7 @@ FieldPerp LaplaceMultigrid::solve(const FieldPerp& b_in, const FieldPerp& x0) { // Neumann boundary condition if (inner_boundary_flags & INVERT_SET) { // guard cells of x0 specify gradient to set at inner boundary - Coordinates::MetricTensor covariant_components = coords->getCovariantMetricTensor(); + Coordinates::CovariantMetricTensor covariant_components = coords->getCovariantMetricTensor(); int i2 = -1 + localmesh->xstart; BOUT_OMP_PERF(parallel default(shared)) BOUT_OMP_PERF(for) @@ -491,7 +491,7 @@ FieldPerp LaplaceMultigrid::solve(const FieldPerp& b_in, const FieldPerp& x0) { int k2 = k - 1; result(i2, k2) = x[lz2 + k] - x0(localmesh->xstart - 1, k2) - * sqrt(covariant_components.g11(localmesh->xstart, yindex)) + * sqrt(covariant_components.g_11(localmesh->xstart, yindex)) * coords->dx(localmesh->xstart, yindex); } } else { @@ -532,7 +532,7 @@ FieldPerp LaplaceMultigrid::solve(const FieldPerp& b_in, const FieldPerp& x0) { // Neumann boundary condition if (inner_boundary_flags & INVERT_SET) { // guard cells of x0 specify gradient to set at outer boundary - Coordinates::MetricTensor covariant_components = coords->getCovariantMetricTensor(); + Coordinates::CovariantMetricTensor covariant_components = coords->getCovariantMetricTensor(); int i2 = lxx + localmesh->xstart; BOUT_OMP_PERF(parallel default(shared)) BOUT_OMP_PERF(for) @@ -540,7 +540,7 @@ FieldPerp LaplaceMultigrid::solve(const FieldPerp& b_in, const FieldPerp& x0) { int k2 = k - 1; result(i2, k2) = x[lxx * lz2 + k] + x0(localmesh->xend + 1, k2) - * sqrt(covariant_components.g11(localmesh->xend, yindex)) + * sqrt(covariant_components.g_11(localmesh->xend, yindex)) * coords->dx(localmesh->xend, yindex); } } else { @@ -601,7 +601,7 @@ void LaplaceMultigrid::generateMatrixF(int level) { int k2p = (k2 + 1) % Nz_global; int k2m = (k2 + Nz_global - 1) % Nz_global; - Coordinates::MetricTensor contravariant_components = coords->getContravariantMetricTensor(); + Coordinates::ContravariantMetricTensor contravariant_components = coords->getContravariantMetricTensor(); BoutReal dz = coords->dz(i2, yindex); BoutReal ddx_C = (C2(i2 + 1, yindex, k2) - C2(i2 - 1, yindex, k2)) / 2. diff --git a/src/invert/laplace/impls/naulin/naulin_laplace.cxx b/src/invert/laplace/impls/naulin/naulin_laplace.cxx index 8bcb7af624..7b7770ab11 100644 --- a/src/invert/laplace/impls/naulin/naulin_laplace.cxx +++ b/src/invert/laplace/impls/naulin/naulin_laplace.cxx @@ -248,7 +248,7 @@ Field3D LaplaceNaulin::solve(const Field3D& rhs, const Field3D& x0) { // Derivatives of x Field3D ddx_x = DDX(x_in, location, "C2"); Field3D ddz_x = DDZ(x_in, location, "FFT"); - Coordinates::MetricTensor g = coords->getContravariantMetricTensor(); + Coordinates::ContravariantMetricTensor g = coords->getContravariantMetricTensor(); return rhsOverD - (g.g11 * coef_x_AC * ddx_x + g.g33 * coef_z * ddz_x + g.g13 * (coef_x_AC * ddz_x + coef_z * ddx_x)) diff --git a/src/invert/laplace/impls/serial_band/serial_band.cxx b/src/invert/laplace/impls/serial_band/serial_band.cxx index 55178f8b34..a0e805e927 100644 --- a/src/invert/laplace/impls/serial_band/serial_band.cxx +++ b/src/invert/laplace/impls/serial_band/serial_band.cxx @@ -158,7 +158,7 @@ FieldPerp LaplaceSerialBand::solve(const FieldPerp& b, const FieldPerp& x0) { A(ix, 4) = 0.; #else // Set coefficients - Coordinates::MetricTensor g = coords->getContravariantMetricTensor(); + Coordinates::ContravariantMetricTensor g = coords->getContravariantMetricTensor(); coef1 = g.g11(ix, jy); // X 2nd derivative coef2 = g.g33(ix, jy); // Z 2nd derivative coef3 = g.g13(ix, jy); // X-Z mixed derivatives @@ -215,7 +215,7 @@ FieldPerp LaplaceSerialBand::solve(const FieldPerp& b, const FieldPerp& x0) { int ix = 1; auto kwave = kwave_(ix, jy); - Coordinates::MetricTensor g = coords->getContravariantMetricTensor(); + Coordinates::ContravariantMetricTensor g = coords->getContravariantMetricTensor(); coef1 = g.g11(ix, jy) / (SQ(coords->dx(ix, jy))); coef2 = g.g33(ix, jy); coef3 = kwave * g.g13(ix, jy) / (2. * coords->dx(ix, jy)); @@ -266,7 +266,7 @@ FieldPerp LaplaceSerialBand::solve(const FieldPerp& b, const FieldPerp& x0) { if (iz == 0) { // DC - Coordinates::MetricTensor covariant_components = coords->getCovariantMetricTensor(); + Coordinates::CovariantMetricTensor covariant_components = coords->getCovariantMetricTensor(); // Inner boundary if (inner_boundary_flags & (INVERT_DC_GRAD + INVERT_SET) @@ -276,8 +276,8 @@ FieldPerp LaplaceSerialBand::solve(const FieldPerp& b, const FieldPerp& x0) { for (int ix = 0; ix < xbndry; ix++) { A(ix, 0) = 0.; A(ix, 1) = 0.; - A(ix, 2) = -.5 / sqrt(covariant_components.g11(ix, jy)) / coords->dx(ix, jy); - A(ix, 3) = .5 / sqrt(covariant_components.g11(ix, jy)) / coords->dx(ix, jy); + A(ix, 2) = -.5 / sqrt(covariant_components.g_11(ix, jy)) / coords->dx(ix, jy); + A(ix, 3) = .5 / sqrt(covariant_components.g_11(ix, jy)) / coords->dx(ix, jy); A(ix, 4) = 0.; } @@ -296,17 +296,17 @@ FieldPerp LaplaceSerialBand::solve(const FieldPerp& b, const FieldPerp& x0) { for (int ix = 0; ix < xbndry; ix++) { A(ix, 0) = 0.; A(ix, 1) = 0.; - A(ix, 2) = -3. / sqrt(covariant_components.g22(ix, jy)); - A(ix, 3) = 4. / sqrt(covariant_components.g22(ix + 1, jy)); - A(ix, 4) = -1. / sqrt(covariant_components.g22(ix + 2, jy)); + A(ix, 2) = -3. / sqrt(covariant_components.g_22(ix, jy)); + A(ix, 3) = 4. / sqrt(covariant_components.g_22(ix + 1, jy)); + A(ix, 4) = -1. / sqrt(covariant_components.g_22(ix + 2, jy)); } } else if (inner_boundary_flags & INVERT_DC_GRADPARINV) { for (int ix = 0; ix < xbndry; ix++) { A(ix, 0) = 0.; A(ix, 1) = 0.; - A(ix, 2) = -3. * sqrt(covariant_components.g22(ix, jy)); - A(ix, 3) = 4. * sqrt(covariant_components.g22(ix + 1, jy)); - A(ix, 4) = -sqrt(covariant_components.g22(ix + 2, jy)); + A(ix, 2) = -3. * sqrt(covariant_components.g_22(ix, jy)); + A(ix, 3) = 4. * sqrt(covariant_components.g_22(ix + 1, jy)); + A(ix, 4) = -sqrt(covariant_components.g_22(ix + 2, jy)); } } else if (inner_boundary_flags & INVERT_DC_LAP) { for (int ix = 0; ix < xbndry; ix++) { @@ -341,7 +341,7 @@ FieldPerp LaplaceSerialBand::solve(const FieldPerp& b, const FieldPerp& x0) { int ix = 1; auto kwave = kwave_(ix, jy); - Coordinates::MetricTensor g = coords->getContravariantMetricTensor(); + Coordinates::ContravariantMetricTensor g = coords->getContravariantMetricTensor(); coef1 = g.g11(ix, jy) / (12. * SQ(coords->dx(ix, jy))); coef2 = g.g33(ix, jy); @@ -386,7 +386,7 @@ FieldPerp LaplaceSerialBand::solve(const FieldPerp& b, const FieldPerp& x0) { int ix = ncx - 1; - Coordinates::MetricTensor g = coords->getContravariantMetricTensor(); + Coordinates::ContravariantMetricTensor g = coords->getContravariantMetricTensor(); coef1 = g.g11(ix, jy) / (12. * SQ(coords->dx(ix, jy))); coef2 = g.g33(ix, jy); diff --git a/src/invert/laplace/invert_laplace.cxx b/src/invert/laplace/invert_laplace.cxx index ca1bb87b22..9c61e4bf4a 100644 --- a/src/invert/laplace/invert_laplace.cxx +++ b/src/invert/laplace/invert_laplace.cxx @@ -324,7 +324,7 @@ void Laplacian::tridagCoefs(int jx, int jy, BoutReal kwave, dcomplex& a, dcomple BoutReal coef1, coef2, coef3, coef4, coef5; - Coordinates::MetricTensor contravariant_components = localcoords->getContravariantMetricTensor(); + Coordinates::ContravariantMetricTensor contravariant_components = localcoords->getContravariantMetricTensor(); coef1 = contravariant_components.g11(jx, jy); ///< X 2nd derivative coefficient coef2 = contravariant_components.g33(jx, jy); ///< Z 2nd derivative coefficient @@ -494,8 +494,8 @@ void Laplacian::tridagMatrix(dcomplex* avec, dcomplex* bvec, dcomplex* cvec, dco // Set the boundary conditions if x is not periodic if (!localmesh->periodicX) { - Coordinates::MetricTensor contravariant_components = coords->getContravariantMetricTensor(); - Coordinates::MetricTensor covariant_components = coords->getCovariantMetricTensor(); + Coordinates::ContravariantMetricTensor contravariant_components = coords->getContravariantMetricTensor(); + Coordinates::CovariantMetricTensor covariant_components = coords->getCovariantMetricTensor(); if (localmesh->firstX()) { // INNER BOUNDARY ON THIS PROCESSOR @@ -515,8 +515,8 @@ void Laplacian::tridagMatrix(dcomplex* avec, dcomplex* bvec, dcomplex* cvec, dco // Zero gradient at inner boundary for (int ix = 0; ix < inbndry; ix++) { avec[ix] = 0.; - bvec[ix] = -1. / sqrt(covariant_components.g11(ix, jy)) / coords->dx(ix, jy); - cvec[ix] = 1. / sqrt(covariant_components.g11(ix, jy)) / coords->dx(ix, jy); + bvec[ix] = -1. / sqrt(covariant_components.g_11(ix, jy)) / coords->dx(ix, jy); + cvec[ix] = 1. / sqrt(covariant_components.g_11(ix, jy)) / coords->dx(ix, jy); } } else if (inner_boundary_flags & INVERT_DC_GRAD) { // Zero gradient at inner boundary @@ -528,14 +528,14 @@ void Laplacian::tridagMatrix(dcomplex* avec, dcomplex* bvec, dcomplex* cvec, dco } else if (inner_boundary_flags & INVERT_DC_GRADPAR) { for (int ix = 0; ix < inbndry; ix++) { avec[ix] = 0.0; - bvec[ix] = 1.0 / sqrt(covariant_components.g22(ix, jy)); - cvec[ix] = -1.0 / sqrt(covariant_components.g22(ix + 1, jy)); + bvec[ix] = 1.0 / sqrt(covariant_components.g_22(ix, jy)); + cvec[ix] = -1.0 / sqrt(covariant_components.g_22(ix + 1, jy)); } } else if (inner_boundary_flags & INVERT_DC_GRADPARINV) { for (int ix = 0; ix < inbndry; ix++) { avec[ix] = 0.0; - bvec[ix] = sqrt(covariant_components.g22(ix, jy)); - cvec[ix] = -sqrt(covariant_components.g22(ix + 1, jy)); + bvec[ix] = sqrt(covariant_components.g_22(ix, jy)); + cvec[ix] = -sqrt(covariant_components.g_22(ix + 1, jy)); } } else if (inner_boundary_flags & INVERT_DC_LAP) { // Decaying boundary conditions @@ -612,8 +612,8 @@ void Laplacian::tridagMatrix(dcomplex* avec, dcomplex* bvec, dcomplex* cvec, dco for (int ix = 0; ix < inbndry; ix++) { avec[ix] = dcomplex(0., 0.); bvec[ix] = - dcomplex(-1., 0.) / sqrt(covariant_components.g11(ix, jy)) / coords->dx(ix, jy); - cvec[ix] = dcomplex(1., 0.) / sqrt(covariant_components.g11(ix, jy)) / coords->dx(ix, jy); + dcomplex(-1., 0.) / sqrt(covariant_components.g_11(ix, jy)) / coords->dx(ix, jy); + cvec[ix] = dcomplex(1., 0.) / sqrt(covariant_components.g_11(ix, jy)) / coords->dx(ix, jy); } } else if (inner_boundary_flags & INVERT_AC_GRAD) { // Zero gradient at inner boundary @@ -672,9 +672,9 @@ void Laplacian::tridagMatrix(dcomplex* avec, dcomplex* bvec, dcomplex* cvec, dco && (outer_boundary_flags & INVERT_SET || outer_boundary_flags & INVERT_RHS)) { // Zero gradient at outer boundary for (int ix = 0; ix < outbndry; ix++) { - avec[ncx - ix] = dcomplex(-1., 0.) / sqrt(covariant_components.g11(ncx - ix, jy)) + avec[ncx - ix] = dcomplex(-1., 0.) / sqrt(covariant_components.g_11(ncx - ix, jy)) / coords->dx(ncx - ix, jy); - bvec[ncx - ix] = dcomplex(1., 0.) / sqrt(covariant_components.g11(ncx - ix, jy)) + bvec[ncx - ix] = dcomplex(1., 0.) / sqrt(covariant_components.g_11(ncx - ix, jy)) / coords->dx(ncx - ix, jy); cvec[ncx - ix] = dcomplex(0., 0.); } @@ -687,14 +687,14 @@ void Laplacian::tridagMatrix(dcomplex* avec, dcomplex* bvec, dcomplex* cvec, dco } } else if (inner_boundary_flags & INVERT_DC_GRADPAR) { for (int ix = 0; ix < inbndry; ix++) { - avec[ncx - ix] = 1.0 / sqrt(covariant_components.g22(ncx - ix + 1, jy)); - bvec[ncx - ix] = -1.0 / sqrt(covariant_components.g22(ncx - ix, jy)); + avec[ncx - ix] = 1.0 / sqrt(covariant_components.g_22(ncx - ix + 1, jy)); + bvec[ncx - ix] = -1.0 / sqrt(covariant_components.g_22(ncx - ix, jy)); cvec[ncx - ix] = 0.0; } } else if (inner_boundary_flags & INVERT_DC_GRADPARINV) { for (int ix = 0; ix < inbndry; ix++) { - avec[ncx - ix] = sqrt(covariant_components.g22(ncx - ix - 1, jy)); - bvec[ncx - ix] = -sqrt(covariant_components.g22(ncx - ix, jy)); + avec[ncx - ix] = sqrt(covariant_components.g_22(ncx - ix - 1, jy)); + bvec[ncx - ix] = -sqrt(covariant_components.g_22(ncx - ix, jy)); cvec[ncx - ix] = 0.0; } } else if (inner_boundary_flags & INVERT_DC_LAP) { @@ -730,9 +730,9 @@ void Laplacian::tridagMatrix(dcomplex* avec, dcomplex* bvec, dcomplex* cvec, dco && (outer_boundary_flags & INVERT_SET || outer_boundary_flags & INVERT_RHS)) { // Zero gradient at outer boundary for (int ix = 0; ix < outbndry; ix++) { - avec[ncx - ix] = dcomplex(-1., 0.) / sqrt(covariant_components.g11(ncx - ix, jy)) + avec[ncx - ix] = dcomplex(-1., 0.) / sqrt(covariant_components.g_11(ncx - ix, jy)) / coords->dx(ncx - ix, jy); - bvec[ncx - ix] = dcomplex(1., 0.) / sqrt(covariant_components.g11(ncx - ix, jy)) + bvec[ncx - ix] = dcomplex(1., 0.) / sqrt(covariant_components.g_11(ncx - ix, jy)) / coords->dx(ncx - ix, jy); cvec[ncx - ix] = dcomplex(0., 0.); } diff --git a/src/invert/laplacexz/impls/cyclic/laplacexz-cyclic.cxx b/src/invert/laplacexz/impls/cyclic/laplacexz-cyclic.cxx index 97a0a72c16..3127bccdc8 100644 --- a/src/invert/laplacexz/impls/cyclic/laplacexz-cyclic.cxx +++ b/src/invert/laplacexz/impls/cyclic/laplacexz-cyclic.cxx @@ -72,7 +72,7 @@ void LaplaceXZcyclic::setCoefs(const Field2D& A2D, const Field2D& B2D) { // Set coefficients Coordinates* coord = localmesh->getCoordinates(location); - Coordinates::MetricTensor g = coord->getContravariantMetricTensor(); + Coordinates::ContravariantMetricTensor g = coord->getContravariantMetricTensor(); // NOTE: For now the X-Z terms are omitted, so check that they are small ASSERT2(max(abs(g.g13)) < 1e-5); diff --git a/src/invert/parderiv/impls/cyclic/cyclic.cxx b/src/invert/parderiv/impls/cyclic/cyclic.cxx index 94098e8aa0..67e7eb6863 100644 --- a/src/invert/parderiv/impls/cyclic/cyclic.cxx +++ b/src/invert/parderiv/impls/cyclic/cyclic.cxx @@ -58,7 +58,7 @@ InvertParCR::InvertParCR(Options* opt, CELL_LOC location, Mesh* mesh_in) // Number of k equations to solve for each x location nsys = 1 + (localmesh->LocalNz) / 2; - sg = sqrt(localmesh->getCoordinates(location)->getCovariantMetricTensor().g22); + sg = sqrt(localmesh->getCoordinates(location)->getCovariantMetricTensor().g_22); sg = DDY(1. / sg) / sg; } @@ -154,7 +154,7 @@ const Field3D InvertParCR::solve(const Field3D& f) { BoutReal acoef = A(x, y + local_ystart); // Constant BoutReal bcoef = - B(x, y + local_ystart) / coord->getCovariantMetricTensor().g22(x, y + local_ystart); // d2dy2 + B(x, y + local_ystart) / coord->getCovariantMetricTensor().g_22(x, y + local_ystart); // d2dy2 BoutReal ccoef = C(x, y + local_ystart); // d2dydz BoutReal dcoef = D(x, y + local_ystart); // d2dz2 BoutReal ecoef = E(x, y + local_ystart) diff --git a/src/invert/pardiv/impls/cyclic/pardiv_cyclic.cxx b/src/invert/pardiv/impls/cyclic/pardiv_cyclic.cxx index 3b43205eab..5a30aa3830 100644 --- a/src/invert/pardiv/impls/cyclic/pardiv_cyclic.cxx +++ b/src/invert/pardiv/impls/cyclic/pardiv_cyclic.cxx @@ -106,7 +106,7 @@ Field3D InvertParDivCR::solve(const Field3D& f) { const Field2D dy = coord->dy; const Field2D J = coord->J; - const Field2D g_22 = coord->getCovariantMetricTensor().g22; + const Field2D g_22 = coord->getCovariantMetricTensor().g_22; const auto zlength = getUniform(coord->zlength()); // Loop over flux-surfaces diff --git a/src/mesh/boundary_standard.cxx b/src/mesh/boundary_standard.cxx index fa1b81fe76..c270455e10 100644 --- a/src/mesh/boundary_standard.cxx +++ b/src/mesh/boundary_standard.cxx @@ -1602,7 +1602,7 @@ void BoundaryNeumann_NonOrthogonal::apply(Field2D& f) { // Calculate derivatives for metric use mesh->communicate(f); Field2D dfdy = DDY(f); - Coordinates::MetricTensor contravariant_components = metric->getContravariantMetricTensor(); + Coordinates::ContravariantMetricTensor contravariant_components = metric->getContravariantMetricTensor(); // Loop over all elements and set equal to the next point in for (bndry->first(); !bndry->isDone(); bndry->next1d()) { // Interpolate (linearly) metrics to halfway between last cell and boundary cell @@ -1660,7 +1660,7 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { mesh->communicate(f); Field3D dfdy = DDY(f); Field3D dfdz = DDZ(f); - Coordinates::MetricTensor g = metric->getContravariantMetricTensor(); + Coordinates::ContravariantMetricTensor g = metric->getContravariantMetricTensor(); // Loop over all elements and set equal to the next point in for (bndry->first(); !bndry->isDone(); bndry->next1d()) { #if BOUT_USE_METRIC_3D @@ -2451,13 +2451,13 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { void BoundaryNeumannPar::apply(Field2D & f) { #if not(BOUT_USE_METRIC_3D) Coordinates* metric = f.getCoordinates(); - Coordinates::MetricTensor covariant_components = metric->getCovariantMetricTensor(); + Coordinates::CovariantMetricTensor covariant_components = metric->getCovariantMetricTensor(); // Loop over all elements and set equal to the next point in for (bndry->first(); !bndry->isDone(); bndry->next()) { f(bndry->x, bndry->y) = f(bndry->x - bndry->bx, bndry->y - bndry->by) - * sqrt(covariant_components.g22(bndry->x, bndry->y) - / covariant_components.g22(bndry->x - bndry->bx, bndry->y - bndry->by)); + * sqrt(covariant_components.g_22(bndry->x, bndry->y) + / covariant_components.g_22(bndry->x - bndry->bx, bndry->y - bndry->by)); } #else throw BoutException("Applying boundary condition 'neumannpar' to Field2D not " @@ -2469,13 +2469,13 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { Mesh* mesh = bndry->localmesh; ASSERT1(mesh == f.getMesh()); Coordinates* metric = f.getCoordinates(); - Coordinates::MetricTensor covariant_components = metric->getCovariantMetricTensor(); + Coordinates::CovariantMetricTensor covariant_components = metric->getCovariantMetricTensor(); for (bndry->first(); !bndry->isDone(); bndry->next()) { for (int z = 0; z < mesh->LocalNz; z++) { f(bndry->x, bndry->y, z) = f(bndry->x - bndry->bx, bndry->y - bndry->by, z) - * sqrt(covariant_components.g22(bndry->x, bndry->y, z) - / covariant_components.g22(bndry->x - bndry->bx, bndry->y - bndry->by, z)); + * sqrt(covariant_components.g_22(bndry->x, bndry->y, z) + / covariant_components.g_22(bndry->x - bndry->bx, bndry->y - bndry->by, z)); } } } @@ -2649,7 +2649,7 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { } int bx = bndry->bx; - Coordinates::MetricTensor g = metric->getContravariantMetricTensor(); + Coordinates::ContravariantMetricTensor g = metric->getContravariantMetricTensor(); // Loop over the Y dimension for (bndry->first(); !bndry->isDone(); bndry->nextY()) { // bndry->(x,y) is the first point in the boundary @@ -2856,7 +2856,7 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { Array c0(ncz / 2 + 1), c1(ncz / 2 + 1), c2(ncz / 2 + 1); int bx = bndry->bx; - Coordinates::MetricTensor g = metric->getContravariantMetricTensor(); + Coordinates::ContravariantMetricTensor g = metric->getContravariantMetricTensor(); // Loop over the Y dimension for (bndry->first(); !bndry->isDone(); bndry->nextY()) { int x = bndry->x; @@ -2947,7 +2947,7 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { } jx = mesh->xend + 1; - Coordinates::MetricTensor contravariant_components = metric->getContravariantMetricTensor(); + Coordinates::ContravariantMetricTensor contravariant_components = metric->getContravariantMetricTensor(); for (jy = 1; jy < mesh->LocalNy - 1; jy++) { for (jz = 0; jz < ncz; jz++) { jzp = (jz + 1) % ncz; diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 9b1f3f3278..5c76ca5750 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -834,7 +834,7 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, dz = interpolateAndExtrapolate(coords_in->dz, location, true, true, false, transform.get()); - Coordinates::MetricTensor metric_tensor = coords_in->getContravariantMetricTensor(); + Coordinates::ContravariantMetricTensor metric_tensor = coords_in->getContravariantMetricTensor(); // Diagonal components of metric tensor g^{ij} g11 = interpolateAndExtrapolate(metric_tensor.g11, location, true, true, false, @@ -2026,18 +2026,18 @@ void Coordinates::checkContravariant() { } } -Coordinates::MetricTensor Coordinates::getContravariantMetricTensor() const { - MetricTensor g_contravariant = { g11, g22, g33, g12, g13, g23 }; +Coordinates::ContravariantMetricTensor Coordinates::getContravariantMetricTensor() const { + ContravariantMetricTensor g_contravariant = { g11, g22, g33, g12, g13, g23 }; return g_contravariant; } -Coordinates::MetricTensor Coordinates::getCovariantMetricTensor() const { - MetricTensor g_covariant = { g_11, g_22, g_33, g_12, g_13, g_23 }; +Coordinates::CovariantMetricTensor Coordinates::getCovariantMetricTensor() const { + CovariantMetricTensor g_covariant = { g_11, g_22, g_33, g_12, g_13, g_23 }; return g_covariant; } void Coordinates::setContravariantMetricTensor( - const Coordinates::MetricTensor& metric_tensor) { + const Coordinates::ContravariantMetricTensor& metric_tensor) { g11 = metric_tensor.g11; g22 = metric_tensor.g22; g33 = metric_tensor.g33; @@ -2048,12 +2048,12 @@ void Coordinates::setContravariantMetricTensor( } void Coordinates::setCovariantMetricTensor( - const Coordinates::MetricTensor& metric_tensor) { - g_11 = metric_tensor.g11; - g_22 = metric_tensor.g22; - g_33 = metric_tensor.g33; - g_12 = metric_tensor.g12; - g_13 = metric_tensor.g13; - g_23 = metric_tensor.g23; + const Coordinates::CovariantMetricTensor& metric_tensor) { + g_11 = metric_tensor.g_11; + g_22 = metric_tensor.g_22; + g_33 = metric_tensor.g_33; + g_12 = metric_tensor.g_12; + g_13 = metric_tensor.g_13; + g_23 = metric_tensor.g_23; calcContravariant(); } diff --git a/src/mesh/coordinates_accessor.cxx b/src/mesh/coordinates_accessor.cxx index fd6449e3f2..95e03e83c5 100644 --- a/src/mesh/coordinates_accessor.cxx +++ b/src/mesh/coordinates_accessor.cxx @@ -60,7 +60,7 @@ CoordinatesAccessor::CoordinatesAccessor(const Coordinates* coords) { coords->Bxy.ydown()[ind]; COPY_STRIPE(G1, G3); - Coordinates::MetricTensor contravariant_components = + Coordinates::ContravariantMetricTensor contravariant_components = coords->getContravariantMetricTensor(); // COPY_STRIPE(g11, g12, g13, g22, g23, g33); data[stripe_size * ind.ind + static_cast(Offset::g11)] = @@ -77,19 +77,19 @@ CoordinatesAccessor::CoordinatesAccessor(const Coordinates* coords) { contravariant_components.g33[ind]; // COPY_STRIPE(g_11, g_12, g_13, g_22, g_23, g_33); - Coordinates::MetricTensor covariant_components = coords->getCovariantMetricTensor(); + Coordinates::CovariantMetricTensor covariant_components = coords->getCovariantMetricTensor(); data[stripe_size * ind.ind + static_cast(Offset::g_11)] = - covariant_components.g11[ind]; + covariant_components.g_11[ind]; data[stripe_size * ind.ind + static_cast(Offset::g_12)] = - covariant_components.g12[ind]; + covariant_components.g_12[ind]; data[stripe_size * ind.ind + static_cast(Offset::g_13)] = - covariant_components.g13[ind]; + covariant_components.g_13[ind]; data[stripe_size * ind.ind + static_cast(Offset::g_22)] = - covariant_components.g22[ind]; + covariant_components.g_22[ind]; data[stripe_size * ind.ind + static_cast(Offset::g_23)] = - covariant_components.g23[ind]; + covariant_components.g_23[ind]; data[stripe_size * ind.ind + static_cast(Offset::g_33)] = - covariant_components.g33[ind]; + covariant_components.g_33[ind]; } } diff --git a/src/mesh/difops.cxx b/src/mesh/difops.cxx index cb5c9ced42..6453ed395c 100644 --- a/src/mesh/difops.cxx +++ b/src/mesh/difops.cxx @@ -101,11 +101,11 @@ Field3D Grad_parP(const Field3D& apar, const Field3D& f) { } } - Coordinates::MetricTensor covariant_components = metric->getCovariantMetricTensor(); + Coordinates::CovariantMetricTensor covariant_components = metric->getCovariantMetricTensor(); for (int x = 1; x <= mesh->LocalNx - 2; x++) { for (int y = mesh->ystart; y <= mesh->yend; y++) { for (int z = 0; z < ncz; z++) { - BoutReal by = 1. / sqrt(covariant_components.g22(x, y, z)); + BoutReal by = 1. / sqrt(covariant_components.g_22(x, y, z)); // Z indices zm and zp int zm = (z - 1 + ncz) % ncz; int zp = (z + 1) % ncz; @@ -248,7 +248,7 @@ Field3D Div_par(const Field3D& f, const Field3D& v) { Coordinates* coord = f.getCoordinates(); - Coordinates::MetricTensor covariant_components = coord->getCovariantMetricTensor(); + Coordinates::CovariantMetricTensor covariant_components = coord->getCovariantMetricTensor(); for (int i = mesh->xstart; i <= mesh->xend; i++) { for (int j = mesh->ystart; j <= mesh->yend; j++) { @@ -263,12 +263,12 @@ Field3D Div_par(const Field3D& f, const Field3D& v) { // Calculate flux at right boundary (y+1/2) BoutReal fluxRight = fR * vR * (coord->J(i, j, k) + coord->J(i, j + 1, k)) - / (sqrt(covariant_components.g22(i, j, k)) + sqrt(covariant_components.g22(i, j + 1, k))); + / (sqrt(covariant_components.g_22(i, j, k)) + sqrt(covariant_components.g_22(i, j + 1, k))); // Calculate at left boundary (y-1/2) BoutReal fluxLeft = fL * vL * (coord->J(i, j, k) + coord->J(i, j - 1, k)) - / (sqrt(covariant_components.g22(i, j, k)) + sqrt(covariant_components.g22(i, j - 1, k))); + / (sqrt(covariant_components.g_22(i, j, k)) + sqrt(covariant_components.g_22(i, j - 1, k))); result(i, j, k) = (fluxRight - fluxLeft) / (coord->dy(i, j, k) * coord->J(i, j, k)); @@ -290,7 +290,7 @@ Field3D Div_par_flux(const Field3D& v, const Field3D& f, CELL_LOC outloc, if (!f.hasParallelSlices()) { return metric->Bxy * FDDY(v, f / Bxy_floc, outloc, method) - / sqrt(metric->getCovariantMetricTensor().g22); + / sqrt(metric->getCovariantMetricTensor().g_22); } // Need to modify yup and ydown fields @@ -301,7 +301,7 @@ Field3D Div_par_flux(const Field3D& v, const Field3D& f, CELL_LOC outloc, f_B.ydown() = f.ydown() / Bxy_floc; return metric->Bxy * FDDY(v, f_B, outloc, method) - / sqrt(metric->getCovariantMetricTensor().g22); + / sqrt(metric->getCovariantMetricTensor().g_22); } Field3D Div_par_flux(const Field3D& v, const Field3D& f, const std::string& method, @@ -479,15 +479,15 @@ Coordinates::FieldMetric b0xGrad_dot_Grad(const Field2D& phi, const Field2D& A, Coordinates::FieldMetric dpdx = DDX(phi, outloc); Coordinates::FieldMetric dpdy = DDY(phi, outloc); - Coordinates::MetricTensor covariant_components = metric->getCovariantMetricTensor(); + Coordinates::CovariantMetricTensor covariant_components = metric->getCovariantMetricTensor(); // Calculate advection velocity - Coordinates::FieldMetric vx = -covariant_components.g23 * dpdy; - Coordinates::FieldMetric vy = covariant_components.g23 * dpdx; + Coordinates::FieldMetric vx = -covariant_components.g_23 * dpdy; + Coordinates::FieldMetric vy = covariant_components.g_23 * dpdx; // Upwind A using these velocities Coordinates::FieldMetric result = VDDX(vx, A, outloc) + VDDY(vy, A, outloc); - result /= metric->J * sqrt(covariant_components.g22); + result /= metric->J * sqrt(covariant_components.g_22); ASSERT1(result.getLocation() == outloc); @@ -514,12 +514,12 @@ Field3D b0xGrad_dot_Grad(const Field2D& phi, const Field3D& A, CELL_LOC outloc) Coordinates::FieldMetric dpdx = DDX(phi, outloc); Coordinates::FieldMetric dpdy = DDY(phi, outloc); - Coordinates::MetricTensor covariant_components = metric->getCovariantMetricTensor(); + Coordinates::CovariantMetricTensor covariant_components = metric->getCovariantMetricTensor(); // Calculate advection velocity - Coordinates::FieldMetric vx = -covariant_components.g23 * dpdy; - Coordinates::FieldMetric vy = covariant_components.g23 * dpdx; - Coordinates::FieldMetric vz = covariant_components.g12 * dpdy - covariant_components.g22 * dpdx; + Coordinates::FieldMetric vx = -covariant_components.g_23 * dpdy; + Coordinates::FieldMetric vy = covariant_components.g_23 * dpdx; + Coordinates::FieldMetric vz = covariant_components.g_12 * dpdy - covariant_components.g_22 * dpdx; if (mesh->IncIntShear) { // BOUT-06 style differencing @@ -530,7 +530,7 @@ Field3D b0xGrad_dot_Grad(const Field2D& phi, const Field3D& A, CELL_LOC outloc) Field3D result = VDDX(vx, A, outloc) + VDDY(vy, A, outloc) + VDDZ(vz, A, outloc); - result /= (metric->J * sqrt(covariant_components.g22)); + result /= (metric->J * sqrt(covariant_components.g_22)); #if BOUT_USE_TRACK result.name = "b0xGrad_dot_Grad(" + phi.name + "," + A.name + ")"; @@ -557,17 +557,17 @@ Field3D b0xGrad_dot_Grad(const Field3D& p, const Field2D& A, CELL_LOC outloc) { Field3D dpdy = DDY(p, outloc); Field3D dpdz = DDZ(p, outloc); - Coordinates::MetricTensor covariant_components = metric->getCovariantMetricTensor(); + Coordinates::CovariantMetricTensor covariant_components = metric->getCovariantMetricTensor(); // Calculate advection velocity - Field3D vx = covariant_components.g22 * dpdz - covariant_components.g23 * dpdy; - Field3D vy = covariant_components.g23 * dpdx - covariant_components.g12 * dpdz; + Field3D vx = covariant_components.g_22 * dpdz - covariant_components.g_23 * dpdy; + Field3D vy = covariant_components.g_23 * dpdx - covariant_components.g_12 * dpdz; // Upwind A using these velocities Field3D result = VDDX(vx, A, outloc) + VDDY(vy, A, outloc); - result /= (metric->J * sqrt(covariant_components.g22)); + result /= (metric->J * sqrt(covariant_components.g_22)); #if BOUT_USE_TRACK result.name = "b0xGrad_dot_Grad(" + p.name + "," + A.name + ")"; @@ -596,12 +596,12 @@ Field3D b0xGrad_dot_Grad(const Field3D& phi, const Field3D& A, CELL_LOC outloc) Field3D dpdy = DDY(phi, outloc); Field3D dpdz = DDZ(phi, outloc); - Coordinates::MetricTensor covariant_components = metric->getCovariantMetricTensor(); + Coordinates::CovariantMetricTensor covariant_components = metric->getCovariantMetricTensor(); // Calculate advection velocity - Field3D vx = covariant_components.g22 * dpdz - covariant_components.g23 * dpdy; - Field3D vy = covariant_components.g23 * dpdx - covariant_components.g12 * dpdz; - Field3D vz = covariant_components.g12 * dpdy - covariant_components.g22 * dpdx; + Field3D vx = covariant_components.g_22 * dpdz - covariant_components.g_23 * dpdy; + Field3D vy = covariant_components.g_23 * dpdx - covariant_components.g_12 * dpdz; + Field3D vz = covariant_components.g_12 * dpdy - covariant_components.g_22 * dpdx; if (mesh->IncIntShear) { // BOUT-06 style differencing @@ -610,7 +610,7 @@ Field3D b0xGrad_dot_Grad(const Field3D& phi, const Field3D& A, CELL_LOC outloc) Field3D result = VDDX(vx, A, outloc) + VDDY(vy, A, outloc) + VDDZ(vz, A, outloc); - result /= (metric->J * sqrt(covariant_components.g22)); + result /= (metric->J * sqrt(covariant_components.g_22)); #if BOUT_USE_TRACK result.name = "b0xGrad_dot_Grad(" + phi.name + "," + A.name + ")"; diff --git a/src/mesh/fv_ops.cxx b/src/mesh/fv_ops.cxx index 516efc9a6b..bd6cc9b654 100644 --- a/src/mesh/fv_ops.cxx +++ b/src/mesh/fv_ops.cxx @@ -31,7 +31,7 @@ Field3D Div_a_Grad_perp(const Field3D& a, const Field3D& f) { Field3D result{zeroFrom(f)}; Coordinates* coord = f.getCoordinates(); - Coordinates::MetricTensor g = coord->getContravariantMetricTensor(); + Coordinates::ContravariantMetricTensor g = coord->getContravariantMetricTensor(); // Flux in x @@ -70,8 +70,8 @@ Field3D Div_a_Grad_perp(const Field3D& a, const Field3D& f) { // 3D Metric, need yup/ydown fields. // Requires previous communication of metrics // -- should insert communication here? - Coordinates::MetricTensor covariant_components = coord->getCovariantMetricTensor(); - if (!g.g23.hasParallelSlices() || !covariant_components.g23.hasParallelSlices() + Coordinates::CovariantMetricTensor covariant_components = coord->getCovariantMetricTensor(); + if (!g.g23.hasParallelSlices() || !covariant_components.g_23.hasParallelSlices() || !coord->dy.hasParallelSlices() || !coord->dz.hasParallelSlices() || !coord->Bxy.hasParallelSlices() || !coord->J.hasParallelSlices()) { throw BoutException("metrics have no yup/down: Maybe communicate in init?"); @@ -91,7 +91,7 @@ Field3D Div_a_Grad_perp(const Field3D& a, const Field3D& f) { const bool metric_fci = fci and bout::build::use_metric_3d; const auto g23 = makeslices(metric_fci, g.g23); const auto g_23 = makeslices(metric_fci, - coord->getCovariantMetricTensor().g23); + coord->getCovariantMetricTensor().g_23); const auto J = makeslices(metric_fci, coord->J); const auto dy = makeslices(metric_fci, coord->dy); const auto dz = makeslices(metric_fci, coord->dz); @@ -211,14 +211,14 @@ const Field3D Div_par_K_Grad_par(const Field3D& Kin, const Field3D& fin, const auto iyp = i.yp(); const auto iym = i.ym(); - Coordinates::MetricTensor covariant_components = coord->getCovariantMetricTensor(); + Coordinates::CovariantMetricTensor covariant_components = coord->getCovariantMetricTensor(); if (bndry_flux || mesh->periodicY(i.x()) || !mesh->lastY(i.x()) || (i.y() != mesh->yend)) { BoutReal c = 0.5 * (K[i] + Kup[iyp]); // K at the upper boundary BoutReal J = 0.5 * (coord->J[i] + coord->J[iyp]); // Jacobian at boundary - BoutReal g_22 = 0.5 * (covariant_components.g22[i] + covariant_components.g22[iyp]); + BoutReal g_22 = 0.5 * (covariant_components.g_22[i] + covariant_components.g_22[iyp]); BoutReal gradient = 2. * (fup[iyp] - f[i]) / (coord->dy[i] + coord->dy[iyp]); @@ -233,7 +233,7 @@ const Field3D Div_par_K_Grad_par(const Field3D& Kin, const Field3D& fin, BoutReal c = 0.5 * (K[i] + Kdown[iym]); // K at the lower boundary BoutReal J = 0.5 * (coord->J[i] + coord->J[iym]); // Jacobian at boundary - BoutReal g_22 = 0.5 * (covariant_components.g22[i] + covariant_components.g22[iym]); + BoutReal g_22 = 0.5 * (covariant_components.g_22[i] + covariant_components.g_22[iym]); BoutReal gradient = 2. * (f[i] - fdown[iym]) / (coord->dy[i] + coord->dy[iym]); @@ -503,7 +503,7 @@ Field3D Div_Perp_Lap(const Field3D& a, const Field3D& f, CELL_LOC outloc) { // Coordinates* coords = a.getCoordinates(outloc); Mesh* mesh = f.getMesh(); - Coordinates::MetricTensor g = coords->getContravariantMetricTensor(); + Coordinates::ContravariantMetricTensor g = coords->getContravariantMetricTensor(); for (int i = mesh->xstart; i <= mesh->xend; i++) { for (int j = mesh->ystart; j <= mesh->yend; j++) { diff --git a/tests/MMS/GBS/gbs.cxx b/tests/MMS/GBS/gbs.cxx index ea564f5286..ff572ad393 100644 --- a/tests/MMS/GBS/gbs.cxx +++ b/tests/MMS/GBS/gbs.cxx @@ -362,12 +362,12 @@ void GBS::LoadMetric(BoutReal Lnorm, BoutReal Bnorm) { coords->J = hthe / Bpxy; Coordinates::MetricTensor covariant_components; - covariant_components.g11 = 1.0 / contravariant_components.g11 + SQ(sinty * Rxy); - covariant_components.g22 = SQ(coords->Bxy * hthe / Bpxy); - covariant_components.g33 = Rxy * Rxy; - covariant_components.g12 = sbp * Btxy * hthe * sinty * Rxy / Bpxy; - covariant_components.g13 = sinty * Rxy * Rxy; - covariant_components.g23 = sbp * Btxy * hthe * Rxy / Bpxy; + covariant_components.g_11 = 1.0 / contravariant_components.g11 + SQ(sinty * Rxy); + covariant_components.g_22 = SQ(coords->Bxy * hthe / Bpxy); + covariant_components.g_33 = Rxy * Rxy; + covariant_components.g_12 = sbp * Btxy * hthe * sinty * Rxy / Bpxy; + covariant_components.g_13 = sinty * Rxy * Rxy; + covariant_components.g_23 = sbp * Btxy * hthe * Rxy / Bpxy; coords->setCovariantMetricTensor(covariant_components); coords->geometry(); diff --git a/tests/MMS/diffusion/diffusion.cxx b/tests/MMS/diffusion/diffusion.cxx index a885603834..8ae1f2a861 100644 --- a/tests/MMS/diffusion/diffusion.cxx +++ b/tests/MMS/diffusion/diffusion.cxx @@ -43,7 +43,7 @@ int Diffusion::init(bool UNUSED(restarting)) { SAVE_ONCE(mu_N); //set mesh - Coordinates::MetricTensor contravariant_components; + Coordinates::ContravariantMetricTensor contravariant_components; contravariant_components.g11 = 1.0; contravariant_components.g22 = 1.0; contravariant_components.g33 = 1.0; @@ -52,13 +52,13 @@ int Diffusion::init(bool UNUSED(restarting)) { contravariant_components.g23 = 0.0; coord->setContravariantMetricTensor(contravariant_components); - Coordinates::MetricTensor covariant_components; - covariant_components.g11 = 1.0; - covariant_components.g22 = 1.0; - covariant_components.g33 = 1.0; - covariant_components.g12 = 0.0; - covariant_components.g13 = 0.0; - covariant_components.g23 = 0.0; + Coordinates::CovariantMetricTensor covariant_components; + covariant_components.g_11 = 1.0; + covariant_components.g_22 = 1.0; + covariant_components.g_33 = 1.0; + covariant_components.g_12 = 0.0; + covariant_components.g_13 = 0.0; + covariant_components.g_23 = 0.0; coord->setCovariantMetricTensor(covariant_components); coord->geometry(); diff --git a/tests/MMS/diffusion2/diffusion.cxx b/tests/MMS/diffusion2/diffusion.cxx index 3dd77c06f8..dd7928097a 100644 --- a/tests/MMS/diffusion2/diffusion.cxx +++ b/tests/MMS/diffusion2/diffusion.cxx @@ -48,12 +48,12 @@ class Diffusion : public PhysicsModel { coords->setContravariantMetricTensor(contravariant_components); Coordinates::MetricTensor covariant_components; - covariant_components.g11 = 1.0; - covariant_components.g22 = 1.0; - covariant_components.g33 = 1.0; - covariant_components.g12 = 0.0; - covariant_components.g13 = 0.0; - covariant_components.g23 = 0.0; + covariant_components.g_11 = 1.0; + covariant_components.g_22 = 1.0; + covariant_components.g_33 = 1.0; + covariant_components.g_12 = 0.0; + covariant_components.g_13 = 0.0; + covariant_components.g_23 = 0.0; coords->setCovariantMetricTensor(covariant_components); coords->geometry(); diff --git a/tests/MMS/wave-1d/wave.cxx b/tests/MMS/wave-1d/wave.cxx index 69ea122160..6dfc56b63f 100644 --- a/tests/MMS/wave-1d/wave.cxx +++ b/tests/MMS/wave-1d/wave.cxx @@ -32,7 +32,7 @@ class Wave1D : public PhysicsModel { SAVE_ONCE(Lx, Ly); //set mesh - Coordinates::MetricTensor contravariant_components = coord->getContravariantMetricTensor(); + Coordinates::ContravariantMetricTensor contravariant_components = coord->getContravariantMetricTensor(); contravariant_components.g11 = 1.0; contravariant_components.g22 = 1.0; contravariant_components.g33 = 1.0; @@ -41,13 +41,13 @@ class Wave1D : public PhysicsModel { contravariant_components.g23 = 0.0; coord->setContravariantMetricTensor(contravariant_components); - Coordinates::MetricTensor covariant_components; - covariant_components.g11 = 1.0; - covariant_components.g22 = 1.0; - covariant_components.g33 = 1.0; - covariant_components.g12 = 0.0; - covariant_components.g13 = 0.0; - covariant_components.g23 = 0.0; + Coordinates::CovariantMetricTensor covariant_components; + covariant_components.g_11 = 1.0; + covariant_components.g_22 = 1.0; + covariant_components.g_33 = 1.0; + covariant_components.g_12 = 0.0; + covariant_components.g_13 = 0.0; + covariant_components.g_23 = 0.0; coord->setCovariantMetricTensor(covariant_components); coord->geometry(); diff --git a/tests/integrated/test-drift-instability/2fluid.cxx b/tests/integrated/test-drift-instability/2fluid.cxx index 8c85f0f410..da9e39882e 100644 --- a/tests/integrated/test-drift-instability/2fluid.cxx +++ b/tests/integrated/test-drift-instability/2fluid.cxx @@ -217,7 +217,7 @@ class TwoFluid : public PhysicsModel { /**************** CALCULATE METRICS ******************/ - Coordinates::MetricTensor contravariant_components; + Coordinates::ContravariantMetricTensor contravariant_components; contravariant_components.g11 = SQ(Rxy * Bpxy); contravariant_components.g22 = 1.0 / SQ(hthe); contravariant_components.g33 = SQ(I) * contravariant_components.g11 + SQ(coord->Bxy) / contravariant_components.g11; @@ -228,13 +228,13 @@ class TwoFluid : public PhysicsModel { coord->J = hthe / Bpxy; - Coordinates::MetricTensor covariant_components; - covariant_components.g11 = 1.0 / contravariant_components.g11 + SQ(I * Rxy); - covariant_components.g22 = SQ(coord->Bxy * hthe / Bpxy); - covariant_components.g33 = Rxy * Rxy; - covariant_components.g12 = Btxy * hthe * I * Rxy / Bpxy; - covariant_components.g13 = I * Rxy * Rxy; - covariant_components.g23 = Btxy * hthe * Rxy / Bpxy; + Coordinates::CovariantMetricTensor covariant_components; + covariant_components.g_11 = 1.0 / contravariant_components.g11 + SQ(I * Rxy); + covariant_components.g_22 = SQ(coord->Bxy * hthe / Bpxy); + covariant_components.g_33 = Rxy * Rxy; + covariant_components.g_12 = Btxy * hthe * I * Rxy / Bpxy; + covariant_components.g_13 = I * Rxy * Rxy; + covariant_components.g_23 = Btxy * hthe * Rxy / Bpxy; coord->setCovariantMetricTensor(covariant_components); coord->geometry(); diff --git a/tests/integrated/test-interchange-instability/2fluid.cxx b/tests/integrated/test-interchange-instability/2fluid.cxx index 139fd8a361..e070b34432 100644 --- a/tests/integrated/test-interchange-instability/2fluid.cxx +++ b/tests/integrated/test-interchange-instability/2fluid.cxx @@ -139,7 +139,7 @@ class Interchange : public PhysicsModel { /**************** CALCULATE METRICS ******************/ - Coordinates::MetricTensor contravariant_components; + Coordinates::ContravariantMetricTensor contravariant_components; contravariant_components.g11 = SQ(Rxy * Bpxy); contravariant_components.g22 = 1.0 / SQ(hthe); contravariant_components.g33 = SQ(I) * contravariant_components.g11 + SQ(coord->Bxy) / contravariant_components.g11; @@ -150,13 +150,13 @@ class Interchange : public PhysicsModel { coord->J = hthe / Bpxy; - Coordinates::MetricTensor covariant_components; - covariant_components.g11 = 1.0 / contravariant_components.g11 + SQ(I * Rxy); - covariant_components.g22 = SQ(coord->Bxy * hthe / Bpxy); - covariant_components.g33 = Rxy * Rxy; - covariant_components.g12 = Btxy * hthe * I * Rxy / Bpxy; - covariant_components.g13 = I * Rxy * Rxy; - covariant_components.g23 = Btxy * hthe * Rxy / Bpxy; + Coordinates::CovariantMetricTensor covariant_components; + covariant_components.g_11 = 1.0 / contravariant_components.g11 + SQ(I * Rxy); + covariant_components.g_22 = SQ(coord->Bxy * hthe / Bpxy); + covariant_components.g_33 = Rxy * Rxy; + covariant_components.g_12 = Btxy * hthe * I * Rxy / Bpxy; + covariant_components.g_13 = I * Rxy * Rxy; + covariant_components.g_23 = Btxy * hthe * Rxy / Bpxy; coord->setCovariantMetricTensor(covariant_components); coord->geometry(); diff --git a/tests/integrated/test-multigrid_laplace/test_multigrid_laplace.cxx b/tests/integrated/test-multigrid_laplace/test_multigrid_laplace.cxx index e984087514..89ba6c0eb4 100644 --- a/tests/integrated/test-multigrid_laplace/test_multigrid_laplace.cxx +++ b/tests/integrated/test-multigrid_laplace/test_multigrid_laplace.cxx @@ -242,7 +242,7 @@ int main(int argc, char** argv) { x0(mesh->xstart - 1, mesh->ystart, k) = (f4(mesh->xstart, mesh->ystart, k) - f4(mesh->xstart - 1, mesh->ystart, k)) / mesh->getCoordinates()->dx(mesh->xstart, mesh->ystart, k) - / sqrt(mesh->getCoordinates()->getCovariantMetricTensor().g11(mesh->xstart, mesh->ystart, k)); + / sqrt(mesh->getCoordinates()->getCovariantMetricTensor().g_11(mesh->xstart, mesh->ystart, k)); } } if (mesh->lastX()) { @@ -250,7 +250,7 @@ int main(int argc, char** argv) { x0(mesh->xend + 1, mesh->ystart, k) = (f4(mesh->xend + 1, mesh->ystart, k) - f4(mesh->xend, mesh->ystart, k)) / mesh->getCoordinates()->dx(mesh->xend, mesh->ystart, k) - / sqrt(mesh->getCoordinates()->getCovariantMetricTensor().g11(mesh->xend, mesh->ystart, k)); + / sqrt(mesh->getCoordinates()->getCovariantMetricTensor().g_11(mesh->xend, mesh->ystart, k)); } } diff --git a/tests/integrated/test-naulin-laplace/test_naulin_laplace.cxx b/tests/integrated/test-naulin-laplace/test_naulin_laplace.cxx index 9eab3f9697..ff737200f9 100644 --- a/tests/integrated/test-naulin-laplace/test_naulin_laplace.cxx +++ b/tests/integrated/test-naulin-laplace/test_naulin_laplace.cxx @@ -246,7 +246,7 @@ int main(int argc, char** argv) { x0(mesh->xstart - 1, mesh->ystart, k) = (f4(mesh->xstart, mesh->ystart, k) - f4(mesh->xstart - 1, mesh->ystart, k)) / mesh->getCoordinates()->dx(mesh->xstart, mesh->ystart, k) - / sqrt(mesh->getCoordinates()->getCovariantMetricTensor().g11(mesh->xstart, mesh->ystart, k)); + / sqrt(mesh->getCoordinates()->getCovariantMetricTensor().g_11(mesh->xstart, mesh->ystart, k)); } } if (mesh->lastX()) { @@ -254,7 +254,7 @@ int main(int argc, char** argv) { x0(mesh->xend + 1, mesh->ystart, k) = (f4(mesh->xend + 1, mesh->ystart, k) - f4(mesh->xend, mesh->ystart, k)) / mesh->getCoordinates()->dx(mesh->xend, mesh->ystart, k) - / sqrt(mesh->getCoordinates()->getCovariantMetricTensor().g11(mesh->xend, mesh->ystart, k)); + / sqrt(mesh->getCoordinates()->getCovariantMetricTensor().g_11(mesh->xend, mesh->ystart, k)); } } @@ -304,7 +304,7 @@ int main(int argc, char** argv) { Field3D this_Grad_perp_dot_Grad_perp(const Field3D& f, const Field3D& g) { const auto* coords = f.getCoordinates(); - Coordinates::MetricTensor metric_tensor = coords->getContravariantMetricTensor(); + Coordinates::ContravariantMetricTensor metric_tensor = coords->getContravariantMetricTensor(); Field3D result = metric_tensor.g11 * ::DDX(f) * ::DDX(g) + metric_tensor.g33 * ::DDZ(f) * ::DDZ(g) + metric_tensor.g13 * (DDX(f) * DDZ(g) + DDZ(f) * DDX(g)); diff --git a/tests/unit/invert/laplace/test_laplace_cyclic.cxx b/tests/unit/invert/laplace/test_laplace_cyclic.cxx index 279e124030..c22bb8587f 100644 --- a/tests/unit/invert/laplace/test_laplace_cyclic.cxx +++ b/tests/unit/invert/laplace/test_laplace_cyclic.cxx @@ -39,7 +39,7 @@ class CyclicForwardOperator { } const Field3D operator()(Field3D& f) { - Coordinates::MetricTensor contravariant_components = coords->getContravariantMetricTensor(); + Coordinates::ContravariantMetricTensor contravariant_components = coords->getContravariantMetricTensor(); auto result = d * Delp2(f) + (contravariant_components.g11 * DDX(f) + contravariant_components.g13 * DDZ(f)) * DDX(c2) / c1 + a * f + ex * DDX(f) + ez * DDZ(f); @@ -52,10 +52,10 @@ class CyclicForwardOperator { bool inner_x_neumann, outer_x_neumann; // If false then use Dirichlet conditions void applyBoundaries(Field3D& newF, const Field3D& f) { - Coordinates::MetricTensor covariant_components = coords->getCovariantMetricTensor(); + Coordinates::CovariantMetricTensor covariant_components = coords->getCovariantMetricTensor(); BOUT_FOR(i, f.getMesh()->getRegion3D("RGN_INNER_X")) { if (inner_x_neumann) { - newF[i] = (f[i.xp()] - f[i]) / coords->dx[i] / sqrt(covariant_components.g11[i]); + newF[i] = (f[i.xp()] - f[i]) / coords->dx[i] / sqrt(covariant_components.g_11[i]); } else { newF[i] = 0.5 * (f[i] + f[i.xp()]); } @@ -63,7 +63,7 @@ class CyclicForwardOperator { BOUT_FOR(i, f.getMesh()->getRegion3D("RGN_OUTER_X")) { if (outer_x_neumann) { - newF[i] = (f[i] - f[i.xm()]) / coords->dx[i] / sqrt(covariant_components.g11[i]); + newF[i] = (f[i] - f[i.xm()]) / coords->dx[i] / sqrt(covariant_components.g_11[i]); } else { newF[i] = 0.5 * (f[i.xm()] + f[i]); } diff --git a/tests/unit/mesh/data/test_gridfromoptions.cxx b/tests/unit/mesh/data/test_gridfromoptions.cxx index 00293f4e4f..0738d47525 100644 --- a/tests/unit/mesh/data/test_gridfromoptions.cxx +++ b/tests/unit/mesh/data/test_gridfromoptions.cxx @@ -359,7 +359,7 @@ TEST_F(GridFromOptionsTest, CoordinatesCentre) { mesh_from_options.communicate(expected_2d); - Coordinates::MetricTensor g = coords->getContravariantMetricTensor(); + Coordinates::ContravariantMetricTensor g = coords->getContravariantMetricTensor(); EXPECT_TRUE(IsFieldEqual(g.g11, expected_metric + 5.)); EXPECT_TRUE(IsFieldEqual(g.g22, expected_metric + 4.)); @@ -375,7 +375,7 @@ TEST_F(GridFromOptionsTest, CoordinatesZlow) { mesh_from_options.communicate(expected_2d); - Coordinates::MetricTensor g = coords->getContravariantMetricTensor(); + Coordinates::ContravariantMetricTensor g = coords->getContravariantMetricTensor(); EXPECT_TRUE(IsFieldEqual(g.g11, expected_metric + 5.)); EXPECT_TRUE(IsFieldEqual(g.g22, expected_metric + 4.)); @@ -405,7 +405,7 @@ TEST_F(GridFromOptionsTest, CoordinatesXlowInterp) { mesh_from_options.communicate(expected_xlow); - Coordinates::MetricTensor g = coords->getContravariantMetricTensor(); + Coordinates::ContravariantMetricTensor g = coords->getContravariantMetricTensor(); EXPECT_TRUE( IsFieldEqual(g.g11, expected_xlow + 5., "RGN_NOBNDRY", this_tolerance)); @@ -449,7 +449,7 @@ TEST_F(GridFromOptionsTest, CoordinatesXlowRead) { mesh_from_options.communicate(expected_xlow); - Coordinates::MetricTensor g = coords->getContravariantMetricTensor(); + Coordinates::ContravariantMetricTensor g = coords->getContravariantMetricTensor(); EXPECT_TRUE(IsFieldEqual(g.g11, expected_xlow + 5.)); EXPECT_TRUE(g.g11.getLocation() == CELL_XLOW); @@ -483,7 +483,7 @@ TEST_F(GridFromOptionsTest, CoordinatesYlowInterp) { mesh_from_options.communicate(expected_ylow); - Coordinates::MetricTensor g = coords->getContravariantMetricTensor(); + Coordinates::ContravariantMetricTensor g = coords->getContravariantMetricTensor(); EXPECT_TRUE( IsFieldEqual(g.g11, expected_ylow + 5., "RGN_NOBNDRY", this_tolerance)); @@ -536,7 +536,7 @@ TEST_F(GridFromOptionsTest, CoordinatesYlowRead) { mesh_from_options.communicate(expected_ylow); - Coordinates::MetricTensor g = coords->getContravariantMetricTensor(); + Coordinates::ContravariantMetricTensor g = coords->getContravariantMetricTensor(); EXPECT_TRUE(IsFieldEqual(g.g11, expected_ylow + 5., "RGN_ALL", this_tolerance)); EXPECT_TRUE(g.g11.getLocation() == CELL_YLOW); @@ -560,7 +560,7 @@ TEST_F(GridFromOptionsTest, CoordinatesZlowRead) { auto coords = mesh_from_options.getCoordinates(CELL_ZLOW); - Coordinates::MetricTensor g = coords->getContravariantMetricTensor(); + Coordinates::ContravariantMetricTensor g = coords->getContravariantMetricTensor(); EXPECT_TRUE(IsFieldEqual(g.g11, expected_2d + 5.)); EXPECT_TRUE(IsFieldEqual(g.g22, expected_2d + 4.)); diff --git a/tests/unit/mesh/test_coordinates.cxx b/tests/unit/mesh/test_coordinates.cxx index 95fdc2e8e9..8c4594de0f 100644 --- a/tests/unit/mesh/test_coordinates.cxx +++ b/tests/unit/mesh/test_coordinates.cxx @@ -370,7 +370,7 @@ TEST_F(CoordinatesTest, SetContravariantMetricTensor) { FieldMetric{0.0}}; // IntShiftTorsion // Modify with setter - Coordinates::MetricTensor updated_metric_tensor; + Coordinates::ContravariantMetricTensor updated_metric_tensor; updated_metric_tensor.g11 = 1.7; updated_metric_tensor.g22 = 2.3; updated_metric_tensor.g33 = 3.1; @@ -413,12 +413,12 @@ TEST_F(CoordinatesTest, GetCovariantMetricTensor) { const auto covariant_components = coords.getCovariantMetricTensor(); - EXPECT_TRUE(IsFieldEqual(covariant_components.g11, 9.7)); - EXPECT_TRUE(IsFieldEqual(covariant_components.g22, 7.5)); - EXPECT_TRUE(IsFieldEqual(covariant_components.g33, 4.7)); - EXPECT_TRUE(IsFieldEqual(covariant_components.g12, 3.9)); - EXPECT_TRUE(IsFieldEqual(covariant_components.g13, 1.7)); - EXPECT_TRUE(IsFieldEqual(covariant_components.g23, 5.3)); + EXPECT_TRUE(IsFieldEqual(covariant_components.g_11, 9.7)); + EXPECT_TRUE(IsFieldEqual(covariant_components.g_22, 7.5)); + EXPECT_TRUE(IsFieldEqual(covariant_components.g_33, 4.7)); + EXPECT_TRUE(IsFieldEqual(covariant_components.g_12, 3.9)); + EXPECT_TRUE(IsFieldEqual(covariant_components.g_13, 1.7)); + EXPECT_TRUE(IsFieldEqual(covariant_components.g_23, 5.3)); } TEST_F(CoordinatesTest, SetCovariantMetricTensor) { @@ -446,13 +446,13 @@ TEST_F(CoordinatesTest, SetCovariantMetricTensor) { FieldMetric{0.0}}; // IntShiftTorsion // Modify with setter - Coordinates::MetricTensor updated_metric_tensor; - updated_metric_tensor.g11 = 1.7; - updated_metric_tensor.g22 = 2.3; - updated_metric_tensor.g33 = 3.1; - updated_metric_tensor.g12 = 0.9; - updated_metric_tensor.g13 = 5.7; - updated_metric_tensor.g23 = 1.9; + Coordinates::CovariantMetricTensor updated_metric_tensor; + updated_metric_tensor.g_11 = 1.7; + updated_metric_tensor.g_22 = 2.3; + updated_metric_tensor.g_33 = 3.1; + updated_metric_tensor.g_12 = 0.9; + updated_metric_tensor.g_13 = 5.7; + updated_metric_tensor.g_23 = 1.9; coords.setCovariantMetricTensor(updated_metric_tensor); // Get values with getter and check they have been modified as expected From 3a7dc7199f4c74a0e39581313cb45305a8027399 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Mon, 25 Sep 2023 09:46:34 +0100 Subject: [PATCH 044/491] Use const auto instead of specific type name. --- src/field/vecops.cxx | 4 ++-- src/field/vector2d.cxx | 20 ++++++++-------- src/field/vector3d.cxx | 24 +++++++++---------- .../impls/multigrid/multigrid_laplace.cxx | 10 ++++---- .../laplace/impls/naulin/naulin_laplace.cxx | 2 +- .../laplace/impls/serial_band/serial_band.cxx | 10 ++++---- src/invert/laplace/invert_laplace.cxx | 6 ++--- .../impls/cyclic/laplacexz-cyclic.cxx | 2 +- src/mesh/boundary_standard.cxx | 14 +++++------ src/mesh/difops.cxx | 12 +++++----- src/mesh/fv_ops.cxx | 8 +++---- tests/MMS/diffusion/diffusion.cxx | 2 +- tests/MMS/wave-1d/wave.cxx | 4 ++-- .../test-drift-instability/2fluid.cxx | 4 ++-- .../test-interchange-instability/2fluid.cxx | 4 ++-- .../test_naulin_laplace.cxx | 2 +- .../invert/laplace/test_laplace_cyclic.cxx | 4 ++-- tests/unit/mesh/data/test_gridfromoptions.cxx | 14 +++++------ tests/unit/mesh/test_coordinates.cxx | 4 ++-- 19 files changed, 75 insertions(+), 75 deletions(-) diff --git a/src/field/vecops.cxx b/src/field/vecops.cxx index 5fb9744931..c88bc893df 100644 --- a/src/field/vecops.cxx +++ b/src/field/vecops.cxx @@ -105,7 +105,7 @@ Vector3D Grad_perp(const Field3D& f, CELL_LOC outloc, const std::string& method) Vector3D result(f.getMesh()); - Coordinates::CovariantMetricTensor covariant_components = metric->getCovariantMetricTensor(); + const auto covariant_components = metric->getCovariantMetricTensor(); result.x = DDX(f, outloc, method) - covariant_components.g_12 * DDY(f, outloc, method) / SQ(metric->J * metric->Bxy); result.y = 0.0; @@ -128,7 +128,7 @@ Vector2D Grad_perp(const Field2D& f, CELL_LOC outloc, const std::string& method) Vector2D result(f.getMesh()); - Coordinates::CovariantMetricTensor covariant_components = metric->getCovariantMetricTensor(); + const auto covariant_components = metric->getCovariantMetricTensor(); result.x = DDX(f, outloc, method) - covariant_components.g_12 * DDY(f, outloc, method) / SQ(metric->J * metric->Bxy); result.y = 0.0; diff --git a/src/field/vector2d.cxx b/src/field/vector2d.cxx index eba1a86418..bc38515b73 100644 --- a/src/field/vector2d.cxx +++ b/src/field/vector2d.cxx @@ -84,9 +84,9 @@ void Vector2D::toCovariant() { const auto x_at_z = interp_to(x, z.getLocation()); const auto y_at_z = interp_to(y, z.getLocation()); - Coordinates::CovariantMetricTensor covariant_components_x = metric_x->getCovariantMetricTensor(); - Coordinates::CovariantMetricTensor covariant_components_y = metric_y->getCovariantMetricTensor(); - Coordinates::CovariantMetricTensor covariant_components_z = metric_z->getCovariantMetricTensor(); + const auto covariant_components_x = metric_x->getCovariantMetricTensor(); + const auto covariant_components_y = metric_y->getCovariantMetricTensor(); + const auto covariant_components_z = metric_z->getCovariantMetricTensor(); // multiply by g_{ij} BOUT_FOR(i, x.getRegion("RGN_ALL")) { @@ -103,7 +103,7 @@ void Vector2D::toCovariant() { // Need to use temporary arrays to store result Coordinates::FieldMetric gx{emptyFrom(x)}, gy{emptyFrom(y)}, gz{emptyFrom(z)}; - Coordinates::CovariantMetricTensor covariant_components = metric->getCovariantMetricTensor(); + const auto covariant_components = metric->getCovariantMetricTensor(); BOUT_FOR(i, x.getRegion("RGN_ALL")) { gx[i] = covariant_components.g_11[i] * x[i] + covariant_components.g_12[i] * y[i] + covariant_components.g_13[i] * z[i]; gy[i] = covariant_components.g_22[i] * y[i] + covariant_components.g_12[i] * x[i] + covariant_components.g_23[i] * z[i]; @@ -143,9 +143,9 @@ void Vector2D::toContravariant() { const auto x_at_z = interp_to(x, z.getLocation()); const auto y_at_z = interp_to(y, z.getLocation()); - Coordinates::ContravariantMetricTensor g_x = metric_x->getContravariantMetricTensor(); - Coordinates::ContravariantMetricTensor g_y = metric_y->getContravariantMetricTensor(); - Coordinates::ContravariantMetricTensor g_z = metric_z->getContravariantMetricTensor(); + const auto g_x = metric_x->getContravariantMetricTensor(); + const auto g_y = metric_y->getContravariantMetricTensor(); + const auto g_z = metric_z->getContravariantMetricTensor(); // multiply by g_{ij} BOUT_FOR(i, x.getRegion("RGN_ALL")) { @@ -160,7 +160,7 @@ void Vector2D::toContravariant() { // Need to use temporary arrays to store result Coordinates::FieldMetric gx{emptyFrom(x)}, gy{emptyFrom(y)}, gz{emptyFrom(z)}; - Coordinates::ContravariantMetricTensor contravariant_components = metric->getContravariantMetricTensor(); + const auto contravariant_components = metric->getContravariantMetricTensor(); BOUT_FOR(i, x.getRegion("RGN_ALL")) { gx[i] = contravariant_components.g11[i] * x[i] + contravariant_components.g12[i] * y[i] + contravariant_components.g13[i] * z[i]; @@ -399,7 +399,7 @@ const Coordinates::FieldMetric Vector2D::operator*(const Vector2D& rhs) const { if (covariant) { // Both covariant - Coordinates::ContravariantMetricTensor contravariant_components = metric->getContravariantMetricTensor(); + const auto contravariant_components = metric->getContravariantMetricTensor(); result = x * rhs.x * contravariant_components.g11 + y * rhs.y * contravariant_components.g22 + z * rhs.z * contravariant_components.g33; result += (x * rhs.y + y * rhs.x) * contravariant_components.g12 @@ -407,7 +407,7 @@ const Coordinates::FieldMetric Vector2D::operator*(const Vector2D& rhs) const { + (y * rhs.z + z * rhs.y) * contravariant_components.g23; } else { // Both contravariant - Coordinates::CovariantMetricTensor covariant_components = metric->getCovariantMetricTensor(); + const auto covariant_components = metric->getCovariantMetricTensor(); result = x * rhs.x * covariant_components.g_11 + y * rhs.y * covariant_components.g_22 + z * rhs.z * covariant_components.g_33; result += (x * rhs.y + y * rhs.x) * covariant_components.g_12 diff --git a/src/field/vector3d.cxx b/src/field/vector3d.cxx index 7db305e4bc..f8374aa21b 100644 --- a/src/field/vector3d.cxx +++ b/src/field/vector3d.cxx @@ -85,9 +85,9 @@ void Vector3D::toCovariant() { const auto x_at_z = interp_to(x, z.getLocation()); const auto y_at_z = interp_to(y, z.getLocation()); - Coordinates::CovariantMetricTensor g_x = metric_x->getCovariantMetricTensor(); - Coordinates::CovariantMetricTensor g_y = metric_y->getCovariantMetricTensor(); - Coordinates::CovariantMetricTensor g_z = metric_z->getCovariantMetricTensor(); + const auto g_x = metric_x->getCovariantMetricTensor(); + const auto g_y = metric_y->getCovariantMetricTensor(); + const auto g_z = metric_z->getCovariantMetricTensor(); // multiply by g_{ij} BOUT_FOR(i, localmesh->getRegion3D("RGN_ALL")) { @@ -104,7 +104,7 @@ void Vector3D::toCovariant() { // Need to use temporary arrays to store result Field3D gx{emptyFrom(x)}, gy{emptyFrom(y)}, gz{emptyFrom(z)}; - Coordinates::CovariantMetricTensor covariant_components = metric->getCovariantMetricTensor(); + const auto covariant_components = metric->getCovariantMetricTensor(); BOUT_FOR(i, localmesh->getRegion3D("RGN_ALL")) { gx[i] = covariant_components.g_11[i] * x[i] + covariant_components.g_12[i] * y[i] + covariant_components.g_13[i] * z[i]; @@ -145,9 +145,9 @@ void Vector3D::toContravariant() { const auto x_at_z = interp_to(x, z.getLocation()); const auto y_at_z = interp_to(y, z.getLocation()); - Coordinates::ContravariantMetricTensor g_x = metric_x->getContravariantMetricTensor(); - Coordinates::ContravariantMetricTensor g_y = metric_y->getContravariantMetricTensor(); - Coordinates::ContravariantMetricTensor g_z = metric_z->getContravariantMetricTensor(); + const auto g_x = metric_x->getContravariantMetricTensor(); + const auto g_y = metric_y->getContravariantMetricTensor(); + const auto g_z = metric_z->getContravariantMetricTensor(); // multiply by g_{ij} BOUT_FOR(i, localmesh->getRegion3D("RGN_ALL")) { @@ -162,7 +162,7 @@ void Vector3D::toContravariant() { // Need to use temporary arrays to store result Field3D gx{emptyFrom(x)}, gy{emptyFrom(y)}, gz{emptyFrom(z)}; - Coordinates::ContravariantMetricTensor g = metric->getContravariantMetricTensor(); + const auto g = metric->getContravariantMetricTensor(); BOUT_FOR(i, localmesh->getRegion3D("RGN_ALL")) { gx[i] = g.g11[i] * x[i] + g.g12[i] * y[i] + g.g13[i] * z[i]; @@ -491,7 +491,7 @@ const Field3D Vector3D::operator*(const Vector3D& rhs) const { if (covariant) { // Both covariant - Coordinates::ContravariantMetricTensor g = metric->getContravariantMetricTensor(); + const auto g = metric->getContravariantMetricTensor(); result = x * rhs.x * g.g11 + y * rhs.y * g.g22 + z * rhs.z * g.g33; result += (x * rhs.y + y * rhs.x) * g.g12 @@ -499,7 +499,7 @@ const Field3D Vector3D::operator*(const Vector3D& rhs) const { + (y * rhs.z + z * rhs.y) * g.g23; } else { // Both contravariant - Coordinates::CovariantMetricTensor covariant_components = metric->getCovariantMetricTensor(); + const auto covariant_components = metric->getCovariantMetricTensor(); result = x * rhs.x * covariant_components.g_11 + y * rhs.y * covariant_components.g_22 + z * rhs.z * covariant_components.g_33; result += (x * rhs.y + y * rhs.x) * covariant_components.g_12 @@ -525,7 +525,7 @@ const Field3D Vector3D::operator*(const Vector2D& rhs) const { Coordinates* metric = x.getCoordinates(location); if (covariant) { // Both covariant - Coordinates::ContravariantMetricTensor g = metric->getContravariantMetricTensor(); + const auto g = metric->getContravariantMetricTensor(); result = x * rhs.x * g.g11 + y * rhs.y * g.g22 + z * rhs.z * g.g33; result += (x * rhs.y + y * rhs.x) * g.g12 @@ -533,7 +533,7 @@ const Field3D Vector3D::operator*(const Vector2D& rhs) const { + (y * rhs.z + z * rhs.y) * g.g23; } else { // Both contravariant - Coordinates::CovariantMetricTensor covariant_components = metric->getCovariantMetricTensor(); + const auto covariant_components = metric->getCovariantMetricTensor(); result = x * rhs.x * covariant_components.g_11 + y * rhs.y * covariant_components.g_22 + z * rhs.z * covariant_components.g_33; result += (x * rhs.y + y * rhs.x) * covariant_components.g_12 diff --git a/src/invert/laplace/impls/multigrid/multigrid_laplace.cxx b/src/invert/laplace/impls/multigrid/multigrid_laplace.cxx index 838cad4c11..c34a0002e0 100644 --- a/src/invert/laplace/impls/multigrid/multigrid_laplace.cxx +++ b/src/invert/laplace/impls/multigrid/multigrid_laplace.cxx @@ -280,7 +280,7 @@ FieldPerp LaplaceMultigrid::solve(const FieldPerp& b_in, const FieldPerp& x0) { // Neumann boundary condition if (inner_boundary_flags & INVERT_SET) { // guard cells of x0 specify gradient to set at inner boundary - Coordinates::CovariantMetricTensor covariant_components = coords->getCovariantMetricTensor(); + const auto covariant_components = coords->getCovariantMetricTensor(); BOUT_OMP_PERF(parallel default(shared)) BOUT_OMP_PERF(for) for (int k = 1; k < lzz + 1; k++) { @@ -325,7 +325,7 @@ FieldPerp LaplaceMultigrid::solve(const FieldPerp& b_in, const FieldPerp& x0) { // Neumann boundary condition if (inner_boundary_flags & INVERT_SET) { // guard cells of x0 specify gradient to set at outer boundary - Coordinates::CovariantMetricTensor covariant_components = coords->getCovariantMetricTensor(); + const auto covariant_components = coords->getCovariantMetricTensor(); BOUT_OMP_PERF(parallel default(shared)) BOUT_OMP_PERF(for) for (int k = 1; k < lzz + 1; k++) { @@ -483,7 +483,7 @@ FieldPerp LaplaceMultigrid::solve(const FieldPerp& b_in, const FieldPerp& x0) { // Neumann boundary condition if (inner_boundary_flags & INVERT_SET) { // guard cells of x0 specify gradient to set at inner boundary - Coordinates::CovariantMetricTensor covariant_components = coords->getCovariantMetricTensor(); + const auto covariant_components = coords->getCovariantMetricTensor(); int i2 = -1 + localmesh->xstart; BOUT_OMP_PERF(parallel default(shared)) BOUT_OMP_PERF(for) @@ -532,7 +532,7 @@ FieldPerp LaplaceMultigrid::solve(const FieldPerp& b_in, const FieldPerp& x0) { // Neumann boundary condition if (inner_boundary_flags & INVERT_SET) { // guard cells of x0 specify gradient to set at outer boundary - Coordinates::CovariantMetricTensor covariant_components = coords->getCovariantMetricTensor(); + const auto covariant_components = coords->getCovariantMetricTensor(); int i2 = lxx + localmesh->xstart; BOUT_OMP_PERF(parallel default(shared)) BOUT_OMP_PERF(for) @@ -601,7 +601,7 @@ void LaplaceMultigrid::generateMatrixF(int level) { int k2p = (k2 + 1) % Nz_global; int k2m = (k2 + Nz_global - 1) % Nz_global; - Coordinates::ContravariantMetricTensor contravariant_components = coords->getContravariantMetricTensor(); + const auto contravariant_components = coords->getContravariantMetricTensor(); BoutReal dz = coords->dz(i2, yindex); BoutReal ddx_C = (C2(i2 + 1, yindex, k2) - C2(i2 - 1, yindex, k2)) / 2. diff --git a/src/invert/laplace/impls/naulin/naulin_laplace.cxx b/src/invert/laplace/impls/naulin/naulin_laplace.cxx index 7b7770ab11..dcc7093974 100644 --- a/src/invert/laplace/impls/naulin/naulin_laplace.cxx +++ b/src/invert/laplace/impls/naulin/naulin_laplace.cxx @@ -248,7 +248,7 @@ Field3D LaplaceNaulin::solve(const Field3D& rhs, const Field3D& x0) { // Derivatives of x Field3D ddx_x = DDX(x_in, location, "C2"); Field3D ddz_x = DDZ(x_in, location, "FFT"); - Coordinates::ContravariantMetricTensor g = coords->getContravariantMetricTensor(); + const auto g = coords->getContravariantMetricTensor(); return rhsOverD - (g.g11 * coef_x_AC * ddx_x + g.g33 * coef_z * ddz_x + g.g13 * (coef_x_AC * ddz_x + coef_z * ddx_x)) diff --git a/src/invert/laplace/impls/serial_band/serial_band.cxx b/src/invert/laplace/impls/serial_band/serial_band.cxx index a0e805e927..02412553f8 100644 --- a/src/invert/laplace/impls/serial_band/serial_band.cxx +++ b/src/invert/laplace/impls/serial_band/serial_band.cxx @@ -158,7 +158,7 @@ FieldPerp LaplaceSerialBand::solve(const FieldPerp& b, const FieldPerp& x0) { A(ix, 4) = 0.; #else // Set coefficients - Coordinates::ContravariantMetricTensor g = coords->getContravariantMetricTensor(); + const auto g = coords->getContravariantMetricTensor(); coef1 = g.g11(ix, jy); // X 2nd derivative coef2 = g.g33(ix, jy); // Z 2nd derivative coef3 = g.g13(ix, jy); // X-Z mixed derivatives @@ -215,7 +215,7 @@ FieldPerp LaplaceSerialBand::solve(const FieldPerp& b, const FieldPerp& x0) { int ix = 1; auto kwave = kwave_(ix, jy); - Coordinates::ContravariantMetricTensor g = coords->getContravariantMetricTensor(); + const auto g = coords->getContravariantMetricTensor(); coef1 = g.g11(ix, jy) / (SQ(coords->dx(ix, jy))); coef2 = g.g33(ix, jy); coef3 = kwave * g.g13(ix, jy) / (2. * coords->dx(ix, jy)); @@ -266,7 +266,7 @@ FieldPerp LaplaceSerialBand::solve(const FieldPerp& b, const FieldPerp& x0) { if (iz == 0) { // DC - Coordinates::CovariantMetricTensor covariant_components = coords->getCovariantMetricTensor(); + const auto covariant_components = coords->getCovariantMetricTensor(); // Inner boundary if (inner_boundary_flags & (INVERT_DC_GRAD + INVERT_SET) @@ -341,7 +341,7 @@ FieldPerp LaplaceSerialBand::solve(const FieldPerp& b, const FieldPerp& x0) { int ix = 1; auto kwave = kwave_(ix, jy); - Coordinates::ContravariantMetricTensor g = coords->getContravariantMetricTensor(); + const auto g = coords->getContravariantMetricTensor(); coef1 = g.g11(ix, jy) / (12. * SQ(coords->dx(ix, jy))); coef2 = g.g33(ix, jy); @@ -386,7 +386,7 @@ FieldPerp LaplaceSerialBand::solve(const FieldPerp& b, const FieldPerp& x0) { int ix = ncx - 1; - Coordinates::ContravariantMetricTensor g = coords->getContravariantMetricTensor(); + const auto g = coords->getContravariantMetricTensor(); coef1 = g.g11(ix, jy) / (12. * SQ(coords->dx(ix, jy))); coef2 = g.g33(ix, jy); diff --git a/src/invert/laplace/invert_laplace.cxx b/src/invert/laplace/invert_laplace.cxx index 9c61e4bf4a..0651f9e395 100644 --- a/src/invert/laplace/invert_laplace.cxx +++ b/src/invert/laplace/invert_laplace.cxx @@ -324,7 +324,7 @@ void Laplacian::tridagCoefs(int jx, int jy, BoutReal kwave, dcomplex& a, dcomple BoutReal coef1, coef2, coef3, coef4, coef5; - Coordinates::ContravariantMetricTensor contravariant_components = localcoords->getContravariantMetricTensor(); + const auto contravariant_components = localcoords->getContravariantMetricTensor(); coef1 = contravariant_components.g11(jx, jy); ///< X 2nd derivative coefficient coef2 = contravariant_components.g33(jx, jy); ///< Z 2nd derivative coefficient @@ -494,8 +494,8 @@ void Laplacian::tridagMatrix(dcomplex* avec, dcomplex* bvec, dcomplex* cvec, dco // Set the boundary conditions if x is not periodic if (!localmesh->periodicX) { - Coordinates::ContravariantMetricTensor contravariant_components = coords->getContravariantMetricTensor(); - Coordinates::CovariantMetricTensor covariant_components = coords->getCovariantMetricTensor(); + const auto contravariant_components = coords->getContravariantMetricTensor(); + const auto covariant_components = coords->getCovariantMetricTensor(); if (localmesh->firstX()) { // INNER BOUNDARY ON THIS PROCESSOR diff --git a/src/invert/laplacexz/impls/cyclic/laplacexz-cyclic.cxx b/src/invert/laplacexz/impls/cyclic/laplacexz-cyclic.cxx index 3127bccdc8..228a7d3e58 100644 --- a/src/invert/laplacexz/impls/cyclic/laplacexz-cyclic.cxx +++ b/src/invert/laplacexz/impls/cyclic/laplacexz-cyclic.cxx @@ -72,7 +72,7 @@ void LaplaceXZcyclic::setCoefs(const Field2D& A2D, const Field2D& B2D) { // Set coefficients Coordinates* coord = localmesh->getCoordinates(location); - Coordinates::ContravariantMetricTensor g = coord->getContravariantMetricTensor(); + const auto g = coord->getContravariantMetricTensor(); // NOTE: For now the X-Z terms are omitted, so check that they are small ASSERT2(max(abs(g.g13)) < 1e-5); diff --git a/src/mesh/boundary_standard.cxx b/src/mesh/boundary_standard.cxx index c270455e10..3df5d30f6b 100644 --- a/src/mesh/boundary_standard.cxx +++ b/src/mesh/boundary_standard.cxx @@ -1602,7 +1602,7 @@ void BoundaryNeumann_NonOrthogonal::apply(Field2D& f) { // Calculate derivatives for metric use mesh->communicate(f); Field2D dfdy = DDY(f); - Coordinates::ContravariantMetricTensor contravariant_components = metric->getContravariantMetricTensor(); + const auto contravariant_components = metric->getContravariantMetricTensor(); // Loop over all elements and set equal to the next point in for (bndry->first(); !bndry->isDone(); bndry->next1d()) { // Interpolate (linearly) metrics to halfway between last cell and boundary cell @@ -1660,7 +1660,7 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { mesh->communicate(f); Field3D dfdy = DDY(f); Field3D dfdz = DDZ(f); - Coordinates::ContravariantMetricTensor g = metric->getContravariantMetricTensor(); + const auto g = metric->getContravariantMetricTensor(); // Loop over all elements and set equal to the next point in for (bndry->first(); !bndry->isDone(); bndry->next1d()) { #if BOUT_USE_METRIC_3D @@ -2451,7 +2451,7 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { void BoundaryNeumannPar::apply(Field2D & f) { #if not(BOUT_USE_METRIC_3D) Coordinates* metric = f.getCoordinates(); - Coordinates::CovariantMetricTensor covariant_components = metric->getCovariantMetricTensor(); + const auto covariant_components = metric->getCovariantMetricTensor(); // Loop over all elements and set equal to the next point in for (bndry->first(); !bndry->isDone(); bndry->next()) { f(bndry->x, bndry->y) = @@ -2469,7 +2469,7 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { Mesh* mesh = bndry->localmesh; ASSERT1(mesh == f.getMesh()); Coordinates* metric = f.getCoordinates(); - Coordinates::CovariantMetricTensor covariant_components = metric->getCovariantMetricTensor(); + const auto covariant_components = metric->getCovariantMetricTensor(); for (bndry->first(); !bndry->isDone(); bndry->next()) { for (int z = 0; z < mesh->LocalNz; z++) { f(bndry->x, bndry->y, z) = @@ -2649,7 +2649,7 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { } int bx = bndry->bx; - Coordinates::ContravariantMetricTensor g = metric->getContravariantMetricTensor(); + const auto g = metric->getContravariantMetricTensor(); // Loop over the Y dimension for (bndry->first(); !bndry->isDone(); bndry->nextY()) { // bndry->(x,y) is the first point in the boundary @@ -2856,7 +2856,7 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { Array c0(ncz / 2 + 1), c1(ncz / 2 + 1), c2(ncz / 2 + 1); int bx = bndry->bx; - Coordinates::ContravariantMetricTensor g = metric->getContravariantMetricTensor(); + const auto g = metric->getContravariantMetricTensor(); // Loop over the Y dimension for (bndry->first(); !bndry->isDone(); bndry->nextY()) { int x = bndry->x; @@ -2947,7 +2947,7 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { } jx = mesh->xend + 1; - Coordinates::ContravariantMetricTensor contravariant_components = metric->getContravariantMetricTensor(); + const auto contravariant_components = metric->getContravariantMetricTensor(); for (jy = 1; jy < mesh->LocalNy - 1; jy++) { for (jz = 0; jz < ncz; jz++) { jzp = (jz + 1) % ncz; diff --git a/src/mesh/difops.cxx b/src/mesh/difops.cxx index 6453ed395c..8399f7fc75 100644 --- a/src/mesh/difops.cxx +++ b/src/mesh/difops.cxx @@ -101,7 +101,7 @@ Field3D Grad_parP(const Field3D& apar, const Field3D& f) { } } - Coordinates::CovariantMetricTensor covariant_components = metric->getCovariantMetricTensor(); + const auto covariant_components = metric->getCovariantMetricTensor(); for (int x = 1; x <= mesh->LocalNx - 2; x++) { for (int y = mesh->ystart; y <= mesh->yend; y++) { for (int z = 0; z < ncz; z++) { @@ -248,7 +248,7 @@ Field3D Div_par(const Field3D& f, const Field3D& v) { Coordinates* coord = f.getCoordinates(); - Coordinates::CovariantMetricTensor covariant_components = coord->getCovariantMetricTensor(); + const auto covariant_components = coord->getCovariantMetricTensor(); for (int i = mesh->xstart; i <= mesh->xend; i++) { for (int j = mesh->ystart; j <= mesh->yend; j++) { @@ -479,7 +479,7 @@ Coordinates::FieldMetric b0xGrad_dot_Grad(const Field2D& phi, const Field2D& A, Coordinates::FieldMetric dpdx = DDX(phi, outloc); Coordinates::FieldMetric dpdy = DDY(phi, outloc); - Coordinates::CovariantMetricTensor covariant_components = metric->getCovariantMetricTensor(); + const auto covariant_components = metric->getCovariantMetricTensor(); // Calculate advection velocity Coordinates::FieldMetric vx = -covariant_components.g_23 * dpdy; @@ -514,7 +514,7 @@ Field3D b0xGrad_dot_Grad(const Field2D& phi, const Field3D& A, CELL_LOC outloc) Coordinates::FieldMetric dpdx = DDX(phi, outloc); Coordinates::FieldMetric dpdy = DDY(phi, outloc); - Coordinates::CovariantMetricTensor covariant_components = metric->getCovariantMetricTensor(); + const auto covariant_components = metric->getCovariantMetricTensor(); // Calculate advection velocity Coordinates::FieldMetric vx = -covariant_components.g_23 * dpdy; @@ -557,7 +557,7 @@ Field3D b0xGrad_dot_Grad(const Field3D& p, const Field2D& A, CELL_LOC outloc) { Field3D dpdy = DDY(p, outloc); Field3D dpdz = DDZ(p, outloc); - Coordinates::CovariantMetricTensor covariant_components = metric->getCovariantMetricTensor(); + const auto covariant_components = metric->getCovariantMetricTensor(); // Calculate advection velocity Field3D vx = covariant_components.g_22 * dpdz - covariant_components.g_23 * dpdy; @@ -596,7 +596,7 @@ Field3D b0xGrad_dot_Grad(const Field3D& phi, const Field3D& A, CELL_LOC outloc) Field3D dpdy = DDY(phi, outloc); Field3D dpdz = DDZ(phi, outloc); - Coordinates::CovariantMetricTensor covariant_components = metric->getCovariantMetricTensor(); + const auto covariant_components = metric->getCovariantMetricTensor(); // Calculate advection velocity Field3D vx = covariant_components.g_22 * dpdz - covariant_components.g_23 * dpdy; diff --git a/src/mesh/fv_ops.cxx b/src/mesh/fv_ops.cxx index bd6cc9b654..d55a461397 100644 --- a/src/mesh/fv_ops.cxx +++ b/src/mesh/fv_ops.cxx @@ -31,7 +31,7 @@ Field3D Div_a_Grad_perp(const Field3D& a, const Field3D& f) { Field3D result{zeroFrom(f)}; Coordinates* coord = f.getCoordinates(); - Coordinates::ContravariantMetricTensor g = coord->getContravariantMetricTensor(); + const auto g = coord->getContravariantMetricTensor(); // Flux in x @@ -70,7 +70,7 @@ Field3D Div_a_Grad_perp(const Field3D& a, const Field3D& f) { // 3D Metric, need yup/ydown fields. // Requires previous communication of metrics // -- should insert communication here? - Coordinates::CovariantMetricTensor covariant_components = coord->getCovariantMetricTensor(); + const auto covariant_components = coord->getCovariantMetricTensor(); if (!g.g23.hasParallelSlices() || !covariant_components.g_23.hasParallelSlices() || !coord->dy.hasParallelSlices() || !coord->dz.hasParallelSlices() || !coord->Bxy.hasParallelSlices() || !coord->J.hasParallelSlices()) { @@ -211,7 +211,7 @@ const Field3D Div_par_K_Grad_par(const Field3D& Kin, const Field3D& fin, const auto iyp = i.yp(); const auto iym = i.ym(); - Coordinates::CovariantMetricTensor covariant_components = coord->getCovariantMetricTensor(); + const auto covariant_components = coord->getCovariantMetricTensor(); if (bndry_flux || mesh->periodicY(i.x()) || !mesh->lastY(i.x()) || (i.y() != mesh->yend)) { @@ -503,7 +503,7 @@ Field3D Div_Perp_Lap(const Field3D& a, const Field3D& f, CELL_LOC outloc) { // Coordinates* coords = a.getCoordinates(outloc); Mesh* mesh = f.getMesh(); - Coordinates::ContravariantMetricTensor g = coords->getContravariantMetricTensor(); + const auto g = coords->getContravariantMetricTensor(); for (int i = mesh->xstart; i <= mesh->xend; i++) { for (int j = mesh->ystart; j <= mesh->yend; j++) { diff --git a/tests/MMS/diffusion/diffusion.cxx b/tests/MMS/diffusion/diffusion.cxx index 8ae1f2a861..50b611ff10 100644 --- a/tests/MMS/diffusion/diffusion.cxx +++ b/tests/MMS/diffusion/diffusion.cxx @@ -52,7 +52,7 @@ int Diffusion::init(bool UNUSED(restarting)) { contravariant_components.g23 = 0.0; coord->setContravariantMetricTensor(contravariant_components); - Coordinates::CovariantMetricTensor covariant_components; + Coordinates:: covariant_components; covariant_components.g_11 = 1.0; covariant_components.g_22 = 1.0; covariant_components.g_33 = 1.0; diff --git a/tests/MMS/wave-1d/wave.cxx b/tests/MMS/wave-1d/wave.cxx index 6dfc56b63f..b1df3a3d8c 100644 --- a/tests/MMS/wave-1d/wave.cxx +++ b/tests/MMS/wave-1d/wave.cxx @@ -32,7 +32,7 @@ class Wave1D : public PhysicsModel { SAVE_ONCE(Lx, Ly); //set mesh - Coordinates::ContravariantMetricTensor contravariant_components = coord->getContravariantMetricTensor(); + const auto contravariant_components = coord->getContravariantMetricTensor(); contravariant_components.g11 = 1.0; contravariant_components.g22 = 1.0; contravariant_components.g33 = 1.0; @@ -41,7 +41,7 @@ class Wave1D : public PhysicsModel { contravariant_components.g23 = 0.0; coord->setContravariantMetricTensor(contravariant_components); - Coordinates::CovariantMetricTensor covariant_components; + const auto covariant_components; covariant_components.g_11 = 1.0; covariant_components.g_22 = 1.0; covariant_components.g_33 = 1.0; diff --git a/tests/integrated/test-drift-instability/2fluid.cxx b/tests/integrated/test-drift-instability/2fluid.cxx index da9e39882e..4a1efd873e 100644 --- a/tests/integrated/test-drift-instability/2fluid.cxx +++ b/tests/integrated/test-drift-instability/2fluid.cxx @@ -217,7 +217,7 @@ class TwoFluid : public PhysicsModel { /**************** CALCULATE METRICS ******************/ - Coordinates::ContravariantMetricTensor contravariant_components; + const auto contravariant_components; contravariant_components.g11 = SQ(Rxy * Bpxy); contravariant_components.g22 = 1.0 / SQ(hthe); contravariant_components.g33 = SQ(I) * contravariant_components.g11 + SQ(coord->Bxy) / contravariant_components.g11; @@ -228,7 +228,7 @@ class TwoFluid : public PhysicsModel { coord->J = hthe / Bpxy; - Coordinates::CovariantMetricTensor covariant_components; + const auto covariant_components; covariant_components.g_11 = 1.0 / contravariant_components.g11 + SQ(I * Rxy); covariant_components.g_22 = SQ(coord->Bxy * hthe / Bpxy); covariant_components.g_33 = Rxy * Rxy; diff --git a/tests/integrated/test-interchange-instability/2fluid.cxx b/tests/integrated/test-interchange-instability/2fluid.cxx index e070b34432..b7d21d6d20 100644 --- a/tests/integrated/test-interchange-instability/2fluid.cxx +++ b/tests/integrated/test-interchange-instability/2fluid.cxx @@ -139,7 +139,7 @@ class Interchange : public PhysicsModel { /**************** CALCULATE METRICS ******************/ - Coordinates::ContravariantMetricTensor contravariant_components; + const auto contravariant_components; contravariant_components.g11 = SQ(Rxy * Bpxy); contravariant_components.g22 = 1.0 / SQ(hthe); contravariant_components.g33 = SQ(I) * contravariant_components.g11 + SQ(coord->Bxy) / contravariant_components.g11; @@ -150,7 +150,7 @@ class Interchange : public PhysicsModel { coord->J = hthe / Bpxy; - Coordinates::CovariantMetricTensor covariant_components; + const auto covariant_components; covariant_components.g_11 = 1.0 / contravariant_components.g11 + SQ(I * Rxy); covariant_components.g_22 = SQ(coord->Bxy * hthe / Bpxy); covariant_components.g_33 = Rxy * Rxy; diff --git a/tests/integrated/test-naulin-laplace/test_naulin_laplace.cxx b/tests/integrated/test-naulin-laplace/test_naulin_laplace.cxx index ff737200f9..30d001a9e4 100644 --- a/tests/integrated/test-naulin-laplace/test_naulin_laplace.cxx +++ b/tests/integrated/test-naulin-laplace/test_naulin_laplace.cxx @@ -304,7 +304,7 @@ int main(int argc, char** argv) { Field3D this_Grad_perp_dot_Grad_perp(const Field3D& f, const Field3D& g) { const auto* coords = f.getCoordinates(); - Coordinates::ContravariantMetricTensor metric_tensor = coords->getContravariantMetricTensor(); + const auto metric_tensor = coords->getContravariantMetricTensor(); Field3D result = metric_tensor.g11 * ::DDX(f) * ::DDX(g) + metric_tensor.g33 * ::DDZ(f) * ::DDZ(g) + metric_tensor.g13 * (DDX(f) * DDZ(g) + DDZ(f) * DDX(g)); diff --git a/tests/unit/invert/laplace/test_laplace_cyclic.cxx b/tests/unit/invert/laplace/test_laplace_cyclic.cxx index c22bb8587f..0a40630a34 100644 --- a/tests/unit/invert/laplace/test_laplace_cyclic.cxx +++ b/tests/unit/invert/laplace/test_laplace_cyclic.cxx @@ -39,7 +39,7 @@ class CyclicForwardOperator { } const Field3D operator()(Field3D& f) { - Coordinates::ContravariantMetricTensor contravariant_components = coords->getContravariantMetricTensor(); + const auto contravariant_components = coords->getContravariantMetricTensor(); auto result = d * Delp2(f) + (contravariant_components.g11 * DDX(f) + contravariant_components.g13 * DDZ(f)) * DDX(c2) / c1 + a * f + ex * DDX(f) + ez * DDZ(f); @@ -52,7 +52,7 @@ class CyclicForwardOperator { bool inner_x_neumann, outer_x_neumann; // If false then use Dirichlet conditions void applyBoundaries(Field3D& newF, const Field3D& f) { - Coordinates::CovariantMetricTensor covariant_components = coords->getCovariantMetricTensor(); + const auto covariant_components = coords->getCovariantMetricTensor(); BOUT_FOR(i, f.getMesh()->getRegion3D("RGN_INNER_X")) { if (inner_x_neumann) { newF[i] = (f[i.xp()] - f[i]) / coords->dx[i] / sqrt(covariant_components.g_11[i]); diff --git a/tests/unit/mesh/data/test_gridfromoptions.cxx b/tests/unit/mesh/data/test_gridfromoptions.cxx index 0738d47525..e5e4b63cd4 100644 --- a/tests/unit/mesh/data/test_gridfromoptions.cxx +++ b/tests/unit/mesh/data/test_gridfromoptions.cxx @@ -359,7 +359,7 @@ TEST_F(GridFromOptionsTest, CoordinatesCentre) { mesh_from_options.communicate(expected_2d); - Coordinates::ContravariantMetricTensor g = coords->getContravariantMetricTensor(); + const auto g = coords->getContravariantMetricTensor(); EXPECT_TRUE(IsFieldEqual(g.g11, expected_metric + 5.)); EXPECT_TRUE(IsFieldEqual(g.g22, expected_metric + 4.)); @@ -375,7 +375,7 @@ TEST_F(GridFromOptionsTest, CoordinatesZlow) { mesh_from_options.communicate(expected_2d); - Coordinates::ContravariantMetricTensor g = coords->getContravariantMetricTensor(); + const auto g = coords->getContravariantMetricTensor(); EXPECT_TRUE(IsFieldEqual(g.g11, expected_metric + 5.)); EXPECT_TRUE(IsFieldEqual(g.g22, expected_metric + 4.)); @@ -405,7 +405,7 @@ TEST_F(GridFromOptionsTest, CoordinatesXlowInterp) { mesh_from_options.communicate(expected_xlow); - Coordinates::ContravariantMetricTensor g = coords->getContravariantMetricTensor(); + const auto g = coords->getContravariantMetricTensor(); EXPECT_TRUE( IsFieldEqual(g.g11, expected_xlow + 5., "RGN_NOBNDRY", this_tolerance)); @@ -449,7 +449,7 @@ TEST_F(GridFromOptionsTest, CoordinatesXlowRead) { mesh_from_options.communicate(expected_xlow); - Coordinates::ContravariantMetricTensor g = coords->getContravariantMetricTensor(); + const auto g = coords->getContravariantMetricTensor(); EXPECT_TRUE(IsFieldEqual(g.g11, expected_xlow + 5.)); EXPECT_TRUE(g.g11.getLocation() == CELL_XLOW); @@ -483,7 +483,7 @@ TEST_F(GridFromOptionsTest, CoordinatesYlowInterp) { mesh_from_options.communicate(expected_ylow); - Coordinates::ContravariantMetricTensor g = coords->getContravariantMetricTensor(); + const auto g = coords->getContravariantMetricTensor(); EXPECT_TRUE( IsFieldEqual(g.g11, expected_ylow + 5., "RGN_NOBNDRY", this_tolerance)); @@ -536,7 +536,7 @@ TEST_F(GridFromOptionsTest, CoordinatesYlowRead) { mesh_from_options.communicate(expected_ylow); - Coordinates::ContravariantMetricTensor g = coords->getContravariantMetricTensor(); + const auto g = coords->getContravariantMetricTensor(); EXPECT_TRUE(IsFieldEqual(g.g11, expected_ylow + 5., "RGN_ALL", this_tolerance)); EXPECT_TRUE(g.g11.getLocation() == CELL_YLOW); @@ -560,7 +560,7 @@ TEST_F(GridFromOptionsTest, CoordinatesZlowRead) { auto coords = mesh_from_options.getCoordinates(CELL_ZLOW); - Coordinates::ContravariantMetricTensor g = coords->getContravariantMetricTensor(); + const auto g = coords->getContravariantMetricTensor(); EXPECT_TRUE(IsFieldEqual(g.g11, expected_2d + 5.)); EXPECT_TRUE(IsFieldEqual(g.g22, expected_2d + 4.)); diff --git a/tests/unit/mesh/test_coordinates.cxx b/tests/unit/mesh/test_coordinates.cxx index 8c4594de0f..bcc78fde47 100644 --- a/tests/unit/mesh/test_coordinates.cxx +++ b/tests/unit/mesh/test_coordinates.cxx @@ -370,7 +370,7 @@ TEST_F(CoordinatesTest, SetContravariantMetricTensor) { FieldMetric{0.0}}; // IntShiftTorsion // Modify with setter - Coordinates::ContravariantMetricTensor updated_metric_tensor; + const auto updated_metric_tensor; updated_metric_tensor.g11 = 1.7; updated_metric_tensor.g22 = 2.3; updated_metric_tensor.g33 = 3.1; @@ -446,7 +446,7 @@ TEST_F(CoordinatesTest, SetCovariantMetricTensor) { FieldMetric{0.0}}; // IntShiftTorsion // Modify with setter - Coordinates::CovariantMetricTensor updated_metric_tensor; + const auto updated_metric_tensor; updated_metric_tensor.g_11 = 1.7; updated_metric_tensor.g_22 = 2.3; updated_metric_tensor.g_33 = 3.1; From b347c054d35f8c3f7fa61497489377833af34c9d Mon Sep 17 00:00:00 2001 From: tomc271 Date: Mon, 25 Sep 2023 13:04:01 +0100 Subject: [PATCH 045/491] Move new classes to separate files. --- CMakeLists.txt | 4 +- include/bout/ContravariantMetricTensor.cpp | 134 +++++++++++++++++++++ include/bout/ContravariantMetricTensor.h | 41 +++++++ include/bout/coordinates.hxx | 8 +- src/mesh/coordinates.cxx | 133 ++------------------ src/mesh/coordinates_accessor.cxx | 5 +- 6 files changed, 193 insertions(+), 132 deletions(-) create mode 100644 include/bout/ContravariantMetricTensor.cpp create mode 100644 include/bout/ContravariantMetricTensor.h diff --git a/CMakeLists.txt b/CMakeLists.txt index c1c82ea4e3..97d20cf66e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -354,7 +354,9 @@ set(BOUT_SOURCES ./src/sys/utils.cxx ${CMAKE_CURRENT_BINARY_DIR}/include/bout/revision.hxx ${CMAKE_CURRENT_BINARY_DIR}/include/bout/version.hxx - ) + include/bout/ContravariantMetricTensor.cpp + include/bout/ContravariantMetricTensor.h +) find_package(Python3) diff --git a/include/bout/ContravariantMetricTensor.cpp b/include/bout/ContravariantMetricTensor.cpp new file mode 100644 index 0000000000..e8a4fff217 --- /dev/null +++ b/include/bout/ContravariantMetricTensor.cpp @@ -0,0 +1,134 @@ + +#include "ContravariantMetricTensor.h" + + +ContravariantMetricTensor ContravariantMetricTensor::getContravariantMetricTensor() const { + ContravariantMetricTensor g_contravariant = { g11, g22, g33, g12, g13, g23 }; + return g_contravariant; +} + +void ContravariantMetricTensor::setContravariantMetricTensor( + const ContravariantMetricTensor& metric_tensor) { + g11 = metric_tensor.g11; + g22 = metric_tensor.g22; + g33 = metric_tensor.g33; + g12 = metric_tensor.g12; + g13 = metric_tensor.g13; + g23 = metric_tensor.g23; + calcCovariant(); +} + +int ContravariantMetricTensor::calcCovariant(const std::string& region) { + TRACE("Coordinates::calcCovariant"); + + // Make sure metric elements are allocated + g_11.allocate(); + g_22.allocate(); + g_33.allocate(); + g_12.allocate(); + g_13.allocate(); + g_23.allocate(); + + g_11.setLocation(location); + g_22.setLocation(location); + g_33.setLocation(location); + g_12.setLocation(location); + g_13.setLocation(location); + g_23.setLocation(location); + + // Perform inversion of g^{ij} to get g_{ij} + // NOTE: Currently this bit assumes that metric terms are Field2D objects + + auto a = Matrix(3, 3); + + BOUT_FOR_SERIAL(i, g11.getRegion(region)) { + a(0, 0) = g11[i]; + a(1, 1) = g22[i]; + a(2, 2) = g33[i]; + + a(0, 1) = a(1, 0) = g12[i]; + a(1, 2) = a(2, 1) = g23[i]; + a(0, 2) = a(2, 0) = g13[i]; + + if (invert3x3(a)) { + output_error.write("\tERROR: metric tensor is singular at ({:d}, {:d})\n", i.x(), + i.y()); + return 1; + } + + g_11[i] = a(0, 0); + g_22[i] = a(1, 1); + g_33[i] = a(2, 2); + + g_12[i] = a(0, 1); + g_13[i] = a(0, 2); + g_23[i] = a(1, 2); + } + + BoutReal maxerr; + maxerr = BOUTMAX(max(abs((g_11 * g11 + g_12 * g12 + g_13 * g13) - 1)), + max(abs((g_12 * g12 + g_22 * g22 + g_23 * g23) - 1)), + max(abs((g_13 * g13 + g_23 * g23 + g_33 * g33) - 1))); + + output_info.write("\tLocal maximum error in diagonal inversion is {:e}\n", maxerr); + + maxerr = BOUTMAX(max(abs(g_11 * g12 + g_12 * g22 + g_13 * g23)), + max(abs(g_11 * g13 + g_12 * g23 + g_13 * g33)), + max(abs(g_12 * g13 + g_22 * g23 + g_23 * g33))); + + output_info.write("\tLocal maximum error in off-diagonal inversion is {:e}\n", maxerr); + + return 0; +} + +void ContravariantMetricTensor::checkContravariant() { + // Diagonal metric components should be finite + bout::checkFinite(g11, "g11", "RGN_NOCORNERS"); + bout::checkFinite(g22, "g22", "RGN_NOCORNERS"); + bout::checkFinite(g33, "g33", "RGN_NOCORNERS"); + if (g11.hasParallelSlices() && &g11.ynext(1) != &g11) { + for (int dy = 1; dy <= localmesh->ystart; ++dy) { + for (const auto sign : {1, -1}) { + bout::checkFinite(g11.ynext(sign * dy), "g11.ynext", + fmt::format("RGN_YPAR_{:+d}", sign * dy)); + bout::checkFinite(g22.ynext(sign * dy), "g22.ynext", + fmt::format("RGN_YPAR_{:+d}", sign * dy)); + bout::checkFinite(g33.ynext(sign * dy), "g33.ynext", + fmt::format("RGN_YPAR_{:+d}", sign * dy)); + } + } + } + // Diagonal metric components should be positive + bout::checkPositive(g11, "g11", "RGN_NOCORNERS"); + bout::checkPositive(g22, "g22", "RGN_NOCORNERS"); + bout::checkPositive(g33, "g33", "RGN_NOCORNERS"); + if (g11.hasParallelSlices() && &g11.ynext(1) != &g11) { + for (int dy = 1; dy <= localmesh->ystart; ++dy) { + for (const auto sign : {1, -1}) { + bout::checkPositive(g11.ynext(sign * dy), "g11.ynext", + fmt::format("RGN_YPAR_{:+d}", sign * dy)); + bout::checkPositive(g22.ynext(sign * dy), "g22.ynext", + fmt::format("RGN_YPAR_{:+d}", sign * dy)); + bout::checkPositive(g33.ynext(sign * dy), "g33.ynext", + fmt::format("RGN_YPAR_{:+d}", sign * dy)); + } + } + } + + // Off-diagonal metric components should be finite + bout::checkFinite(g12, "g12", "RGN_NOCORNERS"); + bout::checkFinite(g13, "g13", "RGN_NOCORNERS"); + bout::checkFinite(g23, "g23", "RGN_NOCORNERS"); + if (g23.hasParallelSlices() && &g23.ynext(1) != &g23) { + for (int dy = 1; dy <= localmesh->ystart; ++dy) { + for (const auto sign : {1, -1}) { + bout::checkFinite(g12.ynext(sign * dy), "g12.ynext", + fmt::format("RGN_YPAR_{:+d}", sign * dy)); + bout::checkFinite(g13.ynext(sign * dy), "g13.ynext", + fmt::format("RGN_YPAR_{:+d}", sign * dy)); + bout::checkFinite(g23.ynext(sign * dy), "g23.ynext", + fmt::format("RGN_YPAR_{:+d}", sign * dy)); + } + } + } +} diff --git a/include/bout/ContravariantMetricTensor.h b/include/bout/ContravariantMetricTensor.h new file mode 100644 index 0000000000..54342260af --- /dev/null +++ b/include/bout/ContravariantMetricTensor.h @@ -0,0 +1,41 @@ + +#ifndef BOUT_CONTRAVARIANTMETRICTENSOR_H +#define BOUT_CONTRAVARIANTMETRICTENSOR_H + +#include "bout/field2d.hxx" +#include "bout/field3d.hxx" +#include "bout/paralleltransform.hxx" +#include "bout/utils.hxx" +#include + +class ContravariantMetricTensor { + +public: +#if BOUT_USE_METRIC_3D + using FieldMetric = Field +#else + using FieldMetric = Field2D; +#endif + + ContravariantMetricTensor(const FieldMetric g11, const FieldMetric g22, + const FieldMetric g33, const FieldMetric g12, + const FieldMetric g13, const FieldMetric g23) + : g11(g11), + g22(g22), g33(g33), g12(g12), g13(g13), g23(g23) { + } + + /// Invert contravariant metric to get covariant components + int calcCovariant(const std::string& region = "RGN_ALL"); + +private: + FieldMetric g11, g22, g33, g12, g13, g23; + + // check that contravariant tensors are positive (if expected) and finite (always) + void checkContravariant(); + +public: + void setContravariantMetricTensor(const ContravariantMetricTensor& metric_tensor); + ContravariantMetricTensor getContravariantMetricTensor() const; +}; + +#endif //BOUT_CONTRAVARIANTMETRICTENSOR_H diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index 73f3df7963..77bfbe3395 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -33,6 +33,7 @@ #ifndef BOUT_COORDINATES_H #define BOUT_COORDINATES_H +#include "ContravariantMetricTensor.h" #include "bout/field2d.hxx" #include "bout/field3d.hxx" #include "bout/paralleltransform.hxx" @@ -98,17 +99,12 @@ public: private: - /// Contravariant metric tensor (g^{ij}) - FieldMetric g11, g22, g33, g12, g13, g23; - + ContravariantMetricTensor contravariantMetricTensor; /// Covariant metric tensor FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; public: - struct ContravariantMetricTensor { - FieldMetric g11, g22, g33, g12, g13, g23; - }; struct CovariantMetricTensor { diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 5c76ca5750..2c550d91e1 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -18,6 +18,7 @@ #include +#include "bout/ContravariantMetricTensor.h" #include "parallel/fci.hxx" #include "parallel/shiftedmetricinterp.hxx" @@ -834,7 +835,7 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, dz = interpolateAndExtrapolate(coords_in->dz, location, true, true, false, transform.get()); - Coordinates::ContravariantMetricTensor metric_tensor = coords_in->getContravariantMetricTensor(); + ContravariantMetricTensor metric_tensor = coords_in->getContravariantMetricTensor(); // Diagonal components of metric tensor g^{ij} g11 = interpolateAndExtrapolate(metric_tensor.g11, location, true, true, false, @@ -1216,67 +1217,8 @@ void Coordinates::CalculateChristoffelSymbols() { int Coordinates::calcCovariant(const std::string& region) { TRACE("Coordinates::calcCovariant"); - - // Make sure metric elements are allocated - g_11.allocate(); - g_22.allocate(); - g_33.allocate(); - g_12.allocate(); - g_13.allocate(); - g_23.allocate(); - - g_11.setLocation(location); - g_22.setLocation(location); - g_33.setLocation(location); - g_12.setLocation(location); - g_13.setLocation(location); - g_23.setLocation(location); - - // Perform inversion of g^{ij} to get g_{ij} - // NOTE: Currently this bit assumes that metric terms are Field2D objects - - auto a = Matrix(3, 3); - - BOUT_FOR_SERIAL(i, g11.getRegion(region)) { - a(0, 0) = g11[i]; - a(1, 1) = g22[i]; - a(2, 2) = g33[i]; - - a(0, 1) = a(1, 0) = g12[i]; - a(1, 2) = a(2, 1) = g23[i]; - a(0, 2) = a(2, 0) = g13[i]; - - if (invert3x3(a)) { - output_error.write("\tERROR: metric tensor is singular at ({:d}, {:d})\n", i.x(), - i.y()); - return 1; - } - - g_11[i] = a(0, 0); - g_22[i] = a(1, 1); - g_33[i] = a(2, 2); - - g_12[i] = a(0, 1); - g_13[i] = a(0, 2); - g_23[i] = a(1, 2); - } - - BoutReal maxerr; - maxerr = BOUTMAX(max(abs((g_11 * g11 + g_12 * g12 + g_13 * g13) - 1)), - max(abs((g_12 * g12 + g_22 * g22 + g_23 * g23) - 1)), - max(abs((g_13 * g13 + g_23 * g23 + g_33 * g33) - 1))); - - output_info.write("\tLocal maximum error in diagonal inversion is {:e}\n", maxerr); - - maxerr = BOUTMAX(max(abs(g_11 * g12 + g_12 * g22 + g_13 * g23)), - max(abs(g_11 * g13 + g_12 * g23 + g_13 * g33)), - max(abs(g_12 * g13 + g_22 * g23 + g_23 * g33))); - - output_info.write("\tLocal maximum error in off-diagonal inversion is {:e}\n", maxerr); - - return 0; + return contravariantMetricTensor.calcCovariant(region); } - int Coordinates::calcContravariant(const std::string& region) { TRACE("Coordinates::calcContravariant"); @@ -1975,60 +1917,16 @@ void Coordinates::checkCovariant() { } void Coordinates::checkContravariant() { - // Diagonal metric components should be finite - bout::checkFinite(g11, "g11", "RGN_NOCORNERS"); - bout::checkFinite(g22, "g22", "RGN_NOCORNERS"); - bout::checkFinite(g33, "g33", "RGN_NOCORNERS"); - if (g11.hasParallelSlices() && &g11.ynext(1) != &g11) { - for (int dy = 1; dy <= localmesh->ystart; ++dy) { - for (const auto sign : {1, -1}) { - bout::checkFinite(g11.ynext(sign * dy), "g11.ynext", - fmt::format("RGN_YPAR_{:+d}", sign * dy)); - bout::checkFinite(g22.ynext(sign * dy), "g22.ynext", - fmt::format("RGN_YPAR_{:+d}", sign * dy)); - bout::checkFinite(g33.ynext(sign * dy), "g33.ynext", - fmt::format("RGN_YPAR_{:+d}", sign * dy)); - } - } - } - // Diagonal metric components should be positive - bout::checkPositive(g11, "g11", "RGN_NOCORNERS"); - bout::checkPositive(g22, "g22", "RGN_NOCORNERS"); - bout::checkPositive(g33, "g33", "RGN_NOCORNERS"); - if (g11.hasParallelSlices() && &g11.ynext(1) != &g11) { - for (int dy = 1; dy <= localmesh->ystart; ++dy) { - for (const auto sign : {1, -1}) { - bout::checkPositive(g11.ynext(sign * dy), "g11.ynext", - fmt::format("RGN_YPAR_{:+d}", sign * dy)); - bout::checkPositive(g22.ynext(sign * dy), "g22.ynext", - fmt::format("RGN_YPAR_{:+d}", sign * dy)); - bout::checkPositive(g33.ynext(sign * dy), "g33.ynext", - fmt::format("RGN_YPAR_{:+d}", sign * dy)); - } - } - } + contravariantMetricTensor.checkContravariant(); +} - // Off-diagonal metric components should be finite - bout::checkFinite(g12, "g12", "RGN_NOCORNERS"); - bout::checkFinite(g13, "g13", "RGN_NOCORNERS"); - bout::checkFinite(g23, "g23", "RGN_NOCORNERS"); - if (g23.hasParallelSlices() && &g23.ynext(1) != &g23) { - for (int dy = 1; dy <= localmesh->ystart; ++dy) { - for (const auto sign : {1, -1}) { - bout::checkFinite(g12.ynext(sign * dy), "g12.ynext", - fmt::format("RGN_YPAR_{:+d}", sign * dy)); - bout::checkFinite(g13.ynext(sign * dy), "g13.ynext", - fmt::format("RGN_YPAR_{:+d}", sign * dy)); - bout::checkFinite(g23.ynext(sign * dy), "g23.ynext", - fmt::format("RGN_YPAR_{:+d}", sign * dy)); - } - } - } +void Coordinates::setContravariantMetricTensor( + const ContravariantMetricTensor& metric_tensor) { + contravariantMetricTensor.setContravariantMetricTensor(metric_tensor); } -Coordinates::ContravariantMetricTensor Coordinates::getContravariantMetricTensor() const { - ContravariantMetricTensor g_contravariant = { g11, g22, g33, g12, g13, g23 }; - return g_contravariant; +ContravariantMetricTensor Coordinates::getContravariantMetricTensor() const { + return contravariantMetricTensor.getContravariantMetricTensor(); } Coordinates::CovariantMetricTensor Coordinates::getCovariantMetricTensor() const { @@ -2036,17 +1934,6 @@ Coordinates::CovariantMetricTensor Coordinates::getCovariantMetricTensor() const return g_covariant; } -void Coordinates::setContravariantMetricTensor( - const Coordinates::ContravariantMetricTensor& metric_tensor) { - g11 = metric_tensor.g11; - g22 = metric_tensor.g22; - g33 = metric_tensor.g33; - g12 = metric_tensor.g12; - g13 = metric_tensor.g13; - g23 = metric_tensor.g23; - calcCovariant(); -} - void Coordinates::setCovariantMetricTensor( const Coordinates::CovariantMetricTensor& metric_tensor) { g_11 = metric_tensor.g_11; diff --git a/src/mesh/coordinates_accessor.cxx b/src/mesh/coordinates_accessor.cxx index 95e03e83c5..fbb0e0f55f 100644 --- a/src/mesh/coordinates_accessor.cxx +++ b/src/mesh/coordinates_accessor.cxx @@ -1,5 +1,6 @@ #include "bout/coordinates_accessor.hxx" +#include "bout/ContravariantMetricTensor.h" #include "bout/mesh.hxx" #include @@ -60,7 +61,7 @@ CoordinatesAccessor::CoordinatesAccessor(const Coordinates* coords) { coords->Bxy.ydown()[ind]; COPY_STRIPE(G1, G3); - Coordinates::ContravariantMetricTensor contravariant_components = + const auto contravariant_components = coords->getContravariantMetricTensor(); // COPY_STRIPE(g11, g12, g13, g22, g23, g33); data[stripe_size * ind.ind + static_cast(Offset::g11)] = @@ -77,7 +78,7 @@ CoordinatesAccessor::CoordinatesAccessor(const Coordinates* coords) { contravariant_components.g33[ind]; // COPY_STRIPE(g_11, g_12, g_13, g_22, g_23, g_33); - Coordinates::CovariantMetricTensor covariant_components = coords->getCovariantMetricTensor(); + const auto covariant_components = coords->getCovariantMetricTensor(); data[stripe_size * ind.ind + static_cast(Offset::g_11)] = covariant_components.g_11[ind]; data[stripe_size * ind.ind + static_cast(Offset::g_12)] = From 80166813a4725f0271069d35377a71f9e6cabd1f Mon Sep 17 00:00:00 2001 From: tomc271 Date: Mon, 25 Sep 2023 16:48:12 +0100 Subject: [PATCH 046/491] Bundle components in new ContravariantComponents struct. --- include/bout/ContravariantMetricTensor.cpp | 72 ++++++++++++---------- include/bout/ContravariantMetricTensor.h | 19 +++--- include/bout/coordinates.hxx | 2 +- src/mesh/coordinates.cxx | 2 +- 4 files changed, 52 insertions(+), 43 deletions(-) diff --git a/include/bout/ContravariantMetricTensor.cpp b/include/bout/ContravariantMetricTensor.cpp index e8a4fff217..2658ebd972 100644 --- a/include/bout/ContravariantMetricTensor.cpp +++ b/include/bout/ContravariantMetricTensor.cpp @@ -1,20 +1,28 @@ #include "ContravariantMetricTensor.h" +ContravariantMetricTensor::ContravariantMetricTensor( + const FieldMetric g11, const FieldMetric g22, const FieldMetric g33, + const FieldMetric g12, const FieldMetric g13, const FieldMetric g23) { + contravariant_components = {g11, g22, g33, g12, g13, g23}; +} -ContravariantMetricTensor ContravariantMetricTensor::getContravariantMetricTensor() const { - ContravariantMetricTensor g_contravariant = { g11, g22, g33, g12, g13, g23 }; - return g_contravariant; +ContravariantMetricTensor::ContravariantComponents ContravariantMetricTensor::getContravariantMetricTensor() const { + return ContravariantComponents{ + contravariant_components.g11, contravariant_components.g22, + contravariant_components.g33, contravariant_components.g12, + contravariant_components.g13, contravariant_components.g23}; } -void ContravariantMetricTensor::setContravariantMetricTensor( - const ContravariantMetricTensor& metric_tensor) { - g11 = metric_tensor.g11; - g22 = metric_tensor.g22; - g33 = metric_tensor.g33; - g12 = metric_tensor.g12; - g13 = metric_tensor.g13; - g23 = metric_tensor.g23; +void ContravariantMetricTensor::setContravariantMetricTensor(const ContravariantMetricTensor& metric_tensor) { + + const auto new_components = metric_tensor.getContravariantMetricTensor(); + contravariant_components.g11 = new_components.g11; + contravariant_components.g22 = new_components.g22; + contravariant_components.g33 = new_components.g33; + contravariant_components.g12 = new_components.g12; + contravariant_components.g13 = new_components.g13; + contravariant_components.g23 = new_components.g23; calcCovariant(); } @@ -83,50 +91,50 @@ int ContravariantMetricTensor::calcCovariant(const std::string& region) { void ContravariantMetricTensor::checkContravariant() { // Diagonal metric components should be finite - bout::checkFinite(g11, "g11", "RGN_NOCORNERS"); - bout::checkFinite(g22, "g22", "RGN_NOCORNERS"); - bout::checkFinite(g33, "g33", "RGN_NOCORNERS"); - if (g11.hasParallelSlices() && &g11.ynext(1) != &g11) { + bout::checkFinite(contravariant_components.g11, "g11", "RGN_NOCORNERS"); + bout::checkFinite(contravariant_components.g22, "g22", "RGN_NOCORNERS"); + bout::checkFinite(contravariant_components.g33, "g33", "RGN_NOCORNERS"); + if (contravariant_components.g11.hasParallelSlices() && &contravariant_components.g11.ynext(1) != &contravariant_components.g11) { for (int dy = 1; dy <= localmesh->ystart; ++dy) { for (const auto sign : {1, -1}) { - bout::checkFinite(g11.ynext(sign * dy), "g11.ynext", + bout::checkFinite(contravariant_components.g11.ynext(sign * dy), "g11.ynext", fmt::format("RGN_YPAR_{:+d}", sign * dy)); - bout::checkFinite(g22.ynext(sign * dy), "g22.ynext", + bout::checkFinite(contravariant_components.g22.ynext(sign * dy), "g22.ynext", fmt::format("RGN_YPAR_{:+d}", sign * dy)); - bout::checkFinite(g33.ynext(sign * dy), "g33.ynext", + bout::checkFinite(contravariant_components.g33.ynext(sign * dy), "g33.ynext", fmt::format("RGN_YPAR_{:+d}", sign * dy)); } } } // Diagonal metric components should be positive - bout::checkPositive(g11, "g11", "RGN_NOCORNERS"); - bout::checkPositive(g22, "g22", "RGN_NOCORNERS"); - bout::checkPositive(g33, "g33", "RGN_NOCORNERS"); - if (g11.hasParallelSlices() && &g11.ynext(1) != &g11) { + bout::checkPositive(contravariant_components.g11, "g11", "RGN_NOCORNERS"); + bout::checkPositive(contravariant_components.g22, "g22", "RGN_NOCORNERS"); + bout::checkPositive(contravariant_components.g33, "g33", "RGN_NOCORNERS"); + if (contravariant_components.g11.hasParallelSlices() && &contravariant_components.g11.ynext(1) != &contravariant_components.g11) { for (int dy = 1; dy <= localmesh->ystart; ++dy) { for (const auto sign : {1, -1}) { - bout::checkPositive(g11.ynext(sign * dy), "g11.ynext", + bout::checkPositive(contravariant_components.g11.ynext(sign * dy), "g11.ynext", fmt::format("RGN_YPAR_{:+d}", sign * dy)); - bout::checkPositive(g22.ynext(sign * dy), "g22.ynext", + bout::checkPositive(contravariant_components.g22.ynext(sign * dy), "g22.ynext", fmt::format("RGN_YPAR_{:+d}", sign * dy)); - bout::checkPositive(g33.ynext(sign * dy), "g33.ynext", + bout::checkPositive(contravariant_components.g33.ynext(sign * dy), "g33.ynext", fmt::format("RGN_YPAR_{:+d}", sign * dy)); } } } // Off-diagonal metric components should be finite - bout::checkFinite(g12, "g12", "RGN_NOCORNERS"); - bout::checkFinite(g13, "g13", "RGN_NOCORNERS"); - bout::checkFinite(g23, "g23", "RGN_NOCORNERS"); - if (g23.hasParallelSlices() && &g23.ynext(1) != &g23) { + bout::checkFinite(contravariant_components.g12, "g12", "RGN_NOCORNERS"); + bout::checkFinite(contravariant_components.g13, "g13", "RGN_NOCORNERS"); + bout::checkFinite(contravariant_components.g23, "g23", "RGN_NOCORNERS"); + if (contravariant_components.g23.hasParallelSlices() && &contravariant_components.g23.ynext(1) != &contravariant_components.g23) { for (int dy = 1; dy <= localmesh->ystart; ++dy) { for (const auto sign : {1, -1}) { - bout::checkFinite(g12.ynext(sign * dy), "g12.ynext", + bout::checkFinite(contravariant_components.g12.ynext(sign * dy), "g12.ynext", fmt::format("RGN_YPAR_{:+d}", sign * dy)); - bout::checkFinite(g13.ynext(sign * dy), "g13.ynext", + bout::checkFinite(contravariant_components.g13.ynext(sign * dy), "g13.ynext", fmt::format("RGN_YPAR_{:+d}", sign * dy)); - bout::checkFinite(g23.ynext(sign * dy), "g23.ynext", + bout::checkFinite(contravariant_components.g23.ynext(sign * dy), "g23.ynext", fmt::format("RGN_YPAR_{:+d}", sign * dy)); } } diff --git a/include/bout/ContravariantMetricTensor.h b/include/bout/ContravariantMetricTensor.h index 54342260af..d5716ea412 100644 --- a/include/bout/ContravariantMetricTensor.h +++ b/include/bout/ContravariantMetricTensor.h @@ -17,25 +17,26 @@ class ContravariantMetricTensor { using FieldMetric = Field2D; #endif + struct ContravariantComponents { + FieldMetric g11, g22, g33, g12, g13, g23; + }; + ContravariantMetricTensor(const FieldMetric g11, const FieldMetric g22, const FieldMetric g33, const FieldMetric g12, - const FieldMetric g13, const FieldMetric g23) - : g11(g11), - g22(g22), g33(g33), g12(g12), g13(g13), g23(g23) { - } + const FieldMetric g13, const FieldMetric g23); /// Invert contravariant metric to get covariant components int calcCovariant(const std::string& region = "RGN_ALL"); -private: - FieldMetric g11, g22, g33, g12, g13, g23; - // check that contravariant tensors are positive (if expected) and finite (always) void checkContravariant(); -public: void setContravariantMetricTensor(const ContravariantMetricTensor& metric_tensor); - ContravariantMetricTensor getContravariantMetricTensor() const; + + ContravariantComponents getContravariantMetricTensor() const; + +private: + ContravariantComponents contravariant_components; }; #endif //BOUT_CONTRAVARIANTMETRICTENSOR_H diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index 77bfbe3395..d11135c742 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -111,7 +111,7 @@ public: FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; }; - ContravariantMetricTensor getContravariantMetricTensor() const; + ContravariantMetricTensor::ContravariantComponents getContravariantMetricTensor() const; CovariantMetricTensor getCovariantMetricTensor() const; diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 2c550d91e1..990ac13ab8 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -1925,7 +1925,7 @@ void Coordinates::setContravariantMetricTensor( contravariantMetricTensor.setContravariantMetricTensor(metric_tensor); } -ContravariantMetricTensor Coordinates::getContravariantMetricTensor() const { +ContravariantMetricTensor::ContravariantComponents Coordinates::getContravariantMetricTensor() const { return contravariantMetricTensor.getContravariantMetricTensor(); } From 8355b88bb776a14e8785c2e656d1831077c22559 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Tue, 26 Sep 2023 09:07:55 +0100 Subject: [PATCH 047/491] Added struct CovariantMetricTensor. --- include/bout/ContravariantMetricTensor.h | 4 ++++ include/bout/coordinates.hxx | 12 ++++-------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/include/bout/ContravariantMetricTensor.h b/include/bout/ContravariantMetricTensor.h index d5716ea412..082ac874a6 100644 --- a/include/bout/ContravariantMetricTensor.h +++ b/include/bout/ContravariantMetricTensor.h @@ -21,6 +21,10 @@ class ContravariantMetricTensor { FieldMetric g11, g22, g33, g12, g13, g23; }; + struct CovariantMetricTensor { + FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; + }; + ContravariantMetricTensor(const FieldMetric g11, const FieldMetric g22, const FieldMetric g33, const FieldMetric g12, const FieldMetric g13, const FieldMetric g23); diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index d11135c742..b31de11cd6 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -100,20 +100,16 @@ public: private: ContravariantMetricTensor contravariantMetricTensor; + /// Covariant metric tensor - FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; + CovariantMetricTensor covariantMetricTensor; +// FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; public: - - struct CovariantMetricTensor - { - FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; - }; - ContravariantMetricTensor::ContravariantComponents getContravariantMetricTensor() const; - CovariantMetricTensor getCovariantMetricTensor() const; + CovariantMetricTensor::CovariantComponents getCovariantMetricTensor() const; void setContravariantMetricTensor(const ContravariantMetricTensor& metric_tensor); From e66fb708baf72b94a02a636f4bb9316f00dd203a Mon Sep 17 00:00:00 2001 From: tomc271 Date: Tue, 26 Sep 2023 09:58:21 +0100 Subject: [PATCH 048/491] Fix typo. --- include/bout/ContravariantMetricTensor.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/bout/ContravariantMetricTensor.cpp b/include/bout/ContravariantMetricTensor.cpp index 2658ebd972..26a37b197d 100644 --- a/include/bout/ContravariantMetricTensor.cpp +++ b/include/bout/ContravariantMetricTensor.cpp @@ -27,7 +27,7 @@ void ContravariantMetricTensor::setContravariantMetricTensor(const Contravariant } int ContravariantMetricTensor::calcCovariant(const std::string& region) { - TRACE("Coordinates::calcCovariant"); + TRACE("ContravariantMetricTensor::calcCovariant"); // Make sure metric elements are allocated g_11.allocate(); From 1f3a6ddecb9f15f71a85f26903a93528db68d063 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Tue, 26 Sep 2023 09:59:09 +0100 Subject: [PATCH 049/491] Extracted class CovariantMetricTensor. Bundle components in new CovariantComponents struct. --- CMakeLists.txt | 4 +- include/bout/ContravariantMetricTensor.h | 4 - include/bout/CovariantMetricTensor.cpp | 135 +++++++++++++++++++++++ include/bout/CovariantMetricTensor.h | 39 +++++++ include/bout/coordinates.hxx | 4 +- src/mesh/coordinates.cxx | 124 ++------------------- 6 files changed, 185 insertions(+), 125 deletions(-) create mode 100644 include/bout/CovariantMetricTensor.cpp create mode 100644 include/bout/CovariantMetricTensor.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 97d20cf66e..9ec9c53948 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -354,8 +354,10 @@ set(BOUT_SOURCES ./src/sys/utils.cxx ${CMAKE_CURRENT_BINARY_DIR}/include/bout/revision.hxx ${CMAKE_CURRENT_BINARY_DIR}/include/bout/version.hxx - include/bout/ContravariantMetricTensor.cpp include/bout/ContravariantMetricTensor.h + include/bout/ContravariantMetricTensor.cpp + include/bout/CovariantMetricTensor.h + include/bout/CovariantMetricTensor.cpp ) diff --git a/include/bout/ContravariantMetricTensor.h b/include/bout/ContravariantMetricTensor.h index 082ac874a6..d5716ea412 100644 --- a/include/bout/ContravariantMetricTensor.h +++ b/include/bout/ContravariantMetricTensor.h @@ -21,10 +21,6 @@ class ContravariantMetricTensor { FieldMetric g11, g22, g33, g12, g13, g23; }; - struct CovariantMetricTensor { - FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; - }; - ContravariantMetricTensor(const FieldMetric g11, const FieldMetric g22, const FieldMetric g33, const FieldMetric g12, const FieldMetric g13, const FieldMetric g23); diff --git a/include/bout/CovariantMetricTensor.cpp b/include/bout/CovariantMetricTensor.cpp new file mode 100644 index 0000000000..a5fa7d9d5f --- /dev/null +++ b/include/bout/CovariantMetricTensor.cpp @@ -0,0 +1,135 @@ + +#include "CovariantMetricTensor.h" + +CovariantMetricTensor::CovariantMetricTensor( + const FieldMetric g_11, const FieldMetric g_22, const FieldMetric g_33, + const FieldMetric g_12, const FieldMetric g_13, const FieldMetric g_23) { + covariant_components = {g_11, g_22, g_33, g_12, g_13, g_23}; +} + +CovariantMetricTensor::CovariantComponents +CovariantMetricTensor::getCovariantMetricTensor() const { + return CovariantComponents{covariant_components.g_11, covariant_components.g_22, + covariant_components.g_33, covariant_components.g_12, + covariant_components.g_13, covariant_components.g_23}; +} + +void CovariantMetricTensor::setCovariantMetricTensor( + const CovariantMetricTensor& metric_tensor) { + + const auto new_components = metric_tensor.getCovariantMetricTensor(); + covariant_components.g_11 = new_components.g_11; + covariant_components.g_22 = new_components.g_22; + covariant_components.g_33 = new_components.g_33; + covariant_components.g_12 = new_components.g_12; + covariant_components.g_13 = new_components.g_13; + covariant_components.g_23 = new_components.g_23; + calcContravariant(); +} + +int CovariantMetricTensor::calcContravariant(const std::string& region) { + TRACE("CovariantMetricTensor::calcContravariant"); + + // Make sure metric elements are allocated + g11.allocate(); + g22.allocate(); + g33.allocate(); + g12.allocate(); + g13.allocate(); + g23.allocate(); + + // Perform inversion of g_{ij} to get g^{ij} + // NOTE: Currently this bit assumes that metric terms are Field2D objects + + auto a = Matrix(3, 3); + + BOUT_FOR_SERIAL(i, g_11.getRegion(region)) { + a(0, 0) = g_11[i]; + a(1, 1) = g_22[i]; + a(2, 2) = g_33[i]; + + a(0, 1) = a(1, 0) = g_12[i]; + a(1, 2) = a(2, 1) = g_23[i]; + a(0, 2) = a(2, 0) = g_13[i]; + + if (invert3x3(a)) { + output_error.write("\tERROR: metric tensor is singular at ({:d}, {:d})\n", i.x(), + i.y()); + return 1; + } + + g11[i] = a(0, 0); + g22[i] = a(1, 1); + g33[i] = a(2, 2); + + g12[i] = a(0, 1); + g13[i] = a(0, 2); + g23[i] = a(1, 2); + } + + BoutReal maxerr; + maxerr = BOUTMAX(max(abs((g_11 * g11 + g_12 * g12 + g_13 * g13) - 1)), + max(abs((g_12 * g12 + g_22 * g22 + g_23 * g23) - 1)), + max(abs((g_13 * g13 + g_23 * g23 + g_33 * g33) - 1))); + + output_info.write("\tMaximum error in diagonal inversion is {:e}\n", maxerr); + + maxerr = BOUTMAX(max(abs(g_11 * g12 + g_12 * g22 + g_13 * g23)), + max(abs(g_11 * g13 + g_12 * g23 + g_13 * g33)), + max(abs(g_12 * g13 + g_22 * g23 + g_23 * g33))); + + output_info.write("\tMaximum error in off-diagonal inversion is {:e}\n", maxerr); + return 0; +} + +void CovariantMetricTensor::checkCovariant() { + // Diagonal metric components should be finite + bout::checkFinite(g_11, "g_11", "RGN_NOCORNERS"); + bout::checkFinite(g_22, "g_22", "RGN_NOCORNERS"); + bout::checkFinite(g_33, "g_33", "RGN_NOCORNERS"); + if (g_11.hasParallelSlices() && &g_11.ynext(1) != &g_11) { + for (int dy = 1; dy <= localmesh->ystart; ++dy) { + for (const auto sign : {1, -1}) { + bout::checkFinite(g_11.ynext(sign * dy), "g_11.ynext", + fmt::format("RGN_YPAR_{:+d}", sign * dy)); + bout::checkFinite(g_22.ynext(sign * dy), "g_22.ynext", + fmt::format("RGN_YPAR_{:+d}", sign * dy)); + bout::checkFinite(g_33.ynext(sign * dy), "g_33.ynext", + fmt::format("RGN_YPAR_{:+d}", sign * dy)); + } + } + } + // Diagonal metric components should be positive + bout::checkPositive(g_11, "g_11", "RGN_NOCORNERS"); + bout::checkPositive(g_22, "g_22", "RGN_NOCORNERS"); + bout::checkPositive(g_33, "g_33", "RGN_NOCORNERS"); + if (g_11.hasParallelSlices() && &g_11.ynext(1) != &g_11) { + for (int dy = 1; dy <= localmesh->ystart; ++dy) { + for (const auto sign : {1, -1}) { + bout::checkPositive(g_11.ynext(sign * dy), "g_11.ynext", + fmt::format("RGN_YPAR_{:+d}", sign * dy)); + bout::checkPositive(g_22.ynext(sign * dy), "g_22.ynext", + fmt::format("RGN_YPAR_{:+d}", sign * dy)); + bout::checkPositive(g_33.ynext(sign * dy), "g_33.ynext", + fmt::format("RGN_YPAR_{:+d}", sign * dy)); + } + } + } + + // Off-diagonal metric components should be finite + bout::checkFinite(g_12, "g_12", "RGN_NOCORNERS"); + bout::checkFinite(g_13, "g_13", "RGN_NOCORNERS"); + bout::checkFinite(g_23, "g_23", "RGN_NOCORNERS"); + if (g_23.hasParallelSlices() && &g_23.ynext(1) != &g_23) { + for (int dy = 1; dy <= localmesh->ystart; ++dy) { + for (const auto sign : {1, -1}) { + bout::checkFinite(g_12.ynext(sign * dy), "g_12.ynext", + fmt::format("RGN_YPAR_{:+d}", sign * dy)); + bout::checkFinite(g_13.ynext(sign * dy), "g_13.ynext", + fmt::format("RGN_YPAR_{:+d}", sign * dy)); + bout::checkFinite(g_23.ynext(sign * dy), "g_23.ynext", + fmt::format("RGN_YPAR_{:+d}", sign * dy)); + } + } + } +} diff --git a/include/bout/CovariantMetricTensor.h b/include/bout/CovariantMetricTensor.h new file mode 100644 index 0000000000..8eb4a072ac --- /dev/null +++ b/include/bout/CovariantMetricTensor.h @@ -0,0 +1,39 @@ + +#ifndef BOUT_COVARIANTMETRICTENSOR_H +#define BOUT_COVARIANTMETRICTENSOR_H + +#include "field2d.hxx" +#include "bout/field3d.hxx" + +class CovariantMetricTensor { + +public: +#if BOUT_USE_METRIC_3D + using FieldMetric = Field +#else + using FieldMetric = Field2D; +#endif + + struct CovariantComponents { + FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; + }; + + CovariantMetricTensor(const FieldMetric g_11, const FieldMetric g_22, + const FieldMetric g_33, const FieldMetric g_12, + const FieldMetric g_13, const FieldMetric g_23); + + /// Invert covariant metric to get contravariant components + int calcContravariant(const std::string& region = "RGN_ALL"); + + // check that covariant tensors are positive (if expected) and finite (always) + void checkCovariant(); + + void setCovariantMetricTensor(const CovariantMetricTensor& metric_tensor); + + CovariantComponents getCovariantMetricTensor() const; + +private: + CovariantComponents covariant_components; +}; + +#endif //BOUT_COVARIANTMETRICTENSOR_H diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index b31de11cd6..61e4958ee8 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -34,6 +34,7 @@ #define BOUT_COORDINATES_H #include "ContravariantMetricTensor.h" +#include "CovariantMetricTensor.h" #include "bout/field2d.hxx" #include "bout/field3d.hxx" #include "bout/paralleltransform.hxx" @@ -100,10 +101,7 @@ public: private: ContravariantMetricTensor contravariantMetricTensor; - - /// Covariant metric tensor CovariantMetricTensor covariantMetricTensor; -// FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; public: diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 990ac13ab8..cb893808be 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -1221,57 +1221,7 @@ int Coordinates::calcCovariant(const std::string& region) { } int Coordinates::calcContravariant(const std::string& region) { TRACE("Coordinates::calcContravariant"); - - // Make sure metric elements are allocated - g11.allocate(); - g22.allocate(); - g33.allocate(); - g12.allocate(); - g13.allocate(); - g23.allocate(); - - // Perform inversion of g_{ij} to get g^{ij} - // NOTE: Currently this bit assumes that metric terms are Field2D objects - - auto a = Matrix(3, 3); - - BOUT_FOR_SERIAL(i, g_11.getRegion(region)) { - a(0, 0) = g_11[i]; - a(1, 1) = g_22[i]; - a(2, 2) = g_33[i]; - - a(0, 1) = a(1, 0) = g_12[i]; - a(1, 2) = a(2, 1) = g_23[i]; - a(0, 2) = a(2, 0) = g_13[i]; - - if (invert3x3(a)) { - output_error.write("\tERROR: metric tensor is singular at ({:d}, {:d})\n", i.x(), - i.y()); - return 1; - } - - g11[i] = a(0, 0); - g22[i] = a(1, 1); - g33[i] = a(2, 2); - - g12[i] = a(0, 1); - g13[i] = a(0, 2); - g23[i] = a(1, 2); - } - - BoutReal maxerr; - maxerr = BOUTMAX(max(abs((g_11 * g11 + g_12 * g12 + g_13 * g13) - 1)), - max(abs((g_12 * g12 + g_22 * g22 + g_23 * g23) - 1)), - max(abs((g_13 * g13 + g_23 * g23 + g_33 * g33) - 1))); - - output_info.write("\tMaximum error in diagonal inversion is {:e}\n", maxerr); - - maxerr = BOUTMAX(max(abs(g_11 * g12 + g_12 * g22 + g_13 * g23)), - max(abs(g_11 * g13 + g_12 * g23 + g_13 * g33)), - max(abs(g_12 * g13 + g_22 * g23 + g_23 * g33))); - - output_info.write("\tMaximum error in off-diagonal inversion is {:e}\n", maxerr); - return 0; + return covariantMetricTensor.calcContravariant(); } int Coordinates::jacobian() { @@ -1864,61 +1814,9 @@ Coordinates::Grad2_par2_DDY_invSg(CELL_LOC outloc, const std::string& method) co return *Grad2_par2_DDY_invSgCache[method]; } -void Coordinates::checkCovariant() { - // Diagonal metric components should be finite - bout::checkFinite(g_11, "g_11", "RGN_NOCORNERS"); - bout::checkFinite(g_22, "g_22", "RGN_NOCORNERS"); - bout::checkFinite(g_33, "g_33", "RGN_NOCORNERS"); - if (g_11.hasParallelSlices() && &g_11.ynext(1) != &g_11) { - for (int dy = 1; dy <= localmesh->ystart; ++dy) { - for (const auto sign : {1, -1}) { - bout::checkFinite(g_11.ynext(sign * dy), "g_11.ynext", - fmt::format("RGN_YPAR_{:+d}", sign * dy)); - bout::checkFinite(g_22.ynext(sign * dy), "g_22.ynext", - fmt::format("RGN_YPAR_{:+d}", sign * dy)); - bout::checkFinite(g_33.ynext(sign * dy), "g_33.ynext", - fmt::format("RGN_YPAR_{:+d}", sign * dy)); - } - } - } - // Diagonal metric components should be positive - bout::checkPositive(g_11, "g_11", "RGN_NOCORNERS"); - bout::checkPositive(g_22, "g_22", "RGN_NOCORNERS"); - bout::checkPositive(g_33, "g_33", "RGN_NOCORNERS"); - if (g_11.hasParallelSlices() && &g_11.ynext(1) != &g_11) { - for (int dy = 1; dy <= localmesh->ystart; ++dy) { - for (const auto sign : {1, -1}) { - bout::checkPositive(g_11.ynext(sign * dy), "g_11.ynext", - fmt::format("RGN_YPAR_{:+d}", sign * dy)); - bout::checkPositive(g_22.ynext(sign * dy), "g_22.ynext", - fmt::format("RGN_YPAR_{:+d}", sign * dy)); - bout::checkPositive(g_33.ynext(sign * dy), "g_33.ynext", - fmt::format("RGN_YPAR_{:+d}", sign * dy)); - } - } - } +void Coordinates::checkCovariant() { covariantMetricTensor.checkCovariant(); } - // Off-diagonal metric components should be finite - bout::checkFinite(g_12, "g_12", "RGN_NOCORNERS"); - bout::checkFinite(g_13, "g_13", "RGN_NOCORNERS"); - bout::checkFinite(g_23, "g_23", "RGN_NOCORNERS"); - if (g_23.hasParallelSlices() && &g_23.ynext(1) != &g_23) { - for (int dy = 1; dy <= localmesh->ystart; ++dy) { - for (const auto sign : {1, -1}) { - bout::checkFinite(g_12.ynext(sign * dy), "g_12.ynext", - fmt::format("RGN_YPAR_{:+d}", sign * dy)); - bout::checkFinite(g_13.ynext(sign * dy), "g_13.ynext", - fmt::format("RGN_YPAR_{:+d}", sign * dy)); - bout::checkFinite(g_23.ynext(sign * dy), "g_23.ynext", - fmt::format("RGN_YPAR_{:+d}", sign * dy)); - } - } - } -} - -void Coordinates::checkContravariant() { - contravariantMetricTensor.checkContravariant(); -} +void Coordinates::checkContravariant() { contravariantMetricTensor.checkContravariant(); } void Coordinates::setContravariantMetricTensor( const ContravariantMetricTensor& metric_tensor) { @@ -1929,18 +1827,10 @@ ContravariantMetricTensor::ContravariantComponents Coordinates::getContravariant return contravariantMetricTensor.getContravariantMetricTensor(); } -Coordinates::CovariantMetricTensor Coordinates::getCovariantMetricTensor() const { - CovariantMetricTensor g_covariant = { g_11, g_22, g_33, g_12, g_13, g_23 }; - return g_covariant; +CovariantMetricTensor::CovariantComponents Coordinates::getCovariantMetricTensor() const { + return covariantMetricTensor.getCovariantMetricTensor(); } -void Coordinates::setCovariantMetricTensor( - const Coordinates::CovariantMetricTensor& metric_tensor) { - g_11 = metric_tensor.g_11; - g_22 = metric_tensor.g_22; - g_33 = metric_tensor.g_33; - g_12 = metric_tensor.g_12; - g_13 = metric_tensor.g_13; - g_23 = metric_tensor.g_23; - calcContravariant(); +void Coordinates::setCovariantMetricTensor(const CovariantMetricTensor& metric_tensor) { + covariantMetricTensor.setCovariantMetricTensor(metric_tensor); } From 5aaa4b5d4f38de6bb6a5d7b93e0d4c4739b54dbb Mon Sep 17 00:00:00 2001 From: tomc271 Date: Tue, 26 Sep 2023 16:37:06 +0100 Subject: [PATCH 050/491] Fix calcContravariant and checkCovariant methods. --- include/bout/ContravariantMetricTensor.cpp | 25 +++- include/bout/ContravariantMetricTensor.h | 2 + include/bout/CovariantMetricTensor.cpp | 134 +++++++++++---------- include/bout/CovariantMetricTensor.h | 2 +- src/mesh/coordinates.cxx | 3 +- 5 files changed, 97 insertions(+), 69 deletions(-) diff --git a/include/bout/ContravariantMetricTensor.cpp b/include/bout/ContravariantMetricTensor.cpp index 26a37b197d..f9b67b633a 100644 --- a/include/bout/ContravariantMetricTensor.cpp +++ b/include/bout/ContravariantMetricTensor.cpp @@ -5,16 +5,19 @@ ContravariantMetricTensor::ContravariantMetricTensor( const FieldMetric g11, const FieldMetric g22, const FieldMetric g33, const FieldMetric g12, const FieldMetric g13, const FieldMetric g23) { contravariant_components = {g11, g22, g33, g12, g13, g23}; + Allocate(); // Make sure metric elements are allocated // ; TODO: Required? } -ContravariantMetricTensor::ContravariantComponents ContravariantMetricTensor::getContravariantMetricTensor() const { +ContravariantMetricTensor::ContravariantComponents +ContravariantMetricTensor::getContravariantMetricTensor() const { return ContravariantComponents{ contravariant_components.g11, contravariant_components.g22, contravariant_components.g33, contravariant_components.g12, contravariant_components.g13, contravariant_components.g23}; } -void ContravariantMetricTensor::setContravariantMetricTensor(const ContravariantMetricTensor& metric_tensor) { +void ContravariantMetricTensor::setContravariantMetricTensor( + const ContravariantMetricTensor& metric_tensor) { const auto new_components = metric_tensor.getContravariantMetricTensor(); contravariant_components.g11 = new_components.g11; @@ -94,7 +97,8 @@ void ContravariantMetricTensor::checkContravariant() { bout::checkFinite(contravariant_components.g11, "g11", "RGN_NOCORNERS"); bout::checkFinite(contravariant_components.g22, "g22", "RGN_NOCORNERS"); bout::checkFinite(contravariant_components.g33, "g33", "RGN_NOCORNERS"); - if (contravariant_components.g11.hasParallelSlices() && &contravariant_components.g11.ynext(1) != &contravariant_components.g11) { + if (contravariant_components.g11.hasParallelSlices() + && &contravariant_components.g11.ynext(1) != &contravariant_components.g11) { for (int dy = 1; dy <= localmesh->ystart; ++dy) { for (const auto sign : {1, -1}) { bout::checkFinite(contravariant_components.g11.ynext(sign * dy), "g11.ynext", @@ -110,7 +114,8 @@ void ContravariantMetricTensor::checkContravariant() { bout::checkPositive(contravariant_components.g11, "g11", "RGN_NOCORNERS"); bout::checkPositive(contravariant_components.g22, "g22", "RGN_NOCORNERS"); bout::checkPositive(contravariant_components.g33, "g33", "RGN_NOCORNERS"); - if (contravariant_components.g11.hasParallelSlices() && &contravariant_components.g11.ynext(1) != &contravariant_components.g11) { + if (contravariant_components.g11.hasParallelSlices() + && &contravariant_components.g11.ynext(1) != &contravariant_components.g11) { for (int dy = 1; dy <= localmesh->ystart; ++dy) { for (const auto sign : {1, -1}) { bout::checkPositive(contravariant_components.g11.ynext(sign * dy), "g11.ynext", @@ -127,7 +132,8 @@ void ContravariantMetricTensor::checkContravariant() { bout::checkFinite(contravariant_components.g12, "g12", "RGN_NOCORNERS"); bout::checkFinite(contravariant_components.g13, "g13", "RGN_NOCORNERS"); bout::checkFinite(contravariant_components.g23, "g23", "RGN_NOCORNERS"); - if (contravariant_components.g23.hasParallelSlices() && &contravariant_components.g23.ynext(1) != &contravariant_components.g23) { + if (contravariant_components.g23.hasParallelSlices() + && &contravariant_components.g23.ynext(1) != &contravariant_components.g23) { for (int dy = 1; dy <= localmesh->ystart; ++dy) { for (const auto sign : {1, -1}) { bout::checkFinite(contravariant_components.g12.ynext(sign * dy), "g12.ynext", @@ -140,3 +146,12 @@ void ContravariantMetricTensor::checkContravariant() { } } } + +void ContravariantMetricTensor::Allocate() { // ; TODO: Required? + contravariant_components.g11.allocate(); + contravariant_components.g22.allocate(); + contravariant_components.g33.allocate(); + contravariant_components.g12.allocate(); + contravariant_components.g13.allocate(); + contravariant_components.g23.allocate(); +} diff --git a/include/bout/ContravariantMetricTensor.h b/include/bout/ContravariantMetricTensor.h index d5716ea412..d77953aeb7 100644 --- a/include/bout/ContravariantMetricTensor.h +++ b/include/bout/ContravariantMetricTensor.h @@ -35,6 +35,8 @@ class ContravariantMetricTensor { ContravariantComponents getContravariantMetricTensor() const; + void Allocate(); + private: ContravariantComponents contravariant_components; }; diff --git a/include/bout/CovariantMetricTensor.cpp b/include/bout/CovariantMetricTensor.cpp index a5fa7d9d5f..12c2a033f6 100644 --- a/include/bout/CovariantMetricTensor.cpp +++ b/include/bout/CovariantMetricTensor.cpp @@ -1,5 +1,8 @@ #include "CovariantMetricTensor.h" +#include "ContravariantMetricTensor.h" +#include +#include CovariantMetricTensor::CovariantMetricTensor( const FieldMetric g_11, const FieldMetric g_22, const FieldMetric g_33, @@ -30,27 +33,19 @@ void CovariantMetricTensor::setCovariantMetricTensor( int CovariantMetricTensor::calcContravariant(const std::string& region) { TRACE("CovariantMetricTensor::calcContravariant"); - // Make sure metric elements are allocated - g11.allocate(); - g22.allocate(); - g33.allocate(); - g12.allocate(); - g13.allocate(); - g23.allocate(); - // Perform inversion of g_{ij} to get g^{ij} // NOTE: Currently this bit assumes that metric terms are Field2D objects auto a = Matrix(3, 3); - BOUT_FOR_SERIAL(i, g_11.getRegion(region)) { - a(0, 0) = g_11[i]; - a(1, 1) = g_22[i]; - a(2, 2) = g_33[i]; + BOUT_FOR_SERIAL(i, covariant_components.g_11.getRegion(region)) { + a(0, 0) = covariant_components.g_11[i]; + a(1, 1) = covariant_components.g_22[i]; + a(2, 2) = covariant_components.g_33[i]; - a(0, 1) = a(1, 0) = g_12[i]; - a(1, 2) = a(2, 1) = g_23[i]; - a(0, 2) = a(2, 0) = g_13[i]; + a(0, 1) = a(1, 0) = covariant_components.g_12[i]; + a(1, 2) = a(2, 1) = covariant_components.g_23[i]; + a(0, 2) = a(2, 0) = covariant_components.g_13[i]; if (invert3x3(a)) { output_error.write("\tERROR: metric tensor is singular at ({:d}, {:d})\n", i.x(), @@ -58,76 +53,93 @@ int CovariantMetricTensor::calcContravariant(const std::string& region) { return 1; } - g11[i] = a(0, 0); - g22[i] = a(1, 1); - g33[i] = a(2, 2); - - g12[i] = a(0, 1); - g13[i] = a(0, 2); - g23[i] = a(1, 2); + ContravariantMetricTensor const contravariantMetricTensor = + ContravariantMetricTensor(a(0, 0), a(1, 1), a(2, 2), a(0, 1), a(0, 2), a(1, 2)); + + auto const contravariant_components = + contravariantMetricTensor.getContravariantMetricTensor(); + + BoutReal maxerr; + maxerr = BOUTMAX(max(abs((covariant_components.g_11 * contravariant_components.g11 + + covariant_components.g_12 * contravariant_components.g12 + + covariant_components.g_13 * contravariant_components.g13) + - 1)), + max(abs((covariant_components.g_12 * contravariant_components.g12 + + covariant_components.g_22 * contravariant_components.g22 + + covariant_components.g_23 * contravariant_components.g23) + - 1)), + max(abs((covariant_components.g_13 * contravariant_components.g13 + + covariant_components.g_23 * contravariant_components.g23 + + covariant_components.g_33 * contravariant_components.g33) + - 1))); + + output_info.write("\tMaximum error in diagonal inversion is {:e}\n", maxerr); + + maxerr = + BOUTMAX(max(abs(covariant_components.g_11 * contravariant_components.g12 + + covariant_components.g_12 * contravariant_components.g22 + + covariant_components.g_13 * contravariant_components.g23)), + max(abs(covariant_components.g_11 * contravariant_components.g13 + + covariant_components.g_12 * contravariant_components.g23 + + covariant_components.g_13 * contravariant_components.g33)), + max(abs(covariant_components.g_12 * contravariant_components.g13 + + covariant_components.g_22 * contravariant_components.g23 + + covariant_components.g_23 * contravariant_components.g33))); + + output_info.write("\tMaximum error in off-diagonal inversion is {:e}\n", maxerr); + return 0; } - - BoutReal maxerr; - maxerr = BOUTMAX(max(abs((g_11 * g11 + g_12 * g12 + g_13 * g13) - 1)), - max(abs((g_12 * g12 + g_22 * g22 + g_23 * g23) - 1)), - max(abs((g_13 * g13 + g_23 * g23 + g_33 * g33) - 1))); - - output_info.write("\tMaximum error in diagonal inversion is {:e}\n", maxerr); - - maxerr = BOUTMAX(max(abs(g_11 * g12 + g_12 * g22 + g_13 * g23)), - max(abs(g_11 * g13 + g_12 * g23 + g_13 * g33)), - max(abs(g_12 * g13 + g_22 * g23 + g_23 * g33))); - - output_info.write("\tMaximum error in off-diagonal inversion is {:e}\n", maxerr); - return 0; } -void CovariantMetricTensor::checkCovariant() { +void CovariantMetricTensor::checkCovariant(int ystart) { // Diagonal metric components should be finite - bout::checkFinite(g_11, "g_11", "RGN_NOCORNERS"); - bout::checkFinite(g_22, "g_22", "RGN_NOCORNERS"); - bout::checkFinite(g_33, "g_33", "RGN_NOCORNERS"); - if (g_11.hasParallelSlices() && &g_11.ynext(1) != &g_11) { - for (int dy = 1; dy <= localmesh->ystart; ++dy) { + bout::checkFinite(covariant_components.g_11, "g_11", "RGN_NOCORNERS"); + bout::checkFinite(covariant_components.g_22, "g_22", "RGN_NOCORNERS"); + bout::checkFinite(covariant_components.g_33, "g_33", "RGN_NOCORNERS"); + if (covariant_components.g_11.hasParallelSlices() + && &covariant_components.g_11.ynext(1) != &covariant_components.g_11) { + for (int dy = 1; dy <= ystart; ++dy) { for (const auto sign : {1, -1}) { - bout::checkFinite(g_11.ynext(sign * dy), "g_11.ynext", + bout::checkFinite(covariant_components.g_11.ynext(sign * dy), "g_11.ynext", fmt::format("RGN_YPAR_{:+d}", sign * dy)); - bout::checkFinite(g_22.ynext(sign * dy), "g_22.ynext", + bout::checkFinite(covariant_components.g_22.ynext(sign * dy), "g_22.ynext", fmt::format("RGN_YPAR_{:+d}", sign * dy)); - bout::checkFinite(g_33.ynext(sign * dy), "g_33.ynext", + bout::checkFinite(covariant_components.g_33.ynext(sign * dy), "g_33.ynext", fmt::format("RGN_YPAR_{:+d}", sign * dy)); } } } // Diagonal metric components should be positive - bout::checkPositive(g_11, "g_11", "RGN_NOCORNERS"); - bout::checkPositive(g_22, "g_22", "RGN_NOCORNERS"); - bout::checkPositive(g_33, "g_33", "RGN_NOCORNERS"); - if (g_11.hasParallelSlices() && &g_11.ynext(1) != &g_11) { - for (int dy = 1; dy <= localmesh->ystart; ++dy) { + bout::checkPositive(covariant_components.g_11, "g_11", "RGN_NOCORNERS"); + bout::checkPositive(covariant_components.g_22, "g_22", "RGN_NOCORNERS"); + bout::checkPositive(covariant_components.g_33, "g_33", "RGN_NOCORNERS"); + if (covariant_components.g_11.hasParallelSlices() + && &covariant_components.g_11.ynext(1) != &covariant_components.g_11) { + for (int dy = 1; dy <= ystart; ++dy) { for (const auto sign : {1, -1}) { - bout::checkPositive(g_11.ynext(sign * dy), "g_11.ynext", + bout::checkPositive(covariant_components.g_11.ynext(sign * dy), "g_11.ynext", fmt::format("RGN_YPAR_{:+d}", sign * dy)); - bout::checkPositive(g_22.ynext(sign * dy), "g_22.ynext", + bout::checkPositive(covariant_components.g_22.ynext(sign * dy), "g_22.ynext", fmt::format("RGN_YPAR_{:+d}", sign * dy)); - bout::checkPositive(g_33.ynext(sign * dy), "g_33.ynext", + bout::checkPositive(covariant_components.g_33.ynext(sign * dy), "g_33.ynext", fmt::format("RGN_YPAR_{:+d}", sign * dy)); } } } // Off-diagonal metric components should be finite - bout::checkFinite(g_12, "g_12", "RGN_NOCORNERS"); - bout::checkFinite(g_13, "g_13", "RGN_NOCORNERS"); - bout::checkFinite(g_23, "g_23", "RGN_NOCORNERS"); - if (g_23.hasParallelSlices() && &g_23.ynext(1) != &g_23) { - for (int dy = 1; dy <= localmesh->ystart; ++dy) { + bout::checkFinite(covariant_components.g_12, "g_12", "RGN_NOCORNERS"); + bout::checkFinite(covariant_components.g_13, "g_13", "RGN_NOCORNERS"); + bout::checkFinite(covariant_components.g_23, "g_23", "RGN_NOCORNERS"); + if (covariant_components.g_23.hasParallelSlices() + && &covariant_components.g_23.ynext(1) != &covariant_components.g_23) { + for (int dy = 1; dy <= ystart; ++dy) { for (const auto sign : {1, -1}) { - bout::checkFinite(g_12.ynext(sign * dy), "g_12.ynext", + bout::checkFinite(covariant_components.g_12.ynext(sign * dy), "g_12.ynext", fmt::format("RGN_YPAR_{:+d}", sign * dy)); - bout::checkFinite(g_13.ynext(sign * dy), "g_13.ynext", + bout::checkFinite(covariant_components.g_13.ynext(sign * dy), "g_13.ynext", fmt::format("RGN_YPAR_{:+d}", sign * dy)); - bout::checkFinite(g_23.ynext(sign * dy), "g_23.ynext", + bout::checkFinite(covariant_components.g_23.ynext(sign * dy), "g_23.ynext", fmt::format("RGN_YPAR_{:+d}", sign * dy)); } } diff --git a/include/bout/CovariantMetricTensor.h b/include/bout/CovariantMetricTensor.h index 8eb4a072ac..f2b6d3139c 100644 --- a/include/bout/CovariantMetricTensor.h +++ b/include/bout/CovariantMetricTensor.h @@ -26,7 +26,7 @@ class CovariantMetricTensor { int calcContravariant(const std::string& region = "RGN_ALL"); // check that covariant tensors are positive (if expected) and finite (always) - void checkCovariant(); + void checkCovariant(int ystart); void setCovariantMetricTensor(const CovariantMetricTensor& metric_tensor); diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index cb893808be..d36b2ea044 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -8,7 +8,6 @@ #include #include #include -#include #include #include @@ -1814,7 +1813,7 @@ Coordinates::Grad2_par2_DDY_invSg(CELL_LOC outloc, const std::string& method) co return *Grad2_par2_DDY_invSgCache[method]; } -void Coordinates::checkCovariant() { covariantMetricTensor.checkCovariant(); } +void Coordinates::checkCovariant() { covariantMetricTensor.checkCovariant(localmesh->ystart); } void Coordinates::checkContravariant() { contravariantMetricTensor.checkContravariant(); } From 3f0e0eb1789652d25390b72b65d3aa8e23fe7bce Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 27 Sep 2023 09:31:41 +0100 Subject: [PATCH 051/491] Fix brackets. --- include/bout/CovariantMetricTensor.cpp | 60 +++++++++++++------------- 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/include/bout/CovariantMetricTensor.cpp b/include/bout/CovariantMetricTensor.cpp index 12c2a033f6..ca3f787053 100644 --- a/include/bout/CovariantMetricTensor.cpp +++ b/include/bout/CovariantMetricTensor.cpp @@ -52,43 +52,43 @@ int CovariantMetricTensor::calcContravariant(const std::string& region) { i.y()); return 1; } + } - ContravariantMetricTensor const contravariantMetricTensor = - ContravariantMetricTensor(a(0, 0), a(1, 1), a(2, 2), a(0, 1), a(0, 2), a(1, 2)); + ContravariantMetricTensor const contravariantMetricTensor = + ContravariantMetricTensor(a(0, 0), a(1, 1), a(2, 2), a(0, 1), a(0, 2), a(1, 2)); - auto const contravariant_components = - contravariantMetricTensor.getContravariantMetricTensor(); + auto const contravariant_components = + contravariantMetricTensor.getContravariantMetricTensor(); - BoutReal maxerr; - maxerr = BOUTMAX(max(abs((covariant_components.g_11 * contravariant_components.g11 - + covariant_components.g_12 * contravariant_components.g12 - + covariant_components.g_13 * contravariant_components.g13) - - 1)), - max(abs((covariant_components.g_12 * contravariant_components.g12 - + covariant_components.g_22 * contravariant_components.g22 - + covariant_components.g_23 * contravariant_components.g23) - - 1)), - max(abs((covariant_components.g_13 * contravariant_components.g13 - + covariant_components.g_23 * contravariant_components.g23 - + covariant_components.g_33 * contravariant_components.g33) - - 1))); + BoutReal maxerr; + maxerr = BOUTMAX(max(abs((covariant_components.g_11 * contravariant_components.g11 + + covariant_components.g_12 * contravariant_components.g12 + + covariant_components.g_13 * contravariant_components.g13) + - 1)), + max(abs((covariant_components.g_12 * contravariant_components.g12 + + covariant_components.g_22 * contravariant_components.g22 + + covariant_components.g_23 * contravariant_components.g23) + - 1)), + max(abs((covariant_components.g_13 * contravariant_components.g13 + + covariant_components.g_23 * contravariant_components.g23 + + covariant_components.g_33 * contravariant_components.g33) + - 1))); - output_info.write("\tMaximum error in diagonal inversion is {:e}\n", maxerr); + output_info.write("\tMaximum error in diagonal inversion is {:e}\n", maxerr); maxerr = BOUTMAX(max(abs(covariant_components.g_11 * contravariant_components.g12 - + covariant_components.g_12 * contravariant_components.g22 - + covariant_components.g_13 * contravariant_components.g23)), - max(abs(covariant_components.g_11 * contravariant_components.g13 - + covariant_components.g_12 * contravariant_components.g23 - + covariant_components.g_13 * contravariant_components.g33)), - max(abs(covariant_components.g_12 * contravariant_components.g13 - + covariant_components.g_22 * contravariant_components.g23 - + covariant_components.g_23 * contravariant_components.g33))); - - output_info.write("\tMaximum error in off-diagonal inversion is {:e}\n", maxerr); - return 0; - } + + covariant_components.g_12 * contravariant_components.g22 + + covariant_components.g_13 * contravariant_components.g23)), + max(abs(covariant_components.g_11 * contravariant_components.g13 + + covariant_components.g_12 * contravariant_components.g23 + + covariant_components.g_13 * contravariant_components.g33)), + max(abs(covariant_components.g_12 * contravariant_components.g13 + + covariant_components.g_22 * contravariant_components.g23 + + covariant_components.g_23 * contravariant_components.g33))); + + output_info.write("\tMaximum error in off-diagonal inversion is {:e}\n", maxerr); + return 0; } void CovariantMetricTensor::checkCovariant(int ystart) { From 840bd99ca55e39e866707385f4223f083f05a0c0 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 27 Sep 2023 09:33:11 +0100 Subject: [PATCH 052/491] Fix calcCovariant and checkContravariant methods. --- include/bout/ContravariantMetricTensor.cpp | 92 ++++++++++++---------- include/bout/ContravariantMetricTensor.h | 7 +- include/bout/CovariantMetricTensor.cpp | 13 ++- include/bout/CovariantMetricTensor.h | 2 + src/mesh/coordinates.cxx | 8 +- 5 files changed, 71 insertions(+), 51 deletions(-) diff --git a/include/bout/ContravariantMetricTensor.cpp b/include/bout/ContravariantMetricTensor.cpp index f9b67b633a..7fdc35a001 100644 --- a/include/bout/ContravariantMetricTensor.cpp +++ b/include/bout/ContravariantMetricTensor.cpp @@ -1,11 +1,12 @@ #include "ContravariantMetricTensor.h" +#include "CovariantMetricTensor.h" ContravariantMetricTensor::ContravariantMetricTensor( const FieldMetric g11, const FieldMetric g22, const FieldMetric g33, const FieldMetric g12, const FieldMetric g13, const FieldMetric g23) { contravariant_components = {g11, g22, g33, g12, g13, g23}; - Allocate(); // Make sure metric elements are allocated // ; TODO: Required? + Allocate(); // Make sure metric elements are allocated // ; TODO: Required? } ContravariantMetricTensor::ContravariantComponents @@ -17,7 +18,7 @@ ContravariantMetricTensor::getContravariantMetricTensor() const { } void ContravariantMetricTensor::setContravariantMetricTensor( - const ContravariantMetricTensor& metric_tensor) { + CELL_LOC location, const ContravariantMetricTensor& metric_tensor) { const auto new_components = metric_tensor.getContravariantMetricTensor(); contravariant_components.g11 = new_components.g11; @@ -26,80 +27,85 @@ void ContravariantMetricTensor::setContravariantMetricTensor( contravariant_components.g12 = new_components.g12; contravariant_components.g13 = new_components.g13; contravariant_components.g23 = new_components.g23; - calcCovariant(); + calcCovariant(location); } -int ContravariantMetricTensor::calcCovariant(const std::string& region) { +int ContravariantMetricTensor::calcCovariant(const CELL_LOC location, + std::string& region) { TRACE("ContravariantMetricTensor::calcCovariant"); - // Make sure metric elements are allocated - g_11.allocate(); - g_22.allocate(); - g_33.allocate(); - g_12.allocate(); - g_13.allocate(); - g_23.allocate(); - - g_11.setLocation(location); - g_22.setLocation(location); - g_33.setLocation(location); - g_12.setLocation(location); - g_13.setLocation(location); - g_23.setLocation(location); - // Perform inversion of g^{ij} to get g_{ij} // NOTE: Currently this bit assumes that metric terms are Field2D objects auto a = Matrix(3, 3); - BOUT_FOR_SERIAL(i, g11.getRegion(region)) { - a(0, 0) = g11[i]; - a(1, 1) = g22[i]; - a(2, 2) = g33[i]; + BOUT_FOR_SERIAL(i, contravariant_components.g11.getRegion(region)) { + a(0, 0) = contravariant_components.g11[i]; + a(1, 1) = contravariant_components.g22[i]; + a(2, 2) = contravariant_components.g33[i]; - a(0, 1) = a(1, 0) = g12[i]; - a(1, 2) = a(2, 1) = g23[i]; - a(0, 2) = a(2, 0) = g13[i]; + a(0, 1) = a(1, 0) = contravariant_components.g12[i]; + a(1, 2) = a(2, 1) = contravariant_components.g23[i]; + a(0, 2) = a(2, 0) = contravariant_components.g13[i]; if (invert3x3(a)) { output_error.write("\tERROR: metric tensor is singular at ({:d}, {:d})\n", i.x(), i.y()); return 1; } + } - g_11[i] = a(0, 0); - g_22[i] = a(1, 1); - g_33[i] = a(2, 2); + CovariantMetricTensor const covariantMetricTensor = + CovariantMetricTensor(a(0, 0), a(1, 1), a(2, 2), a(0, 1), a(0, 2), a(1, 2)); - g_12[i] = a(0, 1); - g_13[i] = a(0, 2); - g_23[i] = a(1, 2); - } + auto const covariant_components = covariantMetricTensor.getCovariantMetricTensor(); + + covariant_components.g_11.setLocation(location); + covariant_components.g_22.setLocation(location); + covariant_components.g_33.setLocation(location); + covariant_components.g_12.setLocation(location); + covariant_components.g_13.setLocation(location); + covariant_components.g_23.setLocation(location); BoutReal maxerr; - maxerr = BOUTMAX(max(abs((g_11 * g11 + g_12 * g12 + g_13 * g13) - 1)), - max(abs((g_12 * g12 + g_22 * g22 + g_23 * g23) - 1)), - max(abs((g_13 * g13 + g_23 * g23 + g_33 * g33) - 1))); + maxerr = BOUTMAX(max(abs((covariant_components.g_11 * contravariant_components.g11 + + covariant_components.g_12 * contravariant_components.g12 + + covariant_components.g_13 * contravariant_components.g13) + - 1)), + max(abs((covariant_components.g_12 * contravariant_components.g12 + + covariant_components.g_22 * contravariant_components.g22 + + covariant_components.g_23 * contravariant_components.g23) + - 1)), + max(abs((covariant_components.g_13 * contravariant_components.g13 + + covariant_components.g_23 * contravariant_components.g23 + + covariant_components.g_33 * contravariant_components.g33) + - 1))); output_info.write("\tLocal maximum error in diagonal inversion is {:e}\n", maxerr); - maxerr = BOUTMAX(max(abs(g_11 * g12 + g_12 * g22 + g_13 * g23)), - max(abs(g_11 * g13 + g_12 * g23 + g_13 * g33)), - max(abs(g_12 * g13 + g_22 * g23 + g_23 * g33))); + maxerr = BOUTMAX(max(abs(covariant_components.g_11 * contravariant_components.g12 + + covariant_components.g_12 * contravariant_components.g22 + + covariant_components.g_13 * contravariant_components.g23)), + max(abs(covariant_components.g_11 * contravariant_components.g13 + + covariant_components.g_12 * contravariant_components.g23 + + covariant_components.g_13 * contravariant_components.g33)), + max(abs(covariant_components.g_12 * contravariant_components.g13 + + covariant_components.g_22 * contravariant_components.g23 + + covariant_components.g_23 * contravariant_components.g33))); output_info.write("\tLocal maximum error in off-diagonal inversion is {:e}\n", maxerr); return 0; } -void ContravariantMetricTensor::checkContravariant() { +void ContravariantMetricTensor::checkContravariant(int ystart) { // Diagonal metric components should be finite bout::checkFinite(contravariant_components.g11, "g11", "RGN_NOCORNERS"); bout::checkFinite(contravariant_components.g22, "g22", "RGN_NOCORNERS"); bout::checkFinite(contravariant_components.g33, "g33", "RGN_NOCORNERS"); if (contravariant_components.g11.hasParallelSlices() && &contravariant_components.g11.ynext(1) != &contravariant_components.g11) { - for (int dy = 1; dy <= localmesh->ystart; ++dy) { + for (int dy = 1; dy <= ystart; ++dy) { for (const auto sign : {1, -1}) { bout::checkFinite(contravariant_components.g11.ynext(sign * dy), "g11.ynext", fmt::format("RGN_YPAR_{:+d}", sign * dy)); @@ -116,7 +122,7 @@ void ContravariantMetricTensor::checkContravariant() { bout::checkPositive(contravariant_components.g33, "g33", "RGN_NOCORNERS"); if (contravariant_components.g11.hasParallelSlices() && &contravariant_components.g11.ynext(1) != &contravariant_components.g11) { - for (int dy = 1; dy <= localmesh->ystart; ++dy) { + for (int dy = 1; dy <= ystart; ++dy) { for (const auto sign : {1, -1}) { bout::checkPositive(contravariant_components.g11.ynext(sign * dy), "g11.ynext", fmt::format("RGN_YPAR_{:+d}", sign * dy)); @@ -134,7 +140,7 @@ void ContravariantMetricTensor::checkContravariant() { bout::checkFinite(contravariant_components.g23, "g23", "RGN_NOCORNERS"); if (contravariant_components.g23.hasParallelSlices() && &contravariant_components.g23.ynext(1) != &contravariant_components.g23) { - for (int dy = 1; dy <= localmesh->ystart; ++dy) { + for (int dy = 1; dy <= ystart; ++dy) { for (const auto sign : {1, -1}) { bout::checkFinite(contravariant_components.g12.ynext(sign * dy), "g12.ynext", fmt::format("RGN_YPAR_{:+d}", sign * dy)); diff --git a/include/bout/ContravariantMetricTensor.h b/include/bout/ContravariantMetricTensor.h index d77953aeb7..fb9bd50dc2 100644 --- a/include/bout/ContravariantMetricTensor.h +++ b/include/bout/ContravariantMetricTensor.h @@ -26,12 +26,13 @@ class ContravariantMetricTensor { const FieldMetric g13, const FieldMetric g23); /// Invert contravariant metric to get covariant components - int calcCovariant(const std::string& region = "RGN_ALL"); + int calcCovariant(CELL_LOC location, std::string& region = (std::string&)"RGN_ALL"); // check that contravariant tensors are positive (if expected) and finite (always) - void checkContravariant(); + void checkContravariant(int ystart); - void setContravariantMetricTensor(const ContravariantMetricTensor& metric_tensor); + void setContravariantMetricTensor(CELL_LOC location, + const ContravariantMetricTensor& metric_tensor); ContravariantComponents getContravariantMetricTensor() const; diff --git a/include/bout/CovariantMetricTensor.cpp b/include/bout/CovariantMetricTensor.cpp index ca3f787053..951be1101f 100644 --- a/include/bout/CovariantMetricTensor.cpp +++ b/include/bout/CovariantMetricTensor.cpp @@ -8,6 +8,7 @@ CovariantMetricTensor::CovariantMetricTensor( const FieldMetric g_11, const FieldMetric g_22, const FieldMetric g_33, const FieldMetric g_12, const FieldMetric g_13, const FieldMetric g_23) { covariant_components = {g_11, g_22, g_33, g_12, g_13, g_23}; + Allocate(); // Make sure metric elements are allocated // ; TODO: Required? } CovariantMetricTensor::CovariantComponents @@ -76,8 +77,7 @@ int CovariantMetricTensor::calcContravariant(const std::string& region) { output_info.write("\tMaximum error in diagonal inversion is {:e}\n", maxerr); - maxerr = - BOUTMAX(max(abs(covariant_components.g_11 * contravariant_components.g12 + maxerr = BOUTMAX(max(abs(covariant_components.g_11 * contravariant_components.g12 + covariant_components.g_12 * contravariant_components.g22 + covariant_components.g_13 * contravariant_components.g23)), max(abs(covariant_components.g_11 * contravariant_components.g13 @@ -145,3 +145,12 @@ void CovariantMetricTensor::checkCovariant(int ystart) { } } } + +void CovariantMetricTensor::Allocate() { // ; TODO: Required? + covariant_components.g_11.allocate(); + covariant_components.g_22.allocate(); + covariant_components.g_33.allocate(); + covariant_components.g_12.allocate(); + covariant_components.g_13.allocate(); + covariant_components.g_23.allocate(); +} diff --git a/include/bout/CovariantMetricTensor.h b/include/bout/CovariantMetricTensor.h index f2b6d3139c..fd20a8922f 100644 --- a/include/bout/CovariantMetricTensor.h +++ b/include/bout/CovariantMetricTensor.h @@ -32,6 +32,8 @@ class CovariantMetricTensor { CovariantComponents getCovariantMetricTensor() const; + void Allocate(); + private: CovariantComponents covariant_components; }; diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index d36b2ea044..de103032da 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -1216,8 +1216,10 @@ void Coordinates::CalculateChristoffelSymbols() { int Coordinates::calcCovariant(const std::string& region) { TRACE("Coordinates::calcCovariant"); - return contravariantMetricTensor.calcCovariant(region); + return contravariantMetricTensor.calcCovariant(location, + const_cast(region)); } + int Coordinates::calcContravariant(const std::string& region) { TRACE("Coordinates::calcContravariant"); return covariantMetricTensor.calcContravariant(); @@ -1815,11 +1817,11 @@ Coordinates::Grad2_par2_DDY_invSg(CELL_LOC outloc, const std::string& method) co void Coordinates::checkCovariant() { covariantMetricTensor.checkCovariant(localmesh->ystart); } -void Coordinates::checkContravariant() { contravariantMetricTensor.checkContravariant(); } +void Coordinates::checkContravariant() { contravariantMetricTensor.checkContravariant(localmesh->ystart); } void Coordinates::setContravariantMetricTensor( const ContravariantMetricTensor& metric_tensor) { - contravariantMetricTensor.setContravariantMetricTensor(metric_tensor); + contravariantMetricTensor.setContravariantMetricTensor(location, metric_tensor); } ContravariantMetricTensor::ContravariantComponents Coordinates::getContravariantMetricTensor() const { From ead39dc34b96beee770edef7faf6fa3516130994 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 27 Sep 2023 12:46:39 +0100 Subject: [PATCH 053/491] Fix various methods in Coordinates class, to reflect refactoring of metric tensor. --- src/mesh/coordinates.cxx | 71 +++++++++++++++++++++++++++------------- 1 file changed, 48 insertions(+), 23 deletions(-) diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index de103032da..5ee1cb152d 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -1511,8 +1511,9 @@ Coordinates::FieldMetric Coordinates::Grad2_par2(const Field2D& f, CELL_LOC outl TRACE("Coordinates::Grad2_par2( Field2D )"); ASSERT1(location == outloc || (outloc == CELL_DEFAULT && location == f.getLocation())); + auto const covariant_components = covariantMetricTensor.getCovariantMetricTensor(); auto result = Grad2_par2_DDY_invSg(outloc, method) * DDY(f, outloc, method) - + D2DY2(f, outloc, method) / g_22; + + D2DY2(f, outloc, method) / covariant_components.g_22; return result; } @@ -1527,7 +1528,8 @@ Field3D Coordinates::Grad2_par2(const Field3D& f, CELL_LOC outloc, Field3D result = ::DDY(f, outloc, method); - Field3D r2 = D2DY2(f, outloc, method) / g_22; + auto const covariant_components = covariantMetricTensor.getCovariantMetricTensor(); + Field3D r2 = D2DY2(f, outloc, method) / covariant_components.g_22; result = Grad2_par2_DDY_invSg(outloc, method) * result + r2; @@ -1546,7 +1548,9 @@ Coordinates::FieldMetric Coordinates::Delp2(const Field2D& f, CELL_LOC outloc, TRACE("Coordinates::Delp2( Field2D )"); ASSERT1(location == outloc || outloc == CELL_DEFAULT); - auto result = G1 * DDX(f, outloc) + g11 * D2DX2(f, outloc); + auto const contravariant_components = + contravariantMetricTensor.getContravariantMetricTensor(); + auto result = G1 * DDX(f, outloc) + contravariant_components.g11 * D2DX2(f, outloc); return result; } @@ -1608,8 +1612,12 @@ Field3D Coordinates::Delp2(const Field3D& f, CELL_LOC outloc, bool useFFT) { } } } else { - result = G1 * ::DDX(f, outloc) + G3 * ::DDZ(f, outloc) + g11 * ::D2DX2(f, outloc) - + g33 * ::D2DZ2(f, outloc) + 2 * g13 * ::D2DXDZ(f, outloc); + auto const contravariant_components = + contravariantMetricTensor.getContravariantMetricTensor(); + result = G1 * ::DDX(f, outloc) + G3 * ::DDZ(f, outloc) + + contravariant_components.g11 * ::D2DX2(f, outloc) + + contravariant_components.g33 * ::D2DZ2(f, outloc) + + 2 * contravariant_components.g13 * ::D2DXDZ(f, outloc); }; ASSERT2(result.getLocation() == outloc); @@ -1682,12 +1690,17 @@ FieldPerp Coordinates::Delp2(const FieldPerp& f, CELL_LOC outloc, bool useFFT) { Coordinates::FieldMetric Coordinates::Laplace_par(const Field2D& f, CELL_LOC outloc) { ASSERT1(location == outloc || outloc == CELL_DEFAULT); - return D2DY2(f, outloc) / g_22 + DDY(J / g_22, outloc) * DDY(f, outloc) / J; + + auto const covariant_components = covariantMetricTensor.getCovariantMetricTensor(); + return D2DY2(f, outloc) / covariant_components.g_22 + + DDY(J / covariant_components.g_22, outloc) * DDY(f, outloc) / J; } Field3D Coordinates::Laplace_par(const Field3D& f, CELL_LOC outloc) { ASSERT1(location == outloc || outloc == CELL_DEFAULT); - return D2DY2(f, outloc) / g_22 + DDY(J / g_22, outloc) * ::DDY(f, outloc) / J; + auto const covariant_components = covariantMetricTensor.getCovariantMetricTensor(); + return D2DY2(f, outloc) / covariant_components.g_22 + + DDY(J / covariant_components.g_22, outloc) * ::DDY(f, outloc) / J; } // Full Laplacian operator on scalar field @@ -1698,9 +1711,12 @@ Coordinates::FieldMetric Coordinates::Laplace(const Field2D& f, CELL_LOC outloc, TRACE("Coordinates::Laplace( Field2D )"); ASSERT1(location == outloc || outloc == CELL_DEFAULT); - auto result = G1 * DDX(f, outloc) + G2 * DDY(f, outloc) + g11 * D2DX2(f, outloc) - + g22 * D2DY2(f, outloc) - + 2.0 * g12 + auto const contravariant_components = + contravariantMetricTensor.getContravariantMetricTensor(); + auto result = G1 * DDX(f, outloc) + G2 * DDY(f, outloc) + + contravariant_components.g11 * D2DX2(f, outloc) + + contravariant_components.g22 * D2DY2(f, outloc) + + 2.0 * contravariant_components.g12 * D2DXDY(f, outloc, "DEFAULT", "RGN_NOBNDRY", dfdy_boundary_conditions, dfdy_dy_region); @@ -1713,14 +1729,18 @@ Field3D Coordinates::Laplace(const Field3D& f, CELL_LOC outloc, TRACE("Coordinates::Laplace( Field3D )"); ASSERT1(location == outloc || outloc == CELL_DEFAULT); + auto const contravariant_components = + contravariantMetricTensor.getContravariantMetricTensor(); Field3D result = G1 * ::DDX(f, outloc) + G2 * ::DDY(f, outloc) + G3 * ::DDZ(f, outloc) - + g11 * D2DX2(f, outloc) + g22 * D2DY2(f, outloc) - + g33 * D2DZ2(f, outloc) + + contravariant_components.g11 * D2DX2(f, outloc) + + contravariant_components.g22 * D2DY2(f, outloc) + + contravariant_components.g33 * D2DZ2(f, outloc) + 2.0 - * (g12 + * (contravariant_components.g12 * D2DXDY(f, outloc, "DEFAULT", "RGN_NOBNDRY", dfdy_boundary_conditions, dfdy_dy_region) - + g13 * D2DXDZ(f, outloc) + g23 * D2DYDZ(f, outloc)); + + contravariant_components.g13 * D2DXDZ(f, outloc) + + contravariant_components.g23 * D2DYDZ(f, outloc)); return result; } @@ -1740,7 +1760,8 @@ Field2D Coordinates::Laplace_perpXY([[maybe_unused]] const Field2D& A, const auto outer_x_avg = [&i](const auto& f) { return 0.5 * (f[i] + f[i.xp()]); }; const BoutReal outer_x_A = outer_x_avg(A); const BoutReal outer_x_J = outer_x_avg(J); - const BoutReal outer_x_g11 = outer_x_avg(g11); + const BoutReal outer_x_g11 = + outer_x_avg(contravariantMetricTensor.getContravariantMetricTensor().g11); const BoutReal outer_x_dx = outer_x_avg(dx); const BoutReal outer_x_value = outer_x_A * outer_x_J * outer_x_g11 / (J[i] * outer_x_dx * dx[i]); @@ -1750,7 +1771,8 @@ Field2D Coordinates::Laplace_perpXY([[maybe_unused]] const Field2D& A, const auto inner_x_avg = [&i](const auto& f) { return 0.5 * (f[i] + f[i.xm()]); }; const BoutReal inner_x_A = inner_x_avg(A); const BoutReal inner_x_J = inner_x_avg(J); - const BoutReal inner_x_g11 = inner_x_avg(g11); + const BoutReal inner_x_g11 = + inner_x_avg(contravariantMetricTensor.getContravariantMetricTensor().g11); const BoutReal inner_x_dx = inner_x_avg(dx); const BoutReal inner_x_value = inner_x_A * inner_x_J * inner_x_g11 / (J[i] * inner_x_dx * dx[i]); @@ -1758,11 +1780,14 @@ Field2D Coordinates::Laplace_perpXY([[maybe_unused]] const Field2D& A, // upper y boundary const auto upper_y_avg = [&i](const auto& f) { return 0.5 * (f[i] + f[i.yp()]); }; + auto const covariant_components = covariantMetricTensor.getCovariantMetricTensor(); + auto const contravariant_components = + contravariantMetricTensor.getContravariantMetricTensor(); const BoutReal upper_y_A = upper_y_avg(A); const BoutReal upper_y_J = upper_y_avg(J); - const BoutReal upper_y_g_22 = upper_y_avg(g_22); - const BoutReal upper_y_g23 = upper_y_avg(g23); - const BoutReal upper_y_g_23 = upper_y_avg(g_23); + const BoutReal upper_y_g_22 = upper_y_avg(covariant_components.g_22); + const BoutReal upper_y_g23 = upper_y_avg(contravariant_components.g23); + const BoutReal upper_y_g_23 = upper_y_avg(covariant_components.g_23); const BoutReal upper_y_dy = upper_y_avg(dy); const BoutReal upper_y_value = -upper_y_A * upper_y_J * upper_y_g23 * upper_y_g_23 / (upper_y_g_22 * J[i] * upper_y_dy * dy[i]); @@ -1772,9 +1797,9 @@ Field2D Coordinates::Laplace_perpXY([[maybe_unused]] const Field2D& A, const auto lower_y_avg = [&i](const auto& f) { return 0.5 * (f[i] + f[i.ym()]); }; const BoutReal lower_y_A = lower_y_avg(A); const BoutReal lower_y_J = lower_y_avg(J); - const BoutReal lower_y_g_22 = lower_y_avg(g_22); - const BoutReal lower_y_g23 = lower_y_avg(g23); - const BoutReal lower_y_g_23 = lower_y_avg(g_23); + const BoutReal lower_y_g_22 = lower_y_avg(covariant_components.g_22); + const BoutReal lower_y_g23 = lower_y_avg(contravariant_components.g23); + const BoutReal lower_y_g_23 = lower_y_avg(covariant_components.g_23); const BoutReal lower_y_dy = lower_y_avg(dy); const BoutReal lower_y_value = -lower_y_A * lower_y_J * lower_y_g23 * lower_y_g_23 / (lower_y_g_22 * J[i] * lower_y_dy * dy[i]); @@ -1790,7 +1815,7 @@ Field2D Coordinates::Laplace_perpXY([[maybe_unused]] const Field2D& A, const Coordinates::FieldMetric& Coordinates::invSg() const { if (invSgCache == nullptr) { auto ptr = std::make_unique(); - (*ptr) = 1.0 / sqrt(g_22); + (*ptr) = 1.0 / sqrt(covariantMetricTensor.getCovariantMetricTensor().g_22); invSgCache = std::move(ptr); } return *invSgCache; From 759790bb8ea37eeac00ee1d0c03ac43a98b7f368 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 27 Sep 2023 12:47:09 +0100 Subject: [PATCH 054/491] Formatting. --- src/mesh/coordinates.cxx | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 5ee1cb152d..465ae78002 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -17,9 +17,9 @@ #include -#include "bout/ContravariantMetricTensor.h" #include "parallel/fci.hxx" #include "parallel/shiftedmetricinterp.hxx" +#include "bout/ContravariantMetricTensor.h" // use anonymous namespace so this utility function is not available outside this file namespace { @@ -364,6 +364,7 @@ std::string getLocationSuffix(CELL_LOC location) { } } } + } // anonymous namespace Coordinates::Coordinates(Mesh* mesh, FieldMetric dx, FieldMetric dy, FieldMetric dz, @@ -468,6 +469,7 @@ Coordinates::Coordinates(Mesh* mesh, Options* options) }; // Diagonal components of metric tensor g^{ij} (default to 1) + g11 = getUnalignedAtLocation(g11, "g11", 1.0); g22 = getUnalignedAtLocation(g22, "g22", 1.0); g33 = getUnalignedAtLocation(g33, "g33", 1.0); @@ -1840,16 +1842,21 @@ Coordinates::Grad2_par2_DDY_invSg(CELL_LOC outloc, const std::string& method) co return *Grad2_par2_DDY_invSgCache[method]; } -void Coordinates::checkCovariant() { covariantMetricTensor.checkCovariant(localmesh->ystart); } +void Coordinates::checkCovariant() { + covariantMetricTensor.checkCovariant(localmesh->ystart); +} -void Coordinates::checkContravariant() { contravariantMetricTensor.checkContravariant(localmesh->ystart); } +void Coordinates::checkContravariant() { + contravariantMetricTensor.checkContravariant(localmesh->ystart); +} void Coordinates::setContravariantMetricTensor( const ContravariantMetricTensor& metric_tensor) { contravariantMetricTensor.setContravariantMetricTensor(location, metric_tensor); } -ContravariantMetricTensor::ContravariantComponents Coordinates::getContravariantMetricTensor() const { +ContravariantMetricTensor::ContravariantComponents +Coordinates::getContravariantMetricTensor() const { return contravariantMetricTensor.getContravariantMetricTensor(); } From 7f90eb3ca340274e0ee6d044634b3fb4926bbb8a Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 27 Sep 2023 13:02:00 +0100 Subject: [PATCH 055/491] Fix Coordinates::outputVars method, to reflect refactoring of metric tensor. --- src/mesh/coordinates.cxx | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 465ae78002..7510032637 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -904,19 +904,23 @@ void Coordinates::outputVars(Options& output_options) { output_options["dy" + loc_string].force(dy, "Coordinates"); output_options["dz" + loc_string].force(dz, "Coordinates"); - output_options["g11" + loc_string].force(g11, "Coordinates"); - output_options["g22" + loc_string].force(g22, "Coordinates"); - output_options["g33" + loc_string].force(g33, "Coordinates"); - output_options["g12" + loc_string].force(g12, "Coordinates"); - output_options["g13" + loc_string].force(g13, "Coordinates"); - output_options["g23" + loc_string].force(g23, "Coordinates"); - - output_options["g_11" + loc_string].force(g_11, "Coordinates"); - output_options["g_22" + loc_string].force(g_22, "Coordinates"); - output_options["g_33" + loc_string].force(g_33, "Coordinates"); - output_options["g_12" + loc_string].force(g_12, "Coordinates"); - output_options["g_13" + loc_string].force(g_13, "Coordinates"); - output_options["g_23" + loc_string].force(g_23, "Coordinates"); + auto const contravariant_components = + contravariantMetricTensor.getContravariantMetricTensor(); + auto const covariant_components = covariantMetricTensor.getCovariantMetricTensor(); + + output_options["g11" + loc_string].force(contravariant_components.g11, "Coordinates"); + output_options["g22" + loc_string].force(contravariant_components.g22, "Coordinates"); + output_options["g33" + loc_string].force(contravariant_components.g33, "Coordinates"); + output_options["g12" + loc_string].force(contravariant_components.g12, "Coordinates"); + output_options["g13" + loc_string].force(contravariant_components.g13, "Coordinates"); + output_options["g23" + loc_string].force(contravariant_components.g23, "Coordinates"); + + output_options["g_11" + loc_string].force(covariant_components.g_11, "Coordinates"); + output_options["g_22" + loc_string].force(covariant_components.g_22, "Coordinates"); + output_options["g_33" + loc_string].force(covariant_components.g_33, "Coordinates"); + output_options["g_12" + loc_string].force(covariant_components.g_12, "Coordinates"); + output_options["g_13" + loc_string].force(covariant_components.g_13, "Coordinates"); + output_options["g_23" + loc_string].force(covariant_components.g_23, "Coordinates"); output_options["J" + loc_string].force(J, "Coordinates"); output_options["Bxy" + loc_string].force(Bxy, "Coordinates"); From 59aaa0709c9eaf5b2132c437fa430f0c742cf08f Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 27 Sep 2023 13:02:27 +0100 Subject: [PATCH 056/491] Fix Coordinates::geometry method, to reflect refactoring of metric tensor. --- src/mesh/coordinates.cxx | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 7510032637..a57d46704b 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -950,8 +950,17 @@ const Field2D& Coordinates::zlength() const { int Coordinates::geometry(bool recalculate_staggered, bool force_interpolate_from_centre) { TRACE("Coordinates::geometry"); - communicate(dx, dy, dz, g11, g22, g33, g12, g13, g23, g_11, g_22, g_33, g_12, g_13, - g_23, J, Bxy); + + auto const contravariant_components = + contravariantMetricTensor.getContravariantMetricTensor(); + auto const covariant_components = covariantMetricTensor.getCovariantMetricTensor(); + + communicate(dx, dy, dz, contravariant_components.g11, contravariant_components.g22, + contravariant_components.g33, contravariant_components.g12, + contravariant_components.g13, contravariant_components.g23, + covariant_components.g_11, covariant_components.g_22, + covariant_components.g_33, covariant_components.g_12, + covariant_components.g_13, covariant_components.g_23, J, Bxy); output_progress.write("Calculating differential geometry terms\n"); @@ -972,15 +981,21 @@ int Coordinates::geometry(bool recalculate_staggered, checkCovariant(); CalculateChristoffelSymbols(); - auto tmp = J * g12; + auto tmp = J * contravariant_components.g12; communicate(tmp); - G1 = (DDX(J * g11) + DDY(tmp) + DDZ(J * g13)) / J; - tmp = J * g22; + G1 = (DDX(J * contravariant_components.g11) + DDY(tmp) + + DDZ(J * contravariant_components.g13)) + / J; + tmp = J * contravariant_components.g22; communicate(tmp); - G2 = (DDX(J * g12) + DDY(tmp) + DDZ(J * g23)) / J; - tmp = J * g23; + G2 = (DDX(J * contravariant_components.g12) + DDY(tmp) + + DDZ(J * contravariant_components.g23)) + / J; + tmp = J * contravariant_components.g23; communicate(tmp); - G3 = (DDX(J * g13) + DDY(tmp) + DDZ(J * g33)) / J; + G3 = (DDX(J * contravariant_components.g13) + DDY(tmp) + + DDZ(J * contravariant_components.g33)) + / J; // Communicate christoffel symbol terms output_progress.write("\tCommunicating connection terms\n"); From 9f684dee23fdd5021da3f73e3c2da356a66041e7 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 27 Sep 2023 13:12:19 +0100 Subject: [PATCH 057/491] Fix Coordinates::CalculateChristoffelSymbols method, to reflect refactoring of metric tensor. --- src/mesh/coordinates.cxx | 154 +++++++++++++++++++++++++++------------ 1 file changed, 107 insertions(+), 47 deletions(-) diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index a57d46704b..168d993dcc 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -1176,63 +1176,123 @@ void Coordinates::CalculateChristoffelSymbols() { // Note: This calculation is completely general: metric // tensor can be 2D or 3D. For 2D, all DDZ terms are zero - G1_11 = 0.5 * g11 * DDX(g_11) + g12 * (DDX(g_12) - 0.5 * DDY(g_11)) - + g13 * (DDX(g_13) - 0.5 * DDZ(g_11)); - G1_22 = g11 * (DDY(g_12) - 0.5 * DDX(g_22)) + 0.5 * g12 * DDY(g_22) - + g13 * (DDY(g_23) - 0.5 * DDZ(g_22)); - G1_33 = g11 * (DDZ(g_13) - 0.5 * DDX(g_33)) + g12 * (DDZ(g_23) - 0.5 * DDY(g_33)) - + 0.5 * g13 * DDZ(g_33); - G1_12 = 0.5 * g11 * DDY(g_11) + 0.5 * g12 * DDX(g_22) - + 0.5 * g13 * (DDY(g_13) + DDX(g_23) - DDZ(g_12)); - G1_13 = 0.5 * g11 * DDZ(g_11) + 0.5 * g12 * (DDZ(g_12) + DDX(g_23) - DDY(g_13)) - + 0.5 * g13 * DDX(g_33); - G1_23 = 0.5 * g11 * (DDZ(g_12) + DDY(g_13) - DDX(g_23)) - + 0.5 * g12 * (DDZ(g_22) + DDY(g_23) - DDY(g_23)) + auto const contravariant_components = + contravariantMetricTensor.getContravariantMetricTensor(); + auto const covariant_components = covariantMetricTensor.getCovariantMetricTensor(); + + G1_11 = 0.5 * contravariant_components.g11 * DDX(covariant_components.g_11) + + contravariant_components.g12 + * (DDX(covariant_components.g_12) - 0.5 * DDY(covariant_components.g_11)) + + contravariant_components.g13 + * (DDX(covariant_components.g_13) - 0.5 * DDZ(covariant_components.g_11)); + G1_22 = contravariant_components.g11 + * (DDY(covariant_components.g_12) - 0.5 * DDX(covariant_components.g_22)) + + 0.5 * contravariant_components.g12 * DDY(covariant_components.g_22) + + contravariant_components.g13 + * (DDY(covariant_components.g_23) - 0.5 * DDZ(covariant_components.g_22)); + G1_33 = contravariant_components.g11 + * (DDZ(covariant_components.g_13) - 0.5 * DDX(covariant_components.g_33)) + + contravariant_components.g12 + * (DDZ(covariant_components.g_23) - 0.5 * DDY(covariant_components.g_33)) + + 0.5 * contravariant_components.g13 * DDZ(covariant_components.g_33); + G1_12 = 0.5 * contravariant_components.g11 * DDY(covariant_components.g_11) + + 0.5 * contravariant_components.g12 * DDX(covariant_components.g_22) + + 0.5 * contravariant_components.g13 + * (DDY(covariant_components.g_13) + DDX(covariant_components.g_23) + - DDZ(covariant_components.g_12)); + G1_13 = 0.5 * contravariant_components.g11 * DDZ(covariant_components.g_11) + + 0.5 * contravariant_components.g12 + * (DDZ(covariant_components.g_12) + DDX(covariant_components.g_23) + - DDY(covariant_components.g_13)) + + 0.5 * contravariant_components.g13 * DDX(covariant_components.g_33); + G1_23 = 0.5 * contravariant_components.g11 + * (DDZ(covariant_components.g_12) + DDY(covariant_components.g_13) + - DDX(covariant_components.g_23)) + + 0.5 * contravariant_components.g12 + * (DDZ(covariant_components.g_22) + DDY(covariant_components.g_23) + - DDY(covariant_components.g_23)) // + 0.5 *g13*(DDZ(g_32) + DDY(g_33) - DDZ(g_23)); // which equals - + 0.5 * g13 * DDY(g_33); - - G2_11 = 0.5 * g12 * DDX(g_11) + g22 * (DDX(g_12) - 0.5 * DDY(g_11)) - + g23 * (DDX(g_13) - 0.5 * DDZ(g_11)); - G2_22 = g12 * (DDY(g_12) - 0.5 * DDX(g_22)) + 0.5 * g22 * DDY(g_22) - + g23 * (DDY(g23) - 0.5 * DDZ(g_22)); - G2_33 = g12 * (DDZ(g_13) - 0.5 * DDX(g_33)) + g22 * (DDZ(g_23) - 0.5 * DDY(g_33)) - + 0.5 * g23 * DDZ(g_33); - G2_12 = 0.5 * g12 * DDY(g_11) + 0.5 * g22 * DDX(g_22) - + 0.5 * g23 * (DDY(g_13) + DDX(g_23) - DDZ(g_12)); + + 0.5 * contravariant_components.g13 * DDY(covariant_components.g_33); + + G2_11 = 0.5 * contravariant_components.g12 * DDX(covariant_components.g_11) + + contravariant_components.g22 + * (DDX(covariant_components.g_12) - 0.5 * DDY(covariant_components.g_11)) + + contravariant_components.g23 + * (DDX(covariant_components.g_13) - 0.5 * DDZ(covariant_components.g_11)); + G2_22 = + contravariant_components.g12 + * (DDY(covariant_components.g_12) - 0.5 * DDX(covariant_components.g_22)) + + 0.5 * contravariant_components.g22 * DDY(covariant_components.g_22) + + contravariant_components.g23 + * (DDY(contravariant_components.g23) - 0.5 * DDZ(covariant_components.g_22)); + G2_33 = contravariant_components.g12 + * (DDZ(covariant_components.g_13) - 0.5 * DDX(covariant_components.g_33)) + + contravariant_components.g22 + * (DDZ(covariant_components.g_23) - 0.5 * DDY(covariant_components.g_33)) + + 0.5 * contravariant_components.g23 * DDZ(covariant_components.g_33); + G2_12 = 0.5 * contravariant_components.g12 * DDY(covariant_components.g_11) + + 0.5 * contravariant_components.g22 * DDX(covariant_components.g_22) + + 0.5 * contravariant_components.g23 + * (DDY(covariant_components.g_13) + DDX(covariant_components.g_23) + - DDZ(covariant_components.g_12)); G2_13 = - // 0.5 *g21*(DDZ(g_11) + DDX(g_13) - DDX(g_13)) + // 0.5 *g21*(DDZ(covariant_components.g_11) + DDX(covariant_components.g_13) - DDX(covariant_components.g_13)) // which equals - 0.5 * g12 * (DDZ(g_11) + DDX(g_13) - DDX(g_13)) - // + 0.5 *g22*(DDZ(g_21) + DDX(g_23) - DDY(g_13)) + 0.5 * contravariant_components.g12 + * (DDZ(covariant_components.g_11) + DDX(covariant_components.g_13) + - DDX(covariant_components.g_13)) + // + 0.5 *g22*(DDZ(covariant_components.g_21) + DDX(covariant_components.g_23) - DDY(covariant_components.g_13)) // which equals - + 0.5 * g22 * (DDZ(g_12) + DDX(g_23) - DDY(g_13)) - // + 0.5 *g23*(DDZ(g_31) + DDX(g_33) - DDZ(g_13)); + + 0.5 * contravariant_components.g22 + * (DDZ(covariant_components.g_12) + DDX(covariant_components.g_23) + - DDY(covariant_components.g_13)) + // + 0.5 *g23*(DDZ(covariant_components.g_31) + DDX(covariant_components.g_33) - DDZ(g_13)); // which equals - + 0.5 * g23 * DDX(g_33); - G2_23 = 0.5 * g12 * (DDZ(g_12) + DDY(g_13) - DDX(g_23)) + 0.5 * g22 * DDZ(g_22) - + 0.5 * g23 * DDY(g_33); - - G3_11 = 0.5 * g13 * DDX(g_11) + g23 * (DDX(g_12) - 0.5 * DDY(g_11)) - + g33 * (DDX(g_13) - 0.5 * DDZ(g_11)); - G3_22 = g13 * (DDY(g_12) - 0.5 * DDX(g_22)) + 0.5 * g23 * DDY(g_22) - + g33 * (DDY(g_23) - 0.5 * DDZ(g_22)); - G3_33 = g13 * (DDZ(g_13) - 0.5 * DDX(g_33)) + g23 * (DDZ(g_23) - 0.5 * DDY(g_33)) - + 0.5 * g33 * DDZ(g_33); + + 0.5 * contravariant_components.g23 * DDX(covariant_components.g_33); + G2_23 = 0.5 * contravariant_components.g12 + * (DDZ(covariant_components.g_12) + DDY(covariant_components.g_13) + - DDX(covariant_components.g_23)) + + 0.5 * contravariant_components.g22 * DDZ(covariant_components.g_22) + + 0.5 * contravariant_components.g23 * DDY(covariant_components.g_33); + + G3_11 = 0.5 * contravariant_components.g13 * DDX(covariant_components.g_11) + + contravariant_components.g23 + * (DDX(covariant_components.g_12) - 0.5 * DDY(covariant_components.g_11)) + + contravariant_components.g33 + * (DDX(covariant_components.g_13) - 0.5 * DDZ(covariant_components.g_11)); + G3_22 = contravariant_components.g13 + * (DDY(covariant_components.g_12) - 0.5 * DDX(covariant_components.g_22)) + + 0.5 * contravariant_components.g23 * DDY(covariant_components.g_22) + + contravariant_components.g33 + * (DDY(covariant_components.g_23) - 0.5 * DDZ(covariant_components.g_22)); + G3_33 = contravariant_components.g13 + * (DDZ(covariant_components.g_13) - 0.5 * DDX(covariant_components.g_33)) + + contravariant_components.g23 + * (DDZ(covariant_components.g_23) - 0.5 * DDY(covariant_components.g_33)) + + 0.5 * contravariant_components.g33 * DDZ(covariant_components.g_33); G3_12 = - // 0.5 *g31*(DDY(g_11) + DDX(g_12) - DDX(g_12)) + // 0.5 *g31*(DDY(covariant_components.g_11) + DDX(covariant_components.g_12) - DDX(covariant_components.g_12)) // which equals to - 0.5 * g13 * DDY(g_11) - // + 0.5 *g32*(DDY(g_21) + DDX(g_22) - DDY(g_12)) + 0.5 * contravariant_components.g13 * DDY(covariant_components.g_11) + // + 0.5 *g32*(DDY(covariant_components.g_21) + DDX(covariant_components.g_22) - DDY(covariant_components.g_12)) // which equals to - + 0.5 * g23 * DDX(g_22) - //+ 0.5 *g33*(DDY(g_31) + DDX(g_32) - DDZ(g_12)); + + 0.5 * contravariant_components.g23 * DDX(covariant_components.g_22) + //+ 0.5 *g33*(DDY(covariant_components.g_31) + DDX(covariant_components.g_32) - DDZ(covariant_components.g_12)); // which equals to - + 0.5 * g33 * (DDY(g_13) + DDX(g_23) - DDZ(g_12)); - G3_13 = 0.5 * g13 * DDZ(g_11) + 0.5 * g23 * (DDZ(g_12) + DDX(g_23) - DDY(g_13)) - + 0.5 * g33 * DDX(g_33); - G3_23 = 0.5 * g13 * (DDZ(g_12) + DDY(g_13) - DDX(g_23)) + 0.5 * g23 * DDZ(g_22) - + 0.5 * g33 * DDY(g_33); + + 0.5 * contravariant_components.g33 + * (DDY(covariant_components.g_13) + DDX(covariant_components.g_23) + - DDZ(covariant_components.g_12)); + G3_13 = 0.5 * contravariant_components.g13 * DDZ(covariant_components.g_11) + + 0.5 * contravariant_components.g23 + * (DDZ(covariant_components.g_12) + DDX(covariant_components.g_23) + - DDY(covariant_components.g_13)) + + 0.5 * contravariant_components.g33 * DDX(covariant_components.g_33); + G3_23 = 0.5 * contravariant_components.g13 + * (DDZ(covariant_components.g_12) + DDY(covariant_components.g_13) + - DDX(covariant_components.g_23)) + + 0.5 * contravariant_components.g23 * DDZ(covariant_components.g_22) + + 0.5 * contravariant_components.g33 * DDY(covariant_components.g_33); } int Coordinates::calcCovariant(const std::string& region) { From b404e798d317e3c0c62bf7587b712025e4474754 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 27 Sep 2023 13:13:49 +0100 Subject: [PATCH 058/491] Fix Coordinates::jacobian method, to reflect refactoring of metric tensor. --- src/mesh/coordinates.cxx | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 168d993dcc..8b18c8298c 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -1313,8 +1313,18 @@ int Coordinates::jacobian() { const bool extrapolate_x = not localmesh->sourceHasXBoundaryGuards(); const bool extrapolate_y = not localmesh->sourceHasYBoundaryGuards(); - auto g = g11 * g22 * g33 + 2.0 * g12 * g13 * g23 - g11 * g23 * g23 - g22 * g13 * g13 - - g33 * g12 * g12; + auto const contravariant_components = + contravariantMetricTensor.getContravariantMetricTensor(); + auto g = contravariant_components.g11 * contravariant_components.g22 + * contravariant_components.g33 + + 2.0 * contravariant_components.g12 * contravariant_components.g13 + * contravariant_components.g23 + - contravariant_components.g11 * contravariant_components.g23 + * contravariant_components.g23 + - contravariant_components.g22 * contravariant_components.g13 + * contravariant_components.g13 + - contravariant_components.g33 * contravariant_components.g12 + * contravariant_components.g12; // Check that g is positive bout::checkPositive(g, "The determinant of g^ij", "RGN_NOBNDRY"); @@ -1325,7 +1335,7 @@ int Coordinates::jacobian() { J = interpolateAndExtrapolate(J, location, extrapolate_x, extrapolate_y, false, transform.get()); - Bxy = sqrt(g_22) / J; + Bxy = sqrt(covariantMetricTensor.getCovariantMetricTensor().g_22) / J; Bxy = interpolateAndExtrapolate(Bxy, location, extrapolate_x, extrapolate_y, false, transform.get()); From 60a78f9a64f644b19067677138d3e8fc51fe18f3 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 27 Sep 2023 13:32:16 +0100 Subject: [PATCH 059/491] Fix Coordinates constructor. --- include/bout/ContravariantMetricTensor.cpp | 7 +++++-- include/bout/CovariantMetricTensor.cpp | 7 +++++-- src/mesh/coordinates.cxx | 10 ++++------ 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/include/bout/ContravariantMetricTensor.cpp b/include/bout/ContravariantMetricTensor.cpp index 7fdc35a001..9c0854ce6d 100644 --- a/include/bout/ContravariantMetricTensor.cpp +++ b/include/bout/ContravariantMetricTensor.cpp @@ -4,8 +4,11 @@ ContravariantMetricTensor::ContravariantMetricTensor( const FieldMetric g11, const FieldMetric g22, const FieldMetric g33, - const FieldMetric g12, const FieldMetric g13, const FieldMetric g23) { - contravariant_components = {g11, g22, g33, g12, g13, g23}; + const FieldMetric g12, const FieldMetric g13, const FieldMetric g23) + : contravariant_components({g11(std::move(g11)), g22(std::move(g22)), + g33(std::move(g33)), g12(std::move(g12)), + g13(std::move(g13)), g23(std::move(g23))}) { + Allocate(); // Make sure metric elements are allocated // ; TODO: Required? } diff --git a/include/bout/CovariantMetricTensor.cpp b/include/bout/CovariantMetricTensor.cpp index 951be1101f..bd0e5848a1 100644 --- a/include/bout/CovariantMetricTensor.cpp +++ b/include/bout/CovariantMetricTensor.cpp @@ -6,8 +6,11 @@ CovariantMetricTensor::CovariantMetricTensor( const FieldMetric g_11, const FieldMetric g_22, const FieldMetric g_33, - const FieldMetric g_12, const FieldMetric g_13, const FieldMetric g_23) { - covariant_components = {g_11, g_22, g_33, g_12, g_13, g_23}; + const FieldMetric g_12, const FieldMetric g_13, const FieldMetric g_23) + : covariant_components({g_11(std::move(g_11)), g_22(std::move(g_22)), + g_33(std::move(g_33)), g_12(std::move(g_12)), + g_13(std::move(g_13)), g_23(std::move(g_23))}) { + Allocate(); // Make sure metric elements are allocated // ; TODO: Required? } diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 8b18c8298c..fe1262aa8a 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -375,12 +375,10 @@ Coordinates::Coordinates(Mesh* mesh, FieldMetric dx, FieldMetric dy, FieldMetric FieldMetric g_23, FieldMetric ShiftTorsion, FieldMetric IntShiftTorsion) : dx(std::move(dx)), dy(std::move(dy)), dz(dz), J(std::move(J)), Bxy(std::move(Bxy)), - g11(std::move(g11)), g22(std::move(g22)), g33(std::move(g33)), g12(std::move(g12)), - g13(std::move(g13)), g23(std::move(g23)), g_11(std::move(g_11)), - g_22(std::move(g_22)), g_33(std::move(g_33)), g_12(std::move(g_12)), - g_13(std::move(g_13)), g_23(std::move(g_23)), ShiftTorsion(std::move(ShiftTorsion)), - IntShiftTorsion(std::move(IntShiftTorsion)), nz(mesh->LocalNz), localmesh(mesh), - location(CELL_CENTRE) {} + ShiftTorsion(std::move(ShiftTorsion)), IntShiftTorsion(std::move(IntShiftTorsion)), + nz(mesh->LocalNz), localmesh(mesh), location(CELL_CENTRE), + contravariantMetricTensor(g11, g22, g33, g12, g13, g23), + covariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23) {} Coordinates::Coordinates(Mesh* mesh, Options* options) : dx(1., mesh), dy(1., mesh), dz(1., mesh), d1_dx(mesh), d1_dy(mesh), d1_dz(mesh), From b7820000cbb3108ae6607eefcb275fa0e0668656 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Mon, 2 Oct 2023 13:59:13 +0100 Subject: [PATCH 060/491] Fix other constructors. --- include/bout/ContravariantMetricTensor.cpp | 18 +++++++-- include/bout/ContravariantMetricTensor.h | 5 +++ include/bout/CovariantMetricTensor.cpp | 19 +++++++-- include/bout/CovariantMetricTensor.h | 5 +++ src/mesh/coordinates.cxx | 45 +++++++++++++--------- 5 files changed, 68 insertions(+), 24 deletions(-) diff --git a/include/bout/ContravariantMetricTensor.cpp b/include/bout/ContravariantMetricTensor.cpp index 9c0854ce6d..3446cb3c84 100644 --- a/include/bout/ContravariantMetricTensor.cpp +++ b/include/bout/ContravariantMetricTensor.cpp @@ -5,9 +5,21 @@ ContravariantMetricTensor::ContravariantMetricTensor( const FieldMetric g11, const FieldMetric g22, const FieldMetric g33, const FieldMetric g12, const FieldMetric g13, const FieldMetric g23) - : contravariant_components({g11(std::move(g11)), g22(std::move(g22)), - g33(std::move(g33)), g12(std::move(g12)), - g13(std::move(g13)), g23(std::move(g23))}) { + : contravariant_components({FieldMetric(std::move(g11)), FieldMetric(std::move(g22)), + FieldMetric(std::move(g33)), FieldMetric(std::move(g12)), + FieldMetric(std::move(g13)), + FieldMetric(std::move(g23))}) { + + Allocate(); // Make sure metric elements are allocated // ; TODO: Required? +} + +ContravariantMetricTensor::ContravariantMetricTensor( + const Array g11, const Array g22, const Array g33, + const Array g12, const Array g13, const Array g23, + Mesh* mesh) + : contravariant_components({FieldMetric(g11, mesh), FieldMetric(g22, mesh), + FieldMetric(g33, mesh), FieldMetric(g12, mesh), + FieldMetric(g13, mesh), FieldMetric(g23, mesh)}) { Allocate(); // Make sure metric elements are allocated // ; TODO: Required? } diff --git a/include/bout/ContravariantMetricTensor.h b/include/bout/ContravariantMetricTensor.h index fb9bd50dc2..dc7fcd37ad 100644 --- a/include/bout/ContravariantMetricTensor.h +++ b/include/bout/ContravariantMetricTensor.h @@ -25,6 +25,11 @@ class ContravariantMetricTensor { const FieldMetric g33, const FieldMetric g12, const FieldMetric g13, const FieldMetric g23); + ContravariantMetricTensor(const Array g11, const Array g22, + const Array g33, const Array g12, + const Array g13, const Array g23, + Mesh* mesh); + /// Invert contravariant metric to get covariant components int calcCovariant(CELL_LOC location, std::string& region = (std::string&)"RGN_ALL"); diff --git a/include/bout/CovariantMetricTensor.cpp b/include/bout/CovariantMetricTensor.cpp index bd0e5848a1..f935107691 100644 --- a/include/bout/CovariantMetricTensor.cpp +++ b/include/bout/CovariantMetricTensor.cpp @@ -7,9 +7,22 @@ CovariantMetricTensor::CovariantMetricTensor( const FieldMetric g_11, const FieldMetric g_22, const FieldMetric g_33, const FieldMetric g_12, const FieldMetric g_13, const FieldMetric g_23) - : covariant_components({g_11(std::move(g_11)), g_22(std::move(g_22)), - g_33(std::move(g_33)), g_12(std::move(g_12)), - g_13(std::move(g_13)), g_23(std::move(g_23))}) { + : covariant_components({FieldMetric(std::move(g_11)), FieldMetric(std::move(g_22)), + FieldMetric(std::move(g_33)), FieldMetric(std::move(g_12)), + FieldMetric(std::move(g_13)), FieldMetric(std::move(g_23))}) { + + Allocate(); // Make sure metric elements are allocated // ; TODO: Required? +} + +CovariantMetricTensor::CovariantMetricTensor(const Array g_11, + const Array g_22, + const Array g_33, + const Array g_12, + const Array g_13, + const Array g_23, Mesh* mesh) + : covariant_components({FieldMetric(g_11, mesh), FieldMetric(g_22, mesh), + FieldMetric(g_33, mesh), FieldMetric(g_12, mesh), + FieldMetric(g_13, mesh), FieldMetric(g_23, mesh)}) { Allocate(); // Make sure metric elements are allocated // ; TODO: Required? } diff --git a/include/bout/CovariantMetricTensor.h b/include/bout/CovariantMetricTensor.h index fd20a8922f..aa7820349a 100644 --- a/include/bout/CovariantMetricTensor.h +++ b/include/bout/CovariantMetricTensor.h @@ -22,6 +22,11 @@ class CovariantMetricTensor { const FieldMetric g_33, const FieldMetric g_12, const FieldMetric g_13, const FieldMetric g_23); + CovariantMetricTensor(const Array g_11, const Array g_22, + const Array g_33, const Array g_12, + const Array g_13, const Array g_23, + Mesh* mesh); + /// Invert covariant metric to get contravariant components int calcContravariant(const std::string& region = "RGN_ALL"); diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index fe1262aa8a..f79c5cf177 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -382,15 +382,14 @@ Coordinates::Coordinates(Mesh* mesh, FieldMetric dx, FieldMetric dy, FieldMetric Coordinates::Coordinates(Mesh* mesh, Options* options) : dx(1., mesh), dy(1., mesh), dz(1., mesh), d1_dx(mesh), d1_dy(mesh), d1_dz(mesh), - J(1., mesh), Bxy(1., mesh), - // Identity metric tensor - g11(1., mesh), g22(1., mesh), g33(1., mesh), g12(0, mesh), g13(0, mesh), - g23(0, mesh), g_11(1., mesh), g_22(1., mesh), g_33(1., mesh), g_12(0, mesh), - g_13(0, mesh), g_23(0, mesh), G1_11(mesh), G1_22(mesh), G1_33(mesh), G1_12(mesh), + J(1., mesh), Bxy(1., mesh), G1_11(mesh), G1_22(mesh), G1_33(mesh), G1_12(mesh), G1_13(mesh), G1_23(mesh), G2_11(mesh), G2_22(mesh), G2_33(mesh), G2_12(mesh), G2_13(mesh), G2_23(mesh), G3_11(mesh), G3_22(mesh), G3_33(mesh), G3_12(mesh), G3_13(mesh), G3_23(mesh), G1(mesh), G2(mesh), G3(mesh), ShiftTorsion(mesh), - IntShiftTorsion(mesh), localmesh(mesh), location(CELL_CENTRE) { + IntShiftTorsion(mesh), localmesh(mesh), location(CELL_CENTRE), + // Identity metric tensor + contravariantMetricTensor(1., 1., 1., 0, 0, 0, mesh), + covariantMetricTensor(1., 1., 1., 0, 0, 0, mesh) { if (options == nullptr) { options = Options::getRoot()->getSection("mesh"); @@ -468,14 +467,21 @@ Coordinates::Coordinates(Mesh* mesh, Options* options) // Diagonal components of metric tensor g^{ij} (default to 1) - g11 = getUnalignedAtLocation(g11, "g11", 1.0); - g22 = getUnalignedAtLocation(g22, "g22", 1.0); - g33 = getUnalignedAtLocation(g33, "g33", 1.0); + auto const contravariant_components = + contravariantMetricTensor.getContravariantMetricTensor(); + + FieldMetric g11, g22, g33, g12, g13, g23; + + g11 = getUnalignedAtLocation(contravariant_components.g11, "g11", 1.0); + g22 = getUnalignedAtLocation(contravariant_components.g22, "g22", 1.0); + g33 = getUnalignedAtLocation(contravariant_components.g33, "g33", 1.0); // Off-diagonal elements. Default to 0 - g12 = getUnalignedAtLocation(g12, "g12", 0.0); - g13 = getUnalignedAtLocation(g13, "g13", 0.0); - g23 = getUnalignedAtLocation(g23, "g23", 0.0); + g12 = getUnalignedAtLocation(contravariant_components.g12, "g12", 0.0); + g13 = getUnalignedAtLocation(contravariant_components.g13, "g13", 0.0); + g23 = getUnalignedAtLocation(contravariant_components.g23, "g23", 0.0); + + contravariantMetricTensor.setContravariantMetricTensor(g11, g22, g33, g12, g13, g23); // Check input metrics checkContravariant(); @@ -491,12 +497,15 @@ Coordinates::Coordinates(Mesh* mesh, Options* options) // Check that all components are present if (std::all_of(begin(covariant_component_names), end(covariant_component_names), source_has_component)) { - g_11 = getUnaligned(g_11, "g_11", 1.0); - g_22 = getUnaligned(g_22, "g_22", 1.0); - g_33 = getUnaligned(g_33, "g_33", 1.0); - g_12 = getUnaligned(g_12, "g_12", 0.0); - g_13 = getUnaligned(g_13, "g_13", 0.0); - g_23 = getUnaligned(g_23, "g_23", 0.0); + + auto const covariant_components = covariantMetricTensor.getCovariantMetricTensor(); + + covariant_components.g_11 = getUnaligned(covariant_components.g_11, "g_11", 1.0); + covariant_components.g_22 = getUnaligned(covariant_components.g_22, "g_22", 1.0); + covariant_components.g_33 = getUnaligned(covariant_components.g_33, "g_33", 1.0); + covariant_components.g_12 = getUnaligned(covariant_components.g_12, "g_12", 0.0); + covariant_components.g_13 = getUnaligned(covariant_components.g_13, "g_13", 0.0); + covariant_components.g_23 = getUnaligned(covariant_components.g_23, "g_23", 0.0); output_warn.write("\tWARNING! Covariant components of metric tensor set manually. " "Contravariant components NOT recalculated\n"); From 305e9541e8d910df76f42f28449b2d670d5c2c53 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Tue, 3 Oct 2023 10:49:41 +0100 Subject: [PATCH 061/491] MetricTensor components can't be const. --- include/bout/ContravariantMetricTensor.cpp | 2 +- include/bout/CovariantMetricTensor.cpp | 4 ++-- include/bout/CovariantMetricTensor.h | 4 ++-- src/mesh/coordinates.cxx | 4 ++-- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/include/bout/ContravariantMetricTensor.cpp b/include/bout/ContravariantMetricTensor.cpp index 3446cb3c84..48219e5d63 100644 --- a/include/bout/ContravariantMetricTensor.cpp +++ b/include/bout/ContravariantMetricTensor.cpp @@ -35,7 +35,7 @@ ContravariantMetricTensor::getContravariantMetricTensor() const { void ContravariantMetricTensor::setContravariantMetricTensor( CELL_LOC location, const ContravariantMetricTensor& metric_tensor) { - const auto new_components = metric_tensor.getContravariantMetricTensor(); + auto new_components = metric_tensor.getContravariantMetricTensor(); contravariant_components.g11 = new_components.g11; contravariant_components.g22 = new_components.g22; contravariant_components.g33 = new_components.g33; diff --git a/include/bout/CovariantMetricTensor.cpp b/include/bout/CovariantMetricTensor.cpp index f935107691..22626c1ef4 100644 --- a/include/bout/CovariantMetricTensor.cpp +++ b/include/bout/CovariantMetricTensor.cpp @@ -28,14 +28,14 @@ CovariantMetricTensor::CovariantMetricTensor(const Array g_11, } CovariantMetricTensor::CovariantComponents -CovariantMetricTensor::getCovariantMetricTensor() const { +CovariantMetricTensor::getCovariantMetricTensor() { return CovariantComponents{covariant_components.g_11, covariant_components.g_22, covariant_components.g_33, covariant_components.g_12, covariant_components.g_13, covariant_components.g_23}; } void CovariantMetricTensor::setCovariantMetricTensor( - const CovariantMetricTensor& metric_tensor) { + CovariantMetricTensor& metric_tensor) { const auto new_components = metric_tensor.getCovariantMetricTensor(); covariant_components.g_11 = new_components.g_11; diff --git a/include/bout/CovariantMetricTensor.h b/include/bout/CovariantMetricTensor.h index aa7820349a..e09236fbd4 100644 --- a/include/bout/CovariantMetricTensor.h +++ b/include/bout/CovariantMetricTensor.h @@ -33,9 +33,9 @@ class CovariantMetricTensor { // check that covariant tensors are positive (if expected) and finite (always) void checkCovariant(int ystart); - void setCovariantMetricTensor(const CovariantMetricTensor& metric_tensor); + void setCovariantMetricTensor(CovariantMetricTensor& metric_tensor); - CovariantComponents getCovariantMetricTensor() const; + CovariantComponents getCovariantMetricTensor(); void Allocate(); diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index f79c5cf177..f3e1df2256 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -467,7 +467,7 @@ Coordinates::Coordinates(Mesh* mesh, Options* options) // Diagonal components of metric tensor g^{ij} (default to 1) - auto const contravariant_components = + auto contravariant_components = contravariantMetricTensor.getContravariantMetricTensor(); FieldMetric g11, g22, g33, g12, g13, g23; @@ -498,7 +498,7 @@ Coordinates::Coordinates(Mesh* mesh, Options* options) if (std::all_of(begin(covariant_component_names), end(covariant_component_names), source_has_component)) { - auto const covariant_components = covariantMetricTensor.getCovariantMetricTensor(); + auto covariant_components = covariantMetricTensor.getCovariantMetricTensor(); covariant_components.g_11 = getUnaligned(covariant_components.g_11, "g_11", 1.0); covariant_components.g_22 = getUnaligned(covariant_components.g_22, "g_22", 1.0); From c36a8f89c770ecbf37cae0a220c2436c78b97ca1 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Tue, 3 Oct 2023 11:19:56 +0100 Subject: [PATCH 062/491] Fix call to setContravariantMetricTensor(). --- src/mesh/coordinates.cxx | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index f3e1df2256..d4da558f58 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -481,7 +481,8 @@ Coordinates::Coordinates(Mesh* mesh, Options* options) g13 = getUnalignedAtLocation(contravariant_components.g13, "g13", 0.0); g23 = getUnalignedAtLocation(contravariant_components.g23, "g23", 0.0); - contravariantMetricTensor.setContravariantMetricTensor(g11, g22, g33, g12, g13, g23); + contravariantMetricTensor.setContravariantMetricTensor( + location, ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); // Check input metrics checkContravariant(); From 63cc1b61d2d6da8e869e741027ac57a949d86b1d Mon Sep 17 00:00:00 2001 From: tomc271 Date: Tue, 3 Oct 2023 12:04:39 +0100 Subject: [PATCH 063/491] Fix another Coordinates constructor. --- src/mesh/coordinates.cxx | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index d4da558f58..cc69ad410a 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -616,15 +616,14 @@ Coordinates::Coordinates(Mesh* mesh, Options* options) Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, const Coordinates* coords_in, bool force_interpolate_from_centre) : dx(1., mesh), dy(1., mesh), dz(1., mesh), d1_dx(mesh), d1_dy(mesh), d1_dz(mesh), - J(1., mesh), Bxy(1., mesh), - // Identity metric tensor - g11(1., mesh), g22(1., mesh), g33(1., mesh), g12(0, mesh), g13(0, mesh), - g23(0, mesh), g_11(1., mesh), g_22(1., mesh), g_33(1., mesh), g_12(0, mesh), - g_13(0, mesh), g_23(0, mesh), G1_11(mesh), G1_22(mesh), G1_33(mesh), G1_12(mesh), + J(1., mesh), Bxy(1., mesh), G1_11(mesh), G1_22(mesh), G1_33(mesh), G1_12(mesh), G1_13(mesh), G1_23(mesh), G2_11(mesh), G2_22(mesh), G2_33(mesh), G2_12(mesh), G2_13(mesh), G2_23(mesh), G3_11(mesh), G3_22(mesh), G3_33(mesh), G3_12(mesh), G3_13(mesh), G3_23(mesh), G1(mesh), G2(mesh), G3(mesh), ShiftTorsion(mesh), - IntShiftTorsion(mesh), localmesh(mesh), location(loc) { + IntShiftTorsion(mesh), localmesh(mesh), location(loc), + // Identity metric tensor + contravariantMetricTensor(1., 1., 1., 0, 0, 0, mesh), + covariantMetricTensor(1., 1., 1., 0, 0, 0, mesh) { std::string suffix = getLocationSuffix(location); From 3e30a625f13085598a8990e22e9f62cf53cb7452 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Tue, 3 Oct 2023 12:05:34 +0100 Subject: [PATCH 064/491] Actually setCovariantMetricTensor. --- src/mesh/coordinates.cxx | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index cc69ad410a..30a7db06c5 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -525,6 +525,9 @@ Coordinates::Coordinates(Mesh* mesh, Options* options) throw BoutException("Error in calcCovariant call"); } } + + FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; + // More robust to extrapolate derived quantities directly, rather than // deriving from extrapolated covariant metric components g_11 = interpolateAndExtrapolate(g_11, location, extrapolate_x, extrapolate_y, false, @@ -540,6 +543,9 @@ Coordinates::Coordinates(Mesh* mesh, Options* options) g_23 = interpolateAndExtrapolate(g_23, location, extrapolate_x, extrapolate_y, false, transform.get()); + covariantMetricTensor.setCovariantMetricTensor( + CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + // Check covariant metrics checkCovariant(); From 1ae227b42b27748629b7e6dcb5cc80aafa11d638 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Tue, 3 Oct 2023 12:06:12 +0100 Subject: [PATCH 065/491] Remove more const keywords. --- include/bout/coordinates.hxx | 4 ++-- src/mesh/coordinates.cxx | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index 61e4958ee8..05fe73df76 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -107,11 +107,11 @@ public: ContravariantMetricTensor::ContravariantComponents getContravariantMetricTensor() const; - CovariantMetricTensor::CovariantComponents getCovariantMetricTensor() const; + CovariantMetricTensor::CovariantComponents getCovariantMetricTensor(); void setContravariantMetricTensor(const ContravariantMetricTensor& metric_tensor); - void setCovariantMetricTensor(const CovariantMetricTensor& metric_tensor); + void setCovariantMetricTensor(CovariantMetricTensor& metric_tensor); /// Christoffel symbol of the second kind (connection coefficients) FieldMetric G1_11, G1_22, G1_33, G1_12, G1_13, G1_23; diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 30a7db06c5..1d36400aa7 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -1962,10 +1962,10 @@ Coordinates::getContravariantMetricTensor() const { return contravariantMetricTensor.getContravariantMetricTensor(); } -CovariantMetricTensor::CovariantComponents Coordinates::getCovariantMetricTensor() const { +CovariantMetricTensor::CovariantComponents Coordinates::getCovariantMetricTensor() { return covariantMetricTensor.getCovariantMetricTensor(); } -void Coordinates::setCovariantMetricTensor(const CovariantMetricTensor& metric_tensor) { +void Coordinates::setCovariantMetricTensor(CovariantMetricTensor& metric_tensor) { covariantMetricTensor.setCovariantMetricTensor(metric_tensor); } From 2c76def3af6c3ab0895e5722fe4922716e4f6f83 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Tue, 3 Oct 2023 12:07:01 +0100 Subject: [PATCH 066/491] Change metric_tensor argument of setCovariantMetricTensor() to value type. --- include/bout/CovariantMetricTensor.cpp | 2 +- include/bout/CovariantMetricTensor.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/include/bout/CovariantMetricTensor.cpp b/include/bout/CovariantMetricTensor.cpp index 22626c1ef4..aff1123618 100644 --- a/include/bout/CovariantMetricTensor.cpp +++ b/include/bout/CovariantMetricTensor.cpp @@ -35,7 +35,7 @@ CovariantMetricTensor::getCovariantMetricTensor() { } void CovariantMetricTensor::setCovariantMetricTensor( - CovariantMetricTensor& metric_tensor) { + CovariantMetricTensor metric_tensor) { const auto new_components = metric_tensor.getCovariantMetricTensor(); covariant_components.g_11 = new_components.g_11; diff --git a/include/bout/CovariantMetricTensor.h b/include/bout/CovariantMetricTensor.h index e09236fbd4..032c7457f9 100644 --- a/include/bout/CovariantMetricTensor.h +++ b/include/bout/CovariantMetricTensor.h @@ -33,7 +33,7 @@ class CovariantMetricTensor { // check that covariant tensors are positive (if expected) and finite (always) void checkCovariant(int ystart); - void setCovariantMetricTensor(CovariantMetricTensor& metric_tensor); + void setCovariantMetricTensor(CovariantMetricTensor metric_tensor); CovariantComponents getCovariantMetricTensor(); From 411da980b4bb1c8eb63241a53bf043e8a8ea2c15 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Tue, 3 Oct 2023 15:01:01 +0100 Subject: [PATCH 067/491] Further fixes in Coordinates constructor. --- src/mesh/coordinates.cxx | 107 +++++++++++++++++++++++---------------- 1 file changed, 64 insertions(+), 43 deletions(-) diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 1d36400aa7..3dad81c8b3 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -679,20 +679,23 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, getAtLocAndFillGuards(mesh, dy, "dy", suffix, location, 1.0, extrapolate_x, extrapolate_y, false, transform.get()); + auto contravariant_components = + contravariantMetricTensor.getContravariantMetricTensor(); + // grid data source has staggered fields, so read instead of interpolating // Diagonal components of metric tensor g^{ij} (default to 1) - getAtLocAndFillGuards(mesh, g11, "g11", suffix, location, 1.0, extrapolate_x, - extrapolate_y, false, transform.get()); - getAtLocAndFillGuards(mesh, g22, "g22", suffix, location, 1.0, extrapolate_x, - extrapolate_y, false, transform.get()); - getAtLocAndFillGuards(mesh, g33, "g33", suffix, location, 1.0, extrapolate_x, - extrapolate_y, false, transform.get()); - getAtLocAndFillGuards(mesh, g12, "g12", suffix, location, 0.0, extrapolate_x, - extrapolate_y, false, transform.get()); - getAtLocAndFillGuards(mesh, g13, "g13", suffix, location, 0.0, extrapolate_x, - extrapolate_y, false, transform.get()); - getAtLocAndFillGuards(mesh, g23, "g23", suffix, location, 0.0, extrapolate_x, - extrapolate_y, false, transform.get()); + getAtLocAndFillGuards(mesh, contravariant_components.g11, "g11", suffix, location, + 1.0, extrapolate_x, extrapolate_y, false, transform.get()); + getAtLocAndFillGuards(mesh, contravariant_components.g22, "g22", suffix, location, + 1.0, extrapolate_x, extrapolate_y, false, transform.get()); + getAtLocAndFillGuards(mesh, contravariant_components.g33, "g33", suffix, location, + 1.0, extrapolate_x, extrapolate_y, false, transform.get()); + getAtLocAndFillGuards(mesh, contravariant_components.g12, "g12", suffix, location, + 0.0, extrapolate_x, extrapolate_y, false, transform.get()); + getAtLocAndFillGuards(mesh, contravariant_components.g13, "g13", suffix, location, + 0.0, extrapolate_x, extrapolate_y, false, transform.get()); + getAtLocAndFillGuards(mesh, contravariant_components.g23, "g23", suffix, location, + 0.0, extrapolate_x, extrapolate_y, false, transform.get()); // Check input metrics checkContravariant(); @@ -702,6 +705,9 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, auto source_has_component = [&suffix, &mesh](const std::string& name) { return mesh->sourceHasVar(name + suffix); }; + + auto covariant_components = covariantMetricTensor.getCovariantMetricTensor(); + // Check if any of the components are present if (std::any_of(begin(covariant_component_names), end(covariant_component_names), source_has_component)) { @@ -709,12 +715,12 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, if (std::all_of(begin(covariant_component_names), end(covariant_component_names), source_has_component)) { - getAtLoc(mesh, g_11, "g_11", suffix, location); - getAtLoc(mesh, g_22, "g_22", suffix, location); - getAtLoc(mesh, g_33, "g_33", suffix, location); - getAtLoc(mesh, g_12, "g_12", suffix, location); - getAtLoc(mesh, g_13, "g_13", suffix, location); - getAtLoc(mesh, g_23, "g_23", suffix, location); + getAtLoc(mesh, covariant_components.g_11, "g_11", suffix, location); + getAtLoc(mesh, covariant_components.g_22, "g_22", suffix, location); + getAtLoc(mesh, covariant_components.g_33, "g_33", suffix, location); + getAtLoc(mesh, covariant_components.g_12, "g_12", suffix, location); + getAtLoc(mesh, covariant_components.g_13, "g_13", suffix, location); + getAtLoc(mesh, covariant_components.g_23, "g_23", suffix, location); output_warn.write( "\tWARNING! Staggered covariant components of metric tensor set manually. " @@ -735,20 +741,23 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, throw BoutException("Error in staggered calcCovariant call"); } } + + FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; + // More robust to extrapolate derived quantities directly, rather than // deriving from extrapolated covariant metric components - g_11 = interpolateAndExtrapolate(g_11, location, extrapolate_x, extrapolate_y, false, - transform.get()); - g_22 = interpolateAndExtrapolate(g_22, location, extrapolate_x, extrapolate_y, false, - transform.get()); - g_33 = interpolateAndExtrapolate(g_33, location, extrapolate_x, extrapolate_y, false, - transform.get()); - g_12 = interpolateAndExtrapolate(g_12, location, extrapolate_x, extrapolate_y, false, - transform.get()); - g_13 = interpolateAndExtrapolate(g_13, location, extrapolate_x, extrapolate_y, false, - transform.get()); - g_23 = interpolateAndExtrapolate(g_23, location, extrapolate_x, extrapolate_y, false, - transform.get()); + g_11 = interpolateAndExtrapolate(covariant_components.g_11, location, extrapolate_x, + extrapolate_y, false, transform.get()); + g_22 = interpolateAndExtrapolate(covariant_components.g_22, location, extrapolate_x, + extrapolate_y, false, transform.get()); + g_33 = interpolateAndExtrapolate(covariant_components.g_33, location, extrapolate_x, + extrapolate_y, false, transform.get()); + g_12 = interpolateAndExtrapolate(covariant_components.g_12, location, extrapolate_x, + extrapolate_y, false, transform.get()); + g_13 = interpolateAndExtrapolate(covariant_components.g_13, location, extrapolate_x, + extrapolate_y, false, transform.get()); + g_23 = interpolateAndExtrapolate(covariant_components.g_23, location, extrapolate_x, + extrapolate_y, false, transform.get()); // Check covariant metrics checkCovariant(); @@ -849,7 +858,9 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, dz = interpolateAndExtrapolate(coords_in->dz, location, true, true, false, transform.get()); - ContravariantMetricTensor metric_tensor = coords_in->getContravariantMetricTensor(); + ContravariantMetricTensor::ContravariantComponents metric_tensor = + coords_in->getContravariantMetricTensor(); + FieldMetric g11, g22, g33, g12, g13, g23; // Diagonal components of metric tensor g^{ij} g11 = interpolateAndExtrapolate(metric_tensor.g11, location, true, true, false, @@ -867,22 +878,32 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, g23 = interpolateAndExtrapolate(metric_tensor.g23, location, true, true, false, transform.get()); + contravariantMetricTensor.setContravariantMetricTensor( + loc, ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); + + FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; + + const auto covariant_components = coords_in->getCovariantMetricTensor(); + // 3x3 matrix inversion can exaggerate small interpolation errors, so it is // more robust to interpolate and extrapolate derived quantities directly, // rather than deriving from interpolated/extrapolated covariant metric // components - g_11 = interpolateAndExtrapolate(coords_in->g_11, location, true, true, false, - transform.get()); - g_22 = interpolateAndExtrapolate(coords_in->g_22, location, true, true, false, - transform.get()); - g_33 = interpolateAndExtrapolate(coords_in->g_33, location, true, true, false, - transform.get()); - g_12 = interpolateAndExtrapolate(coords_in->g_12, location, true, true, false, - transform.get()); - g_13 = interpolateAndExtrapolate(coords_in->g_13, location, true, true, false, - transform.get()); - g_23 = interpolateAndExtrapolate(coords_in->g_23, location, true, true, false, - transform.get()); + g_11 = interpolateAndExtrapolate(covariant_components.g_11, location, true, true, + false, transform.get()); + g_22 = interpolateAndExtrapolate(covariant_components.g_22, location, true, true, + false, transform.get()); + g_33 = interpolateAndExtrapolate(covariant_components.g_33, location, true, true, + false, transform.get()); + g_12 = interpolateAndExtrapolate(covariant_components.g_12, location, true, true, + false, transform.get()); + g_13 = interpolateAndExtrapolate(covariant_components.g_13, location, true, true, + false, transform.get()); + g_23 = interpolateAndExtrapolate(covariant_components.g_23, location, true, true, + false, transform.get()); + + covariantMetricTensor.setCovariantMetricTensor( + CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); // Check input metrics checkContravariant(); From 36e8728bad3ea985f39406a09617fd8e91648ec8 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 4 Oct 2023 09:02:29 +0100 Subject: [PATCH 068/491] Fix uses of const. Don't cast Region to string. --- include/bout/ContravariantMetricTensor.cpp | 4 ++-- include/bout/ContravariantMetricTensor.h | 2 +- include/bout/CovariantMetricTensor.cpp | 2 +- include/bout/CovariantMetricTensor.h | 2 +- include/bout/coordinates.hxx | 2 +- src/mesh/coordinates.cxx | 9 ++++----- 6 files changed, 10 insertions(+), 11 deletions(-) diff --git a/include/bout/ContravariantMetricTensor.cpp b/include/bout/ContravariantMetricTensor.cpp index 48219e5d63..21b1f2f0f8 100644 --- a/include/bout/ContravariantMetricTensor.cpp +++ b/include/bout/ContravariantMetricTensor.cpp @@ -46,7 +46,7 @@ void ContravariantMetricTensor::setContravariantMetricTensor( } int ContravariantMetricTensor::calcCovariant(const CELL_LOC location, - std::string& region) { + const std::string& region) { TRACE("ContravariantMetricTensor::calcCovariant"); // Perform inversion of g^{ij} to get g_{ij} @@ -73,7 +73,7 @@ int ContravariantMetricTensor::calcCovariant(const CELL_LOC location, CovariantMetricTensor const covariantMetricTensor = CovariantMetricTensor(a(0, 0), a(1, 1), a(2, 2), a(0, 1), a(0, 2), a(1, 2)); - auto const covariant_components = covariantMetricTensor.getCovariantMetricTensor(); + auto covariant_components = covariantMetricTensor.getCovariantMetricTensor(); covariant_components.g_11.setLocation(location); covariant_components.g_22.setLocation(location); diff --git a/include/bout/ContravariantMetricTensor.h b/include/bout/ContravariantMetricTensor.h index dc7fcd37ad..a1f3137b46 100644 --- a/include/bout/ContravariantMetricTensor.h +++ b/include/bout/ContravariantMetricTensor.h @@ -31,7 +31,7 @@ class ContravariantMetricTensor { Mesh* mesh); /// Invert contravariant metric to get covariant components - int calcCovariant(CELL_LOC location, std::string& region = (std::string&)"RGN_ALL"); + int calcCovariant(CELL_LOC location, const std::string& region = "RGN_ALL"); // check that contravariant tensors are positive (if expected) and finite (always) void checkContravariant(int ystart); diff --git a/include/bout/CovariantMetricTensor.cpp b/include/bout/CovariantMetricTensor.cpp index aff1123618..701909509d 100644 --- a/include/bout/CovariantMetricTensor.cpp +++ b/include/bout/CovariantMetricTensor.cpp @@ -28,7 +28,7 @@ CovariantMetricTensor::CovariantMetricTensor(const Array g_11, } CovariantMetricTensor::CovariantComponents -CovariantMetricTensor::getCovariantMetricTensor() { +CovariantMetricTensor::getCovariantMetricTensor() const { return CovariantComponents{covariant_components.g_11, covariant_components.g_22, covariant_components.g_33, covariant_components.g_12, covariant_components.g_13, covariant_components.g_23}; diff --git a/include/bout/CovariantMetricTensor.h b/include/bout/CovariantMetricTensor.h index 032c7457f9..5ec97a349e 100644 --- a/include/bout/CovariantMetricTensor.h +++ b/include/bout/CovariantMetricTensor.h @@ -35,7 +35,7 @@ class CovariantMetricTensor { void setCovariantMetricTensor(CovariantMetricTensor metric_tensor); - CovariantComponents getCovariantMetricTensor(); + CovariantComponents getCovariantMetricTensor() const; void Allocate(); diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index 05fe73df76..68723be218 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -107,7 +107,7 @@ public: ContravariantMetricTensor::ContravariantComponents getContravariantMetricTensor() const; - CovariantMetricTensor::CovariantComponents getCovariantMetricTensor(); + CovariantMetricTensor::CovariantComponents getCovariantMetricTensor() const; void setContravariantMetricTensor(const ContravariantMetricTensor& metric_tensor); diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 3dad81c8b3..02c01349a8 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -985,9 +985,9 @@ int Coordinates::geometry(bool recalculate_staggered, bool force_interpolate_from_centre) { TRACE("Coordinates::geometry"); - auto const contravariant_components = + auto contravariant_components = contravariantMetricTensor.getContravariantMetricTensor(); - auto const covariant_components = covariantMetricTensor.getCovariantMetricTensor(); + auto covariant_components = covariantMetricTensor.getCovariantMetricTensor(); communicate(dx, dy, dz, contravariant_components.g11, contravariant_components.g22, contravariant_components.g33, contravariant_components.g12, @@ -1331,8 +1331,7 @@ void Coordinates::CalculateChristoffelSymbols() { int Coordinates::calcCovariant(const std::string& region) { TRACE("Coordinates::calcCovariant"); - return contravariantMetricTensor.calcCovariant(location, - const_cast(region)); + return contravariantMetricTensor.calcCovariant(location, region); } int Coordinates::calcContravariant(const std::string& region) { @@ -1983,7 +1982,7 @@ Coordinates::getContravariantMetricTensor() const { return contravariantMetricTensor.getContravariantMetricTensor(); } -CovariantMetricTensor::CovariantComponents Coordinates::getCovariantMetricTensor() { +CovariantMetricTensor::CovariantComponents Coordinates::getCovariantMetricTensor() const { return covariantMetricTensor.getCovariantMetricTensor(); } From 0bedec34c3a94daa888e03911bebf5ab6c9ce24c Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 4 Oct 2023 09:33:12 +0100 Subject: [PATCH 069/491] Update fv_ops to access the covariant components of the metric tensor through Coordinates->getCovariantMetricTensor(). --- include/bout/fv_ops.hxx | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/include/bout/fv_ops.hxx b/include/bout/fv_ops.hxx index 94007a57a2..37f26bd710 100644 --- a/include/bout/fv_ops.hxx +++ b/include/bout/fv_ops.hxx @@ -239,12 +239,15 @@ const Field3D Div_par(const Field3D& f_in, const Field3D& v_in, ye = mesh->yend; } + const auto covariant_components = coord->getCovariantMetricTensor(); + for (int j = ys; j <= ye; j++) { // Pre-calculate factors which multiply fluxes #if not(BOUT_USE_METRIC_3D) // For right cell boundaries BoutReal common_factor = (coord->J(i, j) + coord->J(i, j + 1)) - / (sqrt(coord->g_22(i, j)) + sqrt(coord->g_22(i, j + 1))); + / (sqrt(covariant_components.g_22(i, j)) + + sqrt(covariant_components.g_22(i, j + 1))); BoutReal flux_factor_rc = common_factor / (coord->dy(i, j) * coord->J(i, j)); BoutReal flux_factor_rp = @@ -252,7 +255,8 @@ const Field3D Div_par(const Field3D& f_in, const Field3D& v_in, // For left cell boundaries common_factor = (coord->J(i, j) + coord->J(i, j - 1)) - / (sqrt(coord->g_22(i, j)) + sqrt(coord->g_22(i, j - 1))); + / (sqrt(covariant_components.g_22(i, j)) + + sqrt(covariant_components.g_22(i, j - 1))); BoutReal flux_factor_lc = common_factor / (coord->dy(i, j) * coord->J(i, j)); BoutReal flux_factor_lm = From 5d8f4ef1737c1aa85034d44772e258dbb75f958c Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 4 Oct 2023 09:34:00 +0100 Subject: [PATCH 070/491] Fixed incorrect variable names. --- tests/unit/mesh/test_coordinates.cxx | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/tests/unit/mesh/test_coordinates.cxx b/tests/unit/mesh/test_coordinates.cxx index bcc78fde47..13b0d9b251 100644 --- a/tests/unit/mesh/test_coordinates.cxx +++ b/tests/unit/mesh/test_coordinates.cxx @@ -145,12 +145,12 @@ TEST_F(CoordinatesTest, CalcContravariant) { const auto updated_coords = coords.getCovariantMetricTensor(); - EXPECT_TRUE(IsFieldEqual(updated_coords.g11, 1.0)); - EXPECT_TRUE(IsFieldEqual(updated_coords.g22, 1.0)); - EXPECT_TRUE(IsFieldEqual(updated_coords.g33, 1.0)); - EXPECT_TRUE(IsFieldEqual(updated_coords.g12, 0.0)); - EXPECT_TRUE(IsFieldEqual(updated_coords.g13, 0.0)); - EXPECT_TRUE(IsFieldEqual(updated_coords.g23, 0.0)); + EXPECT_TRUE(IsFieldEqual(updated_coords.g_11, 1.0)); + EXPECT_TRUE(IsFieldEqual(updated_coords.g_22, 1.0)); + EXPECT_TRUE(IsFieldEqual(updated_coords.g_33, 1.0)); + EXPECT_TRUE(IsFieldEqual(updated_coords.g_12, 0.0)); + EXPECT_TRUE(IsFieldEqual(updated_coords.g_13, 0.0)); + EXPECT_TRUE(IsFieldEqual(updated_coords.g_23, 0.0)); } TEST_F(CoordinatesTest, CalcCovariant) { @@ -284,9 +284,9 @@ TEST_F(CoordinatesTest, ConstructWithDiagonalContravariantMetric) { // Covariant metric should be inverse // Note: Not calculated in corners const auto updated_coords = coords.getCovariantMetricTensor(); - EXPECT_TRUE(IsFieldEqual(updated_coords.g11, 1. / 2.0, "RGN_NOCORNERS")); - EXPECT_TRUE(IsFieldEqual(updated_coords.g22, 1. / 3.2, "RGN_NOCORNERS")); - EXPECT_TRUE(IsFieldEqual(updated_coords.g33, 1. / 42, "RGN_NOCORNERS")); + EXPECT_TRUE(IsFieldEqual(updated_coords.g_11, 1. / 2.0, "RGN_NOCORNERS")); + EXPECT_TRUE(IsFieldEqual(updated_coords.g_22, 1. / 3.2, "RGN_NOCORNERS")); + EXPECT_TRUE(IsFieldEqual(updated_coords.g_33, 1. / 42, "RGN_NOCORNERS")); EXPECT_TRUE(IsFieldEqual(coords.J, 1. / sqrt(2.0 * 3.2 * 42), "RGN_NOCORNERS")); EXPECT_TRUE(IsFieldEqual(coords.Bxy, sqrt(2.0 * 42), "RGN_NOCORNERS", 1e-10)); From 9753b54dd5695aa62d90b6160db4c62074bfdc39 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 4 Oct 2023 09:52:59 +0100 Subject: [PATCH 071/491] Fixed tests SetContravariantMetricTensor and SetCovariantMetricTensor. --- tests/unit/mesh/test_coordinates.cxx | 28 ++++++++-------------------- 1 file changed, 8 insertions(+), 20 deletions(-) diff --git a/tests/unit/mesh/test_coordinates.cxx b/tests/unit/mesh/test_coordinates.cxx index 13b0d9b251..97ae9a10fb 100644 --- a/tests/unit/mesh/test_coordinates.cxx +++ b/tests/unit/mesh/test_coordinates.cxx @@ -370,13 +370,7 @@ TEST_F(CoordinatesTest, SetContravariantMetricTensor) { FieldMetric{0.0}}; // IntShiftTorsion // Modify with setter - const auto updated_metric_tensor; - updated_metric_tensor.g11 = 1.7; - updated_metric_tensor.g22 = 2.3; - updated_metric_tensor.g33 = 3.1; - updated_metric_tensor.g12 = 0.9; - updated_metric_tensor.g13 = 5.7; - updated_metric_tensor.g23 = 1.9; + auto updated_metric_tensor = ContravariantMetricTensor(1.7, 2.3, 3.1, 0.9, 5.7, 1.9); coords.setContravariantMetricTensor(updated_metric_tensor); // Get values with getter and check they have been modified as expected @@ -446,22 +440,16 @@ TEST_F(CoordinatesTest, SetCovariantMetricTensor) { FieldMetric{0.0}}; // IntShiftTorsion // Modify with setter - const auto updated_metric_tensor; - updated_metric_tensor.g_11 = 1.7; - updated_metric_tensor.g_22 = 2.3; - updated_metric_tensor.g_33 = 3.1; - updated_metric_tensor.g_12 = 0.9; - updated_metric_tensor.g_13 = 5.7; - updated_metric_tensor.g_23 = 1.9; + auto updated_metric_tensor = CovariantMetricTensor(1.7, 2.3, 3.1, 0.9, 5.7, 1.9); coords.setCovariantMetricTensor(updated_metric_tensor); // Get values with getter and check they have been modified as expected const auto g = coords.getCovariantMetricTensor(); - EXPECT_TRUE(IsFieldEqual(g.g11, 1.7)); - EXPECT_TRUE(IsFieldEqual(g.g22, 2.3)); - EXPECT_TRUE(IsFieldEqual(g.g33, 3.1)); - EXPECT_TRUE(IsFieldEqual(g.g12, 0.9)); - EXPECT_TRUE(IsFieldEqual(g.g13, 5.7)); - EXPECT_TRUE(IsFieldEqual(g.g23, 1.9)); + EXPECT_TRUE(IsFieldEqual(g.g_11, 1.7)); + EXPECT_TRUE(IsFieldEqual(g.g_22, 2.3)); + EXPECT_TRUE(IsFieldEqual(g.g_33, 3.1)); + EXPECT_TRUE(IsFieldEqual(g.g_12, 0.9)); + EXPECT_TRUE(IsFieldEqual(g.g_13, 5.7)); + EXPECT_TRUE(IsFieldEqual(g.g_23, 1.9)); } } \ No newline at end of file From e0e7933afa687c4950451a8ab4e2e5a24ad136f6 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 4 Oct 2023 09:53:44 +0100 Subject: [PATCH 072/491] Removed another const. --- include/bout/coordinates.hxx | 8 +++----- src/mesh/coordinates.cxx | 3 +-- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index 68723be218..ea8d079863 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -99,17 +99,15 @@ public: FieldMetric Bxy; ///< Magnitude of B = nabla z times nabla x private: - ContravariantMetricTensor contravariantMetricTensor; CovariantMetricTensor covariantMetricTensor; public: - ContravariantMetricTensor::ContravariantComponents getContravariantMetricTensor() const; CovariantMetricTensor::CovariantComponents getCovariantMetricTensor() const; - void setContravariantMetricTensor(const ContravariantMetricTensor& metric_tensor); + void setContravariantMetricTensor(ContravariantMetricTensor& metric_tensor); void setCovariantMetricTensor(CovariantMetricTensor& metric_tensor); @@ -132,9 +130,9 @@ public: int calcCovariant(const std::string& region = "RGN_ALL"); /// Invert covariant metric to get contravariant components int calcContravariant(const std::string& region = "RGN_ALL"); - int jacobian(); ///< Calculate J and Bxy + int jacobian(); ///< Calculate J and Bxy void CalculateChristoffelSymbols(); /// Calculate Christoffel symbol terms - + /////////////////////////////////////////////////////////// // Parallel transforms /////////////////////////////////////////////////////////// diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 02c01349a8..2bff19cf68 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -1972,8 +1972,7 @@ void Coordinates::checkContravariant() { contravariantMetricTensor.checkContravariant(localmesh->ystart); } -void Coordinates::setContravariantMetricTensor( - const ContravariantMetricTensor& metric_tensor) { +void Coordinates::setContravariantMetricTensor(ContravariantMetricTensor& metric_tensor) { contravariantMetricTensor.setContravariantMetricTensor(location, metric_tensor); } From 1cf04829d4f969d18ff8e3a80997d9e8f4790cb7 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Thu, 5 Oct 2023 10:03:58 +0100 Subject: [PATCH 073/491] calcCovariant method to return a CovariantMetricTensor rather than int. --- include/bout/ContravariantMetricTensor.cpp | 13 +++++++------ include/bout/ContravariantMetricTensor.h | 3 ++- include/bout/coordinates.hxx | 2 +- src/mesh/coordinates.cxx | 18 +++++++++++++----- 4 files changed, 23 insertions(+), 13 deletions(-) diff --git a/include/bout/ContravariantMetricTensor.cpp b/include/bout/ContravariantMetricTensor.cpp index 21b1f2f0f8..258f20efb7 100644 --- a/include/bout/ContravariantMetricTensor.cpp +++ b/include/bout/ContravariantMetricTensor.cpp @@ -45,8 +45,9 @@ void ContravariantMetricTensor::setContravariantMetricTensor( calcCovariant(location); } -int ContravariantMetricTensor::calcCovariant(const CELL_LOC location, - const std::string& region) { +CovariantMetricTensor +ContravariantMetricTensor::calcCovariant(const CELL_LOC location, + const std::string& region) { TRACE("ContravariantMetricTensor::calcCovariant"); // Perform inversion of g^{ij} to get g_{ij} @@ -64,9 +65,9 @@ int ContravariantMetricTensor::calcCovariant(const CELL_LOC location, a(0, 2) = a(2, 0) = contravariant_components.g13[i]; if (invert3x3(a)) { - output_error.write("\tERROR: metric tensor is singular at ({:d}, {:d})\n", i.x(), - i.y()); - return 1; + const auto error_message = "\tERROR: metric tensor is singular at ({:d}, {:d})\n"; + output_error.write(error_message, i.x(), i.y()); + throw BoutException(error_message); } } @@ -110,7 +111,7 @@ int ContravariantMetricTensor::calcCovariant(const CELL_LOC location, output_info.write("\tLocal maximum error in off-diagonal inversion is {:e}\n", maxerr); - return 0; + return covariantMetricTensor; } void ContravariantMetricTensor::checkContravariant(int ystart) { diff --git a/include/bout/ContravariantMetricTensor.h b/include/bout/ContravariantMetricTensor.h index a1f3137b46..9834e4ca9c 100644 --- a/include/bout/ContravariantMetricTensor.h +++ b/include/bout/ContravariantMetricTensor.h @@ -2,6 +2,7 @@ #ifndef BOUT_CONTRAVARIANTMETRICTENSOR_H #define BOUT_CONTRAVARIANTMETRICTENSOR_H +#include "CovariantMetricTensor.h" #include "bout/field2d.hxx" #include "bout/field3d.hxx" #include "bout/paralleltransform.hxx" @@ -31,7 +32,7 @@ class ContravariantMetricTensor { Mesh* mesh); /// Invert contravariant metric to get covariant components - int calcCovariant(CELL_LOC location, const std::string& region = "RGN_ALL"); + CovariantMetricTensor calcCovariant(CELL_LOC location, const std::string& region = "RGN_ALL"); // check that contravariant tensors are positive (if expected) and finite (always) void checkContravariant(int ystart); diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index ea8d079863..5ed6922123 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -127,7 +127,7 @@ public: int geometry(bool recalculate_staggered = true, bool force_interpolate_from_centre = false); /// Invert contravariant metric to get covariant components - int calcCovariant(const std::string& region = "RGN_ALL"); + CovariantMetricTensor calcCovariant(const std::string& region = "RGN_ALL"); /// Invert covariant metric to get contravariant components int calcContravariant(const std::string& region = "RGN_ALL"); int jacobian(); ///< Calculate J and Bxy diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 2bff19cf68..eae122bc89 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -515,13 +515,17 @@ Coordinates::Coordinates(Mesh* mesh, Options* options) output_warn.write("Not all covariant components of metric tensor found. " "Calculating all from the contravariant tensor\n"); /// Calculate contravariant metric components if not found - if (calcCovariant("RGN_NOCORNERS")) { + try { + calcCovariant("RGN_NOCORNERS"); + } catch (BoutException) { throw BoutException("Error in calcCovariant call"); } } } else { /// Calculate contravariant metric components if not found - if (calcCovariant("RGN_NOCORNERS")) { + try { + calcCovariant("RGN_NOCORNERS"); + } catch (BoutException) { throw BoutException("Error in calcCovariant call"); } } @@ -731,13 +735,17 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, "Not all staggered covariant components of metric tensor found. " "Calculating all from the contravariant tensor\n"); /// Calculate contravariant metric components if not found - if (calcCovariant("RGN_NOCORNERS")) { + try { + calcCovariant("RGN_NOCORNERS"); + } catch (BoutException) { throw BoutException("Error in staggered calcCovariant call"); } } } else { /// Calculate contravariant metric components if not found - if (calcCovariant("RGN_NOCORNERS")) { + try { + calcCovariant("RGN_NOCORNERS"); + } catch (BoutException) { throw BoutException("Error in staggered calcCovariant call"); } } @@ -1329,7 +1337,7 @@ void Coordinates::CalculateChristoffelSymbols() { + 0.5 * contravariant_components.g33 * DDY(covariant_components.g_33); } -int Coordinates::calcCovariant(const std::string& region) { +CovariantMetricTensor Coordinates::calcCovariant(const std::string& region) { TRACE("Coordinates::calcCovariant"); return contravariantMetricTensor.calcCovariant(location, region); } From fee9223199eea372452271f54fffd726059ec325 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Thu, 5 Oct 2023 10:15:31 +0100 Subject: [PATCH 074/491] Add missing 'region' parameter in delegated function call. --- src/mesh/coordinates.cxx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index eae122bc89..1283608b95 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -1344,7 +1344,7 @@ CovariantMetricTensor Coordinates::calcCovariant(const std::string& region) { int Coordinates::calcContravariant(const std::string& region) { TRACE("Coordinates::calcContravariant"); - return covariantMetricTensor.calcContravariant(); + return covariantMetricTensor.calcContravariant(region); } int Coordinates::jacobian() { From f8f7ac1e5194dc68b698ca4537c8e5702ebee9d3 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Thu, 5 Oct 2023 11:46:03 +0100 Subject: [PATCH 075/491] The calcCovariant method of the Coordinates class should update the covariantMetricTensor field. --- src/mesh/coordinates.cxx | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 1283608b95..83345657fa 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -1339,7 +1339,9 @@ void Coordinates::CalculateChristoffelSymbols() { CovariantMetricTensor Coordinates::calcCovariant(const std::string& region) { TRACE("Coordinates::calcCovariant"); - return contravariantMetricTensor.calcCovariant(location, region); + auto new_covariantMetricTensor = contravariantMetricTensor.calcCovariant(location, region); + covariantMetricTensor = new_covariantMetricTensor; + return new_covariantMetricTensor; } int Coordinates::calcContravariant(const std::string& region) { From a0c1b7856a7dfeef1947ab3f61f29c8fb743cfc4 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Thu, 5 Oct 2023 11:56:34 +0100 Subject: [PATCH 076/491] calcContravariant method to return a ContravariantMetricTensor rather than int. Predeclare class ContravariantMetricTensor. The calcContravariant method of the Coordinates class should update the contravariantMetricTensor field. --- include/bout/CovariantMetricTensor.cpp | 11 ++++++----- include/bout/CovariantMetricTensor.h | 3 ++- include/bout/coordinates.hxx | 2 +- src/mesh/coordinates.cxx | 9 ++++++--- 4 files changed, 15 insertions(+), 10 deletions(-) diff --git a/include/bout/CovariantMetricTensor.cpp b/include/bout/CovariantMetricTensor.cpp index 701909509d..ab8f0202f1 100644 --- a/include/bout/CovariantMetricTensor.cpp +++ b/include/bout/CovariantMetricTensor.cpp @@ -47,7 +47,8 @@ void CovariantMetricTensor::setCovariantMetricTensor( calcContravariant(); } -int CovariantMetricTensor::calcContravariant(const std::string& region) { +ContravariantMetricTensor +CovariantMetricTensor::calcContravariant(const std::string& region) { TRACE("CovariantMetricTensor::calcContravariant"); // Perform inversion of g_{ij} to get g^{ij} @@ -65,9 +66,9 @@ int CovariantMetricTensor::calcContravariant(const std::string& region) { a(0, 2) = a(2, 0) = covariant_components.g_13[i]; if (invert3x3(a)) { - output_error.write("\tERROR: metric tensor is singular at ({:d}, {:d})\n", i.x(), - i.y()); - return 1; + const auto error_message = "\tERROR: metric tensor is singular at ({:d}, {:d})\n"; + output_error.write(error_message, i.x(), i.y()); + throw BoutException(error_message); } } @@ -104,7 +105,7 @@ int CovariantMetricTensor::calcContravariant(const std::string& region) { + covariant_components.g_23 * contravariant_components.g33))); output_info.write("\tMaximum error in off-diagonal inversion is {:e}\n", maxerr); - return 0; + return contravariantMetricTensor; } void CovariantMetricTensor::checkCovariant(int ystart) { diff --git a/include/bout/CovariantMetricTensor.h b/include/bout/CovariantMetricTensor.h index 5ec97a349e..bc896f0b21 100644 --- a/include/bout/CovariantMetricTensor.h +++ b/include/bout/CovariantMetricTensor.h @@ -5,6 +5,7 @@ #include "field2d.hxx" #include "bout/field3d.hxx" +class ContravariantMetricTensor; class CovariantMetricTensor { public: @@ -28,7 +29,7 @@ class CovariantMetricTensor { Mesh* mesh); /// Invert covariant metric to get contravariant components - int calcContravariant(const std::string& region = "RGN_ALL"); + ContravariantMetricTensor calcContravariant(const std::string& region = "RGN_ALL"); // check that covariant tensors are positive (if expected) and finite (always) void checkCovariant(int ystart); diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index 5ed6922123..702cbffccc 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -129,7 +129,7 @@ public: /// Invert contravariant metric to get covariant components CovariantMetricTensor calcCovariant(const std::string& region = "RGN_ALL"); /// Invert covariant metric to get contravariant components - int calcContravariant(const std::string& region = "RGN_ALL"); + ContravariantMetricTensor calcContravariant(const std::string& region = "RGN_ALL"); int jacobian(); ///< Calculate J and Bxy void CalculateChristoffelSymbols(); /// Calculate Christoffel symbol terms diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 83345657fa..893aafb278 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -1339,14 +1339,17 @@ void Coordinates::CalculateChristoffelSymbols() { CovariantMetricTensor Coordinates::calcCovariant(const std::string& region) { TRACE("Coordinates::calcCovariant"); - auto new_covariantMetricTensor = contravariantMetricTensor.calcCovariant(location, region); + auto new_covariantMetricTensor = + contravariantMetricTensor.calcCovariant(location, region); covariantMetricTensor = new_covariantMetricTensor; return new_covariantMetricTensor; } -int Coordinates::calcContravariant(const std::string& region) { +ContravariantMetricTensor Coordinates::calcContravariant(const std::string& region) { TRACE("Coordinates::calcContravariant"); - return covariantMetricTensor.calcContravariant(region); + auto new_contravariantMetricTensor = covariantMetricTensor.calcContravariant(region); + contravariantMetricTensor = new_contravariantMetricTensor; + return new_contravariantMetricTensor; } int Coordinates::jacobian() { From 25509e50644bf570632d469b9f960e6097538f80 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Mon, 9 Oct 2023 16:15:53 +0100 Subject: [PATCH 077/491] Individual metric tensor components are BoutReal not Array. --- include/bout/ContravariantMetricTensor.cpp | 5 ++--- include/bout/ContravariantMetricTensor.h | 8 ++++---- include/bout/CovariantMetricTensor.cpp | 12 ++++++------ include/bout/CovariantMetricTensor.h | 5 ++--- 4 files changed, 14 insertions(+), 16 deletions(-) diff --git a/include/bout/ContravariantMetricTensor.cpp b/include/bout/ContravariantMetricTensor.cpp index 258f20efb7..809307dbe4 100644 --- a/include/bout/ContravariantMetricTensor.cpp +++ b/include/bout/ContravariantMetricTensor.cpp @@ -14,9 +14,8 @@ ContravariantMetricTensor::ContravariantMetricTensor( } ContravariantMetricTensor::ContravariantMetricTensor( - const Array g11, const Array g22, const Array g33, - const Array g12, const Array g13, const Array g23, - Mesh* mesh) + const BoutReal g11, const BoutReal g22, const BoutReal g33, const BoutReal g12, + const BoutReal g13, const BoutReal g23, Mesh* mesh) : contravariant_components({FieldMetric(g11, mesh), FieldMetric(g22, mesh), FieldMetric(g33, mesh), FieldMetric(g12, mesh), FieldMetric(g13, mesh), FieldMetric(g23, mesh)}) { diff --git a/include/bout/ContravariantMetricTensor.h b/include/bout/ContravariantMetricTensor.h index 9834e4ca9c..45fccf0a62 100644 --- a/include/bout/ContravariantMetricTensor.h +++ b/include/bout/ContravariantMetricTensor.h @@ -26,13 +26,13 @@ class ContravariantMetricTensor { const FieldMetric g33, const FieldMetric g12, const FieldMetric g13, const FieldMetric g23); - ContravariantMetricTensor(const Array g11, const Array g22, - const Array g33, const Array g12, - const Array g13, const Array g23, + ContravariantMetricTensor(const BoutReal g11, const BoutReal g22, const BoutReal g33, + const BoutReal g12, const BoutReal g13, const BoutReal g23, Mesh* mesh); /// Invert contravariant metric to get covariant components - CovariantMetricTensor calcCovariant(CELL_LOC location, const std::string& region = "RGN_ALL"); + CovariantMetricTensor calcCovariant(CELL_LOC location, + const std::string& region = "RGN_ALL"); // check that contravariant tensors are positive (if expected) and finite (always) void checkContravariant(int ystart); diff --git a/include/bout/CovariantMetricTensor.cpp b/include/bout/CovariantMetricTensor.cpp index ab8f0202f1..5c94af411f 100644 --- a/include/bout/CovariantMetricTensor.cpp +++ b/include/bout/CovariantMetricTensor.cpp @@ -14,12 +14,12 @@ CovariantMetricTensor::CovariantMetricTensor( Allocate(); // Make sure metric elements are allocated // ; TODO: Required? } -CovariantMetricTensor::CovariantMetricTensor(const Array g_11, - const Array g_22, - const Array g_33, - const Array g_12, - const Array g_13, - const Array g_23, Mesh* mesh) +CovariantMetricTensor::CovariantMetricTensor(const BoutReal g_11, + const BoutReal g_22, + const BoutReal g_33, + const BoutReal g_12, + const BoutReal g_13, + const BoutReal g_23, Mesh* mesh) : covariant_components({FieldMetric(g_11, mesh), FieldMetric(g_22, mesh), FieldMetric(g_33, mesh), FieldMetric(g_12, mesh), FieldMetric(g_13, mesh), FieldMetric(g_23, mesh)}) { diff --git a/include/bout/CovariantMetricTensor.h b/include/bout/CovariantMetricTensor.h index bc896f0b21..34172d3217 100644 --- a/include/bout/CovariantMetricTensor.h +++ b/include/bout/CovariantMetricTensor.h @@ -23,9 +23,8 @@ class CovariantMetricTensor { const FieldMetric g_33, const FieldMetric g_12, const FieldMetric g_13, const FieldMetric g_23); - CovariantMetricTensor(const Array g_11, const Array g_22, - const Array g_33, const Array g_12, - const Array g_13, const Array g_23, + CovariantMetricTensor(const BoutReal g_11, const BoutReal g_22, const BoutReal g_33, + const BoutReal g_12, const BoutReal g_13, const BoutReal g_23, Mesh* mesh); /// Invert covariant metric to get contravariant components From 7bb2d5556bbe06cee05213d573027d95f766b81b Mon Sep 17 00:00:00 2001 From: tomc271 Date: Tue, 10 Oct 2023 10:21:42 +0100 Subject: [PATCH 078/491] Fix Coordinates constructor. --- src/mesh/coordinates.cxx | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 893aafb278..b3df5791c7 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -530,21 +530,22 @@ Coordinates::Coordinates(Mesh* mesh, Options* options) } } + auto covariant_components = covariantMetricTensor.getCovariantMetricTensor(); FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; // More robust to extrapolate derived quantities directly, rather than // deriving from extrapolated covariant metric components - g_11 = interpolateAndExtrapolate(g_11, location, extrapolate_x, extrapolate_y, false, + g_11 = interpolateAndExtrapolate(covariant_components.g_11, location, extrapolate_x, extrapolate_y, false, transform.get()); - g_22 = interpolateAndExtrapolate(g_22, location, extrapolate_x, extrapolate_y, false, + g_22 = interpolateAndExtrapolate(covariant_components.g_22, location, extrapolate_x, extrapolate_y, false, transform.get()); - g_33 = interpolateAndExtrapolate(g_33, location, extrapolate_x, extrapolate_y, false, + g_33 = interpolateAndExtrapolate(covariant_components.g_33, location, extrapolate_x, extrapolate_y, false, transform.get()); - g_12 = interpolateAndExtrapolate(g_12, location, extrapolate_x, extrapolate_y, false, + g_12 = interpolateAndExtrapolate(covariant_components.g_12, location, extrapolate_x, extrapolate_y, false, transform.get()); - g_13 = interpolateAndExtrapolate(g_13, location, extrapolate_x, extrapolate_y, false, + g_13 = interpolateAndExtrapolate(covariant_components.g_13, location, extrapolate_x, extrapolate_y, false, transform.get()); - g_23 = interpolateAndExtrapolate(g_23, location, extrapolate_x, extrapolate_y, false, + g_23 = interpolateAndExtrapolate(covariant_components.g_23, location, extrapolate_x, extrapolate_y, false, transform.get()); covariantMetricTensor.setCovariantMetricTensor( From 9ac75d01d03ef006146f82a707074550ea27ca36 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Tue, 10 Oct 2023 11:00:04 +0100 Subject: [PATCH 079/491] Update MMS tests to use new ContravariantMetricTensor and CovariantMetricTensor constructors. --- tests/MMS/diffusion/diffusion.cxx | 23 ++++++----------------- tests/MMS/diffusion2/diffusion.cxx | 23 ++++++----------------- tests/MMS/spatial/diffusion/diffusion.cxx | 22 +++++++--------------- tests/MMS/wave-1d/wave.cxx | 23 ++++++----------------- 4 files changed, 25 insertions(+), 66 deletions(-) diff --git a/tests/MMS/diffusion/diffusion.cxx b/tests/MMS/diffusion/diffusion.cxx index 50b611ff10..d7eb25de21 100644 --- a/tests/MMS/diffusion/diffusion.cxx +++ b/tests/MMS/diffusion/diffusion.cxx @@ -43,23 +43,12 @@ int Diffusion::init(bool UNUSED(restarting)) { SAVE_ONCE(mu_N); //set mesh - Coordinates::ContravariantMetricTensor contravariant_components; - contravariant_components.g11 = 1.0; - contravariant_components.g22 = 1.0; - contravariant_components.g33 = 1.0; - contravariant_components.g12 = 0.0; - contravariant_components.g13 = 0.0; - contravariant_components.g23 = 0.0; - coord->setContravariantMetricTensor(contravariant_components); - - Coordinates:: covariant_components; - covariant_components.g_11 = 1.0; - covariant_components.g_22 = 1.0; - covariant_components.g_33 = 1.0; - covariant_components.g_12 = 0.0; - covariant_components.g_13 = 0.0; - covariant_components.g_23 = 0.0; - coord->setCovariantMetricTensor(covariant_components); + auto contravariant_metric_tensor = + ContravariantMetricTensor(1.1, 1.1, 1.1, 0.0, 0.0, 0.0); + coord->setContravariantMetricTensor(contravariant_metric_tensor); + + auto covariant_metric_tensor = CovariantMetricTensor(1.1, 1.1, 1.1, 0.0, 0.0, 0.0); + coord->setCovariantMetricTensor(covariant_metric_tensor); coord->geometry(); diff --git a/tests/MMS/diffusion2/diffusion.cxx b/tests/MMS/diffusion2/diffusion.cxx index dd7928097a..db9a9b92fd 100644 --- a/tests/MMS/diffusion2/diffusion.cxx +++ b/tests/MMS/diffusion2/diffusion.cxx @@ -38,23 +38,12 @@ class Diffusion : public PhysicsModel { SAVE_ONCE3(Dx, Dy, Dz); // set mesh - Coordinates::MetricTensor contravariant_components = coords->getContravariantMetricTensor(); - contravariant_components.g11 = 1.0; - contravariant_components.g22 = 1.0; - contravariant_components.g33 = 1.0; - contravariant_components.g12 = 0.0; - contravariant_components.g13 = 0.0; - contravariant_components.g23 = 0.0; - coords->setContravariantMetricTensor(contravariant_components); - - Coordinates::MetricTensor covariant_components; - covariant_components.g_11 = 1.0; - covariant_components.g_22 = 1.0; - covariant_components.g_33 = 1.0; - covariant_components.g_12 = 0.0; - covariant_components.g_13 = 0.0; - covariant_components.g_23 = 0.0; - coords->setCovariantMetricTensor(covariant_components); + auto contravariant_metric_tensor = + ContravariantMetricTensor(1.1, 1.1, 1.1, 0.0, 0.0, 0.0); + coords->setContravariantMetricTensor(contravariant_metric_tensor); + + auto covariant_metric_tensor = CovariantMetricTensor(1.1, 1.1, 1.1, 0.0, 0.0, 0.0); + coords->setCovariantMetricTensor(covariant_metric_tensor); coords->geometry(); diff --git a/tests/MMS/spatial/diffusion/diffusion.cxx b/tests/MMS/spatial/diffusion/diffusion.cxx index 0514d851d9..0abdaae162 100644 --- a/tests/MMS/spatial/diffusion/diffusion.cxx +++ b/tests/MMS/spatial/diffusion/diffusion.cxx @@ -39,21 +39,13 @@ class Diffusion : public PhysicsModel { SAVE_ONCE3(Dx, Dy, Dz); // set mesh - Coordinates::MetricTensor contravariant_components = coords->getContravariantMetricTensor(); - contravariant_components.g11 = 1.0; - contravariant_components.g22 = 1.0; - contravariant_components.g33 = 1.0; - contravariant_components.g12 = 0.0; - contravariant_components.g13 = 0.0; - contravariant_components.g23 = 0.0; - coords->setContravariantMetricTensor(contravariant_components); - - coords->g_11 = 1.0; - coords->g_22 = 1.0; - coords->g_33 = 1.0; - coords->g_12 = 0.0; - coords->g_13 = 0.0; - coords->g_23 = 0.0; + auto contravariant_metric_tensor = + ContravariantMetricTensor(1.1, 1.1, 1.1, 0.0, 0.0, 0.0); + coords->setContravariantMetricTensor(contravariant_metric_tensor); + + auto covariant_metric_tensor = CovariantMetricTensor(1.1, 1.1, 1.1, 0.0, 0.0, 0.0); + coords->setCovariantMetricTensor(covariant_metric_tensor); + coords->geometry(); // Tell BOUT++ to solve N diff --git a/tests/MMS/wave-1d/wave.cxx b/tests/MMS/wave-1d/wave.cxx index b1df3a3d8c..e928dd1307 100644 --- a/tests/MMS/wave-1d/wave.cxx +++ b/tests/MMS/wave-1d/wave.cxx @@ -32,24 +32,13 @@ class Wave1D : public PhysicsModel { SAVE_ONCE(Lx, Ly); //set mesh - const auto contravariant_components = coord->getContravariantMetricTensor(); - contravariant_components.g11 = 1.0; - contravariant_components.g22 = 1.0; - contravariant_components.g33 = 1.0; - contravariant_components.g12 = 0.0; - contravariant_components.g13 = 0.0; - contravariant_components.g23 = 0.0; - coord->setContravariantMetricTensor(contravariant_components); - - const auto covariant_components; - covariant_components.g_11 = 1.0; - covariant_components.g_22 = 1.0; - covariant_components.g_33 = 1.0; - covariant_components.g_12 = 0.0; - covariant_components.g_13 = 0.0; - covariant_components.g_23 = 0.0; - coord->setCovariantMetricTensor(covariant_components); + auto contravariant_metric_tensor = + ContravariantMetricTensor(1.1, 1.1, 1.1, 0.0, 0.0, 0.0); + coord->setContravariantMetricTensor(contravariant_metric_tensor); + auto covariant_metric_tensor = CovariantMetricTensor(1.1, 1.1, 1.1, 0.0, 0.0, 0.0); + coord->setCovariantMetricTensor(covariant_metric_tensor); + coord->geometry(); g.setLocation(CELL_XLOW); // g staggered to the left of f From 7553325f34e80f6c2c24da2210ddc11f2d4e7cae Mon Sep 17 00:00:00 2001 From: tomc271 Date: Tue, 10 Oct 2023 12:13:23 +0100 Subject: [PATCH 080/491] Fix integrated tests after refactoring metric tensor classes. --- include/bout/coordinates.hxx | 4 +-- src/mesh/coordinates.cxx | 28 +++++++-------- .../test-drift-instability/2fluid.cxx | 36 ++++++++++--------- .../test-interchange-instability/2fluid.cxx | 36 ++++++++++--------- 4 files changed, 56 insertions(+), 48 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index 702cbffccc..70d47afec9 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -107,9 +107,9 @@ public: CovariantMetricTensor::CovariantComponents getCovariantMetricTensor() const; - void setContravariantMetricTensor(ContravariantMetricTensor& metric_tensor); + void setContravariantMetricTensor(ContravariantMetricTensor metric_tensor); - void setCovariantMetricTensor(CovariantMetricTensor& metric_tensor); + void setCovariantMetricTensor(CovariantMetricTensor metric_tensor); /// Christoffel symbol of the second kind (connection coefficients) FieldMetric G1_11, G1_22, G1_33, G1_12, G1_13, G1_23; diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index b3df5791c7..6c13a99f57 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -535,18 +535,18 @@ Coordinates::Coordinates(Mesh* mesh, Options* options) // More robust to extrapolate derived quantities directly, rather than // deriving from extrapolated covariant metric components - g_11 = interpolateAndExtrapolate(covariant_components.g_11, location, extrapolate_x, extrapolate_y, false, - transform.get()); - g_22 = interpolateAndExtrapolate(covariant_components.g_22, location, extrapolate_x, extrapolate_y, false, - transform.get()); - g_33 = interpolateAndExtrapolate(covariant_components.g_33, location, extrapolate_x, extrapolate_y, false, - transform.get()); - g_12 = interpolateAndExtrapolate(covariant_components.g_12, location, extrapolate_x, extrapolate_y, false, - transform.get()); - g_13 = interpolateAndExtrapolate(covariant_components.g_13, location, extrapolate_x, extrapolate_y, false, - transform.get()); - g_23 = interpolateAndExtrapolate(covariant_components.g_23, location, extrapolate_x, extrapolate_y, false, - transform.get()); + g_11 = interpolateAndExtrapolate(covariant_components.g_11, location, extrapolate_x, + extrapolate_y, false, transform.get()); + g_22 = interpolateAndExtrapolate(covariant_components.g_22, location, extrapolate_x, + extrapolate_y, false, transform.get()); + g_33 = interpolateAndExtrapolate(covariant_components.g_33, location, extrapolate_x, + extrapolate_y, false, transform.get()); + g_12 = interpolateAndExtrapolate(covariant_components.g_12, location, extrapolate_x, + extrapolate_y, false, transform.get()); + g_13 = interpolateAndExtrapolate(covariant_components.g_13, location, extrapolate_x, + extrapolate_y, false, transform.get()); + g_23 = interpolateAndExtrapolate(covariant_components.g_23, location, extrapolate_x, + extrapolate_y, false, transform.get()); covariantMetricTensor.setCovariantMetricTensor( CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); @@ -1986,7 +1986,7 @@ void Coordinates::checkContravariant() { contravariantMetricTensor.checkContravariant(localmesh->ystart); } -void Coordinates::setContravariantMetricTensor(ContravariantMetricTensor& metric_tensor) { +void Coordinates::setContravariantMetricTensor(ContravariantMetricTensor metric_tensor) { contravariantMetricTensor.setContravariantMetricTensor(location, metric_tensor); } @@ -1999,6 +1999,6 @@ CovariantMetricTensor::CovariantComponents Coordinates::getCovariantMetricTensor return covariantMetricTensor.getCovariantMetricTensor(); } -void Coordinates::setCovariantMetricTensor(CovariantMetricTensor& metric_tensor) { +void Coordinates::setCovariantMetricTensor(CovariantMetricTensor metric_tensor) { covariantMetricTensor.setCovariantMetricTensor(metric_tensor); } diff --git a/tests/integrated/test-drift-instability/2fluid.cxx b/tests/integrated/test-drift-instability/2fluid.cxx index 4a1efd873e..f46f00e843 100644 --- a/tests/integrated/test-drift-instability/2fluid.cxx +++ b/tests/integrated/test-drift-instability/2fluid.cxx @@ -217,25 +217,29 @@ class TwoFluid : public PhysicsModel { /**************** CALCULATE METRICS ******************/ - const auto contravariant_components; - contravariant_components.g11 = SQ(Rxy * Bpxy); - contravariant_components.g22 = 1.0 / SQ(hthe); - contravariant_components.g33 = SQ(I) * contravariant_components.g11 + SQ(coord->Bxy) / contravariant_components.g11; - contravariant_components.g12 = 0.0; - contravariant_components.g13 = -I * contravariant_components.g11; - contravariant_components.g23 = -Btxy / (hthe * Bpxy * Rxy); - coord->setContravariantMetricTensor(contravariant_components); + const auto contravariant_components = coord->getContravariantMetricTensor(); + CovariantMetricTensor::FieldMetric g11, g22, g33, g12, g13, g23; + g11 = SQ(Rxy * Bpxy); + g22 = 1.0 / SQ(hthe); + g33 = SQ(I) * contravariant_components.g11 + + SQ(coord->Bxy) / contravariant_components.g11; + g12 = 0.0; + g13 = -I * contravariant_components.g11; + g23 = -Btxy / (hthe * Bpxy * Rxy); + coord->setContravariantMetricTensor( + ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); coord->J = hthe / Bpxy; - const auto covariant_components; - covariant_components.g_11 = 1.0 / contravariant_components.g11 + SQ(I * Rxy); - covariant_components.g_22 = SQ(coord->Bxy * hthe / Bpxy); - covariant_components.g_33 = Rxy * Rxy; - covariant_components.g_12 = Btxy * hthe * I * Rxy / Bpxy; - covariant_components.g_13 = I * Rxy * Rxy; - covariant_components.g_23 = Btxy * hthe * Rxy / Bpxy; - coord->setCovariantMetricTensor(covariant_components); + CovariantMetricTensor::FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; + g_11 = 1.0 / contravariant_components.g11 + SQ(I * Rxy); + g_22 = SQ(coord->Bxy * hthe / Bpxy); + g_33 = Rxy * Rxy; + g_12 = Btxy * hthe * I * Rxy / Bpxy; + g_13 = I * Rxy * Rxy; + g_23 = Btxy * hthe * Rxy / Bpxy; + coord->setCovariantMetricTensor( + CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); coord->geometry(); diff --git a/tests/integrated/test-interchange-instability/2fluid.cxx b/tests/integrated/test-interchange-instability/2fluid.cxx index b7d21d6d20..d962e46121 100644 --- a/tests/integrated/test-interchange-instability/2fluid.cxx +++ b/tests/integrated/test-interchange-instability/2fluid.cxx @@ -139,25 +139,29 @@ class Interchange : public PhysicsModel { /**************** CALCULATE METRICS ******************/ - const auto contravariant_components; - contravariant_components.g11 = SQ(Rxy * Bpxy); - contravariant_components.g22 = 1.0 / SQ(hthe); - contravariant_components.g33 = SQ(I) * contravariant_components.g11 + SQ(coord->Bxy) / contravariant_components.g11; - contravariant_components.g12 = 0.0; - contravariant_components.g13 = -I * contravariant_components.g11; - contravariant_components.g23 = -Btxy / (hthe * Bpxy * Rxy); - coord->setContravariantMetricTensor(contravariant_components); + const auto contravariant_components = coord->getContravariantMetricTensor(); + CovariantMetricTensor::FieldMetric g11, g22, g33, g12, g13, g23; + g11 = SQ(Rxy * Bpxy); + g22 = 1.0 / SQ(hthe); + g33 = SQ(I) * contravariant_components.g11 + + SQ(coord->Bxy) / contravariant_components.g11; + g12 = 0.0; + g13 = -I * contravariant_components.g11; + g23 = -Btxy / (hthe * Bpxy * Rxy); + coord->setContravariantMetricTensor( + ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); coord->J = hthe / Bpxy; - const auto covariant_components; - covariant_components.g_11 = 1.0 / contravariant_components.g11 + SQ(I * Rxy); - covariant_components.g_22 = SQ(coord->Bxy * hthe / Bpxy); - covariant_components.g_33 = Rxy * Rxy; - covariant_components.g_12 = Btxy * hthe * I * Rxy / Bpxy; - covariant_components.g_13 = I * Rxy * Rxy; - covariant_components.g_23 = Btxy * hthe * Rxy / Bpxy; - coord->setCovariantMetricTensor(covariant_components); + CovariantMetricTensor::FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; + g_11 = 1.0 / contravariant_components.g11 + SQ(I * Rxy); + g_22 = SQ(coord->Bxy * hthe / Bpxy); + g_33 = Rxy * Rxy; + g_12 = Btxy * hthe * I * Rxy / Bpxy; + g_13 = I * Rxy * Rxy; + g_23 = Btxy * hthe * Rxy / Bpxy; + coord->setCovariantMetricTensor( + CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); coord->geometry(); From 8737145d63a87e70f7753288e387c4b3001be900 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 11 Oct 2023 10:58:03 +0100 Subject: [PATCH 081/491] Get mesh from first contravariant component in order to pass to covariant components in calcCovariant(), and visa versa. --- include/bout/ContravariantMetricTensor.cpp | 5 +++-- include/bout/CovariantMetricTensor.cpp | 3 ++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/include/bout/ContravariantMetricTensor.cpp b/include/bout/ContravariantMetricTensor.cpp index 809307dbe4..1c37c416f8 100644 --- a/include/bout/ContravariantMetricTensor.cpp +++ b/include/bout/ContravariantMetricTensor.cpp @@ -69,9 +69,10 @@ ContravariantMetricTensor::calcCovariant(const CELL_LOC location, throw BoutException(error_message); } } - + + const auto mesh = contravariant_components.g11.getMesh(); //TODO: Add a getMesh() method to ContravariantComponents? CovariantMetricTensor const covariantMetricTensor = - CovariantMetricTensor(a(0, 0), a(1, 1), a(2, 2), a(0, 1), a(0, 2), a(1, 2)); + CovariantMetricTensor(a(0, 0), a(1, 1), a(2, 2), a(0, 1), a(0, 2), a(1, 2), mesh); auto covariant_components = covariantMetricTensor.getCovariantMetricTensor(); diff --git a/include/bout/CovariantMetricTensor.cpp b/include/bout/CovariantMetricTensor.cpp index 5c94af411f..caff478c82 100644 --- a/include/bout/CovariantMetricTensor.cpp +++ b/include/bout/CovariantMetricTensor.cpp @@ -72,8 +72,9 @@ CovariantMetricTensor::calcContravariant(const std::string& region) { } } + const auto mesh = covariant_components.g_11.getMesh(); //TODO: Add a getMesh() method to CovariantComponents? ContravariantMetricTensor const contravariantMetricTensor = - ContravariantMetricTensor(a(0, 0), a(1, 1), a(2, 2), a(0, 1), a(0, 2), a(1, 2)); + ContravariantMetricTensor(a(0, 0), a(1, 1), a(2, 2), a(0, 1), a(0, 2), a(1, 2), mesh); auto const contravariant_components = contravariantMetricTensor.getContravariantMetricTensor(); From 77df76aabe5fc22aec89ff2a9a37f920b90a9b42 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Thu, 12 Oct 2023 10:10:23 +0100 Subject: [PATCH 082/491] Improved exception handling (include the message from the inner exception). --- src/mesh/coordinates.cxx | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 6c13a99f57..b82bb8b051 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -738,16 +738,19 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, /// Calculate contravariant metric components if not found try { calcCovariant("RGN_NOCORNERS"); - } catch (BoutException) { - throw BoutException("Error in staggered calcCovariant call"); + } catch (BoutException& err) { + std::cout << err.what(); + throw BoutException("Error in staggered calcCovariant call/n" + + std::string(err.what())); } } } else { /// Calculate contravariant metric components if not found try { calcCovariant("RGN_NOCORNERS"); - } catch (BoutException) { - throw BoutException("Error in staggered calcCovariant call"); + } catch (BoutException& err) { + throw BoutException("Error in staggered calcCovariant call/n" + + std::string(err.what())); } } From e8c140c0f3e1bbda2da2fa1f83fdd6853b8556f5 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Thu, 12 Oct 2023 10:34:13 +0100 Subject: [PATCH 083/491] Set location of contravariant components as well as covariant components, so that they match. --- include/bout/ContravariantMetricTensor.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/include/bout/ContravariantMetricTensor.cpp b/include/bout/ContravariantMetricTensor.cpp index 1c37c416f8..37d251f955 100644 --- a/include/bout/ContravariantMetricTensor.cpp +++ b/include/bout/ContravariantMetricTensor.cpp @@ -83,6 +83,13 @@ ContravariantMetricTensor::calcCovariant(const CELL_LOC location, covariant_components.g_13.setLocation(location); covariant_components.g_23.setLocation(location); + contravariant_components.g11.setLocation(location); + contravariant_components.g22.setLocation(location); + contravariant_components.g33.setLocation(location); + contravariant_components.g12.setLocation(location); + contravariant_components.g13.setLocation(location); + contravariant_components.g23.setLocation(location); + BoutReal maxerr; maxerr = BOUTMAX(max(abs((covariant_components.g_11 * contravariant_components.g11 + covariant_components.g_12 * contravariant_components.g12 From 2fbc60ca6a2faad8d65e90161b0cfbc1ceec3a9f Mon Sep 17 00:00:00 2001 From: tomc271 Date: Sat, 14 Oct 2023 17:51:16 +0100 Subject: [PATCH 084/491] calcCovariant should return covariant metric tensor with updated location. --- include/bout/ContravariantMetricTensor.cpp | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/include/bout/ContravariantMetricTensor.cpp b/include/bout/ContravariantMetricTensor.cpp index 37d251f955..94e8b4aba0 100644 --- a/include/bout/ContravariantMetricTensor.cpp +++ b/include/bout/ContravariantMetricTensor.cpp @@ -82,6 +82,14 @@ ContravariantMetricTensor::calcCovariant(const CELL_LOC location, covariant_components.g_12.setLocation(location); covariant_components.g_13.setLocation(location); covariant_components.g_23.setLocation(location); + + const auto updated_covariantMetricTensor = CovariantMetricTensor( + covariant_components.g_11, + covariant_components.g_22, + covariant_components.g_33, + covariant_components.g_12, + covariant_components.g_13, + covariant_components.g_23); contravariant_components.g11.setLocation(location); contravariant_components.g22.setLocation(location); @@ -118,7 +126,7 @@ ContravariantMetricTensor::calcCovariant(const CELL_LOC location, output_info.write("\tLocal maximum error in off-diagonal inversion is {:e}\n", maxerr); - return covariantMetricTensor; + return updated_covariantMetricTensor; } void ContravariantMetricTensor::checkContravariant(int ystart) { From d741f7a6348d4d50f6ea2491598e72ed3ac604d6 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Sat, 14 Oct 2023 18:03:19 +0100 Subject: [PATCH 085/491] Use setContravariantMetricTensor method to update Coordinates::contravariantMetricTensor with modified components. --- src/mesh/coordinates.cxx | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index b82bb8b051..981370608f 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -689,6 +689,7 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, // grid data source has staggered fields, so read instead of interpolating // Diagonal components of metric tensor g^{ij} (default to 1) + // TODO: Method `getAtLocAndFillGuards` violates command–query separation principle? getAtLocAndFillGuards(mesh, contravariant_components.g11, "g11", suffix, location, 1.0, extrapolate_x, extrapolate_y, false, transform.get()); getAtLocAndFillGuards(mesh, contravariant_components.g22, "g22", suffix, location, @@ -702,6 +703,12 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, getAtLocAndFillGuards(mesh, contravariant_components.g23, "g23", suffix, location, 0.0, extrapolate_x, extrapolate_y, false, transform.get()); + contravariantMetricTensor.setContravariantMetricTensor( + loc, ContravariantMetricTensor( + contravariant_components.g11, contravariant_components.g22, + contravariant_components.g33, contravariant_components.g12, + contravariant_components.g13, contravariant_components.g23)); + // Check input metrics checkContravariant(); From 511e361ba274681da88e4e0c25e97ed3a12fc4db Mon Sep 17 00:00:00 2001 From: tomc271 Date: Sat, 14 Oct 2023 18:19:17 +0100 Subject: [PATCH 086/491] Reorder initialization list of Coordinates constructor to match declaration order. --- src/mesh/coordinates.cxx | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 981370608f..2890031fc9 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -375,21 +375,21 @@ Coordinates::Coordinates(Mesh* mesh, FieldMetric dx, FieldMetric dy, FieldMetric FieldMetric g_23, FieldMetric ShiftTorsion, FieldMetric IntShiftTorsion) : dx(std::move(dx)), dy(std::move(dy)), dz(dz), J(std::move(J)), Bxy(std::move(Bxy)), - ShiftTorsion(std::move(ShiftTorsion)), IntShiftTorsion(std::move(IntShiftTorsion)), - nz(mesh->LocalNz), localmesh(mesh), location(CELL_CENTRE), contravariantMetricTensor(g11, g22, g33, g12, g13, g23), - covariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23) {} + covariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23), + ShiftTorsion(std::move(ShiftTorsion)), IntShiftTorsion(std::move(IntShiftTorsion)), + nz(mesh->LocalNz), localmesh(mesh), location(CELL_CENTRE) {} Coordinates::Coordinates(Mesh* mesh, Options* options) : dx(1., mesh), dy(1., mesh), dz(1., mesh), d1_dx(mesh), d1_dy(mesh), d1_dz(mesh), - J(1., mesh), Bxy(1., mesh), G1_11(mesh), G1_22(mesh), G1_33(mesh), G1_12(mesh), - G1_13(mesh), G1_23(mesh), G2_11(mesh), G2_22(mesh), G2_33(mesh), G2_12(mesh), - G2_13(mesh), G2_23(mesh), G3_11(mesh), G3_22(mesh), G3_33(mesh), G3_12(mesh), - G3_13(mesh), G3_23(mesh), G1(mesh), G2(mesh), G3(mesh), ShiftTorsion(mesh), - IntShiftTorsion(mesh), localmesh(mesh), location(CELL_CENTRE), + J(1., mesh), Bxy(1., mesh), contravariantMetricTensor(1., 1., 1., 0, 0, 0, mesh), + covariantMetricTensor(1., 1., 1., 0, 0, 0, mesh), G1_11(mesh), G1_22(mesh), + G1_33(mesh), G1_12(mesh), G1_13(mesh), G1_23(mesh), G2_11(mesh), G2_22(mesh), + G2_33(mesh), G2_12(mesh), G2_13(mesh), G2_23(mesh), G3_11(mesh), G3_22(mesh), + G3_33(mesh), G3_12(mesh), G3_13(mesh), G3_23(mesh), G1(mesh), G2(mesh), G3(mesh), + ShiftTorsion(mesh), IntShiftTorsion(mesh), // Identity metric tensor - contravariantMetricTensor(1., 1., 1., 0, 0, 0, mesh), - covariantMetricTensor(1., 1., 1., 0, 0, 0, mesh) { + localmesh(mesh), location(CELL_CENTRE) { if (options == nullptr) { options = Options::getRoot()->getSection("mesh"); @@ -627,14 +627,14 @@ Coordinates::Coordinates(Mesh* mesh, Options* options) Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, const Coordinates* coords_in, bool force_interpolate_from_centre) : dx(1., mesh), dy(1., mesh), dz(1., mesh), d1_dx(mesh), d1_dy(mesh), d1_dz(mesh), - J(1., mesh), Bxy(1., mesh), G1_11(mesh), G1_22(mesh), G1_33(mesh), G1_12(mesh), - G1_13(mesh), G1_23(mesh), G2_11(mesh), G2_22(mesh), G2_33(mesh), G2_12(mesh), - G2_13(mesh), G2_23(mesh), G3_11(mesh), G3_22(mesh), G3_33(mesh), G3_12(mesh), - G3_13(mesh), G3_23(mesh), G1(mesh), G2(mesh), G3(mesh), ShiftTorsion(mesh), - IntShiftTorsion(mesh), localmesh(mesh), location(loc), + J(1., mesh), Bxy(1., mesh), contravariantMetricTensor(1., 1., 1., 0, 0, 0, mesh), covariantMetricTensor(1., 1., 1., 0, 0, 0, mesh), G1_11(mesh), G1_22(mesh), + G1_33(mesh), G1_12(mesh), G1_13(mesh), G1_23(mesh), G2_11(mesh), G2_22(mesh), + G2_33(mesh), G2_12(mesh), G2_13(mesh), G2_23(mesh), G3_11(mesh), G3_22(mesh), + G3_33(mesh), G3_12(mesh), G3_13(mesh), G3_23(mesh), G1(mesh), G2(mesh), + G3(mesh), ShiftTorsion(mesh), IntShiftTorsion(mesh), // Identity metric tensor - contravariantMetricTensor(1., 1., 1., 0, 0, 0, mesh), - covariantMetricTensor(1., 1., 1., 0, 0, 0, mesh) { + localmesh(mesh), + location(loc) { std::string suffix = getLocationSuffix(location); @@ -689,7 +689,7 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, // grid data source has staggered fields, so read instead of interpolating // Diagonal components of metric tensor g^{ij} (default to 1) - // TODO: Method `getAtLocAndFillGuards` violates command–query separation principle? + // TODO: Method `getAtLocAndFillGuards` violates command–query separation principle? getAtLocAndFillGuards(mesh, contravariant_components.g11, "g11", suffix, location, 1.0, extrapolate_x, extrapolate_y, false, transform.get()); getAtLocAndFillGuards(mesh, contravariant_components.g22, "g22", suffix, location, From cb15c97c06d714ade5d3780af9d5cb87d018a739 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Sat, 14 Oct 2023 18:22:11 +0100 Subject: [PATCH 087/491] Changed auto to auto*. --- include/bout/ContravariantMetricTensor.cpp | 18 ++++++++---------- include/bout/CovariantMetricTensor.cpp | 18 +++++++++--------- 2 files changed, 17 insertions(+), 19 deletions(-) diff --git a/include/bout/ContravariantMetricTensor.cpp b/include/bout/ContravariantMetricTensor.cpp index 94e8b4aba0..389256f00d 100644 --- a/include/bout/ContravariantMetricTensor.cpp +++ b/include/bout/ContravariantMetricTensor.cpp @@ -69,8 +69,10 @@ ContravariantMetricTensor::calcCovariant(const CELL_LOC location, throw BoutException(error_message); } } - - const auto mesh = contravariant_components.g11.getMesh(); //TODO: Add a getMesh() method to ContravariantComponents? + + auto* const mesh = + contravariant_components.g11 + .getMesh(); //TODO: Add a getMesh() method to ContravariantComponents? CovariantMetricTensor const covariantMetricTensor = CovariantMetricTensor(a(0, 0), a(1, 1), a(2, 2), a(0, 1), a(0, 2), a(1, 2), mesh); @@ -82,14 +84,10 @@ ContravariantMetricTensor::calcCovariant(const CELL_LOC location, covariant_components.g_12.setLocation(location); covariant_components.g_13.setLocation(location); covariant_components.g_23.setLocation(location); - + const auto updated_covariantMetricTensor = CovariantMetricTensor( - covariant_components.g_11, - covariant_components.g_22, - covariant_components.g_33, - covariant_components.g_12, - covariant_components.g_13, - covariant_components.g_23); + covariant_components.g_11, covariant_components.g_22, covariant_components.g_33, + covariant_components.g_12, covariant_components.g_13, covariant_components.g_23); contravariant_components.g11.setLocation(location); contravariant_components.g22.setLocation(location); @@ -97,7 +95,7 @@ ContravariantMetricTensor::calcCovariant(const CELL_LOC location, contravariant_components.g12.setLocation(location); contravariant_components.g13.setLocation(location); contravariant_components.g23.setLocation(location); - + BoutReal maxerr; maxerr = BOUTMAX(max(abs((covariant_components.g_11 * contravariant_components.g11 + covariant_components.g_12 * contravariant_components.g12 diff --git a/include/bout/CovariantMetricTensor.cpp b/include/bout/CovariantMetricTensor.cpp index caff478c82..cdac1c2ad9 100644 --- a/include/bout/CovariantMetricTensor.cpp +++ b/include/bout/CovariantMetricTensor.cpp @@ -14,12 +14,10 @@ CovariantMetricTensor::CovariantMetricTensor( Allocate(); // Make sure metric elements are allocated // ; TODO: Required? } -CovariantMetricTensor::CovariantMetricTensor(const BoutReal g_11, - const BoutReal g_22, - const BoutReal g_33, - const BoutReal g_12, - const BoutReal g_13, - const BoutReal g_23, Mesh* mesh) +CovariantMetricTensor::CovariantMetricTensor(const BoutReal g_11, const BoutReal g_22, + const BoutReal g_33, const BoutReal g_12, + const BoutReal g_13, const BoutReal g_23, + Mesh* mesh) : covariant_components({FieldMetric(g_11, mesh), FieldMetric(g_22, mesh), FieldMetric(g_33, mesh), FieldMetric(g_12, mesh), FieldMetric(g_13, mesh), FieldMetric(g_23, mesh)}) { @@ -72,9 +70,11 @@ CovariantMetricTensor::calcContravariant(const std::string& region) { } } - const auto mesh = covariant_components.g_11.getMesh(); //TODO: Add a getMesh() method to CovariantComponents? - ContravariantMetricTensor const contravariantMetricTensor = - ContravariantMetricTensor(a(0, 0), a(1, 1), a(2, 2), a(0, 1), a(0, 2), a(1, 2), mesh); + auto* const mesh = + covariant_components.g_11 + .getMesh(); //TODO: Add a getMesh() method to CovariantComponents? + ContravariantMetricTensor const contravariantMetricTensor = ContravariantMetricTensor( + a(0, 0), a(1, 1), a(2, 2), a(0, 1), a(0, 2), a(1, 2), mesh); auto const contravariant_components = contravariantMetricTensor.getContravariantMetricTensor(); From 28ca5040218796bfe879c710ed9cce58e6f9c9a1 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Sat, 14 Oct 2023 21:07:34 +0100 Subject: [PATCH 088/491] Set location in calcContravariant method to ensure locations of covariant and contravariant metric tensors match. --- include/bout/CovariantMetricTensor.cpp | 22 +++++++++++++++++----- include/bout/CovariantMetricTensor.h | 5 +++-- src/mesh/coordinates.cxx | 19 ++++++++++--------- 3 files changed, 30 insertions(+), 16 deletions(-) diff --git a/include/bout/CovariantMetricTensor.cpp b/include/bout/CovariantMetricTensor.cpp index cdac1c2ad9..5e69ab0c49 100644 --- a/include/bout/CovariantMetricTensor.cpp +++ b/include/bout/CovariantMetricTensor.cpp @@ -33,7 +33,7 @@ CovariantMetricTensor::getCovariantMetricTensor() const { } void CovariantMetricTensor::setCovariantMetricTensor( - CovariantMetricTensor metric_tensor) { + CELL_LOC location, CovariantMetricTensor metric_tensor) { const auto new_components = metric_tensor.getCovariantMetricTensor(); covariant_components.g_11 = new_components.g_11; @@ -42,11 +42,11 @@ void CovariantMetricTensor::setCovariantMetricTensor( covariant_components.g_12 = new_components.g_12; covariant_components.g_13 = new_components.g_13; covariant_components.g_23 = new_components.g_23; - calcContravariant(); + calcContravariant(location); } ContravariantMetricTensor -CovariantMetricTensor::calcContravariant(const std::string& region) { +CovariantMetricTensor::calcContravariant(CELL_LOC location, const std::string& region) { TRACE("CovariantMetricTensor::calcContravariant"); // Perform inversion of g_{ij} to get g^{ij} @@ -76,9 +76,21 @@ CovariantMetricTensor::calcContravariant(const std::string& region) { ContravariantMetricTensor const contravariantMetricTensor = ContravariantMetricTensor( a(0, 0), a(1, 1), a(2, 2), a(0, 1), a(0, 2), a(1, 2), mesh); - auto const contravariant_components = + auto contravariant_components = contravariantMetricTensor.getContravariantMetricTensor(); + contravariant_components.g11.setLocation(location); + contravariant_components.g22.setLocation(location); + contravariant_components.g33.setLocation(location); + contravariant_components.g12.setLocation(location); + contravariant_components.g13.setLocation(location); + contravariant_components.g23.setLocation(location); + + const auto updated_contravariantMetricTensor = ContravariantMetricTensor( + contravariant_components.g11, contravariant_components.g22, + contravariant_components.g33, contravariant_components.g12, + contravariant_components.g13, contravariant_components.g23); + BoutReal maxerr; maxerr = BOUTMAX(max(abs((covariant_components.g_11 * contravariant_components.g11 + covariant_components.g_12 * contravariant_components.g12 @@ -106,7 +118,7 @@ CovariantMetricTensor::calcContravariant(const std::string& region) { + covariant_components.g_23 * contravariant_components.g33))); output_info.write("\tMaximum error in off-diagonal inversion is {:e}\n", maxerr); - return contravariantMetricTensor; + return updated_contravariantMetricTensor; } void CovariantMetricTensor::checkCovariant(int ystart) { diff --git a/include/bout/CovariantMetricTensor.h b/include/bout/CovariantMetricTensor.h index 34172d3217..fa6f698c9d 100644 --- a/include/bout/CovariantMetricTensor.h +++ b/include/bout/CovariantMetricTensor.h @@ -28,12 +28,13 @@ class CovariantMetricTensor { Mesh* mesh); /// Invert covariant metric to get contravariant components - ContravariantMetricTensor calcContravariant(const std::string& region = "RGN_ALL"); + ContravariantMetricTensor calcContravariant(CELL_LOC location, + const std::string& region = "RGN_ALL"); // check that covariant tensors are positive (if expected) and finite (always) void checkCovariant(int ystart); - void setCovariantMetricTensor(CovariantMetricTensor metric_tensor); + void setCovariantMetricTensor(CELL_LOC location, CovariantMetricTensor metric_tensor); CovariantComponents getCovariantMetricTensor() const; diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 2890031fc9..d95acbf11f 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -549,7 +549,7 @@ Coordinates::Coordinates(Mesh* mesh, Options* options) extrapolate_y, false, transform.get()); covariantMetricTensor.setCovariantMetricTensor( - CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + location, CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); // Check covariant metrics checkCovariant(); @@ -627,14 +627,14 @@ Coordinates::Coordinates(Mesh* mesh, Options* options) Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, const Coordinates* coords_in, bool force_interpolate_from_centre) : dx(1., mesh), dy(1., mesh), dz(1., mesh), d1_dx(mesh), d1_dy(mesh), d1_dz(mesh), - J(1., mesh), Bxy(1., mesh), contravariantMetricTensor(1., 1., 1., 0, 0, 0, mesh), covariantMetricTensor(1., 1., 1., 0, 0, 0, mesh), G1_11(mesh), G1_22(mesh), + J(1., mesh), Bxy(1., mesh), contravariantMetricTensor(1., 1., 1., 0, 0, 0, mesh), + covariantMetricTensor(1., 1., 1., 0, 0, 0, mesh), G1_11(mesh), G1_22(mesh), G1_33(mesh), G1_12(mesh), G1_13(mesh), G1_23(mesh), G2_11(mesh), G2_22(mesh), G2_33(mesh), G2_12(mesh), G2_13(mesh), G2_23(mesh), G3_11(mesh), G3_22(mesh), - G3_33(mesh), G3_12(mesh), G3_13(mesh), G3_23(mesh), G1(mesh), G2(mesh), - G3(mesh), ShiftTorsion(mesh), IntShiftTorsion(mesh), + G3_33(mesh), G3_12(mesh), G3_13(mesh), G3_23(mesh), G1(mesh), G2(mesh), G3(mesh), + ShiftTorsion(mesh), IntShiftTorsion(mesh), // Identity metric tensor - localmesh(mesh), - location(loc) { + localmesh(mesh), location(loc) { std::string suffix = getLocationSuffix(location); @@ -922,7 +922,7 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, false, transform.get()); covariantMetricTensor.setCovariantMetricTensor( - CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + location, CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); // Check input metrics checkContravariant(); @@ -1358,7 +1358,8 @@ CovariantMetricTensor Coordinates::calcCovariant(const std::string& region) { ContravariantMetricTensor Coordinates::calcContravariant(const std::string& region) { TRACE("Coordinates::calcContravariant"); - auto new_contravariantMetricTensor = covariantMetricTensor.calcContravariant(region); + auto new_contravariantMetricTensor = + covariantMetricTensor.calcContravariant(location, region); contravariantMetricTensor = new_contravariantMetricTensor; return new_contravariantMetricTensor; } @@ -2010,5 +2011,5 @@ CovariantMetricTensor::CovariantComponents Coordinates::getCovariantMetricTensor } void Coordinates::setCovariantMetricTensor(CovariantMetricTensor metric_tensor) { - covariantMetricTensor.setCovariantMetricTensor(metric_tensor); + covariantMetricTensor.setCovariantMetricTensor(location, metric_tensor); } From 9692ab636878561d1aae794be83a9c1759a4c6de Mon Sep 17 00:00:00 2001 From: tomc271 Date: Sat, 14 Oct 2023 21:24:32 +0100 Subject: [PATCH 089/491] Change type of argument of setCovariantMetricTensor method to const CovariantMetricTensor&, to match setContravariantMetricTensor method. --- include/bout/ContravariantMetricTensor.cpp | 2 +- include/bout/CovariantMetricTensor.cpp | 3 ++- include/bout/CovariantMetricTensor.h | 3 ++- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/include/bout/ContravariantMetricTensor.cpp b/include/bout/ContravariantMetricTensor.cpp index 389256f00d..3c5dc83260 100644 --- a/include/bout/ContravariantMetricTensor.cpp +++ b/include/bout/ContravariantMetricTensor.cpp @@ -34,7 +34,7 @@ ContravariantMetricTensor::getContravariantMetricTensor() const { void ContravariantMetricTensor::setContravariantMetricTensor( CELL_LOC location, const ContravariantMetricTensor& metric_tensor) { - auto new_components = metric_tensor.getContravariantMetricTensor(); + const auto new_components = metric_tensor.getContravariantMetricTensor(); contravariant_components.g11 = new_components.g11; contravariant_components.g22 = new_components.g22; contravariant_components.g33 = new_components.g33; diff --git a/include/bout/CovariantMetricTensor.cpp b/include/bout/CovariantMetricTensor.cpp index 5e69ab0c49..f08d599870 100644 --- a/include/bout/CovariantMetricTensor.cpp +++ b/include/bout/CovariantMetricTensor.cpp @@ -33,7 +33,7 @@ CovariantMetricTensor::getCovariantMetricTensor() const { } void CovariantMetricTensor::setCovariantMetricTensor( - CELL_LOC location, CovariantMetricTensor metric_tensor) { + CELL_LOC location, const CovariantMetricTensor& metric_tensor) { const auto new_components = metric_tensor.getCovariantMetricTensor(); covariant_components.g_11 = new_components.g_11; @@ -118,6 +118,7 @@ CovariantMetricTensor::calcContravariant(CELL_LOC location, const std::string& r + covariant_components.g_23 * contravariant_components.g33))); output_info.write("\tMaximum error in off-diagonal inversion is {:e}\n", maxerr); + return updated_contravariantMetricTensor; } diff --git a/include/bout/CovariantMetricTensor.h b/include/bout/CovariantMetricTensor.h index fa6f698c9d..a5a20c013e 100644 --- a/include/bout/CovariantMetricTensor.h +++ b/include/bout/CovariantMetricTensor.h @@ -34,7 +34,8 @@ class CovariantMetricTensor { // check that covariant tensors are positive (if expected) and finite (always) void checkCovariant(int ystart); - void setCovariantMetricTensor(CELL_LOC location, CovariantMetricTensor metric_tensor); + void setCovariantMetricTensor(CELL_LOC location, + const CovariantMetricTensor& metric_tensor); CovariantComponents getCovariantMetricTensor() const; From ec6b917ff74be6805f62444831ef75f0b48257cd Mon Sep 17 00:00:00 2001 From: tomc271 Date: Sun, 15 Oct 2023 12:43:46 +0100 Subject: [PATCH 090/491] Extract function CovariantMetricTensor::setLocation(const CELL_LOC location). --- include/bout/ContravariantMetricTensor.cpp | 12 +++--------- include/bout/CovariantMetricTensor.cpp | 9 +++++++++ include/bout/CovariantMetricTensor.h | 2 ++ 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/include/bout/ContravariantMetricTensor.cpp b/include/bout/ContravariantMetricTensor.cpp index 3c5dc83260..f846c75fc9 100644 --- a/include/bout/ContravariantMetricTensor.cpp +++ b/include/bout/ContravariantMetricTensor.cpp @@ -73,18 +73,12 @@ ContravariantMetricTensor::calcCovariant(const CELL_LOC location, auto* const mesh = contravariant_components.g11 .getMesh(); //TODO: Add a getMesh() method to ContravariantComponents? - CovariantMetricTensor const covariantMetricTensor = + CovariantMetricTensor covariantMetricTensor = CovariantMetricTensor(a(0, 0), a(1, 1), a(2, 2), a(0, 1), a(0, 2), a(1, 2), mesh); - auto covariant_components = covariantMetricTensor.getCovariantMetricTensor(); - - covariant_components.g_11.setLocation(location); - covariant_components.g_22.setLocation(location); - covariant_components.g_33.setLocation(location); - covariant_components.g_12.setLocation(location); - covariant_components.g_13.setLocation(location); - covariant_components.g_23.setLocation(location); + covariantMetricTensor.setLocation(location); + auto covariant_components = covariantMetricTensor.getCovariantMetricTensor(); const auto updated_covariantMetricTensor = CovariantMetricTensor( covariant_components.g_11, covariant_components.g_22, covariant_components.g_33, covariant_components.g_12, covariant_components.g_13, covariant_components.g_23); diff --git a/include/bout/CovariantMetricTensor.cpp b/include/bout/CovariantMetricTensor.cpp index f08d599870..5aec7bc16a 100644 --- a/include/bout/CovariantMetricTensor.cpp +++ b/include/bout/CovariantMetricTensor.cpp @@ -185,3 +185,12 @@ void CovariantMetricTensor::Allocate() { // ; TODO: Required? covariant_components.g_13.allocate(); covariant_components.g_23.allocate(); } + +void CovariantMetricTensor::setLocation(const CELL_LOC location) { + covariant_components.g_11.setLocation(location); + covariant_components.g_22.setLocation(location); + covariant_components.g_33.setLocation(location); + covariant_components.g_12.setLocation(location); + covariant_components.g_13.setLocation(location); + covariant_components.g_23.setLocation(location); +} diff --git a/include/bout/CovariantMetricTensor.h b/include/bout/CovariantMetricTensor.h index a5a20c013e..a4f782064d 100644 --- a/include/bout/CovariantMetricTensor.h +++ b/include/bout/CovariantMetricTensor.h @@ -41,6 +41,8 @@ class CovariantMetricTensor { void Allocate(); + void setLocation(const CELL_LOC location); + private: CovariantComponents covariant_components; }; From bda52c03f26c3c1cd5277a163a58b44e91332fff Mon Sep 17 00:00:00 2001 From: tomc271 Date: Sun, 15 Oct 2023 12:50:28 +0100 Subject: [PATCH 091/491] Extract function ContravariantMetricTensor::setLocation(const CELL_LOC location). --- include/bout/ContravariantMetricTensor.cpp | 16 ++++++++++------ include/bout/ContravariantMetricTensor.h | 2 ++ include/bout/CovariantMetricTensor.cpp | 9 ++------- 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/include/bout/ContravariantMetricTensor.cpp b/include/bout/ContravariantMetricTensor.cpp index f846c75fc9..0ba4f57739 100644 --- a/include/bout/ContravariantMetricTensor.cpp +++ b/include/bout/ContravariantMetricTensor.cpp @@ -83,12 +83,7 @@ ContravariantMetricTensor::calcCovariant(const CELL_LOC location, covariant_components.g_11, covariant_components.g_22, covariant_components.g_33, covariant_components.g_12, covariant_components.g_13, covariant_components.g_23); - contravariant_components.g11.setLocation(location); - contravariant_components.g22.setLocation(location); - contravariant_components.g33.setLocation(location); - contravariant_components.g12.setLocation(location); - contravariant_components.g13.setLocation(location); - contravariant_components.g23.setLocation(location); + setLocation(location); BoutReal maxerr; maxerr = BOUTMAX(max(abs((covariant_components.g_11 * contravariant_components.g11 @@ -184,3 +179,12 @@ void ContravariantMetricTensor::Allocate() { // ; TODO: Required? contravariant_components.g13.allocate(); contravariant_components.g23.allocate(); } + +void ContravariantMetricTensor::setLocation(const CELL_LOC location) { + contravariant_components.g11.setLocation(location); + contravariant_components.g22.setLocation(location); + contravariant_components.g33.setLocation(location); + contravariant_components.g12.setLocation(location); + contravariant_components.g13.setLocation(location); + contravariant_components.g23.setLocation(location); +} diff --git a/include/bout/ContravariantMetricTensor.h b/include/bout/ContravariantMetricTensor.h index 45fccf0a62..0a01569e32 100644 --- a/include/bout/ContravariantMetricTensor.h +++ b/include/bout/ContravariantMetricTensor.h @@ -44,6 +44,8 @@ class ContravariantMetricTensor { void Allocate(); + void setLocation(const CELL_LOC location); + private: ContravariantComponents contravariant_components; }; diff --git a/include/bout/CovariantMetricTensor.cpp b/include/bout/CovariantMetricTensor.cpp index 5aec7bc16a..03150d53b6 100644 --- a/include/bout/CovariantMetricTensor.cpp +++ b/include/bout/CovariantMetricTensor.cpp @@ -73,18 +73,13 @@ CovariantMetricTensor::calcContravariant(CELL_LOC location, const std::string& r auto* const mesh = covariant_components.g_11 .getMesh(); //TODO: Add a getMesh() method to CovariantComponents? - ContravariantMetricTensor const contravariantMetricTensor = ContravariantMetricTensor( + ContravariantMetricTensor contravariantMetricTensor = ContravariantMetricTensor( a(0, 0), a(1, 1), a(2, 2), a(0, 1), a(0, 2), a(1, 2), mesh); auto contravariant_components = contravariantMetricTensor.getContravariantMetricTensor(); - contravariant_components.g11.setLocation(location); - contravariant_components.g22.setLocation(location); - contravariant_components.g33.setLocation(location); - contravariant_components.g12.setLocation(location); - contravariant_components.g13.setLocation(location); - contravariant_components.g23.setLocation(location); + contravariantMetricTensor.setLocation(location); const auto updated_contravariantMetricTensor = ContravariantMetricTensor( contravariant_components.g11, contravariant_components.g22, From 95654fecd4f7de9e15c03c3c096f00e868e60d21 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Sun, 15 Oct 2023 13:28:05 +0100 Subject: [PATCH 092/491] Set contravariantMetricTensor before get. --- include/bout/CovariantMetricTensor.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/bout/CovariantMetricTensor.cpp b/include/bout/CovariantMetricTensor.cpp index 03150d53b6..30277c9213 100644 --- a/include/bout/CovariantMetricTensor.cpp +++ b/include/bout/CovariantMetricTensor.cpp @@ -76,11 +76,11 @@ CovariantMetricTensor::calcContravariant(CELL_LOC location, const std::string& r ContravariantMetricTensor contravariantMetricTensor = ContravariantMetricTensor( a(0, 0), a(1, 1), a(2, 2), a(0, 1), a(0, 2), a(1, 2), mesh); + contravariantMetricTensor.setLocation(location); + auto contravariant_components = contravariantMetricTensor.getContravariantMetricTensor(); - contravariantMetricTensor.setLocation(location); - const auto updated_contravariantMetricTensor = ContravariantMetricTensor( contravariant_components.g11, contravariant_components.g22, contravariant_components.g33, contravariant_components.g12, From 6d9d5ea018758ca11f25e3767877d1849daca84a Mon Sep 17 00:00:00 2001 From: tomc271 Date: Sun, 15 Oct 2023 13:31:32 +0100 Subject: [PATCH 093/491] No need to create new object. --- include/bout/ContravariantMetricTensor.cpp | 8 ++------ include/bout/CovariantMetricTensor.cpp | 9 ++------- 2 files changed, 4 insertions(+), 13 deletions(-) diff --git a/include/bout/ContravariantMetricTensor.cpp b/include/bout/ContravariantMetricTensor.cpp index 0ba4f57739..bb1087cdab 100644 --- a/include/bout/ContravariantMetricTensor.cpp +++ b/include/bout/ContravariantMetricTensor.cpp @@ -76,14 +76,10 @@ ContravariantMetricTensor::calcCovariant(const CELL_LOC location, CovariantMetricTensor covariantMetricTensor = CovariantMetricTensor(a(0, 0), a(1, 1), a(2, 2), a(0, 1), a(0, 2), a(1, 2), mesh); + setLocation(location); covariantMetricTensor.setLocation(location); auto covariant_components = covariantMetricTensor.getCovariantMetricTensor(); - const auto updated_covariantMetricTensor = CovariantMetricTensor( - covariant_components.g_11, covariant_components.g_22, covariant_components.g_33, - covariant_components.g_12, covariant_components.g_13, covariant_components.g_23); - - setLocation(location); BoutReal maxerr; maxerr = BOUTMAX(max(abs((covariant_components.g_11 * contravariant_components.g11 @@ -113,7 +109,7 @@ ContravariantMetricTensor::calcCovariant(const CELL_LOC location, output_info.write("\tLocal maximum error in off-diagonal inversion is {:e}\n", maxerr); - return updated_covariantMetricTensor; + return covariantMetricTensor; } void ContravariantMetricTensor::checkContravariant(int ystart) { diff --git a/include/bout/CovariantMetricTensor.cpp b/include/bout/CovariantMetricTensor.cpp index 30277c9213..55aff96851 100644 --- a/include/bout/CovariantMetricTensor.cpp +++ b/include/bout/CovariantMetricTensor.cpp @@ -75,17 +75,12 @@ CovariantMetricTensor::calcContravariant(CELL_LOC location, const std::string& r .getMesh(); //TODO: Add a getMesh() method to CovariantComponents? ContravariantMetricTensor contravariantMetricTensor = ContravariantMetricTensor( a(0, 0), a(1, 1), a(2, 2), a(0, 1), a(0, 2), a(1, 2), mesh); - + contravariantMetricTensor.setLocation(location); auto contravariant_components = contravariantMetricTensor.getContravariantMetricTensor(); - const auto updated_contravariantMetricTensor = ContravariantMetricTensor( - contravariant_components.g11, contravariant_components.g22, - contravariant_components.g33, contravariant_components.g12, - contravariant_components.g13, contravariant_components.g23); - BoutReal maxerr; maxerr = BOUTMAX(max(abs((covariant_components.g_11 * contravariant_components.g11 + covariant_components.g_12 * contravariant_components.g12 @@ -114,7 +109,7 @@ CovariantMetricTensor::calcContravariant(CELL_LOC location, const std::string& r output_info.write("\tMaximum error in off-diagonal inversion is {:e}\n", maxerr); - return updated_contravariantMetricTensor; + return contravariantMetricTensor; } void CovariantMetricTensor::checkCovariant(int ystart) { From 51c2d6a8458bbd2b57c96118849cb78da5409020 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Mon, 16 Oct 2023 10:45:03 +0100 Subject: [PATCH 094/491] Use setCovariantMetricTensor method to update covariantMetricTensor with modified components. --- src/mesh/coordinates.cxx | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index d95acbf11f..a60cf27274 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -500,13 +500,17 @@ Coordinates::Coordinates(Mesh* mesh, Options* options) source_has_component)) { auto covariant_components = covariantMetricTensor.getCovariantMetricTensor(); - - covariant_components.g_11 = getUnaligned(covariant_components.g_11, "g_11", 1.0); - covariant_components.g_22 = getUnaligned(covariant_components.g_22, "g_22", 1.0); - covariant_components.g_33 = getUnaligned(covariant_components.g_33, "g_33", 1.0); - covariant_components.g_12 = getUnaligned(covariant_components.g_12, "g_12", 0.0); - covariant_components.g_13 = getUnaligned(covariant_components.g_13, "g_13", 0.0); - covariant_components.g_23 = getUnaligned(covariant_components.g_23, "g_23", 0.0); + + FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; + g_11 = getUnaligned(covariant_components.g_11, "g_11", 1.0); + g_22 = getUnaligned(covariant_components.g_22, "g_22", 1.0); + g_33 = getUnaligned(covariant_components.g_33, "g_33", 1.0); + g_12 = getUnaligned(covariant_components.g_12, "g_12", 0.0); + g_13 = getUnaligned(covariant_components.g_13, "g_13", 0.0); + g_23 = getUnaligned(covariant_components.g_23, "g_23", 0.0); + + covariantMetricTensor.setCovariantMetricTensor( + location, CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); output_warn.write("\tWARNING! Covariant components of metric tensor set manually. " "Contravariant components NOT recalculated\n"); From e0229aad17a1442ce52048ce9ac9b34c62feb9c8 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Mon, 16 Oct 2023 12:26:36 +0100 Subject: [PATCH 095/491] Give header files hxx extension. Move cpp files from include/bout to src/mesh. [skip ci] --- CMakeLists.txt | 8 ++++---- ...ariantMetricTensor.h => ContravariantMetricTensor.hxx} | 8 ++++---- ...{CovariantMetricTensor.h => CovariantMetricTensor.hxx} | 6 +++--- include/bout/coordinates.hxx | 4 ++-- {include/bout => src/mesh}/ContravariantMetricTensor.cpp | 4 ++-- {include/bout => src/mesh}/CovariantMetricTensor.cpp | 8 ++++---- src/mesh/coordinates.cxx | 4 ++-- src/mesh/coordinates_accessor.cxx | 2 +- 8 files changed, 22 insertions(+), 22 deletions(-) rename include/bout/{ContravariantMetricTensor.h => ContravariantMetricTensor.hxx} (89%) rename include/bout/{CovariantMetricTensor.h => CovariantMetricTensor.hxx} (92%) rename {include/bout => src/mesh}/ContravariantMetricTensor.cpp (99%) rename {include/bout => src/mesh}/CovariantMetricTensor.cpp (98%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9ec9c53948..676902361d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -354,10 +354,10 @@ set(BOUT_SOURCES ./src/sys/utils.cxx ${CMAKE_CURRENT_BINARY_DIR}/include/bout/revision.hxx ${CMAKE_CURRENT_BINARY_DIR}/include/bout/version.hxx - include/bout/ContravariantMetricTensor.h - include/bout/ContravariantMetricTensor.cpp - include/bout/CovariantMetricTensor.h - include/bout/CovariantMetricTensor.cpp + include/bout/ContravariantMetricTensor.hxx + src/mesh/ContravariantMetricTensor.cpp + include/bout/CovariantMetricTensor.hxx + src/mesh/CovariantMetricTensor.cpp ) diff --git a/include/bout/ContravariantMetricTensor.h b/include/bout/ContravariantMetricTensor.hxx similarity index 89% rename from include/bout/ContravariantMetricTensor.h rename to include/bout/ContravariantMetricTensor.hxx index 0a01569e32..7dfee2b32e 100644 --- a/include/bout/ContravariantMetricTensor.h +++ b/include/bout/ContravariantMetricTensor.hxx @@ -1,8 +1,8 @@ -#ifndef BOUT_CONTRAVARIANTMETRICTENSOR_H -#define BOUT_CONTRAVARIANTMETRICTENSOR_H +#ifndef BOUT_CONTRAVARIANTMETRICTENSOR_HXX +#define BOUT_CONTRAVARIANTMETRICTENSOR_HXX -#include "CovariantMetricTensor.h" +#include "CovariantMetricTensor.hxx" #include "bout/field2d.hxx" #include "bout/field3d.hxx" #include "bout/paralleltransform.hxx" @@ -50,4 +50,4 @@ class ContravariantMetricTensor { ContravariantComponents contravariant_components; }; -#endif //BOUT_CONTRAVARIANTMETRICTENSOR_H +#endif //BOUT_CONTRAVARIANTMETRICTENSOR_HXX diff --git a/include/bout/CovariantMetricTensor.h b/include/bout/CovariantMetricTensor.hxx similarity index 92% rename from include/bout/CovariantMetricTensor.h rename to include/bout/CovariantMetricTensor.hxx index a4f782064d..80436a64c7 100644 --- a/include/bout/CovariantMetricTensor.h +++ b/include/bout/CovariantMetricTensor.hxx @@ -1,6 +1,6 @@ -#ifndef BOUT_COVARIANTMETRICTENSOR_H -#define BOUT_COVARIANTMETRICTENSOR_H +#ifndef BOUT_COVARIANTMETRICTENSOR_HXX +#define BOUT_COVARIANTMETRICTENSOR_HXX #include "field2d.hxx" #include "bout/field3d.hxx" @@ -47,4 +47,4 @@ class CovariantMetricTensor { CovariantComponents covariant_components; }; -#endif //BOUT_COVARIANTMETRICTENSOR_H +#endif //BOUT_COVARIANTMETRICTENSOR_HXX diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index 70d47afec9..72ae58a785 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -33,8 +33,8 @@ #ifndef BOUT_COORDINATES_H #define BOUT_COORDINATES_H -#include "ContravariantMetricTensor.h" -#include "CovariantMetricTensor.h" +#include "ContravariantMetricTensor.hxx" +#include "CovariantMetricTensor.hxx" #include "bout/field2d.hxx" #include "bout/field3d.hxx" #include "bout/paralleltransform.hxx" diff --git a/include/bout/ContravariantMetricTensor.cpp b/src/mesh/ContravariantMetricTensor.cpp similarity index 99% rename from include/bout/ContravariantMetricTensor.cpp rename to src/mesh/ContravariantMetricTensor.cpp index bb1087cdab..9663dbf71d 100644 --- a/include/bout/ContravariantMetricTensor.cpp +++ b/src/mesh/ContravariantMetricTensor.cpp @@ -1,6 +1,6 @@ -#include "ContravariantMetricTensor.h" -#include "CovariantMetricTensor.h" +#include "bout/ContravariantMetricTensor.hxx" +#include "bout/CovariantMetricTensor.hxx" ContravariantMetricTensor::ContravariantMetricTensor( const FieldMetric g11, const FieldMetric g22, const FieldMetric g33, diff --git a/include/bout/CovariantMetricTensor.cpp b/src/mesh/CovariantMetricTensor.cpp similarity index 98% rename from include/bout/CovariantMetricTensor.cpp rename to src/mesh/CovariantMetricTensor.cpp index 55aff96851..bd197f21c1 100644 --- a/include/bout/CovariantMetricTensor.cpp +++ b/src/mesh/CovariantMetricTensor.cpp @@ -1,8 +1,8 @@ -#include "CovariantMetricTensor.h" -#include "ContravariantMetricTensor.h" -#include -#include +#include "bout/CovariantMetricTensor.hxx" +#include "bout/ContravariantMetricTensor.hxx" +#include "bout/coordinates.hxx" +#include "bout/output.hxx" CovariantMetricTensor::CovariantMetricTensor( const FieldMetric g_11, const FieldMetric g_22, const FieldMetric g_33, diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index a60cf27274..bc1960ccef 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -19,7 +19,7 @@ #include "parallel/fci.hxx" #include "parallel/shiftedmetricinterp.hxx" -#include "bout/ContravariantMetricTensor.h" +#include "bout/ContravariantMetricTensor.hxx" // use anonymous namespace so this utility function is not available outside this file namespace { @@ -500,7 +500,7 @@ Coordinates::Coordinates(Mesh* mesh, Options* options) source_has_component)) { auto covariant_components = covariantMetricTensor.getCovariantMetricTensor(); - + FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; g_11 = getUnaligned(covariant_components.g_11, "g_11", 1.0); g_22 = getUnaligned(covariant_components.g_22, "g_22", 1.0); diff --git a/src/mesh/coordinates_accessor.cxx b/src/mesh/coordinates_accessor.cxx index fbb0e0f55f..47871c0917 100644 --- a/src/mesh/coordinates_accessor.cxx +++ b/src/mesh/coordinates_accessor.cxx @@ -1,6 +1,6 @@ #include "bout/coordinates_accessor.hxx" -#include "bout/ContravariantMetricTensor.h" +#include "bout/ContravariantMetricTensor.hxx" #include "bout/mesh.hxx" #include From 18e3f7aa855009ffd5e33ab23f55a02897b73df3 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Mon, 16 Oct 2023 10:41:05 +0100 Subject: [PATCH 096/491] Remove CovariantComponents struct - individual getters for metric tensor components. --- include/bout/CovariantMetricTensor.hxx | 15 +- include/bout/coordinates.hxx | 7 +- include/bout/fv_ops.hxx | 11 +- src/field/vecops.cxx | 10 +- src/field/vector2d.cxx | 56 +-- src/field/vector3d.cxx | 61 ++- .../impls/multigrid/multigrid_laplace.cxx | 29 +- .../laplace/impls/serial_band/serial_band.cxx | 37 +- src/invert/laplace/invert_laplace.cxx | 54 +-- src/invert/parderiv/impls/cyclic/cyclic.cxx | 8 +- .../pardiv/impls/cyclic/pardiv_cyclic.cxx | 2 +- src/mesh/ContravariantMetricTensor.cpp | 48 +-- src/mesh/CovariantMetricTensor.cpp | 161 ++++---- src/mesh/boundary_standard.cxx | 51 ++- src/mesh/coordinates.cxx | 388 ++++++++++-------- src/mesh/coordinates_accessor.cxx | 22 +- src/mesh/difops.cxx | 53 +-- src/mesh/fv_ops.cxx | 22 +- .../invert/laplace/test_laplace_cyclic.cxx | 13 +- tests/unit/mesh/test_coordinates.cxx | 48 +-- 20 files changed, 540 insertions(+), 556 deletions(-) diff --git a/include/bout/CovariantMetricTensor.hxx b/include/bout/CovariantMetricTensor.hxx index 80436a64c7..03cce5d04f 100644 --- a/include/bout/CovariantMetricTensor.hxx +++ b/include/bout/CovariantMetricTensor.hxx @@ -15,10 +15,6 @@ public: using FieldMetric = Field2D; #endif - struct CovariantComponents { - FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; - }; - CovariantMetricTensor(const FieldMetric g_11, const FieldMetric g_22, const FieldMetric g_33, const FieldMetric g_12, const FieldMetric g_13, const FieldMetric g_23); @@ -34,17 +30,22 @@ public: // check that covariant tensors are positive (if expected) and finite (always) void checkCovariant(int ystart); + FieldMetric Getg_11() const; + FieldMetric Getg_22() const; + FieldMetric Getg_33() const; + FieldMetric Getg_12() const; + FieldMetric Getg_13() const; + FieldMetric Getg_23() const; + void setCovariantMetricTensor(CELL_LOC location, const CovariantMetricTensor& metric_tensor); - CovariantComponents getCovariantMetricTensor() const; - void Allocate(); void setLocation(const CELL_LOC location); private: - CovariantComponents covariant_components; + FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; }; #endif //BOUT_COVARIANTMETRICTENSOR_HXX diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index 72ae58a785..9f79f52201 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -105,7 +105,12 @@ private: public: ContravariantMetricTensor::ContravariantComponents getContravariantMetricTensor() const; - CovariantMetricTensor::CovariantComponents getCovariantMetricTensor() const; + FieldMetric g_11() const; + FieldMetric g_22() const; + FieldMetric g_33() const; + FieldMetric g_12() const; + FieldMetric g_13() const; + FieldMetric g_23() const; void setContravariantMetricTensor(ContravariantMetricTensor metric_tensor); diff --git a/include/bout/fv_ops.hxx b/include/bout/fv_ops.hxx index 37f26bd710..162548d917 100644 --- a/include/bout/fv_ops.hxx +++ b/include/bout/fv_ops.hxx @@ -239,15 +239,13 @@ const Field3D Div_par(const Field3D& f_in, const Field3D& v_in, ye = mesh->yend; } - const auto covariant_components = coord->getCovariantMetricTensor(); - for (int j = ys; j <= ye; j++) { // Pre-calculate factors which multiply fluxes #if not(BOUT_USE_METRIC_3D) // For right cell boundaries - BoutReal common_factor = (coord->J(i, j) + coord->J(i, j + 1)) - / (sqrt(covariant_components.g_22(i, j)) - + sqrt(covariant_components.g_22(i, j + 1))); + BoutReal common_factor = + (coord->J(i, j) + coord->J(i, j + 1)) + / (sqrt(coord->g_22()(i, j)) + sqrt(coord->g_22()(i, j + 1))); BoutReal flux_factor_rc = common_factor / (coord->dy(i, j) * coord->J(i, j)); BoutReal flux_factor_rp = @@ -255,8 +253,7 @@ const Field3D Div_par(const Field3D& f_in, const Field3D& v_in, // For left cell boundaries common_factor = (coord->J(i, j) + coord->J(i, j - 1)) - / (sqrt(covariant_components.g_22(i, j)) - + sqrt(covariant_components.g_22(i, j - 1))); + / (sqrt(coord->g_22()(i, j)) + sqrt(coord->g_22()(i, j - 1))); BoutReal flux_factor_lc = common_factor / (coord->dy(i, j) * coord->J(i, j)); BoutReal flux_factor_lm = diff --git a/src/field/vecops.cxx b/src/field/vecops.cxx index c88bc893df..0e2d59e909 100644 --- a/src/field/vecops.cxx +++ b/src/field/vecops.cxx @@ -105,12 +105,11 @@ Vector3D Grad_perp(const Field3D& f, CELL_LOC outloc, const std::string& method) Vector3D result(f.getMesh()); - const auto covariant_components = metric->getCovariantMetricTensor(); result.x = DDX(f, outloc, method) - - covariant_components.g_12 * DDY(f, outloc, method) / SQ(metric->J * metric->Bxy); + - metric->g_12() * DDY(f, outloc, method) / SQ(metric->J * metric->Bxy); result.y = 0.0; result.z = DDZ(f, outloc, method) - - covariant_components.g_23 * DDY(f, outloc, method) / SQ(metric->J * metric->Bxy); + - metric->g_23() * DDY(f, outloc, method) / SQ(metric->J * metric->Bxy); result.setLocation(result.x.getLocation()); @@ -128,11 +127,10 @@ Vector2D Grad_perp(const Field2D& f, CELL_LOC outloc, const std::string& method) Vector2D result(f.getMesh()); - const auto covariant_components = metric->getCovariantMetricTensor(); result.x = DDX(f, outloc, method) - - covariant_components.g_12 * DDY(f, outloc, method) / SQ(metric->J * metric->Bxy); + - metric->g_12() * DDY(f, outloc, method) / SQ(metric->J * metric->Bxy); result.y = 0.0; - result.z = - covariant_components.g_23 * DDY(f, outloc, method) / SQ(metric->J * metric->Bxy); + result.z = -metric->g_23() * DDY(f, outloc, method) / SQ(metric->J * metric->Bxy); result.setLocation(result.x.getLocation()); diff --git a/src/field/vector2d.cxx b/src/field/vector2d.cxx index bc38515b73..ae01866798 100644 --- a/src/field/vector2d.cxx +++ b/src/field/vector2d.cxx @@ -83,19 +83,15 @@ void Vector2D::toCovariant() { const auto z_at_y = interp_to(z, y.getLocation()); const auto x_at_z = interp_to(x, z.getLocation()); const auto y_at_z = interp_to(y, z.getLocation()); - - const auto covariant_components_x = metric_x->getCovariantMetricTensor(); - const auto covariant_components_y = metric_y->getCovariantMetricTensor(); - const auto covariant_components_z = metric_z->getCovariantMetricTensor(); - + // multiply by g_{ij} BOUT_FOR(i, x.getRegion("RGN_ALL")) { - x[i] = covariant_components_x.g_11[i] * x[i] + covariant_components_x.g_12[i] * y_at_x[i] - + covariant_components_x.g_13[i] * z_at_x[i]; - y[i] = covariant_components_y.g_22[i] * y[i] + covariant_components_y.g_12[i] * x_at_y[i] - + covariant_components_y.g_23[i] * z_at_y[i]; - z[i] = covariant_components_z.g_33[i] * z[i] + covariant_components_z.g_13[i] * x_at_z[i] - + covariant_components_z.g_23[i] * y_at_z[i]; + x[i] = metric_x->g_11()[i] * x[i] + metric_x->g_12()[i] * y_at_x[i] + + metric_x->g_13()[i] * z_at_x[i]; + y[i] = metric_x->g_22()[i] * y[i] + metric_x->g_12()[i] * x_at_y[i] + + metric_x->g_23()[i] * z_at_y[i]; + z[i] = metric_x->g_33()[i] * z[i] + metric_x->g_13()[i] * x_at_z[i] + + metric_x->g_23()[i] * y_at_z[i]; }; } else { const auto metric = localmesh->getCoordinates(location); @@ -103,11 +99,13 @@ void Vector2D::toCovariant() { // Need to use temporary arrays to store result Coordinates::FieldMetric gx{emptyFrom(x)}, gy{emptyFrom(y)}, gz{emptyFrom(z)}; - const auto covariant_components = metric->getCovariantMetricTensor(); BOUT_FOR(i, x.getRegion("RGN_ALL")) { - gx[i] = covariant_components.g_11[i] * x[i] + covariant_components.g_12[i] * y[i] + covariant_components.g_13[i] * z[i]; - gy[i] = covariant_components.g_22[i] * y[i] + covariant_components.g_12[i] * x[i] + covariant_components.g_23[i] * z[i]; - gz[i] = covariant_components.g_33[i] * z[i] + covariant_components.g_13[i] * x[i] + covariant_components.g_23[i] * y[i]; + gx[i] = metric->g_11()[i] * x[i] + metric->g_12()[i] * y[i] + + metric->g_13()[i] * z[i]; + gy[i] = metric->g_22()[i] * y[i] + metric->g_12()[i] * x[i] + + metric->g_23()[i] * z[i]; + gz[i] = metric->g_33()[i] * z[i] + metric->g_13()[i] * x[i] + + metric->g_23()[i] * y[i]; }; x = gx; @@ -163,9 +161,15 @@ void Vector2D::toContravariant() { const auto contravariant_components = metric->getContravariantMetricTensor(); BOUT_FOR(i, x.getRegion("RGN_ALL")) { - gx[i] = contravariant_components.g11[i] * x[i] + contravariant_components.g12[i] * y[i] + contravariant_components.g13[i] * z[i]; - gy[i] = contravariant_components.g22[i] * y[i] + contravariant_components.g12[i] * x[i] + contravariant_components.g23[i] * z[i]; - gz[i] = contravariant_components.g33[i] * z[i] + contravariant_components.g13[i] * x[i] + contravariant_components.g23[i] * y[i]; + gx[i] = contravariant_components.g11[i] * x[i] + + contravariant_components.g12[i] * y[i] + + contravariant_components.g13[i] * z[i]; + gy[i] = contravariant_components.g22[i] * y[i] + + contravariant_components.g12[i] * x[i] + + contravariant_components.g23[i] * z[i]; + gz[i] = contravariant_components.g33[i] * z[i] + + contravariant_components.g13[i] * x[i] + + contravariant_components.g23[i] * y[i]; }; x = gx; @@ -400,19 +404,19 @@ const Coordinates::FieldMetric Vector2D::operator*(const Vector2D& rhs) const { // Both covariant const auto contravariant_components = metric->getContravariantMetricTensor(); - result = - x * rhs.x * contravariant_components.g11 + y * rhs.y * contravariant_components.g22 + z * rhs.z * contravariant_components.g33; + result = x * rhs.x * contravariant_components.g11 + + y * rhs.y * contravariant_components.g22 + + z * rhs.z * contravariant_components.g33; result += (x * rhs.y + y * rhs.x) * contravariant_components.g12 + (x * rhs.z + z * rhs.x) * contravariant_components.g13 + (y * rhs.z + z * rhs.y) * contravariant_components.g23; } else { // Both contravariant - const auto covariant_components = metric->getCovariantMetricTensor(); - result = - x * rhs.x * covariant_components.g_11 + y * rhs.y * covariant_components.g_22 + z * rhs.z * covariant_components.g_33; - result += (x * rhs.y + y * rhs.x) * covariant_components.g_12 - + (x * rhs.z + z * rhs.x) * covariant_components.g_13 - + (y * rhs.z + z * rhs.y) * covariant_components.g_23; + result = x * rhs.x * metric->g_11() + y * rhs.y * metric->g_22() + + z * rhs.z * metric->g_33(); + result += (x * rhs.y + y * rhs.x) * metric->g_12() + + (x * rhs.z + z * rhs.x) * metric->g_13() + + (y * rhs.z + z * rhs.y) * metric->g_23(); } } diff --git a/src/field/vector3d.cxx b/src/field/vector3d.cxx index f8374aa21b..5ec8a8faae 100644 --- a/src/field/vector3d.cxx +++ b/src/field/vector3d.cxx @@ -85,18 +85,14 @@ void Vector3D::toCovariant() { const auto x_at_z = interp_to(x, z.getLocation()); const auto y_at_z = interp_to(y, z.getLocation()); - const auto g_x = metric_x->getCovariantMetricTensor(); - const auto g_y = metric_y->getCovariantMetricTensor(); - const auto g_z = metric_z->getCovariantMetricTensor(); - // multiply by g_{ij} BOUT_FOR(i, localmesh->getRegion3D("RGN_ALL")) { - x[i] = g_x.g_11[i] * x[i] + g_x.g_12[i] * y_at_x[i] - + g_x.g_13[i] * z_at_x[i]; - y[i] = g_y.g_22[i] * y[i] + g_y.g_12[i] * x_at_y[i] - + g_y.g_23[i] * z_at_y[i]; - z[i] = g_z.g_33[i] * z[i] + g_z.g_13[i] * x_at_z[i] - + g_z.g_23[i] * y_at_z[i]; + x[i] = metric_x->g_11()[i] * x[i] + metric_x->g_12()[i] * y_at_x[i] + + metric_x->g_13()[i] * z_at_x[i]; + y[i] = metric_y->g_22()[i] * y[i] + metric_y->g_12()[i] * x_at_y[i] + + metric_y->g_23()[i] * z_at_y[i]; + z[i] = metric_z->g_33()[i] * z[i] + metric_z->g_13()[i] * x_at_z[i] + + metric_z->g_23()[i] * y_at_z[i]; }; } else { const auto metric = localmesh->getCoordinates(location); @@ -104,12 +100,13 @@ void Vector3D::toCovariant() { // Need to use temporary arrays to store result Field3D gx{emptyFrom(x)}, gy{emptyFrom(y)}, gz{emptyFrom(z)}; - const auto covariant_components = metric->getCovariantMetricTensor(); - BOUT_FOR(i, localmesh->getRegion3D("RGN_ALL")) { - gx[i] = covariant_components.g_11[i] * x[i] + covariant_components.g_12[i] * y[i] + covariant_components.g_13[i] * z[i]; - gy[i] = covariant_components.g_22[i] * y[i] + covariant_components.g_12[i] * x[i] + covariant_components.g_23[i] * z[i]; - gz[i] = covariant_components.g_33[i] * z[i] + covariant_components.g_13[i] * x[i] + covariant_components.g_23[i] * y[i]; + gx[i] = metric->g_11()[i] * x[i] + metric->g_12()[i] * y[i] + + metric->g_13()[i] * z[i]; + gy[i] = metric->g_22()[i] * y[i] + metric->g_12()[i] * x[i] + + metric->g_23()[i] * z[i]; + gz[i] = metric->g_33()[i] * z[i] + metric->g_13()[i] * x[i] + + metric->g_23()[i] * y[i]; }; x = gx; @@ -492,19 +489,16 @@ const Field3D Vector3D::operator*(const Vector3D& rhs) const { if (covariant) { // Both covariant const auto g = metric->getContravariantMetricTensor(); - result = - x * rhs.x * g.g11 + y * rhs.y * g.g22 + z * rhs.z * g.g33; - result += (x * rhs.y + y * rhs.x) * g.g12 - + (x * rhs.z + z * rhs.x) * g.g13 + result = x * rhs.x * g.g11 + y * rhs.y * g.g22 + z * rhs.z * g.g33; + result += (x * rhs.y + y * rhs.x) * g.g12 + (x * rhs.z + z * rhs.x) * g.g13 + (y * rhs.z + z * rhs.y) * g.g23; } else { // Both contravariant - const auto covariant_components = metric->getCovariantMetricTensor(); - result = - x * rhs.x * covariant_components.g_11 + y * rhs.y * covariant_components.g_22 + z * rhs.z * covariant_components.g_33; - result += (x * rhs.y + y * rhs.x) * covariant_components.g_12 - + (x * rhs.z + z * rhs.x) * covariant_components.g_13 - + (y * rhs.z + z * rhs.y) * covariant_components.g_23; + result = x * rhs.x * metric->g_11() + y * rhs.y * metric->g_22() + + z * rhs.z * metric->g_33(); + result += (x * rhs.y + y * rhs.x) * metric->g_12() + + (x * rhs.z + z * rhs.x) * metric->g_13() + + (y * rhs.z + z * rhs.y) * metric->g_23(); } } @@ -526,19 +520,16 @@ const Field3D Vector3D::operator*(const Vector2D& rhs) const { if (covariant) { // Both covariant const auto g = metric->getContravariantMetricTensor(); - result = - x * rhs.x * g.g11 + y * rhs.y * g.g22 + z * rhs.z * g.g33; - result += (x * rhs.y + y * rhs.x) * g.g12 - + (x * rhs.z + z * rhs.x) * g.g13 + result = x * rhs.x * g.g11 + y * rhs.y * g.g22 + z * rhs.z * g.g33; + result += (x * rhs.y + y * rhs.x) * g.g12 + (x * rhs.z + z * rhs.x) * g.g13 + (y * rhs.z + z * rhs.y) * g.g23; } else { // Both contravariant - const auto covariant_components = metric->getCovariantMetricTensor(); - result = - x * rhs.x * covariant_components.g_11 + y * rhs.y * covariant_components.g_22 + z * rhs.z * covariant_components.g_33; - result += (x * rhs.y + y * rhs.x) * covariant_components.g_12 - + (x * rhs.z + z * rhs.x) * covariant_components.g_13 - + (y * rhs.z + z * rhs.y) * covariant_components.g_23; + result = x * rhs.x * metric->g_11() + y * rhs.y * metric->g_22() + + z * rhs.z * metric->g_33(); + result += (x * rhs.y + y * rhs.x) * metric->g_12() + + (x * rhs.z + z * rhs.x) * metric->g_13() + + (y * rhs.z + z * rhs.y) * metric->g_23(); } } diff --git a/src/invert/laplace/impls/multigrid/multigrid_laplace.cxx b/src/invert/laplace/impls/multigrid/multigrid_laplace.cxx index c34a0002e0..6742b6c9bf 100644 --- a/src/invert/laplace/impls/multigrid/multigrid_laplace.cxx +++ b/src/invert/laplace/impls/multigrid/multigrid_laplace.cxx @@ -280,13 +280,12 @@ FieldPerp LaplaceMultigrid::solve(const FieldPerp& b_in, const FieldPerp& x0) { // Neumann boundary condition if (inner_boundary_flags & INVERT_SET) { // guard cells of x0 specify gradient to set at inner boundary - const auto covariant_components = coords->getCovariantMetricTensor(); BOUT_OMP_PERF(parallel default(shared)) BOUT_OMP_PERF(for) for (int k = 1; k < lzz + 1; k++) { int k2 = k - 1; x[k] = -x0(localmesh->xstart - 1, k2) - * sqrt(covariant_components.g_11(localmesh->xstart, yindex)) + * sqrt(coords->g_11()(localmesh->xstart, yindex)) * coords->dx(localmesh->xstart, yindex); } } else { @@ -325,13 +324,12 @@ FieldPerp LaplaceMultigrid::solve(const FieldPerp& b_in, const FieldPerp& x0) { // Neumann boundary condition if (inner_boundary_flags & INVERT_SET) { // guard cells of x0 specify gradient to set at outer boundary - const auto covariant_components = coords->getCovariantMetricTensor(); BOUT_OMP_PERF(parallel default(shared)) BOUT_OMP_PERF(for) for (int k = 1; k < lzz + 1; k++) { int k2 = k - 1; x[(lxx + 1) * lz2 + k] = x0(localmesh->xend + 1, k2) - * sqrt(covariant_components.g_11(localmesh->xend, yindex)) + * sqrt(coords->g_11()(localmesh->xend, yindex)) * coords->dx(localmesh->xend, yindex); // this is the value to set the gradient to at the outer boundary } @@ -483,7 +481,6 @@ FieldPerp LaplaceMultigrid::solve(const FieldPerp& b_in, const FieldPerp& x0) { // Neumann boundary condition if (inner_boundary_flags & INVERT_SET) { // guard cells of x0 specify gradient to set at inner boundary - const auto covariant_components = coords->getCovariantMetricTensor(); int i2 = -1 + localmesh->xstart; BOUT_OMP_PERF(parallel default(shared)) BOUT_OMP_PERF(for) @@ -491,7 +488,7 @@ FieldPerp LaplaceMultigrid::solve(const FieldPerp& b_in, const FieldPerp& x0) { int k2 = k - 1; result(i2, k2) = x[lz2 + k] - x0(localmesh->xstart - 1, k2) - * sqrt(covariant_components.g_11(localmesh->xstart, yindex)) + * sqrt(coords->g_11()(localmesh->xstart, yindex)) * coords->dx(localmesh->xstart, yindex); } } else { @@ -532,7 +529,6 @@ FieldPerp LaplaceMultigrid::solve(const FieldPerp& b_in, const FieldPerp& x0) { // Neumann boundary condition if (inner_boundary_flags & INVERT_SET) { // guard cells of x0 specify gradient to set at outer boundary - const auto covariant_components = coords->getCovariantMetricTensor(); int i2 = lxx + localmesh->xstart; BOUT_OMP_PERF(parallel default(shared)) BOUT_OMP_PERF(for) @@ -540,7 +536,7 @@ FieldPerp LaplaceMultigrid::solve(const FieldPerp& b_in, const FieldPerp& x0) { int k2 = k - 1; result(i2, k2) = x[lxx * lz2 + k] + x0(localmesh->xend + 1, k2) - * sqrt(covariant_components.g_11(localmesh->xend, yindex)) + * sqrt(coords->g_11()(localmesh->xend, yindex)) * coords->dx(localmesh->xend, yindex); } } else { @@ -609,20 +605,22 @@ void LaplaceMultigrid::generateMatrixF(int level) { BoutReal ddz_C = (C2(i2, yindex, k2p) - C2(i2, yindex, k2m)) / 2. / dz / C1(i2, yindex, k2); - BoutReal ddx = D(i2, yindex, k2) * contravariant_components.g11(i2, yindex) / coords->dx(i2, yindex) - / coords->dx(i2, yindex); + BoutReal ddx = D(i2, yindex, k2) * contravariant_components.g11(i2, yindex) + / coords->dx(i2, yindex) / coords->dx(i2, yindex); // coefficient of 2nd derivative stencil (x-direction) - BoutReal ddz = D(i2, yindex, k2) * contravariant_components.g33(i2, yindex) / SQ(dz); + BoutReal ddz = + D(i2, yindex, k2) * contravariant_components.g33(i2, yindex) / SQ(dz); // coefficient of 2nd derivative stencil (z-direction) - BoutReal dxdz = - D(i2, yindex, k2) * 2. * contravariant_components.g13(i2, yindex) / coords->dx(i2, yindex) / dz; + BoutReal dxdz = D(i2, yindex, k2) * 2. * contravariant_components.g13(i2, yindex) + / coords->dx(i2, yindex) / dz; // coefficient of mixed derivative stencil (could assume zero, at least initially, // if easier; then check this is true in constructor) BoutReal dxd = - (D(i2, yindex, k2) * coords->G1(i2, yindex) + contravariant_components.g11(i2, yindex) * ddx_C + (D(i2, yindex, k2) * coords->G1(i2, yindex) + + contravariant_components.g11(i2, yindex) * ddx_C + contravariant_components.g13(i2, yindex) * ddz_C // (could assume zero, at least initially, if easier; then check this is true in constructor) ) @@ -633,7 +631,8 @@ void LaplaceMultigrid::generateMatrixF(int level) { } BoutReal dzd = - (D(i2, yindex, k2) * coords->G3(i2, yindex) + contravariant_components.g33(i2, yindex) * ddz_C + (D(i2, yindex, k2) * coords->G3(i2, yindex) + + contravariant_components.g33(i2, yindex) * ddz_C + contravariant_components.g13(i2, yindex) * ddx_C // (could assume zero, at least initially, if easier; then check // this is true in constructor) diff --git a/src/invert/laplace/impls/serial_band/serial_band.cxx b/src/invert/laplace/impls/serial_band/serial_band.cxx index 02412553f8..c60c9b671f 100644 --- a/src/invert/laplace/impls/serial_band/serial_band.cxx +++ b/src/invert/laplace/impls/serial_band/serial_band.cxx @@ -162,9 +162,9 @@ FieldPerp LaplaceSerialBand::solve(const FieldPerp& b, const FieldPerp& x0) { coef1 = g.g11(ix, jy); // X 2nd derivative coef2 = g.g33(ix, jy); // Z 2nd derivative coef3 = g.g13(ix, jy); // X-Z mixed derivatives - coef4 = 0.0; // X 1st derivative - coef5 = 0.0; // Z 1st derivative - coef6 = Acoef(ix, jy); // Constant + coef4 = 0.0; // X 1st derivative + coef5 = 0.0; // Z 1st derivative + coef6 = Acoef(ix, jy); // Constant // Multiply Delp2 component by a factor coef1 *= Dcoef(ix, jy); @@ -266,8 +266,6 @@ FieldPerp LaplaceSerialBand::solve(const FieldPerp& b, const FieldPerp& x0) { if (iz == 0) { // DC - const auto covariant_components = coords->getCovariantMetricTensor(); - // Inner boundary if (inner_boundary_flags & (INVERT_DC_GRAD + INVERT_SET) || inner_boundary_flags & (INVERT_DC_GRAD + INVERT_RHS)) { @@ -276,8 +274,8 @@ FieldPerp LaplaceSerialBand::solve(const FieldPerp& b, const FieldPerp& x0) { for (int ix = 0; ix < xbndry; ix++) { A(ix, 0) = 0.; A(ix, 1) = 0.; - A(ix, 2) = -.5 / sqrt(covariant_components.g_11(ix, jy)) / coords->dx(ix, jy); - A(ix, 3) = .5 / sqrt(covariant_components.g_11(ix, jy)) / coords->dx(ix, jy); + A(ix, 2) = -.5 / sqrt(coords->g_11()(ix, jy)) / coords->dx(ix, jy); + A(ix, 3) = .5 / sqrt(coords->g_11()(ix, jy)) / coords->dx(ix, jy); A(ix, 4) = 0.; } @@ -296,17 +294,17 @@ FieldPerp LaplaceSerialBand::solve(const FieldPerp& b, const FieldPerp& x0) { for (int ix = 0; ix < xbndry; ix++) { A(ix, 0) = 0.; A(ix, 1) = 0.; - A(ix, 2) = -3. / sqrt(covariant_components.g_22(ix, jy)); - A(ix, 3) = 4. / sqrt(covariant_components.g_22(ix + 1, jy)); - A(ix, 4) = -1. / sqrt(covariant_components.g_22(ix + 2, jy)); + A(ix, 2) = -3. / sqrt(coords->g_22()(ix, jy)); + A(ix, 3) = 4. / sqrt(coords->g_22()(ix + 1, jy)); + A(ix, 4) = -1. / sqrt(coords->g_22()(ix + 2, jy)); } } else if (inner_boundary_flags & INVERT_DC_GRADPARINV) { for (int ix = 0; ix < xbndry; ix++) { A(ix, 0) = 0.; A(ix, 1) = 0.; - A(ix, 2) = -3. * sqrt(covariant_components.g_22(ix, jy)); - A(ix, 3) = 4. * sqrt(covariant_components.g_22(ix + 1, jy)); - A(ix, 4) = -sqrt(covariant_components.g_22(ix + 2, jy)); + A(ix, 2) = -3. * sqrt(coords->g_22()(ix, jy)); + A(ix, 3) = 4. * sqrt(coords->g_22()(ix + 1, jy)); + A(ix, 4) = -sqrt(coords->g_22()(ix + 2, jy)); } } else if (inner_boundary_flags & INVERT_DC_LAP) { for (int ix = 0; ix < xbndry; ix++) { @@ -353,9 +351,7 @@ FieldPerp LaplaceSerialBand::solve(const FieldPerp& b, const FieldPerp& x0) { // Combine 4th order at 1 with 2nd order at 0 A(1, 0) = 0.0; // Not used A(1, 1) = dcomplex( - (14. - - SQ(coords->dx(0, jy) * kwave) * g.g33(0, jy) / g.g11(0, jy)) - * coef1, + (14. - SQ(coords->dx(0, jy) * kwave) * g.g33(0, jy) / g.g11(0, jy)) * coef1, -coef3); A(1, 2) = dcomplex(-29. * coef1 - SQ(kwave) * coef2 + coef4, 0.0); A(1, 3) = dcomplex(16. * coef1, coef3); @@ -400,11 +396,10 @@ FieldPerp LaplaceSerialBand::solve(const FieldPerp& b, const FieldPerp& x0) { A(ix, 0) = dcomplex(-coef1, 0.0); A(ix, 1) = dcomplex(16. * coef1, -coef3); A(ix, 2) = dcomplex(-29. * coef1 - SQ(kwave) * coef2 + coef4, 0.0); - A(ix, 3) = dcomplex((14. - - SQ(coords->dx(ncx, jy) * kwave) * g.g33(ncx, jy) - / g.g11(ncx, jy)) - * coef1, - coef3); + A(ix, 3) = dcomplex( + (14. - SQ(coords->dx(ncx, jy) * kwave) * g.g33(ncx, jy) / g.g11(ncx, jy)) + * coef1, + coef3); A(ix, 4) = 0.0; // Not used coef1 = g.g11(ix, jy) / (SQ(coords->dx(ix, jy))); diff --git a/src/invert/laplace/invert_laplace.cxx b/src/invert/laplace/invert_laplace.cxx index 0651f9e395..2cd87212d1 100644 --- a/src/invert/laplace/invert_laplace.cxx +++ b/src/invert/laplace/invert_laplace.cxx @@ -495,7 +495,6 @@ void Laplacian::tridagMatrix(dcomplex* avec, dcomplex* bvec, dcomplex* cvec, dco // Set the boundary conditions if x is not periodic if (!localmesh->periodicX) { const auto contravariant_components = coords->getContravariantMetricTensor(); - const auto covariant_components = coords->getCovariantMetricTensor(); if (localmesh->firstX()) { // INNER BOUNDARY ON THIS PROCESSOR @@ -515,8 +514,8 @@ void Laplacian::tridagMatrix(dcomplex* avec, dcomplex* bvec, dcomplex* cvec, dco // Zero gradient at inner boundary for (int ix = 0; ix < inbndry; ix++) { avec[ix] = 0.; - bvec[ix] = -1. / sqrt(covariant_components.g_11(ix, jy)) / coords->dx(ix, jy); - cvec[ix] = 1. / sqrt(covariant_components.g_11(ix, jy)) / coords->dx(ix, jy); + bvec[ix] = -1. / sqrt(coords->g_11()(ix, jy)) / coords->dx(ix, jy); + cvec[ix] = 1. / sqrt(coords->g_11()(ix, jy)) / coords->dx(ix, jy); } } else if (inner_boundary_flags & INVERT_DC_GRAD) { // Zero gradient at inner boundary @@ -528,14 +527,14 @@ void Laplacian::tridagMatrix(dcomplex* avec, dcomplex* bvec, dcomplex* cvec, dco } else if (inner_boundary_flags & INVERT_DC_GRADPAR) { for (int ix = 0; ix < inbndry; ix++) { avec[ix] = 0.0; - bvec[ix] = 1.0 / sqrt(covariant_components.g_22(ix, jy)); - cvec[ix] = -1.0 / sqrt(covariant_components.g_22(ix + 1, jy)); + bvec[ix] = 1.0 / sqrt(coords->g_22()(ix, jy)); + cvec[ix] = -1.0 / sqrt(coords->g_22()(ix + 1, jy)); } } else if (inner_boundary_flags & INVERT_DC_GRADPARINV) { for (int ix = 0; ix < inbndry; ix++) { avec[ix] = 0.0; - bvec[ix] = sqrt(covariant_components.g_22(ix, jy)); - cvec[ix] = -sqrt(covariant_components.g_22(ix + 1, jy)); + bvec[ix] = sqrt(coords->g_22()(ix, jy)); + cvec[ix] = -sqrt(coords->g_22()(ix + 1, jy)); } } else if (inner_boundary_flags & INVERT_DC_LAP) { // Decaying boundary conditions @@ -550,7 +549,8 @@ void Laplacian::tridagMatrix(dcomplex* avec, dcomplex* bvec, dcomplex* cvec, dco for (int ix = 0; ix < inbndry; ix++) { avec[ix] = 0.; bvec[ix] = 1.; - cvec[ix] = -exp(-k * coords->dx(ix, jy) / sqrt(contravariant_components.g11(ix, jy))); + cvec[ix] = -exp(-k * coords->dx(ix, jy) + / sqrt(contravariant_components.g11(ix, jy))); } } else if (inner_boundary_flags & INVERT_IN_CYLINDER) { // Condition for inner radial boundary for cylindrical coordinates @@ -612,8 +612,9 @@ void Laplacian::tridagMatrix(dcomplex* avec, dcomplex* bvec, dcomplex* cvec, dco for (int ix = 0; ix < inbndry; ix++) { avec[ix] = dcomplex(0., 0.); bvec[ix] = - dcomplex(-1., 0.) / sqrt(covariant_components.g_11(ix, jy)) / coords->dx(ix, jy); - cvec[ix] = dcomplex(1., 0.) / sqrt(covariant_components.g_11(ix, jy)) / coords->dx(ix, jy); + dcomplex(-1., 0.) / sqrt(coords->g_11()(ix, jy)) / coords->dx(ix, jy); + cvec[ix] = + dcomplex(1., 0.) / sqrt(coords->g_11()(ix, jy)) / coords->dx(ix, jy); } } else if (inner_boundary_flags & INVERT_AC_GRAD) { // Zero gradient at inner boundary @@ -627,8 +628,10 @@ void Laplacian::tridagMatrix(dcomplex* avec, dcomplex* bvec, dcomplex* cvec, dco for (int ix = 0; ix < inbndry; ix++) { avec[ix] = 0.0; bvec[ix] = 1.0; - cvec[ix] = -exp(-1.0 * sqrt(contravariant_components.g33(ix, jy) / contravariant_components.g11(ix, jy)) * kwave - * coords->dx(ix, jy)); + cvec[ix] = -exp(-1.0 + * sqrt(contravariant_components.g33(ix, jy) + / contravariant_components.g11(ix, jy)) + * kwave * coords->dx(ix, jy)); } } else if (inner_boundary_flags & INVERT_IN_CYLINDER) { // Condition for inner radial boundary for cylindrical coordinates @@ -672,9 +675,9 @@ void Laplacian::tridagMatrix(dcomplex* avec, dcomplex* bvec, dcomplex* cvec, dco && (outer_boundary_flags & INVERT_SET || outer_boundary_flags & INVERT_RHS)) { // Zero gradient at outer boundary for (int ix = 0; ix < outbndry; ix++) { - avec[ncx - ix] = dcomplex(-1., 0.) / sqrt(covariant_components.g_11(ncx - ix, jy)) + avec[ncx - ix] = dcomplex(-1., 0.) / sqrt(coords->g_11()(ncx - ix, jy)) / coords->dx(ncx - ix, jy); - bvec[ncx - ix] = dcomplex(1., 0.) / sqrt(covariant_components.g_11(ncx - ix, jy)) + bvec[ncx - ix] = dcomplex(1., 0.) / sqrt(coords->g_11()(ncx - ix, jy)) / coords->dx(ncx - ix, jy); cvec[ncx - ix] = dcomplex(0., 0.); } @@ -687,14 +690,14 @@ void Laplacian::tridagMatrix(dcomplex* avec, dcomplex* bvec, dcomplex* cvec, dco } } else if (inner_boundary_flags & INVERT_DC_GRADPAR) { for (int ix = 0; ix < inbndry; ix++) { - avec[ncx - ix] = 1.0 / sqrt(covariant_components.g_22(ncx - ix + 1, jy)); - bvec[ncx - ix] = -1.0 / sqrt(covariant_components.g_22(ncx - ix, jy)); + avec[ncx - ix] = 1.0 / sqrt(coords->g_22()(ncx - ix + 1, jy)); + bvec[ncx - ix] = -1.0 / sqrt(coords->g_22()(ncx - ix, jy)); cvec[ncx - ix] = 0.0; } } else if (inner_boundary_flags & INVERT_DC_GRADPARINV) { for (int ix = 0; ix < inbndry; ix++) { - avec[ncx - ix] = sqrt(covariant_components.g_22(ncx - ix - 1, jy)); - bvec[ncx - ix] = -sqrt(covariant_components.g_22(ncx - ix, jy)); + avec[ncx - ix] = sqrt(coords->g_22()(ncx - ix - 1, jy)); + bvec[ncx - ix] = -sqrt(coords->g_22()(ncx - ix, jy)); cvec[ncx - ix] = 0.0; } } else if (inner_boundary_flags & INVERT_DC_LAP) { @@ -710,8 +713,8 @@ void Laplacian::tridagMatrix(dcomplex* avec, dcomplex* bvec, dcomplex* cvec, dco for (int ix = 0; ix < inbndry; ix++) { cvec[ncx - ix] = 0.; bvec[ncx - ix] = 1.; - avec[ncx - ix] = - -exp(-k * coords->dx(ncx - ix, jy) / sqrt(contravariant_components.g11(ncx - ix, jy))); + avec[ncx - ix] = -exp(-k * coords->dx(ncx - ix, jy) + / sqrt(contravariant_components.g11(ncx - ix, jy))); } } else { // Order 2 dirichlet BC (boundary half between points) @@ -730,9 +733,9 @@ void Laplacian::tridagMatrix(dcomplex* avec, dcomplex* bvec, dcomplex* cvec, dco && (outer_boundary_flags & INVERT_SET || outer_boundary_flags & INVERT_RHS)) { // Zero gradient at outer boundary for (int ix = 0; ix < outbndry; ix++) { - avec[ncx - ix] = dcomplex(-1., 0.) / sqrt(covariant_components.g_11(ncx - ix, jy)) + avec[ncx - ix] = dcomplex(-1., 0.) / sqrt(coords->g_11()(ncx - ix, jy)) / coords->dx(ncx - ix, jy); - bvec[ncx - ix] = dcomplex(1., 0.) / sqrt(covariant_components.g_11(ncx - ix, jy)) + bvec[ncx - ix] = dcomplex(1., 0.) / sqrt(coords->g_11()(ncx - ix, jy)) / coords->dx(ncx - ix, jy); cvec[ncx - ix] = dcomplex(0., 0.); } @@ -746,9 +749,10 @@ void Laplacian::tridagMatrix(dcomplex* avec, dcomplex* bvec, dcomplex* cvec, dco } else if (outer_boundary_flags & INVERT_AC_LAP) { // Use decaying zero-Laplacian solution in the boundary for (int ix = 0; ix < outbndry; ix++) { - avec[ncx - ix] = - -exp(-1.0 * sqrt(contravariant_components.g33(xe - ix, jy) / contravariant_components.g11(xe - ix, jy)) - * kwave * coords->dx(xe - ix, jy)); + avec[ncx - ix] = -exp(-1.0 + * sqrt(contravariant_components.g33(xe - ix, jy) + / contravariant_components.g11(xe - ix, jy)) + * kwave * coords->dx(xe - ix, jy)); bvec[ncx - ix] = 1.0; cvec[ncx - ix] = 0.0; } diff --git a/src/invert/parderiv/impls/cyclic/cyclic.cxx b/src/invert/parderiv/impls/cyclic/cyclic.cxx index 67e7eb6863..55eb785cea 100644 --- a/src/invert/parderiv/impls/cyclic/cyclic.cxx +++ b/src/invert/parderiv/impls/cyclic/cyclic.cxx @@ -58,7 +58,7 @@ InvertParCR::InvertParCR(Options* opt, CELL_LOC location, Mesh* mesh_in) // Number of k equations to solve for each x location nsys = 1 + (localmesh->LocalNz) / 2; - sg = sqrt(localmesh->getCoordinates(location)->getCovariantMetricTensor().g_22); + sg = sqrt(localmesh->getCoordinates(location)->g_22()); sg = DDY(1. / sg) / sg; } @@ -154,9 +154,9 @@ const Field3D InvertParCR::solve(const Field3D& f) { BoutReal acoef = A(x, y + local_ystart); // Constant BoutReal bcoef = - B(x, y + local_ystart) / coord->getCovariantMetricTensor().g_22(x, y + local_ystart); // d2dy2 - BoutReal ccoef = C(x, y + local_ystart); // d2dydz - BoutReal dcoef = D(x, y + local_ystart); // d2dz2 + B(x, y + local_ystart) / coord->g_22()(x, y + local_ystart); // d2dy2 + BoutReal ccoef = C(x, y + local_ystart); // d2dydz + BoutReal dcoef = D(x, y + local_ystart); // d2dz2 BoutReal ecoef = E(x, y + local_ystart) + sg(x, y + local_ystart) * B(x, y + local_ystart); // ddy diff --git a/src/invert/pardiv/impls/cyclic/pardiv_cyclic.cxx b/src/invert/pardiv/impls/cyclic/pardiv_cyclic.cxx index 5a30aa3830..41f17ca597 100644 --- a/src/invert/pardiv/impls/cyclic/pardiv_cyclic.cxx +++ b/src/invert/pardiv/impls/cyclic/pardiv_cyclic.cxx @@ -106,7 +106,7 @@ Field3D InvertParDivCR::solve(const Field3D& f) { const Field2D dy = coord->dy; const Field2D J = coord->J; - const Field2D g_22 = coord->getCovariantMetricTensor().g_22; + const Field2D g_22 = coord->g_22(); const auto zlength = getUniform(coord->zlength()); // Loop over flux-surfaces diff --git a/src/mesh/ContravariantMetricTensor.cpp b/src/mesh/ContravariantMetricTensor.cpp index 9663dbf71d..c737fb1376 100644 --- a/src/mesh/ContravariantMetricTensor.cpp +++ b/src/mesh/ContravariantMetricTensor.cpp @@ -76,36 +76,36 @@ ContravariantMetricTensor::calcCovariant(const CELL_LOC location, CovariantMetricTensor covariantMetricTensor = CovariantMetricTensor(a(0, 0), a(1, 1), a(2, 2), a(0, 1), a(0, 2), a(1, 2), mesh); - setLocation(location); + this->setLocation(location); covariantMetricTensor.setLocation(location); - auto covariant_components = covariantMetricTensor.getCovariantMetricTensor(); - BoutReal maxerr; - maxerr = BOUTMAX(max(abs((covariant_components.g_11 * contravariant_components.g11 - + covariant_components.g_12 * contravariant_components.g12 - + covariant_components.g_13 * contravariant_components.g13) - - 1)), - max(abs((covariant_components.g_12 * contravariant_components.g12 - + covariant_components.g_22 * contravariant_components.g22 - + covariant_components.g_23 * contravariant_components.g23) - - 1)), - max(abs((covariant_components.g_13 * contravariant_components.g13 - + covariant_components.g_23 * contravariant_components.g23 - + covariant_components.g_33 * contravariant_components.g33) - - 1))); + maxerr = + BOUTMAX(max(abs((covariantMetricTensor.Getg_11() * contravariant_components.g11 + + covariantMetricTensor.Getg_12() * contravariant_components.g12 + + covariantMetricTensor.Getg_13() * contravariant_components.g13) + - 1)), + max(abs((covariantMetricTensor.Getg_12() * contravariant_components.g12 + + covariantMetricTensor.Getg_22() * contravariant_components.g22 + + covariantMetricTensor.Getg_23() * contravariant_components.g23) + - 1)), + max(abs((covariantMetricTensor.Getg_13() * contravariant_components.g13 + + covariantMetricTensor.Getg_23() * contravariant_components.g23 + + covariantMetricTensor.Getg_33() * contravariant_components.g33) + - 1))); output_info.write("\tLocal maximum error in diagonal inversion is {:e}\n", maxerr); - maxerr = BOUTMAX(max(abs(covariant_components.g_11 * contravariant_components.g12 - + covariant_components.g_12 * contravariant_components.g22 - + covariant_components.g_13 * contravariant_components.g23)), - max(abs(covariant_components.g_11 * contravariant_components.g13 - + covariant_components.g_12 * contravariant_components.g23 - + covariant_components.g_13 * contravariant_components.g33)), - max(abs(covariant_components.g_12 * contravariant_components.g13 - + covariant_components.g_22 * contravariant_components.g23 - + covariant_components.g_23 * contravariant_components.g33))); + maxerr = + BOUTMAX(max(abs(covariantMetricTensor.Getg_11() * contravariant_components.g12 + + covariantMetricTensor.Getg_12() * contravariant_components.g22 + + covariantMetricTensor.Getg_13() * contravariant_components.g23)), + max(abs(covariantMetricTensor.Getg_11() * contravariant_components.g13 + + covariantMetricTensor.Getg_12() * contravariant_components.g23 + + covariantMetricTensor.Getg_13() * contravariant_components.g33)), + max(abs(covariantMetricTensor.Getg_12() * contravariant_components.g13 + + covariantMetricTensor.Getg_22() * contravariant_components.g23 + + covariantMetricTensor.Getg_23() * contravariant_components.g33))); output_info.write("\tLocal maximum error in off-diagonal inversion is {:e}\n", maxerr); diff --git a/src/mesh/CovariantMetricTensor.cpp b/src/mesh/CovariantMetricTensor.cpp index bd197f21c1..221f054694 100644 --- a/src/mesh/CovariantMetricTensor.cpp +++ b/src/mesh/CovariantMetricTensor.cpp @@ -7,9 +7,8 @@ CovariantMetricTensor::CovariantMetricTensor( const FieldMetric g_11, const FieldMetric g_22, const FieldMetric g_33, const FieldMetric g_12, const FieldMetric g_13, const FieldMetric g_23) - : covariant_components({FieldMetric(std::move(g_11)), FieldMetric(std::move(g_22)), - FieldMetric(std::move(g_33)), FieldMetric(std::move(g_12)), - FieldMetric(std::move(g_13)), FieldMetric(std::move(g_23))}) { + : g_11(std::move(g_11)), g_22(std::move(g_22)), g_33(std::move(g_33)), + g_12(std::move(g_12)), g_13(std::move(g_13)), g_23(std::move(g_23)) { Allocate(); // Make sure metric elements are allocated // ; TODO: Required? } @@ -18,30 +17,28 @@ CovariantMetricTensor::CovariantMetricTensor(const BoutReal g_11, const BoutReal const BoutReal g_33, const BoutReal g_12, const BoutReal g_13, const BoutReal g_23, Mesh* mesh) - : covariant_components({FieldMetric(g_11, mesh), FieldMetric(g_22, mesh), - FieldMetric(g_33, mesh), FieldMetric(g_12, mesh), - FieldMetric(g_13, mesh), FieldMetric(g_23, mesh)}) { + : g_11(g_11, mesh), g_22(g_22, mesh), g_33(g_33, mesh), g_12(g_12, mesh), + g_13(g_13, mesh), g_23(g_23, mesh) { Allocate(); // Make sure metric elements are allocated // ; TODO: Required? } -CovariantMetricTensor::CovariantComponents -CovariantMetricTensor::getCovariantMetricTensor() const { - return CovariantComponents{covariant_components.g_11, covariant_components.g_22, - covariant_components.g_33, covariant_components.g_12, - covariant_components.g_13, covariant_components.g_23}; -} +CovariantMetricTensor::FieldMetric CovariantMetricTensor::Getg_11() const { return g_11; } +CovariantMetricTensor::FieldMetric CovariantMetricTensor::Getg_22() const { return g_22; } +CovariantMetricTensor::FieldMetric CovariantMetricTensor::Getg_33() const { return g_33; } +CovariantMetricTensor::FieldMetric CovariantMetricTensor::Getg_12() const { return g_12; } +CovariantMetricTensor::FieldMetric CovariantMetricTensor::Getg_13() const { return g_13; } +CovariantMetricTensor::FieldMetric CovariantMetricTensor::Getg_23() const { return g_23; } void CovariantMetricTensor::setCovariantMetricTensor( CELL_LOC location, const CovariantMetricTensor& metric_tensor) { - const auto new_components = metric_tensor.getCovariantMetricTensor(); - covariant_components.g_11 = new_components.g_11; - covariant_components.g_22 = new_components.g_22; - covariant_components.g_33 = new_components.g_33; - covariant_components.g_12 = new_components.g_12; - covariant_components.g_13 = new_components.g_13; - covariant_components.g_23 = new_components.g_23; + g_11 = metric_tensor.Getg_11(); + g_22 = metric_tensor.Getg_22(); + g_33 = metric_tensor.Getg_33(); + g_12 = metric_tensor.Getg_12(); + g_13 = metric_tensor.Getg_13(); + g_23 = metric_tensor.Getg_23(); calcContravariant(location); } @@ -54,14 +51,14 @@ CovariantMetricTensor::calcContravariant(CELL_LOC location, const std::string& r auto a = Matrix(3, 3); - BOUT_FOR_SERIAL(i, covariant_components.g_11.getRegion(region)) { - a(0, 0) = covariant_components.g_11[i]; - a(1, 1) = covariant_components.g_22[i]; - a(2, 2) = covariant_components.g_33[i]; + BOUT_FOR_SERIAL(i, g_11.getRegion(region)) { + a(0, 0) = g_11[i]; + a(1, 1) = g_22[i]; + a(2, 2) = g_33[i]; - a(0, 1) = a(1, 0) = covariant_components.g_12[i]; - a(1, 2) = a(2, 1) = covariant_components.g_23[i]; - a(0, 2) = a(2, 0) = covariant_components.g_13[i]; + a(0, 1) = a(1, 0) = g_12[i]; + a(1, 2) = a(2, 1) = g_23[i]; + a(0, 2) = a(2, 0) = g_13[i]; if (invert3x3(a)) { const auto error_message = "\tERROR: metric tensor is singular at ({:d}, {:d})\n"; @@ -71,41 +68,36 @@ CovariantMetricTensor::calcContravariant(CELL_LOC location, const std::string& r } auto* const mesh = - covariant_components.g_11 - .getMesh(); //TODO: Add a getMesh() method to CovariantComponents? + g_11.getMesh(); //TODO: Add a getMesh() method to CovariantComponents? ContravariantMetricTensor contravariantMetricTensor = ContravariantMetricTensor( a(0, 0), a(1, 1), a(2, 2), a(0, 1), a(0, 2), a(1, 2), mesh); - + contravariantMetricTensor.setLocation(location); auto contravariant_components = contravariantMetricTensor.getContravariantMetricTensor(); BoutReal maxerr; - maxerr = BOUTMAX(max(abs((covariant_components.g_11 * contravariant_components.g11 - + covariant_components.g_12 * contravariant_components.g12 - + covariant_components.g_13 * contravariant_components.g13) - - 1)), - max(abs((covariant_components.g_12 * contravariant_components.g12 - + covariant_components.g_22 * contravariant_components.g22 - + covariant_components.g_23 * contravariant_components.g23) - - 1)), - max(abs((covariant_components.g_13 * contravariant_components.g13 - + covariant_components.g_23 * contravariant_components.g23 - + covariant_components.g_33 * contravariant_components.g33) - - 1))); + maxerr = BOUTMAX( + max(abs((g_11 * contravariant_components.g11 + g_12 * contravariant_components.g12 + + g_13 * contravariant_components.g13) + - 1)), + max(abs((g_12 * contravariant_components.g12 + g_22 * contravariant_components.g22 + + g_23 * contravariant_components.g23) + - 1)), + max(abs((g_13 * contravariant_components.g13 + g_23 * contravariant_components.g23 + + g_33 * contravariant_components.g33) + - 1))); output_info.write("\tMaximum error in diagonal inversion is {:e}\n", maxerr); - maxerr = BOUTMAX(max(abs(covariant_components.g_11 * contravariant_components.g12 - + covariant_components.g_12 * contravariant_components.g22 - + covariant_components.g_13 * contravariant_components.g23)), - max(abs(covariant_components.g_11 * contravariant_components.g13 - + covariant_components.g_12 * contravariant_components.g23 - + covariant_components.g_13 * contravariant_components.g33)), - max(abs(covariant_components.g_12 * contravariant_components.g13 - + covariant_components.g_22 * contravariant_components.g23 - + covariant_components.g_23 * contravariant_components.g33))); + maxerr = BOUTMAX( + max(abs(g_11 * contravariant_components.g12 + g_12 * contravariant_components.g22 + + g_13 * contravariant_components.g23)), + max(abs(g_11 * contravariant_components.g13 + g_12 * contravariant_components.g23 + + g_13 * contravariant_components.g33)), + max(abs(g_12 * contravariant_components.g13 + g_22 * contravariant_components.g23 + + g_23 * contravariant_components.g33))); output_info.write("\tMaximum error in off-diagonal inversion is {:e}\n", maxerr); @@ -114,53 +106,50 @@ CovariantMetricTensor::calcContravariant(CELL_LOC location, const std::string& r void CovariantMetricTensor::checkCovariant(int ystart) { // Diagonal metric components should be finite - bout::checkFinite(covariant_components.g_11, "g_11", "RGN_NOCORNERS"); - bout::checkFinite(covariant_components.g_22, "g_22", "RGN_NOCORNERS"); - bout::checkFinite(covariant_components.g_33, "g_33", "RGN_NOCORNERS"); - if (covariant_components.g_11.hasParallelSlices() - && &covariant_components.g_11.ynext(1) != &covariant_components.g_11) { + bout::checkFinite(g_11, "g_11", "RGN_NOCORNERS"); + bout::checkFinite(g_22, "g_22", "RGN_NOCORNERS"); + bout::checkFinite(g_33, "g_33", "RGN_NOCORNERS"); + if (g_11.hasParallelSlices() && &g_11.ynext(1) != &g_11) { for (int dy = 1; dy <= ystart; ++dy) { for (const auto sign : {1, -1}) { - bout::checkFinite(covariant_components.g_11.ynext(sign * dy), "g_11.ynext", + bout::checkFinite(g_11.ynext(sign * dy), "g_11.ynext", fmt::format("RGN_YPAR_{:+d}", sign * dy)); - bout::checkFinite(covariant_components.g_22.ynext(sign * dy), "g_22.ynext", + bout::checkFinite(g_22.ynext(sign * dy), "g_22.ynext", fmt::format("RGN_YPAR_{:+d}", sign * dy)); - bout::checkFinite(covariant_components.g_33.ynext(sign * dy), "g_33.ynext", + bout::checkFinite(g_33.ynext(sign * dy), "g_33.ynext", fmt::format("RGN_YPAR_{:+d}", sign * dy)); } } } // Diagonal metric components should be positive - bout::checkPositive(covariant_components.g_11, "g_11", "RGN_NOCORNERS"); - bout::checkPositive(covariant_components.g_22, "g_22", "RGN_NOCORNERS"); - bout::checkPositive(covariant_components.g_33, "g_33", "RGN_NOCORNERS"); - if (covariant_components.g_11.hasParallelSlices() - && &covariant_components.g_11.ynext(1) != &covariant_components.g_11) { + bout::checkPositive(g_11, "g_11", "RGN_NOCORNERS"); + bout::checkPositive(g_22, "g_22", "RGN_NOCORNERS"); + bout::checkPositive(g_33, "g_33", "RGN_NOCORNERS"); + if (g_11.hasParallelSlices() && &g_11.ynext(1) != &g_11) { for (int dy = 1; dy <= ystart; ++dy) { for (const auto sign : {1, -1}) { - bout::checkPositive(covariant_components.g_11.ynext(sign * dy), "g_11.ynext", + bout::checkPositive(g_11.ynext(sign * dy), "g_11.ynext", fmt::format("RGN_YPAR_{:+d}", sign * dy)); - bout::checkPositive(covariant_components.g_22.ynext(sign * dy), "g_22.ynext", + bout::checkPositive(g_22.ynext(sign * dy), "g_22.ynext", fmt::format("RGN_YPAR_{:+d}", sign * dy)); - bout::checkPositive(covariant_components.g_33.ynext(sign * dy), "g_33.ynext", + bout::checkPositive(g_33.ynext(sign * dy), "g_33.ynext", fmt::format("RGN_YPAR_{:+d}", sign * dy)); } } } // Off-diagonal metric components should be finite - bout::checkFinite(covariant_components.g_12, "g_12", "RGN_NOCORNERS"); - bout::checkFinite(covariant_components.g_13, "g_13", "RGN_NOCORNERS"); - bout::checkFinite(covariant_components.g_23, "g_23", "RGN_NOCORNERS"); - if (covariant_components.g_23.hasParallelSlices() - && &covariant_components.g_23.ynext(1) != &covariant_components.g_23) { + bout::checkFinite(g_12, "g_12", "RGN_NOCORNERS"); + bout::checkFinite(g_13, "g_13", "RGN_NOCORNERS"); + bout::checkFinite(g_23, "g_23", "RGN_NOCORNERS"); + if (g_23.hasParallelSlices() && &g_23.ynext(1) != &g_23) { for (int dy = 1; dy <= ystart; ++dy) { for (const auto sign : {1, -1}) { - bout::checkFinite(covariant_components.g_12.ynext(sign * dy), "g_12.ynext", + bout::checkFinite(g_12.ynext(sign * dy), "g_12.ynext", fmt::format("RGN_YPAR_{:+d}", sign * dy)); - bout::checkFinite(covariant_components.g_13.ynext(sign * dy), "g_13.ynext", + bout::checkFinite(g_13.ynext(sign * dy), "g_13.ynext", fmt::format("RGN_YPAR_{:+d}", sign * dy)); - bout::checkFinite(covariant_components.g_23.ynext(sign * dy), "g_23.ynext", + bout::checkFinite(g_23.ynext(sign * dy), "g_23.ynext", fmt::format("RGN_YPAR_{:+d}", sign * dy)); } } @@ -168,19 +157,19 @@ void CovariantMetricTensor::checkCovariant(int ystart) { } void CovariantMetricTensor::Allocate() { // ; TODO: Required? - covariant_components.g_11.allocate(); - covariant_components.g_22.allocate(); - covariant_components.g_33.allocate(); - covariant_components.g_12.allocate(); - covariant_components.g_13.allocate(); - covariant_components.g_23.allocate(); + g_11.allocate(); + g_22.allocate(); + g_33.allocate(); + g_12.allocate(); + g_13.allocate(); + g_23.allocate(); } void CovariantMetricTensor::setLocation(const CELL_LOC location) { - covariant_components.g_11.setLocation(location); - covariant_components.g_22.setLocation(location); - covariant_components.g_33.setLocation(location); - covariant_components.g_12.setLocation(location); - covariant_components.g_13.setLocation(location); - covariant_components.g_23.setLocation(location); + g_11.setLocation(location); + g_22.setLocation(location); + g_33.setLocation(location); + g_12.setLocation(location); + g_13.setLocation(location); + g_23.setLocation(location); } diff --git a/src/mesh/boundary_standard.cxx b/src/mesh/boundary_standard.cxx index 3df5d30f6b..bcdc730aea 100644 --- a/src/mesh/boundary_standard.cxx +++ b/src/mesh/boundary_standard.cxx @@ -1612,8 +1612,8 @@ void BoundaryNeumann_NonOrthogonal::apply(Field2D& f) { + contravariant_components.g11(bndry->x - bndry->bx, bndry->y)); BoutReal g12shift = 0.5 - * (contravariant_components.g12(bndry->x, bndry->y) + - contravariant_components.g12(bndry->x - bndry->bx, bndry->y)); + * (contravariant_components.g12(bndry->x, bndry->y) + + contravariant_components.g12(bndry->x - bndry->bx, bndry->y)); // Have to use derivatives at last gridpoint instead of derivatives on boundary layer // because derivative values don't exist in boundary region // NOTE: should be fixed to interpolate to boundary line @@ -1669,15 +1669,12 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { int z = 0; #endif // Interpolate (linearly) metrics to halfway between last cell and boundary cell - BoutReal g11shift = 0.5 - * (g.g11(bndry->x, bndry->y, z) - + g.g11(bndry->x - bndry->bx, bndry->y, z)); - BoutReal g12shift = 0.5 - * (g.g12(bndry->x, bndry->y, z) - + g.g12(bndry->x - bndry->bx, bndry->y, z)); - BoutReal g13shift = 0.5 - * (g.g13(bndry->x, bndry->y, z) - + g.g13(bndry->x - bndry->bx, bndry->y, z)); + BoutReal g11shift = + 0.5 * (g.g11(bndry->x, bndry->y, z) + g.g11(bndry->x - bndry->bx, bndry->y, z)); + BoutReal g12shift = + 0.5 * (g.g12(bndry->x, bndry->y, z) + g.g12(bndry->x - bndry->bx, bndry->y, z)); + BoutReal g13shift = + 0.5 * (g.g13(bndry->x, bndry->y, z) + g.g13(bndry->x - bndry->bx, bndry->y, z)); // Have to use derivatives at last gridpoint instead of derivatives on boundary // layer // because derivative values don't exist in boundary region @@ -2451,13 +2448,12 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { void BoundaryNeumannPar::apply(Field2D & f) { #if not(BOUT_USE_METRIC_3D) Coordinates* metric = f.getCoordinates(); - const auto covariant_components = metric->getCovariantMetricTensor(); // Loop over all elements and set equal to the next point in for (bndry->first(); !bndry->isDone(); bndry->next()) { f(bndry->x, bndry->y) = f(bndry->x - bndry->bx, bndry->y - bndry->by) - * sqrt(covariant_components.g_22(bndry->x, bndry->y) - / covariant_components.g_22(bndry->x - bndry->bx, bndry->y - bndry->by)); + * sqrt(metric->g_22()(bndry->x, bndry->y) + / metric->g_22()(bndry->x - bndry->bx, bndry->y - bndry->by)); } #else throw BoutException("Applying boundary condition 'neumannpar' to Field2D not " @@ -2469,13 +2465,12 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { Mesh* mesh = bndry->localmesh; ASSERT1(mesh == f.getMesh()); Coordinates* metric = f.getCoordinates(); - const auto covariant_components = metric->getCovariantMetricTensor(); for (bndry->first(); !bndry->isDone(); bndry->next()) { for (int z = 0; z < mesh->LocalNz; z++) { f(bndry->x, bndry->y, z) = f(bndry->x - bndry->bx, bndry->y - bndry->by, z) - * sqrt(covariant_components.g_22(bndry->x, bndry->y, z) - / covariant_components.g_22(bndry->x - bndry->bx, bndry->y - bndry->by, z)); + * sqrt(metric->g_22()(bndry->x, bndry->y, z) + / metric->g_22()(bndry->x - bndry->bx, bndry->y - bndry->by, z)); } } } @@ -2672,8 +2667,7 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { c0[0] += c1[0]; // Straight line // kz != 0 solution - BoutReal coef = - -1.0 * sqrt(g.g33(x, y) / g.g11(x, y)) * metric->dx(x, y); + BoutReal coef = -1.0 * sqrt(g.g33(x, y) / g.g11(x, y)) * metric->dx(x, y); for (int jz = 1; jz <= ncz / 2; jz++) { BoutReal kwave = jz * 2.0 * PI / metric->zlength()(x, y); // wavenumber in [rad^-1] @@ -2887,8 +2881,8 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { xpos -= metric->dx(x, y); c2[0] = c0[0] + k0lin * xpos + 0.5 * c1[0] * xpos * xpos / g.g11(x - bx, y); // kz != 0 solution - BoutReal coef = -1.0 * sqrt(g.g33(x - bx, y) / g.g11(x - bx, y)) - * metric->dx(x - bx, y); + BoutReal coef = + -1.0 * sqrt(g.g33(x - bx, y) / g.g11(x - bx, y)) * metric->dx(x - bx, y); for (int jz = 1; jz <= ncz / 2; jz++) { BoutReal kwave = jz * 2.0 * PI / getUniform(metric->zlength()); // wavenumber in [rad^-1] @@ -2982,9 +2976,12 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { tmp = -(metric->J(jx, jy) * contravariant_components.g12(jx, jy) * var.y(jx, jy, jz) - + metric->J(jx, jy) * contravariant_components.g13(jx, jy) * var.z(jx, jy, jz) - - metric->J(jx - 2, jy) * contravariant_components.g12(jx - 2, jy) * var.y(jx - 2, jy, jz) - + metric->J(jx - 2, jy) * contravariant_components.g13(jx - 2, jy) * var.z(jx - 2, jy, jz)) + + metric->J(jx, jy) * contravariant_components.g13(jx, jy) + * var.z(jx, jy, jz) + - metric->J(jx - 2, jy) * contravariant_components.g12(jx - 2, jy) + * var.y(jx - 2, jy, jz) + + metric->J(jx - 2, jy) * contravariant_components.g13(jx - 2, jy) + * var.z(jx - 2, jy, jz)) / (metric->dx(jx - 2, jy) + metric->dx(jx - 1, jy)); // First term (d/dx) using vals calculated above tmp -= (metric->J(jx - 1, jy + 1) * contravariant_components.g12(jx - 1, jy + 1) @@ -3009,12 +3006,14 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { / (2. * metric->dz(jx - 1, jy)); var.x(jx, jy, jz) = - (metric->J(jx - 2, jy) * contravariant_components.g11(jx - 2, jy) * var.x(jx - 2, jy, jz) + (metric->J(jx - 2, jy) * contravariant_components.g11(jx - 2, jy) + * var.x(jx - 2, jy, jz) + (metric->dx(jx - 2, jy) + metric->dx(jx - 1, jy)) * tmp) / metric->J(jx, jy) * contravariant_components.g11(jx, jy); if (mesh->xstart == 2) { var.x(jx + 1, jy, jz) = - (metric->J(jx - 3, jy) * contravariant_components.g11(jx - 3, jy) * var.x(jx - 3, jy, jz) + (metric->J(jx - 3, jy) * contravariant_components.g11(jx - 3, jy) + * var.x(jx - 3, jy, jz) + 4. * metric->dx(jx, jy) * tmp) / metric->J(jx + 1, jy) * contravariant_components.g11(jx + 1, jy); } diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index bc1960ccef..b980193011 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -26,7 +26,7 @@ namespace { template // Use sendY()/sendX() and wait() instead of Mesh::communicate() to ensure we // don't try to calculate parallel slices as Coordinates are not constructed yet -void communicate(T& t, Ts&... ts) { +void communicate(T& t, Ts... ts) { FieldGroup g(t, ts...); auto h = t.getMesh()->sendY(g); t.getMesh()->wait(h); @@ -39,7 +39,7 @@ void communicate(T& t, Ts&... ts) { /// Boundary guard cells are set by extrapolating from the grid, like /// 'free_o3' boundary conditions /// Corner guard cells are set to BoutNaN -Field2D interpolateAndExtrapolate(const Field2D& f, CELL_LOC location, bool extrapolate_x, +Field2D interpolateAndExtrapolate(const Field2D f, CELL_LOC location, bool extrapolate_x, bool extrapolate_y, bool no_extra_interpolate, ParallelTransform* UNUSED(pt) = nullptr) { @@ -322,7 +322,7 @@ void checkStaggeredGet(Mesh* mesh, const std::string& name, const std::string& s } // convenience function for repeated code -int getAtLoc(Mesh* mesh, Coordinates::FieldMetric& var, const std::string& name, +int getAtLoc(Mesh* mesh, Coordinates::FieldMetric var, const std::string& name, const std::string& suffix, CELL_LOC location, BoutReal default_value = 0.) { checkStaggeredGet(mesh, name, suffix); @@ -446,7 +446,7 @@ Coordinates::Coordinates(Mesh* mesh, Options* options) dy = interpolateAndExtrapolate(dy, location, extrapolate_x, extrapolate_y, false, transform.get()); - auto getUnaligned = [this](auto& field, const std::string& name, + auto getUnaligned = [this](auto field, const std::string& name, BoutReal default_value) { localmesh->get(field, name, default_value, false); if (field.getDirectionY() == YDirectionType::Aligned @@ -499,15 +499,13 @@ Coordinates::Coordinates(Mesh* mesh, Options* options) if (std::all_of(begin(covariant_component_names), end(covariant_component_names), source_has_component)) { - auto covariant_components = covariantMetricTensor.getCovariantMetricTensor(); - FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; - g_11 = getUnaligned(covariant_components.g_11, "g_11", 1.0); - g_22 = getUnaligned(covariant_components.g_22, "g_22", 1.0); - g_33 = getUnaligned(covariant_components.g_33, "g_33", 1.0); - g_12 = getUnaligned(covariant_components.g_12, "g_12", 0.0); - g_13 = getUnaligned(covariant_components.g_13, "g_13", 0.0); - g_23 = getUnaligned(covariant_components.g_23, "g_23", 0.0); + g_11 = getUnaligned(covariantMetricTensor.Getg_11(), "g_11", 1.0); + g_22 = getUnaligned(covariantMetricTensor.Getg_22(), "g_22", 1.0); + g_33 = getUnaligned(covariantMetricTensor.Getg_33(), "g_33", 1.0); + g_12 = getUnaligned(covariantMetricTensor.Getg_12(), "g_12", 0.0); + g_13 = getUnaligned(covariantMetricTensor.Getg_13(), "g_13", 0.0); + g_23 = getUnaligned(covariantMetricTensor.Getg_23(), "g_23", 0.0); covariantMetricTensor.setCovariantMetricTensor( location, CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); @@ -534,23 +532,22 @@ Coordinates::Coordinates(Mesh* mesh, Options* options) } } - auto covariant_components = covariantMetricTensor.getCovariantMetricTensor(); FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; // More robust to extrapolate derived quantities directly, rather than // deriving from extrapolated covariant metric components - g_11 = interpolateAndExtrapolate(covariant_components.g_11, location, extrapolate_x, - extrapolate_y, false, transform.get()); - g_22 = interpolateAndExtrapolate(covariant_components.g_22, location, extrapolate_x, - extrapolate_y, false, transform.get()); - g_33 = interpolateAndExtrapolate(covariant_components.g_33, location, extrapolate_x, - extrapolate_y, false, transform.get()); - g_12 = interpolateAndExtrapolate(covariant_components.g_12, location, extrapolate_x, - extrapolate_y, false, transform.get()); - g_13 = interpolateAndExtrapolate(covariant_components.g_13, location, extrapolate_x, - extrapolate_y, false, transform.get()); - g_23 = interpolateAndExtrapolate(covariant_components.g_23, location, extrapolate_x, - extrapolate_y, false, transform.get()); + g_11 = interpolateAndExtrapolate(covariantMetricTensor.Getg_11(), location, + extrapolate_x, extrapolate_y, false, transform.get()); + g_22 = interpolateAndExtrapolate(covariantMetricTensor.Getg_22(), location, + extrapolate_x, extrapolate_y, false, transform.get()); + g_33 = interpolateAndExtrapolate(covariantMetricTensor.Getg_33(), location, + extrapolate_x, extrapolate_y, false, transform.get()); + g_12 = interpolateAndExtrapolate(covariantMetricTensor.Getg_12(), location, + extrapolate_x, extrapolate_y, false, transform.get()); + g_13 = interpolateAndExtrapolate(covariantMetricTensor.Getg_13(), location, + extrapolate_x, extrapolate_y, false, transform.get()); + g_23 = interpolateAndExtrapolate(covariantMetricTensor.Getg_23(), location, + extrapolate_x, extrapolate_y, false, transform.get()); covariantMetricTensor.setCovariantMetricTensor( location, CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); @@ -722,8 +719,6 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, return mesh->sourceHasVar(name + suffix); }; - auto covariant_components = covariantMetricTensor.getCovariantMetricTensor(); - // Check if any of the components are present if (std::any_of(begin(covariant_component_names), end(covariant_component_names), source_has_component)) { @@ -731,12 +726,12 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, if (std::all_of(begin(covariant_component_names), end(covariant_component_names), source_has_component)) { - getAtLoc(mesh, covariant_components.g_11, "g_11", suffix, location); - getAtLoc(mesh, covariant_components.g_22, "g_22", suffix, location); - getAtLoc(mesh, covariant_components.g_33, "g_33", suffix, location); - getAtLoc(mesh, covariant_components.g_12, "g_12", suffix, location); - getAtLoc(mesh, covariant_components.g_13, "g_13", suffix, location); - getAtLoc(mesh, covariant_components.g_23, "g_23", suffix, location); + getAtLoc(mesh, covariantMetricTensor.Getg_11(), "g_11", suffix, location); + getAtLoc(mesh, covariantMetricTensor.Getg_22(), "g_22", suffix, location); + getAtLoc(mesh, covariantMetricTensor.Getg_33(), "g_33", suffix, location); + getAtLoc(mesh, covariantMetricTensor.Getg_12(), "g_12", suffix, location); + getAtLoc(mesh, covariantMetricTensor.Getg_13(), "g_13", suffix, location); + getAtLoc(mesh, covariantMetricTensor.Getg_23(), "g_23", suffix, location); output_warn.write( "\tWARNING! Staggered covariant components of metric tensor set manually. " @@ -769,18 +764,24 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, // More robust to extrapolate derived quantities directly, rather than // deriving from extrapolated covariant metric components - g_11 = interpolateAndExtrapolate(covariant_components.g_11, location, extrapolate_x, - extrapolate_y, false, transform.get()); - g_22 = interpolateAndExtrapolate(covariant_components.g_22, location, extrapolate_x, - extrapolate_y, false, transform.get()); - g_33 = interpolateAndExtrapolate(covariant_components.g_33, location, extrapolate_x, - extrapolate_y, false, transform.get()); - g_12 = interpolateAndExtrapolate(covariant_components.g_12, location, extrapolate_x, - extrapolate_y, false, transform.get()); - g_13 = interpolateAndExtrapolate(covariant_components.g_13, location, extrapolate_x, - extrapolate_y, false, transform.get()); - g_23 = interpolateAndExtrapolate(covariant_components.g_23, location, extrapolate_x, - extrapolate_y, false, transform.get()); + g_11 = + interpolateAndExtrapolate(covariantMetricTensor.Getg_11(), location, + extrapolate_x, extrapolate_y, false, transform.get()); + g_22 = + interpolateAndExtrapolate(covariantMetricTensor.Getg_22(), location, + extrapolate_x, extrapolate_y, false, transform.get()); + g_33 = + interpolateAndExtrapolate(covariantMetricTensor.Getg_33(), location, + extrapolate_x, extrapolate_y, false, transform.get()); + g_12 = + interpolateAndExtrapolate(covariantMetricTensor.Getg_12(), location, + extrapolate_x, extrapolate_y, false, transform.get()); + g_13 = + interpolateAndExtrapolate(covariantMetricTensor.Getg_13(), location, + extrapolate_x, extrapolate_y, false, transform.get()); + g_23 = + interpolateAndExtrapolate(covariantMetricTensor.Getg_23(), location, + extrapolate_x, extrapolate_y, false, transform.get()); // Check covariant metrics checkCovariant(); @@ -906,24 +907,22 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; - const auto covariant_components = coords_in->getCovariantMetricTensor(); - // 3x3 matrix inversion can exaggerate small interpolation errors, so it is // more robust to interpolate and extrapolate derived quantities directly, // rather than deriving from interpolated/extrapolated covariant metric // components - g_11 = interpolateAndExtrapolate(covariant_components.g_11, location, true, true, - false, transform.get()); - g_22 = interpolateAndExtrapolate(covariant_components.g_22, location, true, true, - false, transform.get()); - g_33 = interpolateAndExtrapolate(covariant_components.g_33, location, true, true, - false, transform.get()); - g_12 = interpolateAndExtrapolate(covariant_components.g_12, location, true, true, - false, transform.get()); - g_13 = interpolateAndExtrapolate(covariant_components.g_13, location, true, true, - false, transform.get()); - g_23 = interpolateAndExtrapolate(covariant_components.g_23, location, true, true, - false, transform.get()); + g_11 = interpolateAndExtrapolate(covariantMetricTensor.Getg_11(), location, true, + true, false, transform.get()); + g_22 = interpolateAndExtrapolate(covariantMetricTensor.Getg_22(), location, true, + true, false, transform.get()); + g_33 = interpolateAndExtrapolate(covariantMetricTensor.Getg_33(), location, true, + true, false, transform.get()); + g_12 = interpolateAndExtrapolate(covariantMetricTensor.Getg_12(), location, true, + true, false, transform.get()); + g_13 = interpolateAndExtrapolate(covariantMetricTensor.Getg_13(), location, true, + true, false, transform.get()); + g_23 = interpolateAndExtrapolate(covariantMetricTensor.Getg_23(), location, true, + true, false, transform.get()); covariantMetricTensor.setCovariantMetricTensor( location, CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); @@ -963,7 +962,6 @@ void Coordinates::outputVars(Options& output_options) { auto const contravariant_components = contravariantMetricTensor.getContravariantMetricTensor(); - auto const covariant_components = covariantMetricTensor.getCovariantMetricTensor(); output_options["g11" + loc_string].force(contravariant_components.g11, "Coordinates"); output_options["g22" + loc_string].force(contravariant_components.g22, "Coordinates"); @@ -972,12 +970,18 @@ void Coordinates::outputVars(Options& output_options) { output_options["g13" + loc_string].force(contravariant_components.g13, "Coordinates"); output_options["g23" + loc_string].force(contravariant_components.g23, "Coordinates"); - output_options["g_11" + loc_string].force(covariant_components.g_11, "Coordinates"); - output_options["g_22" + loc_string].force(covariant_components.g_22, "Coordinates"); - output_options["g_33" + loc_string].force(covariant_components.g_33, "Coordinates"); - output_options["g_12" + loc_string].force(covariant_components.g_12, "Coordinates"); - output_options["g_13" + loc_string].force(covariant_components.g_13, "Coordinates"); - output_options["g_23" + loc_string].force(covariant_components.g_23, "Coordinates"); + output_options["g_11" + loc_string].force(covariantMetricTensor.Getg_11(), + "Coordinates"); + output_options["g_22" + loc_string].force(covariantMetricTensor.Getg_22(), + "Coordinates"); + output_options["g_33" + loc_string].force(covariantMetricTensor.Getg_33(), + "Coordinates"); + output_options["g_12" + loc_string].force(covariantMetricTensor.Getg_12(), + "Coordinates"); + output_options["g_13" + loc_string].force(covariantMetricTensor.Getg_13(), + "Coordinates"); + output_options["g_23" + loc_string].force(covariantMetricTensor.Getg_23(), + "Coordinates"); output_options["J" + loc_string].force(J, "Coordinates"); output_options["Bxy" + loc_string].force(Bxy, "Coordinates"); @@ -1010,14 +1014,13 @@ int Coordinates::geometry(bool recalculate_staggered, auto contravariant_components = contravariantMetricTensor.getContravariantMetricTensor(); - auto covariant_components = covariantMetricTensor.getCovariantMetricTensor(); communicate(dx, dy, dz, contravariant_components.g11, contravariant_components.g22, contravariant_components.g33, contravariant_components.g12, contravariant_components.g13, contravariant_components.g23, - covariant_components.g_11, covariant_components.g_22, - covariant_components.g_33, covariant_components.g_12, - covariant_components.g_13, covariant_components.g_23, J, Bxy); + covariantMetricTensor.Getg_11(), covariantMetricTensor.Getg_22(), + covariantMetricTensor.Getg_33(), covariantMetricTensor.Getg_12(), + covariantMetricTensor.Getg_13(), covariantMetricTensor.Getg_23(), J, Bxy); output_progress.write("Calculating differential geometry terms\n"); @@ -1235,121 +1238,144 @@ void Coordinates::CalculateChristoffelSymbols() { auto const contravariant_components = contravariantMetricTensor.getContravariantMetricTensor(); - auto const covariant_components = covariantMetricTensor.getCovariantMetricTensor(); - G1_11 = 0.5 * contravariant_components.g11 * DDX(covariant_components.g_11) + G1_11 = 0.5 * contravariant_components.g11 * DDX(covariantMetricTensor.Getg_11()) + contravariant_components.g12 - * (DDX(covariant_components.g_12) - 0.5 * DDY(covariant_components.g_11)) + * (DDX(covariantMetricTensor.Getg_12()) + - 0.5 * DDY(covariantMetricTensor.Getg_11())) + contravariant_components.g13 - * (DDX(covariant_components.g_13) - 0.5 * DDZ(covariant_components.g_11)); + * (DDX(covariantMetricTensor.Getg_13()) + - 0.5 * DDZ(covariantMetricTensor.Getg_11())); G1_22 = contravariant_components.g11 - * (DDY(covariant_components.g_12) - 0.5 * DDX(covariant_components.g_22)) - + 0.5 * contravariant_components.g12 * DDY(covariant_components.g_22) + * (DDY(covariantMetricTensor.Getg_12()) + - 0.5 * DDX(covariantMetricTensor.Getg_22())) + + 0.5 * contravariant_components.g12 * DDY(covariantMetricTensor.Getg_22()) + contravariant_components.g13 - * (DDY(covariant_components.g_23) - 0.5 * DDZ(covariant_components.g_22)); + * (DDY(covariantMetricTensor.Getg_23()) + - 0.5 * DDZ(covariantMetricTensor.Getg_22())); G1_33 = contravariant_components.g11 - * (DDZ(covariant_components.g_13) - 0.5 * DDX(covariant_components.g_33)) + * (DDZ(covariantMetricTensor.Getg_13()) + - 0.5 * DDX(covariantMetricTensor.Getg_33())) + contravariant_components.g12 - * (DDZ(covariant_components.g_23) - 0.5 * DDY(covariant_components.g_33)) - + 0.5 * contravariant_components.g13 * DDZ(covariant_components.g_33); - G1_12 = 0.5 * contravariant_components.g11 * DDY(covariant_components.g_11) - + 0.5 * contravariant_components.g12 * DDX(covariant_components.g_22) - + 0.5 * contravariant_components.g13 - * (DDY(covariant_components.g_13) + DDX(covariant_components.g_23) - - DDZ(covariant_components.g_12)); - G1_13 = 0.5 * contravariant_components.g11 * DDZ(covariant_components.g_11) - + 0.5 * contravariant_components.g12 - * (DDZ(covariant_components.g_12) + DDX(covariant_components.g_23) - - DDY(covariant_components.g_13)) - + 0.5 * contravariant_components.g13 * DDX(covariant_components.g_33); - G1_23 = 0.5 * contravariant_components.g11 - * (DDZ(covariant_components.g_12) + DDY(covariant_components.g_13) - - DDX(covariant_components.g_23)) - + 0.5 * contravariant_components.g12 - * (DDZ(covariant_components.g_22) + DDY(covariant_components.g_23) - - DDY(covariant_components.g_23)) - // + 0.5 *g13*(DDZ(g_32) + DDY(g_33) - DDZ(g_23)); - // which equals - + 0.5 * contravariant_components.g13 * DDY(covariant_components.g_33); - - G2_11 = 0.5 * contravariant_components.g12 * DDX(covariant_components.g_11) + * (DDZ(covariantMetricTensor.Getg_23()) + - 0.5 * DDY(covariantMetricTensor.Getg_33())) + + 0.5 * contravariant_components.g13 * DDZ(covariantMetricTensor.Getg_33()); + G1_12 = + 0.5 * contravariant_components.g11 * DDY(covariantMetricTensor.Getg_11()) + + 0.5 * contravariant_components.g12 * DDX(covariantMetricTensor.Getg_22()) + + 0.5 * contravariant_components.g13 + * (DDY(covariantMetricTensor.Getg_13()) + DDX(covariantMetricTensor.Getg_23()) + - DDZ(covariantMetricTensor.Getg_12())); + G1_13 = + 0.5 * contravariant_components.g11 * DDZ(covariantMetricTensor.Getg_11()) + + 0.5 * contravariant_components.g12 + * (DDZ(covariantMetricTensor.Getg_12()) + DDX(covariantMetricTensor.Getg_23()) + - DDY(covariantMetricTensor.Getg_13())) + + 0.5 * contravariant_components.g13 * DDX(covariantMetricTensor.Getg_33()); + G1_23 = + 0.5 * contravariant_components.g11 + * (DDZ(covariantMetricTensor.Getg_12()) + DDY(covariantMetricTensor.Getg_13()) + - DDX(covariantMetricTensor.Getg_23())) + + 0.5 * contravariant_components.g12 + * (DDZ(covariantMetricTensor.Getg_22()) + DDY(covariantMetricTensor.Getg_23()) + - DDY(covariantMetricTensor.Getg_23())) + // + 0.5 *g13*(DDZ(g_32) + DDY(g_33) - DDZ(g_23)); + // which equals + + 0.5 * contravariant_components.g13 * DDY(covariantMetricTensor.Getg_33()); + + G2_11 = 0.5 * contravariant_components.g12 * DDX(covariantMetricTensor.Getg_11()) + contravariant_components.g22 - * (DDX(covariant_components.g_12) - 0.5 * DDY(covariant_components.g_11)) + * (DDX(covariantMetricTensor.Getg_12()) + - 0.5 * DDY(covariantMetricTensor.Getg_11())) + + contravariant_components.g23 + * (DDX(covariantMetricTensor.Getg_13()) + - 0.5 * DDZ(covariantMetricTensor.Getg_11())); + G2_22 = contravariant_components.g12 + * (DDY(covariantMetricTensor.Getg_12()) + - 0.5 * DDX(covariantMetricTensor.Getg_22())) + + 0.5 * contravariant_components.g22 * DDY(covariantMetricTensor.Getg_22()) + contravariant_components.g23 - * (DDX(covariant_components.g_13) - 0.5 * DDZ(covariant_components.g_11)); - G2_22 = - contravariant_components.g12 - * (DDY(covariant_components.g_12) - 0.5 * DDX(covariant_components.g_22)) - + 0.5 * contravariant_components.g22 * DDY(covariant_components.g_22) - + contravariant_components.g23 - * (DDY(contravariant_components.g23) - 0.5 * DDZ(covariant_components.g_22)); + * (DDY(contravariant_components.g23) + - 0.5 * DDZ(covariantMetricTensor.Getg_22())); G2_33 = contravariant_components.g12 - * (DDZ(covariant_components.g_13) - 0.5 * DDX(covariant_components.g_33)) + * (DDZ(covariantMetricTensor.Getg_13()) + - 0.5 * DDX(covariantMetricTensor.Getg_33())) + contravariant_components.g22 - * (DDZ(covariant_components.g_23) - 0.5 * DDY(covariant_components.g_33)) - + 0.5 * contravariant_components.g23 * DDZ(covariant_components.g_33); - G2_12 = 0.5 * contravariant_components.g12 * DDY(covariant_components.g_11) - + 0.5 * contravariant_components.g22 * DDX(covariant_components.g_22) - + 0.5 * contravariant_components.g23 - * (DDY(covariant_components.g_13) + DDX(covariant_components.g_23) - - DDZ(covariant_components.g_12)); + * (DDZ(covariantMetricTensor.Getg_23()) + - 0.5 * DDY(covariantMetricTensor.Getg_33())) + + 0.5 * contravariant_components.g23 * DDZ(covariantMetricTensor.Getg_33()); + G2_12 = + 0.5 * contravariant_components.g12 * DDY(covariantMetricTensor.Getg_11()) + + 0.5 * contravariant_components.g22 * DDX(covariantMetricTensor.Getg_22()) + + 0.5 * contravariant_components.g23 + * (DDY(covariantMetricTensor.Getg_13()) + DDX(covariantMetricTensor.Getg_23()) + - DDZ(covariantMetricTensor.Getg_12())); G2_13 = - // 0.5 *g21*(DDZ(covariant_components.g_11) + DDX(covariant_components.g_13) - DDX(covariant_components.g_13)) + // 0.5 *g21*(DDZ(covariantMetricTensor.Getg_11()) + DDX(covariantMetricTensor.Getg_13()) - DDX(covariantMetricTensor.Getg_13())) // which equals 0.5 * contravariant_components.g12 - * (DDZ(covariant_components.g_11) + DDX(covariant_components.g_13) - - DDX(covariant_components.g_13)) - // + 0.5 *g22*(DDZ(covariant_components.g_21) + DDX(covariant_components.g_23) - DDY(covariant_components.g_13)) + * (DDZ(covariantMetricTensor.Getg_11()) + DDX(covariantMetricTensor.Getg_13()) + - DDX(covariantMetricTensor.Getg_13())) + // + 0.5 *g22*(DDZ(covariantMetricTensor.Getg_21()) + DDX(covariantMetricTensor.Getg_23()) - DDY(covariantMetricTensor.Getg_13())) // which equals + 0.5 * contravariant_components.g22 - * (DDZ(covariant_components.g_12) + DDX(covariant_components.g_23) - - DDY(covariant_components.g_13)) - // + 0.5 *g23*(DDZ(covariant_components.g_31) + DDX(covariant_components.g_33) - DDZ(g_13)); + * (DDZ(covariantMetricTensor.Getg_12()) + DDX(covariantMetricTensor.Getg_23()) + - DDY(covariantMetricTensor.Getg_13())) + // + 0.5 *g23*(DDZ(covariantMetricTensor.Getg_31()) + DDX(covariantMetricTensor.Getg_33()) - DDZ(g_13)); // which equals - + 0.5 * contravariant_components.g23 * DDX(covariant_components.g_33); - G2_23 = 0.5 * contravariant_components.g12 - * (DDZ(covariant_components.g_12) + DDY(covariant_components.g_13) - - DDX(covariant_components.g_23)) - + 0.5 * contravariant_components.g22 * DDZ(covariant_components.g_22) - + 0.5 * contravariant_components.g23 * DDY(covariant_components.g_33); - - G3_11 = 0.5 * contravariant_components.g13 * DDX(covariant_components.g_11) + + 0.5 * contravariant_components.g23 * DDX(covariantMetricTensor.Getg_33()); + G2_23 = + 0.5 * contravariant_components.g12 + * (DDZ(covariantMetricTensor.Getg_12()) + DDY(covariantMetricTensor.Getg_13()) + - DDX(covariantMetricTensor.Getg_23())) + + 0.5 * contravariant_components.g22 * DDZ(covariantMetricTensor.Getg_22()) + + 0.5 * contravariant_components.g23 * DDY(covariantMetricTensor.Getg_33()); + + G3_11 = 0.5 * contravariant_components.g13 * DDX(covariantMetricTensor.Getg_11()) + contravariant_components.g23 - * (DDX(covariant_components.g_12) - 0.5 * DDY(covariant_components.g_11)) + * (DDX(covariantMetricTensor.Getg_12()) + - 0.5 * DDY(covariantMetricTensor.Getg_11())) + contravariant_components.g33 - * (DDX(covariant_components.g_13) - 0.5 * DDZ(covariant_components.g_11)); + * (DDX(covariantMetricTensor.Getg_13()) + - 0.5 * DDZ(covariantMetricTensor.Getg_11())); G3_22 = contravariant_components.g13 - * (DDY(covariant_components.g_12) - 0.5 * DDX(covariant_components.g_22)) - + 0.5 * contravariant_components.g23 * DDY(covariant_components.g_22) + * (DDY(covariantMetricTensor.Getg_12()) + - 0.5 * DDX(covariantMetricTensor.Getg_22())) + + 0.5 * contravariant_components.g23 * DDY(covariantMetricTensor.Getg_22()) + contravariant_components.g33 - * (DDY(covariant_components.g_23) - 0.5 * DDZ(covariant_components.g_22)); + * (DDY(covariantMetricTensor.Getg_23()) + - 0.5 * DDZ(covariantMetricTensor.Getg_22())); G3_33 = contravariant_components.g13 - * (DDZ(covariant_components.g_13) - 0.5 * DDX(covariant_components.g_33)) + * (DDZ(covariantMetricTensor.Getg_13()) + - 0.5 * DDX(covariantMetricTensor.Getg_33())) + contravariant_components.g23 - * (DDZ(covariant_components.g_23) - 0.5 * DDY(covariant_components.g_33)) - + 0.5 * contravariant_components.g33 * DDZ(covariant_components.g_33); + * (DDZ(covariantMetricTensor.Getg_23()) + - 0.5 * DDY(covariantMetricTensor.Getg_33())) + + 0.5 * contravariant_components.g33 * DDZ(covariantMetricTensor.Getg_33()); G3_12 = - // 0.5 *g31*(DDY(covariant_components.g_11) + DDX(covariant_components.g_12) - DDX(covariant_components.g_12)) + // 0.5 *g31*(DDY(covariantMetricTensor.Getg_11()) + DDX(covariantMetricTensor.Getg_12()) - DDX(covariantMetricTensor.Getg_12())) // which equals to - 0.5 * contravariant_components.g13 * DDY(covariant_components.g_11) - // + 0.5 *g32*(DDY(covariant_components.g_21) + DDX(covariant_components.g_22) - DDY(covariant_components.g_12)) + 0.5 * contravariant_components.g13 * DDY(covariantMetricTensor.Getg_11()) + // + 0.5 *g32*(DDY(covariantMetricTensor.Getg_21()) + DDX(covariantMetricTensor.Getg_22()) - DDY(covariantMetricTensor.Getg_12())) // which equals to - + 0.5 * contravariant_components.g23 * DDX(covariant_components.g_22) - //+ 0.5 *g33*(DDY(covariant_components.g_31) + DDX(covariant_components.g_32) - DDZ(covariant_components.g_12)); + + 0.5 * contravariant_components.g23 * DDX(covariantMetricTensor.Getg_22()) + //+ 0.5 *g33*(DDY(covariantMetricTensor.Getg_31()) + DDX(covariantMetricTensor.Getg_32()) - DDZ(covariantMetricTensor.Getg_12())); // which equals to + 0.5 * contravariant_components.g33 - * (DDY(covariant_components.g_13) + DDX(covariant_components.g_23) - - DDZ(covariant_components.g_12)); - G3_13 = 0.5 * contravariant_components.g13 * DDZ(covariant_components.g_11) - + 0.5 * contravariant_components.g23 - * (DDZ(covariant_components.g_12) + DDX(covariant_components.g_23) - - DDY(covariant_components.g_13)) - + 0.5 * contravariant_components.g33 * DDX(covariant_components.g_33); - G3_23 = 0.5 * contravariant_components.g13 - * (DDZ(covariant_components.g_12) + DDY(covariant_components.g_13) - - DDX(covariant_components.g_23)) - + 0.5 * contravariant_components.g23 * DDZ(covariant_components.g_22) - + 0.5 * contravariant_components.g33 * DDY(covariant_components.g_33); + * (DDY(covariantMetricTensor.Getg_13()) + DDX(covariantMetricTensor.Getg_23()) + - DDZ(covariantMetricTensor.Getg_12())); + G3_13 = + 0.5 * contravariant_components.g13 * DDZ(covariantMetricTensor.Getg_11()) + + 0.5 * contravariant_components.g23 + * (DDZ(covariantMetricTensor.Getg_12()) + DDX(covariantMetricTensor.Getg_23()) + - DDY(covariantMetricTensor.Getg_13())) + + 0.5 * contravariant_components.g33 * DDX(covariantMetricTensor.Getg_33()); + G3_23 = + 0.5 * contravariant_components.g13 + * (DDZ(covariantMetricTensor.Getg_12()) + DDY(covariantMetricTensor.Getg_13()) + - DDX(covariantMetricTensor.Getg_23())) + + 0.5 * contravariant_components.g23 * DDZ(covariantMetricTensor.Getg_22()) + + 0.5 * contravariant_components.g33 * DDY(covariantMetricTensor.Getg_33()); } CovariantMetricTensor Coordinates::calcCovariant(const std::string& region) { @@ -1397,7 +1423,7 @@ int Coordinates::jacobian() { J = interpolateAndExtrapolate(J, location, extrapolate_x, extrapolate_y, false, transform.get()); - Bxy = sqrt(covariantMetricTensor.getCovariantMetricTensor().g_22) / J; + Bxy = sqrt(covariantMetricTensor.Getg_22()) / J; Bxy = interpolateAndExtrapolate(Bxy, location, extrapolate_x, extrapolate_y, false, transform.get()); @@ -1664,9 +1690,8 @@ Coordinates::FieldMetric Coordinates::Grad2_par2(const Field2D& f, CELL_LOC outl TRACE("Coordinates::Grad2_par2( Field2D )"); ASSERT1(location == outloc || (outloc == CELL_DEFAULT && location == f.getLocation())); - auto const covariant_components = covariantMetricTensor.getCovariantMetricTensor(); auto result = Grad2_par2_DDY_invSg(outloc, method) * DDY(f, outloc, method) - + D2DY2(f, outloc, method) / covariant_components.g_22; + + D2DY2(f, outloc, method) / covariantMetricTensor.Getg_22(); return result; } @@ -1681,8 +1706,7 @@ Field3D Coordinates::Grad2_par2(const Field3D& f, CELL_LOC outloc, Field3D result = ::DDY(f, outloc, method); - auto const covariant_components = covariantMetricTensor.getCovariantMetricTensor(); - Field3D r2 = D2DY2(f, outloc, method) / covariant_components.g_22; + Field3D r2 = D2DY2(f, outloc, method) / covariantMetricTensor.Getg_22(); result = Grad2_par2_DDY_invSg(outloc, method) * result + r2; @@ -1844,16 +1868,14 @@ FieldPerp Coordinates::Delp2(const FieldPerp& f, CELL_LOC outloc, bool useFFT) { Coordinates::FieldMetric Coordinates::Laplace_par(const Field2D& f, CELL_LOC outloc) { ASSERT1(location == outloc || outloc == CELL_DEFAULT); - auto const covariant_components = covariantMetricTensor.getCovariantMetricTensor(); - return D2DY2(f, outloc) / covariant_components.g_22 - + DDY(J / covariant_components.g_22, outloc) * DDY(f, outloc) / J; + return D2DY2(f, outloc) / covariantMetricTensor.Getg_22() + + DDY(J / covariantMetricTensor.Getg_22(), outloc) * DDY(f, outloc) / J; } Field3D Coordinates::Laplace_par(const Field3D& f, CELL_LOC outloc) { ASSERT1(location == outloc || outloc == CELL_DEFAULT); - auto const covariant_components = covariantMetricTensor.getCovariantMetricTensor(); - return D2DY2(f, outloc) / covariant_components.g_22 - + DDY(J / covariant_components.g_22, outloc) * ::DDY(f, outloc) / J; + return D2DY2(f, outloc) / covariantMetricTensor.Getg_22() + + DDY(J / covariantMetricTensor.Getg_22(), outloc) * ::DDY(f, outloc) / J; } // Full Laplacian operator on scalar field @@ -1933,14 +1955,13 @@ Field2D Coordinates::Laplace_perpXY([[maybe_unused]] const Field2D& A, // upper y boundary const auto upper_y_avg = [&i](const auto& f) { return 0.5 * (f[i] + f[i.yp()]); }; - auto const covariant_components = covariantMetricTensor.getCovariantMetricTensor(); auto const contravariant_components = contravariantMetricTensor.getContravariantMetricTensor(); const BoutReal upper_y_A = upper_y_avg(A); const BoutReal upper_y_J = upper_y_avg(J); - const BoutReal upper_y_g_22 = upper_y_avg(covariant_components.g_22); + const BoutReal upper_y_g_22 = upper_y_avg(covariantMetricTensor.Getg_22()); const BoutReal upper_y_g23 = upper_y_avg(contravariant_components.g23); - const BoutReal upper_y_g_23 = upper_y_avg(covariant_components.g_23); + const BoutReal upper_y_g_23 = upper_y_avg(covariantMetricTensor.Getg_23()); const BoutReal upper_y_dy = upper_y_avg(dy); const BoutReal upper_y_value = -upper_y_A * upper_y_J * upper_y_g23 * upper_y_g_23 / (upper_y_g_22 * J[i] * upper_y_dy * dy[i]); @@ -1950,9 +1971,9 @@ Field2D Coordinates::Laplace_perpXY([[maybe_unused]] const Field2D& A, const auto lower_y_avg = [&i](const auto& f) { return 0.5 * (f[i] + f[i.ym()]); }; const BoutReal lower_y_A = lower_y_avg(A); const BoutReal lower_y_J = lower_y_avg(J); - const BoutReal lower_y_g_22 = lower_y_avg(covariant_components.g_22); + const BoutReal lower_y_g_22 = lower_y_avg(covariantMetricTensor.Getg_22()); const BoutReal lower_y_g23 = lower_y_avg(contravariant_components.g23); - const BoutReal lower_y_g_23 = lower_y_avg(covariant_components.g_23); + const BoutReal lower_y_g_23 = lower_y_avg(covariantMetricTensor.Getg_23()); const BoutReal lower_y_dy = lower_y_avg(dy); const BoutReal lower_y_value = -lower_y_A * lower_y_J * lower_y_g23 * lower_y_g_23 / (lower_y_g_22 * J[i] * lower_y_dy * dy[i]); @@ -1968,7 +1989,7 @@ Field2D Coordinates::Laplace_perpXY([[maybe_unused]] const Field2D& A, const Coordinates::FieldMetric& Coordinates::invSg() const { if (invSgCache == nullptr) { auto ptr = std::make_unique(); - (*ptr) = 1.0 / sqrt(covariantMetricTensor.getCovariantMetricTensor().g_22); + (*ptr) = 1.0 / sqrt(covariantMetricTensor.Getg_22()); invSgCache = std::move(ptr); } return *invSgCache; @@ -2010,8 +2031,23 @@ Coordinates::getContravariantMetricTensor() const { return contravariantMetricTensor.getContravariantMetricTensor(); } -CovariantMetricTensor::CovariantComponents Coordinates::getCovariantMetricTensor() const { - return covariantMetricTensor.getCovariantMetricTensor(); +CovariantMetricTensor::FieldMetric Coordinates::g_11() const { + return covariantMetricTensor.Getg_11(); +} +CovariantMetricTensor::FieldMetric Coordinates::g_22() const { + return covariantMetricTensor.Getg_22(); +} +CovariantMetricTensor::FieldMetric Coordinates::g_33() const { + return covariantMetricTensor.Getg_33(); +} +CovariantMetricTensor::FieldMetric Coordinates::g_12() const { + return covariantMetricTensor.Getg_12(); +} +CovariantMetricTensor::FieldMetric Coordinates::g_13() const { + return covariantMetricTensor.Getg_13(); +} +CovariantMetricTensor::FieldMetric Coordinates::g_23() const { + return covariantMetricTensor.Getg_23(); } void Coordinates::setCovariantMetricTensor(CovariantMetricTensor metric_tensor) { diff --git a/src/mesh/coordinates_accessor.cxx b/src/mesh/coordinates_accessor.cxx index 47871c0917..102251a62c 100644 --- a/src/mesh/coordinates_accessor.cxx +++ b/src/mesh/coordinates_accessor.cxx @@ -61,8 +61,7 @@ CoordinatesAccessor::CoordinatesAccessor(const Coordinates* coords) { coords->Bxy.ydown()[ind]; COPY_STRIPE(G1, G3); - const auto contravariant_components = - coords->getContravariantMetricTensor(); + const auto contravariant_components = coords->getContravariantMetricTensor(); // COPY_STRIPE(g11, g12, g13, g22, g23, g33); data[stripe_size * ind.ind + static_cast(Offset::g11)] = contravariant_components.g11[ind]; @@ -78,19 +77,12 @@ CoordinatesAccessor::CoordinatesAccessor(const Coordinates* coords) { contravariant_components.g33[ind]; // COPY_STRIPE(g_11, g_12, g_13, g_22, g_23, g_33); - const auto covariant_components = coords->getCovariantMetricTensor(); - data[stripe_size * ind.ind + static_cast(Offset::g_11)] = - covariant_components.g_11[ind]; - data[stripe_size * ind.ind + static_cast(Offset::g_12)] = - covariant_components.g_12[ind]; - data[stripe_size * ind.ind + static_cast(Offset::g_13)] = - covariant_components.g_13[ind]; - data[stripe_size * ind.ind + static_cast(Offset::g_22)] = - covariant_components.g_22[ind]; - data[stripe_size * ind.ind + static_cast(Offset::g_23)] = - covariant_components.g_23[ind]; - data[stripe_size * ind.ind + static_cast(Offset::g_33)] = - covariant_components.g_33[ind]; + data[stripe_size * ind.ind + static_cast(Offset::g_11)] = coords->g_11()[ind]; + data[stripe_size * ind.ind + static_cast(Offset::g_12)] = coords->g_12()[ind]; + data[stripe_size * ind.ind + static_cast(Offset::g_13)] = coords->g_13()[ind]; + data[stripe_size * ind.ind + static_cast(Offset::g_22)] = coords->g_22()[ind]; + data[stripe_size * ind.ind + static_cast(Offset::g_23)] = coords->g_23()[ind]; + data[stripe_size * ind.ind + static_cast(Offset::g_33)] = coords->g_33()[ind]; } } diff --git a/src/mesh/difops.cxx b/src/mesh/difops.cxx index 8399f7fc75..4ad956b336 100644 --- a/src/mesh/difops.cxx +++ b/src/mesh/difops.cxx @@ -101,11 +101,10 @@ Field3D Grad_parP(const Field3D& apar, const Field3D& f) { } } - const auto covariant_components = metric->getCovariantMetricTensor(); for (int x = 1; x <= mesh->LocalNx - 2; x++) { for (int y = mesh->ystart; y <= mesh->yend; y++) { for (int z = 0; z < ncz; z++) { - BoutReal by = 1. / sqrt(covariant_components.g_22(x, y, z)); + BoutReal by = 1. / sqrt(metric->g_22()(x, y, z)); // Z indices zm and zp int zm = (z - 1 + ncz) % ncz; int zp = (z + 1) % ncz; @@ -248,8 +247,6 @@ Field3D Div_par(const Field3D& f, const Field3D& v) { Coordinates* coord = f.getCoordinates(); - const auto covariant_components = coord->getCovariantMetricTensor(); - for (int i = mesh->xstart; i <= mesh->xend; i++) { for (int j = mesh->ystart; j <= mesh->yend; j++) { for (int k = mesh->zstart; k <= mesh->zend; k++) { @@ -263,12 +260,12 @@ Field3D Div_par(const Field3D& f, const Field3D& v) { // Calculate flux at right boundary (y+1/2) BoutReal fluxRight = fR * vR * (coord->J(i, j, k) + coord->J(i, j + 1, k)) - / (sqrt(covariant_components.g_22(i, j, k)) + sqrt(covariant_components.g_22(i, j + 1, k))); + / (sqrt(coord->g_22()(i, j, k)) + sqrt(coord->g_22()(i, j + 1, k))); // Calculate at left boundary (y-1/2) BoutReal fluxLeft = fL * vL * (coord->J(i, j, k) + coord->J(i, j - 1, k)) - / (sqrt(covariant_components.g_22(i, j, k)) + sqrt(covariant_components.g_22(i, j - 1, k))); + / (sqrt(coord->g_22()(i, j, k)) + sqrt(coord->g_22()(i, j - 1, k))); result(i, j, k) = (fluxRight - fluxLeft) / (coord->dy(i, j, k) * coord->J(i, j, k)); @@ -288,9 +285,7 @@ Field3D Div_par_flux(const Field3D& v, const Field3D& f, CELL_LOC outloc, auto Bxy_floc = f.getCoordinates()->Bxy; if (!f.hasParallelSlices()) { - return metric->Bxy - * FDDY(v, f / Bxy_floc, outloc, method) - / sqrt(metric->getCovariantMetricTensor().g_22); + return metric->Bxy * FDDY(v, f / Bxy_floc, outloc, method) / sqrt(metric->g_22()); } // Need to modify yup and ydown fields @@ -299,9 +294,7 @@ Field3D Div_par_flux(const Field3D& v, const Field3D& f, CELL_LOC outloc, f_B.splitParallelSlices(); f_B.yup() = f.yup() / Bxy_floc; f_B.ydown() = f.ydown() / Bxy_floc; - return metric->Bxy - * FDDY(v, f_B, outloc, method) - / sqrt(metric->getCovariantMetricTensor().g_22); + return metric->Bxy * FDDY(v, f_B, outloc, method) / sqrt(metric->g_22()); } Field3D Div_par_flux(const Field3D& v, const Field3D& f, const std::string& method, @@ -479,15 +472,13 @@ Coordinates::FieldMetric b0xGrad_dot_Grad(const Field2D& phi, const Field2D& A, Coordinates::FieldMetric dpdx = DDX(phi, outloc); Coordinates::FieldMetric dpdy = DDY(phi, outloc); - const auto covariant_components = metric->getCovariantMetricTensor(); - // Calculate advection velocity - Coordinates::FieldMetric vx = -covariant_components.g_23 * dpdy; - Coordinates::FieldMetric vy = covariant_components.g_23 * dpdx; + Coordinates::FieldMetric vx = -metric->g_23() * dpdy; + Coordinates::FieldMetric vy = metric->g_23() * dpdx; // Upwind A using these velocities Coordinates::FieldMetric result = VDDX(vx, A, outloc) + VDDY(vy, A, outloc); - result /= metric->J * sqrt(covariant_components.g_22); + result /= metric->J * sqrt(metric->g_22()); ASSERT1(result.getLocation() == outloc); @@ -514,12 +505,10 @@ Field3D b0xGrad_dot_Grad(const Field2D& phi, const Field3D& A, CELL_LOC outloc) Coordinates::FieldMetric dpdx = DDX(phi, outloc); Coordinates::FieldMetric dpdy = DDY(phi, outloc); - const auto covariant_components = metric->getCovariantMetricTensor(); - // Calculate advection velocity - Coordinates::FieldMetric vx = -covariant_components.g_23 * dpdy; - Coordinates::FieldMetric vy = covariant_components.g_23 * dpdx; - Coordinates::FieldMetric vz = covariant_components.g_12 * dpdy - covariant_components.g_22 * dpdx; + Coordinates::FieldMetric vx = -metric->g_23() * dpdy; + Coordinates::FieldMetric vy = metric->g_23() * dpdx; + Coordinates::FieldMetric vz = metric->g_12() * dpdy - metric->g_22() * dpdx; if (mesh->IncIntShear) { // BOUT-06 style differencing @@ -530,7 +519,7 @@ Field3D b0xGrad_dot_Grad(const Field2D& phi, const Field3D& A, CELL_LOC outloc) Field3D result = VDDX(vx, A, outloc) + VDDY(vy, A, outloc) + VDDZ(vz, A, outloc); - result /= (metric->J * sqrt(covariant_components.g_22)); + result /= (metric->J * sqrt(metric->g_22())); #if BOUT_USE_TRACK result.name = "b0xGrad_dot_Grad(" + phi.name + "," + A.name + ")"; @@ -557,17 +546,15 @@ Field3D b0xGrad_dot_Grad(const Field3D& p, const Field2D& A, CELL_LOC outloc) { Field3D dpdy = DDY(p, outloc); Field3D dpdz = DDZ(p, outloc); - const auto covariant_components = metric->getCovariantMetricTensor(); - // Calculate advection velocity - Field3D vx = covariant_components.g_22 * dpdz - covariant_components.g_23 * dpdy; - Field3D vy = covariant_components.g_23 * dpdx - covariant_components.g_12 * dpdz; + Field3D vx = metric->g_22() * dpdz - metric->g_23() * dpdy; + Field3D vy = metric->g_23() * dpdx - metric->g_12() * dpdz; // Upwind A using these velocities Field3D result = VDDX(vx, A, outloc) + VDDY(vy, A, outloc); - result /= (metric->J * sqrt(covariant_components.g_22)); + result /= (metric->J * sqrt(metric->g_22())); #if BOUT_USE_TRACK result.name = "b0xGrad_dot_Grad(" + p.name + "," + A.name + ")"; @@ -596,12 +583,10 @@ Field3D b0xGrad_dot_Grad(const Field3D& phi, const Field3D& A, CELL_LOC outloc) Field3D dpdy = DDY(phi, outloc); Field3D dpdz = DDZ(phi, outloc); - const auto covariant_components = metric->getCovariantMetricTensor(); - // Calculate advection velocity - Field3D vx = covariant_components.g_22 * dpdz - covariant_components.g_23 * dpdy; - Field3D vy = covariant_components.g_23 * dpdx - covariant_components.g_12 * dpdz; - Field3D vz = covariant_components.g_12 * dpdy - covariant_components.g_22 * dpdx; + Field3D vx = metric->g_22() * dpdz - metric->g_23() * dpdy; + Field3D vy = metric->g_23() * dpdx - metric->g_12() * dpdz; + Field3D vz = metric->g_12() * dpdy - metric->g_22() * dpdx; if (mesh->IncIntShear) { // BOUT-06 style differencing @@ -610,7 +595,7 @@ Field3D b0xGrad_dot_Grad(const Field3D& phi, const Field3D& A, CELL_LOC outloc) Field3D result = VDDX(vx, A, outloc) + VDDY(vy, A, outloc) + VDDZ(vz, A, outloc); - result /= (metric->J * sqrt(covariant_components.g_22)); + result /= (metric->J * sqrt(metric->g_22())); #if BOUT_USE_TRACK result.name = "b0xGrad_dot_Grad(" + phi.name + "," + A.name + ")"; diff --git a/src/mesh/fv_ops.cxx b/src/mesh/fv_ops.cxx index d55a461397..234ded5ab0 100644 --- a/src/mesh/fv_ops.cxx +++ b/src/mesh/fv_ops.cxx @@ -70,8 +70,7 @@ Field3D Div_a_Grad_perp(const Field3D& a, const Field3D& f) { // 3D Metric, need yup/ydown fields. // Requires previous communication of metrics // -- should insert communication here? - const auto covariant_components = coord->getCovariantMetricTensor(); - if (!g.g23.hasParallelSlices() || !covariant_components.g_23.hasParallelSlices() + if (!g.g23.hasParallelSlices() || !coord->g_23().hasParallelSlices() || !coord->dy.hasParallelSlices() || !coord->dz.hasParallelSlices() || !coord->Bxy.hasParallelSlices() || !coord->J.hasParallelSlices()) { throw BoutException("metrics have no yup/down: Maybe communicate in init?"); @@ -90,8 +89,7 @@ Field3D Div_a_Grad_perp(const Field3D& a, const Field3D& f) { // Only in 3D case with FCI do the metrics have parallel slices const bool metric_fci = fci and bout::build::use_metric_3d; const auto g23 = makeslices(metric_fci, g.g23); - const auto g_23 = makeslices(metric_fci, - coord->getCovariantMetricTensor().g_23); + const auto g_23 = makeslices(metric_fci, coord->g_23()); const auto J = makeslices(metric_fci, coord->J); const auto dy = makeslices(metric_fci, coord->dy); const auto dz = makeslices(metric_fci, coord->dz); @@ -211,14 +209,12 @@ const Field3D Div_par_K_Grad_par(const Field3D& Kin, const Field3D& fin, const auto iyp = i.yp(); const auto iym = i.ym(); - const auto covariant_components = coord->getCovariantMetricTensor(); - if (bndry_flux || mesh->periodicY(i.x()) || !mesh->lastY(i.x()) || (i.y() != mesh->yend)) { BoutReal c = 0.5 * (K[i] + Kup[iyp]); // K at the upper boundary BoutReal J = 0.5 * (coord->J[i] + coord->J[iyp]); // Jacobian at boundary - BoutReal g_22 = 0.5 * (covariant_components.g_22[i] + covariant_components.g_22[iyp]); + BoutReal g_22 = 0.5 * (coord->g_22()[i] + coord->g_22()[iyp]); BoutReal gradient = 2. * (fup[iyp] - f[i]) / (coord->dy[i] + coord->dy[iyp]); @@ -233,7 +229,7 @@ const Field3D Div_par_K_Grad_par(const Field3D& Kin, const Field3D& fin, BoutReal c = 0.5 * (K[i] + Kdown[iym]); // K at the lower boundary BoutReal J = 0.5 * (coord->J[i] + coord->J[iym]); // Jacobian at boundary - BoutReal g_22 = 0.5 * (covariant_components.g_22[i] + covariant_components.g_22[iym]); + BoutReal g_22 = 0.5 * (coord->g_22()[i] + coord->g_22()[iym]); BoutReal gradient = 2. * (f[i] - fdown[iym]) / (coord->dy[i] + coord->dy[iym]); @@ -516,29 +512,27 @@ Field3D Div_Perp_Lap(const Field3D& a, const Field3D& f, CELL_LOC outloc) { // Calculate gradients on cell faces -- assumes constant grid spacing BoutReal gR = - (g.g11(i, j, k) + g.g11(i + 1, j, k)) - * (f(i + 1, j, k) - f(i, j, k)) + (g.g11(i, j, k) + g.g11(i + 1, j, k)) * (f(i + 1, j, k) - f(i, j, k)) / (coords->dx(i + 1, j, k) + coords->dx(i, j, k)) + 0.5 * (g.g13(i, j, k) + g.g13(i + 1, j, k)) * (f(i + 1, j, kp) - f(i + 1, j, km) + f(i, j, kp) - f(i, j, km)) / (4. * coords->dz(i, j, k)); BoutReal gL = - (g.g11(i - 1, j, k) + g.g11(i, j, k)) - * (f(i, j, k) - f(i - 1, j, k)) + (g.g11(i - 1, j, k) + g.g11(i, j, k)) * (f(i, j, k) - f(i - 1, j, k)) / (coords->dx(i - 1, j, k) + coords->dx(i, j, k)) + 0.5 * (g.g13(i - 1, j, k) + g.g13(i, j, k)) * (f(i - 1, j, kp) - f(i - 1, j, km) + f(i, j, kp) - f(i, j, km)) / (4 * coords->dz(i, j, k)); BoutReal gD = - g.g13(i, j, k) + g.g13(i, j, k) * (f(i + 1, j, km) - f(i - 1, j, km) + f(i + 1, j, k) - f(i - 1, j, k)) / (4. * coords->dx(i, j, k)) + g.g33(i, j, k) * (f(i, j, k) - f(i, j, km)) / coords->dz(i, j, k); BoutReal gU = - g.g13(i, j, k) + g.g13(i, j, k) * (f(i + 1, j, kp) - f(i - 1, j, kp) + f(i + 1, j, k) - f(i - 1, j, k)) / (4. * coords->dx(i, j, k)) + g.g33(i, j, k) * (f(i, j, kp) - f(i, j, k)) / coords->dz(i, j, k); diff --git a/tests/unit/invert/laplace/test_laplace_cyclic.cxx b/tests/unit/invert/laplace/test_laplace_cyclic.cxx index 0a40630a34..3f9cd57799 100644 --- a/tests/unit/invert/laplace/test_laplace_cyclic.cxx +++ b/tests/unit/invert/laplace/test_laplace_cyclic.cxx @@ -40,9 +40,11 @@ class CyclicForwardOperator { const Field3D operator()(Field3D& f) { const auto contravariant_components = coords->getContravariantMetricTensor(); - auto result = d * Delp2(f) - + (contravariant_components.g11 * DDX(f) + contravariant_components.g13 * DDZ(f)) * DDX(c2) / c1 + a * f - + ex * DDX(f) + ez * DDZ(f); + auto result = + d * Delp2(f) + + (contravariant_components.g11 * DDX(f) + contravariant_components.g13 * DDZ(f)) + * DDX(c2) / c1 + + a * f + ex * DDX(f) + ez * DDZ(f); applyBoundaries(result, f); return result; } @@ -52,10 +54,9 @@ class CyclicForwardOperator { bool inner_x_neumann, outer_x_neumann; // If false then use Dirichlet conditions void applyBoundaries(Field3D& newF, const Field3D& f) { - const auto covariant_components = coords->getCovariantMetricTensor(); BOUT_FOR(i, f.getMesh()->getRegion3D("RGN_INNER_X")) { if (inner_x_neumann) { - newF[i] = (f[i.xp()] - f[i]) / coords->dx[i] / sqrt(covariant_components.g_11[i]); + newF[i] = (f[i.xp()] - f[i]) / coords->dx[i] / sqrt(coords->g_11()[i]); } else { newF[i] = 0.5 * (f[i] + f[i.xp()]); } @@ -63,7 +64,7 @@ class CyclicForwardOperator { BOUT_FOR(i, f.getMesh()->getRegion3D("RGN_OUTER_X")) { if (outer_x_neumann) { - newF[i] = (f[i] - f[i.xm()]) / coords->dx[i] / sqrt(covariant_components.g_11[i]); + newF[i] = (f[i] - f[i.xm()]) / coords->dx[i] / sqrt(coords->g_11()[i]); } else { newF[i] = 0.5 * (f[i.xm()] + f[i]); } diff --git a/tests/unit/mesh/test_coordinates.cxx b/tests/unit/mesh/test_coordinates.cxx index 97ae9a10fb..a6f0e741eb 100644 --- a/tests/unit/mesh/test_coordinates.cxx +++ b/tests/unit/mesh/test_coordinates.cxx @@ -143,14 +143,12 @@ TEST_F(CoordinatesTest, CalcContravariant) { coords.calcCovariant(); output_info.enable(); - const auto updated_coords = coords.getCovariantMetricTensor(); - - EXPECT_TRUE(IsFieldEqual(updated_coords.g_11, 1.0)); - EXPECT_TRUE(IsFieldEqual(updated_coords.g_22, 1.0)); - EXPECT_TRUE(IsFieldEqual(updated_coords.g_33, 1.0)); - EXPECT_TRUE(IsFieldEqual(updated_coords.g_12, 0.0)); - EXPECT_TRUE(IsFieldEqual(updated_coords.g_13, 0.0)); - EXPECT_TRUE(IsFieldEqual(updated_coords.g_23, 0.0)); + EXPECT_TRUE(IsFieldEqual(coords.g_11(), 1.0)); + EXPECT_TRUE(IsFieldEqual(coords.g_22(), 1.0)); + EXPECT_TRUE(IsFieldEqual(coords.g_33(), 1.0)); + EXPECT_TRUE(IsFieldEqual(coords.g_12(), 0.0)); + EXPECT_TRUE(IsFieldEqual(coords.g_13(), 0.0)); + EXPECT_TRUE(IsFieldEqual(coords.g_23(), 0.0)); } TEST_F(CoordinatesTest, CalcCovariant) { @@ -283,10 +281,9 @@ TEST_F(CoordinatesTest, ConstructWithDiagonalContravariantMetric) { // Covariant metric should be inverse // Note: Not calculated in corners - const auto updated_coords = coords.getCovariantMetricTensor(); - EXPECT_TRUE(IsFieldEqual(updated_coords.g_11, 1. / 2.0, "RGN_NOCORNERS")); - EXPECT_TRUE(IsFieldEqual(updated_coords.g_22, 1. / 3.2, "RGN_NOCORNERS")); - EXPECT_TRUE(IsFieldEqual(updated_coords.g_33, 1. / 42, "RGN_NOCORNERS")); + EXPECT_TRUE(IsFieldEqual(coords.g_11(), 1. / 2.0, "RGN_NOCORNERS")); + EXPECT_TRUE(IsFieldEqual(coords.g_22(), 1. / 3.2, "RGN_NOCORNERS")); + EXPECT_TRUE(IsFieldEqual(coords.g_33(), 1. / 42, "RGN_NOCORNERS")); EXPECT_TRUE(IsFieldEqual(coords.J, 1. / sqrt(2.0 * 3.2 * 42), "RGN_NOCORNERS")); EXPECT_TRUE(IsFieldEqual(coords.Bxy, sqrt(2.0 * 42), "RGN_NOCORNERS", 1e-10)); @@ -405,14 +402,12 @@ TEST_F(CoordinatesTest, GetCovariantMetricTensor) { FieldMetric{0.0}, // ShiftTorsion FieldMetric{0.0}}; // IntShiftTorsion - const auto covariant_components = coords.getCovariantMetricTensor(); - - EXPECT_TRUE(IsFieldEqual(covariant_components.g_11, 9.7)); - EXPECT_TRUE(IsFieldEqual(covariant_components.g_22, 7.5)); - EXPECT_TRUE(IsFieldEqual(covariant_components.g_33, 4.7)); - EXPECT_TRUE(IsFieldEqual(covariant_components.g_12, 3.9)); - EXPECT_TRUE(IsFieldEqual(covariant_components.g_13, 1.7)); - EXPECT_TRUE(IsFieldEqual(covariant_components.g_23, 5.3)); + EXPECT_TRUE(IsFieldEqual(coords.g_11(), 9.7)); + EXPECT_TRUE(IsFieldEqual(coords.g_22(), 7.5)); + EXPECT_TRUE(IsFieldEqual(coords.g_33(), 4.7)); + EXPECT_TRUE(IsFieldEqual(coords.g_12(), 3.9)); + EXPECT_TRUE(IsFieldEqual(coords.g_13(), 1.7)); + EXPECT_TRUE(IsFieldEqual(coords.g_23(), 5.3)); } TEST_F(CoordinatesTest, SetCovariantMetricTensor) { @@ -444,12 +439,11 @@ TEST_F(CoordinatesTest, SetCovariantMetricTensor) { coords.setCovariantMetricTensor(updated_metric_tensor); // Get values with getter and check they have been modified as expected - const auto g = coords.getCovariantMetricTensor(); - EXPECT_TRUE(IsFieldEqual(g.g_11, 1.7)); - EXPECT_TRUE(IsFieldEqual(g.g_22, 2.3)); - EXPECT_TRUE(IsFieldEqual(g.g_33, 3.1)); - EXPECT_TRUE(IsFieldEqual(g.g_12, 0.9)); - EXPECT_TRUE(IsFieldEqual(g.g_13, 5.7)); - EXPECT_TRUE(IsFieldEqual(g.g_23, 1.9)); + EXPECT_TRUE(IsFieldEqual(coords.g_11(), 1.7)); + EXPECT_TRUE(IsFieldEqual(coords.g_22(), 2.3)); + EXPECT_TRUE(IsFieldEqual(coords.g_33(), 3.1)); + EXPECT_TRUE(IsFieldEqual(coords.g_12(), 0.9)); + EXPECT_TRUE(IsFieldEqual(coords.g_13(), 5.7)); + EXPECT_TRUE(IsFieldEqual(coords.g_23(), 1.9)); } } \ No newline at end of file From 931524a63a01c9321f46f86f5bd0a76c719be682 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 18 Oct 2023 15:13:54 +0100 Subject: [PATCH 097/491] Make metric tensor readonly, and pass it by reference [Unfinished]. --- include/bout/ContravariantMetricTensor.hxx | 8 ++++---- include/bout/CovariantMetricTensor.hxx | 12 ++++++------ include/bout/coordinates.hxx | 12 ++++++------ src/mesh/ContravariantMetricTensor.cpp | 6 +++--- src/mesh/CovariantMetricTensor.cpp | 12 ++++++------ src/mesh/coordinates.cxx | 20 ++++++++++---------- 6 files changed, 35 insertions(+), 35 deletions(-) diff --git a/include/bout/ContravariantMetricTensor.hxx b/include/bout/ContravariantMetricTensor.hxx index 7dfee2b32e..1b7075740f 100644 --- a/include/bout/ContravariantMetricTensor.hxx +++ b/include/bout/ContravariantMetricTensor.hxx @@ -19,12 +19,12 @@ public: #endif struct ContravariantComponents { - FieldMetric g11, g22, g33, g12, g13, g23; + const FieldMetric g11, g22, g33, g12, g13, g23; }; - ContravariantMetricTensor(const FieldMetric g11, const FieldMetric g22, - const FieldMetric g33, const FieldMetric g12, - const FieldMetric g13, const FieldMetric g23); + ContravariantMetricTensor(const FieldMetric& g11, const FieldMetric& g22, + const FieldMetric& g33, const FieldMetric& g12, + const FieldMetric& g13, const FieldMetric& g23); ContravariantMetricTensor(const BoutReal g11, const BoutReal g22, const BoutReal g33, const BoutReal g12, const BoutReal g13, const BoutReal g23, diff --git a/include/bout/CovariantMetricTensor.hxx b/include/bout/CovariantMetricTensor.hxx index 03cce5d04f..3a93c157a3 100644 --- a/include/bout/CovariantMetricTensor.hxx +++ b/include/bout/CovariantMetricTensor.hxx @@ -30,12 +30,12 @@ public: // check that covariant tensors are positive (if expected) and finite (always) void checkCovariant(int ystart); - FieldMetric Getg_11() const; - FieldMetric Getg_22() const; - FieldMetric Getg_33() const; - FieldMetric Getg_12() const; - FieldMetric Getg_13() const; - FieldMetric Getg_23() const; + const FieldMetric& Getg_11() const; + const FieldMetric& Getg_22() const; + const FieldMetric& Getg_33() const; + const FieldMetric& Getg_12() const; + const FieldMetric& Getg_13() const; + const FieldMetric& Getg_23() const; void setCovariantMetricTensor(CELL_LOC location, const CovariantMetricTensor& metric_tensor); diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index 9f79f52201..a2e2d6f1f2 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -105,12 +105,12 @@ private: public: ContravariantMetricTensor::ContravariantComponents getContravariantMetricTensor() const; - FieldMetric g_11() const; - FieldMetric g_22() const; - FieldMetric g_33() const; - FieldMetric g_12() const; - FieldMetric g_13() const; - FieldMetric g_23() const; + const FieldMetric& g_11() const; + const FieldMetric& g_22() const; + const FieldMetric& g_33() const; + const FieldMetric& g_12() const; + const FieldMetric& g_13() const; + const FieldMetric& g_23() const; void setContravariantMetricTensor(ContravariantMetricTensor metric_tensor); diff --git a/src/mesh/ContravariantMetricTensor.cpp b/src/mesh/ContravariantMetricTensor.cpp index c737fb1376..d7d1ba01ad 100644 --- a/src/mesh/ContravariantMetricTensor.cpp +++ b/src/mesh/ContravariantMetricTensor.cpp @@ -3,8 +3,8 @@ #include "bout/CovariantMetricTensor.hxx" ContravariantMetricTensor::ContravariantMetricTensor( - const FieldMetric g11, const FieldMetric g22, const FieldMetric g33, - const FieldMetric g12, const FieldMetric g13, const FieldMetric g23) + const FieldMetric& g11, const FieldMetric& g22, const FieldMetric& g33, + const FieldMetric& g12, const FieldMetric& g13, const FieldMetric& g23) : contravariant_components({FieldMetric(std::move(g11)), FieldMetric(std::move(g22)), FieldMetric(std::move(g33)), FieldMetric(std::move(g12)), FieldMetric(std::move(g13)), @@ -76,7 +76,7 @@ ContravariantMetricTensor::calcCovariant(const CELL_LOC location, CovariantMetricTensor covariantMetricTensor = CovariantMetricTensor(a(0, 0), a(1, 1), a(2, 2), a(0, 1), a(0, 2), a(1, 2), mesh); - this->setLocation(location); + setLocation(location); covariantMetricTensor.setLocation(location); BoutReal maxerr; diff --git a/src/mesh/CovariantMetricTensor.cpp b/src/mesh/CovariantMetricTensor.cpp index 221f054694..d1b3a5e356 100644 --- a/src/mesh/CovariantMetricTensor.cpp +++ b/src/mesh/CovariantMetricTensor.cpp @@ -23,12 +23,12 @@ CovariantMetricTensor::CovariantMetricTensor(const BoutReal g_11, const BoutReal Allocate(); // Make sure metric elements are allocated // ; TODO: Required? } -CovariantMetricTensor::FieldMetric CovariantMetricTensor::Getg_11() const { return g_11; } -CovariantMetricTensor::FieldMetric CovariantMetricTensor::Getg_22() const { return g_22; } -CovariantMetricTensor::FieldMetric CovariantMetricTensor::Getg_33() const { return g_33; } -CovariantMetricTensor::FieldMetric CovariantMetricTensor::Getg_12() const { return g_12; } -CovariantMetricTensor::FieldMetric CovariantMetricTensor::Getg_13() const { return g_13; } -CovariantMetricTensor::FieldMetric CovariantMetricTensor::Getg_23() const { return g_23; } +const CovariantMetricTensor::FieldMetric& CovariantMetricTensor::Getg_11() const { return g_11; } +const CovariantMetricTensor::FieldMetric& CovariantMetricTensor::Getg_22() const { return g_22; } +const CovariantMetricTensor::FieldMetric& CovariantMetricTensor::Getg_33() const { return g_33; } +const CovariantMetricTensor::FieldMetric& CovariantMetricTensor::Getg_12() const { return g_12; } +const CovariantMetricTensor::FieldMetric& CovariantMetricTensor::Getg_13() const { return g_13; } +const CovariantMetricTensor::FieldMetric& CovariantMetricTensor::Getg_23() const { return g_23; } void CovariantMetricTensor::setCovariantMetricTensor( CELL_LOC location, const CovariantMetricTensor& metric_tensor) { diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index b980193011..3985e64372 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -39,7 +39,7 @@ void communicate(T& t, Ts... ts) { /// Boundary guard cells are set by extrapolating from the grid, like /// 'free_o3' boundary conditions /// Corner guard cells are set to BoutNaN -Field2D interpolateAndExtrapolate(const Field2D f, CELL_LOC location, bool extrapolate_x, +const Field2D& interpolateAndExtrapolate(const Field2D& f, CELL_LOC location, bool extrapolate_x, bool extrapolate_y, bool no_extra_interpolate, ParallelTransform* UNUSED(pt) = nullptr) { @@ -322,7 +322,7 @@ void checkStaggeredGet(Mesh* mesh, const std::string& name, const std::string& s } // convenience function for repeated code -int getAtLoc(Mesh* mesh, Coordinates::FieldMetric var, const std::string& name, +int getAtLoc(Mesh* mesh, Coordinates::FieldMetric& var, const std::string& name, const std::string& suffix, CELL_LOC location, BoutReal default_value = 0.) { checkStaggeredGet(mesh, name, suffix); @@ -446,7 +446,7 @@ Coordinates::Coordinates(Mesh* mesh, Options* options) dy = interpolateAndExtrapolate(dy, location, extrapolate_x, extrapolate_y, false, transform.get()); - auto getUnaligned = [this](auto field, const std::string& name, + auto getUnaligned = [this](const auto field, const std::string& name, BoutReal default_value) { localmesh->get(field, name, default_value, false); if (field.getDirectionY() == YDirectionType::Aligned @@ -458,7 +458,7 @@ Coordinates::Coordinates(Mesh* mesh, Options* options) }; auto getUnalignedAtLocation = [this, extrapolate_x, extrapolate_y, - getUnaligned](auto& field, const std::string& name, + getUnaligned](const auto& field, const std::string& name, BoutReal default_value) { field = getUnaligned(field, name, default_value); return interpolateAndExtrapolate(field, location, extrapolate_x, extrapolate_y, false, @@ -2031,22 +2031,22 @@ Coordinates::getContravariantMetricTensor() const { return contravariantMetricTensor.getContravariantMetricTensor(); } -CovariantMetricTensor::FieldMetric Coordinates::g_11() const { +const CovariantMetricTensor::FieldMetric& Coordinates::g_11() const { return covariantMetricTensor.Getg_11(); } -CovariantMetricTensor::FieldMetric Coordinates::g_22() const { +const CovariantMetricTensor::FieldMetric& Coordinates::g_22() const { return covariantMetricTensor.Getg_22(); } -CovariantMetricTensor::FieldMetric Coordinates::g_33() const { +const CovariantMetricTensor::FieldMetric& Coordinates::g_33() const { return covariantMetricTensor.Getg_33(); } -CovariantMetricTensor::FieldMetric Coordinates::g_12() const { +const CovariantMetricTensor::FieldMetric& Coordinates::g_12() const { return covariantMetricTensor.Getg_12(); } -CovariantMetricTensor::FieldMetric Coordinates::g_13() const { +const CovariantMetricTensor::FieldMetric& Coordinates::g_13() const { return covariantMetricTensor.Getg_13(); } -CovariantMetricTensor::FieldMetric Coordinates::g_23() const { +const CovariantMetricTensor::FieldMetric& Coordinates::g_23() const { return covariantMetricTensor.Getg_23(); } From 3b6d1f275e31cba6c359d619b3500e3b04fa4193 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 18 Oct 2023 18:21:01 +0100 Subject: [PATCH 098/491] Moved calcContravariant function onto ContravariantMetricTensor class. Added an overloaded Mesh::get method that returns a value rather than using and 'out' parameter. Components of ContravariantComponents can't be const because setLocation() and allocate() have to modify them. Create new variables for components to pass to getUnalignedAtLocation method, since they are modified by setDirectionY function before being passed back. --- include/bout/ContravariantMetricTensor.hxx | 6 +- include/bout/CovariantMetricTensor.hxx | 7 +- include/bout/mesh.hxx | 13 +++ src/mesh/ContravariantMetricTensor.cpp | 66 +++++++++++ src/mesh/CovariantMetricTensor.cpp | 89 ++++----------- src/mesh/coordinates.cxx | 122 +++++++++++++-------- src/mesh/mesh.cxx | 25 +++++ 7 files changed, 207 insertions(+), 121 deletions(-) diff --git a/include/bout/ContravariantMetricTensor.hxx b/include/bout/ContravariantMetricTensor.hxx index 1b7075740f..f9098e7833 100644 --- a/include/bout/ContravariantMetricTensor.hxx +++ b/include/bout/ContravariantMetricTensor.hxx @@ -19,7 +19,7 @@ public: #endif struct ContravariantComponents { - const FieldMetric g11, g22, g33, g12, g13, g23; + FieldMetric g11, g22, g33, g12, g13, g23; }; ContravariantMetricTensor(const FieldMetric& g11, const FieldMetric& g22, @@ -46,6 +46,10 @@ public: void setLocation(const CELL_LOC location); + /// Invert covariant metric to get contravariant components + void calcContravariant(CovariantMetricTensor covariantMetricTensor, CELL_LOC location, + const std::string& region = "RGN_ALL"); + private: ContravariantComponents contravariant_components; }; diff --git a/include/bout/CovariantMetricTensor.hxx b/include/bout/CovariantMetricTensor.hxx index 3a93c157a3..211f6ce3a3 100644 --- a/include/bout/CovariantMetricTensor.hxx +++ b/include/bout/CovariantMetricTensor.hxx @@ -23,10 +23,6 @@ public: const BoutReal g_12, const BoutReal g_13, const BoutReal g_23, Mesh* mesh); - /// Invert covariant metric to get contravariant components - ContravariantMetricTensor calcContravariant(CELL_LOC location, - const std::string& region = "RGN_ALL"); - // check that covariant tensors are positive (if expected) and finite (always) void checkCovariant(int ystart); @@ -37,8 +33,7 @@ public: const FieldMetric& Getg_13() const; const FieldMetric& Getg_23() const; - void setCovariantMetricTensor(CELL_LOC location, - const CovariantMetricTensor& metric_tensor); + void setCovariantMetricTensor(const CovariantMetricTensor& metric_tensor); void Allocate(); diff --git a/include/bout/mesh.hxx b/include/bout/mesh.hxx index 3bc01d3787..22a32083e6 100644 --- a/include/bout/mesh.hxx +++ b/include/bout/mesh.hxx @@ -187,6 +187,19 @@ public: int get(Field2D& var, const std::string& name, BoutReal def = 0.0, bool communicate = true, CELL_LOC location = CELL_DEFAULT); + /// Get a Field2D from the input source + /// including communicating guard cells + /// This is a new version of the `get` function, that returns the value + /// avoiding the use of an out parameter + /// + /// @param[in] name Name of the variable to read + /// @param[in] def The default value if not found + /// @param[in] communicate Should the field be communicated to fill guard cells? + /// + /// @returns the value. Will be allocated if needed + Coordinates::FieldMetric& get(const std::string& name, BoutReal def = 0.0, + bool communicate = true, CELL_LOC location = CELL_DEFAULT); + /// Get a Field3D from the input source /// /// @param[out] var This will be set to the value. Will be allocated if needed diff --git a/src/mesh/ContravariantMetricTensor.cpp b/src/mesh/ContravariantMetricTensor.cpp index d7d1ba01ad..4d3f97d7c7 100644 --- a/src/mesh/ContravariantMetricTensor.cpp +++ b/src/mesh/ContravariantMetricTensor.cpp @@ -184,3 +184,69 @@ void ContravariantMetricTensor::setLocation(const CELL_LOC location) { contravariant_components.g13.setLocation(location); contravariant_components.g23.setLocation(location); } + +void ContravariantMetricTensor::calcContravariant( + CovariantMetricTensor covariantMetricTensor, CELL_LOC location, + const std::string& region) { + + TRACE("ContravariantMetricTensor::calcContravariant"); + + // Perform inversion of g_{ij} to get g^{ij} + // NOTE: Currently this bit assumes that metric terms are Field2D objects + + auto a = Matrix(3, 3); + + BOUT_FOR_SERIAL(i, covariantMetricTensor.Getg_11().getRegion(region)) { + a(0, 0) = covariantMetricTensor.Getg_11()[i]; + a(1, 1) = covariantMetricTensor.Getg_22()[i]; + a(2, 2) = covariantMetricTensor.Getg_33()[i]; + + a(0, 1) = a(1, 0) = covariantMetricTensor.Getg_12()[i]; + a(1, 2) = a(2, 1) = covariantMetricTensor.Getg_23()[i]; + a(0, 2) = a(2, 0) = covariantMetricTensor.Getg_13()[i]; + + if (invert3x3(a)) { + const auto error_message = "\tERROR: metric tensor is singular at ({:d}, {:d})\n"; + output_error.write(error_message, i.x(), i.y()); + throw BoutException(error_message); + } + } + + auto* const mesh = + covariantMetricTensor.Getg_11() + .getMesh(); //TODO: Add a getMesh() method to CovariantComponents? + ContravariantMetricTensor contravariantMetricTensor = ContravariantMetricTensor( + a(0, 0), a(1, 1), a(2, 2), a(0, 1), a(0, 2), a(1, 2), mesh); + + setLocation(location); + + BoutReal maxerr; + maxerr = + BOUTMAX(max(abs((covariantMetricTensor.Getg_11() * contravariant_components.g11 + + covariantMetricTensor.Getg_12() * contravariant_components.g12 + + covariantMetricTensor.Getg_13() * contravariant_components.g13) + - 1)), + max(abs((covariantMetricTensor.Getg_12() * contravariant_components.g12 + + covariantMetricTensor.Getg_22() * contravariant_components.g22 + + covariantMetricTensor.Getg_23() * contravariant_components.g23) + - 1)), + max(abs((covariantMetricTensor.Getg_13() * contravariant_components.g13 + + covariantMetricTensor.Getg_23() * contravariant_components.g23 + + covariantMetricTensor.Getg_33() * contravariant_components.g33) + - 1))); + + output_info.write("\tMaximum error in diagonal inversion is {:e}\n", maxerr); + + maxerr = + BOUTMAX(max(abs(covariantMetricTensor.Getg_11() * contravariant_components.g12 + + covariantMetricTensor.Getg_12() * contravariant_components.g22 + + covariantMetricTensor.Getg_13() * contravariant_components.g23)), + max(abs(covariantMetricTensor.Getg_11() * contravariant_components.g13 + + covariantMetricTensor.Getg_12() * contravariant_components.g23 + + covariantMetricTensor.Getg_13() * contravariant_components.g33)), + max(abs(covariantMetricTensor.Getg_12() * contravariant_components.g13 + + covariantMetricTensor.Getg_22() * contravariant_components.g23 + + covariantMetricTensor.Getg_23() * contravariant_components.g33))); + + output_info.write("\tMaximum error in off-diagonal inversion is {:e}\n", maxerr); +} diff --git a/src/mesh/CovariantMetricTensor.cpp b/src/mesh/CovariantMetricTensor.cpp index d1b3a5e356..3923fad6ca 100644 --- a/src/mesh/CovariantMetricTensor.cpp +++ b/src/mesh/CovariantMetricTensor.cpp @@ -23,15 +23,27 @@ CovariantMetricTensor::CovariantMetricTensor(const BoutReal g_11, const BoutReal Allocate(); // Make sure metric elements are allocated // ; TODO: Required? } -const CovariantMetricTensor::FieldMetric& CovariantMetricTensor::Getg_11() const { return g_11; } -const CovariantMetricTensor::FieldMetric& CovariantMetricTensor::Getg_22() const { return g_22; } -const CovariantMetricTensor::FieldMetric& CovariantMetricTensor::Getg_33() const { return g_33; } -const CovariantMetricTensor::FieldMetric& CovariantMetricTensor::Getg_12() const { return g_12; } -const CovariantMetricTensor::FieldMetric& CovariantMetricTensor::Getg_13() const { return g_13; } -const CovariantMetricTensor::FieldMetric& CovariantMetricTensor::Getg_23() const { return g_23; } +const CovariantMetricTensor::FieldMetric& CovariantMetricTensor::Getg_11() const { + return g_11; +} +const CovariantMetricTensor::FieldMetric& CovariantMetricTensor::Getg_22() const { + return g_22; +} +const CovariantMetricTensor::FieldMetric& CovariantMetricTensor::Getg_33() const { + return g_33; +} +const CovariantMetricTensor::FieldMetric& CovariantMetricTensor::Getg_12() const { + return g_12; +} +const CovariantMetricTensor::FieldMetric& CovariantMetricTensor::Getg_13() const { + return g_13; +} +const CovariantMetricTensor::FieldMetric& CovariantMetricTensor::Getg_23() const { + return g_23; +} void CovariantMetricTensor::setCovariantMetricTensor( - CELL_LOC location, const CovariantMetricTensor& metric_tensor) { + const CovariantMetricTensor& metric_tensor) { g_11 = metric_tensor.Getg_11(); g_22 = metric_tensor.Getg_22(); @@ -39,69 +51,6 @@ void CovariantMetricTensor::setCovariantMetricTensor( g_12 = metric_tensor.Getg_12(); g_13 = metric_tensor.Getg_13(); g_23 = metric_tensor.Getg_23(); - calcContravariant(location); -} - -ContravariantMetricTensor -CovariantMetricTensor::calcContravariant(CELL_LOC location, const std::string& region) { - TRACE("CovariantMetricTensor::calcContravariant"); - - // Perform inversion of g_{ij} to get g^{ij} - // NOTE: Currently this bit assumes that metric terms are Field2D objects - - auto a = Matrix(3, 3); - - BOUT_FOR_SERIAL(i, g_11.getRegion(region)) { - a(0, 0) = g_11[i]; - a(1, 1) = g_22[i]; - a(2, 2) = g_33[i]; - - a(0, 1) = a(1, 0) = g_12[i]; - a(1, 2) = a(2, 1) = g_23[i]; - a(0, 2) = a(2, 0) = g_13[i]; - - if (invert3x3(a)) { - const auto error_message = "\tERROR: metric tensor is singular at ({:d}, {:d})\n"; - output_error.write(error_message, i.x(), i.y()); - throw BoutException(error_message); - } - } - - auto* const mesh = - g_11.getMesh(); //TODO: Add a getMesh() method to CovariantComponents? - ContravariantMetricTensor contravariantMetricTensor = ContravariantMetricTensor( - a(0, 0), a(1, 1), a(2, 2), a(0, 1), a(0, 2), a(1, 2), mesh); - - contravariantMetricTensor.setLocation(location); - - auto contravariant_components = - contravariantMetricTensor.getContravariantMetricTensor(); - - BoutReal maxerr; - maxerr = BOUTMAX( - max(abs((g_11 * contravariant_components.g11 + g_12 * contravariant_components.g12 - + g_13 * contravariant_components.g13) - - 1)), - max(abs((g_12 * contravariant_components.g12 + g_22 * contravariant_components.g22 - + g_23 * contravariant_components.g23) - - 1)), - max(abs((g_13 * contravariant_components.g13 + g_23 * contravariant_components.g23 - + g_33 * contravariant_components.g33) - - 1))); - - output_info.write("\tMaximum error in diagonal inversion is {:e}\n", maxerr); - - maxerr = BOUTMAX( - max(abs(g_11 * contravariant_components.g12 + g_12 * contravariant_components.g22 - + g_13 * contravariant_components.g23)), - max(abs(g_11 * contravariant_components.g13 + g_12 * contravariant_components.g23 - + g_13 * contravariant_components.g33)), - max(abs(g_12 * contravariant_components.g13 + g_22 * contravariant_components.g23 - + g_23 * contravariant_components.g33))); - - output_info.write("\tMaximum error in off-diagonal inversion is {:e}\n", maxerr); - - return contravariantMetricTensor; } void CovariantMetricTensor::checkCovariant(int ystart) { diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 3985e64372..9ce7532f4c 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -39,9 +39,10 @@ void communicate(T& t, Ts... ts) { /// Boundary guard cells are set by extrapolating from the grid, like /// 'free_o3' boundary conditions /// Corner guard cells are set to BoutNaN -const Field2D& interpolateAndExtrapolate(const Field2D& f, CELL_LOC location, bool extrapolate_x, - bool extrapolate_y, bool no_extra_interpolate, - ParallelTransform* UNUSED(pt) = nullptr) { +const Field2D& interpolateAndExtrapolate(const Field2D& f, CELL_LOC location, + bool extrapolate_x, bool extrapolate_y, + bool no_extra_interpolate, + ParallelTransform* UNUSED(pt) = nullptr) { Mesh* localmesh = f.getMesh(); Field2D result = interp_to(f, location, "RGN_NOBNDRY"); @@ -446,9 +447,9 @@ Coordinates::Coordinates(Mesh* mesh, Options* options) dy = interpolateAndExtrapolate(dy, location, extrapolate_x, extrapolate_y, false, transform.get()); - auto getUnaligned = [this](const auto field, const std::string& name, + auto getUnaligned = [this](auto& field, const std::string& name, BoutReal default_value) { - localmesh->get(field, name, default_value, false); + field = localmesh->get(name, default_value, false); if (field.getDirectionY() == YDirectionType::Aligned and transform->canToFromFieldAligned()) { return transform->fromFieldAligned(field); @@ -458,7 +459,7 @@ Coordinates::Coordinates(Mesh* mesh, Options* options) }; auto getUnalignedAtLocation = [this, extrapolate_x, extrapolate_y, - getUnaligned](const auto& field, const std::string& name, + getUnaligned](auto& field, const std::string& name, BoutReal default_value) { field = getUnaligned(field, name, default_value); return interpolateAndExtrapolate(field, location, extrapolate_x, extrapolate_y, false, @@ -470,16 +471,29 @@ Coordinates::Coordinates(Mesh* mesh, Options* options) auto contravariant_components = contravariantMetricTensor.getContravariantMetricTensor(); + // FieldMetric old_g11 = contravariantMetricTensor.Getg11(); // non-const + // FieldMetric old_g22 = contravariantMetricTensor.Getg22(); // non-const + // FieldMetric old_g33 = contravariantMetricTensor.Getg33(); // non-const + // FieldMetric old_g12 = contravariantMetricTensor.Getg12(); // non-const + // FieldMetric old_g13 = contravariantMetricTensor.Getg13(); // non-const + // FieldMetric old_g23 = contravariantMetricTensor.Getg23(); // non-const + FieldMetric old_g11 = contravariant_components.g11; // non-const + FieldMetric old_g22 = contravariant_components.g22; // non-const + FieldMetric old_g33 = contravariant_components.g33; // non-const + FieldMetric old_g12 = contravariant_components.g12; // non-const + FieldMetric old_g13 = contravariant_components.g13; // non-const + FieldMetric old_g23 = contravariant_components.g23; // non-const + FieldMetric g11, g22, g33, g12, g13, g23; - g11 = getUnalignedAtLocation(contravariant_components.g11, "g11", 1.0); - g22 = getUnalignedAtLocation(contravariant_components.g22, "g22", 1.0); - g33 = getUnalignedAtLocation(contravariant_components.g33, "g33", 1.0); + g11 = getUnalignedAtLocation(old_g11, "g11", 1.0); + g22 = getUnalignedAtLocation(old_g22, "g22", 1.0); + g33 = getUnalignedAtLocation(old_g33, "g33", 1.0); // Off-diagonal elements. Default to 0 - g12 = getUnalignedAtLocation(contravariant_components.g12, "g12", 0.0); - g13 = getUnalignedAtLocation(contravariant_components.g13, "g13", 0.0); - g23 = getUnalignedAtLocation(contravariant_components.g23, "g23", 0.0); + g12 = getUnalignedAtLocation(old_g12, "g12", 0.0); + g13 = getUnalignedAtLocation(old_g13, "g13", 0.0); + g23 = getUnalignedAtLocation(old_g23, "g23", 0.0); contravariantMetricTensor.setContravariantMetricTensor( location, ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); @@ -499,16 +513,23 @@ Coordinates::Coordinates(Mesh* mesh, Options* options) if (std::all_of(begin(covariant_component_names), end(covariant_component_names), source_has_component)) { + FieldMetric old_g_11 = covariantMetricTensor.Getg_11(); // non-const + FieldMetric old_g_22 = covariantMetricTensor.Getg_22(); // non-const + FieldMetric old_g_33 = covariantMetricTensor.Getg_33(); // non-const + FieldMetric old_g_12 = covariantMetricTensor.Getg_12(); // non-const + FieldMetric old_g_13 = covariantMetricTensor.Getg_13(); // non-const + FieldMetric old_g_23 = covariantMetricTensor.Getg_23(); // non-const + FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; - g_11 = getUnaligned(covariantMetricTensor.Getg_11(), "g_11", 1.0); - g_22 = getUnaligned(covariantMetricTensor.Getg_22(), "g_22", 1.0); - g_33 = getUnaligned(covariantMetricTensor.Getg_33(), "g_33", 1.0); - g_12 = getUnaligned(covariantMetricTensor.Getg_12(), "g_12", 0.0); - g_13 = getUnaligned(covariantMetricTensor.Getg_13(), "g_13", 0.0); - g_23 = getUnaligned(covariantMetricTensor.Getg_23(), "g_23", 0.0); + // g_11 = getUnaligned(old_g_11, "g_11", 1.0); + // g_22 = getUnaligned(old_g_22, "g_22", 1.0); + // g_33 = getUnaligned(old_g_33, "g_33", 1.0); + // g_12 = getUnaligned(old_g_12, "g_12", 0.0); + // g_13 = getUnaligned(old_g_13, "g_13", 0.0); + // g_23 = getUnaligned(old_g_23, "g_23", 0.0); covariantMetricTensor.setCovariantMetricTensor( - location, CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); output_warn.write("\tWARNING! Covariant components of metric tensor set manually. " "Contravariant components NOT recalculated\n"); @@ -550,7 +571,7 @@ Coordinates::Coordinates(Mesh* mesh, Options* options) extrapolate_x, extrapolate_y, false, transform.get()); covariantMetricTensor.setCovariantMetricTensor( - location, CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); // Check covariant metrics checkCovariant(); @@ -688,21 +709,28 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, auto contravariant_components = contravariantMetricTensor.getContravariantMetricTensor(); + FieldMetric old_g11 = contravariant_components.g11; // non-const + FieldMetric old_g22 = contravariant_components.g22; // non-const + FieldMetric old_g33 = contravariant_components.g33; // non-const + FieldMetric old_g12 = contravariant_components.g12; // non-const + FieldMetric old_g13 = contravariant_components.g13; // non-const + FieldMetric old_g23 = contravariant_components.g23; // non-const + // grid data source has staggered fields, so read instead of interpolating // Diagonal components of metric tensor g^{ij} (default to 1) // TODO: Method `getAtLocAndFillGuards` violates command–query separation principle? - getAtLocAndFillGuards(mesh, contravariant_components.g11, "g11", suffix, location, - 1.0, extrapolate_x, extrapolate_y, false, transform.get()); - getAtLocAndFillGuards(mesh, contravariant_components.g22, "g22", suffix, location, - 1.0, extrapolate_x, extrapolate_y, false, transform.get()); - getAtLocAndFillGuards(mesh, contravariant_components.g33, "g33", suffix, location, - 1.0, extrapolate_x, extrapolate_y, false, transform.get()); - getAtLocAndFillGuards(mesh, contravariant_components.g12, "g12", suffix, location, - 0.0, extrapolate_x, extrapolate_y, false, transform.get()); - getAtLocAndFillGuards(mesh, contravariant_components.g13, "g13", suffix, location, - 0.0, extrapolate_x, extrapolate_y, false, transform.get()); - getAtLocAndFillGuards(mesh, contravariant_components.g23, "g23", suffix, location, - 0.0, extrapolate_x, extrapolate_y, false, transform.get()); + getAtLocAndFillGuards(mesh, old_g11, "g11", suffix, location, 1.0, extrapolate_x, + extrapolate_y, false, transform.get()); + getAtLocAndFillGuards(mesh, old_g22, "g22", suffix, location, 1.0, extrapolate_x, + extrapolate_y, false, transform.get()); + getAtLocAndFillGuards(mesh, old_g33, "g33", suffix, location, 1.0, extrapolate_x, + extrapolate_y, false, transform.get()); + getAtLocAndFillGuards(mesh, old_g12, "g12", suffix, location, 0.0, extrapolate_x, + extrapolate_y, false, transform.get()); + getAtLocAndFillGuards(mesh, old_g13, "g13", suffix, location, 0.0, extrapolate_x, + extrapolate_y, false, transform.get()); + getAtLocAndFillGuards(mesh, old_g23, "g23", suffix, location, 0.0, extrapolate_x, + extrapolate_y, false, transform.get()); contravariantMetricTensor.setContravariantMetricTensor( loc, ContravariantMetricTensor( @@ -726,12 +754,19 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, if (std::all_of(begin(covariant_component_names), end(covariant_component_names), source_has_component)) { - getAtLoc(mesh, covariantMetricTensor.Getg_11(), "g_11", suffix, location); - getAtLoc(mesh, covariantMetricTensor.Getg_22(), "g_22", suffix, location); - getAtLoc(mesh, covariantMetricTensor.Getg_33(), "g_33", suffix, location); - getAtLoc(mesh, covariantMetricTensor.Getg_12(), "g_12", suffix, location); - getAtLoc(mesh, covariantMetricTensor.Getg_13(), "g_13", suffix, location); - getAtLoc(mesh, covariantMetricTensor.Getg_23(), "g_23", suffix, location); + FieldMetric old_g_11 = covariantMetricTensor.Getg_11(); // non-const + FieldMetric old_g_22 = covariantMetricTensor.Getg_22(); // non-const + FieldMetric old_g_33 = covariantMetricTensor.Getg_33(); // non-const + FieldMetric old_g_12 = covariantMetricTensor.Getg_12(); // non-const + FieldMetric old_g_13 = covariantMetricTensor.Getg_13(); // non-const + FieldMetric old_g_23 = covariantMetricTensor.Getg_23(); // non-const + + getAtLoc(mesh, old_g_11, "g_11", suffix, location); + getAtLoc(mesh, old_g_22, "g_22", suffix, location); + getAtLoc(mesh, old_g_33, "g_33", suffix, location); + getAtLoc(mesh, old_g_12, "g_12", suffix, location); + getAtLoc(mesh, old_g_13, "g_13", suffix, location); + getAtLoc(mesh, old_g_23, "g_23", suffix, location); output_warn.write( "\tWARNING! Staggered covariant components of metric tensor set manually. " @@ -925,7 +960,7 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, true, false, transform.get()); covariantMetricTensor.setCovariantMetricTensor( - location, CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); // Check input metrics checkContravariant(); @@ -1388,10 +1423,8 @@ CovariantMetricTensor Coordinates::calcCovariant(const std::string& region) { ContravariantMetricTensor Coordinates::calcContravariant(const std::string& region) { TRACE("Coordinates::calcContravariant"); - auto new_contravariantMetricTensor = - covariantMetricTensor.calcContravariant(location, region); - contravariantMetricTensor = new_contravariantMetricTensor; - return new_contravariantMetricTensor; + contravariantMetricTensor.calcContravariant(covariantMetricTensor, location, region); + return contravariantMetricTensor; } int Coordinates::jacobian() { @@ -2051,5 +2084,6 @@ const CovariantMetricTensor::FieldMetric& Coordinates::g_23() const { } void Coordinates::setCovariantMetricTensor(CovariantMetricTensor metric_tensor) { - covariantMetricTensor.setCovariantMetricTensor(location, metric_tensor); + covariantMetricTensor.setCovariantMetricTensor(metric_tensor); + contravariantMetricTensor.calcContravariant(covariantMetricTensor, location); } diff --git a/src/mesh/mesh.cxx b/src/mesh/mesh.cxx index 870f3413cd..146b7603f6 100644 --- a/src/mesh/mesh.cxx +++ b/src/mesh/mesh.cxx @@ -175,6 +175,31 @@ int Mesh::get(Field2D& var, const std::string& name, BoutReal def, bool communic return 0; } +Coordinates::FieldMetric& Mesh::get(const std::string& name, BoutReal def, + bool communicate, CELL_LOC location) { + TRACE("Loading 2D field: Mesh::get(Field2D, {:s})", name); + + if (source == nullptr) { + throw BoutException("source == nullptr, in Coordinates::FieldMetric& Mesh::get"); + } + Field2D temporary_var = Field2D{}; // TODO: There must be a better way of doing this + Field2D& var = temporary_var; + if (!source->get(this, var, name, def, location)) { + throw BoutException( + fmt::format("Exception thrown getting value of `def` from {}", toString(source))); + } + + // Communicate to get guard cell data + if (communicate) { + Mesh::communicate(var); + } + + // Check that the data is valid + checkData(var); + + return var; +} + int Mesh::get(Field3D& var, const std::string& name, BoutReal def, bool communicate, CELL_LOC location) { TRACE("Loading 3D field: Mesh::get(Field3D, {:s})", name); From 9aaa1d640aae51662e6bf6d306f7735d3b89e9e4 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 18 Oct 2023 19:56:57 +0100 Subject: [PATCH 099/491] interpolateAndExtrapolate function has to return a new Field2D object. --- src/mesh/coordinates.cxx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 9ce7532f4c..68d6027ffc 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -39,7 +39,7 @@ void communicate(T& t, Ts... ts) { /// Boundary guard cells are set by extrapolating from the grid, like /// 'free_o3' boundary conditions /// Corner guard cells are set to BoutNaN -const Field2D& interpolateAndExtrapolate(const Field2D& f, CELL_LOC location, +const Field2D interpolateAndExtrapolate(const Field2D& f, CELL_LOC location, bool extrapolate_x, bool extrapolate_y, bool no_extra_interpolate, ParallelTransform* UNUSED(pt) = nullptr) { From b4adc5bd919f3dc3c2e0b1aac6d49eef57885f61 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 18 Oct 2023 19:57:49 +0100 Subject: [PATCH 100/491] Don't throw, just return `def`. --- src/mesh/mesh.cxx | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/mesh/mesh.cxx b/src/mesh/mesh.cxx index 146b7603f6..4e90df4f43 100644 --- a/src/mesh/mesh.cxx +++ b/src/mesh/mesh.cxx @@ -179,14 +179,14 @@ Coordinates::FieldMetric& Mesh::get(const std::string& name, BoutReal def, bool communicate, CELL_LOC location) { TRACE("Loading 2D field: Mesh::get(Field2D, {:s})", name); - if (source == nullptr) { - throw BoutException("source == nullptr, in Coordinates::FieldMetric& Mesh::get"); - } Field2D temporary_var = Field2D{}; // TODO: There must be a better way of doing this Field2D& var = temporary_var; - if (!source->get(this, var, name, def, location)) { - throw BoutException( - fmt::format("Exception thrown getting value of `def` from {}", toString(source))); + + if (source == nullptr or !source->get(this, var, name, def, location)) { + // set val to default in source==nullptr too: + var = def; + var.setLocation(location); + return var; } // Communicate to get guard cell data From 8a65ab59a723adcf921270c7fd8c6e264082e6f9 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 18 Oct 2023 20:03:32 +0100 Subject: [PATCH 101/491] Minor refactoring for readability. --- src/mesh/mesh.cxx | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/mesh/mesh.cxx b/src/mesh/mesh.cxx index 4e90df4f43..7c0ab594f0 100644 --- a/src/mesh/mesh.cxx +++ b/src/mesh/mesh.cxx @@ -182,7 +182,8 @@ Coordinates::FieldMetric& Mesh::get(const std::string& name, BoutReal def, Field2D temporary_var = Field2D{}; // TODO: There must be a better way of doing this Field2D& var = temporary_var; - if (source == nullptr or !source->get(this, var, name, def, location)) { + bool failed_to_get_from_GridDataSource = !source->get(this, var, name, def, location); + if (source == nullptr or failed_to_get_from_GridDataSource) { // set val to default in source==nullptr too: var = def; var.setLocation(location); From 6634a4c2cd35b4eeedcfc05a7920183bbf182db1 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Thu, 19 Oct 2023 11:37:36 +0100 Subject: [PATCH 102/491] Mesh::get method return a new Field2D rather than a reference to one. --- include/bout/mesh.hxx | 7 ++++--- src/mesh/mesh.cxx | 7 +++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/include/bout/mesh.hxx b/include/bout/mesh.hxx index 22a32083e6..e3b447e8fc 100644 --- a/include/bout/mesh.hxx +++ b/include/bout/mesh.hxx @@ -188,16 +188,17 @@ public: bool communicate = true, CELL_LOC location = CELL_DEFAULT); /// Get a Field2D from the input source - /// including communicating guard cells + /// including communicating guard cells. /// This is a new version of the `get` function, that returns the value - /// avoiding the use of an out parameter + /// avoiding the use of an out parameter. + /// Also returns a new Field2D rather than a reference to one /// /// @param[in] name Name of the variable to read /// @param[in] def The default value if not found /// @param[in] communicate Should the field be communicated to fill guard cells? /// /// @returns the value. Will be allocated if needed - Coordinates::FieldMetric& get(const std::string& name, BoutReal def = 0.0, + Coordinates::FieldMetric get(const std::string& name, BoutReal def = 0.0, bool communicate = true, CELL_LOC location = CELL_DEFAULT); /// Get a Field3D from the input source diff --git a/src/mesh/mesh.cxx b/src/mesh/mesh.cxx index 7c0ab594f0..bb7dc1fa74 100644 --- a/src/mesh/mesh.cxx +++ b/src/mesh/mesh.cxx @@ -175,12 +175,11 @@ int Mesh::get(Field2D& var, const std::string& name, BoutReal def, bool communic return 0; } -Coordinates::FieldMetric& Mesh::get(const std::string& name, BoutReal def, - bool communicate, CELL_LOC location) { +Field2D Mesh::get(const std::string& name, BoutReal def, bool communicate, + CELL_LOC location) { TRACE("Loading 2D field: Mesh::get(Field2D, {:s})", name); - Field2D temporary_var = Field2D{}; // TODO: There must be a better way of doing this - Field2D& var = temporary_var; + Field2D var = Field2D{}; bool failed_to_get_from_GridDataSource = !source->get(this, var, name, def, location); if (source == nullptr or failed_to_get_from_GridDataSource) { From 74e48157133dd22f0e55d624e308c8f638c7191e Mon Sep 17 00:00:00 2001 From: tomc271 Date: Thu, 19 Oct 2023 12:13:06 +0100 Subject: [PATCH 103/491] Fixed 'Moved calcContravariant function onto ContravariantMetricTensor class'. --- src/mesh/ContravariantMetricTensor.cpp | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/mesh/ContravariantMetricTensor.cpp b/src/mesh/ContravariantMetricTensor.cpp index 4d3f97d7c7..2764033c62 100644 --- a/src/mesh/ContravariantMetricTensor.cpp +++ b/src/mesh/ContravariantMetricTensor.cpp @@ -212,11 +212,8 @@ void ContravariantMetricTensor::calcContravariant( } } - auto* const mesh = - covariantMetricTensor.Getg_11() - .getMesh(); //TODO: Add a getMesh() method to CovariantComponents? - ContravariantMetricTensor contravariantMetricTensor = ContravariantMetricTensor( - a(0, 0), a(1, 1), a(2, 2), a(0, 1), a(0, 2), a(1, 2), mesh); + contravariant_components = + ContravariantComponents{a(0, 0), a(1, 1), a(2, 2), a(0, 1), a(0, 2), a(1, 2)}; setLocation(location); From 1f42210a5a79c1a80c3af62f12eaa8ba697219f4 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Thu, 19 Oct 2023 13:17:42 +0100 Subject: [PATCH 104/491] Don't need to pass a Field2D reference if returning a new one. --- src/mesh/coordinates.cxx | 52 +++++++++++++--------------------------- 1 file changed, 17 insertions(+), 35 deletions(-) diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 68d6027ffc..b9ad3eae3f 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -40,9 +40,9 @@ void communicate(T& t, Ts... ts) { /// 'free_o3' boundary conditions /// Corner guard cells are set to BoutNaN const Field2D interpolateAndExtrapolate(const Field2D& f, CELL_LOC location, - bool extrapolate_x, bool extrapolate_y, - bool no_extra_interpolate, - ParallelTransform* UNUSED(pt) = nullptr) { + bool extrapolate_x, bool extrapolate_y, + bool no_extra_interpolate, + ParallelTransform* UNUSED(pt) = nullptr) { Mesh* localmesh = f.getMesh(); Field2D result = interp_to(f, location, "RGN_NOBNDRY"); @@ -447,53 +447,35 @@ Coordinates::Coordinates(Mesh* mesh, Options* options) dy = interpolateAndExtrapolate(dy, location, extrapolate_x, extrapolate_y, false, transform.get()); - auto getUnaligned = [this](auto& field, const std::string& name, - BoutReal default_value) { - field = localmesh->get(name, default_value, false); + auto getUnaligned = [this](const std::string& name, BoutReal default_value) { + auto field = localmesh->get(name, default_value, false); if (field.getDirectionY() == YDirectionType::Aligned and transform->canToFromFieldAligned()) { return transform->fromFieldAligned(field); } else { - return field.setDirectionY(YDirectionType::Standard); + field.setDirectionY(YDirectionType::Standard); + return field; } }; - auto getUnalignedAtLocation = [this, extrapolate_x, extrapolate_y, - getUnaligned](auto& field, const std::string& name, - BoutReal default_value) { - field = getUnaligned(field, name, default_value); + auto getUnalignedAtLocation = [this, extrapolate_x, extrapolate_y, getUnaligned]( + const std::string& name, BoutReal default_value) { + auto field = getUnaligned(name, default_value); return interpolateAndExtrapolate(field, location, extrapolate_x, extrapolate_y, false, transform.get()); }; - // Diagonal components of metric tensor g^{ij} (default to 1) - - auto contravariant_components = - contravariantMetricTensor.getContravariantMetricTensor(); - - // FieldMetric old_g11 = contravariantMetricTensor.Getg11(); // non-const - // FieldMetric old_g22 = contravariantMetricTensor.Getg22(); // non-const - // FieldMetric old_g33 = contravariantMetricTensor.Getg33(); // non-const - // FieldMetric old_g12 = contravariantMetricTensor.Getg12(); // non-const - // FieldMetric old_g13 = contravariantMetricTensor.Getg13(); // non-const - // FieldMetric old_g23 = contravariantMetricTensor.Getg23(); // non-const - FieldMetric old_g11 = contravariant_components.g11; // non-const - FieldMetric old_g22 = contravariant_components.g22; // non-const - FieldMetric old_g33 = contravariant_components.g33; // non-const - FieldMetric old_g12 = contravariant_components.g12; // non-const - FieldMetric old_g13 = contravariant_components.g13; // non-const - FieldMetric old_g23 = contravariant_components.g23; // non-const - FieldMetric g11, g22, g33, g12, g13, g23; - g11 = getUnalignedAtLocation(old_g11, "g11", 1.0); - g22 = getUnalignedAtLocation(old_g22, "g22", 1.0); - g33 = getUnalignedAtLocation(old_g33, "g33", 1.0); + // Diagonal components of metric tensor g^{ij} (default to 1) + g11 = getUnalignedAtLocation("g11", 1.0); + g22 = getUnalignedAtLocation("g22", 1.0); + g33 = getUnalignedAtLocation("g33", 1.0); // Off-diagonal elements. Default to 0 - g12 = getUnalignedAtLocation(old_g12, "g12", 0.0); - g13 = getUnalignedAtLocation(old_g13, "g13", 0.0); - g23 = getUnalignedAtLocation(old_g23, "g23", 0.0); + g12 = getUnalignedAtLocation("g12", 0.0); + g13 = getUnalignedAtLocation("g13", 0.0); + g23 = getUnalignedAtLocation("g23", 0.0); contravariantMetricTensor.setContravariantMetricTensor( location, ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); From 9e0419a99c60c9e7980b7b30859d431165ea2ccf Mon Sep 17 00:00:00 2001 From: tomc271 Date: Thu, 19 Oct 2023 13:18:31 +0100 Subject: [PATCH 105/491] Have to initialize Field2D with mesh and location fields if returning a new one. --- src/mesh/mesh.cxx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mesh/mesh.cxx b/src/mesh/mesh.cxx index bb7dc1fa74..55ae8adbff 100644 --- a/src/mesh/mesh.cxx +++ b/src/mesh/mesh.cxx @@ -179,7 +179,7 @@ Field2D Mesh::get(const std::string& name, BoutReal def, bool communicate, CELL_LOC location) { TRACE("Loading 2D field: Mesh::get(Field2D, {:s})", name); - Field2D var = Field2D{}; + Field2D var = Field2D(this, location); bool failed_to_get_from_GridDataSource = !source->get(this, var, name, def, location); if (source == nullptr or failed_to_get_from_GridDataSource) { From 7583e9231bd92c6b60b49ac5c024a106d2ea9c14 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Fri, 20 Oct 2023 15:10:17 +0100 Subject: [PATCH 106/491] Fix Coordinates constructor - update existing contravariant components. --- src/mesh/coordinates.cxx | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index b9ad3eae3f..8b54ef9c9c 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -691,27 +691,20 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, auto contravariant_components = contravariantMetricTensor.getContravariantMetricTensor(); - FieldMetric old_g11 = contravariant_components.g11; // non-const - FieldMetric old_g22 = contravariant_components.g22; // non-const - FieldMetric old_g33 = contravariant_components.g33; // non-const - FieldMetric old_g12 = contravariant_components.g12; // non-const - FieldMetric old_g13 = contravariant_components.g13; // non-const - FieldMetric old_g23 = contravariant_components.g23; // non-const - // grid data source has staggered fields, so read instead of interpolating // Diagonal components of metric tensor g^{ij} (default to 1) // TODO: Method `getAtLocAndFillGuards` violates command–query separation principle? - getAtLocAndFillGuards(mesh, old_g11, "g11", suffix, location, 1.0, extrapolate_x, + getAtLocAndFillGuards(mesh, contravariant_components.g11, "g11", suffix, location, 1.0, extrapolate_x, extrapolate_y, false, transform.get()); - getAtLocAndFillGuards(mesh, old_g22, "g22", suffix, location, 1.0, extrapolate_x, + getAtLocAndFillGuards(mesh, contravariant_components.g22, "g22", suffix, location, 1.0, extrapolate_x, extrapolate_y, false, transform.get()); - getAtLocAndFillGuards(mesh, old_g33, "g33", suffix, location, 1.0, extrapolate_x, + getAtLocAndFillGuards(mesh, contravariant_components.g33, "g33", suffix, location, 1.0, extrapolate_x, extrapolate_y, false, transform.get()); - getAtLocAndFillGuards(mesh, old_g12, "g12", suffix, location, 0.0, extrapolate_x, + getAtLocAndFillGuards(mesh, contravariant_components.g12, "g12", suffix, location, 0.0, extrapolate_x, extrapolate_y, false, transform.get()); - getAtLocAndFillGuards(mesh, old_g13, "g13", suffix, location, 0.0, extrapolate_x, + getAtLocAndFillGuards(mesh, contravariant_components.g13, "g13", suffix, location, 0.0, extrapolate_x, extrapolate_y, false, transform.get()); - getAtLocAndFillGuards(mesh, old_g23, "g23", suffix, location, 0.0, extrapolate_x, + getAtLocAndFillGuards(mesh, contravariant_components.g23, "g23", suffix, location, 0.0, extrapolate_x, extrapolate_y, false, transform.get()); contravariantMetricTensor.setContravariantMetricTensor( From dc1c812ef5d830f6e1af373066debd03c4dc599a Mon Sep 17 00:00:00 2001 From: tomc271 Date: Sun, 22 Oct 2023 19:45:02 +0100 Subject: [PATCH 107/491] Remove usages of getCovariantMetricTensor method from integrated tests. --- .../test_multigrid_laplace.cxx | 12 +++++++----- .../test-naulin-laplace/test_naulin_laplace.cxx | 7 ++++--- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/tests/integrated/test-multigrid_laplace/test_multigrid_laplace.cxx b/tests/integrated/test-multigrid_laplace/test_multigrid_laplace.cxx index 89ba6c0eb4..6bad20c934 100644 --- a/tests/integrated/test-multigrid_laplace/test_multigrid_laplace.cxx +++ b/tests/integrated/test-multigrid_laplace/test_multigrid_laplace.cxx @@ -242,7 +242,7 @@ int main(int argc, char** argv) { x0(mesh->xstart - 1, mesh->ystart, k) = (f4(mesh->xstart, mesh->ystart, k) - f4(mesh->xstart - 1, mesh->ystart, k)) / mesh->getCoordinates()->dx(mesh->xstart, mesh->ystart, k) - / sqrt(mesh->getCoordinates()->getCovariantMetricTensor().g_11(mesh->xstart, mesh->ystart, k)); + / sqrt(mesh->getCoordinates()->g_11()(mesh->xstart, mesh->ystart, k)); } } if (mesh->lastX()) { @@ -250,7 +250,7 @@ int main(int argc, char** argv) { x0(mesh->xend + 1, mesh->ystart, k) = (f4(mesh->xend + 1, mesh->ystart, k) - f4(mesh->xend, mesh->ystart, k)) / mesh->getCoordinates()->dx(mesh->xend, mesh->ystart, k) - / sqrt(mesh->getCoordinates()->getCovariantMetricTensor().g_11(mesh->xend, mesh->ystart, k)); + / sqrt(mesh->getCoordinates()->g_11()(mesh->xend, mesh->ystart, k)); } } @@ -298,9 +298,11 @@ int main(int argc, char** argv) { Field3D this_Grad_perp_dot_Grad_perp(const Field3D& f, const Field3D& g) { auto* mesh = f.getMesh(); - Field3D result = mesh->getCoordinates()->getContravariantMetricTensor().g11 * ::DDX(f) * ::DDX(g) - + mesh->getCoordinates()->getContravariantMetricTensor().g33 * ::DDZ(f) * ::DDZ(g) - + mesh->getCoordinates()->getContravariantMetricTensor().g13 * (DDX(f) * DDZ(g) + DDZ(f) * DDX(g)); + Field3D result = + mesh->getCoordinates()->getContravariantMetricTensor().g11 * ::DDX(f) * ::DDX(g) + + mesh->getCoordinates()->getContravariantMetricTensor().g33 * ::DDZ(f) * ::DDZ(g) + + mesh->getCoordinates()->getContravariantMetricTensor().g13 + * (DDX(f) * DDZ(g) + DDZ(f) * DDX(g)); return result; } diff --git a/tests/integrated/test-naulin-laplace/test_naulin_laplace.cxx b/tests/integrated/test-naulin-laplace/test_naulin_laplace.cxx index 30d001a9e4..24ff91b052 100644 --- a/tests/integrated/test-naulin-laplace/test_naulin_laplace.cxx +++ b/tests/integrated/test-naulin-laplace/test_naulin_laplace.cxx @@ -246,7 +246,7 @@ int main(int argc, char** argv) { x0(mesh->xstart - 1, mesh->ystart, k) = (f4(mesh->xstart, mesh->ystart, k) - f4(mesh->xstart - 1, mesh->ystart, k)) / mesh->getCoordinates()->dx(mesh->xstart, mesh->ystart, k) - / sqrt(mesh->getCoordinates()->getCovariantMetricTensor().g_11(mesh->xstart, mesh->ystart, k)); + / sqrt(mesh->getCoordinates()->g_11()(mesh->xstart, mesh->ystart, k)); } } if (mesh->lastX()) { @@ -254,7 +254,7 @@ int main(int argc, char** argv) { x0(mesh->xend + 1, mesh->ystart, k) = (f4(mesh->xend + 1, mesh->ystart, k) - f4(mesh->xend, mesh->ystart, k)) / mesh->getCoordinates()->dx(mesh->xend, mesh->ystart, k) - / sqrt(mesh->getCoordinates()->getCovariantMetricTensor().g_11(mesh->xend, mesh->ystart, k)); + / sqrt(mesh->getCoordinates()->g_11()(mesh->xend, mesh->ystart, k)); } } @@ -305,7 +305,8 @@ int main(int argc, char** argv) { Field3D this_Grad_perp_dot_Grad_perp(const Field3D& f, const Field3D& g) { const auto* coords = f.getCoordinates(); const auto metric_tensor = coords->getContravariantMetricTensor(); - Field3D result = metric_tensor.g11 * ::DDX(f) * ::DDX(g) + metric_tensor.g33 * ::DDZ(f) * ::DDZ(g) + Field3D result = metric_tensor.g11 * ::DDX(f) * ::DDX(g) + + metric_tensor.g33 * ::DDZ(f) * ::DDZ(g) + metric_tensor.g13 * (DDX(f) * DDZ(g) + DDZ(f) * DDX(g)); return result; From a0f29d556a8c6199142683e88c2f87f14566f389 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Mon, 23 Oct 2023 09:45:47 +0100 Subject: [PATCH 108/491] Call calcCovariant() from Coordinates::setContravariantMetricTensor() rather than in ContravariantMetricTensor::setContravariantMetricTensor(). --- include/bout/ContravariantMetricTensor.hxx | 3 +- src/mesh/ContravariantMetricTensor.cpp | 3 +- src/mesh/coordinates.cxx | 44 +++++++++++----------- 3 files changed, 23 insertions(+), 27 deletions(-) diff --git a/include/bout/ContravariantMetricTensor.hxx b/include/bout/ContravariantMetricTensor.hxx index f9098e7833..6017b5649f 100644 --- a/include/bout/ContravariantMetricTensor.hxx +++ b/include/bout/ContravariantMetricTensor.hxx @@ -37,8 +37,7 @@ public: // check that contravariant tensors are positive (if expected) and finite (always) void checkContravariant(int ystart); - void setContravariantMetricTensor(CELL_LOC location, - const ContravariantMetricTensor& metric_tensor); + void setContravariantMetricTensor(const ContravariantMetricTensor& metric_tensor); ContravariantComponents getContravariantMetricTensor() const; diff --git a/src/mesh/ContravariantMetricTensor.cpp b/src/mesh/ContravariantMetricTensor.cpp index 2764033c62..1c1407ba8c 100644 --- a/src/mesh/ContravariantMetricTensor.cpp +++ b/src/mesh/ContravariantMetricTensor.cpp @@ -32,7 +32,7 @@ ContravariantMetricTensor::getContravariantMetricTensor() const { } void ContravariantMetricTensor::setContravariantMetricTensor( - CELL_LOC location, const ContravariantMetricTensor& metric_tensor) { + const ContravariantMetricTensor& metric_tensor) { const auto new_components = metric_tensor.getContravariantMetricTensor(); contravariant_components.g11 = new_components.g11; @@ -41,7 +41,6 @@ void ContravariantMetricTensor::setContravariantMetricTensor( contravariant_components.g12 = new_components.g12; contravariant_components.g13 = new_components.g13; contravariant_components.g23 = new_components.g23; - calcCovariant(location); } CovariantMetricTensor diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 8b54ef9c9c..2b89f949ab 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -477,8 +477,7 @@ Coordinates::Coordinates(Mesh* mesh, Options* options) g13 = getUnalignedAtLocation("g13", 0.0); g23 = getUnalignedAtLocation("g23", 0.0); - contravariantMetricTensor.setContravariantMetricTensor( - location, ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); + setContravariantMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); // Check input metrics checkContravariant(); @@ -694,24 +693,23 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, // grid data source has staggered fields, so read instead of interpolating // Diagonal components of metric tensor g^{ij} (default to 1) // TODO: Method `getAtLocAndFillGuards` violates command–query separation principle? - getAtLocAndFillGuards(mesh, contravariant_components.g11, "g11", suffix, location, 1.0, extrapolate_x, - extrapolate_y, false, transform.get()); - getAtLocAndFillGuards(mesh, contravariant_components.g22, "g22", suffix, location, 1.0, extrapolate_x, - extrapolate_y, false, transform.get()); - getAtLocAndFillGuards(mesh, contravariant_components.g33, "g33", suffix, location, 1.0, extrapolate_x, - extrapolate_y, false, transform.get()); - getAtLocAndFillGuards(mesh, contravariant_components.g12, "g12", suffix, location, 0.0, extrapolate_x, - extrapolate_y, false, transform.get()); - getAtLocAndFillGuards(mesh, contravariant_components.g13, "g13", suffix, location, 0.0, extrapolate_x, - extrapolate_y, false, transform.get()); - getAtLocAndFillGuards(mesh, contravariant_components.g23, "g23", suffix, location, 0.0, extrapolate_x, - extrapolate_y, false, transform.get()); - - contravariantMetricTensor.setContravariantMetricTensor( - loc, ContravariantMetricTensor( - contravariant_components.g11, contravariant_components.g22, - contravariant_components.g33, contravariant_components.g12, - contravariant_components.g13, contravariant_components.g23)); + getAtLocAndFillGuards(mesh, contravariant_components.g11, "g11", suffix, location, + 1.0, extrapolate_x, extrapolate_y, false, transform.get()); + getAtLocAndFillGuards(mesh, contravariant_components.g22, "g22", suffix, location, + 1.0, extrapolate_x, extrapolate_y, false, transform.get()); + getAtLocAndFillGuards(mesh, contravariant_components.g33, "g33", suffix, location, + 1.0, extrapolate_x, extrapolate_y, false, transform.get()); + getAtLocAndFillGuards(mesh, contravariant_components.g12, "g12", suffix, location, + 0.0, extrapolate_x, extrapolate_y, false, transform.get()); + getAtLocAndFillGuards(mesh, contravariant_components.g13, "g13", suffix, location, + 0.0, extrapolate_x, extrapolate_y, false, transform.get()); + getAtLocAndFillGuards(mesh, contravariant_components.g23, "g23", suffix, location, + 0.0, extrapolate_x, extrapolate_y, false, transform.get()); + + setContravariantMetricTensor(ContravariantMetricTensor( + contravariant_components.g11, contravariant_components.g22, + contravariant_components.g33, contravariant_components.g12, + contravariant_components.g13, contravariant_components.g23)); // Check input metrics checkContravariant(); @@ -912,8 +910,7 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, g23 = interpolateAndExtrapolate(metric_tensor.g23, location, true, true, false, transform.get()); - contravariantMetricTensor.setContravariantMetricTensor( - loc, ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); + setContravariantMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; @@ -2031,7 +2028,8 @@ void Coordinates::checkContravariant() { } void Coordinates::setContravariantMetricTensor(ContravariantMetricTensor metric_tensor) { - contravariantMetricTensor.setContravariantMetricTensor(location, metric_tensor); + contravariantMetricTensor.setContravariantMetricTensor(metric_tensor); + contravariantMetricTensor.calcCovariant(location); } ContravariantMetricTensor::ContravariantComponents From 1d9a1dedbc47fd9ccda6efca1f9f6389deafbc03 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 25 Oct 2023 13:46:08 +0100 Subject: [PATCH 109/491] Coordinates::setContravariantMetricTensor method has to be able to pass 'region' to calcCovariant method. --- include/bout/coordinates.hxx | 3 ++- src/mesh/coordinates.cxx | 8 +++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index a2e2d6f1f2..18dd45176f 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -112,7 +112,8 @@ public: const FieldMetric& g_13() const; const FieldMetric& g_23() const; - void setContravariantMetricTensor(ContravariantMetricTensor metric_tensor); + void setContravariantMetricTensor(ContravariantMetricTensor metric_tensor, + const std::string& region = "RGN_ALL"); void setCovariantMetricTensor(CovariantMetricTensor metric_tensor); diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 2b89f949ab..44b7db85c7 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -910,7 +910,8 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, g23 = interpolateAndExtrapolate(metric_tensor.g23, location, true, true, false, transform.get()); - setContravariantMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); + setContravariantMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23), + "RGN_NOBNDRY"); FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; @@ -2027,9 +2028,10 @@ void Coordinates::checkContravariant() { contravariantMetricTensor.checkContravariant(localmesh->ystart); } -void Coordinates::setContravariantMetricTensor(ContravariantMetricTensor metric_tensor) { +void Coordinates::setContravariantMetricTensor(ContravariantMetricTensor metric_tensor, + const std::string& region) { contravariantMetricTensor.setContravariantMetricTensor(metric_tensor); - contravariantMetricTensor.calcCovariant(location); + contravariantMetricTensor.calcCovariant(location, region); } ContravariantMetricTensor::ContravariantComponents From 42e1a84b1826afb08c7db74bb72b365849a05466 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 25 Oct 2023 14:35:33 +0100 Subject: [PATCH 110/491] Minor refactoring - simplified Coordinates::calcCovariant(). --- src/mesh/coordinates.cxx | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 44b7db85c7..d8705ca225 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -1388,10 +1388,8 @@ void Coordinates::CalculateChristoffelSymbols() { CovariantMetricTensor Coordinates::calcCovariant(const std::string& region) { TRACE("Coordinates::calcCovariant"); - auto new_covariantMetricTensor = - contravariantMetricTensor.calcCovariant(location, region); - covariantMetricTensor = new_covariantMetricTensor; - return new_covariantMetricTensor; + covariantMetricTensor = contravariantMetricTensor.calcCovariant(location, region); + return covariantMetricTensor; } ContravariantMetricTensor Coordinates::calcContravariant(const std::string& region) { From 0afb18daf8a07b850092e51cffac7080ac204363 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 25 Oct 2023 14:54:45 +0100 Subject: [PATCH 111/491] Moved calcCovariant function onto CovariantMetricTensor class. --- include/bout/ContravariantMetricTensor.hxx | 4 -- include/bout/CovariantMetricTensor.hxx | 4 ++ src/mesh/ContravariantMetricTensor.cpp | 68 ---------------------- src/mesh/CovariantMetricTensor.cpp | 65 +++++++++++++++++++++ src/mesh/coordinates.cxx | 4 +- 5 files changed, 71 insertions(+), 74 deletions(-) diff --git a/include/bout/ContravariantMetricTensor.hxx b/include/bout/ContravariantMetricTensor.hxx index 6017b5649f..a56ed195e6 100644 --- a/include/bout/ContravariantMetricTensor.hxx +++ b/include/bout/ContravariantMetricTensor.hxx @@ -30,10 +30,6 @@ public: const BoutReal g12, const BoutReal g13, const BoutReal g23, Mesh* mesh); - /// Invert contravariant metric to get covariant components - CovariantMetricTensor calcCovariant(CELL_LOC location, - const std::string& region = "RGN_ALL"); - // check that contravariant tensors are positive (if expected) and finite (always) void checkContravariant(int ystart); diff --git a/include/bout/CovariantMetricTensor.hxx b/include/bout/CovariantMetricTensor.hxx index 211f6ce3a3..bc1d47e5ff 100644 --- a/include/bout/CovariantMetricTensor.hxx +++ b/include/bout/CovariantMetricTensor.hxx @@ -39,6 +39,10 @@ public: void setLocation(const CELL_LOC location); + /// Invert contravariant metric to get covariant components + void calcCovariant(ContravariantMetricTensor contravariantMetricTensor, + CELL_LOC location, const std::string& region = "RGN_ALL"); + private: FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; }; diff --git a/src/mesh/ContravariantMetricTensor.cpp b/src/mesh/ContravariantMetricTensor.cpp index 1c1407ba8c..2c53da92a4 100644 --- a/src/mesh/ContravariantMetricTensor.cpp +++ b/src/mesh/ContravariantMetricTensor.cpp @@ -43,74 +43,6 @@ void ContravariantMetricTensor::setContravariantMetricTensor( contravariant_components.g23 = new_components.g23; } -CovariantMetricTensor -ContravariantMetricTensor::calcCovariant(const CELL_LOC location, - const std::string& region) { - TRACE("ContravariantMetricTensor::calcCovariant"); - - // Perform inversion of g^{ij} to get g_{ij} - // NOTE: Currently this bit assumes that metric terms are Field2D objects - - auto a = Matrix(3, 3); - - BOUT_FOR_SERIAL(i, contravariant_components.g11.getRegion(region)) { - a(0, 0) = contravariant_components.g11[i]; - a(1, 1) = contravariant_components.g22[i]; - a(2, 2) = contravariant_components.g33[i]; - - a(0, 1) = a(1, 0) = contravariant_components.g12[i]; - a(1, 2) = a(2, 1) = contravariant_components.g23[i]; - a(0, 2) = a(2, 0) = contravariant_components.g13[i]; - - if (invert3x3(a)) { - const auto error_message = "\tERROR: metric tensor is singular at ({:d}, {:d})\n"; - output_error.write(error_message, i.x(), i.y()); - throw BoutException(error_message); - } - } - - auto* const mesh = - contravariant_components.g11 - .getMesh(); //TODO: Add a getMesh() method to ContravariantComponents? - CovariantMetricTensor covariantMetricTensor = - CovariantMetricTensor(a(0, 0), a(1, 1), a(2, 2), a(0, 1), a(0, 2), a(1, 2), mesh); - - setLocation(location); - covariantMetricTensor.setLocation(location); - - BoutReal maxerr; - maxerr = - BOUTMAX(max(abs((covariantMetricTensor.Getg_11() * contravariant_components.g11 - + covariantMetricTensor.Getg_12() * contravariant_components.g12 - + covariantMetricTensor.Getg_13() * contravariant_components.g13) - - 1)), - max(abs((covariantMetricTensor.Getg_12() * contravariant_components.g12 - + covariantMetricTensor.Getg_22() * contravariant_components.g22 - + covariantMetricTensor.Getg_23() * contravariant_components.g23) - - 1)), - max(abs((covariantMetricTensor.Getg_13() * contravariant_components.g13 - + covariantMetricTensor.Getg_23() * contravariant_components.g23 - + covariantMetricTensor.Getg_33() * contravariant_components.g33) - - 1))); - - output_info.write("\tLocal maximum error in diagonal inversion is {:e}\n", maxerr); - - maxerr = - BOUTMAX(max(abs(covariantMetricTensor.Getg_11() * contravariant_components.g12 - + covariantMetricTensor.Getg_12() * contravariant_components.g22 - + covariantMetricTensor.Getg_13() * contravariant_components.g23)), - max(abs(covariantMetricTensor.Getg_11() * contravariant_components.g13 - + covariantMetricTensor.Getg_12() * contravariant_components.g23 - + covariantMetricTensor.Getg_13() * contravariant_components.g33)), - max(abs(covariantMetricTensor.Getg_12() * contravariant_components.g13 - + covariantMetricTensor.Getg_22() * contravariant_components.g23 - + covariantMetricTensor.Getg_23() * contravariant_components.g33))); - - output_info.write("\tLocal maximum error in off-diagonal inversion is {:e}\n", maxerr); - - return covariantMetricTensor; -} - void ContravariantMetricTensor::checkContravariant(int ystart) { // Diagonal metric components should be finite bout::checkFinite(contravariant_components.g11, "g11", "RGN_NOCORNERS"); diff --git a/src/mesh/CovariantMetricTensor.cpp b/src/mesh/CovariantMetricTensor.cpp index 3923fad6ca..97a1134a0d 100644 --- a/src/mesh/CovariantMetricTensor.cpp +++ b/src/mesh/CovariantMetricTensor.cpp @@ -122,3 +122,68 @@ void CovariantMetricTensor::setLocation(const CELL_LOC location) { g_13.setLocation(location); g_23.setLocation(location); } + +void CovariantMetricTensor::calcCovariant( + ContravariantMetricTensor contravariantMetricTensor, const CELL_LOC location, + const std::string& region) { + TRACE("CovariantMetricTensor::calcCovariant"); + + // Perform inversion of g^{ij} to get g_{ij} + // NOTE: Currently this bit assumes that metric terms are Field2D objects + + const auto contravariant_components = + contravariantMetricTensor.getContravariantMetricTensor(); + + auto a = Matrix(3, 3); + + BOUT_FOR_SERIAL(i, contravariant_components.g11.getRegion(region)) { + a(0, 0) = contravariant_components.g11[i]; + a(1, 1) = contravariant_components.g22[i]; + a(2, 2) = contravariant_components.g33[i]; + + a(0, 1) = a(1, 0) = contravariant_components.g12[i]; + a(1, 2) = a(2, 1) = contravariant_components.g23[i]; + a(0, 2) = a(2, 0) = contravariant_components.g13[i]; + + if (invert3x3(a)) { + const auto error_message = "\tERROR: metric tensor is singular at ({:d}, {:d})\n"; + output_error.write(error_message, i.x(), i.y()); + throw BoutException(error_message); + } + } + + g_11 = a(0, 0); + g_22 = a(1, 1); + g_33 = a(2, 2); + g_12 = a(0, 1); + g_13 = a(0, 2); + g_23 = a(1, 2); + // covariant_components = + // CovariantComponents{(a(0, 0), a(1, 1), a(2, 2), a(0, 1), a(0, 2), a(1, 2))}; + + setLocation(location); + + BoutReal maxerr; + maxerr = BOUTMAX( + max(abs((g_11 * contravariant_components.g11 + g_12 * contravariant_components.g12 + + g_13 * contravariant_components.g13) + - 1)), + max(abs((g_12 * contravariant_components.g12 + g_22 * contravariant_components.g22 + + g_23 * contravariant_components.g23) + - 1)), + max(abs((g_13 * contravariant_components.g13 + g_23 * contravariant_components.g23 + + g_33 * contravariant_components.g33) + - 1))); + + output_info.write("\tLocal maximum error in diagonal inversion is {:e}\n", maxerr); + + maxerr = BOUTMAX( + max(abs(g_11 * contravariant_components.g12 + g_12 * contravariant_components.g22 + + g_13 * contravariant_components.g23)), + max(abs(g_11 * contravariant_components.g13 + g_12 * contravariant_components.g23 + + g_13 * contravariant_components.g33)), + max(abs(g_12 * contravariant_components.g13 + g_22 * contravariant_components.g23 + + g_23 * contravariant_components.g33))); + + output_info.write("\tLocal maximum error in off-diagonal inversion is {:e}\n", maxerr); +} diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index d8705ca225..3280c41d06 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -1388,7 +1388,7 @@ void Coordinates::CalculateChristoffelSymbols() { CovariantMetricTensor Coordinates::calcCovariant(const std::string& region) { TRACE("Coordinates::calcCovariant"); - covariantMetricTensor = contravariantMetricTensor.calcCovariant(location, region); + covariantMetricTensor.calcCovariant(contravariantMetricTensor, location, region); return covariantMetricTensor; } @@ -2029,7 +2029,7 @@ void Coordinates::checkContravariant() { void Coordinates::setContravariantMetricTensor(ContravariantMetricTensor metric_tensor, const std::string& region) { contravariantMetricTensor.setContravariantMetricTensor(metric_tensor); - contravariantMetricTensor.calcCovariant(location, region); + covariantMetricTensor.calcCovariant(contravariantMetricTensor, location, region); } ContravariantMetricTensor::ContravariantComponents From 2ba8bbb1cbb00175d3616b45a6259c84af7bc7ac Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 25 Oct 2023 15:56:39 +0100 Subject: [PATCH 112/491] Extract method interpolateAndExtrapolateContravariantMetricTensor. --- include/bout/coordinates.hxx | 1 + src/mesh/coordinates.cxx | 52 ++++++++++++++++++++---------------- 2 files changed, 30 insertions(+), 23 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index 18dd45176f..c3466c085a 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -264,6 +264,7 @@ private: void checkCovariant(); // check that contravariant tensors are positive (if expected) and finite (always) void checkContravariant(); + void interpolateAndExtrapolateContravariantMetricTensor(const Coordinates* coords_in); }; /* diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 3280c41d06..14051a32ad 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -890,31 +890,9 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, dz = interpolateAndExtrapolate(coords_in->dz, location, true, true, false, transform.get()); - ContravariantMetricTensor::ContravariantComponents metric_tensor = - coords_in->getContravariantMetricTensor(); - FieldMetric g11, g22, g33, g12, g13, g23; - - // Diagonal components of metric tensor g^{ij} - g11 = interpolateAndExtrapolate(metric_tensor.g11, location, true, true, false, - transform.get()); - g22 = interpolateAndExtrapolate(metric_tensor.g22, location, true, true, false, - transform.get()); - g33 = interpolateAndExtrapolate(metric_tensor.g33, location, true, true, false, - transform.get()); - - // Off-diagonal elements. - g12 = interpolateAndExtrapolate(metric_tensor.g12, location, true, true, false, - transform.get()); - g13 = interpolateAndExtrapolate(metric_tensor.g13, location, true, true, false, - transform.get()); - g23 = interpolateAndExtrapolate(metric_tensor.g23, location, true, true, false, - transform.get()); - - setContravariantMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23), - "RGN_NOBNDRY"); + interpolateAndExtrapolateContravariantMetricTensor(coords_in); FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; - // 3x3 matrix inversion can exaggerate small interpolation errors, so it is // more robust to interpolate and extrapolate derived quantities directly, // rather than deriving from interpolated/extrapolated covariant metric @@ -959,6 +937,34 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, } } +void Coordinates::interpolateAndExtrapolateContravariantMetricTensor( + const Coordinates* coords_in) { + + ContravariantMetricTensor::ContravariantComponents metric_tensor = + coords_in->getContravariantMetricTensor(); + + FieldMetric g11, g22, g33, g12, g13, g23; + + // Diagonal components of metric tensor g^{ij} + g11 = interpolateAndExtrapolate(metric_tensor.g11, location, true, true, false, + transform.get()); + g22 = interpolateAndExtrapolate(metric_tensor.g22, location, true, true, false, + transform.get()); + g33 = interpolateAndExtrapolate(metric_tensor.g33, location, true, true, false, + transform.get()); + + // Off-diagonal elements. + g12 = interpolateAndExtrapolate(metric_tensor.g12, location, true, true, false, + transform.get()); + g13 = interpolateAndExtrapolate(metric_tensor.g13, location, true, true, false, + transform.get()); + g23 = interpolateAndExtrapolate(metric_tensor.g23, location, true, true, false, + transform.get()); + + setContravariantMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23), + "RGN_NOBNDRY"); +} + void Coordinates::outputVars(Options& output_options) { Timer time("io"); const std::string loc_string = From 00e516daf3948cde6051e114f7083108b55edd9f Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 25 Oct 2023 16:05:22 +0100 Subject: [PATCH 113/491] Extract variable 'region'. --- src/mesh/coordinates.cxx | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 14051a32ad..024682e98f 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -42,10 +42,11 @@ void communicate(T& t, Ts... ts) { const Field2D interpolateAndExtrapolate(const Field2D& f, CELL_LOC location, bool extrapolate_x, bool extrapolate_y, bool no_extra_interpolate, - ParallelTransform* UNUSED(pt) = nullptr) { + ParallelTransform* UNUSED(pt) = nullptr, + const std::string& region = "RGN_NOBNDRY") { Mesh* localmesh = f.getMesh(); - Field2D result = interp_to(f, location, "RGN_NOBNDRY"); + Field2D result = interp_to(f, location, region); // Ensure result's data is unique. Otherwise result might be a duplicate of // f (if no interpolation is needed, e.g. if interpolation is in the // z-direction); then f would be communicated. Since this function is used @@ -943,26 +944,28 @@ void Coordinates::interpolateAndExtrapolateContravariantMetricTensor( ContravariantMetricTensor::ContravariantComponents metric_tensor = coords_in->getContravariantMetricTensor(); + const auto region = "RGN_NOBNDRY"; + FieldMetric g11, g22, g33, g12, g13, g23; // Diagonal components of metric tensor g^{ij} g11 = interpolateAndExtrapolate(metric_tensor.g11, location, true, true, false, - transform.get()); + transform.get(), region); g22 = interpolateAndExtrapolate(metric_tensor.g22, location, true, true, false, - transform.get()); + transform.get(), region); g33 = interpolateAndExtrapolate(metric_tensor.g33, location, true, true, false, - transform.get()); + transform.get(), region); // Off-diagonal elements. g12 = interpolateAndExtrapolate(metric_tensor.g12, location, true, true, false, - transform.get()); + transform.get(), region); g13 = interpolateAndExtrapolate(metric_tensor.g13, location, true, true, false, - transform.get()); + transform.get(), region); g23 = interpolateAndExtrapolate(metric_tensor.g23, location, true, true, false, - transform.get()); + transform.get(), region); setContravariantMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23), - "RGN_NOBNDRY"); + region); } void Coordinates::outputVars(Options& output_options) { From 2b317f1c588f8c3ec27ce463de51809a1786eab8 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 25 Oct 2023 18:30:52 +0100 Subject: [PATCH 114/491] Pass covariant metric tensor components by reference. --- include/bout/CovariantMetricTensor.hxx | 6 +++--- src/mesh/CovariantMetricTensor.cpp | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/include/bout/CovariantMetricTensor.hxx b/include/bout/CovariantMetricTensor.hxx index bc1d47e5ff..d80e22a398 100644 --- a/include/bout/CovariantMetricTensor.hxx +++ b/include/bout/CovariantMetricTensor.hxx @@ -15,9 +15,9 @@ public: using FieldMetric = Field2D; #endif - CovariantMetricTensor(const FieldMetric g_11, const FieldMetric g_22, - const FieldMetric g_33, const FieldMetric g_12, - const FieldMetric g_13, const FieldMetric g_23); + CovariantMetricTensor(const FieldMetric& g_11, const FieldMetric& g_22, + const FieldMetric& g_33, const FieldMetric& g_12, + const FieldMetric& g_13, const FieldMetric& g_23); CovariantMetricTensor(const BoutReal g_11, const BoutReal g_22, const BoutReal g_33, const BoutReal g_12, const BoutReal g_13, const BoutReal g_23, diff --git a/src/mesh/CovariantMetricTensor.cpp b/src/mesh/CovariantMetricTensor.cpp index 97a1134a0d..44905431c2 100644 --- a/src/mesh/CovariantMetricTensor.cpp +++ b/src/mesh/CovariantMetricTensor.cpp @@ -3,10 +3,10 @@ #include "bout/ContravariantMetricTensor.hxx" #include "bout/coordinates.hxx" #include "bout/output.hxx" - + CovariantMetricTensor::CovariantMetricTensor( - const FieldMetric g_11, const FieldMetric g_22, const FieldMetric g_33, - const FieldMetric g_12, const FieldMetric g_13, const FieldMetric g_23) + const FieldMetric& g_11, const FieldMetric& g_22, const FieldMetric& g_33, + const FieldMetric& g_12, const FieldMetric& g_13, const FieldMetric& g_23) : g_11(std::move(g_11)), g_22(std::move(g_22)), g_33(std::move(g_33)), g_12(std::move(g_12)), g_13(std::move(g_13)), g_23(std::move(g_23)) { From 0e946061ff7151020b4095fda28b07285dd51d4d Mon Sep 17 00:00:00 2001 From: tomc271 Date: Thu, 26 Oct 2023 09:19:01 +0100 Subject: [PATCH 115/491] Remove ContravariantComponents struct - individual getters for metric tensor components. [unfinished] --- include/bout/ContravariantMetricTensor.hxx | 15 +- include/bout/coordinates.hxx | 8 +- src/field/vector2d.cxx | 44 +- src/field/vector3d.cxx | 44 +- .../impls/multigrid/multigrid_laplace.cxx | 19 +- .../laplace/impls/naulin/naulin_laplace.cxx | 5 +- .../laplace/impls/serial_band/serial_band.cxx | 73 ++-- src/invert/laplace/invert_laplace.cxx | 27 +- .../impls/cyclic/laplacexz-cyclic.cxx | 9 +- src/mesh/ContravariantMetricTensor.cpp | 169 ++++---- src/mesh/CovariantMetricTensor.cpp | 60 +-- src/mesh/boundary_standard.cxx | 85 ++-- src/mesh/coordinates.cxx | 378 +++++++++--------- src/mesh/coordinates_accessor.cxx | 19 +- src/mesh/fv_ops.cxx | 30 +- 15 files changed, 486 insertions(+), 499 deletions(-) diff --git a/include/bout/ContravariantMetricTensor.hxx b/include/bout/ContravariantMetricTensor.hxx index a56ed195e6..eeef9621f2 100644 --- a/include/bout/ContravariantMetricTensor.hxx +++ b/include/bout/ContravariantMetricTensor.hxx @@ -18,10 +18,6 @@ public: using FieldMetric = Field2D; #endif - struct ContravariantComponents { - FieldMetric g11, g22, g33, g12, g13, g23; - }; - ContravariantMetricTensor(const FieldMetric& g11, const FieldMetric& g22, const FieldMetric& g33, const FieldMetric& g12, const FieldMetric& g13, const FieldMetric& g23); @@ -33,9 +29,14 @@ public: // check that contravariant tensors are positive (if expected) and finite (always) void checkContravariant(int ystart); - void setContravariantMetricTensor(const ContravariantMetricTensor& metric_tensor); + const FieldMetric& Getg11() const; + const FieldMetric& Getg22() const; + const FieldMetric& Getg33() const; + const FieldMetric& Getg12() const; + const FieldMetric& Getg13() const; + const FieldMetric& Getg23() const; - ContravariantComponents getContravariantMetricTensor() const; + void setContravariantMetricTensor(const ContravariantMetricTensor& metric_tensor); void Allocate(); @@ -46,7 +47,7 @@ public: const std::string& region = "RGN_ALL"); private: - ContravariantComponents contravariant_components; + FieldMetric g11, g22, g33, g12, g13, g23; }; #endif //BOUT_CONTRAVARIANTMETRICTENSOR_HXX diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index c3466c085a..776f2b145a 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -103,7 +103,6 @@ private: CovariantMetricTensor covariantMetricTensor; public: - ContravariantMetricTensor::ContravariantComponents getContravariantMetricTensor() const; const FieldMetric& g_11() const; const FieldMetric& g_22() const; @@ -112,6 +111,13 @@ public: const FieldMetric& g_13() const; const FieldMetric& g_23() const; + const FieldMetric& g11() const; + const FieldMetric& g22() const; + const FieldMetric& g33() const; + const FieldMetric& g12() const; + const FieldMetric& g13() const; + const FieldMetric& g23() const; + void setContravariantMetricTensor(ContravariantMetricTensor metric_tensor, const std::string& region = "RGN_ALL"); diff --git a/src/field/vector2d.cxx b/src/field/vector2d.cxx index ae01866798..55658e3111 100644 --- a/src/field/vector2d.cxx +++ b/src/field/vector2d.cxx @@ -83,7 +83,7 @@ void Vector2D::toCovariant() { const auto z_at_y = interp_to(z, y.getLocation()); const auto x_at_z = interp_to(x, z.getLocation()); const auto y_at_z = interp_to(y, z.getLocation()); - + // multiply by g_{ij} BOUT_FOR(i, x.getRegion("RGN_ALL")) { x[i] = metric_x->g_11()[i] * x[i] + metric_x->g_12()[i] * y_at_x[i] @@ -141,15 +141,14 @@ void Vector2D::toContravariant() { const auto x_at_z = interp_to(x, z.getLocation()); const auto y_at_z = interp_to(y, z.getLocation()); - const auto g_x = metric_x->getContravariantMetricTensor(); - const auto g_y = metric_y->getContravariantMetricTensor(); - const auto g_z = metric_z->getContravariantMetricTensor(); - // multiply by g_{ij} BOUT_FOR(i, x.getRegion("RGN_ALL")) { - x[i] = g_x.g11[i] * x[i] + g_x.g12[i] * y_at_x[i] + g_x.g13[i] * z_at_x[i]; - y[i] = g_y.g22[i] * y[i] + g_y.g12[i] * x_at_y[i] + g_y.g23[i] * z_at_y[i]; - z[i] = g_z.g33[i] * z[i] + g_z.g13[i] * x_at_z[i] + g_z.g23[i] * y_at_z[i]; + x[i] = metric_x->g11()[i] * x[i] + metric_x->g12()[i] * y_at_x[i] + + metric_x->g13()[i] * z_at_x[i]; + y[i] = metric_x->g22()[i] * y[i] + metric_x->g12()[i] * x_at_y[i] + + metric_x->g23()[i] * z_at_y[i]; + z[i] = metric_x->g33()[i] * z[i] + metric_x->g13()[i] * x_at_z[i] + + metric_x->g23()[i] * y_at_z[i]; }; } else { @@ -158,18 +157,13 @@ void Vector2D::toContravariant() { // Need to use temporary arrays to store result Coordinates::FieldMetric gx{emptyFrom(x)}, gy{emptyFrom(y)}, gz{emptyFrom(z)}; - const auto contravariant_components = metric->getContravariantMetricTensor(); - BOUT_FOR(i, x.getRegion("RGN_ALL")) { - gx[i] = contravariant_components.g11[i] * x[i] - + contravariant_components.g12[i] * y[i] - + contravariant_components.g13[i] * z[i]; - gy[i] = contravariant_components.g22[i] * y[i] - + contravariant_components.g12[i] * x[i] - + contravariant_components.g23[i] * z[i]; - gz[i] = contravariant_components.g33[i] * z[i] - + contravariant_components.g13[i] * x[i] - + contravariant_components.g23[i] * y[i]; + gx[i] = + metric->g11()[i] * x[i] + metric->g12()[i] * y[i] + metric->g13()[i] * z[i]; + gy[i] = + metric->g22()[i] * y[i] + metric->g12()[i] * x[i] + metric->g23()[i] * z[i]; + gz[i] = + metric->g33()[i] * z[i] + metric->g13()[i] * x[i] + metric->g23()[i] * y[i]; }; x = gx; @@ -403,13 +397,11 @@ const Coordinates::FieldMetric Vector2D::operator*(const Vector2D& rhs) const { if (covariant) { // Both covariant - const auto contravariant_components = metric->getContravariantMetricTensor(); - result = x * rhs.x * contravariant_components.g11 - + y * rhs.y * contravariant_components.g22 - + z * rhs.z * contravariant_components.g33; - result += (x * rhs.y + y * rhs.x) * contravariant_components.g12 - + (x * rhs.z + z * rhs.x) * contravariant_components.g13 - + (y * rhs.z + z * rhs.y) * contravariant_components.g23; + result = x * rhs.x * metric->g11() + y * rhs.y * metric->g22() + + z * rhs.z * metric->g33(); + result += (x * rhs.y + y * rhs.x) * metric->g12() + + (x * rhs.z + z * rhs.x) * metric->g13() + + (y * rhs.z + z * rhs.y) * metric->g23(); } else { // Both contravariant result = x * rhs.x * metric->g_11() + y * rhs.y * metric->g_22() diff --git a/src/field/vector3d.cxx b/src/field/vector3d.cxx index 5ec8a8faae..84b53c2a0a 100644 --- a/src/field/vector3d.cxx +++ b/src/field/vector3d.cxx @@ -142,15 +142,14 @@ void Vector3D::toContravariant() { const auto x_at_z = interp_to(x, z.getLocation()); const auto y_at_z = interp_to(y, z.getLocation()); - const auto g_x = metric_x->getContravariantMetricTensor(); - const auto g_y = metric_y->getContravariantMetricTensor(); - const auto g_z = metric_z->getContravariantMetricTensor(); - // multiply by g_{ij} - BOUT_FOR(i, localmesh->getRegion3D("RGN_ALL")) { - x[i] = g_x.g11[i] * x[i] + g_x.g12[i] * y_at_x[i] + g_x.g13[i] * z_at_x[i]; - y[i] = g_y.g22[i] * y[i] + g_y.g12[i] * x_at_y[i] + g_y.g23[i] * z_at_y[i]; - z[i] = g_z.g33[i] * z[i] + g_z.g13[i] * x_at_z[i] + g_z.g23[i] * y_at_z[i]; + BOUT_FOR(i, x.getRegion("RGN_ALL")) { + x[i] = metric_x->g11()[i] * x[i] + metric_x->g12()[i] * y_at_x[i] + + metric_x->g13()[i] * z_at_x[i]; + y[i] = metric_x->g22()[i] * y[i] + metric_x->g12()[i] * x_at_y[i] + + metric_x->g23()[i] * z_at_y[i]; + z[i] = metric_x->g33()[i] * z[i] + metric_x->g13()[i] * x_at_z[i] + + metric_x->g23()[i] * y_at_z[i]; }; } else { @@ -159,12 +158,13 @@ void Vector3D::toContravariant() { // Need to use temporary arrays to store result Field3D gx{emptyFrom(x)}, gy{emptyFrom(y)}, gz{emptyFrom(z)}; - const auto g = metric->getContravariantMetricTensor(); - BOUT_FOR(i, localmesh->getRegion3D("RGN_ALL")) { - gx[i] = g.g11[i] * x[i] + g.g12[i] * y[i] + g.g13[i] * z[i]; - gy[i] = g.g22[i] * y[i] + g.g12[i] * x[i] + g.g23[i] * z[i]; - gz[i] = g.g33[i] * z[i] + g.g13[i] * x[i] + g.g23[i] * y[i]; + gx[i] = + metric->g11()[i] * x[i] + metric->g12()[i] * y[i] + metric->g13()[i] * z[i]; + gy[i] = + metric->g22()[i] * y[i] + metric->g12()[i] * x[i] + metric->g23()[i] * z[i]; + gz[i] = + metric->g33()[i] * z[i] + metric->g13()[i] * x[i] + metric->g23()[i] * y[i]; }; x = gx; @@ -488,10 +488,11 @@ const Field3D Vector3D::operator*(const Vector3D& rhs) const { if (covariant) { // Both covariant - const auto g = metric->getContravariantMetricTensor(); - result = x * rhs.x * g.g11 + y * rhs.y * g.g22 + z * rhs.z * g.g33; - result += (x * rhs.y + y * rhs.x) * g.g12 + (x * rhs.z + z * rhs.x) * g.g13 - + (y * rhs.z + z * rhs.y) * g.g23; + result = x * rhs.x * metric->g11() + y * rhs.y * metric->g22() + + z * rhs.z * metric->g33(); + result += (x * rhs.y + y * rhs.x) * metric->g12() + + (x * rhs.z + z * rhs.x) * metric->g13() + + (y * rhs.z + z * rhs.y) * metric->g23(); } else { // Both contravariant result = x * rhs.x * metric->g_11() + y * rhs.y * metric->g_22() @@ -519,10 +520,11 @@ const Field3D Vector3D::operator*(const Vector2D& rhs) const { Coordinates* metric = x.getCoordinates(location); if (covariant) { // Both covariant - const auto g = metric->getContravariantMetricTensor(); - result = x * rhs.x * g.g11 + y * rhs.y * g.g22 + z * rhs.z * g.g33; - result += (x * rhs.y + y * rhs.x) * g.g12 + (x * rhs.z + z * rhs.x) * g.g13 - + (y * rhs.z + z * rhs.y) * g.g23; + result = x * rhs.x * metric->g11() + y * rhs.y * metric->g22() + + z * rhs.z * metric->g33(); + result += (x * rhs.y + y * rhs.x) * metric->g12() + + (x * rhs.z + z * rhs.x) * metric->g13() + + (y * rhs.z + z * rhs.y) * metric->g23(); } else { // Both contravariant result = x * rhs.x * metric->g_11() + y * rhs.y * metric->g_22() diff --git a/src/invert/laplace/impls/multigrid/multigrid_laplace.cxx b/src/invert/laplace/impls/multigrid/multigrid_laplace.cxx index 6742b6c9bf..a4a72f3d7e 100644 --- a/src/invert/laplace/impls/multigrid/multigrid_laplace.cxx +++ b/src/invert/laplace/impls/multigrid/multigrid_laplace.cxx @@ -597,31 +597,27 @@ void LaplaceMultigrid::generateMatrixF(int level) { int k2p = (k2 + 1) % Nz_global; int k2m = (k2 + Nz_global - 1) % Nz_global; - const auto contravariant_components = coords->getContravariantMetricTensor(); - BoutReal dz = coords->dz(i2, yindex); BoutReal ddx_C = (C2(i2 + 1, yindex, k2) - C2(i2 - 1, yindex, k2)) / 2. / coords->dx(i2, yindex) / C1(i2, yindex, k2); BoutReal ddz_C = (C2(i2, yindex, k2p) - C2(i2, yindex, k2m)) / 2. / dz / C1(i2, yindex, k2); - BoutReal ddx = D(i2, yindex, k2) * contravariant_components.g11(i2, yindex) + BoutReal ddx = D(i2, yindex, k2) * coords->g11()(i2, yindex) / coords->dx(i2, yindex) / coords->dx(i2, yindex); // coefficient of 2nd derivative stencil (x-direction) - BoutReal ddz = - D(i2, yindex, k2) * contravariant_components.g33(i2, yindex) / SQ(dz); + BoutReal ddz = D(i2, yindex, k2) * coords->g33()(i2, yindex) / SQ(dz); // coefficient of 2nd derivative stencil (z-direction) - BoutReal dxdz = D(i2, yindex, k2) * 2. * contravariant_components.g13(i2, yindex) + BoutReal dxdz = D(i2, yindex, k2) * 2. * coords->g13()(i2, yindex) / coords->dx(i2, yindex) / dz; // coefficient of mixed derivative stencil (could assume zero, at least initially, // if easier; then check this is true in constructor) BoutReal dxd = - (D(i2, yindex, k2) * coords->G1(i2, yindex) - + contravariant_components.g11(i2, yindex) * ddx_C - + contravariant_components.g13(i2, yindex) + (D(i2, yindex, k2) * coords->G1(i2, yindex) + coords->g11()(i2, yindex) * ddx_C + + coords->g13()(i2, yindex) * ddz_C // (could assume zero, at least initially, if easier; then check this is true in constructor) ) / coords->dx(i2, yindex); // coefficient of 1st derivative stencil (x-direction) @@ -631,9 +627,8 @@ void LaplaceMultigrid::generateMatrixF(int level) { } BoutReal dzd = - (D(i2, yindex, k2) * coords->G3(i2, yindex) - + contravariant_components.g33(i2, yindex) * ddz_C - + contravariant_components.g13(i2, yindex) + (D(i2, yindex, k2) * coords->G3(i2, yindex) + coords->g33()(i2, yindex) * ddz_C + + coords->g13()(i2, yindex) * ddx_C // (could assume zero, at least initially, if easier; then check // this is true in constructor) ) diff --git a/src/invert/laplace/impls/naulin/naulin_laplace.cxx b/src/invert/laplace/impls/naulin/naulin_laplace.cxx index dcc7093974..d4f9998f32 100644 --- a/src/invert/laplace/impls/naulin/naulin_laplace.cxx +++ b/src/invert/laplace/impls/naulin/naulin_laplace.cxx @@ -248,10 +248,9 @@ Field3D LaplaceNaulin::solve(const Field3D& rhs, const Field3D& x0) { // Derivatives of x Field3D ddx_x = DDX(x_in, location, "C2"); Field3D ddz_x = DDZ(x_in, location, "FFT"); - const auto g = coords->getContravariantMetricTensor(); return rhsOverD - - (g.g11 * coef_x_AC * ddx_x + g.g33 * coef_z * ddz_x - + g.g13 * (coef_x_AC * ddz_x + coef_z * ddx_x)) + - (coords->g11() * coef_x_AC * ddx_x + coords->g33() * coef_z * ddz_x + + coords->g13() * (coef_x_AC * ddz_x + coef_z * ddx_x)) - AOverD_AC * x_in; }; diff --git a/src/invert/laplace/impls/serial_band/serial_band.cxx b/src/invert/laplace/impls/serial_band/serial_band.cxx index c60c9b671f..cdfe9497e6 100644 --- a/src/invert/laplace/impls/serial_band/serial_band.cxx +++ b/src/invert/laplace/impls/serial_band/serial_band.cxx @@ -158,13 +158,12 @@ FieldPerp LaplaceSerialBand::solve(const FieldPerp& b, const FieldPerp& x0) { A(ix, 4) = 0.; #else // Set coefficients - const auto g = coords->getContravariantMetricTensor(); - coef1 = g.g11(ix, jy); // X 2nd derivative - coef2 = g.g33(ix, jy); // Z 2nd derivative - coef3 = g.g13(ix, jy); // X-Z mixed derivatives - coef4 = 0.0; // X 1st derivative - coef5 = 0.0; // Z 1st derivative - coef6 = Acoef(ix, jy); // Constant + coef1 = coords->g11()(ix, jy); // X 2nd derivative + coef2 = coords->g33()(ix, jy); // Z 2nd derivative + coef3 = coords->g13()(ix, jy); // X-Z mixed derivatives + coef4 = 0.0; // X 1st derivative + coef5 = 0.0; // Z 1st derivative + coef6 = Acoef(ix, jy); // Constant // Multiply Delp2 component by a factor coef1 *= Dcoef(ix, jy); @@ -179,7 +178,7 @@ FieldPerp LaplaceSerialBand::solve(const FieldPerp& b, const FieldPerp& x0) { if (nonuniform) { // non-uniform localmesh correction if ((ix != 0) && (ix != ncx)) { - coef4 += g.g11(ix, jy) + coef4 += coords->g11()(ix, jy) * ((1.0 / coords->dx(ix + 1, jy)) - (1.0 / coords->dx(ix - 1, jy))) / (2.0 * coords->dx(ix, jy)); } @@ -188,7 +187,7 @@ FieldPerp LaplaceSerialBand::solve(const FieldPerp& b, const FieldPerp& x0) { // A first order derivative term (1/c)\nabla_perp c\cdot\nabla_\perp x if ((ix > 1) && (ix < (localmesh->LocalNx - 2))) { - coef4 += g.g11(ix, jy) + coef4 += coords->g11()(ix, jy) * (Ccoef(ix - 2, jy) - 8. * Ccoef(ix - 1, jy) + 8. * Ccoef(ix + 1, jy) - Ccoef(ix + 2, jy)) / (12. * coords->dx(ix, jy) * (Ccoef(ix, jy))); @@ -215,10 +214,9 @@ FieldPerp LaplaceSerialBand::solve(const FieldPerp& b, const FieldPerp& x0) { int ix = 1; auto kwave = kwave_(ix, jy); - const auto g = coords->getContravariantMetricTensor(); - coef1 = g.g11(ix, jy) / (SQ(coords->dx(ix, jy))); - coef2 = g.g33(ix, jy); - coef3 = kwave * g.g13(ix, jy) / (2. * coords->dx(ix, jy)); + coef1 = coords->g11()(ix, jy) / (SQ(coords->dx(ix, jy))); + coef2 = coords->g33()(ix, jy); + coef3 = kwave * coords->g13()(ix, jy) / (2. * coords->dx(ix, jy)); // Multiply Delp2 component by a factor coef1 *= Dcoef(ix, jy); @@ -233,9 +231,9 @@ FieldPerp LaplaceSerialBand::solve(const FieldPerp& b, const FieldPerp& x0) { ix = ncx - 1; - coef1 = g.g11(ix, jy) / (SQ(coords->dx(ix, jy))); - coef2 = g.g33(ix, jy); - coef3 = kwave * g.g13(ix, jy) / (2. * coords->dx(ix, jy)); + coef1 = coords->g11()(ix, jy) / (SQ(coords->dx(ix, jy))); + coef2 = coords->g33()(ix, jy); + coef3 = kwave * coords->g13()(ix, jy) / (2. * coords->dx(ix, jy)); A(ix, 0) = 0.0; A(ix, 1) = dcomplex(coef1, -coef3); @@ -339,27 +337,28 @@ FieldPerp LaplaceSerialBand::solve(const FieldPerp& b, const FieldPerp& x0) { int ix = 1; auto kwave = kwave_(ix, jy); - const auto g = coords->getContravariantMetricTensor(); - coef1 = g.g11(ix, jy) / (12. * SQ(coords->dx(ix, jy))); + coef1 = coords->g11()(ix, jy) / (12. * SQ(coords->dx(ix, jy))); - coef2 = g.g33(ix, jy); + coef2 = coords->g33()(ix, jy); - coef3 = kwave * g.g13(ix, jy) / (2. * coords->dx(ix, jy)); + coef3 = kwave * coords->g13()(ix, jy) / (2. * coords->dx(ix, jy)); coef4 = Acoef(ix, jy); // Combine 4th order at 1 with 2nd order at 0 A(1, 0) = 0.0; // Not used - A(1, 1) = dcomplex( - (14. - SQ(coords->dx(0, jy) * kwave) * g.g33(0, jy) / g.g11(0, jy)) * coef1, - -coef3); + A(1, 1) = dcomplex((14. + - SQ(coords->dx(0, jy) * kwave) * coords->g33()(0, jy) + / coords->g11()(0, jy)) + * coef1, + -coef3); A(1, 2) = dcomplex(-29. * coef1 - SQ(kwave) * coef2 + coef4, 0.0); A(1, 3) = dcomplex(16. * coef1, coef3); A(1, 4) = dcomplex(-coef1, 0.0); - coef1 = g.g11(ix, jy) / (SQ(coords->dx(ix, jy))); - coef2 = g.g33(ix, jy); - coef3 = kwave * g.g13(ix, jy) / (2. * coords->dx(ix, jy)); + coef1 = coords->g11()(ix, jy) / (SQ(coords->dx(ix, jy))); + coef2 = coords->g33()(ix, jy); + coef3 = kwave * coords->g13()(ix, jy) / (2. * coords->dx(ix, jy)); // Use 2nd order at 1 A(0, 0) = 0.0; // Should never be used @@ -382,13 +381,12 @@ FieldPerp LaplaceSerialBand::solve(const FieldPerp& b, const FieldPerp& x0) { int ix = ncx - 1; - const auto g = coords->getContravariantMetricTensor(); - coef1 = g.g11(ix, jy) / (12. * SQ(coords->dx(ix, jy))); + coef1 = coords->g11()(ix, jy) / (12. * SQ(coords->dx(ix, jy))); - coef2 = g.g33(ix, jy); + coef2 = coords->g33()(ix, jy); auto kwave = kwave_(ix, jy); - coef3 = kwave * g.g13(ix, jy) / (2. * coords->dx(ix, jy)); + coef3 = kwave * coords->g13()(ix, jy) / (2. * coords->dx(ix, jy)); coef4 = Acoef(ix, jy); @@ -396,15 +394,16 @@ FieldPerp LaplaceSerialBand::solve(const FieldPerp& b, const FieldPerp& x0) { A(ix, 0) = dcomplex(-coef1, 0.0); A(ix, 1) = dcomplex(16. * coef1, -coef3); A(ix, 2) = dcomplex(-29. * coef1 - SQ(kwave) * coef2 + coef4, 0.0); - A(ix, 3) = dcomplex( - (14. - SQ(coords->dx(ncx, jy) * kwave) * g.g33(ncx, jy) / g.g11(ncx, jy)) - * coef1, - coef3); + A(ix, 3) = dcomplex((14. + - SQ(coords->dx(ncx, jy) * kwave) * coords->g33()(ncx, jy) + / coords->g11()(ncx, jy)) + * coef1, + coef3); A(ix, 4) = 0.0; // Not used - coef1 = g.g11(ix, jy) / (SQ(coords->dx(ix, jy))); - coef2 = g.g33(ix, jy); - coef3 = kwave * g.g13(ix, jy) / (2. * coords->dx(ix, jy)); + coef1 = coords->g11()(ix, jy) / (SQ(coords->dx(ix, jy))); + coef2 = coords->g33()(ix, jy); + coef3 = kwave * coords->g13()(ix, jy) / (2. * coords->dx(ix, jy)); // Use 2nd order at ncx - 1 A(ncx, 0) = dcomplex(coef1, -coef3); diff --git a/src/invert/laplace/invert_laplace.cxx b/src/invert/laplace/invert_laplace.cxx index 2cd87212d1..1e95ff77ad 100644 --- a/src/invert/laplace/invert_laplace.cxx +++ b/src/invert/laplace/invert_laplace.cxx @@ -324,11 +324,9 @@ void Laplacian::tridagCoefs(int jx, int jy, BoutReal kwave, dcomplex& a, dcomple BoutReal coef1, coef2, coef3, coef4, coef5; - const auto contravariant_components = localcoords->getContravariantMetricTensor(); - - coef1 = contravariant_components.g11(jx, jy); ///< X 2nd derivative coefficient - coef2 = contravariant_components.g33(jx, jy); ///< Z 2nd derivative coefficient - coef3 = 2. * contravariant_components.g13(jx, jy); ///< X-Z mixed derivative coefficient + coef1 = localcoords->g11()(jx, jy); ///< X 2nd derivative coefficient + coef2 = localcoords->g33()(jx, jy); ///< Z 2nd derivative coefficient + coef3 = 2. * localcoords->g13()(jx, jy); ///< X-Z mixed derivative coefficient coef4 = 0.0; coef5 = 0.0; @@ -362,14 +360,14 @@ void Laplacian::tridagCoefs(int jx, int jy, BoutReal kwave, dcomplex& a, dcomple if ((jx > 0) && (jx < (localmesh->LocalNx - 1))) { BoutReal dc2dx_over_c1 = ((*c2coef)(jx + 1, jy) - (*c2coef)(jx - 1, jy)) / (2. * localcoords->dx(jx, jy) * ((*c1coef)(jx, jy))); - coef4 += contravariant_components.g11(jx, jy) * dc2dx_over_c1; - coef5 += contravariant_components.g13(jx, jy) * dc2dx_over_c1; + coef4 += localcoords->g11()(jx, jy) * dc2dx_over_c1; + coef5 += localcoords->g13()(jx, jy) * dc2dx_over_c1; } } if (localmesh->IncIntShear) { // d2dz2 term - coef2 += contravariant_components.g11(jx, jy) * localcoords->IntShiftTorsion(jx, jy) + coef2 += localcoords->g11()(jx, jy) * localcoords->IntShiftTorsion(jx, jy) * localcoords->IntShiftTorsion(jx, jy); // Mixed derivative coef3 = 0.0; // This cancels out @@ -494,7 +492,6 @@ void Laplacian::tridagMatrix(dcomplex* avec, dcomplex* bvec, dcomplex* cvec, dco // Set the boundary conditions if x is not periodic if (!localmesh->periodicX) { - const auto contravariant_components = coords->getContravariantMetricTensor(); if (localmesh->firstX()) { // INNER BOUNDARY ON THIS PROCESSOR @@ -550,7 +547,7 @@ void Laplacian::tridagMatrix(dcomplex* avec, dcomplex* bvec, dcomplex* cvec, dco avec[ix] = 0.; bvec[ix] = 1.; cvec[ix] = -exp(-k * coords->dx(ix, jy) - / sqrt(contravariant_components.g11(ix, jy))); + / sqrt(coords->g11()(ix, jy))); } } else if (inner_boundary_flags & INVERT_IN_CYLINDER) { // Condition for inner radial boundary for cylindrical coordinates @@ -629,8 +626,8 @@ void Laplacian::tridagMatrix(dcomplex* avec, dcomplex* bvec, dcomplex* cvec, dco avec[ix] = 0.0; bvec[ix] = 1.0; cvec[ix] = -exp(-1.0 - * sqrt(contravariant_components.g33(ix, jy) - / contravariant_components.g11(ix, jy)) + * sqrt(coords->g33()(ix, jy) + / coords->g11()(ix, jy)) * kwave * coords->dx(ix, jy)); } } else if (inner_boundary_flags & INVERT_IN_CYLINDER) { @@ -714,7 +711,7 @@ void Laplacian::tridagMatrix(dcomplex* avec, dcomplex* bvec, dcomplex* cvec, dco cvec[ncx - ix] = 0.; bvec[ncx - ix] = 1.; avec[ncx - ix] = -exp(-k * coords->dx(ncx - ix, jy) - / sqrt(contravariant_components.g11(ncx - ix, jy))); + / sqrt(coords->g11()(ncx - ix, jy))); } } else { // Order 2 dirichlet BC (boundary half between points) @@ -750,8 +747,8 @@ void Laplacian::tridagMatrix(dcomplex* avec, dcomplex* bvec, dcomplex* cvec, dco // Use decaying zero-Laplacian solution in the boundary for (int ix = 0; ix < outbndry; ix++) { avec[ncx - ix] = -exp(-1.0 - * sqrt(contravariant_components.g33(xe - ix, jy) - / contravariant_components.g11(xe - ix, jy)) + * sqrt(coords->g33()(xe - ix, jy) + / coords->g11()(xe - ix, jy)) * kwave * coords->dx(xe - ix, jy)); bvec[ncx - ix] = 1.0; cvec[ncx - ix] = 0.0; diff --git a/src/invert/laplacexz/impls/cyclic/laplacexz-cyclic.cxx b/src/invert/laplacexz/impls/cyclic/laplacexz-cyclic.cxx index 228a7d3e58..104cc4fb55 100644 --- a/src/invert/laplacexz/impls/cyclic/laplacexz-cyclic.cxx +++ b/src/invert/laplacexz/impls/cyclic/laplacexz-cyclic.cxx @@ -72,10 +72,9 @@ void LaplaceXZcyclic::setCoefs(const Field2D& A2D, const Field2D& B2D) { // Set coefficients Coordinates* coord = localmesh->getCoordinates(location); - const auto g = coord->getContravariantMetricTensor(); // NOTE: For now the X-Z terms are omitted, so check that they are small - ASSERT2(max(abs(g.g13)) < 1e-5); + ASSERT2(max(abs(coord->g13())) < 1e-5); int ind = 0; const BoutReal zlength = getUniform(coord->zlength()); @@ -118,7 +117,7 @@ void LaplaceXZcyclic::setCoefs(const Field2D& A2D, const Field2D& B2D) { // Metrics on x+1/2 boundary BoutReal J = 0.5 * (coord->J(x, y) + coord->J(x + 1, y)); - BoutReal g11 = 0.5 * (g.g11(x, y) + g.g11(x + 1, y)); + BoutReal g11 = 0.5 * (coord->g11()(x, y) + coord->g11()(x + 1, y)); BoutReal dx = 0.5 * (coord->dx(x, y) + coord->dx(x + 1, y)); BoutReal A = 0.5 * (A2D(x, y) + A2D(x + 1, y)); @@ -129,7 +128,7 @@ void LaplaceXZcyclic::setCoefs(const Field2D& A2D, const Field2D& B2D) { // Metrics on x-1/2 boundary J = 0.5 * (coord->J(x, y) + coord->J(x - 1, y)); - g11 = 0.5 * (g.g11(x, y) + g.g11(x - 1, y)); + g11 = 0.5 * (coord->g11()(x, y) + coord->g11()(x - 1, y)); dx = 0.5 * (coord->dx(x, y) + coord->dx(x - 1, y)); A = 0.5 * (A2D(x, y) + A2D(x - 1, y)); @@ -138,7 +137,7 @@ void LaplaceXZcyclic::setCoefs(const Field2D& A2D, const Field2D& B2D) { bcoef(ind, x - xstart) -= val; // ZZ component - bcoef(ind, x - xstart) -= A2D(x, y) * SQ(kwave) * g.g33(x, y); + bcoef(ind, x - xstart) -= A2D(x, y) * SQ(kwave) * coord->g33()(x, y); } // Outer X boundary diff --git a/src/mesh/ContravariantMetricTensor.cpp b/src/mesh/ContravariantMetricTensor.cpp index 2c53da92a4..0108cc6465 100644 --- a/src/mesh/ContravariantMetricTensor.cpp +++ b/src/mesh/ContravariantMetricTensor.cpp @@ -5,10 +5,8 @@ ContravariantMetricTensor::ContravariantMetricTensor( const FieldMetric& g11, const FieldMetric& g22, const FieldMetric& g33, const FieldMetric& g12, const FieldMetric& g13, const FieldMetric& g23) - : contravariant_components({FieldMetric(std::move(g11)), FieldMetric(std::move(g22)), - FieldMetric(std::move(g33)), FieldMetric(std::move(g12)), - FieldMetric(std::move(g13)), - FieldMetric(std::move(g23))}) { + : g11(std::move(g11)), g22(std::move(g22)), g33(std::move(g33)), g12(std::move(g12)), + g13(std::move(g13)), g23(std::move(g23)) { Allocate(); // Make sure metric elements are allocated // ; TODO: Required? } @@ -16,82 +14,88 @@ ContravariantMetricTensor::ContravariantMetricTensor( ContravariantMetricTensor::ContravariantMetricTensor( const BoutReal g11, const BoutReal g22, const BoutReal g33, const BoutReal g12, const BoutReal g13, const BoutReal g23, Mesh* mesh) - : contravariant_components({FieldMetric(g11, mesh), FieldMetric(g22, mesh), - FieldMetric(g33, mesh), FieldMetric(g12, mesh), - FieldMetric(g13, mesh), FieldMetric(g23, mesh)}) { + : g11(g11, mesh), g22(g22, mesh), g33(g33, mesh), g12(g12, mesh), g13(g13, mesh), + g23(g23, mesh) { Allocate(); // Make sure metric elements are allocated // ; TODO: Required? } -ContravariantMetricTensor::ContravariantComponents -ContravariantMetricTensor::getContravariantMetricTensor() const { - return ContravariantComponents{ - contravariant_components.g11, contravariant_components.g22, - contravariant_components.g33, contravariant_components.g12, - contravariant_components.g13, contravariant_components.g23}; +const ContravariantMetricTensor::FieldMetric& ContravariantMetricTensor::Getg11() const { + return g11; +} +const ContravariantMetricTensor::FieldMetric& ContravariantMetricTensor::Getg22() const { + return g22; +} +const ContravariantMetricTensor::FieldMetric& ContravariantMetricTensor::Getg33() const { + return g33; +} +const ContravariantMetricTensor::FieldMetric& ContravariantMetricTensor::Getg12() const { + return g12; +} +const ContravariantMetricTensor::FieldMetric& ContravariantMetricTensor::Getg13() const { + return g13; +} +const ContravariantMetricTensor::FieldMetric& ContravariantMetricTensor::Getg23() const { + return g23; } void ContravariantMetricTensor::setContravariantMetricTensor( const ContravariantMetricTensor& metric_tensor) { - const auto new_components = metric_tensor.getContravariantMetricTensor(); - contravariant_components.g11 = new_components.g11; - contravariant_components.g22 = new_components.g22; - contravariant_components.g33 = new_components.g33; - contravariant_components.g12 = new_components.g12; - contravariant_components.g13 = new_components.g13; - contravariant_components.g23 = new_components.g23; + g11 = metric_tensor.Getg11(); + g22 = metric_tensor.Getg22(); + g33 = metric_tensor.Getg33(); + g12 = metric_tensor.Getg12(); + g13 = metric_tensor.Getg13(); + g23 = metric_tensor.Getg23(); } void ContravariantMetricTensor::checkContravariant(int ystart) { // Diagonal metric components should be finite - bout::checkFinite(contravariant_components.g11, "g11", "RGN_NOCORNERS"); - bout::checkFinite(contravariant_components.g22, "g22", "RGN_NOCORNERS"); - bout::checkFinite(contravariant_components.g33, "g33", "RGN_NOCORNERS"); - if (contravariant_components.g11.hasParallelSlices() - && &contravariant_components.g11.ynext(1) != &contravariant_components.g11) { + bout::checkFinite(g11, "g11", "RGN_NOCORNERS"); + bout::checkFinite(g22, "g22", "RGN_NOCORNERS"); + bout::checkFinite(g33, "g33", "RGN_NOCORNERS"); + if (g11.hasParallelSlices() && &g11.ynext(1) != &g11) { for (int dy = 1; dy <= ystart; ++dy) { for (const auto sign : {1, -1}) { - bout::checkFinite(contravariant_components.g11.ynext(sign * dy), "g11.ynext", + bout::checkFinite(g11.ynext(sign * dy), "g11.ynext", fmt::format("RGN_YPAR_{:+d}", sign * dy)); - bout::checkFinite(contravariant_components.g22.ynext(sign * dy), "g22.ynext", + bout::checkFinite(g22.ynext(sign * dy), "g22.ynext", fmt::format("RGN_YPAR_{:+d}", sign * dy)); - bout::checkFinite(contravariant_components.g33.ynext(sign * dy), "g33.ynext", + bout::checkFinite(g33.ynext(sign * dy), "g33.ynext", fmt::format("RGN_YPAR_{:+d}", sign * dy)); } } } // Diagonal metric components should be positive - bout::checkPositive(contravariant_components.g11, "g11", "RGN_NOCORNERS"); - bout::checkPositive(contravariant_components.g22, "g22", "RGN_NOCORNERS"); - bout::checkPositive(contravariant_components.g33, "g33", "RGN_NOCORNERS"); - if (contravariant_components.g11.hasParallelSlices() - && &contravariant_components.g11.ynext(1) != &contravariant_components.g11) { + bout::checkPositive(g11, "g11", "RGN_NOCORNERS"); + bout::checkPositive(g22, "g22", "RGN_NOCORNERS"); + bout::checkPositive(g33, "g33", "RGN_NOCORNERS"); + if (g11.hasParallelSlices() && &g11.ynext(1) != &g11) { for (int dy = 1; dy <= ystart; ++dy) { for (const auto sign : {1, -1}) { - bout::checkPositive(contravariant_components.g11.ynext(sign * dy), "g11.ynext", + bout::checkPositive(g11.ynext(sign * dy), "g11.ynext", fmt::format("RGN_YPAR_{:+d}", sign * dy)); - bout::checkPositive(contravariant_components.g22.ynext(sign * dy), "g22.ynext", + bout::checkPositive(g22.ynext(sign * dy), "g22.ynext", fmt::format("RGN_YPAR_{:+d}", sign * dy)); - bout::checkPositive(contravariant_components.g33.ynext(sign * dy), "g33.ynext", + bout::checkPositive(g33.ynext(sign * dy), "g33.ynext", fmt::format("RGN_YPAR_{:+d}", sign * dy)); } } } // Off-diagonal metric components should be finite - bout::checkFinite(contravariant_components.g12, "g12", "RGN_NOCORNERS"); - bout::checkFinite(contravariant_components.g13, "g13", "RGN_NOCORNERS"); - bout::checkFinite(contravariant_components.g23, "g23", "RGN_NOCORNERS"); - if (contravariant_components.g23.hasParallelSlices() - && &contravariant_components.g23.ynext(1) != &contravariant_components.g23) { + bout::checkFinite(g12, "g12", "RGN_NOCORNERS"); + bout::checkFinite(g13, "g13", "RGN_NOCORNERS"); + bout::checkFinite(g23, "g23", "RGN_NOCORNERS"); + if (g23.hasParallelSlices() && &g23.ynext(1) != &g23) { for (int dy = 1; dy <= ystart; ++dy) { for (const auto sign : {1, -1}) { - bout::checkFinite(contravariant_components.g12.ynext(sign * dy), "g12.ynext", + bout::checkFinite(g12.ynext(sign * dy), "g12.ynext", fmt::format("RGN_YPAR_{:+d}", sign * dy)); - bout::checkFinite(contravariant_components.g13.ynext(sign * dy), "g13.ynext", + bout::checkFinite(g13.ynext(sign * dy), "g13.ynext", fmt::format("RGN_YPAR_{:+d}", sign * dy)); - bout::checkFinite(contravariant_components.g23.ynext(sign * dy), "g23.ynext", + bout::checkFinite(g23.ynext(sign * dy), "g23.ynext", fmt::format("RGN_YPAR_{:+d}", sign * dy)); } } @@ -99,21 +103,21 @@ void ContravariantMetricTensor::checkContravariant(int ystart) { } void ContravariantMetricTensor::Allocate() { // ; TODO: Required? - contravariant_components.g11.allocate(); - contravariant_components.g22.allocate(); - contravariant_components.g33.allocate(); - contravariant_components.g12.allocate(); - contravariant_components.g13.allocate(); - contravariant_components.g23.allocate(); + g11.allocate(); + g22.allocate(); + g33.allocate(); + g12.allocate(); + g13.allocate(); + g23.allocate(); } void ContravariantMetricTensor::setLocation(const CELL_LOC location) { - contravariant_components.g11.setLocation(location); - contravariant_components.g22.setLocation(location); - contravariant_components.g33.setLocation(location); - contravariant_components.g12.setLocation(location); - contravariant_components.g13.setLocation(location); - contravariant_components.g23.setLocation(location); + g11.setLocation(location); + g22.setLocation(location); + g33.setLocation(location); + g12.setLocation(location); + g13.setLocation(location); + g23.setLocation(location); } void ContravariantMetricTensor::calcContravariant( @@ -143,38 +147,41 @@ void ContravariantMetricTensor::calcContravariant( } } - contravariant_components = - ContravariantComponents{a(0, 0), a(1, 1), a(2, 2), a(0, 1), a(0, 2), a(1, 2)}; + g11 = a(0, 0); + g22 = a(1, 1); + g33 = a(2, 2); + g12 = a(0, 1); + g13 = a(0, 2); + g23 = a(1, 2); + // contravariant_components = ContravariantComponents{a(0, 0), a(1, 1), a(2, 2), a(0, 1), a(0, 2), a(1, 2)}; setLocation(location); BoutReal maxerr; - maxerr = - BOUTMAX(max(abs((covariantMetricTensor.Getg_11() * contravariant_components.g11 - + covariantMetricTensor.Getg_12() * contravariant_components.g12 - + covariantMetricTensor.Getg_13() * contravariant_components.g13) - - 1)), - max(abs((covariantMetricTensor.Getg_12() * contravariant_components.g12 - + covariantMetricTensor.Getg_22() * contravariant_components.g22 - + covariantMetricTensor.Getg_23() * contravariant_components.g23) - - 1)), - max(abs((covariantMetricTensor.Getg_13() * contravariant_components.g13 - + covariantMetricTensor.Getg_23() * contravariant_components.g23 - + covariantMetricTensor.Getg_33() * contravariant_components.g33) - - 1))); + maxerr = BOUTMAX(max(abs((covariantMetricTensor.Getg_11() * g11 + + covariantMetricTensor.Getg_12() * g12 + + covariantMetricTensor.Getg_13() * g13) + - 1)), + max(abs((covariantMetricTensor.Getg_12() * g12 + + covariantMetricTensor.Getg_22() * g22 + + covariantMetricTensor.Getg_23() * g23) + - 1)), + max(abs((covariantMetricTensor.Getg_13() * g13 + + covariantMetricTensor.Getg_23() * g23 + + covariantMetricTensor.Getg_33() * g33) + - 1))); output_info.write("\tMaximum error in diagonal inversion is {:e}\n", maxerr); - maxerr = - BOUTMAX(max(abs(covariantMetricTensor.Getg_11() * contravariant_components.g12 - + covariantMetricTensor.Getg_12() * contravariant_components.g22 - + covariantMetricTensor.Getg_13() * contravariant_components.g23)), - max(abs(covariantMetricTensor.Getg_11() * contravariant_components.g13 - + covariantMetricTensor.Getg_12() * contravariant_components.g23 - + covariantMetricTensor.Getg_13() * contravariant_components.g33)), - max(abs(covariantMetricTensor.Getg_12() * contravariant_components.g13 - + covariantMetricTensor.Getg_22() * contravariant_components.g23 - + covariantMetricTensor.Getg_23() * contravariant_components.g33))); + maxerr = BOUTMAX(max(abs(covariantMetricTensor.Getg_11() * g12 + + covariantMetricTensor.Getg_12() * g22 + + covariantMetricTensor.Getg_13() * g23)), + max(abs(covariantMetricTensor.Getg_11() * g13 + + covariantMetricTensor.Getg_12() * g23 + + covariantMetricTensor.Getg_13() * g33)), + max(abs(covariantMetricTensor.Getg_12() * g13 + + covariantMetricTensor.Getg_22() * g23 + + covariantMetricTensor.Getg_23() * g33))); output_info.write("\tMaximum error in off-diagonal inversion is {:e}\n", maxerr); } diff --git a/src/mesh/CovariantMetricTensor.cpp b/src/mesh/CovariantMetricTensor.cpp index 44905431c2..1ecaa75ef3 100644 --- a/src/mesh/CovariantMetricTensor.cpp +++ b/src/mesh/CovariantMetricTensor.cpp @@ -3,7 +3,7 @@ #include "bout/ContravariantMetricTensor.hxx" #include "bout/coordinates.hxx" #include "bout/output.hxx" - + CovariantMetricTensor::CovariantMetricTensor( const FieldMetric& g_11, const FieldMetric& g_22, const FieldMetric& g_33, const FieldMetric& g_12, const FieldMetric& g_13, const FieldMetric& g_23) @@ -131,19 +131,16 @@ void CovariantMetricTensor::calcCovariant( // Perform inversion of g^{ij} to get g_{ij} // NOTE: Currently this bit assumes that metric terms are Field2D objects - const auto contravariant_components = - contravariantMetricTensor.getContravariantMetricTensor(); - auto a = Matrix(3, 3); - BOUT_FOR_SERIAL(i, contravariant_components.g11.getRegion(region)) { - a(0, 0) = contravariant_components.g11[i]; - a(1, 1) = contravariant_components.g22[i]; - a(2, 2) = contravariant_components.g33[i]; + BOUT_FOR_SERIAL(i, contravariantMetricTensor.Getg11().getRegion(region)) { + a(0, 0) = contravariantMetricTensor.Getg11()[i]; + a(1, 1) = contravariantMetricTensor.Getg22()[i]; + a(2, 2) = contravariantMetricTensor.Getg33()[i]; - a(0, 1) = a(1, 0) = contravariant_components.g12[i]; - a(1, 2) = a(2, 1) = contravariant_components.g23[i]; - a(0, 2) = a(2, 0) = contravariant_components.g13[i]; + a(0, 1) = a(1, 0) = contravariantMetricTensor.Getg12()[i]; + a(1, 2) = a(2, 1) = contravariantMetricTensor.Getg23()[i]; + a(0, 2) = a(2, 0) = contravariantMetricTensor.Getg13()[i]; if (invert3x3(a)) { const auto error_message = "\tERROR: metric tensor is singular at ({:d}, {:d})\n"; @@ -158,32 +155,35 @@ void CovariantMetricTensor::calcCovariant( g_12 = a(0, 1); g_13 = a(0, 2); g_23 = a(1, 2); - // covariant_components = - // CovariantComponents{(a(0, 0), a(1, 1), a(2, 2), a(0, 1), a(0, 2), a(1, 2))}; + // covariant_components = CovariantComponents{(a(0, 0), a(1, 1), a(2, 2), a(0, 1), a(0, 2), a(1, 2))}; setLocation(location); BoutReal maxerr; - maxerr = BOUTMAX( - max(abs((g_11 * contravariant_components.g11 + g_12 * contravariant_components.g12 - + g_13 * contravariant_components.g13) - - 1)), - max(abs((g_12 * contravariant_components.g12 + g_22 * contravariant_components.g22 - + g_23 * contravariant_components.g23) - - 1)), - max(abs((g_13 * contravariant_components.g13 + g_23 * contravariant_components.g23 - + g_33 * contravariant_components.g33) - - 1))); + maxerr = BOUTMAX(max(abs((g_11 * contravariantMetricTensor.Getg11() + + g_12 * contravariantMetricTensor.Getg12() + + g_13 * contravariantMetricTensor.Getg13()) + - 1)), + max(abs((g_12 * contravariantMetricTensor.Getg12() + + g_22 * contravariantMetricTensor.Getg22() + + g_23 * contravariantMetricTensor.Getg23()) + - 1)), + max(abs((g_13 * contravariantMetricTensor.Getg13() + + g_23 * contravariantMetricTensor.Getg23() + + g_33 * contravariantMetricTensor.Getg33()) + - 1))); output_info.write("\tLocal maximum error in diagonal inversion is {:e}\n", maxerr); - maxerr = BOUTMAX( - max(abs(g_11 * contravariant_components.g12 + g_12 * contravariant_components.g22 - + g_13 * contravariant_components.g23)), - max(abs(g_11 * contravariant_components.g13 + g_12 * contravariant_components.g23 - + g_13 * contravariant_components.g33)), - max(abs(g_12 * contravariant_components.g13 + g_22 * contravariant_components.g23 - + g_23 * contravariant_components.g33))); + maxerr = BOUTMAX(max(abs(g_11 * contravariantMetricTensor.Getg12() + + g_12 * contravariantMetricTensor.Getg22() + + g_13 * contravariantMetricTensor.Getg23())), + max(abs(g_11 * contravariantMetricTensor.Getg13() + + g_12 * contravariantMetricTensor.Getg23() + + g_13 * contravariantMetricTensor.Getg33())), + max(abs(g_12 * contravariantMetricTensor.Getg13() + + g_22 * contravariantMetricTensor.Getg23() + + g_23 * contravariantMetricTensor.Getg33()))); output_info.write("\tLocal maximum error in off-diagonal inversion is {:e}\n", maxerr); } diff --git a/src/mesh/boundary_standard.cxx b/src/mesh/boundary_standard.cxx index bcdc730aea..313f125ef5 100644 --- a/src/mesh/boundary_standard.cxx +++ b/src/mesh/boundary_standard.cxx @@ -1602,18 +1602,15 @@ void BoundaryNeumann_NonOrthogonal::apply(Field2D& f) { // Calculate derivatives for metric use mesh->communicate(f); Field2D dfdy = DDY(f); - const auto contravariant_components = metric->getContravariantMetricTensor(); // Loop over all elements and set equal to the next point in for (bndry->first(); !bndry->isDone(); bndry->next1d()) { // Interpolate (linearly) metrics to halfway between last cell and boundary cell - BoutReal g11shift = - 0.5 - * (contravariant_components.g11(bndry->x, bndry->y) - + contravariant_components.g11(bndry->x - bndry->bx, bndry->y)); - BoutReal g12shift = - 0.5 - * (contravariant_components.g12(bndry->x, bndry->y) - + contravariant_components.g12(bndry->x - bndry->bx, bndry->y)); + BoutReal g11shift = 0.5 + * (metric->g11()(bndry->x, bndry->y) + + metric->g11()(bndry->x - bndry->bx, bndry->y)); + BoutReal g12shift = 0.5 + * (metric->g12()(bndry->x, bndry->y) + + metric->g12()(bndry->x - bndry->bx, bndry->y)); // Have to use derivatives at last gridpoint instead of derivatives on boundary layer // because derivative values don't exist in boundary region // NOTE: should be fixed to interpolate to boundary line @@ -1660,7 +1657,6 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { mesh->communicate(f); Field3D dfdy = DDY(f); Field3D dfdz = DDZ(f); - const auto g = metric->getContravariantMetricTensor(); // Loop over all elements and set equal to the next point in for (bndry->first(); !bndry->isDone(); bndry->next1d()) { #if BOUT_USE_METRIC_3D @@ -1669,12 +1665,15 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { int z = 0; #endif // Interpolate (linearly) metrics to halfway between last cell and boundary cell - BoutReal g11shift = - 0.5 * (g.g11(bndry->x, bndry->y, z) + g.g11(bndry->x - bndry->bx, bndry->y, z)); - BoutReal g12shift = - 0.5 * (g.g12(bndry->x, bndry->y, z) + g.g12(bndry->x - bndry->bx, bndry->y, z)); - BoutReal g13shift = - 0.5 * (g.g13(bndry->x, bndry->y, z) + g.g13(bndry->x - bndry->bx, bndry->y, z)); + BoutReal g11shift = 0.5 + * (metric->g11()(bndry->x, bndry->y, z) + + metric->g11()(bndry->x - bndry->bx, bndry->y, z)); + BoutReal g12shift = 0.5 + * (metric->g12()(bndry->x, bndry->y, z) + + metric->g12()(bndry->x - bndry->bx, bndry->y, z)); + BoutReal g13shift = 0.5 + * (metric->g13()(bndry->x, bndry->y, z) + + metric->g13()(bndry->x - bndry->bx, bndry->y, z)); // Have to use derivatives at last gridpoint instead of derivatives on boundary // layer // because derivative values don't exist in boundary region @@ -2644,7 +2643,6 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { } int bx = bndry->bx; - const auto g = metric->getContravariantMetricTensor(); // Loop over the Y dimension for (bndry->first(); !bndry->isDone(); bndry->nextY()) { // bndry->(x,y) is the first point in the boundary @@ -2667,7 +2665,8 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { c0[0] += c1[0]; // Straight line // kz != 0 solution - BoutReal coef = -1.0 * sqrt(g.g33(x, y) / g.g11(x, y)) * metric->dx(x, y); + BoutReal coef = + -1.0 * sqrt(metric->g33()(x, y) / metric->g11()(x, y)) * metric->dx(x, y); for (int jz = 1; jz <= ncz / 2; jz++) { BoutReal kwave = jz * 2.0 * PI / metric->zlength()(x, y); // wavenumber in [rad^-1] @@ -2850,7 +2849,6 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { Array c0(ncz / 2 + 1), c1(ncz / 2 + 1), c2(ncz / 2 + 1); int bx = bndry->bx; - const auto g = metric->getContravariantMetricTensor(); // Loop over the Y dimension for (bndry->first(); !bndry->isDone(); bndry->nextY()) { int x = bndry->x; @@ -2879,16 +2877,17 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { do { // kz = 0 solution xpos -= metric->dx(x, y); - c2[0] = c0[0] + k0lin * xpos + 0.5 * c1[0] * xpos * xpos / g.g11(x - bx, y); + c2[0] = + c0[0] + k0lin * xpos + 0.5 * c1[0] * xpos * xpos / metric->g11()(x - bx, y); // kz != 0 solution - BoutReal coef = - -1.0 * sqrt(g.g33(x - bx, y) / g.g11(x - bx, y)) * metric->dx(x - bx, y); + BoutReal coef = -1.0 * sqrt(metric->g33()(x - bx, y) / metric->g11()(x - bx, y)) + * metric->dx(x - bx, y); for (int jz = 1; jz <= ncz / 2; jz++) { BoutReal kwave = jz * 2.0 * PI / getUniform(metric->zlength()); // wavenumber in [rad^-1] c0[jz] *= exp(coef * kwave); // The decaying solution only // Add the particular solution - c2[jz] = c0[jz] - c1[jz] / (g.g33(x - bx, y) * kwave * kwave); + c2[jz] = c0[jz] - c1[jz] / (metric->g33()(x - bx, y) * kwave * kwave); } // Reverse FFT irfft(c2.begin(), ncz, f(x, y)); @@ -2941,7 +2940,6 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { } jx = mesh->xend + 1; - const auto contravariant_components = metric->getContravariantMetricTensor(); for (jy = 1; jy < mesh->LocalNy - 1; jy++) { for (jz = 0; jz < ncz; jz++) { jzp = (jz + 1) % ncz; @@ -2975,47 +2973,42 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { // - d/dy( JB^y ) - d/dz( JB^z ) tmp = - -(metric->J(jx, jy) * contravariant_components.g12(jx, jy) * var.y(jx, jy, jz) - + metric->J(jx, jy) * contravariant_components.g13(jx, jy) - * var.z(jx, jy, jz) - - metric->J(jx - 2, jy) * contravariant_components.g12(jx - 2, jy) - * var.y(jx - 2, jy, jz) - + metric->J(jx - 2, jy) * contravariant_components.g13(jx - 2, jy) - * var.z(jx - 2, jy, jz)) + -(metric->J(jx, jy) * metric->g12()(jx, jy) * var.y(jx, jy, jz) + + metric->J(jx, jy) * metric->g13()(jx, jy) * var.z(jx, jy, jz) + - metric->J(jx - 2, jy) * metric->g12()(jx - 2, jy) * var.y(jx - 2, jy, jz) + + metric->J(jx - 2, jy) * metric->g13()(jx - 2, jy) * var.z(jx - 2, jy, jz)) / (metric->dx(jx - 2, jy) + metric->dx(jx - 1, jy)); // First term (d/dx) using vals calculated above - tmp -= (metric->J(jx - 1, jy + 1) * contravariant_components.g12(jx - 1, jy + 1) + tmp -= (metric->J(jx - 1, jy + 1) * metric->g12()(jx - 1, jy + 1) * var.x(jx - 1, jy + 1, jz) - - metric->J(jx - 1, jy - 1) * contravariant_components.g12(jx - 1, jy - 1) + - metric->J(jx - 1, jy - 1) * metric->g12()(jx - 1, jy - 1) * var.x(jx - 1, jy - 1, jz) - + metric->J(jx - 1, jy + 1) * contravariant_components.g22(jx - 1, jy + 1) + + metric->J(jx - 1, jy + 1) * metric->g22()(jx - 1, jy + 1) * var.y(jx - 1, jy + 1, jz) - - metric->J(jx - 1, jy - 1) * contravariant_components.g22(jx - 1, jy - 1) + - metric->J(jx - 1, jy - 1) * metric->g22()(jx - 1, jy - 1) * var.y(jx - 1, jy - 1, jz) - + metric->J(jx - 1, jy + 1) * contravariant_components.g23(jx - 1, jy + 1) + + metric->J(jx - 1, jy + 1) * metric->g23()(jx - 1, jy + 1) * var.z(jx - 1, jy + 1, jz) - - metric->J(jx - 1, jy - 1) * contravariant_components.g23(jx - 1, jy - 1) + - metric->J(jx - 1, jy - 1) * metric->g23()(jx - 1, jy - 1) * var.z(jx - 1, jy - 1, jz)) / (metric->dy(jx - 1, jy - 1) + metric->dy(jx - 1, jy)); // second (d/dy) - tmp -= (metric->J(jx - 1, jy) * contravariant_components.g13(jx - 1, jy) + tmp -= (metric->J(jx - 1, jy) * metric->g13()(jx - 1, jy) * (var.x(jx - 1, jy, jzp) - var.x(jx - 1, jy, jzm)) - + metric->J(jx - 1, jy) * contravariant_components.g23(jx - 1, jy) + + metric->J(jx - 1, jy) * metric->g23()(jx - 1, jy) * (var.y(jx - 1, jy, jzp) - var.y(jx - 1, jy, jzm)) - + metric->J(jx - 1, jy) * contravariant_components.g33(jx - 1, jy) + + metric->J(jx - 1, jy) * metric->g33()(jx - 1, jy) * (var.z(jx - 1, jy, jzp) - var.z(jx - 1, jy, jzm))) / (2. * metric->dz(jx - 1, jy)); var.x(jx, jy, jz) = - (metric->J(jx - 2, jy) * contravariant_components.g11(jx - 2, jy) - * var.x(jx - 2, jy, jz) + (metric->J(jx - 2, jy) * metric->g11()(jx - 2, jy) * var.x(jx - 2, jy, jz) + (metric->dx(jx - 2, jy) + metric->dx(jx - 1, jy)) * tmp) - / metric->J(jx, jy) * contravariant_components.g11(jx, jy); + / metric->J(jx, jy) * metric->g11()(jx, jy); if (mesh->xstart == 2) { var.x(jx + 1, jy, jz) = - (metric->J(jx - 3, jy) * contravariant_components.g11(jx - 3, jy) - * var.x(jx - 3, jy, jz) + (metric->J(jx - 3, jy) * metric->g11()(jx - 3, jy) * var.x(jx - 3, jy, jz) + 4. * metric->dx(jx, jy) * tmp) - / metric->J(jx + 1, jy) * contravariant_components.g11(jx + 1, jy); + / metric->J(jx + 1, jy) * metric->g11()(jx + 1, jy); } } } diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 024682e98f..651bad4f46 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -324,16 +324,15 @@ void checkStaggeredGet(Mesh* mesh, const std::string& name, const std::string& s } // convenience function for repeated code -int getAtLoc(Mesh* mesh, Coordinates::FieldMetric& var, const std::string& name, +int getAtLoc(Mesh* mesh, const Coordinates::FieldMetric& var, const std::string& name, const std::string& suffix, CELL_LOC location, BoutReal default_value = 0.) { checkStaggeredGet(mesh, name, suffix); - int result = mesh->get(var, name + suffix, default_value, false, location); - - return result; + auto field = mesh->get(name + suffix, default_value, false, location); + return 0; } -int getAtLocAndFillGuards(Mesh* mesh, Coordinates::FieldMetric& var, +int getAtLocAndFillGuards(Mesh* mesh, const Coordinates::FieldMetric& var, const std::string& name, const std::string& suffix, CELL_LOC location, BoutReal default_value, bool extrapolate_x, bool extrapolate_y, bool no_extra_interpolate, @@ -688,29 +687,32 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, getAtLocAndFillGuards(mesh, dy, "dy", suffix, location, 1.0, extrapolate_x, extrapolate_y, false, transform.get()); - auto contravariant_components = - contravariantMetricTensor.getContravariantMetricTensor(); - // grid data source has staggered fields, so read instead of interpolating // Diagonal components of metric tensor g^{ij} (default to 1) // TODO: Method `getAtLocAndFillGuards` violates command–query separation principle? - getAtLocAndFillGuards(mesh, contravariant_components.g11, "g11", suffix, location, - 1.0, extrapolate_x, extrapolate_y, false, transform.get()); - getAtLocAndFillGuards(mesh, contravariant_components.g22, "g22", suffix, location, - 1.0, extrapolate_x, extrapolate_y, false, transform.get()); - getAtLocAndFillGuards(mesh, contravariant_components.g33, "g33", suffix, location, - 1.0, extrapolate_x, extrapolate_y, false, transform.get()); - getAtLocAndFillGuards(mesh, contravariant_components.g12, "g12", suffix, location, - 0.0, extrapolate_x, extrapolate_y, false, transform.get()); - getAtLocAndFillGuards(mesh, contravariant_components.g13, "g13", suffix, location, - 0.0, extrapolate_x, extrapolate_y, false, transform.get()); - getAtLocAndFillGuards(mesh, contravariant_components.g23, "g23", suffix, location, - 0.0, extrapolate_x, extrapolate_y, false, transform.get()); + getAtLocAndFillGuards(mesh, contravariantMetricTensor.Getg11(), "g11", suffix, + location, 1.0, extrapolate_x, extrapolate_y, false, + transform.get()); + getAtLocAndFillGuards(mesh, contravariantMetricTensor.Getg22(), "g22", suffix, + location, 1.0, extrapolate_x, extrapolate_y, false, + transform.get()); + getAtLocAndFillGuards(mesh, contravariantMetricTensor.Getg33(), "g33", suffix, + location, 1.0, extrapolate_x, extrapolate_y, false, + transform.get()); + getAtLocAndFillGuards(mesh, contravariantMetricTensor.Getg12(), "g12", suffix, + location, 0.0, extrapolate_x, extrapolate_y, false, + transform.get()); + getAtLocAndFillGuards(mesh, contravariantMetricTensor.Getg13(), "g13", suffix, + location, 0.0, extrapolate_x, extrapolate_y, false, + transform.get()); + getAtLocAndFillGuards(mesh, contravariantMetricTensor.Getg23(), "g23", suffix, + location, 0.0, extrapolate_x, extrapolate_y, false, + transform.get()); setContravariantMetricTensor(ContravariantMetricTensor( - contravariant_components.g11, contravariant_components.g22, - contravariant_components.g33, contravariant_components.g12, - contravariant_components.g13, contravariant_components.g23)); + contravariantMetricTensor.Getg11(), contravariantMetricTensor.Getg22(), + contravariantMetricTensor.Getg33(), contravariantMetricTensor.Getg12(), + contravariantMetricTensor.Getg13(), contravariantMetricTensor.Getg23())); // Check input metrics checkContravariant(); @@ -941,27 +943,24 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, void Coordinates::interpolateAndExtrapolateContravariantMetricTensor( const Coordinates* coords_in) { - ContravariantMetricTensor::ContravariantComponents metric_tensor = - coords_in->getContravariantMetricTensor(); - const auto region = "RGN_NOBNDRY"; FieldMetric g11, g22, g33, g12, g13, g23; // Diagonal components of metric tensor g^{ij} - g11 = interpolateAndExtrapolate(metric_tensor.g11, location, true, true, false, + g11 = interpolateAndExtrapolate(coords_in->g11(), location, true, true, false, transform.get(), region); - g22 = interpolateAndExtrapolate(metric_tensor.g22, location, true, true, false, + g22 = interpolateAndExtrapolate(coords_in->g22(), location, true, true, false, transform.get(), region); - g33 = interpolateAndExtrapolate(metric_tensor.g33, location, true, true, false, + g33 = interpolateAndExtrapolate(coords_in->g33(), location, true, true, false, transform.get(), region); // Off-diagonal elements. - g12 = interpolateAndExtrapolate(metric_tensor.g12, location, true, true, false, + g12 = interpolateAndExtrapolate(coords_in->g12(), location, true, true, false, transform.get(), region); - g13 = interpolateAndExtrapolate(metric_tensor.g13, location, true, true, false, + g13 = interpolateAndExtrapolate(coords_in->g13(), location, true, true, false, transform.get(), region); - g23 = interpolateAndExtrapolate(metric_tensor.g23, location, true, true, false, + g23 = interpolateAndExtrapolate(coords_in->g23(), location, true, true, false, transform.get(), region); setContravariantMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23), @@ -977,15 +976,18 @@ void Coordinates::outputVars(Options& output_options) { output_options["dy" + loc_string].force(dy, "Coordinates"); output_options["dz" + loc_string].force(dz, "Coordinates"); - auto const contravariant_components = - contravariantMetricTensor.getContravariantMetricTensor(); - - output_options["g11" + loc_string].force(contravariant_components.g11, "Coordinates"); - output_options["g22" + loc_string].force(contravariant_components.g22, "Coordinates"); - output_options["g33" + loc_string].force(contravariant_components.g33, "Coordinates"); - output_options["g12" + loc_string].force(contravariant_components.g12, "Coordinates"); - output_options["g13" + loc_string].force(contravariant_components.g13, "Coordinates"); - output_options["g23" + loc_string].force(contravariant_components.g23, "Coordinates"); + output_options["g11" + loc_string].force(contravariantMetricTensor.Getg11(), + "Coordinates"); + output_options["g22" + loc_string].force(contravariantMetricTensor.Getg22(), + "Coordinates"); + output_options["g33" + loc_string].force(contravariantMetricTensor.Getg33(), + "Coordinates"); + output_options["g12" + loc_string].force(contravariantMetricTensor.Getg12(), + "Coordinates"); + output_options["g13" + loc_string].force(contravariantMetricTensor.Getg13(), + "Coordinates"); + output_options["g23" + loc_string].force(contravariantMetricTensor.Getg23(), + "Coordinates"); output_options["g_11" + loc_string].force(covariantMetricTensor.Getg_11(), "Coordinates"); @@ -1029,15 +1031,13 @@ int Coordinates::geometry(bool recalculate_staggered, bool force_interpolate_from_centre) { TRACE("Coordinates::geometry"); - auto contravariant_components = - contravariantMetricTensor.getContravariantMetricTensor(); - - communicate(dx, dy, dz, contravariant_components.g11, contravariant_components.g22, - contravariant_components.g33, contravariant_components.g12, - contravariant_components.g13, contravariant_components.g23, - covariantMetricTensor.Getg_11(), covariantMetricTensor.Getg_22(), - covariantMetricTensor.Getg_33(), covariantMetricTensor.Getg_12(), - covariantMetricTensor.Getg_13(), covariantMetricTensor.Getg_23(), J, Bxy); + communicate(dx, dy, dz, contravariantMetricTensor.Getg11(), + contravariantMetricTensor.Getg22(), contravariantMetricTensor.Getg33(), + contravariantMetricTensor.Getg12(), contravariantMetricTensor.Getg13(), + contravariantMetricTensor.Getg23(), covariantMetricTensor.Getg_11(), + covariantMetricTensor.Getg_22(), covariantMetricTensor.Getg_33(), + covariantMetricTensor.Getg_12(), covariantMetricTensor.Getg_13(), + covariantMetricTensor.Getg_23(), J, Bxy); output_progress.write("Calculating differential geometry terms\n"); @@ -1058,20 +1058,20 @@ int Coordinates::geometry(bool recalculate_staggered, checkCovariant(); CalculateChristoffelSymbols(); - auto tmp = J * contravariant_components.g12; + auto tmp = J * contravariantMetricTensor.Getg12(); communicate(tmp); - G1 = (DDX(J * contravariant_components.g11) + DDY(tmp) - + DDZ(J * contravariant_components.g13)) + G1 = (DDX(J * contravariantMetricTensor.Getg11()) + DDY(tmp) + + DDZ(J * contravariantMetricTensor.Getg13())) / J; - tmp = J * contravariant_components.g22; + tmp = J * contravariantMetricTensor.Getg22(); communicate(tmp); - G2 = (DDX(J * contravariant_components.g12) + DDY(tmp) - + DDZ(J * contravariant_components.g23)) + G2 = (DDX(J * contravariantMetricTensor.Getg12()) + DDY(tmp) + + DDZ(J * contravariantMetricTensor.Getg23())) / J; - tmp = J * contravariant_components.g23; + tmp = J * contravariantMetricTensor.Getg23(); communicate(tmp); - G3 = (DDX(J * contravariant_components.g13) + DDY(tmp) - + DDZ(J * contravariant_components.g33)) + G3 = (DDX(J * contravariantMetricTensor.Getg13()) + DDY(tmp) + + DDZ(J * contravariantMetricTensor.Getg33())) / J; // Communicate christoffel symbol terms @@ -1253,146 +1253,149 @@ void Coordinates::CalculateChristoffelSymbols() { // Note: This calculation is completely general: metric // tensor can be 2D or 3D. For 2D, all DDZ terms are zero - auto const contravariant_components = - contravariantMetricTensor.getContravariantMetricTensor(); - - G1_11 = 0.5 * contravariant_components.g11 * DDX(covariantMetricTensor.Getg_11()) - + contravariant_components.g12 + G1_11 = 0.5 * contravariantMetricTensor.Getg11() * DDX(covariantMetricTensor.Getg_11()) + + contravariantMetricTensor.Getg12() * (DDX(covariantMetricTensor.Getg_12()) - 0.5 * DDY(covariantMetricTensor.Getg_11())) - + contravariant_components.g13 + + contravariantMetricTensor.Getg13() * (DDX(covariantMetricTensor.Getg_13()) - 0.5 * DDZ(covariantMetricTensor.Getg_11())); - G1_22 = contravariant_components.g11 - * (DDY(covariantMetricTensor.Getg_12()) - - 0.5 * DDX(covariantMetricTensor.Getg_22())) - + 0.5 * contravariant_components.g12 * DDY(covariantMetricTensor.Getg_22()) - + contravariant_components.g13 - * (DDY(covariantMetricTensor.Getg_23()) - - 0.5 * DDZ(covariantMetricTensor.Getg_22())); - G1_33 = contravariant_components.g11 - * (DDZ(covariantMetricTensor.Getg_13()) - - 0.5 * DDX(covariantMetricTensor.Getg_33())) - + contravariant_components.g12 - * (DDZ(covariantMetricTensor.Getg_23()) - - 0.5 * DDY(covariantMetricTensor.Getg_33())) - + 0.5 * contravariant_components.g13 * DDZ(covariantMetricTensor.Getg_33()); + G1_22 = + contravariantMetricTensor.Getg11() + * (DDY(covariantMetricTensor.Getg_12()) + - 0.5 * DDX(covariantMetricTensor.Getg_22())) + + 0.5 * contravariantMetricTensor.Getg12() * DDY(covariantMetricTensor.Getg_22()) + + contravariantMetricTensor.Getg13() + * (DDY(covariantMetricTensor.Getg_23()) + - 0.5 * DDZ(covariantMetricTensor.Getg_22())); + G1_33 = + contravariantMetricTensor.Getg11() + * (DDZ(covariantMetricTensor.Getg_13()) + - 0.5 * DDX(covariantMetricTensor.Getg_33())) + + contravariantMetricTensor.Getg12() + * (DDZ(covariantMetricTensor.Getg_23()) + - 0.5 * DDY(covariantMetricTensor.Getg_33())) + + 0.5 * contravariantMetricTensor.Getg13() * DDZ(covariantMetricTensor.Getg_33()); G1_12 = - 0.5 * contravariant_components.g11 * DDY(covariantMetricTensor.Getg_11()) - + 0.5 * contravariant_components.g12 * DDX(covariantMetricTensor.Getg_22()) - + 0.5 * contravariant_components.g13 + 0.5 * contravariantMetricTensor.Getg11() * DDY(covariantMetricTensor.Getg_11()) + + 0.5 * contravariantMetricTensor.Getg12() * DDX(covariantMetricTensor.Getg_22()) + + 0.5 * contravariantMetricTensor.Getg13() * (DDY(covariantMetricTensor.Getg_13()) + DDX(covariantMetricTensor.Getg_23()) - DDZ(covariantMetricTensor.Getg_12())); G1_13 = - 0.5 * contravariant_components.g11 * DDZ(covariantMetricTensor.Getg_11()) - + 0.5 * contravariant_components.g12 + 0.5 * contravariantMetricTensor.Getg11() * DDZ(covariantMetricTensor.Getg_11()) + + 0.5 * contravariantMetricTensor.Getg12() * (DDZ(covariantMetricTensor.Getg_12()) + DDX(covariantMetricTensor.Getg_23()) - DDY(covariantMetricTensor.Getg_13())) - + 0.5 * contravariant_components.g13 * DDX(covariantMetricTensor.Getg_33()); + + 0.5 * contravariantMetricTensor.Getg13() * DDX(covariantMetricTensor.Getg_33()); G1_23 = - 0.5 * contravariant_components.g11 + 0.5 * contravariantMetricTensor.Getg11() * (DDZ(covariantMetricTensor.Getg_12()) + DDY(covariantMetricTensor.Getg_13()) - DDX(covariantMetricTensor.Getg_23())) - + 0.5 * contravariant_components.g12 + + 0.5 * contravariantMetricTensor.Getg12() * (DDZ(covariantMetricTensor.Getg_22()) + DDY(covariantMetricTensor.Getg_23()) - DDY(covariantMetricTensor.Getg_23())) // + 0.5 *g13*(DDZ(g_32) + DDY(g_33) - DDZ(g_23)); // which equals - + 0.5 * contravariant_components.g13 * DDY(covariantMetricTensor.Getg_33()); + + 0.5 * contravariantMetricTensor.Getg13() * DDY(covariantMetricTensor.Getg_33()); - G2_11 = 0.5 * contravariant_components.g12 * DDX(covariantMetricTensor.Getg_11()) - + contravariant_components.g22 + G2_11 = 0.5 * contravariantMetricTensor.Getg12() * DDX(covariantMetricTensor.Getg_11()) + + contravariantMetricTensor.Getg22() * (DDX(covariantMetricTensor.Getg_12()) - 0.5 * DDY(covariantMetricTensor.Getg_11())) - + contravariant_components.g23 + + contravariantMetricTensor.Getg23() * (DDX(covariantMetricTensor.Getg_13()) - 0.5 * DDZ(covariantMetricTensor.Getg_11())); - G2_22 = contravariant_components.g12 - * (DDY(covariantMetricTensor.Getg_12()) - - 0.5 * DDX(covariantMetricTensor.Getg_22())) - + 0.5 * contravariant_components.g22 * DDY(covariantMetricTensor.Getg_22()) - + contravariant_components.g23 - * (DDY(contravariant_components.g23) - - 0.5 * DDZ(covariantMetricTensor.Getg_22())); - G2_33 = contravariant_components.g12 - * (DDZ(covariantMetricTensor.Getg_13()) - - 0.5 * DDX(covariantMetricTensor.Getg_33())) - + contravariant_components.g22 - * (DDZ(covariantMetricTensor.Getg_23()) - - 0.5 * DDY(covariantMetricTensor.Getg_33())) - + 0.5 * contravariant_components.g23 * DDZ(covariantMetricTensor.Getg_33()); + G2_22 = + contravariantMetricTensor.Getg12() + * (DDY(covariantMetricTensor.Getg_12()) + - 0.5 * DDX(covariantMetricTensor.Getg_22())) + + 0.5 * contravariantMetricTensor.Getg22() * DDY(covariantMetricTensor.Getg_22()) + + contravariantMetricTensor.Getg23() + * (DDY(contravariantMetricTensor.Getg23()) + - 0.5 * DDZ(covariantMetricTensor.Getg_22())); + G2_33 = + contravariantMetricTensor.Getg12() + * (DDZ(covariantMetricTensor.Getg_13()) + - 0.5 * DDX(covariantMetricTensor.Getg_33())) + + contravariantMetricTensor.Getg22() + * (DDZ(covariantMetricTensor.Getg_23()) + - 0.5 * DDY(covariantMetricTensor.Getg_33())) + + 0.5 * contravariantMetricTensor.Getg23() * DDZ(covariantMetricTensor.Getg_33()); G2_12 = - 0.5 * contravariant_components.g12 * DDY(covariantMetricTensor.Getg_11()) - + 0.5 * contravariant_components.g22 * DDX(covariantMetricTensor.Getg_22()) - + 0.5 * contravariant_components.g23 + 0.5 * contravariantMetricTensor.Getg12() * DDY(covariantMetricTensor.Getg_11()) + + 0.5 * contravariantMetricTensor.Getg22() * DDX(covariantMetricTensor.Getg_22()) + + 0.5 * contravariantMetricTensor.Getg23() * (DDY(covariantMetricTensor.Getg_13()) + DDX(covariantMetricTensor.Getg_23()) - DDZ(covariantMetricTensor.Getg_12())); G2_13 = // 0.5 *g21*(DDZ(covariantMetricTensor.Getg_11()) + DDX(covariantMetricTensor.Getg_13()) - DDX(covariantMetricTensor.Getg_13())) // which equals - 0.5 * contravariant_components.g12 + 0.5 * contravariantMetricTensor.Getg12() * (DDZ(covariantMetricTensor.Getg_11()) + DDX(covariantMetricTensor.Getg_13()) - DDX(covariantMetricTensor.Getg_13())) // + 0.5 *g22*(DDZ(covariantMetricTensor.Getg_21()) + DDX(covariantMetricTensor.Getg_23()) - DDY(covariantMetricTensor.Getg_13())) // which equals - + 0.5 * contravariant_components.g22 + + 0.5 * contravariantMetricTensor.Getg22() * (DDZ(covariantMetricTensor.Getg_12()) + DDX(covariantMetricTensor.Getg_23()) - DDY(covariantMetricTensor.Getg_13())) // + 0.5 *g23*(DDZ(covariantMetricTensor.Getg_31()) + DDX(covariantMetricTensor.Getg_33()) - DDZ(g_13)); // which equals - + 0.5 * contravariant_components.g23 * DDX(covariantMetricTensor.Getg_33()); + + 0.5 * contravariantMetricTensor.Getg23() * DDX(covariantMetricTensor.Getg_33()); G2_23 = - 0.5 * contravariant_components.g12 + 0.5 * contravariantMetricTensor.Getg12() * (DDZ(covariantMetricTensor.Getg_12()) + DDY(covariantMetricTensor.Getg_13()) - DDX(covariantMetricTensor.Getg_23())) - + 0.5 * contravariant_components.g22 * DDZ(covariantMetricTensor.Getg_22()) - + 0.5 * contravariant_components.g23 * DDY(covariantMetricTensor.Getg_33()); + + 0.5 * contravariantMetricTensor.Getg22() * DDZ(covariantMetricTensor.Getg_22()) + + 0.5 * contravariantMetricTensor.Getg23() * DDY(covariantMetricTensor.Getg_33()); - G3_11 = 0.5 * contravariant_components.g13 * DDX(covariantMetricTensor.Getg_11()) - + contravariant_components.g23 + G3_11 = 0.5 * contravariantMetricTensor.Getg13() * DDX(covariantMetricTensor.Getg_11()) + + contravariantMetricTensor.Getg23() * (DDX(covariantMetricTensor.Getg_12()) - 0.5 * DDY(covariantMetricTensor.Getg_11())) - + contravariant_components.g33 + + contravariantMetricTensor.Getg33() * (DDX(covariantMetricTensor.Getg_13()) - 0.5 * DDZ(covariantMetricTensor.Getg_11())); - G3_22 = contravariant_components.g13 - * (DDY(covariantMetricTensor.Getg_12()) - - 0.5 * DDX(covariantMetricTensor.Getg_22())) - + 0.5 * contravariant_components.g23 * DDY(covariantMetricTensor.Getg_22()) - + contravariant_components.g33 - * (DDY(covariantMetricTensor.Getg_23()) - - 0.5 * DDZ(covariantMetricTensor.Getg_22())); - G3_33 = contravariant_components.g13 - * (DDZ(covariantMetricTensor.Getg_13()) - - 0.5 * DDX(covariantMetricTensor.Getg_33())) - + contravariant_components.g23 - * (DDZ(covariantMetricTensor.Getg_23()) - - 0.5 * DDY(covariantMetricTensor.Getg_33())) - + 0.5 * contravariant_components.g33 * DDZ(covariantMetricTensor.Getg_33()); + G3_22 = + contravariantMetricTensor.Getg13() + * (DDY(covariantMetricTensor.Getg_12()) + - 0.5 * DDX(covariantMetricTensor.Getg_22())) + + 0.5 * contravariantMetricTensor.Getg23() * DDY(covariantMetricTensor.Getg_22()) + + contravariantMetricTensor.Getg33() + * (DDY(covariantMetricTensor.Getg_23()) + - 0.5 * DDZ(covariantMetricTensor.Getg_22())); + G3_33 = + contravariantMetricTensor.Getg13() + * (DDZ(covariantMetricTensor.Getg_13()) + - 0.5 * DDX(covariantMetricTensor.Getg_33())) + + contravariantMetricTensor.Getg23() + * (DDZ(covariantMetricTensor.Getg_23()) + - 0.5 * DDY(covariantMetricTensor.Getg_33())) + + 0.5 * contravariantMetricTensor.Getg33() * DDZ(covariantMetricTensor.Getg_33()); G3_12 = // 0.5 *g31*(DDY(covariantMetricTensor.Getg_11()) + DDX(covariantMetricTensor.Getg_12()) - DDX(covariantMetricTensor.Getg_12())) // which equals to - 0.5 * contravariant_components.g13 * DDY(covariantMetricTensor.Getg_11()) + 0.5 * contravariantMetricTensor.Getg13() * DDY(covariantMetricTensor.Getg_11()) // + 0.5 *g32*(DDY(covariantMetricTensor.Getg_21()) + DDX(covariantMetricTensor.Getg_22()) - DDY(covariantMetricTensor.Getg_12())) // which equals to - + 0.5 * contravariant_components.g23 * DDX(covariantMetricTensor.Getg_22()) + + 0.5 * contravariantMetricTensor.Getg23() * DDX(covariantMetricTensor.Getg_22()) //+ 0.5 *g33*(DDY(covariantMetricTensor.Getg_31()) + DDX(covariantMetricTensor.Getg_32()) - DDZ(covariantMetricTensor.Getg_12())); // which equals to - + 0.5 * contravariant_components.g33 + + 0.5 * contravariantMetricTensor.Getg33() * (DDY(covariantMetricTensor.Getg_13()) + DDX(covariantMetricTensor.Getg_23()) - DDZ(covariantMetricTensor.Getg_12())); G3_13 = - 0.5 * contravariant_components.g13 * DDZ(covariantMetricTensor.Getg_11()) - + 0.5 * contravariant_components.g23 + 0.5 * contravariantMetricTensor.Getg13() * DDZ(covariantMetricTensor.Getg_11()) + + 0.5 * contravariantMetricTensor.Getg23() * (DDZ(covariantMetricTensor.Getg_12()) + DDX(covariantMetricTensor.Getg_23()) - DDY(covariantMetricTensor.Getg_13())) - + 0.5 * contravariant_components.g33 * DDX(covariantMetricTensor.Getg_33()); + + 0.5 * contravariantMetricTensor.Getg33() * DDX(covariantMetricTensor.Getg_33()); G3_23 = - 0.5 * contravariant_components.g13 + 0.5 * contravariantMetricTensor.Getg13() * (DDZ(covariantMetricTensor.Getg_12()) + DDY(covariantMetricTensor.Getg_13()) - DDX(covariantMetricTensor.Getg_23())) - + 0.5 * contravariant_components.g23 * DDZ(covariantMetricTensor.Getg_22()) - + 0.5 * contravariant_components.g33 * DDY(covariantMetricTensor.Getg_33()); + + 0.5 * contravariantMetricTensor.Getg23() * DDZ(covariantMetricTensor.Getg_22()) + + 0.5 * contravariantMetricTensor.Getg33() * DDY(covariantMetricTensor.Getg_33()); } CovariantMetricTensor Coordinates::calcCovariant(const std::string& region) { @@ -1414,18 +1417,16 @@ int Coordinates::jacobian() { const bool extrapolate_x = not localmesh->sourceHasXBoundaryGuards(); const bool extrapolate_y = not localmesh->sourceHasYBoundaryGuards(); - auto const contravariant_components = - contravariantMetricTensor.getContravariantMetricTensor(); - auto g = contravariant_components.g11 * contravariant_components.g22 - * contravariant_components.g33 - + 2.0 * contravariant_components.g12 * contravariant_components.g13 - * contravariant_components.g23 - - contravariant_components.g11 * contravariant_components.g23 - * contravariant_components.g23 - - contravariant_components.g22 * contravariant_components.g13 - * contravariant_components.g13 - - contravariant_components.g33 * contravariant_components.g12 - * contravariant_components.g12; + auto g = contravariantMetricTensor.Getg11() * contravariantMetricTensor.Getg22() + * contravariantMetricTensor.Getg33() + + 2.0 * contravariantMetricTensor.Getg12() * contravariantMetricTensor.Getg13() + * contravariantMetricTensor.Getg23() + - contravariantMetricTensor.Getg11() * contravariantMetricTensor.Getg23() + * contravariantMetricTensor.Getg23() + - contravariantMetricTensor.Getg22() * contravariantMetricTensor.Getg13() + * contravariantMetricTensor.Getg13() + - contravariantMetricTensor.Getg33() * contravariantMetricTensor.Getg12() + * contravariantMetricTensor.Getg12(); // Check that g is positive bout::checkPositive(g, "The determinant of g^ij", "RGN_NOBNDRY"); @@ -1738,9 +1739,8 @@ Coordinates::FieldMetric Coordinates::Delp2(const Field2D& f, CELL_LOC outloc, TRACE("Coordinates::Delp2( Field2D )"); ASSERT1(location == outloc || outloc == CELL_DEFAULT); - auto const contravariant_components = - contravariantMetricTensor.getContravariantMetricTensor(); - auto result = G1 * DDX(f, outloc) + contravariant_components.g11 * D2DX2(f, outloc); + auto result = + G1 * DDX(f, outloc) + contravariantMetricTensor.Getg11() * D2DX2(f, outloc); return result; } @@ -1802,12 +1802,10 @@ Field3D Coordinates::Delp2(const Field3D& f, CELL_LOC outloc, bool useFFT) { } } } else { - auto const contravariant_components = - contravariantMetricTensor.getContravariantMetricTensor(); result = G1 * ::DDX(f, outloc) + G3 * ::DDZ(f, outloc) - + contravariant_components.g11 * ::D2DX2(f, outloc) - + contravariant_components.g33 * ::D2DZ2(f, outloc) - + 2 * contravariant_components.g13 * ::D2DXDZ(f, outloc); + + contravariantMetricTensor.Getg11() * ::D2DX2(f, outloc) + + contravariantMetricTensor.Getg33() * ::D2DZ2(f, outloc) + + 2 * contravariantMetricTensor.Getg13() * ::D2DXDZ(f, outloc); }; ASSERT2(result.getLocation() == outloc); @@ -1899,12 +1897,10 @@ Coordinates::FieldMetric Coordinates::Laplace(const Field2D& f, CELL_LOC outloc, TRACE("Coordinates::Laplace( Field2D )"); ASSERT1(location == outloc || outloc == CELL_DEFAULT); - auto const contravariant_components = - contravariantMetricTensor.getContravariantMetricTensor(); auto result = G1 * DDX(f, outloc) + G2 * DDY(f, outloc) - + contravariant_components.g11 * D2DX2(f, outloc) - + contravariant_components.g22 * D2DY2(f, outloc) - + 2.0 * contravariant_components.g12 + + contravariantMetricTensor.Getg11() * D2DX2(f, outloc) + + contravariantMetricTensor.Getg22() * D2DY2(f, outloc) + + 2.0 * contravariantMetricTensor.Getg12() * D2DXDY(f, outloc, "DEFAULT", "RGN_NOBNDRY", dfdy_boundary_conditions, dfdy_dy_region); @@ -1917,18 +1913,16 @@ Field3D Coordinates::Laplace(const Field3D& f, CELL_LOC outloc, TRACE("Coordinates::Laplace( Field3D )"); ASSERT1(location == outloc || outloc == CELL_DEFAULT); - auto const contravariant_components = - contravariantMetricTensor.getContravariantMetricTensor(); Field3D result = G1 * ::DDX(f, outloc) + G2 * ::DDY(f, outloc) + G3 * ::DDZ(f, outloc) - + contravariant_components.g11 * D2DX2(f, outloc) - + contravariant_components.g22 * D2DY2(f, outloc) - + contravariant_components.g33 * D2DZ2(f, outloc) + + contravariantMetricTensor.Getg11() * D2DX2(f, outloc) + + contravariantMetricTensor.Getg22() * D2DY2(f, outloc) + + contravariantMetricTensor.Getg33() * D2DZ2(f, outloc) + 2.0 - * (contravariant_components.g12 + * (contravariantMetricTensor.Getg12() * D2DXDY(f, outloc, "DEFAULT", "RGN_NOBNDRY", dfdy_boundary_conditions, dfdy_dy_region) - + contravariant_components.g13 * D2DXDZ(f, outloc) - + contravariant_components.g23 * D2DYDZ(f, outloc)); + + contravariantMetricTensor.Getg13() * D2DXDZ(f, outloc) + + contravariantMetricTensor.Getg23() * D2DYDZ(f, outloc)); return result; } @@ -1948,8 +1942,7 @@ Field2D Coordinates::Laplace_perpXY([[maybe_unused]] const Field2D& A, const auto outer_x_avg = [&i](const auto& f) { return 0.5 * (f[i] + f[i.xp()]); }; const BoutReal outer_x_A = outer_x_avg(A); const BoutReal outer_x_J = outer_x_avg(J); - const BoutReal outer_x_g11 = - outer_x_avg(contravariantMetricTensor.getContravariantMetricTensor().g11); + const BoutReal outer_x_g11 = outer_x_avg(contravariantMetricTensor.Getg11()); const BoutReal outer_x_dx = outer_x_avg(dx); const BoutReal outer_x_value = outer_x_A * outer_x_J * outer_x_g11 / (J[i] * outer_x_dx * dx[i]); @@ -1959,8 +1952,7 @@ Field2D Coordinates::Laplace_perpXY([[maybe_unused]] const Field2D& A, const auto inner_x_avg = [&i](const auto& f) { return 0.5 * (f[i] + f[i.xm()]); }; const BoutReal inner_x_A = inner_x_avg(A); const BoutReal inner_x_J = inner_x_avg(J); - const BoutReal inner_x_g11 = - inner_x_avg(contravariantMetricTensor.getContravariantMetricTensor().g11); + const BoutReal inner_x_g11 = inner_x_avg(contravariantMetricTensor.Getg11()); const BoutReal inner_x_dx = inner_x_avg(dx); const BoutReal inner_x_value = inner_x_A * inner_x_J * inner_x_g11 / (J[i] * inner_x_dx * dx[i]); @@ -1968,12 +1960,10 @@ Field2D Coordinates::Laplace_perpXY([[maybe_unused]] const Field2D& A, // upper y boundary const auto upper_y_avg = [&i](const auto& f) { return 0.5 * (f[i] + f[i.yp()]); }; - auto const contravariant_components = - contravariantMetricTensor.getContravariantMetricTensor(); const BoutReal upper_y_A = upper_y_avg(A); const BoutReal upper_y_J = upper_y_avg(J); const BoutReal upper_y_g_22 = upper_y_avg(covariantMetricTensor.Getg_22()); - const BoutReal upper_y_g23 = upper_y_avg(contravariant_components.g23); + const BoutReal upper_y_g23 = upper_y_avg(covariantMetricTensor.Getg_23()); const BoutReal upper_y_g_23 = upper_y_avg(covariantMetricTensor.Getg_23()); const BoutReal upper_y_dy = upper_y_avg(dy); const BoutReal upper_y_value = -upper_y_A * upper_y_J * upper_y_g23 * upper_y_g_23 @@ -1985,7 +1975,7 @@ Field2D Coordinates::Laplace_perpXY([[maybe_unused]] const Field2D& A, const BoutReal lower_y_A = lower_y_avg(A); const BoutReal lower_y_J = lower_y_avg(J); const BoutReal lower_y_g_22 = lower_y_avg(covariantMetricTensor.Getg_22()); - const BoutReal lower_y_g23 = lower_y_avg(contravariant_components.g23); + const BoutReal lower_y_g23 = lower_y_avg(contravariantMetricTensor.Getg23()); const BoutReal lower_y_g_23 = lower_y_avg(covariantMetricTensor.Getg_23()); const BoutReal lower_y_dy = lower_y_avg(dy); const BoutReal lower_y_value = -lower_y_A * lower_y_J * lower_y_g23 * lower_y_g_23 @@ -2041,11 +2031,6 @@ void Coordinates::setContravariantMetricTensor(ContravariantMetricTensor metric_ covariantMetricTensor.calcCovariant(contravariantMetricTensor, location, region); } -ContravariantMetricTensor::ContravariantComponents -Coordinates::getContravariantMetricTensor() const { - return contravariantMetricTensor.getContravariantMetricTensor(); -} - const CovariantMetricTensor::FieldMetric& Coordinates::g_11() const { return covariantMetricTensor.Getg_11(); } @@ -2065,7 +2050,26 @@ const CovariantMetricTensor::FieldMetric& Coordinates::g_23() const { return covariantMetricTensor.Getg_23(); } +const ContravariantMetricTensor::FieldMetric& Coordinates::g11() const { + return contravariantMetricTensor.Getg11(); +} +const ContravariantMetricTensor::FieldMetric& Coordinates::g22() const { + return contravariantMetricTensor.Getg22(); +} +const ContravariantMetricTensor::FieldMetric& Coordinates::g33() const { + return contravariantMetricTensor.Getg33(); +} +const ContravariantMetricTensor::FieldMetric& Coordinates::g12() const { + return contravariantMetricTensor.Getg12(); +} +const ContravariantMetricTensor::FieldMetric& Coordinates::g13() const { + return contravariantMetricTensor.Getg13(); +} +const ContravariantMetricTensor::FieldMetric& Coordinates::g23() const { + return contravariantMetricTensor.Getg23(); +} + void Coordinates::setCovariantMetricTensor(CovariantMetricTensor metric_tensor) { covariantMetricTensor.setCovariantMetricTensor(metric_tensor); contravariantMetricTensor.calcContravariant(covariantMetricTensor, location); -} +} \ No newline at end of file diff --git a/src/mesh/coordinates_accessor.cxx b/src/mesh/coordinates_accessor.cxx index 102251a62c..1acee91cb0 100644 --- a/src/mesh/coordinates_accessor.cxx +++ b/src/mesh/coordinates_accessor.cxx @@ -61,20 +61,13 @@ CoordinatesAccessor::CoordinatesAccessor(const Coordinates* coords) { coords->Bxy.ydown()[ind]; COPY_STRIPE(G1, G3); - const auto contravariant_components = coords->getContravariantMetricTensor(); // COPY_STRIPE(g11, g12, g13, g22, g23, g33); - data[stripe_size * ind.ind + static_cast(Offset::g11)] = - contravariant_components.g11[ind]; - data[stripe_size * ind.ind + static_cast(Offset::g12)] = - contravariant_components.g12[ind]; - data[stripe_size * ind.ind + static_cast(Offset::g13)] = - contravariant_components.g13[ind]; - data[stripe_size * ind.ind + static_cast(Offset::g22)] = - contravariant_components.g22[ind]; - data[stripe_size * ind.ind + static_cast(Offset::g23)] = - contravariant_components.g23[ind]; - data[stripe_size * ind.ind + static_cast(Offset::g33)] = - contravariant_components.g33[ind]; + data[stripe_size * ind.ind + static_cast(Offset::g11)] = coords->g11()[ind]; + data[stripe_size * ind.ind + static_cast(Offset::g12)] = coords->g12()[ind]; + data[stripe_size * ind.ind + static_cast(Offset::g13)] = coords->g13()[ind]; + data[stripe_size * ind.ind + static_cast(Offset::g22)] = coords->g22()[ind]; + data[stripe_size * ind.ind + static_cast(Offset::g23)] = coords->g23()[ind]; + data[stripe_size * ind.ind + static_cast(Offset::g33)] = coords->g33()[ind]; // COPY_STRIPE(g_11, g_12, g_13, g_22, g_23, g_33); data[stripe_size * ind.ind + static_cast(Offset::g_11)] = coords->g_11()[ind]; diff --git a/src/mesh/fv_ops.cxx b/src/mesh/fv_ops.cxx index 234ded5ab0..f9bc7323d2 100644 --- a/src/mesh/fv_ops.cxx +++ b/src/mesh/fv_ops.cxx @@ -31,7 +31,6 @@ Field3D Div_a_Grad_perp(const Field3D& a, const Field3D& f) { Field3D result{zeroFrom(f)}; Coordinates* coord = f.getCoordinates(); - const auto g = coord->getContravariantMetricTensor(); // Flux in x @@ -53,8 +52,8 @@ Field3D Div_a_Grad_perp(const Field3D& a, const Field3D& f) { // Calculate flux from i to i+1 BoutReal fout = 0.5 * (a(i, j, k) + a(i + 1, j, k)) - * (coord->J(i, j, k) * g.g11(i, j, k) - + coord->J(i + 1, j, k) * g.g11(i + 1, j, k)) + * (coord->J(i, j, k) * coord->g11()(i, j, k) + + coord->J(i + 1, j, k) * coord->g11()(i + 1, j, k)) * (f(i + 1, j, k) - f(i, j, k)) / (coord->dx(i, j, k) + coord->dx(i + 1, j, k)); @@ -70,7 +69,7 @@ Field3D Div_a_Grad_perp(const Field3D& a, const Field3D& f) { // 3D Metric, need yup/ydown fields. // Requires previous communication of metrics // -- should insert communication here? - if (!g.g23.hasParallelSlices() || !coord->g_23().hasParallelSlices() + if (!coord->g23().hasParallelSlices() || !coord->g_23().hasParallelSlices() || !coord->dy.hasParallelSlices() || !coord->dz.hasParallelSlices() || !coord->Bxy.hasParallelSlices() || !coord->J.hasParallelSlices()) { throw BoutException("metrics have no yup/down: Maybe communicate in init?"); @@ -88,7 +87,7 @@ Field3D Div_a_Grad_perp(const Field3D& a, const Field3D& f) { // Only in 3D case with FCI do the metrics have parallel slices const bool metric_fci = fci and bout::build::use_metric_3d; - const auto g23 = makeslices(metric_fci, g.g23); + const auto g23 = makeslices(metric_fci, coord->g23()); const auto g_23 = makeslices(metric_fci, coord->g_23()); const auto J = makeslices(metric_fci, coord->J); const auto dy = makeslices(metric_fci, coord->dy); @@ -157,7 +156,7 @@ Field3D Div_a_Grad_perp(const Field3D& a, const Field3D& f) { const BoutReal fout = 0.25 * (a_slice.c[i] + a_slice.c[ikp]) - * (J.c[i] * g.g33[i] + J.c[ikp] * g.g33[ikp]) + * (J.c[i] * coord->g33()[i] + J.c[ikp] * coord->g33()[ikp]) * ( // df/dz (f_slice.c[ikp] - f_slice.c[i]) / dz.c[i] // - g_yz * df/dy / SQ(J*B) @@ -499,7 +498,6 @@ Field3D Div_Perp_Lap(const Field3D& a, const Field3D& f, CELL_LOC outloc) { // Coordinates* coords = a.getCoordinates(outloc); Mesh* mesh = f.getMesh(); - const auto g = coords->getContravariantMetricTensor(); for (int i = mesh->xstart; i <= mesh->xend; i++) { for (int j = mesh->ystart; j <= mesh->yend; j++) { @@ -512,30 +510,32 @@ Field3D Div_Perp_Lap(const Field3D& a, const Field3D& f, CELL_LOC outloc) { // Calculate gradients on cell faces -- assumes constant grid spacing BoutReal gR = - (g.g11(i, j, k) + g.g11(i + 1, j, k)) * (f(i + 1, j, k) - f(i, j, k)) + (coords->g11()(i, j, k) + coords->g11()(i + 1, j, k)) + * (f(i + 1, j, k) - f(i, j, k)) / (coords->dx(i + 1, j, k) + coords->dx(i, j, k)) - + 0.5 * (g.g13(i, j, k) + g.g13(i + 1, j, k)) + + 0.5 * (coords->g13()(i, j, k) + coords->g13()(i + 1, j, k)) * (f(i + 1, j, kp) - f(i + 1, j, km) + f(i, j, kp) - f(i, j, km)) / (4. * coords->dz(i, j, k)); BoutReal gL = - (g.g11(i - 1, j, k) + g.g11(i, j, k)) * (f(i, j, k) - f(i - 1, j, k)) + (coords->g11()(i - 1, j, k) + coords->g11()(i, j, k)) + * (f(i, j, k) - f(i - 1, j, k)) / (coords->dx(i - 1, j, k) + coords->dx(i, j, k)) - + 0.5 * (g.g13(i - 1, j, k) + g.g13(i, j, k)) + + 0.5 * (coords->g13()(i - 1, j, k) + coords->g13()(i, j, k)) * (f(i - 1, j, kp) - f(i - 1, j, km) + f(i, j, kp) - f(i, j, km)) / (4 * coords->dz(i, j, k)); BoutReal gD = - g.g13(i, j, k) + coords->g13()(i, j, k) * (f(i + 1, j, km) - f(i - 1, j, km) + f(i + 1, j, k) - f(i - 1, j, k)) / (4. * coords->dx(i, j, k)) - + g.g33(i, j, k) * (f(i, j, k) - f(i, j, km)) / coords->dz(i, j, k); + + coords->g33()(i, j, k) * (f(i, j, k) - f(i, j, km)) / coords->dz(i, j, k); BoutReal gU = - g.g13(i, j, k) + coords->g13()(i, j, k) * (f(i + 1, j, kp) - f(i - 1, j, kp) + f(i + 1, j, k) - f(i - 1, j, k)) / (4. * coords->dx(i, j, k)) - + g.g33(i, j, k) * (f(i, j, kp) - f(i, j, k)) / coords->dz(i, j, k); + + coords->g33()(i, j, k) * (f(i, j, kp) - f(i, j, k)) / coords->dz(i, j, k); // Flow right BoutReal flux = gR * 0.25 * (coords->J(i + 1, j, k) + coords->J(i, j, k)) From 7c44f866069cd03427c653d8f85d48c93f514cd8 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Thu, 26 Oct 2023 11:25:06 +0100 Subject: [PATCH 116/491] Make getAtLocAndFillGuards return new variables for components rather than updated the existing ones. --- src/mesh/coordinates.cxx | 70 ++++++++++++++++++++-------------------- 1 file changed, 35 insertions(+), 35 deletions(-) diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 651bad4f46..324dd02d7e 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -332,15 +332,15 @@ int getAtLoc(Mesh* mesh, const Coordinates::FieldMetric& var, const std::string& return 0; } -int getAtLocAndFillGuards(Mesh* mesh, const Coordinates::FieldMetric& var, - const std::string& name, const std::string& suffix, - CELL_LOC location, BoutReal default_value, bool extrapolate_x, - bool extrapolate_y, bool no_extra_interpolate, - ParallelTransform* pt) { +const Coordinates::FieldMetric +getAtLocAndFillGuards(Mesh* mesh, const Coordinates::FieldMetric& var, + const std::string& name, const std::string& suffix, + CELL_LOC location, BoutReal default_value, bool extrapolate_x, + bool extrapolate_y, bool no_extra_interpolate, + ParallelTransform* pt) { auto ret = getAtLoc(mesh, var, name, suffix, location, default_value); - var = interpolateAndExtrapolate(var, location, extrapolate_x, extrapolate_y, - no_extra_interpolate, pt); - return ret; + return interpolateAndExtrapolate(var, location, extrapolate_x, extrapolate_y, + no_extra_interpolate, pt); } std::string getLocationSuffix(CELL_LOC location) { @@ -677,42 +677,42 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, dz = interpolateAndExtrapolate(dz, location, extrapolate_x, extrapolate_y, false, transform.get()); - getAtLocAndFillGuards(mesh, dx, "dx", suffix, location, 1.0, extrapolate_x, - extrapolate_y, false, transform.get()); + dx = getAtLocAndFillGuards(mesh, dx, "dx", suffix, location, 1.0, extrapolate_x, + extrapolate_y, false, transform.get()); if (mesh->periodicX) { communicate(dx); } - getAtLocAndFillGuards(mesh, dy, "dy", suffix, location, 1.0, extrapolate_x, - extrapolate_y, false, transform.get()); + dy = getAtLocAndFillGuards(mesh, dy, "dy", suffix, location, 1.0, extrapolate_x, + extrapolate_y, false, transform.get()); // grid data source has staggered fields, so read instead of interpolating // Diagonal components of metric tensor g^{ij} (default to 1) // TODO: Method `getAtLocAndFillGuards` violates command–query separation principle? - getAtLocAndFillGuards(mesh, contravariantMetricTensor.Getg11(), "g11", suffix, - location, 1.0, extrapolate_x, extrapolate_y, false, - transform.get()); - getAtLocAndFillGuards(mesh, contravariantMetricTensor.Getg22(), "g22", suffix, - location, 1.0, extrapolate_x, extrapolate_y, false, - transform.get()); - getAtLocAndFillGuards(mesh, contravariantMetricTensor.Getg33(), "g33", suffix, - location, 1.0, extrapolate_x, extrapolate_y, false, - transform.get()); - getAtLocAndFillGuards(mesh, contravariantMetricTensor.Getg12(), "g12", suffix, - location, 0.0, extrapolate_x, extrapolate_y, false, - transform.get()); - getAtLocAndFillGuards(mesh, contravariantMetricTensor.Getg13(), "g13", suffix, - location, 0.0, extrapolate_x, extrapolate_y, false, - transform.get()); - getAtLocAndFillGuards(mesh, contravariantMetricTensor.Getg23(), "g23", suffix, - location, 0.0, extrapolate_x, extrapolate_y, false, - transform.get()); - - setContravariantMetricTensor(ContravariantMetricTensor( - contravariantMetricTensor.Getg11(), contravariantMetricTensor.Getg22(), - contravariantMetricTensor.Getg33(), contravariantMetricTensor.Getg12(), - contravariantMetricTensor.Getg13(), contravariantMetricTensor.Getg23())); + + FieldMetric g11, g22, g33, g12, g13, g23; + + g11 = getAtLocAndFillGuards(mesh, contravariantMetricTensor.Getg11(), "g11", suffix, + location, 1.0, extrapolate_x, extrapolate_y, false, + transform.get()); + g22 = getAtLocAndFillGuards(mesh, contravariantMetricTensor.Getg22(), "g22", suffix, + location, 1.0, extrapolate_x, extrapolate_y, false, + transform.get()); + g33 = getAtLocAndFillGuards(mesh, contravariantMetricTensor.Getg33(), "g33", suffix, + location, 1.0, extrapolate_x, extrapolate_y, false, + transform.get()); + g12 = getAtLocAndFillGuards(mesh, contravariantMetricTensor.Getg12(), "g12", suffix, + location, 0.0, extrapolate_x, extrapolate_y, false, + transform.get()); + g13 = getAtLocAndFillGuards(mesh, contravariantMetricTensor.Getg13(), "g13", suffix, + location, 0.0, extrapolate_x, extrapolate_y, false, + transform.get()); + g23 = getAtLocAndFillGuards(mesh, contravariantMetricTensor.Getg23(), "g23", suffix, + location, 0.0, extrapolate_x, extrapolate_y, false, + transform.get()); + + setContravariantMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); // Check input metrics checkContravariant(); From 43022314e6c08d647186c12456c37e11f2bd6159 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Thu, 26 Oct 2023 11:41:08 +0100 Subject: [PATCH 117/491] Update more tests to use new ContravariantMetricTensor getter methods. --- tests/MMS/tokamak/tokamak.cxx | 30 ++-- .../test-drift-instability/2fluid.cxx | 8 +- .../test-interchange-instability/2fluid.cxx | 8 +- .../test-laplacexy-short/test-laplacexy.cxx | 8 +- .../integrated/test-laplacexy/loadmetric.cxx | 29 ++-- .../test-laplacexy/test-laplacexy.cxx | 6 +- .../test-laplacexz/test-laplacexz.cxx | 12 +- .../test_multigrid_laplace.cxx | 8 +- .../test_naulin_laplace.cxx | 7 +- .../invert/laplace/test_laplace_cyclic.cxx | 9 +- tests/unit/mesh/data/test_gridfromoptions.cxx | 134 ++++++++---------- tests/unit/mesh/test_coordinates.cxx | 82 +++++------ 12 files changed, 156 insertions(+), 185 deletions(-) diff --git a/tests/MMS/tokamak/tokamak.cxx b/tests/MMS/tokamak/tokamak.cxx index 653a938c64..5ffe1bf2f4 100644 --- a/tests/MMS/tokamak/tokamak.cxx +++ b/tests/MMS/tokamak/tokamak.cxx @@ -80,23 +80,25 @@ class TokamakMMS : public PhysicsModel { sbp = -1.0; } - Coordinates::MetricTensor contravariant_components = coords->getContravariantMetricTensor(); - contravariant_components.g11 = SQ(Rxy * Bpxy); - contravariant_components.g22 = 1.0 / SQ(hthe); - contravariant_components.g33 = SQ(sinty) * contravariant_components.g11 + SQ(coords->Bxy) / contravariant_components.g11; - contravariant_components.g12 = 0.0; - contravariant_components.g13 = -sinty * contravariant_components.g11; - contravariant_components.g23 = -sbp * Btxy / (hthe * Bpxy * Rxy); - coords->setContravariantMetricTensor(contravariant_components); + const auto g11 = SQ(Rxy * Bpxy); + const auto g22 = 1.0 / SQ(hthe); + const auto g33 = SQ(sinty) * coords->g11() + SQ(coords->Bxy) / coords->g11(); + const auto g12 = 0.0; + const auto g13 = -sinty * coords->g11(); + const auto g23 = -sbp * Btxy / (hthe * Bpxy * Rxy); + coords->setContravariantMetricTensor( + ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); coords->J = hthe / Bpxy; - coords->g_11 = 1.0 / contravariant_components.g11 + SQ(sinty * Rxy); - coords->g_22 = SQ(coords->Bxy * hthe / Bpxy); - coords->g_33 = Rxy * Rxy; - coords->g_12 = sbp * Btxy * hthe * sinty * Rxy / Bpxy; - coords->g_13 = sinty * Rxy * Rxy; - coords->g_23 = sbp * Btxy * hthe * Rxy / Bpxy; + const auto g_11 = 1.0 / coords->g11() + SQ(sinty * Rxy); + const auto g_22 = SQ(coords->Bxy * hthe / Bpxy); + const auto g_33 = Rxy * Rxy; + const auto g_12 = sbp * Btxy * hthe * sinty * Rxy / Bpxy; + const auto g_13 = sinty * Rxy * Rxy; + const auto g_23 = sbp * Btxy * hthe * Rxy / Bpxy; + coords->setCovariantMetricTensor( + CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); coords->geometry(); } diff --git a/tests/integrated/test-drift-instability/2fluid.cxx b/tests/integrated/test-drift-instability/2fluid.cxx index f46f00e843..f69ec5ee2f 100644 --- a/tests/integrated/test-drift-instability/2fluid.cxx +++ b/tests/integrated/test-drift-instability/2fluid.cxx @@ -217,14 +217,12 @@ class TwoFluid : public PhysicsModel { /**************** CALCULATE METRICS ******************/ - const auto contravariant_components = coord->getContravariantMetricTensor(); CovariantMetricTensor::FieldMetric g11, g22, g33, g12, g13, g23; g11 = SQ(Rxy * Bpxy); g22 = 1.0 / SQ(hthe); - g33 = SQ(I) * contravariant_components.g11 - + SQ(coord->Bxy) / contravariant_components.g11; + g33 = SQ(I) * coord->g11() + SQ(coord->Bxy) / coord->g11(); g12 = 0.0; - g13 = -I * contravariant_components.g11; + g13 = -I * coord->g11(); g23 = -Btxy / (hthe * Bpxy * Rxy); coord->setContravariantMetricTensor( ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); @@ -232,7 +230,7 @@ class TwoFluid : public PhysicsModel { coord->J = hthe / Bpxy; CovariantMetricTensor::FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; - g_11 = 1.0 / contravariant_components.g11 + SQ(I * Rxy); + g_11 = 1.0 / coord->g11() + SQ(I * Rxy); g_22 = SQ(coord->Bxy * hthe / Bpxy); g_33 = Rxy * Rxy; g_12 = Btxy * hthe * I * Rxy / Bpxy; diff --git a/tests/integrated/test-interchange-instability/2fluid.cxx b/tests/integrated/test-interchange-instability/2fluid.cxx index d962e46121..632c1eadf2 100644 --- a/tests/integrated/test-interchange-instability/2fluid.cxx +++ b/tests/integrated/test-interchange-instability/2fluid.cxx @@ -139,14 +139,12 @@ class Interchange : public PhysicsModel { /**************** CALCULATE METRICS ******************/ - const auto contravariant_components = coord->getContravariantMetricTensor(); CovariantMetricTensor::FieldMetric g11, g22, g33, g12, g13, g23; g11 = SQ(Rxy * Bpxy); g22 = 1.0 / SQ(hthe); - g33 = SQ(I) * contravariant_components.g11 - + SQ(coord->Bxy) / contravariant_components.g11; + g33 = SQ(I) * coord->g11() + SQ(coord->Bxy) / coord->g11(); g12 = 0.0; - g13 = -I * contravariant_components.g11; + g13 = -I * coord->g11(); g23 = -Btxy / (hthe * Bpxy * Rxy); coord->setContravariantMetricTensor( ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); @@ -154,7 +152,7 @@ class Interchange : public PhysicsModel { coord->J = hthe / Bpxy; CovariantMetricTensor::FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; - g_11 = 1.0 / contravariant_components.g11 + SQ(I * Rxy); + g_11 = 1.0 / coord->g11() + SQ(I * Rxy); g_22 = SQ(coord->Bxy * hthe / Bpxy); g_33 = Rxy * Rxy; g_12 = Btxy * hthe * I * Rxy / Bpxy; diff --git a/tests/integrated/test-laplacexy-short/test-laplacexy.cxx b/tests/integrated/test-laplacexy-short/test-laplacexy.cxx index d0727ec97f..baa9b21264 100644 --- a/tests/integrated/test-laplacexy-short/test-laplacexy.cxx +++ b/tests/integrated/test-laplacexy-short/test-laplacexy.cxx @@ -65,9 +65,8 @@ int main(int argc, char** argv) { if (include_y_derivs) { rhs = a * DC(Laplace_perp(f)) + DC(Grad_perp(a) * Grad_perp(f)) + b * f; } else { - Coordinates::MetricTensor metric_tensor = coords->getContravariantMetricTensor(); - rhs = - a * DC(Delp2(f, CELL_DEFAULT, false)) + DC(metric_tensor.g11 * DDX(a) * DDX(f)) + b * f; + rhs = a * DC(Delp2(f, CELL_DEFAULT, false)) + DC(coords->g11() * DDX(a) * DDX(f)) + + b * f; } laplacexy.setCoefs(a, b); @@ -83,9 +82,8 @@ int main(int argc, char** argv) { if (include_y_derivs) { rhs_check = a * DC(Laplace_perp(sol)) + DC(Grad_perp(a) * Grad_perp(sol)) + b * sol; } else { - Coordinates::MetricTensor metric_tensor = coords->getContravariantMetricTensor(); rhs_check = a * DC(Delp2(sol, CELL_DEFAULT, false)) - + DC(metric_tensor.g11 * DDX(a) * DDX(sol)) + b * sol; + + DC(coords->g11() * DDX(a) * DDX(sol)) + b * sol; } Options dump; diff --git a/tests/integrated/test-laplacexy/loadmetric.cxx b/tests/integrated/test-laplacexy/loadmetric.cxx index 8ac3275f05..2bb1a79114 100644 --- a/tests/integrated/test-laplacexy/loadmetric.cxx +++ b/tests/integrated/test-laplacexy/loadmetric.cxx @@ -49,22 +49,25 @@ void LoadMetric(BoutReal Lnorm, BoutReal Bnorm) { sbp = -1.0; } - Coordinates::MetricTensor metric_tensor = coords->getContravariantMetricTensor(); - metric_tensor.g11 = pow(Rxy * Bpxy, 2); - metric_tensor.g22 = 1.0 / pow(hthe, 2); - metric_tensor.g33 = pow(sinty, 2) * metric_tensor.g11 + pow(coords->Bxy, 2) / metric_tensor.g11; - metric_tensor.g12 = 0.0; - metric_tensor.g13 = -sinty * metric_tensor.g11; - metric_tensor.g23 = -sbp * Btxy / (hthe * Bpxy * Rxy); + const auto g11 = pow(Rxy * Bpxy, 2); + const auto g22 = 1.0 / pow(hthe, 2); + const auto g33 = pow(sinty, 2) * coords->g11() + pow(coords->Bxy, 2) / coords->g11(); + const auto g12 = 0.0; + const auto g13 = -sinty * coords->g11(); + const auto g23 = -sbp * Btxy / (hthe * Bpxy * Rxy); + coords->setContravariantMetricTensor( + ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); coords->J = hthe / Bpxy; - coords->g_11 = 1.0 / metric_tensor.g11 + pow(sinty * Rxy, 2); - coords->g_22 = pow(coords->Bxy * hthe / Bpxy, 2); - coords->g_33 = Rxy * Rxy; - coords->g_12 = sbp * Btxy * hthe * sinty * Rxy / Bpxy; - coords->g_13 = sinty * Rxy * Rxy; - coords->g_23 = sbp * Btxy * hthe * Rxy / Bpxy; + const auto g_11 = 1.0 / coords->g11() + pow(sinty * Rxy, 2); + const auto g_22 = pow(coords->Bxy * hthe / Bpxy, 2); + const auto g_33 = Rxy * Rxy; + const auto g_12 = sbp * Btxy * hthe * sinty * Rxy / Bpxy; + const auto g_13 = sinty * Rxy * Rxy; + const auto g_23 = sbp * Btxy * hthe * Rxy / Bpxy; + coords->setCovariantMetricTensor( + CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); coords->geometry(); } diff --git a/tests/integrated/test-laplacexy/test-laplacexy.cxx b/tests/integrated/test-laplacexy/test-laplacexy.cxx index 5e310346a8..f06c95b4a4 100644 --- a/tests/integrated/test-laplacexy/test-laplacexy.cxx +++ b/tests/integrated/test-laplacexy/test-laplacexy.cxx @@ -61,8 +61,7 @@ int main(int argc, char** argv) { if (include_y_derivs) { rhs = a * Laplace_perp(f) + Grad_perp(a) * Grad_perp(f) + b * f; } else { - Coordinates::MetricTensor metric_tensor = coords->getContravariantMetricTensor(); - rhs = a * Delp2(f, CELL_DEFAULT, false) + metric_tensor.g11 * DDX(a) * DDX(f) + b * f; + rhs = a * Delp2(f, CELL_DEFAULT, false) + coords->g11() * DDX(a) * DDX(f) + b * f; } LaplaceXY laplacexy; @@ -81,9 +80,8 @@ int main(int argc, char** argv) { rhs_check = a * Laplace_perp(solution) + Grad_perp(a) * Grad_perp(solution) + b * solution; } else { - Coordinates::MetricTensor metric_tensor = coords->getContravariantMetricTensor(); rhs_check = a * Delp2(solution, CELL_DEFAULT, false) - + metric_tensor.g11 * DDX(a) * DDX(solution) + b * solution; + + coords->g11() * DDX(a) * DDX(solution) + b * solution; } Options dump; diff --git a/tests/integrated/test-laplacexz/test-laplacexz.cxx b/tests/integrated/test-laplacexz/test-laplacexz.cxx index 8d1b733488..5a6571d33b 100644 --- a/tests/integrated/test-laplacexz/test-laplacexz.cxx +++ b/tests/integrated/test-laplacexz/test-laplacexz.cxx @@ -20,20 +20,24 @@ int main(int argc, char** argv) { auto inv = LaplaceXZ::create(bout::globals::mesh); auto coord = bout::globals::mesh->getCoordinates(); - Coordinates::MetricTensor metric_tensor = coord->getContravariantMetricTensor(); - metric_tensor.g13 = 1.8; // test off-diagonal components with nonzero value + const auto g13 = 1.8; // test off-diagonal components with nonzero value + coord->setContravariantMetricTensor(ContravariantMetricTensor( + coord->g11(), coord->g22(), coord->g33(), coord->g12(), g13, coord->g23())); // create some input field Field3D f = FieldFactory::get()->create3D("f", Options::getRoot(), bout::globals::mesh); // Calculate the Laplacian with non-zero g13 - Field3D g = metric_tensor.g11 * D2DX2(f) + metric_tensor.g13 * D2DXDZ(f) + metric_tensor.g33 * D2DZ2(f); + Field3D g = + coord->g11() * D2DX2(f) + coord->g13() * D2DXDZ(f) + coord->g33() * D2DZ2(f); inv->setCoefs(Field2D(1.0), Field2D(0.0)); Field3D f2 = inv->solve(g, 0.0); // Invert the Laplacian. - metric_tensor.g13 = 0.0; // reset to 0.0 for original laplacexz test + // reset to 0.0 for original laplacexz test + coord->setContravariantMetricTensor(ContravariantMetricTensor( + coord->g11(), coord->g22(), coord->g33(), coord->g12(), 0.0, coord->g23())); // Now the normal test. output.write("Setting coefficients\n"); diff --git a/tests/integrated/test-multigrid_laplace/test_multigrid_laplace.cxx b/tests/integrated/test-multigrid_laplace/test_multigrid_laplace.cxx index 6bad20c934..50a3cbfc1a 100644 --- a/tests/integrated/test-multigrid_laplace/test_multigrid_laplace.cxx +++ b/tests/integrated/test-multigrid_laplace/test_multigrid_laplace.cxx @@ -298,11 +298,9 @@ int main(int argc, char** argv) { Field3D this_Grad_perp_dot_Grad_perp(const Field3D& f, const Field3D& g) { auto* mesh = f.getMesh(); - Field3D result = - mesh->getCoordinates()->getContravariantMetricTensor().g11 * ::DDX(f) * ::DDX(g) - + mesh->getCoordinates()->getContravariantMetricTensor().g33 * ::DDZ(f) * ::DDZ(g) - + mesh->getCoordinates()->getContravariantMetricTensor().g13 - * (DDX(f) * DDZ(g) + DDZ(f) * DDX(g)); + Field3D result = mesh->getCoordinates()->g11() * ::DDX(f) * ::DDX(g) + + mesh->getCoordinates()->g33() * ::DDZ(f) * ::DDZ(g) + + mesh->getCoordinates()->g13() * (DDX(f) * DDZ(g) + DDZ(f) * DDX(g)); return result; } diff --git a/tests/integrated/test-naulin-laplace/test_naulin_laplace.cxx b/tests/integrated/test-naulin-laplace/test_naulin_laplace.cxx index 24ff91b052..83b589d145 100644 --- a/tests/integrated/test-naulin-laplace/test_naulin_laplace.cxx +++ b/tests/integrated/test-naulin-laplace/test_naulin_laplace.cxx @@ -304,10 +304,9 @@ int main(int argc, char** argv) { Field3D this_Grad_perp_dot_Grad_perp(const Field3D& f, const Field3D& g) { const auto* coords = f.getCoordinates(); - const auto metric_tensor = coords->getContravariantMetricTensor(); - Field3D result = metric_tensor.g11 * ::DDX(f) * ::DDX(g) - + metric_tensor.g33 * ::DDZ(f) * ::DDZ(g) - + metric_tensor.g13 * (DDX(f) * DDZ(g) + DDZ(f) * DDX(g)); + Field3D result = coords->g11() * ::DDX(f) * ::DDX(g) + + coords->g33() * ::DDZ(f) * ::DDZ(g) + + coords->g13() * (DDX(f) * DDZ(g) + DDZ(f) * DDX(g)); return result; } diff --git a/tests/unit/invert/laplace/test_laplace_cyclic.cxx b/tests/unit/invert/laplace/test_laplace_cyclic.cxx index 3f9cd57799..709896d682 100644 --- a/tests/unit/invert/laplace/test_laplace_cyclic.cxx +++ b/tests/unit/invert/laplace/test_laplace_cyclic.cxx @@ -39,12 +39,9 @@ class CyclicForwardOperator { } const Field3D operator()(Field3D& f) { - const auto contravariant_components = coords->getContravariantMetricTensor(); - auto result = - d * Delp2(f) - + (contravariant_components.g11 * DDX(f) + contravariant_components.g13 * DDZ(f)) - * DDX(c2) / c1 - + a * f + ex * DDX(f) + ez * DDZ(f); + auto result = d * Delp2(f) + + (coords->g11() * DDX(f) + coords->g13() * DDZ(f)) * DDX(c2) / c1 + + a * f + ex * DDX(f) + ez * DDZ(f); applyBoundaries(result, f); return result; } diff --git a/tests/unit/mesh/data/test_gridfromoptions.cxx b/tests/unit/mesh/data/test_gridfromoptions.cxx index e5e4b63cd4..c7766f6505 100644 --- a/tests/unit/mesh/data/test_gridfromoptions.cxx +++ b/tests/unit/mesh/data/test_gridfromoptions.cxx @@ -359,14 +359,12 @@ TEST_F(GridFromOptionsTest, CoordinatesCentre) { mesh_from_options.communicate(expected_2d); - const auto g = coords->getContravariantMetricTensor(); - - EXPECT_TRUE(IsFieldEqual(g.g11, expected_metric + 5.)); - EXPECT_TRUE(IsFieldEqual(g.g22, expected_metric + 4.)); - EXPECT_TRUE(IsFieldEqual(g.g33, expected_metric + 3.)); - EXPECT_TRUE(IsFieldEqual(g.g12, expected_metric + 2.)); - EXPECT_TRUE(IsFieldEqual(g.g13, expected_metric + 1.)); - EXPECT_TRUE(IsFieldEqual(g.g23, expected_metric)); + EXPECT_TRUE(IsFieldEqual(coords->g11(), expected_metric + 5.)); + EXPECT_TRUE(IsFieldEqual(coords->g22(), expected_metric + 4.)); + EXPECT_TRUE(IsFieldEqual(coords->g33(), expected_metric + 3.)); + EXPECT_TRUE(IsFieldEqual(coords->g12(), expected_metric + 2.)); + EXPECT_TRUE(IsFieldEqual(coords->g13(), expected_metric + 1.)); + EXPECT_TRUE(IsFieldEqual(coords->g23(), expected_metric)); } #if not(BOUT_USE_METRIC_3D) @@ -375,14 +373,12 @@ TEST_F(GridFromOptionsTest, CoordinatesZlow) { mesh_from_options.communicate(expected_2d); - const auto g = coords->getContravariantMetricTensor(); - - EXPECT_TRUE(IsFieldEqual(g.g11, expected_metric + 5.)); - EXPECT_TRUE(IsFieldEqual(g.g22, expected_metric + 4.)); - EXPECT_TRUE(IsFieldEqual(g.g33, expected_metric + 3.)); - EXPECT_TRUE(IsFieldEqual(g.g12, expected_metric + 2.)); - EXPECT_TRUE(IsFieldEqual(g.g13, expected_metric + 1.)); - EXPECT_TRUE(IsFieldEqual(g.g23, expected_metric)); + EXPECT_TRUE(IsFieldEqual(coords->g11(), expected_metric + 5.)); + EXPECT_TRUE(IsFieldEqual(coords->g22(), expected_metric + 4.)); + EXPECT_TRUE(IsFieldEqual(coords->g33(), expected_metric + 3.)); + EXPECT_TRUE(IsFieldEqual(coords->g12(), expected_metric + 2.)); + EXPECT_TRUE(IsFieldEqual(coords->g13(), expected_metric + 1.)); + EXPECT_TRUE(IsFieldEqual(coords->g23(), expected_metric)); } #else // Maybe replace by MMS test, because we need a periodic function in z. @@ -405,19 +401,17 @@ TEST_F(GridFromOptionsTest, CoordinatesXlowInterp) { mesh_from_options.communicate(expected_xlow); - const auto g = coords->getContravariantMetricTensor(); - EXPECT_TRUE( - IsFieldEqual(g.g11, expected_xlow + 5., "RGN_NOBNDRY", this_tolerance)); + IsFieldEqual(coords->g11(), expected_xlow + 5., "RGN_NOBNDRY", this_tolerance)); EXPECT_TRUE( - IsFieldEqual(g.g22, expected_xlow + 4., "RGN_NOBNDRY", this_tolerance)); + IsFieldEqual(coords->g22(), expected_xlow + 4., "RGN_NOBNDRY", this_tolerance)); EXPECT_TRUE( - IsFieldEqual(g.g33, expected_xlow + 3., "RGN_NOBNDRY", this_tolerance)); + IsFieldEqual(coords->g33(), expected_xlow + 3., "RGN_NOBNDRY", this_tolerance)); EXPECT_TRUE( - IsFieldEqual(g.g12, expected_xlow + 2., "RGN_NOBNDRY", this_tolerance)); + IsFieldEqual(coords->g12(), expected_xlow + 2., "RGN_NOBNDRY", this_tolerance)); EXPECT_TRUE( - IsFieldEqual(g.g13, expected_xlow + 1., "RGN_NOBNDRY", this_tolerance)); - EXPECT_TRUE(IsFieldEqual(g.g23, expected_xlow, "RGN_NOBNDRY", this_tolerance)); + IsFieldEqual(coords->g13(), expected_xlow + 1., "RGN_NOBNDRY", this_tolerance)); + EXPECT_TRUE(IsFieldEqual(coords->g23(), expected_xlow, "RGN_NOBNDRY", this_tolerance)); } TEST_F(GridFromOptionsTest, CoordinatesXlowRead) { @@ -449,20 +443,18 @@ TEST_F(GridFromOptionsTest, CoordinatesXlowRead) { mesh_from_options.communicate(expected_xlow); - const auto g = coords->getContravariantMetricTensor(); - - EXPECT_TRUE(IsFieldEqual(g.g11, expected_xlow + 5.)); - EXPECT_TRUE(g.g11.getLocation() == CELL_XLOW); - EXPECT_TRUE(IsFieldEqual(g.g22, expected_xlow + 4.)); - EXPECT_TRUE(g.g22.getLocation() == CELL_XLOW); - EXPECT_TRUE(IsFieldEqual(g.g33, expected_xlow + 3.)); - EXPECT_TRUE(g.g33.getLocation() == CELL_XLOW); - EXPECT_TRUE(IsFieldEqual(g.g12, expected_xlow + 2.)); - EXPECT_TRUE(g.g12.getLocation() == CELL_XLOW); - EXPECT_TRUE(IsFieldEqual(g.g13, expected_xlow + 1.)); - EXPECT_TRUE(g.g13.getLocation() == CELL_XLOW); - EXPECT_TRUE(IsFieldEqual(g.g23, expected_xlow)); - EXPECT_TRUE(g.g23.getLocation() == CELL_XLOW); + EXPECT_TRUE(IsFieldEqual(coords->g11(), expected_xlow + 5.)); + EXPECT_TRUE(coords->g11().getLocation() == CELL_XLOW); + EXPECT_TRUE(IsFieldEqual(coords->g22(), expected_xlow + 4.)); + EXPECT_TRUE(coords->g22().getLocation() == CELL_XLOW); + EXPECT_TRUE(IsFieldEqual(coords->g33(), expected_xlow + 3.)); + EXPECT_TRUE(coords->g33().getLocation() == CELL_XLOW); + EXPECT_TRUE(IsFieldEqual(coords->g12(), expected_xlow + 2.)); + EXPECT_TRUE(coords->g12().getLocation() == CELL_XLOW); + EXPECT_TRUE(IsFieldEqual(coords->g13(), expected_xlow + 1.)); + EXPECT_TRUE(coords->g13().getLocation() == CELL_XLOW); + EXPECT_TRUE(IsFieldEqual(coords->g23(), expected_xlow)); + EXPECT_TRUE(coords->g23().getLocation() == CELL_XLOW); } TEST_F(GridFromOptionsTest, CoordinatesYlowInterp) { @@ -483,25 +475,23 @@ TEST_F(GridFromOptionsTest, CoordinatesYlowInterp) { mesh_from_options.communicate(expected_ylow); - const auto g = coords->getContravariantMetricTensor(); - EXPECT_TRUE( - IsFieldEqual(g.g11, expected_ylow + 5., "RGN_NOBNDRY", this_tolerance)); - EXPECT_TRUE(g.g11.getLocation() == CELL_YLOW); + IsFieldEqual(coords->g11(), expected_ylow + 5., "RGN_NOBNDRY", this_tolerance)); + EXPECT_TRUE(coords->g11().getLocation() == CELL_YLOW); EXPECT_TRUE( - IsFieldEqual(g.g22, expected_ylow + 4., "RGN_NOBNDRY", this_tolerance)); - EXPECT_TRUE(g.g22.getLocation() == CELL_YLOW); + IsFieldEqual(coords->g22(), expected_ylow + 4., "RGN_NOBNDRY", this_tolerance)); + EXPECT_TRUE(coords->g22().getLocation() == CELL_YLOW); EXPECT_TRUE( - IsFieldEqual(g.g33, expected_ylow + 3., "RGN_NOBNDRY", this_tolerance)); - EXPECT_TRUE(g.g33.getLocation() == CELL_YLOW); + IsFieldEqual(coords->g33(), expected_ylow + 3., "RGN_NOBNDRY", this_tolerance)); + EXPECT_TRUE(coords->g33().getLocation() == CELL_YLOW); EXPECT_TRUE( - IsFieldEqual(g.g12, expected_ylow + 2., "RGN_NOBNDRY", this_tolerance)); - EXPECT_TRUE(g.g12.getLocation() == CELL_YLOW); + IsFieldEqual(coords->g12(), expected_ylow + 2., "RGN_NOBNDRY", this_tolerance)); + EXPECT_TRUE(coords->g12().getLocation() == CELL_YLOW); EXPECT_TRUE( - IsFieldEqual(g.g13, expected_ylow + 1., "RGN_NOBNDRY", this_tolerance)); - EXPECT_TRUE(g.g13.getLocation() == CELL_YLOW); - EXPECT_TRUE(IsFieldEqual(g.g23, expected_ylow, "RGN_NOBNDRY", this_tolerance)); - EXPECT_TRUE(g.g23.getLocation() == CELL_YLOW); + IsFieldEqual(coords->g13(), expected_ylow + 1., "RGN_NOBNDRY", this_tolerance)); + EXPECT_TRUE(coords->g13().getLocation() == CELL_YLOW); + EXPECT_TRUE(IsFieldEqual(coords->g23(), expected_ylow, "RGN_NOBNDRY", this_tolerance)); + EXPECT_TRUE(coords->g23().getLocation() == CELL_YLOW); #endif } @@ -536,20 +526,18 @@ TEST_F(GridFromOptionsTest, CoordinatesYlowRead) { mesh_from_options.communicate(expected_ylow); - const auto g = coords->getContravariantMetricTensor(); - - EXPECT_TRUE(IsFieldEqual(g.g11, expected_ylow + 5., "RGN_ALL", this_tolerance)); - EXPECT_TRUE(g.g11.getLocation() == CELL_YLOW); - EXPECT_TRUE(IsFieldEqual(g.g22, expected_ylow + 4., "RGN_ALL", this_tolerance)); - EXPECT_TRUE(g.g22.getLocation() == CELL_YLOW); - EXPECT_TRUE(IsFieldEqual(g.g33, expected_ylow + 3., "RGN_ALL", this_tolerance)); - EXPECT_TRUE(g.g33.getLocation() == CELL_YLOW); - EXPECT_TRUE(IsFieldEqual(g.g12, expected_ylow + 2., "RGN_ALL", this_tolerance)); - EXPECT_TRUE(g.g12.getLocation() == CELL_YLOW); - EXPECT_TRUE(IsFieldEqual(g.g13, expected_ylow + 1., "RGN_ALL", this_tolerance)); - EXPECT_TRUE(g.g13.getLocation() == CELL_YLOW); - EXPECT_TRUE(IsFieldEqual(g.g23, expected_ylow, "RGN_ALL", this_tolerance)); - EXPECT_TRUE(g.g23.getLocation() == CELL_YLOW); + EXPECT_TRUE(IsFieldEqual(coords->g11(), expected_ylow + 5., "RGN_ALL", this_tolerance)); + EXPECT_TRUE(coords->g11().getLocation() == CELL_YLOW); + EXPECT_TRUE(IsFieldEqual(coords->g22(), expected_ylow + 4., "RGN_ALL", this_tolerance)); + EXPECT_TRUE(coords->g22().getLocation() == CELL_YLOW); + EXPECT_TRUE(IsFieldEqual(coords->g33(), expected_ylow + 3., "RGN_ALL", this_tolerance)); + EXPECT_TRUE(coords->g33().getLocation() == CELL_YLOW); + EXPECT_TRUE(IsFieldEqual(coords->g12(), expected_ylow + 2., "RGN_ALL", this_tolerance)); + EXPECT_TRUE(coords->g12().getLocation() == CELL_YLOW); + EXPECT_TRUE(IsFieldEqual(coords->g13(), expected_ylow + 1., "RGN_ALL", this_tolerance)); + EXPECT_TRUE(coords->g13().getLocation() == CELL_YLOW); + EXPECT_TRUE(IsFieldEqual(coords->g23(), expected_ylow, "RGN_ALL", this_tolerance)); + EXPECT_TRUE(coords->g23().getLocation() == CELL_YLOW); #endif } @@ -560,13 +548,11 @@ TEST_F(GridFromOptionsTest, CoordinatesZlowRead) { auto coords = mesh_from_options.getCoordinates(CELL_ZLOW); - const auto g = coords->getContravariantMetricTensor(); - - EXPECT_TRUE(IsFieldEqual(g.g11, expected_2d + 5.)); - EXPECT_TRUE(IsFieldEqual(g.g22, expected_2d + 4.)); - EXPECT_TRUE(IsFieldEqual(g.g33, expected_2d + 3.)); - EXPECT_TRUE(IsFieldEqual(g.g12, expected_2d + 2.)); - EXPECT_TRUE(IsFieldEqual(g.g13, expected_2d + 1.)); - EXPECT_TRUE(IsFieldEqual(g.g23, expected_2d)); + EXPECT_TRUE(IsFieldEqual(coords->g11(), expected_2d + 5.)); + EXPECT_TRUE(IsFieldEqual(coords->g22(), expected_2d + 4.)); + EXPECT_TRUE(IsFieldEqual(coords->g33(), expected_2d + 3.)); + EXPECT_TRUE(IsFieldEqual(coords->g12(), expected_2d + 2.)); + EXPECT_TRUE(IsFieldEqual(coords->g13(), expected_2d + 1.)); + EXPECT_TRUE(IsFieldEqual(coords->g23(), expected_2d)); #endif } diff --git a/tests/unit/mesh/test_coordinates.cxx b/tests/unit/mesh/test_coordinates.cxx index a6f0e741eb..d0a31d91f1 100644 --- a/tests/unit/mesh/test_coordinates.cxx +++ b/tests/unit/mesh/test_coordinates.cxx @@ -178,14 +178,12 @@ TEST_F(CoordinatesTest, CalcCovariant) { coords.calcContravariant(); output_info.enable(); - const auto g = coords.getContravariantMetricTensor(); - - EXPECT_TRUE(IsFieldEqual(g.g11, 1.0)); - EXPECT_TRUE(IsFieldEqual(g.g22, 1.0)); - EXPECT_TRUE(IsFieldEqual(g.g33, 1.0)); - EXPECT_TRUE(IsFieldEqual(g.g12, 0.0)); - EXPECT_TRUE(IsFieldEqual(g.g13, 0.0)); - EXPECT_TRUE(IsFieldEqual(g.g23, 0.0)); + EXPECT_TRUE(IsFieldEqual(coords.g11(), 1.0)); + EXPECT_TRUE(IsFieldEqual(coords.g22(), 1.0)); + EXPECT_TRUE(IsFieldEqual(coords.g33(), 1.0)); + EXPECT_TRUE(IsFieldEqual(coords.g12(), 0.0)); + EXPECT_TRUE(IsFieldEqual(coords.g13(), 0.0)); + EXPECT_TRUE(IsFieldEqual(coords.g23(), 0.0)); } // #endif @@ -200,14 +198,12 @@ TEST_F(CoordinatesTest, DefaultConstructor) { EXPECT_TRUE(IsFieldEqual(coords.dy, 1.0)); EXPECT_TRUE(IsFieldEqual(coords.dz, default_dz)); - const auto g = coords.getContravariantMetricTensor(); - - EXPECT_TRUE(IsFieldEqual(g.g11, 1.0)); - EXPECT_TRUE(IsFieldEqual(g.g22, 1.0)); - EXPECT_TRUE(IsFieldEqual(g.g33, 1.0)); - EXPECT_TRUE(IsFieldEqual(g.g12, 0.0)); - EXPECT_TRUE(IsFieldEqual(g.g13, 0.0)); - EXPECT_TRUE(IsFieldEqual(g.g23, 0.0)); + EXPECT_TRUE(IsFieldEqual(coords.g11(), 1.0)); + EXPECT_TRUE(IsFieldEqual(coords.g22(), 1.0)); + EXPECT_TRUE(IsFieldEqual(coords.g33(), 1.0)); + EXPECT_TRUE(IsFieldEqual(coords.g12(), 0.0)); + EXPECT_TRUE(IsFieldEqual(coords.g13(), 0.0)); + EXPECT_TRUE(IsFieldEqual(coords.g23(), 0.0)); EXPECT_TRUE(IsFieldEqual(coords.J, 1.0)); EXPECT_TRUE(IsFieldEqual(coords.Bxy, 1.0)); @@ -228,14 +224,12 @@ TEST_F(CoordinatesTest, ConstructWithMeshSpacing) { EXPECT_TRUE(IsFieldEqual(coords.dy, 3.2)); EXPECT_TRUE(IsFieldEqual(coords.dz, 42.)); - const auto g = coords.getContravariantMetricTensor(); - - EXPECT_TRUE(IsFieldEqual(g.g11, 1.0)); - EXPECT_TRUE(IsFieldEqual(g.g22, 1.0)); - EXPECT_TRUE(IsFieldEqual(g.g33, 1.0)); - EXPECT_TRUE(IsFieldEqual(g.g12, 0.0)); - EXPECT_TRUE(IsFieldEqual(g.g13, 0.0)); - EXPECT_TRUE(IsFieldEqual(g.g23, 0.0)); + EXPECT_TRUE(IsFieldEqual(coords.g11(), 1.0)); + EXPECT_TRUE(IsFieldEqual(coords.g22(), 1.0)); + EXPECT_TRUE(IsFieldEqual(coords.g33(), 1.0)); + EXPECT_TRUE(IsFieldEqual(coords.g12(), 0.0)); + EXPECT_TRUE(IsFieldEqual(coords.g13(), 0.0)); + EXPECT_TRUE(IsFieldEqual(coords.g23(), 0.0)); EXPECT_TRUE(IsFieldEqual(coords.J, 1.0)); EXPECT_TRUE(IsFieldEqual(coords.Bxy, 1.0)); @@ -271,13 +265,12 @@ TEST_F(CoordinatesTest, ConstructWithDiagonalContravariantMetric) { EXPECT_TRUE(IsFieldEqual(coords.dz, default_dz)); // Diagonal contravariant metric - const auto g = coords.getContravariantMetricTensor(); - EXPECT_TRUE(IsFieldEqual(g.g11, 2.0)); - EXPECT_TRUE(IsFieldEqual(g.g22, 3.2)); - EXPECT_TRUE(IsFieldEqual(g.g33, 42)); - EXPECT_TRUE(IsFieldEqual(g.g12, 0.0)); - EXPECT_TRUE(IsFieldEqual(g.g13, 0.0)); - EXPECT_TRUE(IsFieldEqual(g.g23, 0.0)); + EXPECT_TRUE(IsFieldEqual(coords.g11(), 2.0)); + EXPECT_TRUE(IsFieldEqual(coords.g22(), 3.2)); + EXPECT_TRUE(IsFieldEqual(coords.g33(), 42)); + EXPECT_TRUE(IsFieldEqual(coords.g12(), 0.0)); + EXPECT_TRUE(IsFieldEqual(coords.g13(), 0.0)); + EXPECT_TRUE(IsFieldEqual(coords.g23(), 0.0)); // Covariant metric should be inverse // Note: Not calculated in corners @@ -333,14 +326,12 @@ TEST_F(CoordinatesTest, GetContravariantMetricTensor) { FieldMetric{0.0}, // ShiftTorsion FieldMetric{0.0}}; // IntShiftTorsion - const auto contravariant_components = coords.getContravariantMetricTensor(); - - EXPECT_TRUE(IsFieldEqual(contravariant_components.g11, 1.2)); - EXPECT_TRUE(IsFieldEqual(contravariant_components.g22, 2.3)); - EXPECT_TRUE(IsFieldEqual(contravariant_components.g33, 3.4)); - EXPECT_TRUE(IsFieldEqual(contravariant_components.g12, 4.5)); - EXPECT_TRUE(IsFieldEqual(contravariant_components.g13, 5.6)); - EXPECT_TRUE(IsFieldEqual(contravariant_components.g23, 6.7)); + EXPECT_TRUE(IsFieldEqual(coords.g11(), 1.2)); + EXPECT_TRUE(IsFieldEqual(coords.g22(), 2.3)); + EXPECT_TRUE(IsFieldEqual(coords.g33(), 3.4)); + EXPECT_TRUE(IsFieldEqual(coords.g12(), 4.5)); + EXPECT_TRUE(IsFieldEqual(coords.g13(), 5.6)); + EXPECT_TRUE(IsFieldEqual(coords.g23(), 6.7)); } TEST_F(CoordinatesTest, SetContravariantMetricTensor) { @@ -371,13 +362,12 @@ TEST_F(CoordinatesTest, SetContravariantMetricTensor) { coords.setContravariantMetricTensor(updated_metric_tensor); // Get values with getter and check they have been modified as expected - const auto g = coords.getContravariantMetricTensor(); - EXPECT_TRUE(IsFieldEqual(g.g11, 1.7)); - EXPECT_TRUE(IsFieldEqual(g.g22, 2.3)); - EXPECT_TRUE(IsFieldEqual(g.g33, 3.1)); - EXPECT_TRUE(IsFieldEqual(g.g12, 0.9)); - EXPECT_TRUE(IsFieldEqual(g.g13, 5.7)); - EXPECT_TRUE(IsFieldEqual(g.g23, 1.9)); + EXPECT_TRUE(IsFieldEqual(coords.g11(), 1.7)); + EXPECT_TRUE(IsFieldEqual(coords.g22(), 2.3)); + EXPECT_TRUE(IsFieldEqual(coords.g33(), 3.1)); + EXPECT_TRUE(IsFieldEqual(coords.g12(), 0.9)); + EXPECT_TRUE(IsFieldEqual(coords.g13(), 5.7)); + EXPECT_TRUE(IsFieldEqual(coords.g23(), 1.9)); } TEST_F(CoordinatesTest, GetCovariantMetricTensor) { From 5d331cfb9aa5188477c353e2400c7b3da434d17a Mon Sep 17 00:00:00 2001 From: tomc271 Date: Thu, 26 Oct 2023 12:13:47 +0100 Subject: [PATCH 118/491] Update to use new ContravariantMetricTensor getter methods in more places. --- tests/MMS/GBS/gbs.cxx | 52 ++++++++++++++++++------------------- tests/MMS/elm-pb/elm_pb.cxx | 31 ++++++++++++---------- 2 files changed, 42 insertions(+), 41 deletions(-) diff --git a/tests/MMS/GBS/gbs.cxx b/tests/MMS/GBS/gbs.cxx index ff572ad393..59dd02202b 100644 --- a/tests/MMS/GBS/gbs.cxx +++ b/tests/MMS/GBS/gbs.cxx @@ -295,17 +295,15 @@ int GBS::init(bool restarting) { dz4 = SQ(SQ(coords->dz)); SAVE_REPEAT(Ve); - - Coordinates::MetricTensor metric_tensor = coords->getContravariantMetricTensor(); - - output.write("dx = {:e}, dy = {:e}, dz = {:e}\n", coords->dx(2, 2), coords->dy(2, 2), - coords->dz); - output.write("g11 = {:e}, g22 = {:e}, g33 = {:e}\n", metric_tensor.g11(2, 2), - metric_tensor.g22(2, 2), metric_tensor.g33(2, 2)); - output.write("g12 = {:e}, g23 = {:e}\n", metric_tensor.g12(2, 2), metric_tensor.g23(2, 2)); - output.write("g_11 = {:e}, g_22 = {:e}, g_33 = {:e}\n", coords->g_11(2, 2), - coords->g_22(2, 2), coords->g_33(2, 2)); - output.write("g_12 = {:e}, g_23 = {:e}\n", coords->g_12(2, 2), coords->g_23(2, 2)); + + output.write("dx = {:e}, dy = {:e}, dz = {:e}\n", (coords->dx)(2, 2), + (coords->dy)(2, 2), coords->dz); + output.write("g11 = {:e}, g22 = {:e}, g33 = {:e}\n", coords->g11()(2, 2), + coords->g22()(2, 2), coords->g33()(2, 2)); + output.write("g12 = {:e}, g23 = {:e}\n", coords->g12()(2, 2), coords->g23()(2, 2)); + output.write("g_11 = {:e}, g_22 = {:e}, g_33 = {:e}\n", coords->g_11()(2, 2), + coords->g_22()(2, 2), coords->g_33()(2, 2)); + output.write("g_12 = {:e}, g_23 = {:e}\n", coords->g_12()(2, 2), coords->g_23()(2, 2)); std::shared_ptr gen = FieldFactory::get()->parse("source", Options::getRoot()->getSection("ne")); @@ -350,25 +348,25 @@ void GBS::LoadMetric(BoutReal Lnorm, BoutReal Bnorm) { sbp = -1.0; } - Coordinates::MetricTensor contravariant_components = coords->getContravariantMetricTensor(); - contravariant_components.g11 = SQ(Rxy * Bpxy); - contravariant_components.g22 = 1.0 / SQ(hthe); - contravariant_components.g33 = SQ(sinty) * contravariant_components.g11 + SQ(coords->Bxy) / contravariant_components.g11; - contravariant_components.g12 = 0.0; - contravariant_components.g13 = -sinty * contravariant_components.g11; - contravariant_components.g23 = -sbp * Btxy / (hthe * Bpxy * Rxy); - coords->setContravariantMetricTensor(contravariant_components); + const auto g11 = SQ(Rxy * Bpxy); + const auto g22 = 1.0 / SQ(hthe); + const auto g33 = SQ(sinty) * coords->g11() + SQ(coords->Bxy) / coords->g11(); + const auto g12 = 0.0; + const auto g13 = -sinty * coords->g11(); + const auto g23 = -sbp * Btxy / (hthe * Bpxy * Rxy); + coords->setContravariantMetricTensor( + ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); coords->J = hthe / Bpxy; - Coordinates::MetricTensor covariant_components; - covariant_components.g_11 = 1.0 / contravariant_components.g11 + SQ(sinty * Rxy); - covariant_components.g_22 = SQ(coords->Bxy * hthe / Bpxy); - covariant_components.g_33 = Rxy * Rxy; - covariant_components.g_12 = sbp * Btxy * hthe * sinty * Rxy / Bpxy; - covariant_components.g_13 = sinty * Rxy * Rxy; - covariant_components.g_23 = sbp * Btxy * hthe * Rxy / Bpxy; - coords->setCovariantMetricTensor(covariant_components); + const auto g_11 = 1.0 / coords->g11() + SQ(sinty * Rxy); + const auto g_22 = SQ(coords->Bxy * hthe / Bpxy); + const auto g_33 = Rxy * Rxy; + const auto g_12 = sbp * Btxy * hthe * sinty * Rxy / Bpxy; + const auto g_13 = sinty * Rxy * Rxy; + const auto g_23 = sbp * Btxy * hthe * Rxy / Bpxy; + coords->setCovariantMetricTensor( + CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); coords->geometry(); } diff --git a/tests/MMS/elm-pb/elm_pb.cxx b/tests/MMS/elm-pb/elm_pb.cxx index 512e045466..548efe12f6 100644 --- a/tests/MMS/elm-pb/elm_pb.cxx +++ b/tests/MMS/elm-pb/elm_pb.cxx @@ -416,24 +416,27 @@ class ELMpb : public PhysicsModel { dump.add(eta, "eta", 0); /**************** CALCULATE METRICS ******************/ - - Coordinates::MetricTensor metric_tensor = coords->getContravariantMetricTensor(); - metric_tensor.g11 = SQ(Rxy * Bpxy); - metric_tensor.g22 = 1.0 / SQ(hthe); - metric_tensor.g33 = SQ(I) * metric_tensor.g11 + SQ(B0) / metric_tensor.g11; - metric_tensor.g12 = 0.0; - metric_tensor.g13 = -I * metric_tensor.g11; - metric_tensor.g23 = -Btxy / (hthe * Bpxy * Rxy); + + const auto g11 = SQ(Rxy * Bpxy); + const auto g22 = 1.0 / SQ(hthe); + const auto g33 = SQ(I) * coords->g11() + SQ(B0) / coords->g11(); + const auto g12 = 0.0; + const auto g13 = -I * coords->g11(); + const auto g23 = -Btxy / (hthe * Bpxy * Rxy); + coords->setContravariantMetricTensor( + ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); coords->J = hthe / Bpxy; coords->Bxy = B0; - coords->g_11 = 1.0 / metric_tensor.g11 + (SQ(I * Rxy)); - coords->g_22 = SQ(B0 * hthe / Bpxy); - coords->g_33 = Rxy * Rxy; - coords->g_12 = Btxy * hthe * I * Rxy / Bpxy; - coords->g_13 = I * Rxy * Rxy; - coords->g_23 = Btxy * hthe * Rxy / Bpxy; + const auto g_11 = 1.0 / coords->g11() + (SQ(I * Rxy)); + const auto g_22 = SQ(B0 * hthe / Bpxy); + const auto g_33 = Rxy * Rxy; + const auto g_12 = Btxy * hthe * I * Rxy / Bpxy; + const auto g_13 = I * Rxy * Rxy; + const auto g_23 = Btxy * hthe * Rxy / Bpxy; + coords->setCovariantMetricTensor( + CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); coords->geometry(); // Calculate quantities from metric tensor From ce01880cc54b38a6b4cc0845409dac7d92bb2206 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Thu, 2 Nov 2023 17:35:46 +0000 Subject: [PATCH 119/491] Fixed refactoring of getAtLoc function. --- src/mesh/coordinates.cxx | 61 +++++++++++++++++++--------------------- 1 file changed, 29 insertions(+), 32 deletions(-) diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 324dd02d7e..a410f7adf3 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -324,21 +324,20 @@ void checkStaggeredGet(Mesh* mesh, const std::string& name, const std::string& s } // convenience function for repeated code -int getAtLoc(Mesh* mesh, const Coordinates::FieldMetric& var, const std::string& name, - const std::string& suffix, CELL_LOC location, BoutReal default_value = 0.) { +auto getAtLoc(Mesh* mesh, const std::string& name, const std::string& suffix, + CELL_LOC location, BoutReal default_value = 0.) { checkStaggeredGet(mesh, name, suffix); - auto field = mesh->get(name + suffix, default_value, false, location); - return 0; + return mesh->get(name + suffix, default_value, false, location); } -const Coordinates::FieldMetric +Coordinates::FieldMetric getAtLocAndFillGuards(Mesh* mesh, const Coordinates::FieldMetric& var, const std::string& name, const std::string& suffix, CELL_LOC location, BoutReal default_value, bool extrapolate_x, bool extrapolate_y, bool no_extra_interpolate, ParallelTransform* pt) { - auto ret = getAtLoc(mesh, var, name, suffix, location, default_value); + auto ret = getAtLoc(mesh, name, suffix, location, default_value); return interpolateAndExtrapolate(var, location, extrapolate_x, extrapolate_y, no_extra_interpolate, pt); } @@ -670,7 +669,7 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, : options["ZMAX"].withDefault(1.0); const auto default_dz = (zmax - zmin) * TWOPI / nz; - getAtLoc(mesh, dz, "dz", suffix, location, default_dz); + dz = getAtLoc(mesh, "dz", suffix, location, default_dz); } setParallelTransform(options); @@ -730,19 +729,15 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, if (std::all_of(begin(covariant_component_names), end(covariant_component_names), source_has_component)) { - FieldMetric old_g_11 = covariantMetricTensor.Getg_11(); // non-const - FieldMetric old_g_22 = covariantMetricTensor.Getg_22(); // non-const - FieldMetric old_g_33 = covariantMetricTensor.Getg_33(); // non-const - FieldMetric old_g_12 = covariantMetricTensor.Getg_12(); // non-const - FieldMetric old_g_13 = covariantMetricTensor.Getg_13(); // non-const - FieldMetric old_g_23 = covariantMetricTensor.Getg_23(); // non-const - - getAtLoc(mesh, old_g_11, "g_11", suffix, location); - getAtLoc(mesh, old_g_22, "g_22", suffix, location); - getAtLoc(mesh, old_g_33, "g_33", suffix, location); - getAtLoc(mesh, old_g_12, "g_12", suffix, location); - getAtLoc(mesh, old_g_13, "g_13", suffix, location); - getAtLoc(mesh, old_g_23, "g_23", suffix, location); + FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; + g_11 = getAtLoc(mesh, "g_11", suffix, location); + g_22 = getAtLoc(mesh, "g_22", suffix, location); + g_33 = getAtLoc(mesh, "g_33", suffix, location); + g_12 = getAtLoc(mesh, "g_12", suffix, location); + g_22 = getAtLoc(mesh, "g_13", suffix, location); + g_33 = getAtLoc(mesh, "g_23", suffix, location); + covariantMetricTensor.setCovariantMetricTensor( + CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); output_warn.write( "\tWARNING! Staggered covariant components of metric tensor set manually. " @@ -805,12 +800,8 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, // Attempt to read J from the grid file auto Jcalc = J; - if (getAtLoc(mesh, J, "J", suffix, location)) { - output_warn.write( - "\tWARNING: Jacobian 'J_{:s}' not found. Calculating from metric tensor\n", - suffix); - J = Jcalc; - } else { + try { + J = getAtLoc(mesh, "J", suffix, location); J = interpolateAndExtrapolate(J, location, extrapolate_x, extrapolate_y, false, transform.get()); @@ -819,6 +810,11 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, // Re-evaluate Bxy using new J Bxy = sqrt(g_22) / J; + } catch (BoutException) { + output_warn.write( + "\tWARNING: Jacobian 'J_{:s}' not found. Calculating from metric tensor\n", + suffix); + J = Jcalc; } // Check jacobian @@ -830,17 +826,18 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, // Attempt to read Bxy from the grid file auto Bcalc = Bxy; - if (getAtLoc(mesh, Bxy, "Bxy", suffix, location)) { + try { + Bxy = getAtLoc(mesh, "Bxy", suffix, location); + Bxy = interpolateAndExtrapolate(Bxy, location, extrapolate_x, extrapolate_y, false, + transform.get()); + + output_warn.write("\tMaximum difference in Bxy is %e\n", max(abs(Bxy - Bcalc))); + } catch (BoutException) { output_warn.write( "\tWARNING: Magnitude of B field 'Bxy_{:s}' not found. Calculating " " from metric tensor\n", suffix); Bxy = Bcalc; - } else { - Bxy = interpolateAndExtrapolate(Bxy, location, extrapolate_x, extrapolate_y, false, - transform.get()); - - output_warn.write("\tMaximum difference in Bxy is %e\n", max(abs(Bxy - Bcalc))); } // Check Bxy From cb2c4cfe28053371d2ca3950850c5a5dfe5f4ea2 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Thu, 2 Nov 2023 18:55:58 +0000 Subject: [PATCH 120/491] Fixed refactoring of getAtLocAndFillGuards function. --- src/mesh/coordinates.cxx | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index a410f7adf3..c91ad0b025 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -332,12 +332,12 @@ auto getAtLoc(Mesh* mesh, const std::string& name, const std::string& suffix, } Coordinates::FieldMetric -getAtLocAndFillGuards(Mesh* mesh, const Coordinates::FieldMetric& var, +getAtLocAndFillGuards(Mesh* mesh, const std::string& name, const std::string& suffix, CELL_LOC location, BoutReal default_value, bool extrapolate_x, bool extrapolate_y, bool no_extra_interpolate, ParallelTransform* pt) { - auto ret = getAtLoc(mesh, name, suffix, location, default_value); + auto var = getAtLoc(mesh, name, suffix, location, default_value); return interpolateAndExtrapolate(var, location, extrapolate_x, extrapolate_y, no_extra_interpolate, pt); } @@ -676,14 +676,14 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, dz = interpolateAndExtrapolate(dz, location, extrapolate_x, extrapolate_y, false, transform.get()); - dx = getAtLocAndFillGuards(mesh, dx, "dx", suffix, location, 1.0, extrapolate_x, + dx = getAtLocAndFillGuards(mesh, "dx", suffix, location, 1.0, extrapolate_x, extrapolate_y, false, transform.get()); if (mesh->periodicX) { communicate(dx); } - dy = getAtLocAndFillGuards(mesh, dy, "dy", suffix, location, 1.0, extrapolate_x, + dy = getAtLocAndFillGuards(mesh, "dy", suffix, location, 1.0, extrapolate_x, extrapolate_y, false, transform.get()); // grid data source has staggered fields, so read instead of interpolating @@ -692,22 +692,22 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, FieldMetric g11, g22, g33, g12, g13, g23; - g11 = getAtLocAndFillGuards(mesh, contravariantMetricTensor.Getg11(), "g11", suffix, + g11 = getAtLocAndFillGuards(mesh, "g11", suffix, location, 1.0, extrapolate_x, extrapolate_y, false, transform.get()); - g22 = getAtLocAndFillGuards(mesh, contravariantMetricTensor.Getg22(), "g22", suffix, + g22 = getAtLocAndFillGuards(mesh, "g22", suffix, location, 1.0, extrapolate_x, extrapolate_y, false, transform.get()); - g33 = getAtLocAndFillGuards(mesh, contravariantMetricTensor.Getg33(), "g33", suffix, + g33 = getAtLocAndFillGuards(mesh, "g33", suffix, location, 1.0, extrapolate_x, extrapolate_y, false, transform.get()); - g12 = getAtLocAndFillGuards(mesh, contravariantMetricTensor.Getg12(), "g12", suffix, + g12 = getAtLocAndFillGuards(mesh, "g12", suffix, location, 0.0, extrapolate_x, extrapolate_y, false, transform.get()); - g13 = getAtLocAndFillGuards(mesh, contravariantMetricTensor.Getg13(), "g13", suffix, + g13 = getAtLocAndFillGuards(mesh, "g13", suffix, location, 0.0, extrapolate_x, extrapolate_y, false, transform.get()); - g23 = getAtLocAndFillGuards(mesh, contravariantMetricTensor.Getg23(), "g23", suffix, + g23 = getAtLocAndFillGuards(mesh, "g23", suffix, location, 0.0, extrapolate_x, extrapolate_y, false, transform.get()); From 43f4edf8bf2834adf9fd5447b3aa108acaf71aec Mon Sep 17 00:00:00 2001 From: tomc271 Date: Thu, 2 Nov 2023 19:07:02 +0000 Subject: [PATCH 121/491] Both old and new versions of getAtLoc function. --- src/mesh/coordinates.cxx | 79 ++++++++++++++++++++-------------------- 1 file changed, 40 insertions(+), 39 deletions(-) diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index c91ad0b025..97c4aa9812 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -324,6 +324,15 @@ void checkStaggeredGet(Mesh* mesh, const std::string& name, const std::string& s } // convenience function for repeated code +int getAtLoc(Mesh* mesh, Coordinates::FieldMetric& var, const std::string& name, + const std::string& suffix, CELL_LOC location, BoutReal default_value = 0.) { + + checkStaggeredGet(mesh, name, suffix); + int result = mesh->get(var, name + suffix, default_value, false, location); + + return result; +} + auto getAtLoc(Mesh* mesh, const std::string& name, const std::string& suffix, CELL_LOC location, BoutReal default_value = 0.) { @@ -331,12 +340,12 @@ auto getAtLoc(Mesh* mesh, const std::string& name, const std::string& suffix, return mesh->get(name + suffix, default_value, false, location); } -Coordinates::FieldMetric -getAtLocAndFillGuards(Mesh* mesh, - const std::string& name, const std::string& suffix, - CELL_LOC location, BoutReal default_value, bool extrapolate_x, - bool extrapolate_y, bool no_extra_interpolate, - ParallelTransform* pt) { +Coordinates::FieldMetric getAtLocAndFillGuards(Mesh* mesh, const std::string& name, + const std::string& suffix, + CELL_LOC location, BoutReal default_value, + bool extrapolate_x, bool extrapolate_y, + bool no_extra_interpolate, + ParallelTransform* pt) { auto var = getAtLoc(mesh, name, suffix, location, default_value); return interpolateAndExtrapolate(var, location, extrapolate_x, extrapolate_y, no_extra_interpolate, pt); @@ -669,7 +678,7 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, : options["ZMAX"].withDefault(1.0); const auto default_dz = (zmax - zmin) * TWOPI / nz; - dz = getAtLoc(mesh, "dz", suffix, location, default_dz); + getAtLoc(mesh, dz, "dz", suffix, location, default_dz); } setParallelTransform(options); @@ -692,24 +701,18 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, FieldMetric g11, g22, g33, g12, g13, g23; - g11 = getAtLocAndFillGuards(mesh, "g11", suffix, - location, 1.0, extrapolate_x, extrapolate_y, false, - transform.get()); - g22 = getAtLocAndFillGuards(mesh, "g22", suffix, - location, 1.0, extrapolate_x, extrapolate_y, false, - transform.get()); - g33 = getAtLocAndFillGuards(mesh, "g33", suffix, - location, 1.0, extrapolate_x, extrapolate_y, false, - transform.get()); - g12 = getAtLocAndFillGuards(mesh, "g12", suffix, - location, 0.0, extrapolate_x, extrapolate_y, false, - transform.get()); - g13 = getAtLocAndFillGuards(mesh, "g13", suffix, - location, 0.0, extrapolate_x, extrapolate_y, false, - transform.get()); - g23 = getAtLocAndFillGuards(mesh, "g23", suffix, - location, 0.0, extrapolate_x, extrapolate_y, false, - transform.get()); + g11 = getAtLocAndFillGuards(mesh, "g11", suffix, location, 1.0, extrapolate_x, + extrapolate_y, false, transform.get()); + g22 = getAtLocAndFillGuards(mesh, "g22", suffix, location, 1.0, extrapolate_x, + extrapolate_y, false, transform.get()); + g33 = getAtLocAndFillGuards(mesh, "g33", suffix, location, 1.0, extrapolate_x, + extrapolate_y, false, transform.get()); + g12 = getAtLocAndFillGuards(mesh, "g12", suffix, location, 0.0, extrapolate_x, + extrapolate_y, false, transform.get()); + g13 = getAtLocAndFillGuards(mesh, "g13", suffix, location, 0.0, extrapolate_x, + extrapolate_y, false, transform.get()); + g23 = getAtLocAndFillGuards(mesh, "g23", suffix, location, 0.0, extrapolate_x, + extrapolate_y, false, transform.get()); setContravariantMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); @@ -800,8 +803,12 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, // Attempt to read J from the grid file auto Jcalc = J; - try { - J = getAtLoc(mesh, "J", suffix, location); + if (getAtLoc(mesh, J, "J", suffix, location)) { + output_warn.write( + "\tWARNING: Jacobian 'J_{:s}' not found. Calculating from metric tensor\n", + suffix); + J = Jcalc; + } else { J = interpolateAndExtrapolate(J, location, extrapolate_x, extrapolate_y, false, transform.get()); @@ -810,11 +817,6 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, // Re-evaluate Bxy using new J Bxy = sqrt(g_22) / J; - } catch (BoutException) { - output_warn.write( - "\tWARNING: Jacobian 'J_{:s}' not found. Calculating from metric tensor\n", - suffix); - J = Jcalc; } // Check jacobian @@ -826,18 +828,17 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, // Attempt to read Bxy from the grid file auto Bcalc = Bxy; - try { - Bxy = getAtLoc(mesh, "Bxy", suffix, location); - Bxy = interpolateAndExtrapolate(Bxy, location, extrapolate_x, extrapolate_y, false, - transform.get()); - - output_warn.write("\tMaximum difference in Bxy is %e\n", max(abs(Bxy - Bcalc))); - } catch (BoutException) { + if (getAtLoc(mesh, Bxy, "Bxy", suffix, location)) { output_warn.write( "\tWARNING: Magnitude of B field 'Bxy_{:s}' not found. Calculating " " from metric tensor\n", suffix); Bxy = Bcalc; + } else { + Bxy = interpolateAndExtrapolate(Bxy, location, extrapolate_x, extrapolate_y, false, + transform.get()); + + output_warn.write("\tMaximum difference in Bxy is %e\n", max(abs(Bxy - Bcalc))); } // Check Bxy From c6c00a7d3982aaca75d26bde01e5cc6d8e0c9c4c Mon Sep 17 00:00:00 2001 From: tomc271 Date: Thu, 2 Nov 2023 20:15:11 +0000 Subject: [PATCH 122/491] Uncomment code. [no ci] --- src/mesh/coordinates.cxx | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 97c4aa9812..7fa1e0a59f 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -502,20 +502,13 @@ Coordinates::Coordinates(Mesh* mesh, Options* options) if (std::all_of(begin(covariant_component_names), end(covariant_component_names), source_has_component)) { - FieldMetric old_g_11 = covariantMetricTensor.Getg_11(); // non-const - FieldMetric old_g_22 = covariantMetricTensor.Getg_22(); // non-const - FieldMetric old_g_33 = covariantMetricTensor.Getg_33(); // non-const - FieldMetric old_g_12 = covariantMetricTensor.Getg_12(); // non-const - FieldMetric old_g_13 = covariantMetricTensor.Getg_13(); // non-const - FieldMetric old_g_23 = covariantMetricTensor.Getg_23(); // non-const - FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; - // g_11 = getUnaligned(old_g_11, "g_11", 1.0); - // g_22 = getUnaligned(old_g_22, "g_22", 1.0); - // g_33 = getUnaligned(old_g_33, "g_33", 1.0); - // g_12 = getUnaligned(old_g_12, "g_12", 0.0); - // g_13 = getUnaligned(old_g_13, "g_13", 0.0); - // g_23 = getUnaligned(old_g_23, "g_23", 0.0); + g_11 = getUnaligned("g_11", 1.0); + g_22 = getUnaligned("g_22", 1.0); + g_33 = getUnaligned("g_33", 1.0); + g_12 = getUnaligned("g_12", 0.0); + g_13 = getUnaligned("g_13", 0.0); + g_23 = getUnaligned("g_23", 0.0); covariantMetricTensor.setCovariantMetricTensor( CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); From df0ef6f60950bce04b4f54046a02dc56144b2ba9 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Mon, 6 Nov 2023 10:51:07 +0000 Subject: [PATCH 123/491] Rename files to use cxx extension rather than cpp. --- CMakeLists.txt | 8 ++++---- ...iantMetricTensor.hxx => contravariantMetricTensor.hxx} | 2 +- include/bout/coordinates.hxx | 4 ++-- ...ovariantMetricTensor.hxx => covariantMetricTensor.hxx} | 0 ...iantMetricTensor.cpp => contravariantMetricTensor.cxx} | 4 ++-- src/mesh/coordinates.cxx | 2 +- src/mesh/coordinates_accessor.cxx | 2 +- ...ovariantMetricTensor.cpp => covariantMetricTensor.cxx} | 4 ++-- 8 files changed, 13 insertions(+), 13 deletions(-) rename include/bout/{ContravariantMetricTensor.hxx => contravariantMetricTensor.hxx} (97%) rename include/bout/{CovariantMetricTensor.hxx => covariantMetricTensor.hxx} (100%) rename src/mesh/{ContravariantMetricTensor.cpp => contravariantMetricTensor.cxx} (98%) rename src/mesh/{CovariantMetricTensor.cpp => covariantMetricTensor.cxx} (98%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 676902361d..9912239bf5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -354,10 +354,10 @@ set(BOUT_SOURCES ./src/sys/utils.cxx ${CMAKE_CURRENT_BINARY_DIR}/include/bout/revision.hxx ${CMAKE_CURRENT_BINARY_DIR}/include/bout/version.hxx - include/bout/ContravariantMetricTensor.hxx - src/mesh/ContravariantMetricTensor.cpp - include/bout/CovariantMetricTensor.hxx - src/mesh/CovariantMetricTensor.cpp + include/bout/contravariantMetricTensor.hxx + src/mesh/contravariantMetricTensor.cxx + include/bout/covariantMetricTensor.hxx + src/mesh/covariantMetricTensor.cxx ) diff --git a/include/bout/ContravariantMetricTensor.hxx b/include/bout/contravariantMetricTensor.hxx similarity index 97% rename from include/bout/ContravariantMetricTensor.hxx rename to include/bout/contravariantMetricTensor.hxx index eeef9621f2..975796c31b 100644 --- a/include/bout/ContravariantMetricTensor.hxx +++ b/include/bout/contravariantMetricTensor.hxx @@ -2,7 +2,7 @@ #ifndef BOUT_CONTRAVARIANTMETRICTENSOR_HXX #define BOUT_CONTRAVARIANTMETRICTENSOR_HXX -#include "CovariantMetricTensor.hxx" +#include "covariantMetricTensor.hxx" #include "bout/field2d.hxx" #include "bout/field3d.hxx" #include "bout/paralleltransform.hxx" diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index 776f2b145a..f5356e78a2 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -33,8 +33,8 @@ #ifndef BOUT_COORDINATES_H #define BOUT_COORDINATES_H -#include "ContravariantMetricTensor.hxx" -#include "CovariantMetricTensor.hxx" +#include "contravariantMetricTensor.hxx" +#include "covariantMetricTensor.hxx" #include "bout/field2d.hxx" #include "bout/field3d.hxx" #include "bout/paralleltransform.hxx" diff --git a/include/bout/CovariantMetricTensor.hxx b/include/bout/covariantMetricTensor.hxx similarity index 100% rename from include/bout/CovariantMetricTensor.hxx rename to include/bout/covariantMetricTensor.hxx diff --git a/src/mesh/ContravariantMetricTensor.cpp b/src/mesh/contravariantMetricTensor.cxx similarity index 98% rename from src/mesh/ContravariantMetricTensor.cpp rename to src/mesh/contravariantMetricTensor.cxx index 0108cc6465..c3818e2198 100644 --- a/src/mesh/ContravariantMetricTensor.cpp +++ b/src/mesh/contravariantMetricTensor.cxx @@ -1,6 +1,6 @@ -#include "bout/ContravariantMetricTensor.hxx" -#include "bout/CovariantMetricTensor.hxx" +#include "bout/contravariantMetricTensor.hxx" +#include "bout/covariantMetricTensor.hxx" ContravariantMetricTensor::ContravariantMetricTensor( const FieldMetric& g11, const FieldMetric& g22, const FieldMetric& g33, diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 7fa1e0a59f..32cefdbfd3 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -19,7 +19,7 @@ #include "parallel/fci.hxx" #include "parallel/shiftedmetricinterp.hxx" -#include "bout/ContravariantMetricTensor.hxx" +#include "bout/contravariantMetricTensor.hxx" // use anonymous namespace so this utility function is not available outside this file namespace { diff --git a/src/mesh/coordinates_accessor.cxx b/src/mesh/coordinates_accessor.cxx index 1acee91cb0..be885a0d4e 100644 --- a/src/mesh/coordinates_accessor.cxx +++ b/src/mesh/coordinates_accessor.cxx @@ -1,6 +1,6 @@ #include "bout/coordinates_accessor.hxx" -#include "bout/ContravariantMetricTensor.hxx" +#include "bout/contravariantMetricTensor.hxx" #include "bout/mesh.hxx" #include diff --git a/src/mesh/CovariantMetricTensor.cpp b/src/mesh/covariantMetricTensor.cxx similarity index 98% rename from src/mesh/CovariantMetricTensor.cpp rename to src/mesh/covariantMetricTensor.cxx index 1ecaa75ef3..c39cb4afeb 100644 --- a/src/mesh/CovariantMetricTensor.cpp +++ b/src/mesh/covariantMetricTensor.cxx @@ -1,6 +1,6 @@ -#include "bout/CovariantMetricTensor.hxx" -#include "bout/ContravariantMetricTensor.hxx" +#include "bout/covariantMetricTensor.hxx" +#include "bout/contravariantMetricTensor.hxx" #include "bout/coordinates.hxx" #include "bout/output.hxx" From ca4ce58de7a8ee4934417819684dd0ec18a302b8 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Mon, 6 Nov 2023 14:24:54 +0000 Subject: [PATCH 124/491] Add base class MetricTensor to DRY up code. --- CMakeLists.txt | 2 + include/bout/bout_types.hxx | 4 + include/bout/contravariantMetricTensor.hxx | 35 +- include/bout/coordinates.hxx | 4 +- include/bout/covariantMetricTensor.hxx | 32 +- include/bout/metricTensor.hxx | 49 +++ src/mesh/contravariantMetricTensor.cxx | 167 ++------- src/mesh/coordinates.cxx | 344 +++++++++--------- src/mesh/covariantMetricTensor.cxx | 172 ++------- src/mesh/metricTensor.cxx | 107 ++++++ tests/MMS/GBS/gbs.cxx | 4 +- tests/MMS/diffusion/diffusion.cxx | 4 +- tests/MMS/diffusion2/diffusion.cxx | 4 +- tests/MMS/elm-pb/elm_pb.cxx | 4 +- tests/MMS/spatial/diffusion/diffusion.cxx | 4 +- tests/MMS/tokamak/tokamak.cxx | 4 +- tests/MMS/wave-1d/wave.cxx | 4 +- .../test-drift-instability/2fluid.cxx | 4 +- .../test-interchange-instability/2fluid.cxx | 4 +- .../integrated/test-laplacexy/loadmetric.cxx | 4 +- .../test-laplacexz/test-laplacexz.cxx | 4 +- tests/unit/mesh/test_coordinates.cxx | 4 +- 22 files changed, 423 insertions(+), 541 deletions(-) create mode 100644 include/bout/metricTensor.hxx create mode 100644 src/mesh/metricTensor.cxx diff --git a/CMakeLists.txt b/CMakeLists.txt index 9912239bf5..959a39dfdd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -358,6 +358,8 @@ set(BOUT_SOURCES src/mesh/contravariantMetricTensor.cxx include/bout/covariantMetricTensor.hxx src/mesh/covariantMetricTensor.cxx + src/mesh/metricTensor.cxx + include/bout/metricTensor.hxx ) diff --git a/include/bout/bout_types.hxx b/include/bout/bout_types.hxx index c1f06fca7c..8a9cd1d608 100644 --- a/include/bout/bout_types.hxx +++ b/include/bout/bout_types.hxx @@ -46,6 +46,10 @@ constexpr CELL_LOC CELL_YLOW = CELL_LOC::ylow; constexpr CELL_LOC CELL_ZLOW = CELL_LOC::zlow; constexpr CELL_LOC CELL_VSHIFT = CELL_LOC::vshift; +enum class METRIC_TYPE { contravariant, covariant }; +constexpr METRIC_TYPE TYPE_CONTRAVARIANT = METRIC_TYPE::contravariant; +constexpr METRIC_TYPE TYPE_COVARIANT = METRIC_TYPE::covariant; + std::string toString(CELL_LOC location); CELL_LOC CELL_LOCFromString(const std::string& location_string); diff --git a/include/bout/contravariantMetricTensor.hxx b/include/bout/contravariantMetricTensor.hxx index 975796c31b..4c9225a6b5 100644 --- a/include/bout/contravariantMetricTensor.hxx +++ b/include/bout/contravariantMetricTensor.hxx @@ -3,51 +3,20 @@ #define BOUT_CONTRAVARIANTMETRICTENSOR_HXX #include "covariantMetricTensor.hxx" -#include "bout/field2d.hxx" -#include "bout/field3d.hxx" -#include "bout/paralleltransform.hxx" -#include "bout/utils.hxx" -#include +#include "metricTensor.hxx" -class ContravariantMetricTensor { +class ContravariantMetricTensor : public MetricTensor { public: -#if BOUT_USE_METRIC_3D - using FieldMetric = Field -#else - using FieldMetric = Field2D; -#endif - ContravariantMetricTensor(const FieldMetric& g11, const FieldMetric& g22, const FieldMetric& g33, const FieldMetric& g12, const FieldMetric& g13, const FieldMetric& g23); - ContravariantMetricTensor(const BoutReal g11, const BoutReal g22, const BoutReal g33, const BoutReal g12, const BoutReal g13, const BoutReal g23, Mesh* mesh); - - // check that contravariant tensors are positive (if expected) and finite (always) - void checkContravariant(int ystart); - - const FieldMetric& Getg11() const; - const FieldMetric& Getg22() const; - const FieldMetric& Getg33() const; - const FieldMetric& Getg12() const; - const FieldMetric& Getg13() const; - const FieldMetric& Getg23() const; - - void setContravariantMetricTensor(const ContravariantMetricTensor& metric_tensor); - - void Allocate(); - - void setLocation(const CELL_LOC location); - /// Invert covariant metric to get contravariant components void calcContravariant(CovariantMetricTensor covariantMetricTensor, CELL_LOC location, const std::string& region = "RGN_ALL"); - -private: - FieldMetric g11, g22, g33, g12, g13, g23; }; #endif //BOUT_CONTRAVARIANTMETRICTENSOR_HXX diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index f5356e78a2..d5b5499f5e 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -118,10 +118,10 @@ public: const FieldMetric& g13() const; const FieldMetric& g23() const; - void setContravariantMetricTensor(ContravariantMetricTensor metric_tensor, + void setMetricTensor(ContravariantMetricTensor metric_tensor, const std::string& region = "RGN_ALL"); - void setCovariantMetricTensor(CovariantMetricTensor metric_tensor); + void setMetricTensor(CovariantMetricTensor metric_tensor); /// Christoffel symbol of the second kind (connection coefficients) FieldMetric G1_11, G1_22, G1_33, G1_12, G1_13, G1_23; diff --git a/include/bout/covariantMetricTensor.hxx b/include/bout/covariantMetricTensor.hxx index d80e22a398..25d80cc01f 100644 --- a/include/bout/covariantMetricTensor.hxx +++ b/include/bout/covariantMetricTensor.hxx @@ -2,49 +2,21 @@ #ifndef BOUT_COVARIANTMETRICTENSOR_HXX #define BOUT_COVARIANTMETRICTENSOR_HXX -#include "field2d.hxx" -#include "bout/field3d.hxx" +#include "metricTensor.hxx" class ContravariantMetricTensor; -class CovariantMetricTensor { +class CovariantMetricTensor : public MetricTensor { public: -#if BOUT_USE_METRIC_3D - using FieldMetric = Field -#else - using FieldMetric = Field2D; -#endif - CovariantMetricTensor(const FieldMetric& g_11, const FieldMetric& g_22, const FieldMetric& g_33, const FieldMetric& g_12, const FieldMetric& g_13, const FieldMetric& g_23); - CovariantMetricTensor(const BoutReal g_11, const BoutReal g_22, const BoutReal g_33, const BoutReal g_12, const BoutReal g_13, const BoutReal g_23, Mesh* mesh); - - // check that covariant tensors are positive (if expected) and finite (always) - void checkCovariant(int ystart); - - const FieldMetric& Getg_11() const; - const FieldMetric& Getg_22() const; - const FieldMetric& Getg_33() const; - const FieldMetric& Getg_12() const; - const FieldMetric& Getg_13() const; - const FieldMetric& Getg_23() const; - - void setCovariantMetricTensor(const CovariantMetricTensor& metric_tensor); - - void Allocate(); - - void setLocation(const CELL_LOC location); - /// Invert contravariant metric to get covariant components void calcCovariant(ContravariantMetricTensor contravariantMetricTensor, CELL_LOC location, const std::string& region = "RGN_ALL"); - -private: - FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; }; #endif //BOUT_COVARIANTMETRICTENSOR_HXX diff --git a/include/bout/metricTensor.hxx b/include/bout/metricTensor.hxx new file mode 100644 index 0000000000..aaeecf0b81 --- /dev/null +++ b/include/bout/metricTensor.hxx @@ -0,0 +1,49 @@ + +#ifndef BOUT_METRICTENSOR_HXX +#define BOUT_METRICTENSOR_HXX + +#include "bout/field2d.hxx" +#include "bout/field3d.hxx" +//#include "bout/paralleltransform.hxx" +//#include "bout/utils.hxx" +#include + +class MetricTensor { + +public: +#if BOUT_USE_METRIC_3D + using FieldMetric = Field +#else + using FieldMetric = Field2D; +#endif + + MetricTensor(); + MetricTensor(const FieldMetric& g11, const FieldMetric& g22, const FieldMetric& g33, + const FieldMetric& g12, const FieldMetric& g13, const FieldMetric& g23); + + MetricTensor(const BoutReal g11, const BoutReal g22, const BoutReal g33, + const BoutReal g12, const BoutReal g13, const BoutReal g23, Mesh* mesh); + + // check that tensors are positive (if expected) and finite (always) + void check(int ystart); + + const FieldMetric& Getg11() const; + const FieldMetric& Getg22() const; + const FieldMetric& Getg33() const; + const FieldMetric& Getg12() const; + const FieldMetric& Getg13() const; + const FieldMetric& Getg23() const; + + void setMetricTensor(const MetricTensor& metric_tensor); + + void Allocate(); + + void setLocation(const CELL_LOC location); + +// [[maybe_unused]] virtual METRIC_TYPE MetricType() = 0; + +protected: + FieldMetric g11, g22, g33, g12, g13, g23; +}; + +#endif //BOUT_METRICTENSOR_HXX diff --git a/src/mesh/contravariantMetricTensor.cxx b/src/mesh/contravariantMetricTensor.cxx index c3818e2198..91ce7ec421 100644 --- a/src/mesh/contravariantMetricTensor.cxx +++ b/src/mesh/contravariantMetricTensor.cxx @@ -1,124 +1,19 @@ #include "bout/contravariantMetricTensor.hxx" #include "bout/covariantMetricTensor.hxx" +#include "bout/output.hxx" ContravariantMetricTensor::ContravariantMetricTensor( const FieldMetric& g11, const FieldMetric& g22, const FieldMetric& g33, const FieldMetric& g12, const FieldMetric& g13, const FieldMetric& g23) - : g11(std::move(g11)), g22(std::move(g22)), g33(std::move(g33)), g12(std::move(g12)), - g13(std::move(g13)), g23(std::move(g23)) { - - Allocate(); // Make sure metric elements are allocated // ; TODO: Required? -} + : MetricTensor::MetricTensor(g11, g22, g33, g12, g13, g23) {} ContravariantMetricTensor::ContravariantMetricTensor( const BoutReal g11, const BoutReal g22, const BoutReal g33, const BoutReal g12, const BoutReal g13, const BoutReal g23, Mesh* mesh) - : g11(g11, mesh), g22(g22, mesh), g33(g33, mesh), g12(g12, mesh), g13(g13, mesh), - g23(g23, mesh) { - - Allocate(); // Make sure metric elements are allocated // ; TODO: Required? -} - -const ContravariantMetricTensor::FieldMetric& ContravariantMetricTensor::Getg11() const { - return g11; -} -const ContravariantMetricTensor::FieldMetric& ContravariantMetricTensor::Getg22() const { - return g22; -} -const ContravariantMetricTensor::FieldMetric& ContravariantMetricTensor::Getg33() const { - return g33; -} -const ContravariantMetricTensor::FieldMetric& ContravariantMetricTensor::Getg12() const { - return g12; -} -const ContravariantMetricTensor::FieldMetric& ContravariantMetricTensor::Getg13() const { - return g13; -} -const ContravariantMetricTensor::FieldMetric& ContravariantMetricTensor::Getg23() const { - return g23; -} - -void ContravariantMetricTensor::setContravariantMetricTensor( - const ContravariantMetricTensor& metric_tensor) { - - g11 = metric_tensor.Getg11(); - g22 = metric_tensor.Getg22(); - g33 = metric_tensor.Getg33(); - g12 = metric_tensor.Getg12(); - g13 = metric_tensor.Getg13(); - g23 = metric_tensor.Getg23(); -} + : MetricTensor::MetricTensor(g11, g22, g33, g12, g13, g23, mesh) {} -void ContravariantMetricTensor::checkContravariant(int ystart) { - // Diagonal metric components should be finite - bout::checkFinite(g11, "g11", "RGN_NOCORNERS"); - bout::checkFinite(g22, "g22", "RGN_NOCORNERS"); - bout::checkFinite(g33, "g33", "RGN_NOCORNERS"); - if (g11.hasParallelSlices() && &g11.ynext(1) != &g11) { - for (int dy = 1; dy <= ystart; ++dy) { - for (const auto sign : {1, -1}) { - bout::checkFinite(g11.ynext(sign * dy), "g11.ynext", - fmt::format("RGN_YPAR_{:+d}", sign * dy)); - bout::checkFinite(g22.ynext(sign * dy), "g22.ynext", - fmt::format("RGN_YPAR_{:+d}", sign * dy)); - bout::checkFinite(g33.ynext(sign * dy), "g33.ynext", - fmt::format("RGN_YPAR_{:+d}", sign * dy)); - } - } - } - // Diagonal metric components should be positive - bout::checkPositive(g11, "g11", "RGN_NOCORNERS"); - bout::checkPositive(g22, "g22", "RGN_NOCORNERS"); - bout::checkPositive(g33, "g33", "RGN_NOCORNERS"); - if (g11.hasParallelSlices() && &g11.ynext(1) != &g11) { - for (int dy = 1; dy <= ystart; ++dy) { - for (const auto sign : {1, -1}) { - bout::checkPositive(g11.ynext(sign * dy), "g11.ynext", - fmt::format("RGN_YPAR_{:+d}", sign * dy)); - bout::checkPositive(g22.ynext(sign * dy), "g22.ynext", - fmt::format("RGN_YPAR_{:+d}", sign * dy)); - bout::checkPositive(g33.ynext(sign * dy), "g33.ynext", - fmt::format("RGN_YPAR_{:+d}", sign * dy)); - } - } - } - - // Off-diagonal metric components should be finite - bout::checkFinite(g12, "g12", "RGN_NOCORNERS"); - bout::checkFinite(g13, "g13", "RGN_NOCORNERS"); - bout::checkFinite(g23, "g23", "RGN_NOCORNERS"); - if (g23.hasParallelSlices() && &g23.ynext(1) != &g23) { - for (int dy = 1; dy <= ystart; ++dy) { - for (const auto sign : {1, -1}) { - bout::checkFinite(g12.ynext(sign * dy), "g12.ynext", - fmt::format("RGN_YPAR_{:+d}", sign * dy)); - bout::checkFinite(g13.ynext(sign * dy), "g13.ynext", - fmt::format("RGN_YPAR_{:+d}", sign * dy)); - bout::checkFinite(g23.ynext(sign * dy), "g23.ynext", - fmt::format("RGN_YPAR_{:+d}", sign * dy)); - } - } - } -} - -void ContravariantMetricTensor::Allocate() { // ; TODO: Required? - g11.allocate(); - g22.allocate(); - g33.allocate(); - g12.allocate(); - g13.allocate(); - g23.allocate(); -} - -void ContravariantMetricTensor::setLocation(const CELL_LOC location) { - g11.setLocation(location); - g22.setLocation(location); - g33.setLocation(location); - g12.setLocation(location); - g13.setLocation(location); - g23.setLocation(location); -} +//[[maybe_unused]] METRIC_TYPE MetricType() { return TYPE_CONTRAVARIANT; } void ContravariantMetricTensor::calcContravariant( CovariantMetricTensor covariantMetricTensor, CELL_LOC location, @@ -126,19 +21,19 @@ void ContravariantMetricTensor::calcContravariant( TRACE("ContravariantMetricTensor::calcContravariant"); - // Perform inversion of g_{ij} to get g^{ij} + // Perform inversion of g{ij} to get g^{ij} // NOTE: Currently this bit assumes that metric terms are Field2D objects auto a = Matrix(3, 3); - BOUT_FOR_SERIAL(i, covariantMetricTensor.Getg_11().getRegion(region)) { - a(0, 0) = covariantMetricTensor.Getg_11()[i]; - a(1, 1) = covariantMetricTensor.Getg_22()[i]; - a(2, 2) = covariantMetricTensor.Getg_33()[i]; + BOUT_FOR_SERIAL(i, covariantMetricTensor.Getg11().getRegion(region)) { + a(0, 0) = covariantMetricTensor.Getg11()[i]; + a(1, 1) = covariantMetricTensor.Getg22()[i]; + a(2, 2) = covariantMetricTensor.Getg33()[i]; - a(0, 1) = a(1, 0) = covariantMetricTensor.Getg_12()[i]; - a(1, 2) = a(2, 1) = covariantMetricTensor.Getg_23()[i]; - a(0, 2) = a(2, 0) = covariantMetricTensor.Getg_13()[i]; + a(0, 1) = a(1, 0) = covariantMetricTensor.Getg12()[i]; + a(1, 2) = a(2, 1) = covariantMetricTensor.Getg23()[i]; + a(0, 2) = a(2, 0) = covariantMetricTensor.Getg13()[i]; if (invert3x3(a)) { const auto error_message = "\tERROR: metric tensor is singular at ({:d}, {:d})\n"; @@ -158,30 +53,26 @@ void ContravariantMetricTensor::calcContravariant( setLocation(location); BoutReal maxerr; - maxerr = BOUTMAX(max(abs((covariantMetricTensor.Getg_11() * g11 - + covariantMetricTensor.Getg_12() * g12 - + covariantMetricTensor.Getg_13() * g13) - - 1)), - max(abs((covariantMetricTensor.Getg_12() * g12 - + covariantMetricTensor.Getg_22() * g22 - + covariantMetricTensor.Getg_23() * g23) - - 1)), - max(abs((covariantMetricTensor.Getg_13() * g13 - + covariantMetricTensor.Getg_23() * g23 - + covariantMetricTensor.Getg_33() * g33) - - 1))); + maxerr = BOUTMAX( + max(abs((covariantMetricTensor.Getg11() * g11 + covariantMetricTensor.Getg12() * g12 + + covariantMetricTensor.Getg13() * g13) + - 1)), + max(abs((covariantMetricTensor.Getg12() * g12 + covariantMetricTensor.Getg22() * g22 + + covariantMetricTensor.Getg23() * g23) + - 1)), + max(abs((covariantMetricTensor.Getg13() * g13 + covariantMetricTensor.Getg23() * g23 + + covariantMetricTensor.Getg33() * g33) + - 1))); output_info.write("\tMaximum error in diagonal inversion is {:e}\n", maxerr); - maxerr = BOUTMAX(max(abs(covariantMetricTensor.Getg_11() * g12 - + covariantMetricTensor.Getg_12() * g22 - + covariantMetricTensor.Getg_13() * g23)), - max(abs(covariantMetricTensor.Getg_11() * g13 - + covariantMetricTensor.Getg_12() * g23 - + covariantMetricTensor.Getg_13() * g33)), - max(abs(covariantMetricTensor.Getg_12() * g13 - + covariantMetricTensor.Getg_22() * g23 - + covariantMetricTensor.Getg_23() * g33))); + maxerr = BOUTMAX( + max(abs(covariantMetricTensor.Getg11() * g12 + covariantMetricTensor.Getg12() * g22 + + covariantMetricTensor.Getg13() * g23)), + max(abs(covariantMetricTensor.Getg11() * g13 + covariantMetricTensor.Getg12() * g23 + + covariantMetricTensor.Getg13() * g33)), + max(abs(covariantMetricTensor.Getg12() * g13 + covariantMetricTensor.Getg22() * g23 + + covariantMetricTensor.Getg23() * g33))); output_info.write("\tMaximum error in off-diagonal inversion is {:e}\n", maxerr); } diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 32cefdbfd3..da1960881a 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -485,7 +485,7 @@ Coordinates::Coordinates(Mesh* mesh, Options* options) g13 = getUnalignedAtLocation("g13", 0.0); g23 = getUnalignedAtLocation("g23", 0.0); - setContravariantMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); + setMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); // Check input metrics checkContravariant(); @@ -510,7 +510,7 @@ Coordinates::Coordinates(Mesh* mesh, Options* options) g_13 = getUnaligned("g_13", 0.0); g_23 = getUnaligned("g_23", 0.0); - covariantMetricTensor.setCovariantMetricTensor( + covariantMetricTensor.setMetricTensor( CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); output_warn.write("\tWARNING! Covariant components of metric tensor set manually. " @@ -539,20 +539,20 @@ Coordinates::Coordinates(Mesh* mesh, Options* options) // More robust to extrapolate derived quantities directly, rather than // deriving from extrapolated covariant metric components - g_11 = interpolateAndExtrapolate(covariantMetricTensor.Getg_11(), location, + g_11 = interpolateAndExtrapolate(covariantMetricTensor.Getg11(), location, extrapolate_x, extrapolate_y, false, transform.get()); - g_22 = interpolateAndExtrapolate(covariantMetricTensor.Getg_22(), location, + g_22 = interpolateAndExtrapolate(covariantMetricTensor.Getg22(), location, extrapolate_x, extrapolate_y, false, transform.get()); - g_33 = interpolateAndExtrapolate(covariantMetricTensor.Getg_33(), location, + g_33 = interpolateAndExtrapolate(covariantMetricTensor.Getg33(), location, extrapolate_x, extrapolate_y, false, transform.get()); - g_12 = interpolateAndExtrapolate(covariantMetricTensor.Getg_12(), location, + g_12 = interpolateAndExtrapolate(covariantMetricTensor.Getg12(), location, extrapolate_x, extrapolate_y, false, transform.get()); - g_13 = interpolateAndExtrapolate(covariantMetricTensor.Getg_13(), location, + g_13 = interpolateAndExtrapolate(covariantMetricTensor.Getg13(), location, extrapolate_x, extrapolate_y, false, transform.get()); - g_23 = interpolateAndExtrapolate(covariantMetricTensor.Getg_23(), location, + g_23 = interpolateAndExtrapolate(covariantMetricTensor.Getg23(), location, extrapolate_x, extrapolate_y, false, transform.get()); - covariantMetricTensor.setCovariantMetricTensor( + covariantMetricTensor.setMetricTensor( CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); // Check covariant metrics @@ -707,7 +707,7 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, g23 = getAtLocAndFillGuards(mesh, "g23", suffix, location, 0.0, extrapolate_x, extrapolate_y, false, transform.get()); - setContravariantMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); + setMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); // Check input metrics checkContravariant(); @@ -732,7 +732,7 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, g_12 = getAtLoc(mesh, "g_12", suffix, location); g_22 = getAtLoc(mesh, "g_13", suffix, location); g_33 = getAtLoc(mesh, "g_23", suffix, location); - covariantMetricTensor.setCovariantMetricTensor( + covariantMetricTensor.setMetricTensor( CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); output_warn.write( @@ -767,23 +767,23 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, // More robust to extrapolate derived quantities directly, rather than // deriving from extrapolated covariant metric components g_11 = - interpolateAndExtrapolate(covariantMetricTensor.Getg_11(), location, - extrapolate_x, extrapolate_y, false, transform.get()); + interpolateAndExtrapolate(covariantMetricTensor.Getg11(), location, extrapolate_x, + extrapolate_y, false, transform.get()); g_22 = - interpolateAndExtrapolate(covariantMetricTensor.Getg_22(), location, - extrapolate_x, extrapolate_y, false, transform.get()); + interpolateAndExtrapolate(covariantMetricTensor.Getg22(), location, extrapolate_x, + extrapolate_y, false, transform.get()); g_33 = - interpolateAndExtrapolate(covariantMetricTensor.Getg_33(), location, - extrapolate_x, extrapolate_y, false, transform.get()); + interpolateAndExtrapolate(covariantMetricTensor.Getg33(), location, extrapolate_x, + extrapolate_y, false, transform.get()); g_12 = - interpolateAndExtrapolate(covariantMetricTensor.Getg_12(), location, - extrapolate_x, extrapolate_y, false, transform.get()); + interpolateAndExtrapolate(covariantMetricTensor.Getg12(), location, extrapolate_x, + extrapolate_y, false, transform.get()); g_13 = - interpolateAndExtrapolate(covariantMetricTensor.Getg_13(), location, - extrapolate_x, extrapolate_y, false, transform.get()); + interpolateAndExtrapolate(covariantMetricTensor.Getg13(), location, extrapolate_x, + extrapolate_y, false, transform.get()); g_23 = - interpolateAndExtrapolate(covariantMetricTensor.Getg_23(), location, - extrapolate_x, extrapolate_y, false, transform.get()); + interpolateAndExtrapolate(covariantMetricTensor.Getg23(), location, extrapolate_x, + extrapolate_y, false, transform.get()); // Check covariant metrics checkCovariant(); @@ -891,20 +891,20 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, // more robust to interpolate and extrapolate derived quantities directly, // rather than deriving from interpolated/extrapolated covariant metric // components - g_11 = interpolateAndExtrapolate(covariantMetricTensor.Getg_11(), location, true, - true, false, transform.get()); - g_22 = interpolateAndExtrapolate(covariantMetricTensor.Getg_22(), location, true, - true, false, transform.get()); - g_33 = interpolateAndExtrapolate(covariantMetricTensor.Getg_33(), location, true, - true, false, transform.get()); - g_12 = interpolateAndExtrapolate(covariantMetricTensor.Getg_12(), location, true, - true, false, transform.get()); - g_13 = interpolateAndExtrapolate(covariantMetricTensor.Getg_13(), location, true, - true, false, transform.get()); - g_23 = interpolateAndExtrapolate(covariantMetricTensor.Getg_23(), location, true, - true, false, transform.get()); - - covariantMetricTensor.setCovariantMetricTensor( + g_11 = interpolateAndExtrapolate(covariantMetricTensor.Getg11(), location, true, true, + false, transform.get()); + g_22 = interpolateAndExtrapolate(covariantMetricTensor.Getg22(), location, true, true, + false, transform.get()); + g_33 = interpolateAndExtrapolate(covariantMetricTensor.Getg33(), location, true, true, + false, transform.get()); + g_12 = interpolateAndExtrapolate(covariantMetricTensor.Getg12(), location, true, true, + false, transform.get()); + g_13 = interpolateAndExtrapolate(covariantMetricTensor.Getg13(), location, true, true, + false, transform.get()); + g_23 = interpolateAndExtrapolate(covariantMetricTensor.Getg23(), location, true, true, + false, transform.get()); + + covariantMetricTensor.setMetricTensor( CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); // Check input metrics @@ -954,8 +954,7 @@ void Coordinates::interpolateAndExtrapolateContravariantMetricTensor( g23 = interpolateAndExtrapolate(coords_in->g23(), location, true, true, false, transform.get(), region); - setContravariantMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23), - region); + setMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23), region); } void Coordinates::outputVars(Options& output_options) { @@ -980,17 +979,17 @@ void Coordinates::outputVars(Options& output_options) { output_options["g23" + loc_string].force(contravariantMetricTensor.Getg23(), "Coordinates"); - output_options["g_11" + loc_string].force(covariantMetricTensor.Getg_11(), + output_options["g_11" + loc_string].force(covariantMetricTensor.Getg11(), "Coordinates"); - output_options["g_22" + loc_string].force(covariantMetricTensor.Getg_22(), + output_options["g_22" + loc_string].force(covariantMetricTensor.Getg22(), "Coordinates"); - output_options["g_33" + loc_string].force(covariantMetricTensor.Getg_33(), + output_options["g_33" + loc_string].force(covariantMetricTensor.Getg33(), "Coordinates"); - output_options["g_12" + loc_string].force(covariantMetricTensor.Getg_12(), + output_options["g_12" + loc_string].force(covariantMetricTensor.Getg12(), "Coordinates"); - output_options["g_13" + loc_string].force(covariantMetricTensor.Getg_13(), + output_options["g_13" + loc_string].force(covariantMetricTensor.Getg13(), "Coordinates"); - output_options["g_23" + loc_string].force(covariantMetricTensor.Getg_23(), + output_options["g_23" + loc_string].force(covariantMetricTensor.Getg23(), "Coordinates"); output_options["J" + loc_string].force(J, "Coordinates"); @@ -1025,10 +1024,10 @@ int Coordinates::geometry(bool recalculate_staggered, communicate(dx, dy, dz, contravariantMetricTensor.Getg11(), contravariantMetricTensor.Getg22(), contravariantMetricTensor.Getg33(), contravariantMetricTensor.Getg12(), contravariantMetricTensor.Getg13(), - contravariantMetricTensor.Getg23(), covariantMetricTensor.Getg_11(), - covariantMetricTensor.Getg_22(), covariantMetricTensor.Getg_33(), - covariantMetricTensor.Getg_12(), covariantMetricTensor.Getg_13(), - covariantMetricTensor.Getg_23(), J, Bxy); + contravariantMetricTensor.Getg23(), covariantMetricTensor.Getg11(), + covariantMetricTensor.Getg22(), covariantMetricTensor.Getg33(), + covariantMetricTensor.Getg12(), covariantMetricTensor.Getg13(), + covariantMetricTensor.Getg23(), J, Bxy); output_progress.write("Calculating differential geometry terms\n"); @@ -1244,149 +1243,146 @@ void Coordinates::CalculateChristoffelSymbols() { // Note: This calculation is completely general: metric // tensor can be 2D or 3D. For 2D, all DDZ terms are zero - G1_11 = 0.5 * contravariantMetricTensor.Getg11() * DDX(covariantMetricTensor.Getg_11()) + G1_11 = 0.5 * contravariantMetricTensor.Getg11() * DDX(covariantMetricTensor.Getg11()) + contravariantMetricTensor.Getg12() - * (DDX(covariantMetricTensor.Getg_12()) - - 0.5 * DDY(covariantMetricTensor.Getg_11())) + * (DDX(covariantMetricTensor.Getg12()) + - 0.5 * DDY(covariantMetricTensor.Getg11())) + contravariantMetricTensor.Getg13() - * (DDX(covariantMetricTensor.Getg_13()) - - 0.5 * DDZ(covariantMetricTensor.Getg_11())); - G1_22 = - contravariantMetricTensor.Getg11() - * (DDY(covariantMetricTensor.Getg_12()) - - 0.5 * DDX(covariantMetricTensor.Getg_22())) - + 0.5 * contravariantMetricTensor.Getg12() * DDY(covariantMetricTensor.Getg_22()) - + contravariantMetricTensor.Getg13() - * (DDY(covariantMetricTensor.Getg_23()) - - 0.5 * DDZ(covariantMetricTensor.Getg_22())); + * (DDX(covariantMetricTensor.Getg13()) + - 0.5 * DDZ(covariantMetricTensor.Getg11())); + G1_22 = contravariantMetricTensor.Getg11() + * (DDY(covariantMetricTensor.Getg12()) + - 0.5 * DDX(covariantMetricTensor.Getg22())) + + 0.5 * contravariantMetricTensor.Getg12() * DDY(covariantMetricTensor.Getg22()) + + contravariantMetricTensor.Getg13() + * (DDY(covariantMetricTensor.Getg23()) + - 0.5 * DDZ(covariantMetricTensor.Getg22())); G1_33 = contravariantMetricTensor.Getg11() - * (DDZ(covariantMetricTensor.Getg_13()) - - 0.5 * DDX(covariantMetricTensor.Getg_33())) + * (DDZ(covariantMetricTensor.Getg13()) + - 0.5 * DDX(covariantMetricTensor.Getg33())) + contravariantMetricTensor.Getg12() - * (DDZ(covariantMetricTensor.Getg_23()) - - 0.5 * DDY(covariantMetricTensor.Getg_33())) - + 0.5 * contravariantMetricTensor.Getg13() * DDZ(covariantMetricTensor.Getg_33()); + * (DDZ(covariantMetricTensor.Getg23()) + - 0.5 * DDY(covariantMetricTensor.Getg33())) + + 0.5 * contravariantMetricTensor.Getg13() * DDZ(covariantMetricTensor.Getg33()); G1_12 = - 0.5 * contravariantMetricTensor.Getg11() * DDY(covariantMetricTensor.Getg_11()) - + 0.5 * contravariantMetricTensor.Getg12() * DDX(covariantMetricTensor.Getg_22()) + 0.5 * contravariantMetricTensor.Getg11() * DDY(covariantMetricTensor.Getg11()) + + 0.5 * contravariantMetricTensor.Getg12() * DDX(covariantMetricTensor.Getg22()) + 0.5 * contravariantMetricTensor.Getg13() - * (DDY(covariantMetricTensor.Getg_13()) + DDX(covariantMetricTensor.Getg_23()) - - DDZ(covariantMetricTensor.Getg_12())); + * (DDY(covariantMetricTensor.Getg13()) + DDX(covariantMetricTensor.Getg23()) + - DDZ(covariantMetricTensor.Getg12())); G1_13 = - 0.5 * contravariantMetricTensor.Getg11() * DDZ(covariantMetricTensor.Getg_11()) + 0.5 * contravariantMetricTensor.Getg11() * DDZ(covariantMetricTensor.Getg11()) + 0.5 * contravariantMetricTensor.Getg12() - * (DDZ(covariantMetricTensor.Getg_12()) + DDX(covariantMetricTensor.Getg_23()) - - DDY(covariantMetricTensor.Getg_13())) - + 0.5 * contravariantMetricTensor.Getg13() * DDX(covariantMetricTensor.Getg_33()); + * (DDZ(covariantMetricTensor.Getg12()) + DDX(covariantMetricTensor.Getg23()) + - DDY(covariantMetricTensor.Getg13())) + + 0.5 * contravariantMetricTensor.Getg13() * DDX(covariantMetricTensor.Getg33()); G1_23 = 0.5 * contravariantMetricTensor.Getg11() - * (DDZ(covariantMetricTensor.Getg_12()) + DDY(covariantMetricTensor.Getg_13()) - - DDX(covariantMetricTensor.Getg_23())) + * (DDZ(covariantMetricTensor.Getg12()) + DDY(covariantMetricTensor.Getg13()) + - DDX(covariantMetricTensor.Getg23())) + 0.5 * contravariantMetricTensor.Getg12() - * (DDZ(covariantMetricTensor.Getg_22()) + DDY(covariantMetricTensor.Getg_23()) - - DDY(covariantMetricTensor.Getg_23())) + * (DDZ(covariantMetricTensor.Getg22()) + DDY(covariantMetricTensor.Getg23()) + - DDY(covariantMetricTensor.Getg23())) // + 0.5 *g13*(DDZ(g_32) + DDY(g_33) - DDZ(g_23)); // which equals - + 0.5 * contravariantMetricTensor.Getg13() * DDY(covariantMetricTensor.Getg_33()); + + 0.5 * contravariantMetricTensor.Getg13() * DDY(covariantMetricTensor.Getg33()); - G2_11 = 0.5 * contravariantMetricTensor.Getg12() * DDX(covariantMetricTensor.Getg_11()) + G2_11 = 0.5 * contravariantMetricTensor.Getg12() * DDX(covariantMetricTensor.Getg11()) + contravariantMetricTensor.Getg22() - * (DDX(covariantMetricTensor.Getg_12()) - - 0.5 * DDY(covariantMetricTensor.Getg_11())) + * (DDX(covariantMetricTensor.Getg12()) + - 0.5 * DDY(covariantMetricTensor.Getg11())) + contravariantMetricTensor.Getg23() - * (DDX(covariantMetricTensor.Getg_13()) - - 0.5 * DDZ(covariantMetricTensor.Getg_11())); - G2_22 = - contravariantMetricTensor.Getg12() - * (DDY(covariantMetricTensor.Getg_12()) - - 0.5 * DDX(covariantMetricTensor.Getg_22())) - + 0.5 * contravariantMetricTensor.Getg22() * DDY(covariantMetricTensor.Getg_22()) - + contravariantMetricTensor.Getg23() - * (DDY(contravariantMetricTensor.Getg23()) - - 0.5 * DDZ(covariantMetricTensor.Getg_22())); + * (DDX(covariantMetricTensor.Getg13()) + - 0.5 * DDZ(covariantMetricTensor.Getg11())); + G2_22 = contravariantMetricTensor.Getg12() + * (DDY(covariantMetricTensor.Getg12()) + - 0.5 * DDX(covariantMetricTensor.Getg22())) + + 0.5 * contravariantMetricTensor.Getg22() * DDY(covariantMetricTensor.Getg22()) + + contravariantMetricTensor.Getg23() + * (DDY(contravariantMetricTensor.Getg23()) + - 0.5 * DDZ(covariantMetricTensor.Getg22())); G2_33 = contravariantMetricTensor.Getg12() - * (DDZ(covariantMetricTensor.Getg_13()) - - 0.5 * DDX(covariantMetricTensor.Getg_33())) + * (DDZ(covariantMetricTensor.Getg13()) + - 0.5 * DDX(covariantMetricTensor.Getg33())) + contravariantMetricTensor.Getg22() - * (DDZ(covariantMetricTensor.Getg_23()) - - 0.5 * DDY(covariantMetricTensor.Getg_33())) - + 0.5 * contravariantMetricTensor.Getg23() * DDZ(covariantMetricTensor.Getg_33()); + * (DDZ(covariantMetricTensor.Getg23()) + - 0.5 * DDY(covariantMetricTensor.Getg33())) + + 0.5 * contravariantMetricTensor.Getg23() * DDZ(covariantMetricTensor.Getg33()); G2_12 = - 0.5 * contravariantMetricTensor.Getg12() * DDY(covariantMetricTensor.Getg_11()) - + 0.5 * contravariantMetricTensor.Getg22() * DDX(covariantMetricTensor.Getg_22()) + 0.5 * contravariantMetricTensor.Getg12() * DDY(covariantMetricTensor.Getg11()) + + 0.5 * contravariantMetricTensor.Getg22() * DDX(covariantMetricTensor.Getg22()) + 0.5 * contravariantMetricTensor.Getg23() - * (DDY(covariantMetricTensor.Getg_13()) + DDX(covariantMetricTensor.Getg_23()) - - DDZ(covariantMetricTensor.Getg_12())); + * (DDY(covariantMetricTensor.Getg13()) + DDX(covariantMetricTensor.Getg23()) + - DDZ(covariantMetricTensor.Getg12())); G2_13 = - // 0.5 *g21*(DDZ(covariantMetricTensor.Getg_11()) + DDX(covariantMetricTensor.Getg_13()) - DDX(covariantMetricTensor.Getg_13())) + // 0.5 *g21*(DDZ(covariantMetricTensor.Getg11()) + DDX(covariantMetricTensor.Getg13()) - DDX(covariantMetricTensor.Getg13())) // which equals 0.5 * contravariantMetricTensor.Getg12() - * (DDZ(covariantMetricTensor.Getg_11()) + DDX(covariantMetricTensor.Getg_13()) - - DDX(covariantMetricTensor.Getg_13())) - // + 0.5 *g22*(DDZ(covariantMetricTensor.Getg_21()) + DDX(covariantMetricTensor.Getg_23()) - DDY(covariantMetricTensor.Getg_13())) + * (DDZ(covariantMetricTensor.Getg11()) + DDX(covariantMetricTensor.Getg13()) + - DDX(covariantMetricTensor.Getg13())) + // + 0.5 *g22*(DDZ(covariantMetricTensor.Getg21()) + DDX(covariantMetricTensor.Getg23()) - DDY(covariantMetricTensor.Getg13())) // which equals + 0.5 * contravariantMetricTensor.Getg22() - * (DDZ(covariantMetricTensor.Getg_12()) + DDX(covariantMetricTensor.Getg_23()) - - DDY(covariantMetricTensor.Getg_13())) - // + 0.5 *g23*(DDZ(covariantMetricTensor.Getg_31()) + DDX(covariantMetricTensor.Getg_33()) - DDZ(g_13)); + * (DDZ(covariantMetricTensor.Getg12()) + DDX(covariantMetricTensor.Getg23()) + - DDY(covariantMetricTensor.Getg13())) + // + 0.5 *g23*(DDZ(covariantMetricTensor.Getg31()) + DDX(covariantMetricTensor.Getg33()) - DDZ(g_13)); // which equals - + 0.5 * contravariantMetricTensor.Getg23() * DDX(covariantMetricTensor.Getg_33()); + + 0.5 * contravariantMetricTensor.Getg23() * DDX(covariantMetricTensor.Getg33()); G2_23 = 0.5 * contravariantMetricTensor.Getg12() - * (DDZ(covariantMetricTensor.Getg_12()) + DDY(covariantMetricTensor.Getg_13()) - - DDX(covariantMetricTensor.Getg_23())) - + 0.5 * contravariantMetricTensor.Getg22() * DDZ(covariantMetricTensor.Getg_22()) - + 0.5 * contravariantMetricTensor.Getg23() * DDY(covariantMetricTensor.Getg_33()); + * (DDZ(covariantMetricTensor.Getg12()) + DDY(covariantMetricTensor.Getg13()) + - DDX(covariantMetricTensor.Getg23())) + + 0.5 * contravariantMetricTensor.Getg22() * DDZ(covariantMetricTensor.Getg22()) + + 0.5 * contravariantMetricTensor.Getg23() * DDY(covariantMetricTensor.Getg33()); - G3_11 = 0.5 * contravariantMetricTensor.Getg13() * DDX(covariantMetricTensor.Getg_11()) + G3_11 = 0.5 * contravariantMetricTensor.Getg13() * DDX(covariantMetricTensor.Getg11()) + contravariantMetricTensor.Getg23() - * (DDX(covariantMetricTensor.Getg_12()) - - 0.5 * DDY(covariantMetricTensor.Getg_11())) + * (DDX(covariantMetricTensor.Getg12()) + - 0.5 * DDY(covariantMetricTensor.Getg11())) + contravariantMetricTensor.Getg33() - * (DDX(covariantMetricTensor.Getg_13()) - - 0.5 * DDZ(covariantMetricTensor.Getg_11())); - G3_22 = - contravariantMetricTensor.Getg13() - * (DDY(covariantMetricTensor.Getg_12()) - - 0.5 * DDX(covariantMetricTensor.Getg_22())) - + 0.5 * contravariantMetricTensor.Getg23() * DDY(covariantMetricTensor.Getg_22()) - + contravariantMetricTensor.Getg33() - * (DDY(covariantMetricTensor.Getg_23()) - - 0.5 * DDZ(covariantMetricTensor.Getg_22())); + * (DDX(covariantMetricTensor.Getg13()) + - 0.5 * DDZ(covariantMetricTensor.Getg11())); + G3_22 = contravariantMetricTensor.Getg13() + * (DDY(covariantMetricTensor.Getg12()) + - 0.5 * DDX(covariantMetricTensor.Getg22())) + + 0.5 * contravariantMetricTensor.Getg23() * DDY(covariantMetricTensor.Getg22()) + + contravariantMetricTensor.Getg33() + * (DDY(covariantMetricTensor.Getg23()) + - 0.5 * DDZ(covariantMetricTensor.Getg22())); G3_33 = contravariantMetricTensor.Getg13() - * (DDZ(covariantMetricTensor.Getg_13()) - - 0.5 * DDX(covariantMetricTensor.Getg_33())) + * (DDZ(covariantMetricTensor.Getg13()) + - 0.5 * DDX(covariantMetricTensor.Getg33())) + contravariantMetricTensor.Getg23() - * (DDZ(covariantMetricTensor.Getg_23()) - - 0.5 * DDY(covariantMetricTensor.Getg_33())) - + 0.5 * contravariantMetricTensor.Getg33() * DDZ(covariantMetricTensor.Getg_33()); + * (DDZ(covariantMetricTensor.Getg23()) + - 0.5 * DDY(covariantMetricTensor.Getg33())) + + 0.5 * contravariantMetricTensor.Getg33() * DDZ(covariantMetricTensor.Getg33()); G3_12 = - // 0.5 *g31*(DDY(covariantMetricTensor.Getg_11()) + DDX(covariantMetricTensor.Getg_12()) - DDX(covariantMetricTensor.Getg_12())) + // 0.5 *g31*(DDY(covariantMetricTensor.Getg11()) + DDX(covariantMetricTensor.Getg12()) - DDX(covariantMetricTensor.Getg12())) // which equals to - 0.5 * contravariantMetricTensor.Getg13() * DDY(covariantMetricTensor.Getg_11()) - // + 0.5 *g32*(DDY(covariantMetricTensor.Getg_21()) + DDX(covariantMetricTensor.Getg_22()) - DDY(covariantMetricTensor.Getg_12())) + 0.5 * contravariantMetricTensor.Getg13() * DDY(covariantMetricTensor.Getg11()) + // + 0.5 *g32*(DDY(covariantMetricTensor.Getg21()) + DDX(covariantMetricTensor.Getg22()) - DDY(covariantMetricTensor.Getg12())) // which equals to - + 0.5 * contravariantMetricTensor.Getg23() * DDX(covariantMetricTensor.Getg_22()) - //+ 0.5 *g33*(DDY(covariantMetricTensor.Getg_31()) + DDX(covariantMetricTensor.Getg_32()) - DDZ(covariantMetricTensor.Getg_12())); + + 0.5 * contravariantMetricTensor.Getg23() * DDX(covariantMetricTensor.Getg22()) + //+ 0.5 *g33*(DDY(covariantMetricTensor.Getg31()) + DDX(covariantMetricTensor.Getg32()) - DDZ(covariantMetricTensor.Getg12())); // which equals to + 0.5 * contravariantMetricTensor.Getg33() - * (DDY(covariantMetricTensor.Getg_13()) + DDX(covariantMetricTensor.Getg_23()) - - DDZ(covariantMetricTensor.Getg_12())); + * (DDY(covariantMetricTensor.Getg13()) + DDX(covariantMetricTensor.Getg23()) + - DDZ(covariantMetricTensor.Getg12())); G3_13 = - 0.5 * contravariantMetricTensor.Getg13() * DDZ(covariantMetricTensor.Getg_11()) + 0.5 * contravariantMetricTensor.Getg13() * DDZ(covariantMetricTensor.Getg11()) + 0.5 * contravariantMetricTensor.Getg23() - * (DDZ(covariantMetricTensor.Getg_12()) + DDX(covariantMetricTensor.Getg_23()) - - DDY(covariantMetricTensor.Getg_13())) - + 0.5 * contravariantMetricTensor.Getg33() * DDX(covariantMetricTensor.Getg_33()); + * (DDZ(covariantMetricTensor.Getg12()) + DDX(covariantMetricTensor.Getg23()) + - DDY(covariantMetricTensor.Getg13())) + + 0.5 * contravariantMetricTensor.Getg33() * DDX(covariantMetricTensor.Getg33()); G3_23 = 0.5 * contravariantMetricTensor.Getg13() - * (DDZ(covariantMetricTensor.Getg_12()) + DDY(covariantMetricTensor.Getg_13()) - - DDX(covariantMetricTensor.Getg_23())) - + 0.5 * contravariantMetricTensor.Getg23() * DDZ(covariantMetricTensor.Getg_22()) - + 0.5 * contravariantMetricTensor.Getg33() * DDY(covariantMetricTensor.Getg_33()); + * (DDZ(covariantMetricTensor.Getg12()) + DDY(covariantMetricTensor.Getg13()) + - DDX(covariantMetricTensor.Getg23())) + + 0.5 * contravariantMetricTensor.Getg23() * DDZ(covariantMetricTensor.Getg22()) + + 0.5 * contravariantMetricTensor.Getg33() * DDY(covariantMetricTensor.Getg33()); } CovariantMetricTensor Coordinates::calcCovariant(const std::string& region) { @@ -1428,7 +1424,7 @@ int Coordinates::jacobian() { J = interpolateAndExtrapolate(J, location, extrapolate_x, extrapolate_y, false, transform.get()); - Bxy = sqrt(covariantMetricTensor.Getg_22()) / J; + Bxy = sqrt(covariantMetricTensor.Getg22()) / J; Bxy = interpolateAndExtrapolate(Bxy, location, extrapolate_x, extrapolate_y, false, transform.get()); @@ -1696,7 +1692,7 @@ Coordinates::FieldMetric Coordinates::Grad2_par2(const Field2D& f, CELL_LOC outl ASSERT1(location == outloc || (outloc == CELL_DEFAULT && location == f.getLocation())); auto result = Grad2_par2_DDY_invSg(outloc, method) * DDY(f, outloc, method) - + D2DY2(f, outloc, method) / covariantMetricTensor.Getg_22(); + + D2DY2(f, outloc, method) / covariantMetricTensor.Getg22(); return result; } @@ -1711,7 +1707,7 @@ Field3D Coordinates::Grad2_par2(const Field3D& f, CELL_LOC outloc, Field3D result = ::DDY(f, outloc, method); - Field3D r2 = D2DY2(f, outloc, method) / covariantMetricTensor.Getg_22(); + Field3D r2 = D2DY2(f, outloc, method) / covariantMetricTensor.Getg22(); result = Grad2_par2_DDY_invSg(outloc, method) * result + r2; @@ -1870,14 +1866,14 @@ FieldPerp Coordinates::Delp2(const FieldPerp& f, CELL_LOC outloc, bool useFFT) { Coordinates::FieldMetric Coordinates::Laplace_par(const Field2D& f, CELL_LOC outloc) { ASSERT1(location == outloc || outloc == CELL_DEFAULT); - return D2DY2(f, outloc) / covariantMetricTensor.Getg_22() - + DDY(J / covariantMetricTensor.Getg_22(), outloc) * DDY(f, outloc) / J; + return D2DY2(f, outloc) / covariantMetricTensor.Getg22() + + DDY(J / covariantMetricTensor.Getg22(), outloc) * DDY(f, outloc) / J; } Field3D Coordinates::Laplace_par(const Field3D& f, CELL_LOC outloc) { ASSERT1(location == outloc || outloc == CELL_DEFAULT); - return D2DY2(f, outloc) / covariantMetricTensor.Getg_22() - + DDY(J / covariantMetricTensor.Getg_22(), outloc) * ::DDY(f, outloc) / J; + return D2DY2(f, outloc) / covariantMetricTensor.Getg22() + + DDY(J / covariantMetricTensor.Getg22(), outloc) * ::DDY(f, outloc) / J; } // Full Laplacian operator on scalar field @@ -1953,9 +1949,9 @@ Field2D Coordinates::Laplace_perpXY([[maybe_unused]] const Field2D& A, const auto upper_y_avg = [&i](const auto& f) { return 0.5 * (f[i] + f[i.yp()]); }; const BoutReal upper_y_A = upper_y_avg(A); const BoutReal upper_y_J = upper_y_avg(J); - const BoutReal upper_y_g_22 = upper_y_avg(covariantMetricTensor.Getg_22()); - const BoutReal upper_y_g23 = upper_y_avg(covariantMetricTensor.Getg_23()); - const BoutReal upper_y_g_23 = upper_y_avg(covariantMetricTensor.Getg_23()); + const BoutReal upper_y_g_22 = upper_y_avg(covariantMetricTensor.Getg22()); + const BoutReal upper_y_g23 = upper_y_avg(covariantMetricTensor.Getg23()); + const BoutReal upper_y_g_23 = upper_y_avg(covariantMetricTensor.Getg23()); const BoutReal upper_y_dy = upper_y_avg(dy); const BoutReal upper_y_value = -upper_y_A * upper_y_J * upper_y_g23 * upper_y_g_23 / (upper_y_g_22 * J[i] * upper_y_dy * dy[i]); @@ -1965,9 +1961,9 @@ Field2D Coordinates::Laplace_perpXY([[maybe_unused]] const Field2D& A, const auto lower_y_avg = [&i](const auto& f) { return 0.5 * (f[i] + f[i.ym()]); }; const BoutReal lower_y_A = lower_y_avg(A); const BoutReal lower_y_J = lower_y_avg(J); - const BoutReal lower_y_g_22 = lower_y_avg(covariantMetricTensor.Getg_22()); + const BoutReal lower_y_g_22 = lower_y_avg(covariantMetricTensor.Getg22()); const BoutReal lower_y_g23 = lower_y_avg(contravariantMetricTensor.Getg23()); - const BoutReal lower_y_g_23 = lower_y_avg(covariantMetricTensor.Getg_23()); + const BoutReal lower_y_g_23 = lower_y_avg(covariantMetricTensor.Getg23()); const BoutReal lower_y_dy = lower_y_avg(dy); const BoutReal lower_y_value = -lower_y_A * lower_y_J * lower_y_g23 * lower_y_g_23 / (lower_y_g_22 * J[i] * lower_y_dy * dy[i]); @@ -1983,7 +1979,7 @@ Field2D Coordinates::Laplace_perpXY([[maybe_unused]] const Field2D& A, const Coordinates::FieldMetric& Coordinates::invSg() const { if (invSgCache == nullptr) { auto ptr = std::make_unique(); - (*ptr) = 1.0 / sqrt(covariantMetricTensor.Getg_22()); + (*ptr) = 1.0 / sqrt(covariantMetricTensor.Getg22()); invSgCache = std::move(ptr); } return *invSgCache; @@ -2008,37 +2004,35 @@ Coordinates::Grad2_par2_DDY_invSg(CELL_LOC outloc, const std::string& method) co return *Grad2_par2_DDY_invSgCache[method]; } -void Coordinates::checkCovariant() { - covariantMetricTensor.checkCovariant(localmesh->ystart); -} +void Coordinates::checkCovariant() { covariantMetricTensor.check(localmesh->ystart); } void Coordinates::checkContravariant() { - contravariantMetricTensor.checkContravariant(localmesh->ystart); + contravariantMetricTensor.check(localmesh->ystart); } -void Coordinates::setContravariantMetricTensor(ContravariantMetricTensor metric_tensor, - const std::string& region) { - contravariantMetricTensor.setContravariantMetricTensor(metric_tensor); +void Coordinates::setMetricTensor(ContravariantMetricTensor metric_tensor, + const std::string& region) { + contravariantMetricTensor.setMetricTensor(metric_tensor); covariantMetricTensor.calcCovariant(contravariantMetricTensor, location, region); } const CovariantMetricTensor::FieldMetric& Coordinates::g_11() const { - return covariantMetricTensor.Getg_11(); + return covariantMetricTensor.Getg11(); } const CovariantMetricTensor::FieldMetric& Coordinates::g_22() const { - return covariantMetricTensor.Getg_22(); + return covariantMetricTensor.Getg22(); } const CovariantMetricTensor::FieldMetric& Coordinates::g_33() const { - return covariantMetricTensor.Getg_33(); + return covariantMetricTensor.Getg33(); } const CovariantMetricTensor::FieldMetric& Coordinates::g_12() const { - return covariantMetricTensor.Getg_12(); + return covariantMetricTensor.Getg12(); } const CovariantMetricTensor::FieldMetric& Coordinates::g_13() const { - return covariantMetricTensor.Getg_13(); + return covariantMetricTensor.Getg13(); } const CovariantMetricTensor::FieldMetric& Coordinates::g_23() const { - return covariantMetricTensor.Getg_23(); + return covariantMetricTensor.Getg23(); } const ContravariantMetricTensor::FieldMetric& Coordinates::g11() const { @@ -2060,7 +2054,7 @@ const ContravariantMetricTensor::FieldMetric& Coordinates::g23() const { return contravariantMetricTensor.Getg23(); } -void Coordinates::setCovariantMetricTensor(CovariantMetricTensor metric_tensor) { - covariantMetricTensor.setCovariantMetricTensor(metric_tensor); +void Coordinates::setMetricTensor(CovariantMetricTensor metric_tensor) { + covariantMetricTensor.setMetricTensor(metric_tensor); contravariantMetricTensor.calcContravariant(covariantMetricTensor, location); } \ No newline at end of file diff --git a/src/mesh/covariantMetricTensor.cxx b/src/mesh/covariantMetricTensor.cxx index c39cb4afeb..337ca11081 100644 --- a/src/mesh/covariantMetricTensor.cxx +++ b/src/mesh/covariantMetricTensor.cxx @@ -5,130 +5,24 @@ #include "bout/output.hxx" CovariantMetricTensor::CovariantMetricTensor( - const FieldMetric& g_11, const FieldMetric& g_22, const FieldMetric& g_33, - const FieldMetric& g_12, const FieldMetric& g_13, const FieldMetric& g_23) - : g_11(std::move(g_11)), g_22(std::move(g_22)), g_33(std::move(g_33)), - g_12(std::move(g_12)), g_13(std::move(g_13)), g_23(std::move(g_23)) { + const FieldMetric& g11, const FieldMetric& g22, const FieldMetric& g33, + const FieldMetric& g12, const FieldMetric& g13, const FieldMetric& g23) + : MetricTensor::MetricTensor(g11, g22, g33, g12, g13, g23) {} - Allocate(); // Make sure metric elements are allocated // ; TODO: Required? -} - -CovariantMetricTensor::CovariantMetricTensor(const BoutReal g_11, const BoutReal g_22, - const BoutReal g_33, const BoutReal g_12, - const BoutReal g_13, const BoutReal g_23, +CovariantMetricTensor::CovariantMetricTensor(const BoutReal g11, const BoutReal g22, + const BoutReal g33, const BoutReal g12, + const BoutReal g13, const BoutReal g23, Mesh* mesh) - : g_11(g_11, mesh), g_22(g_22, mesh), g_33(g_33, mesh), g_12(g_12, mesh), - g_13(g_13, mesh), g_23(g_23, mesh) { - - Allocate(); // Make sure metric elements are allocated // ; TODO: Required? -} - -const CovariantMetricTensor::FieldMetric& CovariantMetricTensor::Getg_11() const { - return g_11; -} -const CovariantMetricTensor::FieldMetric& CovariantMetricTensor::Getg_22() const { - return g_22; -} -const CovariantMetricTensor::FieldMetric& CovariantMetricTensor::Getg_33() const { - return g_33; -} -const CovariantMetricTensor::FieldMetric& CovariantMetricTensor::Getg_12() const { - return g_12; -} -const CovariantMetricTensor::FieldMetric& CovariantMetricTensor::Getg_13() const { - return g_13; -} -const CovariantMetricTensor::FieldMetric& CovariantMetricTensor::Getg_23() const { - return g_23; -} - -void CovariantMetricTensor::setCovariantMetricTensor( - const CovariantMetricTensor& metric_tensor) { - - g_11 = metric_tensor.Getg_11(); - g_22 = metric_tensor.Getg_22(); - g_33 = metric_tensor.Getg_33(); - g_12 = metric_tensor.Getg_12(); - g_13 = metric_tensor.Getg_13(); - g_23 = metric_tensor.Getg_23(); -} + : MetricTensor::MetricTensor(g11, g22, g33, g12, g13, g23, mesh) {} -void CovariantMetricTensor::checkCovariant(int ystart) { - // Diagonal metric components should be finite - bout::checkFinite(g_11, "g_11", "RGN_NOCORNERS"); - bout::checkFinite(g_22, "g_22", "RGN_NOCORNERS"); - bout::checkFinite(g_33, "g_33", "RGN_NOCORNERS"); - if (g_11.hasParallelSlices() && &g_11.ynext(1) != &g_11) { - for (int dy = 1; dy <= ystart; ++dy) { - for (const auto sign : {1, -1}) { - bout::checkFinite(g_11.ynext(sign * dy), "g_11.ynext", - fmt::format("RGN_YPAR_{:+d}", sign * dy)); - bout::checkFinite(g_22.ynext(sign * dy), "g_22.ynext", - fmt::format("RGN_YPAR_{:+d}", sign * dy)); - bout::checkFinite(g_33.ynext(sign * dy), "g_33.ynext", - fmt::format("RGN_YPAR_{:+d}", sign * dy)); - } - } - } - // Diagonal metric components should be positive - bout::checkPositive(g_11, "g_11", "RGN_NOCORNERS"); - bout::checkPositive(g_22, "g_22", "RGN_NOCORNERS"); - bout::checkPositive(g_33, "g_33", "RGN_NOCORNERS"); - if (g_11.hasParallelSlices() && &g_11.ynext(1) != &g_11) { - for (int dy = 1; dy <= ystart; ++dy) { - for (const auto sign : {1, -1}) { - bout::checkPositive(g_11.ynext(sign * dy), "g_11.ynext", - fmt::format("RGN_YPAR_{:+d}", sign * dy)); - bout::checkPositive(g_22.ynext(sign * dy), "g_22.ynext", - fmt::format("RGN_YPAR_{:+d}", sign * dy)); - bout::checkPositive(g_33.ynext(sign * dy), "g_33.ynext", - fmt::format("RGN_YPAR_{:+d}", sign * dy)); - } - } - } - - // Off-diagonal metric components should be finite - bout::checkFinite(g_12, "g_12", "RGN_NOCORNERS"); - bout::checkFinite(g_13, "g_13", "RGN_NOCORNERS"); - bout::checkFinite(g_23, "g_23", "RGN_NOCORNERS"); - if (g_23.hasParallelSlices() && &g_23.ynext(1) != &g_23) { - for (int dy = 1; dy <= ystart; ++dy) { - for (const auto sign : {1, -1}) { - bout::checkFinite(g_12.ynext(sign * dy), "g_12.ynext", - fmt::format("RGN_YPAR_{:+d}", sign * dy)); - bout::checkFinite(g_13.ynext(sign * dy), "g_13.ynext", - fmt::format("RGN_YPAR_{:+d}", sign * dy)); - bout::checkFinite(g_23.ynext(sign * dy), "g_23.ynext", - fmt::format("RGN_YPAR_{:+d}", sign * dy)); - } - } - } -} - -void CovariantMetricTensor::Allocate() { // ; TODO: Required? - g_11.allocate(); - g_22.allocate(); - g_33.allocate(); - g_12.allocate(); - g_13.allocate(); - g_23.allocate(); -} - -void CovariantMetricTensor::setLocation(const CELL_LOC location) { - g_11.setLocation(location); - g_22.setLocation(location); - g_33.setLocation(location); - g_12.setLocation(location); - g_13.setLocation(location); - g_23.setLocation(location); -} +//[[maybe_unused]] METRIC_TYPE MetricType() { return TYPE_CONTRAVARIANT; } void CovariantMetricTensor::calcCovariant( ContravariantMetricTensor contravariantMetricTensor, const CELL_LOC location, const std::string& region) { TRACE("CovariantMetricTensor::calcCovariant"); - // Perform inversion of g^{ij} to get g_{ij} + // Perform inversion of g^{ij} to get g{ij} // NOTE: Currently this bit assumes that metric terms are Field2D objects auto a = Matrix(3, 3); @@ -149,41 +43,41 @@ void CovariantMetricTensor::calcCovariant( } } - g_11 = a(0, 0); - g_22 = a(1, 1); - g_33 = a(2, 2); - g_12 = a(0, 1); - g_13 = a(0, 2); - g_23 = a(1, 2); + g11 = a(0, 0); + g22 = a(1, 1); + g33 = a(2, 2); + g12 = a(0, 1); + g13 = a(0, 2); + g23 = a(1, 2); // covariant_components = CovariantComponents{(a(0, 0), a(1, 1), a(2, 2), a(0, 1), a(0, 2), a(1, 2))}; setLocation(location); BoutReal maxerr; - maxerr = BOUTMAX(max(abs((g_11 * contravariantMetricTensor.Getg11() - + g_12 * contravariantMetricTensor.Getg12() - + g_13 * contravariantMetricTensor.Getg13()) + maxerr = BOUTMAX(max(abs((g11 * contravariantMetricTensor.Getg11() + + g12 * contravariantMetricTensor.Getg12() + + g13 * contravariantMetricTensor.Getg13()) - 1)), - max(abs((g_12 * contravariantMetricTensor.Getg12() - + g_22 * contravariantMetricTensor.Getg22() - + g_23 * contravariantMetricTensor.Getg23()) + max(abs((g12 * contravariantMetricTensor.Getg12() + + g22 * contravariantMetricTensor.Getg22() + + g23 * contravariantMetricTensor.Getg23()) - 1)), - max(abs((g_13 * contravariantMetricTensor.Getg13() - + g_23 * contravariantMetricTensor.Getg23() - + g_33 * contravariantMetricTensor.Getg33()) + max(abs((g13 * contravariantMetricTensor.Getg13() + + g23 * contravariantMetricTensor.Getg23() + + g33 * contravariantMetricTensor.Getg33()) - 1))); output_info.write("\tLocal maximum error in diagonal inversion is {:e}\n", maxerr); - maxerr = BOUTMAX(max(abs(g_11 * contravariantMetricTensor.Getg12() - + g_12 * contravariantMetricTensor.Getg22() - + g_13 * contravariantMetricTensor.Getg23())), - max(abs(g_11 * contravariantMetricTensor.Getg13() - + g_12 * contravariantMetricTensor.Getg23() - + g_13 * contravariantMetricTensor.Getg33())), - max(abs(g_12 * contravariantMetricTensor.Getg13() - + g_22 * contravariantMetricTensor.Getg23() - + g_23 * contravariantMetricTensor.Getg33()))); + maxerr = BOUTMAX(max(abs(g11 * contravariantMetricTensor.Getg12() + + g12 * contravariantMetricTensor.Getg22() + + g13 * contravariantMetricTensor.Getg23())), + max(abs(g11 * contravariantMetricTensor.Getg13() + + g12 * contravariantMetricTensor.Getg23() + + g13 * contravariantMetricTensor.Getg33())), + max(abs(g12 * contravariantMetricTensor.Getg13() + + g22 * contravariantMetricTensor.Getg23() + + g23 * contravariantMetricTensor.Getg33()))); output_info.write("\tLocal maximum error in off-diagonal inversion is {:e}\n", maxerr); } diff --git a/src/mesh/metricTensor.cxx b/src/mesh/metricTensor.cxx new file mode 100644 index 0000000000..505cbf63ba --- /dev/null +++ b/src/mesh/metricTensor.cxx @@ -0,0 +1,107 @@ + +#include "bout/metricTensor.hxx" + +MetricTensor::MetricTensor(const FieldMetric& g11, const FieldMetric& g22, + const FieldMetric& g33, const FieldMetric& g12, + const FieldMetric& g13, const FieldMetric& g23) + : g11(std::move(g11)), g22(std::move(g22)), g33(std::move(g33)), g12(std::move(g12)), + g13(std::move(g13)), g23(std::move(g23)) { + + Allocate(); // Make sure metric elements are allocated // ; TODO: Required? +} + +MetricTensor::MetricTensor(const BoutReal g11, const BoutReal g22, const BoutReal g33, + const BoutReal g12, const BoutReal g13, const BoutReal g23, + Mesh* mesh) + : g11(g11, mesh), g22(g22, mesh), g33(g33, mesh), g12(g12, mesh), g13(g13, mesh), + g23(g23, mesh) { + + Allocate(); // Make sure metric elements are allocated // ; TODO: Required? +} + +const MetricTensor::FieldMetric& MetricTensor::Getg11() const { return g11; } +const MetricTensor::FieldMetric& MetricTensor::Getg22() const { return g22; } +const MetricTensor::FieldMetric& MetricTensor::Getg33() const { return g33; } +const MetricTensor::FieldMetric& MetricTensor::Getg12() const { return g12; } +const MetricTensor::FieldMetric& MetricTensor::Getg13() const { return g13; } +const MetricTensor::FieldMetric& MetricTensor::Getg23() const { return g23; } + +void MetricTensor::setMetricTensor(const MetricTensor& metric_tensor) { + + g11 = metric_tensor.Getg11(); + g22 = metric_tensor.Getg22(); + g33 = metric_tensor.Getg33(); + g12 = metric_tensor.Getg12(); + g13 = metric_tensor.Getg13(); + g23 = metric_tensor.Getg23(); +} + +void MetricTensor::check(int ystart) { + // Diagonal metric components should be finite + bout::checkFinite(g11, "g11", "RGN_NOCORNERS"); + bout::checkFinite(g22, "g22", "RGN_NOCORNERS"); + bout::checkFinite(g33, "g33", "RGN_NOCORNERS"); + if (g11.hasParallelSlices() && &g11.ynext(1) != &g11) { + for (int dy = 1; dy <= ystart; ++dy) { + for (const auto sign : {1, -1}) { + bout::checkFinite(g11.ynext(sign * dy), "g11.ynext", + fmt::format("RGN_YPAR_{:+d}", sign * dy)); + bout::checkFinite(g22.ynext(sign * dy), "g22.ynext", + fmt::format("RGN_YPAR_{:+d}", sign * dy)); + bout::checkFinite(g33.ynext(sign * dy), "g33.ynext", + fmt::format("RGN_YPAR_{:+d}", sign * dy)); + } + } + } + // Diagonal metric components should be positive + bout::checkPositive(g11, "g11", "RGN_NOCORNERS"); + bout::checkPositive(g22, "g22", "RGN_NOCORNERS"); + bout::checkPositive(g33, "g33", "RGN_NOCORNERS"); + if (g11.hasParallelSlices() && &g11.ynext(1) != &g11) { + for (int dy = 1; dy <= ystart; ++dy) { + for (const auto sign : {1, -1}) { + bout::checkPositive(g11.ynext(sign * dy), "g11.ynext", + fmt::format("RGN_YPAR_{:+d}", sign * dy)); + bout::checkPositive(g22.ynext(sign * dy), "g22.ynext", + fmt::format("RGN_YPAR_{:+d}", sign * dy)); + bout::checkPositive(g33.ynext(sign * dy), "g33.ynext", + fmt::format("RGN_YPAR_{:+d}", sign * dy)); + } + } + } + + // Off-diagonal metric components should be finite + bout::checkFinite(g12, "g12", "RGN_NOCORNERS"); + bout::checkFinite(g13, "g13", "RGN_NOCORNERS"); + bout::checkFinite(g23, "g23", "RGN_NOCORNERS"); + if (g23.hasParallelSlices() && &g23.ynext(1) != &g23) { + for (int dy = 1; dy <= ystart; ++dy) { + for (const auto sign : {1, -1}) { + bout::checkFinite(g12.ynext(sign * dy), "g12.ynext", + fmt::format("RGN_YPAR_{:+d}", sign * dy)); + bout::checkFinite(g13.ynext(sign * dy), "g13.ynext", + fmt::format("RGN_YPAR_{:+d}", sign * dy)); + bout::checkFinite(g23.ynext(sign * dy), "g23.ynext", + fmt::format("RGN_YPAR_{:+d}", sign * dy)); + } + } + } +} + +void MetricTensor::Allocate() { // ; TODO: Required? + g11.allocate(); + g22.allocate(); + g33.allocate(); + g12.allocate(); + g13.allocate(); + g23.allocate(); +} + +void MetricTensor::setLocation(const CELL_LOC location) { + g11.setLocation(location); + g22.setLocation(location); + g33.setLocation(location); + g12.setLocation(location); + g13.setLocation(location); + g23.setLocation(location); +} diff --git a/tests/MMS/GBS/gbs.cxx b/tests/MMS/GBS/gbs.cxx index 59dd02202b..2a167d0c70 100644 --- a/tests/MMS/GBS/gbs.cxx +++ b/tests/MMS/GBS/gbs.cxx @@ -354,7 +354,7 @@ void GBS::LoadMetric(BoutReal Lnorm, BoutReal Bnorm) { const auto g12 = 0.0; const auto g13 = -sinty * coords->g11(); const auto g23 = -sbp * Btxy / (hthe * Bpxy * Rxy); - coords->setContravariantMetricTensor( + coords->setMetricTensor( ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); coords->J = hthe / Bpxy; @@ -365,7 +365,7 @@ void GBS::LoadMetric(BoutReal Lnorm, BoutReal Bnorm) { const auto g_12 = sbp * Btxy * hthe * sinty * Rxy / Bpxy; const auto g_13 = sinty * Rxy * Rxy; const auto g_23 = sbp * Btxy * hthe * Rxy / Bpxy; - coords->setCovariantMetricTensor( + coords->setMetricTensor( CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); coords->geometry(); diff --git a/tests/MMS/diffusion/diffusion.cxx b/tests/MMS/diffusion/diffusion.cxx index d7eb25de21..aba31de755 100644 --- a/tests/MMS/diffusion/diffusion.cxx +++ b/tests/MMS/diffusion/diffusion.cxx @@ -45,10 +45,10 @@ int Diffusion::init(bool UNUSED(restarting)) { //set mesh auto contravariant_metric_tensor = ContravariantMetricTensor(1.1, 1.1, 1.1, 0.0, 0.0, 0.0); - coord->setContravariantMetricTensor(contravariant_metric_tensor); + coord->setMetricTensor(contravariant_metric_tensor); auto covariant_metric_tensor = CovariantMetricTensor(1.1, 1.1, 1.1, 0.0, 0.0, 0.0); - coord->setCovariantMetricTensor(covariant_metric_tensor); + coord->setMetricTensor(covariant_metric_tensor); coord->geometry(); diff --git a/tests/MMS/diffusion2/diffusion.cxx b/tests/MMS/diffusion2/diffusion.cxx index db9a9b92fd..7c136f4e7f 100644 --- a/tests/MMS/diffusion2/diffusion.cxx +++ b/tests/MMS/diffusion2/diffusion.cxx @@ -40,10 +40,10 @@ class Diffusion : public PhysicsModel { // set mesh auto contravariant_metric_tensor = ContravariantMetricTensor(1.1, 1.1, 1.1, 0.0, 0.0, 0.0); - coords->setContravariantMetricTensor(contravariant_metric_tensor); + coords->setMetricTensor(contravariant_metric_tensor); auto covariant_metric_tensor = CovariantMetricTensor(1.1, 1.1, 1.1, 0.0, 0.0, 0.0); - coords->setCovariantMetricTensor(covariant_metric_tensor); + coords->setMetricTensor(covariant_metric_tensor); coords->geometry(); diff --git a/tests/MMS/elm-pb/elm_pb.cxx b/tests/MMS/elm-pb/elm_pb.cxx index 548efe12f6..1664479a52 100644 --- a/tests/MMS/elm-pb/elm_pb.cxx +++ b/tests/MMS/elm-pb/elm_pb.cxx @@ -423,7 +423,7 @@ class ELMpb : public PhysicsModel { const auto g12 = 0.0; const auto g13 = -I * coords->g11(); const auto g23 = -Btxy / (hthe * Bpxy * Rxy); - coords->setContravariantMetricTensor( + coords->setMetricTensor( ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); coords->J = hthe / Bpxy; @@ -435,7 +435,7 @@ class ELMpb : public PhysicsModel { const auto g_12 = Btxy * hthe * I * Rxy / Bpxy; const auto g_13 = I * Rxy * Rxy; const auto g_23 = Btxy * hthe * Rxy / Bpxy; - coords->setCovariantMetricTensor( + coords->setMetricTensor( CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); coords->geometry(); // Calculate quantities from metric tensor diff --git a/tests/MMS/spatial/diffusion/diffusion.cxx b/tests/MMS/spatial/diffusion/diffusion.cxx index 0abdaae162..878ae776c7 100644 --- a/tests/MMS/spatial/diffusion/diffusion.cxx +++ b/tests/MMS/spatial/diffusion/diffusion.cxx @@ -41,10 +41,10 @@ class Diffusion : public PhysicsModel { // set mesh auto contravariant_metric_tensor = ContravariantMetricTensor(1.1, 1.1, 1.1, 0.0, 0.0, 0.0); - coords->setContravariantMetricTensor(contravariant_metric_tensor); + coords->setMetricTensor(contravariant_metric_tensor); auto covariant_metric_tensor = CovariantMetricTensor(1.1, 1.1, 1.1, 0.0, 0.0, 0.0); - coords->setCovariantMetricTensor(covariant_metric_tensor); + coords->setMetricTensor(covariant_metric_tensor); coords->geometry(); diff --git a/tests/MMS/tokamak/tokamak.cxx b/tests/MMS/tokamak/tokamak.cxx index 5ffe1bf2f4..b750f50f09 100644 --- a/tests/MMS/tokamak/tokamak.cxx +++ b/tests/MMS/tokamak/tokamak.cxx @@ -86,7 +86,7 @@ class TokamakMMS : public PhysicsModel { const auto g12 = 0.0; const auto g13 = -sinty * coords->g11(); const auto g23 = -sbp * Btxy / (hthe * Bpxy * Rxy); - coords->setContravariantMetricTensor( + coords->setMetricTensor( ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); coords->J = hthe / Bpxy; @@ -97,7 +97,7 @@ class TokamakMMS : public PhysicsModel { const auto g_12 = sbp * Btxy * hthe * sinty * Rxy / Bpxy; const auto g_13 = sinty * Rxy * Rxy; const auto g_23 = sbp * Btxy * hthe * Rxy / Bpxy; - coords->setCovariantMetricTensor( + coords->setMetricTensor( CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); coords->geometry(); diff --git a/tests/MMS/wave-1d/wave.cxx b/tests/MMS/wave-1d/wave.cxx index e928dd1307..3fddf2f3e0 100644 --- a/tests/MMS/wave-1d/wave.cxx +++ b/tests/MMS/wave-1d/wave.cxx @@ -34,10 +34,10 @@ class Wave1D : public PhysicsModel { //set mesh auto contravariant_metric_tensor = ContravariantMetricTensor(1.1, 1.1, 1.1, 0.0, 0.0, 0.0); - coord->setContravariantMetricTensor(contravariant_metric_tensor); + coord->setMetricTensor(contravariant_metric_tensor); auto covariant_metric_tensor = CovariantMetricTensor(1.1, 1.1, 1.1, 0.0, 0.0, 0.0); - coord->setCovariantMetricTensor(covariant_metric_tensor); + coord->setMetricTensor(covariant_metric_tensor); coord->geometry(); diff --git a/tests/integrated/test-drift-instability/2fluid.cxx b/tests/integrated/test-drift-instability/2fluid.cxx index f69ec5ee2f..8b770f420b 100644 --- a/tests/integrated/test-drift-instability/2fluid.cxx +++ b/tests/integrated/test-drift-instability/2fluid.cxx @@ -224,7 +224,7 @@ class TwoFluid : public PhysicsModel { g12 = 0.0; g13 = -I * coord->g11(); g23 = -Btxy / (hthe * Bpxy * Rxy); - coord->setContravariantMetricTensor( + coord->setMetricTensor( ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); coord->J = hthe / Bpxy; @@ -236,7 +236,7 @@ class TwoFluid : public PhysicsModel { g_12 = Btxy * hthe * I * Rxy / Bpxy; g_13 = I * Rxy * Rxy; g_23 = Btxy * hthe * Rxy / Bpxy; - coord->setCovariantMetricTensor( + coord->setMetricTensor( CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); coord->geometry(); diff --git a/tests/integrated/test-interchange-instability/2fluid.cxx b/tests/integrated/test-interchange-instability/2fluid.cxx index 632c1eadf2..282df1c3fb 100644 --- a/tests/integrated/test-interchange-instability/2fluid.cxx +++ b/tests/integrated/test-interchange-instability/2fluid.cxx @@ -146,7 +146,7 @@ class Interchange : public PhysicsModel { g12 = 0.0; g13 = -I * coord->g11(); g23 = -Btxy / (hthe * Bpxy * Rxy); - coord->setContravariantMetricTensor( + coord->setMetricTensor( ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); coord->J = hthe / Bpxy; @@ -158,7 +158,7 @@ class Interchange : public PhysicsModel { g_12 = Btxy * hthe * I * Rxy / Bpxy; g_13 = I * Rxy * Rxy; g_23 = Btxy * hthe * Rxy / Bpxy; - coord->setCovariantMetricTensor( + coord->setMetricTensor( CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); coord->geometry(); diff --git a/tests/integrated/test-laplacexy/loadmetric.cxx b/tests/integrated/test-laplacexy/loadmetric.cxx index 2bb1a79114..2aa11b0049 100644 --- a/tests/integrated/test-laplacexy/loadmetric.cxx +++ b/tests/integrated/test-laplacexy/loadmetric.cxx @@ -55,7 +55,7 @@ void LoadMetric(BoutReal Lnorm, BoutReal Bnorm) { const auto g12 = 0.0; const auto g13 = -sinty * coords->g11(); const auto g23 = -sbp * Btxy / (hthe * Bpxy * Rxy); - coords->setContravariantMetricTensor( + coords->setMetricTensor( ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); coords->J = hthe / Bpxy; @@ -66,7 +66,7 @@ void LoadMetric(BoutReal Lnorm, BoutReal Bnorm) { const auto g_12 = sbp * Btxy * hthe * sinty * Rxy / Bpxy; const auto g_13 = sinty * Rxy * Rxy; const auto g_23 = sbp * Btxy * hthe * Rxy / Bpxy; - coords->setCovariantMetricTensor( + coords->setMetricTensor( CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); coords->geometry(); diff --git a/tests/integrated/test-laplacexz/test-laplacexz.cxx b/tests/integrated/test-laplacexz/test-laplacexz.cxx index 5a6571d33b..3c90a276ee 100644 --- a/tests/integrated/test-laplacexz/test-laplacexz.cxx +++ b/tests/integrated/test-laplacexz/test-laplacexz.cxx @@ -21,7 +21,7 @@ int main(int argc, char** argv) { auto coord = bout::globals::mesh->getCoordinates(); const auto g13 = 1.8; // test off-diagonal components with nonzero value - coord->setContravariantMetricTensor(ContravariantMetricTensor( + coord->setMetricTensor(ContravariantMetricTensor( coord->g11(), coord->g22(), coord->g33(), coord->g12(), g13, coord->g23())); // create some input field @@ -36,7 +36,7 @@ int main(int argc, char** argv) { Field3D f2 = inv->solve(g, 0.0); // Invert the Laplacian. // reset to 0.0 for original laplacexz test - coord->setContravariantMetricTensor(ContravariantMetricTensor( + coord->setMetricTensor(ContravariantMetricTensor( coord->g11(), coord->g22(), coord->g33(), coord->g12(), 0.0, coord->g23())); // Now the normal test. diff --git a/tests/unit/mesh/test_coordinates.cxx b/tests/unit/mesh/test_coordinates.cxx index d0a31d91f1..16d948d3ae 100644 --- a/tests/unit/mesh/test_coordinates.cxx +++ b/tests/unit/mesh/test_coordinates.cxx @@ -359,7 +359,7 @@ TEST_F(CoordinatesTest, SetContravariantMetricTensor) { // Modify with setter auto updated_metric_tensor = ContravariantMetricTensor(1.7, 2.3, 3.1, 0.9, 5.7, 1.9); - coords.setContravariantMetricTensor(updated_metric_tensor); + coords.setMetricTensor(updated_metric_tensor); // Get values with getter and check they have been modified as expected EXPECT_TRUE(IsFieldEqual(coords.g11(), 1.7)); @@ -426,7 +426,7 @@ TEST_F(CoordinatesTest, SetCovariantMetricTensor) { // Modify with setter auto updated_metric_tensor = CovariantMetricTensor(1.7, 2.3, 3.1, 0.9, 5.7, 1.9); - coords.setCovariantMetricTensor(updated_metric_tensor); + coords.setMetricTensor(updated_metric_tensor); // Get values with getter and check they have been modified as expected EXPECT_TRUE(IsFieldEqual(coords.g_11(), 1.7)); From 98488a55b229c61647d3d20a2093fcc2d7f75157 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Mon, 6 Nov 2023 14:36:24 +0000 Subject: [PATCH 125/491] Naming in constructor. --- src/mesh/covariantMetricTensor.cxx | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/mesh/covariantMetricTensor.cxx b/src/mesh/covariantMetricTensor.cxx index 337ca11081..d0a7e28959 100644 --- a/src/mesh/covariantMetricTensor.cxx +++ b/src/mesh/covariantMetricTensor.cxx @@ -5,15 +5,15 @@ #include "bout/output.hxx" CovariantMetricTensor::CovariantMetricTensor( - const FieldMetric& g11, const FieldMetric& g22, const FieldMetric& g33, - const FieldMetric& g12, const FieldMetric& g13, const FieldMetric& g23) - : MetricTensor::MetricTensor(g11, g22, g33, g12, g13, g23) {} + const FieldMetric& g_11, const FieldMetric& g_22, const FieldMetric& g_33, + const FieldMetric& g_12, const FieldMetric& g_13, const FieldMetric& g_23) + : MetricTensor::MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23) {} -CovariantMetricTensor::CovariantMetricTensor(const BoutReal g11, const BoutReal g22, - const BoutReal g33, const BoutReal g12, - const BoutReal g13, const BoutReal g23, +CovariantMetricTensor::CovariantMetricTensor(const BoutReal g_11, const BoutReal g_22, + const BoutReal g_33, const BoutReal g_12, + const BoutReal g_13, const BoutReal g_23, Mesh* mesh) - : MetricTensor::MetricTensor(g11, g22, g33, g12, g13, g23, mesh) {} + : MetricTensor::MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23, mesh) {} //[[maybe_unused]] METRIC_TYPE MetricType() { return TYPE_CONTRAVARIANT; } From 7668e0faefb8c163b89d09d635a88471fcacd00f Mon Sep 17 00:00:00 2001 From: tomc271 Date: Mon, 6 Nov 2023 14:37:34 +0000 Subject: [PATCH 126/491] YAGNI: Remove MetricType(). --- include/bout/bout_types.hxx | 4 ---- include/bout/metricTensor.hxx | 2 -- src/mesh/contravariantMetricTensor.cxx | 2 -- src/mesh/covariantMetricTensor.cxx | 2 -- 4 files changed, 10 deletions(-) diff --git a/include/bout/bout_types.hxx b/include/bout/bout_types.hxx index 8a9cd1d608..c1f06fca7c 100644 --- a/include/bout/bout_types.hxx +++ b/include/bout/bout_types.hxx @@ -46,10 +46,6 @@ constexpr CELL_LOC CELL_YLOW = CELL_LOC::ylow; constexpr CELL_LOC CELL_ZLOW = CELL_LOC::zlow; constexpr CELL_LOC CELL_VSHIFT = CELL_LOC::vshift; -enum class METRIC_TYPE { contravariant, covariant }; -constexpr METRIC_TYPE TYPE_CONTRAVARIANT = METRIC_TYPE::contravariant; -constexpr METRIC_TYPE TYPE_COVARIANT = METRIC_TYPE::covariant; - std::string toString(CELL_LOC location); CELL_LOC CELL_LOCFromString(const std::string& location_string); diff --git a/include/bout/metricTensor.hxx b/include/bout/metricTensor.hxx index aaeecf0b81..96e3bda7e0 100644 --- a/include/bout/metricTensor.hxx +++ b/include/bout/metricTensor.hxx @@ -40,8 +40,6 @@ public: void setLocation(const CELL_LOC location); -// [[maybe_unused]] virtual METRIC_TYPE MetricType() = 0; - protected: FieldMetric g11, g22, g33, g12, g13, g23; }; diff --git a/src/mesh/contravariantMetricTensor.cxx b/src/mesh/contravariantMetricTensor.cxx index 91ce7ec421..1481151e66 100644 --- a/src/mesh/contravariantMetricTensor.cxx +++ b/src/mesh/contravariantMetricTensor.cxx @@ -13,8 +13,6 @@ ContravariantMetricTensor::ContravariantMetricTensor( const BoutReal g13, const BoutReal g23, Mesh* mesh) : MetricTensor::MetricTensor(g11, g22, g33, g12, g13, g23, mesh) {} -//[[maybe_unused]] METRIC_TYPE MetricType() { return TYPE_CONTRAVARIANT; } - void ContravariantMetricTensor::calcContravariant( CovariantMetricTensor covariantMetricTensor, CELL_LOC location, const std::string& region) { diff --git a/src/mesh/covariantMetricTensor.cxx b/src/mesh/covariantMetricTensor.cxx index d0a7e28959..4064af07df 100644 --- a/src/mesh/covariantMetricTensor.cxx +++ b/src/mesh/covariantMetricTensor.cxx @@ -15,8 +15,6 @@ CovariantMetricTensor::CovariantMetricTensor(const BoutReal g_11, const BoutReal Mesh* mesh) : MetricTensor::MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23, mesh) {} -//[[maybe_unused]] METRIC_TYPE MetricType() { return TYPE_CONTRAVARIANT; } - void CovariantMetricTensor::calcCovariant( ContravariantMetricTensor contravariantMetricTensor, const CELL_LOC location, const std::string& region) { From 66ec7e5dd4e37eb274ff1ba3df6c2e13383e1027 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Mon, 6 Nov 2023 15:41:32 +0000 Subject: [PATCH 127/491] Make MetricTensor abstract (by making calcCovariant() and calcContravariant() implementations of CalculateOppositeRepresentation()). --- include/bout/contravariantMetricTensor.hxx | 6 ++++-- include/bout/covariantMetricTensor.hxx | 6 ++++-- include/bout/metricTensor.hxx | 5 ++++- src/mesh/contravariantMetricTensor.cxx | 7 +++---- src/mesh/coordinates.cxx | 16 ++++++++++------ src/mesh/covariantMetricTensor.cxx | 6 +++--- tools/pylib/_boutpp_build/boutcpp.pxd.jinja | 4 ++-- 7 files changed, 30 insertions(+), 20 deletions(-) diff --git a/include/bout/contravariantMetricTensor.hxx b/include/bout/contravariantMetricTensor.hxx index 4c9225a6b5..de04927ce1 100644 --- a/include/bout/contravariantMetricTensor.hxx +++ b/include/bout/contravariantMetricTensor.hxx @@ -14,9 +14,11 @@ public: ContravariantMetricTensor(const BoutReal g11, const BoutReal g22, const BoutReal g33, const BoutReal g12, const BoutReal g13, const BoutReal g23, Mesh* mesh); + /// Invert covariant metric to get contravariant components - void calcContravariant(CovariantMetricTensor covariantMetricTensor, CELL_LOC location, - const std::string& region = "RGN_ALL"); + void CalculateOppositeRepresentation(MetricTensor& covariantMetricTensor, + CELL_LOC location, + const std::string& region = "RGN_ALL") override; }; #endif //BOUT_CONTRAVARIANTMETRICTENSOR_HXX diff --git a/include/bout/covariantMetricTensor.hxx b/include/bout/covariantMetricTensor.hxx index 25d80cc01f..30c0f178cd 100644 --- a/include/bout/covariantMetricTensor.hxx +++ b/include/bout/covariantMetricTensor.hxx @@ -14,9 +14,11 @@ public: CovariantMetricTensor(const BoutReal g_11, const BoutReal g_22, const BoutReal g_33, const BoutReal g_12, const BoutReal g_13, const BoutReal g_23, Mesh* mesh); + /// Invert contravariant metric to get covariant components - void calcCovariant(ContravariantMetricTensor contravariantMetricTensor, - CELL_LOC location, const std::string& region = "RGN_ALL"); + void CalculateOppositeRepresentation(MetricTensor& contravariantMetricTensor, + const CELL_LOC location, + const std::string& region = "RGN_ALL") override; }; #endif //BOUT_COVARIANTMETRICTENSOR_HXX diff --git a/include/bout/metricTensor.hxx b/include/bout/metricTensor.hxx index 96e3bda7e0..17db1af321 100644 --- a/include/bout/metricTensor.hxx +++ b/include/bout/metricTensor.hxx @@ -17,7 +17,6 @@ public: using FieldMetric = Field2D; #endif - MetricTensor(); MetricTensor(const FieldMetric& g11, const FieldMetric& g22, const FieldMetric& g33, const FieldMetric& g12, const FieldMetric& g13, const FieldMetric& g23); @@ -40,6 +39,10 @@ public: void setLocation(const CELL_LOC location); + virtual void CalculateOppositeRepresentation(MetricTensor& covariantMetricTensor, + CELL_LOC location, + const std::string& region) = 0; + protected: FieldMetric g11, g22, g33, g12, g13, g23; }; diff --git a/src/mesh/contravariantMetricTensor.cxx b/src/mesh/contravariantMetricTensor.cxx index 1481151e66..5a1bb6035a 100644 --- a/src/mesh/contravariantMetricTensor.cxx +++ b/src/mesh/contravariantMetricTensor.cxx @@ -13,11 +13,10 @@ ContravariantMetricTensor::ContravariantMetricTensor( const BoutReal g13, const BoutReal g23, Mesh* mesh) : MetricTensor::MetricTensor(g11, g22, g33, g12, g13, g23, mesh) {} -void ContravariantMetricTensor::calcContravariant( - CovariantMetricTensor covariantMetricTensor, CELL_LOC location, - const std::string& region) { +void ContravariantMetricTensor::CalculateOppositeRepresentation( + MetricTensor& covariantMetricTensor, CELL_LOC location, const std::string& region) { - TRACE("ContravariantMetricTensor::calcContravariant"); + TRACE("ContravariantMetricTensor::CalculateOppositeRepresentation"); // Perform inversion of g{ij} to get g^{ij} // NOTE: Currently this bit assumes that metric terms are Field2D objects diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index da1960881a..7f9b9048f6 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -1386,14 +1386,16 @@ void Coordinates::CalculateChristoffelSymbols() { } CovariantMetricTensor Coordinates::calcCovariant(const std::string& region) { - TRACE("Coordinates::calcCovariant"); - covariantMetricTensor.calcCovariant(contravariantMetricTensor, location, region); + TRACE("Coordinates::CalculateOppositeRepresentation"); + covariantMetricTensor.CalculateOppositeRepresentation(contravariantMetricTensor, + location, region); return covariantMetricTensor; } ContravariantMetricTensor Coordinates::calcContravariant(const std::string& region) { - TRACE("Coordinates::calcContravariant"); - contravariantMetricTensor.calcContravariant(covariantMetricTensor, location, region); + TRACE("Coordinates::CalculateOppositeRepresentation"); + contravariantMetricTensor.CalculateOppositeRepresentation(covariantMetricTensor, + location, region); return contravariantMetricTensor; } @@ -2013,7 +2015,8 @@ void Coordinates::checkContravariant() { void Coordinates::setMetricTensor(ContravariantMetricTensor metric_tensor, const std::string& region) { contravariantMetricTensor.setMetricTensor(metric_tensor); - covariantMetricTensor.calcCovariant(contravariantMetricTensor, location, region); + covariantMetricTensor.CalculateOppositeRepresentation(contravariantMetricTensor, + location, region); } const CovariantMetricTensor::FieldMetric& Coordinates::g_11() const { @@ -2056,5 +2059,6 @@ const ContravariantMetricTensor::FieldMetric& Coordinates::g23() const { void Coordinates::setMetricTensor(CovariantMetricTensor metric_tensor) { covariantMetricTensor.setMetricTensor(metric_tensor); - contravariantMetricTensor.calcContravariant(covariantMetricTensor, location); + contravariantMetricTensor.CalculateOppositeRepresentation(covariantMetricTensor, + location); } \ No newline at end of file diff --git a/src/mesh/covariantMetricTensor.cxx b/src/mesh/covariantMetricTensor.cxx index 4064af07df..f7aa40cb25 100644 --- a/src/mesh/covariantMetricTensor.cxx +++ b/src/mesh/covariantMetricTensor.cxx @@ -15,10 +15,10 @@ CovariantMetricTensor::CovariantMetricTensor(const BoutReal g_11, const BoutReal Mesh* mesh) : MetricTensor::MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23, mesh) {} -void CovariantMetricTensor::calcCovariant( - ContravariantMetricTensor contravariantMetricTensor, const CELL_LOC location, +void CovariantMetricTensor::CalculateOppositeRepresentation( + MetricTensor& contravariantMetricTensor, const CELL_LOC location, const std::string& region) { - TRACE("CovariantMetricTensor::calcCovariant"); + TRACE("CovariantMetricTensor::CalculateOppositeRepresentation"); // Perform inversion of g^{ij} to get g{ij} // NOTE: Currently this bit assumes that metric terms are Field2D objects diff --git a/tools/pylib/_boutpp_build/boutcpp.pxd.jinja b/tools/pylib/_boutpp_build/boutcpp.pxd.jinja index 12e210a5b5..f66607a373 100644 --- a/tools/pylib/_boutpp_build/boutcpp.pxd.jinja +++ b/tools/pylib/_boutpp_build/boutcpp.pxd.jinja @@ -79,8 +79,8 @@ cdef extern from "bout/coordinates.hxx": {{ metric_field }} ShiftTorsion {{ metric_field }} IntShiftTorsion int geometry() - int calcCovariant() - int calcContravariant() + int CalculateOppositeRepresentation() + int CalculateOppositeRepresentation() int jacobian() cdef extern from "bout/fieldgroup.hxx": From 888cb0def754ce5b2b1c4bd7207793b472d393b3 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Mon, 6 Nov 2023 15:56:37 +0000 Subject: [PATCH 128/491] Catch BoutException by reference rather than by value. --- src/mesh/coordinates.cxx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 7f9b9048f6..785cbd97cc 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -522,7 +522,7 @@ Coordinates::Coordinates(Mesh* mesh, Options* options) /// Calculate contravariant metric components if not found try { calcCovariant("RGN_NOCORNERS"); - } catch (BoutException) { + } catch (BoutException&) { throw BoutException("Error in calcCovariant call"); } } @@ -530,11 +530,11 @@ Coordinates::Coordinates(Mesh* mesh, Options* options) /// Calculate contravariant metric components if not found try { calcCovariant("RGN_NOCORNERS"); - } catch (BoutException) { + } catch (BoutException&) { throw BoutException("Error in calcCovariant call"); } } - + FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; // More robust to extrapolate derived quantities directly, rather than From de50683fc96d680a8a11f572a614c2d7be24b0dc Mon Sep 17 00:00:00 2001 From: tomc271 Date: Tue, 7 Nov 2023 09:24:13 +0000 Subject: [PATCH 129/491] DRY: Replace contravariantMetricTensor and covariantMetricTensor with single class metricTensor. --- CMakeLists.txt | 4 - include/bout/contravariantMetricTensor.hxx | 24 ------ include/bout/coordinates.hxx | 19 ++--- include/bout/covariantMetricTensor.hxx | 24 ------ include/bout/fv_ops.hxx | 5 +- include/bout/metricTensor.hxx | 8 +- src/mesh/contravariantMetricTensor.cxx | 75 ----------------- src/mesh/coordinates.cxx | 67 ++++++++------- src/mesh/coordinates_accessor.cxx | 3 +- src/mesh/covariantMetricTensor.cxx | 81 ------------------- src/mesh/metricTensor.cxx | 63 +++++++++++++++ tests/MMS/GBS/gbs.cxx | 8 +- tests/MMS/diffusion/diffusion.cxx | 8 +- tests/MMS/diffusion2/diffusion.cxx | 8 +- tests/MMS/elm-pb/elm_pb.cxx | 8 +- tests/MMS/fieldalign/fieldalign.cxx | 17 ++-- tests/MMS/spatial/diffusion/diffusion.cxx | 8 +- tests/MMS/tokamak/tokamak.cxx | 8 +- tests/MMS/wave-1d/wave.cxx | 11 ++- .../test-drift-instability/2fluid.cxx | 10 +-- .../test-interchange-instability/2fluid.cxx | 12 +-- .../integrated/test-laplacexy/loadmetric.cxx | 8 +- .../test-laplacexz/test-laplacexz.cxx | 4 +- tests/unit/mesh/test_coordinates.cxx | 8 +- 24 files changed, 170 insertions(+), 321 deletions(-) delete mode 100644 include/bout/contravariantMetricTensor.hxx delete mode 100644 include/bout/covariantMetricTensor.hxx delete mode 100644 src/mesh/contravariantMetricTensor.cxx delete mode 100644 src/mesh/covariantMetricTensor.cxx diff --git a/CMakeLists.txt b/CMakeLists.txt index 959a39dfdd..334481b4f6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -354,10 +354,6 @@ set(BOUT_SOURCES ./src/sys/utils.cxx ${CMAKE_CURRENT_BINARY_DIR}/include/bout/revision.hxx ${CMAKE_CURRENT_BINARY_DIR}/include/bout/version.hxx - include/bout/contravariantMetricTensor.hxx - src/mesh/contravariantMetricTensor.cxx - include/bout/covariantMetricTensor.hxx - src/mesh/covariantMetricTensor.cxx src/mesh/metricTensor.cxx include/bout/metricTensor.hxx ) diff --git a/include/bout/contravariantMetricTensor.hxx b/include/bout/contravariantMetricTensor.hxx deleted file mode 100644 index de04927ce1..0000000000 --- a/include/bout/contravariantMetricTensor.hxx +++ /dev/null @@ -1,24 +0,0 @@ - -#ifndef BOUT_CONTRAVARIANTMETRICTENSOR_HXX -#define BOUT_CONTRAVARIANTMETRICTENSOR_HXX - -#include "covariantMetricTensor.hxx" -#include "metricTensor.hxx" - -class ContravariantMetricTensor : public MetricTensor { - -public: - ContravariantMetricTensor(const FieldMetric& g11, const FieldMetric& g22, - const FieldMetric& g33, const FieldMetric& g12, - const FieldMetric& g13, const FieldMetric& g23); - ContravariantMetricTensor(const BoutReal g11, const BoutReal g22, const BoutReal g33, - const BoutReal g12, const BoutReal g13, const BoutReal g23, - Mesh* mesh); - - /// Invert covariant metric to get contravariant components - void CalculateOppositeRepresentation(MetricTensor& covariantMetricTensor, - CELL_LOC location, - const std::string& region = "RGN_ALL") override; -}; - -#endif //BOUT_CONTRAVARIANTMETRICTENSOR_HXX diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index d5b5499f5e..c28d576ecb 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -33,8 +33,7 @@ #ifndef BOUT_COORDINATES_H #define BOUT_COORDINATES_H -#include "contravariantMetricTensor.hxx" -#include "covariantMetricTensor.hxx" +#include "metricTensor.hxx" #include "bout/field2d.hxx" #include "bout/field3d.hxx" #include "bout/paralleltransform.hxx" @@ -99,11 +98,11 @@ public: FieldMetric Bxy; ///< Magnitude of B = nabla z times nabla x private: - ContravariantMetricTensor contravariantMetricTensor; - CovariantMetricTensor covariantMetricTensor; + MetricTensor contravariantMetricTensor; + MetricTensor covariantMetricTensor; public: - + /// Covariant metric tensor const FieldMetric& g_11() const; const FieldMetric& g_22() const; const FieldMetric& g_33() const; @@ -111,6 +110,7 @@ public: const FieldMetric& g_13() const; const FieldMetric& g_23() const; + /// Contravariant metric tensor (g^{ij}) const FieldMetric& g11() const; const FieldMetric& g22() const; const FieldMetric& g33() const; @@ -118,10 +118,11 @@ public: const FieldMetric& g13() const; const FieldMetric& g23() const; - void setMetricTensor(ContravariantMetricTensor metric_tensor, + void setContravariantMetricTensor(MetricTensor metric_tensor, const std::string& region = "RGN_ALL"); - void setMetricTensor(CovariantMetricTensor metric_tensor); + void setCovariantMetricTensor(MetricTensor metric_tensor, + const std::string& region = "RGN_ALL"); /// Christoffel symbol of the second kind (connection coefficients) FieldMetric G1_11, G1_22, G1_33, G1_12, G1_13, G1_23; @@ -139,9 +140,9 @@ public: int geometry(bool recalculate_staggered = true, bool force_interpolate_from_centre = false); /// Invert contravariant metric to get covariant components - CovariantMetricTensor calcCovariant(const std::string& region = "RGN_ALL"); + MetricTensor calcCovariant(const std::string& region = "RGN_ALL"); /// Invert covariant metric to get contravariant components - ContravariantMetricTensor calcContravariant(const std::string& region = "RGN_ALL"); + MetricTensor calcContravariant(const std::string& region = "RGN_ALL"); int jacobian(); ///< Calculate J and Bxy void CalculateChristoffelSymbols(); /// Calculate Christoffel symbol terms diff --git a/include/bout/covariantMetricTensor.hxx b/include/bout/covariantMetricTensor.hxx deleted file mode 100644 index 30c0f178cd..0000000000 --- a/include/bout/covariantMetricTensor.hxx +++ /dev/null @@ -1,24 +0,0 @@ - -#ifndef BOUT_COVARIANTMETRICTENSOR_HXX -#define BOUT_COVARIANTMETRICTENSOR_HXX - -#include "metricTensor.hxx" - -class ContravariantMetricTensor; -class CovariantMetricTensor : public MetricTensor { - -public: - CovariantMetricTensor(const FieldMetric& g_11, const FieldMetric& g_22, - const FieldMetric& g_33, const FieldMetric& g_12, - const FieldMetric& g_13, const FieldMetric& g_23); - CovariantMetricTensor(const BoutReal g_11, const BoutReal g_22, const BoutReal g_33, - const BoutReal g_12, const BoutReal g_13, const BoutReal g_23, - Mesh* mesh); - - /// Invert contravariant metric to get covariant components - void CalculateOppositeRepresentation(MetricTensor& contravariantMetricTensor, - const CELL_LOC location, - const std::string& region = "RGN_ALL") override; -}; - -#endif //BOUT_COVARIANTMETRICTENSOR_HXX diff --git a/include/bout/fv_ops.hxx b/include/bout/fv_ops.hxx index 162548d917..958fbf80f0 100644 --- a/include/bout/fv_ops.hxx +++ b/include/bout/fv_ops.hxx @@ -243,9 +243,8 @@ const Field3D Div_par(const Field3D& f_in, const Field3D& v_in, // Pre-calculate factors which multiply fluxes #if not(BOUT_USE_METRIC_3D) // For right cell boundaries - BoutReal common_factor = - (coord->J(i, j) + coord->J(i, j + 1)) - / (sqrt(coord->g_22()(i, j)) + sqrt(coord->g_22()(i, j + 1))); + BoutReal common_factor = (coord->J(i, j) + coord->J(i, j + 1)) + / (sqrt(coord->g_22()(i, j)) + sqrt(coord->g_22()(i, j + 1))); BoutReal flux_factor_rc = common_factor / (coord->dy(i, j) * coord->J(i, j)); BoutReal flux_factor_rp = diff --git a/include/bout/metricTensor.hxx b/include/bout/metricTensor.hxx index 17db1af321..f58530ba30 100644 --- a/include/bout/metricTensor.hxx +++ b/include/bout/metricTensor.hxx @@ -4,8 +4,6 @@ #include "bout/field2d.hxx" #include "bout/field3d.hxx" -//#include "bout/paralleltransform.hxx" -//#include "bout/utils.hxx" #include class MetricTensor { @@ -39,9 +37,9 @@ public: void setLocation(const CELL_LOC location); - virtual void CalculateOppositeRepresentation(MetricTensor& covariantMetricTensor, - CELL_LOC location, - const std::string& region) = 0; + void CalculateOppositeRepresentation(MetricTensor& contravariantMetricTensor, + const CELL_LOC location, + const std::string& region = "RGN_ALL"); protected: FieldMetric g11, g22, g33, g12, g13, g23; diff --git a/src/mesh/contravariantMetricTensor.cxx b/src/mesh/contravariantMetricTensor.cxx deleted file mode 100644 index 5a1bb6035a..0000000000 --- a/src/mesh/contravariantMetricTensor.cxx +++ /dev/null @@ -1,75 +0,0 @@ - -#include "bout/contravariantMetricTensor.hxx" -#include "bout/covariantMetricTensor.hxx" -#include "bout/output.hxx" - -ContravariantMetricTensor::ContravariantMetricTensor( - const FieldMetric& g11, const FieldMetric& g22, const FieldMetric& g33, - const FieldMetric& g12, const FieldMetric& g13, const FieldMetric& g23) - : MetricTensor::MetricTensor(g11, g22, g33, g12, g13, g23) {} - -ContravariantMetricTensor::ContravariantMetricTensor( - const BoutReal g11, const BoutReal g22, const BoutReal g33, const BoutReal g12, - const BoutReal g13, const BoutReal g23, Mesh* mesh) - : MetricTensor::MetricTensor(g11, g22, g33, g12, g13, g23, mesh) {} - -void ContravariantMetricTensor::CalculateOppositeRepresentation( - MetricTensor& covariantMetricTensor, CELL_LOC location, const std::string& region) { - - TRACE("ContravariantMetricTensor::CalculateOppositeRepresentation"); - - // Perform inversion of g{ij} to get g^{ij} - // NOTE: Currently this bit assumes that metric terms are Field2D objects - - auto a = Matrix(3, 3); - - BOUT_FOR_SERIAL(i, covariantMetricTensor.Getg11().getRegion(region)) { - a(0, 0) = covariantMetricTensor.Getg11()[i]; - a(1, 1) = covariantMetricTensor.Getg22()[i]; - a(2, 2) = covariantMetricTensor.Getg33()[i]; - - a(0, 1) = a(1, 0) = covariantMetricTensor.Getg12()[i]; - a(1, 2) = a(2, 1) = covariantMetricTensor.Getg23()[i]; - a(0, 2) = a(2, 0) = covariantMetricTensor.Getg13()[i]; - - if (invert3x3(a)) { - const auto error_message = "\tERROR: metric tensor is singular at ({:d}, {:d})\n"; - output_error.write(error_message, i.x(), i.y()); - throw BoutException(error_message); - } - } - - g11 = a(0, 0); - g22 = a(1, 1); - g33 = a(2, 2); - g12 = a(0, 1); - g13 = a(0, 2); - g23 = a(1, 2); - // contravariant_components = ContravariantComponents{a(0, 0), a(1, 1), a(2, 2), a(0, 1), a(0, 2), a(1, 2)}; - - setLocation(location); - - BoutReal maxerr; - maxerr = BOUTMAX( - max(abs((covariantMetricTensor.Getg11() * g11 + covariantMetricTensor.Getg12() * g12 - + covariantMetricTensor.Getg13() * g13) - - 1)), - max(abs((covariantMetricTensor.Getg12() * g12 + covariantMetricTensor.Getg22() * g22 - + covariantMetricTensor.Getg23() * g23) - - 1)), - max(abs((covariantMetricTensor.Getg13() * g13 + covariantMetricTensor.Getg23() * g23 - + covariantMetricTensor.Getg33() * g33) - - 1))); - - output_info.write("\tMaximum error in diagonal inversion is {:e}\n", maxerr); - - maxerr = BOUTMAX( - max(abs(covariantMetricTensor.Getg11() * g12 + covariantMetricTensor.Getg12() * g22 - + covariantMetricTensor.Getg13() * g23)), - max(abs(covariantMetricTensor.Getg11() * g13 + covariantMetricTensor.Getg12() * g23 - + covariantMetricTensor.Getg13() * g33)), - max(abs(covariantMetricTensor.Getg12() * g13 + covariantMetricTensor.Getg22() * g23 - + covariantMetricTensor.Getg23() * g33))); - - output_info.write("\tMaximum error in off-diagonal inversion is {:e}\n", maxerr); -} diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 785cbd97cc..d6f9893ef5 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -19,7 +19,6 @@ #include "parallel/fci.hxx" #include "parallel/shiftedmetricinterp.hxx" -#include "bout/contravariantMetricTensor.hxx" // use anonymous namespace so this utility function is not available outside this file namespace { @@ -485,7 +484,7 @@ Coordinates::Coordinates(Mesh* mesh, Options* options) g13 = getUnalignedAtLocation("g13", 0.0); g23 = getUnalignedAtLocation("g23", 0.0); - setMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); + setContravariantMetricTensor(MetricTensor(g11, g22, g33, g12, g13, g23)); // Check input metrics checkContravariant(); @@ -511,7 +510,7 @@ Coordinates::Coordinates(Mesh* mesh, Options* options) g_23 = getUnaligned("g_23", 0.0); covariantMetricTensor.setMetricTensor( - CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); output_warn.write("\tWARNING! Covariant components of metric tensor set manually. " "Contravariant components NOT recalculated\n"); @@ -534,7 +533,7 @@ Coordinates::Coordinates(Mesh* mesh, Options* options) throw BoutException("Error in calcCovariant call"); } } - + FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; // More robust to extrapolate derived quantities directly, rather than @@ -552,8 +551,7 @@ Coordinates::Coordinates(Mesh* mesh, Options* options) g_23 = interpolateAndExtrapolate(covariantMetricTensor.Getg23(), location, extrapolate_x, extrapolate_y, false, transform.get()); - covariantMetricTensor.setMetricTensor( - CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + covariantMetricTensor.setMetricTensor(MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); // Check covariant metrics checkCovariant(); @@ -707,7 +705,7 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, g23 = getAtLocAndFillGuards(mesh, "g23", suffix, location, 0.0, extrapolate_x, extrapolate_y, false, transform.get()); - setMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); + setContravariantMetricTensor(MetricTensor(g11, g22, g33, g12, g13, g23)); // Check input metrics checkContravariant(); @@ -733,7 +731,7 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, g_22 = getAtLoc(mesh, "g_13", suffix, location); g_33 = getAtLoc(mesh, "g_23", suffix, location); covariantMetricTensor.setMetricTensor( - CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); output_warn.write( "\tWARNING! Staggered covariant components of metric tensor set manually. " @@ -905,7 +903,7 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, false, transform.get()); covariantMetricTensor.setMetricTensor( - CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); // Check input metrics checkContravariant(); @@ -954,7 +952,7 @@ void Coordinates::interpolateAndExtrapolateContravariantMetricTensor( g23 = interpolateAndExtrapolate(coords_in->g23(), location, true, true, false, transform.get(), region); - setMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23), region); + setContravariantMetricTensor(MetricTensor(g11, g22, g33, g12, g13, g23), region); } void Coordinates::outputVars(Options& output_options) { @@ -1385,15 +1383,15 @@ void Coordinates::CalculateChristoffelSymbols() { + 0.5 * contravariantMetricTensor.Getg33() * DDY(covariantMetricTensor.Getg33()); } -CovariantMetricTensor Coordinates::calcCovariant(const std::string& region) { - TRACE("Coordinates::CalculateOppositeRepresentation"); +MetricTensor Coordinates::calcCovariant(const std::string& region) { + TRACE("Coordinates::calcCovariant"); covariantMetricTensor.CalculateOppositeRepresentation(contravariantMetricTensor, location, region); return covariantMetricTensor; } -ContravariantMetricTensor Coordinates::calcContravariant(const std::string& region) { - TRACE("Coordinates::CalculateOppositeRepresentation"); +MetricTensor Coordinates::calcContravariant(const std::string& region) { + TRACE("Coordinates::calcContravariant"); contravariantMetricTensor.CalculateOppositeRepresentation(covariantMetricTensor, location, region); return contravariantMetricTensor; @@ -2012,53 +2010,54 @@ void Coordinates::checkContravariant() { contravariantMetricTensor.check(localmesh->ystart); } -void Coordinates::setMetricTensor(ContravariantMetricTensor metric_tensor, - const std::string& region) { +void Coordinates::setContravariantMetricTensor(MetricTensor metric_tensor, + const std::string& region) { contravariantMetricTensor.setMetricTensor(metric_tensor); covariantMetricTensor.CalculateOppositeRepresentation(contravariantMetricTensor, location, region); } -const CovariantMetricTensor::FieldMetric& Coordinates::g_11() const { +void Coordinates::setCovariantMetricTensor(MetricTensor metric_tensor, + const std::string& region) { + covariantMetricTensor.setMetricTensor(metric_tensor); + contravariantMetricTensor.CalculateOppositeRepresentation(covariantMetricTensor, + location, region); +} + +const MetricTensor::FieldMetric& Coordinates::g_11() const { return covariantMetricTensor.Getg11(); } -const CovariantMetricTensor::FieldMetric& Coordinates::g_22() const { +const MetricTensor::FieldMetric& Coordinates::g_22() const { return covariantMetricTensor.Getg22(); } -const CovariantMetricTensor::FieldMetric& Coordinates::g_33() const { +const MetricTensor::FieldMetric& Coordinates::g_33() const { return covariantMetricTensor.Getg33(); } -const CovariantMetricTensor::FieldMetric& Coordinates::g_12() const { +const MetricTensor::FieldMetric& Coordinates::g_12() const { return covariantMetricTensor.Getg12(); } -const CovariantMetricTensor::FieldMetric& Coordinates::g_13() const { +const MetricTensor::FieldMetric& Coordinates::g_13() const { return covariantMetricTensor.Getg13(); } -const CovariantMetricTensor::FieldMetric& Coordinates::g_23() const { +const MetricTensor::FieldMetric& Coordinates::g_23() const { return covariantMetricTensor.Getg23(); } -const ContravariantMetricTensor::FieldMetric& Coordinates::g11() const { +const MetricTensor::FieldMetric& Coordinates::g11() const { return contravariantMetricTensor.Getg11(); } -const ContravariantMetricTensor::FieldMetric& Coordinates::g22() const { +const MetricTensor::FieldMetric& Coordinates::g22() const { return contravariantMetricTensor.Getg22(); } -const ContravariantMetricTensor::FieldMetric& Coordinates::g33() const { +const MetricTensor::FieldMetric& Coordinates::g33() const { return contravariantMetricTensor.Getg33(); } -const ContravariantMetricTensor::FieldMetric& Coordinates::g12() const { +const MetricTensor::FieldMetric& Coordinates::g12() const { return contravariantMetricTensor.Getg12(); } -const ContravariantMetricTensor::FieldMetric& Coordinates::g13() const { +const MetricTensor::FieldMetric& Coordinates::g13() const { return contravariantMetricTensor.Getg13(); } -const ContravariantMetricTensor::FieldMetric& Coordinates::g23() const { +const MetricTensor::FieldMetric& Coordinates::g23() const { return contravariantMetricTensor.Getg23(); } - -void Coordinates::setMetricTensor(CovariantMetricTensor metric_tensor) { - covariantMetricTensor.setMetricTensor(metric_tensor); - contravariantMetricTensor.CalculateOppositeRepresentation(covariantMetricTensor, - location); -} \ No newline at end of file diff --git a/src/mesh/coordinates_accessor.cxx b/src/mesh/coordinates_accessor.cxx index be885a0d4e..55ee8b8be8 100644 --- a/src/mesh/coordinates_accessor.cxx +++ b/src/mesh/coordinates_accessor.cxx @@ -1,6 +1,5 @@ #include "bout/coordinates_accessor.hxx" - -#include "bout/contravariantMetricTensor.hxx" +#include "bout/metricTensor.hxx" #include "bout/mesh.hxx" #include diff --git a/src/mesh/covariantMetricTensor.cxx b/src/mesh/covariantMetricTensor.cxx deleted file mode 100644 index f7aa40cb25..0000000000 --- a/src/mesh/covariantMetricTensor.cxx +++ /dev/null @@ -1,81 +0,0 @@ - -#include "bout/covariantMetricTensor.hxx" -#include "bout/contravariantMetricTensor.hxx" -#include "bout/coordinates.hxx" -#include "bout/output.hxx" - -CovariantMetricTensor::CovariantMetricTensor( - const FieldMetric& g_11, const FieldMetric& g_22, const FieldMetric& g_33, - const FieldMetric& g_12, const FieldMetric& g_13, const FieldMetric& g_23) - : MetricTensor::MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23) {} - -CovariantMetricTensor::CovariantMetricTensor(const BoutReal g_11, const BoutReal g_22, - const BoutReal g_33, const BoutReal g_12, - const BoutReal g_13, const BoutReal g_23, - Mesh* mesh) - : MetricTensor::MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23, mesh) {} - -void CovariantMetricTensor::CalculateOppositeRepresentation( - MetricTensor& contravariantMetricTensor, const CELL_LOC location, - const std::string& region) { - TRACE("CovariantMetricTensor::CalculateOppositeRepresentation"); - - // Perform inversion of g^{ij} to get g{ij} - // NOTE: Currently this bit assumes that metric terms are Field2D objects - - auto a = Matrix(3, 3); - - BOUT_FOR_SERIAL(i, contravariantMetricTensor.Getg11().getRegion(region)) { - a(0, 0) = contravariantMetricTensor.Getg11()[i]; - a(1, 1) = contravariantMetricTensor.Getg22()[i]; - a(2, 2) = contravariantMetricTensor.Getg33()[i]; - - a(0, 1) = a(1, 0) = contravariantMetricTensor.Getg12()[i]; - a(1, 2) = a(2, 1) = contravariantMetricTensor.Getg23()[i]; - a(0, 2) = a(2, 0) = contravariantMetricTensor.Getg13()[i]; - - if (invert3x3(a)) { - const auto error_message = "\tERROR: metric tensor is singular at ({:d}, {:d})\n"; - output_error.write(error_message, i.x(), i.y()); - throw BoutException(error_message); - } - } - - g11 = a(0, 0); - g22 = a(1, 1); - g33 = a(2, 2); - g12 = a(0, 1); - g13 = a(0, 2); - g23 = a(1, 2); - // covariant_components = CovariantComponents{(a(0, 0), a(1, 1), a(2, 2), a(0, 1), a(0, 2), a(1, 2))}; - - setLocation(location); - - BoutReal maxerr; - maxerr = BOUTMAX(max(abs((g11 * contravariantMetricTensor.Getg11() - + g12 * contravariantMetricTensor.Getg12() - + g13 * contravariantMetricTensor.Getg13()) - - 1)), - max(abs((g12 * contravariantMetricTensor.Getg12() - + g22 * contravariantMetricTensor.Getg22() - + g23 * contravariantMetricTensor.Getg23()) - - 1)), - max(abs((g13 * contravariantMetricTensor.Getg13() - + g23 * contravariantMetricTensor.Getg23() - + g33 * contravariantMetricTensor.Getg33()) - - 1))); - - output_info.write("\tLocal maximum error in diagonal inversion is {:e}\n", maxerr); - - maxerr = BOUTMAX(max(abs(g11 * contravariantMetricTensor.Getg12() - + g12 * contravariantMetricTensor.Getg22() - + g13 * contravariantMetricTensor.Getg23())), - max(abs(g11 * contravariantMetricTensor.Getg13() - + g12 * contravariantMetricTensor.Getg23() - + g13 * contravariantMetricTensor.Getg33())), - max(abs(g12 * contravariantMetricTensor.Getg13() - + g22 * contravariantMetricTensor.Getg23() - + g23 * contravariantMetricTensor.Getg33()))); - - output_info.write("\tLocal maximum error in off-diagonal inversion is {:e}\n", maxerr); -} diff --git a/src/mesh/metricTensor.cxx b/src/mesh/metricTensor.cxx index 505cbf63ba..e82a1d1975 100644 --- a/src/mesh/metricTensor.cxx +++ b/src/mesh/metricTensor.cxx @@ -1,5 +1,6 @@ #include "bout/metricTensor.hxx" +#include "bout/output.hxx" MetricTensor::MetricTensor(const FieldMetric& g11, const FieldMetric& g22, const FieldMetric& g33, const FieldMetric& g12, @@ -105,3 +106,65 @@ void MetricTensor::setLocation(const CELL_LOC location) { g13.setLocation(location); g23.setLocation(location); } + +void MetricTensor::CalculateOppositeRepresentation(MetricTensor& originalMetricTensor, + CELL_LOC location, + const std::string& region) { + + TRACE("MetricTensor::CalculateOppositeRepresentation"); + + // Perform inversion of g{ij} to get g^{ij}, or vice versa + // NOTE: Currently this bit assumes that metric terms are Field2D objects + + auto a = Matrix(3, 3); + + BOUT_FOR_SERIAL(i, originalMetricTensor.Getg11().getRegion(region)) { + a(0, 0) = originalMetricTensor.Getg11()[i]; + a(1, 1) = originalMetricTensor.Getg22()[i]; + a(2, 2) = originalMetricTensor.Getg33()[i]; + + a(0, 1) = a(1, 0) = originalMetricTensor.Getg12()[i]; + a(1, 2) = a(2, 1) = originalMetricTensor.Getg23()[i]; + a(0, 2) = a(2, 0) = originalMetricTensor.Getg13()[i]; + + if (invert3x3(a)) { + const auto error_message = "\tERROR: metric tensor is singular at ({:d}, {:d})\n"; + output_error.write(error_message, i.x(), i.y()); + throw BoutException(error_message); + } + } + + g11 = a(0, 0); + g22 = a(1, 1); + g33 = a(2, 2); + g12 = a(0, 1); + g13 = a(0, 2); + g23 = a(1, 2); + // contravariant_components = ContravariantComponents{a(0, 0), a(1, 1), a(2, 2), a(0, 1), a(0, 2), a(1, 2)}; + + setLocation(location); + + BoutReal maxerr; + maxerr = BOUTMAX( + max(abs((originalMetricTensor.Getg11() * g11 + originalMetricTensor.Getg12() * g12 + + originalMetricTensor.Getg13() * g13) + - 1)), + max(abs((originalMetricTensor.Getg12() * g12 + originalMetricTensor.Getg22() * g22 + + originalMetricTensor.Getg23() * g23) + - 1)), + max(abs((originalMetricTensor.Getg13() * g13 + originalMetricTensor.Getg23() * g23 + + originalMetricTensor.Getg33() * g33) + - 1))); + + output_info.write("\tMaximum error in diagonal inversion is {:e}\n", maxerr); + + maxerr = BOUTMAX( + max(abs(originalMetricTensor.Getg11() * g12 + originalMetricTensor.Getg12() * g22 + + originalMetricTensor.Getg13() * g23)), + max(abs(originalMetricTensor.Getg11() * g13 + originalMetricTensor.Getg12() * g23 + + originalMetricTensor.Getg13() * g33)), + max(abs(originalMetricTensor.Getg12() * g13 + originalMetricTensor.Getg22() * g23 + + originalMetricTensor.Getg23() * g33))); + + output_info.write("\tMaximum error in off-diagonal inversion is {:e}\n", maxerr); +} diff --git a/tests/MMS/GBS/gbs.cxx b/tests/MMS/GBS/gbs.cxx index 2a167d0c70..6d90ded097 100644 --- a/tests/MMS/GBS/gbs.cxx +++ b/tests/MMS/GBS/gbs.cxx @@ -354,8 +354,8 @@ void GBS::LoadMetric(BoutReal Lnorm, BoutReal Bnorm) { const auto g12 = 0.0; const auto g13 = -sinty * coords->g11(); const auto g23 = -sbp * Btxy / (hthe * Bpxy * Rxy); - coords->setMetricTensor( - ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); + coords->setContravariantMetricTensor( + MetricTensor(g11, g22, g33, g12, g13, g23)); coords->J = hthe / Bpxy; @@ -365,8 +365,8 @@ void GBS::LoadMetric(BoutReal Lnorm, BoutReal Bnorm) { const auto g_12 = sbp * Btxy * hthe * sinty * Rxy / Bpxy; const auto g_13 = sinty * Rxy * Rxy; const auto g_23 = sbp * Btxy * hthe * Rxy / Bpxy; - coords->setMetricTensor( - CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + coords->setCovariantMetricTensor( + MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); coords->geometry(); } diff --git a/tests/MMS/diffusion/diffusion.cxx b/tests/MMS/diffusion/diffusion.cxx index aba31de755..d6a40f81c2 100644 --- a/tests/MMS/diffusion/diffusion.cxx +++ b/tests/MMS/diffusion/diffusion.cxx @@ -44,11 +44,11 @@ int Diffusion::init(bool UNUSED(restarting)) { //set mesh auto contravariant_metric_tensor = - ContravariantMetricTensor(1.1, 1.1, 1.1, 0.0, 0.0, 0.0); - coord->setMetricTensor(contravariant_metric_tensor); + MetricTensor(1.1, 1.1, 1.1, 0.0, 0.0, 0.0); + coord->setContravariantMetricTensor(contravariant_metric_tensor); - auto covariant_metric_tensor = CovariantMetricTensor(1.1, 1.1, 1.1, 0.0, 0.0, 0.0); - coord->setMetricTensor(covariant_metric_tensor); + auto covariant_metric_tensor = MetricTensor(1.1, 1.1, 1.1, 0.0, 0.0, 0.0); + coord->setCovariantMetricTensor(covariant_metric_tensor); coord->geometry(); diff --git a/tests/MMS/diffusion2/diffusion.cxx b/tests/MMS/diffusion2/diffusion.cxx index 7c136f4e7f..c7508b558d 100644 --- a/tests/MMS/diffusion2/diffusion.cxx +++ b/tests/MMS/diffusion2/diffusion.cxx @@ -39,11 +39,11 @@ class Diffusion : public PhysicsModel { // set mesh auto contravariant_metric_tensor = - ContravariantMetricTensor(1.1, 1.1, 1.1, 0.0, 0.0, 0.0); - coords->setMetricTensor(contravariant_metric_tensor); + MetricTensor(1.1, 1.1, 1.1, 0.0, 0.0, 0.0); + coords->setContravariantMetricTensor(contravariant_metric_tensor); - auto covariant_metric_tensor = CovariantMetricTensor(1.1, 1.1, 1.1, 0.0, 0.0, 0.0); - coords->setMetricTensor(covariant_metric_tensor); + auto covariant_metric_tensor = MetricTensor(1.1, 1.1, 1.1, 0.0, 0.0, 0.0); + coords->setCovariantMetricTensor(covariant_metric_tensor); coords->geometry(); diff --git a/tests/MMS/elm-pb/elm_pb.cxx b/tests/MMS/elm-pb/elm_pb.cxx index 1664479a52..0a5fa4be51 100644 --- a/tests/MMS/elm-pb/elm_pb.cxx +++ b/tests/MMS/elm-pb/elm_pb.cxx @@ -423,8 +423,8 @@ class ELMpb : public PhysicsModel { const auto g12 = 0.0; const auto g13 = -I * coords->g11(); const auto g23 = -Btxy / (hthe * Bpxy * Rxy); - coords->setMetricTensor( - ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); + coords->setContravariantMetricTensor( + MetricTensor(g11, g22, g33, g12, g13, g23)); coords->J = hthe / Bpxy; coords->Bxy = B0; @@ -435,8 +435,8 @@ class ELMpb : public PhysicsModel { const auto g_12 = Btxy * hthe * I * Rxy / Bpxy; const auto g_13 = I * Rxy * Rxy; const auto g_23 = Btxy * hthe * Rxy / Bpxy; - coords->setMetricTensor( - CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + coords->setCovariantMetricTensor( + MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); coords->geometry(); // Calculate quantities from metric tensor diff --git a/tests/MMS/fieldalign/fieldalign.cxx b/tests/MMS/fieldalign/fieldalign.cxx index 6aa3c2705e..5793eb9d0e 100644 --- a/tests/MMS/fieldalign/fieldalign.cxx +++ b/tests/MMS/fieldalign/fieldalign.cxx @@ -18,17 +18,18 @@ class FieldAlign : public PhysicsModel { f.applyBoundary(t); // df/dt = df/dtheta + df/dphi - - Coordinates::MetricTensor metric_tensor = metric->getContravariantMetricTensor(); + ddt(f) = - vx / G * (metric_tensor.g11 * DDX(f) + metric_tensor.g12 * DDY(f) + metric_tensor.g13 * DDZ(f)) - + vy / G * (metric_tensor.g12 * DDX(f) + metric_tensor.g22 * DDY(f) + metric_tensor.g23 * DDZ(f)) + vx / G + * (metric->g11() * DDX(f) + metric->g12() * DDY(f) + metric->g13() * DDZ(f)) + + vy / G + * (metric->g12() * DDX(f) + metric->g22() * DDY(f) + metric->g23() * DDZ(f)) + // Upwinding with second-order central differencing vz / G - * (metric_tensor.g13 * DDX(f) + metric_tensor.g23 * DDY(f) - + metric_tensor.g33 * DDZ(f)); // (unstable without additional dissipation) - -SQ(SQ(metric->dx)) * D4DX4(f) /*- SQ(SQ(metric->dy))*D4DY4(f)*/ - - SQ(SQ(metric->dz)) * D4DZ4(f); // Numerical dissipation terms + * (metric->g13() * DDX(f) + metric->g23() * DDY(f) + + metric->g33() * DDZ(f)); // (unstable without additional dissipation) + -SQ(SQ(metric->dx)) * D4DX4(f) /*- SQ(SQ(metric->dy))*D4DY4(f)*/ + - SQ(SQ(metric->dz)) * D4DZ4(f); // Numerical dissipation terms return 0; } diff --git a/tests/MMS/spatial/diffusion/diffusion.cxx b/tests/MMS/spatial/diffusion/diffusion.cxx index 878ae776c7..4034255ae4 100644 --- a/tests/MMS/spatial/diffusion/diffusion.cxx +++ b/tests/MMS/spatial/diffusion/diffusion.cxx @@ -40,11 +40,11 @@ class Diffusion : public PhysicsModel { // set mesh auto contravariant_metric_tensor = - ContravariantMetricTensor(1.1, 1.1, 1.1, 0.0, 0.0, 0.0); - coords->setMetricTensor(contravariant_metric_tensor); + MetricTensor(1.1, 1.1, 1.1, 0.0, 0.0, 0.0); + coords->setContravariantMetricTensor(contravariant_metric_tensor); - auto covariant_metric_tensor = CovariantMetricTensor(1.1, 1.1, 1.1, 0.0, 0.0, 0.0); - coords->setMetricTensor(covariant_metric_tensor); + auto covariant_metric_tensor = MetricTensor(1.1, 1.1, 1.1, 0.0, 0.0, 0.0); + coords->setCovariantMetricTensor(covariant_metric_tensor); coords->geometry(); diff --git a/tests/MMS/tokamak/tokamak.cxx b/tests/MMS/tokamak/tokamak.cxx index b750f50f09..a0c1ef2a82 100644 --- a/tests/MMS/tokamak/tokamak.cxx +++ b/tests/MMS/tokamak/tokamak.cxx @@ -86,8 +86,8 @@ class TokamakMMS : public PhysicsModel { const auto g12 = 0.0; const auto g13 = -sinty * coords->g11(); const auto g23 = -sbp * Btxy / (hthe * Bpxy * Rxy); - coords->setMetricTensor( - ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); + coords->setContravariantMetricTensor( + MetricTensor(g11, g22, g33, g12, g13, g23)); coords->J = hthe / Bpxy; @@ -97,8 +97,8 @@ class TokamakMMS : public PhysicsModel { const auto g_12 = sbp * Btxy * hthe * sinty * Rxy / Bpxy; const auto g_13 = sinty * Rxy * Rxy; const auto g_23 = sbp * Btxy * hthe * Rxy / Bpxy; - coords->setMetricTensor( - CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + coords->setCovariantMetricTensor( + MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); coords->geometry(); } diff --git a/tests/MMS/wave-1d/wave.cxx b/tests/MMS/wave-1d/wave.cxx index 3fddf2f3e0..fefd7fc59d 100644 --- a/tests/MMS/wave-1d/wave.cxx +++ b/tests/MMS/wave-1d/wave.cxx @@ -32,13 +32,12 @@ class Wave1D : public PhysicsModel { SAVE_ONCE(Lx, Ly); //set mesh - auto contravariant_metric_tensor = - ContravariantMetricTensor(1.1, 1.1, 1.1, 0.0, 0.0, 0.0); - coord->setMetricTensor(contravariant_metric_tensor); + auto contravariant_metric_tensor = MetricTensor(1.1, 1.1, 1.1, 0.0, 0.0, 0.0); + coord->setContravariantMetricTensor(contravariant_metric_tensor); + + auto covariant_metric_tensor = MetricTensor(1.1, 1.1, 1.1, 0.0, 0.0, 0.0); + coord->setCovariantMetricTensor(covariant_metric_tensor); - auto covariant_metric_tensor = CovariantMetricTensor(1.1, 1.1, 1.1, 0.0, 0.0, 0.0); - coord->setMetricTensor(covariant_metric_tensor); - coord->geometry(); g.setLocation(CELL_XLOW); // g staggered to the left of f diff --git a/tests/integrated/test-drift-instability/2fluid.cxx b/tests/integrated/test-drift-instability/2fluid.cxx index 8b770f420b..f4ec753e60 100644 --- a/tests/integrated/test-drift-instability/2fluid.cxx +++ b/tests/integrated/test-drift-instability/2fluid.cxx @@ -217,27 +217,25 @@ class TwoFluid : public PhysicsModel { /**************** CALCULATE METRICS ******************/ - CovariantMetricTensor::FieldMetric g11, g22, g33, g12, g13, g23; + MetricTensor::FieldMetric g11, g22, g33, g12, g13, g23; g11 = SQ(Rxy * Bpxy); g22 = 1.0 / SQ(hthe); g33 = SQ(I) * coord->g11() + SQ(coord->Bxy) / coord->g11(); g12 = 0.0; g13 = -I * coord->g11(); g23 = -Btxy / (hthe * Bpxy * Rxy); - coord->setMetricTensor( - ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); + coord->setContravariantMetricTensor(MetricTensor(g11, g22, g33, g12, g13, g23)); coord->J = hthe / Bpxy; - CovariantMetricTensor::FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; + MetricTensor::FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; g_11 = 1.0 / coord->g11() + SQ(I * Rxy); g_22 = SQ(coord->Bxy * hthe / Bpxy); g_33 = Rxy * Rxy; g_12 = Btxy * hthe * I * Rxy / Bpxy; g_13 = I * Rxy * Rxy; g_23 = Btxy * hthe * Rxy / Bpxy; - coord->setMetricTensor( - CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + coord->setCovariantMetricTensor(MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); coord->geometry(); diff --git a/tests/integrated/test-interchange-instability/2fluid.cxx b/tests/integrated/test-interchange-instability/2fluid.cxx index 282df1c3fb..fc5311940d 100644 --- a/tests/integrated/test-interchange-instability/2fluid.cxx +++ b/tests/integrated/test-interchange-instability/2fluid.cxx @@ -139,27 +139,27 @@ class Interchange : public PhysicsModel { /**************** CALCULATE METRICS ******************/ - CovariantMetricTensor::FieldMetric g11, g22, g33, g12, g13, g23; + MetricTensor::FieldMetric g11, g22, g33, g12, g13, g23; g11 = SQ(Rxy * Bpxy); g22 = 1.0 / SQ(hthe); g33 = SQ(I) * coord->g11() + SQ(coord->Bxy) / coord->g11(); g12 = 0.0; g13 = -I * coord->g11(); g23 = -Btxy / (hthe * Bpxy * Rxy); - coord->setMetricTensor( - ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); + coord->setContravariantMetricTensor( + MetricTensor(g11, g22, g33, g12, g13, g23)); coord->J = hthe / Bpxy; - CovariantMetricTensor::FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; + MetricTensor::FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; g_11 = 1.0 / coord->g11() + SQ(I * Rxy); g_22 = SQ(coord->Bxy * hthe / Bpxy); g_33 = Rxy * Rxy; g_12 = Btxy * hthe * I * Rxy / Bpxy; g_13 = I * Rxy * Rxy; g_23 = Btxy * hthe * Rxy / Bpxy; - coord->setMetricTensor( - CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + coord->setCovariantMetricTensor( + MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); coord->geometry(); diff --git a/tests/integrated/test-laplacexy/loadmetric.cxx b/tests/integrated/test-laplacexy/loadmetric.cxx index 2aa11b0049..52575eb9e9 100644 --- a/tests/integrated/test-laplacexy/loadmetric.cxx +++ b/tests/integrated/test-laplacexy/loadmetric.cxx @@ -55,8 +55,8 @@ void LoadMetric(BoutReal Lnorm, BoutReal Bnorm) { const auto g12 = 0.0; const auto g13 = -sinty * coords->g11(); const auto g23 = -sbp * Btxy / (hthe * Bpxy * Rxy); - coords->setMetricTensor( - ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); + coords->setContravariantMetricTensor( + MetricTensor(g11, g22, g33, g12, g13, g23)); coords->J = hthe / Bpxy; @@ -66,8 +66,8 @@ void LoadMetric(BoutReal Lnorm, BoutReal Bnorm) { const auto g_12 = sbp * Btxy * hthe * sinty * Rxy / Bpxy; const auto g_13 = sinty * Rxy * Rxy; const auto g_23 = sbp * Btxy * hthe * Rxy / Bpxy; - coords->setMetricTensor( - CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + coords->setCovariantMetricTensor( + MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); coords->geometry(); } diff --git a/tests/integrated/test-laplacexz/test-laplacexz.cxx b/tests/integrated/test-laplacexz/test-laplacexz.cxx index 3c90a276ee..5d6ccddd68 100644 --- a/tests/integrated/test-laplacexz/test-laplacexz.cxx +++ b/tests/integrated/test-laplacexz/test-laplacexz.cxx @@ -21,7 +21,7 @@ int main(int argc, char** argv) { auto coord = bout::globals::mesh->getCoordinates(); const auto g13 = 1.8; // test off-diagonal components with nonzero value - coord->setMetricTensor(ContravariantMetricTensor( + coord->setContravariantMetricTensor(MetricTensor( coord->g11(), coord->g22(), coord->g33(), coord->g12(), g13, coord->g23())); // create some input field @@ -36,7 +36,7 @@ int main(int argc, char** argv) { Field3D f2 = inv->solve(g, 0.0); // Invert the Laplacian. // reset to 0.0 for original laplacexz test - coord->setMetricTensor(ContravariantMetricTensor( + coord->setContravariantMetricTensor(MetricTensor( coord->g11(), coord->g22(), coord->g33(), coord->g12(), 0.0, coord->g23())); // Now the normal test. diff --git a/tests/unit/mesh/test_coordinates.cxx b/tests/unit/mesh/test_coordinates.cxx index 16d948d3ae..d6fd43d7a8 100644 --- a/tests/unit/mesh/test_coordinates.cxx +++ b/tests/unit/mesh/test_coordinates.cxx @@ -358,8 +358,8 @@ TEST_F(CoordinatesTest, SetContravariantMetricTensor) { FieldMetric{0.0}}; // IntShiftTorsion // Modify with setter - auto updated_metric_tensor = ContravariantMetricTensor(1.7, 2.3, 3.1, 0.9, 5.7, 1.9); - coords.setMetricTensor(updated_metric_tensor); + auto updated_metric_tensor = MetricTensor(1.7, 2.3, 3.1, 0.9, 5.7, 1.9); + coords.setContravariantMetricTensor(updated_metric_tensor); // Get values with getter and check they have been modified as expected EXPECT_TRUE(IsFieldEqual(coords.g11(), 1.7)); @@ -425,8 +425,8 @@ TEST_F(CoordinatesTest, SetCovariantMetricTensor) { FieldMetric{0.0}}; // IntShiftTorsion // Modify with setter - auto updated_metric_tensor = CovariantMetricTensor(1.7, 2.3, 3.1, 0.9, 5.7, 1.9); - coords.setMetricTensor(updated_metric_tensor); + auto updated_metric_tensor = MetricTensor(1.7, 2.3, 3.1, 0.9, 5.7, 1.9); + coords.setCovariantMetricTensor(updated_metric_tensor); // Get values with getter and check they have been modified as expected EXPECT_TRUE(IsFieldEqual(coords.g_11(), 1.7)); From 8799208179ba19e83eda0be24e1f877c468a10ac Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 15 Nov 2023 14:19:04 +0000 Subject: [PATCH 130/491] Update more tests. --- examples/2Dturbulence_multigrid/esel.cxx | 30 ++++++++++++--------- examples/6field-simple/elm_6f.cxx | 28 ++++++++++--------- examples/conducting-wall-mode/cwm.cxx | 28 ++++++++++--------- examples/constraints/alfven-wave/alfven.cxx | 28 ++++++++++--------- examples/dalf3/dalf3.cxx | 28 ++++++++++--------- examples/em-drift/2fluid.cxx | 28 ++++++++++--------- examples/gyro-gem/gem.cxx | 28 ++++++++++--------- examples/jorek-compare/jorek_compare.cxx | 28 ++++++++++--------- examples/lapd-drift/lapd_drift.cxx | 28 ++++++++++--------- examples/laplacexy/alfven-wave/alfven.cxx | 30 ++++++++++++--------- examples/laplacexy/laplace_perp/test.cxx | 28 ++++++++++--------- examples/reconnect-2field/2field.cxx | 28 ++++++++++--------- examples/shear-alfven-wave/2fluid.cxx | 28 ++++++++++--------- examples/tokamak-2fluid/2fluid.cxx | 28 ++++++++++--------- 14 files changed, 226 insertions(+), 170 deletions(-) diff --git a/examples/2Dturbulence_multigrid/esel.cxx b/examples/2Dturbulence_multigrid/esel.cxx index 810e1aa018..ff9332f12b 100644 --- a/examples/2Dturbulence_multigrid/esel.cxx +++ b/examples/2Dturbulence_multigrid/esel.cxx @@ -71,19 +71,23 @@ class ESEL : public PhysicsModel { // generate coordinate system coord->Bxy = 1; - coord->g11 = 1.0; - coord->g22 = 1.0; - coord->g33 = 1.0; - coord->g12 = 0.0; - coord->g13 = 0.0; - coord->g23 = 0.0; - - coord->g_11 = 1.0; - coord->g_22 = 1.0; - coord->g_33 = 1.0; - coord->g_12 = 0.0; - coord->g_13 = 0.0; - coord->g_23 = 0.0; + MetricTensor::FieldMetric g11, g22, g33, g12, g13, g23; + g11 = 1.0; + g22 = 1.0; + g33 = 1.0; + g12 = 0.0; + g13 = 0.0; + g23 = 0.0; + coord->setContravariantMetricTensor(MetricTensor(g11, g22, g33, g12, g13, g23)); + + MetricTensor::FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; + g_11 = 1.0; + g_22 = 1.0; + g_33 = 1.0; + g_12 = 0.0; + g_13 = 0.0; + g_23 = 0.0; + coord->setCovariantMetricTensor(MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); coord->geometry(); diff --git a/examples/6field-simple/elm_6f.cxx b/examples/6field-simple/elm_6f.cxx index 40e4f7b3b3..a2fe370025 100644 --- a/examples/6field-simple/elm_6f.cxx +++ b/examples/6field-simple/elm_6f.cxx @@ -1051,22 +1051,26 @@ class Elm_6f : public PhysicsModel { /**************** CALCULATE METRICS ******************/ - coord->g11 = SQ(Rxy * Bpxy); - coord->g22 = 1.0 / SQ(hthe); - coord->g33 = SQ(I) * coord->g11 + SQ(B0) / coord->g11; - coord->g12 = 0.0; - coord->g13 = -I * coord->g11; - coord->g23 = -Btxy / (hthe * Bpxy * Rxy); + MetricTensor::FieldMetric g11, g22, g33, g12, g13, g23; + g11 = SQ(Rxy * Bpxy); + g22 = 1.0 / SQ(hthe); + g33 = SQ(I) * coord->g11() + SQ(B0) / coord->g11(); + g12 = 0.0; + g13 = -I * coord->g11(); + g23 = -Btxy / (hthe * Bpxy * Rxy); + coord->setContravariantMetricTensor(MetricTensor(g11, g22, g33, g12, g13, g23)); coord->J = hthe / Bpxy; coord->Bxy = B0; - coord->g_11 = 1.0 / coord->g11 + SQ(I * Rxy); - coord->g_22 = SQ(B0 * hthe / Bpxy); - coord->g_33 = Rxy * Rxy; - coord->g_12 = Btxy * hthe * I * Rxy / Bpxy; - coord->g_13 = I * Rxy * Rxy; - coord->g_23 = Btxy * hthe * Rxy / Bpxy; + MetricTensor::FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; + g_11 = 1.0 / coord->g11() + SQ(I * Rxy); + g_22 = SQ(B0 * hthe / Bpxy); + g_33 = Rxy * Rxy; + g_12 = Btxy * hthe * I * Rxy / Bpxy; + g_13 = I * Rxy * Rxy; + g_23 = Btxy * hthe * Rxy / Bpxy; + coord->setCovariantMetricTensor(MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); coord->geometry(); // Calculate quantities from metric tensor diff --git a/examples/conducting-wall-mode/cwm.cxx b/examples/conducting-wall-mode/cwm.cxx index 3b3fba1a45..4694824ebd 100644 --- a/examples/conducting-wall-mode/cwm.cxx +++ b/examples/conducting-wall-mode/cwm.cxx @@ -188,21 +188,25 @@ class CWM : public PhysicsModel { /**************** CALCULATE METRICS ******************/ - coord->g11 = SQ(Rxy * Bpxy); - coord->g22 = 1.0 / SQ(hthe); - coord->g33 = SQ(I) * coord->g11 + SQ(coord->Bxy) / coord->g11; - coord->g12 = 0.0; - coord->g13 = -I * coord->g11; - coord->g23 = -Btxy / (hthe * Bpxy * Rxy); + MetricTensor::FieldMetric g11, g22, g33, g12, g13, g23; + g11 = SQ(Rxy * Bpxy); + g22 = 1.0 / SQ(hthe); + g33 = SQ(I) * coord->g11() + SQ(coord->Bxy) / coord->g11(); + g12 = 0.0; + g13 = -I * coord->g11(); + g23 = -Btxy / (hthe * Bpxy * Rxy); + coord->setContravariantMetricTensor(MetricTensor(g11, g22, g33, g12, g13, g23)); coord->J = hthe / Bpxy; - coord->g_11 = 1.0 / coord->g11 + SQ(I * Rxy); - coord->g_22 = SQ(coord->Bxy * hthe / Bpxy); - coord->g_33 = Rxy * Rxy; - coord->g_12 = Btxy * hthe * I * Rxy / Bpxy; - coord->g_13 = I * Rxy * Rxy; - coord->g_23 = Btxy * hthe * Rxy / Bpxy; + MetricTensor::FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; + g_11 = 1.0 / coord->g11() + SQ(I * Rxy); + g_22 = SQ(coord->Bxy * hthe / Bpxy); + g_33 = Rxy * Rxy; + g_12 = Btxy * hthe * I * Rxy / Bpxy; + g_13 = I * Rxy * Rxy; + g_23 = Btxy * hthe * Rxy / Bpxy; + coord->setCovariantMetricTensor(MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); coord->geometry(); diff --git a/examples/constraints/alfven-wave/alfven.cxx b/examples/constraints/alfven-wave/alfven.cxx index 3e643331a1..cac3ea4d40 100644 --- a/examples/constraints/alfven-wave/alfven.cxx +++ b/examples/constraints/alfven-wave/alfven.cxx @@ -201,21 +201,25 @@ class Alfven : public PhysicsModel { // Calculate metric components - coord->g11 = SQ(Rxy * Bpxy); - coord->g22 = 1.0 / SQ(hthe); - coord->g33 = SQ(sinty) * coord->g11 + SQ(coord->Bxy) / coord->g11; - coord->g12 = 0.0; - coord->g13 = -sinty * coord->g11; - coord->g23 = -sbp * Btxy / (hthe * Bpxy * Rxy); + MetricTensor::FieldMetric g11, g22, g33, g12, g13, g23; + g11 = SQ(Rxy * Bpxy); + g22 = 1.0 / SQ(hthe); + g33 = SQ(sinty) * coord->g11() + SQ(coord->Bxy) / coord->g11(); + g12 = 0.0; + g13 = -sinty * coord->g11(); + g23 = -sbp * Btxy / (hthe * Bpxy * Rxy); + coord->setContravariantMetricTensor(MetricTensor(g11, g22, g33, g12, g13, g23)); coord->J = hthe / Bpxy; - coord->g_11 = 1.0 / coord->g11 + SQ(sinty * Rxy); - coord->g_22 = SQ(coord->Bxy * hthe / Bpxy); - coord->g_33 = Rxy * Rxy; - coord->g_12 = sbp * Btxy * hthe * sinty * Rxy / Bpxy; - coord->g_13 = sinty * Rxy * Rxy; - coord->g_23 = sbp * Btxy * hthe * Rxy / Bpxy; + MetricTensor::FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; + g_11 = 1.0 / coord->g11() + SQ(sinty * Rxy); + g_22 = SQ(coord->Bxy * hthe / Bpxy); + g_33 = Rxy * Rxy; + g_12 = sbp * Btxy * hthe * sinty * Rxy / Bpxy; + g_13 = sinty * Rxy * Rxy; + g_23 = sbp * Btxy * hthe * Rxy / Bpxy; + coord->setCovariantMetricTensor(MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); coord->geometry(); } diff --git a/examples/dalf3/dalf3.cxx b/examples/dalf3/dalf3.cxx index 246cda7b74..2fd8e6326e 100644 --- a/examples/dalf3/dalf3.cxx +++ b/examples/dalf3/dalf3.cxx @@ -253,22 +253,26 @@ class DALF3 : public PhysicsModel { /////////////////////////////////////////////////// // CALCULATE METRICS - coord->g11 = SQ(Rxy * Bpxy); - coord->g22 = 1.0 / SQ(hthe); - coord->g33 = SQ(I) * coord->g11 + SQ(B0) / coord->g11; - coord->g12 = 0.0; - coord->g13 = -I * coord->g11; - coord->g23 = -Btxy / (hthe * Bpxy * Rxy); + MetricTensor::FieldMetric g11, g22, g33, g12, g13, g23; + g11 = SQ(Rxy * Bpxy); + g22 = 1.0 / SQ(hthe); + g33 = SQ(I) * coord->g11() + SQ(B0) / coord->g11(); + g12 = 0.0; + g13 = -I * coord->g11(); + g23 = -Btxy / (hthe * Bpxy * Rxy); + coord->setContravariantMetricTensor(MetricTensor(g11, g22, g33, g12, g13, g23)); coord->J = hthe / Bpxy; coord->Bxy = B0; - coord->g_11 = 1.0 / coord->g11 + SQ(I * Rxy); - coord->g_22 = SQ(B0 * hthe / Bpxy); - coord->g_33 = Rxy * Rxy; - coord->g_12 = Btxy * hthe * I * Rxy / Bpxy; - coord->g_13 = I * Rxy * Rxy; - coord->g_23 = Btxy * hthe * Rxy / Bpxy; + MetricTensor::FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; + g_11 = 1.0 / coord->g11() + SQ(I * Rxy); + g_22 = SQ(B0 * hthe / Bpxy); + g_33 = Rxy * Rxy; + g_12 = Btxy * hthe * I * Rxy / Bpxy; + g_13 = I * Rxy * Rxy; + g_23 = Btxy * hthe * Rxy / Bpxy; + coord->setCovariantMetricTensor(MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); coord->geometry(); // Calculate quantities from metric tensor diff --git a/examples/em-drift/2fluid.cxx b/examples/em-drift/2fluid.cxx index 00bd7e206c..fb9280e07f 100644 --- a/examples/em-drift/2fluid.cxx +++ b/examples/em-drift/2fluid.cxx @@ -178,21 +178,25 @@ class EMdrift : public PhysicsModel { /**************** CALCULATE METRICS ******************/ - coord->g11 = SQ(Rxy * Bpxy); - coord->g22 = 1.0 / SQ(hthe); - coord->g33 = SQ(I) * coord->g11 + SQ(coord->Bxy) / coord->g11; - coord->g12 = 0.0; - coord->g13 = -I * coord->g11; - coord->g23 = -Btxy / (hthe * Bpxy * Rxy); + MetricTensor::FieldMetric g11, g22, g33, g12, g13, g23; + g11 = SQ(Rxy * Bpxy); + g22 = 1.0 / SQ(hthe); + g33 = SQ(I) * coord->g11() + SQ(coord->Bxy) / coord->g11(); + g12 = 0.0; + g13 = -I * coord->g11(); + g23 = -Btxy / (hthe * Bpxy * Rxy); + coord->setContravariantMetricTensor(MetricTensor(g11, g22, g33, g12, g13, g23)); coord->J = hthe / Bpxy; - coord->g_11 = 1.0 / coord->g11 + SQ(I * Rxy); - coord->g_22 = SQ(coord->Bxy * hthe / Bpxy); - coord->g_33 = Rxy * Rxy; - coord->g_12 = Btxy * hthe * I * Rxy / Bpxy; - coord->g_13 = I * Rxy * Rxy; - coord->g_23 = Btxy * hthe * Rxy / Bpxy; + MetricTensor::FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; + g_11 = 1.0 / coord->g11() + SQ(I * Rxy); + g_22 = SQ(coord->Bxy * hthe / Bpxy); + g_33 = Rxy * Rxy; + g_12 = Btxy * hthe * I * Rxy / Bpxy; + g_13 = I * Rxy * Rxy; + g_23 = Btxy * hthe * Rxy / Bpxy; + coord->setCovariantMetricTensor(MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); /**************** SET EVOLVING VARIABLES *************/ diff --git a/examples/gyro-gem/gem.cxx b/examples/gyro-gem/gem.cxx index 1a056c1c08..f962b3c44c 100644 --- a/examples/gyro-gem/gem.cxx +++ b/examples/gyro-gem/gem.cxx @@ -369,22 +369,26 @@ class GEM : public PhysicsModel { // Metric components - coord->g11 = SQ(Rxy * Bpxy); - coord->g22 = 1.0 / SQ(hthe); - coord->g33 = SQ(Bxy) / coord->g11; - coord->g12 = 0.0; - coord->g13 = 0.; - coord->g23 = -Btxy / (hthe * Bpxy * Rxy); + MetricTensor::FieldMetric g11, g22, g33, g12, g13, g23; + g11 = SQ(Rxy * Bpxy); + g22 = 1.0 / SQ(hthe); + g33 = SQ(Bxy) / coord->g11(); + g12 = 0.0; + g13 = 0.; + g23 = -Btxy / (hthe * Bpxy * Rxy); + coord->setContravariantMetricTensor(MetricTensor(g11, g22, g33, g12, g13, g23)); coord->J = hthe / Bpxy; coord->Bxy = Bxy; - coord->g_11 = 1.0 / coord->g11; - coord->g_22 = SQ(Bxy * hthe / Bpxy); - coord->g_33 = Rxy * Rxy; - coord->g_12 = 0.; - coord->g_13 = 0.; - coord->g_23 = Btxy * hthe * Rxy / Bpxy; + MetricTensor::FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; + g_11 = 1.0 / coord->g11(); + g_22 = SQ(Bxy * hthe / Bpxy); + g_33 = Rxy * Rxy; + g_12 = 0.; + g_13 = 0.; + g_23 = Btxy * hthe * Rxy / Bpxy; + coord->setCovariantMetricTensor(MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); coord->geometry(); diff --git a/examples/jorek-compare/jorek_compare.cxx b/examples/jorek-compare/jorek_compare.cxx index 5963fbd7ca..b5e8f90684 100644 --- a/examples/jorek-compare/jorek_compare.cxx +++ b/examples/jorek-compare/jorek_compare.cxx @@ -299,22 +299,26 @@ class Jorek : public PhysicsModel { ////////////////////////////////////////////////////////////// // CALCULATE METRICS - coord->g11 = SQ(Rxy * Bpxy); - coord->g22 = 1.0 / SQ(hthe); - coord->g33 = SQ(I) * coord->g11 + SQ(B0) / coord->g11; - coord->g12 = 0.0; - coord->g13 = -I * coord->g11; - coord->g23 = -Btxy / (hthe * Bpxy * Rxy); + MetricTensor::FieldMetric g11, g22, g33, g12, g13, g23; + g11 = SQ(Rxy * Bpxy); + g22 = 1.0 / SQ(hthe); + g33 = SQ(I) * coord->g11() + SQ(B0) / coord->g11(); + g12 = 0.0; + g13 = -I * coord->g11(); + g23 = -Btxy / (hthe * Bpxy * Rxy); + coord->setContravariantMetricTensor(MetricTensor(g11, g22, g33, g12, g13, g23)); coord->J = hthe / Bpxy; coord->Bxy = B0; - coord->g_11 = 1.0 / coord->g11 + SQ(I * Rxy); - coord->g_22 = SQ(B0 * hthe / Bpxy); - coord->g_33 = Rxy * Rxy; - coord->g_12 = Btxy * hthe * I * Rxy / Bpxy; - coord->g_13 = I * Rxy * Rxy; - coord->g_23 = Btxy * hthe * Rxy / Bpxy; + MetricTensor::FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; + g_11 = 1.0 / coord->g11() + SQ(I * Rxy); + g_22 = SQ(B0 * hthe / Bpxy); + g_33 = Rxy * Rxy; + g_12 = Btxy * hthe * I * Rxy / Bpxy; + g_13 = I * Rxy * Rxy; + g_23 = Btxy * hthe * Rxy / Bpxy; + coord->setCovariantMetricTensor(MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); coord->geometry(); // Calculate quantities from metric tensor diff --git a/examples/lapd-drift/lapd_drift.cxx b/examples/lapd-drift/lapd_drift.cxx index b7579dacb1..a61b3536e4 100644 --- a/examples/lapd-drift/lapd_drift.cxx +++ b/examples/lapd-drift/lapd_drift.cxx @@ -332,21 +332,25 @@ class LAPDdrift : public PhysicsModel { /**************** CALCULATE METRICS ******************/ - coord->g11 = SQ(Rxy * Bpxy); - coord->g22 = 1.0 / SQ(hthe); - coord->g33 = SQ(I) * coord->g11 + SQ(coord->Bxy) / coord->g11; - coord->g12 = 0.0; - coord->g13 = -I * coord->g11; - coord->g23 = -Btxy / (hthe * Bpxy * Rxy); + MetricTensor::FieldMetric g11, g22, g33, g12, g13, g23; + g11 = SQ(Rxy * Bpxy); + g22 = 1.0 / SQ(hthe); + g33 = SQ(I) * coord->g11() + SQ(coord->Bxy) / coord->g11(); + g12 = 0.0; + g13 = -I * coord->g11(); + g23 = -Btxy / (hthe * Bpxy * Rxy); + coord->setContravariantMetricTensor(MetricTensor(g11, g22, g33, g12, g13, g23)); coord->J = hthe / Bpxy; - coord->g_11 = 1.0 / coord->g11 + SQ(I * Rxy); - coord->g_22 = SQ(coord->Bxy * hthe / Bpxy); - coord->g_33 = Rxy * Rxy; - coord->g_12 = Btxy * hthe * I * Rxy / Bpxy; - coord->g_13 = I * Rxy * Rxy; - coord->g_23 = Btxy * hthe * Rxy / Bpxy; + MetricTensor::FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; + g_11 = 1.0 / coord->g11() + SQ(I * Rxy); + g_22 = SQ(coord->Bxy * hthe / Bpxy); + g_33 = Rxy * Rxy; + g_12 = Btxy * hthe * I * Rxy / Bpxy; + g_13 = I * Rxy * Rxy; + g_23 = Btxy * hthe * Rxy / Bpxy; + coord->setCovariantMetricTensor(MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); coord->geometry(); diff --git a/examples/laplacexy/alfven-wave/alfven.cxx b/examples/laplacexy/alfven-wave/alfven.cxx index e7a945b3f4..db1e853d9a 100644 --- a/examples/laplacexy/alfven-wave/alfven.cxx +++ b/examples/laplacexy/alfven-wave/alfven.cxx @@ -201,22 +201,26 @@ class Alfven : public PhysicsModel { if (min(Bpxy, true) < 0.0) { sbp = -1.0; } - - coord->g11 = SQ(Rxy * Bpxy); - coord->g22 = 1.0 / SQ(hthe); - coord->g33 = SQ(sinty) * coord->g11 + SQ(coord->Bxy) / coord->g11; - coord->g12 = 0.0; - coord->g13 = -sinty * coord->g11; - coord->g23 = -sbp * Btxy / (hthe * Bpxy * Rxy); + + MetricTensor::FieldMetric g11, g22, g33, g12, g13, g23; + g11 = SQ(Rxy * Bpxy); + g22 = 1.0 / SQ(hthe); + g33 = SQ(sinty) * coord->g11() + SQ(coord->Bxy) / coord->g11(); + g12 = 0.0; + g13 = -sinty * coord->g11(); + g23 = -sbp * Btxy / (hthe * Bpxy * Rxy); + coord->setContravariantMetricTensor(MetricTensor(g11, g22, g33, g12, g13, g23)); coord->J = hthe / Bpxy; - coord->g_11 = 1.0 / coord->g11 + SQ(sinty * Rxy); - coord->g_22 = SQ(coord->Bxy * hthe / Bpxy); - coord->g_33 = Rxy * Rxy; - coord->g_12 = sbp * Btxy * hthe * sinty * Rxy / Bpxy; - coord->g_13 = sinty * Rxy * Rxy; - coord->g_23 = sbp * Btxy * hthe * Rxy / Bpxy; + MetricTensor::FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; + g_11 = 1.0 / coord->g11() + SQ(sinty * Rxy); + g_22 = SQ(coord->Bxy * hthe / Bpxy); + g_33 = Rxy * Rxy; + g_12 = sbp * Btxy * hthe * sinty * Rxy / Bpxy; + g_13 = sinty * Rxy * Rxy; + g_23 = sbp * Btxy * hthe * Rxy / Bpxy; + coord->setCovariantMetricTensor(MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); coord->geometry(); } diff --git a/examples/laplacexy/laplace_perp/test.cxx b/examples/laplacexy/laplace_perp/test.cxx index 7881f4ab82..18bd8839bf 100644 --- a/examples/laplacexy/laplace_perp/test.cxx +++ b/examples/laplacexy/laplace_perp/test.cxx @@ -25,22 +25,26 @@ int main(int argc, char** argv) { Coordinates* coord = mesh->getCoordinates(); // Calculate metrics - coord->g11 = SQ(Rxy * Bpxy); - coord->g22 = 1.0 / SQ(hthe); - coord->g33 = SQ(I) * coord->g11 + SQ(B0) / coord->g11; - coord->g12 = 0.0; - coord->g13 = -I * coord->g11; - coord->g23 = -Btxy / (hthe * Bpxy * Rxy); + MetricTensor::FieldMetric g11, g22, g33, g12, g13, g23; + g11 = SQ(Rxy * Bpxy); + g22 = 1.0 / SQ(hthe); + g33 = SQ(I) * coord->g11() + SQ(B0) / coord->g11(); + g12 = 0.0; + g13 = -I * coord->g11(); + g23 = -Btxy / (hthe * Bpxy * Rxy); + coord->setContravariantMetricTensor(MetricTensor(g11, g22, g33, g12, g13, g23)); coord->J = hthe / Bpxy; coord->Bxy = B0; - coord->g_11 = 1.0 / coord->g11 + SQ(I * Rxy); - coord->g_22 = SQ(B0 * hthe / Bpxy); - coord->g_33 = Rxy * Rxy; - coord->g_12 = Btxy * hthe * I * Rxy / Bpxy; - coord->g_13 = I * Rxy * Rxy; - coord->g_23 = Btxy * hthe * Rxy / Bpxy; + MetricTensor::FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; + g_11 = 1.0 / coord->g11() + SQ(I * Rxy); + g_22 = SQ(B0 * hthe / Bpxy); + g_33 = Rxy * Rxy; + g_12 = Btxy * hthe * I * Rxy / Bpxy; + g_13 = I * Rxy * Rxy; + g_23 = Btxy * hthe * Rxy / Bpxy; + coord->setCovariantMetricTensor(MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); coord->geometry(); } diff --git a/examples/reconnect-2field/2field.cxx b/examples/reconnect-2field/2field.cxx index ed800e18dd..ee46ede474 100644 --- a/examples/reconnect-2field/2field.cxx +++ b/examples/reconnect-2field/2field.cxx @@ -163,21 +163,25 @@ class TwoField : public PhysicsModel { // CALCULATE METRICS - coord->g11 = SQ(Rxy * Bpxy); - coord->g22 = 1.0 / SQ(hthe); - coord->g33 = SQ(coord->Bxy) / coord->g11; - coord->g12 = 0.0; - coord->g13 = 0.; - coord->g23 = -Btxy / (hthe * Bpxy * Rxy); + MetricTensor::FieldMetric g11, g22, g33, g12, g13, g23; + g11 = SQ(Rxy * Bpxy); + g22 = 1.0 / SQ(hthe); + g33 = SQ(coord->Bxy) / coord->g11(); + g12 = 0.0; + g13 = 0.; + g23 = -Btxy / (hthe * Bpxy * Rxy); + coord->setContravariantMetricTensor(MetricTensor(g11, g22, g33, g12, g13, g23)); coord->J = hthe / Bpxy; - coord->g_11 = 1.0 / coord->g11; - coord->g_22 = SQ(coord->Bxy * hthe / Bpxy); - coord->g_33 = Rxy * Rxy; - coord->g_12 = 0.; - coord->g_13 = 0.; - coord->g_23 = Btxy * hthe * Rxy / Bpxy; + MetricTensor::FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; + g_11 = 1.0 / coord->g11(); + g_22 = SQ(coord->Bxy * hthe / Bpxy); + g_33 = Rxy * Rxy; + g_12 = 0.; + g_13 = 0.; + g_23 = Btxy * hthe * Rxy / Bpxy; + coord->setCovariantMetricTensor(MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); coord->geometry(); diff --git a/examples/shear-alfven-wave/2fluid.cxx b/examples/shear-alfven-wave/2fluid.cxx index a38da9a22a..3a2e1a5fce 100644 --- a/examples/shear-alfven-wave/2fluid.cxx +++ b/examples/shear-alfven-wave/2fluid.cxx @@ -182,21 +182,25 @@ class ShearAlfven : public PhysicsModel { /**************** CALCULATE METRICS ******************/ - coord->g11 = SQ(Rxy * Bpxy); - coord->g22 = 1.0 / SQ(hthe); - coord->g33 = SQ(I) * coord->g11 + SQ(coord->Bxy) / coord->g11; - coord->g12 = 0.0; - coord->g13 = -I * coord->g11; - coord->g23 = -Btxy / (hthe * Bpxy * Rxy); + MetricTensor::FieldMetric g11, g22, g33, g12, g13, g23; + g11 = SQ(Rxy * Bpxy); + g22 = 1.0 / SQ(hthe); + g33 = SQ(I) * coord->g11() + SQ(coord->Bxy) / coord->g11(); + g12 = 0.0; + g13 = -I * coord->g11(); + g23 = -Btxy / (hthe * Bpxy * Rxy); + coord->setContravariantMetricTensor(MetricTensor(g11, g22, g33, g12, g13, g23)); coord->J = hthe / Bpxy; - coord->g_11 = 1.0 / coord->g11 + SQ(I * Rxy); - coord->g_22 = SQ(coord->Bxy * hthe / Bpxy); - coord->g_33 = Rxy * Rxy; - coord->g_12 = Btxy * hthe * I * Rxy / Bpxy; - coord->g_13 = I * Rxy * Rxy; - coord->g_23 = Btxy * hthe * Rxy / Bpxy; + MetricTensor::FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; + g_11 = 1.0 / coord->g11() + SQ(I * Rxy); + g_22 = SQ(coord->Bxy * hthe / Bpxy); + g_33 = Rxy * Rxy; + g_12 = Btxy * hthe * I * Rxy / Bpxy; + g_13 = I * Rxy * Rxy; + g_23 = Btxy * hthe * Rxy / Bpxy; + coord->setCovariantMetricTensor(MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); coord->geometry(); diff --git a/examples/tokamak-2fluid/2fluid.cxx b/examples/tokamak-2fluid/2fluid.cxx index 03458d2d4e..fdb9720f39 100644 --- a/examples/tokamak-2fluid/2fluid.cxx +++ b/examples/tokamak-2fluid/2fluid.cxx @@ -399,21 +399,25 @@ class TwoFluid : public PhysicsModel { //////////////////////////////////////////////////////// // CALCULATE METRICS - coord->g11 = SQ(Rxy * Bpxy); - coord->g22 = 1.0 / SQ(hthe); - coord->g33 = SQ(I) * coord->g11 + SQ(coord->Bxy) / coord->g11; - coord->g12 = 0.0; - coord->g13 = -I * coord->g11; - coord->g23 = -Btxy / (hthe * Bpxy * Rxy); + MetricTensor::FieldMetric g11, g22, g33, g12, g13, g23; + g11 = SQ(Rxy * Bpxy); + g22 = 1.0 / SQ(hthe); + g33 = SQ(I) * coord->g11() + SQ(coord->Bxy) / coord->g11(); + g12 = 0.0; + g13 = -I * coord->g11(); + g23 = -Btxy / (hthe * Bpxy * Rxy); + coord->setContravariantMetricTensor(MetricTensor(g11, g22, g33, g12, g13, g23)); coord->J = hthe / Bpxy; - coord->g_11 = 1.0 / coord->g11 + SQ(I * Rxy); - coord->g_22 = SQ(coord->Bxy * hthe / Bpxy); - coord->g_33 = Rxy * Rxy; - coord->g_12 = Btxy * hthe * I * Rxy / Bpxy; - coord->g_13 = I * Rxy * Rxy; - coord->g_23 = Btxy * hthe * Rxy / Bpxy; + MetricTensor::FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; + g_11 = 1.0 / coord->g11() + SQ(I * Rxy); + g_22 = SQ(coord->Bxy * hthe / Bpxy); + g_33 = Rxy * Rxy; + g_12 = Btxy * hthe * I * Rxy / Bpxy; + g_13 = I * Rxy * Rxy; + g_23 = Btxy * hthe * Rxy / Bpxy; + coord->setCovariantMetricTensor(MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); //////////////////////////////////////////////////////// // SET EVOLVING VARIABLES From 888aba40d9262986862dfb6086687268a45313ee Mon Sep 17 00:00:00 2001 From: tomc271 Date: Thu, 16 Nov 2023 10:24:04 +0000 Subject: [PATCH 131/491] Group all public and private members together. --- include/bout/coordinates.hxx | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index c28d576ecb..4c743000e9 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -97,11 +97,6 @@ public: FieldMetric Bxy; ///< Magnitude of B = nabla z times nabla x -private: - MetricTensor contravariantMetricTensor; - MetricTensor covariantMetricTensor; - -public: /// Covariant metric tensor const FieldMetric& g_11() const; const FieldMetric& g_22() const; @@ -272,6 +267,9 @@ private: // check that contravariant tensors are positive (if expected) and finite (always) void checkContravariant(); void interpolateAndExtrapolateContravariantMetricTensor(const Coordinates* coords_in); + + MetricTensor contravariantMetricTensor; + MetricTensor covariantMetricTensor; }; /* From e4b134f55bbfa4714888d0ec6f28e304bb0072f6 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Thu, 16 Nov 2023 11:10:12 +0000 Subject: [PATCH 132/491] MetricTensor::oppositeRepresentation return the calculated opposite representation. --- include/bout/coordinates.hxx | 4 +-- include/bout/metricTensor.hxx | 5 ++- src/mesh/coordinates.cxx | 22 ++++++------- src/mesh/metricTensor.cxx | 62 +++++++++++++++-------------------- 4 files changed, 40 insertions(+), 53 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index 4c743000e9..03f4c0f7a1 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -135,9 +135,9 @@ public: int geometry(bool recalculate_staggered = true, bool force_interpolate_from_centre = false); /// Invert contravariant metric to get covariant components - MetricTensor calcCovariant(const std::string& region = "RGN_ALL"); + void calcCovariant(const std::string& region = "RGN_ALL"); /// Invert covariant metric to get contravariant components - MetricTensor calcContravariant(const std::string& region = "RGN_ALL"); + void calcContravariant(const std::string& region = "RGN_ALL"); int jacobian(); ///< Calculate J and Bxy void CalculateChristoffelSymbols(); /// Calculate Christoffel symbol terms diff --git a/include/bout/metricTensor.hxx b/include/bout/metricTensor.hxx index f58530ba30..c6f2917262 100644 --- a/include/bout/metricTensor.hxx +++ b/include/bout/metricTensor.hxx @@ -37,9 +37,8 @@ public: void setLocation(const CELL_LOC location); - void CalculateOppositeRepresentation(MetricTensor& contravariantMetricTensor, - const CELL_LOC location, - const std::string& region = "RGN_ALL"); + MetricTensor oppositeRepresentation(const CELL_LOC location, + const std::string& region = "RGN_ALL"); protected: FieldMetric g11, g22, g33, g12, g13, g23; diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index d6f9893ef5..b5b3a58258 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -1383,18 +1383,16 @@ void Coordinates::CalculateChristoffelSymbols() { + 0.5 * contravariantMetricTensor.Getg33() * DDY(covariantMetricTensor.Getg33()); } -MetricTensor Coordinates::calcCovariant(const std::string& region) { +void Coordinates::calcCovariant(const std::string& region) { TRACE("Coordinates::calcCovariant"); - covariantMetricTensor.CalculateOppositeRepresentation(contravariantMetricTensor, - location, region); - return covariantMetricTensor; + covariantMetricTensor.setMetricTensor( + contravariantMetricTensor.oppositeRepresentation(location, region)); } -MetricTensor Coordinates::calcContravariant(const std::string& region) { +void Coordinates::calcContravariant(const std::string& region) { TRACE("Coordinates::calcContravariant"); - contravariantMetricTensor.CalculateOppositeRepresentation(covariantMetricTensor, - location, region); - return contravariantMetricTensor; + contravariantMetricTensor.setMetricTensor( + covariantMetricTensor.oppositeRepresentation(location, region)); } int Coordinates::jacobian() { @@ -2013,15 +2011,15 @@ void Coordinates::checkContravariant() { void Coordinates::setContravariantMetricTensor(MetricTensor metric_tensor, const std::string& region) { contravariantMetricTensor.setMetricTensor(metric_tensor); - covariantMetricTensor.CalculateOppositeRepresentation(contravariantMetricTensor, - location, region); + covariantMetricTensor.setMetricTensor( + contravariantMetricTensor.oppositeRepresentation(location, region)); } void Coordinates::setCovariantMetricTensor(MetricTensor metric_tensor, const std::string& region) { covariantMetricTensor.setMetricTensor(metric_tensor); - contravariantMetricTensor.CalculateOppositeRepresentation(covariantMetricTensor, - location, region); + contravariantMetricTensor.setMetricTensor( + covariantMetricTensor.oppositeRepresentation(location, region)); } const MetricTensor::FieldMetric& Coordinates::g_11() const { diff --git a/src/mesh/metricTensor.cxx b/src/mesh/metricTensor.cxx index e82a1d1975..48c3009344 100644 --- a/src/mesh/metricTensor.cxx +++ b/src/mesh/metricTensor.cxx @@ -107,9 +107,8 @@ void MetricTensor::setLocation(const CELL_LOC location) { g23.setLocation(location); } -void MetricTensor::CalculateOppositeRepresentation(MetricTensor& originalMetricTensor, - CELL_LOC location, - const std::string& region) { +MetricTensor MetricTensor::oppositeRepresentation(CELL_LOC location, + const std::string& region) { TRACE("MetricTensor::CalculateOppositeRepresentation"); @@ -118,14 +117,14 @@ void MetricTensor::CalculateOppositeRepresentation(MetricTensor& originalMetricT auto a = Matrix(3, 3); - BOUT_FOR_SERIAL(i, originalMetricTensor.Getg11().getRegion(region)) { - a(0, 0) = originalMetricTensor.Getg11()[i]; - a(1, 1) = originalMetricTensor.Getg22()[i]; - a(2, 2) = originalMetricTensor.Getg33()[i]; + BOUT_FOR_SERIAL(i, g11.getRegion(region)) { + a(0, 0) = g11[i]; + a(1, 1) = g22[i]; + a(2, 2) = g33[i]; - a(0, 1) = a(1, 0) = originalMetricTensor.Getg12()[i]; - a(1, 2) = a(2, 1) = originalMetricTensor.Getg23()[i]; - a(0, 2) = a(2, 0) = originalMetricTensor.Getg13()[i]; + a(0, 1) = a(1, 0) = g12[i]; + a(1, 2) = a(2, 1) = g23[i]; + a(0, 2) = a(2, 0) = g13[i]; if (invert3x3(a)) { const auto error_message = "\tERROR: metric tensor is singular at ({:d}, {:d})\n"; @@ -134,37 +133,28 @@ void MetricTensor::CalculateOppositeRepresentation(MetricTensor& originalMetricT } } - g11 = a(0, 0); - g22 = a(1, 1); - g33 = a(2, 2); - g12 = a(0, 1); - g13 = a(0, 2); - g23 = a(1, 2); - // contravariant_components = ContravariantComponents{a(0, 0), a(1, 1), a(2, 2), a(0, 1), a(0, 2), a(1, 2)}; - - setLocation(location); + FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; + g_11 = a(0, 0); + g_22 = a(1, 1); + g_33 = a(2, 2); + g_12 = a(0, 1); + g_13 = a(0, 2); + g_23 = a(1, 2); BoutReal maxerr; - maxerr = BOUTMAX( - max(abs((originalMetricTensor.Getg11() * g11 + originalMetricTensor.Getg12() * g12 - + originalMetricTensor.Getg13() * g13) - - 1)), - max(abs((originalMetricTensor.Getg12() * g12 + originalMetricTensor.Getg22() * g22 - + originalMetricTensor.Getg23() * g23) - - 1)), - max(abs((originalMetricTensor.Getg13() * g13 + originalMetricTensor.Getg23() * g23 - + originalMetricTensor.Getg33() * g33) - - 1))); + maxerr = BOUTMAX(max(abs((g_11 * g_11 + g_12 * g_12 + g_13 * g_13) - 1)), + max(abs((g_12 * g_12 + g_22 * g_22 + g_23 * g_23) - 1)), + max(abs((g_13 * g_13 + g_23 * g_23 + g_33 * g_33) - 1))); output_info.write("\tMaximum error in diagonal inversion is {:e}\n", maxerr); - maxerr = BOUTMAX( - max(abs(originalMetricTensor.Getg11() * g12 + originalMetricTensor.Getg12() * g22 - + originalMetricTensor.Getg13() * g23)), - max(abs(originalMetricTensor.Getg11() * g13 + originalMetricTensor.Getg12() * g23 - + originalMetricTensor.Getg13() * g33)), - max(abs(originalMetricTensor.Getg12() * g13 + originalMetricTensor.Getg22() * g23 - + originalMetricTensor.Getg23() * g33))); + maxerr = BOUTMAX(max(abs(g_11 * g_12 + g_12 * g_22 + g_13 * g_23)), + max(abs(g_11 * g_13 + g_12 * g_23 + g_13 * g_33)), + max(abs(g_12 * g_13 + g_22 * g_23 + g_23 * g_33))); output_info.write("\tMaximum error in off-diagonal inversion is {:e}\n", maxerr); + + auto other_representation = MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23); + other_representation.setLocation(location); + return other_representation; } From 0932c41f4dd4ddd2c182cd5dede26825198ccd00 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Thu, 16 Nov 2023 15:08:51 +0000 Subject: [PATCH 133/491] Need to specify a Mesh (to avoid it being null). --- include/bout/metricTensor.hxx | 2 +- src/mesh/coordinates.cxx | 8 +++---- src/mesh/metricTensor.cxx | 43 ++++++++++++++++++++++------------- 3 files changed, 32 insertions(+), 21 deletions(-) diff --git a/include/bout/metricTensor.hxx b/include/bout/metricTensor.hxx index c6f2917262..0f424365ca 100644 --- a/include/bout/metricTensor.hxx +++ b/include/bout/metricTensor.hxx @@ -37,7 +37,7 @@ public: void setLocation(const CELL_LOC location); - MetricTensor oppositeRepresentation(const CELL_LOC location, + MetricTensor oppositeRepresentation(const CELL_LOC location, Mesh* mesh, const std::string& region = "RGN_ALL"); protected: diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index b5b3a58258..48d69f6e4c 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -1386,13 +1386,13 @@ void Coordinates::CalculateChristoffelSymbols() { void Coordinates::calcCovariant(const std::string& region) { TRACE("Coordinates::calcCovariant"); covariantMetricTensor.setMetricTensor( - contravariantMetricTensor.oppositeRepresentation(location, region)); + contravariantMetricTensor.oppositeRepresentation(location, localmesh, region)); } void Coordinates::calcContravariant(const std::string& region) { TRACE("Coordinates::calcContravariant"); contravariantMetricTensor.setMetricTensor( - covariantMetricTensor.oppositeRepresentation(location, region)); + covariantMetricTensor.oppositeRepresentation(location, localmesh, region)); } int Coordinates::jacobian() { @@ -2012,14 +2012,14 @@ void Coordinates::setContravariantMetricTensor(MetricTensor metric_tensor, const std::string& region) { contravariantMetricTensor.setMetricTensor(metric_tensor); covariantMetricTensor.setMetricTensor( - contravariantMetricTensor.oppositeRepresentation(location, region)); + contravariantMetricTensor.oppositeRepresentation(location, localmesh, region)); } void Coordinates::setCovariantMetricTensor(MetricTensor metric_tensor, const std::string& region) { covariantMetricTensor.setMetricTensor(metric_tensor); contravariantMetricTensor.setMetricTensor( - covariantMetricTensor.oppositeRepresentation(location, region)); + covariantMetricTensor.oppositeRepresentation(location, localmesh, region)); } const MetricTensor::FieldMetric& Coordinates::g_11() const { diff --git a/src/mesh/metricTensor.cxx b/src/mesh/metricTensor.cxx index 48c3009344..3903079948 100644 --- a/src/mesh/metricTensor.cxx +++ b/src/mesh/metricTensor.cxx @@ -107,7 +107,7 @@ void MetricTensor::setLocation(const CELL_LOC location) { g23.setLocation(location); } -MetricTensor MetricTensor::oppositeRepresentation(CELL_LOC location, +MetricTensor MetricTensor::oppositeRepresentation(CELL_LOC location, Mesh* mesh, const std::string& region) { TRACE("MetricTensor::CalculateOppositeRepresentation"); @@ -133,7 +133,7 @@ MetricTensor MetricTensor::oppositeRepresentation(CELL_LOC location, } } - FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; + BoutReal g_11, g_22, g_33, g_12, g_13, g_23; g_11 = a(0, 0); g_22 = a(1, 1); g_33 = a(2, 2); @@ -141,20 +141,31 @@ MetricTensor MetricTensor::oppositeRepresentation(CELL_LOC location, g_13 = a(0, 2); g_23 = a(1, 2); - BoutReal maxerr; - maxerr = BOUTMAX(max(abs((g_11 * g_11 + g_12 * g_12 + g_13 * g_13) - 1)), - max(abs((g_12 * g_12 + g_22 * g_22 + g_23 * g_23) - 1)), - max(abs((g_13 * g_13 + g_23 * g_23 + g_33 * g_33) - 1))); - - output_info.write("\tMaximum error in diagonal inversion is {:e}\n", maxerr); - - maxerr = BOUTMAX(max(abs(g_11 * g_12 + g_12 * g_22 + g_13 * g_23)), - max(abs(g_11 * g_13 + g_12 * g_23 + g_13 * g_33)), - max(abs(g_12 * g_13 + g_22 * g_23 + g_23 * g_33))); - - output_info.write("\tMaximum error in off-diagonal inversion is {:e}\n", maxerr); - - auto other_representation = MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23); + // BoutReal maxerr; + // maxerr = BOUTMAX( + // max(abs((g_11 * g_11 + g_12 * g_12 + // + g_13 * g_13) + // - 1)), + // max(abs((g_12 * g_12 + g_22 * g_22 + // + g_23 * g_23) + // - 1)), + // max(abs((g_13 * g_13 + g_23 * g_23 + // + g_33 * g_33) + // - 1))); + // + // output_info.write("\tMaximum error in diagonal inversion is {:e}\n", maxerr); + // + // maxerr = BOUTMAX( + // max(abs(g_11 * g_12 + g_12 * g_22 + // + g_13 * g_23)), + // max(abs(g_11 * g_13 + g_12 * g_23 + // + g_13 * g_33)), + // max(abs(g_12 * g_13 + g_22 * g_23 + // + g_23 * g_33))); + // + // output_info.write("\tMaximum error in off-diagonal inversion is {:e}\n", maxerr); + + auto other_representation = MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23, mesh); other_representation.setLocation(location); return other_representation; } From 741a4dee3100cd9a9ca4372b7cbe3abf953dc332 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Thu, 16 Nov 2023 15:54:02 +0000 Subject: [PATCH 134/491] Actually set covariantMetricTensor after recalculating components with interpolateAndExtrapolate function. --- src/mesh/coordinates.cxx | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 48d69f6e4c..9f9f5fcc22 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -782,6 +782,8 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, g_23 = interpolateAndExtrapolate(covariantMetricTensor.Getg23(), location, extrapolate_x, extrapolate_y, false, transform.get()); + covariantMetricTensor.setMetricTensor( + MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); // Check covariant metrics checkCovariant(); From 74a516810506ed514023c81c5b71f8269a2747ad Mon Sep 17 00:00:00 2001 From: tomc271 Date: Sat, 18 Nov 2023 09:51:11 +0000 Subject: [PATCH 135/491] Make jacobian private. --- .../elm-pb-outerloop/elm_pb_outerloop.cxx | 2 +- examples/elm-pb/elm_pb.cxx | 2 +- include/bout/coordinates.hxx | 7 +- include/bout/fv_ops.hxx | 50 ++++---- src/field/vecops.cxx | 56 ++++----- src/field/vector3d.cxx | 6 +- .../impls/cyclic/laplacexz-cyclic.cxx | 8 +- .../pardiv/impls/cyclic/pardiv_cyclic.cxx | 2 +- src/mesh/boundary_standard.cxx | 34 +++--- src/mesh/coordinates.cxx | 107 +++++++++--------- src/mesh/coordinates_accessor.cxx | 2 +- src/mesh/difops.cxx | 14 +-- src/mesh/fv_ops.cxx | 66 +++++------ src/physics/smoothing.cxx | 2 +- tests/unit/mesh/test_coordinates.cxx | 8 +- 15 files changed, 186 insertions(+), 180 deletions(-) diff --git a/examples/elm-pb-outerloop/elm_pb_outerloop.cxx b/examples/elm-pb-outerloop/elm_pb_outerloop.cxx index 8e84901806..18a3acdd56 100644 --- a/examples/elm-pb-outerloop/elm_pb_outerloop.cxx +++ b/examples/elm-pb-outerloop/elm_pb_outerloop.cxx @@ -1099,7 +1099,7 @@ class ELMpb : public PhysicsModel { metric->g13 = -I * metric->g11; metric->g23 = -Btxy / (hthe * Bpxy * Rxy); - metric->J = hthe / Bpxy; + metric->J() = hthe / Bpxy; metric->Bxy = B0; metric->g_11 = 1.0 / metric->g11 + SQ(I * Rxy); diff --git a/examples/elm-pb/elm_pb.cxx b/examples/elm-pb/elm_pb.cxx index e81742747a..91a7e067da 100644 --- a/examples/elm-pb/elm_pb.cxx +++ b/examples/elm-pb/elm_pb.cxx @@ -1040,7 +1040,7 @@ class ELMpb : public PhysicsModel { metric->g13 = -I * metric->g11; metric->g23 = -Btxy / (hthe * Bpxy * Rxy); - metric->J = hthe / Bpxy; + metric->J() = hthe / Bpxy; metric->Bxy = B0; metric->g_11 = 1.0 / metric->g11 + SQ(I * Rxy); diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index 03f4c0f7a1..3b6f78cc45 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -93,8 +93,6 @@ public: /// 2nd-order correction for non-uniform meshes d/di(1/dx), d/di(1/dy) and d/di(1/dz) FieldMetric d1_dx, d1_dy, d1_dz; - FieldMetric J; ///< Coordinate system Jacobian, so volume of cell is J*dx*dy*dz - FieldMetric Bxy; ///< Magnitude of B = nabla z times nabla x /// Covariant metric tensor @@ -113,6 +111,9 @@ public: const FieldMetric& g13() const; const FieldMetric& g23() const; + ///< Coordinate system Jacobian, so volume of cell is J*dx*dy*dz + const FieldMetric J() const; + void setContravariantMetricTensor(MetricTensor metric_tensor, const std::string& region = "RGN_ALL"); @@ -270,6 +271,8 @@ private: MetricTensor contravariantMetricTensor; MetricTensor covariantMetricTensor; + + FieldMetric j; }; /* diff --git a/include/bout/fv_ops.hxx b/include/bout/fv_ops.hxx index 958fbf80f0..ed18b62c68 100644 --- a/include/bout/fv_ops.hxx +++ b/include/bout/fv_ops.hxx @@ -243,20 +243,20 @@ const Field3D Div_par(const Field3D& f_in, const Field3D& v_in, // Pre-calculate factors which multiply fluxes #if not(BOUT_USE_METRIC_3D) // For right cell boundaries - BoutReal common_factor = (coord->J(i, j) + coord->J(i, j + 1)) + BoutReal common_factor = (coord->J()(i, j) + coord->J()(i, j + 1)) / (sqrt(coord->g_22()(i, j)) + sqrt(coord->g_22()(i, j + 1))); - BoutReal flux_factor_rc = common_factor / (coord->dy(i, j) * coord->J(i, j)); + BoutReal flux_factor_rc = common_factor / (coord->dy(i, j) * coord->J()(i, j)); BoutReal flux_factor_rp = - common_factor / (coord->dy(i, j + 1) * coord->J(i, j + 1)); + common_factor / (coord->dy(i, j + 1) * coord->J()(i, j + 1)); // For left cell boundaries - common_factor = (coord->J(i, j) + coord->J(i, j - 1)) + common_factor = (coord->J()(i, j) + coord->J()(i, j - 1)) / (sqrt(coord->g_22()(i, j)) + sqrt(coord->g_22()(i, j - 1))); - BoutReal flux_factor_lc = common_factor / (coord->dy(i, j) * coord->J(i, j)); + BoutReal flux_factor_lc = common_factor / (coord->dy(i, j) * coord->J()(i, j)); BoutReal flux_factor_lm = - common_factor / (coord->dy(i, j - 1) * coord->J(i, j - 1)); + common_factor / (coord->dy(i, j - 1) * coord->J()(i, j - 1)); #endif for (int k = 0; k < mesh->LocalNz; k++) { #if BOUT_USE_METRIC_3D @@ -406,10 +406,10 @@ const Field3D Div_f_v(const Field3D& n_in, const Vector3D& v, bool bndry_flux) { BOUT_FOR(i, result.getRegion("RGN_NOBNDRY")) { // Calculate velocities - BoutReal vU = 0.25 * (vz[i.zp()] + vz[i]) * (coord->J[i.zp()] + coord->J[i]); - BoutReal vD = 0.25 * (vz[i.zm()] + vz[i]) * (coord->J[i.zm()] + coord->J[i]); - BoutReal vL = 0.25 * (vx[i.xm()] + vx[i]) * (coord->J[i.xm()] + coord->J[i]); - BoutReal vR = 0.25 * (vx[i.xp()] + vx[i]) * (coord->J[i.xp()] + coord->J[i]); + BoutReal vU = 0.25 * (vz[i.zp()] + vz[i]) * (coord->J()[i.zp()] + coord->J()[i]); + BoutReal vD = 0.25 * (vz[i.zm()] + vz[i]) * (coord->J()[i.zm()] + coord->J()[i]); + BoutReal vL = 0.25 * (vx[i.xm()] + vx[i]) * (coord->J()[i.xm()] + coord->J()[i]); + BoutReal vR = 0.25 * (vx[i.xp()] + vx[i]) * (coord->J()[i.xp()] + coord->J()[i]); // X direction Stencil1D s; @@ -432,16 +432,16 @@ const Field3D Div_f_v(const Field3D& n_in, const Vector3D& v, bool bndry_flux) { // Flux in from boundary flux = vR * 0.5 * (n[i.xp()] + n[i]); } - result[i] += flux / (coord->dx[i] * coord->J[i]); - result[i.xp()] -= flux / (coord->dx[i.xp()] * coord->J[i.xp()]); + result[i] += flux / (coord->dx[i] * coord->J()[i]); + result[i.xp()] -= flux / (coord->dx[i.xp()] * coord->J()[i.xp()]); } } else { // Not at a boundary if (vR > 0.0) { // Flux out into next cell BoutReal flux = vR * s.R; - result[i] += flux / (coord->dx[i] * coord->J[i]); - result[i.xp()] -= flux / (coord->dx[i.xp()] * coord->J[i.xp()]); + result[i] += flux / (coord->dx[i] * coord->J()[i]); + result[i.xp()] -= flux / (coord->dx[i.xp()] * coord->J()[i.xp()]); } } @@ -459,15 +459,15 @@ const Field3D Div_f_v(const Field3D& n_in, const Vector3D& v, bool bndry_flux) { // Flux in from boundary flux = vL * 0.5 * (n[i.xm()] + n[i]); } - result[i] -= flux / (coord->dx[i] * coord->J[i]); - result[i.xm()] += flux / (coord->dx[i.xm()] * coord->J[i.xm()]); + result[i] -= flux / (coord->dx[i] * coord->J()[i]); + result[i.xm()] += flux / (coord->dx[i.xm()] * coord->J()[i.xm()]); } } else { // Not at a boundary if (vL < 0.0) { BoutReal flux = vL * s.L; - result[i] -= flux / (coord->dx[i] * coord->J[i]); - result[i.xm()] += flux / (coord->dx[i.xm()] * coord->J[i.xm()]); + result[i] -= flux / (coord->dx[i] * coord->J()[i]); + result[i.xm()] += flux / (coord->dx[i.xm()] * coord->J()[i.xm()]); } } @@ -483,13 +483,13 @@ const Field3D Div_f_v(const Field3D& n_in, const Vector3D& v, bool bndry_flux) { if (vU > 0.0) { BoutReal flux = vU * s.R; - result[i] += flux / (coord->J[i] * coord->dz[i]); - result[i.zp()] -= flux / (coord->J[i.zp()] * coord->dz[i.zp()]); + result[i] += flux / (coord->J()[i] * coord->dz[i]); + result[i.zp()] -= flux / (coord->J()[i.zp()] * coord->dz[i.zp()]); } if (vD < 0.0) { BoutReal flux = vD * s.L; - result[i] -= flux / (coord->J[i] * coord->dz[i]); - result[i.zm()] += flux / (coord->J[i.zm()] * coord->dz[i.zm()]); + result[i] -= flux / (coord->J()[i] * coord->dz[i]); + result[i.zm()] += flux / (coord->J()[i.zm()] * coord->dz[i.zm()]); } } @@ -507,15 +507,15 @@ const Field3D Div_f_v(const Field3D& n_in, const Vector3D& v, bool bndry_flux) { BOUT_FOR(i, result.getRegion("RGN_NOBNDRY")) { // Y velocities on y boundaries - BoutReal vU = 0.25 * (vy[i] + vy[i.yp()]) * (coord->J[i] + coord->J[i.yp()]); - BoutReal vD = 0.25 * (vy[i] + vy[i.ym()]) * (coord->J[i] + coord->J[i.ym()]); + BoutReal vU = 0.25 * (vy[i] + vy[i.yp()]) * (coord->J()[i] + coord->J()[i.yp()]); + BoutReal vD = 0.25 * (vy[i] + vy[i.ym()]) * (coord->J()[i] + coord->J()[i.ym()]); // n (advected quantity) on y boundaries // Note: Use unshifted n_in variable BoutReal nU = 0.5 * (n[i] + n[i.yp()]); BoutReal nD = 0.5 * (n[i] + n[i.ym()]); - yresult[i] = (nU * vU - nD * vD) / (coord->J[i] * coord->dy[i]); + yresult[i] = (nU * vU - nD * vD) / (coord->J()[i] * coord->dy[i]); } return result + fromFieldAligned(yresult, "RGN_NOBNDRY"); } diff --git a/src/field/vecops.cxx b/src/field/vecops.cxx index 0e2d59e909..14079d6691 100644 --- a/src/field/vecops.cxx +++ b/src/field/vecops.cxx @@ -106,10 +106,10 @@ Vector3D Grad_perp(const Field3D& f, CELL_LOC outloc, const std::string& method) Vector3D result(f.getMesh()); result.x = DDX(f, outloc, method) - - metric->g_12() * DDY(f, outloc, method) / SQ(metric->J * metric->Bxy); + - metric->g_12() * DDY(f, outloc, method) / SQ(metric->J() * metric->Bxy); result.y = 0.0; result.z = DDZ(f, outloc, method) - - metric->g_23() * DDY(f, outloc, method) / SQ(metric->J * metric->Bxy); + - metric->g_23() * DDY(f, outloc, method) / SQ(metric->J() * metric->Bxy); result.setLocation(result.x.getLocation()); @@ -128,9 +128,9 @@ Vector2D Grad_perp(const Field2D& f, CELL_LOC outloc, const std::string& method) Vector2D result(f.getMesh()); result.x = DDX(f, outloc, method) - - metric->g_12() * DDY(f, outloc, method) / SQ(metric->J * metric->Bxy); + - metric->g_12() * DDY(f, outloc, method) / SQ(metric->J() * metric->Bxy); result.y = 0.0; - result.z = -metric->g_23() * DDY(f, outloc, method) / SQ(metric->J * metric->Bxy); + result.z = -metric->g_23() * DDY(f, outloc, method) / SQ(metric->J() * metric->Bxy); result.setLocation(result.x.getLocation()); @@ -161,10 +161,10 @@ Coordinates::FieldMetric Div(const Vector2D& v, CELL_LOC outloc, Vector2D vcn = v; vcn.toContravariant(); - Coordinates::FieldMetric result = DDX(metric->J * vcn.x, outloc, method); - result += DDY(metric->J * vcn.y, outloc, method); - result += DDZ(metric->J * vcn.z, outloc, method); - result /= metric->J; + Coordinates::FieldMetric result = DDX(metric->J() * vcn.x, outloc, method); + result += DDY(metric->J() * vcn.y, outloc, method); + result += DDZ(metric->J() * vcn.z, outloc, method); + result /= metric->J(); return result; } @@ -187,7 +187,7 @@ Field3D Div(const Vector3D& v, CELL_LOC outloc, const std::string& method) { Vector3D vcn = v; vcn.toContravariant(); - auto vcnJy = vcn.y.getCoordinates()->J * vcn.y; + auto vcnJy = vcn.y.getCoordinates()->J() * vcn.y; if (v.y.hasParallelSlices()) { // If v.y has parallel slices then we are using ShiftedMetric (with // mesh:calcParallelSlices_on_communicate=true) or FCI, so we should calculate @@ -196,9 +196,9 @@ Field3D Div(const Vector3D& v, CELL_LOC outloc, const std::string& method) { } auto result = DDY(vcnJy, outloc, method); - result += DDX(vcn.x.getCoordinates()->J * vcn.x, outloc, method); - result += DDZ(vcn.z.getCoordinates()->J * vcn.z, outloc, method); - result /= metric->J; + result += DDX(vcn.x.getCoordinates()->J() * vcn.x, outloc, method); + result += DDZ(vcn.z.getCoordinates()->J() * vcn.z, outloc, method); + result /= metric->J(); return result; } @@ -226,10 +226,10 @@ Coordinates::FieldMetric Div(const Vector2D& v, const Field2D& f, CELL_LOC outlo vcn.toContravariant(); Coordinates::FieldMetric result = - FDDX(vcn.x.getCoordinates()->J * vcn.x, f, outloc, method); - result += FDDY(vcn.y.getCoordinates()->J * vcn.y, f, outloc, method); - result += FDDZ(vcn.z.getCoordinates()->J * vcn.z, f, outloc, method); - result /= metric->J; + FDDX(vcn.x.getCoordinates()->J() * vcn.x, f, outloc, method); + result += FDDY(vcn.y.getCoordinates()->J() * vcn.y, f, outloc, method); + result += FDDZ(vcn.z.getCoordinates()->J() * vcn.z, f, outloc, method); + result /= metric->J(); return result; } @@ -251,10 +251,10 @@ Field3D Div(const Vector3D& v, const Field3D& f, CELL_LOC outloc, Vector3D vcn = v; vcn.toContravariant(); - Field3D result = FDDX(vcn.x.getCoordinates()->J * vcn.x, f, outloc, method); - result += FDDY(vcn.y.getCoordinates()->J * vcn.y, f, outloc, method); - result += FDDZ(vcn.z.getCoordinates()->J * vcn.z, f, outloc, method); - result /= metric->J; + Field3D result = FDDX(vcn.x.getCoordinates()->J() * vcn.x, f, outloc, method); + result += FDDY(vcn.y.getCoordinates()->J() * vcn.y, f, outloc, method); + result += FDDZ(vcn.z.getCoordinates()->J() * vcn.z, f, outloc, method); + result /= metric->J(); return result; } @@ -277,12 +277,12 @@ Vector2D Curl(const Vector2D& v) { // get components (curl(v))^j Vector2D result(localmesh); - result.x = (DDY(vco.z) - DDZ(vco.y)) / metric->J; - result.y = (DDZ(vco.x) - DDX(vco.z)) / metric->J; - result.z = (DDX(vco.y) - DDY(vco.x)) / metric->J; + result.x = (DDY(vco.z) - DDZ(vco.y)) / metric->J(); + result.y = (DDZ(vco.x) - DDX(vco.z)) / metric->J(); + result.z = (DDX(vco.y) - DDY(vco.x)) / metric->J(); /// Coordinate torsion - result.z -= metric->ShiftTorsion * vco.z / metric->J; + result.z -= metric->ShiftTorsion * vco.z / metric->J(); result.setLocation(v.getLocation()); @@ -305,12 +305,12 @@ Vector3D Curl(const Vector3D& v) { // get components (curl(v))^j Vector3D result(localmesh); - result.x = (DDY(vco.z) - DDZ(vco.y)) / metric->J; - result.y = (DDZ(vco.x) - DDX(vco.z)) / metric->J; - result.z = (DDX(vco.y) - DDY(vco.x)) / metric->J; + result.x = (DDY(vco.z) - DDZ(vco.y)) / metric->J(); + result.y = (DDZ(vco.x) - DDX(vco.z)) / metric->J(); + result.z = (DDX(vco.y) - DDY(vco.x)) / metric->J(); // Coordinate torsion - result.z -= metric->ShiftTorsion * vco.z / metric->J; + result.z -= metric->ShiftTorsion * vco.z / metric->J(); result.setLocation(v.getLocation()); diff --git a/src/field/vector3d.cxx b/src/field/vector3d.cxx index 84b53c2a0a..d972f02e8e 100644 --- a/src/field/vector3d.cxx +++ b/src/field/vector3d.cxx @@ -385,9 +385,9 @@ Vector3D& Vector3D::operator/=(const Field3D& rhs) { Coordinates* metric = localmesh->getCoordinates(lhs.getLocation()); \ \ /* calculate contravariant components of cross-product */ \ - result.x = (lco.y * rco.z - lco.z * rco.y) / metric->J; \ - result.y = (lco.z * rco.x - lco.x * rco.z) / metric->J; \ - result.z = (lco.x * rco.y - lco.y * rco.x) / metric->J; \ + result.x = (lco.y * rco.z - lco.z * rco.y) / metric->J(); \ + result.y = (lco.z * rco.x - lco.x * rco.z) / metric->J(); \ + result.z = (lco.x * rco.y - lco.y * rco.x) / metric->J(); \ result.covariant = false; \ \ return result; \ diff --git a/src/invert/laplacexz/impls/cyclic/laplacexz-cyclic.cxx b/src/invert/laplacexz/impls/cyclic/laplacexz-cyclic.cxx index 104cc4fb55..3f482a167d 100644 --- a/src/invert/laplacexz/impls/cyclic/laplacexz-cyclic.cxx +++ b/src/invert/laplacexz/impls/cyclic/laplacexz-cyclic.cxx @@ -116,23 +116,23 @@ void LaplaceXZcyclic::setCoefs(const Field2D& A2D, const Field2D& B2D) { // XX component // Metrics on x+1/2 boundary - BoutReal J = 0.5 * (coord->J(x, y) + coord->J(x + 1, y)); + BoutReal J = 0.5 * (coord->J()(x, y) + coord->J()(x + 1, y)); BoutReal g11 = 0.5 * (coord->g11()(x, y) + coord->g11()(x + 1, y)); BoutReal dx = 0.5 * (coord->dx(x, y) + coord->dx(x + 1, y)); BoutReal A = 0.5 * (A2D(x, y) + A2D(x + 1, y)); - BoutReal val = A * J * g11 / (coord->J(x, y) * dx * coord->dx(x, y)); + BoutReal val = A * J * g11 / (coord->J()(x, y) * dx * coord->dx(x, y)); ccoef(ind, x - xstart) += val; bcoef(ind, x - xstart) -= val; // Metrics on x-1/2 boundary - J = 0.5 * (coord->J(x, y) + coord->J(x - 1, y)); + J = 0.5 * (coord->J()(x, y) + coord->J()(x - 1, y)); g11 = 0.5 * (coord->g11()(x, y) + coord->g11()(x - 1, y)); dx = 0.5 * (coord->dx(x, y) + coord->dx(x - 1, y)); A = 0.5 * (A2D(x, y) + A2D(x - 1, y)); - val = A * J * g11 / (coord->J(x, y) * dx * coord->dx(x, y)); + val = A * J * g11 / (coord->J()(x, y) * dx * coord->dx(x, y)); acoef(ind, x - xstart) += val; bcoef(ind, x - xstart) -= val; diff --git a/src/invert/pardiv/impls/cyclic/pardiv_cyclic.cxx b/src/invert/pardiv/impls/cyclic/pardiv_cyclic.cxx index 41f17ca597..68f7f034ad 100644 --- a/src/invert/pardiv/impls/cyclic/pardiv_cyclic.cxx +++ b/src/invert/pardiv/impls/cyclic/pardiv_cyclic.cxx @@ -105,7 +105,7 @@ Field3D InvertParDivCR::solve(const Field3D& f) { auto c = Matrix(nsys, size); const Field2D dy = coord->dy; - const Field2D J = coord->J; + const Field2D J = coord->J(); const Field2D g_22 = coord->g_22(); const auto zlength = getUniform(coord->zlength()); diff --git a/src/mesh/boundary_standard.cxx b/src/mesh/boundary_standard.cxx index 313f125ef5..33f08a50de 100644 --- a/src/mesh/boundary_standard.cxx +++ b/src/mesh/boundary_standard.cxx @@ -2973,42 +2973,42 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { // - d/dy( JB^y ) - d/dz( JB^z ) tmp = - -(metric->J(jx, jy) * metric->g12()(jx, jy) * var.y(jx, jy, jz) - + metric->J(jx, jy) * metric->g13()(jx, jy) * var.z(jx, jy, jz) - - metric->J(jx - 2, jy) * metric->g12()(jx - 2, jy) * var.y(jx - 2, jy, jz) - + metric->J(jx - 2, jy) * metric->g13()(jx - 2, jy) * var.z(jx - 2, jy, jz)) + -(metric->J()(jx, jy) * metric->g12()(jx, jy) * var.y(jx, jy, jz) + + metric->J()(jx, jy) * metric->g13()(jx, jy) * var.z(jx, jy, jz) + - metric->J()(jx - 2, jy) * metric->g12()(jx - 2, jy) * var.y(jx - 2, jy, jz) + + metric->J()(jx - 2, jy) * metric->g13()(jx - 2, jy) * var.z(jx - 2, jy, jz)) / (metric->dx(jx - 2, jy) + metric->dx(jx - 1, jy)); // First term (d/dx) using vals calculated above - tmp -= (metric->J(jx - 1, jy + 1) * metric->g12()(jx - 1, jy + 1) + tmp -= (metric->J()(jx - 1, jy + 1) * metric->g12()(jx - 1, jy + 1) * var.x(jx - 1, jy + 1, jz) - - metric->J(jx - 1, jy - 1) * metric->g12()(jx - 1, jy - 1) + - metric->J()(jx - 1, jy - 1) * metric->g12()(jx - 1, jy - 1) * var.x(jx - 1, jy - 1, jz) - + metric->J(jx - 1, jy + 1) * metric->g22()(jx - 1, jy + 1) + + metric->J()(jx - 1, jy + 1) * metric->g22()(jx - 1, jy + 1) * var.y(jx - 1, jy + 1, jz) - - metric->J(jx - 1, jy - 1) * metric->g22()(jx - 1, jy - 1) + - metric->J()(jx - 1, jy - 1) * metric->g22()(jx - 1, jy - 1) * var.y(jx - 1, jy - 1, jz) - + metric->J(jx - 1, jy + 1) * metric->g23()(jx - 1, jy + 1) + + metric->J()(jx - 1, jy + 1) * metric->g23()(jx - 1, jy + 1) * var.z(jx - 1, jy + 1, jz) - - metric->J(jx - 1, jy - 1) * metric->g23()(jx - 1, jy - 1) + - metric->J()(jx - 1, jy - 1) * metric->g23()(jx - 1, jy - 1) * var.z(jx - 1, jy - 1, jz)) / (metric->dy(jx - 1, jy - 1) + metric->dy(jx - 1, jy)); // second (d/dy) - tmp -= (metric->J(jx - 1, jy) * metric->g13()(jx - 1, jy) + tmp -= (metric->J()(jx - 1, jy) * metric->g13()(jx - 1, jy) * (var.x(jx - 1, jy, jzp) - var.x(jx - 1, jy, jzm)) - + metric->J(jx - 1, jy) * metric->g23()(jx - 1, jy) + + metric->J()(jx - 1, jy) * metric->g23()(jx - 1, jy) * (var.y(jx - 1, jy, jzp) - var.y(jx - 1, jy, jzm)) - + metric->J(jx - 1, jy) * metric->g33()(jx - 1, jy) + + metric->J()(jx - 1, jy) * metric->g33()(jx - 1, jy) * (var.z(jx - 1, jy, jzp) - var.z(jx - 1, jy, jzm))) / (2. * metric->dz(jx - 1, jy)); var.x(jx, jy, jz) = - (metric->J(jx - 2, jy) * metric->g11()(jx - 2, jy) * var.x(jx - 2, jy, jz) + (metric->J()(jx - 2, jy) * metric->g11()(jx - 2, jy) * var.x(jx - 2, jy, jz) + (metric->dx(jx - 2, jy) + metric->dx(jx - 1, jy)) * tmp) - / metric->J(jx, jy) * metric->g11()(jx, jy); + / metric->J()(jx, jy) * metric->g11()(jx, jy); if (mesh->xstart == 2) { var.x(jx + 1, jy, jz) = - (metric->J(jx - 3, jy) * metric->g11()(jx - 3, jy) * var.x(jx - 3, jy, jz) + (metric->J()(jx - 3, jy) * metric->g11()(jx - 3, jy) * var.x(jx - 3, jy, jz) + 4. * metric->dx(jx, jy) * tmp) - / metric->J(jx + 1, jy) * metric->g11()(jx + 1, jy); + / metric->J()(jx + 1, jy) * metric->g11()(jx + 1, jy); } } } diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 9f9f5fcc22..3adcde3f67 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -382,7 +382,7 @@ Coordinates::Coordinates(Mesh* mesh, FieldMetric dx, FieldMetric dy, FieldMetric FieldMetric g_33, FieldMetric g_12, FieldMetric g_13, FieldMetric g_23, FieldMetric ShiftTorsion, FieldMetric IntShiftTorsion) - : dx(std::move(dx)), dy(std::move(dy)), dz(dz), J(std::move(J)), Bxy(std::move(Bxy)), + : dx(std::move(dx)), dy(std::move(dy)), dz(dz), j(std::move(J)), Bxy(std::move(Bxy)), contravariantMetricTensor(g11, g22, g33, g12, g13, g23), covariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23), ShiftTorsion(std::move(ShiftTorsion)), IntShiftTorsion(std::move(IntShiftTorsion)), @@ -390,7 +390,7 @@ Coordinates::Coordinates(Mesh* mesh, FieldMetric dx, FieldMetric dy, FieldMetric Coordinates::Coordinates(Mesh* mesh, Options* options) : dx(1., mesh), dy(1., mesh), dz(1., mesh), d1_dx(mesh), d1_dy(mesh), d1_dz(mesh), - J(1., mesh), Bxy(1., mesh), contravariantMetricTensor(1., 1., 1., 0, 0, 0, mesh), + j(1., mesh), Bxy(1., mesh), contravariantMetricTensor(1., 1., 1., 0, 0, 0, mesh), covariantMetricTensor(1., 1., 1., 0, 0, 0, mesh), G1_11(mesh), G1_22(mesh), G1_33(mesh), G1_12(mesh), G1_13(mesh), G1_23(mesh), G2_11(mesh), G2_22(mesh), G2_33(mesh), G2_12(mesh), G2_13(mesh), G2_23(mesh), G3_11(mesh), G3_22(mesh), @@ -562,28 +562,28 @@ Coordinates::Coordinates(Mesh* mesh, Options* options) } // Attempt to read J from the grid file - auto Jcalc = J; - if (mesh->get(J, "J", 0.0, false)) { + auto Jcalc = j; + if (mesh->get(j, "J", 0.0, false)) { output_warn.write( "\tWARNING: Jacobian 'J' not found. Calculating from metric tensor\n"); - J = Jcalc; + j = Jcalc; } else { - J = interpolateAndExtrapolate(J, location, extrapolate_x, extrapolate_y, false, + j = interpolateAndExtrapolate(j, location, extrapolate_x, extrapolate_y, false, transform.get()); // Compare calculated and loaded values - output_warn.write("\tMaximum difference in J is {:e}\n", max(abs(J - Jcalc))); + output_warn.write("\tMaximum difference in J is {:e}\n", max(abs(j - Jcalc))); - communicate(J); + communicate(j); // Re-evaluate Bxy using new J - Bxy = sqrt(g_22) / J; + Bxy = sqrt(g_22) / j; } // Check jacobian - bout::checkFinite(J, "J", "RGN_NOCORNERS"); - bout::checkPositive(J, "J", "RGN_NOCORNERS"); - if (min(abs(J)) < 1.0e-10) { + bout::checkFinite(j, "J", "RGN_NOCORNERS"); + bout::checkPositive(j, "J", "RGN_NOCORNERS"); + if (min(abs(j)) < 1.0e-10) { throw BoutException("\tERROR: Jacobian becomes very small\n"); } @@ -629,7 +629,7 @@ Coordinates::Coordinates(Mesh* mesh, Options* options) Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, const Coordinates* coords_in, bool force_interpolate_from_centre) : dx(1., mesh), dy(1., mesh), dz(1., mesh), d1_dx(mesh), d1_dy(mesh), d1_dz(mesh), - J(1., mesh), Bxy(1., mesh), contravariantMetricTensor(1., 1., 1., 0, 0, 0, mesh), + j(1., mesh), Bxy(1., mesh), contravariantMetricTensor(1., 1., 1., 0, 0, 0, mesh), covariantMetricTensor(1., 1., 1., 0, 0, 0, mesh), G1_11(mesh), G1_22(mesh), G1_33(mesh), G1_12(mesh), G1_13(mesh), G1_23(mesh), G2_11(mesh), G2_22(mesh), G2_33(mesh), G2_12(mesh), G2_13(mesh), G2_23(mesh), G3_11(mesh), G3_22(mesh), @@ -795,27 +795,27 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, } // Attempt to read J from the grid file - auto Jcalc = J; - if (getAtLoc(mesh, J, "J", suffix, location)) { + auto Jcalc = j; + if (getAtLoc(mesh, j, "J", suffix, location)) { output_warn.write( "\tWARNING: Jacobian 'J_{:s}' not found. Calculating from metric tensor\n", suffix); - J = Jcalc; + j = Jcalc; } else { - J = interpolateAndExtrapolate(J, location, extrapolate_x, extrapolate_y, false, + j = interpolateAndExtrapolate(j, location, extrapolate_x, extrapolate_y, false, transform.get()); // Compare calculated and loaded values - output_warn.write("\tMaximum difference in J is %e\n", max(abs(J - Jcalc))); + output_warn.write("\tMaximum difference in J is %e\n", max(abs(j - Jcalc))); // Re-evaluate Bxy using new J - Bxy = sqrt(g_22) / J; + Bxy = sqrt(g_22) / j; } // Check jacobian - bout::checkFinite(J, "J" + suffix, "RGN_NOCORNERS"); - bout::checkPositive(J, "J" + suffix, "RGN_NOCORNERS"); - if (min(abs(J)) < 1.0e-10) { + bout::checkFinite(j, "J" + suffix, "RGN_NOCORNERS"); + bout::checkPositive(j, "J" + suffix, "RGN_NOCORNERS"); + if (min(abs(j)) < 1.0e-10) { throw BoutException("\tERROR: Jacobian{:s} becomes very small\n", suffix); } @@ -911,13 +911,13 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, checkContravariant(); checkCovariant(); - J = interpolateAndExtrapolate(coords_in->J, location, true, true, false, + j = interpolateAndExtrapolate(coords_in->J(), location, true, true, false, transform.get()); Bxy = interpolateAndExtrapolate(coords_in->Bxy, location, true, true, false, transform.get()); - bout::checkFinite(J, "The Jacobian", "RGN_NOCORNERS"); - bout::checkPositive(J, "The Jacobian", "RGN_NOCORNERS"); + bout::checkFinite(j, "The Jacobian", "RGN_NOCORNERS"); + bout::checkPositive(j, "The Jacobian", "RGN_NOCORNERS"); bout::checkFinite(Bxy, "Bxy", "RGN_NOCORNERS"); bout::checkPositive(Bxy, "Bxy", "RGN_NOCORNERS"); @@ -992,7 +992,7 @@ void Coordinates::outputVars(Options& output_options) { output_options["g_23" + loc_string].force(covariantMetricTensor.Getg23(), "Coordinates"); - output_options["J" + loc_string].force(J, "Coordinates"); + output_options["J" + loc_string].force(j, "Coordinates"); output_options["Bxy" + loc_string].force(Bxy, "Coordinates"); output_options["G1" + loc_string].force(G1, "Coordinates"); @@ -1027,7 +1027,7 @@ int Coordinates::geometry(bool recalculate_staggered, contravariantMetricTensor.Getg23(), covariantMetricTensor.Getg11(), covariantMetricTensor.Getg22(), covariantMetricTensor.Getg33(), covariantMetricTensor.Getg12(), covariantMetricTensor.Getg13(), - covariantMetricTensor.Getg23(), J, Bxy); + covariantMetricTensor.Getg23(), j, Bxy); output_progress.write("Calculating differential geometry terms\n"); @@ -1048,21 +1048,21 @@ int Coordinates::geometry(bool recalculate_staggered, checkCovariant(); CalculateChristoffelSymbols(); - auto tmp = J * contravariantMetricTensor.Getg12(); + auto tmp = j * contravariantMetricTensor.Getg12(); communicate(tmp); - G1 = (DDX(J * contravariantMetricTensor.Getg11()) + DDY(tmp) - + DDZ(J * contravariantMetricTensor.Getg13())) - / J; - tmp = J * contravariantMetricTensor.Getg22(); + G1 = (DDX(j * contravariantMetricTensor.Getg11()) + DDY(tmp) + + DDZ(j * contravariantMetricTensor.Getg13())) + / j; + tmp = j * contravariantMetricTensor.Getg22(); communicate(tmp); - G2 = (DDX(J * contravariantMetricTensor.Getg12()) + DDY(tmp) - + DDZ(J * contravariantMetricTensor.Getg23())) - / J; - tmp = J * contravariantMetricTensor.Getg23(); + G2 = (DDX(j * contravariantMetricTensor.Getg12()) + DDY(tmp) + + DDZ(j * contravariantMetricTensor.Getg23())) + / j; + tmp = j * contravariantMetricTensor.Getg23(); communicate(tmp); - G3 = (DDX(J * contravariantMetricTensor.Getg13()) + DDY(tmp) - + DDZ(J * contravariantMetricTensor.Getg33())) - / J; + G3 = (DDX(j * contravariantMetricTensor.Getg13()) + DDY(tmp) + + DDZ(j * contravariantMetricTensor.Getg33())) + / j; // Communicate christoffel symbol terms output_progress.write("\tCommunicating connection terms\n"); @@ -1418,13 +1418,13 @@ int Coordinates::jacobian() { // Check that g is positive bout::checkPositive(g, "The determinant of g^ij", "RGN_NOBNDRY"); - J = 1. / sqrt(g); + j = 1. / sqrt(g); // More robust to extrapolate derived quantities directly, rather than // deriving from extrapolated covariant metric components - J = interpolateAndExtrapolate(J, location, extrapolate_x, extrapolate_y, false, + j = interpolateAndExtrapolate(j, location, extrapolate_x, extrapolate_y, false, transform.get()); - Bxy = sqrt(covariantMetricTensor.Getg22()) / J; + Bxy = sqrt(covariantMetricTensor.Getg22()) / j; Bxy = interpolateAndExtrapolate(Bxy, location, extrapolate_x, extrapolate_y, false, transform.get()); @@ -1867,13 +1867,13 @@ Coordinates::FieldMetric Coordinates::Laplace_par(const Field2D& f, CELL_LOC out ASSERT1(location == outloc || outloc == CELL_DEFAULT); return D2DY2(f, outloc) / covariantMetricTensor.Getg22() - + DDY(J / covariantMetricTensor.Getg22(), outloc) * DDY(f, outloc) / J; + + DDY(j / covariantMetricTensor.Getg22(), outloc) * DDY(f, outloc) / j; } Field3D Coordinates::Laplace_par(const Field3D& f, CELL_LOC outloc) { ASSERT1(location == outloc || outloc == CELL_DEFAULT); return D2DY2(f, outloc) / covariantMetricTensor.Getg22() - + DDY(J / covariantMetricTensor.Getg22(), outloc) * ::DDY(f, outloc) / J; + + DDY(j / covariantMetricTensor.Getg22(), outloc) * ::DDY(f, outloc) / j; } // Full Laplacian operator on scalar field @@ -1928,45 +1928,45 @@ Field2D Coordinates::Laplace_perpXY([[maybe_unused]] const Field2D& A, // outer x boundary const auto outer_x_avg = [&i](const auto& f) { return 0.5 * (f[i] + f[i.xp()]); }; const BoutReal outer_x_A = outer_x_avg(A); - const BoutReal outer_x_J = outer_x_avg(J); + const BoutReal outer_x_J = outer_x_avg(j); const BoutReal outer_x_g11 = outer_x_avg(contravariantMetricTensor.Getg11()); const BoutReal outer_x_dx = outer_x_avg(dx); const BoutReal outer_x_value = - outer_x_A * outer_x_J * outer_x_g11 / (J[i] * outer_x_dx * dx[i]); + outer_x_A * outer_x_J * outer_x_g11 / (j[i] * outer_x_dx * dx[i]); result[i] += outer_x_value * (f[i.xp()] - f[i]); // inner x boundary const auto inner_x_avg = [&i](const auto& f) { return 0.5 * (f[i] + f[i.xm()]); }; const BoutReal inner_x_A = inner_x_avg(A); - const BoutReal inner_x_J = inner_x_avg(J); + const BoutReal inner_x_J = inner_x_avg(j); const BoutReal inner_x_g11 = inner_x_avg(contravariantMetricTensor.Getg11()); const BoutReal inner_x_dx = inner_x_avg(dx); const BoutReal inner_x_value = - inner_x_A * inner_x_J * inner_x_g11 / (J[i] * inner_x_dx * dx[i]); + inner_x_A * inner_x_J * inner_x_g11 / (j[i] * inner_x_dx * dx[i]); result[i] += inner_x_value * (f[i.xm()] - f[i]); // upper y boundary const auto upper_y_avg = [&i](const auto& f) { return 0.5 * (f[i] + f[i.yp()]); }; const BoutReal upper_y_A = upper_y_avg(A); - const BoutReal upper_y_J = upper_y_avg(J); + const BoutReal upper_y_J = upper_y_avg(j); const BoutReal upper_y_g_22 = upper_y_avg(covariantMetricTensor.Getg22()); const BoutReal upper_y_g23 = upper_y_avg(covariantMetricTensor.Getg23()); const BoutReal upper_y_g_23 = upper_y_avg(covariantMetricTensor.Getg23()); const BoutReal upper_y_dy = upper_y_avg(dy); const BoutReal upper_y_value = -upper_y_A * upper_y_J * upper_y_g23 * upper_y_g_23 - / (upper_y_g_22 * J[i] * upper_y_dy * dy[i]); + / (upper_y_g_22 * j[i] * upper_y_dy * dy[i]); result[i] += upper_y_value * (f[i.yp()] - f[i]); // lower y boundary const auto lower_y_avg = [&i](const auto& f) { return 0.5 * (f[i] + f[i.ym()]); }; const BoutReal lower_y_A = lower_y_avg(A); - const BoutReal lower_y_J = lower_y_avg(J); + const BoutReal lower_y_J = lower_y_avg(j); const BoutReal lower_y_g_22 = lower_y_avg(covariantMetricTensor.Getg22()); const BoutReal lower_y_g23 = lower_y_avg(contravariantMetricTensor.Getg23()); const BoutReal lower_y_g_23 = lower_y_avg(covariantMetricTensor.Getg23()); const BoutReal lower_y_dy = lower_y_avg(dy); const BoutReal lower_y_value = -lower_y_A * lower_y_J * lower_y_g23 * lower_y_g_23 - / (lower_y_g_22 * J[i] * lower_y_dy * dy[i]); + / (lower_y_g_22 * j[i] * lower_y_dy * dy[i]); result[i] += lower_y_value * (f[i.ym()] - f[i]); } @@ -2061,3 +2061,6 @@ const MetricTensor::FieldMetric& Coordinates::g13() const { const MetricTensor::FieldMetric& Coordinates::g23() const { return contravariantMetricTensor.Getg23(); } +const MetricTensor::FieldMetric Coordinates::J() const { + return j; +} diff --git a/src/mesh/coordinates_accessor.cxx b/src/mesh/coordinates_accessor.cxx index 55ee8b8be8..eed64d0a88 100644 --- a/src/mesh/coordinates_accessor.cxx +++ b/src/mesh/coordinates_accessor.cxx @@ -52,7 +52,7 @@ CoordinatesAccessor::CoordinatesAccessor(const Coordinates* coords) { for (const auto& ind : coords->dx.getRegion("RGN_ALL")) { COPY_STRIPE(dx, dy, dz); COPY_STRIPE(d1_dx, d1_dy, d1_dz); - COPY_STRIPE(J); + data[stripe_size * ind.ind + static_cast(Offset::J)] = coords->J()[ind]; data[stripe_size * ind.ind + static_cast(Offset::B)] = coords->Bxy[ind]; data[stripe_size * ind.ind + static_cast(Offset::Byup)] = coords->Bxy.yup()[ind]; diff --git a/src/mesh/difops.cxx b/src/mesh/difops.cxx index 4ad956b336..950e1e6146 100644 --- a/src/mesh/difops.cxx +++ b/src/mesh/difops.cxx @@ -259,16 +259,16 @@ Field3D Div_par(const Field3D& f, const Field3D& v) { // Calculate flux at right boundary (y+1/2) BoutReal fluxRight = - fR * vR * (coord->J(i, j, k) + coord->J(i, j + 1, k)) + fR * vR * (coord->J()(i, j, k) + coord->J()(i, j + 1, k)) / (sqrt(coord->g_22()(i, j, k)) + sqrt(coord->g_22()(i, j + 1, k))); // Calculate at left boundary (y-1/2) BoutReal fluxLeft = - fL * vL * (coord->J(i, j, k) + coord->J(i, j - 1, k)) + fL * vL * (coord->J()(i, j, k) + coord->J()(i, j - 1, k)) / (sqrt(coord->g_22()(i, j, k)) + sqrt(coord->g_22()(i, j - 1, k))); result(i, j, k) = - (fluxRight - fluxLeft) / (coord->dy(i, j, k) * coord->J(i, j, k)); + (fluxRight - fluxLeft) / (coord->dy(i, j, k) * coord->J()(i, j, k)); } } } @@ -478,7 +478,7 @@ Coordinates::FieldMetric b0xGrad_dot_Grad(const Field2D& phi, const Field2D& A, // Upwind A using these velocities Coordinates::FieldMetric result = VDDX(vx, A, outloc) + VDDY(vy, A, outloc); - result /= metric->J * sqrt(metric->g_22()); + result /= metric->J() * sqrt(metric->g_22()); ASSERT1(result.getLocation() == outloc); @@ -519,7 +519,7 @@ Field3D b0xGrad_dot_Grad(const Field2D& phi, const Field3D& A, CELL_LOC outloc) Field3D result = VDDX(vx, A, outloc) + VDDY(vy, A, outloc) + VDDZ(vz, A, outloc); - result /= (metric->J * sqrt(metric->g_22())); + result /= (metric->J() * sqrt(metric->g_22())); #if BOUT_USE_TRACK result.name = "b0xGrad_dot_Grad(" + phi.name + "," + A.name + ")"; @@ -554,7 +554,7 @@ Field3D b0xGrad_dot_Grad(const Field3D& p, const Field2D& A, CELL_LOC outloc) { Field3D result = VDDX(vx, A, outloc) + VDDY(vy, A, outloc); - result /= (metric->J * sqrt(metric->g_22())); + result /= (metric->J() * sqrt(metric->g_22())); #if BOUT_USE_TRACK result.name = "b0xGrad_dot_Grad(" + p.name + "," + A.name + ")"; @@ -595,7 +595,7 @@ Field3D b0xGrad_dot_Grad(const Field3D& phi, const Field3D& A, CELL_LOC outloc) Field3D result = VDDX(vx, A, outloc) + VDDY(vy, A, outloc) + VDDZ(vz, A, outloc); - result /= (metric->J * sqrt(metric->g_22())); + result /= (metric->J() * sqrt(metric->g_22())); #if BOUT_USE_TRACK result.name = "b0xGrad_dot_Grad(" + phi.name + "," + A.name + ")"; diff --git a/src/mesh/fv_ops.cxx b/src/mesh/fv_ops.cxx index f9bc7323d2..69b419b76f 100644 --- a/src/mesh/fv_ops.cxx +++ b/src/mesh/fv_ops.cxx @@ -52,13 +52,13 @@ Field3D Div_a_Grad_perp(const Field3D& a, const Field3D& f) { // Calculate flux from i to i+1 BoutReal fout = 0.5 * (a(i, j, k) + a(i + 1, j, k)) - * (coord->J(i, j, k) * coord->g11()(i, j, k) - + coord->J(i + 1, j, k) * coord->g11()(i + 1, j, k)) + * (coord->J()(i, j, k) * coord->g11()(i, j, k) + + coord->J()(i + 1, j, k) * coord->g11()(i + 1, j, k)) * (f(i + 1, j, k) - f(i, j, k)) / (coord->dx(i, j, k) + coord->dx(i + 1, j, k)); - result(i, j, k) += fout / (coord->dx(i, j, k) * coord->J(i, j, k)); - result(i + 1, j, k) -= fout / (coord->dx(i + 1, j, k) * coord->J(i + 1, j, k)); + result(i, j, k) += fout / (coord->dx(i, j, k) * coord->J()(i, j, k)); + result(i + 1, j, k) -= fout / (coord->dx(i + 1, j, k) * coord->J()(i + 1, j, k)); } } } @@ -71,7 +71,7 @@ Field3D Div_a_Grad_perp(const Field3D& a, const Field3D& f) { // -- should insert communication here? if (!coord->g23().hasParallelSlices() || !coord->g_23().hasParallelSlices() || !coord->dy.hasParallelSlices() || !coord->dz.hasParallelSlices() - || !coord->Bxy.hasParallelSlices() || !coord->J.hasParallelSlices()) { + || !coord->Bxy.hasParallelSlices() || !coord->J().hasParallelSlices()) { throw BoutException("metrics have no yup/down: Maybe communicate in init?"); } } @@ -89,7 +89,7 @@ Field3D Div_a_Grad_perp(const Field3D& a, const Field3D& f) { const bool metric_fci = fci and bout::build::use_metric_3d; const auto g23 = makeslices(metric_fci, coord->g23()); const auto g_23 = makeslices(metric_fci, coord->g_23()); - const auto J = makeslices(metric_fci, coord->J); + const auto J = makeslices(metric_fci, coord->J()); const auto dy = makeslices(metric_fci, coord->dy); const auto dz = makeslices(metric_fci, coord->dz); const auto Bxy = makeslices(metric_fci, coord->Bxy); @@ -212,21 +212,21 @@ const Field3D Div_par_K_Grad_par(const Field3D& Kin, const Field3D& fin, || (i.y() != mesh->yend)) { BoutReal c = 0.5 * (K[i] + Kup[iyp]); // K at the upper boundary - BoutReal J = 0.5 * (coord->J[i] + coord->J[iyp]); // Jacobian at boundary + BoutReal J = 0.5 * (coord->J()[i] + coord->J()[iyp]); // Jacobian at boundary BoutReal g_22 = 0.5 * (coord->g_22()[i] + coord->g_22()[iyp]); BoutReal gradient = 2. * (fup[iyp] - f[i]) / (coord->dy[i] + coord->dy[iyp]); BoutReal flux = c * J * gradient / g_22; - result[i] += flux / (coord->dy[i] * coord->J[i]); + result[i] += flux / (coord->dy[i] * coord->J()[i]); } // Calculate flux at lower surface if (bndry_flux || mesh->periodicY(i.x()) || !mesh->firstY(i.x()) || (i.y() != mesh->ystart)) { BoutReal c = 0.5 * (K[i] + Kdown[iym]); // K at the lower boundary - BoutReal J = 0.5 * (coord->J[i] + coord->J[iym]); // Jacobian at boundary + BoutReal J = 0.5 * (coord->J()[i] + coord->J()[iym]); // Jacobian at boundary BoutReal g_22 = 0.5 * (coord->g_22()[i] + coord->g_22()[iym]); @@ -234,7 +234,7 @@ const Field3D Div_par_K_Grad_par(const Field3D& Kin, const Field3D& fin, BoutReal flux = c * J * gradient / g_22; - result[i] -= flux / (coord->dy[i] * coord->J[i]); + result[i] -= flux / (coord->dy[i] * coord->J()[i]); } } @@ -291,10 +291,10 @@ const Field3D D4DY4(const Field3D& d_in, const Field3D& f_in) { / dy3; BoutReal flux = 0.5 * (d(i, j, k) + d(i, j + 1, k)) - * (coord->J(i, j, k) + coord->J(i, j + 1, k)) * d3fdy3; + * (coord->J()(i, j, k) + coord->J()(i, j + 1, k)) * d3fdy3; - result(i, j, k) += flux / (coord->J(i, j, k) * coord->dy(i, j, k)); - result(i, j + 1, k) -= flux / (coord->J(i, j + 1, k) * coord->dy(i, j + 1, k)); + result(i, j, k) += flux / (coord->J()(i, j, k) * coord->dy(i, j, k)); + result(i, j + 1, k) -= flux / (coord->J()(i, j + 1, k) * coord->dy(i, j + 1, k)); } } } @@ -333,12 +333,12 @@ const Field3D D4DY4_Index(const Field3D& f_in, bool bndry_flux) { // Right boundary common factors const BoutReal common_factor = 0.25 * (coord->dy(i, j, k) + coord->dy(i, j + 1, k)) - * (coord->J(i, j, j) + coord->J(i, j + 1, k)); + * (coord->J()(i, j, j) + coord->J()(i, j + 1, k)); const BoutReal factor_rc = - common_factor / (coord->J(i, j, k) * coord->dy(i, j, k)); + common_factor / (coord->J()(i, j, k) * coord->dy(i, j, k)); const BoutReal factor_rp = - common_factor / (coord->J(i, j + 1, k) * coord->dy(i, j + 1, k)); + common_factor / (coord->J()(i, j + 1, k) * coord->dy(i, j + 1, k)); // Not on domain boundary // 3rd derivative at right cell boundary @@ -357,12 +357,12 @@ const Field3D D4DY4_Index(const Field3D& f_in, bool bndry_flux) { // Right boundary common factors const BoutReal common_factor = 0.25 * (coord->dy(i, j, k) + coord->dy(i, j + 1, k)) - * (coord->J(i, j, j) + coord->J(i, j + 1, k)); + * (coord->J()(i, j, j) + coord->J()(i, j + 1, k)); const BoutReal factor_rc = - common_factor / (coord->J(i, j, k) * coord->dy(i, j, k)); + common_factor / (coord->J()(i, j, k) * coord->dy(i, j, k)); const BoutReal factor_rp = - common_factor / (coord->J(i, j + 1, k) * coord->dy(i, j + 1, k)); + common_factor / (coord->J()(i, j + 1, k) * coord->dy(i, j + 1, k)); const BoutReal d3fdx3 = -((16. / 5) * 0.5 * (f(i, j + 1, k) + f(i, j, k)) // Boundary value f_b @@ -386,12 +386,12 @@ const Field3D D4DY4_Index(const Field3D& f_in, bool bndry_flux) { for (int k = 0; k < mesh->LocalNz; k++) { const BoutReal common_factor = 0.25 * (coord->dy(i, j, k) + coord->dy(i, j + 1, k)) - * (coord->J(i, j, k) + coord->J(i, j - 1, k)); + * (coord->J()(i, j, k) + coord->J()(i, j - 1, k)); const BoutReal factor_lc = - common_factor / (coord->J(i, j, k) * coord->dy(i, j, k)); + common_factor / (coord->J()(i, j, k) * coord->dy(i, j, k)); const BoutReal factor_lm = - common_factor / (coord->J(i, j - 1, k) * coord->dy(i, j - 1, k)); + common_factor / (coord->J()(i, j - 1, k) * coord->dy(i, j - 1, k)); // Not on a domain boundary const BoutReal d3fdx3 = @@ -405,12 +405,12 @@ const Field3D D4DY4_Index(const Field3D& f_in, bool bndry_flux) { for (int k = 0; k < mesh->LocalNz; k++) { const BoutReal common_factor = 0.25 * (coord->dy(i, j, k) + coord->dy(i, j + 1, k)) - * (coord->J(i, j, k) + coord->J(i, j - 1, k)); + * (coord->J()(i, j, k) + coord->J()(i, j - 1, k)); const BoutReal factor_lc = - common_factor / (coord->J(i, j, k) * coord->dy(i, j, k)); + common_factor / (coord->J()(i, j, k) * coord->dy(i, j, k)); const BoutReal factor_lm = - common_factor / (coord->J(i, j - 1, k) * coord->dy(i, j - 1, k)); + common_factor / (coord->J()(i, j - 1, k) * coord->dy(i, j - 1, k)); const BoutReal d3fdx3 = -(-(16. / 5) * 0.5 * (f(i, j - 1, k) + f(i, j, k)) // Boundary value f_b + 6. * f(i, j, k) // f_0 @@ -538,24 +538,24 @@ Field3D Div_Perp_Lap(const Field3D& a, const Field3D& f, CELL_LOC outloc) { + coords->g33()(i, j, k) * (f(i, j, kp) - f(i, j, k)) / coords->dz(i, j, k); // Flow right - BoutReal flux = gR * 0.25 * (coords->J(i + 1, j, k) + coords->J(i, j, k)) + BoutReal flux = gR * 0.25 * (coords->J()(i + 1, j, k) + coords->J()(i, j, k)) * (a(i + 1, j, k) + a(i, j, k)); - result(i, j, k) += flux / (coords->dx(i, j, k) * coords->J(i, j, k)); + result(i, j, k) += flux / (coords->dx(i, j, k) * coords->J()(i, j, k)); // Flow left - flux = gL * 0.25 * (coords->J(i - 1, j, k) + coords->J(i, j, k)) + flux = gL * 0.25 * (coords->J()(i - 1, j, k) + coords->J()(i, j, k)) * (a(i - 1, j, k) + a(i, j, k)); - result(i, j, k) -= flux / (coords->dx(i, j, k) * coords->J(i, j, k)); + result(i, j, k) -= flux / (coords->dx(i, j, k) * coords->J()(i, j, k)); // Flow up - flux = gU * 0.25 * (coords->J(i, j, k) + coords->J(i, j, kp)) + flux = gU * 0.25 * (coords->J()(i, j, k) + coords->J()(i, j, kp)) * (a(i, j, k) + a(i, j, kp)); - result(i, j, k) += flux / (coords->dz(i, j, k) * coords->J(i, j, k)); + result(i, j, k) += flux / (coords->dz(i, j, k) * coords->J()(i, j, k)); // Flow down - flux = gD * 0.25 * (coords->J(i, j, km) + coords->J(i, j, k)) + flux = gD * 0.25 * (coords->J()(i, j, km) + coords->J()(i, j, k)) * (a(i, j, km) + a(i, j, k)); - result(i, j, k) += flux / (coords->dz(i, j, k) * coords->J(i, j, k)); + result(i, j, k) += flux / (coords->dz(i, j, k) * coords->J()(i, j, k)); } } } diff --git a/src/physics/smoothing.cxx b/src/physics/smoothing.cxx index 78ac1814a5..4fe6876115 100644 --- a/src/physics/smoothing.cxx +++ b/src/physics/smoothing.cxx @@ -359,7 +359,7 @@ BoutReal Vol_Integral(const Field2D& var) { BoutReal Int_Glb; Coordinates* metric = var.getCoordinates(); - auto result = metric->J * var * metric->dx * metric->dy; + auto result = metric->J() * var * metric->dx * metric->dy; Int_Glb = Average_XY(result); Int_Glb *= static_cast( diff --git a/tests/unit/mesh/test_coordinates.cxx b/tests/unit/mesh/test_coordinates.cxx index d6fd43d7a8..c47f39e8eb 100644 --- a/tests/unit/mesh/test_coordinates.cxx +++ b/tests/unit/mesh/test_coordinates.cxx @@ -110,7 +110,7 @@ TEST_F(CoordinatesTest, Jacobian) { EXPECT_NO_THROW(coords.jacobian()); - EXPECT_TRUE(IsFieldEqual(coords.J, 1.0)); + EXPECT_TRUE(IsFieldEqual(coords.J(), 1.0)); EXPECT_TRUE(IsFieldEqual(coords.Bxy, 1.0)); } @@ -205,7 +205,7 @@ TEST_F(CoordinatesTest, DefaultConstructor) { EXPECT_TRUE(IsFieldEqual(coords.g13(), 0.0)); EXPECT_TRUE(IsFieldEqual(coords.g23(), 0.0)); - EXPECT_TRUE(IsFieldEqual(coords.J, 1.0)); + EXPECT_TRUE(IsFieldEqual(coords.J(), 1.0)); EXPECT_TRUE(IsFieldEqual(coords.Bxy, 1.0)); } @@ -231,7 +231,7 @@ TEST_F(CoordinatesTest, ConstructWithMeshSpacing) { EXPECT_TRUE(IsFieldEqual(coords.g13(), 0.0)); EXPECT_TRUE(IsFieldEqual(coords.g23(), 0.0)); - EXPECT_TRUE(IsFieldEqual(coords.J, 1.0)); + EXPECT_TRUE(IsFieldEqual(coords.J(), 1.0)); EXPECT_TRUE(IsFieldEqual(coords.Bxy, 1.0)); } @@ -278,7 +278,7 @@ TEST_F(CoordinatesTest, ConstructWithDiagonalContravariantMetric) { EXPECT_TRUE(IsFieldEqual(coords.g_22(), 1. / 3.2, "RGN_NOCORNERS")); EXPECT_TRUE(IsFieldEqual(coords.g_33(), 1. / 42, "RGN_NOCORNERS")); - EXPECT_TRUE(IsFieldEqual(coords.J, 1. / sqrt(2.0 * 3.2 * 42), "RGN_NOCORNERS")); + EXPECT_TRUE(IsFieldEqual(coords.J(), 1. / sqrt(2.0 * 3.2 * 42), "RGN_NOCORNERS")); EXPECT_TRUE(IsFieldEqual(coords.Bxy, sqrt(2.0 * 42), "RGN_NOCORNERS", 1e-10)); } From c2738a9eeec4b8af8be4b0529efa01076aaeb572 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Sat, 18 Nov 2023 10:10:21 +0000 Subject: [PATCH 136/491] Rename private field j to this_J. --- include/bout/coordinates.hxx | 2 +- src/mesh/coordinates.cxx | 108 +++++++++++++++++------------------ 2 files changed, 55 insertions(+), 55 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index 3b6f78cc45..17ef5917fe 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -272,7 +272,7 @@ private: MetricTensor contravariantMetricTensor; MetricTensor covariantMetricTensor; - FieldMetric j; + FieldMetric this_J; }; /* diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 3adcde3f67..af701ccf30 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -382,7 +382,7 @@ Coordinates::Coordinates(Mesh* mesh, FieldMetric dx, FieldMetric dy, FieldMetric FieldMetric g_33, FieldMetric g_12, FieldMetric g_13, FieldMetric g_23, FieldMetric ShiftTorsion, FieldMetric IntShiftTorsion) - : dx(std::move(dx)), dy(std::move(dy)), dz(dz), j(std::move(J)), Bxy(std::move(Bxy)), + : dx(std::move(dx)), dy(std::move(dy)), dz(dz), this_J(std::move(J)), Bxy(std::move(Bxy)), contravariantMetricTensor(g11, g22, g33, g12, g13, g23), covariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23), ShiftTorsion(std::move(ShiftTorsion)), IntShiftTorsion(std::move(IntShiftTorsion)), @@ -390,7 +390,7 @@ Coordinates::Coordinates(Mesh* mesh, FieldMetric dx, FieldMetric dy, FieldMetric Coordinates::Coordinates(Mesh* mesh, Options* options) : dx(1., mesh), dy(1., mesh), dz(1., mesh), d1_dx(mesh), d1_dy(mesh), d1_dz(mesh), - j(1., mesh), Bxy(1., mesh), contravariantMetricTensor(1., 1., 1., 0, 0, 0, mesh), + this_J(1., mesh), Bxy(1., mesh), contravariantMetricTensor(1., 1., 1., 0, 0, 0, mesh), covariantMetricTensor(1., 1., 1., 0, 0, 0, mesh), G1_11(mesh), G1_22(mesh), G1_33(mesh), G1_12(mesh), G1_13(mesh), G1_23(mesh), G2_11(mesh), G2_22(mesh), G2_33(mesh), G2_12(mesh), G2_13(mesh), G2_23(mesh), G3_11(mesh), G3_22(mesh), @@ -562,28 +562,28 @@ Coordinates::Coordinates(Mesh* mesh, Options* options) } // Attempt to read J from the grid file - auto Jcalc = j; - if (mesh->get(j, "J", 0.0, false)) { + auto Jcalc = this_J; + if (mesh->get(this_J, "J", 0.0, false)) { output_warn.write( "\tWARNING: Jacobian 'J' not found. Calculating from metric tensor\n"); - j = Jcalc; + this_J = Jcalc; } else { - j = interpolateAndExtrapolate(j, location, extrapolate_x, extrapolate_y, false, - transform.get()); + this_J = interpolateAndExtrapolate(this_J, location, extrapolate_x, extrapolate_y, + false, transform.get()); // Compare calculated and loaded values - output_warn.write("\tMaximum difference in J is {:e}\n", max(abs(j - Jcalc))); + output_warn.write("\tMaximum difference in J is {:e}\n", max(abs(this_J - Jcalc))); - communicate(j); + communicate(this_J); // Re-evaluate Bxy using new J - Bxy = sqrt(g_22) / j; + Bxy = sqrt(g_22) / this_J; } // Check jacobian - bout::checkFinite(j, "J", "RGN_NOCORNERS"); - bout::checkPositive(j, "J", "RGN_NOCORNERS"); - if (min(abs(j)) < 1.0e-10) { + bout::checkFinite(this_J, "J", "RGN_NOCORNERS"); + bout::checkPositive(this_J, "J", "RGN_NOCORNERS"); + if (min(abs(this_J)) < 1.0e-10) { throw BoutException("\tERROR: Jacobian becomes very small\n"); } @@ -629,7 +629,7 @@ Coordinates::Coordinates(Mesh* mesh, Options* options) Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, const Coordinates* coords_in, bool force_interpolate_from_centre) : dx(1., mesh), dy(1., mesh), dz(1., mesh), d1_dx(mesh), d1_dy(mesh), d1_dz(mesh), - j(1., mesh), Bxy(1., mesh), contravariantMetricTensor(1., 1., 1., 0, 0, 0, mesh), + _J(1., mesh), Bxy(1., mesh), contravariantMetricTensor(1., 1., 1., 0, 0, 0, mesh), covariantMetricTensor(1., 1., 1., 0, 0, 0, mesh), G1_11(mesh), G1_22(mesh), G1_33(mesh), G1_12(mesh), G1_13(mesh), G1_23(mesh), G2_11(mesh), G2_22(mesh), G2_33(mesh), G2_12(mesh), G2_13(mesh), G2_23(mesh), G3_11(mesh), G3_22(mesh), @@ -795,27 +795,27 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, } // Attempt to read J from the grid file - auto Jcalc = j; - if (getAtLoc(mesh, j, "J", suffix, location)) { + auto Jcalc = this_J; + if (getAtLoc(mesh, this_J, "J", suffix, location)) { output_warn.write( "\tWARNING: Jacobian 'J_{:s}' not found. Calculating from metric tensor\n", suffix); - j = Jcalc; + this_J = Jcalc; } else { - j = interpolateAndExtrapolate(j, location, extrapolate_x, extrapolate_y, false, + this_J = interpolateAndExtrapolate(this_J, location, extrapolate_x, extrapolate_y, false, transform.get()); // Compare calculated and loaded values - output_warn.write("\tMaximum difference in J is %e\n", max(abs(j - Jcalc))); + output_warn.write("\tMaximum difference in J is %e\n", max(abs(this_J - Jcalc))); // Re-evaluate Bxy using new J - Bxy = sqrt(g_22) / j; + Bxy = sqrt(g_22) / this_J; } // Check jacobian - bout::checkFinite(j, "J" + suffix, "RGN_NOCORNERS"); - bout::checkPositive(j, "J" + suffix, "RGN_NOCORNERS"); - if (min(abs(j)) < 1.0e-10) { + bout::checkFinite(this_J, "J" + suffix, "RGN_NOCORNERS"); + bout::checkPositive(this_J, "J" + suffix, "RGN_NOCORNERS"); + if (min(abs(this_J)) < 1.0e-10) { throw BoutException("\tERROR: Jacobian{:s} becomes very small\n", suffix); } @@ -911,13 +911,13 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, checkContravariant(); checkCovariant(); - j = interpolateAndExtrapolate(coords_in->J(), location, true, true, false, + this_J = interpolateAndExtrapolate(coords_in->J(), location, true, true, false, transform.get()); Bxy = interpolateAndExtrapolate(coords_in->Bxy, location, true, true, false, transform.get()); - bout::checkFinite(j, "The Jacobian", "RGN_NOCORNERS"); - bout::checkPositive(j, "The Jacobian", "RGN_NOCORNERS"); + bout::checkFinite(this_J, "The Jacobian", "RGN_NOCORNERS"); + bout::checkPositive(this_J, "The Jacobian", "RGN_NOCORNERS"); bout::checkFinite(Bxy, "Bxy", "RGN_NOCORNERS"); bout::checkPositive(Bxy, "Bxy", "RGN_NOCORNERS"); @@ -992,7 +992,7 @@ void Coordinates::outputVars(Options& output_options) { output_options["g_23" + loc_string].force(covariantMetricTensor.Getg23(), "Coordinates"); - output_options["J" + loc_string].force(j, "Coordinates"); + output_options["J" + loc_string].force(this_J, "Coordinates"); output_options["Bxy" + loc_string].force(Bxy, "Coordinates"); output_options["G1" + loc_string].force(G1, "Coordinates"); @@ -1027,7 +1027,7 @@ int Coordinates::geometry(bool recalculate_staggered, contravariantMetricTensor.Getg23(), covariantMetricTensor.Getg11(), covariantMetricTensor.Getg22(), covariantMetricTensor.Getg33(), covariantMetricTensor.Getg12(), covariantMetricTensor.Getg13(), - covariantMetricTensor.Getg23(), j, Bxy); + covariantMetricTensor.Getg23(), this_J, Bxy); output_progress.write("Calculating differential geometry terms\n"); @@ -1048,21 +1048,21 @@ int Coordinates::geometry(bool recalculate_staggered, checkCovariant(); CalculateChristoffelSymbols(); - auto tmp = j * contravariantMetricTensor.Getg12(); + auto tmp = this_J * contravariantMetricTensor.Getg12(); communicate(tmp); - G1 = (DDX(j * contravariantMetricTensor.Getg11()) + DDY(tmp) - + DDZ(j * contravariantMetricTensor.Getg13())) - / j; - tmp = j * contravariantMetricTensor.Getg22(); + G1 = (DDX(this_J * contravariantMetricTensor.Getg11()) + DDY(tmp) + + DDZ(this_J * contravariantMetricTensor.Getg13())) + / this_J; + tmp = this_J * contravariantMetricTensor.Getg22(); communicate(tmp); - G2 = (DDX(j * contravariantMetricTensor.Getg12()) + DDY(tmp) - + DDZ(j * contravariantMetricTensor.Getg23())) - / j; - tmp = j * contravariantMetricTensor.Getg23(); + G2 = (DDX(this_J * contravariantMetricTensor.Getg12()) + DDY(tmp) + + DDZ(this_J * contravariantMetricTensor.Getg23())) + / this_J; + tmp = this_J * contravariantMetricTensor.Getg23(); communicate(tmp); - G3 = (DDX(j * contravariantMetricTensor.Getg13()) + DDY(tmp) - + DDZ(j * contravariantMetricTensor.Getg33())) - / j; + G3 = (DDX(this_J * contravariantMetricTensor.Getg13()) + DDY(tmp) + + DDZ(this_J * contravariantMetricTensor.Getg33())) + / this_J; // Communicate christoffel symbol terms output_progress.write("\tCommunicating connection terms\n"); @@ -1418,13 +1418,13 @@ int Coordinates::jacobian() { // Check that g is positive bout::checkPositive(g, "The determinant of g^ij", "RGN_NOBNDRY"); - j = 1. / sqrt(g); + this_J = 1. / sqrt(g); // More robust to extrapolate derived quantities directly, rather than // deriving from extrapolated covariant metric components - j = interpolateAndExtrapolate(j, location, extrapolate_x, extrapolate_y, false, + this_J = interpolateAndExtrapolate(this_J, location, extrapolate_x, extrapolate_y, false, transform.get()); - Bxy = sqrt(covariantMetricTensor.Getg22()) / j; + Bxy = sqrt(covariantMetricTensor.Getg22()) / this_J; Bxy = interpolateAndExtrapolate(Bxy, location, extrapolate_x, extrapolate_y, false, transform.get()); @@ -1867,13 +1867,13 @@ Coordinates::FieldMetric Coordinates::Laplace_par(const Field2D& f, CELL_LOC out ASSERT1(location == outloc || outloc == CELL_DEFAULT); return D2DY2(f, outloc) / covariantMetricTensor.Getg22() - + DDY(j / covariantMetricTensor.Getg22(), outloc) * DDY(f, outloc) / j; + + DDY(this_J / covariantMetricTensor.Getg22(), outloc) * DDY(f, outloc) / this_J; } Field3D Coordinates::Laplace_par(const Field3D& f, CELL_LOC outloc) { ASSERT1(location == outloc || outloc == CELL_DEFAULT); return D2DY2(f, outloc) / covariantMetricTensor.Getg22() - + DDY(j / covariantMetricTensor.Getg22(), outloc) * ::DDY(f, outloc) / j; + + DDY(this_J / covariantMetricTensor.Getg22(), outloc) * ::DDY(f, outloc) / this_J; } // Full Laplacian operator on scalar field @@ -1928,45 +1928,45 @@ Field2D Coordinates::Laplace_perpXY([[maybe_unused]] const Field2D& A, // outer x boundary const auto outer_x_avg = [&i](const auto& f) { return 0.5 * (f[i] + f[i.xp()]); }; const BoutReal outer_x_A = outer_x_avg(A); - const BoutReal outer_x_J = outer_x_avg(j); + const BoutReal outer_x_J = outer_x_avg(_J); const BoutReal outer_x_g11 = outer_x_avg(contravariantMetricTensor.Getg11()); const BoutReal outer_x_dx = outer_x_avg(dx); const BoutReal outer_x_value = - outer_x_A * outer_x_J * outer_x_g11 / (j[i] * outer_x_dx * dx[i]); + outer_x_A * outer_x_J * outer_x_g11 / (this_J[i] * outer_x_dx * dx[i]); result[i] += outer_x_value * (f[i.xp()] - f[i]); // inner x boundary const auto inner_x_avg = [&i](const auto& f) { return 0.5 * (f[i] + f[i.xm()]); }; const BoutReal inner_x_A = inner_x_avg(A); - const BoutReal inner_x_J = inner_x_avg(j); + const BoutReal inner_x_J = inner_x_avg(this_J); const BoutReal inner_x_g11 = inner_x_avg(contravariantMetricTensor.Getg11()); const BoutReal inner_x_dx = inner_x_avg(dx); const BoutReal inner_x_value = - inner_x_A * inner_x_J * inner_x_g11 / (j[i] * inner_x_dx * dx[i]); + inner_x_A * inner_x_J * inner_x_g11 / (this_J[i] * inner_x_dx * dx[i]); result[i] += inner_x_value * (f[i.xm()] - f[i]); // upper y boundary const auto upper_y_avg = [&i](const auto& f) { return 0.5 * (f[i] + f[i.yp()]); }; const BoutReal upper_y_A = upper_y_avg(A); - const BoutReal upper_y_J = upper_y_avg(j); + const BoutReal upper_y_J = upper_y_avg(this_J); const BoutReal upper_y_g_22 = upper_y_avg(covariantMetricTensor.Getg22()); const BoutReal upper_y_g23 = upper_y_avg(covariantMetricTensor.Getg23()); const BoutReal upper_y_g_23 = upper_y_avg(covariantMetricTensor.Getg23()); const BoutReal upper_y_dy = upper_y_avg(dy); const BoutReal upper_y_value = -upper_y_A * upper_y_J * upper_y_g23 * upper_y_g_23 - / (upper_y_g_22 * j[i] * upper_y_dy * dy[i]); + / (upper_y_g_22 * this_J[i] * upper_y_dy * dy[i]); result[i] += upper_y_value * (f[i.yp()] - f[i]); // lower y boundary const auto lower_y_avg = [&i](const auto& f) { return 0.5 * (f[i] + f[i.ym()]); }; const BoutReal lower_y_A = lower_y_avg(A); - const BoutReal lower_y_J = lower_y_avg(j); + const BoutReal lower_y_J = lower_y_avg(this_J); const BoutReal lower_y_g_22 = lower_y_avg(covariantMetricTensor.Getg22()); const BoutReal lower_y_g23 = lower_y_avg(contravariantMetricTensor.Getg23()); const BoutReal lower_y_g_23 = lower_y_avg(covariantMetricTensor.Getg23()); const BoutReal lower_y_dy = lower_y_avg(dy); const BoutReal lower_y_value = -lower_y_A * lower_y_J * lower_y_g23 * lower_y_g_23 - / (lower_y_g_22 * j[i] * lower_y_dy * dy[i]); + / (lower_y_g_22 * this_J[i] * lower_y_dy * dy[i]); result[i] += lower_y_value * (f[i.ym()] - f[i]); } @@ -2062,5 +2062,5 @@ const MetricTensor::FieldMetric& Coordinates::g23() const { return contravariantMetricTensor.Getg23(); } const MetricTensor::FieldMetric Coordinates::J() const { - return j; + return this_J; } From 1b991ddb126f617252472444d12f7d8354af8a15 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Sat, 18 Nov 2023 10:26:39 +0000 Subject: [PATCH 137/491] Make Bxy private. --- examples/2Dturbulence_multigrid/esel.cxx | 2 +- examples/6field-simple/elm_6f.cxx | 2 +- examples/conducting-wall-mode/cwm.cxx | 12 +-- examples/constraints/alfven-wave/alfven.cxx | 6 +- examples/dalf3/dalf3.cxx | 2 +- .../elm-pb-outerloop/elm_pb_outerloop.cxx | 2 +- examples/elm-pb/elm_pb.cxx | 2 +- examples/em-drift/2fluid.cxx | 10 +- examples/gravity_reduced/gravity_reduced.cxx | 36 +++---- examples/gyro-gem/gem.cxx | 12 +-- examples/jorek-compare/jorek_compare.cxx | 2 +- examples/lapd-drift/lapd_drift.cxx | 16 ++-- examples/laplacexy/alfven-wave/alfven.cxx | 6 +- examples/laplacexy/laplace_perp/test.cxx | 2 +- examples/reconnect-2field/2field.cxx | 26 ++--- examples/shear-alfven-wave/2fluid.cxx | 10 +- examples/tokamak-2fluid/2fluid.cxx | 12 +-- examples/uedge-benchmark/ue_bmark.cxx | 6 +- examples/wave-slab/wave_slab.cxx | 6 +- include/bout/coordinates.hxx | 6 +- src/field/vecops.cxx | 8 +- src/mesh/coordinates.cxx | 94 ++++++++++--------- src/mesh/coordinates_accessor.cxx | 6 +- src/mesh/difops.cxx | 14 +-- src/mesh/fv_ops.cxx | 4 +- tests/MMS/GBS/gbs.cxx | 22 ++--- tests/MMS/elm-pb/elm_pb.cxx | 2 +- tests/MMS/tokamak/tokamak.cxx | 6 +- .../test-drift-instability/2fluid.cxx | 10 +- .../test-interchange-instability/2fluid.cxx | 10 +- .../integrated/test-laplacexy/loadmetric.cxx | 6 +- tests/unit/mesh/test_coordinates.cxx | 8 +- tests/unit/test_extras.hxx | 4 +- 33 files changed, 191 insertions(+), 181 deletions(-) diff --git a/examples/2Dturbulence_multigrid/esel.cxx b/examples/2Dturbulence_multigrid/esel.cxx index ff9332f12b..916e0a7bb9 100644 --- a/examples/2Dturbulence_multigrid/esel.cxx +++ b/examples/2Dturbulence_multigrid/esel.cxx @@ -69,7 +69,7 @@ class ESEL : public PhysicsModel { Coordinates* coord = mesh->getCoordinates(); // generate coordinate system - coord->Bxy = 1; + coord->Bxy() = 1; MetricTensor::FieldMetric g11, g22, g33, g12, g13, g23; g11 = 1.0; diff --git a/examples/6field-simple/elm_6f.cxx b/examples/6field-simple/elm_6f.cxx index a2fe370025..7036be72c1 100644 --- a/examples/6field-simple/elm_6f.cxx +++ b/examples/6field-simple/elm_6f.cxx @@ -1061,7 +1061,7 @@ class Elm_6f : public PhysicsModel { coord->setContravariantMetricTensor(MetricTensor(g11, g22, g33, g12, g13, g23)); coord->J = hthe / Bpxy; - coord->Bxy = B0; + coord->Bxy() = B0; MetricTensor::FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; g_11 = 1.0 / coord->g11() + SQ(I * Rxy); diff --git a/examples/conducting-wall-mode/cwm.cxx b/examples/conducting-wall-mode/cwm.cxx index 4694824ebd..bbff227b6a 100644 --- a/examples/conducting-wall-mode/cwm.cxx +++ b/examples/conducting-wall-mode/cwm.cxx @@ -181,7 +181,7 @@ class CWM : public PhysicsModel { // Normalise magnetic field Bpxy /= (bmag / 1.e4); Btxy /= (bmag / 1.e4); - coord->Bxy /= (bmag / 1.e4); + coord->Bxy() /= (bmag / 1.e4); // Set nu nu = nu_hat * Ni0 / pow(Te0, 1.5); @@ -191,7 +191,7 @@ class CWM : public PhysicsModel { MetricTensor::FieldMetric g11, g22, g33, g12, g13, g23; g11 = SQ(Rxy * Bpxy); g22 = 1.0 / SQ(hthe); - g33 = SQ(I) * coord->g11() + SQ(coord->Bxy) / coord->g11(); + g33 = SQ(I) * coord->g11() + SQ(coord->Bxy()) / coord->g11(); g12 = 0.0; g13 = -I * coord->g11(); g23 = -Btxy / (hthe * Bpxy * Rxy); @@ -201,7 +201,7 @@ class CWM : public PhysicsModel { MetricTensor::FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; g_11 = 1.0 / coord->g11() + SQ(I * Rxy); - g_22 = SQ(coord->Bxy * hthe / Bpxy); + g_22 = SQ(coord->Bxy() * hthe / Bpxy); g_33 = Rxy * Rxy; g_12 = Btxy * hthe * I * Rxy / Bpxy; g_13 = I * Rxy * Rxy; @@ -346,7 +346,7 @@ class CWM : public PhysicsModel { result = VDDX(DDZ(p), f); } else { // Use full expression with all terms - result = b0xGrad_dot_Grad(p, f) / coord->Bxy; + result = b0xGrad_dot_Grad(p, f) / coord->Bxy(); } return result; } @@ -358,7 +358,7 @@ class CWM : public PhysicsModel { result = VDDZ(-DDX(p), f); } else { // Use full expression with all terms - result = b0xGrad_dot_Grad(p, f) / coord->Bxy; + result = b0xGrad_dot_Grad(p, f) / coord->Bxy(); } return result; } @@ -370,7 +370,7 @@ class CWM : public PhysicsModel { result = VDDX(DDZ(p), f) + VDDZ(-DDX(p), f); } else { // Use full expression with all terms - result = b0xGrad_dot_Grad(p, f) / coord->Bxy; + result = b0xGrad_dot_Grad(p, f) / coord->Bxy(); } return result; } diff --git a/examples/constraints/alfven-wave/alfven.cxx b/examples/constraints/alfven-wave/alfven.cxx index cac3ea4d40..a753803d03 100644 --- a/examples/constraints/alfven-wave/alfven.cxx +++ b/examples/constraints/alfven-wave/alfven.cxx @@ -183,7 +183,7 @@ class Alfven : public PhysicsModel { Bpxy /= Bnorm; Btxy /= Bnorm; - coord->Bxy /= Bnorm; + coord->Bxy() /= Bnorm; // Check type of parallel transform std::string ptstr = @@ -204,7 +204,7 @@ class Alfven : public PhysicsModel { MetricTensor::FieldMetric g11, g22, g33, g12, g13, g23; g11 = SQ(Rxy * Bpxy); g22 = 1.0 / SQ(hthe); - g33 = SQ(sinty) * coord->g11() + SQ(coord->Bxy) / coord->g11(); + g33 = SQ(sinty) * coord->g11() + SQ(coord->Bxy()) / coord->g11(); g12 = 0.0; g13 = -sinty * coord->g11(); g23 = -sbp * Btxy / (hthe * Bpxy * Rxy); @@ -214,7 +214,7 @@ class Alfven : public PhysicsModel { MetricTensor::FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; g_11 = 1.0 / coord->g11() + SQ(sinty * Rxy); - g_22 = SQ(coord->Bxy * hthe / Bpxy); + g_22 = SQ(coord->Bxy() * hthe / Bpxy); g_33 = Rxy * Rxy; g_12 = sbp * Btxy * hthe * sinty * Rxy / Bpxy; g_13 = sinty * Rxy * Rxy; diff --git a/examples/dalf3/dalf3.cxx b/examples/dalf3/dalf3.cxx index 2fd8e6326e..823fd94d42 100644 --- a/examples/dalf3/dalf3.cxx +++ b/examples/dalf3/dalf3.cxx @@ -263,7 +263,7 @@ class DALF3 : public PhysicsModel { coord->setContravariantMetricTensor(MetricTensor(g11, g22, g33, g12, g13, g23)); coord->J = hthe / Bpxy; - coord->Bxy = B0; + coord->Bxy() = B0; MetricTensor::FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; g_11 = 1.0 / coord->g11() + SQ(I * Rxy); diff --git a/examples/elm-pb-outerloop/elm_pb_outerloop.cxx b/examples/elm-pb-outerloop/elm_pb_outerloop.cxx index 18a3acdd56..65a1935e1f 100644 --- a/examples/elm-pb-outerloop/elm_pb_outerloop.cxx +++ b/examples/elm-pb-outerloop/elm_pb_outerloop.cxx @@ -1100,7 +1100,7 @@ class ELMpb : public PhysicsModel { metric->g23 = -Btxy / (hthe * Bpxy * Rxy); metric->J() = hthe / Bpxy; - metric->Bxy = B0; + metric->Bxy() = B0; metric->g_11 = 1.0 / metric->g11 + SQ(I * Rxy); metric->g_22 = SQ(B0 * hthe / Bpxy); diff --git a/examples/elm-pb/elm_pb.cxx b/examples/elm-pb/elm_pb.cxx index 91a7e067da..4b934fb880 100644 --- a/examples/elm-pb/elm_pb.cxx +++ b/examples/elm-pb/elm_pb.cxx @@ -1041,7 +1041,7 @@ class ELMpb : public PhysicsModel { metric->g23 = -Btxy / (hthe * Bpxy * Rxy); metric->J() = hthe / Bpxy; - metric->Bxy = B0; + metric->Bxy() = B0; metric->g_11 = 1.0 / metric->g11 + SQ(I * Rxy); metric->g_22 = SQ(B0 * hthe / Bpxy); diff --git a/examples/em-drift/2fluid.cxx b/examples/em-drift/2fluid.cxx index fb9280e07f..bc78e5b58f 100644 --- a/examples/em-drift/2fluid.cxx +++ b/examples/em-drift/2fluid.cxx @@ -174,14 +174,14 @@ class EMdrift : public PhysicsModel { // Normalise magnetic field Bpxy /= (bmag / 1.e4); Btxy /= (bmag / 1.e4); - coord->Bxy /= (bmag / 1.e4); + coord->Bxy() /= (bmag / 1.e4); /**************** CALCULATE METRICS ******************/ MetricTensor::FieldMetric g11, g22, g33, g12, g13, g23; g11 = SQ(Rxy * Bpxy); g22 = 1.0 / SQ(hthe); - g33 = SQ(I) * coord->g11() + SQ(coord->Bxy) / coord->g11(); + g33 = SQ(I) * coord->g11() + SQ(coord->Bxy()) / coord->g11(); g12 = 0.0; g13 = -I * coord->g11(); g23 = -Btxy / (hthe * Bpxy * Rxy); @@ -191,7 +191,7 @@ class EMdrift : public PhysicsModel { MetricTensor::FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; g_11 = 1.0 / coord->g11() + SQ(I * Rxy); - g_22 = SQ(coord->Bxy * hthe / Bpxy); + g_22 = SQ(coord->Bxy() * hthe / Bpxy); g_33 = Rxy * Rxy; g_12 = Btxy * hthe * I * Rxy / Bpxy; g_13 = I * Rxy * Rxy; @@ -249,7 +249,7 @@ class EMdrift : public PhysicsModel { } // just define a macro for V_E dot Grad -#define vE_Grad(f, p) (b0xGrad_dot_Grad(p, f) / coord->Bxy) +#define vE_Grad(f, p) (b0xGrad_dot_Grad(p, f) / coord->Bxy()) int rhs(BoutReal UNUSED(t)) override { @@ -304,7 +304,7 @@ class EMdrift : public PhysicsModel { // VORTICITY - ddt(rho) = SQ(coord->Bxy) * Div_par(jpar); + ddt(rho) = SQ(coord->Bxy()) * Div_par(jpar); // AJPAR diff --git a/examples/gravity_reduced/gravity_reduced.cxx b/examples/gravity_reduced/gravity_reduced.cxx index 2d4fb7ec94..c58d61a48c 100644 --- a/examples/gravity_reduced/gravity_reduced.cxx +++ b/examples/gravity_reduced/gravity_reduced.cxx @@ -173,21 +173,21 @@ class GravityReduced : public PhysicsModel { ddt(Psi) = -(1 / B0) * Grad_par(B0 * phi, CELL_YLOW); // + 1e-2*Jpar; if (nonlinear) { - ddt(Psi) += (1 / B0) * bracket(Psi, B0 * phi, bm) * coord->Bxy; + ddt(Psi) += (1 / B0) * bracket(Psi, B0 * phi, bm) * coord->Bxy(); } //Parallel vorticity ddt(U) = (SQ(B0) / rho0) * (Grad_par(Jpar / interp_to(B0, CELL_YLOW), CELL_CENTRE)); - ddt(U) -= (1 / rho0) * bracket(G, rho, bm) * coord->Bxy; + ddt(U) -= (1 / rho0) * bracket(G, rho, bm) * coord->Bxy(); - ddt(U) -= (SQ(B0) / rho0) * bracket(Psi, Jpar0 / B0, bm) * coord->Bxy; + ddt(U) -= (SQ(B0) / rho0) * bracket(Psi, Jpar0 / B0, bm) * coord->Bxy(); if (nonlinear) { - ddt(U) -= bracket(phi, U, bm) * coord->Bxy; + ddt(U) -= bracket(phi, U, bm) * coord->Bxy(); - ddt(U) -= (SQ(B0) / rho0) * bracket(Psi, Jpar / B0, bm) * coord->Bxy; + ddt(U) -= (SQ(B0) / rho0) * bracket(Psi, Jpar / B0, bm) * coord->Bxy(); } // Viscosity terms @@ -200,43 +200,43 @@ class GravityReduced : public PhysicsModel { } // Parallel velocity - ddt(Vpar) = bracket(Psi, p0, bm) * coord->Bxy / rho0; + ddt(Vpar) = bracket(Psi, p0, bm) * coord->Bxy() / rho0; ddt(Vpar) += -(Grad_par(p, CELL_YLOW)) / rho0; - ddt(Vpar) += bracket(G, Psi, bm) * coord->Bxy; + ddt(Vpar) += bracket(G, Psi, bm) * coord->Bxy(); if (nonlinear) { - ddt(Vpar) -= bracket(phi, Vpar, bm) * coord->Bxy; + ddt(Vpar) -= bracket(phi, Vpar, bm) * coord->Bxy(); - ddt(Vpar) += bracket(Psi, p, bm) * coord->Bxy / rho0; + ddt(Vpar) += bracket(Psi, p, bm) * coord->Bxy() / rho0; } //Pressure ddt(p) = -bracket(phi, p0, bm); ddt(p) += -((Gamma * p0) / (1 + Gamma * p0 * mu_0 / SQ(B0))) - * ((rho0 * mu_0 / SQ(B0)) * bracket(G, phi, bm) * coord->Bxy + * ((rho0 * mu_0 / SQ(B0)) * bracket(G, phi, bm) * coord->Bxy() + Grad_par(Vpar, CELL_CENTRE) - (Vpar / B0) * Grad_par(B0)); if (nonlinear) { - ddt(p) -= bracket(phi, p, bm) * coord->Bxy; + ddt(p) -= bracket(phi, p, bm) * coord->Bxy(); ddt(p) += ((Gamma * p0) / (1 + Gamma * p0 * mu_0 / SQ(B0))) * bracket(Psi, Vpar, bm) - * coord->Bxy; + * coord->Bxy(); } //Density - ddt(rho) = -bracket(phi, rho0, bm) * coord->Bxy; + ddt(rho) = -bracket(phi, rho0, bm) * coord->Bxy(); ddt(rho) -= (rho0 / (1 + Gamma * p0 * mu_0 / SQ(B0))) - * ((rho0 * mu_0 / SQ(B0)) * bracket(G, phi, bm) * coord->Bxy - + Grad_par(Vpar, CELL_CENTRE) - bracket(Psi, Vpar, bm) * coord->Bxy - - (Vpar / B0) * Grad_par(B0)); + * ((rho0 * mu_0 / SQ(B0)) * bracket(G, phi, bm) * coord->Bxy() + + Grad_par(Vpar, CELL_CENTRE) - bracket(Psi, Vpar, bm) * coord->Bxy() + - (Vpar / B0) * Grad_par(B0)); if (nonlinear) { - ddt(rho) -= bracket(phi, rho, bm) * coord->Bxy; + ddt(rho) -= bracket(phi, rho, bm) * coord->Bxy(); ddt(rho) += ((rho0) / (1 + Gamma * p0 * mu_0 / SQ(B0))) * bracket(Psi, Vpar, bm) - * coord->Bxy; + * coord->Bxy(); } // Iterate over the lower Y boundary diff --git a/examples/gyro-gem/gem.cxx b/examples/gyro-gem/gem.cxx index f962b3c44c..845040bd85 100644 --- a/examples/gyro-gem/gem.cxx +++ b/examples/gyro-gem/gem.cxx @@ -379,7 +379,7 @@ class GEM : public PhysicsModel { coord->setContravariantMetricTensor(MetricTensor(g11, g22, g33, g12, g13, g23)); coord->J = hthe / Bpxy; - coord->Bxy = Bxy; + coord->Bxy() = Bxy; MetricTensor::FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; g_11 = 1.0 / coord->g11(); @@ -404,7 +404,7 @@ class GEM : public PhysicsModel { if (curv_logB) { Grad_par_logB = Grad_par(logB); } else { - Grad_par_logB = Grad_par(log(coord->Bxy)); + Grad_par_logB = Grad_par(log(coord->Bxy())); } } else { Grad_par_logB = 0.; @@ -1157,7 +1157,7 @@ class GEM : public PhysicsModel { if (curv_logB) { return -bracket(2. * logB, f, BRACKET_ARAKAWA); } - return -bracket(2. * log(coord->Bxy), f, BRACKET_ARAKAWA); + return -bracket(2. * log(coord->Bxy()), f, BRACKET_ARAKAWA); } //////////////////////////////////////////////////////////////////////// @@ -1172,7 +1172,7 @@ class GEM : public PhysicsModel { delp2.applyBoundary("neumann"); mesh->communicate(delp2); - return nu_perp * Delp2(delp2 * SQ(SQ(1. / coord->Bxy))) + return nu_perp * Delp2(delp2 * SQ(SQ(1. / coord->Bxy()))) - nu_par * Grad2_par2(f) // NB: This should be changed for variable B ; } @@ -1188,8 +1188,8 @@ class GEM : public PhysicsModel { } const Field3D Div_parP(const Field3D& f, CELL_LOC loc = CELL_DEFAULT) { - return interp_to(coord->Bxy, loc) - * Grad_parP(f / interp_to(coord->Bxy, f.getLocation()), loc); + return interp_to(coord->Bxy(), loc) + * Grad_parP(f / interp_to(coord->Bxy(), f.getLocation()), loc); } }; diff --git a/examples/jorek-compare/jorek_compare.cxx b/examples/jorek-compare/jorek_compare.cxx index b5e8f90684..322a52a0f6 100644 --- a/examples/jorek-compare/jorek_compare.cxx +++ b/examples/jorek-compare/jorek_compare.cxx @@ -309,7 +309,7 @@ class Jorek : public PhysicsModel { coord->setContravariantMetricTensor(MetricTensor(g11, g22, g33, g12, g13, g23)); coord->J = hthe / Bpxy; - coord->Bxy = B0; + coord->Bxy() = B0; MetricTensor::FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; g_11 = 1.0 / coord->g11() + SQ(I * Rxy); diff --git a/examples/lapd-drift/lapd_drift.cxx b/examples/lapd-drift/lapd_drift.cxx index a61b3536e4..0d9cd518b9 100644 --- a/examples/lapd-drift/lapd_drift.cxx +++ b/examples/lapd-drift/lapd_drift.cxx @@ -324,7 +324,7 @@ class LAPDdrift : public PhysicsModel { // Normalise magnetic field Bpxy /= (bmag / 1.e4); Btxy /= (bmag / 1.e4); - coord->Bxy /= (bmag / 1.e4); + coord->Bxy() /= (bmag / 1.e4); // calculate pressures pei0 = (Ti0 + Te0) * Ni0; @@ -335,7 +335,7 @@ class LAPDdrift : public PhysicsModel { MetricTensor::FieldMetric g11, g22, g33, g12, g13, g23; g11 = SQ(Rxy * Bpxy); g22 = 1.0 / SQ(hthe); - g33 = SQ(I) * coord->g11() + SQ(coord->Bxy) / coord->g11(); + g33 = SQ(I) * coord->g11() + SQ(coord->Bxy()) / coord->g11(); g12 = 0.0; g13 = -I * coord->g11(); g23 = -Btxy / (hthe * Bpxy * Rxy); @@ -345,7 +345,7 @@ class LAPDdrift : public PhysicsModel { MetricTensor::FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; g_11 = 1.0 / coord->g11() + SQ(I * Rxy); - g_22 = SQ(coord->Bxy * hthe / Bpxy); + g_22 = SQ(coord->Bxy() * hthe / Bpxy); g_33 = Rxy * Rxy; g_12 = Btxy * hthe * I * Rxy / Bpxy; g_13 = I * Rxy * Rxy; @@ -418,7 +418,7 @@ class LAPDdrift : public PhysicsModel { SAVE_ONCE(Ni0, Te0, phi0, rho0); SAVE_ONCE(Rxy, Bpxy, Btxy, Zxy, hthe); - dump.addOnce(coord->Bxy, "Bxy"); + dump.addOnce(coord->Bxy(), "Bxy"); dump.addOnce(my_ixseps, "ixseps"); SAVE_ONCE(Te_x, Ti_x, Ni_x); @@ -750,7 +750,7 @@ class LAPDdrift : public PhysicsModel { } else { // Use full expression with all terms - result = b0xGrad_dot_Grad(p, f) / mesh->getCoordinates()->Bxy; + result = b0xGrad_dot_Grad(p, f) / mesh->getCoordinates()->Bxy(); } return result; } @@ -802,7 +802,7 @@ class LAPDdrift : public PhysicsModel { result = VDDX(DDZ(p), f); } else { // Use full expression with all terms - result = b0xGrad_dot_Grad(p, f) / coord->Bxy; + result = b0xGrad_dot_Grad(p, f) / coord->Bxy(); } return result; } @@ -814,7 +814,7 @@ class LAPDdrift : public PhysicsModel { result = VDDZ(-DDX(p), f); } else { // Use full expression with all terms - result = b0xGrad_dot_Grad(p, f) / mesh->getCoordinates()->Bxy; + result = b0xGrad_dot_Grad(p, f) / mesh->getCoordinates()->Bxy(); } return result; } @@ -869,7 +869,7 @@ class LAPDdrift : public PhysicsModel { result = VDDX(DDZ(p), f) + VDDZ(-DDX(p), f); } else { // Use full expression with all terms - result = b0xGrad_dot_Grad(p, f) / coord->Bxy; + result = b0xGrad_dot_Grad(p, f) / coord->Bxy(); } return result; } diff --git a/examples/laplacexy/alfven-wave/alfven.cxx b/examples/laplacexy/alfven-wave/alfven.cxx index db1e853d9a..3d78a0e03e 100644 --- a/examples/laplacexy/alfven-wave/alfven.cxx +++ b/examples/laplacexy/alfven-wave/alfven.cxx @@ -192,7 +192,7 @@ class Alfven : public PhysicsModel { Bpxy /= Bnorm; Btxy /= Bnorm; - coord->Bxy /= Bnorm; + coord->Bxy() /= Bnorm; // Calculate metric components sinty = 0.0; // I disappears from metric for shifted coordinates @@ -205,7 +205,7 @@ class Alfven : public PhysicsModel { MetricTensor::FieldMetric g11, g22, g33, g12, g13, g23; g11 = SQ(Rxy * Bpxy); g22 = 1.0 / SQ(hthe); - g33 = SQ(sinty) * coord->g11() + SQ(coord->Bxy) / coord->g11(); + g33 = SQ(sinty) * coord->g11() + SQ(coord->Bxy()) / coord->g11(); g12 = 0.0; g13 = -sinty * coord->g11(); g23 = -sbp * Btxy / (hthe * Bpxy * Rxy); @@ -215,7 +215,7 @@ class Alfven : public PhysicsModel { MetricTensor::FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; g_11 = 1.0 / coord->g11() + SQ(sinty * Rxy); - g_22 = SQ(coord->Bxy * hthe / Bpxy); + g_22 = SQ(coord->Bxy() * hthe / Bpxy); g_33 = Rxy * Rxy; g_12 = sbp * Btxy * hthe * sinty * Rxy / Bpxy; g_13 = sinty * Rxy * Rxy; diff --git a/examples/laplacexy/laplace_perp/test.cxx b/examples/laplacexy/laplace_perp/test.cxx index 18bd8839bf..8198db1bd1 100644 --- a/examples/laplacexy/laplace_perp/test.cxx +++ b/examples/laplacexy/laplace_perp/test.cxx @@ -35,7 +35,7 @@ int main(int argc, char** argv) { coord->setContravariantMetricTensor(MetricTensor(g11, g22, g33, g12, g13, g23)); coord->J = hthe / Bpxy; - coord->Bxy = B0; + coord->Bxy() = B0; MetricTensor::FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; g_11 = 1.0 / coord->g11() + SQ(I * Rxy); diff --git a/examples/reconnect-2field/2field.cxx b/examples/reconnect-2field/2field.cxx index ee46ede474..9f7176f941 100644 --- a/examples/reconnect-2field/2field.cxx +++ b/examples/reconnect-2field/2field.cxx @@ -71,7 +71,7 @@ class TwoField : public PhysicsModel { // Load metrics GRID_LOAD(Rxy, Bpxy, Btxy, hthe); - mesh->get(coord->Bxy, "Bxy"); + mesh->get(coord->Bxy(), "Bxy"); // Set locations of staggered fields Apar.setLocation(CELL_YLOW); @@ -126,7 +126,7 @@ class TwoField : public PhysicsModel { Nenorm = 1.e19; } - Bnorm = max(coord->Bxy, true); + Bnorm = max(coord->Bxy(), true); // Sound speed in m/s Cs = sqrt(Charge * Tenorm / Mi); @@ -156,7 +156,7 @@ class TwoField : public PhysicsModel { // Normalise magnetic field Bpxy /= Bnorm; Btxy /= Bnorm; - coord->Bxy /= Bnorm; + coord->Bxy() /= Bnorm; // Plasma quantities Jpar0 /= Nenorm * Charge * Cs; @@ -166,7 +166,7 @@ class TwoField : public PhysicsModel { MetricTensor::FieldMetric g11, g22, g33, g12, g13, g23; g11 = SQ(Rxy * Bpxy); g22 = 1.0 / SQ(hthe); - g33 = SQ(coord->Bxy) / coord->g11(); + g33 = SQ(coord->Bxy()) / coord->g11(); g12 = 0.0; g13 = 0.; g23 = -Btxy / (hthe * Bpxy * Rxy); @@ -176,7 +176,7 @@ class TwoField : public PhysicsModel { MetricTensor::FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; g_11 = 1.0 / coord->g11(); - g_22 = SQ(coord->Bxy * hthe / Bpxy); + g_22 = SQ(coord->Bxy() * hthe / Bpxy); g_33 = Rxy * Rxy; g_12 = 0.; g_13 = 0.; @@ -203,7 +203,7 @@ class TwoField : public PhysicsModel { SAVE_ONCE(Apar_ext, Jpar_ext); initial_profile("Phi0_ext", Phi0_ext); - U0_ext = -Delp2(Phi0_ext) / coord->Bxy; + U0_ext = -Delp2(Phi0_ext) / coord->Bxy(); SAVE_ONCE(Phi0_ext, U0_ext); // Give the solver the preconditioner function @@ -238,7 +238,7 @@ class TwoField : public PhysicsModel { // Solve EM fields // U = (1/B) * Delp2(phi) - phi = phiSolver->solve(coord->Bxy * U); + phi = phiSolver->solve(coord->Bxy() * U); phi.applyBoundary(); // For target plates only mesh->communicate(U, phi, Apar); @@ -270,12 +270,12 @@ class TwoField : public PhysicsModel { } // VORTICITY - ddt(U) = SQ(coord->Bxy) * Grad_parP(jpar / coord_ylow->Bxy, CELL_CENTRE); + ddt(U) = SQ(coord->Bxy()) * Grad_parP(jpar / coord_ylow->Bxy(), CELL_CENTRE); if (include_jpar0) { ddt(U) -= - SQ(coord->Bxy) * beta_hat - * interp_to(bracket(Apar + Apar_ext, Jpar0 / coord_ylow->Bxy, BRACKET_ARAKAWA), + SQ(coord->Bxy()) * beta_hat + * interp_to(bracket(Apar + Apar_ext, Jpar0 / coord_ylow->Bxy(), BRACKET_ARAKAWA), CELL_CENTRE); } @@ -339,13 +339,13 @@ class TwoField : public PhysicsModel { } Field3D U1 = - ddt(U) + gamma * SQ(coord->Bxy) * Grad_par(Jp / coord_ylow->Bxy, CELL_CENTRE); + ddt(U) + gamma * SQ(coord->Bxy()) * Grad_par(Jp / coord_ylow->Bxy(), CELL_CENTRE); - inv->setCoefB(-SQ(gamma * coord->Bxy) / beta_hat); + inv->setCoefB(-SQ(gamma * coord->Bxy()) / beta_hat); ddt(U) = inv->solve(U1); ddt(U).applyBoundary(); - Field3D phip = phiSolver->solve(coord->Bxy * ddt(U)); + Field3D phip = phiSolver->solve(coord->Bxy() * ddt(U)); mesh->communicate(phip); ddt(Apar) = ddt(Apar) - (gamma / beta_hat) * Grad_par(phip, CELL_YLOW); diff --git a/examples/shear-alfven-wave/2fluid.cxx b/examples/shear-alfven-wave/2fluid.cxx index 3a2e1a5fce..869f62a6e9 100644 --- a/examples/shear-alfven-wave/2fluid.cxx +++ b/examples/shear-alfven-wave/2fluid.cxx @@ -82,7 +82,7 @@ class ShearAlfven : public PhysicsModel { GRID_LOAD(Bpxy); GRID_LOAD(Btxy); GRID_LOAD(hthe); - mesh->get(coord->Bxy, "Bxy"); + mesh->get(coord->Bxy(), "Bxy"); mesh->get(coord->dx, "dpsi"); mesh->get(I, "sinty"); @@ -174,7 +174,7 @@ class ShearAlfven : public PhysicsModel { // Normalise magnetic field Bpxy /= (bmag / 1.e4); Btxy /= (bmag / 1.e4); - coord->Bxy /= (bmag / 1.e4); + coord->Bxy() /= (bmag / 1.e4); // calculate pressures pei0 = (Ti0 + Te0) * Ni0; @@ -185,7 +185,7 @@ class ShearAlfven : public PhysicsModel { MetricTensor::FieldMetric g11, g22, g33, g12, g13, g23; g11 = SQ(Rxy * Bpxy); g22 = 1.0 / SQ(hthe); - g33 = SQ(I) * coord->g11() + SQ(coord->Bxy) / coord->g11(); + g33 = SQ(I) * coord->g11() + SQ(coord->Bxy()) / coord->g11(); g12 = 0.0; g13 = -I * coord->g11(); g23 = -Btxy / (hthe * Bpxy * Rxy); @@ -195,7 +195,7 @@ class ShearAlfven : public PhysicsModel { MetricTensor::FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; g_11 = 1.0 / coord->g11() + SQ(I * Rxy); - g_22 = SQ(coord->Bxy * hthe / Bpxy); + g_22 = SQ(coord->Bxy() * hthe / Bpxy); g_33 = Rxy * Rxy; g_12 = Btxy * hthe * I * Rxy / Bpxy; g_13 = I * Rxy * Rxy; @@ -261,7 +261,7 @@ class ShearAlfven : public PhysicsModel { } // VORTICITY - ddt(rho) = SQ(coord->Bxy) * Div_par(jpar, CELL_CENTRE); + ddt(rho) = SQ(coord->Bxy()) * Div_par(jpar, CELL_CENTRE); // AJPAR diff --git a/examples/tokamak-2fluid/2fluid.cxx b/examples/tokamak-2fluid/2fluid.cxx index fdb9720f39..e30da633d8 100644 --- a/examples/tokamak-2fluid/2fluid.cxx +++ b/examples/tokamak-2fluid/2fluid.cxx @@ -390,7 +390,7 @@ class TwoFluid : public PhysicsModel { // Normalise magnetic field Bpxy /= (bmag / 1.e4); Btxy /= (bmag / 1.e4); - coord->Bxy /= (bmag / 1.e4); + coord->Bxy() /= (bmag / 1.e4); // calculate pressures pei0 = (Ti0 + Te0) * Ni0; @@ -402,7 +402,7 @@ class TwoFluid : public PhysicsModel { MetricTensor::FieldMetric g11, g22, g33, g12, g13, g23; g11 = SQ(Rxy * Bpxy); g22 = 1.0 / SQ(hthe); - g33 = SQ(I) * coord->g11() + SQ(coord->Bxy) / coord->g11(); + g33 = SQ(I) * coord->g11() + SQ(coord->Bxy()) / coord->g11(); g12 = 0.0; g13 = -I * coord->g11(); g23 = -Btxy / (hthe * Bpxy * Rxy); @@ -412,7 +412,7 @@ class TwoFluid : public PhysicsModel { MetricTensor::FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; g_11 = 1.0 / coord->g11() + SQ(I * Rxy); - g_22 = SQ(coord->Bxy * hthe / Bpxy); + g_22 = SQ(coord->Bxy() * hthe / Bpxy); g_33 = Rxy * Rxy; g_12 = Btxy * hthe * I * Rxy / Bpxy; g_13 = I * Rxy * Rxy; @@ -875,14 +875,14 @@ class TwoFluid : public PhysicsModel { if (rho_pei1) { if (curv_upwind) { - ddt(rho) += 2.0 * coord->Bxy * V_dot_Grad(b0xcv, pei); // Use upwinding + ddt(rho) += 2.0 * coord->Bxy() * V_dot_Grad(b0xcv, pei); // Use upwinding } else { - ddt(rho) += 2.0 * coord->Bxy * b0xcv * Grad(pei); // Use central differencing + ddt(rho) += 2.0 * coord->Bxy() * b0xcv * Grad(pei); // Use central differencing } } if (rho_jpar1) { - ddt(rho) += SQ(coord->Bxy) * Div_par(jpar, CELL_CENTRE); + ddt(rho) += SQ(coord->Bxy()) * Div_par(jpar, CELL_CENTRE); } if (rho_rho1) { diff --git a/examples/uedge-benchmark/ue_bmark.cxx b/examples/uedge-benchmark/ue_bmark.cxx index 2d03885c45..94c43bd7de 100644 --- a/examples/uedge-benchmark/ue_bmark.cxx +++ b/examples/uedge-benchmark/ue_bmark.cxx @@ -120,7 +120,7 @@ class UedgeBenchmark : public PhysicsModel { // Normalise magnetic field Bpxy /= (bmag / 1e4); Btxy /= (bmag / 1e4); - coords->Bxy /= (bmag / 1e4); + coords->Bxy() /= (bmag / 1e4); // calculate pressures pei0 = (Ti0 + Te0) * Ni0; @@ -142,7 +142,7 @@ class UedgeBenchmark : public PhysicsModel { coords->g11 = pow(Rxy * Bpxy, 2.0); coords->g22 = 1.0 / pow(hthe, 2.0); - coords->g33 = pow(coords->Bxy, 2.0) / coords->g11; + coords->g33 = pow(coords->Bxy(), 2.0) / coords->g11; coords->g12 = 0.0; coords->g13 = 0.0; coords->g23 = -Btxy / (hthe * Bpxy * Rxy); @@ -150,7 +150,7 @@ class UedgeBenchmark : public PhysicsModel { coords->J = hthe / Bpxy; coords->g_11 = 1.0 / coords->g11; - coords->g_22 = pow(coords->Bxy * hthe / Bpxy, 2.0); + coords->g_22 = pow(coords->Bxy() * hthe / Bpxy, 2.0); coords->g_33 = Rxy * Rxy; coords->g_12 = 0.0; coords->g_13 = 0.0; diff --git a/examples/wave-slab/wave_slab.cxx b/examples/wave-slab/wave_slab.cxx index 4169b63dab..17f24738cc 100644 --- a/examples/wave-slab/wave_slab.cxx +++ b/examples/wave-slab/wave_slab.cxx @@ -21,7 +21,7 @@ class WaveTest : public PhysicsModel { GRID_LOAD(Bpxy); GRID_LOAD(Btxy); GRID_LOAD(hthe); - mesh->get(coords->Bxy, "Bxy"); + mesh->get(coords->Bxy(), "Bxy"); int ShiftXderivs = 0; mesh->get(ShiftXderivs, "false"); if (ShiftXderivs) { @@ -33,7 +33,7 @@ class WaveTest : public PhysicsModel { coords->g11 = pow(Rxy * Bpxy, 2.0); coords->g22 = 1.0 / pow(hthe, 2.0); - coords->g33 = pow(I, 2.0) * coords->g11 + pow(coords->Bxy, 2.0) / coords->g11; + coords->g33 = pow(I, 2.0) * coords->g11 + pow(coords->Bxy(), 2.0) / coords->g11; coords->g12 = 0.0; coords->g13 = -I * coords->g11; coords->g23 = -Btxy / (hthe * Bpxy * Rxy); @@ -41,7 +41,7 @@ class WaveTest : public PhysicsModel { coords->J = hthe / Bpxy; coords->g_11 = 1.0 / coords->g11 + (pow(I * Rxy, 2.0)); - coords->g_22 = pow(coords->Bxy * hthe / Bpxy, 2.0); + coords->g_22 = pow(coords->Bxy() * hthe / Bpxy, 2.0); coords->g_33 = Rxy * Rxy; coords->g_12 = Btxy * hthe * I * Rxy / Bpxy; coords->g_13 = I * Rxy * Rxy; diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index 17ef5917fe..98e5963d2d 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -93,8 +93,6 @@ public: /// 2nd-order correction for non-uniform meshes d/di(1/dx), d/di(1/dy) and d/di(1/dz) FieldMetric d1_dx, d1_dy, d1_dz; - FieldMetric Bxy; ///< Magnitude of B = nabla z times nabla x - /// Covariant metric tensor const FieldMetric& g_11() const; const FieldMetric& g_22() const; @@ -114,6 +112,9 @@ public: ///< Coordinate system Jacobian, so volume of cell is J*dx*dy*dz const FieldMetric J() const; + ///< Magnitude of B = nabla z times nabla x + const FieldMetric Bxy() const; + void setContravariantMetricTensor(MetricTensor metric_tensor, const std::string& region = "RGN_ALL"); @@ -273,6 +274,7 @@ private: MetricTensor covariantMetricTensor; FieldMetric this_J; + FieldMetric this_Bxy; ///< Magnitude of B = nabla z times nabla x }; /* diff --git a/src/field/vecops.cxx b/src/field/vecops.cxx index 14079d6691..1368e5d0e1 100644 --- a/src/field/vecops.cxx +++ b/src/field/vecops.cxx @@ -106,10 +106,10 @@ Vector3D Grad_perp(const Field3D& f, CELL_LOC outloc, const std::string& method) Vector3D result(f.getMesh()); result.x = DDX(f, outloc, method) - - metric->g_12() * DDY(f, outloc, method) / SQ(metric->J() * metric->Bxy); + - metric->g_12() * DDY(f, outloc, method) / SQ(metric->J() * metric->Bxy()); result.y = 0.0; result.z = DDZ(f, outloc, method) - - metric->g_23() * DDY(f, outloc, method) / SQ(metric->J() * metric->Bxy); + - metric->g_23() * DDY(f, outloc, method) / SQ(metric->J() * metric->Bxy()); result.setLocation(result.x.getLocation()); @@ -128,9 +128,9 @@ Vector2D Grad_perp(const Field2D& f, CELL_LOC outloc, const std::string& method) Vector2D result(f.getMesh()); result.x = DDX(f, outloc, method) - - metric->g_12() * DDY(f, outloc, method) / SQ(metric->J() * metric->Bxy); + - metric->g_12() * DDY(f, outloc, method) / SQ(metric->J() * metric->Bxy()); result.y = 0.0; - result.z = -metric->g_23() * DDY(f, outloc, method) / SQ(metric->J() * metric->Bxy); + result.z = -metric->g_23() * DDY(f, outloc, method) / SQ(metric->J() * metric->Bxy()); result.setLocation(result.x.getLocation()); diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index af701ccf30..ed01778332 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -382,15 +382,16 @@ Coordinates::Coordinates(Mesh* mesh, FieldMetric dx, FieldMetric dy, FieldMetric FieldMetric g_33, FieldMetric g_12, FieldMetric g_13, FieldMetric g_23, FieldMetric ShiftTorsion, FieldMetric IntShiftTorsion) - : dx(std::move(dx)), dy(std::move(dy)), dz(dz), this_J(std::move(J)), Bxy(std::move(Bxy)), - contravariantMetricTensor(g11, g22, g33, g12, g13, g23), + : dx(std::move(dx)), dy(std::move(dy)), dz(dz), this_J(std::move(J)), + this_Bxy(std::move(Bxy)), contravariantMetricTensor(g11, g22, g33, g12, g13, g23), covariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23), ShiftTorsion(std::move(ShiftTorsion)), IntShiftTorsion(std::move(IntShiftTorsion)), nz(mesh->LocalNz), localmesh(mesh), location(CELL_CENTRE) {} Coordinates::Coordinates(Mesh* mesh, Options* options) : dx(1., mesh), dy(1., mesh), dz(1., mesh), d1_dx(mesh), d1_dy(mesh), d1_dz(mesh), - this_J(1., mesh), Bxy(1., mesh), contravariantMetricTensor(1., 1., 1., 0, 0, 0, mesh), + this_J(1., mesh), this_Bxy(1., mesh), + contravariantMetricTensor(1., 1., 1., 0, 0, 0, mesh), covariantMetricTensor(1., 1., 1., 0, 0, 0, mesh), G1_11(mesh), G1_22(mesh), G1_33(mesh), G1_12(mesh), G1_13(mesh), G1_23(mesh), G2_11(mesh), G2_22(mesh), G2_33(mesh), G2_12(mesh), G2_13(mesh), G2_23(mesh), G3_11(mesh), G3_22(mesh), @@ -577,7 +578,7 @@ Coordinates::Coordinates(Mesh* mesh, Options* options) communicate(this_J); // Re-evaluate Bxy using new J - Bxy = sqrt(g_22) / this_J; + this_Bxy = sqrt(g_22) / this_J; } // Check jacobian @@ -588,21 +589,22 @@ Coordinates::Coordinates(Mesh* mesh, Options* options) } // Attempt to read Bxy from the grid file - auto Bcalc = Bxy; - if (mesh->get(Bxy, "Bxy", 0.0, false)) { + auto Bcalc = this_Bxy; + if (mesh->get(this_Bxy, "Bxy", 0.0, false)) { output_warn.write("\tWARNING: Magnitude of B field 'Bxy' not found. Calculating from " "metric tensor\n"); - Bxy = Bcalc; + this_Bxy = Bcalc; } else { - Bxy = interpolateAndExtrapolate(Bxy, location, extrapolate_x, extrapolate_y, false, - transform.get()); - output_warn.write("\tMaximum difference in Bxy is {:e}\n", max(abs(Bxy - Bcalc))); + this_Bxy = interpolateAndExtrapolate(this_Bxy, location, extrapolate_x, extrapolate_y, + false, transform.get()); + output_warn.write("\tMaximum difference in Bxy is {:e}\n", + max(abs(this_Bxy - Bcalc))); } // Check Bxy - bout::checkFinite(Bxy, "Bxy", "RGN_NOCORNERS"); - bout::checkPositive(Bxy, "Bxy", "RGN_NOCORNERS"); + bout::checkFinite(this_Bxy, "Bxy", "RGN_NOCORNERS"); + bout::checkPositive(this_Bxy, "Bxy", "RGN_NOCORNERS"); if (mesh->get(ShiftTorsion, "ShiftTorsion", 0.0, false)) { output_warn.write( @@ -629,7 +631,8 @@ Coordinates::Coordinates(Mesh* mesh, Options* options) Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, const Coordinates* coords_in, bool force_interpolate_from_centre) : dx(1., mesh), dy(1., mesh), dz(1., mesh), d1_dx(mesh), d1_dy(mesh), d1_dz(mesh), - _J(1., mesh), Bxy(1., mesh), contravariantMetricTensor(1., 1., 1., 0, 0, 0, mesh), + this_J(1., mesh), this_Bxy(1., mesh), + contravariantMetricTensor(1., 1., 1., 0, 0, 0, mesh), covariantMetricTensor(1., 1., 1., 0, 0, 0, mesh), G1_11(mesh), G1_22(mesh), G1_33(mesh), G1_12(mesh), G1_13(mesh), G1_23(mesh), G2_11(mesh), G2_22(mesh), G2_33(mesh), G2_12(mesh), G2_13(mesh), G2_23(mesh), G3_11(mesh), G3_22(mesh), @@ -802,14 +805,14 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, suffix); this_J = Jcalc; } else { - this_J = interpolateAndExtrapolate(this_J, location, extrapolate_x, extrapolate_y, false, - transform.get()); + this_J = interpolateAndExtrapolate(this_J, location, extrapolate_x, extrapolate_y, + false, transform.get()); // Compare calculated and loaded values output_warn.write("\tMaximum difference in J is %e\n", max(abs(this_J - Jcalc))); // Re-evaluate Bxy using new J - Bxy = sqrt(g_22) / this_J; + this_Bxy = sqrt(g_22) / this_J; } // Check jacobian @@ -820,23 +823,24 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, } // Attempt to read Bxy from the grid file - auto Bcalc = Bxy; - if (getAtLoc(mesh, Bxy, "Bxy", suffix, location)) { + auto Bcalc = this_Bxy; + if (getAtLoc(mesh, this_Bxy, "Bxy", suffix, location)) { output_warn.write( "\tWARNING: Magnitude of B field 'Bxy_{:s}' not found. Calculating " " from metric tensor\n", suffix); - Bxy = Bcalc; + this_Bxy = Bcalc; } else { - Bxy = interpolateAndExtrapolate(Bxy, location, extrapolate_x, extrapolate_y, false, - transform.get()); + this_Bxy = interpolateAndExtrapolate(this_Bxy, location, extrapolate_x, + extrapolate_y, false, transform.get()); - output_warn.write("\tMaximum difference in Bxy is %e\n", max(abs(Bxy - Bcalc))); + output_warn.write("\tMaximum difference in Bxy is %e\n", + max(abs(this_Bxy - Bcalc))); } // Check Bxy - bout::checkFinite(Bxy, "Bxy" + suffix, "RGN_NOCORNERS"); - bout::checkPositive(Bxy, "Bxy" + suffix, "RGN_NOCORNERS"); + bout::checkFinite(this_Bxy, "Bxy" + suffix, "RGN_NOCORNERS"); + bout::checkPositive(this_Bxy, "Bxy" + suffix, "RGN_NOCORNERS"); checkStaggeredGet(mesh, "ShiftTorsion", suffix); if (mesh->get(ShiftTorsion, "ShiftTorsion" + suffix, 0.0, false)) { @@ -912,14 +916,14 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, checkCovariant(); this_J = interpolateAndExtrapolate(coords_in->J(), location, true, true, false, - transform.get()); - Bxy = interpolateAndExtrapolate(coords_in->Bxy, location, true, true, false, - transform.get()); + transform.get()); + this_Bxy = interpolateAndExtrapolate(coords_in->Bxy(), location, true, true, false, + transform.get()); bout::checkFinite(this_J, "The Jacobian", "RGN_NOCORNERS"); bout::checkPositive(this_J, "The Jacobian", "RGN_NOCORNERS"); - bout::checkFinite(Bxy, "Bxy", "RGN_NOCORNERS"); - bout::checkPositive(Bxy, "Bxy", "RGN_NOCORNERS"); + bout::checkFinite(this_Bxy, "Bxy", "RGN_NOCORNERS"); + bout::checkPositive(this_Bxy, "Bxy", "RGN_NOCORNERS"); ShiftTorsion = interpolateAndExtrapolate(coords_in->ShiftTorsion, location, true, true, false, transform.get()); @@ -993,7 +997,7 @@ void Coordinates::outputVars(Options& output_options) { "Coordinates"); output_options["J" + loc_string].force(this_J, "Coordinates"); - output_options["Bxy" + loc_string].force(Bxy, "Coordinates"); + output_options["Bxy" + loc_string].force(this_Bxy, "Coordinates"); output_options["G1" + loc_string].force(G1, "Coordinates"); output_options["G2" + loc_string].force(G2, "Coordinates"); @@ -1027,7 +1031,7 @@ int Coordinates::geometry(bool recalculate_staggered, contravariantMetricTensor.Getg23(), covariantMetricTensor.Getg11(), covariantMetricTensor.Getg22(), covariantMetricTensor.Getg33(), covariantMetricTensor.Getg12(), covariantMetricTensor.Getg13(), - covariantMetricTensor.Getg23(), this_J, Bxy); + covariantMetricTensor.Getg23(), this_J, this_Bxy); output_progress.write("Calculating differential geometry terms\n"); @@ -1421,12 +1425,12 @@ int Coordinates::jacobian() { this_J = 1. / sqrt(g); // More robust to extrapolate derived quantities directly, rather than // deriving from extrapolated covariant metric components - this_J = interpolateAndExtrapolate(this_J, location, extrapolate_x, extrapolate_y, false, - transform.get()); + this_J = interpolateAndExtrapolate(this_J, location, extrapolate_x, extrapolate_y, + false, transform.get()); - Bxy = sqrt(covariantMetricTensor.Getg22()) / this_J; - Bxy = interpolateAndExtrapolate(Bxy, location, extrapolate_x, extrapolate_y, false, - transform.get()); + this_Bxy = sqrt(covariantMetricTensor.Getg22()) / this_J; + this_Bxy = interpolateAndExtrapolate(this_Bxy, location, extrapolate_x, extrapolate_y, + false, transform.get()); return 0; } @@ -1652,9 +1656,9 @@ Coordinates::FieldMetric Coordinates::Div_par(const Field2D& f, CELL_LOC outloc, // Need Bxy at location of f, which might be different from location of this // Coordinates object - auto Bxy_floc = f.getCoordinates()->Bxy; + auto Bxy_floc = f.getCoordinates()->Bxy(); - return Bxy * Grad_par(f / Bxy_floc, outloc, method); + return this_Bxy * Grad_par(f / Bxy_floc, outloc, method); } Field3D Coordinates::Div_par(const Field3D& f, CELL_LOC outloc, @@ -1664,12 +1668,12 @@ Field3D Coordinates::Div_par(const Field3D& f, CELL_LOC outloc, // Need Bxy at location of f, which might be different from location of this // Coordinates object - auto Bxy_floc = f.getCoordinates()->Bxy; + auto Bxy_floc = f.getCoordinates()->Bxy(); if (!f.hasParallelSlices()) { // No yup/ydown fields. The Grad_par operator will // shift to field aligned coordinates - return Bxy * Grad_par(f / Bxy_floc, outloc, method); + return this_Bxy * Grad_par(f / Bxy_floc, outloc, method); } // Need to modify yup and ydown fields @@ -1679,7 +1683,7 @@ Field3D Coordinates::Div_par(const Field3D& f, CELL_LOC outloc, f_B.yup(i) = f.yup(i) / Bxy_floc.yup(i); f_B.ydown(i) = f.ydown(i) / Bxy_floc.ydown(i); } - return Bxy * Grad_par(f_B, outloc, method); + return this_Bxy * Grad_par(f_B, outloc, method); } ///////////////////////////////////////////////////////// @@ -1873,7 +1877,8 @@ Coordinates::FieldMetric Coordinates::Laplace_par(const Field2D& f, CELL_LOC out Field3D Coordinates::Laplace_par(const Field3D& f, CELL_LOC outloc) { ASSERT1(location == outloc || outloc == CELL_DEFAULT); return D2DY2(f, outloc) / covariantMetricTensor.Getg22() - + DDY(this_J / covariantMetricTensor.Getg22(), outloc) * ::DDY(f, outloc) / this_J; + + DDY(this_J / covariantMetricTensor.Getg22(), outloc) * ::DDY(f, outloc) + / this_J; } // Full Laplacian operator on scalar field @@ -1928,7 +1933,7 @@ Field2D Coordinates::Laplace_perpXY([[maybe_unused]] const Field2D& A, // outer x boundary const auto outer_x_avg = [&i](const auto& f) { return 0.5 * (f[i] + f[i.xp()]); }; const BoutReal outer_x_A = outer_x_avg(A); - const BoutReal outer_x_J = outer_x_avg(_J); + const BoutReal outer_x_J = outer_x_avg(this_J); const BoutReal outer_x_g11 = outer_x_avg(contravariantMetricTensor.Getg11()); const BoutReal outer_x_dx = outer_x_avg(dx); const BoutReal outer_x_value = @@ -2064,3 +2069,6 @@ const MetricTensor::FieldMetric& Coordinates::g23() const { const MetricTensor::FieldMetric Coordinates::J() const { return this_J; } +const MetricTensor::FieldMetric Coordinates::Bxy() const { + return this_Bxy; +} diff --git a/src/mesh/coordinates_accessor.cxx b/src/mesh/coordinates_accessor.cxx index eed64d0a88..9337b48636 100644 --- a/src/mesh/coordinates_accessor.cxx +++ b/src/mesh/coordinates_accessor.cxx @@ -54,10 +54,10 @@ CoordinatesAccessor::CoordinatesAccessor(const Coordinates* coords) { COPY_STRIPE(d1_dx, d1_dy, d1_dz); data[stripe_size * ind.ind + static_cast(Offset::J)] = coords->J()[ind]; - data[stripe_size * ind.ind + static_cast(Offset::B)] = coords->Bxy[ind]; - data[stripe_size * ind.ind + static_cast(Offset::Byup)] = coords->Bxy.yup()[ind]; + data[stripe_size * ind.ind + static_cast(Offset::B)] = coords->Bxy()[ind]; + data[stripe_size * ind.ind + static_cast(Offset::Byup)] = coords->Bxy().yup()[ind]; data[stripe_size * ind.ind + static_cast(Offset::Bydown)] = - coords->Bxy.ydown()[ind]; + coords->Bxy().ydown()[ind]; COPY_STRIPE(G1, G3); // COPY_STRIPE(g11, g12, g13, g22, g23, g33); diff --git a/src/mesh/difops.cxx b/src/mesh/difops.cxx index 950e1e6146..2616353249 100644 --- a/src/mesh/difops.cxx +++ b/src/mesh/difops.cxx @@ -282,10 +282,10 @@ Field3D Div_par_flux(const Field3D& v, const Field3D& f, CELL_LOC outloc, const std::string& method) { Coordinates* metric = f.getCoordinates(outloc); - auto Bxy_floc = f.getCoordinates()->Bxy; + auto Bxy_floc = f.getCoordinates()->Bxy(); if (!f.hasParallelSlices()) { - return metric->Bxy * FDDY(v, f / Bxy_floc, outloc, method) / sqrt(metric->g_22()); + return metric->Bxy() * FDDY(v, f / Bxy_floc, outloc, method) / sqrt(metric->g_22()); } // Need to modify yup and ydown fields @@ -294,7 +294,7 @@ Field3D Div_par_flux(const Field3D& v, const Field3D& f, CELL_LOC outloc, f_B.splitParallelSlices(); f_B.yup() = f.yup() / Bxy_floc; f_B.ydown() = f.ydown() / Bxy_floc; - return metric->Bxy * FDDY(v, f_B, outloc, method) / sqrt(metric->g_22()); + return metric->Bxy() * FDDY(v, f_B, outloc, method) / sqrt(metric->g_22()); } Field3D Div_par_flux(const Field3D& v, const Field3D& f, const std::string& method, @@ -630,7 +630,7 @@ Coordinates::FieldMetric bracket(const Field2D& f, const Field2D& g, result.setLocation(outloc); } else { // Use full expression with all terms - result = b0xGrad_dot_Grad(f, g, outloc) / f.getCoordinates(outloc)->Bxy; + result = b0xGrad_dot_Grad(f, g, outloc) / f.getCoordinates(outloc)->Bxy(); } return result; } @@ -818,7 +818,7 @@ Field3D bracket(const Field3D& f, const Field2D& g, BRACKET_METHOD method, } default: { // Use full expression with all terms - result = b0xGrad_dot_Grad(f, g, outloc) / metric->Bxy; + result = b0xGrad_dot_Grad(f, g, outloc) / metric->Bxy(); } } return result; @@ -854,7 +854,7 @@ Field3D bracket(const Field2D& f, const Field3D& g, BRACKET_METHOD method, default: { // Use full expression with all terms Coordinates* metric = f.getCoordinates(outloc); - result = b0xGrad_dot_Grad(f, g, outloc) / metric->Bxy; + result = b0xGrad_dot_Grad(f, g, outloc) / metric->Bxy(); } } @@ -1154,7 +1154,7 @@ Field3D bracket(const Field3D& f, const Field3D& g, BRACKET_METHOD method, } default: { // Use full expression with all terms - result = b0xGrad_dot_Grad(f, g, outloc) / metric->Bxy; + result = b0xGrad_dot_Grad(f, g, outloc) / metric->Bxy(); } } diff --git a/src/mesh/fv_ops.cxx b/src/mesh/fv_ops.cxx index 69b419b76f..f67ce7bf20 100644 --- a/src/mesh/fv_ops.cxx +++ b/src/mesh/fv_ops.cxx @@ -71,7 +71,7 @@ Field3D Div_a_Grad_perp(const Field3D& a, const Field3D& f) { // -- should insert communication here? if (!coord->g23().hasParallelSlices() || !coord->g_23().hasParallelSlices() || !coord->dy.hasParallelSlices() || !coord->dz.hasParallelSlices() - || !coord->Bxy.hasParallelSlices() || !coord->J().hasParallelSlices()) { + || !coord->Bxy().hasParallelSlices() || !coord->J().hasParallelSlices()) { throw BoutException("metrics have no yup/down: Maybe communicate in init?"); } } @@ -92,7 +92,7 @@ Field3D Div_a_Grad_perp(const Field3D& a, const Field3D& f) { const auto J = makeslices(metric_fci, coord->J()); const auto dy = makeslices(metric_fci, coord->dy); const auto dz = makeslices(metric_fci, coord->dz); - const auto Bxy = makeslices(metric_fci, coord->Bxy); + const auto Bxy = makeslices(metric_fci, coord->Bxy()); // Result of the Y and Z fluxes Field3D yzresult(0.0, mesh); diff --git a/tests/MMS/GBS/gbs.cxx b/tests/MMS/GBS/gbs.cxx index 6d90ded097..868e98947e 100644 --- a/tests/MMS/GBS/gbs.cxx +++ b/tests/MMS/GBS/gbs.cxx @@ -279,7 +279,7 @@ int GBS::init(bool restarting) { break; } case 3: { // logB, taken from mesh - logB = log(coords->Bxy); + logB = log(coords->Bxy()); break; } default: @@ -334,7 +334,7 @@ void GBS::LoadMetric(BoutReal Lnorm, BoutReal Bnorm) { Bpxy /= Bnorm; Btxy /= Bnorm; - coords->Bxy /= Bnorm; + coords->Bxy() /= Bnorm; // Calculate metric components bool ShiftXderivs; @@ -350,7 +350,7 @@ void GBS::LoadMetric(BoutReal Lnorm, BoutReal Bnorm) { const auto g11 = SQ(Rxy * Bpxy); const auto g22 = 1.0 / SQ(hthe); - const auto g33 = SQ(sinty) * coords->g11() + SQ(coords->Bxy) / coords->g11(); + const auto g33 = SQ(sinty) * coords->g11() + SQ(coords->Bxy()) / coords->g11(); const auto g12 = 0.0; const auto g13 = -sinty * coords->g11(); const auto g23 = -sbp * Btxy / (hthe * Bpxy * Rxy); @@ -360,7 +360,7 @@ void GBS::LoadMetric(BoutReal Lnorm, BoutReal Bnorm) { coords->J = hthe / Bpxy; const auto g_11 = 1.0 / coords->g11() + SQ(sinty * Rxy); - const auto g_22 = SQ(coords->Bxy * hthe / Bpxy); + const auto g_22 = SQ(coords->Bxy() * hthe / Bpxy); const auto g_33 = Rxy * Rxy; const auto g_12 = sbp * Btxy * hthe * sinty * Rxy / Bpxy; const auto g_13 = sinty * Rxy * Rxy; @@ -420,7 +420,7 @@ int GBS::rhs(BoutReal t) { Gi = 0.0; if (ionvis) { Field3D tau_i = Omega_ci * tau_i0 * pow(Ti, 1.5) / Ne; - Gi = -(0.96 * Ti * Ne * tau_i) * (2. * Grad_par(Vi) + C(phi) / coords->Bxy); + Gi = -(0.96 * Ti * Ne * tau_i) * (2. * Grad_par(Vi) + C(phi) / coords->Bxy()); mesh->communicate(Gi); Gi.applyBoundary("neumann"); } else { @@ -433,7 +433,7 @@ int GBS::rhs(BoutReal t) { Ge = 0.0; if (elecvis) { Ge = -(0.73 * Te * Ne * tau_e) - * (2. * Grad_par(Ve) + (5. * C(Te) + 5. * Te * C(logNe) + C(phi)) / coords->Bxy); + * (2. * Grad_par(Ve) + (5. * C(Te) + 5. * Te * C(logNe) + C(phi)) / coords->Bxy()); mesh->communicate(Ge); Ge.applyBoundary("neumann"); } else { @@ -448,7 +448,7 @@ int GBS::rhs(BoutReal t) { if (evolve_Ne) { // Density ddt(Ne) = -vE_Grad(Ne, phi) // ExB term - + (2. / coords->Bxy) * (C(Pe) - Ne * C(phi)) // Perpendicular compression + + (2. / coords->Bxy()) * (C(Pe) - Ne * C(phi)) // Perpendicular compression + D(Ne, Dn) + H(Ne, Hn); if (parallel) { @@ -465,7 +465,7 @@ int GBS::rhs(BoutReal t) { if (evolve_Te) { // Electron temperature ddt(Te) = -vE_Grad(Te, phi) - + (4. / 3.) * (Te / coords->Bxy) + + (4. / 3.) * (Te / coords->Bxy()) * ((7. / 2.) * C(Te) + (Te / Ne) * C(Ne) - C(phi)) + D(Te, Dte) + H(Te, Hte); @@ -489,14 +489,14 @@ int GBS::rhs(BoutReal t) { if (evolve_Vort) { // Vorticity ddt(Vort) = -vE_Grad(Vort, phi) // ExB term - + 2. * coords->Bxy * C(Pe) / Ne + coords->Bxy * C(Gi) / (3. * Ne) + + 2. * coords->Bxy() * C(Pe) / Ne + coords->Bxy() * C(Gi) / (3. * Ne) + D(Vort, Dvort) + H(Vort, Hvort); if (parallel) { Field3D delV = Vi - Ve; mesh->communicate(delV); ddt(Vort) -= Vpar_Grad_par(Vi, Vort); // Parallel advection - ddt(Vort) += SQ(coords->Bxy) * (Grad_par(delV) + (Vi - Ve) * Grad_par(logNe)); + ddt(Vort) += SQ(coords->Bxy()) * (Grad_par(delV) + (Vi - Ve) * Grad_par(logNe)); } } @@ -532,7 +532,7 @@ const Field3D GBS::C(const Field3D& f) { // Curvature operator mesh->communicate(g); return bxcv * Grad(g); } - return coords->Bxy * bracket(logB, f, BRACKET_ARAKAWA); + return coords->Bxy() * bracket(logB, f, BRACKET_ARAKAWA); } const Field3D GBS::D(const Field3D& f, BoutReal d) { // Diffusion operator diff --git a/tests/MMS/elm-pb/elm_pb.cxx b/tests/MMS/elm-pb/elm_pb.cxx index 0a5fa4be51..91a6c0847e 100644 --- a/tests/MMS/elm-pb/elm_pb.cxx +++ b/tests/MMS/elm-pb/elm_pb.cxx @@ -427,7 +427,7 @@ class ELMpb : public PhysicsModel { MetricTensor(g11, g22, g33, g12, g13, g23)); coords->J = hthe / Bpxy; - coords->Bxy = B0; + coords->Bxy() = B0; const auto g_11 = 1.0 / coords->g11() + (SQ(I * Rxy)); const auto g_22 = SQ(B0 * hthe / Bpxy); diff --git a/tests/MMS/tokamak/tokamak.cxx b/tests/MMS/tokamak/tokamak.cxx index a0c1ef2a82..b79e53c6b4 100644 --- a/tests/MMS/tokamak/tokamak.cxx +++ b/tests/MMS/tokamak/tokamak.cxx @@ -66,7 +66,7 @@ class TokamakMMS : public PhysicsModel { Bpxy /= Bnorm; Btxy /= Bnorm; - coords->Bxy /= Bnorm; + coords->Bxy() /= Bnorm; // Calculate metric components bool ShiftXderivs; @@ -82,7 +82,7 @@ class TokamakMMS : public PhysicsModel { const auto g11 = SQ(Rxy * Bpxy); const auto g22 = 1.0 / SQ(hthe); - const auto g33 = SQ(sinty) * coords->g11() + SQ(coords->Bxy) / coords->g11(); + const auto g33 = SQ(sinty) * coords->g11() + SQ(coords->Bxy()) / coords->g11(); const auto g12 = 0.0; const auto g13 = -sinty * coords->g11(); const auto g23 = -sbp * Btxy / (hthe * Bpxy * Rxy); @@ -92,7 +92,7 @@ class TokamakMMS : public PhysicsModel { coords->J = hthe / Bpxy; const auto g_11 = 1.0 / coords->g11() + SQ(sinty * Rxy); - const auto g_22 = SQ(coords->Bxy * hthe / Bpxy); + const auto g_22 = SQ(coords->Bxy() * hthe / Bpxy); const auto g_33 = Rxy * Rxy; const auto g_12 = sbp * Btxy * hthe * sinty * Rxy / Bpxy; const auto g_13 = sinty * Rxy * Rxy; diff --git a/tests/integrated/test-drift-instability/2fluid.cxx b/tests/integrated/test-drift-instability/2fluid.cxx index f4ec753e60..ce5247faf8 100644 --- a/tests/integrated/test-drift-instability/2fluid.cxx +++ b/tests/integrated/test-drift-instability/2fluid.cxx @@ -16,7 +16,7 @@ #include // just define a macro for V_E dot Grad -#define vE_Grad(f, p) (b0xGrad_dot_Grad(p, f) / coord->Bxy) +#define vE_Grad(f, p) (b0xGrad_dot_Grad(p, f) / coord->Bxy()) class TwoFluid : public PhysicsModel { // 2D initial profiles @@ -209,7 +209,7 @@ class TwoFluid : public PhysicsModel { // Normalise magnetic field Bpxy /= (bmag / 1.e4); Btxy /= (bmag / 1.e4); - coord->Bxy /= (bmag / 1.e4); + coord->Bxy() /= (bmag / 1.e4); // calculate pressures pei0 = (Ti0 + Te0) * Ni0; @@ -220,7 +220,7 @@ class TwoFluid : public PhysicsModel { MetricTensor::FieldMetric g11, g22, g33, g12, g13, g23; g11 = SQ(Rxy * Bpxy); g22 = 1.0 / SQ(hthe); - g33 = SQ(I) * coord->g11() + SQ(coord->Bxy) / coord->g11(); + g33 = SQ(I) * coord->g11() + SQ(coord->Bxy()) / coord->g11(); g12 = 0.0; g13 = -I * coord->g11(); g23 = -Btxy / (hthe * Bpxy * Rxy); @@ -230,7 +230,7 @@ class TwoFluid : public PhysicsModel { MetricTensor::FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; g_11 = 1.0 / coord->g11() + SQ(I * Rxy); - g_22 = SQ(coord->Bxy * hthe / Bpxy); + g_22 = SQ(coord->Bxy() * hthe / Bpxy); g_33 = Rxy * Rxy; g_12 = Btxy * hthe * I * Rxy / Bpxy; g_13 = I * Rxy * Rxy; @@ -415,7 +415,7 @@ class TwoFluid : public PhysicsModel { if (evolve_rho) { auto divPar_jpar_ylow = Div_par(jpar); mesh->communicate(divPar_jpar_ylow); - ddt(rho) += SQ(coord->Bxy) * interp_to(divPar_jpar_ylow, CELL_CENTRE); + ddt(rho) += SQ(coord->Bxy()) * interp_to(divPar_jpar_ylow, CELL_CENTRE); } // AJPAR diff --git a/tests/integrated/test-interchange-instability/2fluid.cxx b/tests/integrated/test-interchange-instability/2fluid.cxx index fc5311940d..b3414ae3a9 100644 --- a/tests/integrated/test-interchange-instability/2fluid.cxx +++ b/tests/integrated/test-interchange-instability/2fluid.cxx @@ -135,14 +135,14 @@ class Interchange : public PhysicsModel { // Normalise magnetic field Bpxy /= (bmag / 1.e4); Btxy /= (bmag / 1.e4); - coord->Bxy /= (bmag / 1.e4); + coord->Bxy() /= (bmag / 1.e4); /**************** CALCULATE METRICS ******************/ MetricTensor::FieldMetric g11, g22, g33, g12, g13, g23; g11 = SQ(Rxy * Bpxy); g22 = 1.0 / SQ(hthe); - g33 = SQ(I) * coord->g11() + SQ(coord->Bxy) / coord->g11(); + g33 = SQ(I) * coord->g11() + SQ(coord->Bxy()) / coord->g11(); g12 = 0.0; g13 = -I * coord->g11(); g23 = -Btxy / (hthe * Bpxy * Rxy); @@ -153,7 +153,7 @@ class Interchange : public PhysicsModel { MetricTensor::FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; g_11 = 1.0 / coord->g11() + SQ(I * Rxy); - g_22 = SQ(coord->Bxy * hthe / Bpxy); + g_22 = SQ(coord->Bxy() * hthe / Bpxy); g_33 = Rxy * Rxy; g_12 = Btxy * hthe * I * Rxy / Bpxy; g_13 = I * Rxy * Rxy; @@ -186,10 +186,10 @@ class Interchange : public PhysicsModel { Field3D pei = (Te0 + Ti0) * Ni; // DENSITY EQUATION - ddt(Ni) = -b0xGrad_dot_Grad(phi, Ni0) / coord->Bxy; + ddt(Ni) = -b0xGrad_dot_Grad(phi, Ni0) / coord->Bxy(); // VORTICITY - ddt(rho) = 2.0 * coord->Bxy * b0xcv * Grad(pei); + ddt(rho) = 2.0 * coord->Bxy() * b0xcv * Grad(pei); return (0); } diff --git a/tests/integrated/test-laplacexy/loadmetric.cxx b/tests/integrated/test-laplacexy/loadmetric.cxx index 52575eb9e9..6220ca1f52 100644 --- a/tests/integrated/test-laplacexy/loadmetric.cxx +++ b/tests/integrated/test-laplacexy/loadmetric.cxx @@ -33,7 +33,7 @@ void LoadMetric(BoutReal Lnorm, BoutReal Bnorm) { Bpxy /= Bnorm; Btxy /= Bnorm; - coords->Bxy /= Bnorm; + coords->Bxy() /= Bnorm; // Calculate metric components std::string ptstr; @@ -51,7 +51,7 @@ void LoadMetric(BoutReal Lnorm, BoutReal Bnorm) { const auto g11 = pow(Rxy * Bpxy, 2); const auto g22 = 1.0 / pow(hthe, 2); - const auto g33 = pow(sinty, 2) * coords->g11() + pow(coords->Bxy, 2) / coords->g11(); + const auto g33 = pow(sinty, 2) * coords->g11() + pow(coords->Bxy(), 2) / coords->g11(); const auto g12 = 0.0; const auto g13 = -sinty * coords->g11(); const auto g23 = -sbp * Btxy / (hthe * Bpxy * Rxy); @@ -61,7 +61,7 @@ void LoadMetric(BoutReal Lnorm, BoutReal Bnorm) { coords->J = hthe / Bpxy; const auto g_11 = 1.0 / coords->g11() + pow(sinty * Rxy, 2); - const auto g_22 = pow(coords->Bxy * hthe / Bpxy, 2); + const auto g_22 = pow(coords->Bxy() * hthe / Bpxy, 2); const auto g_33 = Rxy * Rxy; const auto g_12 = sbp * Btxy * hthe * sinty * Rxy / Bpxy; const auto g_13 = sinty * Rxy * Rxy; diff --git a/tests/unit/mesh/test_coordinates.cxx b/tests/unit/mesh/test_coordinates.cxx index c47f39e8eb..4d56cba5c5 100644 --- a/tests/unit/mesh/test_coordinates.cxx +++ b/tests/unit/mesh/test_coordinates.cxx @@ -111,7 +111,7 @@ TEST_F(CoordinatesTest, Jacobian) { EXPECT_NO_THROW(coords.jacobian()); EXPECT_TRUE(IsFieldEqual(coords.J(), 1.0)); - EXPECT_TRUE(IsFieldEqual(coords.Bxy, 1.0)); + EXPECT_TRUE(IsFieldEqual(coords.Bxy(), 1.0)); } /// To do generalise these tests @@ -206,7 +206,7 @@ TEST_F(CoordinatesTest, DefaultConstructor) { EXPECT_TRUE(IsFieldEqual(coords.g23(), 0.0)); EXPECT_TRUE(IsFieldEqual(coords.J(), 1.0)); - EXPECT_TRUE(IsFieldEqual(coords.Bxy, 1.0)); + EXPECT_TRUE(IsFieldEqual(coords.Bxy(), 1.0)); } TEST_F(CoordinatesTest, ConstructWithMeshSpacing) { @@ -232,7 +232,7 @@ TEST_F(CoordinatesTest, ConstructWithMeshSpacing) { EXPECT_TRUE(IsFieldEqual(coords.g23(), 0.0)); EXPECT_TRUE(IsFieldEqual(coords.J(), 1.0)); - EXPECT_TRUE(IsFieldEqual(coords.Bxy, 1.0)); + EXPECT_TRUE(IsFieldEqual(coords.Bxy(), 1.0)); } TEST_F(CoordinatesTest, SmallMeshSpacing) { @@ -279,7 +279,7 @@ TEST_F(CoordinatesTest, ConstructWithDiagonalContravariantMetric) { EXPECT_TRUE(IsFieldEqual(coords.g_33(), 1. / 42, "RGN_NOCORNERS")); EXPECT_TRUE(IsFieldEqual(coords.J(), 1. / sqrt(2.0 * 3.2 * 42), "RGN_NOCORNERS")); - EXPECT_TRUE(IsFieldEqual(coords.Bxy, sqrt(2.0 * 42), "RGN_NOCORNERS", 1e-10)); + EXPECT_TRUE(IsFieldEqual(coords.Bxy(), sqrt(2.0 * 42), "RGN_NOCORNERS", 1e-10)); } TEST_F(CoordinatesTest, NegativeJacobian) { diff --git a/tests/unit/test_extras.hxx b/tests/unit/test_extras.hxx index 6f78e99fd3..42db21cb2c 100644 --- a/tests/unit/test_extras.hxx +++ b/tests/unit/test_extras.hxx @@ -449,8 +449,8 @@ public: test_coords->d1_dx = test_coords->d1_dy = 0.2; test_coords->d1_dz = 0.0; #if BOUT_USE_METRIC_3D - test_coords->Bxy.splitParallelSlices(); - test_coords->Bxy.yup() = test_coords->Bxy.ydown() = test_coords->Bxy; + test_coords->Bxy().splitParallelSlices(); + test_coords->Bxy().yup() = test_coords->Bxy().ydown() = test_coords->Bxy(); #endif // No call to Coordinates::geometry() needed here From c9c9672a538b2dfcd9cb76ac1b9cd7e29c27d436 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Mon, 20 Nov 2023 22:19:10 +0000 Subject: [PATCH 138/491] Fix typo in covariant tensor components. --- src/mesh/coordinates.cxx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index ed01778332..6f4ac6adad 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -731,8 +731,8 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, g_22 = getAtLoc(mesh, "g_22", suffix, location); g_33 = getAtLoc(mesh, "g_33", suffix, location); g_12 = getAtLoc(mesh, "g_12", suffix, location); - g_22 = getAtLoc(mesh, "g_13", suffix, location); - g_33 = getAtLoc(mesh, "g_23", suffix, location); + g_13 = getAtLoc(mesh, "g_13", suffix, location); + g_23 = getAtLoc(mesh, "g_23", suffix, location); covariantMetricTensor.setMetricTensor( MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); From 930f9d257ccbb533e7abd30357ec09b8987b85ec Mon Sep 17 00:00:00 2001 From: tomc271 Date: Sun, 19 Nov 2023 12:44:58 +0000 Subject: [PATCH 139/491] Make getUnaligned a method. --- include/bout/coordinates.hxx | 2 ++ src/mesh/coordinates.cxx | 25 ++++++++++++++----------- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index 98e5963d2d..bcd7e7abc0 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -270,6 +270,8 @@ private: void checkContravariant(); void interpolateAndExtrapolateContravariantMetricTensor(const Coordinates* coords_in); + FieldMetric getUnaligned(const std::string& name, BoutReal default_value); + MetricTensor contravariantMetricTensor; MetricTensor covariantMetricTensor; diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 6f4ac6adad..6e05eb2507 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -455,18 +455,8 @@ Coordinates::Coordinates(Mesh* mesh, Options* options) dy = interpolateAndExtrapolate(dy, location, extrapolate_x, extrapolate_y, false, transform.get()); - auto getUnaligned = [this](const std::string& name, BoutReal default_value) { - auto field = localmesh->get(name, default_value, false); - if (field.getDirectionY() == YDirectionType::Aligned - and transform->canToFromFieldAligned()) { - return transform->fromFieldAligned(field); - } else { - field.setDirectionY(YDirectionType::Standard); - return field; - } - }; - auto getUnalignedAtLocation = [this, extrapolate_x, extrapolate_y, getUnaligned]( + auto getUnalignedAtLocation = [this, extrapolate_x, extrapolate_y]( const std::string& name, BoutReal default_value) { auto field = getUnaligned(name, default_value); return interpolateAndExtrapolate(field, location, extrapolate_x, extrapolate_y, false, @@ -628,6 +618,19 @@ Coordinates::Coordinates(Mesh* mesh, Options* options) } } +Coordinates::FieldMetric Coordinates::getUnaligned(const std::string& name, + BoutReal default_value) { + + auto field = localmesh->get(name, default_value, false); + if (field.getDirectionY() == YDirectionType::Aligned + and transform->canToFromFieldAligned()) { + return transform->fromFieldAligned(field); + } else { + field.setDirectionY(YDirectionType::Standard); + return field; + } +} + Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, const Coordinates* coords_in, bool force_interpolate_from_centre) : dx(1., mesh), dy(1., mesh), dz(1., mesh), d1_dx(mesh), d1_dy(mesh), d1_dz(mesh), From f632c0234518e4369c64e9e2c6f3c9309111f145 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Mon, 20 Nov 2023 23:12:34 +0000 Subject: [PATCH 140/491] Add method 'getAtLocOrUnaligned' to Coordinates class, to help unify duplicated code. --- include/bout/coordinates.hxx | 5 ++++ src/mesh/coordinates.cxx | 50 +++++++++++++++++++----------------- 2 files changed, 32 insertions(+), 23 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index bcd7e7abc0..32efbf4b5a 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -270,6 +270,11 @@ private: void checkContravariant(); void interpolateAndExtrapolateContravariantMetricTensor(const Coordinates* coords_in); + FieldMetric getAtLocOrUnaligned(Mesh* mesh, const std::string& name, + BoutReal default_value = 0., + const std::string& suffix = "", + CELL_LOC cell_location = CELL_CENTRE); + FieldMetric getUnaligned(const std::string& name, BoutReal default_value); MetricTensor contravariantMetricTensor; diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 6e05eb2507..9c04d87b7a 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -327,9 +327,7 @@ int getAtLoc(Mesh* mesh, Coordinates::FieldMetric& var, const std::string& name, const std::string& suffix, CELL_LOC location, BoutReal default_value = 0.) { checkStaggeredGet(mesh, name, suffix); - int result = mesh->get(var, name + suffix, default_value, false, location); - - return result; + return mesh->get(var, name + suffix, default_value, false, location); } auto getAtLoc(Mesh* mesh, const std::string& name, const std::string& suffix, @@ -455,10 +453,9 @@ Coordinates::Coordinates(Mesh* mesh, Options* options) dy = interpolateAndExtrapolate(dy, location, extrapolate_x, extrapolate_y, false, transform.get()); - auto getUnalignedAtLocation = [this, extrapolate_x, extrapolate_y]( const std::string& name, BoutReal default_value) { - auto field = getUnaligned(name, default_value); + auto field = getAtLocOrUnaligned(localmesh, name, default_value); return interpolateAndExtrapolate(field, location, extrapolate_x, extrapolate_y, false, transform.get()); }; @@ -493,12 +490,12 @@ Coordinates::Coordinates(Mesh* mesh, Options* options) source_has_component)) { FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; - g_11 = getUnaligned("g_11", 1.0); - g_22 = getUnaligned("g_22", 1.0); - g_33 = getUnaligned("g_33", 1.0); - g_12 = getUnaligned("g_12", 0.0); - g_13 = getUnaligned("g_13", 0.0); - g_23 = getUnaligned("g_23", 0.0); + g_11 = getAtLocOrUnaligned(localmesh, "g_11", 1.0); + g_22 = getAtLocOrUnaligned(localmesh, "g_22", 1.0); + g_33 = getAtLocOrUnaligned(localmesh, "g_33", 1.0); + g_12 = getAtLocOrUnaligned(localmesh, "g_12", 0.0); + g_13 = getAtLocOrUnaligned(localmesh, "g_13", 0.0); + g_23 = getAtLocOrUnaligned(localmesh, "g_23", 0.0); covariantMetricTensor.setMetricTensor( MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); @@ -618,6 +615,17 @@ Coordinates::Coordinates(Mesh* mesh, Options* options) } } +Coordinates::FieldMetric Coordinates::getAtLocOrUnaligned(Mesh* mesh, + const std::string& name, + BoutReal default_value, + const std::string& suffix, + CELL_LOC cell_location) { + if (suffix == "") { + return getUnaligned(name, default_value); + } + return getAtLoc(mesh, name, suffix, cell_location, default_value); +} + Coordinates::FieldMetric Coordinates::getUnaligned(const std::string& name, BoutReal default_value) { @@ -730,12 +738,12 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, source_has_component)) { FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; - g_11 = getAtLoc(mesh, "g_11", suffix, location); - g_22 = getAtLoc(mesh, "g_22", suffix, location); - g_33 = getAtLoc(mesh, "g_33", suffix, location); - g_12 = getAtLoc(mesh, "g_12", suffix, location); - g_13 = getAtLoc(mesh, "g_13", suffix, location); - g_23 = getAtLoc(mesh, "g_23", suffix, location); + g_11 = getAtLocOrUnaligned(mesh, "g_11", 0.0, suffix, location); + g_22 = getAtLocOrUnaligned(mesh, "g_22", 0.0, suffix, location); + g_33 = getAtLocOrUnaligned(mesh, "g_33", 0.0, suffix, location); + g_12 = getAtLocOrUnaligned(mesh, "g_12", 0.0, suffix, location); + g_13 = getAtLocOrUnaligned(mesh, "g_13", 0.0, suffix, location); + g_23 = getAtLocOrUnaligned(mesh, "g_23", 0.0, suffix, location); covariantMetricTensor.setMetricTensor( MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); @@ -2069,9 +2077,5 @@ const MetricTensor::FieldMetric& Coordinates::g13() const { const MetricTensor::FieldMetric& Coordinates::g23() const { return contravariantMetricTensor.Getg23(); } -const MetricTensor::FieldMetric Coordinates::J() const { - return this_J; -} -const MetricTensor::FieldMetric Coordinates::Bxy() const { - return this_Bxy; -} +const MetricTensor::FieldMetric Coordinates::J() const { return this_J; } +const MetricTensor::FieldMetric Coordinates::Bxy() const { return this_Bxy; } From 0246413827c8caf6362a9ee66342fe419363759f Mon Sep 17 00:00:00 2001 From: tomc271 Date: Mon, 20 Nov 2023 23:40:22 +0000 Subject: [PATCH 141/491] Replace getUnalignedAtLocation and getAtLocAndFillGuards with single method 'getUnalignedAtLocationAndFillGuards'. --- include/bout/coordinates.hxx | 6 ++ src/mesh/coordinates.cxx | 189 ++++++++++++++++++----------------- 2 files changed, 102 insertions(+), 93 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index 32efbf4b5a..38ddeca5dd 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -277,6 +277,12 @@ private: FieldMetric getUnaligned(const std::string& name, BoutReal default_value); + FieldMetric getUnalignedAtLocationAndFillGuards( + Mesh* mesh, const std::string& name, BoutReal default_value, + const std::string& suffix = "", CELL_LOC cell_location = CELL_CENTRE, + bool extrapolate_x = false, bool extrapolate_y = false, + bool no_extra_interpolate = false, ParallelTransform* pParallelTransform = nullptr); + MetricTensor contravariantMetricTensor; MetricTensor covariantMetricTensor; diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 9c04d87b7a..1b71e67cb2 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -337,17 +337,6 @@ auto getAtLoc(Mesh* mesh, const std::string& name, const std::string& suffix, return mesh->get(name + suffix, default_value, false, location); } -Coordinates::FieldMetric getAtLocAndFillGuards(Mesh* mesh, const std::string& name, - const std::string& suffix, - CELL_LOC location, BoutReal default_value, - bool extrapolate_x, bool extrapolate_y, - bool no_extra_interpolate, - ParallelTransform* pt) { - auto var = getAtLoc(mesh, name, suffix, location, default_value); - return interpolateAndExtrapolate(var, location, extrapolate_x, extrapolate_y, - no_extra_interpolate, pt); -} - std::string getLocationSuffix(CELL_LOC location) { switch (location) { case CELL_CENTRE: { @@ -373,6 +362,44 @@ std::string getLocationSuffix(CELL_LOC location) { } // anonymous namespace +Coordinates::FieldMetric Coordinates::getAtLocOrUnaligned(Mesh* mesh, + const std::string& name, + BoutReal default_value, + const std::string& suffix, + CELL_LOC cell_location) { + if (suffix == "") { + return getUnaligned(name, default_value); + } + return getAtLoc(mesh, name, suffix, cell_location, default_value); +} + +Coordinates::FieldMetric Coordinates::getUnaligned(const std::string& name, + BoutReal default_value) { + + auto field = localmesh->get(name, default_value, false); + if (field.getDirectionY() == YDirectionType::Aligned + and transform->canToFromFieldAligned()) { + return transform->fromFieldAligned(field); + } else { + field.setDirectionY(YDirectionType::Standard); + return field; + } +} + +Coordinates::FieldMetric Coordinates::getUnalignedAtLocationAndFillGuards( + Mesh* mesh, const std::string& name, BoutReal default_value, + const std::string& suffix, CELL_LOC location, bool extrapolate_x, bool extrapolate_y, + bool no_extra_interpolate, ParallelTransform* pParallelTransform) { + + auto field = getAtLocOrUnaligned(mesh, name, default_value, suffix, location); + if (suffix == "") { + no_extra_interpolate = false; + pParallelTransform = transform.get(); + } + return interpolateAndExtrapolate(field, location, extrapolate_x, extrapolate_y, + no_extra_interpolate, pParallelTransform); +} + Coordinates::Coordinates(Mesh* mesh, FieldMetric dx, FieldMetric dy, FieldMetric dz, FieldMetric J, FieldMetric Bxy, FieldMetric g11, FieldMetric g22, FieldMetric g33, FieldMetric g12, FieldMetric g13, @@ -453,24 +480,17 @@ Coordinates::Coordinates(Mesh* mesh, Options* options) dy = interpolateAndExtrapolate(dy, location, extrapolate_x, extrapolate_y, false, transform.get()); - auto getUnalignedAtLocation = [this, extrapolate_x, extrapolate_y]( - const std::string& name, BoutReal default_value) { - auto field = getAtLocOrUnaligned(localmesh, name, default_value); - return interpolateAndExtrapolate(field, location, extrapolate_x, extrapolate_y, false, - transform.get()); - }; - FieldMetric g11, g22, g33, g12, g13, g23; // Diagonal components of metric tensor g^{ij} (default to 1) - g11 = getUnalignedAtLocation("g11", 1.0); - g22 = getUnalignedAtLocation("g22", 1.0); - g33 = getUnalignedAtLocation("g33", 1.0); + g11 = getUnalignedAtLocationAndFillGuards(localmesh, "g11", 1.0); + g22 = getUnalignedAtLocationAndFillGuards(localmesh, "g22", 1.0); + g33 = getUnalignedAtLocationAndFillGuards(localmesh, "g33", 1.0); // Off-diagonal elements. Default to 0 - g12 = getUnalignedAtLocation("g12", 0.0); - g13 = getUnalignedAtLocation("g13", 0.0); - g23 = getUnalignedAtLocation("g23", 0.0); + g12 = getUnalignedAtLocationAndFillGuards(localmesh, "g12", 0.0); + g13 = getUnalignedAtLocationAndFillGuards(localmesh, "g13", 0.0); + g23 = getUnalignedAtLocationAndFillGuards(localmesh, "g23", 0.0); setContravariantMetricTensor(MetricTensor(g11, g22, g33, g12, g13, g23)); @@ -552,8 +572,8 @@ Coordinates::Coordinates(Mesh* mesh, Options* options) // Attempt to read J from the grid file auto Jcalc = this_J; if (mesh->get(this_J, "J", 0.0, false)) { - output_warn.write( - "\tWARNING: Jacobian 'J' not found. Calculating from metric tensor\n"); + output_warn.write("\tWARNING: Jacobian 'J' not found. Calculating from " + "metric tensor\n"); this_J = Jcalc; } else { this_J = interpolateAndExtrapolate(this_J, location, extrapolate_x, extrapolate_y, @@ -594,8 +614,8 @@ Coordinates::Coordinates(Mesh* mesh, Options* options) bout::checkPositive(this_Bxy, "Bxy", "RGN_NOCORNERS"); if (mesh->get(ShiftTorsion, "ShiftTorsion", 0.0, false)) { - output_warn.write( - "\tWARNING: No Torsion specified for zShift. Derivatives may not be correct\n"); + output_warn.write("\tWARNING: No Torsion specified for zShift. " + "Derivatives may not be correct\n"); ShiftTorsion = 0.0; } ShiftTorsion = interpolateAndExtrapolate(ShiftTorsion, location, extrapolate_x, @@ -615,30 +635,6 @@ Coordinates::Coordinates(Mesh* mesh, Options* options) } } -Coordinates::FieldMetric Coordinates::getAtLocOrUnaligned(Mesh* mesh, - const std::string& name, - BoutReal default_value, - const std::string& suffix, - CELL_LOC cell_location) { - if (suffix == "") { - return getUnaligned(name, default_value); - } - return getAtLoc(mesh, name, suffix, cell_location, default_value); -} - -Coordinates::FieldMetric Coordinates::getUnaligned(const std::string& name, - BoutReal default_value) { - - auto field = localmesh->get(name, default_value, false); - if (field.getDirectionY() == YDirectionType::Aligned - and transform->canToFromFieldAligned()) { - return transform->fromFieldAligned(field); - } else { - field.setDirectionY(YDirectionType::Standard); - return field; - } -} - Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, const Coordinates* coords_in, bool force_interpolate_from_centre) : dx(1., mesh), dy(1., mesh), dz(1., mesh), d1_dx(mesh), d1_dy(mesh), d1_dz(mesh), @@ -690,34 +686,40 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, dz = interpolateAndExtrapolate(dz, location, extrapolate_x, extrapolate_y, false, transform.get()); - dx = getAtLocAndFillGuards(mesh, "dx", suffix, location, 1.0, extrapolate_x, - extrapolate_y, false, transform.get()); + dx = getUnalignedAtLocationAndFillGuards(mesh, "dx", 1.0, suffix, location, + extrapolate_x, extrapolate_y, false, + transform.get()); if (mesh->periodicX) { communicate(dx); } - dy = getAtLocAndFillGuards(mesh, "dy", suffix, location, 1.0, extrapolate_x, - extrapolate_y, false, transform.get()); + dy = getUnalignedAtLocationAndFillGuards(mesh, "dy", 1.0, suffix, location, + extrapolate_x, extrapolate_y, false, + transform.get()); // grid data source has staggered fields, so read instead of interpolating // Diagonal components of metric tensor g^{ij} (default to 1) // TODO: Method `getAtLocAndFillGuards` violates command–query separation principle? - FieldMetric g11, g22, g33, g12, g13, g23; - - g11 = getAtLocAndFillGuards(mesh, "g11", suffix, location, 1.0, extrapolate_x, - extrapolate_y, false, transform.get()); - g22 = getAtLocAndFillGuards(mesh, "g22", suffix, location, 1.0, extrapolate_x, - extrapolate_y, false, transform.get()); - g33 = getAtLocAndFillGuards(mesh, "g33", suffix, location, 1.0, extrapolate_x, - extrapolate_y, false, transform.get()); - g12 = getAtLocAndFillGuards(mesh, "g12", suffix, location, 0.0, extrapolate_x, - extrapolate_y, false, transform.get()); - g13 = getAtLocAndFillGuards(mesh, "g13", suffix, location, 0.0, extrapolate_x, - extrapolate_y, false, transform.get()); - g23 = getAtLocAndFillGuards(mesh, "g23", suffix, location, 0.0, extrapolate_x, - extrapolate_y, false, transform.get()); + g11 = getUnalignedAtLocationAndFillGuards(mesh, "g11", 1.0, suffix, location, + extrapolate_x, extrapolate_y, false, + transform.get()); + g22 = getUnalignedAtLocationAndFillGuards(mesh, "g22", 1.0, suffix, location, + extrapolate_x, extrapolate_y, false, + transform.get()); + g33 = getUnalignedAtLocationAndFillGuards(mesh, "g33", 1.0, suffix, location, + extrapolate_x, extrapolate_y, false, + transform.get()); + g12 = getUnalignedAtLocationAndFillGuards(mesh, "g12", 0.0, suffix, location, + extrapolate_x, extrapolate_y, false, + transform.get()); + g13 = getUnalignedAtLocationAndFillGuards(mesh, "g13", 0.0, suffix, location, + extrapolate_x, extrapolate_y, false, + transform.get()); + g23 = getUnalignedAtLocationAndFillGuards(mesh, "g23", 0.0, suffix, location, + extrapolate_x, extrapolate_y, false, + transform.get()); setContravariantMetricTensor(MetricTensor(g11, g22, g33, g12, g13, g23)); @@ -747,9 +749,9 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, covariantMetricTensor.setMetricTensor( MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); - output_warn.write( - "\tWARNING! Staggered covariant components of metric tensor set manually. " - "Contravariant components NOT recalculated\n"); + output_warn.write("\tWARNING! Staggered covariant components of " + "metric tensor set manually. " + "Contravariant components NOT recalculated\n"); } else { output_warn.write( @@ -811,9 +813,9 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, // Attempt to read J from the grid file auto Jcalc = this_J; if (getAtLoc(mesh, this_J, "J", suffix, location)) { - output_warn.write( - "\tWARNING: Jacobian 'J_{:s}' not found. Calculating from metric tensor\n", - suffix); + output_warn.write("\tWARNING: Jacobian 'J_{:s}' not found. Calculating " + "from metric tensor\n", + suffix); this_J = Jcalc; } else { this_J = interpolateAndExtrapolate(this_J, location, extrapolate_x, extrapolate_y, @@ -855,8 +857,8 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, checkStaggeredGet(mesh, "ShiftTorsion", suffix); if (mesh->get(ShiftTorsion, "ShiftTorsion" + suffix, 0.0, false)) { - output_warn.write( - "\tWARNING: No Torsion specified for zShift. Derivatives may not be correct\n"); + output_warn.write("\tWARNING: No Torsion specified for zShift. " + "Derivatives may not be correct\n"); ShiftTorsion = 0.0; } ShiftTorsion.setLocation(location); @@ -886,9 +888,10 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, dz = coords_in->dz; dz.setLocation(location); } else { - throw BoutException( - "We are asked to transform dz to get dz before we have a transform, which " - "might require dz!\nPlease provide a dz for the staggered quantity!"); + throw BoutException("We are asked to transform dz to get dz before we " + "have a transform, which " + "might require dz!\nPlease provide a dz for the " + "staggered quantity!"); } setParallelTransform(options); dx = interpolateAndExtrapolate(coords_in->dx, location, true, true, false, @@ -1136,8 +1139,8 @@ int Coordinates::geometry(bool recalculate_staggered, bool extrapolate_y = not localmesh->sourceHasYBoundaryGuards(); if (localmesh->get(d2x, "d2x" + suffix, 0.0, false, location)) { - output_warn.write( - "\tWARNING: differencing quantity 'd2x' not found. Calculating from dx\n"); + output_warn.write("\tWARNING: differencing quantity 'd2x' not found. " + "Calculating from dx\n"); d1_dx = bout::derivatives::index::DDX(1. / dx); // d/di(1/dx) communicate(d1_dx); @@ -1153,8 +1156,8 @@ int Coordinates::geometry(bool recalculate_staggered, } if (localmesh->get(d2y, "d2y" + suffix, 0.0, false, location)) { - output_warn.write( - "\tWARNING: differencing quantity 'd2y' not found. Calculating from dy\n"); + output_warn.write("\tWARNING: differencing quantity 'd2y' not found. " + "Calculating from dy\n"); d1_dy = DDY(1. / dy); // d/di(1/dy) communicate(d1_dy); @@ -1171,8 +1174,8 @@ int Coordinates::geometry(bool recalculate_staggered, #if BOUT_USE_METRIC_3D if (localmesh->get(d2z, "d2z" + suffix, 0.0, false)) { - output_warn.write( - "\tWARNING: differencing quantity 'd2z' not found. Calculating from dz\n"); + output_warn.write("\tWARNING: differencing quantity 'd2z' not found. " + "Calculating from dz\n"); d1_dz = bout::derivatives::index::DDZ(1. / dz); communicate(d1_dz); d1_dz = @@ -1190,8 +1193,8 @@ int Coordinates::geometry(bool recalculate_staggered, #endif } else { if (localmesh->get(d2x, "d2x", 0.0, false)) { - output_warn.write( - "\tWARNING: differencing quantity 'd2x' not found. Calculating from dx\n"); + output_warn.write("\tWARNING: differencing quantity 'd2x' not found. " + "Calculating from dx\n"); d1_dx = bout::derivatives::index::DDX(1. / dx); // d/di(1/dx) communicate(d1_dx); @@ -1205,8 +1208,8 @@ int Coordinates::geometry(bool recalculate_staggered, } if (localmesh->get(d2y, "d2y", 0.0, false)) { - output_warn.write( - "\tWARNING: differencing quantity 'd2y' not found. Calculating from dy\n"); + output_warn.write("\tWARNING: differencing quantity 'd2y' not found. " + "Calculating from dy\n"); d1_dy = DDY(1. / dy); // d/di(1/dy) communicate(d1_dy); @@ -1221,8 +1224,8 @@ int Coordinates::geometry(bool recalculate_staggered, #if BOUT_USE_METRIC_3D if (localmesh->get(d2z, "d2z", 0.0, false)) { - output_warn.write( - "\tWARNING: differencing quantity 'd2z' not found. Calculating from dz\n"); + output_warn.write("\tWARNING: differencing quantity 'd2z' not found. " + "Calculating from dz\n"); d1_dz = bout::derivatives::index::DDZ(1. / dz); communicate(d1_dz); From 450eba03190d274dade09af73993179e24178b2f Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 22 Nov 2023 14:09:38 +0000 Subject: [PATCH 142/491] Bug fix: choose whether to call getUnaligned() or getAtLoc() based on if (cell_location == CELL_CENTRE), as cell_location will be deflt for FieldMetric objects not instantiated via their constructor. --- src/mesh/coordinates.cxx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 1b71e67cb2..afc3ccf4d9 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -367,7 +367,7 @@ Coordinates::FieldMetric Coordinates::getAtLocOrUnaligned(Mesh* mesh, BoutReal default_value, const std::string& suffix, CELL_LOC cell_location) { - if (suffix == "") { + if (cell_location == CELL_CENTRE) { return getUnaligned(name, default_value); } return getAtLoc(mesh, name, suffix, cell_location, default_value); From 57538ab1e318714343fce27bdc0486b4028064e5 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 22 Nov 2023 14:16:10 +0000 Subject: [PATCH 143/491] Invert 'if'. --- src/mesh/coordinates.cxx | 133 ++++++++++++++++++++------------------- 1 file changed, 67 insertions(+), 66 deletions(-) diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index afc3ccf4d9..c6589b79dd 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -656,8 +656,74 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, bool extrapolate_x = true; bool extrapolate_y = true; - if (!force_interpolate_from_centre && mesh->sourceHasVar("dx" + suffix)) { + if (force_interpolate_from_centre || !mesh->sourceHasVar("dx" + suffix)) + { + // Interpolate fields from coords_in + + if (isUniform(coords_in->dz)) { + dz = coords_in->dz; + dz.setLocation(location); + } else { + throw BoutException("We are asked to transform dz to get dz before we " + "have a transform, which " + "might require dz!\nPlease provide a dz for the " + "staggered quantity!"); + } + setParallelTransform(options); + dx = interpolateAndExtrapolate(coords_in->dx, location, true, true, false, + transform.get()); + dy = interpolateAndExtrapolate(coords_in->dy, location, true, true, false, + transform.get()); + // not really needed - we have used dz already ... + dz = interpolateAndExtrapolate(coords_in->dz, location, true, true, false, + transform.get()); + + interpolateAndExtrapolateContravariantMetricTensor(coords_in); + + FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; + // 3x3 matrix inversion can exaggerate small interpolation errors, so it is + // more robust to interpolate and extrapolate derived quantities directly, + // rather than deriving from interpolated/extrapolated covariant metric + // components + g_11 = interpolateAndExtrapolate(covariantMetricTensor.Getg11(), location, true, true, + false, transform.get()); + g_22 = interpolateAndExtrapolate(covariantMetricTensor.Getg22(), location, true, true, + false, transform.get()); + g_33 = interpolateAndExtrapolate(covariantMetricTensor.Getg33(), location, true, true, + false, transform.get()); + g_12 = interpolateAndExtrapolate(covariantMetricTensor.Getg12(), location, true, true, + false, transform.get()); + g_13 = interpolateAndExtrapolate(covariantMetricTensor.Getg13(), location, true, true, + false, transform.get()); + g_23 = interpolateAndExtrapolate(covariantMetricTensor.Getg23(), location, true, true, + false, transform.get()); + + covariantMetricTensor.setMetricTensor( + MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + + // Check input metrics + checkContravariant(); + checkCovariant(); + + this_J = interpolateAndExtrapolate(coords_in->J(), location, true, true, false, + transform.get()); + this_Bxy = interpolateAndExtrapolate(coords_in->Bxy(), location, true, true, false, + transform.get()); + + bout::checkFinite(this_J, "The Jacobian", "RGN_NOCORNERS"); + bout::checkPositive(this_J, "The Jacobian", "RGN_NOCORNERS"); + bout::checkFinite(this_Bxy, "Bxy", "RGN_NOCORNERS"); + bout::checkPositive(this_Bxy, "Bxy", "RGN_NOCORNERS"); + + ShiftTorsion = interpolateAndExtrapolate(coords_in->ShiftTorsion, location, true, + true, false, transform.get()); + + if (mesh->IncIntShear) { + IntShiftTorsion = interpolateAndExtrapolate(coords_in->IntShiftTorsion, location, + true, true, false, transform.get()); + } + } else { extrapolate_x = not mesh->sourceHasXBoundaryGuards(); extrapolate_y = not mesh->sourceHasYBoundaryGuards(); @@ -881,71 +947,6 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, // IntShiftTorsion will not be used, but set to zero to avoid uninitialized field IntShiftTorsion = 0.; } - } else { - // Interpolate fields from coords_in - - if (isUniform(coords_in->dz)) { - dz = coords_in->dz; - dz.setLocation(location); - } else { - throw BoutException("We are asked to transform dz to get dz before we " - "have a transform, which " - "might require dz!\nPlease provide a dz for the " - "staggered quantity!"); - } - setParallelTransform(options); - dx = interpolateAndExtrapolate(coords_in->dx, location, true, true, false, - transform.get()); - dy = interpolateAndExtrapolate(coords_in->dy, location, true, true, false, - transform.get()); - // not really needed - we have used dz already ... - dz = interpolateAndExtrapolate(coords_in->dz, location, true, true, false, - transform.get()); - - interpolateAndExtrapolateContravariantMetricTensor(coords_in); - - FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; - // 3x3 matrix inversion can exaggerate small interpolation errors, so it is - // more robust to interpolate and extrapolate derived quantities directly, - // rather than deriving from interpolated/extrapolated covariant metric - // components - g_11 = interpolateAndExtrapolate(covariantMetricTensor.Getg11(), location, true, true, - false, transform.get()); - g_22 = interpolateAndExtrapolate(covariantMetricTensor.Getg22(), location, true, true, - false, transform.get()); - g_33 = interpolateAndExtrapolate(covariantMetricTensor.Getg33(), location, true, true, - false, transform.get()); - g_12 = interpolateAndExtrapolate(covariantMetricTensor.Getg12(), location, true, true, - false, transform.get()); - g_13 = interpolateAndExtrapolate(covariantMetricTensor.Getg13(), location, true, true, - false, transform.get()); - g_23 = interpolateAndExtrapolate(covariantMetricTensor.Getg23(), location, true, true, - false, transform.get()); - - covariantMetricTensor.setMetricTensor( - MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); - - // Check input metrics - checkContravariant(); - checkCovariant(); - - this_J = interpolateAndExtrapolate(coords_in->J(), location, true, true, false, - transform.get()); - this_Bxy = interpolateAndExtrapolate(coords_in->Bxy(), location, true, true, false, - transform.get()); - - bout::checkFinite(this_J, "The Jacobian", "RGN_NOCORNERS"); - bout::checkPositive(this_J, "The Jacobian", "RGN_NOCORNERS"); - bout::checkFinite(this_Bxy, "Bxy", "RGN_NOCORNERS"); - bout::checkPositive(this_Bxy, "Bxy", "RGN_NOCORNERS"); - - ShiftTorsion = interpolateAndExtrapolate(coords_in->ShiftTorsion, location, true, - true, false, transform.get()); - - if (mesh->IncIntShear) { - IntShiftTorsion = interpolateAndExtrapolate(coords_in->IntShiftTorsion, location, - true, true, false, transform.get()); - } } } From ccd340b4f05a8f026443e53c402ab93b84700ff7 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 22 Nov 2023 14:52:47 +0000 Subject: [PATCH 144/491] DRY: Combine two constructors into one. --- include/bout/coordinates.hxx | 8 +- src/mesh/coordinates.cxx | 307 ++++++----------------------------- 2 files changed, 54 insertions(+), 261 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index 38ddeca5dd..ba793a5c99 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -55,17 +55,15 @@ public: using FieldMetric = Field2D; #endif - /// Standard constructor from input - Coordinates(Mesh* mesh, Options* options = nullptr); - /// Constructor interpolating from another Coordinates object /// By default attempts to read staggered Coordinates from grid data source, /// interpolating from CELL_CENTRE if not present. Set /// force_interpolate_from_centre argument to true to always interpolate /// (useful if CELL_CENTRE Coordinates have been changed, so reading from file /// would not be correct). - Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, - const Coordinates* coords_in, bool force_interpolate_from_centre = false); + Coordinates(Mesh* mesh, Options* options = nullptr, const CELL_LOC loc = CELL_CENTRE, + const Coordinates* coords_in = nullptr, + bool force_interpolate_from_centre = false); /// A constructor useful for testing purposes. To use it, inherit /// from Coordinates. If \p calculate_geometry is true (default), diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index c6589b79dd..1f7856c909 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -370,6 +370,8 @@ Coordinates::FieldMetric Coordinates::getAtLocOrUnaligned(Mesh* mesh, if (cell_location == CELL_CENTRE) { return getUnaligned(name, default_value); } + // grid data source has staggered fields, so read instead of interpolating + // Diagonal components of metric tensor g^{ij} (default to 1) return getAtLoc(mesh, name, suffix, cell_location, default_value); } @@ -413,7 +415,8 @@ Coordinates::Coordinates(Mesh* mesh, FieldMetric dx, FieldMetric dy, FieldMetric ShiftTorsion(std::move(ShiftTorsion)), IntShiftTorsion(std::move(IntShiftTorsion)), nz(mesh->LocalNz), localmesh(mesh), location(CELL_CENTRE) {} -Coordinates::Coordinates(Mesh* mesh, Options* options) +Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, + const Coordinates* coords_in, bool force_interpolate_from_centre) : dx(1., mesh), dy(1., mesh), dz(1., mesh), d1_dx(mesh), d1_dy(mesh), d1_dz(mesh), this_J(1., mesh), this_Bxy(1., mesh), contravariantMetricTensor(1., 1., 1., 0, 0, 0, mesh), @@ -423,231 +426,12 @@ Coordinates::Coordinates(Mesh* mesh, Options* options) G3_33(mesh), G3_12(mesh), G3_13(mesh), G3_23(mesh), G1(mesh), G2(mesh), G3(mesh), ShiftTorsion(mesh), IntShiftTorsion(mesh), // Identity metric tensor - localmesh(mesh), location(CELL_CENTRE) { + localmesh(mesh), location(loc) { if (options == nullptr) { options = Options::getRoot()->getSection("mesh"); } - // Note: If boundary cells were not loaded from the grid file, use - // 'interpolateAndExtrapolate' to set them. Ensures that derivatives are - // smooth at all the boundaries. - - const bool extrapolate_x = - (*options)["extrapolate_x"].withDefault(not mesh->sourceHasXBoundaryGuards()); - const bool extrapolate_y = - (*options)["extrapolate_y"].withDefault(not mesh->sourceHasYBoundaryGuards()); - - if (extrapolate_x) { - output_warn.write(_("WARNING: extrapolating input mesh quantities into x-boundary " - "cells. Set option extrapolate_x=false to disable this.\n")); - } - - if (extrapolate_y) { - output_warn.write(_("WARNING: extrapolating input mesh quantities into y-boundary " - "cells. Set option extrapolate_y=false to disable this.\n")); - } - - mesh->get(dx, "dx", 1.0, false); - mesh->get(dy, "dy", 1.0, false); - - nz = mesh->LocalNz; - - { - auto& options = Options::root(); - const bool has_zperiod = options.isSet("zperiod"); - const auto zmin = has_zperiod ? 0.0 : options["ZMIN"].withDefault(0.0); - const auto zmax = has_zperiod ? 1.0 / options["zperiod"].withDefault(1.0) - : options["ZMAX"].withDefault(1.0); - - const auto default_dz = (zmax - zmin) * TWOPI / nz; - - mesh->get(dz, "dz", default_dz, false); - } - - // required early for differentiation. - setParallelTransform(options); - - dz = interpolateAndExtrapolate(dz, location, extrapolate_x, extrapolate_y, false, - transform.get()); - dx = interpolateAndExtrapolate(dx, location, extrapolate_x, extrapolate_y, false, - transform.get()); - - if (mesh->periodicX) { - communicate(dx); - } - - dy = interpolateAndExtrapolate(dy, location, extrapolate_x, extrapolate_y, false, - transform.get()); - - FieldMetric g11, g22, g33, g12, g13, g23; - - // Diagonal components of metric tensor g^{ij} (default to 1) - g11 = getUnalignedAtLocationAndFillGuards(localmesh, "g11", 1.0); - g22 = getUnalignedAtLocationAndFillGuards(localmesh, "g22", 1.0); - g33 = getUnalignedAtLocationAndFillGuards(localmesh, "g33", 1.0); - - // Off-diagonal elements. Default to 0 - g12 = getUnalignedAtLocationAndFillGuards(localmesh, "g12", 0.0); - g13 = getUnalignedAtLocationAndFillGuards(localmesh, "g13", 0.0); - g23 = getUnalignedAtLocationAndFillGuards(localmesh, "g23", 0.0); - - setContravariantMetricTensor(MetricTensor(g11, g22, g33, g12, g13, g23)); - - // Check input metrics - checkContravariant(); - - /// Find covariant metric components - auto covariant_component_names = {"g_11", "g_22", "g_33", "g_12", "g_13", "g_23"}; - auto source_has_component = [&mesh](const std::string& name) { - return mesh->sourceHasVar(name); - }; - // Check if any of the components are present - if (std::any_of(begin(covariant_component_names), end(covariant_component_names), - source_has_component)) { - // Check that all components are present - if (std::all_of(begin(covariant_component_names), end(covariant_component_names), - source_has_component)) { - - FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; - g_11 = getAtLocOrUnaligned(localmesh, "g_11", 1.0); - g_22 = getAtLocOrUnaligned(localmesh, "g_22", 1.0); - g_33 = getAtLocOrUnaligned(localmesh, "g_33", 1.0); - g_12 = getAtLocOrUnaligned(localmesh, "g_12", 0.0); - g_13 = getAtLocOrUnaligned(localmesh, "g_13", 0.0); - g_23 = getAtLocOrUnaligned(localmesh, "g_23", 0.0); - - covariantMetricTensor.setMetricTensor( - MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); - - output_warn.write("\tWARNING! Covariant components of metric tensor set manually. " - "Contravariant components NOT recalculated\n"); - - } else { - output_warn.write("Not all covariant components of metric tensor found. " - "Calculating all from the contravariant tensor\n"); - /// Calculate contravariant metric components if not found - try { - calcCovariant("RGN_NOCORNERS"); - } catch (BoutException&) { - throw BoutException("Error in calcCovariant call"); - } - } - } else { - /// Calculate contravariant metric components if not found - try { - calcCovariant("RGN_NOCORNERS"); - } catch (BoutException&) { - throw BoutException("Error in calcCovariant call"); - } - } - - FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; - - // More robust to extrapolate derived quantities directly, rather than - // deriving from extrapolated covariant metric components - g_11 = interpolateAndExtrapolate(covariantMetricTensor.Getg11(), location, - extrapolate_x, extrapolate_y, false, transform.get()); - g_22 = interpolateAndExtrapolate(covariantMetricTensor.Getg22(), location, - extrapolate_x, extrapolate_y, false, transform.get()); - g_33 = interpolateAndExtrapolate(covariantMetricTensor.Getg33(), location, - extrapolate_x, extrapolate_y, false, transform.get()); - g_12 = interpolateAndExtrapolate(covariantMetricTensor.Getg12(), location, - extrapolate_x, extrapolate_y, false, transform.get()); - g_13 = interpolateAndExtrapolate(covariantMetricTensor.Getg13(), location, - extrapolate_x, extrapolate_y, false, transform.get()); - g_23 = interpolateAndExtrapolate(covariantMetricTensor.Getg23(), location, - extrapolate_x, extrapolate_y, false, transform.get()); - - covariantMetricTensor.setMetricTensor(MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); - - // Check covariant metrics - checkCovariant(); - - /// Calculate Jacobian and Bxy - if (jacobian()) { - throw BoutException("Error in jacobian call"); - } - - // Attempt to read J from the grid file - auto Jcalc = this_J; - if (mesh->get(this_J, "J", 0.0, false)) { - output_warn.write("\tWARNING: Jacobian 'J' not found. Calculating from " - "metric tensor\n"); - this_J = Jcalc; - } else { - this_J = interpolateAndExtrapolate(this_J, location, extrapolate_x, extrapolate_y, - false, transform.get()); - - // Compare calculated and loaded values - output_warn.write("\tMaximum difference in J is {:e}\n", max(abs(this_J - Jcalc))); - - communicate(this_J); - - // Re-evaluate Bxy using new J - this_Bxy = sqrt(g_22) / this_J; - } - - // Check jacobian - bout::checkFinite(this_J, "J", "RGN_NOCORNERS"); - bout::checkPositive(this_J, "J", "RGN_NOCORNERS"); - if (min(abs(this_J)) < 1.0e-10) { - throw BoutException("\tERROR: Jacobian becomes very small\n"); - } - - // Attempt to read Bxy from the grid file - auto Bcalc = this_Bxy; - if (mesh->get(this_Bxy, "Bxy", 0.0, false)) { - output_warn.write("\tWARNING: Magnitude of B field 'Bxy' not found. Calculating from " - "metric tensor\n"); - this_Bxy = Bcalc; - } else { - - this_Bxy = interpolateAndExtrapolate(this_Bxy, location, extrapolate_x, extrapolate_y, - false, transform.get()); - output_warn.write("\tMaximum difference in Bxy is {:e}\n", - max(abs(this_Bxy - Bcalc))); - } - - // Check Bxy - bout::checkFinite(this_Bxy, "Bxy", "RGN_NOCORNERS"); - bout::checkPositive(this_Bxy, "Bxy", "RGN_NOCORNERS"); - - if (mesh->get(ShiftTorsion, "ShiftTorsion", 0.0, false)) { - output_warn.write("\tWARNING: No Torsion specified for zShift. " - "Derivatives may not be correct\n"); - ShiftTorsion = 0.0; - } - ShiftTorsion = interpolateAndExtrapolate(ShiftTorsion, location, extrapolate_x, - extrapolate_y, false, transform.get()); - - ////////////////////////////////////////////////////// - - if (mesh->IncIntShear) { - if (mesh->get(IntShiftTorsion, "IntShiftTorsion", 0.0, false)) { - output_warn.write("\tWARNING: No Integrated torsion specified\n"); - } - IntShiftTorsion = interpolateAndExtrapolate(IntShiftTorsion, location, extrapolate_x, - extrapolate_y, false, transform.get()); - } else { - // IntShiftTorsion will not be used, but set to zero to avoid uninitialized field - IntShiftTorsion = 0.; - } -} - -Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, - const Coordinates* coords_in, bool force_interpolate_from_centre) - : dx(1., mesh), dy(1., mesh), dz(1., mesh), d1_dx(mesh), d1_dy(mesh), d1_dz(mesh), - this_J(1., mesh), this_Bxy(1., mesh), - contravariantMetricTensor(1., 1., 1., 0, 0, 0, mesh), - covariantMetricTensor(1., 1., 1., 0, 0, 0, mesh), G1_11(mesh), G1_22(mesh), - G1_33(mesh), G1_12(mesh), G1_13(mesh), G1_23(mesh), G2_11(mesh), G2_22(mesh), - G2_33(mesh), G2_12(mesh), G2_13(mesh), G2_23(mesh), G3_11(mesh), G3_22(mesh), - G3_33(mesh), G3_12(mesh), G3_13(mesh), G3_23(mesh), G1(mesh), G2(mesh), G3(mesh), - ShiftTorsion(mesh), IntShiftTorsion(mesh), - // Identity metric tensor - localmesh(mesh), location(loc) { - std::string suffix = getLocationSuffix(location); nz = mesh->LocalNz; @@ -724,19 +508,32 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, true, true, false, transform.get()); } } else { - extrapolate_x = not mesh->sourceHasXBoundaryGuards(); - extrapolate_y = not mesh->sourceHasYBoundaryGuards(); + // Note: If boundary cells were not loaded from the grid file, use + // 'interpolateAndExtrapolate' to set them. Ensures that derivatives are + // smooth at all the boundaries. + + const bool extrapolate_x = + (*options)["extrapolate_x"].withDefault(not mesh->sourceHasXBoundaryGuards()); + const bool extrapolate_y = + (*options)["extrapolate_y"].withDefault(not mesh->sourceHasYBoundaryGuards()); if (extrapolate_x) { output_warn.write(_("WARNING: extrapolating input mesh quantities into x-boundary " - "cells\n")); + "cells. Set option extrapolate_x=false to disable this.\n")); } if (extrapolate_y) { output_warn.write(_("WARNING: extrapolating input mesh quantities into y-boundary " - "cells\n")); + "cells. Set option extrapolate_y=false to disable this.\n")); } + if (coords_in == nullptr) { + mesh->get(dx, "dx", 1.0, false); + mesh->get(dy, "dy", 1.0, false); + } + + nz = mesh->LocalNz; + { auto& options = Options::root(); const bool has_zperiod = options.isSet("zperiod"); @@ -747,6 +544,8 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, const auto default_dz = (zmax - zmin) * TWOPI / nz; getAtLoc(mesh, dz, "dz", suffix, location, default_dz); } + + // required early for differentiation. setParallelTransform(options); dz = interpolateAndExtrapolate(dz, location, extrapolate_x, extrapolate_y, false, @@ -768,6 +567,8 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, // Diagonal components of metric tensor g^{ij} (default to 1) // TODO: Method `getAtLocAndFillGuards` violates command–query separation principle? FieldMetric g11, g22, g33, g12, g13, g23; + + // Diagonal components of metric tensor g^{ij} (default to 1) g11 = getUnalignedAtLocationAndFillGuards(mesh, "g11", 1.0, suffix, location, extrapolate_x, extrapolate_y, false, transform.get()); @@ -777,6 +578,7 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, g33 = getUnalignedAtLocationAndFillGuards(mesh, "g33", 1.0, suffix, location, extrapolate_x, extrapolate_y, false, transform.get()); + // Off-diagonal elements. Default to 0 g12 = getUnalignedAtLocationAndFillGuards(mesh, "g12", 0.0, suffix, location, extrapolate_x, extrapolate_y, false, transform.get()); @@ -806,39 +608,35 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, source_has_component)) { FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; - g_11 = getAtLocOrUnaligned(mesh, "g_11", 0.0, suffix, location); - g_22 = getAtLocOrUnaligned(mesh, "g_22", 0.0, suffix, location); - g_33 = getAtLocOrUnaligned(mesh, "g_33", 0.0, suffix, location); + g_11 = getAtLocOrUnaligned(mesh, "g_11", 1.0, suffix, location); + g_22 = getAtLocOrUnaligned(mesh, "g_22", 1.0, suffix, location); + g_33 = getAtLocOrUnaligned(mesh, "g_33", 1.0, suffix, location); g_12 = getAtLocOrUnaligned(mesh, "g_12", 0.0, suffix, location); g_13 = getAtLocOrUnaligned(mesh, "g_13", 0.0, suffix, location); g_23 = getAtLocOrUnaligned(mesh, "g_23", 0.0, suffix, location); covariantMetricTensor.setMetricTensor( MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); - output_warn.write("\tWARNING! Staggered covariant components of " - "metric tensor set manually. " - "Contravariant components NOT recalculated\n"); + output_warn.write( + "\tWARNING! Covariant components of metric tensor set manually. " + "Contravariant components NOT recalculated\n"); } else { - output_warn.write( - "Not all staggered covariant components of metric tensor found. " - "Calculating all from the contravariant tensor\n"); + output_warn.write("Not all covariant components of metric tensor found. " + "Calculating all from the contravariant tensor\n"); /// Calculate contravariant metric components if not found try { calcCovariant("RGN_NOCORNERS"); - } catch (BoutException& err) { - std::cout << err.what(); - throw BoutException("Error in staggered calcCovariant call/n" - + std::string(err.what())); + } catch (BoutException&) { + throw BoutException("Error in calcCovariant call"); } } } else { /// Calculate contravariant metric components if not found try { calcCovariant("RGN_NOCORNERS"); - } catch (BoutException& err) { - throw BoutException("Error in staggered calcCovariant call/n" - + std::string(err.what())); + } catch (BoutException&) { + throw BoutException("Error in calcCovariant call"); } } @@ -872,23 +670,24 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, /// Calculate Jacobian and Bxy if (jacobian()) { - throw BoutException("Error in jacobian call while constructing staggered " - "Coordinates"); + throw BoutException("Error in jacobian call"); } // Attempt to read J from the grid file auto Jcalc = this_J; if (getAtLoc(mesh, this_J, "J", suffix, location)) { - output_warn.write("\tWARNING: Jacobian 'J_{:s}' not found. Calculating " - "from metric tensor\n", - suffix); + output_warn.write( + "\tWARNING: Jacobian 'J_{:s}' not found. Calculating from metric tensor\n", + suffix); this_J = Jcalc; } else { this_J = interpolateAndExtrapolate(this_J, location, extrapolate_x, extrapolate_y, false, transform.get()); // Compare calculated and loaded values - output_warn.write("\tMaximum difference in J is %e\n", max(abs(this_J - Jcalc))); + output_warn.write("\tMaximum difference in J is {:e}\n", max(abs(this_J - Jcalc))); + + communicate(this_J); // Re-evaluate Bxy using new J this_Bxy = sqrt(g_22) / this_J; @@ -904,16 +703,14 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, // Attempt to read Bxy from the grid file auto Bcalc = this_Bxy; if (getAtLoc(mesh, this_Bxy, "Bxy", suffix, location)) { - output_warn.write( - "\tWARNING: Magnitude of B field 'Bxy_{:s}' not found. Calculating " - " from metric tensor\n", - suffix); + output_warn.write("\tWARNING: Magnitude of B field 'Bxy_{:s}' not found. " + "Calculating from metric tensor\n", + suffix); this_Bxy = Bcalc; } else { this_Bxy = interpolateAndExtrapolate(this_Bxy, location, extrapolate_x, extrapolate_y, false, transform.get()); - - output_warn.write("\tMaximum difference in Bxy is %e\n", + output_warn.write("\tMaximum difference in Bxy is {:e}\n", max(abs(this_Bxy - Bcalc))); } @@ -921,13 +718,11 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, bout::checkFinite(this_Bxy, "Bxy" + suffix, "RGN_NOCORNERS"); bout::checkPositive(this_Bxy, "Bxy" + suffix, "RGN_NOCORNERS"); - checkStaggeredGet(mesh, "ShiftTorsion", suffix); - if (mesh->get(ShiftTorsion, "ShiftTorsion" + suffix, 0.0, false)) { + if (getAtLoc(mesh, ShiftTorsion, "ShiftTorsion", suffix, location)) { output_warn.write("\tWARNING: No Torsion specified for zShift. " "Derivatives may not be correct\n"); ShiftTorsion = 0.0; } - ShiftTorsion.setLocation(location); ShiftTorsion = interpolateAndExtrapolate(ShiftTorsion, location, extrapolate_x, extrapolate_y, false, transform.get()); From f842684d9e1088b9c54a748148dfbaf159b3e682 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 22 Nov 2023 15:58:01 +0000 Subject: [PATCH 145/491] Bug fix: Check that coords_in isn't nullptr and suffix isn't empty string. --- src/mesh/coordinates.cxx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 1f7856c909..a8983d4df3 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -440,9 +440,9 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, bool extrapolate_x = true; bool extrapolate_y = true; - if (force_interpolate_from_centre || !mesh->sourceHasVar("dx" + suffix)) + if (coords_in && suffix != "" + && (force_interpolate_from_centre || !mesh->sourceHasVar("dx" + suffix))) { - { // Interpolate fields from coords_in if (isUniform(coords_in->dz)) { From ff02407a7624da4781efbc8833f888c8431300de Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 22 Nov 2023 16:14:53 +0000 Subject: [PATCH 146/491] Minor renaming. --- src/mesh/coordinates.cxx | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index a8983d4df3..f224b48c74 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -390,15 +390,16 @@ Coordinates::FieldMetric Coordinates::getUnaligned(const std::string& name, Coordinates::FieldMetric Coordinates::getUnalignedAtLocationAndFillGuards( Mesh* mesh, const std::string& name, BoutReal default_value, - const std::string& suffix, CELL_LOC location, bool extrapolate_x, bool extrapolate_y, - bool no_extra_interpolate, ParallelTransform* pParallelTransform) { + const std::string& suffix, CELL_LOC cell_location, bool extrapolate_x, + bool extrapolate_y, bool no_extra_interpolate, + ParallelTransform* pParallelTransform) { - auto field = getAtLocOrUnaligned(mesh, name, default_value, suffix, location); + auto field = getAtLocOrUnaligned(mesh, name, default_value, suffix, cell_location); if (suffix == "") { no_extra_interpolate = false; pParallelTransform = transform.get(); } - return interpolateAndExtrapolate(field, location, extrapolate_x, extrapolate_y, + return interpolateAndExtrapolate(field, cell_location, extrapolate_x, extrapolate_y, no_extra_interpolate, pParallelTransform); } From b40ff2dc1cc4765845f0e5baac5f0475afae67ad Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 22 Nov 2023 16:13:57 +0000 Subject: [PATCH 147/491] Reorder arguments in initializer list to match declaration order. --- src/mesh/coordinates.cxx | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index f224b48c74..a09e0c4d21 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -410,24 +410,24 @@ Coordinates::Coordinates(Mesh* mesh, FieldMetric dx, FieldMetric dy, FieldMetric FieldMetric g_33, FieldMetric g_12, FieldMetric g_13, FieldMetric g_23, FieldMetric ShiftTorsion, FieldMetric IntShiftTorsion) - : dx(std::move(dx)), dy(std::move(dy)), dz(dz), this_J(std::move(J)), - this_Bxy(std::move(Bxy)), contravariantMetricTensor(g11, g22, g33, g12, g13, g23), - covariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23), - ShiftTorsion(std::move(ShiftTorsion)), IntShiftTorsion(std::move(IntShiftTorsion)), - nz(mesh->LocalNz), localmesh(mesh), location(CELL_CENTRE) {} + : dx(std::move(dx)), dy(std::move(dy)), dz(dz), ShiftTorsion(std::move(ShiftTorsion)), + IntShiftTorsion(std::move(IntShiftTorsion)), nz(mesh->LocalNz), localmesh(mesh), + location(CELL_CENTRE), contravariantMetricTensor(g11, g22, g33, g12, g13, g23), + covariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23), this_J(std::move(J)), + this_Bxy(std::move(Bxy)) {} Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, const Coordinates* coords_in, bool force_interpolate_from_centre) : dx(1., mesh), dy(1., mesh), dz(1., mesh), d1_dx(mesh), d1_dy(mesh), d1_dz(mesh), - this_J(1., mesh), this_Bxy(1., mesh), + G1_11(mesh), G1_22(mesh), G1_33(mesh), G1_12(mesh), G1_13(mesh), G1_23(mesh), + G2_11(mesh), G2_22(mesh), G2_33(mesh), G2_12(mesh), G2_13(mesh), G2_23(mesh), + G3_11(mesh), G3_22(mesh), G3_33(mesh), G3_12(mesh), G3_13(mesh), G3_23(mesh), + G1(mesh), G2(mesh), G3(mesh), ShiftTorsion(mesh), IntShiftTorsion(mesh), + localmesh(mesh), location(loc), contravariantMetricTensor(1., 1., 1., 0, 0, 0, mesh), - covariantMetricTensor(1., 1., 1., 0, 0, 0, mesh), G1_11(mesh), G1_22(mesh), - G1_33(mesh), G1_12(mesh), G1_13(mesh), G1_23(mesh), G2_11(mesh), G2_22(mesh), - G2_33(mesh), G2_12(mesh), G2_13(mesh), G2_23(mesh), G3_11(mesh), G3_22(mesh), - G3_33(mesh), G3_12(mesh), G3_13(mesh), G3_23(mesh), G1(mesh), G2(mesh), G3(mesh), - ShiftTorsion(mesh), IntShiftTorsion(mesh), + covariantMetricTensor(1., 1., 1., 0, 0, 0, mesh), // Identity metric tensor - localmesh(mesh), location(loc) { + this_J(1., mesh), this_Bxy(1., mesh) { if (options == nullptr) { options = Options::getRoot()->getSection("mesh"); From 86bea77546535232e2fb95011d3dccc083f5e690 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 22 Nov 2023 16:11:43 +0000 Subject: [PATCH 148/491] Remove redundant semicolons. --- src/mesh/coordinates.cxx | 65 ++++++++++++++++++++-------------------- 1 file changed, 32 insertions(+), 33 deletions(-) diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index a09e0c4d21..8e4d781e7f 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -84,8 +84,8 @@ const Field2D interpolateAndExtrapolate(const Field2D& f, CELL_LOC location, // this point should already be set correctly because the metric // components have been interpolated to here. if (extrap_start > 0 and f.getLocation() != location) { - ASSERT1(bndry->bx == 0 or localmesh->xstart > 1); - ASSERT1(bndry->by == 0 or localmesh->ystart > 1); + ASSERT1(bndry->bx == 0 or localmesh->xstart > 1) + ASSERT1(bndry->by == 0 or localmesh->ystart > 1) // note that either bx or by is >0 here result(bndry->x, bndry->y) = (9. @@ -1365,7 +1365,7 @@ void Coordinates::setParallelTransform(Options* options) { Coordinates::FieldMetric Coordinates::DDX(const Field2D& f, CELL_LOC loc, const std::string& method, const std::string& region) { - ASSERT1(location == loc || loc == CELL_DEFAULT); + ASSERT1(location == loc || loc == CELL_DEFAULT) return bout::derivatives::index::DDX(f, loc, method, region) / dx; } Field3D Coordinates::DDX(const Field3D& f, CELL_LOC outloc, const std::string& method, @@ -1380,12 +1380,12 @@ Field3D Coordinates::DDX(const Field3D& f, CELL_LOC outloc, const std::string& m } return result; -}; +} Coordinates::FieldMetric Coordinates::DDY(const Field2D& f, CELL_LOC loc, const std::string& method, const std::string& region) const { - ASSERT1(location == loc || loc == CELL_DEFAULT); + ASSERT1(location == loc || loc == CELL_DEFAULT) return bout::derivatives::index::DDY(f, loc, method, region) / dy; } @@ -1400,13 +1400,13 @@ Field3D Coordinates::DDY(const Field3D& f, CELL_LOC outloc, const std::string& m } #endif return bout::derivatives::index::DDY(f, outloc, method, region) / dy; -}; +} Coordinates::FieldMetric Coordinates::DDZ(const Field2D& f, CELL_LOC loc, const std::string& UNUSED(method), const std::string& UNUSED(region)) { - ASSERT1(location == loc || loc == CELL_DEFAULT); - ASSERT1(f.getMesh() == localmesh); + ASSERT1(location == loc || loc == CELL_DEFAULT) + ASSERT1(f.getMesh() == localmesh) if (loc == CELL_DEFAULT) { loc = f.getLocation(); } @@ -1415,7 +1415,7 @@ Coordinates::FieldMetric Coordinates::DDZ(const Field2D& f, CELL_LOC loc, Field3D Coordinates::DDZ(const Field3D& f, CELL_LOC outloc, const std::string& method, const std::string& region) { return bout::derivatives::index::DDZ(f, outloc, method, region) / dz; -}; +} ///////////////////////////////////////////////////////// // Parallel gradient @@ -1424,8 +1424,7 @@ Coordinates::FieldMetric Coordinates::Grad_par(const Field2D& var, [[maybe_unused]] CELL_LOC outloc, const std::string& UNUSED(method)) { TRACE("Coordinates::Grad_par( Field2D )"); - ASSERT1(location == outloc - || (outloc == CELL_DEFAULT && location == var.getLocation())); + ASSERT1(location == outloc || (outloc == CELL_DEFAULT && location == var.getLocation())) return DDY(var) * invSg(); } @@ -1433,7 +1432,7 @@ Coordinates::FieldMetric Coordinates::Grad_par(const Field2D& var, Field3D Coordinates::Grad_par(const Field3D& var, CELL_LOC outloc, const std::string& method) { TRACE("Coordinates::Grad_par( Field3D )"); - ASSERT1(location == outloc || outloc == CELL_DEFAULT); + ASSERT1(location == outloc || outloc == CELL_DEFAULT) return ::DDY(var, outloc, method) * invSg(); } @@ -1445,14 +1444,14 @@ Field3D Coordinates::Grad_par(const Field3D& var, CELL_LOC outloc, Coordinates::FieldMetric Coordinates::Vpar_Grad_par(const Field2D& v, const Field2D& f, [[maybe_unused]] CELL_LOC outloc, const std::string& UNUSED(method)) { - ASSERT1(location == outloc || (outloc == CELL_DEFAULT && location == f.getLocation())); + ASSERT1(location == outloc || (outloc == CELL_DEFAULT && location == f.getLocation())) return VDDY(v, f) * invSg(); } Field3D Coordinates::Vpar_Grad_par(const Field3D& v, const Field3D& f, CELL_LOC outloc, const std::string& method) { - ASSERT1(location == outloc || outloc == CELL_DEFAULT); + ASSERT1(location == outloc || outloc == CELL_DEFAULT) return VDDY(v, f, outloc, method) * invSg(); } @@ -1463,7 +1462,7 @@ Field3D Coordinates::Vpar_Grad_par(const Field3D& v, const Field3D& f, CELL_LOC Coordinates::FieldMetric Coordinates::Div_par(const Field2D& f, CELL_LOC outloc, const std::string& method) { TRACE("Coordinates::Div_par( Field2D )"); - ASSERT1(location == outloc || outloc == CELL_DEFAULT); + ASSERT1(location == outloc || outloc == CELL_DEFAULT) // Need Bxy at location of f, which might be different from location of this // Coordinates object @@ -1475,7 +1474,7 @@ Coordinates::FieldMetric Coordinates::Div_par(const Field2D& f, CELL_LOC outloc, Field3D Coordinates::Div_par(const Field3D& f, CELL_LOC outloc, const std::string& method) { TRACE("Coordinates::Div_par( Field3D )"); - ASSERT1(location == outloc || outloc == CELL_DEFAULT); + ASSERT1(location == outloc || outloc == CELL_DEFAULT) // Need Bxy at location of f, which might be different from location of this // Coordinates object @@ -1504,7 +1503,7 @@ Field3D Coordinates::Div_par(const Field3D& f, CELL_LOC outloc, Coordinates::FieldMetric Coordinates::Grad2_par2(const Field2D& f, CELL_LOC outloc, const std::string& method) { TRACE("Coordinates::Grad2_par2( Field2D )"); - ASSERT1(location == outloc || (outloc == CELL_DEFAULT && location == f.getLocation())); + ASSERT1(location == outloc || (outloc == CELL_DEFAULT && location == f.getLocation())) auto result = Grad2_par2_DDY_invSg(outloc, method) * DDY(f, outloc, method) + D2DY2(f, outloc, method) / covariantMetricTensor.Getg22(); @@ -1518,7 +1517,7 @@ Field3D Coordinates::Grad2_par2(const Field3D& f, CELL_LOC outloc, if (outloc == CELL_DEFAULT) { outloc = f.getLocation(); } - ASSERT1(location == outloc); + ASSERT1(location == outloc) Field3D result = ::DDY(f, outloc, method); @@ -1526,7 +1525,7 @@ Field3D Coordinates::Grad2_par2(const Field3D& f, CELL_LOC outloc, result = Grad2_par2_DDY_invSg(outloc, method) * result + r2; - ASSERT2(result.getLocation() == outloc); + ASSERT2(result.getLocation() == outloc) return result; } @@ -1539,7 +1538,7 @@ Field3D Coordinates::Grad2_par2(const Field3D& f, CELL_LOC outloc, Coordinates::FieldMetric Coordinates::Delp2(const Field2D& f, CELL_LOC outloc, bool UNUSED(useFFT)) { TRACE("Coordinates::Delp2( Field2D )"); - ASSERT1(location == outloc || outloc == CELL_DEFAULT); + ASSERT1(location == outloc || outloc == CELL_DEFAULT) auto result = G1 * DDX(f, outloc) + contravariantMetricTensor.Getg11() * D2DX2(f, outloc); @@ -1554,14 +1553,14 @@ Field3D Coordinates::Delp2(const Field3D& f, CELL_LOC outloc, bool useFFT) { outloc = f.getLocation(); } - ASSERT1(location == outloc); - ASSERT1(f.getLocation() == outloc); + ASSERT1(location == outloc) + ASSERT1(f.getLocation() == outloc) if (localmesh->GlobalNx == 1 && localmesh->GlobalNz == 1) { // copy mesh, location, etc return f * 0; } - ASSERT2(localmesh->xstart > 0); // Need at least one guard cell + ASSERT2(localmesh->xstart > 0) // Need at least one guard cell Field3D result{emptyFrom(f).setLocation(outloc)}; @@ -1608,9 +1607,9 @@ Field3D Coordinates::Delp2(const Field3D& f, CELL_LOC outloc, bool useFFT) { + contravariantMetricTensor.Getg11() * ::D2DX2(f, outloc) + contravariantMetricTensor.Getg33() * ::D2DZ2(f, outloc) + 2 * contravariantMetricTensor.Getg13() * ::D2DXDZ(f, outloc); - }; + } - ASSERT2(result.getLocation() == outloc); + ASSERT2(result.getLocation() == outloc) return result; } @@ -1622,14 +1621,14 @@ FieldPerp Coordinates::Delp2(const FieldPerp& f, CELL_LOC outloc, bool useFFT) { outloc = f.getLocation(); } - ASSERT1(location == outloc); - ASSERT1(f.getLocation() == outloc); + ASSERT1(location == outloc) + ASSERT1(f.getLocation() == outloc) if (localmesh->GlobalNx == 1 && localmesh->GlobalNz == 1) { // copy mesh, location, etc return f * 0; } - ASSERT2(localmesh->xstart > 0); // Need at least one guard cell + ASSERT2(localmesh->xstart > 0) // Need at least one guard cell FieldPerp result{emptyFrom(f).setLocation(outloc)}; @@ -1673,20 +1672,20 @@ FieldPerp Coordinates::Delp2(const FieldPerp& f, CELL_LOC outloc, bool useFFT) { // yet // result = G1 * ::DDX(f, outloc) + G3 * ::DDZ(f, outloc) + g11 * ::D2DX2(f, outloc) // + g33 * ::D2DZ2(f, outloc) + 2 * g13 * ::D2DXDZ(f, outloc); - }; + } return result; } Coordinates::FieldMetric Coordinates::Laplace_par(const Field2D& f, CELL_LOC outloc) { - ASSERT1(location == outloc || outloc == CELL_DEFAULT); + ASSERT1(location == outloc || outloc == CELL_DEFAULT) return D2DY2(f, outloc) / covariantMetricTensor.Getg22() + DDY(this_J / covariantMetricTensor.Getg22(), outloc) * DDY(f, outloc) / this_J; } Field3D Coordinates::Laplace_par(const Field3D& f, CELL_LOC outloc) { - ASSERT1(location == outloc || outloc == CELL_DEFAULT); + ASSERT1(location == outloc || outloc == CELL_DEFAULT) return D2DY2(f, outloc) / covariantMetricTensor.Getg22() + DDY(this_J / covariantMetricTensor.Getg22(), outloc) * ::DDY(f, outloc) / this_J; @@ -1698,7 +1697,7 @@ Coordinates::FieldMetric Coordinates::Laplace(const Field2D& f, CELL_LOC outloc, const std::string& dfdy_boundary_conditions, const std::string& dfdy_dy_region) { TRACE("Coordinates::Laplace( Field2D )"); - ASSERT1(location == outloc || outloc == CELL_DEFAULT); + ASSERT1(location == outloc || outloc == CELL_DEFAULT) auto result = G1 * DDX(f, outloc) + G2 * DDY(f, outloc) + contravariantMetricTensor.Getg11() * D2DX2(f, outloc) @@ -1714,7 +1713,7 @@ Field3D Coordinates::Laplace(const Field3D& f, CELL_LOC outloc, const std::string& dfdy_boundary_conditions, const std::string& dfdy_dy_region) { TRACE("Coordinates::Laplace( Field3D )"); - ASSERT1(location == outloc || outloc == CELL_DEFAULT); + ASSERT1(location == outloc || outloc == CELL_DEFAULT) Field3D result = G1 * ::DDX(f, outloc) + G2 * ::DDY(f, outloc) + G3 * ::DDZ(f, outloc) + contravariantMetricTensor.Getg11() * D2DX2(f, outloc) From 251fb64e800d75dcc131138d9898df077307b204 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 22 Nov 2023 16:24:08 +0000 Subject: [PATCH 149/491] Make jacobian variable private in cython interface. --- tools/pylib/_boutpp_build/boutpp.pyx.jinja | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/pylib/_boutpp_build/boutpp.pyx.jinja b/tools/pylib/_boutpp_build/boutpp.pyx.jinja index de979087c1..6badc555d5 100644 --- a/tools/pylib/_boutpp_build/boutpp.pyx.jinja +++ b/tools/pylib/_boutpp_build/boutpp.pyx.jinja @@ -829,7 +829,7 @@ cdef class Coordinates: cdef c.Coordinates * cobj cdef c.bool isSelfOwned cdef public {{ metric_field }} dx, dy, dz - cdef public {{ metric_field }} J + cdef {{ metric_field }} J cdef public {{ metric_field }} Bxy cdef {{ metric_field }} g11, g22, g33, g12, g13, g23 cdef {{ metric_field }} g_11, g_22, g_33, g_12, g_13, g_23 @@ -845,7 +845,7 @@ cdef class Coordinates: self.isSelfOwned = False def _setmembers(self): -{% for f in "dx", "dy", "dz", "J", "Bxy", "G1_11", "G1_22", "G1_33", "G1_12", "G1_13", "G1_23", "G2_11", "G2_22", "G2_33", "G2_12", "G2_13", "G2_23", "G3_11", "G3_22", "G3_33", "G3_12", "G3_13", "G3_23", "G1", "G2", "G3", "ShiftTorsion", "IntShiftTorsion" %} +{% for f in "dx", "dy", "dz", "Bxy", "G1_11", "G1_22", "G1_33", "G1_12", "G1_13", "G1_23", "G2_11", "G2_22", "G2_33", "G2_12", "G2_13", "G2_23", "G3_11", "G3_22", "G3_33", "G3_12", "G3_13", "G3_23", "G1", "G2", "G3", "ShiftTorsion", "IntShiftTorsion" %} self.{{f}} = {{ metric_field.fdd }}FromPtr(&self.cobj.{{f}}) {% endfor %} From 9b9d0a89e3e3829b106e27c65f23903287db964c Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 22 Nov 2023 16:28:04 +0000 Subject: [PATCH 150/491] Make Bxy variable private in cython interface. --- tools/pylib/_boutpp_build/boutpp.pyx.jinja | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/pylib/_boutpp_build/boutpp.pyx.jinja b/tools/pylib/_boutpp_build/boutpp.pyx.jinja index 6badc555d5..f6e125bda7 100644 --- a/tools/pylib/_boutpp_build/boutpp.pyx.jinja +++ b/tools/pylib/_boutpp_build/boutpp.pyx.jinja @@ -830,7 +830,7 @@ cdef class Coordinates: cdef c.bool isSelfOwned cdef public {{ metric_field }} dx, dy, dz cdef {{ metric_field }} J - cdef public {{ metric_field }} Bxy + cdef {{ metric_field }} Bxy cdef {{ metric_field }} g11, g22, g33, g12, g13, g23 cdef {{ metric_field }} g_11, g_22, g_33, g_12, g_13, g_23 cdef public {{ metric_field }} G1_11, G1_22, G1_33, G1_12, G1_13, G1_23 @@ -845,7 +845,7 @@ cdef class Coordinates: self.isSelfOwned = False def _setmembers(self): -{% for f in "dx", "dy", "dz", "Bxy", "G1_11", "G1_22", "G1_33", "G1_12", "G1_13", "G1_23", "G2_11", "G2_22", "G2_33", "G2_12", "G2_13", "G2_23", "G3_11", "G3_22", "G3_33", "G3_12", "G3_13", "G3_23", "G1", "G2", "G3", "ShiftTorsion", "IntShiftTorsion" %} +{% for f in "dx", "dy", "dz", "G1_11", "G1_22", "G1_33", "G1_12", "G1_13", "G1_23", "G2_11", "G2_22", "G2_33", "G2_12", "G2_13", "G2_23", "G3_11", "G3_22", "G3_33", "G3_12", "G3_13", "G3_23", "G1", "G2", "G3", "ShiftTorsion", "IntShiftTorsion" %} self.{{f}} = {{ metric_field.fdd }}FromPtr(&self.cobj.{{f}}) {% endfor %} From 998965be66c95962ff22794e66894a2fb262573a Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 22 Nov 2023 17:43:07 +0000 Subject: [PATCH 151/491] Getters for J and Bxy to return references. --- include/bout/coordinates.hxx | 4 ++-- src/mesh/coordinates.cxx | 5 +++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index ba793a5c99..dd8129bcf7 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -108,10 +108,10 @@ public: const FieldMetric& g23() const; ///< Coordinate system Jacobian, so volume of cell is J*dx*dy*dz - const FieldMetric J() const; + const FieldMetric& J() const; ///< Magnitude of B = nabla z times nabla x - const FieldMetric Bxy() const; + const FieldMetric& Bxy() const; void setContravariantMetricTensor(MetricTensor metric_tensor, const std::string& region = "RGN_ALL"); diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 8e4d781e7f..d0763f983c 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -1876,5 +1876,6 @@ const MetricTensor::FieldMetric& Coordinates::g13() const { const MetricTensor::FieldMetric& Coordinates::g23() const { return contravariantMetricTensor.Getg23(); } -const MetricTensor::FieldMetric Coordinates::J() const { return this_J; } -const MetricTensor::FieldMetric Coordinates::Bxy() const { return this_Bxy; } +const MetricTensor::FieldMetric& Coordinates::J() const { return this_J; } + +const MetricTensor::FieldMetric& Coordinates::Bxy() const { return this_Bxy; } From 566ed062aa8ffcb280c5edcb288284bdf46a23ca Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 22 Nov 2023 17:45:48 +0000 Subject: [PATCH 152/491] Add setters for J and Bxy. --- examples/2Dturbulence_multigrid/esel.cxx | 2 +- examples/6field-simple/elm_6f.cxx | 4 +-- examples/conducting-wall-mode/cwm.cxx | 2 +- examples/constraints/alfven-wave/alfven.cxx | 2 +- examples/dalf3/dalf3.cxx | 4 +-- .../elm-pb-outerloop/elm_pb_outerloop.cxx | 4 +-- examples/elm-pb/elm_pb.cxx | 4 +-- examples/em-drift/2fluid.cxx | 2 +- examples/gyro-gem/gem.cxx | 4 +-- examples/jorek-compare/jorek_compare.cxx | 4 +-- examples/lapd-drift/lapd_drift.cxx | 2 +- examples/laplacexy/alfven-wave/alfven.cxx | 2 +- examples/laplacexy/laplace_perp/test.cxx | 4 +-- examples/reconnect-2field/2field.cxx | 2 +- examples/shear-alfven-wave/2fluid.cxx | 2 +- examples/tokamak-2fluid/2fluid.cxx | 2 +- examples/uedge-benchmark/ue_bmark.cxx | 34 +++++++++++-------- examples/wave-slab/wave_slab.cxx | 32 +++++++++-------- include/bout/coordinates.hxx | 4 +++ src/mesh/coordinates.cxx | 10 ++++++ tests/MMS/GBS/gbs.cxx | 2 +- tests/MMS/elm-pb/elm_pb.cxx | 4 +-- tests/MMS/tokamak/tokamak.cxx | 2 +- .../test-drift-instability/2fluid.cxx | 2 +- .../test-interchange-instability/2fluid.cxx | 2 +- .../integrated/test-laplacexy/loadmetric.cxx | 2 +- 26 files changed, 81 insertions(+), 59 deletions(-) diff --git a/examples/2Dturbulence_multigrid/esel.cxx b/examples/2Dturbulence_multigrid/esel.cxx index 916e0a7bb9..e6be58b34b 100644 --- a/examples/2Dturbulence_multigrid/esel.cxx +++ b/examples/2Dturbulence_multigrid/esel.cxx @@ -69,7 +69,7 @@ class ESEL : public PhysicsModel { Coordinates* coord = mesh->getCoordinates(); // generate coordinate system - coord->Bxy() = 1; + coord->setBxy(1); MetricTensor::FieldMetric g11, g22, g33, g12, g13, g23; g11 = 1.0; diff --git a/examples/6field-simple/elm_6f.cxx b/examples/6field-simple/elm_6f.cxx index 7036be72c1..66f3c0cb1a 100644 --- a/examples/6field-simple/elm_6f.cxx +++ b/examples/6field-simple/elm_6f.cxx @@ -1060,8 +1060,8 @@ class Elm_6f : public PhysicsModel { g23 = -Btxy / (hthe * Bpxy * Rxy); coord->setContravariantMetricTensor(MetricTensor(g11, g22, g33, g12, g13, g23)); - coord->J = hthe / Bpxy; - coord->Bxy() = B0; + coord->setJ(hthe / Bpxy); + coord->setBxy(B0); MetricTensor::FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; g_11 = 1.0 / coord->g11() + SQ(I * Rxy); diff --git a/examples/conducting-wall-mode/cwm.cxx b/examples/conducting-wall-mode/cwm.cxx index bbff227b6a..2418b8183d 100644 --- a/examples/conducting-wall-mode/cwm.cxx +++ b/examples/conducting-wall-mode/cwm.cxx @@ -197,7 +197,7 @@ class CWM : public PhysicsModel { g23 = -Btxy / (hthe * Bpxy * Rxy); coord->setContravariantMetricTensor(MetricTensor(g11, g22, g33, g12, g13, g23)); - coord->J = hthe / Bpxy; + coord->setJ(hthe / Bpxy); MetricTensor::FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; g_11 = 1.0 / coord->g11() + SQ(I * Rxy); diff --git a/examples/constraints/alfven-wave/alfven.cxx b/examples/constraints/alfven-wave/alfven.cxx index a753803d03..bd3b54952d 100644 --- a/examples/constraints/alfven-wave/alfven.cxx +++ b/examples/constraints/alfven-wave/alfven.cxx @@ -210,7 +210,7 @@ class Alfven : public PhysicsModel { g23 = -sbp * Btxy / (hthe * Bpxy * Rxy); coord->setContravariantMetricTensor(MetricTensor(g11, g22, g33, g12, g13, g23)); - coord->J = hthe / Bpxy; + coord->setJ(hthe / Bpxy); MetricTensor::FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; g_11 = 1.0 / coord->g11() + SQ(sinty * Rxy); diff --git a/examples/dalf3/dalf3.cxx b/examples/dalf3/dalf3.cxx index 823fd94d42..68ba3f7d6d 100644 --- a/examples/dalf3/dalf3.cxx +++ b/examples/dalf3/dalf3.cxx @@ -262,8 +262,8 @@ class DALF3 : public PhysicsModel { g23 = -Btxy / (hthe * Bpxy * Rxy); coord->setContravariantMetricTensor(MetricTensor(g11, g22, g33, g12, g13, g23)); - coord->J = hthe / Bpxy; - coord->Bxy() = B0; + coord->setJ(hthe / Bpxy); + coord->setBxy(B0); MetricTensor::FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; g_11 = 1.0 / coord->g11() + SQ(I * Rxy); diff --git a/examples/elm-pb-outerloop/elm_pb_outerloop.cxx b/examples/elm-pb-outerloop/elm_pb_outerloop.cxx index 65a1935e1f..e5cc1939b5 100644 --- a/examples/elm-pb-outerloop/elm_pb_outerloop.cxx +++ b/examples/elm-pb-outerloop/elm_pb_outerloop.cxx @@ -1099,8 +1099,8 @@ class ELMpb : public PhysicsModel { metric->g13 = -I * metric->g11; metric->g23 = -Btxy / (hthe * Bpxy * Rxy); - metric->J() = hthe / Bpxy; - metric->Bxy() = B0; + metric->setJ(hthe / Bpxy); + metric->setBxy(B0); metric->g_11 = 1.0 / metric->g11 + SQ(I * Rxy); metric->g_22 = SQ(B0 * hthe / Bpxy); diff --git a/examples/elm-pb/elm_pb.cxx b/examples/elm-pb/elm_pb.cxx index 4b934fb880..c07e669c22 100644 --- a/examples/elm-pb/elm_pb.cxx +++ b/examples/elm-pb/elm_pb.cxx @@ -1040,8 +1040,8 @@ class ELMpb : public PhysicsModel { metric->g13 = -I * metric->g11; metric->g23 = -Btxy / (hthe * Bpxy * Rxy); - metric->J() = hthe / Bpxy; - metric->Bxy() = B0; + metric->setJ(hthe / Bpxy); + metric->setBxy(B0); metric->g_11 = 1.0 / metric->g11 + SQ(I * Rxy); metric->g_22 = SQ(B0 * hthe / Bpxy); diff --git a/examples/em-drift/2fluid.cxx b/examples/em-drift/2fluid.cxx index bc78e5b58f..3ec54858cd 100644 --- a/examples/em-drift/2fluid.cxx +++ b/examples/em-drift/2fluid.cxx @@ -187,7 +187,7 @@ class EMdrift : public PhysicsModel { g23 = -Btxy / (hthe * Bpxy * Rxy); coord->setContravariantMetricTensor(MetricTensor(g11, g22, g33, g12, g13, g23)); - coord->J = hthe / Bpxy; + coord->setJ(hthe / Bpxy); MetricTensor::FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; g_11 = 1.0 / coord->g11() + SQ(I * Rxy); diff --git a/examples/gyro-gem/gem.cxx b/examples/gyro-gem/gem.cxx index 845040bd85..e0481a401e 100644 --- a/examples/gyro-gem/gem.cxx +++ b/examples/gyro-gem/gem.cxx @@ -378,8 +378,8 @@ class GEM : public PhysicsModel { g23 = -Btxy / (hthe * Bpxy * Rxy); coord->setContravariantMetricTensor(MetricTensor(g11, g22, g33, g12, g13, g23)); - coord->J = hthe / Bpxy; - coord->Bxy() = Bxy; + coord->setJ(hthe / Bpxy); + coord->setBxy(Bxy); MetricTensor::FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; g_11 = 1.0 / coord->g11(); diff --git a/examples/jorek-compare/jorek_compare.cxx b/examples/jorek-compare/jorek_compare.cxx index 322a52a0f6..aa6f7d17eb 100644 --- a/examples/jorek-compare/jorek_compare.cxx +++ b/examples/jorek-compare/jorek_compare.cxx @@ -308,8 +308,8 @@ class Jorek : public PhysicsModel { g23 = -Btxy / (hthe * Bpxy * Rxy); coord->setContravariantMetricTensor(MetricTensor(g11, g22, g33, g12, g13, g23)); - coord->J = hthe / Bpxy; - coord->Bxy() = B0; + coord->setJ(hthe / Bpxy); + coord->setBxy(B0); MetricTensor::FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; g_11 = 1.0 / coord->g11() + SQ(I * Rxy); diff --git a/examples/lapd-drift/lapd_drift.cxx b/examples/lapd-drift/lapd_drift.cxx index 0d9cd518b9..aa8e9284df 100644 --- a/examples/lapd-drift/lapd_drift.cxx +++ b/examples/lapd-drift/lapd_drift.cxx @@ -341,7 +341,7 @@ class LAPDdrift : public PhysicsModel { g23 = -Btxy / (hthe * Bpxy * Rxy); coord->setContravariantMetricTensor(MetricTensor(g11, g22, g33, g12, g13, g23)); - coord->J = hthe / Bpxy; + coord->setJ(hthe / Bpxy); MetricTensor::FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; g_11 = 1.0 / coord->g11() + SQ(I * Rxy); diff --git a/examples/laplacexy/alfven-wave/alfven.cxx b/examples/laplacexy/alfven-wave/alfven.cxx index 3d78a0e03e..3c95f62e97 100644 --- a/examples/laplacexy/alfven-wave/alfven.cxx +++ b/examples/laplacexy/alfven-wave/alfven.cxx @@ -211,7 +211,7 @@ class Alfven : public PhysicsModel { g23 = -sbp * Btxy / (hthe * Bpxy * Rxy); coord->setContravariantMetricTensor(MetricTensor(g11, g22, g33, g12, g13, g23)); - coord->J = hthe / Bpxy; + coord->setJ(hthe / Bpxy); MetricTensor::FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; g_11 = 1.0 / coord->g11() + SQ(sinty * Rxy); diff --git a/examples/laplacexy/laplace_perp/test.cxx b/examples/laplacexy/laplace_perp/test.cxx index 8198db1bd1..8fd5c71c39 100644 --- a/examples/laplacexy/laplace_perp/test.cxx +++ b/examples/laplacexy/laplace_perp/test.cxx @@ -34,8 +34,8 @@ int main(int argc, char** argv) { g23 = -Btxy / (hthe * Bpxy * Rxy); coord->setContravariantMetricTensor(MetricTensor(g11, g22, g33, g12, g13, g23)); - coord->J = hthe / Bpxy; - coord->Bxy() = B0; + coord->setJ(hthe / Bpxy); + coord->setBxy(B0); MetricTensor::FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; g_11 = 1.0 / coord->g11() + SQ(I * Rxy); diff --git a/examples/reconnect-2field/2field.cxx b/examples/reconnect-2field/2field.cxx index 9f7176f941..2d1c499447 100644 --- a/examples/reconnect-2field/2field.cxx +++ b/examples/reconnect-2field/2field.cxx @@ -172,7 +172,7 @@ class TwoField : public PhysicsModel { g23 = -Btxy / (hthe * Bpxy * Rxy); coord->setContravariantMetricTensor(MetricTensor(g11, g22, g33, g12, g13, g23)); - coord->J = hthe / Bpxy; + coord->setJ(hthe / Bpxy); MetricTensor::FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; g_11 = 1.0 / coord->g11(); diff --git a/examples/shear-alfven-wave/2fluid.cxx b/examples/shear-alfven-wave/2fluid.cxx index 869f62a6e9..1506ff7af4 100644 --- a/examples/shear-alfven-wave/2fluid.cxx +++ b/examples/shear-alfven-wave/2fluid.cxx @@ -191,7 +191,7 @@ class ShearAlfven : public PhysicsModel { g23 = -Btxy / (hthe * Bpxy * Rxy); coord->setContravariantMetricTensor(MetricTensor(g11, g22, g33, g12, g13, g23)); - coord->J = hthe / Bpxy; + coord->setJ(hthe / Bpxy); MetricTensor::FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; g_11 = 1.0 / coord->g11() + SQ(I * Rxy); diff --git a/examples/tokamak-2fluid/2fluid.cxx b/examples/tokamak-2fluid/2fluid.cxx index e30da633d8..8f113980c4 100644 --- a/examples/tokamak-2fluid/2fluid.cxx +++ b/examples/tokamak-2fluid/2fluid.cxx @@ -408,7 +408,7 @@ class TwoFluid : public PhysicsModel { g23 = -Btxy / (hthe * Bpxy * Rxy); coord->setContravariantMetricTensor(MetricTensor(g11, g22, g33, g12, g13, g23)); - coord->J = hthe / Bpxy; + coord->setJ(hthe / Bpxy); MetricTensor::FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; g_11 = 1.0 / coord->g11() + SQ(I * Rxy); diff --git a/examples/uedge-benchmark/ue_bmark.cxx b/examples/uedge-benchmark/ue_bmark.cxx index 94c43bd7de..32ee703385 100644 --- a/examples/uedge-benchmark/ue_bmark.cxx +++ b/examples/uedge-benchmark/ue_bmark.cxx @@ -140,21 +140,25 @@ class UedgeBenchmark : public PhysicsModel { /////////////// CALCULATE METRICS ///////////////// - coords->g11 = pow(Rxy * Bpxy, 2.0); - coords->g22 = 1.0 / pow(hthe, 2.0); - coords->g33 = pow(coords->Bxy(), 2.0) / coords->g11; - coords->g12 = 0.0; - coords->g13 = 0.0; - coords->g23 = -Btxy / (hthe * Bpxy * Rxy); - - coords->J = hthe / Bpxy; - - coords->g_11 = 1.0 / coords->g11; - coords->g_22 = pow(coords->Bxy() * hthe / Bpxy, 2.0); - coords->g_33 = Rxy * Rxy; - coords->g_12 = 0.0; - coords->g_13 = 0.0; - coords->g_23 = Btxy * hthe * Rxy / Bpxy; + MetricTensor::FieldMetric g11, g22, g33, g12, g13, g23; + g11 = pow(Rxy * Bpxy, 2.0); + g22 = 1.0 / pow(hthe, 2.0); + g33 = pow(coords->Bxy(), 2.0) / coords->g11(); + g12 = 0.0; + g13 = 0.0; + g23 = -Btxy / (hthe * Bpxy * Rxy); + coords->setContravariantMetricTensor(MetricTensor(g11, g22, g33, g12, g13, g23)); + + coords->setJ(hthe / Bpxy); + + MetricTensor::FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; + g_11 = 1.0 / coords->g11(); + g_22 = pow(coords->Bxy() * hthe / Bpxy, 2.0); + g_33 = Rxy * Rxy; + g_12 = 0.0; + g_13 = 0.0; + g_23 = Btxy * hthe * Rxy / Bpxy; + coords->setCovariantMetricTensor(MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); coords->geometry(); // Calculate other metrics diff --git a/examples/wave-slab/wave_slab.cxx b/examples/wave-slab/wave_slab.cxx index 17f24738cc..9893e88b20 100644 --- a/examples/wave-slab/wave_slab.cxx +++ b/examples/wave-slab/wave_slab.cxx @@ -21,7 +21,7 @@ class WaveTest : public PhysicsModel { GRID_LOAD(Bpxy); GRID_LOAD(Btxy); GRID_LOAD(hthe); - mesh->get(coords->Bxy(), "Bxy"); + coords->setBxy(mesh->get("Bxy")); int ShiftXderivs = 0; mesh->get(ShiftXderivs, "false"); if (ShiftXderivs) { @@ -31,21 +31,25 @@ class WaveTest : public PhysicsModel { mesh->get(I, "sinty"); } - coords->g11 = pow(Rxy * Bpxy, 2.0); - coords->g22 = 1.0 / pow(hthe, 2.0); - coords->g33 = pow(I, 2.0) * coords->g11 + pow(coords->Bxy(), 2.0) / coords->g11; - coords->g12 = 0.0; - coords->g13 = -I * coords->g11; - coords->g23 = -Btxy / (hthe * Bpxy * Rxy); + MetricTensor::FieldMetric g11, g22, g33, g12, g13, g23; + g11 = pow(Rxy * Bpxy, 2.0); + g22 = 1.0 / pow(hthe, 2.0); + g33 = pow(I, 2.0) * coords->g11() + pow(coords->Bxy(), 2.0) / coords->g11(); + g12 = 0.0; + g13 = -I * coords->g11(); + g23 = -Btxy / (hthe * Bpxy * Rxy); + coords->setContravariantMetricTensor(MetricTensor(g11, g22, g33, g12, g13, g23)); - coords->J = hthe / Bpxy; + coords->setJ(hthe / Bpxy); - coords->g_11 = 1.0 / coords->g11 + (pow(I * Rxy, 2.0)); - coords->g_22 = pow(coords->Bxy() * hthe / Bpxy, 2.0); - coords->g_33 = Rxy * Rxy; - coords->g_12 = Btxy * hthe * I * Rxy / Bpxy; - coords->g_13 = I * Rxy * Rxy; - coords->g_23 = Btxy * hthe * Rxy / Bpxy; + MetricTensor::FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; + g_11 = 1.0 / coords->g11() + (pow(I * Rxy, 2.0)); + g_22 = pow(coords->Bxy() * hthe / Bpxy, 2.0); + g_33 = Rxy * Rxy; + g_12 = Btxy * hthe * I * Rxy / Bpxy; + g_13 = I * Rxy * Rxy; + g_23 = Btxy * hthe * Rxy / Bpxy; + coords->setCovariantMetricTensor(MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); coords->geometry(); diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index dd8129bcf7..97b39229b0 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -119,6 +119,10 @@ public: void setCovariantMetricTensor(MetricTensor metric_tensor, const std::string& region = "RGN_ALL"); + void setJ(FieldMetric J); + + void setBxy(FieldMetric Bxy); + /// Christoffel symbol of the second kind (connection coefficients) FieldMetric G1_11, G1_22, G1_33, G1_12, G1_13, G1_23; FieldMetric G2_11, G2_22, G2_33, G2_12, G2_13, G2_23; diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index d0763f983c..be51aa0385 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -1879,3 +1879,13 @@ const MetricTensor::FieldMetric& Coordinates::g23() const { const MetricTensor::FieldMetric& Coordinates::J() const { return this_J; } const MetricTensor::FieldMetric& Coordinates::Bxy() const { return this_Bxy; } + +void Coordinates::setJ(FieldMetric J) { + //TODO: Calculate J and check value is close + this_J = J; +} + +void Coordinates::setBxy(FieldMetric Bxy) { + //TODO: Calculate Bxy and check value is close + this_Bxy = Bxy; +} diff --git a/tests/MMS/GBS/gbs.cxx b/tests/MMS/GBS/gbs.cxx index 868e98947e..a296094af6 100644 --- a/tests/MMS/GBS/gbs.cxx +++ b/tests/MMS/GBS/gbs.cxx @@ -357,7 +357,7 @@ void GBS::LoadMetric(BoutReal Lnorm, BoutReal Bnorm) { coords->setContravariantMetricTensor( MetricTensor(g11, g22, g33, g12, g13, g23)); - coords->J = hthe / Bpxy; + coords->setJ(hthe / Bpxy); const auto g_11 = 1.0 / coords->g11() + SQ(sinty * Rxy); const auto g_22 = SQ(coords->Bxy() * hthe / Bpxy); diff --git a/tests/MMS/elm-pb/elm_pb.cxx b/tests/MMS/elm-pb/elm_pb.cxx index 91a6c0847e..52c5cade55 100644 --- a/tests/MMS/elm-pb/elm_pb.cxx +++ b/tests/MMS/elm-pb/elm_pb.cxx @@ -426,8 +426,8 @@ class ELMpb : public PhysicsModel { coords->setContravariantMetricTensor( MetricTensor(g11, g22, g33, g12, g13, g23)); - coords->J = hthe / Bpxy; - coords->Bxy() = B0; + coords->setJ(hthe / Bpxy); + coords->setBxy(B0); const auto g_11 = 1.0 / coords->g11() + (SQ(I * Rxy)); const auto g_22 = SQ(B0 * hthe / Bpxy); diff --git a/tests/MMS/tokamak/tokamak.cxx b/tests/MMS/tokamak/tokamak.cxx index b79e53c6b4..2ebedfb0fd 100644 --- a/tests/MMS/tokamak/tokamak.cxx +++ b/tests/MMS/tokamak/tokamak.cxx @@ -89,7 +89,7 @@ class TokamakMMS : public PhysicsModel { coords->setContravariantMetricTensor( MetricTensor(g11, g22, g33, g12, g13, g23)); - coords->J = hthe / Bpxy; + coords->setJ(hthe / Bpxy); const auto g_11 = 1.0 / coords->g11() + SQ(sinty * Rxy); const auto g_22 = SQ(coords->Bxy() * hthe / Bpxy); diff --git a/tests/integrated/test-drift-instability/2fluid.cxx b/tests/integrated/test-drift-instability/2fluid.cxx index ce5247faf8..687f55f37a 100644 --- a/tests/integrated/test-drift-instability/2fluid.cxx +++ b/tests/integrated/test-drift-instability/2fluid.cxx @@ -226,7 +226,7 @@ class TwoFluid : public PhysicsModel { g23 = -Btxy / (hthe * Bpxy * Rxy); coord->setContravariantMetricTensor(MetricTensor(g11, g22, g33, g12, g13, g23)); - coord->J = hthe / Bpxy; + coord->setJ(hthe / Bpxy); MetricTensor::FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; g_11 = 1.0 / coord->g11() + SQ(I * Rxy); diff --git a/tests/integrated/test-interchange-instability/2fluid.cxx b/tests/integrated/test-interchange-instability/2fluid.cxx index b3414ae3a9..34496f8825 100644 --- a/tests/integrated/test-interchange-instability/2fluid.cxx +++ b/tests/integrated/test-interchange-instability/2fluid.cxx @@ -149,7 +149,7 @@ class Interchange : public PhysicsModel { coord->setContravariantMetricTensor( MetricTensor(g11, g22, g33, g12, g13, g23)); - coord->J = hthe / Bpxy; + coord->setJ(hthe / Bpxy); MetricTensor::FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; g_11 = 1.0 / coord->g11() + SQ(I * Rxy); diff --git a/tests/integrated/test-laplacexy/loadmetric.cxx b/tests/integrated/test-laplacexy/loadmetric.cxx index 6220ca1f52..7051f7444e 100644 --- a/tests/integrated/test-laplacexy/loadmetric.cxx +++ b/tests/integrated/test-laplacexy/loadmetric.cxx @@ -58,7 +58,7 @@ void LoadMetric(BoutReal Lnorm, BoutReal Bnorm) { coords->setContravariantMetricTensor( MetricTensor(g11, g22, g33, g12, g13, g23)); - coords->J = hthe / Bpxy; + coords->setJ(hthe / Bpxy); const auto g_11 = 1.0 / coords->g11() + pow(sinty * Rxy, 2); const auto g_22 = pow(coords->Bxy() * hthe / Bpxy, 2); From d1c3854d6332e2ea02a56624036eeaeee751447d Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 22 Nov 2023 18:51:16 +0000 Subject: [PATCH 153/491] Update more tests to use Bxy setter. --- examples/conducting-wall-mode/cwm.cxx | 2 +- examples/constraints/alfven-wave/alfven.cxx | 2 +- examples/em-drift/2fluid.cxx | 2 +- examples/lapd-drift/lapd_drift.cxx | 2 +- examples/laplacexy/alfven-wave/alfven.cxx | 2 +- examples/reconnect-2field/2field.cxx | 10 +++++----- examples/shear-alfven-wave/2fluid.cxx | 2 +- examples/tokamak-2fluid/2fluid.cxx | 2 +- tests/integrated/test-drift-instability/2fluid.cxx | 2 +- .../integrated/test-interchange-instability/2fluid.cxx | 8 +++----- 10 files changed, 16 insertions(+), 18 deletions(-) diff --git a/examples/conducting-wall-mode/cwm.cxx b/examples/conducting-wall-mode/cwm.cxx index 2418b8183d..843a364eb0 100644 --- a/examples/conducting-wall-mode/cwm.cxx +++ b/examples/conducting-wall-mode/cwm.cxx @@ -181,7 +181,7 @@ class CWM : public PhysicsModel { // Normalise magnetic field Bpxy /= (bmag / 1.e4); Btxy /= (bmag / 1.e4); - coord->Bxy() /= (bmag / 1.e4); + coord->setBxy(coord->Bxy() / (bmag / 1.e4)); // Set nu nu = nu_hat * Ni0 / pow(Te0, 1.5); diff --git a/examples/constraints/alfven-wave/alfven.cxx b/examples/constraints/alfven-wave/alfven.cxx index bd3b54952d..56d70a37b2 100644 --- a/examples/constraints/alfven-wave/alfven.cxx +++ b/examples/constraints/alfven-wave/alfven.cxx @@ -183,7 +183,7 @@ class Alfven : public PhysicsModel { Bpxy /= Bnorm; Btxy /= Bnorm; - coord->Bxy() /= Bnorm; + coord->setBxy(coord->Bxy() / Bnorm); // Check type of parallel transform std::string ptstr = diff --git a/examples/em-drift/2fluid.cxx b/examples/em-drift/2fluid.cxx index 3ec54858cd..5061b6c106 100644 --- a/examples/em-drift/2fluid.cxx +++ b/examples/em-drift/2fluid.cxx @@ -174,7 +174,7 @@ class EMdrift : public PhysicsModel { // Normalise magnetic field Bpxy /= (bmag / 1.e4); Btxy /= (bmag / 1.e4); - coord->Bxy() /= (bmag / 1.e4); + coord->setBxy(coord->Bxy() / (bmag / 1.e4)); /**************** CALCULATE METRICS ******************/ diff --git a/examples/lapd-drift/lapd_drift.cxx b/examples/lapd-drift/lapd_drift.cxx index aa8e9284df..3542b1bf76 100644 --- a/examples/lapd-drift/lapd_drift.cxx +++ b/examples/lapd-drift/lapd_drift.cxx @@ -324,7 +324,7 @@ class LAPDdrift : public PhysicsModel { // Normalise magnetic field Bpxy /= (bmag / 1.e4); Btxy /= (bmag / 1.e4); - coord->Bxy() /= (bmag / 1.e4); + coord->setBxy(coord->Bxy() / (bmag / 1.e4)); // calculate pressures pei0 = (Ti0 + Te0) * Ni0; diff --git a/examples/laplacexy/alfven-wave/alfven.cxx b/examples/laplacexy/alfven-wave/alfven.cxx index 3c95f62e97..b2507b9ee1 100644 --- a/examples/laplacexy/alfven-wave/alfven.cxx +++ b/examples/laplacexy/alfven-wave/alfven.cxx @@ -192,7 +192,7 @@ class Alfven : public PhysicsModel { Bpxy /= Bnorm; Btxy /= Bnorm; - coord->Bxy() /= Bnorm; + coord->setBxy(coord->Bxy() / Bnorm); // Calculate metric components sinty = 0.0; // I disappears from metric for shifted coordinates diff --git a/examples/reconnect-2field/2field.cxx b/examples/reconnect-2field/2field.cxx index 2d1c499447..aef6a3eb49 100644 --- a/examples/reconnect-2field/2field.cxx +++ b/examples/reconnect-2field/2field.cxx @@ -156,7 +156,7 @@ class TwoField : public PhysicsModel { // Normalise magnetic field Bpxy /= Bnorm; Btxy /= Bnorm; - coord->Bxy() /= Bnorm; + coord->setBxy(coord->Bxy() / Bnorm); // Plasma quantities Jpar0 /= Nenorm * Charge * Cs; @@ -273,10 +273,10 @@ class TwoField : public PhysicsModel { ddt(U) = SQ(coord->Bxy()) * Grad_parP(jpar / coord_ylow->Bxy(), CELL_CENTRE); if (include_jpar0) { - ddt(U) -= - SQ(coord->Bxy()) * beta_hat - * interp_to(bracket(Apar + Apar_ext, Jpar0 / coord_ylow->Bxy(), BRACKET_ARAKAWA), - CELL_CENTRE); + ddt(U) -= SQ(coord->Bxy()) * beta_hat + * interp_to( + bracket(Apar + Apar_ext, Jpar0 / coord_ylow->Bxy(), BRACKET_ARAKAWA), + CELL_CENTRE); } ddt(U) -= bracket(Phi0_ext, U, bm); // ExB advection diff --git a/examples/shear-alfven-wave/2fluid.cxx b/examples/shear-alfven-wave/2fluid.cxx index 1506ff7af4..c137a828ca 100644 --- a/examples/shear-alfven-wave/2fluid.cxx +++ b/examples/shear-alfven-wave/2fluid.cxx @@ -174,7 +174,7 @@ class ShearAlfven : public PhysicsModel { // Normalise magnetic field Bpxy /= (bmag / 1.e4); Btxy /= (bmag / 1.e4); - coord->Bxy() /= (bmag / 1.e4); + coord->setBxy(coord->Bxy() / (bmag / 1.e4)); // calculate pressures pei0 = (Ti0 + Te0) * Ni0; diff --git a/examples/tokamak-2fluid/2fluid.cxx b/examples/tokamak-2fluid/2fluid.cxx index 8f113980c4..aa9c7ee398 100644 --- a/examples/tokamak-2fluid/2fluid.cxx +++ b/examples/tokamak-2fluid/2fluid.cxx @@ -390,7 +390,7 @@ class TwoFluid : public PhysicsModel { // Normalise magnetic field Bpxy /= (bmag / 1.e4); Btxy /= (bmag / 1.e4); - coord->Bxy() /= (bmag / 1.e4); + coord->setBxy(coord->Bxy() / (bmag / 1.e4)); // calculate pressures pei0 = (Ti0 + Te0) * Ni0; diff --git a/tests/integrated/test-drift-instability/2fluid.cxx b/tests/integrated/test-drift-instability/2fluid.cxx index 687f55f37a..3e38eff4be 100644 --- a/tests/integrated/test-drift-instability/2fluid.cxx +++ b/tests/integrated/test-drift-instability/2fluid.cxx @@ -209,7 +209,7 @@ class TwoFluid : public PhysicsModel { // Normalise magnetic field Bpxy /= (bmag / 1.e4); Btxy /= (bmag / 1.e4); - coord->Bxy() /= (bmag / 1.e4); + coord->setBxy(coord->Bxy() / (bmag / 1.e4)); // calculate pressures pei0 = (Ti0 + Te0) * Ni0; diff --git a/tests/integrated/test-interchange-instability/2fluid.cxx b/tests/integrated/test-interchange-instability/2fluid.cxx index 34496f8825..9f0755f2eb 100644 --- a/tests/integrated/test-interchange-instability/2fluid.cxx +++ b/tests/integrated/test-interchange-instability/2fluid.cxx @@ -135,7 +135,7 @@ class Interchange : public PhysicsModel { // Normalise magnetic field Bpxy /= (bmag / 1.e4); Btxy /= (bmag / 1.e4); - coord->Bxy() /= (bmag / 1.e4); + coord->setBxy(coord->Bxy() / (bmag / 1.e4)); /**************** CALCULATE METRICS ******************/ @@ -146,8 +146,7 @@ class Interchange : public PhysicsModel { g12 = 0.0; g13 = -I * coord->g11(); g23 = -Btxy / (hthe * Bpxy * Rxy); - coord->setContravariantMetricTensor( - MetricTensor(g11, g22, g33, g12, g13, g23)); + coord->setContravariantMetricTensor(MetricTensor(g11, g22, g33, g12, g13, g23)); coord->setJ(hthe / Bpxy); @@ -158,8 +157,7 @@ class Interchange : public PhysicsModel { g_12 = Btxy * hthe * I * Rxy / Bpxy; g_13 = I * Rxy * Rxy; g_23 = Btxy * hthe * Rxy / Bpxy; - coord->setCovariantMetricTensor( - MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + coord->setCovariantMetricTensor(MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); coord->geometry(); From bc44dde684219ffb64d057738e884572620d4448 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 22 Nov 2023 19:56:53 +0000 Subject: [PATCH 154/491] Add setter for J that takes [x, y] indices. Update test_snb. --- include/bout/coordinates.hxx | 1 + src/mesh/coordinates.cxx | 5 +++++ tests/integrated/test-snb/test_snb.cxx | 4 ++-- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index 97b39229b0..2a0c1f0ed2 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -120,6 +120,7 @@ public: const std::string& region = "RGN_ALL"); void setJ(FieldMetric J); + void setJ(BoutReal value, int x, int y); void setBxy(FieldMetric Bxy); diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index be51aa0385..3a3f137e9b 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -1885,6 +1885,11 @@ void Coordinates::setJ(FieldMetric J) { this_J = J; } +void Coordinates::setJ(BoutReal value, int x, int y) { + //TODO: Calculate Bxy and check value is close + this_J(x, y) = value; +} + void Coordinates::setBxy(FieldMetric Bxy) { //TODO: Calculate Bxy and check value is close this_Bxy = Bxy; diff --git a/tests/integrated/test-snb/test_snb.cxx b/tests/integrated/test-snb/test_snb.cxx index 1b96bfc8b1..f16f678170 100644 --- a/tests/integrated/test-snb/test_snb.cxx +++ b/tests/integrated/test-snb/test_snb.cxx @@ -212,7 +212,7 @@ int main(int argc, char** argv) { double yn = (double(y) + 0.5) / double(mesh->yend + 1); coord->dy(x, y) = 1. - 0.9 * yn; - coord->J(x, y) = (1. + yn * yn); + coord->setJ((1. + yn * yn), x, y); } } @@ -229,7 +229,7 @@ int main(int argc, char** argv) { EXPECT_FALSE(IsFieldClose(Div_q, Div_q_SH, "RGN_NOBNDRY")); const Field2D dy = coord->dy; - const Field2D J = coord->J; + const Field2D J = coord->J(); // Integrate Div(q) over domain BoutReal q_sh = 0.0; From b75b283d309981a90e0985e3ae9f575fa9b0b01d Mon Sep 17 00:00:00 2001 From: tomc271 Date: Sun, 19 Nov 2023 21:34:00 +0000 Subject: [PATCH 155/491] Add 'getComponents' method to MetricTensor and use std::transform to call a function on each component. --- include/bout/metricTensor.hxx | 2 ++ src/mesh/coordinates.cxx | 26 ++++++++------------------ src/mesh/metricTensor.cxx | 5 +++++ 3 files changed, 15 insertions(+), 18 deletions(-) diff --git a/include/bout/metricTensor.hxx b/include/bout/metricTensor.hxx index 0f424365ca..386ece19ec 100644 --- a/include/bout/metricTensor.hxx +++ b/include/bout/metricTensor.hxx @@ -40,6 +40,8 @@ public: MetricTensor oppositeRepresentation(const CELL_LOC location, Mesh* mesh, const std::string& region = "RGN_ALL"); + std::vector getComponents() const; + protected: FieldMetric g11, g22, g33, g12, g13, g23; }; diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 3a3f137e9b..f57f875f99 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -751,24 +751,14 @@ void Coordinates::interpolateAndExtrapolateContravariantMetricTensor( const auto region = "RGN_NOBNDRY"; - FieldMetric g11, g22, g33, g12, g13, g23; - - // Diagonal components of metric tensor g^{ij} - g11 = interpolateAndExtrapolate(coords_in->g11(), location, true, true, false, - transform.get(), region); - g22 = interpolateAndExtrapolate(coords_in->g22(), location, true, true, false, - transform.get(), region); - g33 = interpolateAndExtrapolate(coords_in->g33(), location, true, true, false, - transform.get(), region); - - // Off-diagonal elements. - g12 = interpolateAndExtrapolate(coords_in->g12(), location, true, true, false, - transform.get(), region); - g13 = interpolateAndExtrapolate(coords_in->g13(), location, true, true, false, - transform.get(), region); - g23 = interpolateAndExtrapolate(coords_in->g23(), location, true, true, false, - transform.get(), region); - + const auto components = coords_in->contravariantMetricTensor.getComponents(); + FieldMetric components_modified[6]; + std::transform(components.begin(), components.end(), components_modified, + [this, ®ion](const FieldMetric component) { + return interpolateAndExtrapolate(component, location, true, true, + false, transform.get(), region); + }); + auto [g11, g22, g33, g12, g13, g23] = components_modified; setContravariantMetricTensor(MetricTensor(g11, g22, g33, g12, g13, g23), region); } diff --git a/src/mesh/metricTensor.cxx b/src/mesh/metricTensor.cxx index 3903079948..ff91c0e0df 100644 --- a/src/mesh/metricTensor.cxx +++ b/src/mesh/metricTensor.cxx @@ -169,3 +169,8 @@ MetricTensor MetricTensor::oppositeRepresentation(CELL_LOC location, Mesh* mesh, other_representation.setLocation(location); return other_representation; } + +std::vector MetricTensor::getComponents() const { + + return std::vector{g11, g22, g33, g12, g13, g23}; +} From d58b9a2b6def06d8f3fa87d7db9b9d101ce1eb77 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Mon, 20 Nov 2023 14:30:19 +0000 Subject: [PATCH 156/491] Add method Coordinates::applyToMetricTensor (and remove 'interpolateAndExtrapolateContravariantMetricTensor' method). --- include/bout/coordinates.hxx | 4 +++- src/mesh/coordinates.cxx | 32 +++++++++++++++++++------------- 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index 2a0c1f0ed2..a2badd6e10 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -271,8 +271,10 @@ private: void checkCovariant(); // check that contravariant tensors are positive (if expected) and finite (always) void checkContravariant(); - void interpolateAndExtrapolateContravariantMetricTensor(const Coordinates* coords_in); + // Transforms a MetricTensor by applying the given function to every component + MetricTensor applyToMetricTensor( + const std::function function) const; FieldMetric getAtLocOrUnaligned(Mesh* mesh, const std::string& name, BoutReal default_value = 0., const std::string& suffix = "", diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index f57f875f99..4976571804 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -464,7 +464,16 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, dz = interpolateAndExtrapolate(coords_in->dz, location, true, true, false, transform.get()); - interpolateAndExtrapolateContravariantMetricTensor(coords_in); + std::function + interpolateAndExtrapolate_function = [this](const FieldMetric component) { + return interpolateAndExtrapolate(component, location, true, true, false, + transform.get()); + }; + + std::basic_string region = std::basic_string("RGN_NOBNDRY"); + const auto new_metric_tensor = + coords_in->applyToMetricTensor(interpolateAndExtrapolate_function); + setContravariantMetricTensor(new_metric_tensor, region); FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; // 3x3 matrix inversion can exaggerate small interpolation errors, so it is @@ -746,20 +755,17 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, } } -void Coordinates::interpolateAndExtrapolateContravariantMetricTensor( - const Coordinates* coords_in) { +MetricTensor Coordinates::applyToMetricTensor( + const std::function function) const { + + const auto components_in = contravariantMetricTensor.getComponents(); + + FieldMetric components_out[6]; - const auto region = "RGN_NOBNDRY"; + std::transform(components_in.begin(), components_in.end(), components_out, function); + auto [g11, g22, g33, g12, g13, g23] = components_out; - const auto components = coords_in->contravariantMetricTensor.getComponents(); - FieldMetric components_modified[6]; - std::transform(components.begin(), components.end(), components_modified, - [this, ®ion](const FieldMetric component) { - return interpolateAndExtrapolate(component, location, true, true, - false, transform.get(), region); - }); - auto [g11, g22, g33, g12, g13, g23] = components_modified; - setContravariantMetricTensor(MetricTensor(g11, g22, g33, g12, g13, g23), region); + return MetricTensor(g11, g22, g33, g12, g13, g23); } void Coordinates::outputVars(Options& output_options) { From 201d27fcb17f27ca28c03eff17ebdc81fafae222 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Mon, 20 Nov 2023 15:14:22 +0000 Subject: [PATCH 157/491] Add MetricTensor::applyToComponents() and remove Coordinates::applyToMetricTensor(). --- include/bout/metricTensor.hxx | 3 +++ src/mesh/coordinates.cxx | 17 ++--------------- src/mesh/metricTensor.cxx | 13 +++++++++++++ 3 files changed, 18 insertions(+), 15 deletions(-) diff --git a/include/bout/metricTensor.hxx b/include/bout/metricTensor.hxx index 386ece19ec..19f6cdefbf 100644 --- a/include/bout/metricTensor.hxx +++ b/include/bout/metricTensor.hxx @@ -42,6 +42,9 @@ public: std::vector getComponents() const; + MetricTensor applyToComponents( + const std::function function) const; + protected: FieldMetric g11, g22, g33, g12, g13, g23; }; diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 4976571804..f39695d582 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -471,8 +471,8 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, }; std::basic_string region = std::basic_string("RGN_NOBNDRY"); - const auto new_metric_tensor = - coords_in->applyToMetricTensor(interpolateAndExtrapolate_function); + const auto new_metric_tensor = coords_in->contravariantMetricTensor.applyToComponents( + interpolateAndExtrapolate_function); setContravariantMetricTensor(new_metric_tensor, region); FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; @@ -755,19 +755,6 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, } } -MetricTensor Coordinates::applyToMetricTensor( - const std::function function) const { - - const auto components_in = contravariantMetricTensor.getComponents(); - - FieldMetric components_out[6]; - - std::transform(components_in.begin(), components_in.end(), components_out, function); - auto [g11, g22, g33, g12, g13, g23] = components_out; - - return MetricTensor(g11, g22, g33, g12, g13, g23); -} - void Coordinates::outputVars(Options& output_options) { Timer time("io"); const std::string loc_string = diff --git a/src/mesh/metricTensor.cxx b/src/mesh/metricTensor.cxx index ff91c0e0df..8f18de9e24 100644 --- a/src/mesh/metricTensor.cxx +++ b/src/mesh/metricTensor.cxx @@ -174,3 +174,16 @@ std::vector MetricTensor::getComponents() const { return std::vector{g11, g22, g33, g12, g13, g23}; } + +MetricTensor MetricTensor::applyToComponents( + const std::function function) const { + + const auto components_in = getComponents(); + + FieldMetric components_out[6]; + + std::transform(components_in.begin(), components_in.end(), components_out, function); + auto [g_11, g_22, g_33, g_12, g_13, g_23] = components_out; + + return MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23); +} From ba24926844341ef454946127a37ebc2987f05e04 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 22 Nov 2023 21:14:13 +0000 Subject: [PATCH 158/491] MetricTensor::getComponents() now only used internally. --- include/bout/coordinates.hxx | 3 --- include/bout/metricTensor.hxx | 4 ++-- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index a2badd6e10..63b9ab450e 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -272,9 +272,6 @@ private: // check that contravariant tensors are positive (if expected) and finite (always) void checkContravariant(); - // Transforms a MetricTensor by applying the given function to every component - MetricTensor applyToMetricTensor( - const std::function function) const; FieldMetric getAtLocOrUnaligned(Mesh* mesh, const std::string& name, BoutReal default_value = 0., const std::string& suffix = "", diff --git a/include/bout/metricTensor.hxx b/include/bout/metricTensor.hxx index 19f6cdefbf..739d76afd3 100644 --- a/include/bout/metricTensor.hxx +++ b/include/bout/metricTensor.hxx @@ -40,13 +40,13 @@ public: MetricTensor oppositeRepresentation(const CELL_LOC location, Mesh* mesh, const std::string& region = "RGN_ALL"); - std::vector getComponents() const; - MetricTensor applyToComponents( const std::function function) const; protected: FieldMetric g11, g22, g33, g12, g13, g23; + + std::vector getComponents() const; }; #endif //BOUT_METRICTENSOR_HXX From 71cdf570675ea07b97368eb56be554a6741c26be Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 22 Nov 2023 21:30:53 +0000 Subject: [PATCH 159/491] Use applyToComponents() method with covariantMetricTensor, to further DRY up the code. --- src/mesh/coordinates.cxx | 32 ++++++++------------------------ 1 file changed, 8 insertions(+), 24 deletions(-) diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index f39695d582..abfa0815c3 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -471,30 +471,14 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, }; std::basic_string region = std::basic_string("RGN_NOBNDRY"); - const auto new_metric_tensor = coords_in->contravariantMetricTensor.applyToComponents( - interpolateAndExtrapolate_function); - setContravariantMetricTensor(new_metric_tensor, region); - - FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; - // 3x3 matrix inversion can exaggerate small interpolation errors, so it is - // more robust to interpolate and extrapolate derived quantities directly, - // rather than deriving from interpolated/extrapolated covariant metric - // components - g_11 = interpolateAndExtrapolate(covariantMetricTensor.Getg11(), location, true, true, - false, transform.get()); - g_22 = interpolateAndExtrapolate(covariantMetricTensor.Getg22(), location, true, true, - false, transform.get()); - g_33 = interpolateAndExtrapolate(covariantMetricTensor.Getg33(), location, true, true, - false, transform.get()); - g_12 = interpolateAndExtrapolate(covariantMetricTensor.Getg12(), location, true, true, - false, transform.get()); - g_13 = interpolateAndExtrapolate(covariantMetricTensor.Getg13(), location, true, true, - false, transform.get()); - g_23 = interpolateAndExtrapolate(covariantMetricTensor.Getg23(), location, true, true, - false, transform.get()); - - covariantMetricTensor.setMetricTensor( - MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + const auto new_contravariant_metric_tensor = + coords_in->contravariantMetricTensor.applyToComponents( + interpolateAndExtrapolate_function); + setContravariantMetricTensor(new_contravariant_metric_tensor, region); + + const auto new_covariant_metric_tensor = + covariantMetricTensor.applyToComponents(interpolateAndExtrapolate_function); + covariantMetricTensor.setMetricTensor(new_covariant_metric_tensor); // Check input metrics checkContravariant(); From 50839fe524a65d93e1980ab36e4ccbcf6f8ee839 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 22 Nov 2023 21:39:30 +0000 Subject: [PATCH 160/491] Add MetricTensor::map() method and use to call interpolateAndExtrapolate() on each component. --- include/bout/metricTensor.hxx | 3 +++ src/mesh/coordinates.cxx | 32 +++++++++----------------------- src/mesh/metricTensor.cxx | 9 +++++++++ 3 files changed, 21 insertions(+), 23 deletions(-) diff --git a/include/bout/metricTensor.hxx b/include/bout/metricTensor.hxx index 739d76afd3..6a71ed9a5c 100644 --- a/include/bout/metricTensor.hxx +++ b/include/bout/metricTensor.hxx @@ -40,6 +40,9 @@ public: MetricTensor oppositeRepresentation(const CELL_LOC location, Mesh* mesh, const std::string& region = "RGN_ALL"); + // Transforms the MetricTensor by applying the given function to every component + void map(const std::function function); + MetricTensor applyToComponents( const std::function function) const; diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index abfa0815c3..967562ca12 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -634,30 +634,16 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, } } - FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; - // More robust to extrapolate derived quantities directly, rather than // deriving from extrapolated covariant metric components - g_11 = - interpolateAndExtrapolate(covariantMetricTensor.Getg11(), location, extrapolate_x, - extrapolate_y, false, transform.get()); - g_22 = - interpolateAndExtrapolate(covariantMetricTensor.Getg22(), location, extrapolate_x, - extrapolate_y, false, transform.get()); - g_33 = - interpolateAndExtrapolate(covariantMetricTensor.Getg33(), location, extrapolate_x, - extrapolate_y, false, transform.get()); - g_12 = - interpolateAndExtrapolate(covariantMetricTensor.Getg12(), location, extrapolate_x, - extrapolate_y, false, transform.get()); - g_13 = - interpolateAndExtrapolate(covariantMetricTensor.Getg13(), location, extrapolate_x, - extrapolate_y, false, transform.get()); - g_23 = - interpolateAndExtrapolate(covariantMetricTensor.Getg23(), location, extrapolate_x, - extrapolate_y, false, transform.get()); - covariantMetricTensor.setMetricTensor( - MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + + std::function + interpolateAndExtrapolate_function = + [this, extrapolate_y, extrapolate_x](const FieldMetric component) { + return interpolateAndExtrapolate(component, location, extrapolate_x, + extrapolate_y, false, transform.get()); + }; + covariantMetricTensor.map(interpolateAndExtrapolate_function); // Check covariant metrics checkCovariant(); @@ -684,7 +670,7 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, communicate(this_J); // Re-evaluate Bxy using new J - this_Bxy = sqrt(g_22) / this_J; + this_Bxy = sqrt(g_22()) / this_J; } // Check jacobian diff --git a/src/mesh/metricTensor.cxx b/src/mesh/metricTensor.cxx index 8f18de9e24..7f639fb813 100644 --- a/src/mesh/metricTensor.cxx +++ b/src/mesh/metricTensor.cxx @@ -175,6 +175,15 @@ std::vector MetricTensor::getComponents() const { return std::vector{g11, g22, g33, g12, g13, g23}; } +void MetricTensor::map( + const std::function function) { + + const auto components_out = applyToComponents(function); + + auto [g_11, g_22, g_33, g_12, g_13, g_23] = components_out; + setMetricTensor(MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); +} + MetricTensor MetricTensor::applyToComponents( const std::function function) const { From e5ecd40447deb1450f88cde34cfe4fda9d222a5f Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 22 Nov 2023 22:55:28 +0000 Subject: [PATCH 161/491] Call setMetricTensor() directly when both both contra- and covariant are being calculated (avoids oppositeRepresentation() being called by the setter). --- src/mesh/coordinates.cxx | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 967562ca12..747371a787 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -471,10 +471,9 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, }; std::basic_string region = std::basic_string("RGN_NOBNDRY"); - const auto new_contravariant_metric_tensor = - coords_in->contravariantMetricTensor.applyToComponents( - interpolateAndExtrapolate_function); - setContravariantMetricTensor(new_contravariant_metric_tensor, region); + setContravariantMetricTensor(coords_in->contravariantMetricTensor.applyToComponents( + interpolateAndExtrapolate_function), + region); const auto new_covariant_metric_tensor = covariantMetricTensor.applyToComponents(interpolateAndExtrapolate_function); @@ -583,7 +582,7 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, extrapolate_x, extrapolate_y, false, transform.get()); - setContravariantMetricTensor(MetricTensor(g11, g22, g33, g12, g13, g23)); + contravariantMetricTensor.setMetricTensor(MetricTensor(g11, g22, g33, g12, g13, g23)); // Check input metrics checkContravariant(); From e58e649e61e6622f98393339e39143bedc0cd523 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Thu, 23 Nov 2023 16:48:29 +0000 Subject: [PATCH 162/491] Removed assignment to temporary variable. --- src/mesh/coordinates.cxx | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 747371a787..957c854b0f 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -475,9 +475,8 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, interpolateAndExtrapolate_function), region); - const auto new_covariant_metric_tensor = - covariantMetricTensor.applyToComponents(interpolateAndExtrapolate_function); - covariantMetricTensor.setMetricTensor(new_covariant_metric_tensor); + covariantMetricTensor.setMetricTensor( + covariantMetricTensor.applyToComponents(interpolateAndExtrapolate_function)); // Check input metrics checkContravariant(); From 778b27d487db650f102d2c6e78c5e722ebfbef9e Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 22 Nov 2023 23:06:15 +0000 Subject: [PATCH 163/491] Use covariantMetricTensor.map() instead of covariantMetricTensor.applyToComponents() and covariantMetricTensor.setMetricTensor(). --- src/mesh/coordinates.cxx | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 957c854b0f..5aa8fbd9d9 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -475,8 +475,7 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, interpolateAndExtrapolate_function), region); - covariantMetricTensor.setMetricTensor( - covariantMetricTensor.applyToComponents(interpolateAndExtrapolate_function)); + covariantMetricTensor.map(interpolateAndExtrapolate_function); // Check input metrics checkContravariant(); From 6815813c11a59bc77ac6eafee75c08817e3f58c4 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 22 Nov 2023 23:18:24 +0000 Subject: [PATCH 164/491] Extract private method 'recalculateBxy'. --- include/bout/coordinates.hxx | 2 ++ src/mesh/coordinates.cxx | 16 +++++++++++----- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index 63b9ab450e..736f631936 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -290,6 +290,8 @@ private: FieldMetric this_J; FieldMetric this_Bxy; ///< Magnitude of B = nabla z times nabla x + + void recalculateBxy(const bool extrapolate_x, const bool extrapolate_y); }; /* diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 5aa8fbd9d9..0f4e5d1585 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -1166,9 +1166,6 @@ int Coordinates::jacobian() { TRACE("Coordinates::jacobian"); // calculate Jacobian using g^-1 = det[g^ij], J = sqrt(g) - const bool extrapolate_x = not localmesh->sourceHasXBoundaryGuards(); - const bool extrapolate_y = not localmesh->sourceHasYBoundaryGuards(); - auto g = contravariantMetricTensor.Getg11() * contravariantMetricTensor.Getg22() * contravariantMetricTensor.Getg33() + 2.0 * contravariantMetricTensor.Getg12() * contravariantMetricTensor.Getg13() @@ -1184,16 +1181,25 @@ int Coordinates::jacobian() { bout::checkPositive(g, "The determinant of g^ij", "RGN_NOBNDRY"); this_J = 1. / sqrt(g); + // More robust to extrapolate derived quantities directly, rather than // deriving from extrapolated covariant metric components + + const bool extrapolate_x = not localmesh->sourceHasXBoundaryGuards(); + const bool extrapolate_y = not localmesh->sourceHasYBoundaryGuards(); + this_J = interpolateAndExtrapolate(this_J, location, extrapolate_x, extrapolate_y, false, transform.get()); + recalculateBxy(extrapolate_x, extrapolate_y); + + return 0; +} + +void Coordinates::recalculateBxy(const bool extrapolate_x, const bool extrapolate_y) { this_Bxy = sqrt(covariantMetricTensor.Getg22()) / this_J; this_Bxy = interpolateAndExtrapolate(this_Bxy, location, extrapolate_x, extrapolate_y, false, transform.get()); - - return 0; } namespace { From 8761934a08e7606b817a77ed8e63e18a676ec12c Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 22 Nov 2023 23:24:00 +0000 Subject: [PATCH 165/491] Extract private method 'recalculateJacobian'. --- include/bout/coordinates.hxx | 3 ++- src/mesh/coordinates.cxx | 18 ++++++++++++------ 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index 736f631936..85c358b36b 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -291,7 +291,8 @@ private: FieldMetric this_J; FieldMetric this_Bxy; ///< Magnitude of B = nabla z times nabla x - void recalculateBxy(const bool extrapolate_x, const bool extrapolate_y); + void recalculateJacobian(); + void recalculateBxy(); }; /* diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 0f4e5d1585..1d418563b5 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -1164,8 +1164,15 @@ void Coordinates::calcContravariant(const std::string& region) { int Coordinates::jacobian() { TRACE("Coordinates::jacobian"); - // calculate Jacobian using g^-1 = det[g^ij], J = sqrt(g) + recalculateJacobian(); + recalculateBxy(); + return 0; +} + +void Coordinates::recalculateJacobian() { + + // calculate Jacobian using g^-1 = det[g^ij], J = sqrt(g) auto g = contravariantMetricTensor.Getg11() * contravariantMetricTensor.Getg22() * contravariantMetricTensor.Getg33() + 2.0 * contravariantMetricTensor.Getg12() * contravariantMetricTensor.Getg13() @@ -1184,19 +1191,18 @@ int Coordinates::jacobian() { // More robust to extrapolate derived quantities directly, rather than // deriving from extrapolated covariant metric components - const bool extrapolate_x = not localmesh->sourceHasXBoundaryGuards(); const bool extrapolate_y = not localmesh->sourceHasYBoundaryGuards(); this_J = interpolateAndExtrapolate(this_J, location, extrapolate_x, extrapolate_y, false, transform.get()); +} - recalculateBxy(extrapolate_x, extrapolate_y); +void Coordinates::recalculateBxy() { - return 0; -} + const bool extrapolate_x = not localmesh->sourceHasXBoundaryGuards(); + const bool extrapolate_y = not localmesh->sourceHasYBoundaryGuards(); -void Coordinates::recalculateBxy(const bool extrapolate_x, const bool extrapolate_y) { this_Bxy = sqrt(covariantMetricTensor.Getg22()) / this_J; this_Bxy = interpolateAndExtrapolate(this_Bxy, location, extrapolate_x, extrapolate_y, false, transform.get()); From 8b0fe855939f8f88a3c16f56fbc39008e4522e19 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 22 Nov 2023 23:31:02 +0000 Subject: [PATCH 166/491] Make recalculateJacobian() and recalculateBxy() just return the calculated values. --- include/bout/coordinates.hxx | 4 ++-- src/mesh/coordinates.cxx | 20 ++++++++++---------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index 85c358b36b..9a466889ec 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -291,8 +291,8 @@ private: FieldMetric this_J; FieldMetric this_Bxy; ///< Magnitude of B = nabla z times nabla x - void recalculateJacobian(); - void recalculateBxy(); + FieldMetric recalculateJacobian(); + FieldMetric recalculateBxy(); }; /* diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 1d418563b5..62e883c50c 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -1165,12 +1165,12 @@ void Coordinates::calcContravariant(const std::string& region) { int Coordinates::jacobian() { TRACE("Coordinates::jacobian"); - recalculateJacobian(); - recalculateBxy(); + this_J = recalculateJacobian(); + this_Bxy = recalculateBxy(); return 0; } -void Coordinates::recalculateJacobian() { +MetricTensor::FieldMetric Coordinates::recalculateJacobian() { // calculate Jacobian using g^-1 = det[g^ij], J = sqrt(g) auto g = contravariantMetricTensor.Getg11() * contravariantMetricTensor.Getg22() @@ -1187,25 +1187,25 @@ void Coordinates::recalculateJacobian() { // Check that g is positive bout::checkPositive(g, "The determinant of g^ij", "RGN_NOBNDRY"); - this_J = 1. / sqrt(g); + const auto J = 1. / sqrt(g); // More robust to extrapolate derived quantities directly, rather than // deriving from extrapolated covariant metric components const bool extrapolate_x = not localmesh->sourceHasXBoundaryGuards(); const bool extrapolate_y = not localmesh->sourceHasYBoundaryGuards(); - this_J = interpolateAndExtrapolate(this_J, location, extrapolate_x, extrapolate_y, - false, transform.get()); + return interpolateAndExtrapolate(J, location, extrapolate_x, extrapolate_y, false, + transform.get()); } -void Coordinates::recalculateBxy() { +MetricTensor::FieldMetric Coordinates::recalculateBxy() { const bool extrapolate_x = not localmesh->sourceHasXBoundaryGuards(); const bool extrapolate_y = not localmesh->sourceHasYBoundaryGuards(); - this_Bxy = sqrt(covariantMetricTensor.Getg22()) / this_J; - this_Bxy = interpolateAndExtrapolate(this_Bxy, location, extrapolate_x, extrapolate_y, - false, transform.get()); + const auto Bxy = sqrt(covariantMetricTensor.Getg22()) / this_J; + return interpolateAndExtrapolate(Bxy, location, extrapolate_x, extrapolate_y, false, + transform.get()); } namespace { From 72c4e2cc8056ec595b011fe20964e63fab1ec023 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 22 Nov 2023 23:41:34 +0000 Subject: [PATCH 167/491] Handle errors with try-catch rather than returning an int. --- include/bout/coordinates.hxx | 2 +- src/mesh/coordinates.cxx | 17 +++++++++-------- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index 9a466889ec..0b2085ffb6 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -143,7 +143,7 @@ public: void calcCovariant(const std::string& region = "RGN_ALL"); /// Invert covariant metric to get contravariant components void calcContravariant(const std::string& region = "RGN_ALL"); - int jacobian(); ///< Calculate J and Bxy + void jacobian(); ///< Calculate J and Bxy void CalculateChristoffelSymbols(); /// Calculate Christoffel symbol terms /////////////////////////////////////////////////////////// diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 62e883c50c..fc309a91a4 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -646,9 +646,7 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, checkCovariant(); /// Calculate Jacobian and Bxy - if (jacobian()) { - throw BoutException("Error in jacobian call"); - } + jacobian(); // Attempt to read J from the grid file auto Jcalc = this_J; @@ -1162,12 +1160,15 @@ void Coordinates::calcContravariant(const std::string& region) { covariantMetricTensor.oppositeRepresentation(location, localmesh, region)); } -int Coordinates::jacobian() { +void Coordinates::jacobian() { TRACE("Coordinates::jacobian"); - - this_J = recalculateJacobian(); - this_Bxy = recalculateBxy(); - return 0; + try { + this_J = recalculateJacobian(); + this_Bxy = recalculateBxy(); + } catch (BoutException&) { + output_error.write("\tError in jacobian call\n"); + throw; + } } MetricTensor::FieldMetric Coordinates::recalculateJacobian() { From 6acbf0bffcff3b196c93e9bc665f469b8abadcf9 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Fri, 24 Nov 2023 11:59:01 +0000 Subject: [PATCH 168/491] Assuming that all the metric tensor components have the same mesh, and that covariant and contravariant components have the same mesh. --- include/bout/metricTensor.hxx | 2 +- src/mesh/coordinates.cxx | 8 ++++---- src/mesh/metricTensor.cxx | 4 ++-- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/include/bout/metricTensor.hxx b/include/bout/metricTensor.hxx index 6a71ed9a5c..4c43acd1ae 100644 --- a/include/bout/metricTensor.hxx +++ b/include/bout/metricTensor.hxx @@ -37,7 +37,7 @@ public: void setLocation(const CELL_LOC location); - MetricTensor oppositeRepresentation(const CELL_LOC location, Mesh* mesh, + MetricTensor oppositeRepresentation(const CELL_LOC location, const std::string& region = "RGN_ALL"); // Transforms the MetricTensor by applying the given function to every component diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index fc309a91a4..8181febc30 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -1151,13 +1151,13 @@ void Coordinates::CalculateChristoffelSymbols() { void Coordinates::calcCovariant(const std::string& region) { TRACE("Coordinates::calcCovariant"); covariantMetricTensor.setMetricTensor( - contravariantMetricTensor.oppositeRepresentation(location, localmesh, region)); + contravariantMetricTensor.oppositeRepresentation(location, region)); } void Coordinates::calcContravariant(const std::string& region) { TRACE("Coordinates::calcContravariant"); contravariantMetricTensor.setMetricTensor( - covariantMetricTensor.oppositeRepresentation(location, localmesh, region)); + covariantMetricTensor.oppositeRepresentation(location, region)); } void Coordinates::jacobian() { @@ -1792,14 +1792,14 @@ void Coordinates::setContravariantMetricTensor(MetricTensor metric_tensor, const std::string& region) { contravariantMetricTensor.setMetricTensor(metric_tensor); covariantMetricTensor.setMetricTensor( - contravariantMetricTensor.oppositeRepresentation(location, localmesh, region)); + contravariantMetricTensor.oppositeRepresentation(location, region)); } void Coordinates::setCovariantMetricTensor(MetricTensor metric_tensor, const std::string& region) { covariantMetricTensor.setMetricTensor(metric_tensor); contravariantMetricTensor.setMetricTensor( - covariantMetricTensor.oppositeRepresentation(location, localmesh, region)); + covariantMetricTensor.oppositeRepresentation(location, region)); } const MetricTensor::FieldMetric& Coordinates::g_11() const { diff --git a/src/mesh/metricTensor.cxx b/src/mesh/metricTensor.cxx index 7f639fb813..11ca8f8f9e 100644 --- a/src/mesh/metricTensor.cxx +++ b/src/mesh/metricTensor.cxx @@ -107,7 +107,7 @@ void MetricTensor::setLocation(const CELL_LOC location) { g23.setLocation(location); } -MetricTensor MetricTensor::oppositeRepresentation(CELL_LOC location, Mesh* mesh, +MetricTensor MetricTensor::oppositeRepresentation(CELL_LOC location, const std::string& region) { TRACE("MetricTensor::CalculateOppositeRepresentation"); @@ -164,7 +164,7 @@ MetricTensor MetricTensor::oppositeRepresentation(CELL_LOC location, Mesh* mesh, // + g_23 * g_33))); // // output_info.write("\tMaximum error in off-diagonal inversion is {:e}\n", maxerr); - + const auto mesh = g11.getMesh(); // All the components have the same mesh auto other_representation = MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23, mesh); other_representation.setLocation(location); return other_representation; From 8e869ad423d06d2f3249890542b96cf74d5cf292 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Fri, 24 Nov 2023 11:49:14 +0000 Subject: [PATCH 169/491] Rename geometry() to calculateGeometry(). --- examples/2Dturbulence_multigrid/esel.cxx | 2 +- examples/6field-simple/elm_6f.cxx | 2 +- examples/conducting-wall-mode/cwm.cxx | 2 +- examples/constraints/alfven-wave/alfven.cxx | 2 +- examples/dalf3/dalf3.cxx | 2 +- examples/elm-pb-outerloop/elm_pb_outerloop.cxx | 2 +- examples/elm-pb/elm_pb.cxx | 2 +- examples/gyro-gem/gem.cxx | 2 +- examples/jorek-compare/jorek_compare.cxx | 2 +- examples/lapd-drift/lapd_drift.cxx | 2 +- examples/laplacexy/alfven-wave/alfven.cxx | 2 +- examples/laplacexy/laplace_perp/test.cxx | 2 +- examples/reconnect-2field/2field.cxx | 2 +- examples/shear-alfven-wave/2fluid.cxx | 2 +- examples/uedge-benchmark/ue_bmark.cxx | 2 +- examples/wave-slab/wave_slab.cxx | 2 +- include/bout/coordinates.hxx | 8 ++++---- include/bout/mesh.hxx | 2 +- manual/sphinx/developer_docs/mesh.rst | 2 +- manual/sphinx/user_docs/new_in_v5.rst | 2 +- src/mesh/mesh.cxx | 2 +- tests/MMS/diffusion/diffusion.cxx | 2 +- tests/MMS/wave-1d/wave.cxx | 2 +- tests/integrated/test-drift-instability/2fluid.cxx | 2 +- .../test-interchange-instability/2fluid.cxx | 2 +- tests/unit/fake_parallel_mesh.hxx | 2 +- tests/unit/field/test_field_factory.cxx | 2 +- tests/unit/field/test_vector2d.cxx | 2 +- tests/unit/field/test_vector3d.cxx | 2 +- tests/unit/include/bout/test_petsc_indexer.cxx | 2 +- tests/unit/invert/laplace/test_laplace_cyclic.cxx | 2 +- tests/unit/mesh/parallel/test_shiftedmetric.cxx | 2 +- tests/unit/mesh/test_coordinates.cxx | 12 ++++++------ tests/unit/mesh/test_interpolation.cxx | 4 ++-- tests/unit/test_extras.hxx | 6 +++--- tools/pylib/_boutpp_build/boutcpp.pxd.jinja | 2 +- 36 files changed, 47 insertions(+), 47 deletions(-) diff --git a/examples/2Dturbulence_multigrid/esel.cxx b/examples/2Dturbulence_multigrid/esel.cxx index e6be58b34b..79fcfc805c 100644 --- a/examples/2Dturbulence_multigrid/esel.cxx +++ b/examples/2Dturbulence_multigrid/esel.cxx @@ -89,7 +89,7 @@ class ESEL : public PhysicsModel { g_23 = 0.0; coord->setCovariantMetricTensor(MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); - coord->geometry(); + coord->calculateGeometry(); SOLVE_FOR(N, vort); SAVE_REPEAT(phi); diff --git a/examples/6field-simple/elm_6f.cxx b/examples/6field-simple/elm_6f.cxx index 66f3c0cb1a..9a11a09521 100644 --- a/examples/6field-simple/elm_6f.cxx +++ b/examples/6field-simple/elm_6f.cxx @@ -1072,7 +1072,7 @@ class Elm_6f : public PhysicsModel { g_23 = Btxy * hthe * Rxy / Bpxy; coord->setCovariantMetricTensor(MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); - coord->geometry(); // Calculate quantities from metric tensor + coord->calculateGeometry(); // Calculate quantities from metric tensor // Set B field vector diff --git a/examples/conducting-wall-mode/cwm.cxx b/examples/conducting-wall-mode/cwm.cxx index 843a364eb0..d00694ff5e 100644 --- a/examples/conducting-wall-mode/cwm.cxx +++ b/examples/conducting-wall-mode/cwm.cxx @@ -208,7 +208,7 @@ class CWM : public PhysicsModel { g_23 = Btxy * hthe * Rxy / Bpxy; coord->setCovariantMetricTensor(MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); - coord->geometry(); + coord->calculateGeometry(); /**************** SET EVOLVING VARIABLES *************/ diff --git a/examples/constraints/alfven-wave/alfven.cxx b/examples/constraints/alfven-wave/alfven.cxx index 56d70a37b2..c28c00ce4f 100644 --- a/examples/constraints/alfven-wave/alfven.cxx +++ b/examples/constraints/alfven-wave/alfven.cxx @@ -221,7 +221,7 @@ class Alfven : public PhysicsModel { g_23 = sbp * Btxy * hthe * Rxy / Bpxy; coord->setCovariantMetricTensor(MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); - coord->geometry(); + coord->calculateGeometry(); } }; diff --git a/examples/dalf3/dalf3.cxx b/examples/dalf3/dalf3.cxx index 68ba3f7d6d..fc71a41923 100644 --- a/examples/dalf3/dalf3.cxx +++ b/examples/dalf3/dalf3.cxx @@ -274,7 +274,7 @@ class DALF3 : public PhysicsModel { g_23 = Btxy * hthe * Rxy / Bpxy; coord->setCovariantMetricTensor(MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); - coord->geometry(); // Calculate quantities from metric tensor + coord->calculateGeometry(); // Calculate quantities from metric tensor SOLVE_FOR3(Vort, Pe, Vpar); comms.add(Vort, Pe, Vpar); diff --git a/examples/elm-pb-outerloop/elm_pb_outerloop.cxx b/examples/elm-pb-outerloop/elm_pb_outerloop.cxx index e5cc1939b5..31676e7e6b 100644 --- a/examples/elm-pb-outerloop/elm_pb_outerloop.cxx +++ b/examples/elm-pb-outerloop/elm_pb_outerloop.cxx @@ -1109,7 +1109,7 @@ class ELMpb : public PhysicsModel { metric->g_13 = I * Rxy * Rxy; metric->g_23 = Btxy * hthe * Rxy / Bpxy; - metric->geometry(); // Calculate quantities from metric tensor + metric->calculateGeometry(); // Calculate quantities from metric tensor // Set B field vector diff --git a/examples/elm-pb/elm_pb.cxx b/examples/elm-pb/elm_pb.cxx index c07e669c22..e82c51a79f 100644 --- a/examples/elm-pb/elm_pb.cxx +++ b/examples/elm-pb/elm_pb.cxx @@ -1050,7 +1050,7 @@ class ELMpb : public PhysicsModel { metric->g_13 = I * Rxy * Rxy; metric->g_23 = Btxy * hthe * Rxy / Bpxy; - metric->geometry(); // Calculate quantities from metric tensor + metric->calculateGeometry(); // Calculate quantities from metric tensor // Set B field vector diff --git a/examples/gyro-gem/gem.cxx b/examples/gyro-gem/gem.cxx index e0481a401e..69b14dbce9 100644 --- a/examples/gyro-gem/gem.cxx +++ b/examples/gyro-gem/gem.cxx @@ -390,7 +390,7 @@ class GEM : public PhysicsModel { g_23 = Btxy * hthe * Rxy / Bpxy; coord->setCovariantMetricTensor(MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); - coord->geometry(); + coord->calculateGeometry(); // Set B field vector diff --git a/examples/jorek-compare/jorek_compare.cxx b/examples/jorek-compare/jorek_compare.cxx index aa6f7d17eb..7890c477c2 100644 --- a/examples/jorek-compare/jorek_compare.cxx +++ b/examples/jorek-compare/jorek_compare.cxx @@ -320,7 +320,7 @@ class Jorek : public PhysicsModel { g_23 = Btxy * hthe * Rxy / Bpxy; coord->setCovariantMetricTensor(MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); - coord->geometry(); // Calculate quantities from metric tensor + coord->calculateGeometry(); // Calculate quantities from metric tensor // Set B field vector B0vec.covariant = false; diff --git a/examples/lapd-drift/lapd_drift.cxx b/examples/lapd-drift/lapd_drift.cxx index 3542b1bf76..d08e644cd6 100644 --- a/examples/lapd-drift/lapd_drift.cxx +++ b/examples/lapd-drift/lapd_drift.cxx @@ -352,7 +352,7 @@ class LAPDdrift : public PhysicsModel { g_23 = Btxy * hthe * Rxy / Bpxy; coord->setCovariantMetricTensor(MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); - coord->geometry(); + coord->calculateGeometry(); rho0 = Ni0 * Delp2(phi0) + Perp_Grad_dot_Grad(phi0, Ni0); diff --git a/examples/laplacexy/alfven-wave/alfven.cxx b/examples/laplacexy/alfven-wave/alfven.cxx index b2507b9ee1..50e8c8fe06 100644 --- a/examples/laplacexy/alfven-wave/alfven.cxx +++ b/examples/laplacexy/alfven-wave/alfven.cxx @@ -222,7 +222,7 @@ class Alfven : public PhysicsModel { g_23 = sbp * Btxy * hthe * Rxy / Bpxy; coord->setCovariantMetricTensor(MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); - coord->geometry(); + coord->calculateGeometry(); } }; diff --git a/examples/laplacexy/laplace_perp/test.cxx b/examples/laplacexy/laplace_perp/test.cxx index 8fd5c71c39..a7932c5885 100644 --- a/examples/laplacexy/laplace_perp/test.cxx +++ b/examples/laplacexy/laplace_perp/test.cxx @@ -46,7 +46,7 @@ int main(int argc, char** argv) { g_23 = Btxy * hthe * Rxy / Bpxy; coord->setCovariantMetricTensor(MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); - coord->geometry(); + coord->calculateGeometry(); } /////////////////////////////////////// diff --git a/examples/reconnect-2field/2field.cxx b/examples/reconnect-2field/2field.cxx index aef6a3eb49..5e830ada78 100644 --- a/examples/reconnect-2field/2field.cxx +++ b/examples/reconnect-2field/2field.cxx @@ -183,7 +183,7 @@ class TwoField : public PhysicsModel { g_23 = Btxy * hthe * Rxy / Bpxy; coord->setCovariantMetricTensor(MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); - coord->geometry(); + coord->calculateGeometry(); // Tell BOUT++ which variables to evolve SOLVE_FOR(U, Apar); diff --git a/examples/shear-alfven-wave/2fluid.cxx b/examples/shear-alfven-wave/2fluid.cxx index c137a828ca..293f1728b8 100644 --- a/examples/shear-alfven-wave/2fluid.cxx +++ b/examples/shear-alfven-wave/2fluid.cxx @@ -202,7 +202,7 @@ class ShearAlfven : public PhysicsModel { g_23 = Btxy * hthe * Rxy / Bpxy; coord->setCovariantMetricTensor(MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); - coord->geometry(); + coord->calculateGeometry(); /**************** SET EVOLVING VARIABLES *************/ diff --git a/examples/uedge-benchmark/ue_bmark.cxx b/examples/uedge-benchmark/ue_bmark.cxx index 32ee703385..93cb853193 100644 --- a/examples/uedge-benchmark/ue_bmark.cxx +++ b/examples/uedge-benchmark/ue_bmark.cxx @@ -160,7 +160,7 @@ class UedgeBenchmark : public PhysicsModel { g_23 = Btxy * hthe * Rxy / Bpxy; coords->setCovariantMetricTensor(MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); - coords->geometry(); // Calculate other metrics + coords->calculateGeometry(); // Calculate other metrics //////////////// BOUNDARIES /////////////////////// // diff --git a/examples/wave-slab/wave_slab.cxx b/examples/wave-slab/wave_slab.cxx index 9893e88b20..eb1851d69d 100644 --- a/examples/wave-slab/wave_slab.cxx +++ b/examples/wave-slab/wave_slab.cxx @@ -51,7 +51,7 @@ class WaveTest : public PhysicsModel { g_23 = Btxy * hthe * Rxy / Bpxy; coords->setCovariantMetricTensor(MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); - coords->geometry(); + coords->calculateGeometry(); solver->add(f, "f"); solver->add(g, "g"); diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index 0b2085ffb6..0f0f101522 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -137,13 +137,13 @@ public: FieldMetric IntShiftTorsion; ///< Integrated shear (I in BOUT notation) /// Calculate differential geometry quantities from the metric tensor - int geometry(bool recalculate_staggered = true, - bool force_interpolate_from_centre = false); + int calculateGeometry(bool recalculate_staggered = true, + bool force_interpolate_from_centre = false); /// Invert contravariant metric to get covariant components void calcCovariant(const std::string& region = "RGN_ALL"); /// Invert covariant metric to get contravariant components void calcContravariant(const std::string& region = "RGN_ALL"); - void jacobian(); ///< Calculate J and Bxy + void jacobian(); ///< Calculate J and Bxy void CalculateChristoffelSymbols(); /// Calculate Christoffel symbol terms /////////////////////////////////////////////////////////// @@ -252,7 +252,7 @@ private: std::unique_ptr transform{nullptr}; /// Cache variable for `zlength`. Invalidated when - /// `Coordinates::geometry` is called + /// `Coordinates::calculateGeometry` is called mutable std::unique_ptr zlength_cache{nullptr}; /// Cache variable for Grad2_par2 diff --git a/include/bout/mesh.hxx b/include/bout/mesh.hxx index e3b447e8fc..c9f3b45ef9 100644 --- a/include/bout/mesh.hxx +++ b/include/bout/mesh.hxx @@ -632,7 +632,7 @@ public: // (circular dependency between Mesh and Coordinates) auto inserted = coords_map.emplace(location, nullptr); inserted.first->second = createDefaultCoordinates(location); - inserted.first->second->geometry(false); + inserted.first->second->calculateGeometry(false); return inserted.first->second; } diff --git a/manual/sphinx/developer_docs/mesh.rst b/manual/sphinx/developer_docs/mesh.rst index ff3e40d4b0..072645f3cc 100644 --- a/manual/sphinx/developer_docs/mesh.rst +++ b/manual/sphinx/developer_docs/mesh.rst @@ -293,7 +293,7 @@ other useful quantities:: FieldMetric Bxy; // Magnitude of B = nabla z times nabla x /// Calculate differential geometry quantities from the metric tensor - int geometry(); + int calculateGeometry(); // Christoffel symbol of the second kind (connection coefficients) FieldMetric G1_11, G1_22, G1_33, G1_12, G1_13; diff --git a/manual/sphinx/user_docs/new_in_v5.rst b/manual/sphinx/user_docs/new_in_v5.rst index b9eb637cd5..8d9820b55a 100644 --- a/manual/sphinx/user_docs/new_in_v5.rst +++ b/manual/sphinx/user_docs/new_in_v5.rst @@ -74,7 +74,7 @@ non-zero ``g_{xz}`` terms. FIXME WHEN COORDINATES REFACTORED ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -In order to simplify a lot of code, the call to `Coordinates::geometry`, which +In order to simplify a lot of code, the call to `Coordinates::calculateGeometry`, which calculates the connection coefficients `Coordinates::G1_11` and so on, has been moved out of the `Coordinates` constructor. This is because computing the coefficients involves derivatives which requires `Coordinates` and causes all diff --git a/src/mesh/mesh.cxx b/src/mesh/mesh.cxx index 55ae8adbff..ca5842fe19 100644 --- a/src/mesh/mesh.cxx +++ b/src/mesh/mesh.cxx @@ -777,7 +777,7 @@ void Mesh::recalculateStaggeredCoordinates() { } *coords_map[location] = std::move(*createDefaultCoordinates(location, true)); - coords_map[location]->geometry(false, true); + coords_map[location]->calculateGeometry(false, true); } } diff --git a/tests/MMS/diffusion/diffusion.cxx b/tests/MMS/diffusion/diffusion.cxx index d6a40f81c2..55bbf49b18 100644 --- a/tests/MMS/diffusion/diffusion.cxx +++ b/tests/MMS/diffusion/diffusion.cxx @@ -50,7 +50,7 @@ int Diffusion::init(bool UNUSED(restarting)) { auto covariant_metric_tensor = MetricTensor(1.1, 1.1, 1.1, 0.0, 0.0, 0.0); coord->setCovariantMetricTensor(covariant_metric_tensor); - coord->geometry(); + coord->calculateGeometry(); // Tell BOUT++ to solve N SOLVE_FOR(N); diff --git a/tests/MMS/wave-1d/wave.cxx b/tests/MMS/wave-1d/wave.cxx index fefd7fc59d..b763f66ef9 100644 --- a/tests/MMS/wave-1d/wave.cxx +++ b/tests/MMS/wave-1d/wave.cxx @@ -38,7 +38,7 @@ class Wave1D : public PhysicsModel { auto covariant_metric_tensor = MetricTensor(1.1, 1.1, 1.1, 0.0, 0.0, 0.0); coord->setCovariantMetricTensor(covariant_metric_tensor); - coord->geometry(); + coord->calculateGeometry(); g.setLocation(CELL_XLOW); // g staggered to the left of f diff --git a/tests/integrated/test-drift-instability/2fluid.cxx b/tests/integrated/test-drift-instability/2fluid.cxx index 3e38eff4be..06ef225371 100644 --- a/tests/integrated/test-drift-instability/2fluid.cxx +++ b/tests/integrated/test-drift-instability/2fluid.cxx @@ -237,7 +237,7 @@ class TwoFluid : public PhysicsModel { g_23 = Btxy * hthe * Rxy / Bpxy; coord->setCovariantMetricTensor(MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); - coord->geometry(); + coord->calculateGeometry(); /**************** SET EVOLVING VARIABLES *************/ diff --git a/tests/integrated/test-interchange-instability/2fluid.cxx b/tests/integrated/test-interchange-instability/2fluid.cxx index 9f0755f2eb..02cba35569 100644 --- a/tests/integrated/test-interchange-instability/2fluid.cxx +++ b/tests/integrated/test-interchange-instability/2fluid.cxx @@ -159,7 +159,7 @@ class Interchange : public PhysicsModel { g_23 = Btxy * hthe * Rxy / Bpxy; coord->setCovariantMetricTensor(MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); - coord->geometry(); + coord->calculateGeometry(); // Tell BOUT++ which variables to evolve SOLVE_FOR2(rho, Ni); diff --git a/tests/unit/fake_parallel_mesh.hxx b/tests/unit/fake_parallel_mesh.hxx index c648bbab9c..97529ac8fb 100644 --- a/tests/unit/fake_parallel_mesh.hxx +++ b/tests/unit/fake_parallel_mesh.hxx @@ -294,7 +294,7 @@ std::vector createFakeProcessors(int nx, int ny, int nz, int n Field2D{0.0}, Field2D{1.0}, Field2D{1.0}, Field2D{1.0}, Field2D{0.0}, Field2D{0.0}, Field2D{0.0}, Field2D{1.0}, Field2D{1.0}, Field2D{1.0}, Field2D{0.0}, Field2D{0.0}, Field2D{0.0}, Field2D{0.0}, Field2D{0.0}); - // No call to Coordinates::geometry() needed here + // No call to Coordinates::calculateGeometry() needed here static_cast(&meshes[j + i * nype])->setCoordinates(test_coords); test_coords->setParallelTransform( bout::utils::make_unique(*bout::globals::mesh)); diff --git a/tests/unit/field/test_field_factory.cxx b/tests/unit/field/test_field_factory.cxx index ca51b845b3..4c5365587e 100644 --- a/tests/unit/field/test_field_factory.cxx +++ b/tests/unit/field/test_field_factory.cxx @@ -560,7 +560,7 @@ TYPED_TEST(FieldFactoryCreationTest, CreateOnMesh) { Field2D{1.0}, Field2D{1.0}, Field2D{1.0}, Field2D{0.0}, Field2D{0.0}, Field2D{0.0}, Field2D{1.0}, Field2D{1.0}, Field2D{1.0}, Field2D{0.0}, Field2D{0.0}, Field2D{0.0}, Field2D{0.0}, Field2D{0.0})); - // No call to Coordinates::geometry() needed here + // No call to Coordinates::calculateGeometry() needed here localmesh.getCoordinates()->setParallelTransform( bout::utils::make_unique(localmesh)); diff --git a/tests/unit/field/test_vector2d.cxx b/tests/unit/field/test_vector2d.cxx index 838876c02b..e1e6360391 100644 --- a/tests/unit/field/test_vector2d.cxx +++ b/tests/unit/field/test_vector2d.cxx @@ -52,7 +52,7 @@ class Vector2DTest : public ::testing::Test { Field2D{1.0}, Field2D{2.0}, Field2D{3.0}, Field2D{4.0}, Field2D{5.0}, Field2D{6.0}, Field2D{1.0}, Field2D{2.0}, Field2D{3.0}, Field2D{4.0}, Field2D{5.0}, Field2D{6.0}, Field2D{0.0}, Field2D{0.0})); - // No call to Coordinates::geometry() needed here + // No call to Coordinates::calculateGeometry() needed here delete mesh_staggered; mesh_staggered = new FakeMesh(nx, ny, nz); diff --git a/tests/unit/field/test_vector3d.cxx b/tests/unit/field/test_vector3d.cxx index ece1f95fcc..1bf5997836 100644 --- a/tests/unit/field/test_vector3d.cxx +++ b/tests/unit/field/test_vector3d.cxx @@ -49,7 +49,7 @@ class Vector3DTest : public ::testing::Test { Field2D{1.0}, Field2D{2.0}, Field2D{3.0}, Field2D{4.0}, Field2D{5.0}, Field2D{6.0}, Field2D{1.0}, Field2D{2.0}, Field2D{3.0}, Field2D{4.0}, Field2D{5.0}, Field2D{6.0}, Field2D{0.0}, Field2D{0.0})); - // No call to Coordinates::geometry() needed here + // No call to Coordinates::calculateGeometry() needed here delete mesh_staggered; mesh_staggered = new FakeMesh(nx, ny, nz); diff --git a/tests/unit/include/bout/test_petsc_indexer.cxx b/tests/unit/include/bout/test_petsc_indexer.cxx index 3c20de9989..977e00fd3b 100644 --- a/tests/unit/include/bout/test_petsc_indexer.cxx +++ b/tests/unit/include/bout/test_petsc_indexer.cxx @@ -51,7 +51,7 @@ class IndexerTest : public FakeMeshFixture { Field2D{0.0}, Field2D{1.0}, Field2D{1.0}, Field2D{1.0}, Field2D{0.0}, Field2D{0.0}, Field2D{0.0}, Field2D{1.0}, Field2D{1.0}, Field2D{1.0}, Field2D{0.0}, Field2D{0.0}, Field2D{0.0}, Field2D{0.0}, Field2D{0.0}); - // No call to Coordinates::geometry() needed here + // No call to Coordinates::calculateGeometry() needed here mesh2.setCoordinates(test_coords); // May need a ParallelTransform to create fields, because create3D calls // fromFieldAligned diff --git a/tests/unit/invert/laplace/test_laplace_cyclic.cxx b/tests/unit/invert/laplace/test_laplace_cyclic.cxx index 709896d682..6c36154f9f 100644 --- a/tests/unit/invert/laplace/test_laplace_cyclic.cxx +++ b/tests/unit/invert/laplace/test_laplace_cyclic.cxx @@ -85,7 +85,7 @@ class CyclicTest : public FakeMeshFixture, static_cast(bout::globals::mesh) ->setGridDataSource(new GridFromOptions(Options::getRoot())); - bout::globals::mesh->getCoordinates()->geometry(); + bout::globals::mesh->getCoordinates()->calculateGeometry(); f3.allocate(); coef2.allocate(); coef3.allocate(); diff --git a/tests/unit/mesh/parallel/test_shiftedmetric.cxx b/tests/unit/mesh/parallel/test_shiftedmetric.cxx index fbeaa10c3a..ebc8ec4b8f 100644 --- a/tests/unit/mesh/parallel/test_shiftedmetric.cxx +++ b/tests/unit/mesh/parallel/test_shiftedmetric.cxx @@ -42,7 +42,7 @@ class ShiftedMetricTest : public ::testing::Test { Field2D{1.0}, Field2D{1.0}, Field2D{1.0}, Field2D{0.0}, Field2D{0.0}, Field2D{0.0}, Field2D{1.0}, Field2D{1.0}, Field2D{1.0}, Field2D{0.0}, Field2D{0.0}, Field2D{0.0}, Field2D{0.0}, Field2D{0.0})); - // No call to Coordinates::geometry() needed here + // No call to Coordinates::calculateGeometry() needed here auto coords = mesh->getCoordinates(); coords->setParallelTransform(bout::utils::make_unique( diff --git a/tests/unit/mesh/test_coordinates.cxx b/tests/unit/mesh/test_coordinates.cxx index 4d56cba5c5..23b29dc328 100644 --- a/tests/unit/mesh/test_coordinates.cxx +++ b/tests/unit/mesh/test_coordinates.cxx @@ -46,7 +46,7 @@ TEST_F(CoordinatesTest, ZLength) { FieldMetric{0.0}, // g_23 FieldMetric{0.0}, // ShiftTorsion FieldMetric{0.0}}; // IntShiftTorsion - // No call to Coordinates::geometry() needed here + // No call to Coordinates::calculateGeometry() needed here EXPECT_TRUE(IsFieldEqual(coords.zlength(), 7.0)); } @@ -79,7 +79,7 @@ TEST_F(CoordinatesTest, ZLength3D) { FieldMetric{0.0}, // g_23 FieldMetric{0.0}, // ShiftTorsion FieldMetric{0.0}}; // IntShiftTorsion - // No call to Coordinates::geometry() needed here + // No call to Coordinates::calculateGeometry() needed here EXPECT_TRUE(IsFieldEqual(coords.zlength(), expected)); } @@ -106,7 +106,7 @@ TEST_F(CoordinatesTest, Jacobian) { FieldMetric{0.0}, // g_23 FieldMetric{0.0}, // ShiftTorsion FieldMetric{0.0}}; // IntShiftTorsion - // No call to Coordinates::geometry() needed here + // No call to Coordinates::calculateGeometry() needed here EXPECT_NO_THROW(coords.jacobian()); @@ -137,7 +137,7 @@ TEST_F(CoordinatesTest, CalcContravariant) { FieldMetric{0.0}, // g_23 FieldMetric{0.0}, // ShiftTorsion FieldMetric{0.0}}; // IntShiftTorsion - // No call to Coordinates::geometry() needed here + // No call to Coordinates::calculateGeometry() needed here output_info.disable(); coords.calcCovariant(); @@ -172,7 +172,7 @@ TEST_F(CoordinatesTest, CalcCovariant) { FieldMetric{0.0}, // g_23 FieldMetric{0.0}, // ShiftTorsion FieldMetric{0.0}}; // IntShiftTorsion - // No call to Coordinates::geometry() needed here + // No call to Coordinates::calculateGeometry() needed here output_info.disable(); coords.calcContravariant(); @@ -242,7 +242,7 @@ TEST_F(CoordinatesTest, SmallMeshSpacing) { output_info.disable(); output_warn.disable(); Coordinates coords(mesh); - EXPECT_THROW(coords.geometry(), BoutException); + EXPECT_THROW(coords.calculateGeometry(), BoutException); output_warn.enable(); output_info.enable(); } diff --git a/tests/unit/mesh/test_interpolation.cxx b/tests/unit/mesh/test_interpolation.cxx index 62a23a0db1..0973dc43c9 100644 --- a/tests/unit/mesh/test_interpolation.cxx +++ b/tests/unit/mesh/test_interpolation.cxx @@ -77,7 +77,7 @@ class Field3DInterpToTest : public ::testing::Test { Field2D{0.0, mesh}, Field2D{0.0, mesh}, Field2D{0.0, mesh}, Field2D{0.0, mesh}), location); - // No call to Coordinates::geometry() needed here + // No call to Coordinates::calculateGeometry() needed here mesh->getCoordinates(location)->setParallelTransform( bout::utils::make_unique(*mesh)); } @@ -324,7 +324,7 @@ class Field2DInterpToTest : public ::testing::Test { Field2D{0.0, mesh}, Field2D{0.0, mesh}, Field2D{0.0, mesh}, Field2D{0.0, mesh}), location); - // No call to Coordinates::geometry() needed here + // No call to Coordinates::calculateGeometry() needed here mesh->getCoordinates(location)->setParallelTransform( bout::utils::make_unique(*mesh)); } diff --git a/tests/unit/test_extras.hxx b/tests/unit/test_extras.hxx index 42db21cb2c..f91a4a237b 100644 --- a/tests/unit/test_extras.hxx +++ b/tests/unit/test_extras.hxx @@ -440,7 +440,7 @@ public: Field2D{0.0}, Field2D{0.0}, Field2D{0.0}, Field2D{0.0}, Field2D{0.0}); // Set some auxilliary variables - // Usually set in geometry() + // Usually set in calculateGeometry() // Note: For testing these are set to non-zero values test_coords->G1 = test_coords->G2 = test_coords->G3 = 0.1; @@ -453,7 +453,7 @@ public: test_coords->Bxy().yup() = test_coords->Bxy().ydown() = test_coords->Bxy(); #endif - // No call to Coordinates::geometry() needed here + // No call to Coordinates::calculateGeometry() needed here static_cast(bout::globals::mesh)->setCoordinates(test_coords); static_cast(bout::globals::mesh) ->setGridDataSource(new FakeGridDataSource()); @@ -498,7 +498,7 @@ public: test_coords_staggered->Bxy; #endif - // No call to Coordinates::geometry() needed here + // No call to Coordinates::calculateGeometry() needed here test_coords_staggered->setParallelTransform( bout::utils::make_unique(*mesh_staggered)); diff --git a/tools/pylib/_boutpp_build/boutcpp.pxd.jinja b/tools/pylib/_boutpp_build/boutcpp.pxd.jinja index f66607a373..a6490e88c7 100644 --- a/tools/pylib/_boutpp_build/boutcpp.pxd.jinja +++ b/tools/pylib/_boutpp_build/boutcpp.pxd.jinja @@ -78,7 +78,7 @@ cdef extern from "bout/coordinates.hxx": {{ metric_field }} G1, G2, G3 {{ metric_field }} ShiftTorsion {{ metric_field }} IntShiftTorsion - int geometry() + int calculateGeometry() int CalculateOppositeRepresentation() int CalculateOppositeRepresentation() int jacobian() From 79212ec4ab7df2688d6680ae45aed0b2c835d4ce Mon Sep 17 00:00:00 2001 From: tomc271 Date: Thu, 23 Nov 2023 12:41:00 +0000 Subject: [PATCH 170/491] Extract class Geometry. --- CMakeLists.txt | 2 + include/bout/coordinates.hxx | 12 +- include/bout/geometry.hxx | 143 ++++ src/mesh/coordinates.cxx | 48 +- src/mesh/geometry.cxx | 1569 ++++++++++++++++++++++++++++++++++ 5 files changed, 1738 insertions(+), 36 deletions(-) create mode 100644 include/bout/geometry.hxx create mode 100644 src/mesh/geometry.cxx diff --git a/CMakeLists.txt b/CMakeLists.txt index 334481b4f6..c85a7bb242 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -356,6 +356,8 @@ set(BOUT_SOURCES ${CMAKE_CURRENT_BINARY_DIR}/include/bout/version.hxx src/mesh/metricTensor.cxx include/bout/metricTensor.hxx + include/bout/geometry.hxx + src/mesh/geometry.cxx ) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index 0f0f101522..4b386de00b 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -33,6 +33,7 @@ #ifndef BOUT_COORDINATES_H #define BOUT_COORDINATES_H +#include "geometry.hxx" #include "metricTensor.hxx" #include "bout/field2d.hxx" #include "bout/field3d.hxx" @@ -44,8 +45,6 @@ class Mesh; /*! * Represents a coordinate system, and associated operators - * - * This is a container for a collection of metric tensor components */ class Coordinates { public: @@ -247,6 +246,7 @@ private: int nz; // Size of mesh in Z. This is mesh->ngz-1 Mesh* localmesh; CELL_LOC location; + Geometry geometry; /// Handles calculation of yup and ydown std::unique_ptr transform{nullptr}; @@ -285,11 +285,11 @@ private: bool extrapolate_x = false, bool extrapolate_y = false, bool no_extra_interpolate = false, ParallelTransform* pParallelTransform = nullptr); - MetricTensor contravariantMetricTensor; - MetricTensor covariantMetricTensor; + // MetricTensor contravariantMetricTensor; + // MetricTensor covariantMetricTensor; - FieldMetric this_J; - FieldMetric this_Bxy; ///< Magnitude of B = nabla z times nabla x + // FieldMetric this_J; + // FieldMetric this_Bxy; ///< Magnitude of B = nabla z times nabla x FieldMetric recalculateJacobian(); FieldMetric recalculateBxy(); diff --git a/include/bout/geometry.hxx b/include/bout/geometry.hxx new file mode 100644 index 0000000000..4f08c874f8 --- /dev/null +++ b/include/bout/geometry.hxx @@ -0,0 +1,143 @@ +/************************************************************************** + * Describes coordinate geometry + * + * ChangeLog + * ========= + * + * 2021-11-23 Tom Chapman + * * Extracted from Coordinates + * + * + ************************************************************************** + * Copyright 2014 B.D.Dudson + * + * Contact: Ben Dudson, bd512@york.ac.uk + * + * This file is part of BOUT++. + * + * BOUT++ is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * BOUT++ is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with BOUT++. If not, see . + * + **************************************************************************/ + +#ifndef __GEOMETRY_H__ +#define __GEOMETRY_H__ + +#include "coordinates.hxx" +#include "metricTensor.hxx" +#include "bout/field2d.hxx" +#include "bout/field3d.hxx" +#include "bout/paralleltransform.hxx" +#include "bout/utils.hxx" +#include + +/*! + * Represents the geometry a coordinate system, and associated operators + * + * This is a container for a collection of metric tensor components + */ +class Geometry { + +public: +#if BOUT_USE_METRIC_3D + using FieldMetric = Field3D; +#else + using FieldMetric = Field2D; +#endif + + // Geometry(); + + Geometry( + // Coordinates& coordinates, + FieldMetric J, FieldMetric Bxy, FieldMetric g11, FieldMetric g22, FieldMetric g33, + FieldMetric g12, FieldMetric g13, FieldMetric g23, FieldMetric g_11, + FieldMetric g_22, FieldMetric g_33, FieldMetric g_12, FieldMetric g_13, + FieldMetric g_23); + + Geometry(Mesh* mesh, const CELL_LOC cell_location); + + /// Christoffel symbol of the second kind (connection coefficients) + FieldMetric G1_11, G1_22, G1_33, G1_12, G1_13, G1_23; + FieldMetric G2_11, G2_22, G2_33, G2_12, G2_13, G2_23; + FieldMetric G3_11, G3_22, G3_33, G3_12, G3_13, G3_23; + + FieldMetric G1, G2, G3; + + /// Covariant metric tensor + const FieldMetric& g_11() const; + const FieldMetric& g_22() const; + const FieldMetric& g_33() const; + const FieldMetric& g_12() const; + const FieldMetric& g_13() const; + const FieldMetric& g_23() const; + + /// Contravariant metric tensor (g^{ij}) + const FieldMetric& g11() const; + const FieldMetric& g22() const; + const FieldMetric& g33() const; + const FieldMetric& g12() const; + const FieldMetric& g13() const; + const FieldMetric& g23() const; + + ///< Coordinate system Jacobian, so volume of cell is J*dx*dy*dz + const FieldMetric& J() const; + + ///< Magnitude of B = nabla z times nabla x + const FieldMetric& Bxy() const; + + void setContravariantMetricTensor(MetricTensor metric_tensor, CELL_LOC cell_location, + Mesh* mesh, const std::string& region = "RGN_ALL"); + + void setCovariantMetricTensor(MetricTensor metric_tensor, CELL_LOC cell_location, + Mesh* mesh, const std::string& region = "RGN_ALL"); + + void setJ(FieldMetric J); + void setJ(BoutReal value, int x, int y); + + void setBxy(FieldMetric Bxy); + + void calcCovariant(const std::string& region = "RGN_ALL"); + + /// Invert covariant metric to get contravariant components + void calcContravariant(const std::string& region = "RGN_ALL"); + + void jacobian(); ///< Calculate J and Bxy + + void CalculateChristoffelSymbols(); /// Calculate Christoffel symbol terms + + // check that covariant tensors are positive (if expected) and finite (always) + void checkCovariant(int ystart); + + // check that contravariant tensors are positive (if expected) and finite (always) + void checkContravariant(int ystart); + +private: + // Coordinates& coordinates; + + // int calculateGeometry(FieldMetric& dx, FieldMetric& dy, FieldMetric& dz, + // bool recalculate_staggered, bool force_interpolate_from_centre); + + MetricTensor contravariantMetricTensor; + MetricTensor covariantMetricTensor; + + FieldMetric this_J; + FieldMetric this_Bxy; ///< Magnitude of B = nabla z times nabla x + + // FieldMetric recalculateJacobian(); + // FieldMetric recalculateBxy(); + + // template + // void communicate(T& t, Ts... ts); +}; + +#endif // __GEOMETRY_H__ diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 8181febc30..16c656394e 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -412,22 +412,18 @@ Coordinates::Coordinates(Mesh* mesh, FieldMetric dx, FieldMetric dy, FieldMetric FieldMetric IntShiftTorsion) : dx(std::move(dx)), dy(std::move(dy)), dz(dz), ShiftTorsion(std::move(ShiftTorsion)), IntShiftTorsion(std::move(IntShiftTorsion)), nz(mesh->LocalNz), localmesh(mesh), - location(CELL_CENTRE), contravariantMetricTensor(g11, g22, g33, g12, g13, g23), - covariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23), this_J(std::move(J)), - this_Bxy(std::move(Bxy)) {} + location(CELL_CENTRE), geometry(Geometry(J, Bxy, g11, g22, g33, g12, g13, g23, g_11, + g_22, g_33, g_12, g_13, g_23)) {} Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, const Coordinates* coords_in, bool force_interpolate_from_centre) : dx(1., mesh), dy(1., mesh), dz(1., mesh), d1_dx(mesh), d1_dy(mesh), d1_dz(mesh), - G1_11(mesh), G1_22(mesh), G1_33(mesh), G1_12(mesh), G1_13(mesh), G1_23(mesh), - G2_11(mesh), G2_22(mesh), G2_33(mesh), G2_12(mesh), G2_13(mesh), G2_23(mesh), - G3_11(mesh), G3_22(mesh), G3_33(mesh), G3_12(mesh), G3_13(mesh), G3_23(mesh), - G1(mesh), G2(mesh), G3(mesh), ShiftTorsion(mesh), IntShiftTorsion(mesh), - localmesh(mesh), location(loc), - contravariantMetricTensor(1., 1., 1., 0, 0, 0, mesh), - covariantMetricTensor(1., 1., 1., 0, 0, 0, mesh), - // Identity metric tensor - this_J(1., mesh), this_Bxy(1., mesh) { + // G1_11(mesh), G1_22(mesh), G1_33(mesh), G1_12(mesh), G1_13(mesh), G1_23(mesh), + // G2_11(mesh), G2_22(mesh), G2_33(mesh), G2_12(mesh), G2_13(mesh), G2_23(mesh), + // G3_11(mesh), G3_22(mesh), G3_33(mesh), G3_12(mesh), G3_13(mesh), G3_23(mesh), + // G1(mesh), G2(mesh), G3(mesh), + ShiftTorsion(mesh), IntShiftTorsion(mesh), localmesh(mesh), location(loc), + geometry(Geometry(mesh, loc)) { if (options == nullptr) { options = Options::getRoot()->getSection("mesh"); @@ -780,9 +776,9 @@ const Field2D& Coordinates::zlength() const { return *zlength_cache; } -int Coordinates::geometry(bool recalculate_staggered, - bool force_interpolate_from_centre) { - TRACE("Coordinates::geometry"); +int Coordinates::calculateGeometry(bool recalculate_staggered, + bool force_interpolate_from_centre) { + TRACE("Coordinates::calculateGeometry"); communicate(dx, dy, dz, contravariantMetricTensor.Getg11(), contravariantMetricTensor.Getg22(), contravariantMetricTensor.Getg33(), @@ -792,7 +788,7 @@ int Coordinates::geometry(bool recalculate_staggered, covariantMetricTensor.Getg12(), covariantMetricTensor.Getg13(), covariantMetricTensor.Getg23(), this_J, this_Bxy); - output_progress.write("Calculating differential geometry terms\n"); + output_progress.write("Calculating differential calculateGeometry terms\n"); if (min(abs(dx)) < 1e-8) { throw BoutException("dx magnitude less than 1e-8"); @@ -1151,13 +1147,13 @@ void Coordinates::CalculateChristoffelSymbols() { void Coordinates::calcCovariant(const std::string& region) { TRACE("Coordinates::calcCovariant"); covariantMetricTensor.setMetricTensor( - contravariantMetricTensor.oppositeRepresentation(location, region)); + contravariantMetricTensor.oppositeRepresentation(location, localmesh, region)); } void Coordinates::calcContravariant(const std::string& region) { TRACE("Coordinates::calcContravariant"); contravariantMetricTensor.setMetricTensor( - covariantMetricTensor.oppositeRepresentation(location, region)); + covariantMetricTensor.oppositeRepresentation(location, localmesh, region)); } void Coordinates::jacobian() { @@ -1172,7 +1168,6 @@ void Coordinates::jacobian() { } MetricTensor::FieldMetric Coordinates::recalculateJacobian() { - // calculate Jacobian using g^-1 = det[g^ij], J = sqrt(g) auto g = contravariantMetricTensor.Getg11() * contravariantMetricTensor.Getg22() * contravariantMetricTensor.Getg33() @@ -1200,7 +1195,6 @@ MetricTensor::FieldMetric Coordinates::recalculateJacobian() { } MetricTensor::FieldMetric Coordinates::recalculateBxy() { - const bool extrapolate_x = not localmesh->sourceHasXBoundaryGuards(); const bool extrapolate_y = not localmesh->sourceHasYBoundaryGuards(); @@ -1241,7 +1235,6 @@ void fixZShiftGuards(Field2D& zShift) { } // namespace void Coordinates::setParallelTransform(Options* options) { - auto ptoptions = options->getSection("paralleltransform"); std::string ptstr; @@ -1333,7 +1326,6 @@ Coordinates::FieldMetric Coordinates::DDX(const Field2D& f, CELL_LOC loc, } Field3D Coordinates::DDX(const Field3D& f, CELL_LOC outloc, const std::string& method, const std::string& region) { - auto result = bout::derivatives::index::DDX(f, outloc, method, region); result /= dx; @@ -1782,24 +1774,20 @@ Coordinates::Grad2_par2_DDY_invSg(CELL_LOC outloc, const std::string& method) co return *Grad2_par2_DDY_invSgCache[method]; } -void Coordinates::checkCovariant() { covariantMetricTensor.check(localmesh->ystart); } +void Coordinates::checkCovariant() { return geometry.checkCovariant(localmesh->ystart); } -void Coordinates::checkContravariant() { - contravariantMetricTensor.check(localmesh->ystart); -} +void Coordinates::checkContravariant() { geometry.checkContravariant(localmesh->ystart); } void Coordinates::setContravariantMetricTensor(MetricTensor metric_tensor, const std::string& region) { - contravariantMetricTensor.setMetricTensor(metric_tensor); - covariantMetricTensor.setMetricTensor( - contravariantMetricTensor.oppositeRepresentation(location, region)); + geometry.setContravariantMetricTensor(metric_tensor, localmesh, region); } void Coordinates::setCovariantMetricTensor(MetricTensor metric_tensor, const std::string& region) { covariantMetricTensor.setMetricTensor(metric_tensor); contravariantMetricTensor.setMetricTensor( - covariantMetricTensor.oppositeRepresentation(location, region)); + covariantMetricTensor.oppositeRepresentation(location, localmesh, region)); } const MetricTensor::FieldMetric& Coordinates::g_11() const { diff --git a/src/mesh/geometry.cxx b/src/mesh/geometry.cxx new file mode 100644 index 0000000000..8fe2ebe4fa --- /dev/null +++ b/src/mesh/geometry.cxx @@ -0,0 +1,1569 @@ +/************************************************************************** + * Differential geometry + * Calculates the covariant metric tensor, and christoffel symbol terms + * given the contravariant metric tensor terms + **************************************************************************/ + +//#include +//#include +#include +#include +//#include +//#include +//#include +// +//#include +//#include +//#include +// +//#include +// +//#include "parallel/fci.hxx" +//#include "parallel/shiftedmetricinterp.hxx" + +//// use anonymous namespace so this utility function is not available outside this file +//namespace { + +//template +//// Use sendY()/sendX() and wait() instead of Mesh::communicate() to ensure we +//// don't try to calculate parallel slices as Coordinates are not constructed yet +//void communicate(T& t, Ts... ts) { +// coordinates.communicate(t, ts); +//} + +///// Interpolate a Field2D to a new CELL_LOC with interp_to. +///// Communicates to set internal guard cells. +///// Boundary guard cells are set by extrapolating from the grid, like +///// 'free_o3' boundary conditions +///// Corner guard cells are set to BoutNaN +//const Field2D interpolateAndExtrapolate(const Field2D& f, CELL_LOC location, +// bool extrapolate_x, bool extrapolate_y, +// bool no_extra_interpolate, +// ParallelTransform* UNUSED(pt) = nullptr, +// const std::string& region = "RGN_NOBNDRY") { +// +// Mesh* localmesh = f.getMesh(); +// Field2D result = interp_to(f, location, region); +// // Ensure result's data is unique. Otherwise result might be a duplicate of +// // f (if no interpolation is needed, e.g. if interpolation is in the +// // z-direction); then f would be communicated. Since this function is used +// // on geometrical quantities that might not be periodic in y even on closed +// // field lines (due to dependence on integrated shear), we don't want to +// // communicate f. We will sort out result's boundary guard cells below, but +// // not f's so we don't want to change f. +// result.allocate(); +// communicate(result); +// +// // Extrapolate into boundaries (if requested) so that differential calculateGeometry +// // terms can be interpolated if necessary +// // Note: cannot use applyBoundary("free_o3") here because applyBoundary() +// // would try to create a new Coordinates object since we have not finished +// // initializing yet, leading to an infinite recursion. +// // Also, here we interpolate for the boundary points at xstart/ystart and +// // (xend+1)/(yend+1) instead of extrapolating. +// for (auto& bndry : localmesh->getBoundaries()) { +// if ((extrapolate_x and bndry->bx != 0) or (extrapolate_y and bndry->by != 0)) { +// int extrap_start = 0; +// if (not no_extra_interpolate) { +// // Can use no_extra_interpolate argument to skip the extra interpolation when we +// // want to extrapolate the Christoffel symbol terms which come from derivatives so +// // don't have the extra point set already +// if ((location == CELL_XLOW) && (bndry->bx > 0)) { +// extrap_start = 1; +// } else if ((location == CELL_YLOW) && (bndry->by > 0)) { +// extrap_start = 1; +// } +// } +// for (bndry->first(); !bndry->isDone(); bndry->next1d()) { +// // interpolate extra boundary point that is missed by interp_to, if +// // necessary. +// // Only interpolate this point if we are actually changing location. E.g. +// // when we use this function to extrapolate J and Bxy on staggered grids, +// // this point should already be set correctly because the metric +// // components have been interpolated to here. +// if (extrap_start > 0 and f.getLocation() != location) { +// ASSERT1(bndry->bx == 0 or localmesh->xstart > 1) +// ASSERT1(bndry->by == 0 or localmesh->ystart > 1) +// // note that either bx or by is >0 here +// result(bndry->x, bndry->y) = +// (9. +// * (f(bndry->x - bndry->bx, bndry->y - bndry->by) +// + f(bndry->x, bndry->y)) +// - f(bndry->x - 2 * bndry->bx, bndry->y - 2 * bndry->by) +// - f(bndry->x + bndry->bx, bndry->y + bndry->by)) +// / 16.; +// } +// +// // set boundary guard cells +// if ((bndry->bx != 0 && localmesh->GlobalNx - 2 * bndry->width >= 3) +// || (bndry->by != 0 +// && localmesh->GlobalNy - localmesh->numberOfYBoundaries() * bndry->width +// >= 3)) { +// if (bndry->bx != 0 && localmesh->LocalNx == 1 && bndry->width == 1) { +// throw BoutException( +// "Not enough points in the x-direction on this " +// "processor for extrapolation needed to use staggered grids. " +// "Increase number of x-guard cells MXG or decrease number of " +// "processors in the x-direction NXPE."); +// } +// if (bndry->by != 0 && localmesh->LocalNy == 1 && bndry->width == 1) { +// throw BoutException( +// "Not enough points in the y-direction on this " +// "processor for extrapolation needed to use staggered grids. " +// "Increase number of y-guard cells MYG or decrease number of " +// "processors in the y-direction NYPE."); +// } +// // extrapolate into boundary guard cells if there are enough grid points +// for (int i = extrap_start; i < bndry->width; i++) { +// int xi = bndry->x + i * bndry->bx; +// int yi = bndry->y + i * bndry->by; +// result(xi, yi) = 3.0 * result(xi - bndry->bx, yi - bndry->by) +// - 3.0 * result(xi - 2 * bndry->bx, yi - 2 * bndry->by) +// + result(xi - 3 * bndry->bx, yi - 3 * bndry->by); +// } +// } else { +// // not enough grid points to extrapolate, set equal to last grid point +// for (int i = extrap_start; i < bndry->width; i++) { +// result(bndry->x + i * bndry->bx, bndry->y + i * bndry->by) = +// result(bndry->x - bndry->bx, bndry->y - bndry->by); +// } +// } +// } +// } +// } +//#if CHECK > 0 +// if (not( +// // if include_corner_cells=true, then we extrapolate valid data into the +// // corner cells if they are not already filled +// localmesh->include_corner_cells +// +// // if we are not extrapolating at all, the corner cells should contain valid +// // data +// or (not extrapolate_x and not extrapolate_y))) { +// // Invalidate corner guard cells +// for (int i = 0; i < localmesh->xstart; i++) { +// for (int j = 0; j < localmesh->ystart; j++) { +// result(i, j) = BoutNaN; +// result(i, localmesh->LocalNy - 1 - j) = BoutNaN; +// result(localmesh->LocalNx - 1 - i, j) = BoutNaN; +// result(localmesh->LocalNx - 1 - i, localmesh->LocalNy - 1 - j) = BoutNaN; +// } +// } +// } +//#endif +// +// return result; +//} +/* + +#if BOUT_USE_METRIC_3D +Field3D interpolateAndExtrapolate(const Field3D& f_, CELL_LOC location, + bool extrapolate_x, bool extrapolate_y, + bool no_extra_interpolate, ParallelTransform* pt_) { + + Mesh* localmesh = f_.getMesh(); + Field3D result; + Field3D f = f_; + ParallelTransform* pt_f; + if (f.getCoordinates() == nullptr) { + // if input f is member of the Coordinates we are currently constructing, it will not + // have Coordinates and needs to use the passed-in ParallelTransform + pt_f = pt_; + } else { + // if input f is from Coordinates at a different location, it will have its own + // Coordinates, and we should use its ParallelTransform + pt_f = &f.getCoordinates()->getParallelTransform(); + } + if (f.getDirectionY() != YDirectionType::Standard) { + if (pt_f->canToFromFieldAligned()) { + f = pt_f->fromFieldAligned(f); + } else { + f.setDirectionY(YDirectionType::Standard); + } + } + if (location == CELL_YLOW and f.getLocation() != CELL_YLOW) { + auto f_aligned = pt_f->toFieldAligned(f, "RGN_NOX"); + result = interp_to(f_aligned, location, "RGN_NOBNDRY"); + ParallelTransform* pt_result; + if (result.getCoordinates() == nullptr) { + pt_result = pt_; + } else { + pt_result = &result.getCoordinates()->getParallelTransform(); + } + result = pt_result->fromFieldAligned(result, "RGN_NOBNDRY"); + } else { + result = interp_to(f, location, "RGN_NOBNDRY"); + } + // Ensure result's data is unique. Otherwise result might be a duplicate of + // f (if no interpolation is needed, e.g. if interpolation is in the + // z-direction); then f would be communicated. Since this function is used + // on geometrical quantities that might not be periodic in y even on closed + // field lines (due to dependence on integrated shear), we don't want to + // communicate f. We will sort out result's boundary guard cells below, but + // not f's so we don't want to change f. + result.allocate(); + communicate(result); + + // Extrapolate into boundaries (if requested) so that differential geometry + // terms can be interpolated if necessary + // Note: cannot use applyBoundary("free_o3") here because applyBoundary() + // would try to create a new Coordinates object since we have not finished + // initializing yet, leading to an infinite recursion. + // Also, here we interpolate for the boundary points at xstart/ystart and + // (xend+1)/(yend+1) instead of extrapolating. + for (auto& bndry : localmesh->getBoundaries()) { + if ((extrapolate_x and bndry->bx != 0) or (extrapolate_y and bndry->by != 0)) { + int extrap_start = 0; + if (not no_extra_interpolate) { + // Can use no_extra_interpolate argument to skip the extra interpolation when we + // want to extrapolate the Christoffel symbol terms which come from derivatives so + // don't have the extra point set already + if ((location == CELL_XLOW) && (bndry->bx > 0)) { + extrap_start = 1; + } else if ((location == CELL_YLOW) && (bndry->by > 0)) { + extrap_start = 1; + } + } + for (bndry->first(); !bndry->isDone(); bndry->next1d()) { + // interpolate extra boundary point that is missed by interp_to, if + // necessary. + // Only interpolate this point if we are actually changing location. E.g. + // when we use this function to extrapolate J and Bxy on staggered grids, + // this point should already be set correctly because the metric + // components have been interpolated to here. + if (extrap_start > 0 and f.getLocation() != location) { + ASSERT1(bndry->bx == 0 or localmesh->xstart > 1); + ASSERT1(bndry->by == 0 or localmesh->ystart > 1); + // note that either bx or by is >0 here + for (int zi = 0; zi < localmesh->LocalNz; ++zi) { + result(bndry->x, bndry->y, zi) = + (9. + * (f(bndry->x - bndry->bx, bndry->y - bndry->by, zi) + + f(bndry->x, bndry->y, zi)) + - f(bndry->x - 2 * bndry->bx, bndry->y - 2 * bndry->by, zi) + - f(bndry->x + bndry->bx, bndry->y + bndry->by, zi)) + / 16.; + } + } + // set boundary guard cells + if ((bndry->bx != 0 && localmesh->GlobalNx - 2 * bndry->width >= 3) + || (bndry->by != 0 && localmesh->GlobalNy - 2 * bndry->width >= 3)) { + if (bndry->bx != 0 && localmesh->LocalNx == 1 && bndry->width == 1) { + throw BoutException( + "Not enough points in the x-direction on this " + "processor for extrapolation needed to use staggered grids. " + "Increase number of x-guard cells MXG or decrease number of " + "processors in the x-direction NXPE."); + } + if (bndry->by != 0 && localmesh->LocalNy == 1 && bndry->width == 1) { + throw BoutException( + "Not enough points in the y-direction on this " + "processor for extrapolation needed to use staggered grids. " + "Increase number of y-guard cells MYG or decrease number of " + "processors in the y-direction NYPE."); + } + // extrapolate into boundary guard cells if there are enough grid points + for (int i = extrap_start; i < bndry->width; i++) { + int xi = bndry->x + i * bndry->bx; + int yi = bndry->y + i * bndry->by; + for (int zi = 0; zi < localmesh->LocalNz; ++zi) { + result(xi, yi, zi) = + 3.0 * result(xi - bndry->bx, yi - bndry->by, zi) + - 3.0 * result(xi - 2 * bndry->bx, yi - 2 * bndry->by, zi) + + result(xi - 3 * bndry->bx, yi - 3 * bndry->by, zi); + } + } + } else { + // not enough grid points to extrapolate, set equal to last grid point + for (int i = extrap_start; i < bndry->width; i++) { + for (int zi = 0; zi < localmesh->LocalNz; ++zi) { + result(bndry->x + i * bndry->bx, bndry->y + i * bndry->by, zi) = + result(bndry->x - bndry->bx, bndry->y - bndry->by, zi); + } + } + } + } + } + } +#if CHECK > 0 + if (not( + // if include_corner_cells=true, then we extrapolate valid data into the + // corner cells if they are not already filled + localmesh->include_corner_cells + + // if we are not extrapolating at all, the corner cells should contain valid + // data + or (not extrapolate_x and not extrapolate_y))) { + // Invalidate corner guard cells + for (int i = 0; i < localmesh->xstart; i++) { + for (int j = 0; j < localmesh->ystart; j++) { + for (int k = 0; k < localmesh->LocalNz; ++k) { + result(i, j, k) = BoutNaN; + result(i, localmesh->LocalNy - 1 - j, k) = BoutNaN; + result(localmesh->LocalNx - 1 - i, j, k) = BoutNaN; + result(localmesh->LocalNx - 1 - i, localmesh->LocalNy - 1 - j, k) = BoutNaN; + } + } + } + } +#endif // CHECK > 0 + + return result; +} +#endif // BOUT_USE_METRIC_3D +*/ +///* +// +//// If the CELL_CENTRE variable was read, the staggered version is required to +//// also exist for consistency +//void checkStaggeredGet(Mesh* mesh, const std::string& name, const std::string& suffix) { +// if (mesh->sourceHasVar(name) != mesh->sourceHasVar(name + suffix)) { +// throw BoutException("Attempting to read staggered fields from grid, but " + name +// + " is not present in both CELL_CENTRE and staggered versions."); +// } +//} +// +//// convenience function for repeated code +//int getAtLoc(Mesh* mesh, Coordinates::FieldMetric& var, const std::string& name, +// const std::string& suffix, CELL_LOC location, BoutReal default_value = 0.) { +// +// checkStaggeredGet(mesh, name, suffix); +// return mesh->get(var, name + suffix, default_value, false, location); +//} +// +//auto getAtLoc(Mesh* mesh, const std::string& name, const std::string& suffix, +// CELL_LOC location, BoutReal default_value = 0.) { +// +// checkStaggeredGet(mesh, name, suffix); +// return mesh->get(name + suffix, default_value, false, location); +//} +// +//std::string getLocationSuffix(CELL_LOC location) { +// switch (location) { +// case CELL_CENTRE: { +// return ""; +// } +// case CELL_XLOW: { +// return "_xlow"; +// } +// case CELL_YLOW: { +// return "_ylow"; +// } +// case CELL_ZLOW: { +// // in 2D metric, same as CELL_CENTRE +// return bout::build::use_metric_3d ? "_zlow" : ""; +// } +// default: { +// throw BoutException( +// "Incorrect location passed to " +// "Coordinates(Mesh*,const CELL_LOC,const Coordinates*) constructor."); +// } +// } +//} +// +//} // anonymous namespace +//*/ +// +//Coordinates::FieldMetric Coordinates::getAtLocOrUnaligned(Mesh* mesh, +// const std::string& name, +// BoutReal default_value, +// const std::string& suffix, +// CELL_LOC cell_location) { +// if (cell_location == CELL_CENTRE) { +// return getUnaligned(name, default_value); +// } +// // grid data source has staggered fields, so read instead of interpolating +// // Diagonal components of metric tensor g^{ij} (default to 1) +// return getAtLoc(mesh, name, suffix, cell_location, default_value); +//} +// +//Coordinates::FieldMetric Coordinates::getUnaligned(const std::string& name, +// BoutReal default_value) { +// +// auto field = localmesh->get(name, default_value, false); +// if (field.getDirectionY() == YDirectionType::Aligned +// and transform->canToFromFieldAligned()) { +// return transform->fromFieldAligned(field); +// } else { +// field.setDirectionY(YDirectionType::Standard); +// return field; +// } +//} +// +//Coordinates::FieldMetric Coordinates::getUnalignedAtLocationAndFillGuards( +// Mesh* mesh, const std::string& name, BoutReal default_value, +// const std::string& suffix, CELL_LOC cell_location, bool extrapolate_x, +// bool extrapolate_y, bool no_extra_interpolate, +// ParallelTransform* pParallelTransform) { +// +// auto field = getAtLocOrUnaligned(mesh, name, default_value, suffix, cell_location); +// if (suffix == "") { +// no_extra_interpolate = false; +// pParallelTransform = transform.get(); +// } +// return interpolateAndExtrapolate(field, cell_location, extrapolate_x, extrapolate_y, +// no_extra_interpolate, pParallelTransform); +//} + +//Geometry::Geometry() {} + +Geometry::Geometry( + // Coordinates& coordinates, + FieldMetric J, FieldMetric Bxy, FieldMetric g11, FieldMetric g22, FieldMetric g33, + FieldMetric g12, FieldMetric g13, FieldMetric g23, FieldMetric g_11, FieldMetric g_22, + FieldMetric g_33, FieldMetric g_12, FieldMetric g_13, FieldMetric g_23) + : // coordinates(coordinates), + contravariantMetricTensor(g11, g22, g33, g12, g13, g23), + covariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23), this_J(J), + this_Bxy(Bxy) {} + +Geometry::Geometry(Mesh* mesh, const CELL_LOC cell_location) + //bool force_interpolate_from_centre) + : G1_11(mesh), G1_22(mesh), G1_33(mesh), G1_12(mesh), G1_13(mesh), G1_23(mesh), + G2_11(mesh), G2_22(mesh), G2_33(mesh), G2_12(mesh), G2_13(mesh), G2_23(mesh), + G3_11(mesh), G3_22(mesh), G3_33(mesh), G3_12(mesh), G3_13(mesh), G3_23(mesh), + G1(mesh), G2(mesh), G3(mesh), contravariantMetricTensor(1., 1., 1., 0, 0, 0, mesh), + covariantMetricTensor(1., 1., 1., 0, 0, 0, mesh), + // Identity metric tensor + this_J(1., mesh), this_Bxy(1., mesh) {} + +// +//void Coordinates::outputVars(Options& output_options) { +// Timer time("io"); +// const std::string loc_string = +// (location == CELL_CENTRE) ? "" : "_" + toString(location); +// +// output_options["dx" + loc_string].force(dx, "Coordinates"); +// output_options["dy" + loc_string].force(dy, "Coordinates"); +// output_options["dz" + loc_string].force(dz, "Coordinates"); +// +// output_options["g11" + loc_string].force(contravariantMetricTensor.Getg11(), +// "Coordinates"); +// output_options["g22" + loc_string].force(contravariantMetricTensor.Getg22(), +// "Coordinates"); +// output_options["g33" + loc_string].force(contravariantMetricTensor.Getg33(), +// "Coordinates"); +// output_options["g12" + loc_string].force(contravariantMetricTensor.Getg12(), +// "Coordinates"); +// output_options["g13" + loc_string].force(contravariantMetricTensor.Getg13(), +// "Coordinates"); +// output_options["g23" + loc_string].force(contravariantMetricTensor.Getg23(), +// "Coordinates"); +// +// output_options["g_11" + loc_string].force(covariantMetricTensor.Getg11(), +// "Coordinates"); +// output_options["g_22" + loc_string].force(covariantMetricTensor.Getg22(), +// "Coordinates"); +// output_options["g_33" + loc_string].force(covariantMetricTensor.Getg33(), +// "Coordinates"); +// output_options["g_12" + loc_string].force(covariantMetricTensor.Getg12(), +// "Coordinates"); +// output_options["g_13" + loc_string].force(covariantMetricTensor.Getg13(), +// "Coordinates"); +// output_options["g_23" + loc_string].force(covariantMetricTensor.Getg23(), +// "Coordinates"); +// +// output_options["J" + loc_string].force(this_J, "Coordinates"); +// output_options["Bxy" + loc_string].force(this_Bxy, "Coordinates"); +// +// output_options["G1" + loc_string].force(G1, "Coordinates"); +// output_options["G2" + loc_string].force(G2, "Coordinates"); +// output_options["G3" + loc_string].force(G3, "Coordinates"); +// +// getParallelTransform().outputVars(output_options); +//} + +//const Field2D& Coordinates::zlength() const { +// BOUT_OMP(critical) +// if (not zlength_cache) { +// zlength_cache = std::make_unique(0., localmesh); +// +//#if BOUT_USE_METRIC_3D +// BOUT_FOR_SERIAL(i, dz.getRegion("RGN_ALL")) { (*zlength_cache)[i] += dz[i]; } +//#else +// (*zlength_cache) = dz * nz; +//#endif +// } +// +// return *zlength_cache; +//} + +//int Geometry::calculateGeometry(bool recalculate_staggered, bool force_interpolate_from_centre) { +// TRACE("Geometry::calculateGeometry"); +// +// communicate(dx, dy, dz, contravariantMetricTensor.Getg11(), +// contravariantMetricTensor.Getg22(), contravariantMetricTensor.Getg33(), +// contravariantMetricTensor.Getg12(), contravariantMetricTensor.Getg13(), +// contravariantMetricTensor.Getg23(), covariantMetricTensor.Getg11(), +// covariantMetricTensor.Getg22(), covariantMetricTensor.Getg33(), +// covariantMetricTensor.Getg12(), covariantMetricTensor.Getg13(), +// covariantMetricTensor.Getg23(), this_J, this_Bxy); +// +// output_progress.write("Calculating differential calculateGeometry terms\n"); +// +// if (min(abs(dx)) < 1e-8) { +// throw BoutException("dx magnitude less than 1e-8"); +// } +// +// if (min(abs(dy)) < 1e-8) { +// throw BoutException("dy magnitude less than 1e-8"); +// } +// +// if (min(abs(dz)) < 1e-8) { +// throw BoutException("dz magnitude less than 1e-8"); +// } +// +// // Check input metrics +// checkContravariant(); +// checkCovariant(); +// CalculateChristoffelSymbols(); +// +// auto tmp = this_J * contravariantMetricTensor.Getg12(); +// communicate(tmp); +// G1 = (DDX(this_J * contravariantMetricTensor.Getg11()) + DDY(tmp) +// + DDZ(this_J * contravariantMetricTensor.Getg13())) +// / this_J; +// tmp = this_J * contravariantMetricTensor.Getg22(); +// communicate(tmp); +// G2 = (DDX(this_J * contravariantMetricTensor.Getg12()) + DDY(tmp) +// + DDZ(this_J * contravariantMetricTensor.Getg23())) +// / this_J; +// tmp = this_J * contravariantMetricTensor.Getg23(); +// communicate(tmp); +// G3 = (DDX(this_J * contravariantMetricTensor.Getg13()) + DDY(tmp) +// + DDZ(this_J * contravariantMetricTensor.Getg33())) +// / this_J; +// +// // Communicate christoffel symbol terms +// output_progress.write("\tCommunicating connection terms\n"); +// +// communicate(G1_11, G1_22, G1_33, G1_12, G1_13, G1_23, G2_11, G2_22, G2_33, G2_12, G2_13, +// G2_23, G3_11, G3_22, G3_33, G3_12, G3_13, G3_23, G1, G2, G3); +// +// // Set boundary guard cells of Christoffel symbol terms +// // Ideally, when location is staggered, we would set the upper/outer boundary point +// // correctly rather than by extrapolating here: e.g. if location==CELL_YLOW and we are +// // at the upper y-boundary the x- and z-derivatives at yend+1 at the boundary can be +// // calculated because the guard cells are available, while the y-derivative could be +// // calculated from the CELL_CENTRE metric components (which have guard cells available +// // past the boundary location). This would avoid the problem that the y-boundary on the +// // CELL_YLOW grid is at a 'guard cell' location (yend+1). +// // However, the above would require lots of special handling, so just extrapolate for +// // now. +// G1_11 = interpolateAndExtrapolate(G1_11, location, true, true, true, transform.get()); +// G1_22 = interpolateAndExtrapolate(G1_22, location, true, true, true, transform.get()); +// G1_33 = interpolateAndExtrapolate(G1_33, location, true, true, true, transform.get()); +// G1_12 = interpolateAndExtrapolate(G1_12, location, true, true, true, transform.get()); +// G1_13 = interpolateAndExtrapolate(G1_13, location, true, true, true, transform.get()); +// G1_23 = interpolateAndExtrapolate(G1_23, location, true, true, true, transform.get()); +// +// G2_11 = interpolateAndExtrapolate(G2_11, location, true, true, true, transform.get()); +// G2_22 = interpolateAndExtrapolate(G2_22, location, true, true, true, transform.get()); +// G2_33 = interpolateAndExtrapolate(G2_33, location, true, true, true, transform.get()); +// G2_12 = interpolateAndExtrapolate(G2_12, location, true, true, true, transform.get()); +// G2_13 = interpolateAndExtrapolate(G2_13, location, true, true, true, transform.get()); +// G2_23 = interpolateAndExtrapolate(G2_23, location, true, true, true, transform.get()); +// +// G3_11 = interpolateAndExtrapolate(G3_11, location, true, true, true, transform.get()); +// G3_22 = interpolateAndExtrapolate(G3_22, location, true, true, true, transform.get()); +// G3_33 = interpolateAndExtrapolate(G3_33, location, true, true, true, transform.get()); +// G3_12 = interpolateAndExtrapolate(G3_12, location, true, true, true, transform.get()); +// G3_13 = interpolateAndExtrapolate(G3_13, location, true, true, true, transform.get()); +// G3_23 = interpolateAndExtrapolate(G3_23, location, true, true, true, transform.get()); +// +// G1 = interpolateAndExtrapolate(G1, location, true, true, true, transform.get()); +// G2 = interpolateAndExtrapolate(G2, location, true, true, true, transform.get()); +// G3 = interpolateAndExtrapolate(G3, location, true, true, true, transform.get()); +// // +// // ////////////////////////////////////////////////////// +// // /// Non-uniform meshes. Need to use DDX, DDY +// // +// // OPTION(Options::getRoot(), non_uniform, true); +// // +// // Coordinates::FieldMetric d2x(localmesh), d2y(localmesh), +// // d2z(localmesh); // d^2 x / d i^2 +// // +// // // Read correction for non-uniform meshes +// // std::string suffix = getLocationSuffix(location); +// // if (location == CELL_CENTRE +// // or (!force_interpolate_from_centre and localmesh->sourceHasVar("dx" + suffix))) { +// // bool extrapolate_x = not localmesh->sourceHasXBoundaryGuards(); +// // bool extrapolate_y = not localmesh->sourceHasYBoundaryGuards(); +// // +// // if (localmesh->get(d2x, "d2x" + suffix, 0.0, false, location)) { +// // output_warn.write("\tWARNING: differencing quantity 'd2x' not found. " +// // "Calculating from dx\n"); +// // d1_dx = bout::derivatives::index::DDX(1. / dx); // d/di(1/dx) +// // +// // communicate(d1_dx); +// // d1_dx = +// // interpolateAndExtrapolate(d1_dx, location, true, true, true, transform.get()); +// // } else { +// // d2x.setLocation(location); +// // // set boundary cells if necessary +// // d2x = interpolateAndExtrapolate(d2x, location, extrapolate_x, extrapolate_y, false, +// // transform.get()); +// // +// // d1_dx = -d2x / (dx * dx); +// // } +// // +// // if (localmesh->get(d2y, "d2y" + suffix, 0.0, false, location)) { +// // output_warn.write("\tWARNING: differencing quantity 'd2y' not found. " +// // "Calculating from dy\n"); +// // d1_dy = DDY(1. / dy); // d/di(1/dy) +// // +// // communicate(d1_dy); +// // d1_dy = +// // interpolateAndExtrapolate(d1_dy, location, true, true, true, transform.get()); +// // } else { +// // d2y.setLocation(location); +// // // set boundary cells if necessary +// // d2y = interpolateAndExtrapolate(d2y, location, extrapolate_x, extrapolate_y, false, +// // transform.get()); +// // +// // d1_dy = -d2y / (dy * dy); +// // } +// // +// //#if BOUT_USE_METRIC_3D +// // if (localmesh->get(d2z, "d2z" + suffix, 0.0, false)) { +// // output_warn.write("\tWARNING: differencing quantity 'd2z' not found. " +// // "Calculating from dz\n"); +// // d1_dz = bout::derivatives::index::DDZ(1. / dz); +// // communicate(d1_dz); +// // d1_dz = +// // interpolateAndExtrapolate(d1_dz, location, true, true, true, transform.get()); +// // } else { +// // d2z.setLocation(location); +// // // set boundary cells if necessary +// // d2z = interpolateAndExtrapolate(d2z, location, extrapolate_x, extrapolate_y, false, +// // transform.get()); +// // +// // d1_dz = -d2z / (dz * dz); +// // } +// //#else +// // d1_dz = 0; +// //#endif +// // } else { +// // if (localmesh->get(d2x, "d2x", 0.0, false)) { +// // output_warn.write("\tWARNING: differencing quantity 'd2x' not found. " +// // "Calculating from dx\n"); +// // d1_dx = bout::derivatives::index::DDX(1. / dx); // d/di(1/dx) +// // +// // communicate(d1_dx); +// // d1_dx = +// // interpolateAndExtrapolate(d1_dx, location, true, true, true, transform.get()); +// // } else { +// // // Shift d2x to our location +// // d2x = interpolateAndExtrapolate(d2x, location, true, true, false, transform.get()); +// // +// // d1_dx = -d2x / (dx * dx); +// // } +// // +// // if (localmesh->get(d2y, "d2y", 0.0, false)) { +// // output_warn.write("\tWARNING: differencing quantity 'd2y' not found. " +// // "Calculating from dy\n"); +// // d1_dy = DDY(1. / dy); // d/di(1/dy) +// // +// // communicate(d1_dy); +// // d1_dy = +// // interpolateAndExtrapolate(d1_dy, location, true, true, true, transform.get()); +// // } else { +// // // Shift d2y to our location +// // d2y = interpolateAndExtrapolate(d2y, location, true, true, false, transform.get()); +// // +// // d1_dy = -d2y / (dy * dy); +// // } +// // +// //#if BOUT_USE_METRIC_3D +// // if (localmesh->get(d2z, "d2z", 0.0, false)) { +// // output_warn.write("\tWARNING: differencing quantity 'd2z' not found. " +// // "Calculating from dz\n"); +// // d1_dz = bout::derivatives::index::DDZ(1. / dz); +// // +// // communicate(d1_dz); +// // d1_dz = +// // interpolateAndExtrapolate(d1_dz, location, true, true, true, transform.get()); +// // } else { +// // // Shift d2z to our location +// // d2z = interpolateAndExtrapolate(d2z, location, true, true, false, transform.get()); +// // +// // d1_dz = -d2z / (dz * dz); +// // } +// //#else +// // d1_dz = 0; +// //#endif +// // } +// // communicate(d1_dx, d1_dy, d1_dz); +// // +// // if (location == CELL_CENTRE && recalculate_staggered) { +// // // Re-calculate interpolated Coordinates at staggered locations +// // localmesh->recalculateStaggeredCoordinates(); +// // } +// // +// // // Invalidate and recalculate cached variables +// // zlength_cache.reset(); +// // Grad2_par2_DDY_invSgCache.clear(); +// // invSgCache.reset(); +// // +// // return 0; +// //} +// +// void Geometry::CalculateChristoffelSymbols() { +// // Calculate Christoffel symbol terms (18 independent values) +// // Note: This calculation is completely general: metric +// // tensor can be 2D or 3D. For 2D, all DDZ terms are zero +// +// G1_11 = 0.5 * contravariantMetricTensor.Getg11() * DDX(covariantMetricTensor.Getg11()) +// + contravariantMetricTensor.Getg12() +// * (DDX(covariantMetricTensor.Getg12()) +// - 0.5 * DDY(covariantMetricTensor.Getg11())) +// + contravariantMetricTensor.Getg13() +// * (DDX(covariantMetricTensor.Getg13()) +// - 0.5 * DDZ(covariantMetricTensor.Getg11())); +// G1_22 = +// contravariantMetricTensor.Getg11() +// * (DDY(covariantMetricTensor.Getg12()) +// - 0.5 * DDX(covariantMetricTensor.Getg22())) +// + 0.5 * contravariantMetricTensor.Getg12() * DDY(covariantMetricTensor.Getg22()) +// + contravariantMetricTensor.Getg13() +// * (DDY(covariantMetricTensor.Getg23()) +// - 0.5 * DDZ(covariantMetricTensor.Getg22())); +// G1_33 = +// contravariantMetricTensor.Getg11() +// * (DDZ(covariantMetricTensor.Getg13()) +// - 0.5 * DDX(covariantMetricTensor.Getg33())) +// + contravariantMetricTensor.Getg12() +// * (DDZ(covariantMetricTensor.Getg23()) +// - 0.5 * DDY(covariantMetricTensor.Getg33())) +// + 0.5 * contravariantMetricTensor.Getg13() * DDZ(covariantMetricTensor.Getg33()); +// G1_12 = +// 0.5 * contravariantMetricTensor.Getg11() * DDY(covariantMetricTensor.Getg11()) +// + 0.5 * contravariantMetricTensor.Getg12() * DDX(covariantMetricTensor.Getg22()) +// + 0.5 * contravariantMetricTensor.Getg13() +// * (DDY(covariantMetricTensor.Getg13()) + DDX(covariantMetricTensor.Getg23()) +// - DDZ(covariantMetricTensor.Getg12())); +// G1_13 = +// 0.5 * contravariantMetricTensor.Getg11() * DDZ(covariantMetricTensor.Getg11()) +// + 0.5 * contravariantMetricTensor.Getg12() +// * (DDZ(covariantMetricTensor.Getg12()) + DDX(covariantMetricTensor.Getg23()) +// - DDY(covariantMetricTensor.Getg13())) +// + 0.5 * contravariantMetricTensor.Getg13() * DDX(covariantMetricTensor.Getg33()); +// G1_23 = +// 0.5 * contravariantMetricTensor.Getg11() +// * (DDZ(covariantMetricTensor.Getg12()) + DDY(covariantMetricTensor.Getg13()) +// - DDX(covariantMetricTensor.Getg23())) +// + 0.5 * contravariantMetricTensor.Getg12() +// * (DDZ(covariantMetricTensor.Getg22()) + DDY(covariantMetricTensor.Getg23()) +// - DDY(covariantMetricTensor.Getg23())) +// // + 0.5 *g13*(DDZ(g_32) + DDY(g_33) - DDZ(g_23)); +// // which equals +// + 0.5 * contravariantMetricTensor.Getg13() * DDY(covariantMetricTensor.Getg33()); +// +// G2_11 = 0.5 * contravariantMetricTensor.Getg12() * DDX(covariantMetricTensor.Getg11()) +// + contravariantMetricTensor.Getg22() +// * (DDX(covariantMetricTensor.Getg12()) +// - 0.5 * DDY(covariantMetricTensor.Getg11())) +// + contravariantMetricTensor.Getg23() +// * (DDX(covariantMetricTensor.Getg13()) +// - 0.5 * DDZ(covariantMetricTensor.Getg11())); +// G2_22 = +// contravariantMetricTensor.Getg12() +// * (DDY(covariantMetricTensor.Getg12()) +// - 0.5 * DDX(covariantMetricTensor.Getg22())) +// + 0.5 * contravariantMetricTensor.Getg22() * DDY(covariantMetricTensor.Getg22()) +// + contravariantMetricTensor.Getg23() +// * (DDY(contravariantMetricTensor.Getg23()) +// - 0.5 * DDZ(covariantMetricTensor.Getg22())); +// G2_33 = +// contravariantMetricTensor.Getg12() +// * (DDZ(covariantMetricTensor.Getg13()) +// - 0.5 * DDX(covariantMetricTensor.Getg33())) +// + contravariantMetricTensor.Getg22() +// * (DDZ(covariantMetricTensor.Getg23()) +// - 0.5 * DDY(covariantMetricTensor.Getg33())) +// + 0.5 * contravariantMetricTensor.Getg23() * DDZ(covariantMetricTensor.Getg33()); +// G2_12 = +// 0.5 * contravariantMetricTensor.Getg12() * DDY(covariantMetricTensor.Getg11()) +// + 0.5 * contravariantMetricTensor.Getg22() * DDX(covariantMetricTensor.Getg22()) +// + 0.5 * contravariantMetricTensor.Getg23() +// * (DDY(covariantMetricTensor.Getg13()) + DDX(covariantMetricTensor.Getg23()) +// - DDZ(covariantMetricTensor.Getg12())); +// G2_13 = +// // 0.5 *g21*(DDZ(covariantMetricTensor.Getg11()) + DDX(covariantMetricTensor.Getg13()) - DDX(covariantMetricTensor.Getg13())) +// // which equals +// 0.5 * contravariantMetricTensor.Getg12() +// * (DDZ(covariantMetricTensor.Getg11()) + DDX(covariantMetricTensor.Getg13()) +// - DDX(covariantMetricTensor.Getg13())) +// // + 0.5 *g22*(DDZ(covariantMetricTensor.Getg21()) + DDX(covariantMetricTensor.Getg23()) - DDY(covariantMetricTensor.Getg13())) +// // which equals +// + 0.5 * contravariantMetricTensor.Getg22() +// * (DDZ(covariantMetricTensor.Getg12()) + DDX(covariantMetricTensor.Getg23()) +// - DDY(covariantMetricTensor.Getg13())) +// // + 0.5 *g23*(DDZ(covariantMetricTensor.Getg31()) + DDX(covariantMetricTensor.Getg33()) - DDZ(g_13)); +// // which equals +// + 0.5 * contravariantMetricTensor.Getg23() * DDX(covariantMetricTensor.Getg33()); +// G2_23 = +// 0.5 * contravariantMetricTensor.Getg12() +// * (DDZ(covariantMetricTensor.Getg12()) + DDY(covariantMetricTensor.Getg13()) +// - DDX(covariantMetricTensor.Getg23())) +// + 0.5 * contravariantMetricTensor.Getg22() * DDZ(covariantMetricTensor.Getg22()) +// + 0.5 * contravariantMetricTensor.Getg23() * DDY(covariantMetricTensor.Getg33()); +// +// G3_11 = 0.5 * contravariantMetricTensor.Getg13() * DDX(covariantMetricTensor.Getg11()) +// + contravariantMetricTensor.Getg23() +// * (DDX(covariantMetricTensor.Getg12()) +// - 0.5 * DDY(covariantMetricTensor.Getg11())) +// + contravariantMetricTensor.Getg33() +// * (DDX(covariantMetricTensor.Getg13()) +// - 0.5 * DDZ(covariantMetricTensor.Getg11())); +// G3_22 = +// contravariantMetricTensor.Getg13() +// * (DDY(covariantMetricTensor.Getg12()) +// - 0.5 * DDX(covariantMetricTensor.Getg22())) +// + 0.5 * contravariantMetricTensor.Getg23() * DDY(covariantMetricTensor.Getg22()) +// + contravariantMetricTensor.Getg33() +// * (DDY(covariantMetricTensor.Getg23()) +// - 0.5 * DDZ(covariantMetricTensor.Getg22())); +// G3_33 = +// contravariantMetricTensor.Getg13() +// * (DDZ(covariantMetricTensor.Getg13()) +// - 0.5 * DDX(covariantMetricTensor.Getg33())) +// + contravariantMetricTensor.Getg23() +// * (DDZ(covariantMetricTensor.Getg23()) +// - 0.5 * DDY(covariantMetricTensor.Getg33())) +// + 0.5 * contravariantMetricTensor.Getg33() * DDZ(covariantMetricTensor.Getg33()); +// G3_12 = +// // 0.5 *g31*(DDY(covariantMetricTensor.Getg11()) + DDX(covariantMetricTensor.Getg12()) - DDX(covariantMetricTensor.Getg12())) +// // which equals to +// 0.5 * contravariantMetricTensor.Getg13() * DDY(covariantMetricTensor.Getg11()) +// // + 0.5 *g32*(DDY(covariantMetricTensor.Getg21()) + DDX(covariantMetricTensor.Getg22()) - DDY(covariantMetricTensor.Getg12())) +// // which equals to +// + 0.5 * contravariantMetricTensor.Getg23() * DDX(covariantMetricTensor.Getg22()) +// //+ 0.5 *g33*(DDY(covariantMetricTensor.Getg31()) + DDX(covariantMetricTensor.Getg32()) - DDZ(covariantMetricTensor.Getg12())); +// // which equals to +// + 0.5 * contravariantMetricTensor.Getg33() +// * (DDY(covariantMetricTensor.Getg13()) + DDX(covariantMetricTensor.Getg23()) +// - DDZ(covariantMetricTensor.Getg12())); +// G3_13 = +// 0.5 * contravariantMetricTensor.Getg13() * DDZ(covariantMetricTensor.Getg11()) +// + 0.5 * contravariantMetricTensor.Getg23() +// * (DDZ(covariantMetricTensor.Getg12()) + DDX(covariantMetricTensor.Getg23()) +// - DDY(covariantMetricTensor.Getg13())) +// + 0.5 * contravariantMetricTensor.Getg33() * DDX(covariantMetricTensor.Getg33()); +// G3_23 = +// 0.5 * contravariantMetricTensor.Getg13() +// * (DDZ(covariantMetricTensor.Getg12()) + DDY(covariantMetricTensor.Getg13()) +// - DDX(covariantMetricTensor.Getg23())) +// + 0.5 * contravariantMetricTensor.Getg23() * DDZ(covariantMetricTensor.Getg22()) +// + 0.5 * contravariantMetricTensor.Getg33() * DDY(covariantMetricTensor.Getg33()); +// } +// +// void Geometry::calcCovariant(const std::string& region) { +// TRACE("Geometry::calcCovariant"); +// covariantMetricTensor.setMetricTensor( +// contravariantMetricTensor.oppositeRepresentation(location, localmesh, region)); +// } +// +// void Geometry::calcContravariant(const std::string& region) { +// TRACE("Geometry::calcContravariant"); +// contravariantMetricTensor.setMetricTensor( +// covariantMetricTensor.oppositeRepresentation(location, localmesh, region)); +// } +// +// void Geometry::jacobian() { +// TRACE("Geometry::jacobian"); +// try { +// this_J = recalculateJacobian(); +// this_Bxy = recalculateBxy(); +// } catch (BoutException&) { +// output_error.write("\tError in jacobian call\n"); +// throw; +// } +// } +// +// MetricTensor::FieldMetric Geometry::recalculateJacobian() { +// +// // calculate Jacobian using g^-1 = det[g^ij], J = sqrt(g) +// auto g = contravariantMetricTensor.Getg11() * contravariantMetricTensor.Getg22() +// * contravariantMetricTensor.Getg33() +// + 2.0 * contravariantMetricTensor.Getg12() +// * contravariantMetricTensor.Getg13() +// * contravariantMetricTensor.Getg23() +// - contravariantMetricTensor.Getg11() * contravariantMetricTensor.Getg23() +// * contravariantMetricTensor.Getg23() +// - contravariantMetricTensor.Getg22() * contravariantMetricTensor.Getg13() +// * contravariantMetricTensor.Getg13() +// - contravariantMetricTensor.Getg33() * contravariantMetricTensor.Getg12() +// * contravariantMetricTensor.Getg12(); +// +// // Check that g is positive +// bout::checkPositive(g, "The determinant of g^ij", "RGN_NOBNDRY"); +// +// const auto J = 1. / sqrt(g); +// +// // More robust to extrapolate derived quantities directly, rather than +// // deriving from extrapolated covariant metric components +// const bool extrapolate_x = not localmesh->sourceHasXBoundaryGuards(); +// const bool extrapolate_y = not localmesh->sourceHasYBoundaryGuards(); +// +// return interpolateAndExtrapolate(J, location, extrapolate_x, extrapolate_y, false, +// transform.get()); +// } +// +// MetricTensor::FieldMetric Geometry::recalculateBxy() { +// +// const bool extrapolate_x = not localmesh->sourceHasXBoundaryGuards(); +// const bool extrapolate_y = not localmesh->sourceHasYBoundaryGuards(); +// +// const auto Bxy = sqrt(covariantMetricTensor.Getg22()) / this_J; +// return interpolateAndExtrapolate(Bxy, location, extrapolate_x, extrapolate_y, false, +// transform.get()); +// } +// +// namespace { +// // Utility function for fixing up guard cells of zShift +// void fixZShiftGuards(Field2D& zShift) { +// auto localmesh = zShift.getMesh(); +// +// // extrapolate into boundary guard cells if necessary +// zShift = interpolateAndExtrapolate(zShift, zShift.getLocation(), +// not localmesh->sourceHasXBoundaryGuards(), +// not localmesh->sourceHasYBoundaryGuards(), false); +// +// // make sure zShift has been communicated +// communicate(zShift); +// +// // Correct guard cells for discontinuity of zShift at poloidal branch cut +// for (int x = 0; x < localmesh->LocalNx; x++) { +// const auto lower = localmesh->hasBranchCutLower(x); +// if (lower.first) { +// for (int y = 0; y < localmesh->ystart; y++) { +// zShift(x, y) -= lower.second; +// } +// } +// const auto upper = localmesh->hasBranchCutUpper(x); +// if (upper.first) { +// for (int y = localmesh->yend + 1; y < localmesh->LocalNy; y++) { +// zShift(x, y) += upper.second; +// } +// } +// } +// } +// } // namespace +// +// //void Coordinates::setParallelTransform(Options* options) { +// // +// // auto ptoptions = options->getSection("paralleltransform"); +// // +// // std::string ptstr; +// // ptoptions->get("type", ptstr, "identity"); +// // +// // // Convert to lower case for comparison +// // ptstr = lowercase(ptstr); +// // +// // if (ptstr == "identity") { +// // // Identity method i.e. no transform needed +// // transform = +// // bout::utils::make_unique(*localmesh, ptoptions); +// // +// // } else if (ptstr == "shifted" or ptstr == "shiftedinterp") { +// // // Shifted metric method +// // +// // Field2D zShift{localmesh}; +// // +// // // Read the zShift angle from the mesh +// // std::string suffix = getLocationSuffix(location); +// // if (localmesh->sourceHasVar("dx" + suffix)) { +// // // Grid file has variables at this location, so should be able to read +// // checkStaggeredGet(localmesh, "zShift", suffix); +// // if (localmesh->get(zShift, "zShift" + suffix, 0.0, false, location)) { +// // // No zShift variable. Try qinty in BOUT grid files +// // if (localmesh->get(zShift, "qinty" + suffix, 0.0, false, location)) { +// // // Failed to find either variable, cannot use ShiftedMetric +// // throw BoutException("Could not read zShift" + suffix + " from grid file"); +// // } +// // } +// // } else { +// // if (location == CELL_YLOW and bout::build::use_metric_3d) { +// // throw BoutException("Cannot interpolate zShift to construct ShiftedMetric when " +// // "using 3d metrics. You must provide zShift_ylow in the grid " +// // "file."); +// // } +// // Field2D zShift_centre; +// // if (localmesh->get(zShift_centre, "zShift", 0.0, false)) { +// // // No zShift variable. Try qinty in BOUT grid files +// // if (localmesh->get(zShift_centre, "qinty", 0.0, false)) { +// // // Failed to find either variable, cannot use ShiftedMetric +// // throw BoutException("Could not read zShift from grid file"); +// // } +// // } +// // +// // fixZShiftGuards(zShift_centre); +// // +// // zShift = interpolateAndExtrapolate(zShift_centre, location, true, true, false, +// // transform.get()); +// // } +// // +// // fixZShiftGuards(zShift); +// // +// // if (ptstr == "shifted") { +// // transform = bout::utils::make_unique(*localmesh, location, zShift, +// // getUniform(zlength())); +// // } else if (ptstr == "shiftedinterp") { +// // transform = bout::utils::make_unique( +// // *localmesh, location, zShift, getUniform(zlength())); +// // } +// // +// // } else if (ptstr == "fci") { +// // +// // if (location != CELL_CENTRE) { +// // throw BoutException("FCITransform is not available on staggered grids."); +// // } +// // +// // // Flux Coordinate Independent method +// // const bool fci_zperiodic = (*ptoptions)["z_periodic"].withDefault(true); +// // transform = +// // bout::utils::make_unique(*localmesh, dy, fci_zperiodic, ptoptions); +// // +// // } else { +// // throw BoutException(_("Unrecognised paralleltransform option.\n" +// // "Valid choices are 'identity', 'shifted', 'fci'")); +// // } +// //} +// +// ///******************************************************************************* +// // * Operators +// // * +// // *******************************************************************************/ +// // +// //Coordinates::FieldMetric Coordinates::DDX(const Field2D& f, CELL_LOC loc, +// // const std::string& method, +// // const std::string& region) { +// // ASSERT1(location == loc || loc == CELL_DEFAULT) +// // return bout::derivatives::index::DDX(f, loc, method, region) / dx; +// //} +// //Field3D Coordinates::DDX(const Field3D& f, CELL_LOC outloc, const std::string& method, +// // const std::string& region) { +// // +// // auto result = bout::derivatives::index::DDX(f, outloc, method, region); +// // result /= dx; +// // +// // if (f.getMesh()->IncIntShear) { +// // // Using BOUT-06 style shifting +// // result += IntShiftTorsion * DDZ(f, outloc, method, region); +// // } +// // +// // return result; +// //} +// // +// //Coordinates::FieldMetric Coordinates::DDY(const Field2D& f, CELL_LOC loc, +// // const std::string& method, +// // const std::string& region) const { +// // ASSERT1(location == loc || loc == CELL_DEFAULT) +// // return bout::derivatives::index::DDY(f, loc, method, region) / dy; +// //} +// // +// //Field3D Coordinates::DDY(const Field3D& f, CELL_LOC outloc, const std::string& method, +// // const std::string& region) const { +// //#if BOUT_USE_METRIC_3D +// // if (!f.hasParallelSlices() and !transform->canToFromFieldAligned()) { +// // Field3D f_parallel = f; +// // transform->calcParallelSlices(f_parallel); +// // f_parallel.applyParallelBoundary("parallel_neumann"); +// // return bout::derivatives::index::DDY(f_parallel, outloc, method, region); +// // } +// //#endif +// // return bout::derivatives::index::DDY(f, outloc, method, region) / dy; +// //} +// // +// //Coordinates::FieldMetric Coordinates::DDZ(const Field2D& f, CELL_LOC loc, +// // const std::string& UNUSED(method), +// // const std::string& UNUSED(region)) { +// // ASSERT1(location == loc || loc == CELL_DEFAULT) +// // ASSERT1(f.getMesh() == localmesh) +// // if (loc == CELL_DEFAULT) { +// // loc = f.getLocation(); +// // } +// // return zeroFrom(f).setLocation(loc); +// //} +// //Field3D Coordinates::DDZ(const Field3D& f, CELL_LOC outloc, const std::string& method, +// // const std::string& region) { +// // return bout::derivatives::index::DDZ(f, outloc, method, region) / dz; +// //} +// +// /////////////////////////////////////////////////////////// +// //// Parallel gradient +// // +// //Coordinates::FieldMetric Coordinates::Grad_par(const Field2D& var, +// // MAYBE_UNUSED(CELL_LOC outloc), +// // const std::string& UNUSED(method)) { +// // TRACE("Coordinates::Grad_par( Field2D )"); +// // ASSERT1(location == outloc || (outloc == CELL_DEFAULT && location == var.getLocation())) +// // +// // return DDY(var) * invSg(); +// //} +// // +// //Field3D Coordinates::Grad_par(const Field3D& var, CELL_LOC outloc, +// // const std::string& method) { +// // TRACE("Coordinates::Grad_par( Field3D )"); +// // ASSERT1(location == outloc || outloc == CELL_DEFAULT) +// // +// // return ::DDY(var, outloc, method) * invSg(); +// //} +// // +// /////////////////////////////////////////////////////////// +// //// Vpar_Grad_par +// //// vparallel times the parallel derivative along unperturbed B-field +// // +// //Coordinates::FieldMetric Coordinates::Vpar_Grad_par(const Field2D& v, const Field2D& f, +// // MAYBE_UNUSED(CELL_LOC outloc), +// // const std::string& UNUSED(method)) { +// // ASSERT1(location == outloc || (outloc == CELL_DEFAULT && location == f.getLocation())) +// // +// // return VDDY(v, f) * invSg(); +// //} +// // +// //Field3D Coordinates::Vpar_Grad_par(const Field3D& v, const Field3D& f, CELL_LOC outloc, +// // const std::string& method) { +// // ASSERT1(location == outloc || outloc == CELL_DEFAULT) +// // +// // return VDDY(v, f, outloc, method) * invSg(); +// //} +// // +// /////////////////////////////////////////////////////////// +// //// Parallel divergence +// // +// //Coordinates::FieldMetric Coordinates::Div_par(const Field2D& f, CELL_LOC outloc, +// // const std::string& method) { +// // TRACE("Coordinates::Div_par( Field2D )"); +// // ASSERT1(location == outloc || outloc == CELL_DEFAULT) +// // +// // // Need Bxy at location of f, which might be different from location of this +// // // Coordinates object +// // auto Bxy_floc = f.getCoordinates()->Bxy(); +// // +// // return this_Bxy * Grad_par(f / Bxy_floc, outloc, method); +// //} +// // +// //Field3D Coordinates::Div_par(const Field3D& f, CELL_LOC outloc, +// // const std::string& method) { +// // TRACE("Coordinates::Div_par( Field3D )"); +// // ASSERT1(location == outloc || outloc == CELL_DEFAULT) +// // +// // // Need Bxy at location of f, which might be different from location of this +// // // Coordinates object +// // auto Bxy_floc = f.getCoordinates()->Bxy(); +// // +// // if (!f.hasParallelSlices()) { +// // // No yup/ydown fields. The Grad_par operator will +// // // shift to field aligned coordinates +// // return this_Bxy * Grad_par(f / Bxy_floc, outloc, method); +// // } +// // +// // // Need to modify yup and ydown fields +// // Field3D f_B = f / Bxy_floc; +// // f_B.splitParallelSlices(); +// // for (int i = 0; i < f.getMesh()->ystart; ++i) { +// // f_B.yup(i) = f.yup(i) / Bxy_floc.yup(i); +// // f_B.ydown(i) = f.ydown(i) / Bxy_floc.ydown(i); +// // } +// // return this_Bxy * Grad_par(f_B, outloc, method); +// //} +// // +// /////////////////////////////////////////////////////////// +// //// second parallel derivative (b dot Grad)(b dot Grad) +// //// Note: For parallel Laplacian use Laplace_par +// // +// //Coordinates::FieldMetric Coordinates::Grad2_par2(const Field2D& f, CELL_LOC outloc, +// // const std::string& method) { +// // TRACE("Coordinates::Grad2_par2( Field2D )"); +// // ASSERT1(location == outloc || (outloc == CELL_DEFAULT && location == f.getLocation())) +// // +// // auto result = Grad2_par2_DDY_invSg(outloc, method) * DDY(f, outloc, method) +// // + D2DY2(f, outloc, method) / covariantMetricTensor.Getg22(); +// // +// // return result; +// //} +// // +// //Field3D Coordinates::Grad2_par2(const Field3D& f, CELL_LOC outloc, +// // const std::string& method) { +// // TRACE("Coordinates::Grad2_par2( Field3D )"); +// // if (outloc == CELL_DEFAULT) { +// // outloc = f.getLocation(); +// // } +// // ASSERT1(location == outloc) +// // +// // Field3D result = ::DDY(f, outloc, method); +// // +// // Field3D r2 = D2DY2(f, outloc, method) / covariantMetricTensor.Getg22(); +// // +// // result = Grad2_par2_DDY_invSg(outloc, method) * result + r2; +// // +// // ASSERT2(result.getLocation() == outloc) +// // +// // return result; +// //} +// // +// /////////////////////////////////////////////////////////// +// //// perpendicular Laplacian operator +// // +// //#include // Delp2 uses same coefficients as inversion code +// // +// //Coordinates::FieldMetric Coordinates::Delp2(const Field2D& f, CELL_LOC outloc, +// // bool UNUSED(useFFT)) { +// // TRACE("Coordinates::Delp2( Field2D )"); +// // ASSERT1(location == outloc || outloc == CELL_DEFAULT) +// // +// // auto result = +// // G1 * DDX(f, outloc) + contravariantMetricTensor.Getg11() * D2DX2(f, outloc); +// // +// // return result; +// //} +// // +// //Field3D Coordinates::Delp2(const Field3D& f, CELL_LOC outloc, bool useFFT) { +// // TRACE("Coordinates::Delp2( Field3D )"); +// // +// // if (outloc == CELL_DEFAULT) { +// // outloc = f.getLocation(); +// // } +// // +// // ASSERT1(location == outloc) +// // ASSERT1(f.getLocation() == outloc) +// // +// // if (localmesh->GlobalNx == 1 && localmesh->GlobalNz == 1) { +// // // copy mesh, location, etc +// // return f * 0; +// // } +// // ASSERT2(localmesh->xstart > 0) // Need at least one guard cell +// // +// // Field3D result{emptyFrom(f).setLocation(outloc)}; +// // +// // if (useFFT and not bout::build::use_metric_3d) { +// // int ncz = localmesh->LocalNz; +// // +// // // Allocate memory +// // auto ft = Matrix(localmesh->LocalNx, ncz / 2 + 1); +// // auto delft = Matrix(localmesh->LocalNx, ncz / 2 + 1); +// // +// // // Loop over y indices +// // // Note: should not include y-guard or y-boundary points here as that would +// // // use values from corner cells in dx, which may not be initialised. +// // for (int jy = localmesh->ystart; jy <= localmesh->yend; jy++) { +// // +// // // Take forward FFT +// // +// // for (int jx = 0; jx < localmesh->LocalNx; jx++) { +// // rfft(&f(jx, jy, 0), ncz, &ft(jx, 0)); +// // } +// // +// // // Loop over kz +// // for (int jz = 0; jz <= ncz / 2; jz++) { +// // +// // // No smoothing in the x direction +// // for (int jx = localmesh->xstart; jx <= localmesh->xend; jx++) { +// // // Perform x derivative +// // +// // dcomplex a, b, c; +// // laplace_tridag_coefs(jx, jy, jz, a, b, c, nullptr, nullptr, outloc); +// // +// // delft(jx, jz) = a * ft(jx - 1, jz) + b * ft(jx, jz) + c * ft(jx + 1, jz); +// // } +// // } +// // +// // // Reverse FFT +// // for (int jx = localmesh->xstart; jx <= localmesh->xend; jx++) { +// // +// // irfft(&delft(jx, 0), ncz, &result(jx, jy, 0)); +// // } +// // } +// // } else { +// // result = G1 * ::DDX(f, outloc) + G3 * ::DDZ(f, outloc) +// // + contravariantMetricTensor.Getg11() * ::D2DX2(f, outloc) +// // + contravariantMetricTensor.Getg33() * ::D2DZ2(f, outloc) +// // + 2 * contravariantMetricTensor.Getg13() * ::D2DXDZ(f, outloc); +// // } +// // +// // ASSERT2(result.getLocation() == outloc) +// // +// // return result; +// //} +// // +// //FieldPerp Coordinates::Delp2(const FieldPerp& f, CELL_LOC outloc, bool useFFT) { +// // TRACE("Coordinates::Delp2( FieldPerp )"); +// // +// // if (outloc == CELL_DEFAULT) { +// // outloc = f.getLocation(); +// // } +// // +// // ASSERT1(location == outloc) +// // ASSERT1(f.getLocation() == outloc) +// // +// // if (localmesh->GlobalNx == 1 && localmesh->GlobalNz == 1) { +// // // copy mesh, location, etc +// // return f * 0; +// // } +// // ASSERT2(localmesh->xstart > 0) // Need at least one guard cell +// // +// // FieldPerp result{emptyFrom(f).setLocation(outloc)}; +// // +// // int jy = f.getIndex(); +// // result.setIndex(jy); +// // +// // if (useFFT) { +// // int ncz = localmesh->LocalNz; +// // +// // // Allocate memory +// // auto ft = Matrix(localmesh->LocalNx, ncz / 2 + 1); +// // auto delft = Matrix(localmesh->LocalNx, ncz / 2 + 1); +// // +// // // Take forward FFT +// // for (int jx = 0; jx < localmesh->LocalNx; jx++) { +// // rfft(&f(jx, 0), ncz, &ft(jx, 0)); +// // } +// // +// // // Loop over kz +// // for (int jz = 0; jz <= ncz / 2; jz++) { +// // +// // // No smoothing in the x direction +// // for (int jx = localmesh->xstart; jx <= localmesh->xend; jx++) { +// // // Perform x derivative +// // +// // dcomplex a, b, c; +// // laplace_tridag_coefs(jx, jy, jz, a, b, c); +// // +// // delft(jx, jz) = a * ft(jx - 1, jz) + b * ft(jx, jz) + c * ft(jx + 1, jz); +// // } +// // } +// // +// // // Reverse FFT +// // for (int jx = localmesh->xstart; jx <= localmesh->xend; jx++) { +// // irfft(&delft(jx, 0), ncz, &result(jx, 0)); +// // } +// // +// // } else { +// // throw BoutException("Non-fourier Delp2 not currently implented for FieldPerp."); +// // // Would be the following but don't have standard derivative operators for FieldPerps +// // // yet +// // // result = G1 * ::DDX(f, outloc) + G3 * ::DDZ(f, outloc) + g11 * ::D2DX2(f, outloc) +// // // + g33 * ::D2DZ2(f, outloc) + 2 * g13 * ::D2DXDZ(f, outloc); +// // } +// // +// // return result; +// //} +// // +// //Coordinates::FieldMetric Coordinates::Laplace_par(const Field2D& f, CELL_LOC outloc) { +// // ASSERT1(location == outloc || outloc == CELL_DEFAULT) +// // +// // return D2DY2(f, outloc) / covariantMetricTensor.Getg22() +// // + DDY(this_J / covariantMetricTensor.Getg22(), outloc) * DDY(f, outloc) / this_J; +// //} +// // +// //Field3D Coordinates::Laplace_par(const Field3D& f, CELL_LOC outloc) { +// // ASSERT1(location == outloc || outloc == CELL_DEFAULT) +// // return D2DY2(f, outloc) / covariantMetricTensor.Getg22() +// // + DDY(this_J / covariantMetricTensor.Getg22(), outloc) * ::DDY(f, outloc) +// // / this_J; +// //} +// // +// //// Full Laplacian operator on scalar field +// // +// //Coordinates::FieldMetric Coordinates::Laplace(const Field2D& f, CELL_LOC outloc, +// // const std::string& dfdy_boundary_conditions, +// // const std::string& dfdy_dy_region) { +// // TRACE("Coordinates::Laplace( Field2D )"); +// // ASSERT1(location == outloc || outloc == CELL_DEFAULT) +// // +// // auto result = G1 * DDX(f, outloc) + G2 * DDY(f, outloc) +// // + contravariantMetricTensor.Getg11() * D2DX2(f, outloc) +// // + contravariantMetricTensor.Getg22() * D2DY2(f, outloc) +// // + 2.0 * contravariantMetricTensor.Getg12() +// // * D2DXDY(f, outloc, "DEFAULT", "RGN_NOBNDRY", +// // dfdy_boundary_conditions, dfdy_dy_region); +// // +// // return result; +// //} +// // +// //Field3D Coordinates::Laplace(const Field3D& f, CELL_LOC outloc, +// // const std::string& dfdy_boundary_conditions, +// // const std::string& dfdy_dy_region) { +// // TRACE("Coordinates::Laplace( Field3D )"); +// // ASSERT1(location == outloc || outloc == CELL_DEFAULT) +// // +// // Field3D result = G1 * ::DDX(f, outloc) + G2 * ::DDY(f, outloc) + G3 * ::DDZ(f, outloc) +// // + contravariantMetricTensor.Getg11() * D2DX2(f, outloc) +// // + contravariantMetricTensor.Getg22() * D2DY2(f, outloc) +// // + contravariantMetricTensor.Getg33() * D2DZ2(f, outloc) +// // + 2.0 +// // * (contravariantMetricTensor.Getg12() +// // * D2DXDY(f, outloc, "DEFAULT", "RGN_NOBNDRY", +// // dfdy_boundary_conditions, dfdy_dy_region) +// // + contravariantMetricTensor.Getg13() * D2DXDZ(f, outloc) +// // + contravariantMetricTensor.Getg23() * D2DYDZ(f, outloc)); +// // +// // return result; +// //} +// // +// //// Full perpendicular Laplacian, in form of inverse of Laplacian operator in LaplaceXY +// //// solver +// //Field2D Coordinates::Laplace_perpXY(MAYBE_UNUSED(const Field2D& A), +// // MAYBE_UNUSED(const Field2D& f)) { +// // TRACE("Coordinates::Laplace_perpXY( Field2D )"); +// //#if not(BOUT_USE_METRIC_3D) +// // Field2D result; +// // result.allocate(); +// // for (auto i : result.getRegion(RGN_NOBNDRY)) { +// // result[i] = 0.; +// // +// // // outer x boundary +// // const auto outer_x_avg = [&i](const auto& f) { return 0.5 * (f[i] + f[i.xp()]); }; +// // const BoutReal outer_x_A = outer_x_avg(A); +// // const BoutReal outer_x_J = outer_x_avg(this_J); +// // const BoutReal outer_x_g11 = outer_x_avg(contravariantMetricTensor.Getg11()); +// // const BoutReal outer_x_dx = outer_x_avg(dx); +// // const BoutReal outer_x_value = +// // outer_x_A * outer_x_J * outer_x_g11 / (this_J[i] * outer_x_dx * dx[i]); +// // result[i] += outer_x_value * (f[i.xp()] - f[i]); +// // +// // // inner x boundary +// // const auto inner_x_avg = [&i](const auto& f) { return 0.5 * (f[i] + f[i.xm()]); }; +// // const BoutReal inner_x_A = inner_x_avg(A); +// // const BoutReal inner_x_J = inner_x_avg(this_J); +// // const BoutReal inner_x_g11 = inner_x_avg(contravariantMetricTensor.Getg11()); +// // const BoutReal inner_x_dx = inner_x_avg(dx); +// // const BoutReal inner_x_value = +// // inner_x_A * inner_x_J * inner_x_g11 / (this_J[i] * inner_x_dx * dx[i]); +// // result[i] += inner_x_value * (f[i.xm()] - f[i]); +// // +// // // upper y boundary +// // const auto upper_y_avg = [&i](const auto& f) { return 0.5 * (f[i] + f[i.yp()]); }; +// // const BoutReal upper_y_A = upper_y_avg(A); +// // const BoutReal upper_y_J = upper_y_avg(this_J); +// // const BoutReal upper_y_g_22 = upper_y_avg(covariantMetricTensor.Getg22()); +// // const BoutReal upper_y_g23 = upper_y_avg(covariantMetricTensor.Getg23()); +// // const BoutReal upper_y_g_23 = upper_y_avg(covariantMetricTensor.Getg23()); +// // const BoutReal upper_y_dy = upper_y_avg(dy); +// // const BoutReal upper_y_value = -upper_y_A * upper_y_J * upper_y_g23 * upper_y_g_23 +// // / (upper_y_g_22 * this_J[i] * upper_y_dy * dy[i]); +// // result[i] += upper_y_value * (f[i.yp()] - f[i]); +// // +// // // lower y boundary +// // const auto lower_y_avg = [&i](const auto& f) { return 0.5 * (f[i] + f[i.ym()]); }; +// // const BoutReal lower_y_A = lower_y_avg(A); +// // const BoutReal lower_y_J = lower_y_avg(this_J); +// // const BoutReal lower_y_g_22 = lower_y_avg(covariantMetricTensor.Getg22()); +// // const BoutReal lower_y_g23 = lower_y_avg(contravariantMetricTensor.Getg23()); +// // const BoutReal lower_y_g_23 = lower_y_avg(covariantMetricTensor.Getg23()); +// // const BoutReal lower_y_dy = lower_y_avg(dy); +// // const BoutReal lower_y_value = -lower_y_A * lower_y_J * lower_y_g23 * lower_y_g_23 +// // / (lower_y_g_22 * this_J[i] * lower_y_dy * dy[i]); +// // result[i] += lower_y_value * (f[i.ym()] - f[i]); +// // } +// // +// // return result; +// //#else +// // throw BoutException("Coordinates::Laplace_perpXY for 3D metric not implemented"); +// //#endif +// //} +// // +// //const Coordinates::FieldMetric& Coordinates::invSg() const { +// // if (invSgCache == nullptr) { +// // auto ptr = std::make_unique(); +// // (*ptr) = 1.0 / sqrt(covariantMetricTensor.Getg22()); +// // invSgCache = std::move(ptr); +// // } +// // return *invSgCache; +// //} +// // +// //const Coordinates::FieldMetric& +// //Coordinates::Grad2_par2_DDY_invSg(CELL_LOC outloc, const std::string& method) const { +// // if (auto search = Grad2_par2_DDY_invSgCache.find(method); +// // search != Grad2_par2_DDY_invSgCache.end()) { +// // return *search->second; +// // } +// // invSg(); +// // +// // // Communicate to get parallel slices +// // localmesh->communicate(*invSgCache); +// // invSgCache->applyParallelBoundary("parallel_neumann"); +// // +// // // cache +// // auto ptr = std::make_unique(); +// // *ptr = DDY(*invSgCache, outloc, method) * invSg(); +// // Grad2_par2_DDY_invSgCache[method] = std::move(ptr); +// // return *Grad2_par2_DDY_invSgCache[method]; +// //} + +void Geometry::checkCovariant(int ystart) { covariantMetricTensor.check(ystart); } + +void Geometry::checkContravariant(int ystart) { contravariantMetricTensor.check(ystart); } + +void Geometry::setContravariantMetricTensor(MetricTensor metric_tensor, + CELL_LOC cell_location, Mesh* mesh, + const std::string& region) { + contravariantMetricTensor.setMetricTensor(metric_tensor); + covariantMetricTensor.setMetricTensor( + contravariantMetricTensor.oppositeRepresentation(cell_location, mesh, region)); +} + +void Geometry::setCovariantMetricTensor(MetricTensor metric_tensor, + CELL_LOC cell_location, Mesh* mesh, + covariantMetricTensor.setMetricTensor(metric_tensor); + contravariantMetricTensor.setMetricTensor( + covariantMetricTensor.oppositeRepresentation(location, localmesh, region)); +} + +const MetricTensor::FieldMetric& Geometry::g_11() const { + return covariantMetricTensor.Getg11(); +} +const MetricTensor::FieldMetric& Geometry::g_22() const { + return covariantMetricTensor.Getg22(); +} +const MetricTensor::FieldMetric& Geometry::g_33() const { + return covariantMetricTensor.Getg33(); +} +const MetricTensor::FieldMetric& Geometry::g_12() const { + return covariantMetricTensor.Getg12(); +} +const MetricTensor::FieldMetric& Geometry::g_13() const { + return covariantMetricTensor.Getg13(); +} +const MetricTensor::FieldMetric& Geometry::g_23() const { + return covariantMetricTensor.Getg23(); +} + +const MetricTensor::FieldMetric& Geometry::g11() const { + return contravariantMetricTensor.Getg11(); +} +const MetricTensor::FieldMetric& Geometry::g22() const { + return contravariantMetricTensor.Getg22(); +} +const MetricTensor::FieldMetric& Geometry::g33() const { + return contravariantMetricTensor.Getg33(); +} +const MetricTensor::FieldMetric& Geometry::g12() const { + return contravariantMetricTensor.Getg12(); +} +const MetricTensor::FieldMetric& Geometry::g13() const { + return contravariantMetricTensor.Getg13(); +} +const MetricTensor::FieldMetric& Geometry::g23() const { + return contravariantMetricTensor.Getg23(); +} +const MetricTensor::FieldMetric& Geometry::J() const { return this_J; } + +const MetricTensor::FieldMetric& Geometry::Bxy() const { return this_Bxy; } + +void Geometry::setJ(FieldMetric J) { + //TODO: Calculate J and check value is close + this_J = J; +} + +void Geometry::setJ(BoutReal value, int x, int y) { + //TODO: Calculate Bxy and check value is close + this_J(x, y) = value; +} + +void Geometry::setBxy(FieldMetric Bxy) { + //TODO: Calculate Bxy and check value is close + this_Bxy = Bxy; +} From 87719aa9cf3a22009d6cb7eab264bfaa9d8cd078 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Fri, 24 Nov 2023 13:32:33 +0000 Subject: [PATCH 171/491] Add Geometry::jacobian(), Geometry::recalculateJacobian(), and Geometry::recalculateBxy(). [Unfinished] --- include/bout/geometry.hxx | 14 ++--- src/mesh/geometry.cxx | 108 +++++++++++++++++++------------------- 2 files changed, 61 insertions(+), 61 deletions(-) diff --git a/include/bout/geometry.hxx b/include/bout/geometry.hxx index 4f08c874f8..67bdc5723a 100644 --- a/include/bout/geometry.hxx +++ b/include/bout/geometry.hxx @@ -96,20 +96,20 @@ public: const FieldMetric& Bxy() const; void setContravariantMetricTensor(MetricTensor metric_tensor, CELL_LOC cell_location, - Mesh* mesh, const std::string& region = "RGN_ALL"); + const std::string& region = "RGN_ALL"); void setCovariantMetricTensor(MetricTensor metric_tensor, CELL_LOC cell_location, - Mesh* mesh, const std::string& region = "RGN_ALL"); + const std::string& region = "RGN_ALL"); void setJ(FieldMetric J); void setJ(BoutReal value, int x, int y); void setBxy(FieldMetric Bxy); - void calcCovariant(const std::string& region = "RGN_ALL"); + // void calcCovariant(const std::string& region = "RGN_ALL"); - /// Invert covariant metric to get contravariant components - void calcContravariant(const std::string& region = "RGN_ALL"); + // /// Invert covariant metric to get contravariant components + // void calcContravariant(const std::string& region = "RGN_ALL"); void jacobian(); ///< Calculate J and Bxy @@ -133,8 +133,8 @@ private: FieldMetric this_J; FieldMetric this_Bxy; ///< Magnitude of B = nabla z times nabla x - // FieldMetric recalculateJacobian(); - // FieldMetric recalculateBxy(); + FieldMetric recalculateJacobian(bool extrapolate_x, bool extrapolate_y); + FieldMetric recalculateBxy(bool extrapolate_x, bool extrapolate_y); // template // void communicate(T& t, Ts... ts); diff --git a/src/mesh/geometry.cxx b/src/mesh/geometry.cxx index 8fe2ebe4fa..b8164acd57 100644 --- a/src/mesh/geometry.cxx +++ b/src/mesh/geometry.cxx @@ -870,56 +870,55 @@ Geometry::Geometry(Mesh* mesh, const CELL_LOC cell_location) // covariantMetricTensor.oppositeRepresentation(location, localmesh, region)); // } // -// void Geometry::jacobian() { -// TRACE("Geometry::jacobian"); -// try { -// this_J = recalculateJacobian(); -// this_Bxy = recalculateBxy(); -// } catch (BoutException&) { -// output_error.write("\tError in jacobian call\n"); -// throw; -// } -// } -// -// MetricTensor::FieldMetric Geometry::recalculateJacobian() { -// -// // calculate Jacobian using g^-1 = det[g^ij], J = sqrt(g) -// auto g = contravariantMetricTensor.Getg11() * contravariantMetricTensor.Getg22() -// * contravariantMetricTensor.Getg33() -// + 2.0 * contravariantMetricTensor.Getg12() -// * contravariantMetricTensor.Getg13() -// * contravariantMetricTensor.Getg23() -// - contravariantMetricTensor.Getg11() * contravariantMetricTensor.Getg23() -// * contravariantMetricTensor.Getg23() -// - contravariantMetricTensor.Getg22() * contravariantMetricTensor.Getg13() -// * contravariantMetricTensor.Getg13() -// - contravariantMetricTensor.Getg33() * contravariantMetricTensor.Getg12() -// * contravariantMetricTensor.Getg12(); -// -// // Check that g is positive -// bout::checkPositive(g, "The determinant of g^ij", "RGN_NOBNDRY"); -// -// const auto J = 1. / sqrt(g); -// -// // More robust to extrapolate derived quantities directly, rather than -// // deriving from extrapolated covariant metric components -// const bool extrapolate_x = not localmesh->sourceHasXBoundaryGuards(); -// const bool extrapolate_y = not localmesh->sourceHasYBoundaryGuards(); -// -// return interpolateAndExtrapolate(J, location, extrapolate_x, extrapolate_y, false, -// transform.get()); -// } -// -// MetricTensor::FieldMetric Geometry::recalculateBxy() { -// -// const bool extrapolate_x = not localmesh->sourceHasXBoundaryGuards(); -// const bool extrapolate_y = not localmesh->sourceHasYBoundaryGuards(); -// -// const auto Bxy = sqrt(covariantMetricTensor.Getg22()) / this_J; -// return interpolateAndExtrapolate(Bxy, location, extrapolate_x, extrapolate_y, false, -// transform.get()); -// } -// +void Geometry::jacobian() { + TRACE("Geometry::jacobian"); + try { + const bool extrapolate_x = not localmesh->sourceHasXBoundaryGuards(); + const bool extrapolate_y = not localmesh->sourceHasYBoundaryGuards(); + + this_J = recalculateJacobian(extrapolate_x, extrapolate_y); + this_Bxy = recalculateBxy(extrapolate_x, extrapolate_y); + } catch (BoutException&) { + output_error.write("\tError in jacobian call\n"); + throw; + } +} + +MetricTensor::FieldMetric Geometry::recalculateJacobian(bool extrapolate_x, + bool extrapolate_y) { + + // calculate Jacobian using g^-1 = det[g^ij], J = sqrt(g) + auto g = contravariantMetricTensor.Getg11() * contravariantMetricTensor.Getg22() + * contravariantMetricTensor.Getg33() + + 2.0 * contravariantMetricTensor.Getg12() * contravariantMetricTensor.Getg13() + * contravariantMetricTensor.Getg23() + - contravariantMetricTensor.Getg11() * contravariantMetricTensor.Getg23() + * contravariantMetricTensor.Getg23() + - contravariantMetricTensor.Getg22() * contravariantMetricTensor.Getg13() + * contravariantMetricTensor.Getg13() + - contravariantMetricTensor.Getg33() * contravariantMetricTensor.Getg12() + * contravariantMetricTensor.Getg12(); + + // Check that g is positive + bout::checkPositive(g, "The determinant of g^ij", "RGN_NOBNDRY"); + + const auto J = 1. / sqrt(g); + + // More robust to extrapolate derived quantities directly, rather than + // deriving from extrapolated covariant metric components + + return interpolateAndExtrapolate(J, location, extrapolate_x, extrapolate_y, false, + transform.get()); +} + +MetricTensor::FieldMetric Geometry::recalculateBxy(bool extrapolate_x, + bool extrapolate_y) { + + const auto Bxy = sqrt(covariantMetricTensor.Getg22()) / this_J; + return interpolateAndExtrapolate(Bxy, location, extrapolate_x, extrapolate_y, false, + transform.get()); +} + // namespace { // // Utility function for fixing up guard cells of zShift // void fixZShiftGuards(Field2D& zShift) { @@ -1498,18 +1497,19 @@ void Geometry::checkCovariant(int ystart) { covariantMetricTensor.check(ystart); void Geometry::checkContravariant(int ystart) { contravariantMetricTensor.check(ystart); } void Geometry::setContravariantMetricTensor(MetricTensor metric_tensor, - CELL_LOC cell_location, Mesh* mesh, + CELL_LOC cell_location, const std::string& region) { contravariantMetricTensor.setMetricTensor(metric_tensor); covariantMetricTensor.setMetricTensor( - contravariantMetricTensor.oppositeRepresentation(cell_location, mesh, region)); + contravariantMetricTensor.oppositeRepresentation(cell_location, region)); } void Geometry::setCovariantMetricTensor(MetricTensor metric_tensor, - CELL_LOC cell_location, Mesh* mesh, + CELL_LOC cell_location, + const std::string& region) { covariantMetricTensor.setMetricTensor(metric_tensor); contravariantMetricTensor.setMetricTensor( - covariantMetricTensor.oppositeRepresentation(location, localmesh, region)); + covariantMetricTensor.oppositeRepresentation(cell_location, region)); } const MetricTensor::FieldMetric& Geometry::g_11() const { From 37c90c5a6d27d15a043362a46f6fd783e5df28a1 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Fri, 24 Nov 2023 13:40:44 +0000 Subject: [PATCH 172/491] Move interpolateAndExtrapolate() to Mesh class. --- include/bout/mesh.hxx | 13 +- src/mesh/coordinates.cxx | 325 ++++++++++++++------------------------- src/mesh/mesh.cxx | 122 +++++++++++++++ 3 files changed, 247 insertions(+), 213 deletions(-) diff --git a/include/bout/mesh.hxx b/include/bout/mesh.hxx index c9f3b45ef9..547a8bfc97 100644 --- a/include/bout/mesh.hxx +++ b/include/bout/mesh.hxx @@ -199,7 +199,7 @@ public: /// /// @returns the value. Will be allocated if needed Coordinates::FieldMetric get(const std::string& name, BoutReal def = 0.0, - bool communicate = true, CELL_LOC location = CELL_DEFAULT); + bool communicate = true, CELL_LOC location = CELL_DEFAULT); /// Get a Field3D from the input source /// @@ -829,6 +829,17 @@ public: return region3D[RegionID.value()]; } + /// Interpolate a Field2D to a new CELL_LOC with interp_to. + /// Communicates to set internal guard cells. + /// Boundary guard cells are set by extrapolating from the grid, like + /// 'free_o3' boundary conditions + /// Corner guard cells are set to BoutNaN + const Field2D interpolateAndExtrapolate(const Field2D& f, CELL_LOC location, + bool extrapolate_x, bool extrapolate_y, + bool no_extra_interpolate, + ParallelTransform* UNUSED(pt) = nullptr, + const std::string& region = "RGN_NOBNDRY"); + private: /// Allocates default Coordinates objects /// By default attempts to read staggered Coordinates from grid data source, diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 16c656394e..7719c11db8 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -13,7 +13,6 @@ #include #include -#include #include @@ -33,130 +32,6 @@ void communicate(T& t, Ts... ts) { t.getMesh()->wait(h); } -/// Interpolate a Field2D to a new CELL_LOC with interp_to. -/// Communicates to set internal guard cells. -/// Boundary guard cells are set by extrapolating from the grid, like -/// 'free_o3' boundary conditions -/// Corner guard cells are set to BoutNaN -const Field2D interpolateAndExtrapolate(const Field2D& f, CELL_LOC location, - bool extrapolate_x, bool extrapolate_y, - bool no_extra_interpolate, - ParallelTransform* UNUSED(pt) = nullptr, - const std::string& region = "RGN_NOBNDRY") { - - Mesh* localmesh = f.getMesh(); - Field2D result = interp_to(f, location, region); - // Ensure result's data is unique. Otherwise result might be a duplicate of - // f (if no interpolation is needed, e.g. if interpolation is in the - // z-direction); then f would be communicated. Since this function is used - // on geometrical quantities that might not be periodic in y even on closed - // field lines (due to dependence on integrated shear), we don't want to - // communicate f. We will sort out result's boundary guard cells below, but - // not f's so we don't want to change f. - result.allocate(); - communicate(result); - - // Extrapolate into boundaries (if requested) so that differential geometry - // terms can be interpolated if necessary - // Note: cannot use applyBoundary("free_o3") here because applyBoundary() - // would try to create a new Coordinates object since we have not finished - // initializing yet, leading to an infinite recursion. - // Also, here we interpolate for the boundary points at xstart/ystart and - // (xend+1)/(yend+1) instead of extrapolating. - for (auto& bndry : localmesh->getBoundaries()) { - if ((extrapolate_x and bndry->bx != 0) or (extrapolate_y and bndry->by != 0)) { - int extrap_start = 0; - if (not no_extra_interpolate) { - // Can use no_extra_interpolate argument to skip the extra interpolation when we - // want to extrapolate the Christoffel symbol terms which come from derivatives so - // don't have the extra point set already - if ((location == CELL_XLOW) && (bndry->bx > 0)) { - extrap_start = 1; - } else if ((location == CELL_YLOW) && (bndry->by > 0)) { - extrap_start = 1; - } - } - for (bndry->first(); !bndry->isDone(); bndry->next1d()) { - // interpolate extra boundary point that is missed by interp_to, if - // necessary. - // Only interpolate this point if we are actually changing location. E.g. - // when we use this function to extrapolate J and Bxy on staggered grids, - // this point should already be set correctly because the metric - // components have been interpolated to here. - if (extrap_start > 0 and f.getLocation() != location) { - ASSERT1(bndry->bx == 0 or localmesh->xstart > 1) - ASSERT1(bndry->by == 0 or localmesh->ystart > 1) - // note that either bx or by is >0 here - result(bndry->x, bndry->y) = - (9. - * (f(bndry->x - bndry->bx, bndry->y - bndry->by) - + f(bndry->x, bndry->y)) - - f(bndry->x - 2 * bndry->bx, bndry->y - 2 * bndry->by) - - f(bndry->x + bndry->bx, bndry->y + bndry->by)) - / 16.; - } - - // set boundary guard cells - if ((bndry->bx != 0 && localmesh->GlobalNx - 2 * bndry->width >= 3) - || (bndry->by != 0 - && localmesh->GlobalNy - localmesh->numberOfYBoundaries() * bndry->width - >= 3)) { - if (bndry->bx != 0 && localmesh->LocalNx == 1 && bndry->width == 1) { - throw BoutException( - "Not enough points in the x-direction on this " - "processor for extrapolation needed to use staggered grids. " - "Increase number of x-guard cells MXG or decrease number of " - "processors in the x-direction NXPE."); - } - if (bndry->by != 0 && localmesh->LocalNy == 1 && bndry->width == 1) { - throw BoutException( - "Not enough points in the y-direction on this " - "processor for extrapolation needed to use staggered grids. " - "Increase number of y-guard cells MYG or decrease number of " - "processors in the y-direction NYPE."); - } - // extrapolate into boundary guard cells if there are enough grid points - for (int i = extrap_start; i < bndry->width; i++) { - int xi = bndry->x + i * bndry->bx; - int yi = bndry->y + i * bndry->by; - result(xi, yi) = 3.0 * result(xi - bndry->bx, yi - bndry->by) - - 3.0 * result(xi - 2 * bndry->bx, yi - 2 * bndry->by) - + result(xi - 3 * bndry->bx, yi - 3 * bndry->by); - } - } else { - // not enough grid points to extrapolate, set equal to last grid point - for (int i = extrap_start; i < bndry->width; i++) { - result(bndry->x + i * bndry->bx, bndry->y + i * bndry->by) = - result(bndry->x - bndry->bx, bndry->y - bndry->by); - } - } - } - } - } -#if CHECK > 0 - if (not( - // if include_corner_cells=true, then we extrapolate valid data into the - // corner cells if they are not already filled - localmesh->include_corner_cells - - // if we are not extrapolating at all, the corner cells should contain valid - // data - or (not extrapolate_x and not extrapolate_y))) { - // Invalidate corner guard cells - for (int i = 0; i < localmesh->xstart; i++) { - for (int j = 0; j < localmesh->ystart; j++) { - result(i, j) = BoutNaN; - result(i, localmesh->LocalNy - 1 - j) = BoutNaN; - result(localmesh->LocalNx - 1 - i, j) = BoutNaN; - result(localmesh->LocalNx - 1 - i, localmesh->LocalNy - 1 - j) = BoutNaN; - } - } - } -#endif - - return result; -} - #if BOUT_USE_METRIC_3D Field3D interpolateAndExtrapolate(const Field3D& f_, CELL_LOC location, bool extrapolate_x, bool extrapolate_y, @@ -399,8 +274,10 @@ Coordinates::FieldMetric Coordinates::getUnalignedAtLocationAndFillGuards( no_extra_interpolate = false; pParallelTransform = transform.get(); } - return interpolateAndExtrapolate(field, cell_location, extrapolate_x, extrapolate_y, - no_extra_interpolate, pParallelTransform); + + return field.getMesh()->interpolateAndExtrapolate(field, cell_location, extrapolate_x, + extrapolate_y, no_extra_interpolate, + pParallelTransform); } Coordinates::Coordinates(Mesh* mesh, FieldMetric dx, FieldMetric dy, FieldMetric dz, @@ -452,18 +329,18 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, "staggered quantity!"); } setParallelTransform(options); - dx = interpolateAndExtrapolate(coords_in->dx, location, true, true, false, - transform.get()); - dy = interpolateAndExtrapolate(coords_in->dy, location, true, true, false, - transform.get()); + dx = localmesh->interpolateAndExtrapolate(coords_in->dx, location, true, true, false, + transform.get()); + dy = localmesh->interpolateAndExtrapolate(coords_in->dy, location, true, true, false, + transform.get()); // not really needed - we have used dz already ... - dz = interpolateAndExtrapolate(coords_in->dz, location, true, true, false, - transform.get()); + dz = localmesh->interpolateAndExtrapolate(coords_in->dz, location, true, true, false, + transform.get()); std::function interpolateAndExtrapolate_function = [this](const FieldMetric component) { - return interpolateAndExtrapolate(component, location, true, true, false, - transform.get()); + return localmesh->interpolateAndExtrapolate(component, location, true, true, + false, transform.get()); }; std::basic_string region = std::basic_string("RGN_NOBNDRY"); @@ -477,22 +354,22 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, checkContravariant(); checkCovariant(); - this_J = interpolateAndExtrapolate(coords_in->J(), location, true, true, false, - transform.get()); - this_Bxy = interpolateAndExtrapolate(coords_in->Bxy(), location, true, true, false, - transform.get()); + this_J = localmesh->interpolateAndExtrapolate(coords_in->J(), location, true, true, + false, transform.get()); + this_Bxy = localmesh->interpolateAndExtrapolate(coords_in->Bxy(), location, true, + true, false, transform.get()); bout::checkFinite(this_J, "The Jacobian", "RGN_NOCORNERS"); bout::checkPositive(this_J, "The Jacobian", "RGN_NOCORNERS"); bout::checkFinite(this_Bxy, "Bxy", "RGN_NOCORNERS"); bout::checkPositive(this_Bxy, "Bxy", "RGN_NOCORNERS"); - ShiftTorsion = interpolateAndExtrapolate(coords_in->ShiftTorsion, location, true, - true, false, transform.get()); + ShiftTorsion = localmesh->interpolateAndExtrapolate( + coords_in->ShiftTorsion, location, true, true, false, transform.get()); if (mesh->IncIntShear) { - IntShiftTorsion = interpolateAndExtrapolate(coords_in->IntShiftTorsion, location, - true, true, false, transform.get()); + IntShiftTorsion = localmesh->interpolateAndExtrapolate( + coords_in->IntShiftTorsion, location, true, true, false, transform.get()); } } else { // Note: If boundary cells were not loaded from the grid file, use @@ -535,8 +412,8 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, // required early for differentiation. setParallelTransform(options); - dz = interpolateAndExtrapolate(dz, location, extrapolate_x, extrapolate_y, false, - transform.get()); + dz = localmesh->interpolateAndExtrapolate(dz, location, extrapolate_x, extrapolate_y, + false, transform.get()); dx = getUnalignedAtLocationAndFillGuards(mesh, "dx", 1.0, suffix, location, extrapolate_x, extrapolate_y, false, @@ -631,11 +508,11 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, // deriving from extrapolated covariant metric components std::function - interpolateAndExtrapolate_function = - [this, extrapolate_y, extrapolate_x](const FieldMetric component) { - return interpolateAndExtrapolate(component, location, extrapolate_x, - extrapolate_y, false, transform.get()); - }; + interpolateAndExtrapolate_function = [this, extrapolate_y, extrapolate_x]( + const FieldMetric component) { + return localmesh->interpolateAndExtrapolate( + component, location, extrapolate_x, extrapolate_y, false, transform.get()); + }; covariantMetricTensor.map(interpolateAndExtrapolate_function); // Check covariant metrics @@ -652,8 +529,8 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, suffix); this_J = Jcalc; } else { - this_J = interpolateAndExtrapolate(this_J, location, extrapolate_x, extrapolate_y, - false, transform.get()); + this_J = localmesh->interpolateAndExtrapolate( + this_J, location, extrapolate_x, extrapolate_y, false, transform.get()); // Compare calculated and loaded values output_warn.write("\tMaximum difference in J is {:e}\n", max(abs(this_J - Jcalc))); @@ -679,8 +556,8 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, suffix); this_Bxy = Bcalc; } else { - this_Bxy = interpolateAndExtrapolate(this_Bxy, location, extrapolate_x, - extrapolate_y, false, transform.get()); + this_Bxy = localmesh->interpolateAndExtrapolate( + this_Bxy, location, extrapolate_x, extrapolate_y, false, transform.get()); output_warn.write("\tMaximum difference in Bxy is {:e}\n", max(abs(this_Bxy - Bcalc))); } @@ -694,8 +571,8 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, "Derivatives may not be correct\n"); ShiftTorsion = 0.0; } - ShiftTorsion = interpolateAndExtrapolate(ShiftTorsion, location, extrapolate_x, - extrapolate_y, false, transform.get()); + ShiftTorsion = localmesh->interpolateAndExtrapolate( + ShiftTorsion, location, extrapolate_x, extrapolate_y, false, transform.get()); ////////////////////////////////////////////////////// @@ -707,8 +584,8 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, } IntShiftTorsion.setLocation(location); IntShiftTorsion = - interpolateAndExtrapolate(IntShiftTorsion, location, extrapolate_x, - extrapolate_y, false, transform.get()); + localmesh->interpolateAndExtrapolate(IntShiftTorsion, location, extrapolate_x, + extrapolate_y, false, transform.get()); } else { // IntShiftTorsion will not be used, but set to zero to avoid uninitialized field IntShiftTorsion = 0.; @@ -839,30 +716,51 @@ int Coordinates::calculateGeometry(bool recalculate_staggered, // CELL_YLOW grid is at a 'guard cell' location (yend+1). // However, the above would require lots of special handling, so just extrapolate for // now. - G1_11 = interpolateAndExtrapolate(G1_11, location, true, true, true, transform.get()); - G1_22 = interpolateAndExtrapolate(G1_22, location, true, true, true, transform.get()); - G1_33 = interpolateAndExtrapolate(G1_33, location, true, true, true, transform.get()); - G1_12 = interpolateAndExtrapolate(G1_12, location, true, true, true, transform.get()); - G1_13 = interpolateAndExtrapolate(G1_13, location, true, true, true, transform.get()); - G1_23 = interpolateAndExtrapolate(G1_23, location, true, true, true, transform.get()); - - G2_11 = interpolateAndExtrapolate(G2_11, location, true, true, true, transform.get()); - G2_22 = interpolateAndExtrapolate(G2_22, location, true, true, true, transform.get()); - G2_33 = interpolateAndExtrapolate(G2_33, location, true, true, true, transform.get()); - G2_12 = interpolateAndExtrapolate(G2_12, location, true, true, true, transform.get()); - G2_13 = interpolateAndExtrapolate(G2_13, location, true, true, true, transform.get()); - G2_23 = interpolateAndExtrapolate(G2_23, location, true, true, true, transform.get()); - - G3_11 = interpolateAndExtrapolate(G3_11, location, true, true, true, transform.get()); - G3_22 = interpolateAndExtrapolate(G3_22, location, true, true, true, transform.get()); - G3_33 = interpolateAndExtrapolate(G3_33, location, true, true, true, transform.get()); - G3_12 = interpolateAndExtrapolate(G3_12, location, true, true, true, transform.get()); - G3_13 = interpolateAndExtrapolate(G3_13, location, true, true, true, transform.get()); - G3_23 = interpolateAndExtrapolate(G3_23, location, true, true, true, transform.get()); - - G1 = interpolateAndExtrapolate(G1, location, true, true, true, transform.get()); - G2 = interpolateAndExtrapolate(G2, location, true, true, true, transform.get()); - G3 = interpolateAndExtrapolate(G3, location, true, true, true, transform.get()); + G1_11 = localmesh->interpolateAndExtrapolate(G1_11, location, true, true, true, + transform.get()); + G1_22 = localmesh->interpolateAndExtrapolate(G1_22, location, true, true, true, + transform.get()); + G1_33 = localmesh->interpolateAndExtrapolate(G1_33, location, true, true, true, + transform.get()); + G1_12 = localmesh->interpolateAndExtrapolate(G1_12, location, true, true, true, + transform.get()); + G1_13 = localmesh->interpolateAndExtrapolate(G1_13, location, true, true, true, + transform.get()); + G1_23 = localmesh->interpolateAndExtrapolate(G1_23, location, true, true, true, + transform.get()); + + G2_11 = localmesh->interpolateAndExtrapolate(G2_11, location, true, true, true, + transform.get()); + G2_22 = localmesh->interpolateAndExtrapolate(G2_22, location, true, true, true, + transform.get()); + G2_33 = localmesh->interpolateAndExtrapolate(G2_33, location, true, true, true, + transform.get()); + G2_12 = localmesh->interpolateAndExtrapolate(G2_12, location, true, true, true, + transform.get()); + G2_13 = localmesh->interpolateAndExtrapolate(G2_13, location, true, true, true, + transform.get()); + G2_23 = localmesh->interpolateAndExtrapolate(G2_23, location, true, true, true, + transform.get()); + + G3_11 = localmesh->interpolateAndExtrapolate(G3_11, location, true, true, true, + transform.get()); + G3_22 = localmesh->interpolateAndExtrapolate(G3_22, location, true, true, true, + transform.get()); + G3_33 = localmesh->interpolateAndExtrapolate(G3_33, location, true, true, true, + transform.get()); + G3_12 = localmesh->interpolateAndExtrapolate(G3_12, location, true, true, true, + transform.get()); + G3_13 = localmesh->interpolateAndExtrapolate(G3_13, location, true, true, true, + transform.get()); + G3_23 = localmesh->interpolateAndExtrapolate(G3_23, location, true, true, true, + transform.get()); + + G1 = localmesh->interpolateAndExtrapolate(G1, location, true, true, true, + transform.get()); + G2 = localmesh->interpolateAndExtrapolate(G2, location, true, true, true, + transform.get()); + G3 = localmesh->interpolateAndExtrapolate(G3, location, true, true, true, + transform.get()); ////////////////////////////////////////////////////// /// Non-uniform meshes. Need to use DDX, DDY @@ -885,13 +783,13 @@ int Coordinates::calculateGeometry(bool recalculate_staggered, d1_dx = bout::derivatives::index::DDX(1. / dx); // d/di(1/dx) communicate(d1_dx); - d1_dx = - interpolateAndExtrapolate(d1_dx, location, true, true, true, transform.get()); + d1_dx = localmesh->interpolateAndExtrapolate(d1_dx, location, true, true, true, + transform.get()); } else { d2x.setLocation(location); // set boundary cells if necessary - d2x = interpolateAndExtrapolate(d2x, location, extrapolate_x, extrapolate_y, false, - transform.get()); + d2x = localmesh->interpolateAndExtrapolate(d2x, location, extrapolate_x, + extrapolate_y, false, transform.get()); d1_dx = -d2x / (dx * dx); } @@ -902,13 +800,13 @@ int Coordinates::calculateGeometry(bool recalculate_staggered, d1_dy = DDY(1. / dy); // d/di(1/dy) communicate(d1_dy); - d1_dy = - interpolateAndExtrapolate(d1_dy, location, true, true, true, transform.get()); + d1_dy = localmesh->interpolateAndExtrapolate(d1_dy, location, true, true, true, + transform.get()); } else { d2y.setLocation(location); // set boundary cells if necessary - d2y = interpolateAndExtrapolate(d2y, location, extrapolate_x, extrapolate_y, false, - transform.get()); + d2y = localmesh->interpolateAndExtrapolate(d2y, location, extrapolate_x, + extrapolate_y, false, transform.get()); d1_dy = -d2y / (dy * dy); } @@ -919,13 +817,13 @@ int Coordinates::calculateGeometry(bool recalculate_staggered, "Calculating from dz\n"); d1_dz = bout::derivatives::index::DDZ(1. / dz); communicate(d1_dz); - d1_dz = - interpolateAndExtrapolate(d1_dz, location, true, true, true, transform.get()); + d1_dz = localmesh->interpolateAndExtrapolate(d1_dz, location, true, true, true, + transform.get()); } else { d2z.setLocation(location); // set boundary cells if necessary - d2z = interpolateAndExtrapolate(d2z, location, extrapolate_x, extrapolate_y, false, - transform.get()); + d2z = localmesh->interpolateAndExtrapolate(d2z, location, extrapolate_x, + extrapolate_y, false, transform.get()); d1_dz = -d2z / (dz * dz); } @@ -939,11 +837,12 @@ int Coordinates::calculateGeometry(bool recalculate_staggered, d1_dx = bout::derivatives::index::DDX(1. / dx); // d/di(1/dx) communicate(d1_dx); - d1_dx = - interpolateAndExtrapolate(d1_dx, location, true, true, true, transform.get()); + d1_dx = localmesh->interpolateAndExtrapolate(d1_dx, location, true, true, true, + transform.get()); } else { // Shift d2x to our location - d2x = interpolateAndExtrapolate(d2x, location, true, true, false, transform.get()); + d2x = localmesh->interpolateAndExtrapolate(d2x, location, true, true, false, + transform.get()); d1_dx = -d2x / (dx * dx); } @@ -954,11 +853,12 @@ int Coordinates::calculateGeometry(bool recalculate_staggered, d1_dy = DDY(1. / dy); // d/di(1/dy) communicate(d1_dy); - d1_dy = - interpolateAndExtrapolate(d1_dy, location, true, true, true, transform.get()); + d1_dy = localmesh->interpolateAndExtrapolate(d1_dy, location, true, true, true, + transform.get()); } else { // Shift d2y to our location - d2y = interpolateAndExtrapolate(d2y, location, true, true, false, transform.get()); + d2y = localmesh->interpolateAndExtrapolate(d2y, location, true, true, false, + transform.get()); d1_dy = -d2y / (dy * dy); } @@ -970,11 +870,12 @@ int Coordinates::calculateGeometry(bool recalculate_staggered, d1_dz = bout::derivatives::index::DDZ(1. / dz); communicate(d1_dz); - d1_dz = - interpolateAndExtrapolate(d1_dz, location, true, true, true, transform.get()); + d1_dz = localmesh->interpolateAndExtrapolate(d1_dz, location, true, true, true, + transform.get()); } else { // Shift d2z to our location - d2z = interpolateAndExtrapolate(d2z, location, true, true, false, transform.get()); + d2z = localmesh->interpolateAndExtrapolate(d2z, location, true, true, false, + transform.get()); d1_dz = -d2z / (dz * dz); } @@ -1190,8 +1091,8 @@ MetricTensor::FieldMetric Coordinates::recalculateJacobian() { const bool extrapolate_x = not localmesh->sourceHasXBoundaryGuards(); const bool extrapolate_y = not localmesh->sourceHasYBoundaryGuards(); - return interpolateAndExtrapolate(J, location, extrapolate_x, extrapolate_y, false, - transform.get()); + return localmesh->interpolateAndExtrapolate(J, location, extrapolate_x, extrapolate_y, + false, transform.get()); } MetricTensor::FieldMetric Coordinates::recalculateBxy() { @@ -1199,8 +1100,8 @@ MetricTensor::FieldMetric Coordinates::recalculateBxy() { const bool extrapolate_y = not localmesh->sourceHasYBoundaryGuards(); const auto Bxy = sqrt(covariantMetricTensor.Getg22()) / this_J; - return interpolateAndExtrapolate(Bxy, location, extrapolate_x, extrapolate_y, false, - transform.get()); + return localmesh->interpolateAndExtrapolate(Bxy, location, extrapolate_x, extrapolate_y, + false, transform.get()); } namespace { @@ -1209,9 +1110,9 @@ void fixZShiftGuards(Field2D& zShift) { auto localmesh = zShift.getMesh(); // extrapolate into boundary guard cells if necessary - zShift = interpolateAndExtrapolate(zShift, zShift.getLocation(), - not localmesh->sourceHasXBoundaryGuards(), - not localmesh->sourceHasYBoundaryGuards(), false); + zShift = localmesh->interpolateAndExtrapolate( + zShift, zShift.getLocation(), not localmesh->sourceHasXBoundaryGuards(), + not localmesh->sourceHasYBoundaryGuards(), false); // make sure zShift has been communicated communicate(zShift); @@ -1282,8 +1183,8 @@ void Coordinates::setParallelTransform(Options* options) { fixZShiftGuards(zShift_centre); - zShift = interpolateAndExtrapolate(zShift_centre, location, true, true, false, - transform.get()); + zShift = localmesh->interpolateAndExtrapolate(zShift_centre, location, true, true, + false, transform.get()); } fixZShiftGuards(zShift); diff --git a/src/mesh/mesh.cxx b/src/mesh/mesh.cxx index ca5842fe19..5efdde36ab 100644 --- a/src/mesh/mesh.cxx +++ b/src/mesh/mesh.cxx @@ -11,6 +11,128 @@ #include #include "impls/bout/boutmesh.hxx" +#include "bout/interpolation.hxx" + +/// Interpolate a Field2D to a new CELL_LOC with interp_to. +/// Communicates to set internal guard cells. +/// Boundary guard cells are set by extrapolating from the grid, like +/// 'free_o3' boundary conditions +/// Corner guard cells are set to BoutNaN +const Field2D Mesh::interpolateAndExtrapolate(const Field2D& f, CELL_LOC location, + bool extrapolate_x, bool extrapolate_y, + bool no_extra_interpolate, + ParallelTransform* UNUSED(pt), + const std::string& region) { + + Field2D result = interp_to(f, location, region); + // Ensure result's data is unique. Otherwise result might be a duplicate of + // f (if no interpolation is needed, e.g. if interpolation is in the + // z-direction); then f would be communicated. Since this function is used + // on geometrical quantities that might not be periodic in y even on closed + // field lines (due to dependence on integrated shear), we don't want to + // communicate f. We will sort out result's boundary guard cells below, but + // not f's so we don't want to change f. + result.allocate(); + communicate(result); + + // Extrapolate into boundaries (if requested) so that differential geometry + // terms can be interpolated if necessary + // Note: cannot use applyBoundary("free_o3") here because applyBoundary() + // would try to create a new Coordinates object since we have not finished + // initializing yet, leading to an infinite recursion. + // Also, here we interpolate for the boundary points at xstart/ystart and + // (xend+1)/(yend+1) instead of extrapolating. + for (auto& bndry : getBoundaries()) { + if ((extrapolate_x and bndry->bx != 0) or (extrapolate_y and bndry->by != 0)) { + int extrap_start = 0; + if (not no_extra_interpolate) { + // Can use no_extra_interpolate argument to skip the extra interpolation when we + // want to extrapolate the Christoffel symbol terms which come from derivatives so + // don't have the extra point set already + if ((location == CELL_XLOW) && (bndry->bx > 0)) { + extrap_start = 1; + } else if ((location == CELL_YLOW) && (bndry->by > 0)) { + extrap_start = 1; + } + } + for (bndry->first(); !bndry->isDone(); bndry->next1d()) { + // interpolate extra boundary point that is missed by interp_to, if + // necessary. + // Only interpolate this point if we are actually changing location. E.g. + // when we use this function to extrapolate J and Bxy on staggered grids, + // this point should already be set correctly because the metric + // components have been interpolated to here. + if (extrap_start > 0 and f.getLocation() != location) { + ASSERT1(bndry->bx == 0 or xstart > 1) + ASSERT1(bndry->by == 0 or ystart > 1) + // note that either bx or by is >0 here + result(bndry->x, bndry->y) = + (9. + * (f(bndry->x - bndry->bx, bndry->y - bndry->by) + + f(bndry->x, bndry->y)) + - f(bndry->x - 2 * bndry->bx, bndry->y - 2 * bndry->by) + - f(bndry->x + bndry->bx, bndry->y + bndry->by)) + / 16.; + } + + // set boundary guard cells + if ((bndry->bx != 0 && GlobalNx - 2 * bndry->width >= 3) + || (bndry->by != 0 && GlobalNy - numberOfYBoundaries() * bndry->width >= 3)) { + if (bndry->bx != 0 && LocalNx == 1 && bndry->width == 1) { + throw BoutException( + "Not enough points in the x-direction on this " + "processor for extrapolation needed to use staggered grids. " + "Increase number of x-guard cells MXG or decrease number of " + "processors in the x-direction NXPE."); + } + if (bndry->by != 0 && LocalNy == 1 && bndry->width == 1) { + throw BoutException( + "Not enough points in the y-direction on this " + "processor for extrapolation needed to use staggered grids. " + "Increase number of y-guard cells MYG or decrease number of " + "processors in the y-direction NYPE."); + } + // extrapolate into boundary guard cells if there are enough grid points + for (int i = extrap_start; i < bndry->width; i++) { + int xi = bndry->x + i * bndry->bx; + int yi = bndry->y + i * bndry->by; + result(xi, yi) = 3.0 * result(xi - bndry->bx, yi - bndry->by) + - 3.0 * result(xi - 2 * bndry->bx, yi - 2 * bndry->by) + + result(xi - 3 * bndry->bx, yi - 3 * bndry->by); + } + } else { + // not enough grid points to extrapolate, set equal to last grid point + for (int i = extrap_start; i < bndry->width; i++) { + result(bndry->x + i * bndry->bx, bndry->y + i * bndry->by) = + result(bndry->x - bndry->bx, bndry->y - bndry->by); + } + } + } + } + } +#if CHECK > 0 + if (not( + // if include_corner_cells=true, then we extrapolate valid data into the + // corner cells if they are not already filled + include_corner_cells + + // if we are not extrapolating at all, the corner cells should contain valid + // data + or (not extrapolate_x and not extrapolate_y))) { + // Invalidate corner guard cells + for (int i = 0; i < xstart; i++) { + for (int j = 0; j < ystart; j++) { + result(i, j) = BoutNaN; + result(i, LocalNy - 1 - j) = BoutNaN; + result(LocalNx - 1 - i, j) = BoutNaN; + result(LocalNx - 1 - i, LocalNy - 1 - j) = BoutNaN; + } + } + } +#endif + + return result; +} MeshFactory::ReturnType MeshFactory::create(Options* options, GridDataSource* source) const { From 1d0c5b9ec095da40cf49bf0556cc9c734548b940 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Fri, 24 Nov 2023 17:51:28 +0000 Subject: [PATCH 173/491] Add getContravariantMetricTensor() methods to Coordinates and Geometry classes, to allow the Coordinates constructor that interpolates from another Coordinates object to access the metric tensor for that object. Add applyToContravariantMetricTensor() and applyToCovariantMetricTensor to Geometry class, to apply a function to each component of the metric tensor. --- include/bout/coordinates.hxx | 4 +++- include/bout/geometry.hxx | 9 +++++++++ src/mesh/coordinates.cxx | 29 ++++++++++++++++------------- src/mesh/geometry.cxx | 16 ++++++++++++++++ 4 files changed, 44 insertions(+), 14 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index 4b386de00b..d25aef7686 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -135,6 +135,8 @@ public: FieldMetric IntShiftTorsion; ///< Integrated shear (I in BOUT notation) + MetricTensor& getContravariantMetricTensor() const; + /// Calculate differential geometry quantities from the metric tensor int calculateGeometry(bool recalculate_staggered = true, bool force_interpolate_from_centre = false); @@ -157,7 +159,7 @@ public: /// Return the parallel transform ParallelTransform& getParallelTransform() { - ASSERT1(transform != nullptr); + ASSERT1(transform != nullptr) return *transform; } diff --git a/include/bout/geometry.hxx b/include/bout/geometry.hxx index 67bdc5723a..39332ae8b1 100644 --- a/include/bout/geometry.hxx +++ b/include/bout/geometry.hxx @@ -89,6 +89,9 @@ public: const FieldMetric& g13() const; const FieldMetric& g23() const; + MetricTensor& getContravariantMetricTensor(); + // MetricTensor& getCovariantMetricTensor(); + ///< Coordinate system Jacobian, so volume of cell is J*dx*dy*dz const FieldMetric& J() const; @@ -121,6 +124,12 @@ public: // check that contravariant tensors are positive (if expected) and finite (always) void checkContravariant(int ystart); + void applyToContravariantMetricTensor( + std::function function); + + void applyToCovariantMetricTensor( + std::function function); + private: // Coordinates& coordinates; diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 7719c11db8..e116f94e08 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -343,26 +343,25 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, false, transform.get()); }; - std::basic_string region = std::basic_string("RGN_NOBNDRY"); - setContravariantMetricTensor(coords_in->contravariantMetricTensor.applyToComponents( - interpolateAndExtrapolate_function), - region); + const auto region = std::basic_string("RGN_NOBNDRY"); + setContravariantMetricTensor(coords_in->getContravariantMetricTensor(), region); - covariantMetricTensor.map(interpolateAndExtrapolate_function); + geometry.applyToContravariantMetricTensor(interpolateAndExtrapolate_function); + geometry.applyToCovariantMetricTensor(interpolateAndExtrapolate_function); // Check input metrics checkContravariant(); checkCovariant(); - this_J = localmesh->interpolateAndExtrapolate(coords_in->J(), location, true, true, - false, transform.get()); - this_Bxy = localmesh->interpolateAndExtrapolate(coords_in->Bxy(), location, true, - true, false, transform.get()); + geometry.setJ(localmesh->interpolateAndExtrapolate(coords_in->J(), location, true, + true, false, transform.get())); + geometry.setBxy(localmesh->interpolateAndExtrapolate(coords_in->Bxy(), location, true, + true, false, transform.get())); - bout::checkFinite(this_J, "The Jacobian", "RGN_NOCORNERS"); - bout::checkPositive(this_J, "The Jacobian", "RGN_NOCORNERS"); - bout::checkFinite(this_Bxy, "Bxy", "RGN_NOCORNERS"); - bout::checkPositive(this_Bxy, "Bxy", "RGN_NOCORNERS"); + bout::checkFinite(geometry.J(), "The Jacobian", "RGN_NOCORNERS"); + bout::checkPositive(geometry.J(), "The Jacobian", "RGN_NOCORNERS"); + bout::checkFinite(geometry.Bxy(), "Bxy", "RGN_NOCORNERS"); + bout::checkPositive(geometry.Bxy(), "Bxy", "RGN_NOCORNERS"); ShiftTorsion = localmesh->interpolateAndExtrapolate( coords_in->ShiftTorsion, location, true, true, false, transform.get()); @@ -1746,3 +1745,7 @@ void Coordinates::setBxy(FieldMetric Bxy) { //TODO: Calculate Bxy and check value is close this_Bxy = Bxy; } + +MetricTensor& Coordinates::getContravariantMetricTensor() const { + return geometry.getContravariantMetricTensor(); +} diff --git a/src/mesh/geometry.cxx b/src/mesh/geometry.cxx index b8164acd57..83658eb0a2 100644 --- a/src/mesh/geometry.cxx +++ b/src/mesh/geometry.cxx @@ -1567,3 +1567,19 @@ void Geometry::setBxy(FieldMetric Bxy) { //TODO: Calculate Bxy and check value is close this_Bxy = Bxy; } + +MetricTensor& Geometry::getContravariantMetricTensor() { + return contravariantMetricTensor; +} + +void Geometry::applyToContravariantMetricTensor( + std::function function) { + contravariantMetricTensor.map(function); +} + +void Geometry::applyToCovariantMetricTensor( + std::function function) { + covariantMetricTensor.map(function); +} + +//MetricTensor& Geometry::getCovariantMetricTensor() { return covariantMetricTensor; } From 7c64aa915246814bddef9f0efdb78759cc419b36 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Fri, 24 Nov 2023 18:22:34 +0000 Subject: [PATCH 174/491] Various fixes. --- include/bout/geometry.hxx | 8 ++-- src/mesh/coordinates.cxx | 78 ++++++++++++++------------------------- src/mesh/geometry.cxx | 7 ++-- 3 files changed, 36 insertions(+), 57 deletions(-) diff --git a/include/bout/geometry.hxx b/include/bout/geometry.hxx index 39332ae8b1..cfabd22618 100644 --- a/include/bout/geometry.hxx +++ b/include/bout/geometry.hxx @@ -64,7 +64,7 @@ public: FieldMetric g_22, FieldMetric g_33, FieldMetric g_12, FieldMetric g_13, FieldMetric g_23); - Geometry(Mesh* mesh, const CELL_LOC cell_location); + Geometry(Mesh* mesh, CELL_LOC cell_location); /// Christoffel symbol of the second kind (connection coefficients) FieldMetric G1_11, G1_22, G1_33, G1_12, G1_13, G1_23; @@ -89,14 +89,14 @@ public: const FieldMetric& g13() const; const FieldMetric& g23() const; - MetricTensor& getContravariantMetricTensor(); + const MetricTensor& getContravariantMetricTensor() const; // MetricTensor& getCovariantMetricTensor(); ///< Coordinate system Jacobian, so volume of cell is J*dx*dy*dz - const FieldMetric& J() const; + const FieldMetric& J(); ///< Magnitude of B = nabla z times nabla x - const FieldMetric& Bxy() const; + const FieldMetric& Bxy(); void setContravariantMetricTensor(MetricTensor metric_tensor, CELL_LOC cell_location, const std::string& region = "RGN_ALL"); diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index e116f94e08..336c619969 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -358,10 +358,10 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, geometry.setBxy(localmesh->interpolateAndExtrapolate(coords_in->Bxy(), location, true, true, false, transform.get())); - bout::checkFinite(geometry.J(), "The Jacobian", "RGN_NOCORNERS"); - bout::checkPositive(geometry.J(), "The Jacobian", "RGN_NOCORNERS"); - bout::checkFinite(geometry.Bxy(), "Bxy", "RGN_NOCORNERS"); - bout::checkPositive(geometry.Bxy(), "Bxy", "RGN_NOCORNERS"); + bout::checkFinite(J(), "The Jacobian", "RGN_NOCORNERS"); + bout::checkPositive(J(), "The Jacobian", "RGN_NOCORNERS"); + bout::checkFinite(Bxy(), "Bxy", "RGN_NOCORNERS"); + bout::checkPositive(Bxy(), "Bxy", "RGN_NOCORNERS"); ShiftTorsion = localmesh->interpolateAndExtrapolate( coords_in->ShiftTorsion, location, true, true, false, transform.get()); @@ -452,7 +452,8 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, extrapolate_x, extrapolate_y, false, transform.get()); - contravariantMetricTensor.setMetricTensor(MetricTensor(g11, g22, g33, g12, g13, g23)); + geometry.setContravariantMetricTensor(MetricTensor(g11, g22, g33, g12, g13, g23), + location); // Check input metrics checkContravariant(); @@ -477,8 +478,8 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, g_12 = getAtLocOrUnaligned(mesh, "g_12", 0.0, suffix, location); g_13 = getAtLocOrUnaligned(mesh, "g_13", 0.0, suffix, location); g_23 = getAtLocOrUnaligned(mesh, "g_23", 0.0, suffix, location); - covariantMetricTensor.setMetricTensor( - MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + geometry.setContravariantMetricTensor( + MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23), location); output_warn.write( "\tWARNING! Covariant components of metric tensor set manually. " @@ -512,7 +513,7 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, return localmesh->interpolateAndExtrapolate( component, location, extrapolate_x, extrapolate_y, false, transform.get()); }; - covariantMetricTensor.map(interpolateAndExtrapolate_function); + geometry.applyToCovariantMetricTensor(interpolateAndExtrapolate_function); // Check covariant metrics checkCovariant(); @@ -521,7 +522,7 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, jacobian(); // Attempt to read J from the grid file - auto Jcalc = this_J; + auto Jcalc = J(); if (getAtLoc(mesh, this_J, "J", suffix, location)) { output_warn.write( "\tWARNING: Jacobian 'J_{:s}' not found. Calculating from metric tensor\n", @@ -1690,60 +1691,37 @@ void Coordinates::setCovariantMetricTensor(MetricTensor metric_tensor, covariantMetricTensor.oppositeRepresentation(location, localmesh, region)); } -const MetricTensor::FieldMetric& Coordinates::g_11() const { - return covariantMetricTensor.Getg11(); -} -const MetricTensor::FieldMetric& Coordinates::g_22() const { - return covariantMetricTensor.Getg22(); -} -const MetricTensor::FieldMetric& Coordinates::g_33() const { - return covariantMetricTensor.Getg33(); -} -const MetricTensor::FieldMetric& Coordinates::g_12() const { - return covariantMetricTensor.Getg12(); -} -const MetricTensor::FieldMetric& Coordinates::g_13() const { - return covariantMetricTensor.Getg13(); -} -const MetricTensor::FieldMetric& Coordinates::g_23() const { - return covariantMetricTensor.Getg23(); -} +const MetricTensor::FieldMetric& Coordinates::g_11() const { return geometry.g_11(); } +const MetricTensor::FieldMetric& Coordinates::g_22() const { return geometry.g_22(); } +const MetricTensor::FieldMetric& Coordinates::g_33() const { return geometry.g_33(); } +const MetricTensor::FieldMetric& Coordinates::g_12() const { return geometry.g_12(); } +const MetricTensor::FieldMetric& Coordinates::g_13() const { return geometry.g_13(); } +const MetricTensor::FieldMetric& Coordinates::g_23() const { return geometry.g_23(); } -const MetricTensor::FieldMetric& Coordinates::g11() const { - return contravariantMetricTensor.Getg11(); -} -const MetricTensor::FieldMetric& Coordinates::g22() const { - return contravariantMetricTensor.Getg22(); -} -const MetricTensor::FieldMetric& Coordinates::g33() const { - return contravariantMetricTensor.Getg33(); -} -const MetricTensor::FieldMetric& Coordinates::g12() const { - return contravariantMetricTensor.Getg12(); -} -const MetricTensor::FieldMetric& Coordinates::g13() const { - return contravariantMetricTensor.Getg13(); -} -const MetricTensor::FieldMetric& Coordinates::g23() const { - return contravariantMetricTensor.Getg23(); -} -const MetricTensor::FieldMetric& Coordinates::J() const { return this_J; } +const MetricTensor::FieldMetric& Coordinates::g11() const { return geometry.g11(); } +const MetricTensor::FieldMetric& Coordinates::g22() const { return geometry.g22(); } +const MetricTensor::FieldMetric& Coordinates::g33() const { return geometry.g33(); } +const MetricTensor::FieldMetric& Coordinates::g12() const { return geometry.g12(); } +const MetricTensor::FieldMetric& Coordinates::g13() const { return geometry.g13(); } +const MetricTensor::FieldMetric& Coordinates::g23() const { return geometry.g23(); } + +const MetricTensor::FieldMetric& Coordinates::J() const { return geometry.J(); } -const MetricTensor::FieldMetric& Coordinates::Bxy() const { return this_Bxy; } +const MetricTensor::FieldMetric& Coordinates::Bxy() const { return geometry.Bxy(); } void Coordinates::setJ(FieldMetric J) { //TODO: Calculate J and check value is close - this_J = J; + geometry.setJ(J); } void Coordinates::setJ(BoutReal value, int x, int y) { //TODO: Calculate Bxy and check value is close - this_J(x, y) = value; + geometry.setJ(value, x, y); } void Coordinates::setBxy(FieldMetric Bxy) { //TODO: Calculate Bxy and check value is close - this_Bxy = Bxy; + geometry.setBxy(Bxy); } MetricTensor& Coordinates::getContravariantMetricTensor() const { diff --git a/src/mesh/geometry.cxx b/src/mesh/geometry.cxx index 83658eb0a2..4e599f7605 100644 --- a/src/mesh/geometry.cxx +++ b/src/mesh/geometry.cxx @@ -1549,9 +1549,10 @@ const MetricTensor::FieldMetric& Geometry::g13() const { const MetricTensor::FieldMetric& Geometry::g23() const { return contravariantMetricTensor.Getg23(); } -const MetricTensor::FieldMetric& Geometry::J() const { return this_J; } -const MetricTensor::FieldMetric& Geometry::Bxy() const { return this_Bxy; } +const MetricTensor::FieldMetric& Geometry::J() { return this_J; } + +const MetricTensor::FieldMetric& Geometry::Bxy() { return this_Bxy; } void Geometry::setJ(FieldMetric J) { //TODO: Calculate J and check value is close @@ -1568,7 +1569,7 @@ void Geometry::setBxy(FieldMetric Bxy) { this_Bxy = Bxy; } -MetricTensor& Geometry::getContravariantMetricTensor() { +const MetricTensor& Geometry::getContravariantMetricTensor() const { return contravariantMetricTensor; } From eb96d678cb82e4a6e050409a9e4e100e375d1263 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Fri, 24 Nov 2023 18:32:28 +0000 Subject: [PATCH 175/491] Make Coordinates::jacobian() delegate to Geometry. --- include/bout/geometry.hxx | 2 +- src/mesh/coordinates.cxx | 12 +++++------- src/mesh/geometry.cxx | 5 +---- 3 files changed, 7 insertions(+), 12 deletions(-) diff --git a/include/bout/geometry.hxx b/include/bout/geometry.hxx index cfabd22618..efd47fc0dd 100644 --- a/include/bout/geometry.hxx +++ b/include/bout/geometry.hxx @@ -114,7 +114,7 @@ public: // /// Invert covariant metric to get contravariant components // void calcContravariant(const std::string& region = "RGN_ALL"); - void jacobian(); ///< Calculate J and Bxy + void jacobian(bool extrapolate_x, bool extrapolate_y); ///< Calculate J and Bxy void CalculateChristoffelSymbols(); /// Calculate Christoffel symbol terms diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 336c619969..be04b3a3a4 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -1059,13 +1059,11 @@ void Coordinates::calcContravariant(const std::string& region) { void Coordinates::jacobian() { TRACE("Coordinates::jacobian"); - try { - this_J = recalculateJacobian(); - this_Bxy = recalculateBxy(); - } catch (BoutException&) { - output_error.write("\tError in jacobian call\n"); - throw; - } + + const bool extrapolate_x = not localmesh->sourceHasXBoundaryGuards(); + const bool extrapolate_y = not localmesh->sourceHasYBoundaryGuards(); + + geometry.jacobian(extrapolate_x, extrapolate_y); } MetricTensor::FieldMetric Coordinates::recalculateJacobian() { diff --git a/src/mesh/geometry.cxx b/src/mesh/geometry.cxx index 4e599f7605..4e1ca6fcc0 100644 --- a/src/mesh/geometry.cxx +++ b/src/mesh/geometry.cxx @@ -870,12 +870,9 @@ Geometry::Geometry(Mesh* mesh, const CELL_LOC cell_location) // covariantMetricTensor.oppositeRepresentation(location, localmesh, region)); // } // -void Geometry::jacobian() { +void Geometry::jacobian(bool extrapolate_x, bool extrapolate_y) { TRACE("Geometry::jacobian"); try { - const bool extrapolate_x = not localmesh->sourceHasXBoundaryGuards(); - const bool extrapolate_y = not localmesh->sourceHasYBoundaryGuards(); - this_J = recalculateJacobian(extrapolate_x, extrapolate_y); this_Bxy = recalculateBxy(extrapolate_x, extrapolate_y); } catch (BoutException&) { From 0c9ecc66c57ec50d7f1bc09bd790b8e5c4d888af Mon Sep 17 00:00:00 2001 From: tomc271 Date: Sat, 25 Nov 2023 10:16:50 +0000 Subject: [PATCH 176/491] Move recalculateJacobian() and recalculateBxy() to Geometery class (and call from Coordinates::jacobian()). --- include/bout/coordinates.hxx | 5 +- include/bout/geometry.hxx | 8 +- src/mesh/coordinates.cxx | 53 ++---- src/mesh/geometry.cxx | 343 +++++++++++++++++------------------ 4 files changed, 191 insertions(+), 218 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index d25aef7686..7b766fa931 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -144,7 +144,7 @@ public: void calcCovariant(const std::string& region = "RGN_ALL"); /// Invert covariant metric to get contravariant components void calcContravariant(const std::string& region = "RGN_ALL"); - void jacobian(); ///< Calculate J and Bxy + void jacobian(); ///< Calculate J and Bxy void CalculateChristoffelSymbols(); /// Calculate Christoffel symbol terms /////////////////////////////////////////////////////////// @@ -292,9 +292,6 @@ private: // FieldMetric this_J; // FieldMetric this_Bxy; ///< Magnitude of B = nabla z times nabla x - - FieldMetric recalculateJacobian(); - FieldMetric recalculateBxy(); }; /* diff --git a/include/bout/geometry.hxx b/include/bout/geometry.hxx index efd47fc0dd..a24f09358e 100644 --- a/include/bout/geometry.hxx +++ b/include/bout/geometry.hxx @@ -114,7 +114,7 @@ public: // /// Invert covariant metric to get contravariant components // void calcContravariant(const std::string& region = "RGN_ALL"); - void jacobian(bool extrapolate_x, bool extrapolate_y); ///< Calculate J and Bxy + // void jacobian(bool extrapolate_x, bool extrapolate_y); ///< Calculate J and Bxy void CalculateChristoffelSymbols(); /// Calculate Christoffel symbol terms @@ -124,6 +124,9 @@ public: // check that contravariant tensors are positive (if expected) and finite (always) void checkContravariant(int ystart); + FieldMetric recalculateJacobian(); + FieldMetric recalculateBxy(); + void applyToContravariantMetricTensor( std::function function); @@ -142,9 +145,6 @@ private: FieldMetric this_J; FieldMetric this_Bxy; ///< Magnitude of B = nabla z times nabla x - FieldMetric recalculateJacobian(bool extrapolate_x, bool extrapolate_y); - FieldMetric recalculateBxy(bool extrapolate_x, bool extrapolate_y); - // template // void communicate(T& t, Ts... ts); }; diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index be04b3a3a4..83e578ace8 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -1063,43 +1063,24 @@ void Coordinates::jacobian() { const bool extrapolate_x = not localmesh->sourceHasXBoundaryGuards(); const bool extrapolate_y = not localmesh->sourceHasYBoundaryGuards(); - geometry.jacobian(extrapolate_x, extrapolate_y); -} - -MetricTensor::FieldMetric Coordinates::recalculateJacobian() { - // calculate Jacobian using g^-1 = det[g^ij], J = sqrt(g) - auto g = contravariantMetricTensor.Getg11() * contravariantMetricTensor.Getg22() - * contravariantMetricTensor.Getg33() - + 2.0 * contravariantMetricTensor.Getg12() * contravariantMetricTensor.Getg13() - * contravariantMetricTensor.Getg23() - - contravariantMetricTensor.Getg11() * contravariantMetricTensor.Getg23() - * contravariantMetricTensor.Getg23() - - contravariantMetricTensor.Getg22() * contravariantMetricTensor.Getg13() - * contravariantMetricTensor.Getg13() - - contravariantMetricTensor.Getg33() * contravariantMetricTensor.Getg12() - * contravariantMetricTensor.Getg12(); - - // Check that g is positive - bout::checkPositive(g, "The determinant of g^ij", "RGN_NOBNDRY"); - - const auto J = 1. / sqrt(g); - - // More robust to extrapolate derived quantities directly, rather than - // deriving from extrapolated covariant metric components - const bool extrapolate_x = not localmesh->sourceHasXBoundaryGuards(); - const bool extrapolate_y = not localmesh->sourceHasYBoundaryGuards(); - - return localmesh->interpolateAndExtrapolate(J, location, extrapolate_x, extrapolate_y, - false, transform.get()); -} + // geometry.jacobian(extrapolate_x, extrapolate_y); + TRACE("Geometry::jacobian"); + try { -MetricTensor::FieldMetric Coordinates::recalculateBxy() { - const bool extrapolate_x = not localmesh->sourceHasXBoundaryGuards(); - const bool extrapolate_y = not localmesh->sourceHasYBoundaryGuards(); - - const auto Bxy = sqrt(covariantMetricTensor.Getg22()) / this_J; - return localmesh->interpolateAndExtrapolate(Bxy, location, extrapolate_x, extrapolate_y, - false, transform.get()); + const auto j = geometry.recalculateJacobian(); + // More robust to extrapolate derived quantities directly, rather than + // deriving from extrapolated covariant metric components + geometry.setJ( + localmesh->interpolateAndExtrapolate(j, extrapolate_x, extrapolate_y, false)); + + const auto Bxy = geometry.recalculateBxy(); + // CELL_LOC location, ParallelTransform* pParallelTransform + geometry.setBxy(localmesh->interpolateAndExtrapolate( + Bxy, location, extrapolate_x, extrapolate_y, false, transform.get())); + } catch (BoutException&) { + output_error.write("\tError in jacobian call\n"); + throw; + } } namespace { diff --git a/src/mesh/geometry.cxx b/src/mesh/geometry.cxx index 4e1ca6fcc0..98942fe8b9 100644 --- a/src/mesh/geometry.cxx +++ b/src/mesh/geometry.cxx @@ -708,156 +708,153 @@ Geometry::Geometry(Mesh* mesh, const CELL_LOC cell_location) // // return 0; // //} // -// void Geometry::CalculateChristoffelSymbols() { -// // Calculate Christoffel symbol terms (18 independent values) -// // Note: This calculation is completely general: metric -// // tensor can be 2D or 3D. For 2D, all DDZ terms are zero -// -// G1_11 = 0.5 * contravariantMetricTensor.Getg11() * DDX(covariantMetricTensor.Getg11()) -// + contravariantMetricTensor.Getg12() -// * (DDX(covariantMetricTensor.Getg12()) -// - 0.5 * DDY(covariantMetricTensor.Getg11())) -// + contravariantMetricTensor.Getg13() -// * (DDX(covariantMetricTensor.Getg13()) -// - 0.5 * DDZ(covariantMetricTensor.Getg11())); -// G1_22 = -// contravariantMetricTensor.Getg11() -// * (DDY(covariantMetricTensor.Getg12()) -// - 0.5 * DDX(covariantMetricTensor.Getg22())) -// + 0.5 * contravariantMetricTensor.Getg12() * DDY(covariantMetricTensor.Getg22()) -// + contravariantMetricTensor.Getg13() -// * (DDY(covariantMetricTensor.Getg23()) -// - 0.5 * DDZ(covariantMetricTensor.Getg22())); -// G1_33 = -// contravariantMetricTensor.Getg11() -// * (DDZ(covariantMetricTensor.Getg13()) -// - 0.5 * DDX(covariantMetricTensor.Getg33())) -// + contravariantMetricTensor.Getg12() -// * (DDZ(covariantMetricTensor.Getg23()) -// - 0.5 * DDY(covariantMetricTensor.Getg33())) -// + 0.5 * contravariantMetricTensor.Getg13() * DDZ(covariantMetricTensor.Getg33()); -// G1_12 = -// 0.5 * contravariantMetricTensor.Getg11() * DDY(covariantMetricTensor.Getg11()) -// + 0.5 * contravariantMetricTensor.Getg12() * DDX(covariantMetricTensor.Getg22()) -// + 0.5 * contravariantMetricTensor.Getg13() -// * (DDY(covariantMetricTensor.Getg13()) + DDX(covariantMetricTensor.Getg23()) -// - DDZ(covariantMetricTensor.Getg12())); -// G1_13 = -// 0.5 * contravariantMetricTensor.Getg11() * DDZ(covariantMetricTensor.Getg11()) -// + 0.5 * contravariantMetricTensor.Getg12() -// * (DDZ(covariantMetricTensor.Getg12()) + DDX(covariantMetricTensor.Getg23()) -// - DDY(covariantMetricTensor.Getg13())) -// + 0.5 * contravariantMetricTensor.Getg13() * DDX(covariantMetricTensor.Getg33()); -// G1_23 = -// 0.5 * contravariantMetricTensor.Getg11() -// * (DDZ(covariantMetricTensor.Getg12()) + DDY(covariantMetricTensor.Getg13()) -// - DDX(covariantMetricTensor.Getg23())) -// + 0.5 * contravariantMetricTensor.Getg12() -// * (DDZ(covariantMetricTensor.Getg22()) + DDY(covariantMetricTensor.Getg23()) -// - DDY(covariantMetricTensor.Getg23())) -// // + 0.5 *g13*(DDZ(g_32) + DDY(g_33) - DDZ(g_23)); -// // which equals -// + 0.5 * contravariantMetricTensor.Getg13() * DDY(covariantMetricTensor.Getg33()); -// -// G2_11 = 0.5 * contravariantMetricTensor.Getg12() * DDX(covariantMetricTensor.Getg11()) -// + contravariantMetricTensor.Getg22() -// * (DDX(covariantMetricTensor.Getg12()) -// - 0.5 * DDY(covariantMetricTensor.Getg11())) -// + contravariantMetricTensor.Getg23() -// * (DDX(covariantMetricTensor.Getg13()) -// - 0.5 * DDZ(covariantMetricTensor.Getg11())); -// G2_22 = -// contravariantMetricTensor.Getg12() -// * (DDY(covariantMetricTensor.Getg12()) -// - 0.5 * DDX(covariantMetricTensor.Getg22())) -// + 0.5 * contravariantMetricTensor.Getg22() * DDY(covariantMetricTensor.Getg22()) -// + contravariantMetricTensor.Getg23() -// * (DDY(contravariantMetricTensor.Getg23()) -// - 0.5 * DDZ(covariantMetricTensor.Getg22())); -// G2_33 = -// contravariantMetricTensor.Getg12() -// * (DDZ(covariantMetricTensor.Getg13()) -// - 0.5 * DDX(covariantMetricTensor.Getg33())) -// + contravariantMetricTensor.Getg22() -// * (DDZ(covariantMetricTensor.Getg23()) -// - 0.5 * DDY(covariantMetricTensor.Getg33())) -// + 0.5 * contravariantMetricTensor.Getg23() * DDZ(covariantMetricTensor.Getg33()); -// G2_12 = -// 0.5 * contravariantMetricTensor.Getg12() * DDY(covariantMetricTensor.Getg11()) -// + 0.5 * contravariantMetricTensor.Getg22() * DDX(covariantMetricTensor.Getg22()) -// + 0.5 * contravariantMetricTensor.Getg23() -// * (DDY(covariantMetricTensor.Getg13()) + DDX(covariantMetricTensor.Getg23()) -// - DDZ(covariantMetricTensor.Getg12())); -// G2_13 = -// // 0.5 *g21*(DDZ(covariantMetricTensor.Getg11()) + DDX(covariantMetricTensor.Getg13()) - DDX(covariantMetricTensor.Getg13())) -// // which equals -// 0.5 * contravariantMetricTensor.Getg12() -// * (DDZ(covariantMetricTensor.Getg11()) + DDX(covariantMetricTensor.Getg13()) -// - DDX(covariantMetricTensor.Getg13())) -// // + 0.5 *g22*(DDZ(covariantMetricTensor.Getg21()) + DDX(covariantMetricTensor.Getg23()) - DDY(covariantMetricTensor.Getg13())) -// // which equals -// + 0.5 * contravariantMetricTensor.Getg22() -// * (DDZ(covariantMetricTensor.Getg12()) + DDX(covariantMetricTensor.Getg23()) -// - DDY(covariantMetricTensor.Getg13())) -// // + 0.5 *g23*(DDZ(covariantMetricTensor.Getg31()) + DDX(covariantMetricTensor.Getg33()) - DDZ(g_13)); -// // which equals -// + 0.5 * contravariantMetricTensor.Getg23() * DDX(covariantMetricTensor.Getg33()); -// G2_23 = -// 0.5 * contravariantMetricTensor.Getg12() -// * (DDZ(covariantMetricTensor.Getg12()) + DDY(covariantMetricTensor.Getg13()) -// - DDX(covariantMetricTensor.Getg23())) -// + 0.5 * contravariantMetricTensor.Getg22() * DDZ(covariantMetricTensor.Getg22()) -// + 0.5 * contravariantMetricTensor.Getg23() * DDY(covariantMetricTensor.Getg33()); -// -// G3_11 = 0.5 * contravariantMetricTensor.Getg13() * DDX(covariantMetricTensor.Getg11()) -// + contravariantMetricTensor.Getg23() -// * (DDX(covariantMetricTensor.Getg12()) -// - 0.5 * DDY(covariantMetricTensor.Getg11())) -// + contravariantMetricTensor.Getg33() -// * (DDX(covariantMetricTensor.Getg13()) -// - 0.5 * DDZ(covariantMetricTensor.Getg11())); -// G3_22 = -// contravariantMetricTensor.Getg13() -// * (DDY(covariantMetricTensor.Getg12()) -// - 0.5 * DDX(covariantMetricTensor.Getg22())) -// + 0.5 * contravariantMetricTensor.Getg23() * DDY(covariantMetricTensor.Getg22()) -// + contravariantMetricTensor.Getg33() -// * (DDY(covariantMetricTensor.Getg23()) -// - 0.5 * DDZ(covariantMetricTensor.Getg22())); -// G3_33 = -// contravariantMetricTensor.Getg13() -// * (DDZ(covariantMetricTensor.Getg13()) -// - 0.5 * DDX(covariantMetricTensor.Getg33())) -// + contravariantMetricTensor.Getg23() -// * (DDZ(covariantMetricTensor.Getg23()) -// - 0.5 * DDY(covariantMetricTensor.Getg33())) -// + 0.5 * contravariantMetricTensor.Getg33() * DDZ(covariantMetricTensor.Getg33()); -// G3_12 = -// // 0.5 *g31*(DDY(covariantMetricTensor.Getg11()) + DDX(covariantMetricTensor.Getg12()) - DDX(covariantMetricTensor.Getg12())) -// // which equals to -// 0.5 * contravariantMetricTensor.Getg13() * DDY(covariantMetricTensor.Getg11()) -// // + 0.5 *g32*(DDY(covariantMetricTensor.Getg21()) + DDX(covariantMetricTensor.Getg22()) - DDY(covariantMetricTensor.Getg12())) -// // which equals to -// + 0.5 * contravariantMetricTensor.Getg23() * DDX(covariantMetricTensor.Getg22()) -// //+ 0.5 *g33*(DDY(covariantMetricTensor.Getg31()) + DDX(covariantMetricTensor.Getg32()) - DDZ(covariantMetricTensor.Getg12())); -// // which equals to -// + 0.5 * contravariantMetricTensor.Getg33() -// * (DDY(covariantMetricTensor.Getg13()) + DDX(covariantMetricTensor.Getg23()) -// - DDZ(covariantMetricTensor.Getg12())); -// G3_13 = -// 0.5 * contravariantMetricTensor.Getg13() * DDZ(covariantMetricTensor.Getg11()) -// + 0.5 * contravariantMetricTensor.Getg23() -// * (DDZ(covariantMetricTensor.Getg12()) + DDX(covariantMetricTensor.Getg23()) -// - DDY(covariantMetricTensor.Getg13())) -// + 0.5 * contravariantMetricTensor.Getg33() * DDX(covariantMetricTensor.Getg33()); -// G3_23 = -// 0.5 * contravariantMetricTensor.Getg13() -// * (DDZ(covariantMetricTensor.Getg12()) + DDY(covariantMetricTensor.Getg13()) -// - DDX(covariantMetricTensor.Getg23())) -// + 0.5 * contravariantMetricTensor.Getg23() * DDZ(covariantMetricTensor.Getg22()) -// + 0.5 * contravariantMetricTensor.Getg33() * DDY(covariantMetricTensor.Getg33()); -// } -// +void Geometry::CalculateChristoffelSymbols() { + // Calculate Christoffel symbol terms (18 independent values) + // Note: This calculation is completely general: metric + // tensor can be 2D or 3D. For 2D, all DDZ terms are zero + + G1_11 = 0.5 * contravariantMetricTensor.Getg11() * DDX(covariantMetricTensor.Getg11()) + + contravariantMetricTensor.Getg12() + * (DDX(covariantMetricTensor.Getg12()) + - 0.5 * DDY(covariantMetricTensor.Getg11())) + + contravariantMetricTensor.Getg13() + * (DDX(covariantMetricTensor.Getg13()) + - 0.5 * DDZ(covariantMetricTensor.Getg11())); + G1_22 = contravariantMetricTensor.Getg11() + * (DDY(covariantMetricTensor.Getg12()) + - 0.5 * DDX(covariantMetricTensor.Getg22())) + + 0.5 * contravariantMetricTensor.Getg12() * DDY(covariantMetricTensor.Getg22()) + + contravariantMetricTensor.Getg13() + * (DDY(covariantMetricTensor.Getg23()) + - 0.5 * DDZ(covariantMetricTensor.Getg22())); + G1_33 = + contravariantMetricTensor.Getg11() + * (DDZ(covariantMetricTensor.Getg13()) + - 0.5 * DDX(covariantMetricTensor.Getg33())) + + contravariantMetricTensor.Getg12() + * (DDZ(covariantMetricTensor.Getg23()) + - 0.5 * DDY(covariantMetricTensor.Getg33())) + + 0.5 * contravariantMetricTensor.Getg13() * DDZ(covariantMetricTensor.Getg33()); + G1_12 = + 0.5 * contravariantMetricTensor.Getg11() * DDY(covariantMetricTensor.Getg11()) + + 0.5 * contravariantMetricTensor.Getg12() * DDX(covariantMetricTensor.Getg22()) + + 0.5 * contravariantMetricTensor.Getg13() + * (DDY(covariantMetricTensor.Getg13()) + DDX(covariantMetricTensor.Getg23()) + - DDZ(covariantMetricTensor.Getg12())); + G1_13 = + 0.5 * contravariantMetricTensor.Getg11() * DDZ(covariantMetricTensor.Getg11()) + + 0.5 * contravariantMetricTensor.Getg12() + * (DDZ(covariantMetricTensor.Getg12()) + DDX(covariantMetricTensor.Getg23()) + - DDY(covariantMetricTensor.Getg13())) + + 0.5 * contravariantMetricTensor.Getg13() * DDX(covariantMetricTensor.Getg33()); + G1_23 = + 0.5 * contravariantMetricTensor.Getg11() + * (DDZ(covariantMetricTensor.Getg12()) + DDY(covariantMetricTensor.Getg13()) + - DDX(covariantMetricTensor.Getg23())) + + 0.5 * contravariantMetricTensor.Getg12() + * (DDZ(covariantMetricTensor.Getg22()) + DDY(covariantMetricTensor.Getg23()) + - DDY(covariantMetricTensor.Getg23())) + // + 0.5 *g13*(DDZ(g_32) + DDY(g_33) - DDZ(g_23)); + // which equals + + 0.5 * contravariantMetricTensor.Getg13() * DDY(covariantMetricTensor.Getg33()); + + G2_11 = 0.5 * contravariantMetricTensor.Getg12() * DDX(covariantMetricTensor.Getg11()) + + contravariantMetricTensor.Getg22() + * (DDX(covariantMetricTensor.Getg12()) + - 0.5 * DDY(covariantMetricTensor.Getg11())) + + contravariantMetricTensor.Getg23() + * (DDX(covariantMetricTensor.Getg13()) + - 0.5 * DDZ(covariantMetricTensor.Getg11())); + G2_22 = contravariantMetricTensor.Getg12() + * (DDY(covariantMetricTensor.Getg12()) + - 0.5 * DDX(covariantMetricTensor.Getg22())) + + 0.5 * contravariantMetricTensor.Getg22() * DDY(covariantMetricTensor.Getg22()) + + contravariantMetricTensor.Getg23() + * (DDY(contravariantMetricTensor.Getg23()) + - 0.5 * DDZ(covariantMetricTensor.Getg22())); + G2_33 = + contravariantMetricTensor.Getg12() + * (DDZ(covariantMetricTensor.Getg13()) + - 0.5 * DDX(covariantMetricTensor.Getg33())) + + contravariantMetricTensor.Getg22() + * (DDZ(covariantMetricTensor.Getg23()) + - 0.5 * DDY(covariantMetricTensor.Getg33())) + + 0.5 * contravariantMetricTensor.Getg23() * DDZ(covariantMetricTensor.Getg33()); + G2_12 = + 0.5 * contravariantMetricTensor.Getg12() * DDY(covariantMetricTensor.Getg11()) + + 0.5 * contravariantMetricTensor.Getg22() * DDX(covariantMetricTensor.Getg22()) + + 0.5 * contravariantMetricTensor.Getg23() + * (DDY(covariantMetricTensor.Getg13()) + DDX(covariantMetricTensor.Getg23()) + - DDZ(covariantMetricTensor.Getg12())); + G2_13 = + // 0.5 *g21*(DDZ(covariantMetricTensor.Getg11()) + DDX(covariantMetricTensor.Getg13()) - DDX(covariantMetricTensor.Getg13())) + // which equals + 0.5 * contravariantMetricTensor.Getg12() + * (DDZ(covariantMetricTensor.Getg11()) + DDX(covariantMetricTensor.Getg13()) + - DDX(covariantMetricTensor.Getg13())) + // + 0.5 *g22*(DDZ(covariantMetricTensor.Getg21()) + DDX(covariantMetricTensor.Getg23()) - DDY(covariantMetricTensor.Getg13())) + // which equals + + 0.5 * contravariantMetricTensor.Getg22() + * (DDZ(covariantMetricTensor.Getg12()) + DDX(covariantMetricTensor.Getg23()) + - DDY(covariantMetricTensor.Getg13())) + // + 0.5 *g23*(DDZ(covariantMetricTensor.Getg31()) + DDX(covariantMetricTensor.Getg33()) - DDZ(g_13)); + // which equals + + 0.5 * contravariantMetricTensor.Getg23() * DDX(covariantMetricTensor.Getg33()); + G2_23 = + 0.5 * contravariantMetricTensor.Getg12() + * (DDZ(covariantMetricTensor.Getg12()) + DDY(covariantMetricTensor.Getg13()) + - DDX(covariantMetricTensor.Getg23())) + + 0.5 * contravariantMetricTensor.Getg22() * DDZ(covariantMetricTensor.Getg22()) + + 0.5 * contravariantMetricTensor.Getg23() * DDY(covariantMetricTensor.Getg33()); + + G3_11 = 0.5 * contravariantMetricTensor.Getg13() * DDX(covariantMetricTensor.Getg11()) + + contravariantMetricTensor.Getg23() + * (DDX(covariantMetricTensor.Getg12()) + - 0.5 * DDY(covariantMetricTensor.Getg11())) + + contravariantMetricTensor.Getg33() + * (DDX(covariantMetricTensor.Getg13()) + - 0.5 * DDZ(covariantMetricTensor.Getg11())); + G3_22 = contravariantMetricTensor.Getg13() + * (DDY(covariantMetricTensor.Getg12()) + - 0.5 * DDX(covariantMetricTensor.Getg22())) + + 0.5 * contravariantMetricTensor.Getg23() * DDY(covariantMetricTensor.Getg22()) + + contravariantMetricTensor.Getg33() + * (DDY(covariantMetricTensor.Getg23()) + - 0.5 * DDZ(covariantMetricTensor.Getg22())); + G3_33 = + contravariantMetricTensor.Getg13() + * (DDZ(covariantMetricTensor.Getg13()) + - 0.5 * DDX(covariantMetricTensor.Getg33())) + + contravariantMetricTensor.Getg23() + * (DDZ(covariantMetricTensor.Getg23()) + - 0.5 * DDY(covariantMetricTensor.Getg33())) + + 0.5 * contravariantMetricTensor.Getg33() * DDZ(covariantMetricTensor.Getg33()); + G3_12 = + // 0.5 *g31*(DDY(covariantMetricTensor.Getg11()) + DDX(covariantMetricTensor.Getg12()) - DDX(covariantMetricTensor.Getg12())) + // which equals to + 0.5 * contravariantMetricTensor.Getg13() * DDY(covariantMetricTensor.Getg11()) + // + 0.5 *g32*(DDY(covariantMetricTensor.Getg21()) + DDX(covariantMetricTensor.Getg22()) - DDY(covariantMetricTensor.Getg12())) + // which equals to + + 0.5 * contravariantMetricTensor.Getg23() * DDX(covariantMetricTensor.Getg22()) + //+ 0.5 *g33*(DDY(covariantMetricTensor.Getg31()) + DDX(covariantMetricTensor.Getg32()) - DDZ(covariantMetricTensor.Getg12())); + // which equals to + + 0.5 * contravariantMetricTensor.Getg33() + * (DDY(covariantMetricTensor.Getg13()) + DDX(covariantMetricTensor.Getg23()) + - DDZ(covariantMetricTensor.Getg12())); + G3_13 = + 0.5 * contravariantMetricTensor.Getg13() * DDZ(covariantMetricTensor.Getg11()) + + 0.5 * contravariantMetricTensor.Getg23() + * (DDZ(covariantMetricTensor.Getg12()) + DDX(covariantMetricTensor.Getg23()) + - DDY(covariantMetricTensor.Getg13())) + + 0.5 * contravariantMetricTensor.Getg33() * DDX(covariantMetricTensor.Getg33()); + G3_23 = + 0.5 * contravariantMetricTensor.Getg13() + * (DDZ(covariantMetricTensor.Getg12()) + DDY(covariantMetricTensor.Getg13()) + - DDX(covariantMetricTensor.Getg23())) + + 0.5 * contravariantMetricTensor.Getg23() * DDZ(covariantMetricTensor.Getg22()) + + 0.5 * contravariantMetricTensor.Getg33() * DDY(covariantMetricTensor.Getg33()); +} + // void Geometry::calcCovariant(const std::string& region) { // TRACE("Geometry::calcCovariant"); // covariantMetricTensor.setMetricTensor( @@ -870,19 +867,26 @@ Geometry::Geometry(Mesh* mesh, const CELL_LOC cell_location) // covariantMetricTensor.oppositeRepresentation(location, localmesh, region)); // } // -void Geometry::jacobian(bool extrapolate_x, bool extrapolate_y) { - TRACE("Geometry::jacobian"); - try { - this_J = recalculateJacobian(extrapolate_x, extrapolate_y); - this_Bxy = recalculateBxy(extrapolate_x, extrapolate_y); - } catch (BoutException&) { - output_error.write("\tError in jacobian call\n"); - throw; - } -} +//void Geometry::jacobian(bool extrapolate_x, bool extrapolate_y) { +// TRACE("Geometry::jacobian"); +// try { +// +// const auto j = recalculateJacobian(extrapolate_x, extrapolate_y); +// // More robust to extrapolate derived quantities directly, rather than +// // deriving from extrapolated covariant metric components +// this_J = interpolateAndExtrapolate(j, extrapolate_x, extrapolate_y, false); +// +// const auto Bxy = recalculateBxy(extrapolate_x, extrapolate_y); +//// CELL_LOC location, ParallelTransform* pParallelTransform +// this_Bxy = interpolateAndExtrapolate(Bxy, location, extrapolate_x, extrapolate_y, false, +// transform.get()); +// } catch (BoutException&) { +// output_error.write("\tError in jacobian call\n"); +// throw; +// } +//} -MetricTensor::FieldMetric Geometry::recalculateJacobian(bool extrapolate_x, - bool extrapolate_y) { +MetricTensor::FieldMetric Geometry::recalculateJacobian() { // calculate Jacobian using g^-1 = det[g^ij], J = sqrt(g) auto g = contravariantMetricTensor.Getg11() * contravariantMetricTensor.Getg22() @@ -899,21 +903,12 @@ MetricTensor::FieldMetric Geometry::recalculateJacobian(bool extrapolate_x, // Check that g is positive bout::checkPositive(g, "The determinant of g^ij", "RGN_NOBNDRY"); - const auto J = 1. / sqrt(g); - - // More robust to extrapolate derived quantities directly, rather than - // deriving from extrapolated covariant metric components - - return interpolateAndExtrapolate(J, location, extrapolate_x, extrapolate_y, false, - transform.get()); + return 1. / sqrt(g); } -MetricTensor::FieldMetric Geometry::recalculateBxy(bool extrapolate_x, - bool extrapolate_y) { +MetricTensor::FieldMetric Geometry::recalculateBxy() { - const auto Bxy = sqrt(covariantMetricTensor.Getg22()) / this_J; - return interpolateAndExtrapolate(Bxy, location, extrapolate_x, extrapolate_y, false, - transform.get()); + return sqrt(covariantMetricTensor.Getg22()) / this_J; } // namespace { From 5401942a72c1ebf36b9530e4c4c6476d1fd39a55 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Sat, 25 Nov 2023 10:18:21 +0000 Subject: [PATCH 177/491] Move CalculateChristoffelSymbols() to Geometery class. --- include/bout/coordinates.hxx | 2 +- src/mesh/coordinates.cxx | 294 +++++++++++++++++------------------ 2 files changed, 148 insertions(+), 148 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index 7b766fa931..bc1cfb5de2 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -145,7 +145,7 @@ public: /// Invert covariant metric to get contravariant components void calcContravariant(const std::string& region = "RGN_ALL"); void jacobian(); ///< Calculate J and Bxy - void CalculateChristoffelSymbols(); /// Calculate Christoffel symbol terms + // void CalculateChristoffelSymbols(); /// Calculate Christoffel symbol terms /////////////////////////////////////////////////////////// // Parallel transforms diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 83e578ace8..1c46beb6e9 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -682,7 +682,7 @@ int Coordinates::calculateGeometry(bool recalculate_staggered, // Check input metrics checkContravariant(); checkCovariant(); - CalculateChristoffelSymbols(); + geometry.CalculateChristoffelSymbols(); auto tmp = this_J * contravariantMetricTensor.Getg12(); communicate(tmp); @@ -898,152 +898,152 @@ int Coordinates::calculateGeometry(bool recalculate_staggered, return 0; } -void Coordinates::CalculateChristoffelSymbols() { - // Calculate Christoffel symbol terms (18 independent values) - // Note: This calculation is completely general: metric - // tensor can be 2D or 3D. For 2D, all DDZ terms are zero - - G1_11 = 0.5 * contravariantMetricTensor.Getg11() * DDX(covariantMetricTensor.Getg11()) - + contravariantMetricTensor.Getg12() - * (DDX(covariantMetricTensor.Getg12()) - - 0.5 * DDY(covariantMetricTensor.Getg11())) - + contravariantMetricTensor.Getg13() - * (DDX(covariantMetricTensor.Getg13()) - - 0.5 * DDZ(covariantMetricTensor.Getg11())); - G1_22 = contravariantMetricTensor.Getg11() - * (DDY(covariantMetricTensor.Getg12()) - - 0.5 * DDX(covariantMetricTensor.Getg22())) - + 0.5 * contravariantMetricTensor.Getg12() * DDY(covariantMetricTensor.Getg22()) - + contravariantMetricTensor.Getg13() - * (DDY(covariantMetricTensor.Getg23()) - - 0.5 * DDZ(covariantMetricTensor.Getg22())); - G1_33 = - contravariantMetricTensor.Getg11() - * (DDZ(covariantMetricTensor.Getg13()) - - 0.5 * DDX(covariantMetricTensor.Getg33())) - + contravariantMetricTensor.Getg12() - * (DDZ(covariantMetricTensor.Getg23()) - - 0.5 * DDY(covariantMetricTensor.Getg33())) - + 0.5 * contravariantMetricTensor.Getg13() * DDZ(covariantMetricTensor.Getg33()); - G1_12 = - 0.5 * contravariantMetricTensor.Getg11() * DDY(covariantMetricTensor.Getg11()) - + 0.5 * contravariantMetricTensor.Getg12() * DDX(covariantMetricTensor.Getg22()) - + 0.5 * contravariantMetricTensor.Getg13() - * (DDY(covariantMetricTensor.Getg13()) + DDX(covariantMetricTensor.Getg23()) - - DDZ(covariantMetricTensor.Getg12())); - G1_13 = - 0.5 * contravariantMetricTensor.Getg11() * DDZ(covariantMetricTensor.Getg11()) - + 0.5 * contravariantMetricTensor.Getg12() - * (DDZ(covariantMetricTensor.Getg12()) + DDX(covariantMetricTensor.Getg23()) - - DDY(covariantMetricTensor.Getg13())) - + 0.5 * contravariantMetricTensor.Getg13() * DDX(covariantMetricTensor.Getg33()); - G1_23 = - 0.5 * contravariantMetricTensor.Getg11() - * (DDZ(covariantMetricTensor.Getg12()) + DDY(covariantMetricTensor.Getg13()) - - DDX(covariantMetricTensor.Getg23())) - + 0.5 * contravariantMetricTensor.Getg12() - * (DDZ(covariantMetricTensor.Getg22()) + DDY(covariantMetricTensor.Getg23()) - - DDY(covariantMetricTensor.Getg23())) - // + 0.5 *g13*(DDZ(g_32) + DDY(g_33) - DDZ(g_23)); - // which equals - + 0.5 * contravariantMetricTensor.Getg13() * DDY(covariantMetricTensor.Getg33()); - - G2_11 = 0.5 * contravariantMetricTensor.Getg12() * DDX(covariantMetricTensor.Getg11()) - + contravariantMetricTensor.Getg22() - * (DDX(covariantMetricTensor.Getg12()) - - 0.5 * DDY(covariantMetricTensor.Getg11())) - + contravariantMetricTensor.Getg23() - * (DDX(covariantMetricTensor.Getg13()) - - 0.5 * DDZ(covariantMetricTensor.Getg11())); - G2_22 = contravariantMetricTensor.Getg12() - * (DDY(covariantMetricTensor.Getg12()) - - 0.5 * DDX(covariantMetricTensor.Getg22())) - + 0.5 * contravariantMetricTensor.Getg22() * DDY(covariantMetricTensor.Getg22()) - + contravariantMetricTensor.Getg23() - * (DDY(contravariantMetricTensor.Getg23()) - - 0.5 * DDZ(covariantMetricTensor.Getg22())); - G2_33 = - contravariantMetricTensor.Getg12() - * (DDZ(covariantMetricTensor.Getg13()) - - 0.5 * DDX(covariantMetricTensor.Getg33())) - + contravariantMetricTensor.Getg22() - * (DDZ(covariantMetricTensor.Getg23()) - - 0.5 * DDY(covariantMetricTensor.Getg33())) - + 0.5 * contravariantMetricTensor.Getg23() * DDZ(covariantMetricTensor.Getg33()); - G2_12 = - 0.5 * contravariantMetricTensor.Getg12() * DDY(covariantMetricTensor.Getg11()) - + 0.5 * contravariantMetricTensor.Getg22() * DDX(covariantMetricTensor.Getg22()) - + 0.5 * contravariantMetricTensor.Getg23() - * (DDY(covariantMetricTensor.Getg13()) + DDX(covariantMetricTensor.Getg23()) - - DDZ(covariantMetricTensor.Getg12())); - G2_13 = - // 0.5 *g21*(DDZ(covariantMetricTensor.Getg11()) + DDX(covariantMetricTensor.Getg13()) - DDX(covariantMetricTensor.Getg13())) - // which equals - 0.5 * contravariantMetricTensor.Getg12() - * (DDZ(covariantMetricTensor.Getg11()) + DDX(covariantMetricTensor.Getg13()) - - DDX(covariantMetricTensor.Getg13())) - // + 0.5 *g22*(DDZ(covariantMetricTensor.Getg21()) + DDX(covariantMetricTensor.Getg23()) - DDY(covariantMetricTensor.Getg13())) - // which equals - + 0.5 * contravariantMetricTensor.Getg22() - * (DDZ(covariantMetricTensor.Getg12()) + DDX(covariantMetricTensor.Getg23()) - - DDY(covariantMetricTensor.Getg13())) - // + 0.5 *g23*(DDZ(covariantMetricTensor.Getg31()) + DDX(covariantMetricTensor.Getg33()) - DDZ(g_13)); - // which equals - + 0.5 * contravariantMetricTensor.Getg23() * DDX(covariantMetricTensor.Getg33()); - G2_23 = - 0.5 * contravariantMetricTensor.Getg12() - * (DDZ(covariantMetricTensor.Getg12()) + DDY(covariantMetricTensor.Getg13()) - - DDX(covariantMetricTensor.Getg23())) - + 0.5 * contravariantMetricTensor.Getg22() * DDZ(covariantMetricTensor.Getg22()) - + 0.5 * contravariantMetricTensor.Getg23() * DDY(covariantMetricTensor.Getg33()); - - G3_11 = 0.5 * contravariantMetricTensor.Getg13() * DDX(covariantMetricTensor.Getg11()) - + contravariantMetricTensor.Getg23() - * (DDX(covariantMetricTensor.Getg12()) - - 0.5 * DDY(covariantMetricTensor.Getg11())) - + contravariantMetricTensor.Getg33() - * (DDX(covariantMetricTensor.Getg13()) - - 0.5 * DDZ(covariantMetricTensor.Getg11())); - G3_22 = contravariantMetricTensor.Getg13() - * (DDY(covariantMetricTensor.Getg12()) - - 0.5 * DDX(covariantMetricTensor.Getg22())) - + 0.5 * contravariantMetricTensor.Getg23() * DDY(covariantMetricTensor.Getg22()) - + contravariantMetricTensor.Getg33() - * (DDY(covariantMetricTensor.Getg23()) - - 0.5 * DDZ(covariantMetricTensor.Getg22())); - G3_33 = - contravariantMetricTensor.Getg13() - * (DDZ(covariantMetricTensor.Getg13()) - - 0.5 * DDX(covariantMetricTensor.Getg33())) - + contravariantMetricTensor.Getg23() - * (DDZ(covariantMetricTensor.Getg23()) - - 0.5 * DDY(covariantMetricTensor.Getg33())) - + 0.5 * contravariantMetricTensor.Getg33() * DDZ(covariantMetricTensor.Getg33()); - G3_12 = - // 0.5 *g31*(DDY(covariantMetricTensor.Getg11()) + DDX(covariantMetricTensor.Getg12()) - DDX(covariantMetricTensor.Getg12())) - // which equals to - 0.5 * contravariantMetricTensor.Getg13() * DDY(covariantMetricTensor.Getg11()) - // + 0.5 *g32*(DDY(covariantMetricTensor.Getg21()) + DDX(covariantMetricTensor.Getg22()) - DDY(covariantMetricTensor.Getg12())) - // which equals to - + 0.5 * contravariantMetricTensor.Getg23() * DDX(covariantMetricTensor.Getg22()) - //+ 0.5 *g33*(DDY(covariantMetricTensor.Getg31()) + DDX(covariantMetricTensor.Getg32()) - DDZ(covariantMetricTensor.Getg12())); - // which equals to - + 0.5 * contravariantMetricTensor.Getg33() - * (DDY(covariantMetricTensor.Getg13()) + DDX(covariantMetricTensor.Getg23()) - - DDZ(covariantMetricTensor.Getg12())); - G3_13 = - 0.5 * contravariantMetricTensor.Getg13() * DDZ(covariantMetricTensor.Getg11()) - + 0.5 * contravariantMetricTensor.Getg23() - * (DDZ(covariantMetricTensor.Getg12()) + DDX(covariantMetricTensor.Getg23()) - - DDY(covariantMetricTensor.Getg13())) - + 0.5 * contravariantMetricTensor.Getg33() * DDX(covariantMetricTensor.Getg33()); - G3_23 = - 0.5 * contravariantMetricTensor.Getg13() - * (DDZ(covariantMetricTensor.Getg12()) + DDY(covariantMetricTensor.Getg13()) - - DDX(covariantMetricTensor.Getg23())) - + 0.5 * contravariantMetricTensor.Getg23() * DDZ(covariantMetricTensor.Getg22()) - + 0.5 * contravariantMetricTensor.Getg33() * DDY(covariantMetricTensor.Getg33()); -} +//void Coordinates::CalculateChristoffelSymbols() { +// // Calculate Christoffel symbol terms (18 independent values) +// // Note: This calculation is completely general: metric +// // tensor can be 2D or 3D. For 2D, all DDZ terms are zero +// +// G1_11 = 0.5 * contravariantMetricTensor.Getg11() * DDX(covariantMetricTensor.Getg11()) +// + contravariantMetricTensor.Getg12() +// * (DDX(covariantMetricTensor.Getg12()) +// - 0.5 * DDY(covariantMetricTensor.Getg11())) +// + contravariantMetricTensor.Getg13() +// * (DDX(covariantMetricTensor.Getg13()) +// - 0.5 * DDZ(covariantMetricTensor.Getg11())); +// G1_22 = contravariantMetricTensor.Getg11() +// * (DDY(covariantMetricTensor.Getg12()) +// - 0.5 * DDX(covariantMetricTensor.Getg22())) +// + 0.5 * contravariantMetricTensor.Getg12() * DDY(covariantMetricTensor.Getg22()) +// + contravariantMetricTensor.Getg13() +// * (DDY(covariantMetricTensor.Getg23()) +// - 0.5 * DDZ(covariantMetricTensor.Getg22())); +// G1_33 = +// contravariantMetricTensor.Getg11() +// * (DDZ(covariantMetricTensor.Getg13()) +// - 0.5 * DDX(covariantMetricTensor.Getg33())) +// + contravariantMetricTensor.Getg12() +// * (DDZ(covariantMetricTensor.Getg23()) +// - 0.5 * DDY(covariantMetricTensor.Getg33())) +// + 0.5 * contravariantMetricTensor.Getg13() * DDZ(covariantMetricTensor.Getg33()); +// G1_12 = +// 0.5 * contravariantMetricTensor.Getg11() * DDY(covariantMetricTensor.Getg11()) +// + 0.5 * contravariantMetricTensor.Getg12() * DDX(covariantMetricTensor.Getg22()) +// + 0.5 * contravariantMetricTensor.Getg13() +// * (DDY(covariantMetricTensor.Getg13()) + DDX(covariantMetricTensor.Getg23()) +// - DDZ(covariantMetricTensor.Getg12())); +// G1_13 = +// 0.5 * contravariantMetricTensor.Getg11() * DDZ(covariantMetricTensor.Getg11()) +// + 0.5 * contravariantMetricTensor.Getg12() +// * (DDZ(covariantMetricTensor.Getg12()) + DDX(covariantMetricTensor.Getg23()) +// - DDY(covariantMetricTensor.Getg13())) +// + 0.5 * contravariantMetricTensor.Getg13() * DDX(covariantMetricTensor.Getg33()); +// G1_23 = +// 0.5 * contravariantMetricTensor.Getg11() +// * (DDZ(covariantMetricTensor.Getg12()) + DDY(covariantMetricTensor.Getg13()) +// - DDX(covariantMetricTensor.Getg23())) +// + 0.5 * contravariantMetricTensor.Getg12() +// * (DDZ(covariantMetricTensor.Getg22()) + DDY(covariantMetricTensor.Getg23()) +// - DDY(covariantMetricTensor.Getg23())) +// // + 0.5 *g13*(DDZ(g_32) + DDY(g_33) - DDZ(g_23)); +// // which equals +// + 0.5 * contravariantMetricTensor.Getg13() * DDY(covariantMetricTensor.Getg33()); +// +// G2_11 = 0.5 * contravariantMetricTensor.Getg12() * DDX(covariantMetricTensor.Getg11()) +// + contravariantMetricTensor.Getg22() +// * (DDX(covariantMetricTensor.Getg12()) +// - 0.5 * DDY(covariantMetricTensor.Getg11())) +// + contravariantMetricTensor.Getg23() +// * (DDX(covariantMetricTensor.Getg13()) +// - 0.5 * DDZ(covariantMetricTensor.Getg11())); +// G2_22 = contravariantMetricTensor.Getg12() +// * (DDY(covariantMetricTensor.Getg12()) +// - 0.5 * DDX(covariantMetricTensor.Getg22())) +// + 0.5 * contravariantMetricTensor.Getg22() * DDY(covariantMetricTensor.Getg22()) +// + contravariantMetricTensor.Getg23() +// * (DDY(contravariantMetricTensor.Getg23()) +// - 0.5 * DDZ(covariantMetricTensor.Getg22())); +// G2_33 = +// contravariantMetricTensor.Getg12() +// * (DDZ(covariantMetricTensor.Getg13()) +// - 0.5 * DDX(covariantMetricTensor.Getg33())) +// + contravariantMetricTensor.Getg22() +// * (DDZ(covariantMetricTensor.Getg23()) +// - 0.5 * DDY(covariantMetricTensor.Getg33())) +// + 0.5 * contravariantMetricTensor.Getg23() * DDZ(covariantMetricTensor.Getg33()); +// G2_12 = +// 0.5 * contravariantMetricTensor.Getg12() * DDY(covariantMetricTensor.Getg11()) +// + 0.5 * contravariantMetricTensor.Getg22() * DDX(covariantMetricTensor.Getg22()) +// + 0.5 * contravariantMetricTensor.Getg23() +// * (DDY(covariantMetricTensor.Getg13()) + DDX(covariantMetricTensor.Getg23()) +// - DDZ(covariantMetricTensor.Getg12())); +// G2_13 = +// // 0.5 *g21*(DDZ(covariantMetricTensor.Getg11()) + DDX(covariantMetricTensor.Getg13()) - DDX(covariantMetricTensor.Getg13())) +// // which equals +// 0.5 * contravariantMetricTensor.Getg12() +// * (DDZ(covariantMetricTensor.Getg11()) + DDX(covariantMetricTensor.Getg13()) +// - DDX(covariantMetricTensor.Getg13())) +// // + 0.5 *g22*(DDZ(covariantMetricTensor.Getg21()) + DDX(covariantMetricTensor.Getg23()) - DDY(covariantMetricTensor.Getg13())) +// // which equals +// + 0.5 * contravariantMetricTensor.Getg22() +// * (DDZ(covariantMetricTensor.Getg12()) + DDX(covariantMetricTensor.Getg23()) +// - DDY(covariantMetricTensor.Getg13())) +// // + 0.5 *g23*(DDZ(covariantMetricTensor.Getg31()) + DDX(covariantMetricTensor.Getg33()) - DDZ(g_13)); +// // which equals +// + 0.5 * contravariantMetricTensor.Getg23() * DDX(covariantMetricTensor.Getg33()); +// G2_23 = +// 0.5 * contravariantMetricTensor.Getg12() +// * (DDZ(covariantMetricTensor.Getg12()) + DDY(covariantMetricTensor.Getg13()) +// - DDX(covariantMetricTensor.Getg23())) +// + 0.5 * contravariantMetricTensor.Getg22() * DDZ(covariantMetricTensor.Getg22()) +// + 0.5 * contravariantMetricTensor.Getg23() * DDY(covariantMetricTensor.Getg33()); +// +// G3_11 = 0.5 * contravariantMetricTensor.Getg13() * DDX(covariantMetricTensor.Getg11()) +// + contravariantMetricTensor.Getg23() +// * (DDX(covariantMetricTensor.Getg12()) +// - 0.5 * DDY(covariantMetricTensor.Getg11())) +// + contravariantMetricTensor.Getg33() +// * (DDX(covariantMetricTensor.Getg13()) +// - 0.5 * DDZ(covariantMetricTensor.Getg11())); +// G3_22 = contravariantMetricTensor.Getg13() +// * (DDY(covariantMetricTensor.Getg12()) +// - 0.5 * DDX(covariantMetricTensor.Getg22())) +// + 0.5 * contravariantMetricTensor.Getg23() * DDY(covariantMetricTensor.Getg22()) +// + contravariantMetricTensor.Getg33() +// * (DDY(covariantMetricTensor.Getg23()) +// - 0.5 * DDZ(covariantMetricTensor.Getg22())); +// G3_33 = +// contravariantMetricTensor.Getg13() +// * (DDZ(covariantMetricTensor.Getg13()) +// - 0.5 * DDX(covariantMetricTensor.Getg33())) +// + contravariantMetricTensor.Getg23() +// * (DDZ(covariantMetricTensor.Getg23()) +// - 0.5 * DDY(covariantMetricTensor.Getg33())) +// + 0.5 * contravariantMetricTensor.Getg33() * DDZ(covariantMetricTensor.Getg33()); +// G3_12 = +// // 0.5 *g31*(DDY(covariantMetricTensor.Getg11()) + DDX(covariantMetricTensor.Getg12()) - DDX(covariantMetricTensor.Getg12())) +// // which equals to +// 0.5 * contravariantMetricTensor.Getg13() * DDY(covariantMetricTensor.Getg11()) +// // + 0.5 *g32*(DDY(covariantMetricTensor.Getg21()) + DDX(covariantMetricTensor.Getg22()) - DDY(covariantMetricTensor.Getg12())) +// // which equals to +// + 0.5 * contravariantMetricTensor.Getg23() * DDX(covariantMetricTensor.Getg22()) +// //+ 0.5 *g33*(DDY(covariantMetricTensor.Getg31()) + DDX(covariantMetricTensor.Getg32()) - DDZ(covariantMetricTensor.Getg12())); +// // which equals to +// + 0.5 * contravariantMetricTensor.Getg33() +// * (DDY(covariantMetricTensor.Getg13()) + DDX(covariantMetricTensor.Getg23()) +// - DDZ(covariantMetricTensor.Getg12())); +// G3_13 = +// 0.5 * contravariantMetricTensor.Getg13() * DDZ(covariantMetricTensor.Getg11()) +// + 0.5 * contravariantMetricTensor.Getg23() +// * (DDZ(covariantMetricTensor.Getg12()) + DDX(covariantMetricTensor.Getg23()) +// - DDY(covariantMetricTensor.Getg13())) +// + 0.5 * contravariantMetricTensor.Getg33() * DDX(covariantMetricTensor.Getg33()); +// G3_23 = +// 0.5 * contravariantMetricTensor.Getg13() +// * (DDZ(covariantMetricTensor.Getg12()) + DDY(covariantMetricTensor.Getg13()) +// - DDX(covariantMetricTensor.Getg23())) +// + 0.5 * contravariantMetricTensor.Getg23() * DDZ(covariantMetricTensor.Getg22()) +// + 0.5 * contravariantMetricTensor.Getg33() * DDY(covariantMetricTensor.Getg33()); +//} void Coordinates::calcCovariant(const std::string& region) { TRACE("Coordinates::calcCovariant"); From bb8ae9f6729da385591b98f8b8aa6edbee488b52 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Sat, 25 Nov 2023 10:18:48 +0000 Subject: [PATCH 178/491] More fixes. Add calcCovariant() and calcContravariant() to Geometry class. --- include/bout/coordinates.hxx | 2 +- include/bout/geometry.hxx | 10 +- src/mesh/coordinates.cxx | 199 ++++++++++++++--------------------- src/mesh/geometry.cxx | 28 ++--- 4 files changed, 101 insertions(+), 138 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index bc1cfb5de2..38ad15b045 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -135,7 +135,7 @@ public: FieldMetric IntShiftTorsion; ///< Integrated shear (I in BOUT notation) - MetricTensor& getContravariantMetricTensor() const; + const MetricTensor& getContravariantMetricTensor() const; /// Calculate differential geometry quantities from the metric tensor int calculateGeometry(bool recalculate_staggered = true, diff --git a/include/bout/geometry.hxx b/include/bout/geometry.hxx index a24f09358e..60ceb91f94 100644 --- a/include/bout/geometry.hxx +++ b/include/bout/geometry.hxx @@ -93,10 +93,10 @@ public: // MetricTensor& getCovariantMetricTensor(); ///< Coordinate system Jacobian, so volume of cell is J*dx*dy*dz - const FieldMetric& J(); + const FieldMetric& J() const; ///< Magnitude of B = nabla z times nabla x - const FieldMetric& Bxy(); + const FieldMetric& Bxy() const; void setContravariantMetricTensor(MetricTensor metric_tensor, CELL_LOC cell_location, const std::string& region = "RGN_ALL"); @@ -109,10 +109,10 @@ public: void setBxy(FieldMetric Bxy); - // void calcCovariant(const std::string& region = "RGN_ALL"); + void calcCovariant(CELL_LOC cell_location, const std::string& region = "RGN_ALL"); - // /// Invert covariant metric to get contravariant components - // void calcContravariant(const std::string& region = "RGN_ALL"); + /// Invert covariant metric to get contravariant components + void calcContravariant(CELL_LOC cell_location, const std::string& region = "RGN_ALL"); // void jacobian(bool extrapolate_x, bool extrapolate_y); ///< Calculate J and Bxy diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 1c46beb6e9..0d9c5cef78 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -527,44 +527,43 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, output_warn.write( "\tWARNING: Jacobian 'J_{:s}' not found. Calculating from metric tensor\n", suffix); - this_J = Jcalc; + setJ(Jcalc); } else { - this_J = localmesh->interpolateAndExtrapolate( - this_J, location, extrapolate_x, extrapolate_y, false, transform.get()); + setJ(localmesh->interpolateAndExtrapolate(J(), location, extrapolate_x, + extrapolate_y, false, transform.get())); // Compare calculated and loaded values - output_warn.write("\tMaximum difference in J is {:e}\n", max(abs(this_J - Jcalc))); + output_warn.write("\tMaximum difference in J is {:e}\n", max(abs(J() - Jcalc))); - communicate(this_J); + communicate(J()); // Re-evaluate Bxy using new J - this_Bxy = sqrt(g_22()) / this_J; + setBxy(sqrt(g_22()) / J()); } // Check jacobian - bout::checkFinite(this_J, "J" + suffix, "RGN_NOCORNERS"); - bout::checkPositive(this_J, "J" + suffix, "RGN_NOCORNERS"); - if (min(abs(this_J)) < 1.0e-10) { + bout::checkFinite(J(), "J" + suffix, "RGN_NOCORNERS"); + bout::checkPositive(J(), "J" + suffix, "RGN_NOCORNERS"); + if (min(abs(J())) < 1.0e-10) { throw BoutException("\tERROR: Jacobian{:s} becomes very small\n", suffix); } // Attempt to read Bxy from the grid file - auto Bcalc = this_Bxy; + auto Bcalc = Bxy(); if (getAtLoc(mesh, this_Bxy, "Bxy", suffix, location)) { output_warn.write("\tWARNING: Magnitude of B field 'Bxy_{:s}' not found. " "Calculating from metric tensor\n", suffix); - this_Bxy = Bcalc; + setBxy(Bcalc); } else { - this_Bxy = localmesh->interpolateAndExtrapolate( - this_Bxy, location, extrapolate_x, extrapolate_y, false, transform.get()); - output_warn.write("\tMaximum difference in Bxy is {:e}\n", - max(abs(this_Bxy - Bcalc))); + setBxy(localmesh->interpolateAndExtrapolate(Bxy(), location, extrapolate_x, + extrapolate_y, false, transform.get())); + output_warn.write("\tMaximum difference in Bxy is {:e}\n", max(abs(Bxy() - Bcalc))); } // Check Bxy - bout::checkFinite(this_Bxy, "Bxy" + suffix, "RGN_NOCORNERS"); - bout::checkPositive(this_Bxy, "Bxy" + suffix, "RGN_NOCORNERS"); + bout::checkFinite(Bxy(), "Bxy" + suffix, "RGN_NOCORNERS"); + bout::checkPositive(Bxy(), "Bxy" + suffix, "RGN_NOCORNERS"); if (getAtLoc(mesh, ShiftTorsion, "ShiftTorsion", suffix, location)) { output_warn.write("\tWARNING: No Torsion specified for zShift. " @@ -602,34 +601,22 @@ void Coordinates::outputVars(Options& output_options) { output_options["dy" + loc_string].force(dy, "Coordinates"); output_options["dz" + loc_string].force(dz, "Coordinates"); - output_options["g11" + loc_string].force(contravariantMetricTensor.Getg11(), - "Coordinates"); - output_options["g22" + loc_string].force(contravariantMetricTensor.Getg22(), - "Coordinates"); - output_options["g33" + loc_string].force(contravariantMetricTensor.Getg33(), - "Coordinates"); - output_options["g12" + loc_string].force(contravariantMetricTensor.Getg12(), - "Coordinates"); - output_options["g13" + loc_string].force(contravariantMetricTensor.Getg13(), - "Coordinates"); - output_options["g23" + loc_string].force(contravariantMetricTensor.Getg23(), - "Coordinates"); - - output_options["g_11" + loc_string].force(covariantMetricTensor.Getg11(), - "Coordinates"); - output_options["g_22" + loc_string].force(covariantMetricTensor.Getg22(), - "Coordinates"); - output_options["g_33" + loc_string].force(covariantMetricTensor.Getg33(), - "Coordinates"); - output_options["g_12" + loc_string].force(covariantMetricTensor.Getg12(), - "Coordinates"); - output_options["g_13" + loc_string].force(covariantMetricTensor.Getg13(), - "Coordinates"); - output_options["g_23" + loc_string].force(covariantMetricTensor.Getg23(), - "Coordinates"); - - output_options["J" + loc_string].force(this_J, "Coordinates"); - output_options["Bxy" + loc_string].force(this_Bxy, "Coordinates"); + output_options["g11" + loc_string].force(g11(), "Coordinates"); + output_options["g22" + loc_string].force(g22(), "Coordinates"); + output_options["g33" + loc_string].force(g33(), "Coordinates"); + output_options["g12" + loc_string].force(g12(), "Coordinates"); + output_options["g13" + loc_string].force(g13(), "Coordinates"); + output_options["g23" + loc_string].force(g23(), "Coordinates"); + + output_options["g_11" + loc_string].force(g11(), "Coordinates"); + output_options["g_22" + loc_string].force(g22(), "Coordinates"); + output_options["g_33" + loc_string].force(g33(), "Coordinates"); + output_options["g_12" + loc_string].force(g12(), "Coordinates"); + output_options["g_13" + loc_string].force(g13(), "Coordinates"); + output_options["g_23" + loc_string].force(g23(), "Coordinates"); + + output_options["J" + loc_string].force(J(), "Coordinates"); + output_options["Bxy" + loc_string].force(Bxy(), "Coordinates"); output_options["G1" + loc_string].force(G1, "Coordinates"); output_options["G2" + loc_string].force(G2, "Coordinates"); @@ -657,13 +644,8 @@ int Coordinates::calculateGeometry(bool recalculate_staggered, bool force_interpolate_from_centre) { TRACE("Coordinates::calculateGeometry"); - communicate(dx, dy, dz, contravariantMetricTensor.Getg11(), - contravariantMetricTensor.Getg22(), contravariantMetricTensor.Getg33(), - contravariantMetricTensor.Getg12(), contravariantMetricTensor.Getg13(), - contravariantMetricTensor.Getg23(), covariantMetricTensor.Getg11(), - covariantMetricTensor.Getg22(), covariantMetricTensor.Getg33(), - covariantMetricTensor.Getg12(), covariantMetricTensor.Getg13(), - covariantMetricTensor.Getg23(), this_J, this_Bxy); + communicate(dx, dy, dz, g11(), g22(), g33(), g12(), g13(), g23(), g11(), g22(), g33(), + g12(), g13(), g23(), J(), Bxy()); output_progress.write("Calculating differential calculateGeometry terms\n"); @@ -684,21 +666,15 @@ int Coordinates::calculateGeometry(bool recalculate_staggered, checkCovariant(); geometry.CalculateChristoffelSymbols(); - auto tmp = this_J * contravariantMetricTensor.Getg12(); + auto tmp = J() * g12(); communicate(tmp); - G1 = (DDX(this_J * contravariantMetricTensor.Getg11()) + DDY(tmp) - + DDZ(this_J * contravariantMetricTensor.Getg13())) - / this_J; - tmp = this_J * contravariantMetricTensor.Getg22(); + G1 = (DDX(J() * g11()) + DDY(tmp) + DDZ(J() * g13())) / J(); + tmp = J() * g22(); communicate(tmp); - G2 = (DDX(this_J * contravariantMetricTensor.Getg12()) + DDY(tmp) - + DDZ(this_J * contravariantMetricTensor.Getg23())) - / this_J; - tmp = this_J * contravariantMetricTensor.Getg23(); + G2 = (DDX(J() * g12()) + DDY(tmp) + DDZ(J() * g23())) / J(); + tmp = J() * g23(); communicate(tmp); - G3 = (DDX(this_J * contravariantMetricTensor.Getg13()) + DDY(tmp) - + DDZ(this_J * contravariantMetricTensor.Getg33())) - / this_J; + G3 = (DDX(J() * g13()) + DDY(tmp) + DDZ(J() * g33())) / J(); // Communicate christoffel symbol terms output_progress.write("\tCommunicating connection terms\n"); @@ -1047,14 +1023,12 @@ int Coordinates::calculateGeometry(bool recalculate_staggered, void Coordinates::calcCovariant(const std::string& region) { TRACE("Coordinates::calcCovariant"); - covariantMetricTensor.setMetricTensor( - contravariantMetricTensor.oppositeRepresentation(location, localmesh, region)); + geometry.calcCovariant(location, region); } void Coordinates::calcContravariant(const std::string& region) { TRACE("Coordinates::calcContravariant"); - contravariantMetricTensor.setMetricTensor( - covariantMetricTensor.oppositeRepresentation(location, localmesh, region)); + geometry.calcContravariant(location, region); } void Coordinates::jacobian() { @@ -1070,8 +1044,8 @@ void Coordinates::jacobian() { const auto j = geometry.recalculateJacobian(); // More robust to extrapolate derived quantities directly, rather than // deriving from extrapolated covariant metric components - geometry.setJ( - localmesh->interpolateAndExtrapolate(j, extrapolate_x, extrapolate_y, false)); + geometry.setJ(localmesh->interpolateAndExtrapolate(j, location, extrapolate_x, + extrapolate_y, false)); const auto Bxy = geometry.recalculateBxy(); // CELL_LOC location, ParallelTransform* pParallelTransform @@ -1303,7 +1277,7 @@ Coordinates::FieldMetric Coordinates::Div_par(const Field2D& f, CELL_LOC outloc, // Coordinates object auto Bxy_floc = f.getCoordinates()->Bxy(); - return this_Bxy * Grad_par(f / Bxy_floc, outloc, method); + return Bxy() * Grad_par(f / Bxy_floc, outloc, method); } Field3D Coordinates::Div_par(const Field3D& f, CELL_LOC outloc, @@ -1318,7 +1292,7 @@ Field3D Coordinates::Div_par(const Field3D& f, CELL_LOC outloc, if (!f.hasParallelSlices()) { // No yup/ydown fields. The Grad_par operator will // shift to field aligned coordinates - return this_Bxy * Grad_par(f / Bxy_floc, outloc, method); + return Bxy() * Grad_par(f / Bxy_floc, outloc, method); } // Need to modify yup and ydown fields @@ -1328,7 +1302,7 @@ Field3D Coordinates::Div_par(const Field3D& f, CELL_LOC outloc, f_B.yup(i) = f.yup(i) / Bxy_floc.yup(i); f_B.ydown(i) = f.ydown(i) / Bxy_floc.ydown(i); } - return this_Bxy * Grad_par(f_B, outloc, method); + return Bxy() * Grad_par(f_B, outloc, method); } ///////////////////////////////////////////////////////// @@ -1341,7 +1315,7 @@ Coordinates::FieldMetric Coordinates::Grad2_par2(const Field2D& f, CELL_LOC outl ASSERT1(location == outloc || (outloc == CELL_DEFAULT && location == f.getLocation())) auto result = Grad2_par2_DDY_invSg(outloc, method) * DDY(f, outloc, method) - + D2DY2(f, outloc, method) / covariantMetricTensor.Getg22(); + + D2DY2(f, outloc, method) / g22(); return result; } @@ -1356,7 +1330,7 @@ Field3D Coordinates::Grad2_par2(const Field3D& f, CELL_LOC outloc, Field3D result = ::DDY(f, outloc, method); - Field3D r2 = D2DY2(f, outloc, method) / covariantMetricTensor.Getg22(); + Field3D r2 = D2DY2(f, outloc, method) / g22(); result = Grad2_par2_DDY_invSg(outloc, method) * result + r2; @@ -1375,8 +1349,7 @@ Coordinates::FieldMetric Coordinates::Delp2(const Field2D& f, CELL_LOC outloc, TRACE("Coordinates::Delp2( Field2D )"); ASSERT1(location == outloc || outloc == CELL_DEFAULT) - auto result = - G1 * DDX(f, outloc) + contravariantMetricTensor.Getg11() * D2DX2(f, outloc); + auto result = G1 * DDX(f, outloc) + g11() * D2DX2(f, outloc); return result; } @@ -1438,10 +1411,8 @@ Field3D Coordinates::Delp2(const Field3D& f, CELL_LOC outloc, bool useFFT) { } } } else { - result = G1 * ::DDX(f, outloc) + G3 * ::DDZ(f, outloc) - + contravariantMetricTensor.Getg11() * ::D2DX2(f, outloc) - + contravariantMetricTensor.Getg33() * ::D2DZ2(f, outloc) - + 2 * contravariantMetricTensor.Getg13() * ::D2DXDZ(f, outloc); + result = G1 * ::DDX(f, outloc) + G3 * ::DDZ(f, outloc) + g11() * ::D2DX2(f, outloc) + + g33() * ::D2DZ2(f, outloc) + 2 * g13() * ::D2DXDZ(f, outloc); } ASSERT2(result.getLocation() == outloc) @@ -1515,15 +1486,12 @@ FieldPerp Coordinates::Delp2(const FieldPerp& f, CELL_LOC outloc, bool useFFT) { Coordinates::FieldMetric Coordinates::Laplace_par(const Field2D& f, CELL_LOC outloc) { ASSERT1(location == outloc || outloc == CELL_DEFAULT) - return D2DY2(f, outloc) / covariantMetricTensor.Getg22() - + DDY(this_J / covariantMetricTensor.Getg22(), outloc) * DDY(f, outloc) / this_J; + return D2DY2(f, outloc) / g22() + DDY(J() / g22(), outloc) * DDY(f, outloc) / J(); } Field3D Coordinates::Laplace_par(const Field3D& f, CELL_LOC outloc) { ASSERT1(location == outloc || outloc == CELL_DEFAULT) - return D2DY2(f, outloc) / covariantMetricTensor.Getg22() - + DDY(this_J / covariantMetricTensor.Getg22(), outloc) * ::DDY(f, outloc) - / this_J; + return D2DY2(f, outloc) / g22() + DDY(J() / g22(), outloc) * ::DDY(f, outloc) / J(); } // Full Laplacian operator on scalar field @@ -1534,10 +1502,9 @@ Coordinates::FieldMetric Coordinates::Laplace(const Field2D& f, CELL_LOC outloc, TRACE("Coordinates::Laplace( Field2D )"); ASSERT1(location == outloc || outloc == CELL_DEFAULT) - auto result = G1 * DDX(f, outloc) + G2 * DDY(f, outloc) - + contravariantMetricTensor.Getg11() * D2DX2(f, outloc) - + contravariantMetricTensor.Getg22() * D2DY2(f, outloc) - + 2.0 * contravariantMetricTensor.Getg12() + auto result = G1 * DDX(f, outloc) + G2 * DDY(f, outloc) + g11() * D2DX2(f, outloc) + + g22() * D2DY2(f, outloc) + + 2.0 * g12() * D2DXDY(f, outloc, "DEFAULT", "RGN_NOBNDRY", dfdy_boundary_conditions, dfdy_dy_region); @@ -1551,15 +1518,13 @@ Field3D Coordinates::Laplace(const Field3D& f, CELL_LOC outloc, ASSERT1(location == outloc || outloc == CELL_DEFAULT) Field3D result = G1 * ::DDX(f, outloc) + G2 * ::DDY(f, outloc) + G3 * ::DDZ(f, outloc) - + contravariantMetricTensor.Getg11() * D2DX2(f, outloc) - + contravariantMetricTensor.Getg22() * D2DY2(f, outloc) - + contravariantMetricTensor.Getg33() * D2DZ2(f, outloc) + + g11() * D2DX2(f, outloc) + g22() * D2DY2(f, outloc) + + g33() * D2DZ2(f, outloc) + 2.0 - * (contravariantMetricTensor.Getg12() + * (g12() * D2DXDY(f, outloc, "DEFAULT", "RGN_NOBNDRY", dfdy_boundary_conditions, dfdy_dy_region) - + contravariantMetricTensor.Getg13() * D2DXDZ(f, outloc) - + contravariantMetricTensor.Getg23() * D2DYDZ(f, outloc)); + + g13() * D2DXDZ(f, outloc) + g23() * D2DYDZ(f, outloc)); return result; } @@ -1578,45 +1543,45 @@ Field2D Coordinates::Laplace_perpXY([[maybe_unused]] const Field2D& A, // outer x boundary const auto outer_x_avg = [&i](const auto& f) { return 0.5 * (f[i] + f[i.xp()]); }; const BoutReal outer_x_A = outer_x_avg(A); - const BoutReal outer_x_J = outer_x_avg(this_J); - const BoutReal outer_x_g11 = outer_x_avg(contravariantMetricTensor.Getg11()); + const BoutReal outer_x_J = outer_x_avg(J()); + const BoutReal outer_x_g11 = outer_x_avg(g11()); const BoutReal outer_x_dx = outer_x_avg(dx); const BoutReal outer_x_value = - outer_x_A * outer_x_J * outer_x_g11 / (this_J[i] * outer_x_dx * dx[i]); + outer_x_A * outer_x_J * outer_x_g11 / (J()[i] * outer_x_dx * dx[i]); result[i] += outer_x_value * (f[i.xp()] - f[i]); // inner x boundary const auto inner_x_avg = [&i](const auto& f) { return 0.5 * (f[i] + f[i.xm()]); }; const BoutReal inner_x_A = inner_x_avg(A); - const BoutReal inner_x_J = inner_x_avg(this_J); - const BoutReal inner_x_g11 = inner_x_avg(contravariantMetricTensor.Getg11()); + const BoutReal inner_x_J = inner_x_avg(J()); + const BoutReal inner_x_g11 = inner_x_avg(g11()); const BoutReal inner_x_dx = inner_x_avg(dx); const BoutReal inner_x_value = - inner_x_A * inner_x_J * inner_x_g11 / (this_J[i] * inner_x_dx * dx[i]); + inner_x_A * inner_x_J * inner_x_g11 / (J()[i] * inner_x_dx * dx[i]); result[i] += inner_x_value * (f[i.xm()] - f[i]); // upper y boundary const auto upper_y_avg = [&i](const auto& f) { return 0.5 * (f[i] + f[i.yp()]); }; const BoutReal upper_y_A = upper_y_avg(A); - const BoutReal upper_y_J = upper_y_avg(this_J); - const BoutReal upper_y_g_22 = upper_y_avg(covariantMetricTensor.Getg22()); - const BoutReal upper_y_g23 = upper_y_avg(covariantMetricTensor.Getg23()); - const BoutReal upper_y_g_23 = upper_y_avg(covariantMetricTensor.Getg23()); + const BoutReal upper_y_J = upper_y_avg(J()); + const BoutReal upper_y_g_22 = upper_y_avg(g22()); + const BoutReal upper_y_g23 = upper_y_avg(g23()); + const BoutReal upper_y_g_23 = upper_y_avg(g23()); const BoutReal upper_y_dy = upper_y_avg(dy); const BoutReal upper_y_value = -upper_y_A * upper_y_J * upper_y_g23 * upper_y_g_23 - / (upper_y_g_22 * this_J[i] * upper_y_dy * dy[i]); + / (upper_y_g_22 * J()[i] * upper_y_dy * dy[i]); result[i] += upper_y_value * (f[i.yp()] - f[i]); // lower y boundary const auto lower_y_avg = [&i](const auto& f) { return 0.5 * (f[i] + f[i.ym()]); }; const BoutReal lower_y_A = lower_y_avg(A); - const BoutReal lower_y_J = lower_y_avg(this_J); - const BoutReal lower_y_g_22 = lower_y_avg(covariantMetricTensor.Getg22()); - const BoutReal lower_y_g23 = lower_y_avg(contravariantMetricTensor.Getg23()); - const BoutReal lower_y_g_23 = lower_y_avg(covariantMetricTensor.Getg23()); + const BoutReal lower_y_J = lower_y_avg(J()); + const BoutReal lower_y_g_22 = lower_y_avg(g22()); + const BoutReal lower_y_g23 = lower_y_avg(g23()); + const BoutReal lower_y_g_23 = lower_y_avg(g23()); const BoutReal lower_y_dy = lower_y_avg(dy); const BoutReal lower_y_value = -lower_y_A * lower_y_J * lower_y_g23 * lower_y_g_23 - / (lower_y_g_22 * this_J[i] * lower_y_dy * dy[i]); + / (lower_y_g_22 * J()[i] * lower_y_dy * dy[i]); result[i] += lower_y_value * (f[i.ym()] - f[i]); } @@ -1629,7 +1594,7 @@ Field2D Coordinates::Laplace_perpXY([[maybe_unused]] const Field2D& A, const Coordinates::FieldMetric& Coordinates::invSg() const { if (invSgCache == nullptr) { auto ptr = std::make_unique(); - (*ptr) = 1.0 / sqrt(covariantMetricTensor.Getg22()); + (*ptr) = 1.0 / sqrt(g22()); invSgCache = std::move(ptr); } return *invSgCache; @@ -1660,14 +1625,12 @@ void Coordinates::checkContravariant() { geometry.checkContravariant(localmesh-> void Coordinates::setContravariantMetricTensor(MetricTensor metric_tensor, const std::string& region) { - geometry.setContravariantMetricTensor(metric_tensor, localmesh, region); + geometry.setContravariantMetricTensor(metric_tensor, location, region); } void Coordinates::setCovariantMetricTensor(MetricTensor metric_tensor, const std::string& region) { - covariantMetricTensor.setMetricTensor(metric_tensor); - contravariantMetricTensor.setMetricTensor( - covariantMetricTensor.oppositeRepresentation(location, localmesh, region)); + geometry.setCovariantMetricTensor(metric_tensor, location, region); } const MetricTensor::FieldMetric& Coordinates::g_11() const { return geometry.g_11(); } @@ -1703,6 +1666,6 @@ void Coordinates::setBxy(FieldMetric Bxy) { geometry.setBxy(Bxy); } -MetricTensor& Coordinates::getContravariantMetricTensor() const { +const MetricTensor& Coordinates::getContravariantMetricTensor() const { return geometry.getContravariantMetricTensor(); } diff --git a/src/mesh/geometry.cxx b/src/mesh/geometry.cxx index 98942fe8b9..f5bea2414c 100644 --- a/src/mesh/geometry.cxx +++ b/src/mesh/geometry.cxx @@ -855,18 +855,18 @@ void Geometry::CalculateChristoffelSymbols() { + 0.5 * contravariantMetricTensor.Getg33() * DDY(covariantMetricTensor.Getg33()); } -// void Geometry::calcCovariant(const std::string& region) { -// TRACE("Geometry::calcCovariant"); -// covariantMetricTensor.setMetricTensor( -// contravariantMetricTensor.oppositeRepresentation(location, localmesh, region)); -// } -// -// void Geometry::calcContravariant(const std::string& region) { -// TRACE("Geometry::calcContravariant"); -// contravariantMetricTensor.setMetricTensor( -// covariantMetricTensor.oppositeRepresentation(location, localmesh, region)); -// } -// +void Geometry::calcCovariant(CELL_LOC cell_location, const std::string& region) { + TRACE("Geometry::calcCovariant"); + covariantMetricTensor.setMetricTensor( + contravariantMetricTensor.oppositeRepresentation(cell_location, region)); +} + +void Geometry::calcContravariant(CELL_LOC cell_location, const std::string& region) { + TRACE("Geometry::calcContravariant"); + contravariantMetricTensor.setMetricTensor( + covariantMetricTensor.oppositeRepresentation(cell_location, region)); +} + //void Geometry::jacobian(bool extrapolate_x, bool extrapolate_y) { // TRACE("Geometry::jacobian"); // try { @@ -1542,9 +1542,9 @@ const MetricTensor::FieldMetric& Geometry::g23() const { return contravariantMetricTensor.Getg23(); } -const MetricTensor::FieldMetric& Geometry::J() { return this_J; } +const Geometry::FieldMetric& Geometry::J() const { return this_J; } -const MetricTensor::FieldMetric& Geometry::Bxy() { return this_Bxy; } +const Geometry::FieldMetric& Geometry::Bxy() const { return this_Bxy; } void Geometry::setJ(FieldMetric J) { //TODO: Calculate J and check value is close From 6e752c06b6d4eb8dc03b085a79eeef052746b6b0 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Sat, 25 Nov 2023 10:49:45 +0000 Subject: [PATCH 179/491] Use version of getAtLoc method that returns a new value rather than updated the existing one. --- src/mesh/coordinates.cxx | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 0d9c5cef78..6630b9d2fa 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -523,12 +523,13 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, // Attempt to read J from the grid file auto Jcalc = J(); - if (getAtLoc(mesh, this_J, "J", suffix, location)) { + try { + Jcalc = getAtLoc(mesh, "J", suffix, location); output_warn.write( "\tWARNING: Jacobian 'J_{:s}' not found. Calculating from metric tensor\n", suffix); setJ(Jcalc); - } else { + } catch (BoutException) { setJ(localmesh->interpolateAndExtrapolate(J(), location, extrapolate_x, extrapolate_y, false, transform.get())); @@ -550,12 +551,13 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, // Attempt to read Bxy from the grid file auto Bcalc = Bxy(); - if (getAtLoc(mesh, this_Bxy, "Bxy", suffix, location)) { + try { + Bcalc = getAtLoc(mesh, "Bxy", suffix, location); output_warn.write("\tWARNING: Magnitude of B field 'Bxy_{:s}' not found. " "Calculating from metric tensor\n", suffix); setBxy(Bcalc); - } else { + } catch (BoutException) { setBxy(localmesh->interpolateAndExtrapolate(Bxy(), location, extrapolate_x, extrapolate_y, false, transform.get())); output_warn.write("\tMaximum difference in Bxy is {:e}\n", max(abs(Bxy() - Bcalc))); From e1da8d7f159ab62f314a3cab938e7ff902318092 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Sat, 25 Nov 2023 11:53:03 +0000 Subject: [PATCH 180/491] Extract class DifferentialOperators. --- CMakeLists.txt | 10 +- include/bout/coordinates.hxx | 9 +- include/bout/differential_operators.hxx | 107 +++ src/mesh/coordinates.cxx | 903 ++++++++++++------------ src/mesh/differential_operators.cxx | 463 ++++++++++++ 5 files changed, 1037 insertions(+), 455 deletions(-) create mode 100644 include/bout/differential_operators.hxx create mode 100644 src/mesh/differential_operators.cxx diff --git a/CMakeLists.txt b/CMakeLists.txt index c85a7bb242..cd76dd005b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -352,12 +352,14 @@ set(BOUT_SOURCES ./src/sys/timer.cxx ./src/sys/type_name.cxx ./src/sys/utils.cxx + ./src/mesh/metricTensor.cxx + ./include/bout/metricTensor.hxx + ./include/bout/geometry.hxx + ./src/mesh/geometry.cxx + ./src/mesh/differential_operators.cxx + ./include/bout/differential_operators.hxx ${CMAKE_CURRENT_BINARY_DIR}/include/bout/revision.hxx ${CMAKE_CURRENT_BINARY_DIR}/include/bout/version.hxx - src/mesh/metricTensor.cxx - include/bout/metricTensor.hxx - include/bout/geometry.hxx - src/mesh/geometry.cxx ) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index 38ad15b045..d49d9285ab 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -144,8 +144,8 @@ public: void calcCovariant(const std::string& region = "RGN_ALL"); /// Invert covariant metric to get contravariant components void calcContravariant(const std::string& region = "RGN_ALL"); - void jacobian(); ///< Calculate J and Bxy - // void CalculateChristoffelSymbols(); /// Calculate Christoffel symbol terms + void jacobian(); ///< Calculate J and Bxy + void CalculateChristoffelSymbols(); /// Calculate Christoffel symbol terms /////////////////////////////////////////////////////////// // Parallel transforms @@ -159,7 +159,7 @@ public: /// Return the parallel transform ParallelTransform& getParallelTransform() { - ASSERT1(transform != nullptr) + ASSERT1(transform != nullptr); return *transform; } @@ -292,6 +292,9 @@ private: // FieldMetric this_J; // FieldMetric this_Bxy; ///< Magnitude of B = nabla z times nabla x + + FieldMetric recalculateJacobian(); + FieldMetric recalculateBxy(); }; /* diff --git a/include/bout/differential_operators.hxx b/include/bout/differential_operators.hxx new file mode 100644 index 0000000000..9ad89127a8 --- /dev/null +++ b/include/bout/differential_operators.hxx @@ -0,0 +1,107 @@ + +#ifndef BOUT_DIFFERENTIALOPERATORS_HXX +#define BOUT_DIFFERENTIALOPERATORS_HXX + +#include "bout/field2d.hxx" +#include "bout/field3d.hxx" +#include "bout/index_derivs_interface.hxx" +#include "bout/paralleltransform.hxx" +#include + +class DifferentialOperators { + +public: +#if BOUT_USE_METRIC_3D + using FieldMetric = Field3D; +#else + using FieldMetric = Field2D; +#endif + + DifferentialOperators(CELL_LOC& location, FieldMetric& dx, FieldMetric& dy, + FieldMetric& dz); + + FieldMetric DDX(const Field2D& f, CELL_LOC outloc = CELL_DEFAULT, + const std::string& method = "DEFAULT", + const std::string& region = "RGN_NOBNDRY"); + + FieldMetric DDY(const Field2D& f, CELL_LOC outloc = CELL_DEFAULT, + const std::string& method = "DEFAULT", + const std::string& region = "RGN_NOBNDRY") const; + + FieldMetric DDZ(const Field2D& f, CELL_LOC outloc = CELL_DEFAULT, + const std::string& method = "DEFAULT", + const std::string& region = "RGN_NOBNDRY"); + + Field3D DDX(const Field3D& f, CELL_LOC outloc = CELL_DEFAULT, + const std::string& method = "DEFAULT", + const std::string& region = "RGN_NOBNDRY"); + + Field3D DDY(const Field3D& f, CELL_LOC outloc = CELL_DEFAULT, + const std::string& method = "DEFAULT", + const std::string& region = "RGN_NOBNDRY") const; + + Field3D DDZ(const Field3D& f, CELL_LOC outloc = CELL_DEFAULT, + const std::string& method = "DEFAULT", + const std::string& region = "RGN_NOBNDRY"); + + /// Gradient along magnetic field b.Grad(f) + FieldMetric Grad_par(const Field2D& var, CELL_LOC outloc = CELL_DEFAULT, + const std::string& method = "DEFAULT"); + + Field3D Grad_par(const Field3D& var, CELL_LOC outloc = CELL_DEFAULT, + const std::string& method = "DEFAULT"); + + /// Advection along magnetic field V*b.Grad(f) + FieldMetric Vpar_Grad_par(const Field2D& v, const Field2D& f, + CELL_LOC outloc = CELL_DEFAULT, + const std::string& method = "DEFAULT"); + + Field3D Vpar_Grad_par(const Field3D& v, const Field3D& f, + CELL_LOC outloc = CELL_DEFAULT, + const std::string& method = "DEFAULT"); + + /// Divergence along magnetic field Div(b*f) = B.Grad(f/B) + FieldMetric Div_par(const Field2D& f, CELL_LOC outloc = CELL_DEFAULT, + const std::string& method = "DEFAULT"); + + Field3D Div_par(const Field3D& f, CELL_LOC outloc = CELL_DEFAULT, + const std::string& method = "DEFAULT"); + + // Second derivative along magnetic field + FieldMetric Grad2_par2(const Field2D& f, CELL_LOC outloc = CELL_DEFAULT, + const std::string& method = "DEFAULT"); + + Field3D Grad2_par2(const Field3D& f, CELL_LOC outloc = CELL_DEFAULT, + const std::string& method = "DEFAULT"); + // Perpendicular Laplacian operator, using only X-Z derivatives + // NOTE: This might be better bundled with the Laplacian inversion code + // since it makes use of the same coefficients and FFT routines + FieldMetric Delp2(const Field2D& f, CELL_LOC outloc = CELL_DEFAULT, bool useFFT = true); + Field3D Delp2(const Field3D& f, CELL_LOC outloc = CELL_DEFAULT, bool useFFT = true); + FieldPerp Delp2(const FieldPerp& f, CELL_LOC outloc = CELL_DEFAULT, bool useFFT = true); + + // Full parallel Laplacian operator on scalar field + // Laplace_par(f) = Div( b (b dot Grad(f)) ) + FieldMetric Laplace_par(const Field2D& f, CELL_LOC outloc = CELL_DEFAULT); + Field3D Laplace_par(const Field3D& f, CELL_LOC outloc = CELL_DEFAULT); + + // Full Laplacian operator on scalar field + FieldMetric Laplace(const Field2D& f, CELL_LOC outloc = CELL_DEFAULT, + const std::string& dfdy_boundary_conditions = "free_o3", + const std::string& dfdy_dy_region = ""); + Field3D Laplace(const Field3D& f, CELL_LOC outloc = CELL_DEFAULT, + const std::string& dfdy_boundary_conditions = "free_o3", + const std::string& dfdy_dy_region = ""); + + // Full perpendicular Laplacian, in form of inverse of Laplacian operator in LaplaceXY + // solver + Field2D Laplace_perpXY(const Field2D& A, const Field2D& f); + +private: + CELL_LOC& location; + FieldMetric& dx; + FieldMetric& dy; + FieldMetric& dz; +}; + +#endif //BOUT_DIFFERENTIALOPERATORS_HXX diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 6630b9d2fa..2d1f2c37eb 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -11,7 +11,7 @@ #include #include -#include +//#include #include #include @@ -19,6 +19,13 @@ #include "parallel/fci.hxx" #include "parallel/shiftedmetricinterp.hxx" +#include "bout/geometry.hxx" +#include "bout/metricTensor.hxx" +#include "bout/field2d.hxx" +#include "bout/field3d.hxx" +#include "bout/utils.hxx" +#include + // use anonymous namespace so this utility function is not available outside this file namespace { template @@ -628,7 +635,7 @@ void Coordinates::outputVars(Options& output_options) { } const Field2D& Coordinates::zlength() const { - BOUT_OMP_SAFE(critical) + BOUT_OMP(critical) if (not zlength_cache) { zlength_cache = std::make_unique(0., localmesh); @@ -1174,452 +1181,452 @@ void Coordinates::setParallelTransform(Options* options) { * *******************************************************************************/ -Coordinates::FieldMetric Coordinates::DDX(const Field2D& f, CELL_LOC loc, - const std::string& method, - const std::string& region) { - ASSERT1(location == loc || loc == CELL_DEFAULT) - return bout::derivatives::index::DDX(f, loc, method, region) / dx; -} -Field3D Coordinates::DDX(const Field3D& f, CELL_LOC outloc, const std::string& method, - const std::string& region) { - auto result = bout::derivatives::index::DDX(f, outloc, method, region); - result /= dx; - - if (f.getMesh()->IncIntShear) { - // Using BOUT-06 style shifting - result += IntShiftTorsion * DDZ(f, outloc, method, region); - } - - return result; -} - -Coordinates::FieldMetric Coordinates::DDY(const Field2D& f, CELL_LOC loc, - const std::string& method, - const std::string& region) const { - ASSERT1(location == loc || loc == CELL_DEFAULT) - return bout::derivatives::index::DDY(f, loc, method, region) / dy; -} - -Field3D Coordinates::DDY(const Field3D& f, CELL_LOC outloc, const std::string& method, - const std::string& region) const { -#if BOUT_USE_METRIC_3D - if (!f.hasParallelSlices() and !transform->canToFromFieldAligned()) { - Field3D f_parallel = f; - transform->calcParallelSlices(f_parallel); - f_parallel.applyParallelBoundary("parallel_neumann"); - return bout::derivatives::index::DDY(f_parallel, outloc, method, region); - } -#endif - return bout::derivatives::index::DDY(f, outloc, method, region) / dy; -} - -Coordinates::FieldMetric Coordinates::DDZ(const Field2D& f, CELL_LOC loc, - const std::string& UNUSED(method), - const std::string& UNUSED(region)) { - ASSERT1(location == loc || loc == CELL_DEFAULT) - ASSERT1(f.getMesh() == localmesh) - if (loc == CELL_DEFAULT) { - loc = f.getLocation(); - } - return zeroFrom(f).setLocation(loc); -} -Field3D Coordinates::DDZ(const Field3D& f, CELL_LOC outloc, const std::string& method, - const std::string& region) { - return bout::derivatives::index::DDZ(f, outloc, method, region) / dz; -} - -///////////////////////////////////////////////////////// -// Parallel gradient - -Coordinates::FieldMetric Coordinates::Grad_par(const Field2D& var, - [[maybe_unused]] CELL_LOC outloc, - const std::string& UNUSED(method)) { - TRACE("Coordinates::Grad_par( Field2D )"); - ASSERT1(location == outloc || (outloc == CELL_DEFAULT && location == var.getLocation())) - - return DDY(var) * invSg(); -} - -Field3D Coordinates::Grad_par(const Field3D& var, CELL_LOC outloc, - const std::string& method) { - TRACE("Coordinates::Grad_par( Field3D )"); - ASSERT1(location == outloc || outloc == CELL_DEFAULT) - - return ::DDY(var, outloc, method) * invSg(); -} - -///////////////////////////////////////////////////////// -// Vpar_Grad_par -// vparallel times the parallel derivative along unperturbed B-field - -Coordinates::FieldMetric Coordinates::Vpar_Grad_par(const Field2D& v, const Field2D& f, - [[maybe_unused]] CELL_LOC outloc, - const std::string& UNUSED(method)) { - ASSERT1(location == outloc || (outloc == CELL_DEFAULT && location == f.getLocation())) - - return VDDY(v, f) * invSg(); -} - -Field3D Coordinates::Vpar_Grad_par(const Field3D& v, const Field3D& f, CELL_LOC outloc, - const std::string& method) { - ASSERT1(location == outloc || outloc == CELL_DEFAULT) - - return VDDY(v, f, outloc, method) * invSg(); -} - -///////////////////////////////////////////////////////// -// Parallel divergence - -Coordinates::FieldMetric Coordinates::Div_par(const Field2D& f, CELL_LOC outloc, - const std::string& method) { - TRACE("Coordinates::Div_par( Field2D )"); - ASSERT1(location == outloc || outloc == CELL_DEFAULT) - - // Need Bxy at location of f, which might be different from location of this - // Coordinates object - auto Bxy_floc = f.getCoordinates()->Bxy(); - - return Bxy() * Grad_par(f / Bxy_floc, outloc, method); -} - -Field3D Coordinates::Div_par(const Field3D& f, CELL_LOC outloc, - const std::string& method) { - TRACE("Coordinates::Div_par( Field3D )"); - ASSERT1(location == outloc || outloc == CELL_DEFAULT) - - // Need Bxy at location of f, which might be different from location of this - // Coordinates object - auto Bxy_floc = f.getCoordinates()->Bxy(); - - if (!f.hasParallelSlices()) { - // No yup/ydown fields. The Grad_par operator will - // shift to field aligned coordinates - return Bxy() * Grad_par(f / Bxy_floc, outloc, method); - } - - // Need to modify yup and ydown fields - Field3D f_B = f / Bxy_floc; - f_B.splitParallelSlices(); - for (int i = 0; i < f.getMesh()->ystart; ++i) { - f_B.yup(i) = f.yup(i) / Bxy_floc.yup(i); - f_B.ydown(i) = f.ydown(i) / Bxy_floc.ydown(i); - } - return Bxy() * Grad_par(f_B, outloc, method); -} - -///////////////////////////////////////////////////////// -// second parallel derivative (b dot Grad)(b dot Grad) -// Note: For parallel Laplacian use Laplace_par - -Coordinates::FieldMetric Coordinates::Grad2_par2(const Field2D& f, CELL_LOC outloc, - const std::string& method) { - TRACE("Coordinates::Grad2_par2( Field2D )"); - ASSERT1(location == outloc || (outloc == CELL_DEFAULT && location == f.getLocation())) - - auto result = Grad2_par2_DDY_invSg(outloc, method) * DDY(f, outloc, method) - + D2DY2(f, outloc, method) / g22(); - - return result; -} - -Field3D Coordinates::Grad2_par2(const Field3D& f, CELL_LOC outloc, - const std::string& method) { - TRACE("Coordinates::Grad2_par2( Field3D )"); - if (outloc == CELL_DEFAULT) { - outloc = f.getLocation(); - } - ASSERT1(location == outloc) - - Field3D result = ::DDY(f, outloc, method); - - Field3D r2 = D2DY2(f, outloc, method) / g22(); - - result = Grad2_par2_DDY_invSg(outloc, method) * result + r2; - - ASSERT2(result.getLocation() == outloc) - - return result; -} - -///////////////////////////////////////////////////////// -// perpendicular Laplacian operator - -#include // Delp2 uses same coefficients as inversion code - -Coordinates::FieldMetric Coordinates::Delp2(const Field2D& f, CELL_LOC outloc, - bool UNUSED(useFFT)) { - TRACE("Coordinates::Delp2( Field2D )"); - ASSERT1(location == outloc || outloc == CELL_DEFAULT) - - auto result = G1 * DDX(f, outloc) + g11() * D2DX2(f, outloc); - - return result; -} - -Field3D Coordinates::Delp2(const Field3D& f, CELL_LOC outloc, bool useFFT) { - TRACE("Coordinates::Delp2( Field3D )"); - - if (outloc == CELL_DEFAULT) { - outloc = f.getLocation(); - } - - ASSERT1(location == outloc) - ASSERT1(f.getLocation() == outloc) - - if (localmesh->GlobalNx == 1 && localmesh->GlobalNz == 1) { - // copy mesh, location, etc - return f * 0; - } - ASSERT2(localmesh->xstart > 0) // Need at least one guard cell - - Field3D result{emptyFrom(f).setLocation(outloc)}; - - if (useFFT and not bout::build::use_metric_3d) { - int ncz = localmesh->LocalNz; - - // Allocate memory - auto ft = Matrix(localmesh->LocalNx, ncz / 2 + 1); - auto delft = Matrix(localmesh->LocalNx, ncz / 2 + 1); - - // Loop over y indices - // Note: should not include y-guard or y-boundary points here as that would - // use values from corner cells in dx, which may not be initialised. - for (int jy = localmesh->ystart; jy <= localmesh->yend; jy++) { - - // Take forward FFT - - for (int jx = 0; jx < localmesh->LocalNx; jx++) { - rfft(&f(jx, jy, 0), ncz, &ft(jx, 0)); - } - - // Loop over kz - for (int jz = 0; jz <= ncz / 2; jz++) { - - // No smoothing in the x direction - for (int jx = localmesh->xstart; jx <= localmesh->xend; jx++) { - // Perform x derivative - - dcomplex a, b, c; - laplace_tridag_coefs(jx, jy, jz, a, b, c, nullptr, nullptr, outloc); - - delft(jx, jz) = a * ft(jx - 1, jz) + b * ft(jx, jz) + c * ft(jx + 1, jz); - } - } - - // Reverse FFT - for (int jx = localmesh->xstart; jx <= localmesh->xend; jx++) { - - irfft(&delft(jx, 0), ncz, &result(jx, jy, 0)); - } - } - } else { - result = G1 * ::DDX(f, outloc) + G3 * ::DDZ(f, outloc) + g11() * ::D2DX2(f, outloc) - + g33() * ::D2DZ2(f, outloc) + 2 * g13() * ::D2DXDZ(f, outloc); - } - - ASSERT2(result.getLocation() == outloc) - - return result; -} - -FieldPerp Coordinates::Delp2(const FieldPerp& f, CELL_LOC outloc, bool useFFT) { - TRACE("Coordinates::Delp2( FieldPerp )"); - - if (outloc == CELL_DEFAULT) { - outloc = f.getLocation(); - } - - ASSERT1(location == outloc) - ASSERT1(f.getLocation() == outloc) - - if (localmesh->GlobalNx == 1 && localmesh->GlobalNz == 1) { - // copy mesh, location, etc - return f * 0; - } - ASSERT2(localmesh->xstart > 0) // Need at least one guard cell - - FieldPerp result{emptyFrom(f).setLocation(outloc)}; - - int jy = f.getIndex(); - result.setIndex(jy); - - if (useFFT) { - int ncz = localmesh->LocalNz; - - // Allocate memory - auto ft = Matrix(localmesh->LocalNx, ncz / 2 + 1); - auto delft = Matrix(localmesh->LocalNx, ncz / 2 + 1); - - // Take forward FFT - for (int jx = 0; jx < localmesh->LocalNx; jx++) { - rfft(&f(jx, 0), ncz, &ft(jx, 0)); - } - - // Loop over kz - for (int jz = 0; jz <= ncz / 2; jz++) { - - // No smoothing in the x direction - for (int jx = localmesh->xstart; jx <= localmesh->xend; jx++) { - // Perform x derivative - - dcomplex a, b, c; - laplace_tridag_coefs(jx, jy, jz, a, b, c); - - delft(jx, jz) = a * ft(jx - 1, jz) + b * ft(jx, jz) + c * ft(jx + 1, jz); - } - } - - // Reverse FFT - for (int jx = localmesh->xstart; jx <= localmesh->xend; jx++) { - irfft(&delft(jx, 0), ncz, &result(jx, 0)); - } - - } else { - throw BoutException("Non-fourier Delp2 not currently implented for FieldPerp."); - // Would be the following but don't have standard derivative operators for FieldPerps - // yet - // result = G1 * ::DDX(f, outloc) + G3 * ::DDZ(f, outloc) + g11 * ::D2DX2(f, outloc) - // + g33 * ::D2DZ2(f, outloc) + 2 * g13 * ::D2DXDZ(f, outloc); - } - - return result; -} - -Coordinates::FieldMetric Coordinates::Laplace_par(const Field2D& f, CELL_LOC outloc) { - ASSERT1(location == outloc || outloc == CELL_DEFAULT) - - return D2DY2(f, outloc) / g22() + DDY(J() / g22(), outloc) * DDY(f, outloc) / J(); -} - -Field3D Coordinates::Laplace_par(const Field3D& f, CELL_LOC outloc) { - ASSERT1(location == outloc || outloc == CELL_DEFAULT) - return D2DY2(f, outloc) / g22() + DDY(J() / g22(), outloc) * ::DDY(f, outloc) / J(); -} - -// Full Laplacian operator on scalar field - -Coordinates::FieldMetric Coordinates::Laplace(const Field2D& f, CELL_LOC outloc, - const std::string& dfdy_boundary_conditions, - const std::string& dfdy_dy_region) { - TRACE("Coordinates::Laplace( Field2D )"); - ASSERT1(location == outloc || outloc == CELL_DEFAULT) - - auto result = G1 * DDX(f, outloc) + G2 * DDY(f, outloc) + g11() * D2DX2(f, outloc) - + g22() * D2DY2(f, outloc) - + 2.0 * g12() - * D2DXDY(f, outloc, "DEFAULT", "RGN_NOBNDRY", - dfdy_boundary_conditions, dfdy_dy_region); - - return result; -} - -Field3D Coordinates::Laplace(const Field3D& f, CELL_LOC outloc, - const std::string& dfdy_boundary_conditions, - const std::string& dfdy_dy_region) { - TRACE("Coordinates::Laplace( Field3D )"); - ASSERT1(location == outloc || outloc == CELL_DEFAULT) - - Field3D result = G1 * ::DDX(f, outloc) + G2 * ::DDY(f, outloc) + G3 * ::DDZ(f, outloc) - + g11() * D2DX2(f, outloc) + g22() * D2DY2(f, outloc) - + g33() * D2DZ2(f, outloc) - + 2.0 - * (g12() - * D2DXDY(f, outloc, "DEFAULT", "RGN_NOBNDRY", - dfdy_boundary_conditions, dfdy_dy_region) - + g13() * D2DXDZ(f, outloc) + g23() * D2DYDZ(f, outloc)); - - return result; -} - -// Full perpendicular Laplacian, in form of inverse of Laplacian operator in LaplaceXY -// solver -Field2D Coordinates::Laplace_perpXY([[maybe_unused]] const Field2D& A, - [[maybe_unused]] const Field2D& f) { - TRACE("Coordinates::Laplace_perpXY( Field2D )"); -#if not(BOUT_USE_METRIC_3D) - Field2D result; - result.allocate(); - for (auto i : result.getRegion(RGN_NOBNDRY)) { - result[i] = 0.; - - // outer x boundary - const auto outer_x_avg = [&i](const auto& f) { return 0.5 * (f[i] + f[i.xp()]); }; - const BoutReal outer_x_A = outer_x_avg(A); - const BoutReal outer_x_J = outer_x_avg(J()); - const BoutReal outer_x_g11 = outer_x_avg(g11()); - const BoutReal outer_x_dx = outer_x_avg(dx); - const BoutReal outer_x_value = - outer_x_A * outer_x_J * outer_x_g11 / (J()[i] * outer_x_dx * dx[i]); - result[i] += outer_x_value * (f[i.xp()] - f[i]); - - // inner x boundary - const auto inner_x_avg = [&i](const auto& f) { return 0.5 * (f[i] + f[i.xm()]); }; - const BoutReal inner_x_A = inner_x_avg(A); - const BoutReal inner_x_J = inner_x_avg(J()); - const BoutReal inner_x_g11 = inner_x_avg(g11()); - const BoutReal inner_x_dx = inner_x_avg(dx); - const BoutReal inner_x_value = - inner_x_A * inner_x_J * inner_x_g11 / (J()[i] * inner_x_dx * dx[i]); - result[i] += inner_x_value * (f[i.xm()] - f[i]); - - // upper y boundary - const auto upper_y_avg = [&i](const auto& f) { return 0.5 * (f[i] + f[i.yp()]); }; - const BoutReal upper_y_A = upper_y_avg(A); - const BoutReal upper_y_J = upper_y_avg(J()); - const BoutReal upper_y_g_22 = upper_y_avg(g22()); - const BoutReal upper_y_g23 = upper_y_avg(g23()); - const BoutReal upper_y_g_23 = upper_y_avg(g23()); - const BoutReal upper_y_dy = upper_y_avg(dy); - const BoutReal upper_y_value = -upper_y_A * upper_y_J * upper_y_g23 * upper_y_g_23 - / (upper_y_g_22 * J()[i] * upper_y_dy * dy[i]); - result[i] += upper_y_value * (f[i.yp()] - f[i]); - - // lower y boundary - const auto lower_y_avg = [&i](const auto& f) { return 0.5 * (f[i] + f[i.ym()]); }; - const BoutReal lower_y_A = lower_y_avg(A); - const BoutReal lower_y_J = lower_y_avg(J()); - const BoutReal lower_y_g_22 = lower_y_avg(g22()); - const BoutReal lower_y_g23 = lower_y_avg(g23()); - const BoutReal lower_y_g_23 = lower_y_avg(g23()); - const BoutReal lower_y_dy = lower_y_avg(dy); - const BoutReal lower_y_value = -lower_y_A * lower_y_J * lower_y_g23 * lower_y_g_23 - / (lower_y_g_22 * J()[i] * lower_y_dy * dy[i]); - result[i] += lower_y_value * (f[i.ym()] - f[i]); - } - - return result; -#else - throw BoutException("Coordinates::Laplace_perpXY for 3D metric not implemented"); -#endif -} - -const Coordinates::FieldMetric& Coordinates::invSg() const { - if (invSgCache == nullptr) { - auto ptr = std::make_unique(); - (*ptr) = 1.0 / sqrt(g22()); - invSgCache = std::move(ptr); - } - return *invSgCache; -} - -const Coordinates::FieldMetric& -Coordinates::Grad2_par2_DDY_invSg(CELL_LOC outloc, const std::string& method) const { - if (auto search = Grad2_par2_DDY_invSgCache.find(method); - search != Grad2_par2_DDY_invSgCache.end()) { - return *search->second; - } - invSg(); - - // Communicate to get parallel slices - localmesh->communicate(*invSgCache); - invSgCache->applyParallelBoundary("parallel_neumann"); - - // cache - auto ptr = std::make_unique(); - *ptr = DDY(*invSgCache, outloc, method) * invSg(); - Grad2_par2_DDY_invSgCache[method] = std::move(ptr); - return *Grad2_par2_DDY_invSgCache[method]; -} +//Coordinates::FieldMetric Coordinates::DDX(const Field2D& f, CELL_LOC loc, +// const std::string& method, +// const std::string& region) { +// ASSERT1(location == loc || loc == CELL_DEFAULT) +// return bout::derivatives::index::DDX(f, loc, method, region) / dx; +//} +//Field3D Coordinates::DDX(const Field3D& f, CELL_LOC outloc, const std::string& method, +// const std::string& region) { +// auto result = bout::derivatives::index::DDX(f, outloc, method, region); +// result /= dx; +// +// if (f.getMesh()->IncIntShear) { +// // Using BOUT-06 style shifting +// result += IntShiftTorsion * DDZ(f, outloc, method, region); +// } +// +// return result; +//} +// +//Coordinates::FieldMetric Coordinates::DDY(const Field2D& f, CELL_LOC loc, +// const std::string& method, +// const std::string& region) const { +// ASSERT1(location == loc || loc == CELL_DEFAULT) +// return bout::derivatives::index::DDY(f, loc, method, region) / dy; +//} +// +//Field3D Coordinates::DDY(const Field3D& f, CELL_LOC outloc, const std::string& method, +// const std::string& region) const { +//#if BOUT_USE_METRIC_3D +// if (!f.hasParallelSlices() and !transform->canToFromFieldAligned()) { +// Field3D f_parallel = f; +// transform->calcParallelSlices(f_parallel); +// f_parallel.applyParallelBoundary("parallel_neumann"); +// return bout::derivatives::index::DDY(f_parallel, outloc, method, region); +// } +//#endif +// return bout::derivatives::index::DDY(f, outloc, method, region) / dy; +//} +// +//Coordinates::FieldMetric Coordinates::DDZ(const Field2D& f, CELL_LOC loc, +// const std::string& UNUSED(method), +// const std::string& UNUSED(region)) { +// ASSERT1(location == loc || loc == CELL_DEFAULT) +// ASSERT1(f.getMesh() == localmesh) +// if (loc == CELL_DEFAULT) { +// loc = f.getLocation(); +// } +// return zeroFrom(f).setLocation(loc); +//} +//Field3D Coordinates::DDZ(const Field3D& f, CELL_LOC outloc, const std::string& method, +// const std::string& region) { +// return bout::derivatives::index::DDZ(f, outloc, method, region) / dz; +//} +// +/////////////////////////////////////////////////////////// +//// Parallel gradient +// +//Coordinates::FieldMetric Coordinates::Grad_par(const Field2D& var, +// MAYBE_UNUSED(CELL_LOC outloc), +// const std::string& UNUSED(method)) { +// TRACE("Coordinates::Grad_par( Field2D )"); +// ASSERT1(location == outloc || (outloc == CELL_DEFAULT && location == var.getLocation())) +// +// return DDY(var) * invSg(); +//} +// +//Field3D Coordinates::Grad_par(const Field3D& var, CELL_LOC outloc, +// const std::string& method) { +// TRACE("Coordinates::Grad_par( Field3D )"); +// ASSERT1(location == outloc || outloc == CELL_DEFAULT) +// +// return ::DDY(var, outloc, method) * invSg(); +//} +// +/////////////////////////////////////////////////////////// +//// Vpar_Grad_par +//// vparallel times the parallel derivative along unperturbed B-field +// +//Coordinates::FieldMetric Coordinates::Vpar_Grad_par(const Field2D& v, const Field2D& f, +// MAYBE_UNUSED(CELL_LOC outloc), +// const std::string& UNUSED(method)) { +// ASSERT1(location == outloc || (outloc == CELL_DEFAULT && location == f.getLocation())) +// +// return VDDY(v, f) * invSg(); +//} +// +//Field3D Coordinates::Vpar_Grad_par(const Field3D& v, const Field3D& f, CELL_LOC outloc, +// const std::string& method) { +// ASSERT1(location == outloc || outloc == CELL_DEFAULT) +// +// return VDDY(v, f, outloc, method) * invSg(); +//} +// +/////////////////////////////////////////////////////////// +//// Parallel divergence +// +//Coordinates::FieldMetric Coordinates::Div_par(const Field2D& f, CELL_LOC outloc, +// const std::string& method) { +// TRACE("Coordinates::Div_par( Field2D )"); +// ASSERT1(location == outloc || outloc == CELL_DEFAULT) +// +// // Need Bxy at location of f, which might be different from location of this +// // Coordinates object +// auto Bxy_floc = f.getCoordinates()->Bxy(); +// +// return Bxy() * Grad_par(f / Bxy_floc, outloc, method); +//} +// +//Field3D Coordinates::Div_par(const Field3D& f, CELL_LOC outloc, +// const std::string& method) { +// TRACE("Coordinates::Div_par( Field3D )"); +// ASSERT1(location == outloc || outloc == CELL_DEFAULT) +// +// // Need Bxy at location of f, which might be different from location of this +// // Coordinates object +// auto Bxy_floc = f.getCoordinates()->Bxy(); +// +// if (!f.hasParallelSlices()) { +// // No yup/ydown fields. The Grad_par operator will +// // shift to field aligned coordinates +// return Bxy() * Grad_par(f / Bxy_floc, outloc, method); +// } +// +// // Need to modify yup and ydown fields +// Field3D f_B = f / Bxy_floc; +// f_B.splitParallelSlices(); +// for (int i = 0; i < f.getMesh()->ystart; ++i) { +// f_B.yup(i) = f.yup(i) / Bxy_floc.yup(i); +// f_B.ydown(i) = f.ydown(i) / Bxy_floc.ydown(i); +// } +// return Bxy() * Grad_par(f_B, outloc, method); +//} +// +/////////////////////////////////////////////////////////// +//// second parallel derivative (b dot Grad)(b dot Grad) +//// Note: For parallel Laplacian use Laplace_par +// +//Coordinates::FieldMetric Coordinates::Grad2_par2(const Field2D& f, CELL_LOC outloc, +// const std::string& method) { +// TRACE("Coordinates::Grad2_par2( Field2D )"); +// ASSERT1(location == outloc || (outloc == CELL_DEFAULT && location == f.getLocation())) +// +// auto result = Grad2_par2_DDY_invSg(outloc, method) * DDY(f, outloc, method) +// + D2DY2(f, outloc, method) / g22(); +// +// return result; +//} +// +//Field3D Coordinates::Grad2_par2(const Field3D& f, CELL_LOC outloc, +// const std::string& method) { +// TRACE("Coordinates::Grad2_par2( Field3D )"); +// if (outloc == CELL_DEFAULT) { +// outloc = f.getLocation(); +// } +// ASSERT1(location == outloc) +// +// Field3D result = ::DDY(f, outloc, method); +// +// Field3D r2 = D2DY2(f, outloc, method) / g22(); +// +// result = Grad2_par2_DDY_invSg(outloc, method) * result + r2; +// +// ASSERT2(result.getLocation() == outloc) +// +// return result; +//} +// +/////////////////////////////////////////////////////////// +//// perpendicular Laplacian operator +// +//#include // Delp2 uses same coefficients as inversion code +// +//Coordinates::FieldMetric Coordinates::Delp2(const Field2D& f, CELL_LOC outloc, +// bool UNUSED(useFFT)) { +// TRACE("Coordinates::Delp2( Field2D )"); +// ASSERT1(location == outloc || outloc == CELL_DEFAULT) +// +// auto result = G1 * DDX(f, outloc) + g11() * D2DX2(f, outloc); +// +// return result; +//} +// +//Field3D Coordinates::Delp2(const Field3D& f, CELL_LOC outloc, bool useFFT) { +// TRACE("Coordinates::Delp2( Field3D )"); +// +// if (outloc == CELL_DEFAULT) { +// outloc = f.getLocation(); +// } +// +// ASSERT1(location == outloc) +// ASSERT1(f.getLocation() == outloc) +// +// if (localmesh->GlobalNx == 1 && localmesh->GlobalNz == 1) { +// // copy mesh, location, etc +// return f * 0; +// } +// ASSERT2(localmesh->xstart > 0) // Need at least one guard cell +// +// Field3D result{emptyFrom(f).setLocation(outloc)}; +// +// if (useFFT and not bout::build::use_metric_3d) { +// int ncz = localmesh->LocalNz; +// +// // Allocate memory +// auto ft = Matrix(localmesh->LocalNx, ncz / 2 + 1); +// auto delft = Matrix(localmesh->LocalNx, ncz / 2 + 1); +// +// // Loop over y indices +// // Note: should not include y-guard or y-boundary points here as that would +// // use values from corner cells in dx, which may not be initialised. +// for (int jy = localmesh->ystart; jy <= localmesh->yend; jy++) { +// +// // Take forward FFT +// +// for (int jx = 0; jx < localmesh->LocalNx; jx++) { +// rfft(&f(jx, jy, 0), ncz, &ft(jx, 0)); +// } +// +// // Loop over kz +// for (int jz = 0; jz <= ncz / 2; jz++) { +// +// // No smoothing in the x direction +// for (int jx = localmesh->xstart; jx <= localmesh->xend; jx++) { +// // Perform x derivative +// +// dcomplex a, b, c; +// laplace_tridag_coefs(jx, jy, jz, a, b, c, nullptr, nullptr, outloc); +// +// delft(jx, jz) = a * ft(jx - 1, jz) + b * ft(jx, jz) + c * ft(jx + 1, jz); +// } +// } +// +// // Reverse FFT +// for (int jx = localmesh->xstart; jx <= localmesh->xend; jx++) { +// +// irfft(&delft(jx, 0), ncz, &result(jx, jy, 0)); +// } +// } +// } else { +// result = G1 * ::DDX(f, outloc) + G3 * ::DDZ(f, outloc) + g11() * ::D2DX2(f, outloc) +// + g33() * ::D2DZ2(f, outloc) + 2 * g13() * ::D2DXDZ(f, outloc); +// } +// +// ASSERT2(result.getLocation() == outloc) +// +// return result; +//} +// +//FieldPerp Coordinates::Delp2(const FieldPerp& f, CELL_LOC outloc, bool useFFT) { +// TRACE("Coordinates::Delp2( FieldPerp )"); +// +// if (outloc == CELL_DEFAULT) { +// outloc = f.getLocation(); +// } +// +// ASSERT1(location == outloc) +// ASSERT1(f.getLocation() == outloc) +// +// if (localmesh->GlobalNx == 1 && localmesh->GlobalNz == 1) { +// // copy mesh, location, etc +// return f * 0; +// } +// ASSERT2(localmesh->xstart > 0) // Need at least one guard cell +// +// FieldPerp result{emptyFrom(f).setLocation(outloc)}; +// +// int jy = f.getIndex(); +// result.setIndex(jy); +// +// if (useFFT) { +// int ncz = localmesh->LocalNz; +// +// // Allocate memory +// auto ft = Matrix(localmesh->LocalNx, ncz / 2 + 1); +// auto delft = Matrix(localmesh->LocalNx, ncz / 2 + 1); +// +// // Take forward FFT +// for (int jx = 0; jx < localmesh->LocalNx; jx++) { +// rfft(&f(jx, 0), ncz, &ft(jx, 0)); +// } +// +// // Loop over kz +// for (int jz = 0; jz <= ncz / 2; jz++) { +// +// // No smoothing in the x direction +// for (int jx = localmesh->xstart; jx <= localmesh->xend; jx++) { +// // Perform x derivative +// +// dcomplex a, b, c; +// laplace_tridag_coefs(jx, jy, jz, a, b, c); +// +// delft(jx, jz) = a * ft(jx - 1, jz) + b * ft(jx, jz) + c * ft(jx + 1, jz); +// } +// } +// +// // Reverse FFT +// for (int jx = localmesh->xstart; jx <= localmesh->xend; jx++) { +// irfft(&delft(jx, 0), ncz, &result(jx, 0)); +// } +// +// } else { +// throw BoutException("Non-fourier Delp2 not currently implented for FieldPerp."); +// // Would be the following but don't have standard derivative operators for FieldPerps +// // yet +// // result = G1 * ::DDX(f, outloc) + G3 * ::DDZ(f, outloc) + g11 * ::D2DX2(f, outloc) +// // + g33 * ::D2DZ2(f, outloc) + 2 * g13 * ::D2DXDZ(f, outloc); +// } +// +// return result; +//} +// +//Coordinates::FieldMetric Coordinates::Laplace_par(const Field2D& f, CELL_LOC outloc) { +// ASSERT1(location == outloc || outloc == CELL_DEFAULT) +// +// return D2DY2(f, outloc) / g22() + DDY(J() / g22(), outloc) * DDY(f, outloc) / J(); +//} +// +//Field3D Coordinates::Laplace_par(const Field3D& f, CELL_LOC outloc) { +// ASSERT1(location == outloc || outloc == CELL_DEFAULT) +// return D2DY2(f, outloc) / g22() + DDY(J() / g22(), outloc) * ::DDY(f, outloc) / J(); +//} +// +//// Full Laplacian operator on scalar field +// +//Coordinates::FieldMetric Coordinates::Laplace(const Field2D& f, CELL_LOC outloc, +// const std::string& dfdy_boundary_conditions, +// const std::string& dfdy_dy_region) { +// TRACE("Coordinates::Laplace( Field2D )"); +// ASSERT1(location == outloc || outloc == CELL_DEFAULT) +// +// auto result = G1 * DDX(f, outloc) + G2 * DDY(f, outloc) + g11() * D2DX2(f, outloc) +// + g22() * D2DY2(f, outloc) +// + 2.0 * g12() +// * D2DXDY(f, outloc, "DEFAULT", "RGN_NOBNDRY", +// dfdy_boundary_conditions, dfdy_dy_region); +// +// return result; +//} +// +//Field3D Coordinates::Laplace(const Field3D& f, CELL_LOC outloc, +// const std::string& dfdy_boundary_conditions, +// const std::string& dfdy_dy_region) { +// TRACE("Coordinates::Laplace( Field3D )"); +// ASSERT1(location == outloc || outloc == CELL_DEFAULT) +// +// Field3D result = G1 * ::DDX(f, outloc) + G2 * ::DDY(f, outloc) + G3 * ::DDZ(f, outloc) +// + g11() * D2DX2(f, outloc) + g22() * D2DY2(f, outloc) +// + g33() * D2DZ2(f, outloc) +// + 2.0 +// * (g12() +// * D2DXDY(f, outloc, "DEFAULT", "RGN_NOBNDRY", +// dfdy_boundary_conditions, dfdy_dy_region) +// + g13() * D2DXDZ(f, outloc) + g23() * D2DYDZ(f, outloc)); +// +// return result; +//} +// +//// Full perpendicular Laplacian, in form of inverse of Laplacian operator in LaplaceXY +//// solver +//Field2D Coordinates::Laplace_perpXY(MAYBE_UNUSED(const Field2D& A), +// MAYBE_UNUSED(const Field2D& f)) { +// TRACE("Coordinates::Laplace_perpXY( Field2D )"); +//#if not(BOUT_USE_METRIC_3D) +// Field2D result; +// result.allocate(); +// for (auto i : result.getRegion(RGN_NOBNDRY)) { +// result[i] = 0.; +// +// // outer x boundary +// const auto outer_x_avg = [&i](const auto& f) { return 0.5 * (f[i] + f[i.xp()]); }; +// const BoutReal outer_x_A = outer_x_avg(A); +// const BoutReal outer_x_J = outer_x_avg(J()); +// const BoutReal outer_x_g11 = outer_x_avg(g11()); +// const BoutReal outer_x_dx = outer_x_avg(dx); +// const BoutReal outer_x_value = +// outer_x_A * outer_x_J * outer_x_g11 / (J()[i] * outer_x_dx * dx[i]); +// result[i] += outer_x_value * (f[i.xp()] - f[i]); +// +// // inner x boundary +// const auto inner_x_avg = [&i](const auto& f) { return 0.5 * (f[i] + f[i.xm()]); }; +// const BoutReal inner_x_A = inner_x_avg(A); +// const BoutReal inner_x_J = inner_x_avg(J()); +// const BoutReal inner_x_g11 = inner_x_avg(g11()); +// const BoutReal inner_x_dx = inner_x_avg(dx); +// const BoutReal inner_x_value = +// inner_x_A * inner_x_J * inner_x_g11 / (J()[i] * inner_x_dx * dx[i]); +// result[i] += inner_x_value * (f[i.xm()] - f[i]); +// +// // upper y boundary +// const auto upper_y_avg = [&i](const auto& f) { return 0.5 * (f[i] + f[i.yp()]); }; +// const BoutReal upper_y_A = upper_y_avg(A); +// const BoutReal upper_y_J = upper_y_avg(J()); +// const BoutReal upper_y_g_22 = upper_y_avg(g22()); +// const BoutReal upper_y_g23 = upper_y_avg(g23()); +// const BoutReal upper_y_g_23 = upper_y_avg(g23()); +// const BoutReal upper_y_dy = upper_y_avg(dy); +// const BoutReal upper_y_value = -upper_y_A * upper_y_J * upper_y_g23 * upper_y_g_23 +// / (upper_y_g_22 * J()[i] * upper_y_dy * dy[i]); +// result[i] += upper_y_value * (f[i.yp()] - f[i]); +// +// // lower y boundary +// const auto lower_y_avg = [&i](const auto& f) { return 0.5 * (f[i] + f[i.ym()]); }; +// const BoutReal lower_y_A = lower_y_avg(A); +// const BoutReal lower_y_J = lower_y_avg(J()); +// const BoutReal lower_y_g_22 = lower_y_avg(g22()); +// const BoutReal lower_y_g23 = lower_y_avg(g23()); +// const BoutReal lower_y_g_23 = lower_y_avg(g23()); +// const BoutReal lower_y_dy = lower_y_avg(dy); +// const BoutReal lower_y_value = -lower_y_A * lower_y_J * lower_y_g23 * lower_y_g_23 +// / (lower_y_g_22 * J()[i] * lower_y_dy * dy[i]); +// result[i] += lower_y_value * (f[i.ym()] - f[i]); +// } +// +// return result; +//#else +// throw BoutException("Coordinates::Laplace_perpXY for 3D metric not implemented"); +//#endif +//} +// +//const Coordinates::FieldMetric& Coordinates::invSg() const { +// if (invSgCache == nullptr) { +// auto ptr = std::make_unique(); +// (*ptr) = 1.0 / sqrt(g22()); +// invSgCache = std::move(ptr); +// } +// return *invSgCache; +//} +// +//const Coordinates::FieldMetric& +//Coordinates::Grad2_par2_DDY_invSg(CELL_LOC outloc, const std::string& method) const { +// if (auto search = Grad2_par2_DDY_invSgCache.find(method); +// search != Grad2_par2_DDY_invSgCache.end()) { +// return *search->second; +// } +// invSg(); +// +// // Communicate to get parallel slices +// localmesh->communicate(*invSgCache); +// invSgCache->applyParallelBoundary("parallel_neumann"); +// +// // cache +// auto ptr = std::make_unique(); +// *ptr = DDY(*invSgCache, outloc, method) * invSg(); +// Grad2_par2_DDY_invSgCache[method] = std::move(ptr); +// return *Grad2_par2_DDY_invSgCache[method]; +//} void Coordinates::checkCovariant() { return geometry.checkCovariant(localmesh->ystart); } diff --git a/src/mesh/differential_operators.cxx b/src/mesh/differential_operators.cxx new file mode 100644 index 0000000000..edda63a747 --- /dev/null +++ b/src/mesh/differential_operators.cxx @@ -0,0 +1,463 @@ + +#include "bout/differential_operators.hxx" + +DifferentialOperators::DifferentialOperators(CELL_LOC& location, FieldMetric& dx, + FieldMetric& dy, FieldMetric& dz) + : location(location), dx(dx), dy(dy), dz(dz) {} + +DifferentialOperators::FieldMetric DifferentialOperators::DDX(const Field2D& f, + CELL_LOC loc, + const std::string& method, + const std::string& region) { + ASSERT1(location == loc || loc == CELL_DEFAULT) + return bout::derivatives::index::DDX(f, loc, method, region) / dx; +} +Field3D DifferentialOperators::DDX(const Field3D& f, CELL_LOC outloc, + const std::string& method, const std::string& region) { + auto result = bout::derivatives::index::DDX(f, outloc, method, region); + result /= dx; + + if (f.getMesh()->IncIntShear) { + // Using BOUT-06 style shifting + result += IntShiftTorsion * DDZ(f, outloc, method, region); + } + + return result; +} + +DifferentialOperators::FieldMetric +DifferentialOperators::DDY(const Field2D& f, CELL_LOC loc, const std::string& method, + const std::string& region) const { + ASSERT1(location == loc || loc == CELL_DEFAULT) + return bout::derivatives::index::DDY(f, loc, method, region) / dy; +} + +Field3D DifferentialOperators::DDY(const Field3D& f, CELL_LOC outloc, + const std::string& method, + const std::string& region) const { +#if BOUT_USE_METRIC_3D + if (!f.hasParallelSlices() and !transform->canToFromFieldAligned()) { + Field3D f_parallel = f; + transform->calcParallelSlices(f_parallel); + f_parallel.applyParallelBoundary("parallel_neumann"); + return bout::derivatives::index::DDY(f_parallel, outloc, method, region); + } +#endif + return bout::derivatives::index::DDY(f, outloc, method, region) / dy; +} + +DifferentialOperators::FieldMetric +DifferentialOperators::DDZ(const Field2D& f, CELL_LOC loc, + const std::string& UNUSED(method), + const std::string& UNUSED(region)) { + ASSERT1(location == loc || loc == CELL_DEFAULT) + ASSERT1(f.getMesh() == localmesh) + if (loc == CELL_DEFAULT) { + loc = f.getLocation(); + } + return zeroFrom(f).setLocation(loc); +} +Field3D DifferentialOperators::DDZ(const Field3D& f, CELL_LOC outloc, + const std::string& method, const std::string& region) { + return bout::derivatives::index::DDZ(f, outloc, method, region) / dz; +} + +///////////////////////////////////////////////////////// +// Parallel gradient + +DifferentialOperators::FieldMetric +DifferentialOperators::Grad_par(const Field2D& var, [[maybe_unused]] CELL_LOC outloc, + const std::string& UNUSED(method)) { + TRACE("DifferentialOperators::Grad_par( Field2D )"); + ASSERT1(location == outloc || (outloc == CELL_DEFAULT && location == var.getLocation())) + + return DDY(var) * invSg(); +} + +Field3D DifferentialOperators::Grad_par(const Field3D& var, CELL_LOC outloc, + const std::string& method) { + TRACE("DifferentialOperators::Grad_par( Field3D )"); + ASSERT1(location == outloc || outloc == CELL_DEFAULT) + + return ::DDY(var, outloc, method) * invSg(); +} + +///////////////////////////////////////////////////////// +// Vpar_Grad_par +// vparallel times the parallel derivative along unperturbed B-field + +DifferentialOperators::FieldMetric +DifferentialOperators::Vpar_Grad_par(const Field2D& v, const Field2D& f, + [[maybe_unused]] CELL_LOC outloc, + const std::string& UNUSED(method)) { + ASSERT1(location == outloc || (outloc == CELL_DEFAULT && location == f.getLocation())) + + return VDDY(v, f) * invSg(); +} + +Field3D DifferentialOperators::Vpar_Grad_par(const Field3D& v, const Field3D& f, + CELL_LOC outloc, const std::string& method) { + ASSERT1(location == outloc || outloc == CELL_DEFAULT) + + return VDDY(v, f, outloc, method) * invSg(); +} + +///////////////////////////////////////////////////////// +// Parallel divergence + +DifferentialOperators::FieldMetric +DifferentialOperators::Div_par(const Field2D& f, CELL_LOC outloc, + const std::string& method) { + TRACE("DifferentialOperators::Div_par( Field2D )"); + ASSERT1(location == outloc || outloc == CELL_DEFAULT) + + // Need Bxy at location of f, which might be different from location of this + // Coordinates object + auto Bxy_floc = f.getCoordinates()->Bxy(); + + return Bxy() * Grad_par(f / Bxy_floc, outloc, method); +} + +Field3D DifferentialOperators::Div_par(const Field3D& f, CELL_LOC outloc, + const std::string& method) { + TRACE("DifferentialOperators::Div_par( Field3D )"); + ASSERT1(location == outloc || outloc == CELL_DEFAULT) + + // Need Bxy at location of f, which might be different from location of this + // Coordinates object + auto Bxy_floc = f.getCoordinates()->Bxy(); + + if (!f.hasParallelSlices()) { + // No yup/ydown fields. The Grad_par operator will + // shift to field aligned coordinates + return Bxy() * Grad_par(f / Bxy_floc, outloc, method); + } + + // Need to modify yup and ydown fields + Field3D f_B = f / Bxy_floc; + f_B.splitParallelSlices(); + for (int i = 0; i < f.getMesh()->ystart; ++i) { + f_B.yup(i) = f.yup(i) / Bxy_floc.yup(i); + f_B.ydown(i) = f.ydown(i) / Bxy_floc.ydown(i); + } + return Bxy() * Grad_par(f_B, outloc, method); +} + +///////////////////////////////////////////////////////// +// second parallel derivative (b dot Grad)(b dot Grad) +// Note: For parallel Laplacian use Laplace_par + +DifferentialOperators::FieldMetric +DifferentialOperators::Grad2_par2(const Field2D& f, CELL_LOC outloc, + const std::string& method) { + TRACE("DifferentialOperators::Grad2_par2( Field2D )"); + ASSERT1(location == outloc || (outloc == CELL_DEFAULT && location == f.getLocation())) + + auto result = Grad2_par2_DDY_invSg(outloc, method) * DDY(f, outloc, method) + + D2DY2(f, outloc, method) / g22(); + + return result; +} + +Field3D DifferentialOperators::Grad2_par2(const Field3D& f, CELL_LOC outloc, + const std::string& method) { + TRACE("DifferentialOperators::Grad2_par2( Field3D )"); + if (outloc == CELL_DEFAULT) { + outloc = f.getLocation(); + } + ASSERT1(location == outloc) + + Field3D result = ::DDY(f, outloc, method); + + Field3D r2 = D2DY2(f, outloc, method) / g22(); + + result = Grad2_par2_DDY_invSg(outloc, method) * result + r2; + + ASSERT2(result.getLocation() == outloc) + + return result; +} + +///////////////////////////////////////////////////////// +// perpendicular Laplacian operator + +#include // Delp2 uses same coefficients as inversion code + +DifferentialOperators::FieldMetric +DifferentialOperators::Delp2(const Field2D& f, CELL_LOC outloc, bool UNUSED(useFFT)) { + TRACE("DifferentialOperators::Delp2( Field2D )"); + ASSERT1(location == outloc || outloc == CELL_DEFAULT) + + auto result = G1 * DDX(f, outloc) + g11() * D2DX2(f, outloc); + + return result; +} + +Field3D DifferentialOperators::Delp2(const Field3D& f, CELL_LOC outloc, bool useFFT) { + TRACE("DifferentialOperators::Delp2( Field3D )"); + + if (outloc == CELL_DEFAULT) { + outloc = f.getLocation(); + } + + ASSERT1(location == outloc) + ASSERT1(f.getLocation() == outloc) + + if (localmesh->GlobalNx == 1 && localmesh->GlobalNz == 1) { + // copy mesh, location, etc + return f * 0; + } + ASSERT2(localmesh->xstart > 0) // Need at least one guard cell + + Field3D result{emptyFrom(f).setLocation(outloc)}; + + if (useFFT and not bout::build::use_metric_3d) { + int ncz = localmesh->LocalNz; + + // Allocate memory + auto ft = Matrix(localmesh->LocalNx, ncz / 2 + 1); + auto delft = Matrix(localmesh->LocalNx, ncz / 2 + 1); + + // Loop over y indices + // Note: should not include y-guard or y-boundary points here as that would + // use values from corner cells in dx, which may not be initialised. + for (int jy = localmesh->ystart; jy <= localmesh->yend; jy++) { + + // Take forward FFT + + for (int jx = 0; jx < localmesh->LocalNx; jx++) { + rfft(&f(jx, jy, 0), ncz, &ft(jx, 0)); + } + + // Loop over kz + for (int jz = 0; jz <= ncz / 2; jz++) { + + // No smoothing in the x direction + for (int jx = localmesh->xstart; jx <= localmesh->xend; jx++) { + // Perform x derivative + + dcomplex a, b, c; + laplace_tridag_coefs(jx, jy, jz, a, b, c, nullptr, nullptr, outloc); + + delft(jx, jz) = a * ft(jx - 1, jz) + b * ft(jx, jz) + c * ft(jx + 1, jz); + } + } + + // Reverse FFT + for (int jx = localmesh->xstart; jx <= localmesh->xend; jx++) { + + irfft(&delft(jx, 0), ncz, &result(jx, jy, 0)); + } + } + } else { + result = G1 * ::DDX(f, outloc) + G3 * ::DDZ(f, outloc) + g11() * ::D2DX2(f, outloc) + + g33() * ::D2DZ2(f, outloc) + 2 * g13() * ::D2DXDZ(f, outloc); + } + + ASSERT2(result.getLocation() == outloc) + + return result; +} + +FieldPerp DifferentialOperators::Delp2(const FieldPerp& f, CELL_LOC outloc, bool useFFT) { + TRACE("DifferentialOperators::Delp2( FieldPerp )"); + + if (outloc == CELL_DEFAULT) { + outloc = f.getLocation(); + } + + ASSERT1(location == outloc) + ASSERT1(f.getLocation() == outloc) + + if (localmesh->GlobalNx == 1 && localmesh->GlobalNz == 1) { + // copy mesh, location, etc + return f * 0; + } + ASSERT2(localmesh->xstart > 0) // Need at least one guard cell + + FieldPerp result{emptyFrom(f).setLocation(outloc)}; + + int jy = f.getIndex(); + result.setIndex(jy); + + if (useFFT) { + int ncz = localmesh->LocalNz; + + // Allocate memory + auto ft = Matrix(localmesh->LocalNx, ncz / 2 + 1); + auto delft = Matrix(localmesh->LocalNx, ncz / 2 + 1); + + // Take forward FFT + for (int jx = 0; jx < localmesh->LocalNx; jx++) { + rfft(&f(jx, 0), ncz, &ft(jx, 0)); + } + + // Loop over kz + for (int jz = 0; jz <= ncz / 2; jz++) { + + // No smoothing in the x direction + for (int jx = localmesh->xstart; jx <= localmesh->xend; jx++) { + // Perform x derivative + + dcomplex a, b, c; + laplace_tridag_coefs(jx, jy, jz, a, b, c); + + delft(jx, jz) = a * ft(jx - 1, jz) + b * ft(jx, jz) + c * ft(jx + 1, jz); + } + } + + // Reverse FFT + for (int jx = localmesh->xstart; jx <= localmesh->xend; jx++) { + irfft(&delft(jx, 0), ncz, &result(jx, 0)); + } + + } else { + throw BoutException("Non-fourier Delp2 not currently implented for FieldPerp."); + // Would be the following but don't have standard derivative operators for FieldPerps + // yet + // result = G1 * ::DDX(f, outloc) + G3 * ::DDZ(f, outloc) + g11 * ::D2DX2(f, outloc) + // + g33 * ::D2DZ2(f, outloc) + 2 * g13 * ::D2DXDZ(f, outloc); + } + + return result; +} + +DifferentialOperators::FieldMetric DifferentialOperators::Laplace_par(const Field2D& f, + CELL_LOC outloc) { + ASSERT1(location == outloc || outloc == CELL_DEFAULT) + + return D2DY2(f, outloc) / g22() + DDY(J() / g22(), outloc) * DDY(f, outloc) / J(); +} + +Field3D DifferentialOperators::Laplace_par(const Field3D& f, CELL_LOC outloc) { + ASSERT1(location == outloc || outloc == CELL_DEFAULT) + return D2DY2(f, outloc) / g22() + DDY(J() / g22(), outloc) * ::DDY(f, outloc) / J(); +} + +// Full Laplacian operator on scalar field + +DifferentialOperators::FieldMetric +DifferentialOperators::Laplace(const Field2D& f, CELL_LOC outloc, + const std::string& dfdy_boundary_conditions, + const std::string& dfdy_dy_region) { + TRACE("DifferentialOperators::Laplace( Field2D )"); + ASSERT1(location == outloc || outloc == CELL_DEFAULT) + + auto result = G1 * DDX(f, outloc) + G2 * DDY(f, outloc) + g11() * D2DX2(f, outloc) + + g22() * D2DY2(f, outloc) + + 2.0 * g12() + * D2DXDY(f, outloc, "DEFAULT", "RGN_NOBNDRY", + dfdy_boundary_conditions, dfdy_dy_region); + + return result; +} + +Field3D DifferentialOperators::Laplace(const Field3D& f, CELL_LOC outloc, + const std::string& dfdy_boundary_conditions, + const std::string& dfdy_dy_region) { + TRACE("DifferentialOperators::Laplace( Field3D )"); + ASSERT1(location == outloc || outloc == CELL_DEFAULT) + + Field3D result = G1 * ::DDX(f, outloc) + G2 * ::DDY(f, outloc) + G3 * ::DDZ(f, outloc) + + g11() * D2DX2(f, outloc) + g22() * D2DY2(f, outloc) + + g33() * D2DZ2(f, outloc) + + 2.0 + * (g12() + * D2DXDY(f, outloc, "DEFAULT", "RGN_NOBNDRY", + dfdy_boundary_conditions, dfdy_dy_region) + + g13() * D2DXDZ(f, outloc) + g23() * D2DYDZ(f, outloc)); + + return result; +} + +// Full perpendicular Laplacian, in form of inverse of Laplacian operator in LaplaceXY +// solver +Field2D DifferentialOperators::Laplace_perpXY([[maybe_unused]] const Field2D& A, + [[maybe_unused]] const Field2D& f) { + TRACE("DifferentialOperators::Laplace_perpXY( Field2D )"); +#if not(BOUT_USE_METRIC_3D) + Field2D result; + result.allocate(); + for (auto i : result.getRegion(RGN_NOBNDRY)) { + result[i] = 0.; + + // outer x boundary + const auto outer_x_avg = [&i](const auto& f) { return 0.5 * (f[i] + f[i.xp()]); }; + const BoutReal outer_x_A = outer_x_avg(A); + const BoutReal outer_x_J = outer_x_avg(J()); + const BoutReal outer_x_g11 = outer_x_avg(g11()); + const BoutReal outer_x_dx = outer_x_avg(dx); + const BoutReal outer_x_value = + outer_x_A * outer_x_J * outer_x_g11 / (J()[i] * outer_x_dx * dx[i]); + result[i] += outer_x_value * (f[i.xp()] - f[i]); + + // inner x boundary + const auto inner_x_avg = [&i](const auto& f) { return 0.5 * (f[i] + f[i.xm()]); }; + const BoutReal inner_x_A = inner_x_avg(A); + const BoutReal inner_x_J = inner_x_avg(J()); + const BoutReal inner_x_g11 = inner_x_avg(g11()); + const BoutReal inner_x_dx = inner_x_avg(dx); + const BoutReal inner_x_value = + inner_x_A * inner_x_J * inner_x_g11 / (J()[i] * inner_x_dx * dx[i]); + result[i] += inner_x_value * (f[i.xm()] - f[i]); + + // upper y boundary + const auto upper_y_avg = [&i](const auto& f) { return 0.5 * (f[i] + f[i.yp()]); }; + const BoutReal upper_y_A = upper_y_avg(A); + const BoutReal upper_y_J = upper_y_avg(J()); + const BoutReal upper_y_g_22 = upper_y_avg(g22()); + const BoutReal upper_y_g23 = upper_y_avg(g23()); + const BoutReal upper_y_g_23 = upper_y_avg(g23()); + const BoutReal upper_y_dy = upper_y_avg(dy); + const BoutReal upper_y_value = -upper_y_A * upper_y_J * upper_y_g23 * upper_y_g_23 + / (upper_y_g_22 * J()[i] * upper_y_dy * dy[i]); + result[i] += upper_y_value * (f[i.yp()] - f[i]); + + // lower y boundary + const auto lower_y_avg = [&i](const auto& f) { return 0.5 * (f[i] + f[i.ym()]); }; + const BoutReal lower_y_A = lower_y_avg(A); + const BoutReal lower_y_J = lower_y_avg(J()); + const BoutReal lower_y_g_22 = lower_y_avg(g22()); + const BoutReal lower_y_g23 = lower_y_avg(g23()); + const BoutReal lower_y_g_23 = lower_y_avg(g23()); + const BoutReal lower_y_dy = lower_y_avg(dy); + const BoutReal lower_y_value = -lower_y_A * lower_y_J * lower_y_g23 * lower_y_g_23 + / (lower_y_g_22 * J()[i] * lower_y_dy * dy[i]); + result[i] += lower_y_value * (f[i.ym()] - f[i]); + } + + return result; +#else + throw BoutException( + "DifferentialOperators::Laplace_perpXY for 3D metric not implemented"); +#endif +} + +const DifferentialOperators::FieldMetric& DifferentialOperators::invSg() const { + if (invSgCache == nullptr) { + auto ptr = std::make_unique(); + (*ptr) = 1.0 / sqrt(g22()); + invSgCache = std::move(ptr); + } + return *invSgCache; +} + +const DifferentialOperators::FieldMetric& +DifferentialOperators::Grad2_par2_DDY_invSg(CELL_LOC outloc, + const std::string& method) const { + if (auto search = Grad2_par2_DDY_invSgCache.find(method); + search != Grad2_par2_DDY_invSgCache.end()) { + return *search->second; + } + invSg(); + + // Communicate to get parallel slices + localmesh->communicate(*invSgCache); + invSgCache->applyParallelBoundary("parallel_neumann"); + + // cache + auto ptr = std::make_unique(); + *ptr = DDY(*invSgCache, outloc, method) * invSg(); + Grad2_par2_DDY_invSgCache[method] = std::move(ptr); + return *Grad2_par2_DDY_invSgCache[method]; +} From 10178663549b7d021d2040204c5e525c19b0a43f Mon Sep 17 00:00:00 2001 From: tomc271 Date: Sun, 26 Nov 2023 10:26:00 +0000 Subject: [PATCH 181/491] Add field 'mesh' to DifferentialOperators. --- include/bout/differential_operators.hxx | 3 ++- src/mesh/differential_operators.cxx | 9 +++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/include/bout/differential_operators.hxx b/include/bout/differential_operators.hxx index 9ad89127a8..669bc49458 100644 --- a/include/bout/differential_operators.hxx +++ b/include/bout/differential_operators.hxx @@ -17,7 +17,7 @@ public: using FieldMetric = Field2D; #endif - DifferentialOperators(CELL_LOC& location, FieldMetric& dx, FieldMetric& dy, + DifferentialOperators(Mesh* mesh, CELL_LOC& location, FieldMetric& dx, FieldMetric& dy, FieldMetric& dz); FieldMetric DDX(const Field2D& f, CELL_LOC outloc = CELL_DEFAULT, @@ -98,6 +98,7 @@ public: Field2D Laplace_perpXY(const Field2D& A, const Field2D& f); private: + Mesh* mesh; CELL_LOC& location; FieldMetric& dx; FieldMetric& dy; diff --git a/src/mesh/differential_operators.cxx b/src/mesh/differential_operators.cxx index edda63a747..c2bb5aaa0c 100644 --- a/src/mesh/differential_operators.cxx +++ b/src/mesh/differential_operators.cxx @@ -1,9 +1,10 @@ #include "bout/differential_operators.hxx" -DifferentialOperators::DifferentialOperators(CELL_LOC& location, FieldMetric& dx, - FieldMetric& dy, FieldMetric& dz) - : location(location), dx(dx), dy(dy), dz(dz) {} +DifferentialOperators::DifferentialOperators(Mesh* mesh, CELL_LOC& location, + FieldMetric& dx, FieldMetric& dy, + FieldMetric& dz) + : mesh(mesh), location(location), dx(dx), dy(dy), dz(dz) {} DifferentialOperators::FieldMetric DifferentialOperators::DDX(const Field2D& f, CELL_LOC loc, @@ -51,7 +52,7 @@ DifferentialOperators::DDZ(const Field2D& f, CELL_LOC loc, const std::string& UNUSED(method), const std::string& UNUSED(region)) { ASSERT1(location == loc || loc == CELL_DEFAULT) - ASSERT1(f.getMesh() == localmesh) + ASSERT1(f.getMesh() == mesh) if (loc == CELL_DEFAULT) { loc = f.getLocation(); } From a64af0b42bfba24e92a42cc561fb90a17a046cad Mon Sep 17 00:00:00 2001 From: tomc271 Date: Sun, 26 Nov 2023 10:51:25 +0000 Subject: [PATCH 182/491] More fixes. Pass IntShiftTorsion to DifferentialOperators constructor. --- include/bout/differential_operators.hxx | 6 +- include/bout/geometry.hxx | 7 +- src/mesh/coordinates.cxx | 8 +- src/mesh/differential_operators.cxx | 12 +- src/mesh/geometry.cxx | 265 +++++++++++++----------- 5 files changed, 168 insertions(+), 130 deletions(-) diff --git a/include/bout/differential_operators.hxx b/include/bout/differential_operators.hxx index 669bc49458..0539dde238 100644 --- a/include/bout/differential_operators.hxx +++ b/include/bout/differential_operators.hxx @@ -17,7 +17,8 @@ public: using FieldMetric = Field2D; #endif - DifferentialOperators(Mesh* mesh, CELL_LOC& location, FieldMetric& dx, FieldMetric& dy, + DifferentialOperators(Mesh* mesh, FieldMetric& intShiftTorsion, + const CELL_LOC& location, FieldMetric& dx, FieldMetric& dy, FieldMetric& dz); FieldMetric DDX(const Field2D& f, CELL_LOC outloc = CELL_DEFAULT, @@ -99,7 +100,8 @@ public: private: Mesh* mesh; - CELL_LOC& location; + FieldMetric& intShiftTorsion; + const CELL_LOC& location; FieldMetric& dx; FieldMetric& dy; FieldMetric& dz; diff --git a/include/bout/geometry.hxx b/include/bout/geometry.hxx index 60ceb91f94..acaef80545 100644 --- a/include/bout/geometry.hxx +++ b/include/bout/geometry.hxx @@ -34,6 +34,7 @@ #define __GEOMETRY_H__ #include "coordinates.hxx" +#include "differential_operators.hxx" #include "metricTensor.hxx" #include "bout/field2d.hxx" #include "bout/field3d.hxx" @@ -62,9 +63,9 @@ public: FieldMetric J, FieldMetric Bxy, FieldMetric g11, FieldMetric g22, FieldMetric g33, FieldMetric g12, FieldMetric g13, FieldMetric g23, FieldMetric g_11, FieldMetric g_22, FieldMetric g_33, FieldMetric g_12, FieldMetric g_13, - FieldMetric g_23); + FieldMetric g_23, DifferentialOperators differential_operators); - Geometry(Mesh* mesh, CELL_LOC cell_location); + Geometry(Mesh* mesh, DifferentialOperators differential_operators); /// Christoffel symbol of the second kind (connection coefficients) FieldMetric G1_11, G1_22, G1_33, G1_12, G1_13, G1_23; @@ -145,6 +146,8 @@ private: FieldMetric this_J; FieldMetric this_Bxy; ///< Magnitude of B = nabla z times nabla x + DifferentialOperators differential_operators; + // template // void communicate(T& t, Ts... ts); }; diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 2d1f2c37eb..220ff4f842 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -19,10 +19,11 @@ #include "parallel/fci.hxx" #include "parallel/shiftedmetricinterp.hxx" -#include "bout/geometry.hxx" -#include "bout/metricTensor.hxx" +#include "bout/differential_operators.hxx" #include "bout/field2d.hxx" #include "bout/field3d.hxx" +#include "bout/geometry.hxx" +#include "bout/metricTensor.hxx" #include "bout/utils.hxx" #include @@ -307,7 +308,8 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, // G3_11(mesh), G3_22(mesh), G3_33(mesh), G3_12(mesh), G3_13(mesh), G3_23(mesh), // G1(mesh), G2(mesh), G3(mesh), ShiftTorsion(mesh), IntShiftTorsion(mesh), localmesh(mesh), location(loc), - geometry(Geometry(mesh, loc)) { + geometry( + Geometry(mesh, DifferentialOperators(mesh, IntShiftTorsion, loc, dx, dy, dz))) { if (options == nullptr) { options = Options::getRoot()->getSection("mesh"); diff --git a/src/mesh/differential_operators.cxx b/src/mesh/differential_operators.cxx index c2bb5aaa0c..4ccf1e1088 100644 --- a/src/mesh/differential_operators.cxx +++ b/src/mesh/differential_operators.cxx @@ -1,10 +1,12 @@ #include "bout/differential_operators.hxx" +#include "bout/mesh.hxx" -DifferentialOperators::DifferentialOperators(Mesh* mesh, CELL_LOC& location, - FieldMetric& dx, FieldMetric& dy, - FieldMetric& dz) - : mesh(mesh), location(location), dx(dx), dy(dy), dz(dz) {} +DifferentialOperators::DifferentialOperators(Mesh* mesh, FieldMetric& intShiftTorsion, + const CELL_LOC& location, FieldMetric& dx, + FieldMetric& dy, FieldMetric& dz) + : mesh(mesh), intShiftTorsion(intShiftTorsion), location(location), dx(dx), dy(dy), + dz(dz) {} DifferentialOperators::FieldMetric DifferentialOperators::DDX(const Field2D& f, CELL_LOC loc, @@ -20,7 +22,7 @@ Field3D DifferentialOperators::DDX(const Field3D& f, CELL_LOC outloc, if (f.getMesh()->IncIntShear) { // Using BOUT-06 style shifting - result += IntShiftTorsion * DDZ(f, outloc, method, region); + result += intShiftTorsion * DDZ(f, outloc, method, region); } return result; diff --git a/src/mesh/geometry.cxx b/src/mesh/geometry.cxx index f5bea2414c..6038e8312a 100644 --- a/src/mesh/geometry.cxx +++ b/src/mesh/geometry.cxx @@ -337,7 +337,7 @@ Field3D interpolateAndExtrapolate(const Field3D& f_, CELL_LOC location, // checkStaggeredGet(mesh, name, suffix); // return mesh->get(name + suffix, default_value, false, location); //} -// + //std::string getLocationSuffix(CELL_LOC location) { // switch (location) { // case CELL_CENTRE: { @@ -411,13 +411,14 @@ Geometry::Geometry( // Coordinates& coordinates, FieldMetric J, FieldMetric Bxy, FieldMetric g11, FieldMetric g22, FieldMetric g33, FieldMetric g12, FieldMetric g13, FieldMetric g23, FieldMetric g_11, FieldMetric g_22, - FieldMetric g_33, FieldMetric g_12, FieldMetric g_13, FieldMetric g_23) + FieldMetric g_33, FieldMetric g_12, FieldMetric g_13, FieldMetric g_23, + DifferentialOperators differential_operators) : // coordinates(coordinates), contravariantMetricTensor(g11, g22, g33, g12, g13, g23), - covariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23), this_J(J), - this_Bxy(Bxy) {} + covariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23), this_J(J), this_Bxy(Bxy), + differential_operators(differential_operators) {} -Geometry::Geometry(Mesh* mesh, const CELL_LOC cell_location) +Geometry::Geometry(Mesh* mesh, DifferentialOperators differential_operators) //bool force_interpolate_from_centre) : G1_11(mesh), G1_22(mesh), G1_33(mesh), G1_12(mesh), G1_13(mesh), G1_23(mesh), G2_11(mesh), G2_22(mesh), G2_33(mesh), G2_12(mesh), G2_13(mesh), G2_23(mesh), @@ -425,7 +426,8 @@ Geometry::Geometry(Mesh* mesh, const CELL_LOC cell_location) G1(mesh), G2(mesh), G3(mesh), contravariantMetricTensor(1., 1., 1., 0, 0, 0, mesh), covariantMetricTensor(1., 1., 1., 0, 0, 0, mesh), // Identity metric tensor - this_J(1., mesh), this_Bxy(1., mesh) {} + this_J(1., mesh), this_Bxy(1., mesh), + differential_operators(differential_operators) {} // //void Coordinates::outputVars(Options& output_options) { @@ -707,152 +709,179 @@ Geometry::Geometry(Mesh* mesh, const CELL_LOC cell_location) // // // // return 0; // //} -// + void Geometry::CalculateChristoffelSymbols() { // Calculate Christoffel symbol terms (18 independent values) // Note: This calculation is completely general: metric // tensor can be 2D or 3D. For 2D, all DDZ terms are zero - G1_11 = 0.5 * contravariantMetricTensor.Getg11() * DDX(covariantMetricTensor.Getg11()) + G1_11 = 0.5 * contravariantMetricTensor.Getg11() + * differential_operators.DDX(covariantMetricTensor.Getg11()) + contravariantMetricTensor.Getg12() - * (DDX(covariantMetricTensor.Getg12()) - - 0.5 * DDY(covariantMetricTensor.Getg11())) + * (differential_operators.DDX(covariantMetricTensor.Getg12()) + - 0.5 * differential_operators.DDY(covariantMetricTensor.Getg11())) + contravariantMetricTensor.Getg13() - * (DDX(covariantMetricTensor.Getg13()) - - 0.5 * DDZ(covariantMetricTensor.Getg11())); + * (differential_operators.DDX(covariantMetricTensor.Getg13()) + - 0.5 * differential_operators.DDZ(covariantMetricTensor.Getg11())); G1_22 = contravariantMetricTensor.Getg11() - * (DDY(covariantMetricTensor.Getg12()) - - 0.5 * DDX(covariantMetricTensor.Getg22())) - + 0.5 * contravariantMetricTensor.Getg12() * DDY(covariantMetricTensor.Getg22()) + * (differential_operators.DDY(covariantMetricTensor.Getg12()) + - 0.5 * differential_operators.DDX(covariantMetricTensor.Getg22())) + + 0.5 * contravariantMetricTensor.Getg12() + * differential_operators.DDY(covariantMetricTensor.Getg22()) + contravariantMetricTensor.Getg13() - * (DDY(covariantMetricTensor.Getg23()) - - 0.5 * DDZ(covariantMetricTensor.Getg22())); - G1_33 = - contravariantMetricTensor.Getg11() - * (DDZ(covariantMetricTensor.Getg13()) - - 0.5 * DDX(covariantMetricTensor.Getg33())) - + contravariantMetricTensor.Getg12() - * (DDZ(covariantMetricTensor.Getg23()) - - 0.5 * DDY(covariantMetricTensor.Getg33())) - + 0.5 * contravariantMetricTensor.Getg13() * DDZ(covariantMetricTensor.Getg33()); - G1_12 = - 0.5 * contravariantMetricTensor.Getg11() * DDY(covariantMetricTensor.Getg11()) - + 0.5 * contravariantMetricTensor.Getg12() * DDX(covariantMetricTensor.Getg22()) - + 0.5 * contravariantMetricTensor.Getg13() - * (DDY(covariantMetricTensor.Getg13()) + DDX(covariantMetricTensor.Getg23()) - - DDZ(covariantMetricTensor.Getg12())); - G1_13 = - 0.5 * contravariantMetricTensor.Getg11() * DDZ(covariantMetricTensor.Getg11()) - + 0.5 * contravariantMetricTensor.Getg12() - * (DDZ(covariantMetricTensor.Getg12()) + DDX(covariantMetricTensor.Getg23()) - - DDY(covariantMetricTensor.Getg13())) - + 0.5 * contravariantMetricTensor.Getg13() * DDX(covariantMetricTensor.Getg33()); + * (differential_operators.DDY(covariantMetricTensor.Getg23()) + - 0.5 * differential_operators.DDZ(covariantMetricTensor.Getg22())); + G1_33 = contravariantMetricTensor.Getg11() + * (differential_operators.DDZ(covariantMetricTensor.Getg13()) + - 0.5 * differential_operators.DDX(covariantMetricTensor.Getg33())) + + contravariantMetricTensor.Getg12() + * (differential_operators.DDZ(covariantMetricTensor.Getg23()) + - 0.5 * differential_operators.DDY(covariantMetricTensor.Getg33())) + + 0.5 * contravariantMetricTensor.Getg13() + * differential_operators.DDZ(covariantMetricTensor.Getg33()); + G1_12 = 0.5 * contravariantMetricTensor.Getg11() + * differential_operators.DDY(covariantMetricTensor.Getg11()) + + 0.5 * contravariantMetricTensor.Getg12() + * differential_operators.DDX(covariantMetricTensor.Getg22()) + + 0.5 * contravariantMetricTensor.Getg13() + * (differential_operators.DDY(covariantMetricTensor.Getg13()) + + differential_operators.DDX(covariantMetricTensor.Getg23()) + - differential_operators.DDZ(covariantMetricTensor.Getg12())); + G1_13 = 0.5 * contravariantMetricTensor.Getg11() + * differential_operators.DDZ(covariantMetricTensor.Getg11()) + + 0.5 * contravariantMetricTensor.Getg12() + * (differential_operators.DDZ(covariantMetricTensor.Getg12()) + + differential_operators.DDX(covariantMetricTensor.Getg23()) + - differential_operators.DDY(covariantMetricTensor.Getg13())) + + 0.5 * contravariantMetricTensor.Getg13() + * differential_operators.DDX(covariantMetricTensor.Getg33()); G1_23 = 0.5 * contravariantMetricTensor.Getg11() - * (DDZ(covariantMetricTensor.Getg12()) + DDY(covariantMetricTensor.Getg13()) - - DDX(covariantMetricTensor.Getg23())) + * (differential_operators.DDZ(covariantMetricTensor.Getg12()) + + differential_operators.DDY(covariantMetricTensor.Getg13()) + - differential_operators.DDX(covariantMetricTensor.Getg23())) + 0.5 * contravariantMetricTensor.Getg12() - * (DDZ(covariantMetricTensor.Getg22()) + DDY(covariantMetricTensor.Getg23()) - - DDY(covariantMetricTensor.Getg23())) - // + 0.5 *g13*(DDZ(g_32) + DDY(g_33) - DDZ(g_23)); + * (differential_operators.DDZ(covariantMetricTensor.Getg22()) + + differential_operators.DDY(covariantMetricTensor.Getg23()) + - differential_operators.DDY(covariantMetricTensor.Getg23())) + // + 0.5 *g13*(differential_operators.DDZ(g_32) + differential_operators.DDY(g_33) - differential_operators.DDZ(g_23)); // which equals - + 0.5 * contravariantMetricTensor.Getg13() * DDY(covariantMetricTensor.Getg33()); + + 0.5 * contravariantMetricTensor.Getg13() + * differential_operators.DDY(covariantMetricTensor.Getg33()); - G2_11 = 0.5 * contravariantMetricTensor.Getg12() * DDX(covariantMetricTensor.Getg11()) + G2_11 = 0.5 * contravariantMetricTensor.Getg12() + * differential_operators.DDX(covariantMetricTensor.Getg11()) + contravariantMetricTensor.Getg22() - * (DDX(covariantMetricTensor.Getg12()) - - 0.5 * DDY(covariantMetricTensor.Getg11())) + * (differential_operators.DDX(covariantMetricTensor.Getg12()) + - 0.5 * differential_operators.DDY(covariantMetricTensor.Getg11())) + contravariantMetricTensor.Getg23() - * (DDX(covariantMetricTensor.Getg13()) - - 0.5 * DDZ(covariantMetricTensor.Getg11())); + * (differential_operators.DDX(covariantMetricTensor.Getg13()) + - 0.5 * differential_operators.DDZ(covariantMetricTensor.Getg11())); G2_22 = contravariantMetricTensor.Getg12() - * (DDY(covariantMetricTensor.Getg12()) - - 0.5 * DDX(covariantMetricTensor.Getg22())) - + 0.5 * contravariantMetricTensor.Getg22() * DDY(covariantMetricTensor.Getg22()) + * (differential_operators.DDY(covariantMetricTensor.Getg12()) + - 0.5 * differential_operators.DDX(covariantMetricTensor.Getg22())) + + 0.5 * contravariantMetricTensor.Getg22() + * differential_operators.DDY(covariantMetricTensor.Getg22()) + contravariantMetricTensor.Getg23() - * (DDY(contravariantMetricTensor.Getg23()) - - 0.5 * DDZ(covariantMetricTensor.Getg22())); - G2_33 = - contravariantMetricTensor.Getg12() - * (DDZ(covariantMetricTensor.Getg13()) - - 0.5 * DDX(covariantMetricTensor.Getg33())) - + contravariantMetricTensor.Getg22() - * (DDZ(covariantMetricTensor.Getg23()) - - 0.5 * DDY(covariantMetricTensor.Getg33())) - + 0.5 * contravariantMetricTensor.Getg23() * DDZ(covariantMetricTensor.Getg33()); - G2_12 = - 0.5 * contravariantMetricTensor.Getg12() * DDY(covariantMetricTensor.Getg11()) - + 0.5 * contravariantMetricTensor.Getg22() * DDX(covariantMetricTensor.Getg22()) - + 0.5 * contravariantMetricTensor.Getg23() - * (DDY(covariantMetricTensor.Getg13()) + DDX(covariantMetricTensor.Getg23()) - - DDZ(covariantMetricTensor.Getg12())); + * (differential_operators.DDY(contravariantMetricTensor.Getg23()) + - 0.5 * differential_operators.DDZ(covariantMetricTensor.Getg22())); + G2_33 = contravariantMetricTensor.Getg12() + * (differential_operators.DDZ(covariantMetricTensor.Getg13()) + - 0.5 * differential_operators.DDX(covariantMetricTensor.Getg33())) + + contravariantMetricTensor.Getg22() + * (differential_operators.DDZ(covariantMetricTensor.Getg23()) + - 0.5 * differential_operators.DDY(covariantMetricTensor.Getg33())) + + 0.5 * contravariantMetricTensor.Getg23() + * differential_operators.DDZ(covariantMetricTensor.Getg33()); + G2_12 = 0.5 * contravariantMetricTensor.Getg12() + * differential_operators.DDY(covariantMetricTensor.Getg11()) + + 0.5 * contravariantMetricTensor.Getg22() + * differential_operators.DDX(covariantMetricTensor.Getg22()) + + 0.5 * contravariantMetricTensor.Getg23() + * (differential_operators.DDY(covariantMetricTensor.Getg13()) + + differential_operators.DDX(covariantMetricTensor.Getg23()) + - differential_operators.DDZ(covariantMetricTensor.Getg12())); G2_13 = - // 0.5 *g21*(DDZ(covariantMetricTensor.Getg11()) + DDX(covariantMetricTensor.Getg13()) - DDX(covariantMetricTensor.Getg13())) + // 0.5 *g21*(differential_operators.DDZ(covariantMetricTensor.Getg11()) + differential_operators.DDX(covariantMetricTensor.Getg13()) - differential_operators.DDX(covariantMetricTensor.Getg13())) // which equals 0.5 * contravariantMetricTensor.Getg12() - * (DDZ(covariantMetricTensor.Getg11()) + DDX(covariantMetricTensor.Getg13()) - - DDX(covariantMetricTensor.Getg13())) - // + 0.5 *g22*(DDZ(covariantMetricTensor.Getg21()) + DDX(covariantMetricTensor.Getg23()) - DDY(covariantMetricTensor.Getg13())) + * (differential_operators.DDZ(covariantMetricTensor.Getg11()) + + differential_operators.DDX(covariantMetricTensor.Getg13()) + - differential_operators.DDX(covariantMetricTensor.Getg13())) + // + 0.5 *g22*(differential_operators.DDZ(covariantMetricTensor.Getg21()) + differential_operators.DDX(covariantMetricTensor.Getg23()) - differential_operators.DDY(covariantMetricTensor.Getg13())) // which equals + 0.5 * contravariantMetricTensor.Getg22() - * (DDZ(covariantMetricTensor.Getg12()) + DDX(covariantMetricTensor.Getg23()) - - DDY(covariantMetricTensor.Getg13())) - // + 0.5 *g23*(DDZ(covariantMetricTensor.Getg31()) + DDX(covariantMetricTensor.Getg33()) - DDZ(g_13)); + * (differential_operators.DDZ(covariantMetricTensor.Getg12()) + + differential_operators.DDX(covariantMetricTensor.Getg23()) + - differential_operators.DDY(covariantMetricTensor.Getg13())) + // + 0.5 *g23*(differential_operators.DDZ(covariantMetricTensor.Getg31()) + differential_operators.DDX(covariantMetricTensor.Getg33()) - differential_operators.DDZ(g_13)); // which equals - + 0.5 * contravariantMetricTensor.Getg23() * DDX(covariantMetricTensor.Getg33()); - G2_23 = - 0.5 * contravariantMetricTensor.Getg12() - * (DDZ(covariantMetricTensor.Getg12()) + DDY(covariantMetricTensor.Getg13()) - - DDX(covariantMetricTensor.Getg23())) - + 0.5 * contravariantMetricTensor.Getg22() * DDZ(covariantMetricTensor.Getg22()) - + 0.5 * contravariantMetricTensor.Getg23() * DDY(covariantMetricTensor.Getg33()); + + 0.5 * contravariantMetricTensor.Getg23() + * differential_operators.DDX(covariantMetricTensor.Getg33()); + G2_23 = 0.5 * contravariantMetricTensor.Getg12() + * (differential_operators.DDZ(covariantMetricTensor.Getg12()) + + differential_operators.DDY(covariantMetricTensor.Getg13()) + - differential_operators.DDX(covariantMetricTensor.Getg23())) + + 0.5 * contravariantMetricTensor.Getg22() + * differential_operators.DDZ(covariantMetricTensor.Getg22()) + + 0.5 * contravariantMetricTensor.Getg23() + * differential_operators.DDY(covariantMetricTensor.Getg33()); - G3_11 = 0.5 * contravariantMetricTensor.Getg13() * DDX(covariantMetricTensor.Getg11()) + G3_11 = 0.5 * contravariantMetricTensor.Getg13() + * differential_operators.DDX(covariantMetricTensor.Getg11()) + contravariantMetricTensor.Getg23() - * (DDX(covariantMetricTensor.Getg12()) - - 0.5 * DDY(covariantMetricTensor.Getg11())) + * (differential_operators.DDX(covariantMetricTensor.Getg12()) + - 0.5 * differential_operators.DDY(covariantMetricTensor.Getg11())) + contravariantMetricTensor.Getg33() - * (DDX(covariantMetricTensor.Getg13()) - - 0.5 * DDZ(covariantMetricTensor.Getg11())); + * (differential_operators.DDX(covariantMetricTensor.Getg13()) + - 0.5 * differential_operators.DDZ(covariantMetricTensor.Getg11())); G3_22 = contravariantMetricTensor.Getg13() - * (DDY(covariantMetricTensor.Getg12()) - - 0.5 * DDX(covariantMetricTensor.Getg22())) - + 0.5 * contravariantMetricTensor.Getg23() * DDY(covariantMetricTensor.Getg22()) + * (differential_operators.DDY(covariantMetricTensor.Getg12()) + - 0.5 * differential_operators.DDX(covariantMetricTensor.Getg22())) + + 0.5 * contravariantMetricTensor.Getg23() + * differential_operators.DDY(covariantMetricTensor.Getg22()) + contravariantMetricTensor.Getg33() - * (DDY(covariantMetricTensor.Getg23()) - - 0.5 * DDZ(covariantMetricTensor.Getg22())); - G3_33 = - contravariantMetricTensor.Getg13() - * (DDZ(covariantMetricTensor.Getg13()) - - 0.5 * DDX(covariantMetricTensor.Getg33())) - + contravariantMetricTensor.Getg23() - * (DDZ(covariantMetricTensor.Getg23()) - - 0.5 * DDY(covariantMetricTensor.Getg33())) - + 0.5 * contravariantMetricTensor.Getg33() * DDZ(covariantMetricTensor.Getg33()); + * (differential_operators.DDY(covariantMetricTensor.Getg23()) + - 0.5 * differential_operators.DDZ(covariantMetricTensor.Getg22())); + G3_33 = contravariantMetricTensor.Getg13() + * (differential_operators.DDZ(covariantMetricTensor.Getg13()) + - 0.5 * differential_operators.DDX(covariantMetricTensor.Getg33())) + + contravariantMetricTensor.Getg23() + * (differential_operators.DDZ(covariantMetricTensor.Getg23()) + - 0.5 * differential_operators.DDY(covariantMetricTensor.Getg33())) + + 0.5 * contravariantMetricTensor.Getg33() + * differential_operators.DDZ(covariantMetricTensor.Getg33()); G3_12 = - // 0.5 *g31*(DDY(covariantMetricTensor.Getg11()) + DDX(covariantMetricTensor.Getg12()) - DDX(covariantMetricTensor.Getg12())) + // 0.5 *g31*(differential_operators.DDY(covariantMetricTensor.Getg11()) + differential_operators.DDX(covariantMetricTensor.Getg12()) - differential_operators.DDX(covariantMetricTensor.Getg12())) // which equals to - 0.5 * contravariantMetricTensor.Getg13() * DDY(covariantMetricTensor.Getg11()) - // + 0.5 *g32*(DDY(covariantMetricTensor.Getg21()) + DDX(covariantMetricTensor.Getg22()) - DDY(covariantMetricTensor.Getg12())) + 0.5 * contravariantMetricTensor.Getg13() + * differential_operators.DDY(covariantMetricTensor.Getg11()) + // + 0.5 *g32*(differential_operators.DDY(covariantMetricTensor.Getg21()) + differential_operators.DDX(covariantMetricTensor.Getg22()) - differential_operators.DDY(covariantMetricTensor.Getg12())) // which equals to - + 0.5 * contravariantMetricTensor.Getg23() * DDX(covariantMetricTensor.Getg22()) - //+ 0.5 *g33*(DDY(covariantMetricTensor.Getg31()) + DDX(covariantMetricTensor.Getg32()) - DDZ(covariantMetricTensor.Getg12())); + + 0.5 * contravariantMetricTensor.Getg23() + * differential_operators.DDX(covariantMetricTensor.Getg22()) + //+ 0.5 *g33*(differential_operators.DDY(covariantMetricTensor.Getg31()) + differential_operators.DDX(covariantMetricTensor.Getg32()) - differential_operators.DDZ(covariantMetricTensor.Getg12())); // which equals to + 0.5 * contravariantMetricTensor.Getg33() - * (DDY(covariantMetricTensor.Getg13()) + DDX(covariantMetricTensor.Getg23()) - - DDZ(covariantMetricTensor.Getg12())); - G3_13 = - 0.5 * contravariantMetricTensor.Getg13() * DDZ(covariantMetricTensor.Getg11()) - + 0.5 * contravariantMetricTensor.Getg23() - * (DDZ(covariantMetricTensor.Getg12()) + DDX(covariantMetricTensor.Getg23()) - - DDY(covariantMetricTensor.Getg13())) - + 0.5 * contravariantMetricTensor.Getg33() * DDX(covariantMetricTensor.Getg33()); - G3_23 = - 0.5 * contravariantMetricTensor.Getg13() - * (DDZ(covariantMetricTensor.Getg12()) + DDY(covariantMetricTensor.Getg13()) - - DDX(covariantMetricTensor.Getg23())) - + 0.5 * contravariantMetricTensor.Getg23() * DDZ(covariantMetricTensor.Getg22()) - + 0.5 * contravariantMetricTensor.Getg33() * DDY(covariantMetricTensor.Getg33()); + * (differential_operators.DDY(covariantMetricTensor.Getg13()) + + differential_operators.DDX(covariantMetricTensor.Getg23()) + - differential_operators.DDZ(covariantMetricTensor.Getg12())); + G3_13 = 0.5 * contravariantMetricTensor.Getg13() + * differential_operators.DDZ(covariantMetricTensor.Getg11()) + + 0.5 * contravariantMetricTensor.Getg23() + * (differential_operators.DDZ(covariantMetricTensor.Getg12()) + + differential_operators.DDX(covariantMetricTensor.Getg23()) + - differential_operators.DDY(covariantMetricTensor.Getg13())) + + 0.5 * contravariantMetricTensor.Getg33() + * differential_operators.DDX(covariantMetricTensor.Getg33()); + G3_23 = 0.5 * contravariantMetricTensor.Getg13() + * (differential_operators.DDZ(covariantMetricTensor.Getg12()) + + differential_operators.DDY(covariantMetricTensor.Getg13()) + - differential_operators.DDX(covariantMetricTensor.Getg23())) + + 0.5 * contravariantMetricTensor.Getg23() + * differential_operators.DDZ(covariantMetricTensor.Getg22()) + + 0.5 * contravariantMetricTensor.Getg33() + * differential_operators.DDY(covariantMetricTensor.Getg33()); } void Geometry::calcCovariant(CELL_LOC cell_location, const std::string& region) { From eba329870ed43ea8a22e92e6c03e282ffea57f3a Mon Sep 17 00:00:00 2001 From: tomc271 Date: Sun, 26 Nov 2023 14:17:52 +0000 Subject: [PATCH 183/491] More fixes. --- include/bout/coordinates.hxx | 10 +- include/bout/differential_operators.hxx | 61 ++++++-- include/bout/geometry.hxx | 12 +- src/mesh/coordinates.cxx | 3 + src/mesh/differential_operators.cxx | 193 +++++++++++++----------- src/mesh/geometry.cxx | 20 +-- 6 files changed, 179 insertions(+), 120 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index d49d9285ab..ca86284e9f 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -257,16 +257,16 @@ private: /// `Coordinates::calculateGeometry` is called mutable std::unique_ptr zlength_cache{nullptr}; - /// Cache variable for Grad2_par2 - mutable std::map> Grad2_par2_DDY_invSgCache; - mutable std::unique_ptr invSgCache{nullptr}; +// /// Cache variable for Grad2_par2 +// mutable std::map> Grad2_par2_DDY_invSgCache; +// mutable std::unique_ptr invSgCache{nullptr}; /// Set the parallel (y) transform from the options file. /// Used in the constructor to create the transform object. void setParallelTransform(Options* options); - const FieldMetric& invSg() const; - const FieldMetric& Grad2_par2_DDY_invSg(CELL_LOC outloc, +// const FieldMetric& invSg() const; +// const FieldMetric& Grad2_par2_DDY_invSg(CELL_LOC outloc, const std::string& method) const; // check that covariant tensors are positive (if expected) and finite (always) diff --git a/include/bout/differential_operators.hxx b/include/bout/differential_operators.hxx index 0539dde238..38ee1b0ce8 100644 --- a/include/bout/differential_operators.hxx +++ b/include/bout/differential_operators.hxx @@ -4,7 +4,8 @@ #include "bout/field2d.hxx" #include "bout/field3d.hxx" -#include "bout/index_derivs_interface.hxx" +//#include "bout/index_derivs_interface.hxx" +#include "metricTensor.hxx" #include "bout/paralleltransform.hxx" #include @@ -46,57 +47,77 @@ public: const std::string& region = "RGN_NOBNDRY"); /// Gradient along magnetic field b.Grad(f) - FieldMetric Grad_par(const Field2D& var, CELL_LOC outloc = CELL_DEFAULT, + FieldMetric Grad_par(const Field2D& var, MetricTensor& covariantMetricTensor, + CELL_LOC outloc = CELL_DEFAULT, const std::string& method = "DEFAULT"); - Field3D Grad_par(const Field3D& var, CELL_LOC outloc = CELL_DEFAULT, - const std::string& method = "DEFAULT"); + Field3D Grad_par(const Field3D& var, MetricTensor& covariantMetricTensor, + CELL_LOC outloc = CELL_DEFAULT, const std::string& method = "DEFAULT"); /// Advection along magnetic field V*b.Grad(f) FieldMetric Vpar_Grad_par(const Field2D& v, const Field2D& f, + MetricTensor& covariantMetricTensor, CELL_LOC outloc = CELL_DEFAULT, const std::string& method = "DEFAULT"); Field3D Vpar_Grad_par(const Field3D& v, const Field3D& f, + MetricTensor& covariantMetricTensor, CELL_LOC outloc = CELL_DEFAULT, const std::string& method = "DEFAULT"); /// Divergence along magnetic field Div(b*f) = B.Grad(f/B) - FieldMetric Div_par(const Field2D& f, CELL_LOC outloc = CELL_DEFAULT, + FieldMetric Div_par(const Field2D& f, const Field2D& Bxy, + CELL_LOC outloc = CELL_DEFAULT, const std::string& method = "DEFAULT"); - Field3D Div_par(const Field3D& f, CELL_LOC outloc = CELL_DEFAULT, + Field3D Div_par(const Field3D& f, const Field2D& Bxy, CELL_LOC outloc = CELL_DEFAULT, const std::string& method = "DEFAULT"); // Second derivative along magnetic field - FieldMetric Grad2_par2(const Field2D& f, CELL_LOC outloc = CELL_DEFAULT, + FieldMetric Grad2_par2(const Field2D& f, MetricTensor& covariantMetricTensor, + CELL_LOC outloc = CELL_DEFAULT, const std::string& method = "DEFAULT"); - Field3D Grad2_par2(const Field3D& f, CELL_LOC outloc = CELL_DEFAULT, + Field3D Grad2_par2(const Field3D& f, MetricTensor& covariantMetricTensor, + CELL_LOC outloc = CELL_DEFAULT, const std::string& method = "DEFAULT"); + // Perpendicular Laplacian operator, using only X-Z derivatives // NOTE: This might be better bundled with the Laplacian inversion code // since it makes use of the same coefficients and FFT routines - FieldMetric Delp2(const Field2D& f, CELL_LOC outloc = CELL_DEFAULT, bool useFFT = true); - Field3D Delp2(const Field3D& f, CELL_LOC outloc = CELL_DEFAULT, bool useFFT = true); + FieldMetric Delp2(const Field2D& f, const Field2D& g11, const Field2D& G1, + CELL_LOC outloc = CELL_DEFAULT, bool useFFT = true); + + Field3D Delp2(const Field3D& f, MetricTensor& covariantMetricTensor, const Field3D& G1, + const Field3D& G3, CELL_LOC outloc = CELL_DEFAULT, bool useFFT = true); + FieldPerp Delp2(const FieldPerp& f, CELL_LOC outloc = CELL_DEFAULT, bool useFFT = true); // Full parallel Laplacian operator on scalar field // Laplace_par(f) = Div( b (b dot Grad(f)) ) - FieldMetric Laplace_par(const Field2D& f, CELL_LOC outloc = CELL_DEFAULT); - Field3D Laplace_par(const Field3D& f, CELL_LOC outloc = CELL_DEFAULT); + FieldMetric Laplace_par(const Field2D& f, const Field2D& g22, const Field2D& J, + CELL_LOC outloc = CELL_DEFAULT); + + Field3D Laplace_par(const Field3D& f, const Field3D& g22, const Field3D& J, + CELL_LOC outloc = CELL_DEFAULT); // Full Laplacian operator on scalar field - FieldMetric Laplace(const Field2D& f, CELL_LOC outloc = CELL_DEFAULT, + FieldMetric Laplace(const Field2D& f, MetricTensor& covariantMetricTensor, + const Field2D& G1, const Field2D& G2, const Field2D& G3, + CELL_LOC outloc = CELL_DEFAULT, const std::string& dfdy_boundary_conditions = "free_o3", const std::string& dfdy_dy_region = ""); - Field3D Laplace(const Field3D& f, CELL_LOC outloc = CELL_DEFAULT, + + Field3D Laplace(const Field3D& f, MetricTensor& covariantMetricTensor, + const Field3D& G1, const Field3D& G2, const Field3D& G3, + CELL_LOC outloc = CELL_DEFAULT, const std::string& dfdy_boundary_conditions = "free_o3", const std::string& dfdy_dy_region = ""); // Full perpendicular Laplacian, in form of inverse of Laplacian operator in LaplaceXY // solver - Field2D Laplace_perpXY(const Field2D& A, const Field2D& f); + Field2D Laplace_perpXY(const Field2D& A, const Field2D& f, + MetricTensor& covariantMetricTensor, const Field2D& J); private: Mesh* mesh; @@ -105,6 +126,16 @@ private: FieldMetric& dx; FieldMetric& dy; FieldMetric& dz; + + /// Cache variable for Grad2_par2 + mutable std::map> Grad2_par2_DDY_invSgCache; + mutable std::unique_ptr invSgCache{nullptr}; + + FieldMetric& invSg(MetricTensor& covariantMetricTensor) const; + + const FieldMetric& Grad2_par2_DDY_invSg(MetricTensor& covariantMetricTensor, + CELL_LOC outloc, + const std::string& method) const; }; #endif //BOUT_DIFFERENTIALOPERATORS_HXX diff --git a/include/bout/geometry.hxx b/include/bout/geometry.hxx index acaef80545..58eea7aa38 100644 --- a/include/bout/geometry.hxx +++ b/include/bout/geometry.hxx @@ -58,12 +58,12 @@ public: // Geometry(); - Geometry( - // Coordinates& coordinates, - FieldMetric J, FieldMetric Bxy, FieldMetric g11, FieldMetric g22, FieldMetric g33, - FieldMetric g12, FieldMetric g13, FieldMetric g23, FieldMetric g_11, - FieldMetric g_22, FieldMetric g_33, FieldMetric g_12, FieldMetric g_13, - FieldMetric g_23, DifferentialOperators differential_operators); +// Geometry( +// // Coordinates& coordinates, +// FieldMetric J, FieldMetric Bxy, FieldMetric g11, FieldMetric g22, FieldMetric g33, +// FieldMetric g12, FieldMetric g13, FieldMetric g23, FieldMetric g_11, +// FieldMetric g_22, FieldMetric g_33, FieldMetric g_12, FieldMetric g_13, +// FieldMetric g_23, DifferentialOperators differential_operators); Geometry(Mesh* mesh, DifferentialOperators differential_operators); diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 220ff4f842..17cb0c6a49 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -1680,3 +1680,6 @@ void Coordinates::setBxy(FieldMetric Bxy) { const MetricTensor& Coordinates::getContravariantMetricTensor() const { return geometry.getContravariantMetricTensor(); } +const Coordinates::FieldMetric& Coordinates::invSg() const { + +} diff --git a/src/mesh/differential_operators.cxx b/src/mesh/differential_operators.cxx index 4ccf1e1088..81328a6a6c 100644 --- a/src/mesh/differential_operators.cxx +++ b/src/mesh/differential_operators.cxx @@ -15,6 +15,7 @@ DifferentialOperators::FieldMetric DifferentialOperators::DDX(const Field2D& f, ASSERT1(location == loc || loc == CELL_DEFAULT) return bout::derivatives::index::DDX(f, loc, method, region) / dx; } + Field3D DifferentialOperators::DDX(const Field3D& f, CELL_LOC outloc, const std::string& method, const std::string& region) { auto result = bout::derivatives::index::DDX(f, outloc, method, region); @@ -69,20 +70,22 @@ Field3D DifferentialOperators::DDZ(const Field3D& f, CELL_LOC outloc, // Parallel gradient DifferentialOperators::FieldMetric -DifferentialOperators::Grad_par(const Field2D& var, [[maybe_unused]] CELL_LOC outloc, +DifferentialOperators::Grad_par(const Field2D& var, MetricTensor& covariantMetricTensor, + [[maybe_unused]] CELL_LOC outloc, const std::string& UNUSED(method)) { TRACE("DifferentialOperators::Grad_par( Field2D )"); ASSERT1(location == outloc || (outloc == CELL_DEFAULT && location == var.getLocation())) - return DDY(var) * invSg(); + return DDY(var) * invSg(covariantMetricTensor); } -Field3D DifferentialOperators::Grad_par(const Field3D& var, CELL_LOC outloc, - const std::string& method) { +Field3D DifferentialOperators::Grad_par(const Field3D& var, + MetricTensor& covariantMetricTensor, + CELL_LOC outloc, const std::string& method) { TRACE("DifferentialOperators::Grad_par( Field3D )"); ASSERT1(location == outloc || outloc == CELL_DEFAULT) - return ::DDY(var, outloc, method) * invSg(); + return ::DDY(var, outloc, method) * invSg(covariantMetricTensor); } ///////////////////////////////////////////////////////// @@ -90,26 +93,27 @@ Field3D DifferentialOperators::Grad_par(const Field3D& var, CELL_LOC outloc, // vparallel times the parallel derivative along unperturbed B-field DifferentialOperators::FieldMetric -DifferentialOperators::Vpar_Grad_par(const Field2D& v, const Field2D& f, - [[maybe_unused]] CELL_LOC outloc, - const std::string& UNUSED(method)) { +DifferentialOperators::FieldMetric DifferentialOperators::Vpar_Grad_par( + const Field2D& v, const Field2D& f, MetricTensor& covariantMetricTensor, + [[maybe_unused]] CELL_LOC outloc, const std::string& UNUSED(method)) { ASSERT1(location == outloc || (outloc == CELL_DEFAULT && location == f.getLocation())) - return VDDY(v, f) * invSg(); + return VDDY(v, f) * invSg(covariantMetricTensor); } Field3D DifferentialOperators::Vpar_Grad_par(const Field3D& v, const Field3D& f, + MetricTensor& covariantMetricTensor, CELL_LOC outloc, const std::string& method) { ASSERT1(location == outloc || outloc == CELL_DEFAULT) - return VDDY(v, f, outloc, method) * invSg(); + return VDDY(v, f, outloc, method) * invSg(covariantMetricTensor); } ///////////////////////////////////////////////////////// // Parallel divergence DifferentialOperators::FieldMetric -DifferentialOperators::Div_par(const Field2D& f, CELL_LOC outloc, +DifferentialOperators::Div_par(const Field2D& f, const Field2D& Bxy, CELL_LOC outloc, const std::string& method) { TRACE("DifferentialOperators::Div_par( Field2D )"); ASSERT1(location == outloc || outloc == CELL_DEFAULT) @@ -118,11 +122,11 @@ DifferentialOperators::Div_par(const Field2D& f, CELL_LOC outloc, // Coordinates object auto Bxy_floc = f.getCoordinates()->Bxy(); - return Bxy() * Grad_par(f / Bxy_floc, outloc, method); + return Bxy * Grad_par(f / Bxy_floc, outloc, method); } -Field3D DifferentialOperators::Div_par(const Field3D& f, CELL_LOC outloc, - const std::string& method) { +Field3D DifferentialOperators::Div_par(const Field3D& f, const Field2D& Bxy, + CELL_LOC outloc, const std::string& method) { TRACE("DifferentialOperators::Div_par( Field3D )"); ASSERT1(location == outloc || outloc == CELL_DEFAULT) @@ -133,7 +137,7 @@ Field3D DifferentialOperators::Div_par(const Field3D& f, CELL_LOC outloc, if (!f.hasParallelSlices()) { // No yup/ydown fields. The Grad_par operator will // shift to field aligned coordinates - return Bxy() * Grad_par(f / Bxy_floc, outloc, method); + return Bxy * Grad_par(f / Bxy_floc, outloc, method); } // Need to modify yup and ydown fields @@ -143,7 +147,7 @@ Field3D DifferentialOperators::Div_par(const Field3D& f, CELL_LOC outloc, f_B.yup(i) = f.yup(i) / Bxy_floc.yup(i); f_B.ydown(i) = f.ydown(i) / Bxy_floc.ydown(i); } - return Bxy() * Grad_par(f_B, outloc, method); + return Bxy * Grad_par(f_B, outloc, method); } ///////////////////////////////////////////////////////// @@ -151,19 +155,21 @@ Field3D DifferentialOperators::Div_par(const Field3D& f, CELL_LOC outloc, // Note: For parallel Laplacian use Laplace_par DifferentialOperators::FieldMetric -DifferentialOperators::Grad2_par2(const Field2D& f, CELL_LOC outloc, - const std::string& method) { +DifferentialOperators::Grad2_par2(const Field2D& f, MetricTensor& covariantMetricTensor, + CELL_LOC outloc, const std::string& method) { TRACE("DifferentialOperators::Grad2_par2( Field2D )"); ASSERT1(location == outloc || (outloc == CELL_DEFAULT && location == f.getLocation())) - auto result = Grad2_par2_DDY_invSg(outloc, method) * DDY(f, outloc, method) - + D2DY2(f, outloc, method) / g22(); + auto result = + Grad2_par2_DDY_invSg(covariantMetricTensor, outloc, method) * DDY(f, outloc, method) + + D2DY2(f, outloc, method) / covariantMetricTensor.Getg22(); return result; } -Field3D DifferentialOperators::Grad2_par2(const Field3D& f, CELL_LOC outloc, - const std::string& method) { +Field3D DifferentialOperators::Grad2_par2(const Field3D& f, + MetricTensor& covariantMetricTensor, + CELL_LOC outloc, const std::string& method) { TRACE("DifferentialOperators::Grad2_par2( Field3D )"); if (outloc == CELL_DEFAULT) { outloc = f.getLocation(); @@ -172,9 +178,9 @@ Field3D DifferentialOperators::Grad2_par2(const Field3D& f, CELL_LOC outloc, Field3D result = ::DDY(f, outloc, method); - Field3D r2 = D2DY2(f, outloc, method) / g22(); + Field3D r2 = D2DY2(f, outloc, method) / covariantMetricTensor.Getg22(); - result = Grad2_par2_DDY_invSg(outloc, method) * result + r2; + result = Grad2_par2_DDY_invSg(covariantMetricTensor, outloc, method) * result + r2; ASSERT2(result.getLocation() == outloc) @@ -187,16 +193,20 @@ Field3D DifferentialOperators::Grad2_par2(const Field3D& f, CELL_LOC outloc, #include // Delp2 uses same coefficients as inversion code DifferentialOperators::FieldMetric -DifferentialOperators::Delp2(const Field2D& f, CELL_LOC outloc, bool UNUSED(useFFT)) { +DifferentialOperators::Delp2(const Field2D& f, const Field2D& g11, const Field2D& G1, + CELL_LOC outloc, bool UNUSED(useFFT)) { TRACE("DifferentialOperators::Delp2( Field2D )"); ASSERT1(location == outloc || outloc == CELL_DEFAULT) - auto result = G1 * DDX(f, outloc) + g11() * D2DX2(f, outloc); + auto result = G1 * DDX(f, outloc) + g11 * D2DX2(f, outloc); return result; } -Field3D DifferentialOperators::Delp2(const Field3D& f, CELL_LOC outloc, bool useFFT) { +Field3D DifferentialOperators::Delp2(const Field3D& f, + MetricTensor& covariantMetricTensor, + const Field3D& G1, const Field3D& G3, + CELL_LOC outloc, bool useFFT) { TRACE("DifferentialOperators::Delp2( Field3D )"); if (outloc == CELL_DEFAULT) { @@ -206,29 +216,29 @@ Field3D DifferentialOperators::Delp2(const Field3D& f, CELL_LOC outloc, bool use ASSERT1(location == outloc) ASSERT1(f.getLocation() == outloc) - if (localmesh->GlobalNx == 1 && localmesh->GlobalNz == 1) { + if (mesh->GlobalNx == 1 && mesh->GlobalNz == 1) { // copy mesh, location, etc return f * 0; } - ASSERT2(localmesh->xstart > 0) // Need at least one guard cell + ASSERT2(mesh->xstart > 0) // Need at least one guard cell Field3D result{emptyFrom(f).setLocation(outloc)}; if (useFFT and not bout::build::use_metric_3d) { - int ncz = localmesh->LocalNz; + int ncz = mesh->LocalNz; // Allocate memory - auto ft = Matrix(localmesh->LocalNx, ncz / 2 + 1); - auto delft = Matrix(localmesh->LocalNx, ncz / 2 + 1); + auto ft = Matrix(mesh->LocalNx, ncz / 2 + 1); + auto delft = Matrix(mesh->LocalNx, ncz / 2 + 1); // Loop over y indices // Note: should not include y-guard or y-boundary points here as that would // use values from corner cells in dx, which may not be initialised. - for (int jy = localmesh->ystart; jy <= localmesh->yend; jy++) { + for (int jy = mesh->ystart; jy <= mesh->yend; jy++) { // Take forward FFT - for (int jx = 0; jx < localmesh->LocalNx; jx++) { + for (int jx = 0; jx < mesh->LocalNx; jx++) { rfft(&f(jx, jy, 0), ncz, &ft(jx, 0)); } @@ -236,7 +246,7 @@ Field3D DifferentialOperators::Delp2(const Field3D& f, CELL_LOC outloc, bool use for (int jz = 0; jz <= ncz / 2; jz++) { // No smoothing in the x direction - for (int jx = localmesh->xstart; jx <= localmesh->xend; jx++) { + for (int jx = mesh->xstart; jx <= mesh->xend; jx++) { // Perform x derivative dcomplex a, b, c; @@ -247,14 +257,16 @@ Field3D DifferentialOperators::Delp2(const Field3D& f, CELL_LOC outloc, bool use } // Reverse FFT - for (int jx = localmesh->xstart; jx <= localmesh->xend; jx++) { + for (int jx = mesh->xstart; jx <= mesh->xend; jx++) { irfft(&delft(jx, 0), ncz, &result(jx, jy, 0)); } } } else { - result = G1 * ::DDX(f, outloc) + G3 * ::DDZ(f, outloc) + g11() * ::D2DX2(f, outloc) - + g33() * ::D2DZ2(f, outloc) + 2 * g13() * ::D2DXDZ(f, outloc); + result = G1 * ::DDX(f, outloc) + G3 * ::DDZ(f, outloc) + + covariantMetricTensor.Getg11() * ::D2DX2(f, outloc) + + covariantMetricTensor.Getg33() * ::D2DZ2(f, outloc) + + 2 * covariantMetricTensor.Getg13() * ::D2DXDZ(f, outloc); } ASSERT2(result.getLocation() == outloc) @@ -272,11 +284,11 @@ FieldPerp DifferentialOperators::Delp2(const FieldPerp& f, CELL_LOC outloc, bool ASSERT1(location == outloc) ASSERT1(f.getLocation() == outloc) - if (localmesh->GlobalNx == 1 && localmesh->GlobalNz == 1) { + if (mesh->GlobalNx == 1 && mesh->GlobalNz == 1) { // copy mesh, location, etc return f * 0; } - ASSERT2(localmesh->xstart > 0) // Need at least one guard cell + ASSERT2(mesh->xstart > 0) // Need at least one guard cell FieldPerp result{emptyFrom(f).setLocation(outloc)}; @@ -284,14 +296,14 @@ FieldPerp DifferentialOperators::Delp2(const FieldPerp& f, CELL_LOC outloc, bool result.setIndex(jy); if (useFFT) { - int ncz = localmesh->LocalNz; + int ncz = mesh->LocalNz; // Allocate memory - auto ft = Matrix(localmesh->LocalNx, ncz / 2 + 1); - auto delft = Matrix(localmesh->LocalNx, ncz / 2 + 1); + auto ft = Matrix(mesh->LocalNx, ncz / 2 + 1); + auto delft = Matrix(mesh->LocalNx, ncz / 2 + 1); // Take forward FFT - for (int jx = 0; jx < localmesh->LocalNx; jx++) { + for (int jx = 0; jx < mesh->LocalNx; jx++) { rfft(&f(jx, 0), ncz, &ft(jx, 0)); } @@ -299,7 +311,7 @@ FieldPerp DifferentialOperators::Delp2(const FieldPerp& f, CELL_LOC outloc, bool for (int jz = 0; jz <= ncz / 2; jz++) { // No smoothing in the x direction - for (int jx = localmesh->xstart; jx <= localmesh->xend; jx++) { + for (int jx = mesh->xstart; jx <= mesh->xend; jx++) { // Perform x derivative dcomplex a, b, c; @@ -310,7 +322,7 @@ FieldPerp DifferentialOperators::Delp2(const FieldPerp& f, CELL_LOC outloc, bool } // Reverse FFT - for (int jx = localmesh->xstart; jx <= localmesh->xend; jx++) { + for (int jx = mesh->xstart; jx <= mesh->xend; jx++) { irfft(&delft(jx, 0), ncz, &result(jx, 0)); } @@ -326,49 +338,58 @@ FieldPerp DifferentialOperators::Delp2(const FieldPerp& f, CELL_LOC outloc, bool } DifferentialOperators::FieldMetric DifferentialOperators::Laplace_par(const Field2D& f, + const Field2D& g22, + const Field2D& J, CELL_LOC outloc) { ASSERT1(location == outloc || outloc == CELL_DEFAULT) - return D2DY2(f, outloc) / g22() + DDY(J() / g22(), outloc) * DDY(f, outloc) / J(); + return D2DY2(f, outloc) / g22 + DDY(J / g22, outloc) * DDY(f, outloc) / J; } -Field3D DifferentialOperators::Laplace_par(const Field3D& f, CELL_LOC outloc) { +Field3D DifferentialOperators::Laplace_par(const Field3D& f, const Field3D& g22, + const Field3D& J, CELL_LOC outloc) { ASSERT1(location == outloc || outloc == CELL_DEFAULT) - return D2DY2(f, outloc) / g22() + DDY(J() / g22(), outloc) * ::DDY(f, outloc) / J(); + return D2DY2(f, outloc) / g22 + DDY(J / g22, outloc) * ::DDY(f, outloc) / J; } // Full Laplacian operator on scalar field -DifferentialOperators::FieldMetric -DifferentialOperators::Laplace(const Field2D& f, CELL_LOC outloc, - const std::string& dfdy_boundary_conditions, - const std::string& dfdy_dy_region) { +DifferentialOperators::FieldMetric DifferentialOperators::Laplace( + const Field2D& f, MetricTensor& covariantMetricTensor, const Field2D& G1, + const Field2D& G2, const Field2D& G3, CELL_LOC outloc, + const std::string& dfdy_boundary_conditions, const std::string& dfdy_dy_region) { TRACE("DifferentialOperators::Laplace( Field2D )"); ASSERT1(location == outloc || outloc == CELL_DEFAULT) - auto result = G1 * DDX(f, outloc) + G2 * DDY(f, outloc) + g11() * D2DX2(f, outloc) - + g22() * D2DY2(f, outloc) - + 2.0 * g12() + auto result = G1 * DDX(f, outloc) + G2 * DDY(f, outloc) + + covariantMetricTensor.Getg11() * D2DX2(f, outloc) + + covariantMetricTensor.Getg22() * D2DY2(f, outloc) + + 2.0 * covariantMetricTensor.Getg12() * D2DXDY(f, outloc, "DEFAULT", "RGN_NOBNDRY", dfdy_boundary_conditions, dfdy_dy_region); return result; } -Field3D DifferentialOperators::Laplace(const Field3D& f, CELL_LOC outloc, +Field3D DifferentialOperators::Laplace(const Field3D& f, + MetricTensor& covariantMetricTensor, + const Field3D& G1, const Field3D& G2, + const Field3D& G3, CELL_LOC outloc, const std::string& dfdy_boundary_conditions, const std::string& dfdy_dy_region) { TRACE("DifferentialOperators::Laplace( Field3D )"); ASSERT1(location == outloc || outloc == CELL_DEFAULT) Field3D result = G1 * ::DDX(f, outloc) + G2 * ::DDY(f, outloc) + G3 * ::DDZ(f, outloc) - + g11() * D2DX2(f, outloc) + g22() * D2DY2(f, outloc) - + g33() * D2DZ2(f, outloc) + + covariantMetricTensor.Getg11() * D2DX2(f, outloc) + + covariantMetricTensor.Getg22() * D2DY2(f, outloc) + + covariantMetricTensor.Getg33() * D2DZ2(f, outloc) + 2.0 - * (g12() + * (covariantMetricTensor.Getg12() * D2DXDY(f, outloc, "DEFAULT", "RGN_NOBNDRY", dfdy_boundary_conditions, dfdy_dy_region) - + g13() * D2DXDZ(f, outloc) + g23() * D2DYDZ(f, outloc)); + + covariantMetricTensor.Getg13() * D2DXDZ(f, outloc) + + covariantMetricTensor.Getg23() * D2DYDZ(f, outloc)); return result; } @@ -376,7 +397,9 @@ Field3D DifferentialOperators::Laplace(const Field3D& f, CELL_LOC outloc, // Full perpendicular Laplacian, in form of inverse of Laplacian operator in LaplaceXY // solver Field2D DifferentialOperators::Laplace_perpXY([[maybe_unused]] const Field2D& A, - [[maybe_unused]] const Field2D& f) { + [[maybe_unused]] const Field2D& f), + MetricTensor& covariantMetricTensor, + const Field2D& J) { TRACE("DifferentialOperators::Laplace_perpXY( Field2D )"); #if not(BOUT_USE_METRIC_3D) Field2D result; @@ -387,45 +410,45 @@ Field2D DifferentialOperators::Laplace_perpXY([[maybe_unused]] const Field2D& A, // outer x boundary const auto outer_x_avg = [&i](const auto& f) { return 0.5 * (f[i] + f[i.xp()]); }; const BoutReal outer_x_A = outer_x_avg(A); - const BoutReal outer_x_J = outer_x_avg(J()); - const BoutReal outer_x_g11 = outer_x_avg(g11()); + const BoutReal outer_x_J = outer_x_avg(J); + const BoutReal outer_x_g11 = outer_x_avg(covariantMetricTensor.Getg11()); const BoutReal outer_x_dx = outer_x_avg(dx); const BoutReal outer_x_value = - outer_x_A * outer_x_J * outer_x_g11 / (J()[i] * outer_x_dx * dx[i]); + outer_x_A * outer_x_J * outer_x_g11 / (J[i] * outer_x_dx * dx[i]); result[i] += outer_x_value * (f[i.xp()] - f[i]); // inner x boundary const auto inner_x_avg = [&i](const auto& f) { return 0.5 * (f[i] + f[i.xm()]); }; const BoutReal inner_x_A = inner_x_avg(A); - const BoutReal inner_x_J = inner_x_avg(J()); - const BoutReal inner_x_g11 = inner_x_avg(g11()); + const BoutReal inner_x_J = inner_x_avg(J); + const BoutReal inner_x_g11 = inner_x_avg(covariantMetricTensor.Getg11()); const BoutReal inner_x_dx = inner_x_avg(dx); const BoutReal inner_x_value = - inner_x_A * inner_x_J * inner_x_g11 / (J()[i] * inner_x_dx * dx[i]); + inner_x_A * inner_x_J * inner_x_g11 / (J[i] * inner_x_dx * dx[i]); result[i] += inner_x_value * (f[i.xm()] - f[i]); // upper y boundary const auto upper_y_avg = [&i](const auto& f) { return 0.5 * (f[i] + f[i.yp()]); }; const BoutReal upper_y_A = upper_y_avg(A); - const BoutReal upper_y_J = upper_y_avg(J()); - const BoutReal upper_y_g_22 = upper_y_avg(g22()); - const BoutReal upper_y_g23 = upper_y_avg(g23()); - const BoutReal upper_y_g_23 = upper_y_avg(g23()); + const BoutReal upper_y_J = upper_y_avg(J); + const BoutReal upper_y_g_22 = upper_y_avg(covariantMetricTensor.Getg22()); + const BoutReal upper_y_g23 = upper_y_avg(covariantMetricTensor.Getg23()); + const BoutReal upper_y_g_23 = upper_y_avg(covariantMetricTensor.Getg23()); const BoutReal upper_y_dy = upper_y_avg(dy); const BoutReal upper_y_value = -upper_y_A * upper_y_J * upper_y_g23 * upper_y_g_23 - / (upper_y_g_22 * J()[i] * upper_y_dy * dy[i]); + / (upper_y_g_22 * J[i] * upper_y_dy * dy[i]); result[i] += upper_y_value * (f[i.yp()] - f[i]); // lower y boundary const auto lower_y_avg = [&i](const auto& f) { return 0.5 * (f[i] + f[i.ym()]); }; const BoutReal lower_y_A = lower_y_avg(A); - const BoutReal lower_y_J = lower_y_avg(J()); - const BoutReal lower_y_g_22 = lower_y_avg(g22()); - const BoutReal lower_y_g23 = lower_y_avg(g23()); - const BoutReal lower_y_g_23 = lower_y_avg(g23()); + const BoutReal lower_y_J = lower_y_avg(J); + const BoutReal lower_y_g_22 = lower_y_avg(covariantMetricTensor.Getg22()); + const BoutReal lower_y_g23 = lower_y_avg(covariantMetricTensor.Getg23()); + const BoutReal lower_y_g_23 = lower_y_avg(covariantMetricTensor.Getg23()); const BoutReal lower_y_dy = lower_y_avg(dy); const BoutReal lower_y_value = -lower_y_A * lower_y_J * lower_y_g23 * lower_y_g_23 - / (lower_y_g_22 * J()[i] * lower_y_dy * dy[i]); + / (lower_y_g_22 * J[i] * lower_y_dy * dy[i]); result[i] += lower_y_value * (f[i.ym()] - f[i]); } @@ -436,31 +459,33 @@ Field2D DifferentialOperators::Laplace_perpXY([[maybe_unused]] const Field2D& A, #endif } -const DifferentialOperators::FieldMetric& DifferentialOperators::invSg() const { +DifferentialOperators::FieldMetric& +DifferentialOperators::invSg(MetricTensor& covariantMetricTensor) const { if (invSgCache == nullptr) { auto ptr = std::make_unique(); - (*ptr) = 1.0 / sqrt(g22()); + (*ptr) = 1.0 / sqrt(covariantMetricTensor.Getg22()); invSgCache = std::move(ptr); } return *invSgCache; } const DifferentialOperators::FieldMetric& -DifferentialOperators::Grad2_par2_DDY_invSg(CELL_LOC outloc, +DifferentialOperators::Grad2_par2_DDY_invSg(MetricTensor& covariantMetricTensor, + CELL_LOC outloc, const std::string& method) const { if (auto search = Grad2_par2_DDY_invSgCache.find(method); search != Grad2_par2_DDY_invSgCache.end()) { return *search->second; } - invSg(); + invSg(covariantMetricTensor); // Communicate to get parallel slices - localmesh->communicate(*invSgCache); + mesh->communicate(*invSgCache); invSgCache->applyParallelBoundary("parallel_neumann"); // cache auto ptr = std::make_unique(); - *ptr = DDY(*invSgCache, outloc, method) * invSg(); + *ptr = DDY(*invSgCache, outloc, method) * invSg(covariantMetricTensor); Grad2_par2_DDY_invSgCache[method] = std::move(ptr); return *Grad2_par2_DDY_invSgCache[method]; } diff --git a/src/mesh/geometry.cxx b/src/mesh/geometry.cxx index 6038e8312a..c9fca645e8 100644 --- a/src/mesh/geometry.cxx +++ b/src/mesh/geometry.cxx @@ -407,16 +407,16 @@ Field3D interpolateAndExtrapolate(const Field3D& f_, CELL_LOC location, //Geometry::Geometry() {} -Geometry::Geometry( - // Coordinates& coordinates, - FieldMetric J, FieldMetric Bxy, FieldMetric g11, FieldMetric g22, FieldMetric g33, - FieldMetric g12, FieldMetric g13, FieldMetric g23, FieldMetric g_11, FieldMetric g_22, - FieldMetric g_33, FieldMetric g_12, FieldMetric g_13, FieldMetric g_23, - DifferentialOperators differential_operators) - : // coordinates(coordinates), - contravariantMetricTensor(g11, g22, g33, g12, g13, g23), - covariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23), this_J(J), this_Bxy(Bxy), - differential_operators(differential_operators) {} +//Geometry::Geometry( +// // Coordinates& coordinates, +// FieldMetric J, FieldMetric Bxy, FieldMetric g11, FieldMetric g22, FieldMetric g33, +// FieldMetric g12, FieldMetric g13, FieldMetric g23, FieldMetric g_11, FieldMetric g_22, +// FieldMetric g_33, FieldMetric g_12, FieldMetric g_13, FieldMetric g_23, +// DifferentialOperators differential_operators) +// : // coordinates(coordinates), +// contravariantMetricTensor(g11, g22, g33, g12, g13, g23), +// covariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23), this_J(J), this_Bxy(Bxy), +// differential_operators(differential_operators) {} Geometry::Geometry(Mesh* mesh, DifferentialOperators differential_operators) //bool force_interpolate_from_centre) From bba0c68c0d35b2ef2e54247d5e40c33cf8e89f3f Mon Sep 17 00:00:00 2001 From: tomc271 Date: Sun, 26 Nov 2023 15:30:53 +0000 Subject: [PATCH 184/491] More fixes. Extract method Geometry::invalidateAndRecalculateCachedVariables(). --- include/bout/coordinates.hxx | 189 ++++++++++++------------ include/bout/differential_operators.hxx | 7 +- include/bout/geometry.hxx | 17 +-- src/mesh/coordinates.cxx | 23 +-- src/mesh/differential_operators.cxx | 5 + src/mesh/geometry.cxx | 24 ++- 6 files changed, 136 insertions(+), 129 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index ca86284e9f..1b0de517a7 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -33,10 +33,10 @@ #ifndef BOUT_COORDINATES_H #define BOUT_COORDINATES_H -#include "geometry.hxx" -#include "metricTensor.hxx" #include "bout/field2d.hxx" #include "bout/field3d.hxx" +#include "bout/geometry.hxx" +#include "bout/metricTensor.hxx" #include "bout/paralleltransform.hxx" #include "bout/utils.hxx" #include @@ -60,7 +60,7 @@ public: /// force_interpolate_from_centre argument to true to always interpolate /// (useful if CELL_CENTRE Coordinates have been changed, so reading from file /// would not be correct). - Coordinates(Mesh* mesh, Options* options = nullptr, const CELL_LOC loc = CELL_CENTRE, + Coordinates(Mesh* mesh, Options* options = nullptr, CELL_LOC loc = CELL_CENTRE, const Coordinates* coords_in = nullptr, bool force_interpolate_from_centre = false); @@ -144,8 +144,8 @@ public: void calcCovariant(const std::string& region = "RGN_ALL"); /// Invert covariant metric to get contravariant components void calcContravariant(const std::string& region = "RGN_ALL"); - void jacobian(); ///< Calculate J and Bxy - void CalculateChristoffelSymbols(); /// Calculate Christoffel symbol terms + void jacobian(); ///< Calculate J and Bxy + // void CalculateChristoffelSymbols(); /// Calculate Christoffel symbol terms /////////////////////////////////////////////////////////// // Parallel transforms @@ -159,95 +159,96 @@ public: /// Return the parallel transform ParallelTransform& getParallelTransform() { - ASSERT1(transform != nullptr); + ASSERT1(transform != nullptr) return *transform; } - /////////////////////////////////////////////////////////// - // Operators - /////////////////////////////////////////////////////////// - - FieldMetric DDX(const Field2D& f, CELL_LOC outloc = CELL_DEFAULT, - const std::string& method = "DEFAULT", - const std::string& region = "RGN_NOBNDRY"); - - FieldMetric DDY(const Field2D& f, CELL_LOC outloc = CELL_DEFAULT, - const std::string& method = "DEFAULT", - const std::string& region = "RGN_NOBNDRY") const; - - FieldMetric DDZ(const Field2D& f, CELL_LOC outloc = CELL_DEFAULT, - const std::string& method = "DEFAULT", - const std::string& region = "RGN_NOBNDRY"); - - Field3D DDX(const Field3D& f, CELL_LOC outloc = CELL_DEFAULT, - const std::string& method = "DEFAULT", - const std::string& region = "RGN_NOBNDRY"); - - Field3D DDY(const Field3D& f, CELL_LOC outloc = CELL_DEFAULT, - const std::string& method = "DEFAULT", - const std::string& region = "RGN_NOBNDRY") const; - - Field3D DDZ(const Field3D& f, CELL_LOC outloc = CELL_DEFAULT, - const std::string& method = "DEFAULT", - const std::string& region = "RGN_NOBNDRY"); - - /// Gradient along magnetic field b.Grad(f) - FieldMetric Grad_par(const Field2D& var, CELL_LOC outloc = CELL_DEFAULT, - const std::string& method = "DEFAULT"); - - Field3D Grad_par(const Field3D& var, CELL_LOC outloc = CELL_DEFAULT, - const std::string& method = "DEFAULT"); - - /// Advection along magnetic field V*b.Grad(f) - FieldMetric Vpar_Grad_par(const Field2D& v, const Field2D& f, - CELL_LOC outloc = CELL_DEFAULT, - const std::string& method = "DEFAULT"); - - Field3D Vpar_Grad_par(const Field3D& v, const Field3D& f, - CELL_LOC outloc = CELL_DEFAULT, - const std::string& method = "DEFAULT"); - - /// Divergence along magnetic field Div(b*f) = B.Grad(f/B) - FieldMetric Div_par(const Field2D& f, CELL_LOC outloc = CELL_DEFAULT, - const std::string& method = "DEFAULT"); - - Field3D Div_par(const Field3D& f, CELL_LOC outloc = CELL_DEFAULT, - const std::string& method = "DEFAULT"); - - // Second derivative along magnetic field - FieldMetric Grad2_par2(const Field2D& f, CELL_LOC outloc = CELL_DEFAULT, - const std::string& method = "DEFAULT"); - - Field3D Grad2_par2(const Field3D& f, CELL_LOC outloc = CELL_DEFAULT, - const std::string& method = "DEFAULT"); - // Perpendicular Laplacian operator, using only X-Z derivatives - // NOTE: This might be better bundled with the Laplacian inversion code - // since it makes use of the same coefficients and FFT routines - FieldMetric Delp2(const Field2D& f, CELL_LOC outloc = CELL_DEFAULT, bool useFFT = true); - Field3D Delp2(const Field3D& f, CELL_LOC outloc = CELL_DEFAULT, bool useFFT = true); - FieldPerp Delp2(const FieldPerp& f, CELL_LOC outloc = CELL_DEFAULT, bool useFFT = true); - - // Full parallel Laplacian operator on scalar field - // Laplace_par(f) = Div( b (b dot Grad(f)) ) - FieldMetric Laplace_par(const Field2D& f, CELL_LOC outloc = CELL_DEFAULT); - Field3D Laplace_par(const Field3D& f, CELL_LOC outloc = CELL_DEFAULT); - - // Full Laplacian operator on scalar field - FieldMetric Laplace(const Field2D& f, CELL_LOC outloc = CELL_DEFAULT, - const std::string& dfdy_boundary_conditions = "free_o3", - const std::string& dfdy_dy_region = ""); - Field3D Laplace(const Field3D& f, CELL_LOC outloc = CELL_DEFAULT, - const std::string& dfdy_boundary_conditions = "free_o3", - const std::string& dfdy_dy_region = ""); - - // Full perpendicular Laplacian, in form of inverse of Laplacian operator in LaplaceXY - // solver - Field2D Laplace_perpXY(const Field2D& A, const Field2D& f); + // /////////////////////////////////////////////////////////// + // // Operators + // /////////////////////////////////////////////////////////// + // + // FieldMetric DDX(const Field2D& f, CELL_LOC outloc = CELL_DEFAULT, + // const std::string& method = "DEFAULT", + // const std::string& region = "RGN_NOBNDRY"); + // + // FieldMetric DDY(const Field2D& f, CELL_LOC outloc = CELL_DEFAULT, + // const std::string& method = "DEFAULT", + // const std::string& region = "RGN_NOBNDRY") const; + // + // FieldMetric DDZ(const Field2D& f, CELL_LOC outloc = CELL_DEFAULT, + // const std::string& method = "DEFAULT", + // const std::string& region = "RGN_NOBNDRY"); + // + // Field3D DDX(const Field3D& f, CELL_LOC outloc = CELL_DEFAULT, + // const std::string& method = "DEFAULT", + // const std::string& region = "RGN_NOBNDRY"); + // + // Field3D DDY(const Field3D& f, CELL_LOC outloc = CELL_DEFAULT, + // const std::string& method = "DEFAULT", + // const std::string& region = "RGN_NOBNDRY") const; + // + // Field3D DDZ(const Field3D& f, CELL_LOC outloc = CELL_DEFAULT, + // const std::string& method = "DEFAULT", + // const std::string& region = "RGN_NOBNDRY"); + // + // /// Gradient along magnetic field b.Grad(f) + // FieldMetric Grad_par(const Field2D& var, CELL_LOC outloc = CELL_DEFAULT, + // const std::string& method = "DEFAULT"); + // + // Field3D Grad_par(const Field3D& var, CELL_LOC outloc = CELL_DEFAULT, + // const std::string& method = "DEFAULT"); + // + // /// Advection along magnetic field V*b.Grad(f) + // FieldMetric Vpar_Grad_par(const Field2D& v, const Field2D& f, + // CELL_LOC outloc = CELL_DEFAULT, + // const std::string& method = "DEFAULT"); + // + // Field3D Vpar_Grad_par(const Field3D& v, const Field3D& f, + // CELL_LOC outloc = CELL_DEFAULT, + // const std::string& method = "DEFAULT"); + // + // /// Divergence along magnetic field Div(b*f) = B.Grad(f/B) + // FieldMetric Div_par(const Field2D& f, CELL_LOC outloc = CELL_DEFAULT, + // const std::string& method = "DEFAULT"); + // + // Field3D Div_par(const Field3D& f, CELL_LOC outloc = CELL_DEFAULT, + // const std::string& method = "DEFAULT"); + // + // // Second derivative along magnetic field + // FieldMetric Grad2_par2(const Field2D& f, CELL_LOC outloc = CELL_DEFAULT, + // const std::string& method = "DEFAULT"); + // + // Field3D Grad2_par2(const Field3D& f, CELL_LOC outloc = CELL_DEFAULT, + // const std::string& method = "DEFAULT"); + // // Perpendicular Laplacian operator, using only X-Z derivatives + // // NOTE: This might be better bundled with the Laplacian inversion code + // // since it makes use of the same coefficients and FFT routines + // FieldMetric Delp2(const Field2D& f, CELL_LOC outloc = CELL_DEFAULT, bool useFFT = true); + // Field3D Delp2(const Field3D& f, CELL_LOC outloc = CELL_DEFAULT, bool useFFT = true); + // FieldPerp Delp2(const FieldPerp& f, CELL_LOC outloc = CELL_DEFAULT, bool useFFT = true); + // + // // Full parallel Laplacian operator on scalar field + // // Laplace_par(f) = Div( b (b dot Grad(f)) ) + // FieldMetric Laplace_par(const Field2D& f, CELL_LOC outloc = CELL_DEFAULT); + // Field3D Laplace_par(const Field3D& f, CELL_LOC outloc = CELL_DEFAULT); + // + // // Full Laplacian operator on scalar field + // FieldMetric Laplace(const Field2D& f, CELL_LOC outloc = CELL_DEFAULT, + // const std::string& dfdy_boundary_conditions = "free_o3", + // const std::string& dfdy_dy_region = ""); + // Field3D Laplace(const Field3D& f, CELL_LOC outloc = CELL_DEFAULT, + // const std::string& dfdy_boundary_conditions = "free_o3", + // const std::string& dfdy_dy_region = ""); + // + // // Full perpendicular Laplacian, in form of inverse of Laplacian operator in LaplaceXY + // // solver + // Field2D Laplace_perpXY(const Field2D& A, const Field2D& f); private: int nz; // Size of mesh in Z. This is mesh->ngz-1 Mesh* localmesh; CELL_LOC location; + DifferentialOperators differential_operators; Geometry geometry; /// Handles calculation of yup and ydown @@ -257,17 +258,17 @@ private: /// `Coordinates::calculateGeometry` is called mutable std::unique_ptr zlength_cache{nullptr}; -// /// Cache variable for Grad2_par2 -// mutable std::map> Grad2_par2_DDY_invSgCache; -// mutable std::unique_ptr invSgCache{nullptr}; + // /// Cache variable for Grad2_par2 + // mutable std::map> Grad2_par2_DDY_invSgCache; + // mutable std::unique_ptr invSgCache{nullptr}; /// Set the parallel (y) transform from the options file. /// Used in the constructor to create the transform object. void setParallelTransform(Options* options); -// const FieldMetric& invSg() const; -// const FieldMetric& Grad2_par2_DDY_invSg(CELL_LOC outloc, - const std::string& method) const; + // const FieldMetric& invSg() const; + // const FieldMetric& Grad2_par2_DDY_invSg(CELL_LOC outloc, + // const std::string& method) const; // check that covariant tensors are positive (if expected) and finite (always) void checkCovariant(); @@ -293,8 +294,8 @@ private: // FieldMetric this_J; // FieldMetric this_Bxy; ///< Magnitude of B = nabla z times nabla x - FieldMetric recalculateJacobian(); - FieldMetric recalculateBxy(); + // FieldMetric recalculateJacobian(); + // FieldMetric recalculateBxy(); }; /* diff --git a/include/bout/differential_operators.hxx b/include/bout/differential_operators.hxx index 38ee1b0ce8..563bfa9717 100644 --- a/include/bout/differential_operators.hxx +++ b/include/bout/differential_operators.hxx @@ -5,7 +5,7 @@ #include "bout/field2d.hxx" #include "bout/field3d.hxx" //#include "bout/index_derivs_interface.hxx" -#include "metricTensor.hxx" +#include "bout/metricTensor.hxx" #include "bout/paralleltransform.hxx" #include @@ -22,6 +22,9 @@ public: const CELL_LOC& location, FieldMetric& dx, FieldMetric& dy, FieldMetric& dz); + // DifferentialOperators(DifferentialOperators operators, + // DifferentialOperators::FieldMetric& dx); + FieldMetric DDX(const Field2D& f, CELL_LOC outloc = CELL_DEFAULT, const std::string& method = "DEFAULT", const std::string& region = "RGN_NOBNDRY"); @@ -119,6 +122,8 @@ public: Field2D Laplace_perpXY(const Field2D& A, const Field2D& f, MetricTensor& covariantMetricTensor, const Field2D& J); + void invalidateAndRecalculateCachedVariables(); + private: Mesh* mesh; FieldMetric& intShiftTorsion; diff --git a/include/bout/geometry.hxx b/include/bout/geometry.hxx index 58eea7aa38..8a0011bfb9 100644 --- a/include/bout/geometry.hxx +++ b/include/bout/geometry.hxx @@ -56,16 +56,13 @@ public: using FieldMetric = Field2D; #endif - // Geometry(); + Geometry(FieldMetric J, FieldMetric Bxy, FieldMetric g11, FieldMetric g22, + FieldMetric g33, FieldMetric g12, FieldMetric g13, FieldMetric g23, + FieldMetric g_11, FieldMetric g_22, FieldMetric g_33, FieldMetric g_12, + FieldMetric g_13, FieldMetric g_23, + DifferentialOperators& differential_operators); -// Geometry( -// // Coordinates& coordinates, -// FieldMetric J, FieldMetric Bxy, FieldMetric g11, FieldMetric g22, FieldMetric g33, -// FieldMetric g12, FieldMetric g13, FieldMetric g23, FieldMetric g_11, -// FieldMetric g_22, FieldMetric g_33, FieldMetric g_12, FieldMetric g_13, -// FieldMetric g_23, DifferentialOperators differential_operators); - - Geometry(Mesh* mesh, DifferentialOperators differential_operators); + Geometry(Mesh* mesh, DifferentialOperators& differential_operators); /// Christoffel symbol of the second kind (connection coefficients) FieldMetric G1_11, G1_22, G1_33, G1_12, G1_13, G1_23; @@ -146,7 +143,7 @@ private: FieldMetric this_J; FieldMetric this_Bxy; ///< Magnitude of B = nabla z times nabla x - DifferentialOperators differential_operators; + DifferentialOperators& differential_operators; // template // void communicate(T& t, Ts... ts); diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 17cb0c6a49..03fde30605 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -297,8 +297,10 @@ Coordinates::Coordinates(Mesh* mesh, FieldMetric dx, FieldMetric dy, FieldMetric FieldMetric IntShiftTorsion) : dx(std::move(dx)), dy(std::move(dy)), dz(dz), ShiftTorsion(std::move(ShiftTorsion)), IntShiftTorsion(std::move(IntShiftTorsion)), nz(mesh->LocalNz), localmesh(mesh), - location(CELL_CENTRE), geometry(Geometry(J, Bxy, g11, g22, g33, g12, g13, g23, g_11, - g_22, g_33, g_12, g_13, g_23)) {} + location(CELL_CENTRE), differential_operators(DifferentialOperators( + mesh, IntShiftTorsion, location, dx, dy, dz)), + geometry(Geometry(J, Bxy, g11, g22, g33, g12, g13, g23, g_11, g_22, g_33, g_12, + g_13, g_23, differential_operators)) {} Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, const Coordinates* coords_in, bool force_interpolate_from_centre) @@ -308,8 +310,9 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, // G3_11(mesh), G3_22(mesh), G3_33(mesh), G3_12(mesh), G3_13(mesh), G3_23(mesh), // G1(mesh), G2(mesh), G3(mesh), ShiftTorsion(mesh), IntShiftTorsion(mesh), localmesh(mesh), location(loc), - geometry( - Geometry(mesh, DifferentialOperators(mesh, IntShiftTorsion, loc, dx, dy, dz))) { + differential_operators( + DifferentialOperators(mesh, IntShiftTorsion, loc, dx, dy, dz)), + geometry(Geometry(mesh, differential_operators)) { if (options == nullptr) { options = Options::getRoot()->getSection("mesh"); @@ -879,8 +882,7 @@ int Coordinates::calculateGeometry(bool recalculate_staggered, // Invalidate and recalculate cached variables zlength_cache.reset(); - Grad2_par2_DDY_invSgCache.clear(); - invSgCache.reset(); + differential_operators.invalidateAndRecalculateCachedVariables(); return 0; } @@ -1493,18 +1495,18 @@ void Coordinates::setParallelTransform(Options* options) { // // return result; //} -// + //Coordinates::FieldMetric Coordinates::Laplace_par(const Field2D& f, CELL_LOC outloc) { // ASSERT1(location == outloc || outloc == CELL_DEFAULT) // // return D2DY2(f, outloc) / g22() + DDY(J() / g22(), outloc) * DDY(f, outloc) / J(); //} -// + //Field3D Coordinates::Laplace_par(const Field3D& f, CELL_LOC outloc) { // ASSERT1(location == outloc || outloc == CELL_DEFAULT) // return D2DY2(f, outloc) / g22() + DDY(J() / g22(), outloc) * ::DDY(f, outloc) / J(); //} -// + //// Full Laplacian operator on scalar field // //Coordinates::FieldMetric Coordinates::Laplace(const Field2D& f, CELL_LOC outloc, @@ -1680,6 +1682,5 @@ void Coordinates::setBxy(FieldMetric Bxy) { const MetricTensor& Coordinates::getContravariantMetricTensor() const { return geometry.getContravariantMetricTensor(); } -const Coordinates::FieldMetric& Coordinates::invSg() const { -} +//const Coordinates::FieldMetric& Coordinates::invSg() const {} diff --git a/src/mesh/differential_operators.cxx b/src/mesh/differential_operators.cxx index 81328a6a6c..005769121f 100644 --- a/src/mesh/differential_operators.cxx +++ b/src/mesh/differential_operators.cxx @@ -489,3 +489,8 @@ DifferentialOperators::Grad2_par2_DDY_invSg(MetricTensor& covariantMetricTensor, Grad2_par2_DDY_invSgCache[method] = std::move(ptr); return *Grad2_par2_DDY_invSgCache[method]; } + +void DifferentialOperators::invalidateAndRecalculateCachedVariables() { + Grad2_par2_DDY_invSgCache.clear(); + invSgCache.reset(); +} diff --git a/src/mesh/geometry.cxx b/src/mesh/geometry.cxx index c9fca645e8..712448cf02 100644 --- a/src/mesh/geometry.cxx +++ b/src/mesh/geometry.cxx @@ -6,7 +6,7 @@ //#include //#include -#include +//#include #include //#include //#include @@ -407,19 +407,17 @@ Field3D interpolateAndExtrapolate(const Field3D& f_, CELL_LOC location, //Geometry::Geometry() {} -//Geometry::Geometry( -// // Coordinates& coordinates, -// FieldMetric J, FieldMetric Bxy, FieldMetric g11, FieldMetric g22, FieldMetric g33, -// FieldMetric g12, FieldMetric g13, FieldMetric g23, FieldMetric g_11, FieldMetric g_22, -// FieldMetric g_33, FieldMetric g_12, FieldMetric g_13, FieldMetric g_23, -// DifferentialOperators differential_operators) -// : // coordinates(coordinates), -// contravariantMetricTensor(g11, g22, g33, g12, g13, g23), -// covariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23), this_J(J), this_Bxy(Bxy), -// differential_operators(differential_operators) {} +Geometry::Geometry(FieldMetric J, FieldMetric Bxy, FieldMetric g11, FieldMetric g22, + FieldMetric g33, FieldMetric g12, FieldMetric g13, FieldMetric g23, + FieldMetric g_11, FieldMetric g_22, FieldMetric g_33, FieldMetric g_12, + FieldMetric g_13, FieldMetric g_23, + DifferentialOperators& differential_operators) + : contravariantMetricTensor(g11, g22, g33, g12, g13, g23), + covariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23), this_J(J), this_Bxy(Bxy), + differential_operators(differential_operators) {} -Geometry::Geometry(Mesh* mesh, DifferentialOperators differential_operators) - //bool force_interpolate_from_centre) +Geometry::Geometry(Mesh* mesh, DifferentialOperators& differential_operators) + //bool force_interpolate_from_centre : G1_11(mesh), G1_22(mesh), G1_33(mesh), G1_12(mesh), G1_13(mesh), G1_23(mesh), G2_11(mesh), G2_22(mesh), G2_33(mesh), G2_12(mesh), G2_13(mesh), G2_23(mesh), G3_11(mesh), G3_22(mesh), G3_33(mesh), G3_12(mesh), G3_13(mesh), G3_23(mesh), From 558550e4fa6393432aaee51af655155a2c0f5af9 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Sun, 26 Nov 2023 15:59:47 +0000 Subject: [PATCH 185/491] Coordinates::J() and Geometry::J() can't work with communicate(T& t, Ts... ts) function if they are const. --- include/bout/coordinates.hxx | 2 +- include/bout/geometry.hxx | 2 +- src/mesh/coordinates.cxx | 2 +- src/mesh/geometry.cxx | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index 1b0de517a7..8acd032b26 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -107,7 +107,7 @@ public: const FieldMetric& g23() const; ///< Coordinate system Jacobian, so volume of cell is J*dx*dy*dz - const FieldMetric& J() const; + FieldMetric& J() const; ///< Magnitude of B = nabla z times nabla x const FieldMetric& Bxy() const; diff --git a/include/bout/geometry.hxx b/include/bout/geometry.hxx index 8a0011bfb9..6edd733c7c 100644 --- a/include/bout/geometry.hxx +++ b/include/bout/geometry.hxx @@ -91,7 +91,7 @@ public: // MetricTensor& getCovariantMetricTensor(); ///< Coordinate system Jacobian, so volume of cell is J*dx*dy*dz - const FieldMetric& J() const; + FieldMetric J() const; ///< Magnitude of B = nabla z times nabla x const FieldMetric& Bxy() const; diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 03fde30605..3c4cd906de 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -1660,7 +1660,7 @@ const MetricTensor::FieldMetric& Coordinates::g12() const { return geometry.g12( const MetricTensor::FieldMetric& Coordinates::g13() const { return geometry.g13(); } const MetricTensor::FieldMetric& Coordinates::g23() const { return geometry.g23(); } -const MetricTensor::FieldMetric& Coordinates::J() const { return geometry.J(); } +MetricTensor::FieldMetric& Coordinates::J() const { return geometry.J(); } const MetricTensor::FieldMetric& Coordinates::Bxy() const { return geometry.Bxy(); } diff --git a/src/mesh/geometry.cxx b/src/mesh/geometry.cxx index 712448cf02..8bb53f0a60 100644 --- a/src/mesh/geometry.cxx +++ b/src/mesh/geometry.cxx @@ -1569,7 +1569,7 @@ const MetricTensor::FieldMetric& Geometry::g23() const { return contravariantMetricTensor.Getg23(); } -const Geometry::FieldMetric& Geometry::J() const { return this_J; } +Geometry::FieldMetric Geometry::J() const { return this_J; } const Geometry::FieldMetric& Geometry::Bxy() const { return this_Bxy; } From 64fd6519fcc7b108d83c50524c7117f78b05fa0b Mon Sep 17 00:00:00 2001 From: tomc271 Date: Sun, 26 Nov 2023 16:21:51 +0000 Subject: [PATCH 186/491] Sort out #include statements. --- CMakeLists.txt | 4 ++-- include/bout/coordinates.hxx | 8 ++++---- include/bout/differential_operators.hxx | 11 ++++++----- include/bout/geometry.hxx | 10 +++++----- src/mesh/differential_operators.cxx | 8 ++++++++ src/mesh/geometry.cxx | 10 +++++++++- 6 files changed, 34 insertions(+), 17 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index cd76dd005b..90d6255717 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -352,12 +352,12 @@ set(BOUT_SOURCES ./src/sys/timer.cxx ./src/sys/type_name.cxx ./src/sys/utils.cxx - ./src/mesh/metricTensor.cxx ./include/bout/metricTensor.hxx + ./src/mesh/metricTensor.cxx ./include/bout/geometry.hxx ./src/mesh/geometry.cxx - ./src/mesh/differential_operators.cxx ./include/bout/differential_operators.hxx + ./src/mesh/differential_operators.cxx ${CMAKE_CURRENT_BINARY_DIR}/include/bout/revision.hxx ${CMAKE_CURRENT_BINARY_DIR}/include/bout/version.hxx ) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index 8acd032b26..620d4cf8c7 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -33,13 +33,13 @@ #ifndef BOUT_COORDINATES_H #define BOUT_COORDINATES_H -#include "bout/field2d.hxx" -#include "bout/field3d.hxx" +//#include "bout/field2d.hxx" +//#include "bout/field3d.hxx" #include "bout/geometry.hxx" #include "bout/metricTensor.hxx" #include "bout/paralleltransform.hxx" -#include "bout/utils.hxx" -#include +//#include "bout/utils.hxx" +//#include class Mesh; diff --git a/include/bout/differential_operators.hxx b/include/bout/differential_operators.hxx index 563bfa9717..7867013b94 100644 --- a/include/bout/differential_operators.hxx +++ b/include/bout/differential_operators.hxx @@ -2,12 +2,13 @@ #ifndef BOUT_DIFFERENTIALOPERATORS_HXX #define BOUT_DIFFERENTIALOPERATORS_HXX -#include "bout/field2d.hxx" -#include "bout/field3d.hxx" -//#include "bout/index_derivs_interface.hxx" +//#include "bout/field2d.hxx" +//#include "bout/field3d.hxx" #include "bout/metricTensor.hxx" -#include "bout/paralleltransform.hxx" -#include +//#include "bout/mesh.hxx" +////#include "bout/index_derivs_interface.hxx" +//#include "bout/paralleltransform.hxx" +//#include class DifferentialOperators { diff --git a/include/bout/geometry.hxx b/include/bout/geometry.hxx index 6edd733c7c..ce61115ade 100644 --- a/include/bout/geometry.hxx +++ b/include/bout/geometry.hxx @@ -33,12 +33,12 @@ #ifndef __GEOMETRY_H__ #define __GEOMETRY_H__ -#include "coordinates.hxx" +//#include "coordinates.hxx" #include "differential_operators.hxx" -#include "metricTensor.hxx" -#include "bout/field2d.hxx" -#include "bout/field3d.hxx" -#include "bout/paralleltransform.hxx" +//#include "metricTensor.hxx" +//#include "bout/field2d.hxx" +//#include "bout/field3d.hxx" +//#include "bout/paralleltransform.hxx" #include "bout/utils.hxx" #include diff --git a/src/mesh/differential_operators.cxx b/src/mesh/differential_operators.cxx index 005769121f..dffeb1f67a 100644 --- a/src/mesh/differential_operators.cxx +++ b/src/mesh/differential_operators.cxx @@ -1,6 +1,14 @@ #include "bout/differential_operators.hxx" +//#include "bout/mesh.hxx" + +#include "bout/field2d.hxx" +#include "bout/field3d.hxx" +//#include "bout/metricTensor.hxx" #include "bout/mesh.hxx" +//#include "bout/index_derivs_interface.hxx" +#include "bout/paralleltransform.hxx" +#include DifferentialOperators::DifferentialOperators(Mesh* mesh, FieldMetric& intShiftTorsion, const CELL_LOC& location, FieldMetric& dx, diff --git a/src/mesh/geometry.cxx b/src/mesh/geometry.cxx index 8bb53f0a60..e42222e90f 100644 --- a/src/mesh/geometry.cxx +++ b/src/mesh/geometry.cxx @@ -4,10 +4,18 @@ * given the contravariant metric tensor terms **************************************************************************/ +#include "bout/field2d.hxx" +#include "bout/field3d.hxx" +#include "bout/geometry.hxx" +//#include "bout/metricTensor.hxx" +#include "bout/paralleltransform.hxx" +#include "bout/utils.hxx" +#include + //#include //#include //#include -#include +//#include //#include //#include //#include From cbffeb6c153af509b4e4c1f44fd603522a92f44d Mon Sep 17 00:00:00 2001 From: tomc271 Date: Sun, 26 Nov 2023 16:24:31 +0000 Subject: [PATCH 187/491] Remove commented-out #include statements. --- include/bout/coordinates.hxx | 4 ---- include/bout/differential_operators.hxx | 8 ++------ src/mesh/coordinates.cxx | 1 - src/mesh/geometry.cxx | 22 +--------------------- 4 files changed, 3 insertions(+), 32 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index 620d4cf8c7..6913bb7aec 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -33,13 +33,9 @@ #ifndef BOUT_COORDINATES_H #define BOUT_COORDINATES_H -//#include "bout/field2d.hxx" -//#include "bout/field3d.hxx" #include "bout/geometry.hxx" #include "bout/metricTensor.hxx" #include "bout/paralleltransform.hxx" -//#include "bout/utils.hxx" -//#include class Mesh; diff --git a/include/bout/differential_operators.hxx b/include/bout/differential_operators.hxx index 7867013b94..8bbd1c6429 100644 --- a/include/bout/differential_operators.hxx +++ b/include/bout/differential_operators.hxx @@ -2,13 +2,9 @@ #ifndef BOUT_DIFFERENTIALOPERATORS_HXX #define BOUT_DIFFERENTIALOPERATORS_HXX -//#include "bout/field2d.hxx" -//#include "bout/field3d.hxx" #include "bout/metricTensor.hxx" -//#include "bout/mesh.hxx" -////#include "bout/index_derivs_interface.hxx" -//#include "bout/paralleltransform.hxx" -//#include +//#include "bout/index_derivs_interface.hxx" + class DifferentialOperators { diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 3c4cd906de..857be5af44 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -11,7 +11,6 @@ #include #include -//#include #include #include diff --git a/src/mesh/geometry.cxx b/src/mesh/geometry.cxx index e42222e90f..654786e999 100644 --- a/src/mesh/geometry.cxx +++ b/src/mesh/geometry.cxx @@ -4,31 +4,11 @@ * given the contravariant metric tensor terms **************************************************************************/ -#include "bout/field2d.hxx" -#include "bout/field3d.hxx" #include "bout/geometry.hxx" -//#include "bout/metricTensor.hxx" -#include "bout/paralleltransform.hxx" +#include "bout/field2d.hxx" #include "bout/utils.hxx" #include -//#include -//#include -//#include -//#include -//#include -//#include -//#include -// -//#include -//#include -//#include -// -//#include -// -//#include "parallel/fci.hxx" -//#include "parallel/shiftedmetricinterp.hxx" - //// use anonymous namespace so this utility function is not available outside this file //namespace { From bebfc4311d2fb079a196ce3e78ef8f22dcff5f5b Mon Sep 17 00:00:00 2001 From: tomc271 Date: Sun, 26 Nov 2023 16:30:17 +0000 Subject: [PATCH 188/491] include derivs.hxx, so that Coordinates methods can access differential operator functions. --- src/mesh/coordinates.cxx | 1 + 1 file changed, 1 insertion(+) diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 857be5af44..76e20576f2 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -18,6 +18,7 @@ #include "parallel/fci.hxx" #include "parallel/shiftedmetricinterp.hxx" +#include "bout/derivs.hxx" #include "bout/differential_operators.hxx" #include "bout/field2d.hxx" #include "bout/field3d.hxx" From 0f1256886158bd6da8025ae6e4abc0ea7960780e Mon Sep 17 00:00:00 2001 From: tomc271 Date: Sun, 26 Nov 2023 16:44:17 +0000 Subject: [PATCH 189/491] Sort out more #include statements. "using FieldMetric = MetricTensor::FieldMetric" instead of duplicated 'using' statements. --- include/bout/differential_operators.hxx | 8 +-- include/bout/geometry.hxx | 18 ++---- src/mesh/differential_operators.cxx | 85 ++++++++++++------------- src/mesh/geometry.cxx | 5 +- 4 files changed, 48 insertions(+), 68 deletions(-) diff --git a/include/bout/differential_operators.hxx b/include/bout/differential_operators.hxx index 8bbd1c6429..e26ede3b1c 100644 --- a/include/bout/differential_operators.hxx +++ b/include/bout/differential_operators.hxx @@ -5,15 +5,9 @@ #include "bout/metricTensor.hxx" //#include "bout/index_derivs_interface.hxx" - class DifferentialOperators { -public: -#if BOUT_USE_METRIC_3D - using FieldMetric = Field3D; -#else - using FieldMetric = Field2D; -#endif + using FieldMetric = MetricTensor::FieldMetric; DifferentialOperators(Mesh* mesh, FieldMetric& intShiftTorsion, const CELL_LOC& location, FieldMetric& dx, FieldMetric& dy, diff --git a/include/bout/geometry.hxx b/include/bout/geometry.hxx index ce61115ade..7e29dc5a76 100644 --- a/include/bout/geometry.hxx +++ b/include/bout/geometry.hxx @@ -33,14 +33,12 @@ #ifndef __GEOMETRY_H__ #define __GEOMETRY_H__ -//#include "coordinates.hxx" #include "differential_operators.hxx" -//#include "metricTensor.hxx" -//#include "bout/field2d.hxx" -//#include "bout/field3d.hxx" -//#include "bout/paralleltransform.hxx" -#include "bout/utils.hxx" -#include +#include "metricTensor.hxx" +//#include "bout/utils.hxx" +//#include + +using FieldMetric = MetricTensor::FieldMetric; /*! * Represents the geometry a coordinate system, and associated operators @@ -50,12 +48,6 @@ class Geometry { public: -#if BOUT_USE_METRIC_3D - using FieldMetric = Field3D; -#else - using FieldMetric = Field2D; -#endif - Geometry(FieldMetric J, FieldMetric Bxy, FieldMetric g11, FieldMetric g22, FieldMetric g33, FieldMetric g12, FieldMetric g13, FieldMetric g23, FieldMetric g_11, FieldMetric g_22, FieldMetric g_33, FieldMetric g_12, diff --git a/src/mesh/differential_operators.cxx b/src/mesh/differential_operators.cxx index dffeb1f67a..7c1e191aaf 100644 --- a/src/mesh/differential_operators.cxx +++ b/src/mesh/differential_operators.cxx @@ -1,14 +1,12 @@ #include "bout/differential_operators.hxx" +//#include "bout/field2d.hxx" +//#include "bout/field3d.hxx" //#include "bout/mesh.hxx" - -#include "bout/field2d.hxx" -#include "bout/field3d.hxx" //#include "bout/metricTensor.hxx" -#include "bout/mesh.hxx" //#include "bout/index_derivs_interface.hxx" -#include "bout/paralleltransform.hxx" -#include +//#include "bout/paralleltransform.hxx" +//#include DifferentialOperators::DifferentialOperators(Mesh* mesh, FieldMetric& intShiftTorsion, const CELL_LOC& location, FieldMetric& dx, @@ -16,10 +14,9 @@ DifferentialOperators::DifferentialOperators(Mesh* mesh, FieldMetric& intShiftTo : mesh(mesh), intShiftTorsion(intShiftTorsion), location(location), dx(dx), dy(dy), dz(dz) {} -DifferentialOperators::FieldMetric DifferentialOperators::DDX(const Field2D& f, - CELL_LOC loc, - const std::string& method, - const std::string& region) { +MetricTensor::FieldMetric DifferentialOperators::DDX(const Field2D& f, CELL_LOC loc, + const std::string& method, + const std::string& region) { ASSERT1(location == loc || loc == CELL_DEFAULT) return bout::derivatives::index::DDX(f, loc, method, region) / dx; } @@ -37,9 +34,9 @@ Field3D DifferentialOperators::DDX(const Field3D& f, CELL_LOC outloc, return result; } -DifferentialOperators::FieldMetric -DifferentialOperators::DDY(const Field2D& f, CELL_LOC loc, const std::string& method, - const std::string& region) const { +FieldMetric DifferentialOperators::DDY(const Field2D& f, CELL_LOC loc, + const std::string& method, + const std::string& region) const { ASSERT1(location == loc || loc == CELL_DEFAULT) return bout::derivatives::index::DDY(f, loc, method, region) / dy; } @@ -58,10 +55,9 @@ Field3D DifferentialOperators::DDY(const Field3D& f, CELL_LOC outloc, return bout::derivatives::index::DDY(f, outloc, method, region) / dy; } -DifferentialOperators::FieldMetric -DifferentialOperators::DDZ(const Field2D& f, CELL_LOC loc, - const std::string& UNUSED(method), - const std::string& UNUSED(region)) { +FieldMetric DifferentialOperators::DDZ(const Field2D& f, CELL_LOC loc, + const std::string& UNUSED(method), + const std::string& UNUSED(region)) { ASSERT1(location == loc || loc == CELL_DEFAULT) ASSERT1(f.getMesh() == mesh) if (loc == CELL_DEFAULT) { @@ -77,10 +73,10 @@ Field3D DifferentialOperators::DDZ(const Field3D& f, CELL_LOC outloc, ///////////////////////////////////////////////////////// // Parallel gradient -DifferentialOperators::FieldMetric -DifferentialOperators::Grad_par(const Field2D& var, MetricTensor& covariantMetricTensor, - [[maybe_unused]] CELL_LOC outloc, - const std::string& UNUSED(method)) { +FieldMetric DifferentialOperators::Grad_par(const Field2D& var, + MetricTensor& covariantMetricTensor, + [[maybe_unused]] CELL_LOC outloc, + const std::string& UNUSED(method)) { TRACE("DifferentialOperators::Grad_par( Field2D )"); ASSERT1(location == outloc || (outloc == CELL_DEFAULT && location == var.getLocation())) @@ -100,10 +96,10 @@ Field3D DifferentialOperators::Grad_par(const Field3D& var, // Vpar_Grad_par // vparallel times the parallel derivative along unperturbed B-field -DifferentialOperators::FieldMetric -DifferentialOperators::FieldMetric DifferentialOperators::Vpar_Grad_par( - const Field2D& v, const Field2D& f, MetricTensor& covariantMetricTensor, - [[maybe_unused]] CELL_LOC outloc, const std::string& UNUSED(method)) { +FieldMetric DifferentialOperators::Vpar_Grad_par(const Field2D& v, const Field2D& f, + MetricTensor& covariantMetricTensor, + [[maybe_unused]] CELL_LOC outloc, + const std::string& UNUSED(method)) { ASSERT1(location == outloc || (outloc == CELL_DEFAULT && location == f.getLocation())) return VDDY(v, f) * invSg(covariantMetricTensor); @@ -120,9 +116,8 @@ Field3D DifferentialOperators::Vpar_Grad_par(const Field3D& v, const Field3D& f, ///////////////////////////////////////////////////////// // Parallel divergence -DifferentialOperators::FieldMetric -DifferentialOperators::Div_par(const Field2D& f, const Field2D& Bxy, CELL_LOC outloc, - const std::string& method) { +FieldMetric DifferentialOperators::Div_par(const Field2D& f, const Field2D& Bxy, + CELL_LOC outloc, const std::string& method) { TRACE("DifferentialOperators::Div_par( Field2D )"); ASSERT1(location == outloc || outloc == CELL_DEFAULT) @@ -162,9 +157,10 @@ Field3D DifferentialOperators::Div_par(const Field3D& f, const Field2D& Bxy, // second parallel derivative (b dot Grad)(b dot Grad) // Note: For parallel Laplacian use Laplace_par -DifferentialOperators::FieldMetric -DifferentialOperators::Grad2_par2(const Field2D& f, MetricTensor& covariantMetricTensor, - CELL_LOC outloc, const std::string& method) { +FieldMetric DifferentialOperators::Grad2_par2(const Field2D& f, + MetricTensor& covariantMetricTensor, + CELL_LOC outloc, + const std::string& method) { TRACE("DifferentialOperators::Grad2_par2( Field2D )"); ASSERT1(location == outloc || (outloc == CELL_DEFAULT && location == f.getLocation())) @@ -200,9 +196,9 @@ Field3D DifferentialOperators::Grad2_par2(const Field3D& f, #include // Delp2 uses same coefficients as inversion code -DifferentialOperators::FieldMetric -DifferentialOperators::Delp2(const Field2D& f, const Field2D& g11, const Field2D& G1, - CELL_LOC outloc, bool UNUSED(useFFT)) { +FieldMetric DifferentialOperators::Delp2(const Field2D& f, const Field2D& g11, + const Field2D& G1, CELL_LOC outloc, + bool UNUSED(useFFT)) { TRACE("DifferentialOperators::Delp2( Field2D )"); ASSERT1(location == outloc || outloc == CELL_DEFAULT) @@ -345,10 +341,8 @@ FieldPerp DifferentialOperators::Delp2(const FieldPerp& f, CELL_LOC outloc, bool return result; } -DifferentialOperators::FieldMetric DifferentialOperators::Laplace_par(const Field2D& f, - const Field2D& g22, - const Field2D& J, - CELL_LOC outloc) { +FieldMetric DifferentialOperators::Laplace_par(const Field2D& f, const Field2D& g22, + const Field2D& J, CELL_LOC outloc) { ASSERT1(location == outloc || outloc == CELL_DEFAULT) return D2DY2(f, outloc) / g22 + DDY(J / g22, outloc) * DDY(f, outloc) / J; @@ -362,10 +356,12 @@ Field3D DifferentialOperators::Laplace_par(const Field3D& f, const Field3D& g22, // Full Laplacian operator on scalar field -DifferentialOperators::FieldMetric DifferentialOperators::Laplace( - const Field2D& f, MetricTensor& covariantMetricTensor, const Field2D& G1, - const Field2D& G2, const Field2D& G3, CELL_LOC outloc, - const std::string& dfdy_boundary_conditions, const std::string& dfdy_dy_region) { +FieldMetric DifferentialOperators::Laplace(const Field2D& f, + MetricTensor& covariantMetricTensor, + const Field2D& G1, const Field2D& G2, + const Field2D& G3, CELL_LOC outloc, + const std::string& dfdy_boundary_conditions, + const std::string& dfdy_dy_region) { TRACE("DifferentialOperators::Laplace( Field2D )"); ASSERT1(location == outloc || outloc == CELL_DEFAULT) @@ -467,8 +463,7 @@ Field2D DifferentialOperators::Laplace_perpXY([[maybe_unused]] const Field2D& A, #endif } -DifferentialOperators::FieldMetric& -DifferentialOperators::invSg(MetricTensor& covariantMetricTensor) const { +FieldMetric& DifferentialOperators::invSg(MetricTensor& covariantMetricTensor) const { if (invSgCache == nullptr) { auto ptr = std::make_unique(); (*ptr) = 1.0 / sqrt(covariantMetricTensor.Getg22()); @@ -477,7 +472,7 @@ DifferentialOperators::invSg(MetricTensor& covariantMetricTensor) const { return *invSgCache; } -const DifferentialOperators::FieldMetric& +const FieldMetric& DifferentialOperators::Grad2_par2_DDY_invSg(MetricTensor& covariantMetricTensor, CELL_LOC outloc, const std::string& method) const { diff --git a/src/mesh/geometry.cxx b/src/mesh/geometry.cxx index 654786e999..7b3092a068 100644 --- a/src/mesh/geometry.cxx +++ b/src/mesh/geometry.cxx @@ -5,9 +5,8 @@ **************************************************************************/ #include "bout/geometry.hxx" -#include "bout/field2d.hxx" -#include "bout/utils.hxx" -#include +//#include "bout/field2d.hxx" +//#include //// use anonymous namespace so this utility function is not available outside this file //namespace { From 986a225a8cafba4ea80098506238cf68f403ea53 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Sun, 26 Nov 2023 16:46:36 +0000 Subject: [PATCH 190/491] Specify public methods of DifferentialOperators. --- include/bout/differential_operators.hxx | 1 + 1 file changed, 1 insertion(+) diff --git a/include/bout/differential_operators.hxx b/include/bout/differential_operators.hxx index e26ede3b1c..d00b6b7987 100644 --- a/include/bout/differential_operators.hxx +++ b/include/bout/differential_operators.hxx @@ -9,6 +9,7 @@ class DifferentialOperators { using FieldMetric = MetricTensor::FieldMetric; +public: DifferentialOperators(Mesh* mesh, FieldMetric& intShiftTorsion, const CELL_LOC& location, FieldMetric& dx, FieldMetric& dy, FieldMetric& dz); From 364cd21d202513ffd2b6fbaa3f98558412b57786 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Sun, 26 Nov 2023 16:50:42 +0000 Subject: [PATCH 191/491] Sort out more #include statements. "using FieldMetric = MetricTensor::FieldMetric" instead of duplicated 'using' statements. --- src/mesh/coordinates.cxx | 34 +++++++++++++++++----------------- src/mesh/geometry.cxx | 4 ++-- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 76e20576f2..efc62b9551 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -1646,23 +1646,23 @@ void Coordinates::setCovariantMetricTensor(MetricTensor metric_tensor, geometry.setCovariantMetricTensor(metric_tensor, location, region); } -const MetricTensor::FieldMetric& Coordinates::g_11() const { return geometry.g_11(); } -const MetricTensor::FieldMetric& Coordinates::g_22() const { return geometry.g_22(); } -const MetricTensor::FieldMetric& Coordinates::g_33() const { return geometry.g_33(); } -const MetricTensor::FieldMetric& Coordinates::g_12() const { return geometry.g_12(); } -const MetricTensor::FieldMetric& Coordinates::g_13() const { return geometry.g_13(); } -const MetricTensor::FieldMetric& Coordinates::g_23() const { return geometry.g_23(); } - -const MetricTensor::FieldMetric& Coordinates::g11() const { return geometry.g11(); } -const MetricTensor::FieldMetric& Coordinates::g22() const { return geometry.g22(); } -const MetricTensor::FieldMetric& Coordinates::g33() const { return geometry.g33(); } -const MetricTensor::FieldMetric& Coordinates::g12() const { return geometry.g12(); } -const MetricTensor::FieldMetric& Coordinates::g13() const { return geometry.g13(); } -const MetricTensor::FieldMetric& Coordinates::g23() const { return geometry.g23(); } - -MetricTensor::FieldMetric& Coordinates::J() const { return geometry.J(); } - -const MetricTensor::FieldMetric& Coordinates::Bxy() const { return geometry.Bxy(); } +const FieldMetric& Coordinates::g_11() const { return geometry.g_11(); } +const FieldMetric& Coordinates::g_22() const { return geometry.g_22(); } +const FieldMetric& Coordinates::g_33() const { return geometry.g_33(); } +const FieldMetric& Coordinates::g_12() const { return geometry.g_12(); } +const FieldMetric& Coordinates::g_13() const { return geometry.g_13(); } +const FieldMetric& Coordinates::g_23() const { return geometry.g_23(); } + +const FieldMetric& Coordinates::g11() const { return geometry.g11(); } +const FieldMetric& Coordinates::g22() const { return geometry.g22(); } +const FieldMetric& Coordinates::g33() const { return geometry.g33(); } +const FieldMetric& Coordinates::g12() const { return geometry.g12(); } +const FieldMetric& Coordinates::g13() const { return geometry.g13(); } +const FieldMetric& Coordinates::g23() const { return geometry.g23(); } + +FieldMetric& Coordinates::J() const { return geometry.J(); } + +const FieldMetric& Coordinates::Bxy() const { return geometry.Bxy(); } void Coordinates::setJ(FieldMetric J) { //TODO: Calculate J and check value is close diff --git a/src/mesh/geometry.cxx b/src/mesh/geometry.cxx index 7b3092a068..c89d359525 100644 --- a/src/mesh/geometry.cxx +++ b/src/mesh/geometry.cxx @@ -1556,9 +1556,9 @@ const MetricTensor::FieldMetric& Geometry::g23() const { return contravariantMetricTensor.Getg23(); } -Geometry::FieldMetric Geometry::J() const { return this_J; } +FieldMetric Geometry::J() const { return this_J; } -const Geometry::FieldMetric& Geometry::Bxy() const { return this_Bxy; } +const FieldMetric& Geometry::Bxy() const { return this_Bxy; } void Geometry::setJ(FieldMetric J) { //TODO: Calculate J and check value is close From 18641e6b7b4012de4c10fc8a63ec4296a9250a55 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Sun, 26 Nov 2023 21:28:24 +0000 Subject: [PATCH 192/491] Make copy of value of J to pass to communicate(). --- include/bout/coordinates.hxx | 2 +- include/bout/geometry.hxx | 2 +- src/mesh/coordinates.cxx | 5 +++-- src/mesh/geometry.cxx | 2 +- 4 files changed, 6 insertions(+), 5 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index 6913bb7aec..3afd2e03a2 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -103,7 +103,7 @@ public: const FieldMetric& g23() const; ///< Coordinate system Jacobian, so volume of cell is J*dx*dy*dz - FieldMetric& J() const; + const FieldMetric& J() const; ///< Magnitude of B = nabla z times nabla x const FieldMetric& Bxy() const; diff --git a/include/bout/geometry.hxx b/include/bout/geometry.hxx index 7e29dc5a76..52fdb956ad 100644 --- a/include/bout/geometry.hxx +++ b/include/bout/geometry.hxx @@ -83,7 +83,7 @@ public: // MetricTensor& getCovariantMetricTensor(); ///< Coordinate system Jacobian, so volume of cell is J*dx*dy*dz - FieldMetric J() const; + const FieldMetric& J() const; ///< Magnitude of B = nabla z times nabla x const FieldMetric& Bxy() const; diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index efc62b9551..0a8b9b2353 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -548,7 +548,8 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, // Compare calculated and loaded values output_warn.write("\tMaximum difference in J is {:e}\n", max(abs(J() - Jcalc))); - communicate(J()); + auto J_value = J(); // TODO: There may be a better way + communicate(J_value); // Re-evaluate Bxy using new J setBxy(sqrt(g_22()) / J()); @@ -1660,7 +1661,7 @@ const FieldMetric& Coordinates::g12() const { return geometry.g12(); } const FieldMetric& Coordinates::g13() const { return geometry.g13(); } const FieldMetric& Coordinates::g23() const { return geometry.g23(); } -FieldMetric& Coordinates::J() const { return geometry.J(); } +const FieldMetric& Coordinates::J() const { return geometry.J(); } const FieldMetric& Coordinates::Bxy() const { return geometry.Bxy(); } diff --git a/src/mesh/geometry.cxx b/src/mesh/geometry.cxx index c89d359525..0765b67ea5 100644 --- a/src/mesh/geometry.cxx +++ b/src/mesh/geometry.cxx @@ -1556,7 +1556,7 @@ const MetricTensor::FieldMetric& Geometry::g23() const { return contravariantMetricTensor.Getg23(); } -FieldMetric Geometry::J() const { return this_J; } +const FieldMetric& Geometry::J() const { return this_J; } const FieldMetric& Geometry::Bxy() const { return this_Bxy; } From 2574db394d67afd5d0916bbce29d7c4cd59179b9 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Sun, 26 Nov 2023 22:02:29 +0000 Subject: [PATCH 193/491] Fix Grad_par, Vpar_Grad_par, invSg, and Grad2_par2_DDY_invSg methods. --- include/bout/coordinates.hxx | 34 +++++----- include/bout/differential_operators.hxx | 18 +++--- include/bout/geometry.hxx | 2 +- src/mesh/coordinates.cxx | 84 +++++++++++++------------ src/mesh/differential_operators.cxx | 30 ++++----- src/mesh/geometry.cxx | 6 +- 6 files changed, 88 insertions(+), 86 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index 3afd2e03a2..dd1cca401b 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -132,6 +132,7 @@ public: FieldMetric IntShiftTorsion; ///< Integrated shear (I in BOUT notation) const MetricTensor& getContravariantMetricTensor() const; + // const MetricTensor& getCovariantMetricTensor() const; /// Calculate differential geometry quantities from the metric tensor int calculateGeometry(bool recalculate_staggered = true, @@ -186,23 +187,22 @@ public: // Field3D DDZ(const Field3D& f, CELL_LOC outloc = CELL_DEFAULT, // const std::string& method = "DEFAULT", // const std::string& region = "RGN_NOBNDRY"); - // - // /// Gradient along magnetic field b.Grad(f) - // FieldMetric Grad_par(const Field2D& var, CELL_LOC outloc = CELL_DEFAULT, - // const std::string& method = "DEFAULT"); - // - // Field3D Grad_par(const Field3D& var, CELL_LOC outloc = CELL_DEFAULT, - // const std::string& method = "DEFAULT"); - // - // /// Advection along magnetic field V*b.Grad(f) - // FieldMetric Vpar_Grad_par(const Field2D& v, const Field2D& f, - // CELL_LOC outloc = CELL_DEFAULT, - // const std::string& method = "DEFAULT"); - // - // Field3D Vpar_Grad_par(const Field3D& v, const Field3D& f, - // CELL_LOC outloc = CELL_DEFAULT, - // const std::string& method = "DEFAULT"); - // + + /// Gradient along magnetic field b.Grad(f) + Field2D Grad_par(const Field2D& var); + + Field3D Grad_par(const Field3D& var, CELL_LOC outloc = CELL_DEFAULT, + const std::string& method = "DEFAULT"); + + /// Advection along magnetic field V*b.Grad(f) + FieldMetric Vpar_Grad_par(const Field2D& v, const Field2D& f, + CELL_LOC outloc = CELL_DEFAULT, + const std::string& method = "DEFAULT"); + + Field3D Vpar_Grad_par(const Field3D& v, const Field3D& f, + CELL_LOC outloc = CELL_DEFAULT, + const std::string& method = "DEFAULT"); + // /// Divergence along magnetic field Div(b*f) = B.Grad(f/B) // FieldMetric Div_par(const Field2D& f, CELL_LOC outloc = CELL_DEFAULT, // const std::string& method = "DEFAULT"); diff --git a/include/bout/differential_operators.hxx b/include/bout/differential_operators.hxx index d00b6b7987..3056dd8318 100644 --- a/include/bout/differential_operators.hxx +++ b/include/bout/differential_operators.hxx @@ -42,21 +42,21 @@ public: const std::string& region = "RGN_NOBNDRY"); /// Gradient along magnetic field b.Grad(f) - FieldMetric Grad_par(const Field2D& var, MetricTensor& covariantMetricTensor, + FieldMetric Grad_par(const Field2D& var, const MetricTensor& covariantMetricTensor, CELL_LOC outloc = CELL_DEFAULT, const std::string& method = "DEFAULT"); - Field3D Grad_par(const Field3D& var, MetricTensor& covariantMetricTensor, + Field3D Grad_par(const Field3D& var, const MetricTensor& covariantMetricTensor, CELL_LOC outloc = CELL_DEFAULT, const std::string& method = "DEFAULT"); /// Advection along magnetic field V*b.Grad(f) - FieldMetric Vpar_Grad_par(const Field2D& v, const Field2D& f, - MetricTensor& covariantMetricTensor, - CELL_LOC outloc = CELL_DEFAULT, - const std::string& method = "DEFAULT"); + Field2D Vpar_Grad_par(const Field2D& v, const Field2D& f, + const MetricTensor& covariantMetricTensor, + CELL_LOC outloc = CELL_DEFAULT, + const std::string& method = "DEFAULT"); Field3D Vpar_Grad_par(const Field3D& v, const Field3D& f, - MetricTensor& covariantMetricTensor, + const MetricTensor& covariantMetricTensor, CELL_LOC outloc = CELL_DEFAULT, const std::string& method = "DEFAULT"); @@ -128,9 +128,9 @@ private: mutable std::map> Grad2_par2_DDY_invSgCache; mutable std::unique_ptr invSgCache{nullptr}; - FieldMetric& invSg(MetricTensor& covariantMetricTensor) const; + FieldMetric& invSg(const MetricTensor& covariantMetricTensor) const; - const FieldMetric& Grad2_par2_DDY_invSg(MetricTensor& covariantMetricTensor, + const FieldMetric& Grad2_par2_DDY_invSg(const MetricTensor& covariantMetricTensor, CELL_LOC outloc, const std::string& method) const; }; diff --git a/include/bout/geometry.hxx b/include/bout/geometry.hxx index 52fdb956ad..7a354caf65 100644 --- a/include/bout/geometry.hxx +++ b/include/bout/geometry.hxx @@ -80,7 +80,7 @@ public: const FieldMetric& g23() const; const MetricTensor& getContravariantMetricTensor() const; - // MetricTensor& getCovariantMetricTensor(); + const MetricTensor& getCovariantMetricTensor() const; ///< Coordinate system Jacobian, so volume of cell is J*dx*dy*dz const FieldMetric& J() const; diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 0a8b9b2353..cc2f0e56e2 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -1239,46 +1239,46 @@ void Coordinates::setParallelTransform(Options* options) { // const std::string& region) { // return bout::derivatives::index::DDZ(f, outloc, method, region) / dz; //} -// -/////////////////////////////////////////////////////////// -//// Parallel gradient -// -//Coordinates::FieldMetric Coordinates::Grad_par(const Field2D& var, -// MAYBE_UNUSED(CELL_LOC outloc), -// const std::string& UNUSED(method)) { -// TRACE("Coordinates::Grad_par( Field2D )"); -// ASSERT1(location == outloc || (outloc == CELL_DEFAULT && location == var.getLocation())) -// -// return DDY(var) * invSg(); -//} -// -//Field3D Coordinates::Grad_par(const Field3D& var, CELL_LOC outloc, -// const std::string& method) { -// TRACE("Coordinates::Grad_par( Field3D )"); -// ASSERT1(location == outloc || outloc == CELL_DEFAULT) -// -// return ::DDY(var, outloc, method) * invSg(); -//} -// -/////////////////////////////////////////////////////////// -//// Vpar_Grad_par -//// vparallel times the parallel derivative along unperturbed B-field -// -//Coordinates::FieldMetric Coordinates::Vpar_Grad_par(const Field2D& v, const Field2D& f, -// MAYBE_UNUSED(CELL_LOC outloc), -// const std::string& UNUSED(method)) { -// ASSERT1(location == outloc || (outloc == CELL_DEFAULT && location == f.getLocation())) -// -// return VDDY(v, f) * invSg(); -//} -// -//Field3D Coordinates::Vpar_Grad_par(const Field3D& v, const Field3D& f, CELL_LOC outloc, -// const std::string& method) { -// ASSERT1(location == outloc || outloc == CELL_DEFAULT) -// -// return VDDY(v, f, outloc, method) * invSg(); -//} -// + +///////////////////////////////////////////////////////// +// Parallel gradient + +Field2D Coordinates::Grad_par(const Field2D& var) { + TRACE("Coordinates::Grad_par( Field2D )"); + + return differential_operators.Grad_par(var, geometry.getCovariantMetricTensor()); +} + +Field3D Coordinates::Grad_par(const Field3D& var, CELL_LOC outloc, + const std::string& method) { + TRACE("Coordinates::Grad_par( Field3D )"); + ASSERT1(location == outloc || outloc == CELL_DEFAULT) + + return differential_operators.Grad_par(var, geometry.getCovariantMetricTensor(), outloc, + method); +} + +///////////////////////////////////////////////////////// +// Vpar_Grad_par +// vparallel times the parallel derivative along unperturbed B-field + +Field2D Coordinates::Vpar_Grad_par(const Field2D& v, const Field2D& f, + MAYBE_UNUSED(CELL_LOC outloc), + const std::string& UNUSED(method)) { + ASSERT1(location == outloc || (outloc == CELL_DEFAULT && location == f.getLocation())) + + return differential_operators.Vpar_Grad_par(v, f, geometry.getCovariantMetricTensor(), + outloc); +} + +Field3D Coordinates::Vpar_Grad_par(const Field3D& v, const Field3D& f, CELL_LOC outloc, + const std::string& method) { + ASSERT1(location == outloc || outloc == CELL_DEFAULT) + + return differential_operators.Vpar_Grad_par(v, f, geometry.getCovariantMetricTensor(), + outloc, method); +} + /////////////////////////////////////////////////////////// //// Parallel divergence // @@ -1684,4 +1684,8 @@ const MetricTensor& Coordinates::getContravariantMetricTensor() const { return geometry.getContravariantMetricTensor(); } +//const MetricTensor& Coordinates::getCovariantMetricTensor() const { +// return geometry.getCovariantMetricTensor(); +//} + //const Coordinates::FieldMetric& Coordinates::invSg() const {} diff --git a/src/mesh/differential_operators.cxx b/src/mesh/differential_operators.cxx index 7c1e191aaf..55e86db0fa 100644 --- a/src/mesh/differential_operators.cxx +++ b/src/mesh/differential_operators.cxx @@ -2,11 +2,11 @@ #include "bout/differential_operators.hxx" //#include "bout/field2d.hxx" //#include "bout/field3d.hxx" -//#include "bout/mesh.hxx" +#include "bout/mesh.hxx" //#include "bout/metricTensor.hxx" //#include "bout/index_derivs_interface.hxx" //#include "bout/paralleltransform.hxx" -//#include +#include DifferentialOperators::DifferentialOperators(Mesh* mesh, FieldMetric& intShiftTorsion, const CELL_LOC& location, FieldMetric& dx, @@ -74,41 +74,36 @@ Field3D DifferentialOperators::DDZ(const Field3D& f, CELL_LOC outloc, // Parallel gradient FieldMetric DifferentialOperators::Grad_par(const Field2D& var, - MetricTensor& covariantMetricTensor, + const MetricTensor& covariantMetricTensor, [[maybe_unused]] CELL_LOC outloc, const std::string& UNUSED(method)) { TRACE("DifferentialOperators::Grad_par( Field2D )"); - ASSERT1(location == outloc || (outloc == CELL_DEFAULT && location == var.getLocation())) return DDY(var) * invSg(covariantMetricTensor); } Field3D DifferentialOperators::Grad_par(const Field3D& var, - MetricTensor& covariantMetricTensor, + const MetricTensor& covariantMetricTensor, CELL_LOC outloc, const std::string& method) { TRACE("DifferentialOperators::Grad_par( Field3D )"); - ASSERT1(location == outloc || outloc == CELL_DEFAULT) - return ::DDY(var, outloc, method) * invSg(covariantMetricTensor); + return DDY(var, outloc, method) * invSg(covariantMetricTensor); } ///////////////////////////////////////////////////////// // Vpar_Grad_par // vparallel times the parallel derivative along unperturbed B-field -FieldMetric DifferentialOperators::Vpar_Grad_par(const Field2D& v, const Field2D& f, - MetricTensor& covariantMetricTensor, - [[maybe_unused]] CELL_LOC outloc, - const std::string& UNUSED(method)) { - ASSERT1(location == outloc || (outloc == CELL_DEFAULT && location == f.getLocation())) - +Field2D DifferentialOperators::Vpar_Grad_par(const Field2D& v, const Field2D& f, + const MetricTensor& covariantMetricTensor, + [[maybe_unused]] CELL_LOC outloc, + const std::string& UNUSED(method)) { return VDDY(v, f) * invSg(covariantMetricTensor); } Field3D DifferentialOperators::Vpar_Grad_par(const Field3D& v, const Field3D& f, - MetricTensor& covariantMetricTensor, + const MetricTensor& covariantMetricTensor, CELL_LOC outloc, const std::string& method) { - ASSERT1(location == outloc || outloc == CELL_DEFAULT) return VDDY(v, f, outloc, method) * invSg(covariantMetricTensor); } @@ -463,7 +458,8 @@ Field2D DifferentialOperators::Laplace_perpXY([[maybe_unused]] const Field2D& A, #endif } -FieldMetric& DifferentialOperators::invSg(MetricTensor& covariantMetricTensor) const { +FieldMetric& +DifferentialOperators::invSg(const MetricTensor& covariantMetricTensor) const { if (invSgCache == nullptr) { auto ptr = std::make_unique(); (*ptr) = 1.0 / sqrt(covariantMetricTensor.Getg22()); @@ -473,7 +469,7 @@ FieldMetric& DifferentialOperators::invSg(MetricTensor& covariantMetricTensor) c } const FieldMetric& -DifferentialOperators::Grad2_par2_DDY_invSg(MetricTensor& covariantMetricTensor, +DifferentialOperators::Grad2_par2_DDY_invSg(const MetricTensor& covariantMetricTensor, CELL_LOC outloc, const std::string& method) const { if (auto search = Grad2_par2_DDY_invSgCache.find(method); diff --git a/src/mesh/geometry.cxx b/src/mesh/geometry.cxx index 0765b67ea5..c181b24836 100644 --- a/src/mesh/geometry.cxx +++ b/src/mesh/geometry.cxx @@ -1579,6 +1579,10 @@ const MetricTensor& Geometry::getContravariantMetricTensor() const { return contravariantMetricTensor; } +const MetricTensor& Geometry::getCovariantMetricTensor() const { + return covariantMetricTensor; +} + void Geometry::applyToContravariantMetricTensor( std::function function) { contravariantMetricTensor.map(function); @@ -1588,5 +1592,3 @@ void Geometry::applyToCovariantMetricTensor( std::function function) { covariantMetricTensor.map(function); } - -//MetricTensor& Geometry::getCovariantMetricTensor() { return covariantMetricTensor; } From 451c70da414ebc31913079d587b05b39e5ac6afb Mon Sep 17 00:00:00 2001 From: tomc271 Date: Sun, 26 Nov 2023 22:20:20 +0000 Subject: [PATCH 194/491] Move parallel divergence methods back onto Coordinates class. --- include/bout/coordinates.hxx | 72 +-- include/bout/differential_operators.hxx | 6 +- src/mesh/coordinates.cxx | 698 ++++++++++++------------ src/mesh/differential_operators.cxx | 149 ++--- 4 files changed, 463 insertions(+), 462 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index dd1cca401b..17210c4a14 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -203,42 +203,42 @@ public: CELL_LOC outloc = CELL_DEFAULT, const std::string& method = "DEFAULT"); - // /// Divergence along magnetic field Div(b*f) = B.Grad(f/B) - // FieldMetric Div_par(const Field2D& f, CELL_LOC outloc = CELL_DEFAULT, - // const std::string& method = "DEFAULT"); - // - // Field3D Div_par(const Field3D& f, CELL_LOC outloc = CELL_DEFAULT, - // const std::string& method = "DEFAULT"); - // - // // Second derivative along magnetic field - // FieldMetric Grad2_par2(const Field2D& f, CELL_LOC outloc = CELL_DEFAULT, - // const std::string& method = "DEFAULT"); - // - // Field3D Grad2_par2(const Field3D& f, CELL_LOC outloc = CELL_DEFAULT, - // const std::string& method = "DEFAULT"); - // // Perpendicular Laplacian operator, using only X-Z derivatives - // // NOTE: This might be better bundled with the Laplacian inversion code - // // since it makes use of the same coefficients and FFT routines - // FieldMetric Delp2(const Field2D& f, CELL_LOC outloc = CELL_DEFAULT, bool useFFT = true); - // Field3D Delp2(const Field3D& f, CELL_LOC outloc = CELL_DEFAULT, bool useFFT = true); - // FieldPerp Delp2(const FieldPerp& f, CELL_LOC outloc = CELL_DEFAULT, bool useFFT = true); - // - // // Full parallel Laplacian operator on scalar field - // // Laplace_par(f) = Div( b (b dot Grad(f)) ) - // FieldMetric Laplace_par(const Field2D& f, CELL_LOC outloc = CELL_DEFAULT); - // Field3D Laplace_par(const Field3D& f, CELL_LOC outloc = CELL_DEFAULT); - // - // // Full Laplacian operator on scalar field - // FieldMetric Laplace(const Field2D& f, CELL_LOC outloc = CELL_DEFAULT, - // const std::string& dfdy_boundary_conditions = "free_o3", - // const std::string& dfdy_dy_region = ""); - // Field3D Laplace(const Field3D& f, CELL_LOC outloc = CELL_DEFAULT, - // const std::string& dfdy_boundary_conditions = "free_o3", - // const std::string& dfdy_dy_region = ""); - // - // // Full perpendicular Laplacian, in form of inverse of Laplacian operator in LaplaceXY - // // solver - // Field2D Laplace_perpXY(const Field2D& A, const Field2D& f); + /// Divergence along magnetic field Div(b*f) = B.Grad(f/B) + FieldMetric Div_par(const Field2D& f, CELL_LOC outloc = CELL_DEFAULT, + const std::string& method = "DEFAULT"); + + Field3D Div_par(const Field3D& f, CELL_LOC outloc = CELL_DEFAULT, + const std::string& method = "DEFAULT"); + + // Second derivative along magnetic field + FieldMetric Grad2_par2(const Field2D& f, CELL_LOC outloc = CELL_DEFAULT, + const std::string& method = "DEFAULT"); + + Field3D Grad2_par2(const Field3D& f, CELL_LOC outloc = CELL_DEFAULT, + const std::string& method = "DEFAULT"); + // Perpendicular Laplacian operator, using only X-Z derivatives + // NOTE: This might be better bundled with the Laplacian inversion code + // since it makes use of the same coefficients and FFT routines + FieldMetric Delp2(const Field2D& f, CELL_LOC outloc = CELL_DEFAULT, bool useFFT = true); + Field3D Delp2(const Field3D& f, CELL_LOC outloc = CELL_DEFAULT, bool useFFT = true); + FieldPerp Delp2(const FieldPerp& f, CELL_LOC outloc = CELL_DEFAULT, bool useFFT = true); + + // Full parallel Laplacian operator on scalar field + // Laplace_par(f) = Div( b (b dot Grad(f)) ) + FieldMetric Laplace_par(const Field2D& f, CELL_LOC outloc = CELL_DEFAULT); + Field3D Laplace_par(const Field3D& f, CELL_LOC outloc = CELL_DEFAULT); + + // Full Laplacian operator on scalar field + FieldMetric Laplace(const Field2D& f, CELL_LOC outloc = CELL_DEFAULT, + const std::string& dfdy_boundary_conditions = "free_o3", + const std::string& dfdy_dy_region = ""); + Field3D Laplace(const Field3D& f, CELL_LOC outloc = CELL_DEFAULT, + const std::string& dfdy_boundary_conditions = "free_o3", + const std::string& dfdy_dy_region = ""); + + // Full perpendicular Laplacian, in form of inverse of Laplacian operator in LaplaceXY + // solver + Field2D Laplace_perpXY(const Field2D& A, const Field2D& f); private: int nz; // Size of mesh in Z. This is mesh->ngz-1 diff --git a/include/bout/differential_operators.hxx b/include/bout/differential_operators.hxx index 3056dd8318..7f17b9f9b7 100644 --- a/include/bout/differential_operators.hxx +++ b/include/bout/differential_operators.hxx @@ -62,11 +62,13 @@ public: /// Divergence along magnetic field Div(b*f) = B.Grad(f/B) FieldMetric Div_par(const Field2D& f, const Field2D& Bxy, + const MetricTensor& covariantMetricTensor, CELL_LOC outloc = CELL_DEFAULT, const std::string& method = "DEFAULT"); - Field3D Div_par(const Field3D& f, const Field2D& Bxy, CELL_LOC outloc = CELL_DEFAULT, - const std::string& method = "DEFAULT"); + Field3D Div_par(const Field3D& f, const Field2D& Bxy, + const MetricTensor& covariantMetricTensor, + CELL_LOC outloc = CELL_DEFAULT, const std::string& method = "DEFAULT"); // Second derivative along magnetic field FieldMetric Grad2_par2(const Field2D& f, MetricTensor& covariantMetricTensor, diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index cc2f0e56e2..3aa3d32c58 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -1279,359 +1279,357 @@ Field3D Coordinates::Vpar_Grad_par(const Field3D& v, const Field3D& f, CELL_LOC outloc, method); } -/////////////////////////////////////////////////////////// -//// Parallel divergence -// -//Coordinates::FieldMetric Coordinates::Div_par(const Field2D& f, CELL_LOC outloc, -// const std::string& method) { -// TRACE("Coordinates::Div_par( Field2D )"); -// ASSERT1(location == outloc || outloc == CELL_DEFAULT) -// -// // Need Bxy at location of f, which might be different from location of this -// // Coordinates object -// auto Bxy_floc = f.getCoordinates()->Bxy(); -// -// return Bxy() * Grad_par(f / Bxy_floc, outloc, method); -//} -// -//Field3D Coordinates::Div_par(const Field3D& f, CELL_LOC outloc, -// const std::string& method) { -// TRACE("Coordinates::Div_par( Field3D )"); -// ASSERT1(location == outloc || outloc == CELL_DEFAULT) -// -// // Need Bxy at location of f, which might be different from location of this -// // Coordinates object -// auto Bxy_floc = f.getCoordinates()->Bxy(); -// -// if (!f.hasParallelSlices()) { -// // No yup/ydown fields. The Grad_par operator will -// // shift to field aligned coordinates -// return Bxy() * Grad_par(f / Bxy_floc, outloc, method); -// } -// -// // Need to modify yup and ydown fields -// Field3D f_B = f / Bxy_floc; -// f_B.splitParallelSlices(); -// for (int i = 0; i < f.getMesh()->ystart; ++i) { -// f_B.yup(i) = f.yup(i) / Bxy_floc.yup(i); -// f_B.ydown(i) = f.ydown(i) / Bxy_floc.ydown(i); -// } -// return Bxy() * Grad_par(f_B, outloc, method); -//} -// -/////////////////////////////////////////////////////////// -//// second parallel derivative (b dot Grad)(b dot Grad) -//// Note: For parallel Laplacian use Laplace_par -// -//Coordinates::FieldMetric Coordinates::Grad2_par2(const Field2D& f, CELL_LOC outloc, -// const std::string& method) { -// TRACE("Coordinates::Grad2_par2( Field2D )"); -// ASSERT1(location == outloc || (outloc == CELL_DEFAULT && location == f.getLocation())) -// -// auto result = Grad2_par2_DDY_invSg(outloc, method) * DDY(f, outloc, method) -// + D2DY2(f, outloc, method) / g22(); -// -// return result; -//} -// -//Field3D Coordinates::Grad2_par2(const Field3D& f, CELL_LOC outloc, -// const std::string& method) { -// TRACE("Coordinates::Grad2_par2( Field3D )"); -// if (outloc == CELL_DEFAULT) { -// outloc = f.getLocation(); -// } -// ASSERT1(location == outloc) -// -// Field3D result = ::DDY(f, outloc, method); -// -// Field3D r2 = D2DY2(f, outloc, method) / g22(); -// -// result = Grad2_par2_DDY_invSg(outloc, method) * result + r2; -// -// ASSERT2(result.getLocation() == outloc) -// -// return result; -//} -// -/////////////////////////////////////////////////////////// -//// perpendicular Laplacian operator -// -//#include // Delp2 uses same coefficients as inversion code -// -//Coordinates::FieldMetric Coordinates::Delp2(const Field2D& f, CELL_LOC outloc, -// bool UNUSED(useFFT)) { -// TRACE("Coordinates::Delp2( Field2D )"); -// ASSERT1(location == outloc || outloc == CELL_DEFAULT) -// -// auto result = G1 * DDX(f, outloc) + g11() * D2DX2(f, outloc); -// -// return result; -//} -// -//Field3D Coordinates::Delp2(const Field3D& f, CELL_LOC outloc, bool useFFT) { -// TRACE("Coordinates::Delp2( Field3D )"); -// -// if (outloc == CELL_DEFAULT) { -// outloc = f.getLocation(); -// } -// -// ASSERT1(location == outloc) -// ASSERT1(f.getLocation() == outloc) -// -// if (localmesh->GlobalNx == 1 && localmesh->GlobalNz == 1) { -// // copy mesh, location, etc -// return f * 0; -// } -// ASSERT2(localmesh->xstart > 0) // Need at least one guard cell -// -// Field3D result{emptyFrom(f).setLocation(outloc)}; -// -// if (useFFT and not bout::build::use_metric_3d) { -// int ncz = localmesh->LocalNz; -// -// // Allocate memory -// auto ft = Matrix(localmesh->LocalNx, ncz / 2 + 1); -// auto delft = Matrix(localmesh->LocalNx, ncz / 2 + 1); -// -// // Loop over y indices -// // Note: should not include y-guard or y-boundary points here as that would -// // use values from corner cells in dx, which may not be initialised. -// for (int jy = localmesh->ystart; jy <= localmesh->yend; jy++) { -// -// // Take forward FFT -// -// for (int jx = 0; jx < localmesh->LocalNx; jx++) { -// rfft(&f(jx, jy, 0), ncz, &ft(jx, 0)); -// } -// -// // Loop over kz -// for (int jz = 0; jz <= ncz / 2; jz++) { -// -// // No smoothing in the x direction -// for (int jx = localmesh->xstart; jx <= localmesh->xend; jx++) { -// // Perform x derivative -// -// dcomplex a, b, c; -// laplace_tridag_coefs(jx, jy, jz, a, b, c, nullptr, nullptr, outloc); -// -// delft(jx, jz) = a * ft(jx - 1, jz) + b * ft(jx, jz) + c * ft(jx + 1, jz); -// } -// } -// -// // Reverse FFT -// for (int jx = localmesh->xstart; jx <= localmesh->xend; jx++) { -// -// irfft(&delft(jx, 0), ncz, &result(jx, jy, 0)); -// } -// } -// } else { -// result = G1 * ::DDX(f, outloc) + G3 * ::DDZ(f, outloc) + g11() * ::D2DX2(f, outloc) -// + g33() * ::D2DZ2(f, outloc) + 2 * g13() * ::D2DXDZ(f, outloc); -// } -// -// ASSERT2(result.getLocation() == outloc) -// -// return result; -//} -// -//FieldPerp Coordinates::Delp2(const FieldPerp& f, CELL_LOC outloc, bool useFFT) { -// TRACE("Coordinates::Delp2( FieldPerp )"); -// -// if (outloc == CELL_DEFAULT) { -// outloc = f.getLocation(); -// } -// -// ASSERT1(location == outloc) -// ASSERT1(f.getLocation() == outloc) -// -// if (localmesh->GlobalNx == 1 && localmesh->GlobalNz == 1) { -// // copy mesh, location, etc -// return f * 0; -// } -// ASSERT2(localmesh->xstart > 0) // Need at least one guard cell -// -// FieldPerp result{emptyFrom(f).setLocation(outloc)}; -// -// int jy = f.getIndex(); -// result.setIndex(jy); -// -// if (useFFT) { -// int ncz = localmesh->LocalNz; -// -// // Allocate memory -// auto ft = Matrix(localmesh->LocalNx, ncz / 2 + 1); -// auto delft = Matrix(localmesh->LocalNx, ncz / 2 + 1); -// -// // Take forward FFT -// for (int jx = 0; jx < localmesh->LocalNx; jx++) { -// rfft(&f(jx, 0), ncz, &ft(jx, 0)); -// } -// -// // Loop over kz -// for (int jz = 0; jz <= ncz / 2; jz++) { -// -// // No smoothing in the x direction -// for (int jx = localmesh->xstart; jx <= localmesh->xend; jx++) { -// // Perform x derivative -// -// dcomplex a, b, c; -// laplace_tridag_coefs(jx, jy, jz, a, b, c); -// -// delft(jx, jz) = a * ft(jx - 1, jz) + b * ft(jx, jz) + c * ft(jx + 1, jz); -// } -// } -// -// // Reverse FFT -// for (int jx = localmesh->xstart; jx <= localmesh->xend; jx++) { -// irfft(&delft(jx, 0), ncz, &result(jx, 0)); -// } -// -// } else { -// throw BoutException("Non-fourier Delp2 not currently implented for FieldPerp."); -// // Would be the following but don't have standard derivative operators for FieldPerps -// // yet -// // result = G1 * ::DDX(f, outloc) + G3 * ::DDZ(f, outloc) + g11 * ::D2DX2(f, outloc) -// // + g33 * ::D2DZ2(f, outloc) + 2 * g13 * ::D2DXDZ(f, outloc); -// } -// -// return result; -//} +///////////////////////////////////////////////////////// +// Parallel divergence -//Coordinates::FieldMetric Coordinates::Laplace_par(const Field2D& f, CELL_LOC outloc) { -// ASSERT1(location == outloc || outloc == CELL_DEFAULT) -// -// return D2DY2(f, outloc) / g22() + DDY(J() / g22(), outloc) * DDY(f, outloc) / J(); -//} +Coordinates::FieldMetric Coordinates::Div_par(const Field2D& f, CELL_LOC outloc, + const std::string& method) { + TRACE("Coordinates::Div_par( Field2D )"); + ASSERT1(location == outloc || outloc == CELL_DEFAULT) -//Field3D Coordinates::Laplace_par(const Field3D& f, CELL_LOC outloc) { -// ASSERT1(location == outloc || outloc == CELL_DEFAULT) -// return D2DY2(f, outloc) / g22() + DDY(J() / g22(), outloc) * ::DDY(f, outloc) / J(); -//} + return differential_operators.Div_par( + f, geometry.Bxy(), geometry.getCovariantMetricTensor(), outloc, method); +} -//// Full Laplacian operator on scalar field -// -//Coordinates::FieldMetric Coordinates::Laplace(const Field2D& f, CELL_LOC outloc, -// const std::string& dfdy_boundary_conditions, -// const std::string& dfdy_dy_region) { -// TRACE("Coordinates::Laplace( Field2D )"); -// ASSERT1(location == outloc || outloc == CELL_DEFAULT) -// -// auto result = G1 * DDX(f, outloc) + G2 * DDY(f, outloc) + g11() * D2DX2(f, outloc) -// + g22() * D2DY2(f, outloc) -// + 2.0 * g12() -// * D2DXDY(f, outloc, "DEFAULT", "RGN_NOBNDRY", -// dfdy_boundary_conditions, dfdy_dy_region); -// -// return result; -//} -// -//Field3D Coordinates::Laplace(const Field3D& f, CELL_LOC outloc, -// const std::string& dfdy_boundary_conditions, -// const std::string& dfdy_dy_region) { -// TRACE("Coordinates::Laplace( Field3D )"); -// ASSERT1(location == outloc || outloc == CELL_DEFAULT) -// -// Field3D result = G1 * ::DDX(f, outloc) + G2 * ::DDY(f, outloc) + G3 * ::DDZ(f, outloc) -// + g11() * D2DX2(f, outloc) + g22() * D2DY2(f, outloc) -// + g33() * D2DZ2(f, outloc) -// + 2.0 -// * (g12() -// * D2DXDY(f, outloc, "DEFAULT", "RGN_NOBNDRY", -// dfdy_boundary_conditions, dfdy_dy_region) -// + g13() * D2DXDZ(f, outloc) + g23() * D2DYDZ(f, outloc)); -// -// return result; -//} -// -//// Full perpendicular Laplacian, in form of inverse of Laplacian operator in LaplaceXY -//// solver -//Field2D Coordinates::Laplace_perpXY(MAYBE_UNUSED(const Field2D& A), -// MAYBE_UNUSED(const Field2D& f)) { -// TRACE("Coordinates::Laplace_perpXY( Field2D )"); -//#if not(BOUT_USE_METRIC_3D) -// Field2D result; -// result.allocate(); -// for (auto i : result.getRegion(RGN_NOBNDRY)) { -// result[i] = 0.; -// -// // outer x boundary -// const auto outer_x_avg = [&i](const auto& f) { return 0.5 * (f[i] + f[i.xp()]); }; -// const BoutReal outer_x_A = outer_x_avg(A); -// const BoutReal outer_x_J = outer_x_avg(J()); -// const BoutReal outer_x_g11 = outer_x_avg(g11()); -// const BoutReal outer_x_dx = outer_x_avg(dx); -// const BoutReal outer_x_value = -// outer_x_A * outer_x_J * outer_x_g11 / (J()[i] * outer_x_dx * dx[i]); -// result[i] += outer_x_value * (f[i.xp()] - f[i]); -// -// // inner x boundary -// const auto inner_x_avg = [&i](const auto& f) { return 0.5 * (f[i] + f[i.xm()]); }; -// const BoutReal inner_x_A = inner_x_avg(A); -// const BoutReal inner_x_J = inner_x_avg(J()); -// const BoutReal inner_x_g11 = inner_x_avg(g11()); -// const BoutReal inner_x_dx = inner_x_avg(dx); -// const BoutReal inner_x_value = -// inner_x_A * inner_x_J * inner_x_g11 / (J()[i] * inner_x_dx * dx[i]); -// result[i] += inner_x_value * (f[i.xm()] - f[i]); -// -// // upper y boundary -// const auto upper_y_avg = [&i](const auto& f) { return 0.5 * (f[i] + f[i.yp()]); }; -// const BoutReal upper_y_A = upper_y_avg(A); -// const BoutReal upper_y_J = upper_y_avg(J()); -// const BoutReal upper_y_g_22 = upper_y_avg(g22()); -// const BoutReal upper_y_g23 = upper_y_avg(g23()); -// const BoutReal upper_y_g_23 = upper_y_avg(g23()); -// const BoutReal upper_y_dy = upper_y_avg(dy); -// const BoutReal upper_y_value = -upper_y_A * upper_y_J * upper_y_g23 * upper_y_g_23 -// / (upper_y_g_22 * J()[i] * upper_y_dy * dy[i]); -// result[i] += upper_y_value * (f[i.yp()] - f[i]); -// -// // lower y boundary -// const auto lower_y_avg = [&i](const auto& f) { return 0.5 * (f[i] + f[i.ym()]); }; -// const BoutReal lower_y_A = lower_y_avg(A); -// const BoutReal lower_y_J = lower_y_avg(J()); -// const BoutReal lower_y_g_22 = lower_y_avg(g22()); -// const BoutReal lower_y_g23 = lower_y_avg(g23()); -// const BoutReal lower_y_g_23 = lower_y_avg(g23()); -// const BoutReal lower_y_dy = lower_y_avg(dy); -// const BoutReal lower_y_value = -lower_y_A * lower_y_J * lower_y_g23 * lower_y_g_23 -// / (lower_y_g_22 * J()[i] * lower_y_dy * dy[i]); -// result[i] += lower_y_value * (f[i.ym()] - f[i]); -// } -// -// return result; -//#else -// throw BoutException("Coordinates::Laplace_perpXY for 3D metric not implemented"); -//#endif -//} -// -//const Coordinates::FieldMetric& Coordinates::invSg() const { -// if (invSgCache == nullptr) { -// auto ptr = std::make_unique(); -// (*ptr) = 1.0 / sqrt(g22()); -// invSgCache = std::move(ptr); -// } -// return *invSgCache; -//} -// -//const Coordinates::FieldMetric& -//Coordinates::Grad2_par2_DDY_invSg(CELL_LOC outloc, const std::string& method) const { -// if (auto search = Grad2_par2_DDY_invSgCache.find(method); -// search != Grad2_par2_DDY_invSgCache.end()) { -// return *search->second; -// } -// invSg(); -// -// // Communicate to get parallel slices -// localmesh->communicate(*invSgCache); -// invSgCache->applyParallelBoundary("parallel_neumann"); -// -// // cache -// auto ptr = std::make_unique(); -// *ptr = DDY(*invSgCache, outloc, method) * invSg(); -// Grad2_par2_DDY_invSgCache[method] = std::move(ptr); -// return *Grad2_par2_DDY_invSgCache[method]; -//} +Field3D Coordinates::Div_par(const Field3D& f, CELL_LOC outloc, + const std::string& method) { + TRACE("Coordinates::Div_par( Field3D )"); + ASSERT1(location == outloc || outloc == CELL_DEFAULT) + + // Need Bxy at location of f, which might be different from location of this + // Coordinates object + auto Bxy_floc = f.getCoordinates()->Bxy(); + + if (!f.hasParallelSlices()) { + // No yup/ydown fields. The Grad_par operator will + // shift to field aligned coordinates + return differential_operators.Div_par( + f, geometry.Bxy(), geometry.getCovariantMetricTensor(), outloc, method); + } + + // Need to modify yup and ydown fields + Field3D f_B = f / Bxy_floc; + f_B.splitParallelSlices(); + for (int i = 0; i < f.getMesh()->ystart; ++i) { + f_B.yup(i) = f.yup(i) / Bxy_floc.yup(i); + f_B.ydown(i) = f.ydown(i) / Bxy_floc.ydown(i); + } + return Bxy() * Grad_par(f_B, outloc, method); +} + +///////////////////////////////////////////////////////// +// second parallel derivative (b dot Grad)(b dot Grad) +// Note: For parallel Laplacian use Laplace_par + +Coordinates::FieldMetric Coordinates::Grad2_par2(const Field2D& f, CELL_LOC outloc, + const std::string& method) { + TRACE("Coordinates::Grad2_par2( Field2D )"); + ASSERT1(location == outloc || (outloc == CELL_DEFAULT && location == f.getLocation())) + + auto result = Grad2_par2_DDY_invSg(outloc, method) * DDY(f, outloc, method) + + D2DY2(f, outloc, method) / g22(); + + return result; +} + +Field3D Coordinates::Grad2_par2(const Field3D& f, CELL_LOC outloc, + const std::string& method) { + TRACE("Coordinates::Grad2_par2( Field3D )"); + if (outloc == CELL_DEFAULT) { + outloc = f.getLocation(); + } + ASSERT1(location == outloc) + + Field3D result = ::DDY(f, outloc, method); + + Field3D r2 = D2DY2(f, outloc, method) / g22(); + + result = Grad2_par2_DDY_invSg(outloc, method) * result + r2; + + ASSERT2(result.getLocation() == outloc) + + return result; +} + +///////////////////////////////////////////////////////// +// perpendicular Laplacian operator + +#include // Delp2 uses same coefficients as inversion code + +Coordinates::FieldMetric Coordinates::Delp2(const Field2D& f, CELL_LOC outloc, + bool UNUSED(useFFT)) { + TRACE("Coordinates::Delp2( Field2D )"); + ASSERT1(location == outloc || outloc == CELL_DEFAULT) + + auto result = G1 * DDX(f, outloc) + g11() * D2DX2(f, outloc); + + return result; +} + +Field3D Coordinates::Delp2(const Field3D& f, CELL_LOC outloc, bool useFFT) { + TRACE("Coordinates::Delp2( Field3D )"); + + if (outloc == CELL_DEFAULT) { + outloc = f.getLocation(); + } + + ASSERT1(location == outloc) + ASSERT1(f.getLocation() == outloc) + + if (localmesh->GlobalNx == 1 && localmesh->GlobalNz == 1) { + // copy mesh, location, etc + return f * 0; + } + ASSERT2(localmesh->xstart > 0) // Need at least one guard cell + + Field3D result{emptyFrom(f).setLocation(outloc)}; + + if (useFFT and not bout::build::use_metric_3d) { + int ncz = localmesh->LocalNz; + + // Allocate memory + auto ft = Matrix(localmesh->LocalNx, ncz / 2 + 1); + auto delft = Matrix(localmesh->LocalNx, ncz / 2 + 1); + + // Loop over y indices + // Note: should not include y-guard or y-boundary points here as that would + // use values from corner cells in dx, which may not be initialised. + for (int jy = localmesh->ystart; jy <= localmesh->yend; jy++) { + + // Take forward FFT + + for (int jx = 0; jx < localmesh->LocalNx; jx++) { + rfft(&f(jx, jy, 0), ncz, &ft(jx, 0)); + } + + // Loop over kz + for (int jz = 0; jz <= ncz / 2; jz++) { + + // No smoothing in the x direction + for (int jx = localmesh->xstart; jx <= localmesh->xend; jx++) { + // Perform x derivative + + dcomplex a, b, c; + laplace_tridag_coefs(jx, jy, jz, a, b, c, nullptr, nullptr, outloc); + + delft(jx, jz) = a * ft(jx - 1, jz) + b * ft(jx, jz) + c * ft(jx + 1, jz); + } + } + + // Reverse FFT + for (int jx = localmesh->xstart; jx <= localmesh->xend; jx++) { + + irfft(&delft(jx, 0), ncz, &result(jx, jy, 0)); + } + } + } else { + result = G1 * ::DDX(f, outloc) + G3 * ::DDZ(f, outloc) + g11() * ::D2DX2(f, outloc) + + g33() * ::D2DZ2(f, outloc) + 2 * g13() * ::D2DXDZ(f, outloc); + } + + ASSERT2(result.getLocation() == outloc) + + return result; +} + +FieldPerp Coordinates::Delp2(const FieldPerp& f, CELL_LOC outloc, bool useFFT) { + TRACE("Coordinates::Delp2( FieldPerp )"); + + if (outloc == CELL_DEFAULT) { + outloc = f.getLocation(); + } + + ASSERT1(location == outloc) + ASSERT1(f.getLocation() == outloc) + + if (localmesh->GlobalNx == 1 && localmesh->GlobalNz == 1) { + // copy mesh, location, etc + return f * 0; + } + ASSERT2(localmesh->xstart > 0) // Need at least one guard cell + + FieldPerp result{emptyFrom(f).setLocation(outloc)}; + + int jy = f.getIndex(); + result.setIndex(jy); + + if (useFFT) { + int ncz = localmesh->LocalNz; + + // Allocate memory + auto ft = Matrix(localmesh->LocalNx, ncz / 2 + 1); + auto delft = Matrix(localmesh->LocalNx, ncz / 2 + 1); + + // Take forward FFT + for (int jx = 0; jx < localmesh->LocalNx; jx++) { + rfft(&f(jx, 0), ncz, &ft(jx, 0)); + } + + // Loop over kz + for (int jz = 0; jz <= ncz / 2; jz++) { + + // No smoothing in the x direction + for (int jx = localmesh->xstart; jx <= localmesh->xend; jx++) { + // Perform x derivative + + dcomplex a, b, c; + laplace_tridag_coefs(jx, jy, jz, a, b, c); + + delft(jx, jz) = a * ft(jx - 1, jz) + b * ft(jx, jz) + c * ft(jx + 1, jz); + } + } + + // Reverse FFT + for (int jx = localmesh->xstart; jx <= localmesh->xend; jx++) { + irfft(&delft(jx, 0), ncz, &result(jx, 0)); + } + + } else { + throw BoutException("Non-fourier Delp2 not currently implented for FieldPerp."); + // Would be the following but don't have standard derivative operators for FieldPerps + // yet + // result = G1 * ::DDX(f, outloc) + G3 * ::DDZ(f, outloc) + g11 * ::D2DX2(f, outloc) + // + g33 * ::D2DZ2(f, outloc) + 2 * g13 * ::D2DXDZ(f, outloc); + } + + return result; +} + +Coordinates::FieldMetric Coordinates::Laplace_par(const Field2D& f, CELL_LOC outloc) { + ASSERT1(location == outloc || outloc == CELL_DEFAULT) + + return D2DY2(f, outloc) / g22() + DDY(J() / g22(), outloc) * DDY(f, outloc) / J(); +} + +Field3D Coordinates::Laplace_par(const Field3D& f, CELL_LOC outloc) { + ASSERT1(location == outloc || outloc == CELL_DEFAULT) + return D2DY2(f, outloc) / g22() + DDY(J() / g22(), outloc) * ::DDY(f, outloc) / J(); +} + +// Full Laplacian operator on scalar field + +Coordinates::FieldMetric Coordinates::Laplace(const Field2D& f, CELL_LOC outloc, + const std::string& dfdy_boundary_conditions, + const std::string& dfdy_dy_region) { + TRACE("Coordinates::Laplace( Field2D )"); + ASSERT1(location == outloc || outloc == CELL_DEFAULT) + + auto result = G1 * DDX(f, outloc) + G2 * DDY(f, outloc) + g11() * D2DX2(f, outloc) + + g22() * D2DY2(f, outloc) + + 2.0 * g12() + * D2DXDY(f, outloc, "DEFAULT", "RGN_NOBNDRY", + dfdy_boundary_conditions, dfdy_dy_region); + + return result; +} + +Field3D Coordinates::Laplace(const Field3D& f, CELL_LOC outloc, + const std::string& dfdy_boundary_conditions, + const std::string& dfdy_dy_region) { + TRACE("Coordinates::Laplace( Field3D )"); + ASSERT1(location == outloc || outloc == CELL_DEFAULT) + + Field3D result = G1 * ::DDX(f, outloc) + G2 * ::DDY(f, outloc) + G3 * ::DDZ(f, outloc) + + g11() * D2DX2(f, outloc) + g22() * D2DY2(f, outloc) + + g33() * D2DZ2(f, outloc) + + 2.0 + * (g12() + * D2DXDY(f, outloc, "DEFAULT", "RGN_NOBNDRY", + dfdy_boundary_conditions, dfdy_dy_region) + + g13() * D2DXDZ(f, outloc) + g23() * D2DYDZ(f, outloc)); + + return result; +} + +// Full perpendicular Laplacian, in form of inverse of Laplacian operator in LaplaceXY +// solver +Field2D Coordinates::Laplace_perpXY(MAYBE_UNUSED(const Field2D& A), + MAYBE_UNUSED(const Field2D& f)) { + TRACE("Coordinates::Laplace_perpXY( Field2D )"); +#if not(BOUT_USE_METRIC_3D) + Field2D result; + result.allocate(); + for (auto i : result.getRegion(RGN_NOBNDRY)) { + result[i] = 0.; + + // outer x boundary + const auto outer_x_avg = [&i](const auto& f) { return 0.5 * (f[i] + f[i.xp()]); }; + const BoutReal outer_x_A = outer_x_avg(A); + const BoutReal outer_x_J = outer_x_avg(J()); + const BoutReal outer_x_g11 = outer_x_avg(g11()); + const BoutReal outer_x_dx = outer_x_avg(dx); + const BoutReal outer_x_value = + outer_x_A * outer_x_J * outer_x_g11 / (J()[i] * outer_x_dx * dx[i]); + result[i] += outer_x_value * (f[i.xp()] - f[i]); + + // inner x boundary + const auto inner_x_avg = [&i](const auto& f) { return 0.5 * (f[i] + f[i.xm()]); }; + const BoutReal inner_x_A = inner_x_avg(A); + const BoutReal inner_x_J = inner_x_avg(J()); + const BoutReal inner_x_g11 = inner_x_avg(g11()); + const BoutReal inner_x_dx = inner_x_avg(dx); + const BoutReal inner_x_value = + inner_x_A * inner_x_J * inner_x_g11 / (J()[i] * inner_x_dx * dx[i]); + result[i] += inner_x_value * (f[i.xm()] - f[i]); + + // upper y boundary + const auto upper_y_avg = [&i](const auto& f) { return 0.5 * (f[i] + f[i.yp()]); }; + const BoutReal upper_y_A = upper_y_avg(A); + const BoutReal upper_y_J = upper_y_avg(J()); + const BoutReal upper_y_g_22 = upper_y_avg(g22()); + const BoutReal upper_y_g23 = upper_y_avg(g23()); + const BoutReal upper_y_g_23 = upper_y_avg(g23()); + const BoutReal upper_y_dy = upper_y_avg(dy); + const BoutReal upper_y_value = -upper_y_A * upper_y_J * upper_y_g23 * upper_y_g_23 + / (upper_y_g_22 * J()[i] * upper_y_dy * dy[i]); + result[i] += upper_y_value * (f[i.yp()] - f[i]); + + // lower y boundary + const auto lower_y_avg = [&i](const auto& f) { return 0.5 * (f[i] + f[i.ym()]); }; + const BoutReal lower_y_A = lower_y_avg(A); + const BoutReal lower_y_J = lower_y_avg(J()); + const BoutReal lower_y_g_22 = lower_y_avg(g22()); + const BoutReal lower_y_g23 = lower_y_avg(g23()); + const BoutReal lower_y_g_23 = lower_y_avg(g23()); + const BoutReal lower_y_dy = lower_y_avg(dy); + const BoutReal lower_y_value = -lower_y_A * lower_y_J * lower_y_g23 * lower_y_g_23 + / (lower_y_g_22 * J()[i] * lower_y_dy * dy[i]); + result[i] += lower_y_value * (f[i.ym()] - f[i]); + } + + return result; +#else + throw BoutException("Coordinates::Laplace_perpXY for 3D metric not implemented"); +#endif +} + +const Coordinates::FieldMetric& Coordinates::invSg() const { + if (invSgCache == nullptr) { + auto ptr = std::make_unique(); + (*ptr) = 1.0 / sqrt(g22()); + invSgCache = std::move(ptr); + } + return *invSgCache; +} + +const Coordinates::FieldMetric& +Coordinates::Grad2_par2_DDY_invSg(CELL_LOC outloc, const std::string& method) const { + if (auto search = Grad2_par2_DDY_invSgCache.find(method); + search != Grad2_par2_DDY_invSgCache.end()) { + return *search->second; + } + invSg(); + + // Communicate to get parallel slices + localmesh->communicate(*invSgCache); + invSgCache->applyParallelBoundary("parallel_neumann"); + + // cache + auto ptr = std::make_unique(); + *ptr = DDY(*invSgCache, outloc, method) * invSg(); + Grad2_par2_DDY_invSgCache[method] = std::move(ptr); + return *Grad2_par2_DDY_invSgCache[method]; +} void Coordinates::checkCovariant() { return geometry.checkCovariant(localmesh->ystart); } diff --git a/src/mesh/differential_operators.cxx b/src/mesh/differential_operators.cxx index 55e86db0fa..35e7c9b607 100644 --- a/src/mesh/differential_operators.cxx +++ b/src/mesh/differential_operators.cxx @@ -112,18 +112,19 @@ Field3D DifferentialOperators::Vpar_Grad_par(const Field3D& v, const Field3D& f, // Parallel divergence FieldMetric DifferentialOperators::Div_par(const Field2D& f, const Field2D& Bxy, + const MetricTensor& covariantMetricTensor, CELL_LOC outloc, const std::string& method) { TRACE("DifferentialOperators::Div_par( Field2D )"); - ASSERT1(location == outloc || outloc == CELL_DEFAULT) // Need Bxy at location of f, which might be different from location of this // Coordinates object auto Bxy_floc = f.getCoordinates()->Bxy(); - return Bxy * Grad_par(f / Bxy_floc, outloc, method); + return Bxy * Grad_par(f / Bxy_floc, covariantMetricTensor, outloc, method); } Field3D DifferentialOperators::Div_par(const Field3D& f, const Field2D& Bxy, + const MetricTensor& covariantMetricTensor, CELL_LOC outloc, const std::string& method) { TRACE("DifferentialOperators::Div_par( Field3D )"); ASSERT1(location == outloc || outloc == CELL_DEFAULT) @@ -135,7 +136,7 @@ Field3D DifferentialOperators::Div_par(const Field3D& f, const Field2D& Bxy, if (!f.hasParallelSlices()) { // No yup/ydown fields. The Grad_par operator will // shift to field aligned coordinates - return Bxy * Grad_par(f / Bxy_floc, outloc, method); + return Bxy * Grad_par(f / Bxy_floc, covariantMetricTensor, outloc, method); } // Need to modify yup and ydown fields @@ -145,7 +146,7 @@ Field3D DifferentialOperators::Div_par(const Field3D& f, const Field2D& Bxy, f_B.yup(i) = f.yup(i) / Bxy_floc.yup(i); f_B.ydown(i) = f.ydown(i) / Bxy_floc.ydown(i); } - return Bxy * Grad_par(f_B, outloc, method); + return Bxy * Grad_par(f_B, covariantMetricTensor, outloc, method); } ///////////////////////////////////////////////////////// @@ -202,76 +203,76 @@ FieldMetric DifferentialOperators::Delp2(const Field2D& f, const Field2D& g11, return result; } -Field3D DifferentialOperators::Delp2(const Field3D& f, - MetricTensor& covariantMetricTensor, - const Field3D& G1, const Field3D& G3, - CELL_LOC outloc, bool useFFT) { - TRACE("DifferentialOperators::Delp2( Field3D )"); - - if (outloc == CELL_DEFAULT) { - outloc = f.getLocation(); - } - - ASSERT1(location == outloc) - ASSERT1(f.getLocation() == outloc) - - if (mesh->GlobalNx == 1 && mesh->GlobalNz == 1) { - // copy mesh, location, etc - return f * 0; - } - ASSERT2(mesh->xstart > 0) // Need at least one guard cell - - Field3D result{emptyFrom(f).setLocation(outloc)}; - - if (useFFT and not bout::build::use_metric_3d) { - int ncz = mesh->LocalNz; - - // Allocate memory - auto ft = Matrix(mesh->LocalNx, ncz / 2 + 1); - auto delft = Matrix(mesh->LocalNx, ncz / 2 + 1); - - // Loop over y indices - // Note: should not include y-guard or y-boundary points here as that would - // use values from corner cells in dx, which may not be initialised. - for (int jy = mesh->ystart; jy <= mesh->yend; jy++) { - - // Take forward FFT - - for (int jx = 0; jx < mesh->LocalNx; jx++) { - rfft(&f(jx, jy, 0), ncz, &ft(jx, 0)); - } - - // Loop over kz - for (int jz = 0; jz <= ncz / 2; jz++) { - - // No smoothing in the x direction - for (int jx = mesh->xstart; jx <= mesh->xend; jx++) { - // Perform x derivative - - dcomplex a, b, c; - laplace_tridag_coefs(jx, jy, jz, a, b, c, nullptr, nullptr, outloc); - - delft(jx, jz) = a * ft(jx - 1, jz) + b * ft(jx, jz) + c * ft(jx + 1, jz); - } - } - - // Reverse FFT - for (int jx = mesh->xstart; jx <= mesh->xend; jx++) { - - irfft(&delft(jx, 0), ncz, &result(jx, jy, 0)); - } - } - } else { - result = G1 * ::DDX(f, outloc) + G3 * ::DDZ(f, outloc) - + covariantMetricTensor.Getg11() * ::D2DX2(f, outloc) - + covariantMetricTensor.Getg33() * ::D2DZ2(f, outloc) - + 2 * covariantMetricTensor.Getg13() * ::D2DXDZ(f, outloc); - } - - ASSERT2(result.getLocation() == outloc) - - return result; -} +//Field3D DifferentialOperators::Delp2(const Field3D& f, +// MetricTensor& covariantMetricTensor, +// const Field3D& G1, const Field3D& G3, +// CELL_LOC outloc, bool useFFT) { +// TRACE("DifferentialOperators::Delp2( Field3D )"); +// +// if (outloc == CELL_DEFAULT) { +// outloc = f.getLocation(); +// } +// +// ASSERT1(location == outloc) +// ASSERT1(f.getLocation() == outloc) +// +// if (mesh->GlobalNx == 1 && mesh->GlobalNz == 1) { +// // copy mesh, location, etc +// return f * 0; +// } +// ASSERT2(mesh->xstart > 0) // Need at least one guard cell +// +// Field3D result{emptyFrom(f).setLocation(outloc)}; +// +// if (useFFT and not bout::build::use_metric_3d) { +// int ncz = mesh->LocalNz; +// +// // Allocate memory +// auto ft = Matrix(mesh->LocalNx, ncz / 2 + 1); +// auto delft = Matrix(mesh->LocalNx, ncz / 2 + 1); +// +// // Loop over y indices +// // Note: should not include y-guard or y-boundary points here as that would +// // use values from corner cells in dx, which may not be initialised. +// for (int jy = mesh->ystart; jy <= mesh->yend; jy++) { +// +// // Take forward FFT +// +// for (int jx = 0; jx < mesh->LocalNx; jx++) { +// rfft(&f(jx, jy, 0), ncz, &ft(jx, 0)); +// } +// +// // Loop over kz +// for (int jz = 0; jz <= ncz / 2; jz++) { +// +// // No smoothing in the x direction +// for (int jx = mesh->xstart; jx <= mesh->xend; jx++) { +// // Perform x derivative +// +// dcomplex a, b, c; +// laplace_tridag_coefs(jx, jy, jz, a, b, c, nullptr, nullptr, outloc); +// +// delft(jx, jz) = a * ft(jx - 1, jz) + b * ft(jx, jz) + c * ft(jx + 1, jz); +// } +// } +// +// // Reverse FFT +// for (int jx = mesh->xstart; jx <= mesh->xend; jx++) { +// +// irfft(&delft(jx, 0), ncz, &result(jx, jy, 0)); +// } +// } +// } else { +// result = G1 * ::DDX(f, outloc) + G3 * ::DDZ(f, outloc) +// + covariantMetricTensor.Getg11() * ::D2DX2(f, outloc) +// + covariantMetricTensor.Getg33() * ::D2DZ2(f, outloc) +// + 2 * covariantMetricTensor.Getg13() * ::D2DXDZ(f, outloc); +// } +// +// ASSERT2(result.getLocation() == outloc) +// +// return result; +//} FieldPerp DifferentialOperators::Delp2(const FieldPerp& f, CELL_LOC outloc, bool useFFT) { TRACE("DifferentialOperators::Delp2( FieldPerp )"); From c397fe3b83ad33d1c5b2719933f027b545d3dc0c Mon Sep 17 00:00:00 2001 From: tomc271 Date: Sun, 26 Nov 2023 22:27:15 +0000 Subject: [PATCH 195/491] Make DifferentialOperators::Grad2_par2_DDY_invSg() method public. --- include/bout/differential_operators.hxx | 8 ++-- src/mesh/coordinates.cxx | 62 ++++++++++++++----------- 2 files changed, 38 insertions(+), 32 deletions(-) diff --git a/include/bout/differential_operators.hxx b/include/bout/differential_operators.hxx index 7f17b9f9b7..9d01e3ee29 100644 --- a/include/bout/differential_operators.hxx +++ b/include/bout/differential_operators.hxx @@ -118,6 +118,10 @@ public: void invalidateAndRecalculateCachedVariables(); + const FieldMetric& Grad2_par2_DDY_invSg(const MetricTensor& covariantMetricTensor, + CELL_LOC outloc, + const std::string& method) const; + private: Mesh* mesh; FieldMetric& intShiftTorsion; @@ -131,10 +135,6 @@ private: mutable std::unique_ptr invSgCache{nullptr}; FieldMetric& invSg(const MetricTensor& covariantMetricTensor) const; - - const FieldMetric& Grad2_par2_DDY_invSg(const MetricTensor& covariantMetricTensor, - CELL_LOC outloc, - const std::string& method) const; }; #endif //BOUT_DIFFERENTIALOPERATORS_HXX diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 3aa3d32c58..2546e44023 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -1326,7 +1326,9 @@ Coordinates::FieldMetric Coordinates::Grad2_par2(const Field2D& f, CELL_LOC outl TRACE("Coordinates::Grad2_par2( Field2D )"); ASSERT1(location == outloc || (outloc == CELL_DEFAULT && location == f.getLocation())) - auto result = Grad2_par2_DDY_invSg(outloc, method) * DDY(f, outloc, method) + auto result = differential_operators.Grad2_par2_DDY_invSg( + geometry.getCovariantMetricTensor(), outloc, method) + * DDY(f, outloc, method) + D2DY2(f, outloc, method) / g22(); return result; @@ -1344,7 +1346,10 @@ Field3D Coordinates::Grad2_par2(const Field3D& f, CELL_LOC outloc, Field3D r2 = D2DY2(f, outloc, method) / g22(); - result = Grad2_par2_DDY_invSg(outloc, method) * result + r2; + result = differential_operators.Grad2_par2_DDY_invSg( + geometry.getCovariantMetricTensor(), outloc, method) + * result + + r2; ASSERT2(result.getLocation() == outloc) @@ -1603,33 +1608,34 @@ Field2D Coordinates::Laplace_perpXY(MAYBE_UNUSED(const Field2D& A), #endif } -const Coordinates::FieldMetric& Coordinates::invSg() const { - if (invSgCache == nullptr) { - auto ptr = std::make_unique(); - (*ptr) = 1.0 / sqrt(g22()); - invSgCache = std::move(ptr); - } - return *invSgCache; -} - -const Coordinates::FieldMetric& -Coordinates::Grad2_par2_DDY_invSg(CELL_LOC outloc, const std::string& method) const { - if (auto search = Grad2_par2_DDY_invSgCache.find(method); - search != Grad2_par2_DDY_invSgCache.end()) { - return *search->second; - } - invSg(); - - // Communicate to get parallel slices - localmesh->communicate(*invSgCache); - invSgCache->applyParallelBoundary("parallel_neumann"); +//const Coordinates::FieldMetric& Coordinates::invSg() const { +// if (invSgCache == nullptr) { +// auto ptr = std::make_unique(); +// (*ptr) = 1.0 / sqrt(g22()); +// invSgCache = std::move(ptr); +// } +// return *invSgCache; +//} - // cache - auto ptr = std::make_unique(); - *ptr = DDY(*invSgCache, outloc, method) * invSg(); - Grad2_par2_DDY_invSgCache[method] = std::move(ptr); - return *Grad2_par2_DDY_invSgCache[method]; -} +//const Coordinates::FieldMetric& +//Coordinates::Grad2_par2_DDY_invSg(CELL_LOC outloc, const std::string& method) const { +// +// if (auto search = Grad2_par2_DDY_invSgCache.find(method); +// search != Grad2_par2_DDY_invSgCache.end()) { +// return *search->second; +// } +// invSg(); +// +// // Communicate to get parallel slices +// localmesh->communicate(*invSgCache); +// invSgCache->applyParallelBoundary("parallel_neumann"); +// +// // cache +// auto ptr = std::make_unique(); +// *ptr = DDY(*invSgCache, outloc, method) * invSg(); +// Grad2_par2_DDY_invSgCache[method] = std::move(ptr); +// return *Grad2_par2_DDY_invSgCache[method]; +//} void Coordinates::checkCovariant() { return geometry.checkCovariant(localmesh->ystart); } From e5b9f83150cb1608debe6b7a0e7e6d2cd4723779 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Sun, 26 Nov 2023 22:40:58 +0000 Subject: [PATCH 196/491] Fix signature of 'Coordinates::Grad_par' method (re-add unused parameters). --- include/bout/coordinates.hxx | 75 ++++++++++++++++++------------------ src/mesh/coordinates.cxx | 9 +++-- 2 files changed, 43 insertions(+), 41 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index 17210c4a14..87fc868628 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -189,7 +189,8 @@ public: // const std::string& region = "RGN_NOBNDRY"); /// Gradient along magnetic field b.Grad(f) - Field2D Grad_par(const Field2D& var); + Field2D Grad_par(const Field2D& var, CELL_LOC outloc = CELL_DEFAULT, + const std::string& method = "DEFAULT"); Field3D Grad_par(const Field3D& var, CELL_LOC outloc = CELL_DEFAULT, const std::string& method = "DEFAULT"); @@ -203,42 +204,42 @@ public: CELL_LOC outloc = CELL_DEFAULT, const std::string& method = "DEFAULT"); - /// Divergence along magnetic field Div(b*f) = B.Grad(f/B) - FieldMetric Div_par(const Field2D& f, CELL_LOC outloc = CELL_DEFAULT, - const std::string& method = "DEFAULT"); - - Field3D Div_par(const Field3D& f, CELL_LOC outloc = CELL_DEFAULT, - const std::string& method = "DEFAULT"); - - // Second derivative along magnetic field - FieldMetric Grad2_par2(const Field2D& f, CELL_LOC outloc = CELL_DEFAULT, - const std::string& method = "DEFAULT"); - - Field3D Grad2_par2(const Field3D& f, CELL_LOC outloc = CELL_DEFAULT, - const std::string& method = "DEFAULT"); - // Perpendicular Laplacian operator, using only X-Z derivatives - // NOTE: This might be better bundled with the Laplacian inversion code - // since it makes use of the same coefficients and FFT routines - FieldMetric Delp2(const Field2D& f, CELL_LOC outloc = CELL_DEFAULT, bool useFFT = true); - Field3D Delp2(const Field3D& f, CELL_LOC outloc = CELL_DEFAULT, bool useFFT = true); - FieldPerp Delp2(const FieldPerp& f, CELL_LOC outloc = CELL_DEFAULT, bool useFFT = true); - - // Full parallel Laplacian operator on scalar field - // Laplace_par(f) = Div( b (b dot Grad(f)) ) - FieldMetric Laplace_par(const Field2D& f, CELL_LOC outloc = CELL_DEFAULT); - Field3D Laplace_par(const Field3D& f, CELL_LOC outloc = CELL_DEFAULT); - - // Full Laplacian operator on scalar field - FieldMetric Laplace(const Field2D& f, CELL_LOC outloc = CELL_DEFAULT, - const std::string& dfdy_boundary_conditions = "free_o3", - const std::string& dfdy_dy_region = ""); - Field3D Laplace(const Field3D& f, CELL_LOC outloc = CELL_DEFAULT, - const std::string& dfdy_boundary_conditions = "free_o3", - const std::string& dfdy_dy_region = ""); - - // Full perpendicular Laplacian, in form of inverse of Laplacian operator in LaplaceXY - // solver - Field2D Laplace_perpXY(const Field2D& A, const Field2D& f); + /// Divergence along magnetic field Div(b*f) = B.Grad(f/B) + FieldMetric Div_par(const Field2D& f, CELL_LOC outloc = CELL_DEFAULT, + const std::string& method = "DEFAULT"); + + Field3D Div_par(const Field3D& f, CELL_LOC outloc = CELL_DEFAULT, + const std::string& method = "DEFAULT"); + + // Second derivative along magnetic field + FieldMetric Grad2_par2(const Field2D& f, CELL_LOC outloc = CELL_DEFAULT, + const std::string& method = "DEFAULT"); + + Field3D Grad2_par2(const Field3D& f, CELL_LOC outloc = CELL_DEFAULT, + const std::string& method = "DEFAULT"); + // Perpendicular Laplacian operator, using only X-Z derivatives + // NOTE: This might be better bundled with the Laplacian inversion code + // since it makes use of the same coefficients and FFT routines + FieldMetric Delp2(const Field2D& f, CELL_LOC outloc = CELL_DEFAULT, bool useFFT = true); + Field3D Delp2(const Field3D& f, CELL_LOC outloc = CELL_DEFAULT, bool useFFT = true); + FieldPerp Delp2(const FieldPerp& f, CELL_LOC outloc = CELL_DEFAULT, bool useFFT = true); + + // Full parallel Laplacian operator on scalar field + // Laplace_par(f) = Div( b (b dot Grad(f)) ) + FieldMetric Laplace_par(const Field2D& f, CELL_LOC outloc = CELL_DEFAULT); + Field3D Laplace_par(const Field3D& f, CELL_LOC outloc = CELL_DEFAULT); + + // Full Laplacian operator on scalar field + FieldMetric Laplace(const Field2D& f, CELL_LOC outloc = CELL_DEFAULT, + const std::string& dfdy_boundary_conditions = "free_o3", + const std::string& dfdy_dy_region = ""); + Field3D Laplace(const Field3D& f, CELL_LOC outloc = CELL_DEFAULT, + const std::string& dfdy_boundary_conditions = "free_o3", + const std::string& dfdy_dy_region = ""); + + // Full perpendicular Laplacian, in form of inverse of Laplacian operator in LaplaceXY + // solver + Field2D Laplace_perpXY(const Field2D& A, const Field2D& f); private: int nz; // Size of mesh in Z. This is mesh->ngz-1 diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 2546e44023..7d824a4ce6 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -1243,7 +1243,8 @@ void Coordinates::setParallelTransform(Options* options) { ///////////////////////////////////////////////////////// // Parallel gradient -Field2D Coordinates::Grad_par(const Field2D& var) { +Field2D Coordinates::Grad_par(const Field2D& var, [[maybe_unused]] CELL_LOC outloc, + const std::string& UNUSED(method)) { TRACE("Coordinates::Grad_par( Field2D )"); return differential_operators.Grad_par(var, geometry.getCovariantMetricTensor()); @@ -1263,7 +1264,7 @@ Field3D Coordinates::Grad_par(const Field3D& var, CELL_LOC outloc, // vparallel times the parallel derivative along unperturbed B-field Field2D Coordinates::Vpar_Grad_par(const Field2D& v, const Field2D& f, - MAYBE_UNUSED(CELL_LOC outloc), + [[maybe_unused]] CELL_LOC outloc, const std::string& UNUSED(method)) { ASSERT1(location == outloc || (outloc == CELL_DEFAULT && location == f.getLocation())) @@ -1548,8 +1549,8 @@ Field3D Coordinates::Laplace(const Field3D& f, CELL_LOC outloc, // Full perpendicular Laplacian, in form of inverse of Laplacian operator in LaplaceXY // solver -Field2D Coordinates::Laplace_perpXY(MAYBE_UNUSED(const Field2D& A), - MAYBE_UNUSED(const Field2D& f)) { +Field2D Coordinates::Laplace_perpXY([[maybe_unused]] const Field2D& A, + [[maybe_unused]] const Field2D& f) const { TRACE("Coordinates::Laplace_perpXY( Field2D )"); #if not(BOUT_USE_METRIC_3D) Field2D result; From e111202896d036af46a047d5747db6de9f1913a0 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Sun, 26 Nov 2023 22:41:10 +0000 Subject: [PATCH 197/491] Remove 'Delp2' methods from DifferentialOperators class. --- include/bout/differential_operators.hxx | 20 +-- src/mesh/differential_operators.cxx | 154 ++++++++++++------------ 2 files changed, 87 insertions(+), 87 deletions(-) diff --git a/include/bout/differential_operators.hxx b/include/bout/differential_operators.hxx index 9d01e3ee29..e0293ec665 100644 --- a/include/bout/differential_operators.hxx +++ b/include/bout/differential_operators.hxx @@ -79,16 +79,16 @@ public: CELL_LOC outloc = CELL_DEFAULT, const std::string& method = "DEFAULT"); - // Perpendicular Laplacian operator, using only X-Z derivatives - // NOTE: This might be better bundled with the Laplacian inversion code - // since it makes use of the same coefficients and FFT routines - FieldMetric Delp2(const Field2D& f, const Field2D& g11, const Field2D& G1, - CELL_LOC outloc = CELL_DEFAULT, bool useFFT = true); - - Field3D Delp2(const Field3D& f, MetricTensor& covariantMetricTensor, const Field3D& G1, - const Field3D& G3, CELL_LOC outloc = CELL_DEFAULT, bool useFFT = true); - - FieldPerp Delp2(const FieldPerp& f, CELL_LOC outloc = CELL_DEFAULT, bool useFFT = true); +// // Perpendicular Laplacian operator, using only X-Z derivatives +// // NOTE: This might be better bundled with the Laplacian inversion code +// // since it makes use of the same coefficients and FFT routines +// FieldMetric Delp2(const Field2D& f, const Field2D& g11, const Field2D& G1, +// CELL_LOC outloc = CELL_DEFAULT, bool useFFT = true); +// +// Field3D Delp2(const Field3D& f, MetricTensor& covariantMetricTensor, const Field3D& G1, +// const Field3D& G3, CELL_LOC outloc = CELL_DEFAULT, bool useFFT = true); +// +// FieldPerp Delp2(const FieldPerp& f, CELL_LOC outloc = CELL_DEFAULT, bool useFFT = true); // Full parallel Laplacian operator on scalar field // Laplace_par(f) = Div( b (b dot Grad(f)) ) diff --git a/src/mesh/differential_operators.cxx b/src/mesh/differential_operators.cxx index 35e7c9b607..ea54dac371 100644 --- a/src/mesh/differential_operators.cxx +++ b/src/mesh/differential_operators.cxx @@ -187,21 +187,21 @@ Field3D DifferentialOperators::Grad2_par2(const Field3D& f, return result; } -///////////////////////////////////////////////////////// -// perpendicular Laplacian operator - -#include // Delp2 uses same coefficients as inversion code - -FieldMetric DifferentialOperators::Delp2(const Field2D& f, const Field2D& g11, - const Field2D& G1, CELL_LOC outloc, - bool UNUSED(useFFT)) { - TRACE("DifferentialOperators::Delp2( Field2D )"); - ASSERT1(location == outloc || outloc == CELL_DEFAULT) - - auto result = G1 * DDX(f, outloc) + g11 * D2DX2(f, outloc); - - return result; -} +/////////////////////////////////////////////////////////// +//// perpendicular Laplacian operator +// +//#include // Delp2 uses same coefficients as inversion code +// +//FieldMetric DifferentialOperators::Delp2(const Field2D& f, const Field2D& g11, +// const Field2D& G1, CELL_LOC outloc, +// bool UNUSED(useFFT)) { +// TRACE("DifferentialOperators::Delp2( Field2D )"); +// ASSERT1(location == outloc || outloc == CELL_DEFAULT) +// +// auto result = G1 * DDX(f, outloc) + g11 * D2DX2(f, outloc); +// +// return result; +//} //Field3D DifferentialOperators::Delp2(const Field3D& f, // MetricTensor& covariantMetricTensor, @@ -274,68 +274,68 @@ FieldMetric DifferentialOperators::Delp2(const Field2D& f, const Field2D& g11, // return result; //} -FieldPerp DifferentialOperators::Delp2(const FieldPerp& f, CELL_LOC outloc, bool useFFT) { - TRACE("DifferentialOperators::Delp2( FieldPerp )"); - - if (outloc == CELL_DEFAULT) { - outloc = f.getLocation(); - } - - ASSERT1(location == outloc) - ASSERT1(f.getLocation() == outloc) - - if (mesh->GlobalNx == 1 && mesh->GlobalNz == 1) { - // copy mesh, location, etc - return f * 0; - } - ASSERT2(mesh->xstart > 0) // Need at least one guard cell - - FieldPerp result{emptyFrom(f).setLocation(outloc)}; - - int jy = f.getIndex(); - result.setIndex(jy); - - if (useFFT) { - int ncz = mesh->LocalNz; - - // Allocate memory - auto ft = Matrix(mesh->LocalNx, ncz / 2 + 1); - auto delft = Matrix(mesh->LocalNx, ncz / 2 + 1); - - // Take forward FFT - for (int jx = 0; jx < mesh->LocalNx; jx++) { - rfft(&f(jx, 0), ncz, &ft(jx, 0)); - } - - // Loop over kz - for (int jz = 0; jz <= ncz / 2; jz++) { - - // No smoothing in the x direction - for (int jx = mesh->xstart; jx <= mesh->xend; jx++) { - // Perform x derivative - - dcomplex a, b, c; - laplace_tridag_coefs(jx, jy, jz, a, b, c); - - delft(jx, jz) = a * ft(jx - 1, jz) + b * ft(jx, jz) + c * ft(jx + 1, jz); - } - } - - // Reverse FFT - for (int jx = mesh->xstart; jx <= mesh->xend; jx++) { - irfft(&delft(jx, 0), ncz, &result(jx, 0)); - } - - } else { - throw BoutException("Non-fourier Delp2 not currently implented for FieldPerp."); - // Would be the following but don't have standard derivative operators for FieldPerps - // yet - // result = G1 * ::DDX(f, outloc) + G3 * ::DDZ(f, outloc) + g11 * ::D2DX2(f, outloc) - // + g33 * ::D2DZ2(f, outloc) + 2 * g13 * ::D2DXDZ(f, outloc); - } - - return result; -} +//FieldPerp DifferentialOperators::Delp2(const FieldPerp& f, CELL_LOC outloc, bool useFFT) { +// TRACE("DifferentialOperators::Delp2( FieldPerp )"); +// +// if (outloc == CELL_DEFAULT) { +// outloc = f.getLocation(); +// } +// +// ASSERT1(location == outloc) +// ASSERT1(f.getLocation() == outloc) +// +// if (mesh->GlobalNx == 1 && mesh->GlobalNz == 1) { +// // copy mesh, location, etc +// return f * 0; +// } +// ASSERT2(mesh->xstart > 0) // Need at least one guard cell +// +// FieldPerp result{emptyFrom(f).setLocation(outloc)}; +// +// int jy = f.getIndex(); +// result.setIndex(jy); +// +// if (useFFT) { +// int ncz = mesh->LocalNz; +// +// // Allocate memory +// auto ft = Matrix(mesh->LocalNx, ncz / 2 + 1); +// auto delft = Matrix(mesh->LocalNx, ncz / 2 + 1); +// +// // Take forward FFT +// for (int jx = 0; jx < mesh->LocalNx; jx++) { +// rfft(&f(jx, 0), ncz, &ft(jx, 0)); +// } +// +// // Loop over kz +// for (int jz = 0; jz <= ncz / 2; jz++) { +// +// // No smoothing in the x direction +// for (int jx = mesh->xstart; jx <= mesh->xend; jx++) { +// // Perform x derivative +// +// dcomplex a, b, c; +// laplace_tridag_coefs(jx, jy, jz, a, b, c); +// +// delft(jx, jz) = a * ft(jx - 1, jz) + b * ft(jx, jz) + c * ft(jx + 1, jz); +// } +// } +// +// // Reverse FFT +// for (int jx = mesh->xstart; jx <= mesh->xend; jx++) { +// irfft(&delft(jx, 0), ncz, &result(jx, 0)); +// } +// +// } else { +// throw BoutException("Non-fourier Delp2 not currently implented for FieldPerp."); +// // Would be the following but don't have standard derivative operators for FieldPerps +// // yet +// // result = G1 * ::DDX(f, outloc) + G3 * ::DDZ(f, outloc) + g11 * ::D2DX2(f, outloc) +// // + g33 * ::D2DZ2(f, outloc) + 2 * g13 * ::D2DXDZ(f, outloc); +// } +// +// return result; +//} FieldMetric DifferentialOperators::Laplace_par(const Field2D& f, const Field2D& g22, const Field2D& J, CELL_LOC outloc) { From 21299b4e9d74eb305705bb998e74d9ec08edc332 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Sun, 26 Nov 2023 22:48:09 +0000 Subject: [PATCH 198/491] intShiftTorsion can't be a reference type (apparently). --- include/bout/differential_operators.hxx | 27 ++++++++++++------------- src/mesh/coordinates.cxx | 2 +- src/mesh/differential_operators.cxx | 2 +- 3 files changed, 15 insertions(+), 16 deletions(-) diff --git a/include/bout/differential_operators.hxx b/include/bout/differential_operators.hxx index e0293ec665..2747eb2b15 100644 --- a/include/bout/differential_operators.hxx +++ b/include/bout/differential_operators.hxx @@ -10,9 +10,8 @@ class DifferentialOperators { using FieldMetric = MetricTensor::FieldMetric; public: - DifferentialOperators(Mesh* mesh, FieldMetric& intShiftTorsion, - const CELL_LOC& location, FieldMetric& dx, FieldMetric& dy, - FieldMetric& dz); + DifferentialOperators(Mesh* mesh, FieldMetric intShiftTorsion, const CELL_LOC& location, + FieldMetric& dx, FieldMetric& dy, FieldMetric& dz); // DifferentialOperators(DifferentialOperators operators, // DifferentialOperators::FieldMetric& dx); @@ -79,16 +78,16 @@ public: CELL_LOC outloc = CELL_DEFAULT, const std::string& method = "DEFAULT"); -// // Perpendicular Laplacian operator, using only X-Z derivatives -// // NOTE: This might be better bundled with the Laplacian inversion code -// // since it makes use of the same coefficients and FFT routines -// FieldMetric Delp2(const Field2D& f, const Field2D& g11, const Field2D& G1, -// CELL_LOC outloc = CELL_DEFAULT, bool useFFT = true); -// -// Field3D Delp2(const Field3D& f, MetricTensor& covariantMetricTensor, const Field3D& G1, -// const Field3D& G3, CELL_LOC outloc = CELL_DEFAULT, bool useFFT = true); -// -// FieldPerp Delp2(const FieldPerp& f, CELL_LOC outloc = CELL_DEFAULT, bool useFFT = true); + // // Perpendicular Laplacian operator, using only X-Z derivatives + // // NOTE: This might be better bundled with the Laplacian inversion code + // // since it makes use of the same coefficients and FFT routines + // FieldMetric Delp2(const Field2D& f, const Field2D& g11, const Field2D& G1, + // CELL_LOC outloc = CELL_DEFAULT, bool useFFT = true); + // + // Field3D Delp2(const Field3D& f, MetricTensor& covariantMetricTensor, const Field3D& G1, + // const Field3D& G3, CELL_LOC outloc = CELL_DEFAULT, bool useFFT = true); + // + // FieldPerp Delp2(const FieldPerp& f, CELL_LOC outloc = CELL_DEFAULT, bool useFFT = true); // Full parallel Laplacian operator on scalar field // Laplace_par(f) = Div( b (b dot Grad(f)) ) @@ -124,7 +123,7 @@ public: private: Mesh* mesh; - FieldMetric& intShiftTorsion; + FieldMetric intShiftTorsion; const CELL_LOC& location; FieldMetric& dx; FieldMetric& dy; diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 7d824a4ce6..5567b199b4 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -298,7 +298,7 @@ Coordinates::Coordinates(Mesh* mesh, FieldMetric dx, FieldMetric dy, FieldMetric : dx(std::move(dx)), dy(std::move(dy)), dz(dz), ShiftTorsion(std::move(ShiftTorsion)), IntShiftTorsion(std::move(IntShiftTorsion)), nz(mesh->LocalNz), localmesh(mesh), location(CELL_CENTRE), differential_operators(DifferentialOperators( - mesh, IntShiftTorsion, location, dx, dy, dz)), + mesh, this->IntShiftTorsion, location, dx, dy, dz)), geometry(Geometry(J, Bxy, g11, g22, g33, g12, g13, g23, g_11, g_22, g_33, g_12, g_13, g_23, differential_operators)) {} diff --git a/src/mesh/differential_operators.cxx b/src/mesh/differential_operators.cxx index ea54dac371..439257cb28 100644 --- a/src/mesh/differential_operators.cxx +++ b/src/mesh/differential_operators.cxx @@ -8,7 +8,7 @@ //#include "bout/paralleltransform.hxx" #include -DifferentialOperators::DifferentialOperators(Mesh* mesh, FieldMetric& intShiftTorsion, +DifferentialOperators::DifferentialOperators(Mesh* mesh, FieldMetric intShiftTorsion, const CELL_LOC& location, FieldMetric& dx, FieldMetric& dy, FieldMetric& dz) : mesh(mesh), intShiftTorsion(intShiftTorsion), location(location), dx(dx), dy(dy), From d77ab314f1e4d07868dedfe3848770c3374d2452 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Sun, 26 Nov 2023 23:03:37 +0000 Subject: [PATCH 199/491] Clang-Tidy --- include/bout/coordinates.hxx | 2 +- src/mesh/coordinates.cxx | 94 ++++++++++++++++++------------------ 2 files changed, 49 insertions(+), 47 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index 87fc868628..65b6abb45a 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -82,7 +82,7 @@ public: const Field2D& zlength() const; /// True if corrections for non-uniform mesh spacing should be included in operators - bool non_uniform; + bool non_uniform{}; /// 2nd-order correction for non-uniform meshes d/di(1/dx), d/di(1/dy) and d/di(1/dz) FieldMetric d1_dx, d1_dy, d1_dz; diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 5567b199b4..c851522f77 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -13,8 +13,6 @@ #include -#include - #include "parallel/fci.hxx" #include "parallel/shiftedmetricinterp.hxx" @@ -24,7 +22,6 @@ #include "bout/field3d.hxx" #include "bout/geometry.hxx" #include "bout/metricTensor.hxx" -#include "bout/utils.hxx" #include // use anonymous namespace so this utility function is not available outside this file @@ -265,10 +262,9 @@ Coordinates::FieldMetric Coordinates::getUnaligned(const std::string& name, if (field.getDirectionY() == YDirectionType::Aligned and transform->canToFromFieldAligned()) { return transform->fromFieldAligned(field); - } else { - field.setDirectionY(YDirectionType::Standard); - return field; } + field.setDirectionY(YDirectionType::Standard); + return field; } Coordinates::FieldMetric Coordinates::getUnalignedAtLocationAndFillGuards( @@ -278,7 +274,7 @@ Coordinates::FieldMetric Coordinates::getUnalignedAtLocationAndFillGuards( ParallelTransform* pParallelTransform) { auto field = getAtLocOrUnaligned(mesh, name, default_value, suffix, cell_location); - if (suffix == "") { + if (suffix.empty()) { no_extra_interpolate = false; pParallelTransform = transform.get(); } @@ -297,10 +293,14 @@ Coordinates::Coordinates(Mesh* mesh, FieldMetric dx, FieldMetric dy, FieldMetric FieldMetric IntShiftTorsion) : dx(std::move(dx)), dy(std::move(dy)), dz(dz), ShiftTorsion(std::move(ShiftTorsion)), IntShiftTorsion(std::move(IntShiftTorsion)), nz(mesh->LocalNz), localmesh(mesh), - location(CELL_CENTRE), differential_operators(DifferentialOperators( - mesh, this->IntShiftTorsion, location, dx, dy, dz)), - geometry(Geometry(J, Bxy, g11, g22, g33, g12, g13, g23, g_11, g_22, g_33, g_12, - g_13, g_23, differential_operators)) {} + location(CELL_CENTRE), + differential_operators(DifferentialOperators(mesh, this->IntShiftTorsion, location, + this->dx, this->dy, dz)), + geometry(Geometry(std::move(J), std::move(Bxy), std::move(g11), std::move(g22), + std::move(g33), std::move(g12), std::move(g13), std::move(g23), + std::move(g_11), std::move(g_22), std::move(g_33), + std::move(g_12), std::move(g_13), std::move(g_23), + differential_operators)) {} Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, const Coordinates* coords_in, bool force_interpolate_from_centre) @@ -323,10 +323,10 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, nz = mesh->LocalNz; // Default to true in case staggered quantities are not read from file - bool extrapolate_x = true; - bool extrapolate_y = true; + const bool extrapolate_x = true; + const bool extrapolate_y = true; - if (coords_in && suffix != "" + if ((coords_in != nullptr) && !suffix.empty() && (force_interpolate_from_centre || !mesh->sourceHasVar("dx" + suffix))) { // Interpolate fields from coords_in @@ -349,8 +349,8 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, dz = localmesh->interpolateAndExtrapolate(coords_in->dz, location, true, true, false, transform.get()); - std::function - interpolateAndExtrapolate_function = [this](const FieldMetric component) { + std::function const + interpolateAndExtrapolate_function = [this](const FieldMetric& component) { return localmesh->interpolateAndExtrapolate(component, location, true, true, false, transform.get()); }; @@ -519,9 +519,9 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, // More robust to extrapolate derived quantities directly, rather than // deriving from extrapolated covariant metric components - std::function + std::function const interpolateAndExtrapolate_function = [this, extrapolate_y, extrapolate_x]( - const FieldMetric component) { + const FieldMetric& component) { return localmesh->interpolateAndExtrapolate( component, location, extrapolate_x, extrapolate_y, false, transform.get()); }; @@ -541,7 +541,7 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, "\tWARNING: Jacobian 'J_{:s}' not found. Calculating from metric tensor\n", suffix); setJ(Jcalc); - } catch (BoutException) { + } catch (BoutException&) { setJ(localmesh->interpolateAndExtrapolate(J(), location, extrapolate_x, extrapolate_y, false, transform.get())); @@ -570,7 +570,7 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, "Calculating from metric tensor\n", suffix); setBxy(Bcalc); - } catch (BoutException) { + } catch (BoutException&) { setBxy(localmesh->interpolateAndExtrapolate(Bxy(), location, extrapolate_x, extrapolate_y, false, transform.get())); output_warn.write("\tMaximum difference in Bxy is {:e}\n", max(abs(Bxy() - Bcalc))); @@ -580,7 +580,7 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, bout::checkFinite(Bxy(), "Bxy" + suffix, "RGN_NOCORNERS"); bout::checkPositive(Bxy(), "Bxy" + suffix, "RGN_NOCORNERS"); - if (getAtLoc(mesh, ShiftTorsion, "ShiftTorsion", suffix, location)) { + if (getAtLoc(mesh, ShiftTorsion, "ShiftTorsion", suffix, location) != 0) { output_warn.write("\tWARNING: No Torsion specified for zShift. " "Derivatives may not be correct\n"); ShiftTorsion = 0.0; @@ -592,7 +592,7 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, if (mesh->IncIntShear) { checkStaggeredGet(mesh, "IntShiftTorsion", suffix); - if (mesh->get(IntShiftTorsion, "IntShiftTorsion" + suffix, 0.0, false)) { + if (mesh->get(IntShiftTorsion, "IntShiftTorsion" + suffix, 0.0, false) != 0) { output_warn.write("\tWARNING: No Integrated torsion specified\n"); IntShiftTorsion = 0.0; } @@ -608,7 +608,7 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, } void Coordinates::outputVars(Options& output_options) { - Timer time("io"); + Timer const time("io"); const std::string loc_string = (location == CELL_CENTRE) ? "" : "_" + toString(location); @@ -758,17 +758,18 @@ int Coordinates::calculateGeometry(bool recalculate_staggered, OPTION(Options::getRoot(), non_uniform, true); - Coordinates::FieldMetric d2x(localmesh), d2y(localmesh), - d2z(localmesh); // d^2 x / d i^2 + Coordinates::FieldMetric d2x(localmesh); + Coordinates::FieldMetric d2y(localmesh); + Coordinates::FieldMetric const d2z(localmesh); // d^2 x / d i^2 // Read correction for non-uniform meshes - std::string suffix = getLocationSuffix(location); + std::string const suffix = getLocationSuffix(location); if (location == CELL_CENTRE or (!force_interpolate_from_centre and localmesh->sourceHasVar("dx" + suffix))) { - bool extrapolate_x = not localmesh->sourceHasXBoundaryGuards(); - bool extrapolate_y = not localmesh->sourceHasYBoundaryGuards(); + bool const extrapolate_x = not localmesh->sourceHasXBoundaryGuards(); + bool const extrapolate_y = not localmesh->sourceHasYBoundaryGuards(); - if (localmesh->get(d2x, "d2x" + suffix, 0.0, false, location)) { + if (localmesh->get(d2x, "d2x" + suffix, 0.0, false, location) != 0) { output_warn.write("\tWARNING: differencing quantity 'd2x' not found. " "Calculating from dx\n"); d1_dx = bout::derivatives::index::DDX(1. / dx); // d/di(1/dx) @@ -785,7 +786,7 @@ int Coordinates::calculateGeometry(bool recalculate_staggered, d1_dx = -d2x / (dx * dx); } - if (localmesh->get(d2y, "d2y" + suffix, 0.0, false, location)) { + if (localmesh->get(d2y, "d2y" + suffix, 0.0, false, location) != 0) { output_warn.write("\tWARNING: differencing quantity 'd2y' not found. " "Calculating from dy\n"); d1_dy = DDY(1. / dy); // d/di(1/dy) @@ -822,7 +823,7 @@ int Coordinates::calculateGeometry(bool recalculate_staggered, d1_dz = 0; #endif } else { - if (localmesh->get(d2x, "d2x", 0.0, false)) { + if (localmesh->get(d2x, "d2x", 0.0, false) != 0) { output_warn.write("\tWARNING: differencing quantity 'd2x' not found. " "Calculating from dx\n"); d1_dx = bout::derivatives::index::DDX(1. / dx); // d/di(1/dx) @@ -838,7 +839,7 @@ int Coordinates::calculateGeometry(bool recalculate_staggered, d1_dx = -d2x / (dx * dx); } - if (localmesh->get(d2y, "d2y", 0.0, false)) { + if (localmesh->get(d2y, "d2y", 0.0, false) != 0) { output_warn.write("\tWARNING: differencing quantity 'd2y' not found. " "Calculating from dy\n"); d1_dy = DDY(1. / dy); // d/di(1/dy) @@ -1055,10 +1056,10 @@ void Coordinates::jacobian() { TRACE("Geometry::jacobian"); try { - const auto j = geometry.recalculateJacobian(); + const auto jacobian = geometry.recalculateJacobian(); // More robust to extrapolate derived quantities directly, rather than // deriving from extrapolated covariant metric components - geometry.setJ(localmesh->interpolateAndExtrapolate(j, location, extrapolate_x, + geometry.setJ(localmesh->interpolateAndExtrapolate(jacobian, location, extrapolate_x, extrapolate_y, false)); const auto Bxy = geometry.recalculateBxy(); @@ -1074,7 +1075,7 @@ void Coordinates::jacobian() { namespace { // Utility function for fixing up guard cells of zShift void fixZShiftGuards(Field2D& zShift) { - auto localmesh = zShift.getMesh(); + auto* localmesh = zShift.getMesh(); // extrapolate into boundary guard cells if necessary zShift = localmesh->interpolateAndExtrapolate( @@ -1103,7 +1104,7 @@ void fixZShiftGuards(Field2D& zShift) { } // namespace void Coordinates::setParallelTransform(Options* options) { - auto ptoptions = options->getSection("paralleltransform"); + auto* ptoptions = options->getSection("paralleltransform"); std::string ptstr; ptoptions->get("type", ptstr, "identity"); @@ -1122,13 +1123,13 @@ void Coordinates::setParallelTransform(Options* options) { Field2D zShift{localmesh}; // Read the zShift angle from the mesh - std::string suffix = getLocationSuffix(location); + std::string const suffix = getLocationSuffix(location); if (localmesh->sourceHasVar("dx" + suffix)) { // Grid file has variables at this location, so should be able to read checkStaggeredGet(localmesh, "zShift", suffix); - if (localmesh->get(zShift, "zShift" + suffix, 0.0, false, location)) { + if (localmesh->get(zShift, "zShift" + suffix, 0.0, false, location) != 0) { // No zShift variable. Try qinty in BOUT grid files - if (localmesh->get(zShift, "qinty" + suffix, 0.0, false, location)) { + if (localmesh->get(zShift, "qinty" + suffix, 0.0, false, location) != 0) { // Failed to find either variable, cannot use ShiftedMetric throw BoutException("Could not read zShift" + suffix + " from grid file"); } @@ -1140,9 +1141,9 @@ void Coordinates::setParallelTransform(Options* options) { "file."); } Field2D zShift_centre; - if (localmesh->get(zShift_centre, "zShift", 0.0, false)) { + if (localmesh->get(zShift_centre, "zShift", 0.0, false) != 0) { // No zShift variable. Try qinty in BOUT grid files - if (localmesh->get(zShift_centre, "qinty", 0.0, false)) { + if (localmesh->get(zShift_centre, "qinty", 0.0, false) != 0) { // Failed to find either variable, cannot use ShiftedMetric throw BoutException("Could not read zShift from grid file"); } @@ -1361,6 +1362,7 @@ Field3D Coordinates::Grad2_par2(const Field3D& f, CELL_LOC outloc, // perpendicular Laplacian operator #include // Delp2 uses same coefficients as inversion code +#include Coordinates::FieldMetric Coordinates::Delp2(const Field2D& f, CELL_LOC outloc, bool UNUSED(useFFT)) { @@ -1456,11 +1458,11 @@ FieldPerp Coordinates::Delp2(const FieldPerp& f, CELL_LOC outloc, bool useFFT) { FieldPerp result{emptyFrom(f).setLocation(outloc)}; - int jy = f.getIndex(); + int const jy = f.getIndex(); result.setIndex(jy); if (useFFT) { - int ncz = localmesh->LocalNz; + int const ncz = localmesh->LocalNz; // Allocate memory auto ft = Matrix(localmesh->LocalNx, ncz / 2 + 1); @@ -1644,12 +1646,12 @@ void Coordinates::checkContravariant() { geometry.checkContravariant(localmesh-> void Coordinates::setContravariantMetricTensor(MetricTensor metric_tensor, const std::string& region) { - geometry.setContravariantMetricTensor(metric_tensor, location, region); + geometry.setContravariantMetricTensor(std::move(metric_tensor), location, region); } void Coordinates::setCovariantMetricTensor(MetricTensor metric_tensor, const std::string& region) { - geometry.setCovariantMetricTensor(metric_tensor, location, region); + geometry.setCovariantMetricTensor(std::move(metric_tensor), location, region); } const FieldMetric& Coordinates::g_11() const { return geometry.g_11(); } @@ -1682,7 +1684,7 @@ void Coordinates::setJ(BoutReal value, int x, int y) { void Coordinates::setBxy(FieldMetric Bxy) { //TODO: Calculate Bxy and check value is close - geometry.setBxy(Bxy); + geometry.setBxy(std::move(Bxy)); } const MetricTensor& Coordinates::getContravariantMetricTensor() const { From 069b09ca23a5d1ac1231aea82f171fb0b2f2f676 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Sun, 26 Nov 2023 23:14:19 +0000 Subject: [PATCH 200/491] DifferentialOperators field 'location' can't be a reference type, or const qualified. --- include/bout/differential_operators.hxx | 4 ++-- src/mesh/differential_operators.cxx | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/include/bout/differential_operators.hxx b/include/bout/differential_operators.hxx index 2747eb2b15..ab8d6ed0ab 100644 --- a/include/bout/differential_operators.hxx +++ b/include/bout/differential_operators.hxx @@ -10,7 +10,7 @@ class DifferentialOperators { using FieldMetric = MetricTensor::FieldMetric; public: - DifferentialOperators(Mesh* mesh, FieldMetric intShiftTorsion, const CELL_LOC& location, + DifferentialOperators(Mesh* mesh, FieldMetric intShiftTorsion, CELL_LOC location, FieldMetric& dx, FieldMetric& dy, FieldMetric& dz); // DifferentialOperators(DifferentialOperators operators, @@ -124,7 +124,7 @@ public: private: Mesh* mesh; FieldMetric intShiftTorsion; - const CELL_LOC& location; + CELL_LOC location; FieldMetric& dx; FieldMetric& dy; FieldMetric& dz; diff --git a/src/mesh/differential_operators.cxx b/src/mesh/differential_operators.cxx index 439257cb28..25464469de 100644 --- a/src/mesh/differential_operators.cxx +++ b/src/mesh/differential_operators.cxx @@ -9,7 +9,7 @@ #include DifferentialOperators::DifferentialOperators(Mesh* mesh, FieldMetric intShiftTorsion, - const CELL_LOC& location, FieldMetric& dx, + const CELL_LOC location, FieldMetric& dx, FieldMetric& dy, FieldMetric& dz) : mesh(mesh), intShiftTorsion(intShiftTorsion), location(location), dx(dx), dy(dy), dz(dz) {} From a592fac09131cb38e011d2aa09eddf462f10e138 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Tue, 28 Nov 2023 12:05:35 +0000 Subject: [PATCH 201/491] unfinished changes --- include/bout/coordinates.hxx | 4 ++++ include/bout/differential_operators.hxx | 2 ++ src/mesh/differential_operators.cxx | 2 ++ 3 files changed, 8 insertions(+) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index 65b6abb45a..da79bb46c7 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -69,8 +69,12 @@ public: FieldMetric g_22, FieldMetric g_33, FieldMetric g_12, FieldMetric g_13, FieldMetric g_23, FieldMetric ShiftTorsion, FieldMetric IntShiftTorsion); + Coordinates(Coordinates&&) = default; Coordinates& operator=(Coordinates&&) = default; + Coordinates(const Coordinates&) = delete; + Coordinates& operator=(const Coordinates&) = delete; + ~Coordinates() = default; /// Add variables to \p output_options, for post-processing diff --git a/include/bout/differential_operators.hxx b/include/bout/differential_operators.hxx index ab8d6ed0ab..8f92845c8b 100644 --- a/include/bout/differential_operators.hxx +++ b/include/bout/differential_operators.hxx @@ -10,6 +10,8 @@ class DifferentialOperators { using FieldMetric = MetricTensor::FieldMetric; public: + DifferentialOperators(DifferentialOperators const& operators); + DifferentialOperators(Mesh* mesh, FieldMetric intShiftTorsion, CELL_LOC location, FieldMetric& dx, FieldMetric& dy, FieldMetric& dz); diff --git a/src/mesh/differential_operators.cxx b/src/mesh/differential_operators.cxx index 25464469de..f8e8856c9c 100644 --- a/src/mesh/differential_operators.cxx +++ b/src/mesh/differential_operators.cxx @@ -8,6 +8,8 @@ //#include "bout/paralleltransform.hxx" #include +DifferentialOperators::DifferentialOperators(const DifferentialOperators& operators) {} + DifferentialOperators::DifferentialOperators(Mesh* mesh, FieldMetric intShiftTorsion, const CELL_LOC location, FieldMetric& dx, FieldMetric& dy, FieldMetric& dz) From 2a9f1807b1187ffb8b5c76e2f1a7b06d6eb15004 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Tue, 28 Nov 2023 12:16:55 +0000 Subject: [PATCH 202/491] "Rule of 5" for DifferentialOperators. --- include/bout/differential_operators.hxx | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/include/bout/differential_operators.hxx b/include/bout/differential_operators.hxx index 8f92845c8b..736f7c3d2e 100644 --- a/include/bout/differential_operators.hxx +++ b/include/bout/differential_operators.hxx @@ -10,8 +10,12 @@ class DifferentialOperators { using FieldMetric = MetricTensor::FieldMetric; public: - DifferentialOperators(DifferentialOperators const& operators); - + DifferentialOperators(DifferentialOperators&&) = default; + DifferentialOperators& operator=(DifferentialOperators&&) = default; + + DifferentialOperators(const DifferentialOperators& operators) = delete; + DifferentialOperators& operator=(DifferentialOperators&) = delete; + DifferentialOperators(Mesh* mesh, FieldMetric intShiftTorsion, CELL_LOC location, FieldMetric& dx, FieldMetric& dy, FieldMetric& dz); From b879ed690c468608d20830a08c43234c30afbede Mon Sep 17 00:00:00 2001 From: tomc271 Date: Sun, 26 Nov 2023 23:25:30 +0000 Subject: [PATCH 203/491] DifferentialOperators fields dx, dy, dz can't be a reference types. --- include/bout/differential_operators.hxx | 8 ++++---- src/mesh/differential_operators.cxx | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/include/bout/differential_operators.hxx b/include/bout/differential_operators.hxx index 736f7c3d2e..9592b8277b 100644 --- a/include/bout/differential_operators.hxx +++ b/include/bout/differential_operators.hxx @@ -17,7 +17,7 @@ public: DifferentialOperators& operator=(DifferentialOperators&) = delete; DifferentialOperators(Mesh* mesh, FieldMetric intShiftTorsion, CELL_LOC location, - FieldMetric& dx, FieldMetric& dy, FieldMetric& dz); + FieldMetric dx, FieldMetric dy, FieldMetric dz); // DifferentialOperators(DifferentialOperators operators, // DifferentialOperators::FieldMetric& dx); @@ -131,9 +131,9 @@ private: Mesh* mesh; FieldMetric intShiftTorsion; CELL_LOC location; - FieldMetric& dx; - FieldMetric& dy; - FieldMetric& dz; + FieldMetric dx; + FieldMetric dy; + FieldMetric dz; /// Cache variable for Grad2_par2 mutable std::map> Grad2_par2_DDY_invSgCache; diff --git a/src/mesh/differential_operators.cxx b/src/mesh/differential_operators.cxx index f8e8856c9c..11854255f7 100644 --- a/src/mesh/differential_operators.cxx +++ b/src/mesh/differential_operators.cxx @@ -11,8 +11,8 @@ DifferentialOperators::DifferentialOperators(const DifferentialOperators& operators) {} DifferentialOperators::DifferentialOperators(Mesh* mesh, FieldMetric intShiftTorsion, - const CELL_LOC location, FieldMetric& dx, - FieldMetric& dy, FieldMetric& dz) + const CELL_LOC location, FieldMetric dx, + FieldMetric dy, FieldMetric dz) : mesh(mesh), intShiftTorsion(intShiftTorsion), location(location), dx(dx), dy(dy), dz(dz) {} From 892f8ae67590c41f5093308ac7724fe5f7243dc1 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Tue, 28 Nov 2023 13:58:19 +0000 Subject: [PATCH 204/491] Default destructor for DifferentialOperators. --- include/bout/differential_operators.hxx | 2 ++ 1 file changed, 2 insertions(+) diff --git a/include/bout/differential_operators.hxx b/include/bout/differential_operators.hxx index 9592b8277b..9874afec45 100644 --- a/include/bout/differential_operators.hxx +++ b/include/bout/differential_operators.hxx @@ -19,6 +19,8 @@ public: DifferentialOperators(Mesh* mesh, FieldMetric intShiftTorsion, CELL_LOC location, FieldMetric dx, FieldMetric dy, FieldMetric dz); + ~DifferentialOperators() = default; + // DifferentialOperators(DifferentialOperators operators, // DifferentialOperators::FieldMetric& dx); From db1c2ff5875037d17dd3c28ed37751e9d8ec9e0e Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 29 Nov 2023 09:56:19 +0000 Subject: [PATCH 205/491] "Rule of zero". --- include/bout/coordinates.hxx | 8 -------- include/bout/differential_operators.hxx | 7 ------- 2 files changed, 15 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index da79bb46c7..54f8a82867 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -69,14 +69,6 @@ public: FieldMetric g_22, FieldMetric g_33, FieldMetric g_12, FieldMetric g_13, FieldMetric g_23, FieldMetric ShiftTorsion, FieldMetric IntShiftTorsion); - Coordinates(Coordinates&&) = default; - Coordinates& operator=(Coordinates&&) = default; - - Coordinates(const Coordinates&) = delete; - Coordinates& operator=(const Coordinates&) = delete; - - ~Coordinates() = default; - /// Add variables to \p output_options, for post-processing void outputVars(Options& output_options); diff --git a/include/bout/differential_operators.hxx b/include/bout/differential_operators.hxx index 9874afec45..067bd33d73 100644 --- a/include/bout/differential_operators.hxx +++ b/include/bout/differential_operators.hxx @@ -10,17 +10,10 @@ class DifferentialOperators { using FieldMetric = MetricTensor::FieldMetric; public: - DifferentialOperators(DifferentialOperators&&) = default; - DifferentialOperators& operator=(DifferentialOperators&&) = default; - - DifferentialOperators(const DifferentialOperators& operators) = delete; - DifferentialOperators& operator=(DifferentialOperators&) = delete; DifferentialOperators(Mesh* mesh, FieldMetric intShiftTorsion, CELL_LOC location, FieldMetric dx, FieldMetric dy, FieldMetric dz); - ~DifferentialOperators() = default; - // DifferentialOperators(DifferentialOperators operators, // DifferentialOperators::FieldMetric& dx); From 04b8c75e653933bd023a4094bfc700cf15e182bb Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 29 Nov 2023 11:03:47 +0000 Subject: [PATCH 206/491] Use pointer instead of reference to DifferentialOperators in Geometry class. --- include/bout/geometry.hxx | 6 +- src/mesh/coordinates.cxx | 4 +- src/mesh/geometry.cxx | 206 +++++++++++++++++++------------------- 3 files changed, 108 insertions(+), 108 deletions(-) diff --git a/include/bout/geometry.hxx b/include/bout/geometry.hxx index 7a354caf65..9b73aa2907 100644 --- a/include/bout/geometry.hxx +++ b/include/bout/geometry.hxx @@ -52,9 +52,9 @@ public: FieldMetric g33, FieldMetric g12, FieldMetric g13, FieldMetric g23, FieldMetric g_11, FieldMetric g_22, FieldMetric g_33, FieldMetric g_12, FieldMetric g_13, FieldMetric g_23, - DifferentialOperators& differential_operators); + DifferentialOperators* differential_operators); - Geometry(Mesh* mesh, DifferentialOperators& differential_operators); + Geometry(Mesh* mesh, DifferentialOperators* differential_operators); /// Christoffel symbol of the second kind (connection coefficients) FieldMetric G1_11, G1_22, G1_33, G1_12, G1_13, G1_23; @@ -135,7 +135,7 @@ private: FieldMetric this_J; FieldMetric this_Bxy; ///< Magnitude of B = nabla z times nabla x - DifferentialOperators& differential_operators; + DifferentialOperators* differential_operators; // template // void communicate(T& t, Ts... ts); diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index c851522f77..d4b4e63a9b 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -300,7 +300,7 @@ Coordinates::Coordinates(Mesh* mesh, FieldMetric dx, FieldMetric dy, FieldMetric std::move(g33), std::move(g12), std::move(g13), std::move(g23), std::move(g_11), std::move(g_22), std::move(g_33), std::move(g_12), std::move(g_13), std::move(g_23), - differential_operators)) {} + &differential_operators)) {} Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, const Coordinates* coords_in, bool force_interpolate_from_centre) @@ -312,7 +312,7 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, ShiftTorsion(mesh), IntShiftTorsion(mesh), localmesh(mesh), location(loc), differential_operators( DifferentialOperators(mesh, IntShiftTorsion, loc, dx, dy, dz)), - geometry(Geometry(mesh, differential_operators)) { + geometry(Geometry(mesh, &differential_operators)) { if (options == nullptr) { options = Options::getRoot()->getSection("mesh"); diff --git a/src/mesh/geometry.cxx b/src/mesh/geometry.cxx index c181b24836..8ce0e26d5c 100644 --- a/src/mesh/geometry.cxx +++ b/src/mesh/geometry.cxx @@ -398,12 +398,12 @@ Geometry::Geometry(FieldMetric J, FieldMetric Bxy, FieldMetric g11, FieldMetric FieldMetric g33, FieldMetric g12, FieldMetric g13, FieldMetric g23, FieldMetric g_11, FieldMetric g_22, FieldMetric g_33, FieldMetric g_12, FieldMetric g_13, FieldMetric g_23, - DifferentialOperators& differential_operators) + DifferentialOperators* differential_operators) : contravariantMetricTensor(g11, g22, g33, g12, g13, g23), covariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23), this_J(J), this_Bxy(Bxy), differential_operators(differential_operators) {} -Geometry::Geometry(Mesh* mesh, DifferentialOperators& differential_operators) +Geometry::Geometry(Mesh* mesh, DifferentialOperators* differential_operators) //bool force_interpolate_from_centre : G1_11(mesh), G1_22(mesh), G1_33(mesh), G1_12(mesh), G1_13(mesh), G1_23(mesh), G2_11(mesh), G2_22(mesh), G2_33(mesh), G2_12(mesh), G2_13(mesh), G2_23(mesh), @@ -701,172 +701,172 @@ void Geometry::CalculateChristoffelSymbols() { // tensor can be 2D or 3D. For 2D, all DDZ terms are zero G1_11 = 0.5 * contravariantMetricTensor.Getg11() - * differential_operators.DDX(covariantMetricTensor.Getg11()) + * differential_operators->DDX(covariantMetricTensor.Getg11(), dx) + contravariantMetricTensor.Getg12() - * (differential_operators.DDX(covariantMetricTensor.Getg12()) - - 0.5 * differential_operators.DDY(covariantMetricTensor.Getg11())) + * (differential_operators->DDX(covariantMetricTensor.Getg12()) + - 0.5 * differential_operators->DDY(covariantMetricTensor.Getg11())) + contravariantMetricTensor.Getg13() - * (differential_operators.DDX(covariantMetricTensor.Getg13()) - - 0.5 * differential_operators.DDZ(covariantMetricTensor.Getg11())); + * (differential_operators->DDX(covariantMetricTensor.Getg13()) + - 0.5 * differential_operators->DDZ(covariantMetricTensor.Getg11())); G1_22 = contravariantMetricTensor.Getg11() - * (differential_operators.DDY(covariantMetricTensor.Getg12()) - - 0.5 * differential_operators.DDX(covariantMetricTensor.Getg22())) + * (differential_operators->DDY(covariantMetricTensor.Getg12()) + - 0.5 * differential_operators->DDX(covariantMetricTensor.Getg22())) + 0.5 * contravariantMetricTensor.Getg12() - * differential_operators.DDY(covariantMetricTensor.Getg22()) + * differential_operators->DDY(covariantMetricTensor.Getg22()) + contravariantMetricTensor.Getg13() - * (differential_operators.DDY(covariantMetricTensor.Getg23()) - - 0.5 * differential_operators.DDZ(covariantMetricTensor.Getg22())); + * (differential_operators->DDY(covariantMetricTensor.Getg23()) + - 0.5 * differential_operators->DDZ(covariantMetricTensor.Getg22())); G1_33 = contravariantMetricTensor.Getg11() - * (differential_operators.DDZ(covariantMetricTensor.Getg13()) - - 0.5 * differential_operators.DDX(covariantMetricTensor.Getg33())) + * (differential_operators->DDZ(covariantMetricTensor.Getg13()) + - 0.5 * differential_operators->DDX(covariantMetricTensor.Getg33())) + contravariantMetricTensor.Getg12() - * (differential_operators.DDZ(covariantMetricTensor.Getg23()) - - 0.5 * differential_operators.DDY(covariantMetricTensor.Getg33())) + * (differential_operators->DDZ(covariantMetricTensor.Getg23()) + - 0.5 * differential_operators->DDY(covariantMetricTensor.Getg33())) + 0.5 * contravariantMetricTensor.Getg13() - * differential_operators.DDZ(covariantMetricTensor.Getg33()); + * differential_operators->DDZ(covariantMetricTensor.Getg33()); G1_12 = 0.5 * contravariantMetricTensor.Getg11() - * differential_operators.DDY(covariantMetricTensor.Getg11()) + * differential_operators->DDY(covariantMetricTensor.Getg11()) + 0.5 * contravariantMetricTensor.Getg12() - * differential_operators.DDX(covariantMetricTensor.Getg22()) + * differential_operators->DDX(covariantMetricTensor.Getg22()) + 0.5 * contravariantMetricTensor.Getg13() - * (differential_operators.DDY(covariantMetricTensor.Getg13()) - + differential_operators.DDX(covariantMetricTensor.Getg23()) - - differential_operators.DDZ(covariantMetricTensor.Getg12())); + * (differential_operators->DDY(covariantMetricTensor.Getg13()) + + differential_operators->DDX(covariantMetricTensor.Getg23()) + - differential_operators->DDZ(covariantMetricTensor.Getg12())); G1_13 = 0.5 * contravariantMetricTensor.Getg11() - * differential_operators.DDZ(covariantMetricTensor.Getg11()) + * differential_operators->DDZ(covariantMetricTensor.Getg11()) + 0.5 * contravariantMetricTensor.Getg12() - * (differential_operators.DDZ(covariantMetricTensor.Getg12()) - + differential_operators.DDX(covariantMetricTensor.Getg23()) - - differential_operators.DDY(covariantMetricTensor.Getg13())) + * (differential_operators->DDZ(covariantMetricTensor.Getg12()) + + differential_operators->DDX(covariantMetricTensor.Getg23()) + - differential_operators->DDY(covariantMetricTensor.Getg13())) + 0.5 * contravariantMetricTensor.Getg13() - * differential_operators.DDX(covariantMetricTensor.Getg33()); + * differential_operators->DDX(covariantMetricTensor.Getg33()); G1_23 = 0.5 * contravariantMetricTensor.Getg11() - * (differential_operators.DDZ(covariantMetricTensor.Getg12()) - + differential_operators.DDY(covariantMetricTensor.Getg13()) - - differential_operators.DDX(covariantMetricTensor.Getg23())) + * (differential_operators->DDZ(covariantMetricTensor.Getg12()) + + differential_operators->DDY(covariantMetricTensor.Getg13()) + - differential_operators->DDX(covariantMetricTensor.Getg23())) + 0.5 * contravariantMetricTensor.Getg12() - * (differential_operators.DDZ(covariantMetricTensor.Getg22()) - + differential_operators.DDY(covariantMetricTensor.Getg23()) - - differential_operators.DDY(covariantMetricTensor.Getg23())) - // + 0.5 *g13*(differential_operators.DDZ(g_32) + differential_operators.DDY(g_33) - differential_operators.DDZ(g_23)); + * (differential_operators->DDZ(covariantMetricTensor.Getg22()) + + differential_operators->DDY(covariantMetricTensor.Getg23()) + - differential_operators->DDY(covariantMetricTensor.Getg23())) + // + 0.5 *g13*(differential_operators->DDZ(g_32) + differential_operators->DDY(g_33) - differential_operators->DDZ(g_23)); // which equals + 0.5 * contravariantMetricTensor.Getg13() - * differential_operators.DDY(covariantMetricTensor.Getg33()); + * differential_operators->DDY(covariantMetricTensor.Getg33()); G2_11 = 0.5 * contravariantMetricTensor.Getg12() - * differential_operators.DDX(covariantMetricTensor.Getg11()) + * differential_operators->DDX(covariantMetricTensor.Getg11()) + contravariantMetricTensor.Getg22() - * (differential_operators.DDX(covariantMetricTensor.Getg12()) - - 0.5 * differential_operators.DDY(covariantMetricTensor.Getg11())) + * (differential_operators->DDX(covariantMetricTensor.Getg12()) + - 0.5 * differential_operators->DDY(covariantMetricTensor.Getg11())) + contravariantMetricTensor.Getg23() - * (differential_operators.DDX(covariantMetricTensor.Getg13()) - - 0.5 * differential_operators.DDZ(covariantMetricTensor.Getg11())); + * (differential_operators->DDX(covariantMetricTensor.Getg13()) + - 0.5 * differential_operators->DDZ(covariantMetricTensor.Getg11())); G2_22 = contravariantMetricTensor.Getg12() - * (differential_operators.DDY(covariantMetricTensor.Getg12()) - - 0.5 * differential_operators.DDX(covariantMetricTensor.Getg22())) + * (differential_operators->DDY(covariantMetricTensor.Getg12()) + - 0.5 * differential_operators->DDX(covariantMetricTensor.Getg22())) + 0.5 * contravariantMetricTensor.Getg22() - * differential_operators.DDY(covariantMetricTensor.Getg22()) + * differential_operators->DDY(covariantMetricTensor.Getg22()) + contravariantMetricTensor.Getg23() - * (differential_operators.DDY(contravariantMetricTensor.Getg23()) - - 0.5 * differential_operators.DDZ(covariantMetricTensor.Getg22())); + * (differential_operators->DDY(contravariantMetricTensor.Getg23()) + - 0.5 * differential_operators->DDZ(covariantMetricTensor.Getg22())); G2_33 = contravariantMetricTensor.Getg12() - * (differential_operators.DDZ(covariantMetricTensor.Getg13()) - - 0.5 * differential_operators.DDX(covariantMetricTensor.Getg33())) + * (differential_operators->DDZ(covariantMetricTensor.Getg13()) + - 0.5 * differential_operators->DDX(covariantMetricTensor.Getg33())) + contravariantMetricTensor.Getg22() - * (differential_operators.DDZ(covariantMetricTensor.Getg23()) - - 0.5 * differential_operators.DDY(covariantMetricTensor.Getg33())) + * (differential_operators->DDZ(covariantMetricTensor.Getg23()) + - 0.5 * differential_operators->DDY(covariantMetricTensor.Getg33())) + 0.5 * contravariantMetricTensor.Getg23() - * differential_operators.DDZ(covariantMetricTensor.Getg33()); + * differential_operators->DDZ(covariantMetricTensor.Getg33()); G2_12 = 0.5 * contravariantMetricTensor.Getg12() - * differential_operators.DDY(covariantMetricTensor.Getg11()) + * differential_operators->DDY(covariantMetricTensor.Getg11()) + 0.5 * contravariantMetricTensor.Getg22() - * differential_operators.DDX(covariantMetricTensor.Getg22()) + * differential_operators->DDX(covariantMetricTensor.Getg22()) + 0.5 * contravariantMetricTensor.Getg23() - * (differential_operators.DDY(covariantMetricTensor.Getg13()) - + differential_operators.DDX(covariantMetricTensor.Getg23()) - - differential_operators.DDZ(covariantMetricTensor.Getg12())); + * (differential_operators->DDY(covariantMetricTensor.Getg13()) + + differential_operators->DDX(covariantMetricTensor.Getg23()) + - differential_operators->DDZ(covariantMetricTensor.Getg12())); G2_13 = - // 0.5 *g21*(differential_operators.DDZ(covariantMetricTensor.Getg11()) + differential_operators.DDX(covariantMetricTensor.Getg13()) - differential_operators.DDX(covariantMetricTensor.Getg13())) + // 0.5 *g21*(differential_operators->DDZ(covariantMetricTensor.Getg11()) + differential_operators->DDX(covariantMetricTensor.Getg13()) - differential_operators->DDX(covariantMetricTensor.Getg13())) // which equals 0.5 * contravariantMetricTensor.Getg12() - * (differential_operators.DDZ(covariantMetricTensor.Getg11()) - + differential_operators.DDX(covariantMetricTensor.Getg13()) - - differential_operators.DDX(covariantMetricTensor.Getg13())) - // + 0.5 *g22*(differential_operators.DDZ(covariantMetricTensor.Getg21()) + differential_operators.DDX(covariantMetricTensor.Getg23()) - differential_operators.DDY(covariantMetricTensor.Getg13())) + * (differential_operators->DDZ(covariantMetricTensor.Getg11()) + + differential_operators->DDX(covariantMetricTensor.Getg13()) + - differential_operators->DDX(covariantMetricTensor.Getg13())) + // + 0.5 *g22*(differential_operators->DDZ(covariantMetricTensor.Getg21()) + differential_operators->DDX(covariantMetricTensor.Getg23()) - differential_operators->DDY(covariantMetricTensor.Getg13())) // which equals + 0.5 * contravariantMetricTensor.Getg22() - * (differential_operators.DDZ(covariantMetricTensor.Getg12()) - + differential_operators.DDX(covariantMetricTensor.Getg23()) - - differential_operators.DDY(covariantMetricTensor.Getg13())) - // + 0.5 *g23*(differential_operators.DDZ(covariantMetricTensor.Getg31()) + differential_operators.DDX(covariantMetricTensor.Getg33()) - differential_operators.DDZ(g_13)); + * (differential_operators->DDZ(covariantMetricTensor.Getg12()) + + differential_operators->DDX(covariantMetricTensor.Getg23()) + - differential_operators->DDY(covariantMetricTensor.Getg13())) + // + 0.5 *g23*(differential_operators->DDZ(covariantMetricTensor.Getg31()) + differential_operators->DDX(covariantMetricTensor.Getg33()) - differential_operators->DDZ(g_13)); // which equals + 0.5 * contravariantMetricTensor.Getg23() - * differential_operators.DDX(covariantMetricTensor.Getg33()); + * differential_operators->DDX(covariantMetricTensor.Getg33()); G2_23 = 0.5 * contravariantMetricTensor.Getg12() - * (differential_operators.DDZ(covariantMetricTensor.Getg12()) - + differential_operators.DDY(covariantMetricTensor.Getg13()) - - differential_operators.DDX(covariantMetricTensor.Getg23())) + * (differential_operators->DDZ(covariantMetricTensor.Getg12()) + + differential_operators->DDY(covariantMetricTensor.Getg13()) + - differential_operators->DDX(covariantMetricTensor.Getg23())) + 0.5 * contravariantMetricTensor.Getg22() - * differential_operators.DDZ(covariantMetricTensor.Getg22()) + * differential_operators->DDZ(covariantMetricTensor.Getg22()) + 0.5 * contravariantMetricTensor.Getg23() - * differential_operators.DDY(covariantMetricTensor.Getg33()); + * differential_operators->DDY(covariantMetricTensor.Getg33()); G3_11 = 0.5 * contravariantMetricTensor.Getg13() - * differential_operators.DDX(covariantMetricTensor.Getg11()) + * differential_operators->DDX(covariantMetricTensor.Getg11()) + contravariantMetricTensor.Getg23() - * (differential_operators.DDX(covariantMetricTensor.Getg12()) - - 0.5 * differential_operators.DDY(covariantMetricTensor.Getg11())) + * (differential_operators->DDX(covariantMetricTensor.Getg12()) + - 0.5 * differential_operators->DDY(covariantMetricTensor.Getg11())) + contravariantMetricTensor.Getg33() - * (differential_operators.DDX(covariantMetricTensor.Getg13()) - - 0.5 * differential_operators.DDZ(covariantMetricTensor.Getg11())); + * (differential_operators->DDX(covariantMetricTensor.Getg13()) + - 0.5 * differential_operators->DDZ(covariantMetricTensor.Getg11())); G3_22 = contravariantMetricTensor.Getg13() - * (differential_operators.DDY(covariantMetricTensor.Getg12()) - - 0.5 * differential_operators.DDX(covariantMetricTensor.Getg22())) + * (differential_operators->DDY(covariantMetricTensor.Getg12()) + - 0.5 * differential_operators->DDX(covariantMetricTensor.Getg22())) + 0.5 * contravariantMetricTensor.Getg23() - * differential_operators.DDY(covariantMetricTensor.Getg22()) + * differential_operators->DDY(covariantMetricTensor.Getg22()) + contravariantMetricTensor.Getg33() - * (differential_operators.DDY(covariantMetricTensor.Getg23()) - - 0.5 * differential_operators.DDZ(covariantMetricTensor.Getg22())); + * (differential_operators->DDY(covariantMetricTensor.Getg23()) + - 0.5 * differential_operators->DDZ(covariantMetricTensor.Getg22())); G3_33 = contravariantMetricTensor.Getg13() - * (differential_operators.DDZ(covariantMetricTensor.Getg13()) - - 0.5 * differential_operators.DDX(covariantMetricTensor.Getg33())) + * (differential_operators->DDZ(covariantMetricTensor.Getg13()) + - 0.5 * differential_operators->DDX(covariantMetricTensor.Getg33())) + contravariantMetricTensor.Getg23() - * (differential_operators.DDZ(covariantMetricTensor.Getg23()) - - 0.5 * differential_operators.DDY(covariantMetricTensor.Getg33())) + * (differential_operators->DDZ(covariantMetricTensor.Getg23()) + - 0.5 * differential_operators->DDY(covariantMetricTensor.Getg33())) + 0.5 * contravariantMetricTensor.Getg33() - * differential_operators.DDZ(covariantMetricTensor.Getg33()); + * differential_operators->DDZ(covariantMetricTensor.Getg33()); G3_12 = - // 0.5 *g31*(differential_operators.DDY(covariantMetricTensor.Getg11()) + differential_operators.DDX(covariantMetricTensor.Getg12()) - differential_operators.DDX(covariantMetricTensor.Getg12())) + // 0.5 *g31*(differential_operators->DDY(covariantMetricTensor.Getg11()) + differential_operators->DDX(covariantMetricTensor.Getg12()) - differential_operators->DDX(covariantMetricTensor.Getg12())) // which equals to 0.5 * contravariantMetricTensor.Getg13() - * differential_operators.DDY(covariantMetricTensor.Getg11()) - // + 0.5 *g32*(differential_operators.DDY(covariantMetricTensor.Getg21()) + differential_operators.DDX(covariantMetricTensor.Getg22()) - differential_operators.DDY(covariantMetricTensor.Getg12())) + * differential_operators->DDY(covariantMetricTensor.Getg11()) + // + 0.5 *g32*(differential_operators->DDY(covariantMetricTensor.Getg21()) + differential_operators->DDX(covariantMetricTensor.Getg22()) - differential_operators->DDY(covariantMetricTensor.Getg12())) // which equals to + 0.5 * contravariantMetricTensor.Getg23() - * differential_operators.DDX(covariantMetricTensor.Getg22()) - //+ 0.5 *g33*(differential_operators.DDY(covariantMetricTensor.Getg31()) + differential_operators.DDX(covariantMetricTensor.Getg32()) - differential_operators.DDZ(covariantMetricTensor.Getg12())); + * differential_operators->DDX(covariantMetricTensor.Getg22()) + //+ 0.5 *g33*(differential_operators->DDY(covariantMetricTensor.Getg31()) + differential_operators->DDX(covariantMetricTensor.Getg32()) - differential_operators->DDZ(covariantMetricTensor.Getg12())); // which equals to + 0.5 * contravariantMetricTensor.Getg33() - * (differential_operators.DDY(covariantMetricTensor.Getg13()) - + differential_operators.DDX(covariantMetricTensor.Getg23()) - - differential_operators.DDZ(covariantMetricTensor.Getg12())); + * (differential_operators->DDY(covariantMetricTensor.Getg13()) + + differential_operators->DDX(covariantMetricTensor.Getg23()) + - differential_operators->DDZ(covariantMetricTensor.Getg12())); G3_13 = 0.5 * contravariantMetricTensor.Getg13() - * differential_operators.DDZ(covariantMetricTensor.Getg11()) + * differential_operators->DDZ(covariantMetricTensor.Getg11()) + 0.5 * contravariantMetricTensor.Getg23() - * (differential_operators.DDZ(covariantMetricTensor.Getg12()) - + differential_operators.DDX(covariantMetricTensor.Getg23()) - - differential_operators.DDY(covariantMetricTensor.Getg13())) + * (differential_operators->DDZ(covariantMetricTensor.Getg12()) + + differential_operators->DDX(covariantMetricTensor.Getg23()) + - differential_operators->DDY(covariantMetricTensor.Getg13())) + 0.5 * contravariantMetricTensor.Getg33() - * differential_operators.DDX(covariantMetricTensor.Getg33()); + * differential_operators->DDX(covariantMetricTensor.Getg33()); G3_23 = 0.5 * contravariantMetricTensor.Getg13() - * (differential_operators.DDZ(covariantMetricTensor.Getg12()) - + differential_operators.DDY(covariantMetricTensor.Getg13()) - - differential_operators.DDX(covariantMetricTensor.Getg23())) + * (differential_operators->DDZ(covariantMetricTensor.Getg12()) + + differential_operators->DDY(covariantMetricTensor.Getg13()) + - differential_operators->DDX(covariantMetricTensor.Getg23())) + 0.5 * contravariantMetricTensor.Getg23() - * differential_operators.DDZ(covariantMetricTensor.Getg22()) + * differential_operators->DDZ(covariantMetricTensor.Getg22()) + 0.5 * contravariantMetricTensor.Getg33() - * differential_operators.DDY(covariantMetricTensor.Getg33()); + * differential_operators->DDY(covariantMetricTensor.Getg33()); } void Geometry::calcCovariant(CELL_LOC cell_location, const std::string& region) { From 2939baa0013b957c7b9923a329f45272fd86628e Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 29 Nov 2023 11:53:13 +0000 Subject: [PATCH 207/491] Pass dx, dy, dz, location, and intShiftTorsion as parameters; dont' store them as fields on DifferentialOperators. More fixes. --- include/bout/coordinates.hxx | 54 +++---- include/bout/differential_operators.hxx | 87 +++++------ include/bout/geometry.hxx | 3 +- src/mesh/coordinates.cxx | 177 +++++++++------------ src/mesh/differential_operators.cxx | 139 +++++++---------- src/mesh/geometry.cxx | 198 ++++++++++++------------ 6 files changed, 303 insertions(+), 355 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index 54f8a82867..46cd821bf3 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -156,33 +156,33 @@ public: return *transform; } - // /////////////////////////////////////////////////////////// - // // Operators - // /////////////////////////////////////////////////////////// - // - // FieldMetric DDX(const Field2D& f, CELL_LOC outloc = CELL_DEFAULT, - // const std::string& method = "DEFAULT", - // const std::string& region = "RGN_NOBNDRY"); - // - // FieldMetric DDY(const Field2D& f, CELL_LOC outloc = CELL_DEFAULT, - // const std::string& method = "DEFAULT", - // const std::string& region = "RGN_NOBNDRY") const; - // - // FieldMetric DDZ(const Field2D& f, CELL_LOC outloc = CELL_DEFAULT, - // const std::string& method = "DEFAULT", - // const std::string& region = "RGN_NOBNDRY"); - // - // Field3D DDX(const Field3D& f, CELL_LOC outloc = CELL_DEFAULT, - // const std::string& method = "DEFAULT", - // const std::string& region = "RGN_NOBNDRY"); - // - // Field3D DDY(const Field3D& f, CELL_LOC outloc = CELL_DEFAULT, - // const std::string& method = "DEFAULT", - // const std::string& region = "RGN_NOBNDRY") const; - // - // Field3D DDZ(const Field3D& f, CELL_LOC outloc = CELL_DEFAULT, - // const std::string& method = "DEFAULT", - // const std::string& region = "RGN_NOBNDRY"); + /////////////////////////////////////////////////////////// + // Operators + /////////////////////////////////////////////////////////// + + FieldMetric DDX(const Field2D& f, CELL_LOC outloc = CELL_DEFAULT, + const std::string& method = "DEFAULT", + const std::string& region = "RGN_NOBNDRY"); + + FieldMetric DDY(const Field2D& f, CELL_LOC outloc = CELL_DEFAULT, + const std::string& method = "DEFAULT", + const std::string& region = "RGN_NOBNDRY") const; + + FieldMetric DDZ(const Field2D& f, CELL_LOC outloc = CELL_DEFAULT, + const std::string& method = "DEFAULT", + const std::string& region = "RGN_NOBNDRY"); + + Field3D DDX(const Field3D& f, CELL_LOC outloc = CELL_DEFAULT, + const std::string& method = "DEFAULT", + const std::string& region = "RGN_NOBNDRY"); + + Field3D DDY(const Field3D& f, CELL_LOC outloc = CELL_DEFAULT, + const std::string& method = "DEFAULT", + const std::string& region = "RGN_NOBNDRY") const; + + Field3D DDZ(const Field3D& f, CELL_LOC outloc = CELL_DEFAULT, + const std::string& method = "DEFAULT", + const std::string& region = "RGN_NOBNDRY"); /// Gradient along magnetic field b.Grad(f) Field2D Grad_par(const Field2D& var, CELL_LOC outloc = CELL_DEFAULT, diff --git a/include/bout/differential_operators.hxx b/include/bout/differential_operators.hxx index 067bd33d73..5795b4fc97 100644 --- a/include/bout/differential_operators.hxx +++ b/include/bout/differential_operators.hxx @@ -10,43 +10,43 @@ class DifferentialOperators { using FieldMetric = MetricTensor::FieldMetric; public: - - DifferentialOperators(Mesh* mesh, FieldMetric intShiftTorsion, CELL_LOC location, - FieldMetric dx, FieldMetric dy, FieldMetric dz); + DifferentialOperators(Mesh* mesh); // DifferentialOperators(DifferentialOperators operators, // DifferentialOperators::FieldMetric& dx); - FieldMetric DDX(const Field2D& f, CELL_LOC outloc = CELL_DEFAULT, - const std::string& method = "DEFAULT", - const std::string& region = "RGN_NOBNDRY"); + Field2D DDX(const Field2D& f, const Field2D& dx, CELL_LOC outloc = CELL_DEFAULT, + const std::string& method = "DEFAULT", + const std::string& region = "RGN_NOBNDRY"); - FieldMetric DDY(const Field2D& f, CELL_LOC outloc = CELL_DEFAULT, - const std::string& method = "DEFAULT", - const std::string& region = "RGN_NOBNDRY") const; + Field2D DDY(const Field2D& f, const Field2D& dy, CELL_LOC outloc = CELL_DEFAULT, + const std::string& method = "DEFAULT", + const std::string& region = "RGN_NOBNDRY") const; - FieldMetric DDZ(const Field2D& f, CELL_LOC outloc = CELL_DEFAULT, - const std::string& method = "DEFAULT", - const std::string& region = "RGN_NOBNDRY"); + Field2D DDZ(const Field2D& f, CELL_LOC outloc = CELL_DEFAULT, + const std::string& method = "DEFAULT", + const std::string& region = "RGN_NOBNDRY"); - Field3D DDX(const Field3D& f, CELL_LOC outloc = CELL_DEFAULT, + Field3D DDX(const Field3D& f, const Field3D& dx, const Field3D& dz, + FieldMetric& intShiftTorsion, CELL_LOC outloc = CELL_DEFAULT, const std::string& method = "DEFAULT", const std::string& region = "RGN_NOBNDRY"); - Field3D DDY(const Field3D& f, CELL_LOC outloc = CELL_DEFAULT, + Field3D DDY(const Field3D& f, const Field3D& dy, CELL_LOC outloc = CELL_DEFAULT, const std::string& method = "DEFAULT", const std::string& region = "RGN_NOBNDRY") const; - Field3D DDZ(const Field3D& f, CELL_LOC outloc = CELL_DEFAULT, + Field3D DDZ(const Field3D& f, const Field3D& dz, CELL_LOC outloc = CELL_DEFAULT, const std::string& method = "DEFAULT", const std::string& region = "RGN_NOBNDRY"); /// Gradient along magnetic field b.Grad(f) - FieldMetric Grad_par(const Field2D& var, const MetricTensor& covariantMetricTensor, - CELL_LOC outloc = CELL_DEFAULT, - const std::string& method = "DEFAULT"); + Field2D Grad_par(const Field2D& var, const Field2D& dy, + const MetricTensor& covariantMetricTensor, + CELL_LOC outloc = CELL_DEFAULT, const std::string& method = "DEFAULT"); - Field3D Grad_par(const Field3D& var, const MetricTensor& covariantMetricTensor, + Field3D Grad_par(const Field3D& var, const Field3D& dy, + const MetricTensor& covariantMetricTensor, CELL_LOC outloc = CELL_DEFAULT, const std::string& method = "DEFAULT"); /// Advection along magnetic field V*b.Grad(f) @@ -61,22 +61,21 @@ public: const std::string& method = "DEFAULT"); /// Divergence along magnetic field Div(b*f) = B.Grad(f/B) - FieldMetric Div_par(const Field2D& f, const Field2D& Bxy, - const MetricTensor& covariantMetricTensor, - CELL_LOC outloc = CELL_DEFAULT, - const std::string& method = "DEFAULT"); + Field2D Div_par(const Field2D& f, const Field2D& Bxy, const Field2D& dy, + const MetricTensor& covariantMetricTensor, + CELL_LOC outloc = CELL_DEFAULT, const std::string& method = "DEFAULT"); - Field3D Div_par(const Field3D& f, const Field2D& Bxy, + Field3D Div_par(const Field3D& f, const Field3D& Bxy, const Field3D& dy, const MetricTensor& covariantMetricTensor, CELL_LOC outloc = CELL_DEFAULT, const std::string& method = "DEFAULT"); // Second derivative along magnetic field - FieldMetric Grad2_par2(const Field2D& f, MetricTensor& covariantMetricTensor, - CELL_LOC outloc = CELL_DEFAULT, - const std::string& method = "DEFAULT"); + Field2D Grad2_par2(const Field2D& f, const Field2D& dy, + MetricTensor& covariantMetricTensor, CELL_LOC outloc = CELL_DEFAULT, + const std::string& method = "DEFAULT"); - Field3D Grad2_par2(const Field3D& f, MetricTensor& covariantMetricTensor, - CELL_LOC outloc = CELL_DEFAULT, + Field3D Grad2_par2(const Field3D& f, const FieldMetric& dy, + MetricTensor& covariantMetricTensor, CELL_LOC outloc = CELL_DEFAULT, const std::string& method = "DEFAULT"); // // Perpendicular Laplacian operator, using only X-Z derivatives @@ -92,18 +91,18 @@ public: // Full parallel Laplacian operator on scalar field // Laplace_par(f) = Div( b (b dot Grad(f)) ) - FieldMetric Laplace_par(const Field2D& f, const Field2D& g22, const Field2D& J, - CELL_LOC outloc = CELL_DEFAULT); + Field2D Laplace_par(const Field2D& f, const Field2D& g22, const Field2D& J, + const Field2D& dy, CELL_LOC outloc = CELL_DEFAULT); Field3D Laplace_par(const Field3D& f, const Field3D& g22, const Field3D& J, - CELL_LOC outloc = CELL_DEFAULT); + const Field2D& dy, CELL_LOC outloc = CELL_DEFAULT); // Full Laplacian operator on scalar field - FieldMetric Laplace(const Field2D& f, MetricTensor& covariantMetricTensor, - const Field2D& G1, const Field2D& G2, const Field2D& G3, - CELL_LOC outloc = CELL_DEFAULT, - const std::string& dfdy_boundary_conditions = "free_o3", - const std::string& dfdy_dy_region = ""); + Field2D Laplace(const Field2D& f, MetricTensor& covariantMetricTensor, + const Field2D& dy, const Field2D& G1, const Field2D& G2, + const Field2D& G3, CELL_LOC outloc = CELL_DEFAULT, + const std::string& dfdy_boundary_conditions = "free_o3", + const std::string& dfdy_dy_region = ""); Field3D Laplace(const Field3D& f, MetricTensor& covariantMetricTensor, const Field3D& G1, const Field3D& G2, const Field3D& G3, @@ -114,21 +113,17 @@ public: // Full perpendicular Laplacian, in form of inverse of Laplacian operator in LaplaceXY // solver Field2D Laplace_perpXY(const Field2D& A, const Field2D& f, - MetricTensor& covariantMetricTensor, const Field2D& J); + MetricTensor& covariantMetricTensor, const Field2D& J, + const Field2D& dx, const Field2D& dy); void invalidateAndRecalculateCachedVariables(); - const FieldMetric& Grad2_par2_DDY_invSg(const MetricTensor& covariantMetricTensor, - CELL_LOC outloc, - const std::string& method) const; + const Field2D& Grad2_par2_DDY_invSg(const MetricTensor& covariantMetricTensor, + const FieldMetric& dy, CELL_LOC outloc, + const std::string& method) const; private: Mesh* mesh; - FieldMetric intShiftTorsion; - CELL_LOC location; - FieldMetric dx; - FieldMetric dy; - FieldMetric dz; /// Cache variable for Grad2_par2 mutable std::map> Grad2_par2_DDY_invSgCache; diff --git a/include/bout/geometry.hxx b/include/bout/geometry.hxx index 9b73aa2907..84c4a3f0ab 100644 --- a/include/bout/geometry.hxx +++ b/include/bout/geometry.hxx @@ -106,7 +106,8 @@ public: // void jacobian(bool extrapolate_x, bool extrapolate_y); ///< Calculate J and Bxy - void CalculateChristoffelSymbols(); /// Calculate Christoffel symbol terms + /// Calculate Christoffel symbol terms + void CalculateChristoffelSymbols(FieldMetric& dx, FieldMetric& dy); // check that covariant tensors are positive (if expected) and finite (always) void checkCovariant(int ystart); diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index d4b4e63a9b..e2112bf57b 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -293,9 +293,7 @@ Coordinates::Coordinates(Mesh* mesh, FieldMetric dx, FieldMetric dy, FieldMetric FieldMetric IntShiftTorsion) : dx(std::move(dx)), dy(std::move(dy)), dz(dz), ShiftTorsion(std::move(ShiftTorsion)), IntShiftTorsion(std::move(IntShiftTorsion)), nz(mesh->LocalNz), localmesh(mesh), - location(CELL_CENTRE), - differential_operators(DifferentialOperators(mesh, this->IntShiftTorsion, location, - this->dx, this->dy, dz)), + location(CELL_CENTRE), differential_operators(DifferentialOperators(mesh)), geometry(Geometry(std::move(J), std::move(Bxy), std::move(g11), std::move(g22), std::move(g33), std::move(g12), std::move(g13), std::move(g23), std::move(g_11), std::move(g_22), std::move(g_33), @@ -310,8 +308,7 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, // G3_11(mesh), G3_22(mesh), G3_33(mesh), G3_12(mesh), G3_13(mesh), G3_23(mesh), // G1(mesh), G2(mesh), G3(mesh), ShiftTorsion(mesh), IntShiftTorsion(mesh), localmesh(mesh), location(loc), - differential_operators( - DifferentialOperators(mesh, IntShiftTorsion, loc, dx, dy, dz)), + differential_operators(DifferentialOperators(mesh)), geometry(Geometry(mesh, &differential_operators)) { if (options == nullptr) { @@ -679,7 +676,7 @@ int Coordinates::calculateGeometry(bool recalculate_staggered, // Check input metrics checkContravariant(); checkCovariant(); - geometry.CalculateChristoffelSymbols(); + geometry.CalculateChristoffelSymbols(dx, dy); auto tmp = J() * g12(); communicate(tmp); @@ -1187,59 +1184,52 @@ void Coordinates::setParallelTransform(Options* options) { * *******************************************************************************/ -//Coordinates::FieldMetric Coordinates::DDX(const Field2D& f, CELL_LOC loc, -// const std::string& method, -// const std::string& region) { -// ASSERT1(location == loc || loc == CELL_DEFAULT) -// return bout::derivatives::index::DDX(f, loc, method, region) / dx; -//} -//Field3D Coordinates::DDX(const Field3D& f, CELL_LOC outloc, const std::string& method, -// const std::string& region) { -// auto result = bout::derivatives::index::DDX(f, outloc, method, region); -// result /= dx; -// -// if (f.getMesh()->IncIntShear) { -// // Using BOUT-06 style shifting -// result += IntShiftTorsion * DDZ(f, outloc, method, region); -// } -// -// return result; -//} -// -//Coordinates::FieldMetric Coordinates::DDY(const Field2D& f, CELL_LOC loc, -// const std::string& method, -// const std::string& region) const { -// ASSERT1(location == loc || loc == CELL_DEFAULT) -// return bout::derivatives::index::DDY(f, loc, method, region) / dy; -//} -// -//Field3D Coordinates::DDY(const Field3D& f, CELL_LOC outloc, const std::string& method, -// const std::string& region) const { -//#if BOUT_USE_METRIC_3D -// if (!f.hasParallelSlices() and !transform->canToFromFieldAligned()) { -// Field3D f_parallel = f; -// transform->calcParallelSlices(f_parallel); -// f_parallel.applyParallelBoundary("parallel_neumann"); -// return bout::derivatives::index::DDY(f_parallel, outloc, method, region); -// } -//#endif -// return bout::derivatives::index::DDY(f, outloc, method, region) / dy; -//} -// -//Coordinates::FieldMetric Coordinates::DDZ(const Field2D& f, CELL_LOC loc, -// const std::string& UNUSED(method), -// const std::string& UNUSED(region)) { -// ASSERT1(location == loc || loc == CELL_DEFAULT) -// ASSERT1(f.getMesh() == localmesh) -// if (loc == CELL_DEFAULT) { -// loc = f.getLocation(); -// } -// return zeroFrom(f).setLocation(loc); -//} -//Field3D Coordinates::DDZ(const Field3D& f, CELL_LOC outloc, const std::string& method, -// const std::string& region) { -// return bout::derivatives::index::DDZ(f, outloc, method, region) / dz; -//} +Coordinates::FieldMetric Coordinates::DDX(const Field2D& f, CELL_LOC loc, + const std::string& method, + const std::string& region) { + ASSERT1(location == loc || loc == CELL_DEFAULT) + return differential_operators.DDX(f, dx, loc, method, region); +} + +Field3D Coordinates::DDX(const Field3D& f, CELL_LOC outloc, const std::string& method, + const std::string& region) { + return differential_operators.DDX(f, dx, dz, IntShiftTorsion, outloc, method, region); +} + +Coordinates::FieldMetric Coordinates::DDY(const Field2D& f, CELL_LOC loc, + const std::string& method, + const std::string& region) const { + ASSERT1(location == loc || loc == CELL_DEFAULT) + return differential_operators.DDY(f, dy, loc, method, region); +} + +Field3D Coordinates::DDY(const Field3D& f, CELL_LOC outloc, const std::string& method, + const std::string& region) const { +#if BOUT_USE_METRIC_3D + if (!f.hasParallelSlices() and !transform->canToFromFieldAligned()) { + Field3D f_parallel = f; + transform->calcParallelSlices(f_parallel); + f_parallel.applyParallelBoundary("parallel_neumann"); + return bout::derivatives::index::DDY(f_parallel, outloc, method, region); + } +#endif + return differential_operators.DDY(f, dy, outloc, method, region); +} + +Coordinates::FieldMetric Coordinates::DDZ(const Field2D& f, CELL_LOC loc, + const std::string& UNUSED(method), + const std::string& UNUSED(region)) { + ASSERT1(location == loc || loc == CELL_DEFAULT) + ASSERT1(f.getMesh() == localmesh) + if (loc == CELL_DEFAULT) { + loc = f.getLocation(); + } + return zeroFrom(f).setLocation(loc); +} +Field3D Coordinates::DDZ(const Field3D& f, CELL_LOC outloc, const std::string& method, + const std::string& region) { + return bout::derivatives::index::DDZ(f, outloc, method, region) / dz; +} ///////////////////////////////////////////////////////// // Parallel gradient @@ -1248,7 +1238,7 @@ Field2D Coordinates::Grad_par(const Field2D& var, [[maybe_unused]] CELL_LOC outl const std::string& UNUSED(method)) { TRACE("Coordinates::Grad_par( Field2D )"); - return differential_operators.Grad_par(var, geometry.getCovariantMetricTensor()); + return differential_operators.Grad_par(var, dy, geometry.getCovariantMetricTensor()); } Field3D Coordinates::Grad_par(const Field3D& var, CELL_LOC outloc, @@ -1256,8 +1246,8 @@ Field3D Coordinates::Grad_par(const Field3D& var, CELL_LOC outloc, TRACE("Coordinates::Grad_par( Field3D )"); ASSERT1(location == outloc || outloc == CELL_DEFAULT) - return differential_operators.Grad_par(var, geometry.getCovariantMetricTensor(), outloc, - method); + return differential_operators.Grad_par(var, dy, geometry.getCovariantMetricTensor(), + outloc, method); } ///////////////////////////////////////////////////////// @@ -1290,7 +1280,7 @@ Coordinates::FieldMetric Coordinates::Div_par(const Field2D& f, CELL_LOC outloc, ASSERT1(location == outloc || outloc == CELL_DEFAULT) return differential_operators.Div_par( - f, geometry.Bxy(), geometry.getCovariantMetricTensor(), outloc, method); + f, geometry.Bxy(), dy, geometry.getCovariantMetricTensor(), outloc, method); } Field3D Coordinates::Div_par(const Field3D& f, CELL_LOC outloc, @@ -1306,7 +1296,7 @@ Field3D Coordinates::Div_par(const Field3D& f, CELL_LOC outloc, // No yup/ydown fields. The Grad_par operator will // shift to field aligned coordinates return differential_operators.Div_par( - f, geometry.Bxy(), geometry.getCovariantMetricTensor(), outloc, method); + f, dy, geometry.Bxy(), geometry.getCovariantMetricTensor(), outloc, method); } // Need to modify yup and ydown fields @@ -1328,34 +1318,16 @@ Coordinates::FieldMetric Coordinates::Grad2_par2(const Field2D& f, CELL_LOC outl TRACE("Coordinates::Grad2_par2( Field2D )"); ASSERT1(location == outloc || (outloc == CELL_DEFAULT && location == f.getLocation())) - auto result = differential_operators.Grad2_par2_DDY_invSg( - geometry.getCovariantMetricTensor(), outloc, method) - * DDY(f, outloc, method) - + D2DY2(f, outloc, method) / g22(); - - return result; + return differential_operators.Grad2_par2_DDY_invSg(geometry.getCovariantMetricTensor(), + dy, outloc, method); } Field3D Coordinates::Grad2_par2(const Field3D& f, CELL_LOC outloc, const std::string& method) { TRACE("Coordinates::Grad2_par2( Field3D )"); - if (outloc == CELL_DEFAULT) { - outloc = f.getLocation(); - } - ASSERT1(location == outloc) - - Field3D result = ::DDY(f, outloc, method); - - Field3D r2 = D2DY2(f, outloc, method) / g22(); - result = differential_operators.Grad2_par2_DDY_invSg( - geometry.getCovariantMetricTensor(), outloc, method) - * result - + r2; - - ASSERT2(result.getLocation() == outloc) - - return result; + return differential_operators.Grad2_par2_DDY_invSg(geometry.getCovariantMetricTensor(), + dy, outloc, method); } ///////////////////////////////////////////////////////// @@ -1369,9 +1341,7 @@ Coordinates::FieldMetric Coordinates::Delp2(const Field2D& f, CELL_LOC outloc, TRACE("Coordinates::Delp2( Field2D )"); ASSERT1(location == outloc || outloc == CELL_DEFAULT) - auto result = G1 * DDX(f, outloc) + g11() * D2DX2(f, outloc); - - return result; + return G1 * DDX(f, outloc) + g11() * D2DX2(f, outloc); } Field3D Coordinates::Delp2(const Field3D& f, CELL_LOC outloc, bool useFFT) { @@ -1522,13 +1492,11 @@ Coordinates::FieldMetric Coordinates::Laplace(const Field2D& f, CELL_LOC outloc, TRACE("Coordinates::Laplace( Field2D )"); ASSERT1(location == outloc || outloc == CELL_DEFAULT) - auto result = G1 * DDX(f, outloc) + G2 * DDY(f, outloc) + g11() * D2DX2(f, outloc) - + g22() * D2DY2(f, outloc) - + 2.0 * g12() - * D2DXDY(f, outloc, "DEFAULT", "RGN_NOBNDRY", - dfdy_boundary_conditions, dfdy_dy_region); - - return result; + return G1 * DDX(f, outloc) + G2 * DDY(f, outloc) + g11() * D2DX2(f, outloc) + + g22() * D2DY2(f, outloc) + + 2.0 * g12() + * D2DXDY(f, outloc, "DEFAULT", "RGN_NOBNDRY", dfdy_boundary_conditions, + dfdy_dy_region); } Field3D Coordinates::Laplace(const Field3D& f, CELL_LOC outloc, @@ -1537,16 +1505,13 @@ Field3D Coordinates::Laplace(const Field3D& f, CELL_LOC outloc, TRACE("Coordinates::Laplace( Field3D )"); ASSERT1(location == outloc || outloc == CELL_DEFAULT) - Field3D result = G1 * ::DDX(f, outloc) + G2 * ::DDY(f, outloc) + G3 * ::DDZ(f, outloc) - + g11() * D2DX2(f, outloc) + g22() * D2DY2(f, outloc) - + g33() * D2DZ2(f, outloc) - + 2.0 - * (g12() - * D2DXDY(f, outloc, "DEFAULT", "RGN_NOBNDRY", - dfdy_boundary_conditions, dfdy_dy_region) - + g13() * D2DXDZ(f, outloc) + g23() * D2DYDZ(f, outloc)); - - return result; + return G1 * ::DDX(f, outloc) + G2 * ::DDY(f, outloc) + G3 * ::DDZ(f, outloc) + + g11() * D2DX2(f, outloc) + g22() * D2DY2(f, outloc) + g33() * D2DZ2(f, outloc) + + 2.0 + * (g12() + * D2DXDY(f, outloc, "DEFAULT", "RGN_NOBNDRY", + dfdy_boundary_conditions, dfdy_dy_region) + + g13() * D2DXDZ(f, outloc) + g23() * D2DYDZ(f, outloc)); } // Full perpendicular Laplacian, in form of inverse of Laplacian operator in LaplaceXY diff --git a/src/mesh/differential_operators.cxx b/src/mesh/differential_operators.cxx index 11854255f7..1c02c5ab90 100644 --- a/src/mesh/differential_operators.cxx +++ b/src/mesh/differential_operators.cxx @@ -8,42 +8,34 @@ //#include "bout/paralleltransform.hxx" #include -DifferentialOperators::DifferentialOperators(const DifferentialOperators& operators) {} - -DifferentialOperators::DifferentialOperators(Mesh* mesh, FieldMetric intShiftTorsion, - const CELL_LOC location, FieldMetric dx, - FieldMetric dy, FieldMetric dz) - : mesh(mesh), intShiftTorsion(intShiftTorsion), location(location), dx(dx), dy(dy), - dz(dz) {} - -MetricTensor::FieldMetric DifferentialOperators::DDX(const Field2D& f, CELL_LOC loc, - const std::string& method, - const std::string& region) { - ASSERT1(location == loc || loc == CELL_DEFAULT) +DifferentialOperators::DifferentialOperators(Mesh* mesh) : mesh(mesh) {} + +Field2D DifferentialOperators::DDX(const Field2D& f, const Field2D& dx, CELL_LOC loc, + const std::string& method, const std::string& region) { return bout::derivatives::index::DDX(f, loc, method, region) / dx; } -Field3D DifferentialOperators::DDX(const Field3D& f, CELL_LOC outloc, +Field3D DifferentialOperators::DDX(const Field3D& f, const Field3D& dx, const Field3D& dz, + FieldMetric& intShiftTorsion, CELL_LOC outloc, const std::string& method, const std::string& region) { auto result = bout::derivatives::index::DDX(f, outloc, method, region); result /= dx; if (f.getMesh()->IncIntShear) { // Using BOUT-06 style shifting - result += intShiftTorsion * DDZ(f, outloc, method, region); + result += intShiftTorsion * DDZ(f, dz, outloc, method, region); } return result; } -FieldMetric DifferentialOperators::DDY(const Field2D& f, CELL_LOC loc, - const std::string& method, - const std::string& region) const { - ASSERT1(location == loc || loc == CELL_DEFAULT) +Field2D DifferentialOperators::DDY(const Field2D& f, const Field2D& dy, CELL_LOC loc, + const std::string& method, + const std::string& region) const { return bout::derivatives::index::DDY(f, loc, method, region) / dy; } -Field3D DifferentialOperators::DDY(const Field3D& f, CELL_LOC outloc, +Field3D DifferentialOperators::DDY(const Field3D& f, const Field3D& dy, CELL_LOC outloc, const std::string& method, const std::string& region) const { #if BOUT_USE_METRIC_3D @@ -57,17 +49,16 @@ Field3D DifferentialOperators::DDY(const Field3D& f, CELL_LOC outloc, return bout::derivatives::index::DDY(f, outloc, method, region) / dy; } -FieldMetric DifferentialOperators::DDZ(const Field2D& f, CELL_LOC loc, - const std::string& UNUSED(method), - const std::string& UNUSED(region)) { - ASSERT1(location == loc || loc == CELL_DEFAULT) - ASSERT1(f.getMesh() == mesh) +Field2D DifferentialOperators::DDZ(const Field2D& f, CELL_LOC loc, + const std::string& UNUSED(method), + const std::string& UNUSED(region)) { if (loc == CELL_DEFAULT) { loc = f.getLocation(); } return zeroFrom(f).setLocation(loc); } -Field3D DifferentialOperators::DDZ(const Field3D& f, CELL_LOC outloc, + +Field3D DifferentialOperators::DDZ(const Field3D& f, const Field3D& dz, CELL_LOC outloc, const std::string& method, const std::string& region) { return bout::derivatives::index::DDZ(f, outloc, method, region) / dz; } @@ -75,21 +66,21 @@ Field3D DifferentialOperators::DDZ(const Field3D& f, CELL_LOC outloc, ///////////////////////////////////////////////////////// // Parallel gradient -FieldMetric DifferentialOperators::Grad_par(const Field2D& var, - const MetricTensor& covariantMetricTensor, - [[maybe_unused]] CELL_LOC outloc, - const std::string& UNUSED(method)) { +Field2D DifferentialOperators::Grad_par(const Field2D& var, const Field2D& dy, + const MetricTensor& covariantMetricTensor, + [[maybe_unused]] CELL_LOC outloc, + const std::string& UNUSED(method)) { TRACE("DifferentialOperators::Grad_par( Field2D )"); - return DDY(var) * invSg(covariantMetricTensor); + return DDY(var, dy) * invSg(covariantMetricTensor); } -Field3D DifferentialOperators::Grad_par(const Field3D& var, +Field3D DifferentialOperators::Grad_par(const Field3D& var, const Field3D& dy, const MetricTensor& covariantMetricTensor, CELL_LOC outloc, const std::string& method) { TRACE("DifferentialOperators::Grad_par( Field3D )"); - return DDY(var, outloc, method) * invSg(covariantMetricTensor); + return DDY(var, dy, outloc, method) * invSg(covariantMetricTensor); } ///////////////////////////////////////////////////////// @@ -113,23 +104,24 @@ Field3D DifferentialOperators::Vpar_Grad_par(const Field3D& v, const Field3D& f, ///////////////////////////////////////////////////////// // Parallel divergence -FieldMetric DifferentialOperators::Div_par(const Field2D& f, const Field2D& Bxy, - const MetricTensor& covariantMetricTensor, - CELL_LOC outloc, const std::string& method) { +Field2D DifferentialOperators::Div_par(const Field2D& f, const Field2D& Bxy, + const Field2D& dy, + const MetricTensor& covariantMetricTensor, + CELL_LOC outloc, const std::string& method) { TRACE("DifferentialOperators::Div_par( Field2D )"); // Need Bxy at location of f, which might be different from location of this // Coordinates object auto Bxy_floc = f.getCoordinates()->Bxy(); - return Bxy * Grad_par(f / Bxy_floc, covariantMetricTensor, outloc, method); + return Bxy * Grad_par(f / Bxy_floc, dy, covariantMetricTensor, outloc, method); } -Field3D DifferentialOperators::Div_par(const Field3D& f, const Field2D& Bxy, +Field3D DifferentialOperators::Div_par(const Field3D& f, const Field3D& Bxy, + const Field3D& dy, const MetricTensor& covariantMetricTensor, CELL_LOC outloc, const std::string& method) { TRACE("DifferentialOperators::Div_par( Field3D )"); - ASSERT1(location == outloc || outloc == CELL_DEFAULT) // Need Bxy at location of f, which might be different from location of this // Coordinates object @@ -138,7 +130,7 @@ Field3D DifferentialOperators::Div_par(const Field3D& f, const Field2D& Bxy, if (!f.hasParallelSlices()) { // No yup/ydown fields. The Grad_par operator will // shift to field aligned coordinates - return Bxy * Grad_par(f / Bxy_floc, covariantMetricTensor, outloc, method); + return Bxy * Grad_par(f / Bxy_floc, dy, covariantMetricTensor, outloc, method); } // Need to modify yup and ydown fields @@ -148,41 +140,36 @@ Field3D DifferentialOperators::Div_par(const Field3D& f, const Field2D& Bxy, f_B.yup(i) = f.yup(i) / Bxy_floc.yup(i); f_B.ydown(i) = f.ydown(i) / Bxy_floc.ydown(i); } - return Bxy * Grad_par(f_B, covariantMetricTensor, outloc, method); + return Bxy * Grad_par(f_B, dy, covariantMetricTensor, outloc, method); } ///////////////////////////////////////////////////////// // second parallel derivative (b dot Grad)(b dot Grad) // Note: For parallel Laplacian use Laplace_par -FieldMetric DifferentialOperators::Grad2_par2(const Field2D& f, - MetricTensor& covariantMetricTensor, - CELL_LOC outloc, - const std::string& method) { +Field2D DifferentialOperators::Grad2_par2(const Field2D& f, const Field2D& dy, + MetricTensor& covariantMetricTensor, + CELL_LOC outloc, const std::string& method) { TRACE("DifferentialOperators::Grad2_par2( Field2D )"); - ASSERT1(location == outloc || (outloc == CELL_DEFAULT && location == f.getLocation())) - - auto result = - Grad2_par2_DDY_invSg(covariantMetricTensor, outloc, method) * DDY(f, outloc, method) - + D2DY2(f, outloc, method) / covariantMetricTensor.Getg22(); - return result; + return Grad2_par2_DDY_invSg(covariantMetricTensor, dy, outloc, method) + * DDY(f, dy, outloc, method) + + D2DY2(f, outloc, method) / covariantMetricTensor.Getg22(); } -Field3D DifferentialOperators::Grad2_par2(const Field3D& f, +Field3D DifferentialOperators::Grad2_par2(const Field3D& f, const FieldMetric& dy, MetricTensor& covariantMetricTensor, CELL_LOC outloc, const std::string& method) { TRACE("DifferentialOperators::Grad2_par2( Field3D )"); if (outloc == CELL_DEFAULT) { outloc = f.getLocation(); } - ASSERT1(location == outloc) Field3D result = ::DDY(f, outloc, method); Field3D r2 = D2DY2(f, outloc, method) / covariantMetricTensor.Getg22(); - result = Grad2_par2_DDY_invSg(covariantMetricTensor, outloc, method) * result + r2; + result = Grad2_par2_DDY_invSg(covariantMetricTensor, dy, outloc, method) * result + r2; ASSERT2(result.getLocation() == outloc) @@ -340,37 +327,31 @@ Field3D DifferentialOperators::Grad2_par2(const Field3D& f, //} FieldMetric DifferentialOperators::Laplace_par(const Field2D& f, const Field2D& g22, - const Field2D& J, CELL_LOC outloc) { - ASSERT1(location == outloc || outloc == CELL_DEFAULT) - - return D2DY2(f, outloc) / g22 + DDY(J / g22, outloc) * DDY(f, outloc) / J; + const Field2D& J, const Field2D& dy, + CELL_LOC outloc) { + return D2DY2(f, outloc) / g22 + DDY(J / g22, dy, outloc) * DDY(f, dy, outloc) / J; } Field3D DifferentialOperators::Laplace_par(const Field3D& f, const Field3D& g22, - const Field3D& J, CELL_LOC outloc) { - ASSERT1(location == outloc || outloc == CELL_DEFAULT) - return D2DY2(f, outloc) / g22 + DDY(J / g22, outloc) * ::DDY(f, outloc) / J; + const Field3D& J, const Field2D& dy, + CELL_LOC outloc) { + return D2DY2(f, outloc) / g22 + DDY(J / g22, dy, outloc) * ::DDY(f, outloc) / J; } // Full Laplacian operator on scalar field -FieldMetric DifferentialOperators::Laplace(const Field2D& f, - MetricTensor& covariantMetricTensor, - const Field2D& G1, const Field2D& G2, - const Field2D& G3, CELL_LOC outloc, - const std::string& dfdy_boundary_conditions, - const std::string& dfdy_dy_region) { +FieldMetric DifferentialOperators::Laplace( + const Field2D& f, MetricTensor& covariantMetricTensor, const Field2D& dy, + const Field2D& G1, const Field2D& G2, const Field2D& G3, CELL_LOC outloc, + const std::string& dfdy_boundary_conditions, const std::string& dfdy_dy_region) { TRACE("DifferentialOperators::Laplace( Field2D )"); - ASSERT1(location == outloc || outloc == CELL_DEFAULT) - auto result = G1 * DDX(f, outloc) + G2 * DDY(f, outloc) - + covariantMetricTensor.Getg11() * D2DX2(f, outloc) - + covariantMetricTensor.Getg22() * D2DY2(f, outloc) - + 2.0 * covariantMetricTensor.Getg12() - * D2DXDY(f, outloc, "DEFAULT", "RGN_NOBNDRY", - dfdy_boundary_conditions, dfdy_dy_region); - - return result; + return G1 * DDX(f, dy, outloc) + G2 * DDY(f, dy, outloc) + + covariantMetricTensor.Getg11() * D2DX2(f, outloc) + + covariantMetricTensor.Getg22() * D2DY2(f, outloc) + + 2.0 * covariantMetricTensor.Getg12() + * D2DXDY(f, outloc, "DEFAULT", "RGN_NOBNDRY", dfdy_boundary_conditions, + dfdy_dy_region); } Field3D DifferentialOperators::Laplace(const Field3D& f, @@ -380,7 +361,6 @@ Field3D DifferentialOperators::Laplace(const Field3D& f, const std::string& dfdy_boundary_conditions, const std::string& dfdy_dy_region) { TRACE("DifferentialOperators::Laplace( Field3D )"); - ASSERT1(location == outloc || outloc == CELL_DEFAULT) Field3D result = G1 * ::DDX(f, outloc) + G2 * ::DDY(f, outloc) + G3 * ::DDZ(f, outloc) + covariantMetricTensor.Getg11() * D2DX2(f, outloc) @@ -401,7 +381,8 @@ Field3D DifferentialOperators::Laplace(const Field3D& f, Field2D DifferentialOperators::Laplace_perpXY([[maybe_unused]] const Field2D& A, [[maybe_unused]] const Field2D& f), MetricTensor& covariantMetricTensor, - const Field2D& J) { + const Field2D& J, const Field2D& dx, + const Field2D& dy) { TRACE("DifferentialOperators::Laplace_perpXY( Field2D )"); #if not(BOUT_USE_METRIC_3D) Field2D result; @@ -473,7 +454,7 @@ DifferentialOperators::invSg(const MetricTensor& covariantMetricTensor) const { const FieldMetric& DifferentialOperators::Grad2_par2_DDY_invSg(const MetricTensor& covariantMetricTensor, - CELL_LOC outloc, + const FieldMetric& dy, CELL_LOC outloc, const std::string& method) const { if (auto search = Grad2_par2_DDY_invSgCache.find(method); search != Grad2_par2_DDY_invSgCache.end()) { @@ -487,7 +468,7 @@ DifferentialOperators::Grad2_par2_DDY_invSg(const MetricTensor& covariantMetricT // cache auto ptr = std::make_unique(); - *ptr = DDY(*invSgCache, outloc, method) * invSg(covariantMetricTensor); + *ptr = DDY(*invSgCache, dy, outloc, method) * invSg(covariantMetricTensor); Grad2_par2_DDY_invSgCache[method] = std::move(ptr); return *Grad2_par2_DDY_invSgCache[method]; } diff --git a/src/mesh/geometry.cxx b/src/mesh/geometry.cxx index 8ce0e26d5c..0cbb93ef3b 100644 --- a/src/mesh/geometry.cxx +++ b/src/mesh/geometry.cxx @@ -695,178 +695,184 @@ Geometry::Geometry(Mesh* mesh, DifferentialOperators* differential_operators) // // return 0; // //} -void Geometry::CalculateChristoffelSymbols() { +void Geometry::CalculateChristoffelSymbols(FieldMetric& dx, FieldMetric& dy) { // Calculate Christoffel symbol terms (18 independent values) // Note: This calculation is completely general: metric // tensor can be 2D or 3D. For 2D, all DDZ terms are zero - G1_11 = 0.5 * contravariantMetricTensor.Getg11() - * differential_operators->DDX(covariantMetricTensor.Getg11(), dx) - + contravariantMetricTensor.Getg12() - * (differential_operators->DDX(covariantMetricTensor.Getg12()) - - 0.5 * differential_operators->DDY(covariantMetricTensor.Getg11())) - + contravariantMetricTensor.Getg13() - * (differential_operators->DDX(covariantMetricTensor.Getg13()) - - 0.5 * differential_operators->DDZ(covariantMetricTensor.Getg11())); + G1_11 = + 0.5 * contravariantMetricTensor.Getg11() + * differential_operators->DDX(covariantMetricTensor.Getg11(), dx) + + contravariantMetricTensor.Getg12() + * (differential_operators->DDX(covariantMetricTensor.Getg12(), dx) + - 0.5 * differential_operators->DDY(covariantMetricTensor.Getg11(), dy)) + + contravariantMetricTensor.Getg13() + * (differential_operators->DDX(covariantMetricTensor.Getg13(), dx) + - 0.5 * differential_operators->DDZ(covariantMetricTensor.Getg11())); G1_22 = contravariantMetricTensor.Getg11() - * (differential_operators->DDY(covariantMetricTensor.Getg12()) - - 0.5 * differential_operators->DDX(covariantMetricTensor.Getg22())) + * (differential_operators->DDY(covariantMetricTensor.Getg12(), dy) + - 0.5 * differential_operators->DDX(covariantMetricTensor.Getg22(), dx)) + 0.5 * contravariantMetricTensor.Getg12() - * differential_operators->DDY(covariantMetricTensor.Getg22()) + * differential_operators->DDY(covariantMetricTensor.Getg22(), dy) + contravariantMetricTensor.Getg13() - * (differential_operators->DDY(covariantMetricTensor.Getg23()) + * (differential_operators->DDY(covariantMetricTensor.Getg23(), dy) - 0.5 * differential_operators->DDZ(covariantMetricTensor.Getg22())); - G1_33 = contravariantMetricTensor.Getg11() - * (differential_operators->DDZ(covariantMetricTensor.Getg13()) - - 0.5 * differential_operators->DDX(covariantMetricTensor.Getg33())) - + contravariantMetricTensor.Getg12() - * (differential_operators->DDZ(covariantMetricTensor.Getg23()) - - 0.5 * differential_operators->DDY(covariantMetricTensor.Getg33())) - + 0.5 * contravariantMetricTensor.Getg13() - * differential_operators->DDZ(covariantMetricTensor.Getg33()); + G1_33 = + contravariantMetricTensor.Getg11() + * (differential_operators->DDZ(covariantMetricTensor.Getg13()) + - 0.5 * differential_operators->DDX(covariantMetricTensor.Getg33(), dx)) + + contravariantMetricTensor.Getg12() + * (differential_operators->DDZ(covariantMetricTensor.Getg23()) + - 0.5 * differential_operators->DDY(covariantMetricTensor.Getg33(), dy)) + + 0.5 * contravariantMetricTensor.Getg13() + * differential_operators->DDZ(covariantMetricTensor.Getg33()); G1_12 = 0.5 * contravariantMetricTensor.Getg11() - * differential_operators->DDY(covariantMetricTensor.Getg11()) + * differential_operators->DDY(covariantMetricTensor.Getg11(), dy) + 0.5 * contravariantMetricTensor.Getg12() - * differential_operators->DDX(covariantMetricTensor.Getg22()) + * differential_operators->DDX(covariantMetricTensor.Getg22(), dx) + 0.5 * contravariantMetricTensor.Getg13() - * (differential_operators->DDY(covariantMetricTensor.Getg13()) - + differential_operators->DDX(covariantMetricTensor.Getg23()) + * (differential_operators->DDY(covariantMetricTensor.Getg13(), dy) + + differential_operators->DDX(covariantMetricTensor.Getg23(), dx) - differential_operators->DDZ(covariantMetricTensor.Getg12())); G1_13 = 0.5 * contravariantMetricTensor.Getg11() * differential_operators->DDZ(covariantMetricTensor.Getg11()) + 0.5 * contravariantMetricTensor.Getg12() * (differential_operators->DDZ(covariantMetricTensor.Getg12()) - + differential_operators->DDX(covariantMetricTensor.Getg23()) - - differential_operators->DDY(covariantMetricTensor.Getg13())) + + differential_operators->DDX(covariantMetricTensor.Getg23(), dx) + - differential_operators->DDY(covariantMetricTensor.Getg13(), dy)) + 0.5 * contravariantMetricTensor.Getg13() - * differential_operators->DDX(covariantMetricTensor.Getg33()); + * differential_operators->DDX(covariantMetricTensor.Getg33(), dx); G1_23 = 0.5 * contravariantMetricTensor.Getg11() * (differential_operators->DDZ(covariantMetricTensor.Getg12()) - + differential_operators->DDY(covariantMetricTensor.Getg13()) - - differential_operators->DDX(covariantMetricTensor.Getg23())) + + differential_operators->DDY(covariantMetricTensor.Getg13(), dy) + - differential_operators->DDX(covariantMetricTensor.Getg23(), dx)) + 0.5 * contravariantMetricTensor.Getg12() * (differential_operators->DDZ(covariantMetricTensor.Getg22()) - + differential_operators->DDY(covariantMetricTensor.Getg23()) - - differential_operators->DDY(covariantMetricTensor.Getg23())) + + differential_operators->DDY(covariantMetricTensor.Getg23(), dy) + - differential_operators->DDY(covariantMetricTensor.Getg23(), dy)) // + 0.5 *g13*(differential_operators->DDZ(g_32) + differential_operators->DDY(g_33) - differential_operators->DDZ(g_23)); // which equals + 0.5 * contravariantMetricTensor.Getg13() - * differential_operators->DDY(covariantMetricTensor.Getg33()); + * differential_operators->DDY(covariantMetricTensor.Getg33(), dy); - G2_11 = 0.5 * contravariantMetricTensor.Getg12() - * differential_operators->DDX(covariantMetricTensor.Getg11()) - + contravariantMetricTensor.Getg22() - * (differential_operators->DDX(covariantMetricTensor.Getg12()) - - 0.5 * differential_operators->DDY(covariantMetricTensor.Getg11())) - + contravariantMetricTensor.Getg23() - * (differential_operators->DDX(covariantMetricTensor.Getg13()) - - 0.5 * differential_operators->DDZ(covariantMetricTensor.Getg11())); + G2_11 = + 0.5 * contravariantMetricTensor.Getg12() + * differential_operators->DDX(covariantMetricTensor.Getg11(), dx) + + contravariantMetricTensor.Getg22() + * (differential_operators->DDX(covariantMetricTensor.Getg12(), dx) + - 0.5 * differential_operators->DDY(covariantMetricTensor.Getg11(), dy)) + + contravariantMetricTensor.Getg23() + * (differential_operators->DDX(covariantMetricTensor.Getg13(), dx) + - 0.5 * differential_operators->DDZ(covariantMetricTensor.Getg11())); G2_22 = contravariantMetricTensor.Getg12() - * (differential_operators->DDY(covariantMetricTensor.Getg12()) - - 0.5 * differential_operators->DDX(covariantMetricTensor.Getg22())) + * (differential_operators->DDY(covariantMetricTensor.Getg12(), dy) + - 0.5 * differential_operators->DDX(covariantMetricTensor.Getg22(), dx)) + 0.5 * contravariantMetricTensor.Getg22() - * differential_operators->DDY(covariantMetricTensor.Getg22()) + * differential_operators->DDY(covariantMetricTensor.Getg22(), dy) + contravariantMetricTensor.Getg23() - * (differential_operators->DDY(contravariantMetricTensor.Getg23()) + * (differential_operators->DDY(contravariantMetricTensor.Getg23(), dy) - 0.5 * differential_operators->DDZ(covariantMetricTensor.Getg22())); - G2_33 = contravariantMetricTensor.Getg12() - * (differential_operators->DDZ(covariantMetricTensor.Getg13()) - - 0.5 * differential_operators->DDX(covariantMetricTensor.Getg33())) - + contravariantMetricTensor.Getg22() - * (differential_operators->DDZ(covariantMetricTensor.Getg23()) - - 0.5 * differential_operators->DDY(covariantMetricTensor.Getg33())) - + 0.5 * contravariantMetricTensor.Getg23() - * differential_operators->DDZ(covariantMetricTensor.Getg33()); + G2_33 = + contravariantMetricTensor.Getg12() + * (differential_operators->DDZ(covariantMetricTensor.Getg13()) + - 0.5 * differential_operators->DDX(covariantMetricTensor.Getg33(), dx)) + + contravariantMetricTensor.Getg22() + * (differential_operators->DDZ(covariantMetricTensor.Getg23()) + - 0.5 * differential_operators->DDY(covariantMetricTensor.Getg33(), dy)) + + 0.5 * contravariantMetricTensor.Getg23() + * differential_operators->DDZ(covariantMetricTensor.Getg33()); G2_12 = 0.5 * contravariantMetricTensor.Getg12() - * differential_operators->DDY(covariantMetricTensor.Getg11()) + * differential_operators->DDY(covariantMetricTensor.Getg11(), dy) + 0.5 * contravariantMetricTensor.Getg22() - * differential_operators->DDX(covariantMetricTensor.Getg22()) + * differential_operators->DDX(covariantMetricTensor.Getg22(), dx) + 0.5 * contravariantMetricTensor.Getg23() - * (differential_operators->DDY(covariantMetricTensor.Getg13()) - + differential_operators->DDX(covariantMetricTensor.Getg23()) + * (differential_operators->DDY(covariantMetricTensor.Getg13(), dy) + + differential_operators->DDX(covariantMetricTensor.Getg23(), dx) - differential_operators->DDZ(covariantMetricTensor.Getg12())); G2_13 = // 0.5 *g21*(differential_operators->DDZ(covariantMetricTensor.Getg11()) + differential_operators->DDX(covariantMetricTensor.Getg13()) - differential_operators->DDX(covariantMetricTensor.Getg13())) // which equals 0.5 * contravariantMetricTensor.Getg12() * (differential_operators->DDZ(covariantMetricTensor.Getg11()) - + differential_operators->DDX(covariantMetricTensor.Getg13()) - - differential_operators->DDX(covariantMetricTensor.Getg13())) + + differential_operators->DDX(covariantMetricTensor.Getg13(), dx) + - differential_operators->DDX(covariantMetricTensor.Getg13(), dx)) // + 0.5 *g22*(differential_operators->DDZ(covariantMetricTensor.Getg21()) + differential_operators->DDX(covariantMetricTensor.Getg23()) - differential_operators->DDY(covariantMetricTensor.Getg13())) // which equals + 0.5 * contravariantMetricTensor.Getg22() * (differential_operators->DDZ(covariantMetricTensor.Getg12()) - + differential_operators->DDX(covariantMetricTensor.Getg23()) - - differential_operators->DDY(covariantMetricTensor.Getg13())) + + differential_operators->DDX(covariantMetricTensor.Getg23(), dx) + - differential_operators->DDY(covariantMetricTensor.Getg13(), dy)) // + 0.5 *g23*(differential_operators->DDZ(covariantMetricTensor.Getg31()) + differential_operators->DDX(covariantMetricTensor.Getg33()) - differential_operators->DDZ(g_13)); // which equals + 0.5 * contravariantMetricTensor.Getg23() - * differential_operators->DDX(covariantMetricTensor.Getg33()); + * differential_operators->DDX(covariantMetricTensor.Getg33(), dx); G2_23 = 0.5 * contravariantMetricTensor.Getg12() * (differential_operators->DDZ(covariantMetricTensor.Getg12()) - + differential_operators->DDY(covariantMetricTensor.Getg13()) - - differential_operators->DDX(covariantMetricTensor.Getg23())) + + differential_operators->DDY(covariantMetricTensor.Getg13(), dy) + - differential_operators->DDX(covariantMetricTensor.Getg23(), dx)) + 0.5 * contravariantMetricTensor.Getg22() * differential_operators->DDZ(covariantMetricTensor.Getg22()) + 0.5 * contravariantMetricTensor.Getg23() - * differential_operators->DDY(covariantMetricTensor.Getg33()); + * differential_operators->DDY(covariantMetricTensor.Getg33(), dy); - G3_11 = 0.5 * contravariantMetricTensor.Getg13() - * differential_operators->DDX(covariantMetricTensor.Getg11()) - + contravariantMetricTensor.Getg23() - * (differential_operators->DDX(covariantMetricTensor.Getg12()) - - 0.5 * differential_operators->DDY(covariantMetricTensor.Getg11())) - + contravariantMetricTensor.Getg33() - * (differential_operators->DDX(covariantMetricTensor.Getg13()) - - 0.5 * differential_operators->DDZ(covariantMetricTensor.Getg11())); + G3_11 = + 0.5 * contravariantMetricTensor.Getg13() + * differential_operators->DDX(covariantMetricTensor.Getg11(), dx) + + contravariantMetricTensor.Getg23() + * (differential_operators->DDX(covariantMetricTensor.Getg12(), dx) + - 0.5 * differential_operators->DDY(covariantMetricTensor.Getg11(), dy)) + + contravariantMetricTensor.Getg33() + * (differential_operators->DDX(covariantMetricTensor.Getg13(), dx) + - 0.5 * differential_operators->DDZ(covariantMetricTensor.Getg11())); G3_22 = contravariantMetricTensor.Getg13() - * (differential_operators->DDY(covariantMetricTensor.Getg12()) - - 0.5 * differential_operators->DDX(covariantMetricTensor.Getg22())) + * (differential_operators->DDY(covariantMetricTensor.Getg12(), dy) + - 0.5 * differential_operators->DDX(covariantMetricTensor.Getg22(), dx)) + 0.5 * contravariantMetricTensor.Getg23() - * differential_operators->DDY(covariantMetricTensor.Getg22()) + * differential_operators->DDY(covariantMetricTensor.Getg22(), dy) + contravariantMetricTensor.Getg33() - * (differential_operators->DDY(covariantMetricTensor.Getg23()) + * (differential_operators->DDY(covariantMetricTensor.Getg23(), dy) - 0.5 * differential_operators->DDZ(covariantMetricTensor.Getg22())); - G3_33 = contravariantMetricTensor.Getg13() - * (differential_operators->DDZ(covariantMetricTensor.Getg13()) - - 0.5 * differential_operators->DDX(covariantMetricTensor.Getg33())) - + contravariantMetricTensor.Getg23() - * (differential_operators->DDZ(covariantMetricTensor.Getg23()) - - 0.5 * differential_operators->DDY(covariantMetricTensor.Getg33())) - + 0.5 * contravariantMetricTensor.Getg33() - * differential_operators->DDZ(covariantMetricTensor.Getg33()); + G3_33 = + contravariantMetricTensor.Getg13() + * (differential_operators->DDZ(covariantMetricTensor.Getg13()) + - 0.5 * differential_operators->DDX(covariantMetricTensor.Getg33(), dx)) + + contravariantMetricTensor.Getg23() + * (differential_operators->DDZ(covariantMetricTensor.Getg23()) + - 0.5 * differential_operators->DDY(covariantMetricTensor.Getg33(), dy)) + + 0.5 * contravariantMetricTensor.Getg33() + * differential_operators->DDZ(covariantMetricTensor.Getg33()); G3_12 = // 0.5 *g31*(differential_operators->DDY(covariantMetricTensor.Getg11()) + differential_operators->DDX(covariantMetricTensor.Getg12()) - differential_operators->DDX(covariantMetricTensor.Getg12())) // which equals to 0.5 * contravariantMetricTensor.Getg13() - * differential_operators->DDY(covariantMetricTensor.Getg11()) + * differential_operators->DDY(covariantMetricTensor.Getg11(), dy) // + 0.5 *g32*(differential_operators->DDY(covariantMetricTensor.Getg21()) + differential_operators->DDX(covariantMetricTensor.Getg22()) - differential_operators->DDY(covariantMetricTensor.Getg12())) // which equals to + 0.5 * contravariantMetricTensor.Getg23() - * differential_operators->DDX(covariantMetricTensor.Getg22()) + * differential_operators->DDX(covariantMetricTensor.Getg22(), dx) //+ 0.5 *g33*(differential_operators->DDY(covariantMetricTensor.Getg31()) + differential_operators->DDX(covariantMetricTensor.Getg32()) - differential_operators->DDZ(covariantMetricTensor.Getg12())); // which equals to + 0.5 * contravariantMetricTensor.Getg33() - * (differential_operators->DDY(covariantMetricTensor.Getg13()) - + differential_operators->DDX(covariantMetricTensor.Getg23()) - - differential_operators->DDZ(covariantMetricTensor.Getg12())); + * (differential_operators->DDY(covariantMetricTensor.Getg13(), dy)) + + differential_operators->DDX(covariantMetricTensor.Getg23(), dx) + - differential_operators->DDZ(covariantMetricTensor.Getg12()); G3_13 = 0.5 * contravariantMetricTensor.Getg13() * differential_operators->DDZ(covariantMetricTensor.Getg11()) + 0.5 * contravariantMetricTensor.Getg23() * (differential_operators->DDZ(covariantMetricTensor.Getg12()) - + differential_operators->DDX(covariantMetricTensor.Getg23()) - - differential_operators->DDY(covariantMetricTensor.Getg13())) + + differential_operators->DDX(covariantMetricTensor.Getg23(), dx) + - differential_operators->DDY(covariantMetricTensor.Getg13(), dy)) + 0.5 * contravariantMetricTensor.Getg33() - * differential_operators->DDX(covariantMetricTensor.Getg33()); + * differential_operators->DDX(covariantMetricTensor.Getg33(), dx); G3_23 = 0.5 * contravariantMetricTensor.Getg13() * (differential_operators->DDZ(covariantMetricTensor.Getg12()) - + differential_operators->DDY(covariantMetricTensor.Getg13()) - - differential_operators->DDX(covariantMetricTensor.Getg23())) + + differential_operators->DDY(covariantMetricTensor.Getg13(), dy)) + - differential_operators->DDX(covariantMetricTensor.Getg23(), dx) + 0.5 * contravariantMetricTensor.Getg23() * differential_operators->DDZ(covariantMetricTensor.Getg22()) + 0.5 * contravariantMetricTensor.Getg33() - * differential_operators->DDY(covariantMetricTensor.Getg33()); + * differential_operators->DDY(covariantMetricTensor.Getg33(), dy); } void Geometry::calcCovariant(CELL_LOC cell_location, const std::string& region) { From ba261313603718a6d3927f1f64b7125350d3f717 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 29 Nov 2023 14:00:18 +0000 Subject: [PATCH 208/491] Fixed obtaining initial values of J and Bxy. --- src/mesh/coordinates.cxx | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index e2112bf57b..f1cb3fee9b 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -531,18 +531,16 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, jacobian(); // Attempt to read J from the grid file - auto Jcalc = J(); - try { - Jcalc = getAtLoc(mesh, "J", suffix, location); + if (!localmesh->sourceHasVar("J" + suffix)) { output_warn.write( "\tWARNING: Jacobian 'J_{:s}' not found. Calculating from metric tensor\n", suffix); - setJ(Jcalc); - } catch (BoutException&) { + } else { setJ(localmesh->interpolateAndExtrapolate(J(), location, extrapolate_x, extrapolate_y, false, transform.get())); // Compare calculated and loaded values + const auto Jcalc = getAtLoc(mesh, "J", suffix, location); output_warn.write("\tMaximum difference in J is {:e}\n", max(abs(J() - Jcalc))); auto J_value = J(); // TODO: There may be a better way @@ -560,19 +558,16 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, } // Attempt to read Bxy from the grid file - auto Bcalc = Bxy(); - try { - Bcalc = getAtLoc(mesh, "Bxy", suffix, location); + if (!localmesh->sourceHasVar("Bxy" + suffix)) { output_warn.write("\tWARNING: Magnitude of B field 'Bxy_{:s}' not found. " "Calculating from metric tensor\n", suffix); - setBxy(Bcalc); - } catch (BoutException&) { + } else { setBxy(localmesh->interpolateAndExtrapolate(Bxy(), location, extrapolate_x, extrapolate_y, false, transform.get())); + const auto Bcalc = getAtLoc(mesh, "Bxy", suffix, location); output_warn.write("\tMaximum difference in Bxy is {:e}\n", max(abs(Bxy() - Bcalc))); } - // Check Bxy bout::checkFinite(Bxy(), "Bxy" + suffix, "RGN_NOCORNERS"); bout::checkPositive(Bxy(), "Bxy" + suffix, "RGN_NOCORNERS"); From 0da4ba911c6d9ab72cacc3d7d825a3a2bbc914a9 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 29 Nov 2023 14:06:38 +0000 Subject: [PATCH 209/491] Update helper.cxx.jinja to reflect renaming of geometry() to calculateGeometry(). --- tools/pylib/_boutpp_build/helper.cxx.jinja | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/pylib/_boutpp_build/helper.cxx.jinja b/tools/pylib/_boutpp_build/helper.cxx.jinja index fdaa944f3e..3a2cdb5626 100644 --- a/tools/pylib/_boutpp_build/helper.cxx.jinja +++ b/tools/pylib/_boutpp_build/helper.cxx.jinja @@ -173,5 +173,5 @@ void c_mesh_normalise(Mesh * msh, double norm){ coord->dx /= norm; coord->dy /= norm; coord->dz /= norm; - coord->geometry(); + coord->calculateGeometry(); } From 942bd875c4b3e0d3560c79d2b1631067a7be0a58 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 29 Nov 2023 16:51:12 +0000 Subject: [PATCH 210/491] Further fix. --- src/mesh/coordinates.cxx | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index f1cb3fee9b..65f4b8d3b7 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -536,11 +536,11 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, "\tWARNING: Jacobian 'J_{:s}' not found. Calculating from metric tensor\n", suffix); } else { - setJ(localmesh->interpolateAndExtrapolate(J(), location, extrapolate_x, + const auto Jcalc = getAtLoc(mesh, "J", suffix, location); + setJ(localmesh->interpolateAndExtrapolate(Jcalc, location, extrapolate_x, extrapolate_y, false, transform.get())); // Compare calculated and loaded values - const auto Jcalc = getAtLoc(mesh, "J", suffix, location); output_warn.write("\tMaximum difference in J is {:e}\n", max(abs(J() - Jcalc))); auto J_value = J(); // TODO: There may be a better way @@ -563,9 +563,9 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, "Calculating from metric tensor\n", suffix); } else { - setBxy(localmesh->interpolateAndExtrapolate(Bxy(), location, extrapolate_x, - extrapolate_y, false, transform.get())); const auto Bcalc = getAtLoc(mesh, "Bxy", suffix, location); + setBxy(localmesh->interpolateAndExtrapolate(Bcalc, location, extrapolate_x, + extrapolate_y, false, transform.get())); output_warn.write("\tMaximum difference in Bxy is {:e}\n", max(abs(Bxy() - Bcalc))); } // Check Bxy From 512ef73c155e195ac043aa509444e9285cf6ef10 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 29 Nov 2023 16:52:07 +0000 Subject: [PATCH 211/491] Implement 'hasVar' method of FakeGridDataSource, rather than always returning false. --- tests/unit/test_extras.hxx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit/test_extras.hxx b/tests/unit/test_extras.hxx index f91a4a237b..35262d23cd 100644 --- a/tests/unit/test_extras.hxx +++ b/tests/unit/test_extras.hxx @@ -337,7 +337,7 @@ public: /// Take an rvalue (e.g. initializer list), convert to lvalue and delegate constructor FakeGridDataSource(Options&& values) : FakeGridDataSource(values) {} - bool hasVar(const std::string& UNUSED(name)) override { return false; } + bool hasVar(const std::string& name) override { return values.isSet(name); } bool get([[maybe_unused]] Mesh* m, std::string& sval, const std::string& name, const std::string& def = "") override { From e0d1793766103bb388611e4802e9d1dcb7b669ed Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 29 Nov 2023 17:24:47 +0000 Subject: [PATCH 212/491] Correct accidentally altered comments. --- src/mesh/coordinates.cxx | 2 +- src/mesh/geometry.cxx | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 65f4b8d3b7..e1996d1d62 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -654,7 +654,7 @@ int Coordinates::calculateGeometry(bool recalculate_staggered, communicate(dx, dy, dz, g11(), g22(), g33(), g12(), g13(), g23(), g11(), g22(), g33(), g12(), g13(), g23(), J(), Bxy()); - output_progress.write("Calculating differential calculateGeometry terms\n"); + output_progress.write("Calculating differential geometry terms\n"); if (min(abs(dx)) < 1e-8) { throw BoutException("dx magnitude less than 1e-8"); diff --git a/src/mesh/geometry.cxx b/src/mesh/geometry.cxx index 0cbb93ef3b..7442acc574 100644 --- a/src/mesh/geometry.cxx +++ b/src/mesh/geometry.cxx @@ -41,7 +41,7 @@ // result.allocate(); // communicate(result); // -// // Extrapolate into boundaries (if requested) so that differential calculateGeometry +// // Extrapolate into boundaries (if requested) so that differential geometry // // terms can be interpolated if necessary // // Note: cannot use applyBoundary("free_o3") here because applyBoundary() // // would try to create a new Coordinates object since we have not finished @@ -486,7 +486,7 @@ Geometry::Geometry(Mesh* mesh, DifferentialOperators* differential_operators) // covariantMetricTensor.Getg12(), covariantMetricTensor.Getg13(), // covariantMetricTensor.Getg23(), this_J, this_Bxy); // -// output_progress.write("Calculating differential calculateGeometry terms\n"); +// output_progress.write("Calculating differential geometry terms\n"); // // if (min(abs(dx)) < 1e-8) { // throw BoutException("dx magnitude less than 1e-8"); From fa0c0b4d1fd9d4ed929e3a7421e593f60267f15a Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 29 Nov 2023 17:25:38 +0000 Subject: [PATCH 213/491] Extract method communicateChristoffelSymbolTerms(). --- include/bout/coordinates.hxx | 2 ++ src/mesh/coordinates.cxx | 14 +++++++++----- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index 46cd821bf3..e9f177e372 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -289,6 +289,8 @@ private: // FieldMetric recalculateJacobian(); // FieldMetric recalculateBxy(); + + void communicateChristoffelSymbolTerms(); }; /* diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index e1996d1d62..3c84e562dd 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -683,11 +683,7 @@ int Coordinates::calculateGeometry(bool recalculate_staggered, communicate(tmp); G3 = (DDX(J() * g13()) + DDY(tmp) + DDZ(J() * g33())) / J(); - // Communicate christoffel symbol terms - output_progress.write("\tCommunicating connection terms\n"); - - communicate(G1_11, G1_22, G1_33, G1_12, G1_13, G1_23, G2_11, G2_22, G2_33, G2_12, G2_13, - G2_23, G3_11, G3_22, G3_33, G3_12, G3_13, G3_23, G1, G2, G3); + communicateChristoffelSymbolTerms(); // Set boundary guard cells of Christoffel symbol terms // Ideally, when location is staggered, we would set the upper/outer boundary point @@ -1651,6 +1647,14 @@ const MetricTensor& Coordinates::getContravariantMetricTensor() const { return geometry.getContravariantMetricTensor(); } +void Coordinates::communicateChristoffelSymbolTerms() { + + output_progress.write("\tCommunicating connection terms\n"); + + communicate(G1_11, G1_22, G1_33, G1_12, G1_13, G1_23, G2_11, G2_22, G2_33, G2_12, G2_13, + G2_23, G3_11, G3_22, G3_33, G3_12, G3_13, G3_23, G1, G2, G3); +} + //const MetricTensor& Coordinates::getCovariantMetricTensor() const { // return geometry.getCovariantMetricTensor(); //} From 252c84c9ae41574b0002eacc381ff4ce077e3f9e Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 29 Nov 2023 17:52:08 +0000 Subject: [PATCH 214/491] Move Christoffel symbol of the second kind from Coordinates to Geometry, and make private. --- include/bout/coordinates.hxx | 32 ++++++++++++---- include/bout/geometry.hxx | 39 +++++++++++++++---- src/field/vecops.cxx | 72 ++++++++++++++++++------------------ src/mesh/coordinates.cxx | 36 +++++++++++++++--- src/mesh/geometry.cxx | 25 +++++++++++++ src/sys/derivs.cxx | 24 ++++++------ 6 files changed, 161 insertions(+), 67 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index e9f177e372..a2a22f8345 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -115,13 +115,6 @@ public: void setBxy(FieldMetric Bxy); - /// Christoffel symbol of the second kind (connection coefficients) - FieldMetric G1_11, G1_22, G1_33, G1_12, G1_13, G1_23; - FieldMetric G2_11, G2_22, G2_33, G2_12, G2_13, G2_23; - FieldMetric G3_11, G3_22, G3_33, G3_12, G3_13, G3_23; - - FieldMetric G1, G2, G3; - /// d pitch angle / dx. Needed for vector differentials (Curl) FieldMetric ShiftTorsion; @@ -237,6 +230,31 @@ public: // solver Field2D Laplace_perpXY(const Field2D& A, const Field2D& f); + const FieldMetric& G1_11(); + const FieldMetric& G1_22(); + const FieldMetric& G1_33(); + const FieldMetric& G1_12(); + const FieldMetric& G1_13(); + const FieldMetric& G1_23(); + + const FieldMetric& G2_11(); + const FieldMetric& G2_22(); + const FieldMetric& G2_33(); + const FieldMetric& G2_12(); + const FieldMetric& G2_13(); + const FieldMetric& G2_23(); + + const FieldMetric& G3_11(); + const FieldMetric& G3_22(); + const FieldMetric& G3_33(); + const FieldMetric& G3_12(); + const FieldMetric& G3_13(); + const FieldMetric& G3_23(); + + const FieldMetric& G1() const; + const FieldMetric& G2() const; + const FieldMetric& G3() const; + private: int nz; // Size of mesh in Z. This is mesh->ngz-1 Mesh* localmesh; diff --git a/include/bout/geometry.hxx b/include/bout/geometry.hxx index 84c4a3f0ab..e082c866fe 100644 --- a/include/bout/geometry.hxx +++ b/include/bout/geometry.hxx @@ -56,13 +56,6 @@ public: Geometry(Mesh* mesh, DifferentialOperators* differential_operators); - /// Christoffel symbol of the second kind (connection coefficients) - FieldMetric G1_11, G1_22, G1_33, G1_12, G1_13, G1_23; - FieldMetric G2_11, G2_22, G2_33, G2_12, G2_13, G2_23; - FieldMetric G3_11, G3_22, G3_33, G3_12, G3_13, G3_23; - - FieldMetric G1, G2, G3; - /// Covariant metric tensor const FieldMetric& g_11() const; const FieldMetric& g_22() const; @@ -82,6 +75,31 @@ public: const MetricTensor& getContravariantMetricTensor() const; const MetricTensor& getCovariantMetricTensor() const; + const FieldMetric& G1_11() const; + const FieldMetric& G1_22() const; + const FieldMetric& G1_33() const; + const FieldMetric& G1_12() const; + const FieldMetric& G1_13() const; + const FieldMetric& G1_23() const; + + const FieldMetric& G2_11() const; + const FieldMetric& G2_22() const; + const FieldMetric& G2_33() const; + const FieldMetric& G2_12() const; + const FieldMetric& G2_13() const; + const FieldMetric& G2_23() const; + + const FieldMetric& G3_11() const; + const FieldMetric& G3_22() const; + const FieldMetric& G3_33() const; + const FieldMetric& G3_12() const; + const FieldMetric& G3_13() const; + const FieldMetric& G3_23() const; + + const FieldMetric& G1() const; + const FieldMetric& G2() const; + const FieldMetric& G3() const; + ///< Coordinate system Jacobian, so volume of cell is J*dx*dy*dz const FieldMetric& J() const; @@ -133,6 +151,13 @@ private: MetricTensor contravariantMetricTensor; MetricTensor covariantMetricTensor; + /// Christoffel symbol of the second kind (connection coefficients) + FieldMetric G1_11_, G1_22_, G1_33_, G1_12_, G1_13_, G1_23_; + FieldMetric G2_11_, G2_22_, G2_33_, G2_12_, G2_13_, G2_23_; + FieldMetric G3_11_, G3_22_, G3_33_, G3_12_, G3_13_, G3_23_; + + FieldMetric G1_, G2_, G3_; + FieldMetric this_J; FieldMetric this_Bxy; ///< Magnitude of B = nabla z times nabla x diff --git a/src/field/vecops.cxx b/src/field/vecops.cxx index 1368e5d0e1..23bb75ee1a 100644 --- a/src/field/vecops.cxx +++ b/src/field/vecops.cxx @@ -390,80 +390,80 @@ R V_dot_Grad(const T& v, const F& a) { result.x = VDDX(vcn.x, a.x) + VDDY(vcn.y, a.x) + VDDZ(vcn.z, a.x); BOUT_FOR(i, result.x.getRegion("RGN_ALL")) { result.x[i] -= vcn.x[i] - * (metric->G1_11[i] * a.x[i] + metric->G2_11[i] * a.y[i] - + metric->G3_11[i] * a.z[i]); + * (metric->G1_11()[i] * a.x[i] + metric->G2_11()[i] * a.y[i] + + metric->G3_11()[i] * a.z[i]); result.x[i] -= vcn.y[i] - * (metric->G1_12[i] * a.x[i] + metric->G2_12[i] * a.y[i] - + metric->G3_12[i] * a.z[i]); + * (metric->G1_12()[i] * a.x[i] + metric->G2_12()[i] * a.y[i] + + metric->G3_12()[i] * a.z[i]); result.x[i] -= vcn.z[i] - * (metric->G1_13[i] * a.x[i] + metric->G2_13[i] * a.y[i] - + metric->G3_13[i] * a.z[i]); + * (metric->G1_13()[i] * a.x[i] + metric->G2_13()[i] * a.y[i] + + metric->G3_13()[i] * a.z[i]); } result.y = VDDX(vcn.x, a.y) + VDDY(vcn.y, a.y) + VDDZ(vcn.z, a.y); BOUT_FOR(i, result.y.getRegion("RGN_ALL")) { result.y[i] -= vcn.x[i] - * (metric->G1_12[i] * a.x[i] + metric->G2_12[i] * a.y[i] - + metric->G3_12[i] * a.z[i]); + * (metric->G1_12()[i] * a.x[i] + metric->G2_12()[i] * a.y[i] + + metric->G3_12()[i] * a.z[i]); result.y[i] -= vcn.y[i] - * (metric->G1_22[i] * a.x[i] + metric->G2_22[i] * a.y[i] - + metric->G3_22[i] * a.z[i]); + * (metric->G1_22()[i] * a.x[i] + metric->G2_22()[i] * a.y[i] + + metric->G3_22()[i] * a.z[i]); result.y[i] -= vcn.z[i] - * (metric->G1_23[i] * a.x[i] + metric->G2_23[i] * a.y[i] - + metric->G3_23[i] * a.z[i]); + * (metric->G1_23()[i] * a.x[i] + metric->G2_23()[i] * a.y[i] + + metric->G3_23()[i] * a.z[i]); } result.z = VDDX(vcn.x, a.z) + VDDY(vcn.y, a.z) + VDDZ(vcn.z, a.z); BOUT_FOR(i, result.z.getRegion("RGN_ALL")) { result.z[i] -= vcn.x[i] - * (metric->G1_13[i] * a.x[i] + metric->G2_13[i] * a.y[i] - + metric->G3_13[i] * a.z[i]); + * (metric->G1_13()[i] * a.x[i] + metric->G2_13()[i] * a.y[i] + + metric->G3_13()[i] * a.z[i]); result.z[i] -= vcn.y[i] - * (metric->G1_23[i] * a.x[i] + metric->G2_23[i] * a.y[i] - + metric->G3_23[i] * a.z[i]); + * (metric->G1_23()[i] * a.x[i] + metric->G2_23()[i] * a.y[i] + + metric->G3_23()[i] * a.z[i]); result.z[i] -= vcn.z[i] - * (metric->G1_33[i] * a.x[i] + metric->G2_33[i] * a.y[i] - + metric->G3_33[i] * a.z[i]); + * (metric->G1_33()[i] * a.x[i] + metric->G2_33()[i] * a.y[i] + + metric->G3_33()[i] * a.z[i]); } result.covariant = true; } else { result.x = VDDX(vcn.x, a.x) + VDDY(vcn.y, a.x) + VDDZ(vcn.z, a.x); BOUT_FOR(i, result.x.getRegion("RGN_ALL")) { result.x[i] += vcn.x[i] - * (metric->G1_11[i] * a.x[i] + metric->G1_12[i] * a.y[i] - + metric->G1_13[i] * a.z[i]); + * (metric->G1_11()[i] * a.x[i] + metric->G1_12()[i] * a.y[i] + + metric->G1_13()[i] * a.z[i]); result.x[i] += vcn.y[i] - * (metric->G1_12[i] * a.x[i] + metric->G1_22[i] * a.y[i] - + metric->G1_23[i] * a.z[i]); + * (metric->G1_12()[i] * a.x[i] + metric->G1_22()[i] * a.y[i] + + metric->G1_23()[i] * a.z[i]); result.x[i] += vcn.z[i] - * (metric->G1_13[i] * a.x[i] + metric->G1_23[i] * a.y[i] - + metric->G1_33[i] * a.z[i]); + * (metric->G1_13()[i] * a.x[i] + metric->G1_23()[i] * a.y[i] + + metric->G1_33()[i] * a.z[i]); } result.y = VDDX(vcn.x, a.y) + VDDY(vcn.y, a.y) + VDDZ(vcn.z, a.y); BOUT_FOR(i, result.y.getRegion("RGN_ALL")) { result.y[i] += vcn.x[i] - * (metric->G2_11[i] * a.x[i] + metric->G2_12[i] * a.y[i] - + metric->G2_13[i] * a.z[i]); + * (metric->G2_11()[i] * a.x[i] + metric->G2_12()[i] * a.y[i] + + metric->G2_13()[i] * a.z[i]); result.y[i] += vcn.y[i] - * (metric->G2_12[i] * a.x[i] + metric->G2_22[i] * a.y[i] - + metric->G2_23[i] * a.z[i]); + * (metric->G2_12()[i] * a.x[i] + metric->G2_22()[i] * a.y[i] + + metric->G2_23()[i] * a.z[i]); result.y[i] += vcn.z[i] - * (metric->G2_13[i] * a.x[i] + metric->G2_23[i] * a.y[i] - + metric->G2_33[i] * a.z[i]); + * (metric->G2_13()[i] * a.x[i] + metric->G2_23()[i] * a.y[i] + + metric->G2_33()[i] * a.z[i]); } result.z = VDDX(vcn.x, a.z) + VDDY(vcn.y, a.z) + VDDZ(vcn.z, a.z); BOUT_FOR(i, result.z.getRegion("RGN_ALL")) { result.z[i] += vcn.x[i] - * (metric->G3_11[i] * a.x[i] + metric->G3_12[i] * a.y[i] - + metric->G3_13[i] * a.z[i]); + * (metric->G3_11()[i] * a.x[i] + metric->G3_12()[i] * a.y[i] + + metric->G3_13()[i] * a.z[i]); result.z[i] += vcn.y[i] - * (metric->G3_12[i] * a.x[i] + metric->G3_22[i] * a.y[i] - + metric->G3_23[i] * a.z[i]); + * (metric->G3_12()[i] * a.x[i] + metric->G3_22()[i] * a.y[i] + + metric->G3_23()[i] * a.z[i]); result.z[i] += vcn.z[i] - * (metric->G3_13[i] * a.x[i] + metric->G3_23[i] * a.y[i] - + metric->G3_33[i] * a.z[i]); + * (metric->G3_13()[i] * a.x[i] + metric->G3_23()[i] * a.y[i] + + metric->G3_33()[i] * a.z[i]); } result.covariant = false; diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 3c84e562dd..450f2bfe8e 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -625,9 +625,9 @@ void Coordinates::outputVars(Options& output_options) { output_options["J" + loc_string].force(J(), "Coordinates"); output_options["Bxy" + loc_string].force(Bxy(), "Coordinates"); - output_options["G1" + loc_string].force(G1, "Coordinates"); - output_options["G2" + loc_string].force(G2, "Coordinates"); - output_options["G3" + loc_string].force(G3, "Coordinates"); + output_options["G1" + loc_string].force(G1(), "Coordinates"); + output_options["G2" + loc_string].force(G2(), "Coordinates"); + output_options["G3" + loc_string].force(G3(), "Coordinates"); getParallelTransform().outputVars(output_options); } @@ -1628,6 +1628,31 @@ const FieldMetric& Coordinates::J() const { return geometry.J(); } const FieldMetric& Coordinates::Bxy() const { return geometry.Bxy(); } +const FieldMetric& Coordinates::G1_11() { return geometry.G1_11(); } +const FieldMetric& Coordinates::G1_22() { return geometry.G1_22(); } +const FieldMetric& Coordinates::G1_33() { return geometry.G1_33(); } +const FieldMetric& Coordinates::G1_12() { return geometry.G1_12(); } +const FieldMetric& Coordinates::G1_13() { return geometry.G1_13(); } +const FieldMetric& Coordinates::G1_23() { return geometry.G1_23(); } + +const FieldMetric& Coordinates::G2_11() { return geometry.G2_11(); } +const FieldMetric& Coordinates::G2_22() { return geometry.G2_22(); } +const FieldMetric& Coordinates::G2_33() { return geometry.G2_33(); } +const FieldMetric& Coordinates::G2_12() { return geometry.G2_12(); } +const FieldMetric& Coordinates::G2_13() { return geometry.G2_13(); } +const FieldMetric& Coordinates::G2_23() { return geometry.G2_23(); } + +const FieldMetric& Coordinates::G3_11() { return geometry.G3_11(); } +const FieldMetric& Coordinates::G3_22() { return geometry.G3_22(); } +const FieldMetric& Coordinates::G3_33() { return geometry.G3_33(); } +const FieldMetric& Coordinates::G3_12() { return geometry.G3_12(); } +const FieldMetric& Coordinates::G3_13() { return geometry.G3_13(); } +const FieldMetric& Coordinates::G3_23() { return geometry.G3_23(); } + +const FieldMetric& Coordinates::G1() const { return geometry.G1(); } +const FieldMetric& Coordinates::G2() const { return geometry.G2(); } +const FieldMetric& Coordinates::G3() const { return geometry.G3(); } + void Coordinates::setJ(FieldMetric J) { //TODO: Calculate J and check value is close geometry.setJ(J); @@ -1651,8 +1676,9 @@ void Coordinates::communicateChristoffelSymbolTerms() { output_progress.write("\tCommunicating connection terms\n"); - communicate(G1_11, G1_22, G1_33, G1_12, G1_13, G1_23, G2_11, G2_22, G2_33, G2_12, G2_13, - G2_23, G3_11, G3_22, G3_33, G3_12, G3_13, G3_23, G1, G2, G3); + communicate(G1_11(), G1_22(), G1_33(), G1_12(), G1_13(), G1_23(), G2_11(), G2_22(), + G2_33(), G2_12(), G2_13(), G2_23(), G3_11(), G3_22(), G3_33(), G3_12(), + G3_13(), G3_23(), G1(), G2(), G3()); } //const MetricTensor& Coordinates::getCovariantMetricTensor() const { diff --git a/src/mesh/geometry.cxx b/src/mesh/geometry.cxx index 7442acc574..839633ac2d 100644 --- a/src/mesh/geometry.cxx +++ b/src/mesh/geometry.cxx @@ -1562,6 +1562,31 @@ const MetricTensor::FieldMetric& Geometry::g23() const { return contravariantMetricTensor.Getg23(); } +const FieldMetric& Geometry::G1_11() const { return G1_11_; } +const FieldMetric& Geometry::G1_22() const { return G1_22_; } +const FieldMetric& Geometry::G1_33() const { return G1_33_; } +const FieldMetric& Geometry::G1_12() const { return G1_12_; } +const FieldMetric& Geometry::G1_13() const { return G1_13_; } +const FieldMetric& Geometry::G1_23() const { return G1_23_; } + +const FieldMetric& Geometry::G2_11() const { return G2_11_; } +const FieldMetric& Geometry::G2_22() const { return G2_22_; } +const FieldMetric& Geometry::G2_33() const { return G2_33_; } +const FieldMetric& Geometry::G2_12() const { return G2_12_; } +const FieldMetric& Geometry::G2_13() const { return G2_13_; } +const FieldMetric& Geometry::G2_23() const { return G2_23_; } + +const FieldMetric& Geometry::G3_11() const { return G3_11_; } +const FieldMetric& Geometry::G3_22() const { return G3_22_; } +const FieldMetric& Geometry::G3_33() const { return G3_33_; } +const FieldMetric& Geometry::G3_12() const { return G3_12_; } +const FieldMetric& Geometry::G3_13() const { return G3_13_; } +const FieldMetric& Geometry::G3_23() const { return G3_23_; } + +const FieldMetric& Geometry::G1() const { return G1_; } +const FieldMetric& Geometry::G2() const { return G2_; } +const FieldMetric& Geometry::G3() const { return G3_; } + const FieldMetric& Geometry::J() const { return this_J; } const FieldMetric& Geometry::Bxy() const { return this_Bxy; } diff --git a/src/sys/derivs.cxx b/src/sys/derivs.cxx index ee9bcbcc2c..a31bf93649 100644 --- a/src/sys/derivs.cxx +++ b/src/sys/derivs.cxx @@ -104,21 +104,21 @@ Vector3D DDZ(const Vector3D& v, CELL_LOC outloc, const std::string& method, if (v.covariant) { // From equation (2.6.32) in D'Haeseleer - result.x = DDZ(v.x, outloc, method, region) - v.x * metric->G1_13 - - v.y * metric->G2_13 - v.z * metric->G3_13; - result.y = DDZ(v.y, outloc, method, region) - v.x * metric->G1_23 - - v.y * metric->G2_23 - v.z * metric->G3_23; - result.z = DDZ(v.z, outloc, method, region) - v.x * metric->G1_33 - - v.y * metric->G2_33 - v.z * metric->G3_33; + result.x = DDZ(v.x, outloc, method, region) - v.x * metric->G1_13() + - v.y * metric->G2_13() - v.z * metric->G3_13(); + result.y = DDZ(v.y, outloc, method, region) - v.x * metric->G1_23() + - v.y * metric->G2_23() - v.z * metric->G3_23(); + result.z = DDZ(v.z, outloc, method, region) - v.x * metric->G1_33() + - v.y * metric->G2_33() - v.z * metric->G3_33(); result.covariant = true; } else { // From equation (2.6.31) in D'Haeseleer - result.x = DDZ(v.x, outloc, method, region) + v.x * metric->G1_13 - + v.y * metric->G1_23 + v.z * metric->G1_33; - result.y = DDZ(v.y, outloc, method, region) + v.x * metric->G2_13 - + v.y * metric->G2_23 + v.z * metric->G2_33; - result.z = DDZ(v.z, outloc, method, region) + v.x * metric->G3_13 - + v.y * metric->G3_23 + v.z * metric->G3_33; + result.x = DDZ(v.x, outloc, method, region) + v.x * metric->G1_13() + + v.y * metric->G1_23() + v.z * metric->G1_33(); + result.y = DDZ(v.y, outloc, method, region) + v.x * metric->G2_13() + + v.y * metric->G2_23() + v.z * metric->G2_33(); + result.z = DDZ(v.z, outloc, method, region) + v.x * metric->G3_13() + + v.y * metric->G3_23() + v.z * metric->G3_33(); result.covariant = false; } From 1e7cb52f7ae13792e86f96a6f17dcdbb81dfd962 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 29 Nov 2023 18:13:04 +0000 Subject: [PATCH 215/491] Add setters, and update tests. --- include/bout/geometry.hxx | 33 ++- .../impls/multigrid/multigrid_laplace.cxx | 6 +- .../laplace/impls/petsc/petsc_laplace.cxx | 4 +- .../laplace/impls/serial_band/serial_band.cxx | 4 +- src/invert/laplace/invert_laplace.cxx | 4 +- src/mesh/coordinates.cxx | 98 ++++----- src/mesh/geometry.cxx | 196 ++++++++++-------- 7 files changed, 199 insertions(+), 146 deletions(-) diff --git a/include/bout/geometry.hxx b/include/bout/geometry.hxx index e082c866fe..c12af2152c 100644 --- a/include/bout/geometry.hxx +++ b/include/bout/geometry.hxx @@ -99,7 +99,7 @@ public: const FieldMetric& G1() const; const FieldMetric& G2() const; const FieldMetric& G3() const; - + ///< Coordinate system Jacobian, so volume of cell is J*dx*dy*dz const FieldMetric& J() const; @@ -112,6 +112,31 @@ public: void setCovariantMetricTensor(MetricTensor metric_tensor, CELL_LOC cell_location, const std::string& region = "RGN_ALL"); + void setG1_11(FieldMetric G1_11); + void setG1_22(FieldMetric G1_22); + void setG1_33(FieldMetric G1_33); + void setG1_12(FieldMetric G1_12); + void setG1_13(FieldMetric G1_13); + void setG1_23(FieldMetric G1_23); + + void setG2_11(FieldMetric G2_11); + void setG2_22(FieldMetric G2_22); + void setG2_33(FieldMetric G2_33); + void setG2_12(FieldMetric G2_12); + void setG2_13(FieldMetric G2_13); + void setG2_23(FieldMetric G2_23); + + void setG3_11(FieldMetric G3_11); + void setG3_22(FieldMetric G3_22); + void setG3_33(FieldMetric G3_33); + void setG3_12(FieldMetric G3_12); + void setG3_13(FieldMetric G3_13); + void setG3_23(FieldMetric G3_23); + + void setG3(FieldMetric G3); + void setG1(FieldMetric G1); + void setG2(FieldMetric G2); + void setJ(FieldMetric J); void setJ(BoutReal value, int x, int y); @@ -148,9 +173,6 @@ private: // int calculateGeometry(FieldMetric& dx, FieldMetric& dy, FieldMetric& dz, // bool recalculate_staggered, bool force_interpolate_from_centre); - MetricTensor contravariantMetricTensor; - MetricTensor covariantMetricTensor; - /// Christoffel symbol of the second kind (connection coefficients) FieldMetric G1_11_, G1_22_, G1_33_, G1_12_, G1_13_, G1_23_; FieldMetric G2_11_, G2_22_, G2_33_, G2_12_, G2_13_, G2_23_; @@ -158,6 +180,9 @@ private: FieldMetric G1_, G2_, G3_; + MetricTensor contravariantMetricTensor; + MetricTensor covariantMetricTensor; + FieldMetric this_J; FieldMetric this_Bxy; ///< Magnitude of B = nabla z times nabla x diff --git a/src/invert/laplace/impls/multigrid/multigrid_laplace.cxx b/src/invert/laplace/impls/multigrid/multigrid_laplace.cxx index a4a72f3d7e..6f7070e1e1 100644 --- a/src/invert/laplace/impls/multigrid/multigrid_laplace.cxx +++ b/src/invert/laplace/impls/multigrid/multigrid_laplace.cxx @@ -616,7 +616,8 @@ void LaplaceMultigrid::generateMatrixF(int level) { // if easier; then check this is true in constructor) BoutReal dxd = - (D(i2, yindex, k2) * coords->G1(i2, yindex) + coords->g11()(i2, yindex) * ddx_C + (D(i2, yindex, k2) * coords->G1()(i2, yindex) + + coords->g11()(i2, yindex) * ddx_C + coords->g13()(i2, yindex) * ddz_C // (could assume zero, at least initially, if easier; then check this is true in constructor) ) @@ -627,7 +628,8 @@ void LaplaceMultigrid::generateMatrixF(int level) { } BoutReal dzd = - (D(i2, yindex, k2) * coords->G3(i2, yindex) + coords->g33()(i2, yindex) * ddz_C + (D(i2, yindex, k2) * coords->G3()(i2, yindex) + + coords->g33()(i2, yindex) * ddz_C + coords->g13()(i2, yindex) * ddx_C // (could assume zero, at least initially, if easier; then check // this is true in constructor) diff --git a/src/invert/laplace/impls/petsc/petsc_laplace.cxx b/src/invert/laplace/impls/petsc/petsc_laplace.cxx index d125b90694..623792ff9c 100644 --- a/src/invert/laplace/impls/petsc/petsc_laplace.cxx +++ b/src/invert/laplace/impls/petsc/petsc_laplace.cxx @@ -1010,8 +1010,8 @@ void LaplacePetsc::Coeffs(int x, int y, int z, BoutReal& coef1, BoutReal& coef2, coef5 = 0.0; // If global flag all_terms are set (true by default) if (all_terms) { - coef4 = coords->G1(x, y, z); // X 1st derivative - coef5 = coords->G3(x, y, z); // Z 1st derivative + coef4 = coords->G1()(x, y, z); // X 1st derivative + coef5 = coords->G3()(x, y, z); // Z 1st derivative ASSERT3(finite(coef4)); ASSERT3(finite(coef5)); diff --git a/src/invert/laplace/impls/serial_band/serial_band.cxx b/src/invert/laplace/impls/serial_band/serial_band.cxx index cdfe9497e6..beb034f281 100644 --- a/src/invert/laplace/impls/serial_band/serial_band.cxx +++ b/src/invert/laplace/impls/serial_band/serial_band.cxx @@ -171,8 +171,8 @@ FieldPerp LaplaceSerialBand::solve(const FieldPerp& b, const FieldPerp& x0) { coef3 *= Dcoef(ix, jy); if (all_terms) { - coef4 = coords->G1(ix, jy); - coef5 = coords->G3(ix, jy); + coef4 = coords->G1()(ix, jy); + coef5 = coords->G3()(ix, jy); } if (nonuniform) { diff --git a/src/invert/laplace/invert_laplace.cxx b/src/invert/laplace/invert_laplace.cxx index 1e95ff77ad..fea972ebee 100644 --- a/src/invert/laplace/invert_laplace.cxx +++ b/src/invert/laplace/invert_laplace.cxx @@ -332,8 +332,8 @@ void Laplacian::tridagCoefs(int jx, int jy, BoutReal kwave, dcomplex& a, dcomple coef5 = 0.0; // If global flag all_terms are set (true by default) if (all_terms) { - coef4 = localcoords->G1(jx, jy); // X 1st derivative - coef5 = localcoords->G3(jx, jy); // Z 1st derivative + coef4 = localcoords->G1()(jx, jy); // X 1st derivative + coef5 = localcoords->G3()(jx, jy); // Z 1st derivative } if (d != nullptr) { diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 450f2bfe8e..48614457c7 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -675,13 +675,13 @@ int Coordinates::calculateGeometry(bool recalculate_staggered, auto tmp = J() * g12(); communicate(tmp); - G1 = (DDX(J() * g11()) + DDY(tmp) + DDZ(J() * g13())) / J(); + geometry.setG1((DDX(J() * g11()) + DDY(tmp) + DDZ(J() * g13())) / J()); tmp = J() * g22(); communicate(tmp); - G2 = (DDX(J() * g12()) + DDY(tmp) + DDZ(J() * g23())) / J(); + geometry.setG2((DDX(J() * g12()) + DDY(tmp) + DDZ(J() * g23())) / J()); tmp = J() * g23(); communicate(tmp); - G3 = (DDX(J() * g13()) + DDY(tmp) + DDZ(J() * g33())) / J(); + geometry.setG3((DDX(J() * g13()) + DDY(tmp) + DDZ(J() * g33())) / J()); communicateChristoffelSymbolTerms(); @@ -695,51 +695,51 @@ int Coordinates::calculateGeometry(bool recalculate_staggered, // CELL_YLOW grid is at a 'guard cell' location (yend+1). // However, the above would require lots of special handling, so just extrapolate for // now. - G1_11 = localmesh->interpolateAndExtrapolate(G1_11, location, true, true, true, - transform.get()); - G1_22 = localmesh->interpolateAndExtrapolate(G1_22, location, true, true, true, - transform.get()); - G1_33 = localmesh->interpolateAndExtrapolate(G1_33, location, true, true, true, - transform.get()); - G1_12 = localmesh->interpolateAndExtrapolate(G1_12, location, true, true, true, - transform.get()); - G1_13 = localmesh->interpolateAndExtrapolate(G1_13, location, true, true, true, - transform.get()); - G1_23 = localmesh->interpolateAndExtrapolate(G1_23, location, true, true, true, - transform.get()); - - G2_11 = localmesh->interpolateAndExtrapolate(G2_11, location, true, true, true, - transform.get()); - G2_22 = localmesh->interpolateAndExtrapolate(G2_22, location, true, true, true, - transform.get()); - G2_33 = localmesh->interpolateAndExtrapolate(G2_33, location, true, true, true, - transform.get()); - G2_12 = localmesh->interpolateAndExtrapolate(G2_12, location, true, true, true, - transform.get()); - G2_13 = localmesh->interpolateAndExtrapolate(G2_13, location, true, true, true, - transform.get()); - G2_23 = localmesh->interpolateAndExtrapolate(G2_23, location, true, true, true, - transform.get()); - - G3_11 = localmesh->interpolateAndExtrapolate(G3_11, location, true, true, true, - transform.get()); - G3_22 = localmesh->interpolateAndExtrapolate(G3_22, location, true, true, true, - transform.get()); - G3_33 = localmesh->interpolateAndExtrapolate(G3_33, location, true, true, true, - transform.get()); - G3_12 = localmesh->interpolateAndExtrapolate(G3_12, location, true, true, true, - transform.get()); - G3_13 = localmesh->interpolateAndExtrapolate(G3_13, location, true, true, true, - transform.get()); - G3_23 = localmesh->interpolateAndExtrapolate(G3_23, location, true, true, true, - transform.get()); - - G1 = localmesh->interpolateAndExtrapolate(G1, location, true, true, true, - transform.get()); - G2 = localmesh->interpolateAndExtrapolate(G2, location, true, true, true, - transform.get()); - G3 = localmesh->interpolateAndExtrapolate(G3, location, true, true, true, - transform.get()); + geometry.setG1_11(localmesh->interpolateAndExtrapolate(G1_11(), location, true, true, + true, transform.get())); + geometry.setG1_22(localmesh->interpolateAndExtrapolate(G1_22(), location, true, true, + true, transform.get())); + geometry.setG1_33(localmesh->interpolateAndExtrapolate(G1_33(), location, true, true, + true, transform.get())); + geometry.setG1_12(localmesh->interpolateAndExtrapolate(G1_12(), location, true, true, + true, transform.get())); + geometry.setG1_13(localmesh->interpolateAndExtrapolate(G1_13(), location, true, true, + true, transform.get())); + geometry.setG1_23(localmesh->interpolateAndExtrapolate(G1_23(), location, true, true, + true, transform.get())); + + geometry.setG2_11(localmesh->interpolateAndExtrapolate(G2_11(), location, true, true, + true, transform.get())); + geometry.setG2_22(localmesh->interpolateAndExtrapolate(G2_22(), location, true, true, + true, transform.get())); + geometry.setG2_33(localmesh->interpolateAndExtrapolate(G2_33(), location, true, true, + true, transform.get())); + geometry.setG2_12(localmesh->interpolateAndExtrapolate(G2_12(), location, true, true, + true, transform.get())); + geometry.setG2_13(localmesh->interpolateAndExtrapolate(G2_13(), location, true, true, + true, transform.get())); + geometry.setG2_23(localmesh->interpolateAndExtrapolate(G2_23(), location, true, true, + true, transform.get())); + + geometry.setG3_11(localmesh->interpolateAndExtrapolate(G3_11(), location, true, true, + true, transform.get())); + geometry.setG3_22(localmesh->interpolateAndExtrapolate(G3_22(), location, true, true, + true, transform.get())); + geometry.setG3_33(localmesh->interpolateAndExtrapolate(G3_33(), location, true, true, + true, transform.get())); + geometry.setG3_12(localmesh->interpolateAndExtrapolate(G3_12(), location, true, true, + true, transform.get())); + geometry.setG3_13(localmesh->interpolateAndExtrapolate(G3_13(), location, true, true, + true, transform.get())); + geometry.setG3_23(localmesh->interpolateAndExtrapolate(G3_23(), location, true, true, + true, transform.get())); + + geometry.setG1(localmesh->interpolateAndExtrapolate(G1(), location, true, true, true, + transform.get())); + geometry.setG2(localmesh->interpolateAndExtrapolate(G2(), location, true, true, true, + transform.get())); + geometry.setG3(localmesh->interpolateAndExtrapolate(G3(), location, true, true, true, + transform.get())); ////////////////////////////////////////////////////// /// Non-uniform meshes. Need to use DDX, DDY @@ -1332,7 +1332,7 @@ Coordinates::FieldMetric Coordinates::Delp2(const Field2D& f, CELL_LOC outloc, TRACE("Coordinates::Delp2( Field2D )"); ASSERT1(location == outloc || outloc == CELL_DEFAULT) - return G1 * DDX(f, outloc) + g11() * D2DX2(f, outloc); + return G1() * DDX(f, outloc) + g11() * D2DX2(f, outloc); } Field3D Coordinates::Delp2(const Field3D& f, CELL_LOC outloc, bool useFFT) { diff --git a/src/mesh/geometry.cxx b/src/mesh/geometry.cxx index 839633ac2d..568ecac22a 100644 --- a/src/mesh/geometry.cxx +++ b/src/mesh/geometry.cxx @@ -405,10 +405,11 @@ Geometry::Geometry(FieldMetric J, FieldMetric Bxy, FieldMetric g11, FieldMetric Geometry::Geometry(Mesh* mesh, DifferentialOperators* differential_operators) //bool force_interpolate_from_centre - : G1_11(mesh), G1_22(mesh), G1_33(mesh), G1_12(mesh), G1_13(mesh), G1_23(mesh), - G2_11(mesh), G2_22(mesh), G2_33(mesh), G2_12(mesh), G2_13(mesh), G2_23(mesh), - G3_11(mesh), G3_22(mesh), G3_33(mesh), G3_12(mesh), G3_13(mesh), G3_23(mesh), - G1(mesh), G2(mesh), G3(mesh), contravariantMetricTensor(1., 1., 1., 0, 0, 0, mesh), + : G1_11_(mesh), G1_22_(mesh), G1_33_(mesh), G1_12_(mesh), G1_13_(mesh), G1_23_(mesh), + G2_11_(mesh), G2_22_(mesh), G2_33_(mesh), G2_12_(mesh), G2_13_(mesh), G2_23_(mesh), + G3_11_(mesh), G3_22_(mesh), G3_33_(mesh), G3_12_(mesh), G3_13_(mesh), G3_23_(mesh), + G1_(mesh), G2_(mesh), G3_(mesh), + contravariantMetricTensor(1., 1., 1., 0, 0, 0, mesh), covariantMetricTensor(1., 1., 1., 0, 0, 0, mesh), // Identity metric tensor this_J(1., mesh), this_Bxy(1., mesh), @@ -700,7 +701,7 @@ void Geometry::CalculateChristoffelSymbols(FieldMetric& dx, FieldMetric& dy) { // Note: This calculation is completely general: metric // tensor can be 2D or 3D. For 2D, all DDZ terms are zero - G1_11 = + G1_11_ = 0.5 * contravariantMetricTensor.Getg11() * differential_operators->DDX(covariantMetricTensor.Getg11(), dx) + contravariantMetricTensor.Getg12() @@ -709,15 +710,15 @@ void Geometry::CalculateChristoffelSymbols(FieldMetric& dx, FieldMetric& dy) { + contravariantMetricTensor.Getg13() * (differential_operators->DDX(covariantMetricTensor.Getg13(), dx) - 0.5 * differential_operators->DDZ(covariantMetricTensor.Getg11())); - G1_22 = contravariantMetricTensor.Getg11() - * (differential_operators->DDY(covariantMetricTensor.Getg12(), dy) - - 0.5 * differential_operators->DDX(covariantMetricTensor.Getg22(), dx)) - + 0.5 * contravariantMetricTensor.Getg12() - * differential_operators->DDY(covariantMetricTensor.Getg22(), dy) - + contravariantMetricTensor.Getg13() - * (differential_operators->DDY(covariantMetricTensor.Getg23(), dy) - - 0.5 * differential_operators->DDZ(covariantMetricTensor.Getg22())); - G1_33 = + G1_22_ = contravariantMetricTensor.Getg11() + * (differential_operators->DDY(covariantMetricTensor.Getg12(), dy) + - 0.5 * differential_operators->DDX(covariantMetricTensor.Getg22(), dx)) + + 0.5 * contravariantMetricTensor.Getg12() + * differential_operators->DDY(covariantMetricTensor.Getg22(), dy) + + contravariantMetricTensor.Getg13() + * (differential_operators->DDY(covariantMetricTensor.Getg23(), dy) + - 0.5 * differential_operators->DDZ(covariantMetricTensor.Getg22())); + G1_33_ = contravariantMetricTensor.Getg11() * (differential_operators->DDZ(covariantMetricTensor.Getg13()) - 0.5 * differential_operators->DDX(covariantMetricTensor.Getg33(), dx)) @@ -726,23 +727,23 @@ void Geometry::CalculateChristoffelSymbols(FieldMetric& dx, FieldMetric& dy) { - 0.5 * differential_operators->DDY(covariantMetricTensor.Getg33(), dy)) + 0.5 * contravariantMetricTensor.Getg13() * differential_operators->DDZ(covariantMetricTensor.Getg33()); - G1_12 = 0.5 * contravariantMetricTensor.Getg11() - * differential_operators->DDY(covariantMetricTensor.Getg11(), dy) - + 0.5 * contravariantMetricTensor.Getg12() - * differential_operators->DDX(covariantMetricTensor.Getg22(), dx) - + 0.5 * contravariantMetricTensor.Getg13() - * (differential_operators->DDY(covariantMetricTensor.Getg13(), dy) - + differential_operators->DDX(covariantMetricTensor.Getg23(), dx) - - differential_operators->DDZ(covariantMetricTensor.Getg12())); - G1_13 = 0.5 * contravariantMetricTensor.Getg11() - * differential_operators->DDZ(covariantMetricTensor.Getg11()) - + 0.5 * contravariantMetricTensor.Getg12() - * (differential_operators->DDZ(covariantMetricTensor.Getg12()) - + differential_operators->DDX(covariantMetricTensor.Getg23(), dx) - - differential_operators->DDY(covariantMetricTensor.Getg13(), dy)) - + 0.5 * contravariantMetricTensor.Getg13() - * differential_operators->DDX(covariantMetricTensor.Getg33(), dx); - G1_23 = + G1_12_ = 0.5 * contravariantMetricTensor.Getg11() + * differential_operators->DDY(covariantMetricTensor.Getg11(), dy) + + 0.5 * contravariantMetricTensor.Getg12() + * differential_operators->DDX(covariantMetricTensor.Getg22(), dx) + + 0.5 * contravariantMetricTensor.Getg13() + * (differential_operators->DDY(covariantMetricTensor.Getg13(), dy) + + differential_operators->DDX(covariantMetricTensor.Getg23(), dx) + - differential_operators->DDZ(covariantMetricTensor.Getg12())); + G1_13_ = 0.5 * contravariantMetricTensor.Getg11() + * differential_operators->DDZ(covariantMetricTensor.Getg11()) + + 0.5 * contravariantMetricTensor.Getg12() + * (differential_operators->DDZ(covariantMetricTensor.Getg12()) + + differential_operators->DDX(covariantMetricTensor.Getg23(), dx) + - differential_operators->DDY(covariantMetricTensor.Getg13(), dy)) + + 0.5 * contravariantMetricTensor.Getg13() + * differential_operators->DDX(covariantMetricTensor.Getg33(), dx); + G1_23_ = 0.5 * contravariantMetricTensor.Getg11() * (differential_operators->DDZ(covariantMetricTensor.Getg12()) + differential_operators->DDY(covariantMetricTensor.Getg13(), dy) @@ -756,7 +757,7 @@ void Geometry::CalculateChristoffelSymbols(FieldMetric& dx, FieldMetric& dy) { + 0.5 * contravariantMetricTensor.Getg13() * differential_operators->DDY(covariantMetricTensor.Getg33(), dy); - G2_11 = + G2_11_ = 0.5 * contravariantMetricTensor.Getg12() * differential_operators->DDX(covariantMetricTensor.Getg11(), dx) + contravariantMetricTensor.Getg22() @@ -765,15 +766,15 @@ void Geometry::CalculateChristoffelSymbols(FieldMetric& dx, FieldMetric& dy) { + contravariantMetricTensor.Getg23() * (differential_operators->DDX(covariantMetricTensor.Getg13(), dx) - 0.5 * differential_operators->DDZ(covariantMetricTensor.Getg11())); - G2_22 = contravariantMetricTensor.Getg12() - * (differential_operators->DDY(covariantMetricTensor.Getg12(), dy) - - 0.5 * differential_operators->DDX(covariantMetricTensor.Getg22(), dx)) - + 0.5 * contravariantMetricTensor.Getg22() - * differential_operators->DDY(covariantMetricTensor.Getg22(), dy) - + contravariantMetricTensor.Getg23() - * (differential_operators->DDY(contravariantMetricTensor.Getg23(), dy) - - 0.5 * differential_operators->DDZ(covariantMetricTensor.Getg22())); - G2_33 = + G2_22_ = contravariantMetricTensor.Getg12() + * (differential_operators->DDY(covariantMetricTensor.Getg12(), dy) + - 0.5 * differential_operators->DDX(covariantMetricTensor.Getg22(), dx)) + + 0.5 * contravariantMetricTensor.Getg22() + * differential_operators->DDY(covariantMetricTensor.Getg22(), dy) + + contravariantMetricTensor.Getg23() + * (differential_operators->DDY(contravariantMetricTensor.Getg23(), dy) + - 0.5 * differential_operators->DDZ(covariantMetricTensor.Getg22())); + G2_33_ = contravariantMetricTensor.Getg12() * (differential_operators->DDZ(covariantMetricTensor.Getg13()) - 0.5 * differential_operators->DDX(covariantMetricTensor.Getg33(), dx)) @@ -782,15 +783,15 @@ void Geometry::CalculateChristoffelSymbols(FieldMetric& dx, FieldMetric& dy) { - 0.5 * differential_operators->DDY(covariantMetricTensor.Getg33(), dy)) + 0.5 * contravariantMetricTensor.Getg23() * differential_operators->DDZ(covariantMetricTensor.Getg33()); - G2_12 = 0.5 * contravariantMetricTensor.Getg12() - * differential_operators->DDY(covariantMetricTensor.Getg11(), dy) - + 0.5 * contravariantMetricTensor.Getg22() - * differential_operators->DDX(covariantMetricTensor.Getg22(), dx) - + 0.5 * contravariantMetricTensor.Getg23() - * (differential_operators->DDY(covariantMetricTensor.Getg13(), dy) - + differential_operators->DDX(covariantMetricTensor.Getg23(), dx) - - differential_operators->DDZ(covariantMetricTensor.Getg12())); - G2_13 = + G2_12_ = 0.5 * contravariantMetricTensor.Getg12() + * differential_operators->DDY(covariantMetricTensor.Getg11(), dy) + + 0.5 * contravariantMetricTensor.Getg22() + * differential_operators->DDX(covariantMetricTensor.Getg22(), dx) + + 0.5 * contravariantMetricTensor.Getg23() + * (differential_operators->DDY(covariantMetricTensor.Getg13(), dy) + + differential_operators->DDX(covariantMetricTensor.Getg23(), dx) + - differential_operators->DDZ(covariantMetricTensor.Getg12())); + G2_13_ = // 0.5 *g21*(differential_operators->DDZ(covariantMetricTensor.Getg11()) + differential_operators->DDX(covariantMetricTensor.Getg13()) - differential_operators->DDX(covariantMetricTensor.Getg13())) // which equals 0.5 * contravariantMetricTensor.Getg12() @@ -807,16 +808,16 @@ void Geometry::CalculateChristoffelSymbols(FieldMetric& dx, FieldMetric& dy) { // which equals + 0.5 * contravariantMetricTensor.Getg23() * differential_operators->DDX(covariantMetricTensor.Getg33(), dx); - G2_23 = 0.5 * contravariantMetricTensor.Getg12() - * (differential_operators->DDZ(covariantMetricTensor.Getg12()) - + differential_operators->DDY(covariantMetricTensor.Getg13(), dy) - - differential_operators->DDX(covariantMetricTensor.Getg23(), dx)) - + 0.5 * contravariantMetricTensor.Getg22() - * differential_operators->DDZ(covariantMetricTensor.Getg22()) - + 0.5 * contravariantMetricTensor.Getg23() - * differential_operators->DDY(covariantMetricTensor.Getg33(), dy); + G2_23_ = 0.5 * contravariantMetricTensor.Getg12() + * (differential_operators->DDZ(covariantMetricTensor.Getg12()) + + differential_operators->DDY(covariantMetricTensor.Getg13(), dy) + - differential_operators->DDX(covariantMetricTensor.Getg23(), dx)) + + 0.5 * contravariantMetricTensor.Getg22() + * differential_operators->DDZ(covariantMetricTensor.Getg22()) + + 0.5 * contravariantMetricTensor.Getg23() + * differential_operators->DDY(covariantMetricTensor.Getg33(), dy); - G3_11 = + G3_11_ = 0.5 * contravariantMetricTensor.Getg13() * differential_operators->DDX(covariantMetricTensor.Getg11(), dx) + contravariantMetricTensor.Getg23() @@ -825,15 +826,15 @@ void Geometry::CalculateChristoffelSymbols(FieldMetric& dx, FieldMetric& dy) { + contravariantMetricTensor.Getg33() * (differential_operators->DDX(covariantMetricTensor.Getg13(), dx) - 0.5 * differential_operators->DDZ(covariantMetricTensor.Getg11())); - G3_22 = contravariantMetricTensor.Getg13() - * (differential_operators->DDY(covariantMetricTensor.Getg12(), dy) - - 0.5 * differential_operators->DDX(covariantMetricTensor.Getg22(), dx)) - + 0.5 * contravariantMetricTensor.Getg23() - * differential_operators->DDY(covariantMetricTensor.Getg22(), dy) - + contravariantMetricTensor.Getg33() - * (differential_operators->DDY(covariantMetricTensor.Getg23(), dy) - - 0.5 * differential_operators->DDZ(covariantMetricTensor.Getg22())); - G3_33 = + G3_22_ = contravariantMetricTensor.Getg13() + * (differential_operators->DDY(covariantMetricTensor.Getg12(), dy) + - 0.5 * differential_operators->DDX(covariantMetricTensor.Getg22(), dx)) + + 0.5 * contravariantMetricTensor.Getg23() + * differential_operators->DDY(covariantMetricTensor.Getg22(), dy) + + contravariantMetricTensor.Getg33() + * (differential_operators->DDY(covariantMetricTensor.Getg23(), dy) + - 0.5 * differential_operators->DDZ(covariantMetricTensor.Getg22())); + G3_33_ = contravariantMetricTensor.Getg13() * (differential_operators->DDZ(covariantMetricTensor.Getg13()) - 0.5 * differential_operators->DDX(covariantMetricTensor.Getg33(), dx)) @@ -842,7 +843,7 @@ void Geometry::CalculateChristoffelSymbols(FieldMetric& dx, FieldMetric& dy) { - 0.5 * differential_operators->DDY(covariantMetricTensor.Getg33(), dy)) + 0.5 * contravariantMetricTensor.Getg33() * differential_operators->DDZ(covariantMetricTensor.Getg33()); - G3_12 = + G3_12_ = // 0.5 *g31*(differential_operators->DDY(covariantMetricTensor.Getg11()) + differential_operators->DDX(covariantMetricTensor.Getg12()) - differential_operators->DDX(covariantMetricTensor.Getg12())) // which equals to 0.5 * contravariantMetricTensor.Getg13() @@ -857,22 +858,22 @@ void Geometry::CalculateChristoffelSymbols(FieldMetric& dx, FieldMetric& dy) { * (differential_operators->DDY(covariantMetricTensor.Getg13(), dy)) + differential_operators->DDX(covariantMetricTensor.Getg23(), dx) - differential_operators->DDZ(covariantMetricTensor.Getg12()); - G3_13 = 0.5 * contravariantMetricTensor.Getg13() - * differential_operators->DDZ(covariantMetricTensor.Getg11()) - + 0.5 * contravariantMetricTensor.Getg23() - * (differential_operators->DDZ(covariantMetricTensor.Getg12()) - + differential_operators->DDX(covariantMetricTensor.Getg23(), dx) - - differential_operators->DDY(covariantMetricTensor.Getg13(), dy)) - + 0.5 * contravariantMetricTensor.Getg33() - * differential_operators->DDX(covariantMetricTensor.Getg33(), dx); - G3_23 = 0.5 * contravariantMetricTensor.Getg13() - * (differential_operators->DDZ(covariantMetricTensor.Getg12()) - + differential_operators->DDY(covariantMetricTensor.Getg13(), dy)) - - differential_operators->DDX(covariantMetricTensor.Getg23(), dx) - + 0.5 * contravariantMetricTensor.Getg23() - * differential_operators->DDZ(covariantMetricTensor.Getg22()) - + 0.5 * contravariantMetricTensor.Getg33() - * differential_operators->DDY(covariantMetricTensor.Getg33(), dy); + G3_13_ = 0.5 * contravariantMetricTensor.Getg13() + * differential_operators->DDZ(covariantMetricTensor.Getg11()) + + 0.5 * contravariantMetricTensor.Getg23() + * (differential_operators->DDZ(covariantMetricTensor.Getg12()) + + differential_operators->DDX(covariantMetricTensor.Getg23(), dx) + - differential_operators->DDY(covariantMetricTensor.Getg13(), dy)) + + 0.5 * contravariantMetricTensor.Getg33() + * differential_operators->DDX(covariantMetricTensor.Getg33(), dx); + G3_23_ = 0.5 * contravariantMetricTensor.Getg13() + * (differential_operators->DDZ(covariantMetricTensor.Getg12()) + + differential_operators->DDY(covariantMetricTensor.Getg13(), dy)) + - differential_operators->DDX(covariantMetricTensor.Getg23(), dx) + + 0.5 * contravariantMetricTensor.Getg23() + * differential_operators->DDZ(covariantMetricTensor.Getg22()) + + 0.5 * contravariantMetricTensor.Getg33() + * differential_operators->DDY(covariantMetricTensor.Getg33(), dy); } void Geometry::calcCovariant(CELL_LOC cell_location, const std::string& region) { @@ -1591,6 +1592,31 @@ const FieldMetric& Geometry::J() const { return this_J; } const FieldMetric& Geometry::Bxy() const { return this_Bxy; } +void Geometry::setG1_11(FieldMetric G1_11) { G1_11_ = G1_11; } +void Geometry::setG1_22(FieldMetric G1_22) { G1_11_ = G1_22; } +void Geometry::setG1_33(FieldMetric G1_33) { G1_11_ = G1_33; } +void Geometry::setG1_12(FieldMetric G1_12) { G1_11_ = G1_12; } +void Geometry::setG1_13(FieldMetric G1_13) { G1_11_ = G1_13; } +void Geometry::setG1_23(FieldMetric G1_23) { G1_11_ = G1_23; } + +void Geometry::setG2_11(FieldMetric G2_11) { G2_11_ = G2_11; } +void Geometry::setG2_22(FieldMetric G2_22) { G2_11_ = G2_22; } +void Geometry::setG2_33(FieldMetric G2_33) { G2_11_ = G2_33; } +void Geometry::setG2_12(FieldMetric G2_12) { G2_11_ = G2_12; } +void Geometry::setG2_13(FieldMetric G2_13) { G2_11_ = G2_13; } +void Geometry::setG2_23(FieldMetric G2_23) { G2_11_ = G2_23; } + +void Geometry::setG3_11(FieldMetric G3_11) { G3_11_ = G3_11; } +void Geometry::setG3_22(FieldMetric G3_22) { G3_11_ = G3_22; } +void Geometry::setG3_33(FieldMetric G3_33) { G3_11_ = G3_33; } +void Geometry::setG3_12(FieldMetric G3_12) { G3_11_ = G3_12; } +void Geometry::setG3_13(FieldMetric G3_13) { G3_11_ = G3_13; } +void Geometry::setG3_23(FieldMetric G3_23) { G3_11_ = G3_23; } + +void Geometry::setG1(FieldMetric G1) { G1_ = G1; } +void Geometry::setG2(FieldMetric G2) { G2_ = G2; } +void Geometry::setG3(FieldMetric G3) { G3_ = G3; } + void Geometry::setJ(FieldMetric J) { //TODO: Calculate J and check value is close this_J = J; From 6f655c0e7ebd05169ebbbd28e88544ec61d59e27 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 29 Nov 2023 18:21:38 +0000 Subject: [PATCH 216/491] Update to use getters for Christoffel symbols. --- src/mesh/coordinates.cxx | 9 +++++---- src/mesh/coordinates_accessor.cxx | 8 +++++--- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 48614457c7..3d15c4e3ea 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -1392,8 +1392,9 @@ Field3D Coordinates::Delp2(const Field3D& f, CELL_LOC outloc, bool useFFT) { } } } else { - result = G1 * ::DDX(f, outloc) + G3 * ::DDZ(f, outloc) + g11() * ::D2DX2(f, outloc) - + g33() * ::D2DZ2(f, outloc) + 2 * g13() * ::D2DXDZ(f, outloc); + result = G1() * ::DDX(f, outloc) + G3() * ::DDZ(f, outloc) + + g11() * ::D2DX2(f, outloc) + g33() * ::D2DZ2(f, outloc) + + 2 * g13() * ::D2DXDZ(f, outloc); } ASSERT2(result.getLocation() == outloc) @@ -1483,7 +1484,7 @@ Coordinates::FieldMetric Coordinates::Laplace(const Field2D& f, CELL_LOC outloc, TRACE("Coordinates::Laplace( Field2D )"); ASSERT1(location == outloc || outloc == CELL_DEFAULT) - return G1 * DDX(f, outloc) + G2 * DDY(f, outloc) + g11() * D2DX2(f, outloc) + return G1() * DDX(f, outloc) + G2() * DDY(f, outloc) + g11() * D2DX2(f, outloc) + g22() * D2DY2(f, outloc) + 2.0 * g12() * D2DXDY(f, outloc, "DEFAULT", "RGN_NOBNDRY", dfdy_boundary_conditions, @@ -1496,7 +1497,7 @@ Field3D Coordinates::Laplace(const Field3D& f, CELL_LOC outloc, TRACE("Coordinates::Laplace( Field3D )"); ASSERT1(location == outloc || outloc == CELL_DEFAULT) - return G1 * ::DDX(f, outloc) + G2 * ::DDY(f, outloc) + G3 * ::DDZ(f, outloc) + return G1() * ::DDX(f, outloc) + G2() * ::DDY(f, outloc) + G3() * ::DDZ(f, outloc) + g11() * D2DX2(f, outloc) + g22() * D2DY2(f, outloc) + g33() * D2DZ2(f, outloc) + 2.0 * (g12() diff --git a/src/mesh/coordinates_accessor.cxx b/src/mesh/coordinates_accessor.cxx index 9337b48636..c1fb1d4c7a 100644 --- a/src/mesh/coordinates_accessor.cxx +++ b/src/mesh/coordinates_accessor.cxx @@ -1,6 +1,6 @@ #include "bout/coordinates_accessor.hxx" -#include "bout/metricTensor.hxx" #include "bout/mesh.hxx" +#include "bout/metricTensor.hxx" #include @@ -55,11 +55,13 @@ CoordinatesAccessor::CoordinatesAccessor(const Coordinates* coords) { data[stripe_size * ind.ind + static_cast(Offset::J)] = coords->J()[ind]; data[stripe_size * ind.ind + static_cast(Offset::B)] = coords->Bxy()[ind]; - data[stripe_size * ind.ind + static_cast(Offset::Byup)] = coords->Bxy().yup()[ind]; + data[stripe_size * ind.ind + static_cast(Offset::Byup)] = + coords->Bxy().yup()[ind]; data[stripe_size * ind.ind + static_cast(Offset::Bydown)] = coords->Bxy().ydown()[ind]; - COPY_STRIPE(G1, G3); + data[stripe_size * ind.ind + static_cast(Offset::G1)] = coords->G1()[ind]; + data[stripe_size * ind.ind + static_cast(Offset::G3)] = coords->G3()[ind]; // COPY_STRIPE(g11, g12, g13, g22, g23, g33); data[stripe_size * ind.ind + static_cast(Offset::g11)] = coords->g11()[ind]; data[stripe_size * ind.ind + static_cast(Offset::g12)] = coords->g12()[ind]; From 74bf09c9808069b512e73844d7db3b250b22c8e8 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Thu, 30 Nov 2023 09:27:49 +0000 Subject: [PATCH 217/491] Add a version of communicate() to handle the case where all elements of the parameter pack are references. --- src/mesh/coordinates.cxx | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 3d15c4e3ea..50c7caeafd 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -37,6 +37,17 @@ void communicate(T& t, Ts... ts) { t.getMesh()->wait(h); } +template +// Use sendY()/sendX() and wait() instead of Mesh::communicate() to ensure we +// don't try to calculate parallel slices as Coordinates are not constructed yet +void communicate(T& t, Ts&... ts) { + FieldGroup g(t, ts...); + auto h = t.getMesh()->sendY(g); + t.getMesh()->wait(h); + h = t.getMesh()->sendX(g); + t.getMesh()->wait(h); +} + #if BOUT_USE_METRIC_3D Field3D interpolateAndExtrapolate(const Field3D& f_, CELL_LOC location, bool extrapolate_x, bool extrapolate_y, From 11009acd47338bf11b36cec5d918092631d0ccee Mon Sep 17 00:00:00 2001 From: tomc271 Date: Thu, 30 Nov 2023 09:38:10 +0000 Subject: [PATCH 218/491] Didn't work - using instead the existing technique of copying the first element to a 'tmp' variable. --- src/mesh/coordinates.cxx | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 50c7caeafd..a00c992956 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -26,21 +26,11 @@ // use anonymous namespace so this utility function is not available outside this file namespace { -template -// Use sendY()/sendX() and wait() instead of Mesh::communicate() to ensure we -// don't try to calculate parallel slices as Coordinates are not constructed yet -void communicate(T& t, Ts... ts) { - FieldGroup g(t, ts...); - auto h = t.getMesh()->sendY(g); - t.getMesh()->wait(h); - h = t.getMesh()->sendX(g); - t.getMesh()->wait(h); -} template // Use sendY()/sendX() and wait() instead of Mesh::communicate() to ensure we // don't try to calculate parallel slices as Coordinates are not constructed yet -void communicate(T& t, Ts&... ts) { +void communicate(T& t, Ts... ts) { FieldGroup g(t, ts...); auto h = t.getMesh()->sendY(g); t.getMesh()->wait(h); @@ -1688,9 +1678,10 @@ void Coordinates::communicateChristoffelSymbolTerms() { output_progress.write("\tCommunicating connection terms\n"); - communicate(G1_11(), G1_22(), G1_33(), G1_12(), G1_13(), G1_23(), G2_11(), G2_22(), - G2_33(), G2_12(), G2_13(), G2_23(), G3_11(), G3_22(), G3_33(), G3_12(), - G3_13(), G3_23(), G1(), G2(), G3()); + auto tmp = G1_11(); // TODO: There must be a better way than this! + communicate(tmp, G1_22(), G1_33(), G1_12(), G1_13(), G1_23(), G2_11(), G2_22(), G2_33(), + G2_12(), G2_13(), G2_23(), G3_11(), G3_22(), G3_33(), G3_12(), G3_13(), + G3_23(), G1(), G2(), G3()); } //const MetricTensor& Coordinates::getCovariantMetricTensor() const { From 3a599e9009b7ba8a2a8f94a16e7202655e65d0ad Mon Sep 17 00:00:00 2001 From: tomc271 Date: Thu, 30 Nov 2023 09:41:09 +0000 Subject: [PATCH 219/491] Add setters for G1, G2, G2 on Coordinates class. --- include/bout/coordinates.hxx | 4 ++++ src/mesh/coordinates.cxx | 4 ++++ tests/unit/mesh/test_coordinates_accessor.cxx | 12 +++++++++--- tests/unit/test_extras.hxx | 9 ++++++--- 4 files changed, 23 insertions(+), 6 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index a2a22f8345..a51ca4aba6 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -255,6 +255,10 @@ public: const FieldMetric& G2() const; const FieldMetric& G3() const; + void setG1(FieldMetric G1); + void setG2(FieldMetric G2); + void setG3(FieldMetric G3); + private: int nz; // Size of mesh in Z. This is mesh->ngz-1 Mesh* localmesh; diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index a00c992956..7084c8d53b 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -1655,6 +1655,10 @@ const FieldMetric& Coordinates::G1() const { return geometry.G1(); } const FieldMetric& Coordinates::G2() const { return geometry.G2(); } const FieldMetric& Coordinates::G3() const { return geometry.G3(); } +void Coordinates::setG1(FieldMetric G1) { geometry.setG1(G1); } +void Coordinates::setG2(FieldMetric G2) { geometry.setG2(G2); } +void Coordinates::setG3(FieldMetric G3) { geometry.setG3(G3); } + void Coordinates::setJ(FieldMetric J) { //TODO: Calculate J and check value is close geometry.setJ(J); diff --git a/tests/unit/mesh/test_coordinates_accessor.cxx b/tests/unit/mesh/test_coordinates_accessor.cxx index ccdd031e4e..ad8c49f233 100644 --- a/tests/unit/mesh/test_coordinates_accessor.cxx +++ b/tests/unit/mesh/test_coordinates_accessor.cxx @@ -84,7 +84,9 @@ TEST_F(CoordinatesAccessorTest, ClearBoth) { FieldMetric{0.0}, // ShiftTorsion FieldMetric{0.0}}; // IntShiftTorsion // Need to set geometry information - coords.G1 = coords.G2 = coords.G3 = 0.2; + coords.setG1(0.2); + coords.setG2(0.2); + coords.setG3(0.2); coords.non_uniform = true; coords.d1_dx = coords.d1_dy = coords.d1_dz = 0.1; #if BOUT_USE_METRIC_3D @@ -124,7 +126,9 @@ TEST_F(CoordinatesAccessorTest, ClearOneTwo) { FieldMetric{0.0}, // ShiftTorsion FieldMetric{0.0}}; // IntShiftTorsion // Need to set geometry information - coords.G1 = coords.G2 = coords.G3 = 0.2; + coords.setG1(0.2); + coords.setG2(0.2); + coords.setG3(0.2); coords.non_uniform = true; coords.d1_dx = coords.d1_dy = coords.d1_dz = 0.1; #if BOUT_USE_METRIC_3D @@ -166,7 +170,9 @@ TEST_F(CoordinatesAccessorTest, ClearTwoOneNone) { FieldMetric{0.0}, // ShiftTorsion FieldMetric{0.0}}; // IntShiftTorsion // Need to set geometry information - coords.G1 = coords.G2 = coords.G3 = 0.2; + coords.setG1(0.2); + coords.setG2(0.2); + coords.setG3(0.2); coords.non_uniform = true; coords.d1_dx = coords.d1_dy = coords.d1_dz = 0.1; #if BOUT_USE_METRIC_3D diff --git a/tests/unit/test_extras.hxx b/tests/unit/test_extras.hxx index 35262d23cd..eba66653c8 100644 --- a/tests/unit/test_extras.hxx +++ b/tests/unit/test_extras.hxx @@ -442,7 +442,9 @@ public: // Set some auxilliary variables // Usually set in calculateGeometry() // Note: For testing these are set to non-zero values - test_coords->G1 = test_coords->G2 = test_coords->G3 = 0.1; + test_coords->setG1(0.1); + test_coords->setG2(0.1); + test_coords->setG3(0.1); // Set nonuniform corrections test_coords->non_uniform = true; @@ -485,8 +487,9 @@ public: Field2D{0.0, mesh_staggered}); // Set some auxilliary variables - test_coords_staggered->G1 = test_coords_staggered->G2 = test_coords_staggered->G3 = - 0.1; + test_coords_staggered->setG1(0.1); + test_coords_staggered->setG2(0.1); + test_coords_staggered->setG3(0.1); // Set nonuniform corrections test_coords_staggered->non_uniform = true; From f477017f11f4a57fa6102b268112df9f8f341515 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Thu, 30 Nov 2023 10:35:36 +0000 Subject: [PATCH 220/491] Make Christoffel symbols private in cython interface. --- tools/pylib/_boutpp_build/boutpp.pyx.jinja | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tools/pylib/_boutpp_build/boutpp.pyx.jinja b/tools/pylib/_boutpp_build/boutpp.pyx.jinja index f6e125bda7..7bcdb9a104 100644 --- a/tools/pylib/_boutpp_build/boutpp.pyx.jinja +++ b/tools/pylib/_boutpp_build/boutpp.pyx.jinja @@ -833,10 +833,10 @@ cdef class Coordinates: cdef {{ metric_field }} Bxy cdef {{ metric_field }} g11, g22, g33, g12, g13, g23 cdef {{ metric_field }} g_11, g_22, g_33, g_12, g_13, g_23 - cdef public {{ metric_field }} G1_11, G1_22, G1_33, G1_12, G1_13, G1_23 - cdef public {{ metric_field }} G2_11, G2_22, G2_33, G2_12, G2_13, G2_23 - cdef public {{ metric_field }} G3_11, G3_22, G3_33, G3_12, G3_13, G3_23 - cdef public {{ metric_field }} G1, G2, G3 + cdef {{ metric_field }} G1_11, G1_22, G1_33, G1_12, G1_13, G1_23 + cdef {{ metric_field }} G2_11, G2_22, G2_33, G2_12, G2_13, G2_23 + cdef {{ metric_field }} G3_11, G3_22, G3_33, G3_12, G3_13, G3_23 + cdef {{ metric_field }} G1, G2, G3 cdef public {{ metric_field }} ShiftTorsion cdef public {{ metric_field }} IntShiftTorsion @@ -845,7 +845,7 @@ cdef class Coordinates: self.isSelfOwned = False def _setmembers(self): -{% for f in "dx", "dy", "dz", "G1_11", "G1_22", "G1_33", "G1_12", "G1_13", "G1_23", "G2_11", "G2_22", "G2_33", "G2_12", "G2_13", "G2_23", "G3_11", "G3_22", "G3_33", "G3_12", "G3_13", "G3_23", "G1", "G2", "G3", "ShiftTorsion", "IntShiftTorsion" %} +{% for f in "dx", "dy", "dz", "ShiftTorsion", "IntShiftTorsion" %} self.{{f}} = {{ metric_field.fdd }}FromPtr(&self.cobj.{{f}}) {% endfor %} From fb5a5c00c76e630a337b86361b75b06b4c3d155e Mon Sep 17 00:00:00 2001 From: tomc271 Date: Thu, 30 Nov 2023 10:53:19 +0000 Subject: [PATCH 221/491] Remove commented-out code. --- include/bout/coordinates.hxx | 19 - include/bout/differential_operators.hxx | 17 - include/bout/geometry.hxx | 12 - src/mesh/coordinates.cxx | 188 ---- src/mesh/differential_operators.cxx | 155 --- src/mesh/geometry.cxx | 1261 ----------------------- 6 files changed, 1652 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index a51ca4aba6..a4a08e09bf 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -121,7 +121,6 @@ public: FieldMetric IntShiftTorsion; ///< Integrated shear (I in BOUT notation) const MetricTensor& getContravariantMetricTensor() const; - // const MetricTensor& getCovariantMetricTensor() const; /// Calculate differential geometry quantities from the metric tensor int calculateGeometry(bool recalculate_staggered = true, @@ -131,7 +130,6 @@ public: /// Invert covariant metric to get contravariant components void calcContravariant(const std::string& region = "RGN_ALL"); void jacobian(); ///< Calculate J and Bxy - // void CalculateChristoffelSymbols(); /// Calculate Christoffel symbol terms /////////////////////////////////////////////////////////// // Parallel transforms @@ -273,18 +271,10 @@ private: /// `Coordinates::calculateGeometry` is called mutable std::unique_ptr zlength_cache{nullptr}; - // /// Cache variable for Grad2_par2 - // mutable std::map> Grad2_par2_DDY_invSgCache; - // mutable std::unique_ptr invSgCache{nullptr}; - /// Set the parallel (y) transform from the options file. /// Used in the constructor to create the transform object. void setParallelTransform(Options* options); - // const FieldMetric& invSg() const; - // const FieldMetric& Grad2_par2_DDY_invSg(CELL_LOC outloc, - // const std::string& method) const; - // check that covariant tensors are positive (if expected) and finite (always) void checkCovariant(); // check that contravariant tensors are positive (if expected) and finite (always) @@ -303,15 +293,6 @@ private: bool extrapolate_x = false, bool extrapolate_y = false, bool no_extra_interpolate = false, ParallelTransform* pParallelTransform = nullptr); - // MetricTensor contravariantMetricTensor; - // MetricTensor covariantMetricTensor; - - // FieldMetric this_J; - // FieldMetric this_Bxy; ///< Magnitude of B = nabla z times nabla x - - // FieldMetric recalculateJacobian(); - // FieldMetric recalculateBxy(); - void communicateChristoffelSymbolTerms(); }; diff --git a/include/bout/differential_operators.hxx b/include/bout/differential_operators.hxx index 5795b4fc97..c1b2bfffb7 100644 --- a/include/bout/differential_operators.hxx +++ b/include/bout/differential_operators.hxx @@ -3,7 +3,6 @@ #define BOUT_DIFFERENTIALOPERATORS_HXX #include "bout/metricTensor.hxx" -//#include "bout/index_derivs_interface.hxx" class DifferentialOperators { @@ -12,9 +11,6 @@ class DifferentialOperators { public: DifferentialOperators(Mesh* mesh); - // DifferentialOperators(DifferentialOperators operators, - // DifferentialOperators::FieldMetric& dx); - Field2D DDX(const Field2D& f, const Field2D& dx, CELL_LOC outloc = CELL_DEFAULT, const std::string& method = "DEFAULT", const std::string& region = "RGN_NOBNDRY"); @@ -78,19 +74,6 @@ public: MetricTensor& covariantMetricTensor, CELL_LOC outloc = CELL_DEFAULT, const std::string& method = "DEFAULT"); - // // Perpendicular Laplacian operator, using only X-Z derivatives - // // NOTE: This might be better bundled with the Laplacian inversion code - // // since it makes use of the same coefficients and FFT routines - // FieldMetric Delp2(const Field2D& f, const Field2D& g11, const Field2D& G1, - // CELL_LOC outloc = CELL_DEFAULT, bool useFFT = true); - // - // Field3D Delp2(const Field3D& f, MetricTensor& covariantMetricTensor, const Field3D& G1, - // const Field3D& G3, CELL_LOC outloc = CELL_DEFAULT, bool useFFT = true); - // - // FieldPerp Delp2(const FieldPerp& f, CELL_LOC outloc = CELL_DEFAULT, bool useFFT = true); - - // Full parallel Laplacian operator on scalar field - // Laplace_par(f) = Div( b (b dot Grad(f)) ) Field2D Laplace_par(const Field2D& f, const Field2D& g22, const Field2D& J, const Field2D& dy, CELL_LOC outloc = CELL_DEFAULT); diff --git a/include/bout/geometry.hxx b/include/bout/geometry.hxx index c12af2152c..53ff4c4d37 100644 --- a/include/bout/geometry.hxx +++ b/include/bout/geometry.hxx @@ -35,8 +35,6 @@ #include "differential_operators.hxx" #include "metricTensor.hxx" -//#include "bout/utils.hxx" -//#include using FieldMetric = MetricTensor::FieldMetric; @@ -147,8 +145,6 @@ public: /// Invert covariant metric to get contravariant components void calcContravariant(CELL_LOC cell_location, const std::string& region = "RGN_ALL"); - // void jacobian(bool extrapolate_x, bool extrapolate_y); ///< Calculate J and Bxy - /// Calculate Christoffel symbol terms void CalculateChristoffelSymbols(FieldMetric& dx, FieldMetric& dy); @@ -168,11 +164,6 @@ public: std::function function); private: - // Coordinates& coordinates; - - // int calculateGeometry(FieldMetric& dx, FieldMetric& dy, FieldMetric& dz, - // bool recalculate_staggered, bool force_interpolate_from_centre); - /// Christoffel symbol of the second kind (connection coefficients) FieldMetric G1_11_, G1_22_, G1_33_, G1_12_, G1_13_, G1_23_; FieldMetric G2_11_, G2_22_, G2_33_, G2_12_, G2_13_, G2_23_; @@ -187,9 +178,6 @@ private: FieldMetric this_Bxy; ///< Magnitude of B = nabla z times nabla x DifferentialOperators* differential_operators; - - // template - // void communicate(T& t, Ts... ts); }; #endif // __GEOMETRY_H__ diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 7084c8d53b..1267b5c12a 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -304,10 +304,6 @@ Coordinates::Coordinates(Mesh* mesh, FieldMetric dx, FieldMetric dy, FieldMetric Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, const Coordinates* coords_in, bool force_interpolate_from_centre) : dx(1., mesh), dy(1., mesh), dz(1., mesh), d1_dx(mesh), d1_dy(mesh), d1_dz(mesh), - // G1_11(mesh), G1_22(mesh), G1_33(mesh), G1_12(mesh), G1_13(mesh), G1_23(mesh), - // G2_11(mesh), G2_22(mesh), G2_33(mesh), G2_12(mesh), G2_13(mesh), G2_23(mesh), - // G3_11(mesh), G3_22(mesh), G3_33(mesh), G3_12(mesh), G3_13(mesh), G3_23(mesh), - // G1(mesh), G2(mesh), G3(mesh), ShiftTorsion(mesh), IntShiftTorsion(mesh), localmesh(mesh), location(loc), differential_operators(DifferentialOperators(mesh)), geometry(Geometry(mesh, &differential_operators)) { @@ -878,153 +874,6 @@ int Coordinates::calculateGeometry(bool recalculate_staggered, return 0; } -//void Coordinates::CalculateChristoffelSymbols() { -// // Calculate Christoffel symbol terms (18 independent values) -// // Note: This calculation is completely general: metric -// // tensor can be 2D or 3D. For 2D, all DDZ terms are zero -// -// G1_11 = 0.5 * contravariantMetricTensor.Getg11() * DDX(covariantMetricTensor.Getg11()) -// + contravariantMetricTensor.Getg12() -// * (DDX(covariantMetricTensor.Getg12()) -// - 0.5 * DDY(covariantMetricTensor.Getg11())) -// + contravariantMetricTensor.Getg13() -// * (DDX(covariantMetricTensor.Getg13()) -// - 0.5 * DDZ(covariantMetricTensor.Getg11())); -// G1_22 = contravariantMetricTensor.Getg11() -// * (DDY(covariantMetricTensor.Getg12()) -// - 0.5 * DDX(covariantMetricTensor.Getg22())) -// + 0.5 * contravariantMetricTensor.Getg12() * DDY(covariantMetricTensor.Getg22()) -// + contravariantMetricTensor.Getg13() -// * (DDY(covariantMetricTensor.Getg23()) -// - 0.5 * DDZ(covariantMetricTensor.Getg22())); -// G1_33 = -// contravariantMetricTensor.Getg11() -// * (DDZ(covariantMetricTensor.Getg13()) -// - 0.5 * DDX(covariantMetricTensor.Getg33())) -// + contravariantMetricTensor.Getg12() -// * (DDZ(covariantMetricTensor.Getg23()) -// - 0.5 * DDY(covariantMetricTensor.Getg33())) -// + 0.5 * contravariantMetricTensor.Getg13() * DDZ(covariantMetricTensor.Getg33()); -// G1_12 = -// 0.5 * contravariantMetricTensor.Getg11() * DDY(covariantMetricTensor.Getg11()) -// + 0.5 * contravariantMetricTensor.Getg12() * DDX(covariantMetricTensor.Getg22()) -// + 0.5 * contravariantMetricTensor.Getg13() -// * (DDY(covariantMetricTensor.Getg13()) + DDX(covariantMetricTensor.Getg23()) -// - DDZ(covariantMetricTensor.Getg12())); -// G1_13 = -// 0.5 * contravariantMetricTensor.Getg11() * DDZ(covariantMetricTensor.Getg11()) -// + 0.5 * contravariantMetricTensor.Getg12() -// * (DDZ(covariantMetricTensor.Getg12()) + DDX(covariantMetricTensor.Getg23()) -// - DDY(covariantMetricTensor.Getg13())) -// + 0.5 * contravariantMetricTensor.Getg13() * DDX(covariantMetricTensor.Getg33()); -// G1_23 = -// 0.5 * contravariantMetricTensor.Getg11() -// * (DDZ(covariantMetricTensor.Getg12()) + DDY(covariantMetricTensor.Getg13()) -// - DDX(covariantMetricTensor.Getg23())) -// + 0.5 * contravariantMetricTensor.Getg12() -// * (DDZ(covariantMetricTensor.Getg22()) + DDY(covariantMetricTensor.Getg23()) -// - DDY(covariantMetricTensor.Getg23())) -// // + 0.5 *g13*(DDZ(g_32) + DDY(g_33) - DDZ(g_23)); -// // which equals -// + 0.5 * contravariantMetricTensor.Getg13() * DDY(covariantMetricTensor.Getg33()); -// -// G2_11 = 0.5 * contravariantMetricTensor.Getg12() * DDX(covariantMetricTensor.Getg11()) -// + contravariantMetricTensor.Getg22() -// * (DDX(covariantMetricTensor.Getg12()) -// - 0.5 * DDY(covariantMetricTensor.Getg11())) -// + contravariantMetricTensor.Getg23() -// * (DDX(covariantMetricTensor.Getg13()) -// - 0.5 * DDZ(covariantMetricTensor.Getg11())); -// G2_22 = contravariantMetricTensor.Getg12() -// * (DDY(covariantMetricTensor.Getg12()) -// - 0.5 * DDX(covariantMetricTensor.Getg22())) -// + 0.5 * contravariantMetricTensor.Getg22() * DDY(covariantMetricTensor.Getg22()) -// + contravariantMetricTensor.Getg23() -// * (DDY(contravariantMetricTensor.Getg23()) -// - 0.5 * DDZ(covariantMetricTensor.Getg22())); -// G2_33 = -// contravariantMetricTensor.Getg12() -// * (DDZ(covariantMetricTensor.Getg13()) -// - 0.5 * DDX(covariantMetricTensor.Getg33())) -// + contravariantMetricTensor.Getg22() -// * (DDZ(covariantMetricTensor.Getg23()) -// - 0.5 * DDY(covariantMetricTensor.Getg33())) -// + 0.5 * contravariantMetricTensor.Getg23() * DDZ(covariantMetricTensor.Getg33()); -// G2_12 = -// 0.5 * contravariantMetricTensor.Getg12() * DDY(covariantMetricTensor.Getg11()) -// + 0.5 * contravariantMetricTensor.Getg22() * DDX(covariantMetricTensor.Getg22()) -// + 0.5 * contravariantMetricTensor.Getg23() -// * (DDY(covariantMetricTensor.Getg13()) + DDX(covariantMetricTensor.Getg23()) -// - DDZ(covariantMetricTensor.Getg12())); -// G2_13 = -// // 0.5 *g21*(DDZ(covariantMetricTensor.Getg11()) + DDX(covariantMetricTensor.Getg13()) - DDX(covariantMetricTensor.Getg13())) -// // which equals -// 0.5 * contravariantMetricTensor.Getg12() -// * (DDZ(covariantMetricTensor.Getg11()) + DDX(covariantMetricTensor.Getg13()) -// - DDX(covariantMetricTensor.Getg13())) -// // + 0.5 *g22*(DDZ(covariantMetricTensor.Getg21()) + DDX(covariantMetricTensor.Getg23()) - DDY(covariantMetricTensor.Getg13())) -// // which equals -// + 0.5 * contravariantMetricTensor.Getg22() -// * (DDZ(covariantMetricTensor.Getg12()) + DDX(covariantMetricTensor.Getg23()) -// - DDY(covariantMetricTensor.Getg13())) -// // + 0.5 *g23*(DDZ(covariantMetricTensor.Getg31()) + DDX(covariantMetricTensor.Getg33()) - DDZ(g_13)); -// // which equals -// + 0.5 * contravariantMetricTensor.Getg23() * DDX(covariantMetricTensor.Getg33()); -// G2_23 = -// 0.5 * contravariantMetricTensor.Getg12() -// * (DDZ(covariantMetricTensor.Getg12()) + DDY(covariantMetricTensor.Getg13()) -// - DDX(covariantMetricTensor.Getg23())) -// + 0.5 * contravariantMetricTensor.Getg22() * DDZ(covariantMetricTensor.Getg22()) -// + 0.5 * contravariantMetricTensor.Getg23() * DDY(covariantMetricTensor.Getg33()); -// -// G3_11 = 0.5 * contravariantMetricTensor.Getg13() * DDX(covariantMetricTensor.Getg11()) -// + contravariantMetricTensor.Getg23() -// * (DDX(covariantMetricTensor.Getg12()) -// - 0.5 * DDY(covariantMetricTensor.Getg11())) -// + contravariantMetricTensor.Getg33() -// * (DDX(covariantMetricTensor.Getg13()) -// - 0.5 * DDZ(covariantMetricTensor.Getg11())); -// G3_22 = contravariantMetricTensor.Getg13() -// * (DDY(covariantMetricTensor.Getg12()) -// - 0.5 * DDX(covariantMetricTensor.Getg22())) -// + 0.5 * contravariantMetricTensor.Getg23() * DDY(covariantMetricTensor.Getg22()) -// + contravariantMetricTensor.Getg33() -// * (DDY(covariantMetricTensor.Getg23()) -// - 0.5 * DDZ(covariantMetricTensor.Getg22())); -// G3_33 = -// contravariantMetricTensor.Getg13() -// * (DDZ(covariantMetricTensor.Getg13()) -// - 0.5 * DDX(covariantMetricTensor.Getg33())) -// + contravariantMetricTensor.Getg23() -// * (DDZ(covariantMetricTensor.Getg23()) -// - 0.5 * DDY(covariantMetricTensor.Getg33())) -// + 0.5 * contravariantMetricTensor.Getg33() * DDZ(covariantMetricTensor.Getg33()); -// G3_12 = -// // 0.5 *g31*(DDY(covariantMetricTensor.Getg11()) + DDX(covariantMetricTensor.Getg12()) - DDX(covariantMetricTensor.Getg12())) -// // which equals to -// 0.5 * contravariantMetricTensor.Getg13() * DDY(covariantMetricTensor.Getg11()) -// // + 0.5 *g32*(DDY(covariantMetricTensor.Getg21()) + DDX(covariantMetricTensor.Getg22()) - DDY(covariantMetricTensor.Getg12())) -// // which equals to -// + 0.5 * contravariantMetricTensor.Getg23() * DDX(covariantMetricTensor.Getg22()) -// //+ 0.5 *g33*(DDY(covariantMetricTensor.Getg31()) + DDX(covariantMetricTensor.Getg32()) - DDZ(covariantMetricTensor.Getg12())); -// // which equals to -// + 0.5 * contravariantMetricTensor.Getg33() -// * (DDY(covariantMetricTensor.Getg13()) + DDX(covariantMetricTensor.Getg23()) -// - DDZ(covariantMetricTensor.Getg12())); -// G3_13 = -// 0.5 * contravariantMetricTensor.Getg13() * DDZ(covariantMetricTensor.Getg11()) -// + 0.5 * contravariantMetricTensor.Getg23() -// * (DDZ(covariantMetricTensor.Getg12()) + DDX(covariantMetricTensor.Getg23()) -// - DDY(covariantMetricTensor.Getg13())) -// + 0.5 * contravariantMetricTensor.Getg33() * DDX(covariantMetricTensor.Getg33()); -// G3_23 = -// 0.5 * contravariantMetricTensor.Getg13() -// * (DDZ(covariantMetricTensor.Getg12()) + DDY(covariantMetricTensor.Getg13()) -// - DDX(covariantMetricTensor.Getg23())) -// + 0.5 * contravariantMetricTensor.Getg23() * DDZ(covariantMetricTensor.Getg22()) -// + 0.5 * contravariantMetricTensor.Getg33() * DDY(covariantMetricTensor.Getg33()); -//} - void Coordinates::calcCovariant(const std::string& region) { TRACE("Coordinates::calcCovariant"); geometry.calcCovariant(location, region); @@ -1041,8 +890,6 @@ void Coordinates::jacobian() { const bool extrapolate_x = not localmesh->sourceHasXBoundaryGuards(); const bool extrapolate_y = not localmesh->sourceHasYBoundaryGuards(); - // geometry.jacobian(extrapolate_x, extrapolate_y); - TRACE("Geometry::jacobian"); try { const auto jacobian = geometry.recalculateJacobian(); @@ -1569,35 +1416,6 @@ Field2D Coordinates::Laplace_perpXY([[maybe_unused]] const Field2D& A, #endif } -//const Coordinates::FieldMetric& Coordinates::invSg() const { -// if (invSgCache == nullptr) { -// auto ptr = std::make_unique(); -// (*ptr) = 1.0 / sqrt(g22()); -// invSgCache = std::move(ptr); -// } -// return *invSgCache; -//} - -//const Coordinates::FieldMetric& -//Coordinates::Grad2_par2_DDY_invSg(CELL_LOC outloc, const std::string& method) const { -// -// if (auto search = Grad2_par2_DDY_invSgCache.find(method); -// search != Grad2_par2_DDY_invSgCache.end()) { -// return *search->second; -// } -// invSg(); -// -// // Communicate to get parallel slices -// localmesh->communicate(*invSgCache); -// invSgCache->applyParallelBoundary("parallel_neumann"); -// -// // cache -// auto ptr = std::make_unique(); -// *ptr = DDY(*invSgCache, outloc, method) * invSg(); -// Grad2_par2_DDY_invSgCache[method] = std::move(ptr); -// return *Grad2_par2_DDY_invSgCache[method]; -//} - void Coordinates::checkCovariant() { return geometry.checkCovariant(localmesh->ystart); } void Coordinates::checkContravariant() { geometry.checkContravariant(localmesh->ystart); } @@ -1687,9 +1505,3 @@ void Coordinates::communicateChristoffelSymbolTerms() { G2_12(), G2_13(), G2_23(), G3_11(), G3_22(), G3_33(), G3_12(), G3_13(), G3_23(), G1(), G2(), G3()); } - -//const MetricTensor& Coordinates::getCovariantMetricTensor() const { -// return geometry.getCovariantMetricTensor(); -//} - -//const Coordinates::FieldMetric& Coordinates::invSg() const {} diff --git a/src/mesh/differential_operators.cxx b/src/mesh/differential_operators.cxx index 1c02c5ab90..2f6085d501 100644 --- a/src/mesh/differential_operators.cxx +++ b/src/mesh/differential_operators.cxx @@ -1,11 +1,6 @@ #include "bout/differential_operators.hxx" -//#include "bout/field2d.hxx" -//#include "bout/field3d.hxx" #include "bout/mesh.hxx" -//#include "bout/metricTensor.hxx" -//#include "bout/index_derivs_interface.hxx" -//#include "bout/paralleltransform.hxx" #include DifferentialOperators::DifferentialOperators(Mesh* mesh) : mesh(mesh) {} @@ -176,156 +171,6 @@ Field3D DifferentialOperators::Grad2_par2(const Field3D& f, const FieldMetric& d return result; } -/////////////////////////////////////////////////////////// -//// perpendicular Laplacian operator -// -//#include // Delp2 uses same coefficients as inversion code -// -//FieldMetric DifferentialOperators::Delp2(const Field2D& f, const Field2D& g11, -// const Field2D& G1, CELL_LOC outloc, -// bool UNUSED(useFFT)) { -// TRACE("DifferentialOperators::Delp2( Field2D )"); -// ASSERT1(location == outloc || outloc == CELL_DEFAULT) -// -// auto result = G1 * DDX(f, outloc) + g11 * D2DX2(f, outloc); -// -// return result; -//} - -//Field3D DifferentialOperators::Delp2(const Field3D& f, -// MetricTensor& covariantMetricTensor, -// const Field3D& G1, const Field3D& G3, -// CELL_LOC outloc, bool useFFT) { -// TRACE("DifferentialOperators::Delp2( Field3D )"); -// -// if (outloc == CELL_DEFAULT) { -// outloc = f.getLocation(); -// } -// -// ASSERT1(location == outloc) -// ASSERT1(f.getLocation() == outloc) -// -// if (mesh->GlobalNx == 1 && mesh->GlobalNz == 1) { -// // copy mesh, location, etc -// return f * 0; -// } -// ASSERT2(mesh->xstart > 0) // Need at least one guard cell -// -// Field3D result{emptyFrom(f).setLocation(outloc)}; -// -// if (useFFT and not bout::build::use_metric_3d) { -// int ncz = mesh->LocalNz; -// -// // Allocate memory -// auto ft = Matrix(mesh->LocalNx, ncz / 2 + 1); -// auto delft = Matrix(mesh->LocalNx, ncz / 2 + 1); -// -// // Loop over y indices -// // Note: should not include y-guard or y-boundary points here as that would -// // use values from corner cells in dx, which may not be initialised. -// for (int jy = mesh->ystart; jy <= mesh->yend; jy++) { -// -// // Take forward FFT -// -// for (int jx = 0; jx < mesh->LocalNx; jx++) { -// rfft(&f(jx, jy, 0), ncz, &ft(jx, 0)); -// } -// -// // Loop over kz -// for (int jz = 0; jz <= ncz / 2; jz++) { -// -// // No smoothing in the x direction -// for (int jx = mesh->xstart; jx <= mesh->xend; jx++) { -// // Perform x derivative -// -// dcomplex a, b, c; -// laplace_tridag_coefs(jx, jy, jz, a, b, c, nullptr, nullptr, outloc); -// -// delft(jx, jz) = a * ft(jx - 1, jz) + b * ft(jx, jz) + c * ft(jx + 1, jz); -// } -// } -// -// // Reverse FFT -// for (int jx = mesh->xstart; jx <= mesh->xend; jx++) { -// -// irfft(&delft(jx, 0), ncz, &result(jx, jy, 0)); -// } -// } -// } else { -// result = G1 * ::DDX(f, outloc) + G3 * ::DDZ(f, outloc) -// + covariantMetricTensor.Getg11() * ::D2DX2(f, outloc) -// + covariantMetricTensor.Getg33() * ::D2DZ2(f, outloc) -// + 2 * covariantMetricTensor.Getg13() * ::D2DXDZ(f, outloc); -// } -// -// ASSERT2(result.getLocation() == outloc) -// -// return result; -//} - -//FieldPerp DifferentialOperators::Delp2(const FieldPerp& f, CELL_LOC outloc, bool useFFT) { -// TRACE("DifferentialOperators::Delp2( FieldPerp )"); -// -// if (outloc == CELL_DEFAULT) { -// outloc = f.getLocation(); -// } -// -// ASSERT1(location == outloc) -// ASSERT1(f.getLocation() == outloc) -// -// if (mesh->GlobalNx == 1 && mesh->GlobalNz == 1) { -// // copy mesh, location, etc -// return f * 0; -// } -// ASSERT2(mesh->xstart > 0) // Need at least one guard cell -// -// FieldPerp result{emptyFrom(f).setLocation(outloc)}; -// -// int jy = f.getIndex(); -// result.setIndex(jy); -// -// if (useFFT) { -// int ncz = mesh->LocalNz; -// -// // Allocate memory -// auto ft = Matrix(mesh->LocalNx, ncz / 2 + 1); -// auto delft = Matrix(mesh->LocalNx, ncz / 2 + 1); -// -// // Take forward FFT -// for (int jx = 0; jx < mesh->LocalNx; jx++) { -// rfft(&f(jx, 0), ncz, &ft(jx, 0)); -// } -// -// // Loop over kz -// for (int jz = 0; jz <= ncz / 2; jz++) { -// -// // No smoothing in the x direction -// for (int jx = mesh->xstart; jx <= mesh->xend; jx++) { -// // Perform x derivative -// -// dcomplex a, b, c; -// laplace_tridag_coefs(jx, jy, jz, a, b, c); -// -// delft(jx, jz) = a * ft(jx - 1, jz) + b * ft(jx, jz) + c * ft(jx + 1, jz); -// } -// } -// -// // Reverse FFT -// for (int jx = mesh->xstart; jx <= mesh->xend; jx++) { -// irfft(&delft(jx, 0), ncz, &result(jx, 0)); -// } -// -// } else { -// throw BoutException("Non-fourier Delp2 not currently implented for FieldPerp."); -// // Would be the following but don't have standard derivative operators for FieldPerps -// // yet -// // result = G1 * ::DDX(f, outloc) + G3 * ::DDZ(f, outloc) + g11 * ::D2DX2(f, outloc) -// // + g33 * ::D2DZ2(f, outloc) + 2 * g13 * ::D2DXDZ(f, outloc); -// } -// -// return result; -//} - FieldMetric DifferentialOperators::Laplace_par(const Field2D& f, const Field2D& g22, const Field2D& J, const Field2D& dy, CELL_LOC outloc) { diff --git a/src/mesh/geometry.cxx b/src/mesh/geometry.cxx index 568ecac22a..bd26cdbc87 100644 --- a/src/mesh/geometry.cxx +++ b/src/mesh/geometry.cxx @@ -5,394 +5,6 @@ **************************************************************************/ #include "bout/geometry.hxx" -//#include "bout/field2d.hxx" -//#include - -//// use anonymous namespace so this utility function is not available outside this file -//namespace { - -//template -//// Use sendY()/sendX() and wait() instead of Mesh::communicate() to ensure we -//// don't try to calculate parallel slices as Coordinates are not constructed yet -//void communicate(T& t, Ts... ts) { -// coordinates.communicate(t, ts); -//} - -///// Interpolate a Field2D to a new CELL_LOC with interp_to. -///// Communicates to set internal guard cells. -///// Boundary guard cells are set by extrapolating from the grid, like -///// 'free_o3' boundary conditions -///// Corner guard cells are set to BoutNaN -//const Field2D interpolateAndExtrapolate(const Field2D& f, CELL_LOC location, -// bool extrapolate_x, bool extrapolate_y, -// bool no_extra_interpolate, -// ParallelTransform* UNUSED(pt) = nullptr, -// const std::string& region = "RGN_NOBNDRY") { -// -// Mesh* localmesh = f.getMesh(); -// Field2D result = interp_to(f, location, region); -// // Ensure result's data is unique. Otherwise result might be a duplicate of -// // f (if no interpolation is needed, e.g. if interpolation is in the -// // z-direction); then f would be communicated. Since this function is used -// // on geometrical quantities that might not be periodic in y even on closed -// // field lines (due to dependence on integrated shear), we don't want to -// // communicate f. We will sort out result's boundary guard cells below, but -// // not f's so we don't want to change f. -// result.allocate(); -// communicate(result); -// -// // Extrapolate into boundaries (if requested) so that differential geometry -// // terms can be interpolated if necessary -// // Note: cannot use applyBoundary("free_o3") here because applyBoundary() -// // would try to create a new Coordinates object since we have not finished -// // initializing yet, leading to an infinite recursion. -// // Also, here we interpolate for the boundary points at xstart/ystart and -// // (xend+1)/(yend+1) instead of extrapolating. -// for (auto& bndry : localmesh->getBoundaries()) { -// if ((extrapolate_x and bndry->bx != 0) or (extrapolate_y and bndry->by != 0)) { -// int extrap_start = 0; -// if (not no_extra_interpolate) { -// // Can use no_extra_interpolate argument to skip the extra interpolation when we -// // want to extrapolate the Christoffel symbol terms which come from derivatives so -// // don't have the extra point set already -// if ((location == CELL_XLOW) && (bndry->bx > 0)) { -// extrap_start = 1; -// } else if ((location == CELL_YLOW) && (bndry->by > 0)) { -// extrap_start = 1; -// } -// } -// for (bndry->first(); !bndry->isDone(); bndry->next1d()) { -// // interpolate extra boundary point that is missed by interp_to, if -// // necessary. -// // Only interpolate this point if we are actually changing location. E.g. -// // when we use this function to extrapolate J and Bxy on staggered grids, -// // this point should already be set correctly because the metric -// // components have been interpolated to here. -// if (extrap_start > 0 and f.getLocation() != location) { -// ASSERT1(bndry->bx == 0 or localmesh->xstart > 1) -// ASSERT1(bndry->by == 0 or localmesh->ystart > 1) -// // note that either bx or by is >0 here -// result(bndry->x, bndry->y) = -// (9. -// * (f(bndry->x - bndry->bx, bndry->y - bndry->by) -// + f(bndry->x, bndry->y)) -// - f(bndry->x - 2 * bndry->bx, bndry->y - 2 * bndry->by) -// - f(bndry->x + bndry->bx, bndry->y + bndry->by)) -// / 16.; -// } -// -// // set boundary guard cells -// if ((bndry->bx != 0 && localmesh->GlobalNx - 2 * bndry->width >= 3) -// || (bndry->by != 0 -// && localmesh->GlobalNy - localmesh->numberOfYBoundaries() * bndry->width -// >= 3)) { -// if (bndry->bx != 0 && localmesh->LocalNx == 1 && bndry->width == 1) { -// throw BoutException( -// "Not enough points in the x-direction on this " -// "processor for extrapolation needed to use staggered grids. " -// "Increase number of x-guard cells MXG or decrease number of " -// "processors in the x-direction NXPE."); -// } -// if (bndry->by != 0 && localmesh->LocalNy == 1 && bndry->width == 1) { -// throw BoutException( -// "Not enough points in the y-direction on this " -// "processor for extrapolation needed to use staggered grids. " -// "Increase number of y-guard cells MYG or decrease number of " -// "processors in the y-direction NYPE."); -// } -// // extrapolate into boundary guard cells if there are enough grid points -// for (int i = extrap_start; i < bndry->width; i++) { -// int xi = bndry->x + i * bndry->bx; -// int yi = bndry->y + i * bndry->by; -// result(xi, yi) = 3.0 * result(xi - bndry->bx, yi - bndry->by) -// - 3.0 * result(xi - 2 * bndry->bx, yi - 2 * bndry->by) -// + result(xi - 3 * bndry->bx, yi - 3 * bndry->by); -// } -// } else { -// // not enough grid points to extrapolate, set equal to last grid point -// for (int i = extrap_start; i < bndry->width; i++) { -// result(bndry->x + i * bndry->bx, bndry->y + i * bndry->by) = -// result(bndry->x - bndry->bx, bndry->y - bndry->by); -// } -// } -// } -// } -// } -//#if CHECK > 0 -// if (not( -// // if include_corner_cells=true, then we extrapolate valid data into the -// // corner cells if they are not already filled -// localmesh->include_corner_cells -// -// // if we are not extrapolating at all, the corner cells should contain valid -// // data -// or (not extrapolate_x and not extrapolate_y))) { -// // Invalidate corner guard cells -// for (int i = 0; i < localmesh->xstart; i++) { -// for (int j = 0; j < localmesh->ystart; j++) { -// result(i, j) = BoutNaN; -// result(i, localmesh->LocalNy - 1 - j) = BoutNaN; -// result(localmesh->LocalNx - 1 - i, j) = BoutNaN; -// result(localmesh->LocalNx - 1 - i, localmesh->LocalNy - 1 - j) = BoutNaN; -// } -// } -// } -//#endif -// -// return result; -//} -/* - -#if BOUT_USE_METRIC_3D -Field3D interpolateAndExtrapolate(const Field3D& f_, CELL_LOC location, - bool extrapolate_x, bool extrapolate_y, - bool no_extra_interpolate, ParallelTransform* pt_) { - - Mesh* localmesh = f_.getMesh(); - Field3D result; - Field3D f = f_; - ParallelTransform* pt_f; - if (f.getCoordinates() == nullptr) { - // if input f is member of the Coordinates we are currently constructing, it will not - // have Coordinates and needs to use the passed-in ParallelTransform - pt_f = pt_; - } else { - // if input f is from Coordinates at a different location, it will have its own - // Coordinates, and we should use its ParallelTransform - pt_f = &f.getCoordinates()->getParallelTransform(); - } - if (f.getDirectionY() != YDirectionType::Standard) { - if (pt_f->canToFromFieldAligned()) { - f = pt_f->fromFieldAligned(f); - } else { - f.setDirectionY(YDirectionType::Standard); - } - } - if (location == CELL_YLOW and f.getLocation() != CELL_YLOW) { - auto f_aligned = pt_f->toFieldAligned(f, "RGN_NOX"); - result = interp_to(f_aligned, location, "RGN_NOBNDRY"); - ParallelTransform* pt_result; - if (result.getCoordinates() == nullptr) { - pt_result = pt_; - } else { - pt_result = &result.getCoordinates()->getParallelTransform(); - } - result = pt_result->fromFieldAligned(result, "RGN_NOBNDRY"); - } else { - result = interp_to(f, location, "RGN_NOBNDRY"); - } - // Ensure result's data is unique. Otherwise result might be a duplicate of - // f (if no interpolation is needed, e.g. if interpolation is in the - // z-direction); then f would be communicated. Since this function is used - // on geometrical quantities that might not be periodic in y even on closed - // field lines (due to dependence on integrated shear), we don't want to - // communicate f. We will sort out result's boundary guard cells below, but - // not f's so we don't want to change f. - result.allocate(); - communicate(result); - - // Extrapolate into boundaries (if requested) so that differential geometry - // terms can be interpolated if necessary - // Note: cannot use applyBoundary("free_o3") here because applyBoundary() - // would try to create a new Coordinates object since we have not finished - // initializing yet, leading to an infinite recursion. - // Also, here we interpolate for the boundary points at xstart/ystart and - // (xend+1)/(yend+1) instead of extrapolating. - for (auto& bndry : localmesh->getBoundaries()) { - if ((extrapolate_x and bndry->bx != 0) or (extrapolate_y and bndry->by != 0)) { - int extrap_start = 0; - if (not no_extra_interpolate) { - // Can use no_extra_interpolate argument to skip the extra interpolation when we - // want to extrapolate the Christoffel symbol terms which come from derivatives so - // don't have the extra point set already - if ((location == CELL_XLOW) && (bndry->bx > 0)) { - extrap_start = 1; - } else if ((location == CELL_YLOW) && (bndry->by > 0)) { - extrap_start = 1; - } - } - for (bndry->first(); !bndry->isDone(); bndry->next1d()) { - // interpolate extra boundary point that is missed by interp_to, if - // necessary. - // Only interpolate this point if we are actually changing location. E.g. - // when we use this function to extrapolate J and Bxy on staggered grids, - // this point should already be set correctly because the metric - // components have been interpolated to here. - if (extrap_start > 0 and f.getLocation() != location) { - ASSERT1(bndry->bx == 0 or localmesh->xstart > 1); - ASSERT1(bndry->by == 0 or localmesh->ystart > 1); - // note that either bx or by is >0 here - for (int zi = 0; zi < localmesh->LocalNz; ++zi) { - result(bndry->x, bndry->y, zi) = - (9. - * (f(bndry->x - bndry->bx, bndry->y - bndry->by, zi) - + f(bndry->x, bndry->y, zi)) - - f(bndry->x - 2 * bndry->bx, bndry->y - 2 * bndry->by, zi) - - f(bndry->x + bndry->bx, bndry->y + bndry->by, zi)) - / 16.; - } - } - // set boundary guard cells - if ((bndry->bx != 0 && localmesh->GlobalNx - 2 * bndry->width >= 3) - || (bndry->by != 0 && localmesh->GlobalNy - 2 * bndry->width >= 3)) { - if (bndry->bx != 0 && localmesh->LocalNx == 1 && bndry->width == 1) { - throw BoutException( - "Not enough points in the x-direction on this " - "processor for extrapolation needed to use staggered grids. " - "Increase number of x-guard cells MXG or decrease number of " - "processors in the x-direction NXPE."); - } - if (bndry->by != 0 && localmesh->LocalNy == 1 && bndry->width == 1) { - throw BoutException( - "Not enough points in the y-direction on this " - "processor for extrapolation needed to use staggered grids. " - "Increase number of y-guard cells MYG or decrease number of " - "processors in the y-direction NYPE."); - } - // extrapolate into boundary guard cells if there are enough grid points - for (int i = extrap_start; i < bndry->width; i++) { - int xi = bndry->x + i * bndry->bx; - int yi = bndry->y + i * bndry->by; - for (int zi = 0; zi < localmesh->LocalNz; ++zi) { - result(xi, yi, zi) = - 3.0 * result(xi - bndry->bx, yi - bndry->by, zi) - - 3.0 * result(xi - 2 * bndry->bx, yi - 2 * bndry->by, zi) - + result(xi - 3 * bndry->bx, yi - 3 * bndry->by, zi); - } - } - } else { - // not enough grid points to extrapolate, set equal to last grid point - for (int i = extrap_start; i < bndry->width; i++) { - for (int zi = 0; zi < localmesh->LocalNz; ++zi) { - result(bndry->x + i * bndry->bx, bndry->y + i * bndry->by, zi) = - result(bndry->x - bndry->bx, bndry->y - bndry->by, zi); - } - } - } - } - } - } -#if CHECK > 0 - if (not( - // if include_corner_cells=true, then we extrapolate valid data into the - // corner cells if they are not already filled - localmesh->include_corner_cells - - // if we are not extrapolating at all, the corner cells should contain valid - // data - or (not extrapolate_x and not extrapolate_y))) { - // Invalidate corner guard cells - for (int i = 0; i < localmesh->xstart; i++) { - for (int j = 0; j < localmesh->ystart; j++) { - for (int k = 0; k < localmesh->LocalNz; ++k) { - result(i, j, k) = BoutNaN; - result(i, localmesh->LocalNy - 1 - j, k) = BoutNaN; - result(localmesh->LocalNx - 1 - i, j, k) = BoutNaN; - result(localmesh->LocalNx - 1 - i, localmesh->LocalNy - 1 - j, k) = BoutNaN; - } - } - } - } -#endif // CHECK > 0 - - return result; -} -#endif // BOUT_USE_METRIC_3D -*/ -///* -// -//// If the CELL_CENTRE variable was read, the staggered version is required to -//// also exist for consistency -//void checkStaggeredGet(Mesh* mesh, const std::string& name, const std::string& suffix) { -// if (mesh->sourceHasVar(name) != mesh->sourceHasVar(name + suffix)) { -// throw BoutException("Attempting to read staggered fields from grid, but " + name -// + " is not present in both CELL_CENTRE and staggered versions."); -// } -//} -// -//// convenience function for repeated code -//int getAtLoc(Mesh* mesh, Coordinates::FieldMetric& var, const std::string& name, -// const std::string& suffix, CELL_LOC location, BoutReal default_value = 0.) { -// -// checkStaggeredGet(mesh, name, suffix); -// return mesh->get(var, name + suffix, default_value, false, location); -//} -// -//auto getAtLoc(Mesh* mesh, const std::string& name, const std::string& suffix, -// CELL_LOC location, BoutReal default_value = 0.) { -// -// checkStaggeredGet(mesh, name, suffix); -// return mesh->get(name + suffix, default_value, false, location); -//} - -//std::string getLocationSuffix(CELL_LOC location) { -// switch (location) { -// case CELL_CENTRE: { -// return ""; -// } -// case CELL_XLOW: { -// return "_xlow"; -// } -// case CELL_YLOW: { -// return "_ylow"; -// } -// case CELL_ZLOW: { -// // in 2D metric, same as CELL_CENTRE -// return bout::build::use_metric_3d ? "_zlow" : ""; -// } -// default: { -// throw BoutException( -// "Incorrect location passed to " -// "Coordinates(Mesh*,const CELL_LOC,const Coordinates*) constructor."); -// } -// } -//} -// -//} // anonymous namespace -//*/ -// -//Coordinates::FieldMetric Coordinates::getAtLocOrUnaligned(Mesh* mesh, -// const std::string& name, -// BoutReal default_value, -// const std::string& suffix, -// CELL_LOC cell_location) { -// if (cell_location == CELL_CENTRE) { -// return getUnaligned(name, default_value); -// } -// // grid data source has staggered fields, so read instead of interpolating -// // Diagonal components of metric tensor g^{ij} (default to 1) -// return getAtLoc(mesh, name, suffix, cell_location, default_value); -//} -// -//Coordinates::FieldMetric Coordinates::getUnaligned(const std::string& name, -// BoutReal default_value) { -// -// auto field = localmesh->get(name, default_value, false); -// if (field.getDirectionY() == YDirectionType::Aligned -// and transform->canToFromFieldAligned()) { -// return transform->fromFieldAligned(field); -// } else { -// field.setDirectionY(YDirectionType::Standard); -// return field; -// } -//} -// -//Coordinates::FieldMetric Coordinates::getUnalignedAtLocationAndFillGuards( -// Mesh* mesh, const std::string& name, BoutReal default_value, -// const std::string& suffix, CELL_LOC cell_location, bool extrapolate_x, -// bool extrapolate_y, bool no_extra_interpolate, -// ParallelTransform* pParallelTransform) { -// -// auto field = getAtLocOrUnaligned(mesh, name, default_value, suffix, cell_location); -// if (suffix == "") { -// no_extra_interpolate = false; -// pParallelTransform = transform.get(); -// } -// return interpolateAndExtrapolate(field, cell_location, extrapolate_x, extrapolate_y, -// no_extra_interpolate, pParallelTransform); -//} - -//Geometry::Geometry() {} Geometry::Geometry(FieldMetric J, FieldMetric Bxy, FieldMetric g11, FieldMetric g22, FieldMetric g33, FieldMetric g12, FieldMetric g13, FieldMetric g23, @@ -415,287 +27,6 @@ Geometry::Geometry(Mesh* mesh, DifferentialOperators* differential_operators) this_J(1., mesh), this_Bxy(1., mesh), differential_operators(differential_operators) {} -// -//void Coordinates::outputVars(Options& output_options) { -// Timer time("io"); -// const std::string loc_string = -// (location == CELL_CENTRE) ? "" : "_" + toString(location); -// -// output_options["dx" + loc_string].force(dx, "Coordinates"); -// output_options["dy" + loc_string].force(dy, "Coordinates"); -// output_options["dz" + loc_string].force(dz, "Coordinates"); -// -// output_options["g11" + loc_string].force(contravariantMetricTensor.Getg11(), -// "Coordinates"); -// output_options["g22" + loc_string].force(contravariantMetricTensor.Getg22(), -// "Coordinates"); -// output_options["g33" + loc_string].force(contravariantMetricTensor.Getg33(), -// "Coordinates"); -// output_options["g12" + loc_string].force(contravariantMetricTensor.Getg12(), -// "Coordinates"); -// output_options["g13" + loc_string].force(contravariantMetricTensor.Getg13(), -// "Coordinates"); -// output_options["g23" + loc_string].force(contravariantMetricTensor.Getg23(), -// "Coordinates"); -// -// output_options["g_11" + loc_string].force(covariantMetricTensor.Getg11(), -// "Coordinates"); -// output_options["g_22" + loc_string].force(covariantMetricTensor.Getg22(), -// "Coordinates"); -// output_options["g_33" + loc_string].force(covariantMetricTensor.Getg33(), -// "Coordinates"); -// output_options["g_12" + loc_string].force(covariantMetricTensor.Getg12(), -// "Coordinates"); -// output_options["g_13" + loc_string].force(covariantMetricTensor.Getg13(), -// "Coordinates"); -// output_options["g_23" + loc_string].force(covariantMetricTensor.Getg23(), -// "Coordinates"); -// -// output_options["J" + loc_string].force(this_J, "Coordinates"); -// output_options["Bxy" + loc_string].force(this_Bxy, "Coordinates"); -// -// output_options["G1" + loc_string].force(G1, "Coordinates"); -// output_options["G2" + loc_string].force(G2, "Coordinates"); -// output_options["G3" + loc_string].force(G3, "Coordinates"); -// -// getParallelTransform().outputVars(output_options); -//} - -//const Field2D& Coordinates::zlength() const { -// BOUT_OMP(critical) -// if (not zlength_cache) { -// zlength_cache = std::make_unique(0., localmesh); -// -//#if BOUT_USE_METRIC_3D -// BOUT_FOR_SERIAL(i, dz.getRegion("RGN_ALL")) { (*zlength_cache)[i] += dz[i]; } -//#else -// (*zlength_cache) = dz * nz; -//#endif -// } -// -// return *zlength_cache; -//} - -//int Geometry::calculateGeometry(bool recalculate_staggered, bool force_interpolate_from_centre) { -// TRACE("Geometry::calculateGeometry"); -// -// communicate(dx, dy, dz, contravariantMetricTensor.Getg11(), -// contravariantMetricTensor.Getg22(), contravariantMetricTensor.Getg33(), -// contravariantMetricTensor.Getg12(), contravariantMetricTensor.Getg13(), -// contravariantMetricTensor.Getg23(), covariantMetricTensor.Getg11(), -// covariantMetricTensor.Getg22(), covariantMetricTensor.Getg33(), -// covariantMetricTensor.Getg12(), covariantMetricTensor.Getg13(), -// covariantMetricTensor.Getg23(), this_J, this_Bxy); -// -// output_progress.write("Calculating differential geometry terms\n"); -// -// if (min(abs(dx)) < 1e-8) { -// throw BoutException("dx magnitude less than 1e-8"); -// } -// -// if (min(abs(dy)) < 1e-8) { -// throw BoutException("dy magnitude less than 1e-8"); -// } -// -// if (min(abs(dz)) < 1e-8) { -// throw BoutException("dz magnitude less than 1e-8"); -// } -// -// // Check input metrics -// checkContravariant(); -// checkCovariant(); -// CalculateChristoffelSymbols(); -// -// auto tmp = this_J * contravariantMetricTensor.Getg12(); -// communicate(tmp); -// G1 = (DDX(this_J * contravariantMetricTensor.Getg11()) + DDY(tmp) -// + DDZ(this_J * contravariantMetricTensor.Getg13())) -// / this_J; -// tmp = this_J * contravariantMetricTensor.Getg22(); -// communicate(tmp); -// G2 = (DDX(this_J * contravariantMetricTensor.Getg12()) + DDY(tmp) -// + DDZ(this_J * contravariantMetricTensor.Getg23())) -// / this_J; -// tmp = this_J * contravariantMetricTensor.Getg23(); -// communicate(tmp); -// G3 = (DDX(this_J * contravariantMetricTensor.Getg13()) + DDY(tmp) -// + DDZ(this_J * contravariantMetricTensor.Getg33())) -// / this_J; -// -// // Communicate christoffel symbol terms -// output_progress.write("\tCommunicating connection terms\n"); -// -// communicate(G1_11, G1_22, G1_33, G1_12, G1_13, G1_23, G2_11, G2_22, G2_33, G2_12, G2_13, -// G2_23, G3_11, G3_22, G3_33, G3_12, G3_13, G3_23, G1, G2, G3); -// -// // Set boundary guard cells of Christoffel symbol terms -// // Ideally, when location is staggered, we would set the upper/outer boundary point -// // correctly rather than by extrapolating here: e.g. if location==CELL_YLOW and we are -// // at the upper y-boundary the x- and z-derivatives at yend+1 at the boundary can be -// // calculated because the guard cells are available, while the y-derivative could be -// // calculated from the CELL_CENTRE metric components (which have guard cells available -// // past the boundary location). This would avoid the problem that the y-boundary on the -// // CELL_YLOW grid is at a 'guard cell' location (yend+1). -// // However, the above would require lots of special handling, so just extrapolate for -// // now. -// G1_11 = interpolateAndExtrapolate(G1_11, location, true, true, true, transform.get()); -// G1_22 = interpolateAndExtrapolate(G1_22, location, true, true, true, transform.get()); -// G1_33 = interpolateAndExtrapolate(G1_33, location, true, true, true, transform.get()); -// G1_12 = interpolateAndExtrapolate(G1_12, location, true, true, true, transform.get()); -// G1_13 = interpolateAndExtrapolate(G1_13, location, true, true, true, transform.get()); -// G1_23 = interpolateAndExtrapolate(G1_23, location, true, true, true, transform.get()); -// -// G2_11 = interpolateAndExtrapolate(G2_11, location, true, true, true, transform.get()); -// G2_22 = interpolateAndExtrapolate(G2_22, location, true, true, true, transform.get()); -// G2_33 = interpolateAndExtrapolate(G2_33, location, true, true, true, transform.get()); -// G2_12 = interpolateAndExtrapolate(G2_12, location, true, true, true, transform.get()); -// G2_13 = interpolateAndExtrapolate(G2_13, location, true, true, true, transform.get()); -// G2_23 = interpolateAndExtrapolate(G2_23, location, true, true, true, transform.get()); -// -// G3_11 = interpolateAndExtrapolate(G3_11, location, true, true, true, transform.get()); -// G3_22 = interpolateAndExtrapolate(G3_22, location, true, true, true, transform.get()); -// G3_33 = interpolateAndExtrapolate(G3_33, location, true, true, true, transform.get()); -// G3_12 = interpolateAndExtrapolate(G3_12, location, true, true, true, transform.get()); -// G3_13 = interpolateAndExtrapolate(G3_13, location, true, true, true, transform.get()); -// G3_23 = interpolateAndExtrapolate(G3_23, location, true, true, true, transform.get()); -// -// G1 = interpolateAndExtrapolate(G1, location, true, true, true, transform.get()); -// G2 = interpolateAndExtrapolate(G2, location, true, true, true, transform.get()); -// G3 = interpolateAndExtrapolate(G3, location, true, true, true, transform.get()); -// // -// // ////////////////////////////////////////////////////// -// // /// Non-uniform meshes. Need to use DDX, DDY -// // -// // OPTION(Options::getRoot(), non_uniform, true); -// // -// // Coordinates::FieldMetric d2x(localmesh), d2y(localmesh), -// // d2z(localmesh); // d^2 x / d i^2 -// // -// // // Read correction for non-uniform meshes -// // std::string suffix = getLocationSuffix(location); -// // if (location == CELL_CENTRE -// // or (!force_interpolate_from_centre and localmesh->sourceHasVar("dx" + suffix))) { -// // bool extrapolate_x = not localmesh->sourceHasXBoundaryGuards(); -// // bool extrapolate_y = not localmesh->sourceHasYBoundaryGuards(); -// // -// // if (localmesh->get(d2x, "d2x" + suffix, 0.0, false, location)) { -// // output_warn.write("\tWARNING: differencing quantity 'd2x' not found. " -// // "Calculating from dx\n"); -// // d1_dx = bout::derivatives::index::DDX(1. / dx); // d/di(1/dx) -// // -// // communicate(d1_dx); -// // d1_dx = -// // interpolateAndExtrapolate(d1_dx, location, true, true, true, transform.get()); -// // } else { -// // d2x.setLocation(location); -// // // set boundary cells if necessary -// // d2x = interpolateAndExtrapolate(d2x, location, extrapolate_x, extrapolate_y, false, -// // transform.get()); -// // -// // d1_dx = -d2x / (dx * dx); -// // } -// // -// // if (localmesh->get(d2y, "d2y" + suffix, 0.0, false, location)) { -// // output_warn.write("\tWARNING: differencing quantity 'd2y' not found. " -// // "Calculating from dy\n"); -// // d1_dy = DDY(1. / dy); // d/di(1/dy) -// // -// // communicate(d1_dy); -// // d1_dy = -// // interpolateAndExtrapolate(d1_dy, location, true, true, true, transform.get()); -// // } else { -// // d2y.setLocation(location); -// // // set boundary cells if necessary -// // d2y = interpolateAndExtrapolate(d2y, location, extrapolate_x, extrapolate_y, false, -// // transform.get()); -// // -// // d1_dy = -d2y / (dy * dy); -// // } -// // -// //#if BOUT_USE_METRIC_3D -// // if (localmesh->get(d2z, "d2z" + suffix, 0.0, false)) { -// // output_warn.write("\tWARNING: differencing quantity 'd2z' not found. " -// // "Calculating from dz\n"); -// // d1_dz = bout::derivatives::index::DDZ(1. / dz); -// // communicate(d1_dz); -// // d1_dz = -// // interpolateAndExtrapolate(d1_dz, location, true, true, true, transform.get()); -// // } else { -// // d2z.setLocation(location); -// // // set boundary cells if necessary -// // d2z = interpolateAndExtrapolate(d2z, location, extrapolate_x, extrapolate_y, false, -// // transform.get()); -// // -// // d1_dz = -d2z / (dz * dz); -// // } -// //#else -// // d1_dz = 0; -// //#endif -// // } else { -// // if (localmesh->get(d2x, "d2x", 0.0, false)) { -// // output_warn.write("\tWARNING: differencing quantity 'd2x' not found. " -// // "Calculating from dx\n"); -// // d1_dx = bout::derivatives::index::DDX(1. / dx); // d/di(1/dx) -// // -// // communicate(d1_dx); -// // d1_dx = -// // interpolateAndExtrapolate(d1_dx, location, true, true, true, transform.get()); -// // } else { -// // // Shift d2x to our location -// // d2x = interpolateAndExtrapolate(d2x, location, true, true, false, transform.get()); -// // -// // d1_dx = -d2x / (dx * dx); -// // } -// // -// // if (localmesh->get(d2y, "d2y", 0.0, false)) { -// // output_warn.write("\tWARNING: differencing quantity 'd2y' not found. " -// // "Calculating from dy\n"); -// // d1_dy = DDY(1. / dy); // d/di(1/dy) -// // -// // communicate(d1_dy); -// // d1_dy = -// // interpolateAndExtrapolate(d1_dy, location, true, true, true, transform.get()); -// // } else { -// // // Shift d2y to our location -// // d2y = interpolateAndExtrapolate(d2y, location, true, true, false, transform.get()); -// // -// // d1_dy = -d2y / (dy * dy); -// // } -// // -// //#if BOUT_USE_METRIC_3D -// // if (localmesh->get(d2z, "d2z", 0.0, false)) { -// // output_warn.write("\tWARNING: differencing quantity 'd2z' not found. " -// // "Calculating from dz\n"); -// // d1_dz = bout::derivatives::index::DDZ(1. / dz); -// // -// // communicate(d1_dz); -// // d1_dz = -// // interpolateAndExtrapolate(d1_dz, location, true, true, true, transform.get()); -// // } else { -// // // Shift d2z to our location -// // d2z = interpolateAndExtrapolate(d2z, location, true, true, false, transform.get()); -// // -// // d1_dz = -d2z / (dz * dz); -// // } -// //#else -// // d1_dz = 0; -// //#endif -// // } -// // communicate(d1_dx, d1_dy, d1_dz); -// // -// // if (location == CELL_CENTRE && recalculate_staggered) { -// // // Re-calculate interpolated Coordinates at staggered locations -// // localmesh->recalculateStaggeredCoordinates(); -// // } -// // -// // // Invalidate and recalculate cached variables -// // zlength_cache.reset(); -// // Grad2_par2_DDY_invSgCache.clear(); -// // invSgCache.reset(); -// // -// // return 0; -// //} - void Geometry::CalculateChristoffelSymbols(FieldMetric& dx, FieldMetric& dy) { // Calculate Christoffel symbol terms (18 independent values) // Note: This calculation is completely general: metric @@ -888,25 +219,6 @@ void Geometry::calcContravariant(CELL_LOC cell_location, const std::string& regi covariantMetricTensor.oppositeRepresentation(cell_location, region)); } -//void Geometry::jacobian(bool extrapolate_x, bool extrapolate_y) { -// TRACE("Geometry::jacobian"); -// try { -// -// const auto j = recalculateJacobian(extrapolate_x, extrapolate_y); -// // More robust to extrapolate derived quantities directly, rather than -// // deriving from extrapolated covariant metric components -// this_J = interpolateAndExtrapolate(j, extrapolate_x, extrapolate_y, false); -// -// const auto Bxy = recalculateBxy(extrapolate_x, extrapolate_y); -//// CELL_LOC location, ParallelTransform* pParallelTransform -// this_Bxy = interpolateAndExtrapolate(Bxy, location, extrapolate_x, extrapolate_y, false, -// transform.get()); -// } catch (BoutException&) { -// output_error.write("\tError in jacobian call\n"); -// throw; -// } -//} - MetricTensor::FieldMetric Geometry::recalculateJacobian() { // calculate Jacobian using g^-1 = det[g^ij], J = sqrt(g) @@ -932,579 +244,6 @@ MetricTensor::FieldMetric Geometry::recalculateBxy() { return sqrt(covariantMetricTensor.Getg22()) / this_J; } -// namespace { -// // Utility function for fixing up guard cells of zShift -// void fixZShiftGuards(Field2D& zShift) { -// auto localmesh = zShift.getMesh(); -// -// // extrapolate into boundary guard cells if necessary -// zShift = interpolateAndExtrapolate(zShift, zShift.getLocation(), -// not localmesh->sourceHasXBoundaryGuards(), -// not localmesh->sourceHasYBoundaryGuards(), false); -// -// // make sure zShift has been communicated -// communicate(zShift); -// -// // Correct guard cells for discontinuity of zShift at poloidal branch cut -// for (int x = 0; x < localmesh->LocalNx; x++) { -// const auto lower = localmesh->hasBranchCutLower(x); -// if (lower.first) { -// for (int y = 0; y < localmesh->ystart; y++) { -// zShift(x, y) -= lower.second; -// } -// } -// const auto upper = localmesh->hasBranchCutUpper(x); -// if (upper.first) { -// for (int y = localmesh->yend + 1; y < localmesh->LocalNy; y++) { -// zShift(x, y) += upper.second; -// } -// } -// } -// } -// } // namespace -// -// //void Coordinates::setParallelTransform(Options* options) { -// // -// // auto ptoptions = options->getSection("paralleltransform"); -// // -// // std::string ptstr; -// // ptoptions->get("type", ptstr, "identity"); -// // -// // // Convert to lower case for comparison -// // ptstr = lowercase(ptstr); -// // -// // if (ptstr == "identity") { -// // // Identity method i.e. no transform needed -// // transform = -// // bout::utils::make_unique(*localmesh, ptoptions); -// // -// // } else if (ptstr == "shifted" or ptstr == "shiftedinterp") { -// // // Shifted metric method -// // -// // Field2D zShift{localmesh}; -// // -// // // Read the zShift angle from the mesh -// // std::string suffix = getLocationSuffix(location); -// // if (localmesh->sourceHasVar("dx" + suffix)) { -// // // Grid file has variables at this location, so should be able to read -// // checkStaggeredGet(localmesh, "zShift", suffix); -// // if (localmesh->get(zShift, "zShift" + suffix, 0.0, false, location)) { -// // // No zShift variable. Try qinty in BOUT grid files -// // if (localmesh->get(zShift, "qinty" + suffix, 0.0, false, location)) { -// // // Failed to find either variable, cannot use ShiftedMetric -// // throw BoutException("Could not read zShift" + suffix + " from grid file"); -// // } -// // } -// // } else { -// // if (location == CELL_YLOW and bout::build::use_metric_3d) { -// // throw BoutException("Cannot interpolate zShift to construct ShiftedMetric when " -// // "using 3d metrics. You must provide zShift_ylow in the grid " -// // "file."); -// // } -// // Field2D zShift_centre; -// // if (localmesh->get(zShift_centre, "zShift", 0.0, false)) { -// // // No zShift variable. Try qinty in BOUT grid files -// // if (localmesh->get(zShift_centre, "qinty", 0.0, false)) { -// // // Failed to find either variable, cannot use ShiftedMetric -// // throw BoutException("Could not read zShift from grid file"); -// // } -// // } -// // -// // fixZShiftGuards(zShift_centre); -// // -// // zShift = interpolateAndExtrapolate(zShift_centre, location, true, true, false, -// // transform.get()); -// // } -// // -// // fixZShiftGuards(zShift); -// // -// // if (ptstr == "shifted") { -// // transform = bout::utils::make_unique(*localmesh, location, zShift, -// // getUniform(zlength())); -// // } else if (ptstr == "shiftedinterp") { -// // transform = bout::utils::make_unique( -// // *localmesh, location, zShift, getUniform(zlength())); -// // } -// // -// // } else if (ptstr == "fci") { -// // -// // if (location != CELL_CENTRE) { -// // throw BoutException("FCITransform is not available on staggered grids."); -// // } -// // -// // // Flux Coordinate Independent method -// // const bool fci_zperiodic = (*ptoptions)["z_periodic"].withDefault(true); -// // transform = -// // bout::utils::make_unique(*localmesh, dy, fci_zperiodic, ptoptions); -// // -// // } else { -// // throw BoutException(_("Unrecognised paralleltransform option.\n" -// // "Valid choices are 'identity', 'shifted', 'fci'")); -// // } -// //} -// -// ///******************************************************************************* -// // * Operators -// // * -// // *******************************************************************************/ -// // -// //Coordinates::FieldMetric Coordinates::DDX(const Field2D& f, CELL_LOC loc, -// // const std::string& method, -// // const std::string& region) { -// // ASSERT1(location == loc || loc == CELL_DEFAULT) -// // return bout::derivatives::index::DDX(f, loc, method, region) / dx; -// //} -// //Field3D Coordinates::DDX(const Field3D& f, CELL_LOC outloc, const std::string& method, -// // const std::string& region) { -// // -// // auto result = bout::derivatives::index::DDX(f, outloc, method, region); -// // result /= dx; -// // -// // if (f.getMesh()->IncIntShear) { -// // // Using BOUT-06 style shifting -// // result += IntShiftTorsion * DDZ(f, outloc, method, region); -// // } -// // -// // return result; -// //} -// // -// //Coordinates::FieldMetric Coordinates::DDY(const Field2D& f, CELL_LOC loc, -// // const std::string& method, -// // const std::string& region) const { -// // ASSERT1(location == loc || loc == CELL_DEFAULT) -// // return bout::derivatives::index::DDY(f, loc, method, region) / dy; -// //} -// // -// //Field3D Coordinates::DDY(const Field3D& f, CELL_LOC outloc, const std::string& method, -// // const std::string& region) const { -// //#if BOUT_USE_METRIC_3D -// // if (!f.hasParallelSlices() and !transform->canToFromFieldAligned()) { -// // Field3D f_parallel = f; -// // transform->calcParallelSlices(f_parallel); -// // f_parallel.applyParallelBoundary("parallel_neumann"); -// // return bout::derivatives::index::DDY(f_parallel, outloc, method, region); -// // } -// //#endif -// // return bout::derivatives::index::DDY(f, outloc, method, region) / dy; -// //} -// // -// //Coordinates::FieldMetric Coordinates::DDZ(const Field2D& f, CELL_LOC loc, -// // const std::string& UNUSED(method), -// // const std::string& UNUSED(region)) { -// // ASSERT1(location == loc || loc == CELL_DEFAULT) -// // ASSERT1(f.getMesh() == localmesh) -// // if (loc == CELL_DEFAULT) { -// // loc = f.getLocation(); -// // } -// // return zeroFrom(f).setLocation(loc); -// //} -// //Field3D Coordinates::DDZ(const Field3D& f, CELL_LOC outloc, const std::string& method, -// // const std::string& region) { -// // return bout::derivatives::index::DDZ(f, outloc, method, region) / dz; -// //} -// -// /////////////////////////////////////////////////////////// -// //// Parallel gradient -// // -// //Coordinates::FieldMetric Coordinates::Grad_par(const Field2D& var, -// // MAYBE_UNUSED(CELL_LOC outloc), -// // const std::string& UNUSED(method)) { -// // TRACE("Coordinates::Grad_par( Field2D )"); -// // ASSERT1(location == outloc || (outloc == CELL_DEFAULT && location == var.getLocation())) -// // -// // return DDY(var) * invSg(); -// //} -// // -// //Field3D Coordinates::Grad_par(const Field3D& var, CELL_LOC outloc, -// // const std::string& method) { -// // TRACE("Coordinates::Grad_par( Field3D )"); -// // ASSERT1(location == outloc || outloc == CELL_DEFAULT) -// // -// // return ::DDY(var, outloc, method) * invSg(); -// //} -// // -// /////////////////////////////////////////////////////////// -// //// Vpar_Grad_par -// //// vparallel times the parallel derivative along unperturbed B-field -// // -// //Coordinates::FieldMetric Coordinates::Vpar_Grad_par(const Field2D& v, const Field2D& f, -// // MAYBE_UNUSED(CELL_LOC outloc), -// // const std::string& UNUSED(method)) { -// // ASSERT1(location == outloc || (outloc == CELL_DEFAULT && location == f.getLocation())) -// // -// // return VDDY(v, f) * invSg(); -// //} -// // -// //Field3D Coordinates::Vpar_Grad_par(const Field3D& v, const Field3D& f, CELL_LOC outloc, -// // const std::string& method) { -// // ASSERT1(location == outloc || outloc == CELL_DEFAULT) -// // -// // return VDDY(v, f, outloc, method) * invSg(); -// //} -// // -// /////////////////////////////////////////////////////////// -// //// Parallel divergence -// // -// //Coordinates::FieldMetric Coordinates::Div_par(const Field2D& f, CELL_LOC outloc, -// // const std::string& method) { -// // TRACE("Coordinates::Div_par( Field2D )"); -// // ASSERT1(location == outloc || outloc == CELL_DEFAULT) -// // -// // // Need Bxy at location of f, which might be different from location of this -// // // Coordinates object -// // auto Bxy_floc = f.getCoordinates()->Bxy(); -// // -// // return this_Bxy * Grad_par(f / Bxy_floc, outloc, method); -// //} -// // -// //Field3D Coordinates::Div_par(const Field3D& f, CELL_LOC outloc, -// // const std::string& method) { -// // TRACE("Coordinates::Div_par( Field3D )"); -// // ASSERT1(location == outloc || outloc == CELL_DEFAULT) -// // -// // // Need Bxy at location of f, which might be different from location of this -// // // Coordinates object -// // auto Bxy_floc = f.getCoordinates()->Bxy(); -// // -// // if (!f.hasParallelSlices()) { -// // // No yup/ydown fields. The Grad_par operator will -// // // shift to field aligned coordinates -// // return this_Bxy * Grad_par(f / Bxy_floc, outloc, method); -// // } -// // -// // // Need to modify yup and ydown fields -// // Field3D f_B = f / Bxy_floc; -// // f_B.splitParallelSlices(); -// // for (int i = 0; i < f.getMesh()->ystart; ++i) { -// // f_B.yup(i) = f.yup(i) / Bxy_floc.yup(i); -// // f_B.ydown(i) = f.ydown(i) / Bxy_floc.ydown(i); -// // } -// // return this_Bxy * Grad_par(f_B, outloc, method); -// //} -// // -// /////////////////////////////////////////////////////////// -// //// second parallel derivative (b dot Grad)(b dot Grad) -// //// Note: For parallel Laplacian use Laplace_par -// // -// //Coordinates::FieldMetric Coordinates::Grad2_par2(const Field2D& f, CELL_LOC outloc, -// // const std::string& method) { -// // TRACE("Coordinates::Grad2_par2( Field2D )"); -// // ASSERT1(location == outloc || (outloc == CELL_DEFAULT && location == f.getLocation())) -// // -// // auto result = Grad2_par2_DDY_invSg(outloc, method) * DDY(f, outloc, method) -// // + D2DY2(f, outloc, method) / covariantMetricTensor.Getg22(); -// // -// // return result; -// //} -// // -// //Field3D Coordinates::Grad2_par2(const Field3D& f, CELL_LOC outloc, -// // const std::string& method) { -// // TRACE("Coordinates::Grad2_par2( Field3D )"); -// // if (outloc == CELL_DEFAULT) { -// // outloc = f.getLocation(); -// // } -// // ASSERT1(location == outloc) -// // -// // Field3D result = ::DDY(f, outloc, method); -// // -// // Field3D r2 = D2DY2(f, outloc, method) / covariantMetricTensor.Getg22(); -// // -// // result = Grad2_par2_DDY_invSg(outloc, method) * result + r2; -// // -// // ASSERT2(result.getLocation() == outloc) -// // -// // return result; -// //} -// // -// /////////////////////////////////////////////////////////// -// //// perpendicular Laplacian operator -// // -// //#include // Delp2 uses same coefficients as inversion code -// // -// //Coordinates::FieldMetric Coordinates::Delp2(const Field2D& f, CELL_LOC outloc, -// // bool UNUSED(useFFT)) { -// // TRACE("Coordinates::Delp2( Field2D )"); -// // ASSERT1(location == outloc || outloc == CELL_DEFAULT) -// // -// // auto result = -// // G1 * DDX(f, outloc) + contravariantMetricTensor.Getg11() * D2DX2(f, outloc); -// // -// // return result; -// //} -// // -// //Field3D Coordinates::Delp2(const Field3D& f, CELL_LOC outloc, bool useFFT) { -// // TRACE("Coordinates::Delp2( Field3D )"); -// // -// // if (outloc == CELL_DEFAULT) { -// // outloc = f.getLocation(); -// // } -// // -// // ASSERT1(location == outloc) -// // ASSERT1(f.getLocation() == outloc) -// // -// // if (localmesh->GlobalNx == 1 && localmesh->GlobalNz == 1) { -// // // copy mesh, location, etc -// // return f * 0; -// // } -// // ASSERT2(localmesh->xstart > 0) // Need at least one guard cell -// // -// // Field3D result{emptyFrom(f).setLocation(outloc)}; -// // -// // if (useFFT and not bout::build::use_metric_3d) { -// // int ncz = localmesh->LocalNz; -// // -// // // Allocate memory -// // auto ft = Matrix(localmesh->LocalNx, ncz / 2 + 1); -// // auto delft = Matrix(localmesh->LocalNx, ncz / 2 + 1); -// // -// // // Loop over y indices -// // // Note: should not include y-guard or y-boundary points here as that would -// // // use values from corner cells in dx, which may not be initialised. -// // for (int jy = localmesh->ystart; jy <= localmesh->yend; jy++) { -// // -// // // Take forward FFT -// // -// // for (int jx = 0; jx < localmesh->LocalNx; jx++) { -// // rfft(&f(jx, jy, 0), ncz, &ft(jx, 0)); -// // } -// // -// // // Loop over kz -// // for (int jz = 0; jz <= ncz / 2; jz++) { -// // -// // // No smoothing in the x direction -// // for (int jx = localmesh->xstart; jx <= localmesh->xend; jx++) { -// // // Perform x derivative -// // -// // dcomplex a, b, c; -// // laplace_tridag_coefs(jx, jy, jz, a, b, c, nullptr, nullptr, outloc); -// // -// // delft(jx, jz) = a * ft(jx - 1, jz) + b * ft(jx, jz) + c * ft(jx + 1, jz); -// // } -// // } -// // -// // // Reverse FFT -// // for (int jx = localmesh->xstart; jx <= localmesh->xend; jx++) { -// // -// // irfft(&delft(jx, 0), ncz, &result(jx, jy, 0)); -// // } -// // } -// // } else { -// // result = G1 * ::DDX(f, outloc) + G3 * ::DDZ(f, outloc) -// // + contravariantMetricTensor.Getg11() * ::D2DX2(f, outloc) -// // + contravariantMetricTensor.Getg33() * ::D2DZ2(f, outloc) -// // + 2 * contravariantMetricTensor.Getg13() * ::D2DXDZ(f, outloc); -// // } -// // -// // ASSERT2(result.getLocation() == outloc) -// // -// // return result; -// //} -// // -// //FieldPerp Coordinates::Delp2(const FieldPerp& f, CELL_LOC outloc, bool useFFT) { -// // TRACE("Coordinates::Delp2( FieldPerp )"); -// // -// // if (outloc == CELL_DEFAULT) { -// // outloc = f.getLocation(); -// // } -// // -// // ASSERT1(location == outloc) -// // ASSERT1(f.getLocation() == outloc) -// // -// // if (localmesh->GlobalNx == 1 && localmesh->GlobalNz == 1) { -// // // copy mesh, location, etc -// // return f * 0; -// // } -// // ASSERT2(localmesh->xstart > 0) // Need at least one guard cell -// // -// // FieldPerp result{emptyFrom(f).setLocation(outloc)}; -// // -// // int jy = f.getIndex(); -// // result.setIndex(jy); -// // -// // if (useFFT) { -// // int ncz = localmesh->LocalNz; -// // -// // // Allocate memory -// // auto ft = Matrix(localmesh->LocalNx, ncz / 2 + 1); -// // auto delft = Matrix(localmesh->LocalNx, ncz / 2 + 1); -// // -// // // Take forward FFT -// // for (int jx = 0; jx < localmesh->LocalNx; jx++) { -// // rfft(&f(jx, 0), ncz, &ft(jx, 0)); -// // } -// // -// // // Loop over kz -// // for (int jz = 0; jz <= ncz / 2; jz++) { -// // -// // // No smoothing in the x direction -// // for (int jx = localmesh->xstart; jx <= localmesh->xend; jx++) { -// // // Perform x derivative -// // -// // dcomplex a, b, c; -// // laplace_tridag_coefs(jx, jy, jz, a, b, c); -// // -// // delft(jx, jz) = a * ft(jx - 1, jz) + b * ft(jx, jz) + c * ft(jx + 1, jz); -// // } -// // } -// // -// // // Reverse FFT -// // for (int jx = localmesh->xstart; jx <= localmesh->xend; jx++) { -// // irfft(&delft(jx, 0), ncz, &result(jx, 0)); -// // } -// // -// // } else { -// // throw BoutException("Non-fourier Delp2 not currently implented for FieldPerp."); -// // // Would be the following but don't have standard derivative operators for FieldPerps -// // // yet -// // // result = G1 * ::DDX(f, outloc) + G3 * ::DDZ(f, outloc) + g11 * ::D2DX2(f, outloc) -// // // + g33 * ::D2DZ2(f, outloc) + 2 * g13 * ::D2DXDZ(f, outloc); -// // } -// // -// // return result; -// //} -// // -// //Coordinates::FieldMetric Coordinates::Laplace_par(const Field2D& f, CELL_LOC outloc) { -// // ASSERT1(location == outloc || outloc == CELL_DEFAULT) -// // -// // return D2DY2(f, outloc) / covariantMetricTensor.Getg22() -// // + DDY(this_J / covariantMetricTensor.Getg22(), outloc) * DDY(f, outloc) / this_J; -// //} -// // -// //Field3D Coordinates::Laplace_par(const Field3D& f, CELL_LOC outloc) { -// // ASSERT1(location == outloc || outloc == CELL_DEFAULT) -// // return D2DY2(f, outloc) / covariantMetricTensor.Getg22() -// // + DDY(this_J / covariantMetricTensor.Getg22(), outloc) * ::DDY(f, outloc) -// // / this_J; -// //} -// // -// //// Full Laplacian operator on scalar field -// // -// //Coordinates::FieldMetric Coordinates::Laplace(const Field2D& f, CELL_LOC outloc, -// // const std::string& dfdy_boundary_conditions, -// // const std::string& dfdy_dy_region) { -// // TRACE("Coordinates::Laplace( Field2D )"); -// // ASSERT1(location == outloc || outloc == CELL_DEFAULT) -// // -// // auto result = G1 * DDX(f, outloc) + G2 * DDY(f, outloc) -// // + contravariantMetricTensor.Getg11() * D2DX2(f, outloc) -// // + contravariantMetricTensor.Getg22() * D2DY2(f, outloc) -// // + 2.0 * contravariantMetricTensor.Getg12() -// // * D2DXDY(f, outloc, "DEFAULT", "RGN_NOBNDRY", -// // dfdy_boundary_conditions, dfdy_dy_region); -// // -// // return result; -// //} -// // -// //Field3D Coordinates::Laplace(const Field3D& f, CELL_LOC outloc, -// // const std::string& dfdy_boundary_conditions, -// // const std::string& dfdy_dy_region) { -// // TRACE("Coordinates::Laplace( Field3D )"); -// // ASSERT1(location == outloc || outloc == CELL_DEFAULT) -// // -// // Field3D result = G1 * ::DDX(f, outloc) + G2 * ::DDY(f, outloc) + G3 * ::DDZ(f, outloc) -// // + contravariantMetricTensor.Getg11() * D2DX2(f, outloc) -// // + contravariantMetricTensor.Getg22() * D2DY2(f, outloc) -// // + contravariantMetricTensor.Getg33() * D2DZ2(f, outloc) -// // + 2.0 -// // * (contravariantMetricTensor.Getg12() -// // * D2DXDY(f, outloc, "DEFAULT", "RGN_NOBNDRY", -// // dfdy_boundary_conditions, dfdy_dy_region) -// // + contravariantMetricTensor.Getg13() * D2DXDZ(f, outloc) -// // + contravariantMetricTensor.Getg23() * D2DYDZ(f, outloc)); -// // -// // return result; -// //} -// // -// //// Full perpendicular Laplacian, in form of inverse of Laplacian operator in LaplaceXY -// //// solver -// //Field2D Coordinates::Laplace_perpXY(MAYBE_UNUSED(const Field2D& A), -// // MAYBE_UNUSED(const Field2D& f)) { -// // TRACE("Coordinates::Laplace_perpXY( Field2D )"); -// //#if not(BOUT_USE_METRIC_3D) -// // Field2D result; -// // result.allocate(); -// // for (auto i : result.getRegion(RGN_NOBNDRY)) { -// // result[i] = 0.; -// // -// // // outer x boundary -// // const auto outer_x_avg = [&i](const auto& f) { return 0.5 * (f[i] + f[i.xp()]); }; -// // const BoutReal outer_x_A = outer_x_avg(A); -// // const BoutReal outer_x_J = outer_x_avg(this_J); -// // const BoutReal outer_x_g11 = outer_x_avg(contravariantMetricTensor.Getg11()); -// // const BoutReal outer_x_dx = outer_x_avg(dx); -// // const BoutReal outer_x_value = -// // outer_x_A * outer_x_J * outer_x_g11 / (this_J[i] * outer_x_dx * dx[i]); -// // result[i] += outer_x_value * (f[i.xp()] - f[i]); -// // -// // // inner x boundary -// // const auto inner_x_avg = [&i](const auto& f) { return 0.5 * (f[i] + f[i.xm()]); }; -// // const BoutReal inner_x_A = inner_x_avg(A); -// // const BoutReal inner_x_J = inner_x_avg(this_J); -// // const BoutReal inner_x_g11 = inner_x_avg(contravariantMetricTensor.Getg11()); -// // const BoutReal inner_x_dx = inner_x_avg(dx); -// // const BoutReal inner_x_value = -// // inner_x_A * inner_x_J * inner_x_g11 / (this_J[i] * inner_x_dx * dx[i]); -// // result[i] += inner_x_value * (f[i.xm()] - f[i]); -// // -// // // upper y boundary -// // const auto upper_y_avg = [&i](const auto& f) { return 0.5 * (f[i] + f[i.yp()]); }; -// // const BoutReal upper_y_A = upper_y_avg(A); -// // const BoutReal upper_y_J = upper_y_avg(this_J); -// // const BoutReal upper_y_g_22 = upper_y_avg(covariantMetricTensor.Getg22()); -// // const BoutReal upper_y_g23 = upper_y_avg(covariantMetricTensor.Getg23()); -// // const BoutReal upper_y_g_23 = upper_y_avg(covariantMetricTensor.Getg23()); -// // const BoutReal upper_y_dy = upper_y_avg(dy); -// // const BoutReal upper_y_value = -upper_y_A * upper_y_J * upper_y_g23 * upper_y_g_23 -// // / (upper_y_g_22 * this_J[i] * upper_y_dy * dy[i]); -// // result[i] += upper_y_value * (f[i.yp()] - f[i]); -// // -// // // lower y boundary -// // const auto lower_y_avg = [&i](const auto& f) { return 0.5 * (f[i] + f[i.ym()]); }; -// // const BoutReal lower_y_A = lower_y_avg(A); -// // const BoutReal lower_y_J = lower_y_avg(this_J); -// // const BoutReal lower_y_g_22 = lower_y_avg(covariantMetricTensor.Getg22()); -// // const BoutReal lower_y_g23 = lower_y_avg(contravariantMetricTensor.Getg23()); -// // const BoutReal lower_y_g_23 = lower_y_avg(covariantMetricTensor.Getg23()); -// // const BoutReal lower_y_dy = lower_y_avg(dy); -// // const BoutReal lower_y_value = -lower_y_A * lower_y_J * lower_y_g23 * lower_y_g_23 -// // / (lower_y_g_22 * this_J[i] * lower_y_dy * dy[i]); -// // result[i] += lower_y_value * (f[i.ym()] - f[i]); -// // } -// // -// // return result; -// //#else -// // throw BoutException("Coordinates::Laplace_perpXY for 3D metric not implemented"); -// //#endif -// //} -// // -// //const Coordinates::FieldMetric& Coordinates::invSg() const { -// // if (invSgCache == nullptr) { -// // auto ptr = std::make_unique(); -// // (*ptr) = 1.0 / sqrt(covariantMetricTensor.Getg22()); -// // invSgCache = std::move(ptr); -// // } -// // return *invSgCache; -// //} -// // -// //const Coordinates::FieldMetric& -// //Coordinates::Grad2_par2_DDY_invSg(CELL_LOC outloc, const std::string& method) const { -// // if (auto search = Grad2_par2_DDY_invSgCache.find(method); -// // search != Grad2_par2_DDY_invSgCache.end()) { -// // return *search->second; -// // } -// // invSg(); -// // -// // // Communicate to get parallel slices -// // localmesh->communicate(*invSgCache); -// // invSgCache->applyParallelBoundary("parallel_neumann"); -// // -// // // cache -// // auto ptr = std::make_unique(); -// // *ptr = DDY(*invSgCache, outloc, method) * invSg(); -// // Grad2_par2_DDY_invSgCache[method] = std::move(ptr); -// // return *Grad2_par2_DDY_invSgCache[method]; -// //} - void Geometry::checkCovariant(int ystart) { covariantMetricTensor.check(ystart); } void Geometry::checkContravariant(int ystart) { contravariantMetricTensor.check(ystart); } From 76fad4958ec8dba3224f9cfcc07d0046f070fc44 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Thu, 30 Nov 2023 11:02:30 +0000 Subject: [PATCH 222/491] Const references and move(). --- include/bout/geometry.hxx | 9 +++++---- src/mesh/geometry.cxx | 12 +++++++----- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/include/bout/geometry.hxx b/include/bout/geometry.hxx index 53ff4c4d37..151f359a4b 100644 --- a/include/bout/geometry.hxx +++ b/include/bout/geometry.hxx @@ -46,10 +46,11 @@ using FieldMetric = MetricTensor::FieldMetric; class Geometry { public: - Geometry(FieldMetric J, FieldMetric Bxy, FieldMetric g11, FieldMetric g22, - FieldMetric g33, FieldMetric g12, FieldMetric g13, FieldMetric g23, - FieldMetric g_11, FieldMetric g_22, FieldMetric g_33, FieldMetric g_12, - FieldMetric g_13, FieldMetric g_23, + Geometry(const FieldMetric& J, const FieldMetric& Bxy, const FieldMetric& g11, + const FieldMetric& g22, const FieldMetric& g33, const FieldMetric& g12, + const FieldMetric& g13, const FieldMetric& g23, const FieldMetric& g_11, + const FieldMetric& g_22, const FieldMetric& g_33, const FieldMetric& g_12, + const FieldMetric& g_13, const FieldMetric& g_23, DifferentialOperators* differential_operators); Geometry(Mesh* mesh, DifferentialOperators* differential_operators); diff --git a/src/mesh/geometry.cxx b/src/mesh/geometry.cxx index bd26cdbc87..4d5cee1a39 100644 --- a/src/mesh/geometry.cxx +++ b/src/mesh/geometry.cxx @@ -6,10 +6,12 @@ #include "bout/geometry.hxx" -Geometry::Geometry(FieldMetric J, FieldMetric Bxy, FieldMetric g11, FieldMetric g22, - FieldMetric g33, FieldMetric g12, FieldMetric g13, FieldMetric g23, - FieldMetric g_11, FieldMetric g_22, FieldMetric g_33, FieldMetric g_12, - FieldMetric g_13, FieldMetric g_23, +Geometry::Geometry(const FieldMetric& J, const FieldMetric& Bxy, const FieldMetric& g11, + const FieldMetric& g22, const FieldMetric& g33, const FieldMetric& g12, + const FieldMetric& g13, const FieldMetric& g23, + const FieldMetric& g_11, const FieldMetric& g_22, + const FieldMetric& g_33, const FieldMetric& g_12, + const FieldMetric& g_13, const FieldMetric& g_23, DifferentialOperators* differential_operators) : contravariantMetricTensor(g11, g22, g33, g12, g13, g23), covariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23), this_J(J), this_Bxy(Bxy), @@ -386,5 +388,5 @@ void Geometry::applyToContravariantMetricTensor( void Geometry::applyToCovariantMetricTensor( std::function function) { - covariantMetricTensor.map(function); + covariantMetricTensor.map(std::move(function)); } From 75b033f08f5faadcd7fcb98c44ad5e0af6da464d Mon Sep 17 00:00:00 2001 From: tomc271 Date: Thu, 30 Nov 2023 11:07:05 +0000 Subject: [PATCH 223/491] Null check differential_operators in Geometry constructors. --- src/mesh/geometry.cxx | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/mesh/geometry.cxx b/src/mesh/geometry.cxx index 4d5cee1a39..9a04391d87 100644 --- a/src/mesh/geometry.cxx +++ b/src/mesh/geometry.cxx @@ -15,7 +15,9 @@ Geometry::Geometry(const FieldMetric& J, const FieldMetric& Bxy, const FieldMetr DifferentialOperators* differential_operators) : contravariantMetricTensor(g11, g22, g33, g12, g13, g23), covariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23), this_J(J), this_Bxy(Bxy), - differential_operators(differential_operators) {} + differential_operators(differential_operators) { + ASSERT0(differential_operators != nullptr); +} Geometry::Geometry(Mesh* mesh, DifferentialOperators* differential_operators) //bool force_interpolate_from_centre @@ -27,7 +29,9 @@ Geometry::Geometry(Mesh* mesh, DifferentialOperators* differential_operators) covariantMetricTensor(1., 1., 1., 0, 0, 0, mesh), // Identity metric tensor this_J(1., mesh), this_Bxy(1., mesh), - differential_operators(differential_operators) {} + differential_operators(differential_operators) { + ASSERT0(differential_operators != nullptr); +} void Geometry::CalculateChristoffelSymbols(FieldMetric& dx, FieldMetric& dy) { // Calculate Christoffel symbol terms (18 independent values) From af5f757ed5795550942aad3055f35a2662bbe433 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Thu, 30 Nov 2023 17:37:49 +0000 Subject: [PATCH 224/491] Remove redundant use of std::move() in MetricTensor constructor. --- src/mesh/metricTensor.cxx | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/mesh/metricTensor.cxx b/src/mesh/metricTensor.cxx index 11ca8f8f9e..424a006a51 100644 --- a/src/mesh/metricTensor.cxx +++ b/src/mesh/metricTensor.cxx @@ -5,9 +5,7 @@ MetricTensor::MetricTensor(const FieldMetric& g11, const FieldMetric& g22, const FieldMetric& g33, const FieldMetric& g12, const FieldMetric& g13, const FieldMetric& g23) - : g11(std::move(g11)), g22(std::move(g22)), g33(std::move(g33)), g12(std::move(g12)), - g13(std::move(g13)), g23(std::move(g23)) { - + : g11(g11), g22(g22), g33(g33), g12(g12), g13(g13), g23(g23) { Allocate(); // Make sure metric elements are allocated // ; TODO: Required? } From 90d970cd2d3323be6d15327d45db1b30af2ee845 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Thu, 30 Nov 2023 18:36:03 +0000 Subject: [PATCH 225/491] Bug fix (setCovariantMetricTensor not setContravariantMetricTensor!) --- src/mesh/coordinates.cxx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 1267b5c12a..59c3768dce 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -484,7 +484,7 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, g_12 = getAtLocOrUnaligned(mesh, "g_12", 0.0, suffix, location); g_13 = getAtLocOrUnaligned(mesh, "g_13", 0.0, suffix, location); g_23 = getAtLocOrUnaligned(mesh, "g_23", 0.0, suffix, location); - geometry.setContravariantMetricTensor( + geometry.setCovariantMetricTensor( MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23), location); output_warn.write( From 5d2a9dd10f75601382c85d85eef2d73e419182b1 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Fri, 1 Dec 2023 14:40:44 +0000 Subject: [PATCH 226/491] Minor fixes. --- include/bout/coordinates.hxx | 46 ++++++++++++++++-------------------- src/mesh/coordinates.cxx | 39 ++++++++++-------------------- 2 files changed, 33 insertions(+), 52 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index a4a08e09bf..5eac2e21a0 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -151,17 +151,17 @@ public: // Operators /////////////////////////////////////////////////////////// - FieldMetric DDX(const Field2D& f, CELL_LOC outloc = CELL_DEFAULT, - const std::string& method = "DEFAULT", - const std::string& region = "RGN_NOBNDRY"); + Field2D DDX(const Field2D& f, CELL_LOC outloc = CELL_DEFAULT, + const std::string& method = "DEFAULT", + const std::string& region = "RGN_NOBNDRY"); - FieldMetric DDY(const Field2D& f, CELL_LOC outloc = CELL_DEFAULT, - const std::string& method = "DEFAULT", - const std::string& region = "RGN_NOBNDRY") const; + Field2D DDY(const Field2D& f, CELL_LOC outloc = CELL_DEFAULT, + const std::string& method = "DEFAULT", + const std::string& region = "RGN_NOBNDRY") const; - FieldMetric DDZ(const Field2D& f, CELL_LOC outloc = CELL_DEFAULT, - const std::string& method = "DEFAULT", - const std::string& region = "RGN_NOBNDRY"); + Field2D DDZ(const Field2D& f, CELL_LOC outloc = CELL_DEFAULT, + const std::string& method = "DEFAULT", + const std::string& region = "RGN_NOBNDRY"); Field3D DDX(const Field3D& f, CELL_LOC outloc = CELL_DEFAULT, const std::string& method = "DEFAULT", @@ -171,10 +171,6 @@ public: const std::string& method = "DEFAULT", const std::string& region = "RGN_NOBNDRY") const; - Field3D DDZ(const Field3D& f, CELL_LOC outloc = CELL_DEFAULT, - const std::string& method = "DEFAULT", - const std::string& region = "RGN_NOBNDRY"); - /// Gradient along magnetic field b.Grad(f) Field2D Grad_par(const Field2D& var, CELL_LOC outloc = CELL_DEFAULT, const std::string& method = "DEFAULT"); @@ -183,43 +179,43 @@ public: const std::string& method = "DEFAULT"); /// Advection along magnetic field V*b.Grad(f) - FieldMetric Vpar_Grad_par(const Field2D& v, const Field2D& f, - CELL_LOC outloc = CELL_DEFAULT, - const std::string& method = "DEFAULT"); + Field2D Vpar_Grad_par(const Field2D& v, const Field2D& f, + CELL_LOC outloc = CELL_DEFAULT, + const std::string& method = "DEFAULT"); Field3D Vpar_Grad_par(const Field3D& v, const Field3D& f, CELL_LOC outloc = CELL_DEFAULT, const std::string& method = "DEFAULT"); /// Divergence along magnetic field Div(b*f) = B.Grad(f/B) - FieldMetric Div_par(const Field2D& f, CELL_LOC outloc = CELL_DEFAULT, - const std::string& method = "DEFAULT"); + Field2D Div_par(const Field2D& f, CELL_LOC outloc = CELL_DEFAULT, + const std::string& method = "DEFAULT"); Field3D Div_par(const Field3D& f, CELL_LOC outloc = CELL_DEFAULT, const std::string& method = "DEFAULT"); // Second derivative along magnetic field - FieldMetric Grad2_par2(const Field2D& f, CELL_LOC outloc = CELL_DEFAULT, - const std::string& method = "DEFAULT"); + Field2D Grad2_par2(const Field2D& f, CELL_LOC outloc = CELL_DEFAULT, + const std::string& method = "DEFAULT"); Field3D Grad2_par2(const Field3D& f, CELL_LOC outloc = CELL_DEFAULT, const std::string& method = "DEFAULT"); // Perpendicular Laplacian operator, using only X-Z derivatives // NOTE: This might be better bundled with the Laplacian inversion code // since it makes use of the same coefficients and FFT routines - FieldMetric Delp2(const Field2D& f, CELL_LOC outloc = CELL_DEFAULT, bool useFFT = true); + Field2D Delp2(const Field2D& f, CELL_LOC outloc = CELL_DEFAULT, bool useFFT = true); Field3D Delp2(const Field3D& f, CELL_LOC outloc = CELL_DEFAULT, bool useFFT = true); FieldPerp Delp2(const FieldPerp& f, CELL_LOC outloc = CELL_DEFAULT, bool useFFT = true); // Full parallel Laplacian operator on scalar field // Laplace_par(f) = Div( b (b dot Grad(f)) ) - FieldMetric Laplace_par(const Field2D& f, CELL_LOC outloc = CELL_DEFAULT); + Field2D Laplace_par(const Field2D& f, CELL_LOC outloc = CELL_DEFAULT); Field3D Laplace_par(const Field3D& f, CELL_LOC outloc = CELL_DEFAULT); // Full Laplacian operator on scalar field - FieldMetric Laplace(const Field2D& f, CELL_LOC outloc = CELL_DEFAULT, - const std::string& dfdy_boundary_conditions = "free_o3", - const std::string& dfdy_dy_region = ""); + Field2D Laplace(const Field2D& f, CELL_LOC outloc = CELL_DEFAULT, + const std::string& dfdy_boundary_conditions = "free_o3", + const std::string& dfdy_dy_region = ""); Field3D Laplace(const Field3D& f, CELL_LOC outloc = CELL_DEFAULT, const std::string& dfdy_boundary_conditions = "free_o3", const std::string& dfdy_dy_region = ""); diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 59c3768dce..cb8c34fdc8 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -1023,9 +1023,8 @@ void Coordinates::setParallelTransform(Options* options) { * *******************************************************************************/ -Coordinates::FieldMetric Coordinates::DDX(const Field2D& f, CELL_LOC loc, - const std::string& method, - const std::string& region) { +Field2D Coordinates::DDX(const Field2D& f, CELL_LOC loc, const std::string& method, + const std::string& region) { ASSERT1(location == loc || loc == CELL_DEFAULT) return differential_operators.DDX(f, dx, loc, method, region); } @@ -1035,39 +1034,25 @@ Field3D Coordinates::DDX(const Field3D& f, CELL_LOC outloc, const std::string& m return differential_operators.DDX(f, dx, dz, IntShiftTorsion, outloc, method, region); } -Coordinates::FieldMetric Coordinates::DDY(const Field2D& f, CELL_LOC loc, - const std::string& method, - const std::string& region) const { +Field2D Coordinates::DDY(const Field2D& f, CELL_LOC loc, const std::string& method, + const std::string& region) const { ASSERT1(location == loc || loc == CELL_DEFAULT) return differential_operators.DDY(f, dy, loc, method, region); } Field3D Coordinates::DDY(const Field3D& f, CELL_LOC outloc, const std::string& method, const std::string& region) const { -#if BOUT_USE_METRIC_3D - if (!f.hasParallelSlices() and !transform->canToFromFieldAligned()) { - Field3D f_parallel = f; - transform->calcParallelSlices(f_parallel); - f_parallel.applyParallelBoundary("parallel_neumann"); - return bout::derivatives::index::DDY(f_parallel, outloc, method, region); - } -#endif return differential_operators.DDY(f, dy, outloc, method, region); } -Coordinates::FieldMetric Coordinates::DDZ(const Field2D& f, CELL_LOC loc, - const std::string& UNUSED(method), - const std::string& UNUSED(region)) { +Field2D Coordinates::DDZ(const Field2D& f, CELL_LOC loc, const std::string& method, + const std::string& region) { ASSERT1(location == loc || loc == CELL_DEFAULT) ASSERT1(f.getMesh() == localmesh) if (loc == CELL_DEFAULT) { loc = f.getLocation(); } - return zeroFrom(f).setLocation(loc); -} -Field3D Coordinates::DDZ(const Field3D& f, CELL_LOC outloc, const std::string& method, - const std::string& region) { - return bout::derivatives::index::DDZ(f, outloc, method, region) / dz; + return differential_operators.DDZ(f, loc, method, region); } ///////////////////////////////////////////////////////// @@ -1113,7 +1098,7 @@ Field3D Coordinates::Vpar_Grad_par(const Field3D& v, const Field3D& f, CELL_LOC ///////////////////////////////////////////////////////// // Parallel divergence -Coordinates::FieldMetric Coordinates::Div_par(const Field2D& f, CELL_LOC outloc, +Field2D Coordinates::Div_par(const Field2D& f, CELL_LOC outloc, const std::string& method) { TRACE("Coordinates::Div_par( Field2D )"); ASSERT1(location == outloc || outloc == CELL_DEFAULT) @@ -1152,7 +1137,7 @@ Field3D Coordinates::Div_par(const Field3D& f, CELL_LOC outloc, // second parallel derivative (b dot Grad)(b dot Grad) // Note: For parallel Laplacian use Laplace_par -Coordinates::FieldMetric Coordinates::Grad2_par2(const Field2D& f, CELL_LOC outloc, +Field2D Coordinates::Grad2_par2(const Field2D& f, CELL_LOC outloc, const std::string& method) { TRACE("Coordinates::Grad2_par2( Field2D )"); ASSERT1(location == outloc || (outloc == CELL_DEFAULT && location == f.getLocation())) @@ -1175,7 +1160,7 @@ Field3D Coordinates::Grad2_par2(const Field3D& f, CELL_LOC outloc, #include // Delp2 uses same coefficients as inversion code #include -Coordinates::FieldMetric Coordinates::Delp2(const Field2D& f, CELL_LOC outloc, +Field2D Coordinates::Delp2(const Field2D& f, CELL_LOC outloc, bool UNUSED(useFFT)) { TRACE("Coordinates::Delp2( Field2D )"); ASSERT1(location == outloc || outloc == CELL_DEFAULT) @@ -1313,7 +1298,7 @@ FieldPerp Coordinates::Delp2(const FieldPerp& f, CELL_LOC outloc, bool useFFT) { return result; } -Coordinates::FieldMetric Coordinates::Laplace_par(const Field2D& f, CELL_LOC outloc) { +Field2D Coordinates::Laplace_par(const Field2D& f, CELL_LOC outloc) { ASSERT1(location == outloc || outloc == CELL_DEFAULT) return D2DY2(f, outloc) / g22() + DDY(J() / g22(), outloc) * DDY(f, outloc) / J(); @@ -1326,7 +1311,7 @@ Field3D Coordinates::Laplace_par(const Field3D& f, CELL_LOC outloc) { // Full Laplacian operator on scalar field -Coordinates::FieldMetric Coordinates::Laplace(const Field2D& f, CELL_LOC outloc, +Field2D Coordinates::Laplace(const Field2D& f, CELL_LOC outloc, const std::string& dfdy_boundary_conditions, const std::string& dfdy_dy_region) { TRACE("Coordinates::Laplace( Field2D )"); From 27dc6b5e075be6bba9699b092e041d17efb78eaf Mon Sep 17 00:00:00 2001 From: tomc271 Date: Fri, 1 Dec 2023 14:50:30 +0000 Subject: [PATCH 227/491] Consistent use of 'const' on differential operator methods. Readded Field3D DDZ() method. --- include/bout/coordinates.hxx | 10 +++++++--- include/bout/differential_operators.hxx | 10 +++++----- src/mesh/coordinates.cxx | 22 +++++++++++++--------- src/mesh/differential_operators.cxx | 13 ++++++++----- 4 files changed, 33 insertions(+), 22 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index 5eac2e21a0..5cd429eb2e 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -153,7 +153,7 @@ public: Field2D DDX(const Field2D& f, CELL_LOC outloc = CELL_DEFAULT, const std::string& method = "DEFAULT", - const std::string& region = "RGN_NOBNDRY"); + const std::string& region = "RGN_NOBNDRY") const; Field2D DDY(const Field2D& f, CELL_LOC outloc = CELL_DEFAULT, const std::string& method = "DEFAULT", @@ -161,16 +161,20 @@ public: Field2D DDZ(const Field2D& f, CELL_LOC outloc = CELL_DEFAULT, const std::string& method = "DEFAULT", - const std::string& region = "RGN_NOBNDRY"); + const std::string& region = "RGN_NOBNDRY") const; Field3D DDX(const Field3D& f, CELL_LOC outloc = CELL_DEFAULT, const std::string& method = "DEFAULT", - const std::string& region = "RGN_NOBNDRY"); + const std::string& region = "RGN_NOBNDRY") const; Field3D DDY(const Field3D& f, CELL_LOC outloc = CELL_DEFAULT, const std::string& method = "DEFAULT", const std::string& region = "RGN_NOBNDRY") const; + Field3D DDZ(const Field3D& f, CELL_LOC outloc = CELL_DEFAULT, + const std::string& method = "DEFAULT", + const std::string& region = "RGN_NOBNDRY") const; + /// Gradient along magnetic field b.Grad(f) Field2D Grad_par(const Field2D& var, CELL_LOC outloc = CELL_DEFAULT, const std::string& method = "DEFAULT"); diff --git a/include/bout/differential_operators.hxx b/include/bout/differential_operators.hxx index c1b2bfffb7..ddd87c89f2 100644 --- a/include/bout/differential_operators.hxx +++ b/include/bout/differential_operators.hxx @@ -13,7 +13,7 @@ public: Field2D DDX(const Field2D& f, const Field2D& dx, CELL_LOC outloc = CELL_DEFAULT, const std::string& method = "DEFAULT", - const std::string& region = "RGN_NOBNDRY"); + const std::string& region = "RGN_NOBNDRY") const; Field2D DDY(const Field2D& f, const Field2D& dy, CELL_LOC outloc = CELL_DEFAULT, const std::string& method = "DEFAULT", @@ -21,12 +21,12 @@ public: Field2D DDZ(const Field2D& f, CELL_LOC outloc = CELL_DEFAULT, const std::string& method = "DEFAULT", - const std::string& region = "RGN_NOBNDRY"); + const std::string& region = "RGN_NOBNDRY") const; Field3D DDX(const Field3D& f, const Field3D& dx, const Field3D& dz, - FieldMetric& intShiftTorsion, CELL_LOC outloc = CELL_DEFAULT, + const FieldMetric& intShiftTorsion, CELL_LOC outloc = CELL_DEFAULT, const std::string& method = "DEFAULT", - const std::string& region = "RGN_NOBNDRY"); + const std::string& region = "RGN_NOBNDRY") const; Field3D DDY(const Field3D& f, const Field3D& dy, CELL_LOC outloc = CELL_DEFAULT, const std::string& method = "DEFAULT", @@ -34,7 +34,7 @@ public: Field3D DDZ(const Field3D& f, const Field3D& dz, CELL_LOC outloc = CELL_DEFAULT, const std::string& method = "DEFAULT", - const std::string& region = "RGN_NOBNDRY"); + const std::string& region = "RGN_NOBNDRY") const; /// Gradient along magnetic field b.Grad(f) Field2D Grad_par(const Field2D& var, const Field2D& dy, diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index cb8c34fdc8..ab34d25888 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -1024,13 +1024,13 @@ void Coordinates::setParallelTransform(Options* options) { *******************************************************************************/ Field2D Coordinates::DDX(const Field2D& f, CELL_LOC loc, const std::string& method, - const std::string& region) { + const std::string& region) const { ASSERT1(location == loc || loc == CELL_DEFAULT) return differential_operators.DDX(f, dx, loc, method, region); } Field3D Coordinates::DDX(const Field3D& f, CELL_LOC outloc, const std::string& method, - const std::string& region) { + const std::string& region) const { return differential_operators.DDX(f, dx, dz, IntShiftTorsion, outloc, method, region); } @@ -1046,7 +1046,7 @@ Field3D Coordinates::DDY(const Field3D& f, CELL_LOC outloc, const std::string& m } Field2D Coordinates::DDZ(const Field2D& f, CELL_LOC loc, const std::string& method, - const std::string& region) { + const std::string& region) const { ASSERT1(location == loc || loc == CELL_DEFAULT) ASSERT1(f.getMesh() == localmesh) if (loc == CELL_DEFAULT) { @@ -1055,6 +1055,11 @@ Field2D Coordinates::DDZ(const Field2D& f, CELL_LOC loc, const std::string& meth return differential_operators.DDZ(f, loc, method, region); } +Field3D Coordinates::DDZ(const Field3D& f, CELL_LOC outloc, const std::string& method, + const std::string& region) const { + return differential_operators.DDZ(f, dz, outloc, method, region); +} + ///////////////////////////////////////////////////////// // Parallel gradient @@ -1099,7 +1104,7 @@ Field3D Coordinates::Vpar_Grad_par(const Field3D& v, const Field3D& f, CELL_LOC // Parallel divergence Field2D Coordinates::Div_par(const Field2D& f, CELL_LOC outloc, - const std::string& method) { + const std::string& method) { TRACE("Coordinates::Div_par( Field2D )"); ASSERT1(location == outloc || outloc == CELL_DEFAULT) @@ -1138,7 +1143,7 @@ Field3D Coordinates::Div_par(const Field3D& f, CELL_LOC outloc, // Note: For parallel Laplacian use Laplace_par Field2D Coordinates::Grad2_par2(const Field2D& f, CELL_LOC outloc, - const std::string& method) { + const std::string& method) { TRACE("Coordinates::Grad2_par2( Field2D )"); ASSERT1(location == outloc || (outloc == CELL_DEFAULT && location == f.getLocation())) @@ -1160,8 +1165,7 @@ Field3D Coordinates::Grad2_par2(const Field3D& f, CELL_LOC outloc, #include // Delp2 uses same coefficients as inversion code #include -Field2D Coordinates::Delp2(const Field2D& f, CELL_LOC outloc, - bool UNUSED(useFFT)) { +Field2D Coordinates::Delp2(const Field2D& f, CELL_LOC outloc, bool UNUSED(useFFT)) { TRACE("Coordinates::Delp2( Field2D )"); ASSERT1(location == outloc || outloc == CELL_DEFAULT) @@ -1312,8 +1316,8 @@ Field3D Coordinates::Laplace_par(const Field3D& f, CELL_LOC outloc) { // Full Laplacian operator on scalar field Field2D Coordinates::Laplace(const Field2D& f, CELL_LOC outloc, - const std::string& dfdy_boundary_conditions, - const std::string& dfdy_dy_region) { + const std::string& dfdy_boundary_conditions, + const std::string& dfdy_dy_region) { TRACE("Coordinates::Laplace( Field2D )"); ASSERT1(location == outloc || outloc == CELL_DEFAULT) diff --git a/src/mesh/differential_operators.cxx b/src/mesh/differential_operators.cxx index 2f6085d501..4337537f9b 100644 --- a/src/mesh/differential_operators.cxx +++ b/src/mesh/differential_operators.cxx @@ -6,13 +6,15 @@ DifferentialOperators::DifferentialOperators(Mesh* mesh) : mesh(mesh) {} Field2D DifferentialOperators::DDX(const Field2D& f, const Field2D& dx, CELL_LOC loc, - const std::string& method, const std::string& region) { + const std::string& method, + const std::string& region) const { return bout::derivatives::index::DDX(f, loc, method, region) / dx; } Field3D DifferentialOperators::DDX(const Field3D& f, const Field3D& dx, const Field3D& dz, - FieldMetric& intShiftTorsion, CELL_LOC outloc, - const std::string& method, const std::string& region) { + const FieldMetric& intShiftTorsion, CELL_LOC outloc, + const std::string& method, + const std::string& region) const { auto result = bout::derivatives::index::DDX(f, outloc, method, region); result /= dx; @@ -46,7 +48,7 @@ Field3D DifferentialOperators::DDY(const Field3D& f, const Field3D& dy, CELL_LOC Field2D DifferentialOperators::DDZ(const Field2D& f, CELL_LOC loc, const std::string& UNUSED(method), - const std::string& UNUSED(region)) { + const std::string& UNUSED(region)) const { if (loc == CELL_DEFAULT) { loc = f.getLocation(); } @@ -54,7 +56,8 @@ Field2D DifferentialOperators::DDZ(const Field2D& f, CELL_LOC loc, } Field3D DifferentialOperators::DDZ(const Field3D& f, const Field3D& dz, CELL_LOC outloc, - const std::string& method, const std::string& region) { + const std::string& method, + const std::string& region) const { return bout::derivatives::index::DDZ(f, outloc, method, region) / dz; } From f6954dc8340643f59ad083f9f07b8e9200f10eff Mon Sep 17 00:00:00 2001 From: tomc271 Date: Fri, 1 Dec 2023 18:57:50 +0000 Subject: [PATCH 228/491] DifferentialOperators instance should belong to Mesh, as there is only one Mesh but a new Coordinates instance is created for every 'location'. --- include/bout/coordinates.hxx | 2 +- include/bout/mesh.hxx | 8 +++++- src/mesh/coordinates.cxx | 48 ++++++++++++++++++------------------ src/mesh/mesh.cxx | 10 ++++++++ 4 files changed, 42 insertions(+), 26 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index 5cd429eb2e..5a3df2695d 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -261,7 +261,7 @@ private: int nz; // Size of mesh in Z. This is mesh->ngz-1 Mesh* localmesh; CELL_LOC location; - DifferentialOperators differential_operators; + DifferentialOperators* differential_operators; Geometry geometry; /// Handles calculation of yup and ydown diff --git a/include/bout/mesh.hxx b/include/bout/mesh.hxx index 547a8bfc97..08450d1ddb 100644 --- a/include/bout/mesh.hxx +++ b/include/bout/mesh.hxx @@ -100,7 +100,9 @@ class Mesh { public: /// Constructor for a "bare", uninitialised Mesh /// Only useful for testing - Mesh() : source(nullptr), options(nullptr), include_corner_cells(true) {} + Mesh() + : source(nullptr), options(nullptr), include_corner_cells(true), + differential_operators(DifferentialOperators(this)) {} /// Constructor /// @param[in] s The source to be used for loading variables @@ -782,6 +784,8 @@ public: /// Creates RGN_{ALL,NOBNDRY,NOX,NOY} void createDefaultRegions(); + DifferentialOperators* getDifferentialOperators(); + protected: /// Source for grid data GridDataSource* source{nullptr}; @@ -792,6 +796,8 @@ protected: /// Mesh options section Options* options{nullptr}; + DifferentialOperators differential_operators; + /// Set whether to call calcParallelSlices on all communicated fields (true) or not (false) bool calcParallelSlices_on_communicate{true}; diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index ab34d25888..e9afe093fb 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -294,19 +294,19 @@ Coordinates::Coordinates(Mesh* mesh, FieldMetric dx, FieldMetric dy, FieldMetric FieldMetric IntShiftTorsion) : dx(std::move(dx)), dy(std::move(dy)), dz(dz), ShiftTorsion(std::move(ShiftTorsion)), IntShiftTorsion(std::move(IntShiftTorsion)), nz(mesh->LocalNz), localmesh(mesh), - location(CELL_CENTRE), differential_operators(DifferentialOperators(mesh)), + location(CELL_CENTRE), differential_operators(mesh->getDifferentialOperators()), geometry(Geometry(std::move(J), std::move(Bxy), std::move(g11), std::move(g22), std::move(g33), std::move(g12), std::move(g13), std::move(g23), std::move(g_11), std::move(g_22), std::move(g_33), std::move(g_12), std::move(g_13), std::move(g_23), - &differential_operators)) {} + differential_operators)) {} Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, const Coordinates* coords_in, bool force_interpolate_from_centre) : dx(1., mesh), dy(1., mesh), dz(1., mesh), d1_dx(mesh), d1_dy(mesh), d1_dz(mesh), ShiftTorsion(mesh), IntShiftTorsion(mesh), localmesh(mesh), location(loc), - differential_operators(DifferentialOperators(mesh)), - geometry(Geometry(mesh, &differential_operators)) { + differential_operators(mesh->getDifferentialOperators()), + geometry(Geometry(mesh, differential_operators)) { if (options == nullptr) { options = Options::getRoot()->getSection("mesh"); @@ -869,7 +869,7 @@ int Coordinates::calculateGeometry(bool recalculate_staggered, // Invalidate and recalculate cached variables zlength_cache.reset(); - differential_operators.invalidateAndRecalculateCachedVariables(); + differential_operators->invalidateAndRecalculateCachedVariables(); return 0; } @@ -1026,23 +1026,23 @@ void Coordinates::setParallelTransform(Options* options) { Field2D Coordinates::DDX(const Field2D& f, CELL_LOC loc, const std::string& method, const std::string& region) const { ASSERT1(location == loc || loc == CELL_DEFAULT) - return differential_operators.DDX(f, dx, loc, method, region); + return differential_operators->DDX(f, dx, loc, method, region); } Field3D Coordinates::DDX(const Field3D& f, CELL_LOC outloc, const std::string& method, const std::string& region) const { - return differential_operators.DDX(f, dx, dz, IntShiftTorsion, outloc, method, region); + return differential_operators->DDX(f, dx, dz, IntShiftTorsion, outloc, method, region); } Field2D Coordinates::DDY(const Field2D& f, CELL_LOC loc, const std::string& method, const std::string& region) const { ASSERT1(location == loc || loc == CELL_DEFAULT) - return differential_operators.DDY(f, dy, loc, method, region); + return differential_operators->DDY(f, dy, loc, method, region); } Field3D Coordinates::DDY(const Field3D& f, CELL_LOC outloc, const std::string& method, const std::string& region) const { - return differential_operators.DDY(f, dy, outloc, method, region); + return differential_operators->DDY(f, dy, outloc, method, region); } Field2D Coordinates::DDZ(const Field2D& f, CELL_LOC loc, const std::string& method, @@ -1052,12 +1052,12 @@ Field2D Coordinates::DDZ(const Field2D& f, CELL_LOC loc, const std::string& meth if (loc == CELL_DEFAULT) { loc = f.getLocation(); } - return differential_operators.DDZ(f, loc, method, region); + return differential_operators->DDZ(f, loc, method, region); } Field3D Coordinates::DDZ(const Field3D& f, CELL_LOC outloc, const std::string& method, const std::string& region) const { - return differential_operators.DDZ(f, dz, outloc, method, region); + return differential_operators->DDZ(f, dz, outloc, method, region); } ///////////////////////////////////////////////////////// @@ -1067,7 +1067,7 @@ Field2D Coordinates::Grad_par(const Field2D& var, [[maybe_unused]] CELL_LOC outl const std::string& UNUSED(method)) { TRACE("Coordinates::Grad_par( Field2D )"); - return differential_operators.Grad_par(var, dy, geometry.getCovariantMetricTensor()); + return differential_operators->Grad_par(var, dy, geometry.getCovariantMetricTensor()); } Field3D Coordinates::Grad_par(const Field3D& var, CELL_LOC outloc, @@ -1075,8 +1075,8 @@ Field3D Coordinates::Grad_par(const Field3D& var, CELL_LOC outloc, TRACE("Coordinates::Grad_par( Field3D )"); ASSERT1(location == outloc || outloc == CELL_DEFAULT) - return differential_operators.Grad_par(var, dy, geometry.getCovariantMetricTensor(), - outloc, method); + return differential_operators->Grad_par(var, dy, geometry.getCovariantMetricTensor(), + outloc, method); } ///////////////////////////////////////////////////////// @@ -1088,16 +1088,16 @@ Field2D Coordinates::Vpar_Grad_par(const Field2D& v, const Field2D& f, const std::string& UNUSED(method)) { ASSERT1(location == outloc || (outloc == CELL_DEFAULT && location == f.getLocation())) - return differential_operators.Vpar_Grad_par(v, f, geometry.getCovariantMetricTensor(), - outloc); + return differential_operators->Vpar_Grad_par(v, f, geometry.getCovariantMetricTensor(), + outloc); } Field3D Coordinates::Vpar_Grad_par(const Field3D& v, const Field3D& f, CELL_LOC outloc, const std::string& method) { ASSERT1(location == outloc || outloc == CELL_DEFAULT) - return differential_operators.Vpar_Grad_par(v, f, geometry.getCovariantMetricTensor(), - outloc, method); + return differential_operators->Vpar_Grad_par(v, f, geometry.getCovariantMetricTensor(), + outloc, method); } ///////////////////////////////////////////////////////// @@ -1108,7 +1108,7 @@ Field2D Coordinates::Div_par(const Field2D& f, CELL_LOC outloc, TRACE("Coordinates::Div_par( Field2D )"); ASSERT1(location == outloc || outloc == CELL_DEFAULT) - return differential_operators.Div_par( + return differential_operators->Div_par( f, geometry.Bxy(), dy, geometry.getCovariantMetricTensor(), outloc, method); } @@ -1124,7 +1124,7 @@ Field3D Coordinates::Div_par(const Field3D& f, CELL_LOC outloc, if (!f.hasParallelSlices()) { // No yup/ydown fields. The Grad_par operator will // shift to field aligned coordinates - return differential_operators.Div_par( + return differential_operators->Div_par( f, dy, geometry.Bxy(), geometry.getCovariantMetricTensor(), outloc, method); } @@ -1147,16 +1147,16 @@ Field2D Coordinates::Grad2_par2(const Field2D& f, CELL_LOC outloc, TRACE("Coordinates::Grad2_par2( Field2D )"); ASSERT1(location == outloc || (outloc == CELL_DEFAULT && location == f.getLocation())) - return differential_operators.Grad2_par2_DDY_invSg(geometry.getCovariantMetricTensor(), - dy, outloc, method); + return differential_operators->Grad2_par2_DDY_invSg(geometry.getCovariantMetricTensor(), + dy, outloc, method); } Field3D Coordinates::Grad2_par2(const Field3D& f, CELL_LOC outloc, const std::string& method) { TRACE("Coordinates::Grad2_par2( Field3D )"); - return differential_operators.Grad2_par2_DDY_invSg(geometry.getCovariantMetricTensor(), - dy, outloc, method); + return differential_operators->Grad2_par2_DDY_invSg(geometry.getCovariantMetricTensor(), + dy, outloc, method); } ///////////////////////////////////////////////////////// diff --git a/src/mesh/mesh.cxx b/src/mesh/mesh.cxx index 5efdde36ab..bb9b95c422 100644 --- a/src/mesh/mesh.cxx +++ b/src/mesh/mesh.cxx @@ -177,6 +177,7 @@ Mesh* Mesh::create(Options* opt) { return create(nullptr, opt); } Mesh::Mesh(GridDataSource* s, Options* opt) : source(s), options(opt == nullptr ? Options::getRoot()->getSection("mesh") : opt), + differential_operators(DifferentialOperators(this)), calcParallelSlices_on_communicate( (*options)["calcParallelSlices_on_communicate"] .doc("Calculate parallel slices on all communicated fields") @@ -989,3 +990,12 @@ std::optional Mesh::getCommonRegion(std::optional lhs, } return region3Dintersect[pos]; } + +DifferentialOperators* Mesh::getDifferentialOperators() { + return &differential_operators; +} + +constexpr decltype(MeshFactory::type_name) MeshFactory::type_name; +constexpr decltype(MeshFactory::section_name) MeshFactory::section_name; +constexpr decltype(MeshFactory::option_name) MeshFactory::option_name; +constexpr decltype(MeshFactory::default_type) MeshFactory::default_type; From 720940169a670c5112f0f956d66f95e953fd0677 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Fri, 1 Dec 2023 21:19:21 +0000 Subject: [PATCH 229/491] invSg and Grad2_par2_DDY_invSg methods and caches on Coordinates class. --- include/bout/coordinates.hxx | 11 ++++ include/bout/differential_operators.hxx | 30 ++------- src/mesh/coordinates.cxx | 74 +++++++++++++++++----- src/mesh/differential_operators.cxx | 84 +++++++++---------------- 4 files changed, 103 insertions(+), 96 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index 5a3df2695d..00835f99c1 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -257,6 +257,11 @@ public: void setG2(FieldMetric G2); void setG3(FieldMetric G3); + const FieldMetric& Grad2_par2_DDY_invSg(CELL_LOC outloc, + const std::string& method) const; + + const FieldMetric& invSg() const; + private: int nz; // Size of mesh in Z. This is mesh->ngz-1 Mesh* localmesh; @@ -271,6 +276,12 @@ private: /// `Coordinates::calculateGeometry` is called mutable std::unique_ptr zlength_cache{nullptr}; + /// Cache variable for Grad2_par2 + mutable std::map> Grad2_par2_DDY_invSgCache; + mutable std::unique_ptr invSgCache{nullptr}; + + void invalidateAndRecalculateCachedVariables(); + /// Set the parallel (y) transform from the options file. /// Used in the constructor to create the transform object. void setParallelTransform(Options* options); diff --git a/include/bout/differential_operators.hxx b/include/bout/differential_operators.hxx index ddd87c89f2..36e2d17545 100644 --- a/include/bout/differential_operators.hxx +++ b/include/bout/differential_operators.hxx @@ -37,32 +37,26 @@ public: const std::string& region = "RGN_NOBNDRY") const; /// Gradient along magnetic field b.Grad(f) - Field2D Grad_par(const Field2D& var, const Field2D& dy, - const MetricTensor& covariantMetricTensor, - CELL_LOC outloc = CELL_DEFAULT, const std::string& method = "DEFAULT"); + Field2D Grad_par(const Field2D& var, const Field2D& dy, CELL_LOC outloc = CELL_DEFAULT, + const std::string& method = "DEFAULT"); - Field3D Grad_par(const Field3D& var, const Field3D& dy, - const MetricTensor& covariantMetricTensor, - CELL_LOC outloc = CELL_DEFAULT, const std::string& method = "DEFAULT"); + Field3D Grad_par(const Field3D& var, const Field3D& dy, CELL_LOC outloc = CELL_DEFAULT, + const std::string& method = "DEFAULT"); /// Advection along magnetic field V*b.Grad(f) Field2D Vpar_Grad_par(const Field2D& v, const Field2D& f, - const MetricTensor& covariantMetricTensor, - CELL_LOC outloc = CELL_DEFAULT, + const Coordinates* coordinates, CELL_LOC outloc = CELL_DEFAULT, const std::string& method = "DEFAULT"); Field3D Vpar_Grad_par(const Field3D& v, const Field3D& f, - const MetricTensor& covariantMetricTensor, - CELL_LOC outloc = CELL_DEFAULT, + const Coordinates* coordinates, CELL_LOC outloc = CELL_DEFAULT, const std::string& method = "DEFAULT"); /// Divergence along magnetic field Div(b*f) = B.Grad(f/B) Field2D Div_par(const Field2D& f, const Field2D& Bxy, const Field2D& dy, - const MetricTensor& covariantMetricTensor, CELL_LOC outloc = CELL_DEFAULT, const std::string& method = "DEFAULT"); Field3D Div_par(const Field3D& f, const Field3D& Bxy, const Field3D& dy, - const MetricTensor& covariantMetricTensor, CELL_LOC outloc = CELL_DEFAULT, const std::string& method = "DEFAULT"); // Second derivative along magnetic field @@ -99,20 +93,8 @@ public: MetricTensor& covariantMetricTensor, const Field2D& J, const Field2D& dx, const Field2D& dy); - void invalidateAndRecalculateCachedVariables(); - - const Field2D& Grad2_par2_DDY_invSg(const MetricTensor& covariantMetricTensor, - const FieldMetric& dy, CELL_LOC outloc, - const std::string& method) const; - private: Mesh* mesh; - - /// Cache variable for Grad2_par2 - mutable std::map> Grad2_par2_DDY_invSgCache; - mutable std::unique_ptr invSgCache{nullptr}; - - FieldMetric& invSg(const MetricTensor& covariantMetricTensor) const; }; #endif //BOUT_DIFFERENTIALOPERATORS_HXX diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index e9afe093fb..1e1bf97db0 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -869,7 +869,7 @@ int Coordinates::calculateGeometry(bool recalculate_staggered, // Invalidate and recalculate cached variables zlength_cache.reset(); - differential_operators->invalidateAndRecalculateCachedVariables(); + invalidateAndRecalculateCachedVariables(); return 0; } @@ -1067,7 +1067,7 @@ Field2D Coordinates::Grad_par(const Field2D& var, [[maybe_unused]] CELL_LOC outl const std::string& UNUSED(method)) { TRACE("Coordinates::Grad_par( Field2D )"); - return differential_operators->Grad_par(var, dy, geometry.getCovariantMetricTensor()); + return differential_operators->Grad_par(var, dy); } Field3D Coordinates::Grad_par(const Field3D& var, CELL_LOC outloc, @@ -1075,8 +1075,7 @@ Field3D Coordinates::Grad_par(const Field3D& var, CELL_LOC outloc, TRACE("Coordinates::Grad_par( Field3D )"); ASSERT1(location == outloc || outloc == CELL_DEFAULT) - return differential_operators->Grad_par(var, dy, geometry.getCovariantMetricTensor(), - outloc, method); + return differential_operators->Grad_par(var, dy, outloc, method); } ///////////////////////////////////////////////////////// @@ -1088,16 +1087,14 @@ Field2D Coordinates::Vpar_Grad_par(const Field2D& v, const Field2D& f, const std::string& UNUSED(method)) { ASSERT1(location == outloc || (outloc == CELL_DEFAULT && location == f.getLocation())) - return differential_operators->Vpar_Grad_par(v, f, geometry.getCovariantMetricTensor(), - outloc); + return differential_operators->Vpar_Grad_par(v, f, this, outloc); } Field3D Coordinates::Vpar_Grad_par(const Field3D& v, const Field3D& f, CELL_LOC outloc, const std::string& method) { ASSERT1(location == outloc || outloc == CELL_DEFAULT) - return differential_operators->Vpar_Grad_par(v, f, geometry.getCovariantMetricTensor(), - outloc, method); + return differential_operators->Vpar_Grad_par(v, f, this, outloc, method); } ///////////////////////////////////////////////////////// @@ -1108,8 +1105,7 @@ Field2D Coordinates::Div_par(const Field2D& f, CELL_LOC outloc, TRACE("Coordinates::Div_par( Field2D )"); ASSERT1(location == outloc || outloc == CELL_DEFAULT) - return differential_operators->Div_par( - f, geometry.Bxy(), dy, geometry.getCovariantMetricTensor(), outloc, method); + return differential_operators->Div_par(f, geometry.Bxy(), dy, outloc, method); } Field3D Coordinates::Div_par(const Field3D& f, CELL_LOC outloc, @@ -1124,8 +1120,7 @@ Field3D Coordinates::Div_par(const Field3D& f, CELL_LOC outloc, if (!f.hasParallelSlices()) { // No yup/ydown fields. The Grad_par operator will // shift to field aligned coordinates - return differential_operators->Div_par( - f, dy, geometry.Bxy(), geometry.getCovariantMetricTensor(), outloc, method); + return differential_operators->Div_par(f, dy, geometry.Bxy(), outloc, method); } // Need to modify yup and ydown fields @@ -1147,16 +1142,29 @@ Field2D Coordinates::Grad2_par2(const Field2D& f, CELL_LOC outloc, TRACE("Coordinates::Grad2_par2( Field2D )"); ASSERT1(location == outloc || (outloc == CELL_DEFAULT && location == f.getLocation())) - return differential_operators->Grad2_par2_DDY_invSg(geometry.getCovariantMetricTensor(), - dy, outloc, method); + auto result = Grad2_par2_DDY_invSg(outloc, method) * DDY(f, outloc, method) + + D2DY2(f, outloc, method) / g_22(); + + return result; } Field3D Coordinates::Grad2_par2(const Field3D& f, CELL_LOC outloc, const std::string& method) { TRACE("Coordinates::Grad2_par2( Field3D )"); + if (outloc == CELL_DEFAULT) { + outloc = f.getLocation(); + } + ASSERT1(location == outloc); + + Field3D result = ::DDY(f, outloc, method); - return differential_operators->Grad2_par2_DDY_invSg(geometry.getCovariantMetricTensor(), - dy, outloc, method); + Field3D r2 = D2DY2(f, outloc, method) / g_22(); + + result = Grad2_par2_DDY_invSg(outloc, method) * result + r2; + + ASSERT2(result.getLocation() == outloc); + + return result; } ///////////////////////////////////////////////////////// @@ -1405,6 +1413,35 @@ Field2D Coordinates::Laplace_perpXY([[maybe_unused]] const Field2D& A, #endif } +const Coordinates::FieldMetric& Coordinates::invSg() const { + if (invSgCache == nullptr) { + auto ptr = std::make_unique(); + (*ptr) = 1.0 / sqrt(g22()); + invSgCache = std::move(ptr); + } + return *invSgCache; +} + +const Coordinates::FieldMetric& +Coordinates::Grad2_par2_DDY_invSg(CELL_LOC outloc, const std::string& method) const { + + if (auto search = Grad2_par2_DDY_invSgCache.find(method); + search != Grad2_par2_DDY_invSgCache.end()) { + return *search->second; + } + invSg(); + + // Communicate to get parallel slices + localmesh->communicate(*invSgCache); + invSgCache->applyParallelBoundary("parallel_neumann"); + + // cache + auto ptr = std::make_unique(); + *ptr = DDY(*invSgCache, outloc, method) * invSg(); + Grad2_par2_DDY_invSgCache[method] = std::move(ptr); + return *Grad2_par2_DDY_invSgCache[method]; +} + void Coordinates::checkCovariant() { return geometry.checkCovariant(localmesh->ystart); } void Coordinates::checkContravariant() { geometry.checkContravariant(localmesh->ystart); } @@ -1494,3 +1531,8 @@ void Coordinates::communicateChristoffelSymbolTerms() { G2_12(), G2_13(), G2_23(), G3_11(), G3_22(), G3_33(), G3_12(), G3_13(), G3_23(), G1(), G2(), G3()); } + +void Coordinates::invalidateAndRecalculateCachedVariables() { + Grad2_par2_DDY_invSgCache.clear(); + invSgCache.reset(); +} diff --git a/src/mesh/differential_operators.cxx b/src/mesh/differential_operators.cxx index 4337537f9b..c6599f61f7 100644 --- a/src/mesh/differential_operators.cxx +++ b/src/mesh/differential_operators.cxx @@ -65,20 +65,22 @@ Field3D DifferentialOperators::DDZ(const Field3D& f, const Field3D& dz, CELL_LOC // Parallel gradient Field2D DifferentialOperators::Grad_par(const Field2D& var, const Field2D& dy, - const MetricTensor& covariantMetricTensor, [[maybe_unused]] CELL_LOC outloc, const std::string& UNUSED(method)) { TRACE("DifferentialOperators::Grad_par( Field2D )"); - return DDY(var, dy) * invSg(covariantMetricTensor); + const auto coordinates = dy.getCoordinates(); //TODO: Find a better way + const auto invSg = coordinates->invSg(); + return DDY(var, dy) * invSg; } Field3D DifferentialOperators::Grad_par(const Field3D& var, const Field3D& dy, - const MetricTensor& covariantMetricTensor, CELL_LOC outloc, const std::string& method) { TRACE("DifferentialOperators::Grad_par( Field3D )"); - return DDY(var, dy, outloc, method) * invSg(covariantMetricTensor); + const auto coordinates = dy.getCoordinates(); //TODO: Find a better way + const auto invSg = coordinates->invSg(); + return DDY(var, dy, outloc, method) * invSg; } ///////////////////////////////////////////////////////// @@ -86,39 +88,37 @@ Field3D DifferentialOperators::Grad_par(const Field3D& var, const Field3D& dy, // vparallel times the parallel derivative along unperturbed B-field Field2D DifferentialOperators::Vpar_Grad_par(const Field2D& v, const Field2D& f, - const MetricTensor& covariantMetricTensor, + const Coordinates* coordinates, [[maybe_unused]] CELL_LOC outloc, const std::string& UNUSED(method)) { - return VDDY(v, f) * invSg(covariantMetricTensor); + return VDDY(v, f) * coordinates->invSg(); } Field3D DifferentialOperators::Vpar_Grad_par(const Field3D& v, const Field3D& f, - const MetricTensor& covariantMetricTensor, + const Coordinates* coordinates, CELL_LOC outloc, const std::string& method) { - return VDDY(v, f, outloc, method) * invSg(covariantMetricTensor); + return VDDY(v, f, outloc, method) * coordinates->invSg(); } ///////////////////////////////////////////////////////// // Parallel divergence Field2D DifferentialOperators::Div_par(const Field2D& f, const Field2D& Bxy, - const Field2D& dy, - const MetricTensor& covariantMetricTensor, - CELL_LOC outloc, const std::string& method) { + const Field2D& dy, CELL_LOC outloc, + const std::string& method) { TRACE("DifferentialOperators::Div_par( Field2D )"); // Need Bxy at location of f, which might be different from location of this // Coordinates object auto Bxy_floc = f.getCoordinates()->Bxy(); - return Bxy * Grad_par(f / Bxy_floc, dy, covariantMetricTensor, outloc, method); + return Bxy * Grad_par(f / Bxy_floc, dy, outloc, method); } Field3D DifferentialOperators::Div_par(const Field3D& f, const Field3D& Bxy, - const Field3D& dy, - const MetricTensor& covariantMetricTensor, - CELL_LOC outloc, const std::string& method) { + const Field3D& dy, CELL_LOC outloc, + const std::string& method) { TRACE("DifferentialOperators::Div_par( Field3D )"); // Need Bxy at location of f, which might be different from location of this @@ -128,7 +128,7 @@ Field3D DifferentialOperators::Div_par(const Field3D& f, const Field3D& Bxy, if (!f.hasParallelSlices()) { // No yup/ydown fields. The Grad_par operator will // shift to field aligned coordinates - return Bxy * Grad_par(f / Bxy_floc, dy, covariantMetricTensor, outloc, method); + return Bxy * Grad_par(f / Bxy_floc, dy, outloc, method); } // Need to modify yup and ydown fields @@ -138,7 +138,7 @@ Field3D DifferentialOperators::Div_par(const Field3D& f, const Field3D& Bxy, f_B.yup(i) = f.yup(i) / Bxy_floc.yup(i); f_B.ydown(i) = f.ydown(i) / Bxy_floc.ydown(i); } - return Bxy * Grad_par(f_B, dy, covariantMetricTensor, outloc, method); + return Bxy * Grad_par(f_B, dy, outloc, method); } ///////////////////////////////////////////////////////// @@ -150,8 +150,11 @@ Field2D DifferentialOperators::Grad2_par2(const Field2D& f, const Field2D& dy, CELL_LOC outloc, const std::string& method) { TRACE("DifferentialOperators::Grad2_par2( Field2D )"); - return Grad2_par2_DDY_invSg(covariantMetricTensor, dy, outloc, method) - * DDY(f, dy, outloc, method) + const auto coordinates = dy.getCoordinates(); //TODO: Find a better way + + const auto grad2_par2_DDY_invSg = coordinates->Grad2_par2_DDY_invSg(outloc, method); + + return grad2_par2_DDY_invSg * DDY(f, dy, outloc, method) + D2DY2(f, outloc, method) / covariantMetricTensor.Getg22(); } @@ -167,7 +170,12 @@ Field3D DifferentialOperators::Grad2_par2(const Field3D& f, const FieldMetric& d Field3D r2 = D2DY2(f, outloc, method) / covariantMetricTensor.Getg22(); - result = Grad2_par2_DDY_invSg(covariantMetricTensor, dy, outloc, method) * result + r2; + const auto coordinates = + covariantMetricTensor.Getg11().getCoordinates(); //TODO: Find a better way + + const auto grad2_par2_DDY_invSg = coordinates->Grad2_par2_DDY_invSg(outloc, method); + + result = grad2_par2_DDY_invSg * result + r2; ASSERT2(result.getLocation() == outloc) @@ -289,39 +297,3 @@ Field2D DifferentialOperators::Laplace_perpXY([[maybe_unused]] const Field2D& A, "DifferentialOperators::Laplace_perpXY for 3D metric not implemented"); #endif } - -FieldMetric& -DifferentialOperators::invSg(const MetricTensor& covariantMetricTensor) const { - if (invSgCache == nullptr) { - auto ptr = std::make_unique(); - (*ptr) = 1.0 / sqrt(covariantMetricTensor.Getg22()); - invSgCache = std::move(ptr); - } - return *invSgCache; -} - -const FieldMetric& -DifferentialOperators::Grad2_par2_DDY_invSg(const MetricTensor& covariantMetricTensor, - const FieldMetric& dy, CELL_LOC outloc, - const std::string& method) const { - if (auto search = Grad2_par2_DDY_invSgCache.find(method); - search != Grad2_par2_DDY_invSgCache.end()) { - return *search->second; - } - invSg(covariantMetricTensor); - - // Communicate to get parallel slices - mesh->communicate(*invSgCache); - invSgCache->applyParallelBoundary("parallel_neumann"); - - // cache - auto ptr = std::make_unique(); - *ptr = DDY(*invSgCache, dy, outloc, method) * invSg(covariantMetricTensor); - Grad2_par2_DDY_invSgCache[method] = std::move(ptr); - return *Grad2_par2_DDY_invSgCache[method]; -} - -void DifferentialOperators::invalidateAndRecalculateCachedVariables() { - Grad2_par2_DDY_invSgCache.clear(); - invSgCache.reset(); -} From 1319eda8b4679a0d92f3f9b659435616f67002a6 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Sun, 3 Dec 2023 14:47:03 +0000 Subject: [PATCH 230/491] Bug fix: g_xx not gxx. --- src/mesh/coordinates.cxx | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 1e1bf97db0..57e5a5ded8 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -612,12 +612,12 @@ void Coordinates::outputVars(Options& output_options) { output_options["g13" + loc_string].force(g13(), "Coordinates"); output_options["g23" + loc_string].force(g23(), "Coordinates"); - output_options["g_11" + loc_string].force(g11(), "Coordinates"); - output_options["g_22" + loc_string].force(g22(), "Coordinates"); - output_options["g_33" + loc_string].force(g33(), "Coordinates"); - output_options["g_12" + loc_string].force(g12(), "Coordinates"); - output_options["g_13" + loc_string].force(g13(), "Coordinates"); - output_options["g_23" + loc_string].force(g23(), "Coordinates"); + output_options["g_11" + loc_string].force(g_11(), "Coordinates"); + output_options["g_22" + loc_string].force(g_22(), "Coordinates"); + output_options["g_33" + loc_string].force(g_33(), "Coordinates"); + output_options["g_12" + loc_string].force(g_12(), "Coordinates"); + output_options["g_13" + loc_string].force(g_13(), "Coordinates"); + output_options["g_23" + loc_string].force(g_23(), "Coordinates"); output_options["J" + loc_string].force(J(), "Coordinates"); output_options["Bxy" + loc_string].force(Bxy(), "Coordinates"); @@ -648,8 +648,8 @@ int Coordinates::calculateGeometry(bool recalculate_staggered, bool force_interpolate_from_centre) { TRACE("Coordinates::calculateGeometry"); - communicate(dx, dy, dz, g11(), g22(), g33(), g12(), g13(), g23(), g11(), g22(), g33(), - g12(), g13(), g23(), J(), Bxy()); + communicate(dx, dy, dz, g11(), g22(), g33(), g12(), g13(), g23(), g_11(), g_22(), g_33(), + g_12(), g_13(), g_23(), J(), Bxy()); output_progress.write("Calculating differential geometry terms\n"); @@ -1313,12 +1313,12 @@ FieldPerp Coordinates::Delp2(const FieldPerp& f, CELL_LOC outloc, bool useFFT) { Field2D Coordinates::Laplace_par(const Field2D& f, CELL_LOC outloc) { ASSERT1(location == outloc || outloc == CELL_DEFAULT) - return D2DY2(f, outloc) / g22() + DDY(J() / g22(), outloc) * DDY(f, outloc) / J(); + return D2DY2(f, outloc) / g_22() + DDY(J() / g_22(), outloc) * DDY(f, outloc) / J(); } Field3D Coordinates::Laplace_par(const Field3D& f, CELL_LOC outloc) { ASSERT1(location == outloc || outloc == CELL_DEFAULT) - return D2DY2(f, outloc) / g22() + DDY(J() / g22(), outloc) * ::DDY(f, outloc) / J(); + return D2DY2(f, outloc) / g_22() + DDY(J() / g_22(), outloc) * ::DDY(f, outloc) / J(); } // Full Laplacian operator on scalar field @@ -1386,9 +1386,9 @@ Field2D Coordinates::Laplace_perpXY([[maybe_unused]] const Field2D& A, const auto upper_y_avg = [&i](const auto& f) { return 0.5 * (f[i] + f[i.yp()]); }; const BoutReal upper_y_A = upper_y_avg(A); const BoutReal upper_y_J = upper_y_avg(J()); - const BoutReal upper_y_g_22 = upper_y_avg(g22()); + const BoutReal upper_y_g_22 = upper_y_avg(g_22()); const BoutReal upper_y_g23 = upper_y_avg(g23()); - const BoutReal upper_y_g_23 = upper_y_avg(g23()); + const BoutReal upper_y_g_23 = upper_y_avg(g_23()); const BoutReal upper_y_dy = upper_y_avg(dy); const BoutReal upper_y_value = -upper_y_A * upper_y_J * upper_y_g23 * upper_y_g_23 / (upper_y_g_22 * J()[i] * upper_y_dy * dy[i]); @@ -1398,9 +1398,9 @@ Field2D Coordinates::Laplace_perpXY([[maybe_unused]] const Field2D& A, const auto lower_y_avg = [&i](const auto& f) { return 0.5 * (f[i] + f[i.ym()]); }; const BoutReal lower_y_A = lower_y_avg(A); const BoutReal lower_y_J = lower_y_avg(J()); - const BoutReal lower_y_g_22 = lower_y_avg(g22()); + const BoutReal lower_y_g_22 = lower_y_avg(g_22()); const BoutReal lower_y_g23 = lower_y_avg(g23()); - const BoutReal lower_y_g_23 = lower_y_avg(g23()); + const BoutReal lower_y_g_23 = lower_y_avg(g_23()); const BoutReal lower_y_dy = lower_y_avg(dy); const BoutReal lower_y_value = -lower_y_A * lower_y_J * lower_y_g23 * lower_y_g_23 / (lower_y_g_22 * J()[i] * lower_y_dy * dy[i]); @@ -1416,7 +1416,7 @@ Field2D Coordinates::Laplace_perpXY([[maybe_unused]] const Field2D& A, const Coordinates::FieldMetric& Coordinates::invSg() const { if (invSgCache == nullptr) { auto ptr = std::make_unique(); - (*ptr) = 1.0 / sqrt(g22()); + (*ptr) = 1.0 / sqrt(g_22()); invSgCache = std::move(ptr); } return *invSgCache; From c3531006bae66d88ade31a187c772a57396e9af0 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Sun, 3 Dec 2023 16:47:46 +0000 Subject: [PATCH 231/491] Clang-Tidy: Parameter 'function' is const-qualified in the function declaration; const-qualification of parameters only has an effect in function definitions. --- include/bout/metricTensor.hxx | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/include/bout/metricTensor.hxx b/include/bout/metricTensor.hxx index 4c43acd1ae..e9a3e8783b 100644 --- a/include/bout/metricTensor.hxx +++ b/include/bout/metricTensor.hxx @@ -18,8 +18,8 @@ public: MetricTensor(const FieldMetric& g11, const FieldMetric& g22, const FieldMetric& g33, const FieldMetric& g12, const FieldMetric& g13, const FieldMetric& g23); - MetricTensor(const BoutReal g11, const BoutReal g22, const BoutReal g33, - const BoutReal g12, const BoutReal g13, const BoutReal g23, Mesh* mesh); + MetricTensor(BoutReal g11, BoutReal g22, BoutReal g33, BoutReal g12, BoutReal g13, + BoutReal g23, Mesh* mesh); // check that tensors are positive (if expected) and finite (always) void check(int ystart); @@ -35,16 +35,16 @@ public: void Allocate(); - void setLocation(const CELL_LOC location); + void setLocation(CELL_LOC location); - MetricTensor oppositeRepresentation(const CELL_LOC location, + MetricTensor oppositeRepresentation(CELL_LOC location, const std::string& region = "RGN_ALL"); // Transforms the MetricTensor by applying the given function to every component - void map(const std::function function); + void map(std::function function); - MetricTensor applyToComponents( - const std::function function) const; + MetricTensor + applyToComponents(std::function function) const; protected: FieldMetric g11, g22, g33, g12, g13, g23; From 08f4f16e465a05c56bc16fdab380b330984b8112 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Sun, 3 Dec 2023 17:15:04 +0000 Subject: [PATCH 232/491] Remove redundant method getComponents() from MetricTensor class. --- include/bout/metricTensor.hxx | 2 -- src/mesh/metricTensor.cxx | 7 +------ 2 files changed, 1 insertion(+), 8 deletions(-) diff --git a/include/bout/metricTensor.hxx b/include/bout/metricTensor.hxx index e9a3e8783b..d45a6c5c4a 100644 --- a/include/bout/metricTensor.hxx +++ b/include/bout/metricTensor.hxx @@ -48,8 +48,6 @@ public: protected: FieldMetric g11, g22, g33, g12, g13, g23; - - std::vector getComponents() const; }; #endif //BOUT_METRICTENSOR_HXX diff --git a/src/mesh/metricTensor.cxx b/src/mesh/metricTensor.cxx index 424a006a51..543eba0ab6 100644 --- a/src/mesh/metricTensor.cxx +++ b/src/mesh/metricTensor.cxx @@ -168,11 +168,6 @@ MetricTensor MetricTensor::oppositeRepresentation(CELL_LOC location, return other_representation; } -std::vector MetricTensor::getComponents() const { - - return std::vector{g11, g22, g33, g12, g13, g23}; -} - void MetricTensor::map( const std::function function) { @@ -185,7 +180,7 @@ void MetricTensor::map( MetricTensor MetricTensor::applyToComponents( const std::function function) const { - const auto components_in = getComponents(); + const auto components_in = std::vector{g11, g22, g33, g12, g13, g23}; FieldMetric components_out[6]; From 730f8ac3bd6d92c7c025522880bd0c9d2af53f12 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Sun, 3 Dec 2023 17:40:05 +0000 Subject: [PATCH 233/491] Extract method Geometry::applyToChristoffelSymbols(). --- include/bout/geometry.hxx | 3 +++ src/mesh/coordinates.cxx | 57 +++++++-------------------------------- src/mesh/geometry.cxx | 16 +++++++++++ 3 files changed, 29 insertions(+), 47 deletions(-) diff --git a/include/bout/geometry.hxx b/include/bout/geometry.hxx index 151f359a4b..a58dfb7eda 100644 --- a/include/bout/geometry.hxx +++ b/include/bout/geometry.hxx @@ -164,6 +164,9 @@ public: void applyToCovariantMetricTensor( std::function function); + void applyToChristoffelSymbols( + const std::function function); + private: /// Christoffel symbol of the second kind (connection coefficients) FieldMetric G1_11_, G1_22_, G1_33_, G1_12_, G1_13_, G1_23_; diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 57e5a5ded8..be376bad05 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -648,8 +648,8 @@ int Coordinates::calculateGeometry(bool recalculate_staggered, bool force_interpolate_from_centre) { TRACE("Coordinates::calculateGeometry"); - communicate(dx, dy, dz, g11(), g22(), g33(), g12(), g13(), g23(), g_11(), g_22(), g_33(), - g_12(), g_13(), g_23(), J(), Bxy()); + communicate(dx, dy, dz, g11(), g22(), g33(), g12(), g13(), g23(), g_11(), g_22(), + g_33(), g_12(), g_13(), g_23(), J(), Bxy()); output_progress.write("Calculating differential geometry terms\n"); @@ -692,51 +692,14 @@ int Coordinates::calculateGeometry(bool recalculate_staggered, // CELL_YLOW grid is at a 'guard cell' location (yend+1). // However, the above would require lots of special handling, so just extrapolate for // now. - geometry.setG1_11(localmesh->interpolateAndExtrapolate(G1_11(), location, true, true, - true, transform.get())); - geometry.setG1_22(localmesh->interpolateAndExtrapolate(G1_22(), location, true, true, - true, transform.get())); - geometry.setG1_33(localmesh->interpolateAndExtrapolate(G1_33(), location, true, true, - true, transform.get())); - geometry.setG1_12(localmesh->interpolateAndExtrapolate(G1_12(), location, true, true, - true, transform.get())); - geometry.setG1_13(localmesh->interpolateAndExtrapolate(G1_13(), location, true, true, - true, transform.get())); - geometry.setG1_23(localmesh->interpolateAndExtrapolate(G1_23(), location, true, true, - true, transform.get())); - - geometry.setG2_11(localmesh->interpolateAndExtrapolate(G2_11(), location, true, true, - true, transform.get())); - geometry.setG2_22(localmesh->interpolateAndExtrapolate(G2_22(), location, true, true, - true, transform.get())); - geometry.setG2_33(localmesh->interpolateAndExtrapolate(G2_33(), location, true, true, - true, transform.get())); - geometry.setG2_12(localmesh->interpolateAndExtrapolate(G2_12(), location, true, true, - true, transform.get())); - geometry.setG2_13(localmesh->interpolateAndExtrapolate(G2_13(), location, true, true, - true, transform.get())); - geometry.setG2_23(localmesh->interpolateAndExtrapolate(G2_23(), location, true, true, - true, transform.get())); - - geometry.setG3_11(localmesh->interpolateAndExtrapolate(G3_11(), location, true, true, - true, transform.get())); - geometry.setG3_22(localmesh->interpolateAndExtrapolate(G3_22(), location, true, true, - true, transform.get())); - geometry.setG3_33(localmesh->interpolateAndExtrapolate(G3_33(), location, true, true, - true, transform.get())); - geometry.setG3_12(localmesh->interpolateAndExtrapolate(G3_12(), location, true, true, - true, transform.get())); - geometry.setG3_13(localmesh->interpolateAndExtrapolate(G3_13(), location, true, true, - true, transform.get())); - geometry.setG3_23(localmesh->interpolateAndExtrapolate(G3_23(), location, true, true, - true, transform.get())); - - geometry.setG1(localmesh->interpolateAndExtrapolate(G1(), location, true, true, true, - transform.get())); - geometry.setG2(localmesh->interpolateAndExtrapolate(G2(), location, true, true, true, - transform.get())); - geometry.setG3(localmesh->interpolateAndExtrapolate(G3(), location, true, true, true, - transform.get())); + + std::function const + interpolateAndExtrapolate_function = [this](const FieldMetric& component) { + return localmesh->interpolateAndExtrapolate(component, location, true, true, + false, transform.get()); + }; + + geometry.applyToChristoffelSymbols(interpolateAndExtrapolate_function); ////////////////////////////////////////////////////// /// Non-uniform meshes. Need to use DDX, DDY diff --git a/src/mesh/geometry.cxx b/src/mesh/geometry.cxx index 9a04391d87..7d18cc82f3 100644 --- a/src/mesh/geometry.cxx +++ b/src/mesh/geometry.cxx @@ -394,3 +394,19 @@ void Geometry::applyToCovariantMetricTensor( std::function function) { covariantMetricTensor.map(std::move(function)); } + +void Geometry::applyToChristoffelSymbols( + const std::function function) { + + const auto components_in = + std::vector{G1_11_, G1_22_, G1_33_, G1_12_, G1_13_, G1_23_, G2_11_, + G2_22_, G2_33_, G2_12_, G2_13_, G2_23_, G3_11_, G3_22_, + G3_33_, G3_12_, G3_13_, G3_23_, G1_, G2_, G3_}; + + FieldMetric components_out[21] = {G1_11_, G1_22_, G1_33_, G1_12_, G1_13_, G1_23_, + G2_11_, G2_22_, G2_33_, G2_12_, G2_13_, G2_23_, + G3_11_, G3_22_, G3_33_, G3_12_, G3_13_, G3_23_, + G1_, G2_, G3_}; + + std::transform(components_in.begin(), components_in.end(), components_out, function); +} From 71631c3e88fce767fb8028561c13fa759b29e239 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Sun, 3 Dec 2023 17:49:40 +0000 Subject: [PATCH 234/491] Extract method Coordinates::calculateCommunicateAndExtrapolateChristoffelSymbols(). --- include/bout/coordinates.hxx | 1 + src/mesh/coordinates.cxx | 68 +++++++++++++++++++----------------- 2 files changed, 37 insertions(+), 32 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index 00835f99c1..6553a78a88 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -305,6 +305,7 @@ private: bool no_extra_interpolate = false, ParallelTransform* pParallelTransform = nullptr); void communicateChristoffelSymbolTerms(); + void calculateCommunicateAndExtrapolateChristoffelSymbols(); }; /* diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index be376bad05..303bcd1ad7 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -668,38 +668,7 @@ int Coordinates::calculateGeometry(bool recalculate_staggered, // Check input metrics checkContravariant(); checkCovariant(); - geometry.CalculateChristoffelSymbols(dx, dy); - - auto tmp = J() * g12(); - communicate(tmp); - geometry.setG1((DDX(J() * g11()) + DDY(tmp) + DDZ(J() * g13())) / J()); - tmp = J() * g22(); - communicate(tmp); - geometry.setG2((DDX(J() * g12()) + DDY(tmp) + DDZ(J() * g23())) / J()); - tmp = J() * g23(); - communicate(tmp); - geometry.setG3((DDX(J() * g13()) + DDY(tmp) + DDZ(J() * g33())) / J()); - - communicateChristoffelSymbolTerms(); - - // Set boundary guard cells of Christoffel symbol terms - // Ideally, when location is staggered, we would set the upper/outer boundary point - // correctly rather than by extrapolating here: e.g. if location==CELL_YLOW and we are - // at the upper y-boundary the x- and z-derivatives at yend+1 at the boundary can be - // calculated because the guard cells are available, while the y-derivative could be - // calculated from the CELL_CENTRE metric components (which have guard cells available - // past the boundary location). This would avoid the problem that the y-boundary on the - // CELL_YLOW grid is at a 'guard cell' location (yend+1). - // However, the above would require lots of special handling, so just extrapolate for - // now. - - std::function const - interpolateAndExtrapolate_function = [this](const FieldMetric& component) { - return localmesh->interpolateAndExtrapolate(component, location, true, true, - false, transform.get()); - }; - - geometry.applyToChristoffelSymbols(interpolateAndExtrapolate_function); + calculateCommunicateAndExtrapolateChristoffelSymbols(); ////////////////////////////////////////////////////// /// Non-uniform meshes. Need to use DDX, DDY @@ -837,6 +806,41 @@ int Coordinates::calculateGeometry(bool recalculate_staggered, return 0; } +void Coordinates::calculateCommunicateAndExtrapolateChristoffelSymbols() { + geometry.CalculateChristoffelSymbols(dx, dy); + + auto tmp = J() * g12(); + communicate(tmp); + geometry.setG1((DDX(J() * g11()) + DDY(tmp) + DDZ(J() * g13())) / J()); + tmp = J() * g22(); + communicate(tmp); + geometry.setG2((DDX(J() * g12()) + DDY(tmp) + DDZ(J() * g23())) / J()); + tmp = J() * g23(); + communicate(tmp); + geometry.setG3((DDX(J() * g13()) + DDY(tmp) + DDZ(J() * g33())) / J()); + + communicateChristoffelSymbolTerms(); + + // Set boundary guard cells of Christoffel symbol terms + // Ideally, when location is staggered, we would set the upper/outer boundary point + // correctly rather than by extrapolating here: e.g. if location==CELL_YLOW and we are + // at the upper y-boundary the x- and z-derivatives at yend+1 at the boundary can be + // calculated because the guard cells are available, while the y-derivative could be + // calculated from the CELL_CENTRE metric components (which have guard cells available + // past the boundary location). This would avoid the problem that the y-boundary on the + // CELL_YLOW grid is at a 'guard cell' location (yend+1). + // However, the above would require lots of special handling, so just extrapolate for + // now. + + std::function const + interpolateAndExtrapolate_function = [this](const FieldMetric& component) { + return localmesh->interpolateAndExtrapolate(component, location, true, true, + false, transform.get()); + }; + + geometry.applyToChristoffelSymbols(interpolateAndExtrapolate_function); +} + void Coordinates::calcCovariant(const std::string& region) { TRACE("Coordinates::calcCovariant"); geometry.calcCovariant(location, region); From 909faf9722696905f8e753ba1003a27d8c67cde2 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Sun, 3 Dec 2023 18:19:17 +0000 Subject: [PATCH 235/491] Reduce duplication. --- src/mesh/coordinates.cxx | 143 +++++++++++++-------------------------- 1 file changed, 48 insertions(+), 95 deletions(-) diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 303bcd1ad7..44483e2cb6 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -668,6 +668,7 @@ int Coordinates::calculateGeometry(bool recalculate_staggered, // Check input metrics checkContravariant(); checkCovariant(); + calculateCommunicateAndExtrapolateChristoffelSymbols(); ////////////////////////////////////////////////////// @@ -681,117 +682,69 @@ int Coordinates::calculateGeometry(bool recalculate_staggered, // Read correction for non-uniform meshes std::string const suffix = getLocationSuffix(location); + + auto extrapolate_x = true; + auto extrapolate_y = true; if (location == CELL_CENTRE or (!force_interpolate_from_centre and localmesh->sourceHasVar("dx" + suffix))) { - bool const extrapolate_x = not localmesh->sourceHasXBoundaryGuards(); - bool const extrapolate_y = not localmesh->sourceHasYBoundaryGuards(); - - if (localmesh->get(d2x, "d2x" + suffix, 0.0, false, location) != 0) { - output_warn.write("\tWARNING: differencing quantity 'd2x' not found. " - "Calculating from dx\n"); - d1_dx = bout::derivatives::index::DDX(1. / dx); // d/di(1/dx) - - communicate(d1_dx); - d1_dx = localmesh->interpolateAndExtrapolate(d1_dx, location, true, true, true, - transform.get()); - } else { - d2x.setLocation(location); - // set boundary cells if necessary - d2x = localmesh->interpolateAndExtrapolate(d2x, location, extrapolate_x, - extrapolate_y, false, transform.get()); - - d1_dx = -d2x / (dx * dx); - } - - if (localmesh->get(d2y, "d2y" + suffix, 0.0, false, location) != 0) { - output_warn.write("\tWARNING: differencing quantity 'd2y' not found. " - "Calculating from dy\n"); - d1_dy = DDY(1. / dy); // d/di(1/dy) - - communicate(d1_dy); - d1_dy = localmesh->interpolateAndExtrapolate(d1_dy, location, true, true, true, - transform.get()); - } else { - d2y.setLocation(location); - // set boundary cells if necessary - d2y = localmesh->interpolateAndExtrapolate(d2y, location, extrapolate_x, - extrapolate_y, false, transform.get()); + extrapolate_x = not localmesh->sourceHasXBoundaryGuards(); + extrapolate_y = not localmesh->sourceHasYBoundaryGuards(); + } - d1_dy = -d2y / (dy * dy); - } + if (localmesh->get(d2x, "d2x" + suffix, 0.0, false, location) != 0) { + output_warn.write("\tWARNING: differencing quantity 'd2x' not found. " + "Calculating from dx\n"); + d1_dx = bout::derivatives::index::DDX(1. / dx); // d/di(1/dx) -#if BOUT_USE_METRIC_3D - if (localmesh->get(d2z, "d2z" + suffix, 0.0, false)) { - output_warn.write("\tWARNING: differencing quantity 'd2z' not found. " - "Calculating from dz\n"); - d1_dz = bout::derivatives::index::DDZ(1. / dz); - communicate(d1_dz); - d1_dz = localmesh->interpolateAndExtrapolate(d1_dz, location, true, true, true, - transform.get()); - } else { - d2z.setLocation(location); - // set boundary cells if necessary - d2z = localmesh->interpolateAndExtrapolate(d2z, location, extrapolate_x, - extrapolate_y, false, transform.get()); - - d1_dz = -d2z / (dz * dz); - } -#else - d1_dz = 0; -#endif - } else { - if (localmesh->get(d2x, "d2x", 0.0, false) != 0) { - output_warn.write("\tWARNING: differencing quantity 'd2x' not found. " - "Calculating from dx\n"); - d1_dx = bout::derivatives::index::DDX(1. / dx); // d/di(1/dx) - - communicate(d1_dx); - d1_dx = localmesh->interpolateAndExtrapolate(d1_dx, location, true, true, true, - transform.get()); - } else { - // Shift d2x to our location - d2x = localmesh->interpolateAndExtrapolate(d2x, location, true, true, false, + communicate(d1_dx); + d1_dx = localmesh->interpolateAndExtrapolate(d1_dx, location, true, true, true, transform.get()); + } else { + d2x.setLocation(location); + // set boundary cells if necessary + d2x = localmesh->interpolateAndExtrapolate(d2x, location, extrapolate_x, + extrapolate_y, false, transform.get()); - d1_dx = -d2x / (dx * dx); - } + d1_dx = -d2x / (dx * dx); + } - if (localmesh->get(d2y, "d2y", 0.0, false) != 0) { - output_warn.write("\tWARNING: differencing quantity 'd2y' not found. " - "Calculating from dy\n"); - d1_dy = DDY(1. / dy); // d/di(1/dy) + if (localmesh->get(d2y, "d2y" + suffix, 0.0, false, location) != 0) { + output_warn.write("\tWARNING: differencing quantity 'd2y' not found. " + "Calculating from dy\n"); + d1_dy = DDY(1. / dy); // d/di(1/dy) - communicate(d1_dy); - d1_dy = localmesh->interpolateAndExtrapolate(d1_dy, location, true, true, true, - transform.get()); - } else { - // Shift d2y to our location - d2y = localmesh->interpolateAndExtrapolate(d2y, location, true, true, false, + communicate(d1_dy); + d1_dy = localmesh->interpolateAndExtrapolate(d1_dy, location, true, true, true, transform.get()); + } else { + d2y.setLocation(location); + // set boundary cells if necessary + d2y = localmesh->interpolateAndExtrapolate(d2y, location, extrapolate_x, + extrapolate_y, false, transform.get()); - d1_dy = -d2y / (dy * dy); - } + d1_dy = -d2y / (dy * dy); + } #if BOUT_USE_METRIC_3D - if (localmesh->get(d2z, "d2z", 0.0, false)) { - output_warn.write("\tWARNING: differencing quantity 'd2z' not found. " - "Calculating from dz\n"); - d1_dz = bout::derivatives::index::DDZ(1. / dz); - - communicate(d1_dz); - d1_dz = localmesh->interpolateAndExtrapolate(d1_dz, location, true, true, true, - transform.get()); - } else { - // Shift d2z to our location - d2z = localmesh->interpolateAndExtrapolate(d2z, location, true, true, false, + if (localmesh->get(d2z, "d2z" + suffix, 0.0, false)) { + output_warn.write("\tWARNING: differencing quantity 'd2z' not found. " + "Calculating from dz\n"); + d1_dz = bout::derivatives::index::DDZ(1. / dz); + communicate(d1_dz); + d1_dz = localmesh->interpolateAndExtrapolate(d1_dz, location, true, true, true, transform.get()); + } else { + d2z.setLocation(location); + // set boundary cells if necessary + d2z = localmesh->interpolateAndExtrapolate(d2z, location, extrapolate_x, + extrapolate_y, false, transform.get()); - d1_dz = -d2z / (dz * dz); - } + d1_dz = -d2z / (dz * dz); + } #else - d1_dz = 0; + d1_dz = 0; #endif - } + communicate(d1_dx, d1_dy, d1_dz); if (location == CELL_CENTRE && recalculate_staggered) { From 6fd37fa2fca17c8baf85e04a8fac58891a443ecb Mon Sep 17 00:00:00 2001 From: tomc271 Date: Mon, 4 Dec 2023 12:20:22 +0000 Subject: [PATCH 236/491] Fix differential operators refactoring. --- include/bout/differential_operators.hxx | 37 ++++++++++++ src/mesh/coordinates.cxx | 50 +++++++++------- src/mesh/differential_operators.cxx | 76 +++++++++++++++++++++++++ 3 files changed, 143 insertions(+), 20 deletions(-) diff --git a/include/bout/differential_operators.hxx b/include/bout/differential_operators.hxx index 36e2d17545..e955bca795 100644 --- a/include/bout/differential_operators.hxx +++ b/include/bout/differential_operators.hxx @@ -11,6 +11,15 @@ class DifferentialOperators { public: DifferentialOperators(Mesh* mesh); + Field3D DDX(const Field3D& f, CELL_LOC outloc, const std::string& method = "DEFAULT", + const std::string& region = "RGN_NOBNDRY") const; + + Field3D DDY(const Field3D& f, CELL_LOC outloc, const std::string& method = "DEFAULT", + const std::string& region = "RGN_NOBNDRY") const; + + Field3D DDZ(const Field3D& f, CELL_LOC outloc, const std::string& method = "DEFAULT", + const std::string& region = "RGN_NOBNDRY") const; + Field2D DDX(const Field2D& f, const Field2D& dx, CELL_LOC outloc = CELL_DEFAULT, const std::string& method = "DEFAULT", const std::string& region = "RGN_NOBNDRY") const; @@ -36,6 +45,34 @@ public: const std::string& method = "DEFAULT", const std::string& region = "RGN_NOBNDRY") const; + Field2D D2DX2(const Field2D& field2D, CELL_LOC outloc); + + Field3D D2DX2(const Field3D& field3D, CELL_LOC outloc); + + Field2D D2DY2(const Field2D& field2D, CELL_LOC outloc = CELL_DEFAULT, + const std::string& method = "DEFAULT"); + + Field3D D2DY2(const Field3D& field3D, CELL_LOC outloc = CELL_DEFAULT, + const std::string& method = "DEFAULT"); + + Field2D D2DZ2(const Field2D& field2D, CELL_LOC outloc); + + Field3D D2DZ2(const Field3D& field3D, CELL_LOC outloc); + + Field2D D2DXDY(const Field2D& field2D, CELL_LOC outloc, const std::string& method, + const std::string& region, const std::string& dfdy_boundary_condition, + const std::string& dfdy_region); + + Field3D D2DXDY(const Field3D& field3D, CELL_LOC outloc, const std::string& method, + const std::string& region, const std::string& dfdy_boundary_condition, + const std::string& dfdy_region); + + Field2D D2DXDZ(const Field2D& field2D, CELL_LOC outloc); + Field3D D2DXDZ(const Field3D& field3D, CELL_LOC outloc); + + Field2D D2DYDZ(const Field2D& field2D, CELL_LOC outloc); + Field3D D2DYDZ(const Field3D& field3D, CELL_LOC outloc); + /// Gradient along magnetic field b.Grad(f) Field2D Grad_par(const Field2D& var, const Field2D& dy, CELL_LOC outloc = CELL_DEFAULT, const std::string& method = "DEFAULT"); diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 44483e2cb6..cdbabca16b 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -16,8 +16,6 @@ #include "parallel/fci.hxx" #include "parallel/shiftedmetricinterp.hxx" -#include "bout/derivs.hxx" -#include "bout/differential_operators.hxx" #include "bout/field2d.hxx" #include "bout/field3d.hxx" #include "bout/geometry.hxx" @@ -1063,7 +1061,7 @@ Field2D Coordinates::Grad2_par2(const Field2D& f, CELL_LOC outloc, ASSERT1(location == outloc || (outloc == CELL_DEFAULT && location == f.getLocation())) auto result = Grad2_par2_DDY_invSg(outloc, method) * DDY(f, outloc, method) - + D2DY2(f, outloc, method) / g_22(); + + differential_operators->D2DY2(f, outloc, method) / g_22(); // derivs 209 return result; } @@ -1076,9 +1074,9 @@ Field3D Coordinates::Grad2_par2(const Field3D& f, CELL_LOC outloc, } ASSERT1(location == outloc); - Field3D result = ::DDY(f, outloc, method); + Field3D result = differential_operators->DDY(f, outloc, method); - Field3D r2 = D2DY2(f, outloc, method) / g_22(); + Field3D r2 = differential_operators->D2DY2(f, outloc, method) / g_22(); result = Grad2_par2_DDY_invSg(outloc, method) * result + r2; @@ -1097,7 +1095,7 @@ Field2D Coordinates::Delp2(const Field2D& f, CELL_LOC outloc, bool UNUSED(useFFT TRACE("Coordinates::Delp2( Field2D )"); ASSERT1(location == outloc || outloc == CELL_DEFAULT) - return G1() * DDX(f, outloc) + g11() * D2DX2(f, outloc); + return G1() * DDX(f, outloc) + g11() * differential_operators->D2DX2(f, outloc); } Field3D Coordinates::Delp2(const Field3D& f, CELL_LOC outloc, bool useFFT) { @@ -1157,9 +1155,11 @@ Field3D Coordinates::Delp2(const Field3D& f, CELL_LOC outloc, bool useFFT) { } } } else { - result = G1() * ::DDX(f, outloc) + G3() * ::DDZ(f, outloc) - + g11() * ::D2DX2(f, outloc) + g33() * ::D2DZ2(f, outloc) - + 2 * g13() * ::D2DXDZ(f, outloc); + result = G1() * differential_operators->DDX(f, outloc) + + G3() * differential_operators->DDZ(f, outloc) + + g11() * differential_operators->D2DX2(f, outloc) + + g33() * differential_operators->D2DZ2(f, outloc) + + 2 * g13() * differential_operators->D2DXDZ(f, outloc); } ASSERT2(result.getLocation() == outloc) @@ -1233,12 +1233,15 @@ FieldPerp Coordinates::Delp2(const FieldPerp& f, CELL_LOC outloc, bool useFFT) { Field2D Coordinates::Laplace_par(const Field2D& f, CELL_LOC outloc) { ASSERT1(location == outloc || outloc == CELL_DEFAULT) - return D2DY2(f, outloc) / g_22() + DDY(J() / g_22(), outloc) * DDY(f, outloc) / J(); + return differential_operators->D2DY2(f, outloc) / g_22() + + DDY(J() / g_22(), outloc) * DDY(f, outloc) / J(); } Field3D Coordinates::Laplace_par(const Field3D& f, CELL_LOC outloc) { ASSERT1(location == outloc || outloc == CELL_DEFAULT) - return D2DY2(f, outloc) / g_22() + DDY(J() / g_22(), outloc) * ::DDY(f, outloc) / J(); + return differential_operators->D2DY2(f, outloc) / g_22() + + differential_operators->DDY(J() / g_22(), outloc) + * differential_operators->DDY(f, outloc) / J(); } // Full Laplacian operator on scalar field @@ -1249,11 +1252,12 @@ Field2D Coordinates::Laplace(const Field2D& f, CELL_LOC outloc, TRACE("Coordinates::Laplace( Field2D )"); ASSERT1(location == outloc || outloc == CELL_DEFAULT) - return G1() * DDX(f, outloc) + G2() * DDY(f, outloc) + g11() * D2DX2(f, outloc) - + g22() * D2DY2(f, outloc) + return G1() * DDX(f, outloc) + G2() * DDY(f, outloc) + + g11() * differential_operators->D2DX2(f, outloc) + + g22() * differential_operators->D2DY2(f, outloc) + 2.0 * g12() - * D2DXDY(f, outloc, "DEFAULT", "RGN_NOBNDRY", dfdy_boundary_conditions, - dfdy_dy_region); + * differential_operators->D2DXDY(f, outloc, "DEFAULT", "RGN_NOBNDRY", + dfdy_boundary_conditions, dfdy_dy_region); } Field3D Coordinates::Laplace(const Field3D& f, CELL_LOC outloc, @@ -1262,13 +1266,19 @@ Field3D Coordinates::Laplace(const Field3D& f, CELL_LOC outloc, TRACE("Coordinates::Laplace( Field3D )"); ASSERT1(location == outloc || outloc == CELL_DEFAULT) - return G1() * ::DDX(f, outloc) + G2() * ::DDY(f, outloc) + G3() * ::DDZ(f, outloc) - + g11() * D2DX2(f, outloc) + g22() * D2DY2(f, outloc) + g33() * D2DZ2(f, outloc) + return G1() * differential_operators->DDX(f, outloc) + + G2() * differential_operators->DDY(f, outloc) + + G3() * differential_operators->DDZ(f, outloc) + + g11() * differential_operators->D2DX2(f, outloc) + + g22() * differential_operators->D2DY2(f, outloc) + + g33() * differential_operators->D2DZ2(f, outloc) + 2.0 * (g12() - * D2DXDY(f, outloc, "DEFAULT", "RGN_NOBNDRY", - dfdy_boundary_conditions, dfdy_dy_region) - + g13() * D2DXDZ(f, outloc) + g23() * D2DYDZ(f, outloc)); + * differential_operators->D2DXDY( + f, outloc, "DEFAULT", "RGN_NOBNDRY", dfdy_boundary_conditions, + dfdy_dy_region) + + g13() * differential_operators->D2DXDZ(f, outloc) + + g23() * differential_operators->D2DYDZ(f, outloc)); } // Full perpendicular Laplacian, in form of inverse of Laplacian operator in LaplaceXY diff --git a/src/mesh/differential_operators.cxx b/src/mesh/differential_operators.cxx index c6599f61f7..71548d798e 100644 --- a/src/mesh/differential_operators.cxx +++ b/src/mesh/differential_operators.cxx @@ -5,6 +5,24 @@ DifferentialOperators::DifferentialOperators(Mesh* mesh) : mesh(mesh) {} +Field3D DifferentialOperators::DDX(const Field3D& f, CELL_LOC outloc, + const std::string& method, + const std::string& region) const { + return ::DDX(f, outloc, method, region); +} + +Field3D DifferentialOperators::DDY(const Field3D& f, CELL_LOC outloc, + const std::string& method, + const std::string& region) const { + return ::DDY(f, outloc, method, region); +} + +Field3D DifferentialOperators::DDZ(const Field3D& f, CELL_LOC outloc, + const std::string& method, + const std::string& region) const { + return ::DDZ(f, outloc, method, region); +} + Field2D DifferentialOperators::DDX(const Field2D& f, const Field2D& dx, CELL_LOC loc, const std::string& method, const std::string& region) const { @@ -61,6 +79,64 @@ Field3D DifferentialOperators::DDZ(const Field3D& f, const Field3D& dz, CELL_LOC return bout::derivatives::index::DDZ(f, outloc, method, region) / dz; } +Field2D DifferentialOperators::D2DX2(const Field2D& field2D, CELL_LOC outloc) { + return ::D2DX2(field2D, outloc); +} + +Field3D DifferentialOperators::D2DX2(const Field3D& field3D, CELL_LOC outloc) { + return ::D2DX2(field3D, outloc); +} + +Field2D DifferentialOperators::D2DY2(const Field2D& field2D, CELL_LOC outloc, + const std::string& method) { + return ::D2DY2(field2D, outloc, method); +} + +Field3D DifferentialOperators::D2DY2(const Field3D& field3D, CELL_LOC outloc, + const std::string& method) { + return ::D2DY2(field3D, outloc, method); +} + +Field2D DifferentialOperators::D2DZ2(const Field2D& field2D, CELL_LOC outloc) { + return ::D2DZ2(field2D, outloc); +} + +Field3D DifferentialOperators::D2DZ2(const Field3D& field3D, CELL_LOC outloc) { + return ::D2DZ2(field3D, outloc); +} + +Field2D DifferentialOperators::D2DXDY(const Field2D& field2D, CELL_LOC outloc, + const std::string& method, + const std::string& region, + const std::string& dfdy_boundary_condition, + const std::string& dfdy_region) { + return ::D2DXDY(field2D, outloc, method, region, dfdy_boundary_condition, dfdy_region); +} + +Field3D DifferentialOperators::D2DXDY(const Field3D& field3D, CELL_LOC outloc, + const std::string& method, + const std::string& region, + const std::string& dfdy_boundary_condition, + const std::string& dfdy_region) { + return ::D2DXDY(field3D, outloc, method, region, dfdy_boundary_condition, dfdy_region); +} + +Field2D DifferentialOperators::D2DXDZ(const Field2D& field2D, CELL_LOC outloc) { + return ::D2DXDZ(field2D, outloc); +} + +Field2D DifferentialOperators::D2DYDZ(const Field2D& field2D, CELL_LOC outloc) { + return ::D2DYDZ(field2D, outloc); +} + +Field3D DifferentialOperators::D2DXDZ(const Field3D& field3D, CELL_LOC outloc) { + return ::D2DXDZ(field3D, outloc); +} + +Field3D DifferentialOperators::D2DYDZ(const Field3D& field3D, CELL_LOC outloc) { + return ::D2DYDZ(field3D, outloc); +} + ///////////////////////////////////////////////////////// // Parallel gradient From ddc0e50994b7190348d13e2d592bf63efefcc27f Mon Sep 17 00:00:00 2001 From: tomc271 Date: Mon, 4 Dec 2023 12:38:45 +0000 Subject: [PATCH 237/491] Remove unused field Mesh from DifferentialOperators. --- include/bout/differential_operators.hxx | 5 ----- include/bout/mesh.hxx | 6 +++--- src/mesh/differential_operators.cxx | 2 -- src/mesh/mesh.cxx | 2 +- 4 files changed, 4 insertions(+), 11 deletions(-) diff --git a/include/bout/differential_operators.hxx b/include/bout/differential_operators.hxx index e955bca795..f4d760fa9f 100644 --- a/include/bout/differential_operators.hxx +++ b/include/bout/differential_operators.hxx @@ -9,8 +9,6 @@ class DifferentialOperators { using FieldMetric = MetricTensor::FieldMetric; public: - DifferentialOperators(Mesh* mesh); - Field3D DDX(const Field3D& f, CELL_LOC outloc, const std::string& method = "DEFAULT", const std::string& region = "RGN_NOBNDRY") const; @@ -129,9 +127,6 @@ public: Field2D Laplace_perpXY(const Field2D& A, const Field2D& f, MetricTensor& covariantMetricTensor, const Field2D& J, const Field2D& dx, const Field2D& dy); - -private: - Mesh* mesh; }; #endif //BOUT_DIFFERENTIALOPERATORS_HXX diff --git a/include/bout/mesh.hxx b/include/bout/mesh.hxx index 08450d1ddb..5824915078 100644 --- a/include/bout/mesh.hxx +++ b/include/bout/mesh.hxx @@ -102,7 +102,7 @@ public: /// Only useful for testing Mesh() : source(nullptr), options(nullptr), include_corner_cells(true), - differential_operators(DifferentialOperators(this)) {} + differential_operators(DifferentialOperators()) {} /// Constructor /// @param[in] s The source to be used for loading variables @@ -620,8 +620,8 @@ public: std::shared_ptr getCoordinatesSmart(const CELL_LOC location = CELL_CENTRE) { - ASSERT1(location != CELL_DEFAULT); - ASSERT1(location != CELL_VSHIFT); + ASSERT1(location != CELL_DEFAULT) + ASSERT1(location != CELL_VSHIFT) auto found = coords_map.find(location); if (found != coords_map.end()) { diff --git a/src/mesh/differential_operators.cxx b/src/mesh/differential_operators.cxx index 71548d798e..1f97b3ff6a 100644 --- a/src/mesh/differential_operators.cxx +++ b/src/mesh/differential_operators.cxx @@ -3,8 +3,6 @@ #include "bout/mesh.hxx" #include -DifferentialOperators::DifferentialOperators(Mesh* mesh) : mesh(mesh) {} - Field3D DifferentialOperators::DDX(const Field3D& f, CELL_LOC outloc, const std::string& method, const std::string& region) const { diff --git a/src/mesh/mesh.cxx b/src/mesh/mesh.cxx index bb9b95c422..d0c8e1250a 100644 --- a/src/mesh/mesh.cxx +++ b/src/mesh/mesh.cxx @@ -177,7 +177,7 @@ Mesh* Mesh::create(Options* opt) { return create(nullptr, opt); } Mesh::Mesh(GridDataSource* s, Options* opt) : source(s), options(opt == nullptr ? Options::getRoot()->getSection("mesh") : opt), - differential_operators(DifferentialOperators(this)), + differential_operators(DifferentialOperators()), calcParallelSlices_on_communicate( (*options)["calcParallelSlices_on_communicate"] .doc("Calculate parallel slices on all communicated fields") From 1eed6fed517b9a54fcd9ac6a3910e5a0d30258f6 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Mon, 4 Dec 2023 12:39:46 +0000 Subject: [PATCH 238/491] Remove unused field dy from DifferentialOperators::Grad2_par2 method. --- include/bout/differential_operators.hxx | 4 ++-- src/mesh/differential_operators.cxx | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/include/bout/differential_operators.hxx b/include/bout/differential_operators.hxx index f4d760fa9f..60e2206814 100644 --- a/include/bout/differential_operators.hxx +++ b/include/bout/differential_operators.hxx @@ -99,8 +99,8 @@ public: MetricTensor& covariantMetricTensor, CELL_LOC outloc = CELL_DEFAULT, const std::string& method = "DEFAULT"); - Field3D Grad2_par2(const Field3D& f, const FieldMetric& dy, - MetricTensor& covariantMetricTensor, CELL_LOC outloc = CELL_DEFAULT, + Field3D Grad2_par2(const Field3D& f, MetricTensor& covariantMetricTensor, + CELL_LOC outloc = CELL_DEFAULT, const std::string& method = "DEFAULT"); Field2D Laplace_par(const Field2D& f, const Field2D& g22, const Field2D& J, diff --git a/src/mesh/differential_operators.cxx b/src/mesh/differential_operators.cxx index 1f97b3ff6a..3823a46940 100644 --- a/src/mesh/differential_operators.cxx +++ b/src/mesh/differential_operators.cxx @@ -232,7 +232,7 @@ Field2D DifferentialOperators::Grad2_par2(const Field2D& f, const Field2D& dy, + D2DY2(f, outloc, method) / covariantMetricTensor.Getg22(); } -Field3D DifferentialOperators::Grad2_par2(const Field3D& f, const FieldMetric& dy, +Field3D DifferentialOperators::Grad2_par2(const Field3D& f, MetricTensor& covariantMetricTensor, CELL_LOC outloc, const std::string& method) { TRACE("DifferentialOperators::Grad2_par2( Field3D )"); From dd76c2a0be72d291774864c438bc365aee9eeff8 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Mon, 4 Dec 2023 12:42:08 +0000 Subject: [PATCH 239/491] Update existing variable. --- src/mesh/coordinates.cxx | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index cdbabca16b..5ffc09e144 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -315,8 +315,8 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, nz = mesh->LocalNz; // Default to true in case staggered quantities are not read from file - const bool extrapolate_x = true; - const bool extrapolate_y = true; + bool extrapolate_x = true; + bool extrapolate_y = true; if ((coords_in != nullptr) && !suffix.empty() && (force_interpolate_from_centre || !mesh->sourceHasVar("dx" + suffix))) { @@ -379,9 +379,9 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, // 'interpolateAndExtrapolate' to set them. Ensures that derivatives are // smooth at all the boundaries. - const bool extrapolate_x = + extrapolate_x = (*options)["extrapolate_x"].withDefault(not mesh->sourceHasXBoundaryGuards()); - const bool extrapolate_y = + extrapolate_y = (*options)["extrapolate_y"].withDefault(not mesh->sourceHasYBoundaryGuards()); if (extrapolate_x) { @@ -1072,7 +1072,7 @@ Field3D Coordinates::Grad2_par2(const Field3D& f, CELL_LOC outloc, if (outloc == CELL_DEFAULT) { outloc = f.getLocation(); } - ASSERT1(location == outloc); + ASSERT1(location == outloc) Field3D result = differential_operators->DDY(f, outloc, method); @@ -1080,7 +1080,7 @@ Field3D Coordinates::Grad2_par2(const Field3D& f, CELL_LOC outloc, result = Grad2_par2_DDY_invSg(outloc, method) * result + r2; - ASSERT2(result.getLocation() == outloc); + ASSERT2(result.getLocation() == outloc) return result; } From d35f4b97dff4a16d3cbdea6a8d492809c7bb8a57 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Mon, 4 Dec 2023 13:13:45 +0000 Subject: [PATCH 240/491] Remove unused #include statements. --- src/mesh/coordinates.cxx | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 5ffc09e144..6e2819f1e2 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -16,10 +16,7 @@ #include "parallel/fci.hxx" #include "parallel/shiftedmetricinterp.hxx" -#include "bout/field2d.hxx" -#include "bout/field3d.hxx" #include "bout/geometry.hxx" -#include "bout/metricTensor.hxx" #include // use anonymous namespace so this utility function is not available outside this file From 4001a1f29bf91a1f3cb4f06c524207d6f45c77f6 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Mon, 4 Dec 2023 15:28:51 +0000 Subject: [PATCH 241/491] Include zlength_cache.reset(); in invalidateAndRecalculateCachedVariables(). --- src/mesh/coordinates.cxx | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 6e2819f1e2..b92599c543 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -747,8 +747,6 @@ int Coordinates::calculateGeometry(bool recalculate_staggered, localmesh->recalculateStaggeredCoordinates(); } - // Invalidate and recalculate cached variables - zlength_cache.reset(); invalidateAndRecalculateCachedVariables(); return 0; @@ -1460,6 +1458,7 @@ void Coordinates::communicateChristoffelSymbolTerms() { } void Coordinates::invalidateAndRecalculateCachedVariables() { + zlength_cache.reset(); Grad2_par2_DDY_invSgCache.clear(); invSgCache.reset(); } From 9a6de2ee87be45f654ed432834d6dac17c93a37d Mon Sep 17 00:00:00 2001 From: tomc271 Date: Mon, 4 Dec 2023 13:36:42 +0000 Subject: [PATCH 242/491] Consistent use of bout::derivatives::index::DDY(), rather than differential_operators->DDY(). --- src/mesh/coordinates.cxx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index b92599c543..532f7b4653 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -706,7 +706,7 @@ int Coordinates::calculateGeometry(bool recalculate_staggered, if (localmesh->get(d2y, "d2y" + suffix, 0.0, false, location) != 0) { output_warn.write("\tWARNING: differencing quantity 'd2y' not found. " "Calculating from dy\n"); - d1_dy = DDY(1. / dy); // d/di(1/dy) + d1_dy = bout::derivatives::index::DDY(1. / dy); // d/di(1/dy) communicate(d1_dy); d1_dy = localmesh->interpolateAndExtrapolate(d1_dy, location, true, true, true, From 9a0bf195aea64e03057b64d31ebbb053bbc25a76 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Mon, 4 Dec 2023 13:58:43 +0000 Subject: [PATCH 243/491] Reorder arguments in initializer list to match declaration order. --- include/bout/mesh.hxx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/bout/mesh.hxx b/include/bout/mesh.hxx index 5824915078..fc3fdd60ee 100644 --- a/include/bout/mesh.hxx +++ b/include/bout/mesh.hxx @@ -101,8 +101,8 @@ public: /// Constructor for a "bare", uninitialised Mesh /// Only useful for testing Mesh() - : source(nullptr), options(nullptr), include_corner_cells(true), - differential_operators(DifferentialOperators()) {} + : source(nullptr), options(nullptr), + differential_operators(DifferentialOperators()), include_corner_cells(true) {} /// Constructor /// @param[in] s The source to be used for loading variables From eeaa4050883a9b83d4135a89e213fe4750c371c0 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Mon, 4 Dec 2023 14:50:25 +0000 Subject: [PATCH 244/491] Extract method Coordinates::correctionForNonUniformMeshes(). --- include/bout/coordinates.hxx | 3 +++ src/mesh/coordinates.cxx | 29 +++++++++++++++-------------- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index 6553a78a88..bc2adac203 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -306,6 +306,9 @@ private: void communicateChristoffelSymbolTerms(); void calculateCommunicateAndExtrapolateChristoffelSymbols(); + + /// Non-uniform meshes. Need to use DDX, DDY + void correctionForNonUniformMeshes(bool force_interpolate_from_centre); }; /* diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 532f7b4653..cdbedeb7f0 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -666,14 +666,24 @@ int Coordinates::calculateGeometry(bool recalculate_staggered, calculateCommunicateAndExtrapolateChristoffelSymbols(); - ////////////////////////////////////////////////////// - /// Non-uniform meshes. Need to use DDX, DDY + correctionForNonUniformMeshes(force_interpolate_from_centre); + if (location == CELL_CENTRE && recalculate_staggered) { + // Re-calculate interpolated Coordinates at staggered locations + localmesh->recalculateStaggeredCoordinates(); + } + + invalidateAndRecalculateCachedVariables(); + + return 0; +} + +void Coordinates::correctionForNonUniformMeshes(bool force_interpolate_from_centre) { OPTION(Options::getRoot(), non_uniform, true); - Coordinates::FieldMetric d2x(localmesh); - Coordinates::FieldMetric d2y(localmesh); - Coordinates::FieldMetric const d2z(localmesh); // d^2 x / d i^2 + FieldMetric d2x(localmesh); + FieldMetric d2y(localmesh); + FieldMetric const d2z(localmesh); // d^2 x / d i^2 // Read correction for non-uniform meshes std::string const suffix = getLocationSuffix(location); @@ -741,15 +751,6 @@ int Coordinates::calculateGeometry(bool recalculate_staggered, #endif communicate(d1_dx, d1_dy, d1_dz); - - if (location == CELL_CENTRE && recalculate_staggered) { - // Re-calculate interpolated Coordinates at staggered locations - localmesh->recalculateStaggeredCoordinates(); - } - - invalidateAndRecalculateCachedVariables(); - - return 0; } void Coordinates::calculateCommunicateAndExtrapolateChristoffelSymbols() { From 0bf1187bcbbb5858d13aa6568c78755bd37cd911 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Mon, 4 Dec 2023 15:11:23 +0000 Subject: [PATCH 245/491] Extract methods 'interpolateFieldsFromOtherCoordinates' and 'setBoundaryCells'. --- include/bout/coordinates.hxx | 4 + src/mesh/coordinates.cxx | 486 ++++++++++++++++++----------------- 2 files changed, 249 insertions(+), 241 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index bc2adac203..e5d815d903 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -309,6 +309,10 @@ private: /// Non-uniform meshes. Need to use DDX, DDY void correctionForNonUniformMeshes(bool force_interpolate_from_centre); + + void interpolateFieldsFromOtherCoordinates(const Mesh* mesh, Options* options, const Coordinates* coords_in); + void setBoundaryCells(Mesh* mesh, Options* options, const Coordinates* coords_in, + const std::string& suffix); }; /* diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index cdbedeb7f0..20c4f547e5 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -307,196 +307,194 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, options = Options::getRoot()->getSection("mesh"); } - std::string suffix = getLocationSuffix(location); - nz = mesh->LocalNz; - // Default to true in case staggered quantities are not read from file - bool extrapolate_x = true; - bool extrapolate_y = true; + std::string suffix = getLocationSuffix(location); if ((coords_in != nullptr) && !suffix.empty() && (force_interpolate_from_centre || !mesh->sourceHasVar("dx" + suffix))) { + interpolateFieldsFromOtherCoordinates(mesh, options, coords_in); + } else { + setBoundaryCells(mesh, options, coords_in, suffix); + } +} - // Interpolate fields from coords_in +void Coordinates::interpolateFieldsFromOtherCoordinates(const Mesh* mesh, + Options* options, + const Coordinates* coords_in) { - if (isUniform(coords_in->dz)) { - dz = coords_in->dz; - dz.setLocation(location); - } else { - throw BoutException("We are asked to transform dz to get dz before we " - "have a transform, which " - "might require dz!\nPlease provide a dz for the " - "staggered quantity!"); - } - setParallelTransform(options); - dx = localmesh->interpolateAndExtrapolate(coords_in->dx, location, true, true, false, - transform.get()); - dy = localmesh->interpolateAndExtrapolate(coords_in->dy, location, true, true, false, - transform.get()); - // not really needed - we have used dz already ... - dz = localmesh->interpolateAndExtrapolate(coords_in->dz, location, true, true, false, - transform.get()); - - std::function const - interpolateAndExtrapolate_function = [this](const FieldMetric& component) { - return localmesh->interpolateAndExtrapolate(component, location, true, true, - false, transform.get()); - }; - - const auto region = std::basic_string("RGN_NOBNDRY"); - setContravariantMetricTensor(coords_in->getContravariantMetricTensor(), region); - - geometry.applyToContravariantMetricTensor(interpolateAndExtrapolate_function); - geometry.applyToCovariantMetricTensor(interpolateAndExtrapolate_function); - - // Check input metrics - checkContravariant(); - checkCovariant(); - - geometry.setJ(localmesh->interpolateAndExtrapolate(coords_in->J(), location, true, + if (isUniform(coords_in->dz)) { + dz = coords_in->dz; + dz.setLocation(location); + } else { + throw BoutException("We are asked to transform dz to get dz before we " + "have a transform, which " + "might require dz!\nPlease provide a dz for the " + "staggered quantity!"); + } + setParallelTransform(options); + dx = localmesh->interpolateAndExtrapolate(coords_in->dx, location, true, true, false, + transform.get()); + dy = localmesh->interpolateAndExtrapolate(coords_in->dy, location, true, true, false, + transform.get()); + // not really needed - we have used dz already ... + dz = localmesh->interpolateAndExtrapolate(coords_in->dz, location, true, true, false, + transform.get()); + + std::function const + interpolateAndExtrapolate_function = [this](const FieldMetric& component) { + return localmesh->interpolateAndExtrapolate(component, location, true, true, + false, transform.get()); + }; + + const auto region = std::basic_string("RGN_NOBNDRY"); + setContravariantMetricTensor(coords_in->getContravariantMetricTensor(), region); + + geometry.applyToContravariantMetricTensor(interpolateAndExtrapolate_function); + geometry.applyToCovariantMetricTensor(interpolateAndExtrapolate_function); + + // Check input metrics + checkContravariant(); + checkCovariant(); + + geometry.setJ(localmesh->interpolateAndExtrapolate(coords_in->J(), location, true, true, + false, transform.get())); + geometry.setBxy(localmesh->interpolateAndExtrapolate(coords_in->Bxy(), location, true, true, false, transform.get())); - geometry.setBxy(localmesh->interpolateAndExtrapolate(coords_in->Bxy(), location, true, - true, false, transform.get())); - bout::checkFinite(J(), "The Jacobian", "RGN_NOCORNERS"); - bout::checkPositive(J(), "The Jacobian", "RGN_NOCORNERS"); - bout::checkFinite(Bxy(), "Bxy", "RGN_NOCORNERS"); - bout::checkPositive(Bxy(), "Bxy", "RGN_NOCORNERS"); + bout::checkFinite(J(), "The Jacobian", "RGN_NOCORNERS"); + bout::checkPositive(J(), "The Jacobian", "RGN_NOCORNERS"); + bout::checkFinite(Bxy(), "Bxy", "RGN_NOCORNERS"); + bout::checkPositive(Bxy(), "Bxy", "RGN_NOCORNERS"); - ShiftTorsion = localmesh->interpolateAndExtrapolate( - coords_in->ShiftTorsion, location, true, true, false, transform.get()); + ShiftTorsion = localmesh->interpolateAndExtrapolate(coords_in->ShiftTorsion, location, + true, true, false, transform.get()); - if (mesh->IncIntShear) { - IntShiftTorsion = localmesh->interpolateAndExtrapolate( - coords_in->IntShiftTorsion, location, true, true, false, transform.get()); - } - } else { - // Note: If boundary cells were not loaded from the grid file, use - // 'interpolateAndExtrapolate' to set them. Ensures that derivatives are - // smooth at all the boundaries. - - extrapolate_x = - (*options)["extrapolate_x"].withDefault(not mesh->sourceHasXBoundaryGuards()); - extrapolate_y = - (*options)["extrapolate_y"].withDefault(not mesh->sourceHasYBoundaryGuards()); - - if (extrapolate_x) { - output_warn.write(_("WARNING: extrapolating input mesh quantities into x-boundary " - "cells. Set option extrapolate_x=false to disable this.\n")); - } + if (mesh->IncIntShear) { + IntShiftTorsion = localmesh->interpolateAndExtrapolate( + coords_in->IntShiftTorsion, location, true, true, false, transform.get()); + } +} - if (extrapolate_y) { - output_warn.write(_("WARNING: extrapolating input mesh quantities into y-boundary " - "cells. Set option extrapolate_y=false to disable this.\n")); - } +// Note: If boundary cells were not loaded from the grid file, use +// 'interpolateAndExtrapolate' to set them. Ensures that derivatives are +// smooth at all the boundaries. +void Coordinates::setBoundaryCells(Mesh* mesh, Options* options, + const Coordinates* coords_in, + const std::string& suffix) { + + const bool extrapolate_x = + (*options)["extrapolate_x"].withDefault(not mesh->sourceHasXBoundaryGuards()); + const bool extrapolate_y = + (*options)["extrapolate_y"].withDefault(not mesh->sourceHasYBoundaryGuards()); + + if (extrapolate_x) { + output_warn.write(_("WARNING: extrapolating input mesh quantities into x-boundary " + "cells. Set option extrapolate_x=false to disable this.\n")); + } - if (coords_in == nullptr) { - mesh->get(dx, "dx", 1.0, false); - mesh->get(dy, "dy", 1.0, false); - } + if (extrapolate_y) { + output_warn.write(_("WARNING: extrapolating input mesh quantities into y-boundary " + "cells. Set option extrapolate_y=false to disable this.\n")); + } - nz = mesh->LocalNz; + if (coords_in == nullptr) { + mesh->get(dx, "dx", 1.0, false); + mesh->get(dy, "dy", 1.0, false); + } - { - auto& options = Options::root(); - const bool has_zperiod = options.isSet("zperiod"); - const auto zmin = has_zperiod ? 0.0 : options["ZMIN"].withDefault(0.0); - const auto zmax = has_zperiod ? 1.0 / options["zperiod"].withDefault(1.0) - : options["ZMAX"].withDefault(1.0); + nz = mesh->LocalNz; - const auto default_dz = (zmax - zmin) * TWOPI / nz; - getAtLoc(mesh, dz, "dz", suffix, location, default_dz); - } + { + auto& options = Options::root(); + const bool has_zperiod = options.isSet("zperiod"); + const auto zmin = has_zperiod ? 0.0 : options["ZMIN"].withDefault(0.0); + const auto zmax = has_zperiod ? 1.0 / options["zperiod"].withDefault(1.0) + : options["ZMAX"].withDefault(1.0); + + const auto default_dz = (zmax - zmin) * TWOPI / nz; + getAtLoc(mesh, dz, "dz", suffix, location, default_dz); + } - // required early for differentiation. - setParallelTransform(options); + // required early for differentiation. + setParallelTransform(options); - dz = localmesh->interpolateAndExtrapolate(dz, location, extrapolate_x, extrapolate_y, - false, transform.get()); + dz = localmesh->interpolateAndExtrapolate(dz, location, extrapolate_x, extrapolate_y, + false, transform.get()); - dx = getUnalignedAtLocationAndFillGuards(mesh, "dx", 1.0, suffix, location, - extrapolate_x, extrapolate_y, false, - transform.get()); + dx = getUnalignedAtLocationAndFillGuards(mesh, "dx", 1.0, suffix, location, + extrapolate_x, extrapolate_y, false, + transform.get()); - if (mesh->periodicX) { - communicate(dx); - } + if (mesh->periodicX) { + communicate(dx); + } + + dy = getUnalignedAtLocationAndFillGuards(mesh, "dy", 1.0, suffix, location, + extrapolate_x, extrapolate_y, false, + transform.get()); + + // grid data source has staggered fields, so read instead of interpolating + // Diagonal components of metric tensor g^{ij} (default to 1) + // TODO: Method `getAtLocAndFillGuards` violates command–query separation principle? + FieldMetric g11, g22, g33, g12, g13, g23; + + // Diagonal components of metric tensor g^{ij} (default to 1) + g11 = getUnalignedAtLocationAndFillGuards(mesh, "g11", 1.0, suffix, location, + extrapolate_x, extrapolate_y, false, + transform.get()); + g22 = getUnalignedAtLocationAndFillGuards(mesh, "g22", 1.0, suffix, location, + extrapolate_x, extrapolate_y, false, + transform.get()); + g33 = getUnalignedAtLocationAndFillGuards(mesh, "g33", 1.0, suffix, location, + extrapolate_x, extrapolate_y, false, + transform.get()); + // Off-diagonal elements. Default to 0 + g12 = getUnalignedAtLocationAndFillGuards(mesh, "g12", 0.0, suffix, location, + extrapolate_x, extrapolate_y, false, + transform.get()); + g13 = getUnalignedAtLocationAndFillGuards(mesh, "g13", 0.0, suffix, location, + extrapolate_x, extrapolate_y, false, + transform.get()); + g23 = getUnalignedAtLocationAndFillGuards(mesh, "g23", 0.0, suffix, location, + extrapolate_x, extrapolate_y, false, + transform.get()); + + geometry.setContravariantMetricTensor(MetricTensor(g11, g22, g33, g12, g13, g23), + location); - dy = getUnalignedAtLocationAndFillGuards(mesh, "dy", 1.0, suffix, location, - extrapolate_x, extrapolate_y, false, - transform.get()); - - // grid data source has staggered fields, so read instead of interpolating - // Diagonal components of metric tensor g^{ij} (default to 1) - // TODO: Method `getAtLocAndFillGuards` violates command–query separation principle? - FieldMetric g11, g22, g33, g12, g13, g23; - - // Diagonal components of metric tensor g^{ij} (default to 1) - g11 = getUnalignedAtLocationAndFillGuards(mesh, "g11", 1.0, suffix, location, - extrapolate_x, extrapolate_y, false, - transform.get()); - g22 = getUnalignedAtLocationAndFillGuards(mesh, "g22", 1.0, suffix, location, - extrapolate_x, extrapolate_y, false, - transform.get()); - g33 = getUnalignedAtLocationAndFillGuards(mesh, "g33", 1.0, suffix, location, - extrapolate_x, extrapolate_y, false, - transform.get()); - // Off-diagonal elements. Default to 0 - g12 = getUnalignedAtLocationAndFillGuards(mesh, "g12", 0.0, suffix, location, - extrapolate_x, extrapolate_y, false, - transform.get()); - g13 = getUnalignedAtLocationAndFillGuards(mesh, "g13", 0.0, suffix, location, - extrapolate_x, extrapolate_y, false, - transform.get()); - g23 = getUnalignedAtLocationAndFillGuards(mesh, "g23", 0.0, suffix, location, - extrapolate_x, extrapolate_y, false, - transform.get()); - - geometry.setContravariantMetricTensor(MetricTensor(g11, g22, g33, g12, g13, g23), - location); - - // Check input metrics - checkContravariant(); - - /// Find covariant metric components - auto covariant_component_names = {"g_11", "g_22", "g_33", "g_12", "g_13", "g_23"}; - auto source_has_component = [&suffix, &mesh](const std::string& name) { - return mesh->sourceHasVar(name + suffix); - }; - - // Check if any of the components are present - if (std::any_of(begin(covariant_component_names), end(covariant_component_names), + // Check input metrics + checkContravariant(); + + /// Find covariant metric components + auto covariant_component_names = {"g_11", "g_22", "g_33", "g_12", "g_13", "g_23"}; + auto source_has_component = [&suffix, &mesh](const std::string& name) { + return mesh->sourceHasVar(name + suffix); + }; + + // Check if any of the components are present + if (std::any_of(begin(covariant_component_names), end(covariant_component_names), + source_has_component)) { + // Check that all components are present + if (std::all_of(begin(covariant_component_names), end(covariant_component_names), source_has_component)) { - // Check that all components are present - if (std::all_of(begin(covariant_component_names), end(covariant_component_names), - source_has_component)) { - - FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; - g_11 = getAtLocOrUnaligned(mesh, "g_11", 1.0, suffix, location); - g_22 = getAtLocOrUnaligned(mesh, "g_22", 1.0, suffix, location); - g_33 = getAtLocOrUnaligned(mesh, "g_33", 1.0, suffix, location); - g_12 = getAtLocOrUnaligned(mesh, "g_12", 0.0, suffix, location); - g_13 = getAtLocOrUnaligned(mesh, "g_13", 0.0, suffix, location); - g_23 = getAtLocOrUnaligned(mesh, "g_23", 0.0, suffix, location); - geometry.setCovariantMetricTensor( - MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23), location); - - output_warn.write( - "\tWARNING! Covariant components of metric tensor set manually. " - "Contravariant components NOT recalculated\n"); - - } else { - output_warn.write("Not all covariant components of metric tensor found. " - "Calculating all from the contravariant tensor\n"); - /// Calculate contravariant metric components if not found - try { - calcCovariant("RGN_NOCORNERS"); - } catch (BoutException&) { - throw BoutException("Error in calcCovariant call"); - } - } + + FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; + g_11 = getAtLocOrUnaligned(mesh, "g_11", 1.0, suffix, location); + g_22 = getAtLocOrUnaligned(mesh, "g_22", 1.0, suffix, location); + g_33 = getAtLocOrUnaligned(mesh, "g_33", 1.0, suffix, location); + g_12 = getAtLocOrUnaligned(mesh, "g_12", 0.0, suffix, location); + g_13 = getAtLocOrUnaligned(mesh, "g_13", 0.0, suffix, location); + g_23 = getAtLocOrUnaligned(mesh, "g_23", 0.0, suffix, location); + geometry.setCovariantMetricTensor(MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23), + location); + + output_warn.write("\tWARNING! Covariant components of metric tensor set manually. " + "Contravariant components NOT recalculated\n"); + } else { + output_warn.write("Not all covariant components of metric tensor found. " + "Calculating all from the contravariant tensor\n"); /// Calculate contravariant metric components if not found try { calcCovariant("RGN_NOCORNERS"); @@ -504,90 +502,96 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, throw BoutException("Error in calcCovariant call"); } } + } else { + /// Calculate contravariant metric components if not found + try { + calcCovariant("RGN_NOCORNERS"); + } catch (BoutException&) { + throw BoutException("Error in calcCovariant call"); + } + } - // More robust to extrapolate derived quantities directly, rather than - // deriving from extrapolated covariant metric components + // More robust to extrapolate derived quantities directly, rather than + // deriving from extrapolated covariant metric components - std::function const - interpolateAndExtrapolate_function = [this, extrapolate_y, extrapolate_x]( - const FieldMetric& component) { - return localmesh->interpolateAndExtrapolate( - component, location, extrapolate_x, extrapolate_y, false, transform.get()); - }; - geometry.applyToCovariantMetricTensor(interpolateAndExtrapolate_function); - - // Check covariant metrics - checkCovariant(); - - /// Calculate Jacobian and Bxy - jacobian(); - - // Attempt to read J from the grid file - if (!localmesh->sourceHasVar("J" + suffix)) { - output_warn.write( - "\tWARNING: Jacobian 'J_{:s}' not found. Calculating from metric tensor\n", - suffix); - } else { - const auto Jcalc = getAtLoc(mesh, "J", suffix, location); - setJ(localmesh->interpolateAndExtrapolate(Jcalc, location, extrapolate_x, - extrapolate_y, false, transform.get())); + std::function const + interpolateAndExtrapolate_function = [this, extrapolate_y, + extrapolate_x](const FieldMetric& component) { + return localmesh->interpolateAndExtrapolate( + component, location, extrapolate_x, extrapolate_y, false, transform.get()); + }; + geometry.applyToCovariantMetricTensor(interpolateAndExtrapolate_function); - // Compare calculated and loaded values - output_warn.write("\tMaximum difference in J is {:e}\n", max(abs(J() - Jcalc))); + // Check covariant metrics + checkCovariant(); - auto J_value = J(); // TODO: There may be a better way - communicate(J_value); + /// Calculate Jacobian and Bxy + jacobian(); - // Re-evaluate Bxy using new J - setBxy(sqrt(g_22()) / J()); - } + // Attempt to read J from the grid file + if (!localmesh->sourceHasVar("J" + suffix)) { + output_warn.write( + "\tWARNING: Jacobian 'J_{:s}' not found. Calculating from metric tensor\n", + suffix); + } else { + const auto Jcalc = getAtLoc(mesh, "J", suffix, location); + setJ(localmesh->interpolateAndExtrapolate(Jcalc, location, extrapolate_x, + extrapolate_y, false, transform.get())); - // Check jacobian - bout::checkFinite(J(), "J" + suffix, "RGN_NOCORNERS"); - bout::checkPositive(J(), "J" + suffix, "RGN_NOCORNERS"); - if (min(abs(J())) < 1.0e-10) { - throw BoutException("\tERROR: Jacobian{:s} becomes very small\n", suffix); - } + // Compare calculated and loaded values + output_warn.write("\tMaximum difference in J is {:e}\n", max(abs(J() - Jcalc))); - // Attempt to read Bxy from the grid file - if (!localmesh->sourceHasVar("Bxy" + suffix)) { - output_warn.write("\tWARNING: Magnitude of B field 'Bxy_{:s}' not found. " - "Calculating from metric tensor\n", - suffix); - } else { - const auto Bcalc = getAtLoc(mesh, "Bxy", suffix, location); - setBxy(localmesh->interpolateAndExtrapolate(Bcalc, location, extrapolate_x, - extrapolate_y, false, transform.get())); - output_warn.write("\tMaximum difference in Bxy is {:e}\n", max(abs(Bxy() - Bcalc))); - } - // Check Bxy - bout::checkFinite(Bxy(), "Bxy" + suffix, "RGN_NOCORNERS"); - bout::checkPositive(Bxy(), "Bxy" + suffix, "RGN_NOCORNERS"); - - if (getAtLoc(mesh, ShiftTorsion, "ShiftTorsion", suffix, location) != 0) { - output_warn.write("\tWARNING: No Torsion specified for zShift. " - "Derivatives may not be correct\n"); - ShiftTorsion = 0.0; - } - ShiftTorsion = localmesh->interpolateAndExtrapolate( - ShiftTorsion, location, extrapolate_x, extrapolate_y, false, transform.get()); + auto J_value = J(); // TODO: There may be a better way + communicate(J_value); - ////////////////////////////////////////////////////// + // Re-evaluate Bxy using new J + setBxy(sqrt(g_22()) / J()); + } - if (mesh->IncIntShear) { - checkStaggeredGet(mesh, "IntShiftTorsion", suffix); - if (mesh->get(IntShiftTorsion, "IntShiftTorsion" + suffix, 0.0, false) != 0) { - output_warn.write("\tWARNING: No Integrated torsion specified\n"); - IntShiftTorsion = 0.0; - } - IntShiftTorsion.setLocation(location); - IntShiftTorsion = - localmesh->interpolateAndExtrapolate(IntShiftTorsion, location, extrapolate_x, - extrapolate_y, false, transform.get()); - } else { - // IntShiftTorsion will not be used, but set to zero to avoid uninitialized field - IntShiftTorsion = 0.; + // Check jacobian + bout::checkFinite(J(), "J" + suffix, "RGN_NOCORNERS"); + bout::checkPositive(J(), "J" + suffix, "RGN_NOCORNERS"); + if (min(abs(J())) < 1.0e-10) { + throw BoutException("\tERROR: Jacobian{:s} becomes very small\n", suffix); + } + + // Attempt to read Bxy from the grid file + if (!localmesh->sourceHasVar("Bxy" + suffix)) { + output_warn.write("\tWARNING: Magnitude of B field 'Bxy_{:s}' not found. " + "Calculating from metric tensor\n", + suffix); + } else { + const auto Bcalc = getAtLoc(mesh, "Bxy", suffix, location); + setBxy(localmesh->interpolateAndExtrapolate(Bcalc, location, extrapolate_x, + extrapolate_y, false, transform.get())); + output_warn.write("\tMaximum difference in Bxy is {:e}\n", max(abs(Bxy() - Bcalc))); + } + // Check Bxy + bout::checkFinite(Bxy(), "Bxy" + suffix, "RGN_NOCORNERS"); + bout::checkPositive(Bxy(), "Bxy" + suffix, "RGN_NOCORNERS"); + + if (getAtLoc(mesh, ShiftTorsion, "ShiftTorsion", suffix, location) != 0) { + output_warn.write("\tWARNING: No Torsion specified for zShift. " + "Derivatives may not be correct\n"); + ShiftTorsion = 0.0; + } + ShiftTorsion = localmesh->interpolateAndExtrapolate( + ShiftTorsion, location, extrapolate_x, extrapolate_y, false, transform.get()); + + ////////////////////////////////////////////////////// + + if (mesh->IncIntShear) { + checkStaggeredGet(mesh, "IntShiftTorsion", suffix); + if (mesh->get(IntShiftTorsion, "IntShiftTorsion" + suffix, 0.0, false) != 0) { + output_warn.write("\tWARNING: No Integrated torsion specified\n"); + IntShiftTorsion = 0.0; } + IntShiftTorsion.setLocation(location); + IntShiftTorsion = localmesh->interpolateAndExtrapolate( + IntShiftTorsion, location, extrapolate_x, extrapolate_y, false, transform.get()); + } else { + // IntShiftTorsion will not be used, but set to zero to avoid uninitialized field + IntShiftTorsion = 0.; } } From a2c3a6e29e65ee472c1fe420818f62b260aaddc3 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Mon, 4 Dec 2023 15:18:41 +0000 Subject: [PATCH 246/491] Invert 'if' condition. --- src/mesh/coordinates.cxx | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 20c4f547e5..f013567c0a 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -311,11 +311,11 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, std::string suffix = getLocationSuffix(location); - if ((coords_in != nullptr) && !suffix.empty() - && (force_interpolate_from_centre || !mesh->sourceHasVar("dx" + suffix))) { - interpolateFieldsFromOtherCoordinates(mesh, options, coords_in); - } else { + if (coords_in == nullptr || suffix.empty() + || !(force_interpolate_from_centre || !mesh->sourceHasVar("dx" + suffix))) { setBoundaryCells(mesh, options, coords_in, suffix); + } else { + interpolateFieldsFromOtherCoordinates(mesh, options, coords_in); } } From e5963abd29c4e34c93da54626b3cb41ce9715afd Mon Sep 17 00:00:00 2001 From: tomc271 Date: Mon, 4 Dec 2023 16:53:48 +0000 Subject: [PATCH 247/491] Extract method recalculateAndReset() from calculateGeometry(), and call from setContravariantMetricTensor() and setCovariantMetricTensor() and from createDefaultCoordinates(). --- include/bout/coordinates.hxx | 21 ++++++++++++++++----- include/bout/mesh.hxx | 6 +++--- src/mesh/coordinates.cxx | 22 ++++++++++++++++------ src/mesh/mesh.cxx | 22 ++++++++++++++-------- 4 files changed, 49 insertions(+), 22 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index e5d815d903..31f51b4cc2 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -105,10 +105,14 @@ public: const FieldMetric& Bxy() const; void setContravariantMetricTensor(MetricTensor metric_tensor, - const std::string& region = "RGN_ALL"); + const std::string& region = "RGN_ALL", + bool recalculate_staggered = true, + bool force_interpolate_from_centre = false); void setCovariantMetricTensor(MetricTensor metric_tensor, - const std::string& region = "RGN_ALL"); + const std::string& region = "RGN_ALL", + bool recalculate_staggered = true, + bool force_interpolate_from_centre = false); void setJ(FieldMetric J); void setJ(BoutReal value, int x, int y); @@ -123,12 +127,14 @@ public: const MetricTensor& getContravariantMetricTensor() const; /// Calculate differential geometry quantities from the metric tensor - int calculateGeometry(bool recalculate_staggered = true, - bool force_interpolate_from_centre = false); + int calculateGeometry(); + /// Invert contravariant metric to get covariant components void calcCovariant(const std::string& region = "RGN_ALL"); + /// Invert covariant metric to get contravariant components void calcContravariant(const std::string& region = "RGN_ALL"); + void jacobian(); ///< Calculate J and Bxy /////////////////////////////////////////////////////////// @@ -262,6 +268,9 @@ public: const FieldMetric& invSg() const; + void recalculateAndReset(bool recalculate_staggered, + bool force_interpolate_from_centre); + private: int nz; // Size of mesh in Z. This is mesh->ngz-1 Mesh* localmesh; @@ -310,7 +319,9 @@ private: /// Non-uniform meshes. Need to use DDX, DDY void correctionForNonUniformMeshes(bool force_interpolate_from_centre); - void interpolateFieldsFromOtherCoordinates(const Mesh* mesh, Options* options, const Coordinates* coords_in); + void interpolateFieldsFromOtherCoordinates(const Mesh* mesh, Options* options, + const Coordinates* coords_in); + void setBoundaryCells(Mesh* mesh, Options* options, const Coordinates* coords_in, const std::string& suffix); }; diff --git a/include/bout/mesh.hxx b/include/bout/mesh.hxx index fc3fdd60ee..fded79f777 100644 --- a/include/bout/mesh.hxx +++ b/include/bout/mesh.hxx @@ -633,8 +633,8 @@ public: // Note that this can't be allocated here due to incomplete type // (circular dependency between Mesh and Coordinates) auto inserted = coords_map.emplace(location, nullptr); - inserted.first->second = createDefaultCoordinates(location); - inserted.first->second->calculateGeometry(false); + inserted.first->second = createDefaultCoordinates(location, false); + inserted.first->second->calculateGeometry(); return inserted.first->second; } @@ -854,7 +854,7 @@ private: /// (useful if CELL_CENTRE Coordinates have been changed, so reading from file /// would not be correct). std::shared_ptr - createDefaultCoordinates(const CELL_LOC location, + createDefaultCoordinates(CELL_LOC location, bool recalculate_staggered = true, bool force_interpolate_from_centre = false); //Internal region related information diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index f013567c0a..008bac6e04 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -643,8 +643,7 @@ const Field2D& Coordinates::zlength() const { return *zlength_cache; } -int Coordinates::calculateGeometry(bool recalculate_staggered, - bool force_interpolate_from_centre) { +int Coordinates::calculateGeometry() { TRACE("Coordinates::calculateGeometry"); communicate(dx, dy, dz, g11(), g22(), g33(), g12(), g13(), g23(), g_11(), g_22(), @@ -664,6 +663,12 @@ int Coordinates::calculateGeometry(bool recalculate_staggered, throw BoutException("dz magnitude less than 1e-8"); } + return 0; +} + +void Coordinates::recalculateAndReset(bool recalculate_staggered, + bool force_interpolate_from_centre) { + // Check input metrics checkContravariant(); checkCovariant(); @@ -678,8 +683,6 @@ int Coordinates::calculateGeometry(bool recalculate_staggered, } invalidateAndRecalculateCachedVariables(); - - return 0; } void Coordinates::correctionForNonUniformMeshes(bool force_interpolate_from_centre) { @@ -758,6 +761,7 @@ void Coordinates::correctionForNonUniformMeshes(bool force_interpolate_from_cent } void Coordinates::calculateCommunicateAndExtrapolateChristoffelSymbols() { + geometry.CalculateChristoffelSymbols(dx, dy); auto tmp = J() * g12(); @@ -1377,13 +1381,19 @@ void Coordinates::checkCovariant() { return geometry.checkCovariant(localmesh->y void Coordinates::checkContravariant() { geometry.checkContravariant(localmesh->ystart); } void Coordinates::setContravariantMetricTensor(MetricTensor metric_tensor, - const std::string& region) { + const std::string& region, + bool recalculate_staggered, + bool force_interpolate_from_centre) { geometry.setContravariantMetricTensor(std::move(metric_tensor), location, region); + recalculateAndReset(recalculate_staggered, force_interpolate_from_centre); } void Coordinates::setCovariantMetricTensor(MetricTensor metric_tensor, - const std::string& region) { + const std::string& region, + bool recalculate_staggered, + bool force_interpolate_from_centre) { geometry.setCovariantMetricTensor(std::move(metric_tensor), location, region); + recalculateAndReset(recalculate_staggered, force_interpolate_from_centre); } const FieldMetric& Coordinates::g_11() const { return geometry.g_11(); } diff --git a/src/mesh/mesh.cxx b/src/mesh/mesh.cxx index d0c8e1250a..53792c974d 100644 --- a/src/mesh/mesh.cxx +++ b/src/mesh/mesh.cxx @@ -688,18 +688,24 @@ const std::vector Mesh::readInts(const std::string& name, int n) { } std::shared_ptr -Mesh::createDefaultCoordinates(const CELL_LOC location, +Mesh::createDefaultCoordinates(const CELL_LOC location, bool recalculate_staggered, bool force_interpolate_from_centre) { if (location == CELL_CENTRE || location == CELL_DEFAULT) { // Initialize coordinates from input - return std::make_shared(this, options); - } else { - // Interpolate coordinates from CELL_CENTRE version - return std::make_shared(this, options, location, - getCoordinates(CELL_CENTRE), + const std::shared_ptr& new_coordinates = + std::make_shared(this, options); + new_coordinates->recalculateAndReset(recalculate_staggered, force_interpolate_from_centre); + return new_coordinates; } + // Interpolate coordinates from CELL_CENTRE version + const std::shared_ptr& new_coordinates = + std::make_shared(this, options, location, getCoordinates(CELL_CENTRE), + force_interpolate_from_centre); + new_coordinates->recalculateAndReset(recalculate_staggered, + force_interpolate_from_centre); + return new_coordinates; } const Region<>& Mesh::getRegion3D(const std::string& region_name) const { @@ -899,8 +905,8 @@ void Mesh::recalculateStaggeredCoordinates() { continue; } - *coords_map[location] = std::move(*createDefaultCoordinates(location, true)); - coords_map[location]->calculateGeometry(false, true); + *coords_map[location] = std::move(*createDefaultCoordinates(location, false, true)); + coords_map[location]->calculateGeometry(); } } From bcc9696294e4e0d5572b622a31aac7687c29e294 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Mon, 4 Dec 2023 18:53:56 +0000 Subject: [PATCH 248/491] Updated CoordinatesTest.SetContravariantMetricTensor and CoordinatesTest.SetCovariantMetricTensor to use valid values for the metric tensor components. --- tests/unit/mesh/test_coordinates.cxx | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/tests/unit/mesh/test_coordinates.cxx b/tests/unit/mesh/test_coordinates.cxx index 23b29dc328..abccacedbb 100644 --- a/tests/unit/mesh/test_coordinates.cxx +++ b/tests/unit/mesh/test_coordinates.cxx @@ -358,16 +358,16 @@ TEST_F(CoordinatesTest, SetContravariantMetricTensor) { FieldMetric{0.0}}; // IntShiftTorsion // Modify with setter - auto updated_metric_tensor = MetricTensor(1.7, 2.3, 3.1, 0.9, 5.7, 1.9); + auto updated_metric_tensor = MetricTensor(1.0, 2.0, 0.4, 1.0, 0.0, 0.2); coords.setContravariantMetricTensor(updated_metric_tensor); // Get values with getter and check they have been modified as expected - EXPECT_TRUE(IsFieldEqual(coords.g11(), 1.7)); - EXPECT_TRUE(IsFieldEqual(coords.g22(), 2.3)); - EXPECT_TRUE(IsFieldEqual(coords.g33(), 3.1)); - EXPECT_TRUE(IsFieldEqual(coords.g12(), 0.9)); - EXPECT_TRUE(IsFieldEqual(coords.g13(), 5.7)); - EXPECT_TRUE(IsFieldEqual(coords.g23(), 1.9)); + EXPECT_TRUE(IsFieldEqual(coords.g11(), 1.0)); + EXPECT_TRUE(IsFieldEqual(coords.g22(), 2.0)); + EXPECT_TRUE(IsFieldEqual(coords.g33(), 0.4)); + EXPECT_TRUE(IsFieldEqual(coords.g12(), 1.0)); + EXPECT_TRUE(IsFieldEqual(coords.g13(), 0.0)); + EXPECT_TRUE(IsFieldEqual(coords.g23(), 0.2)); } TEST_F(CoordinatesTest, GetCovariantMetricTensor) { @@ -425,15 +425,15 @@ TEST_F(CoordinatesTest, SetCovariantMetricTensor) { FieldMetric{0.0}}; // IntShiftTorsion // Modify with setter - auto updated_metric_tensor = MetricTensor(1.7, 2.3, 3.1, 0.9, 5.7, 1.9); + auto updated_metric_tensor = MetricTensor(1.0, 2.0, 0.4, 1.0, 0.0, 0.2); coords.setCovariantMetricTensor(updated_metric_tensor); // Get values with getter and check they have been modified as expected - EXPECT_TRUE(IsFieldEqual(coords.g_11(), 1.7)); - EXPECT_TRUE(IsFieldEqual(coords.g_22(), 2.3)); - EXPECT_TRUE(IsFieldEqual(coords.g_33(), 3.1)); - EXPECT_TRUE(IsFieldEqual(coords.g_12(), 0.9)); - EXPECT_TRUE(IsFieldEqual(coords.g_13(), 5.7)); - EXPECT_TRUE(IsFieldEqual(coords.g_23(), 1.9)); + EXPECT_TRUE(IsFieldEqual(coords.g_11(), 1.0)); + EXPECT_TRUE(IsFieldEqual(coords.g_22(), 2.0)); + EXPECT_TRUE(IsFieldEqual(coords.g_33(), 0.4)); + EXPECT_TRUE(IsFieldEqual(coords.g_12(), 1.0)); + EXPECT_TRUE(IsFieldEqual(coords.g_13(), 0.0)); + EXPECT_TRUE(IsFieldEqual(coords.g_23(), 0.2)); } } \ No newline at end of file From a22b5b4dacf9221b4f0bc398f9479ec6c91f692e Mon Sep 17 00:00:00 2001 From: tomc271 Date: Tue, 5 Dec 2023 10:12:20 +0000 Subject: [PATCH 249/491] Invert boolean condition. --- src/mesh/coordinates.cxx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 008bac6e04..dd36218989 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -312,7 +312,7 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, std::string suffix = getLocationSuffix(location); if (coords_in == nullptr || suffix.empty() - || !(force_interpolate_from_centre || !mesh->sourceHasVar("dx" + suffix))) { + || (!force_interpolate_from_centre && mesh->sourceHasVar("dx" + suffix))) { setBoundaryCells(mesh, options, coords_in, suffix); } else { interpolateFieldsFromOtherCoordinates(mesh, options, coords_in); From 96a0a4b622cfe9d4b5114080eab55fbbfa028a7b Mon Sep 17 00:00:00 2001 From: tomc271 Date: Tue, 5 Dec 2023 10:39:20 +0000 Subject: [PATCH 250/491] Use own location in MetricTensor::oppositeRepresentation, rather than passing in a location. --- include/bout/geometry.hxx | 9 +++++---- include/bout/metricTensor.hxx | 3 +-- src/mesh/coordinates.cxx | 14 ++++++-------- src/mesh/geometry.cxx | 14 ++++++-------- src/mesh/metricTensor.cxx | 4 ++-- 5 files changed, 20 insertions(+), 24 deletions(-) diff --git a/include/bout/geometry.hxx b/include/bout/geometry.hxx index a58dfb7eda..55e4a3b2f4 100644 --- a/include/bout/geometry.hxx +++ b/include/bout/geometry.hxx @@ -105,10 +105,10 @@ public: ///< Magnitude of B = nabla z times nabla x const FieldMetric& Bxy() const; - void setContravariantMetricTensor(MetricTensor metric_tensor, CELL_LOC cell_location, + void setContravariantMetricTensor(MetricTensor metric_tensor, const std::string& region = "RGN_ALL"); - void setCovariantMetricTensor(MetricTensor metric_tensor, CELL_LOC cell_location, + void setCovariantMetricTensor(MetricTensor metric_tensor, const std::string& region = "RGN_ALL"); void setG1_11(FieldMetric G1_11); @@ -141,10 +141,11 @@ public: void setBxy(FieldMetric Bxy); - void calcCovariant(CELL_LOC cell_location, const std::string& region = "RGN_ALL"); + /// Invert contravariant metric to get covariant components + void calcCovariant(const std::string& region = "RGN_ALL"); /// Invert covariant metric to get contravariant components - void calcContravariant(CELL_LOC cell_location, const std::string& region = "RGN_ALL"); + void calcContravariant(const std::string& region = "RGN_ALL"); /// Calculate Christoffel symbol terms void CalculateChristoffelSymbols(FieldMetric& dx, FieldMetric& dy); diff --git a/include/bout/metricTensor.hxx b/include/bout/metricTensor.hxx index d45a6c5c4a..05faba517c 100644 --- a/include/bout/metricTensor.hxx +++ b/include/bout/metricTensor.hxx @@ -37,8 +37,7 @@ public: void setLocation(CELL_LOC location); - MetricTensor oppositeRepresentation(CELL_LOC location, - const std::string& region = "RGN_ALL"); + MetricTensor oppositeRepresentation(const std::string& region = "RGN_ALL"); // Transforms the MetricTensor by applying the given function to every component void map(std::function function); diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index dd36218989..f29b46d4a6 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -460,8 +460,7 @@ void Coordinates::setBoundaryCells(Mesh* mesh, Options* options, extrapolate_x, extrapolate_y, false, transform.get()); - geometry.setContravariantMetricTensor(MetricTensor(g11, g22, g33, g12, g13, g23), - location); + geometry.setContravariantMetricTensor(MetricTensor(g11, g22, g33, g12, g13, g23)); // Check input metrics checkContravariant(); @@ -486,8 +485,7 @@ void Coordinates::setBoundaryCells(Mesh* mesh, Options* options, g_12 = getAtLocOrUnaligned(mesh, "g_12", 0.0, suffix, location); g_13 = getAtLocOrUnaligned(mesh, "g_13", 0.0, suffix, location); g_23 = getAtLocOrUnaligned(mesh, "g_23", 0.0, suffix, location); - geometry.setCovariantMetricTensor(MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23), - location); + geometry.setCovariantMetricTensor(MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); output_warn.write("\tWARNING! Covariant components of metric tensor set manually. " "Contravariant components NOT recalculated\n"); @@ -798,12 +796,12 @@ void Coordinates::calculateCommunicateAndExtrapolateChristoffelSymbols() { void Coordinates::calcCovariant(const std::string& region) { TRACE("Coordinates::calcCovariant"); - geometry.calcCovariant(location, region); + geometry.calcCovariant(region); } void Coordinates::calcContravariant(const std::string& region) { TRACE("Coordinates::calcContravariant"); - geometry.calcContravariant(location, region); + geometry.calcContravariant(region); } void Coordinates::jacobian() { @@ -1384,7 +1382,7 @@ void Coordinates::setContravariantMetricTensor(MetricTensor metric_tensor, const std::string& region, bool recalculate_staggered, bool force_interpolate_from_centre) { - geometry.setContravariantMetricTensor(std::move(metric_tensor), location, region); + geometry.setContravariantMetricTensor(std::move(metric_tensor), region); recalculateAndReset(recalculate_staggered, force_interpolate_from_centre); } @@ -1392,7 +1390,7 @@ void Coordinates::setCovariantMetricTensor(MetricTensor metric_tensor, const std::string& region, bool recalculate_staggered, bool force_interpolate_from_centre) { - geometry.setCovariantMetricTensor(std::move(metric_tensor), location, region); + geometry.setCovariantMetricTensor(std::move(metric_tensor), region); recalculateAndReset(recalculate_staggered, force_interpolate_from_centre); } diff --git a/src/mesh/geometry.cxx b/src/mesh/geometry.cxx index 7d18cc82f3..a708ab376e 100644 --- a/src/mesh/geometry.cxx +++ b/src/mesh/geometry.cxx @@ -213,16 +213,16 @@ void Geometry::CalculateChristoffelSymbols(FieldMetric& dx, FieldMetric& dy) { * differential_operators->DDY(covariantMetricTensor.Getg33(), dy); } -void Geometry::calcCovariant(CELL_LOC cell_location, const std::string& region) { +void Geometry::calcCovariant(const std::string& region) { TRACE("Geometry::calcCovariant"); covariantMetricTensor.setMetricTensor( - contravariantMetricTensor.oppositeRepresentation(cell_location, region)); + contravariantMetricTensor.oppositeRepresentation(region)); } -void Geometry::calcContravariant(CELL_LOC cell_location, const std::string& region) { +void Geometry::calcContravariant(const std::string& region) { TRACE("Geometry::calcContravariant"); contravariantMetricTensor.setMetricTensor( - covariantMetricTensor.oppositeRepresentation(cell_location, region)); + covariantMetricTensor.oppositeRepresentation(region)); } MetricTensor::FieldMetric Geometry::recalculateJacobian() { @@ -255,19 +255,17 @@ void Geometry::checkCovariant(int ystart) { covariantMetricTensor.check(ystart); void Geometry::checkContravariant(int ystart) { contravariantMetricTensor.check(ystart); } void Geometry::setContravariantMetricTensor(MetricTensor metric_tensor, - CELL_LOC cell_location, const std::string& region) { contravariantMetricTensor.setMetricTensor(metric_tensor); covariantMetricTensor.setMetricTensor( - contravariantMetricTensor.oppositeRepresentation(cell_location, region)); + contravariantMetricTensor.oppositeRepresentation(region)); } void Geometry::setCovariantMetricTensor(MetricTensor metric_tensor, - CELL_LOC cell_location, const std::string& region) { covariantMetricTensor.setMetricTensor(metric_tensor); contravariantMetricTensor.setMetricTensor( - covariantMetricTensor.oppositeRepresentation(cell_location, region)); + covariantMetricTensor.oppositeRepresentation(region)); } const MetricTensor::FieldMetric& Geometry::g_11() const { diff --git a/src/mesh/metricTensor.cxx b/src/mesh/metricTensor.cxx index 543eba0ab6..6e36fd2b5c 100644 --- a/src/mesh/metricTensor.cxx +++ b/src/mesh/metricTensor.cxx @@ -105,8 +105,7 @@ void MetricTensor::setLocation(const CELL_LOC location) { g23.setLocation(location); } -MetricTensor MetricTensor::oppositeRepresentation(CELL_LOC location, - const std::string& region) { +MetricTensor MetricTensor::oppositeRepresentation(const std::string& region) { TRACE("MetricTensor::CalculateOppositeRepresentation"); @@ -164,6 +163,7 @@ MetricTensor MetricTensor::oppositeRepresentation(CELL_LOC location, // output_info.write("\tMaximum error in off-diagonal inversion is {:e}\n", maxerr); const auto mesh = g11.getMesh(); // All the components have the same mesh auto other_representation = MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23, mesh); + const auto location = g11.getLocation(); other_representation.setLocation(location); return other_representation; } From e7deb1e4b57c75e91fba65a9ec910a1dddf903a4 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Tue, 5 Dec 2023 10:56:54 +0000 Subject: [PATCH 251/491] Set location of dx, dy dz after using them with 'default coordinates' (with location = CELL_CENTRE) in calculation of metric quantities. --- src/mesh/coordinates.cxx | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index f29b46d4a6..42698c78bc 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -323,24 +323,6 @@ void Coordinates::interpolateFieldsFromOtherCoordinates(const Mesh* mesh, Options* options, const Coordinates* coords_in) { - if (isUniform(coords_in->dz)) { - dz = coords_in->dz; - dz.setLocation(location); - } else { - throw BoutException("We are asked to transform dz to get dz before we " - "have a transform, which " - "might require dz!\nPlease provide a dz for the " - "staggered quantity!"); - } - setParallelTransform(options); - dx = localmesh->interpolateAndExtrapolate(coords_in->dx, location, true, true, false, - transform.get()); - dy = localmesh->interpolateAndExtrapolate(coords_in->dy, location, true, true, false, - transform.get()); - // not really needed - we have used dz already ... - dz = localmesh->interpolateAndExtrapolate(coords_in->dz, location, true, true, false, - transform.get()); - std::function const interpolateAndExtrapolate_function = [this](const FieldMetric& component) { return localmesh->interpolateAndExtrapolate(component, location, true, true, @@ -374,6 +356,24 @@ void Coordinates::interpolateFieldsFromOtherCoordinates(const Mesh* mesh, IntShiftTorsion = localmesh->interpolateAndExtrapolate( coords_in->IntShiftTorsion, location, true, true, false, transform.get()); } + + if (isUniform(coords_in->dz)) { + dz = coords_in->dz; + dz.setLocation(location); + } else { + throw BoutException("We are asked to transform dz to get dz before we " + "have a transform, which " + "might require dz!\nPlease provide a dz for the " + "staggered quantity!"); + } + setParallelTransform(options); + dx = localmesh->interpolateAndExtrapolate(coords_in->dx, location, true, true, false, + transform.get()); + dy = localmesh->interpolateAndExtrapolate(coords_in->dy, location, true, true, false, + transform.get()); + // not really needed - we have used dz already ... + dz = localmesh->interpolateAndExtrapolate(coords_in->dz, location, true, true, false, + transform.get()); } // Note: If boundary cells were not loaded from the grid file, use From 1309a4ac5ceca41ff0251858235d7855155549b8 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Tue, 5 Dec 2023 11:00:02 +0000 Subject: [PATCH 252/491] Rename 'oppositeRepresentation' method of MetricTensor to 'inverse'. --- include/bout/metricTensor.hxx | 2 +- src/mesh/geometry.cxx | 12 ++++-------- src/mesh/metricTensor.cxx | 2 +- 3 files changed, 6 insertions(+), 10 deletions(-) diff --git a/include/bout/metricTensor.hxx b/include/bout/metricTensor.hxx index 05faba517c..fcf126585c 100644 --- a/include/bout/metricTensor.hxx +++ b/include/bout/metricTensor.hxx @@ -37,7 +37,7 @@ public: void setLocation(CELL_LOC location); - MetricTensor oppositeRepresentation(const std::string& region = "RGN_ALL"); + MetricTensor inverse(const std::string& region = "RGN_ALL"); // Transforms the MetricTensor by applying the given function to every component void map(std::function function); diff --git a/src/mesh/geometry.cxx b/src/mesh/geometry.cxx index a708ab376e..3907e5b70b 100644 --- a/src/mesh/geometry.cxx +++ b/src/mesh/geometry.cxx @@ -215,14 +215,12 @@ void Geometry::CalculateChristoffelSymbols(FieldMetric& dx, FieldMetric& dy) { void Geometry::calcCovariant(const std::string& region) { TRACE("Geometry::calcCovariant"); - covariantMetricTensor.setMetricTensor( - contravariantMetricTensor.oppositeRepresentation(region)); + covariantMetricTensor.setMetricTensor(contravariantMetricTensor.inverse(region)); } void Geometry::calcContravariant(const std::string& region) { TRACE("Geometry::calcContravariant"); - contravariantMetricTensor.setMetricTensor( - covariantMetricTensor.oppositeRepresentation(region)); + contravariantMetricTensor.setMetricTensor(covariantMetricTensor.inverse(region)); } MetricTensor::FieldMetric Geometry::recalculateJacobian() { @@ -257,15 +255,13 @@ void Geometry::checkContravariant(int ystart) { contravariantMetricTensor.check( void Geometry::setContravariantMetricTensor(MetricTensor metric_tensor, const std::string& region) { contravariantMetricTensor.setMetricTensor(metric_tensor); - covariantMetricTensor.setMetricTensor( - contravariantMetricTensor.oppositeRepresentation(region)); + covariantMetricTensor.setMetricTensor(contravariantMetricTensor.inverse(region)); } void Geometry::setCovariantMetricTensor(MetricTensor metric_tensor, const std::string& region) { covariantMetricTensor.setMetricTensor(metric_tensor); - contravariantMetricTensor.setMetricTensor( - covariantMetricTensor.oppositeRepresentation(region)); + contravariantMetricTensor.setMetricTensor(covariantMetricTensor.inverse(region)); } const MetricTensor::FieldMetric& Geometry::g_11() const { diff --git a/src/mesh/metricTensor.cxx b/src/mesh/metricTensor.cxx index 6e36fd2b5c..1edb757812 100644 --- a/src/mesh/metricTensor.cxx +++ b/src/mesh/metricTensor.cxx @@ -105,7 +105,7 @@ void MetricTensor::setLocation(const CELL_LOC location) { g23.setLocation(location); } -MetricTensor MetricTensor::oppositeRepresentation(const std::string& region) { +MetricTensor MetricTensor::inverse(const std::string& region) { TRACE("MetricTensor::CalculateOppositeRepresentation"); From e6f3c2d9327e33aceb385292e0148f403255c047 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Tue, 5 Dec 2023 11:07:53 +0000 Subject: [PATCH 253/491] Remove call to calculateGeometry() in tests and examples since no longer need to call it now that metric tensor quantities are updated by Coordinates::recalculateAndReset when calling setContravariantMetricTensor() and setCovariantMetricTensor(). --- examples/2Dturbulence_multigrid/esel.cxx | 2 -- examples/6field-simple/elm_6f.cxx | 2 -- examples/conducting-wall-mode/cwm.cxx | 2 -- examples/constraints/alfven-wave/alfven.cxx | 2 -- examples/dalf3/dalf3.cxx | 2 -- examples/elm-pb-outerloop/elm_pb_outerloop.cxx | 12 +++++------- examples/elm-pb/elm_pb.cxx | 2 -- examples/gyro-gem/gem.cxx | 2 -- examples/jorek-compare/jorek_compare.cxx | 2 -- examples/lapd-drift/lapd_drift.cxx | 2 -- examples/laplacexy/alfven-wave/alfven.cxx | 4 +--- examples/laplacexy/laplace_perp/test.cxx | 2 -- examples/reconnect-2field/2field.cxx | 2 -- examples/shear-alfven-wave/2fluid.cxx | 2 -- examples/uedge-benchmark/ue_bmark.cxx | 2 -- examples/wave-slab/wave_slab.cxx | 2 -- tests/MMS/diffusion/diffusion.cxx | 5 +---- tests/MMS/wave-1d/wave.cxx | 2 -- tests/integrated/test-drift-instability/2fluid.cxx | 2 -- .../test-interchange-instability/2fluid.cxx | 2 -- tests/unit/invert/laplace/test_laplace_cyclic.cxx | 1 - 21 files changed, 7 insertions(+), 49 deletions(-) diff --git a/examples/2Dturbulence_multigrid/esel.cxx b/examples/2Dturbulence_multigrid/esel.cxx index 79fcfc805c..821a0fce3d 100644 --- a/examples/2Dturbulence_multigrid/esel.cxx +++ b/examples/2Dturbulence_multigrid/esel.cxx @@ -89,8 +89,6 @@ class ESEL : public PhysicsModel { g_23 = 0.0; coord->setCovariantMetricTensor(MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); - coord->calculateGeometry(); - SOLVE_FOR(N, vort); SAVE_REPEAT(phi); if (test_laplacian) { diff --git a/examples/6field-simple/elm_6f.cxx b/examples/6field-simple/elm_6f.cxx index 9a11a09521..6b01a0ae69 100644 --- a/examples/6field-simple/elm_6f.cxx +++ b/examples/6field-simple/elm_6f.cxx @@ -1072,8 +1072,6 @@ class Elm_6f : public PhysicsModel { g_23 = Btxy * hthe * Rxy / Bpxy; coord->setCovariantMetricTensor(MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); - coord->calculateGeometry(); // Calculate quantities from metric tensor - // Set B field vector B0vec.covariant = false; diff --git a/examples/conducting-wall-mode/cwm.cxx b/examples/conducting-wall-mode/cwm.cxx index d00694ff5e..485d78252a 100644 --- a/examples/conducting-wall-mode/cwm.cxx +++ b/examples/conducting-wall-mode/cwm.cxx @@ -208,8 +208,6 @@ class CWM : public PhysicsModel { g_23 = Btxy * hthe * Rxy / Bpxy; coord->setCovariantMetricTensor(MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); - coord->calculateGeometry(); - /**************** SET EVOLVING VARIABLES *************/ // Tell BOUT++ which variables to evolve diff --git a/examples/constraints/alfven-wave/alfven.cxx b/examples/constraints/alfven-wave/alfven.cxx index c28c00ce4f..f224d3d6b2 100644 --- a/examples/constraints/alfven-wave/alfven.cxx +++ b/examples/constraints/alfven-wave/alfven.cxx @@ -220,8 +220,6 @@ class Alfven : public PhysicsModel { g_13 = sinty * Rxy * Rxy; g_23 = sbp * Btxy * hthe * Rxy / Bpxy; coord->setCovariantMetricTensor(MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); - - coord->calculateGeometry(); } }; diff --git a/examples/dalf3/dalf3.cxx b/examples/dalf3/dalf3.cxx index fc71a41923..96102ee6f2 100644 --- a/examples/dalf3/dalf3.cxx +++ b/examples/dalf3/dalf3.cxx @@ -274,8 +274,6 @@ class DALF3 : public PhysicsModel { g_23 = Btxy * hthe * Rxy / Bpxy; coord->setCovariantMetricTensor(MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); - coord->calculateGeometry(); // Calculate quantities from metric tensor - SOLVE_FOR3(Vort, Pe, Vpar); comms.add(Vort, Pe, Vpar); if (!(estatic && ZeroElMass)) { diff --git a/examples/elm-pb-outerloop/elm_pb_outerloop.cxx b/examples/elm-pb-outerloop/elm_pb_outerloop.cxx index 31676e7e6b..7baa422b99 100644 --- a/examples/elm-pb-outerloop/elm_pb_outerloop.cxx +++ b/examples/elm-pb-outerloop/elm_pb_outerloop.cxx @@ -1109,8 +1109,6 @@ class ELMpb : public PhysicsModel { metric->g_13 = I * Rxy * Rxy; metric->g_23 = Btxy * hthe * Rxy / Bpxy; - metric->calculateGeometry(); // Calculate quantities from metric tensor - // Set B field vector B0vec.covariant = false; @@ -1680,12 +1678,12 @@ class ELMpb : public PhysicsModel { #endif }; - // Terms which are not yet single index operators - // Note: Terms which are included in the single index loop - // may be commented out here, to allow comparison/testing + // Terms which are not yet single index operators + // Note: Terms which are included in the single index loop + // may be commented out here, to allow comparison/testing - //////////////////////////////////////////////////// - // Parallel electric field + //////////////////////////////////////////////////// + // Parallel electric field #if not EVOLVE_JPAR // Vector potential diff --git a/examples/elm-pb/elm_pb.cxx b/examples/elm-pb/elm_pb.cxx index e82c51a79f..8ca2fb1cee 100644 --- a/examples/elm-pb/elm_pb.cxx +++ b/examples/elm-pb/elm_pb.cxx @@ -1050,8 +1050,6 @@ class ELMpb : public PhysicsModel { metric->g_13 = I * Rxy * Rxy; metric->g_23 = Btxy * hthe * Rxy / Bpxy; - metric->calculateGeometry(); // Calculate quantities from metric tensor - // Set B field vector B0vec.covariant = false; diff --git a/examples/gyro-gem/gem.cxx b/examples/gyro-gem/gem.cxx index 69b14dbce9..686ff5f01e 100644 --- a/examples/gyro-gem/gem.cxx +++ b/examples/gyro-gem/gem.cxx @@ -390,8 +390,6 @@ class GEM : public PhysicsModel { g_23 = Btxy * hthe * Rxy / Bpxy; coord->setCovariantMetricTensor(MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); - coord->calculateGeometry(); - // Set B field vector B0vec.covariant = false; diff --git a/examples/jorek-compare/jorek_compare.cxx b/examples/jorek-compare/jorek_compare.cxx index 7890c477c2..9fe706ab4b 100644 --- a/examples/jorek-compare/jorek_compare.cxx +++ b/examples/jorek-compare/jorek_compare.cxx @@ -320,8 +320,6 @@ class Jorek : public PhysicsModel { g_23 = Btxy * hthe * Rxy / Bpxy; coord->setCovariantMetricTensor(MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); - coord->calculateGeometry(); // Calculate quantities from metric tensor - // Set B field vector B0vec.covariant = false; B0vec.x = 0.; diff --git a/examples/lapd-drift/lapd_drift.cxx b/examples/lapd-drift/lapd_drift.cxx index d08e644cd6..7362bb3b5d 100644 --- a/examples/lapd-drift/lapd_drift.cxx +++ b/examples/lapd-drift/lapd_drift.cxx @@ -352,8 +352,6 @@ class LAPDdrift : public PhysicsModel { g_23 = Btxy * hthe * Rxy / Bpxy; coord->setCovariantMetricTensor(MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); - coord->calculateGeometry(); - rho0 = Ni0 * Delp2(phi0) + Perp_Grad_dot_Grad(phi0, Ni0); /**************** SET EVOLVING VARIABLES *************/ diff --git a/examples/laplacexy/alfven-wave/alfven.cxx b/examples/laplacexy/alfven-wave/alfven.cxx index 50e8c8fe06..67ca1c3bc6 100644 --- a/examples/laplacexy/alfven-wave/alfven.cxx +++ b/examples/laplacexy/alfven-wave/alfven.cxx @@ -201,7 +201,7 @@ class Alfven : public PhysicsModel { if (min(Bpxy, true) < 0.0) { sbp = -1.0; } - + MetricTensor::FieldMetric g11, g22, g33, g12, g13, g23; g11 = SQ(Rxy * Bpxy); g22 = 1.0 / SQ(hthe); @@ -221,8 +221,6 @@ class Alfven : public PhysicsModel { g_13 = sinty * Rxy * Rxy; g_23 = sbp * Btxy * hthe * Rxy / Bpxy; coord->setCovariantMetricTensor(MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); - - coord->calculateGeometry(); } }; diff --git a/examples/laplacexy/laplace_perp/test.cxx b/examples/laplacexy/laplace_perp/test.cxx index a7932c5885..174ac1e48d 100644 --- a/examples/laplacexy/laplace_perp/test.cxx +++ b/examples/laplacexy/laplace_perp/test.cxx @@ -45,8 +45,6 @@ int main(int argc, char** argv) { g_13 = I * Rxy * Rxy; g_23 = Btxy * hthe * Rxy / Bpxy; coord->setCovariantMetricTensor(MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); - - coord->calculateGeometry(); } /////////////////////////////////////// diff --git a/examples/reconnect-2field/2field.cxx b/examples/reconnect-2field/2field.cxx index 5e830ada78..38c645c663 100644 --- a/examples/reconnect-2field/2field.cxx +++ b/examples/reconnect-2field/2field.cxx @@ -183,8 +183,6 @@ class TwoField : public PhysicsModel { g_23 = Btxy * hthe * Rxy / Bpxy; coord->setCovariantMetricTensor(MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); - coord->calculateGeometry(); - // Tell BOUT++ which variables to evolve SOLVE_FOR(U, Apar); diff --git a/examples/shear-alfven-wave/2fluid.cxx b/examples/shear-alfven-wave/2fluid.cxx index 293f1728b8..a0a1997611 100644 --- a/examples/shear-alfven-wave/2fluid.cxx +++ b/examples/shear-alfven-wave/2fluid.cxx @@ -202,8 +202,6 @@ class ShearAlfven : public PhysicsModel { g_23 = Btxy * hthe * Rxy / Bpxy; coord->setCovariantMetricTensor(MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); - coord->calculateGeometry(); - /**************** SET EVOLVING VARIABLES *************/ // Tell BOUT++ which variables to evolve diff --git a/examples/uedge-benchmark/ue_bmark.cxx b/examples/uedge-benchmark/ue_bmark.cxx index 93cb853193..a52b149194 100644 --- a/examples/uedge-benchmark/ue_bmark.cxx +++ b/examples/uedge-benchmark/ue_bmark.cxx @@ -160,8 +160,6 @@ class UedgeBenchmark : public PhysicsModel { g_23 = Btxy * hthe * Rxy / Bpxy; coords->setCovariantMetricTensor(MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); - coords->calculateGeometry(); // Calculate other metrics - //////////////// BOUNDARIES /////////////////////// // // We want to apply the relaxing boundries to total density, diff --git a/examples/wave-slab/wave_slab.cxx b/examples/wave-slab/wave_slab.cxx index eb1851d69d..fd9692a2c5 100644 --- a/examples/wave-slab/wave_slab.cxx +++ b/examples/wave-slab/wave_slab.cxx @@ -51,8 +51,6 @@ class WaveTest : public PhysicsModel { g_23 = Btxy * hthe * Rxy / Bpxy; coords->setCovariantMetricTensor(MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); - coords->calculateGeometry(); - solver->add(f, "f"); solver->add(g, "g"); diff --git a/tests/MMS/diffusion/diffusion.cxx b/tests/MMS/diffusion/diffusion.cxx index 55bbf49b18..02d0e6fe80 100644 --- a/tests/MMS/diffusion/diffusion.cxx +++ b/tests/MMS/diffusion/diffusion.cxx @@ -43,15 +43,12 @@ int Diffusion::init(bool UNUSED(restarting)) { SAVE_ONCE(mu_N); //set mesh - auto contravariant_metric_tensor = - MetricTensor(1.1, 1.1, 1.1, 0.0, 0.0, 0.0); + auto contravariant_metric_tensor = MetricTensor(1.1, 1.1, 1.1, 0.0, 0.0, 0.0); coord->setContravariantMetricTensor(contravariant_metric_tensor); auto covariant_metric_tensor = MetricTensor(1.1, 1.1, 1.1, 0.0, 0.0, 0.0); coord->setCovariantMetricTensor(covariant_metric_tensor); - coord->calculateGeometry(); - // Tell BOUT++ to solve N SOLVE_FOR(N); diff --git a/tests/MMS/wave-1d/wave.cxx b/tests/MMS/wave-1d/wave.cxx index b763f66ef9..38a95942b3 100644 --- a/tests/MMS/wave-1d/wave.cxx +++ b/tests/MMS/wave-1d/wave.cxx @@ -38,8 +38,6 @@ class Wave1D : public PhysicsModel { auto covariant_metric_tensor = MetricTensor(1.1, 1.1, 1.1, 0.0, 0.0, 0.0); coord->setCovariantMetricTensor(covariant_metric_tensor); - coord->calculateGeometry(); - g.setLocation(CELL_XLOW); // g staggered to the left of f // Tell BOUT++ to solve f and g diff --git a/tests/integrated/test-drift-instability/2fluid.cxx b/tests/integrated/test-drift-instability/2fluid.cxx index 06ef225371..9b195b3a7b 100644 --- a/tests/integrated/test-drift-instability/2fluid.cxx +++ b/tests/integrated/test-drift-instability/2fluid.cxx @@ -237,8 +237,6 @@ class TwoFluid : public PhysicsModel { g_23 = Btxy * hthe * Rxy / Bpxy; coord->setCovariantMetricTensor(MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); - coord->calculateGeometry(); - /**************** SET EVOLVING VARIABLES *************/ // Tell BOUT++ which variables to evolve diff --git a/tests/integrated/test-interchange-instability/2fluid.cxx b/tests/integrated/test-interchange-instability/2fluid.cxx index 02cba35569..2c0fe22de2 100644 --- a/tests/integrated/test-interchange-instability/2fluid.cxx +++ b/tests/integrated/test-interchange-instability/2fluid.cxx @@ -159,8 +159,6 @@ class Interchange : public PhysicsModel { g_23 = Btxy * hthe * Rxy / Bpxy; coord->setCovariantMetricTensor(MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); - coord->calculateGeometry(); - // Tell BOUT++ which variables to evolve SOLVE_FOR2(rho, Ni); diff --git a/tests/unit/invert/laplace/test_laplace_cyclic.cxx b/tests/unit/invert/laplace/test_laplace_cyclic.cxx index 6c36154f9f..b0cc73bf37 100644 --- a/tests/unit/invert/laplace/test_laplace_cyclic.cxx +++ b/tests/unit/invert/laplace/test_laplace_cyclic.cxx @@ -85,7 +85,6 @@ class CyclicTest : public FakeMeshFixture, static_cast(bout::globals::mesh) ->setGridDataSource(new GridFromOptions(Options::getRoot())); - bout::globals::mesh->getCoordinates()->calculateGeometry(); f3.allocate(); coef2.allocate(); coef3.allocate(); From d4503f1187e6b4cb7be9e420c19173e62c787f5f Mon Sep 17 00:00:00 2001 From: tomc271 Date: Tue, 5 Dec 2023 12:49:54 +0000 Subject: [PATCH 254/491] Remove calls to calcCovariant() and calcContravariant() since they are now calculated when calling setContravariantMetricTensor() and setCovariantMetricTensor(). --- include/bout/coordinates.hxx | 6 ------ include/bout/geometry.hxx | 6 ------ src/mesh/coordinates.cxx | 23 ----------------------- src/mesh/geometry.cxx | 10 ---------- tests/unit/mesh/test_coordinates.cxx | 8 ++------ 5 files changed, 2 insertions(+), 51 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index 31f51b4cc2..de940b6e3c 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -129,12 +129,6 @@ public: /// Calculate differential geometry quantities from the metric tensor int calculateGeometry(); - /// Invert contravariant metric to get covariant components - void calcCovariant(const std::string& region = "RGN_ALL"); - - /// Invert covariant metric to get contravariant components - void calcContravariant(const std::string& region = "RGN_ALL"); - void jacobian(); ///< Calculate J and Bxy /////////////////////////////////////////////////////////// diff --git a/include/bout/geometry.hxx b/include/bout/geometry.hxx index 55e4a3b2f4..dcc31b237b 100644 --- a/include/bout/geometry.hxx +++ b/include/bout/geometry.hxx @@ -141,12 +141,6 @@ public: void setBxy(FieldMetric Bxy); - /// Invert contravariant metric to get covariant components - void calcCovariant(const std::string& region = "RGN_ALL"); - - /// Invert covariant metric to get contravariant components - void calcContravariant(const std::string& region = "RGN_ALL"); - /// Calculate Christoffel symbol terms void CalculateChristoffelSymbols(FieldMetric& dx, FieldMetric& dy); diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 42698c78bc..c72d8aa9b1 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -493,19 +493,6 @@ void Coordinates::setBoundaryCells(Mesh* mesh, Options* options, } else { output_warn.write("Not all covariant components of metric tensor found. " "Calculating all from the contravariant tensor\n"); - /// Calculate contravariant metric components if not found - try { - calcCovariant("RGN_NOCORNERS"); - } catch (BoutException&) { - throw BoutException("Error in calcCovariant call"); - } - } - } else { - /// Calculate contravariant metric components if not found - try { - calcCovariant("RGN_NOCORNERS"); - } catch (BoutException&) { - throw BoutException("Error in calcCovariant call"); } } @@ -794,16 +781,6 @@ void Coordinates::calculateCommunicateAndExtrapolateChristoffelSymbols() { geometry.applyToChristoffelSymbols(interpolateAndExtrapolate_function); } -void Coordinates::calcCovariant(const std::string& region) { - TRACE("Coordinates::calcCovariant"); - geometry.calcCovariant(region); -} - -void Coordinates::calcContravariant(const std::string& region) { - TRACE("Coordinates::calcContravariant"); - geometry.calcContravariant(region); -} - void Coordinates::jacobian() { TRACE("Coordinates::jacobian"); diff --git a/src/mesh/geometry.cxx b/src/mesh/geometry.cxx index 3907e5b70b..73bd3f9a11 100644 --- a/src/mesh/geometry.cxx +++ b/src/mesh/geometry.cxx @@ -213,16 +213,6 @@ void Geometry::CalculateChristoffelSymbols(FieldMetric& dx, FieldMetric& dy) { * differential_operators->DDY(covariantMetricTensor.Getg33(), dy); } -void Geometry::calcCovariant(const std::string& region) { - TRACE("Geometry::calcCovariant"); - covariantMetricTensor.setMetricTensor(contravariantMetricTensor.inverse(region)); -} - -void Geometry::calcContravariant(const std::string& region) { - TRACE("Geometry::calcContravariant"); - contravariantMetricTensor.setMetricTensor(covariantMetricTensor.inverse(region)); -} - MetricTensor::FieldMetric Geometry::recalculateJacobian() { // calculate Jacobian using g^-1 = det[g^ij], J = sqrt(g) diff --git a/tests/unit/mesh/test_coordinates.cxx b/tests/unit/mesh/test_coordinates.cxx index abccacedbb..86335d9c6a 100644 --- a/tests/unit/mesh/test_coordinates.cxx +++ b/tests/unit/mesh/test_coordinates.cxx @@ -139,9 +139,7 @@ TEST_F(CoordinatesTest, CalcContravariant) { FieldMetric{0.0}}; // IntShiftTorsion // No call to Coordinates::calculateGeometry() needed here - output_info.disable(); - coords.calcCovariant(); - output_info.enable(); + coords.setContravariantMetricTensor(MetricTensor(1.0, 1.0, 1.0, 0.0, 0.0, 0.0)); EXPECT_TRUE(IsFieldEqual(coords.g_11(), 1.0)); EXPECT_TRUE(IsFieldEqual(coords.g_22(), 1.0)); @@ -174,9 +172,7 @@ TEST_F(CoordinatesTest, CalcCovariant) { FieldMetric{0.0}}; // IntShiftTorsion // No call to Coordinates::calculateGeometry() needed here - output_info.disable(); - coords.calcContravariant(); - output_info.enable(); + coords.setCovariantMetricTensor(MetricTensor(1.0, 1.0, 1.0, 0.0, 0.0, 0.0)); EXPECT_TRUE(IsFieldEqual(coords.g11(), 1.0)); EXPECT_TRUE(IsFieldEqual(coords.g22(), 1.0)); From 904546bbb61e9534a94fb92b39cb15befd85e9e7 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Tue, 5 Dec 2023 14:08:30 +0000 Subject: [PATCH 255/491] Update documentation. --- manual/sphinx/developer_docs/mesh.rst | 3 --- 1 file changed, 3 deletions(-) diff --git a/manual/sphinx/developer_docs/mesh.rst b/manual/sphinx/developer_docs/mesh.rst index 072645f3cc..b4b7fc14d4 100644 --- a/manual/sphinx/developer_docs/mesh.rst +++ b/manual/sphinx/developer_docs/mesh.rst @@ -278,9 +278,6 @@ metric tensor components are public members of `Coordinates`:: // Covariant metric tensor FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; - int calcCovariant(); // Invert contravatiant metric to get covariant - int calcContravariant(); // Invert covariant metric to get contravariant - If only one of these sets is modified by an external code, then `Coordinates::calcCovariant()` and `Coordinates::calcContravariant()` can be used to calculate the other (uses Gauss-Jordan currently). From dc82c8b66508ff1082ecc56d616784caf774cbd9 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Tue, 5 Dec 2023 14:09:30 +0000 Subject: [PATCH 256/491] Make parameters to Coordinates constructor const references. --- include/bout/coordinates.hxx | 12 +++++++----- src/mesh/coordinates.cxx | 28 ++++++++++++++-------------- 2 files changed, 21 insertions(+), 19 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index de940b6e3c..c8866dcc20 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -63,11 +63,13 @@ public: /// A constructor useful for testing purposes. To use it, inherit /// from Coordinates. If \p calculate_geometry is true (default), /// calculate the non-uniform variables, Christoffel symbols - Coordinates(Mesh* mesh, FieldMetric dx, FieldMetric dy, FieldMetric dz, FieldMetric J, - FieldMetric Bxy, FieldMetric g11, FieldMetric g22, FieldMetric g33, - FieldMetric g12, FieldMetric g13, FieldMetric g23, FieldMetric g_11, - FieldMetric g_22, FieldMetric g_33, FieldMetric g_12, FieldMetric g_13, - FieldMetric g_23, FieldMetric ShiftTorsion, FieldMetric IntShiftTorsion); + Coordinates(Mesh* mesh, FieldMetric dx, FieldMetric dy, FieldMetric dz, + const FieldMetric& J, const FieldMetric& Bxy, const FieldMetric& g11, + const FieldMetric& g22, const FieldMetric& g33, const FieldMetric& g12, + const FieldMetric& g13, const FieldMetric& g23, const FieldMetric& g_11, + const FieldMetric& g_22, const FieldMetric& g_33, const FieldMetric& g_12, + const FieldMetric& g_13, const FieldMetric& g_23, FieldMetric ShiftTorsion, + FieldMetric IntShiftTorsion); /// Add variables to \p output_options, for post-processing void outputVars(Options& output_options); diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index c72d8aa9b1..8475d8c237 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -281,20 +281,20 @@ Coordinates::FieldMetric Coordinates::getUnalignedAtLocationAndFillGuards( } Coordinates::Coordinates(Mesh* mesh, FieldMetric dx, FieldMetric dy, FieldMetric dz, - FieldMetric J, FieldMetric Bxy, FieldMetric g11, FieldMetric g22, - FieldMetric g33, FieldMetric g12, FieldMetric g13, - FieldMetric g23, FieldMetric g_11, FieldMetric g_22, - FieldMetric g_33, FieldMetric g_12, FieldMetric g_13, - FieldMetric g_23, FieldMetric ShiftTorsion, - FieldMetric IntShiftTorsion) - : dx(std::move(dx)), dy(std::move(dy)), dz(dz), ShiftTorsion(std::move(ShiftTorsion)), - IntShiftTorsion(std::move(IntShiftTorsion)), nz(mesh->LocalNz), localmesh(mesh), - location(CELL_CENTRE), differential_operators(mesh->getDifferentialOperators()), - geometry(Geometry(std::move(J), std::move(Bxy), std::move(g11), std::move(g22), - std::move(g33), std::move(g12), std::move(g13), std::move(g23), - std::move(g_11), std::move(g_22), std::move(g_33), - std::move(g_12), std::move(g_13), std::move(g_23), - differential_operators)) {} + const FieldMetric& J, const FieldMetric& Bxy, + const FieldMetric& g11, const FieldMetric& g22, + const FieldMetric& g33, const FieldMetric& g12, + const FieldMetric& g13, const FieldMetric& g23, + const FieldMetric& g_11, const FieldMetric& g_22, + const FieldMetric& g_33, const FieldMetric& g_12, + const FieldMetric& g_13, const FieldMetric& g_23, + FieldMetric ShiftTorsion, FieldMetric IntShiftTorsion) + : dx(std::move(dx)), dy(std::move(dy)), dz(std::move(dz)), + ShiftTorsion(std::move(ShiftTorsion)), IntShiftTorsion(std::move(IntShiftTorsion)), + nz(mesh->LocalNz), localmesh(mesh), location(CELL_CENTRE), + differential_operators(mesh->getDifferentialOperators()), + geometry(Geometry(J, Bxy, g11, g22, g33, g12, g13, g23, g_11, g_22, g_33, g_12, + g_13, g_23, differential_operators)) {} Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, const Coordinates* coords_in, bool force_interpolate_from_centre) From 924618ddcded06cd806398c66ee2dbd1fbda05e2 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Tue, 5 Dec 2023 14:13:04 +0000 Subject: [PATCH 257/491] Prefer const. --- src/mesh/coordinates.cxx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 8475d8c237..b16722be4c 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -309,7 +309,7 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, nz = mesh->LocalNz; - std::string suffix = getLocationSuffix(location); + const std::string suffix = getLocationSuffix(location); if (coords_in == nullptr || suffix.empty() || (!force_interpolate_from_centre && mesh->sourceHasVar("dx" + suffix))) { From ab2e12218048becedbbd0106f064b667159a7499 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Tue, 5 Dec 2023 14:14:29 +0000 Subject: [PATCH 258/491] Remove unused setters for Christoffel symbol terms. --- include/bout/geometry.hxx | 21 --------------------- src/mesh/geometry.cxx | 21 --------------------- 2 files changed, 42 deletions(-) diff --git a/include/bout/geometry.hxx b/include/bout/geometry.hxx index dcc31b237b..66f2666634 100644 --- a/include/bout/geometry.hxx +++ b/include/bout/geometry.hxx @@ -111,27 +111,6 @@ public: void setCovariantMetricTensor(MetricTensor metric_tensor, const std::string& region = "RGN_ALL"); - void setG1_11(FieldMetric G1_11); - void setG1_22(FieldMetric G1_22); - void setG1_33(FieldMetric G1_33); - void setG1_12(FieldMetric G1_12); - void setG1_13(FieldMetric G1_13); - void setG1_23(FieldMetric G1_23); - - void setG2_11(FieldMetric G2_11); - void setG2_22(FieldMetric G2_22); - void setG2_33(FieldMetric G2_33); - void setG2_12(FieldMetric G2_12); - void setG2_13(FieldMetric G2_13); - void setG2_23(FieldMetric G2_23); - - void setG3_11(FieldMetric G3_11); - void setG3_22(FieldMetric G3_22); - void setG3_33(FieldMetric G3_33); - void setG3_12(FieldMetric G3_12); - void setG3_13(FieldMetric G3_13); - void setG3_23(FieldMetric G3_23); - void setG3(FieldMetric G3); void setG1(FieldMetric G1); void setG2(FieldMetric G2); diff --git a/src/mesh/geometry.cxx b/src/mesh/geometry.cxx index 73bd3f9a11..88c1a1a6fa 100644 --- a/src/mesh/geometry.cxx +++ b/src/mesh/geometry.cxx @@ -321,27 +321,6 @@ const FieldMetric& Geometry::J() const { return this_J; } const FieldMetric& Geometry::Bxy() const { return this_Bxy; } -void Geometry::setG1_11(FieldMetric G1_11) { G1_11_ = G1_11; } -void Geometry::setG1_22(FieldMetric G1_22) { G1_11_ = G1_22; } -void Geometry::setG1_33(FieldMetric G1_33) { G1_11_ = G1_33; } -void Geometry::setG1_12(FieldMetric G1_12) { G1_11_ = G1_12; } -void Geometry::setG1_13(FieldMetric G1_13) { G1_11_ = G1_13; } -void Geometry::setG1_23(FieldMetric G1_23) { G1_11_ = G1_23; } - -void Geometry::setG2_11(FieldMetric G2_11) { G2_11_ = G2_11; } -void Geometry::setG2_22(FieldMetric G2_22) { G2_11_ = G2_22; } -void Geometry::setG2_33(FieldMetric G2_33) { G2_11_ = G2_33; } -void Geometry::setG2_12(FieldMetric G2_12) { G2_11_ = G2_12; } -void Geometry::setG2_13(FieldMetric G2_13) { G2_11_ = G2_13; } -void Geometry::setG2_23(FieldMetric G2_23) { G2_11_ = G2_23; } - -void Geometry::setG3_11(FieldMetric G3_11) { G3_11_ = G3_11; } -void Geometry::setG3_22(FieldMetric G3_22) { G3_11_ = G3_22; } -void Geometry::setG3_33(FieldMetric G3_33) { G3_11_ = G3_33; } -void Geometry::setG3_12(FieldMetric G3_12) { G3_11_ = G3_12; } -void Geometry::setG3_13(FieldMetric G3_13) { G3_11_ = G3_13; } -void Geometry::setG3_23(FieldMetric G3_23) { G3_11_ = G3_23; } - void Geometry::setG1(FieldMetric G1) { G1_ = G1; } void Geometry::setG2(FieldMetric G2) { G2_ = G2; } void Geometry::setG3(FieldMetric G3) { G3_ = G3; } From 07a286240b4a201c383fe7f4af901db93788aadd Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 6 Dec 2023 09:40:18 +0000 Subject: [PATCH 259/491] Make dx, dy, dz private. --- include/bout/coordinates.hxx | 30 ++-- include/bout/geometry.hxx | 2 +- .../laplace/impls/cyclic/cyclic_laplace.cxx | 2 +- src/mesh/coordinates.cxx | 149 ++++++++++-------- src/mesh/coordinates_accessor.cxx | 10 +- src/mesh/geometry.cxx | 2 +- 6 files changed, 109 insertions(+), 86 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index c8866dcc20..d9fc9bb68f 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -63,24 +63,32 @@ public: /// A constructor useful for testing purposes. To use it, inherit /// from Coordinates. If \p calculate_geometry is true (default), /// calculate the non-uniform variables, Christoffel symbols - Coordinates(Mesh* mesh, FieldMetric dx, FieldMetric dy, FieldMetric dz, - const FieldMetric& J, const FieldMetric& Bxy, const FieldMetric& g11, - const FieldMetric& g22, const FieldMetric& g33, const FieldMetric& g12, - const FieldMetric& g13, const FieldMetric& g23, const FieldMetric& g_11, - const FieldMetric& g_22, const FieldMetric& g_33, const FieldMetric& g_12, - const FieldMetric& g_13, const FieldMetric& g_23, FieldMetric ShiftTorsion, - FieldMetric IntShiftTorsion); + Coordinates(Mesh* mesh, const FieldMetric& dx, const FieldMetric& dy, + const FieldMetric& dz, const FieldMetric& J, const FieldMetric& Bxy, + const FieldMetric& g11, const FieldMetric& g22, const FieldMetric& g33, + const FieldMetric& g12, const FieldMetric& g13, const FieldMetric& g23, + const FieldMetric& g_11, const FieldMetric& g_22, const FieldMetric& g_33, + const FieldMetric& g_12, const FieldMetric& g_13, const FieldMetric& g_23, + FieldMetric ShiftTorsion, FieldMetric IntShiftTorsion); /// Add variables to \p output_options, for post-processing void outputVars(Options& output_options); - FieldMetric dx, dy, dz; ///< Mesh spacing in x, y and z + ///< Mesh spacing in x, y and z + const FieldMetric& dx() const; + const FieldMetric& dy() const; + const FieldMetric& dz() const; + + void setDx(FieldMetric dx); + void setDy(FieldMetric dy); + void setDz(FieldMetric dz); /// Length of the Z domain. Used for FFTs const Field2D& zlength() const; /// True if corrections for non-uniform mesh spacing should be included in operators bool non_uniform{}; + /// 2nd-order correction for non-uniform meshes d/di(1/dx), d/di(1/dy) and d/di(1/dz) FieldMetric d1_dx, d1_dy, d1_dz; @@ -129,7 +137,7 @@ public: const MetricTensor& getContravariantMetricTensor() const; /// Calculate differential geometry quantities from the metric tensor - int calculateGeometry(); + int calculateGeometry() const; void jacobian(); ///< Calculate J and Bxy @@ -228,7 +236,7 @@ public: // Full perpendicular Laplacian, in form of inverse of Laplacian operator in LaplaceXY // solver - Field2D Laplace_perpXY(const Field2D& A, const Field2D& f); + Field2D Laplace_perpXY(const Field2D& A, const Field2D& f) const; const FieldMetric& G1_11(); const FieldMetric& G1_22(); @@ -274,6 +282,8 @@ private: DifferentialOperators* differential_operators; Geometry geometry; + FieldMetric dx_, dy_, dz_; ///< Mesh spacing in x, y and z + /// Handles calculation of yup and ydown std::unique_ptr transform{nullptr}; diff --git a/include/bout/geometry.hxx b/include/bout/geometry.hxx index 66f2666634..39d0d2b731 100644 --- a/include/bout/geometry.hxx +++ b/include/bout/geometry.hxx @@ -121,7 +121,7 @@ public: void setBxy(FieldMetric Bxy); /// Calculate Christoffel symbol terms - void CalculateChristoffelSymbols(FieldMetric& dx, FieldMetric& dy); + void CalculateChristoffelSymbols(const FieldMetric& dx, const FieldMetric& dy); // check that covariant tensors are positive (if expected) and finite (always) void checkCovariant(int ystart); diff --git a/src/invert/laplace/impls/cyclic/cyclic_laplace.cxx b/src/invert/laplace/impls/cyclic/cyclic_laplace.cxx index cf16240c0c..ce9378fd9d 100644 --- a/src/invert/laplace/impls/cyclic/cyclic_laplace.cxx +++ b/src/invert/laplace/impls/cyclic/cyclic_laplace.cxx @@ -160,7 +160,7 @@ FieldPerp LaplaceCyclic::solve(const FieldPerp& rhs, const FieldPerp& x0) { // Get elements of the tridiagonal matrix // including boundary conditions - BoutReal zlen = getUniform(coords->dz) * (localmesh->LocalNz - 3); + BoutReal zlen = getUniform(coords->dz()) * (localmesh->LocalNz - 3); BOUT_OMP_PERF(for nowait) for (int kz = 0; kz < nmode; kz++) { // wave number is 1/[rad]; DST has extra 2. diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index b16722be4c..eba93a0321 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -280,25 +280,26 @@ Coordinates::FieldMetric Coordinates::getUnalignedAtLocationAndFillGuards( pParallelTransform); } -Coordinates::Coordinates(Mesh* mesh, FieldMetric dx, FieldMetric dy, FieldMetric dz, - const FieldMetric& J, const FieldMetric& Bxy, - const FieldMetric& g11, const FieldMetric& g22, - const FieldMetric& g33, const FieldMetric& g12, - const FieldMetric& g13, const FieldMetric& g23, - const FieldMetric& g_11, const FieldMetric& g_22, - const FieldMetric& g_33, const FieldMetric& g_12, - const FieldMetric& g_13, const FieldMetric& g_23, - FieldMetric ShiftTorsion, FieldMetric IntShiftTorsion) - : dx(std::move(dx)), dy(std::move(dy)), dz(std::move(dz)), - ShiftTorsion(std::move(ShiftTorsion)), IntShiftTorsion(std::move(IntShiftTorsion)), +Coordinates::Coordinates(Mesh* mesh, const FieldMetric& dx, const FieldMetric& dy, + const FieldMetric& dz, const FieldMetric& J, + const FieldMetric& Bxy, const FieldMetric& g11, + const FieldMetric& g22, const FieldMetric& g33, + const FieldMetric& g12, const FieldMetric& g13, + const FieldMetric& g23, const FieldMetric& g_11, + const FieldMetric& g_22, const FieldMetric& g_33, + const FieldMetric& g_12, const FieldMetric& g_13, + const FieldMetric& g_23, FieldMetric ShiftTorsion, + FieldMetric IntShiftTorsion) + : ShiftTorsion(std::move(ShiftTorsion)), IntShiftTorsion(std::move(IntShiftTorsion)), nz(mesh->LocalNz), localmesh(mesh), location(CELL_CENTRE), differential_operators(mesh->getDifferentialOperators()), geometry(Geometry(J, Bxy, g11, g22, g33, g12, g13, g23, g_11, g_22, g_33, g_12, - g_13, g_23, differential_operators)) {} + g_13, g_23, differential_operators)), + dx_(dx), dy_(dy), dz_(dz) {} Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, const Coordinates* coords_in, bool force_interpolate_from_centre) - : dx(1., mesh), dy(1., mesh), dz(1., mesh), d1_dx(mesh), d1_dy(mesh), d1_dz(mesh), + : dx_(1., mesh), dy_(1., mesh), dz_(1., mesh), d1_dx(mesh), d1_dy(mesh), d1_dz(mesh), ShiftTorsion(mesh), IntShiftTorsion(mesh), localmesh(mesh), location(loc), differential_operators(mesh->getDifferentialOperators()), geometry(Geometry(mesh, differential_operators)) { @@ -357,9 +358,9 @@ void Coordinates::interpolateFieldsFromOtherCoordinates(const Mesh* mesh, coords_in->IntShiftTorsion, location, true, true, false, transform.get()); } - if (isUniform(coords_in->dz)) { - dz = coords_in->dz; - dz.setLocation(location); + if (isUniform(coords_in->dz())) { + dz_ = coords_in->dz(); + dz_.setLocation(location); } else { throw BoutException("We are asked to transform dz to get dz before we " "have a transform, which " @@ -367,13 +368,13 @@ void Coordinates::interpolateFieldsFromOtherCoordinates(const Mesh* mesh, "staggered quantity!"); } setParallelTransform(options); - dx = localmesh->interpolateAndExtrapolate(coords_in->dx, location, true, true, false, - transform.get()); - dy = localmesh->interpolateAndExtrapolate(coords_in->dy, location, true, true, false, - transform.get()); + dx_ = localmesh->interpolateAndExtrapolate(coords_in->dx(), location, true, true, false, + transform.get()); + dy_ = localmesh->interpolateAndExtrapolate(coords_in->dy(), location, true, true, false, + transform.get()); // not really needed - we have used dz already ... - dz = localmesh->interpolateAndExtrapolate(coords_in->dz, location, true, true, false, - transform.get()); + dz_ = localmesh->interpolateAndExtrapolate(coords_in->dz(), location, true, true, false, + transform.get()); } // Note: If boundary cells were not loaded from the grid file, use @@ -399,8 +400,8 @@ void Coordinates::setBoundaryCells(Mesh* mesh, Options* options, } if (coords_in == nullptr) { - mesh->get(dx, "dx", 1.0, false); - mesh->get(dy, "dy", 1.0, false); + mesh->get(dx_, "dx", 1.0, false); + mesh->get(dy_, "dy", 1.0, false); } nz = mesh->LocalNz; @@ -413,26 +414,26 @@ void Coordinates::setBoundaryCells(Mesh* mesh, Options* options, : options["ZMAX"].withDefault(1.0); const auto default_dz = (zmax - zmin) * TWOPI / nz; - getAtLoc(mesh, dz, "dz", suffix, location, default_dz); + getAtLoc(mesh, dz_, "dz", suffix, location, default_dz); } // required early for differentiation. setParallelTransform(options); - dz = localmesh->interpolateAndExtrapolate(dz, location, extrapolate_x, extrapolate_y, - false, transform.get()); + dz_ = localmesh->interpolateAndExtrapolate(dz_, location, extrapolate_x, extrapolate_y, + false, transform.get()); - dx = getUnalignedAtLocationAndFillGuards(mesh, "dx", 1.0, suffix, location, - extrapolate_x, extrapolate_y, false, - transform.get()); + dx_ = getUnalignedAtLocationAndFillGuards(mesh, "dx", 1.0, suffix, location, + extrapolate_x, extrapolate_y, false, + transform.get()); if (mesh->periodicX) { - communicate(dx); + communicate(dx_); } - dy = getUnalignedAtLocationAndFillGuards(mesh, "dy", 1.0, suffix, location, - extrapolate_x, extrapolate_y, false, - transform.get()); + dy_ = getUnalignedAtLocationAndFillGuards(mesh, "dy", 1.0, suffix, location, + extrapolate_x, extrapolate_y, false, + transform.get()); // grid data source has staggered fields, so read instead of interpolating // Diagonal components of metric tensor g^{ij} (default to 1) @@ -585,9 +586,9 @@ void Coordinates::outputVars(Options& output_options) { const std::string loc_string = (location == CELL_CENTRE) ? "" : "_" + toString(location); - output_options["dx" + loc_string].force(dx, "Coordinates"); - output_options["dy" + loc_string].force(dy, "Coordinates"); - output_options["dz" + loc_string].force(dz, "Coordinates"); + output_options["dx" + loc_string].force(dx(), "Coordinates"); + output_options["dy" + loc_string].force(dy(), "Coordinates"); + output_options["dz" + loc_string].force(dz(), "Coordinates"); output_options["g11" + loc_string].force(g11(), "Coordinates"); output_options["g22" + loc_string].force(g22(), "Coordinates"); @@ -621,30 +622,31 @@ const Field2D& Coordinates::zlength() const { #if BOUT_USE_METRIC_3D BOUT_FOR_SERIAL(i, dz.getRegion("RGN_ALL")) { (*zlength_cache)[i] += dz[i]; } #else - (*zlength_cache) = dz * nz; + (*zlength_cache) = dz_ * nz; #endif } return *zlength_cache; } -int Coordinates::calculateGeometry() { +int Coordinates::calculateGeometry() const { TRACE("Coordinates::calculateGeometry"); - communicate(dx, dy, dz, g11(), g22(), g33(), g12(), g13(), g23(), g_11(), g_22(), + auto tmp = dx(); // TODO: There must be a better way than this! + communicate(tmp, dy(), dz(), g11(), g22(), g33(), g12(), g13(), g23(), g_11(), g_22(), g_33(), g_12(), g_13(), g_23(), J(), Bxy()); output_progress.write("Calculating differential geometry terms\n"); - if (min(abs(dx)) < 1e-8) { + if (min(abs(dx())) < 1e-8) { throw BoutException("dx magnitude less than 1e-8"); } - if (min(abs(dy)) < 1e-8) { + if (min(abs(dy())) < 1e-8) { throw BoutException("dy magnitude less than 1e-8"); } - if (min(abs(dz)) < 1e-8) { + if (min(abs(dz())) < 1e-8) { throw BoutException("dz magnitude less than 1e-8"); } @@ -691,7 +693,7 @@ void Coordinates::correctionForNonUniformMeshes(bool force_interpolate_from_cent if (localmesh->get(d2x, "d2x" + suffix, 0.0, false, location) != 0) { output_warn.write("\tWARNING: differencing quantity 'd2x' not found. " "Calculating from dx\n"); - d1_dx = bout::derivatives::index::DDX(1. / dx); // d/di(1/dx) + d1_dx = bout::derivatives::index::DDX(1. / dx()); // d/di(1/dx) communicate(d1_dx); d1_dx = localmesh->interpolateAndExtrapolate(d1_dx, location, true, true, true, @@ -702,13 +704,13 @@ void Coordinates::correctionForNonUniformMeshes(bool force_interpolate_from_cent d2x = localmesh->interpolateAndExtrapolate(d2x, location, extrapolate_x, extrapolate_y, false, transform.get()); - d1_dx = -d2x / (dx * dx); + d1_dx = -d2x / (dx() * dx()); } if (localmesh->get(d2y, "d2y" + suffix, 0.0, false, location) != 0) { output_warn.write("\tWARNING: differencing quantity 'd2y' not found. " "Calculating from dy\n"); - d1_dy = bout::derivatives::index::DDY(1. / dy); // d/di(1/dy) + d1_dy = bout::derivatives::index::DDY(1. / dy()); // d/di(1/dy) communicate(d1_dy); d1_dy = localmesh->interpolateAndExtrapolate(d1_dy, location, true, true, true, @@ -719,14 +721,14 @@ void Coordinates::correctionForNonUniformMeshes(bool force_interpolate_from_cent d2y = localmesh->interpolateAndExtrapolate(d2y, location, extrapolate_x, extrapolate_y, false, transform.get()); - d1_dy = -d2y / (dy * dy); + d1_dy = -d2y / (dy() * dy()); } #if BOUT_USE_METRIC_3D if (localmesh->get(d2z, "d2z" + suffix, 0.0, false)) { output_warn.write("\tWARNING: differencing quantity 'd2z' not found. " "Calculating from dz\n"); - d1_dz = bout::derivatives::index::DDZ(1. / dz); + d1_dz = bout::derivatives::index::DDZ(1. / dz()); communicate(d1_dz); d1_dz = localmesh->interpolateAndExtrapolate(d1_dz, location, true, true, true, transform.get()); @@ -736,7 +738,7 @@ void Coordinates::correctionForNonUniformMeshes(bool force_interpolate_from_cent d2z = localmesh->interpolateAndExtrapolate(d2z, location, extrapolate_x, extrapolate_y, false, transform.get()); - d1_dz = -d2z / (dz * dz); + d1_dz = -d2z / (dz() * dz()); } #else d1_dz = 0; @@ -747,7 +749,7 @@ void Coordinates::correctionForNonUniformMeshes(bool force_interpolate_from_cent void Coordinates::calculateCommunicateAndExtrapolateChristoffelSymbols() { - geometry.CalculateChristoffelSymbols(dx, dy); + geometry.CalculateChristoffelSymbols(dx(), dy()); auto tmp = J() * g12(); communicate(tmp); @@ -906,8 +908,8 @@ void Coordinates::setParallelTransform(Options* options) { // Flux Coordinate Independent method const bool fci_zperiodic = (*ptoptions)["z_periodic"].withDefault(true); - transform = - bout::utils::make_unique(*localmesh, dy, fci_zperiodic, ptoptions); + transform = bout::utils::make_unique(*localmesh, dy(), fci_zperiodic, + ptoptions); } else { throw BoutException(_("Unrecognised paralleltransform option.\n" @@ -923,23 +925,24 @@ void Coordinates::setParallelTransform(Options* options) { Field2D Coordinates::DDX(const Field2D& f, CELL_LOC loc, const std::string& method, const std::string& region) const { ASSERT1(location == loc || loc == CELL_DEFAULT) - return differential_operators->DDX(f, dx, loc, method, region); + return differential_operators->DDX(f, dx(), loc, method, region); } Field3D Coordinates::DDX(const Field3D& f, CELL_LOC outloc, const std::string& method, const std::string& region) const { - return differential_operators->DDX(f, dx, dz, IntShiftTorsion, outloc, method, region); + return differential_operators->DDX(f, dx(), dz(), IntShiftTorsion, outloc, method, + region); } Field2D Coordinates::DDY(const Field2D& f, CELL_LOC loc, const std::string& method, const std::string& region) const { ASSERT1(location == loc || loc == CELL_DEFAULT) - return differential_operators->DDY(f, dy, loc, method, region); + return differential_operators->DDY(f, dy(), loc, method, region); } Field3D Coordinates::DDY(const Field3D& f, CELL_LOC outloc, const std::string& method, const std::string& region) const { - return differential_operators->DDY(f, dy, outloc, method, region); + return differential_operators->DDY(f, dy(), outloc, method, region); } Field2D Coordinates::DDZ(const Field2D& f, CELL_LOC loc, const std::string& method, @@ -954,7 +957,7 @@ Field2D Coordinates::DDZ(const Field2D& f, CELL_LOC loc, const std::string& meth Field3D Coordinates::DDZ(const Field3D& f, CELL_LOC outloc, const std::string& method, const std::string& region) const { - return differential_operators->DDZ(f, dz, outloc, method, region); + return differential_operators->DDZ(f, dz(), outloc, method, region); } ///////////////////////////////////////////////////////// @@ -964,7 +967,7 @@ Field2D Coordinates::Grad_par(const Field2D& var, [[maybe_unused]] CELL_LOC outl const std::string& UNUSED(method)) { TRACE("Coordinates::Grad_par( Field2D )"); - return differential_operators->Grad_par(var, dy); + return differential_operators->Grad_par(var, dy()); } Field3D Coordinates::Grad_par(const Field3D& var, CELL_LOC outloc, @@ -972,7 +975,7 @@ Field3D Coordinates::Grad_par(const Field3D& var, CELL_LOC outloc, TRACE("Coordinates::Grad_par( Field3D )"); ASSERT1(location == outloc || outloc == CELL_DEFAULT) - return differential_operators->Grad_par(var, dy, outloc, method); + return differential_operators->Grad_par(var, dy(), outloc, method); } ///////////////////////////////////////////////////////// @@ -1002,7 +1005,7 @@ Field2D Coordinates::Div_par(const Field2D& f, CELL_LOC outloc, TRACE("Coordinates::Div_par( Field2D )"); ASSERT1(location == outloc || outloc == CELL_DEFAULT) - return differential_operators->Div_par(f, geometry.Bxy(), dy, outloc, method); + return differential_operators->Div_par(f, geometry.Bxy(), dy(), outloc, method); } Field3D Coordinates::Div_par(const Field3D& f, CELL_LOC outloc, @@ -1017,7 +1020,7 @@ Field3D Coordinates::Div_par(const Field3D& f, CELL_LOC outloc, if (!f.hasParallelSlices()) { // No yup/ydown fields. The Grad_par operator will // shift to field aligned coordinates - return differential_operators->Div_par(f, dy, geometry.Bxy(), outloc, method); + return differential_operators->Div_par(f, dy(), geometry.Bxy(), outloc, method); } // Need to modify yup and ydown fields @@ -1276,9 +1279,9 @@ Field2D Coordinates::Laplace_perpXY([[maybe_unused]] const Field2D& A, const BoutReal outer_x_A = outer_x_avg(A); const BoutReal outer_x_J = outer_x_avg(J()); const BoutReal outer_x_g11 = outer_x_avg(g11()); - const BoutReal outer_x_dx = outer_x_avg(dx); + const BoutReal outer_x_dx = outer_x_avg(dx()); const BoutReal outer_x_value = - outer_x_A * outer_x_J * outer_x_g11 / (J()[i] * outer_x_dx * dx[i]); + outer_x_A * outer_x_J * outer_x_g11 / (J()[i] * outer_x_dx * dx()[i]); result[i] += outer_x_value * (f[i.xp()] - f[i]); // inner x boundary @@ -1286,9 +1289,9 @@ Field2D Coordinates::Laplace_perpXY([[maybe_unused]] const Field2D& A, const BoutReal inner_x_A = inner_x_avg(A); const BoutReal inner_x_J = inner_x_avg(J()); const BoutReal inner_x_g11 = inner_x_avg(g11()); - const BoutReal inner_x_dx = inner_x_avg(dx); + const BoutReal inner_x_dx = inner_x_avg(dx()); const BoutReal inner_x_value = - inner_x_A * inner_x_J * inner_x_g11 / (J()[i] * inner_x_dx * dx[i]); + inner_x_A * inner_x_J * inner_x_g11 / (J()[i] * inner_x_dx * dx()[i]); result[i] += inner_x_value * (f[i.xm()] - f[i]); // upper y boundary @@ -1298,9 +1301,9 @@ Field2D Coordinates::Laplace_perpXY([[maybe_unused]] const Field2D& A, const BoutReal upper_y_g_22 = upper_y_avg(g_22()); const BoutReal upper_y_g23 = upper_y_avg(g23()); const BoutReal upper_y_g_23 = upper_y_avg(g_23()); - const BoutReal upper_y_dy = upper_y_avg(dy); + const BoutReal upper_y_dy = upper_y_avg(dy()); const BoutReal upper_y_value = -upper_y_A * upper_y_J * upper_y_g23 * upper_y_g_23 - / (upper_y_g_22 * J()[i] * upper_y_dy * dy[i]); + / (upper_y_g_22 * J()[i] * upper_y_dy * dy()[i]); result[i] += upper_y_value * (f[i.yp()] - f[i]); // lower y boundary @@ -1310,9 +1313,9 @@ Field2D Coordinates::Laplace_perpXY([[maybe_unused]] const Field2D& A, const BoutReal lower_y_g_22 = lower_y_avg(g_22()); const BoutReal lower_y_g23 = lower_y_avg(g23()); const BoutReal lower_y_g_23 = lower_y_avg(g_23()); - const BoutReal lower_y_dy = lower_y_avg(dy); + const BoutReal lower_y_dy = lower_y_avg(dy()); const BoutReal lower_y_value = -lower_y_A * lower_y_J * lower_y_g23 * lower_y_g_23 - / (lower_y_g_22 * J()[i] * lower_y_dy * dy[i]); + / (lower_y_g_22 * J()[i] * lower_y_dy * dy()[i]); result[i] += lower_y_value * (f[i.ym()] - f[i]); } @@ -1355,6 +1358,14 @@ void Coordinates::checkCovariant() { return geometry.checkCovariant(localmesh->y void Coordinates::checkContravariant() { geometry.checkContravariant(localmesh->ystart); } +const FieldMetric& Coordinates::dx() const { return dx_; } +const FieldMetric& Coordinates::dy() const { return dy_; } +const FieldMetric& Coordinates::dz() const { return dz_; } + +void Coordinates::setDx(FieldMetric dx) { dx_ = dx; } +void Coordinates::setDy(FieldMetric dy) { dy_ = dy; } +void Coordinates::setDz(FieldMetric dz) { dz_ = dz; } + void Coordinates::setContravariantMetricTensor(MetricTensor metric_tensor, const std::string& region, bool recalculate_staggered, diff --git a/src/mesh/coordinates_accessor.cxx b/src/mesh/coordinates_accessor.cxx index c1fb1d4c7a..b8e8124d09 100644 --- a/src/mesh/coordinates_accessor.cxx +++ b/src/mesh/coordinates_accessor.cxx @@ -17,7 +17,7 @@ CoordinatesAccessor::CoordinatesAccessor(const Coordinates* coords) { ASSERT0(coords != nullptr); // Size of the mesh in Z. Used to convert 3D -> 2D index - Mesh* mesh = coords->dx.getMesh(); + Mesh* mesh = coords->dx().getMesh(); mesh_nz = mesh->LocalNz; auto search = coords_store.find(coords); @@ -49,9 +49,11 @@ CoordinatesAccessor::CoordinatesAccessor(const Coordinates* coords) { // Iterate over all points in the field // Note this could be 2D or 3D, depending on FieldMetric type - for (const auto& ind : coords->dx.getRegion("RGN_ALL")) { - COPY_STRIPE(dx, dy, dz); - COPY_STRIPE(d1_dx, d1_dy, d1_dz); + for (const auto& ind : coords->dx().getRegion("RGN_ALL")) { + data[stripe_size * ind.ind + static_cast(Offset::dx)] = coords->dx()[ind]; + data[stripe_size * ind.ind + static_cast(Offset::dy)] = coords->dy()[ind]; + data[stripe_size * ind.ind + static_cast(Offset::dz)] = coords->dz()[ind]; + data[stripe_size * ind.ind + static_cast(Offset::J)] = coords->J()[ind]; data[stripe_size * ind.ind + static_cast(Offset::B)] = coords->Bxy()[ind]; diff --git a/src/mesh/geometry.cxx b/src/mesh/geometry.cxx index 88c1a1a6fa..8c2b93dc25 100644 --- a/src/mesh/geometry.cxx +++ b/src/mesh/geometry.cxx @@ -33,7 +33,7 @@ Geometry::Geometry(Mesh* mesh, DifferentialOperators* differential_operators) ASSERT0(differential_operators != nullptr); } -void Geometry::CalculateChristoffelSymbols(FieldMetric& dx, FieldMetric& dy) { +void Geometry::CalculateChristoffelSymbols(const FieldMetric& dx, const FieldMetric& dy) { // Calculate Christoffel symbol terms (18 independent values) // Note: This calculation is completely general: metric // tensor can be 2D or 3D. For 2D, all DDZ terms are zero From 294737b29c7040d897a4b7aae88477baf23ec5bd Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 6 Dec 2023 18:31:47 +0000 Subject: [PATCH 260/491] Make dx, dy, dz private in cython interface. --- tools/pylib/_boutpp_build/boutpp.pyx.jinja | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/pylib/_boutpp_build/boutpp.pyx.jinja b/tools/pylib/_boutpp_build/boutpp.pyx.jinja index 7bcdb9a104..b980965cb3 100644 --- a/tools/pylib/_boutpp_build/boutpp.pyx.jinja +++ b/tools/pylib/_boutpp_build/boutpp.pyx.jinja @@ -828,7 +828,7 @@ cdef class Coordinates: """ cdef c.Coordinates * cobj cdef c.bool isSelfOwned - cdef public {{ metric_field }} dx, dy, dz + cdef {{ metric_field }} dx, dy, dz cdef {{ metric_field }} J cdef {{ metric_field }} Bxy cdef {{ metric_field }} g11, g22, g33, g12, g13, g23 @@ -845,7 +845,7 @@ cdef class Coordinates: self.isSelfOwned = False def _setmembers(self): -{% for f in "dx", "dy", "dz", "ShiftTorsion", "IntShiftTorsion" %} +{% for f in "ShiftTorsion", "IntShiftTorsion" %} self.{{f}} = {{ metric_field.fdd }}FromPtr(&self.cobj.{{f}}) {% endfor %} From 84e108f16c47b99b210a7c5714e062c5dbd99214 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 6 Dec 2023 10:00:55 +0000 Subject: [PATCH 261/491] Update more tests and examples to use new dx, dy, dz getters and setters. --- examples/6field-simple/elm_6f.cxx | 6 +- examples/advdiff/advdiff.cxx | 6 +- examples/advdiff2/init.cxx | 6 +- examples/conducting-wall-mode/cwm.cxx | 4 +- examples/constraints/alfven-wave/alfven.cxx | 4 +- examples/dalf3/dalf3.cxx | 2 +- .../elm-pb-outerloop/elm_pb_outerloop.cxx | 4 +- examples/elm-pb/elm_pb.cxx | 4 +- examples/em-drift/2fluid.cxx | 4 +- examples/fci-wave-logn/fci-wave.cxx | 2 +- examples/fci-wave/fci-wave.cxx | 2 +- examples/gyro-gem/gem.cxx | 2 +- examples/lapd-drift/lapd_drift.cxx | 26 +- examples/laplace-petsc3d/test-laplace3d.cxx | 2 +- examples/laplacexy/alfven-wave/alfven.cxx | 4 +- examples/reconnect-2field/2field.cxx | 2 +- examples/shear-alfven-wave/2fluid.cxx | 4 +- examples/tokamak-2fluid/2fluid.cxx | 4 +- examples/uedge-benchmark/ue_bmark.cxx | 6 +- include/bout/fv_ops.hxx | 59 ++--- include/bout/physicsmodel.hxx | 4 + .../sphinx/developer_docs/petsc_interface.rst | 4 +- .../laplace/impls/cyclic/cyclic_laplace.cxx | 2 +- .../laplace/impls/hypre3d/hypre3d_laplace.cxx | 44 ++-- .../impls/multigrid/multigrid_laplace.cxx | 18 +- src/invert/laplace/impls/pcr/pcr.cxx | 4 +- .../laplace/impls/pcr_thomas/pcr_thomas.cxx | 4 +- .../laplace/impls/petsc3damg/petsc3damg.cxx | 36 +-- .../laplace/impls/serial_band/serial_band.cxx | 44 ++-- src/invert/laplace/invert_laplace.cxx | 48 ++-- src/invert/laplacexy/laplacexy.cxx | 8 +- src/invert/laplacexy2/laplacexy2.cxx | 16 +- src/invert/laplacexy2/laplacexy2_hypre.cxx | 16 +- .../impls/cyclic/laplacexz-cyclic.cxx | 8 +- src/invert/parderiv/impls/cyclic/cyclic.cxx | 6 +- .../pardiv/impls/cyclic/pardiv_cyclic.cxx | 2 +- src/mesh/boundary_standard.cxx | 237 +++++++++--------- src/mesh/difops.cxx | 166 ++++++------ src/mesh/fv_ops.cxx | 102 ++++---- src/mesh/parallel_boundary_op.cxx | 10 +- src/physics/smoothing.cxx | 2 +- src/sys/derivs.cxx | 62 ++--- tests/MMS/GBS/gbs.cxx | 29 +-- tests/MMS/advection/advection.cxx | 4 +- tests/MMS/diffusion/diffusion.cxx | 4 +- tests/MMS/diffusion2/diffusion.cxx | 6 +- tests/MMS/elm-pb/elm_pb.cxx | 6 +- tests/MMS/fieldalign/fieldalign.cxx | 4 +- tests/MMS/hw/hw.cxx | 8 +- tests/MMS/laplace/laplace.cxx | 4 +- tests/MMS/spatial/diffusion/diffusion.cxx | 17 +- tests/MMS/tokamak/tokamak.cxx | 10 +- tests/MMS/wave-1d/wave.cxx | 8 +- .../test-drift-instability/2fluid.cxx | 4 +- .../test-interchange-instability/2fluid.cxx | 4 +- .../integrated/test-laplacexy/loadmetric.cxx | 6 +- .../test_multigrid_laplace.cxx | 4 +- .../test_naulin_laplace.cxx | 4 +- tests/integrated/test-snb/test_snb.cxx | 2 +- .../invert/laplace/test_laplace_cyclic.cxx | 4 +- .../invert/laplace/test_laplace_hypre3d.cxx | 8 +- .../laplace/test_laplace_petsc3damg.cxx | 8 +- tests/unit/mesh/test_coordinates.cxx | 18 +- tests/unit/mesh/test_coordinates_accessor.cxx | 2 +- tools/pylib/_boutpp_build/boutpp.pyx.jinja | 2 +- tools/pylib/_boutpp_build/helper.cxx.jinja | 6 +- 66 files changed, 585 insertions(+), 583 deletions(-) diff --git a/examples/6field-simple/elm_6f.cxx b/examples/6field-simple/elm_6f.cxx index 6b01a0ae69..2c515f89ce 100644 --- a/examples/6field-simple/elm_6f.cxx +++ b/examples/6field-simple/elm_6f.cxx @@ -361,7 +361,7 @@ class Elm_6f : public PhysicsModel { result.allocate(); for (auto i : result) { result[i] = - (fp[i.yp()] - fm[i.ym()]) / (2. * coord->dy[i] * sqrt(coord->g_22[i])); + (fp[i.yp()] - fm[i.ym()]) / (2. * coord->dy()[i] * sqrt(coord->g_22[i])); } } else { result = Grad_par(f, loc); @@ -867,7 +867,7 @@ class Elm_6f : public PhysicsModel { Btxy /= Bbar; B0 /= Bbar; hthe /= Lbar; - coord->dx /= Lbar * Lbar * Bbar; + coord->setDx(coord->dx() / (Lbar * Lbar * Bbar)); I *= Lbar * Lbar * Bbar; if ((!T0_fake_prof) && n0_fake_prof) { @@ -1448,7 +1448,7 @@ class Elm_6f : public PhysicsModel { if (hyperviscos > 0.0) { // Calculate coefficient. - hyper_mu_x = hyperviscos * coord->g_11 * SQ(coord->dx) + hyper_mu_x = hyperviscos * coord->g_11 * SQ(coord->dx()) * abs(coord->g11 * D2DX2(U)) / (abs(U) + 1e-3); hyper_mu_x.applyBoundary("dirichlet"); // Set to zero on all boundaries diff --git a/examples/advdiff/advdiff.cxx b/examples/advdiff/advdiff.cxx index 2e7d3748fc..0d36f0671f 100644 --- a/examples/advdiff/advdiff.cxx +++ b/examples/advdiff/advdiff.cxx @@ -22,13 +22,13 @@ class AdvDiff : public PhysicsModel { Coordinates* coord = mesh->getCoordinates(); mesh->get(V0, "V0"); - mesh->get(coord->dx, "dx"); - mesh->get(coord->dy, "dy"); + coord->setDx(mesh->get("dx")); + coord->setDy(mesh->get("dy")); // read options // Set evolving variables - SOLVE_FOR(V); + SOLVE_FOR(V) if (!restarting) { // Set variables to these values (+ the initial perturbation) diff --git a/examples/advdiff2/init.cxx b/examples/advdiff2/init.cxx index 7954f7b543..5dd0a1d6ba 100644 --- a/examples/advdiff2/init.cxx +++ b/examples/advdiff2/init.cxx @@ -9,13 +9,13 @@ int AdvDiff::init(bool restarting) { // Read initial conditions mesh->get(V0, "V0"); - mesh->get(mesh->getCoordinates()->dx, "dx"); - mesh->get(mesh->getCoordinates()->dy, "dy"); + mesh->getCoordinates()->setDx(mesh->get("dx")); + mesh->getCoordinates()->setDy(mesh->get("dy")); // read options // Set evolving variables - SOLVE_FOR(V); + SOLVE_FOR(V) if (!restarting) { // Set variables to these values (+ the initial perturbation) diff --git a/examples/conducting-wall-mode/cwm.cxx b/examples/conducting-wall-mode/cwm.cxx index 485d78252a..4f48f2c6c8 100644 --- a/examples/conducting-wall-mode/cwm.cxx +++ b/examples/conducting-wall-mode/cwm.cxx @@ -89,7 +89,7 @@ class CWM : public PhysicsModel { mesh->get(Bpxy, "Bpxy"); mesh->get(Btxy, "Btxy"); mesh->get(hthe, "hthe"); - mesh->get(coord->dx, "dpsi"); + coord->setDx(mesh->get("dpsi")); mesh->get(I, "sinty"); // Load normalisation values @@ -176,7 +176,7 @@ class CWM : public PhysicsModel { Rxy /= rho_s; hthe /= rho_s; I *= rho_s * rho_s * (bmag / 1e4) * ShearFactor; - coord->dx /= rho_s * rho_s * (bmag / 1e4); + coord->setDx(coord->dx() / (rho_s * rho_s * (bmag / 1e4))); // Normalise magnetic field Bpxy /= (bmag / 1.e4); diff --git a/examples/constraints/alfven-wave/alfven.cxx b/examples/constraints/alfven-wave/alfven.cxx index f224d3d6b2..928bc3a1ac 100644 --- a/examples/constraints/alfven-wave/alfven.cxx +++ b/examples/constraints/alfven-wave/alfven.cxx @@ -170,7 +170,7 @@ class Alfven : public PhysicsModel { Field2D dx; if (!mesh->get(dx, "dpsi")) { output << "\tUsing dpsi as the x grid spacing\n"; - coord->dx = dx; // Only use dpsi if found + coord->dx() = dx; // Only use dpsi if found } else { // dx will have been read already from the grid output << "\tUsing dx as the x grid spacing\n"; @@ -179,7 +179,7 @@ class Alfven : public PhysicsModel { Rxy /= Lnorm; hthe /= Lnorm; sinty *= SQ(Lnorm) * Bnorm; - coord->dx /= SQ(Lnorm) * Bnorm; + coord->setDx(coord->dx() / (SQ(Lnorm) * Bnorm)); Bpxy /= Bnorm; Btxy /= Bnorm; diff --git a/examples/dalf3/dalf3.cxx b/examples/dalf3/dalf3.cxx index 96102ee6f2..2869404907 100644 --- a/examples/dalf3/dalf3.cxx +++ b/examples/dalf3/dalf3.cxx @@ -248,7 +248,7 @@ class DALF3 : public PhysicsModel { Btxy /= Bnorm; B0 /= Bnorm; - coord->dx /= rho_s * rho_s * Bnorm; + coord->setDx(coord->dx() / (rho_s * rho_s * Bnorm)); /////////////////////////////////////////////////// // CALCULATE METRICS diff --git a/examples/elm-pb-outerloop/elm_pb_outerloop.cxx b/examples/elm-pb-outerloop/elm_pb_outerloop.cxx index 7baa422b99..caa63c710f 100644 --- a/examples/elm-pb-outerloop/elm_pb_outerloop.cxx +++ b/examples/elm-pb-outerloop/elm_pb_outerloop.cxx @@ -962,7 +962,7 @@ class ELMpb : public PhysicsModel { Btxy /= Bbar; B0 /= Bbar; hthe /= Lbar; - metric->dx /= Lbar * Lbar * Bbar; + metric->setDx(metric->dx() / (Lbar * Lbar * Bbar)); I *= Lbar * Lbar * Bbar; if (constn0) { @@ -1790,7 +1790,7 @@ class ELMpb : public PhysicsModel { if (hyperviscos > 0.0) { // Calculate coefficient. - hyper_mu_x = hyperviscos * metric->g_11 * SQ(metric->dx) + hyper_mu_x = hyperviscos * metric->g_11 * SQ(metric->dx()) * abs(metric->g11 * D2DX2(U)) / (abs(U) + 1e-3); hyper_mu_x.applyBoundary("dirichlet"); // Set to zero on all boundaries diff --git a/examples/elm-pb/elm_pb.cxx b/examples/elm-pb/elm_pb.cxx index 8ca2fb1cee..df54764676 100644 --- a/examples/elm-pb/elm_pb.cxx +++ b/examples/elm-pb/elm_pb.cxx @@ -903,7 +903,7 @@ class ELMpb : public PhysicsModel { Btxy /= Bbar; B0 /= Bbar; hthe /= Lbar; - metric->dx /= Lbar * Lbar * Bbar; + metric->setDx(metric->dx() / (Lbar * Lbar * Bbar)); I *= Lbar * Lbar * Bbar; if (constn0) { @@ -1589,7 +1589,7 @@ class ELMpb : public PhysicsModel { if (hyperviscos > 0.0) { // Calculate coefficient. - hyper_mu_x = hyperviscos * metric->g_11 * SQ(metric->dx) + hyper_mu_x = hyperviscos * metric->g_11 * SQ(metric->dx()) * abs(metric->g11 * D2DX2(U)) / (abs(U) + 1e-3); hyper_mu_x.applyBoundary("dirichlet"); // Set to zero on all boundaries diff --git a/examples/em-drift/2fluid.cxx b/examples/em-drift/2fluid.cxx index 5061b6c106..5443b9c22c 100644 --- a/examples/em-drift/2fluid.cxx +++ b/examples/em-drift/2fluid.cxx @@ -72,7 +72,7 @@ class EMdrift : public PhysicsModel { mesh->get(Bpxy, "Bpxy"); mesh->get(Btxy, "Btxy"); mesh->get(hthe, "hthe"); - mesh->get(coord->dx, "dpsi"); + coord->setDx(mesh->get("dpsi")); mesh->get(I, "sinty"); // Load normalisation values @@ -169,7 +169,7 @@ class EMdrift : public PhysicsModel { Rxy /= rho_s; hthe /= rho_s; I *= rho_s * rho_s * (bmag / 1e4) * ShearFactor; - coord->dx /= rho_s * rho_s * (bmag / 1e4); + coord->setDx(coord->dx() / (rho_s * rho_s * (bmag / 1e4))); // Normalise magnetic field Bpxy /= (bmag / 1.e4); diff --git a/examples/fci-wave-logn/fci-wave.cxx b/examples/fci-wave-logn/fci-wave.cxx index 731897ad4e..6aa85c898d 100644 --- a/examples/fci-wave-logn/fci-wave.cxx +++ b/examples/fci-wave-logn/fci-wave.cxx @@ -43,7 +43,7 @@ class FCIwave : public PhysicsModel { for (auto i : result.getRegion(RGN_NOBNDRY)) { result[i] = Bxyz[i] * (f_B.yup()[i.yp()] - f_B.ydown()[i.ym()]) - / (2. * coord->dy[i] * sqrt(coord->g_22[i])); + / (2. * coord->dy()[i] * sqrt(coord->g_22[i])); if (!finite(result[i])) { output.write("[{:d},{:d},{:d}]: {:e}, {:e} -> {:e}\n", i.x(), i.y(), i.z(), diff --git a/examples/fci-wave/fci-wave.cxx b/examples/fci-wave/fci-wave.cxx index 226b52c808..697e39f77c 100644 --- a/examples/fci-wave/fci-wave.cxx +++ b/examples/fci-wave/fci-wave.cxx @@ -44,7 +44,7 @@ class FCIwave : public PhysicsModel { for (auto i : result.getRegion(RGN_NOBNDRY)) { result[i] = Bxyz[i] * (f_B.yup()[i.yp()] - f_B.ydown()[i.ym()]) - / (2. * coord->dy[i] * sqrt(coord->g_22[i])); + / (2. * coord->dy()[i] * sqrt(coord->g_22[i])); if (!finite(result[i])) { output.write("[{:d},{:d},{:d}]: {:e}, {:e} -> {:e}\n", i.x(), i.y(), i.z(), diff --git a/examples/gyro-gem/gem.cxx b/examples/gyro-gem/gem.cxx index 686ff5f01e..d489d693b1 100644 --- a/examples/gyro-gem/gem.cxx +++ b/examples/gyro-gem/gem.cxx @@ -365,7 +365,7 @@ class GEM : public PhysicsModel { Bxy /= Bbar; Rxy /= rho_s; // Perpendicular derivatives normalised to rho_s - coord->dx /= rho_s * rho_s * Bbar; + coord->setDx(coord->dx() / (rho_s * rho_s * Bbar)); // Metric components diff --git a/examples/lapd-drift/lapd_drift.cxx b/examples/lapd-drift/lapd_drift.cxx index 7362bb3b5d..cfe1fc9a05 100644 --- a/examples/lapd-drift/lapd_drift.cxx +++ b/examples/lapd-drift/lapd_drift.cxx @@ -142,7 +142,7 @@ class LAPDdrift : public PhysicsModel { mesh->get(Bpxy, "Bpxy"); mesh->get(Btxy, "Btxy"); mesh->get(hthe, "hthe"); - mesh->get(coord->dx, "dpsi"); + coord->setDx(mesh->get("dpsi")); mesh->get(I, "sinty"); // Load normalisation values @@ -319,7 +319,7 @@ class LAPDdrift : public PhysicsModel { Rxy /= rho_s; hthe /= rho_s; I *= rho_s * rho_s * (bmag / 1e4) * ShearFactor; - coord->dx /= rho_s * rho_s * (bmag / 1e4); + coord->setDx(coord->dx() / (rho_s * rho_s * (bmag / 1e4))); // Normalise magnetic field Bpxy /= (bmag / 1.e4); @@ -416,7 +416,8 @@ class LAPDdrift : public PhysicsModel { SAVE_ONCE(Ni0, Te0, phi0, rho0); SAVE_ONCE(Rxy, Bpxy, Btxy, Zxy, hthe); - dump.addOnce(coord->Bxy(), "Bxy"); + const FieldMetric tmp = coord->Bxy(); + dump.addOnce(tmp, "Bxy"); dump.addOnce(my_ixseps, "ixseps"); SAVE_ONCE(Te_x, Ti_x, Ni_x); @@ -503,7 +504,8 @@ class LAPDdrift : public PhysicsModel { // Calculate E cross B velocity if (nonlinear) { - VEt = sqrt(coord->g11 * DDX(phit) * DDX(phit) + coord->g33 * DDZ(phit) * DDZ(phit)); + VEt = sqrt(coord->g11() * DDX(phit) * DDX(phit) + + coord->g33() * DDZ(phit) * DDZ(phit)); // Set boundary condition on VEt VEt.applyBoundary(); @@ -608,7 +610,7 @@ class LAPDdrift : public PhysicsModel { } if (rho_ve2lin) { - ddt(rho) -= coord->g11 * coord->g33 * DDX(phi0) + ddt(rho) -= coord->g11() * coord->g33() * DDX(phi0) * (DDX(Ni0) * D2DXDZ(phi) - D2DX2(phi0) * DDZ(ni)); } @@ -733,7 +735,7 @@ class LAPDdrift : public PhysicsModel { /****************SPECIAL DIFFERENTIAL OPERATORS******************/ Coordinates::FieldMetric Perp_Grad_dot_Grad(const Field2D& p, const Field2D& f) { - return DDX(p) * DDX(f) * mesh->getCoordinates()->g11; + return DDX(p) * DDX(f) * mesh->getCoordinates()->g11(); } ///////////////////////////////////////////////////////////////// @@ -773,7 +775,7 @@ class LAPDdrift : public PhysicsModel { 0.25 * ((p(jx, jy, jzp) - p(jx, jy, jzm)) * (f(jx + 1, jy) - f(jx - 1, jy)) - (p(jx + 1, jy, jz) - p(jx - 1, jy, jz)) * (f(jx, jy) - f(jx, jy))) - / (coord->dx(jx, jy, jz) * coord->dz(jx, jy, jz)); + / (coord->dx()(jx, jy, jz) * coord->dz()(jx, jy, jz)); // J+x BoutReal Jpx = 0.25 @@ -781,14 +783,14 @@ class LAPDdrift : public PhysicsModel { - f(jx - 1, jy) * (p(jx - 1, jy, jzp) - p(jx - 1, jy, jzm)) - f(jx, jy) * (p(jx + 1, jy, jzp) - p(jx - 1, jy, jzp)) + f(jx, jy) * (p(jx + 1, jy, jzm) - p(jx - 1, jy, jzm))) - / (coord->dx(jx, jy, jz) * coord->dz(jx, jy, jz)); + / (coord->dx()(jx, jy, jz) * coord->dz()(jx, jy, jz)); // Jx+ BoutReal Jxp = 0.25 * (f(jx + 1, jy) * (p(jx, jy, jzp) - p(jx + 1, jy, jz)) - f(jx - 1, jy) * (p(jx - 1, jy, jz) - p(jx, jy, jzm)) - f(jx - 1, jy) * (p(jx, jy, jzp) - p(jx - 1, jy, jz)) + f(jx + 1, jy) * (p(jx + 1, jy, jz) - p(jx, jy, jzm))) - / (coord->dx(jx, jy, jz) * coord->dz(jx, jy, jz)); + / (coord->dx()(jx, jy, jz) * coord->dz()(jx, jy, jz)); result(jx, jy, jz) = (Jpp + Jpx + Jxp) / 3.; } @@ -839,7 +841,7 @@ class LAPDdrift : public PhysicsModel { * (f(jx + 1, jy, jz) - f(jx - 1, jy, jz)) - (p(jx + 1, jy, jz) - p(jx - 1, jy, jz)) * (f(jx, jy, jzp) - f(jx, jy, jzm))) - / (coord->dx(jx, jy, jz) * coord->dz(jx, jy, jz)); + / (coord->dx()(jx, jy, jz) * coord->dz()(jx, jy, jz)); // J+x BoutReal Jpx = @@ -848,14 +850,14 @@ class LAPDdrift : public PhysicsModel { - f(jx - 1, jy, jz) * (p(jx - 1, jy, jzp) - p(jx - 1, jy, jzm)) - f(jx, jy, jzp) * (p(jx + 1, jy, jzp) - p(jx - 1, jy, jzp)) + f(jx, jy, jzm) * (p(jx + 1, jy, jzm) - p(jx - 1, jy, jzm))) - / (coord->dx(jx, jy, jz) * coord->dz(jx, jy, jz)); + / (coord->dx()(jx, jy, jz) * coord->dz()(jx, jy, jz)); // Jx+ BoutReal Jxp = 0.25 * (f(jx + 1, jy, jzp) * (p(jx, jy, jzp) - p(jx + 1, jy, jz)) - f(jx - 1, jy, jzm) * (p(jx - 1, jy, jz) - p(jx, jy, jzm)) - f(jx - 1, jy, jzp) * (p(jx, jy, jzp) - p(jx - 1, jy, jz)) + f(jx + 1, jy, jzm) * (p(jx + 1, jy, jz) - p(jx, jy, jzm))) - / (coord->dx(jx, jy, jz) * coord->dz(jx, jy, jz)); + / (coord->dx()(jx, jy, jz) * coord->dz()(jx, jy, jz)); result(jx, jy, jz) = (Jpp + Jpx + Jxp) / 3.; } diff --git a/examples/laplace-petsc3d/test-laplace3d.cxx b/examples/laplace-petsc3d/test-laplace3d.cxx index 46bfce7859..9097109ed2 100644 --- a/examples/laplace-petsc3d/test-laplace3d.cxx +++ b/examples/laplace-petsc3d/test-laplace3d.cxx @@ -41,7 +41,7 @@ Field3D this_Laplace_perp(const Field3D& f) { + coords->G3 * DDZ(f) + coords->g11 * D2DX2(f) + (coords->g22 - 1. / coords->g_22) * D2DY2(f) + coords->g33 * D2DZ2(f) + 2. - * (coords->g12 * DDX(dfdy) / coords->dy + coords->g13 * D2DXDZ(f) + * (coords->g12 * DDX(dfdy) / coords->dy() + coords->g13 * D2DXDZ(f) + coords->g23 * D2DYDZ(f)); } diff --git a/examples/laplacexy/alfven-wave/alfven.cxx b/examples/laplacexy/alfven-wave/alfven.cxx index 67ca1c3bc6..799a467ed1 100644 --- a/examples/laplacexy/alfven-wave/alfven.cxx +++ b/examples/laplacexy/alfven-wave/alfven.cxx @@ -179,7 +179,7 @@ class Alfven : public PhysicsModel { Field2D dx; if (!mesh->get(dx, "dpsi")) { output << "\tUsing dpsi as the x grid spacing\n"; - coord->dx = dx; // Only use dpsi if found + coord->dx() = dx; // Only use dpsi if found } else { // dx will have been read already from the grid output << "\tUsing dx as the x grid spacing\n"; @@ -188,7 +188,7 @@ class Alfven : public PhysicsModel { Rxy /= Lnorm; hthe /= Lnorm; sinty *= SQ(Lnorm) * Bnorm; - coord->dx /= SQ(Lnorm) * Bnorm; + coord->setDx(coord->dx() / (SQ(Lnorm) * Bnorm)); Bpxy /= Bnorm; Btxy /= Bnorm; diff --git a/examples/reconnect-2field/2field.cxx b/examples/reconnect-2field/2field.cxx index 38c645c663..25788397e0 100644 --- a/examples/reconnect-2field/2field.cxx +++ b/examples/reconnect-2field/2field.cxx @@ -151,7 +151,7 @@ class TwoField : public PhysicsModel { // Normalise geometry Rxy /= rho_s; hthe /= rho_s; - coord->dx /= rho_s * rho_s * Bnorm; + coord->setDx(coord->dx() / (rho_s * rho_s * Bnorm)); // Normalise magnetic field Bpxy /= Bnorm; diff --git a/examples/shear-alfven-wave/2fluid.cxx b/examples/shear-alfven-wave/2fluid.cxx index a0a1997611..b3faa9cf50 100644 --- a/examples/shear-alfven-wave/2fluid.cxx +++ b/examples/shear-alfven-wave/2fluid.cxx @@ -83,7 +83,7 @@ class ShearAlfven : public PhysicsModel { GRID_LOAD(Btxy); GRID_LOAD(hthe); mesh->get(coord->Bxy(), "Bxy"); - mesh->get(coord->dx, "dpsi"); + coord->setDx(mesh->get("dpsi")); mesh->get(I, "sinty"); // Load normalisation values @@ -169,7 +169,7 @@ class ShearAlfven : public PhysicsModel { Rxy /= rho_s; hthe /= rho_s; I *= rho_s * rho_s * (bmag / 1e4) * ShearFactor; - coord->dx /= rho_s * rho_s * (bmag / 1e4); + coord->setDx(coord->dx() / (rho_s * rho_s * (bmag / 1e4))); // Normalise magnetic field Bpxy /= (bmag / 1.e4); diff --git a/examples/tokamak-2fluid/2fluid.cxx b/examples/tokamak-2fluid/2fluid.cxx index aa9c7ee398..6d86b82f60 100644 --- a/examples/tokamak-2fluid/2fluid.cxx +++ b/examples/tokamak-2fluid/2fluid.cxx @@ -138,7 +138,7 @@ class TwoFluid : public PhysicsModel { Field2D dx; if (!mesh->get(dx, "dpsi")) { output << "\tUsing dpsi as the x grid spacing\n"; - coord->dx = dx; // Only use dpsi if found + coord->dx() = dx; // Only use dpsi if found } else { // dx will have been read already from the grid output << "\tUsing dx as the x grid spacing\n"; @@ -385,7 +385,7 @@ class TwoFluid : public PhysicsModel { Rxy /= rho_s; hthe /= rho_s; I *= rho_s * rho_s * (bmag / 1e4) * ShearFactor; - coord->dx /= rho_s * rho_s * (bmag / 1e4); + coord->setDx(coord->dx() / (rho_s * rho_s * (bmag / 1e4))); // Normalise magnetic field Bpxy /= (bmag / 1.e4); diff --git a/examples/uedge-benchmark/ue_bmark.cxx b/examples/uedge-benchmark/ue_bmark.cxx index a52b149194..6986968c19 100644 --- a/examples/uedge-benchmark/ue_bmark.cxx +++ b/examples/uedge-benchmark/ue_bmark.cxx @@ -59,7 +59,7 @@ class UedgeBenchmark : public PhysicsModel { GRID_LOAD(Rxy); // Major radius [m] GRID_LOAD(Bpxy, Btxy); // Poloidal, Toroidal B field [T] GRID_LOAD(hthe); // Poloidal arc length [m / radian] - mesh->get(mesh->getCoordinates()->dx, "dpsi"); + mesh->getCoordinates()->setDx(mesh->get("dpsi")); // Load normalisation values GRID_LOAD(Te_x, Ti_x, Ni_x, bmag); @@ -115,12 +115,12 @@ class UedgeBenchmark : public PhysicsModel { // Normalise geometry Rxy /= rho_s; hthe /= rho_s; - coords->dx /= rho_s * rho_s * (bmag / 1e4); + coords->setDx(coords->dx() / (rho_s * rho_s * (bmag / 1e4))); // Normalise magnetic field Bpxy /= (bmag / 1e4); Btxy /= (bmag / 1e4); - coords->Bxy() /= (bmag / 1e4); + coords->setBxy(coords->Bxy() / (bmag / 1e4)); // calculate pressures pei0 = (Ti0 + Te0) * Ni0; diff --git a/include/bout/fv_ops.hxx b/include/bout/fv_ops.hxx index ed18b62c68..a8456ee82b 100644 --- a/include/bout/fv_ops.hxx +++ b/include/bout/fv_ops.hxx @@ -192,15 +192,15 @@ template const Field3D Div_par(const Field3D& f_in, const Field3D& v_in, const Field3D& wave_speed_in, bool fixflux = true) { - ASSERT1_FIELDS_COMPATIBLE(f_in, v_in); - ASSERT1_FIELDS_COMPATIBLE(f_in, wave_speed_in); + ASSERT1_FIELDS_COMPATIBLE(f_in, v_in) + ASSERT1_FIELDS_COMPATIBLE(f_in, wave_speed_in) Mesh* mesh = f_in.getMesh(); CellEdges cellboundary; - ASSERT2(f_in.getDirectionY() == v_in.getDirectionY()); - ASSERT2(f_in.getDirectionY() == wave_speed_in.getDirectionY()); + ASSERT2(f_in.getDirectionY() == v_in.getDirectionY()) + ASSERT2(f_in.getDirectionY() == wave_speed_in.getDirectionY()) const bool are_unaligned = ((f_in.getDirectionY() == YDirectionType::Standard) and (v_in.getDirectionY() == YDirectionType::Standard) @@ -243,20 +243,21 @@ const Field3D Div_par(const Field3D& f_in, const Field3D& v_in, // Pre-calculate factors which multiply fluxes #if not(BOUT_USE_METRIC_3D) // For right cell boundaries - BoutReal common_factor = (coord->J()(i, j) + coord->J()(i, j + 1)) - / (sqrt(coord->g_22()(i, j)) + sqrt(coord->g_22()(i, j + 1))); + BoutReal common_factor = + (coord->J()(i, j) + coord->J()(i, j + 1)) + / (sqrt(coord->g_22()(i, j)) + sqrt(coord->g_22()(i, j + 1))); - BoutReal flux_factor_rc = common_factor / (coord->dy(i, j) * coord->J()(i, j)); + BoutReal flux_factor_rc = common_factor / (coord->dy()(i, j) * coord->J()(i, j)); BoutReal flux_factor_rp = - common_factor / (coord->dy(i, j + 1) * coord->J()(i, j + 1)); + common_factor / (coord->dy()(i, j + 1) * coord->J()(i, j + 1)); // For left cell boundaries common_factor = (coord->J()(i, j) + coord->J()(i, j - 1)) / (sqrt(coord->g_22()(i, j)) + sqrt(coord->g_22()(i, j - 1))); - BoutReal flux_factor_lc = common_factor / (coord->dy(i, j) * coord->J()(i, j)); + BoutReal flux_factor_lc = common_factor / (coord->dy()(i, j) * coord->J()(i, j)); BoutReal flux_factor_lm = - common_factor / (coord->dy(i, j - 1) * coord->J()(i, j - 1)); + common_factor / (coord->dy()(i, j - 1) * coord->J()(i, j - 1)); #endif for (int k = 0; k < mesh->LocalNz; k++) { #if BOUT_USE_METRIC_3D @@ -266,18 +267,18 @@ const Field3D Div_par(const Field3D& f_in, const Field3D& v_in, / (sqrt(coord->g_22(i, j, k)) + sqrt(coord->g_22(i, j + 1, k))); BoutReal flux_factor_rc = - common_factor / (coord->dy(i, j, k) * coord->J(i, j, k)); + common_factor / (coord->dy()(i, j, k) * coord->J(i, j, k)); BoutReal flux_factor_rp = - common_factor / (coord->dy(i, j + 1, k) * coord->J(i, j + 1, k)); + common_factor / (coord->dy()(i, j + 1, k) * coord->J(i, j + 1, k)); // For left cell boundaries common_factor = (coord->J(i, j, k) + coord->J(i, j - 1, k)) / (sqrt(coord->g_22(i, j, k)) + sqrt(coord->g_22(i, j - 1, k))); BoutReal flux_factor_lc = - common_factor / (coord->dy(i, j, k) * coord->J(i, j, k)); + common_factor / (coord->dy()(i, j, k) * coord->J(i, j, k)); BoutReal flux_factor_lm = - common_factor / (coord->dy(i, j - 1, k) * coord->J(i, j - 1, k)); + common_factor / (coord->dy()(i, j - 1, k) * coord->J(i, j - 1, k)); #endif //////////////////////////////////////////// @@ -384,8 +385,8 @@ const Field3D Div_par(const Field3D& f_in, const Field3D& v_in, */ template const Field3D Div_f_v(const Field3D& n_in, const Vector3D& v, bool bndry_flux) { - ASSERT1(n_in.getLocation() == v.getLocation()); - ASSERT1_FIELDS_COMPATIBLE(n_in, v.x); + ASSERT1(n_in.getLocation() == v.getLocation()) + ASSERT1_FIELDS_COMPATIBLE(n_in, v.x) Mesh* mesh = n_in.getMesh(); @@ -432,16 +433,16 @@ const Field3D Div_f_v(const Field3D& n_in, const Vector3D& v, bool bndry_flux) { // Flux in from boundary flux = vR * 0.5 * (n[i.xp()] + n[i]); } - result[i] += flux / (coord->dx[i] * coord->J()[i]); - result[i.xp()] -= flux / (coord->dx[i.xp()] * coord->J()[i.xp()]); + result[i] += flux / (coord->dx()[i] * coord->J()[i]); + result[i.xp()] -= flux / (coord->dx()[i.xp()] * coord->J()[i.xp()]); } } else { // Not at a boundary if (vR > 0.0) { // Flux out into next cell BoutReal flux = vR * s.R; - result[i] += flux / (coord->dx[i] * coord->J()[i]); - result[i.xp()] -= flux / (coord->dx[i.xp()] * coord->J()[i.xp()]); + result[i] += flux / (coord->dx()[i] * coord->J()[i]); + result[i.xp()] -= flux / (coord->dx()[i.xp()] * coord->J()[i.xp()]); } } @@ -459,15 +460,15 @@ const Field3D Div_f_v(const Field3D& n_in, const Vector3D& v, bool bndry_flux) { // Flux in from boundary flux = vL * 0.5 * (n[i.xm()] + n[i]); } - result[i] -= flux / (coord->dx[i] * coord->J()[i]); - result[i.xm()] += flux / (coord->dx[i.xm()] * coord->J()[i.xm()]); + result[i] -= flux / (coord->dx()[i] * coord->J()[i]); + result[i.xm()] += flux / (coord->dx()[i.xm()] * coord->J()[i.xm()]); } } else { // Not at a boundary if (vL < 0.0) { BoutReal flux = vL * s.L; - result[i] -= flux / (coord->dx[i] * coord->J()[i]); - result[i.xm()] += flux / (coord->dx[i.xm()] * coord->J()[i.xm()]); + result[i] -= flux / (coord->dx()[i] * coord->J()[i]); + result[i.xm()] += flux / (coord->dx()[i.xm()] * coord->J()[i.xm()]); } } @@ -483,13 +484,13 @@ const Field3D Div_f_v(const Field3D& n_in, const Vector3D& v, bool bndry_flux) { if (vU > 0.0) { BoutReal flux = vU * s.R; - result[i] += flux / (coord->J()[i] * coord->dz[i]); - result[i.zp()] -= flux / (coord->J()[i.zp()] * coord->dz[i.zp()]); + result[i] += flux / (coord->J()[i] * coord->dz()[i]); + result[i.zp()] -= flux / (coord->J()[i.zp()] * coord->dz()[i.zp()]); } if (vD < 0.0) { BoutReal flux = vD * s.L; - result[i] -= flux / (coord->J()[i] * coord->dz[i]); - result[i.zm()] += flux / (coord->J()[i.zm()] * coord->dz[i.zm()]); + result[i] -= flux / (coord->J()[i] * coord->dz()[i]); + result[i.zm()] += flux / (coord->J()[i.zm()] * coord->dz()[i.zm()]); } } @@ -515,7 +516,7 @@ const Field3D Div_f_v(const Field3D& n_in, const Vector3D& v, bool bndry_flux) { BoutReal nU = 0.5 * (n[i] + n[i.yp()]); BoutReal nD = 0.5 * (n[i] + n[i.ym()]); - yresult[i] = (nU * vU - nD * vD) / (coord->J()[i] * coord->dy[i]); + yresult[i] = (nU * vU - nD * vD) / (coord->J()[i] * coord->dy()[i]); } return result + fromFieldAligned(yresult, "RGN_NOBNDRY"); } diff --git a/include/bout/physicsmodel.hxx b/include/bout/physicsmodel.hxx index 9fa25d8b0f..2b2badf0f3 100644 --- a/include/bout/physicsmodel.hxx +++ b/include/bout/physicsmodel.hxx @@ -78,6 +78,10 @@ public: add(&value, name, false); } template + void addOnce(const T& value, const std::string& name) { + add(&value, name, false); + } + template void add(T& value, const std::string& name, bool save_repeat = false) { add(&value, name, save_repeat); } diff --git a/manual/sphinx/developer_docs/petsc_interface.rst b/manual/sphinx/developer_docs/petsc_interface.rst index 9dba044c9b..d9805f9c6b 100644 --- a/manual/sphinx/developer_docs/petsc_interface.rst +++ b/manual/sphinx/developer_docs/petsc_interface.rst @@ -286,8 +286,8 @@ the Laplace operator defined in :ref:`sec-operator-stencil`. :: PetscMatrix matrix(indexer); - Field2D &dx = localmesh->getCoordinates()->dx, - &dy = localmesh->getCoordinates()->dy; + Field2D &dx = localmesh->getCoordinates()->dx(), + &dy = localmesh->getCoordinates()->dy(); // Set up x-derivatives BOUT_FOR(i, indexer->getRegionNobndry()) { diff --git a/src/invert/laplace/impls/cyclic/cyclic_laplace.cxx b/src/invert/laplace/impls/cyclic/cyclic_laplace.cxx index ce9378fd9d..42918cc6f3 100644 --- a/src/invert/laplace/impls/cyclic/cyclic_laplace.cxx +++ b/src/invert/laplace/impls/cyclic/cyclic_laplace.cxx @@ -392,7 +392,7 @@ Field3D LaplaceCyclic::solve(const Field3D& rhs, const Field3D& x0) { // Get elements of the tridiagonal matrix // including boundary conditions - const BoutReal zlen = getUniform(coords->dz) * (localmesh->LocalNz - 3); + const BoutReal zlen = getUniform(coords->dz()) * (localmesh->LocalNz - 3); BOUT_OMP_PERF(for nowait) for (int ind = 0; ind < nsys; ind++) { // ind = (iy - ys) * nmode + kz diff --git a/src/invert/laplace/impls/hypre3d/hypre3d_laplace.cxx b/src/invert/laplace/impls/hypre3d/hypre3d_laplace.cxx index c74e184be3..b61f70c4b1 100644 --- a/src/invert/laplace/impls/hypre3d/hypre3d_laplace.cxx +++ b/src/invert/laplace/impls/hypre3d/hypre3d_laplace.cxx @@ -101,8 +101,8 @@ LaplaceHypre3d::LaplaceHypre3d(Options* opt, const CELL_LOC loc, Mesh* mesh_in, BOUT_FOR_SERIAL(i, indexer->getRegionInnerX()) { if (inner_boundary_flags & INVERT_AC_GRAD) { // Neumann on inner X boundary - operator3D(i, i) = -1. / coords->dx[i] / sqrt(coords->g_11[i]); - operator3D(i, i.xp()) = 1. / coords->dx[i] / sqrt(coords->g_11[i]); + operator3D(i, i) = -1. / coords->dx()[i] / sqrt(coords->g_11[i]); + operator3D(i, i.xp()) = 1. / coords->dx()[i] / sqrt(coords->g_11[i]); } else { // Dirichlet on inner X boundary operator3D(i, i) = 0.5; @@ -113,8 +113,8 @@ LaplaceHypre3d::LaplaceHypre3d(Options* opt, const CELL_LOC loc, Mesh* mesh_in, BOUT_FOR_SERIAL(i, indexer->getRegionOuterX()) { if (outer_boundary_flags & INVERT_AC_GRAD) { // Neumann on outer X boundary - operator3D(i, i) = 1. / coords->dx[i] / sqrt(coords->g_11[i]); - operator3D(i, i.xm()) = -1. / coords->dx[i] / sqrt(coords->g_11[i]); + operator3D(i, i) = 1. / coords->dx()[i] / sqrt(coords->g_11[i]); + operator3D(i, i.xm()) = -1. / coords->dx()[i] / sqrt(coords->g_11[i]); } else { // Dirichlet on outer X boundary operator3D(i, i) = 0.5; @@ -125,8 +125,8 @@ LaplaceHypre3d::LaplaceHypre3d(Options* opt, const CELL_LOC loc, Mesh* mesh_in, BOUT_FOR_SERIAL(i, indexer->getRegionLowerY()) { if (lower_boundary_flags & INVERT_AC_GRAD) { // Neumann on lower Y boundary - operator3D(i, i) = -1. / coords->dy[i] / sqrt(coords->g_22[i]); - operator3D(i, i.yp()) = 1. / coords->dy[i] / sqrt(coords->g_22[i]); + operator3D(i, i) = -1. / coords->dy()[i] / sqrt(coords->g_22[i]); + operator3D(i, i.yp()) = 1. / coords->dy()[i] / sqrt(coords->g_22[i]); } else { // Dirichlet on lower Y boundary operator3D(i, i) = 0.5; @@ -137,8 +137,8 @@ LaplaceHypre3d::LaplaceHypre3d(Options* opt, const CELL_LOC loc, Mesh* mesh_in, BOUT_FOR_SERIAL(i, indexer->getRegionUpperY()) { if (upper_boundary_flags & INVERT_AC_GRAD) { // Neumann on upper Y boundary - operator3D(i, i) = 1. / coords->dy[i] / sqrt(coords->g_22[i]); - operator3D(i, i.ym()) = -1. / coords->dy[i] / sqrt(coords->g_22[i]); + operator3D(i, i) = 1. / coords->dy()[i] / sqrt(coords->g_22[i]); + operator3D(i, i.ym()) = -1. / coords->dy()[i] / sqrt(coords->g_22[i]); } else { // Dirichlet on upper Y boundary operator3D(i, i) = 0.5; @@ -322,14 +322,14 @@ void LaplaceHypre3d::updateMatrix3D() { if (nonuniform) { C_df_dx += C_d2f_dx2 * coords->d1_dx[l]; } - C_df_dx /= 2 * coords->dx[l]; - C_df_dz /= 2 * coords->dz[l]; + C_df_dx /= 2 * coords->dx()[l]; + C_df_dz /= 2 * coords->dz()[l]; - C_d2f_dx2 /= SQ(coords->dx[l]); - C_d2f_dy2 /= SQ(coords->dy[l]); - C_d2f_dz2 /= SQ(coords->dz[l]); + C_d2f_dx2 /= SQ(coords->dx()[l]); + C_d2f_dy2 /= SQ(coords->dy()[l]); + C_d2f_dz2 /= SQ(coords->dz()[l]); - C_d2f_dxdz /= 4 * coords->dx[l] * coords->dz[l]; + C_d2f_dxdz /= 4 * coords->dx()[l] * coords->dz()[l]; operator3D(l, l) = -2 * (C_d2f_dx2 + C_d2f_dy2 + C_d2f_dz2) + A[l]; operator3D(l, l.xp()) = C_df_dx + C_d2f_dx2; @@ -386,12 +386,12 @@ void LaplaceHypre3d::updateMatrix3D() { if (nonuniform) { C_df_dy += C_d2f_dy2 * coords->d1_dy[l]; } - C_df_dy /= 2 * coords->dy[l]; - C_d2f_dy2 /= SQ(coords->dy[l]); - C_d2f_dxdy /= 4 * coords->dx[l]; // NOTE: This value is not completed here. It needs + C_df_dy /= 2 * coords->dy()[l]; + C_d2f_dy2 /= SQ(coords->dy()[l]); + C_d2f_dxdy /= 4 * coords->dx()[l]; // NOTE: This value is not completed here. It needs // to be divide by dx(i +/- 1, j, k) when using to // set a matrix element - C_d2f_dydz /= 4 * coords->dy[l] * coords->dz[l]; + C_d2f_dydz /= 4 * coords->dy()[l] * coords->dz()[l]; // The values stored in the y-boundary are already interpolated // up/down, so we don't want the matrix to do any such @@ -401,10 +401,10 @@ void LaplaceHypre3d::updateMatrix3D() { operator3D.yup(yup)(l, l.yp()) += C_df_dy + C_d2f_dy2; operator3D.ydown(ydown)(l, l.ym()) += -C_df_dy + C_d2f_dy2; - operator3D.yup(yup)(l, l.xp().yp()) += C_d2f_dxdy / coords->dy[l.xp()]; - operator3D.ydown(ydown)(l, l.xp().ym()) += -C_d2f_dxdy / coords->dy[l.xp()]; - operator3D.yup(yup)(l, l.xm().yp()) += -C_d2f_dxdy / coords->dy[l.xm()]; - operator3D.ydown(ydown)(l, l.xm().ym()) += C_d2f_dxdy / coords->dy[l.xm()]; + operator3D.yup(yup)(l, l.xp().yp()) += C_d2f_dxdy / coords->dy()[l.xp()]; + operator3D.ydown(ydown)(l, l.xp().ym()) += -C_d2f_dxdy / coords->dy()[l.xp()]; + operator3D.yup(yup)(l, l.xm().yp()) += -C_d2f_dxdy / coords->dy()[l.xm()]; + operator3D.ydown(ydown)(l, l.xm().ym()) += C_d2f_dxdy / coords->dy()[l.xm()]; operator3D.yup(yup)(l, l.yp().zp()) += C_d2f_dydz; operator3D.yup(yup)(l, l.yp().zm()) += -C_d2f_dydz; operator3D.ydown(ydown)(l, l.ym().zp()) += -C_d2f_dydz; diff --git a/src/invert/laplace/impls/multigrid/multigrid_laplace.cxx b/src/invert/laplace/impls/multigrid/multigrid_laplace.cxx index 6f7070e1e1..afc8045130 100644 --- a/src/invert/laplace/impls/multigrid/multigrid_laplace.cxx +++ b/src/invert/laplace/impls/multigrid/multigrid_laplace.cxx @@ -286,7 +286,7 @@ FieldPerp LaplaceMultigrid::solve(const FieldPerp& b_in, const FieldPerp& x0) { int k2 = k - 1; x[k] = -x0(localmesh->xstart - 1, k2) * sqrt(coords->g_11()(localmesh->xstart, yindex)) - * coords->dx(localmesh->xstart, yindex); + * coords->dx()(localmesh->xstart, yindex); } } else { // zero gradient inner boundary condition @@ -330,7 +330,7 @@ FieldPerp LaplaceMultigrid::solve(const FieldPerp& b_in, const FieldPerp& x0) { int k2 = k - 1; x[(lxx + 1) * lz2 + k] = x0(localmesh->xend + 1, k2) * sqrt(coords->g_11()(localmesh->xend, yindex)) - * coords->dx(localmesh->xend, yindex); + * coords->dx()(localmesh->xend, yindex); // this is the value to set the gradient to at the outer boundary } } else { @@ -489,7 +489,7 @@ FieldPerp LaplaceMultigrid::solve(const FieldPerp& b_in, const FieldPerp& x0) { result(i2, k2) = x[lz2 + k] - x0(localmesh->xstart - 1, k2) * sqrt(coords->g_11()(localmesh->xstart, yindex)) - * coords->dx(localmesh->xstart, yindex); + * coords->dx()(localmesh->xstart, yindex); } } else { // zero gradient inner boundary condition @@ -537,7 +537,7 @@ FieldPerp LaplaceMultigrid::solve(const FieldPerp& b_in, const FieldPerp& x0) { result(i2, k2) = x[lxx * lz2 + k] + x0(localmesh->xend + 1, k2) * sqrt(coords->g_11()(localmesh->xend, yindex)) - * coords->dx(localmesh->xend, yindex); + * coords->dx()(localmesh->xend, yindex); } } else { // zero gradient outer boundary condition @@ -597,21 +597,21 @@ void LaplaceMultigrid::generateMatrixF(int level) { int k2p = (k2 + 1) % Nz_global; int k2m = (k2 + Nz_global - 1) % Nz_global; - BoutReal dz = coords->dz(i2, yindex); + BoutReal dz = coords->dz()(i2, yindex); BoutReal ddx_C = (C2(i2 + 1, yindex, k2) - C2(i2 - 1, yindex, k2)) / 2. - / coords->dx(i2, yindex) / C1(i2, yindex, k2); + / coords->dx()(i2, yindex) / C1(i2, yindex, k2); BoutReal ddz_C = (C2(i2, yindex, k2p) - C2(i2, yindex, k2m)) / 2. / dz / C1(i2, yindex, k2); BoutReal ddx = D(i2, yindex, k2) * coords->g11()(i2, yindex) - / coords->dx(i2, yindex) / coords->dx(i2, yindex); + / coords->dx()(i2, yindex) / coords->dx()(i2, yindex); // coefficient of 2nd derivative stencil (x-direction) BoutReal ddz = D(i2, yindex, k2) * coords->g33()(i2, yindex) / SQ(dz); // coefficient of 2nd derivative stencil (z-direction) BoutReal dxdz = D(i2, yindex, k2) * 2. * coords->g13()(i2, yindex) - / coords->dx(i2, yindex) / dz; + / coords->dx()(i2, yindex) / dz; // coefficient of mixed derivative stencil (could assume zero, at least initially, // if easier; then check this is true in constructor) @@ -621,7 +621,7 @@ void LaplaceMultigrid::generateMatrixF(int level) { + coords->g13()(i2, yindex) * ddz_C // (could assume zero, at least initially, if easier; then check this is true in constructor) ) - / coords->dx(i2, yindex); // coefficient of 1st derivative stencil (x-direction) + / coords->dx()(i2, yindex); // coefficient of 1st derivative stencil (x-direction) if (nonuniform) { // add correction for non-uniform dx dxd += D(i2, yindex, k2) * coords->d1_dx(i2, yindex); diff --git a/src/invert/laplace/impls/pcr/pcr.cxx b/src/invert/laplace/impls/pcr/pcr.cxx index 5c4f8da35b..7fa2cf8d83 100644 --- a/src/invert/laplace/impls/pcr/pcr.cxx +++ b/src/invert/laplace/impls/pcr/pcr.cxx @@ -160,7 +160,7 @@ FieldPerp LaplacePCR::solve(const FieldPerp& rhs, const FieldPerp& x0) { } if (dst) { - const BoutReal zlen = getUniform(coords->dz) * (localmesh->LocalNz - 3); + const BoutReal zlen = getUniform(coords->dz()) * (localmesh->LocalNz - 3); BOUT_OMP_PERF(parallel) { /// Create a local thread-scope working array @@ -370,7 +370,7 @@ Field3D LaplacePCR::solve(const Field3D& rhs, const Field3D& x0) { auto bcmplx3D = Matrix(nsys, nx); if (dst) { - const BoutReal zlen = getUniform(coords->dz) * (localmesh->LocalNz - 3); + const BoutReal zlen = getUniform(coords->dz()) * (localmesh->LocalNz - 3); BOUT_OMP_PERF(parallel) { /// Create a local thread-scope working array diff --git a/src/invert/laplace/impls/pcr_thomas/pcr_thomas.cxx b/src/invert/laplace/impls/pcr_thomas/pcr_thomas.cxx index 35a25779a7..6f7aa799f1 100644 --- a/src/invert/laplace/impls/pcr_thomas/pcr_thomas.cxx +++ b/src/invert/laplace/impls/pcr_thomas/pcr_thomas.cxx @@ -156,7 +156,7 @@ FieldPerp LaplacePCR_THOMAS::solve(const FieldPerp& rhs, const FieldPerp& x0) { } if (dst) { - const BoutReal zlength = getUniform(coords->dz) * (localmesh->LocalNz - 3); + const BoutReal zlength = getUniform(coords->dz()) * (localmesh->LocalNz - 3); BOUT_OMP_PERF(parallel) { /// Create a local thread-scope working array @@ -366,7 +366,7 @@ Field3D LaplacePCR_THOMAS::solve(const Field3D& rhs, const Field3D& x0) { auto bcmplx3D = Matrix(nsys, nx); if (dst) { - const BoutReal zlength = getUniform(coords->dz) * (localmesh->LocalNz - 3); + const BoutReal zlength = getUniform(coords->dz()) * (localmesh->LocalNz - 3); BOUT_OMP_PERF(parallel) { /// Create a local thread-scope working array diff --git a/src/invert/laplace/impls/petsc3damg/petsc3damg.cxx b/src/invert/laplace/impls/petsc3damg/petsc3damg.cxx index d1e2207725..c6fc823637 100644 --- a/src/invert/laplace/impls/petsc3damg/petsc3damg.cxx +++ b/src/invert/laplace/impls/petsc3damg/petsc3damg.cxx @@ -126,7 +126,7 @@ LaplacePetsc3dAmg::LaplacePetsc3dAmg(Options* opt, const CELL_LOC loc, Mesh* mes BOUT_FOR_SERIAL(i, indexer->getRegionInnerX()) { operator3D(i, i) = inner_X_BC[i]; operator3D(i, i.xp()) = inner_X_BC_plus[i]; - } + } const bool outer_X_neumann = flagSet(outer_boundary_flags, INVERT_AC_GRAD); const auto outer_X_BC = outer_X_neumann ? 1. / coords->dx / sqrt(coords->g_11) : 0.5; @@ -135,7 +135,7 @@ LaplacePetsc3dAmg::LaplacePetsc3dAmg(Options* opt, const CELL_LOC loc, Mesh* mes BOUT_FOR_SERIAL(i, indexer->getRegionOuterX()) { operator3D(i, i) = outer_X_BC[i]; operator3D(i, i.xm()) = outer_X_BC_minus[i]; - } + } const bool lower_Y_neumann = flagSet(lower_boundary_flags, INVERT_AC_GRAD); const auto lower_Y_BC = lower_Y_neumann ? -1. / coords->dy / sqrt(coords->g_22) : 0.5; @@ -144,7 +144,7 @@ LaplacePetsc3dAmg::LaplacePetsc3dAmg(Options* opt, const CELL_LOC loc, Mesh* mes BOUT_FOR_SERIAL(i, indexer->getRegionLowerY()) { operator3D(i, i) = lower_Y_BC[i]; operator3D(i, i.yp()) = lower_Y_BC_plus[i]; - } + } const bool upper_Y_neumann = flagSet(upper_boundary_flags, INVERT_AC_GRAD); const auto upper_Y_BC = upper_Y_neumann ? 1. / coords->dy / sqrt(coords->g_22) : 0.5; @@ -153,8 +153,8 @@ LaplacePetsc3dAmg::LaplacePetsc3dAmg(Options* opt, const CELL_LOC loc, Mesh* mes BOUT_FOR_SERIAL(i, indexer->getRegionUpperY()) { operator3D(i, i) = upper_Y_BC[i]; operator3D(i, i.ym()) = upper_Y_BC_minus[i]; + } } -} LaplacePetsc3dAmg::~LaplacePetsc3dAmg() { if (kspInitialised) { @@ -321,14 +321,14 @@ void LaplacePetsc3dAmg::updateMatrix3D() { if (nonuniform) { C_df_dx += C_d2f_dx2 * coords->d1_dx[l]; } - C_df_dx /= 2 * coords->dx[l]; - C_df_dz /= 2 * coords->dz[l]; + C_df_dx /= 2 * coords->dx()[l]; + C_df_dz /= 2 * coords->dz()[l]; - C_d2f_dx2 /= SQ(coords->dx[l]); - C_d2f_dy2 /= SQ(coords->dy[l]); - C_d2f_dz2 /= SQ(coords->dz[l]); + C_d2f_dx2 /= SQ(coords->dx()[l]); + C_d2f_dy2 /= SQ(coords->dy()[l]); + C_d2f_dz2 /= SQ(coords->dz()[l]); - C_d2f_dxdz /= 4 * coords->dx[l] * coords->dz[l]; + C_d2f_dxdz /= 4 * coords->dx()[l] * coords->dz()[l]; operator3D(l, l) = -2 * (C_d2f_dx2 + C_d2f_dy2 + C_d2f_dz2) + A[l]; operator3D(l, l.xp()) = C_df_dx + C_d2f_dx2; @@ -387,13 +387,13 @@ void LaplacePetsc3dAmg::updateMatrix3D() { if (nonuniform) { C_df_dy += C_d2f_dy2 * coords->d1_dy[l]; } - C_df_dy /= 2 * coords->dy[l]; - C_d2f_dy2 /= SQ(coords->dy[l]); + C_df_dy /= 2 * coords->dy()[l]; + C_d2f_dy2 /= SQ(coords->dy()[l]); C_d2f_dxdy /= - 4 * coords->dx[l]; // NOTE: This value is not completed here. It needs to + 4 * coords->dx()[l]; // NOTE: This value is not completed here. It needs to // be divide by dx(i +/- 1, j, k) when using to set a // matrix element - C_d2f_dydz /= 4 * coords->dy[l] * coords->dz[l]; + C_d2f_dydz /= 4 * coords->dy()[l] * coords->dz()[l]; // The values stored in the y-boundary are already interpolated // up/down, so we don't want the matrix to do any such @@ -403,10 +403,10 @@ void LaplacePetsc3dAmg::updateMatrix3D() { operator3D.yup(yup)(l, l.yp()) += C_df_dy + C_d2f_dy2; operator3D.ydown(ydown)(l, l.ym()) += -C_df_dy + C_d2f_dy2; - operator3D.yup(yup)(l, l.xp().yp()) += C_d2f_dxdy / coords->dy[l.xp()]; - operator3D.ydown(ydown)(l, l.xp().ym()) += -C_d2f_dxdy / coords->dy[l.xp()]; - operator3D.yup(yup)(l, l.xm().yp()) += -C_d2f_dxdy / coords->dy[l.xm()]; - operator3D.ydown(ydown)(l, l.xm().ym()) += C_d2f_dxdy / coords->dy[l.xm()]; + operator3D.yup(yup)(l, l.xp().yp()) += C_d2f_dxdy / coords->dy()[l.xp()]; + operator3D.ydown(ydown)(l, l.xp().ym()) += -C_d2f_dxdy / coords->dy()[l.xp()]; + operator3D.yup(yup)(l, l.xm().yp()) += -C_d2f_dxdy / coords->dy()[l.xm()]; + operator3D.ydown(ydown)(l, l.xm().ym()) += C_d2f_dxdy / coords->dy()[l.xm()]; operator3D.yup(yup)(l, l.yp().zp()) += C_d2f_dydz; operator3D.yup(yup)(l, l.yp().zm()) += -C_d2f_dydz; operator3D.ydown(ydown)(l, l.ym().zp()) += -C_d2f_dydz; diff --git a/src/invert/laplace/impls/serial_band/serial_band.cxx b/src/invert/laplace/impls/serial_band/serial_band.cxx index beb034f281..f292667e1c 100644 --- a/src/invert/laplace/impls/serial_band/serial_band.cxx +++ b/src/invert/laplace/impls/serial_band/serial_band.cxx @@ -179,8 +179,8 @@ FieldPerp LaplaceSerialBand::solve(const FieldPerp& b, const FieldPerp& x0) { // non-uniform localmesh correction if ((ix != 0) && (ix != ncx)) { coef4 += coords->g11()(ix, jy) - * ((1.0 / coords->dx(ix + 1, jy)) - (1.0 / coords->dx(ix - 1, jy))) - / (2.0 * coords->dx(ix, jy)); + * ((1.0 / coords->dx()(ix + 1, jy)) - (1.0 / coords->dx()(ix - 1, jy))) + / (2.0 * coords->dx()(ix, jy)); } } @@ -190,14 +190,14 @@ FieldPerp LaplaceSerialBand::solve(const FieldPerp& b, const FieldPerp& x0) { coef4 += coords->g11()(ix, jy) * (Ccoef(ix - 2, jy) - 8. * Ccoef(ix - 1, jy) + 8. * Ccoef(ix + 1, jy) - Ccoef(ix + 2, jy)) - / (12. * coords->dx(ix, jy) * (Ccoef(ix, jy))); + / (12. * coords->dx()(ix, jy) * (Ccoef(ix, jy))); } // Put into matrix - coef1 /= 12. * SQ(coords->dx(ix, jy)); + coef1 /= 12. * SQ(coords->dx()(ix, jy)); coef2 *= SQ(kwave); - coef3 *= kwave / (12. * coords->dx(ix, jy)); - coef4 /= 12. * coords->dx(ix, jy); + coef3 *= kwave / (12. * coords->dx()(ix, jy)); + coef4 /= 12. * coords->dx()(ix, jy); coef5 *= kwave; A(ix, 0) = dcomplex(-coef1 + coef4, coef3); @@ -214,9 +214,9 @@ FieldPerp LaplaceSerialBand::solve(const FieldPerp& b, const FieldPerp& x0) { int ix = 1; auto kwave = kwave_(ix, jy); - coef1 = coords->g11()(ix, jy) / (SQ(coords->dx(ix, jy))); + coef1 = coords->g11()(ix, jy) / (SQ(coords->dx()(ix, jy))); coef2 = coords->g33()(ix, jy); - coef3 = kwave * coords->g13()(ix, jy) / (2. * coords->dx(ix, jy)); + coef3 = kwave * coords->g13()(ix, jy) / (2. * coords->dx()(ix, jy)); // Multiply Delp2 component by a factor coef1 *= Dcoef(ix, jy); @@ -231,9 +231,9 @@ FieldPerp LaplaceSerialBand::solve(const FieldPerp& b, const FieldPerp& x0) { ix = ncx - 1; - coef1 = coords->g11()(ix, jy) / (SQ(coords->dx(ix, jy))); + coef1 = coords->g11()(ix, jy) / (SQ(coords->dx()(ix, jy))); coef2 = coords->g33()(ix, jy); - coef3 = kwave * coords->g13()(ix, jy) / (2. * coords->dx(ix, jy)); + coef3 = kwave * coords->g13()(ix, jy) / (2. * coords->dx()(ix, jy)); A(ix, 0) = 0.0; A(ix, 1) = dcomplex(coef1, -coef3); @@ -272,8 +272,8 @@ FieldPerp LaplaceSerialBand::solve(const FieldPerp& b, const FieldPerp& x0) { for (int ix = 0; ix < xbndry; ix++) { A(ix, 0) = 0.; A(ix, 1) = 0.; - A(ix, 2) = -.5 / sqrt(coords->g_11()(ix, jy)) / coords->dx(ix, jy); - A(ix, 3) = .5 / sqrt(coords->g_11()(ix, jy)) / coords->dx(ix, jy); + A(ix, 2) = -.5 / sqrt(coords->g_11()(ix, jy)) / coords->dx()(ix, jy); + A(ix, 3) = .5 / sqrt(coords->g_11()(ix, jy)) / coords->dx()(ix, jy); A(ix, 4) = 0.; } @@ -337,18 +337,18 @@ FieldPerp LaplaceSerialBand::solve(const FieldPerp& b, const FieldPerp& x0) { int ix = 1; auto kwave = kwave_(ix, jy); - coef1 = coords->g11()(ix, jy) / (12. * SQ(coords->dx(ix, jy))); + coef1 = coords->g11()(ix, jy) / (12. * SQ(coords->dx()(ix, jy))); coef2 = coords->g33()(ix, jy); - coef3 = kwave * coords->g13()(ix, jy) / (2. * coords->dx(ix, jy)); + coef3 = kwave * coords->g13()(ix, jy) / (2. * coords->dx()(ix, jy)); coef4 = Acoef(ix, jy); // Combine 4th order at 1 with 2nd order at 0 A(1, 0) = 0.0; // Not used A(1, 1) = dcomplex((14. - - SQ(coords->dx(0, jy) * kwave) * coords->g33()(0, jy) + - SQ(coords->dx()(0, jy) * kwave) * coords->g33()(0, jy) / coords->g11()(0, jy)) * coef1, -coef3); @@ -356,9 +356,9 @@ FieldPerp LaplaceSerialBand::solve(const FieldPerp& b, const FieldPerp& x0) { A(1, 3) = dcomplex(16. * coef1, coef3); A(1, 4) = dcomplex(-coef1, 0.0); - coef1 = coords->g11()(ix, jy) / (SQ(coords->dx(ix, jy))); + coef1 = coords->g11()(ix, jy) / (SQ(coords->dx()(ix, jy))); coef2 = coords->g33()(ix, jy); - coef3 = kwave * coords->g13()(ix, jy) / (2. * coords->dx(ix, jy)); + coef3 = kwave * coords->g13()(ix, jy) / (2. * coords->dx()(ix, jy)); // Use 2nd order at 1 A(0, 0) = 0.0; // Should never be used @@ -381,12 +381,12 @@ FieldPerp LaplaceSerialBand::solve(const FieldPerp& b, const FieldPerp& x0) { int ix = ncx - 1; - coef1 = coords->g11()(ix, jy) / (12. * SQ(coords->dx(ix, jy))); + coef1 = coords->g11()(ix, jy) / (12. * SQ(coords->dx()(ix, jy))); coef2 = coords->g33()(ix, jy); auto kwave = kwave_(ix, jy); - coef3 = kwave * coords->g13()(ix, jy) / (2. * coords->dx(ix, jy)); + coef3 = kwave * coords->g13()(ix, jy) / (2. * coords->dx()(ix, jy)); coef4 = Acoef(ix, jy); @@ -395,15 +395,15 @@ FieldPerp LaplaceSerialBand::solve(const FieldPerp& b, const FieldPerp& x0) { A(ix, 1) = dcomplex(16. * coef1, -coef3); A(ix, 2) = dcomplex(-29. * coef1 - SQ(kwave) * coef2 + coef4, 0.0); A(ix, 3) = dcomplex((14. - - SQ(coords->dx(ncx, jy) * kwave) * coords->g33()(ncx, jy) + - SQ(coords->dx()(ncx, jy) * kwave) * coords->g33()(ncx, jy) / coords->g11()(ncx, jy)) * coef1, coef3); A(ix, 4) = 0.0; // Not used - coef1 = coords->g11()(ix, jy) / (SQ(coords->dx(ix, jy))); + coef1 = coords->g11()(ix, jy) / (SQ(coords->dx()(ix, jy))); coef2 = coords->g33()(ix, jy); - coef3 = kwave * coords->g13()(ix, jy) / (2. * coords->dx(ix, jy)); + coef3 = kwave * coords->g13()(ix, jy) / (2. * coords->dx()(ix, jy)); // Use 2nd order at ncx - 1 A(ncx, 0) = dcomplex(coef1, -coef3); diff --git a/src/invert/laplace/invert_laplace.cxx b/src/invert/laplace/invert_laplace.cxx index fea972ebee..9c17d8f60f 100644 --- a/src/invert/laplace/invert_laplace.cxx +++ b/src/invert/laplace/invert_laplace.cxx @@ -349,8 +349,8 @@ void Laplacian::tridagCoefs(int jx, int jy, BoutReal kwave, dcomplex& a, dcomple // non-uniform mesh correction if ((jx != 0) && (jx != (localmesh->LocalNx - 1))) { coef4 -= 0.5 - * ((localcoords->dx(jx + 1, jy) - localcoords->dx(jx - 1, jy)) - / SQ(localcoords->dx(jx, jy))) + * ((localcoords->dx()(jx + 1, jy) - localcoords->dx()(jx - 1, jy)) + / SQ(localcoords->dx()(jx, jy))) * coef1; } } @@ -359,7 +359,7 @@ void Laplacian::tridagCoefs(int jx, int jy, BoutReal kwave, dcomplex& a, dcomple // First derivative terms if ((jx > 0) && (jx < (localmesh->LocalNx - 1))) { BoutReal dc2dx_over_c1 = ((*c2coef)(jx + 1, jy) - (*c2coef)(jx - 1, jy)) - / (2. * localcoords->dx(jx, jy) * ((*c1coef)(jx, jy))); + / (2. * localcoords->dx()(jx, jy) * ((*c1coef)(jx, jy))); coef4 += localcoords->g11()(jx, jy) * dc2dx_over_c1; coef5 += localcoords->g13()(jx, jy) * dc2dx_over_c1; } @@ -373,9 +373,9 @@ void Laplacian::tridagCoefs(int jx, int jy, BoutReal kwave, dcomplex& a, dcomple coef3 = 0.0; // This cancels out } - coef1 /= SQ(localcoords->dx(jx, jy)); - coef3 /= 2. * localcoords->dx(jx, jy); - coef4 /= 2. * localcoords->dx(jx, jy); + coef1 /= SQ(localcoords->dx()(jx, jy)); + coef3 /= 2. * localcoords->dx()(jx, jy); + coef4 /= 2. * localcoords->dx()(jx, jy); a = dcomplex(coef1 - coef4, -kwave * coef3); b = dcomplex(-2.0 * coef1 - SQ(kwave) * coef2, kwave * coef5); @@ -511,8 +511,8 @@ void Laplacian::tridagMatrix(dcomplex* avec, dcomplex* bvec, dcomplex* cvec, dco // Zero gradient at inner boundary for (int ix = 0; ix < inbndry; ix++) { avec[ix] = 0.; - bvec[ix] = -1. / sqrt(coords->g_11()(ix, jy)) / coords->dx(ix, jy); - cvec[ix] = 1. / sqrt(coords->g_11()(ix, jy)) / coords->dx(ix, jy); + bvec[ix] = -1. / sqrt(coords->g_11()(ix, jy)) / coords->dx()(ix, jy); + cvec[ix] = 1. / sqrt(coords->g_11()(ix, jy)) / coords->dx()(ix, jy); } } else if (inner_boundary_flags & INVERT_DC_GRAD) { // Zero gradient at inner boundary @@ -546,8 +546,7 @@ void Laplacian::tridagMatrix(dcomplex* avec, dcomplex* bvec, dcomplex* cvec, dco for (int ix = 0; ix < inbndry; ix++) { avec[ix] = 0.; bvec[ix] = 1.; - cvec[ix] = -exp(-k * coords->dx(ix, jy) - / sqrt(coords->g11()(ix, jy))); + cvec[ix] = -exp(-k * coords->dx()(ix, jy) / sqrt(coords->g11()(ix, jy))); } } else if (inner_boundary_flags & INVERT_IN_CYLINDER) { // Condition for inner radial boundary for cylindrical coordinates @@ -609,9 +608,9 @@ void Laplacian::tridagMatrix(dcomplex* avec, dcomplex* bvec, dcomplex* cvec, dco for (int ix = 0; ix < inbndry; ix++) { avec[ix] = dcomplex(0., 0.); bvec[ix] = - dcomplex(-1., 0.) / sqrt(coords->g_11()(ix, jy)) / coords->dx(ix, jy); + dcomplex(-1., 0.) / sqrt(coords->g_11()(ix, jy)) / coords->dx()(ix, jy); cvec[ix] = - dcomplex(1., 0.) / sqrt(coords->g_11()(ix, jy)) / coords->dx(ix, jy); + dcomplex(1., 0.) / sqrt(coords->g_11()(ix, jy)) / coords->dx()(ix, jy); } } else if (inner_boundary_flags & INVERT_AC_GRAD) { // Zero gradient at inner boundary @@ -625,10 +624,8 @@ void Laplacian::tridagMatrix(dcomplex* avec, dcomplex* bvec, dcomplex* cvec, dco for (int ix = 0; ix < inbndry; ix++) { avec[ix] = 0.0; bvec[ix] = 1.0; - cvec[ix] = -exp(-1.0 - * sqrt(coords->g33()(ix, jy) - / coords->g11()(ix, jy)) - * kwave * coords->dx(ix, jy)); + cvec[ix] = -exp(-1.0 * sqrt(coords->g33()(ix, jy) / coords->g11()(ix, jy)) + * kwave * coords->dx()(ix, jy)); } } else if (inner_boundary_flags & INVERT_IN_CYLINDER) { // Condition for inner radial boundary for cylindrical coordinates @@ -673,9 +670,9 @@ void Laplacian::tridagMatrix(dcomplex* avec, dcomplex* bvec, dcomplex* cvec, dco // Zero gradient at outer boundary for (int ix = 0; ix < outbndry; ix++) { avec[ncx - ix] = dcomplex(-1., 0.) / sqrt(coords->g_11()(ncx - ix, jy)) - / coords->dx(ncx - ix, jy); + / coords->dx()(ncx - ix, jy); bvec[ncx - ix] = dcomplex(1., 0.) / sqrt(coords->g_11()(ncx - ix, jy)) - / coords->dx(ncx - ix, jy); + / coords->dx()(ncx - ix, jy); cvec[ncx - ix] = dcomplex(0., 0.); } } else if (outer_boundary_flags & INVERT_DC_GRAD) { @@ -710,8 +707,8 @@ void Laplacian::tridagMatrix(dcomplex* avec, dcomplex* bvec, dcomplex* cvec, dco for (int ix = 0; ix < inbndry; ix++) { cvec[ncx - ix] = 0.; bvec[ncx - ix] = 1.; - avec[ncx - ix] = -exp(-k * coords->dx(ncx - ix, jy) - / sqrt(coords->g11()(ncx - ix, jy))); + avec[ncx - ix] = + -exp(-k * coords->dx()(ncx - ix, jy) / sqrt(coords->g11()(ncx - ix, jy))); } } else { // Order 2 dirichlet BC (boundary half between points) @@ -731,9 +728,9 @@ void Laplacian::tridagMatrix(dcomplex* avec, dcomplex* bvec, dcomplex* cvec, dco // Zero gradient at outer boundary for (int ix = 0; ix < outbndry; ix++) { avec[ncx - ix] = dcomplex(-1., 0.) / sqrt(coords->g_11()(ncx - ix, jy)) - / coords->dx(ncx - ix, jy); + / coords->dx()(ncx - ix, jy); bvec[ncx - ix] = dcomplex(1., 0.) / sqrt(coords->g_11()(ncx - ix, jy)) - / coords->dx(ncx - ix, jy); + / coords->dx()(ncx - ix, jy); cvec[ncx - ix] = dcomplex(0., 0.); } } else if (outer_boundary_flags & INVERT_AC_GRAD) { @@ -746,10 +743,9 @@ void Laplacian::tridagMatrix(dcomplex* avec, dcomplex* bvec, dcomplex* cvec, dco } else if (outer_boundary_flags & INVERT_AC_LAP) { // Use decaying zero-Laplacian solution in the boundary for (int ix = 0; ix < outbndry; ix++) { - avec[ncx - ix] = -exp(-1.0 - * sqrt(coords->g33()(xe - ix, jy) - / coords->g11()(xe - ix, jy)) - * kwave * coords->dx(xe - ix, jy)); + avec[ncx - ix] = + -exp(-1.0 * sqrt(coords->g33()(xe - ix, jy) / coords->g11()(xe - ix, jy)) + * kwave * coords->dx()(xe - ix, jy)); bvec[ncx - ix] = 1.0; cvec[ncx - ix] = 0.0; } diff --git a/src/invert/laplacexy/laplacexy.cxx b/src/invert/laplacexy/laplacexy.cxx index c1bc616bb1..b3bd7d403f 100644 --- a/src/invert/laplacexy/laplacexy.cxx +++ b/src/invert/laplacexy/laplacexy.cxx @@ -899,8 +899,8 @@ void LaplaceXY::setMatrixElementsFiniteVolume(const Field2D& A, const Field2D& B auto coords = localmesh->getCoordinates(location); const Field2D J_DC = DC(coords->J); const Field2D g11_DC = DC(coords->g11); - const Field2D dx_DC = DC(coords->dx); - const Field2D dy_DC = DC(coords->dy); + const Field2D dx_DC = DC(coords->dx()); + const Field2D dy_DC = DC(coords->dy()); const Field2D g_22_DC = DC(coords->g_22); const Field2D g_23_DC = DC(coords->g_23); const Field2D g23_DC = DC(coords->g23); @@ -1016,8 +1016,8 @@ void LaplaceXY::setMatrixElementsFiniteDifference(const Field2D& A, const Field2 const Field2D g12_2D = DC(coords->g12); const Field2D d1_dx_2D = DC(coords->d1_dx); const Field2D d1_dy_2D = DC(coords->d1_dy); - const Field2D dx_2D = DC(coords->dx); - const Field2D dy_2D = DC(coords->dy); + const Field2D dx_2D = DC(coords->dx()); + const Field2D dy_2D = DC(coords->dy()); const Field2D coef_dfdy = G2_2D - DC(DDY(J_2D / g_22_2D) / J_2D); diff --git a/src/invert/laplacexy2/laplacexy2.cxx b/src/invert/laplacexy2/laplacexy2.cxx index 8b5e98d747..7563e9618b 100644 --- a/src/invert/laplacexy2/laplacexy2.cxx +++ b/src/invert/laplacexy2/laplacexy2.cxx @@ -149,18 +149,18 @@ void LaplaceXY2::setCoefs(const Field2D& A, const Field2D& B) { // Metrics on x+1/2 boundary BoutReal J = 0.5 * (coords->J[index] + coords->J[ind_xp]); BoutReal g11 = 0.5 * (coords->g11[index] + coords->g11[ind_xp]); - BoutReal dx = 0.5 * (coords->dx[index] + coords->dx[ind_xp]); + BoutReal dx = 0.5 * (coords->dx()[index] + coords->dx()[ind_xp]); BoutReal Acoef = 0.5 * (A[index] + A[ind_xp]); - BoutReal xp = Acoef * J * g11 / (coords->J[index] * dx * coords->dx[index]); + BoutReal xp = Acoef * J * g11 / (coords->J[index] * dx * coords->dx()[index]); // Metrics on x-1/2 boundary J = 0.5 * (coords->J[index] + coords->J[ind_xm]); g11 = 0.5 * (coords->g11[index] + coords->g11[ind_xm]); - dx = 0.5 * (coords->dx[index] + coords->dx[ind_xm]); + dx = 0.5 * (coords->dx()[index] + coords->dx()[ind_xm]); Acoef = 0.5 * (A[index] + A[ind_xm]); - BoutReal xm = Acoef * J * g11 / (coords->J[index] * dx * coords->dx[index]); + BoutReal xm = Acoef * J * g11 / (coords->J[index] * dx * coords->dx()[index]); BoutReal c = B[index] - xp - xm; // Central coefficient @@ -177,11 +177,11 @@ void LaplaceXY2::setCoefs(const Field2D& A, const Field2D& B) { BoutReal g_22 = 0.5 * (coords->g_22[index] + coords->g_22[ind_yp]); BoutReal g23 = 0.5 * (coords->g23[index] + coords->g23[ind_yp]); BoutReal g_23 = 0.5 * (coords->g_23[index] + coords->g_23[ind_yp]); - BoutReal dy = 0.5 * (coords->dy[index] + coords->dy[ind_yp]); + BoutReal dy = 0.5 * (coords->dy()[index] + coords->dy()[ind_yp]); Acoef = 0.5 * (A[ind_yp] + A[index]); BoutReal yp = - -Acoef * J * g23 * g_23 / (g_22 * coords->J[index] * dy * coords->dy[index]); + -Acoef * J * g23 * g_23 / (g_22 * coords->J[index] * dy * coords->dy()[index]); c -= yp; matrix(index, ind_yp) = yp; @@ -190,11 +190,11 @@ void LaplaceXY2::setCoefs(const Field2D& A, const Field2D& B) { g_22 = 0.5 * (coords->g_22[index] + coords->g_22[ind_ym]); g23 = 0.5 * (coords->g23[index] + coords->g23[ind_ym]); g_23 = 0.5 * (coords->g_23[index] + coords->g_23[ind_ym]); - dy = 0.5 * (coords->dy[index] + coords->dy[ind_ym]); + dy = 0.5 * (coords->dy()[index] + coords->dy()[ind_ym]); Acoef = 0.5 * (A[ind_ym] + A[index]); BoutReal ym = - -Acoef * J * g23 * g_23 / (g_22 * coords->J[index] * dy * coords->dy[index]); + -Acoef * J * g23 * g_23 / (g_22 * coords->J[index] * dy * coords->dy()[index]); c -= ym; matrix(index, ind_ym) = ym; } diff --git a/src/invert/laplacexy2/laplacexy2_hypre.cxx b/src/invert/laplacexy2/laplacexy2_hypre.cxx index b18903be3a..a0d7487948 100644 --- a/src/invert/laplacexy2/laplacexy2_hypre.cxx +++ b/src/invert/laplacexy2/laplacexy2_hypre.cxx @@ -108,18 +108,18 @@ void LaplaceXY2Hypre::setCoefs(const Field2D& A, const Field2D& B) { // Metrics on x+1/2 boundary BoutReal J = 0.5 * (coords->J[index] + coords->J[ind_xp]); BoutReal g11 = 0.5 * (coords->g11[index] + coords->g11[ind_xp]); - BoutReal dx = 0.5 * (coords->dx[index] + coords->dx[ind_xp]); + BoutReal dx = 0.5 * (coords->dx()[index] + coords->dx()[ind_xp]); BoutReal Acoef = 0.5 * (A[index] + A[ind_xp]); - BoutReal xp = Acoef * J * g11 / (coords->J[index] * dx * coords->dx[index]); + BoutReal xp = Acoef * J * g11 / (coords->J[index] * dx * coords->dx()[index]); // Metrics on x-1/2 boundary J = 0.5 * (coords->J[index] + coords->J[ind_xm]); g11 = 0.5 * (coords->g11[index] + coords->g11[ind_xm]); - dx = 0.5 * (coords->dx[index] + coords->dx[ind_xm]); + dx = 0.5 * (coords->dx()[index] + coords->dx()[ind_xm]); Acoef = 0.5 * (A[index] + A[ind_xm]); - BoutReal xm = Acoef * J * g11 / (coords->J[index] * dx * coords->dx[index]); + BoutReal xm = Acoef * J * g11 / (coords->J[index] * dx * coords->dx()[index]); BoutReal c = B[index] - xp - xm; // Central coefficient @@ -136,11 +136,11 @@ void LaplaceXY2Hypre::setCoefs(const Field2D& A, const Field2D& B) { BoutReal g_22 = 0.5 * (coords->g_22[index] + coords->g_22[ind_yp]); BoutReal g23 = 0.5 * (coords->g23[index] + coords->g23[ind_yp]); BoutReal g_23 = 0.5 * (coords->g_23[index] + coords->g_23[ind_yp]); - BoutReal dy = 0.5 * (coords->dy[index] + coords->dy[ind_yp]); + BoutReal dy = 0.5 * (coords->dy()[index] + coords->dy()[ind_yp]); Acoef = 0.5 * (A[ind_yp] + A[index]); BoutReal yp = - -Acoef * J * g23 * g_23 / (g_22 * coords->J[index] * dy * coords->dy[index]); + -Acoef * J * g23 * g_23 / (g_22 * coords->J[index] * dy * coords->dy()[index]); c -= yp; // Metrics at y-1/2 @@ -148,11 +148,11 @@ void LaplaceXY2Hypre::setCoefs(const Field2D& A, const Field2D& B) { g_22 = 0.5 * (coords->g_22[index] + coords->g_22[ind_ym]); g23 = 0.5 * (coords->g23[index] + coords->g23[ind_ym]); g_23 = 0.5 * (coords->g_23[index] + coords->g_23[ind_ym]); - dy = 0.5 * (coords->dy[index] + coords->dy[ind_ym]); + dy = 0.5 * (coords->dy()[index] + coords->dy()[ind_ym]); Acoef = 0.5 * (A[ind_ym] + A[index]); BoutReal ym = - -Acoef * J * g23 * g_23 / (g_22 * coords->J[index] * dy * coords->dy[index]); + -Acoef * J * g23 * g_23 / (g_22 * coords->J[index] * dy * coords->dy()[index]); c -= ym; M(index, ind_yp) = yp; M(index, ind_ym) = ym; diff --git a/src/invert/laplacexz/impls/cyclic/laplacexz-cyclic.cxx b/src/invert/laplacexz/impls/cyclic/laplacexz-cyclic.cxx index 3f482a167d..5f64919d15 100644 --- a/src/invert/laplacexz/impls/cyclic/laplacexz-cyclic.cxx +++ b/src/invert/laplacexz/impls/cyclic/laplacexz-cyclic.cxx @@ -118,10 +118,10 @@ void LaplaceXZcyclic::setCoefs(const Field2D& A2D, const Field2D& B2D) { // Metrics on x+1/2 boundary BoutReal J = 0.5 * (coord->J()(x, y) + coord->J()(x + 1, y)); BoutReal g11 = 0.5 * (coord->g11()(x, y) + coord->g11()(x + 1, y)); - BoutReal dx = 0.5 * (coord->dx(x, y) + coord->dx(x + 1, y)); + BoutReal dx = 0.5 * (coord->dx()(x, y) + coord->dx()(x + 1, y)); BoutReal A = 0.5 * (A2D(x, y) + A2D(x + 1, y)); - BoutReal val = A * J * g11 / (coord->J()(x, y) * dx * coord->dx(x, y)); + BoutReal val = A * J * g11 / (coord->J()(x, y) * dx * coord->dx()(x, y)); ccoef(ind, x - xstart) += val; bcoef(ind, x - xstart) -= val; @@ -129,10 +129,10 @@ void LaplaceXZcyclic::setCoefs(const Field2D& A2D, const Field2D& B2D) { // Metrics on x-1/2 boundary J = 0.5 * (coord->J()(x, y) + coord->J()(x - 1, y)); g11 = 0.5 * (coord->g11()(x, y) + coord->g11()(x - 1, y)); - dx = 0.5 * (coord->dx(x, y) + coord->dx(x - 1, y)); + dx = 0.5 * (coord->dx()(x, y) + coord->dx()(x - 1, y)); A = 0.5 * (A2D(x, y) + A2D(x - 1, y)); - val = A * J * g11 / (coord->J()(x, y) * dx * coord->dx(x, y)); + val = A * J * g11 / (coord->J()(x, y) * dx * coord->dx()(x, y)); acoef(ind, x - xstart) += val; bcoef(ind, x - xstart) -= val; diff --git a/src/invert/parderiv/impls/cyclic/cyclic.cxx b/src/invert/parderiv/impls/cyclic/cyclic.cxx index 55eb785cea..78089b7026 100644 --- a/src/invert/parderiv/impls/cyclic/cyclic.cxx +++ b/src/invert/parderiv/impls/cyclic/cyclic.cxx @@ -164,9 +164,9 @@ const Field3D InvertParCR::solve(const Field3D& f) { ecoef += bcoef * coord->d1_dy(x, y + local_ystart); } - bcoef /= SQ(coord->dy(x, y + local_ystart)); - ccoef /= coord->dy(x, y + local_ystart); - ecoef /= coord->dy(x, y + local_ystart); + bcoef /= SQ(coord->dy()(x, y + local_ystart)); + ccoef /= coord->dy()(x, y + local_ystart); + ecoef /= coord->dy()(x, y + local_ystart); // const d2dy2 d2dydz d2dz2 ddy // ----- ----- ------ ----- --- diff --git a/src/invert/pardiv/impls/cyclic/pardiv_cyclic.cxx b/src/invert/pardiv/impls/cyclic/pardiv_cyclic.cxx index 68f7f034ad..f1fb894ec8 100644 --- a/src/invert/pardiv/impls/cyclic/pardiv_cyclic.cxx +++ b/src/invert/pardiv/impls/cyclic/pardiv_cyclic.cxx @@ -104,7 +104,7 @@ Field3D InvertParDivCR::solve(const Field3D& f) { auto b = Matrix(nsys, size); auto c = Matrix(nsys, size); - const Field2D dy = coord->dy; + const Field2D dy = coord->dy(); const Field2D J = coord->J(); const Field2D g_22 = coord->g_22(); diff --git a/src/mesh/boundary_standard.cxx b/src/mesh/boundary_standard.cxx index 33f08a50de..6f71a5b5e9 100644 --- a/src/mesh/boundary_standard.cxx +++ b/src/mesh/boundary_standard.cxx @@ -137,7 +137,7 @@ void BoundaryDirichlet::apply(Field2D& f, BoutReal t) { // cell to be val N.B. Only first guard cells (closest to the grid) should ever be used Mesh* mesh = bndry->localmesh; - ASSERT1(mesh == f.getMesh()); + ASSERT1(mesh == f.getMesh()) bndry->first(); // Decide which generator to use @@ -300,7 +300,7 @@ void BoundaryDirichlet::apply(Field3D& f, BoutReal t) { // cell to be val N.B. Only first guard cells (closest to the grid) should ever be used Mesh* mesh = bndry->localmesh; - ASSERT1(mesh == f.getMesh()); + ASSERT1(mesh == f.getMesh()) bndry->first(); // Decide which generator to use @@ -591,7 +591,7 @@ void BoundaryDirichlet::apply_ddt(Field2D& f) { void BoundaryDirichlet::apply_ddt(Field3D& f) { Mesh* mesh = bndry->localmesh; - ASSERT1(mesh == f.getMesh()); + ASSERT1(mesh == f.getMesh()) Field3D* dt = f.timeDeriv(); for (bndry->first(); !bndry->isDone(); bndry->next()) { @@ -622,7 +622,7 @@ void BoundaryDirichlet_O3::apply(Field2D& f, BoutReal t) { // cell to be val N.B. Only first guard cells (closest to the grid) should ever be used Mesh* mesh = bndry->localmesh; - ASSERT1(mesh == f.getMesh()); + ASSERT1(mesh == f.getMesh()) bndry->first(); // Decide which generator to use @@ -802,7 +802,7 @@ void BoundaryDirichlet_O3::apply(Field3D& f, BoutReal t) { // cell to be val N.B. Only first guard cells (closest to the grid) should ever be used Mesh* mesh = bndry->localmesh; - ASSERT1(mesh == f.getMesh()); + ASSERT1(mesh == f.getMesh()) bndry->first(); // Decide which generator to use @@ -1032,7 +1032,7 @@ void BoundaryDirichlet_O3::apply_ddt(Field2D& f) { void BoundaryDirichlet_O3::apply_ddt(Field3D& f) { Mesh* mesh = bndry->localmesh; - ASSERT1(mesh == f.getMesh()); + ASSERT1(mesh == f.getMesh()) Field3D* dt = f.timeDeriv(); bndry->first(); @@ -1064,7 +1064,7 @@ void BoundaryDirichlet_O4::apply(Field2D& f, BoutReal t) { // cell to be val N.B. Only first guard cells (closest to the grid) should ever be used Mesh* mesh = bndry->localmesh; - ASSERT1(mesh == f.getMesh()); + ASSERT1(mesh == f.getMesh()) bndry->first(); // Decide which generator to use @@ -1259,7 +1259,7 @@ void BoundaryDirichlet_O4::apply(Field3D& f, BoutReal t) { // cell to be val N.B. Only first guard cells (closest to the grid) should ever be used Mesh* mesh = bndry->localmesh; - ASSERT1(mesh == f.getMesh()); + ASSERT1(mesh == f.getMesh()) bndry->first(); // Decide which generator to use @@ -1501,7 +1501,7 @@ void BoundaryDirichlet_O4::apply_ddt(Field2D& f) { void BoundaryDirichlet_O4::apply_ddt(Field3D& f) { Mesh* mesh = bndry->localmesh; - ASSERT1(mesh == f.getMesh()); + ASSERT1(mesh == f.getMesh()) Field3D* dt = f.timeDeriv(); for (bndry->first(); !bndry->isDone(); bndry->next()) { for (int z = 0; z < mesh->LocalNz; z++) { @@ -1542,7 +1542,7 @@ void BoundaryDirichlet_4thOrder::apply(Field2D& f) { void BoundaryDirichlet_4thOrder::apply(Field3D& f) { Mesh* mesh = bndry->localmesh; - ASSERT1(mesh == f.getMesh()); + ASSERT1(mesh == f.getMesh()) // Set (at 4th order) the value at the mid-point between the guard cell and the grid // cell to be val for (bndry->first(); !bndry->isDone(); bndry->next1d()) { @@ -1570,7 +1570,7 @@ void BoundaryDirichlet_4thOrder::apply_ddt(Field2D& f) { void BoundaryDirichlet_4thOrder::apply_ddt(Field3D& f) { Mesh* mesh = bndry->localmesh; - ASSERT1(mesh == f.getMesh()); + ASSERT1(mesh == f.getMesh()) Field3D* dt = f.timeDeriv(); for (bndry->first(); !bndry->isDone(); bndry->next()) { for (int z = 0; z < mesh->LocalNz; z++) { @@ -1596,7 +1596,7 @@ BoundaryOp* BoundaryNeumann_NonOrthogonal::clone(BoundaryRegion* region, void BoundaryNeumann_NonOrthogonal::apply(Field2D& f) { #if not(BOUT_USE_METRIC_3D) Mesh* mesh = bndry->localmesh; - ASSERT1(mesh == f.getMesh()); + ASSERT1(mesh == f.getMesh()) Coordinates* metric = f.getCoordinates(); // Calculate derivatives for metric use @@ -1618,7 +1618,7 @@ void BoundaryNeumann_NonOrthogonal::apply(Field2D& f) { if (bndry->bx != 0 && bndry->by == 0) { // x boundaries only - BoutReal delta = bndry->bx * metric->dx(bndry->x, bndry->y); + BoutReal delta = bndry->bx * metric->dx()(bndry->x, bndry->y); f(bndry->x, bndry->y) = f(bndry->x - bndry->bx, bndry->y) + delta / g11shift * (val - xshift); if (bndry->bx == 2) { @@ -1628,7 +1628,7 @@ void BoundaryNeumann_NonOrthogonal::apply(Field2D& f) { } else if (bndry->by != 0 && bndry->bx == 0) { // y boundaries only // no need to shift this b/c we want parallel nuemann not theta - BoutReal delta = bndry->by * metric->dy(bndry->x, bndry->y); + BoutReal delta = bndry->by * metric->dy()(bndry->x, bndry->y); f(bndry->x, bndry->y) = f(bndry->x, bndry->y - bndry->by) + delta * val; if (bndry->width == 2) { f(bndry->x, bndry->y + bndry->by) = @@ -1650,7 +1650,7 @@ void BoundaryNeumann_NonOrthogonal::apply(Field2D& f) { void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { Mesh* mesh = bndry->localmesh; - ASSERT1(mesh == f.getMesh()); + ASSERT1(mesh == f.getMesh()) Coordinates* metric = f.getCoordinates(); // Calculate derivatives for metric use @@ -1685,7 +1685,7 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { + g13shift * dfdz(bndry->x - bndry->bx, bndry->y, z); if (bndry->bx != 0 && bndry->by == 0) { // x boundaries only - BoutReal delta = bndry->bx * metric->dx(bndry->x, bndry->y, z); + BoutReal delta = bndry->bx * metric->dx()(bndry->x, bndry->y, z); f(bndry->x, bndry->y, z) = f(bndry->x - bndry->bx, bndry->y, z) + delta / g11shift * (val - xshift); if (bndry->width == 2) { @@ -1696,7 +1696,7 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { } else if (bndry->by != 0 && bndry->bx == 0) { // y boundaries only // no need to shift this b/c we want parallel nuemann not theta - BoutReal delta = bndry->by * metric->dy(bndry->x, bndry->y, z); + BoutReal delta = bndry->by * metric->dy()(bndry->x, bndry->y, z); f(bndry->x, bndry->y, z) = f(bndry->x, bndry->y - bndry->by, z) + delta * val; if (bndry->width == 2) { f(bndry->x, bndry->y + bndry->by, z) = @@ -1736,7 +1736,7 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { #if not(BOUT_USE_METRIC_3D) Mesh* mesh = bndry->localmesh; - ASSERT1(mesh == f.getMesh()); + ASSERT1(mesh == f.getMesh()) Coordinates* metric = f.getCoordinates(); bndry->first(); @@ -1767,7 +1767,7 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { if (fg) { val = fg->generate(Context(bndry, loc, t, mesh)) - * metric->dx(bndry->x, bndry->y); + * metric->dx()(bndry->x, bndry->y); } f(bndry->x, bndry->y) = (4. * f(bndry->x - bndry->bx, bndry->y) @@ -1793,7 +1793,7 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { if (fg) { val = fg->generate(Context(bndry, loc, t, mesh)) - * metric->dx(bndry->x, bndry->y); + * metric->dx()(bndry->x, bndry->y); } f(bndry->x - bndry->bx, bndry->y) = @@ -1819,8 +1819,8 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { // y boundaries for (bndry->first(); !bndry->isDone(); bndry->next1d()) { - BoutReal delta = bndry->bx * metric->dx(bndry->x, bndry->y) - + bndry->by * metric->dy(bndry->x, bndry->y); + BoutReal delta = bndry->bx * metric->dx()(bndry->x, bndry->y) + + bndry->by * metric->dy()(bndry->x, bndry->y); if (fg) { val = fg->generate(Context(bndry, loc, t, mesh)); @@ -1844,7 +1844,7 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { for (; !bndry->isDone(); bndry->next1d()) { if (fg) { val = fg->generate(Context(bndry, loc, t, mesh)) - * metric->dy(bndry->x, bndry->y); + * metric->dy()(bndry->x, bndry->y); } f(bndry->x, bndry->y) = (4. * f(bndry->x, bndry->y - bndry->by) - f(bndry->x, bndry->y - 2 * bndry->by) + 2. * val) @@ -1870,7 +1870,7 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { if (fg) { val = fg->generate(Context(bndry, loc, t, mesh)) - * metric->dy(bndry->x, bndry->y - bndry->by); + * metric->dy()(bndry->x, bndry->y - bndry->by); } f(bndry->x, bndry->y - bndry->by) = (4. * f(bndry->x, bndry->y - 2 * bndry->by) @@ -1894,8 +1894,8 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { if (bndry->bx != 0) { // x boundaries for (bndry->first(); !bndry->isDone(); bndry->next1d()) { - BoutReal delta = bndry->bx * metric->dx(bndry->x, bndry->y) - + bndry->by * metric->dy(bndry->x, bndry->y); + BoutReal delta = bndry->bx * metric->dx()(bndry->x, bndry->y) + + bndry->by * metric->dy()(bndry->x, bndry->y); if (fg) { val = fg->generate(Context(bndry, loc, t, mesh)); @@ -1915,8 +1915,8 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { // Non-staggered, standard case for (bndry->first(); !bndry->isDone(); bndry->next1d()) { - BoutReal delta = bndry->bx * metric->dx(bndry->x, bndry->y) - + bndry->by * metric->dy(bndry->x, bndry->y); + BoutReal delta = bndry->bx * metric->dx()(bndry->x, bndry->y) + + bndry->by * metric->dy()(bndry->x, bndry->y); if (fg) { val = fg->generate(Context(bndry, loc, t, mesh)); @@ -1940,7 +1940,7 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { void BoundaryNeumann::apply(Field3D & f, BoutReal t) { Mesh* mesh = bndry->localmesh; - ASSERT1(mesh == f.getMesh()); + ASSERT1(mesh == f.getMesh()) Coordinates* metric = f.getCoordinates(); bndry->first(); @@ -1970,7 +1970,7 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { for (int zk = 0; zk < mesh->LocalNz; zk++) { if (fg) { val = fg->generate(Context(bndry, zk, loc, t, mesh)) - * metric->dx(bndry->x, bndry->y, zk); + * metric->dx()(bndry->x, bndry->y, zk); } f(bndry->x, bndry->y, zk) = @@ -1999,7 +1999,7 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { for (int zk = 0; zk < mesh->LocalNz; zk++) { if (fg) { val = fg->generate(Context(bndry, zk, loc, t, mesh)) - * metric->dx(bndry->x - bndry->bx, bndry->y, zk); + * metric->dx()(bndry->x - bndry->bx, bndry->y, zk); } f(bndry->x - bndry->bx, bndry->y, zk) = @@ -2025,13 +2025,13 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { if (bndry->by != 0) { for (; !bndry->isDone(); bndry->next1d()) { #if not(BOUT_USE_METRIC_3D) - BoutReal delta = bndry->bx * metric->dx(bndry->x, bndry->y) - + bndry->by * metric->dy(bndry->x, bndry->y); + BoutReal delta = bndry->bx * metric->dx()(bndry->x, bndry->y) + + bndry->by * metric->dy()(bndry->x, bndry->y); #endif for (int zk = 0; zk < mesh->LocalNz; zk++) { #if BOUT_USE_METRIC_3D - BoutReal delta = bndry->bx * metric->dx(bndry->x, bndry->y, zk) - + bndry->by * metric->dy(bndry->x, bndry->y, zk); + BoutReal delta = bndry->bx * metric->dx()(bndry->x, bndry->y, zk) + + bndry->by * metric->dy()(bndry->x, bndry->y, zk); #endif if (fg) { val = fg->generate(Context(bndry, zk, loc, t, mesh)); @@ -2055,7 +2055,7 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { for (int zk = 0; zk < mesh->LocalNz; zk++) { if (fg) { val = fg->generate(Context(bndry, zk, loc, t, mesh)) - * metric->dy(bndry->x, bndry->y, zk); + * metric->dy()(bndry->x, bndry->y, zk); } f(bndry->x, bndry->y, zk) = (4. * f(bndry->x, bndry->y - bndry->by, zk) @@ -2083,7 +2083,7 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { for (int zk = 0; zk < mesh->LocalNz; zk++) { if (fg) { val = fg->generate(Context(bndry, zk, loc, t, mesh)) - * metric->dy(bndry->x, bndry->y - bndry->by, zk); + * metric->dy()(bndry->x, bndry->y - bndry->by, zk); } f(bndry->x, bndry->y - bndry->by, zk) = @@ -2111,13 +2111,13 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { for (; !bndry->isDone(); bndry->next1d()) { #if not(BOUT_USE_METRIC_3D) int zk = 0; - BoutReal delta = bndry->bx * metric->dx(bndry->x, bndry->y, zk) - + bndry->by * metric->dy(bndry->x, bndry->y, zk); + BoutReal delta = bndry->bx * metric->dx()(bndry->x, bndry->y, zk) + + bndry->by * metric->dy()(bndry->x, bndry->y, zk); #endif for (int zk = 0; zk < mesh->LocalNz; zk++) { #if BOUT_USE_METRIC_3D - BoutReal delta = bndry->bx * metric->dx(bndry->x, bndry->y, zk) - + bndry->by * metric->dy(bndry->x, bndry->y, zk); + BoutReal delta = bndry->bx * metric->dx()(bndry->x, bndry->y, zk) + + bndry->by * metric->dy()(bndry->x, bndry->y, zk); #endif if (fg) { val = fg->generate(Context(bndry, zk, loc, t, mesh)); @@ -2146,8 +2146,8 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { + mesh->GlobalY(bndry->y - bndry->by)); // the grid cell for (int zk = 0; zk < mesh->LocalNz; zk++) { - BoutReal delta = bndry->bx * metric->dx(bndry->x, bndry->y, zk) - + bndry->by * metric->dy(bndry->x, bndry->y, zk); + BoutReal delta = bndry->bx * metric->dx()(bndry->x, bndry->y, zk) + + bndry->by * metric->dy()(bndry->x, bndry->y, zk); if (fg) { val = fg->generate(xnorm, TWOPI * ynorm, TWOPI * (zk - 0.5) / (mesh->LocalNz), t); @@ -2168,11 +2168,11 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { for (; !bndry->isDone(); bndry->next1d()) { #if BOUT_USE_METRIC_3D for (int zk = 0; zk < mesh->LocalNz; zk++) { - BoutReal delta = bndry->bx * metric->dx(bndry->x, bndry->y, zk) - + bndry->by * metric->dy(bndry->x, bndry->y, zk); + BoutReal delta = bndry->bx * metric->dx()(bndry->x, bndry->y, zk) + + bndry->by * metric->dy()(bndry->x, bndry->y, zk); #else - BoutReal delta = bndry->bx * metric->dx(bndry->x, bndry->y) - + bndry->by * metric->dy(bndry->x, bndry->y); + BoutReal delta = bndry->bx * metric->dx()(bndry->x, bndry->y) + + bndry->by * metric->dy()(bndry->x, bndry->y); for (int zk = 0; zk < mesh->LocalNz; zk++) { #endif if (fg) { @@ -2199,7 +2199,7 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { void BoundaryNeumann::apply_ddt(Field3D & f) { Mesh* mesh = bndry->localmesh; - ASSERT1(mesh == f.getMesh()); + ASSERT1(mesh == f.getMesh()) Field3D* dt = f.timeDeriv(); for (bndry->first(); !bndry->isDone(); bndry->next()) { for (int z = 0; z < mesh->LocalNz; z++) { @@ -2225,7 +2225,7 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { void BoundaryNeumann_O4::apply(Field2D & f, BoutReal t) { #if not(BOUT_USE_METRIC_3D) Mesh* mesh = bndry->localmesh; - ASSERT1(mesh == f.getMesh()); + ASSERT1(mesh == f.getMesh()) // Set (at 4th order) the value at the mid-point between the guard cell and the grid // cell to be val N.B. Only first guard cells (closest to the grid) should ever be @@ -2250,8 +2250,8 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { Coordinates* coords = f.getCoordinates(); for (bndry->first(); !bndry->isDone(); bndry->next1d()) { - BoutReal delta = bndry->bx * coords->dx(bndry->x, bndry->y) - + bndry->by * coords->dy(bndry->x, bndry->y); + BoutReal delta = bndry->bx * coords->dx()(bndry->x, bndry->y) + + bndry->by * coords->dy()(bndry->x, bndry->y); if (fg) { val = fg->generate(Context(bndry, loc, t, mesh)); @@ -2281,7 +2281,7 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { void BoundaryNeumann_O4::apply(Field3D & f, BoutReal t) { Mesh* mesh = bndry->localmesh; - ASSERT1(mesh == f.getMesh()); + ASSERT1(mesh == f.getMesh()) bndry->first(); // Decide which generator to use @@ -2300,8 +2300,8 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { Coordinates* coords = f.getCoordinates(); for (; !bndry->isDone(); bndry->next1d()) { for (int zk = 0; zk < mesh->LocalNz; zk++) { - BoutReal delta = bndry->bx * coords->dx(bndry->x, bndry->y, zk) - + bndry->by * coords->dy(bndry->x, bndry->y, zk); + BoutReal delta = bndry->bx * coords->dx()(bndry->x, bndry->y, zk) + + bndry->by * coords->dy()(bndry->x, bndry->y, zk); if (fg) { val = fg->generate(Context(bndry, zk, loc, t, mesh)); } @@ -2332,7 +2332,7 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { void BoundaryNeumann_O4::apply_ddt(Field3D & f) { Mesh* mesh = bndry->localmesh; - ASSERT1(mesh == f.getMesh()); + ASSERT1(mesh == f.getMesh()) Field3D* dt = f.timeDeriv(); for (bndry->first(); !bndry->isDone(); bndry->next()) { for (int z = 0; z < mesh->LocalNz; z++) { @@ -2361,8 +2361,8 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { // grid cell to be val This sets the value of the co-ordinate derivative, i.e. DDX/DDY // not Grad_par/Grad_perp.x for (bndry->first(); !bndry->isDone(); bndry->next1d()) { - BoutReal delta = -(bndry->bx * metric->dx(bndry->x, bndry->y) - + bndry->by * metric->dy(bndry->x, bndry->y)); + BoutReal delta = -(bndry->bx * metric->dx()(bndry->x, bndry->y) + + bndry->by * metric->dy()(bndry->x, bndry->y)); f(bndry->x, bndry->y) = 12. * delta / 11. * val + 17. / 22. * f(bndry->x - bndry->bx, bndry->y - bndry->by) @@ -2388,15 +2388,15 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { void BoundaryNeumann_4thOrder::apply(Field3D & f) { Mesh* mesh = bndry->localmesh; - ASSERT1(mesh == f.getMesh()); + ASSERT1(mesh == f.getMesh()) Coordinates* metric = f.getCoordinates(); // Set (at 4th order) the gradient at the mid-point between the guard cell and the // grid cell to be val This sets the value of the co-ordinate derivative, i.e. DDX/DDY // not Grad_par/Grad_perp.x for (bndry->first(); !bndry->isDone(); bndry->next1d()) { for (int z = 0; z < mesh->LocalNz; z++) { - BoutReal delta = -(bndry->bx * metric->dx(bndry->x, bndry->y, z) - + bndry->by * metric->dy(bndry->x, bndry->y, z)); + BoutReal delta = -(bndry->bx * metric->dx()(bndry->x, bndry->y, z) + + bndry->by * metric->dy()(bndry->x, bndry->y, z)); f(bndry->x, bndry->y, z) = 12. * delta / 11. * val + 17. / 22. * f(bndry->x - bndry->bx, bndry->y - bndry->by, z) @@ -2424,7 +2424,7 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { void BoundaryNeumann_4thOrder::apply_ddt(Field3D & f) { Mesh* mesh = bndry->localmesh; - ASSERT1(mesh == f.getMesh()); + ASSERT1(mesh == f.getMesh()) Field3D* dt = f.timeDeriv(); for (bndry->first(); !bndry->isDone(); bndry->next()) { for (int z = 0; z < mesh->LocalNz; z++) { @@ -2462,7 +2462,7 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { void BoundaryNeumannPar::apply(Field3D & f) { Mesh* mesh = bndry->localmesh; - ASSERT1(mesh == f.getMesh()); + ASSERT1(mesh == f.getMesh()) Coordinates* metric = f.getCoordinates(); for (bndry->first(); !bndry->isDone(); bndry->next()) { for (int z = 0; z < mesh->LocalNz; z++) { @@ -2529,7 +2529,7 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { void BoundaryRobin::apply(Field3D & f) { Mesh* mesh = bndry->localmesh; - ASSERT1(mesh == f.getMesh()); + ASSERT1(mesh == f.getMesh()) if (fabs(bval) < 1.e-12) { for (bndry->first(); !bndry->isDone(); bndry->next()) { for (int z = 0; z < mesh->LocalNz; z++) { @@ -2564,7 +2564,7 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { void BoundaryConstGradient::apply(Field3D & f) { Mesh* mesh = bndry->localmesh; - ASSERT1(mesh == f.getMesh()); + ASSERT1(mesh == f.getMesh()) for (bndry->first(); !bndry->isDone(); bndry->next()) { for (int z = 0; z < mesh->LocalNz; z++) { f(bndry->x, bndry->y, z) = @@ -2610,10 +2610,10 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { for (bndry->first(); !bndry->isDone(); bndry->nextY()) { int x = bndry->x; int y = bndry->y; - BoutReal g = (f(x - bx, y) - f(x - 2 * bx, y)) / metric->dx(x - bx, y); + BoutReal g = (f(x - bx, y) - f(x - 2 * bx, y)) / metric->dx()(x - bx, y); // Loop in X towards edge of domain do { - f(x, y) = f(x - bx, y) + g * metric->dx(x, y); + f(x, y) = f(x - bx, y) + g * metric->dx()(x, y); bndry->nextX(); x = bndry->x; y = bndry->y; @@ -2628,7 +2628,7 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { void BoundaryZeroLaplace::apply(Field3D & f) { #if not(BOUT_USE_METRIC_3D) Mesh* mesh = bndry->localmesh; - ASSERT1(mesh == f.getMesh()); + ASSERT1(mesh == f.getMesh()) int ncz = mesh->LocalNz; Coordinates* metric = f.getCoordinates(); @@ -2666,7 +2666,7 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { // kz != 0 solution BoutReal coef = - -1.0 * sqrt(metric->g33()(x, y) / metric->g11()(x, y)) * metric->dx(x, y); + -1.0 * sqrt(metric->g33()(x, y) / metric->g11()(x, y)) * metric->dx()(x, y); for (int jz = 1; jz <= ncz / 2; jz++) { BoutReal kwave = jz * 2.0 * PI / metric->zlength()(x, y); // wavenumber in [rad^-1] @@ -2713,10 +2713,10 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { for (bndry->first(); !bndry->isDone(); bndry->nextY()) { int x = bndry->x; int y = bndry->y; - BoutReal g = (f(x - bx, y) - f(x - 2 * bx, y)) / metric->dx(x - bx, y); + BoutReal g = (f(x - bx, y) - f(x - 2 * bx, y)) / metric->dx()(x - bx, y); // Loop in X towards edge of domain do { - f(x, y) = f(x - bx, y) + g * metric->dx(x, y); + f(x, y) = f(x - bx, y) + g * metric->dx()(x, y); bndry->nextX(); x = bndry->x; y = bndry->y; @@ -2731,10 +2731,10 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { void BoundaryZeroLaplace2::apply(Field3D & f) { #if not(BOUT_USE_METRIC_3D) Mesh* mesh = bndry->localmesh; - ASSERT1(mesh == f.getMesh()); + ASSERT1(mesh == f.getMesh()) const int ncz = mesh->LocalNz; - ASSERT0(ncz % 2 == 0); // Allocation assumes even number + ASSERT0(ncz % 2 == 0) // Allocation assumes even number // allocate memory Array c0(ncz / 2 + 1), c1(ncz / 2 + 1), c2(ncz / 2 + 1); @@ -2840,7 +2840,7 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { } Mesh* mesh = bndry->localmesh; - ASSERT1(mesh == f.getMesh()); + ASSERT1(mesh == f.getMesh()) Coordinates* metric = f.getCoordinates(); int ncz = mesh->LocalNz; @@ -2858,7 +2858,7 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { rfft(f(x - bx, y), ncz, c0.begin()); rfft(f(x - 2 * bx, y), ncz, c1.begin()); rfft(f(x - 3 * bx, y), ncz, c2.begin()); - dcomplex k0lin = (c1[0] - c0[0]) / metric->dx(x - bx, y); // for kz=0 solution + dcomplex k0lin = (c1[0] - c0[0]) / metric->dx()(x - bx, y); // for kz=0 solution // Calculate Delp2 on point MXG+1 (and put into c1) for (int jz = 0; jz <= ncz / 2; jz++) { @@ -2876,12 +2876,12 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { // Loop in X towards edge of domain do { // kz = 0 solution - xpos -= metric->dx(x, y); + xpos -= metric->dx()(x, y); c2[0] = c0[0] + k0lin * xpos + 0.5 * c1[0] * xpos * xpos / metric->g11()(x - bx, y); // kz != 0 solution BoutReal coef = -1.0 * sqrt(metric->g33()(x - bx, y) / metric->g11()(x - bx, y)) - * metric->dx(x - bx, y); + * metric->dx()(x - bx, y); for (int jz = 1; jz <= ncz / 2; jz++) { BoutReal kwave = jz * 2.0 * PI / getUniform(metric->zlength()); // wavenumber in [rad^-1] @@ -2920,7 +2920,7 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { void BoundaryDivCurl::apply(Vector3D & var) { #if not(BOUT_USE_METRIC_3D) Mesh* mesh = bndry->localmesh; - ASSERT1(mesh == var.getMesh()); + ASSERT1(mesh == var.getMesh()) int jx, jy, jz, jzp, jzm; BoutReal tmp; @@ -2949,65 +2949,68 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { // dB_x / dy tmp = (var.x(jx - 1, jy + 1, jz) - var.x(jx - 1, jy - 1, jz)) - / (metric->dy(jx - 1, jy - 1) + metric->dy(jx - 1, jy)); + / (metric->dy()(jx - 1, jy - 1) + metric->dy()(jx - 1, jy)); var.y(jx, jy, jz) = var.y(jx - 2, jy, jz) - + (metric->dx(jx - 2, jy) + metric->dx(jx - 1, jy)) * tmp; + + (metric->dx()(jx - 2, jy) + metric->dx()(jx - 1, jy)) * tmp; if (mesh->xstart == 2) { // 4th order to get last point - var.y(jx + 1, jy, jz) = var.y(jx - 3, jy, jz) + 4. * metric->dx(jx, jy) * tmp; + var.y(jx + 1, jy, jz) = var.y(jx - 3, jy, jz) + 4. * metric->dx()(jx, jy) * tmp; } // dB_z / dx = dB_x / dz tmp = (var.x(jx - 1, jy, jzp) - var.x(jx - 1, jy, jzm)) - / (2. * metric->dz(jx - 1, jy)); + / (2. * metric->dz()(jx - 1, jy)); var.z(jx, jy, jz) = var.z(jx - 2, jy, jz) - + (metric->dx(jx - 2, jy) + metric->dx(jx - 1, jy)) * tmp; + + (metric->dx()(jx - 2, jy) + metric->dx()(jx - 1, jy)) * tmp; if (mesh->xstart == 2) { - var.z(jx + 1, jy, jz) = var.z(jx - 3, jy, jz) + 4. * metric->dx(jx, jy) * tmp; + var.z(jx + 1, jy, jz) = var.z(jx - 3, jy, jz) + 4. * metric->dx()(jx, jy) * tmp; } // d/dx( Jmetric->g11 B_x ) = - d/dx( Jmetric->g12 B_y + Jmetric->g13 B_z) // - d/dy( JB^y ) - d/dz( JB^z ) - tmp = - -(metric->J()(jx, jy) * metric->g12()(jx, jy) * var.y(jx, jy, jz) - + metric->J()(jx, jy) * metric->g13()(jx, jy) * var.z(jx, jy, jz) - - metric->J()(jx - 2, jy) * metric->g12()(jx - 2, jy) * var.y(jx - 2, jy, jz) - + metric->J()(jx - 2, jy) * metric->g13()(jx - 2, jy) * var.z(jx - 2, jy, jz)) - / (metric->dx(jx - 2, jy) - + metric->dx(jx - 1, jy)); // First term (d/dx) using vals calculated above - tmp -= (metric->J()(jx - 1, jy + 1) * metric->g12()(jx - 1, jy + 1) - * var.x(jx - 1, jy + 1, jz) - - metric->J()(jx - 1, jy - 1) * metric->g12()(jx - 1, jy - 1) - * var.x(jx - 1, jy - 1, jz) - + metric->J()(jx - 1, jy + 1) * metric->g22()(jx - 1, jy + 1) - * var.y(jx - 1, jy + 1, jz) - - metric->J()(jx - 1, jy - 1) * metric->g22()(jx - 1, jy - 1) - * var.y(jx - 1, jy - 1, jz) - + metric->J()(jx - 1, jy + 1) * metric->g23()(jx - 1, jy + 1) - * var.z(jx - 1, jy + 1, jz) - - metric->J()(jx - 1, jy - 1) * metric->g23()(jx - 1, jy - 1) - * var.z(jx - 1, jy - 1, jz)) - / (metric->dy(jx - 1, jy - 1) + metric->dy(jx - 1, jy)); // second (d/dy) + tmp = -(metric->J()(jx, jy) * metric->g12()(jx, jy) * var.y(jx, jy, jz) + + metric->J()(jx, jy) * metric->g13()(jx, jy) * var.z(jx, jy, jz) + - metric->J()(jx - 2, jy) * metric->g12()(jx - 2, jy) + * var.y(jx - 2, jy, jz) + + metric->J()(jx - 2, jy) * metric->g13()(jx - 2, jy) + * var.z(jx - 2, jy, jz)) + / (metric->dx()(jx - 2, jy) + + metric->dx()(jx - 1, + jy)); // First term (d/dx) using vals calculated above + tmp -= + (metric->J()(jx - 1, jy + 1) * metric->g12()(jx - 1, jy + 1) + * var.x(jx - 1, jy + 1, jz) + - metric->J()(jx - 1, jy - 1) * metric->g12()(jx - 1, jy - 1) + * var.x(jx - 1, jy - 1, jz) + + metric->J()(jx - 1, jy + 1) * metric->g22()(jx - 1, jy + 1) + * var.y(jx - 1, jy + 1, jz) + - metric->J()(jx - 1, jy - 1) * metric->g22()(jx - 1, jy - 1) + * var.y(jx - 1, jy - 1, jz) + + metric->J()(jx - 1, jy + 1) * metric->g23()(jx - 1, jy + 1) + * var.z(jx - 1, jy + 1, jz) + - metric->J()(jx - 1, jy - 1) * metric->g23()(jx - 1, jy - 1) + * var.z(jx - 1, jy - 1, jz)) + / (metric->dy()(jx - 1, jy - 1) + metric->dy()(jx - 1, jy)); // second (d/dy) tmp -= (metric->J()(jx - 1, jy) * metric->g13()(jx - 1, jy) * (var.x(jx - 1, jy, jzp) - var.x(jx - 1, jy, jzm)) + metric->J()(jx - 1, jy) * metric->g23()(jx - 1, jy) * (var.y(jx - 1, jy, jzp) - var.y(jx - 1, jy, jzm)) + metric->J()(jx - 1, jy) * metric->g33()(jx - 1, jy) * (var.z(jx - 1, jy, jzp) - var.z(jx - 1, jy, jzm))) - / (2. * metric->dz(jx - 1, jy)); + / (2. * metric->dz()(jx - 1, jy)); var.x(jx, jy, jz) = (metric->J()(jx - 2, jy) * metric->g11()(jx - 2, jy) * var.x(jx - 2, jy, jz) - + (metric->dx(jx - 2, jy) + metric->dx(jx - 1, jy)) * tmp) + + (metric->dx()(jx - 2, jy) + metric->dx()(jx - 1, jy)) * tmp) / metric->J()(jx, jy) * metric->g11()(jx, jy); if (mesh->xstart == 2) { var.x(jx + 1, jy, jz) = (metric->J()(jx - 3, jy) * metric->g11()(jx - 3, jy) * var.x(jx - 3, jy, jz) - + 4. * metric->dx(jx, jy) * tmp) + + 4. * metric->dx()(jx, jy) * tmp) / metric->J()(jx + 1, jy) * metric->g11()(jx + 1, jy); } } @@ -3067,7 +3070,7 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { // used Mesh* mesh = bndry->localmesh; - ASSERT1(mesh == f.getMesh()); + ASSERT1(mesh == f.getMesh()) bndry->first(); // Check for staggered grids @@ -3171,7 +3174,7 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { // order. Mesh* mesh = bndry->localmesh; - ASSERT1(mesh == f.getMesh()); + ASSERT1(mesh == f.getMesh()) bndry->first(); // Check for staggered grids @@ -3297,7 +3300,7 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { void BoundaryFree_O2::apply_ddt(Field3D & f) { Mesh* mesh = bndry->localmesh; - ASSERT1(mesh == f.getMesh()); + ASSERT1(mesh == f.getMesh()) Field3D* dt = f.timeDeriv(); for (bndry->first(); !bndry->isDone(); bndry->next()) { for (int z = 0; z < mesh->LocalNz; z++) { @@ -3322,7 +3325,7 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { void BoundaryFree_O3::apply(Field2D & f) { Mesh* mesh = bndry->localmesh; - ASSERT1(mesh == f.getMesh()); + ASSERT1(mesh == f.getMesh()) bndry->first(); // Check for staggered grids @@ -3433,7 +3436,7 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { // order. Mesh* mesh = bndry->localmesh; - ASSERT1(mesh == f.getMesh()); + ASSERT1(mesh == f.getMesh()) bndry->first(); // Check for staggered grids @@ -3566,7 +3569,7 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { void BoundaryFree_O3::apply_ddt(Field3D & f) { Mesh* mesh = bndry->localmesh; - ASSERT1(mesh == f.getMesh()); + ASSERT1(mesh == f.getMesh()) Field3D* dt = f.timeDeriv(); for (bndry->first(); !bndry->isDone(); bndry->next()) { for (int z = 0; z < mesh->LocalNz; z++) { @@ -3621,7 +3624,7 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { TRACE("BoundaryRelax::apply_ddt(Field3D)"); Mesh* mesh = bndry->localmesh; - ASSERT1(mesh == f.getMesh()); + ASSERT1(mesh == f.getMesh()) // Make a copy of f Field3D g = f; // NOTE: This is not very efficient... copying entire field @@ -3698,7 +3701,7 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { void BoundaryToFieldAligned::apply(Field2D & f, BoutReal t) { op->apply(f, t); } void BoundaryToFieldAligned::apply(Field3D & f, BoutReal t) { - ASSERT1(bndry->localmesh == f.getMesh()); + ASSERT1(bndry->localmesh == f.getMesh()) // NOTE: This is not very efficient... updating entire field f = fromFieldAligned(f); @@ -3717,7 +3720,7 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { void BoundaryToFieldAligned::apply_ddt(Field2D & f) { op->apply_ddt(f); } void BoundaryToFieldAligned::apply_ddt(Field3D & f) { - ASSERT1(bndry->localmesh == f.getMesh()); + ASSERT1(bndry->localmesh == f.getMesh()) f = fromFieldAligned(f); ddt(f) = fromFieldAligned(ddt(f)); @@ -3741,7 +3744,7 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { void BoundaryFromFieldAligned::apply(Field2D & f, BoutReal t) { op->apply(f, t); } void BoundaryFromFieldAligned::apply(Field3D & f, BoutReal t) { - ASSERT1(bndry->localmesh == f.getMesh()); + ASSERT1(bndry->localmesh == f.getMesh()) // NOTE: This is not very efficient... shifting entire field f = toFieldAligned(f); @@ -3760,7 +3763,7 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { void BoundaryFromFieldAligned::apply_ddt(Field2D & f) { op->apply_ddt(f); } void BoundaryFromFieldAligned::apply_ddt(Field3D & f) { - ASSERT1(bndry->localmesh == f.getMesh()); + ASSERT1(bndry->localmesh == f.getMesh()) f = toFieldAligned(f); ddt(f) = toFieldAligned(ddt(f)); diff --git a/src/mesh/difops.cxx b/src/mesh/difops.cxx index 2616353249..71985cd3ba 100644 --- a/src/mesh/difops.cxx +++ b/src/mesh/difops.cxx @@ -77,8 +77,8 @@ Field3D Grad_par(const Field3D& var, const std::string& method, CELL_LOC outloc) *******************************************************************************/ Field3D Grad_parP(const Field3D& apar, const Field3D& f) { - ASSERT1_FIELDS_COMPATIBLE(apar, f); - ASSERT1(f.hasParallelSlices()); + ASSERT1_FIELDS_COMPATIBLE(apar, f) + ASSERT1(f.hasParallelSlices()) Mesh* mesh = apar.getMesh(); @@ -95,8 +95,8 @@ Field3D Grad_parP(const Field3D& apar, const Field3D& f) { for (int y = 1; y <= mesh->LocalNy - 2; y++) { for (int z = 0; z < ncz; z++) { gys(x, y, z) = (f.yup()(x, y + 1, z) - f.ydown()(x, y - 1, z)) - / (0.5 * metric->dy(x, y + 1, z) + metric->dy(x, y, z) - + 0.5 * metric->dy(x, y - 1, z)); + / (0.5 * metric->dy()(x, y + 1, z) + metric->dy()(x, y, z) + + 0.5 * metric->dy()(x, y - 1, z)); } } } @@ -111,78 +111,78 @@ Field3D Grad_parP(const Field3D& apar, const Field3D& f) { // bx = -DDZ(apar) BoutReal bx = (apar(x, y, zm) - apar(x, y, zp)) - / (0.5 * metric->dz(x, y, zm) + metric->dz(x, y, z) - + 0.5 * metric->dz(x, y, zp)); + / (0.5 * metric->dz()(x, y, zm) + metric->dz()(x, y, z) + + 0.5 * metric->dz()(x, y, zp)); // bz = DDX(f) BoutReal bz = (apar(x + 1, y, z) - apar(x - 1, y, z)) - / (0.5 * metric->dx(x - 1, y, z) + metric->dx(x, y, z) - + 0.5 * metric->dx(x + 1, y, z)); + / (0.5 * metric->dx()(x - 1, y, z) + metric->dx()(x, y, z) + + 0.5 * metric->dx()(x + 1, y, z)); // Now calculate (bx*d/dx + by*d/dy + bz*d/dz) f // Length dl for predictor - BoutReal dl = fabs(metric->dx(x, y, z)) / (fabs(bx) + 1e-16); - dl = BOUTMIN(dl, fabs(metric->dy(x, y, z)) / (fabs(by) + 1e-16)); - dl = BOUTMIN(dl, fabs(metric->dz(x, y, z)) / (fabs(bz) + 1e-16)); + BoutReal dl = fabs(metric->dx()(x, y, z)) / (fabs(bx) + 1e-16); + dl = BOUTMIN(dl, fabs(metric->dy()(x, y, z)) / (fabs(by) + 1e-16)); + dl = BOUTMIN(dl, fabs(metric->dz()(x, y, z)) / (fabs(bz) + 1e-16)); BoutReal fp, fm; // X differencing - fp = - f(x + 1, y, z) - + (0.25 * dl / metric->dz(x, y, z)) * bz * (f(x + 1, y, zm) - f(x + 1, y, zp)) - - 0.5 * dl * by * gys(x + 1, y, z); + fp = f(x + 1, y, z) + + (0.25 * dl / metric->dz()(x, y, z)) * bz + * (f(x + 1, y, zm) - f(x + 1, y, zp)) + - 0.5 * dl * by * gys(x + 1, y, z); - fm = - f(x - 1, y, z) - + (0.25 * dl / metric->dz(x, y, z)) * bz * (f(x - 1, y, zm) - f(x - 1, y, zp)) - - 0.5 * dl * by * gys(x - 1, y, z); + fm = f(x - 1, y, z) + + (0.25 * dl / metric->dz()(x, y, z)) * bz + * (f(x - 1, y, zm) - f(x - 1, y, zp)) + - 0.5 * dl * by * gys(x - 1, y, z); result(x, y, z) = bx * (fp - fm) - / (0.5 * metric->dx(x - 1, y, z) + metric->dx(x, y, z) - + 0.5 * metric->dx(x + 1, y, z)); + / (0.5 * metric->dx()(x - 1, y, z) + metric->dx()(x, y, z) + + 0.5 * metric->dx()(x + 1, y, z)); // Z differencing - fp = - f(x, y, zp) - + (0.25 * dl / metric->dx(x, y, z)) * bx * (f(x - 1, y, zp) - f(x + 1, y, zp)) - - 0.5 * dl * by * gys(x, y, zp); + fp = f(x, y, zp) + + (0.25 * dl / metric->dx()(x, y, z)) * bx + * (f(x - 1, y, zp) - f(x + 1, y, zp)) + - 0.5 * dl * by * gys(x, y, zp); - fm = - f(x, y, zm) - + (0.25 * dl / metric->dx(x, y, z)) * bx * (f(x - 1, y, zm) - f(x + 1, y, zm)) - - 0.5 * dl * by * gys(x, y, zm); + fm = f(x, y, zm) + + (0.25 * dl / metric->dx()(x, y, z)) * bx + * (f(x - 1, y, zm) - f(x + 1, y, zm)) + - 0.5 * dl * by * gys(x, y, zm); result(x, y, z) += bz * (fp - fm) - / (0.5 * metric->dz(x, y, zm) + metric->dz(x, y, z) - + 0.5 * metric->dz(x, y, zp)); + / (0.5 * metric->dz()(x, y, zm) + metric->dz()(x, y, z) + + 0.5 * metric->dz()(x, y, zp)); // Y differencing fp = f.yup()(x, y + 1, z) - 0.5 * dl * bx * (f.yup()(x + 1, y + 1, z) - f.yup()(x - 1, y + 1, z)) - / (0.5 * metric->dx(x - 1, y, z) + metric->dx(x, y, z) - + 0.5 * metric->dx(x + 1, y, z)) + / (0.5 * metric->dx()(x - 1, y, z) + metric->dx()(x, y, z) + + 0.5 * metric->dx()(x + 1, y, z)) - + (0.25 * dl / metric->dz(x, y, z)) * bz + + (0.25 * dl / metric->dz()(x, y, z)) * bz * (f.yup()(x, y + 1, zm) - f.yup()(x, y + 1, zp)); fm = f.ydown()(x, y - 1, z) - 0.5 * dl * bx * (f.ydown()(x + 1, y - 1, z) - f.ydown()(x - 1, y - 1, z)) - / (0.5 * metric->dx(x - 1, y, z) + metric->dx(x, y, z) - + 0.5 * metric->dx(x + 1, y, z)) - + (0.25 * dl / metric->dz(x, y, z)) * bz + / (0.5 * metric->dx()(x - 1, y, z) + metric->dx()(x, y, z) + + 0.5 * metric->dx()(x + 1, y, z)) + + (0.25 * dl / metric->dz()(x, y, z)) * bz * (f.ydown()(x, y - 1, zm) - f.ydown()(x, y - 1, zp)); result(x, y, z) += by * (fp - fm) - / (0.5 * metric->dy(x, y - 1, z) + metric->dy(x, y, z) - + 0.5 * metric->dy(x, y + 1, z)); + / (0.5 * metric->dy()(x, y - 1, z) + metric->dy()(x, y, z) + + 0.5 * metric->dy()(x, y + 1, z)); } } } - ASSERT2(result.getLocation() == f.getLocation()); + ASSERT2(result.getLocation() == f.getLocation()) return result; } @@ -235,9 +235,9 @@ Field3D Div_par(const Field3D& f, const std::string& method, CELL_LOC outloc) { } Field3D Div_par(const Field3D& f, const Field3D& v) { - ASSERT1_FIELDS_COMPATIBLE(f, v); - ASSERT1(f.hasParallelSlices()); - ASSERT1(v.hasParallelSlices()); + ASSERT1_FIELDS_COMPATIBLE(f, v) + ASSERT1(f.hasParallelSlices()) + ASSERT1(v.hasParallelSlices()) // Parallel divergence, using velocities at cell boundaries // Note: Not guaranteed to be flux conservative @@ -268,7 +268,7 @@ Field3D Div_par(const Field3D& f, const Field3D& v) { / (sqrt(coord->g_22()(i, j, k)) + sqrt(coord->g_22()(i, j - 1, k))); result(i, j, k) = - (fluxRight - fluxLeft) / (coord->dy(i, j, k) * coord->J()(i, j, k)); + (fluxRight - fluxLeft) / (coord->dy()(i, j, k) * coord->J()(i, j, k)); } } } @@ -464,7 +464,7 @@ Coordinates::FieldMetric b0xGrad_dot_Grad(const Field2D& phi, const Field2D& A, outloc = A.getLocation(); } - ASSERT1(phi.getMesh() == A.getMesh()); + ASSERT1(phi.getMesh() == A.getMesh()) Coordinates* metric = phi.getCoordinates(outloc); @@ -480,7 +480,7 @@ Coordinates::FieldMetric b0xGrad_dot_Grad(const Field2D& phi, const Field2D& A, Coordinates::FieldMetric result = VDDX(vx, A, outloc) + VDDY(vy, A, outloc); result /= metric->J() * sqrt(metric->g_22()); - ASSERT1(result.getLocation() == outloc); + ASSERT1(result.getLocation() == outloc) #if BOUT_USE_TRACK result.name = "b0xGrad_dot_Grad(" + phi.name + "," + A.name + ")"; @@ -495,7 +495,7 @@ Field3D b0xGrad_dot_Grad(const Field2D& phi, const Field3D& A, CELL_LOC outloc) outloc = A.getLocation(); } - ASSERT1(phi.getMesh() == A.getMesh()); + ASSERT1(phi.getMesh() == A.getMesh()) Mesh* mesh = phi.getMesh(); @@ -525,7 +525,7 @@ Field3D b0xGrad_dot_Grad(const Field2D& phi, const Field3D& A, CELL_LOC outloc) result.name = "b0xGrad_dot_Grad(" + phi.name + "," + A.name + ")"; #endif - ASSERT2(result.getLocation() == outloc); + ASSERT2(result.getLocation() == outloc) return result; } @@ -537,7 +537,7 @@ Field3D b0xGrad_dot_Grad(const Field3D& p, const Field2D& A, CELL_LOC outloc) { outloc = A.getLocation(); } - ASSERT1(p.getMesh() == A.getMesh()); + ASSERT1(p.getMesh() == A.getMesh()) Coordinates* metric = p.getCoordinates(outloc); @@ -560,7 +560,7 @@ Field3D b0xGrad_dot_Grad(const Field3D& p, const Field2D& A, CELL_LOC outloc) { result.name = "b0xGrad_dot_Grad(" + p.name + "," + A.name + ")"; #endif - ASSERT2(result.getLocation() == outloc); + ASSERT2(result.getLocation() == outloc) return result; } @@ -572,7 +572,7 @@ Field3D b0xGrad_dot_Grad(const Field3D& phi, const Field3D& A, CELL_LOC outloc) outloc = A.getLocation(); } - ASSERT1(phi.getMesh() == A.getMesh()); + ASSERT1(phi.getMesh() == A.getMesh()) Mesh* mesh = phi.getMesh(); @@ -601,7 +601,7 @@ Field3D b0xGrad_dot_Grad(const Field3D& phi, const Field3D& A, CELL_LOC outloc) result.name = "b0xGrad_dot_Grad(" + phi.name + "," + A.name + ")"; #endif - ASSERT2(result.getLocation() == outloc); + ASSERT2(result.getLocation() == outloc) return result; } @@ -616,11 +616,11 @@ Coordinates::FieldMetric bracket(const Field2D& f, const Field2D& g, Solver* UNUSED(solver)) { TRACE("bracket(Field2D, Field2D)"); - ASSERT1_FIELDS_COMPATIBLE(f, g); + ASSERT1_FIELDS_COMPATIBLE(f, g) if (outloc == CELL_DEFAULT) { outloc = g.getLocation(); } - ASSERT1(outloc == g.getLocation()); + ASSERT1(outloc == g.getLocation()) Coordinates::FieldMetric result{emptyFrom(f)}; @@ -639,11 +639,11 @@ Field3D bracket(const Field3D& f, const Field2D& g, BRACKET_METHOD method, CELL_LOC outloc, Solver* solver) { TRACE("bracket(Field3D, Field2D)"); - ASSERT1_FIELDS_COMPATIBLE(f, g); + ASSERT1_FIELDS_COMPATIBLE(f, g) if (outloc == CELL_DEFAULT) { outloc = g.getLocation(); } - ASSERT1(outloc == g.getLocation()); + ASSERT1(outloc == g.getLocation()) [[maybe_unused]] Mesh* mesh = f.getMesh(); @@ -672,10 +672,10 @@ Field3D bracket(const Field3D& f, const Field2D& g, BRACKET_METHOD method, BoutReal gp, gm; // Vx = DDZ(f) - BoutReal vx = (f(x, y, zp) - f(x, y, zm)) / (2. * metric->dz(x, y, z)); + BoutReal vx = (f(x, y, zp) - f(x, y, zm)) / (2. * metric->dz()(x, y, z)); // Set stability condition - solver->setMaxTimestep(metric->dx(x, y, z) / (fabs(vx) + 1e-16)); + solver->setMaxTimestep(metric->dx()(x, y, z) / (fabs(vx) + 1e-16)); // X differencing if (vx > 0.0) { @@ -689,7 +689,7 @@ Field3D bracket(const Field3D& f, const Field2D& g, BRACKET_METHOD method, gm = g(x, y); } - result(x, y, z) = vx * (gp - gm) / metric->dx(x, y, z); + result(x, y, z) = vx * (gp - gm) / metric->dx()(x, y, z); } } } @@ -706,7 +706,7 @@ Field3D bracket(const Field3D& f, const Field2D& g, BRACKET_METHOD method, BOUT_FOR(j2D, result.getRegion2D("RGN_NOBNDRY")) { // Get constants for this iteration - const BoutReal spacingFactor = 1.0 / (12 * metric->dz[j2D] * metric->dx[j2D]); + const BoutReal spacingFactor = 1.0 / (12 * metric->dz()[j2D] * metric->dx()[j2D]); const int jy = j2D.y(), jx = j2D.x(); const int xm = jx - 1, xp = jx + 1; @@ -777,8 +777,8 @@ Field3D bracket(const Field3D& f, const Field2D& g, BRACKET_METHOD method, BOUT_OMP_PERF(parallel for) for (int jx = mesh->xstart; jx <= mesh->xend; jx++) { for (int jy = mesh->ystart; jy <= mesh->yend; jy++) { - const BoutReal partialFactor = 1.0 / (12 * metric->dz(jx, jy)); - const BoutReal spacingFactor = partialFactor / metric->dx(jx, jy); + const BoutReal partialFactor = 1.0 / (12 * metric->dz()(jx, jy)); + const BoutReal spacingFactor = partialFactor / metric->dx()(jx, jy); for (int jz = 0; jz < mesh->LocalNz; jz++) { const int jzp = jz + 1 < ncz ? jz + 1 : 0; // Above is alternative to const int jzp = (jz + 1) % ncz; @@ -828,7 +828,7 @@ Field3D bracket(const Field2D& f, const Field3D& g, BRACKET_METHOD method, CELL_LOC outloc, Solver* solver) { TRACE("bracket(Field2D, Field3D)"); - ASSERT1_FIELDS_COMPATIBLE(f, g); + ASSERT1_FIELDS_COMPATIBLE(f, g) if (outloc == CELL_DEFAULT) { outloc = g.getLocation(); } @@ -865,11 +865,11 @@ Field3D bracket(const Field3D& f, const Field3D& g, BRACKET_METHOD method, CELL_LOC outloc, [[maybe_unused]] Solver* solver) { TRACE("Field3D, Field3D"); - ASSERT1_FIELDS_COMPATIBLE(f, g); + ASSERT1_FIELDS_COMPATIBLE(f, g) if (outloc == CELL_DEFAULT) { outloc = g.getLocation(); } - ASSERT1(outloc == g.getLocation()); + ASSERT1(outloc == g.getLocation()) Mesh* mesh = f.getMesh(); @@ -909,15 +909,15 @@ Field3D bracket(const Field3D& f, const Field3D& g, BRACKET_METHOD method, int zp = (z + 1) % ncz; // Vx = DDZ(f) - vx(x, z) = (f(x, y, zp) - f(x, y, zm)) / (2. * metric->dz(x, y, z)); + vx(x, z) = (f(x, y, zp) - f(x, y, zm)) / (2. * metric->dz()(x, y, z)); // Vz = -DDX(f) vz(x, z) = (f(x - 1, y, z) - f(x + 1, y, z)) - / (0.5 * metric->dx(x - 1, y) + metric->dx(x, y) - + 0.5 * metric->dx(x + 1, y)); + / (0.5 * metric->dx()(x - 1, y) + metric->dx()(x, y) + + 0.5 * metric->dx()(x + 1, y)); // Set stability condition - solver->setMaxTimestep(fabs(metric->dx(x, y)) / (fabs(vx(x, z)) + 1e-16)); - solver->setMaxTimestep(metric->dz(x, y) / (fabs(vz(x, z)) + 1e-16)); + solver->setMaxTimestep(fabs(metric->dx()(x, y)) / (fabs(vx(x, z)) + 1e-16)); + solver->setMaxTimestep(metric->dz()(x, y) / (fabs(vz(x, z)) + 1e-16)); } } @@ -933,53 +933,53 @@ Field3D bracket(const Field3D& f, const Field3D& g, BRACKET_METHOD method, // X differencing if (vx(x, z) > 0.0) { gp = g(x, y, z) - + (0.5 * dt / metric->dz(x, y)) + + (0.5 * dt / metric->dz()(x, y)) * ((vz(x, z) > 0) ? vz(x, z) * (g(x, y, zm) - g(x, y, z)) : vz(x, z) * (g(x, y, z) - g(x, y, zp))); gm = g(x - 1, y, z) - + (0.5 * dt / metric->dz(x, y)) + + (0.5 * dt / metric->dz()(x, y)) * ((vz(x, z) > 0) ? vz(x, z) * (g(x - 1, y, zm) - g(x - 1, y, z)) : vz(x, z) * (g(x - 1, y, z) - g(x - 1, y, zp))); } else { gp = g(x + 1, y, z) - + (0.5 * dt / metric->dz(x, y)) + + (0.5 * dt / metric->dz()(x, y)) * ((vz(x, z) > 0) ? vz(x, z) * (g(x + 1, y, zm) - g(x + 1, y, z)) : vz[x][z] * (g(x + 1, y, z) - g(x + 1, y, zp))); gm = g(x, y, z) - + (0.5 * dt / metric->dz(x, y)) + + (0.5 * dt / metric->dz()(x, y)) * ((vz(x, z) > 0) ? vz(x, z) * (g(x, y, zm) - g(x, y, z)) : vz(x, z) * (g(x, y, z) - g(x, y, zp))); } - result(x, y, z) = vx(x, z) * (gp - gm) / metric->dx(x, y); + result(x, y, z) = vx(x, z) * (gp - gm) / metric->dx()(x, y); // Z differencing if (vz(x, z) > 0.0) { gp = g(x, y, z) - + (0.5 * dt / metric->dx(x, y)) + + (0.5 * dt / metric->dx()(x, y)) * ((vx[x][z] > 0) ? vx[x][z] * (g(x - 1, y, z) - g(x, y, z)) : vx[x][z] * (g(x, y, z) - g(x + 1, y, z))); gm = g(x, y, zm) - + (0.5 * dt / metric->dx(x, y)) + + (0.5 * dt / metric->dx()(x, y)) * ((vx(x, z) > 0) ? vx(x, z) * (g(x - 1, y, zm) - g(x, y, zm)) : vx(x, z) * (g(x, y, zm) - g(x + 1, y, zm))); } else { gp = g(x, y, zp) - + (0.5 * dt / metric->dx(x, y)) + + (0.5 * dt / metric->dx()(x, y)) * ((vx(x, z) > 0) ? vx(x, z) * (g(x - 1, y, zp) - g(x, y, zp)) : vx(x, z) * (g(x, y, zp) - g(x + 1, y, zp))); gm = g(x, y, z) - + (0.5 * dt / metric->dx(x, y)) + + (0.5 * dt / metric->dx()(x, y)) * ((vx(x, z) > 0) ? vx(x, z) * (g(x - 1, y, z) - g(x, y, z)) : vx(x, z) * (g(x, y, z) - g(x + 1, y, z))); } - result(x, y, z) += vz(x, z) * (gp - gm) / metric->dz(x, y); + result(x, y, z) += vz(x, z) * (gp - gm) / metric->dz()(x, y); } } } @@ -999,7 +999,7 @@ Field3D bracket(const Field3D& f, const Field3D& g, BRACKET_METHOD method, BOUT_FOR(j2D, result.getRegion2D("RGN_NOBNDRY")) { #if not(BOUT_USE_METRIC_3D) - const BoutReal spacingFactor = 1.0 / (12 * metric->dz[j2D] * metric->dx[j2D]); + const BoutReal spacingFactor = 1.0 / (12 * metric->dz()[j2D] * metric->dx()[j2D]); #endif const int jy = j2D.y(), jx = j2D.x(); const int xm = jx - 1, xp = jx + 1; @@ -1105,7 +1105,7 @@ Field3D bracket(const Field3D& f, const Field3D& g, BRACKET_METHOD method, for (int jy = mesh->ystart; jy <= mesh->yend; jy++) { #if not(BOUT_USE_METRIC_3D) const BoutReal spacingFactor = - 1.0 / (12 * metric->dz(jx, jy) * metric->dx(jx, jy)); + 1.0 / (12 * metric->dz()(jx, jy) * metric->dx()(jx, jy)); #endif const BoutReal* Fxm = f_temp(jx - 1, jy); const BoutReal* Fx = f_temp(jx, jy); @@ -1116,7 +1116,7 @@ Field3D bracket(const Field3D& f, const Field3D& g, BRACKET_METHOD method, for (int jz = 0; jz < mesh->LocalNz; jz++) { #if BOUT_USE_METRIC_3D const BoutReal spacingFactor = - 1.0 / (12 * metric->dz(jx, jy, jz) * metric->dx(jx, jy, jz)); + 1.0 / (12 * metric->dz()(jx, jy, jz) * metric->dx()(jx, jy, jz)); #endif const int jzp = jz + 1 < ncz ? jz + 1 : 0; // Above is alternative to const int jzp = (jz + 1) % ncz; diff --git a/src/mesh/fv_ops.cxx b/src/mesh/fv_ops.cxx index f67ce7bf20..7a18de4e4e 100644 --- a/src/mesh/fv_ops.cxx +++ b/src/mesh/fv_ops.cxx @@ -55,10 +55,11 @@ Field3D Div_a_Grad_perp(const Field3D& a, const Field3D& f) { * (coord->J()(i, j, k) * coord->g11()(i, j, k) + coord->J()(i + 1, j, k) * coord->g11()(i + 1, j, k)) * (f(i + 1, j, k) - f(i, j, k)) - / (coord->dx(i, j, k) + coord->dx(i + 1, j, k)); + / (coord->dx()(i, j, k) + coord->dx()(i + 1, j, k)); - result(i, j, k) += fout / (coord->dx(i, j, k) * coord->J()(i, j, k)); - result(i + 1, j, k) -= fout / (coord->dx(i + 1, j, k) * coord->J()(i + 1, j, k)); + result(i, j, k) += fout / (coord->dx()(i, j, k) * coord->J()(i, j, k)); + result(i + 1, j, k) -= + fout / (coord->dx()(i + 1, j, k) * coord->J()(i + 1, j, k)); } } } @@ -70,7 +71,7 @@ Field3D Div_a_Grad_perp(const Field3D& a, const Field3D& f) { // Requires previous communication of metrics // -- should insert communication here? if (!coord->g23().hasParallelSlices() || !coord->g_23().hasParallelSlices() - || !coord->dy.hasParallelSlices() || !coord->dz.hasParallelSlices() + || !coord->dy().hasParallelSlices() || !coord->dz().hasParallelSlices() || !coord->Bxy().hasParallelSlices() || !coord->J().hasParallelSlices()) { throw BoutException("metrics have no yup/down: Maybe communicate in init?"); } @@ -90,8 +91,8 @@ Field3D Div_a_Grad_perp(const Field3D& a, const Field3D& f) { const auto g23 = makeslices(metric_fci, coord->g23()); const auto g_23 = makeslices(metric_fci, coord->g_23()); const auto J = makeslices(metric_fci, coord->J()); - const auto dy = makeslices(metric_fci, coord->dy); - const auto dz = makeslices(metric_fci, coord->dz); + const auto dy = makeslices(metric_fci, coord->dy()); + const auto dz = makeslices(metric_fci, coord->dz()); const auto Bxy = makeslices(metric_fci, coord->Bxy()); // Result of the Y and Z fluxes @@ -183,7 +184,7 @@ const Field3D Div_par_K_Grad_par(const Field3D& Kin, const Field3D& fin, bool bndry_flux) { TRACE("FV::Div_par_K_Grad_par"); - ASSERT2(Kin.getLocation() == fin.getLocation()); + ASSERT2(Kin.getLocation() == fin.getLocation()) Mesh* mesh = Kin.getMesh(); @@ -211,30 +212,30 @@ const Field3D Div_par_K_Grad_par(const Field3D& Kin, const Field3D& fin, if (bndry_flux || mesh->periodicY(i.x()) || !mesh->lastY(i.x()) || (i.y() != mesh->yend)) { - BoutReal c = 0.5 * (K[i] + Kup[iyp]); // K at the upper boundary + BoutReal c = 0.5 * (K[i] + Kup[iyp]); // K at the upper boundary BoutReal J = 0.5 * (coord->J()[i] + coord->J()[iyp]); // Jacobian at boundary BoutReal g_22 = 0.5 * (coord->g_22()[i] + coord->g_22()[iyp]); - BoutReal gradient = 2. * (fup[iyp] - f[i]) / (coord->dy[i] + coord->dy[iyp]); + BoutReal gradient = 2. * (fup[iyp] - f[i]) / (coord->dy()[i] + coord->dy()[iyp]); BoutReal flux = c * J * gradient / g_22; - result[i] += flux / (coord->dy[i] * coord->J()[i]); + result[i] += flux / (coord->dy()[i] * coord->J()[i]); } // Calculate flux at lower surface if (bndry_flux || mesh->periodicY(i.x()) || !mesh->firstY(i.x()) || (i.y() != mesh->ystart)) { - BoutReal c = 0.5 * (K[i] + Kdown[iym]); // K at the lower boundary + BoutReal c = 0.5 * (K[i] + Kdown[iym]); // K at the lower boundary BoutReal J = 0.5 * (coord->J()[i] + coord->J()[iym]); // Jacobian at boundary BoutReal g_22 = 0.5 * (coord->g_22()[i] + coord->g_22()[iym]); - BoutReal gradient = 2. * (f[i] - fdown[iym]) / (coord->dy[i] + coord->dy[iym]); + BoutReal gradient = 2. * (f[i] - fdown[iym]) / (coord->dy()[i] + coord->dy()[iym]); BoutReal flux = c * J * gradient / g_22; - result[i] -= flux / (coord->dy[i] * coord->J()[i]); + result[i] -= flux / (coord->dy()[i] * coord->J()[i]); } } @@ -247,13 +248,13 @@ const Field3D Div_par_K_Grad_par(const Field3D& Kin, const Field3D& fin, } const Field3D D4DY4(const Field3D& d_in, const Field3D& f_in) { - ASSERT1_FIELDS_COMPATIBLE(d_in, f_in); + ASSERT1_FIELDS_COMPATIBLE(d_in, f_in) Mesh* mesh = d_in.getMesh(); Coordinates* coord = f_in.getCoordinates(); - ASSERT2(d_in.getDirectionY() == f_in.getDirectionY()); + ASSERT2(d_in.getDirectionY() == f_in.getDirectionY()) const bool are_unaligned = ((d_in.getDirectionY() == YDirectionType::Standard) and (f_in.getDirectionY() == YDirectionType::Standard)); @@ -283,7 +284,7 @@ const Field3D D4DY4(const Field3D& d_in, const Field3D& f_in) { for (int j = ystart; j <= yend; j++) { for (int k = 0; k < mesh->LocalNz; k++) { - BoutReal dy3 = SQ(coord->dy(i, j, k)) * coord->dy(i, j, k); + BoutReal dy3 = SQ(coord->dz()(i, j, k)) * coord->dz()(i, j, k); // 3rd derivative at upper boundary BoutReal d3fdy3 = @@ -293,8 +294,9 @@ const Field3D D4DY4(const Field3D& d_in, const Field3D& f_in) { BoutReal flux = 0.5 * (d(i, j, k) + d(i, j + 1, k)) * (coord->J()(i, j, k) + coord->J()(i, j + 1, k)) * d3fdy3; - result(i, j, k) += flux / (coord->J()(i, j, k) * coord->dy(i, j, k)); - result(i, j + 1, k) -= flux / (coord->J()(i, j + 1, k) * coord->dy(i, j + 1, k)); + result(i, j, k) += flux / (coord->J()(i, j, k) * coord->dz()(i, j, k)); + result(i, j + 1, k) -= + flux / (coord->J()(i, j + 1, k) * coord->dz()(i, j + 1, k)); } } } @@ -331,14 +333,14 @@ const Field3D D4DY4_Index(const Field3D& f_in, bool bndry_flux) { for (int k = 0; k < mesh->LocalNz; k++) { // Right boundary common factors - const BoutReal common_factor = 0.25 - * (coord->dy(i, j, k) + coord->dy(i, j + 1, k)) - * (coord->J()(i, j, j) + coord->J()(i, j + 1, k)); + const BoutReal common_factor = + 0.25 * (coord->dz()(i, j, k) + coord->dz()(i, j + 1, k)) + * (coord->J()(i, j, j) + coord->J()(i, j + 1, k)); const BoutReal factor_rc = - common_factor / (coord->J()(i, j, k) * coord->dy(i, j, k)); + common_factor / (coord->J()(i, j, k) * coord->dz()(i, j, k)); const BoutReal factor_rp = - common_factor / (coord->J()(i, j + 1, k) * coord->dy(i, j + 1, k)); + common_factor / (coord->J()(i, j + 1, k) * coord->dz()(i, j + 1, k)); // Not on domain boundary // 3rd derivative at right cell boundary @@ -355,14 +357,14 @@ const Field3D D4DY4_Index(const Field3D& f_in, bool bndry_flux) { for (int k = 0; k < mesh->LocalNz; k++) { // Right boundary common factors - const BoutReal common_factor = 0.25 - * (coord->dy(i, j, k) + coord->dy(i, j + 1, k)) - * (coord->J()(i, j, j) + coord->J()(i, j + 1, k)); + const BoutReal common_factor = + 0.25 * (coord->dz()(i, j, k) + coord->dz()(i, j + 1, k)) + * (coord->J()(i, j, j) + coord->J()(i, j + 1, k)); const BoutReal factor_rc = - common_factor / (coord->J()(i, j, k) * coord->dy(i, j, k)); + common_factor / (coord->J()(i, j, k) * coord->dz()(i, j, k)); const BoutReal factor_rp = - common_factor / (coord->J()(i, j + 1, k) * coord->dy(i, j + 1, k)); + common_factor / (coord->J()(i, j + 1, k) * coord->dz()(i, j + 1, k)); const BoutReal d3fdx3 = -((16. / 5) * 0.5 * (f(i, j + 1, k) + f(i, j, k)) // Boundary value f_b @@ -384,14 +386,14 @@ const Field3D D4DY4_Index(const Field3D& f_in, bool bndry_flux) { if (j != mesh->ystart || !has_lower_boundary) { for (int k = 0; k < mesh->LocalNz; k++) { - const BoutReal common_factor = 0.25 - * (coord->dy(i, j, k) + coord->dy(i, j + 1, k)) - * (coord->J()(i, j, k) + coord->J()(i, j - 1, k)); + const BoutReal common_factor = + 0.25 * (coord->dz()(i, j, k) + coord->dz()(i, j + 1, k)) + * (coord->J()(i, j, k) + coord->J()(i, j - 1, k)); const BoutReal factor_lc = - common_factor / (coord->J()(i, j, k) * coord->dy(i, j, k)); + common_factor / (coord->J()(i, j, k) * coord->dz()(i, j, k)); const BoutReal factor_lm = - common_factor / (coord->J()(i, j - 1, k) * coord->dy(i, j - 1, k)); + common_factor / (coord->J()(i, j - 1, k) * coord->dz()(i, j - 1, k)); // Not on a domain boundary const BoutReal d3fdx3 = @@ -403,14 +405,14 @@ const Field3D D4DY4_Index(const Field3D& f_in, bool bndry_flux) { } else { // On a domain (Y) boundary for (int k = 0; k < mesh->LocalNz; k++) { - const BoutReal common_factor = 0.25 - * (coord->dy(i, j, k) + coord->dy(i, j + 1, k)) - * (coord->J()(i, j, k) + coord->J()(i, j - 1, k)); + const BoutReal common_factor = + 0.25 * (coord->dz()(i, j, k) + coord->dz()(i, j + 1, k)) + * (coord->J()(i, j, k) + coord->J()(i, j - 1, k)); const BoutReal factor_lc = - common_factor / (coord->J()(i, j, k) * coord->dy(i, j, k)); + common_factor / (coord->J()(i, j, k) * coord->dz()(i, j, k)); const BoutReal factor_lm = - common_factor / (coord->J()(i, j - 1, k) * coord->dy(i, j - 1, k)); + common_factor / (coord->J()(i, j - 1, k) * coord->dz()(i, j - 1, k)); const BoutReal d3fdx3 = -(-(16. / 5) * 0.5 * (f(i, j - 1, k) + f(i, j, k)) // Boundary value f_b + 6. * f(i, j, k) // f_0 @@ -512,50 +514,50 @@ Field3D Div_Perp_Lap(const Field3D& a, const Field3D& f, CELL_LOC outloc) { BoutReal gR = (coords->g11()(i, j, k) + coords->g11()(i + 1, j, k)) * (f(i + 1, j, k) - f(i, j, k)) - / (coords->dx(i + 1, j, k) + coords->dx(i, j, k)) + / (coords->dx()(i + 1, j, k) + coords->dx()(i, j, k)) + 0.5 * (coords->g13()(i, j, k) + coords->g13()(i + 1, j, k)) * (f(i + 1, j, kp) - f(i + 1, j, km) + f(i, j, kp) - f(i, j, km)) - / (4. * coords->dz(i, j, k)); + / (4. * coords->dz()(i, j, k)); BoutReal gL = (coords->g11()(i - 1, j, k) + coords->g11()(i, j, k)) * (f(i, j, k) - f(i - 1, j, k)) - / (coords->dx(i - 1, j, k) + coords->dx(i, j, k)) + / (coords->dx()(i - 1, j, k) + coords->dx()(i, j, k)) + 0.5 * (coords->g13()(i - 1, j, k) + coords->g13()(i, j, k)) * (f(i - 1, j, kp) - f(i - 1, j, km) + f(i, j, kp) - f(i, j, km)) - / (4 * coords->dz(i, j, k)); + / (4 * coords->dz()(i, j, k)); BoutReal gD = coords->g13()(i, j, k) * (f(i + 1, j, km) - f(i - 1, j, km) + f(i + 1, j, k) - f(i - 1, j, k)) - / (4. * coords->dx(i, j, k)) - + coords->g33()(i, j, k) * (f(i, j, k) - f(i, j, km)) / coords->dz(i, j, k); + / (4. * coords->dx()(i, j, k)) + + coords->g33()(i, j, k) * (f(i, j, k) - f(i, j, km)) / coords->dz()(i, j, k); BoutReal gU = coords->g13()(i, j, k) * (f(i + 1, j, kp) - f(i - 1, j, kp) + f(i + 1, j, k) - f(i - 1, j, k)) - / (4. * coords->dx(i, j, k)) - + coords->g33()(i, j, k) * (f(i, j, kp) - f(i, j, k)) / coords->dz(i, j, k); + / (4. * coords->dx()(i, j, k)) + + coords->g33()(i, j, k) * (f(i, j, kp) - f(i, j, k)) / coords->dz()(i, j, k); // Flow right BoutReal flux = gR * 0.25 * (coords->J()(i + 1, j, k) + coords->J()(i, j, k)) * (a(i + 1, j, k) + a(i, j, k)); - result(i, j, k) += flux / (coords->dx(i, j, k) * coords->J()(i, j, k)); + result(i, j, k) += flux / (coords->dx()(i, j, k) * coords->J()(i, j, k)); // Flow left flux = gL * 0.25 * (coords->J()(i - 1, j, k) + coords->J()(i, j, k)) * (a(i - 1, j, k) + a(i, j, k)); - result(i, j, k) -= flux / (coords->dx(i, j, k) * coords->J()(i, j, k)); + result(i, j, k) -= flux / (coords->dx()(i, j, k) * coords->J()(i, j, k)); // Flow up flux = gU * 0.25 * (coords->J()(i, j, k) + coords->J()(i, j, kp)) * (a(i, j, k) + a(i, j, kp)); - result(i, j, k) += flux / (coords->dz(i, j, k) * coords->J()(i, j, k)); + result(i, j, k) += flux / (coords->dz()(i, j, k) * coords->J()(i, j, k)); // Flow down flux = gD * 0.25 * (coords->J()(i, j, km) + coords->J()(i, j, k)) * (a(i, j, km) + a(i, j, k)); - result(i, j, k) += flux / (coords->dz(i, j, k) * coords->J()(i, j, k)); + result(i, j, k) += flux / (coords->dz()(i, j, k) * coords->J()(i, j, k)); } } } diff --git a/src/mesh/parallel_boundary_op.cxx b/src/mesh/parallel_boundary_op.cxx index 8b2c294a4a..ecca6df1e0 100644 --- a/src/mesh/parallel_boundary_op.cxx +++ b/src/mesh/parallel_boundary_op.cxx @@ -47,7 +47,7 @@ void BoundaryOpPar_dirichlet::apply(Field3D& f, BoutReal t) { // Scale the field and normalise to the desired value BoutReal y_prime = bndry->length; - BoutReal f2 = (f(x, y, z) - value) * (coord.dy(x, y, z) - y_prime) / y_prime; + BoutReal f2 = (f(x, y, z) - value) * (coord.dy()(x, y, z) - y_prime) / y_prime; f_next(x, y + bndry->dir, z) = value - f2; } @@ -75,9 +75,9 @@ void BoundaryOpPar_dirichlet_O3::apply(Field3D& f, BoutReal t) { BoutReal fb = getValue(*bndry, t); BoutReal f1 = f_prev(x, y - bndry->dir, z); BoutReal f2 = f(x, y, z); - BoutReal l1 = coord.dy(x, y, z); + BoutReal l1 = coord.dy()(x, y, z); BoutReal l2 = bndry->length; - BoutReal l3 = coord.dy(x, y, z) - l2; + BoutReal l3 = coord.dy()(x, y, z) - l2; BoutReal denom = (l1 * l1 * l2 + l1 * l2 * l2); BoutReal term1 = (l2 * l2 * l3 + l2 * l3 * l3); @@ -110,7 +110,7 @@ void BoundaryOpPar_dirichlet_interp::apply(Field3D& f, BoutReal t) { BoutReal fs = getValue(*bndry, t); // Scale the field and normalise to the desired value - BoutReal dy = coord.dy(x, y, z); + BoutReal dy = coord.dy()(x, y, z); BoutReal s = bndry->length * dy; f_next(x, y + bndry->dir, z) = @@ -140,7 +140,7 @@ void BoundaryOpPar_neumann::apply(Field3D& f, BoutReal t) { // Generate the boundary value BoutReal value = getValue(*bndry, t); - BoutReal dy = coord.dy(x, y, z); + BoutReal dy = coord.dy()(x, y, z); f_next(x, y + bndry->dir, z) = f(x, y, z) + bndry->dir * value * dy; } diff --git a/src/physics/smoothing.cxx b/src/physics/smoothing.cxx index 4fe6876115..10d5b070be 100644 --- a/src/physics/smoothing.cxx +++ b/src/physics/smoothing.cxx @@ -359,7 +359,7 @@ BoutReal Vol_Integral(const Field2D& var) { BoutReal Int_Glb; Coordinates* metric = var.getCoordinates(); - auto result = metric->J() * var * metric->dx * metric->dy; + auto result = metric->J() * var * metric->dx() * metric->dy(); Int_Glb = Average_XY(result); Int_Glb *= static_cast( diff --git a/src/sys/derivs.cxx b/src/sys/derivs.cxx index a31bf93649..415c150671 100644 --- a/src/sys/derivs.cxx +++ b/src/sys/derivs.cxx @@ -73,7 +73,7 @@ Coordinates::FieldMetric DDX(const Field2D& f, CELL_LOC outloc, const std::strin Field3D DDY(const Field3D& f, CELL_LOC outloc, const std::string& method, const std::string& region) { return bout::derivatives::index::DDY(f, outloc, method, region) - / f.getCoordinates(outloc)->dy; + / f.getCoordinates(outloc)->dy(); } Coordinates::FieldMetric DDY(const Field2D& f, CELL_LOC outloc, const std::string& method, @@ -86,7 +86,7 @@ Coordinates::FieldMetric DDY(const Field2D& f, CELL_LOC outloc, const std::strin Field3D DDZ(const Field3D& f, CELL_LOC outloc, const std::string& method, const std::string& region) { return bout::derivatives::index::DDZ(f, outloc, method, region) - / f.getCoordinates(outloc)->dz; + / f.getCoordinates(outloc)->dz(); } Coordinates::FieldMetric DDZ(const Field2D& f, CELL_LOC UNUSED(outloc), @@ -155,12 +155,12 @@ Field3D D2DX2(const Field3D& f, CELL_LOC outloc, const std::string& method, Coordinates* coords = f.getCoordinates(outloc); Field3D result = - bout::derivatives::index::D2DX2(f, outloc, method, region) / SQ(coords->dx); + bout::derivatives::index::D2DX2(f, outloc, method, region) / SQ(coords->dx()); if (coords->non_uniform) { // Correction for non-uniform f.getMesh() result += coords->d1_dx * bout::derivatives::index::DDX(f, outloc, "DEFAULT", region) - / coords->dx; + / coords->dx(); } ASSERT2(((outloc == CELL_DEFAULT) && (result.getLocation() == f.getLocation())) @@ -174,12 +174,12 @@ Coordinates::FieldMetric D2DX2(const Field2D& f, CELL_LOC outloc, Coordinates* coords = f.getCoordinates(outloc); auto result = - bout::derivatives::index::D2DX2(f, outloc, method, region) / SQ(coords->dx); + bout::derivatives::index::D2DX2(f, outloc, method, region) / SQ(coords->dx()); if (coords->non_uniform) { // Correction for non-uniform f.getMesh() result += coords->d1_dx * bout::derivatives::index::DDX(f, outloc, "DEFAULT", region) - / coords->dx; + / coords->dx(); } return result; @@ -192,12 +192,12 @@ Field3D D2DY2(const Field3D& f, CELL_LOC outloc, const std::string& method, Coordinates* coords = f.getCoordinates(outloc); Field3D result = - bout::derivatives::index::D2DY2(f, outloc, method, region) / SQ(coords->dy); + bout::derivatives::index::D2DY2(f, outloc, method, region) / SQ(coords->dy()); if (coords->non_uniform) { // Correction for non-uniform f.getMesh() result += coords->d1_dy * bout::derivatives::index::DDY(f, outloc, "DEFAULT", region) - / coords->dy; + / coords->dy(); } ASSERT2(((outloc == CELL_DEFAULT) && (result.getLocation() == f.getLocation())) @@ -211,11 +211,11 @@ Coordinates::FieldMetric D2DY2(const Field2D& f, CELL_LOC outloc, Coordinates* coords = f.getCoordinates(outloc); auto result = - bout::derivatives::index::D2DY2(f, outloc, method, region) / SQ(coords->dy); + bout::derivatives::index::D2DY2(f, outloc, method, region) / SQ(coords->dy()); if (coords->non_uniform) { // Correction for non-uniform f.getMesh() result += coords->d1_dy * bout::derivatives::index::DDY(f, outloc, "DEFAULT", region) - / coords->dy; + / coords->dy(); } return result; @@ -226,13 +226,13 @@ Coordinates::FieldMetric D2DY2(const Field2D& f, CELL_LOC outloc, Field3D D2DZ2(const Field3D& f, CELL_LOC outloc, const std::string& method, const std::string& region) { return bout::derivatives::index::D2DZ2(f, outloc, method, region) - / SQ(f.getCoordinates(outloc)->dz); + / SQ(f.getCoordinates(outloc)->dz()); } Coordinates::FieldMetric D2DZ2(const Field2D& f, CELL_LOC outloc, const std::string& method, const std::string& region) { return bout::derivatives::index::D2DZ2(f, outloc, method, region) - / SQ(f.getCoordinates(outloc)->dz); + / SQ(f.getCoordinates(outloc)->dz()); } /******************************************************************************* @@ -242,37 +242,37 @@ Coordinates::FieldMetric D2DZ2(const Field2D& f, CELL_LOC outloc, Field3D D4DX4(const Field3D& f, CELL_LOC outloc, const std::string& method, const std::string& region) { return bout::derivatives::index::D4DX4(f, outloc, method, region) - / SQ(SQ(f.getCoordinates(outloc)->dx)); + / SQ(SQ(f.getCoordinates(outloc)->dx())); } Coordinates::FieldMetric D4DX4(const Field2D& f, CELL_LOC outloc, const std::string& method, const std::string& region) { return bout::derivatives::index::D4DX4(f, outloc, method, region) - / SQ(SQ(f.getCoordinates(outloc)->dx)); + / SQ(SQ(f.getCoordinates(outloc)->dx())); } Field3D D4DY4(const Field3D& f, CELL_LOC outloc, const std::string& method, const std::string& region) { return bout::derivatives::index::D4DY4(f, outloc, method, region) - / SQ(SQ(f.getCoordinates(outloc)->dy)); + / SQ(SQ(f.getCoordinates(outloc)->dy())); } Coordinates::FieldMetric D4DY4(const Field2D& f, CELL_LOC outloc, const std::string& method, const std::string& region) { return bout::derivatives::index::D4DY4(f, outloc, method, region) - / SQ(SQ(f.getCoordinates(outloc)->dy)); + / SQ(SQ(f.getCoordinates(outloc)->dy())); } Field3D D4DZ4(const Field3D& f, CELL_LOC outloc, const std::string& method, const std::string& region) { return bout::derivatives::index::D4DZ4(f, outloc, method, region) - / SQ(SQ(f.getCoordinates(outloc)->dz)); + / SQ(SQ(f.getCoordinates(outloc)->dz())); } Coordinates::FieldMetric D4DZ4(const Field2D& f, CELL_LOC outloc, const std::string& method, const std::string& region) { return bout::derivatives::index::D4DZ4(f, outloc, method, region) - / SQ(SQ(f.getCoordinates(outloc)->dz)); + / SQ(SQ(f.getCoordinates(outloc)->dz())); } /******************************************************************************* @@ -390,14 +390,14 @@ Field3D D2DYDZ(const Field3D& f, CELL_LOC outloc, Coordinates::FieldMetric VDDX(const Field2D& v, const Field2D& f, CELL_LOC outloc, const std::string& method, const std::string& region) { return bout::derivatives::index::VDDX(v, f, outloc, method, region) - / f.getCoordinates(outloc)->dx; + / f.getCoordinates(outloc)->dx(); } /// General version for 2 or 3-D objects Field3D VDDX(const Field3D& v, const Field3D& f, CELL_LOC outloc, const std::string& method, const std::string& region) { return bout::derivatives::index::VDDX(v, f, outloc, method, region) - / f.getCoordinates(outloc)->dx; + / f.getCoordinates(outloc)->dx(); } ////////////// Y DERIVATIVE ///////////////// @@ -406,14 +406,14 @@ Field3D VDDX(const Field3D& v, const Field3D& f, CELL_LOC outloc, Coordinates::FieldMetric VDDY(const Field2D& v, const Field2D& f, CELL_LOC outloc, const std::string& method, const std::string& region) { return bout::derivatives::index::VDDY(v, f, outloc, method, region) - / f.getCoordinates(outloc)->dy; + / f.getCoordinates(outloc)->dy(); } // general case Field3D VDDY(const Field3D& v, const Field3D& f, CELL_LOC outloc, const std::string& method, const std::string& region) { return bout::derivatives::index::VDDY(v, f, outloc, method, region) - / f.getCoordinates(outloc)->dy; + / f.getCoordinates(outloc)->dy(); } ////////////// Z DERIVATIVE ///////////////// @@ -422,7 +422,7 @@ Field3D VDDY(const Field3D& v, const Field3D& f, CELL_LOC outloc, Coordinates::FieldMetric VDDZ(const Field2D& v, const Field2D& f, CELL_LOC outloc, const std::string& method, const std::string& region) { return bout::derivatives::index::VDDZ(v, f, outloc, method, region) - / f.getCoordinates(outloc)->dz; + / f.getCoordinates(outloc)->dz(); } // Note that this is zero because no compression is included @@ -432,7 +432,7 @@ Coordinates::FieldMetric VDDZ([[maybe_unused]] const Field3D& v, const Field2D& #if BOUT_USE_METRIC_3D Field3D tmp{f}; return bout::derivatives::index::VDDZ(v, tmp, outloc, method, region) - / f.getCoordinates(outloc)->dz; + / f.getCoordinates(outloc)->dz(); #else if (outloc == CELL_DEFAULT) { outloc = f.getLocation(); @@ -445,7 +445,7 @@ Coordinates::FieldMetric VDDZ([[maybe_unused]] const Field3D& v, const Field2D& Field3D VDDZ(const Field3D& v, const Field3D& f, CELL_LOC outloc, const std::string& method, const std::string& region) { return bout::derivatives::index::VDDZ(v, f, outloc, method, region) - / f.getCoordinates(outloc)->dz; + / f.getCoordinates(outloc)->dz(); } /******************************************************************************* @@ -454,13 +454,13 @@ Field3D VDDZ(const Field3D& v, const Field3D& f, CELL_LOC outloc, Coordinates::FieldMetric FDDX(const Field2D& v, const Field2D& f, CELL_LOC outloc, const std::string& method, const std::string& region) { return bout::derivatives::index::FDDX(v, f, outloc, method, region) - / f.getCoordinates(outloc)->dx; + / f.getCoordinates(outloc)->dx(); } Field3D FDDX(const Field3D& v, const Field3D& f, CELL_LOC outloc, const std::string& method, const std::string& region) { return bout::derivatives::index::FDDX(v, f, outloc, method, region) - / f.getCoordinates(outloc)->dx; + / f.getCoordinates(outloc)->dx(); } ///////////////////////////////////////////////////////////////////////// @@ -468,13 +468,13 @@ Field3D FDDX(const Field3D& v, const Field3D& f, CELL_LOC outloc, Coordinates::FieldMetric FDDY(const Field2D& v, const Field2D& f, CELL_LOC outloc, const std::string& method, const std::string& region) { return bout::derivatives::index::FDDY(v, f, outloc, method, region) - / f.getCoordinates(outloc)->dy; + / f.getCoordinates(outloc)->dy(); } Field3D FDDY(const Field3D& v, const Field3D& f, CELL_LOC outloc, const std::string& method, const std::string& region) { return bout::derivatives::index::FDDY(v, f, outloc, method, region) - / f.getCoordinates(outloc)->dy; + / f.getCoordinates(outloc)->dy(); } ///////////////////////////////////////////////////////////////////////// @@ -482,11 +482,11 @@ Field3D FDDY(const Field3D& v, const Field3D& f, CELL_LOC outloc, Coordinates::FieldMetric FDDZ(const Field2D& v, const Field2D& f, CELL_LOC outloc, const std::string& method, const std::string& region) { return bout::derivatives::index::FDDZ(v, f, outloc, method, region) - / f.getCoordinates(outloc)->dz; + / f.getCoordinates(outloc)->dz(); } Field3D FDDZ(const Field3D& v, const Field3D& f, CELL_LOC outloc, const std::string& method, const std::string& region) { return bout::derivatives::index::FDDZ(v, f, outloc, method, region) - / f.getCoordinates(outloc)->dz; + / f.getCoordinates(outloc)->dz(); } diff --git a/tests/MMS/GBS/gbs.cxx b/tests/MMS/GBS/gbs.cxx index a296094af6..72dee35f17 100644 --- a/tests/MMS/GBS/gbs.cxx +++ b/tests/MMS/GBS/gbs.cxx @@ -290,14 +290,14 @@ int GBS::init(bool restarting) { phiSolver = Laplacian::create(opt->getSection("phiSolver")); aparSolver = Laplacian::create(opt->getSection("aparSolver")); - dx4 = SQ(SQ(coords->dx)); - dy4 = SQ(SQ(coords->dy)); - dz4 = SQ(SQ(coords->dz)); + dx4 = SQ(SQ(coords->dx())); + dy4 = SQ(SQ(coords->dy())); + dz4 = SQ(SQ(coords->dz())); SAVE_REPEAT(Ve); - output.write("dx = {:e}, dy = {:e}, dz = {:e}\n", (coords->dx)(2, 2), - (coords->dy)(2, 2), coords->dz); + output.write("dx = {:e}, dy = {:e}, dz = {:e}\n", (coords->dx())(2, 2), + (coords->dy())(2, 2), coords->dz()); output.write("g11 = {:e}, g22 = {:e}, g33 = {:e}\n", coords->g11()(2, 2), coords->g22()(2, 2), coords->g33()(2, 2)); output.write("g12 = {:e}, g23 = {:e}\n", coords->g12()(2, 2), coords->g23()(2, 2)); @@ -321,7 +321,7 @@ void GBS::LoadMetric(BoutReal Lnorm, BoutReal Bnorm) { Field2D dx; if (!mesh->get(dx, "dpsi")) { output << "\tUsing dpsi as the x grid spacing\n"; - coords->dx = dx; // Only use dpsi if found + coords->setDx(dx); // Only use dpsi if found } else { // dx will have been read already from the grid output << "\tUsing dx as the x grid spacing\n"; @@ -330,11 +330,11 @@ void GBS::LoadMetric(BoutReal Lnorm, BoutReal Bnorm) { Rxy /= Lnorm; hthe /= Lnorm; sinty *= SQ(Lnorm) * Bnorm; - coords->dx /= SQ(Lnorm) * Bnorm; + coords->setDx(coords->dx() / (SQ(Lnorm) * Bnorm)); Bpxy /= Bnorm; Btxy /= Bnorm; - coords->Bxy() /= Bnorm; + coords->setBxy(coords->Bxy() / Bnorm); // Calculate metric components bool ShiftXderivs; @@ -354,8 +354,7 @@ void GBS::LoadMetric(BoutReal Lnorm, BoutReal Bnorm) { const auto g12 = 0.0; const auto g13 = -sinty * coords->g11(); const auto g23 = -sbp * Btxy / (hthe * Bpxy * Rxy); - coords->setContravariantMetricTensor( - MetricTensor(g11, g22, g33, g12, g13, g23)); + coords->setContravariantMetricTensor(MetricTensor(g11, g22, g33, g12, g13, g23)); coords->setJ(hthe / Bpxy); @@ -365,10 +364,7 @@ void GBS::LoadMetric(BoutReal Lnorm, BoutReal Bnorm) { const auto g_12 = sbp * Btxy * hthe * sinty * Rxy / Bpxy; const auto g_13 = sinty * Rxy * Rxy; const auto g_23 = sbp * Btxy * hthe * Rxy / Bpxy; - coords->setCovariantMetricTensor( - MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); - - coords->geometry(); + coords->setCovariantMetricTensor(MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); } // just define a macro for V_E dot Grad @@ -433,7 +429,8 @@ int GBS::rhs(BoutReal t) { Ge = 0.0; if (elecvis) { Ge = -(0.73 * Te * Ne * tau_e) - * (2. * Grad_par(Ve) + (5. * C(Te) + 5. * Te * C(logNe) + C(phi)) / coords->Bxy()); + * (2. * Grad_par(Ve) + + (5. * C(Te) + 5. * Te * C(logNe) + C(phi)) / coords->Bxy()); mesh->communicate(Ge); Ge.applyBoundary("neumann"); } else { @@ -447,7 +444,7 @@ int GBS::rhs(BoutReal t) { if (evolve_Ne) { // Density - ddt(Ne) = -vE_Grad(Ne, phi) // ExB term + ddt(Ne) = -vE_Grad(Ne, phi) // ExB term + (2. / coords->Bxy()) * (C(Pe) - Ne * C(phi)) // Perpendicular compression + D(Ne, Dn) + H(Ne, Hn); diff --git a/tests/MMS/advection/advection.cxx b/tests/MMS/advection/advection.cxx index 1201fbc3ac..6b8510ec83 100644 --- a/tests/MMS/advection/advection.cxx +++ b/tests/MMS/advection/advection.cxx @@ -18,8 +18,8 @@ class AdvectMMS : public PhysicsModel { Coordinates* coords = mesh->getCoordinates(); - dx_sq_sq = SQ(SQ(coords->dx)); - dz_sq_sq = SQ(SQ(coords->dz)); + dx_sq_sq = SQ(SQ(coords->dx())); + dz_sq_sq = SQ(SQ(coords->dz())); return 0; } diff --git a/tests/MMS/diffusion/diffusion.cxx b/tests/MMS/diffusion/diffusion.cxx index 02d0e6fe80..eada9b1926 100644 --- a/tests/MMS/diffusion/diffusion.cxx +++ b/tests/MMS/diffusion/diffusion.cxx @@ -32,8 +32,8 @@ int Diffusion::init(bool UNUSED(restarting)) { /*this assumes equidistant grid*/ int nguard = mesh->xstart; - coord->dx = Lx / (mesh->GlobalNx - 2 * nguard); - coord->dy = Ly / (mesh->GlobalNy - 2 * nguard); + coord->setDx(Lx / (mesh->GlobalNx - 2 * nguard)); + coord->setDy(Ly / (mesh->GlobalNy - 2 * nguard)); SAVE_ONCE2(Lx, Ly); diff --git a/tests/MMS/diffusion2/diffusion.cxx b/tests/MMS/diffusion2/diffusion.cxx index c7508b558d..70010b0c49 100644 --- a/tests/MMS/diffusion2/diffusion.cxx +++ b/tests/MMS/diffusion2/diffusion.cxx @@ -21,12 +21,12 @@ class Diffusion : public PhysicsModel { meshoptions->get("Ly", Ly, 1.0); /*this assumes equidistant grid*/ - coords->dx = Lx / (mesh->GlobalNx - 2 * mesh->xstart); + coords->setDx(Lx / (mesh->GlobalNx - 2 * mesh->xstart)); - coords->dy = Ly / (mesh->GlobalNy - 2 * mesh->ystart); + coords->setDy(Ly / (mesh->GlobalNy - 2 * mesh->ystart)); output.write("SIZES: {:d}, {:d}, {:e}\n", mesh->GlobalNy, - (mesh->GlobalNy - 2 * mesh->ystart), coords->dy(0, 0, 0)); + (mesh->GlobalNy - 2 * mesh->ystart), coords->dy()(0, 0, 0)); SAVE_ONCE2(Lx, Ly); diff --git a/tests/MMS/elm-pb/elm_pb.cxx b/tests/MMS/elm-pb/elm_pb.cxx index 52c5cade55..81db0d4eb0 100644 --- a/tests/MMS/elm-pb/elm_pb.cxx +++ b/tests/MMS/elm-pb/elm_pb.cxx @@ -389,7 +389,7 @@ class ELMpb : public PhysicsModel { Btxy /= Bbar; B0 /= Bbar; hthe /= Lbar; - coords->dx /= Lbar * Lbar * Bbar; + coords->setDx(coords->dx() / (Lbar * Lbar * Bbar)); I *= Lbar * Lbar * Bbar; BoutReal pnorm = max(P0, true); // Maximum over all processors @@ -638,7 +638,7 @@ class ELMpb : public PhysicsModel { ddt(U) += viscos_perp * Delp2(U); // Perpendicular viscosity } - ddt(U) -= 10 * (SQ(SQ(coords->dx)) * D4DX4(U) + SQ(SQ(coords->dz)) * D4DZ4(U)); + ddt(U) -= 10 * (SQ(SQ(coords->dx())) * D4DX4(U) + SQ(SQ(coords->dz())) * D4DZ4(U)); //////////////////////////////////////////////////// // Pressure equation @@ -658,7 +658,7 @@ class ELMpb : public PhysicsModel { ddt(P) += diffusion_par * Grad2_par2(P); // Parallel diffusion } - ddt(P) -= 10 * (SQ(SQ(coords->dx)) * D4DX4(P) + SQ(SQ(coords->dz)) * D4DZ4(P)); + ddt(P) -= 10 * (SQ(SQ(coords->dx())) * D4DX4(P) + SQ(SQ(coords->dz())) * D4DZ4(P)); //////////////////////////////////////////////////// // Compressional effects diff --git a/tests/MMS/fieldalign/fieldalign.cxx b/tests/MMS/fieldalign/fieldalign.cxx index 5793eb9d0e..715cb616b9 100644 --- a/tests/MMS/fieldalign/fieldalign.cxx +++ b/tests/MMS/fieldalign/fieldalign.cxx @@ -28,8 +28,8 @@ class FieldAlign : public PhysicsModel { vz / G * (metric->g13() * DDX(f) + metric->g23() * DDY(f) + metric->g33() * DDZ(f)); // (unstable without additional dissipation) - -SQ(SQ(metric->dx)) * D4DX4(f) /*- SQ(SQ(metric->dy))*D4DY4(f)*/ - - SQ(SQ(metric->dz)) * D4DZ4(f); // Numerical dissipation terms + -SQ(SQ(metric->dx())) * D4DX4(f) /*- SQ(SQ(metric->dy()))*D4DY4(f)*/ + - SQ(SQ(metric->dz())) * D4DZ4(f); // Numerical dissipation terms return 0; } diff --git a/tests/MMS/hw/hw.cxx b/tests/MMS/hw/hw.cxx index c5dd25773f..b14dc7fb47 100644 --- a/tests/MMS/hw/hw.cxx +++ b/tests/MMS/hw/hw.cxx @@ -39,12 +39,12 @@ class Hw : public PhysicsModel { /*this assumes equidistant grid*/ int nguard = mesh->xstart; - mesh->getCoordinates()->dx = Lx / (mesh->GlobalNx - 2 * nguard); - mesh->getCoordinates()->dz = TWOPI * Lx / (mesh->LocalNz); + mesh->getCoordinates()->setDx(Lx / (mesh->GlobalNx - 2 * nguard)); + mesh->getCoordinates()->setDz(TWOPI * Lx / (mesh->LocalNz)); ///// - SOLVE_FOR2(n, vort); - SAVE_REPEAT(phi); + SOLVE_FOR2(n, vort) + SAVE_REPEAT(phi) phiSolver = Laplacian::create(); phi = 0.; // Starting phi diff --git a/tests/MMS/laplace/laplace.cxx b/tests/MMS/laplace/laplace.cxx index 54dbaaba67..475725f269 100644 --- a/tests/MMS/laplace/laplace.cxx +++ b/tests/MMS/laplace/laplace.cxx @@ -22,8 +22,8 @@ int main(int argc, char** argv) { /*this assumes equidistant grid*/ int nguard = mesh->xstart; - mesh->getCoordinates()->dx = Lx / (mesh->GlobalNx - 2 * nguard); - mesh->getCoordinates()->dz = TWOPI * Lx / (mesh->LocalNz); + mesh->getCoordinates()->setDx(Lx / (mesh->GlobalNx - 2 * nguard)); + mesh->getCoordinates()->setDz(TWOPI * Lx / (mesh->LocalNz)); ///// // Create a Laplacian inversion solver diff --git a/tests/MMS/spatial/diffusion/diffusion.cxx b/tests/MMS/spatial/diffusion/diffusion.cxx index 4034255ae4..f5d20324f6 100644 --- a/tests/MMS/spatial/diffusion/diffusion.cxx +++ b/tests/MMS/spatial/diffusion/diffusion.cxx @@ -22,34 +22,31 @@ class Diffusion : public PhysicsModel { meshoptions->get("Ly", Ly, 1.0); /*this assumes equidistant grid*/ - coords->dx = Lx / (mesh->GlobalNx - 2 * mesh->xstart); + coords->setDx(Lx / (mesh->GlobalNx - 2 * mesh->xstart)); - coords->dy = Ly / (mesh->GlobalNy - 2 * mesh->ystart); + coords->setDy(Ly / (mesh->GlobalNy - 2 * mesh->ystart)); output.write("SIZES: {:d}, {:d}, {:e}\n", mesh->GlobalNy, - (mesh->GlobalNy - 2 * mesh->ystart), coords->dy(0, 0, 0)); + (mesh->GlobalNy - 2 * mesh->ystart), coords->dy()(0, 0, 0)); - SAVE_ONCE2(Lx, Ly); + SAVE_ONCE2(Lx, Ly) Options* cytooptions = Options::getRoot()->getSection("cyto"); OPTION(cytooptions, Dx, 1.0); OPTION(cytooptions, Dy, -1.0); OPTION(cytooptions, Dz, -1.0); - SAVE_ONCE3(Dx, Dy, Dz); + SAVE_ONCE3(Dx, Dy, Dz) // set mesh - auto contravariant_metric_tensor = - MetricTensor(1.1, 1.1, 1.1, 0.0, 0.0, 0.0); + auto contravariant_metric_tensor = MetricTensor(1.1, 1.1, 1.1, 0.0, 0.0, 0.0); coords->setContravariantMetricTensor(contravariant_metric_tensor); auto covariant_metric_tensor = MetricTensor(1.1, 1.1, 1.1, 0.0, 0.0, 0.0); coords->setCovariantMetricTensor(covariant_metric_tensor); - coords->geometry(); - // Tell BOUT++ to solve N - SOLVE_FOR(N); + SOLVE_FOR(N) return 0; } diff --git a/tests/MMS/tokamak/tokamak.cxx b/tests/MMS/tokamak/tokamak.cxx index 2ebedfb0fd..ac46d69b93 100644 --- a/tests/MMS/tokamak/tokamak.cxx +++ b/tests/MMS/tokamak/tokamak.cxx @@ -31,8 +31,8 @@ class TokamakMMS : public PhysicsModel { // Test bracket advection operator ddt(advect) = -1e-3 * bracket(drive, advect, BRACKET_ARAKAWA) - 10. - * (SQ(SQ(mesh->getCoordinates()->dx)) * D4DX4(advect) - + SQ(SQ(mesh->getCoordinates()->dz)) * D4DZ4(advect)); + * (SQ(SQ(mesh->getCoordinates()->dx())) * D4DX4(advect) + + SQ(SQ(mesh->getCoordinates()->dz())) * D4DZ4(advect)); // Test perpendicular diffusion operator ddt(delp2) = 1e-5 * Delp2(delp2); @@ -53,7 +53,7 @@ class TokamakMMS : public PhysicsModel { Field2D dx; if (!mesh->get(dx, "dpsi")) { output << "\tUsing dpsi as the x grid spacing\n"; - coords->dx = dx; // Only use dpsi if found + coords->dx() = dx; // Only use dpsi if found } else { // dx will have been read already from the grid output << "\tUsing dx as the x grid spacing\n"; @@ -62,11 +62,11 @@ class TokamakMMS : public PhysicsModel { Rxy /= Lnorm; hthe /= Lnorm; sinty *= SQ(Lnorm) * Bnorm; - coords->dx /= SQ(Lnorm) * Bnorm; + coords->setDx(coords->dx() / (SQ(Lnorm) * Bnorm)); Bpxy /= Bnorm; Btxy /= Bnorm; - coords->Bxy() /= Bnorm; + coords->setBxy(coords->Bxy() / Bnorm); // Calculate metric components bool ShiftXderivs; diff --git a/tests/MMS/wave-1d/wave.cxx b/tests/MMS/wave-1d/wave.cxx index 38a95942b3..3a41a3d743 100644 --- a/tests/MMS/wave-1d/wave.cxx +++ b/tests/MMS/wave-1d/wave.cxx @@ -26,8 +26,8 @@ class Wave1D : public PhysicsModel { // this assumes equidistant grid int nguard = mesh->xstart; - coord->dx = Lx / (mesh->GlobalNx - 2 * nguard); - coord->dy = Ly / (mesh->GlobalNy - 2 * nguard); + coord->setDx(Lx / (mesh->GlobalNx - 2 * nguard)); + coord->setDy(Ly / (mesh->GlobalNy - 2 * nguard)); SAVE_ONCE(Lx, Ly); @@ -54,8 +54,8 @@ class Wave1D : public PhysicsModel { g.applyBoundary(t); // Central differencing - ddt(f) = DDX(g, CELL_CENTRE); // + 20*SQ(coord->dx)*D2DX2(f); - ddt(g) = DDX(f, CELL_XLOW); // + 20*SQ(coord->dx)*D2DX2(g); + ddt(f) = DDX(g, CELL_CENTRE); // + 20*SQ(coord->dx())*D2DX2(f); + ddt(g) = DDX(f, CELL_XLOW); // + 20*SQ(coord->dx())*D2DX2(g); return 0; } diff --git a/tests/integrated/test-drift-instability/2fluid.cxx b/tests/integrated/test-drift-instability/2fluid.cxx index 9b195b3a7b..0c356ea92f 100644 --- a/tests/integrated/test-drift-instability/2fluid.cxx +++ b/tests/integrated/test-drift-instability/2fluid.cxx @@ -96,7 +96,7 @@ class TwoFluid : public PhysicsModel { GRID_LOAD(Bpxy); GRID_LOAD(Btxy); GRID_LOAD(hthe); - mesh->get(coord->dx, "dpsi"); + coord->setDx(mesh->get("dpsi")); mesh->get(I, "sinty"); // Load normalisation values @@ -204,7 +204,7 @@ class TwoFluid : public PhysicsModel { Rxy /= rho_s; hthe /= rho_s; I *= rho_s * rho_s * (bmag / 1e4) * ShearFactor; - coord->dx /= rho_s * rho_s * (bmag / 1e4); + coord->setDx(coord->dx() / (rho_s * rho_s * (bmag / 1e4))); // Normalise magnetic field Bpxy /= (bmag / 1.e4); diff --git a/tests/integrated/test-interchange-instability/2fluid.cxx b/tests/integrated/test-interchange-instability/2fluid.cxx index 2c0fe22de2..18e58c9969 100644 --- a/tests/integrated/test-interchange-instability/2fluid.cxx +++ b/tests/integrated/test-interchange-instability/2fluid.cxx @@ -64,7 +64,7 @@ class Interchange : public PhysicsModel { GRID_LOAD(Bpxy); GRID_LOAD(Btxy); GRID_LOAD(hthe); - mesh->get(coord->dx, "dpsi"); + coord->setDx(mesh->get("dpsi")); mesh->get(I, "sinty"); // Load normalisation values @@ -130,7 +130,7 @@ class Interchange : public PhysicsModel { Rxy /= rho_s; hthe /= rho_s; I *= rho_s * rho_s * (bmag / 1e4) * ShearFactor; - coord->dx /= rho_s * rho_s * (bmag / 1e4); + coord->setDx(coord->dx() / (rho_s * rho_s * (bmag / 1e4))); // Normalise magnetic field Bpxy /= (bmag / 1.e4); diff --git a/tests/integrated/test-laplacexy/loadmetric.cxx b/tests/integrated/test-laplacexy/loadmetric.cxx index 7051f7444e..347e82b224 100644 --- a/tests/integrated/test-laplacexy/loadmetric.cxx +++ b/tests/integrated/test-laplacexy/loadmetric.cxx @@ -19,7 +19,7 @@ void LoadMetric(BoutReal Lnorm, BoutReal Bnorm) { Field2D dx; if (!mesh->get(dx, "dpsi")) { output << "\tUsing dpsi as the x grid spacing\n"; - coords->dx = dx; // Only use dpsi if found + coords->setDx(dx); // Only use dpsi if found } else { // dx will have been read already from the grid output << "\tUsing dx as the x grid spacing\n"; @@ -29,11 +29,11 @@ void LoadMetric(BoutReal Lnorm, BoutReal Bnorm) { Rxy /= Lnorm; hthe /= Lnorm; sinty *= SQ(Lnorm) * Bnorm; - coords->dx /= SQ(Lnorm) * Bnorm; + coords->setDx(coords->dx() / (SQ(Lnorm) * Bnorm)); Bpxy /= Bnorm; Btxy /= Bnorm; - coords->Bxy() /= Bnorm; + coords->setBxy(coords->Bxy() / Bnorm); // Calculate metric components std::string ptstr; diff --git a/tests/integrated/test-multigrid_laplace/test_multigrid_laplace.cxx b/tests/integrated/test-multigrid_laplace/test_multigrid_laplace.cxx index 50a3cbfc1a..2cf2ac7dee 100644 --- a/tests/integrated/test-multigrid_laplace/test_multigrid_laplace.cxx +++ b/tests/integrated/test-multigrid_laplace/test_multigrid_laplace.cxx @@ -241,7 +241,7 @@ int main(int argc, char** argv) { for (int k = 0; k < mesh->LocalNz; k++) { x0(mesh->xstart - 1, mesh->ystart, k) = (f4(mesh->xstart, mesh->ystart, k) - f4(mesh->xstart - 1, mesh->ystart, k)) - / mesh->getCoordinates()->dx(mesh->xstart, mesh->ystart, k) + / mesh->getCoordinates()->dx()(mesh->xstart, mesh->ystart, k) / sqrt(mesh->getCoordinates()->g_11()(mesh->xstart, mesh->ystart, k)); } } @@ -249,7 +249,7 @@ int main(int argc, char** argv) { for (int k = 0; k < mesh->LocalNz; k++) { x0(mesh->xend + 1, mesh->ystart, k) = (f4(mesh->xend + 1, mesh->ystart, k) - f4(mesh->xend, mesh->ystart, k)) - / mesh->getCoordinates()->dx(mesh->xend, mesh->ystart, k) + / mesh->getCoordinates()->dx()(mesh->xend, mesh->ystart, k) / sqrt(mesh->getCoordinates()->g_11()(mesh->xend, mesh->ystart, k)); } } diff --git a/tests/integrated/test-naulin-laplace/test_naulin_laplace.cxx b/tests/integrated/test-naulin-laplace/test_naulin_laplace.cxx index 83b589d145..b4813bcedf 100644 --- a/tests/integrated/test-naulin-laplace/test_naulin_laplace.cxx +++ b/tests/integrated/test-naulin-laplace/test_naulin_laplace.cxx @@ -245,7 +245,7 @@ int main(int argc, char** argv) { for (int k = 0; k < mesh->LocalNz; k++) { x0(mesh->xstart - 1, mesh->ystart, k) = (f4(mesh->xstart, mesh->ystart, k) - f4(mesh->xstart - 1, mesh->ystart, k)) - / mesh->getCoordinates()->dx(mesh->xstart, mesh->ystart, k) + / mesh->getCoordinates()->dx()(mesh->xstart, mesh->ystart, k) / sqrt(mesh->getCoordinates()->g_11()(mesh->xstart, mesh->ystart, k)); } } @@ -253,7 +253,7 @@ int main(int argc, char** argv) { for (int k = 0; k < mesh->LocalNz; k++) { x0(mesh->xend + 1, mesh->ystart, k) = (f4(mesh->xend + 1, mesh->ystart, k) - f4(mesh->xend, mesh->ystart, k)) - / mesh->getCoordinates()->dx(mesh->xend, mesh->ystart, k) + / mesh->getCoordinates()->dx()(mesh->xend, mesh->ystart, k) / sqrt(mesh->getCoordinates()->g_11()(mesh->xend, mesh->ystart, k)); } } diff --git a/tests/integrated/test-snb/test_snb.cxx b/tests/integrated/test-snb/test_snb.cxx index f16f678170..155050b0b1 100644 --- a/tests/integrated/test-snb/test_snb.cxx +++ b/tests/integrated/test-snb/test_snb.cxx @@ -228,7 +228,7 @@ int main(int argc, char** argv) { // Check that fluxes are not equal EXPECT_FALSE(IsFieldClose(Div_q, Div_q_SH, "RGN_NOBNDRY")); - const Field2D dy = coord->dy; + const Field2D dy = coord->dy(); const Field2D J = coord->J(); // Integrate Div(q) over domain diff --git a/tests/unit/invert/laplace/test_laplace_cyclic.cxx b/tests/unit/invert/laplace/test_laplace_cyclic.cxx index b0cc73bf37..bb12021021 100644 --- a/tests/unit/invert/laplace/test_laplace_cyclic.cxx +++ b/tests/unit/invert/laplace/test_laplace_cyclic.cxx @@ -53,7 +53,7 @@ class CyclicForwardOperator { void applyBoundaries(Field3D& newF, const Field3D& f) { BOUT_FOR(i, f.getMesh()->getRegion3D("RGN_INNER_X")) { if (inner_x_neumann) { - newF[i] = (f[i.xp()] - f[i]) / coords->dx[i] / sqrt(coords->g_11()[i]); + newF[i] = (f[i.xp()] - f[i]) / coords->dx()[i] / sqrt(coords->g_11()[i]); } else { newF[i] = 0.5 * (f[i] + f[i.xp()]); } @@ -61,7 +61,7 @@ class CyclicForwardOperator { BOUT_FOR(i, f.getMesh()->getRegion3D("RGN_OUTER_X")) { if (outer_x_neumann) { - newF[i] = (f[i] - f[i.xm()]) / coords->dx[i] / sqrt(coords->g_11()[i]); + newF[i] = (f[i] - f[i.xm()]) / coords->dx()[i] / sqrt(coords->g_11()[i]); } else { newF[i] = 0.5 * (f[i.xm()] + f[i]); } diff --git a/tests/unit/invert/laplace/test_laplace_hypre3d.cxx b/tests/unit/invert/laplace/test_laplace_hypre3d.cxx index 1a11e98506..35bbb227e3 100644 --- a/tests/unit/invert/laplace/test_laplace_hypre3d.cxx +++ b/tests/unit/invert/laplace/test_laplace_hypre3d.cxx @@ -61,7 +61,7 @@ class ForwardOperator { void applyBoundaries(Field3D& newF, Field3D& f) { BOUT_FOR(i, f.getMesh()->getRegion3D("RGN_INNER_X")) { if (inner_x_neumann) { - newF[i] = (f[i.xp()] - f[i]) / coords->dx[i] / sqrt(coords->g_11[i]); + newF[i] = (f[i.xp()] - f[i]) / coords->dx()[i] / sqrt(coords->g_11[i]); } else { newF[i] = 0.5 * (f[i] + f[i.xp()]); } @@ -69,7 +69,7 @@ class ForwardOperator { BOUT_FOR(i, f.getMesh()->getRegion3D("RGN_OUTER_X")) { if (outer_x_neumann) { - newF[i] = (f[i] - f[i.xm()]) / coords->dx[i] / sqrt(coords->g_11[i]); + newF[i] = (f[i] - f[i.xm()]) / coords->dx()[i] / sqrt(coords->g_11[i]); } else { newF[i] = 0.5 * (f[i.xm()] + f[i]); } @@ -77,7 +77,7 @@ class ForwardOperator { BOUT_FOR(i, f.getMesh()->getRegion3D("RGN_LOWER_Y")) { if (lower_y_neumann) { - newF[i] = (f[i.yp()] - f[i]) / coords->dx[i] / sqrt(coords->g_11[i]); + newF[i] = (f[i.yp()] - f[i]) / coords->dx()[i] / sqrt(coords->g_11[i]); } else { newF[i] = 0.5 * (f[i] + f[i.yp()]); } @@ -85,7 +85,7 @@ class ForwardOperator { BOUT_FOR(i, f.getMesh()->getRegion3D("RGN_UPPER_Y")) { if (upper_y_neumann) { - newF[i] = (f[i] - f[i.ym()]) / coords->dx[i] / sqrt(coords->g_11[i]); + newF[i] = (f[i] - f[i.ym()]) / coords->dx()[i] / sqrt(coords->g_11[i]); } else { newF[i] = 0.5 * (f[i.ym()] + f[i]); } diff --git a/tests/unit/invert/laplace/test_laplace_petsc3damg.cxx b/tests/unit/invert/laplace/test_laplace_petsc3damg.cxx index b2831808a4..b6e1ede337 100644 --- a/tests/unit/invert/laplace/test_laplace_petsc3damg.cxx +++ b/tests/unit/invert/laplace/test_laplace_petsc3damg.cxx @@ -61,7 +61,7 @@ class ForwardOperator { void applyBoundaries(Field3D& newF, Field3D& f) { BOUT_FOR(i, f.getMesh()->getRegion3D("RGN_INNER_X")) { if (inner_x_neumann) { - newF[i] = (f[i.xp()] - f[i]) / coords->dx[i] / sqrt(coords->g_11[i]); + newF[i] = (f[i.xp()] - f[i]) / coords->dx()[i] / sqrt(coords->g_11[i]); } else { newF[i] = 0.5 * (f[i] + f[i.xp()]); } @@ -69,7 +69,7 @@ class ForwardOperator { BOUT_FOR(i, f.getMesh()->getRegion3D("RGN_OUTER_X")) { if (outer_x_neumann) { - newF[i] = (f[i] - f[i.xm()]) / coords->dx[i] / sqrt(coords->g_11[i]); + newF[i] = (f[i] - f[i.xm()]) / coords->dx()[i] / sqrt(coords->g_11[i]); } else { newF[i] = 0.5 * (f[i.xm()] + f[i]); } @@ -77,7 +77,7 @@ class ForwardOperator { BOUT_FOR(i, f.getMesh()->getRegion3D("RGN_LOWER_Y")) { if (lower_y_neumann) { - newF[i] = (f[i.yp()] - f[i]) / coords->dx[i] / sqrt(coords->g_11[i]); + newF[i] = (f[i.yp()] - f[i]) / coords->dx()[i] / sqrt(coords->g_11[i]); } else { newF[i] = 0.5 * (f[i] + f[i.yp()]); } @@ -85,7 +85,7 @@ class ForwardOperator { BOUT_FOR(i, f.getMesh()->getRegion3D("RGN_UPPER_Y")) { if (upper_y_neumann) { - newF[i] = (f[i] - f[i.ym()]) / coords->dx[i] / sqrt(coords->g_11[i]); + newF[i] = (f[i] - f[i.ym()]) / coords->dx()[i] / sqrt(coords->g_11[i]); } else { newF[i] = 0.5 * (f[i.ym()] + f[i]); } diff --git a/tests/unit/mesh/test_coordinates.cxx b/tests/unit/mesh/test_coordinates.cxx index 86335d9c6a..9ea8ee1198 100644 --- a/tests/unit/mesh/test_coordinates.cxx +++ b/tests/unit/mesh/test_coordinates.cxx @@ -190,9 +190,9 @@ TEST_F(CoordinatesTest, DefaultConstructor) { output_warn.enable(); output_info.enable(); - EXPECT_TRUE(IsFieldEqual(coords.dx, 1.0)); - EXPECT_TRUE(IsFieldEqual(coords.dy, 1.0)); - EXPECT_TRUE(IsFieldEqual(coords.dz, default_dz)); + EXPECT_TRUE(IsFieldEqual(coords.dx(), 1.0)); + EXPECT_TRUE(IsFieldEqual(coords.dy(), 1.0)); + EXPECT_TRUE(IsFieldEqual(coords.dz(), default_dz)); EXPECT_TRUE(IsFieldEqual(coords.g11(), 1.0)); EXPECT_TRUE(IsFieldEqual(coords.g22(), 1.0)); @@ -216,9 +216,9 @@ TEST_F(CoordinatesTest, ConstructWithMeshSpacing) { output_warn.enable(); output_info.enable(); - EXPECT_TRUE(IsFieldEqual(coords.dx, 2.0)); - EXPECT_TRUE(IsFieldEqual(coords.dy, 3.2)); - EXPECT_TRUE(IsFieldEqual(coords.dz, 42.)); + EXPECT_TRUE(IsFieldEqual(coords.dx(), 2.0)); + EXPECT_TRUE(IsFieldEqual(coords.dy(), 3.2)); + EXPECT_TRUE(IsFieldEqual(coords.dz(), 42.)); EXPECT_TRUE(IsFieldEqual(coords.g11(), 1.0)); EXPECT_TRUE(IsFieldEqual(coords.g22(), 1.0)); @@ -256,9 +256,9 @@ TEST_F(CoordinatesTest, ConstructWithDiagonalContravariantMetric) { output_info.enable(); // Didn't specify grid spacing, so default to 1 - EXPECT_TRUE(IsFieldEqual(coords.dx, 1.0)); - EXPECT_TRUE(IsFieldEqual(coords.dy, 1.0)); - EXPECT_TRUE(IsFieldEqual(coords.dz, default_dz)); + EXPECT_TRUE(IsFieldEqual(coords.dx(), 1.0)); + EXPECT_TRUE(IsFieldEqual(coords.dy(), 1.0)); + EXPECT_TRUE(IsFieldEqual(coords.dz(), default_dz)); // Diagonal contravariant metric EXPECT_TRUE(IsFieldEqual(coords.g11(), 2.0)); diff --git a/tests/unit/mesh/test_coordinates_accessor.cxx b/tests/unit/mesh/test_coordinates_accessor.cxx index ad8c49f233..d13339d1be 100644 --- a/tests/unit/mesh/test_coordinates_accessor.cxx +++ b/tests/unit/mesh/test_coordinates_accessor.cxx @@ -23,7 +23,7 @@ TEST_F(CoordinatesAccessorTest, CreateAccessor) { // Basic sanity checks EXPECT_TRUE(acc.mesh_nz == mesh->LocalNz); EXPECT_TRUE(acc.data != nullptr); - EXPECT_FLOAT_EQ(mesh->getCoordinates()->dx(0, 0, 0), acc.dx(0)); + EXPECT_FLOAT_EQ(mesh->getCoordinates()->dx()(0, 0, 0), acc.dx(0)); } TEST_F(CoordinatesAccessorTest, CreateTwoAccessors) { diff --git a/tools/pylib/_boutpp_build/boutpp.pyx.jinja b/tools/pylib/_boutpp_build/boutpp.pyx.jinja index b980965cb3..ef610bcf57 100644 --- a/tools/pylib/_boutpp_build/boutpp.pyx.jinja +++ b/tools/pylib/_boutpp_build/boutpp.pyx.jinja @@ -769,7 +769,7 @@ cdef class Mesh: Usefull if the Options are in SI units, but the simulation is written in Bohm units. Calling it multiple times will not change the mesh, if the normalisation is always the same. - It calls mesh->dx/=norm etc. followed by a call to geometry(). + It calls mesh->dx()/=norm etc. followed by a call to geometry(). Parameters ---------- diff --git a/tools/pylib/_boutpp_build/helper.cxx.jinja b/tools/pylib/_boutpp_build/helper.cxx.jinja index 3a2cdb5626..fd89b2f414 100644 --- a/tools/pylib/_boutpp_build/helper.cxx.jinja +++ b/tools/pylib/_boutpp_build/helper.cxx.jinja @@ -170,8 +170,8 @@ Mesh * c_get_global_mesh(){ void c_mesh_normalise(Mesh * msh, double norm){ //printf("%g\n",norm); auto coord = msh->getCoordinates(); - coord->dx /= norm; - coord->dy /= norm; - coord->dz /= norm; + coord->setDx(coord->dx() / norm); + coord->setDy(coord->dy() / norm); + coord->setDz(coord->dz() / norm); coord->calculateGeometry(); } From 54b343b4a96a4cbcfacb7672fa0d6e7224317425 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Thu, 7 Dec 2023 11:05:13 +0000 Subject: [PATCH 262/491] Add setter for dy that takes [x, y] indices. Update test_snb. --- include/bout/coordinates.hxx | 2 ++ src/mesh/coordinates.cxx | 2 ++ tests/integrated/test-snb/test_snb.cxx | 18 +++++++++--------- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index d9fc9bb68f..dca884af89 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -83,6 +83,8 @@ public: void setDy(FieldMetric dy); void setDz(FieldMetric dz); + void setDy(BoutReal value, int x, int y); + /// Length of the Z domain. Used for FFTs const Field2D& zlength() const; diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index eba93a0321..585d16fd16 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -1366,6 +1366,8 @@ void Coordinates::setDx(FieldMetric dx) { dx_ = dx; } void Coordinates::setDy(FieldMetric dy) { dy_ = dy; } void Coordinates::setDz(FieldMetric dz) { dz_ = dz; } +void Coordinates::setDy(BoutReal value, int x, int y) { dy_(x, y) = value; } + void Coordinates::setContravariantMetricTensor(MetricTensor metric_tensor, const std::string& region, bool recalculate_staggered, diff --git a/tests/integrated/test-snb/test_snb.cxx b/tests/integrated/test-snb/test_snb.cxx index 155050b0b1..b81c3d8904 100644 --- a/tests/integrated/test-snb/test_snb.cxx +++ b/tests/integrated/test-snb/test_snb.cxx @@ -96,8 +96,8 @@ int main(int argc, char** argv) { Field3D Div_q = snb.divHeatFlux(Te, Ne, &Div_q_SH); // Check that flux is not zero - EXPECT_FALSE(IsFieldEqual(Div_q_SH, 0.0, "RGN_NOBNDRY")); - EXPECT_FALSE(IsFieldEqual(Div_q, 0.0, "RGN_NOBNDRY")); + EXPECT_FALSE(IsFieldEqual(Div_q_SH, 0.0, "RGN_NOBNDRY")) + EXPECT_FALSE(IsFieldEqual(Div_q, 0.0, "RGN_NOBNDRY")) } /////////////////////////////////////////////////////////// @@ -116,8 +116,8 @@ int main(int argc, char** argv) { Field3D Div_q = snb.divHeatFlux(Te, Ne, &Div_q_SH); // Check that flux is zero - EXPECT_TRUE(IsFieldEqual(Div_q_SH, 0.0, "RGN_NOBNDRY")); - EXPECT_TRUE(IsFieldEqual(Div_q, 0.0, "RGN_NOBNDRY")); + EXPECT_TRUE(IsFieldEqual(Div_q_SH, 0.0, "RGN_NOBNDRY")) + EXPECT_TRUE(IsFieldEqual(Div_q, 0.0, "RGN_NOBNDRY")) } /////////////////////////////////////////////////////////// @@ -135,7 +135,7 @@ int main(int argc, char** argv) { Field3D Div_q = snb.divHeatFlux(Te, Ne, &Div_q_SH); // Check that flux is zero - EXPECT_TRUE(IsFieldClose(Div_q, Div_q_SH, "RGN_NOBNDRY")); + EXPECT_TRUE(IsFieldClose(Div_q, Div_q_SH, "RGN_NOBNDRY")) } /////////////////////////////////////////////////////////// @@ -153,7 +153,7 @@ int main(int argc, char** argv) { Field3D Div_q = snb.divHeatFlux(Te, Ne, &Div_q_SH); // Check that fluxes are not equal - EXPECT_FALSE(IsFieldClose(Div_q, Div_q_SH, "RGN_NOBNDRY")); + EXPECT_FALSE(IsFieldClose(Div_q, Div_q_SH, "RGN_NOBNDRY")) } /////////////////////////////////////////////////////////// @@ -211,7 +211,7 @@ int main(int argc, char** argv) { for (int y = mesh->ystart; y <= mesh->yend; y++) { double yn = (double(y) + 0.5) / double(mesh->yend + 1); - coord->dy(x, y) = 1. - 0.9 * yn; + coord->setDy((1. - 0.9 * yn), x, y); coord->setJ((1. + yn * yn), x, y); } } @@ -226,7 +226,7 @@ int main(int argc, char** argv) { Div_q *= SI::qe; // Check that fluxes are not equal - EXPECT_FALSE(IsFieldClose(Div_q, Div_q_SH, "RGN_NOBNDRY")); + EXPECT_FALSE(IsFieldClose(Div_q, Div_q_SH, "RGN_NOBNDRY")) const Field2D dy = coord->dy(); const Field2D J = coord->J(); @@ -242,7 +242,7 @@ int main(int argc, char** argv) { q_maxabs = BOUTMAX(q_maxabs, fabs(q_sh), fabs(q_snb)); } // Expect integrals to be the same - EXPECT_LT(fabs(q_sh - q_snb), 1e-8 * q_maxabs); + EXPECT_LT(fabs(q_sh - q_snb), 1e-8 * q_maxabs) } bout::checkForUnusedOptions(); From 41b0a5bd8a2063b5471aec0d92f6b73249efaaaa Mon Sep 17 00:00:00 2001 From: tomc271 Date: Thu, 7 Dec 2023 11:01:40 +0000 Subject: [PATCH 263/491] Corrected comment. --- include/bout/coordinates.hxx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index dca884af89..10d1097743 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -290,7 +290,7 @@ private: std::unique_ptr transform{nullptr}; /// Cache variable for `zlength`. Invalidated when - /// `Coordinates::calculateGeometry` is called + /// `Coordinates::recalculateAndReset` is called mutable std::unique_ptr zlength_cache{nullptr}; /// Cache variable for Grad2_par2 From 0106a2c5b0a19840be0d3e2370b43be2250615f8 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 6 Dec 2023 10:09:54 +0000 Subject: [PATCH 264/491] Prefer const. --- src/mesh/difops.cxx | 103 +++++++++++++++--------------- src/mesh/fv_ops.cxx | 76 +++++++++++----------- src/mesh/parallel_boundary_op.cxx | 70 ++++++++++---------- 3 files changed, 126 insertions(+), 123 deletions(-) diff --git a/src/mesh/difops.cxx b/src/mesh/difops.cxx index 71985cd3ba..2a67c33178 100644 --- a/src/mesh/difops.cxx +++ b/src/mesh/difops.cxx @@ -84,7 +84,7 @@ Field3D Grad_parP(const Field3D& apar, const Field3D& f) { Field3D result{emptyFrom(f)}; - int ncz = mesh->LocalNz; + int const ncz = mesh->LocalNz; Coordinates* metric = apar.getCoordinates(); @@ -106,17 +106,17 @@ Field3D Grad_parP(const Field3D& apar, const Field3D& f) { for (int z = 0; z < ncz; z++) { BoutReal by = 1. / sqrt(metric->g_22()(x, y, z)); // Z indices zm and zp - int zm = (z - 1 + ncz) % ncz; - int zp = (z + 1) % ncz; + int const zm = (z - 1 + ncz) % ncz; + int const zp = (z + 1) % ncz; // bx = -DDZ(apar) - BoutReal bx = (apar(x, y, zm) - apar(x, y, zp)) - / (0.5 * metric->dz()(x, y, zm) + metric->dz()(x, y, z) - + 0.5 * metric->dz()(x, y, zp)); + BoutReal const bx = (apar(x, y, zm) - apar(x, y, zp)) + / (0.5 * metric->dz()(x, y, zm) + metric->dz()(x, y, z) + + 0.5 * metric->dz()(x, y, zp)); // bz = DDX(f) - BoutReal bz = (apar(x + 1, y, z) - apar(x - 1, y, z)) - / (0.5 * metric->dx()(x - 1, y, z) + metric->dx()(x, y, z) - + 0.5 * metric->dx()(x + 1, y, z)); + BoutReal const bz = (apar(x + 1, y, z) - apar(x - 1, y, z)) + / (0.5 * metric->dx()(x - 1, y, z) + metric->dx()(x, y, z) + + 0.5 * metric->dx()(x + 1, y, z)); // Now calculate (bx*d/dx + by*d/dy + bz*d/dz) f @@ -251,19 +251,19 @@ Field3D Div_par(const Field3D& f, const Field3D& v) { for (int j = mesh->ystart; j <= mesh->yend; j++) { for (int k = mesh->zstart; k <= mesh->zend; k++) { // Value of f and v at left cell face - BoutReal fL = 0.5 * (f(i, j, k) + f.ydown()(i, j - 1, k)); - BoutReal vL = 0.5 * (v(i, j, k) + v.ydown()(i, j - 1, k)); + BoutReal const fL = 0.5 * (f(i, j, k) + f.ydown()(i, j - 1, k)); + BoutReal const vL = 0.5 * (v(i, j, k) + v.ydown()(i, j - 1, k)); - BoutReal fR = 0.5 * (f(i, j, k) + f.yup()(i, j + 1, k)); - BoutReal vR = 0.5 * (v(i, j, k) + v.yup()(i, j + 1, k)); + BoutReal const fR = 0.5 * (f(i, j, k) + f.yup()(i, j + 1, k)); + BoutReal const vR = 0.5 * (v(i, j, k) + v.yup()(i, j + 1, k)); // Calculate flux at right boundary (y+1/2) - BoutReal fluxRight = + BoutReal const fluxRight = fR * vR * (coord->J()(i, j, k) + coord->J()(i, j + 1, k)) / (sqrt(coord->g_22()(i, j, k)) + sqrt(coord->g_22()(i, j + 1, k))); // Calculate at left boundary (y-1/2) - BoutReal fluxLeft = + BoutReal const fluxLeft = fL * vL * (coord->J()(i, j, k) + coord->J()(i, j - 1, k)) / (sqrt(coord->g_22()(i, j, k)) + sqrt(coord->g_22()(i, j - 1, k))); @@ -469,12 +469,12 @@ Coordinates::FieldMetric b0xGrad_dot_Grad(const Field2D& phi, const Field2D& A, Coordinates* metric = phi.getCoordinates(outloc); // Calculate phi derivatives - Coordinates::FieldMetric dpdx = DDX(phi, outloc); - Coordinates::FieldMetric dpdy = DDY(phi, outloc); + Coordinates::FieldMetric const dpdx = DDX(phi, outloc); + Coordinates::FieldMetric const dpdy = DDY(phi, outloc); // Calculate advection velocity - Coordinates::FieldMetric vx = -metric->g_23() * dpdy; - Coordinates::FieldMetric vy = metric->g_23() * dpdx; + Coordinates::FieldMetric const vx = -metric->g_23() * dpdy; + Coordinates::FieldMetric const vy = metric->g_23() * dpdx; // Upwind A using these velocities Coordinates::FieldMetric result = VDDX(vx, A, outloc) + VDDY(vy, A, outloc); @@ -502,12 +502,12 @@ Field3D b0xGrad_dot_Grad(const Field2D& phi, const Field3D& A, CELL_LOC outloc) Coordinates* metric = phi.getCoordinates(outloc); // Calculate phi derivatives - Coordinates::FieldMetric dpdx = DDX(phi, outloc); - Coordinates::FieldMetric dpdy = DDY(phi, outloc); + Coordinates::FieldMetric const dpdx = DDX(phi, outloc); + Coordinates::FieldMetric const dpdy = DDY(phi, outloc); // Calculate advection velocity - Coordinates::FieldMetric vx = -metric->g_23() * dpdy; - Coordinates::FieldMetric vy = metric->g_23() * dpdx; + Coordinates::FieldMetric const vx = -metric->g_23() * dpdy; + Coordinates::FieldMetric const vy = metric->g_23() * dpdx; Coordinates::FieldMetric vz = metric->g_12() * dpdy - metric->g_22() * dpdx; if (mesh->IncIntShear) { @@ -542,13 +542,13 @@ Field3D b0xGrad_dot_Grad(const Field3D& p, const Field2D& A, CELL_LOC outloc) { Coordinates* metric = p.getCoordinates(outloc); // Calculate phi derivatives - Field3D dpdx = DDX(p, outloc); - Field3D dpdy = DDY(p, outloc); - Field3D dpdz = DDZ(p, outloc); + Field3D const dpdx = DDX(p, outloc); + Field3D const dpdy = DDY(p, outloc); + Field3D const dpdz = DDZ(p, outloc); // Calculate advection velocity - Field3D vx = metric->g_22() * dpdz - metric->g_23() * dpdy; - Field3D vy = metric->g_23() * dpdx - metric->g_12() * dpdz; + Field3D const vx = metric->g_22() * dpdz - metric->g_23() * dpdy; + Field3D const vy = metric->g_23() * dpdx - metric->g_12() * dpdz; // Upwind A using these velocities @@ -579,13 +579,13 @@ Field3D b0xGrad_dot_Grad(const Field3D& phi, const Field3D& A, CELL_LOC outloc) Coordinates* metric = phi.getCoordinates(outloc); // Calculate phi derivatives - Field3D dpdx = DDX(phi, outloc); - Field3D dpdy = DDY(phi, outloc); - Field3D dpdz = DDZ(phi, outloc); + Field3D const dpdx = DDX(phi, outloc); + Field3D const dpdy = DDY(phi, outloc); + Field3D const dpdz = DDZ(phi, outloc); // Calculate advection velocity - Field3D vx = metric->g_22() * dpdz - metric->g_23() * dpdy; - Field3D vy = metric->g_23() * dpdx - metric->g_12() * dpdz; + Field3D const vx = metric->g_22() * dpdz - metric->g_23() * dpdy; + Field3D const vy = metric->g_23() * dpdx - metric->g_12() * dpdz; Field3D vz = metric->g_12() * dpdy - metric->g_22() * dpdx; if (mesh->IncIntShear) { @@ -666,13 +666,13 @@ Field3D bracket(const Field3D& f, const Field2D& g, BRACKET_METHOD method, for (int x = mesh->xstart; x <= mesh->xend; x++) { for (int y = mesh->ystart; y <= mesh->yend; y++) { for (int z = 0; z < ncz; z++) { - int zm = (z - 1 + ncz) % ncz; - int zp = (z + 1) % ncz; + int const zm = (z - 1 + ncz) % ncz; + int const zp = (z + 1) % ncz; BoutReal gp, gm; // Vx = DDZ(f) - BoutReal vx = (f(x, y, zp) - f(x, y, zm)) / (2. * metric->dz()(x, y, z)); + BoutReal const vx = (f(x, y, zp) - f(x, y, zm)) / (2. * metric->dz()(x, y, z)); // Set stability condition solver->setMaxTimestep(metric->dx()(x, y, z) / (fabs(vx) + 1e-16)); @@ -786,21 +786,22 @@ Field3D bracket(const Field3D& f, const Field2D& g, BRACKET_METHOD method, // Above is alternative to const int jzmTmp = (jz - 1 + ncz) % ncz; // J++ = DDZ(f)*DDX(g) - DDX(f)*DDZ(g) - BoutReal Jpp = + BoutReal const Jpp = ((f(jx, jy, jzp) - f(jx, jy, jzm)) * (g(jx + 1, jy) - g(jx - 1, jy)) - (f(jx + 1, jy, jz) - f(jx - 1, jy, jz)) * (g(jx, jy) - g(jx, jy))); // J+x - BoutReal Jpx = (g(jx + 1, jy) * (f(jx + 1, jy, jzp) - f(jx + 1, jy, jzm)) - - g(jx - 1, jy) * (f(jx - 1, jy, jzp) - f(jx - 1, jy, jzm)) - - g(jx, jy) * (f(jx + 1, jy, jzp) - f(jx - 1, jy, jzp)) - + g(jx, jy) * (f(jx + 1, jy, jzm) - f(jx - 1, jy, jzm))); + BoutReal const Jpx = + (g(jx + 1, jy) * (f(jx + 1, jy, jzp) - f(jx + 1, jy, jzm)) + - g(jx - 1, jy) * (f(jx - 1, jy, jzp) - f(jx - 1, jy, jzm)) + - g(jx, jy) * (f(jx + 1, jy, jzp) - f(jx - 1, jy, jzp)) + + g(jx, jy) * (f(jx + 1, jy, jzm) - f(jx - 1, jy, jzm))); // Jx+ - BoutReal Jxp = (g(jx + 1, jy) * (f(jx, jy, jzp) - f(jx + 1, jy, jz)) - - g(jx - 1, jy) * (f(jx - 1, jy, jz) - f(jx, jy, jzm)) - - g(jx - 1, jy) * (f(jx, jy, jzp) - f(jx - 1, jy, jz)) - + g(jx + 1, jy) * (f(jx + 1, jy, jz) - f(jx, jy, jzm))); + BoutReal const Jxp = (g(jx + 1, jy) * (f(jx, jy, jzp) - f(jx + 1, jy, jz)) + - g(jx - 1, jy) * (f(jx - 1, jy, jz) - f(jx, jy, jzm)) + - g(jx - 1, jy) * (f(jx, jy, jzp) - f(jx - 1, jy, jz)) + + g(jx + 1, jy) * (f(jx + 1, jy, jz) - f(jx, jy, jzm))); result(jx, jy, jz) = (Jpp + Jpx + Jxp) * spacingFactor; } @@ -893,7 +894,7 @@ Field3D bracket(const Field3D& f, const Field3D& g, BRACKET_METHOD method, } // Get current timestep - BoutReal dt = solver->getCurrentTimestep(); + BoutReal const dt = solver->getCurrentTimestep(); FieldPerp vx(mesh), vz(mesh); vx.allocate(); @@ -901,12 +902,12 @@ Field3D bracket(const Field3D& f, const Field3D& g, BRACKET_METHOD method, vz.allocate(); vz.setLocation(outloc); - int ncz = mesh->LocalNz; + int const ncz = mesh->LocalNz; for (int y = mesh->ystart; y <= mesh->yend; y++) { for (int x = 1; x <= mesh->LocalNx - 2; x++) { for (int z = 0; z < mesh->LocalNz; z++) { - int zm = (z - 1 + ncz) % ncz; - int zp = (z + 1) % ncz; + int const zm = (z - 1 + ncz) % ncz; + int const zp = (z + 1) % ncz; // Vx = DDZ(f) vx(x, z) = (f(x, y, zp) - f(x, y, zm)) / (2. * metric->dz()(x, y, z)); @@ -925,8 +926,8 @@ Field3D bracket(const Field3D& f, const Field3D& g, BRACKET_METHOD method, for (int x = mesh->xstart; x <= mesh->xend; x++) { for (int z = 0; z < ncz; z++) { - int zm = (z - 1 + ncz) % ncz; - int zp = (z + 1) % ncz; + int const zm = (z - 1 + ncz) % ncz; + int const zp = (z + 1) % ncz; BoutReal gp, gm; diff --git a/src/mesh/fv_ops.cxx b/src/mesh/fv_ops.cxx index 7a18de4e4e..ce5d7ff448 100644 --- a/src/mesh/fv_ops.cxx +++ b/src/mesh/fv_ops.cxx @@ -34,8 +34,8 @@ Field3D Div_a_Grad_perp(const Field3D& a, const Field3D& f) { // Flux in x - int xs = mesh->xstart - 1; - int xe = mesh->xend; + int const xs = mesh->xstart - 1; + int const xe = mesh->xend; /* if(mesh->firstX()) @@ -51,11 +51,11 @@ Field3D Div_a_Grad_perp(const Field3D& a, const Field3D& f) { for (int k = 0; k < mesh->LocalNz; k++) { // Calculate flux from i to i+1 - BoutReal fout = 0.5 * (a(i, j, k) + a(i + 1, j, k)) - * (coord->J()(i, j, k) * coord->g11()(i, j, k) - + coord->J()(i + 1, j, k) * coord->g11()(i + 1, j, k)) - * (f(i + 1, j, k) - f(i, j, k)) - / (coord->dx()(i, j, k) + coord->dx()(i + 1, j, k)); + BoutReal const fout = 0.5 * (a(i, j, k) + a(i + 1, j, k)) + * (coord->J()(i, j, k) * coord->g11()(i, j, k) + + coord->J()(i + 1, j, k) * coord->g11()(i + 1, j, k)) + * (f(i + 1, j, k) - f(i, j, k)) + / (coord->dx()(i, j, k) + coord->dx()(i + 1, j, k)); result(i, j, k) += fout / (coord->dx()(i, j, k) * coord->J()(i, j, k)); result(i + 1, j, k) -= @@ -188,7 +188,7 @@ const Field3D Div_par_K_Grad_par(const Field3D& Kin, const Field3D& fin, Mesh* mesh = Kin.getMesh(); - bool use_parallel_slices = (Kin.hasParallelSlices() && fin.hasParallelSlices()); + bool const use_parallel_slices = (Kin.hasParallelSlices() && fin.hasParallelSlices()); const auto& K = use_parallel_slices ? Kin : toFieldAligned(Kin, "RGN_NOX"); const auto& f = use_parallel_slices ? fin : toFieldAligned(fin, "RGN_NOX"); @@ -212,13 +212,14 @@ const Field3D Div_par_K_Grad_par(const Field3D& Kin, const Field3D& fin, if (bndry_flux || mesh->periodicY(i.x()) || !mesh->lastY(i.x()) || (i.y() != mesh->yend)) { - BoutReal c = 0.5 * (K[i] + Kup[iyp]); // K at the upper boundary - BoutReal J = 0.5 * (coord->J()[i] + coord->J()[iyp]); // Jacobian at boundary - BoutReal g_22 = 0.5 * (coord->g_22()[i] + coord->g_22()[iyp]); + BoutReal const c = 0.5 * (K[i] + Kup[iyp]); // K at the upper boundary + BoutReal const J = 0.5 * (coord->J()[i] + coord->J()[iyp]); // Jacobian at boundary + BoutReal const g_22 = 0.5 * (coord->g_22()[i] + coord->g_22()[iyp]); - BoutReal gradient = 2. * (fup[iyp] - f[i]) / (coord->dy()[i] + coord->dy()[iyp]); + BoutReal const gradient = + 2. * (fup[iyp] - f[i]) / (coord->dy()[i] + coord->dy()[iyp]); - BoutReal flux = c * J * gradient / g_22; + BoutReal const flux = c * J * gradient / g_22; result[i] += flux / (coord->dy()[i] * coord->J()[i]); } @@ -226,14 +227,15 @@ const Field3D Div_par_K_Grad_par(const Field3D& Kin, const Field3D& fin, // Calculate flux at lower surface if (bndry_flux || mesh->periodicY(i.x()) || !mesh->firstY(i.x()) || (i.y() != mesh->ystart)) { - BoutReal c = 0.5 * (K[i] + Kdown[iym]); // K at the lower boundary - BoutReal J = 0.5 * (coord->J()[i] + coord->J()[iym]); // Jacobian at boundary + BoutReal const c = 0.5 * (K[i] + Kdown[iym]); // K at the lower boundary + BoutReal const J = 0.5 * (coord->J()[i] + coord->J()[iym]); // Jacobian at boundary - BoutReal g_22 = 0.5 * (coord->g_22()[i] + coord->g_22()[iym]); + BoutReal const g_22 = 0.5 * (coord->g_22()[i] + coord->g_22()[iym]); - BoutReal gradient = 2. * (f[i] - fdown[iym]) / (coord->dy()[i] + coord->dy()[iym]); + BoutReal const gradient = + 2. * (f[i] - fdown[iym]) / (coord->dy()[i] + coord->dy()[iym]); - BoutReal flux = c * J * gradient / g_22; + BoutReal const flux = c * J * gradient / g_22; result[i] -= flux / (coord->dy()[i] * coord->J()[i]); } @@ -266,9 +268,9 @@ const Field3D D4DY4(const Field3D& d_in, const Field3D& f_in) { for (int i = mesh->xstart; i <= mesh->xend; i++) { // Check for boundaries - bool yperiodic = mesh->periodicY(i); - bool has_upper_boundary = !yperiodic && mesh->lastY(i); - bool has_lower_boundary = !yperiodic && mesh->firstY(i); + bool const yperiodic = mesh->periodicY(i); + bool const has_upper_boundary = !yperiodic && mesh->lastY(i); + bool const has_lower_boundary = !yperiodic && mesh->firstY(i); // Always calculate fluxes at upper Y cell boundary const int ystart = @@ -284,15 +286,15 @@ const Field3D D4DY4(const Field3D& d_in, const Field3D& f_in) { for (int j = ystart; j <= yend; j++) { for (int k = 0; k < mesh->LocalNz; k++) { - BoutReal dy3 = SQ(coord->dz()(i, j, k)) * coord->dz()(i, j, k); + BoutReal const dy3 = SQ(coord->dz()(i, j, k)) * coord->dz()(i, j, k); // 3rd derivative at upper boundary - BoutReal d3fdy3 = + BoutReal const d3fdy3 = (f(i, j + 2, k) - 3. * f(i, j + 1, k) + 3. * f(i, j, k) - f(i, j - 1, k)) / dy3; - BoutReal flux = 0.5 * (d(i, j, k) + d(i, j + 1, k)) - * (coord->J()(i, j, k) + coord->J()(i, j + 1, k)) * d3fdy3; + BoutReal const flux = 0.5 * (d(i, j, k) + d(i, j + 1, k)) + * (coord->J()(i, j, k) + coord->J()(i, j + 1, k)) * d3fdy3; result(i, j, k) += flux / (coord->J()(i, j, k) * coord->dz()(i, j, k)); result(i, j + 1, k) -= @@ -317,10 +319,10 @@ const Field3D D4DY4_Index(const Field3D& f_in, bool bndry_flux) { Coordinates* coord = f_in.getCoordinates(); for (int i = mesh->xstart; i <= mesh->xend; i++) { - bool yperiodic = mesh->periodicY(i); + bool const yperiodic = mesh->periodicY(i); - bool has_upper_boundary = !yperiodic && mesh->lastY(i); - bool has_lower_boundary = !yperiodic && mesh->firstY(i); + bool const has_upper_boundary = !yperiodic && mesh->lastY(i); + bool const has_lower_boundary = !yperiodic && mesh->firstY(i); for (int j = mesh->ystart; j <= mesh->yend; j++) { @@ -440,12 +442,12 @@ void communicateFluxes(Field3D& f) { throw BoutException("communicateFluxes: Sorry!"); } - int size = mesh->LocalNy * mesh->LocalNz; + int const size = mesh->LocalNy * mesh->LocalNz; comm_handle xin, xout; // Cache results to silence spurious compiler warning about xin, // xout possibly being uninitialised when used - bool not_first = !mesh->firstX(); - bool not_last = !mesh->lastX(); + bool const not_first = !mesh->firstX(); + bool const not_last = !mesh->lastX(); if (not_first) { xin = mesh->irecvXIn(f(0, 0), size, 0); } @@ -506,12 +508,12 @@ Field3D Div_Perp_Lap(const Field3D& a, const Field3D& f, CELL_LOC outloc) { for (int k = 0; k < mesh->LocalNz; k++) { // wrap k-index around as Z is (currently) periodic. - int kp = (k + 1) % (mesh->LocalNz); - int km = (k - 1 + mesh->LocalNz) % (mesh->LocalNz); + int const kp = (k + 1) % (mesh->LocalNz); + int const km = (k - 1 + mesh->LocalNz) % (mesh->LocalNz); // Calculate gradients on cell faces -- assumes constant grid spacing - BoutReal gR = + BoutReal const gR = (coords->g11()(i, j, k) + coords->g11()(i + 1, j, k)) * (f(i + 1, j, k) - f(i, j, k)) / (coords->dx()(i + 1, j, k) + coords->dx()(i, j, k)) @@ -519,7 +521,7 @@ Field3D Div_Perp_Lap(const Field3D& a, const Field3D& f, CELL_LOC outloc) { * (f(i + 1, j, kp) - f(i + 1, j, km) + f(i, j, kp) - f(i, j, km)) / (4. * coords->dz()(i, j, k)); - BoutReal gL = + BoutReal const gL = (coords->g11()(i - 1, j, k) + coords->g11()(i, j, k)) * (f(i, j, k) - f(i - 1, j, k)) / (coords->dx()(i - 1, j, k) + coords->dx()(i, j, k)) @@ -527,13 +529,13 @@ Field3D Div_Perp_Lap(const Field3D& a, const Field3D& f, CELL_LOC outloc) { * (f(i - 1, j, kp) - f(i - 1, j, km) + f(i, j, kp) - f(i, j, km)) / (4 * coords->dz()(i, j, k)); - BoutReal gD = + BoutReal const gD = coords->g13()(i, j, k) * (f(i + 1, j, km) - f(i - 1, j, km) + f(i + 1, j, k) - f(i - 1, j, k)) / (4. * coords->dx()(i, j, k)) + coords->g33()(i, j, k) * (f(i, j, k) - f(i, j, km)) / coords->dz()(i, j, k); - BoutReal gU = + BoutReal const gU = coords->g13()(i, j, k) * (f(i + 1, j, kp) - f(i - 1, j, kp) + f(i + 1, j, k) - f(i - 1, j, k)) / (4. * coords->dx()(i, j, k)) diff --git a/src/mesh/parallel_boundary_op.cxx b/src/mesh/parallel_boundary_op.cxx index ecca6df1e0..552dd0090b 100644 --- a/src/mesh/parallel_boundary_op.cxx +++ b/src/mesh/parallel_boundary_op.cxx @@ -32,22 +32,22 @@ BoutReal BoundaryOpPar::getValue(const BoundaryRegionPar& bndry, BoutReal t) { void BoundaryOpPar_dirichlet::apply(Field3D& f, BoutReal t) { Field3D& f_next = f.ynext(bndry->dir); - Coordinates& coord = *(f.getCoordinates()); + Coordinates const& coord = *(f.getCoordinates()); // Loop over grid points If point is in boundary, then fill in // f_next such that the field would be VALUE on the boundary for (bndry->first(); !bndry->isDone(); bndry->next()) { // temp variables for convenience - int x = bndry->x; - int y = bndry->y; - int z = bndry->z; + int const x = bndry->x; + int const y = bndry->y; + int const z = bndry->z; // Generate the boundary value - BoutReal value = getValue(*bndry, t); + BoutReal const value = getValue(*bndry, t); // Scale the field and normalise to the desired value - BoutReal y_prime = bndry->length; - BoutReal f2 = (f(x, y, z) - value) * (coord.dy()(x, y, z) - y_prime) / y_prime; + BoutReal const y_prime = bndry->length; + BoutReal const f2 = (f(x, y, z) - value) * (coord.dy()(x, y, z) - y_prime) / y_prime; f_next(x, y + bndry->dir, z) = value - f2; } @@ -61,28 +61,28 @@ void BoundaryOpPar_dirichlet_O3::apply(Field3D& f, BoutReal t) { Field3D& f_next = f.ynext(bndry->dir); Field3D& f_prev = f.ynext(-bndry->dir); - Coordinates& coord = *(f.getCoordinates()); + Coordinates const& coord = *(f.getCoordinates()); // Loop over grid points If point is in boundary, then fill in // f_next such that the field would be VALUE on the boundary for (bndry->first(); !bndry->isDone(); bndry->next()) { // temp variables for convenience - int x = bndry->x; - int y = bndry->y; - int z = bndry->z; + int const x = bndry->x; + int const y = bndry->y; + int const z = bndry->z; // Generate the boundary value - BoutReal fb = getValue(*bndry, t); - BoutReal f1 = f_prev(x, y - bndry->dir, z); - BoutReal f2 = f(x, y, z); - BoutReal l1 = coord.dy()(x, y, z); - BoutReal l2 = bndry->length; - BoutReal l3 = coord.dy()(x, y, z) - l2; - - BoutReal denom = (l1 * l1 * l2 + l1 * l2 * l2); - BoutReal term1 = (l2 * l2 * l3 + l2 * l3 * l3); - BoutReal term2 = l1 * (l1 + l2 + l3) * (l2 + l3); - BoutReal term3 = l3 * ((l1 + l2) * l3 + (l1 + l2) * (l1 + l2)); + BoutReal const fb = getValue(*bndry, t); + BoutReal const f1 = f_prev(x, y - bndry->dir, z); + BoutReal const f2 = f(x, y, z); + BoutReal const l1 = coord.dy()(x, y, z); + BoutReal const l2 = bndry->length; + BoutReal const l3 = coord.dy()(x, y, z) - l2; + + BoutReal const denom = (l1 * l1 * l2 + l1 * l2 * l2); + BoutReal const term1 = (l2 * l2 * l3 + l2 * l3 * l3); + BoutReal const term2 = l1 * (l1 + l2 + l3) * (l2 + l3); + BoutReal const term3 = l3 * ((l1 + l2) * l3 + (l1 + l2) * (l1 + l2)); f_next(x, y + bndry->dir, z) = (term1 * f1 + term2 * fb - term3 * f2) / denom; } @@ -96,22 +96,22 @@ void BoundaryOpPar_dirichlet_interp::apply(Field3D& f, BoutReal t) { Field3D& f_next = f.ynext(bndry->dir); Field3D& f_prev = f.ynext(-bndry->dir); - Coordinates& coord = *(f.getCoordinates()); + Coordinates const& coord = *(f.getCoordinates()); // Loop over grid points If point is in boundary, then fill in // f_next such that the field would be VALUE on the boundary for (bndry->first(); !bndry->isDone(); bndry->next()) { // temp variables for convenience - int x = bndry->x; - int y = bndry->y; - int z = bndry->z; + int const x = bndry->x; + int const y = bndry->y; + int const z = bndry->z; // Generate the boundary value - BoutReal fs = getValue(*bndry, t); + BoutReal const fs = getValue(*bndry, t); // Scale the field and normalise to the desired value - BoutReal dy = coord.dy()(x, y, z); - BoutReal s = bndry->length * dy; + BoutReal const dy = coord.dy()(x, y, z); + BoutReal const s = bndry->length * dy; f_next(x, y + bndry->dir, z) = f_prev(x, y - bndry->dir, z) * (1. - (2. * s / (dy + s))) @@ -128,19 +128,19 @@ void BoundaryOpPar_neumann::apply(Field3D& f, BoutReal t) { Field3D& f_next = f.ynext(bndry->dir); f_next.allocate(); // Ensure unique before modifying - Coordinates& coord = *(f.getCoordinates()); + Coordinates const& coord = *(f.getCoordinates()); // If point is in boundary, then fill in f_next such that the derivative // would be VALUE on the boundary for (bndry->first(); !bndry->isDone(); bndry->next()) { // temp variables for convience - int x = bndry->x; - int y = bndry->y; - int z = bndry->z; + int const x = bndry->x; + int const y = bndry->y; + int const z = bndry->z; // Generate the boundary value - BoutReal value = getValue(*bndry, t); - BoutReal dy = coord.dy()(x, y, z); + BoutReal const value = getValue(*bndry, t); + BoutReal const dy = coord.dy()(x, y, z); f_next(x, y + bndry->dir, z) = f(x, y, z) + bndry->dir * value * dy; } From 0bf195b2d12f7849c7cecdf523fa698d9eabf677 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 6 Dec 2023 10:41:25 +0000 Subject: [PATCH 265/491] Make d1_dx, d1_dy, d1_dz private. --- include/bout/coordinates.hxx | 11 ++++- .../laplace/impls/hypre3d/hypre3d_laplace.cxx | 2 +- .../impls/multigrid/multigrid_laplace.cxx | 2 +- .../laplace/impls/petsc3damg/petsc3damg.cxx | 2 +- src/invert/laplacexy/laplacexy.cxx | 4 +- src/invert/parderiv/impls/cyclic/cyclic.cxx | 6 +-- src/mesh/coordinates.cxx | 49 +++++++++++-------- src/mesh/coordinates_accessor.cxx | 4 ++ src/sys/derivs.cxx | 12 +++-- tests/unit/mesh/test_coordinates_accessor.cxx | 12 +++-- tests/unit/test_extras.hxx | 10 ++-- 11 files changed, 74 insertions(+), 40 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index 10d1097743..0d6cb41f40 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -85,6 +85,10 @@ public: void setDy(BoutReal value, int x, int y); + void setD1_dx(FieldMetric d1_dx); + void setD1_dy(FieldMetric d1_dy); + void setD1_dz(FieldMetric d1_dz); + /// Length of the Z domain. Used for FFTs const Field2D& zlength() const; @@ -92,7 +96,9 @@ public: bool non_uniform{}; /// 2nd-order correction for non-uniform meshes d/di(1/dx), d/di(1/dy) and d/di(1/dz) - FieldMetric d1_dx, d1_dy, d1_dz; + const FieldMetric& d1_dx() const; + const FieldMetric& d1_dy() const; + const FieldMetric& d1_dz() const; /// Covariant metric tensor const FieldMetric& g_11() const; @@ -286,6 +292,9 @@ private: FieldMetric dx_, dy_, dz_; ///< Mesh spacing in x, y and z + /// 2nd-order correction for non-uniform meshes d/di(1/dx), d/di(1/dy) and d/di(1/dz) + FieldMetric d1_dx_, d1_dy_, d1_dz_; + /// Handles calculation of yup and ydown std::unique_ptr transform{nullptr}; diff --git a/src/invert/laplace/impls/hypre3d/hypre3d_laplace.cxx b/src/invert/laplace/impls/hypre3d/hypre3d_laplace.cxx index b61f70c4b1..2d7a19b8c0 100644 --- a/src/invert/laplace/impls/hypre3d/hypre3d_laplace.cxx +++ b/src/invert/laplace/impls/hypre3d/hypre3d_laplace.cxx @@ -320,7 +320,7 @@ void LaplaceHypre3d::updateMatrix3D() { // Adjust the coefficients to include finite-difference factors if (nonuniform) { - C_df_dx += C_d2f_dx2 * coords->d1_dx[l]; + C_df_dx += C_d2f_dx2 * coords->d1_dx()[l]; } C_df_dx /= 2 * coords->dx()[l]; C_df_dz /= 2 * coords->dz()[l]; diff --git a/src/invert/laplace/impls/multigrid/multigrid_laplace.cxx b/src/invert/laplace/impls/multigrid/multigrid_laplace.cxx index afc8045130..78b970f434 100644 --- a/src/invert/laplace/impls/multigrid/multigrid_laplace.cxx +++ b/src/invert/laplace/impls/multigrid/multigrid_laplace.cxx @@ -624,7 +624,7 @@ void LaplaceMultigrid::generateMatrixF(int level) { / coords->dx()(i2, yindex); // coefficient of 1st derivative stencil (x-direction) if (nonuniform) { // add correction for non-uniform dx - dxd += D(i2, yindex, k2) * coords->d1_dx(i2, yindex); + dxd += D(i2, yindex, k2) * coords->d1_dx()(i2, yindex); } BoutReal dzd = diff --git a/src/invert/laplace/impls/petsc3damg/petsc3damg.cxx b/src/invert/laplace/impls/petsc3damg/petsc3damg.cxx index c6fc823637..b9b01748c4 100644 --- a/src/invert/laplace/impls/petsc3damg/petsc3damg.cxx +++ b/src/invert/laplace/impls/petsc3damg/petsc3damg.cxx @@ -319,7 +319,7 @@ void LaplacePetsc3dAmg::updateMatrix3D() { // Adjust the coefficients to include finite-difference factors if (nonuniform) { - C_df_dx += C_d2f_dx2 * coords->d1_dx[l]; + C_df_dx += C_d2f_dx2 * coords->d1_dx()[l]; } C_df_dx /= 2 * coords->dx()[l]; C_df_dz /= 2 * coords->dz()[l]; diff --git a/src/invert/laplacexy/laplacexy.cxx b/src/invert/laplacexy/laplacexy.cxx index b3bd7d403f..ef039c75cc 100644 --- a/src/invert/laplacexy/laplacexy.cxx +++ b/src/invert/laplacexy/laplacexy.cxx @@ -1014,8 +1014,8 @@ void LaplaceXY::setMatrixElementsFiniteDifference(const Field2D& A, const Field2 const Field2D g_22_2D = DC(coords->g_22); const Field2D g22_2D = DC(coords->g22); const Field2D g12_2D = DC(coords->g12); - const Field2D d1_dx_2D = DC(coords->d1_dx); - const Field2D d1_dy_2D = DC(coords->d1_dy); + const Field2D d1_dx_2D = DC(coords->d1_dx()); + const Field2D d1_dy_2D = DC(coords->d1_dy()); const Field2D dx_2D = DC(coords->dx()); const Field2D dy_2D = DC(coords->dy()); diff --git a/src/invert/parderiv/impls/cyclic/cyclic.cxx b/src/invert/parderiv/impls/cyclic/cyclic.cxx index 78089b7026..15e176fc00 100644 --- a/src/invert/parderiv/impls/cyclic/cyclic.cxx +++ b/src/invert/parderiv/impls/cyclic/cyclic.cxx @@ -64,8 +64,8 @@ InvertParCR::InvertParCR(Options* opt, CELL_LOC location, Mesh* mesh_in) const Field3D InvertParCR::solve(const Field3D& f) { TRACE("InvertParCR::solve(Field3D)"); - ASSERT1(localmesh == f.getMesh()); - ASSERT1(location == f.getLocation()); + ASSERT1(localmesh == f.getMesh()) + ASSERT1(location == f.getLocation()) Field3D result = emptyFrom(f).setDirectionY(YDirectionType::Aligned); @@ -161,7 +161,7 @@ const Field3D InvertParCR::solve(const Field3D& f) { + sg(x, y + local_ystart) * B(x, y + local_ystart); // ddy if (coord->non_uniform) { - ecoef += bcoef * coord->d1_dy(x, y + local_ystart); + ecoef += bcoef * coord->d1_dy()(x, y + local_ystart); } bcoef /= SQ(coord->dy()(x, y + local_ystart)); diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 585d16fd16..cc5b5d37c2 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -299,9 +299,9 @@ Coordinates::Coordinates(Mesh* mesh, const FieldMetric& dx, const FieldMetric& d Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, const Coordinates* coords_in, bool force_interpolate_from_centre) - : dx_(1., mesh), dy_(1., mesh), dz_(1., mesh), d1_dx(mesh), d1_dy(mesh), d1_dz(mesh), - ShiftTorsion(mesh), IntShiftTorsion(mesh), localmesh(mesh), location(loc), - differential_operators(mesh->getDifferentialOperators()), + : dx_(1., mesh), dy_(1., mesh), dz_(1., mesh), d1_dx_(mesh), d1_dy_(mesh), + d1_dz_(mesh), ShiftTorsion(mesh), IntShiftTorsion(mesh), localmesh(mesh), + location(loc), differential_operators(mesh->getDifferentialOperators()), geometry(Geometry(mesh, differential_operators)) { if (options == nullptr) { @@ -693,58 +693,59 @@ void Coordinates::correctionForNonUniformMeshes(bool force_interpolate_from_cent if (localmesh->get(d2x, "d2x" + suffix, 0.0, false, location) != 0) { output_warn.write("\tWARNING: differencing quantity 'd2x' not found. " "Calculating from dx\n"); - d1_dx = bout::derivatives::index::DDX(1. / dx()); // d/di(1/dx) + d1_dx_ = bout::derivatives::index::DDX(1. / dx()); // d/di(1/dx) - communicate(d1_dx); - d1_dx = localmesh->interpolateAndExtrapolate(d1_dx, location, true, true, true, - transform.get()); + communicate(d1_dx_); + d1_dx_ = localmesh->interpolateAndExtrapolate(d1_dx_, location, true, true, true, + transform.get()); } else { d2x.setLocation(location); // set boundary cells if necessary d2x = localmesh->interpolateAndExtrapolate(d2x, location, extrapolate_x, extrapolate_y, false, transform.get()); - d1_dx = -d2x / (dx() * dx()); + d1_dx_ = -d2x / (dx() * dx()); } if (localmesh->get(d2y, "d2y" + suffix, 0.0, false, location) != 0) { output_warn.write("\tWARNING: differencing quantity 'd2y' not found. " "Calculating from dy\n"); - d1_dy = bout::derivatives::index::DDY(1. / dy()); // d/di(1/dy) + d1_dy_ = bout::derivatives::index::DDY(1. / dy()); // d/di(1/dy) - communicate(d1_dy); - d1_dy = localmesh->interpolateAndExtrapolate(d1_dy, location, true, true, true, - transform.get()); + communicate(d1_dy_); + d1_dy_ = localmesh->interpolateAndExtrapolate(d1_dy_, location, true, true, true, + transform.get()); } else { d2y.setLocation(location); // set boundary cells if necessary d2y = localmesh->interpolateAndExtrapolate(d2y, location, extrapolate_x, extrapolate_y, false, transform.get()); - d1_dy = -d2y / (dy() * dy()); + d1_dy_ = -d2y / (dy() * dy()); } #if BOUT_USE_METRIC_3D if (localmesh->get(d2z, "d2z" + suffix, 0.0, false)) { output_warn.write("\tWARNING: differencing quantity 'd2z' not found. " "Calculating from dz\n"); - d1_dz = bout::derivatives::index::DDZ(1. / dz()); - communicate(d1_dz); - d1_dz = localmesh->interpolateAndExtrapolate(d1_dz, location, true, true, true, - transform.get()); + d1_dz_ = bout::derivatives::index::DDZ(1. / dz()); + communicate(d1_dz_); + d1_dz_ = localmesh->interpolateAndExtrapolate(d1_dz_, location, true, true, true, + transform.get()); } else { d2z.setLocation(location); // set boundary cells if necessary d2z = localmesh->interpolateAndExtrapolate(d2z, location, extrapolate_x, extrapolate_y, false, transform.get()); - d1_dz = -d2z / (dz() * dz()); + d1_dz_ = -d2z / (dz() * dz()); } #else - d1_dz = 0; + d1_dz_ = 0; #endif - communicate(d1_dx, d1_dy, d1_dz); + auto tmp = d1_dx(); // TODO: There must be a better way than this! + communicate(tmp, d1_dy(), d1_dz()); } void Coordinates::calculateCommunicateAndExtrapolateChristoffelSymbols() { @@ -1368,6 +1369,14 @@ void Coordinates::setDz(FieldMetric dz) { dz_ = dz; } void Coordinates::setDy(BoutReal value, int x, int y) { dy_(x, y) = value; } +void Coordinates::setD1_dx(FieldMetric d1_dx) { d1_dx_ = d1_dx; } +void Coordinates::setD1_dy(FieldMetric d1_dy) { d1_dy_ = d1_dy; } +void Coordinates::setD1_dz(FieldMetric d1_dz) { d1_dz_ = d1_dz; } + +const FieldMetric& Coordinates::d1_dx() const { return d1_dx_; } +const FieldMetric& Coordinates::d1_dy() const { return d1_dy_; } +const FieldMetric& Coordinates::d1_dz() const { return d1_dz_; } + void Coordinates::setContravariantMetricTensor(MetricTensor metric_tensor, const std::string& region, bool recalculate_staggered, diff --git a/src/mesh/coordinates_accessor.cxx b/src/mesh/coordinates_accessor.cxx index b8e8124d09..9872caeea7 100644 --- a/src/mesh/coordinates_accessor.cxx +++ b/src/mesh/coordinates_accessor.cxx @@ -54,6 +54,10 @@ CoordinatesAccessor::CoordinatesAccessor(const Coordinates* coords) { data[stripe_size * ind.ind + static_cast(Offset::dy)] = coords->dy()[ind]; data[stripe_size * ind.ind + static_cast(Offset::dz)] = coords->dz()[ind]; + data[stripe_size * ind.ind + static_cast(Offset::d1_dx)] = coords->d1_dx()[ind]; + data[stripe_size * ind.ind + static_cast(Offset::d1_dy)] = coords->d1_dy()[ind]; + data[stripe_size * ind.ind + static_cast(Offset::d1_dz)] = coords->d1_dz()[ind]; + data[stripe_size * ind.ind + static_cast(Offset::J)] = coords->J()[ind]; data[stripe_size * ind.ind + static_cast(Offset::B)] = coords->Bxy()[ind]; diff --git a/src/sys/derivs.cxx b/src/sys/derivs.cxx index 415c150671..8d74caa4af 100644 --- a/src/sys/derivs.cxx +++ b/src/sys/derivs.cxx @@ -159,7 +159,8 @@ Field3D D2DX2(const Field3D& f, CELL_LOC outloc, const std::string& method, if (coords->non_uniform) { // Correction for non-uniform f.getMesh() - result += coords->d1_dx * bout::derivatives::index::DDX(f, outloc, "DEFAULT", region) + result += coords->d1_dx() + * bout::derivatives::index::DDX(f, outloc, "DEFAULT", region) / coords->dx(); } @@ -178,7 +179,8 @@ Coordinates::FieldMetric D2DX2(const Field2D& f, CELL_LOC outloc, if (coords->non_uniform) { // Correction for non-uniform f.getMesh() - result += coords->d1_dx * bout::derivatives::index::DDX(f, outloc, "DEFAULT", region) + result += coords->d1_dx() + * bout::derivatives::index::DDX(f, outloc, "DEFAULT", region) / coords->dx(); } @@ -196,7 +198,8 @@ Field3D D2DY2(const Field3D& f, CELL_LOC outloc, const std::string& method, if (coords->non_uniform) { // Correction for non-uniform f.getMesh() - result += coords->d1_dy * bout::derivatives::index::DDY(f, outloc, "DEFAULT", region) + result += coords->d1_dy() + * bout::derivatives::index::DDY(f, outloc, "DEFAULT", region) / coords->dy(); } @@ -214,7 +217,8 @@ Coordinates::FieldMetric D2DY2(const Field2D& f, CELL_LOC outloc, bout::derivatives::index::D2DY2(f, outloc, method, region) / SQ(coords->dy()); if (coords->non_uniform) { // Correction for non-uniform f.getMesh() - result += coords->d1_dy * bout::derivatives::index::DDY(f, outloc, "DEFAULT", region) + result += coords->d1_dy() + * bout::derivatives::index::DDY(f, outloc, "DEFAULT", region) / coords->dy(); } diff --git a/tests/unit/mesh/test_coordinates_accessor.cxx b/tests/unit/mesh/test_coordinates_accessor.cxx index d13339d1be..d080418f3d 100644 --- a/tests/unit/mesh/test_coordinates_accessor.cxx +++ b/tests/unit/mesh/test_coordinates_accessor.cxx @@ -88,7 +88,9 @@ TEST_F(CoordinatesAccessorTest, ClearBoth) { coords.setG2(0.2); coords.setG3(0.2); coords.non_uniform = true; - coords.d1_dx = coords.d1_dy = coords.d1_dz = 0.1; + coords.setD1_dx(0.1); + coords.setD1_dy(0.1); + coords.setD1_dz(0.1); #if BOUT_USE_METRIC_3D coords.Bxy.splitParallelSlices(); coords.Bxy.yup() = coords.Bxy.ydown() = coords.Bxy; @@ -130,7 +132,9 @@ TEST_F(CoordinatesAccessorTest, ClearOneTwo) { coords.setG2(0.2); coords.setG3(0.2); coords.non_uniform = true; - coords.d1_dx = coords.d1_dy = coords.d1_dz = 0.1; + coords.setD1_dx(0.1); + coords.setD1_dy(0.1); + coords.setD1_dz(0.1); #if BOUT_USE_METRIC_3D coords.Bxy.splitParallelSlices(); coords.Bxy.yup() = coords.Bxy.ydown() = coords.Bxy; @@ -174,7 +178,9 @@ TEST_F(CoordinatesAccessorTest, ClearTwoOneNone) { coords.setG2(0.2); coords.setG3(0.2); coords.non_uniform = true; - coords.d1_dx = coords.d1_dy = coords.d1_dz = 0.1; + coords.setD1_dx(0.1); + coords.setD1_dy(0.1); + coords.setD1_dz(0.1); #if BOUT_USE_METRIC_3D coords.Bxy.splitParallelSlices(); coords.Bxy.yup() = coords.Bxy.ydown() = coords.Bxy; diff --git a/tests/unit/test_extras.hxx b/tests/unit/test_extras.hxx index eba66653c8..9cb8cb5eff 100644 --- a/tests/unit/test_extras.hxx +++ b/tests/unit/test_extras.hxx @@ -448,8 +448,9 @@ public: // Set nonuniform corrections test_coords->non_uniform = true; - test_coords->d1_dx = test_coords->d1_dy = 0.2; - test_coords->d1_dz = 0.0; + test_coords->setD1_dx(0.2); + test_coords->setD1_dy(0.2); + test_coords->setD1_dz(0.0); #if BOUT_USE_METRIC_3D test_coords->Bxy().splitParallelSlices(); test_coords->Bxy().yup() = test_coords->Bxy().ydown() = test_coords->Bxy(); @@ -493,8 +494,9 @@ public: // Set nonuniform corrections test_coords_staggered->non_uniform = true; - test_coords_staggered->d1_dx = test_coords_staggered->d1_dy = 0.2; - test_coords_staggered->d1_dz = 0.0; + test_coords_staggered->setD1_dx(0.2); + test_coords_staggered->setD1_dy(0.2); + test_coords_staggered->setD1_dz(0.0); #if BOUT_USE_METRIC_3D test_coords_staggered->Bxy.splitParallelSlices(); test_coords_staggered->Bxy.yup() = test_coords_staggered->Bxy.ydown() = From f248de9c3b58b1fca668f564f894f6fbc4c3993a Mon Sep 17 00:00:00 2001 From: tomc271 Date: Thu, 7 Dec 2023 09:09:51 +0000 Subject: [PATCH 266/491] Let DataFileFacade::add() take a const value. --- include/bout/physicsmodel.hxx | 2 +- src/physics/physicsmodel.cxx | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/include/bout/physicsmodel.hxx b/include/bout/physicsmodel.hxx index 2b2badf0f3..f34163c4af 100644 --- a/include/bout/physicsmodel.hxx +++ b/include/bout/physicsmodel.hxx @@ -88,7 +88,7 @@ public: void addRepeat(ValueType value, const std::string& name) { add(value, name, true); } void addOnce(ValueType value, const std::string& name) { add(value, name, false); } - void add(ValueType value, const std::string& name, bool save_repeat = false); + void add(const ValueType value, const std::string& name, bool save_repeat = false); void add(Vector2D* value, const std::string& name, bool save_repeat = false); void add(Vector3D* value, const std::string& name, bool save_repeat = false); diff --git a/src/physics/physicsmodel.cxx b/src/physics/physicsmodel.cxx index 9f538895ed..aa4a99f07a 100644 --- a/src/physics/physicsmodel.cxx +++ b/src/physics/physicsmodel.cxx @@ -43,7 +43,7 @@ using namespace std::literals; namespace bout { -void DataFileFacade::add(ValueType value, const std::string& name, bool save_repeat) { +void DataFileFacade::add(const ValueType value, const std::string& name, bool save_repeat) { data.emplace_back(name, value, save_repeat); } void DataFileFacade::add(Vector2D* value, const std::string& name, bool save_repeat) { From 652865896e370a22d0c0da08cf5cf4e4d115241b Mon Sep 17 00:00:00 2001 From: tomc271 Date: Thu, 7 Dec 2023 11:25:14 +0000 Subject: [PATCH 267/491] Make non_uniform private. --- include/bout/coordinates.hxx | 6 +++++- src/invert/laplace/invert_laplace.cxx | 2 +- src/invert/parderiv/impls/cyclic/cyclic.cxx | 2 +- src/mesh/coordinates.cxx | 5 ++++- src/sys/derivs.cxx | 8 ++++---- tests/unit/mesh/test_coordinates_accessor.cxx | 6 +++--- tests/unit/test_extras.hxx | 4 ++-- 7 files changed, 20 insertions(+), 13 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index 0d6cb41f40..8d15dacc6a 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -93,7 +93,8 @@ public: const Field2D& zlength() const; /// True if corrections for non-uniform mesh spacing should be included in operators - bool non_uniform{}; + bool non_uniform() const; + void setNon_uniform(bool non_uniform); /// 2nd-order correction for non-uniform meshes d/di(1/dx), d/di(1/dy) and d/di(1/dz) const FieldMetric& d1_dx() const; @@ -290,6 +291,9 @@ private: DifferentialOperators* differential_operators; Geometry geometry; + /// True if corrections for non-uniform mesh spacing should be included in operators + bool non_uniform_{}; + FieldMetric dx_, dy_, dz_; ///< Mesh spacing in x, y and z /// 2nd-order correction for non-uniform meshes d/di(1/dx), d/di(1/dy) and d/di(1/dz) diff --git a/src/invert/laplace/invert_laplace.cxx b/src/invert/laplace/invert_laplace.cxx index 9c17d8f60f..a54294b5f4 100644 --- a/src/invert/laplace/invert_laplace.cxx +++ b/src/invert/laplace/invert_laplace.cxx @@ -110,7 +110,7 @@ Laplacian::Laplacian(Options* options, const CELL_LOC loc, Mesh* mesh_in, nonuniform = (*options)["nonuniform"] .doc("Use non-uniform grid corrections? Default is the mesh setting.") - .withDefault(coords->non_uniform); + .withDefault(coords->non_uniform()); all_terms = (*options)["all_terms"].doc("Include first derivative terms?").withDefault(true); diff --git a/src/invert/parderiv/impls/cyclic/cyclic.cxx b/src/invert/parderiv/impls/cyclic/cyclic.cxx index 15e176fc00..046639f5e7 100644 --- a/src/invert/parderiv/impls/cyclic/cyclic.cxx +++ b/src/invert/parderiv/impls/cyclic/cyclic.cxx @@ -160,7 +160,7 @@ const Field3D InvertParCR::solve(const Field3D& f) { BoutReal ecoef = E(x, y + local_ystart) + sg(x, y + local_ystart) * B(x, y + local_ystart); // ddy - if (coord->non_uniform) { + if (coord->non_uniform()) { ecoef += bcoef * coord->d1_dy()(x, y + local_ystart); } diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index cc5b5d37c2..e7de2a2678 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -673,7 +673,7 @@ void Coordinates::recalculateAndReset(bool recalculate_staggered, } void Coordinates::correctionForNonUniformMeshes(bool force_interpolate_from_centre) { - OPTION(Options::getRoot(), non_uniform, true); + OPTION(Options::getRoot(), non_uniform_, true); FieldMetric d2x(localmesh); FieldMetric d2y(localmesh); @@ -1359,6 +1359,9 @@ void Coordinates::checkCovariant() { return geometry.checkCovariant(localmesh->y void Coordinates::checkContravariant() { geometry.checkContravariant(localmesh->ystart); } +bool Coordinates::non_uniform() const { return non_uniform_; } +void Coordinates::setNon_uniform(bool non_uniform) { non_uniform_ = non_uniform; } + const FieldMetric& Coordinates::dx() const { return dx_; } const FieldMetric& Coordinates::dy() const { return dy_; } const FieldMetric& Coordinates::dz() const { return dz_; } diff --git a/src/sys/derivs.cxx b/src/sys/derivs.cxx index 8d74caa4af..b48a7b0f49 100644 --- a/src/sys/derivs.cxx +++ b/src/sys/derivs.cxx @@ -157,7 +157,7 @@ Field3D D2DX2(const Field3D& f, CELL_LOC outloc, const std::string& method, Field3D result = bout::derivatives::index::D2DX2(f, outloc, method, region) / SQ(coords->dx()); - if (coords->non_uniform) { + if (coords->non_uniform()) { // Correction for non-uniform f.getMesh() result += coords->d1_dx() * bout::derivatives::index::DDX(f, outloc, "DEFAULT", region) @@ -177,7 +177,7 @@ Coordinates::FieldMetric D2DX2(const Field2D& f, CELL_LOC outloc, auto result = bout::derivatives::index::D2DX2(f, outloc, method, region) / SQ(coords->dx()); - if (coords->non_uniform) { + if (coords->non_uniform()) { // Correction for non-uniform f.getMesh() result += coords->d1_dx() * bout::derivatives::index::DDX(f, outloc, "DEFAULT", region) @@ -196,7 +196,7 @@ Field3D D2DY2(const Field3D& f, CELL_LOC outloc, const std::string& method, Field3D result = bout::derivatives::index::D2DY2(f, outloc, method, region) / SQ(coords->dy()); - if (coords->non_uniform) { + if (coords->non_uniform()) { // Correction for non-uniform f.getMesh() result += coords->d1_dy() * bout::derivatives::index::DDY(f, outloc, "DEFAULT", region) @@ -215,7 +215,7 @@ Coordinates::FieldMetric D2DY2(const Field2D& f, CELL_LOC outloc, auto result = bout::derivatives::index::D2DY2(f, outloc, method, region) / SQ(coords->dy()); - if (coords->non_uniform) { + if (coords->non_uniform()) { // Correction for non-uniform f.getMesh() result += coords->d1_dy() * bout::derivatives::index::DDY(f, outloc, "DEFAULT", region) diff --git a/tests/unit/mesh/test_coordinates_accessor.cxx b/tests/unit/mesh/test_coordinates_accessor.cxx index d080418f3d..c9efe9ec74 100644 --- a/tests/unit/mesh/test_coordinates_accessor.cxx +++ b/tests/unit/mesh/test_coordinates_accessor.cxx @@ -87,7 +87,7 @@ TEST_F(CoordinatesAccessorTest, ClearBoth) { coords.setG1(0.2); coords.setG2(0.2); coords.setG3(0.2); - coords.non_uniform = true; + coords.setNon_uniform(true); coords.setD1_dx(0.1); coords.setD1_dy(0.1); coords.setD1_dz(0.1); @@ -131,7 +131,7 @@ TEST_F(CoordinatesAccessorTest, ClearOneTwo) { coords.setG1(0.2); coords.setG2(0.2); coords.setG3(0.2); - coords.non_uniform = true; + coords.setNon_uniform(true); coords.setD1_dx(0.1); coords.setD1_dy(0.1); coords.setD1_dz(0.1); @@ -177,7 +177,7 @@ TEST_F(CoordinatesAccessorTest, ClearTwoOneNone) { coords.setG1(0.2); coords.setG2(0.2); coords.setG3(0.2); - coords.non_uniform = true; + coords.setNon_uniform(true); coords.setD1_dx(0.1); coords.setD1_dy(0.1); coords.setD1_dz(0.1); diff --git a/tests/unit/test_extras.hxx b/tests/unit/test_extras.hxx index 9cb8cb5eff..1716d5cf4b 100644 --- a/tests/unit/test_extras.hxx +++ b/tests/unit/test_extras.hxx @@ -447,7 +447,7 @@ public: test_coords->setG3(0.1); // Set nonuniform corrections - test_coords->non_uniform = true; + test_coords->setNon_uniform(true); test_coords->setD1_dx(0.2); test_coords->setD1_dy(0.2); test_coords->setD1_dz(0.0); @@ -493,7 +493,7 @@ public: test_coords_staggered->setG3(0.1); // Set nonuniform corrections - test_coords_staggered->non_uniform = true; + test_coords_staggered->setNon_uniform(true); test_coords_staggered->setD1_dx(0.2); test_coords_staggered->setD1_dy(0.2); test_coords_staggered->setD1_dz(0.0); From 3b3915e4ed121485d231a62ddcdcc8780db922e3 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Thu, 7 Dec 2023 13:41:38 +0000 Subject: [PATCH 268/491] Remove call to calculateGeometry() after setting metric tensor quantities. --- tests/MMS/diffusion2/diffusion.cxx | 5 +---- tests/MMS/elm-pb/elm_pb.cxx | 8 ++------ tests/MMS/tokamak/tokamak.cxx | 8 ++------ tests/integrated/test-laplacexy/loadmetric.cxx | 8 ++------ 4 files changed, 7 insertions(+), 22 deletions(-) diff --git a/tests/MMS/diffusion2/diffusion.cxx b/tests/MMS/diffusion2/diffusion.cxx index 70010b0c49..347f595c66 100644 --- a/tests/MMS/diffusion2/diffusion.cxx +++ b/tests/MMS/diffusion2/diffusion.cxx @@ -38,15 +38,12 @@ class Diffusion : public PhysicsModel { SAVE_ONCE3(Dx, Dy, Dz); // set mesh - auto contravariant_metric_tensor = - MetricTensor(1.1, 1.1, 1.1, 0.0, 0.0, 0.0); + auto contravariant_metric_tensor = MetricTensor(1.1, 1.1, 1.1, 0.0, 0.0, 0.0); coords->setContravariantMetricTensor(contravariant_metric_tensor); auto covariant_metric_tensor = MetricTensor(1.1, 1.1, 1.1, 0.0, 0.0, 0.0); coords->setCovariantMetricTensor(covariant_metric_tensor); - coords->geometry(); - // Tell BOUT++ to solve N SOLVE_FOR(N); diff --git a/tests/MMS/elm-pb/elm_pb.cxx b/tests/MMS/elm-pb/elm_pb.cxx index 81db0d4eb0..a227f0f931 100644 --- a/tests/MMS/elm-pb/elm_pb.cxx +++ b/tests/MMS/elm-pb/elm_pb.cxx @@ -423,8 +423,7 @@ class ELMpb : public PhysicsModel { const auto g12 = 0.0; const auto g13 = -I * coords->g11(); const auto g23 = -Btxy / (hthe * Bpxy * Rxy); - coords->setContravariantMetricTensor( - MetricTensor(g11, g22, g33, g12, g13, g23)); + coords->setContravariantMetricTensor(MetricTensor(g11, g22, g33, g12, g13, g23)); coords->setJ(hthe / Bpxy); coords->setBxy(B0); @@ -435,10 +434,7 @@ class ELMpb : public PhysicsModel { const auto g_12 = Btxy * hthe * I * Rxy / Bpxy; const auto g_13 = I * Rxy * Rxy; const auto g_23 = Btxy * hthe * Rxy / Bpxy; - coords->setCovariantMetricTensor( - MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); - - coords->geometry(); // Calculate quantities from metric tensor + coords->setCovariantMetricTensor(MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); // Set B field vector diff --git a/tests/MMS/tokamak/tokamak.cxx b/tests/MMS/tokamak/tokamak.cxx index ac46d69b93..2a4bb19d97 100644 --- a/tests/MMS/tokamak/tokamak.cxx +++ b/tests/MMS/tokamak/tokamak.cxx @@ -86,8 +86,7 @@ class TokamakMMS : public PhysicsModel { const auto g12 = 0.0; const auto g13 = -sinty * coords->g11(); const auto g23 = -sbp * Btxy / (hthe * Bpxy * Rxy); - coords->setContravariantMetricTensor( - MetricTensor(g11, g22, g33, g12, g13, g23)); + coords->setContravariantMetricTensor(MetricTensor(g11, g22, g33, g12, g13, g23)); coords->setJ(hthe / Bpxy); @@ -97,10 +96,7 @@ class TokamakMMS : public PhysicsModel { const auto g_12 = sbp * Btxy * hthe * sinty * Rxy / Bpxy; const auto g_13 = sinty * Rxy * Rxy; const auto g_23 = sbp * Btxy * hthe * Rxy / Bpxy; - coords->setCovariantMetricTensor( - MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); - - coords->geometry(); + coords->setCovariantMetricTensor(MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); } private: diff --git a/tests/integrated/test-laplacexy/loadmetric.cxx b/tests/integrated/test-laplacexy/loadmetric.cxx index 347e82b224..2e8c12e2ef 100644 --- a/tests/integrated/test-laplacexy/loadmetric.cxx +++ b/tests/integrated/test-laplacexy/loadmetric.cxx @@ -55,8 +55,7 @@ void LoadMetric(BoutReal Lnorm, BoutReal Bnorm) { const auto g12 = 0.0; const auto g13 = -sinty * coords->g11(); const auto g23 = -sbp * Btxy / (hthe * Bpxy * Rxy); - coords->setContravariantMetricTensor( - MetricTensor(g11, g22, g33, g12, g13, g23)); + coords->setContravariantMetricTensor(MetricTensor(g11, g22, g33, g12, g13, g23)); coords->setJ(hthe / Bpxy); @@ -66,8 +65,5 @@ void LoadMetric(BoutReal Lnorm, BoutReal Bnorm) { const auto g_12 = sbp * Btxy * hthe * sinty * Rxy / Bpxy; const auto g_13 = sinty * Rxy * Rxy; const auto g_23 = sbp * Btxy * hthe * Rxy / Bpxy; - coords->setCovariantMetricTensor( - MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); - - coords->geometry(); + coords->setCovariantMetricTensor(MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); } From 05ea951b1f69cbb63760847a1bb40c7490c107de Mon Sep 17 00:00:00 2001 From: tomc271 Date: Fri, 8 Dec 2023 13:02:08 +0000 Subject: [PATCH 269/491] Clang-Tidy: Constructors that are callable with a single argument must be marked explicit to avoid unintentional implicit conversions. --- include/bout/coordinates.hxx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index 8d15dacc6a..944b56346a 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -56,9 +56,9 @@ public: /// force_interpolate_from_centre argument to true to always interpolate /// (useful if CELL_CENTRE Coordinates have been changed, so reading from file /// would not be correct). - Coordinates(Mesh* mesh, Options* options = nullptr, CELL_LOC loc = CELL_CENTRE, - const Coordinates* coords_in = nullptr, - bool force_interpolate_from_centre = false); + explicit Coordinates(Mesh* mesh, Options* options = nullptr, CELL_LOC loc = CELL_CENTRE, + const Coordinates* coords_in = nullptr, + bool force_interpolate_from_centre = false); /// A constructor useful for testing purposes. To use it, inherit /// from Coordinates. If \p calculate_geometry is true (default), From 46a11ae6c9935cdfc067e130b17fb3bb0cf9f1e7 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Fri, 8 Dec 2023 13:47:20 +0000 Subject: [PATCH 270/491] Make ShiftTorsion and IntShiftTorsion private. --- examples/6field-simple/elm_6f.cxx | 2 +- .../elm-pb-outerloop/elm_pb_outerloop.cxx | 40 +++++++------ examples/elm-pb/elm_pb.cxx | 40 +++++++------ include/bout/coordinates.hxx | 26 ++++++--- src/field/vecops.cxx | 4 +- .../laplace/impls/petsc/petsc_laplace.cxx | 4 +- src/invert/laplace/invert_laplace.cxx | 4 +- src/mesh/coordinates.cxx | 57 +++++++++++-------- src/mesh/difops.cxx | 4 +- tests/MMS/elm-pb/elm_pb.cxx | 2 +- tools/pylib/_boutpp_build/boutpp.pyx.jinja | 8 +-- 11 files changed, 106 insertions(+), 85 deletions(-) diff --git a/examples/6field-simple/elm_6f.cxx b/examples/6field-simple/elm_6f.cxx index 2c515f89ce..f1e6659aec 100644 --- a/examples/6field-simple/elm_6f.cxx +++ b/examples/6field-simple/elm_6f.cxx @@ -705,7 +705,7 @@ class Elm_6f : public PhysicsModel { if (mesh->IncIntShear) { // BOUT-06 style, using d/dx = d/dpsi + I * d/dz - coord->IntShiftTorsion = I; + coord->setIntShiftTorsion(I); } else { // Dimits style, using local coordinate system diff --git a/examples/elm-pb-outerloop/elm_pb_outerloop.cxx b/examples/elm-pb-outerloop/elm_pb_outerloop.cxx index caa63c710f..12c6a436a5 100644 --- a/examples/elm-pb-outerloop/elm_pb_outerloop.cxx +++ b/examples/elm-pb-outerloop/elm_pb_outerloop.cxx @@ -769,7 +769,7 @@ class ELMpb : public PhysicsModel { BoutReal angle = rmp_m * pol_angle(jx, jy) - + rmp_n * ((BoutReal)jz) * mesh->getCoordinates()->dz(jx, jy, jz); + + rmp_n * ((BoutReal)jz) * mesh->getCoordinates()->dz()(jx, jy, jz); rmp_Psi0(jx, jy, jz) = (((BoutReal)(jx - 4)) / ((BoutReal)(mesh->LocalNx - 5))) * rmp_factor * cos(angle); @@ -825,7 +825,7 @@ class ELMpb : public PhysicsModel { if (mesh->IncIntShear) { // BOUT-06 style, using d/dx = d/dpsi + I * d/dz - metric->IntShiftTorsion = I; + metric->setIntShiftTorsion(I); } else { // Dimits style, using local coordinate system @@ -1092,22 +1092,26 @@ class ELMpb : public PhysicsModel { /**************** CALCULATE METRICS ******************/ - metric->g11 = SQ(Rxy * Bpxy); - metric->g22 = 1.0 / SQ(hthe); - metric->g33 = SQ(I) * metric->g11 + SQ(B0) / metric->g11; - metric->g12 = 0.0; - metric->g13 = -I * metric->g11; - metric->g23 = -Btxy / (hthe * Bpxy * Rxy); + MetricTensor::FieldMetric g11, g22, g33, g12, g13, g23; + g11 = SQ(Rxy * Bpxy); + g22 = 1.0 / SQ(hthe); + g33 = SQ(I) * metric->g11() + SQ(B0) / metric->g11(); + g12 = 0.0; + g13 = -I * metric->g11(); + g23 = -Btxy / (hthe * Bpxy * Rxy); + metric->setContravariantMetricTensor(MetricTensor(g11, g22, g33, g12, g13, g23)); metric->setJ(hthe / Bpxy); metric->setBxy(B0); - metric->g_11 = 1.0 / metric->g11 + SQ(I * Rxy); - metric->g_22 = SQ(B0 * hthe / Bpxy); - metric->g_33 = Rxy * Rxy; - metric->g_12 = Btxy * hthe * I * Rxy / Bpxy; - metric->g_13 = I * Rxy * Rxy; - metric->g_23 = Btxy * hthe * Rxy / Bpxy; + MetricTensor::FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; + g_11 = 1.0 / metric->g11() + SQ(I * Rxy); + g_22 = SQ(B0 * hthe / Bpxy); + g_33 = Rxy * Rxy; + g_12 = Btxy * hthe * I * Rxy / Bpxy; + g_13 = I * Rxy * Rxy; + g_23 = Btxy * hthe * Rxy / Bpxy; + metric->setCovariantMetricTensor(MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); // Set B field vector @@ -1790,11 +1794,11 @@ class ELMpb : public PhysicsModel { if (hyperviscos > 0.0) { // Calculate coefficient. - hyper_mu_x = hyperviscos * metric->g_11 * SQ(metric->dx()) - * abs(metric->g11 * D2DX2(U)) / (abs(U) + 1e-3); + hyper_mu_x = hyperviscos * metric->g_11() * SQ(metric->dx()) + * abs(metric->g11() * D2DX2(U)) / (abs(U) + 1e-3); hyper_mu_x.applyBoundary("dirichlet"); // Set to zero on all boundaries - ddt(U) += hyper_mu_x * metric->g11 * D2DX2(U); + ddt(U) += hyper_mu_x * metric->g11() * D2DX2(U); if (first_run) { // Print out maximum values of viscosity used on this processor output.write(" Hyper-viscosity values:\n"); @@ -1905,7 +1909,7 @@ class ELMpb : public PhysicsModel { BoutReal pnorm = P0(0, 0); ddt(P) += heating_P * source_expx2(P0, 2. * hp_width, 0.5 * hp_length) * (Tbar / pnorm); // heat source - ddt(P) += (100. * source_tanhx(P0, hp_width, hp_length) + 0.01) * metric->g11 + ddt(P) += (100. * source_tanhx(P0, hp_width, hp_length) + 0.01) * metric->g11() * D2DX2(P) * (Tbar / Lbar / Lbar); // radial diffusion } diff --git a/examples/elm-pb/elm_pb.cxx b/examples/elm-pb/elm_pb.cxx index df54764676..21ec924129 100644 --- a/examples/elm-pb/elm_pb.cxx +++ b/examples/elm-pb/elm_pb.cxx @@ -710,7 +710,7 @@ class ELMpb : public PhysicsModel { BoutReal angle = rmp_m * pol_angle(jx, jy) - + rmp_n * ((BoutReal)jz) * mesh->getCoordinates()->dz(jx, jy, jz); + + rmp_n * ((BoutReal)jz) * mesh->getCoordinates()->dz()(jx, jy, jz); rmp_Psi0(jx, jy, jz) = (((BoutReal)(jx - 4)) / ((BoutReal)(mesh->LocalNx - 5))) * rmp_factor * cos(angle); @@ -766,7 +766,7 @@ class ELMpb : public PhysicsModel { if (mesh->IncIntShear) { // BOUT-06 style, using d/dx = d/dpsi + I * d/dz - metric->IntShiftTorsion = I; + metric->setIntShiftTorsion(I); } else { // Dimits style, using local coordinate system @@ -1033,22 +1033,26 @@ class ELMpb : public PhysicsModel { /**************** CALCULATE METRICS ******************/ - metric->g11 = SQ(Rxy * Bpxy); - metric->g22 = 1.0 / SQ(hthe); - metric->g33 = SQ(I) * metric->g11 + SQ(B0) / metric->g11; - metric->g12 = 0.0; - metric->g13 = -I * metric->g11; - metric->g23 = -Btxy / (hthe * Bpxy * Rxy); + MetricTensor::FieldMetric g11, g22, g33, g12, g13, g23; + g11 = SQ(Rxy * Bpxy); + g22 = 1.0 / SQ(hthe); + g33 = SQ(I) * metric->g11() + SQ(B0) / metric->g11(); + g12 = 0.0; + g13 = -I * metric->g11(); + g23 = -Btxy / (hthe * Bpxy * Rxy); + metric->setContravariantMetricTensor(MetricTensor(g11, g22, g33, g12, g13, g23)); metric->setJ(hthe / Bpxy); metric->setBxy(B0); - metric->g_11 = 1.0 / metric->g11 + SQ(I * Rxy); - metric->g_22 = SQ(B0 * hthe / Bpxy); - metric->g_33 = Rxy * Rxy; - metric->g_12 = Btxy * hthe * I * Rxy / Bpxy; - metric->g_13 = I * Rxy * Rxy; - metric->g_23 = Btxy * hthe * Rxy / Bpxy; + MetricTensor::FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; + g_11 = 1.0 / metric->g11() + SQ(I * Rxy); + g_22 = SQ(B0 * hthe / Bpxy); + g_33 = Rxy * Rxy; + g_12 = Btxy * hthe * I * Rxy / Bpxy; + g_13 = I * Rxy * Rxy; + g_23 = Btxy * hthe * Rxy / Bpxy; + metric->setCovariantMetricTensor(MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); // Set B field vector @@ -1589,11 +1593,11 @@ class ELMpb : public PhysicsModel { if (hyperviscos > 0.0) { // Calculate coefficient. - hyper_mu_x = hyperviscos * metric->g_11 * SQ(metric->dx()) - * abs(metric->g11 * D2DX2(U)) / (abs(U) + 1e-3); + hyper_mu_x = hyperviscos * metric->g_11() * SQ(metric->dx()) + * abs(metric->g11() * D2DX2(U)) / (abs(U) + 1e-3); hyper_mu_x.applyBoundary("dirichlet"); // Set to zero on all boundaries - ddt(U) += hyper_mu_x * metric->g11 * D2DX2(U); + ddt(U) += hyper_mu_x * metric->g11() * D2DX2(U); if (first_run) { // Print out maximum values of viscosity used on this processor output.write(" Hyper-viscosity values:\n"); @@ -1704,7 +1708,7 @@ class ELMpb : public PhysicsModel { BoutReal pnorm = P0(0, 0); ddt(P) += heating_P * source_expx2(P0, 2. * hp_width, 0.5 * hp_length) * (Tbar / pnorm); // heat source - ddt(P) += (100. * source_tanhx(P0, hp_width, hp_length) + 0.01) * metric->g11 + ddt(P) += (100. * source_tanhx(P0, hp_width, hp_length) + 0.01) * metric->g11() * D2DX2(P) * (Tbar / Lbar / Lbar); // radial diffusion } diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index 944b56346a..0f1951d7d0 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -69,7 +69,7 @@ public: const FieldMetric& g12, const FieldMetric& g13, const FieldMetric& g23, const FieldMetric& g_11, const FieldMetric& g_22, const FieldMetric& g_33, const FieldMetric& g_12, const FieldMetric& g_13, const FieldMetric& g_23, - FieldMetric ShiftTorsion, FieldMetric IntShiftTorsion); + const FieldMetric& ShiftTorsion, const FieldMetric& IntShiftTorsion); /// Add variables to \p output_options, for post-processing void outputVars(Options& output_options); @@ -117,12 +117,6 @@ public: const FieldMetric& g13() const; const FieldMetric& g23() const; - ///< Coordinate system Jacobian, so volume of cell is J*dx*dy*dz - const FieldMetric& J() const; - - ///< Magnitude of B = nabla z times nabla x - const FieldMetric& Bxy() const; - void setContravariantMetricTensor(MetricTensor metric_tensor, const std::string& region = "RGN_ALL", bool recalculate_staggered = true, @@ -133,15 +127,23 @@ public: bool recalculate_staggered = true, bool force_interpolate_from_centre = false); + ///< Coordinate system Jacobian, so volume of cell is J*dx*dy*dz + const FieldMetric& J() const; + + ///< Magnitude of B = nabla z times nabla x + const FieldMetric& Bxy() const; + void setJ(FieldMetric J); void setJ(BoutReal value, int x, int y); void setBxy(FieldMetric Bxy); /// d pitch angle / dx. Needed for vector differentials (Curl) - FieldMetric ShiftTorsion; + const FieldMetric& ShiftTorsion() const; - FieldMetric IntShiftTorsion; ///< Integrated shear (I in BOUT notation) + ///< Integrated shear (I in BOUT notation) + const FieldMetric& IntShiftTorsion() const; + void setIntShiftTorsion(FieldMetric IntShiftTorsion); const MetricTensor& getContravariantMetricTensor() const; @@ -299,6 +301,12 @@ private: /// 2nd-order correction for non-uniform meshes d/di(1/dx), d/di(1/dy) and d/di(1/dz) FieldMetric d1_dx_, d1_dy_, d1_dz_; + /// d pitch angle / dx. Needed for vector differentials (Curl) + FieldMetric ShiftTorsion_; + + ///< Integrated shear (I in BOUT notation) + FieldMetric IntShiftTorsion_; + /// Handles calculation of yup and ydown std::unique_ptr transform{nullptr}; diff --git a/src/field/vecops.cxx b/src/field/vecops.cxx index 23bb75ee1a..fb6b12ce4a 100644 --- a/src/field/vecops.cxx +++ b/src/field/vecops.cxx @@ -282,7 +282,7 @@ Vector2D Curl(const Vector2D& v) { result.z = (DDX(vco.y) - DDY(vco.x)) / metric->J(); /// Coordinate torsion - result.z -= metric->ShiftTorsion * vco.z / metric->J(); + result.z -= metric->ShiftTorsion() * vco.z / metric->J(); result.setLocation(v.getLocation()); @@ -310,7 +310,7 @@ Vector3D Curl(const Vector3D& v) { result.z = (DDX(vco.y) - DDY(vco.x)) / metric->J(); // Coordinate torsion - result.z -= metric->ShiftTorsion * vco.z / metric->J(); + result.z -= metric->ShiftTorsion() * vco.z / metric->J(); result.setLocation(v.getLocation()); diff --git a/src/invert/laplace/impls/petsc/petsc_laplace.cxx b/src/invert/laplace/impls/petsc/petsc_laplace.cxx index 623792ff9c..d2275bd789 100644 --- a/src/invert/laplace/impls/petsc/petsc_laplace.cxx +++ b/src/invert/laplace/impls/petsc/petsc_laplace.cxx @@ -1029,8 +1029,8 @@ void LaplacePetsc::Coeffs(int x, int y, int z, BoutReal& coef1, BoutReal& coef2, if (localmesh->IncIntShear) { // d2dz2 term - coef2 += coords->g11(x, y, z) * coords->IntShiftTorsion(x, y, z) - * coords->IntShiftTorsion(x, y, z); + coef2 += coords->g11(x, y, z) * coords->IntShiftTorsion()(x, y, z) + * coords->IntShiftTorsion()(x, y, z); // Mixed derivative coef3 = 0.0; // This cancels out } diff --git a/src/invert/laplace/invert_laplace.cxx b/src/invert/laplace/invert_laplace.cxx index a54294b5f4..d11421d041 100644 --- a/src/invert/laplace/invert_laplace.cxx +++ b/src/invert/laplace/invert_laplace.cxx @@ -367,8 +367,8 @@ void Laplacian::tridagCoefs(int jx, int jy, BoutReal kwave, dcomplex& a, dcomple if (localmesh->IncIntShear) { // d2dz2 term - coef2 += localcoords->g11()(jx, jy) * localcoords->IntShiftTorsion(jx, jy) - * localcoords->IntShiftTorsion(jx, jy); + coef2 += localcoords->g11()(jx, jy) * localcoords->IntShiftTorsion()(jx, jy) + * localcoords->IntShiftTorsion()(jx, jy); // Mixed derivative coef3 = 0.0; // This cancels out } diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index e7de2a2678..558d4187a6 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -288,21 +288,22 @@ Coordinates::Coordinates(Mesh* mesh, const FieldMetric& dx, const FieldMetric& d const FieldMetric& g23, const FieldMetric& g_11, const FieldMetric& g_22, const FieldMetric& g_33, const FieldMetric& g_12, const FieldMetric& g_13, - const FieldMetric& g_23, FieldMetric ShiftTorsion, - FieldMetric IntShiftTorsion) - : ShiftTorsion(std::move(ShiftTorsion)), IntShiftTorsion(std::move(IntShiftTorsion)), - nz(mesh->LocalNz), localmesh(mesh), location(CELL_CENTRE), + const FieldMetric& g_23, const FieldMetric& ShiftTorsion, + const FieldMetric& IntShiftTorsion) + : nz(mesh->LocalNz), localmesh(mesh), location(CELL_CENTRE), differential_operators(mesh->getDifferentialOperators()), geometry(Geometry(J, Bxy, g11, g22, g33, g12, g13, g23, g_11, g_22, g_33, g_12, g_13, g_23, differential_operators)), - dx_(dx), dy_(dy), dz_(dz) {} + dx_(dx), dy_(dy), dz_(dz), ShiftTorsion_(ShiftTorsion), + IntShiftTorsion_(IntShiftTorsion) {} Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, const Coordinates* coords_in, bool force_interpolate_from_centre) - : dx_(1., mesh), dy_(1., mesh), dz_(1., mesh), d1_dx_(mesh), d1_dy_(mesh), - d1_dz_(mesh), ShiftTorsion(mesh), IntShiftTorsion(mesh), localmesh(mesh), - location(loc), differential_operators(mesh->getDifferentialOperators()), - geometry(Geometry(mesh, differential_operators)) { + : localmesh(mesh), location(loc), + differential_operators(mesh->getDifferentialOperators()), + geometry(Geometry(mesh, differential_operators)), dx_(1., mesh), dy_(1., mesh), + dz_(1., mesh), d1_dx_(mesh), d1_dy_(mesh), d1_dz_(mesh), ShiftTorsion_(mesh), + IntShiftTorsion_(mesh) { if (options == nullptr) { options = Options::getRoot()->getSection("mesh"); @@ -350,12 +351,12 @@ void Coordinates::interpolateFieldsFromOtherCoordinates(const Mesh* mesh, bout::checkFinite(Bxy(), "Bxy", "RGN_NOCORNERS"); bout::checkPositive(Bxy(), "Bxy", "RGN_NOCORNERS"); - ShiftTorsion = localmesh->interpolateAndExtrapolate(coords_in->ShiftTorsion, location, - true, true, false, transform.get()); + ShiftTorsion_ = localmesh->interpolateAndExtrapolate( + coords_in->ShiftTorsion(), location, true, true, false, transform.get()); if (mesh->IncIntShear) { - IntShiftTorsion = localmesh->interpolateAndExtrapolate( - coords_in->IntShiftTorsion, location, true, true, false, transform.get()); + IntShiftTorsion_ = localmesh->interpolateAndExtrapolate( + coords_in->IntShiftTorsion(), location, true, true, false, transform.get()); } if (isUniform(coords_in->dz())) { @@ -556,28 +557,28 @@ void Coordinates::setBoundaryCells(Mesh* mesh, Options* options, bout::checkFinite(Bxy(), "Bxy" + suffix, "RGN_NOCORNERS"); bout::checkPositive(Bxy(), "Bxy" + suffix, "RGN_NOCORNERS"); - if (getAtLoc(mesh, ShiftTorsion, "ShiftTorsion", suffix, location) != 0) { + if (getAtLoc(mesh, ShiftTorsion_, "ShiftTorsion", suffix, location) != 0) { output_warn.write("\tWARNING: No Torsion specified for zShift. " "Derivatives may not be correct\n"); - ShiftTorsion = 0.0; + ShiftTorsion_ = 0.0; } - ShiftTorsion = localmesh->interpolateAndExtrapolate( - ShiftTorsion, location, extrapolate_x, extrapolate_y, false, transform.get()); + ShiftTorsion_ = localmesh->interpolateAndExtrapolate( + ShiftTorsion_, location, extrapolate_x, extrapolate_y, false, transform.get()); ////////////////////////////////////////////////////// if (mesh->IncIntShear) { checkStaggeredGet(mesh, "IntShiftTorsion", suffix); - if (mesh->get(IntShiftTorsion, "IntShiftTorsion" + suffix, 0.0, false) != 0) { + if (mesh->get(IntShiftTorsion_, "IntShiftTorsion" + suffix, 0.0, false) != 0) { output_warn.write("\tWARNING: No Integrated torsion specified\n"); - IntShiftTorsion = 0.0; + IntShiftTorsion_ = 0.0; } - IntShiftTorsion.setLocation(location); - IntShiftTorsion = localmesh->interpolateAndExtrapolate( - IntShiftTorsion, location, extrapolate_x, extrapolate_y, false, transform.get()); + IntShiftTorsion_.setLocation(location); + IntShiftTorsion_ = localmesh->interpolateAndExtrapolate( + IntShiftTorsion_, location, extrapolate_x, extrapolate_y, false, transform.get()); } else { // IntShiftTorsion will not be used, but set to zero to avoid uninitialized field - IntShiftTorsion = 0.; + IntShiftTorsion_ = 0.; } } @@ -931,7 +932,7 @@ Field2D Coordinates::DDX(const Field2D& f, CELL_LOC loc, const std::string& meth Field3D Coordinates::DDX(const Field3D& f, CELL_LOC outloc, const std::string& method, const std::string& region) const { - return differential_operators->DDX(f, dx(), dz(), IntShiftTorsion, outloc, method, + return differential_operators->DDX(f, dx(), dz(), IntShiftTorsion_, outloc, method, region); } @@ -1380,6 +1381,14 @@ const FieldMetric& Coordinates::d1_dx() const { return d1_dx_; } const FieldMetric& Coordinates::d1_dy() const { return d1_dy_; } const FieldMetric& Coordinates::d1_dz() const { return d1_dz_; } +const FieldMetric& Coordinates::ShiftTorsion() const { return ShiftTorsion_; } + +const FieldMetric& Coordinates::IntShiftTorsion() const { return IntShiftTorsion_; } + +void Coordinates::setIntShiftTorsion(FieldMetric IntShiftTorsion) { + IntShiftTorsion_ = IntShiftTorsion; +} + void Coordinates::setContravariantMetricTensor(MetricTensor metric_tensor, const std::string& region, bool recalculate_staggered, diff --git a/src/mesh/difops.cxx b/src/mesh/difops.cxx index 2a67c33178..51c1e59194 100644 --- a/src/mesh/difops.cxx +++ b/src/mesh/difops.cxx @@ -512,7 +512,7 @@ Field3D b0xGrad_dot_Grad(const Field2D& phi, const Field3D& A, CELL_LOC outloc) if (mesh->IncIntShear) { // BOUT-06 style differencing - vz += metric->IntShiftTorsion * vx; + vz += metric->IntShiftTorsion() * vx; } // Upwind A using these velocities @@ -590,7 +590,7 @@ Field3D b0xGrad_dot_Grad(const Field3D& phi, const Field3D& A, CELL_LOC outloc) if (mesh->IncIntShear) { // BOUT-06 style differencing - vz += metric->IntShiftTorsion * vx; + vz += metric->IntShiftTorsion() * vx; } Field3D result = VDDX(vx, A, outloc) + VDDY(vy, A, outloc) + VDDZ(vz, A, outloc); diff --git a/tests/MMS/elm-pb/elm_pb.cxx b/tests/MMS/elm-pb/elm_pb.cxx index a227f0f931..36a4fee088 100644 --- a/tests/MMS/elm-pb/elm_pb.cxx +++ b/tests/MMS/elm-pb/elm_pb.cxx @@ -314,7 +314,7 @@ class ELMpb : public PhysicsModel { if (ShiftXderivs) { if (mesh->IncIntShear) { // BOUT-06 style, using d/dx = d/dpsi + I * d/dz - coords->IntShiftTorsion = I; + coords->setIntShiftTorsion(I); } else { // Dimits style, using local coordinate system diff --git a/tools/pylib/_boutpp_build/boutpp.pyx.jinja b/tools/pylib/_boutpp_build/boutpp.pyx.jinja index ef610bcf57..ce97dc2e14 100644 --- a/tools/pylib/_boutpp_build/boutpp.pyx.jinja +++ b/tools/pylib/_boutpp_build/boutpp.pyx.jinja @@ -837,17 +837,13 @@ cdef class Coordinates: cdef {{ metric_field }} G2_11, G2_22, G2_33, G2_12, G2_13, G2_23 cdef {{ metric_field }} G3_11, G3_22, G3_33, G3_12, G3_13, G3_23 cdef {{ metric_field }} G1, G2, G3 - cdef public {{ metric_field }} ShiftTorsion - cdef public {{ metric_field }} IntShiftTorsion + cdef {{ metric_field }} ShiftTorsion + cdef {{ metric_field }} IntShiftTorsion def __init__(self): self.cobj = NULL self.isSelfOwned = False - def _setmembers(self): -{% for f in "ShiftTorsion", "IntShiftTorsion" %} - self.{{f}} = {{ metric_field.fdd }}FromPtr(&self.cobj.{{f}}) -{% endfor %} def __dealloc__(self): self.__boutpp_dealloc() From 62d19fb749ac0443b6b867668b021c569268ecf5 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Fri, 8 Dec 2023 13:52:56 +0000 Subject: [PATCH 271/491] Clang-Tidy: Pass by value and use std::move(). --- include/bout/coordinates.hxx | 14 +++++------ src/mesh/coordinates.cxx | 46 ++++++++++++++++++------------------ 2 files changed, 30 insertions(+), 30 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index 0f1951d7d0..0a48b235dd 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -63,13 +63,13 @@ public: /// A constructor useful for testing purposes. To use it, inherit /// from Coordinates. If \p calculate_geometry is true (default), /// calculate the non-uniform variables, Christoffel symbols - Coordinates(Mesh* mesh, const FieldMetric& dx, const FieldMetric& dy, - const FieldMetric& dz, const FieldMetric& J, const FieldMetric& Bxy, - const FieldMetric& g11, const FieldMetric& g22, const FieldMetric& g33, - const FieldMetric& g12, const FieldMetric& g13, const FieldMetric& g23, - const FieldMetric& g_11, const FieldMetric& g_22, const FieldMetric& g_33, - const FieldMetric& g_12, const FieldMetric& g_13, const FieldMetric& g_23, - const FieldMetric& ShiftTorsion, const FieldMetric& IntShiftTorsion); + Coordinates(Mesh* mesh, FieldMetric dx, FieldMetric dy, FieldMetric dz, + const FieldMetric& J, const FieldMetric& Bxy, const FieldMetric& g11, + const FieldMetric& g22, const FieldMetric& g33, const FieldMetric& g12, + const FieldMetric& g13, const FieldMetric& g23, const FieldMetric& g_11, + const FieldMetric& g_22, const FieldMetric& g_33, const FieldMetric& g_12, + const FieldMetric& g_13, const FieldMetric& g_23, FieldMetric ShiftTorsion, + FieldMetric IntShiftTorsion); /// Add variables to \p output_options, for post-processing void outputVars(Options& output_options); diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 558d4187a6..341c1c8786 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -280,22 +280,22 @@ Coordinates::FieldMetric Coordinates::getUnalignedAtLocationAndFillGuards( pParallelTransform); } -Coordinates::Coordinates(Mesh* mesh, const FieldMetric& dx, const FieldMetric& dy, - const FieldMetric& dz, const FieldMetric& J, - const FieldMetric& Bxy, const FieldMetric& g11, - const FieldMetric& g22, const FieldMetric& g33, - const FieldMetric& g12, const FieldMetric& g13, - const FieldMetric& g23, const FieldMetric& g_11, - const FieldMetric& g_22, const FieldMetric& g_33, - const FieldMetric& g_12, const FieldMetric& g_13, - const FieldMetric& g_23, const FieldMetric& ShiftTorsion, - const FieldMetric& IntShiftTorsion) +Coordinates::Coordinates(Mesh* mesh, FieldMetric dx, FieldMetric dy, FieldMetric dz, + const FieldMetric& J, const FieldMetric& Bxy, + const FieldMetric& g11, const FieldMetric& g22, + const FieldMetric& g33, const FieldMetric& g12, + const FieldMetric& g13, const FieldMetric& g23, + const FieldMetric& g_11, const FieldMetric& g_22, + const FieldMetric& g_33, const FieldMetric& g_12, + const FieldMetric& g_13, const FieldMetric& g_23, + FieldMetric ShiftTorsion, FieldMetric IntShiftTorsion) : nz(mesh->LocalNz), localmesh(mesh), location(CELL_CENTRE), differential_operators(mesh->getDifferentialOperators()), geometry(Geometry(J, Bxy, g11, g22, g33, g12, g13, g23, g_11, g_22, g_33, g_12, g_13, g_23, differential_operators)), - dx_(dx), dy_(dy), dz_(dz), ShiftTorsion_(ShiftTorsion), - IntShiftTorsion_(IntShiftTorsion) {} + dx_(std::move(dx)), dy_(std::move(dy)), dz_(std::move(dz)), + ShiftTorsion_(std::move(ShiftTorsion)), + IntShiftTorsion_(std::move(IntShiftTorsion)) {} Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, const Coordinates* coords_in, bool force_interpolate_from_centre) @@ -1367,15 +1367,15 @@ const FieldMetric& Coordinates::dx() const { return dx_; } const FieldMetric& Coordinates::dy() const { return dy_; } const FieldMetric& Coordinates::dz() const { return dz_; } -void Coordinates::setDx(FieldMetric dx) { dx_ = dx; } -void Coordinates::setDy(FieldMetric dy) { dy_ = dy; } -void Coordinates::setDz(FieldMetric dz) { dz_ = dz; } +void Coordinates::setDx(FieldMetric dx) { dx_ = std::move(dx); } +void Coordinates::setDy(FieldMetric dy) { dy_ = std::move(dy); } +void Coordinates::setDz(FieldMetric dz) { dz_ = std::move(dz); } void Coordinates::setDy(BoutReal value, int x, int y) { dy_(x, y) = value; } -void Coordinates::setD1_dx(FieldMetric d1_dx) { d1_dx_ = d1_dx; } -void Coordinates::setD1_dy(FieldMetric d1_dy) { d1_dy_ = d1_dy; } -void Coordinates::setD1_dz(FieldMetric d1_dz) { d1_dz_ = d1_dz; } +void Coordinates::setD1_dx(FieldMetric d1_dx) { d1_dx_ = std::move(d1_dx); } +void Coordinates::setD1_dy(FieldMetric d1_dy) { d1_dy_ = std::move(d1_dy); } +void Coordinates::setD1_dz(FieldMetric d1_dz) { d1_dz_ = std::move(d1_dz); } const FieldMetric& Coordinates::d1_dx() const { return d1_dx_; } const FieldMetric& Coordinates::d1_dy() const { return d1_dy_; } @@ -1386,7 +1386,7 @@ const FieldMetric& Coordinates::ShiftTorsion() const { return ShiftTorsion_; } const FieldMetric& Coordinates::IntShiftTorsion() const { return IntShiftTorsion_; } void Coordinates::setIntShiftTorsion(FieldMetric IntShiftTorsion) { - IntShiftTorsion_ = IntShiftTorsion; + IntShiftTorsion_ = std::move(IntShiftTorsion); } void Coordinates::setContravariantMetricTensor(MetricTensor metric_tensor, @@ -1448,13 +1448,13 @@ const FieldMetric& Coordinates::G1() const { return geometry.G1(); } const FieldMetric& Coordinates::G2() const { return geometry.G2(); } const FieldMetric& Coordinates::G3() const { return geometry.G3(); } -void Coordinates::setG1(FieldMetric G1) { geometry.setG1(G1); } -void Coordinates::setG2(FieldMetric G2) { geometry.setG2(G2); } -void Coordinates::setG3(FieldMetric G3) { geometry.setG3(G3); } +void Coordinates::setG1(FieldMetric G1) { geometry.setG1(std::move(G1)); } +void Coordinates::setG2(FieldMetric G2) { geometry.setG2(std::move(G2)); } +void Coordinates::setG3(FieldMetric G3) { geometry.setG3(std::move(G3)); } void Coordinates::setJ(FieldMetric J) { //TODO: Calculate J and check value is close - geometry.setJ(J); + geometry.setJ(std::move(J)); } void Coordinates::setJ(BoutReal value, int x, int y) { From 331eb1efcb0de970c0d0e040cafaffb145dc260c Mon Sep 17 00:00:00 2001 From: tomc271 Date: Fri, 8 Dec 2023 14:00:17 +0000 Subject: [PATCH 272/491] Rename calculateGeometry() to communicateAndCheckMeshSpacing(). --- include/bout/coordinates.hxx | 2 +- include/bout/mesh.hxx | 2 +- src/mesh/coordinates.cxx | 4 ++-- src/mesh/mesh.cxx | 2 +- tools/pylib/_boutpp_build/boutcpp.pxd.jinja | 2 +- tools/pylib/_boutpp_build/helper.cxx.jinja | 2 +- 6 files changed, 7 insertions(+), 7 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index 0a48b235dd..bb5c29b8dd 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -148,7 +148,7 @@ public: const MetricTensor& getContravariantMetricTensor() const; /// Calculate differential geometry quantities from the metric tensor - int calculateGeometry() const; + int communicateAndCheckMeshSpacing() const; void jacobian(); ///< Calculate J and Bxy diff --git a/include/bout/mesh.hxx b/include/bout/mesh.hxx index fded79f777..f5f6570100 100644 --- a/include/bout/mesh.hxx +++ b/include/bout/mesh.hxx @@ -634,7 +634,7 @@ public: // (circular dependency between Mesh and Coordinates) auto inserted = coords_map.emplace(location, nullptr); inserted.first->second = createDefaultCoordinates(location, false); - inserted.first->second->calculateGeometry(); + inserted.first->second->communicateAndCheckMeshSpacing(); return inserted.first->second; } diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 341c1c8786..e2cbddcf0f 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -630,8 +630,8 @@ const Field2D& Coordinates::zlength() const { return *zlength_cache; } -int Coordinates::calculateGeometry() const { - TRACE("Coordinates::calculateGeometry"); +int Coordinates::communicateAndCheckMeshSpacing() const { + TRACE("Coordinates::communicateAndCheckMeshSpacing"); auto tmp = dx(); // TODO: There must be a better way than this! communicate(tmp, dy(), dz(), g11(), g22(), g33(), g12(), g13(), g23(), g_11(), g_22(), diff --git a/src/mesh/mesh.cxx b/src/mesh/mesh.cxx index 53792c974d..192c87fb47 100644 --- a/src/mesh/mesh.cxx +++ b/src/mesh/mesh.cxx @@ -906,7 +906,7 @@ void Mesh::recalculateStaggeredCoordinates() { } *coords_map[location] = std::move(*createDefaultCoordinates(location, false, true)); - coords_map[location]->calculateGeometry(); + coords_map[location]->communicateAndCheckMeshSpacing(); } } diff --git a/tools/pylib/_boutpp_build/boutcpp.pxd.jinja b/tools/pylib/_boutpp_build/boutcpp.pxd.jinja index a6490e88c7..fe82f05adf 100644 --- a/tools/pylib/_boutpp_build/boutcpp.pxd.jinja +++ b/tools/pylib/_boutpp_build/boutcpp.pxd.jinja @@ -78,7 +78,7 @@ cdef extern from "bout/coordinates.hxx": {{ metric_field }} G1, G2, G3 {{ metric_field }} ShiftTorsion {{ metric_field }} IntShiftTorsion - int calculateGeometry() + int communicateAndCheckMeshSpacing() int CalculateOppositeRepresentation() int CalculateOppositeRepresentation() int jacobian() diff --git a/tools/pylib/_boutpp_build/helper.cxx.jinja b/tools/pylib/_boutpp_build/helper.cxx.jinja index fd89b2f414..d7f13b84ea 100644 --- a/tools/pylib/_boutpp_build/helper.cxx.jinja +++ b/tools/pylib/_boutpp_build/helper.cxx.jinja @@ -173,5 +173,5 @@ void c_mesh_normalise(Mesh * msh, double norm){ coord->setDx(coord->dx() / norm); coord->setDy(coord->dy() / norm); coord->setDz(coord->dz() / norm); - coord->calculateGeometry(); + coord->communicateAndCheckMeshSpacing(); } From 31c76404c7096e7bca71acf12de3162f6430ec6a Mon Sep 17 00:00:00 2001 From: tomc271 Date: Fri, 8 Dec 2023 14:01:01 +0000 Subject: [PATCH 273/491] Remove comments referring to obsolete method Coordinates::calculateGeometry(). --- manual/sphinx/developer_docs/mesh.rst | 2 -- tests/unit/fake_parallel_mesh.hxx | 1 - tests/unit/field/test_field_factory.cxx | 1 - tests/unit/field/test_vector2d.cxx | 1 - tests/unit/field/test_vector3d.cxx | 1 - tests/unit/include/bout/test_petsc_indexer.cxx | 1 - tests/unit/mesh/parallel/test_shiftedmetric.cxx | 1 - tests/unit/mesh/test_coordinates.cxx | 7 +------ tests/unit/mesh/test_interpolation.cxx | 2 -- tests/unit/test_extras.hxx | 3 --- 10 files changed, 1 insertion(+), 19 deletions(-) diff --git a/manual/sphinx/developer_docs/mesh.rst b/manual/sphinx/developer_docs/mesh.rst index b4b7fc14d4..60ce2643c7 100644 --- a/manual/sphinx/developer_docs/mesh.rst +++ b/manual/sphinx/developer_docs/mesh.rst @@ -289,8 +289,6 @@ other useful quantities:: FieldMetric J; // Jacobian FieldMetric Bxy; // Magnitude of B = nabla z times nabla x - /// Calculate differential geometry quantities from the metric tensor - int calculateGeometry(); // Christoffel symbol of the second kind (connection coefficients) FieldMetric G1_11, G1_22, G1_33, G1_12, G1_13; diff --git a/tests/unit/fake_parallel_mesh.hxx b/tests/unit/fake_parallel_mesh.hxx index 97529ac8fb..ecc438ad0f 100644 --- a/tests/unit/fake_parallel_mesh.hxx +++ b/tests/unit/fake_parallel_mesh.hxx @@ -294,7 +294,6 @@ std::vector createFakeProcessors(int nx, int ny, int nz, int n Field2D{0.0}, Field2D{1.0}, Field2D{1.0}, Field2D{1.0}, Field2D{0.0}, Field2D{0.0}, Field2D{0.0}, Field2D{1.0}, Field2D{1.0}, Field2D{1.0}, Field2D{0.0}, Field2D{0.0}, Field2D{0.0}, Field2D{0.0}, Field2D{0.0}); - // No call to Coordinates::calculateGeometry() needed here static_cast(&meshes[j + i * nype])->setCoordinates(test_coords); test_coords->setParallelTransform( bout::utils::make_unique(*bout::globals::mesh)); diff --git a/tests/unit/field/test_field_factory.cxx b/tests/unit/field/test_field_factory.cxx index 4c5365587e..c8688e46a6 100644 --- a/tests/unit/field/test_field_factory.cxx +++ b/tests/unit/field/test_field_factory.cxx @@ -560,7 +560,6 @@ TYPED_TEST(FieldFactoryCreationTest, CreateOnMesh) { Field2D{1.0}, Field2D{1.0}, Field2D{1.0}, Field2D{0.0}, Field2D{0.0}, Field2D{0.0}, Field2D{1.0}, Field2D{1.0}, Field2D{1.0}, Field2D{0.0}, Field2D{0.0}, Field2D{0.0}, Field2D{0.0}, Field2D{0.0})); - // No call to Coordinates::calculateGeometry() needed here localmesh.getCoordinates()->setParallelTransform( bout::utils::make_unique(localmesh)); diff --git a/tests/unit/field/test_vector2d.cxx b/tests/unit/field/test_vector2d.cxx index e1e6360391..a21d8a3999 100644 --- a/tests/unit/field/test_vector2d.cxx +++ b/tests/unit/field/test_vector2d.cxx @@ -52,7 +52,6 @@ class Vector2DTest : public ::testing::Test { Field2D{1.0}, Field2D{2.0}, Field2D{3.0}, Field2D{4.0}, Field2D{5.0}, Field2D{6.0}, Field2D{1.0}, Field2D{2.0}, Field2D{3.0}, Field2D{4.0}, Field2D{5.0}, Field2D{6.0}, Field2D{0.0}, Field2D{0.0})); - // No call to Coordinates::calculateGeometry() needed here delete mesh_staggered; mesh_staggered = new FakeMesh(nx, ny, nz); diff --git a/tests/unit/field/test_vector3d.cxx b/tests/unit/field/test_vector3d.cxx index 1bf5997836..20cca4b362 100644 --- a/tests/unit/field/test_vector3d.cxx +++ b/tests/unit/field/test_vector3d.cxx @@ -49,7 +49,6 @@ class Vector3DTest : public ::testing::Test { Field2D{1.0}, Field2D{2.0}, Field2D{3.0}, Field2D{4.0}, Field2D{5.0}, Field2D{6.0}, Field2D{1.0}, Field2D{2.0}, Field2D{3.0}, Field2D{4.0}, Field2D{5.0}, Field2D{6.0}, Field2D{0.0}, Field2D{0.0})); - // No call to Coordinates::calculateGeometry() needed here delete mesh_staggered; mesh_staggered = new FakeMesh(nx, ny, nz); diff --git a/tests/unit/include/bout/test_petsc_indexer.cxx b/tests/unit/include/bout/test_petsc_indexer.cxx index 977e00fd3b..b10d8c8d62 100644 --- a/tests/unit/include/bout/test_petsc_indexer.cxx +++ b/tests/unit/include/bout/test_petsc_indexer.cxx @@ -51,7 +51,6 @@ class IndexerTest : public FakeMeshFixture { Field2D{0.0}, Field2D{1.0}, Field2D{1.0}, Field2D{1.0}, Field2D{0.0}, Field2D{0.0}, Field2D{0.0}, Field2D{1.0}, Field2D{1.0}, Field2D{1.0}, Field2D{0.0}, Field2D{0.0}, Field2D{0.0}, Field2D{0.0}, Field2D{0.0}); - // No call to Coordinates::calculateGeometry() needed here mesh2.setCoordinates(test_coords); // May need a ParallelTransform to create fields, because create3D calls // fromFieldAligned diff --git a/tests/unit/mesh/parallel/test_shiftedmetric.cxx b/tests/unit/mesh/parallel/test_shiftedmetric.cxx index ebc8ec4b8f..02086c3e4d 100644 --- a/tests/unit/mesh/parallel/test_shiftedmetric.cxx +++ b/tests/unit/mesh/parallel/test_shiftedmetric.cxx @@ -42,7 +42,6 @@ class ShiftedMetricTest : public ::testing::Test { Field2D{1.0}, Field2D{1.0}, Field2D{1.0}, Field2D{0.0}, Field2D{0.0}, Field2D{0.0}, Field2D{1.0}, Field2D{1.0}, Field2D{1.0}, Field2D{0.0}, Field2D{0.0}, Field2D{0.0}, Field2D{0.0}, Field2D{0.0})); - // No call to Coordinates::calculateGeometry() needed here auto coords = mesh->getCoordinates(); coords->setParallelTransform(bout::utils::make_unique( diff --git a/tests/unit/mesh/test_coordinates.cxx b/tests/unit/mesh/test_coordinates.cxx index 9ea8ee1198..24adaaa034 100644 --- a/tests/unit/mesh/test_coordinates.cxx +++ b/tests/unit/mesh/test_coordinates.cxx @@ -46,7 +46,6 @@ TEST_F(CoordinatesTest, ZLength) { FieldMetric{0.0}, // g_23 FieldMetric{0.0}, // ShiftTorsion FieldMetric{0.0}}; // IntShiftTorsion - // No call to Coordinates::calculateGeometry() needed here EXPECT_TRUE(IsFieldEqual(coords.zlength(), 7.0)); } @@ -79,7 +78,6 @@ TEST_F(CoordinatesTest, ZLength3D) { FieldMetric{0.0}, // g_23 FieldMetric{0.0}, // ShiftTorsion FieldMetric{0.0}}; // IntShiftTorsion - // No call to Coordinates::calculateGeometry() needed here EXPECT_TRUE(IsFieldEqual(coords.zlength(), expected)); } @@ -106,7 +104,6 @@ TEST_F(CoordinatesTest, Jacobian) { FieldMetric{0.0}, // g_23 FieldMetric{0.0}, // ShiftTorsion FieldMetric{0.0}}; // IntShiftTorsion - // No call to Coordinates::calculateGeometry() needed here EXPECT_NO_THROW(coords.jacobian()); @@ -137,7 +134,6 @@ TEST_F(CoordinatesTest, CalcContravariant) { FieldMetric{0.0}, // g_23 FieldMetric{0.0}, // ShiftTorsion FieldMetric{0.0}}; // IntShiftTorsion - // No call to Coordinates::calculateGeometry() needed here coords.setContravariantMetricTensor(MetricTensor(1.0, 1.0, 1.0, 0.0, 0.0, 0.0)); @@ -170,7 +166,6 @@ TEST_F(CoordinatesTest, CalcCovariant) { FieldMetric{0.0}, // g_23 FieldMetric{0.0}, // ShiftTorsion FieldMetric{0.0}}; // IntShiftTorsion - // No call to Coordinates::calculateGeometry() needed here coords.setCovariantMetricTensor(MetricTensor(1.0, 1.0, 1.0, 0.0, 0.0, 0.0)); @@ -238,7 +233,7 @@ TEST_F(CoordinatesTest, SmallMeshSpacing) { output_info.disable(); output_warn.disable(); Coordinates coords(mesh); - EXPECT_THROW(coords.calculateGeometry(), BoutException); + EXPECT_THROW(coords.communicateAndCheckMeshSpacing(), BoutException); output_warn.enable(); output_info.enable(); } diff --git a/tests/unit/mesh/test_interpolation.cxx b/tests/unit/mesh/test_interpolation.cxx index 0973dc43c9..b90fc78da9 100644 --- a/tests/unit/mesh/test_interpolation.cxx +++ b/tests/unit/mesh/test_interpolation.cxx @@ -77,7 +77,6 @@ class Field3DInterpToTest : public ::testing::Test { Field2D{0.0, mesh}, Field2D{0.0, mesh}, Field2D{0.0, mesh}, Field2D{0.0, mesh}), location); - // No call to Coordinates::calculateGeometry() needed here mesh->getCoordinates(location)->setParallelTransform( bout::utils::make_unique(*mesh)); } @@ -324,7 +323,6 @@ class Field2DInterpToTest : public ::testing::Test { Field2D{0.0, mesh}, Field2D{0.0, mesh}, Field2D{0.0, mesh}, Field2D{0.0, mesh}), location); - // No call to Coordinates::calculateGeometry() needed here mesh->getCoordinates(location)->setParallelTransform( bout::utils::make_unique(*mesh)); } diff --git a/tests/unit/test_extras.hxx b/tests/unit/test_extras.hxx index 1716d5cf4b..81a8829695 100644 --- a/tests/unit/test_extras.hxx +++ b/tests/unit/test_extras.hxx @@ -440,7 +440,6 @@ public: Field2D{0.0}, Field2D{0.0}, Field2D{0.0}, Field2D{0.0}, Field2D{0.0}); // Set some auxilliary variables - // Usually set in calculateGeometry() // Note: For testing these are set to non-zero values test_coords->setG1(0.1); test_coords->setG2(0.1); @@ -456,7 +455,6 @@ public: test_coords->Bxy().yup() = test_coords->Bxy().ydown() = test_coords->Bxy(); #endif - // No call to Coordinates::calculateGeometry() needed here static_cast(bout::globals::mesh)->setCoordinates(test_coords); static_cast(bout::globals::mesh) ->setGridDataSource(new FakeGridDataSource()); @@ -503,7 +501,6 @@ public: test_coords_staggered->Bxy; #endif - // No call to Coordinates::calculateGeometry() needed here test_coords_staggered->setParallelTransform( bout::utils::make_unique(*mesh_staggered)); From 6fe1813f7c8dee967cbbe00bcdfeafdd72e5e113 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Sat, 9 Dec 2023 09:58:59 +0000 Subject: [PATCH 274/491] Extract class ChristoffelSymbols. --- CMakeLists.txt | 1 + include/bout/christoffel_symbols.hxx | 99 +++++++++ include/bout/geometry.hxx | 18 +- src/mesh/christoffel_symbols.cxx | 306 +++++++++++++++++++++++++++ src/mesh/coordinates.cxx | 2 +- src/mesh/geometry.cxx | 259 +++-------------------- src/mesh/metricTensor.cxx | 7 +- 7 files changed, 454 insertions(+), 238 deletions(-) create mode 100644 include/bout/christoffel_symbols.hxx create mode 100644 src/mesh/christoffel_symbols.cxx diff --git a/CMakeLists.txt b/CMakeLists.txt index 90d6255717..b79fc6b733 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -358,6 +358,7 @@ set(BOUT_SOURCES ./src/mesh/geometry.cxx ./include/bout/differential_operators.hxx ./src/mesh/differential_operators.cxx + ./src/mesh/christoffel_symbols.cxx ${CMAKE_CURRENT_BINARY_DIR}/include/bout/revision.hxx ${CMAKE_CURRENT_BINARY_DIR}/include/bout/version.hxx ) diff --git a/include/bout/christoffel_symbols.hxx b/include/bout/christoffel_symbols.hxx new file mode 100644 index 0000000000..a2e54cd062 --- /dev/null +++ b/include/bout/christoffel_symbols.hxx @@ -0,0 +1,99 @@ + +#ifndef BOUT_CHRISTOFFELSYMBOLS_HXX +#define BOUT_CHRISTOFFELSYMBOLS_HXX + +#include "differential_operators.hxx" +#include "bout/field2d.hxx" +#include "bout/field3d.hxx" +#include "bout/metricTensor.hxx" +#include + +using FieldMetric = MetricTensor::FieldMetric; + +class ChristoffelSymbols { + +public: + ChristoffelSymbols(const FieldMetric& G1_11, const FieldMetric& G1_22, + const FieldMetric& G1_33, const FieldMetric& G1_12, + const FieldMetric& G1_13, const FieldMetric& G1_23, + const FieldMetric& G2_11, const FieldMetric& G2_22, + const FieldMetric& G2_33, const FieldMetric& G2_12, + const FieldMetric& G2_13, const FieldMetric& G2_23, + const FieldMetric& G3_11, const FieldMetric& G3_22, + const FieldMetric& G3_33, const FieldMetric& G3_12, + const FieldMetric& G3_13, const FieldMetric& G3_23, + const FieldMetric& G1, const FieldMetric& G2, const FieldMetric& G3, + DifferentialOperators* differential_operators); + + // ChristoffelSymbols(BoutReal g11, BoutReal g22, BoutReal g33, BoutReal g12, BoutReal g13, + // BoutReal g23, Mesh* mesh); + + ChristoffelSymbols(Mesh* mesh, DifferentialOperators* differential_operators); + + const FieldMetric& G1_11() const; + const FieldMetric& G1_22() const; + const FieldMetric& G1_33() const; + const FieldMetric& G1_12() const; + const FieldMetric& G1_13() const; + const FieldMetric& G1_23() const; + + const FieldMetric& G2_11() const; + const FieldMetric& G2_22() const; + const FieldMetric& G2_33() const; + const FieldMetric& G2_12() const; + const FieldMetric& G2_13() const; + const FieldMetric& G2_23() const; + + const FieldMetric& G3_11() const; + const FieldMetric& G3_22() const; + const FieldMetric& G3_33() const; + const FieldMetric& G3_12() const; + const FieldMetric& G3_13() const; + const FieldMetric& G3_23() const; + + const FieldMetric& G1() const; + const FieldMetric& G2() const; + const FieldMetric& G3() const; + + void setG1(FieldMetric& G1); + void setG2(FieldMetric& G2); + void setG3(FieldMetric& G3); + + void setChristoffelSymbols(const FieldMetric& G1_11, const FieldMetric& G1_22, const FieldMetric& G1_33, + const FieldMetric& G1_12, const FieldMetric& G1_13, const FieldMetric& G1_23, + const FieldMetric& G2_11, const FieldMetric& G2_22, const FieldMetric& G2_33, + const FieldMetric& G2_12, const FieldMetric& G2_13, const FieldMetric& G2_23, + const FieldMetric& G3_11, const FieldMetric& G3_22, const FieldMetric& G3_33, + const FieldMetric& G3_12, const FieldMetric& G3_13, const FieldMetric& G3_23, + const FieldMetric& G1, const FieldMetric& G2, const FieldMetric& G3); + + // void Allocate(); + // + // void setLocation(CELL_LOC location); + // + // // Transforms the ChristoffelSymbols by applying the given function to every component + // void map(std::function function); + // + // ChristoffelSymbols + // applyToComponents(std::function function) const; + + void CalculateChristoffelSymbols(const FieldMetric& dx, const FieldMetric& dy, + const MetricTensor& contravariantMetricTensor, + const MetricTensor& covariantMetricTensor); + + // Transforms the ChristoffelSymbols by applying the given function to every element + void map(std::function function); + +private: + FieldMetric G1_11_, G1_22_, G1_33_, G1_12_, G1_13_, G1_23_; + FieldMetric G2_11_, G2_22_, G2_33_, G2_12_, G2_13_, G2_23_; + FieldMetric G3_11_, G3_22_, G3_33_, G3_12_, G3_13_, G3_23_; + FieldMetric G1_, G2_, G3_; + + DifferentialOperators* differential_operators; + + ChristoffelSymbols + applyToComponents(std::function function) const; +}; + +#endif //BOUT_CHRISTOFFELSYMBOLS_HXX diff --git a/include/bout/geometry.hxx b/include/bout/geometry.hxx index 39d0d2b731..cde6c11c3e 100644 --- a/include/bout/geometry.hxx +++ b/include/bout/geometry.hxx @@ -33,6 +33,7 @@ #ifndef __GEOMETRY_H__ #define __GEOMETRY_H__ +#include "christoffel_symbols.hxx" #include "differential_operators.hxx" #include "metricTensor.hxx" @@ -51,7 +52,7 @@ public: const FieldMetric& g13, const FieldMetric& g23, const FieldMetric& g_11, const FieldMetric& g_22, const FieldMetric& g_33, const FieldMetric& g_12, const FieldMetric& g_13, const FieldMetric& g_23, - DifferentialOperators* differential_operators); + DifferentialOperators* differential_operators, Mesh* mesh); Geometry(Mesh* mesh, DifferentialOperators* differential_operators); @@ -74,6 +75,7 @@ public: const MetricTensor& getContravariantMetricTensor() const; const MetricTensor& getCovariantMetricTensor() const; + /// Christoffel symbol of the second kind (connection coefficients) const FieldMetric& G1_11() const; const FieldMetric& G1_22() const; const FieldMetric& G1_33() const; @@ -99,6 +101,10 @@ public: const FieldMetric& G2() const; const FieldMetric& G3() const; + void setG1(FieldMetric G1); + void setG2(FieldMetric G2); + void setG3(FieldMetric G3); + ///< Coordinate system Jacobian, so volume of cell is J*dx*dy*dz const FieldMetric& J() const; @@ -111,10 +117,6 @@ public: void setCovariantMetricTensor(MetricTensor metric_tensor, const std::string& region = "RGN_ALL"); - void setG3(FieldMetric G3); - void setG1(FieldMetric G1); - void setG2(FieldMetric G2); - void setJ(FieldMetric J); void setJ(BoutReal value, int x, int y); @@ -143,11 +145,7 @@ public: private: /// Christoffel symbol of the second kind (connection coefficients) - FieldMetric G1_11_, G1_22_, G1_33_, G1_12_, G1_13_, G1_23_; - FieldMetric G2_11_, G2_22_, G2_33_, G2_12_, G2_13_, G2_23_; - FieldMetric G3_11_, G3_22_, G3_33_, G3_12_, G3_13_, G3_23_; - - FieldMetric G1_, G2_, G3_; + ChristoffelSymbols christoffel_symbols; MetricTensor contravariantMetricTensor; MetricTensor covariantMetricTensor; diff --git a/src/mesh/christoffel_symbols.cxx b/src/mesh/christoffel_symbols.cxx new file mode 100644 index 0000000000..d960678118 --- /dev/null +++ b/src/mesh/christoffel_symbols.cxx @@ -0,0 +1,306 @@ + +#include "bout/christoffel_symbols.hxx" + +ChristoffelSymbols::ChristoffelSymbols( + const FieldMetric& G1_11, const FieldMetric& G1_22, const FieldMetric& G1_33, + const FieldMetric& G1_12, const FieldMetric& G1_13, const FieldMetric& G1_23, + const FieldMetric& G2_11, const FieldMetric& G2_22, const FieldMetric& G2_33, + const FieldMetric& G2_12, const FieldMetric& G2_13, const FieldMetric& G2_23, + const FieldMetric& G3_11, const FieldMetric& G3_22, const FieldMetric& G3_33, + const FieldMetric& G3_12, const FieldMetric& G3_13, const FieldMetric& G3_23, + const FieldMetric& G1, const FieldMetric& G2, const FieldMetric& G3, + DifferentialOperators* differential_operators) + : G1_11_(G1_11), G1_22_(G1_22), G1_33_(G1_33), G1_12_(G1_12), G1_13_(G1_13), + G1_23_(G1_23), G2_11_(G2_11), G2_22_(G2_22), G2_33_(G2_33), G2_12_(G2_12), + G2_13_(G2_13), G2_23_(G2_23), G3_11_(G3_11), G3_22_(G3_22), G3_33_(G3_33), + G3_12_(G3_12), G3_13_(G3_13), G3_23_(G3_23), G1_(G1), G2_(G2), G3_(G3), + differential_operators(differential_operators){ + ASSERT0(differential_operators != nullptr)} + + ChristoffelSymbols::ChristoffelSymbols(Mesh * mesh, DifferentialOperators + * differential_operators) + : G1_11_(mesh), G1_22_(mesh), G1_33_(mesh), G1_12_(mesh), G1_13_(mesh), G1_23_(mesh), + G2_11_(mesh), G2_22_(mesh), G2_33_(mesh), G2_12_(mesh), G2_13_(mesh), G2_23_(mesh), + G3_11_(mesh), G3_22_(mesh), G3_33_(mesh), G3_12_(mesh), G3_13_(mesh), G3_23_(mesh), + G1_(mesh), G2_(mesh), G3_(mesh), differential_operators(differential_operators) {} + +const FieldMetric& ChristoffelSymbols::G1_11() const { return G1_11_; } +const FieldMetric& ChristoffelSymbols::G1_22() const { return G1_22_; } +const FieldMetric& ChristoffelSymbols::G1_33() const { return G1_33_; } +const FieldMetric& ChristoffelSymbols::G1_12() const { return G1_12_; } +const FieldMetric& ChristoffelSymbols::G1_13() const { return G1_13_; } +const FieldMetric& ChristoffelSymbols::G1_23() const { return G1_23_; } + +const FieldMetric& ChristoffelSymbols::G2_11() const { return G2_11_; } +const FieldMetric& ChristoffelSymbols::G2_22() const { return G2_22_; } +const FieldMetric& ChristoffelSymbols::G2_33() const { return G2_33_; } +const FieldMetric& ChristoffelSymbols::G2_12() const { return G2_12_; } +const FieldMetric& ChristoffelSymbols::G2_13() const { return G2_13_; } +const FieldMetric& ChristoffelSymbols::G2_23() const { return G2_23_; } + +const FieldMetric& ChristoffelSymbols::G3_11() const { return G3_11_; } +const FieldMetric& ChristoffelSymbols::G3_22() const { return G3_22_; } +const FieldMetric& ChristoffelSymbols::G3_33() const { return G3_33_; } +const FieldMetric& ChristoffelSymbols::G3_12() const { return G3_12_; } +const FieldMetric& ChristoffelSymbols::G3_13() const { return G3_13_; } +const FieldMetric& ChristoffelSymbols::G3_23() const { return G3_23_; } + +const FieldMetric& ChristoffelSymbols::G1() const { return G1_; } +const FieldMetric& ChristoffelSymbols::G2() const { return G2_; } +const FieldMetric& ChristoffelSymbols::G3() const { return G3_; } + +void ChristoffelSymbols::setG1(FieldMetric& G1) { G1_ = G1; } +void ChristoffelSymbols::setG2(FieldMetric& G2) { G2_ = G2; } +void ChristoffelSymbols::setG3(FieldMetric& G3) { G3_ = G3; } + +void ChristoffelSymbols::setChristoffelSymbols( + const FieldMetric& G1_11, const FieldMetric& G1_22, const FieldMetric& G1_33, + const FieldMetric& G1_12, const FieldMetric& G1_13, const FieldMetric& G1_23, + const FieldMetric& G2_11, const FieldMetric& G2_22, const FieldMetric& G2_33, + const FieldMetric& G2_12, const FieldMetric& G2_13, const FieldMetric& G2_23, + const FieldMetric& G3_11, const FieldMetric& G3_22, const FieldMetric& G3_33, + const FieldMetric& G3_12, const FieldMetric& G3_13, const FieldMetric& G3_23, + const FieldMetric& G1, const FieldMetric& G2, const FieldMetric& G3) { + G1_11_ = G1_11; + G1_22_ = G1_22; + G1_33_ = G1_33; + G1_12_ = G1_12; + G1_13_ = G1_13; + G1_23_ = G1_23; + G2_11_ = G2_11; + G2_22_ = G2_22; + G2_33_ = G2_33; + G2_12_ = G2_12; + G2_13_ = G2_13; + G2_23_ = G2_23; + G3_11_ = G3_11; + G3_22_ = G3_22; + G3_33_ = G3_33; + G3_12_ = G3_12; + G3_13_ = G3_13; + G3_23_ = G3_23; + G1_ = G1; + G2_ = G2; + G3_ = G3; +} + +void ChristoffelSymbols::CalculateChristoffelSymbols( + const FieldMetric& dx, const FieldMetric& dy, + const MetricTensor& contravariantMetricTensor, + const MetricTensor& covariantMetricTensor) { + // Calculate Christoffel symbol terms (18 independent values) + // Note: This calculation is completely general: metric + // tensor can be 2D or 3D. For 2D, all DDZ terms are zero + + G1_11_ = + 0.5 * contravariantMetricTensor.Getg11() + * differential_operators->DDX(covariantMetricTensor.Getg11(), dx) + + contravariantMetricTensor.Getg12() + * (differential_operators->DDX(covariantMetricTensor.Getg12(), dx) + - 0.5 * differential_operators->DDY(covariantMetricTensor.Getg11(), dy)) + + contravariantMetricTensor.Getg13() + * (differential_operators->DDX(covariantMetricTensor.Getg13(), dx) + - 0.5 * differential_operators->DDZ(covariantMetricTensor.Getg11())); + G1_22_ = contravariantMetricTensor.Getg11() + * (differential_operators->DDY(covariantMetricTensor.Getg12(), dy) + - 0.5 * differential_operators->DDX(covariantMetricTensor.Getg22(), dx)) + + 0.5 * contravariantMetricTensor.Getg12() + * differential_operators->DDY(covariantMetricTensor.Getg22(), dy) + + contravariantMetricTensor.Getg13() + * (differential_operators->DDY(covariantMetricTensor.Getg23(), dy) + - 0.5 * differential_operators->DDZ(covariantMetricTensor.Getg22())); + G1_33_ = + contravariantMetricTensor.Getg11() + * (differential_operators->DDZ(covariantMetricTensor.Getg13()) + - 0.5 * differential_operators->DDX(covariantMetricTensor.Getg33(), dx)) + + contravariantMetricTensor.Getg12() + * (differential_operators->DDZ(covariantMetricTensor.Getg23()) + - 0.5 * differential_operators->DDY(covariantMetricTensor.Getg33(), dy)) + + 0.5 * contravariantMetricTensor.Getg13() + * differential_operators->DDZ(covariantMetricTensor.Getg33()); + G1_12_ = 0.5 * contravariantMetricTensor.Getg11() + * differential_operators->DDY(covariantMetricTensor.Getg11(), dy) + + 0.5 * contravariantMetricTensor.Getg12() + * differential_operators->DDX(covariantMetricTensor.Getg22(), dx) + + 0.5 * contravariantMetricTensor.Getg13() + * (differential_operators->DDY(covariantMetricTensor.Getg13(), dy) + + differential_operators->DDX(covariantMetricTensor.Getg23(), dx) + - differential_operators->DDZ(covariantMetricTensor.Getg12())); + G1_13_ = 0.5 * contravariantMetricTensor.Getg11() + * differential_operators->DDZ(covariantMetricTensor.Getg11()) + + 0.5 * contravariantMetricTensor.Getg12() + * (differential_operators->DDZ(covariantMetricTensor.Getg12()) + + differential_operators->DDX(covariantMetricTensor.Getg23(), dx) + - differential_operators->DDY(covariantMetricTensor.Getg13(), dy)) + + 0.5 * contravariantMetricTensor.Getg13() + * differential_operators->DDX(covariantMetricTensor.Getg33(), dx); + G1_23_ = + 0.5 * contravariantMetricTensor.Getg11() + * (differential_operators->DDZ(covariantMetricTensor.Getg12()) + + differential_operators->DDY(covariantMetricTensor.Getg13(), dy) + - differential_operators->DDX(covariantMetricTensor.Getg23(), dx)) + + 0.5 * contravariantMetricTensor.Getg12() + * (differential_operators->DDZ(covariantMetricTensor.Getg22()) + + differential_operators->DDY(covariantMetricTensor.Getg23(), dy) + - differential_operators->DDY(covariantMetricTensor.Getg23(), dy)) + // + 0.5 *g13*(differential_operators->DDZ(g_32) + differential_operators->DDY(g_33) - differential_operators->DDZ(g_23)); + // which equals + + 0.5 * contravariantMetricTensor.Getg13() + * differential_operators->DDY(covariantMetricTensor.Getg33(), dy); + + G2_11_ = + 0.5 * contravariantMetricTensor.Getg12() + * differential_operators->DDX(covariantMetricTensor.Getg11(), dx) + + contravariantMetricTensor.Getg22() + * (differential_operators->DDX(covariantMetricTensor.Getg12(), dx) + - 0.5 * differential_operators->DDY(covariantMetricTensor.Getg11(), dy)) + + contravariantMetricTensor.Getg23() + * (differential_operators->DDX(covariantMetricTensor.Getg13(), dx) + - 0.5 * differential_operators->DDZ(covariantMetricTensor.Getg11())); + G2_22_ = contravariantMetricTensor.Getg12() + * (differential_operators->DDY(covariantMetricTensor.Getg12(), dy) + - 0.5 * differential_operators->DDX(covariantMetricTensor.Getg22(), dx)) + + 0.5 * contravariantMetricTensor.Getg22() + * differential_operators->DDY(covariantMetricTensor.Getg22(), dy) + + contravariantMetricTensor.Getg23() + * (differential_operators->DDY(contravariantMetricTensor.Getg23(), dy) + - 0.5 * differential_operators->DDZ(covariantMetricTensor.Getg22())); + G2_33_ = + contravariantMetricTensor.Getg12() + * (differential_operators->DDZ(covariantMetricTensor.Getg13()) + - 0.5 * differential_operators->DDX(covariantMetricTensor.Getg33(), dx)) + + contravariantMetricTensor.Getg22() + * (differential_operators->DDZ(covariantMetricTensor.Getg23()) + - 0.5 * differential_operators->DDY(covariantMetricTensor.Getg33(), dy)) + + 0.5 * contravariantMetricTensor.Getg23() + * differential_operators->DDZ(covariantMetricTensor.Getg33()); + G2_12_ = 0.5 * contravariantMetricTensor.Getg12() + * differential_operators->DDY(covariantMetricTensor.Getg11(), dy) + + 0.5 * contravariantMetricTensor.Getg22() + * differential_operators->DDX(covariantMetricTensor.Getg22(), dx) + + 0.5 * contravariantMetricTensor.Getg23() + * (differential_operators->DDY(covariantMetricTensor.Getg13(), dy) + + differential_operators->DDX(covariantMetricTensor.Getg23(), dx) + - differential_operators->DDZ(covariantMetricTensor.Getg12())); + G2_13_ = + // 0.5 *g21*(differential_operators->DDZ(covariantMetricTensor.Getg11()) + differential_operators->DDX(covariantMetricTensor.Getg13()) - differential_operators->DDX(covariantMetricTensor.Getg13())) + // which equals + 0.5 * contravariantMetricTensor.Getg12() + * (differential_operators->DDZ(covariantMetricTensor.Getg11()) + + differential_operators->DDX(covariantMetricTensor.Getg13(), dx) + - differential_operators->DDX(covariantMetricTensor.Getg13(), dx)) + // + 0.5 *g22*(differential_operators->DDZ(covariantMetricTensor.Getg21()) + differential_operators->DDX(covariantMetricTensor.Getg23()) - differential_operators->DDY(covariantMetricTensor.Getg13())) + // which equals + + 0.5 * contravariantMetricTensor.Getg22() + * (differential_operators->DDZ(covariantMetricTensor.Getg12()) + + differential_operators->DDX(covariantMetricTensor.Getg23(), dx) + - differential_operators->DDY(covariantMetricTensor.Getg13(), dy)) + // + 0.5 *g23*(differential_operators->DDZ(covariantMetricTensor.Getg31()) + differential_operators->DDX(covariantMetricTensor.Getg33()) - differential_operators->DDZ(g_13)); + // which equals + + 0.5 * contravariantMetricTensor.Getg23() + * differential_operators->DDX(covariantMetricTensor.Getg33(), dx); + G2_23_ = 0.5 * contravariantMetricTensor.Getg12() + * (differential_operators->DDZ(covariantMetricTensor.Getg12()) + + differential_operators->DDY(covariantMetricTensor.Getg13(), dy) + - differential_operators->DDX(covariantMetricTensor.Getg23(), dx)) + + 0.5 * contravariantMetricTensor.Getg22() + * differential_operators->DDZ(covariantMetricTensor.Getg22()) + + 0.5 * contravariantMetricTensor.Getg23() + * differential_operators->DDY(covariantMetricTensor.Getg33(), dy); + + G3_11_ = + 0.5 * contravariantMetricTensor.Getg13() + * differential_operators->DDX(covariantMetricTensor.Getg11(), dx) + + contravariantMetricTensor.Getg23() + * (differential_operators->DDX(covariantMetricTensor.Getg12(), dx) + - 0.5 * differential_operators->DDY(covariantMetricTensor.Getg11(), dy)) + + contravariantMetricTensor.Getg33() + * (differential_operators->DDX(covariantMetricTensor.Getg13(), dx) + - 0.5 * differential_operators->DDZ(covariantMetricTensor.Getg11())); + G3_22_ = contravariantMetricTensor.Getg13() + * (differential_operators->DDY(covariantMetricTensor.Getg12(), dy) + - 0.5 * differential_operators->DDX(covariantMetricTensor.Getg22(), dx)) + + 0.5 * contravariantMetricTensor.Getg23() + * differential_operators->DDY(covariantMetricTensor.Getg22(), dy) + + contravariantMetricTensor.Getg33() + * (differential_operators->DDY(covariantMetricTensor.Getg23(), dy) + - 0.5 * differential_operators->DDZ(covariantMetricTensor.Getg22())); + G3_33_ = + contravariantMetricTensor.Getg13() + * (differential_operators->DDZ(covariantMetricTensor.Getg13()) + - 0.5 * differential_operators->DDX(covariantMetricTensor.Getg33(), dx)) + + contravariantMetricTensor.Getg23() + * (differential_operators->DDZ(covariantMetricTensor.Getg23()) + - 0.5 * differential_operators->DDY(covariantMetricTensor.Getg33(), dy)) + + 0.5 * contravariantMetricTensor.Getg33() + * differential_operators->DDZ(covariantMetricTensor.Getg33()); + G3_12_ = + // 0.5 *g31*(differential_operators->DDY(covariantMetricTensor.Getg11()) + differential_operators->DDX(covariantMetricTensor.Getg12()) - differential_operators->DDX(covariantMetricTensor.Getg12())) + // which equals to + 0.5 * contravariantMetricTensor.Getg13() + * differential_operators->DDY(covariantMetricTensor.Getg11(), dy) + // + 0.5 *g32*(differential_operators->DDY(covariantMetricTensor.Getg21()) + differential_operators->DDX(covariantMetricTensor.Getg22()) - differential_operators->DDY(covariantMetricTensor.Getg12())) + // which equals to + + 0.5 * contravariantMetricTensor.Getg23() + * differential_operators->DDX(covariantMetricTensor.Getg22(), dx) + //+ 0.5 *g33*(differential_operators->DDY(covariantMetricTensor.Getg31()) + differential_operators->DDX(covariantMetricTensor.Getg32()) - differential_operators->DDZ(covariantMetricTensor.Getg12())); + // which equals to + + 0.5 * contravariantMetricTensor.Getg33() + * (differential_operators->DDY(covariantMetricTensor.Getg13(), dy)) + + differential_operators->DDX(covariantMetricTensor.Getg23(), dx) + - differential_operators->DDZ(covariantMetricTensor.Getg12()); + G3_13_ = 0.5 * contravariantMetricTensor.Getg13() + * differential_operators->DDZ(covariantMetricTensor.Getg11()) + + 0.5 * contravariantMetricTensor.Getg23() + * (differential_operators->DDZ(covariantMetricTensor.Getg12()) + + differential_operators->DDX(covariantMetricTensor.Getg23(), dx) + - differential_operators->DDY(covariantMetricTensor.Getg13(), dy)) + + 0.5 * contravariantMetricTensor.Getg33() + * differential_operators->DDX(covariantMetricTensor.Getg33(), dx); + G3_23_ = 0.5 * contravariantMetricTensor.Getg13() + * (differential_operators->DDZ(covariantMetricTensor.Getg12()) + + differential_operators->DDY(covariantMetricTensor.Getg13(), dy)) + - differential_operators->DDX(covariantMetricTensor.Getg23(), dx) + + 0.5 * contravariantMetricTensor.Getg23() + * differential_operators->DDZ(covariantMetricTensor.Getg22()) + + 0.5 * contravariantMetricTensor.Getg33() + * differential_operators->DDY(covariantMetricTensor.Getg33(), dy); +} + +void ChristoffelSymbols::map( + const std::function function) { + + const ChristoffelSymbols updated_christoffel_symbols = applyToComponents(function); + + setChristoffelSymbols( + updated_christoffel_symbols.G1_11_, updated_christoffel_symbols.G1_22_, + updated_christoffel_symbols.G1_33_, updated_christoffel_symbols.G1_12_, + updated_christoffel_symbols.G1_13_, updated_christoffel_symbols.G1_23_, + updated_christoffel_symbols.G2_11_, updated_christoffel_symbols.G2_22_, + updated_christoffel_symbols.G2_33_, updated_christoffel_symbols.G2_12_, + updated_christoffel_symbols.G2_13_, updated_christoffel_symbols.G2_23_, + updated_christoffel_symbols.G3_11_, updated_christoffel_symbols.G3_22_, + updated_christoffel_symbols.G3_33_, updated_christoffel_symbols.G3_12_, + updated_christoffel_symbols.G3_13_, updated_christoffel_symbols.G3_23_, + updated_christoffel_symbols.G1_, updated_christoffel_symbols.G2_, + updated_christoffel_symbols.G3_); +} + +ChristoffelSymbols ChristoffelSymbols::applyToComponents( + const std::function function) const { + + const auto components_in = + std::vector{G1_11_, G1_22_, G1_33_, G1_12_, G1_13_, G1_23_, G2_11_, + G2_22_, G2_33_, G2_12_, G2_13_, G2_23_, G3_11_, G3_22_, + G3_33_, G3_12_, G3_13_, G3_23_, G1_, G2_, G3_}; + + FieldMetric components_out[21]; + + std::transform(components_in.begin(), components_in.end(), components_out, function); + auto [G1_11, G1_22, G1_33, G1_12, G1_13, G1_23, G2_11, G2_22, G2_33, G2_12, G2_13, + G2_23, G3_11, G3_22, G3_33, G3_12, G3_13, G3_23, G1, G2, G3] = components_out; + + return ChristoffelSymbols(G1_11, G1_22, G1_33, G1_12, G1_13, G1_23, G2_11, G2_22, G2_33, + G2_12, G2_13, G2_23, G3_11, G3_22, G3_33, G3_12, G3_13, G3_23, + G1, G2, G3, differential_operators); +} diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index e2cbddcf0f..f6299eedb7 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -292,7 +292,7 @@ Coordinates::Coordinates(Mesh* mesh, FieldMetric dx, FieldMetric dy, FieldMetric : nz(mesh->LocalNz), localmesh(mesh), location(CELL_CENTRE), differential_operators(mesh->getDifferentialOperators()), geometry(Geometry(J, Bxy, g11, g22, g33, g12, g13, g23, g_11, g_22, g_33, g_12, - g_13, g_23, differential_operators)), + g_13, g_23, differential_operators, mesh)), dx_(std::move(dx)), dy_(std::move(dy)), dz_(std::move(dz)), ShiftTorsion_(std::move(ShiftTorsion)), IntShiftTorsion_(std::move(IntShiftTorsion)) {} diff --git a/src/mesh/geometry.cxx b/src/mesh/geometry.cxx index 8c2b93dc25..1a78af5491 100644 --- a/src/mesh/geometry.cxx +++ b/src/mesh/geometry.cxx @@ -12,8 +12,9 @@ Geometry::Geometry(const FieldMetric& J, const FieldMetric& Bxy, const FieldMetr const FieldMetric& g_11, const FieldMetric& g_22, const FieldMetric& g_33, const FieldMetric& g_12, const FieldMetric& g_13, const FieldMetric& g_23, - DifferentialOperators* differential_operators) - : contravariantMetricTensor(g11, g22, g33, g12, g13, g23), + DifferentialOperators* differential_operators, Mesh* mesh) + : christoffel_symbols(mesh, differential_operators), + contravariantMetricTensor(g11, g22, g33, g12, g13, g23), covariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23), this_J(J), this_Bxy(Bxy), differential_operators(differential_operators) { ASSERT0(differential_operators != nullptr); @@ -21,196 +22,17 @@ Geometry::Geometry(const FieldMetric& J, const FieldMetric& Bxy, const FieldMetr Geometry::Geometry(Mesh* mesh, DifferentialOperators* differential_operators) //bool force_interpolate_from_centre - : G1_11_(mesh), G1_22_(mesh), G1_33_(mesh), G1_12_(mesh), G1_13_(mesh), G1_23_(mesh), - G2_11_(mesh), G2_22_(mesh), G2_33_(mesh), G2_12_(mesh), G2_13_(mesh), G2_23_(mesh), - G3_11_(mesh), G3_22_(mesh), G3_33_(mesh), G3_12_(mesh), G3_13_(mesh), G3_23_(mesh), - G1_(mesh), G2_(mesh), G3_(mesh), + : christoffel_symbols(mesh, differential_operators), contravariantMetricTensor(1., 1., 1., 0, 0, 0, mesh), - covariantMetricTensor(1., 1., 1., 0, 0, 0, mesh), // Identity metric tensor - this_J(1., mesh), this_Bxy(1., mesh), - differential_operators(differential_operators) { + covariantMetricTensor(1., 1., 1., 0, 0, 0, mesh), this_J(1., mesh), + this_Bxy(1., mesh), differential_operators(differential_operators) { ASSERT0(differential_operators != nullptr); } void Geometry::CalculateChristoffelSymbols(const FieldMetric& dx, const FieldMetric& dy) { - // Calculate Christoffel symbol terms (18 independent values) - // Note: This calculation is completely general: metric - // tensor can be 2D or 3D. For 2D, all DDZ terms are zero - - G1_11_ = - 0.5 * contravariantMetricTensor.Getg11() - * differential_operators->DDX(covariantMetricTensor.Getg11(), dx) - + contravariantMetricTensor.Getg12() - * (differential_operators->DDX(covariantMetricTensor.Getg12(), dx) - - 0.5 * differential_operators->DDY(covariantMetricTensor.Getg11(), dy)) - + contravariantMetricTensor.Getg13() - * (differential_operators->DDX(covariantMetricTensor.Getg13(), dx) - - 0.5 * differential_operators->DDZ(covariantMetricTensor.Getg11())); - G1_22_ = contravariantMetricTensor.Getg11() - * (differential_operators->DDY(covariantMetricTensor.Getg12(), dy) - - 0.5 * differential_operators->DDX(covariantMetricTensor.Getg22(), dx)) - + 0.5 * contravariantMetricTensor.Getg12() - * differential_operators->DDY(covariantMetricTensor.Getg22(), dy) - + contravariantMetricTensor.Getg13() - * (differential_operators->DDY(covariantMetricTensor.Getg23(), dy) - - 0.5 * differential_operators->DDZ(covariantMetricTensor.Getg22())); - G1_33_ = - contravariantMetricTensor.Getg11() - * (differential_operators->DDZ(covariantMetricTensor.Getg13()) - - 0.5 * differential_operators->DDX(covariantMetricTensor.Getg33(), dx)) - + contravariantMetricTensor.Getg12() - * (differential_operators->DDZ(covariantMetricTensor.Getg23()) - - 0.5 * differential_operators->DDY(covariantMetricTensor.Getg33(), dy)) - + 0.5 * contravariantMetricTensor.Getg13() - * differential_operators->DDZ(covariantMetricTensor.Getg33()); - G1_12_ = 0.5 * contravariantMetricTensor.Getg11() - * differential_operators->DDY(covariantMetricTensor.Getg11(), dy) - + 0.5 * contravariantMetricTensor.Getg12() - * differential_operators->DDX(covariantMetricTensor.Getg22(), dx) - + 0.5 * contravariantMetricTensor.Getg13() - * (differential_operators->DDY(covariantMetricTensor.Getg13(), dy) - + differential_operators->DDX(covariantMetricTensor.Getg23(), dx) - - differential_operators->DDZ(covariantMetricTensor.Getg12())); - G1_13_ = 0.5 * contravariantMetricTensor.Getg11() - * differential_operators->DDZ(covariantMetricTensor.Getg11()) - + 0.5 * contravariantMetricTensor.Getg12() - * (differential_operators->DDZ(covariantMetricTensor.Getg12()) - + differential_operators->DDX(covariantMetricTensor.Getg23(), dx) - - differential_operators->DDY(covariantMetricTensor.Getg13(), dy)) - + 0.5 * contravariantMetricTensor.Getg13() - * differential_operators->DDX(covariantMetricTensor.Getg33(), dx); - G1_23_ = - 0.5 * contravariantMetricTensor.Getg11() - * (differential_operators->DDZ(covariantMetricTensor.Getg12()) - + differential_operators->DDY(covariantMetricTensor.Getg13(), dy) - - differential_operators->DDX(covariantMetricTensor.Getg23(), dx)) - + 0.5 * contravariantMetricTensor.Getg12() - * (differential_operators->DDZ(covariantMetricTensor.Getg22()) - + differential_operators->DDY(covariantMetricTensor.Getg23(), dy) - - differential_operators->DDY(covariantMetricTensor.Getg23(), dy)) - // + 0.5 *g13*(differential_operators->DDZ(g_32) + differential_operators->DDY(g_33) - differential_operators->DDZ(g_23)); - // which equals - + 0.5 * contravariantMetricTensor.Getg13() - * differential_operators->DDY(covariantMetricTensor.Getg33(), dy); - - G2_11_ = - 0.5 * contravariantMetricTensor.Getg12() - * differential_operators->DDX(covariantMetricTensor.Getg11(), dx) - + contravariantMetricTensor.Getg22() - * (differential_operators->DDX(covariantMetricTensor.Getg12(), dx) - - 0.5 * differential_operators->DDY(covariantMetricTensor.Getg11(), dy)) - + contravariantMetricTensor.Getg23() - * (differential_operators->DDX(covariantMetricTensor.Getg13(), dx) - - 0.5 * differential_operators->DDZ(covariantMetricTensor.Getg11())); - G2_22_ = contravariantMetricTensor.Getg12() - * (differential_operators->DDY(covariantMetricTensor.Getg12(), dy) - - 0.5 * differential_operators->DDX(covariantMetricTensor.Getg22(), dx)) - + 0.5 * contravariantMetricTensor.Getg22() - * differential_operators->DDY(covariantMetricTensor.Getg22(), dy) - + contravariantMetricTensor.Getg23() - * (differential_operators->DDY(contravariantMetricTensor.Getg23(), dy) - - 0.5 * differential_operators->DDZ(covariantMetricTensor.Getg22())); - G2_33_ = - contravariantMetricTensor.Getg12() - * (differential_operators->DDZ(covariantMetricTensor.Getg13()) - - 0.5 * differential_operators->DDX(covariantMetricTensor.Getg33(), dx)) - + contravariantMetricTensor.Getg22() - * (differential_operators->DDZ(covariantMetricTensor.Getg23()) - - 0.5 * differential_operators->DDY(covariantMetricTensor.Getg33(), dy)) - + 0.5 * contravariantMetricTensor.Getg23() - * differential_operators->DDZ(covariantMetricTensor.Getg33()); - G2_12_ = 0.5 * contravariantMetricTensor.Getg12() - * differential_operators->DDY(covariantMetricTensor.Getg11(), dy) - + 0.5 * contravariantMetricTensor.Getg22() - * differential_operators->DDX(covariantMetricTensor.Getg22(), dx) - + 0.5 * contravariantMetricTensor.Getg23() - * (differential_operators->DDY(covariantMetricTensor.Getg13(), dy) - + differential_operators->DDX(covariantMetricTensor.Getg23(), dx) - - differential_operators->DDZ(covariantMetricTensor.Getg12())); - G2_13_ = - // 0.5 *g21*(differential_operators->DDZ(covariantMetricTensor.Getg11()) + differential_operators->DDX(covariantMetricTensor.Getg13()) - differential_operators->DDX(covariantMetricTensor.Getg13())) - // which equals - 0.5 * contravariantMetricTensor.Getg12() - * (differential_operators->DDZ(covariantMetricTensor.Getg11()) - + differential_operators->DDX(covariantMetricTensor.Getg13(), dx) - - differential_operators->DDX(covariantMetricTensor.Getg13(), dx)) - // + 0.5 *g22*(differential_operators->DDZ(covariantMetricTensor.Getg21()) + differential_operators->DDX(covariantMetricTensor.Getg23()) - differential_operators->DDY(covariantMetricTensor.Getg13())) - // which equals - + 0.5 * contravariantMetricTensor.Getg22() - * (differential_operators->DDZ(covariantMetricTensor.Getg12()) - + differential_operators->DDX(covariantMetricTensor.Getg23(), dx) - - differential_operators->DDY(covariantMetricTensor.Getg13(), dy)) - // + 0.5 *g23*(differential_operators->DDZ(covariantMetricTensor.Getg31()) + differential_operators->DDX(covariantMetricTensor.Getg33()) - differential_operators->DDZ(g_13)); - // which equals - + 0.5 * contravariantMetricTensor.Getg23() - * differential_operators->DDX(covariantMetricTensor.Getg33(), dx); - G2_23_ = 0.5 * contravariantMetricTensor.Getg12() - * (differential_operators->DDZ(covariantMetricTensor.Getg12()) - + differential_operators->DDY(covariantMetricTensor.Getg13(), dy) - - differential_operators->DDX(covariantMetricTensor.Getg23(), dx)) - + 0.5 * contravariantMetricTensor.Getg22() - * differential_operators->DDZ(covariantMetricTensor.Getg22()) - + 0.5 * contravariantMetricTensor.Getg23() - * differential_operators->DDY(covariantMetricTensor.Getg33(), dy); - - G3_11_ = - 0.5 * contravariantMetricTensor.Getg13() - * differential_operators->DDX(covariantMetricTensor.Getg11(), dx) - + contravariantMetricTensor.Getg23() - * (differential_operators->DDX(covariantMetricTensor.Getg12(), dx) - - 0.5 * differential_operators->DDY(covariantMetricTensor.Getg11(), dy)) - + contravariantMetricTensor.Getg33() - * (differential_operators->DDX(covariantMetricTensor.Getg13(), dx) - - 0.5 * differential_operators->DDZ(covariantMetricTensor.Getg11())); - G3_22_ = contravariantMetricTensor.Getg13() - * (differential_operators->DDY(covariantMetricTensor.Getg12(), dy) - - 0.5 * differential_operators->DDX(covariantMetricTensor.Getg22(), dx)) - + 0.5 * contravariantMetricTensor.Getg23() - * differential_operators->DDY(covariantMetricTensor.Getg22(), dy) - + contravariantMetricTensor.Getg33() - * (differential_operators->DDY(covariantMetricTensor.Getg23(), dy) - - 0.5 * differential_operators->DDZ(covariantMetricTensor.Getg22())); - G3_33_ = - contravariantMetricTensor.Getg13() - * (differential_operators->DDZ(covariantMetricTensor.Getg13()) - - 0.5 * differential_operators->DDX(covariantMetricTensor.Getg33(), dx)) - + contravariantMetricTensor.Getg23() - * (differential_operators->DDZ(covariantMetricTensor.Getg23()) - - 0.5 * differential_operators->DDY(covariantMetricTensor.Getg33(), dy)) - + 0.5 * contravariantMetricTensor.Getg33() - * differential_operators->DDZ(covariantMetricTensor.Getg33()); - G3_12_ = - // 0.5 *g31*(differential_operators->DDY(covariantMetricTensor.Getg11()) + differential_operators->DDX(covariantMetricTensor.Getg12()) - differential_operators->DDX(covariantMetricTensor.Getg12())) - // which equals to - 0.5 * contravariantMetricTensor.Getg13() - * differential_operators->DDY(covariantMetricTensor.Getg11(), dy) - // + 0.5 *g32*(differential_operators->DDY(covariantMetricTensor.Getg21()) + differential_operators->DDX(covariantMetricTensor.Getg22()) - differential_operators->DDY(covariantMetricTensor.Getg12())) - // which equals to - + 0.5 * contravariantMetricTensor.Getg23() - * differential_operators->DDX(covariantMetricTensor.Getg22(), dx) - //+ 0.5 *g33*(differential_operators->DDY(covariantMetricTensor.Getg31()) + differential_operators->DDX(covariantMetricTensor.Getg32()) - differential_operators->DDZ(covariantMetricTensor.Getg12())); - // which equals to - + 0.5 * contravariantMetricTensor.Getg33() - * (differential_operators->DDY(covariantMetricTensor.Getg13(), dy)) - + differential_operators->DDX(covariantMetricTensor.Getg23(), dx) - - differential_operators->DDZ(covariantMetricTensor.Getg12()); - G3_13_ = 0.5 * contravariantMetricTensor.Getg13() - * differential_operators->DDZ(covariantMetricTensor.Getg11()) - + 0.5 * contravariantMetricTensor.Getg23() - * (differential_operators->DDZ(covariantMetricTensor.Getg12()) - + differential_operators->DDX(covariantMetricTensor.Getg23(), dx) - - differential_operators->DDY(covariantMetricTensor.Getg13(), dy)) - + 0.5 * contravariantMetricTensor.Getg33() - * differential_operators->DDX(covariantMetricTensor.Getg33(), dx); - G3_23_ = 0.5 * contravariantMetricTensor.Getg13() - * (differential_operators->DDZ(covariantMetricTensor.Getg12()) - + differential_operators->DDY(covariantMetricTensor.Getg13(), dy)) - - differential_operators->DDX(covariantMetricTensor.Getg23(), dx) - + 0.5 * contravariantMetricTensor.Getg23() - * differential_operators->DDZ(covariantMetricTensor.Getg22()) - + 0.5 * contravariantMetricTensor.Getg33() - * differential_operators->DDY(covariantMetricTensor.Getg33(), dy); + christoffel_symbols.CalculateChristoffelSymbols(dx, dy, contravariantMetricTensor, + covariantMetricTensor); } MetricTensor::FieldMetric Geometry::recalculateJacobian() { @@ -292,38 +114,38 @@ const MetricTensor::FieldMetric& Geometry::g23() const { return contravariantMetricTensor.Getg23(); } -const FieldMetric& Geometry::G1_11() const { return G1_11_; } -const FieldMetric& Geometry::G1_22() const { return G1_22_; } -const FieldMetric& Geometry::G1_33() const { return G1_33_; } -const FieldMetric& Geometry::G1_12() const { return G1_12_; } -const FieldMetric& Geometry::G1_13() const { return G1_13_; } -const FieldMetric& Geometry::G1_23() const { return G1_23_; } +const FieldMetric& Geometry::G1_11() const { return christoffel_symbols.G1_11(); } +const FieldMetric& Geometry::G1_22() const { return christoffel_symbols.G1_22(); } +const FieldMetric& Geometry::G1_33() const { return christoffel_symbols.G1_33(); } +const FieldMetric& Geometry::G1_12() const { return christoffel_symbols.G1_12(); } +const FieldMetric& Geometry::G1_13() const { return christoffel_symbols.G1_13(); } +const FieldMetric& Geometry::G1_23() const { return christoffel_symbols.G1_23(); } -const FieldMetric& Geometry::G2_11() const { return G2_11_; } -const FieldMetric& Geometry::G2_22() const { return G2_22_; } -const FieldMetric& Geometry::G2_33() const { return G2_33_; } -const FieldMetric& Geometry::G2_12() const { return G2_12_; } -const FieldMetric& Geometry::G2_13() const { return G2_13_; } -const FieldMetric& Geometry::G2_23() const { return G2_23_; } +const FieldMetric& Geometry::G2_11() const { return christoffel_symbols.G2_11(); } +const FieldMetric& Geometry::G2_22() const { return christoffel_symbols.G2_22(); } +const FieldMetric& Geometry::G2_33() const { return christoffel_symbols.G2_33(); } +const FieldMetric& Geometry::G2_12() const { return christoffel_symbols.G2_12(); } +const FieldMetric& Geometry::G2_13() const { return christoffel_symbols.G2_13(); } +const FieldMetric& Geometry::G2_23() const { return christoffel_symbols.G2_23(); } -const FieldMetric& Geometry::G3_11() const { return G3_11_; } -const FieldMetric& Geometry::G3_22() const { return G3_22_; } -const FieldMetric& Geometry::G3_33() const { return G3_33_; } -const FieldMetric& Geometry::G3_12() const { return G3_12_; } -const FieldMetric& Geometry::G3_13() const { return G3_13_; } -const FieldMetric& Geometry::G3_23() const { return G3_23_; } +const FieldMetric& Geometry::G3_11() const { return christoffel_symbols.G3_11(); } +const FieldMetric& Geometry::G3_22() const { return christoffel_symbols.G3_22(); } +const FieldMetric& Geometry::G3_33() const { return christoffel_symbols.G3_33(); } +const FieldMetric& Geometry::G3_12() const { return christoffel_symbols.G3_12(); } +const FieldMetric& Geometry::G3_13() const { return christoffel_symbols.G3_13(); } +const FieldMetric& Geometry::G3_23() const { return christoffel_symbols.G3_23(); } -const FieldMetric& Geometry::G1() const { return G1_; } -const FieldMetric& Geometry::G2() const { return G2_; } -const FieldMetric& Geometry::G3() const { return G3_; } +const FieldMetric& Geometry::G1() const { return christoffel_symbols.G1(); } +const FieldMetric& Geometry::G2() const { return christoffel_symbols.G2(); } +const FieldMetric& Geometry::G3() const { return christoffel_symbols.G3(); } const FieldMetric& Geometry::J() const { return this_J; } const FieldMetric& Geometry::Bxy() const { return this_Bxy; } -void Geometry::setG1(FieldMetric G1) { G1_ = G1; } -void Geometry::setG2(FieldMetric G2) { G2_ = G2; } -void Geometry::setG3(FieldMetric G3) { G3_ = G3; } +void Geometry::setG1(FieldMetric G1) { christoffel_symbols.setG1(G1); } +void Geometry::setG2(FieldMetric G2) { christoffel_symbols.setG2(G2); } +void Geometry::setG3(FieldMetric G3) { christoffel_symbols.setG3(G3); } void Geometry::setJ(FieldMetric J) { //TODO: Calculate J and check value is close @@ -350,7 +172,7 @@ const MetricTensor& Geometry::getCovariantMetricTensor() const { void Geometry::applyToContravariantMetricTensor( std::function function) { - contravariantMetricTensor.map(function); + contravariantMetricTensor.map(std::move(function)); } void Geometry::applyToCovariantMetricTensor( @@ -359,17 +181,6 @@ void Geometry::applyToCovariantMetricTensor( } void Geometry::applyToChristoffelSymbols( - const std::function function) { - - const auto components_in = - std::vector{G1_11_, G1_22_, G1_33_, G1_12_, G1_13_, G1_23_, G2_11_, - G2_22_, G2_33_, G2_12_, G2_13_, G2_23_, G3_11_, G3_22_, - G3_33_, G3_12_, G3_13_, G3_23_, G1_, G2_, G3_}; - - FieldMetric components_out[21] = {G1_11_, G1_22_, G1_33_, G1_12_, G1_13_, G1_23_, - G2_11_, G2_22_, G2_33_, G2_12_, G2_13_, G2_23_, - G3_11_, G3_22_, G3_33_, G3_12_, G3_13_, G3_23_, - G1_, G2_, G3_}; - - std::transform(components_in.begin(), components_in.end(), components_out, function); + std::function function) { + christoffel_symbols.map(std::move(function)); } diff --git a/src/mesh/metricTensor.cxx b/src/mesh/metricTensor.cxx index 1edb757812..841c7ecd75 100644 --- a/src/mesh/metricTensor.cxx +++ b/src/mesh/metricTensor.cxx @@ -171,10 +171,11 @@ MetricTensor MetricTensor::inverse(const std::string& region) { void MetricTensor::map( const std::function function) { - const auto components_out = applyToComponents(function); + const MetricTensor updated_metric_tensor = applyToComponents(function); - auto [g_11, g_22, g_33, g_12, g_13, g_23] = components_out; - setMetricTensor(MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + setMetricTensor(MetricTensor(updated_metric_tensor.g11, updated_metric_tensor.g22, + updated_metric_tensor.g33, updated_metric_tensor.g12, + updated_metric_tensor.g13, updated_metric_tensor.g23)); } MetricTensor MetricTensor::applyToComponents( From 2cf94539330ab58212f244493e665a82c21e60a8 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Sat, 9 Dec 2023 21:19:24 +0000 Subject: [PATCH 275/491] Error handling: null check in Mesh::sourceHasYBoundaryGuards(). --- src/mesh/mesh.cxx | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/mesh/mesh.cxx b/src/mesh/mesh.cxx index 192c87fb47..0dbee548bc 100644 --- a/src/mesh/mesh.cxx +++ b/src/mesh/mesh.cxx @@ -425,10 +425,20 @@ bool Mesh::sourceHasVar(const std::string& name) { } /// Wrapper for GridDataSource::hasXBoundaryGuards -bool Mesh::sourceHasXBoundaryGuards() { return source->hasXBoundaryGuards(this); } +bool Mesh::sourceHasXBoundaryGuards() { + if (!source) { + throw BoutException("Mesh has no source. "); + } + return source->hasXBoundaryGuards(this); +} /// Wrapper for GridDataSource::hasYBoundaryGuards -bool Mesh::sourceHasYBoundaryGuards() { return source->hasYBoundaryGuards(); } +bool Mesh::sourceHasYBoundaryGuards() { + if (!source) { + throw BoutException("Mesh has no source. "); + } + return source->hasYBoundaryGuards(); +} /************************************************************************** * Communications From 3ccd7206f5417cc83c9dab946fdc47e39dbe3c65 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Sat, 9 Dec 2023 21:38:50 +0000 Subject: [PATCH 276/491] Add a constructor for ChristoffelSymbols that takes no Mesh (because FakeMesh has no 'source', which causes a crash when the constructor leads to initialisation of FieldMetric objects). --- include/bout/christoffel_symbols.hxx | 3 +++ src/mesh/christoffel_symbols.cxx | 8 ++++++++ src/mesh/geometry.cxx | 2 +- 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/include/bout/christoffel_symbols.hxx b/include/bout/christoffel_symbols.hxx index a2e54cd062..5ec858ddb0 100644 --- a/include/bout/christoffel_symbols.hxx +++ b/include/bout/christoffel_symbols.hxx @@ -28,8 +28,11 @@ public: // ChristoffelSymbols(BoutReal g11, BoutReal g22, BoutReal g33, BoutReal g12, BoutReal g13, // BoutReal g23, Mesh* mesh); + ChristoffelSymbols(DifferentialOperators* differential_operators); + ChristoffelSymbols(Mesh* mesh, DifferentialOperators* differential_operators); + const FieldMetric& G1_11() const; const FieldMetric& G1_22() const; const FieldMetric& G1_33() const; diff --git a/src/mesh/christoffel_symbols.cxx b/src/mesh/christoffel_symbols.cxx index d960678118..676332c9bf 100644 --- a/src/mesh/christoffel_symbols.cxx +++ b/src/mesh/christoffel_symbols.cxx @@ -17,6 +17,14 @@ ChristoffelSymbols::ChristoffelSymbols( differential_operators(differential_operators){ ASSERT0(differential_operators != nullptr)} + /// This constructor (taking no mesh) is needed because mesh may be a FakeMesh, + /// which has no 'source' set, leading to a SEGFAULT if the constructor leads + /// to initialisation of FieldMetric objects. + ChristoffelSymbols::ChristoffelSymbols(DifferentialOperators + * differential_operators) + : differential_operators( + differential_operators){ASSERT0(differential_operators != nullptr)} + ChristoffelSymbols::ChristoffelSymbols(Mesh * mesh, DifferentialOperators * differential_operators) : G1_11_(mesh), G1_22_(mesh), G1_33_(mesh), G1_12_(mesh), G1_13_(mesh), G1_23_(mesh), diff --git a/src/mesh/geometry.cxx b/src/mesh/geometry.cxx index 1a78af5491..4a223d3a1b 100644 --- a/src/mesh/geometry.cxx +++ b/src/mesh/geometry.cxx @@ -13,7 +13,7 @@ Geometry::Geometry(const FieldMetric& J, const FieldMetric& Bxy, const FieldMetr const FieldMetric& g_33, const FieldMetric& g_12, const FieldMetric& g_13, const FieldMetric& g_23, DifferentialOperators* differential_operators, Mesh* mesh) - : christoffel_symbols(mesh, differential_operators), + : christoffel_symbols(differential_operators), contravariantMetricTensor(g11, g22, g33, g12, g13, g23), covariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23), this_J(J), this_Bxy(Bxy), differential_operators(differential_operators) { From 83c5d721a2a62644e58a36c17f6a2d7721af9320 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Sat, 9 Dec 2023 11:36:17 +0000 Subject: [PATCH 277/491] Make 'function' parameter to map() and applyToComponents() methods a reference. --- include/bout/christoffel_symbols.hxx | 4 ++-- include/bout/geometry.hxx | 8 ++++---- include/bout/metricTensor.hxx | 4 ++-- src/mesh/christoffel_symbols.cxx | 4 ++-- src/mesh/geometry.cxx | 21 ++++++++++----------- src/mesh/metricTensor.cxx | 4 ++-- 6 files changed, 22 insertions(+), 23 deletions(-) diff --git a/include/bout/christoffel_symbols.hxx b/include/bout/christoffel_symbols.hxx index 5ec858ddb0..bc950bb594 100644 --- a/include/bout/christoffel_symbols.hxx +++ b/include/bout/christoffel_symbols.hxx @@ -85,7 +85,7 @@ public: const MetricTensor& covariantMetricTensor); // Transforms the ChristoffelSymbols by applying the given function to every element - void map(std::function function); + void map(const std::function& function); private: FieldMetric G1_11_, G1_22_, G1_33_, G1_12_, G1_13_, G1_23_; @@ -96,7 +96,7 @@ private: DifferentialOperators* differential_operators; ChristoffelSymbols - applyToComponents(std::function function) const; + applyToComponents(const std::function& function) const; }; #endif //BOUT_CHRISTOFFELSYMBOLS_HXX diff --git a/include/bout/geometry.hxx b/include/bout/geometry.hxx index cde6c11c3e..53d4dcec03 100644 --- a/include/bout/geometry.hxx +++ b/include/bout/geometry.hxx @@ -135,13 +135,13 @@ public: FieldMetric recalculateBxy(); void applyToContravariantMetricTensor( - std::function function); + const std::function& function); void applyToCovariantMetricTensor( - std::function function); + const std::function& function); - void applyToChristoffelSymbols( - const std::function function); + void + applyToChristoffelSymbols(const std::function& function); private: /// Christoffel symbol of the second kind (connection coefficients) diff --git a/include/bout/metricTensor.hxx b/include/bout/metricTensor.hxx index fcf126585c..2ee8e5a1b3 100644 --- a/include/bout/metricTensor.hxx +++ b/include/bout/metricTensor.hxx @@ -40,10 +40,10 @@ public: MetricTensor inverse(const std::string& region = "RGN_ALL"); // Transforms the MetricTensor by applying the given function to every component - void map(std::function function); + void map(const std::function& function); MetricTensor - applyToComponents(std::function function) const; + applyToComponents(const std::function& function) const; protected: FieldMetric g11, g22, g33, g12, g13, g23; diff --git a/src/mesh/christoffel_symbols.cxx b/src/mesh/christoffel_symbols.cxx index 676332c9bf..0ba183e8c7 100644 --- a/src/mesh/christoffel_symbols.cxx +++ b/src/mesh/christoffel_symbols.cxx @@ -276,7 +276,7 @@ void ChristoffelSymbols::CalculateChristoffelSymbols( } void ChristoffelSymbols::map( - const std::function function) { + const std::function& function) { const ChristoffelSymbols updated_christoffel_symbols = applyToComponents(function); @@ -295,7 +295,7 @@ void ChristoffelSymbols::map( } ChristoffelSymbols ChristoffelSymbols::applyToComponents( - const std::function function) const { + const std::function& function) const { const auto components_in = std::vector{G1_11_, G1_22_, G1_33_, G1_12_, G1_13_, G1_23_, G2_11_, diff --git a/src/mesh/geometry.cxx b/src/mesh/geometry.cxx index 4a223d3a1b..d5f9cc2ae8 100644 --- a/src/mesh/geometry.cxx +++ b/src/mesh/geometry.cxx @@ -16,18 +16,17 @@ Geometry::Geometry(const FieldMetric& J, const FieldMetric& Bxy, const FieldMetr : christoffel_symbols(differential_operators), contravariantMetricTensor(g11, g22, g33, g12, g13, g23), covariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23), this_J(J), this_Bxy(Bxy), - differential_operators(differential_operators) { - ASSERT0(differential_operators != nullptr); -} + differential_operators(differential_operators){ + ASSERT0(differential_operators != nullptr)} -Geometry::Geometry(Mesh* mesh, DifferentialOperators* differential_operators) + Geometry::Geometry(Mesh * mesh, DifferentialOperators * differential_operators) //bool force_interpolate_from_centre : christoffel_symbols(mesh, differential_operators), contravariantMetricTensor(1., 1., 1., 0, 0, 0, mesh), // Identity metric tensor covariantMetricTensor(1., 1., 1., 0, 0, 0, mesh), this_J(1., mesh), this_Bxy(1., mesh), differential_operators(differential_operators) { - ASSERT0(differential_operators != nullptr); + ASSERT0(differential_operators != nullptr) } void Geometry::CalculateChristoffelSymbols(const FieldMetric& dx, const FieldMetric& dy) { @@ -171,16 +170,16 @@ const MetricTensor& Geometry::getCovariantMetricTensor() const { } void Geometry::applyToContravariantMetricTensor( - std::function function) { - contravariantMetricTensor.map(std::move(function)); + const std::function& function) { + contravariantMetricTensor.map(function); } void Geometry::applyToCovariantMetricTensor( - std::function function) { - covariantMetricTensor.map(std::move(function)); + const std::function& function) { + covariantMetricTensor.map(function); } void Geometry::applyToChristoffelSymbols( - std::function function) { - christoffel_symbols.map(std::move(function)); + const std::function& function) { + christoffel_symbols.map(function); } diff --git a/src/mesh/metricTensor.cxx b/src/mesh/metricTensor.cxx index 841c7ecd75..37fa1afed5 100644 --- a/src/mesh/metricTensor.cxx +++ b/src/mesh/metricTensor.cxx @@ -169,7 +169,7 @@ MetricTensor MetricTensor::inverse(const std::string& region) { } void MetricTensor::map( - const std::function function) { + const std::function& function) { const MetricTensor updated_metric_tensor = applyToComponents(function); @@ -179,7 +179,7 @@ void MetricTensor::map( } MetricTensor MetricTensor::applyToComponents( - const std::function function) const { + const std::function& function) const { const auto components_in = std::vector{g11, g22, g33, g12, g13, g23}; From 8c06cd149b1ba0c895e378b861b5ed15f11be348 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Sat, 9 Dec 2023 11:40:52 +0000 Subject: [PATCH 278/491] Make parameters to metric tensor setters const references. --- include/bout/coordinates.hxx | 4 ++-- include/bout/geometry.hxx | 4 ++-- src/mesh/coordinates.cxx | 8 ++++---- src/mesh/geometry.cxx | 4 ++-- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index bb5c29b8dd..d6918591eb 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -117,12 +117,12 @@ public: const FieldMetric& g13() const; const FieldMetric& g23() const; - void setContravariantMetricTensor(MetricTensor metric_tensor, + void setContravariantMetricTensor(const MetricTensor& metric_tensor, const std::string& region = "RGN_ALL", bool recalculate_staggered = true, bool force_interpolate_from_centre = false); - void setCovariantMetricTensor(MetricTensor metric_tensor, + void setCovariantMetricTensor(const MetricTensor& metric_tensor, const std::string& region = "RGN_ALL", bool recalculate_staggered = true, bool force_interpolate_from_centre = false); diff --git a/include/bout/geometry.hxx b/include/bout/geometry.hxx index 53d4dcec03..22e5b20048 100644 --- a/include/bout/geometry.hxx +++ b/include/bout/geometry.hxx @@ -111,10 +111,10 @@ public: ///< Magnitude of B = nabla z times nabla x const FieldMetric& Bxy() const; - void setContravariantMetricTensor(MetricTensor metric_tensor, + void setContravariantMetricTensor(const MetricTensor& metric_tensor, const std::string& region = "RGN_ALL"); - void setCovariantMetricTensor(MetricTensor metric_tensor, + void setCovariantMetricTensor(const MetricTensor& metric_tensor, const std::string& region = "RGN_ALL"); void setJ(FieldMetric J); diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index f6299eedb7..737b0a3f74 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -1389,19 +1389,19 @@ void Coordinates::setIntShiftTorsion(FieldMetric IntShiftTorsion) { IntShiftTorsion_ = std::move(IntShiftTorsion); } -void Coordinates::setContravariantMetricTensor(MetricTensor metric_tensor, +void Coordinates::setContravariantMetricTensor(const MetricTensor& metric_tensor, const std::string& region, bool recalculate_staggered, bool force_interpolate_from_centre) { - geometry.setContravariantMetricTensor(std::move(metric_tensor), region); + geometry.setContravariantMetricTensor(metric_tensor, region); recalculateAndReset(recalculate_staggered, force_interpolate_from_centre); } -void Coordinates::setCovariantMetricTensor(MetricTensor metric_tensor, +void Coordinates::setCovariantMetricTensor(const MetricTensor& metric_tensor, const std::string& region, bool recalculate_staggered, bool force_interpolate_from_centre) { - geometry.setCovariantMetricTensor(std::move(metric_tensor), region); + geometry.setCovariantMetricTensor(metric_tensor, region); recalculateAndReset(recalculate_staggered, force_interpolate_from_centre); } diff --git a/src/mesh/geometry.cxx b/src/mesh/geometry.cxx index d5f9cc2ae8..20dd7bdb9b 100644 --- a/src/mesh/geometry.cxx +++ b/src/mesh/geometry.cxx @@ -63,13 +63,13 @@ void Geometry::checkCovariant(int ystart) { covariantMetricTensor.check(ystart); void Geometry::checkContravariant(int ystart) { contravariantMetricTensor.check(ystart); } -void Geometry::setContravariantMetricTensor(MetricTensor metric_tensor, +void Geometry::setContravariantMetricTensor(const MetricTensor& metric_tensor, const std::string& region) { contravariantMetricTensor.setMetricTensor(metric_tensor); covariantMetricTensor.setMetricTensor(contravariantMetricTensor.inverse(region)); } -void Geometry::setCovariantMetricTensor(MetricTensor metric_tensor, +void Geometry::setCovariantMetricTensor(const MetricTensor& metric_tensor, const std::string& region) { covariantMetricTensor.setMetricTensor(metric_tensor); contravariantMetricTensor.setMetricTensor(covariantMetricTensor.inverse(region)); From b8e192d1d41f13433f8ae9999ad393700b389e6c Mon Sep 17 00:00:00 2001 From: tomc271 Date: Sat, 9 Dec 2023 11:58:13 +0000 Subject: [PATCH 279/491] Rename getters (and private fields) for metric tensor components. --- include/bout/metricTensor.hxx | 18 +- src/mesh/christoffel_symbols.cxx | 314 ++++++++++++++-------------- src/mesh/differential_operators.cxx | 40 ++-- src/mesh/geometry.cxx | 46 ++-- src/mesh/metricTensor.cxx | 122 +++++------ 5 files changed, 267 insertions(+), 273 deletions(-) diff --git a/include/bout/metricTensor.hxx b/include/bout/metricTensor.hxx index 2ee8e5a1b3..2ef093621b 100644 --- a/include/bout/metricTensor.hxx +++ b/include/bout/metricTensor.hxx @@ -24,12 +24,12 @@ public: // check that tensors are positive (if expected) and finite (always) void check(int ystart); - const FieldMetric& Getg11() const; - const FieldMetric& Getg22() const; - const FieldMetric& Getg33() const; - const FieldMetric& Getg12() const; - const FieldMetric& Getg13() const; - const FieldMetric& Getg23() const; + const FieldMetric& g11() const; + const FieldMetric& g22() const; + const FieldMetric& g33() const; + const FieldMetric& g12() const; + const FieldMetric& g13() const; + const FieldMetric& g23() const; void setMetricTensor(const MetricTensor& metric_tensor); @@ -42,11 +42,11 @@ public: // Transforms the MetricTensor by applying the given function to every component void map(const std::function& function); - MetricTensor - applyToComponents(const std::function& function) const; + MetricTensor applyToComponents( + const std::function& function) const; protected: - FieldMetric g11, g22, g33, g12, g13, g23; + FieldMetric g11_, g22_, g33_, g12_, g13_, g23_; }; #endif //BOUT_METRICTENSOR_HXX diff --git a/src/mesh/christoffel_symbols.cxx b/src/mesh/christoffel_symbols.cxx index 0ba183e8c7..ce3692f253 100644 --- a/src/mesh/christoffel_symbols.cxx +++ b/src/mesh/christoffel_symbols.cxx @@ -100,179 +100,173 @@ void ChristoffelSymbols::CalculateChristoffelSymbols( // Note: This calculation is completely general: metric // tensor can be 2D or 3D. For 2D, all DDZ terms are zero - G1_11_ = - 0.5 * contravariantMetricTensor.Getg11() - * differential_operators->DDX(covariantMetricTensor.Getg11(), dx) - + contravariantMetricTensor.Getg12() - * (differential_operators->DDX(covariantMetricTensor.Getg12(), dx) - - 0.5 * differential_operators->DDY(covariantMetricTensor.Getg11(), dy)) - + contravariantMetricTensor.Getg13() - * (differential_operators->DDX(covariantMetricTensor.Getg13(), dx) - - 0.5 * differential_operators->DDZ(covariantMetricTensor.Getg11())); - G1_22_ = contravariantMetricTensor.Getg11() - * (differential_operators->DDY(covariantMetricTensor.Getg12(), dy) - - 0.5 * differential_operators->DDX(covariantMetricTensor.Getg22(), dx)) - + 0.5 * contravariantMetricTensor.Getg12() - * differential_operators->DDY(covariantMetricTensor.Getg22(), dy) - + contravariantMetricTensor.Getg13() - * (differential_operators->DDY(covariantMetricTensor.Getg23(), dy) - - 0.5 * differential_operators->DDZ(covariantMetricTensor.Getg22())); - G1_33_ = - contravariantMetricTensor.Getg11() - * (differential_operators->DDZ(covariantMetricTensor.Getg13()) - - 0.5 * differential_operators->DDX(covariantMetricTensor.Getg33(), dx)) - + contravariantMetricTensor.Getg12() - * (differential_operators->DDZ(covariantMetricTensor.Getg23()) - - 0.5 * differential_operators->DDY(covariantMetricTensor.Getg33(), dy)) - + 0.5 * contravariantMetricTensor.Getg13() - * differential_operators->DDZ(covariantMetricTensor.Getg33()); - G1_12_ = 0.5 * contravariantMetricTensor.Getg11() - * differential_operators->DDY(covariantMetricTensor.Getg11(), dy) - + 0.5 * contravariantMetricTensor.Getg12() - * differential_operators->DDX(covariantMetricTensor.Getg22(), dx) - + 0.5 * contravariantMetricTensor.Getg13() - * (differential_operators->DDY(covariantMetricTensor.Getg13(), dy) - + differential_operators->DDX(covariantMetricTensor.Getg23(), dx) - - differential_operators->DDZ(covariantMetricTensor.Getg12())); - G1_13_ = 0.5 * contravariantMetricTensor.Getg11() - * differential_operators->DDZ(covariantMetricTensor.Getg11()) - + 0.5 * contravariantMetricTensor.Getg12() - * (differential_operators->DDZ(covariantMetricTensor.Getg12()) - + differential_operators->DDX(covariantMetricTensor.Getg23(), dx) - - differential_operators->DDY(covariantMetricTensor.Getg13(), dy)) - + 0.5 * contravariantMetricTensor.Getg13() - * differential_operators->DDX(covariantMetricTensor.Getg33(), dx); + G1_11_ = 0.5 * contravariantMetricTensor.g11() + * differential_operators->DDX(covariantMetricTensor.g11(), dx) + + contravariantMetricTensor.g12() + * (differential_operators->DDX(covariantMetricTensor.g12(), dx) + - 0.5 * differential_operators->DDY(covariantMetricTensor.g11(), dy)) + + contravariantMetricTensor.g13() + * (differential_operators->DDX(covariantMetricTensor.g13(), dx) + - 0.5 * differential_operators->DDZ(covariantMetricTensor.g11())); + G1_22_ = contravariantMetricTensor.g11() + * (differential_operators->DDY(covariantMetricTensor.g12(), dy) + - 0.5 * differential_operators->DDX(covariantMetricTensor.g22(), dx)) + + 0.5 * contravariantMetricTensor.g12() + * differential_operators->DDY(covariantMetricTensor.g22(), dy) + + contravariantMetricTensor.g13() + * (differential_operators->DDY(covariantMetricTensor.g23(), dy) + - 0.5 * differential_operators->DDZ(covariantMetricTensor.g22())); + G1_33_ = contravariantMetricTensor.g11() + * (differential_operators->DDZ(covariantMetricTensor.g13()) + - 0.5 * differential_operators->DDX(covariantMetricTensor.g33(), dx)) + + contravariantMetricTensor.g12() + * (differential_operators->DDZ(covariantMetricTensor.g23()) + - 0.5 * differential_operators->DDY(covariantMetricTensor.g33(), dy)) + + 0.5 * contravariantMetricTensor.g13() + * differential_operators->DDZ(covariantMetricTensor.g33()); + G1_12_ = 0.5 * contravariantMetricTensor.g11() + * differential_operators->DDY(covariantMetricTensor.g11(), dy) + + 0.5 * contravariantMetricTensor.g12() + * differential_operators->DDX(covariantMetricTensor.g22(), dx) + + 0.5 * contravariantMetricTensor.g13() + * (differential_operators->DDY(covariantMetricTensor.g13(), dy) + + differential_operators->DDX(covariantMetricTensor.g23(), dx) + - differential_operators->DDZ(covariantMetricTensor.g12())); + G1_13_ = 0.5 * contravariantMetricTensor.g11() + * differential_operators->DDZ(covariantMetricTensor.g11()) + + 0.5 * contravariantMetricTensor.g12() + * (differential_operators->DDZ(covariantMetricTensor.g12()) + + differential_operators->DDX(covariantMetricTensor.g23(), dx) + - differential_operators->DDY(covariantMetricTensor.g13(), dy)) + + 0.5 * contravariantMetricTensor.g13() + * differential_operators->DDX(covariantMetricTensor.g33(), dx); G1_23_ = - 0.5 * contravariantMetricTensor.Getg11() - * (differential_operators->DDZ(covariantMetricTensor.Getg12()) - + differential_operators->DDY(covariantMetricTensor.Getg13(), dy) - - differential_operators->DDX(covariantMetricTensor.Getg23(), dx)) - + 0.5 * contravariantMetricTensor.Getg12() - * (differential_operators->DDZ(covariantMetricTensor.Getg22()) - + differential_operators->DDY(covariantMetricTensor.Getg23(), dy) - - differential_operators->DDY(covariantMetricTensor.Getg23(), dy)) + 0.5 * contravariantMetricTensor.g11() + * (differential_operators->DDZ(covariantMetricTensor.g12()) + + differential_operators->DDY(covariantMetricTensor.g13(), dy) + - differential_operators->DDX(covariantMetricTensor.g23(), dx)) + + 0.5 * contravariantMetricTensor.g12() + * (differential_operators->DDZ(covariantMetricTensor.g22()) + + differential_operators->DDY(covariantMetricTensor.g23(), dy) + - differential_operators->DDY(covariantMetricTensor.g23(), dy)) // + 0.5 *g13*(differential_operators->DDZ(g_32) + differential_operators->DDY(g_33) - differential_operators->DDZ(g_23)); // which equals - + 0.5 * contravariantMetricTensor.Getg13() - * differential_operators->DDY(covariantMetricTensor.Getg33(), dy); + + 0.5 * contravariantMetricTensor.g13() + * differential_operators->DDY(covariantMetricTensor.g33(), dy); - G2_11_ = - 0.5 * contravariantMetricTensor.Getg12() - * differential_operators->DDX(covariantMetricTensor.Getg11(), dx) - + contravariantMetricTensor.Getg22() - * (differential_operators->DDX(covariantMetricTensor.Getg12(), dx) - - 0.5 * differential_operators->DDY(covariantMetricTensor.Getg11(), dy)) - + contravariantMetricTensor.Getg23() - * (differential_operators->DDX(covariantMetricTensor.Getg13(), dx) - - 0.5 * differential_operators->DDZ(covariantMetricTensor.Getg11())); - G2_22_ = contravariantMetricTensor.Getg12() - * (differential_operators->DDY(covariantMetricTensor.Getg12(), dy) - - 0.5 * differential_operators->DDX(covariantMetricTensor.Getg22(), dx)) - + 0.5 * contravariantMetricTensor.Getg22() - * differential_operators->DDY(covariantMetricTensor.Getg22(), dy) - + contravariantMetricTensor.Getg23() - * (differential_operators->DDY(contravariantMetricTensor.Getg23(), dy) - - 0.5 * differential_operators->DDZ(covariantMetricTensor.Getg22())); - G2_33_ = - contravariantMetricTensor.Getg12() - * (differential_operators->DDZ(covariantMetricTensor.Getg13()) - - 0.5 * differential_operators->DDX(covariantMetricTensor.Getg33(), dx)) - + contravariantMetricTensor.Getg22() - * (differential_operators->DDZ(covariantMetricTensor.Getg23()) - - 0.5 * differential_operators->DDY(covariantMetricTensor.Getg33(), dy)) - + 0.5 * contravariantMetricTensor.Getg23() - * differential_operators->DDZ(covariantMetricTensor.Getg33()); - G2_12_ = 0.5 * contravariantMetricTensor.Getg12() - * differential_operators->DDY(covariantMetricTensor.Getg11(), dy) - + 0.5 * contravariantMetricTensor.Getg22() - * differential_operators->DDX(covariantMetricTensor.Getg22(), dx) - + 0.5 * contravariantMetricTensor.Getg23() - * (differential_operators->DDY(covariantMetricTensor.Getg13(), dy) - + differential_operators->DDX(covariantMetricTensor.Getg23(), dx) - - differential_operators->DDZ(covariantMetricTensor.Getg12())); + G2_11_ = 0.5 * contravariantMetricTensor.g12() + * differential_operators->DDX(covariantMetricTensor.g11(), dx) + + contravariantMetricTensor.g22() + * (differential_operators->DDX(covariantMetricTensor.g12(), dx) + - 0.5 * differential_operators->DDY(covariantMetricTensor.g11(), dy)) + + contravariantMetricTensor.g23() + * (differential_operators->DDX(covariantMetricTensor.g13(), dx) + - 0.5 * differential_operators->DDZ(covariantMetricTensor.g11())); + G2_22_ = contravariantMetricTensor.g12() + * (differential_operators->DDY(covariantMetricTensor.g12(), dy) + - 0.5 * differential_operators->DDX(covariantMetricTensor.g22(), dx)) + + 0.5 * contravariantMetricTensor.g22() + * differential_operators->DDY(covariantMetricTensor.g22(), dy) + + contravariantMetricTensor.g23() + * (differential_operators->DDY(contravariantMetricTensor.g23(), dy) + - 0.5 * differential_operators->DDZ(covariantMetricTensor.g22())); + G2_33_ = contravariantMetricTensor.g12() + * (differential_operators->DDZ(covariantMetricTensor.g13()) + - 0.5 * differential_operators->DDX(covariantMetricTensor.g33(), dx)) + + contravariantMetricTensor.g22() + * (differential_operators->DDZ(covariantMetricTensor.g23()) + - 0.5 * differential_operators->DDY(covariantMetricTensor.g33(), dy)) + + 0.5 * contravariantMetricTensor.g23() + * differential_operators->DDZ(covariantMetricTensor.g33()); + G2_12_ = 0.5 * contravariantMetricTensor.g12() + * differential_operators->DDY(covariantMetricTensor.g11(), dy) + + 0.5 * contravariantMetricTensor.g22() + * differential_operators->DDX(covariantMetricTensor.g22(), dx) + + 0.5 * contravariantMetricTensor.g23() + * (differential_operators->DDY(covariantMetricTensor.g13(), dy) + + differential_operators->DDX(covariantMetricTensor.g23(), dx) + - differential_operators->DDZ(covariantMetricTensor.g12())); G2_13_ = - // 0.5 *g21*(differential_operators->DDZ(covariantMetricTensor.Getg11()) + differential_operators->DDX(covariantMetricTensor.Getg13()) - differential_operators->DDX(covariantMetricTensor.Getg13())) + // 0.5 *g21*(differential_operators->DDZ(covariantMetricTensor.g11()) + differential_operators->DDX(covariantMetricTensor.Getg13()) - differential_operators->DDX(covariantMetricTensor.g13())) // which equals - 0.5 * contravariantMetricTensor.Getg12() - * (differential_operators->DDZ(covariantMetricTensor.Getg11()) - + differential_operators->DDX(covariantMetricTensor.Getg13(), dx) - - differential_operators->DDX(covariantMetricTensor.Getg13(), dx)) - // + 0.5 *g22*(differential_operators->DDZ(covariantMetricTensor.Getg21()) + differential_operators->DDX(covariantMetricTensor.Getg23()) - differential_operators->DDY(covariantMetricTensor.Getg13())) + 0.5 * contravariantMetricTensor.g12() + * (differential_operators->DDZ(covariantMetricTensor.g11()) + + differential_operators->DDX(covariantMetricTensor.g13(), dx) + - differential_operators->DDX(covariantMetricTensor.g13(), dx)) + // + 0.5 *g22*(differential_operators->DDZ(covariantMetricTensor.Getg21()) + differential_operators->DDX(covariantMetricTensor.g23()) - differential_operators->DDY(covariantMetricTensor.g13())) // which equals - + 0.5 * contravariantMetricTensor.Getg22() - * (differential_operators->DDZ(covariantMetricTensor.Getg12()) - + differential_operators->DDX(covariantMetricTensor.Getg23(), dx) - - differential_operators->DDY(covariantMetricTensor.Getg13(), dy)) - // + 0.5 *g23*(differential_operators->DDZ(covariantMetricTensor.Getg31()) + differential_operators->DDX(covariantMetricTensor.Getg33()) - differential_operators->DDZ(g_13)); + + 0.5 * contravariantMetricTensor.g22() + * (differential_operators->DDZ(covariantMetricTensor.g12()) + + differential_operators->DDX(covariantMetricTensor.g23(), dx) + - differential_operators->DDY(covariantMetricTensor.g13(), dy)) + // + 0.5 *g23*(differential_operators->DDZ(covariantMetricTensor.Getg31()) + differential_operators->DDX(covariantMetricTensor.g33()) - differential_operators->DDZ(g_13)); // which equals - + 0.5 * contravariantMetricTensor.Getg23() - * differential_operators->DDX(covariantMetricTensor.Getg33(), dx); - G2_23_ = 0.5 * contravariantMetricTensor.Getg12() - * (differential_operators->DDZ(covariantMetricTensor.Getg12()) - + differential_operators->DDY(covariantMetricTensor.Getg13(), dy) - - differential_operators->DDX(covariantMetricTensor.Getg23(), dx)) - + 0.5 * contravariantMetricTensor.Getg22() - * differential_operators->DDZ(covariantMetricTensor.Getg22()) - + 0.5 * contravariantMetricTensor.Getg23() - * differential_operators->DDY(covariantMetricTensor.Getg33(), dy); + + 0.5 * contravariantMetricTensor.g23() + * differential_operators->DDX(covariantMetricTensor.g33(), dx); + G2_23_ = 0.5 * contravariantMetricTensor.g12() + * (differential_operators->DDZ(covariantMetricTensor.g12()) + + differential_operators->DDY(covariantMetricTensor.g13(), dy) + - differential_operators->DDX(covariantMetricTensor.g23(), dx)) + + 0.5 * contravariantMetricTensor.g22() + * differential_operators->DDZ(covariantMetricTensor.g22()) + + 0.5 * contravariantMetricTensor.g23() + * differential_operators->DDY(covariantMetricTensor.g33(), dy); - G3_11_ = - 0.5 * contravariantMetricTensor.Getg13() - * differential_operators->DDX(covariantMetricTensor.Getg11(), dx) - + contravariantMetricTensor.Getg23() - * (differential_operators->DDX(covariantMetricTensor.Getg12(), dx) - - 0.5 * differential_operators->DDY(covariantMetricTensor.Getg11(), dy)) - + contravariantMetricTensor.Getg33() - * (differential_operators->DDX(covariantMetricTensor.Getg13(), dx) - - 0.5 * differential_operators->DDZ(covariantMetricTensor.Getg11())); - G3_22_ = contravariantMetricTensor.Getg13() - * (differential_operators->DDY(covariantMetricTensor.Getg12(), dy) - - 0.5 * differential_operators->DDX(covariantMetricTensor.Getg22(), dx)) - + 0.5 * contravariantMetricTensor.Getg23() - * differential_operators->DDY(covariantMetricTensor.Getg22(), dy) - + contravariantMetricTensor.Getg33() - * (differential_operators->DDY(covariantMetricTensor.Getg23(), dy) - - 0.5 * differential_operators->DDZ(covariantMetricTensor.Getg22())); - G3_33_ = - contravariantMetricTensor.Getg13() - * (differential_operators->DDZ(covariantMetricTensor.Getg13()) - - 0.5 * differential_operators->DDX(covariantMetricTensor.Getg33(), dx)) - + contravariantMetricTensor.Getg23() - * (differential_operators->DDZ(covariantMetricTensor.Getg23()) - - 0.5 * differential_operators->DDY(covariantMetricTensor.Getg33(), dy)) - + 0.5 * contravariantMetricTensor.Getg33() - * differential_operators->DDZ(covariantMetricTensor.Getg33()); + G3_11_ = 0.5 * contravariantMetricTensor.g13() + * differential_operators->DDX(covariantMetricTensor.g11(), dx) + + contravariantMetricTensor.g23() + * (differential_operators->DDX(covariantMetricTensor.g12(), dx) + - 0.5 * differential_operators->DDY(covariantMetricTensor.g11(), dy)) + + contravariantMetricTensor.g33() + * (differential_operators->DDX(covariantMetricTensor.g13(), dx) + - 0.5 * differential_operators->DDZ(covariantMetricTensor.g11())); + G3_22_ = contravariantMetricTensor.g13() + * (differential_operators->DDY(covariantMetricTensor.g12(), dy) + - 0.5 * differential_operators->DDX(covariantMetricTensor.g22(), dx)) + + 0.5 * contravariantMetricTensor.g23() + * differential_operators->DDY(covariantMetricTensor.g22(), dy) + + contravariantMetricTensor.g33() + * (differential_operators->DDY(covariantMetricTensor.g23(), dy) + - 0.5 * differential_operators->DDZ(covariantMetricTensor.g22())); + G3_33_ = contravariantMetricTensor.g13() + * (differential_operators->DDZ(covariantMetricTensor.g13()) + - 0.5 * differential_operators->DDX(covariantMetricTensor.g33(), dx)) + + contravariantMetricTensor.g23() + * (differential_operators->DDZ(covariantMetricTensor.g23()) + - 0.5 * differential_operators->DDY(covariantMetricTensor.g33(), dy)) + + 0.5 * contravariantMetricTensor.g33() + * differential_operators->DDZ(covariantMetricTensor.g33()); G3_12_ = - // 0.5 *g31*(differential_operators->DDY(covariantMetricTensor.Getg11()) + differential_operators->DDX(covariantMetricTensor.Getg12()) - differential_operators->DDX(covariantMetricTensor.Getg12())) + // 0.5 *g31*(differential_operators->DDY(covariantMetricTensor.g11()) + differential_operators->DDX(covariantMetricTensor.Getg12()) - differential_operators->DDX(covariantMetricTensor.g12())) // which equals to - 0.5 * contravariantMetricTensor.Getg13() - * differential_operators->DDY(covariantMetricTensor.Getg11(), dy) - // + 0.5 *g32*(differential_operators->DDY(covariantMetricTensor.Getg21()) + differential_operators->DDX(covariantMetricTensor.Getg22()) - differential_operators->DDY(covariantMetricTensor.Getg12())) + 0.5 * contravariantMetricTensor.g13() + * differential_operators->DDY(covariantMetricTensor.g11(), dy) + // + 0.5 *g32*(differential_operators->DDY(covariantMetricTensor.Getg21()) + differential_operators->DDX(covariantMetricTensor.g22()) - differential_operators->DDY(covariantMetricTensor.g12())) // which equals to - + 0.5 * contravariantMetricTensor.Getg23() - * differential_operators->DDX(covariantMetricTensor.Getg22(), dx) - //+ 0.5 *g33*(differential_operators->DDY(covariantMetricTensor.Getg31()) + differential_operators->DDX(covariantMetricTensor.Getg32()) - differential_operators->DDZ(covariantMetricTensor.Getg12())); + + 0.5 * contravariantMetricTensor.g23() + * differential_operators->DDX(covariantMetricTensor.g22(), dx) + //+ 0.5 *g33*(differential_operators->DDY(covariantMetricTensor.Getg31()) + differential_operators->DDX(covariantMetricTensor.Getg32()) - differential_operators->DDZ(covariantMetricTensor.g12())); // which equals to - + 0.5 * contravariantMetricTensor.Getg33() - * (differential_operators->DDY(covariantMetricTensor.Getg13(), dy)) - + differential_operators->DDX(covariantMetricTensor.Getg23(), dx) - - differential_operators->DDZ(covariantMetricTensor.Getg12()); - G3_13_ = 0.5 * contravariantMetricTensor.Getg13() - * differential_operators->DDZ(covariantMetricTensor.Getg11()) - + 0.5 * contravariantMetricTensor.Getg23() - * (differential_operators->DDZ(covariantMetricTensor.Getg12()) - + differential_operators->DDX(covariantMetricTensor.Getg23(), dx) - - differential_operators->DDY(covariantMetricTensor.Getg13(), dy)) - + 0.5 * contravariantMetricTensor.Getg33() - * differential_operators->DDX(covariantMetricTensor.Getg33(), dx); - G3_23_ = 0.5 * contravariantMetricTensor.Getg13() - * (differential_operators->DDZ(covariantMetricTensor.Getg12()) - + differential_operators->DDY(covariantMetricTensor.Getg13(), dy)) - - differential_operators->DDX(covariantMetricTensor.Getg23(), dx) - + 0.5 * contravariantMetricTensor.Getg23() - * differential_operators->DDZ(covariantMetricTensor.Getg22()) - + 0.5 * contravariantMetricTensor.Getg33() - * differential_operators->DDY(covariantMetricTensor.Getg33(), dy); + + 0.5 * contravariantMetricTensor.g33() + * (differential_operators->DDY(covariantMetricTensor.g13(), dy)) + + differential_operators->DDX(covariantMetricTensor.g23(), dx) + - differential_operators->DDZ(covariantMetricTensor.g12()); + G3_13_ = 0.5 * contravariantMetricTensor.g13() + * differential_operators->DDZ(covariantMetricTensor.g11()) + + 0.5 * contravariantMetricTensor.g23() + * (differential_operators->DDZ(covariantMetricTensor.g12()) + + differential_operators->DDX(covariantMetricTensor.g23(), dx) + - differential_operators->DDY(covariantMetricTensor.g13(), dy)) + + 0.5 * contravariantMetricTensor.g33() + * differential_operators->DDX(covariantMetricTensor.g33(), dx); + G3_23_ = 0.5 * contravariantMetricTensor.g13() + * (differential_operators->DDZ(covariantMetricTensor.g12()) + + differential_operators->DDY(covariantMetricTensor.g13(), dy)) + - differential_operators->DDX(covariantMetricTensor.g23(), dx) + + 0.5 * contravariantMetricTensor.g23() + * differential_operators->DDZ(covariantMetricTensor.g22()) + + 0.5 * contravariantMetricTensor.g33() + * differential_operators->DDY(covariantMetricTensor.g33(), dy); } void ChristoffelSymbols::map( diff --git a/src/mesh/differential_operators.cxx b/src/mesh/differential_operators.cxx index 3823a46940..a05c9bce58 100644 --- a/src/mesh/differential_operators.cxx +++ b/src/mesh/differential_operators.cxx @@ -229,7 +229,7 @@ Field2D DifferentialOperators::Grad2_par2(const Field2D& f, const Field2D& dy, const auto grad2_par2_DDY_invSg = coordinates->Grad2_par2_DDY_invSg(outloc, method); return grad2_par2_DDY_invSg * DDY(f, dy, outloc, method) - + D2DY2(f, outloc, method) / covariantMetricTensor.Getg22(); + + D2DY2(f, outloc, method) / covariantMetricTensor.g22(); } Field3D DifferentialOperators::Grad2_par2(const Field3D& f, @@ -242,10 +242,10 @@ Field3D DifferentialOperators::Grad2_par2(const Field3D& f, Field3D result = ::DDY(f, outloc, method); - Field3D r2 = D2DY2(f, outloc, method) / covariantMetricTensor.Getg22(); + Field3D r2 = D2DY2(f, outloc, method) / covariantMetricTensor.g22(); const auto coordinates = - covariantMetricTensor.Getg11().getCoordinates(); //TODO: Find a better way + covariantMetricTensor.g11().getCoordinates(); //TODO: Find a better way const auto grad2_par2_DDY_invSg = coordinates->Grad2_par2_DDY_invSg(outloc, method); @@ -277,9 +277,9 @@ FieldMetric DifferentialOperators::Laplace( TRACE("DifferentialOperators::Laplace( Field2D )"); return G1 * DDX(f, dy, outloc) + G2 * DDY(f, dy, outloc) - + covariantMetricTensor.Getg11() * D2DX2(f, outloc) - + covariantMetricTensor.Getg22() * D2DY2(f, outloc) - + 2.0 * covariantMetricTensor.Getg12() + + covariantMetricTensor.g11() * D2DX2(f, outloc) + + covariantMetricTensor.g22() * D2DY2(f, outloc) + + 2.0 * covariantMetricTensor.g12() * D2DXDY(f, outloc, "DEFAULT", "RGN_NOBNDRY", dfdy_boundary_conditions, dfdy_dy_region); } @@ -293,15 +293,15 @@ Field3D DifferentialOperators::Laplace(const Field3D& f, TRACE("DifferentialOperators::Laplace( Field3D )"); Field3D result = G1 * ::DDX(f, outloc) + G2 * ::DDY(f, outloc) + G3 * ::DDZ(f, outloc) - + covariantMetricTensor.Getg11() * D2DX2(f, outloc) - + covariantMetricTensor.Getg22() * D2DY2(f, outloc) - + covariantMetricTensor.Getg33() * D2DZ2(f, outloc) + + covariantMetricTensor.g11() * D2DX2(f, outloc) + + covariantMetricTensor.g22() * D2DY2(f, outloc) + + covariantMetricTensor.g33() * D2DZ2(f, outloc) + 2.0 - * (covariantMetricTensor.Getg12() + * (covariantMetricTensor.g12() * D2DXDY(f, outloc, "DEFAULT", "RGN_NOBNDRY", dfdy_boundary_conditions, dfdy_dy_region) - + covariantMetricTensor.Getg13() * D2DXDZ(f, outloc) - + covariantMetricTensor.Getg23() * D2DYDZ(f, outloc)); + + covariantMetricTensor.g13() * D2DXDZ(f, outloc) + + covariantMetricTensor.g23() * D2DYDZ(f, outloc)); return result; } @@ -324,7 +324,7 @@ Field2D DifferentialOperators::Laplace_perpXY([[maybe_unused]] const Field2D& A, const auto outer_x_avg = [&i](const auto& f) { return 0.5 * (f[i] + f[i.xp()]); }; const BoutReal outer_x_A = outer_x_avg(A); const BoutReal outer_x_J = outer_x_avg(J); - const BoutReal outer_x_g11 = outer_x_avg(covariantMetricTensor.Getg11()); + const BoutReal outer_x_g11 = outer_x_avg(covariantMetricTensor.g11()); const BoutReal outer_x_dx = outer_x_avg(dx); const BoutReal outer_x_value = outer_x_A * outer_x_J * outer_x_g11 / (J[i] * outer_x_dx * dx[i]); @@ -334,7 +334,7 @@ Field2D DifferentialOperators::Laplace_perpXY([[maybe_unused]] const Field2D& A, const auto inner_x_avg = [&i](const auto& f) { return 0.5 * (f[i] + f[i.xm()]); }; const BoutReal inner_x_A = inner_x_avg(A); const BoutReal inner_x_J = inner_x_avg(J); - const BoutReal inner_x_g11 = inner_x_avg(covariantMetricTensor.Getg11()); + const BoutReal inner_x_g11 = inner_x_avg(covariantMetricTensor.g11()); const BoutReal inner_x_dx = inner_x_avg(dx); const BoutReal inner_x_value = inner_x_A * inner_x_J * inner_x_g11 / (J[i] * inner_x_dx * dx[i]); @@ -344,9 +344,9 @@ Field2D DifferentialOperators::Laplace_perpXY([[maybe_unused]] const Field2D& A, const auto upper_y_avg = [&i](const auto& f) { return 0.5 * (f[i] + f[i.yp()]); }; const BoutReal upper_y_A = upper_y_avg(A); const BoutReal upper_y_J = upper_y_avg(J); - const BoutReal upper_y_g_22 = upper_y_avg(covariantMetricTensor.Getg22()); - const BoutReal upper_y_g23 = upper_y_avg(covariantMetricTensor.Getg23()); - const BoutReal upper_y_g_23 = upper_y_avg(covariantMetricTensor.Getg23()); + const BoutReal upper_y_g_22 = upper_y_avg(covariantMetricTensor.g22()); + const BoutReal upper_y_g23 = upper_y_avg(covariantMetricTensor.g23()); + const BoutReal upper_y_g_23 = upper_y_avg(covariantMetricTensor.g23()); const BoutReal upper_y_dy = upper_y_avg(dy); const BoutReal upper_y_value = -upper_y_A * upper_y_J * upper_y_g23 * upper_y_g_23 / (upper_y_g_22 * J[i] * upper_y_dy * dy[i]); @@ -356,9 +356,9 @@ Field2D DifferentialOperators::Laplace_perpXY([[maybe_unused]] const Field2D& A, const auto lower_y_avg = [&i](const auto& f) { return 0.5 * (f[i] + f[i.ym()]); }; const BoutReal lower_y_A = lower_y_avg(A); const BoutReal lower_y_J = lower_y_avg(J); - const BoutReal lower_y_g_22 = lower_y_avg(covariantMetricTensor.Getg22()); - const BoutReal lower_y_g23 = lower_y_avg(covariantMetricTensor.Getg23()); - const BoutReal lower_y_g_23 = lower_y_avg(covariantMetricTensor.Getg23()); + const BoutReal lower_y_g_22 = lower_y_avg(covariantMetricTensor.g22()); + const BoutReal lower_y_g23 = lower_y_avg(covariantMetricTensor.g23()); + const BoutReal lower_y_g_23 = lower_y_avg(covariantMetricTensor.g23()); const BoutReal lower_y_dy = lower_y_avg(dy); const BoutReal lower_y_value = -lower_y_A * lower_y_J * lower_y_g23 * lower_y_g_23 / (lower_y_g_22 * J[i] * lower_y_dy * dy[i]); diff --git a/src/mesh/geometry.cxx b/src/mesh/geometry.cxx index 20dd7bdb9b..5be361d703 100644 --- a/src/mesh/geometry.cxx +++ b/src/mesh/geometry.cxx @@ -37,16 +37,16 @@ void Geometry::CalculateChristoffelSymbols(const FieldMetric& dx, const FieldMet MetricTensor::FieldMetric Geometry::recalculateJacobian() { // calculate Jacobian using g^-1 = det[g^ij], J = sqrt(g) - auto g = contravariantMetricTensor.Getg11() * contravariantMetricTensor.Getg22() - * contravariantMetricTensor.Getg33() - + 2.0 * contravariantMetricTensor.Getg12() * contravariantMetricTensor.Getg13() - * contravariantMetricTensor.Getg23() - - contravariantMetricTensor.Getg11() * contravariantMetricTensor.Getg23() - * contravariantMetricTensor.Getg23() - - contravariantMetricTensor.Getg22() * contravariantMetricTensor.Getg13() - * contravariantMetricTensor.Getg13() - - contravariantMetricTensor.Getg33() * contravariantMetricTensor.Getg12() - * contravariantMetricTensor.Getg12(); + auto g = contravariantMetricTensor.g11() * contravariantMetricTensor.g22() + * contravariantMetricTensor.g33() + + 2.0 * contravariantMetricTensor.g12() * contravariantMetricTensor.g13() + * contravariantMetricTensor.g23() + - contravariantMetricTensor.g11() * contravariantMetricTensor.g23() + * contravariantMetricTensor.g23() + - contravariantMetricTensor.g22() * contravariantMetricTensor.g13() + * contravariantMetricTensor.g13() + - contravariantMetricTensor.g33() * contravariantMetricTensor.g12() + * contravariantMetricTensor.g12(); // Check that g is positive bout::checkPositive(g, "The determinant of g^ij", "RGN_NOBNDRY"); @@ -56,7 +56,7 @@ MetricTensor::FieldMetric Geometry::recalculateJacobian() { MetricTensor::FieldMetric Geometry::recalculateBxy() { - return sqrt(covariantMetricTensor.Getg22()) / this_J; + return sqrt(covariantMetricTensor.g22()) / this_J; } void Geometry::checkCovariant(int ystart) { covariantMetricTensor.check(ystart); } @@ -76,41 +76,41 @@ void Geometry::setCovariantMetricTensor(const MetricTensor& metric_tensor, } const MetricTensor::FieldMetric& Geometry::g_11() const { - return covariantMetricTensor.Getg11(); + return covariantMetricTensor.g11(); } const MetricTensor::FieldMetric& Geometry::g_22() const { - return covariantMetricTensor.Getg22(); + return covariantMetricTensor.g22(); } const MetricTensor::FieldMetric& Geometry::g_33() const { - return covariantMetricTensor.Getg33(); + return covariantMetricTensor.g33(); } const MetricTensor::FieldMetric& Geometry::g_12() const { - return covariantMetricTensor.Getg12(); + return covariantMetricTensor.g12(); } const MetricTensor::FieldMetric& Geometry::g_13() const { - return covariantMetricTensor.Getg13(); + return covariantMetricTensor.g13(); } const MetricTensor::FieldMetric& Geometry::g_23() const { - return covariantMetricTensor.Getg23(); + return covariantMetricTensor.g23(); } const MetricTensor::FieldMetric& Geometry::g11() const { - return contravariantMetricTensor.Getg11(); + return contravariantMetricTensor.g11(); } const MetricTensor::FieldMetric& Geometry::g22() const { - return contravariantMetricTensor.Getg22(); + return contravariantMetricTensor.g22(); } const MetricTensor::FieldMetric& Geometry::g33() const { - return contravariantMetricTensor.Getg33(); + return contravariantMetricTensor.g33(); } const MetricTensor::FieldMetric& Geometry::g12() const { - return contravariantMetricTensor.Getg12(); + return contravariantMetricTensor.g12(); } const MetricTensor::FieldMetric& Geometry::g13() const { - return contravariantMetricTensor.Getg13(); + return contravariantMetricTensor.g13(); } const MetricTensor::FieldMetric& Geometry::g23() const { - return contravariantMetricTensor.Getg23(); + return contravariantMetricTensor.g23(); } const FieldMetric& Geometry::G1_11() const { return christoffel_symbols.G1_11(); } diff --git a/src/mesh/metricTensor.cxx b/src/mesh/metricTensor.cxx index 37fa1afed5..a7a91e8b6d 100644 --- a/src/mesh/metricTensor.cxx +++ b/src/mesh/metricTensor.cxx @@ -5,82 +5,82 @@ MetricTensor::MetricTensor(const FieldMetric& g11, const FieldMetric& g22, const FieldMetric& g33, const FieldMetric& g12, const FieldMetric& g13, const FieldMetric& g23) - : g11(g11), g22(g22), g33(g33), g12(g12), g13(g13), g23(g23) { + : g11_(g11), g22_(g22), g33_(g33), g12_(g12), g13_(g13), g23_(g23) { Allocate(); // Make sure metric elements are allocated // ; TODO: Required? } MetricTensor::MetricTensor(const BoutReal g11, const BoutReal g22, const BoutReal g33, const BoutReal g12, const BoutReal g13, const BoutReal g23, Mesh* mesh) - : g11(g11, mesh), g22(g22, mesh), g33(g33, mesh), g12(g12, mesh), g13(g13, mesh), - g23(g23, mesh) { + : g11_(g11, mesh), g22_(g22, mesh), g33_(g33, mesh), g12_(g12, mesh), g13_(g13, mesh), + g23_(g23, mesh) { Allocate(); // Make sure metric elements are allocated // ; TODO: Required? } -const MetricTensor::FieldMetric& MetricTensor::Getg11() const { return g11; } -const MetricTensor::FieldMetric& MetricTensor::Getg22() const { return g22; } -const MetricTensor::FieldMetric& MetricTensor::Getg33() const { return g33; } -const MetricTensor::FieldMetric& MetricTensor::Getg12() const { return g12; } -const MetricTensor::FieldMetric& MetricTensor::Getg13() const { return g13; } -const MetricTensor::FieldMetric& MetricTensor::Getg23() const { return g23; } +const MetricTensor::FieldMetric& MetricTensor::g11() const { return g11_; } +const MetricTensor::FieldMetric& MetricTensor::g22() const { return g22_; } +const MetricTensor::FieldMetric& MetricTensor::g33() const { return g33_; } +const MetricTensor::FieldMetric& MetricTensor::g12() const { return g12_; } +const MetricTensor::FieldMetric& MetricTensor::g13() const { return g13_; } +const MetricTensor::FieldMetric& MetricTensor::g23() const { return g23_; } void MetricTensor::setMetricTensor(const MetricTensor& metric_tensor) { - g11 = metric_tensor.Getg11(); - g22 = metric_tensor.Getg22(); - g33 = metric_tensor.Getg33(); - g12 = metric_tensor.Getg12(); - g13 = metric_tensor.Getg13(); - g23 = metric_tensor.Getg23(); + g11_ = metric_tensor.g11(); + g22_ = metric_tensor.g22(); + g33_ = metric_tensor.g33(); + g12_ = metric_tensor.g12(); + g13_ = metric_tensor.g13(); + g23_ = metric_tensor.g23(); } void MetricTensor::check(int ystart) { // Diagonal metric components should be finite - bout::checkFinite(g11, "g11", "RGN_NOCORNERS"); - bout::checkFinite(g22, "g22", "RGN_NOCORNERS"); - bout::checkFinite(g33, "g33", "RGN_NOCORNERS"); - if (g11.hasParallelSlices() && &g11.ynext(1) != &g11) { + bout::checkFinite(g11_, "g11", "RGN_NOCORNERS"); + bout::checkFinite(g22_, "g22", "RGN_NOCORNERS"); + bout::checkFinite(g33_, "g33", "RGN_NOCORNERS"); + if (g11_.hasParallelSlices() && &g11_.ynext(1) != &g11_) { for (int dy = 1; dy <= ystart; ++dy) { for (const auto sign : {1, -1}) { - bout::checkFinite(g11.ynext(sign * dy), "g11.ynext", + bout::checkFinite(g11_.ynext(sign * dy), "g11.ynext", fmt::format("RGN_YPAR_{:+d}", sign * dy)); - bout::checkFinite(g22.ynext(sign * dy), "g22.ynext", + bout::checkFinite(g22_.ynext(sign * dy), "g22.ynext", fmt::format("RGN_YPAR_{:+d}", sign * dy)); - bout::checkFinite(g33.ynext(sign * dy), "g33.ynext", + bout::checkFinite(g33_.ynext(sign * dy), "g33.ynext", fmt::format("RGN_YPAR_{:+d}", sign * dy)); } } } // Diagonal metric components should be positive - bout::checkPositive(g11, "g11", "RGN_NOCORNERS"); - bout::checkPositive(g22, "g22", "RGN_NOCORNERS"); - bout::checkPositive(g33, "g33", "RGN_NOCORNERS"); - if (g11.hasParallelSlices() && &g11.ynext(1) != &g11) { + bout::checkPositive(g11_, "g11", "RGN_NOCORNERS"); + bout::checkPositive(g22_, "g22", "RGN_NOCORNERS"); + bout::checkPositive(g33_, "g33", "RGN_NOCORNERS"); + if (g11_.hasParallelSlices() && &g11_.ynext(1) != &g11_) { for (int dy = 1; dy <= ystart; ++dy) { for (const auto sign : {1, -1}) { - bout::checkPositive(g11.ynext(sign * dy), "g11.ynext", + bout::checkPositive(g11_.ynext(sign * dy), "g11.ynext", fmt::format("RGN_YPAR_{:+d}", sign * dy)); - bout::checkPositive(g22.ynext(sign * dy), "g22.ynext", + bout::checkPositive(g22_.ynext(sign * dy), "g22.ynext", fmt::format("RGN_YPAR_{:+d}", sign * dy)); - bout::checkPositive(g33.ynext(sign * dy), "g33.ynext", + bout::checkPositive(g33_.ynext(sign * dy), "g33.ynext", fmt::format("RGN_YPAR_{:+d}", sign * dy)); } } } // Off-diagonal metric components should be finite - bout::checkFinite(g12, "g12", "RGN_NOCORNERS"); - bout::checkFinite(g13, "g13", "RGN_NOCORNERS"); - bout::checkFinite(g23, "g23", "RGN_NOCORNERS"); - if (g23.hasParallelSlices() && &g23.ynext(1) != &g23) { + bout::checkFinite(g12_, "g12", "RGN_NOCORNERS"); + bout::checkFinite(g13_, "g13", "RGN_NOCORNERS"); + bout::checkFinite(g23_, "g23", "RGN_NOCORNERS"); + if (g23_.hasParallelSlices() && &g23_.ynext(1) != &g23_) { for (int dy = 1; dy <= ystart; ++dy) { for (const auto sign : {1, -1}) { - bout::checkFinite(g12.ynext(sign * dy), "g12.ynext", + bout::checkFinite(g12_.ynext(sign * dy), "g12.ynext", fmt::format("RGN_YPAR_{:+d}", sign * dy)); - bout::checkFinite(g13.ynext(sign * dy), "g13.ynext", + bout::checkFinite(g13_.ynext(sign * dy), "g13.ynext", fmt::format("RGN_YPAR_{:+d}", sign * dy)); - bout::checkFinite(g23.ynext(sign * dy), "g23.ynext", + bout::checkFinite(g23_.ynext(sign * dy), "g23.ynext", fmt::format("RGN_YPAR_{:+d}", sign * dy)); } } @@ -88,21 +88,21 @@ void MetricTensor::check(int ystart) { } void MetricTensor::Allocate() { // ; TODO: Required? - g11.allocate(); - g22.allocate(); - g33.allocate(); - g12.allocate(); - g13.allocate(); - g23.allocate(); + g11_.allocate(); + g22_.allocate(); + g33_.allocate(); + g12_.allocate(); + g13_.allocate(); + g23_.allocate(); } void MetricTensor::setLocation(const CELL_LOC location) { - g11.setLocation(location); - g22.setLocation(location); - g33.setLocation(location); - g12.setLocation(location); - g13.setLocation(location); - g23.setLocation(location); + g11_.setLocation(location); + g22_.setLocation(location); + g33_.setLocation(location); + g12_.setLocation(location); + g13_.setLocation(location); + g23_.setLocation(location); } MetricTensor MetricTensor::inverse(const std::string& region) { @@ -114,14 +114,14 @@ MetricTensor MetricTensor::inverse(const std::string& region) { auto a = Matrix(3, 3); - BOUT_FOR_SERIAL(i, g11.getRegion(region)) { - a(0, 0) = g11[i]; - a(1, 1) = g22[i]; - a(2, 2) = g33[i]; + BOUT_FOR_SERIAL(i, g11_.getRegion(region)) { + a(0, 0) = g11_[i]; + a(1, 1) = g22_[i]; + a(2, 2) = g33_[i]; - a(0, 1) = a(1, 0) = g12[i]; - a(1, 2) = a(2, 1) = g23[i]; - a(0, 2) = a(2, 0) = g13[i]; + a(0, 1) = a(1, 0) = g12_[i]; + a(1, 2) = a(2, 1) = g23_[i]; + a(0, 2) = a(2, 0) = g13_[i]; if (invert3x3(a)) { const auto error_message = "\tERROR: metric tensor is singular at ({:d}, {:d})\n"; @@ -161,9 +161,9 @@ MetricTensor MetricTensor::inverse(const std::string& region) { // + g_23 * g_33))); // // output_info.write("\tMaximum error in off-diagonal inversion is {:e}\n", maxerr); - const auto mesh = g11.getMesh(); // All the components have the same mesh + const auto mesh = g11_.getMesh(); // All the components have the same mesh auto other_representation = MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23, mesh); - const auto location = g11.getLocation(); + const auto location = g11_.getLocation(); other_representation.setLocation(location); return other_representation; } @@ -173,15 +173,15 @@ void MetricTensor::map( const MetricTensor updated_metric_tensor = applyToComponents(function); - setMetricTensor(MetricTensor(updated_metric_tensor.g11, updated_metric_tensor.g22, - updated_metric_tensor.g33, updated_metric_tensor.g12, - updated_metric_tensor.g13, updated_metric_tensor.g23)); + setMetricTensor(MetricTensor(updated_metric_tensor.g11_, updated_metric_tensor.g22_, + updated_metric_tensor.g33_, updated_metric_tensor.g12_, + updated_metric_tensor.g13_, updated_metric_tensor.g23_)); } MetricTensor MetricTensor::applyToComponents( const std::function& function) const { - const auto components_in = std::vector{g11, g22, g33, g12, g13, g23}; + const auto components_in = std::vector{g11_, g22_, g33_, g12_, g13_, g23_}; FieldMetric components_out[6]; From aa42c9f5f19175cb7092b3ef5b00ff824404136d Mon Sep 17 00:00:00 2001 From: tomc271 Date: Sat, 9 Dec 2023 12:02:45 +0000 Subject: [PATCH 280/491] Clang-Tidy: Pass by value and use std::move(). --- include/bout/geometry.hxx | 2 +- include/bout/metricTensor.hxx | 4 ++-- src/mesh/geometry.cxx | 13 +++++++------ src/mesh/metricTensor.cxx | 9 +++++---- 4 files changed, 15 insertions(+), 13 deletions(-) diff --git a/include/bout/geometry.hxx b/include/bout/geometry.hxx index 22e5b20048..b41955fa35 100644 --- a/include/bout/geometry.hxx +++ b/include/bout/geometry.hxx @@ -47,7 +47,7 @@ using FieldMetric = MetricTensor::FieldMetric; class Geometry { public: - Geometry(const FieldMetric& J, const FieldMetric& Bxy, const FieldMetric& g11, + Geometry(FieldMetric J, FieldMetric Bxy, const FieldMetric& g11, const FieldMetric& g22, const FieldMetric& g33, const FieldMetric& g12, const FieldMetric& g13, const FieldMetric& g23, const FieldMetric& g_11, const FieldMetric& g_22, const FieldMetric& g_33, const FieldMetric& g_12, diff --git a/include/bout/metricTensor.hxx b/include/bout/metricTensor.hxx index 2ef093621b..d048972b49 100644 --- a/include/bout/metricTensor.hxx +++ b/include/bout/metricTensor.hxx @@ -15,8 +15,8 @@ public: using FieldMetric = Field2D; #endif - MetricTensor(const FieldMetric& g11, const FieldMetric& g22, const FieldMetric& g33, - const FieldMetric& g12, const FieldMetric& g13, const FieldMetric& g23); + MetricTensor(FieldMetric g11, FieldMetric g22, FieldMetric g33, + FieldMetric g12, FieldMetric g13, FieldMetric g23); MetricTensor(BoutReal g11, BoutReal g22, BoutReal g33, BoutReal g12, BoutReal g13, BoutReal g23, Mesh* mesh); diff --git a/src/mesh/geometry.cxx b/src/mesh/geometry.cxx index 5be361d703..b52996425c 100644 --- a/src/mesh/geometry.cxx +++ b/src/mesh/geometry.cxx @@ -4,9 +4,10 @@ * given the contravariant metric tensor terms **************************************************************************/ +#include #include "bout/geometry.hxx" -Geometry::Geometry(const FieldMetric& J, const FieldMetric& Bxy, const FieldMetric& g11, +Geometry::Geometry(FieldMetric J, FieldMetric Bxy, const FieldMetric& g11, const FieldMetric& g22, const FieldMetric& g33, const FieldMetric& g12, const FieldMetric& g13, const FieldMetric& g23, const FieldMetric& g_11, const FieldMetric& g_22, @@ -15,9 +16,9 @@ Geometry::Geometry(const FieldMetric& J, const FieldMetric& Bxy, const FieldMetr DifferentialOperators* differential_operators, Mesh* mesh) : christoffel_symbols(differential_operators), contravariantMetricTensor(g11, g22, g33, g12, g13, g23), - covariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23), this_J(J), this_Bxy(Bxy), - differential_operators(differential_operators){ - ASSERT0(differential_operators != nullptr)} + covariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23), this_J(std::move(J)), + this_Bxy(std::move(Bxy)), differential_operators(differential_operators){ASSERT0( + differential_operators != nullptr)} Geometry::Geometry(Mesh * mesh, DifferentialOperators * differential_operators) //bool force_interpolate_from_centre @@ -148,7 +149,7 @@ void Geometry::setG3(FieldMetric G3) { christoffel_symbols.setG3(G3); } void Geometry::setJ(FieldMetric J) { //TODO: Calculate J and check value is close - this_J = J; + this_J = std::move(J); } void Geometry::setJ(BoutReal value, int x, int y) { @@ -158,7 +159,7 @@ void Geometry::setJ(BoutReal value, int x, int y) { void Geometry::setBxy(FieldMetric Bxy) { //TODO: Calculate Bxy and check value is close - this_Bxy = Bxy; + this_Bxy = std::move(Bxy); } const MetricTensor& Geometry::getContravariantMetricTensor() const { diff --git a/src/mesh/metricTensor.cxx b/src/mesh/metricTensor.cxx index a7a91e8b6d..e5deeafb1d 100644 --- a/src/mesh/metricTensor.cxx +++ b/src/mesh/metricTensor.cxx @@ -1,11 +1,12 @@ +#include #include "bout/metricTensor.hxx" #include "bout/output.hxx" -MetricTensor::MetricTensor(const FieldMetric& g11, const FieldMetric& g22, - const FieldMetric& g33, const FieldMetric& g12, - const FieldMetric& g13, const FieldMetric& g23) - : g11_(g11), g22_(g22), g33_(g33), g12_(g12), g13_(g13), g23_(g23) { +MetricTensor::MetricTensor(FieldMetric g11, FieldMetric g22, FieldMetric g33, + FieldMetric g12, FieldMetric g13, FieldMetric g23) + : g11_(std::move(g11)), g22_(std::move(g22)), g33_(std::move(g33)), + g12_(std::move(g12)), g13_(std::move(g13)), g23_(std::move(g23)) { Allocate(); // Make sure metric elements are allocated // ; TODO: Required? } From e44f1b799ab761c098b704ab621173d4bc42ba36 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Mon, 11 Dec 2023 08:59:43 +0000 Subject: [PATCH 281/491] Clang-Tidy: Pass by value and use std::move(). --- include/bout/christoffel_symbols.hxx | 37 +++++++++++++--------------- src/mesh/christoffel_symbols.cxx | 25 ++++++++++--------- 2 files changed, 31 insertions(+), 31 deletions(-) diff --git a/include/bout/christoffel_symbols.hxx b/include/bout/christoffel_symbols.hxx index bc950bb594..8fcf02d1af 100644 --- a/include/bout/christoffel_symbols.hxx +++ b/include/bout/christoffel_symbols.hxx @@ -13,16 +13,13 @@ using FieldMetric = MetricTensor::FieldMetric; class ChristoffelSymbols { public: - ChristoffelSymbols(const FieldMetric& G1_11, const FieldMetric& G1_22, - const FieldMetric& G1_33, const FieldMetric& G1_12, - const FieldMetric& G1_13, const FieldMetric& G1_23, - const FieldMetric& G2_11, const FieldMetric& G2_22, - const FieldMetric& G2_33, const FieldMetric& G2_12, - const FieldMetric& G2_13, const FieldMetric& G2_23, - const FieldMetric& G3_11, const FieldMetric& G3_22, - const FieldMetric& G3_33, const FieldMetric& G3_12, - const FieldMetric& G3_13, const FieldMetric& G3_23, - const FieldMetric& G1, const FieldMetric& G2, const FieldMetric& G3, + ChristoffelSymbols(FieldMetric G1_11, FieldMetric G1_22, FieldMetric G1_33, + FieldMetric G1_12, FieldMetric G1_13, FieldMetric G1_23, + FieldMetric G2_11, FieldMetric G2_22, FieldMetric G2_33, + FieldMetric G2_12, FieldMetric G2_13, FieldMetric G2_23, + FieldMetric G3_11, FieldMetric G3_22, FieldMetric G3_33, + FieldMetric G3_12, FieldMetric G3_13, FieldMetric G3_23, + FieldMetric G1, FieldMetric G2, FieldMetric G3, DifferentialOperators* differential_operators); // ChristoffelSymbols(BoutReal g11, BoutReal g22, BoutReal g33, BoutReal g12, BoutReal g13, @@ -32,7 +29,6 @@ public: ChristoffelSymbols(Mesh* mesh, DifferentialOperators* differential_operators); - const FieldMetric& G1_11() const; const FieldMetric& G1_22() const; const FieldMetric& G1_33() const; @@ -62,13 +58,14 @@ public: void setG2(FieldMetric& G2); void setG3(FieldMetric& G3); - void setChristoffelSymbols(const FieldMetric& G1_11, const FieldMetric& G1_22, const FieldMetric& G1_33, - const FieldMetric& G1_12, const FieldMetric& G1_13, const FieldMetric& G1_23, - const FieldMetric& G2_11, const FieldMetric& G2_22, const FieldMetric& G2_33, - const FieldMetric& G2_12, const FieldMetric& G2_13, const FieldMetric& G2_23, - const FieldMetric& G3_11, const FieldMetric& G3_22, const FieldMetric& G3_33, - const FieldMetric& G3_12, const FieldMetric& G3_13, const FieldMetric& G3_23, - const FieldMetric& G1, const FieldMetric& G2, const FieldMetric& G3); + void setChristoffelSymbols( + const FieldMetric& G1_11, const FieldMetric& G1_22, const FieldMetric& G1_33, + const FieldMetric& G1_12, const FieldMetric& G1_13, const FieldMetric& G1_23, + const FieldMetric& G2_11, const FieldMetric& G2_22, const FieldMetric& G2_33, + const FieldMetric& G2_12, const FieldMetric& G2_13, const FieldMetric& G2_23, + const FieldMetric& G3_11, const FieldMetric& G3_22, const FieldMetric& G3_33, + const FieldMetric& G3_12, const FieldMetric& G3_13, const FieldMetric& G3_23, + const FieldMetric& G1, const FieldMetric& G2, const FieldMetric& G3); // void Allocate(); // @@ -95,8 +92,8 @@ private: DifferentialOperators* differential_operators; - ChristoffelSymbols - applyToComponents(const std::function& function) const; + ChristoffelSymbols applyToComponents( + const std::function& function) const; }; #endif //BOUT_CHRISTOFFELSYMBOLS_HXX diff --git a/src/mesh/christoffel_symbols.cxx b/src/mesh/christoffel_symbols.cxx index ce3692f253..05636101c9 100644 --- a/src/mesh/christoffel_symbols.cxx +++ b/src/mesh/christoffel_symbols.cxx @@ -1,19 +1,22 @@ +#include + #include "bout/christoffel_symbols.hxx" ChristoffelSymbols::ChristoffelSymbols( - const FieldMetric& G1_11, const FieldMetric& G1_22, const FieldMetric& G1_33, - const FieldMetric& G1_12, const FieldMetric& G1_13, const FieldMetric& G1_23, - const FieldMetric& G2_11, const FieldMetric& G2_22, const FieldMetric& G2_33, - const FieldMetric& G2_12, const FieldMetric& G2_13, const FieldMetric& G2_23, - const FieldMetric& G3_11, const FieldMetric& G3_22, const FieldMetric& G3_33, - const FieldMetric& G3_12, const FieldMetric& G3_13, const FieldMetric& G3_23, - const FieldMetric& G1, const FieldMetric& G2, const FieldMetric& G3, + FieldMetric G1_11, FieldMetric G1_22, FieldMetric G1_33, FieldMetric G1_12, + FieldMetric G1_13, FieldMetric G1_23, FieldMetric G2_11, FieldMetric G2_22, + FieldMetric G2_33, FieldMetric G2_12, FieldMetric G2_13, FieldMetric G2_23, + FieldMetric G3_11, FieldMetric G3_22, FieldMetric G3_33, FieldMetric G3_12, + FieldMetric G3_13, FieldMetric G3_23, FieldMetric G1, FieldMetric G2, FieldMetric G3, DifferentialOperators* differential_operators) - : G1_11_(G1_11), G1_22_(G1_22), G1_33_(G1_33), G1_12_(G1_12), G1_13_(G1_13), - G1_23_(G1_23), G2_11_(G2_11), G2_22_(G2_22), G2_33_(G2_33), G2_12_(G2_12), - G2_13_(G2_13), G2_23_(G2_23), G3_11_(G3_11), G3_22_(G3_22), G3_33_(G3_33), - G3_12_(G3_12), G3_13_(G3_13), G3_23_(G3_23), G1_(G1), G2_(G2), G3_(G3), + : G1_11_(std::move(G1_11)), G1_22_(std::move(G1_22)), G1_33_(std::move(G1_33)), + G1_12_(std::move(G1_12)), G1_13_(std::move(G1_13)), G1_23_(std::move(G1_23)), + G2_11_(std::move(G2_11)), G2_22_(std::move(G2_22)), G2_33_(std::move(G2_33)), + G2_12_(std::move(G2_12)), G2_13_(std::move(G2_13)), G2_23_(std::move(G2_23)), + G3_11_(std::move(G3_11)), G3_22_(std::move(G3_22)), G3_33_(std::move(G3_33)), + G3_12_(std::move(G3_12)), G3_13_(std::move(G3_13)), G3_23_(std::move(G3_23)), + G1_(std::move(G1)), G2_(std::move(G2)), G3_(std::move(G3)), differential_operators(differential_operators){ ASSERT0(differential_operators != nullptr)} From 721479de75c0d11e0f2962a1f9e3535227c10ab0 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Mon, 11 Dec 2023 09:01:14 +0000 Subject: [PATCH 282/491] Clang-Tidy: Single-argument constructors must be marked explicit to avoid unintentional implicit conversions. --- include/bout/christoffel_symbols.hxx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/bout/christoffel_symbols.hxx b/include/bout/christoffel_symbols.hxx index 8fcf02d1af..476554a2fa 100644 --- a/include/bout/christoffel_symbols.hxx +++ b/include/bout/christoffel_symbols.hxx @@ -25,7 +25,7 @@ public: // ChristoffelSymbols(BoutReal g11, BoutReal g22, BoutReal g33, BoutReal g12, BoutReal g13, // BoutReal g23, Mesh* mesh); - ChristoffelSymbols(DifferentialOperators* differential_operators); + explicit ChristoffelSymbols(DifferentialOperators* differential_operators); ChristoffelSymbols(Mesh* mesh, DifferentialOperators* differential_operators); From 0d5c2503ebbde6bf93de299fd3daf193aa11003a Mon Sep 17 00:00:00 2001 From: tomc271 Date: Mon, 11 Dec 2023 09:03:21 +0000 Subject: [PATCH 283/491] Remove redundant 'mesh' parameter from Geometry constructor. --- include/bout/geometry.hxx | 11 +++++------ src/mesh/coordinates.cxx | 2 +- src/mesh/geometry.cxx | 4 ++-- 3 files changed, 8 insertions(+), 9 deletions(-) diff --git a/include/bout/geometry.hxx b/include/bout/geometry.hxx index b41955fa35..6b53aa8224 100644 --- a/include/bout/geometry.hxx +++ b/include/bout/geometry.hxx @@ -47,12 +47,11 @@ using FieldMetric = MetricTensor::FieldMetric; class Geometry { public: - Geometry(FieldMetric J, FieldMetric Bxy, const FieldMetric& g11, - const FieldMetric& g22, const FieldMetric& g33, const FieldMetric& g12, - const FieldMetric& g13, const FieldMetric& g23, const FieldMetric& g_11, - const FieldMetric& g_22, const FieldMetric& g_33, const FieldMetric& g_12, - const FieldMetric& g_13, const FieldMetric& g_23, - DifferentialOperators* differential_operators, Mesh* mesh); + Geometry(FieldMetric J, FieldMetric Bxy, const FieldMetric& g11, const FieldMetric& g22, + const FieldMetric& g33, const FieldMetric& g12, const FieldMetric& g13, + const FieldMetric& g23, const FieldMetric& g_11, const FieldMetric& g_22, + const FieldMetric& g_33, const FieldMetric& g_12, const FieldMetric& g_13, + const FieldMetric& g_23, DifferentialOperators* differential_operators); Geometry(Mesh* mesh, DifferentialOperators* differential_operators); diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 737b0a3f74..facf3775e8 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -292,7 +292,7 @@ Coordinates::Coordinates(Mesh* mesh, FieldMetric dx, FieldMetric dy, FieldMetric : nz(mesh->LocalNz), localmesh(mesh), location(CELL_CENTRE), differential_operators(mesh->getDifferentialOperators()), geometry(Geometry(J, Bxy, g11, g22, g33, g12, g13, g23, g_11, g_22, g_33, g_12, - g_13, g_23, differential_operators, mesh)), + g_13, g_23, differential_operators)), dx_(std::move(dx)), dy_(std::move(dy)), dz_(std::move(dz)), ShiftTorsion_(std::move(ShiftTorsion)), IntShiftTorsion_(std::move(IntShiftTorsion)) {} diff --git a/src/mesh/geometry.cxx b/src/mesh/geometry.cxx index b52996425c..099142d353 100644 --- a/src/mesh/geometry.cxx +++ b/src/mesh/geometry.cxx @@ -4,8 +4,8 @@ * given the contravariant metric tensor terms **************************************************************************/ -#include #include "bout/geometry.hxx" +#include Geometry::Geometry(FieldMetric J, FieldMetric Bxy, const FieldMetric& g11, const FieldMetric& g22, const FieldMetric& g33, const FieldMetric& g12, @@ -13,7 +13,7 @@ Geometry::Geometry(FieldMetric J, FieldMetric Bxy, const FieldMetric& g11, const FieldMetric& g_11, const FieldMetric& g_22, const FieldMetric& g_33, const FieldMetric& g_12, const FieldMetric& g_13, const FieldMetric& g_23, - DifferentialOperators* differential_operators, Mesh* mesh) + DifferentialOperators* differential_operators) : christoffel_symbols(differential_operators), contravariantMetricTensor(g11, g22, g33, g12, g13, g23), covariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23), this_J(std::move(J)), From 046a7add7806e6f86dac195460bea946a3c0dad6 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Mon, 11 Dec 2023 09:04:28 +0000 Subject: [PATCH 284/491] Formatting. --- include/bout/metricTensor.hxx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/bout/metricTensor.hxx b/include/bout/metricTensor.hxx index d048972b49..4dfb1e3d0e 100644 --- a/include/bout/metricTensor.hxx +++ b/include/bout/metricTensor.hxx @@ -15,8 +15,8 @@ public: using FieldMetric = Field2D; #endif - MetricTensor(FieldMetric g11, FieldMetric g22, FieldMetric g33, - FieldMetric g12, FieldMetric g13, FieldMetric g23); + MetricTensor(FieldMetric g11, FieldMetric g22, FieldMetric g33, FieldMetric g12, + FieldMetric g13, FieldMetric g23); MetricTensor(BoutReal g11, BoutReal g22, BoutReal g33, BoutReal g12, BoutReal g13, BoutReal g23, Mesh* mesh); From f2dc950c5bfdd4163e8dad93c26de351d501e015 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Mon, 11 Dec 2023 10:17:52 +0000 Subject: [PATCH 285/491] Remove Geometry class, as now redundant. --- CMakeLists.txt | 4 +- include/bout/coordinates.hxx | 87 ++++++---- include/bout/geometry.hxx | 158 ------------------- src/mesh/coordinates.cxx | 298 ++++++++++++++++++++++------------- src/mesh/geometry.cxx | 186 ---------------------- 5 files changed, 245 insertions(+), 488 deletions(-) delete mode 100644 include/bout/geometry.hxx delete mode 100644 src/mesh/geometry.cxx diff --git a/CMakeLists.txt b/CMakeLists.txt index b79fc6b733..925d637e66 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -354,9 +354,7 @@ set(BOUT_SOURCES ./src/sys/utils.cxx ./include/bout/metricTensor.hxx ./src/mesh/metricTensor.cxx - ./include/bout/geometry.hxx - ./src/mesh/geometry.cxx - ./include/bout/differential_operators.hxx + ./include/bout/differential_operators.hxx ./src/mesh/differential_operators.cxx ./src/mesh/christoffel_symbols.cxx ${CMAKE_CURRENT_BINARY_DIR}/include/bout/revision.hxx diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index d6918591eb..3853f234f7 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -33,7 +33,8 @@ #ifndef BOUT_COORDINATES_H #define BOUT_COORDINATES_H -#include "bout/geometry.hxx" +#include "christoffel_symbols.hxx" +#include "differential_operators.hxx" #include "bout/metricTensor.hxx" #include "bout/paralleltransform.hxx" @@ -63,12 +64,12 @@ public: /// A constructor useful for testing purposes. To use it, inherit /// from Coordinates. If \p calculate_geometry is true (default), /// calculate the non-uniform variables, Christoffel symbols - Coordinates(Mesh* mesh, FieldMetric dx, FieldMetric dy, FieldMetric dz, - const FieldMetric& J, const FieldMetric& Bxy, const FieldMetric& g11, - const FieldMetric& g22, const FieldMetric& g33, const FieldMetric& g12, - const FieldMetric& g13, const FieldMetric& g23, const FieldMetric& g_11, - const FieldMetric& g_22, const FieldMetric& g_33, const FieldMetric& g_12, - const FieldMetric& g_13, const FieldMetric& g_23, FieldMetric ShiftTorsion, + Coordinates(Mesh* mesh, FieldMetric dx, FieldMetric dy, FieldMetric dz, FieldMetric J, + FieldMetric Bxy, const FieldMetric& g11, const FieldMetric& g22, + const FieldMetric& g33, const FieldMetric& g12, const FieldMetric& g13, + const FieldMetric& g23, const FieldMetric& g_11, const FieldMetric& g_22, + const FieldMetric& g_33, const FieldMetric& g_12, const FieldMetric& g_13, + const FieldMetric& g_23, FieldMetric ShiftTorsion, FieldMetric IntShiftTorsion); /// Add variables to \p output_options, for post-processing @@ -117,6 +118,9 @@ public: const FieldMetric& g13() const; const FieldMetric& g23() const; + const MetricTensor& getContravariantMetricTensor() const; + // const MetricTensor& getCovariantMetricTensor() const; + void setContravariantMetricTensor(const MetricTensor& metric_tensor, const std::string& region = "RGN_ALL", bool recalculate_staggered = true, @@ -145,8 +149,6 @@ public: const FieldMetric& IntShiftTorsion() const; void setIntShiftTorsion(FieldMetric IntShiftTorsion); - const MetricTensor& getContravariantMetricTensor() const; - /// Calculate differential geometry quantities from the metric tensor int communicateAndCheckMeshSpacing() const; @@ -249,26 +251,27 @@ public: // solver Field2D Laplace_perpXY(const Field2D& A, const Field2D& f) const; - const FieldMetric& G1_11(); - const FieldMetric& G1_22(); - const FieldMetric& G1_33(); - const FieldMetric& G1_12(); - const FieldMetric& G1_13(); - const FieldMetric& G1_23(); - - const FieldMetric& G2_11(); - const FieldMetric& G2_22(); - const FieldMetric& G2_33(); - const FieldMetric& G2_12(); - const FieldMetric& G2_13(); - const FieldMetric& G2_23(); - - const FieldMetric& G3_11(); - const FieldMetric& G3_22(); - const FieldMetric& G3_33(); - const FieldMetric& G3_12(); - const FieldMetric& G3_13(); - const FieldMetric& G3_23(); + /// Christoffel symbol of the second kind (connection coefficients) + const FieldMetric& G1_11() const; + const FieldMetric& G1_22() const; + const FieldMetric& G1_33() const; + const FieldMetric& G1_12() const; + const FieldMetric& G1_13() const; + const FieldMetric& G1_23() const; + + const FieldMetric& G2_11() const; + const FieldMetric& G2_22() const; + const FieldMetric& G2_33() const; + const FieldMetric& G2_12() const; + const FieldMetric& G2_13() const; + const FieldMetric& G2_23() const; + + const FieldMetric& G3_11() const; + const FieldMetric& G3_22() const; + const FieldMetric& G3_33() const; + const FieldMetric& G3_12() const; + const FieldMetric& G3_13() const; + const FieldMetric& G3_23() const; const FieldMetric& G1() const; const FieldMetric& G2() const; @@ -290,8 +293,8 @@ private: int nz; // Size of mesh in Z. This is mesh->ngz-1 Mesh* localmesh; CELL_LOC location; + DifferentialOperators* differential_operators; - Geometry geometry; /// True if corrections for non-uniform mesh spacing should be included in operators bool non_uniform_{}; @@ -318,6 +321,25 @@ private: mutable std::map> Grad2_par2_DDY_invSgCache; mutable std::unique_ptr invSgCache{nullptr}; + MetricTensor contravariantMetricTensor; + MetricTensor covariantMetricTensor; + + /// Christoffel symbol of the second kind (connection coefficients) + ChristoffelSymbols christoffel_symbols; + + void applyToContravariantMetricTensor( + const std::function& function); + + void applyToCovariantMetricTensor( + const std::function& function); + + void applyToChristoffelSymbols( + const std::function& function); + + FieldMetric J_; + + FieldMetric Bxy_; ///< Magnitude of B = nabla z times nabla x + void invalidateAndRecalculateCachedVariables(); /// Set the parallel (y) transform from the options file. @@ -342,9 +364,12 @@ private: bool extrapolate_x = false, bool extrapolate_y = false, bool no_extra_interpolate = false, ParallelTransform* pParallelTransform = nullptr); - void communicateChristoffelSymbolTerms(); + void communicateChristoffelSymbolTerms() const; void calculateCommunicateAndExtrapolateChristoffelSymbols(); + FieldMetric recalculateJacobian(); + FieldMetric recalculateBxy(); + /// Non-uniform meshes. Need to use DDX, DDY void correctionForNonUniformMeshes(bool force_interpolate_from_centre); diff --git a/include/bout/geometry.hxx b/include/bout/geometry.hxx deleted file mode 100644 index 6b53aa8224..0000000000 --- a/include/bout/geometry.hxx +++ /dev/null @@ -1,158 +0,0 @@ -/************************************************************************** - * Describes coordinate geometry - * - * ChangeLog - * ========= - * - * 2021-11-23 Tom Chapman - * * Extracted from Coordinates - * - * - ************************************************************************** - * Copyright 2014 B.D.Dudson - * - * Contact: Ben Dudson, bd512@york.ac.uk - * - * This file is part of BOUT++. - * - * BOUT++ is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * BOUT++ is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with BOUT++. If not, see . - * - **************************************************************************/ - -#ifndef __GEOMETRY_H__ -#define __GEOMETRY_H__ - -#include "christoffel_symbols.hxx" -#include "differential_operators.hxx" -#include "metricTensor.hxx" - -using FieldMetric = MetricTensor::FieldMetric; - -/*! - * Represents the geometry a coordinate system, and associated operators - * - * This is a container for a collection of metric tensor components - */ -class Geometry { - -public: - Geometry(FieldMetric J, FieldMetric Bxy, const FieldMetric& g11, const FieldMetric& g22, - const FieldMetric& g33, const FieldMetric& g12, const FieldMetric& g13, - const FieldMetric& g23, const FieldMetric& g_11, const FieldMetric& g_22, - const FieldMetric& g_33, const FieldMetric& g_12, const FieldMetric& g_13, - const FieldMetric& g_23, DifferentialOperators* differential_operators); - - Geometry(Mesh* mesh, DifferentialOperators* differential_operators); - - /// Covariant metric tensor - const FieldMetric& g_11() const; - const FieldMetric& g_22() const; - const FieldMetric& g_33() const; - const FieldMetric& g_12() const; - const FieldMetric& g_13() const; - const FieldMetric& g_23() const; - - /// Contravariant metric tensor (g^{ij}) - const FieldMetric& g11() const; - const FieldMetric& g22() const; - const FieldMetric& g33() const; - const FieldMetric& g12() const; - const FieldMetric& g13() const; - const FieldMetric& g23() const; - - const MetricTensor& getContravariantMetricTensor() const; - const MetricTensor& getCovariantMetricTensor() const; - - /// Christoffel symbol of the second kind (connection coefficients) - const FieldMetric& G1_11() const; - const FieldMetric& G1_22() const; - const FieldMetric& G1_33() const; - const FieldMetric& G1_12() const; - const FieldMetric& G1_13() const; - const FieldMetric& G1_23() const; - - const FieldMetric& G2_11() const; - const FieldMetric& G2_22() const; - const FieldMetric& G2_33() const; - const FieldMetric& G2_12() const; - const FieldMetric& G2_13() const; - const FieldMetric& G2_23() const; - - const FieldMetric& G3_11() const; - const FieldMetric& G3_22() const; - const FieldMetric& G3_33() const; - const FieldMetric& G3_12() const; - const FieldMetric& G3_13() const; - const FieldMetric& G3_23() const; - - const FieldMetric& G1() const; - const FieldMetric& G2() const; - const FieldMetric& G3() const; - - void setG1(FieldMetric G1); - void setG2(FieldMetric G2); - void setG3(FieldMetric G3); - - ///< Coordinate system Jacobian, so volume of cell is J*dx*dy*dz - const FieldMetric& J() const; - - ///< Magnitude of B = nabla z times nabla x - const FieldMetric& Bxy() const; - - void setContravariantMetricTensor(const MetricTensor& metric_tensor, - const std::string& region = "RGN_ALL"); - - void setCovariantMetricTensor(const MetricTensor& metric_tensor, - const std::string& region = "RGN_ALL"); - - void setJ(FieldMetric J); - void setJ(BoutReal value, int x, int y); - - void setBxy(FieldMetric Bxy); - - /// Calculate Christoffel symbol terms - void CalculateChristoffelSymbols(const FieldMetric& dx, const FieldMetric& dy); - - // check that covariant tensors are positive (if expected) and finite (always) - void checkCovariant(int ystart); - - // check that contravariant tensors are positive (if expected) and finite (always) - void checkContravariant(int ystart); - - FieldMetric recalculateJacobian(); - FieldMetric recalculateBxy(); - - void applyToContravariantMetricTensor( - const std::function& function); - - void applyToCovariantMetricTensor( - const std::function& function); - - void - applyToChristoffelSymbols(const std::function& function); - -private: - /// Christoffel symbol of the second kind (connection coefficients) - ChristoffelSymbols christoffel_symbols; - - MetricTensor contravariantMetricTensor; - MetricTensor covariantMetricTensor; - - FieldMetric this_J; - FieldMetric this_Bxy; ///< Magnitude of B = nabla z times nabla x - - DifferentialOperators* differential_operators; -}; - -#endif // __GEOMETRY_H__ diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index facf3775e8..d441c6c7a7 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -16,7 +16,6 @@ #include "parallel/fci.hxx" #include "parallel/shiftedmetricinterp.hxx" -#include "bout/geometry.hxx" #include // use anonymous namespace so this utility function is not available outside this file @@ -281,29 +280,35 @@ Coordinates::FieldMetric Coordinates::getUnalignedAtLocationAndFillGuards( } Coordinates::Coordinates(Mesh* mesh, FieldMetric dx, FieldMetric dy, FieldMetric dz, - const FieldMetric& J, const FieldMetric& Bxy, - const FieldMetric& g11, const FieldMetric& g22, - const FieldMetric& g33, const FieldMetric& g12, - const FieldMetric& g13, const FieldMetric& g23, - const FieldMetric& g_11, const FieldMetric& g_22, - const FieldMetric& g_33, const FieldMetric& g_12, - const FieldMetric& g_13, const FieldMetric& g_23, - FieldMetric ShiftTorsion, FieldMetric IntShiftTorsion) + FieldMetric J, FieldMetric Bxy, const FieldMetric& g11, + const FieldMetric& g22, const FieldMetric& g33, + const FieldMetric& g12, const FieldMetric& g13, + const FieldMetric& g23, const FieldMetric& g_11, + const FieldMetric& g_22, const FieldMetric& g_33, + const FieldMetric& g_12, const FieldMetric& g_13, + const FieldMetric& g_23, FieldMetric ShiftTorsion, + FieldMetric IntShiftTorsion) : nz(mesh->LocalNz), localmesh(mesh), location(CELL_CENTRE), - differential_operators(mesh->getDifferentialOperators()), - geometry(Geometry(J, Bxy, g11, g22, g33, g12, g13, g23, g_11, g_22, g_33, g_12, - g_13, g_23, differential_operators)), - dx_(std::move(dx)), dy_(std::move(dy)), dz_(std::move(dz)), - ShiftTorsion_(std::move(ShiftTorsion)), - IntShiftTorsion_(std::move(IntShiftTorsion)) {} - -Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, - const Coordinates* coords_in, bool force_interpolate_from_centre) + differential_operators(mesh->getDifferentialOperators()), dx_(std::move(dx)), + dy_(std::move(dy)), dz_(std::move(dz)), ShiftTorsion_(std::move(ShiftTorsion)), + IntShiftTorsion_(std::move(IntShiftTorsion)), + contravariantMetricTensor(g11, g22, g33, g12, g13, g23), + covariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23), + christoffel_symbols(mesh, differential_operators), J_(std::move(J)), + Bxy_(std::move(Bxy)){ASSERT0(differential_operators != nullptr)} + + Coordinates::Coordinates(Mesh * mesh, Options * options, const CELL_LOC loc, + const Coordinates* coords_in, + bool force_interpolate_from_centre) : localmesh(mesh), location(loc), - differential_operators(mesh->getDifferentialOperators()), - geometry(Geometry(mesh, differential_operators)), dx_(1., mesh), dy_(1., mesh), - dz_(1., mesh), d1_dx_(mesh), d1_dy_(mesh), d1_dz_(mesh), ShiftTorsion_(mesh), - IntShiftTorsion_(mesh) { + differential_operators(mesh->getDifferentialOperators()), dx_(1., mesh), + dy_(1., mesh), dz_(1., mesh), d1_dx_(mesh), d1_dy_(mesh), d1_dz_(mesh), + ShiftTorsion_(mesh), IntShiftTorsion_(mesh), + contravariantMetricTensor(1., 1., 1., 0, 0, 0, mesh), + // Identity metric tensor + covariantMetricTensor(1., 1., 1., 0, 0, 0, mesh), + christoffel_symbols(mesh, differential_operators), J_(1., mesh), Bxy_(1., mesh) { + ASSERT0(differential_operators != nullptr) if (options == nullptr) { options = Options::getRoot()->getSection("mesh"); @@ -334,17 +339,17 @@ void Coordinates::interpolateFieldsFromOtherCoordinates(const Mesh* mesh, const auto region = std::basic_string("RGN_NOBNDRY"); setContravariantMetricTensor(coords_in->getContravariantMetricTensor(), region); - geometry.applyToContravariantMetricTensor(interpolateAndExtrapolate_function); - geometry.applyToCovariantMetricTensor(interpolateAndExtrapolate_function); + applyToContravariantMetricTensor(interpolateAndExtrapolate_function); + applyToCovariantMetricTensor(interpolateAndExtrapolate_function); // Check input metrics checkContravariant(); checkCovariant(); - geometry.setJ(localmesh->interpolateAndExtrapolate(coords_in->J(), location, true, true, - false, transform.get())); - geometry.setBxy(localmesh->interpolateAndExtrapolate(coords_in->Bxy(), location, true, - true, false, transform.get())); + setJ(localmesh->interpolateAndExtrapolate(coords_in->J(), location, true, true, false, + transform.get())); + setBxy(localmesh->interpolateAndExtrapolate(coords_in->Bxy(), location, true, true, + false, transform.get())); bout::checkFinite(J(), "The Jacobian", "RGN_NOCORNERS"); bout::checkPositive(J(), "The Jacobian", "RGN_NOCORNERS"); @@ -462,7 +467,7 @@ void Coordinates::setBoundaryCells(Mesh* mesh, Options* options, extrapolate_x, extrapolate_y, false, transform.get()); - geometry.setContravariantMetricTensor(MetricTensor(g11, g22, g33, g12, g13, g23)); + setContravariantMetricTensor(MetricTensor(g11, g22, g33, g12, g13, g23)); // Check input metrics checkContravariant(); @@ -487,7 +492,7 @@ void Coordinates::setBoundaryCells(Mesh* mesh, Options* options, g_12 = getAtLocOrUnaligned(mesh, "g_12", 0.0, suffix, location); g_13 = getAtLocOrUnaligned(mesh, "g_13", 0.0, suffix, location); g_23 = getAtLocOrUnaligned(mesh, "g_23", 0.0, suffix, location); - geometry.setCovariantMetricTensor(MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + setCovariantMetricTensor(MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); output_warn.write("\tWARNING! Covariant components of metric tensor set manually. " "Contravariant components NOT recalculated\n"); @@ -507,7 +512,7 @@ void Coordinates::setBoundaryCells(Mesh* mesh, Options* options, return localmesh->interpolateAndExtrapolate( component, location, extrapolate_x, extrapolate_y, false, transform.get()); }; - geometry.applyToCovariantMetricTensor(interpolateAndExtrapolate_function); + applyToCovariantMetricTensor(interpolateAndExtrapolate_function); // Check covariant metrics checkCovariant(); @@ -751,17 +756,18 @@ void Coordinates::correctionForNonUniformMeshes(bool force_interpolate_from_cent void Coordinates::calculateCommunicateAndExtrapolateChristoffelSymbols() { - geometry.CalculateChristoffelSymbols(dx(), dy()); + christoffel_symbols.CalculateChristoffelSymbols(dx(), dy(), contravariantMetricTensor, + covariantMetricTensor); auto tmp = J() * g12(); communicate(tmp); - geometry.setG1((DDX(J() * g11()) + DDY(tmp) + DDZ(J() * g13())) / J()); + setG1((DDX(J() * g11()) + DDY(tmp) + DDZ(J() * g13())) / J()); tmp = J() * g22(); communicate(tmp); - geometry.setG2((DDX(J() * g12()) + DDY(tmp) + DDZ(J() * g23())) / J()); + setG2((DDX(J() * g12()) + DDY(tmp) + DDZ(J() * g23())) / J()); tmp = J() * g23(); communicate(tmp); - geometry.setG3((DDX(J() * g13()) + DDY(tmp) + DDZ(J() * g33())) / J()); + setG3((DDX(J() * g13()) + DDY(tmp) + DDZ(J() * g33())) / J()); communicateChristoffelSymbolTerms(); @@ -782,7 +788,32 @@ void Coordinates::calculateCommunicateAndExtrapolateChristoffelSymbols() { false, transform.get()); }; - geometry.applyToChristoffelSymbols(interpolateAndExtrapolate_function); + applyToChristoffelSymbols(interpolateAndExtrapolate_function); +} + +MetricTensor::FieldMetric Coordinates::recalculateJacobian() { + + // calculate Jacobian using g^-1 = det[g^ij], J = sqrt(g) + auto g = contravariantMetricTensor.g11() * contravariantMetricTensor.g22() + * contravariantMetricTensor.g33() + + 2.0 * contravariantMetricTensor.g12() * contravariantMetricTensor.g13() + * contravariantMetricTensor.g23() + - contravariantMetricTensor.g11() * contravariantMetricTensor.g23() + * contravariantMetricTensor.g23() + - contravariantMetricTensor.g22() * contravariantMetricTensor.g13() + * contravariantMetricTensor.g13() + - contravariantMetricTensor.g33() * contravariantMetricTensor.g12() + * contravariantMetricTensor.g12(); + + // Check that g is positive + bout::checkPositive(g, "The determinant of g^ij", "RGN_NOBNDRY"); + + return 1. / sqrt(g); +} + +MetricTensor::FieldMetric Coordinates::recalculateBxy() { + + return sqrt(covariantMetricTensor.g22()) / J_; } void Coordinates::jacobian() { @@ -793,16 +824,16 @@ void Coordinates::jacobian() { try { - const auto jacobian = geometry.recalculateJacobian(); + const auto jacobian = recalculateJacobian(); // More robust to extrapolate derived quantities directly, rather than // deriving from extrapolated covariant metric components - geometry.setJ(localmesh->interpolateAndExtrapolate(jacobian, location, extrapolate_x, - extrapolate_y, false)); + setJ(localmesh->interpolateAndExtrapolate(jacobian, location, extrapolate_x, + extrapolate_y, false)); - const auto Bxy = geometry.recalculateBxy(); + const auto Bxy = recalculateBxy(); // CELL_LOC location, ParallelTransform* pParallelTransform - geometry.setBxy(localmesh->interpolateAndExtrapolate( - Bxy, location, extrapolate_x, extrapolate_y, false, transform.get())); + setBxy(localmesh->interpolateAndExtrapolate(Bxy, location, extrapolate_x, + extrapolate_y, false, transform.get())); } catch (BoutException&) { output_error.write("\tError in jacobian call\n"); throw; @@ -1007,7 +1038,7 @@ Field2D Coordinates::Div_par(const Field2D& f, CELL_LOC outloc, TRACE("Coordinates::Div_par( Field2D )"); ASSERT1(location == outloc || outloc == CELL_DEFAULT) - return differential_operators->Div_par(f, geometry.Bxy(), dy(), outloc, method); + return differential_operators->Div_par(f, Bxy(), dy(), outloc, method); } Field3D Coordinates::Div_par(const Field3D& f, CELL_LOC outloc, @@ -1022,7 +1053,7 @@ Field3D Coordinates::Div_par(const Field3D& f, CELL_LOC outloc, if (!f.hasParallelSlices()) { // No yup/ydown fields. The Grad_par operator will // shift to field aligned coordinates - return differential_operators->Div_par(f, dy(), geometry.Bxy(), outloc, method); + return differential_operators->Div_par(f, dy(), Bxy(), outloc, method); } // Need to modify yup and ydown fields @@ -1356,9 +1387,11 @@ Coordinates::Grad2_par2_DDY_invSg(CELL_LOC outloc, const std::string& method) co return *Grad2_par2_DDY_invSgCache[method]; } -void Coordinates::checkCovariant() { return geometry.checkCovariant(localmesh->ystart); } +void Coordinates::checkCovariant() { covariantMetricTensor.check(localmesh->ystart); } -void Coordinates::checkContravariant() { geometry.checkContravariant(localmesh->ystart); } +void Coordinates::checkContravariant() { + contravariantMetricTensor.check(localmesh->ystart); +} bool Coordinates::non_uniform() const { return non_uniform_; } void Coordinates::setNon_uniform(bool non_uniform) { non_uniform_ = non_uniform; } @@ -1389,89 +1422,134 @@ void Coordinates::setIntShiftTorsion(FieldMetric IntShiftTorsion) { IntShiftTorsion_ = std::move(IntShiftTorsion); } -void Coordinates::setContravariantMetricTensor(const MetricTensor& metric_tensor, - const std::string& region, - bool recalculate_staggered, - bool force_interpolate_from_centre) { - geometry.setContravariantMetricTensor(metric_tensor, region); - recalculateAndReset(recalculate_staggered, force_interpolate_from_centre); +const MetricTensor::FieldMetric& Coordinates::g_11() const { + return covariantMetricTensor.g11(); +} +const MetricTensor::FieldMetric& Coordinates::g_22() const { + return covariantMetricTensor.g22(); +} +const MetricTensor::FieldMetric& Coordinates::g_33() const { + return covariantMetricTensor.g33(); +} +const MetricTensor::FieldMetric& Coordinates::g_12() const { + return covariantMetricTensor.g12(); +} +const MetricTensor::FieldMetric& Coordinates::g_13() const { + return covariantMetricTensor.g13(); +} +const MetricTensor::FieldMetric& Coordinates::g_23() const { + return covariantMetricTensor.g23(); } -void Coordinates::setCovariantMetricTensor(const MetricTensor& metric_tensor, - const std::string& region, - bool recalculate_staggered, - bool force_interpolate_from_centre) { - geometry.setCovariantMetricTensor(metric_tensor, region); - recalculateAndReset(recalculate_staggered, force_interpolate_from_centre); +const MetricTensor::FieldMetric& Coordinates::g11() const { + return contravariantMetricTensor.g11(); +} +const MetricTensor::FieldMetric& Coordinates::g22() const { + return contravariantMetricTensor.g22(); +} +const MetricTensor::FieldMetric& Coordinates::g33() const { + return contravariantMetricTensor.g33(); +} +const MetricTensor::FieldMetric& Coordinates::g12() const { + return contravariantMetricTensor.g12(); +} +const MetricTensor::FieldMetric& Coordinates::g13() const { + return contravariantMetricTensor.g13(); +} +const MetricTensor::FieldMetric& Coordinates::g23() const { + return contravariantMetricTensor.g23(); } -const FieldMetric& Coordinates::g_11() const { return geometry.g_11(); } -const FieldMetric& Coordinates::g_22() const { return geometry.g_22(); } -const FieldMetric& Coordinates::g_33() const { return geometry.g_33(); } -const FieldMetric& Coordinates::g_12() const { return geometry.g_12(); } -const FieldMetric& Coordinates::g_13() const { return geometry.g_13(); } -const FieldMetric& Coordinates::g_23() const { return geometry.g_23(); } - -const FieldMetric& Coordinates::g11() const { return geometry.g11(); } -const FieldMetric& Coordinates::g22() const { return geometry.g22(); } -const FieldMetric& Coordinates::g33() const { return geometry.g33(); } -const FieldMetric& Coordinates::g12() const { return geometry.g12(); } -const FieldMetric& Coordinates::g13() const { return geometry.g13(); } -const FieldMetric& Coordinates::g23() const { return geometry.g23(); } - -const FieldMetric& Coordinates::J() const { return geometry.J(); } - -const FieldMetric& Coordinates::Bxy() const { return geometry.Bxy(); } - -const FieldMetric& Coordinates::G1_11() { return geometry.G1_11(); } -const FieldMetric& Coordinates::G1_22() { return geometry.G1_22(); } -const FieldMetric& Coordinates::G1_33() { return geometry.G1_33(); } -const FieldMetric& Coordinates::G1_12() { return geometry.G1_12(); } -const FieldMetric& Coordinates::G1_13() { return geometry.G1_13(); } -const FieldMetric& Coordinates::G1_23() { return geometry.G1_23(); } - -const FieldMetric& Coordinates::G2_11() { return geometry.G2_11(); } -const FieldMetric& Coordinates::G2_22() { return geometry.G2_22(); } -const FieldMetric& Coordinates::G2_33() { return geometry.G2_33(); } -const FieldMetric& Coordinates::G2_12() { return geometry.G2_12(); } -const FieldMetric& Coordinates::G2_13() { return geometry.G2_13(); } -const FieldMetric& Coordinates::G2_23() { return geometry.G2_23(); } - -const FieldMetric& Coordinates::G3_11() { return geometry.G3_11(); } -const FieldMetric& Coordinates::G3_22() { return geometry.G3_22(); } -const FieldMetric& Coordinates::G3_33() { return geometry.G3_33(); } -const FieldMetric& Coordinates::G3_12() { return geometry.G3_12(); } -const FieldMetric& Coordinates::G3_13() { return geometry.G3_13(); } -const FieldMetric& Coordinates::G3_23() { return geometry.G3_23(); } - -const FieldMetric& Coordinates::G1() const { return geometry.G1(); } -const FieldMetric& Coordinates::G2() const { return geometry.G2(); } -const FieldMetric& Coordinates::G3() const { return geometry.G3(); } - -void Coordinates::setG1(FieldMetric G1) { geometry.setG1(std::move(G1)); } -void Coordinates::setG2(FieldMetric G2) { geometry.setG2(std::move(G2)); } -void Coordinates::setG3(FieldMetric G3) { geometry.setG3(std::move(G3)); } +const FieldMetric& Coordinates::J() const { return J_; } void Coordinates::setJ(FieldMetric J) { //TODO: Calculate J and check value is close - geometry.setJ(std::move(J)); + J_ = std::move(J); } void Coordinates::setJ(BoutReal value, int x, int y) { //TODO: Calculate Bxy and check value is close - geometry.setJ(value, x, y); + J_(x, y) = value; } +const FieldMetric& Coordinates::Bxy() const { return Bxy_; } + void Coordinates::setBxy(FieldMetric Bxy) { //TODO: Calculate Bxy and check value is close - geometry.setBxy(std::move(Bxy)); + Bxy_ = std::move(Bxy); +} + +const FieldMetric& Coordinates::G1_11() const { return christoffel_symbols.G1_11(); } +const FieldMetric& Coordinates::G1_22() const { return christoffel_symbols.G1_22(); } +const FieldMetric& Coordinates::G1_33() const { return christoffel_symbols.G1_33(); } +const FieldMetric& Coordinates::G1_12() const { return christoffel_symbols.G1_12(); } +const FieldMetric& Coordinates::G1_13() const { return christoffel_symbols.G1_13(); } +const FieldMetric& Coordinates::G1_23() const { return christoffel_symbols.G1_23(); } + +const FieldMetric& Coordinates::G2_11() const { return christoffel_symbols.G2_11(); } +const FieldMetric& Coordinates::G2_22() const { return christoffel_symbols.G2_22(); } +const FieldMetric& Coordinates::G2_33() const { return christoffel_symbols.G2_33(); } +const FieldMetric& Coordinates::G2_12() const { return christoffel_symbols.G2_12(); } +const FieldMetric& Coordinates::G2_13() const { return christoffel_symbols.G2_13(); } +const FieldMetric& Coordinates::G2_23() const { return christoffel_symbols.G2_23(); } + +const FieldMetric& Coordinates::G3_11() const { return christoffel_symbols.G3_11(); } +const FieldMetric& Coordinates::G3_22() const { return christoffel_symbols.G3_22(); } +const FieldMetric& Coordinates::G3_33() const { return christoffel_symbols.G3_33(); } +const FieldMetric& Coordinates::G3_12() const { return christoffel_symbols.G3_12(); } +const FieldMetric& Coordinates::G3_13() const { return christoffel_symbols.G3_13(); } +const FieldMetric& Coordinates::G3_23() const { return christoffel_symbols.G3_23(); } + +const FieldMetric& Coordinates::G1() const { return christoffel_symbols.G1(); } +const FieldMetric& Coordinates::G2() const { return christoffel_symbols.G2(); } +const FieldMetric& Coordinates::G3() const { return christoffel_symbols.G3(); } + +void Coordinates::setG1(FieldMetric G1) { christoffel_symbols.setG1(G1); } +void Coordinates::setG2(FieldMetric G2) { christoffel_symbols.setG2(G2); } +void Coordinates::setG3(FieldMetric G3) { christoffel_symbols.setG3(G3); } + +void Coordinates::applyToChristoffelSymbols( + const std::function& function) { + christoffel_symbols.map(function); } const MetricTensor& Coordinates::getContravariantMetricTensor() const { - return geometry.getContravariantMetricTensor(); + return contravariantMetricTensor; +} + +//const MetricTensor& Coordinates::getCovariantMetricTensor() const { +// return covariantMetricTensor; +//} + +void Coordinates::setContravariantMetricTensor(const MetricTensor& metric_tensor, + const std::string& region, + bool recalculate_staggered, + bool force_interpolate_from_centre) { + contravariantMetricTensor.setMetricTensor(metric_tensor); + covariantMetricTensor.setMetricTensor(contravariantMetricTensor.inverse(region)); + recalculateAndReset(recalculate_staggered, force_interpolate_from_centre); } -void Coordinates::communicateChristoffelSymbolTerms() { +void Coordinates::setCovariantMetricTensor(const MetricTensor& metric_tensor, + const std::string& region, + bool recalculate_staggered, + bool force_interpolate_from_centre) { + covariantMetricTensor.setMetricTensor(metric_tensor); + contravariantMetricTensor.setMetricTensor(covariantMetricTensor.inverse(region)); + recalculateAndReset(recalculate_staggered, force_interpolate_from_centre); +} + +void Coordinates::applyToContravariantMetricTensor( + const std::function& function) { + contravariantMetricTensor.map(function); +} + +void Coordinates::applyToCovariantMetricTensor( + const std::function& function) { + covariantMetricTensor.map(function); +} + +void Coordinates::communicateChristoffelSymbolTerms() const { output_progress.write("\tCommunicating connection terms\n"); @@ -1485,4 +1563,4 @@ void Coordinates::invalidateAndRecalculateCachedVariables() { zlength_cache.reset(); Grad2_par2_DDY_invSgCache.clear(); invSgCache.reset(); -} +} \ No newline at end of file diff --git a/src/mesh/geometry.cxx b/src/mesh/geometry.cxx deleted file mode 100644 index 099142d353..0000000000 --- a/src/mesh/geometry.cxx +++ /dev/null @@ -1,186 +0,0 @@ -/************************************************************************** - * Differential geometry - * Calculates the covariant metric tensor, and christoffel symbol terms - * given the contravariant metric tensor terms - **************************************************************************/ - -#include "bout/geometry.hxx" -#include - -Geometry::Geometry(FieldMetric J, FieldMetric Bxy, const FieldMetric& g11, - const FieldMetric& g22, const FieldMetric& g33, const FieldMetric& g12, - const FieldMetric& g13, const FieldMetric& g23, - const FieldMetric& g_11, const FieldMetric& g_22, - const FieldMetric& g_33, const FieldMetric& g_12, - const FieldMetric& g_13, const FieldMetric& g_23, - DifferentialOperators* differential_operators) - : christoffel_symbols(differential_operators), - contravariantMetricTensor(g11, g22, g33, g12, g13, g23), - covariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23), this_J(std::move(J)), - this_Bxy(std::move(Bxy)), differential_operators(differential_operators){ASSERT0( - differential_operators != nullptr)} - - Geometry::Geometry(Mesh * mesh, DifferentialOperators * differential_operators) - //bool force_interpolate_from_centre - : christoffel_symbols(mesh, differential_operators), - contravariantMetricTensor(1., 1., 1., 0, 0, 0, mesh), - // Identity metric tensor - covariantMetricTensor(1., 1., 1., 0, 0, 0, mesh), this_J(1., mesh), - this_Bxy(1., mesh), differential_operators(differential_operators) { - ASSERT0(differential_operators != nullptr) -} - -void Geometry::CalculateChristoffelSymbols(const FieldMetric& dx, const FieldMetric& dy) { - christoffel_symbols.CalculateChristoffelSymbols(dx, dy, contravariantMetricTensor, - covariantMetricTensor); -} - -MetricTensor::FieldMetric Geometry::recalculateJacobian() { - - // calculate Jacobian using g^-1 = det[g^ij], J = sqrt(g) - auto g = contravariantMetricTensor.g11() * contravariantMetricTensor.g22() - * contravariantMetricTensor.g33() - + 2.0 * contravariantMetricTensor.g12() * contravariantMetricTensor.g13() - * contravariantMetricTensor.g23() - - contravariantMetricTensor.g11() * contravariantMetricTensor.g23() - * contravariantMetricTensor.g23() - - contravariantMetricTensor.g22() * contravariantMetricTensor.g13() - * contravariantMetricTensor.g13() - - contravariantMetricTensor.g33() * contravariantMetricTensor.g12() - * contravariantMetricTensor.g12(); - - // Check that g is positive - bout::checkPositive(g, "The determinant of g^ij", "RGN_NOBNDRY"); - - return 1. / sqrt(g); -} - -MetricTensor::FieldMetric Geometry::recalculateBxy() { - - return sqrt(covariantMetricTensor.g22()) / this_J; -} - -void Geometry::checkCovariant(int ystart) { covariantMetricTensor.check(ystart); } - -void Geometry::checkContravariant(int ystart) { contravariantMetricTensor.check(ystart); } - -void Geometry::setContravariantMetricTensor(const MetricTensor& metric_tensor, - const std::string& region) { - contravariantMetricTensor.setMetricTensor(metric_tensor); - covariantMetricTensor.setMetricTensor(contravariantMetricTensor.inverse(region)); -} - -void Geometry::setCovariantMetricTensor(const MetricTensor& metric_tensor, - const std::string& region) { - covariantMetricTensor.setMetricTensor(metric_tensor); - contravariantMetricTensor.setMetricTensor(covariantMetricTensor.inverse(region)); -} - -const MetricTensor::FieldMetric& Geometry::g_11() const { - return covariantMetricTensor.g11(); -} -const MetricTensor::FieldMetric& Geometry::g_22() const { - return covariantMetricTensor.g22(); -} -const MetricTensor::FieldMetric& Geometry::g_33() const { - return covariantMetricTensor.g33(); -} -const MetricTensor::FieldMetric& Geometry::g_12() const { - return covariantMetricTensor.g12(); -} -const MetricTensor::FieldMetric& Geometry::g_13() const { - return covariantMetricTensor.g13(); -} -const MetricTensor::FieldMetric& Geometry::g_23() const { - return covariantMetricTensor.g23(); -} - -const MetricTensor::FieldMetric& Geometry::g11() const { - return contravariantMetricTensor.g11(); -} -const MetricTensor::FieldMetric& Geometry::g22() const { - return contravariantMetricTensor.g22(); -} -const MetricTensor::FieldMetric& Geometry::g33() const { - return contravariantMetricTensor.g33(); -} -const MetricTensor::FieldMetric& Geometry::g12() const { - return contravariantMetricTensor.g12(); -} -const MetricTensor::FieldMetric& Geometry::g13() const { - return contravariantMetricTensor.g13(); -} -const MetricTensor::FieldMetric& Geometry::g23() const { - return contravariantMetricTensor.g23(); -} - -const FieldMetric& Geometry::G1_11() const { return christoffel_symbols.G1_11(); } -const FieldMetric& Geometry::G1_22() const { return christoffel_symbols.G1_22(); } -const FieldMetric& Geometry::G1_33() const { return christoffel_symbols.G1_33(); } -const FieldMetric& Geometry::G1_12() const { return christoffel_symbols.G1_12(); } -const FieldMetric& Geometry::G1_13() const { return christoffel_symbols.G1_13(); } -const FieldMetric& Geometry::G1_23() const { return christoffel_symbols.G1_23(); } - -const FieldMetric& Geometry::G2_11() const { return christoffel_symbols.G2_11(); } -const FieldMetric& Geometry::G2_22() const { return christoffel_symbols.G2_22(); } -const FieldMetric& Geometry::G2_33() const { return christoffel_symbols.G2_33(); } -const FieldMetric& Geometry::G2_12() const { return christoffel_symbols.G2_12(); } -const FieldMetric& Geometry::G2_13() const { return christoffel_symbols.G2_13(); } -const FieldMetric& Geometry::G2_23() const { return christoffel_symbols.G2_23(); } - -const FieldMetric& Geometry::G3_11() const { return christoffel_symbols.G3_11(); } -const FieldMetric& Geometry::G3_22() const { return christoffel_symbols.G3_22(); } -const FieldMetric& Geometry::G3_33() const { return christoffel_symbols.G3_33(); } -const FieldMetric& Geometry::G3_12() const { return christoffel_symbols.G3_12(); } -const FieldMetric& Geometry::G3_13() const { return christoffel_symbols.G3_13(); } -const FieldMetric& Geometry::G3_23() const { return christoffel_symbols.G3_23(); } - -const FieldMetric& Geometry::G1() const { return christoffel_symbols.G1(); } -const FieldMetric& Geometry::G2() const { return christoffel_symbols.G2(); } -const FieldMetric& Geometry::G3() const { return christoffel_symbols.G3(); } - -const FieldMetric& Geometry::J() const { return this_J; } - -const FieldMetric& Geometry::Bxy() const { return this_Bxy; } - -void Geometry::setG1(FieldMetric G1) { christoffel_symbols.setG1(G1); } -void Geometry::setG2(FieldMetric G2) { christoffel_symbols.setG2(G2); } -void Geometry::setG3(FieldMetric G3) { christoffel_symbols.setG3(G3); } - -void Geometry::setJ(FieldMetric J) { - //TODO: Calculate J and check value is close - this_J = std::move(J); -} - -void Geometry::setJ(BoutReal value, int x, int y) { - //TODO: Calculate Bxy and check value is close - this_J(x, y) = value; -} - -void Geometry::setBxy(FieldMetric Bxy) { - //TODO: Calculate Bxy and check value is close - this_Bxy = std::move(Bxy); -} - -const MetricTensor& Geometry::getContravariantMetricTensor() const { - return contravariantMetricTensor; -} - -const MetricTensor& Geometry::getCovariantMetricTensor() const { - return covariantMetricTensor; -} - -void Geometry::applyToContravariantMetricTensor( - const std::function& function) { - contravariantMetricTensor.map(function); -} - -void Geometry::applyToCovariantMetricTensor( - const std::function& function) { - covariantMetricTensor.map(function); -} - -void Geometry::applyToChristoffelSymbols( - const std::function& function) { - christoffel_symbols.map(function); -} From 88e18b6ab2a7951a3d7b3d2c3693d8cba98485da Mon Sep 17 00:00:00 2001 From: tomc271 Date: Mon, 11 Dec 2023 12:02:57 +0000 Subject: [PATCH 286/491] Corrected comment. --- src/mesh/coordinates.cxx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index d441c6c7a7..d3ae6dbd7c 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -1468,7 +1468,7 @@ void Coordinates::setJ(FieldMetric J) { } void Coordinates::setJ(BoutReal value, int x, int y) { - //TODO: Calculate Bxy and check value is close + //TODO: Calculate J and check value is close J_(x, y) = value; } From 0460b04daf8e37ff24bea39bd33b72b3128b1e87 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Mon, 11 Dec 2023 17:18:21 +0000 Subject: [PATCH 287/491] Need to call setter directly on contravariantMetricTensor and covariantMetricTensor in Coordinates::setBoundaryCells method, to avoid recalculation of the christoffel symbols, which would (at this stage) involve field data at different locations (specifically the metric tensor and jacobian). --- src/mesh/coordinates.cxx | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index d3ae6dbd7c..78deee2cf5 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -467,7 +467,7 @@ void Coordinates::setBoundaryCells(Mesh* mesh, Options* options, extrapolate_x, extrapolate_y, false, transform.get()); - setContravariantMetricTensor(MetricTensor(g11, g22, g33, g12, g13, g23)); + contravariantMetricTensor.setMetricTensor(MetricTensor(g11, g22, g33, g12, g13, g23)); // Check input metrics checkContravariant(); @@ -492,7 +492,8 @@ void Coordinates::setBoundaryCells(Mesh* mesh, Options* options, g_12 = getAtLocOrUnaligned(mesh, "g_12", 0.0, suffix, location); g_13 = getAtLocOrUnaligned(mesh, "g_13", 0.0, suffix, location); g_23 = getAtLocOrUnaligned(mesh, "g_23", 0.0, suffix, location); - setCovariantMetricTensor(MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + covariantMetricTensor.setMetricTensor( + MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); output_warn.write("\tWARNING! Covariant components of metric tensor set manually. " "Contravariant components NOT recalculated\n"); From 0c65aed5887001d60a7ce6bdafd0f56a25f39600 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Mon, 11 Dec 2023 18:06:32 +0000 Subject: [PATCH 288/491] Ensure covariantMetricTensor is set. --- src/mesh/coordinates.cxx | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 78deee2cf5..8f8f8b549e 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -497,11 +497,11 @@ void Coordinates::setBoundaryCells(Mesh* mesh, Options* options, output_warn.write("\tWARNING! Covariant components of metric tensor set manually. " "Contravariant components NOT recalculated\n"); - - } else { - output_warn.write("Not all covariant components of metric tensor found. " - "Calculating all from the contravariant tensor\n"); } + } else { + covariantMetricTensor.setMetricTensor(contravariantMetricTensor.inverse()); + output_warn.write("Not all covariant components of metric tensor found. " + "Calculating all from the contravariant tensor\n"); } // More robust to extrapolate derived quantities directly, rather than From aa033054e734f96d3fe57f9b0e91d755e4227ad9 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Mon, 11 Dec 2023 20:02:19 +0000 Subject: [PATCH 289/491] Don't pass mesh to ChristoffelSymbols constructor in the Coordinates constructor that is used with FakeMesh. --- src/mesh/coordinates.cxx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 8f8f8b549e..6d64f11c5f 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -294,7 +294,7 @@ Coordinates::Coordinates(Mesh* mesh, FieldMetric dx, FieldMetric dy, FieldMetric IntShiftTorsion_(std::move(IntShiftTorsion)), contravariantMetricTensor(g11, g22, g33, g12, g13, g23), covariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23), - christoffel_symbols(mesh, differential_operators), J_(std::move(J)), + christoffel_symbols(differential_operators), J_(std::move(J)), Bxy_(std::move(Bxy)){ASSERT0(differential_operators != nullptr)} Coordinates::Coordinates(Mesh * mesh, Options * options, const CELL_LOC loc, From 25399d702ccc2dfa83a44dc42506ef689bcbf1e5 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Mon, 11 Dec 2023 20:03:31 +0000 Subject: [PATCH 290/491] Null check differential_operators in ChristoffelSymbols constructor. --- src/mesh/christoffel_symbols.cxx | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/mesh/christoffel_symbols.cxx b/src/mesh/christoffel_symbols.cxx index 05636101c9..4329ef5e8f 100644 --- a/src/mesh/christoffel_symbols.cxx +++ b/src/mesh/christoffel_symbols.cxx @@ -1,6 +1,5 @@ #include - #include "bout/christoffel_symbols.hxx" ChristoffelSymbols::ChristoffelSymbols( @@ -33,7 +32,9 @@ ChristoffelSymbols::ChristoffelSymbols( : G1_11_(mesh), G1_22_(mesh), G1_33_(mesh), G1_12_(mesh), G1_13_(mesh), G1_23_(mesh), G2_11_(mesh), G2_22_(mesh), G2_33_(mesh), G2_12_(mesh), G2_13_(mesh), G2_23_(mesh), G3_11_(mesh), G3_22_(mesh), G3_33_(mesh), G3_12_(mesh), G3_13_(mesh), G3_23_(mesh), - G1_(mesh), G2_(mesh), G3_(mesh), differential_operators(differential_operators) {} + G1_(mesh), G2_(mesh), G3_(mesh), differential_operators(differential_operators) { + ASSERT0(differential_operators != nullptr) +} const FieldMetric& ChristoffelSymbols::G1_11() const { return G1_11_; } const FieldMetric& ChristoffelSymbols::G1_22() const { return G1_22_; } From 3d81700a8642a423969521782f6940de8d680673 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Mon, 11 Dec 2023 22:14:01 +0000 Subject: [PATCH 291/491] Ensure covariantMetricTensor is set. --- src/mesh/coordinates.cxx | 34 +++++++++++++++------------------- 1 file changed, 15 insertions(+), 19 deletions(-) diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 6d64f11c5f..23efab1777 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -478,26 +478,22 @@ void Coordinates::setBoundaryCells(Mesh* mesh, Options* options, return mesh->sourceHasVar(name + suffix); }; - // Check if any of the components are present - if (std::any_of(begin(covariant_component_names), end(covariant_component_names), + // Check that all components are present + if (std::all_of(begin(covariant_component_names), end(covariant_component_names), source_has_component)) { - // Check that all components are present - if (std::all_of(begin(covariant_component_names), end(covariant_component_names), - source_has_component)) { - - FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; - g_11 = getAtLocOrUnaligned(mesh, "g_11", 1.0, suffix, location); - g_22 = getAtLocOrUnaligned(mesh, "g_22", 1.0, suffix, location); - g_33 = getAtLocOrUnaligned(mesh, "g_33", 1.0, suffix, location); - g_12 = getAtLocOrUnaligned(mesh, "g_12", 0.0, suffix, location); - g_13 = getAtLocOrUnaligned(mesh, "g_13", 0.0, suffix, location); - g_23 = getAtLocOrUnaligned(mesh, "g_23", 0.0, suffix, location); - covariantMetricTensor.setMetricTensor( - MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); - - output_warn.write("\tWARNING! Covariant components of metric tensor set manually. " - "Contravariant components NOT recalculated\n"); - } + + FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; + g_11 = getAtLocOrUnaligned(mesh, "g_11", 1.0, suffix, location); + g_22 = getAtLocOrUnaligned(mesh, "g_22", 1.0, suffix, location); + g_33 = getAtLocOrUnaligned(mesh, "g_33", 1.0, suffix, location); + g_12 = getAtLocOrUnaligned(mesh, "g_12", 0.0, suffix, location); + g_13 = getAtLocOrUnaligned(mesh, "g_13", 0.0, suffix, location); + g_23 = getAtLocOrUnaligned(mesh, "g_23", 0.0, suffix, location); + covariantMetricTensor.setMetricTensor( + MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + + output_warn.write("\tWARNING! Covariant components of metric tensor set manually. " + "Contravariant components NOT recalculated\n"); } else { covariantMetricTensor.setMetricTensor(contravariantMetricTensor.inverse()); output_warn.write("Not all covariant components of metric tensor found. " From e8065ab1cf591ef021a76acabae93e8f464e745c Mon Sep 17 00:00:00 2001 From: tomc271 Date: Tue, 16 Jan 2024 17:34:52 +0000 Subject: [PATCH 292/491] Pass (a reference to) Coordinates to CalculateChristoffelSymbols(), rather than the metric tensor, dx, dy, etc. --- include/bout/christoffel_symbols.hxx | 6 ++--- include/bout/coordinates.hxx | 2 +- src/mesh/christoffel_symbols.cxx | 33 +++++++++++++++------------- src/mesh/coordinates.cxx | 9 ++++---- 4 files changed, 26 insertions(+), 24 deletions(-) diff --git a/include/bout/christoffel_symbols.hxx b/include/bout/christoffel_symbols.hxx index 476554a2fa..e8ffbd6977 100644 --- a/include/bout/christoffel_symbols.hxx +++ b/include/bout/christoffel_symbols.hxx @@ -10,6 +10,8 @@ using FieldMetric = MetricTensor::FieldMetric; +class Coordinates; + class ChristoffelSymbols { public: @@ -77,9 +79,7 @@ public: // ChristoffelSymbols // applyToComponents(std::function function) const; - void CalculateChristoffelSymbols(const FieldMetric& dx, const FieldMetric& dy, - const MetricTensor& contravariantMetricTensor, - const MetricTensor& covariantMetricTensor); + void CalculateChristoffelSymbols(const Coordinates& coordinates); // Transforms the ChristoffelSymbols by applying the given function to every element void map(const std::function& function); diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index 3853f234f7..61f3c0ae87 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -119,7 +119,7 @@ public: const FieldMetric& g23() const; const MetricTensor& getContravariantMetricTensor() const; - // const MetricTensor& getCovariantMetricTensor() const; + const MetricTensor& getCovariantMetricTensor() const; void setContravariantMetricTensor(const MetricTensor& metric_tensor, const std::string& region = "RGN_ALL", diff --git a/src/mesh/christoffel_symbols.cxx b/src/mesh/christoffel_symbols.cxx index 4329ef5e8f..baf9d7b010 100644 --- a/src/mesh/christoffel_symbols.cxx +++ b/src/mesh/christoffel_symbols.cxx @@ -1,6 +1,7 @@ -#include #include "bout/christoffel_symbols.hxx" +#include "bout/coordinates.hxx" +#include ChristoffelSymbols::ChristoffelSymbols( FieldMetric G1_11, FieldMetric G1_22, FieldMetric G1_33, FieldMetric G1_12, @@ -17,18 +18,18 @@ ChristoffelSymbols::ChristoffelSymbols( G3_12_(std::move(G3_12)), G3_13_(std::move(G3_13)), G3_23_(std::move(G3_23)), G1_(std::move(G1)), G2_(std::move(G2)), G3_(std::move(G3)), differential_operators(differential_operators){ - ASSERT0(differential_operators != nullptr)} + ASSERT0(differential_operators != nullptr)}; - /// This constructor (taking no mesh) is needed because mesh may be a FakeMesh, - /// which has no 'source' set, leading to a SEGFAULT if the constructor leads - /// to initialisation of FieldMetric objects. - ChristoffelSymbols::ChristoffelSymbols(DifferentialOperators - * differential_operators) - : differential_operators( - differential_operators){ASSERT0(differential_operators != nullptr)} +/// This constructor (taking no mesh) is needed because mesh may be a FakeMesh, +/// which has no 'source' set, leading to a SEGFAULT if the constructor leads +/// to initialisation of FieldMetric objects. +ChristoffelSymbols::ChristoffelSymbols(DifferentialOperators* differential_operators) + : differential_operators(differential_operators) { + ASSERT0(differential_operators != nullptr); +} - ChristoffelSymbols::ChristoffelSymbols(Mesh * mesh, DifferentialOperators - * differential_operators) +ChristoffelSymbols::ChristoffelSymbols(Mesh* mesh, + DifferentialOperators* differential_operators) : G1_11_(mesh), G1_22_(mesh), G1_33_(mesh), G1_12_(mesh), G1_13_(mesh), G1_23_(mesh), G2_11_(mesh), G2_22_(mesh), G2_33_(mesh), G2_12_(mesh), G2_13_(mesh), G2_23_(mesh), G3_11_(mesh), G3_22_(mesh), G3_33_(mesh), G3_12_(mesh), G3_13_(mesh), G3_23_(mesh), @@ -96,14 +97,16 @@ void ChristoffelSymbols::setChristoffelSymbols( G3_ = G3; } -void ChristoffelSymbols::CalculateChristoffelSymbols( - const FieldMetric& dx, const FieldMetric& dy, - const MetricTensor& contravariantMetricTensor, - const MetricTensor& covariantMetricTensor) { +void ChristoffelSymbols::CalculateChristoffelSymbols(const Coordinates& coordinates) { // Calculate Christoffel symbol terms (18 independent values) // Note: This calculation is completely general: metric // tensor can be 2D or 3D. For 2D, all DDZ terms are zero + const auto& dx = coordinates.dx(); + const auto& dy = coordinates.dy(); + const auto& contravariantMetricTensor = coordinates.getContravariantMetricTensor(); + const auto& covariantMetricTensor = coordinates.getCovariantMetricTensor(); + G1_11_ = 0.5 * contravariantMetricTensor.g11() * differential_operators->DDX(covariantMetricTensor.g11(), dx) + contravariantMetricTensor.g12() diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 23efab1777..45aac41d63 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -753,8 +753,7 @@ void Coordinates::correctionForNonUniformMeshes(bool force_interpolate_from_cent void Coordinates::calculateCommunicateAndExtrapolateChristoffelSymbols() { - christoffel_symbols.CalculateChristoffelSymbols(dx(), dy(), contravariantMetricTensor, - covariantMetricTensor); + christoffel_symbols.CalculateChristoffelSymbols(*this); auto tmp = J() * g12(); communicate(tmp); @@ -1514,9 +1513,9 @@ const MetricTensor& Coordinates::getContravariantMetricTensor() const { return contravariantMetricTensor; } -//const MetricTensor& Coordinates::getCovariantMetricTensor() const { -// return covariantMetricTensor; -//} +const MetricTensor& Coordinates::getCovariantMetricTensor() const { + return covariantMetricTensor; +} void Coordinates::setContravariantMetricTensor(const MetricTensor& metric_tensor, const std::string& region, From 35402ddad6e69a51022c65000cbf7c3a0b64d1ed Mon Sep 17 00:00:00 2001 From: tomc271 Date: Tue, 16 Jan 2024 18:10:40 +0000 Subject: [PATCH 293/491] Use same pattern as for invSg for lazy evaluation of Coordinates::christoffel_symbols. --- include/bout/coordinates.hxx | 4 +- src/mesh/coordinates.cxx | 93 ++++++++++++++++++++++-------------- 2 files changed, 61 insertions(+), 36 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index 61f3c0ae87..67fee046e8 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -286,6 +286,8 @@ public: const FieldMetric& invSg() const; + ChristoffelSymbols christoffel_symbols() const; + void recalculateAndReset(bool recalculate_staggered, bool force_interpolate_from_centre); @@ -325,7 +327,7 @@ private: MetricTensor covariantMetricTensor; /// Christoffel symbol of the second kind (connection coefficients) - ChristoffelSymbols christoffel_symbols; + mutable std::unique_ptr christoffel_symbols_cache{nullptr}; void applyToContravariantMetricTensor( const std::function& function); diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 45aac41d63..767c688859 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -293,22 +293,19 @@ Coordinates::Coordinates(Mesh* mesh, FieldMetric dx, FieldMetric dy, FieldMetric dy_(std::move(dy)), dz_(std::move(dz)), ShiftTorsion_(std::move(ShiftTorsion)), IntShiftTorsion_(std::move(IntShiftTorsion)), contravariantMetricTensor(g11, g22, g33, g12, g13, g23), - covariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23), - christoffel_symbols(differential_operators), J_(std::move(J)), - Bxy_(std::move(Bxy)){ASSERT0(differential_operators != nullptr)} + covariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23), J_(std::move(J)), + Bxy_(std::move(Bxy)){ASSERT0(differential_operators != nullptr)}; - Coordinates::Coordinates(Mesh * mesh, Options * options, const CELL_LOC loc, - const Coordinates* coords_in, - bool force_interpolate_from_centre) +Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, + const Coordinates* coords_in, bool force_interpolate_from_centre) : localmesh(mesh), location(loc), differential_operators(mesh->getDifferentialOperators()), dx_(1., mesh), dy_(1., mesh), dz_(1., mesh), d1_dx_(mesh), d1_dy_(mesh), d1_dz_(mesh), ShiftTorsion_(mesh), IntShiftTorsion_(mesh), contravariantMetricTensor(1., 1., 1., 0, 0, 0, mesh), // Identity metric tensor - covariantMetricTensor(1., 1., 1., 0, 0, 0, mesh), - christoffel_symbols(mesh, differential_operators), J_(1., mesh), Bxy_(1., mesh) { - ASSERT0(differential_operators != nullptr) + covariantMetricTensor(1., 1., 1., 0, 0, 0, mesh), J_(1., mesh), Bxy_(1., mesh) { + ASSERT0(differential_operators != nullptr); if (options == nullptr) { options = Options::getRoot()->getSection("mesh"); @@ -753,7 +750,7 @@ void Coordinates::correctionForNonUniformMeshes(bool force_interpolate_from_cent void Coordinates::calculateCommunicateAndExtrapolateChristoffelSymbols() { - christoffel_symbols.CalculateChristoffelSymbols(*this); + christoffel_symbols_cache.reset(); auto tmp = J() * g12(); communicate(tmp); @@ -1354,6 +1351,15 @@ Field2D Coordinates::Laplace_perpXY([[maybe_unused]] const Field2D& A, #endif } +ChristoffelSymbols Coordinates::christoffel_symbols() const { + if (christoffel_symbols_cache == nullptr) { + auto ptr = std::make_unique(localmesh, differential_operators); + ptr->CalculateChristoffelSymbols(*this); + christoffel_symbols_cache = std::move(ptr); + } + return *christoffel_symbols_cache; +} + const Coordinates::FieldMetric& Coordinates::invSg() const { if (invSgCache == nullptr) { auto ptr = std::make_unique(); @@ -1475,38 +1481,55 @@ void Coordinates::setBxy(FieldMetric Bxy) { Bxy_ = std::move(Bxy); } -const FieldMetric& Coordinates::G1_11() const { return christoffel_symbols.G1_11(); } -const FieldMetric& Coordinates::G1_22() const { return christoffel_symbols.G1_22(); } -const FieldMetric& Coordinates::G1_33() const { return christoffel_symbols.G1_33(); } -const FieldMetric& Coordinates::G1_12() const { return christoffel_symbols.G1_12(); } -const FieldMetric& Coordinates::G1_13() const { return christoffel_symbols.G1_13(); } -const FieldMetric& Coordinates::G1_23() const { return christoffel_symbols.G1_23(); } +const FieldMetric& Coordinates::G1_11() const { return christoffel_symbols().G1_11(); } + +const FieldMetric& Coordinates::G1_22() const { return christoffel_symbols().G1_22(); } + +const FieldMetric& Coordinates::G1_33() const { return christoffel_symbols().G1_33(); } + +const FieldMetric& Coordinates::G1_12() const { return christoffel_symbols().G1_12(); } + +const FieldMetric& Coordinates::G1_13() const { return christoffel_symbols().G1_13(); } + +const FieldMetric& Coordinates::G1_23() const { return christoffel_symbols().G1_23(); } + +const FieldMetric& Coordinates::G2_11() const { return christoffel_symbols().G2_11(); } + +const FieldMetric& Coordinates::G2_22() const { return christoffel_symbols().G2_22(); } + +const FieldMetric& Coordinates::G2_33() const { return christoffel_symbols().G2_33(); } + +const FieldMetric& Coordinates::G2_12() const { return christoffel_symbols().G2_12(); } + +const FieldMetric& Coordinates::G2_13() const { return christoffel_symbols().G2_13(); } + +const FieldMetric& Coordinates::G2_23() const { return christoffel_symbols().G2_23(); } + +const FieldMetric& Coordinates::G3_11() const { return christoffel_symbols().G3_11(); } + +const FieldMetric& Coordinates::G3_22() const { return christoffel_symbols().G3_22(); } + +const FieldMetric& Coordinates::G3_33() const { return christoffel_symbols().G3_33(); } + +const FieldMetric& Coordinates::G3_12() const { return christoffel_symbols().G3_12(); } + +const FieldMetric& Coordinates::G3_13() const { return christoffel_symbols().G3_13(); } + +const FieldMetric& Coordinates::G3_23() const { return christoffel_symbols().G3_23(); } -const FieldMetric& Coordinates::G2_11() const { return christoffel_symbols.G2_11(); } -const FieldMetric& Coordinates::G2_22() const { return christoffel_symbols.G2_22(); } -const FieldMetric& Coordinates::G2_33() const { return christoffel_symbols.G2_33(); } -const FieldMetric& Coordinates::G2_12() const { return christoffel_symbols.G2_12(); } -const FieldMetric& Coordinates::G2_13() const { return christoffel_symbols.G2_13(); } -const FieldMetric& Coordinates::G2_23() const { return christoffel_symbols.G2_23(); } +const FieldMetric& Coordinates::G1() const { return christoffel_symbols().G1(); } -const FieldMetric& Coordinates::G3_11() const { return christoffel_symbols.G3_11(); } -const FieldMetric& Coordinates::G3_22() const { return christoffel_symbols.G3_22(); } -const FieldMetric& Coordinates::G3_33() const { return christoffel_symbols.G3_33(); } -const FieldMetric& Coordinates::G3_12() const { return christoffel_symbols.G3_12(); } -const FieldMetric& Coordinates::G3_13() const { return christoffel_symbols.G3_13(); } -const FieldMetric& Coordinates::G3_23() const { return christoffel_symbols.G3_23(); } +const FieldMetric& Coordinates::G2() const { return christoffel_symbols().G2(); } -const FieldMetric& Coordinates::G1() const { return christoffel_symbols.G1(); } -const FieldMetric& Coordinates::G2() const { return christoffel_symbols.G2(); } -const FieldMetric& Coordinates::G3() const { return christoffel_symbols.G3(); } +const FieldMetric& Coordinates::G3() const { return christoffel_symbols().G3(); } -void Coordinates::setG1(FieldMetric G1) { christoffel_symbols.setG1(G1); } -void Coordinates::setG2(FieldMetric G2) { christoffel_symbols.setG2(G2); } -void Coordinates::setG3(FieldMetric G3) { christoffel_symbols.setG3(G3); } +void Coordinates::setG1(FieldMetric G1) { christoffel_symbols().setG1(G1); } +void Coordinates::setG2(FieldMetric G2) { christoffel_symbols().setG2(G2); } +void Coordinates::setG3(FieldMetric G3) { christoffel_symbols().setG3(G3); } void Coordinates::applyToChristoffelSymbols( const std::function& function) { - christoffel_symbols.map(function); + christoffel_symbols().map(function); } const MetricTensor& Coordinates::getContravariantMetricTensor() const { From 8f326fc0a0a146a5692e72066f4979376893b0ee Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 17 Jan 2024 15:54:23 +0000 Subject: [PATCH 294/491] christoffel_symbols() return type should be reference. --- include/bout/coordinates.hxx | 2 +- src/mesh/coordinates.cxx | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index 67fee046e8..aa5cb77e91 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -286,7 +286,7 @@ public: const FieldMetric& invSg() const; - ChristoffelSymbols christoffel_symbols() const; + ChristoffelSymbols& christoffel_symbols() const; void recalculateAndReset(bool recalculate_staggered, bool force_interpolate_from_centre); diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 767c688859..c97afe253e 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -1351,7 +1351,7 @@ Field2D Coordinates::Laplace_perpXY([[maybe_unused]] const Field2D& A, #endif } -ChristoffelSymbols Coordinates::christoffel_symbols() const { +ChristoffelSymbols& Coordinates::christoffel_symbols() const { if (christoffel_symbols_cache == nullptr) { auto ptr = std::make_unique(localmesh, differential_operators); ptr->CalculateChristoffelSymbols(*this); From fad98bc8571650e7e6445bd46364da04f778077c Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 17 Jan 2024 10:42:28 +0000 Subject: [PATCH 295/491] Remove unused ChristoffelSymbols constructor. --- include/bout/christoffel_symbols.hxx | 1 - src/mesh/christoffel_symbols.cxx | 11 ----------- src/mesh/coordinates.cxx | 2 +- 3 files changed, 1 insertion(+), 13 deletions(-) diff --git a/include/bout/christoffel_symbols.hxx b/include/bout/christoffel_symbols.hxx index e8ffbd6977..7da1c1c2ce 100644 --- a/include/bout/christoffel_symbols.hxx +++ b/include/bout/christoffel_symbols.hxx @@ -29,7 +29,6 @@ public: explicit ChristoffelSymbols(DifferentialOperators* differential_operators); - ChristoffelSymbols(Mesh* mesh, DifferentialOperators* differential_operators); const FieldMetric& G1_11() const; const FieldMetric& G1_22() const; diff --git a/src/mesh/christoffel_symbols.cxx b/src/mesh/christoffel_symbols.cxx index baf9d7b010..1d1575d61d 100644 --- a/src/mesh/christoffel_symbols.cxx +++ b/src/mesh/christoffel_symbols.cxx @@ -20,22 +20,11 @@ ChristoffelSymbols::ChristoffelSymbols( differential_operators(differential_operators){ ASSERT0(differential_operators != nullptr)}; -/// This constructor (taking no mesh) is needed because mesh may be a FakeMesh, -/// which has no 'source' set, leading to a SEGFAULT if the constructor leads -/// to initialisation of FieldMetric objects. ChristoffelSymbols::ChristoffelSymbols(DifferentialOperators* differential_operators) : differential_operators(differential_operators) { ASSERT0(differential_operators != nullptr); } -ChristoffelSymbols::ChristoffelSymbols(Mesh* mesh, - DifferentialOperators* differential_operators) - : G1_11_(mesh), G1_22_(mesh), G1_33_(mesh), G1_12_(mesh), G1_13_(mesh), G1_23_(mesh), - G2_11_(mesh), G2_22_(mesh), G2_33_(mesh), G2_12_(mesh), G2_13_(mesh), G2_23_(mesh), - G3_11_(mesh), G3_22_(mesh), G3_33_(mesh), G3_12_(mesh), G3_13_(mesh), G3_23_(mesh), - G1_(mesh), G2_(mesh), G3_(mesh), differential_operators(differential_operators) { - ASSERT0(differential_operators != nullptr) -} const FieldMetric& ChristoffelSymbols::G1_11() const { return G1_11_; } const FieldMetric& ChristoffelSymbols::G1_22() const { return G1_22_; } diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index c97afe253e..5d522a8575 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -1353,7 +1353,7 @@ Field2D Coordinates::Laplace_perpXY([[maybe_unused]] const Field2D& A, ChristoffelSymbols& Coordinates::christoffel_symbols() const { if (christoffel_symbols_cache == nullptr) { - auto ptr = std::make_unique(localmesh, differential_operators); + auto ptr = std::make_unique(differential_operators); ptr->CalculateChristoffelSymbols(*this); christoffel_symbols_cache = std::move(ptr); } From 1cc67e4be058d7e8eaf1ce97667fc0a7171bcc94 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 17 Jan 2024 11:27:22 +0000 Subject: [PATCH 296/491] G1, G2, G3 not part of christoffel symbols. --- include/bout/christoffel_symbols.hxx | 28 ++++--------- include/bout/coordinates.hxx | 2 + src/mesh/christoffel_symbols.cxx | 36 ++++------------ src/mesh/coordinates.cxx | 61 +++++++++++----------------- 4 files changed, 43 insertions(+), 84 deletions(-) diff --git a/include/bout/christoffel_symbols.hxx b/include/bout/christoffel_symbols.hxx index 7da1c1c2ce..f86757fc0f 100644 --- a/include/bout/christoffel_symbols.hxx +++ b/include/bout/christoffel_symbols.hxx @@ -21,7 +21,6 @@ public: FieldMetric G2_12, FieldMetric G2_13, FieldMetric G2_23, FieldMetric G3_11, FieldMetric G3_22, FieldMetric G3_33, FieldMetric G3_12, FieldMetric G3_13, FieldMetric G3_23, - FieldMetric G1, FieldMetric G2, FieldMetric G3, DifferentialOperators* differential_operators); // ChristoffelSymbols(BoutReal g11, BoutReal g22, BoutReal g33, BoutReal g12, BoutReal g13, @@ -29,7 +28,6 @@ public: explicit ChristoffelSymbols(DifferentialOperators* differential_operators); - const FieldMetric& G1_11() const; const FieldMetric& G1_22() const; const FieldMetric& G1_33() const; @@ -51,22 +49,15 @@ public: const FieldMetric& G3_13() const; const FieldMetric& G3_23() const; - const FieldMetric& G1() const; - const FieldMetric& G2() const; - const FieldMetric& G3() const; - - void setG1(FieldMetric& G1); - void setG2(FieldMetric& G2); - void setG3(FieldMetric& G3); - - void setChristoffelSymbols( - const FieldMetric& G1_11, const FieldMetric& G1_22, const FieldMetric& G1_33, - const FieldMetric& G1_12, const FieldMetric& G1_13, const FieldMetric& G1_23, - const FieldMetric& G2_11, const FieldMetric& G2_22, const FieldMetric& G2_33, - const FieldMetric& G2_12, const FieldMetric& G2_13, const FieldMetric& G2_23, - const FieldMetric& G3_11, const FieldMetric& G3_22, const FieldMetric& G3_33, - const FieldMetric& G3_12, const FieldMetric& G3_13, const FieldMetric& G3_23, - const FieldMetric& G1, const FieldMetric& G2, const FieldMetric& G3); + void setChristoffelSymbols(const FieldMetric& G1_11, const FieldMetric& G1_22, + const FieldMetric& G1_33, const FieldMetric& G1_12, + const FieldMetric& G1_13, const FieldMetric& G1_23, + const FieldMetric& G2_11, const FieldMetric& G2_22, + const FieldMetric& G2_33, const FieldMetric& G2_12, + const FieldMetric& G2_13, const FieldMetric& G2_23, + const FieldMetric& G3_11, const FieldMetric& G3_22, + const FieldMetric& G3_33, const FieldMetric& G3_12, + const FieldMetric& G3_13, const FieldMetric& G3_23); // void Allocate(); // @@ -87,7 +78,6 @@ private: FieldMetric G1_11_, G1_22_, G1_33_, G1_12_, G1_13_, G1_23_; FieldMetric G2_11_, G2_22_, G2_33_, G2_12_, G2_13_, G2_23_; FieldMetric G3_11_, G3_22_, G3_33_, G3_12_, G3_13_, G3_23_; - FieldMetric G1_, G2_, G3_; DifferentialOperators* differential_operators; diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index aa5cb77e91..42b60a4d73 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -342,6 +342,8 @@ private: FieldMetric Bxy_; ///< Magnitude of B = nabla z times nabla x + FieldMetric G1_, G2_, G3_; + void invalidateAndRecalculateCachedVariables(); /// Set the parallel (y) transform from the options file. diff --git a/src/mesh/christoffel_symbols.cxx b/src/mesh/christoffel_symbols.cxx index 1d1575d61d..e366bf81a3 100644 --- a/src/mesh/christoffel_symbols.cxx +++ b/src/mesh/christoffel_symbols.cxx @@ -8,15 +8,13 @@ ChristoffelSymbols::ChristoffelSymbols( FieldMetric G1_13, FieldMetric G1_23, FieldMetric G2_11, FieldMetric G2_22, FieldMetric G2_33, FieldMetric G2_12, FieldMetric G2_13, FieldMetric G2_23, FieldMetric G3_11, FieldMetric G3_22, FieldMetric G3_33, FieldMetric G3_12, - FieldMetric G3_13, FieldMetric G3_23, FieldMetric G1, FieldMetric G2, FieldMetric G3, - DifferentialOperators* differential_operators) + FieldMetric G3_13, FieldMetric G3_23, DifferentialOperators* differential_operators) : G1_11_(std::move(G1_11)), G1_22_(std::move(G1_22)), G1_33_(std::move(G1_33)), G1_12_(std::move(G1_12)), G1_13_(std::move(G1_13)), G1_23_(std::move(G1_23)), G2_11_(std::move(G2_11)), G2_22_(std::move(G2_22)), G2_33_(std::move(G2_33)), G2_12_(std::move(G2_12)), G2_13_(std::move(G2_13)), G2_23_(std::move(G2_23)), G3_11_(std::move(G3_11)), G3_22_(std::move(G3_22)), G3_33_(std::move(G3_33)), G3_12_(std::move(G3_12)), G3_13_(std::move(G3_13)), G3_23_(std::move(G3_23)), - G1_(std::move(G1)), G2_(std::move(G2)), G3_(std::move(G3)), differential_operators(differential_operators){ ASSERT0(differential_operators != nullptr)}; @@ -25,7 +23,6 @@ ChristoffelSymbols::ChristoffelSymbols(DifferentialOperators* differential_opera ASSERT0(differential_operators != nullptr); } - const FieldMetric& ChristoffelSymbols::G1_11() const { return G1_11_; } const FieldMetric& ChristoffelSymbols::G1_22() const { return G1_22_; } const FieldMetric& ChristoffelSymbols::G1_33() const { return G1_33_; } @@ -47,22 +44,13 @@ const FieldMetric& ChristoffelSymbols::G3_12() const { return G3_12_; } const FieldMetric& ChristoffelSymbols::G3_13() const { return G3_13_; } const FieldMetric& ChristoffelSymbols::G3_23() const { return G3_23_; } -const FieldMetric& ChristoffelSymbols::G1() const { return G1_; } -const FieldMetric& ChristoffelSymbols::G2() const { return G2_; } -const FieldMetric& ChristoffelSymbols::G3() const { return G3_; } - -void ChristoffelSymbols::setG1(FieldMetric& G1) { G1_ = G1; } -void ChristoffelSymbols::setG2(FieldMetric& G2) { G2_ = G2; } -void ChristoffelSymbols::setG3(FieldMetric& G3) { G3_ = G3; } - void ChristoffelSymbols::setChristoffelSymbols( const FieldMetric& G1_11, const FieldMetric& G1_22, const FieldMetric& G1_33, const FieldMetric& G1_12, const FieldMetric& G1_13, const FieldMetric& G1_23, const FieldMetric& G2_11, const FieldMetric& G2_22, const FieldMetric& G2_33, const FieldMetric& G2_12, const FieldMetric& G2_13, const FieldMetric& G2_23, const FieldMetric& G3_11, const FieldMetric& G3_22, const FieldMetric& G3_33, - const FieldMetric& G3_12, const FieldMetric& G3_13, const FieldMetric& G3_23, - const FieldMetric& G1, const FieldMetric& G2, const FieldMetric& G3) { + const FieldMetric& G3_12, const FieldMetric& G3_13, const FieldMetric& G3_23) { G1_11_ = G1_11; G1_22_ = G1_22; G1_33_ = G1_33; @@ -81,9 +69,6 @@ void ChristoffelSymbols::setChristoffelSymbols( G3_12_ = G3_12; G3_13_ = G3_13; G3_23_ = G3_23; - G1_ = G1; - G2_ = G2; - G3_ = G3; } void ChristoffelSymbols::CalculateChristoffelSymbols(const Coordinates& coordinates) { @@ -279,26 +264,23 @@ void ChristoffelSymbols::map( updated_christoffel_symbols.G2_13_, updated_christoffel_symbols.G2_23_, updated_christoffel_symbols.G3_11_, updated_christoffel_symbols.G3_22_, updated_christoffel_symbols.G3_33_, updated_christoffel_symbols.G3_12_, - updated_christoffel_symbols.G3_13_, updated_christoffel_symbols.G3_23_, - updated_christoffel_symbols.G1_, updated_christoffel_symbols.G2_, - updated_christoffel_symbols.G3_); + updated_christoffel_symbols.G3_13_, updated_christoffel_symbols.G3_23_); } ChristoffelSymbols ChristoffelSymbols::applyToComponents( const std::function& function) const { - const auto components_in = - std::vector{G1_11_, G1_22_, G1_33_, G1_12_, G1_13_, G1_23_, G2_11_, - G2_22_, G2_33_, G2_12_, G2_13_, G2_23_, G3_11_, G3_22_, - G3_33_, G3_12_, G3_13_, G3_23_, G1_, G2_, G3_}; + const auto components_in = std::vector{ + G1_11_, G1_22_, G1_33_, G1_12_, G1_13_, G1_23_, G2_11_, G2_22_, G2_33_, + G2_12_, G2_13_, G2_23_, G3_11_, G3_22_, G3_33_, G3_12_, G3_13_, G3_23_}; - FieldMetric components_out[21]; + FieldMetric components_out[18]; std::transform(components_in.begin(), components_in.end(), components_out, function); auto [G1_11, G1_22, G1_33, G1_12, G1_13, G1_23, G2_11, G2_22, G2_33, G2_12, G2_13, - G2_23, G3_11, G3_22, G3_33, G3_12, G3_13, G3_23, G1, G2, G3] = components_out; + G2_23, G3_11, G3_22, G3_33, G3_12, G3_13, G3_23] = components_out; return ChristoffelSymbols(G1_11, G1_22, G1_33, G1_12, G1_13, G1_23, G2_11, G2_22, G2_33, G2_12, G2_13, G2_23, G3_11, G3_22, G3_33, G3_12, G3_13, G3_23, - G1, G2, G3, differential_operators); + differential_operators); } diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 5d522a8575..d9dd514454 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -296,8 +296,9 @@ Coordinates::Coordinates(Mesh* mesh, FieldMetric dx, FieldMetric dy, FieldMetric covariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23), J_(std::move(J)), Bxy_(std::move(Bxy)){ASSERT0(differential_operators != nullptr)}; -Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, - const Coordinates* coords_in, bool force_interpolate_from_centre) + Coordinates::Coordinates(Mesh * mesh, Options * options, const CELL_LOC loc, + const Coordinates* coords_in, + bool force_interpolate_from_centre) : localmesh(mesh), location(loc), differential_operators(mesh->getDifferentialOperators()), dx_(1., mesh), dy_(1., mesh), dz_(1., mesh), d1_dx_(mesh), d1_dy_(mesh), d1_dz_(mesh), @@ -662,6 +663,19 @@ void Coordinates::recalculateAndReset(bool recalculate_staggered, calculateCommunicateAndExtrapolateChristoffelSymbols(); + auto tmp = J() * g12(); + communicate(tmp); + setG1((DDX(J() * g11()) + DDY(tmp) + DDZ(J() * g13())) / J()); + tmp = J() * g22(); + communicate(tmp); + setG2((DDX(J() * g12()) + DDY(tmp) + DDZ(J() * g23())) / J()); + tmp = J() * g23(); + communicate(tmp); + setG3((DDX(J() * g13()) + DDY(tmp) + DDZ(J() * g33())) / J()); + + auto temp = G1(); // TODO: There must be a better way than this! + communicate(temp, G2(), G3()); + correctionForNonUniformMeshes(force_interpolate_from_centre); if (location == CELL_CENTRE && recalculate_staggered) { @@ -752,16 +766,6 @@ void Coordinates::calculateCommunicateAndExtrapolateChristoffelSymbols() { christoffel_symbols_cache.reset(); - auto tmp = J() * g12(); - communicate(tmp); - setG1((DDX(J() * g11()) + DDY(tmp) + DDZ(J() * g13())) / J()); - tmp = J() * g22(); - communicate(tmp); - setG2((DDX(J() * g12()) + DDY(tmp) + DDZ(J() * g23())) / J()); - tmp = J() * g23(); - communicate(tmp); - setG3((DDX(J() * g13()) + DDY(tmp) + DDZ(J() * g33())) / J()); - communicateChristoffelSymbolTerms(); // Set boundary guard cells of Christoffel symbol terms @@ -1482,50 +1486,31 @@ void Coordinates::setBxy(FieldMetric Bxy) { } const FieldMetric& Coordinates::G1_11() const { return christoffel_symbols().G1_11(); } - const FieldMetric& Coordinates::G1_22() const { return christoffel_symbols().G1_22(); } - const FieldMetric& Coordinates::G1_33() const { return christoffel_symbols().G1_33(); } - const FieldMetric& Coordinates::G1_12() const { return christoffel_symbols().G1_12(); } - const FieldMetric& Coordinates::G1_13() const { return christoffel_symbols().G1_13(); } - const FieldMetric& Coordinates::G1_23() const { return christoffel_symbols().G1_23(); } - const FieldMetric& Coordinates::G2_11() const { return christoffel_symbols().G2_11(); } - const FieldMetric& Coordinates::G2_22() const { return christoffel_symbols().G2_22(); } - const FieldMetric& Coordinates::G2_33() const { return christoffel_symbols().G2_33(); } - const FieldMetric& Coordinates::G2_12() const { return christoffel_symbols().G2_12(); } - const FieldMetric& Coordinates::G2_13() const { return christoffel_symbols().G2_13(); } - const FieldMetric& Coordinates::G2_23() const { return christoffel_symbols().G2_23(); } - const FieldMetric& Coordinates::G3_11() const { return christoffel_symbols().G3_11(); } - const FieldMetric& Coordinates::G3_22() const { return christoffel_symbols().G3_22(); } - const FieldMetric& Coordinates::G3_33() const { return christoffel_symbols().G3_33(); } - const FieldMetric& Coordinates::G3_12() const { return christoffel_symbols().G3_12(); } - const FieldMetric& Coordinates::G3_13() const { return christoffel_symbols().G3_13(); } - const FieldMetric& Coordinates::G3_23() const { return christoffel_symbols().G3_23(); } -const FieldMetric& Coordinates::G1() const { return christoffel_symbols().G1(); } - -const FieldMetric& Coordinates::G2() const { return christoffel_symbols().G2(); } - -const FieldMetric& Coordinates::G3() const { return christoffel_symbols().G3(); } +const FieldMetric& Coordinates::G1() const { return G1_; } +const FieldMetric& Coordinates::G2() const { return G2_; } +const FieldMetric& Coordinates::G3() const { return G3_; } -void Coordinates::setG1(FieldMetric G1) { christoffel_symbols().setG1(G1); } -void Coordinates::setG2(FieldMetric G2) { christoffel_symbols().setG2(G2); } -void Coordinates::setG3(FieldMetric G3) { christoffel_symbols().setG3(G3); } +void Coordinates::setG1(FieldMetric G1) { G1_ = G1; } +void Coordinates::setG2(FieldMetric G2) { G2_ = G2; } +void Coordinates::setG3(FieldMetric G3) { G3_ = G3; } void Coordinates::applyToChristoffelSymbols( const std::function& function) { @@ -1575,7 +1560,7 @@ void Coordinates::communicateChristoffelSymbolTerms() const { auto tmp = G1_11(); // TODO: There must be a better way than this! communicate(tmp, G1_22(), G1_33(), G1_12(), G1_13(), G1_23(), G2_11(), G2_22(), G2_33(), G2_12(), G2_13(), G2_23(), G3_11(), G3_22(), G3_33(), G3_12(), G3_13(), - G3_23(), G1(), G2(), G3()); + G3_23()); } void Coordinates::invalidateAndRecalculateCachedVariables() { From 99683e445af9c6d9a8e0545b96fd546b3c511725 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 17 Jan 2024 12:32:17 +0000 Subject: [PATCH 297/491] interpolateAndExtrapolate G1, G2, G3. --- src/mesh/coordinates.cxx | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index d9dd514454..6e17599714 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -673,6 +673,10 @@ void Coordinates::recalculateAndReset(bool recalculate_staggered, communicate(tmp); setG3((DDX(J() * g13()) + DDY(tmp) + DDZ(J() * g33())) / J()); + setG1(localmesh->interpolateAndExtrapolate(G1(), location, true, true, true, transform.get())); + setG2(localmesh->interpolateAndExtrapolate(G2(), location, true, true, true, transform.get())); + setG3(localmesh->interpolateAndExtrapolate(G3(), location, true, true, true, transform.get())); + auto temp = G1(); // TODO: There must be a better way than this! communicate(temp, G2(), G3()); From 12cdafea1665fdd924b89206ff5529af94f64321 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 17 Jan 2024 10:21:56 +0000 Subject: [PATCH 298/491] Prefer const. --- include/bout/coordinates.hxx | 8 ++++---- src/mesh/coordinates.cxx | 12 ++++++------ 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index 42b60a4d73..f5bb2dba35 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -277,9 +277,9 @@ public: const FieldMetric& G2() const; const FieldMetric& G3() const; - void setG1(FieldMetric G1); - void setG2(FieldMetric G2); - void setG3(FieldMetric G3); + void setG1(const FieldMetric& G1); + void setG2(const FieldMetric& G2); + void setG3(const FieldMetric& G3); const FieldMetric& Grad2_par2_DDY_invSg(CELL_LOC outloc, const std::string& method) const; @@ -336,7 +336,7 @@ private: const std::function& function); void applyToChristoffelSymbols( - const std::function& function); + const std::function& function) const; FieldMetric J_; diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 6e17599714..d0c69502bc 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -294,7 +294,7 @@ Coordinates::Coordinates(Mesh* mesh, FieldMetric dx, FieldMetric dy, FieldMetric IntShiftTorsion_(std::move(IntShiftTorsion)), contravariantMetricTensor(g11, g22, g33, g12, g13, g23), covariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23), J_(std::move(J)), - Bxy_(std::move(Bxy)){ASSERT0(differential_operators != nullptr)}; + Bxy_(std::move(Bxy)){ASSERT0(differential_operators != nullptr)} Coordinates::Coordinates(Mesh * mesh, Options * options, const CELL_LOC loc, const Coordinates* coords_in, @@ -306,7 +306,7 @@ Coordinates::Coordinates(Mesh* mesh, FieldMetric dx, FieldMetric dy, FieldMetric contravariantMetricTensor(1., 1., 1., 0, 0, 0, mesh), // Identity metric tensor covariantMetricTensor(1., 1., 1., 0, 0, 0, mesh), J_(1., mesh), Bxy_(1., mesh) { - ASSERT0(differential_operators != nullptr); + ASSERT0(differential_operators != nullptr) if (options == nullptr) { options = Options::getRoot()->getSection("mesh"); @@ -1512,12 +1512,12 @@ const FieldMetric& Coordinates::G1() const { return G1_; } const FieldMetric& Coordinates::G2() const { return G2_; } const FieldMetric& Coordinates::G3() const { return G3_; } -void Coordinates::setG1(FieldMetric G1) { G1_ = G1; } -void Coordinates::setG2(FieldMetric G2) { G2_ = G2; } -void Coordinates::setG3(FieldMetric G3) { G3_ = G3; } +void Coordinates::setG1(const FieldMetric& G1) { G1_ = G1; } +void Coordinates::setG2(const FieldMetric& G2) { G2_ = G2; } +void Coordinates::setG3(const FieldMetric& G3) { G3_ = G3; } void Coordinates::applyToChristoffelSymbols( - const std::function& function) { + const std::function& function) const { christoffel_symbols().map(function); } From ecd60c9cec798eac303b7e6357ec2b821b2d5559 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 17 Jan 2024 16:17:48 +0000 Subject: [PATCH 299/491] Tidy. --- include/bout/christoffel_symbols.hxx | 6 ------ src/mesh/coordinates.cxx | 9 ++++++--- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/include/bout/christoffel_symbols.hxx b/include/bout/christoffel_symbols.hxx index f86757fc0f..905e57d8b1 100644 --- a/include/bout/christoffel_symbols.hxx +++ b/include/bout/christoffel_symbols.hxx @@ -62,12 +62,6 @@ public: // void Allocate(); // // void setLocation(CELL_LOC location); - // - // // Transforms the ChristoffelSymbols by applying the given function to every component - // void map(std::function function); - // - // ChristoffelSymbols - // applyToComponents(std::function function) const; void CalculateChristoffelSymbols(const Coordinates& coordinates); diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index d0c69502bc..0a70822a4b 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -673,9 +673,12 @@ void Coordinates::recalculateAndReset(bool recalculate_staggered, communicate(tmp); setG3((DDX(J() * g13()) + DDY(tmp) + DDZ(J() * g33())) / J()); - setG1(localmesh->interpolateAndExtrapolate(G1(), location, true, true, true, transform.get())); - setG2(localmesh->interpolateAndExtrapolate(G2(), location, true, true, true, transform.get())); - setG3(localmesh->interpolateAndExtrapolate(G3(), location, true, true, true, transform.get())); + setG1(localmesh->interpolateAndExtrapolate(G1(), location, true, true, true, + transform.get())); + setG2(localmesh->interpolateAndExtrapolate(G2(), location, true, true, true, + transform.get())); + setG3(localmesh->interpolateAndExtrapolate(G3(), location, true, true, true, + transform.get())); auto temp = G1(); // TODO: There must be a better way than this! communicate(temp, G2(), G3()); From e08bd112f306d68b1c75cb50cb2d879ceccfe532 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Fri, 19 Jan 2024 10:17:50 +0000 Subject: [PATCH 300/491] Refactor. --- include/bout/coordinates.hxx | 2 +- src/mesh/coordinates.cxx | 10 ++++------ 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index f5bb2dba35..d9234aa747 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -369,7 +369,7 @@ private: bool no_extra_interpolate = false, ParallelTransform* pParallelTransform = nullptr); void communicateChristoffelSymbolTerms() const; - void calculateCommunicateAndExtrapolateChristoffelSymbols(); + void extrapolateChristoffelSymbols(); FieldMetric recalculateJacobian(); FieldMetric recalculateBxy(); diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 0a70822a4b..e82f9ae6d4 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -661,7 +661,9 @@ void Coordinates::recalculateAndReset(bool recalculate_staggered, checkContravariant(); checkCovariant(); - calculateCommunicateAndExtrapolateChristoffelSymbols(); + christoffel_symbols_cache.reset(); + communicateChristoffelSymbolTerms(); + extrapolateChristoffelSymbols(); auto tmp = J() * g12(); communicate(tmp); @@ -769,11 +771,7 @@ void Coordinates::correctionForNonUniformMeshes(bool force_interpolate_from_cent communicate(tmp, d1_dy(), d1_dz()); } -void Coordinates::calculateCommunicateAndExtrapolateChristoffelSymbols() { - - christoffel_symbols_cache.reset(); - - communicateChristoffelSymbolTerms(); +void Coordinates::extrapolateChristoffelSymbols() { // Set boundary guard cells of Christoffel symbol terms // Ideally, when location is staggered, we would set the upper/outer boundary point From f87f834cf38851bc6920644708e06b7e9dabbd45 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Tue, 23 Jan 2024 11:25:43 +0000 Subject: [PATCH 301/491] Formatting. --- src/mesh/coordinates.cxx | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index e82f9ae6d4..225a408602 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -294,11 +294,10 @@ Coordinates::Coordinates(Mesh* mesh, FieldMetric dx, FieldMetric dy, FieldMetric IntShiftTorsion_(std::move(IntShiftTorsion)), contravariantMetricTensor(g11, g22, g33, g12, g13, g23), covariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23), J_(std::move(J)), - Bxy_(std::move(Bxy)){ASSERT0(differential_operators != nullptr)} + Bxy_(std::move(Bxy)){ASSERT0(differential_operators != nullptr)}; - Coordinates::Coordinates(Mesh * mesh, Options * options, const CELL_LOC loc, - const Coordinates* coords_in, - bool force_interpolate_from_centre) +Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, + const Coordinates* coords_in, bool force_interpolate_from_centre) : localmesh(mesh), location(loc), differential_operators(mesh->getDifferentialOperators()), dx_(1., mesh), dy_(1., mesh), dz_(1., mesh), d1_dx_(mesh), d1_dy_(mesh), d1_dz_(mesh), From f54a3e4ad1b320d7144a967c84a7d0d6ff8a8395 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Tue, 23 Jan 2024 12:06:13 +0000 Subject: [PATCH 302/491] Refactor method 'setBoundaryCells' (remove getting dx and dy from the input source). --- include/bout/coordinates.hxx | 3 +-- src/mesh/coordinates.cxx | 15 ++++++++------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index d9234aa747..b8de562bba 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -380,8 +380,7 @@ private: void interpolateFieldsFromOtherCoordinates(const Mesh* mesh, Options* options, const Coordinates* coords_in); - void setBoundaryCells(Mesh* mesh, Options* options, const Coordinates* coords_in, - const std::string& suffix); + void setBoundaryCells(Mesh* mesh, Options* options, const std::string& suffix); }; /* diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 225a408602..55929ad0fc 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -317,7 +317,14 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, if (coords_in == nullptr || suffix.empty() || (!force_interpolate_from_centre && mesh->sourceHasVar("dx" + suffix))) { - setBoundaryCells(mesh, options, coords_in, suffix); + + if (coords_in == nullptr) { + mesh->get(dx_, "dx", 1.0, false); + mesh->get(dy_, "dy", 1.0, false); + } + + setBoundaryCells(mesh, options, suffix); + } else { interpolateFieldsFromOtherCoordinates(mesh, options, coords_in); } @@ -384,7 +391,6 @@ void Coordinates::interpolateFieldsFromOtherCoordinates(const Mesh* mesh, // 'interpolateAndExtrapolate' to set them. Ensures that derivatives are // smooth at all the boundaries. void Coordinates::setBoundaryCells(Mesh* mesh, Options* options, - const Coordinates* coords_in, const std::string& suffix) { const bool extrapolate_x = @@ -402,11 +408,6 @@ void Coordinates::setBoundaryCells(Mesh* mesh, Options* options, "cells. Set option extrapolate_y=false to disable this.\n")); } - if (coords_in == nullptr) { - mesh->get(dx_, "dx", 1.0, false); - mesh->get(dy_, "dy", 1.0, false); - } - nz = mesh->LocalNz; { From 529c2531643f6a0a852659ca65c0c2a4c6790f20 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Tue, 23 Jan 2024 13:25:15 +0000 Subject: [PATCH 303/491] Renaming to distinguish between 'options' variables. --- src/mesh/coordinates.cxx | 42 +++++++++++++++++++--------------------- 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 55929ad0fc..b2509ab3ec 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -296,7 +296,7 @@ Coordinates::Coordinates(Mesh* mesh, FieldMetric dx, FieldMetric dy, FieldMetric covariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23), J_(std::move(J)), Bxy_(std::move(Bxy)){ASSERT0(differential_operators != nullptr)}; -Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, +Coordinates::Coordinates(Mesh* mesh, Options* mesh_options, const CELL_LOC loc, const Coordinates* coords_in, bool force_interpolate_from_centre) : localmesh(mesh), location(loc), differential_operators(mesh->getDifferentialOperators()), dx_(1., mesh), @@ -307,8 +307,8 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, covariantMetricTensor(1., 1., 1., 0, 0, 0, mesh), J_(1., mesh), Bxy_(1., mesh) { ASSERT0(differential_operators != nullptr) - if (options == nullptr) { - options = Options::getRoot()->getSection("mesh"); + if (mesh_options == nullptr) { + mesh_options = Options::getRoot()->getSection("mesh"); } nz = mesh->LocalNz; @@ -323,15 +323,15 @@ Coordinates::Coordinates(Mesh* mesh, Options* options, const CELL_LOC loc, mesh->get(dy_, "dy", 1.0, false); } - setBoundaryCells(mesh, options, suffix); + setBoundaryCells(mesh, mesh_options, suffix); } else { - interpolateFieldsFromOtherCoordinates(mesh, options, coords_in); + interpolateFieldsFromOtherCoordinates(mesh, mesh_options, coords_in); } } void Coordinates::interpolateFieldsFromOtherCoordinates(const Mesh* mesh, - Options* options, + Options* mesh_options, const Coordinates* coords_in) { std::function const @@ -377,7 +377,7 @@ void Coordinates::interpolateFieldsFromOtherCoordinates(const Mesh* mesh, "might require dz!\nPlease provide a dz for the " "staggered quantity!"); } - setParallelTransform(options); + setParallelTransform(mesh_options); dx_ = localmesh->interpolateAndExtrapolate(coords_in->dx(), location, true, true, false, transform.get()); dy_ = localmesh->interpolateAndExtrapolate(coords_in->dy(), location, true, true, false, @@ -390,13 +390,13 @@ void Coordinates::interpolateFieldsFromOtherCoordinates(const Mesh* mesh, // Note: If boundary cells were not loaded from the grid file, use // 'interpolateAndExtrapolate' to set them. Ensures that derivatives are // smooth at all the boundaries. -void Coordinates::setBoundaryCells(Mesh* mesh, Options* options, +void Coordinates::setBoundaryCells(Mesh* mesh, Options* mesh_options, const std::string& suffix) { const bool extrapolate_x = - (*options)["extrapolate_x"].withDefault(not mesh->sourceHasXBoundaryGuards()); + (*mesh_options)["extrapolate_x"].withDefault(not mesh->sourceHasXBoundaryGuards()); const bool extrapolate_y = - (*options)["extrapolate_y"].withDefault(not mesh->sourceHasYBoundaryGuards()); + (*mesh_options)["extrapolate_y"].withDefault(not mesh->sourceHasYBoundaryGuards()); if (extrapolate_x) { output_warn.write(_("WARNING: extrapolating input mesh quantities into x-boundary " @@ -410,19 +410,17 @@ void Coordinates::setBoundaryCells(Mesh* mesh, Options* options, nz = mesh->LocalNz; - { - auto& options = Options::root(); - const bool has_zperiod = options.isSet("zperiod"); - const auto zmin = has_zperiod ? 0.0 : options["ZMIN"].withDefault(0.0); - const auto zmax = has_zperiod ? 1.0 / options["zperiod"].withDefault(1.0) - : options["ZMAX"].withDefault(1.0); + auto& options_root = Options::root(); + const bool has_zperiod = options_root.isSet("zperiod"); + const auto zmin = has_zperiod ? 0.0 : options_root["ZMIN"].withDefault(0.0); + const auto zmax = has_zperiod ? 1.0 / options_root["zperiod"].withDefault(1.0) + : options_root["ZMAX"].withDefault(1.0); - const auto default_dz = (zmax - zmin) * TWOPI / nz; - getAtLoc(mesh, dz_, "dz", suffix, location, default_dz); - } + const auto default_dz = (zmax - zmin) * TWOPI / nz; + getAtLoc(mesh, dz_, "dz", suffix, location, default_dz); // required early for differentiation. - setParallelTransform(options); + setParallelTransform(mesh_options); dz_ = localmesh->interpolateAndExtrapolate(dz_, location, extrapolate_x, extrapolate_y, false, transform.get()); @@ -873,8 +871,8 @@ void fixZShiftGuards(Field2D& zShift) { } } // namespace -void Coordinates::setParallelTransform(Options* options) { - auto* ptoptions = options->getSection("paralleltransform"); +void Coordinates::setParallelTransform(Options* mesh_options) { + auto* ptoptions = mesh_options->getSection("paralleltransform"); std::string ptstr; ptoptions->get("type", ptstr, "identity"); From 937094e5e093c5fa168dbab5c6599ab900f5b94d Mon Sep 17 00:00:00 2001 From: tomc271 Date: Tue, 23 Jan 2024 14:57:14 +0000 Subject: [PATCH 304/491] Make CalculateChristoffelSymbols() method just another ChristoffelSymbols constructor. --- include/bout/christoffel_symbols.hxx | 4 +- src/mesh/christoffel_symbols.cxx | 107 ++++++++++++++------------- src/mesh/coordinates.cxx | 3 +- 3 files changed, 58 insertions(+), 56 deletions(-) diff --git a/include/bout/christoffel_symbols.hxx b/include/bout/christoffel_symbols.hxx index 905e57d8b1..26a4b0e7dd 100644 --- a/include/bout/christoffel_symbols.hxx +++ b/include/bout/christoffel_symbols.hxx @@ -23,6 +23,8 @@ public: FieldMetric G3_12, FieldMetric G3_13, FieldMetric G3_23, DifferentialOperators* differential_operators); + ChristoffelSymbols(const Coordinates& coordinates, DifferentialOperators* differential_operators); + // ChristoffelSymbols(BoutReal g11, BoutReal g22, BoutReal g33, BoutReal g12, BoutReal g13, // BoutReal g23, Mesh* mesh); @@ -63,8 +65,6 @@ public: // // void setLocation(CELL_LOC location); - void CalculateChristoffelSymbols(const Coordinates& coordinates); - // Transforms the ChristoffelSymbols by applying the given function to every element void map(const std::function& function); diff --git a/src/mesh/christoffel_symbols.cxx b/src/mesh/christoffel_symbols.cxx index e366bf81a3..e3549489fc 100644 --- a/src/mesh/christoffel_symbols.cxx +++ b/src/mesh/christoffel_symbols.cxx @@ -18,60 +18,10 @@ ChristoffelSymbols::ChristoffelSymbols( differential_operators(differential_operators){ ASSERT0(differential_operators != nullptr)}; -ChristoffelSymbols::ChristoffelSymbols(DifferentialOperators* differential_operators) +ChristoffelSymbols::ChristoffelSymbols(const Coordinates& coordinates, + DifferentialOperators* differential_operators) : differential_operators(differential_operators) { ASSERT0(differential_operators != nullptr); -} - -const FieldMetric& ChristoffelSymbols::G1_11() const { return G1_11_; } -const FieldMetric& ChristoffelSymbols::G1_22() const { return G1_22_; } -const FieldMetric& ChristoffelSymbols::G1_33() const { return G1_33_; } -const FieldMetric& ChristoffelSymbols::G1_12() const { return G1_12_; } -const FieldMetric& ChristoffelSymbols::G1_13() const { return G1_13_; } -const FieldMetric& ChristoffelSymbols::G1_23() const { return G1_23_; } - -const FieldMetric& ChristoffelSymbols::G2_11() const { return G2_11_; } -const FieldMetric& ChristoffelSymbols::G2_22() const { return G2_22_; } -const FieldMetric& ChristoffelSymbols::G2_33() const { return G2_33_; } -const FieldMetric& ChristoffelSymbols::G2_12() const { return G2_12_; } -const FieldMetric& ChristoffelSymbols::G2_13() const { return G2_13_; } -const FieldMetric& ChristoffelSymbols::G2_23() const { return G2_23_; } - -const FieldMetric& ChristoffelSymbols::G3_11() const { return G3_11_; } -const FieldMetric& ChristoffelSymbols::G3_22() const { return G3_22_; } -const FieldMetric& ChristoffelSymbols::G3_33() const { return G3_33_; } -const FieldMetric& ChristoffelSymbols::G3_12() const { return G3_12_; } -const FieldMetric& ChristoffelSymbols::G3_13() const { return G3_13_; } -const FieldMetric& ChristoffelSymbols::G3_23() const { return G3_23_; } - -void ChristoffelSymbols::setChristoffelSymbols( - const FieldMetric& G1_11, const FieldMetric& G1_22, const FieldMetric& G1_33, - const FieldMetric& G1_12, const FieldMetric& G1_13, const FieldMetric& G1_23, - const FieldMetric& G2_11, const FieldMetric& G2_22, const FieldMetric& G2_33, - const FieldMetric& G2_12, const FieldMetric& G2_13, const FieldMetric& G2_23, - const FieldMetric& G3_11, const FieldMetric& G3_22, const FieldMetric& G3_33, - const FieldMetric& G3_12, const FieldMetric& G3_13, const FieldMetric& G3_23) { - G1_11_ = G1_11; - G1_22_ = G1_22; - G1_33_ = G1_33; - G1_12_ = G1_12; - G1_13_ = G1_13; - G1_23_ = G1_23; - G2_11_ = G2_11; - G2_22_ = G2_22; - G2_33_ = G2_33; - G2_12_ = G2_12; - G2_13_ = G2_13; - G2_23_ = G2_23; - G3_11_ = G3_11; - G3_22_ = G3_22; - G3_33_ = G3_33; - G3_12_ = G3_12; - G3_13_ = G3_13; - G3_23_ = G3_23; -} - -void ChristoffelSymbols::CalculateChristoffelSymbols(const Coordinates& coordinates) { // Calculate Christoffel symbol terms (18 independent values) // Note: This calculation is completely general: metric // tensor can be 2D or 3D. For 2D, all DDZ terms are zero @@ -250,6 +200,59 @@ void ChristoffelSymbols::CalculateChristoffelSymbols(const Coordinates& coordina * differential_operators->DDY(covariantMetricTensor.g33(), dy); } +ChristoffelSymbols::ChristoffelSymbols(DifferentialOperators* differential_operators) + : differential_operators(differential_operators) { + ASSERT0(differential_operators != nullptr); +} + +const FieldMetric& ChristoffelSymbols::G1_11() const { return G1_11_; } +const FieldMetric& ChristoffelSymbols::G1_22() const { return G1_22_; } +const FieldMetric& ChristoffelSymbols::G1_33() const { return G1_33_; } +const FieldMetric& ChristoffelSymbols::G1_12() const { return G1_12_; } +const FieldMetric& ChristoffelSymbols::G1_13() const { return G1_13_; } +const FieldMetric& ChristoffelSymbols::G1_23() const { return G1_23_; } + +const FieldMetric& ChristoffelSymbols::G2_11() const { return G2_11_; } +const FieldMetric& ChristoffelSymbols::G2_22() const { return G2_22_; } +const FieldMetric& ChristoffelSymbols::G2_33() const { return G2_33_; } +const FieldMetric& ChristoffelSymbols::G2_12() const { return G2_12_; } +const FieldMetric& ChristoffelSymbols::G2_13() const { return G2_13_; } +const FieldMetric& ChristoffelSymbols::G2_23() const { return G2_23_; } + +const FieldMetric& ChristoffelSymbols::G3_11() const { return G3_11_; } +const FieldMetric& ChristoffelSymbols::G3_22() const { return G3_22_; } +const FieldMetric& ChristoffelSymbols::G3_33() const { return G3_33_; } +const FieldMetric& ChristoffelSymbols::G3_12() const { return G3_12_; } +const FieldMetric& ChristoffelSymbols::G3_13() const { return G3_13_; } +const FieldMetric& ChristoffelSymbols::G3_23() const { return G3_23_; } + +void ChristoffelSymbols::setChristoffelSymbols( + const FieldMetric& G1_11, const FieldMetric& G1_22, const FieldMetric& G1_33, + const FieldMetric& G1_12, const FieldMetric& G1_13, const FieldMetric& G1_23, + const FieldMetric& G2_11, const FieldMetric& G2_22, const FieldMetric& G2_33, + const FieldMetric& G2_12, const FieldMetric& G2_13, const FieldMetric& G2_23, + const FieldMetric& G3_11, const FieldMetric& G3_22, const FieldMetric& G3_33, + const FieldMetric& G3_12, const FieldMetric& G3_13, const FieldMetric& G3_23) { + G1_11_ = G1_11; + G1_22_ = G1_22; + G1_33_ = G1_33; + G1_12_ = G1_12; + G1_13_ = G1_13; + G1_23_ = G1_23; + G2_11_ = G2_11; + G2_22_ = G2_22; + G2_33_ = G2_33; + G2_12_ = G2_12; + G2_13_ = G2_13; + G2_23_ = G2_23; + G3_11_ = G3_11; + G3_22_ = G3_22; + G3_33_ = G3_33; + G3_12_ = G3_12; + G3_13_ = G3_13; + G3_23_ = G3_23; +} + void ChristoffelSymbols::map( const std::function& function) { diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index b2509ab3ec..08fe77a2a0 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -1360,8 +1360,7 @@ Field2D Coordinates::Laplace_perpXY([[maybe_unused]] const Field2D& A, ChristoffelSymbols& Coordinates::christoffel_symbols() const { if (christoffel_symbols_cache == nullptr) { - auto ptr = std::make_unique(differential_operators); - ptr->CalculateChristoffelSymbols(*this); + auto ptr = std::make_unique(*this, differential_operators); christoffel_symbols_cache = std::move(ptr); } return *christoffel_symbols_cache; From 5f5d892010eeef7649d63956b17fa4076477de7c Mon Sep 17 00:00:00 2001 From: tomc271 Date: Tue, 23 Jan 2024 15:58:17 +0000 Subject: [PATCH 305/491] Extract variables for metric tensor components. --- src/mesh/christoffel_symbols.cxx | 291 +++++++++++++++---------------- 1 file changed, 137 insertions(+), 154 deletions(-) diff --git a/src/mesh/christoffel_symbols.cxx b/src/mesh/christoffel_symbols.cxx index e3549489fc..966647ff6d 100644 --- a/src/mesh/christoffel_symbols.cxx +++ b/src/mesh/christoffel_symbols.cxx @@ -31,173 +31,156 @@ ChristoffelSymbols::ChristoffelSymbols(const Coordinates& coordinates, const auto& contravariantMetricTensor = coordinates.getContravariantMetricTensor(); const auto& covariantMetricTensor = coordinates.getCovariantMetricTensor(); - G1_11_ = 0.5 * contravariantMetricTensor.g11() - * differential_operators->DDX(covariantMetricTensor.g11(), dx) - + contravariantMetricTensor.g12() - * (differential_operators->DDX(covariantMetricTensor.g12(), dx) - - 0.5 * differential_operators->DDY(covariantMetricTensor.g11(), dy)) - + contravariantMetricTensor.g13() - * (differential_operators->DDX(covariantMetricTensor.g13(), dx) - - 0.5 * differential_operators->DDZ(covariantMetricTensor.g11())); - G1_22_ = contravariantMetricTensor.g11() - * (differential_operators->DDY(covariantMetricTensor.g12(), dy) - - 0.5 * differential_operators->DDX(covariantMetricTensor.g22(), dx)) - + 0.5 * contravariantMetricTensor.g12() - * differential_operators->DDY(covariantMetricTensor.g22(), dy) - + contravariantMetricTensor.g13() - * (differential_operators->DDY(covariantMetricTensor.g23(), dy) - - 0.5 * differential_operators->DDZ(covariantMetricTensor.g22())); - G1_33_ = contravariantMetricTensor.g11() - * (differential_operators->DDZ(covariantMetricTensor.g13()) - - 0.5 * differential_operators->DDX(covariantMetricTensor.g33(), dx)) - + contravariantMetricTensor.g12() - * (differential_operators->DDZ(covariantMetricTensor.g23()) - - 0.5 * differential_operators->DDY(covariantMetricTensor.g33(), dy)) - + 0.5 * contravariantMetricTensor.g13() - * differential_operators->DDZ(covariantMetricTensor.g33()); - G1_12_ = 0.5 * contravariantMetricTensor.g11() - * differential_operators->DDY(covariantMetricTensor.g11(), dy) - + 0.5 * contravariantMetricTensor.g12() - * differential_operators->DDX(covariantMetricTensor.g22(), dx) - + 0.5 * contravariantMetricTensor.g13() - * (differential_operators->DDY(covariantMetricTensor.g13(), dy) - + differential_operators->DDX(covariantMetricTensor.g23(), dx) - - differential_operators->DDZ(covariantMetricTensor.g12())); - G1_13_ = 0.5 * contravariantMetricTensor.g11() - * differential_operators->DDZ(covariantMetricTensor.g11()) - + 0.5 * contravariantMetricTensor.g12() - * (differential_operators->DDZ(covariantMetricTensor.g12()) - + differential_operators->DDX(covariantMetricTensor.g23(), dx) - - differential_operators->DDY(covariantMetricTensor.g13(), dy)) - + 0.5 * contravariantMetricTensor.g13() - * differential_operators->DDX(covariantMetricTensor.g33(), dx); + const auto& g11 = contravariantMetricTensor.g11(); + const auto& g22 = contravariantMetricTensor.g22(); + const auto& g33 = contravariantMetricTensor.g33(); + const auto& g12 = contravariantMetricTensor.g12(); + const auto& g13 = contravariantMetricTensor.g13(); + const auto& g23 = contravariantMetricTensor.g23(); + + const auto& g_11 = covariantMetricTensor.g11(); + const auto& g_22 = covariantMetricTensor.g22(); + const auto& g_33 = covariantMetricTensor.g33(); + const auto& g_12 = covariantMetricTensor.g12(); + const auto& g_13 = covariantMetricTensor.g13(); + const auto& g_23 = covariantMetricTensor.g23(); + + G1_11_ = 0.5 * g11 * differential_operators->DDX(g_11, dx) + + g12 + * (differential_operators->DDX(g_12, dx) + - 0.5 * differential_operators->DDY(g_11, dy)) + + g13 + * (differential_operators->DDX(g_13, dx) + - 0.5 * differential_operators->DDZ(g_11)); + G1_22_ = g11 + * (differential_operators->DDY(g_12, dy) + - 0.5 * differential_operators->DDX(g_22, dx)) + + 0.5 * g12 * differential_operators->DDY(g_22, dy) + + g13 + * (differential_operators->DDY(g_23, dy) + - 0.5 * differential_operators->DDZ(g_22)); + G1_33_ = g11 + * (differential_operators->DDZ(g_13) + - 0.5 * differential_operators->DDX(g_33, dx)) + + g12 + * (differential_operators->DDZ(g_23) + - 0.5 * differential_operators->DDY(g_33, dy)) + + 0.5 * g13 * differential_operators->DDZ(g_33); + G1_12_ = 0.5 * g11 * differential_operators->DDY(g_11, dy) + + 0.5 * g12 * differential_operators->DDX(g_22, dx) + + 0.5 * g13 + * (differential_operators->DDY(g_13, dy) + + differential_operators->DDX(g_23, dx) + - differential_operators->DDZ(g_12)); + G1_13_ = + 0.5 * g11 * differential_operators->DDZ(g_11) + + 0.5 * g12 + * (differential_operators->DDZ(g_12) + differential_operators->DDX(g_23, dx) + - differential_operators->DDY(g_13, dy)) + + 0.5 * g13 * differential_operators->DDX(g_33, dx); G1_23_ = - 0.5 * contravariantMetricTensor.g11() - * (differential_operators->DDZ(covariantMetricTensor.g12()) - + differential_operators->DDY(covariantMetricTensor.g13(), dy) - - differential_operators->DDX(covariantMetricTensor.g23(), dx)) - + 0.5 * contravariantMetricTensor.g12() - * (differential_operators->DDZ(covariantMetricTensor.g22()) - + differential_operators->DDY(covariantMetricTensor.g23(), dy) - - differential_operators->DDY(covariantMetricTensor.g23(), dy)) + 0.5 * g11 + * (differential_operators->DDZ(g_12) + differential_operators->DDY(g_13, dy) + - differential_operators->DDX(g_23, dx)) + + 0.5 * g12 + * (differential_operators->DDZ(g_22) + differential_operators->DDY(g_23, dy) + - differential_operators->DDY(g_23, dy)) // + 0.5 *g13*(differential_operators->DDZ(g_32) + differential_operators->DDY(g_33) - differential_operators->DDZ(g_23)); // which equals - + 0.5 * contravariantMetricTensor.g13() - * differential_operators->DDY(covariantMetricTensor.g33(), dy); + + 0.5 * g13 * differential_operators->DDY(g_33, dy); - G2_11_ = 0.5 * contravariantMetricTensor.g12() - * differential_operators->DDX(covariantMetricTensor.g11(), dx) - + contravariantMetricTensor.g22() - * (differential_operators->DDX(covariantMetricTensor.g12(), dx) - - 0.5 * differential_operators->DDY(covariantMetricTensor.g11(), dy)) - + contravariantMetricTensor.g23() - * (differential_operators->DDX(covariantMetricTensor.g13(), dx) - - 0.5 * differential_operators->DDZ(covariantMetricTensor.g11())); - G2_22_ = contravariantMetricTensor.g12() - * (differential_operators->DDY(covariantMetricTensor.g12(), dy) - - 0.5 * differential_operators->DDX(covariantMetricTensor.g22(), dx)) - + 0.5 * contravariantMetricTensor.g22() - * differential_operators->DDY(covariantMetricTensor.g22(), dy) - + contravariantMetricTensor.g23() - * (differential_operators->DDY(contravariantMetricTensor.g23(), dy) - - 0.5 * differential_operators->DDZ(covariantMetricTensor.g22())); - G2_33_ = contravariantMetricTensor.g12() - * (differential_operators->DDZ(covariantMetricTensor.g13()) - - 0.5 * differential_operators->DDX(covariantMetricTensor.g33(), dx)) - + contravariantMetricTensor.g22() - * (differential_operators->DDZ(covariantMetricTensor.g23()) - - 0.5 * differential_operators->DDY(covariantMetricTensor.g33(), dy)) - + 0.5 * contravariantMetricTensor.g23() - * differential_operators->DDZ(covariantMetricTensor.g33()); - G2_12_ = 0.5 * contravariantMetricTensor.g12() - * differential_operators->DDY(covariantMetricTensor.g11(), dy) - + 0.5 * contravariantMetricTensor.g22() - * differential_operators->DDX(covariantMetricTensor.g22(), dx) - + 0.5 * contravariantMetricTensor.g23() - * (differential_operators->DDY(covariantMetricTensor.g13(), dy) - + differential_operators->DDX(covariantMetricTensor.g23(), dx) - - differential_operators->DDZ(covariantMetricTensor.g12())); + G2_11_ = 0.5 * g12 * differential_operators->DDX(g_11, dx) + + g22 + * (differential_operators->DDX(g_12, dx) + - 0.5 * differential_operators->DDY(g_11, dy)) + + g23 + * (differential_operators->DDX(g_13, dx) + - 0.5 * differential_operators->DDZ(g_11)); + G2_22_ = g12 + * (differential_operators->DDY(g_12, dy) + - 0.5 * differential_operators->DDX(g_22, dx)) + + 0.5 * g22 * differential_operators->DDY(g_22, dy) + + g23 + * (differential_operators->DDY(g23, dy) + - 0.5 * differential_operators->DDZ(g_22)); + G2_33_ = g12 + * (differential_operators->DDZ(g_13) + - 0.5 * differential_operators->DDX(g_33, dx)) + + g22 + * (differential_operators->DDZ(g_23) + - 0.5 * differential_operators->DDY(g_33, dy)) + + 0.5 * g23 * differential_operators->DDZ(g_33); + G2_12_ = 0.5 * g12 * differential_operators->DDY(g_11, dy) + + 0.5 * g22 * differential_operators->DDX(g_22, dx) + + 0.5 * g23 + * (differential_operators->DDY(g_13, dy) + + differential_operators->DDX(g_23, dx) + - differential_operators->DDZ(g_12)); G2_13_ = - // 0.5 *g21*(differential_operators->DDZ(covariantMetricTensor.g11()) + differential_operators->DDX(covariantMetricTensor.Getg13()) - differential_operators->DDX(covariantMetricTensor.g13())) + // 0.5 *g21*(differential_operators->DDZ(g_11) + differential_operators->DDX(covariantMetricTensor.Getg13()) - differential_operators->DDX(g_13)) // which equals - 0.5 * contravariantMetricTensor.g12() - * (differential_operators->DDZ(covariantMetricTensor.g11()) - + differential_operators->DDX(covariantMetricTensor.g13(), dx) - - differential_operators->DDX(covariantMetricTensor.g13(), dx)) - // + 0.5 *g22*(differential_operators->DDZ(covariantMetricTensor.Getg21()) + differential_operators->DDX(covariantMetricTensor.g23()) - differential_operators->DDY(covariantMetricTensor.g13())) + 0.5 * g12 + * (differential_operators->DDZ(g_11) + differential_operators->DDX(g_13, dx) + - differential_operators->DDX(g_13, dx)) + // + 0.5 *g22*(differential_operators->DDZ(covariantMetricTensor.Getg21()) + differential_operators->DDX(g_23) - differential_operators->DDY(g_13)) // which equals - + 0.5 * contravariantMetricTensor.g22() - * (differential_operators->DDZ(covariantMetricTensor.g12()) - + differential_operators->DDX(covariantMetricTensor.g23(), dx) - - differential_operators->DDY(covariantMetricTensor.g13(), dy)) - // + 0.5 *g23*(differential_operators->DDZ(covariantMetricTensor.Getg31()) + differential_operators->DDX(covariantMetricTensor.g33()) - differential_operators->DDZ(g_13)); + + 0.5 * g22 + * (differential_operators->DDZ(g_12) + differential_operators->DDX(g_23, dx) + - differential_operators->DDY(g_13, dy)) + // + 0.5 *g23*(differential_operators->DDZ(covariantMetricTensor.Getg31()) + differential_operators->DDX(g_33) - differential_operators->DDZ(g_13)); // which equals - + 0.5 * contravariantMetricTensor.g23() - * differential_operators->DDX(covariantMetricTensor.g33(), dx); - G2_23_ = 0.5 * contravariantMetricTensor.g12() - * (differential_operators->DDZ(covariantMetricTensor.g12()) - + differential_operators->DDY(covariantMetricTensor.g13(), dy) - - differential_operators->DDX(covariantMetricTensor.g23(), dx)) - + 0.5 * contravariantMetricTensor.g22() - * differential_operators->DDZ(covariantMetricTensor.g22()) - + 0.5 * contravariantMetricTensor.g23() - * differential_operators->DDY(covariantMetricTensor.g33(), dy); + + 0.5 * g23 * differential_operators->DDX(g_33, dx); + G2_23_ = + 0.5 * g12 + * (differential_operators->DDZ(g_12) + differential_operators->DDY(g_13, dy) + - differential_operators->DDX(g_23, dx)) + + 0.5 * g22 * differential_operators->DDZ(g_22) + + 0.5 * g23 * differential_operators->DDY(g_33, dy); - G3_11_ = 0.5 * contravariantMetricTensor.g13() - * differential_operators->DDX(covariantMetricTensor.g11(), dx) - + contravariantMetricTensor.g23() - * (differential_operators->DDX(covariantMetricTensor.g12(), dx) - - 0.5 * differential_operators->DDY(covariantMetricTensor.g11(), dy)) - + contravariantMetricTensor.g33() - * (differential_operators->DDX(covariantMetricTensor.g13(), dx) - - 0.5 * differential_operators->DDZ(covariantMetricTensor.g11())); - G3_22_ = contravariantMetricTensor.g13() - * (differential_operators->DDY(covariantMetricTensor.g12(), dy) - - 0.5 * differential_operators->DDX(covariantMetricTensor.g22(), dx)) - + 0.5 * contravariantMetricTensor.g23() - * differential_operators->DDY(covariantMetricTensor.g22(), dy) - + contravariantMetricTensor.g33() - * (differential_operators->DDY(covariantMetricTensor.g23(), dy) - - 0.5 * differential_operators->DDZ(covariantMetricTensor.g22())); - G3_33_ = contravariantMetricTensor.g13() - * (differential_operators->DDZ(covariantMetricTensor.g13()) - - 0.5 * differential_operators->DDX(covariantMetricTensor.g33(), dx)) - + contravariantMetricTensor.g23() - * (differential_operators->DDZ(covariantMetricTensor.g23()) - - 0.5 * differential_operators->DDY(covariantMetricTensor.g33(), dy)) - + 0.5 * contravariantMetricTensor.g33() - * differential_operators->DDZ(covariantMetricTensor.g33()); + G3_11_ = 0.5 * g13 * differential_operators->DDX(g_11, dx) + + g23 + * (differential_operators->DDX(g_12, dx) + - 0.5 * differential_operators->DDY(g_11, dy)) + + g33 + * (differential_operators->DDX(g_13, dx) + - 0.5 * differential_operators->DDZ(g_11)); + G3_22_ = g13 + * (differential_operators->DDY(g_12, dy) + - 0.5 * differential_operators->DDX(g_22, dx)) + + 0.5 * g23 * differential_operators->DDY(g_22, dy) + + g33 + * (differential_operators->DDY(g_23, dy) + - 0.5 * differential_operators->DDZ(g_22)); + G3_33_ = g13 + * (differential_operators->DDZ(g_13) + - 0.5 * differential_operators->DDX(g_33, dx)) + + g23 + * (differential_operators->DDZ(g_23) + - 0.5 * differential_operators->DDY(g_33, dy)) + + 0.5 * g33 * differential_operators->DDZ(g_33); G3_12_ = - // 0.5 *g31*(differential_operators->DDY(covariantMetricTensor.g11()) + differential_operators->DDX(covariantMetricTensor.Getg12()) - differential_operators->DDX(covariantMetricTensor.g12())) + // 0.5 *g31*(differential_operators->DDY(g_11) + differential_operators->DDX(covariantMetricTensor.Getg12()) - differential_operators->DDX(g_12)) // which equals to - 0.5 * contravariantMetricTensor.g13() - * differential_operators->DDY(covariantMetricTensor.g11(), dy) - // + 0.5 *g32*(differential_operators->DDY(covariantMetricTensor.Getg21()) + differential_operators->DDX(covariantMetricTensor.g22()) - differential_operators->DDY(covariantMetricTensor.g12())) + 0.5 * g13 * differential_operators->DDY(g_11, dy) + // + 0.5 *g32*(differential_operators->DDY(covariantMetricTensor.Getg21()) + differential_operators->DDX(g_22) - differential_operators->DDY(g_12)) // which equals to - + 0.5 * contravariantMetricTensor.g23() - * differential_operators->DDX(covariantMetricTensor.g22(), dx) - //+ 0.5 *g33*(differential_operators->DDY(covariantMetricTensor.Getg31()) + differential_operators->DDX(covariantMetricTensor.Getg32()) - differential_operators->DDZ(covariantMetricTensor.g12())); + + 0.5 * g23 * differential_operators->DDX(g_22, dx) + //+ 0.5 *g33*(differential_operators->DDY(covariantMetricTensor.Getg31()) + differential_operators->DDX(covariantMetricTensor.Getg32()) - differential_operators->DDZ(g_12)); // which equals to - + 0.5 * contravariantMetricTensor.g33() - * (differential_operators->DDY(covariantMetricTensor.g13(), dy)) - + differential_operators->DDX(covariantMetricTensor.g23(), dx) - - differential_operators->DDZ(covariantMetricTensor.g12()); - G3_13_ = 0.5 * contravariantMetricTensor.g13() - * differential_operators->DDZ(covariantMetricTensor.g11()) - + 0.5 * contravariantMetricTensor.g23() - * (differential_operators->DDZ(covariantMetricTensor.g12()) - + differential_operators->DDX(covariantMetricTensor.g23(), dx) - - differential_operators->DDY(covariantMetricTensor.g13(), dy)) - + 0.5 * contravariantMetricTensor.g33() - * differential_operators->DDX(covariantMetricTensor.g33(), dx); - G3_23_ = 0.5 * contravariantMetricTensor.g13() - * (differential_operators->DDZ(covariantMetricTensor.g12()) - + differential_operators->DDY(covariantMetricTensor.g13(), dy)) - - differential_operators->DDX(covariantMetricTensor.g23(), dx) - + 0.5 * contravariantMetricTensor.g23() - * differential_operators->DDZ(covariantMetricTensor.g22()) - + 0.5 * contravariantMetricTensor.g33() - * differential_operators->DDY(covariantMetricTensor.g33(), dy); + + 0.5 * g33 * (differential_operators->DDY(g_13, dy)) + + differential_operators->DDX(g_23, dx) - differential_operators->DDZ(g_12); + G3_13_ = + 0.5 * g13 * differential_operators->DDZ(g_11) + + 0.5 * g23 + * (differential_operators->DDZ(g_12) + differential_operators->DDX(g_23, dx) + - differential_operators->DDY(g_13, dy)) + + 0.5 * g33 * differential_operators->DDX(g_33, dx); + G3_23_ = + 0.5 * g13 + * (differential_operators->DDZ(g_12) + differential_operators->DDY(g_13, dy)) + - differential_operators->DDX(g_23, dx) + + 0.5 * g23 * differential_operators->DDZ(g_22) + + 0.5 * g33 * differential_operators->DDY(g_33, dy); } ChristoffelSymbols::ChristoffelSymbols(DifferentialOperators* differential_operators) From f93468717ca202afdb4e9e90339e7973b52540f1 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 24 Jan 2024 14:32:04 +0000 Subject: [PATCH 306/491] Simplify case switching (reduce nesting) in Coordinates::setParallelTransform(). --- src/mesh/coordinates.cxx | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 08fe77a2a0..c6ca372f6e 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -884,8 +884,10 @@ void Coordinates::setParallelTransform(Options* mesh_options) { // Identity method i.e. no transform needed transform = bout::utils::make_unique(*localmesh, ptoptions); + return; + } - } else if (ptstr == "shifted" or ptstr == "shiftedinterp") { + if (ptstr == "shifted" or ptstr == "shiftedinterp") { // Shifted metric method Field2D zShift{localmesh}; @@ -928,12 +930,17 @@ void Coordinates::setParallelTransform(Options* mesh_options) { if (ptstr == "shifted") { transform = bout::utils::make_unique(*localmesh, location, zShift, getUniform(zlength())); - } else if (ptstr == "shiftedinterp") { + } + + if (ptstr == "shiftedinterp") { transform = bout::utils::make_unique( *localmesh, location, zShift, getUniform(zlength())); } - } else if (ptstr == "fci") { + return; + } + + if (ptstr == "fci") { if (location != CELL_CENTRE) { throw BoutException("FCITransform is not available on staggered grids."); @@ -943,11 +950,11 @@ void Coordinates::setParallelTransform(Options* mesh_options) { const bool fci_zperiodic = (*ptoptions)["z_periodic"].withDefault(true); transform = bout::utils::make_unique(*localmesh, dy(), fci_zperiodic, ptoptions); - - } else { - throw BoutException(_("Unrecognised paralleltransform option.\n" - "Valid choices are 'identity', 'shifted', 'fci'")); + return; } + + throw BoutException(_("Unrecognised paralleltransform option.\n" + "Valid choices are 'identity', 'shifted', 'fci'")); } /******************************************************************************* From f29ff3d2384743864d4e7e6e8f3470f089f082b6 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 24 Jan 2024 14:53:33 +0000 Subject: [PATCH 307/491] Extract method getDzFromOptionsFile(). --- include/bout/coordinates.hxx | 2 ++ src/mesh/coordinates.cxx | 29 +++++++++++++++++++---------- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index b8de562bba..2fb2aa9726 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -381,6 +381,8 @@ private: const Coordinates* coords_in); void setBoundaryCells(Mesh* mesh, Options* options, const std::string& suffix); + + FieldMetric getDzFromOptionsFile(Mesh* mesh, const std::string& suffix) const; }; /* diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index c6ca372f6e..b342ded45b 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -408,16 +408,7 @@ void Coordinates::setBoundaryCells(Mesh* mesh, Options* mesh_options, "cells. Set option extrapolate_y=false to disable this.\n")); } - nz = mesh->LocalNz; - - auto& options_root = Options::root(); - const bool has_zperiod = options_root.isSet("zperiod"); - const auto zmin = has_zperiod ? 0.0 : options_root["ZMIN"].withDefault(0.0); - const auto zmax = has_zperiod ? 1.0 / options_root["zperiod"].withDefault(1.0) - : options_root["ZMAX"].withDefault(1.0); - - const auto default_dz = (zmax - zmin) * TWOPI / nz; - getAtLoc(mesh, dz_, "dz", suffix, location, default_dz); + dz_ = getDzFromOptionsFile(mesh, suffix); // required early for differentiation. setParallelTransform(mesh_options); @@ -580,6 +571,23 @@ void Coordinates::setBoundaryCells(Mesh* mesh, Options* mesh_options, } } +FieldMetric Coordinates::getDzFromOptionsFile(Mesh* mesh, + const std::string& suffix) const { + + auto& nz = mesh->LocalNz; + + auto& options_root = Options::root(); + const bool has_zperiod = options_root.isSet("zperiod"); + const auto zmin = has_zperiod ? 0.0 : options_root["ZMIN"].withDefault(0.0); + const auto zmax = has_zperiod ? 1.0 / options_root["zperiod"].withDefault(1.0) + : options_root["ZMAX"].withDefault(1.0); + + const auto default_dz = (zmax - zmin) * TWOPI / nz; + FieldMetric dz; + getAtLoc(mesh, dz, "dz", suffix, location, default_dz); + return dz; +} + void Coordinates::outputVars(Options& output_options) { Timer const time("io"); const std::string loc_string = @@ -872,6 +880,7 @@ void fixZShiftGuards(Field2D& zShift) { } // namespace void Coordinates::setParallelTransform(Options* mesh_options) { + auto* ptoptions = mesh_options->getSection("paralleltransform"); std::string ptstr; From 58c21eb99d1a915c2d413ecc5bd74ff1cc2ea9c6 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 24 Jan 2024 15:08:26 +0000 Subject: [PATCH 308/491] Remove 'getAtLoc' function overload that updates an in/out variable, and the remaining two uses of it. --- src/mesh/coordinates.cxx | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index b342ded45b..d39a67bee5 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -198,13 +198,6 @@ void checkStaggeredGet(Mesh* mesh, const std::string& name, const std::string& s } // convenience function for repeated code -int getAtLoc(Mesh* mesh, Coordinates::FieldMetric& var, const std::string& name, - const std::string& suffix, CELL_LOC location, BoutReal default_value = 0.) { - - checkStaggeredGet(mesh, name, suffix); - return mesh->get(var, name + suffix, default_value, false, location); -} - auto getAtLoc(Mesh* mesh, const std::string& name, const std::string& suffix, CELL_LOC location, BoutReal default_value = 0.) { @@ -372,12 +365,13 @@ void Coordinates::interpolateFieldsFromOtherCoordinates(const Mesh* mesh, dz_ = coords_in->dz(); dz_.setLocation(location); } else { - throw BoutException("We are asked to transform dz to get dz before we " - "have a transform, which " - "might require dz!\nPlease provide a dz for the " - "staggered quantity!"); + throw BoutException( + "We are asked to transform dz to get dz before we have a transform, which might " + "require dz! \nPlease provide a dz for the staggered quantity!"); } + setParallelTransform(mesh_options); + dx_ = localmesh->interpolateAndExtrapolate(coords_in->dx(), location, true, true, false, transform.get()); dy_ = localmesh->interpolateAndExtrapolate(coords_in->dy(), location, true, true, false, @@ -546,13 +540,15 @@ void Coordinates::setBoundaryCells(Mesh* mesh, Options* mesh_options, bout::checkFinite(Bxy(), "Bxy" + suffix, "RGN_NOCORNERS"); bout::checkPositive(Bxy(), "Bxy" + suffix, "RGN_NOCORNERS"); - if (getAtLoc(mesh, ShiftTorsion_, "ShiftTorsion", suffix, location) != 0) { + if (!localmesh->sourceHasVar("ShiftTorsion" + suffix)) { output_warn.write("\tWARNING: No Torsion specified for zShift. " "Derivatives may not be correct\n"); ShiftTorsion_ = 0.0; + } else { + const auto shift_torsion = getAtLoc(mesh, "ShiftTorsion", suffix, location, 0.0); + ShiftTorsion_ = localmesh->interpolateAndExtrapolate( + ShiftTorsion_, location, extrapolate_x, extrapolate_y, false, transform.get()); } - ShiftTorsion_ = localmesh->interpolateAndExtrapolate( - ShiftTorsion_, location, extrapolate_x, extrapolate_y, false, transform.get()); ////////////////////////////////////////////////////// @@ -583,8 +579,7 @@ FieldMetric Coordinates::getDzFromOptionsFile(Mesh* mesh, : options_root["ZMAX"].withDefault(1.0); const auto default_dz = (zmax - zmin) * TWOPI / nz; - FieldMetric dz; - getAtLoc(mesh, dz, "dz", suffix, location, default_dz); + FieldMetric dz = getAtLoc(mesh, "dz", suffix, location, default_dz); return dz; } From 4f451424d879c3790bbc8eafc00142617ed4e08d Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 24 Jan 2024 15:46:24 +0000 Subject: [PATCH 309/491] Remove redundant initialisation of IntShiftTorsion, as it will already have been set. --- src/mesh/coordinates.cxx | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index d39a67bee5..3c90a5d76c 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -561,9 +561,6 @@ void Coordinates::setBoundaryCells(Mesh* mesh, Options* mesh_options, IntShiftTorsion_.setLocation(location); IntShiftTorsion_ = localmesh->interpolateAndExtrapolate( IntShiftTorsion_, location, extrapolate_x, extrapolate_y, false, transform.get()); - } else { - // IntShiftTorsion will not be used, but set to zero to avoid uninitialized field - IntShiftTorsion_ = 0.; } } From 47666d01dc46a5264bc7cf859002df5276c0d320 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 24 Jan 2024 16:57:59 +0000 Subject: [PATCH 310/491] Remove method 'getUnalignedAtLocationAndFillGuards'. --- include/bout/coordinates.hxx | 6 --- src/mesh/coordinates.cxx | 76 +++++++++++------------------------- 2 files changed, 22 insertions(+), 60 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index 2fb2aa9726..365f8778b4 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -362,12 +362,6 @@ private: FieldMetric getUnaligned(const std::string& name, BoutReal default_value); - FieldMetric getUnalignedAtLocationAndFillGuards( - Mesh* mesh, const std::string& name, BoutReal default_value, - const std::string& suffix = "", CELL_LOC cell_location = CELL_CENTRE, - bool extrapolate_x = false, bool extrapolate_y = false, - bool no_extra_interpolate = false, ParallelTransform* pParallelTransform = nullptr); - void communicateChristoffelSymbolTerms() const; void extrapolateChristoffelSymbols(); diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 3c90a5d76c..34781a2b22 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -255,23 +255,6 @@ Coordinates::FieldMetric Coordinates::getUnaligned(const std::string& name, return field; } -Coordinates::FieldMetric Coordinates::getUnalignedAtLocationAndFillGuards( - Mesh* mesh, const std::string& name, BoutReal default_value, - const std::string& suffix, CELL_LOC cell_location, bool extrapolate_x, - bool extrapolate_y, bool no_extra_interpolate, - ParallelTransform* pParallelTransform) { - - auto field = getAtLocOrUnaligned(mesh, name, default_value, suffix, cell_location); - if (suffix.empty()) { - no_extra_interpolate = false; - pParallelTransform = transform.get(); - } - - return field.getMesh()->interpolateAndExtrapolate(field, cell_location, extrapolate_x, - extrapolate_y, no_extra_interpolate, - pParallelTransform); -} - Coordinates::Coordinates(Mesh* mesh, FieldMetric dx, FieldMetric dy, FieldMetric dz, FieldMetric J, FieldMetric Bxy, const FieldMetric& g11, const FieldMetric& g22, const FieldMetric& g33, @@ -410,45 +393,39 @@ void Coordinates::setBoundaryCells(Mesh* mesh, Options* mesh_options, dz_ = localmesh->interpolateAndExtrapolate(dz_, location, extrapolate_x, extrapolate_y, false, transform.get()); - dx_ = getUnalignedAtLocationAndFillGuards(mesh, "dx", 1.0, suffix, location, - extrapolate_x, extrapolate_y, false, - transform.get()); + dx_ = getAtLocOrUnaligned(mesh, "dx", 1.0, suffix, location); + dx_ = localmesh->interpolateAndExtrapolate(dx_, location, extrapolate_x, extrapolate_y, + false, transform.get()); if (mesh->periodicX) { communicate(dx_); } - dy_ = getUnalignedAtLocationAndFillGuards(mesh, "dy", 1.0, suffix, location, - extrapolate_x, extrapolate_y, false, - transform.get()); + dy_ = getAtLocOrUnaligned(mesh, "dy", 1.0, suffix, location); + dy_ = localmesh->interpolateAndExtrapolate(dy_, location, extrapolate_x, extrapolate_y, + false, transform.get()); // grid data source has staggered fields, so read instead of interpolating // Diagonal components of metric tensor g^{ij} (default to 1) - // TODO: Method `getAtLocAndFillGuards` violates command–query separation principle? FieldMetric g11, g22, g33, g12, g13, g23; + g11 = getAtLocOrUnaligned(mesh, "g11", 1.0, suffix, location); + g22 = getAtLocOrUnaligned(mesh, "g22", 1.0, suffix, location); + g33 = getAtLocOrUnaligned(mesh, "g33", 1.0, suffix, location); + g12 = getAtLocOrUnaligned(mesh, "g12", 0.0, suffix, location); + g13 = getAtLocOrUnaligned(mesh, "g13", 0.0, suffix, location); + g23 = getAtLocOrUnaligned(mesh, "g23", 0.0, suffix, location); + contravariantMetricTensor.setMetricTensor(MetricTensor(g11, g22, g33, g12, g13, g23)); - // Diagonal components of metric tensor g^{ij} (default to 1) - g11 = getUnalignedAtLocationAndFillGuards(mesh, "g11", 1.0, suffix, location, - extrapolate_x, extrapolate_y, false, - transform.get()); - g22 = getUnalignedAtLocationAndFillGuards(mesh, "g22", 1.0, suffix, location, - extrapolate_x, extrapolate_y, false, - transform.get()); - g33 = getUnalignedAtLocationAndFillGuards(mesh, "g33", 1.0, suffix, location, - extrapolate_x, extrapolate_y, false, - transform.get()); - // Off-diagonal elements. Default to 0 - g12 = getUnalignedAtLocationAndFillGuards(mesh, "g12", 0.0, suffix, location, - extrapolate_x, extrapolate_y, false, - transform.get()); - g13 = getUnalignedAtLocationAndFillGuards(mesh, "g13", 0.0, suffix, location, - extrapolate_x, extrapolate_y, false, - transform.get()); - g23 = getUnalignedAtLocationAndFillGuards(mesh, "g23", 0.0, suffix, location, - extrapolate_x, extrapolate_y, false, - transform.get()); + // More robust to extrapolate derived quantities directly, rather than + // deriving from extrapolated covariant metric components - contravariantMetricTensor.setMetricTensor(MetricTensor(g11, g22, g33, g12, g13, g23)); + std::function const + interpolateAndExtrapolate_function = [this, extrapolate_y, + extrapolate_x](const FieldMetric& component) { + return localmesh->interpolateAndExtrapolate( + component, location, extrapolate_x, extrapolate_y, false, transform.get()); + }; + applyToContravariantMetricTensor(interpolateAndExtrapolate_function); // Check input metrics checkContravariant(); @@ -481,15 +458,6 @@ void Coordinates::setBoundaryCells(Mesh* mesh, Options* mesh_options, "Calculating all from the contravariant tensor\n"); } - // More robust to extrapolate derived quantities directly, rather than - // deriving from extrapolated covariant metric components - - std::function const - interpolateAndExtrapolate_function = [this, extrapolate_y, - extrapolate_x](const FieldMetric& component) { - return localmesh->interpolateAndExtrapolate( - component, location, extrapolate_x, extrapolate_y, false, transform.get()); - }; applyToCovariantMetricTensor(interpolateAndExtrapolate_function); // Check covariant metrics From 44c87b3dfd1dbb50c5855105a930a495e773e325 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 24 Jan 2024 17:04:05 +0000 Subject: [PATCH 311/491] Use member variables 'localmesh' and 'nz' in methods, rather than passing it in as a parameter or re-fetching. --- include/bout/coordinates.hxx | 5 ++- src/mesh/coordinates.cxx | 70 +++++++++++++++++------------------- 2 files changed, 35 insertions(+), 40 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index 365f8778b4..e002dcd012 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -371,10 +371,9 @@ private: /// Non-uniform meshes. Need to use DDX, DDY void correctionForNonUniformMeshes(bool force_interpolate_from_centre); - void interpolateFieldsFromOtherCoordinates(const Mesh* mesh, Options* options, - const Coordinates* coords_in); + void interpolateFieldsFromOtherCoordinates(Options* options, const Coordinates* coords_in); - void setBoundaryCells(Mesh* mesh, Options* options, const std::string& suffix); + void setBoundaryCells(Options* options, const std::string& suffix); FieldMetric getDzFromOptionsFile(Mesh* mesh, const std::string& suffix) const; }; diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 34781a2b22..d552a2534e 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -299,15 +299,14 @@ Coordinates::Coordinates(Mesh* mesh, Options* mesh_options, const CELL_LOC loc, mesh->get(dy_, "dy", 1.0, false); } - setBoundaryCells(mesh, mesh_options, suffix); + setBoundaryCells(mesh_options, suffix); } else { - interpolateFieldsFromOtherCoordinates(mesh, mesh_options, coords_in); + interpolateFieldsFromOtherCoordinates(mesh_options, coords_in); } } -void Coordinates::interpolateFieldsFromOtherCoordinates(const Mesh* mesh, - Options* mesh_options, +void Coordinates::interpolateFieldsFromOtherCoordinates(Options* mesh_options, const Coordinates* coords_in) { std::function const @@ -339,7 +338,7 @@ void Coordinates::interpolateFieldsFromOtherCoordinates(const Mesh* mesh, ShiftTorsion_ = localmesh->interpolateAndExtrapolate( coords_in->ShiftTorsion(), location, true, true, false, transform.get()); - if (mesh->IncIntShear) { + if (localmesh->IncIntShear) { IntShiftTorsion_ = localmesh->interpolateAndExtrapolate( coords_in->IntShiftTorsion(), location, true, true, false, transform.get()); } @@ -367,13 +366,12 @@ void Coordinates::interpolateFieldsFromOtherCoordinates(const Mesh* mesh, // Note: If boundary cells were not loaded from the grid file, use // 'interpolateAndExtrapolate' to set them. Ensures that derivatives are // smooth at all the boundaries. -void Coordinates::setBoundaryCells(Mesh* mesh, Options* mesh_options, - const std::string& suffix) { +void Coordinates::setBoundaryCells(Options* mesh_options, const std::string& suffix) { - const bool extrapolate_x = - (*mesh_options)["extrapolate_x"].withDefault(not mesh->sourceHasXBoundaryGuards()); - const bool extrapolate_y = - (*mesh_options)["extrapolate_y"].withDefault(not mesh->sourceHasYBoundaryGuards()); + const bool extrapolate_x = (*mesh_options)["extrapolate_x"].withDefault( + not localmesh->sourceHasXBoundaryGuards()); + const bool extrapolate_y = (*mesh_options)["extrapolate_y"].withDefault( + not localmesh->sourceHasYBoundaryGuards()); if (extrapolate_x) { output_warn.write(_("WARNING: extrapolating input mesh quantities into x-boundary " @@ -385,7 +383,7 @@ void Coordinates::setBoundaryCells(Mesh* mesh, Options* mesh_options, "cells. Set option extrapolate_y=false to disable this.\n")); } - dz_ = getDzFromOptionsFile(mesh, suffix); + dz_ = getDzFromOptionsFile(localmesh, suffix); // required early for differentiation. setParallelTransform(mesh_options); @@ -393,27 +391,27 @@ void Coordinates::setBoundaryCells(Mesh* mesh, Options* mesh_options, dz_ = localmesh->interpolateAndExtrapolate(dz_, location, extrapolate_x, extrapolate_y, false, transform.get()); - dx_ = getAtLocOrUnaligned(mesh, "dx", 1.0, suffix, location); + dx_ = getAtLocOrUnaligned(localmesh, "dx", 1.0, suffix, location); dx_ = localmesh->interpolateAndExtrapolate(dx_, location, extrapolate_x, extrapolate_y, false, transform.get()); - if (mesh->periodicX) { + if (localmesh->periodicX) { communicate(dx_); } - dy_ = getAtLocOrUnaligned(mesh, "dy", 1.0, suffix, location); + dy_ = getAtLocOrUnaligned(localmesh, "dy", 1.0, suffix, location); dy_ = localmesh->interpolateAndExtrapolate(dy_, location, extrapolate_x, extrapolate_y, false, transform.get()); // grid data source has staggered fields, so read instead of interpolating // Diagonal components of metric tensor g^{ij} (default to 1) FieldMetric g11, g22, g33, g12, g13, g23; - g11 = getAtLocOrUnaligned(mesh, "g11", 1.0, suffix, location); - g22 = getAtLocOrUnaligned(mesh, "g22", 1.0, suffix, location); - g33 = getAtLocOrUnaligned(mesh, "g33", 1.0, suffix, location); - g12 = getAtLocOrUnaligned(mesh, "g12", 0.0, suffix, location); - g13 = getAtLocOrUnaligned(mesh, "g13", 0.0, suffix, location); - g23 = getAtLocOrUnaligned(mesh, "g23", 0.0, suffix, location); + g11 = getAtLocOrUnaligned(localmesh, "g11", 1.0, suffix, location); + g22 = getAtLocOrUnaligned(localmesh, "g22", 1.0, suffix, location); + g33 = getAtLocOrUnaligned(localmesh, "g33", 1.0, suffix, location); + g12 = getAtLocOrUnaligned(localmesh, "g12", 0.0, suffix, location); + g13 = getAtLocOrUnaligned(localmesh, "g13", 0.0, suffix, location); + g23 = getAtLocOrUnaligned(localmesh, "g23", 0.0, suffix, location); contravariantMetricTensor.setMetricTensor(MetricTensor(g11, g22, g33, g12, g13, g23)); // More robust to extrapolate derived quantities directly, rather than @@ -432,8 +430,8 @@ void Coordinates::setBoundaryCells(Mesh* mesh, Options* mesh_options, /// Find covariant metric components auto covariant_component_names = {"g_11", "g_22", "g_33", "g_12", "g_13", "g_23"}; - auto source_has_component = [&suffix, &mesh](const std::string& name) { - return mesh->sourceHasVar(name + suffix); + auto source_has_component = [&suffix, this](const std::string& name) { + return localmesh->sourceHasVar(name + suffix); }; // Check that all components are present @@ -441,12 +439,12 @@ void Coordinates::setBoundaryCells(Mesh* mesh, Options* mesh_options, source_has_component)) { FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; - g_11 = getAtLocOrUnaligned(mesh, "g_11", 1.0, suffix, location); - g_22 = getAtLocOrUnaligned(mesh, "g_22", 1.0, suffix, location); - g_33 = getAtLocOrUnaligned(mesh, "g_33", 1.0, suffix, location); - g_12 = getAtLocOrUnaligned(mesh, "g_12", 0.0, suffix, location); - g_13 = getAtLocOrUnaligned(mesh, "g_13", 0.0, suffix, location); - g_23 = getAtLocOrUnaligned(mesh, "g_23", 0.0, suffix, location); + g_11 = getAtLocOrUnaligned(localmesh, "g_11", 1.0, suffix, location); + g_22 = getAtLocOrUnaligned(localmesh, "g_22", 1.0, suffix, location); + g_33 = getAtLocOrUnaligned(localmesh, "g_33", 1.0, suffix, location); + g_12 = getAtLocOrUnaligned(localmesh, "g_12", 0.0, suffix, location); + g_13 = getAtLocOrUnaligned(localmesh, "g_13", 0.0, suffix, location); + g_23 = getAtLocOrUnaligned(localmesh, "g_23", 0.0, suffix, location); covariantMetricTensor.setMetricTensor( MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); @@ -472,7 +470,7 @@ void Coordinates::setBoundaryCells(Mesh* mesh, Options* mesh_options, "\tWARNING: Jacobian 'J_{:s}' not found. Calculating from metric tensor\n", suffix); } else { - const auto Jcalc = getAtLoc(mesh, "J", suffix, location); + const auto Jcalc = getAtLoc(localmesh, "J", suffix, location); setJ(localmesh->interpolateAndExtrapolate(Jcalc, location, extrapolate_x, extrapolate_y, false, transform.get())); @@ -499,7 +497,7 @@ void Coordinates::setBoundaryCells(Mesh* mesh, Options* mesh_options, "Calculating from metric tensor\n", suffix); } else { - const auto Bcalc = getAtLoc(mesh, "Bxy", suffix, location); + const auto Bcalc = getAtLoc(localmesh, "Bxy", suffix, location); setBxy(localmesh->interpolateAndExtrapolate(Bcalc, location, extrapolate_x, extrapolate_y, false, transform.get())); output_warn.write("\tMaximum difference in Bxy is {:e}\n", max(abs(Bxy() - Bcalc))); @@ -513,16 +511,16 @@ void Coordinates::setBoundaryCells(Mesh* mesh, Options* mesh_options, "Derivatives may not be correct\n"); ShiftTorsion_ = 0.0; } else { - const auto shift_torsion = getAtLoc(mesh, "ShiftTorsion", suffix, location, 0.0); + const auto shift_torsion = getAtLoc(localmesh, "ShiftTorsion", suffix, location, 0.0); ShiftTorsion_ = localmesh->interpolateAndExtrapolate( ShiftTorsion_, location, extrapolate_x, extrapolate_y, false, transform.get()); } ////////////////////////////////////////////////////// - if (mesh->IncIntShear) { - checkStaggeredGet(mesh, "IntShiftTorsion", suffix); - if (mesh->get(IntShiftTorsion_, "IntShiftTorsion" + suffix, 0.0, false) != 0) { + if (localmesh->IncIntShear) { + checkStaggeredGet(localmesh, "IntShiftTorsion", suffix); + if (localmesh->get(IntShiftTorsion_, "IntShiftTorsion" + suffix, 0.0, false) != 0) { output_warn.write("\tWARNING: No Integrated torsion specified\n"); IntShiftTorsion_ = 0.0; } @@ -535,8 +533,6 @@ void Coordinates::setBoundaryCells(Mesh* mesh, Options* mesh_options, FieldMetric Coordinates::getDzFromOptionsFile(Mesh* mesh, const std::string& suffix) const { - auto& nz = mesh->LocalNz; - auto& options_root = Options::root(); const bool has_zperiod = options_root.isSet("zperiod"); const auto zmin = has_zperiod ? 0.0 : options_root["ZMIN"].withDefault(0.0); From cb86bd3e4d8e1ab24aae24aa93e2d313ae19d2b6 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Sun, 28 Jan 2024 16:58:19 +0000 Subject: [PATCH 312/491] Rename private field J_ to jacobian_cache. --- include/bout/coordinates.hxx | 2 +- src/mesh/coordinates.cxx | 13 +++++++------ 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index e002dcd012..3c80c00d51 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -338,7 +338,7 @@ private: void applyToChristoffelSymbols( const std::function& function) const; - FieldMetric J_; + FieldMetric jacobian_cache; FieldMetric Bxy_; ///< Magnitude of B = nabla z times nabla x diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index d552a2534e..c70b5e2bb1 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -269,7 +269,8 @@ Coordinates::Coordinates(Mesh* mesh, FieldMetric dx, FieldMetric dy, FieldMetric dy_(std::move(dy)), dz_(std::move(dz)), ShiftTorsion_(std::move(ShiftTorsion)), IntShiftTorsion_(std::move(IntShiftTorsion)), contravariantMetricTensor(g11, g22, g33, g12, g13, g23), - covariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23), J_(std::move(J)), + covariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23), + jacobian_cache(std::move(J)), Bxy_(std::move(Bxy)){ASSERT0(differential_operators != nullptr)}; Coordinates::Coordinates(Mesh* mesh, Options* mesh_options, const CELL_LOC loc, @@ -280,7 +281,7 @@ Coordinates::Coordinates(Mesh* mesh, Options* mesh_options, const CELL_LOC loc, ShiftTorsion_(mesh), IntShiftTorsion_(mesh), contravariantMetricTensor(1., 1., 1., 0, 0, 0, mesh), // Identity metric tensor - covariantMetricTensor(1., 1., 1., 0, 0, 0, mesh), J_(1., mesh), Bxy_(1., mesh) { + covariantMetricTensor(1., 1., 1., 0, 0, 0, mesh), jacobian_cache(1., mesh), Bxy_(1., mesh) { ASSERT0(differential_operators != nullptr) if (mesh_options == nullptr) { @@ -777,7 +778,7 @@ MetricTensor::FieldMetric Coordinates::recalculateJacobian() { MetricTensor::FieldMetric Coordinates::recalculateBxy() { - return sqrt(covariantMetricTensor.g22()) / J_; + return sqrt(covariantMetricTensor.g22()) / jacobian_cache; } void Coordinates::jacobian() { @@ -1440,16 +1441,16 @@ const MetricTensor::FieldMetric& Coordinates::g23() const { return contravariantMetricTensor.g23(); } -const FieldMetric& Coordinates::J() const { return J_; } +const FieldMetric& Coordinates::J() const { return jacobian_cache; } void Coordinates::setJ(FieldMetric J) { //TODO: Calculate J and check value is close - J_ = std::move(J); + jacobian_cache = std::move(J); } void Coordinates::setJ(BoutReal value, int x, int y) { //TODO: Calculate J and check value is close - J_(x, y) = value; + jacobian_cache(x, y) = value; } const FieldMetric& Coordinates::Bxy() const { return Bxy_; } From 888317e0344ea94ba387cb615548e3e5cb7aed5b Mon Sep 17 00:00:00 2001 From: tomc271 Date: Sun, 28 Jan 2024 17:05:42 +0000 Subject: [PATCH 313/491] Don't recalculate Jacobian and Bxy if read from file. --- src/mesh/coordinates.cxx | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index c70b5e2bb1..293996ae60 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -281,7 +281,8 @@ Coordinates::Coordinates(Mesh* mesh, Options* mesh_options, const CELL_LOC loc, ShiftTorsion_(mesh), IntShiftTorsion_(mesh), contravariantMetricTensor(1., 1., 1., 0, 0, 0, mesh), // Identity metric tensor - covariantMetricTensor(1., 1., 1., 0, 0, 0, mesh), jacobian_cache(1., mesh), Bxy_(1., mesh) { + covariantMetricTensor(1., 1., 1., 0, 0, 0, mesh), jacobian_cache(1., mesh), + Bxy_(1., mesh) { ASSERT0(differential_operators != nullptr) if (mesh_options == nullptr) { @@ -462,14 +463,16 @@ void Coordinates::setBoundaryCells(Options* mesh_options, const std::string& suf // Check covariant metrics checkCovariant(); - /// Calculate Jacobian and Bxy - jacobian(); - // Attempt to read J from the grid file if (!localmesh->sourceHasVar("J" + suffix)) { + output_warn.write( "\tWARNING: Jacobian 'J_{:s}' not found. Calculating from metric tensor\n", suffix); + + /// Calculate Jacobian and Bxy + jacobian(); + } else { const auto Jcalc = getAtLoc(localmesh, "J", suffix, location); setJ(localmesh->interpolateAndExtrapolate(Jcalc, location, extrapolate_x, From 9ffee2cf8596cb6b82a0cd08c96eec3c6bf767b0 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Sun, 28 Jan 2024 17:08:43 +0000 Subject: [PATCH 314/491] Don't recalculate Bxy if read from file. --- src/mesh/coordinates.cxx | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 293996ae60..b4923c79b7 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -483,11 +483,7 @@ void Coordinates::setBoundaryCells(Options* mesh_options, const std::string& suf auto J_value = J(); // TODO: There may be a better way communicate(J_value); - - // Re-evaluate Bxy using new J - setBxy(sqrt(g_22()) / J()); } - // Check jacobian bout::checkFinite(J(), "J" + suffix, "RGN_NOCORNERS"); bout::checkPositive(J(), "J" + suffix, "RGN_NOCORNERS"); @@ -500,6 +496,8 @@ void Coordinates::setBoundaryCells(Options* mesh_options, const std::string& suf output_warn.write("\tWARNING: Magnitude of B field 'Bxy_{:s}' not found. " "Calculating from metric tensor\n", suffix); + // Re-evaluate Bxy using new J + setBxy(sqrt(g_22()) / J()); } else { const auto Bcalc = getAtLoc(localmesh, "Bxy", suffix, location); setBxy(localmesh->interpolateAndExtrapolate(Bcalc, location, extrapolate_x, From 1bc3d8fedf85309195799eb5c4477311badf2d3d Mon Sep 17 00:00:00 2001 From: tomc271 Date: Sun, 28 Jan 2024 17:13:45 +0000 Subject: [PATCH 315/491] DRY - use existing method recalculateBxy(). --- src/mesh/coordinates.cxx | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index b4923c79b7..3913c6d698 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -497,7 +497,7 @@ void Coordinates::setBoundaryCells(Options* mesh_options, const std::string& suf "Calculating from metric tensor\n", suffix); // Re-evaluate Bxy using new J - setBxy(sqrt(g_22()) / J()); + setBxy(recalculateBxy()); } else { const auto Bcalc = getAtLoc(localmesh, "Bxy", suffix, location); setBxy(localmesh->interpolateAndExtrapolate(Bcalc, location, extrapolate_x, @@ -777,10 +777,7 @@ MetricTensor::FieldMetric Coordinates::recalculateJacobian() { return 1. / sqrt(g); } -MetricTensor::FieldMetric Coordinates::recalculateBxy() { - - return sqrt(covariantMetricTensor.g22()) / jacobian_cache; -} +MetricTensor::FieldMetric Coordinates::recalculateBxy() { return sqrt(g_22()) / J(); } void Coordinates::jacobian() { TRACE("Coordinates::jacobian"); From 033c5dfbf12bba4863290e1e1909c774ddf7efb1 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Sun, 28 Jan 2024 17:17:24 +0000 Subject: [PATCH 316/491] Make recalculateBxy() const. --- include/bout/coordinates.hxx | 5 +++-- src/mesh/coordinates.cxx | 4 +++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index 3c80c00d51..681ab76bb4 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -366,12 +366,13 @@ private: void extrapolateChristoffelSymbols(); FieldMetric recalculateJacobian(); - FieldMetric recalculateBxy(); + FieldMetric recalculateBxy() const; /// Non-uniform meshes. Need to use DDX, DDY void correctionForNonUniformMeshes(bool force_interpolate_from_centre); - void interpolateFieldsFromOtherCoordinates(Options* options, const Coordinates* coords_in); + void interpolateFieldsFromOtherCoordinates(Options* options, + const Coordinates* coords_in); void setBoundaryCells(Options* options, const std::string& suffix); diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 3913c6d698..4acd7527f4 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -777,7 +777,9 @@ MetricTensor::FieldMetric Coordinates::recalculateJacobian() { return 1. / sqrt(g); } -MetricTensor::FieldMetric Coordinates::recalculateBxy() { return sqrt(g_22()) / J(); } +MetricTensor::FieldMetric Coordinates::recalculateBxy() const { + return sqrt(g_22()) / J(); +} void Coordinates::jacobian() { TRACE("Coordinates::jacobian"); From e7ae4f3dac23a13bb0d951378aa3e60a8d4ec4e8 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Sun, 28 Jan 2024 17:20:18 +0000 Subject: [PATCH 317/491] No need to recalculate Bxy twice. --- include/bout/coordinates.hxx | 2 +- src/mesh/coordinates.cxx | 9 ++------- 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index 681ab76bb4..f88f86297f 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -152,7 +152,7 @@ public: /// Calculate differential geometry quantities from the metric tensor int communicateAndCheckMeshSpacing() const; - void jacobian(); ///< Calculate J and Bxy + void jacobian(); ///< Calculate J /////////////////////////////////////////////////////////// // Parallel transforms diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 4acd7527f4..c242a79710 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -470,7 +470,7 @@ void Coordinates::setBoundaryCells(Options* mesh_options, const std::string& suf "\tWARNING: Jacobian 'J_{:s}' not found. Calculating from metric tensor\n", suffix); - /// Calculate Jacobian and Bxy + /// Calculate Jacobian jacobian(); } else { @@ -497,7 +497,7 @@ void Coordinates::setBoundaryCells(Options* mesh_options, const std::string& suf "Calculating from metric tensor\n", suffix); // Re-evaluate Bxy using new J - setBxy(recalculateBxy()); + const auto Bxy = recalculateBxy(); } else { const auto Bcalc = getAtLoc(localmesh, "Bxy", suffix, location); setBxy(localmesh->interpolateAndExtrapolate(Bcalc, location, extrapolate_x, @@ -794,11 +794,6 @@ void Coordinates::jacobian() { // deriving from extrapolated covariant metric components setJ(localmesh->interpolateAndExtrapolate(jacobian, location, extrapolate_x, extrapolate_y, false)); - - const auto Bxy = recalculateBxy(); - // CELL_LOC location, ParallelTransform* pParallelTransform - setBxy(localmesh->interpolateAndExtrapolate(Bxy, location, extrapolate_x, - extrapolate_y, false, transform.get())); } catch (BoutException&) { output_error.write("\tError in jacobian call\n"); throw; From 2a3358f5b6a9d03e10c3cb94c53aec6f02d9e78f Mon Sep 17 00:00:00 2001 From: tomc271 Date: Mon, 29 Jan 2024 17:28:59 +0000 Subject: [PATCH 318/491] Use extrapolate_x, extrapolate_y options if set. (So now we can directly replace jacobian() with recalculateJacobian(); no longer a special case). --- include/bout/coordinates.hxx | 5 +-- src/mesh/coordinates.cxx | 63 +++++++++++++--------------- tests/unit/mesh/test_coordinates.cxx | 2 +- 3 files changed, 31 insertions(+), 39 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index f88f86297f..aa114a417a 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -152,8 +152,6 @@ public: /// Calculate differential geometry quantities from the metric tensor int communicateAndCheckMeshSpacing() const; - void jacobian(); ///< Calculate J - /////////////////////////////////////////////////////////// // Parallel transforms /////////////////////////////////////////////////////////// @@ -291,6 +289,8 @@ public: void recalculateAndReset(bool recalculate_staggered, bool force_interpolate_from_centre); + FieldMetric recalculateJacobian() const; + private: int nz; // Size of mesh in Z. This is mesh->ngz-1 Mesh* localmesh; @@ -365,7 +365,6 @@ private: void communicateChristoffelSymbolTerms() const; void extrapolateChristoffelSymbols(); - FieldMetric recalculateJacobian(); FieldMetric recalculateBxy() const; /// Non-uniform meshes. Need to use DDX, DDY diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index c242a79710..52fe3efe51 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -471,7 +471,11 @@ void Coordinates::setBoundaryCells(Options* mesh_options, const std::string& suf suffix); /// Calculate Jacobian - jacobian(); + const auto jacobian = recalculateJacobian(); + // More robust to extrapolate derived quantities directly, rather than + // deriving from extrapolated covariant metric components + setJ(localmesh->interpolateAndExtrapolate(jacobian, location, extrapolate_x, + extrapolate_y, false, transform.get())); } else { const auto Jcalc = getAtLoc(localmesh, "J", suffix, location); @@ -498,6 +502,8 @@ void Coordinates::setBoundaryCells(Options* mesh_options, const std::string& suf suffix); // Re-evaluate Bxy using new J const auto Bxy = recalculateBxy(); + setBxy(localmesh->interpolateAndExtrapolate(Bxy, location, extrapolate_x, + extrapolate_y, false, transform.get())); } else { const auto Bcalc = getAtLoc(localmesh, "Bxy", suffix, location); setBxy(localmesh->interpolateAndExtrapolate(Bcalc, location, extrapolate_x, @@ -757,49 +763,36 @@ void Coordinates::extrapolateChristoffelSymbols() { applyToChristoffelSymbols(interpolateAndExtrapolate_function); } -MetricTensor::FieldMetric Coordinates::recalculateJacobian() { - - // calculate Jacobian using g^-1 = det[g^ij], J = sqrt(g) - auto g = contravariantMetricTensor.g11() * contravariantMetricTensor.g22() - * contravariantMetricTensor.g33() - + 2.0 * contravariantMetricTensor.g12() * contravariantMetricTensor.g13() - * contravariantMetricTensor.g23() - - contravariantMetricTensor.g11() * contravariantMetricTensor.g23() - * contravariantMetricTensor.g23() - - contravariantMetricTensor.g22() * contravariantMetricTensor.g13() - * contravariantMetricTensor.g13() - - contravariantMetricTensor.g33() * contravariantMetricTensor.g12() - * contravariantMetricTensor.g12(); +MetricTensor::FieldMetric Coordinates::recalculateJacobian() const { - // Check that g is positive - bout::checkPositive(g, "The determinant of g^ij", "RGN_NOBNDRY"); - - return 1. / sqrt(g); -} - -MetricTensor::FieldMetric Coordinates::recalculateBxy() const { - return sqrt(g_22()) / J(); -} - -void Coordinates::jacobian() { TRACE("Coordinates::jacobian"); - - const bool extrapolate_x = not localmesh->sourceHasXBoundaryGuards(); - const bool extrapolate_y = not localmesh->sourceHasYBoundaryGuards(); - try { - - const auto jacobian = recalculateJacobian(); - // More robust to extrapolate derived quantities directly, rather than - // deriving from extrapolated covariant metric components - setJ(localmesh->interpolateAndExtrapolate(jacobian, location, extrapolate_x, - extrapolate_y, false)); + // calculate Jacobian using g^-1 = det[g^ij], J = sqrt(g) + auto g = contravariantMetricTensor.g11() * contravariantMetricTensor.g22() + * contravariantMetricTensor.g33() + + 2.0 * contravariantMetricTensor.g12() * contravariantMetricTensor.g13() + * contravariantMetricTensor.g23() + - contravariantMetricTensor.g11() * contravariantMetricTensor.g23() + * contravariantMetricTensor.g23() + - contravariantMetricTensor.g22() * contravariantMetricTensor.g13() + * contravariantMetricTensor.g13() + - contravariantMetricTensor.g33() * contravariantMetricTensor.g12() + * contravariantMetricTensor.g12(); + + // Check that g is positive + bout::checkPositive(g, "The determinant of g^ij", "RGN_NOBNDRY"); + + return 1. / sqrt(g); } catch (BoutException&) { output_error.write("\tError in jacobian call\n"); throw; } } +MetricTensor::FieldMetric Coordinates::recalculateBxy() const { + return sqrt(g_22()) / J(); +} + namespace { // Utility function for fixing up guard cells of zShift void fixZShiftGuards(Field2D& zShift) { diff --git a/tests/unit/mesh/test_coordinates.cxx b/tests/unit/mesh/test_coordinates.cxx index 24adaaa034..ccc5b44159 100644 --- a/tests/unit/mesh/test_coordinates.cxx +++ b/tests/unit/mesh/test_coordinates.cxx @@ -105,7 +105,7 @@ TEST_F(CoordinatesTest, Jacobian) { FieldMetric{0.0}, // ShiftTorsion FieldMetric{0.0}}; // IntShiftTorsion - EXPECT_NO_THROW(coords.jacobian()); + EXPECT_NO_THROW(coords.recalculateJacobian()); EXPECT_TRUE(IsFieldEqual(coords.J(), 1.0)); EXPECT_TRUE(IsFieldEqual(coords.Bxy(), 1.0)); From b08a27c7d90f75683de9ddb70dd1f08edfac407e Mon Sep 17 00:00:00 2001 From: tomc271 Date: Mon, 29 Jan 2024 17:52:04 +0000 Subject: [PATCH 319/491] Reduce code duplication by moving call to interpolateAndExtrapolate() outside of if-else statement. --- src/mesh/coordinates.cxx | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 52fe3efe51..d7b38aa2ff 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -472,22 +472,23 @@ void Coordinates::setBoundaryCells(Options* mesh_options, const std::string& suf /// Calculate Jacobian const auto jacobian = recalculateJacobian(); - // More robust to extrapolate derived quantities directly, rather than - // deriving from extrapolated covariant metric components - setJ(localmesh->interpolateAndExtrapolate(jacobian, location, extrapolate_x, - extrapolate_y, false, transform.get())); + setJ(jacobian); } else { const auto Jcalc = getAtLoc(localmesh, "J", suffix, location); - setJ(localmesh->interpolateAndExtrapolate(Jcalc, location, extrapolate_x, - extrapolate_y, false, transform.get())); - + setJ(Jcalc); // Compare calculated and loaded values output_warn.write("\tMaximum difference in J is {:e}\n", max(abs(J() - Jcalc))); auto J_value = J(); // TODO: There may be a better way communicate(J_value); } + + // More robust to extrapolate derived quantities directly, rather than + // deriving from extrapolated covariant metric components + setJ(localmesh->interpolateAndExtrapolate(J(), location, extrapolate_x, extrapolate_y, + false, transform.get())); + // Check jacobian bout::checkFinite(J(), "J" + suffix, "RGN_NOCORNERS"); bout::checkPositive(J(), "J" + suffix, "RGN_NOCORNERS"); @@ -502,14 +503,16 @@ void Coordinates::setBoundaryCells(Options* mesh_options, const std::string& suf suffix); // Re-evaluate Bxy using new J const auto Bxy = recalculateBxy(); - setBxy(localmesh->interpolateAndExtrapolate(Bxy, location, extrapolate_x, - extrapolate_y, false, transform.get())); + setBxy(Bxy); } else { const auto Bcalc = getAtLoc(localmesh, "Bxy", suffix, location); - setBxy(localmesh->interpolateAndExtrapolate(Bcalc, location, extrapolate_x, - extrapolate_y, false, transform.get())); + setBxy(Bcalc); output_warn.write("\tMaximum difference in Bxy is {:e}\n", max(abs(Bxy() - Bcalc))); } + + setBxy(localmesh->interpolateAndExtrapolate(Bxy(), location, extrapolate_x, + extrapolate_y, false, transform.get())); + // Check Bxy bout::checkFinite(Bxy(), "Bxy" + suffix, "RGN_NOCORNERS"); bout::checkPositive(Bxy(), "Bxy" + suffix, "RGN_NOCORNERS"); From 34e6e37e8fda4ac7b5f3294941d0f0127ac01312 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Mon, 29 Jan 2024 18:09:40 +0000 Subject: [PATCH 320/491] Refactor to use sourceHasVar() and getAtLoc() for IntShiftTorsion, as they are for the other variables. --- include/bout/coordinates.hxx | 1 + src/mesh/coordinates.cxx | 30 +++++++++++++++++------------- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index aa114a417a..dc45343589 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -144,6 +144,7 @@ public: /// d pitch angle / dx. Needed for vector differentials (Curl) const FieldMetric& ShiftTorsion() const; + void setShiftTorsion(FieldMetric ShiftTorsion); ///< Integrated shear (I in BOUT notation) const FieldMetric& IntShiftTorsion() const; diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index d7b38aa2ff..710ce7a289 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -523,22 +523,22 @@ void Coordinates::setBoundaryCells(Options* mesh_options, const std::string& suf ShiftTorsion_ = 0.0; } else { const auto shift_torsion = getAtLoc(localmesh, "ShiftTorsion", suffix, location, 0.0); - ShiftTorsion_ = localmesh->interpolateAndExtrapolate( - ShiftTorsion_, location, extrapolate_x, extrapolate_y, false, transform.get()); } + setShiftTorsion(localmesh->interpolateAndExtrapolate( + ShiftTorsion(), location, extrapolate_x, extrapolate_y, false, transform.get())); - ////////////////////////////////////////////////////// - - if (localmesh->IncIntShear) { - checkStaggeredGet(localmesh, "IntShiftTorsion", suffix); - if (localmesh->get(IntShiftTorsion_, "IntShiftTorsion" + suffix, 0.0, false) != 0) { - output_warn.write("\tWARNING: No Integrated torsion specified\n"); - IntShiftTorsion_ = 0.0; - } - IntShiftTorsion_.setLocation(location); - IntShiftTorsion_ = localmesh->interpolateAndExtrapolate( - IntShiftTorsion_, location, extrapolate_x, extrapolate_y, false, transform.get()); + if (!localmesh->IncIntShear) { + return; } + if (!localmesh->sourceHasVar("IntShiftTorsion" + suffix)) { + output_warn.write("\tWARNING: No Integrated torsion specified\n"); + IntShiftTorsion_ = 0.0; + } else { + const auto shift_torsion = + getAtLoc(localmesh, "IntShiftTorsion", suffix, location, 0.0); + } + setIntShiftTorsion(localmesh->interpolateAndExtrapolate( + IntShiftTorsion(), location, extrapolate_x, extrapolate_y, false, transform.get())); } FieldMetric Coordinates::getDzFromOptionsFile(Mesh* mesh, @@ -1388,6 +1388,10 @@ const FieldMetric& Coordinates::d1_dz() const { return d1_dz_; } const FieldMetric& Coordinates::ShiftTorsion() const { return ShiftTorsion_; } +void Coordinates::setShiftTorsion(FieldMetric ShiftTorsion) { + ShiftTorsion_ = std::move(ShiftTorsion); +} + const FieldMetric& Coordinates::IntShiftTorsion() const { return IntShiftTorsion_; } void Coordinates::setIntShiftTorsion(FieldMetric IntShiftTorsion) { From 12423e2fa7bb7d27d0acee59aafbb16ed6382d6f Mon Sep 17 00:00:00 2001 From: tomc271 Date: Mon, 29 Jan 2024 18:17:58 +0000 Subject: [PATCH 321/491] No need to assign to a local variable before setting property. --- src/mesh/coordinates.cxx | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 710ce7a289..89f910b19b 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -471,8 +471,7 @@ void Coordinates::setBoundaryCells(Options* mesh_options, const std::string& suf suffix); /// Calculate Jacobian - const auto jacobian = recalculateJacobian(); - setJ(jacobian); + setJ(recalculateJacobian()); } else { const auto Jcalc = getAtLoc(localmesh, "J", suffix, location); @@ -502,8 +501,7 @@ void Coordinates::setBoundaryCells(Options* mesh_options, const std::string& suf "Calculating from metric tensor\n", suffix); // Re-evaluate Bxy using new J - const auto Bxy = recalculateBxy(); - setBxy(Bxy); + setBxy(recalculateBxy()); } else { const auto Bcalc = getAtLoc(localmesh, "Bxy", suffix, location); setBxy(Bcalc); From 0ba9c0071545f4098765922d196e298070395bb7 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Mon, 29 Jan 2024 19:52:08 +0000 Subject: [PATCH 322/491] Make jacobian_cache a std::unique_ptr. Set after comparing calculated and loaded values, otherwise there will be no difference. Rename Jcalc to J_from_file. --- include/bout/coordinates.hxx | 2 +- src/mesh/coordinates.cxx | 32 +++++++++++++++++++------------- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index dc45343589..f1e68c57d8 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -339,7 +339,7 @@ private: void applyToChristoffelSymbols( const std::function& function) const; - FieldMetric jacobian_cache; + mutable std::unique_ptr jacobian_cache{nullptr}; FieldMetric Bxy_; ///< Magnitude of B = nabla z times nabla x diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 89f910b19b..3203da5e24 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -270,7 +270,6 @@ Coordinates::Coordinates(Mesh* mesh, FieldMetric dx, FieldMetric dy, FieldMetric IntShiftTorsion_(std::move(IntShiftTorsion)), contravariantMetricTensor(g11, g22, g33, g12, g13, g23), covariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23), - jacobian_cache(std::move(J)), Bxy_(std::move(Bxy)){ASSERT0(differential_operators != nullptr)}; Coordinates::Coordinates(Mesh* mesh, Options* mesh_options, const CELL_LOC loc, @@ -281,8 +280,7 @@ Coordinates::Coordinates(Mesh* mesh, Options* mesh_options, const CELL_LOC loc, ShiftTorsion_(mesh), IntShiftTorsion_(mesh), contravariantMetricTensor(1., 1., 1., 0, 0, 0, mesh), // Identity metric tensor - covariantMetricTensor(1., 1., 1., 0, 0, 0, mesh), jacobian_cache(1., mesh), - Bxy_(1., mesh) { + covariantMetricTensor(1., 1., 1., 0, 0, 0, mesh), Bxy_(1., mesh) { ASSERT0(differential_operators != nullptr) if (mesh_options == nullptr) { @@ -470,14 +468,11 @@ void Coordinates::setBoundaryCells(Options* mesh_options, const std::string& suf "\tWARNING: Jacobian 'J_{:s}' not found. Calculating from metric tensor\n", suffix); - /// Calculate Jacobian - setJ(recalculateJacobian()); - } else { - const auto Jcalc = getAtLoc(localmesh, "J", suffix, location); - setJ(Jcalc); + const auto J_from_file = getAtLoc(localmesh, "J", suffix, location); // Compare calculated and loaded values - output_warn.write("\tMaximum difference in J is {:e}\n", max(abs(J() - Jcalc))); + output_warn.write("\tMaximum difference in J is {:e}\n", max(abs(J() - J_from_file))); + setJ(J_from_file); auto J_value = J(); // TODO: There may be a better way communicate(J_value); @@ -518,7 +513,7 @@ void Coordinates::setBoundaryCells(Options* mesh_options, const std::string& suf if (!localmesh->sourceHasVar("ShiftTorsion" + suffix)) { output_warn.write("\tWARNING: No Torsion specified for zShift. " "Derivatives may not be correct\n"); - ShiftTorsion_ = 0.0; + setShiftTorsion(0.0); } else { const auto shift_torsion = getAtLoc(localmesh, "ShiftTorsion", suffix, location, 0.0); } @@ -1434,16 +1429,27 @@ const MetricTensor::FieldMetric& Coordinates::g23() const { return contravariantMetricTensor.g23(); } -const FieldMetric& Coordinates::J() const { return jacobian_cache; } +const FieldMetric& Coordinates::J() const { + if (jacobian_cache == nullptr) { + const auto j = recalculateJacobian(); + auto ptr = std::make_unique(j); + jacobian_cache = std::move(ptr); + } + return *jacobian_cache; +} void Coordinates::setJ(FieldMetric J) { //TODO: Calculate J and check value is close - jacobian_cache = std::move(J); + auto ptr = std::make_unique(J); + jacobian_cache = std::move(ptr); } void Coordinates::setJ(BoutReal value, int x, int y) { //TODO: Calculate J and check value is close - jacobian_cache(x, y) = value; + FieldMetric f; + f(x, y) = value; + auto ptr = std::make_unique(f); + jacobian_cache = std::move(ptr); } const FieldMetric& Coordinates::Bxy() const { return Bxy_; } From 81b83e49109ae3d83c0196845dd65655e41a46ae Mon Sep 17 00:00:00 2001 From: tomc271 Date: Tue, 30 Jan 2024 09:20:28 +0000 Subject: [PATCH 323/491] Bug fix: update existing value of jacobian when setting for specific x, y indices. --- src/mesh/coordinates.cxx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 3203da5e24..860269033d 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -1446,7 +1446,7 @@ void Coordinates::setJ(FieldMetric J) { void Coordinates::setJ(BoutReal value, int x, int y) { //TODO: Calculate J and check value is close - FieldMetric f; + auto f = J(); f(x, y) = value; auto ptr = std::make_unique(f); jacobian_cache = std::move(ptr); From f9ec8dcff430ad13fe9da5700c2a45a52a01ba5c Mon Sep 17 00:00:00 2001 From: tomc271 Date: Tue, 30 Jan 2024 10:08:52 +0000 Subject: [PATCH 324/491] Remove ShiftTorsion setter (to avoid problems with it not being initialised fully (no mesh)). --- include/bout/coordinates.hxx | 1 - src/mesh/coordinates.cxx | 8 ++------ 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index f1e68c57d8..9aa99decf3 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -144,7 +144,6 @@ public: /// d pitch angle / dx. Needed for vector differentials (Curl) const FieldMetric& ShiftTorsion() const; - void setShiftTorsion(FieldMetric ShiftTorsion); ///< Integrated shear (I in BOUT notation) const FieldMetric& IntShiftTorsion() const; diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 860269033d..1638c4ff04 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -513,11 +513,11 @@ void Coordinates::setBoundaryCells(Options* mesh_options, const std::string& suf if (!localmesh->sourceHasVar("ShiftTorsion" + suffix)) { output_warn.write("\tWARNING: No Torsion specified for zShift. " "Derivatives may not be correct\n"); - setShiftTorsion(0.0); + ShiftTorsion_ = (0.0); } else { const auto shift_torsion = getAtLoc(localmesh, "ShiftTorsion", suffix, location, 0.0); } - setShiftTorsion(localmesh->interpolateAndExtrapolate( + ShiftTorsion_ = (localmesh->interpolateAndExtrapolate( ShiftTorsion(), location, extrapolate_x, extrapolate_y, false, transform.get())); if (!localmesh->IncIntShear) { @@ -1381,10 +1381,6 @@ const FieldMetric& Coordinates::d1_dz() const { return d1_dz_; } const FieldMetric& Coordinates::ShiftTorsion() const { return ShiftTorsion_; } -void Coordinates::setShiftTorsion(FieldMetric ShiftTorsion) { - ShiftTorsion_ = std::move(ShiftTorsion); -} - const FieldMetric& Coordinates::IntShiftTorsion() const { return IntShiftTorsion_; } void Coordinates::setIntShiftTorsion(FieldMetric IntShiftTorsion) { From b7ac99bec59fca9a7a067c7eeaf5bcb307b899b9 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Tue, 20 Feb 2024 10:47:07 +0000 Subject: [PATCH 325/491] Extract class GValues to encapsulate G1, G2, G3 (needs renaming when we know what the name should be) and implement with same pattern as Christoffel symbols. --- CMakeLists.txt | 5 ++++- include/bout/coordinates.hxx | 14 +++++++++----- include/bout/g_values.hxx | 29 +++++++++++++++++++++++++++++ src/mesh/coordinates.cxx | 20 ++++++++++++++------ src/mesh/g_values.cxx | 15 +++++++++++++++ 5 files changed, 71 insertions(+), 12 deletions(-) create mode 100644 include/bout/g_values.hxx create mode 100644 src/mesh/g_values.cxx diff --git a/CMakeLists.txt b/CMakeLists.txt index 925d637e66..bf099fdc71 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -354,9 +354,12 @@ set(BOUT_SOURCES ./src/sys/utils.cxx ./include/bout/metricTensor.hxx ./src/mesh/metricTensor.cxx - ./include/bout/differential_operators.hxx + ./include/bout/differential_operators.hxx ./src/mesh/differential_operators.cxx + ./include/bout/christoffel_symbols.hxx ./src/mesh/christoffel_symbols.cxx + ./include/bout/g_values.hxx + ./src/mesh/g_values.cxx ${CMAKE_CURRENT_BINARY_DIR}/include/bout/revision.hxx ${CMAKE_CURRENT_BINARY_DIR}/include/bout/version.hxx ) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index 9aa99decf3..cc9a27f601 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -34,6 +34,7 @@ #define BOUT_COORDINATES_H #include "christoffel_symbols.hxx" +#include "g_values.hxx" #include "differential_operators.hxx" #include "bout/metricTensor.hxx" #include "bout/paralleltransform.hxx" @@ -275,9 +276,9 @@ public: const FieldMetric& G2() const; const FieldMetric& G3() const; - void setG1(const FieldMetric& G1); - void setG2(const FieldMetric& G2); - void setG3(const FieldMetric& G3); + void setG1(const FieldMetric& G1) const; + void setG2(const FieldMetric& G2) const; + void setG3(const FieldMetric& G3) const; const FieldMetric& Grad2_par2_DDY_invSg(CELL_LOC outloc, const std::string& method) const; @@ -286,6 +287,8 @@ public: ChristoffelSymbols& christoffel_symbols() const; + GValues& g_values() const; + void recalculateAndReset(bool recalculate_staggered, bool force_interpolate_from_centre); @@ -329,6 +332,9 @@ private: /// Christoffel symbol of the second kind (connection coefficients) mutable std::unique_ptr christoffel_symbols_cache{nullptr}; + /// `g_values` needs renaming, when we know what the name should be + mutable std::unique_ptr g_values_cache{nullptr}; + void applyToContravariantMetricTensor( const std::function& function); @@ -342,8 +348,6 @@ private: FieldMetric Bxy_; ///< Magnitude of B = nabla z times nabla x - FieldMetric G1_, G2_, G3_; - void invalidateAndRecalculateCachedVariables(); /// Set the parallel (y) transform from the options file. diff --git a/include/bout/g_values.hxx b/include/bout/g_values.hxx new file mode 100644 index 0000000000..3a47b1dc23 --- /dev/null +++ b/include/bout/g_values.hxx @@ -0,0 +1,29 @@ + +#ifndef BOUT_GVALUES_HXX +#define BOUT_GVALUES_HXX + +#include "bout/metricTensor.hxx" + +using FieldMetric = MetricTensor::FieldMetric; + +/// `GValues` needs renaming, when we know what the name should be +class GValues { + +public: + GValues(FieldMetric G1, FieldMetric G2, FieldMetric G3); + + GValues(); + + const FieldMetric& G1() const; + const FieldMetric& G2() const; + const FieldMetric& G3() const; + + void setG1(const FieldMetric& G1); + void setG2(const FieldMetric& G2); + void setG3(const FieldMetric& G3); + +private: + FieldMetric G1_, G2_, G3_; +}; + +#endif //BOUT_GVALUES_HXX diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 1638c4ff04..caada5207e 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -1323,6 +1323,14 @@ ChristoffelSymbols& Coordinates::christoffel_symbols() const { return *christoffel_symbols_cache; } +GValues& Coordinates::g_values() const { + if (g_values_cache == nullptr) { + auto ptr = std::make_unique(); + g_values_cache = std::move(ptr); + } + return *g_values_cache; +} + const Coordinates::FieldMetric& Coordinates::invSg() const { if (invSgCache == nullptr) { auto ptr = std::make_unique(); @@ -1474,13 +1482,13 @@ const FieldMetric& Coordinates::G3_12() const { return christoffel_symbols().G3_ const FieldMetric& Coordinates::G3_13() const { return christoffel_symbols().G3_13(); } const FieldMetric& Coordinates::G3_23() const { return christoffel_symbols().G3_23(); } -const FieldMetric& Coordinates::G1() const { return G1_; } -const FieldMetric& Coordinates::G2() const { return G2_; } -const FieldMetric& Coordinates::G3() const { return G3_; } +const FieldMetric& Coordinates::G1() const { return g_values().G1(); } +const FieldMetric& Coordinates::G2() const { return g_values().G2(); } +const FieldMetric& Coordinates::G3() const { return g_values().G3(); } -void Coordinates::setG1(const FieldMetric& G1) { G1_ = G1; } -void Coordinates::setG2(const FieldMetric& G2) { G2_ = G2; } -void Coordinates::setG3(const FieldMetric& G3) { G3_ = G3; } +void Coordinates::setG1(const FieldMetric& G1) const { g_values().setG1(G1); } +void Coordinates::setG2(const FieldMetric& G2) const { g_values().setG2(G2); } +void Coordinates::setG3(const FieldMetric& G3) const { g_values().setG3(G3); } void Coordinates::applyToChristoffelSymbols( const std::function& function) const { diff --git a/src/mesh/g_values.cxx b/src/mesh/g_values.cxx new file mode 100644 index 0000000000..7dccb5998e --- /dev/null +++ b/src/mesh/g_values.cxx @@ -0,0 +1,15 @@ + +#include "bout/g_values.hxx" + +GValues::GValues(FieldMetric G1, FieldMetric G2, FieldMetric G3) + : G1_(std::move(G1)), G2_(std::move(G2)), G3_(std::move(G3)){}; + +GValues::GValues() = default; + +const FieldMetric& GValues::G1() const { return G1_; } +const FieldMetric& GValues::G2() const { return G2_; } +const FieldMetric& GValues::G3() const { return G3_; } + +void GValues::setG1(const FieldMetric& G1) {} +void GValues::setG2(const FieldMetric& G2) {} +void GValues::setG3(const FieldMetric& G3) {} From 55386326515dca09300e08ccc84148b9ea30639d Mon Sep 17 00:00:00 2001 From: tomc271 Date: Tue, 20 Feb 2024 13:37:51 +0000 Subject: [PATCH 326/491] Extract methods Coordinates::communicateGValues() and Coordinates::extrapolateGValues(). Make GValues constructor take a reference to Coordinates (so that it can call methods on the Coordinates class (DDX, DDY, DDZ, communicate). Make communicate() a (public) method of Coordinates so that it can be called by the GValues constructor. This means fixZShiftGuards() also has to be a (private) method of Coordinates. --- include/bout/coordinates.hxx | 8 +++ include/bout/g_values.hxx | 2 +- src/mesh/coordinates.cxx | 123 +++++++++++++++++------------------ src/mesh/g_values.cxx | 30 +++++++-- 4 files changed, 95 insertions(+), 68 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index cc9a27f601..c49436593c 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -294,6 +294,9 @@ public: FieldMetric recalculateJacobian() const; + template + void communicate(T& t, Ts... ts) const; + private: int nz; // Size of mesh in Z. This is mesh->ngz-1 Mesh* localmesh; @@ -369,6 +372,9 @@ private: void communicateChristoffelSymbolTerms() const; void extrapolateChristoffelSymbols(); + void communicateGValues() const; + void extrapolateGValues(); + FieldMetric recalculateBxy() const; /// Non-uniform meshes. Need to use DDX, DDY @@ -380,6 +386,8 @@ private: void setBoundaryCells(Options* options, const std::string& suffix); FieldMetric getDzFromOptionsFile(Mesh* mesh, const std::string& suffix) const; + + void fixZShiftGuards(Field2D& zShift) const; }; /* diff --git a/include/bout/g_values.hxx b/include/bout/g_values.hxx index 3a47b1dc23..de11eb7625 100644 --- a/include/bout/g_values.hxx +++ b/include/bout/g_values.hxx @@ -12,7 +12,7 @@ class GValues { public: GValues(FieldMetric G1, FieldMetric G2, FieldMetric G3); - GValues(); + explicit GValues(const Coordinates& coordinates); const FieldMetric& G1() const; const FieldMetric& G2() const; diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index caada5207e..fb47e34034 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -21,17 +21,6 @@ // use anonymous namespace so this utility function is not available outside this file namespace { -template -// Use sendY()/sendX() and wait() instead of Mesh::communicate() to ensure we -// don't try to calculate parallel slices as Coordinates are not constructed yet -void communicate(T& t, Ts... ts) { - FieldGroup g(t, ts...); - auto h = t.getMesh()->sendY(g); - t.getMesh()->wait(h); - h = t.getMesh()->sendX(g); - t.getMesh()->wait(h); -} - #if BOUT_USE_METRIC_3D Field3D interpolateAndExtrapolate(const Field3D& f_, CELL_LOC location, bool extrapolate_x, bool extrapolate_y, @@ -230,6 +219,46 @@ std::string getLocationSuffix(CELL_LOC location) { } // anonymous namespace +template +// Use sendY()/sendX() and wait() instead of Mesh::communicate() to ensure we +// don't try to calculate parallel slices as Coordinates are not constructed yet +void Coordinates::communicate(T& t, Ts... ts) const { + FieldGroup g(t, ts...); + auto h = t.getMesh()->sendY(g); + t.getMesh()->wait(h); + h = t.getMesh()->sendX(g); + t.getMesh()->wait(h); +} + +// Utility function for fixing up guard cells of zShift +void Coordinates::fixZShiftGuards(Field2D& zShift) const { + auto* localmesh = zShift.getMesh(); + + // extrapolate into boundary guard cells if necessary + zShift = localmesh->interpolateAndExtrapolate( + zShift, zShift.getLocation(), not localmesh->sourceHasXBoundaryGuards(), + not localmesh->sourceHasYBoundaryGuards(), false); + + // make sure zShift has been communicated + communicate(zShift); + + // Correct guard cells for discontinuity of zShift at poloidal branch cut + for (int x = 0; x < localmesh->LocalNx; x++) { + const auto lower = localmesh->hasBranchCutLower(x); + if (lower.first) { + for (int y = 0; y < localmesh->ystart; y++) { + zShift(x, y) -= lower.second; + } + } + const auto upper = localmesh->hasBranchCutUpper(x); + if (upper.first) { + for (int y = localmesh->yend + 1; y < localmesh->LocalNy; y++) { + zShift(x, y) += upper.second; + } + } + } +} + Coordinates::FieldMetric Coordinates::getAtLocOrUnaligned(Mesh* mesh, const std::string& name, BoutReal default_value, @@ -631,25 +660,9 @@ void Coordinates::recalculateAndReset(bool recalculate_staggered, communicateChristoffelSymbolTerms(); extrapolateChristoffelSymbols(); - auto tmp = J() * g12(); - communicate(tmp); - setG1((DDX(J() * g11()) + DDY(tmp) + DDZ(J() * g13())) / J()); - tmp = J() * g22(); - communicate(tmp); - setG2((DDX(J() * g12()) + DDY(tmp) + DDZ(J() * g23())) / J()); - tmp = J() * g23(); - communicate(tmp); - setG3((DDX(J() * g13()) + DDY(tmp) + DDZ(J() * g33())) / J()); - - setG1(localmesh->interpolateAndExtrapolate(G1(), location, true, true, true, - transform.get())); - setG2(localmesh->interpolateAndExtrapolate(G2(), location, true, true, true, - transform.get())); - setG3(localmesh->interpolateAndExtrapolate(G3(), location, true, true, true, - transform.get())); - - auto temp = G1(); // TODO: There must be a better way than this! - communicate(temp, G2(), G3()); + g_values_cache.reset(); + communicateGValues(); + extrapolateGValues(); correctionForNonUniformMeshes(force_interpolate_from_centre); @@ -759,6 +772,21 @@ void Coordinates::extrapolateChristoffelSymbols() { applyToChristoffelSymbols(interpolateAndExtrapolate_function); } +void Coordinates::communicateGValues() const { + auto temp = G1(); // TODO: There must be a better way than this! + communicate(temp, G2(), G3()); +} + +void Coordinates::extrapolateGValues() { + + setG1(localmesh->interpolateAndExtrapolate(G1(), location, true, true, true, + transform.get())); + setG2(localmesh->interpolateAndExtrapolate(G2(), location, true, true, true, + transform.get())); + setG3(localmesh->interpolateAndExtrapolate(G3(), location, true, true, true, + transform.get())); +} + MetricTensor::FieldMetric Coordinates::recalculateJacobian() const { TRACE("Coordinates::jacobian"); @@ -789,37 +817,6 @@ MetricTensor::FieldMetric Coordinates::recalculateBxy() const { return sqrt(g_22()) / J(); } -namespace { -// Utility function for fixing up guard cells of zShift -void fixZShiftGuards(Field2D& zShift) { - auto* localmesh = zShift.getMesh(); - - // extrapolate into boundary guard cells if necessary - zShift = localmesh->interpolateAndExtrapolate( - zShift, zShift.getLocation(), not localmesh->sourceHasXBoundaryGuards(), - not localmesh->sourceHasYBoundaryGuards(), false); - - // make sure zShift has been communicated - communicate(zShift); - - // Correct guard cells for discontinuity of zShift at poloidal branch cut - for (int x = 0; x < localmesh->LocalNx; x++) { - const auto lower = localmesh->hasBranchCutLower(x); - if (lower.first) { - for (int y = 0; y < localmesh->ystart; y++) { - zShift(x, y) -= lower.second; - } - } - const auto upper = localmesh->hasBranchCutUpper(x); - if (upper.first) { - for (int y = localmesh->yend + 1; y < localmesh->LocalNy; y++) { - zShift(x, y) += upper.second; - } - } - } -} -} // namespace - void Coordinates::setParallelTransform(Options* mesh_options) { auto* ptoptions = mesh_options->getSection("paralleltransform"); @@ -1325,7 +1322,7 @@ ChristoffelSymbols& Coordinates::christoffel_symbols() const { GValues& Coordinates::g_values() const { if (g_values_cache == nullptr) { - auto ptr = std::make_unique(); + auto ptr = std::make_unique(*this); g_values_cache = std::move(ptr); } return *g_values_cache; @@ -1545,4 +1542,4 @@ void Coordinates::invalidateAndRecalculateCachedVariables() { zlength_cache.reset(); Grad2_par2_DDY_invSgCache.clear(); invSgCache.reset(); -} \ No newline at end of file +} diff --git a/src/mesh/g_values.cxx b/src/mesh/g_values.cxx index 7dccb5998e..cf1b919161 100644 --- a/src/mesh/g_values.cxx +++ b/src/mesh/g_values.cxx @@ -1,15 +1,37 @@ #include "bout/g_values.hxx" +#include "bout/coordinates.hxx" GValues::GValues(FieldMetric G1, FieldMetric G2, FieldMetric G3) : G1_(std::move(G1)), G2_(std::move(G2)), G3_(std::move(G3)){}; -GValues::GValues() = default; +GValues::GValues(const Coordinates& coordinates) { + + const auto& contravariantMetricTensor = coordinates.getContravariantMetricTensor(); + const auto& J = coordinates.J(); + + const auto& g11 = contravariantMetricTensor.g11(); + const auto& g22 = contravariantMetricTensor.g22(); + const auto& g33 = contravariantMetricTensor.g33(); + const auto& g12 = contravariantMetricTensor.g12(); + const auto& g13 = contravariantMetricTensor.g13(); + const auto& g23 = contravariantMetricTensor.g23(); + + auto tmp = J * g12; + coordinates.communicate(tmp); + setG1((coordinates.DDX(J * g11) + coordinates.DDY(tmp) + coordinates.DDZ(J * g13)) / J); + tmp = J * g22; + coordinates.communicate(tmp); + setG2((coordinates.DDX(J * g12) + coordinates.DDY(tmp) + coordinates.DDZ(J * g23)) / J); + tmp = J * g23; + coordinates.communicate(tmp); + setG3((coordinates.DDX(J * g13) + coordinates.DDY(tmp) + coordinates.DDZ(J * g33)) / J); +} const FieldMetric& GValues::G1() const { return G1_; } const FieldMetric& GValues::G2() const { return G2_; } const FieldMetric& GValues::G3() const { return G3_; } -void GValues::setG1(const FieldMetric& G1) {} -void GValues::setG2(const FieldMetric& G2) {} -void GValues::setG3(const FieldMetric& G3) {} +void GValues::setG1(const FieldMetric& G1) { G1_ = G1; } +void GValues::setG2(const FieldMetric& G2) { G2_ = G2; } +void GValues::setG3(const FieldMetric& G3) { G3_ = G3; } From e254b98172263002aedca29616adb822ebf24c81 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Tue, 20 Feb 2024 15:08:05 +0000 Subject: [PATCH 327/491] Remove redundant MetricTensor::Allocate() method. --- include/bout/metricTensor.hxx | 2 -- src/mesh/metricTensor.cxx | 20 +++----------------- 2 files changed, 3 insertions(+), 19 deletions(-) diff --git a/include/bout/metricTensor.hxx b/include/bout/metricTensor.hxx index 4dfb1e3d0e..667e25925a 100644 --- a/include/bout/metricTensor.hxx +++ b/include/bout/metricTensor.hxx @@ -33,8 +33,6 @@ public: void setMetricTensor(const MetricTensor& metric_tensor); - void Allocate(); - void setLocation(CELL_LOC location); MetricTensor inverse(const std::string& region = "RGN_ALL"); diff --git a/src/mesh/metricTensor.cxx b/src/mesh/metricTensor.cxx index e5deeafb1d..13340fdb7d 100644 --- a/src/mesh/metricTensor.cxx +++ b/src/mesh/metricTensor.cxx @@ -1,23 +1,18 @@ -#include #include "bout/metricTensor.hxx" #include "bout/output.hxx" +#include MetricTensor::MetricTensor(FieldMetric g11, FieldMetric g22, FieldMetric g33, FieldMetric g12, FieldMetric g13, FieldMetric g23) : g11_(std::move(g11)), g22_(std::move(g22)), g33_(std::move(g33)), - g12_(std::move(g12)), g13_(std::move(g13)), g23_(std::move(g23)) { - Allocate(); // Make sure metric elements are allocated // ; TODO: Required? -} + g12_(std::move(g12)), g13_(std::move(g13)), g23_(std::move(g23)) {} MetricTensor::MetricTensor(const BoutReal g11, const BoutReal g22, const BoutReal g33, const BoutReal g12, const BoutReal g13, const BoutReal g23, Mesh* mesh) : g11_(g11, mesh), g22_(g22, mesh), g33_(g33, mesh), g12_(g12, mesh), g13_(g13, mesh), - g23_(g23, mesh) { - - Allocate(); // Make sure metric elements are allocated // ; TODO: Required? -} + g23_(g23, mesh) {} const MetricTensor::FieldMetric& MetricTensor::g11() const { return g11_; } const MetricTensor::FieldMetric& MetricTensor::g22() const { return g22_; } @@ -88,15 +83,6 @@ void MetricTensor::check(int ystart) { } } -void MetricTensor::Allocate() { // ; TODO: Required? - g11_.allocate(); - g22_.allocate(); - g33_.allocate(); - g12_.allocate(); - g13_.allocate(); - g23_.allocate(); -} - void MetricTensor::setLocation(const CELL_LOC location) { g11_.setLocation(location); g22_.setLocation(location); From 9ef7b74221515ad4f32974789d39ae4a98596d91 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Tue, 20 Feb 2024 15:10:11 +0000 Subject: [PATCH 328/491] Fix additional strings after renaming "MetricTensor::CalculateOppositeRepresentation" to "MetricTensor::inverse". --- src/mesh/metricTensor.cxx | 2 +- tools/pylib/_boutpp_build/boutcpp.pxd.jinja | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/mesh/metricTensor.cxx b/src/mesh/metricTensor.cxx index 13340fdb7d..b61529fa3d 100644 --- a/src/mesh/metricTensor.cxx +++ b/src/mesh/metricTensor.cxx @@ -94,7 +94,7 @@ void MetricTensor::setLocation(const CELL_LOC location) { MetricTensor MetricTensor::inverse(const std::string& region) { - TRACE("MetricTensor::CalculateOppositeRepresentation"); + TRACE("MetricTensor::inverse"); // Perform inversion of g{ij} to get g^{ij}, or vice versa // NOTE: Currently this bit assumes that metric terms are Field2D objects diff --git a/tools/pylib/_boutpp_build/boutcpp.pxd.jinja b/tools/pylib/_boutpp_build/boutcpp.pxd.jinja index fe82f05adf..02ce267d49 100644 --- a/tools/pylib/_boutpp_build/boutcpp.pxd.jinja +++ b/tools/pylib/_boutpp_build/boutcpp.pxd.jinja @@ -79,8 +79,8 @@ cdef extern from "bout/coordinates.hxx": {{ metric_field }} ShiftTorsion {{ metric_field }} IntShiftTorsion int communicateAndCheckMeshSpacing() - int CalculateOppositeRepresentation() - int CalculateOppositeRepresentation() + int inverse() + int inverse() int jacobian() cdef extern from "bout/fieldgroup.hxx": From 14887ffafb0270094ba7e2f47401ca89fc697472 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Tue, 20 Feb 2024 16:40:29 +0000 Subject: [PATCH 329/491] Define two derived classes CovariantMetricTensor and CovariantMetricTensor that inherit from MetricTensor. --- examples/2Dturbulence_multigrid/esel.cxx | 4 +-- examples/6field-simple/elm_6f.cxx | 4 +-- examples/conducting-wall-mode/cwm.cxx | 4 +-- examples/constraints/alfven-wave/alfven.cxx | 4 +-- examples/dalf3/dalf3.cxx | 4 +-- .../elm-pb-outerloop/elm_pb_outerloop.cxx | 4 +-- examples/elm-pb/elm_pb.cxx | 4 +-- examples/em-drift/2fluid.cxx | 4 +-- examples/gyro-gem/gem.cxx | 4 +-- examples/jorek-compare/jorek_compare.cxx | 4 +-- examples/lapd-drift/lapd_drift.cxx | 4 +-- examples/laplacexy/alfven-wave/alfven.cxx | 4 +-- examples/laplacexy/laplace_perp/test.cxx | 4 +-- examples/reconnect-2field/2field.cxx | 4 +-- examples/shear-alfven-wave/2fluid.cxx | 4 +-- examples/tokamak-2fluid/2fluid.cxx | 4 +-- examples/uedge-benchmark/ue_bmark.cxx | 4 +-- examples/wave-slab/wave_slab.cxx | 4 +-- include/bout/coordinates.hxx | 14 +++++----- include/bout/metricTensor.hxx | 28 +++++++++++++++++++ src/mesh/coordinates.cxx | 18 ++++++------ tests/MMS/GBS/gbs.cxx | 4 +-- tests/MMS/diffusion/diffusion.cxx | 5 ++-- tests/MMS/elm-pb/elm_pb.cxx | 4 +-- tests/MMS/spatial/diffusion/diffusion.cxx | 4 +-- tests/MMS/tokamak/tokamak.cxx | 4 +-- tests/MMS/wave-1d/wave.cxx | 5 ++-- .../test-drift-instability/2fluid.cxx | 4 +-- .../test-interchange-instability/2fluid.cxx | 4 +-- .../integrated/test-laplacexy/loadmetric.cxx | 4 +-- .../test-laplacexz/test-laplacexz.cxx | 4 +-- tests/unit/mesh/test_coordinates.cxx | 9 +++--- 32 files changed, 107 insertions(+), 76 deletions(-) diff --git a/examples/2Dturbulence_multigrid/esel.cxx b/examples/2Dturbulence_multigrid/esel.cxx index 821a0fce3d..fd94808f58 100644 --- a/examples/2Dturbulence_multigrid/esel.cxx +++ b/examples/2Dturbulence_multigrid/esel.cxx @@ -78,7 +78,7 @@ class ESEL : public PhysicsModel { g12 = 0.0; g13 = 0.0; g23 = 0.0; - coord->setContravariantMetricTensor(MetricTensor(g11, g22, g33, g12, g13, g23)); + coord->setContravariantMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); MetricTensor::FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; g_11 = 1.0; @@ -87,7 +87,7 @@ class ESEL : public PhysicsModel { g_12 = 0.0; g_13 = 0.0; g_23 = 0.0; - coord->setCovariantMetricTensor(MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + coord->setCovariantMetricTensor(CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); SOLVE_FOR(N, vort); SAVE_REPEAT(phi); diff --git a/examples/6field-simple/elm_6f.cxx b/examples/6field-simple/elm_6f.cxx index f1e6659aec..c7d63122a3 100644 --- a/examples/6field-simple/elm_6f.cxx +++ b/examples/6field-simple/elm_6f.cxx @@ -1058,7 +1058,7 @@ class Elm_6f : public PhysicsModel { g12 = 0.0; g13 = -I * coord->g11(); g23 = -Btxy / (hthe * Bpxy * Rxy); - coord->setContravariantMetricTensor(MetricTensor(g11, g22, g33, g12, g13, g23)); + coord->setContravariantMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); coord->setJ(hthe / Bpxy); coord->setBxy(B0); @@ -1070,7 +1070,7 @@ class Elm_6f : public PhysicsModel { g_12 = Btxy * hthe * I * Rxy / Bpxy; g_13 = I * Rxy * Rxy; g_23 = Btxy * hthe * Rxy / Bpxy; - coord->setCovariantMetricTensor(MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + coord->setCovariantMetricTensor(CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); // Set B field vector diff --git a/examples/conducting-wall-mode/cwm.cxx b/examples/conducting-wall-mode/cwm.cxx index 4f48f2c6c8..f45af512c7 100644 --- a/examples/conducting-wall-mode/cwm.cxx +++ b/examples/conducting-wall-mode/cwm.cxx @@ -195,7 +195,7 @@ class CWM : public PhysicsModel { g12 = 0.0; g13 = -I * coord->g11(); g23 = -Btxy / (hthe * Bpxy * Rxy); - coord->setContravariantMetricTensor(MetricTensor(g11, g22, g33, g12, g13, g23)); + coord->setContravariantMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); coord->setJ(hthe / Bpxy); @@ -206,7 +206,7 @@ class CWM : public PhysicsModel { g_12 = Btxy * hthe * I * Rxy / Bpxy; g_13 = I * Rxy * Rxy; g_23 = Btxy * hthe * Rxy / Bpxy; - coord->setCovariantMetricTensor(MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + coord->setCovariantMetricTensor(CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); /**************** SET EVOLVING VARIABLES *************/ diff --git a/examples/constraints/alfven-wave/alfven.cxx b/examples/constraints/alfven-wave/alfven.cxx index 928bc3a1ac..a159e7d350 100644 --- a/examples/constraints/alfven-wave/alfven.cxx +++ b/examples/constraints/alfven-wave/alfven.cxx @@ -208,7 +208,7 @@ class Alfven : public PhysicsModel { g12 = 0.0; g13 = -sinty * coord->g11(); g23 = -sbp * Btxy / (hthe * Bpxy * Rxy); - coord->setContravariantMetricTensor(MetricTensor(g11, g22, g33, g12, g13, g23)); + coord->setContravariantMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); coord->setJ(hthe / Bpxy); @@ -219,7 +219,7 @@ class Alfven : public PhysicsModel { g_12 = sbp * Btxy * hthe * sinty * Rxy / Bpxy; g_13 = sinty * Rxy * Rxy; g_23 = sbp * Btxy * hthe * Rxy / Bpxy; - coord->setCovariantMetricTensor(MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + coord->setCovariantMetricTensor(CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); } }; diff --git a/examples/dalf3/dalf3.cxx b/examples/dalf3/dalf3.cxx index 2869404907..dc5de9bbdd 100644 --- a/examples/dalf3/dalf3.cxx +++ b/examples/dalf3/dalf3.cxx @@ -260,7 +260,7 @@ class DALF3 : public PhysicsModel { g12 = 0.0; g13 = -I * coord->g11(); g23 = -Btxy / (hthe * Bpxy * Rxy); - coord->setContravariantMetricTensor(MetricTensor(g11, g22, g33, g12, g13, g23)); + coord->setContravariantMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); coord->setJ(hthe / Bpxy); coord->setBxy(B0); @@ -272,7 +272,7 @@ class DALF3 : public PhysicsModel { g_12 = Btxy * hthe * I * Rxy / Bpxy; g_13 = I * Rxy * Rxy; g_23 = Btxy * hthe * Rxy / Bpxy; - coord->setCovariantMetricTensor(MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + coord->setCovariantMetricTensor(CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); SOLVE_FOR3(Vort, Pe, Vpar); comms.add(Vort, Pe, Vpar); diff --git a/examples/elm-pb-outerloop/elm_pb_outerloop.cxx b/examples/elm-pb-outerloop/elm_pb_outerloop.cxx index 12c6a436a5..4c87b45aa2 100644 --- a/examples/elm-pb-outerloop/elm_pb_outerloop.cxx +++ b/examples/elm-pb-outerloop/elm_pb_outerloop.cxx @@ -1099,7 +1099,7 @@ class ELMpb : public PhysicsModel { g12 = 0.0; g13 = -I * metric->g11(); g23 = -Btxy / (hthe * Bpxy * Rxy); - metric->setContravariantMetricTensor(MetricTensor(g11, g22, g33, g12, g13, g23)); + metric->setContravariantMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); metric->setJ(hthe / Bpxy); metric->setBxy(B0); @@ -1111,7 +1111,7 @@ class ELMpb : public PhysicsModel { g_12 = Btxy * hthe * I * Rxy / Bpxy; g_13 = I * Rxy * Rxy; g_23 = Btxy * hthe * Rxy / Bpxy; - metric->setCovariantMetricTensor(MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + metric->setCovariantMetricTensor(CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); // Set B field vector diff --git a/examples/elm-pb/elm_pb.cxx b/examples/elm-pb/elm_pb.cxx index 21ec924129..fb8bfee46b 100644 --- a/examples/elm-pb/elm_pb.cxx +++ b/examples/elm-pb/elm_pb.cxx @@ -1040,7 +1040,7 @@ class ELMpb : public PhysicsModel { g12 = 0.0; g13 = -I * metric->g11(); g23 = -Btxy / (hthe * Bpxy * Rxy); - metric->setContravariantMetricTensor(MetricTensor(g11, g22, g33, g12, g13, g23)); + metric->setContravariantMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); metric->setJ(hthe / Bpxy); metric->setBxy(B0); @@ -1052,7 +1052,7 @@ class ELMpb : public PhysicsModel { g_12 = Btxy * hthe * I * Rxy / Bpxy; g_13 = I * Rxy * Rxy; g_23 = Btxy * hthe * Rxy / Bpxy; - metric->setCovariantMetricTensor(MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + metric->setCovariantMetricTensor(CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); // Set B field vector diff --git a/examples/em-drift/2fluid.cxx b/examples/em-drift/2fluid.cxx index 5443b9c22c..4bd7703608 100644 --- a/examples/em-drift/2fluid.cxx +++ b/examples/em-drift/2fluid.cxx @@ -185,7 +185,7 @@ class EMdrift : public PhysicsModel { g12 = 0.0; g13 = -I * coord->g11(); g23 = -Btxy / (hthe * Bpxy * Rxy); - coord->setContravariantMetricTensor(MetricTensor(g11, g22, g33, g12, g13, g23)); + coord->setContravariantMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); coord->setJ(hthe / Bpxy); @@ -196,7 +196,7 @@ class EMdrift : public PhysicsModel { g_12 = Btxy * hthe * I * Rxy / Bpxy; g_13 = I * Rxy * Rxy; g_23 = Btxy * hthe * Rxy / Bpxy; - coord->setCovariantMetricTensor(MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + coord->setCovariantMetricTensor(CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); /**************** SET EVOLVING VARIABLES *************/ diff --git a/examples/gyro-gem/gem.cxx b/examples/gyro-gem/gem.cxx index d489d693b1..20c1d180dd 100644 --- a/examples/gyro-gem/gem.cxx +++ b/examples/gyro-gem/gem.cxx @@ -376,7 +376,7 @@ class GEM : public PhysicsModel { g12 = 0.0; g13 = 0.; g23 = -Btxy / (hthe * Bpxy * Rxy); - coord->setContravariantMetricTensor(MetricTensor(g11, g22, g33, g12, g13, g23)); + coord->setContravariantMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); coord->setJ(hthe / Bpxy); coord->setBxy(Bxy); @@ -388,7 +388,7 @@ class GEM : public PhysicsModel { g_12 = 0.; g_13 = 0.; g_23 = Btxy * hthe * Rxy / Bpxy; - coord->setCovariantMetricTensor(MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + coord->setCovariantMetricTensor(CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); // Set B field vector diff --git a/examples/jorek-compare/jorek_compare.cxx b/examples/jorek-compare/jorek_compare.cxx index 9fe706ab4b..d15df9ef72 100644 --- a/examples/jorek-compare/jorek_compare.cxx +++ b/examples/jorek-compare/jorek_compare.cxx @@ -306,7 +306,7 @@ class Jorek : public PhysicsModel { g12 = 0.0; g13 = -I * coord->g11(); g23 = -Btxy / (hthe * Bpxy * Rxy); - coord->setContravariantMetricTensor(MetricTensor(g11, g22, g33, g12, g13, g23)); + coord->setContravariantMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); coord->setJ(hthe / Bpxy); coord->setBxy(B0); @@ -318,7 +318,7 @@ class Jorek : public PhysicsModel { g_12 = Btxy * hthe * I * Rxy / Bpxy; g_13 = I * Rxy * Rxy; g_23 = Btxy * hthe * Rxy / Bpxy; - coord->setCovariantMetricTensor(MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + coord->setCovariantMetricTensor(CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); // Set B field vector B0vec.covariant = false; diff --git a/examples/lapd-drift/lapd_drift.cxx b/examples/lapd-drift/lapd_drift.cxx index cfe1fc9a05..0ea9b85f8f 100644 --- a/examples/lapd-drift/lapd_drift.cxx +++ b/examples/lapd-drift/lapd_drift.cxx @@ -339,7 +339,7 @@ class LAPDdrift : public PhysicsModel { g12 = 0.0; g13 = -I * coord->g11(); g23 = -Btxy / (hthe * Bpxy * Rxy); - coord->setContravariantMetricTensor(MetricTensor(g11, g22, g33, g12, g13, g23)); + coord->setContravariantMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); coord->setJ(hthe / Bpxy); @@ -350,7 +350,7 @@ class LAPDdrift : public PhysicsModel { g_12 = Btxy * hthe * I * Rxy / Bpxy; g_13 = I * Rxy * Rxy; g_23 = Btxy * hthe * Rxy / Bpxy; - coord->setCovariantMetricTensor(MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + coord->setCovariantMetricTensor(CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); rho0 = Ni0 * Delp2(phi0) + Perp_Grad_dot_Grad(phi0, Ni0); diff --git a/examples/laplacexy/alfven-wave/alfven.cxx b/examples/laplacexy/alfven-wave/alfven.cxx index 799a467ed1..a32dc0a045 100644 --- a/examples/laplacexy/alfven-wave/alfven.cxx +++ b/examples/laplacexy/alfven-wave/alfven.cxx @@ -209,7 +209,7 @@ class Alfven : public PhysicsModel { g12 = 0.0; g13 = -sinty * coord->g11(); g23 = -sbp * Btxy / (hthe * Bpxy * Rxy); - coord->setContravariantMetricTensor(MetricTensor(g11, g22, g33, g12, g13, g23)); + coord->setContravariantMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); coord->setJ(hthe / Bpxy); @@ -220,7 +220,7 @@ class Alfven : public PhysicsModel { g_12 = sbp * Btxy * hthe * sinty * Rxy / Bpxy; g_13 = sinty * Rxy * Rxy; g_23 = sbp * Btxy * hthe * Rxy / Bpxy; - coord->setCovariantMetricTensor(MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + coord->setCovariantMetricTensor(CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); } }; diff --git a/examples/laplacexy/laplace_perp/test.cxx b/examples/laplacexy/laplace_perp/test.cxx index 174ac1e48d..23c040012c 100644 --- a/examples/laplacexy/laplace_perp/test.cxx +++ b/examples/laplacexy/laplace_perp/test.cxx @@ -32,7 +32,7 @@ int main(int argc, char** argv) { g12 = 0.0; g13 = -I * coord->g11(); g23 = -Btxy / (hthe * Bpxy * Rxy); - coord->setContravariantMetricTensor(MetricTensor(g11, g22, g33, g12, g13, g23)); + coord->setContravariantMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); coord->setJ(hthe / Bpxy); coord->setBxy(B0); @@ -44,7 +44,7 @@ int main(int argc, char** argv) { g_12 = Btxy * hthe * I * Rxy / Bpxy; g_13 = I * Rxy * Rxy; g_23 = Btxy * hthe * Rxy / Bpxy; - coord->setCovariantMetricTensor(MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + coord->setCovariantMetricTensor(CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); } /////////////////////////////////////// diff --git a/examples/reconnect-2field/2field.cxx b/examples/reconnect-2field/2field.cxx index 25788397e0..ccd5bc3b55 100644 --- a/examples/reconnect-2field/2field.cxx +++ b/examples/reconnect-2field/2field.cxx @@ -170,7 +170,7 @@ class TwoField : public PhysicsModel { g12 = 0.0; g13 = 0.; g23 = -Btxy / (hthe * Bpxy * Rxy); - coord->setContravariantMetricTensor(MetricTensor(g11, g22, g33, g12, g13, g23)); + coord->setContravariantMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); coord->setJ(hthe / Bpxy); @@ -181,7 +181,7 @@ class TwoField : public PhysicsModel { g_12 = 0.; g_13 = 0.; g_23 = Btxy * hthe * Rxy / Bpxy; - coord->setCovariantMetricTensor(MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + coord->setCovariantMetricTensor(CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); // Tell BOUT++ which variables to evolve SOLVE_FOR(U, Apar); diff --git a/examples/shear-alfven-wave/2fluid.cxx b/examples/shear-alfven-wave/2fluid.cxx index b3faa9cf50..c281ad6bc3 100644 --- a/examples/shear-alfven-wave/2fluid.cxx +++ b/examples/shear-alfven-wave/2fluid.cxx @@ -189,7 +189,7 @@ class ShearAlfven : public PhysicsModel { g12 = 0.0; g13 = -I * coord->g11(); g23 = -Btxy / (hthe * Bpxy * Rxy); - coord->setContravariantMetricTensor(MetricTensor(g11, g22, g33, g12, g13, g23)); + coord->setContravariantMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); coord->setJ(hthe / Bpxy); @@ -200,7 +200,7 @@ class ShearAlfven : public PhysicsModel { g_12 = Btxy * hthe * I * Rxy / Bpxy; g_13 = I * Rxy * Rxy; g_23 = Btxy * hthe * Rxy / Bpxy; - coord->setCovariantMetricTensor(MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + coord->setCovariantMetricTensor(CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); /**************** SET EVOLVING VARIABLES *************/ diff --git a/examples/tokamak-2fluid/2fluid.cxx b/examples/tokamak-2fluid/2fluid.cxx index 6d86b82f60..195efc8e7d 100644 --- a/examples/tokamak-2fluid/2fluid.cxx +++ b/examples/tokamak-2fluid/2fluid.cxx @@ -406,7 +406,7 @@ class TwoFluid : public PhysicsModel { g12 = 0.0; g13 = -I * coord->g11(); g23 = -Btxy / (hthe * Bpxy * Rxy); - coord->setContravariantMetricTensor(MetricTensor(g11, g22, g33, g12, g13, g23)); + coord->setContravariantMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); coord->setJ(hthe / Bpxy); @@ -417,7 +417,7 @@ class TwoFluid : public PhysicsModel { g_12 = Btxy * hthe * I * Rxy / Bpxy; g_13 = I * Rxy * Rxy; g_23 = Btxy * hthe * Rxy / Bpxy; - coord->setCovariantMetricTensor(MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + coord->setCovariantMetricTensor(CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); //////////////////////////////////////////////////////// // SET EVOLVING VARIABLES diff --git a/examples/uedge-benchmark/ue_bmark.cxx b/examples/uedge-benchmark/ue_bmark.cxx index 6986968c19..61586de79c 100644 --- a/examples/uedge-benchmark/ue_bmark.cxx +++ b/examples/uedge-benchmark/ue_bmark.cxx @@ -147,7 +147,7 @@ class UedgeBenchmark : public PhysicsModel { g12 = 0.0; g13 = 0.0; g23 = -Btxy / (hthe * Bpxy * Rxy); - coords->setContravariantMetricTensor(MetricTensor(g11, g22, g33, g12, g13, g23)); + coords->setContravariantMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); coords->setJ(hthe / Bpxy); @@ -158,7 +158,7 @@ class UedgeBenchmark : public PhysicsModel { g_12 = 0.0; g_13 = 0.0; g_23 = Btxy * hthe * Rxy / Bpxy; - coords->setCovariantMetricTensor(MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + coords->setCovariantMetricTensor(CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); //////////////// BOUNDARIES /////////////////////// // diff --git a/examples/wave-slab/wave_slab.cxx b/examples/wave-slab/wave_slab.cxx index fd9692a2c5..f31a3c7c6a 100644 --- a/examples/wave-slab/wave_slab.cxx +++ b/examples/wave-slab/wave_slab.cxx @@ -38,7 +38,7 @@ class WaveTest : public PhysicsModel { g12 = 0.0; g13 = -I * coords->g11(); g23 = -Btxy / (hthe * Bpxy * Rxy); - coords->setContravariantMetricTensor(MetricTensor(g11, g22, g33, g12, g13, g23)); + coords->setContravariantMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); coords->setJ(hthe / Bpxy); @@ -49,7 +49,7 @@ class WaveTest : public PhysicsModel { g_12 = Btxy * hthe * I * Rxy / Bpxy; g_13 = I * Rxy * Rxy; g_23 = Btxy * hthe * Rxy / Bpxy; - coords->setCovariantMetricTensor(MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + coords->setCovariantMetricTensor(CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); solver->add(f, "f"); solver->add(g, "g"); diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index c49436593c..1dfdd08de5 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -34,8 +34,8 @@ #define BOUT_COORDINATES_H #include "christoffel_symbols.hxx" -#include "g_values.hxx" #include "differential_operators.hxx" +#include "g_values.hxx" #include "bout/metricTensor.hxx" #include "bout/paralleltransform.hxx" @@ -119,15 +119,15 @@ public: const FieldMetric& g13() const; const FieldMetric& g23() const; - const MetricTensor& getContravariantMetricTensor() const; - const MetricTensor& getCovariantMetricTensor() const; + const ContravariantMetricTensor& getContravariantMetricTensor() const; + const CovariantMetricTensor& getCovariantMetricTensor() const; - void setContravariantMetricTensor(const MetricTensor& metric_tensor, + void setContravariantMetricTensor(const ContravariantMetricTensor& metric_tensor, const std::string& region = "RGN_ALL", bool recalculate_staggered = true, bool force_interpolate_from_centre = false); - void setCovariantMetricTensor(const MetricTensor& metric_tensor, + void setCovariantMetricTensor(const CovariantMetricTensor& metric_tensor, const std::string& region = "RGN_ALL", bool recalculate_staggered = true, bool force_interpolate_from_centre = false); @@ -329,8 +329,8 @@ private: mutable std::map> Grad2_par2_DDY_invSgCache; mutable std::unique_ptr invSgCache{nullptr}; - MetricTensor contravariantMetricTensor; - MetricTensor covariantMetricTensor; + ContravariantMetricTensor contravariantMetricTensor; + CovariantMetricTensor covariantMetricTensor; /// Christoffel symbol of the second kind (connection coefficients) mutable std::unique_ptr christoffel_symbols_cache{nullptr}; diff --git a/include/bout/metricTensor.hxx b/include/bout/metricTensor.hxx index 667e25925a..26a26b01a8 100644 --- a/include/bout/metricTensor.hxx +++ b/include/bout/metricTensor.hxx @@ -47,4 +47,32 @@ protected: FieldMetric g11_, g22_, g33_, g12_, g13_, g23_; }; +class CovariantMetricTensor : public MetricTensor { + +public: + CovariantMetricTensor(FieldMetric g11, FieldMetric g22, FieldMetric g33, + FieldMetric g12, FieldMetric g13, FieldMetric g23) + : MetricTensor(std::move(g11), std::move(g22), std::move(g33), std::move(g12), + std::move(g13), std::move(g23)){}; + + CovariantMetricTensor(const BoutReal g11, const BoutReal g22, const BoutReal g33, + const BoutReal g12, const BoutReal g13, const BoutReal g23, + Mesh* mesh) + : MetricTensor(g11, g22, g33, g12, g13, g23, mesh){}; +}; + +class ContravariantMetricTensor : public MetricTensor { + +public: + ContravariantMetricTensor(FieldMetric g_11, FieldMetric g_22, FieldMetric g_33, + FieldMetric g_12, FieldMetric g_13, FieldMetric g_23) + : MetricTensor(std::move(g_11), std::move(g_22), std::move(g_33), std::move(g_12), + std::move(g_13), std::move(g_23)){}; + + ContravariantMetricTensor(const BoutReal g_11, const BoutReal g_22, const BoutReal g_33, + const BoutReal g_12, const BoutReal g_13, const BoutReal g_23, + Mesh* mesh) + : MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23, mesh){}; +}; + #endif //BOUT_METRICTENSOR_HXX diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index fb47e34034..c0d77ce6e6 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -441,7 +441,8 @@ void Coordinates::setBoundaryCells(Options* mesh_options, const std::string& suf g12 = getAtLocOrUnaligned(localmesh, "g12", 0.0, suffix, location); g13 = getAtLocOrUnaligned(localmesh, "g13", 0.0, suffix, location); g23 = getAtLocOrUnaligned(localmesh, "g23", 0.0, suffix, location); - contravariantMetricTensor.setMetricTensor(MetricTensor(g11, g22, g33, g12, g13, g23)); + contravariantMetricTensor.setMetricTensor( + ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); // More robust to extrapolate derived quantities directly, rather than // deriving from extrapolated covariant metric components @@ -475,7 +476,7 @@ void Coordinates::setBoundaryCells(Options* mesh_options, const std::string& suf g_13 = getAtLocOrUnaligned(localmesh, "g_13", 0.0, suffix, location); g_23 = getAtLocOrUnaligned(localmesh, "g_23", 0.0, suffix, location); covariantMetricTensor.setMetricTensor( - MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); output_warn.write("\tWARNING! Covariant components of metric tensor set manually. " "Contravariant components NOT recalculated\n"); @@ -1492,24 +1493,23 @@ void Coordinates::applyToChristoffelSymbols( christoffel_symbols().map(function); } -const MetricTensor& Coordinates::getContravariantMetricTensor() const { +const ContravariantMetricTensor& Coordinates::getContravariantMetricTensor() const { return contravariantMetricTensor; } -const MetricTensor& Coordinates::getCovariantMetricTensor() const { +const CovariantMetricTensor& Coordinates::getCovariantMetricTensor() const { return covariantMetricTensor; } -void Coordinates::setContravariantMetricTensor(const MetricTensor& metric_tensor, - const std::string& region, - bool recalculate_staggered, - bool force_interpolate_from_centre) { +void Coordinates::setContravariantMetricTensor( + const ContravariantMetricTensor& metric_tensor, const std::string& region, + bool recalculate_staggered, bool force_interpolate_from_centre) { contravariantMetricTensor.setMetricTensor(metric_tensor); covariantMetricTensor.setMetricTensor(contravariantMetricTensor.inverse(region)); recalculateAndReset(recalculate_staggered, force_interpolate_from_centre); } -void Coordinates::setCovariantMetricTensor(const MetricTensor& metric_tensor, +void Coordinates::setCovariantMetricTensor(const CovariantMetricTensor& metric_tensor, const std::string& region, bool recalculate_staggered, bool force_interpolate_from_centre) { diff --git a/tests/MMS/GBS/gbs.cxx b/tests/MMS/GBS/gbs.cxx index 72dee35f17..273575a274 100644 --- a/tests/MMS/GBS/gbs.cxx +++ b/tests/MMS/GBS/gbs.cxx @@ -354,7 +354,7 @@ void GBS::LoadMetric(BoutReal Lnorm, BoutReal Bnorm) { const auto g12 = 0.0; const auto g13 = -sinty * coords->g11(); const auto g23 = -sbp * Btxy / (hthe * Bpxy * Rxy); - coords->setContravariantMetricTensor(MetricTensor(g11, g22, g33, g12, g13, g23)); + coords->setContravariantMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); coords->setJ(hthe / Bpxy); @@ -364,7 +364,7 @@ void GBS::LoadMetric(BoutReal Lnorm, BoutReal Bnorm) { const auto g_12 = sbp * Btxy * hthe * sinty * Rxy / Bpxy; const auto g_13 = sinty * Rxy * Rxy; const auto g_23 = sbp * Btxy * hthe * Rxy / Bpxy; - coords->setCovariantMetricTensor(MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + coords->setCovariantMetricTensor(CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); } // just define a macro for V_E dot Grad diff --git a/tests/MMS/diffusion/diffusion.cxx b/tests/MMS/diffusion/diffusion.cxx index eada9b1926..a39f0485c9 100644 --- a/tests/MMS/diffusion/diffusion.cxx +++ b/tests/MMS/diffusion/diffusion.cxx @@ -43,10 +43,11 @@ int Diffusion::init(bool UNUSED(restarting)) { SAVE_ONCE(mu_N); //set mesh - auto contravariant_metric_tensor = MetricTensor(1.1, 1.1, 1.1, 0.0, 0.0, 0.0); + auto contravariant_metric_tensor = + ContravariantMetricTensor(1.1, 1.1, 1.1, 0.0, 0.0, 0.0); coord->setContravariantMetricTensor(contravariant_metric_tensor); - auto covariant_metric_tensor = MetricTensor(1.1, 1.1, 1.1, 0.0, 0.0, 0.0); + auto covariant_metric_tensor = CovariantMetricTensor(1.1, 1.1, 1.1, 0.0, 0.0, 0.0); coord->setCovariantMetricTensor(covariant_metric_tensor); // Tell BOUT++ to solve N diff --git a/tests/MMS/elm-pb/elm_pb.cxx b/tests/MMS/elm-pb/elm_pb.cxx index 36a4fee088..210f976d69 100644 --- a/tests/MMS/elm-pb/elm_pb.cxx +++ b/tests/MMS/elm-pb/elm_pb.cxx @@ -423,7 +423,7 @@ class ELMpb : public PhysicsModel { const auto g12 = 0.0; const auto g13 = -I * coords->g11(); const auto g23 = -Btxy / (hthe * Bpxy * Rxy); - coords->setContravariantMetricTensor(MetricTensor(g11, g22, g33, g12, g13, g23)); + coords->setContravariantMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); coords->setJ(hthe / Bpxy); coords->setBxy(B0); @@ -434,7 +434,7 @@ class ELMpb : public PhysicsModel { const auto g_12 = Btxy * hthe * I * Rxy / Bpxy; const auto g_13 = I * Rxy * Rxy; const auto g_23 = Btxy * hthe * Rxy / Bpxy; - coords->setCovariantMetricTensor(MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + coords->setCovariantMetricTensor(CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); // Set B field vector diff --git a/tests/MMS/spatial/diffusion/diffusion.cxx b/tests/MMS/spatial/diffusion/diffusion.cxx index f5d20324f6..c72dd87540 100644 --- a/tests/MMS/spatial/diffusion/diffusion.cxx +++ b/tests/MMS/spatial/diffusion/diffusion.cxx @@ -39,10 +39,10 @@ class Diffusion : public PhysicsModel { SAVE_ONCE3(Dx, Dy, Dz) // set mesh - auto contravariant_metric_tensor = MetricTensor(1.1, 1.1, 1.1, 0.0, 0.0, 0.0); + auto contravariant_metric_tensor = ContravariantMetricTensor(1.1, 1.1, 1.1, 0.0, 0.0, 0.0); coords->setContravariantMetricTensor(contravariant_metric_tensor); - auto covariant_metric_tensor = MetricTensor(1.1, 1.1, 1.1, 0.0, 0.0, 0.0); + auto covariant_metric_tensor = CovariantMetricTensor(1.1, 1.1, 1.1, 0.0, 0.0, 0.0); coords->setCovariantMetricTensor(covariant_metric_tensor); // Tell BOUT++ to solve N diff --git a/tests/MMS/tokamak/tokamak.cxx b/tests/MMS/tokamak/tokamak.cxx index 2a4bb19d97..f92d115caa 100644 --- a/tests/MMS/tokamak/tokamak.cxx +++ b/tests/MMS/tokamak/tokamak.cxx @@ -86,7 +86,7 @@ class TokamakMMS : public PhysicsModel { const auto g12 = 0.0; const auto g13 = -sinty * coords->g11(); const auto g23 = -sbp * Btxy / (hthe * Bpxy * Rxy); - coords->setContravariantMetricTensor(MetricTensor(g11, g22, g33, g12, g13, g23)); + coords->setContravariantMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); coords->setJ(hthe / Bpxy); @@ -96,7 +96,7 @@ class TokamakMMS : public PhysicsModel { const auto g_12 = sbp * Btxy * hthe * sinty * Rxy / Bpxy; const auto g_13 = sinty * Rxy * Rxy; const auto g_23 = sbp * Btxy * hthe * Rxy / Bpxy; - coords->setCovariantMetricTensor(MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + coords->setCovariantMetricTensor(CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); } private: diff --git a/tests/MMS/wave-1d/wave.cxx b/tests/MMS/wave-1d/wave.cxx index 3a41a3d743..a4fc3158ce 100644 --- a/tests/MMS/wave-1d/wave.cxx +++ b/tests/MMS/wave-1d/wave.cxx @@ -32,10 +32,11 @@ class Wave1D : public PhysicsModel { SAVE_ONCE(Lx, Ly); //set mesh - auto contravariant_metric_tensor = MetricTensor(1.1, 1.1, 1.1, 0.0, 0.0, 0.0); + auto contravariant_metric_tensor = + ContravariantMetricTensor(1.1, 1.1, 1.1, 0.0, 0.0, 0.0); coord->setContravariantMetricTensor(contravariant_metric_tensor); - auto covariant_metric_tensor = MetricTensor(1.1, 1.1, 1.1, 0.0, 0.0, 0.0); + auto covariant_metric_tensor = CovariantMetricTensor(1.1, 1.1, 1.1, 0.0, 0.0, 0.0); coord->setCovariantMetricTensor(covariant_metric_tensor); g.setLocation(CELL_XLOW); // g staggered to the left of f diff --git a/tests/integrated/test-drift-instability/2fluid.cxx b/tests/integrated/test-drift-instability/2fluid.cxx index 0c356ea92f..577e5b1ef7 100644 --- a/tests/integrated/test-drift-instability/2fluid.cxx +++ b/tests/integrated/test-drift-instability/2fluid.cxx @@ -224,7 +224,7 @@ class TwoFluid : public PhysicsModel { g12 = 0.0; g13 = -I * coord->g11(); g23 = -Btxy / (hthe * Bpxy * Rxy); - coord->setContravariantMetricTensor(MetricTensor(g11, g22, g33, g12, g13, g23)); + coord->setContravariantMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); coord->setJ(hthe / Bpxy); @@ -235,7 +235,7 @@ class TwoFluid : public PhysicsModel { g_12 = Btxy * hthe * I * Rxy / Bpxy; g_13 = I * Rxy * Rxy; g_23 = Btxy * hthe * Rxy / Bpxy; - coord->setCovariantMetricTensor(MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + coord->setCovariantMetricTensor(CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); /**************** SET EVOLVING VARIABLES *************/ diff --git a/tests/integrated/test-interchange-instability/2fluid.cxx b/tests/integrated/test-interchange-instability/2fluid.cxx index 18e58c9969..3574761aa9 100644 --- a/tests/integrated/test-interchange-instability/2fluid.cxx +++ b/tests/integrated/test-interchange-instability/2fluid.cxx @@ -146,7 +146,7 @@ class Interchange : public PhysicsModel { g12 = 0.0; g13 = -I * coord->g11(); g23 = -Btxy / (hthe * Bpxy * Rxy); - coord->setContravariantMetricTensor(MetricTensor(g11, g22, g33, g12, g13, g23)); + coord->setContravariantMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); coord->setJ(hthe / Bpxy); @@ -157,7 +157,7 @@ class Interchange : public PhysicsModel { g_12 = Btxy * hthe * I * Rxy / Bpxy; g_13 = I * Rxy * Rxy; g_23 = Btxy * hthe * Rxy / Bpxy; - coord->setCovariantMetricTensor(MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + coord->setCovariantMetricTensor(CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); // Tell BOUT++ which variables to evolve SOLVE_FOR2(rho, Ni); diff --git a/tests/integrated/test-laplacexy/loadmetric.cxx b/tests/integrated/test-laplacexy/loadmetric.cxx index 2e8c12e2ef..6ab68c1539 100644 --- a/tests/integrated/test-laplacexy/loadmetric.cxx +++ b/tests/integrated/test-laplacexy/loadmetric.cxx @@ -55,7 +55,7 @@ void LoadMetric(BoutReal Lnorm, BoutReal Bnorm) { const auto g12 = 0.0; const auto g13 = -sinty * coords->g11(); const auto g23 = -sbp * Btxy / (hthe * Bpxy * Rxy); - coords->setContravariantMetricTensor(MetricTensor(g11, g22, g33, g12, g13, g23)); + coords->setContravariantMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); coords->setJ(hthe / Bpxy); @@ -65,5 +65,5 @@ void LoadMetric(BoutReal Lnorm, BoutReal Bnorm) { const auto g_12 = sbp * Btxy * hthe * sinty * Rxy / Bpxy; const auto g_13 = sinty * Rxy * Rxy; const auto g_23 = sbp * Btxy * hthe * Rxy / Bpxy; - coords->setCovariantMetricTensor(MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + coords->setCovariantMetricTensor(CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); } diff --git a/tests/integrated/test-laplacexz/test-laplacexz.cxx b/tests/integrated/test-laplacexz/test-laplacexz.cxx index 5d6ccddd68..5a6571d33b 100644 --- a/tests/integrated/test-laplacexz/test-laplacexz.cxx +++ b/tests/integrated/test-laplacexz/test-laplacexz.cxx @@ -21,7 +21,7 @@ int main(int argc, char** argv) { auto coord = bout::globals::mesh->getCoordinates(); const auto g13 = 1.8; // test off-diagonal components with nonzero value - coord->setContravariantMetricTensor(MetricTensor( + coord->setContravariantMetricTensor(ContravariantMetricTensor( coord->g11(), coord->g22(), coord->g33(), coord->g12(), g13, coord->g23())); // create some input field @@ -36,7 +36,7 @@ int main(int argc, char** argv) { Field3D f2 = inv->solve(g, 0.0); // Invert the Laplacian. // reset to 0.0 for original laplacexz test - coord->setContravariantMetricTensor(MetricTensor( + coord->setContravariantMetricTensor(ContravariantMetricTensor( coord->g11(), coord->g22(), coord->g33(), coord->g12(), 0.0, coord->g23())); // Now the normal test. diff --git a/tests/unit/mesh/test_coordinates.cxx b/tests/unit/mesh/test_coordinates.cxx index ccc5b44159..0d26e750e3 100644 --- a/tests/unit/mesh/test_coordinates.cxx +++ b/tests/unit/mesh/test_coordinates.cxx @@ -135,7 +135,8 @@ TEST_F(CoordinatesTest, CalcContravariant) { FieldMetric{0.0}, // ShiftTorsion FieldMetric{0.0}}; // IntShiftTorsion - coords.setContravariantMetricTensor(MetricTensor(1.0, 1.0, 1.0, 0.0, 0.0, 0.0)); + coords.setContravariantMetricTensor( + ContravariantMetricTensor(1.0, 1.0, 1.0, 0.0, 0.0, 0.0)); EXPECT_TRUE(IsFieldEqual(coords.g_11(), 1.0)); EXPECT_TRUE(IsFieldEqual(coords.g_22(), 1.0)); @@ -167,7 +168,7 @@ TEST_F(CoordinatesTest, CalcCovariant) { FieldMetric{0.0}, // ShiftTorsion FieldMetric{0.0}}; // IntShiftTorsion - coords.setCovariantMetricTensor(MetricTensor(1.0, 1.0, 1.0, 0.0, 0.0, 0.0)); + coords.setCovariantMetricTensor(CovariantMetricTensor(1.0, 1.0, 1.0, 0.0, 0.0, 0.0)); EXPECT_TRUE(IsFieldEqual(coords.g11(), 1.0)); EXPECT_TRUE(IsFieldEqual(coords.g22(), 1.0)); @@ -349,7 +350,7 @@ TEST_F(CoordinatesTest, SetContravariantMetricTensor) { FieldMetric{0.0}}; // IntShiftTorsion // Modify with setter - auto updated_metric_tensor = MetricTensor(1.0, 2.0, 0.4, 1.0, 0.0, 0.2); + auto updated_metric_tensor = ContravariantMetricTensor(1.0, 2.0, 0.4, 1.0, 0.0, 0.2); coords.setContravariantMetricTensor(updated_metric_tensor); // Get values with getter and check they have been modified as expected @@ -416,7 +417,7 @@ TEST_F(CoordinatesTest, SetCovariantMetricTensor) { FieldMetric{0.0}}; // IntShiftTorsion // Modify with setter - auto updated_metric_tensor = MetricTensor(1.0, 2.0, 0.4, 1.0, 0.0, 0.2); + auto updated_metric_tensor = CovariantMetricTensor(1.0, 2.0, 0.4, 1.0, 0.0, 0.2); coords.setCovariantMetricTensor(updated_metric_tensor); // Get values with getter and check they have been modified as expected From bb34560cc03ae6014a02f420316dbc5921f18a77 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Mon, 19 Feb 2024 14:32:47 +0000 Subject: [PATCH 330/491] Revert extracted functions on DifferentialOperators for those that are only used by Coordinates class (i.e. put back dependency on bout/derivs). --- include/bout/differential_operators.hxx | 34 ---------- src/mesh/coordinates.cxx | 45 ++++++------- src/mesh/differential_operators.cxx | 87 ++----------------------- 3 files changed, 29 insertions(+), 137 deletions(-) diff --git a/include/bout/differential_operators.hxx b/include/bout/differential_operators.hxx index 60e2206814..2759f0f07b 100644 --- a/include/bout/differential_operators.hxx +++ b/include/bout/differential_operators.hxx @@ -9,15 +9,6 @@ class DifferentialOperators { using FieldMetric = MetricTensor::FieldMetric; public: - Field3D DDX(const Field3D& f, CELL_LOC outloc, const std::string& method = "DEFAULT", - const std::string& region = "RGN_NOBNDRY") const; - - Field3D DDY(const Field3D& f, CELL_LOC outloc, const std::string& method = "DEFAULT", - const std::string& region = "RGN_NOBNDRY") const; - - Field3D DDZ(const Field3D& f, CELL_LOC outloc, const std::string& method = "DEFAULT", - const std::string& region = "RGN_NOBNDRY") const; - Field2D DDX(const Field2D& f, const Field2D& dx, CELL_LOC outloc = CELL_DEFAULT, const std::string& method = "DEFAULT", const std::string& region = "RGN_NOBNDRY") const; @@ -30,47 +21,22 @@ public: const std::string& method = "DEFAULT", const std::string& region = "RGN_NOBNDRY") const; - Field3D DDX(const Field3D& f, const Field3D& dx, const Field3D& dz, - const FieldMetric& intShiftTorsion, CELL_LOC outloc = CELL_DEFAULT, - const std::string& method = "DEFAULT", - const std::string& region = "RGN_NOBNDRY") const; - Field3D DDY(const Field3D& f, const Field3D& dy, CELL_LOC outloc = CELL_DEFAULT, const std::string& method = "DEFAULT", const std::string& region = "RGN_NOBNDRY") const; - Field3D DDZ(const Field3D& f, const Field3D& dz, CELL_LOC outloc = CELL_DEFAULT, - const std::string& method = "DEFAULT", - const std::string& region = "RGN_NOBNDRY") const; - Field2D D2DX2(const Field2D& field2D, CELL_LOC outloc); - Field3D D2DX2(const Field3D& field3D, CELL_LOC outloc); - Field2D D2DY2(const Field2D& field2D, CELL_LOC outloc = CELL_DEFAULT, const std::string& method = "DEFAULT"); Field3D D2DY2(const Field3D& field3D, CELL_LOC outloc = CELL_DEFAULT, const std::string& method = "DEFAULT"); - Field2D D2DZ2(const Field2D& field2D, CELL_LOC outloc); - - Field3D D2DZ2(const Field3D& field3D, CELL_LOC outloc); - - Field2D D2DXDY(const Field2D& field2D, CELL_LOC outloc, const std::string& method, - const std::string& region, const std::string& dfdy_boundary_condition, - const std::string& dfdy_region); - Field3D D2DXDY(const Field3D& field3D, CELL_LOC outloc, const std::string& method, const std::string& region, const std::string& dfdy_boundary_condition, const std::string& dfdy_region); - Field2D D2DXDZ(const Field2D& field2D, CELL_LOC outloc); - Field3D D2DXDZ(const Field3D& field3D, CELL_LOC outloc); - - Field2D D2DYDZ(const Field2D& field2D, CELL_LOC outloc); - Field3D D2DYDZ(const Field3D& field3D, CELL_LOC outloc); - /// Gradient along magnetic field b.Grad(f) Field2D Grad_par(const Field2D& var, const Field2D& dy, CELL_LOC outloc = CELL_DEFAULT, const std::string& method = "DEFAULT"); diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index c0d77ce6e6..f9a6d9a073 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -15,6 +15,7 @@ #include "parallel/fci.hxx" #include "parallel/shiftedmetricinterp.hxx" +#include "bout/derivs.hxx" #include @@ -913,13 +914,20 @@ void Coordinates::setParallelTransform(Options* mesh_options) { Field2D Coordinates::DDX(const Field2D& f, CELL_LOC loc, const std::string& method, const std::string& region) const { ASSERT1(location == loc || loc == CELL_DEFAULT) - return differential_operators->DDX(f, dx(), loc, method, region); + return bout::derivatives::index::DDX(f, loc, method, region) / dx(); } Field3D Coordinates::DDX(const Field3D& f, CELL_LOC outloc, const std::string& method, const std::string& region) const { - return differential_operators->DDX(f, dx(), dz(), IntShiftTorsion_, outloc, method, - region); + auto result = bout::derivatives::index::DDX(f, outloc, method, region); + result /= dx(); + + if (f.getMesh()->IncIntShear) { + // Using BOUT-06 style shifting + result += IntShiftTorsion() * DDZ(f, outloc, method, region); + } + + return result; } Field2D Coordinates::DDY(const Field2D& f, CELL_LOC loc, const std::string& method, @@ -945,7 +953,7 @@ Field2D Coordinates::DDZ(const Field2D& f, CELL_LOC loc, const std::string& meth Field3D Coordinates::DDZ(const Field3D& f, CELL_LOC outloc, const std::string& method, const std::string& region) const { - return differential_operators->DDZ(f, dz(), outloc, method, region); + return bout::derivatives::index::DDZ(f, outloc, method, region) / dz(); } ///////////////////////////////////////////////////////// @@ -1044,7 +1052,7 @@ Field3D Coordinates::Grad2_par2(const Field3D& f, CELL_LOC outloc, } ASSERT1(location == outloc) - Field3D result = differential_operators->DDY(f, outloc, method); + Field3D result = ::DDY(f, outloc, method); Field3D r2 = differential_operators->D2DY2(f, outloc, method) / g_22(); @@ -1125,11 +1133,9 @@ Field3D Coordinates::Delp2(const Field3D& f, CELL_LOC outloc, bool useFFT) { } } } else { - result = G1() * differential_operators->DDX(f, outloc) - + G3() * differential_operators->DDZ(f, outloc) - + g11() * differential_operators->D2DX2(f, outloc) - + g33() * differential_operators->D2DZ2(f, outloc) - + 2 * g13() * differential_operators->D2DXDZ(f, outloc); + result = G1() * ::DDX(f, outloc) + G3() * ::DDZ(f, outloc) + + g11() * ::D2DX2(f, outloc) + g33() * ::D2DZ2(f, outloc) + + 2 * g13() * ::D2DXDZ(f, outloc); } ASSERT2(result.getLocation() == outloc) @@ -1210,8 +1216,7 @@ Field2D Coordinates::Laplace_par(const Field2D& f, CELL_LOC outloc) { Field3D Coordinates::Laplace_par(const Field3D& f, CELL_LOC outloc) { ASSERT1(location == outloc || outloc == CELL_DEFAULT) return differential_operators->D2DY2(f, outloc) / g_22() - + differential_operators->DDY(J() / g_22(), outloc) - * differential_operators->DDY(f, outloc) / J(); + + ::DDY(J() / g_22(), outloc) * ::DDY(f, outloc) / J(); } // Full Laplacian operator on scalar field @@ -1226,8 +1231,8 @@ Field2D Coordinates::Laplace(const Field2D& f, CELL_LOC outloc, + g11() * differential_operators->D2DX2(f, outloc) + g22() * differential_operators->D2DY2(f, outloc) + 2.0 * g12() - * differential_operators->D2DXDY(f, outloc, "DEFAULT", "RGN_NOBNDRY", - dfdy_boundary_conditions, dfdy_dy_region); + * ::D2DXDY(f, outloc, "DEFAULT", "RGN_NOBNDRY", dfdy_boundary_conditions, + dfdy_dy_region); } Field3D Coordinates::Laplace(const Field3D& f, CELL_LOC outloc, @@ -1236,19 +1241,15 @@ Field3D Coordinates::Laplace(const Field3D& f, CELL_LOC outloc, TRACE("Coordinates::Laplace( Field3D )"); ASSERT1(location == outloc || outloc == CELL_DEFAULT) - return G1() * differential_operators->DDX(f, outloc) - + G2() * differential_operators->DDY(f, outloc) - + G3() * differential_operators->DDZ(f, outloc) - + g11() * differential_operators->D2DX2(f, outloc) - + g22() * differential_operators->D2DY2(f, outloc) - + g33() * differential_operators->D2DZ2(f, outloc) + return G1() * ::DDX(f, outloc) + G2() * ::DDY(f, outloc) + G3() * ::DDZ(f, outloc) + + g11() * ::D2DX2(f, outloc) + g22() * ::D2DY2(f, outloc) + + g33() * ::D2DZ2(f, outloc) + 2.0 * (g12() * differential_operators->D2DXDY( f, outloc, "DEFAULT", "RGN_NOBNDRY", dfdy_boundary_conditions, dfdy_dy_region) - + g13() * differential_operators->D2DXDZ(f, outloc) - + g23() * differential_operators->D2DYDZ(f, outloc)); + + g13() * ::D2DXDZ(f, outloc) + g23() * ::D2DYDZ(f, outloc)); } // Full perpendicular Laplacian, in form of inverse of Laplacian operator in LaplaceXY diff --git a/src/mesh/differential_operators.cxx b/src/mesh/differential_operators.cxx index a05c9bce58..75a5b9d1db 100644 --- a/src/mesh/differential_operators.cxx +++ b/src/mesh/differential_operators.cxx @@ -3,45 +3,12 @@ #include "bout/mesh.hxx" #include -Field3D DifferentialOperators::DDX(const Field3D& f, CELL_LOC outloc, - const std::string& method, - const std::string& region) const { - return ::DDX(f, outloc, method, region); -} - -Field3D DifferentialOperators::DDY(const Field3D& f, CELL_LOC outloc, - const std::string& method, - const std::string& region) const { - return ::DDY(f, outloc, method, region); -} - -Field3D DifferentialOperators::DDZ(const Field3D& f, CELL_LOC outloc, - const std::string& method, - const std::string& region) const { - return ::DDZ(f, outloc, method, region); -} - Field2D DifferentialOperators::DDX(const Field2D& f, const Field2D& dx, CELL_LOC loc, const std::string& method, const std::string& region) const { return bout::derivatives::index::DDX(f, loc, method, region) / dx; } -Field3D DifferentialOperators::DDX(const Field3D& f, const Field3D& dx, const Field3D& dz, - const FieldMetric& intShiftTorsion, CELL_LOC outloc, - const std::string& method, - const std::string& region) const { - auto result = bout::derivatives::index::DDX(f, outloc, method, region); - result /= dx; - - if (f.getMesh()->IncIntShear) { - // Using BOUT-06 style shifting - result += intShiftTorsion * DDZ(f, dz, outloc, method, region); - } - - return result; -} - Field2D DifferentialOperators::DDY(const Field2D& f, const Field2D& dy, CELL_LOC loc, const std::string& method, const std::string& region) const { @@ -71,20 +38,10 @@ Field2D DifferentialOperators::DDZ(const Field2D& f, CELL_LOC loc, return zeroFrom(f).setLocation(loc); } -Field3D DifferentialOperators::DDZ(const Field3D& f, const Field3D& dz, CELL_LOC outloc, - const std::string& method, - const std::string& region) const { - return bout::derivatives::index::DDZ(f, outloc, method, region) / dz; -} - Field2D DifferentialOperators::D2DX2(const Field2D& field2D, CELL_LOC outloc) { return ::D2DX2(field2D, outloc); } -Field3D DifferentialOperators::D2DX2(const Field3D& field3D, CELL_LOC outloc) { - return ::D2DX2(field3D, outloc); -} - Field2D DifferentialOperators::D2DY2(const Field2D& field2D, CELL_LOC outloc, const std::string& method) { return ::D2DY2(field2D, outloc, method); @@ -95,22 +52,6 @@ Field3D DifferentialOperators::D2DY2(const Field3D& field3D, CELL_LOC outloc, return ::D2DY2(field3D, outloc, method); } -Field2D DifferentialOperators::D2DZ2(const Field2D& field2D, CELL_LOC outloc) { - return ::D2DZ2(field2D, outloc); -} - -Field3D DifferentialOperators::D2DZ2(const Field3D& field3D, CELL_LOC outloc) { - return ::D2DZ2(field3D, outloc); -} - -Field2D DifferentialOperators::D2DXDY(const Field2D& field2D, CELL_LOC outloc, - const std::string& method, - const std::string& region, - const std::string& dfdy_boundary_condition, - const std::string& dfdy_region) { - return ::D2DXDY(field2D, outloc, method, region, dfdy_boundary_condition, dfdy_region); -} - Field3D DifferentialOperators::D2DXDY(const Field3D& field3D, CELL_LOC outloc, const std::string& method, const std::string& region, @@ -119,22 +60,6 @@ Field3D DifferentialOperators::D2DXDY(const Field3D& field3D, CELL_LOC outloc, return ::D2DXDY(field3D, outloc, method, region, dfdy_boundary_condition, dfdy_region); } -Field2D DifferentialOperators::D2DXDZ(const Field2D& field2D, CELL_LOC outloc) { - return ::D2DXDZ(field2D, outloc); -} - -Field2D DifferentialOperators::D2DYDZ(const Field2D& field2D, CELL_LOC outloc) { - return ::D2DYDZ(field2D, outloc); -} - -Field3D DifferentialOperators::D2DXDZ(const Field3D& field3D, CELL_LOC outloc) { - return ::D2DXDZ(field3D, outloc); -} - -Field3D DifferentialOperators::D2DYDZ(const Field3D& field3D, CELL_LOC outloc) { - return ::D2DYDZ(field3D, outloc); -} - ///////////////////////////////////////////////////////// // Parallel gradient @@ -280,8 +205,8 @@ FieldMetric DifferentialOperators::Laplace( + covariantMetricTensor.g11() * D2DX2(f, outloc) + covariantMetricTensor.g22() * D2DY2(f, outloc) + 2.0 * covariantMetricTensor.g12() - * D2DXDY(f, outloc, "DEFAULT", "RGN_NOBNDRY", dfdy_boundary_conditions, - dfdy_dy_region); + * ::D2DXDY(f, outloc, "DEFAULT", "RGN_NOBNDRY", dfdy_boundary_conditions, + dfdy_dy_region); } Field3D DifferentialOperators::Laplace(const Field3D& f, @@ -293,15 +218,15 @@ Field3D DifferentialOperators::Laplace(const Field3D& f, TRACE("DifferentialOperators::Laplace( Field3D )"); Field3D result = G1 * ::DDX(f, outloc) + G2 * ::DDY(f, outloc) + G3 * ::DDZ(f, outloc) - + covariantMetricTensor.g11() * D2DX2(f, outloc) + + covariantMetricTensor.g11() * ::D2DX2(f, outloc) + covariantMetricTensor.g22() * D2DY2(f, outloc) - + covariantMetricTensor.g33() * D2DZ2(f, outloc) + + covariantMetricTensor.g33() * ::D2DZ2(f, outloc) + 2.0 * (covariantMetricTensor.g12() * D2DXDY(f, outloc, "DEFAULT", "RGN_NOBNDRY", dfdy_boundary_conditions, dfdy_dy_region) - + covariantMetricTensor.g13() * D2DXDZ(f, outloc) - + covariantMetricTensor.g23() * D2DYDZ(f, outloc)); + + covariantMetricTensor.g13() * ::D2DXDZ(f, outloc) + + covariantMetricTensor.g23() * ::D2DYDZ(f, outloc)); return result; } From fb2266fe3e0f1707ccce9139099111d2fbe29f5e Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 21 Feb 2024 10:01:27 +0000 Subject: [PATCH 331/491] Fully revert extracting differential operator methods from Coordinates class onto DifferentialOperators. --- CMakeLists.txt | 2 - include/bout/christoffel_symbols.hxx | 10 +- include/bout/coordinates.hxx | 3 - include/bout/differential_operators.hxx | 98 -------- include/bout/mesh.hxx | 8 +- src/mesh/christoffel_symbols.cxx | 212 ++++++----------- src/mesh/coordinates.cxx | 104 +++++---- src/mesh/differential_operators.cxx | 298 ------------------------ src/mesh/mesh.cxx | 5 - 9 files changed, 131 insertions(+), 609 deletions(-) delete mode 100644 include/bout/differential_operators.hxx delete mode 100644 src/mesh/differential_operators.cxx diff --git a/CMakeLists.txt b/CMakeLists.txt index bf099fdc71..0badccd080 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -354,8 +354,6 @@ set(BOUT_SOURCES ./src/sys/utils.cxx ./include/bout/metricTensor.hxx ./src/mesh/metricTensor.cxx - ./include/bout/differential_operators.hxx - ./src/mesh/differential_operators.cxx ./include/bout/christoffel_symbols.hxx ./src/mesh/christoffel_symbols.cxx ./include/bout/g_values.hxx diff --git a/include/bout/christoffel_symbols.hxx b/include/bout/christoffel_symbols.hxx index 26a4b0e7dd..0f34892236 100644 --- a/include/bout/christoffel_symbols.hxx +++ b/include/bout/christoffel_symbols.hxx @@ -2,7 +2,6 @@ #ifndef BOUT_CHRISTOFFELSYMBOLS_HXX #define BOUT_CHRISTOFFELSYMBOLS_HXX -#include "differential_operators.hxx" #include "bout/field2d.hxx" #include "bout/field3d.hxx" #include "bout/metricTensor.hxx" @@ -20,15 +19,14 @@ public: FieldMetric G2_11, FieldMetric G2_22, FieldMetric G2_33, FieldMetric G2_12, FieldMetric G2_13, FieldMetric G2_23, FieldMetric G3_11, FieldMetric G3_22, FieldMetric G3_33, - FieldMetric G3_12, FieldMetric G3_13, FieldMetric G3_23, - DifferentialOperators* differential_operators); + FieldMetric G3_12, FieldMetric G3_13, FieldMetric G3_23); - ChristoffelSymbols(const Coordinates& coordinates, DifferentialOperators* differential_operators); + explicit ChristoffelSymbols(const Coordinates& coordinates); // ChristoffelSymbols(BoutReal g11, BoutReal g22, BoutReal g33, BoutReal g12, BoutReal g13, // BoutReal g23, Mesh* mesh); - explicit ChristoffelSymbols(DifferentialOperators* differential_operators); + // ChristoffelSymbols() = default; const FieldMetric& G1_11() const; const FieldMetric& G1_22() const; @@ -73,8 +71,6 @@ private: FieldMetric G2_11_, G2_22_, G2_33_, G2_12_, G2_13_, G2_23_; FieldMetric G3_11_, G3_22_, G3_33_, G3_12_, G3_13_, G3_23_; - DifferentialOperators* differential_operators; - ChristoffelSymbols applyToComponents( const std::function& function) const; }; diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index 1dfdd08de5..b6b9af7ae3 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -34,7 +34,6 @@ #define BOUT_COORDINATES_H #include "christoffel_symbols.hxx" -#include "differential_operators.hxx" #include "g_values.hxx" #include "bout/metricTensor.hxx" #include "bout/paralleltransform.hxx" @@ -302,8 +301,6 @@ private: Mesh* localmesh; CELL_LOC location; - DifferentialOperators* differential_operators; - /// True if corrections for non-uniform mesh spacing should be included in operators bool non_uniform_{}; diff --git a/include/bout/differential_operators.hxx b/include/bout/differential_operators.hxx deleted file mode 100644 index 2759f0f07b..0000000000 --- a/include/bout/differential_operators.hxx +++ /dev/null @@ -1,98 +0,0 @@ - -#ifndef BOUT_DIFFERENTIALOPERATORS_HXX -#define BOUT_DIFFERENTIALOPERATORS_HXX - -#include "bout/metricTensor.hxx" - -class DifferentialOperators { - - using FieldMetric = MetricTensor::FieldMetric; - -public: - Field2D DDX(const Field2D& f, const Field2D& dx, CELL_LOC outloc = CELL_DEFAULT, - const std::string& method = "DEFAULT", - const std::string& region = "RGN_NOBNDRY") const; - - Field2D DDY(const Field2D& f, const Field2D& dy, CELL_LOC outloc = CELL_DEFAULT, - const std::string& method = "DEFAULT", - const std::string& region = "RGN_NOBNDRY") const; - - Field2D DDZ(const Field2D& f, CELL_LOC outloc = CELL_DEFAULT, - const std::string& method = "DEFAULT", - const std::string& region = "RGN_NOBNDRY") const; - - Field3D DDY(const Field3D& f, const Field3D& dy, CELL_LOC outloc = CELL_DEFAULT, - const std::string& method = "DEFAULT", - const std::string& region = "RGN_NOBNDRY") const; - - Field2D D2DX2(const Field2D& field2D, CELL_LOC outloc); - - Field2D D2DY2(const Field2D& field2D, CELL_LOC outloc = CELL_DEFAULT, - const std::string& method = "DEFAULT"); - - Field3D D2DY2(const Field3D& field3D, CELL_LOC outloc = CELL_DEFAULT, - const std::string& method = "DEFAULT"); - - Field3D D2DXDY(const Field3D& field3D, CELL_LOC outloc, const std::string& method, - const std::string& region, const std::string& dfdy_boundary_condition, - const std::string& dfdy_region); - - /// Gradient along magnetic field b.Grad(f) - Field2D Grad_par(const Field2D& var, const Field2D& dy, CELL_LOC outloc = CELL_DEFAULT, - const std::string& method = "DEFAULT"); - - Field3D Grad_par(const Field3D& var, const Field3D& dy, CELL_LOC outloc = CELL_DEFAULT, - const std::string& method = "DEFAULT"); - - /// Advection along magnetic field V*b.Grad(f) - Field2D Vpar_Grad_par(const Field2D& v, const Field2D& f, - const Coordinates* coordinates, CELL_LOC outloc = CELL_DEFAULT, - const std::string& method = "DEFAULT"); - - Field3D Vpar_Grad_par(const Field3D& v, const Field3D& f, - const Coordinates* coordinates, CELL_LOC outloc = CELL_DEFAULT, - const std::string& method = "DEFAULT"); - - /// Divergence along magnetic field Div(b*f) = B.Grad(f/B) - Field2D Div_par(const Field2D& f, const Field2D& Bxy, const Field2D& dy, - CELL_LOC outloc = CELL_DEFAULT, const std::string& method = "DEFAULT"); - - Field3D Div_par(const Field3D& f, const Field3D& Bxy, const Field3D& dy, - CELL_LOC outloc = CELL_DEFAULT, const std::string& method = "DEFAULT"); - - // Second derivative along magnetic field - Field2D Grad2_par2(const Field2D& f, const Field2D& dy, - MetricTensor& covariantMetricTensor, CELL_LOC outloc = CELL_DEFAULT, - const std::string& method = "DEFAULT"); - - Field3D Grad2_par2(const Field3D& f, MetricTensor& covariantMetricTensor, - CELL_LOC outloc = CELL_DEFAULT, - const std::string& method = "DEFAULT"); - - Field2D Laplace_par(const Field2D& f, const Field2D& g22, const Field2D& J, - const Field2D& dy, CELL_LOC outloc = CELL_DEFAULT); - - Field3D Laplace_par(const Field3D& f, const Field3D& g22, const Field3D& J, - const Field2D& dy, CELL_LOC outloc = CELL_DEFAULT); - - // Full Laplacian operator on scalar field - Field2D Laplace(const Field2D& f, MetricTensor& covariantMetricTensor, - const Field2D& dy, const Field2D& G1, const Field2D& G2, - const Field2D& G3, CELL_LOC outloc = CELL_DEFAULT, - const std::string& dfdy_boundary_conditions = "free_o3", - const std::string& dfdy_dy_region = ""); - - Field3D Laplace(const Field3D& f, MetricTensor& covariantMetricTensor, - const Field3D& G1, const Field3D& G2, const Field3D& G3, - CELL_LOC outloc = CELL_DEFAULT, - const std::string& dfdy_boundary_conditions = "free_o3", - const std::string& dfdy_dy_region = ""); - - // Full perpendicular Laplacian, in form of inverse of Laplacian operator in LaplaceXY - // solver - Field2D Laplace_perpXY(const Field2D& A, const Field2D& f, - MetricTensor& covariantMetricTensor, const Field2D& J, - const Field2D& dx, const Field2D& dy); -}; - -#endif //BOUT_DIFFERENTIALOPERATORS_HXX diff --git a/include/bout/mesh.hxx b/include/bout/mesh.hxx index f5f6570100..b006fa7cd0 100644 --- a/include/bout/mesh.hxx +++ b/include/bout/mesh.hxx @@ -100,9 +100,7 @@ class Mesh { public: /// Constructor for a "bare", uninitialised Mesh /// Only useful for testing - Mesh() - : source(nullptr), options(nullptr), - differential_operators(DifferentialOperators()), include_corner_cells(true) {} + Mesh() : source(nullptr), options(nullptr), include_corner_cells(true) {} /// Constructor /// @param[in] s The source to be used for loading variables @@ -784,8 +782,6 @@ public: /// Creates RGN_{ALL,NOBNDRY,NOX,NOY} void createDefaultRegions(); - DifferentialOperators* getDifferentialOperators(); - protected: /// Source for grid data GridDataSource* source{nullptr}; @@ -796,8 +792,6 @@ protected: /// Mesh options section Options* options{nullptr}; - DifferentialOperators differential_operators; - /// Set whether to call calcParallelSlices on all communicated fields (true) or not (false) bool calcParallelSlices_on_communicate{true}; diff --git a/src/mesh/christoffel_symbols.cxx b/src/mesh/christoffel_symbols.cxx index 966647ff6d..75bb5318ea 100644 --- a/src/mesh/christoffel_symbols.cxx +++ b/src/mesh/christoffel_symbols.cxx @@ -8,26 +8,19 @@ ChristoffelSymbols::ChristoffelSymbols( FieldMetric G1_13, FieldMetric G1_23, FieldMetric G2_11, FieldMetric G2_22, FieldMetric G2_33, FieldMetric G2_12, FieldMetric G2_13, FieldMetric G2_23, FieldMetric G3_11, FieldMetric G3_22, FieldMetric G3_33, FieldMetric G3_12, - FieldMetric G3_13, FieldMetric G3_23, DifferentialOperators* differential_operators) + FieldMetric G3_13, FieldMetric G3_23) : G1_11_(std::move(G1_11)), G1_22_(std::move(G1_22)), G1_33_(std::move(G1_33)), G1_12_(std::move(G1_12)), G1_13_(std::move(G1_13)), G1_23_(std::move(G1_23)), G2_11_(std::move(G2_11)), G2_22_(std::move(G2_22)), G2_33_(std::move(G2_33)), G2_12_(std::move(G2_12)), G2_13_(std::move(G2_13)), G2_23_(std::move(G2_23)), G3_11_(std::move(G3_11)), G3_22_(std::move(G3_22)), G3_33_(std::move(G3_33)), - G3_12_(std::move(G3_12)), G3_13_(std::move(G3_13)), G3_23_(std::move(G3_23)), - differential_operators(differential_operators){ - ASSERT0(differential_operators != nullptr)}; + G3_12_(std::move(G3_12)), G3_13_(std::move(G3_13)), G3_23_(std::move(G3_23)){}; -ChristoffelSymbols::ChristoffelSymbols(const Coordinates& coordinates, - DifferentialOperators* differential_operators) - : differential_operators(differential_operators) { - ASSERT0(differential_operators != nullptr); +ChristoffelSymbols::ChristoffelSymbols(const Coordinates& coordinates) { // Calculate Christoffel symbol terms (18 independent values) // Note: This calculation is completely general: metric // tensor can be 2D or 3D. For 2D, all DDZ terms are zero - const auto& dx = coordinates.dx(); - const auto& dy = coordinates.dy(); const auto& contravariantMetricTensor = coordinates.getContravariantMetricTensor(); const auto& covariantMetricTensor = coordinates.getCovariantMetricTensor(); @@ -45,148 +38,89 @@ ChristoffelSymbols::ChristoffelSymbols(const Coordinates& coordinates, const auto& g_13 = covariantMetricTensor.g13(); const auto& g_23 = covariantMetricTensor.g23(); - G1_11_ = 0.5 * g11 * differential_operators->DDX(g_11, dx) - + g12 - * (differential_operators->DDX(g_12, dx) - - 0.5 * differential_operators->DDY(g_11, dy)) - + g13 - * (differential_operators->DDX(g_13, dx) - - 0.5 * differential_operators->DDZ(g_11)); - G1_22_ = g11 - * (differential_operators->DDY(g_12, dy) - - 0.5 * differential_operators->DDX(g_22, dx)) - + 0.5 * g12 * differential_operators->DDY(g_22, dy) - + g13 - * (differential_operators->DDY(g_23, dy) - - 0.5 * differential_operators->DDZ(g_22)); - G1_33_ = g11 - * (differential_operators->DDZ(g_13) - - 0.5 * differential_operators->DDX(g_33, dx)) - + g12 - * (differential_operators->DDZ(g_23) - - 0.5 * differential_operators->DDY(g_33, dy)) - + 0.5 * g13 * differential_operators->DDZ(g_33); - G1_12_ = 0.5 * g11 * differential_operators->DDY(g_11, dy) - + 0.5 * g12 * differential_operators->DDX(g_22, dx) - + 0.5 * g13 - * (differential_operators->DDY(g_13, dy) - + differential_operators->DDX(g_23, dx) - - differential_operators->DDZ(g_12)); - G1_13_ = - 0.5 * g11 * differential_operators->DDZ(g_11) - + 0.5 * g12 - * (differential_operators->DDZ(g_12) + differential_operators->DDX(g_23, dx) - - differential_operators->DDY(g_13, dy)) - + 0.5 * g13 * differential_operators->DDX(g_33, dx); + G1_11_ = 0.5 * g11 * coordinates.DDX(g_11) + + g12 * (coordinates.DDX(g_12) - 0.5 * coordinates.DDY(g_11)) + + g13 * (coordinates.DDX(g_13) - 0.5 * coordinates.DDZ(g_11)); + G1_22_ = g11 * (coordinates.DDY(g_12) - 0.5 * coordinates.DDX(g_22)) + + 0.5 * g12 * coordinates.DDY(g_22) + + g13 * (coordinates.DDY(g_23) - 0.5 * coordinates.DDZ(g_22)); + G1_33_ = g11 * (coordinates.DDZ(g_13) - 0.5 * coordinates.DDX(g_33)) + + g12 * (coordinates.DDZ(g_23) - 0.5 * coordinates.DDY(g_33)) + + 0.5 * g13 * coordinates.DDZ(g_33); + G1_12_ = + 0.5 * g11 * coordinates.DDY(g_11) + 0.5 * g12 * coordinates.DDX(g_22) + + 0.5 * g13 + * (coordinates.DDY(g_13) + coordinates.DDX(g_23) - coordinates.DDZ(g_12)); + G1_13_ = 0.5 * g11 * coordinates.DDZ(g_11) + + 0.5 * g12 + * (coordinates.DDZ(g_12) + coordinates.DDX(g_23) - coordinates.DDY(g_13)) + + 0.5 * g13 * coordinates.DDX(g_33); G1_23_ = - 0.5 * g11 - * (differential_operators->DDZ(g_12) + differential_operators->DDY(g_13, dy) - - differential_operators->DDX(g_23, dx)) + 0.5 * g11 * (coordinates.DDZ(g_12) + coordinates.DDY(g_13) - coordinates.DDX(g_23)) + 0.5 * g12 - * (differential_operators->DDZ(g_22) + differential_operators->DDY(g_23, dy) - - differential_operators->DDY(g_23, dy)) - // + 0.5 *g13*(differential_operators->DDZ(g_32) + differential_operators->DDY(g_33) - differential_operators->DDZ(g_23)); + * (coordinates.DDZ(g_22) + coordinates.DDY(g_23) - coordinates.DDY(g_23)) + // + 0.5 *g13*(coordinates.DDZ(g_32) + coordinates.DDY(g_33) - coordinates.DDZ(g_23)); // which equals - + 0.5 * g13 * differential_operators->DDY(g_33, dy); - - G2_11_ = 0.5 * g12 * differential_operators->DDX(g_11, dx) - + g22 - * (differential_operators->DDX(g_12, dx) - - 0.5 * differential_operators->DDY(g_11, dy)) - + g23 - * (differential_operators->DDX(g_13, dx) - - 0.5 * differential_operators->DDZ(g_11)); - G2_22_ = g12 - * (differential_operators->DDY(g_12, dy) - - 0.5 * differential_operators->DDX(g_22, dx)) - + 0.5 * g22 * differential_operators->DDY(g_22, dy) - + g23 - * (differential_operators->DDY(g23, dy) - - 0.5 * differential_operators->DDZ(g_22)); - G2_33_ = g12 - * (differential_operators->DDZ(g_13) - - 0.5 * differential_operators->DDX(g_33, dx)) - + g22 - * (differential_operators->DDZ(g_23) - - 0.5 * differential_operators->DDY(g_33, dy)) - + 0.5 * g23 * differential_operators->DDZ(g_33); - G2_12_ = 0.5 * g12 * differential_operators->DDY(g_11, dy) - + 0.5 * g22 * differential_operators->DDX(g_22, dx) - + 0.5 * g23 - * (differential_operators->DDY(g_13, dy) - + differential_operators->DDX(g_23, dx) - - differential_operators->DDZ(g_12)); + + 0.5 * g13 * coordinates.DDY(g_33); + + G2_11_ = 0.5 * g12 * coordinates.DDX(g_11) + + g22 * (coordinates.DDX(g_12) - 0.5 * coordinates.DDY(g_11)) + + g23 * (coordinates.DDX(g_13) - 0.5 * coordinates.DDZ(g_11)); + G2_22_ = g12 * (coordinates.DDY(g_12) - 0.5 * coordinates.DDX(g_22)) + + 0.5 * g22 * coordinates.DDY(g_22) + + g23 * (coordinates.DDY(g23) - 0.5 * coordinates.DDZ(g_22)); + G2_33_ = g12 * (coordinates.DDZ(g_13) - 0.5 * coordinates.DDX(g_33)) + + g22 * (coordinates.DDZ(g_23) - 0.5 * coordinates.DDY(g_33)) + + 0.5 * g23 * coordinates.DDZ(g_33); + G2_12_ = + 0.5 * g12 * coordinates.DDY(g_11) + 0.5 * g22 * coordinates.DDX(g_22) + + 0.5 * g23 + * (coordinates.DDY(g_13) + coordinates.DDX(g_23) - coordinates.DDZ(g_12)); G2_13_ = - // 0.5 *g21*(differential_operators->DDZ(g_11) + differential_operators->DDX(covariantMetricTensor.Getg13()) - differential_operators->DDX(g_13)) + // 0.5 *g21*(coordinates.DDZ(g_11) + coordinates.DDX(covariantMetricTensor.Getg13()) - coordinates.DDX(g_13)) // which equals - 0.5 * g12 - * (differential_operators->DDZ(g_11) + differential_operators->DDX(g_13, dx) - - differential_operators->DDX(g_13, dx)) - // + 0.5 *g22*(differential_operators->DDZ(covariantMetricTensor.Getg21()) + differential_operators->DDX(g_23) - differential_operators->DDY(g_13)) + 0.5 * g12 * (coordinates.DDZ(g_11) + coordinates.DDX(g_13) - coordinates.DDX(g_13)) + // + 0.5 *g22*(coordinates.DDZ(covariantMetricTensor.Getg21()) + coordinates.DDX(g_23) - coordinates.DDY(g_13)) // which equals + 0.5 * g22 - * (differential_operators->DDZ(g_12) + differential_operators->DDX(g_23, dx) - - differential_operators->DDY(g_13, dy)) - // + 0.5 *g23*(differential_operators->DDZ(covariantMetricTensor.Getg31()) + differential_operators->DDX(g_33) - differential_operators->DDZ(g_13)); + * (coordinates.DDZ(g_12) + coordinates.DDX(g_23) - coordinates.DDY(g_13)) + // + 0.5 *g23*(coordinates.DDZ(covariantMetricTensor.Getg31()) + coordinates.DDX(g_33) - coordinates.DDZ(g_13)); // which equals - + 0.5 * g23 * differential_operators->DDX(g_33, dx); + + 0.5 * g23 * coordinates.DDX(g_33); G2_23_ = - 0.5 * g12 - * (differential_operators->DDZ(g_12) + differential_operators->DDY(g_13, dy) - - differential_operators->DDX(g_23, dx)) - + 0.5 * g22 * differential_operators->DDZ(g_22) - + 0.5 * g23 * differential_operators->DDY(g_33, dy); - - G3_11_ = 0.5 * g13 * differential_operators->DDX(g_11, dx) - + g23 - * (differential_operators->DDX(g_12, dx) - - 0.5 * differential_operators->DDY(g_11, dy)) - + g33 - * (differential_operators->DDX(g_13, dx) - - 0.5 * differential_operators->DDZ(g_11)); - G3_22_ = g13 - * (differential_operators->DDY(g_12, dy) - - 0.5 * differential_operators->DDX(g_22, dx)) - + 0.5 * g23 * differential_operators->DDY(g_22, dy) - + g33 - * (differential_operators->DDY(g_23, dy) - - 0.5 * differential_operators->DDZ(g_22)); - G3_33_ = g13 - * (differential_operators->DDZ(g_13) - - 0.5 * differential_operators->DDX(g_33, dx)) - + g23 - * (differential_operators->DDZ(g_23) - - 0.5 * differential_operators->DDY(g_33, dy)) - + 0.5 * g33 * differential_operators->DDZ(g_33); + 0.5 * g12 * (coordinates.DDZ(g_12) + coordinates.DDY(g_13) - coordinates.DDX(g_23)) + + 0.5 * g22 * coordinates.DDZ(g_22) + 0.5 * g23 * coordinates.DDY(g_33); + + G3_11_ = 0.5 * g13 * coordinates.DDX(g_11) + + g23 * (coordinates.DDX(g_12) - 0.5 * coordinates.DDY(g_11)) + + g33 * (coordinates.DDX(g_13) - 0.5 * coordinates.DDZ(g_11)); + G3_22_ = g13 * (coordinates.DDY(g_12) - 0.5 * coordinates.DDX(g_22)) + + 0.5 * g23 * coordinates.DDY(g_22) + + g33 * (coordinates.DDY(g_23) - 0.5 * coordinates.DDZ(g_22)); + G3_33_ = g13 * (coordinates.DDZ(g_13) - 0.5 * coordinates.DDX(g_33)) + + g23 * (coordinates.DDZ(g_23) - 0.5 * coordinates.DDY(g_33)) + + 0.5 * g33 * coordinates.DDZ(g_33); G3_12_ = - // 0.5 *g31*(differential_operators->DDY(g_11) + differential_operators->DDX(covariantMetricTensor.Getg12()) - differential_operators->DDX(g_12)) + // 0.5 *g31*(coordinates.DDY(g_11) + coordinates.DDX(covariantMetricTensor.Getg12()) - coordinates.DDX(g_12)) // which equals to - 0.5 * g13 * differential_operators->DDY(g_11, dy) - // + 0.5 *g32*(differential_operators->DDY(covariantMetricTensor.Getg21()) + differential_operators->DDX(g_22) - differential_operators->DDY(g_12)) + 0.5 * g13 * coordinates.DDY(g_11) + // + 0.5 *g32*(coordinates.DDY(covariantMetricTensor.Getg21()) + coordinates.DDX(g_22) - coordinates.DDY(g_12)) // which equals to - + 0.5 * g23 * differential_operators->DDX(g_22, dx) - //+ 0.5 *g33*(differential_operators->DDY(covariantMetricTensor.Getg31()) + differential_operators->DDX(covariantMetricTensor.Getg32()) - differential_operators->DDZ(g_12)); + + 0.5 * g23 * coordinates.DDX(g_22) + //+ 0.5 *g33*(coordinates.DDY(covariantMetricTensor.Getg31()) + coordinates.DDX(covariantMetricTensor.Getg32()) - coordinates.DDZ(g_12)); // which equals to - + 0.5 * g33 * (differential_operators->DDY(g_13, dy)) - + differential_operators->DDX(g_23, dx) - differential_operators->DDZ(g_12); - G3_13_ = - 0.5 * g13 * differential_operators->DDZ(g_11) - + 0.5 * g23 - * (differential_operators->DDZ(g_12) + differential_operators->DDX(g_23, dx) - - differential_operators->DDY(g_13, dy)) - + 0.5 * g33 * differential_operators->DDX(g_33, dx); - G3_23_ = - 0.5 * g13 - * (differential_operators->DDZ(g_12) + differential_operators->DDY(g_13, dy)) - - differential_operators->DDX(g_23, dx) - + 0.5 * g23 * differential_operators->DDZ(g_22) - + 0.5 * g33 * differential_operators->DDY(g_33, dy); + + 0.5 * g33 * (coordinates.DDY(g_13)) + coordinates.DDX(g_23) + - coordinates.DDZ(g_12); + G3_13_ = 0.5 * g13 * coordinates.DDZ(g_11) + + 0.5 * g23 + * (coordinates.DDZ(g_12) + coordinates.DDX(g_23) - coordinates.DDY(g_13)) + + 0.5 * g33 * coordinates.DDX(g_33); + G3_23_ = 0.5 * g13 * (coordinates.DDZ(g_12) + coordinates.DDY(g_13)) + - coordinates.DDX(g_23) + 0.5 * g23 * coordinates.DDZ(g_22) + + 0.5 * g33 * coordinates.DDY(g_33); } -ChristoffelSymbols::ChristoffelSymbols(DifferentialOperators* differential_operators) - : differential_operators(differential_operators) { - ASSERT0(differential_operators != nullptr); -} +//ChristoffelSymbols::ChristoffelSymbols() {} const FieldMetric& ChristoffelSymbols::G1_11() const { return G1_11_; } const FieldMetric& ChristoffelSymbols::G1_22() const { return G1_22_; } @@ -267,6 +201,6 @@ ChristoffelSymbols ChristoffelSymbols::applyToComponents( G2_23, G3_11, G3_22, G3_33, G3_12, G3_13, G3_23] = components_out; return ChristoffelSymbols(G1_11, G1_22, G1_33, G1_12, G1_13, G1_23, G2_11, G2_22, G2_33, - G2_12, G2_13, G2_23, G3_11, G3_22, G3_33, G3_12, G3_13, G3_23, - differential_operators); + G2_12, G2_13, G2_23, G3_11, G3_22, G3_33, G3_12, G3_13, + G3_23); } diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index f9a6d9a073..602d948c3b 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -294,24 +294,19 @@ Coordinates::Coordinates(Mesh* mesh, FieldMetric dx, FieldMetric dy, FieldMetric const FieldMetric& g_12, const FieldMetric& g_13, const FieldMetric& g_23, FieldMetric ShiftTorsion, FieldMetric IntShiftTorsion) - : nz(mesh->LocalNz), localmesh(mesh), location(CELL_CENTRE), - differential_operators(mesh->getDifferentialOperators()), dx_(std::move(dx)), + : nz(mesh->LocalNz), localmesh(mesh), location(CELL_CENTRE), dx_(std::move(dx)), dy_(std::move(dy)), dz_(std::move(dz)), ShiftTorsion_(std::move(ShiftTorsion)), IntShiftTorsion_(std::move(IntShiftTorsion)), contravariantMetricTensor(g11, g22, g33, g12, g13, g23), - covariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23), - Bxy_(std::move(Bxy)){ASSERT0(differential_operators != nullptr)}; + covariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23), Bxy_(std::move(Bxy)){}; Coordinates::Coordinates(Mesh* mesh, Options* mesh_options, const CELL_LOC loc, const Coordinates* coords_in, bool force_interpolate_from_centre) - : localmesh(mesh), location(loc), - differential_operators(mesh->getDifferentialOperators()), dx_(1., mesh), - dy_(1., mesh), dz_(1., mesh), d1_dx_(mesh), d1_dy_(mesh), d1_dz_(mesh), - ShiftTorsion_(mesh), IntShiftTorsion_(mesh), - contravariantMetricTensor(1., 1., 1., 0, 0, 0, mesh), + : localmesh(mesh), location(loc), dx_(1., mesh), dy_(1., mesh), dz_(1., mesh), + d1_dx_(mesh), d1_dy_(mesh), d1_dz_(mesh), ShiftTorsion_(mesh), + IntShiftTorsion_(mesh), contravariantMetricTensor(1., 1., 1., 0, 0, 0, mesh), // Identity metric tensor covariantMetricTensor(1., 1., 1., 0, 0, 0, mesh), Bxy_(1., mesh) { - ASSERT0(differential_operators != nullptr) if (mesh_options == nullptr) { mesh_options = Options::getRoot()->getSection("mesh"); @@ -916,9 +911,9 @@ Field2D Coordinates::DDX(const Field2D& f, CELL_LOC loc, const std::string& meth ASSERT1(location == loc || loc == CELL_DEFAULT) return bout::derivatives::index::DDX(f, loc, method, region) / dx(); } - Field3D Coordinates::DDX(const Field3D& f, CELL_LOC outloc, const std::string& method, const std::string& region) const { + auto result = bout::derivatives::index::DDX(f, outloc, method, region); result /= dx(); @@ -932,29 +927,37 @@ Field3D Coordinates::DDX(const Field3D& f, CELL_LOC outloc, const std::string& m Field2D Coordinates::DDY(const Field2D& f, CELL_LOC loc, const std::string& method, const std::string& region) const { - ASSERT1(location == loc || loc == CELL_DEFAULT) - return differential_operators->DDY(f, dy(), loc, method, region); + ASSERT1(location == loc || loc == CELL_DEFAULT); + return bout::derivatives::index::DDY(f, loc, method, region) / dy(); } Field3D Coordinates::DDY(const Field3D& f, CELL_LOC outloc, const std::string& method, const std::string& region) const { - return differential_operators->DDY(f, dy(), outloc, method, region); -} - -Field2D Coordinates::DDZ(const Field2D& f, CELL_LOC loc, const std::string& method, - const std::string& region) const { - ASSERT1(location == loc || loc == CELL_DEFAULT) - ASSERT1(f.getMesh() == localmesh) +#if BOUT_USE_METRIC_3D + if (!f.hasParallelSlices() and !transform->canToFromFieldAligned()) { + Field3D f_parallel = f; + transform->calcParallelSlices(f_parallel); + f_parallel.applyParallelBoundary("parallel_neumann"); + return bout::derivatives::index::DDY(f_parallel, outloc, method, region); + } +#endif + return bout::derivatives::index::DDY(f, outloc, method, region) / dy(); +}; + +Field2D Coordinates::DDZ(const Field2D& f, CELL_LOC loc, + const std::string& UNUSED(method), + const std::string& UNUSED(region)) const { + ASSERT1(location == loc || loc == CELL_DEFAULT); + ASSERT1(f.getMesh() == localmesh); if (loc == CELL_DEFAULT) { loc = f.getLocation(); } - return differential_operators->DDZ(f, loc, method, region); + return zeroFrom(f).setLocation(loc); } - Field3D Coordinates::DDZ(const Field3D& f, CELL_LOC outloc, const std::string& method, const std::string& region) const { return bout::derivatives::index::DDZ(f, outloc, method, region) / dz(); -} +}; ///////////////////////////////////////////////////////// // Parallel gradient @@ -962,16 +965,18 @@ Field3D Coordinates::DDZ(const Field3D& f, CELL_LOC outloc, const std::string& m Field2D Coordinates::Grad_par(const Field2D& var, [[maybe_unused]] CELL_LOC outloc, const std::string& UNUSED(method)) { TRACE("Coordinates::Grad_par( Field2D )"); + ASSERT1(location == outloc + || (outloc == CELL_DEFAULT && location == var.getLocation())); - return differential_operators->Grad_par(var, dy()); + return DDY(var) * invSg(); } Field3D Coordinates::Grad_par(const Field3D& var, CELL_LOC outloc, const std::string& method) { TRACE("Coordinates::Grad_par( Field3D )"); - ASSERT1(location == outloc || outloc == CELL_DEFAULT) + ASSERT1(location == outloc || outloc == CELL_DEFAULT); - return differential_operators->Grad_par(var, dy(), outloc, method); + return ::DDY(var, outloc, method) * invSg(); } ///////////////////////////////////////////////////////// @@ -981,16 +986,16 @@ Field3D Coordinates::Grad_par(const Field3D& var, CELL_LOC outloc, Field2D Coordinates::Vpar_Grad_par(const Field2D& v, const Field2D& f, [[maybe_unused]] CELL_LOC outloc, const std::string& UNUSED(method)) { - ASSERT1(location == outloc || (outloc == CELL_DEFAULT && location == f.getLocation())) + ASSERT1(location == outloc || (outloc == CELL_DEFAULT && location == f.getLocation())); - return differential_operators->Vpar_Grad_par(v, f, this, outloc); + return VDDY(v, f) * invSg(); } Field3D Coordinates::Vpar_Grad_par(const Field3D& v, const Field3D& f, CELL_LOC outloc, const std::string& method) { - ASSERT1(location == outloc || outloc == CELL_DEFAULT) + ASSERT1(location == outloc || outloc == CELL_DEFAULT); - return differential_operators->Vpar_Grad_par(v, f, this, outloc, method); + return VDDY(v, f, outloc, method) * invSg(); } ///////////////////////////////////////////////////////// @@ -999,9 +1004,13 @@ Field3D Coordinates::Vpar_Grad_par(const Field3D& v, const Field3D& f, CELL_LOC Field2D Coordinates::Div_par(const Field2D& f, CELL_LOC outloc, const std::string& method) { TRACE("Coordinates::Div_par( Field2D )"); - ASSERT1(location == outloc || outloc == CELL_DEFAULT) + ASSERT1(location == outloc || outloc == CELL_DEFAULT); - return differential_operators->Div_par(f, Bxy(), dy(), outloc, method); + // Need Bxy at location of f, which might be different from location of this + // Coordinates object + auto Bxy_floc = f.getCoordinates()->Bxy(); + + return Bxy() * Grad_par(f / Bxy_floc, outloc, method); } Field3D Coordinates::Div_par(const Field3D& f, CELL_LOC outloc, @@ -1016,7 +1025,7 @@ Field3D Coordinates::Div_par(const Field3D& f, CELL_LOC outloc, if (!f.hasParallelSlices()) { // No yup/ydown fields. The Grad_par operator will // shift to field aligned coordinates - return differential_operators->Div_par(f, dy(), Bxy(), outloc, method); + return Bxy() * Grad_par(f / Bxy_floc, outloc, method); } // Need to modify yup and ydown fields @@ -1036,10 +1045,10 @@ Field3D Coordinates::Div_par(const Field3D& f, CELL_LOC outloc, Field2D Coordinates::Grad2_par2(const Field2D& f, CELL_LOC outloc, const std::string& method) { TRACE("Coordinates::Grad2_par2( Field2D )"); - ASSERT1(location == outloc || (outloc == CELL_DEFAULT && location == f.getLocation())) + ASSERT1(location == outloc || (outloc == CELL_DEFAULT && location == f.getLocation())); auto result = Grad2_par2_DDY_invSg(outloc, method) * DDY(f, outloc, method) - + differential_operators->D2DY2(f, outloc, method) / g_22(); // derivs 209 + + D2DY2(f, outloc, method) / g_22(); return result; } @@ -1050,11 +1059,11 @@ Field3D Coordinates::Grad2_par2(const Field3D& f, CELL_LOC outloc, if (outloc == CELL_DEFAULT) { outloc = f.getLocation(); } - ASSERT1(location == outloc) + ASSERT1(location == outloc); Field3D result = ::DDY(f, outloc, method); - Field3D r2 = differential_operators->D2DY2(f, outloc, method) / g_22(); + Field3D r2 = D2DY2(f, outloc, method) / g_22(); result = Grad2_par2_DDY_invSg(outloc, method) * result + r2; @@ -1067,13 +1076,12 @@ Field3D Coordinates::Grad2_par2(const Field3D& f, CELL_LOC outloc, // perpendicular Laplacian operator #include // Delp2 uses same coefficients as inversion code -#include Field2D Coordinates::Delp2(const Field2D& f, CELL_LOC outloc, bool UNUSED(useFFT)) { TRACE("Coordinates::Delp2( Field2D )"); ASSERT1(location == outloc || outloc == CELL_DEFAULT) - return G1() * DDX(f, outloc) + g11() * differential_operators->D2DX2(f, outloc); + return G1() * DDX(f, outloc) + g11() * D2DX2(f, outloc); } Field3D Coordinates::Delp2(const Field3D& f, CELL_LOC outloc, bool useFFT) { @@ -1209,14 +1217,12 @@ FieldPerp Coordinates::Delp2(const FieldPerp& f, CELL_LOC outloc, bool useFFT) { Field2D Coordinates::Laplace_par(const Field2D& f, CELL_LOC outloc) { ASSERT1(location == outloc || outloc == CELL_DEFAULT) - return differential_operators->D2DY2(f, outloc) / g_22() - + DDY(J() / g_22(), outloc) * DDY(f, outloc) / J(); + return D2DY2(f, outloc) / g_22() + DDY(J() / g_22(), outloc) * DDY(f, outloc) / J(); } Field3D Coordinates::Laplace_par(const Field3D& f, CELL_LOC outloc) { ASSERT1(location == outloc || outloc == CELL_DEFAULT) - return differential_operators->D2DY2(f, outloc) / g_22() - + ::DDY(J() / g_22(), outloc) * ::DDY(f, outloc) / J(); + return D2DY2(f, outloc) / g_22() + ::DDY(J() / g_22(), outloc) * ::DDY(f, outloc) / J(); } // Full Laplacian operator on scalar field @@ -1227,9 +1233,8 @@ Field2D Coordinates::Laplace(const Field2D& f, CELL_LOC outloc, TRACE("Coordinates::Laplace( Field2D )"); ASSERT1(location == outloc || outloc == CELL_DEFAULT) - return G1() * DDX(f, outloc) + G2() * DDY(f, outloc) - + g11() * differential_operators->D2DX2(f, outloc) - + g22() * differential_operators->D2DY2(f, outloc) + return G1() * DDX(f, outloc) + G2() * DDY(f, outloc) + g11() * D2DX2(f, outloc) + + g22() * D2DY2(f, outloc) + 2.0 * g12() * ::D2DXDY(f, outloc, "DEFAULT", "RGN_NOBNDRY", dfdy_boundary_conditions, dfdy_dy_region); @@ -1246,9 +1251,8 @@ Field3D Coordinates::Laplace(const Field3D& f, CELL_LOC outloc, + g33() * ::D2DZ2(f, outloc) + 2.0 * (g12() - * differential_operators->D2DXDY( - f, outloc, "DEFAULT", "RGN_NOBNDRY", dfdy_boundary_conditions, - dfdy_dy_region) + * D2DXDY(f, outloc, "DEFAULT", "RGN_NOBNDRY", + dfdy_boundary_conditions, dfdy_dy_region) + g13() * ::D2DXDZ(f, outloc) + g23() * ::D2DYDZ(f, outloc)); } @@ -1316,7 +1320,7 @@ Field2D Coordinates::Laplace_perpXY([[maybe_unused]] const Field2D& A, ChristoffelSymbols& Coordinates::christoffel_symbols() const { if (christoffel_symbols_cache == nullptr) { - auto ptr = std::make_unique(*this, differential_operators); + auto ptr = std::make_unique(*this); christoffel_symbols_cache = std::move(ptr); } return *christoffel_symbols_cache; diff --git a/src/mesh/differential_operators.cxx b/src/mesh/differential_operators.cxx deleted file mode 100644 index 75a5b9d1db..0000000000 --- a/src/mesh/differential_operators.cxx +++ /dev/null @@ -1,298 +0,0 @@ - -#include "bout/differential_operators.hxx" -#include "bout/mesh.hxx" -#include - -Field2D DifferentialOperators::DDX(const Field2D& f, const Field2D& dx, CELL_LOC loc, - const std::string& method, - const std::string& region) const { - return bout::derivatives::index::DDX(f, loc, method, region) / dx; -} - -Field2D DifferentialOperators::DDY(const Field2D& f, const Field2D& dy, CELL_LOC loc, - const std::string& method, - const std::string& region) const { - return bout::derivatives::index::DDY(f, loc, method, region) / dy; -} - -Field3D DifferentialOperators::DDY(const Field3D& f, const Field3D& dy, CELL_LOC outloc, - const std::string& method, - const std::string& region) const { -#if BOUT_USE_METRIC_3D - if (!f.hasParallelSlices() and !transform->canToFromFieldAligned()) { - Field3D f_parallel = f; - transform->calcParallelSlices(f_parallel); - f_parallel.applyParallelBoundary("parallel_neumann"); - return bout::derivatives::index::DDY(f_parallel, outloc, method, region); - } -#endif - return bout::derivatives::index::DDY(f, outloc, method, region) / dy; -} - -Field2D DifferentialOperators::DDZ(const Field2D& f, CELL_LOC loc, - const std::string& UNUSED(method), - const std::string& UNUSED(region)) const { - if (loc == CELL_DEFAULT) { - loc = f.getLocation(); - } - return zeroFrom(f).setLocation(loc); -} - -Field2D DifferentialOperators::D2DX2(const Field2D& field2D, CELL_LOC outloc) { - return ::D2DX2(field2D, outloc); -} - -Field2D DifferentialOperators::D2DY2(const Field2D& field2D, CELL_LOC outloc, - const std::string& method) { - return ::D2DY2(field2D, outloc, method); -} - -Field3D DifferentialOperators::D2DY2(const Field3D& field3D, CELL_LOC outloc, - const std::string& method) { - return ::D2DY2(field3D, outloc, method); -} - -Field3D DifferentialOperators::D2DXDY(const Field3D& field3D, CELL_LOC outloc, - const std::string& method, - const std::string& region, - const std::string& dfdy_boundary_condition, - const std::string& dfdy_region) { - return ::D2DXDY(field3D, outloc, method, region, dfdy_boundary_condition, dfdy_region); -} - -///////////////////////////////////////////////////////// -// Parallel gradient - -Field2D DifferentialOperators::Grad_par(const Field2D& var, const Field2D& dy, - [[maybe_unused]] CELL_LOC outloc, - const std::string& UNUSED(method)) { - TRACE("DifferentialOperators::Grad_par( Field2D )"); - - const auto coordinates = dy.getCoordinates(); //TODO: Find a better way - const auto invSg = coordinates->invSg(); - return DDY(var, dy) * invSg; -} - -Field3D DifferentialOperators::Grad_par(const Field3D& var, const Field3D& dy, - CELL_LOC outloc, const std::string& method) { - TRACE("DifferentialOperators::Grad_par( Field3D )"); - - const auto coordinates = dy.getCoordinates(); //TODO: Find a better way - const auto invSg = coordinates->invSg(); - return DDY(var, dy, outloc, method) * invSg; -} - -///////////////////////////////////////////////////////// -// Vpar_Grad_par -// vparallel times the parallel derivative along unperturbed B-field - -Field2D DifferentialOperators::Vpar_Grad_par(const Field2D& v, const Field2D& f, - const Coordinates* coordinates, - [[maybe_unused]] CELL_LOC outloc, - const std::string& UNUSED(method)) { - return VDDY(v, f) * coordinates->invSg(); -} - -Field3D DifferentialOperators::Vpar_Grad_par(const Field3D& v, const Field3D& f, - const Coordinates* coordinates, - CELL_LOC outloc, const std::string& method) { - - return VDDY(v, f, outloc, method) * coordinates->invSg(); -} - -///////////////////////////////////////////////////////// -// Parallel divergence - -Field2D DifferentialOperators::Div_par(const Field2D& f, const Field2D& Bxy, - const Field2D& dy, CELL_LOC outloc, - const std::string& method) { - TRACE("DifferentialOperators::Div_par( Field2D )"); - - // Need Bxy at location of f, which might be different from location of this - // Coordinates object - auto Bxy_floc = f.getCoordinates()->Bxy(); - - return Bxy * Grad_par(f / Bxy_floc, dy, outloc, method); -} - -Field3D DifferentialOperators::Div_par(const Field3D& f, const Field3D& Bxy, - const Field3D& dy, CELL_LOC outloc, - const std::string& method) { - TRACE("DifferentialOperators::Div_par( Field3D )"); - - // Need Bxy at location of f, which might be different from location of this - // Coordinates object - auto Bxy_floc = f.getCoordinates()->Bxy(); - - if (!f.hasParallelSlices()) { - // No yup/ydown fields. The Grad_par operator will - // shift to field aligned coordinates - return Bxy * Grad_par(f / Bxy_floc, dy, outloc, method); - } - - // Need to modify yup and ydown fields - Field3D f_B = f / Bxy_floc; - f_B.splitParallelSlices(); - for (int i = 0; i < f.getMesh()->ystart; ++i) { - f_B.yup(i) = f.yup(i) / Bxy_floc.yup(i); - f_B.ydown(i) = f.ydown(i) / Bxy_floc.ydown(i); - } - return Bxy * Grad_par(f_B, dy, outloc, method); -} - -///////////////////////////////////////////////////////// -// second parallel derivative (b dot Grad)(b dot Grad) -// Note: For parallel Laplacian use Laplace_par - -Field2D DifferentialOperators::Grad2_par2(const Field2D& f, const Field2D& dy, - MetricTensor& covariantMetricTensor, - CELL_LOC outloc, const std::string& method) { - TRACE("DifferentialOperators::Grad2_par2( Field2D )"); - - const auto coordinates = dy.getCoordinates(); //TODO: Find a better way - - const auto grad2_par2_DDY_invSg = coordinates->Grad2_par2_DDY_invSg(outloc, method); - - return grad2_par2_DDY_invSg * DDY(f, dy, outloc, method) - + D2DY2(f, outloc, method) / covariantMetricTensor.g22(); -} - -Field3D DifferentialOperators::Grad2_par2(const Field3D& f, - MetricTensor& covariantMetricTensor, - CELL_LOC outloc, const std::string& method) { - TRACE("DifferentialOperators::Grad2_par2( Field3D )"); - if (outloc == CELL_DEFAULT) { - outloc = f.getLocation(); - } - - Field3D result = ::DDY(f, outloc, method); - - Field3D r2 = D2DY2(f, outloc, method) / covariantMetricTensor.g22(); - - const auto coordinates = - covariantMetricTensor.g11().getCoordinates(); //TODO: Find a better way - - const auto grad2_par2_DDY_invSg = coordinates->Grad2_par2_DDY_invSg(outloc, method); - - result = grad2_par2_DDY_invSg * result + r2; - - ASSERT2(result.getLocation() == outloc) - - return result; -} - -FieldMetric DifferentialOperators::Laplace_par(const Field2D& f, const Field2D& g22, - const Field2D& J, const Field2D& dy, - CELL_LOC outloc) { - return D2DY2(f, outloc) / g22 + DDY(J / g22, dy, outloc) * DDY(f, dy, outloc) / J; -} - -Field3D DifferentialOperators::Laplace_par(const Field3D& f, const Field3D& g22, - const Field3D& J, const Field2D& dy, - CELL_LOC outloc) { - return D2DY2(f, outloc) / g22 + DDY(J / g22, dy, outloc) * ::DDY(f, outloc) / J; -} - -// Full Laplacian operator on scalar field - -FieldMetric DifferentialOperators::Laplace( - const Field2D& f, MetricTensor& covariantMetricTensor, const Field2D& dy, - const Field2D& G1, const Field2D& G2, const Field2D& G3, CELL_LOC outloc, - const std::string& dfdy_boundary_conditions, const std::string& dfdy_dy_region) { - TRACE("DifferentialOperators::Laplace( Field2D )"); - - return G1 * DDX(f, dy, outloc) + G2 * DDY(f, dy, outloc) - + covariantMetricTensor.g11() * D2DX2(f, outloc) - + covariantMetricTensor.g22() * D2DY2(f, outloc) - + 2.0 * covariantMetricTensor.g12() - * ::D2DXDY(f, outloc, "DEFAULT", "RGN_NOBNDRY", dfdy_boundary_conditions, - dfdy_dy_region); -} - -Field3D DifferentialOperators::Laplace(const Field3D& f, - MetricTensor& covariantMetricTensor, - const Field3D& G1, const Field3D& G2, - const Field3D& G3, CELL_LOC outloc, - const std::string& dfdy_boundary_conditions, - const std::string& dfdy_dy_region) { - TRACE("DifferentialOperators::Laplace( Field3D )"); - - Field3D result = G1 * ::DDX(f, outloc) + G2 * ::DDY(f, outloc) + G3 * ::DDZ(f, outloc) - + covariantMetricTensor.g11() * ::D2DX2(f, outloc) - + covariantMetricTensor.g22() * D2DY2(f, outloc) - + covariantMetricTensor.g33() * ::D2DZ2(f, outloc) - + 2.0 - * (covariantMetricTensor.g12() - * D2DXDY(f, outloc, "DEFAULT", "RGN_NOBNDRY", - dfdy_boundary_conditions, dfdy_dy_region) - + covariantMetricTensor.g13() * ::D2DXDZ(f, outloc) - + covariantMetricTensor.g23() * ::D2DYDZ(f, outloc)); - - return result; -} - -// Full perpendicular Laplacian, in form of inverse of Laplacian operator in LaplaceXY -// solver -Field2D DifferentialOperators::Laplace_perpXY([[maybe_unused]] const Field2D& A, - [[maybe_unused]] const Field2D& f), - MetricTensor& covariantMetricTensor, - const Field2D& J, const Field2D& dx, - const Field2D& dy) { - TRACE("DifferentialOperators::Laplace_perpXY( Field2D )"); -#if not(BOUT_USE_METRIC_3D) - Field2D result; - result.allocate(); - for (auto i : result.getRegion(RGN_NOBNDRY)) { - result[i] = 0.; - - // outer x boundary - const auto outer_x_avg = [&i](const auto& f) { return 0.5 * (f[i] + f[i.xp()]); }; - const BoutReal outer_x_A = outer_x_avg(A); - const BoutReal outer_x_J = outer_x_avg(J); - const BoutReal outer_x_g11 = outer_x_avg(covariantMetricTensor.g11()); - const BoutReal outer_x_dx = outer_x_avg(dx); - const BoutReal outer_x_value = - outer_x_A * outer_x_J * outer_x_g11 / (J[i] * outer_x_dx * dx[i]); - result[i] += outer_x_value * (f[i.xp()] - f[i]); - - // inner x boundary - const auto inner_x_avg = [&i](const auto& f) { return 0.5 * (f[i] + f[i.xm()]); }; - const BoutReal inner_x_A = inner_x_avg(A); - const BoutReal inner_x_J = inner_x_avg(J); - const BoutReal inner_x_g11 = inner_x_avg(covariantMetricTensor.g11()); - const BoutReal inner_x_dx = inner_x_avg(dx); - const BoutReal inner_x_value = - inner_x_A * inner_x_J * inner_x_g11 / (J[i] * inner_x_dx * dx[i]); - result[i] += inner_x_value * (f[i.xm()] - f[i]); - - // upper y boundary - const auto upper_y_avg = [&i](const auto& f) { return 0.5 * (f[i] + f[i.yp()]); }; - const BoutReal upper_y_A = upper_y_avg(A); - const BoutReal upper_y_J = upper_y_avg(J); - const BoutReal upper_y_g_22 = upper_y_avg(covariantMetricTensor.g22()); - const BoutReal upper_y_g23 = upper_y_avg(covariantMetricTensor.g23()); - const BoutReal upper_y_g_23 = upper_y_avg(covariantMetricTensor.g23()); - const BoutReal upper_y_dy = upper_y_avg(dy); - const BoutReal upper_y_value = -upper_y_A * upper_y_J * upper_y_g23 * upper_y_g_23 - / (upper_y_g_22 * J[i] * upper_y_dy * dy[i]); - result[i] += upper_y_value * (f[i.yp()] - f[i]); - - // lower y boundary - const auto lower_y_avg = [&i](const auto& f) { return 0.5 * (f[i] + f[i.ym()]); }; - const BoutReal lower_y_A = lower_y_avg(A); - const BoutReal lower_y_J = lower_y_avg(J); - const BoutReal lower_y_g_22 = lower_y_avg(covariantMetricTensor.g22()); - const BoutReal lower_y_g23 = lower_y_avg(covariantMetricTensor.g23()); - const BoutReal lower_y_g_23 = lower_y_avg(covariantMetricTensor.g23()); - const BoutReal lower_y_dy = lower_y_avg(dy); - const BoutReal lower_y_value = -lower_y_A * lower_y_J * lower_y_g23 * lower_y_g_23 - / (lower_y_g_22 * J[i] * lower_y_dy * dy[i]); - result[i] += lower_y_value * (f[i.ym()] - f[i]); - } - - return result; -#else - throw BoutException( - "DifferentialOperators::Laplace_perpXY for 3D metric not implemented"); -#endif -} diff --git a/src/mesh/mesh.cxx b/src/mesh/mesh.cxx index 0dbee548bc..e635107fb7 100644 --- a/src/mesh/mesh.cxx +++ b/src/mesh/mesh.cxx @@ -177,7 +177,6 @@ Mesh* Mesh::create(Options* opt) { return create(nullptr, opt); } Mesh::Mesh(GridDataSource* s, Options* opt) : source(s), options(opt == nullptr ? Options::getRoot()->getSection("mesh") : opt), - differential_operators(DifferentialOperators()), calcParallelSlices_on_communicate( (*options)["calcParallelSlices_on_communicate"] .doc("Calculate parallel slices on all communicated fields") @@ -1007,10 +1006,6 @@ std::optional Mesh::getCommonRegion(std::optional lhs, return region3Dintersect[pos]; } -DifferentialOperators* Mesh::getDifferentialOperators() { - return &differential_operators; -} - constexpr decltype(MeshFactory::type_name) MeshFactory::type_name; constexpr decltype(MeshFactory::section_name) MeshFactory::section_name; constexpr decltype(MeshFactory::option_name) MeshFactory::option_name; From 4ba966925b3fa00ab8dc4248df72d580f6406ce7 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Thu, 22 Feb 2024 09:24:49 +0000 Subject: [PATCH 332/491] Move trivial getters and setters of Coordinates class to header file. --- include/bout/coordinates.hxx | 128 ++++++++++++++++++----------------- src/mesh/coordinates.cxx | 104 ---------------------------- 2 files changed, 67 insertions(+), 165 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index b6b9af7ae3..b33f24601a 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -76,50 +76,55 @@ public: void outputVars(Options& output_options); ///< Mesh spacing in x, y and z - const FieldMetric& dx() const; - const FieldMetric& dy() const; - const FieldMetric& dz() const; + const FieldMetric& dx() const { return dx_; } + const FieldMetric& dy() const { return dy_; } + const FieldMetric& dz() const { return dz_; } - void setDx(FieldMetric dx); - void setDy(FieldMetric dy); - void setDz(FieldMetric dz); + void setDx(FieldMetric dx) { dx_ = std::move(dx); } + void setDy(FieldMetric dy) { dy_ = std::move(dy); } + void setDz(FieldMetric dz) { dz_ = std::move(dz); } - void setDy(BoutReal value, int x, int y); + void setDy(BoutReal value, int x, int y) { dy_(x, y) = value; } - void setD1_dx(FieldMetric d1_dx); - void setD1_dy(FieldMetric d1_dy); - void setD1_dz(FieldMetric d1_dz); + void setD1_dx(FieldMetric d1_dx) { d1_dx_ = std::move(d1_dx); } + void setD1_dy(FieldMetric d1_dy) { d1_dy_ = std::move(d1_dy); } + void setD1_dz(FieldMetric d1_dz) { d1_dz_ = std::move(d1_dz); } /// Length of the Z domain. Used for FFTs const Field2D& zlength() const; /// True if corrections for non-uniform mesh spacing should be included in operators - bool non_uniform() const; - void setNon_uniform(bool non_uniform); + bool non_uniform() const { return non_uniform_; } + void setNon_uniform(bool non_uniform) { non_uniform_ = non_uniform; } /// 2nd-order correction for non-uniform meshes d/di(1/dx), d/di(1/dy) and d/di(1/dz) - const FieldMetric& d1_dx() const; - const FieldMetric& d1_dy() const; - const FieldMetric& d1_dz() const; + const FieldMetric& d1_dx() const { return d1_dx_; } + const FieldMetric& d1_dy() const { return d1_dy_; } + const FieldMetric& d1_dz() const { return d1_dz_; } /// Covariant metric tensor - const FieldMetric& g_11() const; - const FieldMetric& g_22() const; - const FieldMetric& g_33() const; - const FieldMetric& g_12() const; - const FieldMetric& g_13() const; - const FieldMetric& g_23() const; + const MetricTensor::FieldMetric& g_11() const { return covariantMetricTensor.g11(); } + const MetricTensor::FieldMetric& g_22() const { return covariantMetricTensor.g22(); } + const MetricTensor::FieldMetric& g_33() const { return covariantMetricTensor.g33(); } + const MetricTensor::FieldMetric& g_12() const { return covariantMetricTensor.g12(); } + const MetricTensor::FieldMetric& g_13() const { return covariantMetricTensor.g13(); } + const MetricTensor::FieldMetric& g_23() const { return covariantMetricTensor.g23(); } /// Contravariant metric tensor (g^{ij}) - const FieldMetric& g11() const; - const FieldMetric& g22() const; - const FieldMetric& g33() const; - const FieldMetric& g12() const; - const FieldMetric& g13() const; - const FieldMetric& g23() const; + const MetricTensor::FieldMetric& g11() const { return contravariantMetricTensor.g11(); } + const MetricTensor::FieldMetric& g22() const { return contravariantMetricTensor.g22(); } + const MetricTensor::FieldMetric& g33() const { return contravariantMetricTensor.g33(); } + const MetricTensor::FieldMetric& g12() const { return contravariantMetricTensor.g12(); } + const MetricTensor::FieldMetric& g13() const { return contravariantMetricTensor.g13(); } + const MetricTensor::FieldMetric& g23() const { return contravariantMetricTensor.g23(); } + + const ContravariantMetricTensor& getContravariantMetricTensor() const { + return contravariantMetricTensor; + } - const ContravariantMetricTensor& getContravariantMetricTensor() const; - const CovariantMetricTensor& getCovariantMetricTensor() const; + const CovariantMetricTensor& getCovariantMetricTensor() const { + return covariantMetricTensor; + } void setContravariantMetricTensor(const ContravariantMetricTensor& metric_tensor, const std::string& region = "RGN_ALL", @@ -135,7 +140,7 @@ public: const FieldMetric& J() const; ///< Magnitude of B = nabla z times nabla x - const FieldMetric& Bxy() const; + const FieldMetric& Bxy() const { return Bxy_; } void setJ(FieldMetric J); void setJ(BoutReal value, int x, int y); @@ -143,11 +148,14 @@ public: void setBxy(FieldMetric Bxy); /// d pitch angle / dx. Needed for vector differentials (Curl) - const FieldMetric& ShiftTorsion() const; + const FieldMetric& ShiftTorsion() const { return ShiftTorsion_; } ///< Integrated shear (I in BOUT notation) - const FieldMetric& IntShiftTorsion() const; - void setIntShiftTorsion(FieldMetric IntShiftTorsion); + const FieldMetric& IntShiftTorsion() const { return IntShiftTorsion_; } + + void setIntShiftTorsion(FieldMetric IntShiftTorsion) { + IntShiftTorsion_ = std::move(IntShiftTorsion); + } /// Calculate differential geometry quantities from the metric tensor int communicateAndCheckMeshSpacing() const; @@ -250,34 +258,32 @@ public: Field2D Laplace_perpXY(const Field2D& A, const Field2D& f) const; /// Christoffel symbol of the second kind (connection coefficients) - const FieldMetric& G1_11() const; - const FieldMetric& G1_22() const; - const FieldMetric& G1_33() const; - const FieldMetric& G1_12() const; - const FieldMetric& G1_13() const; - const FieldMetric& G1_23() const; - - const FieldMetric& G2_11() const; - const FieldMetric& G2_22() const; - const FieldMetric& G2_33() const; - const FieldMetric& G2_12() const; - const FieldMetric& G2_13() const; - const FieldMetric& G2_23() const; - - const FieldMetric& G3_11() const; - const FieldMetric& G3_22() const; - const FieldMetric& G3_33() const; - const FieldMetric& G3_12() const; - const FieldMetric& G3_13() const; - const FieldMetric& G3_23() const; - - const FieldMetric& G1() const; - const FieldMetric& G2() const; - const FieldMetric& G3() const; - - void setG1(const FieldMetric& G1) const; - void setG2(const FieldMetric& G2) const; - void setG3(const FieldMetric& G3) const; + const FieldMetric& G1_11() const { return christoffel_symbols().G1_11(); } + const FieldMetric& G1_22() const { return christoffel_symbols().G1_22(); } + const FieldMetric& G1_33() const { return christoffel_symbols().G1_33(); } + const FieldMetric& G1_12() const { return christoffel_symbols().G1_12(); } + const FieldMetric& G1_13() const { return christoffel_symbols().G1_13(); } + const FieldMetric& G1_23() const { return christoffel_symbols().G1_23(); } + const FieldMetric& G2_11() const { return christoffel_symbols().G2_11(); } + const FieldMetric& G2_22() const { return christoffel_symbols().G2_22(); } + const FieldMetric& G2_33() const { return christoffel_symbols().G2_33(); } + const FieldMetric& G2_12() const { return christoffel_symbols().G2_12(); } + const FieldMetric& G2_13() const { return christoffel_symbols().G2_13(); } + const FieldMetric& G2_23() const { return christoffel_symbols().G2_23(); } + const FieldMetric& G3_11() const { return christoffel_symbols().G3_11(); } + const FieldMetric& G3_22() const { return christoffel_symbols().G3_22(); } + const FieldMetric& G3_33() const { return christoffel_symbols().G3_33(); } + const FieldMetric& G3_12() const { return christoffel_symbols().G3_12(); } + const FieldMetric& G3_13() const { return christoffel_symbols().G3_13(); } + const FieldMetric& G3_23() const { return christoffel_symbols().G3_23(); } + + const FieldMetric& G1() const { return g_values().G1(); } + const FieldMetric& G2() const { return g_values().G2(); } + const FieldMetric& G3() const { return g_values().G3(); } + + void setG1(const FieldMetric& G1) const { g_values().setG1(G1); } + void setG2(const FieldMetric& G2) const { g_values().setG2(G2); } + void setG3(const FieldMetric& G3) const { g_values().setG3(G3); } const FieldMetric& Grad2_par2_DDY_invSg(CELL_LOC outloc, const std::string& method) const; diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 602d948c3b..34d087f4cb 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -1369,73 +1369,6 @@ void Coordinates::checkContravariant() { contravariantMetricTensor.check(localmesh->ystart); } -bool Coordinates::non_uniform() const { return non_uniform_; } -void Coordinates::setNon_uniform(bool non_uniform) { non_uniform_ = non_uniform; } - -const FieldMetric& Coordinates::dx() const { return dx_; } -const FieldMetric& Coordinates::dy() const { return dy_; } -const FieldMetric& Coordinates::dz() const { return dz_; } - -void Coordinates::setDx(FieldMetric dx) { dx_ = std::move(dx); } -void Coordinates::setDy(FieldMetric dy) { dy_ = std::move(dy); } -void Coordinates::setDz(FieldMetric dz) { dz_ = std::move(dz); } - -void Coordinates::setDy(BoutReal value, int x, int y) { dy_(x, y) = value; } - -void Coordinates::setD1_dx(FieldMetric d1_dx) { d1_dx_ = std::move(d1_dx); } -void Coordinates::setD1_dy(FieldMetric d1_dy) { d1_dy_ = std::move(d1_dy); } -void Coordinates::setD1_dz(FieldMetric d1_dz) { d1_dz_ = std::move(d1_dz); } - -const FieldMetric& Coordinates::d1_dx() const { return d1_dx_; } -const FieldMetric& Coordinates::d1_dy() const { return d1_dy_; } -const FieldMetric& Coordinates::d1_dz() const { return d1_dz_; } - -const FieldMetric& Coordinates::ShiftTorsion() const { return ShiftTorsion_; } - -const FieldMetric& Coordinates::IntShiftTorsion() const { return IntShiftTorsion_; } - -void Coordinates::setIntShiftTorsion(FieldMetric IntShiftTorsion) { - IntShiftTorsion_ = std::move(IntShiftTorsion); -} - -const MetricTensor::FieldMetric& Coordinates::g_11() const { - return covariantMetricTensor.g11(); -} -const MetricTensor::FieldMetric& Coordinates::g_22() const { - return covariantMetricTensor.g22(); -} -const MetricTensor::FieldMetric& Coordinates::g_33() const { - return covariantMetricTensor.g33(); -} -const MetricTensor::FieldMetric& Coordinates::g_12() const { - return covariantMetricTensor.g12(); -} -const MetricTensor::FieldMetric& Coordinates::g_13() const { - return covariantMetricTensor.g13(); -} -const MetricTensor::FieldMetric& Coordinates::g_23() const { - return covariantMetricTensor.g23(); -} - -const MetricTensor::FieldMetric& Coordinates::g11() const { - return contravariantMetricTensor.g11(); -} -const MetricTensor::FieldMetric& Coordinates::g22() const { - return contravariantMetricTensor.g22(); -} -const MetricTensor::FieldMetric& Coordinates::g33() const { - return contravariantMetricTensor.g33(); -} -const MetricTensor::FieldMetric& Coordinates::g12() const { - return contravariantMetricTensor.g12(); -} -const MetricTensor::FieldMetric& Coordinates::g13() const { - return contravariantMetricTensor.g13(); -} -const MetricTensor::FieldMetric& Coordinates::g23() const { - return contravariantMetricTensor.g23(); -} - const FieldMetric& Coordinates::J() const { if (jacobian_cache == nullptr) { const auto j = recalculateJacobian(); @@ -1459,53 +1392,16 @@ void Coordinates::setJ(BoutReal value, int x, int y) { jacobian_cache = std::move(ptr); } -const FieldMetric& Coordinates::Bxy() const { return Bxy_; } - void Coordinates::setBxy(FieldMetric Bxy) { //TODO: Calculate Bxy and check value is close Bxy_ = std::move(Bxy); } -const FieldMetric& Coordinates::G1_11() const { return christoffel_symbols().G1_11(); } -const FieldMetric& Coordinates::G1_22() const { return christoffel_symbols().G1_22(); } -const FieldMetric& Coordinates::G1_33() const { return christoffel_symbols().G1_33(); } -const FieldMetric& Coordinates::G1_12() const { return christoffel_symbols().G1_12(); } -const FieldMetric& Coordinates::G1_13() const { return christoffel_symbols().G1_13(); } -const FieldMetric& Coordinates::G1_23() const { return christoffel_symbols().G1_23(); } -const FieldMetric& Coordinates::G2_11() const { return christoffel_symbols().G2_11(); } -const FieldMetric& Coordinates::G2_22() const { return christoffel_symbols().G2_22(); } -const FieldMetric& Coordinates::G2_33() const { return christoffel_symbols().G2_33(); } -const FieldMetric& Coordinates::G2_12() const { return christoffel_symbols().G2_12(); } -const FieldMetric& Coordinates::G2_13() const { return christoffel_symbols().G2_13(); } -const FieldMetric& Coordinates::G2_23() const { return christoffel_symbols().G2_23(); } -const FieldMetric& Coordinates::G3_11() const { return christoffel_symbols().G3_11(); } -const FieldMetric& Coordinates::G3_22() const { return christoffel_symbols().G3_22(); } -const FieldMetric& Coordinates::G3_33() const { return christoffel_symbols().G3_33(); } -const FieldMetric& Coordinates::G3_12() const { return christoffel_symbols().G3_12(); } -const FieldMetric& Coordinates::G3_13() const { return christoffel_symbols().G3_13(); } -const FieldMetric& Coordinates::G3_23() const { return christoffel_symbols().G3_23(); } - -const FieldMetric& Coordinates::G1() const { return g_values().G1(); } -const FieldMetric& Coordinates::G2() const { return g_values().G2(); } -const FieldMetric& Coordinates::G3() const { return g_values().G3(); } - -void Coordinates::setG1(const FieldMetric& G1) const { g_values().setG1(G1); } -void Coordinates::setG2(const FieldMetric& G2) const { g_values().setG2(G2); } -void Coordinates::setG3(const FieldMetric& G3) const { g_values().setG3(G3); } - void Coordinates::applyToChristoffelSymbols( const std::function& function) const { christoffel_symbols().map(function); } -const ContravariantMetricTensor& Coordinates::getContravariantMetricTensor() const { - return contravariantMetricTensor; -} - -const CovariantMetricTensor& Coordinates::getCovariantMetricTensor() const { - return covariantMetricTensor; -} - void Coordinates::setContravariantMetricTensor( const ContravariantMetricTensor& metric_tensor, const std::string& region, bool recalculate_staggered, bool force_interpolate_from_centre) { From c14070a9dcaa38fe3abb576f2b95ded367968e79 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Thu, 22 Feb 2024 09:27:37 +0000 Subject: [PATCH 333/491] Move trivial getters and setters of ChristoffelSymbols class to header file. --- include/bout/christoffel_symbols.hxx | 63 ++++++++++++++++++---------- src/mesh/christoffel_symbols.cxx | 50 ---------------------- 2 files changed, 40 insertions(+), 73 deletions(-) diff --git a/include/bout/christoffel_symbols.hxx b/include/bout/christoffel_symbols.hxx index 0f34892236..4dc5f4474b 100644 --- a/include/bout/christoffel_symbols.hxx +++ b/include/bout/christoffel_symbols.hxx @@ -26,28 +26,26 @@ public: // ChristoffelSymbols(BoutReal g11, BoutReal g22, BoutReal g33, BoutReal g12, BoutReal g13, // BoutReal g23, Mesh* mesh); - // ChristoffelSymbols() = default; - - const FieldMetric& G1_11() const; - const FieldMetric& G1_22() const; - const FieldMetric& G1_33() const; - const FieldMetric& G1_12() const; - const FieldMetric& G1_13() const; - const FieldMetric& G1_23() const; - - const FieldMetric& G2_11() const; - const FieldMetric& G2_22() const; - const FieldMetric& G2_33() const; - const FieldMetric& G2_12() const; - const FieldMetric& G2_13() const; - const FieldMetric& G2_23() const; - - const FieldMetric& G3_11() const; - const FieldMetric& G3_22() const; - const FieldMetric& G3_33() const; - const FieldMetric& G3_12() const; - const FieldMetric& G3_13() const; - const FieldMetric& G3_23() const; + const FieldMetric& G1_11() const { return G1_11_; } + const FieldMetric& G1_22() const { return G1_22_; } + const FieldMetric& G1_33() const { return G1_33_; } + const FieldMetric& G1_12() const { return G1_12_; } + const FieldMetric& G1_13() const { return G1_13_; } + const FieldMetric& G1_23() const { return G1_23_; } + + const FieldMetric& G2_11() const { return G2_11_; } + const FieldMetric& G2_22() const { return G2_22_; } + const FieldMetric& G2_33() const { return G2_33_; } + const FieldMetric& G2_12() const { return G2_12_; } + const FieldMetric& G2_13() const { return G2_13_; } + const FieldMetric& G2_23() const { return G2_23_; } + + const FieldMetric& G3_11() const { return G3_11_; } + const FieldMetric& G3_22() const { return G3_22_; } + const FieldMetric& G3_33() const { return G3_33_; } + const FieldMetric& G3_12() const { return G3_12_; } + const FieldMetric& G3_13() const { return G3_13_; } + const FieldMetric& G3_23() const { return G3_23_; } void setChristoffelSymbols(const FieldMetric& G1_11, const FieldMetric& G1_22, const FieldMetric& G1_33, const FieldMetric& G1_12, @@ -57,7 +55,26 @@ public: const FieldMetric& G2_13, const FieldMetric& G2_23, const FieldMetric& G3_11, const FieldMetric& G3_22, const FieldMetric& G3_33, const FieldMetric& G3_12, - const FieldMetric& G3_13, const FieldMetric& G3_23); + const FieldMetric& G3_13, const FieldMetric& G3_23) { + G1_11_ = G1_11; + G1_22_ = G1_22; + G1_33_ = G1_33; + G1_12_ = G1_12; + G1_13_ = G1_13; + G1_23_ = G1_23; + G2_11_ = G2_11; + G2_22_ = G2_22; + G2_33_ = G2_33; + G2_12_ = G2_12; + G2_13_ = G2_13; + G2_23_ = G2_23; + G3_11_ = G3_11; + G3_22_ = G3_22; + G3_33_ = G3_33; + G3_12_ = G3_12; + G3_13_ = G3_13; + G3_23_ = G3_23; + } // void Allocate(); // diff --git a/src/mesh/christoffel_symbols.cxx b/src/mesh/christoffel_symbols.cxx index 75bb5318ea..a7084310bb 100644 --- a/src/mesh/christoffel_symbols.cxx +++ b/src/mesh/christoffel_symbols.cxx @@ -120,56 +120,6 @@ ChristoffelSymbols::ChristoffelSymbols(const Coordinates& coordinates) { + 0.5 * g33 * coordinates.DDY(g_33); } -//ChristoffelSymbols::ChristoffelSymbols() {} - -const FieldMetric& ChristoffelSymbols::G1_11() const { return G1_11_; } -const FieldMetric& ChristoffelSymbols::G1_22() const { return G1_22_; } -const FieldMetric& ChristoffelSymbols::G1_33() const { return G1_33_; } -const FieldMetric& ChristoffelSymbols::G1_12() const { return G1_12_; } -const FieldMetric& ChristoffelSymbols::G1_13() const { return G1_13_; } -const FieldMetric& ChristoffelSymbols::G1_23() const { return G1_23_; } - -const FieldMetric& ChristoffelSymbols::G2_11() const { return G2_11_; } -const FieldMetric& ChristoffelSymbols::G2_22() const { return G2_22_; } -const FieldMetric& ChristoffelSymbols::G2_33() const { return G2_33_; } -const FieldMetric& ChristoffelSymbols::G2_12() const { return G2_12_; } -const FieldMetric& ChristoffelSymbols::G2_13() const { return G2_13_; } -const FieldMetric& ChristoffelSymbols::G2_23() const { return G2_23_; } - -const FieldMetric& ChristoffelSymbols::G3_11() const { return G3_11_; } -const FieldMetric& ChristoffelSymbols::G3_22() const { return G3_22_; } -const FieldMetric& ChristoffelSymbols::G3_33() const { return G3_33_; } -const FieldMetric& ChristoffelSymbols::G3_12() const { return G3_12_; } -const FieldMetric& ChristoffelSymbols::G3_13() const { return G3_13_; } -const FieldMetric& ChristoffelSymbols::G3_23() const { return G3_23_; } - -void ChristoffelSymbols::setChristoffelSymbols( - const FieldMetric& G1_11, const FieldMetric& G1_22, const FieldMetric& G1_33, - const FieldMetric& G1_12, const FieldMetric& G1_13, const FieldMetric& G1_23, - const FieldMetric& G2_11, const FieldMetric& G2_22, const FieldMetric& G2_33, - const FieldMetric& G2_12, const FieldMetric& G2_13, const FieldMetric& G2_23, - const FieldMetric& G3_11, const FieldMetric& G3_22, const FieldMetric& G3_33, - const FieldMetric& G3_12, const FieldMetric& G3_13, const FieldMetric& G3_23) { - G1_11_ = G1_11; - G1_22_ = G1_22; - G1_33_ = G1_33; - G1_12_ = G1_12; - G1_13_ = G1_13; - G1_23_ = G1_23; - G2_11_ = G2_11; - G2_22_ = G2_22; - G2_33_ = G2_33; - G2_12_ = G2_12; - G2_13_ = G2_13; - G2_23_ = G2_23; - G3_11_ = G3_11; - G3_22_ = G3_22; - G3_33_ = G3_33; - G3_12_ = G3_12; - G3_13_ = G3_13; - G3_23_ = G3_23; -} - void ChristoffelSymbols::map( const std::function& function) { From e8d5a7e8a79cf74789915c14a7a44d42c1a193f1 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Thu, 22 Feb 2024 09:29:32 +0000 Subject: [PATCH 334/491] Move trivial getters and setters of GValues class to header file. --- include/bout/g_values.hxx | 12 ++++++------ src/mesh/g_values.cxx | 8 -------- 2 files changed, 6 insertions(+), 14 deletions(-) diff --git a/include/bout/g_values.hxx b/include/bout/g_values.hxx index de11eb7625..22f1c9fced 100644 --- a/include/bout/g_values.hxx +++ b/include/bout/g_values.hxx @@ -14,13 +14,13 @@ public: explicit GValues(const Coordinates& coordinates); - const FieldMetric& G1() const; - const FieldMetric& G2() const; - const FieldMetric& G3() const; + const FieldMetric& G1() const { return G1_; } + const FieldMetric& G2() const { return G2_; } + const FieldMetric& G3() const { return G3_; } - void setG1(const FieldMetric& G1); - void setG2(const FieldMetric& G2); - void setG3(const FieldMetric& G3); + void setG1(const FieldMetric& G1) { G1_ = G1; } + void setG2(const FieldMetric& G2) { G2_ = G2; } + void setG3(const FieldMetric& G3) { G3_ = G3; } private: FieldMetric G1_, G2_, G3_; diff --git a/src/mesh/g_values.cxx b/src/mesh/g_values.cxx index cf1b919161..bb2835d51d 100644 --- a/src/mesh/g_values.cxx +++ b/src/mesh/g_values.cxx @@ -27,11 +27,3 @@ GValues::GValues(const Coordinates& coordinates) { coordinates.communicate(tmp); setG3((coordinates.DDX(J * g13) + coordinates.DDY(tmp) + coordinates.DDZ(J * g33)) / J); } - -const FieldMetric& GValues::G1() const { return G1_; } -const FieldMetric& GValues::G2() const { return G2_; } -const FieldMetric& GValues::G3() const { return G3_; } - -void GValues::setG1(const FieldMetric& G1) { G1_ = G1; } -void GValues::setG2(const FieldMetric& G2) { G2_ = G2; } -void GValues::setG3(const FieldMetric& G3) { G3_ = G3; } From 5ee546bd8319a3e801fcce287cd8556298eb8740 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Thu, 22 Feb 2024 09:33:08 +0000 Subject: [PATCH 335/491] Move trivial getters and setters (and setLocation() method) of MetricTensor class to header file. --- include/bout/metricTensor.hxx | 35 +++++++++++++++++++++++++---------- src/mesh/metricTensor.cxx | 26 -------------------------- 2 files changed, 25 insertions(+), 36 deletions(-) diff --git a/include/bout/metricTensor.hxx b/include/bout/metricTensor.hxx index 26a26b01a8..f2c894ba86 100644 --- a/include/bout/metricTensor.hxx +++ b/include/bout/metricTensor.hxx @@ -24,16 +24,31 @@ public: // check that tensors are positive (if expected) and finite (always) void check(int ystart); - const FieldMetric& g11() const; - const FieldMetric& g22() const; - const FieldMetric& g33() const; - const FieldMetric& g12() const; - const FieldMetric& g13() const; - const FieldMetric& g23() const; - - void setMetricTensor(const MetricTensor& metric_tensor); - - void setLocation(CELL_LOC location); + const FieldMetric& g11() const { return g11_; } + const FieldMetric& g22() const { return g22_; } + const FieldMetric& g33() const { return g33_; } + const FieldMetric& g12() const { return g12_; } + const FieldMetric& g13() const { return g13_; } + const FieldMetric& g23() const { return g23_; } + + void setMetricTensor(const MetricTensor& metric_tensor) { + + g11_ = metric_tensor.g11(); + g22_ = metric_tensor.g22(); + g33_ = metric_tensor.g33(); + g12_ = metric_tensor.g12(); + g13_ = metric_tensor.g13(); + g23_ = metric_tensor.g23(); + } + + void setLocation(const CELL_LOC location) { + g11_.setLocation(location); + g22_.setLocation(location); + g33_.setLocation(location); + g12_.setLocation(location); + g13_.setLocation(location); + g23_.setLocation(location); + } MetricTensor inverse(const std::string& region = "RGN_ALL"); diff --git a/src/mesh/metricTensor.cxx b/src/mesh/metricTensor.cxx index b61529fa3d..5786837d63 100644 --- a/src/mesh/metricTensor.cxx +++ b/src/mesh/metricTensor.cxx @@ -14,23 +14,6 @@ MetricTensor::MetricTensor(const BoutReal g11, const BoutReal g22, const BoutRea : g11_(g11, mesh), g22_(g22, mesh), g33_(g33, mesh), g12_(g12, mesh), g13_(g13, mesh), g23_(g23, mesh) {} -const MetricTensor::FieldMetric& MetricTensor::g11() const { return g11_; } -const MetricTensor::FieldMetric& MetricTensor::g22() const { return g22_; } -const MetricTensor::FieldMetric& MetricTensor::g33() const { return g33_; } -const MetricTensor::FieldMetric& MetricTensor::g12() const { return g12_; } -const MetricTensor::FieldMetric& MetricTensor::g13() const { return g13_; } -const MetricTensor::FieldMetric& MetricTensor::g23() const { return g23_; } - -void MetricTensor::setMetricTensor(const MetricTensor& metric_tensor) { - - g11_ = metric_tensor.g11(); - g22_ = metric_tensor.g22(); - g33_ = metric_tensor.g33(); - g12_ = metric_tensor.g12(); - g13_ = metric_tensor.g13(); - g23_ = metric_tensor.g23(); -} - void MetricTensor::check(int ystart) { // Diagonal metric components should be finite bout::checkFinite(g11_, "g11", "RGN_NOCORNERS"); @@ -83,15 +66,6 @@ void MetricTensor::check(int ystart) { } } -void MetricTensor::setLocation(const CELL_LOC location) { - g11_.setLocation(location); - g22_.setLocation(location); - g33_.setLocation(location); - g12_.setLocation(location); - g13_.setLocation(location); - g23_.setLocation(location); -} - MetricTensor MetricTensor::inverse(const std::string& region) { TRACE("MetricTensor::inverse"); From 49378cd7b0310f55eedc111e181ca857c6ce24ed Mon Sep 17 00:00:00 2001 From: tomc271 <58003025+tomc271@users.noreply.github.com> Date: Fri, 23 Feb 2024 16:27:25 +0000 Subject: [PATCH 336/491] No need to pre-assign values to explanatory variables before using to construct a MetricTensor. Co-authored-by: Ben Dudson --- examples/2Dturbulence_multigrid/esel.cxx | 19 ++----------------- 1 file changed, 2 insertions(+), 17 deletions(-) diff --git a/examples/2Dturbulence_multigrid/esel.cxx b/examples/2Dturbulence_multigrid/esel.cxx index fd94808f58..02a8b68bc1 100644 --- a/examples/2Dturbulence_multigrid/esel.cxx +++ b/examples/2Dturbulence_multigrid/esel.cxx @@ -71,23 +71,8 @@ class ESEL : public PhysicsModel { // generate coordinate system coord->setBxy(1); - MetricTensor::FieldMetric g11, g22, g33, g12, g13, g23; - g11 = 1.0; - g22 = 1.0; - g33 = 1.0; - g12 = 0.0; - g13 = 0.0; - g23 = 0.0; - coord->setContravariantMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); - - MetricTensor::FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; - g_11 = 1.0; - g_22 = 1.0; - g_33 = 1.0; - g_12 = 0.0; - g_13 = 0.0; - g_23 = 0.0; - coord->setCovariantMetricTensor(CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + coord->setContravariantMetricTensor(ContravariantMetricTensor(1.0, 1.0, 1.0, 0.0, 0.0, 0.0)); + coord->setCovariantMetricTensor(CovariantMetricTensor(1.0, 1.0, 1.0, 0.0, 0.0, 0.0)); SOLVE_FOR(N, vort); SAVE_REPEAT(phi); From f4cbb8ce5b9ae86b8e40bc2e3fe4680ebb88646c Mon Sep 17 00:00:00 2001 From: tomc271 Date: Tue, 5 Mar 2024 10:15:20 +0000 Subject: [PATCH 337/491] Remove obsolete inverse() and jacobian() in boutcpp.pxd.jinja file. --- tools/pylib/_boutpp_build/boutcpp.pxd.jinja | 3 --- 1 file changed, 3 deletions(-) diff --git a/tools/pylib/_boutpp_build/boutcpp.pxd.jinja b/tools/pylib/_boutpp_build/boutcpp.pxd.jinja index 02ce267d49..dccd37d404 100644 --- a/tools/pylib/_boutpp_build/boutcpp.pxd.jinja +++ b/tools/pylib/_boutpp_build/boutcpp.pxd.jinja @@ -79,9 +79,6 @@ cdef extern from "bout/coordinates.hxx": {{ metric_field }} ShiftTorsion {{ metric_field }} IntShiftTorsion int communicateAndCheckMeshSpacing() - int inverse() - int inverse() - int jacobian() cdef extern from "bout/fieldgroup.hxx": cppclass FieldGroup: From 1293ff613e3c912e0f133957f0323dda559be56a Mon Sep 17 00:00:00 2001 From: tomc271 Date: Tue, 5 Mar 2024 11:04:18 +0000 Subject: [PATCH 338/491] Restore semicolons after macros. --- examples/advdiff/advdiff.cxx | 2 +- examples/advdiff2/init.cxx | 2 +- include/bout/coordinates.hxx | 2 +- include/bout/mesh.hxx | 4 +- src/field/vector3d.cxx | 2 +- src/invert/parderiv/impls/cyclic/cyclic.cxx | 4 +- src/mesh/boundary_standard.cxx | 80 ++++++++++----------- src/mesh/coordinates.cxx | 30 ++++---- src/mesh/difops.cxx | 44 ++++++------ src/mesh/fv_ops.cxx | 6 +- src/mesh/index_derivs.cxx | 2 +- src/mesh/mesh.cxx | 4 +- tests/MMS/hw/hw.cxx | 4 +- tests/MMS/spatial/diffusion/diffusion.cxx | 2 +- 14 files changed, 94 insertions(+), 94 deletions(-) diff --git a/examples/advdiff/advdiff.cxx b/examples/advdiff/advdiff.cxx index 0d36f0671f..fc4d18b5f3 100644 --- a/examples/advdiff/advdiff.cxx +++ b/examples/advdiff/advdiff.cxx @@ -28,7 +28,7 @@ class AdvDiff : public PhysicsModel { // read options // Set evolving variables - SOLVE_FOR(V) + SOLVE_FOR(V); if (!restarting) { // Set variables to these values (+ the initial perturbation) diff --git a/examples/advdiff2/init.cxx b/examples/advdiff2/init.cxx index 5dd0a1d6ba..6fe82a7552 100644 --- a/examples/advdiff2/init.cxx +++ b/examples/advdiff2/init.cxx @@ -15,7 +15,7 @@ int AdvDiff::init(bool restarting) { // read options // Set evolving variables - SOLVE_FOR(V) + SOLVE_FOR(V); if (!restarting) { // Set variables to these values (+ the initial perturbation) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index b33f24601a..4f8e4f4207 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -172,7 +172,7 @@ public: /// Return the parallel transform ParallelTransform& getParallelTransform() { - ASSERT1(transform != nullptr) + ASSERT1(transform != nullptr); return *transform; } diff --git a/include/bout/mesh.hxx b/include/bout/mesh.hxx index b006fa7cd0..d74b02919d 100644 --- a/include/bout/mesh.hxx +++ b/include/bout/mesh.hxx @@ -618,8 +618,8 @@ public: std::shared_ptr getCoordinatesSmart(const CELL_LOC location = CELL_CENTRE) { - ASSERT1(location != CELL_DEFAULT) - ASSERT1(location != CELL_VSHIFT) + ASSERT1(location != CELL_DEFAULT); + ASSERT1(location != CELL_VSHIFT); auto found = coords_map.find(location); if (found != coords_map.end()) { diff --git a/src/field/vector3d.cxx b/src/field/vector3d.cxx index d972f02e8e..9dcc0757fb 100644 --- a/src/field/vector3d.cxx +++ b/src/field/vector3d.cxx @@ -476,7 +476,7 @@ const Field3D Vector3D::operator*(const Vector3D& rhs) const { Mesh* mesh = getMesh(); Field3D result{emptyFrom(x)}; - ASSERT2(location == rhs.getLocation()) + ASSERT2(location == rhs.getLocation()); if (rhs.covariant ^ covariant) { // Both different - just multiply components diff --git a/src/invert/parderiv/impls/cyclic/cyclic.cxx b/src/invert/parderiv/impls/cyclic/cyclic.cxx index 046639f5e7..bc3e4a2846 100644 --- a/src/invert/parderiv/impls/cyclic/cyclic.cxx +++ b/src/invert/parderiv/impls/cyclic/cyclic.cxx @@ -64,8 +64,8 @@ InvertParCR::InvertParCR(Options* opt, CELL_LOC location, Mesh* mesh_in) const Field3D InvertParCR::solve(const Field3D& f) { TRACE("InvertParCR::solve(Field3D)"); - ASSERT1(localmesh == f.getMesh()) - ASSERT1(location == f.getLocation()) + ASSERT1(localmesh == f.getMesh()); + ASSERT1(location == f.getLocation()); Field3D result = emptyFrom(f).setDirectionY(YDirectionType::Aligned); diff --git a/src/mesh/boundary_standard.cxx b/src/mesh/boundary_standard.cxx index 6f71a5b5e9..27851ab489 100644 --- a/src/mesh/boundary_standard.cxx +++ b/src/mesh/boundary_standard.cxx @@ -137,7 +137,7 @@ void BoundaryDirichlet::apply(Field2D& f, BoutReal t) { // cell to be val N.B. Only first guard cells (closest to the grid) should ever be used Mesh* mesh = bndry->localmesh; - ASSERT1(mesh == f.getMesh()) + ASSERT1(mesh == f.getMesh()); bndry->first(); // Decide which generator to use @@ -300,7 +300,7 @@ void BoundaryDirichlet::apply(Field3D& f, BoutReal t) { // cell to be val N.B. Only first guard cells (closest to the grid) should ever be used Mesh* mesh = bndry->localmesh; - ASSERT1(mesh == f.getMesh()) + ASSERT1(mesh == f.getMesh()); bndry->first(); // Decide which generator to use @@ -591,7 +591,7 @@ void BoundaryDirichlet::apply_ddt(Field2D& f) { void BoundaryDirichlet::apply_ddt(Field3D& f) { Mesh* mesh = bndry->localmesh; - ASSERT1(mesh == f.getMesh()) + ASSERT1(mesh == f.getMesh()); Field3D* dt = f.timeDeriv(); for (bndry->first(); !bndry->isDone(); bndry->next()) { @@ -622,7 +622,7 @@ void BoundaryDirichlet_O3::apply(Field2D& f, BoutReal t) { // cell to be val N.B. Only first guard cells (closest to the grid) should ever be used Mesh* mesh = bndry->localmesh; - ASSERT1(mesh == f.getMesh()) + ASSERT1(mesh == f.getMesh()); bndry->first(); // Decide which generator to use @@ -802,7 +802,7 @@ void BoundaryDirichlet_O3::apply(Field3D& f, BoutReal t) { // cell to be val N.B. Only first guard cells (closest to the grid) should ever be used Mesh* mesh = bndry->localmesh; - ASSERT1(mesh == f.getMesh()) + ASSERT1(mesh == f.getMesh()); bndry->first(); // Decide which generator to use @@ -1032,7 +1032,7 @@ void BoundaryDirichlet_O3::apply_ddt(Field2D& f) { void BoundaryDirichlet_O3::apply_ddt(Field3D& f) { Mesh* mesh = bndry->localmesh; - ASSERT1(mesh == f.getMesh()) + ASSERT1(mesh == f.getMesh()); Field3D* dt = f.timeDeriv(); bndry->first(); @@ -1064,7 +1064,7 @@ void BoundaryDirichlet_O4::apply(Field2D& f, BoutReal t) { // cell to be val N.B. Only first guard cells (closest to the grid) should ever be used Mesh* mesh = bndry->localmesh; - ASSERT1(mesh == f.getMesh()) + ASSERT1(mesh == f.getMesh()); bndry->first(); // Decide which generator to use @@ -1259,7 +1259,7 @@ void BoundaryDirichlet_O4::apply(Field3D& f, BoutReal t) { // cell to be val N.B. Only first guard cells (closest to the grid) should ever be used Mesh* mesh = bndry->localmesh; - ASSERT1(mesh == f.getMesh()) + ASSERT1(mesh == f.getMesh()); bndry->first(); // Decide which generator to use @@ -1501,7 +1501,7 @@ void BoundaryDirichlet_O4::apply_ddt(Field2D& f) { void BoundaryDirichlet_O4::apply_ddt(Field3D& f) { Mesh* mesh = bndry->localmesh; - ASSERT1(mesh == f.getMesh()) + ASSERT1(mesh == f.getMesh()); Field3D* dt = f.timeDeriv(); for (bndry->first(); !bndry->isDone(); bndry->next()) { for (int z = 0; z < mesh->LocalNz; z++) { @@ -1542,7 +1542,7 @@ void BoundaryDirichlet_4thOrder::apply(Field2D& f) { void BoundaryDirichlet_4thOrder::apply(Field3D& f) { Mesh* mesh = bndry->localmesh; - ASSERT1(mesh == f.getMesh()) + ASSERT1(mesh == f.getMesh()); // Set (at 4th order) the value at the mid-point between the guard cell and the grid // cell to be val for (bndry->first(); !bndry->isDone(); bndry->next1d()) { @@ -1570,7 +1570,7 @@ void BoundaryDirichlet_4thOrder::apply_ddt(Field2D& f) { void BoundaryDirichlet_4thOrder::apply_ddt(Field3D& f) { Mesh* mesh = bndry->localmesh; - ASSERT1(mesh == f.getMesh()) + ASSERT1(mesh == f.getMesh()); Field3D* dt = f.timeDeriv(); for (bndry->first(); !bndry->isDone(); bndry->next()) { for (int z = 0; z < mesh->LocalNz; z++) { @@ -1596,7 +1596,7 @@ BoundaryOp* BoundaryNeumann_NonOrthogonal::clone(BoundaryRegion* region, void BoundaryNeumann_NonOrthogonal::apply(Field2D& f) { #if not(BOUT_USE_METRIC_3D) Mesh* mesh = bndry->localmesh; - ASSERT1(mesh == f.getMesh()) + ASSERT1(mesh == f.getMesh()); Coordinates* metric = f.getCoordinates(); // Calculate derivatives for metric use @@ -1650,7 +1650,7 @@ void BoundaryNeumann_NonOrthogonal::apply(Field2D& f) { void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { Mesh* mesh = bndry->localmesh; - ASSERT1(mesh == f.getMesh()) + ASSERT1(mesh == f.getMesh()); Coordinates* metric = f.getCoordinates(); // Calculate derivatives for metric use @@ -1736,7 +1736,7 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { #if not(BOUT_USE_METRIC_3D) Mesh* mesh = bndry->localmesh; - ASSERT1(mesh == f.getMesh()) + ASSERT1(mesh == f.getMesh()); Coordinates* metric = f.getCoordinates(); bndry->first(); @@ -1940,7 +1940,7 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { void BoundaryNeumann::apply(Field3D & f, BoutReal t) { Mesh* mesh = bndry->localmesh; - ASSERT1(mesh == f.getMesh()) + ASSERT1(mesh == f.getMesh()); Coordinates* metric = f.getCoordinates(); bndry->first(); @@ -2199,7 +2199,7 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { void BoundaryNeumann::apply_ddt(Field3D & f) { Mesh* mesh = bndry->localmesh; - ASSERT1(mesh == f.getMesh()) + ASSERT1(mesh == f.getMesh()); Field3D* dt = f.timeDeriv(); for (bndry->first(); !bndry->isDone(); bndry->next()) { for (int z = 0; z < mesh->LocalNz; z++) { @@ -2225,7 +2225,7 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { void BoundaryNeumann_O4::apply(Field2D & f, BoutReal t) { #if not(BOUT_USE_METRIC_3D) Mesh* mesh = bndry->localmesh; - ASSERT1(mesh == f.getMesh()) + ASSERT1(mesh == f.getMesh()); // Set (at 4th order) the value at the mid-point between the guard cell and the grid // cell to be val N.B. Only first guard cells (closest to the grid) should ever be @@ -2281,7 +2281,7 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { void BoundaryNeumann_O4::apply(Field3D & f, BoutReal t) { Mesh* mesh = bndry->localmesh; - ASSERT1(mesh == f.getMesh()) + ASSERT1(mesh == f.getMesh()); bndry->first(); // Decide which generator to use @@ -2332,7 +2332,7 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { void BoundaryNeumann_O4::apply_ddt(Field3D & f) { Mesh* mesh = bndry->localmesh; - ASSERT1(mesh == f.getMesh()) + ASSERT1(mesh == f.getMesh()); Field3D* dt = f.timeDeriv(); for (bndry->first(); !bndry->isDone(); bndry->next()) { for (int z = 0; z < mesh->LocalNz; z++) { @@ -2388,7 +2388,7 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { void BoundaryNeumann_4thOrder::apply(Field3D & f) { Mesh* mesh = bndry->localmesh; - ASSERT1(mesh == f.getMesh()) + ASSERT1(mesh == f.getMesh()); Coordinates* metric = f.getCoordinates(); // Set (at 4th order) the gradient at the mid-point between the guard cell and the // grid cell to be val This sets the value of the co-ordinate derivative, i.e. DDX/DDY @@ -2424,7 +2424,7 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { void BoundaryNeumann_4thOrder::apply_ddt(Field3D & f) { Mesh* mesh = bndry->localmesh; - ASSERT1(mesh == f.getMesh()) + ASSERT1(mesh == f.getMesh()); Field3D* dt = f.timeDeriv(); for (bndry->first(); !bndry->isDone(); bndry->next()) { for (int z = 0; z < mesh->LocalNz; z++) { @@ -2462,7 +2462,7 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { void BoundaryNeumannPar::apply(Field3D & f) { Mesh* mesh = bndry->localmesh; - ASSERT1(mesh == f.getMesh()) + ASSERT1(mesh == f.getMesh()); Coordinates* metric = f.getCoordinates(); for (bndry->first(); !bndry->isDone(); bndry->next()) { for (int z = 0; z < mesh->LocalNz; z++) { @@ -2529,7 +2529,7 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { void BoundaryRobin::apply(Field3D & f) { Mesh* mesh = bndry->localmesh; - ASSERT1(mesh == f.getMesh()) + ASSERT1(mesh == f.getMesh()); if (fabs(bval) < 1.e-12) { for (bndry->first(); !bndry->isDone(); bndry->next()) { for (int z = 0; z < mesh->LocalNz; z++) { @@ -2564,7 +2564,7 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { void BoundaryConstGradient::apply(Field3D & f) { Mesh* mesh = bndry->localmesh; - ASSERT1(mesh == f.getMesh()) + ASSERT1(mesh == f.getMesh()); for (bndry->first(); !bndry->isDone(); bndry->next()) { for (int z = 0; z < mesh->LocalNz; z++) { f(bndry->x, bndry->y, z) = @@ -2628,7 +2628,7 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { void BoundaryZeroLaplace::apply(Field3D & f) { #if not(BOUT_USE_METRIC_3D) Mesh* mesh = bndry->localmesh; - ASSERT1(mesh == f.getMesh()) + ASSERT1(mesh == f.getMesh()); int ncz = mesh->LocalNz; Coordinates* metric = f.getCoordinates(); @@ -2731,10 +2731,10 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { void BoundaryZeroLaplace2::apply(Field3D & f) { #if not(BOUT_USE_METRIC_3D) Mesh* mesh = bndry->localmesh; - ASSERT1(mesh == f.getMesh()) + ASSERT1(mesh == f.getMesh()); const int ncz = mesh->LocalNz; - ASSERT0(ncz % 2 == 0) // Allocation assumes even number + ASSERT0(ncz % 2 == 0); // Allocation assumes even number // allocate memory Array c0(ncz / 2 + 1), c1(ncz / 2 + 1), c2(ncz / 2 + 1); @@ -2840,7 +2840,7 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { } Mesh* mesh = bndry->localmesh; - ASSERT1(mesh == f.getMesh()) + ASSERT1(mesh == f.getMesh()); Coordinates* metric = f.getCoordinates(); int ncz = mesh->LocalNz; @@ -2920,7 +2920,7 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { void BoundaryDivCurl::apply(Vector3D & var) { #if not(BOUT_USE_METRIC_3D) Mesh* mesh = bndry->localmesh; - ASSERT1(mesh == var.getMesh()) + ASSERT1(mesh == var.getMesh()); int jx, jy, jz, jzp, jzm; BoutReal tmp; @@ -3070,7 +3070,7 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { // used Mesh* mesh = bndry->localmesh; - ASSERT1(mesh == f.getMesh()) + ASSERT1(mesh == f.getMesh()); bndry->first(); // Check for staggered grids @@ -3174,7 +3174,7 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { // order. Mesh* mesh = bndry->localmesh; - ASSERT1(mesh == f.getMesh()) + ASSERT1(mesh == f.getMesh()); bndry->first(); // Check for staggered grids @@ -3300,7 +3300,7 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { void BoundaryFree_O2::apply_ddt(Field3D & f) { Mesh* mesh = bndry->localmesh; - ASSERT1(mesh == f.getMesh()) + ASSERT1(mesh == f.getMesh()); Field3D* dt = f.timeDeriv(); for (bndry->first(); !bndry->isDone(); bndry->next()) { for (int z = 0; z < mesh->LocalNz; z++) { @@ -3325,7 +3325,7 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { void BoundaryFree_O3::apply(Field2D & f) { Mesh* mesh = bndry->localmesh; - ASSERT1(mesh == f.getMesh()) + ASSERT1(mesh == f.getMesh()); bndry->first(); // Check for staggered grids @@ -3436,7 +3436,7 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { // order. Mesh* mesh = bndry->localmesh; - ASSERT1(mesh == f.getMesh()) + ASSERT1(mesh == f.getMesh()); bndry->first(); // Check for staggered grids @@ -3569,7 +3569,7 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { void BoundaryFree_O3::apply_ddt(Field3D & f) { Mesh* mesh = bndry->localmesh; - ASSERT1(mesh == f.getMesh()) + ASSERT1(mesh == f.getMesh()); Field3D* dt = f.timeDeriv(); for (bndry->first(); !bndry->isDone(); bndry->next()) { for (int z = 0; z < mesh->LocalNz; z++) { @@ -3624,7 +3624,7 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { TRACE("BoundaryRelax::apply_ddt(Field3D)"); Mesh* mesh = bndry->localmesh; - ASSERT1(mesh == f.getMesh()) + ASSERT1(mesh == f.getMesh()); // Make a copy of f Field3D g = f; // NOTE: This is not very efficient... copying entire field @@ -3701,7 +3701,7 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { void BoundaryToFieldAligned::apply(Field2D & f, BoutReal t) { op->apply(f, t); } void BoundaryToFieldAligned::apply(Field3D & f, BoutReal t) { - ASSERT1(bndry->localmesh == f.getMesh()) + ASSERT1(bndry->localmesh == f.getMesh()); // NOTE: This is not very efficient... updating entire field f = fromFieldAligned(f); @@ -3720,7 +3720,7 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { void BoundaryToFieldAligned::apply_ddt(Field2D & f) { op->apply_ddt(f); } void BoundaryToFieldAligned::apply_ddt(Field3D & f) { - ASSERT1(bndry->localmesh == f.getMesh()) + ASSERT1(bndry->localmesh == f.getMesh()); f = fromFieldAligned(f); ddt(f) = fromFieldAligned(ddt(f)); @@ -3744,7 +3744,7 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { void BoundaryFromFieldAligned::apply(Field2D & f, BoutReal t) { op->apply(f, t); } void BoundaryFromFieldAligned::apply(Field3D & f, BoutReal t) { - ASSERT1(bndry->localmesh == f.getMesh()) + ASSERT1(bndry->localmesh == f.getMesh()); // NOTE: This is not very efficient... shifting entire field f = toFieldAligned(f); @@ -3763,7 +3763,7 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { void BoundaryFromFieldAligned::apply_ddt(Field2D & f) { op->apply_ddt(f); } void BoundaryFromFieldAligned::apply_ddt(Field3D & f) { - ASSERT1(bndry->localmesh == f.getMesh()) + ASSERT1(bndry->localmesh == f.getMesh()); f = toFieldAligned(f); ddt(f) = toFieldAligned(ddt(f)); diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 34d087f4cb..44dfa2ae32 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -908,7 +908,7 @@ void Coordinates::setParallelTransform(Options* mesh_options) { Field2D Coordinates::DDX(const Field2D& f, CELL_LOC loc, const std::string& method, const std::string& region) const { - ASSERT1(location == loc || loc == CELL_DEFAULT) + ASSERT1(location == loc || loc == CELL_DEFAULT); return bout::derivatives::index::DDX(f, loc, method, region) / dx(); } Field3D Coordinates::DDX(const Field3D& f, CELL_LOC outloc, const std::string& method, @@ -1016,7 +1016,7 @@ Field2D Coordinates::Div_par(const Field2D& f, CELL_LOC outloc, Field3D Coordinates::Div_par(const Field3D& f, CELL_LOC outloc, const std::string& method) { TRACE("Coordinates::Div_par( Field3D )"); - ASSERT1(location == outloc || outloc == CELL_DEFAULT) + ASSERT1(location == outloc || outloc == CELL_DEFAULT); // Need Bxy at location of f, which might be different from location of this // Coordinates object @@ -1067,7 +1067,7 @@ Field3D Coordinates::Grad2_par2(const Field3D& f, CELL_LOC outloc, result = Grad2_par2_DDY_invSg(outloc, method) * result + r2; - ASSERT2(result.getLocation() == outloc) + ASSERT2(result.getLocation() == outloc); return result; } @@ -1079,7 +1079,7 @@ Field3D Coordinates::Grad2_par2(const Field3D& f, CELL_LOC outloc, Field2D Coordinates::Delp2(const Field2D& f, CELL_LOC outloc, bool UNUSED(useFFT)) { TRACE("Coordinates::Delp2( Field2D )"); - ASSERT1(location == outloc || outloc == CELL_DEFAULT) + ASSERT1(location == outloc || outloc == CELL_DEFAULT); return G1() * DDX(f, outloc) + g11() * D2DX2(f, outloc); } @@ -1091,14 +1091,14 @@ Field3D Coordinates::Delp2(const Field3D& f, CELL_LOC outloc, bool useFFT) { outloc = f.getLocation(); } - ASSERT1(location == outloc) - ASSERT1(f.getLocation() == outloc) + ASSERT1(location == outloc); + ASSERT1(f.getLocation() == outloc); if (localmesh->GlobalNx == 1 && localmesh->GlobalNz == 1) { // copy mesh, location, etc return f * 0; } - ASSERT2(localmesh->xstart > 0) // Need at least one guard cell + ASSERT2(localmesh->xstart > 0); // Need at least one guard cell; Field3D result{emptyFrom(f).setLocation(outloc)}; @@ -1146,7 +1146,7 @@ Field3D Coordinates::Delp2(const Field3D& f, CELL_LOC outloc, bool useFFT) { + 2 * g13() * ::D2DXDZ(f, outloc); } - ASSERT2(result.getLocation() == outloc) + ASSERT2(result.getLocation() == outloc); return result; } @@ -1158,14 +1158,14 @@ FieldPerp Coordinates::Delp2(const FieldPerp& f, CELL_LOC outloc, bool useFFT) { outloc = f.getLocation(); } - ASSERT1(location == outloc) - ASSERT1(f.getLocation() == outloc) + ASSERT1(location == outloc); + ASSERT1(f.getLocation() == outloc); if (localmesh->GlobalNx == 1 && localmesh->GlobalNz == 1) { // copy mesh, location, etc return f * 0; } - ASSERT2(localmesh->xstart > 0) // Need at least one guard cell + ASSERT2(localmesh->xstart > 0); // Need at least one guard cell FieldPerp result{emptyFrom(f).setLocation(outloc)}; @@ -1215,13 +1215,13 @@ FieldPerp Coordinates::Delp2(const FieldPerp& f, CELL_LOC outloc, bool useFFT) { } Field2D Coordinates::Laplace_par(const Field2D& f, CELL_LOC outloc) { - ASSERT1(location == outloc || outloc == CELL_DEFAULT) + ASSERT1(location == outloc || outloc == CELL_DEFAULT); return D2DY2(f, outloc) / g_22() + DDY(J() / g_22(), outloc) * DDY(f, outloc) / J(); } Field3D Coordinates::Laplace_par(const Field3D& f, CELL_LOC outloc) { - ASSERT1(location == outloc || outloc == CELL_DEFAULT) + ASSERT1(location == outloc || outloc == CELL_DEFAULT); return D2DY2(f, outloc) / g_22() + ::DDY(J() / g_22(), outloc) * ::DDY(f, outloc) / J(); } @@ -1231,7 +1231,7 @@ Field2D Coordinates::Laplace(const Field2D& f, CELL_LOC outloc, const std::string& dfdy_boundary_conditions, const std::string& dfdy_dy_region) { TRACE("Coordinates::Laplace( Field2D )"); - ASSERT1(location == outloc || outloc == CELL_DEFAULT) + ASSERT1(location == outloc || outloc == CELL_DEFAULT); return G1() * DDX(f, outloc) + G2() * DDY(f, outloc) + g11() * D2DX2(f, outloc) + g22() * D2DY2(f, outloc) @@ -1244,7 +1244,7 @@ Field3D Coordinates::Laplace(const Field3D& f, CELL_LOC outloc, const std::string& dfdy_boundary_conditions, const std::string& dfdy_dy_region) { TRACE("Coordinates::Laplace( Field3D )"); - ASSERT1(location == outloc || outloc == CELL_DEFAULT) + ASSERT1(location == outloc || outloc == CELL_DEFAULT); return G1() * ::DDX(f, outloc) + G2() * ::DDY(f, outloc) + G3() * ::DDZ(f, outloc) + g11() * ::D2DX2(f, outloc) + g22() * ::D2DY2(f, outloc) diff --git a/src/mesh/difops.cxx b/src/mesh/difops.cxx index 51c1e59194..7b57918c93 100644 --- a/src/mesh/difops.cxx +++ b/src/mesh/difops.cxx @@ -77,8 +77,8 @@ Field3D Grad_par(const Field3D& var, const std::string& method, CELL_LOC outloc) *******************************************************************************/ Field3D Grad_parP(const Field3D& apar, const Field3D& f) { - ASSERT1_FIELDS_COMPATIBLE(apar, f) - ASSERT1(f.hasParallelSlices()) + ASSERT1_FIELDS_COMPATIBLE(apar, f); + ASSERT1(f.hasParallelSlices()); Mesh* mesh = apar.getMesh(); @@ -182,7 +182,7 @@ Field3D Grad_parP(const Field3D& apar, const Field3D& f) { } } - ASSERT2(result.getLocation() == f.getLocation()) + ASSERT2(result.getLocation() == f.getLocation()); return result; } @@ -235,9 +235,9 @@ Field3D Div_par(const Field3D& f, const std::string& method, CELL_LOC outloc) { } Field3D Div_par(const Field3D& f, const Field3D& v) { - ASSERT1_FIELDS_COMPATIBLE(f, v) - ASSERT1(f.hasParallelSlices()) - ASSERT1(v.hasParallelSlices()) + ASSERT1_FIELDS_COMPATIBLE(f, v); + ASSERT1(f.hasParallelSlices()); + ASSERT1(v.hasParallelSlices()); // Parallel divergence, using velocities at cell boundaries // Note: Not guaranteed to be flux conservative @@ -464,7 +464,7 @@ Coordinates::FieldMetric b0xGrad_dot_Grad(const Field2D& phi, const Field2D& A, outloc = A.getLocation(); } - ASSERT1(phi.getMesh() == A.getMesh()) + ASSERT1(phi.getMesh() == A.getMesh()); Coordinates* metric = phi.getCoordinates(outloc); @@ -480,7 +480,7 @@ Coordinates::FieldMetric b0xGrad_dot_Grad(const Field2D& phi, const Field2D& A, Coordinates::FieldMetric result = VDDX(vx, A, outloc) + VDDY(vy, A, outloc); result /= metric->J() * sqrt(metric->g_22()); - ASSERT1(result.getLocation() == outloc) + ASSERT1(result.getLocation() == outloc); #if BOUT_USE_TRACK result.name = "b0xGrad_dot_Grad(" + phi.name + "," + A.name + ")"; @@ -495,7 +495,7 @@ Field3D b0xGrad_dot_Grad(const Field2D& phi, const Field3D& A, CELL_LOC outloc) outloc = A.getLocation(); } - ASSERT1(phi.getMesh() == A.getMesh()) + ASSERT1(phi.getMesh() == A.getMesh()); Mesh* mesh = phi.getMesh(); @@ -525,7 +525,7 @@ Field3D b0xGrad_dot_Grad(const Field2D& phi, const Field3D& A, CELL_LOC outloc) result.name = "b0xGrad_dot_Grad(" + phi.name + "," + A.name + ")"; #endif - ASSERT2(result.getLocation() == outloc) + ASSERT2(result.getLocation() == outloc); return result; } @@ -537,7 +537,7 @@ Field3D b0xGrad_dot_Grad(const Field3D& p, const Field2D& A, CELL_LOC outloc) { outloc = A.getLocation(); } - ASSERT1(p.getMesh() == A.getMesh()) + ASSERT1(p.getMesh() == A.getMesh()); Coordinates* metric = p.getCoordinates(outloc); @@ -560,7 +560,7 @@ Field3D b0xGrad_dot_Grad(const Field3D& p, const Field2D& A, CELL_LOC outloc) { result.name = "b0xGrad_dot_Grad(" + p.name + "," + A.name + ")"; #endif - ASSERT2(result.getLocation() == outloc) + ASSERT2(result.getLocation() == outloc); return result; } @@ -572,7 +572,7 @@ Field3D b0xGrad_dot_Grad(const Field3D& phi, const Field3D& A, CELL_LOC outloc) outloc = A.getLocation(); } - ASSERT1(phi.getMesh() == A.getMesh()) + ASSERT1(phi.getMesh() == A.getMesh()); Mesh* mesh = phi.getMesh(); @@ -601,7 +601,7 @@ Field3D b0xGrad_dot_Grad(const Field3D& phi, const Field3D& A, CELL_LOC outloc) result.name = "b0xGrad_dot_Grad(" + phi.name + "," + A.name + ")"; #endif - ASSERT2(result.getLocation() == outloc) + ASSERT2(result.getLocation() == outloc); return result; } @@ -616,11 +616,11 @@ Coordinates::FieldMetric bracket(const Field2D& f, const Field2D& g, Solver* UNUSED(solver)) { TRACE("bracket(Field2D, Field2D)"); - ASSERT1_FIELDS_COMPATIBLE(f, g) + ASSERT1_FIELDS_COMPATIBLE(f, g); if (outloc == CELL_DEFAULT) { outloc = g.getLocation(); } - ASSERT1(outloc == g.getLocation()) + ASSERT1(outloc == g.getLocation()); Coordinates::FieldMetric result{emptyFrom(f)}; @@ -639,11 +639,11 @@ Field3D bracket(const Field3D& f, const Field2D& g, BRACKET_METHOD method, CELL_LOC outloc, Solver* solver) { TRACE("bracket(Field3D, Field2D)"); - ASSERT1_FIELDS_COMPATIBLE(f, g) + ASSERT1_FIELDS_COMPATIBLE(f, g); if (outloc == CELL_DEFAULT) { outloc = g.getLocation(); } - ASSERT1(outloc == g.getLocation()) + ASSERT1(outloc == g.getLocation()); [[maybe_unused]] Mesh* mesh = f.getMesh(); @@ -829,11 +829,11 @@ Field3D bracket(const Field2D& f, const Field3D& g, BRACKET_METHOD method, CELL_LOC outloc, Solver* solver) { TRACE("bracket(Field2D, Field3D)"); - ASSERT1_FIELDS_COMPATIBLE(f, g) + ASSERT1_FIELDS_COMPATIBLE(f, g); if (outloc == CELL_DEFAULT) { outloc = g.getLocation(); } - ASSERT1(outloc == g.getLocation()) + ASSERT1(outloc == g.getLocation()); Mesh* mesh = f.getMesh(); @@ -866,11 +866,11 @@ Field3D bracket(const Field3D& f, const Field3D& g, BRACKET_METHOD method, CELL_LOC outloc, [[maybe_unused]] Solver* solver) { TRACE("Field3D, Field3D"); - ASSERT1_FIELDS_COMPATIBLE(f, g) + ASSERT1_FIELDS_COMPATIBLE(f, g); if (outloc == CELL_DEFAULT) { outloc = g.getLocation(); } - ASSERT1(outloc == g.getLocation()) + ASSERT1(outloc == g.getLocation()); Mesh* mesh = f.getMesh(); diff --git a/src/mesh/fv_ops.cxx b/src/mesh/fv_ops.cxx index ce5d7ff448..65ccf801ca 100644 --- a/src/mesh/fv_ops.cxx +++ b/src/mesh/fv_ops.cxx @@ -184,7 +184,7 @@ const Field3D Div_par_K_Grad_par(const Field3D& Kin, const Field3D& fin, bool bndry_flux) { TRACE("FV::Div_par_K_Grad_par"); - ASSERT2(Kin.getLocation() == fin.getLocation()) + ASSERT2(Kin.getLocation() == fin.getLocation()); Mesh* mesh = Kin.getMesh(); @@ -250,13 +250,13 @@ const Field3D Div_par_K_Grad_par(const Field3D& Kin, const Field3D& fin, } const Field3D D4DY4(const Field3D& d_in, const Field3D& f_in) { - ASSERT1_FIELDS_COMPATIBLE(d_in, f_in) + ASSERT1_FIELDS_COMPATIBLE(d_in, f_in); Mesh* mesh = d_in.getMesh(); Coordinates* coord = f_in.getCoordinates(); - ASSERT2(d_in.getDirectionY() == f_in.getDirectionY()) + ASSERT2(d_in.getDirectionY() == f_in.getDirectionY()); const bool are_unaligned = ((d_in.getDirectionY() == YDirectionType::Standard) and (f_in.getDirectionY() == YDirectionType::Standard)); diff --git a/src/mesh/index_derivs.cxx b/src/mesh/index_derivs.cxx index ebecb96700..49d99bd9c9 100644 --- a/src/mesh/index_derivs.cxx +++ b/src/mesh/index_derivs.cxx @@ -424,7 +424,7 @@ class FFTDerivativeType { template void standard(const T& var, T& result, const std::string& region) const { AUTO_TRACE(); - ASSERT2(meta.derivType == DERIV::Standard) + ASSERT2(meta.derivType == DERIV::Standard); ASSERT2(var.getMesh()->getNguard(direction) >= nGuards); ASSERT2(direction == DIRECTION::Z); // Only in Z for now ASSERT2(stagger == STAGGER::None); // Staggering not currently supported diff --git a/src/mesh/mesh.cxx b/src/mesh/mesh.cxx index e635107fb7..55df25e9d9 100644 --- a/src/mesh/mesh.cxx +++ b/src/mesh/mesh.cxx @@ -63,8 +63,8 @@ const Field2D Mesh::interpolateAndExtrapolate(const Field2D& f, CELL_LOC locatio // this point should already be set correctly because the metric // components have been interpolated to here. if (extrap_start > 0 and f.getLocation() != location) { - ASSERT1(bndry->bx == 0 or xstart > 1) - ASSERT1(bndry->by == 0 or ystart > 1) + ASSERT1(bndry->bx == 0 or xstart > 1); + ASSERT1(bndry->by == 0 or ystart > 1); // note that either bx or by is >0 here result(bndry->x, bndry->y) = (9. diff --git a/tests/MMS/hw/hw.cxx b/tests/MMS/hw/hw.cxx index b14dc7fb47..c51e17ab14 100644 --- a/tests/MMS/hw/hw.cxx +++ b/tests/MMS/hw/hw.cxx @@ -43,8 +43,8 @@ class Hw : public PhysicsModel { mesh->getCoordinates()->setDz(TWOPI * Lx / (mesh->LocalNz)); ///// - SOLVE_FOR2(n, vort) - SAVE_REPEAT(phi) + SOLVE_FOR2(n, vort); + SAVE_REPEAT(phi); phiSolver = Laplacian::create(); phi = 0.; // Starting phi diff --git a/tests/MMS/spatial/diffusion/diffusion.cxx b/tests/MMS/spatial/diffusion/diffusion.cxx index c72dd87540..ba4e33ca24 100644 --- a/tests/MMS/spatial/diffusion/diffusion.cxx +++ b/tests/MMS/spatial/diffusion/diffusion.cxx @@ -46,7 +46,7 @@ class Diffusion : public PhysicsModel { coords->setCovariantMetricTensor(covariant_metric_tensor); // Tell BOUT++ to solve N - SOLVE_FOR(N) + SOLVE_FOR(N); return 0; } From 8a003ef02207eb20360abf31c5d2cd32179c07d0 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Tue, 5 Mar 2024 12:00:17 +0000 Subject: [PATCH 339/491] Remove redundant `const`. --- include/bout/physicsmodel.hxx | 2 +- src/physics/physicsmodel.cxx | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/include/bout/physicsmodel.hxx b/include/bout/physicsmodel.hxx index f34163c4af..2b2badf0f3 100644 --- a/include/bout/physicsmodel.hxx +++ b/include/bout/physicsmodel.hxx @@ -88,7 +88,7 @@ public: void addRepeat(ValueType value, const std::string& name) { add(value, name, true); } void addOnce(ValueType value, const std::string& name) { add(value, name, false); } - void add(const ValueType value, const std::string& name, bool save_repeat = false); + void add(ValueType value, const std::string& name, bool save_repeat = false); void add(Vector2D* value, const std::string& name, bool save_repeat = false); void add(Vector3D* value, const std::string& name, bool save_repeat = false); diff --git a/src/physics/physicsmodel.cxx b/src/physics/physicsmodel.cxx index aa4a99f07a..9f538895ed 100644 --- a/src/physics/physicsmodel.cxx +++ b/src/physics/physicsmodel.cxx @@ -43,7 +43,7 @@ using namespace std::literals; namespace bout { -void DataFileFacade::add(const ValueType value, const std::string& name, bool save_repeat) { +void DataFileFacade::add(ValueType value, const std::string& name, bool save_repeat) { data.emplace_back(name, value, save_repeat); } void DataFileFacade::add(Vector2D* value, const std::string& name, bool save_repeat) { From 315d739e09760d20c253ad6117e2d40798f8fbf8 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Tue, 5 Mar 2024 12:04:56 +0000 Subject: [PATCH 340/491] Fix MetricTensor::inverse method (it was replacing each metric component with a single scalar value). --- src/mesh/metricTensor.cxx | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/src/mesh/metricTensor.cxx b/src/mesh/metricTensor.cxx index 5786837d63..a3ec81f22c 100644 --- a/src/mesh/metricTensor.cxx +++ b/src/mesh/metricTensor.cxx @@ -75,6 +75,13 @@ MetricTensor MetricTensor::inverse(const std::string& region) { auto a = Matrix(3, 3); + FieldMetric g_11 = emptyFrom(g11_); + FieldMetric g_22 = emptyFrom(g22_); + FieldMetric g_33 = emptyFrom(g33_); + FieldMetric g_12 = emptyFrom(g12_); + FieldMetric g_13 = emptyFrom(g13_); + FieldMetric g_23 = emptyFrom(g23_); + BOUT_FOR_SERIAL(i, g11_.getRegion(region)) { a(0, 0) = g11_[i]; a(1, 1) = g22_[i]; @@ -89,15 +96,14 @@ MetricTensor MetricTensor::inverse(const std::string& region) { output_error.write(error_message, i.x(), i.y()); throw BoutException(error_message); } - } - BoutReal g_11, g_22, g_33, g_12, g_13, g_23; - g_11 = a(0, 0); - g_22 = a(1, 1); - g_33 = a(2, 2); - g_12 = a(0, 1); - g_13 = a(0, 2); - g_23 = a(1, 2); + g_11[i] = a(0, 0); + g_22[i] = a(1, 1); + g_33[i] = a(2, 2); + g_12[i] = a(0, 1); + g_13[i] = a(0, 2); + g_23[i] = a(1, 2); + } // BoutReal maxerr; // maxerr = BOUTMAX( @@ -122,8 +128,8 @@ MetricTensor MetricTensor::inverse(const std::string& region) { // + g_23 * g_33))); // // output_info.write("\tMaximum error in off-diagonal inversion is {:e}\n", maxerr); - const auto mesh = g11_.getMesh(); // All the components have the same mesh - auto other_representation = MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23, mesh); + + auto other_representation = MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23); const auto location = g11_.getLocation(); other_representation.setLocation(location); return other_representation; From eb58344b262c8c68947666e12339468d1435c2c4 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Tue, 5 Mar 2024 12:08:42 +0000 Subject: [PATCH 341/491] Restore commented-out code (Maximum error in diagonal/off-diagonal inversion). --- src/mesh/metricTensor.cxx | 35 ++++++++++++----------------------- 1 file changed, 12 insertions(+), 23 deletions(-) diff --git a/src/mesh/metricTensor.cxx b/src/mesh/metricTensor.cxx index a3ec81f22c..4aa6a8c7ba 100644 --- a/src/mesh/metricTensor.cxx +++ b/src/mesh/metricTensor.cxx @@ -104,30 +104,19 @@ MetricTensor MetricTensor::inverse(const std::string& region) { g_13[i] = a(0, 2); g_23[i] = a(1, 2); } + + BoutReal maxerr; + maxerr = BOUTMAX(max(abs((g_11 * g_11 + g_12 * g_12 + g_13 * g_13) - 1)), + max(abs((g_12 * g_12 + g_22 * g_22 + g_23 * g_23) - 1)), + max(abs((g_13 * g_13 + g_23 * g_23 + g_33 * g_33) - 1))); + + output_info.write("\tMaximum error in diagonal inversion is {:e}\n", maxerr); + + maxerr = BOUTMAX(max(abs(g_11 * g_12 + g_12 * g_22 + g_13 * g_23)), + max(abs(g_11 * g_13 + g_12 * g_23 + g_13 * g_33)), + max(abs(g_12 * g_13 + g_22 * g_23 + g_23 * g_33))); - // BoutReal maxerr; - // maxerr = BOUTMAX( - // max(abs((g_11 * g_11 + g_12 * g_12 - // + g_13 * g_13) - // - 1)), - // max(abs((g_12 * g_12 + g_22 * g_22 - // + g_23 * g_23) - // - 1)), - // max(abs((g_13 * g_13 + g_23 * g_23 - // + g_33 * g_33) - // - 1))); - // - // output_info.write("\tMaximum error in diagonal inversion is {:e}\n", maxerr); - // - // maxerr = BOUTMAX( - // max(abs(g_11 * g_12 + g_12 * g_22 - // + g_13 * g_23)), - // max(abs(g_11 * g_13 + g_12 * g_23 - // + g_13 * g_33)), - // max(abs(g_12 * g_13 + g_22 * g_23 - // + g_23 * g_33))); - // - // output_info.write("\tMaximum error in off-diagonal inversion is {:e}\n", maxerr); + output_info.write("\tMaximum error in off-diagonal inversion is {:e}\n", maxerr); auto other_representation = MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23); const auto location = g11_.getLocation(); From c1246bd5b0ecefa302eab79bc7b23932d8dce481 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Tue, 5 Mar 2024 12:19:11 +0000 Subject: [PATCH 342/491] Use 'auto' when type name is obvious. --- src/mesh/mesh.cxx | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/mesh/mesh.cxx b/src/mesh/mesh.cxx index 55df25e9d9..47a595ac32 100644 --- a/src/mesh/mesh.cxx +++ b/src/mesh/mesh.cxx @@ -702,14 +702,13 @@ Mesh::createDefaultCoordinates(const CELL_LOC location, bool recalculate_stagger if (location == CELL_CENTRE || location == CELL_DEFAULT) { // Initialize coordinates from input - const std::shared_ptr& new_coordinates = - std::make_shared(this, options); + const auto new_coordinates = std::make_shared(this, options); new_coordinates->recalculateAndReset(recalculate_staggered, force_interpolate_from_centre); return new_coordinates; } // Interpolate coordinates from CELL_CENTRE version - const std::shared_ptr& new_coordinates = + const auto new_coordinates = std::make_shared(this, options, location, getCoordinates(CELL_CENTRE), force_interpolate_from_centre); new_coordinates->recalculateAndReset(recalculate_staggered, From b371136691c502e2ef96019792f91ac37d2cf9cb Mon Sep 17 00:00:00 2001 From: tomc271 Date: Mon, 11 Mar 2024 15:25:39 +0000 Subject: [PATCH 343/491] Clang-tidy: "Use auto when initialising with a template cast to avoid duplicating the type name" --- tests/unit/mesh/data/test_gridfromoptions.cxx | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/unit/mesh/data/test_gridfromoptions.cxx b/tests/unit/mesh/data/test_gridfromoptions.cxx index c7766f6505..ef6441768d 100644 --- a/tests/unit/mesh/data/test_gridfromoptions.cxx +++ b/tests/unit/mesh/data/test_gridfromoptions.cxx @@ -393,7 +393,7 @@ TEST_F(GridFromOptionsTest, CoordinatesXlowInterp) { auto coords = mesh_from_options.getCoordinates(CELL_XLOW); - Coordinates::FieldMetric expected_xlow = makeField( + auto expected_xlow = makeField( [](Coordinates::FieldMetric::ind_type& index) { return index.x() - 0.5 + (TWOPI * index.y()) + (TWOPI * index.z() / nz) + 3; }, @@ -434,7 +434,7 @@ TEST_F(GridFromOptionsTest, CoordinatesXlowRead) { auto coords = mesh_from_options.getCoordinates(CELL_XLOW); - Field2D expected_xlow = makeField( + auto expected_xlow = makeField( [](Field2D::ind_type& index) { return (nx - index.x() + 0.5) + (TWOPI * index.y()) + (TWOPI * index.z() / nz) + 3; @@ -467,7 +467,7 @@ TEST_F(GridFromOptionsTest, CoordinatesYlowInterp) { auto* coords = mesh_from_options.getCoordinates(CELL_YLOW); - Field2D expected_ylow = makeField( + auto expected_ylow = makeField( [](Field2D::ind_type& index) { return index.x() + (TWOPI * (index.y() - 0.5)) + (TWOPI * index.z() / nz) + 3; }, @@ -517,7 +517,7 @@ TEST_F(GridFromOptionsTest, CoordinatesYlowRead) { auto coords = mesh_from_options.getCoordinates(CELL_YLOW); - Field2D expected_ylow = makeField( + auto expected_ylow = makeField( [](Field2D::ind_type& index) { return index.x() + (TWOPI * (ny - index.y() + 0.5)) + (TWOPI * index.z() / nz) + 3; From de146f18e1c621c646c643a2dc711e4455dd93e1 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Tue, 12 Mar 2024 12:26:46 +0000 Subject: [PATCH 344/491] Bug fix: Don't specify region when setting contravariant metric tensor in 'interpolateFieldsFromOtherCoordinates' method. --- src/mesh/coordinates.cxx | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 44dfa2ae32..bf2f19f15a 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -340,8 +340,7 @@ void Coordinates::interpolateFieldsFromOtherCoordinates(Options* mesh_options, false, transform.get()); }; - const auto region = std::basic_string("RGN_NOBNDRY"); - setContravariantMetricTensor(coords_in->getContravariantMetricTensor(), region); + setContravariantMetricTensor(coords_in->getContravariantMetricTensor()); applyToContravariantMetricTensor(interpolateAndExtrapolate_function); applyToCovariantMetricTensor(interpolateAndExtrapolate_function); From d1516dbb0ee1da23a4972acfcb0262173c1a4c76 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 13 Mar 2024 10:56:32 +0000 Subject: [PATCH 345/491] Update petsc cases to use new getters. --- examples/6field-simple/elm_6f.cxx | 8 +-- .../IMEX/drift-wave-constraint/test-drift.cxx | 2 +- examples/conducting-wall-mode/cwm.cxx | 8 +-- examples/fci-wave-logn/fci-wave.cxx | 2 +- examples/fci-wave/fci-wave.cxx | 2 +- examples/gravity_reduced/gravity_reduced.cxx | 6 +- examples/laplace-petsc3d/test-laplace3d.cxx | 14 ++-- examples/orszag-tang/mhd.cxx | 2 +- examples/uedge-benchmark/ue_bmark.cxx | 4 +- include/bout/fv_ops.hxx | 12 ++-- .../laplace/impls/hypre3d/hypre3d_laplace.cxx | 48 ++++++------- .../laplace/impls/petsc/petsc_laplace.cxx | 68 +++++++++---------- .../laplace/impls/petsc3damg/petsc3damg.cxx | 44 ++++++------ src/invert/laplacexy/laplacexy.cxx | 24 +++---- src/invert/laplacexy2/laplacexy2.cxx | 32 ++++----- src/invert/laplacexy2/laplacexy2_hypre.cxx | 32 ++++----- .../laplacexz/impls/petsc/laplacexz-petsc.cxx | 64 ++++++++--------- src/mesh/difops.cxx | 6 +- .../invert/laplace/test_laplace_hypre3d.cxx | 10 +-- .../laplace/test_laplace_petsc3damg.cxx | 10 +-- 20 files changed, 199 insertions(+), 199 deletions(-) diff --git a/examples/6field-simple/elm_6f.cxx b/examples/6field-simple/elm_6f.cxx index c7d63122a3..a6babe531f 100644 --- a/examples/6field-simple/elm_6f.cxx +++ b/examples/6field-simple/elm_6f.cxx @@ -361,7 +361,7 @@ class Elm_6f : public PhysicsModel { result.allocate(); for (auto i : result) { result[i] = - (fp[i.yp()] - fm[i.ym()]) / (2. * coord->dy()[i] * sqrt(coord->g_22[i])); + (fp[i.yp()] - fm[i.ym()]) / (2. * coord->dy()[i] * sqrt(coord->g_22()[i])); } } else { result = Grad_par(f, loc); @@ -1448,11 +1448,11 @@ class Elm_6f : public PhysicsModel { if (hyperviscos > 0.0) { // Calculate coefficient. - hyper_mu_x = hyperviscos * coord->g_11 * SQ(coord->dx()) - * abs(coord->g11 * D2DX2(U)) / (abs(U) + 1e-3); + hyper_mu_x = hyperviscos * coord->g_11() * SQ(coord->dx()) + * abs(coord->g11() * D2DX2(U)) / (abs(U) + 1e-3); hyper_mu_x.applyBoundary("dirichlet"); // Set to zero on all boundaries - ddt(U) += hyper_mu_x * coord->g11 * D2DX2(U); + ddt(U) += hyper_mu_x * coord->g11() * D2DX2(U); if (first_run) { // Print out maximum values of viscosity used on this processor diff --git a/examples/IMEX/drift-wave-constraint/test-drift.cxx b/examples/IMEX/drift-wave-constraint/test-drift.cxx index e3bf88acfc..c5a16f3706 100644 --- a/examples/IMEX/drift-wave-constraint/test-drift.cxx +++ b/examples/IMEX/drift-wave-constraint/test-drift.cxx @@ -82,7 +82,7 @@ class DriftWave : public PhysicsModel { // ddt(phi) = Delp2(phi) - Vort; // This version uses central differencing for Delp2 - ddt(phi) = (coord->g11 * D2DX2(phi) + coord->g33 * D2DZ2(phi)) - Vort; + ddt(phi) = (coord->g11() * D2DX2(phi) + coord->g33() * D2DZ2(phi)) - Vort; return 0; } diff --git a/examples/conducting-wall-mode/cwm.cxx b/examples/conducting-wall-mode/cwm.cxx index f45af512c7..c0190fecbb 100644 --- a/examples/conducting-wall-mode/cwm.cxx +++ b/examples/conducting-wall-mode/cwm.cxx @@ -308,8 +308,8 @@ class CWM : public PhysicsModel { for (int jz = 0; jz < mesh->LocalNz; jz++) { var(xrup.ind, jy, jz) = var(xrup.ind, jy - 1, jz) - + coord->dy(xrup.ind, jy, jz) - * sqrt(coord->g_22(xrup.ind, jy, jz)) + + coord->dy()(xrup.ind, jy, jz) + * sqrt(coord->g_22()(xrup.ind, jy, jz)) * value(xrup.ind, jy, jz); } } @@ -325,8 +325,8 @@ class CWM : public PhysicsModel { for (int jz = 0; jz < mesh->LocalNz; jz++) { var(xrdn.ind, jy, jz) = var(xrdn.ind, jy + 1, jz) - - coord->dy(xrdn.ind, jy, jz) - * sqrt(coord->g_22(xrdn.ind, jy, jz)) + - coord->dy()(xrdn.ind, jy, jz) + * sqrt(coord->g_22()(xrdn.ind, jy, jz)) * value(xrdn.ind, jy, jz); } } diff --git a/examples/fci-wave-logn/fci-wave.cxx b/examples/fci-wave-logn/fci-wave.cxx index 6aa85c898d..144695c6cd 100644 --- a/examples/fci-wave-logn/fci-wave.cxx +++ b/examples/fci-wave-logn/fci-wave.cxx @@ -43,7 +43,7 @@ class FCIwave : public PhysicsModel { for (auto i : result.getRegion(RGN_NOBNDRY)) { result[i] = Bxyz[i] * (f_B.yup()[i.yp()] - f_B.ydown()[i.ym()]) - / (2. * coord->dy()[i] * sqrt(coord->g_22[i])); + / (2. * coord->dy()[i] * sqrt(coord->g_22()[i])); if (!finite(result[i])) { output.write("[{:d},{:d},{:d}]: {:e}, {:e} -> {:e}\n", i.x(), i.y(), i.z(), diff --git a/examples/fci-wave/fci-wave.cxx b/examples/fci-wave/fci-wave.cxx index 697e39f77c..11d5ee0468 100644 --- a/examples/fci-wave/fci-wave.cxx +++ b/examples/fci-wave/fci-wave.cxx @@ -44,7 +44,7 @@ class FCIwave : public PhysicsModel { for (auto i : result.getRegion(RGN_NOBNDRY)) { result[i] = Bxyz[i] * (f_B.yup()[i.yp()] - f_B.ydown()[i.ym()]) - / (2. * coord->dy()[i] * sqrt(coord->g_22[i])); + / (2. * coord->dy()[i] * sqrt(coord->g_22()[i])); if (!finite(result[i])) { output.write("[{:d},{:d},{:d}]: {:e}, {:e} -> {:e}\n", i.x(), i.y(), i.z(), diff --git a/examples/gravity_reduced/gravity_reduced.cxx b/examples/gravity_reduced/gravity_reduced.cxx index c58d61a48c..83021a4b56 100644 --- a/examples/gravity_reduced/gravity_reduced.cxx +++ b/examples/gravity_reduced/gravity_reduced.cxx @@ -115,8 +115,8 @@ class GravityReduced : public PhysicsModel { Lz = options["Lz"].withDefault(1.); // Set the metric tensor components to get Lz - coord->g33 = SQ(2. * PI / Lz); - coord->g_33 = 1. / coord->g33; + coord->g33() = SQ(2. * PI / Lz); + coord->g_33() = 1. / coord->g33(); /**************** SET EVOLVING VARIABLES *************/ @@ -129,7 +129,7 @@ class GravityReduced : public PhysicsModel { // Set initial perturbation // U = U0; // U = Delp2(phi0); - U = coord->g11 * D2DX2(phi0) + coord->g33 * D2DZ2(phi0); + U = coord->g11() * D2DX2(phi0) + coord->g33() * D2DZ2(phi0); Vpar = Vpar0; } diff --git a/examples/laplace-petsc3d/test-laplace3d.cxx b/examples/laplace-petsc3d/test-laplace3d.cxx index 9097109ed2..9f21a69be1 100644 --- a/examples/laplace-petsc3d/test-laplace3d.cxx +++ b/examples/laplace-petsc3d/test-laplace3d.cxx @@ -36,13 +36,13 @@ Field3D this_Laplace_perp(const Field3D& f) { // dfdy not divided by dy yet auto dfdy = bout::derivatives::index::DDY(f, CELL_DEFAULT, "DEFAULT", "RGN_NOY"); - return coords->G1 * DDX(f) - + (coords->G2 - DDY(coords->J / coords->g_22) / coords->J) * DDY(f) - + coords->G3 * DDZ(f) + coords->g11 * D2DX2(f) - + (coords->g22 - 1. / coords->g_22) * D2DY2(f) + coords->g33 * D2DZ2(f) + return coords->G1() * DDX(f) + + (coords->G2() - DDY(coords->J() / coords->g_22()) / coords->J()) * DDY(f) + + coords->G3() * DDZ(f) + coords->g11() * D2DX2(f) + + (coords->g22() - 1. / coords->g_22()) * D2DY2(f) + coords->g33() * D2DZ2(f) + 2. - * (coords->g12 * DDX(dfdy) / coords->dy() + coords->g13 * D2DXDZ(f) - + coords->g23 * D2DYDZ(f)); + * (coords->g12() * DDX(dfdy) / coords->dy() + coords->g13() * D2DXDZ(f) + + coords->g23() * D2DYDZ(f)); } int main(int argc, char** argv) { @@ -136,7 +136,7 @@ int main(int argc, char** argv) { /////////////////////////////////////////////////////////////////////////////////////// // Calculate error /////////////////////////////////////////////////////////////////////////////////////// - auto& g_22 = mesh->getCoordinates()->g_22; + auto& g_22 = mesh->getCoordinates()->g_22(); Field3D rhs_check = D * this_Laplace_perp(f) + (Grad(f) * Grad(C2) - DDY(C2) * DDY(f) / g_22) / C1 + A * f; // The usual way to do this would be diff --git a/examples/orszag-tang/mhd.cxx b/examples/orszag-tang/mhd.cxx index 0e12db0cf4..6274d9d24f 100644 --- a/examples/orszag-tang/mhd.cxx +++ b/examples/orszag-tang/mhd.cxx @@ -48,7 +48,7 @@ class MHD : public PhysicsModel { Coordinates* coord = mesh->getCoordinates(); output.write("dx(0,0,0) = {:e}, dy(0,0,0) = {:e}, dz(0,0,0) = {:e}\n", - coord->dx(0, 0, 0), coord->dy(0, 0, 0), coord->dz(0, 0, 0)); + coord->dx()(0, 0, 0), coord->dy()(0, 0, 0), coord->dz()(0, 0, 0)); SAVE_REPEAT(divB); diff --git a/examples/uedge-benchmark/ue_bmark.cxx b/examples/uedge-benchmark/ue_bmark.cxx index 61586de79c..895cc62ad2 100644 --- a/examples/uedge-benchmark/ue_bmark.cxx +++ b/examples/uedge-benchmark/ue_bmark.cxx @@ -193,8 +193,8 @@ class UedgeBenchmark : public PhysicsModel { // Operator for radial diffusive flux /* Field3D Div_X_K_Grad_X(const Field3D &difVi, const Field3D &Vi) { - Field2D sg = 1./sqrt(mesh->g_11); - return difVi * D2DX2(Vi)/mesh->g_11 + Field2D sg = 1./sqrt(mesh->g_11()); + return difVi * D2DX2(Vi)/mesh->g_11() + DDX( difVi * sg ) * DDX(Vi) * sg; } */ diff --git a/include/bout/fv_ops.hxx b/include/bout/fv_ops.hxx index a8456ee82b..b1b34b361b 100644 --- a/include/bout/fv_ops.hxx +++ b/include/bout/fv_ops.hxx @@ -263,22 +263,22 @@ const Field3D Div_par(const Field3D& f_in, const Field3D& v_in, #if BOUT_USE_METRIC_3D // For right cell boundaries BoutReal common_factor = - (coord->J(i, j, k) + coord->J(i, j + 1, k)) + (coord->J()(i, j, k) + coord->J()(i, j + 1, k)) / (sqrt(coord->g_22(i, j, k)) + sqrt(coord->g_22(i, j + 1, k))); BoutReal flux_factor_rc = - common_factor / (coord->dy()(i, j, k) * coord->J(i, j, k)); + common_factor / (coord->dy()(i, j, k) * coord->J()(i, j, k)); BoutReal flux_factor_rp = - common_factor / (coord->dy()(i, j + 1, k) * coord->J(i, j + 1, k)); + common_factor / (coord->dy()(i, j + 1, k) * coord->J()(i, j + 1, k)); // For left cell boundaries - common_factor = (coord->J(i, j, k) + coord->J(i, j - 1, k)) + common_factor = (coord->J()(i, j, k) + coord->J()(i, j - 1, k)) / (sqrt(coord->g_22(i, j, k)) + sqrt(coord->g_22(i, j - 1, k))); BoutReal flux_factor_lc = - common_factor / (coord->dy()(i, j, k) * coord->J(i, j, k)); + common_factor / (coord->dy()(i, j, k) * coord->J()(i, j, k)); BoutReal flux_factor_lm = - common_factor / (coord->dy()(i, j - 1, k) * coord->J(i, j - 1, k)); + common_factor / (coord->dy()(i, j - 1, k) * coord->J()(i, j - 1, k)); #endif //////////////////////////////////////////// diff --git a/src/invert/laplace/impls/hypre3d/hypre3d_laplace.cxx b/src/invert/laplace/impls/hypre3d/hypre3d_laplace.cxx index 2d7a19b8c0..b84f414035 100644 --- a/src/invert/laplace/impls/hypre3d/hypre3d_laplace.cxx +++ b/src/invert/laplace/impls/hypre3d/hypre3d_laplace.cxx @@ -101,8 +101,8 @@ LaplaceHypre3d::LaplaceHypre3d(Options* opt, const CELL_LOC loc, Mesh* mesh_in, BOUT_FOR_SERIAL(i, indexer->getRegionInnerX()) { if (inner_boundary_flags & INVERT_AC_GRAD) { // Neumann on inner X boundary - operator3D(i, i) = -1. / coords->dx()[i] / sqrt(coords->g_11[i]); - operator3D(i, i.xp()) = 1. / coords->dx()[i] / sqrt(coords->g_11[i]); + operator3D(i, i) = -1. / coords->dx()[i] / sqrt(coords->g_11()[i]); + operator3D(i, i.xp()) = 1. / coords->dx()[i] / sqrt(coords->g_11()[i]); } else { // Dirichlet on inner X boundary operator3D(i, i) = 0.5; @@ -113,8 +113,8 @@ LaplaceHypre3d::LaplaceHypre3d(Options* opt, const CELL_LOC loc, Mesh* mesh_in, BOUT_FOR_SERIAL(i, indexer->getRegionOuterX()) { if (outer_boundary_flags & INVERT_AC_GRAD) { // Neumann on outer X boundary - operator3D(i, i) = 1. / coords->dx()[i] / sqrt(coords->g_11[i]); - operator3D(i, i.xm()) = -1. / coords->dx()[i] / sqrt(coords->g_11[i]); + operator3D(i, i) = 1. / coords->dx()[i] / sqrt(coords->g_11()[i]); + operator3D(i, i.xm()) = -1. / coords->dx()[i] / sqrt(coords->g_11()[i]); } else { // Dirichlet on outer X boundary operator3D(i, i) = 0.5; @@ -125,8 +125,8 @@ LaplaceHypre3d::LaplaceHypre3d(Options* opt, const CELL_LOC loc, Mesh* mesh_in, BOUT_FOR_SERIAL(i, indexer->getRegionLowerY()) { if (lower_boundary_flags & INVERT_AC_GRAD) { // Neumann on lower Y boundary - operator3D(i, i) = -1. / coords->dy()[i] / sqrt(coords->g_22[i]); - operator3D(i, i.yp()) = 1. / coords->dy()[i] / sqrt(coords->g_22[i]); + operator3D(i, i) = -1. / coords->dy()[i] / sqrt(coords->g_22()[i]); + operator3D(i, i.yp()) = 1. / coords->dy()[i] / sqrt(coords->g_22()[i]); } else { // Dirichlet on lower Y boundary operator3D(i, i) = 0.5; @@ -137,8 +137,8 @@ LaplaceHypre3d::LaplaceHypre3d(Options* opt, const CELL_LOC loc, Mesh* mesh_in, BOUT_FOR_SERIAL(i, indexer->getRegionUpperY()) { if (upper_boundary_flags & INVERT_AC_GRAD) { // Neumann on upper Y boundary - operator3D(i, i) = 1. / coords->dy()[i] / sqrt(coords->g_22[i]); - operator3D(i, i.ym()) = -1. / coords->dy()[i] / sqrt(coords->g_22[i]); + operator3D(i, i) = 1. / coords->dy()[i] / sqrt(coords->g_22()[i]); + operator3D(i, i.ym()) = -1. / coords->dy()[i] / sqrt(coords->g_22()[i]); } else { // Dirichlet on upper Y boundary operator3D(i, i) = 0.5; @@ -277,7 +277,7 @@ void LaplaceHypre3d::updateMatrix3D() { const Field3D dc_dx = issetC ? DDX(C2) : Field3D(); const Field3D dc_dy = issetC ? DDY(C2) : Field3D(); const Field3D dc_dz = issetC ? DDZ(C2) : Field3D(); - const Field2D dJ_dy = DDY(coords->J / coords->g_22); + const Field2D dJ_dy = DDY(coords->J() / coords->g_22()); // Set up the matrix for the internal points on the grid. // Boundary conditions were set in the constructor. @@ -286,17 +286,17 @@ void LaplaceHypre3d::updateMatrix3D() { // avoid confusing it with the x-index. // Calculate coefficients for the terms in the differential operator - BoutReal C_df_dx = coords->G1[l], C_df_dz = coords->G3[l]; + BoutReal C_df_dx = coords->G1()[l], C_df_dz = coords->G3()[l]; if (issetD) { C_df_dx *= D[l]; C_df_dz *= D[l]; } if (issetC) { - C_df_dx += (coords->g11[l] * dc_dx[l] + coords->g12[l] * dc_dy[l] - + coords->g13[l] * dc_dz[l]) + C_df_dx += (coords->g11()[l] * dc_dx[l] + coords->g12()[l] * dc_dy[l] + + coords->g13()[l] * dc_dz[l]) / C1[l]; - C_df_dz += (coords->g13[l] * dc_dx[l] + coords->g23[l] * dc_dy[l] - + coords->g33[l] * dc_dz[l]) + C_df_dz += (coords->g13()[l] * dc_dx[l] + coords->g23()[l] * dc_dy[l] + + coords->g33()[l] * dc_dz[l]) / C1[l]; } if (issetE) { @@ -304,16 +304,16 @@ void LaplaceHypre3d::updateMatrix3D() { C_df_dz += Ez[l]; } - BoutReal C_d2f_dx2 = coords->g11[l], - C_d2f_dy2 = (coords->g22[l] - 1.0 / coords->g_22[l]), - C_d2f_dz2 = coords->g33[l]; + BoutReal C_d2f_dx2 = coords->g11()[l], + C_d2f_dy2 = (coords->g22()[l] - 1.0 / coords->g_22()[l]), + C_d2f_dz2 = coords->g33()[l]; if (issetD) { C_d2f_dx2 *= D[l]; C_d2f_dy2 *= D[l]; C_d2f_dz2 *= D[l]; } - BoutReal C_d2f_dxdz = 2 * coords->g13[l]; + BoutReal C_d2f_dxdz = 2 * coords->g13()[l]; if (issetD) { C_d2f_dxdz *= D[l]; } @@ -360,23 +360,23 @@ void LaplaceHypre3d::updateMatrix3D() { // Must add these (rather than assign) so that elements used in // interpolation don't overwrite each other. BOUT_FOR_SERIAL(l, indexer->getRegionNobndry()) { - BoutReal C_df_dy = (coords->G2[l] - dJ_dy[l] / coords->J[l]); + BoutReal C_df_dy = (coords->G2()[l] - dJ_dy[l] / coords->J()[l]); if (issetD) { C_df_dy *= D[l]; } if (issetC) { C_df_dy += - (coords->g12[l] * dc_dx[l] + (coords->g22[l] - 1. / coords->g_22[l]) * dc_dy[l] - + coords->g23[l] * dc_dz[l]) + (coords->g12()[l] * dc_dx[l] + (coords->g22()[l] - 1. / coords->g_22()[l]) * dc_dy[l] + + coords->g23()[l] * dc_dz[l]) / C1[l]; } - BoutReal C_d2f_dy2 = (coords->g22[l] - 1.0 / coords->g_22[l]); + BoutReal C_d2f_dy2 = (coords->g22()[l] - 1.0 / coords->g_22()[l]); if (issetD) { C_d2f_dy2 *= D[l]; } - BoutReal C_d2f_dxdy = 2 * coords->g12[l], C_d2f_dydz = 2 * coords->g23[l]; + BoutReal C_d2f_dxdy = 2 * coords->g12()[l], C_d2f_dydz = 2 * coords->g23()[l]; if (issetD) { C_d2f_dxdy *= D[l]; C_d2f_dydz *= D[l]; @@ -384,7 +384,7 @@ void LaplaceHypre3d::updateMatrix3D() { // Adjust the coefficients to include finite-difference factors if (nonuniform) { - C_df_dy += C_d2f_dy2 * coords->d1_dy[l]; + C_df_dy += C_d2f_dy2 * coords->d1_dy()[l]; } C_df_dy /= 2 * coords->dy()[l]; C_d2f_dy2 /= SQ(coords->dy()[l]); diff --git a/src/invert/laplace/impls/petsc/petsc_laplace.cxx b/src/invert/laplace/impls/petsc/petsc_laplace.cxx index d2275bd789..ac7a7bc7be 100644 --- a/src/invert/laplace/impls/petsc/petsc_laplace.cxx +++ b/src/invert/laplace/impls/petsc/petsc_laplace.cxx @@ -420,31 +420,31 @@ FieldPerp LaplacePetsc::solve(const FieldPerp& b, const FieldPerp& x0) { if (fourth_order) { // Fourth Order Accuracy on Boundary Element(i, x, z, 0, 0, - -25.0 / (12.0 * coords->dx(x, y, z)) / sqrt(coords->g_11(x, y, z)), + -25.0 / (12.0 * coords->dx()(x, y, z)) / sqrt(coords->g_11()(x, y, z)), MatA); Element(i, x, z, 1, 0, - 4.0 / coords->dx(x, y, z) / sqrt(coords->g_11(x, y, z)), MatA); + 4.0 / coords->dx()(x, y, z) / sqrt(coords->g_11()(x, y, z)), MatA); Element(i, x, z, 2, 0, - -3.0 / coords->dx(x, y, z) / sqrt(coords->g_11(x, y, z)), MatA); + -3.0 / coords->dx()(x, y, z) / sqrt(coords->g_11()(x, y, z)), MatA); Element(i, x, z, 3, 0, - 4.0 / (3.0 * coords->dx(x, y, z)) / sqrt(coords->g_11(x, y, z)), + 4.0 / (3.0 * coords->dx()(x, y, z)) / sqrt(coords->g_11()(x, y, z)), MatA); Element(i, x, z, 4, 0, - -1.0 / (4.0 * coords->dx(x, y, z)) / sqrt(coords->g_11(x, y, z)), + -1.0 / (4.0 * coords->dx()(x, y, z)) / sqrt(coords->g_11()(x, y, z)), MatA); } else { // Second Order Accuracy on Boundary - // Element(i,x,z, 0, 0, -3.0 / (2.0*coords->dx(x,y)), MatA ); - // Element(i,x,z, 1, 0, 2.0 / coords->dx(x,y), MatA ); - // Element(i,x,z, 2, 0, -1.0 / (2.0*coords->dx(x,y)), MatA ); + // Element(i,x,z, 0, 0, -3.0 / (2.0*coords->dx()(x,y)), MatA ); + // Element(i,x,z, 1, 0, 2.0 / coords->dx()(x,y), MatA ); + // Element(i,x,z, 2, 0, -1.0 / (2.0*coords->dx()(x,y)), MatA ); // Element(i,x,z, 3, 0, 0.0, MatA ); // Reset these elements to 0 // in case 4th order flag was used previously: not allowed now // Element(i,x,z, 4, 0, 0.0, MatA ); // Second Order Accuracy on Boundary, set half-way between grid points Element(i, x, z, 0, 0, - -1.0 / coords->dx(x, y, z) / sqrt(coords->g_11(x, y, z)), MatA); + -1.0 / coords->dx()(x, y, z) / sqrt(coords->g_11()(x, y, z)), MatA); Element(i, x, z, 1, 0, - 1.0 / coords->dx(x, y, z) / sqrt(coords->g_11(x, y, z)), MatA); + 1.0 / coords->dx()(x, y, z) / sqrt(coords->g_11()(x, y, z)), MatA); Element(i, x, z, 2, 0, 0.0, MatA); // Element(i,x,z, 3, 0, 0.0, MatA ); // Reset // these elements to 0 in case 4th order flag was @@ -506,9 +506,9 @@ FieldPerp LaplacePetsc::solve(const FieldPerp& b, const FieldPerp& x0) { // Set the matrix coefficients Coeffs(x, y, z, A1, A2, A3, A4, A5); - BoutReal dx = coords->dx(x, y, z); + BoutReal dx = coords->dx()(x, y, z); BoutReal dx2 = SQ(dx); - BoutReal dz = coords->dz(x, y, z); + BoutReal dz = coords->dz()(x, y, z); BoutReal dz2 = SQ(dz); BoutReal dxdz = dx * dz; @@ -685,32 +685,32 @@ FieldPerp LaplacePetsc::solve(const FieldPerp& b, const FieldPerp& x0) { if (fourth_order) { // Fourth Order Accuracy on Boundary Element(i, x, z, 0, 0, - 25.0 / (12.0 * coords->dx(x, y, z)) / sqrt(coords->g_11(x, y, z)), + 25.0 / (12.0 * coords->dx()(x, y, z)) / sqrt(coords->g_11()(x, y, z)), MatA); Element(i, x, z, -1, 0, - -4.0 / coords->dx(x, y, z) / sqrt(coords->g_11(x, y, z)), MatA); + -4.0 / coords->dx()(x, y, z) / sqrt(coords->g_11()(x, y, z)), MatA); Element(i, x, z, -2, 0, - 3.0 / coords->dx(x, y, z) / sqrt(coords->g_11(x, y, z)), MatA); + 3.0 / coords->dx()(x, y, z) / sqrt(coords->g_11()(x, y, z)), MatA); Element(i, x, z, -3, 0, - -4.0 / (3.0 * coords->dx(x, y, z)) / sqrt(coords->g_11(x, y, z)), + -4.0 / (3.0 * coords->dx()(x, y, z)) / sqrt(coords->g_11()(x, y, z)), MatA); Element(i, x, z, -4, 0, - 1.0 / (4.0 * coords->dx(x, y, z)) / sqrt(coords->g_11(x, y, z)), + 1.0 / (4.0 * coords->dx()(x, y, z)) / sqrt(coords->g_11()(x, y, z)), MatA); } else { // // Second Order Accuracy on Boundary - // Element(i,x,z, 0, 0, 3.0 / (2.0*coords->dx(x,y)), MatA ); - // Element(i,x,z, -1, 0, -2.0 / coords->dx(x,y), MatA ); - // Element(i,x,z, -2, 0, 1.0 / (2.0*coords->dx(x,y)), MatA ); + // Element(i,x,z, 0, 0, 3.0 / (2.0*coords->dx()(x,y)), MatA ); + // Element(i,x,z, -1, 0, -2.0 / coords->dx()(x,y), MatA ); + // Element(i,x,z, -2, 0, 1.0 / (2.0*coords->dx()(x,y)), MatA ); // Element(i,x,z, -3, 0, 0.0, MatA ); // Reset these elements to 0 // in case 4th order flag was used previously: not allowed now // Element(i,x,z, -4, 0, 0.0, MatA ); // Second Order Accuracy on Boundary, set half-way between grid // points Element(i, x, z, 0, 0, - 1.0 / coords->dx(x, y, z) / sqrt(coords->g_11(x, y, z)), MatA); + 1.0 / coords->dx()(x, y, z) / sqrt(coords->g_11()(x, y, z)), MatA); Element(i, x, z, -1, 0, - -1.0 / coords->dx(x, y, z) / sqrt(coords->g_11(x, y, z)), MatA); + -1.0 / coords->dx()(x, y, z) / sqrt(coords->g_11()(x, y, z)), MatA); Element(i, x, z, -2, 0, 0.0, MatA); // Element(i,x,z, -3, 0, 0.0, MatA ); // Reset these elements to 0 // in case 4th order flag was used previously: not allowed now @@ -1002,9 +1002,9 @@ void LaplacePetsc::Element(int i, int x, int z, int xshift, int zshift, PetscSca void LaplacePetsc::Coeffs(int x, int y, int z, BoutReal& coef1, BoutReal& coef2, BoutReal& coef3, BoutReal& coef4, BoutReal& coef5) { - coef1 = coords->g11(x, y, z); // X 2nd derivative coefficient - coef2 = coords->g33(x, y, z); // Z 2nd derivative coefficient - coef3 = 2. * coords->g13(x, y, z); // X-Z mixed derivative coefficient + coef1 = coords->g11()(x, y, z); // X 2nd derivative coefficient + coef2 = coords->g33()(x, y, z); // Z 2nd derivative coefficient + coef3 = 2. * coords->g13()(x, y, z); // X-Z mixed derivative coefficient coef4 = 0.0; coef5 = 0.0; @@ -1021,15 +1021,15 @@ void LaplacePetsc::Coeffs(int x, int y, int z, BoutReal& coef1, BoutReal& coef2, // non-uniform mesh correction if ((x != 0) && (x != (localmesh->LocalNx - 1))) { coef4 -= 0.5 - * ((coords->dx(x + 1, y, z) - coords->dx(x - 1, y, z)) - / SQ(coords->dx(x, y, z))) + * ((coords->dx()(x + 1, y, z) - coords->dx()(x - 1, y, z)) + / SQ(coords->dx()(x, y, z))) * coef1; // BOUT-06 term } } if (localmesh->IncIntShear) { // d2dz2 term - coef2 += coords->g11(x, y, z) * coords->IntShiftTorsion()(x, y, z) + coef2 += coords->g11()(x, y, z) * coords->IntShiftTorsion()(x, y, z) * coords->IntShiftTorsion()(x, y, z); // Mixed derivative coef3 = 0.0; // This cancels out @@ -1070,21 +1070,21 @@ void LaplacePetsc::Coeffs(int x, int y, int z, BoutReal& coef1, BoutReal& coef2, // Fourth order discretization of C in x ddx_C = (-C2(x + 2, y, z) + 8. * C2(x + 1, y, z) - 8. * C2(x - 1, y, z) + C2(x - 2, y, z)) - / (12. * coords->dx(x, y, z) * (C1(x, y, z))); + / (12. * coords->dx()(x, y, z) * (C1(x, y, z))); // Fourth order discretization of C in z ddz_C = (-C2(x, y, zpp) + 8. * C2(x, y, zp) - 8. * C2(x, y, zm) + C2(x, y, zmm)) - / (12. * coords->dz(x, y, z) * (C1(x, y, z))); + / (12. * coords->dz()(x, y, z) * (C1(x, y, z))); } else { // Second order discretization of C in x ddx_C = (C2(x + 1, y, z) - C2(x - 1, y, z)) - / (2. * coords->dx(x, y, z) * (C1(x, y, z))); + / (2. * coords->dx()(x, y, z) * (C1(x, y, z))); // Second order discretization of C in z ddz_C = - (C2(x, y, zp) - C2(x, y, zm)) / (2. * coords->dz(x, y, z) * (C1(x, y, z))); + (C2(x, y, zp) - C2(x, y, zm)) / (2. * coords->dz()(x, y, z) * (C1(x, y, z))); } - coef4 += coords->g11(x, y, z) * ddx_C + coords->g13(x, y, z) * ddz_C; - coef5 += coords->g13(x, y, z) * ddx_C + coords->g33(x, y, z) * ddz_C; + coef4 += coords->g11()(x, y, z) * ddx_C + coords->g13()(x, y, z) * ddz_C; + coef5 += coords->g13()(x, y, z) * ddx_C + coords->g33()(x, y, z) * ddz_C; } } diff --git a/src/invert/laplace/impls/petsc3damg/petsc3damg.cxx b/src/invert/laplace/impls/petsc3damg/petsc3damg.cxx index b9b01748c4..2150c03d7e 100644 --- a/src/invert/laplace/impls/petsc3damg/petsc3damg.cxx +++ b/src/invert/laplace/impls/petsc3damg/petsc3damg.cxx @@ -120,7 +120,7 @@ LaplacePetsc3dAmg::LaplacePetsc3dAmg(Options* opt, const CELL_LOC loc, Mesh* mes // Set up boundary conditions in operator const bool inner_X_neumann = flagSet(inner_boundary_flags, INVERT_AC_GRAD); - const auto inner_X_BC = inner_X_neumann ? -1. / coords->dx / sqrt(coords->g_11) : 0.5; + const auto inner_X_BC = inner_X_neumann ? -1. / coords->dx() / sqrt(coords->g_11()) : 0.5; const auto inner_X_BC_plus = inner_X_neumann ? -inner_X_BC : 0.5; BOUT_FOR_SERIAL(i, indexer->getRegionInnerX()) { @@ -129,7 +129,7 @@ LaplacePetsc3dAmg::LaplacePetsc3dAmg(Options* opt, const CELL_LOC loc, Mesh* mes } const bool outer_X_neumann = flagSet(outer_boundary_flags, INVERT_AC_GRAD); - const auto outer_X_BC = outer_X_neumann ? 1. / coords->dx / sqrt(coords->g_11) : 0.5; + const auto outer_X_BC = outer_X_neumann ? 1. / coords->dx() / sqrt(coords->g_11()) : 0.5; const auto outer_X_BC_minus = outer_X_neumann ? -outer_X_BC : 0.5; BOUT_FOR_SERIAL(i, indexer->getRegionOuterX()) { @@ -138,7 +138,7 @@ LaplacePetsc3dAmg::LaplacePetsc3dAmg(Options* opt, const CELL_LOC loc, Mesh* mes } const bool lower_Y_neumann = flagSet(lower_boundary_flags, INVERT_AC_GRAD); - const auto lower_Y_BC = lower_Y_neumann ? -1. / coords->dy / sqrt(coords->g_22) : 0.5; + const auto lower_Y_BC = lower_Y_neumann ? -1. / coords->dy() / sqrt(coords->g_22()) : 0.5; const auto lower_Y_BC_plus = lower_Y_neumann ? -lower_Y_BC : 0.5; BOUT_FOR_SERIAL(i, indexer->getRegionLowerY()) { @@ -147,7 +147,7 @@ LaplacePetsc3dAmg::LaplacePetsc3dAmg(Options* opt, const CELL_LOC loc, Mesh* mes } const bool upper_Y_neumann = flagSet(upper_boundary_flags, INVERT_AC_GRAD); - const auto upper_Y_BC = upper_Y_neumann ? 1. / coords->dy / sqrt(coords->g_22) : 0.5; + const auto upper_Y_BC = upper_Y_neumann ? 1. / coords->dy() / sqrt(coords->g_22()) : 0.5; const auto upper_Y_BC_minus = upper_Y_neumann ? -upper_Y_BC : 0.5; BOUT_FOR_SERIAL(i, indexer->getRegionUpperY()) { @@ -275,7 +275,7 @@ void LaplacePetsc3dAmg::updateMatrix3D() { const Field3D dc_dx = issetC ? DDX(C2) : Field3D(); const Field3D dc_dy = issetC ? DDY(C2) : Field3D(); const Field3D dc_dz = issetC ? DDZ(C2) : Field3D(); - const auto dJ_dy = DDY(coords->J / coords->g_22); + const auto dJ_dy = DDY(coords->J() / coords->g_22()); // Set up the matrix for the internal points on the grid. // Boundary conditions were set in the constructor. @@ -284,18 +284,18 @@ void LaplacePetsc3dAmg::updateMatrix3D() { // avoid confusing it with the x-index. // Calculate coefficients for the terms in the differential operator - BoutReal C_df_dx = coords->G1[l]; - BoutReal C_df_dz = coords->G3[l]; + BoutReal C_df_dx = coords->G1()[l]; + BoutReal C_df_dz = coords->G3()[l]; if (issetD) { C_df_dx *= D[l]; C_df_dz *= D[l]; } if (issetC) { - C_df_dx += (coords->g11[l] * dc_dx[l] + coords->g12[l] * dc_dy[l] - + coords->g13[l] * dc_dz[l]) + C_df_dx += (coords->g11()[l] * dc_dx[l] + coords->g12()[l] * dc_dy[l] + + coords->g13()[l] * dc_dz[l]) / C1[l]; - C_df_dz += (coords->g13[l] * dc_dx[l] + coords->g23[l] * dc_dy[l] - + coords->g33[l] * dc_dz[l]) + C_df_dz += (coords->g13()[l] * dc_dx[l] + coords->g23()[l] * dc_dy[l] + + coords->g33()[l] * dc_dz[l]) / C1[l]; } if (issetE) { @@ -303,16 +303,16 @@ void LaplacePetsc3dAmg::updateMatrix3D() { C_df_dz += Ez[l]; } - BoutReal C_d2f_dx2 = coords->g11[l]; - BoutReal C_d2f_dy2 = (coords->g22[l] - 1.0 / coords->g_22[l]); - BoutReal C_d2f_dz2 = coords->g33[l]; + BoutReal C_d2f_dx2 = coords->g11()[l]; + BoutReal C_d2f_dy2 = (coords->g22()[l] - 1.0 / coords->g_22()[l]); + BoutReal C_d2f_dz2 = coords->g33()[l]; if (issetD) { C_d2f_dx2 *= D[l]; C_d2f_dy2 *= D[l]; C_d2f_dz2 *= D[l]; } - BoutReal C_d2f_dxdz = 2 * coords->g13[l]; + BoutReal C_d2f_dxdz = 2 * coords->g13()[l]; if (issetD) { C_d2f_dxdz *= D[l]; } @@ -360,24 +360,24 @@ void LaplacePetsc3dAmg::updateMatrix3D() { // Must add these (rather than assign) so that elements used in // interpolation don't overwrite each other. BOUT_FOR_SERIAL(l, indexer->getRegionNobndry()) { - BoutReal C_df_dy = (coords->G2[l] - dJ_dy[l] / coords->J[l]); + BoutReal C_df_dy = (coords->G2()[l] - dJ_dy[l] / coords->J()[l]); if (issetD) { C_df_dy *= D[l]; } if (issetC) { C_df_dy += - (coords->g12[l] * dc_dx[l] + (coords->g22[l] - 1. / coords->g_22[l]) * dc_dy[l] - + coords->g23[l] * dc_dz[l]) + (coords->g12()[l] * dc_dx[l] + (coords->g22()[l] - 1. / coords->g_22()[l]) * dc_dy[l] + + coords->g23()[l] * dc_dz[l]) / C1[l]; } - BoutReal C_d2f_dy2 = (coords->g22[l] - 1.0 / coords->g_22[l]); + BoutReal C_d2f_dy2 = (coords->g22()[l] - 1.0 / coords->g_22()[l]); if (issetD) { C_d2f_dy2 *= D[l]; } - BoutReal C_d2f_dxdy = 2 * coords->g12[l]; - BoutReal C_d2f_dydz = 2 * coords->g23[l]; + BoutReal C_d2f_dxdy = 2 * coords->g12()[l]; + BoutReal C_d2f_dydz = 2 * coords->g23()[l]; if (issetD) { C_d2f_dxdy *= D[l]; C_d2f_dydz *= D[l]; @@ -385,7 +385,7 @@ void LaplacePetsc3dAmg::updateMatrix3D() { // Adjust the coefficients to include finite-difference factors if (nonuniform) { - C_df_dy += C_d2f_dy2 * coords->d1_dy[l]; + C_df_dy += C_d2f_dy2 * coords->d1_dy()[l]; } C_df_dy /= 2 * coords->dy()[l]; C_d2f_dy2 /= SQ(coords->dy()[l]); diff --git a/src/invert/laplacexy/laplacexy.cxx b/src/invert/laplacexy/laplacexy.cxx index ef039c75cc..41b23033ec 100644 --- a/src/invert/laplacexy/laplacexy.cxx +++ b/src/invert/laplacexy/laplacexy.cxx @@ -897,13 +897,13 @@ void LaplaceXY::setMatrixElementsFiniteVolume(const Field2D& A, const Field2D& B // (1/J) d/dx ( J * g11 d/dx ) + (1/J) d/dy ( J * g22 d/dy ) auto coords = localmesh->getCoordinates(location); - const Field2D J_DC = DC(coords->J); - const Field2D g11_DC = DC(coords->g11); + const Field2D J_DC = DC(coords->J()); + const Field2D g11_DC = DC(coords->g11()); const Field2D dx_DC = DC(coords->dx()); const Field2D dy_DC = DC(coords->dy()); - const Field2D g_22_DC = DC(coords->g_22); - const Field2D g_23_DC = DC(coords->g_23); - const Field2D g23_DC = DC(coords->g23); + const Field2D g_22_DC = DC(coords->g_22()); + const Field2D g_23_DC = DC(coords->g_23()); + const Field2D g23_DC = DC(coords->g23()); for (int x = localmesh->xstart; x <= localmesh->xend; x++) { for (int y = localmesh->ystart; y <= localmesh->yend; y++) { @@ -1007,13 +1007,13 @@ void LaplaceXY::setMatrixElementsFiniteDifference(const Field2D& A, const Field2 // + B*f auto coords = localmesh->getCoordinates(location); - const Field2D G1_2D = DC(coords->G1); - const Field2D G2_2D = DC(coords->G2); - const Field2D J_2D = DC(coords->J); - const Field2D g11_2D = DC(coords->g11); - const Field2D g_22_2D = DC(coords->g_22); - const Field2D g22_2D = DC(coords->g22); - const Field2D g12_2D = DC(coords->g12); + const Field2D G1_2D = DC(coords->G1()); + const Field2D G2_2D = DC(coords->G2()); + const Field2D J_2D = DC(coords->J()); + const Field2D g11_2D = DC(coords->g11()); + const Field2D g_22_2D = DC(coords->g_22()); + const Field2D g22_2D = DC(coords->g22()); + const Field2D g12_2D = DC(coords->g12()); const Field2D d1_dx_2D = DC(coords->d1_dx()); const Field2D d1_dy_2D = DC(coords->d1_dy()); const Field2D dx_2D = DC(coords->dx()); diff --git a/src/invert/laplacexy2/laplacexy2.cxx b/src/invert/laplacexy2/laplacexy2.cxx index 7563e9618b..3a637b8491 100644 --- a/src/invert/laplacexy2/laplacexy2.cxx +++ b/src/invert/laplacexy2/laplacexy2.cxx @@ -147,20 +147,20 @@ void LaplaceXY2::setCoefs(const Field2D& A, const Field2D& B) { // XX component // Metrics on x+1/2 boundary - BoutReal J = 0.5 * (coords->J[index] + coords->J[ind_xp]); - BoutReal g11 = 0.5 * (coords->g11[index] + coords->g11[ind_xp]); + BoutReal J = 0.5 * (coords->J()[index] + coords->J()[ind_xp]); + BoutReal g11 = 0.5 * (coords->g11()[index] + coords->g11()[ind_xp]); BoutReal dx = 0.5 * (coords->dx()[index] + coords->dx()[ind_xp]); BoutReal Acoef = 0.5 * (A[index] + A[ind_xp]); - BoutReal xp = Acoef * J * g11 / (coords->J[index] * dx * coords->dx()[index]); + BoutReal xp = Acoef * J * g11 / (coords->J()[index] * dx * coords->dx()[index]); // Metrics on x-1/2 boundary - J = 0.5 * (coords->J[index] + coords->J[ind_xm]); - g11 = 0.5 * (coords->g11[index] + coords->g11[ind_xm]); + J = 0.5 * (coords->J()[index] + coords->J()[ind_xm]); + g11 = 0.5 * (coords->g11()[index] + coords->g11()[ind_xm]); dx = 0.5 * (coords->dx()[index] + coords->dx()[ind_xm]); Acoef = 0.5 * (A[index] + A[ind_xm]); - BoutReal xm = Acoef * J * g11 / (coords->J[index] * dx * coords->dx()[index]); + BoutReal xm = Acoef * J * g11 / (coords->J()[index] * dx * coords->dx()[index]); BoutReal c = B[index] - xp - xm; // Central coefficient @@ -173,28 +173,28 @@ void LaplaceXY2::setCoefs(const Field2D& A, const Field2D& B) { // YY component // Metrics at y+1/2 - J = 0.5 * (coords->J[index] + coords->J[ind_yp]); - BoutReal g_22 = 0.5 * (coords->g_22[index] + coords->g_22[ind_yp]); - BoutReal g23 = 0.5 * (coords->g23[index] + coords->g23[ind_yp]); - BoutReal g_23 = 0.5 * (coords->g_23[index] + coords->g_23[ind_yp]); + J = 0.5 * (coords->J()[index] + coords->J()[ind_yp]); + BoutReal g_22 = 0.5 * (coords->g_22()[index] + coords->g_22()[ind_yp]); + BoutReal g23 = 0.5 * (coords->g23()[index] + coords->g23()[ind_yp]); + BoutReal g_23 = 0.5 * (coords->g_23()[index] + coords->g_23()[ind_yp]); BoutReal dy = 0.5 * (coords->dy()[index] + coords->dy()[ind_yp]); Acoef = 0.5 * (A[ind_yp] + A[index]); BoutReal yp = - -Acoef * J * g23 * g_23 / (g_22 * coords->J[index] * dy * coords->dy()[index]); + -Acoef * J * g23 * g_23 / (g_22 * coords->J()[index] * dy * coords->dy()[index]); c -= yp; matrix(index, ind_yp) = yp; // Metrics at y-1/2 - J = 0.5 * (coords->J[index] + coords->J[ind_ym]); - g_22 = 0.5 * (coords->g_22[index] + coords->g_22[ind_ym]); - g23 = 0.5 * (coords->g23[index] + coords->g23[ind_ym]); - g_23 = 0.5 * (coords->g_23[index] + coords->g_23[ind_ym]); + J = 0.5 * (coords->J()[index] + coords->J()[ind_ym]); + g_22 = 0.5 * (coords->g_22()[index] + coords->g_22()[ind_ym]); + g23 = 0.5 * (coords->g23()[index] + coords->g23()[ind_ym]); + g_23 = 0.5 * (coords->g_23()[index] + coords->g_23()[ind_ym]); dy = 0.5 * (coords->dy()[index] + coords->dy()[ind_ym]); Acoef = 0.5 * (A[ind_ym] + A[index]); BoutReal ym = - -Acoef * J * g23 * g_23 / (g_22 * coords->J[index] * dy * coords->dy()[index]); + -Acoef * J * g23 * g_23 / (g_22 * coords->J()[index] * dy * coords->dy()[index]); c -= ym; matrix(index, ind_ym) = ym; } diff --git a/src/invert/laplacexy2/laplacexy2_hypre.cxx b/src/invert/laplacexy2/laplacexy2_hypre.cxx index a0d7487948..e3d48d5ca5 100644 --- a/src/invert/laplacexy2/laplacexy2_hypre.cxx +++ b/src/invert/laplacexy2/laplacexy2_hypre.cxx @@ -106,20 +106,20 @@ void LaplaceXY2Hypre::setCoefs(const Field2D& A, const Field2D& B) { // XX component // Metrics on x+1/2 boundary - BoutReal J = 0.5 * (coords->J[index] + coords->J[ind_xp]); - BoutReal g11 = 0.5 * (coords->g11[index] + coords->g11[ind_xp]); + BoutReal J = 0.5 * (coords->J()[index] + coords->J()[ind_xp]); + BoutReal g11 = 0.5 * (coords->g11()[index] + coords->g11()[ind_xp]); BoutReal dx = 0.5 * (coords->dx()[index] + coords->dx()[ind_xp]); BoutReal Acoef = 0.5 * (A[index] + A[ind_xp]); - BoutReal xp = Acoef * J * g11 / (coords->J[index] * dx * coords->dx()[index]); + BoutReal xp = Acoef * J * g11 / (coords->J()[index] * dx * coords->dx()[index]); // Metrics on x-1/2 boundary - J = 0.5 * (coords->J[index] + coords->J[ind_xm]); - g11 = 0.5 * (coords->g11[index] + coords->g11[ind_xm]); + J = 0.5 * (coords->J()[index] + coords->J()[ind_xm]); + g11 = 0.5 * (coords->g11()[index] + coords->g11()[ind_xm]); dx = 0.5 * (coords->dx()[index] + coords->dx()[ind_xm]); Acoef = 0.5 * (A[index] + A[ind_xm]); - BoutReal xm = Acoef * J * g11 / (coords->J[index] * dx * coords->dx()[index]); + BoutReal xm = Acoef * J * g11 / (coords->J()[index] * dx * coords->dx()[index]); BoutReal c = B[index] - xp - xm; // Central coefficient @@ -132,27 +132,27 @@ void LaplaceXY2Hypre::setCoefs(const Field2D& A, const Field2D& B) { // YY component // Metrics at y+1/2 - J = 0.5 * (coords->J[index] + coords->J[ind_yp]); - BoutReal g_22 = 0.5 * (coords->g_22[index] + coords->g_22[ind_yp]); - BoutReal g23 = 0.5 * (coords->g23[index] + coords->g23[ind_yp]); - BoutReal g_23 = 0.5 * (coords->g_23[index] + coords->g_23[ind_yp]); + J = 0.5 * (coords->J()[index] + coords->J()[ind_yp]); + BoutReal g_22 = 0.5 * (coords->g_22()[index] + coords->g_22()[ind_yp]); + BoutReal g23 = 0.5 * (coords->g23()[index] + coords->g23()[ind_yp]); + BoutReal g_23 = 0.5 * (coords->g_23()[index] + coords->g_23()[ind_yp]); BoutReal dy = 0.5 * (coords->dy()[index] + coords->dy()[ind_yp]); Acoef = 0.5 * (A[ind_yp] + A[index]); BoutReal yp = - -Acoef * J * g23 * g_23 / (g_22 * coords->J[index] * dy * coords->dy()[index]); + -Acoef * J * g23 * g_23 / (g_22 * coords->J()[index] * dy * coords->dy()[index]); c -= yp; // Metrics at y-1/2 - J = 0.5 * (coords->J[index] + coords->J[ind_ym]); - g_22 = 0.5 * (coords->g_22[index] + coords->g_22[ind_ym]); - g23 = 0.5 * (coords->g23[index] + coords->g23[ind_ym]); - g_23 = 0.5 * (coords->g_23[index] + coords->g_23[ind_ym]); + J = 0.5 * (coords->J()[index] + coords->J()[ind_ym]); + g_22 = 0.5 * (coords->g_22()[index] + coords->g_22()[ind_ym]); + g23 = 0.5 * (coords->g23()[index] + coords->g23()[ind_ym]); + g_23 = 0.5 * (coords->g_23()[index] + coords->g_23()[ind_ym]); dy = 0.5 * (coords->dy()[index] + coords->dy()[ind_ym]); Acoef = 0.5 * (A[ind_ym] + A[index]); BoutReal ym = - -Acoef * J * g23 * g_23 / (g_22 * coords->J[index] * dy * coords->dy()[index]); + -Acoef * J * g23 * g_23 / (g_22 * coords->J()[index] * dy * coords->dy()[index]); c -= ym; M(index, ind_yp) = yp; M(index, ind_ym) = ym; diff --git a/src/invert/laplacexz/impls/petsc/laplacexz-petsc.cxx b/src/invert/laplacexz/impls/petsc/laplacexz-petsc.cxx index 8ddb058ab2..4a3a82386e 100644 --- a/src/invert/laplacexz/impls/petsc/laplacexz-petsc.cxx +++ b/src/invert/laplacexz/impls/petsc/laplacexz-petsc.cxx @@ -402,26 +402,26 @@ void LaplaceXZpetsc::setCoefs(const Field3D& Ain, const Field3D& Bin) { // XX component { // Metrics on x+1/2 boundary - const BoutReal J = 0.5 * (coords->J(x, y, z) + coords->J(x + 1, y, z)); - const BoutReal g11 = 0.5 * (coords->g11(x, y, z) + coords->g11(x + 1, y, z)); - const BoutReal dx = 0.5 * (coords->dx(x, y, z) + coords->dx(x + 1, y, z)); + const BoutReal J = 0.5 * (coords->J()(x, y, z) + coords->J()(x + 1, y, z)); + const BoutReal g11 = 0.5 * (coords->g11()(x, y, z) + coords->g11()(x + 1, y, z)); + const BoutReal dx = 0.5 * (coords->dx()(x, y, z) + coords->dx()(x + 1, y, z)); const BoutReal Acoef = 0.5 * (A(x, y, z) + A(x + 1, y, z)); const BoutReal val = - Acoef * J * g11 / (coords->J(x, y, z) * dx * coords->dx(x, y, z)); + Acoef * J * g11 / (coords->J()(x, y, z) * dx * coords->dx()(x, y, z)); xp = val; c = -val; } { // Metrics on x-1/2 boundary - const BoutReal J = 0.5 * (coords->J(x, y, z) + coords->J(x - 1, y, z)); - const BoutReal g11 = 0.5 * (coords->g11(x, y, z) + coords->g11(x - 1, y, z)); - const BoutReal dx = 0.5 * (coords->dx(x, y, z) + coords->dx(x - 1, y, z)); + const BoutReal J = 0.5 * (coords->J()(x, y, z) + coords->J()(x - 1, y, z)); + const BoutReal g11 = 0.5 * (coords->g11()(x, y, z) + coords->g11()(x - 1, y, z)); + const BoutReal dx = 0.5 * (coords->dx()(x, y, z) + coords->dx()(x - 1, y, z)); const BoutReal Acoef = 0.5 * (A(x, y, z) + A(x - 1, y, z)); const BoutReal val = - Acoef * J * g11 / (coords->J(x, y, z) * dx * coords->dx(x, y, z)); + Acoef * J * g11 / (coords->J()(x, y, z) * dx * coords->dx()(x, y, z)); xm = val; c -= val; } @@ -432,24 +432,24 @@ void LaplaceXZpetsc::setCoefs(const Field3D& Ain, const Field3D& Bin) { const int zplus = (z + 1) % (localmesh->LocalNz); { - const BoutReal J = 0.5 * (coords->J(x, y, z) + coords->J(x, y, zplus)); - const BoutReal g33 = 0.5 * (coords->g33(x, y, z) + coords->g33(x, y, zplus)); - const BoutReal dz = 0.5 * (coords->dz(x, y, z) + coords->dz(x, y, zplus)); + const BoutReal J = 0.5 * (coords->J()(x, y, z) + coords->J()(x, y, zplus)); + const BoutReal g33 = 0.5 * (coords->g33()(x, y, z) + coords->g33()(x, y, zplus)); + const BoutReal dz = 0.5 * (coords->dz()(x, y, z) + coords->dz()(x, y, zplus)); // Metrics on z+1/2 boundary const BoutReal Acoef = 0.5 * (A(x, y, z) + A(x, y, zplus)); - const BoutReal val = Acoef * J * g33 / (coords->J(x, y, z) * dz * dz); + const BoutReal val = Acoef * J * g33 / (coords->J()(x, y, z) * dz * dz); zp = val; c -= val; } { // Metrics on z-1/2 boundary - const BoutReal J = 0.5 * (coords->J(x, y, z) + coords->J(x, y, zminus)); - const BoutReal g33 = 0.5 * (coords->g33(x, y, z) + coords->g33(x, y, zminus)); + const BoutReal J = 0.5 * (coords->J()(x, y, z) + coords->J()(x, y, zminus)); + const BoutReal g33 = 0.5 * (coords->g33()(x, y, z) + coords->g33()(x, y, zminus)); const BoutReal Acoef = 0.5 * (A(x, y, z) + A(x, y, zminus)); - const BoutReal dz = 0.5 * (coords->dz(x, y, z) + coords->dz(x, y, zminus)); + const BoutReal dz = 0.5 * (coords->dz()(x, y, z) + coords->dz()(x, y, zminus)); - const BoutReal val = Acoef * J * g33 / (coords->J(x, y, z) * dz * dz); + const BoutReal val = Acoef * J * g33 / (coords->J()(x, y, z) * dz * dz); zm = val; c -= val; } @@ -470,13 +470,13 @@ void LaplaceXZpetsc::setCoefs(const Field3D& Ain, const Field3D& Bin) { // so metrics at (x+1/2,z) // Metrics - const BoutReal J = 0.5 * (coords->J(x, y, z) + coords->J(x + 1, y, z)); - const BoutReal g13 = 0.5 * (coords->g13(x, y, z) + coords->g13(x + 1, y, z)); - const BoutReal dz = 0.5 * (coords->dz(x, y, z) + coords->dz(x + 1, y, z)); + const BoutReal J = 0.5 * (coords->J()(x, y, z) + coords->J()(x + 1, y, z)); + const BoutReal g13 = 0.5 * (coords->g13()(x, y, z) + coords->g13()(x + 1, y, z)); + const BoutReal dz = 0.5 * (coords->dz()(x, y, z) + coords->dz()(x + 1, y, z)); const BoutReal Acoef = 0.5 * (A(x, y, z) + A(x + 1, y, z)); const BoutReal val = - Acoef * J * g13 / (coords->J(x, y, z) * dz * coords->dx(x, y, z)); + Acoef * J * g13 / (coords->J()(x, y, z) * dz * coords->dx()(x, y, z)); // This val coefficient is multiplied by the (x+1/2,z+1/2) corner // and (x+1/2,z-1/2) corner @@ -506,13 +506,13 @@ void LaplaceXZpetsc::setCoefs(const Field3D& Ain, const Field3D& Bin) { // Taking derivative in z (between corners marked x) // so metrics at (x-1/2,z) - const BoutReal J = 0.5 * (coords->J(x, y, z) + coords->J(x - 1, y, z)); - const BoutReal g13 = 0.5 * (coords->g13(x, y, z) + coords->g13(x - 1, y, z)); - const BoutReal dz = 0.5 * (coords->dz(x, y, z) + coords->dz(x - 1, y, z)); + const BoutReal J = 0.5 * (coords->J()(x, y, z) + coords->J()(x - 1, y, z)); + const BoutReal g13 = 0.5 * (coords->g13()(x, y, z) + coords->g13()(x - 1, y, z)); + const BoutReal dz = 0.5 * (coords->dz()(x, y, z) + coords->dz()(x - 1, y, z)); const BoutReal Acoef = 0.5 * (A(x, y, z) + A(x - 1, y, z)); const BoutReal val = - -Acoef * J * g13 / (coords->J(x, y, z) * dz * coords->dx(x, y, z)); + -Acoef * J * g13 / (coords->J()(x, y, z) * dz * coords->dx()(x, y, z)); // (x+1/2,z+1/2) xpzp += 0.25 * val; @@ -539,13 +539,13 @@ void LaplaceXZpetsc::setCoefs(const Field3D& Ain, const Field3D& Bin) { // Taking derivative in x (between corners marked x) // so metrics at (x,z+1/2) - const BoutReal J = 0.5 * (coords->J(x, y, z) + coords->J(x, y, zplus)); - const BoutReal g13 = 0.5 * (coords->g13(x, y, z) + coords->g13(x, y, zplus)); - const BoutReal dx = 0.5 * (coords->dx(x, y, z) + coords->dx(x, y, zplus)); + const BoutReal J = 0.5 * (coords->J()(x, y, z) + coords->J()(x, y, zplus)); + const BoutReal g13 = 0.5 * (coords->g13()(x, y, z) + coords->g13()(x, y, zplus)); + const BoutReal dx = 0.5 * (coords->dx()(x, y, z) + coords->dx()(x, y, zplus)); const BoutReal Acoef = 0.5 * (A(x, y, z) + A(x, y, zplus)); const BoutReal val = - Acoef * J * g13 / (coords->J(x, y, z) * dx * coords->dz(x, y, z)); + Acoef * J * g13 / (coords->J()(x, y, z) * dx * coords->dz()(x, y, z)); // (x+1/2,z+1/2) //zp += 0.25 * val; Note cancels @@ -572,13 +572,13 @@ void LaplaceXZpetsc::setCoefs(const Field3D& Ain, const Field3D& Bin) { // Taking derivative in x (between corners marked x) // so metrics at (x,z-1/2) - const BoutReal J = 0.5 * (coords->J(x, y, z) + coords->J(x, y, zminus)); - const BoutReal g13 = 0.5 * (coords->g13(x, y, z) + coords->g13(x, y, zminus)); - const BoutReal dx = 0.5 * (coords->dx(x, y, z) + coords->dx(x, y, zminus)); + const BoutReal J = 0.5 * (coords->J()(x, y, z) + coords->J()(x, y, zminus)); + const BoutReal g13 = 0.5 * (coords->g13()(x, y, z) + coords->g13()(x, y, zminus)); + const BoutReal dx = 0.5 * (coords->dx()(x, y, z) + coords->dx()(x, y, zminus)); const BoutReal Acoef = 0.5 * (A(x, y, z) + A(x, y, zminus)); const BoutReal val = - -Acoef * J * g13 / (coords->J(x, y, z) * dx * coords->dz(x, y, z)); + -Acoef * J * g13 / (coords->J()(x, y, z) * dx * coords->dz()(x, y, z)); // (x+1/2,z-1/2) xpzm += 0.25 * val; diff --git a/src/mesh/difops.cxx b/src/mesh/difops.cxx index 7b57918c93..b8fe671640 100644 --- a/src/mesh/difops.cxx +++ b/src/mesh/difops.cxx @@ -1018,7 +1018,7 @@ Field3D bracket(const Field3D& f, const Field3D& g, BRACKET_METHOD method, const int jzm = ncz - 1; #if BOUT_USE_METRIC_3D const BoutReal spacingFactor = - 1.0 / (12 * metric->dz(jx, jy, jz) * metric->dx(jx, jy, jz)); + 1.0 / (12 * metric->dz()(jx, jy, jz) * metric->dx()(jx, jy, jz)); #endif // J++ = DDZ(f)*DDX(g) - DDX(f)*DDZ(g) @@ -1041,7 +1041,7 @@ Field3D bracket(const Field3D& f, const Field3D& g, BRACKET_METHOD method, for (int jz = 1; jz < mesh->LocalNz - 1; jz++) { #if BOUT_USE_METRIC_3D const BoutReal spacingFactor = - 1.0 / (12 * metric->dz(jx, jy, jz) * metric->dx(jx, jy, jz)); + 1.0 / (12 * metric->dz()(jx, jy, jz) * metric->dx()(jx, jy, jz)); #endif const int jzp = jz + 1; const int jzm = jz - 1; @@ -1069,7 +1069,7 @@ Field3D bracket(const Field3D& f, const Field3D& g, BRACKET_METHOD method, const int jzm = ncz - 2; #if BOUT_USE_METRIC_3D const BoutReal spacingFactor = - 1.0 / (12 * metric->dz(jx, jy, jz) * metric->dx(jx, jy, jz)); + 1.0 / (12 * metric->dz()(jx, jy, jz) * metric->dx()(jx, jy, jz)); #endif // J++ = DDZ(f)*DDX(g) - DDX(f)*DDZ(g) diff --git a/tests/unit/invert/laplace/test_laplace_hypre3d.cxx b/tests/unit/invert/laplace/test_laplace_hypre3d.cxx index 35bbb227e3..2cd677550d 100644 --- a/tests/unit/invert/laplace/test_laplace_hypre3d.cxx +++ b/tests/unit/invert/laplace/test_laplace_hypre3d.cxx @@ -45,7 +45,7 @@ class ForwardOperator { const Field3D operator()(Field3D& f) { auto result = d * Laplace_perp(f, CELL_DEFAULT, "free", "RGN_NOY") - + (Grad(f) * Grad(c2) - DDY(c2) * DDY(f) / coords->g_22) / c1 + a * f + + (Grad(f) * Grad(c2) - DDY(c2) * DDY(f) / coords->g_22()) / c1 + a * f + ex * DDX(f) + ez * DDZ(f); applyBoundaries(result, f); return result; @@ -61,7 +61,7 @@ class ForwardOperator { void applyBoundaries(Field3D& newF, Field3D& f) { BOUT_FOR(i, f.getMesh()->getRegion3D("RGN_INNER_X")) { if (inner_x_neumann) { - newF[i] = (f[i.xp()] - f[i]) / coords->dx()[i] / sqrt(coords->g_11[i]); + newF[i] = (f[i.xp()] - f[i]) / coords->dx()[i] / sqrt(coords->g_11()[i]); } else { newF[i] = 0.5 * (f[i] + f[i.xp()]); } @@ -69,7 +69,7 @@ class ForwardOperator { BOUT_FOR(i, f.getMesh()->getRegion3D("RGN_OUTER_X")) { if (outer_x_neumann) { - newF[i] = (f[i] - f[i.xm()]) / coords->dx()[i] / sqrt(coords->g_11[i]); + newF[i] = (f[i] - f[i.xm()]) / coords->dx()[i] / sqrt(coords->g_11()[i]); } else { newF[i] = 0.5 * (f[i.xm()] + f[i]); } @@ -77,7 +77,7 @@ class ForwardOperator { BOUT_FOR(i, f.getMesh()->getRegion3D("RGN_LOWER_Y")) { if (lower_y_neumann) { - newF[i] = (f[i.yp()] - f[i]) / coords->dx()[i] / sqrt(coords->g_11[i]); + newF[i] = (f[i.yp()] - f[i]) / coords->dx()[i] / sqrt(coords->g_11()[i]); } else { newF[i] = 0.5 * (f[i] + f[i.yp()]); } @@ -85,7 +85,7 @@ class ForwardOperator { BOUT_FOR(i, f.getMesh()->getRegion3D("RGN_UPPER_Y")) { if (upper_y_neumann) { - newF[i] = (f[i] - f[i.ym()]) / coords->dx()[i] / sqrt(coords->g_11[i]); + newF[i] = (f[i] - f[i.ym()]) / coords->dx()[i] / sqrt(coords->g_11()[i]); } else { newF[i] = 0.5 * (f[i.ym()] + f[i]); } diff --git a/tests/unit/invert/laplace/test_laplace_petsc3damg.cxx b/tests/unit/invert/laplace/test_laplace_petsc3damg.cxx index b6e1ede337..7e52dd5f4b 100644 --- a/tests/unit/invert/laplace/test_laplace_petsc3damg.cxx +++ b/tests/unit/invert/laplace/test_laplace_petsc3damg.cxx @@ -45,7 +45,7 @@ class ForwardOperator { const Field3D operator()(Field3D& f) { auto result = d * Laplace_perp(f, CELL_DEFAULT, "free", "RGN_NOY") - + (Grad(f) * Grad(c2) - DDY(c2) * DDY(f) / coords->g_22) / c1 + a * f + + (Grad(f) * Grad(c2) - DDY(c2) * DDY(f) / coords->g_22()) / c1 + a * f + ex * DDX(f) + ez * DDZ(f); applyBoundaries(result, f); return result; @@ -61,7 +61,7 @@ class ForwardOperator { void applyBoundaries(Field3D& newF, Field3D& f) { BOUT_FOR(i, f.getMesh()->getRegion3D("RGN_INNER_X")) { if (inner_x_neumann) { - newF[i] = (f[i.xp()] - f[i]) / coords->dx()[i] / sqrt(coords->g_11[i]); + newF[i] = (f[i.xp()] - f[i]) / coords->dx()[i] / sqrt(coords->g_11()[i]); } else { newF[i] = 0.5 * (f[i] + f[i.xp()]); } @@ -69,7 +69,7 @@ class ForwardOperator { BOUT_FOR(i, f.getMesh()->getRegion3D("RGN_OUTER_X")) { if (outer_x_neumann) { - newF[i] = (f[i] - f[i.xm()]) / coords->dx()[i] / sqrt(coords->g_11[i]); + newF[i] = (f[i] - f[i.xm()]) / coords->dx()[i] / sqrt(coords->g_11()[i]); } else { newF[i] = 0.5 * (f[i.xm()] + f[i]); } @@ -77,7 +77,7 @@ class ForwardOperator { BOUT_FOR(i, f.getMesh()->getRegion3D("RGN_LOWER_Y")) { if (lower_y_neumann) { - newF[i] = (f[i.yp()] - f[i]) / coords->dx()[i] / sqrt(coords->g_11[i]); + newF[i] = (f[i.yp()] - f[i]) / coords->dx()[i] / sqrt(coords->g_11()[i]); } else { newF[i] = 0.5 * (f[i] + f[i.yp()]); } @@ -85,7 +85,7 @@ class ForwardOperator { BOUT_FOR(i, f.getMesh()->getRegion3D("RGN_UPPER_Y")) { if (upper_y_neumann) { - newF[i] = (f[i] - f[i.ym()]) / coords->dx()[i] / sqrt(coords->g_11[i]); + newF[i] = (f[i] - f[i.ym()]) / coords->dx()[i] / sqrt(coords->g_11()[i]); } else { newF[i] = 0.5 * (f[i.ym()] + f[i]); } From 518672d961ae75646e881447031c568cd4825ef9 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 13 Mar 2024 12:56:34 +0000 Subject: [PATCH 346/491] Remove calls to geometry(). --- tests/unit/invert/laplace/test_laplace_hypre3d.cxx | 1 - tests/unit/invert/laplace/test_laplace_petsc3damg.cxx | 1 - 2 files changed, 2 deletions(-) diff --git a/tests/unit/invert/laplace/test_laplace_hypre3d.cxx b/tests/unit/invert/laplace/test_laplace_hypre3d.cxx index 2cd677550d..3a8bb811bd 100644 --- a/tests/unit/invert/laplace/test_laplace_hypre3d.cxx +++ b/tests/unit/invert/laplace/test_laplace_hypre3d.cxx @@ -103,7 +103,6 @@ class LaplaceHypre3dTest int nx = mesh->GlobalNx, ny = mesh->GlobalNy, nz = mesh->GlobalNz; static_cast(bout::globals::mesh) ->setGridDataSource(new GridFromOptions(Options::getRoot())); - bout::globals::mesh->getCoordinates()->geometry(); f3.allocate(); coef2.allocate(); coef3.allocate(); diff --git a/tests/unit/invert/laplace/test_laplace_petsc3damg.cxx b/tests/unit/invert/laplace/test_laplace_petsc3damg.cxx index 7e52dd5f4b..18d08800d2 100644 --- a/tests/unit/invert/laplace/test_laplace_petsc3damg.cxx +++ b/tests/unit/invert/laplace/test_laplace_petsc3damg.cxx @@ -104,7 +104,6 @@ class Petsc3dAmgTest int nx = mesh->GlobalNx, ny = mesh->GlobalNy, nz = mesh->GlobalNz; static_cast(bout::globals::mesh) ->setGridDataSource(new GridFromOptions(Options::getRoot())); - bout::globals::mesh->getCoordinates()->geometry(); f3.allocate(); coef2.allocate(); coef3.allocate(); From cf6df7352650aa25e3cf747a9773385e97d20075 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 13 Mar 2024 16:30:48 +0000 Subject: [PATCH 347/491] Set death_test_style to "threadsafe", to prevent timeout. --- tests/unit/bout_test_main.cxx | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/unit/bout_test_main.cxx b/tests/unit/bout_test_main.cxx index 999b5b0dc3..9ccb0b3918 100644 --- a/tests/unit/bout_test_main.cxx +++ b/tests/unit/bout_test_main.cxx @@ -21,6 +21,7 @@ GTEST_API_ int main(int argc, char** argv) { printf("Running main() from bout_test_main.cxx\n"); testing::InitGoogleTest(&argc, argv); + GTEST_FLAG_SET(death_test_style, "threadsafe"); // Explicitly setup and teardown PETSc to avoid reentry problems // with certain MPI implementations (see #1916 for details) From 3cddd467d6f68feb1c7b3000e0c43dc570f15da6 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Thu, 14 Mar 2024 10:31:54 +0000 Subject: [PATCH 348/491] Add [[maybe_unused]] attribute to 'J' parameter in Coordinates constructor. --- include/bout/coordinates.hxx | 14 +++++++------- src/mesh/coordinates.cxx | 16 ++++++++-------- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index 4f8e4f4207..a78ef834f9 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -64,13 +64,13 @@ public: /// A constructor useful for testing purposes. To use it, inherit /// from Coordinates. If \p calculate_geometry is true (default), /// calculate the non-uniform variables, Christoffel symbols - Coordinates(Mesh* mesh, FieldMetric dx, FieldMetric dy, FieldMetric dz, FieldMetric J, - FieldMetric Bxy, const FieldMetric& g11, const FieldMetric& g22, - const FieldMetric& g33, const FieldMetric& g12, const FieldMetric& g13, - const FieldMetric& g23, const FieldMetric& g_11, const FieldMetric& g_22, - const FieldMetric& g_33, const FieldMetric& g_12, const FieldMetric& g_13, - const FieldMetric& g_23, FieldMetric ShiftTorsion, - FieldMetric IntShiftTorsion); + Coordinates(Mesh* mesh, FieldMetric dx, FieldMetric dy, FieldMetric dz, + [[maybe_unused]] const FieldMetric& J, FieldMetric Bxy, + const FieldMetric& g11, const FieldMetric& g22, const FieldMetric& g33, + const FieldMetric& g12, const FieldMetric& g13, const FieldMetric& g23, + const FieldMetric& g_11, const FieldMetric& g_22, const FieldMetric& g_33, + const FieldMetric& g_12, const FieldMetric& g_13, const FieldMetric& g_23, + FieldMetric ShiftTorsion, FieldMetric IntShiftTorsion); /// Add variables to \p output_options, for post-processing void outputVars(Options& output_options); diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index bf2f19f15a..fabc997abf 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -286,14 +286,14 @@ Coordinates::FieldMetric Coordinates::getUnaligned(const std::string& name, } Coordinates::Coordinates(Mesh* mesh, FieldMetric dx, FieldMetric dy, FieldMetric dz, - FieldMetric J, FieldMetric Bxy, const FieldMetric& g11, - const FieldMetric& g22, const FieldMetric& g33, - const FieldMetric& g12, const FieldMetric& g13, - const FieldMetric& g23, const FieldMetric& g_11, - const FieldMetric& g_22, const FieldMetric& g_33, - const FieldMetric& g_12, const FieldMetric& g_13, - const FieldMetric& g_23, FieldMetric ShiftTorsion, - FieldMetric IntShiftTorsion) + [[maybe_unused]] const FieldMetric& J, FieldMetric Bxy, + const FieldMetric& g11, const FieldMetric& g22, + const FieldMetric& g33, const FieldMetric& g12, + const FieldMetric& g13, const FieldMetric& g23, + const FieldMetric& g_11, const FieldMetric& g_22, + const FieldMetric& g_33, const FieldMetric& g_12, + const FieldMetric& g_13, const FieldMetric& g_23, + FieldMetric ShiftTorsion, FieldMetric IntShiftTorsion) : nz(mesh->LocalNz), localmesh(mesh), location(CELL_CENTRE), dx_(std::move(dx)), dy_(std::move(dy)), dz_(std::move(dz)), ShiftTorsion_(std::move(ShiftTorsion)), IntShiftTorsion_(std::move(IntShiftTorsion)), From 99751c6e6147ebb4a577d0621028cc8510d5c3e5 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Thu, 14 Mar 2024 10:51:08 +0000 Subject: [PATCH 349/491] Revert expansion of COPY_STRIPE macro. --- src/mesh/coordinates_accessor.cxx | 36 ++++++------------------------- 1 file changed, 7 insertions(+), 29 deletions(-) diff --git a/src/mesh/coordinates_accessor.cxx b/src/mesh/coordinates_accessor.cxx index 9872caeea7..c8c2fdff66 100644 --- a/src/mesh/coordinates_accessor.cxx +++ b/src/mesh/coordinates_accessor.cxx @@ -41,7 +41,7 @@ CoordinatesAccessor::CoordinatesAccessor(const Coordinates* coords) { // Copy data from Coordinates variable into data array // Uses the symbol to look up the corresponding Offset #define COPY_STRIPE1(symbol) \ - data[stripe_size * ind.ind + static_cast(Offset::symbol)] = coords->symbol[ind]; + data[stripe_size * ind.ind + static_cast(Offset::symbol)] = coords->symbol()[ind]; // Implement copy for each argument #define COPY_STRIPE(...) \ @@ -50,39 +50,17 @@ CoordinatesAccessor::CoordinatesAccessor(const Coordinates* coords) { // Iterate over all points in the field // Note this could be 2D or 3D, depending on FieldMetric type for (const auto& ind : coords->dx().getRegion("RGN_ALL")) { - data[stripe_size * ind.ind + static_cast(Offset::dx)] = coords->dx()[ind]; - data[stripe_size * ind.ind + static_cast(Offset::dy)] = coords->dy()[ind]; - data[stripe_size * ind.ind + static_cast(Offset::dz)] = coords->dz()[ind]; - - data[stripe_size * ind.ind + static_cast(Offset::d1_dx)] = coords->d1_dx()[ind]; - data[stripe_size * ind.ind + static_cast(Offset::d1_dy)] = coords->d1_dy()[ind]; - data[stripe_size * ind.ind + static_cast(Offset::d1_dz)] = coords->d1_dz()[ind]; - - data[stripe_size * ind.ind + static_cast(Offset::J)] = coords->J()[ind]; - + COPY_STRIPE(dx, dy, dz); + COPY_STRIPE(d1_dx, d1_dy, d1_dz); + COPY_STRIPE(J); data[stripe_size * ind.ind + static_cast(Offset::B)] = coords->Bxy()[ind]; data[stripe_size * ind.ind + static_cast(Offset::Byup)] = coords->Bxy().yup()[ind]; data[stripe_size * ind.ind + static_cast(Offset::Bydown)] = coords->Bxy().ydown()[ind]; - - data[stripe_size * ind.ind + static_cast(Offset::G1)] = coords->G1()[ind]; - data[stripe_size * ind.ind + static_cast(Offset::G3)] = coords->G3()[ind]; - // COPY_STRIPE(g11, g12, g13, g22, g23, g33); - data[stripe_size * ind.ind + static_cast(Offset::g11)] = coords->g11()[ind]; - data[stripe_size * ind.ind + static_cast(Offset::g12)] = coords->g12()[ind]; - data[stripe_size * ind.ind + static_cast(Offset::g13)] = coords->g13()[ind]; - data[stripe_size * ind.ind + static_cast(Offset::g22)] = coords->g22()[ind]; - data[stripe_size * ind.ind + static_cast(Offset::g23)] = coords->g23()[ind]; - data[stripe_size * ind.ind + static_cast(Offset::g33)] = coords->g33()[ind]; - - // COPY_STRIPE(g_11, g_12, g_13, g_22, g_23, g_33); - data[stripe_size * ind.ind + static_cast(Offset::g_11)] = coords->g_11()[ind]; - data[stripe_size * ind.ind + static_cast(Offset::g_12)] = coords->g_12()[ind]; - data[stripe_size * ind.ind + static_cast(Offset::g_13)] = coords->g_13()[ind]; - data[stripe_size * ind.ind + static_cast(Offset::g_22)] = coords->g_22()[ind]; - data[stripe_size * ind.ind + static_cast(Offset::g_23)] = coords->g_23()[ind]; - data[stripe_size * ind.ind + static_cast(Offset::g_33)] = coords->g_33()[ind]; + COPY_STRIPE(G1, G3); + COPY_STRIPE(g11, g12, g13, g22, g23, g33); + COPY_STRIPE(g_11, g_12, g_13, g_22, g_23, g_33); } } From 5d0a6960651cc52bb7c20590c4ab832fd181d6f3 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Thu, 14 Mar 2024 11:08:22 +0000 Subject: [PATCH 350/491] Remove redundant function 'ChristoffelSymbols::map' (instead just calling ChristoffelSymbols::applyToComponents() directly). --- include/bout/christoffel_symbols.hxx | 5 +---- src/mesh/christoffel_symbols.cxx | 24 +++--------------------- src/mesh/coordinates.cxx | 2 +- 3 files changed, 5 insertions(+), 26 deletions(-) diff --git a/include/bout/christoffel_symbols.hxx b/include/bout/christoffel_symbols.hxx index 4dc5f4474b..82eeba9424 100644 --- a/include/bout/christoffel_symbols.hxx +++ b/include/bout/christoffel_symbols.hxx @@ -81,15 +81,12 @@ public: // void setLocation(CELL_LOC location); // Transforms the ChristoffelSymbols by applying the given function to every element - void map(const std::function& function); + void applyToComponents(const std::function& function); private: FieldMetric G1_11_, G1_22_, G1_33_, G1_12_, G1_13_, G1_23_; FieldMetric G2_11_, G2_22_, G2_33_, G2_12_, G2_13_, G2_23_; FieldMetric G3_11_, G3_22_, G3_33_, G3_12_, G3_13_, G3_23_; - - ChristoffelSymbols applyToComponents( - const std::function& function) const; }; #endif //BOUT_CHRISTOFFELSYMBOLS_HXX diff --git a/src/mesh/christoffel_symbols.cxx b/src/mesh/christoffel_symbols.cxx index a7084310bb..9e150a161e 100644 --- a/src/mesh/christoffel_symbols.cxx +++ b/src/mesh/christoffel_symbols.cxx @@ -120,26 +120,9 @@ ChristoffelSymbols::ChristoffelSymbols(const Coordinates& coordinates) { + 0.5 * g33 * coordinates.DDY(g_33); } -void ChristoffelSymbols::map( +void ChristoffelSymbols::applyToComponents( const std::function& function) { - const ChristoffelSymbols updated_christoffel_symbols = applyToComponents(function); - - setChristoffelSymbols( - updated_christoffel_symbols.G1_11_, updated_christoffel_symbols.G1_22_, - updated_christoffel_symbols.G1_33_, updated_christoffel_symbols.G1_12_, - updated_christoffel_symbols.G1_13_, updated_christoffel_symbols.G1_23_, - updated_christoffel_symbols.G2_11_, updated_christoffel_symbols.G2_22_, - updated_christoffel_symbols.G2_33_, updated_christoffel_symbols.G2_12_, - updated_christoffel_symbols.G2_13_, updated_christoffel_symbols.G2_23_, - updated_christoffel_symbols.G3_11_, updated_christoffel_symbols.G3_22_, - updated_christoffel_symbols.G3_33_, updated_christoffel_symbols.G3_12_, - updated_christoffel_symbols.G3_13_, updated_christoffel_symbols.G3_23_); -} - -ChristoffelSymbols ChristoffelSymbols::applyToComponents( - const std::function& function) const { - const auto components_in = std::vector{ G1_11_, G1_22_, G1_33_, G1_12_, G1_13_, G1_23_, G2_11_, G2_22_, G2_33_, G2_12_, G2_13_, G2_23_, G3_11_, G3_22_, G3_33_, G3_12_, G3_13_, G3_23_}; @@ -150,7 +133,6 @@ ChristoffelSymbols ChristoffelSymbols::applyToComponents( auto [G1_11, G1_22, G1_33, G1_12, G1_13, G1_23, G2_11, G2_22, G2_33, G2_12, G2_13, G2_23, G3_11, G3_22, G3_33, G3_12, G3_13, G3_23] = components_out; - return ChristoffelSymbols(G1_11, G1_22, G1_33, G1_12, G1_13, G1_23, G2_11, G2_22, G2_33, - G2_12, G2_13, G2_23, G3_11, G3_22, G3_33, G3_12, G3_13, - G3_23); + setChristoffelSymbols(G1_11, G1_22, G1_33, G1_12, G1_13, G1_23, G2_11, G2_22, G2_33, + G2_12, G2_13, G2_23, G3_11, G3_22, G3_33, G3_12, G3_13, G3_23); } diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index fabc997abf..6916e8003b 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -1398,7 +1398,7 @@ void Coordinates::setBxy(FieldMetric Bxy) { void Coordinates::applyToChristoffelSymbols( const std::function& function) const { - christoffel_symbols().map(function); + christoffel_symbols().applyToComponents(function); } void Coordinates::setContravariantMetricTensor( From 15815f408cae012370fb655641111689921eceb9 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Thu, 14 Mar 2024 11:38:32 +0000 Subject: [PATCH 351/491] Revert "Minor fixes." This reverts commit 5d2a9dd10f75601382c85d85eef2d73e419182b1. (Return value could be either Field2D or Field3D). --- include/bout/coordinates.hxx | 42 ++++++++++++++++++------------------ src/mesh/coordinates.cxx | 35 ++++++++++++++++-------------- 2 files changed, 40 insertions(+), 37 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index a78ef834f9..fcd9d55cf8 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -180,17 +180,17 @@ public: // Operators /////////////////////////////////////////////////////////// - Field2D DDX(const Field2D& f, CELL_LOC outloc = CELL_DEFAULT, - const std::string& method = "DEFAULT", - const std::string& region = "RGN_NOBNDRY") const; + FieldMetric DDX(const Field2D& f, CELL_LOC outloc = CELL_DEFAULT, + const std::string& method = "DEFAULT", + const std::string& region = "RGN_NOBNDRY") const; - Field2D DDY(const Field2D& f, CELL_LOC outloc = CELL_DEFAULT, - const std::string& method = "DEFAULT", - const std::string& region = "RGN_NOBNDRY") const; + FieldMetric DDY(const Field2D& f, CELL_LOC outloc = CELL_DEFAULT, + const std::string& method = "DEFAULT", + const std::string& region = "RGN_NOBNDRY") const; - Field2D DDZ(const Field2D& f, CELL_LOC outloc = CELL_DEFAULT, - const std::string& method = "DEFAULT", - const std::string& region = "RGN_NOBNDRY") const; + FieldMetric DDZ(const Field2D& f, CELL_LOC outloc = CELL_DEFAULT, + const std::string& method = "DEFAULT", + const std::string& region = "RGN_NOBNDRY") const; Field3D DDX(const Field3D& f, CELL_LOC outloc = CELL_DEFAULT, const std::string& method = "DEFAULT", @@ -212,43 +212,43 @@ public: const std::string& method = "DEFAULT"); /// Advection along magnetic field V*b.Grad(f) - Field2D Vpar_Grad_par(const Field2D& v, const Field2D& f, - CELL_LOC outloc = CELL_DEFAULT, - const std::string& method = "DEFAULT"); + FieldMetric Vpar_Grad_par(const Field2D& v, const Field2D& f, + CELL_LOC outloc = CELL_DEFAULT, + const std::string& method = "DEFAULT"); Field3D Vpar_Grad_par(const Field3D& v, const Field3D& f, CELL_LOC outloc = CELL_DEFAULT, const std::string& method = "DEFAULT"); /// Divergence along magnetic field Div(b*f) = B.Grad(f/B) - Field2D Div_par(const Field2D& f, CELL_LOC outloc = CELL_DEFAULT, - const std::string& method = "DEFAULT"); + FieldMetric Div_par(const Field2D& f, CELL_LOC outloc = CELL_DEFAULT, + const std::string& method = "DEFAULT"); Field3D Div_par(const Field3D& f, CELL_LOC outloc = CELL_DEFAULT, const std::string& method = "DEFAULT"); // Second derivative along magnetic field - Field2D Grad2_par2(const Field2D& f, CELL_LOC outloc = CELL_DEFAULT, - const std::string& method = "DEFAULT"); + FieldMetric Grad2_par2(const Field2D& f, CELL_LOC outloc = CELL_DEFAULT, + const std::string& method = "DEFAULT"); Field3D Grad2_par2(const Field3D& f, CELL_LOC outloc = CELL_DEFAULT, const std::string& method = "DEFAULT"); // Perpendicular Laplacian operator, using only X-Z derivatives // NOTE: This might be better bundled with the Laplacian inversion code // since it makes use of the same coefficients and FFT routines - Field2D Delp2(const Field2D& f, CELL_LOC outloc = CELL_DEFAULT, bool useFFT = true); + FieldMetric Delp2(const Field2D& f, CELL_LOC outloc = CELL_DEFAULT, bool useFFT = true); Field3D Delp2(const Field3D& f, CELL_LOC outloc = CELL_DEFAULT, bool useFFT = true); FieldPerp Delp2(const FieldPerp& f, CELL_LOC outloc = CELL_DEFAULT, bool useFFT = true); // Full parallel Laplacian operator on scalar field // Laplace_par(f) = Div( b (b dot Grad(f)) ) - Field2D Laplace_par(const Field2D& f, CELL_LOC outloc = CELL_DEFAULT); + FieldMetric Laplace_par(const Field2D& f, CELL_LOC outloc = CELL_DEFAULT); Field3D Laplace_par(const Field3D& f, CELL_LOC outloc = CELL_DEFAULT); // Full Laplacian operator on scalar field - Field2D Laplace(const Field2D& f, CELL_LOC outloc = CELL_DEFAULT, - const std::string& dfdy_boundary_conditions = "free_o3", - const std::string& dfdy_dy_region = ""); + FieldMetric Laplace(const Field2D& f, CELL_LOC outloc = CELL_DEFAULT, + const std::string& dfdy_boundary_conditions = "free_o3", + const std::string& dfdy_dy_region = ""); Field3D Laplace(const Field3D& f, CELL_LOC outloc = CELL_DEFAULT, const std::string& dfdy_boundary_conditions = "free_o3", const std::string& dfdy_dy_region = ""); diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 6916e8003b..43c6c380b1 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -905,8 +905,9 @@ void Coordinates::setParallelTransform(Options* mesh_options) { * *******************************************************************************/ -Field2D Coordinates::DDX(const Field2D& f, CELL_LOC loc, const std::string& method, - const std::string& region) const { +Coordinates::FieldMetric Coordinates::DDX(const Field2D& f, CELL_LOC loc, + const std::string& method, + const std::string& region) const { ASSERT1(location == loc || loc == CELL_DEFAULT); return bout::derivatives::index::DDX(f, loc, method, region) / dx(); } @@ -924,8 +925,9 @@ Field3D Coordinates::DDX(const Field3D& f, CELL_LOC outloc, const std::string& m return result; } -Field2D Coordinates::DDY(const Field2D& f, CELL_LOC loc, const std::string& method, - const std::string& region) const { +Coordinates::FieldMetric Coordinates::DDY(const Field2D& f, CELL_LOC loc, + const std::string& method, + const std::string& region) const { ASSERT1(location == loc || loc == CELL_DEFAULT); return bout::derivatives::index::DDY(f, loc, method, region) / dy(); } @@ -943,9 +945,9 @@ Field3D Coordinates::DDY(const Field3D& f, CELL_LOC outloc, const std::string& m return bout::derivatives::index::DDY(f, outloc, method, region) / dy(); }; -Field2D Coordinates::DDZ(const Field2D& f, CELL_LOC loc, - const std::string& UNUSED(method), - const std::string& UNUSED(region)) const { +Coordinates::FieldMetric Coordinates::DDZ(const Field2D& f, CELL_LOC loc, + const std::string& UNUSED(method), + const std::string& UNUSED(region)) const { ASSERT1(location == loc || loc == CELL_DEFAULT); ASSERT1(f.getMesh() == localmesh); if (loc == CELL_DEFAULT) { @@ -1000,8 +1002,8 @@ Field3D Coordinates::Vpar_Grad_par(const Field3D& v, const Field3D& f, CELL_LOC ///////////////////////////////////////////////////////// // Parallel divergence -Field2D Coordinates::Div_par(const Field2D& f, CELL_LOC outloc, - const std::string& method) { +Coordinates::FieldMetric Coordinates::Div_par(const Field2D& f, CELL_LOC outloc, + const std::string& method) { TRACE("Coordinates::Div_par( Field2D )"); ASSERT1(location == outloc || outloc == CELL_DEFAULT); @@ -1041,8 +1043,8 @@ Field3D Coordinates::Div_par(const Field3D& f, CELL_LOC outloc, // second parallel derivative (b dot Grad)(b dot Grad) // Note: For parallel Laplacian use Laplace_par -Field2D Coordinates::Grad2_par2(const Field2D& f, CELL_LOC outloc, - const std::string& method) { +Coordinates::FieldMetric Coordinates::Grad2_par2(const Field2D& f, CELL_LOC outloc, + const std::string& method) { TRACE("Coordinates::Grad2_par2( Field2D )"); ASSERT1(location == outloc || (outloc == CELL_DEFAULT && location == f.getLocation())); @@ -1076,7 +1078,8 @@ Field3D Coordinates::Grad2_par2(const Field3D& f, CELL_LOC outloc, #include // Delp2 uses same coefficients as inversion code -Field2D Coordinates::Delp2(const Field2D& f, CELL_LOC outloc, bool UNUSED(useFFT)) { +Coordinates::FieldMetric Coordinates::Delp2(const Field2D& f, CELL_LOC outloc, + bool UNUSED(useFFT)) { TRACE("Coordinates::Delp2( Field2D )"); ASSERT1(location == outloc || outloc == CELL_DEFAULT); @@ -1213,7 +1216,7 @@ FieldPerp Coordinates::Delp2(const FieldPerp& f, CELL_LOC outloc, bool useFFT) { return result; } -Field2D Coordinates::Laplace_par(const Field2D& f, CELL_LOC outloc) { +Coordinates::FieldMetric Coordinates::Laplace_par(const Field2D& f, CELL_LOC outloc) { ASSERT1(location == outloc || outloc == CELL_DEFAULT); return D2DY2(f, outloc) / g_22() + DDY(J() / g_22(), outloc) * DDY(f, outloc) / J(); @@ -1226,9 +1229,9 @@ Field3D Coordinates::Laplace_par(const Field3D& f, CELL_LOC outloc) { // Full Laplacian operator on scalar field -Field2D Coordinates::Laplace(const Field2D& f, CELL_LOC outloc, - const std::string& dfdy_boundary_conditions, - const std::string& dfdy_dy_region) { +Coordinates::FieldMetric Coordinates::Laplace(const Field2D& f, CELL_LOC outloc, + const std::string& dfdy_boundary_conditions, + const std::string& dfdy_dy_region) { TRACE("Coordinates::Laplace( Field2D )"); ASSERT1(location == outloc || outloc == CELL_DEFAULT); From b8a2158beabb206b6b3cb356aad9c2fb507797ce Mon Sep 17 00:00:00 2001 From: tomc271 Date: Thu, 14 Mar 2024 12:42:41 +0000 Subject: [PATCH 352/491] Apply clang-format changes --- examples/2Dturbulence_multigrid/esel.cxx | 3 +- examples/6field-simple/elm_6f.cxx | 6 ++-- examples/conducting-wall-mode/cwm.cxx | 6 ++-- examples/constraints/alfven-wave/alfven.cxx | 6 ++-- examples/dalf3/dalf3.cxx | 6 ++-- .../elm-pb-outerloop/elm_pb_outerloop.cxx | 16 ++++++---- examples/elm-pb/elm_pb.cxx | 6 ++-- examples/em-drift/2fluid.cxx | 6 ++-- examples/gravity_reduced/gravity_reduced.cxx | 4 +-- examples/gyro-gem/gem.cxx | 6 ++-- examples/jorek-compare/jorek_compare.cxx | 6 ++-- examples/lapd-drift/lapd_drift.cxx | 6 ++-- examples/laplacexy/alfven-wave/alfven.cxx | 6 ++-- examples/laplacexy/laplace_perp/test.cxx | 6 ++-- examples/reconnect-2field/2field.cxx | 6 ++-- examples/shear-alfven-wave/2fluid.cxx | 6 ++-- examples/tokamak-2fluid/2fluid.cxx | 6 ++-- examples/uedge-benchmark/ue_bmark.cxx | 6 ++-- examples/wave-slab/wave_slab.cxx | 6 ++-- include/bout/christoffel_symbols.hxx | 3 +- src/field/vector3d.cxx | 6 ++-- .../laplace/impls/hypre3d/hypre3d_laplace.cxx | 12 +++---- .../impls/multigrid/multigrid_laplace.cxx | 3 +- .../laplace/impls/petsc/petsc_laplace.cxx | 12 ++++--- .../laplace/impls/petsc3damg/petsc3damg.cxx | 32 +++++++++++-------- src/invert/laplacexy2/laplacexy2.cxx | 8 ++--- src/invert/laplacexy2/laplacexy2_hypre.cxx | 8 ++--- .../laplacexz/impls/petsc/laplacexz-petsc.cxx | 24 +++++++++----- src/mesh/boundary_standard.cxx | 6 ++-- src/mesh/metricTensor.cxx | 4 +-- tests/MMS/GBS/gbs.cxx | 6 ++-- tests/MMS/elm-pb/elm_pb.cxx | 6 ++-- tests/MMS/fieldalign/fieldalign.cxx | 6 ++-- tests/MMS/spatial/diffusion/diffusion.cxx | 3 +- tests/MMS/tokamak/tokamak.cxx | 6 ++-- .../test-drift-instability/2fluid.cxx | 6 ++-- .../test-interchange-instability/2fluid.cxx | 6 ++-- .../integrated/test-laplacexy/loadmetric.cxx | 6 ++-- .../test-laplacexy/test-laplacexy.cxx | 2 +- 39 files changed, 175 insertions(+), 109 deletions(-) diff --git a/examples/2Dturbulence_multigrid/esel.cxx b/examples/2Dturbulence_multigrid/esel.cxx index 02a8b68bc1..4cccfbad38 100644 --- a/examples/2Dturbulence_multigrid/esel.cxx +++ b/examples/2Dturbulence_multigrid/esel.cxx @@ -71,7 +71,8 @@ class ESEL : public PhysicsModel { // generate coordinate system coord->setBxy(1); - coord->setContravariantMetricTensor(ContravariantMetricTensor(1.0, 1.0, 1.0, 0.0, 0.0, 0.0)); + coord->setContravariantMetricTensor( + ContravariantMetricTensor(1.0, 1.0, 1.0, 0.0, 0.0, 0.0)); coord->setCovariantMetricTensor(CovariantMetricTensor(1.0, 1.0, 1.0, 0.0, 0.0, 0.0)); SOLVE_FOR(N, vort); diff --git a/examples/6field-simple/elm_6f.cxx b/examples/6field-simple/elm_6f.cxx index a6babe531f..82cf7d9541 100644 --- a/examples/6field-simple/elm_6f.cxx +++ b/examples/6field-simple/elm_6f.cxx @@ -1058,7 +1058,8 @@ class Elm_6f : public PhysicsModel { g12 = 0.0; g13 = -I * coord->g11(); g23 = -Btxy / (hthe * Bpxy * Rxy); - coord->setContravariantMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); + coord->setContravariantMetricTensor( + ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); coord->setJ(hthe / Bpxy); coord->setBxy(B0); @@ -1070,7 +1071,8 @@ class Elm_6f : public PhysicsModel { g_12 = Btxy * hthe * I * Rxy / Bpxy; g_13 = I * Rxy * Rxy; g_23 = Btxy * hthe * Rxy / Bpxy; - coord->setCovariantMetricTensor(CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + coord->setCovariantMetricTensor( + CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); // Set B field vector diff --git a/examples/conducting-wall-mode/cwm.cxx b/examples/conducting-wall-mode/cwm.cxx index c0190fecbb..cf9255ed1c 100644 --- a/examples/conducting-wall-mode/cwm.cxx +++ b/examples/conducting-wall-mode/cwm.cxx @@ -195,7 +195,8 @@ class CWM : public PhysicsModel { g12 = 0.0; g13 = -I * coord->g11(); g23 = -Btxy / (hthe * Bpxy * Rxy); - coord->setContravariantMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); + coord->setContravariantMetricTensor( + ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); coord->setJ(hthe / Bpxy); @@ -206,7 +207,8 @@ class CWM : public PhysicsModel { g_12 = Btxy * hthe * I * Rxy / Bpxy; g_13 = I * Rxy * Rxy; g_23 = Btxy * hthe * Rxy / Bpxy; - coord->setCovariantMetricTensor(CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + coord->setCovariantMetricTensor( + CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); /**************** SET EVOLVING VARIABLES *************/ diff --git a/examples/constraints/alfven-wave/alfven.cxx b/examples/constraints/alfven-wave/alfven.cxx index a159e7d350..bb3861baaf 100644 --- a/examples/constraints/alfven-wave/alfven.cxx +++ b/examples/constraints/alfven-wave/alfven.cxx @@ -208,7 +208,8 @@ class Alfven : public PhysicsModel { g12 = 0.0; g13 = -sinty * coord->g11(); g23 = -sbp * Btxy / (hthe * Bpxy * Rxy); - coord->setContravariantMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); + coord->setContravariantMetricTensor( + ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); coord->setJ(hthe / Bpxy); @@ -219,7 +220,8 @@ class Alfven : public PhysicsModel { g_12 = sbp * Btxy * hthe * sinty * Rxy / Bpxy; g_13 = sinty * Rxy * Rxy; g_23 = sbp * Btxy * hthe * Rxy / Bpxy; - coord->setCovariantMetricTensor(CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + coord->setCovariantMetricTensor( + CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); } }; diff --git a/examples/dalf3/dalf3.cxx b/examples/dalf3/dalf3.cxx index dc5de9bbdd..cee4164881 100644 --- a/examples/dalf3/dalf3.cxx +++ b/examples/dalf3/dalf3.cxx @@ -260,7 +260,8 @@ class DALF3 : public PhysicsModel { g12 = 0.0; g13 = -I * coord->g11(); g23 = -Btxy / (hthe * Bpxy * Rxy); - coord->setContravariantMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); + coord->setContravariantMetricTensor( + ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); coord->setJ(hthe / Bpxy); coord->setBxy(B0); @@ -272,7 +273,8 @@ class DALF3 : public PhysicsModel { g_12 = Btxy * hthe * I * Rxy / Bpxy; g_13 = I * Rxy * Rxy; g_23 = Btxy * hthe * Rxy / Bpxy; - coord->setCovariantMetricTensor(CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + coord->setCovariantMetricTensor( + CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); SOLVE_FOR3(Vort, Pe, Vpar); comms.add(Vort, Pe, Vpar); diff --git a/examples/elm-pb-outerloop/elm_pb_outerloop.cxx b/examples/elm-pb-outerloop/elm_pb_outerloop.cxx index 4c87b45aa2..e7936a3040 100644 --- a/examples/elm-pb-outerloop/elm_pb_outerloop.cxx +++ b/examples/elm-pb-outerloop/elm_pb_outerloop.cxx @@ -1099,7 +1099,8 @@ class ELMpb : public PhysicsModel { g12 = 0.0; g13 = -I * metric->g11(); g23 = -Btxy / (hthe * Bpxy * Rxy); - metric->setContravariantMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); + metric->setContravariantMetricTensor( + ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); metric->setJ(hthe / Bpxy); metric->setBxy(B0); @@ -1111,7 +1112,8 @@ class ELMpb : public PhysicsModel { g_12 = Btxy * hthe * I * Rxy / Bpxy; g_13 = I * Rxy * Rxy; g_23 = Btxy * hthe * Rxy / Bpxy; - metric->setCovariantMetricTensor(CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + metric->setCovariantMetricTensor( + CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); // Set B field vector @@ -1682,12 +1684,12 @@ class ELMpb : public PhysicsModel { #endif }; - // Terms which are not yet single index operators - // Note: Terms which are included in the single index loop - // may be commented out here, to allow comparison/testing + // Terms which are not yet single index operators + // Note: Terms which are included in the single index loop + // may be commented out here, to allow comparison/testing - //////////////////////////////////////////////////// - // Parallel electric field + //////////////////////////////////////////////////// + // Parallel electric field #if not EVOLVE_JPAR // Vector potential diff --git a/examples/elm-pb/elm_pb.cxx b/examples/elm-pb/elm_pb.cxx index c65b9dc7b4..5f773d54b8 100644 --- a/examples/elm-pb/elm_pb.cxx +++ b/examples/elm-pb/elm_pb.cxx @@ -1040,7 +1040,8 @@ class ELMpb : public PhysicsModel { g12 = 0.0; g13 = -I * metric->g11(); g23 = -Btxy / (hthe * Bpxy * Rxy); - metric->setContravariantMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); + metric->setContravariantMetricTensor( + ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); metric->setJ(hthe / Bpxy); metric->setBxy(B0); @@ -1052,7 +1053,8 @@ class ELMpb : public PhysicsModel { g_12 = Btxy * hthe * I * Rxy / Bpxy; g_13 = I * Rxy * Rxy; g_23 = Btxy * hthe * Rxy / Bpxy; - metric->setCovariantMetricTensor(CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + metric->setCovariantMetricTensor( + CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); // Set B field vector diff --git a/examples/em-drift/2fluid.cxx b/examples/em-drift/2fluid.cxx index 4bd7703608..9bd53d61d9 100644 --- a/examples/em-drift/2fluid.cxx +++ b/examples/em-drift/2fluid.cxx @@ -185,7 +185,8 @@ class EMdrift : public PhysicsModel { g12 = 0.0; g13 = -I * coord->g11(); g23 = -Btxy / (hthe * Bpxy * Rxy); - coord->setContravariantMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); + coord->setContravariantMetricTensor( + ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); coord->setJ(hthe / Bpxy); @@ -196,7 +197,8 @@ class EMdrift : public PhysicsModel { g_12 = Btxy * hthe * I * Rxy / Bpxy; g_13 = I * Rxy * Rxy; g_23 = Btxy * hthe * Rxy / Bpxy; - coord->setCovariantMetricTensor(CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + coord->setCovariantMetricTensor( + CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); /**************** SET EVOLVING VARIABLES *************/ diff --git a/examples/gravity_reduced/gravity_reduced.cxx b/examples/gravity_reduced/gravity_reduced.cxx index 83021a4b56..2085a02ac6 100644 --- a/examples/gravity_reduced/gravity_reduced.cxx +++ b/examples/gravity_reduced/gravity_reduced.cxx @@ -230,8 +230,8 @@ class GravityReduced : public PhysicsModel { ddt(rho) -= (rho0 / (1 + Gamma * p0 * mu_0 / SQ(B0))) * ((rho0 * mu_0 / SQ(B0)) * bracket(G, phi, bm) * coord->Bxy() - + Grad_par(Vpar, CELL_CENTRE) - bracket(Psi, Vpar, bm) * coord->Bxy() - - (Vpar / B0) * Grad_par(B0)); + + Grad_par(Vpar, CELL_CENTRE) - bracket(Psi, Vpar, bm) * coord->Bxy() + - (Vpar / B0) * Grad_par(B0)); if (nonlinear) { ddt(rho) -= bracket(phi, rho, bm) * coord->Bxy(); diff --git a/examples/gyro-gem/gem.cxx b/examples/gyro-gem/gem.cxx index 20c1d180dd..9c3e845eb3 100644 --- a/examples/gyro-gem/gem.cxx +++ b/examples/gyro-gem/gem.cxx @@ -376,7 +376,8 @@ class GEM : public PhysicsModel { g12 = 0.0; g13 = 0.; g23 = -Btxy / (hthe * Bpxy * Rxy); - coord->setContravariantMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); + coord->setContravariantMetricTensor( + ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); coord->setJ(hthe / Bpxy); coord->setBxy(Bxy); @@ -388,7 +389,8 @@ class GEM : public PhysicsModel { g_12 = 0.; g_13 = 0.; g_23 = Btxy * hthe * Rxy / Bpxy; - coord->setCovariantMetricTensor(CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + coord->setCovariantMetricTensor( + CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); // Set B field vector diff --git a/examples/jorek-compare/jorek_compare.cxx b/examples/jorek-compare/jorek_compare.cxx index d15df9ef72..256265ca8c 100644 --- a/examples/jorek-compare/jorek_compare.cxx +++ b/examples/jorek-compare/jorek_compare.cxx @@ -306,7 +306,8 @@ class Jorek : public PhysicsModel { g12 = 0.0; g13 = -I * coord->g11(); g23 = -Btxy / (hthe * Bpxy * Rxy); - coord->setContravariantMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); + coord->setContravariantMetricTensor( + ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); coord->setJ(hthe / Bpxy); coord->setBxy(B0); @@ -318,7 +319,8 @@ class Jorek : public PhysicsModel { g_12 = Btxy * hthe * I * Rxy / Bpxy; g_13 = I * Rxy * Rxy; g_23 = Btxy * hthe * Rxy / Bpxy; - coord->setCovariantMetricTensor(CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + coord->setCovariantMetricTensor( + CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); // Set B field vector B0vec.covariant = false; diff --git a/examples/lapd-drift/lapd_drift.cxx b/examples/lapd-drift/lapd_drift.cxx index 0ea9b85f8f..e112551ae3 100644 --- a/examples/lapd-drift/lapd_drift.cxx +++ b/examples/lapd-drift/lapd_drift.cxx @@ -339,7 +339,8 @@ class LAPDdrift : public PhysicsModel { g12 = 0.0; g13 = -I * coord->g11(); g23 = -Btxy / (hthe * Bpxy * Rxy); - coord->setContravariantMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); + coord->setContravariantMetricTensor( + ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); coord->setJ(hthe / Bpxy); @@ -350,7 +351,8 @@ class LAPDdrift : public PhysicsModel { g_12 = Btxy * hthe * I * Rxy / Bpxy; g_13 = I * Rxy * Rxy; g_23 = Btxy * hthe * Rxy / Bpxy; - coord->setCovariantMetricTensor(CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + coord->setCovariantMetricTensor( + CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); rho0 = Ni0 * Delp2(phi0) + Perp_Grad_dot_Grad(phi0, Ni0); diff --git a/examples/laplacexy/alfven-wave/alfven.cxx b/examples/laplacexy/alfven-wave/alfven.cxx index a32dc0a045..ab8ccf72fa 100644 --- a/examples/laplacexy/alfven-wave/alfven.cxx +++ b/examples/laplacexy/alfven-wave/alfven.cxx @@ -209,7 +209,8 @@ class Alfven : public PhysicsModel { g12 = 0.0; g13 = -sinty * coord->g11(); g23 = -sbp * Btxy / (hthe * Bpxy * Rxy); - coord->setContravariantMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); + coord->setContravariantMetricTensor( + ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); coord->setJ(hthe / Bpxy); @@ -220,7 +221,8 @@ class Alfven : public PhysicsModel { g_12 = sbp * Btxy * hthe * sinty * Rxy / Bpxy; g_13 = sinty * Rxy * Rxy; g_23 = sbp * Btxy * hthe * Rxy / Bpxy; - coord->setCovariantMetricTensor(CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + coord->setCovariantMetricTensor( + CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); } }; diff --git a/examples/laplacexy/laplace_perp/test.cxx b/examples/laplacexy/laplace_perp/test.cxx index 23c040012c..fddced6cb5 100644 --- a/examples/laplacexy/laplace_perp/test.cxx +++ b/examples/laplacexy/laplace_perp/test.cxx @@ -32,7 +32,8 @@ int main(int argc, char** argv) { g12 = 0.0; g13 = -I * coord->g11(); g23 = -Btxy / (hthe * Bpxy * Rxy); - coord->setContravariantMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); + coord->setContravariantMetricTensor( + ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); coord->setJ(hthe / Bpxy); coord->setBxy(B0); @@ -44,7 +45,8 @@ int main(int argc, char** argv) { g_12 = Btxy * hthe * I * Rxy / Bpxy; g_13 = I * Rxy * Rxy; g_23 = Btxy * hthe * Rxy / Bpxy; - coord->setCovariantMetricTensor(CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + coord->setCovariantMetricTensor( + CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); } /////////////////////////////////////// diff --git a/examples/reconnect-2field/2field.cxx b/examples/reconnect-2field/2field.cxx index ccd5bc3b55..9f02b419e2 100644 --- a/examples/reconnect-2field/2field.cxx +++ b/examples/reconnect-2field/2field.cxx @@ -170,7 +170,8 @@ class TwoField : public PhysicsModel { g12 = 0.0; g13 = 0.; g23 = -Btxy / (hthe * Bpxy * Rxy); - coord->setContravariantMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); + coord->setContravariantMetricTensor( + ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); coord->setJ(hthe / Bpxy); @@ -181,7 +182,8 @@ class TwoField : public PhysicsModel { g_12 = 0.; g_13 = 0.; g_23 = Btxy * hthe * Rxy / Bpxy; - coord->setCovariantMetricTensor(CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + coord->setCovariantMetricTensor( + CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); // Tell BOUT++ which variables to evolve SOLVE_FOR(U, Apar); diff --git a/examples/shear-alfven-wave/2fluid.cxx b/examples/shear-alfven-wave/2fluid.cxx index c281ad6bc3..ce73d65361 100644 --- a/examples/shear-alfven-wave/2fluid.cxx +++ b/examples/shear-alfven-wave/2fluid.cxx @@ -189,7 +189,8 @@ class ShearAlfven : public PhysicsModel { g12 = 0.0; g13 = -I * coord->g11(); g23 = -Btxy / (hthe * Bpxy * Rxy); - coord->setContravariantMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); + coord->setContravariantMetricTensor( + ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); coord->setJ(hthe / Bpxy); @@ -200,7 +201,8 @@ class ShearAlfven : public PhysicsModel { g_12 = Btxy * hthe * I * Rxy / Bpxy; g_13 = I * Rxy * Rxy; g_23 = Btxy * hthe * Rxy / Bpxy; - coord->setCovariantMetricTensor(CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + coord->setCovariantMetricTensor( + CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); /**************** SET EVOLVING VARIABLES *************/ diff --git a/examples/tokamak-2fluid/2fluid.cxx b/examples/tokamak-2fluid/2fluid.cxx index 195efc8e7d..512ceee2bf 100644 --- a/examples/tokamak-2fluid/2fluid.cxx +++ b/examples/tokamak-2fluid/2fluid.cxx @@ -406,7 +406,8 @@ class TwoFluid : public PhysicsModel { g12 = 0.0; g13 = -I * coord->g11(); g23 = -Btxy / (hthe * Bpxy * Rxy); - coord->setContravariantMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); + coord->setContravariantMetricTensor( + ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); coord->setJ(hthe / Bpxy); @@ -417,7 +418,8 @@ class TwoFluid : public PhysicsModel { g_12 = Btxy * hthe * I * Rxy / Bpxy; g_13 = I * Rxy * Rxy; g_23 = Btxy * hthe * Rxy / Bpxy; - coord->setCovariantMetricTensor(CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + coord->setCovariantMetricTensor( + CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); //////////////////////////////////////////////////////// // SET EVOLVING VARIABLES diff --git a/examples/uedge-benchmark/ue_bmark.cxx b/examples/uedge-benchmark/ue_bmark.cxx index 895cc62ad2..07f5746676 100644 --- a/examples/uedge-benchmark/ue_bmark.cxx +++ b/examples/uedge-benchmark/ue_bmark.cxx @@ -147,7 +147,8 @@ class UedgeBenchmark : public PhysicsModel { g12 = 0.0; g13 = 0.0; g23 = -Btxy / (hthe * Bpxy * Rxy); - coords->setContravariantMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); + coords->setContravariantMetricTensor( + ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); coords->setJ(hthe / Bpxy); @@ -158,7 +159,8 @@ class UedgeBenchmark : public PhysicsModel { g_12 = 0.0; g_13 = 0.0; g_23 = Btxy * hthe * Rxy / Bpxy; - coords->setCovariantMetricTensor(CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + coords->setCovariantMetricTensor( + CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); //////////////// BOUNDARIES /////////////////////// // diff --git a/examples/wave-slab/wave_slab.cxx b/examples/wave-slab/wave_slab.cxx index f31a3c7c6a..709ef24123 100644 --- a/examples/wave-slab/wave_slab.cxx +++ b/examples/wave-slab/wave_slab.cxx @@ -38,7 +38,8 @@ class WaveTest : public PhysicsModel { g12 = 0.0; g13 = -I * coords->g11(); g23 = -Btxy / (hthe * Bpxy * Rxy); - coords->setContravariantMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); + coords->setContravariantMetricTensor( + ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); coords->setJ(hthe / Bpxy); @@ -49,7 +50,8 @@ class WaveTest : public PhysicsModel { g_12 = Btxy * hthe * I * Rxy / Bpxy; g_13 = I * Rxy * Rxy; g_23 = Btxy * hthe * Rxy / Bpxy; - coords->setCovariantMetricTensor(CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + coords->setCovariantMetricTensor( + CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); solver->add(f, "f"); solver->add(g, "g"); diff --git a/include/bout/christoffel_symbols.hxx b/include/bout/christoffel_symbols.hxx index 82eeba9424..c04676842e 100644 --- a/include/bout/christoffel_symbols.hxx +++ b/include/bout/christoffel_symbols.hxx @@ -81,7 +81,8 @@ public: // void setLocation(CELL_LOC location); // Transforms the ChristoffelSymbols by applying the given function to every element - void applyToComponents(const std::function& function); + void + applyToComponents(const std::function& function); private: FieldMetric G1_11_, G1_22_, G1_33_, G1_12_, G1_13_, G1_23_; diff --git a/src/field/vector3d.cxx b/src/field/vector3d.cxx index 9dcc0757fb..7a0403cfab 100644 --- a/src/field/vector3d.cxx +++ b/src/field/vector3d.cxx @@ -385,9 +385,9 @@ Vector3D& Vector3D::operator/=(const Field3D& rhs) { Coordinates* metric = localmesh->getCoordinates(lhs.getLocation()); \ \ /* calculate contravariant components of cross-product */ \ - result.x = (lco.y * rco.z - lco.z * rco.y) / metric->J(); \ - result.y = (lco.z * rco.x - lco.x * rco.z) / metric->J(); \ - result.z = (lco.x * rco.y - lco.y * rco.x) / metric->J(); \ + result.x = (lco.y * rco.z - lco.z * rco.y) / metric->J(); \ + result.y = (lco.z * rco.x - lco.x * rco.z) / metric->J(); \ + result.z = (lco.x * rco.y - lco.y * rco.x) / metric->J(); \ result.covariant = false; \ \ return result; \ diff --git a/src/invert/laplace/impls/hypre3d/hypre3d_laplace.cxx b/src/invert/laplace/impls/hypre3d/hypre3d_laplace.cxx index b84f414035..9400acc706 100644 --- a/src/invert/laplace/impls/hypre3d/hypre3d_laplace.cxx +++ b/src/invert/laplace/impls/hypre3d/hypre3d_laplace.cxx @@ -365,10 +365,10 @@ void LaplaceHypre3d::updateMatrix3D() { C_df_dy *= D[l]; } if (issetC) { - C_df_dy += - (coords->g12()[l] * dc_dx[l] + (coords->g22()[l] - 1. / coords->g_22()[l]) * dc_dy[l] - + coords->g23()[l] * dc_dz[l]) - / C1[l]; + C_df_dy += (coords->g12()[l] * dc_dx[l] + + (coords->g22()[l] - 1. / coords->g_22()[l]) * dc_dy[l] + + coords->g23()[l] * dc_dz[l]) + / C1[l]; } BoutReal C_d2f_dy2 = (coords->g22()[l] - 1.0 / coords->g_22()[l]); @@ -389,8 +389,8 @@ void LaplaceHypre3d::updateMatrix3D() { C_df_dy /= 2 * coords->dy()[l]; C_d2f_dy2 /= SQ(coords->dy()[l]); C_d2f_dxdy /= 4 * coords->dx()[l]; // NOTE: This value is not completed here. It needs - // to be divide by dx(i +/- 1, j, k) when using to - // set a matrix element + // to be divide by dx(i +/- 1, j, k) when using to + // set a matrix element C_d2f_dydz /= 4 * coords->dy()[l] * coords->dz()[l]; // The values stored in the y-boundary are already interpolated diff --git a/src/invert/laplace/impls/multigrid/multigrid_laplace.cxx b/src/invert/laplace/impls/multigrid/multigrid_laplace.cxx index 78b970f434..c4e68a40a4 100644 --- a/src/invert/laplace/impls/multigrid/multigrid_laplace.cxx +++ b/src/invert/laplace/impls/multigrid/multigrid_laplace.cxx @@ -621,7 +621,8 @@ void LaplaceMultigrid::generateMatrixF(int level) { + coords->g13()(i2, yindex) * ddz_C // (could assume zero, at least initially, if easier; then check this is true in constructor) ) - / coords->dx()(i2, yindex); // coefficient of 1st derivative stencil (x-direction) + / coords->dx()(i2, + yindex); // coefficient of 1st derivative stencil (x-direction) if (nonuniform) { // add correction for non-uniform dx dxd += D(i2, yindex, k2) * coords->d1_dx()(i2, yindex); diff --git a/src/invert/laplace/impls/petsc/petsc_laplace.cxx b/src/invert/laplace/impls/petsc/petsc_laplace.cxx index ac7a7bc7be..35d2ac5ef2 100644 --- a/src/invert/laplace/impls/petsc/petsc_laplace.cxx +++ b/src/invert/laplace/impls/petsc/petsc_laplace.cxx @@ -420,7 +420,8 @@ FieldPerp LaplacePetsc::solve(const FieldPerp& b, const FieldPerp& x0) { if (fourth_order) { // Fourth Order Accuracy on Boundary Element(i, x, z, 0, 0, - -25.0 / (12.0 * coords->dx()(x, y, z)) / sqrt(coords->g_11()(x, y, z)), + -25.0 / (12.0 * coords->dx()(x, y, z)) + / sqrt(coords->g_11()(x, y, z)), MatA); Element(i, x, z, 1, 0, 4.0 / coords->dx()(x, y, z) / sqrt(coords->g_11()(x, y, z)), MatA); @@ -430,7 +431,8 @@ FieldPerp LaplacePetsc::solve(const FieldPerp& b, const FieldPerp& x0) { 4.0 / (3.0 * coords->dx()(x, y, z)) / sqrt(coords->g_11()(x, y, z)), MatA); Element(i, x, z, 4, 0, - -1.0 / (4.0 * coords->dx()(x, y, z)) / sqrt(coords->g_11()(x, y, z)), + -1.0 / (4.0 * coords->dx()(x, y, z)) + / sqrt(coords->g_11()(x, y, z)), MatA); } else { // Second Order Accuracy on Boundary @@ -685,14 +687,16 @@ FieldPerp LaplacePetsc::solve(const FieldPerp& b, const FieldPerp& x0) { if (fourth_order) { // Fourth Order Accuracy on Boundary Element(i, x, z, 0, 0, - 25.0 / (12.0 * coords->dx()(x, y, z)) / sqrt(coords->g_11()(x, y, z)), + 25.0 / (12.0 * coords->dx()(x, y, z)) + / sqrt(coords->g_11()(x, y, z)), MatA); Element(i, x, z, -1, 0, -4.0 / coords->dx()(x, y, z) / sqrt(coords->g_11()(x, y, z)), MatA); Element(i, x, z, -2, 0, 3.0 / coords->dx()(x, y, z) / sqrt(coords->g_11()(x, y, z)), MatA); Element(i, x, z, -3, 0, - -4.0 / (3.0 * coords->dx()(x, y, z)) / sqrt(coords->g_11()(x, y, z)), + -4.0 / (3.0 * coords->dx()(x, y, z)) + / sqrt(coords->g_11()(x, y, z)), MatA); Element(i, x, z, -4, 0, 1.0 / (4.0 * coords->dx()(x, y, z)) / sqrt(coords->g_11()(x, y, z)), diff --git a/src/invert/laplace/impls/petsc3damg/petsc3damg.cxx b/src/invert/laplace/impls/petsc3damg/petsc3damg.cxx index 2150c03d7e..3e85c2ed20 100644 --- a/src/invert/laplace/impls/petsc3damg/petsc3damg.cxx +++ b/src/invert/laplace/impls/petsc3damg/petsc3damg.cxx @@ -120,41 +120,45 @@ LaplacePetsc3dAmg::LaplacePetsc3dAmg(Options* opt, const CELL_LOC loc, Mesh* mes // Set up boundary conditions in operator const bool inner_X_neumann = flagSet(inner_boundary_flags, INVERT_AC_GRAD); - const auto inner_X_BC = inner_X_neumann ? -1. / coords->dx() / sqrt(coords->g_11()) : 0.5; + const auto inner_X_BC = + inner_X_neumann ? -1. / coords->dx() / sqrt(coords->g_11()) : 0.5; const auto inner_X_BC_plus = inner_X_neumann ? -inner_X_BC : 0.5; BOUT_FOR_SERIAL(i, indexer->getRegionInnerX()) { operator3D(i, i) = inner_X_BC[i]; operator3D(i, i.xp()) = inner_X_BC_plus[i]; - } + } const bool outer_X_neumann = flagSet(outer_boundary_flags, INVERT_AC_GRAD); - const auto outer_X_BC = outer_X_neumann ? 1. / coords->dx() / sqrt(coords->g_11()) : 0.5; + const auto outer_X_BC = + outer_X_neumann ? 1. / coords->dx() / sqrt(coords->g_11()) : 0.5; const auto outer_X_BC_minus = outer_X_neumann ? -outer_X_BC : 0.5; BOUT_FOR_SERIAL(i, indexer->getRegionOuterX()) { operator3D(i, i) = outer_X_BC[i]; operator3D(i, i.xm()) = outer_X_BC_minus[i]; - } + } const bool lower_Y_neumann = flagSet(lower_boundary_flags, INVERT_AC_GRAD); - const auto lower_Y_BC = lower_Y_neumann ? -1. / coords->dy() / sqrt(coords->g_22()) : 0.5; + const auto lower_Y_BC = + lower_Y_neumann ? -1. / coords->dy() / sqrt(coords->g_22()) : 0.5; const auto lower_Y_BC_plus = lower_Y_neumann ? -lower_Y_BC : 0.5; BOUT_FOR_SERIAL(i, indexer->getRegionLowerY()) { operator3D(i, i) = lower_Y_BC[i]; operator3D(i, i.yp()) = lower_Y_BC_plus[i]; - } + } const bool upper_Y_neumann = flagSet(upper_boundary_flags, INVERT_AC_GRAD); - const auto upper_Y_BC = upper_Y_neumann ? 1. / coords->dy() / sqrt(coords->g_22()) : 0.5; + const auto upper_Y_BC = + upper_Y_neumann ? 1. / coords->dy() / sqrt(coords->g_22()) : 0.5; const auto upper_Y_BC_minus = upper_Y_neumann ? -upper_Y_BC : 0.5; BOUT_FOR_SERIAL(i, indexer->getRegionUpperY()) { operator3D(i, i) = upper_Y_BC[i]; operator3D(i, i.ym()) = upper_Y_BC_minus[i]; - } } +} LaplacePetsc3dAmg::~LaplacePetsc3dAmg() { if (kspInitialised) { @@ -365,10 +369,10 @@ void LaplacePetsc3dAmg::updateMatrix3D() { C_df_dy *= D[l]; } if (issetC) { - C_df_dy += - (coords->g12()[l] * dc_dx[l] + (coords->g22()[l] - 1. / coords->g_22()[l]) * dc_dy[l] - + coords->g23()[l] * dc_dz[l]) - / C1[l]; + C_df_dy += (coords->g12()[l] * dc_dx[l] + + (coords->g22()[l] - 1. / coords->g_22()[l]) * dc_dy[l] + + coords->g23()[l] * dc_dz[l]) + / C1[l]; } BoutReal C_d2f_dy2 = (coords->g22()[l] - 1.0 / coords->g_22()[l]); @@ -391,8 +395,8 @@ void LaplacePetsc3dAmg::updateMatrix3D() { C_d2f_dy2 /= SQ(coords->dy()[l]); C_d2f_dxdy /= 4 * coords->dx()[l]; // NOTE: This value is not completed here. It needs to - // be divide by dx(i +/- 1, j, k) when using to set a - // matrix element + // be divide by dx(i +/- 1, j, k) when using to set a + // matrix element C_d2f_dydz /= 4 * coords->dy()[l] * coords->dz()[l]; // The values stored in the y-boundary are already interpolated diff --git a/src/invert/laplacexy2/laplacexy2.cxx b/src/invert/laplacexy2/laplacexy2.cxx index 3a637b8491..76e6437866 100644 --- a/src/invert/laplacexy2/laplacexy2.cxx +++ b/src/invert/laplacexy2/laplacexy2.cxx @@ -180,8 +180,8 @@ void LaplaceXY2::setCoefs(const Field2D& A, const Field2D& B) { BoutReal dy = 0.5 * (coords->dy()[index] + coords->dy()[ind_yp]); Acoef = 0.5 * (A[ind_yp] + A[index]); - BoutReal yp = - -Acoef * J * g23 * g_23 / (g_22 * coords->J()[index] * dy * coords->dy()[index]); + BoutReal yp = -Acoef * J * g23 * g_23 + / (g_22 * coords->J()[index] * dy * coords->dy()[index]); c -= yp; matrix(index, ind_yp) = yp; @@ -193,8 +193,8 @@ void LaplaceXY2::setCoefs(const Field2D& A, const Field2D& B) { dy = 0.5 * (coords->dy()[index] + coords->dy()[ind_ym]); Acoef = 0.5 * (A[ind_ym] + A[index]); - BoutReal ym = - -Acoef * J * g23 * g_23 / (g_22 * coords->J()[index] * dy * coords->dy()[index]); + BoutReal ym = -Acoef * J * g23 * g_23 + / (g_22 * coords->J()[index] * dy * coords->dy()[index]); c -= ym; matrix(index, ind_ym) = ym; } diff --git a/src/invert/laplacexy2/laplacexy2_hypre.cxx b/src/invert/laplacexy2/laplacexy2_hypre.cxx index e3d48d5ca5..f56a715ddd 100644 --- a/src/invert/laplacexy2/laplacexy2_hypre.cxx +++ b/src/invert/laplacexy2/laplacexy2_hypre.cxx @@ -139,8 +139,8 @@ void LaplaceXY2Hypre::setCoefs(const Field2D& A, const Field2D& B) { BoutReal dy = 0.5 * (coords->dy()[index] + coords->dy()[ind_yp]); Acoef = 0.5 * (A[ind_yp] + A[index]); - BoutReal yp = - -Acoef * J * g23 * g_23 / (g_22 * coords->J()[index] * dy * coords->dy()[index]); + BoutReal yp = -Acoef * J * g23 * g_23 + / (g_22 * coords->J()[index] * dy * coords->dy()[index]); c -= yp; // Metrics at y-1/2 @@ -151,8 +151,8 @@ void LaplaceXY2Hypre::setCoefs(const Field2D& A, const Field2D& B) { dy = 0.5 * (coords->dy()[index] + coords->dy()[ind_ym]); Acoef = 0.5 * (A[ind_ym] + A[index]); - BoutReal ym = - -Acoef * J * g23 * g_23 / (g_22 * coords->J()[index] * dy * coords->dy()[index]); + BoutReal ym = -Acoef * J * g23 * g_23 + / (g_22 * coords->J()[index] * dy * coords->dy()[index]); c -= ym; M(index, ind_yp) = yp; M(index, ind_ym) = ym; diff --git a/src/invert/laplacexz/impls/petsc/laplacexz-petsc.cxx b/src/invert/laplacexz/impls/petsc/laplacexz-petsc.cxx index 4a3a82386e..c4f7016e04 100644 --- a/src/invert/laplacexz/impls/petsc/laplacexz-petsc.cxx +++ b/src/invert/laplacexz/impls/petsc/laplacexz-petsc.cxx @@ -403,7 +403,8 @@ void LaplaceXZpetsc::setCoefs(const Field3D& Ain, const Field3D& Bin) { { // Metrics on x+1/2 boundary const BoutReal J = 0.5 * (coords->J()(x, y, z) + coords->J()(x + 1, y, z)); - const BoutReal g11 = 0.5 * (coords->g11()(x, y, z) + coords->g11()(x + 1, y, z)); + const BoutReal g11 = + 0.5 * (coords->g11()(x, y, z) + coords->g11()(x + 1, y, z)); const BoutReal dx = 0.5 * (coords->dx()(x, y, z) + coords->dx()(x + 1, y, z)); const BoutReal Acoef = 0.5 * (A(x, y, z) + A(x + 1, y, z)); @@ -416,7 +417,8 @@ void LaplaceXZpetsc::setCoefs(const Field3D& Ain, const Field3D& Bin) { { // Metrics on x-1/2 boundary const BoutReal J = 0.5 * (coords->J()(x, y, z) + coords->J()(x - 1, y, z)); - const BoutReal g11 = 0.5 * (coords->g11()(x, y, z) + coords->g11()(x - 1, y, z)); + const BoutReal g11 = + 0.5 * (coords->g11()(x, y, z) + coords->g11()(x - 1, y, z)); const BoutReal dx = 0.5 * (coords->dx()(x, y, z) + coords->dx()(x - 1, y, z)); const BoutReal Acoef = 0.5 * (A(x, y, z) + A(x - 1, y, z)); @@ -433,7 +435,8 @@ void LaplaceXZpetsc::setCoefs(const Field3D& Ain, const Field3D& Bin) { { const BoutReal J = 0.5 * (coords->J()(x, y, z) + coords->J()(x, y, zplus)); - const BoutReal g33 = 0.5 * (coords->g33()(x, y, z) + coords->g33()(x, y, zplus)); + const BoutReal g33 = + 0.5 * (coords->g33()(x, y, z) + coords->g33()(x, y, zplus)); const BoutReal dz = 0.5 * (coords->dz()(x, y, z) + coords->dz()(x, y, zplus)); // Metrics on z+1/2 boundary const BoutReal Acoef = 0.5 * (A(x, y, z) + A(x, y, zplus)); @@ -445,7 +448,8 @@ void LaplaceXZpetsc::setCoefs(const Field3D& Ain, const Field3D& Bin) { { // Metrics on z-1/2 boundary const BoutReal J = 0.5 * (coords->J()(x, y, z) + coords->J()(x, y, zminus)); - const BoutReal g33 = 0.5 * (coords->g33()(x, y, z) + coords->g33()(x, y, zminus)); + const BoutReal g33 = + 0.5 * (coords->g33()(x, y, z) + coords->g33()(x, y, zminus)); const BoutReal Acoef = 0.5 * (A(x, y, z) + A(x, y, zminus)); const BoutReal dz = 0.5 * (coords->dz()(x, y, z) + coords->dz()(x, y, zminus)); @@ -471,7 +475,8 @@ void LaplaceXZpetsc::setCoefs(const Field3D& Ain, const Field3D& Bin) { // Metrics const BoutReal J = 0.5 * (coords->J()(x, y, z) + coords->J()(x + 1, y, z)); - const BoutReal g13 = 0.5 * (coords->g13()(x, y, z) + coords->g13()(x + 1, y, z)); + const BoutReal g13 = + 0.5 * (coords->g13()(x, y, z) + coords->g13()(x + 1, y, z)); const BoutReal dz = 0.5 * (coords->dz()(x, y, z) + coords->dz()(x + 1, y, z)); const BoutReal Acoef = 0.5 * (A(x, y, z) + A(x + 1, y, z)); @@ -507,7 +512,8 @@ void LaplaceXZpetsc::setCoefs(const Field3D& Ain, const Field3D& Bin) { // so metrics at (x-1/2,z) const BoutReal J = 0.5 * (coords->J()(x, y, z) + coords->J()(x - 1, y, z)); - const BoutReal g13 = 0.5 * (coords->g13()(x, y, z) + coords->g13()(x - 1, y, z)); + const BoutReal g13 = + 0.5 * (coords->g13()(x, y, z) + coords->g13()(x - 1, y, z)); const BoutReal dz = 0.5 * (coords->dz()(x, y, z) + coords->dz()(x - 1, y, z)); const BoutReal Acoef = 0.5 * (A(x, y, z) + A(x - 1, y, z)); @@ -540,7 +546,8 @@ void LaplaceXZpetsc::setCoefs(const Field3D& Ain, const Field3D& Bin) { // so metrics at (x,z+1/2) const BoutReal J = 0.5 * (coords->J()(x, y, z) + coords->J()(x, y, zplus)); - const BoutReal g13 = 0.5 * (coords->g13()(x, y, z) + coords->g13()(x, y, zplus)); + const BoutReal g13 = + 0.5 * (coords->g13()(x, y, z) + coords->g13()(x, y, zplus)); const BoutReal dx = 0.5 * (coords->dx()(x, y, z) + coords->dx()(x, y, zplus)); const BoutReal Acoef = 0.5 * (A(x, y, z) + A(x, y, zplus)); @@ -573,7 +580,8 @@ void LaplaceXZpetsc::setCoefs(const Field3D& Ain, const Field3D& Bin) { // so metrics at (x,z-1/2) const BoutReal J = 0.5 * (coords->J()(x, y, z) + coords->J()(x, y, zminus)); - const BoutReal g13 = 0.5 * (coords->g13()(x, y, z) + coords->g13()(x, y, zminus)); + const BoutReal g13 = + 0.5 * (coords->g13()(x, y, z) + coords->g13()(x, y, zminus)); const BoutReal dx = 0.5 * (coords->dx()(x, y, z) + coords->dx()(x, y, zminus)); const BoutReal Acoef = 0.5 * (A(x, y, z) + A(x, y, zminus)); diff --git a/src/mesh/boundary_standard.cxx b/src/mesh/boundary_standard.cxx index 27851ab489..58f69f5c71 100644 --- a/src/mesh/boundary_standard.cxx +++ b/src/mesh/boundary_standard.cxx @@ -2171,9 +2171,9 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { BoutReal delta = bndry->bx * metric->dx()(bndry->x, bndry->y, zk) + bndry->by * metric->dy()(bndry->x, bndry->y, zk); #else - BoutReal delta = bndry->bx * metric->dx()(bndry->x, bndry->y) - + bndry->by * metric->dy()(bndry->x, bndry->y); - for (int zk = 0; zk < mesh->LocalNz; zk++) { + BoutReal delta = bndry->bx * metric->dx()(bndry->x, bndry->y) + + bndry->by * metric->dy()(bndry->x, bndry->y); + for (int zk = 0; zk < mesh->LocalNz; zk++) { #endif if (fg) { val = fg->generate(Context(bndry, zk, loc, t, mesh)); diff --git a/src/mesh/metricTensor.cxx b/src/mesh/metricTensor.cxx index 4aa6a8c7ba..5bbf570973 100644 --- a/src/mesh/metricTensor.cxx +++ b/src/mesh/metricTensor.cxx @@ -104,7 +104,7 @@ MetricTensor MetricTensor::inverse(const std::string& region) { g_13[i] = a(0, 2); g_23[i] = a(1, 2); } - + BoutReal maxerr; maxerr = BOUTMAX(max(abs((g_11 * g_11 + g_12 * g_12 + g_13 * g_13) - 1)), max(abs((g_12 * g_12 + g_22 * g_22 + g_23 * g_23) - 1)), @@ -117,7 +117,7 @@ MetricTensor MetricTensor::inverse(const std::string& region) { max(abs(g_12 * g_13 + g_22 * g_23 + g_23 * g_33))); output_info.write("\tMaximum error in off-diagonal inversion is {:e}\n", maxerr); - + auto other_representation = MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23); const auto location = g11_.getLocation(); other_representation.setLocation(location); diff --git a/tests/MMS/GBS/gbs.cxx b/tests/MMS/GBS/gbs.cxx index 273575a274..ed43f48452 100644 --- a/tests/MMS/GBS/gbs.cxx +++ b/tests/MMS/GBS/gbs.cxx @@ -354,7 +354,8 @@ void GBS::LoadMetric(BoutReal Lnorm, BoutReal Bnorm) { const auto g12 = 0.0; const auto g13 = -sinty * coords->g11(); const auto g23 = -sbp * Btxy / (hthe * Bpxy * Rxy); - coords->setContravariantMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); + coords->setContravariantMetricTensor( + ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); coords->setJ(hthe / Bpxy); @@ -364,7 +365,8 @@ void GBS::LoadMetric(BoutReal Lnorm, BoutReal Bnorm) { const auto g_12 = sbp * Btxy * hthe * sinty * Rxy / Bpxy; const auto g_13 = sinty * Rxy * Rxy; const auto g_23 = sbp * Btxy * hthe * Rxy / Bpxy; - coords->setCovariantMetricTensor(CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + coords->setCovariantMetricTensor( + CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); } // just define a macro for V_E dot Grad diff --git a/tests/MMS/elm-pb/elm_pb.cxx b/tests/MMS/elm-pb/elm_pb.cxx index 210f976d69..b2e8e0b6b0 100644 --- a/tests/MMS/elm-pb/elm_pb.cxx +++ b/tests/MMS/elm-pb/elm_pb.cxx @@ -423,7 +423,8 @@ class ELMpb : public PhysicsModel { const auto g12 = 0.0; const auto g13 = -I * coords->g11(); const auto g23 = -Btxy / (hthe * Bpxy * Rxy); - coords->setContravariantMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); + coords->setContravariantMetricTensor( + ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); coords->setJ(hthe / Bpxy); coords->setBxy(B0); @@ -434,7 +435,8 @@ class ELMpb : public PhysicsModel { const auto g_12 = Btxy * hthe * I * Rxy / Bpxy; const auto g_13 = I * Rxy * Rxy; const auto g_23 = Btxy * hthe * Rxy / Bpxy; - coords->setCovariantMetricTensor(CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + coords->setCovariantMetricTensor( + CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); // Set B field vector diff --git a/tests/MMS/fieldalign/fieldalign.cxx b/tests/MMS/fieldalign/fieldalign.cxx index 715cb616b9..16e9c50bc8 100644 --- a/tests/MMS/fieldalign/fieldalign.cxx +++ b/tests/MMS/fieldalign/fieldalign.cxx @@ -27,9 +27,9 @@ class FieldAlign : public PhysicsModel { + // Upwinding with second-order central differencing vz / G * (metric->g13() * DDX(f) + metric->g23() * DDY(f) - + metric->g33() * DDZ(f)); // (unstable without additional dissipation) - -SQ(SQ(metric->dx())) * D4DX4(f) /*- SQ(SQ(metric->dy()))*D4DY4(f)*/ - - SQ(SQ(metric->dz())) * D4DZ4(f); // Numerical dissipation terms + + metric->g33() * DDZ(f)); // (unstable without additional dissipation) + -SQ(SQ(metric->dx())) * D4DX4(f) /*- SQ(SQ(metric->dy()))*D4DY4(f)*/ + - SQ(SQ(metric->dz())) * D4DZ4(f); // Numerical dissipation terms return 0; } diff --git a/tests/MMS/spatial/diffusion/diffusion.cxx b/tests/MMS/spatial/diffusion/diffusion.cxx index ba4e33ca24..14b4010df6 100644 --- a/tests/MMS/spatial/diffusion/diffusion.cxx +++ b/tests/MMS/spatial/diffusion/diffusion.cxx @@ -39,7 +39,8 @@ class Diffusion : public PhysicsModel { SAVE_ONCE3(Dx, Dy, Dz) // set mesh - auto contravariant_metric_tensor = ContravariantMetricTensor(1.1, 1.1, 1.1, 0.0, 0.0, 0.0); + auto contravariant_metric_tensor = + ContravariantMetricTensor(1.1, 1.1, 1.1, 0.0, 0.0, 0.0); coords->setContravariantMetricTensor(contravariant_metric_tensor); auto covariant_metric_tensor = CovariantMetricTensor(1.1, 1.1, 1.1, 0.0, 0.0, 0.0); diff --git a/tests/MMS/tokamak/tokamak.cxx b/tests/MMS/tokamak/tokamak.cxx index f92d115caa..127e62e504 100644 --- a/tests/MMS/tokamak/tokamak.cxx +++ b/tests/MMS/tokamak/tokamak.cxx @@ -86,7 +86,8 @@ class TokamakMMS : public PhysicsModel { const auto g12 = 0.0; const auto g13 = -sinty * coords->g11(); const auto g23 = -sbp * Btxy / (hthe * Bpxy * Rxy); - coords->setContravariantMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); + coords->setContravariantMetricTensor( + ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); coords->setJ(hthe / Bpxy); @@ -96,7 +97,8 @@ class TokamakMMS : public PhysicsModel { const auto g_12 = sbp * Btxy * hthe * sinty * Rxy / Bpxy; const auto g_13 = sinty * Rxy * Rxy; const auto g_23 = sbp * Btxy * hthe * Rxy / Bpxy; - coords->setCovariantMetricTensor(CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + coords->setCovariantMetricTensor( + CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); } private: diff --git a/tests/integrated/test-drift-instability/2fluid.cxx b/tests/integrated/test-drift-instability/2fluid.cxx index 577e5b1ef7..fe2b27b681 100644 --- a/tests/integrated/test-drift-instability/2fluid.cxx +++ b/tests/integrated/test-drift-instability/2fluid.cxx @@ -224,7 +224,8 @@ class TwoFluid : public PhysicsModel { g12 = 0.0; g13 = -I * coord->g11(); g23 = -Btxy / (hthe * Bpxy * Rxy); - coord->setContravariantMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); + coord->setContravariantMetricTensor( + ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); coord->setJ(hthe / Bpxy); @@ -235,7 +236,8 @@ class TwoFluid : public PhysicsModel { g_12 = Btxy * hthe * I * Rxy / Bpxy; g_13 = I * Rxy * Rxy; g_23 = Btxy * hthe * Rxy / Bpxy; - coord->setCovariantMetricTensor(CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + coord->setCovariantMetricTensor( + CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); /**************** SET EVOLVING VARIABLES *************/ diff --git a/tests/integrated/test-interchange-instability/2fluid.cxx b/tests/integrated/test-interchange-instability/2fluid.cxx index 3574761aa9..64f822b575 100644 --- a/tests/integrated/test-interchange-instability/2fluid.cxx +++ b/tests/integrated/test-interchange-instability/2fluid.cxx @@ -146,7 +146,8 @@ class Interchange : public PhysicsModel { g12 = 0.0; g13 = -I * coord->g11(); g23 = -Btxy / (hthe * Bpxy * Rxy); - coord->setContravariantMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); + coord->setContravariantMetricTensor( + ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); coord->setJ(hthe / Bpxy); @@ -157,7 +158,8 @@ class Interchange : public PhysicsModel { g_12 = Btxy * hthe * I * Rxy / Bpxy; g_13 = I * Rxy * Rxy; g_23 = Btxy * hthe * Rxy / Bpxy; - coord->setCovariantMetricTensor(CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + coord->setCovariantMetricTensor( + CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); // Tell BOUT++ which variables to evolve SOLVE_FOR2(rho, Ni); diff --git a/tests/integrated/test-laplacexy/loadmetric.cxx b/tests/integrated/test-laplacexy/loadmetric.cxx index 6ab68c1539..c84e5ce476 100644 --- a/tests/integrated/test-laplacexy/loadmetric.cxx +++ b/tests/integrated/test-laplacexy/loadmetric.cxx @@ -55,7 +55,8 @@ void LoadMetric(BoutReal Lnorm, BoutReal Bnorm) { const auto g12 = 0.0; const auto g13 = -sinty * coords->g11(); const auto g23 = -sbp * Btxy / (hthe * Bpxy * Rxy); - coords->setContravariantMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); + coords->setContravariantMetricTensor( + ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); coords->setJ(hthe / Bpxy); @@ -65,5 +66,6 @@ void LoadMetric(BoutReal Lnorm, BoutReal Bnorm) { const auto g_12 = sbp * Btxy * hthe * sinty * Rxy / Bpxy; const auto g_13 = sinty * Rxy * Rxy; const auto g_23 = sbp * Btxy * hthe * Rxy / Bpxy; - coords->setCovariantMetricTensor(CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + coords->setCovariantMetricTensor( + CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); } diff --git a/tests/integrated/test-laplacexy/test-laplacexy.cxx b/tests/integrated/test-laplacexy/test-laplacexy.cxx index f06c95b4a4..b5deb38ce1 100644 --- a/tests/integrated/test-laplacexy/test-laplacexy.cxx +++ b/tests/integrated/test-laplacexy/test-laplacexy.cxx @@ -77,7 +77,7 @@ int main(int argc, char** argv) { mesh->communicate(solution); Field2D rhs_check; if (include_y_derivs) { - rhs_check = + rhs_check = a * Laplace_perp(solution) + Grad_perp(a) * Grad_perp(solution) + b * solution; } else { rhs_check = a * Delp2(solution, CELL_DEFAULT, false) From c46d0ab2b8aeb2d3bab736a728b9ea8fd9245195 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Thu, 14 Mar 2024 15:06:52 +0000 Subject: [PATCH 353/491] More updates to examples and tests to use dx setter. --- examples/constraints/alfven-wave/alfven.cxx | 2 +- examples/laplacexy/alfven-wave/alfven.cxx | 2 +- examples/tokamak-2fluid/2fluid.cxx | 2 +- tests/MMS/tokamak/tokamak.cxx | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/examples/constraints/alfven-wave/alfven.cxx b/examples/constraints/alfven-wave/alfven.cxx index a159e7d350..9a86940daf 100644 --- a/examples/constraints/alfven-wave/alfven.cxx +++ b/examples/constraints/alfven-wave/alfven.cxx @@ -170,7 +170,7 @@ class Alfven : public PhysicsModel { Field2D dx; if (!mesh->get(dx, "dpsi")) { output << "\tUsing dpsi as the x grid spacing\n"; - coord->dx() = dx; // Only use dpsi if found + coord->setDx(dx); // Only use dpsi if found } else { // dx will have been read already from the grid output << "\tUsing dx as the x grid spacing\n"; diff --git a/examples/laplacexy/alfven-wave/alfven.cxx b/examples/laplacexy/alfven-wave/alfven.cxx index a32dc0a045..1849763a61 100644 --- a/examples/laplacexy/alfven-wave/alfven.cxx +++ b/examples/laplacexy/alfven-wave/alfven.cxx @@ -179,7 +179,7 @@ class Alfven : public PhysicsModel { Field2D dx; if (!mesh->get(dx, "dpsi")) { output << "\tUsing dpsi as the x grid spacing\n"; - coord->dx() = dx; // Only use dpsi if found + coord->setDx(dx); // Only use dpsi if found } else { // dx will have been read already from the grid output << "\tUsing dx as the x grid spacing\n"; diff --git a/examples/tokamak-2fluid/2fluid.cxx b/examples/tokamak-2fluid/2fluid.cxx index 195efc8e7d..9f865d773c 100644 --- a/examples/tokamak-2fluid/2fluid.cxx +++ b/examples/tokamak-2fluid/2fluid.cxx @@ -138,7 +138,7 @@ class TwoFluid : public PhysicsModel { Field2D dx; if (!mesh->get(dx, "dpsi")) { output << "\tUsing dpsi as the x grid spacing\n"; - coord->dx() = dx; // Only use dpsi if found + coord->setDx(dx); // Only use dpsi if found } else { // dx will have been read already from the grid output << "\tUsing dx as the x grid spacing\n"; diff --git a/tests/MMS/tokamak/tokamak.cxx b/tests/MMS/tokamak/tokamak.cxx index f92d115caa..0b9455ee79 100644 --- a/tests/MMS/tokamak/tokamak.cxx +++ b/tests/MMS/tokamak/tokamak.cxx @@ -53,7 +53,7 @@ class TokamakMMS : public PhysicsModel { Field2D dx; if (!mesh->get(dx, "dpsi")) { output << "\tUsing dpsi as the x grid spacing\n"; - coords->dx() = dx; // Only use dpsi if found + coords->setDx(dx); // Only use dpsi if found } else { // dx will have been read already from the grid output << "\tUsing dx as the x grid spacing\n"; From 94663341843538e9761d41c23a9358630f231c12 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Thu, 14 Mar 2024 16:05:27 +0000 Subject: [PATCH 354/491] Rename file MetricTensor to use snake case. --- CMakeLists.txt | 4 ++-- include/bout/christoffel_symbols.hxx | 2 +- include/bout/coordinates.hxx | 2 +- include/bout/g_values.hxx | 2 +- include/bout/{metricTensor.hxx => metric_tensor.hxx} | 6 +++--- src/mesh/coordinates_accessor.cxx | 2 +- src/mesh/{metricTensor.cxx => metric_tensor.cxx} | 2 +- 7 files changed, 10 insertions(+), 10 deletions(-) rename include/bout/{metricTensor.hxx => metric_tensor.hxx} (97%) rename src/mesh/{metricTensor.cxx => metric_tensor.cxx} (99%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 0badccd080..eeeeb82496 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -352,8 +352,8 @@ set(BOUT_SOURCES ./src/sys/timer.cxx ./src/sys/type_name.cxx ./src/sys/utils.cxx - ./include/bout/metricTensor.hxx - ./src/mesh/metricTensor.cxx + ./include/bout/metric_tensor.hxx + ./src/mesh/metric_tensor.cxx ./include/bout/christoffel_symbols.hxx ./src/mesh/christoffel_symbols.cxx ./include/bout/g_values.hxx diff --git a/include/bout/christoffel_symbols.hxx b/include/bout/christoffel_symbols.hxx index c04676842e..a6cd19a195 100644 --- a/include/bout/christoffel_symbols.hxx +++ b/include/bout/christoffel_symbols.hxx @@ -4,7 +4,7 @@ #include "bout/field2d.hxx" #include "bout/field3d.hxx" -#include "bout/metricTensor.hxx" +#include "bout/metric_tensor.hxx" #include using FieldMetric = MetricTensor::FieldMetric; diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index fcd9d55cf8..9f0f842b36 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -35,7 +35,7 @@ #include "christoffel_symbols.hxx" #include "g_values.hxx" -#include "bout/metricTensor.hxx" +#include "bout/metric_tensor.hxx" #include "bout/paralleltransform.hxx" class Mesh; diff --git a/include/bout/g_values.hxx b/include/bout/g_values.hxx index 22f1c9fced..ea81ca6932 100644 --- a/include/bout/g_values.hxx +++ b/include/bout/g_values.hxx @@ -2,7 +2,7 @@ #ifndef BOUT_GVALUES_HXX #define BOUT_GVALUES_HXX -#include "bout/metricTensor.hxx" +#include "bout/metric_tensor.hxx" using FieldMetric = MetricTensor::FieldMetric; diff --git a/include/bout/metricTensor.hxx b/include/bout/metric_tensor.hxx similarity index 97% rename from include/bout/metricTensor.hxx rename to include/bout/metric_tensor.hxx index f2c894ba86..fe6a4b9bf5 100644 --- a/include/bout/metricTensor.hxx +++ b/include/bout/metric_tensor.hxx @@ -1,6 +1,6 @@ -#ifndef BOUT_METRICTENSOR_HXX -#define BOUT_METRICTENSOR_HXX +#ifndef BOUT_METRIC_TENSOR_HXX +#define BOUT_METRIC_TENSOR_HXX #include "bout/field2d.hxx" #include "bout/field3d.hxx" @@ -90,4 +90,4 @@ public: : MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23, mesh){}; }; -#endif //BOUT_METRICTENSOR_HXX +#endif //BOUT_METRIC_TENSOR_HXX diff --git a/src/mesh/coordinates_accessor.cxx b/src/mesh/coordinates_accessor.cxx index c8c2fdff66..b6df669066 100644 --- a/src/mesh/coordinates_accessor.cxx +++ b/src/mesh/coordinates_accessor.cxx @@ -1,6 +1,6 @@ #include "bout/coordinates_accessor.hxx" #include "bout/mesh.hxx" -#include "bout/metricTensor.hxx" +#include "bout/metric_tensor.hxx" #include diff --git a/src/mesh/metricTensor.cxx b/src/mesh/metric_tensor.cxx similarity index 99% rename from src/mesh/metricTensor.cxx rename to src/mesh/metric_tensor.cxx index 5bbf570973..dced5047bf 100644 --- a/src/mesh/metricTensor.cxx +++ b/src/mesh/metric_tensor.cxx @@ -1,5 +1,5 @@ -#include "bout/metricTensor.hxx" +#include "bout/metric_tensor.hxx" #include "bout/output.hxx" #include From 031fb4b6907384bf1bf417f5cd78c1a15b98d56d Mon Sep 17 00:00:00 2001 From: tomc271 Date: Thu, 14 Mar 2024 16:17:53 +0000 Subject: [PATCH 355/491] Updated another example (gravity_reduced) to use new metric tensor setters. --- examples/gravity_reduced/gravity_reduced.cxx | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/examples/gravity_reduced/gravity_reduced.cxx b/examples/gravity_reduced/gravity_reduced.cxx index 2085a02ac6..fb20fab375 100644 --- a/examples/gravity_reduced/gravity_reduced.cxx +++ b/examples/gravity_reduced/gravity_reduced.cxx @@ -115,8 +115,21 @@ class GravityReduced : public PhysicsModel { Lz = options["Lz"].withDefault(1.); // Set the metric tensor components to get Lz - coord->g33() = SQ(2. * PI / Lz); - coord->g_33() = 1. / coord->g33(); + const auto g33 = SQ(2. * PI / Lz); + coord->setContravariantMetricTensor(ContravariantMetricTensor(coord->g11(), + coord->g22(), + g33, + coord->g12(), + coord->g13(), + coord->g23())); + + const auto g_33 = 1. / coord->g33(); + coord->setCovariantMetricTensor(CovariantMetricTensor(coord->g_11(), + coord->g_22(), + g_33, + coord->g_12(), + coord->g_13(), + coord->g_23())); /**************** SET EVOLVING VARIABLES *************/ From d259faf2c00c6285b1c295801f90fcd95e1857df Mon Sep 17 00:00:00 2001 From: tomc271 Date: Thu, 14 Mar 2024 16:18:59 +0000 Subject: [PATCH 356/491] Apply clang-format changes --- examples/gravity_reduced/gravity_reduced.cxx | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/examples/gravity_reduced/gravity_reduced.cxx b/examples/gravity_reduced/gravity_reduced.cxx index fb20fab375..556b402d6f 100644 --- a/examples/gravity_reduced/gravity_reduced.cxx +++ b/examples/gravity_reduced/gravity_reduced.cxx @@ -116,20 +116,12 @@ class GravityReduced : public PhysicsModel { // Set the metric tensor components to get Lz const auto g33 = SQ(2. * PI / Lz); - coord->setContravariantMetricTensor(ContravariantMetricTensor(coord->g11(), - coord->g22(), - g33, - coord->g12(), - coord->g13(), - coord->g23())); + coord->setContravariantMetricTensor(ContravariantMetricTensor( + coord->g11(), coord->g22(), g33, coord->g12(), coord->g13(), coord->g23())); const auto g_33 = 1. / coord->g33(); - coord->setCovariantMetricTensor(CovariantMetricTensor(coord->g_11(), - coord->g_22(), - g_33, - coord->g_12(), - coord->g_13(), - coord->g_23())); + coord->setCovariantMetricTensor(CovariantMetricTensor( + coord->g_11(), coord->g_22(), g_33, coord->g_12(), coord->g_13(), coord->g_23())); /**************** SET EVOLVING VARIABLES *************/ From ad36b895aecc14e21dce77a4d6674722b08649c2 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Thu, 14 Mar 2024 16:48:46 +0000 Subject: [PATCH 357/491] Remove obsolete comment. --- include/bout/coordinates.hxx | 1 - 1 file changed, 1 deletion(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index 9f0f842b36..e986840bd9 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -157,7 +157,6 @@ public: IntShiftTorsion_ = std::move(IntShiftTorsion); } - /// Calculate differential geometry quantities from the metric tensor int communicateAndCheckMeshSpacing() const; /////////////////////////////////////////////////////////// From e7bdfcf54d6a157d7a1ea35ebc4433c16b7c0861 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Fri, 15 Mar 2024 10:26:39 +0000 Subject: [PATCH 358/491] Make jacobian setter take a const reference. --- include/bout/coordinates.hxx | 2 +- src/mesh/coordinates.cxx | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index e986840bd9..837e013a0f 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -142,7 +142,7 @@ public: ///< Magnitude of B = nabla z times nabla x const FieldMetric& Bxy() const { return Bxy_; } - void setJ(FieldMetric J); + void setJ(const FieldMetric& J); void setJ(BoutReal value, int x, int y); void setBxy(FieldMetric Bxy); diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 43c6c380b1..765d7d5c66 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -1380,7 +1380,7 @@ const FieldMetric& Coordinates::J() const { return *jacobian_cache; } -void Coordinates::setJ(FieldMetric J) { +void Coordinates::setJ(const FieldMetric& J) { //TODO: Calculate J and check value is close auto ptr = std::make_unique(J); jacobian_cache = std::move(ptr); From 2e461008c26154a0688ad3ad2f5b7480998cc8a7 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Fri, 15 Mar 2024 11:00:45 +0000 Subject: [PATCH 359/491] Call communicateAndCheckMeshSpacing() in ChristoffelSymbols constructor (immediately before calling the differential operator functions). --- src/mesh/christoffel_symbols.cxx | 2 ++ src/mesh/mesh.cxx | 1 - 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/mesh/christoffel_symbols.cxx b/src/mesh/christoffel_symbols.cxx index 9e150a161e..3872470778 100644 --- a/src/mesh/christoffel_symbols.cxx +++ b/src/mesh/christoffel_symbols.cxx @@ -21,6 +21,8 @@ ChristoffelSymbols::ChristoffelSymbols(const Coordinates& coordinates) { // Note: This calculation is completely general: metric // tensor can be 2D or 3D. For 2D, all DDZ terms are zero + coordinates.communicateAndCheckMeshSpacing(); + const auto& contravariantMetricTensor = coordinates.getContravariantMetricTensor(); const auto& covariantMetricTensor = coordinates.getCovariantMetricTensor(); diff --git a/src/mesh/mesh.cxx b/src/mesh/mesh.cxx index 47a595ac32..971bfbe10a 100644 --- a/src/mesh/mesh.cxx +++ b/src/mesh/mesh.cxx @@ -914,7 +914,6 @@ void Mesh::recalculateStaggeredCoordinates() { } *coords_map[location] = std::move(*createDefaultCoordinates(location, false, true)); - coords_map[location]->communicateAndCheckMeshSpacing(); } } From 92aaaa15909c541f38ce1d9f59a1627140d0184e Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 20 Mar 2024 13:52:26 +0000 Subject: [PATCH 360/491] Remove unused #include. --- src/field/field_data.cxx | 1 - 1 file changed, 1 deletion(-) diff --git a/src/field/field_data.cxx b/src/field/field_data.cxx index ee8bd97b30..e0d57c3f65 100644 --- a/src/field/field_data.cxx +++ b/src/field/field_data.cxx @@ -1,5 +1,4 @@ -#include "bout/unused.hxx" #include #include #include From 9adbfda5e3cd43c4271278e7b74606541539ecdc Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 20 Mar 2024 13:57:18 +0000 Subject: [PATCH 361/491] Clang-Tidy: Single-argument constructors must be marked explicit to avoid unintentional implicit conversions. --- include/bout/fieldgroup.hxx | 8 ++++---- src/mesh/impls/bout/boutmesh.hxx | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/include/bout/fieldgroup.hxx b/include/bout/fieldgroup.hxx index 184766c6b8..32bc75e557 100644 --- a/include/bout/fieldgroup.hxx +++ b/include/bout/fieldgroup.hxx @@ -26,10 +26,10 @@ public: FieldGroup& operator=(FieldGroup&& other) = default; /// Constructor with a single FieldData \p f - FieldGroup(FieldData& f) { fvec.push_back(&f); } + explicit FieldGroup(FieldData& f) { fvec.push_back(&f); } /// Constructor with a single Field3D \p f - FieldGroup(Field3D& f) { + explicit FieldGroup(Field3D& f) { fvec.push_back(&f); f3vec.push_back(&f); } @@ -37,7 +37,7 @@ public: /// Constructor with a single Vector2D \p v /// /// This is needed so that fvec only contains Field2D or Field3D - FieldGroup(Vector2D& v) { + explicit FieldGroup(Vector2D& v) { fvec.push_back(&v.x); fvec.push_back(&v.y); fvec.push_back(&v.z); @@ -46,7 +46,7 @@ public: /// Constructor with a single Vector3D \p v /// /// This is needed so that fvec only contains Field2D or Field3D - FieldGroup(Vector3D& v) { + explicit FieldGroup(Vector3D& v) { fvec.push_back(&v.x); fvec.push_back(&v.y); fvec.push_back(&v.z); diff --git a/src/mesh/impls/bout/boutmesh.hxx b/src/mesh/impls/bout/boutmesh.hxx index 59c6ecbfbd..4b8c74e034 100644 --- a/src/mesh/impls/bout/boutmesh.hxx +++ b/src/mesh/impls/bout/boutmesh.hxx @@ -19,7 +19,7 @@ /// conventions. class BoutMesh : public Mesh { public: - BoutMesh(GridDataSource* s, Options* options = nullptr); + explicit BoutMesh(GridDataSource* s, Options* options = nullptr); ~BoutMesh() override; /// Read in the mesh from data sources From 62dcf21e68535cb755e3ad88a86f533863bdc95f Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 20 Mar 2024 14:33:00 +0000 Subject: [PATCH 362/491] Remove unused #include directives. --- src/mesh/impls/bout/boutmesh.cxx | 4 ---- src/mesh/mesh.cxx | 2 -- 2 files changed, 6 deletions(-) diff --git a/src/mesh/impls/bout/boutmesh.cxx b/src/mesh/impls/bout/boutmesh.cxx index 956aba0f79..d6a01644bc 100644 --- a/src/mesh/impls/bout/boutmesh.cxx +++ b/src/mesh/impls/bout/boutmesh.cxx @@ -39,16 +39,12 @@ #include #include #include -#include -#include -#include #include #include #include #include #include -#include #include /// MPI type of BoutReal for communications diff --git a/src/mesh/mesh.cxx b/src/mesh/mesh.cxx index 971bfbe10a..954c6dea0d 100644 --- a/src/mesh/mesh.cxx +++ b/src/mesh/mesh.cxx @@ -1,6 +1,5 @@ #include #include -#include #include #include #include @@ -10,7 +9,6 @@ #include #include -#include "impls/bout/boutmesh.hxx" #include "bout/interpolation.hxx" /// Interpolate a Field2D to a new CELL_LOC with interp_to. From 77231a6dece68e55609be962eabf7329a61b928f Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 20 Mar 2024 14:34:38 +0000 Subject: [PATCH 363/491] Prefer const. --- include/bout/mesh.hxx | 24 ++++++++++++------------ src/mesh/impls/bout/boutmesh.cxx | 2 +- src/mesh/impls/bout/boutmesh.hxx | 2 +- src/mesh/mesh.cxx | 6 +++--- 4 files changed, 17 insertions(+), 17 deletions(-) diff --git a/include/bout/mesh.hxx b/include/bout/mesh.hxx index d74b02919d..6efe62495e 100644 --- a/include/bout/mesh.hxx +++ b/include/bout/mesh.hxx @@ -407,7 +407,7 @@ public: /// @param[in] tag A label for the communication. Must be the same as sent virtual comm_handle irecvXIn(BoutReal* buffer, int size, int tag) = 0; - MPI_Comm getXcomm() { + MPI_Comm getXcomm() const { return getXcomm(0); } ///< Return communicator containing all processors in X virtual MPI_Comm getXcomm(int jy) const = 0; ///< Return X communicator @@ -697,7 +697,7 @@ public: // INDEX DERIVATIVE OPERATORS /////////////////////////////////////////////////////////// - ////// Utilties and parameters + ////// Utilities and parameters /// Fraction of modes to filter. This is set in derivs_init from option "ddz:fft_filter" BoutReal fft_derivs_filter{0.0}; @@ -705,15 +705,15 @@ public: /// Determines the resultant output stagger location in derivatives /// given the input and output location. Also checks that the /// combination of locations is allowed - STAGGER getStagger(const CELL_LOC inloc, const CELL_LOC outloc, - const CELL_LOC allowedloc) const; + STAGGER getStagger(CELL_LOC inloc, CELL_LOC outloc, + CELL_LOC allowedloc) const; /// Determines the resultant output stagger location in derivatives /// given the input and output location. Also checks that the /// combination of locations is allowed. This overload also checks /// the location of a second input field (velocity) is consistent. - STAGGER getStagger(const CELL_LOC vloc, const CELL_LOC inloc, const CELL_LOC outloc, - const CELL_LOC allowedloc) const; + STAGGER getStagger(CELL_LOC vloc, CELL_LOC inloc, CELL_LOC outloc, + CELL_LOC allowedloc) const; /////////////////////////////////////////////////////////// // REGION RELATED ROUTINES @@ -754,20 +754,20 @@ public: void addRegionPerp(const std::string& region_name, const Region& region); /// Converts an Ind2D to an Ind3D using calculation - Ind3D ind2Dto3D(const Ind2D& ind2D, int jz = 0) { + Ind3D ind2Dto3D(const Ind2D& ind2D, int jz = 0) const { return {ind2D.ind * LocalNz + jz, LocalNy, LocalNz}; } /// Converts an Ind3D to an Ind2D using calculation - Ind2D ind3Dto2D(const Ind3D& ind3D) { return {ind3D.ind / LocalNz, LocalNy, 1}; } + Ind2D ind3Dto2D(const Ind3D& ind3D) const { return {ind3D.ind / LocalNz, LocalNy, 1}; } /// Converts an Ind3D to an IndPerp using calculation - IndPerp ind3DtoPerp(const Ind3D& ind3D) { + IndPerp ind3DtoPerp(const Ind3D& ind3D) const { return {ind3D.x() * LocalNz + ind3D.z(), 1, LocalNz}; } /// Converts an IndPerp to an Ind3D using calculation - Ind3D indPerpto3D(const IndPerp& indPerp, int jy = 0) { + Ind3D indPerpto3D(const IndPerp& indPerp, int jy = 0) const { int jz = indPerp.z(); return {(indPerp.ind - jz) * LocalNy + LocalNz * jy + jz, LocalNy, LocalNz}; } @@ -800,7 +800,7 @@ protected: /// Calculates the size of a message for a given x and y range int msg_len(const std::vector& var_list, int xge, int xlt, int yge, - int ylt); + int ylt) const; /// Initialise derivatives void derivs_init(Options* options); @@ -834,7 +834,7 @@ public: /// Boundary guard cells are set by extrapolating from the grid, like /// 'free_o3' boundary conditions /// Corner guard cells are set to BoutNaN - const Field2D interpolateAndExtrapolate(const Field2D& f, CELL_LOC location, + Field2D interpolateAndExtrapolate(const Field2D& f, CELL_LOC location, bool extrapolate_x, bool extrapolate_y, bool no_extra_interpolate, ParallelTransform* UNUSED(pt) = nullptr, diff --git a/src/mesh/impls/bout/boutmesh.cxx b/src/mesh/impls/bout/boutmesh.cxx index d6a01644bc..478fb681a3 100644 --- a/src/mesh/impls/bout/boutmesh.cxx +++ b/src/mesh/impls/bout/boutmesh.cxx @@ -2318,7 +2318,7 @@ int BoutMesh::pack_data(const std::vector& var_list, int xge, int xl } int BoutMesh::unpack_data(const std::vector& var_list, int xge, int xlt, - int yge, int ylt, BoutReal* buffer) { + int yge, int ylt, const BoutReal* buffer) { int len = 0; diff --git a/src/mesh/impls/bout/boutmesh.hxx b/src/mesh/impls/bout/boutmesh.hxx index 4b8c74e034..48152c92cf 100644 --- a/src/mesh/impls/bout/boutmesh.hxx +++ b/src/mesh/impls/bout/boutmesh.hxx @@ -461,7 +461,7 @@ private: /// Copy data from a buffer back into the fields int unpack_data(const std::vector& var_list, int xge, int xlt, int yge, - int ylt, BoutReal* buffer); + int ylt, const BoutReal* buffer); }; namespace { diff --git a/src/mesh/mesh.cxx b/src/mesh/mesh.cxx index 954c6dea0d..67596d11de 100644 --- a/src/mesh/mesh.cxx +++ b/src/mesh/mesh.cxx @@ -16,7 +16,7 @@ /// Boundary guard cells are set by extrapolating from the grid, like /// 'free_o3' boundary conditions /// Corner guard cells are set to BoutNaN -const Field2D Mesh::interpolateAndExtrapolate(const Field2D& f, CELL_LOC location, +Field2D Mesh::interpolateAndExtrapolate(const Field2D& f, CELL_LOC location, bool extrapolate_x, bool extrapolate_y, bool no_extra_interpolate, ParallelTransform* UNUSED(pt), @@ -522,7 +522,7 @@ void Mesh::communicate(FieldPerp& f) { } int Mesh::msg_len(const std::vector& var_list, int xge, int xlt, int yge, - int ylt) { + int ylt) const { int len = 0; /// Loop over variables @@ -671,7 +671,7 @@ int Mesh::globalStartIndexPerp() { return cumulativeSize - localSize; } -const std::vector Mesh::readInts(const std::string& name, int n) { +std::vector Mesh::readInts(const std::string& name, int n) { TRACE("Mesh::readInts({:s})", name); if (source == nullptr) { From 0235449bca00cde8b922bb20e2620af4623b588e Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 20 Mar 2024 14:39:33 +0000 Subject: [PATCH 364/491] Add [[maybe_unused]] attributes. --- include/bout/mesh.hxx | 10 +++++----- src/mesh/impls/bout/boutmesh.cxx | 2 +- src/mesh/impls/bout/boutmesh.hxx | 6 +++--- src/mesh/mesh.cxx | 8 ++++---- 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/include/bout/mesh.hxx b/include/bout/mesh.hxx index 6efe62495e..57229c101e 100644 --- a/include/bout/mesh.hxx +++ b/include/bout/mesh.hxx @@ -288,7 +288,7 @@ public: } template - void communicateYZ(Ts&... ts) { + [[maybe_unused]] void communicateYZ(Ts&... ts) { FieldGroup g(ts...); communicateYZ(g); } @@ -546,11 +546,11 @@ public: virtual int localSizePerp(); /// Get the value of the first global 3D index on this processor. - virtual int globalStartIndex3D(); + [[maybe_unused]] virtual int globalStartIndex3D(); /// Get the value of the first global 2D index on this processor. - virtual int globalStartIndex2D(); + [[maybe_unused]] virtual int globalStartIndex2D(); /// Get the value of the first global perpendicular index on this processor. - virtual int globalStartIndexPerp(); + [[maybe_unused]] virtual int globalStartIndexPerp(); /// Returns a global X index given a local index. /// Global index includes boundary cells, local index includes boundary or guard cells. @@ -796,7 +796,7 @@ protected: bool calcParallelSlices_on_communicate{true}; /// Read a 1D array of integers - const std::vector readInts(const std::string& name, int n); + [[maybe_unused]] std::vector readInts(const std::string& name, int n); /// Calculates the size of a message for a given x and y range int msg_len(const std::vector& var_list, int xge, int xlt, int yge, diff --git a/src/mesh/impls/bout/boutmesh.cxx b/src/mesh/impls/bout/boutmesh.cxx index 478fb681a3..7ff4365e05 100644 --- a/src/mesh/impls/bout/boutmesh.cxx +++ b/src/mesh/impls/bout/boutmesh.cxx @@ -1702,7 +1702,7 @@ int BoutMesh::getLocalYIndexNoBoundaries(int yglobal) const { return yglobal - PE_YIND * MYSUB + MYG; } -int BoutMesh::YGLOBAL(int yloc, int yproc) const { return yloc + yproc * MYSUB - MYG; } +[[maybe_unused]] int BoutMesh::YGLOBAL(int yloc, int yproc) const { return yloc + yproc * MYSUB - MYG; } int BoutMesh::YLOCAL(int yglo, int yproc) const { return yglo - yproc * MYSUB + MYG; } diff --git a/src/mesh/impls/bout/boutmesh.hxx b/src/mesh/impls/bout/boutmesh.hxx index 48152c92cf..f0a4fe89d5 100644 --- a/src/mesh/impls/bout/boutmesh.hxx +++ b/src/mesh/impls/bout/boutmesh.hxx @@ -172,8 +172,8 @@ public: BoutReal GlobalX(BoutReal jx) const override; BoutReal GlobalY(BoutReal jy) const override; - BoutReal getIxseps1() const { return ixseps1; } - BoutReal getIxseps2() const { return ixseps2; } + [[maybe_unused]] BoutReal getIxseps1() const { return ixseps1; } + [[maybe_unused]] BoutReal getIxseps2() const { return ixseps2; } void outputVars(Options& output_options) override; @@ -318,7 +318,7 @@ protected: /// Returns the processor number, given X (\p xind) and Y (\p yind) /// processor indices. Returns -1 if out of range (no processor) int PROC_NUM(int xind, int yind) const; - int YGLOBAL(int yloc, int yproc) const; + [[maybe_unused]] int YGLOBAL(int yloc, int yproc) const; int YLOCAL(int yglo, int yproc) const; /// Return the Y processor number given a global Y index int YPROC(int yind) const; diff --git a/src/mesh/mesh.cxx b/src/mesh/mesh.cxx index 67596d11de..aa8a481452 100644 --- a/src/mesh/mesh.cxx +++ b/src/mesh/mesh.cxx @@ -650,28 +650,28 @@ int Mesh::localSizePerp() { return localNumCellsPerp; } -int Mesh::globalStartIndex3D() { +[[maybe_unused]] int Mesh::globalStartIndex3D() { int localSize = localSize3D(); int cumulativeSize = 0; mpi->MPI_Scan(&localSize, &cumulativeSize, 1, MPI_INT, MPI_SUM, BoutComm::get()); return cumulativeSize - localSize; } -int Mesh::globalStartIndex2D() { +[[maybe_unused]] int Mesh::globalStartIndex2D() { int localSize = localSize2D(); int cumulativeSize = 0; mpi->MPI_Scan(&localSize, &cumulativeSize, 1, MPI_INT, MPI_SUM, BoutComm::get()); return cumulativeSize - localSize; } -int Mesh::globalStartIndexPerp() { +[[maybe_unused]] int Mesh::globalStartIndexPerp() { int localSize = localSizePerp(); int cumulativeSize = 0; mpi->MPI_Scan(&localSize, &cumulativeSize, 1, MPI_INT, MPI_SUM, getXcomm()); return cumulativeSize - localSize; } -std::vector Mesh::readInts(const std::string& name, int n) { +[[maybe_unused]] std::vector Mesh::readInts(const std::string& name, int n) { TRACE("Mesh::readInts({:s})", name); if (source == nullptr) { From c967204829ee017582626bec006737124fb55495 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 20 Mar 2024 15:05:00 +0000 Subject: [PATCH 365/491] More unused #include directives. --- src/sys/derivs.cxx | 5 ----- src/sys/msg_stack.cxx | 1 - src/sys/options.cxx | 3 --- src/sys/optionsreader.cxx | 1 - src/sys/output.cxx | 3 --- 5 files changed, 13 deletions(-) diff --git a/src/sys/derivs.cxx b/src/sys/derivs.cxx index b48a7b0f49..9169781288 100644 --- a/src/sys/derivs.cxx +++ b/src/sys/derivs.cxx @@ -41,13 +41,8 @@ #include #include #include -#include #include #include -#include -#include - -#include #include #include diff --git a/src/sys/msg_stack.cxx b/src/sys/msg_stack.cxx index 502836324c..425f7e06ae 100644 --- a/src/sys/msg_stack.cxx +++ b/src/sys/msg_stack.cxx @@ -27,7 +27,6 @@ #include "bout/openmpwrap.hxx" #include #include -#include #include #if BOUT_USE_OPENMP diff --git a/src/sys/options.cxx b/src/sys/options.cxx index 49a81cfa88..8d7e1b0d36 100644 --- a/src/sys/options.cxx +++ b/src/sys/options.cxx @@ -10,9 +10,7 @@ #include "bout/output.hxx" #include "bout/sys/expressionparser.hxx" #include "bout/sys/gettext.hxx" -#include "bout/sys/type_name.hxx" #include "bout/sys/variant.hxx" -#include "bout/traits.hxx" #include "bout/unused.hxx" #include "bout/utils.hxx" @@ -21,7 +19,6 @@ #include #include -#include #include #include #include diff --git a/src/sys/optionsreader.cxx b/src/sys/optionsreader.cxx index 8624f646df..b6f9b316fc 100644 --- a/src/sys/optionsreader.cxx +++ b/src/sys/optionsreader.cxx @@ -1,4 +1,3 @@ -#include #include #include #include diff --git a/src/sys/output.cxx b/src/sys/output.cxx index c07cd53474..5ce3852f65 100644 --- a/src/sys/output.cxx +++ b/src/sys/output.cxx @@ -24,10 +24,7 @@ **************************************************************************/ #include -#include -#include #include -#include void Output::enable() { add(std::cout); From 271825057a28ce430d35c0558486b90664a2ee43 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 20 Mar 2024 15:06:21 +0000 Subject: [PATCH 366/491] Clang-Tidy: The 'empty' method should be used to check for emptiness instead of 'length'. --- src/sys/optionsreader.cxx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/sys/optionsreader.cxx b/src/sys/optionsreader.cxx index b6f9b316fc..17e3bdbf54 100644 --- a/src/sys/optionsreader.cxx +++ b/src/sys/optionsreader.cxx @@ -64,13 +64,13 @@ void OptionsReader::parseCommandLine(Options* options, options = Options::getRoot(); buffer = argv[i]; - if (buffer.length() == 0) { + if (buffer.empty()) { continue; } // Test if name starts with a '-', and remove if found if (buffer[0] == '-') { buffer = buffer.substr(1); // Remove the first character (-) - if (buffer.length() == 0) { + if (buffer.empty()) { throw BoutException( _("Invalid command line option '-' found - maybe check whitespace?")); } From c7b70a9fb8af0d11368d019f2bc328260c366ff4 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 20 Mar 2024 15:10:50 +0000 Subject: [PATCH 367/491] More [[maybe_unused]] attributes. --- include/bout/mesh.hxx | 8 ++++---- include/bout/options.hxx | 2 +- include/bout/optionsreader.hxx | 2 +- src/sys/options.cxx | 2 +- src/sys/optionsreader.cxx | 2 +- 5 files changed, 8 insertions(+), 8 deletions(-) diff --git a/include/bout/mesh.hxx b/include/bout/mesh.hxx index 57229c101e..c0cc8a8786 100644 --- a/include/bout/mesh.hxx +++ b/include/bout/mesh.hxx @@ -476,10 +476,10 @@ public: /// Iterate over the upper Y boundary virtual RangeIterator iterateBndryUpperY() const = 0; - virtual RangeIterator iterateBndryLowerOuterY() const = 0; - virtual RangeIterator iterateBndryLowerInnerY() const = 0; - virtual RangeIterator iterateBndryUpperOuterY() const = 0; - virtual RangeIterator iterateBndryUpperInnerY() const = 0; + [[maybe_unused]] virtual RangeIterator iterateBndryLowerOuterY() const = 0; + [[maybe_unused]] virtual RangeIterator iterateBndryLowerInnerY() const = 0; + [[maybe_unused]] virtual RangeIterator iterateBndryUpperOuterY() const = 0; + [[maybe_unused]] virtual RangeIterator iterateBndryUpperInnerY() const = 0; /// Is there a boundary on the lower guard cells in Y /// on any processor along the X direction? diff --git a/include/bout/options.hxx b/include/bout/options.hxx index 839c847289..59f1a4ad05 100644 --- a/include/bout/options.hxx +++ b/include/bout/options.hxx @@ -783,7 +783,7 @@ public: void setConditionallyUsed(); /// clean the cache of parsed options - static void cleanCache(); + [[maybe_unused]] static void cleanCache(); /// Read-only access to internal options and sections /// to allow iteration over the tree diff --git a/include/bout/optionsreader.hxx b/include/bout/optionsreader.hxx index de3d40514d..a5121ee087 100644 --- a/include/bout/optionsreader.hxx +++ b/include/bout/optionsreader.hxx @@ -100,7 +100,7 @@ public: /// ... /// return 0; /// } - void parseCommandLine(Options* options, int argc, char** argv); + [[maybe_unused]] void parseCommandLine(Options* options, int argc, char** argv); void parseCommandLine(Options* options, const std::vector& argv); private: diff --git a/src/sys/options.cxx b/src/sys/options.cxx index 8d7e1b0d36..44541ca0cb 100644 --- a/src/sys/options.cxx +++ b/src/sys/options.cxx @@ -863,7 +863,7 @@ void Options::setConditionallyUsed() { } } -void Options::cleanCache() { FieldFactory::get()->cleanCache(); } +[[maybe_unused]] void Options::cleanCache() { FieldFactory::get()->cleanCache(); } std::map Options::subsections() const { std::map sections; diff --git a/src/sys/optionsreader.cxx b/src/sys/optionsreader.cxx index 17e3bdbf54..9862565863 100644 --- a/src/sys/optionsreader.cxx +++ b/src/sys/optionsreader.cxx @@ -43,7 +43,7 @@ void OptionsReader::write(Options* options, const std::string& filename) { OptionINI{}.write(options, filename); } -void OptionsReader::parseCommandLine(Options* options, int argc, char** argv) { +[[maybe_unused]] void OptionsReader::parseCommandLine(Options* options, int argc, char** argv) { return parseCommandLine(options, std::vector(argv, argv + argc)); } From 07078edd399b49e66f86a4b5b746a3d3c575979a Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 20 Mar 2024 15:15:17 +0000 Subject: [PATCH 368/491] Clang-Tidy: Method can be made static. --- include/bout/mesh.hxx | 2 +- include/bout/optionsreader.hxx | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/include/bout/mesh.hxx b/include/bout/mesh.hxx index c0cc8a8786..6c6ed78c8c 100644 --- a/include/bout/mesh.hxx +++ b/include/bout/mesh.hxx @@ -638,7 +638,7 @@ public: /// Returns the non-CELL_CENTRE location /// allowed as a staggered location - CELL_LOC getAllowedStaggerLoc(DIRECTION direction) const { + static CELL_LOC getAllowedStaggerLoc(DIRECTION direction) { AUTO_TRACE(); switch (direction) { case (DIRECTION::X): diff --git a/include/bout/optionsreader.hxx b/include/bout/optionsreader.hxx index a5121ee087..942b677209 100644 --- a/include/bout/optionsreader.hxx +++ b/include/bout/optionsreader.hxx @@ -67,7 +67,7 @@ public: /// /// @param[inout] options The options section to insert values and subsections into /// @param[in] file The name of the file. printf style arguments can be used to create the file name. - void read(Options* options, const std::string& filename); + static void read(Options* options, const std::string& filename); template void read(Options* options, const S& format, const Args&... args) { @@ -78,7 +78,7 @@ public: /// /// @param[in] options The options tree to be written /// @param[in] file The name of the file to (over)write - void write(Options* options, const std::string& filename); + static void write(Options* options, const std::string& filename); template void write(Options* options, const S& format, const Args&... args) { @@ -100,8 +100,8 @@ public: /// ... /// return 0; /// } - [[maybe_unused]] void parseCommandLine(Options* options, int argc, char** argv); - void parseCommandLine(Options* options, const std::vector& argv); + [[maybe_unused]] static void parseCommandLine(Options* options, int argc, char** argv); + static void parseCommandLine(Options* options, const std::vector& argv); private: /// The instance of this singleton From 363223a201fbfdf8393ad807b8affc2678161038 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 20 Mar 2024 21:49:11 +0000 Subject: [PATCH 369/491] Clang-Tidy: Constructors that are callable with a single argument must be marked explicit to avoid unintentional implicit conversions. --- include/bout/boundary_region.hxx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/bout/boundary_region.hxx b/include/bout/boundary_region.hxx index 58de12045e..92aa79367e 100644 --- a/include/bout/boundary_region.hxx +++ b/include/bout/boundary_region.hxx @@ -39,7 +39,7 @@ constexpr BndryLoc BNDRY_PAR_BKWD_XOUT = BndryLoc::par_bkwd_xout; class BoundaryRegionBase { public: BoundaryRegionBase() = delete; - BoundaryRegionBase(std::string name, Mesh* passmesh = nullptr) + explicit BoundaryRegionBase(std::string name, Mesh* passmesh = nullptr) : localmesh(passmesh ? passmesh : bout::globals::mesh), label(std::move(name)) {} BoundaryRegionBase(std::string name, BndryLoc loc, Mesh* passmesh = nullptr) : localmesh(passmesh ? passmesh : bout::globals::mesh), label(std::move(name)), From 1d36429d2610cef032e017bcddb92ea7024c3014 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 20 Mar 2024 21:57:54 +0000 Subject: [PATCH 370/491] Another unused #include directive. --- src/mesh/boundary_region.cxx | 1 - 1 file changed, 1 deletion(-) diff --git a/src/mesh/boundary_region.cxx b/src/mesh/boundary_region.cxx index 700ef8a91f..02a5310149 100644 --- a/src/mesh/boundary_region.cxx +++ b/src/mesh/boundary_region.cxx @@ -1,6 +1,5 @@ #include -#include #include #include From 9e651aa6a7c5a662ef0a690645d28a6c97b9799e Mon Sep 17 00:00:00 2001 From: tomc271 Date: Thu, 21 Mar 2024 09:00:44 +0000 Subject: [PATCH 371/491] More [[maybe_unused]] attributes and unused #include directives. --- src/field/vector2d.cxx | 10 ++++++---- src/field/vector3d.cxx | 6 +++--- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/field/vector2d.cxx b/src/field/vector2d.cxx index 55658e3111..18460c69d8 100644 --- a/src/field/vector2d.cxx +++ b/src/field/vector2d.cxx @@ -28,8 +28,6 @@ * **************************************************************************/ -#include - #include #include #include @@ -67,7 +65,9 @@ void Vector2D::toCovariant() { Mesh* localmesh = getMesh(); if (location == CELL_VSHIFT) { - Coordinates *metric_x, *metric_y, *metric_z; + Coordinates *metric_x; + [[maybe_unused]] Coordinates *metric_y; + [[maybe_unused]] Coordinates *metric_z; metric_x = localmesh->getCoordinates(CELL_XLOW); metric_y = localmesh->getCoordinates(CELL_YLOW); metric_z = localmesh->getCoordinates(CELL_ZLOW); @@ -123,7 +123,9 @@ void Vector2D::toContravariant() { Mesh* localmesh = getMesh(); if (location == CELL_VSHIFT) { - Coordinates *metric_x, *metric_y, *metric_z; + Coordinates *metric_x; + [[maybe_unused]] Coordinates *metric_y; + [[maybe_unused]] Coordinates *metric_z; metric_x = localmesh->getCoordinates(CELL_XLOW); metric_y = localmesh->getCoordinates(CELL_YLOW); diff --git a/src/field/vector3d.cxx b/src/field/vector3d.cxx index 7a0403cfab..88f2809ba1 100644 --- a/src/field/vector3d.cxx +++ b/src/field/vector3d.cxx @@ -28,8 +28,6 @@ * **************************************************************************/ -#include - #include #include #include @@ -124,7 +122,9 @@ void Vector3D::toContravariant() { Mesh* localmesh = getMesh(); if (location == CELL_VSHIFT) { - Coordinates *metric_x, *metric_y, *metric_z; + Coordinates *metric_x; + [[maybe_unused]] Coordinates *metric_y; + [[maybe_unused]] Coordinates *metric_z; metric_x = localmesh->getCoordinates(CELL_XLOW); metric_y = localmesh->getCoordinates(CELL_YLOW); From ac9cad6d105786761334a44c253919f00b7e8e5f Mon Sep 17 00:00:00 2001 From: tomc271 Date: Thu, 21 Mar 2024 09:01:36 +0000 Subject: [PATCH 372/491] Use std::move() for constructor parameters. --- include/bout/boundary_region.hxx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/bout/boundary_region.hxx b/include/bout/boundary_region.hxx index 92aa79367e..0c55df5cf1 100644 --- a/include/bout/boundary_region.hxx +++ b/include/bout/boundary_region.hxx @@ -67,9 +67,9 @@ class BoundaryRegion : public BoundaryRegionBase { public: BoundaryRegion() = delete; BoundaryRegion(std::string name, BndryLoc loc, Mesh* passmesh = nullptr) - : BoundaryRegionBase(name, loc, passmesh) {} + : BoundaryRegionBase(std::move(name), loc, passmesh) {} BoundaryRegion(std::string name, int xd, int yd, Mesh* passmesh = nullptr) - : BoundaryRegionBase(name, passmesh), bx(xd), by(yd), width(2) {} + : BoundaryRegionBase(std::move(name), passmesh), bx(xd), by(yd), width(2) {} ~BoundaryRegion() override = default; int x, y; ///< Indices of the point in the boundary From 0dfafd42d10259079f32be638898ab6123664d33 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Thu, 21 Mar 2024 09:44:18 +0000 Subject: [PATCH 373/491] More unused #include directives. --- src/field/field.cxx | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/field/field.cxx b/src/field/field.cxx index e48a8f3ef7..eb913cd5b9 100644 --- a/src/field/field.cxx +++ b/src/field/field.cxx @@ -23,12 +23,9 @@ * **************************************************************************/ -#include #include #include #include -#include -#include #include Field::Field(Mesh* localmesh, CELL_LOC location_in, DirectionTypes directions_in) From 53b181eef382395aa0f8872335b9f654545febaa Mon Sep 17 00:00:00 2001 From: tomc271 Date: Sat, 23 Mar 2024 09:58:21 +0000 Subject: [PATCH 374/491] No need to communicateAndCheckMeshSpacing() after creating Coordinates. --- include/bout/mesh.hxx | 1 - 1 file changed, 1 deletion(-) diff --git a/include/bout/mesh.hxx b/include/bout/mesh.hxx index 6c6ed78c8c..845cbdf747 100644 --- a/include/bout/mesh.hxx +++ b/include/bout/mesh.hxx @@ -632,7 +632,6 @@ public: // (circular dependency between Mesh and Coordinates) auto inserted = coords_map.emplace(location, nullptr); inserted.first->second = createDefaultCoordinates(location, false); - inserted.first->second->communicateAndCheckMeshSpacing(); return inserted.first->second; } From 1624876ada351305c4fce94a3798d9296ae56c22 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Sat, 23 Mar 2024 10:22:57 +0000 Subject: [PATCH 375/491] Revert "Move interpolateAndExtrapolate() to Mesh class." This reverts commit 37c90c5a6d27d15a043362a46f6fd783e5df28a1. It now has to be a class method, as it's called by other functions that are now member functions. --- include/bout/coordinates.hxx | 15 ++ include/bout/mesh.hxx | 14 +- src/mesh/coordinates.cxx | 289 +++++++++++++++++++++++++---------- src/mesh/mesh.cxx | 123 --------------- 4 files changed, 223 insertions(+), 218 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index 837e013a0f..fc8e2e8e1b 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -390,6 +390,21 @@ private: FieldMetric getDzFromOptionsFile(Mesh* mesh, const std::string& suffix) const; void fixZShiftGuards(Field2D& zShift) const; + + Field2D interpolateAndExtrapolate(const Field2D& f, CELL_LOC location, + bool extrapolate_x, bool extrapolate_y, + bool no_extra_interpolate, + ParallelTransform* UNUSED_pt, + const std::string& region) const; + +#if BOUT_USE_METRIC_3D + Field3D interpolateAndExtrapolate(const Field2D& f, CELL_LOC location, + bool extrapolate_x, bool extrapolate_y, + bool no_extra_interpolate, + ParallelTransform* UNUSED_pt, + const std::string& region) const; +#endif // BOUT_USE_METRIC_3D + }; /* diff --git a/include/bout/mesh.hxx b/include/bout/mesh.hxx index 845cbdf747..43a2e8d215 100644 --- a/include/bout/mesh.hxx +++ b/include/bout/mesh.hxx @@ -704,8 +704,7 @@ public: /// Determines the resultant output stagger location in derivatives /// given the input and output location. Also checks that the /// combination of locations is allowed - STAGGER getStagger(CELL_LOC inloc, CELL_LOC outloc, - CELL_LOC allowedloc) const; + STAGGER getStagger(CELL_LOC inloc, CELL_LOC outloc, CELL_LOC allowedloc) const; /// Determines the resultant output stagger location in derivatives /// given the input and output location. Also checks that the @@ -828,17 +827,6 @@ public: return region3D[RegionID.value()]; } - /// Interpolate a Field2D to a new CELL_LOC with interp_to. - /// Communicates to set internal guard cells. - /// Boundary guard cells are set by extrapolating from the grid, like - /// 'free_o3' boundary conditions - /// Corner guard cells are set to BoutNaN - Field2D interpolateAndExtrapolate(const Field2D& f, CELL_LOC location, - bool extrapolate_x, bool extrapolate_y, - bool no_extra_interpolate, - ParallelTransform* UNUSED(pt) = nullptr, - const std::string& region = "RGN_NOBNDRY"); - private: /// Allocates default Coordinates objects /// By default attempts to read staggered Coordinates from grid data source, diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 765d7d5c66..5770eed9a5 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -12,6 +12,7 @@ #include #include +#include #include "parallel/fci.hxx" #include "parallel/shiftedmetricinterp.hxx" @@ -22,8 +23,185 @@ // use anonymous namespace so this utility function is not available outside this file namespace { +// If the CELL_CENTRE variable was read, the staggered version is required to +// also exist for consistency +void checkStaggeredGet(Mesh* mesh, const std::string& name, const std::string& suffix) { + if (mesh->sourceHasVar(name) != mesh->sourceHasVar(name + suffix)) { + throw BoutException("Attempting to read staggered fields from grid, but " + name + + " is not present in both CELL_CENTRE and staggered versions."); + } +} + +// convenience function for repeated code +auto getAtLoc(Mesh* mesh, const std::string& name, const std::string& suffix, + CELL_LOC location, BoutReal default_value = 0.) { + + checkStaggeredGet(mesh, name, suffix); + return mesh->get(name + suffix, default_value, false, location); +} + +std::string getLocationSuffix(CELL_LOC location) { + switch (location) { + case CELL_CENTRE: { + return ""; + } + case CELL_XLOW: { + return "_xlow"; + } + case CELL_YLOW: { + return "_ylow"; + } + case CELL_ZLOW: { + // in 2D metric, same as CELL_CENTRE + return bout::build::use_metric_3d ? "_zlow" : ""; + } + default: { + throw BoutException( + "Incorrect location passed to " + "Coordinates(Mesh*,const CELL_LOC,const Coordinates*) constructor."); + } + } +} + +} // anonymous namespace + +template +// Use sendY()/sendX() and wait() instead of Mesh::communicate() to ensure we +// don't try to calculate parallel slices as Coordinates are not constructed yet +void Coordinates::communicate(T& t, Ts... ts) const { + FieldGroup g(t, ts...); + auto h = t.getMesh()->sendY(g); + t.getMesh()->wait(h); + h = t.getMesh()->sendX(g); + t.getMesh()->wait(h); +} + +/// Interpolate a Field2D to a new CELL_LOC with interp_to. +/// Communicates to set internal guard cells. +/// Boundary guard cells are set by extrapolating from the grid, like +/// 'free_o3' boundary conditions +/// Corner guard cells are set to BoutNaN +Field2D Coordinates::interpolateAndExtrapolate(const Field2D& f, CELL_LOC location, + bool extrapolate_x, bool extrapolate_y, + bool no_extra_interpolate, + ParallelTransform* UNUSED(pt) = nullptr, + const std::string& region = "RGN_NOBNDRY") const { + + Mesh* localmesh = f.getMesh(); + Field2D result = interp_to(f, location, region); + // Ensure result's data is unique. Otherwise result might be a duplicate of + // f (if no interpolation is needed, e.g. if interpolation is in the + // z-direction); then f would be communicated. Since this function is used + // on geometrical quantities that might not be periodic in y even on closed + // field lines (due to dependence on integrated shear), we don't want to + // communicate f. We will sort out result's boundary guard cells below, but + // not f's so we don't want to change f. + result.allocate(); + communicate(result); + + // Extrapolate into boundaries (if requested) so that differential geometry + // terms can be interpolated if necessary + // Note: cannot use applyBoundary("free_o3") here because applyBoundary() + // would try to create a new Coordinates object since we have not finished + // initializing yet, leading to an infinite recursion. + // Also, here we interpolate for the boundary points at xstart/ystart and + // (xend+1)/(yend+1) instead of extrapolating. + for (auto& bndry : localmesh->getBoundaries()) { + if ((extrapolate_x and bndry->bx != 0) or (extrapolate_y and bndry->by != 0)) { + int extrap_start = 0; + if (not no_extra_interpolate) { + // Can use no_extra_interpolate argument to skip the extra interpolation when we + // want to extrapolate the Christoffel symbol terms which come from derivatives so + // don't have the extra point set already + if ((location == CELL_XLOW) && (bndry->bx > 0)) { + extrap_start = 1; + } else if ((location == CELL_YLOW) && (bndry->by > 0)) { + extrap_start = 1; + } + } + for (bndry->first(); !bndry->isDone(); bndry->next1d()) { + // interpolate extra boundary point that is missed by interp_to, if + // necessary. + // Only interpolate this point if we are actually changing location. E.g. + // when we use this function to extrapolate J and Bxy on staggered grids, + // this point should already be set correctly because the metric + // components have been interpolated to here. + if (extrap_start > 0 and f.getLocation() != location) { + ASSERT1(bndry->bx == 0 or localmesh->xstart > 1) + ASSERT1(bndry->by == 0 or localmesh->ystart > 1) + // note that either bx or by is >0 here + result(bndry->x, bndry->y) = + (9. + * (f(bndry->x - bndry->bx, bndry->y - bndry->by) + + f(bndry->x, bndry->y)) + - f(bndry->x - 2 * bndry->bx, bndry->y - 2 * bndry->by) + - f(bndry->x + bndry->bx, bndry->y + bndry->by)) + / 16.; + } + + // set boundary guard cells + if ((bndry->bx != 0 && localmesh->GlobalNx - 2 * bndry->width >= 3) + || (bndry->by != 0 + && localmesh->GlobalNy - localmesh->numberOfYBoundaries() * bndry->width + >= 3)) { + if (bndry->bx != 0 && localmesh->LocalNx == 1 && bndry->width == 1) { + throw BoutException( + "Not enough points in the x-direction on this " + "processor for extrapolation needed to use staggered grids. " + "Increase number of x-guard cells MXG or decrease number of " + "processors in the x-direction NXPE."); + } + if (bndry->by != 0 && localmesh->LocalNy == 1 && bndry->width == 1) { + throw BoutException( + "Not enough points in the y-direction on this " + "processor for extrapolation needed to use staggered grids. " + "Increase number of y-guard cells MYG or decrease number of " + "processors in the y-direction NYPE."); + } + // extrapolate into boundary guard cells if there are enough grid points + for (int i = extrap_start; i < bndry->width; i++) { + int xi = bndry->x + i * bndry->bx; + int yi = bndry->y + i * bndry->by; + result(xi, yi) = 3.0 * result(xi - bndry->bx, yi - bndry->by) + - 3.0 * result(xi - 2 * bndry->bx, yi - 2 * bndry->by) + + result(xi - 3 * bndry->bx, yi - 3 * bndry->by); + } + } else { + // not enough grid points to extrapolate, set equal to last grid point + for (int i = extrap_start; i < bndry->width; i++) { + result(bndry->x + i * bndry->bx, bndry->y + i * bndry->by) = + result(bndry->x - bndry->bx, bndry->y - bndry->by); + } + } + } + } + } +#if CHECK > 0 + if (not( + // if include_corner_cells=true, then we extrapolate valid data into the + // corner cells if they are not already filled + localmesh->include_corner_cells + + // if we are not extrapolating at all, the corner cells should contain valid + // data + or (not extrapolate_x and not extrapolate_y))) { + // Invalidate corner guard cells + for (int i = 0; i < localmesh->xstart; i++) { + for (int j = 0; j < localmesh->ystart; j++) { + result(i, j) = BoutNaN; + result(i, localmesh->LocalNy - 1 - j) = BoutNaN; + result(localmesh->LocalNx - 1 - i, j) = BoutNaN; + result(localmesh->LocalNx - 1 - i, localmesh->LocalNy - 1 - j) = BoutNaN; + } + } + } +#endif + + return result; +} + #if BOUT_USE_METRIC_3D -Field3D interpolateAndExtrapolate(const Field3D& f_, CELL_LOC location, +Field3D Coordinates::interpolateAndExtrapolate(const Field3D& f_, CELL_LOC location, bool extrapolate_x, bool extrapolate_y, bool no_extra_interpolate, ParallelTransform* pt_) { @@ -178,65 +356,12 @@ Field3D interpolateAndExtrapolate(const Field3D& f_, CELL_LOC location, } #endif // BOUT_USE_METRIC_3D -// If the CELL_CENTRE variable was read, the staggered version is required to -// also exist for consistency -void checkStaggeredGet(Mesh* mesh, const std::string& name, const std::string& suffix) { - if (mesh->sourceHasVar(name) != mesh->sourceHasVar(name + suffix)) { - throw BoutException("Attempting to read staggered fields from grid, but " + name - + " is not present in both CELL_CENTRE and staggered versions."); - } -} - -// convenience function for repeated code -auto getAtLoc(Mesh* mesh, const std::string& name, const std::string& suffix, - CELL_LOC location, BoutReal default_value = 0.) { - - checkStaggeredGet(mesh, name, suffix); - return mesh->get(name + suffix, default_value, false, location); -} - -std::string getLocationSuffix(CELL_LOC location) { - switch (location) { - case CELL_CENTRE: { - return ""; - } - case CELL_XLOW: { - return "_xlow"; - } - case CELL_YLOW: { - return "_ylow"; - } - case CELL_ZLOW: { - // in 2D metric, same as CELL_CENTRE - return bout::build::use_metric_3d ? "_zlow" : ""; - } - default: { - throw BoutException( - "Incorrect location passed to " - "Coordinates(Mesh*,const CELL_LOC,const Coordinates*) constructor."); - } - } -} - -} // anonymous namespace - -template -// Use sendY()/sendX() and wait() instead of Mesh::communicate() to ensure we -// don't try to calculate parallel slices as Coordinates are not constructed yet -void Coordinates::communicate(T& t, Ts... ts) const { - FieldGroup g(t, ts...); - auto h = t.getMesh()->sendY(g); - t.getMesh()->wait(h); - h = t.getMesh()->sendX(g); - t.getMesh()->wait(h); -} - // Utility function for fixing up guard cells of zShift void Coordinates::fixZShiftGuards(Field2D& zShift) const { auto* localmesh = zShift.getMesh(); // extrapolate into boundary guard cells if necessary - zShift = localmesh->interpolateAndExtrapolate( + zShift = interpolateAndExtrapolate( zShift, zShift.getLocation(), not localmesh->sourceHasXBoundaryGuards(), not localmesh->sourceHasYBoundaryGuards(), false); @@ -336,7 +461,7 @@ void Coordinates::interpolateFieldsFromOtherCoordinates(Options* mesh_options, std::function const interpolateAndExtrapolate_function = [this](const FieldMetric& component) { - return localmesh->interpolateAndExtrapolate(component, location, true, true, + return interpolateAndExtrapolate(component, location, true, true, false, transform.get()); }; @@ -349,9 +474,9 @@ void Coordinates::interpolateFieldsFromOtherCoordinates(Options* mesh_options, checkContravariant(); checkCovariant(); - setJ(localmesh->interpolateAndExtrapolate(coords_in->J(), location, true, true, false, + setJ(interpolateAndExtrapolate(coords_in->J(), location, true, true, false, transform.get())); - setBxy(localmesh->interpolateAndExtrapolate(coords_in->Bxy(), location, true, true, + setBxy(interpolateAndExtrapolate(coords_in->Bxy(), location, true, true, false, transform.get())); bout::checkFinite(J(), "The Jacobian", "RGN_NOCORNERS"); @@ -359,11 +484,11 @@ void Coordinates::interpolateFieldsFromOtherCoordinates(Options* mesh_options, bout::checkFinite(Bxy(), "Bxy", "RGN_NOCORNERS"); bout::checkPositive(Bxy(), "Bxy", "RGN_NOCORNERS"); - ShiftTorsion_ = localmesh->interpolateAndExtrapolate( + ShiftTorsion_ = interpolateAndExtrapolate( coords_in->ShiftTorsion(), location, true, true, false, transform.get()); if (localmesh->IncIntShear) { - IntShiftTorsion_ = localmesh->interpolateAndExtrapolate( + IntShiftTorsion_ = interpolateAndExtrapolate( coords_in->IntShiftTorsion(), location, true, true, false, transform.get()); } @@ -378,12 +503,12 @@ void Coordinates::interpolateFieldsFromOtherCoordinates(Options* mesh_options, setParallelTransform(mesh_options); - dx_ = localmesh->interpolateAndExtrapolate(coords_in->dx(), location, true, true, false, + dx_ = interpolateAndExtrapolate(coords_in->dx(), location, true, true, false, transform.get()); - dy_ = localmesh->interpolateAndExtrapolate(coords_in->dy(), location, true, true, false, + dy_ = interpolateAndExtrapolate(coords_in->dy(), location, true, true, false, transform.get()); // not really needed - we have used dz already ... - dz_ = localmesh->interpolateAndExtrapolate(coords_in->dz(), location, true, true, false, + dz_ = interpolateAndExtrapolate(coords_in->dz(), location, true, true, false, transform.get()); } @@ -412,11 +537,11 @@ void Coordinates::setBoundaryCells(Options* mesh_options, const std::string& suf // required early for differentiation. setParallelTransform(mesh_options); - dz_ = localmesh->interpolateAndExtrapolate(dz_, location, extrapolate_x, extrapolate_y, + dz_ = interpolateAndExtrapolate(dz_, location, extrapolate_x, extrapolate_y, false, transform.get()); dx_ = getAtLocOrUnaligned(localmesh, "dx", 1.0, suffix, location); - dx_ = localmesh->interpolateAndExtrapolate(dx_, location, extrapolate_x, extrapolate_y, + dx_ = interpolateAndExtrapolate(dx_, location, extrapolate_x, extrapolate_y, false, transform.get()); if (localmesh->periodicX) { @@ -424,7 +549,7 @@ void Coordinates::setBoundaryCells(Options* mesh_options, const std::string& suf } dy_ = getAtLocOrUnaligned(localmesh, "dy", 1.0, suffix, location); - dy_ = localmesh->interpolateAndExtrapolate(dy_, location, extrapolate_x, extrapolate_y, + dy_ = interpolateAndExtrapolate(dy_, location, extrapolate_x, extrapolate_y, false, transform.get()); // grid data source has staggered fields, so read instead of interpolating @@ -445,7 +570,7 @@ void Coordinates::setBoundaryCells(Options* mesh_options, const std::string& suf std::function const interpolateAndExtrapolate_function = [this, extrapolate_y, extrapolate_x](const FieldMetric& component) { - return localmesh->interpolateAndExtrapolate( + return interpolateAndExtrapolate( component, location, extrapolate_x, extrapolate_y, false, transform.get()); }; applyToContravariantMetricTensor(interpolateAndExtrapolate_function); @@ -505,7 +630,7 @@ void Coordinates::setBoundaryCells(Options* mesh_options, const std::string& suf // More robust to extrapolate derived quantities directly, rather than // deriving from extrapolated covariant metric components - setJ(localmesh->interpolateAndExtrapolate(J(), location, extrapolate_x, extrapolate_y, + setJ(interpolateAndExtrapolate(J(), location, extrapolate_x, extrapolate_y, false, transform.get())); // Check jacobian @@ -528,7 +653,7 @@ void Coordinates::setBoundaryCells(Options* mesh_options, const std::string& suf output_warn.write("\tMaximum difference in Bxy is {:e}\n", max(abs(Bxy() - Bcalc))); } - setBxy(localmesh->interpolateAndExtrapolate(Bxy(), location, extrapolate_x, + setBxy(interpolateAndExtrapolate(Bxy(), location, extrapolate_x, extrapolate_y, false, transform.get())); // Check Bxy @@ -542,7 +667,7 @@ void Coordinates::setBoundaryCells(Options* mesh_options, const std::string& suf } else { const auto shift_torsion = getAtLoc(localmesh, "ShiftTorsion", suffix, location, 0.0); } - ShiftTorsion_ = (localmesh->interpolateAndExtrapolate( + ShiftTorsion_ = (interpolateAndExtrapolate( ShiftTorsion(), location, extrapolate_x, extrapolate_y, false, transform.get())); if (!localmesh->IncIntShear) { @@ -555,7 +680,7 @@ void Coordinates::setBoundaryCells(Options* mesh_options, const std::string& suf const auto shift_torsion = getAtLoc(localmesh, "IntShiftTorsion", suffix, location, 0.0); } - setIntShiftTorsion(localmesh->interpolateAndExtrapolate( + setIntShiftTorsion(interpolateAndExtrapolate( IntShiftTorsion(), location, extrapolate_x, extrapolate_y, false, transform.get())); } @@ -694,12 +819,12 @@ void Coordinates::correctionForNonUniformMeshes(bool force_interpolate_from_cent d1_dx_ = bout::derivatives::index::DDX(1. / dx()); // d/di(1/dx) communicate(d1_dx_); - d1_dx_ = localmesh->interpolateAndExtrapolate(d1_dx_, location, true, true, true, + d1_dx_ = interpolateAndExtrapolate(d1_dx_, location, true, true, true, transform.get()); } else { d2x.setLocation(location); // set boundary cells if necessary - d2x = localmesh->interpolateAndExtrapolate(d2x, location, extrapolate_x, + d2x = interpolateAndExtrapolate(d2x, location, extrapolate_x, extrapolate_y, false, transform.get()); d1_dx_ = -d2x / (dx() * dx()); @@ -711,12 +836,12 @@ void Coordinates::correctionForNonUniformMeshes(bool force_interpolate_from_cent d1_dy_ = bout::derivatives::index::DDY(1. / dy()); // d/di(1/dy) communicate(d1_dy_); - d1_dy_ = localmesh->interpolateAndExtrapolate(d1_dy_, location, true, true, true, + d1_dy_ = interpolateAndExtrapolate(d1_dy_, location, true, true, true, transform.get()); } else { d2y.setLocation(location); // set boundary cells if necessary - d2y = localmesh->interpolateAndExtrapolate(d2y, location, extrapolate_x, + d2y = interpolateAndExtrapolate(d2y, location, extrapolate_x, extrapolate_y, false, transform.get()); d1_dy_ = -d2y / (dy() * dy()); @@ -728,12 +853,12 @@ void Coordinates::correctionForNonUniformMeshes(bool force_interpolate_from_cent "Calculating from dz\n"); d1_dz_ = bout::derivatives::index::DDZ(1. / dz()); communicate(d1_dz_); - d1_dz_ = localmesh->interpolateAndExtrapolate(d1_dz_, location, true, true, true, + d1_dz_ = interpolateAndExtrapolate(d1_dz_, location, true, true, true, transform.get()); } else { d2z.setLocation(location); // set boundary cells if necessary - d2z = localmesh->interpolateAndExtrapolate(d2z, location, extrapolate_x, + d2z = interpolateAndExtrapolate(d2z, location, extrapolate_x, extrapolate_y, false, transform.get()); d1_dz_ = -d2z / (dz() * dz()); @@ -761,7 +886,7 @@ void Coordinates::extrapolateChristoffelSymbols() { std::function const interpolateAndExtrapolate_function = [this](const FieldMetric& component) { - return localmesh->interpolateAndExtrapolate(component, location, true, true, + return interpolateAndExtrapolate(component, location, true, true, false, transform.get()); }; @@ -775,11 +900,11 @@ void Coordinates::communicateGValues() const { void Coordinates::extrapolateGValues() { - setG1(localmesh->interpolateAndExtrapolate(G1(), location, true, true, true, + setG1(interpolateAndExtrapolate(G1(), location, true, true, true, transform.get())); - setG2(localmesh->interpolateAndExtrapolate(G2(), location, true, true, true, + setG2(interpolateAndExtrapolate(G2(), location, true, true, true, transform.get())); - setG3(localmesh->interpolateAndExtrapolate(G3(), location, true, true, true, + setG3(interpolateAndExtrapolate(G3(), location, true, true, true, transform.get())); } @@ -864,7 +989,7 @@ void Coordinates::setParallelTransform(Options* mesh_options) { fixZShiftGuards(zShift_centre); - zShift = localmesh->interpolateAndExtrapolate(zShift_centre, location, true, true, + zShift = interpolateAndExtrapolate(zShift_centre, location, true, true, false, transform.get()); } diff --git a/src/mesh/mesh.cxx b/src/mesh/mesh.cxx index aa8a481452..e8eae8f48a 100644 --- a/src/mesh/mesh.cxx +++ b/src/mesh/mesh.cxx @@ -9,129 +9,6 @@ #include #include -#include "bout/interpolation.hxx" - -/// Interpolate a Field2D to a new CELL_LOC with interp_to. -/// Communicates to set internal guard cells. -/// Boundary guard cells are set by extrapolating from the grid, like -/// 'free_o3' boundary conditions -/// Corner guard cells are set to BoutNaN -Field2D Mesh::interpolateAndExtrapolate(const Field2D& f, CELL_LOC location, - bool extrapolate_x, bool extrapolate_y, - bool no_extra_interpolate, - ParallelTransform* UNUSED(pt), - const std::string& region) { - - Field2D result = interp_to(f, location, region); - // Ensure result's data is unique. Otherwise result might be a duplicate of - // f (if no interpolation is needed, e.g. if interpolation is in the - // z-direction); then f would be communicated. Since this function is used - // on geometrical quantities that might not be periodic in y even on closed - // field lines (due to dependence on integrated shear), we don't want to - // communicate f. We will sort out result's boundary guard cells below, but - // not f's so we don't want to change f. - result.allocate(); - communicate(result); - - // Extrapolate into boundaries (if requested) so that differential geometry - // terms can be interpolated if necessary - // Note: cannot use applyBoundary("free_o3") here because applyBoundary() - // would try to create a new Coordinates object since we have not finished - // initializing yet, leading to an infinite recursion. - // Also, here we interpolate for the boundary points at xstart/ystart and - // (xend+1)/(yend+1) instead of extrapolating. - for (auto& bndry : getBoundaries()) { - if ((extrapolate_x and bndry->bx != 0) or (extrapolate_y and bndry->by != 0)) { - int extrap_start = 0; - if (not no_extra_interpolate) { - // Can use no_extra_interpolate argument to skip the extra interpolation when we - // want to extrapolate the Christoffel symbol terms which come from derivatives so - // don't have the extra point set already - if ((location == CELL_XLOW) && (bndry->bx > 0)) { - extrap_start = 1; - } else if ((location == CELL_YLOW) && (bndry->by > 0)) { - extrap_start = 1; - } - } - for (bndry->first(); !bndry->isDone(); bndry->next1d()) { - // interpolate extra boundary point that is missed by interp_to, if - // necessary. - // Only interpolate this point if we are actually changing location. E.g. - // when we use this function to extrapolate J and Bxy on staggered grids, - // this point should already be set correctly because the metric - // components have been interpolated to here. - if (extrap_start > 0 and f.getLocation() != location) { - ASSERT1(bndry->bx == 0 or xstart > 1); - ASSERT1(bndry->by == 0 or ystart > 1); - // note that either bx or by is >0 here - result(bndry->x, bndry->y) = - (9. - * (f(bndry->x - bndry->bx, bndry->y - bndry->by) - + f(bndry->x, bndry->y)) - - f(bndry->x - 2 * bndry->bx, bndry->y - 2 * bndry->by) - - f(bndry->x + bndry->bx, bndry->y + bndry->by)) - / 16.; - } - - // set boundary guard cells - if ((bndry->bx != 0 && GlobalNx - 2 * bndry->width >= 3) - || (bndry->by != 0 && GlobalNy - numberOfYBoundaries() * bndry->width >= 3)) { - if (bndry->bx != 0 && LocalNx == 1 && bndry->width == 1) { - throw BoutException( - "Not enough points in the x-direction on this " - "processor for extrapolation needed to use staggered grids. " - "Increase number of x-guard cells MXG or decrease number of " - "processors in the x-direction NXPE."); - } - if (bndry->by != 0 && LocalNy == 1 && bndry->width == 1) { - throw BoutException( - "Not enough points in the y-direction on this " - "processor for extrapolation needed to use staggered grids. " - "Increase number of y-guard cells MYG or decrease number of " - "processors in the y-direction NYPE."); - } - // extrapolate into boundary guard cells if there are enough grid points - for (int i = extrap_start; i < bndry->width; i++) { - int xi = bndry->x + i * bndry->bx; - int yi = bndry->y + i * bndry->by; - result(xi, yi) = 3.0 * result(xi - bndry->bx, yi - bndry->by) - - 3.0 * result(xi - 2 * bndry->bx, yi - 2 * bndry->by) - + result(xi - 3 * bndry->bx, yi - 3 * bndry->by); - } - } else { - // not enough grid points to extrapolate, set equal to last grid point - for (int i = extrap_start; i < bndry->width; i++) { - result(bndry->x + i * bndry->bx, bndry->y + i * bndry->by) = - result(bndry->x - bndry->bx, bndry->y - bndry->by); - } - } - } - } - } -#if CHECK > 0 - if (not( - // if include_corner_cells=true, then we extrapolate valid data into the - // corner cells if they are not already filled - include_corner_cells - - // if we are not extrapolating at all, the corner cells should contain valid - // data - or (not extrapolate_x and not extrapolate_y))) { - // Invalidate corner guard cells - for (int i = 0; i < xstart; i++) { - for (int j = 0; j < ystart; j++) { - result(i, j) = BoutNaN; - result(i, LocalNy - 1 - j) = BoutNaN; - result(LocalNx - 1 - i, j) = BoutNaN; - result(LocalNx - 1 - i, LocalNy - 1 - j) = BoutNaN; - } - } - } -#endif - - return result; -} - MeshFactory::ReturnType MeshFactory::create(Options* options, GridDataSource* source) const { return create(getType(options), options, source); From e998baadc1d9df09a93d840873e64fea617dfea3 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Sat, 23 Mar 2024 16:25:27 +0000 Subject: [PATCH 376/491] Fix using FieldMetric = Field3D. --- include/bout/metric_tensor.hxx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/bout/metric_tensor.hxx b/include/bout/metric_tensor.hxx index fe6a4b9bf5..157b6faadf 100644 --- a/include/bout/metric_tensor.hxx +++ b/include/bout/metric_tensor.hxx @@ -10,7 +10,7 @@ class MetricTensor { public: #if BOUT_USE_METRIC_3D - using FieldMetric = Field + using FieldMetric = Field3D; #else using FieldMetric = Field2D; #endif From b50a5e268922de57417dcf378104d31c2f2da753 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Sat, 23 Mar 2024 16:37:25 +0000 Subject: [PATCH 377/491] Fix Field3D Coordinates::interpolateAndExtrapolate method. --- include/bout/coordinates.hxx | 19 +++--- src/mesh/coordinates.cxx | 123 +++++++++++++++++------------------ 2 files changed, 69 insertions(+), 73 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index fc8e2e8e1b..1a5e3abc19 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -392,19 +392,18 @@ private: void fixZShiftGuards(Field2D& zShift) const; Field2D interpolateAndExtrapolate(const Field2D& f, CELL_LOC location, - bool extrapolate_x, bool extrapolate_y, - bool no_extra_interpolate, - ParallelTransform* UNUSED_pt, - const std::string& region) const; + bool extrapolate_x, bool extrapolate_y, + bool no_extra_interpolate, + ParallelTransform* UNUSED_pt, + const std::string& region) const; #if BOUT_USE_METRIC_3D - Field3D interpolateAndExtrapolate(const Field2D& f, CELL_LOC location, - bool extrapolate_x, bool extrapolate_y, - bool no_extra_interpolate, - ParallelTransform* UNUSED_pt, - const std::string& region) const; -#endif // BOUT_USE_METRIC_3D + Field3D interpolateAndExtrapolate(const Field3D& f_, CELL_LOC location, + bool extrapolate_x, bool extrapolate_y, + bool no_extra_interpolate, + ParallelTransform* pt_) const; +#endif // BOUT_USE_METRIC_3D }; /* diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 5770eed9a5..6f5d39e4b3 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -81,11 +81,10 @@ void Coordinates::communicate(T& t, Ts... ts) const { /// Boundary guard cells are set by extrapolating from the grid, like /// 'free_o3' boundary conditions /// Corner guard cells are set to BoutNaN -Field2D Coordinates::interpolateAndExtrapolate(const Field2D& f, CELL_LOC location, - bool extrapolate_x, bool extrapolate_y, - bool no_extra_interpolate, - ParallelTransform* UNUSED(pt) = nullptr, - const std::string& region = "RGN_NOBNDRY") const { +Field2D Coordinates::interpolateAndExtrapolate( + const Field2D& f, CELL_LOC location, bool extrapolate_x, bool extrapolate_y, + bool no_extra_interpolate, ParallelTransform* UNUSED(pt) = nullptr, + const std::string& region = "RGN_NOBNDRY") const { Mesh* localmesh = f.getMesh(); Field2D result = interp_to(f, location, region); @@ -202,8 +201,9 @@ Field2D Coordinates::interpolateAndExtrapolate(const Field2D& f, CELL_LOC locati #if BOUT_USE_METRIC_3D Field3D Coordinates::interpolateAndExtrapolate(const Field3D& f_, CELL_LOC location, - bool extrapolate_x, bool extrapolate_y, - bool no_extra_interpolate, ParallelTransform* pt_) { + bool extrapolate_x, bool extrapolate_y, + bool no_extra_interpolate, + ParallelTransform* pt_) const { Mesh* localmesh = f_.getMesh(); Field3D result; @@ -361,9 +361,9 @@ void Coordinates::fixZShiftGuards(Field2D& zShift) const { auto* localmesh = zShift.getMesh(); // extrapolate into boundary guard cells if necessary - zShift = interpolateAndExtrapolate( - zShift, zShift.getLocation(), not localmesh->sourceHasXBoundaryGuards(), - not localmesh->sourceHasYBoundaryGuards(), false); + zShift = interpolateAndExtrapolate(zShift, zShift.getLocation(), + not localmesh->sourceHasXBoundaryGuards(), + not localmesh->sourceHasYBoundaryGuards(), false); // make sure zShift has been communicated communicate(zShift); @@ -461,8 +461,8 @@ void Coordinates::interpolateFieldsFromOtherCoordinates(Options* mesh_options, std::function const interpolateAndExtrapolate_function = [this](const FieldMetric& component) { - return interpolateAndExtrapolate(component, location, true, true, - false, transform.get()); + return interpolateAndExtrapolate(component, location, true, true, false, + transform.get()); }; setContravariantMetricTensor(coords_in->getContravariantMetricTensor()); @@ -475,21 +475,21 @@ void Coordinates::interpolateFieldsFromOtherCoordinates(Options* mesh_options, checkCovariant(); setJ(interpolateAndExtrapolate(coords_in->J(), location, true, true, false, - transform.get())); - setBxy(interpolateAndExtrapolate(coords_in->Bxy(), location, true, true, - false, transform.get())); + transform.get())); + setBxy(interpolateAndExtrapolate(coords_in->Bxy(), location, true, true, false, + transform.get())); bout::checkFinite(J(), "The Jacobian", "RGN_NOCORNERS"); bout::checkPositive(J(), "The Jacobian", "RGN_NOCORNERS"); bout::checkFinite(Bxy(), "Bxy", "RGN_NOCORNERS"); bout::checkPositive(Bxy(), "Bxy", "RGN_NOCORNERS"); - ShiftTorsion_ = interpolateAndExtrapolate( - coords_in->ShiftTorsion(), location, true, true, false, transform.get()); + ShiftTorsion_ = interpolateAndExtrapolate(coords_in->ShiftTorsion(), location, true, + true, false, transform.get()); if (localmesh->IncIntShear) { - IntShiftTorsion_ = interpolateAndExtrapolate( - coords_in->IntShiftTorsion(), location, true, true, false, transform.get()); + IntShiftTorsion_ = interpolateAndExtrapolate(coords_in->IntShiftTorsion(), location, + true, true, false, transform.get()); } if (isUniform(coords_in->dz())) { @@ -504,12 +504,12 @@ void Coordinates::interpolateFieldsFromOtherCoordinates(Options* mesh_options, setParallelTransform(mesh_options); dx_ = interpolateAndExtrapolate(coords_in->dx(), location, true, true, false, - transform.get()); + transform.get()); dy_ = interpolateAndExtrapolate(coords_in->dy(), location, true, true, false, - transform.get()); + transform.get()); // not really needed - we have used dz already ... dz_ = interpolateAndExtrapolate(coords_in->dz(), location, true, true, false, - transform.get()); + transform.get()); } // Note: If boundary cells were not loaded from the grid file, use @@ -537,20 +537,20 @@ void Coordinates::setBoundaryCells(Options* mesh_options, const std::string& suf // required early for differentiation. setParallelTransform(mesh_options); - dz_ = interpolateAndExtrapolate(dz_, location, extrapolate_x, extrapolate_y, - false, transform.get()); + dz_ = interpolateAndExtrapolate(dz_, location, extrapolate_x, extrapolate_y, false, + transform.get()); dx_ = getAtLocOrUnaligned(localmesh, "dx", 1.0, suffix, location); - dx_ = interpolateAndExtrapolate(dx_, location, extrapolate_x, extrapolate_y, - false, transform.get()); + dx_ = interpolateAndExtrapolate(dx_, location, extrapolate_x, extrapolate_y, false, + transform.get()); if (localmesh->periodicX) { communicate(dx_); } dy_ = getAtLocOrUnaligned(localmesh, "dy", 1.0, suffix, location); - dy_ = interpolateAndExtrapolate(dy_, location, extrapolate_x, extrapolate_y, - false, transform.get()); + dy_ = interpolateAndExtrapolate(dy_, location, extrapolate_x, extrapolate_y, false, + transform.get()); // grid data source has staggered fields, so read instead of interpolating // Diagonal components of metric tensor g^{ij} (default to 1) @@ -568,11 +568,11 @@ void Coordinates::setBoundaryCells(Options* mesh_options, const std::string& suf // deriving from extrapolated covariant metric components std::function const - interpolateAndExtrapolate_function = [this, extrapolate_y, - extrapolate_x](const FieldMetric& component) { - return interpolateAndExtrapolate( - component, location, extrapolate_x, extrapolate_y, false, transform.get()); - }; + interpolateAndExtrapolate_function = + [this, extrapolate_y, extrapolate_x](const FieldMetric& component) { + return interpolateAndExtrapolate(component, location, extrapolate_x, + extrapolate_y, false, transform.get()); + }; applyToContravariantMetricTensor(interpolateAndExtrapolate_function); // Check input metrics @@ -630,8 +630,8 @@ void Coordinates::setBoundaryCells(Options* mesh_options, const std::string& suf // More robust to extrapolate derived quantities directly, rather than // deriving from extrapolated covariant metric components - setJ(interpolateAndExtrapolate(J(), location, extrapolate_x, extrapolate_y, - false, transform.get())); + setJ(interpolateAndExtrapolate(J(), location, extrapolate_x, extrapolate_y, false, + transform.get())); // Check jacobian bout::checkFinite(J(), "J" + suffix, "RGN_NOCORNERS"); @@ -653,8 +653,8 @@ void Coordinates::setBoundaryCells(Options* mesh_options, const std::string& suf output_warn.write("\tMaximum difference in Bxy is {:e}\n", max(abs(Bxy() - Bcalc))); } - setBxy(interpolateAndExtrapolate(Bxy(), location, extrapolate_x, - extrapolate_y, false, transform.get())); + setBxy(interpolateAndExtrapolate(Bxy(), location, extrapolate_x, extrapolate_y, false, + transform.get())); // Check Bxy bout::checkFinite(Bxy(), "Bxy" + suffix, "RGN_NOCORNERS"); @@ -667,8 +667,8 @@ void Coordinates::setBoundaryCells(Options* mesh_options, const std::string& suf } else { const auto shift_torsion = getAtLoc(localmesh, "ShiftTorsion", suffix, location, 0.0); } - ShiftTorsion_ = (interpolateAndExtrapolate( - ShiftTorsion(), location, extrapolate_x, extrapolate_y, false, transform.get())); + ShiftTorsion_ = (interpolateAndExtrapolate(ShiftTorsion(), location, extrapolate_x, + extrapolate_y, false, transform.get())); if (!localmesh->IncIntShear) { return; @@ -680,8 +680,8 @@ void Coordinates::setBoundaryCells(Options* mesh_options, const std::string& suf const auto shift_torsion = getAtLoc(localmesh, "IntShiftTorsion", suffix, location, 0.0); } - setIntShiftTorsion(interpolateAndExtrapolate( - IntShiftTorsion(), location, extrapolate_x, extrapolate_y, false, transform.get())); + setIntShiftTorsion(interpolateAndExtrapolate(IntShiftTorsion(), location, extrapolate_x, + extrapolate_y, false, transform.get())); } FieldMetric Coordinates::getDzFromOptionsFile(Mesh* mesh, @@ -819,13 +819,13 @@ void Coordinates::correctionForNonUniformMeshes(bool force_interpolate_from_cent d1_dx_ = bout::derivatives::index::DDX(1. / dx()); // d/di(1/dx) communicate(d1_dx_); - d1_dx_ = interpolateAndExtrapolate(d1_dx_, location, true, true, true, - transform.get()); + d1_dx_ = + interpolateAndExtrapolate(d1_dx_, location, true, true, true, transform.get()); } else { d2x.setLocation(location); // set boundary cells if necessary - d2x = interpolateAndExtrapolate(d2x, location, extrapolate_x, - extrapolate_y, false, transform.get()); + d2x = interpolateAndExtrapolate(d2x, location, extrapolate_x, extrapolate_y, false, + transform.get()); d1_dx_ = -d2x / (dx() * dx()); } @@ -836,13 +836,13 @@ void Coordinates::correctionForNonUniformMeshes(bool force_interpolate_from_cent d1_dy_ = bout::derivatives::index::DDY(1. / dy()); // d/di(1/dy) communicate(d1_dy_); - d1_dy_ = interpolateAndExtrapolate(d1_dy_, location, true, true, true, - transform.get()); + d1_dy_ = + interpolateAndExtrapolate(d1_dy_, location, true, true, true, transform.get()); } else { d2y.setLocation(location); // set boundary cells if necessary - d2y = interpolateAndExtrapolate(d2y, location, extrapolate_x, - extrapolate_y, false, transform.get()); + d2y = interpolateAndExtrapolate(d2y, location, extrapolate_x, extrapolate_y, false, + transform.get()); d1_dy_ = -d2y / (dy() * dy()); } @@ -853,13 +853,13 @@ void Coordinates::correctionForNonUniformMeshes(bool force_interpolate_from_cent "Calculating from dz\n"); d1_dz_ = bout::derivatives::index::DDZ(1. / dz()); communicate(d1_dz_); - d1_dz_ = interpolateAndExtrapolate(d1_dz_, location, true, true, true, - transform.get()); + d1_dz_ = + interpolateAndExtrapolate(d1_dz_, location, true, true, true, transform.get()); } else { d2z.setLocation(location); // set boundary cells if necessary - d2z = interpolateAndExtrapolate(d2z, location, extrapolate_x, - extrapolate_y, false, transform.get()); + d2z = interpolateAndExtrapolate(d2z, location, extrapolate_x, extrapolate_y, false, + transform.get()); d1_dz_ = -d2z / (dz() * dz()); } @@ -886,8 +886,8 @@ void Coordinates::extrapolateChristoffelSymbols() { std::function const interpolateAndExtrapolate_function = [this](const FieldMetric& component) { - return interpolateAndExtrapolate(component, location, true, true, - false, transform.get()); + return interpolateAndExtrapolate(component, location, true, true, false, + transform.get()); }; applyToChristoffelSymbols(interpolateAndExtrapolate_function); @@ -900,12 +900,9 @@ void Coordinates::communicateGValues() const { void Coordinates::extrapolateGValues() { - setG1(interpolateAndExtrapolate(G1(), location, true, true, true, - transform.get())); - setG2(interpolateAndExtrapolate(G2(), location, true, true, true, - transform.get())); - setG3(interpolateAndExtrapolate(G3(), location, true, true, true, - transform.get())); + setG1(interpolateAndExtrapolate(G1(), location, true, true, true, transform.get())); + setG2(interpolateAndExtrapolate(G2(), location, true, true, true, transform.get())); + setG3(interpolateAndExtrapolate(G3(), location, true, true, true, transform.get())); } MetricTensor::FieldMetric Coordinates::recalculateJacobian() const { @@ -989,8 +986,8 @@ void Coordinates::setParallelTransform(Options* mesh_options) { fixZShiftGuards(zShift_centre); - zShift = interpolateAndExtrapolate(zShift_centre, location, true, true, - false, transform.get()); + zShift = interpolateAndExtrapolate(zShift_centre, location, true, true, false, + transform.get()); } fixZShiftGuards(zShift); From 1786e049cbcb8b01eaad99b8568c2aab5af84536 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Sat, 23 Mar 2024 16:54:31 +0000 Subject: [PATCH 378/491] Add 3D version of setDy() for the BOUT_USE_METRIC_3D case. --- include/bout/coordinates.hxx | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index 1a5e3abc19..0b0cbb0f06 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -84,7 +84,11 @@ public: void setDy(FieldMetric dy) { dy_ = std::move(dy); } void setDz(FieldMetric dz) { dz_ = std::move(dz); } +#if BOUT_USE_METRIC_3D + void setDy(BoutReal value, int x, int y, int z) { dy_(x, y, z) = value; } +#else void setDy(BoutReal value, int x, int y) { dy_(x, y) = value; } +#endif void setD1_dx(FieldMetric d1_dx) { d1_dx_ = std::move(d1_dx); } void setD1_dy(FieldMetric d1_dy) { d1_dy_ = std::move(d1_dy); } From a837b729c0dad105bce704d5c9d0f250899a10a3 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Sat, 23 Mar 2024 17:01:24 +0000 Subject: [PATCH 379/491] d2z can't be const to work with Mesh::get(). --- src/mesh/coordinates.cxx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 6f5d39e4b3..70749ccd60 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -737,7 +737,7 @@ const Field2D& Coordinates::zlength() const { zlength_cache = std::make_unique(0., localmesh); #if BOUT_USE_METRIC_3D - BOUT_FOR_SERIAL(i, dz.getRegion("RGN_ALL")) { (*zlength_cache)[i] += dz[i]; } + BOUT_FOR_SERIAL(i, dz().getRegion("RGN_ALL")) { (*zlength_cache)[i] += dz()[i]; } #else (*zlength_cache) = dz_ * nz; #endif @@ -800,7 +800,7 @@ void Coordinates::correctionForNonUniformMeshes(bool force_interpolate_from_cent FieldMetric d2x(localmesh); FieldMetric d2y(localmesh); - FieldMetric const d2z(localmesh); // d^2 x / d i^2 + FieldMetric d2z(localmesh); // d^2 x / d i^2 // Read correction for non-uniform meshes std::string const suffix = getLocationSuffix(location); @@ -848,7 +848,7 @@ void Coordinates::correctionForNonUniformMeshes(bool force_interpolate_from_cent } #if BOUT_USE_METRIC_3D - if (localmesh->get(d2z, "d2z" + suffix, 0.0, false)) { + if (localmesh->get(d2z, "d2z" + suffix, 0.0, false, location)) { output_warn.write("\tWARNING: differencing quantity 'd2z' not found. " "Calculating from dz\n"); d1_dz_ = bout::derivatives::index::DDZ(1. / dz()); From 99661618abe78e0bf253fdfea54baea1b8db0fe1 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Sat, 23 Mar 2024 17:02:30 +0000 Subject: [PATCH 380/491] Update BOUT_USE_METRIC_3D case to use metric tensor getters. --- include/bout/fv_ops.hxx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/bout/fv_ops.hxx b/include/bout/fv_ops.hxx index b1b34b361b..6e4160e290 100644 --- a/include/bout/fv_ops.hxx +++ b/include/bout/fv_ops.hxx @@ -264,7 +264,7 @@ const Field3D Div_par(const Field3D& f_in, const Field3D& v_in, // For right cell boundaries BoutReal common_factor = (coord->J()(i, j, k) + coord->J()(i, j + 1, k)) - / (sqrt(coord->g_22(i, j, k)) + sqrt(coord->g_22(i, j + 1, k))); + / (sqrt(coord->g_22()(i, j, k)) + sqrt(coord->g_22()(i, j + 1, k))); BoutReal flux_factor_rc = common_factor / (coord->dy()(i, j, k) * coord->J()(i, j, k)); @@ -273,7 +273,7 @@ const Field3D Div_par(const Field3D& f_in, const Field3D& v_in, // For left cell boundaries common_factor = (coord->J()(i, j, k) + coord->J()(i, j - 1, k)) - / (sqrt(coord->g_22(i, j, k)) + sqrt(coord->g_22(i, j - 1, k))); + / (sqrt(coord->g_22()(i, j, k)) + sqrt(coord->g_22()(i, j - 1, k))); BoutReal flux_factor_lc = common_factor / (coord->dy()(i, j, k) * coord->J()(i, j, k)); From e7a8e77d31b8a6c315850c2776e6e263e9aeee0a Mon Sep 17 00:00:00 2001 From: tomc271 Date: Sat, 23 Mar 2024 17:07:39 +0000 Subject: [PATCH 381/491] Return type of Grad_par() and Vpar_Grad_par() should be FieldMetric, not Field2D (as it could be either Field2D or Field3D). --- include/bout/coordinates.hxx | 2 +- src/mesh/coordinates.cxx | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index 0b0cbb0f06..8945d4494a 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -208,7 +208,7 @@ public: const std::string& region = "RGN_NOBNDRY") const; /// Gradient along magnetic field b.Grad(f) - Field2D Grad_par(const Field2D& var, CELL_LOC outloc = CELL_DEFAULT, + FieldMetric Grad_par(const Field2D& var, CELL_LOC outloc = CELL_DEFAULT, const std::string& method = "DEFAULT"); Field3D Grad_par(const Field3D& var, CELL_LOC outloc = CELL_DEFAULT, diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 70749ccd60..8bfbbf8517 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -1085,7 +1085,7 @@ Field3D Coordinates::DDZ(const Field3D& f, CELL_LOC outloc, const std::string& m ///////////////////////////////////////////////////////// // Parallel gradient -Field2D Coordinates::Grad_par(const Field2D& var, [[maybe_unused]] CELL_LOC outloc, +FieldMetric Coordinates::Grad_par(const Field2D& var, [[maybe_unused]] CELL_LOC outloc, const std::string& UNUSED(method)) { TRACE("Coordinates::Grad_par( Field2D )"); ASSERT1(location == outloc @@ -1106,7 +1106,7 @@ Field3D Coordinates::Grad_par(const Field3D& var, CELL_LOC outloc, // Vpar_Grad_par // vparallel times the parallel derivative along unperturbed B-field -Field2D Coordinates::Vpar_Grad_par(const Field2D& v, const Field2D& f, +FieldMetric Coordinates::Vpar_Grad_par(const Field2D& v, const Field2D& f, [[maybe_unused]] CELL_LOC outloc, const std::string& UNUSED(method)) { ASSERT1(location == outloc || (outloc == CELL_DEFAULT && location == f.getLocation())); From 4c33d869256e4c240b8524feacddeb03d8ba1666 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Sat, 23 Mar 2024 17:12:57 +0000 Subject: [PATCH 382/491] Add 3D version of setJ() for the BOUT_USE_METRIC_3D case. --- include/bout/coordinates.hxx | 5 +++++ src/mesh/coordinates.cxx | 10 ++++++++++ 2 files changed, 15 insertions(+) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index 8945d4494a..b8289dbcbb 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -147,7 +147,12 @@ public: const FieldMetric& Bxy() const { return Bxy_; } void setJ(const FieldMetric& J); + +#if BOUT_USE_METRIC_3D + void setJ(BoutReal value, int x, int y, int z); +#else void setJ(BoutReal value, int x, int y); +#endif void setBxy(FieldMetric Bxy); diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 8bfbbf8517..45173b30e4 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -1508,6 +1508,15 @@ void Coordinates::setJ(const FieldMetric& J) { jacobian_cache = std::move(ptr); } +#if BOUT_USE_METRIC_3D +void Coordinates::setJ(BoutReal value, int x, int y, int z) { + //TODO: Calculate J and check value is close + auto f = J(); + f(x, y, z) = value; + auto ptr = std::make_unique(f); + jacobian_cache = std::move(ptr); +} +#else void Coordinates::setJ(BoutReal value, int x, int y) { //TODO: Calculate J and check value is close auto f = J(); @@ -1515,6 +1524,7 @@ void Coordinates::setJ(BoutReal value, int x, int y) { auto ptr = std::make_unique(f); jacobian_cache = std::move(ptr); } +#endif void Coordinates::setBxy(FieldMetric Bxy) { //TODO: Calculate Bxy and check value is close From 44dff025403fe88cfb85a03e055a1480c7a49ad9 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Sat, 23 Mar 2024 17:22:18 +0000 Subject: [PATCH 383/491] Return type of the std::function parameter to MetricTensor::map should be FieldMetric, not Field2D (as it could be either Field2D or Field3D). --- include/bout/metric_tensor.hxx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/bout/metric_tensor.hxx b/include/bout/metric_tensor.hxx index 157b6faadf..4f9cc70999 100644 --- a/include/bout/metric_tensor.hxx +++ b/include/bout/metric_tensor.hxx @@ -53,7 +53,7 @@ public: MetricTensor inverse(const std::string& region = "RGN_ALL"); // Transforms the MetricTensor by applying the given function to every component - void map(const std::function& function); + void map(const std::function& function); MetricTensor applyToComponents( const std::function& function) const; From b46795ed99a409a99ae57db67347c8589faf39bc Mon Sep 17 00:00:00 2001 From: tomc271 Date: Sat, 23 Mar 2024 17:24:07 +0000 Subject: [PATCH 384/491] Return type of Mesh::get() should be FieldMetric, not Field2D (as it could be either Field2D or Field3D). --- src/mesh/mesh.cxx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mesh/mesh.cxx b/src/mesh/mesh.cxx index e8eae8f48a..ae03c3cff0 100644 --- a/src/mesh/mesh.cxx +++ b/src/mesh/mesh.cxx @@ -172,7 +172,7 @@ int Mesh::get(Field2D& var, const std::string& name, BoutReal def, bool communic return 0; } -Field2D Mesh::get(const std::string& name, BoutReal def, bool communicate, +FieldMetric Mesh::get(const std::string& name, BoutReal def, bool communicate, CELL_LOC location) { TRACE("Loading 2D field: Mesh::get(Field2D, {:s})", name); From 164660a8e8c60c6f93054ca123dc299b86d5b431 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Mon, 25 Mar 2024 14:49:15 +0000 Subject: [PATCH 385/491] Bug fix: Mesh::get() should return a Field3D in the BOUT_USE_METRIC_3D case. --- src/mesh/mesh.cxx | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/mesh/mesh.cxx b/src/mesh/mesh.cxx index ae03c3cff0..7f30d3f189 100644 --- a/src/mesh/mesh.cxx +++ b/src/mesh/mesh.cxx @@ -173,11 +173,11 @@ int Mesh::get(Field2D& var, const std::string& name, BoutReal def, bool communic } FieldMetric Mesh::get(const std::string& name, BoutReal def, bool communicate, - CELL_LOC location) { - TRACE("Loading 2D field: Mesh::get(Field2D, {:s})", name); - - Field2D var = Field2D(this, location); + CELL_LOC location) { + TRACE("Loading field: Mesh::get(FieldMetric, {:s})", name); + auto var = FieldMetric(this, location); + bool failed_to_get_from_GridDataSource = !source->get(this, var, name, def, location); if (source == nullptr or failed_to_get_from_GridDataSource) { // set val to default in source==nullptr too: From 3b09a2d1196994056ce16bcc65189ea208e8708d Mon Sep 17 00:00:00 2001 From: tomc271 Date: Sat, 23 Mar 2024 17:58:23 +0000 Subject: [PATCH 386/491] Fix updating of Bxy by splitParallelSlices(). --- tests/unit/test_extras.hxx | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/tests/unit/test_extras.hxx b/tests/unit/test_extras.hxx index 81a8829695..fb7a9734e6 100644 --- a/tests/unit/test_extras.hxx +++ b/tests/unit/test_extras.hxx @@ -451,7 +451,11 @@ public: test_coords->setD1_dy(0.2); test_coords->setD1_dz(0.0); #if BOUT_USE_METRIC_3D - test_coords->Bxy().splitParallelSlices(); + + FieldMetric mutable_Bxy = test_coords->Bxy(); + mutable_Bxy.splitParallelSlices(); + test_coords->setBxy(mutable_Bxy); + test_coords->Bxy().yup() = test_coords->Bxy().ydown() = test_coords->Bxy(); #endif @@ -496,7 +500,11 @@ public: test_coords_staggered->setD1_dy(0.2); test_coords_staggered->setD1_dz(0.0); #if BOUT_USE_METRIC_3D - test_coords_staggered->Bxy.splitParallelSlices(); + + mutable_Bxy = test_coords_staggered->Bxy(); + mutable_Bxy.splitParallelSlices(); + test_coords->setBxy(mutable_Bxy); + test_coords_staggered->Bxy.yup() = test_coords_staggered->Bxy.ydown() = test_coords_staggered->Bxy; #endif From 3e03b7638f6dac6d6410515aec26ba9da64e0223 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Sat, 23 Mar 2024 18:05:33 +0000 Subject: [PATCH 387/491] Fix updating of Bxy yup and ydown. --- tests/unit/test_extras.hxx | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/tests/unit/test_extras.hxx b/tests/unit/test_extras.hxx index fb7a9734e6..1b9d8bbb7a 100644 --- a/tests/unit/test_extras.hxx +++ b/tests/unit/test_extras.hxx @@ -456,7 +456,11 @@ public: mutable_Bxy.splitParallelSlices(); test_coords->setBxy(mutable_Bxy); - test_coords->Bxy().yup() = test_coords->Bxy().ydown() = test_coords->Bxy(); + mutable_Bxy = test_coords->Bxy(); + mutable_Bxy.yup() = test_coords->Bxy(); + mutable_Bxy.ydown() = test_coords->Bxy(); + test_coords->setBxy(mutable_Bxy); + #endif static_cast(bout::globals::mesh)->setCoordinates(test_coords); @@ -503,10 +507,13 @@ public: mutable_Bxy = test_coords_staggered->Bxy(); mutable_Bxy.splitParallelSlices(); - test_coords->setBxy(mutable_Bxy); + test_coords_staggered->setBxy(mutable_Bxy); - test_coords_staggered->Bxy.yup() = test_coords_staggered->Bxy.ydown() = - test_coords_staggered->Bxy; + mutable_Bxy = test_coords_staggered->Bxy(); + mutable_Bxy.yup() = test_coords_staggered->Bxy(); + mutable_Bxy.ydown() = test_coords_staggered->Bxy(); + test_coords_staggered->setBxy(mutable_Bxy); + #endif test_coords_staggered->setParallelTransform( From e551211f40a52968598ac83ab947f3ea0432ade0 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Sat, 23 Mar 2024 18:14:33 +0000 Subject: [PATCH 388/491] Fix updating of Bxy by splitParallelSlices(). --- tests/unit/mesh/test_coordinates_accessor.cxx | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/tests/unit/mesh/test_coordinates_accessor.cxx b/tests/unit/mesh/test_coordinates_accessor.cxx index c9efe9ec74..2738a8a61f 100644 --- a/tests/unit/mesh/test_coordinates_accessor.cxx +++ b/tests/unit/mesh/test_coordinates_accessor.cxx @@ -92,8 +92,13 @@ TEST_F(CoordinatesAccessorTest, ClearBoth) { coords.setD1_dy(0.1); coords.setD1_dz(0.1); #if BOUT_USE_METRIC_3D - coords.Bxy.splitParallelSlices(); + + FieldMetric mutable_Bxy = coords.Bxy(); + mutable_Bxy.splitParallelSlices(); + coords.setBxy(mutable_Bxy); + coords.Bxy.yup() = coords.Bxy.ydown() = coords.Bxy; + #endif CoordinatesAccessor acc(mesh->getCoordinates()); @@ -136,8 +141,13 @@ TEST_F(CoordinatesAccessorTest, ClearOneTwo) { coords.setD1_dy(0.1); coords.setD1_dz(0.1); #if BOUT_USE_METRIC_3D - coords.Bxy.splitParallelSlices(); + + FieldMetric mutable_Bxy = coords.Bxy(); + mutable_Bxy.splitParallelSlices(); + coords.setBxy(mutable_Bxy); + coords.Bxy.yup() = coords.Bxy.ydown() = coords.Bxy; + #endif CoordinatesAccessor acc(mesh->getCoordinates()); @@ -182,8 +192,13 @@ TEST_F(CoordinatesAccessorTest, ClearTwoOneNone) { coords.setD1_dy(0.1); coords.setD1_dz(0.1); #if BOUT_USE_METRIC_3D - coords.Bxy.splitParallelSlices(); + + FieldMetric mutable_Bxy = coords.Bxy(); + mutable_Bxy.splitParallelSlices(); + coords.setBxy(mutable_Bxy); + coords.Bxy.yup() = coords.Bxy.ydown() = coords.Bxy; + #endif CoordinatesAccessor acc(mesh->getCoordinates()); From 3642d6948f89e0d6ed4259fa55b1fb07cc1d7b20 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Sat, 23 Mar 2024 18:16:03 +0000 Subject: [PATCH 389/491] Fix updating of Bxy yup and ydown. --- tests/unit/mesh/test_coordinates_accessor.cxx | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/tests/unit/mesh/test_coordinates_accessor.cxx b/tests/unit/mesh/test_coordinates_accessor.cxx index 2738a8a61f..0f4d67fb65 100644 --- a/tests/unit/mesh/test_coordinates_accessor.cxx +++ b/tests/unit/mesh/test_coordinates_accessor.cxx @@ -97,7 +97,10 @@ TEST_F(CoordinatesAccessorTest, ClearBoth) { mutable_Bxy.splitParallelSlices(); coords.setBxy(mutable_Bxy); - coords.Bxy.yup() = coords.Bxy.ydown() = coords.Bxy; + mutable_Bxy = coords.Bxy(); + mutable_Bxy.yup() = coords.Bxy(); + mutable_Bxy.ydown() = coords.Bxy(); + coords.setBxy(mutable_Bxy); #endif @@ -146,7 +149,10 @@ TEST_F(CoordinatesAccessorTest, ClearOneTwo) { mutable_Bxy.splitParallelSlices(); coords.setBxy(mutable_Bxy); - coords.Bxy.yup() = coords.Bxy.ydown() = coords.Bxy; + mutable_Bxy = coords.Bxy(); + mutable_Bxy.yup() = coords.Bxy(); + mutable_Bxy.ydown() = coords.Bxy(); + coords.setBxy(mutable_Bxy); #endif @@ -197,7 +203,10 @@ TEST_F(CoordinatesAccessorTest, ClearTwoOneNone) { mutable_Bxy.splitParallelSlices(); coords.setBxy(mutable_Bxy); - coords.Bxy.yup() = coords.Bxy.ydown() = coords.Bxy; + mutable_Bxy = coords.Bxy(); + mutable_Bxy.yup() = coords.Bxy(); + mutable_Bxy.ydown() = coords.Bxy(); + coords.setBxy(mutable_Bxy); #endif From 9df8bf82b420c7d27b3b24e3e0ef7b0c5707841b Mon Sep 17 00:00:00 2001 From: tomc271 Date: Sat, 23 Mar 2024 22:42:24 +0000 Subject: [PATCH 390/491] G1, G2, G3 are now calculated in the GValues constructor, so no need to set to arbitrary values in the tests. Also the constructor calls the differential operators, which (in the BOUT_USE_METRIC_3D case) require that the ParallelTransform has already been set. --- tests/unit/mesh/test_coordinates_accessor.cxx | 12 ------------ tests/unit/test_extras.hxx | 13 +------------ 2 files changed, 1 insertion(+), 24 deletions(-) diff --git a/tests/unit/mesh/test_coordinates_accessor.cxx b/tests/unit/mesh/test_coordinates_accessor.cxx index 0f4d67fb65..62f3db6104 100644 --- a/tests/unit/mesh/test_coordinates_accessor.cxx +++ b/tests/unit/mesh/test_coordinates_accessor.cxx @@ -83,10 +83,6 @@ TEST_F(CoordinatesAccessorTest, ClearBoth) { FieldMetric{0.0}, // g_23 FieldMetric{0.0}, // ShiftTorsion FieldMetric{0.0}}; // IntShiftTorsion - // Need to set geometry information - coords.setG1(0.2); - coords.setG2(0.2); - coords.setG3(0.2); coords.setNon_uniform(true); coords.setD1_dx(0.1); coords.setD1_dy(0.1); @@ -135,10 +131,6 @@ TEST_F(CoordinatesAccessorTest, ClearOneTwo) { FieldMetric{0.0}, // g_23 FieldMetric{0.0}, // ShiftTorsion FieldMetric{0.0}}; // IntShiftTorsion - // Need to set geometry information - coords.setG1(0.2); - coords.setG2(0.2); - coords.setG3(0.2); coords.setNon_uniform(true); coords.setD1_dx(0.1); coords.setD1_dy(0.1); @@ -189,10 +181,6 @@ TEST_F(CoordinatesAccessorTest, ClearTwoOneNone) { FieldMetric{0.0}, // g_23 FieldMetric{0.0}, // ShiftTorsion FieldMetric{0.0}}; // IntShiftTorsion - // Need to set geometry information - coords.setG1(0.2); - coords.setG2(0.2); - coords.setG3(0.2); coords.setNon_uniform(true); coords.setD1_dx(0.1); coords.setD1_dy(0.1); diff --git a/tests/unit/test_extras.hxx b/tests/unit/test_extras.hxx index 1b9d8bbb7a..203f19efc4 100644 --- a/tests/unit/test_extras.hxx +++ b/tests/unit/test_extras.hxx @@ -439,12 +439,6 @@ public: Field2D{0.0}, Field2D{0.0}, Field2D{1.0}, Field2D{1.0}, Field2D{1.0}, Field2D{0.0}, Field2D{0.0}, Field2D{0.0}, Field2D{0.0}, Field2D{0.0}); - // Set some auxilliary variables - // Note: For testing these are set to non-zero values - test_coords->setG1(0.1); - test_coords->setG2(0.1); - test_coords->setG3(0.1); - // Set nonuniform corrections test_coords->setNon_uniform(true); test_coords->setD1_dx(0.2); @@ -493,11 +487,6 @@ public: Field2D{0.0, mesh_staggered}, Field2D{0.0, mesh_staggered}, Field2D{0.0, mesh_staggered}); - // Set some auxilliary variables - test_coords_staggered->setG1(0.1); - test_coords_staggered->setG2(0.1); - test_coords_staggered->setG3(0.1); - // Set nonuniform corrections test_coords_staggered->setNon_uniform(true); test_coords_staggered->setD1_dx(0.2); @@ -513,7 +502,7 @@ public: mutable_Bxy.yup() = test_coords_staggered->Bxy(); mutable_Bxy.ydown() = test_coords_staggered->Bxy(); test_coords_staggered->setBxy(mutable_Bxy); - + #endif test_coords_staggered->setParallelTransform( From 002539f48788476d239f11399905403368fa721d Mon Sep 17 00:00:00 2001 From: tomc271 Date: Sat, 23 Mar 2024 23:07:19 +0000 Subject: [PATCH 391/491] Remove call to obsolete method coords._setmembers(). --- tools/pylib/_boutpp_build/boutpp.pyx.jinja | 1 - 1 file changed, 1 deletion(-) diff --git a/tools/pylib/_boutpp_build/boutpp.pyx.jinja b/tools/pylib/_boutpp_build/boutpp.pyx.jinja index ce97dc2e14..24290001f3 100644 --- a/tools/pylib/_boutpp_build/boutpp.pyx.jinja +++ b/tools/pylib/_boutpp_build/boutpp.pyx.jinja @@ -818,7 +818,6 @@ cdef Mesh meshFromPtr(c.Mesh * obj): cdef Coordinates coordsFromPtr(c.Coordinates * obj): coords = Coordinates() coords.cobj = obj - coords._setmembers() coords.isSelfOwned = False return coords From 459e598bb1e123b7da6103a2a9ab3b2af5447cfd Mon Sep 17 00:00:00 2001 From: tomc271 Date: Mon, 25 Mar 2024 09:52:05 +0000 Subject: [PATCH 392/491] Refactoring to DRY up Mesh::createDefaultCoordinates(). --- src/mesh/mesh.cxx | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/src/mesh/mesh.cxx b/src/mesh/mesh.cxx index 7f30d3f189..4fb7e74e05 100644 --- a/src/mesh/mesh.cxx +++ b/src/mesh/mesh.cxx @@ -575,17 +575,16 @@ std::shared_ptr Mesh::createDefaultCoordinates(const CELL_LOC location, bool recalculate_staggered, bool force_interpolate_from_centre) { + std::shared_ptr new_coordinates; if (location == CELL_CENTRE || location == CELL_DEFAULT) { // Initialize coordinates from input - const auto new_coordinates = std::make_shared(this, options); - new_coordinates->recalculateAndReset(recalculate_staggered, - force_interpolate_from_centre); - return new_coordinates; - } - // Interpolate coordinates from CELL_CENTRE version - const auto new_coordinates = - std::make_shared(this, options, location, getCoordinates(CELL_CENTRE), - force_interpolate_from_centre); + new_coordinates = std::make_shared(this, options); + } else { + // Interpolate coordinates from CELL_CENTRE version + new_coordinates = std::make_shared(this, options, location, + getCoordinates(CELL_CENTRE), + force_interpolate_from_centre); + } new_coordinates->recalculateAndReset(recalculate_staggered, force_interpolate_from_centre); return new_coordinates; From ce2066bec692313f604792ae84bb8836abcc1e99 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Mon, 25 Mar 2024 10:07:40 +0000 Subject: [PATCH 393/491] Add new coordinates object to coords_map before calling recalculateAndReset(). Otherwise calculation of the Christoffel symbols and metric tensor will cause Mesh::getCoordinates() to be called (and fail). (This is because of the circular dependency between Mesh and Coordinates). --- include/bout/mesh.hxx | 11 ++++++++--- src/mesh/mesh.cxx | 13 +++++++++---- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/include/bout/mesh.hxx b/include/bout/mesh.hxx index 43a2e8d215..b751621a6a 100644 --- a/include/bout/mesh.hxx +++ b/include/bout/mesh.hxx @@ -631,7 +631,13 @@ public: // Note that this can't be allocated here due to incomplete type // (circular dependency between Mesh and Coordinates) auto inserted = coords_map.emplace(location, nullptr); - inserted.first->second = createDefaultCoordinates(location, false); + auto force_interpolate_from_centre = false; + inserted.first->second = createDefaultCoordinates(location, force_interpolate_from_centre); + + auto recalculate_staggered = false; + inserted.first->second->recalculateAndReset(recalculate_staggered, + force_interpolate_from_centre); + return inserted.first->second; } @@ -835,8 +841,7 @@ private: /// (useful if CELL_CENTRE Coordinates have been changed, so reading from file /// would not be correct). std::shared_ptr - createDefaultCoordinates(CELL_LOC location, bool recalculate_staggered = true, - bool force_interpolate_from_centre = false); + createDefaultCoordinates(CELL_LOC location, bool force_interpolate_from_centre = false); //Internal region related information std::map regionMap3D; diff --git a/src/mesh/mesh.cxx b/src/mesh/mesh.cxx index 4fb7e74e05..dcf986f4d1 100644 --- a/src/mesh/mesh.cxx +++ b/src/mesh/mesh.cxx @@ -572,7 +572,7 @@ int Mesh::localSizePerp() { } std::shared_ptr -Mesh::createDefaultCoordinates(const CELL_LOC location, bool recalculate_staggered, +Mesh::createDefaultCoordinates(const CELL_LOC location, bool force_interpolate_from_centre) { std::shared_ptr new_coordinates; @@ -585,8 +585,6 @@ Mesh::createDefaultCoordinates(const CELL_LOC location, bool recalculate_stagger getCoordinates(CELL_CENTRE), force_interpolate_from_centre); } - new_coordinates->recalculateAndReset(recalculate_staggered, - force_interpolate_from_centre); return new_coordinates; } @@ -787,7 +785,14 @@ void Mesh::recalculateStaggeredCoordinates() { continue; } - *coords_map[location] = std::move(*createDefaultCoordinates(location, false, true)); + auto force_interpolate_from_centre = true; + Coordinates& new_coordinates = + *createDefaultCoordinates(location, force_interpolate_from_centre); + *coords_map[location] = std::move(new_coordinates); + + auto recalculate_staggered = false; + new_coordinates.recalculateAndReset(recalculate_staggered, + force_interpolate_from_centre); } } From a0fc687296b4c9cdc3806cd1ff7228eaa71cc370 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Mon, 25 Mar 2024 10:45:42 +0000 Subject: [PATCH 394/491] Improve exception handling - ensure that transform is not null before attempting to use it in Coordinates::DDY(). --- src/mesh/coordinates.cxx | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 45173b30e4..b349f95058 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -1057,6 +1057,7 @@ Coordinates::FieldMetric Coordinates::DDY(const Field2D& f, CELL_LOC loc, Field3D Coordinates::DDY(const Field3D& f, CELL_LOC outloc, const std::string& method, const std::string& region) const { #if BOUT_USE_METRIC_3D + ASSERT0(transform != nullptr); if (!f.hasParallelSlices() and !transform->canToFromFieldAligned()) { Field3D f_parallel = f; transform->calcParallelSlices(f_parallel); @@ -1086,7 +1087,7 @@ Field3D Coordinates::DDZ(const Field3D& f, CELL_LOC outloc, const std::string& m // Parallel gradient FieldMetric Coordinates::Grad_par(const Field2D& var, [[maybe_unused]] CELL_LOC outloc, - const std::string& UNUSED(method)) { + const std::string& UNUSED(method)) { TRACE("Coordinates::Grad_par( Field2D )"); ASSERT1(location == outloc || (outloc == CELL_DEFAULT && location == var.getLocation())); @@ -1107,8 +1108,8 @@ Field3D Coordinates::Grad_par(const Field3D& var, CELL_LOC outloc, // vparallel times the parallel derivative along unperturbed B-field FieldMetric Coordinates::Vpar_Grad_par(const Field2D& v, const Field2D& f, - [[maybe_unused]] CELL_LOC outloc, - const std::string& UNUSED(method)) { + [[maybe_unused]] CELL_LOC outloc, + const std::string& UNUSED(method)) { ASSERT1(location == outloc || (outloc == CELL_DEFAULT && location == f.getLocation())); return VDDY(v, f) * invSg(); From 968497e50824b295d84fdcf7b04f12ed67811a4a Mon Sep 17 00:00:00 2001 From: tomc271 Date: Mon, 25 Mar 2024 11:20:20 +0000 Subject: [PATCH 395/491] Ensure transform is set even when Coordinates is instantiated with the constructor that takes no Options (Use the basic type, ParallelTransformIdentity, in this case). --- src/mesh/coordinates.cxx | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index b349f95058..a303da4356 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -423,7 +423,12 @@ Coordinates::Coordinates(Mesh* mesh, FieldMetric dx, FieldMetric dy, FieldMetric dy_(std::move(dy)), dz_(std::move(dz)), ShiftTorsion_(std::move(ShiftTorsion)), IntShiftTorsion_(std::move(IntShiftTorsion)), contravariantMetricTensor(g11, g22, g33, g12, g13, g23), - covariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23), Bxy_(std::move(Bxy)){}; + covariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23), Bxy_(std::move(Bxy)) { + + // required early for differentiation. + // Identity method i.e. no transform needed + transform = bout::utils::make_unique(*localmesh); +}; Coordinates::Coordinates(Mesh* mesh, Options* mesh_options, const CELL_LOC loc, const Coordinates* coords_in, bool force_interpolate_from_centre) From cefb4eebfa2e10e0a3dec1e3a30a98327f6390e2 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Tue, 26 Mar 2024 11:21:42 +0000 Subject: [PATCH 396/491] Need to ensure parallel transform is set before differential operators are used (and setParallelTransform() requires that dz is already set!) --- src/mesh/coordinates.cxx | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index a303da4356..ab9f52eef4 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -464,6 +464,19 @@ Coordinates::Coordinates(Mesh* mesh, Options* mesh_options, const CELL_LOC loc, void Coordinates::interpolateFieldsFromOtherCoordinates(Options* mesh_options, const Coordinates* coords_in) { + // Need to ensure parallel transform is set before differential operators are used, + // but setParallelTransform() requires that dz is already set! + if (isUniform(coords_in->dz())) { + dz_ = coords_in->dz(); + dz_.setLocation(location); + } else { + throw BoutException( + "We are asked to transform dz to get dz before we have a transform, which might " + "require dz! \nPlease provide a dz for the staggered quantity!"); + } + + setParallelTransform(mesh_options); + std::function const interpolateAndExtrapolate_function = [this](const FieldMetric& component) { return interpolateAndExtrapolate(component, location, true, true, false, @@ -497,17 +510,6 @@ void Coordinates::interpolateFieldsFromOtherCoordinates(Options* mesh_options, true, true, false, transform.get()); } - if (isUniform(coords_in->dz())) { - dz_ = coords_in->dz(); - dz_.setLocation(location); - } else { - throw BoutException( - "We are asked to transform dz to get dz before we have a transform, which might " - "require dz! \nPlease provide a dz for the staggered quantity!"); - } - - setParallelTransform(mesh_options); - dx_ = interpolateAndExtrapolate(coords_in->dx(), location, true, true, false, transform.get()); dy_ = interpolateAndExtrapolate(coords_in->dy(), location, true, true, false, From b68f69da9f2d1c9398f8c5a4ad19731cf7baf55a Mon Sep 17 00:00:00 2001 From: tomc271 Date: Tue, 26 Mar 2024 12:40:52 +0000 Subject: [PATCH 397/491] Don't setLocation() of dz at this point, to avoid ASSERT1_FIELDS_COMPATIBLE(lhs, rhs) failing in Field3D operator/(const Field3D& lhs, const Field3D& rhs). --- src/mesh/coordinates.cxx | 1 - 1 file changed, 1 deletion(-) diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index ab9f52eef4..fea825ed94 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -468,7 +468,6 @@ void Coordinates::interpolateFieldsFromOtherCoordinates(Options* mesh_options, // but setParallelTransform() requires that dz is already set! if (isUniform(coords_in->dz())) { dz_ = coords_in->dz(); - dz_.setLocation(location); } else { throw BoutException( "We are asked to transform dz to get dz before we have a transform, which might " From dd0ee5c61445bbd96522be234fbcf8195ec1aa09 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Tue, 26 Mar 2024 15:22:08 +0000 Subject: [PATCH 398/491] Reorder to put the variable closer to its point of use. --- src/mesh/coordinates.cxx | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index fea825ed94..4f93bc5ff5 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -476,14 +476,13 @@ void Coordinates::interpolateFieldsFromOtherCoordinates(Options* mesh_options, setParallelTransform(mesh_options); + setContravariantMetricTensor(coords_in->getContravariantMetricTensor()); + std::function const interpolateAndExtrapolate_function = [this](const FieldMetric& component) { return interpolateAndExtrapolate(component, location, true, true, false, transform.get()); }; - - setContravariantMetricTensor(coords_in->getContravariantMetricTensor()); - applyToContravariantMetricTensor(interpolateAndExtrapolate_function); applyToCovariantMetricTensor(interpolateAndExtrapolate_function); From 2ea7e27cb36d466d46167c599de009c38e69ed10 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Tue, 26 Mar 2024 20:54:10 +0000 Subject: [PATCH 399/491] Fix type of dJ_dy in LaplaceHypre3d::updateMatrix3D() (3D not 2D). --- src/invert/laplace/impls/hypre3d/hypre3d_laplace.cxx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/invert/laplace/impls/hypre3d/hypre3d_laplace.cxx b/src/invert/laplace/impls/hypre3d/hypre3d_laplace.cxx index 9400acc706..f41894ad38 100644 --- a/src/invert/laplace/impls/hypre3d/hypre3d_laplace.cxx +++ b/src/invert/laplace/impls/hypre3d/hypre3d_laplace.cxx @@ -277,7 +277,7 @@ void LaplaceHypre3d::updateMatrix3D() { const Field3D dc_dx = issetC ? DDX(C2) : Field3D(); const Field3D dc_dy = issetC ? DDY(C2) : Field3D(); const Field3D dc_dz = issetC ? DDZ(C2) : Field3D(); - const Field2D dJ_dy = DDY(coords->J() / coords->g_22()); + const Field3D dJ_dy = DDY(coords->J() / coords->g_22()); // Set up the matrix for the internal points on the grid. // Boundary conditions were set in the constructor. From c0b4e0465496aab22683bb541d52572cd0d288df Mon Sep 17 00:00:00 2001 From: tomc271 Date: Fri, 29 Mar 2024 03:03:35 +0000 Subject: [PATCH 400/491] Disable "Laplacian solver in 2D (X-Y)" for the BOUT_USE_METRIC_3D case. --- tests/integrated/test-laplacexy2-hypre/CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/integrated/test-laplacexy2-hypre/CMakeLists.txt b/tests/integrated/test-laplacexy2-hypre/CMakeLists.txt index a787752ebc..6a719bfe7f 100644 --- a/tests/integrated/test-laplacexy2-hypre/CMakeLists.txt +++ b/tests/integrated/test-laplacexy2-hypre/CMakeLists.txt @@ -1,6 +1,7 @@ bout_add_integrated_test(test-laplacexy2-hypre SOURCES test-laplacexy.cxx REQUIRES BOUT_HAS_HYPRE + CONFLICTS BOUT_USE_METRIC_3D USE_RUNTEST USE_DATA_BOUT_INP ) From cee7596c9dd0e5023650fa3c4bfac4b107d76d04 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Fri, 29 Mar 2024 03:00:39 +0000 Subject: [PATCH 401/491] Fix test-laplacexz: Use a non-zero value for off-diagonal components that gives a valid inverse metric (i.e. all diagonal components positive). This became a problem once setting the contra- or co-variant metric tensor was made to trigger the calculation of the other representation. --- tests/integrated/test-laplacexz/test-laplacexz.cxx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/integrated/test-laplacexz/test-laplacexz.cxx b/tests/integrated/test-laplacexz/test-laplacexz.cxx index 5a6571d33b..75b92c3f30 100644 --- a/tests/integrated/test-laplacexz/test-laplacexz.cxx +++ b/tests/integrated/test-laplacexz/test-laplacexz.cxx @@ -20,7 +20,7 @@ int main(int argc, char** argv) { auto inv = LaplaceXZ::create(bout::globals::mesh); auto coord = bout::globals::mesh->getCoordinates(); - const auto g13 = 1.8; // test off-diagonal components with nonzero value + const auto g13 = 0.8; // test off-diagonal components with nonzero value coord->setContravariantMetricTensor(ContravariantMetricTensor( coord->g11(), coord->g22(), coord->g33(), coord->g12(), g13, coord->g23())); From 6232eb4e43ef6145993222db0e55e89c4da59591 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Thu, 28 Mar 2024 12:46:12 +0000 Subject: [PATCH 402/491] Apply clang-format changes --- include/bout/coordinates.hxx | 2 +- include/bout/fv_ops.hxx | 5 +++-- include/bout/mesh.hxx | 3 ++- src/field/vector2d.cxx | 12 ++++++------ src/field/vector3d.cxx | 6 +++--- src/mesh/impls/bout/boutmesh.cxx | 4 +++- src/mesh/mesh.cxx | 2 +- src/sys/optionsreader.cxx | 3 ++- 8 files changed, 21 insertions(+), 16 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index b8289dbcbb..39c3516c4d 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -214,7 +214,7 @@ public: /// Gradient along magnetic field b.Grad(f) FieldMetric Grad_par(const Field2D& var, CELL_LOC outloc = CELL_DEFAULT, - const std::string& method = "DEFAULT"); + const std::string& method = "DEFAULT"); Field3D Grad_par(const Field3D& var, CELL_LOC outloc = CELL_DEFAULT, const std::string& method = "DEFAULT"); diff --git a/include/bout/fv_ops.hxx b/include/bout/fv_ops.hxx index 6e4160e290..d57f93ce7c 100644 --- a/include/bout/fv_ops.hxx +++ b/include/bout/fv_ops.hxx @@ -272,8 +272,9 @@ const Field3D Div_par(const Field3D& f_in, const Field3D& v_in, common_factor / (coord->dy()(i, j + 1, k) * coord->J()(i, j + 1, k)); // For left cell boundaries - common_factor = (coord->J()(i, j, k) + coord->J()(i, j - 1, k)) - / (sqrt(coord->g_22()(i, j, k)) + sqrt(coord->g_22()(i, j - 1, k))); + common_factor = + (coord->J()(i, j, k) + coord->J()(i, j - 1, k)) + / (sqrt(coord->g_22()(i, j, k)) + sqrt(coord->g_22()(i, j - 1, k))); BoutReal flux_factor_lc = common_factor / (coord->dy()(i, j, k) * coord->J()(i, j, k)); diff --git a/include/bout/mesh.hxx b/include/bout/mesh.hxx index b751621a6a..4435e8403a 100644 --- a/include/bout/mesh.hxx +++ b/include/bout/mesh.hxx @@ -632,7 +632,8 @@ public: // (circular dependency between Mesh and Coordinates) auto inserted = coords_map.emplace(location, nullptr); auto force_interpolate_from_centre = false; - inserted.first->second = createDefaultCoordinates(location, force_interpolate_from_centre); + inserted.first->second = + createDefaultCoordinates(location, force_interpolate_from_centre); auto recalculate_staggered = false; inserted.first->second->recalculateAndReset(recalculate_staggered, diff --git a/src/field/vector2d.cxx b/src/field/vector2d.cxx index 18460c69d8..c6b0c776c7 100644 --- a/src/field/vector2d.cxx +++ b/src/field/vector2d.cxx @@ -65,9 +65,9 @@ void Vector2D::toCovariant() { Mesh* localmesh = getMesh(); if (location == CELL_VSHIFT) { - Coordinates *metric_x; - [[maybe_unused]] Coordinates *metric_y; - [[maybe_unused]] Coordinates *metric_z; + Coordinates* metric_x; + [[maybe_unused]] Coordinates* metric_y; + [[maybe_unused]] Coordinates* metric_z; metric_x = localmesh->getCoordinates(CELL_XLOW); metric_y = localmesh->getCoordinates(CELL_YLOW); metric_z = localmesh->getCoordinates(CELL_ZLOW); @@ -123,9 +123,9 @@ void Vector2D::toContravariant() { Mesh* localmesh = getMesh(); if (location == CELL_VSHIFT) { - Coordinates *metric_x; - [[maybe_unused]] Coordinates *metric_y; - [[maybe_unused]] Coordinates *metric_z; + Coordinates* metric_x; + [[maybe_unused]] Coordinates* metric_y; + [[maybe_unused]] Coordinates* metric_z; metric_x = localmesh->getCoordinates(CELL_XLOW); metric_y = localmesh->getCoordinates(CELL_YLOW); diff --git a/src/field/vector3d.cxx b/src/field/vector3d.cxx index 88f2809ba1..1cc6273eaa 100644 --- a/src/field/vector3d.cxx +++ b/src/field/vector3d.cxx @@ -122,9 +122,9 @@ void Vector3D::toContravariant() { Mesh* localmesh = getMesh(); if (location == CELL_VSHIFT) { - Coordinates *metric_x; - [[maybe_unused]] Coordinates *metric_y; - [[maybe_unused]] Coordinates *metric_z; + Coordinates* metric_x; + [[maybe_unused]] Coordinates* metric_y; + [[maybe_unused]] Coordinates* metric_z; metric_x = localmesh->getCoordinates(CELL_XLOW); metric_y = localmesh->getCoordinates(CELL_YLOW); diff --git a/src/mesh/impls/bout/boutmesh.cxx b/src/mesh/impls/bout/boutmesh.cxx index 7ff4365e05..9aa724b2fd 100644 --- a/src/mesh/impls/bout/boutmesh.cxx +++ b/src/mesh/impls/bout/boutmesh.cxx @@ -1702,7 +1702,9 @@ int BoutMesh::getLocalYIndexNoBoundaries(int yglobal) const { return yglobal - PE_YIND * MYSUB + MYG; } -[[maybe_unused]] int BoutMesh::YGLOBAL(int yloc, int yproc) const { return yloc + yproc * MYSUB - MYG; } +[[maybe_unused]] int BoutMesh::YGLOBAL(int yloc, int yproc) const { + return yloc + yproc * MYSUB - MYG; +} int BoutMesh::YLOCAL(int yglo, int yproc) const { return yglo - yproc * MYSUB + MYG; } diff --git a/src/mesh/mesh.cxx b/src/mesh/mesh.cxx index dcf986f4d1..8ed2f8a544 100644 --- a/src/mesh/mesh.cxx +++ b/src/mesh/mesh.cxx @@ -177,7 +177,7 @@ FieldMetric Mesh::get(const std::string& name, BoutReal def, bool communicate, TRACE("Loading field: Mesh::get(FieldMetric, {:s})", name); auto var = FieldMetric(this, location); - + bool failed_to_get_from_GridDataSource = !source->get(this, var, name, def, location); if (source == nullptr or failed_to_get_from_GridDataSource) { // set val to default in source==nullptr too: diff --git a/src/sys/optionsreader.cxx b/src/sys/optionsreader.cxx index 9862565863..fcbbddb0db 100644 --- a/src/sys/optionsreader.cxx +++ b/src/sys/optionsreader.cxx @@ -43,7 +43,8 @@ void OptionsReader::write(Options* options, const std::string& filename) { OptionINI{}.write(options, filename); } -[[maybe_unused]] void OptionsReader::parseCommandLine(Options* options, int argc, char** argv) { +[[maybe_unused]] void OptionsReader::parseCommandLine(Options* options, int argc, + char** argv) { return parseCommandLine(options, std::vector(argv, argv + argc)); } From 17f7097ad2e31b5ba7da50d209b753e49ba28ccf Mon Sep 17 00:00:00 2001 From: Tom C Date: Fri, 10 May 2024 12:21:21 +0100 Subject: [PATCH 403/491] include --- include/bout/metric_tensor.hxx | 1 + 1 file changed, 1 insertion(+) diff --git a/include/bout/metric_tensor.hxx b/include/bout/metric_tensor.hxx index 4f9cc70999..deacc20829 100644 --- a/include/bout/metric_tensor.hxx +++ b/include/bout/metric_tensor.hxx @@ -5,6 +5,7 @@ #include "bout/field2d.hxx" #include "bout/field3d.hxx" #include +#include class MetricTensor { From 10d0fdbbfdb10cb5a2f6df217a8867362351bc6e Mon Sep 17 00:00:00 2001 From: tomc271 Date: Sun, 12 May 2024 00:42:50 +0100 Subject: [PATCH 404/491] Add Coordinates::setMetricTensor() method for setting both co- and contravariant components. --- include/bout/coordinates.hxx | 3 +++ src/mesh/coordinates.cxx | 8 +++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index 39c3516c4d..8e193423d2 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -140,6 +140,9 @@ public: bool recalculate_staggered = true, bool force_interpolate_from_centre = false); + void setMetricTensor(ContravariantMetricTensor contravariant_metric_tensor, + CovariantMetricTensor covariant_metric_tensor); + ///< Coordinate system Jacobian, so volume of cell is J*dx*dy*dz const FieldMetric& J() const; diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 4f93bc5ff5..c73a3309da 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -476,7 +476,7 @@ void Coordinates::interpolateFieldsFromOtherCoordinates(Options* mesh_options, setParallelTransform(mesh_options); - setContravariantMetricTensor(coords_in->getContravariantMetricTensor()); + setMetricTensor(coords_in->getContravariantMetricTensor(), coords_in->getCovariantMetricTensor()); std::function const interpolateAndExtrapolate_function = [this](const FieldMetric& component) { @@ -1559,6 +1559,12 @@ void Coordinates::setCovariantMetricTensor(const CovariantMetricTensor& metric_t recalculateAndReset(recalculate_staggered, force_interpolate_from_centre); } +void Coordinates::setMetricTensor(ContravariantMetricTensor contravariant_metric_tensor, + CovariantMetricTensor covariant_metric_tensor) { + contravariantMetricTensor.setMetricTensor(contravariant_metric_tensor); + covariantMetricTensor.setMetricTensor(covariant_metric_tensor); +} + void Coordinates::applyToContravariantMetricTensor( const std::function& function) { contravariantMetricTensor.map(function); From 834f11a5e7b9c2acc4674760ad45ce2c6b38408f Mon Sep 17 00:00:00 2001 From: tomc271 Date: Mon, 13 May 2024 19:50:16 +0100 Subject: [PATCH 405/491] Call Mesh::communicate() rather than Coordinates::communicate() when multiple parameters are sent. --- src/mesh/coordinates.cxx | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index c73a3309da..a6c93de830 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -754,9 +754,8 @@ const Field2D& Coordinates::zlength() const { int Coordinates::communicateAndCheckMeshSpacing() const { TRACE("Coordinates::communicateAndCheckMeshSpacing"); - auto tmp = dx(); // TODO: There must be a better way than this! - communicate(tmp, dy(), dz(), g11(), g22(), g33(), g12(), g13(), g23(), g_11(), g_22(), - g_33(), g_12(), g_13(), g_23(), J(), Bxy()); + localmesh->communicate(dx(), dy(), dz(), g11(), g22(), g33(), g12(), g13(), g23(), g_11(), g_22(), g_33(), + g_12(), g_13(), g_23(), J(), Bxy()); output_progress.write("Calculating differential geometry terms\n"); @@ -872,8 +871,7 @@ void Coordinates::correctionForNonUniformMeshes(bool force_interpolate_from_cent d1_dz_ = 0; #endif - auto tmp = d1_dx(); // TODO: There must be a better way than this! - communicate(tmp, d1_dy(), d1_dz()); + localmesh->communicate(d1_dx(), d1_dy(), d1_dz()); } void Coordinates::extrapolateChristoffelSymbols() { @@ -900,7 +898,7 @@ void Coordinates::extrapolateChristoffelSymbols() { void Coordinates::communicateGValues() const { auto temp = G1(); // TODO: There must be a better way than this! - communicate(temp, G2(), G3()); + localmesh->communicate(temp, G2(), G3()); } void Coordinates::extrapolateGValues() { @@ -1580,7 +1578,7 @@ void Coordinates::communicateChristoffelSymbolTerms() const { output_progress.write("\tCommunicating connection terms\n"); auto tmp = G1_11(); // TODO: There must be a better way than this! - communicate(tmp, G1_22(), G1_33(), G1_12(), G1_13(), G1_23(), G2_11(), G2_22(), G2_33(), + localmesh->communicate(tmp, G1_22(), G1_33(), G1_12(), G1_13(), G1_23(), G2_11(), G2_22(), G2_33(), G2_12(), G2_13(), G2_23(), G3_11(), G3_22(), G3_33(), G3_12(), G3_13(), G3_23()); } From e045f2e8b161a67957f132c82a86176da531c179 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Tue, 14 May 2024 15:56:32 +0100 Subject: [PATCH 406/491] Add a version of Mesh::communicate() that takes const-qualified parameters. --- include/bout/mesh.hxx | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/include/bout/mesh.hxx b/include/bout/mesh.hxx index 9e0234dbcc..fe9c549628 100644 --- a/include/bout/mesh.hxx +++ b/include/bout/mesh.hxx @@ -285,6 +285,11 @@ public: FieldGroup g(ts...); communicate(g); } + template + void communicate(const Ts&... ts) { + FieldGroup g(ts...); + communicate(g); + } template void communicateXZ(Ts&... ts) { From 5ced89d23035d79ed28aff753e897519148d5790 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Tue, 14 May 2024 15:59:59 +0100 Subject: [PATCH 407/491] Allow FieldGroup to take const-qualified parameters. Some duplication of methods because there are still cases where it has to take non-const items. --- include/bout/fieldgroup.hxx | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/include/bout/fieldgroup.hxx b/include/bout/fieldgroup.hxx index 32bc75e557..98d05f5aa1 100644 --- a/include/bout/fieldgroup.hxx +++ b/include/bout/fieldgroup.hxx @@ -65,6 +65,10 @@ public: explicit FieldGroup(Ts&... ts) { add(ts...); } + template + explicit FieldGroup(const Ts&... ts) { + add(ts...); + } /// Copy contents of another FieldGroup \p other into this group. void add(const FieldGroup& other) { @@ -131,23 +135,47 @@ public: add(ts...); // Add the rest } + template + void add(const Field3D& t) { + auto temp = t; // Copy value to remove const qualifier (because std::vector can't take cv-qualified items) + add(temp); // Add using functions above + } + template void add(Field3D& t, Ts&... ts) { add(t); // Add the first using functions above add(ts...); // Add the rest } + template + void add(const Field3D& t, const Ts&... ts) { + auto temp = t; // Copy value to remove const qualifier (because std::vector can't take cv-qualified items) + add(temp); // Add the first using functions above + add(ts...); // Add the rest + } template void add(Vector3D& t, Ts&... ts) { add(t); // Add the first using functions above add(ts...); // Add the rest } + template + void add(const Vector3D& t, const Ts&... ts) { + auto temp = t; // Copy value to remove const qualifier (because std::vector can't take cv-qualified items) + add(temp); // Add the first using functions above + add(ts...); // Add the rest + } template void add(Vector2D& t, Ts&... ts) { add(t); // Add the first using functions above add(ts...); // Add the rest } + template + void add(const Vector2D& t, const Ts&... ts) { + auto temp = t; // Copy value to remove const qualifier (because std::vector can't take cv-qualified items) + add(temp); // Add the first using functions above + add(ts...); // Add the rest + } /// Return number of fields int size() const { return static_cast(fvec.size()); } From 2f7a6af0ccbbf7df9cfe2327f5d7ee5b11d9455c Mon Sep 17 00:00:00 2001 From: tomc271 Date: Tue, 14 May 2024 16:19:19 +0100 Subject: [PATCH 408/491] Add a version of Coordinates::communicate() that takes const-qualified parameters. --- include/bout/coordinates.hxx | 3 +++ src/mesh/coordinates.cxx | 11 +++++++++++ 2 files changed, 14 insertions(+) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index 8e193423d2..f012efbaa9 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -313,6 +313,9 @@ public: template void communicate(T& t, Ts... ts) const; + template + void communicate(const T& t, const Ts... ts) const; + private: int nz; // Size of mesh in Z. This is mesh->ngz-1 Mesh* localmesh; diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index a6c93de830..f85ae1e053 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -76,6 +76,17 @@ void Coordinates::communicate(T& t, Ts... ts) const { t.getMesh()->wait(h); } +template +// Use sendY()/sendX() and wait() instead of Mesh::communicate() to ensure we +// don't try to calculate parallel slices as Coordinates are not constructed yet +void Coordinates::communicate(const T& t, const Ts... ts) const { + FieldGroup g(t, ts...); + auto h = t.getMesh()->sendY(g); + t.getMesh()->wait(h); + h = t.getMesh()->sendX(g); + t.getMesh()->wait(h); +} + /// Interpolate a Field2D to a new CELL_LOC with interp_to. /// Communicates to set internal guard cells. /// Boundary guard cells are set by extrapolating from the grid, like From 22a6341551af8aa67912e1bc77d645ef0bcf792d Mon Sep 17 00:00:00 2001 From: tomc271 Date: Tue, 14 May 2024 12:21:29 +0100 Subject: [PATCH 409/491] Replace variadic template with two methods: void communicate(const Field2D& f) and void communicate(const Field3D& f). --- include/bout/coordinates.hxx | 13 +++++++------ src/mesh/coordinates.cxx | 32 +++++++++++++++----------------- 2 files changed, 22 insertions(+), 23 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index f012efbaa9..d97ac6c8e3 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -309,13 +309,14 @@ public: bool force_interpolate_from_centre); FieldMetric recalculateJacobian() const; + + void communicate(const Field2D& f) const; +#if BOUT_USE_METRIC_3D + // In this case we also need to be able to call with a Field3D + void communicate(const Field3D& f) const; +#endif - template - void communicate(T& t, Ts... ts) const; - - template - void communicate(const T& t, const Ts... ts) const; - + private: int nz; // Size of mesh in Z. This is mesh->ngz-1 Mesh* localmesh; diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index f85ae1e053..5b944175c8 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -65,27 +65,25 @@ std::string getLocationSuffix(CELL_LOC location) { } // anonymous namespace -template -// Use sendY()/sendX() and wait() instead of Mesh::communicate() to ensure we -// don't try to calculate parallel slices as Coordinates are not constructed yet -void Coordinates::communicate(T& t, Ts... ts) const { - FieldGroup g(t, ts...); - auto h = t.getMesh()->sendY(g); - t.getMesh()->wait(h); - h = t.getMesh()->sendX(g); - t.getMesh()->wait(h); -} -template // Use sendY()/sendX() and wait() instead of Mesh::communicate() to ensure we // don't try to calculate parallel slices as Coordinates are not constructed yet -void Coordinates::communicate(const T& t, const Ts... ts) const { - FieldGroup g(t, ts...); - auto h = t.getMesh()->sendY(g); - t.getMesh()->wait(h); - h = t.getMesh()->sendX(g); - t.getMesh()->wait(h); +void Coordinates::communicate(const Field2D& f) const { + FieldGroup g(f); + auto h = f.getMesh()->sendY(g); + f.getMesh()->wait(h); + h = f.getMesh()->sendX(g); + f.getMesh()->wait(h); } +#if BOUT_USE_METRIC_3D +void Coordinates::communicate(const Field3D& f) const { + FieldGroup g(f); + auto h = f.getMesh()->sendY(g); + f.getMesh()->wait(h); + h = f.getMesh()->sendX(g); + f.getMesh()->wait(h); +} +#endif /// Interpolate a Field2D to a new CELL_LOC with interp_to. /// Communicates to set internal guard cells. From 27b461bc6581b5bb1c4b66774a576665f53b1bcb Mon Sep 17 00:00:00 2001 From: tomc271 Date: Tue, 14 May 2024 16:58:32 +0100 Subject: [PATCH 410/491] This fixes the failure of tests `NotConstructableFromInt` and `NotConstructableFromInt`. --- include/bout/fieldgroup.hxx | 4 ---- 1 file changed, 4 deletions(-) diff --git a/include/bout/fieldgroup.hxx b/include/bout/fieldgroup.hxx index 98d05f5aa1..915486d134 100644 --- a/include/bout/fieldgroup.hxx +++ b/include/bout/fieldgroup.hxx @@ -65,10 +65,6 @@ public: explicit FieldGroup(Ts&... ts) { add(ts...); } - template - explicit FieldGroup(const Ts&... ts) { - add(ts...); - } /// Copy contents of another FieldGroup \p other into this group. void add(const FieldGroup& other) { From b53d092ffdce09824d40670c1a64baed0456fd4e Mon Sep 17 00:00:00 2001 From: tomc271 Date: Tue, 14 May 2024 17:40:15 +0100 Subject: [PATCH 411/491] Various Clang-Tidy suggestions. --- include/bout/coordinates.hxx | 18 +++++++++--------- src/mesh/coordinates.cxx | 18 +++++++++--------- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index d97ac6c8e3..e4c5a0f1e3 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -140,8 +140,8 @@ public: bool recalculate_staggered = true, bool force_interpolate_from_centre = false); - void setMetricTensor(ContravariantMetricTensor contravariant_metric_tensor, - CovariantMetricTensor covariant_metric_tensor); + void setMetricTensor(const ContravariantMetricTensor& contravariant_metric_tensor, + const CovariantMetricTensor& covariant_metric_tensor); ///< Coordinate system Jacobian, so volume of cell is J*dx*dy*dz const FieldMetric& J() const; @@ -310,13 +310,13 @@ public: FieldMetric recalculateJacobian() const; - void communicate(const Field2D& f) const; + static void communicate(const Field2D& f) ; #if BOUT_USE_METRIC_3D // In this case we also need to be able to call with a Field3D - void communicate(const Field3D& f) const; + static void communicate(const Field3D& f) ; #endif - + private: int nz; // Size of mesh in Z. This is mesh->ngz-1 Mesh* localmesh; @@ -407,17 +407,17 @@ private: void fixZShiftGuards(Field2D& zShift) const; - Field2D interpolateAndExtrapolate(const Field2D& f, CELL_LOC location, + static Field2D interpolateAndExtrapolate(const Field2D& f, CELL_LOC location, bool extrapolate_x, bool extrapolate_y, bool no_extra_interpolate, ParallelTransform* UNUSED_pt, - const std::string& region) const; + const std::string& region) ; #if BOUT_USE_METRIC_3D - Field3D interpolateAndExtrapolate(const Field3D& f_, CELL_LOC location, + static Field3D interpolateAndExtrapolate(const Field3D& f_, CELL_LOC location, bool extrapolate_x, bool extrapolate_y, bool no_extra_interpolate, - ParallelTransform* pt_) const; + ParallelTransform* pt_) ; #endif // BOUT_USE_METRIC_3D }; diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 5b944175c8..d747b0657a 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -68,17 +68,17 @@ std::string getLocationSuffix(CELL_LOC location) { // Use sendY()/sendX() and wait() instead of Mesh::communicate() to ensure we // don't try to calculate parallel slices as Coordinates are not constructed yet -void Coordinates::communicate(const Field2D& f) const { +void Coordinates::communicate(const Field2D& f) { FieldGroup g(f); - auto h = f.getMesh()->sendY(g); + auto* h = f.getMesh()->sendY(g); f.getMesh()->wait(h); h = f.getMesh()->sendX(g); f.getMesh()->wait(h); } #if BOUT_USE_METRIC_3D -void Coordinates::communicate(const Field3D& f) const { +void Coordinates::communicate(const Field3D& f) { FieldGroup g(f); - auto h = f.getMesh()->sendY(g); + auto* h = f.getMesh()->sendY(g); f.getMesh()->wait(h); h = f.getMesh()->sendX(g); f.getMesh()->wait(h); @@ -93,7 +93,7 @@ void Coordinates::communicate(const Field3D& f) const { Field2D Coordinates::interpolateAndExtrapolate( const Field2D& f, CELL_LOC location, bool extrapolate_x, bool extrapolate_y, bool no_extra_interpolate, ParallelTransform* UNUSED(pt) = nullptr, - const std::string& region = "RGN_NOBNDRY") const { + const std::string& region = "RGN_NOBNDRY") { Mesh* localmesh = f.getMesh(); Field2D result = interp_to(f, location, region); @@ -212,7 +212,7 @@ Field2D Coordinates::interpolateAndExtrapolate( Field3D Coordinates::interpolateAndExtrapolate(const Field3D& f_, CELL_LOC location, bool extrapolate_x, bool extrapolate_y, bool no_extra_interpolate, - ParallelTransform* pt_) const { + ParallelTransform* pt_) { Mesh* localmesh = f_.getMesh(); Field3D result; @@ -237,7 +237,7 @@ Field3D Coordinates::interpolateAndExtrapolate(const Field3D& f_, CELL_LOC locat if (location == CELL_YLOW and f.getLocation() != CELL_YLOW) { auto f_aligned = pt_f->toFieldAligned(f, "RGN_NOX"); result = interp_to(f_aligned, location, "RGN_NOBNDRY"); - ParallelTransform* pt_result; + ParallelTransform* pt_result = nullptr; if (result.getCoordinates() == nullptr) { pt_result = pt_; } else { @@ -1566,8 +1566,8 @@ void Coordinates::setCovariantMetricTensor(const CovariantMetricTensor& metric_t recalculateAndReset(recalculate_staggered, force_interpolate_from_centre); } -void Coordinates::setMetricTensor(ContravariantMetricTensor contravariant_metric_tensor, - CovariantMetricTensor covariant_metric_tensor) { +void Coordinates::setMetricTensor(const ContravariantMetricTensor& contravariant_metric_tensor, + const CovariantMetricTensor& covariant_metric_tensor) { contravariantMetricTensor.setMetricTensor(contravariant_metric_tensor); covariantMetricTensor.setMetricTensor(covariant_metric_tensor); } From f1216b1791c28d6440608fff8de08106b8e9a9b6 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Tue, 14 May 2024 21:17:50 +0100 Subject: [PATCH 412/491] Do the copying in the method that calls communicate(), so that the copies are still in scope when they are used. --- include/bout/fieldgroup.hxx | 31 +++++++------------------------ include/bout/mesh.hxx | 15 ++++++++++++++- src/mesh/coordinates.cxx | 12 ++++++------ 3 files changed, 27 insertions(+), 31 deletions(-) diff --git a/include/bout/fieldgroup.hxx b/include/bout/fieldgroup.hxx index 915486d134..552509fea7 100644 --- a/include/bout/fieldgroup.hxx +++ b/include/bout/fieldgroup.hxx @@ -66,6 +66,13 @@ public: add(ts...); } + template + explicit FieldGroup(std::vector items) { + for (auto i : items){ + add(i); + } + } + /// Copy contents of another FieldGroup \p other into this group. void add(const FieldGroup& other) { fvec.insert(fvec.end(), other.fvec.begin(), other.fvec.end()); @@ -131,47 +138,23 @@ public: add(ts...); // Add the rest } - template - void add(const Field3D& t) { - auto temp = t; // Copy value to remove const qualifier (because std::vector can't take cv-qualified items) - add(temp); // Add using functions above - } - template void add(Field3D& t, Ts&... ts) { add(t); // Add the first using functions above add(ts...); // Add the rest } - template - void add(const Field3D& t, const Ts&... ts) { - auto temp = t; // Copy value to remove const qualifier (because std::vector can't take cv-qualified items) - add(temp); // Add the first using functions above - add(ts...); // Add the rest - } template void add(Vector3D& t, Ts&... ts) { add(t); // Add the first using functions above add(ts...); // Add the rest } - template - void add(const Vector3D& t, const Ts&... ts) { - auto temp = t; // Copy value to remove const qualifier (because std::vector can't take cv-qualified items) - add(temp); // Add the first using functions above - add(ts...); // Add the rest - } template void add(Vector2D& t, Ts&... ts) { add(t); // Add the first using functions above add(ts...); // Add the rest } - template - void add(const Vector2D& t, const Ts&... ts) { - auto temp = t; // Copy value to remove const qualifier (because std::vector can't take cv-qualified items) - add(temp); // Add the first using functions above - add(ts...); // Add the rest - } /// Return number of fields int size() const { return static_cast(fvec.size()); } diff --git a/include/bout/mesh.hxx b/include/bout/mesh.hxx index fe9c549628..e34d836e43 100644 --- a/include/bout/mesh.hxx +++ b/include/bout/mesh.hxx @@ -285,9 +285,22 @@ public: FieldGroup g(ts...); communicate(g); } + template void communicate(const Ts&... ts) { - FieldGroup g(ts...); + // Copy values to remove const qualifier (because std::vector can't take cv-qualified items) + + //Get the type of the first parameter and make a vector of that type to store the copies + auto first_parameter = [] + (auto first_parameter, auto...) -> auto { return first_parameter; }(ts...); + using type_of_first = typeof(first_parameter); + std::vector all_copies; + + for (auto x : {ts...}){ + auto copied_x = x; + all_copies.push_back(copied_x); + } + FieldGroup g(all_copies); communicate(g); } diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index d747b0657a..f8e789a493 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -69,7 +69,8 @@ std::string getLocationSuffix(CELL_LOC location) { // Use sendY()/sendX() and wait() instead of Mesh::communicate() to ensure we // don't try to calculate parallel slices as Coordinates are not constructed yet void Coordinates::communicate(const Field2D& f) { - FieldGroup g(f); + auto f_copy = f; // Copy value to remove const qualifier (because std::vector can't take cv-qualified items) + FieldGroup g(f_copy); auto* h = f.getMesh()->sendY(g); f.getMesh()->wait(h); h = f.getMesh()->sendX(g); @@ -77,7 +78,8 @@ void Coordinates::communicate(const Field2D& f) { } #if BOUT_USE_METRIC_3D void Coordinates::communicate(const Field3D& f) { - FieldGroup g(f); + auto f_copy = f; // Copy value to remove const qualifier (because std::vector can't take cv-qualified items) + FieldGroup g(f_copy); auto* h = f.getMesh()->sendY(g); f.getMesh()->wait(h); h = f.getMesh()->sendX(g); @@ -906,8 +908,7 @@ void Coordinates::extrapolateChristoffelSymbols() { } void Coordinates::communicateGValues() const { - auto temp = G1(); // TODO: There must be a better way than this! - localmesh->communicate(temp, G2(), G3()); + localmesh->communicate(G1(), G2(), G3()); } void Coordinates::extrapolateGValues() { @@ -1586,8 +1587,7 @@ void Coordinates::communicateChristoffelSymbolTerms() const { output_progress.write("\tCommunicating connection terms\n"); - auto tmp = G1_11(); // TODO: There must be a better way than this! - localmesh->communicate(tmp, G1_22(), G1_33(), G1_12(), G1_13(), G1_23(), G2_11(), G2_22(), G2_33(), + localmesh->communicate(G1_11(), G1_22(), G1_33(), G1_12(), G1_13(), G1_23(), G2_11(), G2_22(), G2_33(), G2_12(), G2_13(), G2_23(), G3_11(), G3_22(), G3_33(), G3_12(), G3_13(), G3_23()); } From 736198b95ab7e05b7c5021967469457c42a36e75 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 15 May 2024 13:01:57 +0100 Subject: [PATCH 413/491] Do the copying in the method that calls Mesh::communicate(), so that the copies are still in scope when they are used. --- include/bout/fieldgroup.hxx | 7 ----- include/bout/mesh.hxx | 18 ----------- src/mesh/coordinates.cxx | 60 +++++++++++++++++++++++++++++++------ 3 files changed, 51 insertions(+), 34 deletions(-) diff --git a/include/bout/fieldgroup.hxx b/include/bout/fieldgroup.hxx index 552509fea7..32bc75e557 100644 --- a/include/bout/fieldgroup.hxx +++ b/include/bout/fieldgroup.hxx @@ -66,13 +66,6 @@ public: add(ts...); } - template - explicit FieldGroup(std::vector items) { - for (auto i : items){ - add(i); - } - } - /// Copy contents of another FieldGroup \p other into this group. void add(const FieldGroup& other) { fvec.insert(fvec.end(), other.fvec.begin(), other.fvec.end()); diff --git a/include/bout/mesh.hxx b/include/bout/mesh.hxx index e34d836e43..9e0234dbcc 100644 --- a/include/bout/mesh.hxx +++ b/include/bout/mesh.hxx @@ -286,24 +286,6 @@ public: communicate(g); } - template - void communicate(const Ts&... ts) { - // Copy values to remove const qualifier (because std::vector can't take cv-qualified items) - - //Get the type of the first parameter and make a vector of that type to store the copies - auto first_parameter = [] - (auto first_parameter, auto...) -> auto { return first_parameter; }(ts...); - using type_of_first = typeof(first_parameter); - std::vector all_copies; - - for (auto x : {ts...}){ - auto copied_x = x; - all_copies.push_back(copied_x); - } - FieldGroup g(all_copies); - communicate(g); - } - template void communicateXZ(Ts&... ts) { FieldGroup g(ts...); diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index f8e789a493..dd7675dc25 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -765,8 +765,26 @@ const Field2D& Coordinates::zlength() const { int Coordinates::communicateAndCheckMeshSpacing() const { TRACE("Coordinates::communicateAndCheckMeshSpacing"); - localmesh->communicate(dx(), dy(), dz(), g11(), g22(), g33(), g12(), g13(), g23(), g_11(), g_22(), g_33(), - g_12(), g_13(), g_23(), J(), Bxy()); + auto tmp1 = dx(); + auto tmp2 = dy(); + auto tmp3 = dz(); + auto tmp4 = g11(); + auto tmp5 = g22(); + auto tmp6 = g33(); + auto tmp7 = g12(); + auto tmp8 = g13(); + auto tmp9 = g23(); + auto tmp10 = g_11(); + auto tmp11 = g_22(); + auto tmp12 = g_33(); + auto tmp13 = g_12(); + auto tmp14 = g_13(); + auto tmp15 = g_23(); + auto tmp16 = J(); + auto tmp17 = Bxy(); + localmesh->communicate(tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, + tmp7, tmp8, tmp9, tmp10, tmp11, tmp12, + tmp13, tmp14, tmp15, tmp16, tmp17); output_progress.write("Calculating differential geometry terms\n"); @@ -882,7 +900,10 @@ void Coordinates::correctionForNonUniformMeshes(bool force_interpolate_from_cent d1_dz_ = 0; #endif - localmesh->communicate(d1_dx(), d1_dy(), d1_dz()); + auto tmp1 = d1_dx(); + auto tmp2 = d1_dy(); + auto tmp3 = d1_dz(); + localmesh->communicate(tmp1, tmp2, tmp3); } void Coordinates::extrapolateChristoffelSymbols() { @@ -908,7 +929,10 @@ void Coordinates::extrapolateChristoffelSymbols() { } void Coordinates::communicateGValues() const { - localmesh->communicate(G1(), G2(), G3()); + auto tmp1 = G1(); + auto tmp2 = G2(); + auto tmp3 = G3(); + localmesh->communicate(tmp1, tmp2, tmp3); } void Coordinates::extrapolateGValues() { @@ -1585,11 +1609,29 @@ void Coordinates::applyToCovariantMetricTensor( void Coordinates::communicateChristoffelSymbolTerms() const { - output_progress.write("\tCommunicating connection terms\n"); - - localmesh->communicate(G1_11(), G1_22(), G1_33(), G1_12(), G1_13(), G1_23(), G2_11(), G2_22(), G2_33(), - G2_12(), G2_13(), G2_23(), G3_11(), G3_22(), G3_33(), G3_12(), G3_13(), - G3_23()); + output_progress.write("\tCommunicating connection terms\n"); + + auto tmp1 = G1_11(); + auto tmp2 = G1_22(); + auto tmp3 = G1_33(); + auto tmp4 = G1_12(); + auto tmp5 = G1_13(); + auto tmp6 = G1_23(); + auto tmp7 = G2_11(); + auto tmp8 = G2_22(); + auto tmp9 = G2_33(); + auto tmp10 = G2_12(); + auto tmp11 = G2_13(); + auto tmp12 = G2_23(); + auto tmp13 = G3_11(); + auto tmp14 = G3_22(); + auto tmp15 = G3_33(); + auto tmp16 = G3_12(); + auto tmp17 = G3_13(); + auto tmp18 = G3_23(); + localmesh->communicate(tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, + tmp7, tmp8, tmp9, tmp10, tmp11, tmp12, + tmp13, tmp14, tmp15, tmp16, tmp17, tmp18); } void Coordinates::invalidateAndRecalculateCachedVariables() { From 3d177954b032a6723ec01e98d5b26f3c6ae34074 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Thu, 16 May 2024 18:13:42 +0100 Subject: [PATCH 414/491] Add MetricTensor::communicate() method, since need to pass a non-const reference to the actual data to Mesh::communicate(), so that it can modify it. --- include/bout/christoffel_symbols.hxx | 2 +- include/bout/coordinates.hxx | 46 ++++++++++++++-------------- include/bout/metric_tensor.hxx | 2 ++ src/mesh/christoffel_symbols.cxx | 2 +- src/mesh/coordinates.cxx | 33 +++++--------------- src/mesh/metric_tensor.cxx | 5 +++ 6 files changed, 40 insertions(+), 50 deletions(-) diff --git a/include/bout/christoffel_symbols.hxx b/include/bout/christoffel_symbols.hxx index a6cd19a195..c8f623b92a 100644 --- a/include/bout/christoffel_symbols.hxx +++ b/include/bout/christoffel_symbols.hxx @@ -21,7 +21,7 @@ public: FieldMetric G3_11, FieldMetric G3_22, FieldMetric G3_33, FieldMetric G3_12, FieldMetric G3_13, FieldMetric G3_23); - explicit ChristoffelSymbols(const Coordinates& coordinates); + explicit ChristoffelSymbols(Coordinates& coordinates); // ChristoffelSymbols(BoutReal g11, BoutReal g22, BoutReal g33, BoutReal g12, BoutReal g13, // BoutReal g23, Mesh* mesh); diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index e4c5a0f1e3..3261cd125e 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -144,7 +144,7 @@ public: const CovariantMetricTensor& covariant_metric_tensor); ///< Coordinate system Jacobian, so volume of cell is J*dx*dy*dz - const FieldMetric& J() const; + FieldMetric& J() const; ///< Magnitude of B = nabla z times nabla x const FieldMetric& Bxy() const { return Bxy_; } @@ -169,7 +169,7 @@ public: IntShiftTorsion_ = std::move(IntShiftTorsion); } - int communicateAndCheckMeshSpacing() const; + int communicateAndCheckMeshSpacing(); /////////////////////////////////////////////////////////// // Parallel transforms @@ -269,24 +269,24 @@ public: Field2D Laplace_perpXY(const Field2D& A, const Field2D& f) const; /// Christoffel symbol of the second kind (connection coefficients) - const FieldMetric& G1_11() const { return christoffel_symbols().G1_11(); } - const FieldMetric& G1_22() const { return christoffel_symbols().G1_22(); } - const FieldMetric& G1_33() const { return christoffel_symbols().G1_33(); } - const FieldMetric& G1_12() const { return christoffel_symbols().G1_12(); } - const FieldMetric& G1_13() const { return christoffel_symbols().G1_13(); } - const FieldMetric& G1_23() const { return christoffel_symbols().G1_23(); } - const FieldMetric& G2_11() const { return christoffel_symbols().G2_11(); } - const FieldMetric& G2_22() const { return christoffel_symbols().G2_22(); } - const FieldMetric& G2_33() const { return christoffel_symbols().G2_33(); } - const FieldMetric& G2_12() const { return christoffel_symbols().G2_12(); } - const FieldMetric& G2_13() const { return christoffel_symbols().G2_13(); } - const FieldMetric& G2_23() const { return christoffel_symbols().G2_23(); } - const FieldMetric& G3_11() const { return christoffel_symbols().G3_11(); } - const FieldMetric& G3_22() const { return christoffel_symbols().G3_22(); } - const FieldMetric& G3_33() const { return christoffel_symbols().G3_33(); } - const FieldMetric& G3_12() const { return christoffel_symbols().G3_12(); } - const FieldMetric& G3_13() const { return christoffel_symbols().G3_13(); } - const FieldMetric& G3_23() const { return christoffel_symbols().G3_23(); } + const FieldMetric& G1_11() { return christoffel_symbols().G1_11(); } + const FieldMetric& G1_22() { return christoffel_symbols().G1_22(); } + const FieldMetric& G1_33() { return christoffel_symbols().G1_33(); } + const FieldMetric& G1_12() { return christoffel_symbols().G1_12(); } + const FieldMetric& G1_13() { return christoffel_symbols().G1_13(); } + const FieldMetric& G1_23() { return christoffel_symbols().G1_23(); } + const FieldMetric& G2_11() { return christoffel_symbols().G2_11(); } + const FieldMetric& G2_22() { return christoffel_symbols().G2_22(); } + const FieldMetric& G2_33() { return christoffel_symbols().G2_33(); } + const FieldMetric& G2_12() { return christoffel_symbols().G2_12(); } + const FieldMetric& G2_13() { return christoffel_symbols().G2_13(); } + const FieldMetric& G2_23() { return christoffel_symbols().G2_23(); } + const FieldMetric& G3_11() { return christoffel_symbols().G3_11(); } + const FieldMetric& G3_22() { return christoffel_symbols().G3_22(); } + const FieldMetric& G3_33() { return christoffel_symbols().G3_33(); } + const FieldMetric& G3_12() { return christoffel_symbols().G3_12(); } + const FieldMetric& G3_13() { return christoffel_symbols().G3_13(); } + const FieldMetric& G3_23() { return christoffel_symbols().G3_23(); } const FieldMetric& G1() const { return g_values().G1(); } const FieldMetric& G2() const { return g_values().G2(); } @@ -301,7 +301,7 @@ public: const FieldMetric& invSg() const; - ChristoffelSymbols& christoffel_symbols() const; + ChristoffelSymbols& christoffel_symbols(); GValues& g_values() const; @@ -363,7 +363,7 @@ private: const std::function& function); void applyToChristoffelSymbols( - const std::function& function) const; + const std::function& function); mutable std::unique_ptr jacobian_cache{nullptr}; @@ -387,7 +387,7 @@ private: FieldMetric getUnaligned(const std::string& name, BoutReal default_value); - void communicateChristoffelSymbolTerms() const; + void communicateChristoffelSymbolTerms(); void extrapolateChristoffelSymbols(); void communicateGValues() const; diff --git a/include/bout/metric_tensor.hxx b/include/bout/metric_tensor.hxx index deacc20829..4039142870 100644 --- a/include/bout/metric_tensor.hxx +++ b/include/bout/metric_tensor.hxx @@ -59,6 +59,8 @@ public: MetricTensor applyToComponents( const std::function& function) const; + void communicate(Mesh* mesh); + protected: FieldMetric g11_, g22_, g33_, g12_, g13_, g23_; }; diff --git a/src/mesh/christoffel_symbols.cxx b/src/mesh/christoffel_symbols.cxx index 3872470778..b1ed5556c0 100644 --- a/src/mesh/christoffel_symbols.cxx +++ b/src/mesh/christoffel_symbols.cxx @@ -16,7 +16,7 @@ ChristoffelSymbols::ChristoffelSymbols( G3_11_(std::move(G3_11)), G3_22_(std::move(G3_22)), G3_33_(std::move(G3_33)), G3_12_(std::move(G3_12)), G3_13_(std::move(G3_13)), G3_23_(std::move(G3_23)){}; -ChristoffelSymbols::ChristoffelSymbols(const Coordinates& coordinates) { +ChristoffelSymbols::ChristoffelSymbols(Coordinates& coordinates) { // Calculate Christoffel symbol terms (18 independent values) // Note: This calculation is completely general: metric // tensor can be 2D or 3D. For 2D, all DDZ terms are zero diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index dd7675dc25..7a09bd1dd1 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -762,29 +762,12 @@ const Field2D& Coordinates::zlength() const { return *zlength_cache; } -int Coordinates::communicateAndCheckMeshSpacing() const { +int Coordinates::communicateAndCheckMeshSpacing() { TRACE("Coordinates::communicateAndCheckMeshSpacing"); - auto tmp1 = dx(); - auto tmp2 = dy(); - auto tmp3 = dz(); - auto tmp4 = g11(); - auto tmp5 = g22(); - auto tmp6 = g33(); - auto tmp7 = g12(); - auto tmp8 = g13(); - auto tmp9 = g23(); - auto tmp10 = g_11(); - auto tmp11 = g_22(); - auto tmp12 = g_33(); - auto tmp13 = g_12(); - auto tmp14 = g_13(); - auto tmp15 = g_23(); - auto tmp16 = J(); - auto tmp17 = Bxy(); - localmesh->communicate(tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, - tmp7, tmp8, tmp9, tmp10, tmp11, tmp12, - tmp13, tmp14, tmp15, tmp16, tmp17); + localmesh->communicate(dx_, dy_, dz_, Bxy_, J()); + covariantMetricTensor.communicate(localmesh); + contravariantMetricTensor.communicate(localmesh); output_progress.write("Calculating differential geometry terms\n"); @@ -1480,7 +1463,7 @@ Field2D Coordinates::Laplace_perpXY([[maybe_unused]] const Field2D& A, #endif } -ChristoffelSymbols& Coordinates::christoffel_symbols() const { +ChristoffelSymbols& Coordinates::christoffel_symbols() { if (christoffel_symbols_cache == nullptr) { auto ptr = std::make_unique(*this); christoffel_symbols_cache = std::move(ptr); @@ -1531,7 +1514,7 @@ void Coordinates::checkContravariant() { contravariantMetricTensor.check(localmesh->ystart); } -const FieldMetric& Coordinates::J() const { +FieldMetric& Coordinates::J() const { if (jacobian_cache == nullptr) { const auto j = recalculateJacobian(); auto ptr = std::make_unique(j); @@ -1570,7 +1553,7 @@ void Coordinates::setBxy(FieldMetric Bxy) { } void Coordinates::applyToChristoffelSymbols( - const std::function& function) const { + const std::function& function) { christoffel_symbols().applyToComponents(function); } @@ -1607,7 +1590,7 @@ void Coordinates::applyToCovariantMetricTensor( covariantMetricTensor.map(function); } -void Coordinates::communicateChristoffelSymbolTerms() const { +void Coordinates::communicateChristoffelSymbolTerms() { output_progress.write("\tCommunicating connection terms\n"); diff --git a/src/mesh/metric_tensor.cxx b/src/mesh/metric_tensor.cxx index dced5047bf..5bfab6cec1 100644 --- a/src/mesh/metric_tensor.cxx +++ b/src/mesh/metric_tensor.cxx @@ -2,6 +2,7 @@ #include "bout/metric_tensor.hxx" #include "bout/output.hxx" #include +#include "bout/mesh.hxx" MetricTensor::MetricTensor(FieldMetric g11, FieldMetric g22, FieldMetric g33, FieldMetric g12, FieldMetric g13, FieldMetric g23) @@ -146,3 +147,7 @@ MetricTensor MetricTensor::applyToComponents( return MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23); } + +void MetricTensor::communicate(Mesh* mesh) { + mesh->communicate(g11_, g22_, g33_, g12_, g13_, g23_); +} From 550c38f8cd985bccdfcd041da8509fcfb957cc0c Mon Sep 17 00:00:00 2001 From: tomc271 Date: Thu, 16 May 2024 18:25:08 +0100 Subject: [PATCH 415/491] Add ChristoffelSymbols::communicate() method. --- include/bout/christoffel_symbols.hxx | 3 +++ src/mesh/christoffel_symbols.cxx | 7 +++++++ src/mesh/coordinates.cxx | 24 +----------------------- 3 files changed, 11 insertions(+), 23 deletions(-) diff --git a/include/bout/christoffel_symbols.hxx b/include/bout/christoffel_symbols.hxx index c8f623b92a..7602bb4b20 100644 --- a/include/bout/christoffel_symbols.hxx +++ b/include/bout/christoffel_symbols.hxx @@ -84,6 +84,9 @@ public: void applyToComponents(const std::function& function); + void communicate(Mesh* mesh); + + private: FieldMetric G1_11_, G1_22_, G1_33_, G1_12_, G1_13_, G1_23_; FieldMetric G2_11_, G2_22_, G2_33_, G2_12_, G2_13_, G2_23_; diff --git a/src/mesh/christoffel_symbols.cxx b/src/mesh/christoffel_symbols.cxx index b1ed5556c0..1d079b8dcf 100644 --- a/src/mesh/christoffel_symbols.cxx +++ b/src/mesh/christoffel_symbols.cxx @@ -2,6 +2,7 @@ #include "bout/christoffel_symbols.hxx" #include "bout/coordinates.hxx" #include +#include "bout/mesh.hxx" ChristoffelSymbols::ChristoffelSymbols( FieldMetric G1_11, FieldMetric G1_22, FieldMetric G1_33, FieldMetric G1_12, @@ -138,3 +139,9 @@ void ChristoffelSymbols::applyToComponents( setChristoffelSymbols(G1_11, G1_22, G1_33, G1_12, G1_13, G1_23, G2_11, G2_22, G2_33, G2_12, G2_13, G2_23, G3_11, G3_22, G3_33, G3_12, G3_13, G3_23); } + +void ChristoffelSymbols::communicate(Mesh* mesh) { + mesh->communicate(G1_11_, G1_22_, G1_33_, G1_12_, G1_13_, G1_23_, + G2_11_, G2_22_, G2_33_, G2_12_, G2_13_, G2_23_, + G3_11_, G3_22_, G3_33_, G3_12_, G3_13_, G3_23_); +} diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 7a09bd1dd1..269dbc8297 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -1591,30 +1591,8 @@ void Coordinates::applyToCovariantMetricTensor( } void Coordinates::communicateChristoffelSymbolTerms() { - output_progress.write("\tCommunicating connection terms\n"); - - auto tmp1 = G1_11(); - auto tmp2 = G1_22(); - auto tmp3 = G1_33(); - auto tmp4 = G1_12(); - auto tmp5 = G1_13(); - auto tmp6 = G1_23(); - auto tmp7 = G2_11(); - auto tmp8 = G2_22(); - auto tmp9 = G2_33(); - auto tmp10 = G2_12(); - auto tmp11 = G2_13(); - auto tmp12 = G2_23(); - auto tmp13 = G3_11(); - auto tmp14 = G3_22(); - auto tmp15 = G3_33(); - auto tmp16 = G3_12(); - auto tmp17 = G3_13(); - auto tmp18 = G3_23(); - localmesh->communicate(tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, - tmp7, tmp8, tmp9, tmp10, tmp11, tmp12, - tmp13, tmp14, tmp15, tmp16, tmp17, tmp18); + christoffel_symbols().communicate(localmesh); } void Coordinates::invalidateAndRecalculateCachedVariables() { From 47017c64acaa0a903c93a2d5de136c1ee94ac351 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Thu, 16 May 2024 18:31:01 +0100 Subject: [PATCH 416/491] Pass (const refs to) the actual d1_dx, d1_dy, d1_dz data to localmesh->communicate(), so that it will be able to modify it. --- src/mesh/coordinates.cxx | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 269dbc8297..ccd3e8cf29 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -883,10 +883,7 @@ void Coordinates::correctionForNonUniformMeshes(bool force_interpolate_from_cent d1_dz_ = 0; #endif - auto tmp1 = d1_dx(); - auto tmp2 = d1_dy(); - auto tmp3 = d1_dz(); - localmesh->communicate(tmp1, tmp2, tmp3); + localmesh->communicate(d1_dx_, d1_dy_, d1_dz_); } void Coordinates::extrapolateChristoffelSymbols() { From fdc6c281200a8b9e681883bccb531c241876889e Mon Sep 17 00:00:00 2001 From: tomc271 Date: Thu, 16 May 2024 18:41:06 +0100 Subject: [PATCH 417/491] Add GValues::communicate() method. --- include/bout/g_values.hxx | 2 ++ src/mesh/coordinates.cxx | 5 +---- src/mesh/g_values.cxx | 5 +++++ 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/include/bout/g_values.hxx b/include/bout/g_values.hxx index ea81ca6932..1f4f20ab4a 100644 --- a/include/bout/g_values.hxx +++ b/include/bout/g_values.hxx @@ -22,6 +22,8 @@ public: void setG2(const FieldMetric& G2) { G2_ = G2; } void setG3(const FieldMetric& G3) { G3_ = G3; } + void communicate(Mesh* mesh); + private: FieldMetric G1_, G2_, G3_; }; diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index ccd3e8cf29..dc86ba12fb 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -909,10 +909,7 @@ void Coordinates::extrapolateChristoffelSymbols() { } void Coordinates::communicateGValues() const { - auto tmp1 = G1(); - auto tmp2 = G2(); - auto tmp3 = G3(); - localmesh->communicate(tmp1, tmp2, tmp3); + g_values().communicate(localmesh); } void Coordinates::extrapolateGValues() { diff --git a/src/mesh/g_values.cxx b/src/mesh/g_values.cxx index bb2835d51d..f14013183b 100644 --- a/src/mesh/g_values.cxx +++ b/src/mesh/g_values.cxx @@ -1,6 +1,7 @@ #include "bout/g_values.hxx" #include "bout/coordinates.hxx" +#include "bout/mesh.hxx" GValues::GValues(FieldMetric G1, FieldMetric G2, FieldMetric G3) : G1_(std::move(G1)), G2_(std::move(G2)), G3_(std::move(G3)){}; @@ -27,3 +28,7 @@ GValues::GValues(const Coordinates& coordinates) { coordinates.communicate(tmp); setG3((coordinates.DDX(J * g13) + coordinates.DDY(tmp) + coordinates.DDZ(J * g33)) / J); } + +void GValues::communicate(Mesh* mesh) { + mesh->communicate(G1_, G2_, G3_); +} From 896a6108b7d7d1a315ed47f95f607a8b85861a11 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Thu, 16 May 2024 18:45:28 +0100 Subject: [PATCH 418/491] Remove redundant methods Coordinates::communicateChristoffelSymbolTerms() and Coordinates::communicateGValues(). --- include/bout/coordinates.hxx | 2 -- src/mesh/christoffel_symbols.cxx | 3 +++ src/mesh/coordinates.cxx | 13 ++----------- 3 files changed, 5 insertions(+), 13 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index 3261cd125e..6d029da16d 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -387,10 +387,8 @@ private: FieldMetric getUnaligned(const std::string& name, BoutReal default_value); - void communicateChristoffelSymbolTerms(); void extrapolateChristoffelSymbols(); - void communicateGValues() const; void extrapolateGValues(); FieldMetric recalculateBxy() const; diff --git a/src/mesh/christoffel_symbols.cxx b/src/mesh/christoffel_symbols.cxx index 1d079b8dcf..5789ade8ec 100644 --- a/src/mesh/christoffel_symbols.cxx +++ b/src/mesh/christoffel_symbols.cxx @@ -141,6 +141,9 @@ void ChristoffelSymbols::applyToComponents( } void ChristoffelSymbols::communicate(Mesh* mesh) { + + output_progress.write("\tCommunicating connection terms\n"); + mesh->communicate(G1_11_, G1_22_, G1_33_, G1_12_, G1_13_, G1_23_, G2_11_, G2_22_, G2_33_, G2_12_, G2_13_, G2_23_, G3_11_, G3_22_, G3_33_, G3_12_, G3_13_, G3_23_); diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index dc86ba12fb..bd9994bbc7 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -794,11 +794,11 @@ void Coordinates::recalculateAndReset(bool recalculate_staggered, checkCovariant(); christoffel_symbols_cache.reset(); - communicateChristoffelSymbolTerms(); + christoffel_symbols().communicate(localmesh); extrapolateChristoffelSymbols(); g_values_cache.reset(); - communicateGValues(); + g_values().communicate(localmesh); extrapolateGValues(); correctionForNonUniformMeshes(force_interpolate_from_centre); @@ -908,10 +908,6 @@ void Coordinates::extrapolateChristoffelSymbols() { applyToChristoffelSymbols(interpolateAndExtrapolate_function); } -void Coordinates::communicateGValues() const { - g_values().communicate(localmesh); -} - void Coordinates::extrapolateGValues() { setG1(interpolateAndExtrapolate(G1(), location, true, true, true, transform.get())); @@ -1584,11 +1580,6 @@ void Coordinates::applyToCovariantMetricTensor( covariantMetricTensor.map(function); } -void Coordinates::communicateChristoffelSymbolTerms() { - output_progress.write("\tCommunicating connection terms\n"); - christoffel_symbols().communicate(localmesh); -} - void Coordinates::invalidateAndRecalculateCachedVariables() { zlength_cache.reset(); Grad2_par2_DDY_invSgCache.clear(); From 545ede86a7f211ee0198948de304280bbcca24f3 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Fri, 17 May 2024 15:17:51 +0100 Subject: [PATCH 419/491] Coordinates::communicate() method needs to take a non-const parameter to be able to modify it. --- include/bout/coordinates.hxx | 5 +++-- src/mesh/coordinates.cxx | 13 +++++-------- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index 6d029da16d..8aa7554a15 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -310,10 +310,11 @@ public: FieldMetric recalculateJacobian() const; - static void communicate(const Field2D& f) ; + static void communicate(Field2D& f) ; + #if BOUT_USE_METRIC_3D // In this case we also need to be able to call with a Field3D - static void communicate(const Field3D& f) ; + static void communicate(Field3D& f) ; #endif diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index bd9994bbc7..a1159d541a 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -68,18 +68,16 @@ std::string getLocationSuffix(CELL_LOC location) { // Use sendY()/sendX() and wait() instead of Mesh::communicate() to ensure we // don't try to calculate parallel slices as Coordinates are not constructed yet -void Coordinates::communicate(const Field2D& f) { - auto f_copy = f; // Copy value to remove const qualifier (because std::vector can't take cv-qualified items) - FieldGroup g(f_copy); +void Coordinates::communicate(Field2D& f) { + FieldGroup g(f); auto* h = f.getMesh()->sendY(g); f.getMesh()->wait(h); h = f.getMesh()->sendX(g); f.getMesh()->wait(h); } #if BOUT_USE_METRIC_3D -void Coordinates::communicate(const Field3D& f) { - auto f_copy = f; // Copy value to remove const qualifier (because std::vector can't take cv-qualified items) - FieldGroup g(f_copy); +void Coordinates::communicate(Field3D& f) { + FieldGroup g(f); auto* h = f.getMesh()->sendY(g); f.getMesh()->wait(h); h = f.getMesh()->sendX(g); @@ -640,8 +638,7 @@ void Coordinates::setBoundaryCells(Options* mesh_options, const std::string& suf output_warn.write("\tMaximum difference in J is {:e}\n", max(abs(J() - J_from_file))); setJ(J_from_file); - auto J_value = J(); // TODO: There may be a better way - communicate(J_value); + communicate(J()); } // More robust to extrapolate derived quantities directly, rather than From e7df6bc1bcd2a8b5c85e0395aa141ccd3e8279ea Mon Sep 17 00:00:00 2001 From: tomc271 Date: Thu, 16 May 2024 12:20:32 +0100 Subject: [PATCH 420/491] Fix copy-paste errors introduced during refactoring. --- src/field/vector2d.cxx | 12 ++++++------ src/field/vector3d.cxx | 12 ++++++------ 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/field/vector2d.cxx b/src/field/vector2d.cxx index c6b0c776c7..bcffbcea8b 100644 --- a/src/field/vector2d.cxx +++ b/src/field/vector2d.cxx @@ -66,8 +66,8 @@ void Vector2D::toCovariant() { if (location == CELL_VSHIFT) { Coordinates* metric_x; - [[maybe_unused]] Coordinates* metric_y; - [[maybe_unused]] Coordinates* metric_z; + Coordinates* metric_y; + Coordinates* metric_z; metric_x = localmesh->getCoordinates(CELL_XLOW); metric_y = localmesh->getCoordinates(CELL_YLOW); metric_z = localmesh->getCoordinates(CELL_ZLOW); @@ -88,10 +88,10 @@ void Vector2D::toCovariant() { BOUT_FOR(i, x.getRegion("RGN_ALL")) { x[i] = metric_x->g_11()[i] * x[i] + metric_x->g_12()[i] * y_at_x[i] + metric_x->g_13()[i] * z_at_x[i]; - y[i] = metric_x->g_22()[i] * y[i] + metric_x->g_12()[i] * x_at_y[i] - + metric_x->g_23()[i] * z_at_y[i]; - z[i] = metric_x->g_33()[i] * z[i] + metric_x->g_13()[i] * x_at_z[i] - + metric_x->g_23()[i] * y_at_z[i]; + y[i] = metric_y->g_22()[i] * y[i] + metric_y->g_12()[i] * x_at_y[i] + + metric_y->g_23()[i] * z_at_y[i]; + z[i] = metric_z->g_33()[i] * z[i] + metric_z->g_13()[i] * x_at_z[i] + + metric_z->g_23()[i] * y_at_z[i]; }; } else { const auto metric = localmesh->getCoordinates(location); diff --git a/src/field/vector3d.cxx b/src/field/vector3d.cxx index 1cc6273eaa..273c4c791f 100644 --- a/src/field/vector3d.cxx +++ b/src/field/vector3d.cxx @@ -123,8 +123,8 @@ void Vector3D::toContravariant() { if (location == CELL_VSHIFT) { Coordinates* metric_x; - [[maybe_unused]] Coordinates* metric_y; - [[maybe_unused]] Coordinates* metric_z; + Coordinates* metric_y; + Coordinates* metric_z; metric_x = localmesh->getCoordinates(CELL_XLOW); metric_y = localmesh->getCoordinates(CELL_YLOW); @@ -146,10 +146,10 @@ void Vector3D::toContravariant() { BOUT_FOR(i, x.getRegion("RGN_ALL")) { x[i] = metric_x->g11()[i] * x[i] + metric_x->g12()[i] * y_at_x[i] + metric_x->g13()[i] * z_at_x[i]; - y[i] = metric_x->g22()[i] * y[i] + metric_x->g12()[i] * x_at_y[i] - + metric_x->g23()[i] * z_at_y[i]; - z[i] = metric_x->g33()[i] * z[i] + metric_x->g13()[i] * x_at_z[i] - + metric_x->g23()[i] * y_at_z[i]; + y[i] = metric_y->g22()[i] * y[i] + metric_y->g12()[i] * x_at_y[i] + + metric_y->g23()[i] * z_at_y[i]; + z[i] = metric_z->g33()[i] * z[i] + metric_z->g13()[i] * x_at_z[i] + + metric_z->g23()[i] * y_at_z[i]; }; } else { From 04c9399e6d0679d3032c0d2dcd827f38fe6fe9cc Mon Sep 17 00:00:00 2001 From: tomc271 Date: Thu, 16 May 2024 13:07:29 +0100 Subject: [PATCH 421/491] Restore #include "impls/bout/boutmesh.hxx" (removed in commit 62dcf21e68535cb755e3ad88a86f533863bdc95f) --- src/mesh/mesh.cxx | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/mesh/mesh.cxx b/src/mesh/mesh.cxx index 8ed2f8a544..8830756606 100644 --- a/src/mesh/mesh.cxx +++ b/src/mesh/mesh.cxx @@ -9,6 +9,8 @@ #include #include +#include "impls/bout/boutmesh.hxx" + MeshFactory::ReturnType MeshFactory::create(Options* options, GridDataSource* source) const { return create(getType(options), options, source); From 819486312677b48298a637175cf528d96494fb26 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 22 May 2024 12:53:18 +0100 Subject: [PATCH 422/491] Revert "More [[maybe_unused]] attributes." This reverts commit c7b70a9fb8af0d11368d019f2bc328260c366ff4. --- include/bout/mesh.hxx | 8 ++++---- include/bout/options.hxx | 2 +- include/bout/optionsreader.hxx | 2 +- src/sys/options.cxx | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/include/bout/mesh.hxx b/include/bout/mesh.hxx index 9e0234dbcc..bed8181ba8 100644 --- a/include/bout/mesh.hxx +++ b/include/bout/mesh.hxx @@ -481,10 +481,10 @@ public: /// Iterate over the upper Y boundary virtual RangeIterator iterateBndryUpperY() const = 0; - [[maybe_unused]] virtual RangeIterator iterateBndryLowerOuterY() const = 0; - [[maybe_unused]] virtual RangeIterator iterateBndryLowerInnerY() const = 0; - [[maybe_unused]] virtual RangeIterator iterateBndryUpperOuterY() const = 0; - [[maybe_unused]] virtual RangeIterator iterateBndryUpperInnerY() const = 0; + virtual RangeIterator iterateBndryLowerOuterY() const = 0; + virtual RangeIterator iterateBndryLowerInnerY() const = 0; + virtual RangeIterator iterateBndryUpperOuterY() const = 0; + virtual RangeIterator iterateBndryUpperInnerY() const = 0; /// Is there a boundary on the lower guard cells in Y /// on any processor along the X direction? diff --git a/include/bout/options.hxx b/include/bout/options.hxx index 9257d4a9c6..a0b745670f 100644 --- a/include/bout/options.hxx +++ b/include/bout/options.hxx @@ -786,7 +786,7 @@ public: void setConditionallyUsed(); /// clean the cache of parsed options - [[maybe_unused]] static void cleanCache(); + static void cleanCache(); /// Read-only access to internal options and sections /// to allow iteration over the tree diff --git a/include/bout/optionsreader.hxx b/include/bout/optionsreader.hxx index 942b677209..9069b5d2bf 100644 --- a/include/bout/optionsreader.hxx +++ b/include/bout/optionsreader.hxx @@ -100,7 +100,7 @@ public: /// ... /// return 0; /// } - [[maybe_unused]] static void parseCommandLine(Options* options, int argc, char** argv); + static void parseCommandLine(Options* options, int argc, char** argv); static void parseCommandLine(Options* options, const std::vector& argv); private: diff --git a/src/sys/options.cxx b/src/sys/options.cxx index 521d9f05ce..b49d57c80b 100644 --- a/src/sys/options.cxx +++ b/src/sys/options.cxx @@ -893,7 +893,7 @@ void Options::setConditionallyUsed() { } } -[[maybe_unused]] void Options::cleanCache() { FieldFactory::get()->cleanCache(); } +void Options::cleanCache() { FieldFactory::get()->cleanCache(); } std::map Options::subsections() const { std::map sections; From 3ce726c925d71983805a4695622e0edd7e37109e Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 22 May 2024 12:55:10 +0100 Subject: [PATCH 423/491] Revert "Add [[maybe_unused]] attributes." This reverts commit 0235449bca00cde8b922bb20e2620af4623b588e. --- include/bout/mesh.hxx | 10 +++++----- src/mesh/impls/bout/boutmesh.cxx | 2 +- src/mesh/impls/bout/boutmesh.hxx | 6 +++--- src/mesh/mesh.cxx | 8 ++++---- 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/include/bout/mesh.hxx b/include/bout/mesh.hxx index bed8181ba8..46908d3fdc 100644 --- a/include/bout/mesh.hxx +++ b/include/bout/mesh.hxx @@ -293,7 +293,7 @@ public: } template - [[maybe_unused]] void communicateYZ(Ts&... ts) { + void communicateYZ(Ts&... ts) { FieldGroup g(ts...); communicateYZ(g); } @@ -560,11 +560,11 @@ public: virtual int localSizePerp(); /// Get the value of the first global 3D index on this processor. - [[maybe_unused]] virtual int globalStartIndex3D(); + virtual int globalStartIndex3D(); /// Get the value of the first global 2D index on this processor. - [[maybe_unused]] virtual int globalStartIndex2D(); + virtual int globalStartIndex2D(); /// Get the value of the first global perpendicular index on this processor. - [[maybe_unused]] virtual int globalStartIndexPerp(); + virtual int globalStartIndexPerp(); /// Returns a global X index given a local index. /// Global index includes boundary cells, local index includes boundary or guard cells. @@ -815,7 +815,7 @@ protected: bool calcParallelSlices_on_communicate{true}; /// Read a 1D array of integers - [[maybe_unused]] std::vector readInts(const std::string& name, int n); + const std::vector readInts(const std::string& name, int n); /// Calculates the size of a message for a given x and y range int msg_len(const std::vector& var_list, int xge, int xlt, int yge, diff --git a/src/mesh/impls/bout/boutmesh.cxx b/src/mesh/impls/bout/boutmesh.cxx index c3006213a6..097aaf6b6f 100644 --- a/src/mesh/impls/bout/boutmesh.cxx +++ b/src/mesh/impls/bout/boutmesh.cxx @@ -1701,7 +1701,7 @@ int BoutMesh::getLocalYIndexNoBoundaries(int yglobal) const { return yglobal - PE_YIND * MYSUB + MYG; } -[[maybe_unused]] int BoutMesh::YGLOBAL(int yloc, int yproc) const { +int BoutMesh::YGLOBAL(int yloc, int yproc) const { return yloc + yproc * MYSUB - MYG; } diff --git a/src/mesh/impls/bout/boutmesh.hxx b/src/mesh/impls/bout/boutmesh.hxx index 65af6531a8..f8c2aff33a 100644 --- a/src/mesh/impls/bout/boutmesh.hxx +++ b/src/mesh/impls/bout/boutmesh.hxx @@ -174,8 +174,8 @@ public: BoutReal GlobalX(BoutReal jx) const override; BoutReal GlobalY(BoutReal jy) const override; - [[maybe_unused]] BoutReal getIxseps1() const { return ixseps1; } - [[maybe_unused]] BoutReal getIxseps2() const { return ixseps2; } + BoutReal getIxseps1() const { return ixseps1; } + BoutReal getIxseps2() const { return ixseps2; } void outputVars(Options& output_options) override; @@ -320,7 +320,7 @@ protected: /// Returns the processor number, given X (\p xind) and Y (\p yind) /// processor indices. Returns -1 if out of range (no processor) int PROC_NUM(int xind, int yind) const; - [[maybe_unused]] int YGLOBAL(int yloc, int yproc) const; + int YGLOBAL(int yloc, int yproc) const; int YLOCAL(int yglo, int yproc) const; /// Return the Y processor number given a global Y index int YPROC(int yind) const; diff --git a/src/mesh/mesh.cxx b/src/mesh/mesh.cxx index 8830756606..699f468f2a 100644 --- a/src/mesh/mesh.cxx +++ b/src/mesh/mesh.cxx @@ -529,28 +529,28 @@ int Mesh::localSizePerp() { return localNumCellsPerp; } -[[maybe_unused]] int Mesh::globalStartIndex3D() { +int Mesh::globalStartIndex3D() { int localSize = localSize3D(); int cumulativeSize = 0; mpi->MPI_Scan(&localSize, &cumulativeSize, 1, MPI_INT, MPI_SUM, BoutComm::get()); return cumulativeSize - localSize; } -[[maybe_unused]] int Mesh::globalStartIndex2D() { +int Mesh::globalStartIndex2D() { int localSize = localSize2D(); int cumulativeSize = 0; mpi->MPI_Scan(&localSize, &cumulativeSize, 1, MPI_INT, MPI_SUM, BoutComm::get()); return cumulativeSize - localSize; } -[[maybe_unused]] int Mesh::globalStartIndexPerp() { +int Mesh::globalStartIndexPerp() { int localSize = localSizePerp(); int cumulativeSize = 0; mpi->MPI_Scan(&localSize, &cumulativeSize, 1, MPI_INT, MPI_SUM, getXcomm()); return cumulativeSize - localSize; } -[[maybe_unused]] std::vector Mesh::readInts(const std::string& name, int n) { +std::vector Mesh::readInts(const std::string& name, int n) { TRACE("Mesh::readInts({:s})", name); if (source == nullptr) { From 53622ec34323056b48ef75ea60a76c0c545760af Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 22 May 2024 13:28:26 +0100 Subject: [PATCH 424/491] Revert "Prefer const." This reverts commit 77231a6dece68e55609be962eabf7329a61b928f. --- include/bout/mesh.hxx | 18 +++++++++--------- src/mesh/impls/bout/boutmesh.cxx | 2 +- src/mesh/impls/bout/boutmesh.hxx | 2 +- src/mesh/mesh.cxx | 4 ++-- 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/include/bout/mesh.hxx b/include/bout/mesh.hxx index 46908d3fdc..d315a7655d 100644 --- a/include/bout/mesh.hxx +++ b/include/bout/mesh.hxx @@ -412,7 +412,7 @@ public: /// @param[in] tag A label for the communication. Must be the same as sent virtual comm_handle irecvXIn(BoutReal* buffer, int size, int tag) = 0; - MPI_Comm getXcomm() const { + MPI_Comm getXcomm() { return getXcomm(0); } ///< Return communicator containing all processors in X virtual MPI_Comm getXcomm(int jy) const = 0; ///< Return X communicator @@ -717,7 +717,7 @@ public: // INDEX DERIVATIVE OPERATORS /////////////////////////////////////////////////////////// - ////// Utilities and parameters + ////// Utilties and parameters /// Fraction of modes to filter. This is set in derivs_init from option "ddz:fft_filter" BoutReal fft_derivs_filter{0.0}; @@ -731,8 +731,8 @@ public: /// given the input and output location. Also checks that the /// combination of locations is allowed. This overload also checks /// the location of a second input field (velocity) is consistent. - STAGGER getStagger(CELL_LOC vloc, CELL_LOC inloc, CELL_LOC outloc, - CELL_LOC allowedloc) const; + STAGGER getStagger(const CELL_LOC vloc, const CELL_LOC inloc, const CELL_LOC outloc, + const CELL_LOC allowedloc) const; /////////////////////////////////////////////////////////// // REGION RELATED ROUTINES @@ -773,20 +773,20 @@ public: void addRegionPerp(const std::string& region_name, const Region& region); /// Converts an Ind2D to an Ind3D using calculation - Ind3D ind2Dto3D(const Ind2D& ind2D, int jz = 0) const { + Ind3D ind2Dto3D(const Ind2D& ind2D, int jz = 0) { return {ind2D.ind * LocalNz + jz, LocalNy, LocalNz}; } /// Converts an Ind3D to an Ind2D using calculation - Ind2D ind3Dto2D(const Ind3D& ind3D) const { return {ind3D.ind / LocalNz, LocalNy, 1}; } + Ind2D ind3Dto2D(const Ind3D& ind3D) { return {ind3D.ind / LocalNz, LocalNy, 1}; } /// Converts an Ind3D to an IndPerp using calculation - IndPerp ind3DtoPerp(const Ind3D& ind3D) const { + IndPerp ind3DtoPerp(const Ind3D& ind3D) { return {ind3D.x() * LocalNz + ind3D.z(), 1, LocalNz}; } /// Converts an IndPerp to an Ind3D using calculation - Ind3D indPerpto3D(const IndPerp& indPerp, int jy = 0) const { + Ind3D indPerpto3D(const IndPerp& indPerp, int jy = 0) { int jz = indPerp.z(); return {(indPerp.ind - jz) * LocalNy + LocalNz * jy + jz, LocalNy, LocalNz}; } @@ -819,7 +819,7 @@ protected: /// Calculates the size of a message for a given x and y range int msg_len(const std::vector& var_list, int xge, int xlt, int yge, - int ylt) const; + int ylt); /// Initialise derivatives void derivs_init(Options* options); diff --git a/src/mesh/impls/bout/boutmesh.cxx b/src/mesh/impls/bout/boutmesh.cxx index 097aaf6b6f..bd6b35b9ad 100644 --- a/src/mesh/impls/bout/boutmesh.cxx +++ b/src/mesh/impls/bout/boutmesh.cxx @@ -2319,7 +2319,7 @@ int BoutMesh::pack_data(const std::vector& var_list, int xge, int xl } int BoutMesh::unpack_data(const std::vector& var_list, int xge, int xlt, - int yge, int ylt, const BoutReal* buffer) { + int yge, int ylt, BoutReal* buffer) { int len = 0; diff --git a/src/mesh/impls/bout/boutmesh.hxx b/src/mesh/impls/bout/boutmesh.hxx index f8c2aff33a..546ae9e255 100644 --- a/src/mesh/impls/bout/boutmesh.hxx +++ b/src/mesh/impls/bout/boutmesh.hxx @@ -465,7 +465,7 @@ private: /// Copy data from a buffer back into the fields int unpack_data(const std::vector& var_list, int xge, int xlt, int yge, - int ylt, const BoutReal* buffer); + int ylt, BoutReal* buffer); }; namespace { diff --git a/src/mesh/mesh.cxx b/src/mesh/mesh.cxx index 699f468f2a..460498664e 100644 --- a/src/mesh/mesh.cxx +++ b/src/mesh/mesh.cxx @@ -401,7 +401,7 @@ void Mesh::communicate(FieldPerp& f) { } int Mesh::msg_len(const std::vector& var_list, int xge, int xlt, int yge, - int ylt) const { + int ylt) { int len = 0; /// Loop over variables @@ -550,7 +550,7 @@ int Mesh::globalStartIndexPerp() { return cumulativeSize - localSize; } -std::vector Mesh::readInts(const std::string& name, int n) { +const std::vector Mesh::readInts(const std::string& name, int n) { TRACE("Mesh::readInts({:s})", name); if (source == nullptr) { From 8650f4fa75065d3c18a1f32e8af9e65c9996f704 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 22 May 2024 15:50:45 +0100 Subject: [PATCH 425/491] Add test CheckCovariantCalculatedFromContravariant to CoordinatesTest suite. --- tests/unit/mesh/test_coordinates.cxx | 50 ++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/tests/unit/mesh/test_coordinates.cxx b/tests/unit/mesh/test_coordinates.cxx index 0d26e750e3..caa961a1d6 100644 --- a/tests/unit/mesh/test_coordinates.cxx +++ b/tests/unit/mesh/test_coordinates.cxx @@ -362,6 +362,56 @@ TEST_F(CoordinatesTest, SetContravariantMetricTensor) { EXPECT_TRUE(IsFieldEqual(coords.g23(), 0.2)); } +TEST_F(CoordinatesTest, CheckCovariantCalculatedFromContravariant) { + + // Set initial values for the metric tensor in the Coordinates constructor + Coordinates coords{mesh, + FieldMetric{1.0}, // dx + FieldMetric{1.0}, // dy + FieldMetric{1.0}, // dz + FieldMetric{0.0}, // J + FieldMetric{0.0}, // Bxy + FieldMetric{0.0}, // g11 + FieldMetric{0.0}, // g22 + FieldMetric{0.0}, // g33 + FieldMetric{0.0}, // g12 + FieldMetric{0.0}, // g13 + FieldMetric{0.0}, // g23 + FieldMetric{1.0}, // g_11 + FieldMetric{1.0}, // g_22 + FieldMetric{1.0}, // g_23 + FieldMetric{0.0}, // g_12 + FieldMetric{0.0}, // g_13 + FieldMetric{0.0}, // g_23 + FieldMetric{0.0}, // ShiftTorsion + FieldMetric{0.0}}; // IntShiftTorsion + + // Modify contravariant components + constexpr double g11 = 1.0; + constexpr double g22 = 1.0; + constexpr double g33 = 1.0; + const double g12 = sqrt(3.0)/4.0; // (std::sqrt only constexpr since C++26) + constexpr double g13 = 0.5; + const double g23 = sqrt(3.0)/4.0; // (std::sqrt only constexpr since C++26) + auto updated_metric_tensor = ContravariantMetricTensor(g11, g22, g33, g12, g13, g23); + coords.setContravariantMetricTensor(updated_metric_tensor); + + // Check that the covariant components have been calculated corrected + constexpr double expected_g_11 = 13.0 / 9.0; + constexpr double expected_g_22 = 4.0 / 3.0; + constexpr double expected_g_33 = 13.0 / 9.0; + const double expected_g_12 = -2.0 * sqrt(3.0) / 9.0; + constexpr double expected_g_13 = -5.0 / 9.0; + const double expected_g_23 = -2.0 * sqrt(3.0) / 9.0; + + EXPECT_TRUE(IsFieldEqual(coords.g_11(), expected_g_11)); + EXPECT_TRUE(IsFieldEqual(coords.g_22(), expected_g_22)); + EXPECT_TRUE(IsFieldEqual(coords.g_33(), expected_g_33)); + EXPECT_TRUE(IsFieldEqual(coords.g_12(), expected_g_12)); + EXPECT_TRUE(IsFieldEqual(coords.g_13(), expected_g_13)); + EXPECT_TRUE(IsFieldEqual(coords.g_23(), expected_g_23)); +} + TEST_F(CoordinatesTest, GetCovariantMetricTensor) { Coordinates coords{mesh, FieldMetric{1.0}, // dx From d32f7a6d49fc59e4274310573b7a1f1930d0ed61 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 22 May 2024 15:54:00 +0100 Subject: [PATCH 426/491] Add test CheckContravariantCalculatedFromCovariant to CoordinatesTest suite. --- tests/unit/mesh/test_coordinates.cxx | 50 ++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/tests/unit/mesh/test_coordinates.cxx b/tests/unit/mesh/test_coordinates.cxx index caa961a1d6..a84d790d31 100644 --- a/tests/unit/mesh/test_coordinates.cxx +++ b/tests/unit/mesh/test_coordinates.cxx @@ -412,6 +412,56 @@ TEST_F(CoordinatesTest, CheckCovariantCalculatedFromContravariant) { EXPECT_TRUE(IsFieldEqual(coords.g_23(), expected_g_23)); } +TEST_F(CoordinatesTest, CheckContravariantCalculatedFromCovariant) { + + // Set initial values for the metric tensor in the Coordinates constructor + Coordinates coords{mesh, + FieldMetric{1.0}, // dx + FieldMetric{1.0}, // dy + FieldMetric{1.0}, // dz + FieldMetric{0.0}, // J + FieldMetric{0.0}, // Bxy + FieldMetric{0.0}, // g11 + FieldMetric{0.0}, // g22 + FieldMetric{0.0}, // g33 + FieldMetric{0.0}, // g12 + FieldMetric{0.0}, // g13 + FieldMetric{0.0}, // g23 + FieldMetric{1.0}, // g_11 + FieldMetric{1.0}, // g_22 + FieldMetric{1.0}, // g_23 + FieldMetric{0.0}, // g_12 + FieldMetric{0.0}, // g_13 + FieldMetric{0.0}, // g_23 + FieldMetric{0.0}, // ShiftTorsion + FieldMetric{0.0}}; // IntShiftTorsion + + // Modify covariant components + constexpr double g_11 = 1.0; + constexpr double g_22 = 1.0; + constexpr double g_33 = 1.0; + const double g_12 = sqrt(3.0)/4.0; // (std::sqrt only constexpr since C++26) + constexpr double g_13 = 0.5; + const double g_23 = sqrt(3.0)/4.0; // (std::sqrt only constexpr since C++26) + auto updated_metric_tensor = CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23); + coords.setCovariantMetricTensor(updated_metric_tensor); + + // Check that the contravariant components have been calculated corrected + constexpr double expected_g11 = 13.0 / 9.0; + constexpr double expected_g22 = 4.0 / 3.0; + constexpr double expected_g33 = 13.0 / 9.0; + const double expected_g12 = -2.0 * sqrt(3.0) / 9.0; + constexpr double expected_g13 = -5.0 / 9.0; + const double expected_g23 = -2.0 * sqrt(3.0) / 9.0; + + EXPECT_TRUE(IsFieldEqual(coords.g11(), expected_g11)); + EXPECT_TRUE(IsFieldEqual(coords.g22(), expected_g22)); + EXPECT_TRUE(IsFieldEqual(coords.g33(), expected_g33)); + EXPECT_TRUE(IsFieldEqual(coords.g12(), expected_g12)); + EXPECT_TRUE(IsFieldEqual(coords.g13(), expected_g13)); + EXPECT_TRUE(IsFieldEqual(coords.g23(), expected_g23)); +} + TEST_F(CoordinatesTest, GetCovariantMetricTensor) { Coordinates coords{mesh, FieldMetric{1.0}, // dx From 45abd74f1545d75f0748dbff36dd3e3180243821 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Tue, 4 Jun 2024 12:35:12 +0100 Subject: [PATCH 427/491] Use new method Coordinates::setMetricTensor() when manually setting both co- and contravariant components in the tests and examples. --- examples/2Dturbulence_multigrid/esel.cxx | 5 ++--- examples/6field-simple/elm_6f.cxx | 12 +++++------- examples/conducting-wall-mode/cwm.cxx | 13 +++++-------- examples/constraints/alfven-wave/alfven.cxx | 13 ++++++------- examples/dalf3/dalf3.cxx | 12 +++++------- examples/elm-pb-outerloop/elm_pb_outerloop.cxx | 13 ++++++------- examples/elm-pb/elm_pb.cxx | 12 +++++------- examples/em-drift/2fluid.cxx | 13 ++++++------- examples/gravity_reduced/gravity_reduced.cxx | 10 +++++----- examples/gyro-gem/gem.cxx | 10 ++++------ examples/jorek-compare/jorek_compare.cxx | 13 ++++++------- examples/lapd-drift/lapd_drift.cxx | 12 +++++------- examples/laplacexy/alfven-wave/alfven.cxx | 13 ++++++------- examples/laplacexy/laplace_perp/test.cxx | 12 +++++------- examples/reconnect-2field/2field.cxx | 10 ++++------ examples/shear-alfven-wave/2fluid.cxx | 12 +++++------- examples/tokamak-2fluid/2fluid.cxx | 12 +++++------- examples/uedge-benchmark/ue_bmark.cxx | 10 ++++------ examples/wave-slab/wave_slab.cxx | 12 +++++------- tests/MMS/GBS/gbs.cxx | 12 +++++------- tests/MMS/diffusion/diffusion.cxx | 4 +--- tests/MMS/diffusion2/diffusion.cxx | 9 ++++----- tests/MMS/elm-pb/elm_pb.cxx | 13 ++++++------- tests/MMS/spatial/diffusion/diffusion.cxx | 4 +--- tests/MMS/tokamak/tokamak.cxx | 12 +++++------- tests/MMS/wave-1d/wave.cxx | 4 +--- tests/integrated/test-drift-instability/2fluid.cxx | 13 ++++++------- .../test-interchange-instability/2fluid.cxx | 12 +++++------- tests/integrated/test-laplacexy/loadmetric.cxx | 13 ++++++------- 29 files changed, 134 insertions(+), 181 deletions(-) diff --git a/examples/2Dturbulence_multigrid/esel.cxx b/examples/2Dturbulence_multigrid/esel.cxx index 4cccfbad38..8766d3c73c 100644 --- a/examples/2Dturbulence_multigrid/esel.cxx +++ b/examples/2Dturbulence_multigrid/esel.cxx @@ -71,9 +71,8 @@ class ESEL : public PhysicsModel { // generate coordinate system coord->setBxy(1); - coord->setContravariantMetricTensor( - ContravariantMetricTensor(1.0, 1.0, 1.0, 0.0, 0.0, 0.0)); - coord->setCovariantMetricTensor(CovariantMetricTensor(1.0, 1.0, 1.0, 0.0, 0.0, 0.0)); + coord->setMetricTensor(ContravariantMetricTensor(1.0, 1.0, 1.0, 0.0, 0.0, 0.0), + CovariantMetricTensor(1.0, 1.0, 1.0, 0.0, 0.0, 0.0)); SOLVE_FOR(N, vort); SAVE_REPEAT(phi); diff --git a/examples/6field-simple/elm_6f.cxx b/examples/6field-simple/elm_6f.cxx index 82cf7d9541..f75c35c28e 100644 --- a/examples/6field-simple/elm_6f.cxx +++ b/examples/6field-simple/elm_6f.cxx @@ -1054,25 +1054,23 @@ class Elm_6f : public PhysicsModel { MetricTensor::FieldMetric g11, g22, g33, g12, g13, g23; g11 = SQ(Rxy * Bpxy); g22 = 1.0 / SQ(hthe); - g33 = SQ(I) * coord->g11() + SQ(B0) / coord->g11(); + g33 = SQ(I) * g11 + SQ(B0) / g11; g12 = 0.0; - g13 = -I * coord->g11(); + g13 = -I * g11; g23 = -Btxy / (hthe * Bpxy * Rxy); - coord->setContravariantMetricTensor( - ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); coord->setJ(hthe / Bpxy); coord->setBxy(B0); MetricTensor::FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; - g_11 = 1.0 / coord->g11() + SQ(I * Rxy); + g_11 = 1.0 / g11 + SQ(I * Rxy); g_22 = SQ(B0 * hthe / Bpxy); g_33 = Rxy * Rxy; g_12 = Btxy * hthe * I * Rxy / Bpxy; g_13 = I * Rxy * Rxy; g_23 = Btxy * hthe * Rxy / Bpxy; - coord->setCovariantMetricTensor( - CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + coord->setMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23), + CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); // Set B field vector diff --git a/examples/conducting-wall-mode/cwm.cxx b/examples/conducting-wall-mode/cwm.cxx index cf9255ed1c..e859d7eb4d 100644 --- a/examples/conducting-wall-mode/cwm.cxx +++ b/examples/conducting-wall-mode/cwm.cxx @@ -191,24 +191,21 @@ class CWM : public PhysicsModel { MetricTensor::FieldMetric g11, g22, g33, g12, g13, g23; g11 = SQ(Rxy * Bpxy); g22 = 1.0 / SQ(hthe); - g33 = SQ(I) * coord->g11() + SQ(coord->Bxy()) / coord->g11(); + g33 = SQ(I) * g11 + SQ(coord->Bxy()) / g11; g12 = 0.0; - g13 = -I * coord->g11(); + g13 = -I * g11; g23 = -Btxy / (hthe * Bpxy * Rxy); - coord->setContravariantMetricTensor( - ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); - coord->setJ(hthe / Bpxy); MetricTensor::FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; - g_11 = 1.0 / coord->g11() + SQ(I * Rxy); + g_11 = 1.0 / g11 + SQ(I * Rxy); g_22 = SQ(coord->Bxy() * hthe / Bpxy); g_33 = Rxy * Rxy; g_12 = Btxy * hthe * I * Rxy / Bpxy; g_13 = I * Rxy * Rxy; g_23 = Btxy * hthe * Rxy / Bpxy; - coord->setCovariantMetricTensor( - CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + coord->setMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23), + CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); /**************** SET EVOLVING VARIABLES *************/ diff --git a/examples/constraints/alfven-wave/alfven.cxx b/examples/constraints/alfven-wave/alfven.cxx index ae19b83dd5..afe3953446 100644 --- a/examples/constraints/alfven-wave/alfven.cxx +++ b/examples/constraints/alfven-wave/alfven.cxx @@ -204,24 +204,23 @@ class Alfven : public PhysicsModel { MetricTensor::FieldMetric g11, g22, g33, g12, g13, g23; g11 = SQ(Rxy * Bpxy); g22 = 1.0 / SQ(hthe); - g33 = SQ(sinty) * coord->g11() + SQ(coord->Bxy()) / coord->g11(); + g33 = SQ(sinty) * g11 + SQ(coord->Bxy()) / g11; g12 = 0.0; - g13 = -sinty * coord->g11(); + g13 = -sinty * g11; g23 = -sbp * Btxy / (hthe * Bpxy * Rxy); - coord->setContravariantMetricTensor( - ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); coord->setJ(hthe / Bpxy); MetricTensor::FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; - g_11 = 1.0 / coord->g11() + SQ(sinty * Rxy); + g_11 = 1.0 / g11 + SQ(sinty * Rxy); g_22 = SQ(coord->Bxy() * hthe / Bpxy); g_33 = Rxy * Rxy; g_12 = sbp * Btxy * hthe * sinty * Rxy / Bpxy; g_13 = sinty * Rxy * Rxy; g_23 = sbp * Btxy * hthe * Rxy / Bpxy; - coord->setCovariantMetricTensor( - CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + + coord->setMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23), + CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); } }; diff --git a/examples/dalf3/dalf3.cxx b/examples/dalf3/dalf3.cxx index cee4164881..0898e5ba4e 100644 --- a/examples/dalf3/dalf3.cxx +++ b/examples/dalf3/dalf3.cxx @@ -256,25 +256,23 @@ class DALF3 : public PhysicsModel { MetricTensor::FieldMetric g11, g22, g33, g12, g13, g23; g11 = SQ(Rxy * Bpxy); g22 = 1.0 / SQ(hthe); - g33 = SQ(I) * coord->g11() + SQ(B0) / coord->g11(); + g33 = SQ(I) * g11 + SQ(B0) / g11; g12 = 0.0; - g13 = -I * coord->g11(); + g13 = -I * g11; g23 = -Btxy / (hthe * Bpxy * Rxy); - coord->setContravariantMetricTensor( - ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); coord->setJ(hthe / Bpxy); coord->setBxy(B0); MetricTensor::FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; - g_11 = 1.0 / coord->g11() + SQ(I * Rxy); + g_11 = 1.0 / g11 + SQ(I * Rxy); g_22 = SQ(B0 * hthe / Bpxy); g_33 = Rxy * Rxy; g_12 = Btxy * hthe * I * Rxy / Bpxy; g_13 = I * Rxy * Rxy; g_23 = Btxy * hthe * Rxy / Bpxy; - coord->setCovariantMetricTensor( - CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + coord->setMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23), + CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); SOLVE_FOR3(Vort, Pe, Vpar); comms.add(Vort, Pe, Vpar); diff --git a/examples/elm-pb-outerloop/elm_pb_outerloop.cxx b/examples/elm-pb-outerloop/elm_pb_outerloop.cxx index e7936a3040..074b61525e 100644 --- a/examples/elm-pb-outerloop/elm_pb_outerloop.cxx +++ b/examples/elm-pb-outerloop/elm_pb_outerloop.cxx @@ -1095,25 +1095,24 @@ class ELMpb : public PhysicsModel { MetricTensor::FieldMetric g11, g22, g33, g12, g13, g23; g11 = SQ(Rxy * Bpxy); g22 = 1.0 / SQ(hthe); - g33 = SQ(I) * metric->g11() + SQ(B0) / metric->g11(); + g33 = SQ(I) * g11 + SQ(B0) / g11; g12 = 0.0; - g13 = -I * metric->g11(); + g13 = -I * g11; g23 = -Btxy / (hthe * Bpxy * Rxy); - metric->setContravariantMetricTensor( - ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); metric->setJ(hthe / Bpxy); metric->setBxy(B0); MetricTensor::FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; - g_11 = 1.0 / metric->g11() + SQ(I * Rxy); + g_11 = 1.0 / g11 + SQ(I * Rxy); g_22 = SQ(B0 * hthe / Bpxy); g_33 = Rxy * Rxy; g_12 = Btxy * hthe * I * Rxy / Bpxy; g_13 = I * Rxy * Rxy; g_23 = Btxy * hthe * Rxy / Bpxy; - metric->setCovariantMetricTensor( - CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + + metric->setMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23), + CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); // Set B field vector diff --git a/examples/elm-pb/elm_pb.cxx b/examples/elm-pb/elm_pb.cxx index 5f773d54b8..2ab6f81467 100644 --- a/examples/elm-pb/elm_pb.cxx +++ b/examples/elm-pb/elm_pb.cxx @@ -1036,25 +1036,23 @@ class ELMpb : public PhysicsModel { MetricTensor::FieldMetric g11, g22, g33, g12, g13, g23; g11 = SQ(Rxy * Bpxy); g22 = 1.0 / SQ(hthe); - g33 = SQ(I) * metric->g11() + SQ(B0) / metric->g11(); + g33 = SQ(I) * g11 + SQ(B0) / g11; g12 = 0.0; - g13 = -I * metric->g11(); + g13 = -I * g11; g23 = -Btxy / (hthe * Bpxy * Rxy); - metric->setContravariantMetricTensor( - ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); metric->setJ(hthe / Bpxy); metric->setBxy(B0); MetricTensor::FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; - g_11 = 1.0 / metric->g11() + SQ(I * Rxy); + g_11 = 1.0 / g11 + SQ(I * Rxy); g_22 = SQ(B0 * hthe / Bpxy); g_33 = Rxy * Rxy; g_12 = Btxy * hthe * I * Rxy / Bpxy; g_13 = I * Rxy * Rxy; g_23 = Btxy * hthe * Rxy / Bpxy; - metric->setCovariantMetricTensor( - CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + metric->setMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23), + CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); // Set B field vector diff --git a/examples/em-drift/2fluid.cxx b/examples/em-drift/2fluid.cxx index 9bd53d61d9..41712d5e2f 100644 --- a/examples/em-drift/2fluid.cxx +++ b/examples/em-drift/2fluid.cxx @@ -181,24 +181,23 @@ class EMdrift : public PhysicsModel { MetricTensor::FieldMetric g11, g22, g33, g12, g13, g23; g11 = SQ(Rxy * Bpxy); g22 = 1.0 / SQ(hthe); - g33 = SQ(I) * coord->g11() + SQ(coord->Bxy()) / coord->g11(); + g33 = SQ(I) * g11 + SQ(coord->Bxy()) / g11; g12 = 0.0; - g13 = -I * coord->g11(); + g13 = -I * g11; g23 = -Btxy / (hthe * Bpxy * Rxy); - coord->setContravariantMetricTensor( - ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); coord->setJ(hthe / Bpxy); MetricTensor::FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; - g_11 = 1.0 / coord->g11() + SQ(I * Rxy); + g_11 = 1.0 / g11 + SQ(I * Rxy); g_22 = SQ(coord->Bxy() * hthe / Bpxy); g_33 = Rxy * Rxy; g_12 = Btxy * hthe * I * Rxy / Bpxy; g_13 = I * Rxy * Rxy; g_23 = Btxy * hthe * Rxy / Bpxy; - coord->setCovariantMetricTensor( - CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + + coord->setMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23), + CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); /**************** SET EVOLVING VARIABLES *************/ diff --git a/examples/gravity_reduced/gravity_reduced.cxx b/examples/gravity_reduced/gravity_reduced.cxx index 556b402d6f..fa13bb47c3 100644 --- a/examples/gravity_reduced/gravity_reduced.cxx +++ b/examples/gravity_reduced/gravity_reduced.cxx @@ -116,12 +116,12 @@ class GravityReduced : public PhysicsModel { // Set the metric tensor components to get Lz const auto g33 = SQ(2. * PI / Lz); - coord->setContravariantMetricTensor(ContravariantMetricTensor( - coord->g11(), coord->g22(), g33, coord->g12(), coord->g13(), coord->g23())); - const auto g_33 = 1. / coord->g33(); - coord->setCovariantMetricTensor(CovariantMetricTensor( - coord->g_11(), coord->g_22(), g_33, coord->g_12(), coord->g_13(), coord->g_23())); + coord->setMetricTensor( + ContravariantMetricTensor(coord->g11(), coord->g22(), g33, coord->g12(), + coord->g13(), coord->g23()), + CovariantMetricTensor(coord->g_11(), coord->g_22(), g_33, coord->g_12(), + coord->g_13(), coord->g_23())); /**************** SET EVOLVING VARIABLES *************/ diff --git a/examples/gyro-gem/gem.cxx b/examples/gyro-gem/gem.cxx index 9c3e845eb3..7ffcc12586 100644 --- a/examples/gyro-gem/gem.cxx +++ b/examples/gyro-gem/gem.cxx @@ -372,25 +372,23 @@ class GEM : public PhysicsModel { MetricTensor::FieldMetric g11, g22, g33, g12, g13, g23; g11 = SQ(Rxy * Bpxy); g22 = 1.0 / SQ(hthe); - g33 = SQ(Bxy) / coord->g11(); + g33 = SQ(Bxy) / g11; g12 = 0.0; g13 = 0.; g23 = -Btxy / (hthe * Bpxy * Rxy); - coord->setContravariantMetricTensor( - ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); coord->setJ(hthe / Bpxy); coord->setBxy(Bxy); MetricTensor::FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; - g_11 = 1.0 / coord->g11(); + g_11 = 1.0 / g11; g_22 = SQ(Bxy * hthe / Bpxy); g_33 = Rxy * Rxy; g_12 = 0.; g_13 = 0.; g_23 = Btxy * hthe * Rxy / Bpxy; - coord->setCovariantMetricTensor( - CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + coord->setMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23), + CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); // Set B field vector diff --git a/examples/jorek-compare/jorek_compare.cxx b/examples/jorek-compare/jorek_compare.cxx index 256265ca8c..739c1e7713 100644 --- a/examples/jorek-compare/jorek_compare.cxx +++ b/examples/jorek-compare/jorek_compare.cxx @@ -302,25 +302,24 @@ class Jorek : public PhysicsModel { MetricTensor::FieldMetric g11, g22, g33, g12, g13, g23; g11 = SQ(Rxy * Bpxy); g22 = 1.0 / SQ(hthe); - g33 = SQ(I) * coord->g11() + SQ(B0) / coord->g11(); + g33 = SQ(I) * g11 + SQ(B0) / g11; g12 = 0.0; - g13 = -I * coord->g11(); + g13 = -I * g11; g23 = -Btxy / (hthe * Bpxy * Rxy); - coord->setContravariantMetricTensor( - ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); coord->setJ(hthe / Bpxy); coord->setBxy(B0); MetricTensor::FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; - g_11 = 1.0 / coord->g11() + SQ(I * Rxy); + g_11 = 1.0 / g11 + SQ(I * Rxy); g_22 = SQ(B0 * hthe / Bpxy); g_33 = Rxy * Rxy; g_12 = Btxy * hthe * I * Rxy / Bpxy; g_13 = I * Rxy * Rxy; g_23 = Btxy * hthe * Rxy / Bpxy; - coord->setCovariantMetricTensor( - CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + + coord->setMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23), + CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); // Set B field vector B0vec.covariant = false; diff --git a/examples/lapd-drift/lapd_drift.cxx b/examples/lapd-drift/lapd_drift.cxx index e112551ae3..740515dd9c 100644 --- a/examples/lapd-drift/lapd_drift.cxx +++ b/examples/lapd-drift/lapd_drift.cxx @@ -335,24 +335,22 @@ class LAPDdrift : public PhysicsModel { MetricTensor::FieldMetric g11, g22, g33, g12, g13, g23; g11 = SQ(Rxy * Bpxy); g22 = 1.0 / SQ(hthe); - g33 = SQ(I) * coord->g11() + SQ(coord->Bxy()) / coord->g11(); + g33 = SQ(I) * g11 + SQ(coord->Bxy()) / g11; g12 = 0.0; - g13 = -I * coord->g11(); + g13 = -I * g11; g23 = -Btxy / (hthe * Bpxy * Rxy); - coord->setContravariantMetricTensor( - ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); coord->setJ(hthe / Bpxy); MetricTensor::FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; - g_11 = 1.0 / coord->g11() + SQ(I * Rxy); + g_11 = 1.0 / g11 + SQ(I * Rxy); g_22 = SQ(coord->Bxy() * hthe / Bpxy); g_33 = Rxy * Rxy; g_12 = Btxy * hthe * I * Rxy / Bpxy; g_13 = I * Rxy * Rxy; g_23 = Btxy * hthe * Rxy / Bpxy; - coord->setCovariantMetricTensor( - CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + coord->setMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23), + CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); rho0 = Ni0 * Delp2(phi0) + Perp_Grad_dot_Grad(phi0, Ni0); diff --git a/examples/laplacexy/alfven-wave/alfven.cxx b/examples/laplacexy/alfven-wave/alfven.cxx index ffcd9ba0da..a435c5e88f 100644 --- a/examples/laplacexy/alfven-wave/alfven.cxx +++ b/examples/laplacexy/alfven-wave/alfven.cxx @@ -205,24 +205,23 @@ class Alfven : public PhysicsModel { MetricTensor::FieldMetric g11, g22, g33, g12, g13, g23; g11 = SQ(Rxy * Bpxy); g22 = 1.0 / SQ(hthe); - g33 = SQ(sinty) * coord->g11() + SQ(coord->Bxy()) / coord->g11(); + g33 = SQ(sinty) * g11 + SQ(coord->Bxy()) / g11; g12 = 0.0; - g13 = -sinty * coord->g11(); + g13 = -sinty * g11; g23 = -sbp * Btxy / (hthe * Bpxy * Rxy); - coord->setContravariantMetricTensor( - ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); coord->setJ(hthe / Bpxy); MetricTensor::FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; - g_11 = 1.0 / coord->g11() + SQ(sinty * Rxy); + g_11 = 1.0 / g11 + SQ(sinty * Rxy); g_22 = SQ(coord->Bxy() * hthe / Bpxy); g_33 = Rxy * Rxy; g_12 = sbp * Btxy * hthe * sinty * Rxy / Bpxy; g_13 = sinty * Rxy * Rxy; g_23 = sbp * Btxy * hthe * Rxy / Bpxy; - coord->setCovariantMetricTensor( - CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + + coord->setMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23), + CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); } }; diff --git a/examples/laplacexy/laplace_perp/test.cxx b/examples/laplacexy/laplace_perp/test.cxx index fddced6cb5..7d5b27f11b 100644 --- a/examples/laplacexy/laplace_perp/test.cxx +++ b/examples/laplacexy/laplace_perp/test.cxx @@ -28,25 +28,23 @@ int main(int argc, char** argv) { MetricTensor::FieldMetric g11, g22, g33, g12, g13, g23; g11 = SQ(Rxy * Bpxy); g22 = 1.0 / SQ(hthe); - g33 = SQ(I) * coord->g11() + SQ(B0) / coord->g11(); + g33 = SQ(I) * g11 + SQ(B0) / g11; g12 = 0.0; - g13 = -I * coord->g11(); + g13 = -I * g11; g23 = -Btxy / (hthe * Bpxy * Rxy); - coord->setContravariantMetricTensor( - ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); coord->setJ(hthe / Bpxy); coord->setBxy(B0); MetricTensor::FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; - g_11 = 1.0 / coord->g11() + SQ(I * Rxy); + g_11 = 1.0 / g11 + SQ(I * Rxy); g_22 = SQ(B0 * hthe / Bpxy); g_33 = Rxy * Rxy; g_12 = Btxy * hthe * I * Rxy / Bpxy; g_13 = I * Rxy * Rxy; g_23 = Btxy * hthe * Rxy / Bpxy; - coord->setCovariantMetricTensor( - CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + coord->setMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23), + CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); } /////////////////////////////////////// diff --git a/examples/reconnect-2field/2field.cxx b/examples/reconnect-2field/2field.cxx index 9f02b419e2..a2c7246cd7 100644 --- a/examples/reconnect-2field/2field.cxx +++ b/examples/reconnect-2field/2field.cxx @@ -166,24 +166,22 @@ class TwoField : public PhysicsModel { MetricTensor::FieldMetric g11, g22, g33, g12, g13, g23; g11 = SQ(Rxy * Bpxy); g22 = 1.0 / SQ(hthe); - g33 = SQ(coord->Bxy()) / coord->g11(); + g33 = SQ(coord->Bxy()) / g11; g12 = 0.0; g13 = 0.; g23 = -Btxy / (hthe * Bpxy * Rxy); - coord->setContravariantMetricTensor( - ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); coord->setJ(hthe / Bpxy); MetricTensor::FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; - g_11 = 1.0 / coord->g11(); + g_11 = 1.0 / g11; g_22 = SQ(coord->Bxy() * hthe / Bpxy); g_33 = Rxy * Rxy; g_12 = 0.; g_13 = 0.; g_23 = Btxy * hthe * Rxy / Bpxy; - coord->setCovariantMetricTensor( - CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + coord->setMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23), + CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); // Tell BOUT++ which variables to evolve SOLVE_FOR(U, Apar); diff --git a/examples/shear-alfven-wave/2fluid.cxx b/examples/shear-alfven-wave/2fluid.cxx index ce73d65361..fc181a3b54 100644 --- a/examples/shear-alfven-wave/2fluid.cxx +++ b/examples/shear-alfven-wave/2fluid.cxx @@ -185,24 +185,22 @@ class ShearAlfven : public PhysicsModel { MetricTensor::FieldMetric g11, g22, g33, g12, g13, g23; g11 = SQ(Rxy * Bpxy); g22 = 1.0 / SQ(hthe); - g33 = SQ(I) * coord->g11() + SQ(coord->Bxy()) / coord->g11(); + g33 = SQ(I) * g11 + SQ(coord->Bxy()) / g11; g12 = 0.0; - g13 = -I * coord->g11(); + g13 = -I * g11; g23 = -Btxy / (hthe * Bpxy * Rxy); - coord->setContravariantMetricTensor( - ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); coord->setJ(hthe / Bpxy); MetricTensor::FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; - g_11 = 1.0 / coord->g11() + SQ(I * Rxy); + g_11 = 1.0 / g11 + SQ(I * Rxy); g_22 = SQ(coord->Bxy() * hthe / Bpxy); g_33 = Rxy * Rxy; g_12 = Btxy * hthe * I * Rxy / Bpxy; g_13 = I * Rxy * Rxy; g_23 = Btxy * hthe * Rxy / Bpxy; - coord->setCovariantMetricTensor( - CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + coord->setMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23), + CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); /**************** SET EVOLVING VARIABLES *************/ diff --git a/examples/tokamak-2fluid/2fluid.cxx b/examples/tokamak-2fluid/2fluid.cxx index a67c3898c3..3b8963ac99 100644 --- a/examples/tokamak-2fluid/2fluid.cxx +++ b/examples/tokamak-2fluid/2fluid.cxx @@ -402,24 +402,22 @@ class TwoFluid : public PhysicsModel { MetricTensor::FieldMetric g11, g22, g33, g12, g13, g23; g11 = SQ(Rxy * Bpxy); g22 = 1.0 / SQ(hthe); - g33 = SQ(I) * coord->g11() + SQ(coord->Bxy()) / coord->g11(); + g33 = SQ(I) * g11 + SQ(coord->Bxy()) / g11; g12 = 0.0; - g13 = -I * coord->g11(); + g13 = -I * g11; g23 = -Btxy / (hthe * Bpxy * Rxy); - coord->setContravariantMetricTensor( - ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); coord->setJ(hthe / Bpxy); MetricTensor::FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; - g_11 = 1.0 / coord->g11() + SQ(I * Rxy); + g_11 = 1.0 / g11 + SQ(I * Rxy); g_22 = SQ(coord->Bxy() * hthe / Bpxy); g_33 = Rxy * Rxy; g_12 = Btxy * hthe * I * Rxy / Bpxy; g_13 = I * Rxy * Rxy; g_23 = Btxy * hthe * Rxy / Bpxy; - coord->setCovariantMetricTensor( - CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + coord->setMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23), + CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); //////////////////////////////////////////////////////// // SET EVOLVING VARIABLES diff --git a/examples/uedge-benchmark/ue_bmark.cxx b/examples/uedge-benchmark/ue_bmark.cxx index 07f5746676..a3677e711a 100644 --- a/examples/uedge-benchmark/ue_bmark.cxx +++ b/examples/uedge-benchmark/ue_bmark.cxx @@ -143,24 +143,22 @@ class UedgeBenchmark : public PhysicsModel { MetricTensor::FieldMetric g11, g22, g33, g12, g13, g23; g11 = pow(Rxy * Bpxy, 2.0); g22 = 1.0 / pow(hthe, 2.0); - g33 = pow(coords->Bxy(), 2.0) / coords->g11(); + g33 = pow(coords->Bxy(), 2.0) / g11; g12 = 0.0; g13 = 0.0; g23 = -Btxy / (hthe * Bpxy * Rxy); - coords->setContravariantMetricTensor( - ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); coords->setJ(hthe / Bpxy); MetricTensor::FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; - g_11 = 1.0 / coords->g11(); + g_11 = 1.0 / g11; g_22 = pow(coords->Bxy() * hthe / Bpxy, 2.0); g_33 = Rxy * Rxy; g_12 = 0.0; g_13 = 0.0; g_23 = Btxy * hthe * Rxy / Bpxy; - coords->setCovariantMetricTensor( - CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + coords->setMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23), + CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); //////////////// BOUNDARIES /////////////////////// // diff --git a/examples/wave-slab/wave_slab.cxx b/examples/wave-slab/wave_slab.cxx index 709ef24123..b68547e84b 100644 --- a/examples/wave-slab/wave_slab.cxx +++ b/examples/wave-slab/wave_slab.cxx @@ -34,24 +34,22 @@ class WaveTest : public PhysicsModel { MetricTensor::FieldMetric g11, g22, g33, g12, g13, g23; g11 = pow(Rxy * Bpxy, 2.0); g22 = 1.0 / pow(hthe, 2.0); - g33 = pow(I, 2.0) * coords->g11() + pow(coords->Bxy(), 2.0) / coords->g11(); + g33 = pow(I, 2.0) * g11 + pow(coords->Bxy(), 2.0) / g11; g12 = 0.0; - g13 = -I * coords->g11(); + g13 = -I * g11; g23 = -Btxy / (hthe * Bpxy * Rxy); - coords->setContravariantMetricTensor( - ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); coords->setJ(hthe / Bpxy); MetricTensor::FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; - g_11 = 1.0 / coords->g11() + (pow(I * Rxy, 2.0)); + g_11 = 1.0 / g11 + (pow(I * Rxy, 2.0)); g_22 = pow(coords->Bxy() * hthe / Bpxy, 2.0); g_33 = Rxy * Rxy; g_12 = Btxy * hthe * I * Rxy / Bpxy; g_13 = I * Rxy * Rxy; g_23 = Btxy * hthe * Rxy / Bpxy; - coords->setCovariantMetricTensor( - CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + coords->setMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23), + CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); solver->add(f, "f"); solver->add(g, "g"); diff --git a/tests/MMS/GBS/gbs.cxx b/tests/MMS/GBS/gbs.cxx index ed43f48452..d03bbd04fa 100644 --- a/tests/MMS/GBS/gbs.cxx +++ b/tests/MMS/GBS/gbs.cxx @@ -350,23 +350,21 @@ void GBS::LoadMetric(BoutReal Lnorm, BoutReal Bnorm) { const auto g11 = SQ(Rxy * Bpxy); const auto g22 = 1.0 / SQ(hthe); - const auto g33 = SQ(sinty) * coords->g11() + SQ(coords->Bxy()) / coords->g11(); + const auto g33 = SQ(sinty) * g11 + SQ(coords->Bxy()) / g11; const auto g12 = 0.0; - const auto g13 = -sinty * coords->g11(); + const auto g13 = -sinty * g11; const auto g23 = -sbp * Btxy / (hthe * Bpxy * Rxy); - coords->setContravariantMetricTensor( - ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); coords->setJ(hthe / Bpxy); - const auto g_11 = 1.0 / coords->g11() + SQ(sinty * Rxy); + const auto g_11 = 1.0 / g11 + SQ(sinty * Rxy); const auto g_22 = SQ(coords->Bxy() * hthe / Bpxy); const auto g_33 = Rxy * Rxy; const auto g_12 = sbp * Btxy * hthe * sinty * Rxy / Bpxy; const auto g_13 = sinty * Rxy * Rxy; const auto g_23 = sbp * Btxy * hthe * Rxy / Bpxy; - coords->setCovariantMetricTensor( - CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + coords->setMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23), + CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); } // just define a macro for V_E dot Grad diff --git a/tests/MMS/diffusion/diffusion.cxx b/tests/MMS/diffusion/diffusion.cxx index a39f0485c9..d0e8ed7641 100644 --- a/tests/MMS/diffusion/diffusion.cxx +++ b/tests/MMS/diffusion/diffusion.cxx @@ -45,10 +45,8 @@ int Diffusion::init(bool UNUSED(restarting)) { //set mesh auto contravariant_metric_tensor = ContravariantMetricTensor(1.1, 1.1, 1.1, 0.0, 0.0, 0.0); - coord->setContravariantMetricTensor(contravariant_metric_tensor); - auto covariant_metric_tensor = CovariantMetricTensor(1.1, 1.1, 1.1, 0.0, 0.0, 0.0); - coord->setCovariantMetricTensor(covariant_metric_tensor); + coord->setMetricTensor(contravariant_metric_tensor, covariant_metric_tensor); // Tell BOUT++ to solve N SOLVE_FOR(N); diff --git a/tests/MMS/diffusion2/diffusion.cxx b/tests/MMS/diffusion2/diffusion.cxx index 347f595c66..e60dc4bd4b 100644 --- a/tests/MMS/diffusion2/diffusion.cxx +++ b/tests/MMS/diffusion2/diffusion.cxx @@ -38,11 +38,10 @@ class Diffusion : public PhysicsModel { SAVE_ONCE3(Dx, Dy, Dz); // set mesh - auto contravariant_metric_tensor = MetricTensor(1.1, 1.1, 1.1, 0.0, 0.0, 0.0); - coords->setContravariantMetricTensor(contravariant_metric_tensor); - - auto covariant_metric_tensor = MetricTensor(1.1, 1.1, 1.1, 0.0, 0.0, 0.0); - coords->setCovariantMetricTensor(covariant_metric_tensor); + auto contravariant_metric_tensor = + ContravariantMetricTensor(1.1, 1.1, 1.1, 0.0, 0.0, 0.0); + auto covariant_metric_tensor = CovariantMetricTensor(1.1, 1.1, 1.1, 0.0, 0.0, 0.0); + coords->setMetricTensor(contravariant_metric_tensor, covariant_metric_tensor); // Tell BOUT++ to solve N SOLVE_FOR(N); diff --git a/tests/MMS/elm-pb/elm_pb.cxx b/tests/MMS/elm-pb/elm_pb.cxx index b2e8e0b6b0..90504c2bf5 100644 --- a/tests/MMS/elm-pb/elm_pb.cxx +++ b/tests/MMS/elm-pb/elm_pb.cxx @@ -419,24 +419,23 @@ class ELMpb : public PhysicsModel { const auto g11 = SQ(Rxy * Bpxy); const auto g22 = 1.0 / SQ(hthe); - const auto g33 = SQ(I) * coords->g11() + SQ(B0) / coords->g11(); + const auto g33 = SQ(I) * g11 + SQ(B0) / g11; const auto g12 = 0.0; - const auto g13 = -I * coords->g11(); + const auto g13 = -I * g11; const auto g23 = -Btxy / (hthe * Bpxy * Rxy); - coords->setContravariantMetricTensor( - ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); coords->setJ(hthe / Bpxy); coords->setBxy(B0); - const auto g_11 = 1.0 / coords->g11() + (SQ(I * Rxy)); + const auto g_11 = 1.0 / g11 + (SQ(I * Rxy)); const auto g_22 = SQ(B0 * hthe / Bpxy); const auto g_33 = Rxy * Rxy; const auto g_12 = Btxy * hthe * I * Rxy / Bpxy; const auto g_13 = I * Rxy * Rxy; const auto g_23 = Btxy * hthe * Rxy / Bpxy; - coords->setCovariantMetricTensor( - CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + + coords->setMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23), + CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); // Set B field vector diff --git a/tests/MMS/spatial/diffusion/diffusion.cxx b/tests/MMS/spatial/diffusion/diffusion.cxx index 14b4010df6..6e425a38d6 100644 --- a/tests/MMS/spatial/diffusion/diffusion.cxx +++ b/tests/MMS/spatial/diffusion/diffusion.cxx @@ -41,10 +41,8 @@ class Diffusion : public PhysicsModel { // set mesh auto contravariant_metric_tensor = ContravariantMetricTensor(1.1, 1.1, 1.1, 0.0, 0.0, 0.0); - coords->setContravariantMetricTensor(contravariant_metric_tensor); - auto covariant_metric_tensor = CovariantMetricTensor(1.1, 1.1, 1.1, 0.0, 0.0, 0.0); - coords->setCovariantMetricTensor(covariant_metric_tensor); + coords->setMetricTensor(contravariant_metric_tensor, covariant_metric_tensor); // Tell BOUT++ to solve N SOLVE_FOR(N); diff --git a/tests/MMS/tokamak/tokamak.cxx b/tests/MMS/tokamak/tokamak.cxx index eb2dfef816..c264b137c2 100644 --- a/tests/MMS/tokamak/tokamak.cxx +++ b/tests/MMS/tokamak/tokamak.cxx @@ -82,23 +82,21 @@ class TokamakMMS : public PhysicsModel { const auto g11 = SQ(Rxy * Bpxy); const auto g22 = 1.0 / SQ(hthe); - const auto g33 = SQ(sinty) * coords->g11() + SQ(coords->Bxy()) / coords->g11(); + const auto g33 = SQ(sinty) * g11 + SQ(coords->Bxy()) / g11; const auto g12 = 0.0; - const auto g13 = -sinty * coords->g11(); + const auto g13 = -sinty * g11; const auto g23 = -sbp * Btxy / (hthe * Bpxy * Rxy); - coords->setContravariantMetricTensor( - ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); coords->setJ(hthe / Bpxy); - const auto g_11 = 1.0 / coords->g11() + SQ(sinty * Rxy); + const auto g_11 = 1.0 / g11 + SQ(sinty * Rxy); const auto g_22 = SQ(coords->Bxy() * hthe / Bpxy); const auto g_33 = Rxy * Rxy; const auto g_12 = sbp * Btxy * hthe * sinty * Rxy / Bpxy; const auto g_13 = sinty * Rxy * Rxy; const auto g_23 = sbp * Btxy * hthe * Rxy / Bpxy; - coords->setCovariantMetricTensor( - CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + coords->setMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23), + CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); } private: diff --git a/tests/MMS/wave-1d/wave.cxx b/tests/MMS/wave-1d/wave.cxx index a4fc3158ce..92689b9b2f 100644 --- a/tests/MMS/wave-1d/wave.cxx +++ b/tests/MMS/wave-1d/wave.cxx @@ -34,10 +34,8 @@ class Wave1D : public PhysicsModel { //set mesh auto contravariant_metric_tensor = ContravariantMetricTensor(1.1, 1.1, 1.1, 0.0, 0.0, 0.0); - coord->setContravariantMetricTensor(contravariant_metric_tensor); - auto covariant_metric_tensor = CovariantMetricTensor(1.1, 1.1, 1.1, 0.0, 0.0, 0.0); - coord->setCovariantMetricTensor(covariant_metric_tensor); + coord->setMetricTensor(contravariant_metric_tensor, covariant_metric_tensor); g.setLocation(CELL_XLOW); // g staggered to the left of f diff --git a/tests/integrated/test-drift-instability/2fluid.cxx b/tests/integrated/test-drift-instability/2fluid.cxx index fe2b27b681..9dea9065d0 100644 --- a/tests/integrated/test-drift-instability/2fluid.cxx +++ b/tests/integrated/test-drift-instability/2fluid.cxx @@ -220,24 +220,23 @@ class TwoFluid : public PhysicsModel { MetricTensor::FieldMetric g11, g22, g33, g12, g13, g23; g11 = SQ(Rxy * Bpxy); g22 = 1.0 / SQ(hthe); - g33 = SQ(I) * coord->g11() + SQ(coord->Bxy()) / coord->g11(); + g33 = SQ(I) * g11 + SQ(coord->Bxy()) / g11; g12 = 0.0; - g13 = -I * coord->g11(); + g13 = -I * g11; g23 = -Btxy / (hthe * Bpxy * Rxy); - coord->setContravariantMetricTensor( - ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); coord->setJ(hthe / Bpxy); MetricTensor::FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; - g_11 = 1.0 / coord->g11() + SQ(I * Rxy); + g_11 = 1.0 / g11 + SQ(I * Rxy); g_22 = SQ(coord->Bxy() * hthe / Bpxy); g_33 = Rxy * Rxy; g_12 = Btxy * hthe * I * Rxy / Bpxy; g_13 = I * Rxy * Rxy; g_23 = Btxy * hthe * Rxy / Bpxy; - coord->setCovariantMetricTensor( - CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + + coord->setMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23), + CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); /**************** SET EVOLVING VARIABLES *************/ diff --git a/tests/integrated/test-interchange-instability/2fluid.cxx b/tests/integrated/test-interchange-instability/2fluid.cxx index 64f822b575..4bf52df4c6 100644 --- a/tests/integrated/test-interchange-instability/2fluid.cxx +++ b/tests/integrated/test-interchange-instability/2fluid.cxx @@ -142,24 +142,22 @@ class Interchange : public PhysicsModel { MetricTensor::FieldMetric g11, g22, g33, g12, g13, g23; g11 = SQ(Rxy * Bpxy); g22 = 1.0 / SQ(hthe); - g33 = SQ(I) * coord->g11() + SQ(coord->Bxy()) / coord->g11(); + g33 = SQ(I) * g11 + SQ(coord->Bxy()) / g11; g12 = 0.0; - g13 = -I * coord->g11(); + g13 = -I * g11; g23 = -Btxy / (hthe * Bpxy * Rxy); - coord->setContravariantMetricTensor( - ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); coord->setJ(hthe / Bpxy); MetricTensor::FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; - g_11 = 1.0 / coord->g11() + SQ(I * Rxy); + g_11 = 1.0 / g11 + SQ(I * Rxy); g_22 = SQ(coord->Bxy() * hthe / Bpxy); g_33 = Rxy * Rxy; g_12 = Btxy * hthe * I * Rxy / Bpxy; g_13 = I * Rxy * Rxy; g_23 = Btxy * hthe * Rxy / Bpxy; - coord->setCovariantMetricTensor( - CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + coord->setMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23), + CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); // Tell BOUT++ which variables to evolve SOLVE_FOR2(rho, Ni); diff --git a/tests/integrated/test-laplacexy/loadmetric.cxx b/tests/integrated/test-laplacexy/loadmetric.cxx index c84e5ce476..78999096a7 100644 --- a/tests/integrated/test-laplacexy/loadmetric.cxx +++ b/tests/integrated/test-laplacexy/loadmetric.cxx @@ -51,21 +51,20 @@ void LoadMetric(BoutReal Lnorm, BoutReal Bnorm) { const auto g11 = pow(Rxy * Bpxy, 2); const auto g22 = 1.0 / pow(hthe, 2); - const auto g33 = pow(sinty, 2) * coords->g11() + pow(coords->Bxy(), 2) / coords->g11(); + const auto g33 = pow(sinty, 2) * g11 + pow(coords->Bxy(), 2) / g11; const auto g12 = 0.0; - const auto g13 = -sinty * coords->g11(); + const auto g13 = -sinty * g11; const auto g23 = -sbp * Btxy / (hthe * Bpxy * Rxy); - coords->setContravariantMetricTensor( - ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); coords->setJ(hthe / Bpxy); - const auto g_11 = 1.0 / coords->g11() + pow(sinty * Rxy, 2); + const auto g_11 = 1.0 / g11 + pow(sinty * Rxy, 2); const auto g_22 = pow(coords->Bxy() * hthe / Bpxy, 2); const auto g_33 = Rxy * Rxy; const auto g_12 = sbp * Btxy * hthe * sinty * Rxy / Bpxy; const auto g_13 = sinty * Rxy * Rxy; const auto g_23 = sbp * Btxy * hthe * Rxy / Bpxy; - coords->setCovariantMetricTensor( - CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + + coords->setMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23), + CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); } From 357601d613a1acb6fc72a01f604bdfa95bbdd1f2 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Tue, 4 Jun 2024 12:57:11 +0100 Subject: [PATCH 428/491] Define local variables on assignment (and using auto). --- examples/6field-simple/elm_6f.cxx | 27 +++++++++--------- examples/conducting-wall-mode/cwm.cxx | 28 +++++++++---------- examples/constraints/alfven-wave/alfven.cxx | 26 ++++++++--------- examples/dalf3/dalf3.cxx | 27 +++++++++--------- .../elm-pb-outerloop/elm_pb_outerloop.cxx | 26 ++++++++--------- examples/elm-pb/elm_pb.cxx | 27 +++++++++--------- examples/em-drift/2fluid.cxx | 26 ++++++++--------- examples/gyro-gem/gem.cxx | 27 +++++++++--------- examples/jorek-compare/jorek_compare.cxx | 26 ++++++++--------- examples/lapd-drift/lapd_drift.cxx | 27 +++++++++--------- examples/laplacexy/alfven-wave/alfven.cxx | 26 ++++++++--------- examples/laplacexy/laplace_perp/test.cxx | 27 +++++++++--------- examples/reconnect-2field/2field.cxx | 27 +++++++++--------- examples/shear-alfven-wave/2fluid.cxx | 27 +++++++++--------- examples/tokamak-2fluid/2fluid.cxx | 27 +++++++++--------- examples/uedge-benchmark/ue_bmark.cxx | 27 +++++++++--------- examples/wave-slab/wave_slab.cxx | 27 +++++++++--------- .../test-drift-instability/2fluid.cxx | 26 ++++++++--------- .../test-interchange-instability/2fluid.cxx | 27 +++++++++--------- 19 files changed, 242 insertions(+), 266 deletions(-) diff --git a/examples/6field-simple/elm_6f.cxx b/examples/6field-simple/elm_6f.cxx index f75c35c28e..1eb09c293f 100644 --- a/examples/6field-simple/elm_6f.cxx +++ b/examples/6field-simple/elm_6f.cxx @@ -1051,24 +1051,23 @@ class Elm_6f : public PhysicsModel { /**************** CALCULATE METRICS ******************/ - MetricTensor::FieldMetric g11, g22, g33, g12, g13, g23; - g11 = SQ(Rxy * Bpxy); - g22 = 1.0 / SQ(hthe); - g33 = SQ(I) * g11 + SQ(B0) / g11; - g12 = 0.0; - g13 = -I * g11; - g23 = -Btxy / (hthe * Bpxy * Rxy); + auto g11 = SQ(Rxy * Bpxy); + auto g22 = 1.0 / SQ(hthe); + auto g33 = SQ(I) * g11 + SQ(B0) / g11; + auto g12 = 0.0; + auto g13 = -I * g11; + auto g23 = -Btxy / (hthe * Bpxy * Rxy); coord->setJ(hthe / Bpxy); coord->setBxy(B0); - MetricTensor::FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; - g_11 = 1.0 / g11 + SQ(I * Rxy); - g_22 = SQ(B0 * hthe / Bpxy); - g_33 = Rxy * Rxy; - g_12 = Btxy * hthe * I * Rxy / Bpxy; - g_13 = I * Rxy * Rxy; - g_23 = Btxy * hthe * Rxy / Bpxy; + auto g_11 = 1.0 / g11 + SQ(I * Rxy); + auto g_22 = SQ(B0 * hthe / Bpxy); + auto g_33 = Rxy * Rxy; + auto g_12 = Btxy * hthe * I * Rxy / Bpxy; + auto g_13 = I * Rxy * Rxy; + auto g_23 = Btxy * hthe * Rxy / Bpxy; + coord->setMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23), CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); diff --git a/examples/conducting-wall-mode/cwm.cxx b/examples/conducting-wall-mode/cwm.cxx index e859d7eb4d..83bf392767 100644 --- a/examples/conducting-wall-mode/cwm.cxx +++ b/examples/conducting-wall-mode/cwm.cxx @@ -188,22 +188,22 @@ class CWM : public PhysicsModel { /**************** CALCULATE METRICS ******************/ - MetricTensor::FieldMetric g11, g22, g33, g12, g13, g23; - g11 = SQ(Rxy * Bpxy); - g22 = 1.0 / SQ(hthe); - g33 = SQ(I) * g11 + SQ(coord->Bxy()) / g11; - g12 = 0.0; - g13 = -I * g11; - g23 = -Btxy / (hthe * Bpxy * Rxy); + auto g11 = SQ(Rxy * Bpxy); + auto g22 = 1.0 / SQ(hthe); + auto g33 = SQ(I) * g11 + SQ(coord->Bxy()) / g11; + auto g12 = 0.0; + auto g13 = -I * g11; + auto g23 = -Btxy / (hthe * Bpxy * Rxy); + coord->setJ(hthe / Bpxy); - MetricTensor::FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; - g_11 = 1.0 / g11 + SQ(I * Rxy); - g_22 = SQ(coord->Bxy() * hthe / Bpxy); - g_33 = Rxy * Rxy; - g_12 = Btxy * hthe * I * Rxy / Bpxy; - g_13 = I * Rxy * Rxy; - g_23 = Btxy * hthe * Rxy / Bpxy; + auto g_11 = 1.0 / g11 + SQ(I * Rxy); + auto g_22 = SQ(coord->Bxy() * hthe / Bpxy); + auto g_33 = Rxy * Rxy; + auto g_12 = Btxy * hthe * I * Rxy / Bpxy; + auto g_13 = I * Rxy * Rxy; + auto g_23 = Btxy * hthe * Rxy / Bpxy; + coord->setMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23), CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); diff --git a/examples/constraints/alfven-wave/alfven.cxx b/examples/constraints/alfven-wave/alfven.cxx index afe3953446..ca6c09d4f1 100644 --- a/examples/constraints/alfven-wave/alfven.cxx +++ b/examples/constraints/alfven-wave/alfven.cxx @@ -201,23 +201,21 @@ class Alfven : public PhysicsModel { // Calculate metric components - MetricTensor::FieldMetric g11, g22, g33, g12, g13, g23; - g11 = SQ(Rxy * Bpxy); - g22 = 1.0 / SQ(hthe); - g33 = SQ(sinty) * g11 + SQ(coord->Bxy()) / g11; - g12 = 0.0; - g13 = -sinty * g11; - g23 = -sbp * Btxy / (hthe * Bpxy * Rxy); + auto g11 = SQ(Rxy * Bpxy); + auto g22 = 1.0 / SQ(hthe); + auto g33 = SQ(sinty) * g11 + SQ(coord->Bxy()) / g11; + auto g12 = 0.0; + auto g13 = -sinty * g11; + auto g23 = -sbp * Btxy / (hthe * Bpxy * Rxy); coord->setJ(hthe / Bpxy); - MetricTensor::FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; - g_11 = 1.0 / g11 + SQ(sinty * Rxy); - g_22 = SQ(coord->Bxy() * hthe / Bpxy); - g_33 = Rxy * Rxy; - g_12 = sbp * Btxy * hthe * sinty * Rxy / Bpxy; - g_13 = sinty * Rxy * Rxy; - g_23 = sbp * Btxy * hthe * Rxy / Bpxy; + auto g_11 = 1.0 / g11 + SQ(sinty * Rxy); + auto g_22 = SQ(coord->Bxy() * hthe / Bpxy); + auto g_33 = Rxy * Rxy; + auto g_12 = sbp * Btxy * hthe * sinty * Rxy / Bpxy; + auto g_13 = sinty * Rxy * Rxy; + auto g_23 = sbp * Btxy * hthe * Rxy / Bpxy; coord->setMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23), CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); diff --git a/examples/dalf3/dalf3.cxx b/examples/dalf3/dalf3.cxx index 0898e5ba4e..1a61923c5c 100644 --- a/examples/dalf3/dalf3.cxx +++ b/examples/dalf3/dalf3.cxx @@ -253,24 +253,23 @@ class DALF3 : public PhysicsModel { /////////////////////////////////////////////////// // CALCULATE METRICS - MetricTensor::FieldMetric g11, g22, g33, g12, g13, g23; - g11 = SQ(Rxy * Bpxy); - g22 = 1.0 / SQ(hthe); - g33 = SQ(I) * g11 + SQ(B0) / g11; - g12 = 0.0; - g13 = -I * g11; - g23 = -Btxy / (hthe * Bpxy * Rxy); + auto g11 = SQ(Rxy * Bpxy); + auto g22 = 1.0 / SQ(hthe); + auto g33 = SQ(I) * g11 + SQ(B0) / g11; + auto g12 = 0.0; + auto g13 = -I * g11; + auto g23 = -Btxy / (hthe * Bpxy * Rxy); coord->setJ(hthe / Bpxy); coord->setBxy(B0); - MetricTensor::FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; - g_11 = 1.0 / g11 + SQ(I * Rxy); - g_22 = SQ(B0 * hthe / Bpxy); - g_33 = Rxy * Rxy; - g_12 = Btxy * hthe * I * Rxy / Bpxy; - g_13 = I * Rxy * Rxy; - g_23 = Btxy * hthe * Rxy / Bpxy; + auto g_11 = 1.0 / g11 + SQ(I * Rxy); + auto g_22 = SQ(B0 * hthe / Bpxy); + auto g_33 = Rxy * Rxy; + auto g_12 = Btxy * hthe * I * Rxy / Bpxy; + auto g_13 = I * Rxy * Rxy; + auto g_23 = Btxy * hthe * Rxy / Bpxy; + coord->setMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23), CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); diff --git a/examples/elm-pb-outerloop/elm_pb_outerloop.cxx b/examples/elm-pb-outerloop/elm_pb_outerloop.cxx index 074b61525e..cd0ce0f5bd 100644 --- a/examples/elm-pb-outerloop/elm_pb_outerloop.cxx +++ b/examples/elm-pb-outerloop/elm_pb_outerloop.cxx @@ -1092,24 +1092,22 @@ class ELMpb : public PhysicsModel { /**************** CALCULATE METRICS ******************/ - MetricTensor::FieldMetric g11, g22, g33, g12, g13, g23; - g11 = SQ(Rxy * Bpxy); - g22 = 1.0 / SQ(hthe); - g33 = SQ(I) * g11 + SQ(B0) / g11; - g12 = 0.0; - g13 = -I * g11; - g23 = -Btxy / (hthe * Bpxy * Rxy); + auto g11 = SQ(Rxy * Bpxy); + auto g22 = 1.0 / SQ(hthe); + auto g33 = SQ(I) * g11 + SQ(B0) / g11; + auto g12 = 0.0; + auto g13 = -I * g11; + auto g23 = -Btxy / (hthe * Bpxy * Rxy); metric->setJ(hthe / Bpxy); metric->setBxy(B0); - MetricTensor::FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; - g_11 = 1.0 / g11 + SQ(I * Rxy); - g_22 = SQ(B0 * hthe / Bpxy); - g_33 = Rxy * Rxy; - g_12 = Btxy * hthe * I * Rxy / Bpxy; - g_13 = I * Rxy * Rxy; - g_23 = Btxy * hthe * Rxy / Bpxy; + auto g_11 = 1.0 / g11 + SQ(I * Rxy); + auto g_22 = SQ(B0 * hthe / Bpxy); + auto g_33 = Rxy * Rxy; + auto g_12 = Btxy * hthe * I * Rxy / Bpxy; + auto g_13 = I * Rxy * Rxy; + auto g_23 = Btxy * hthe * Rxy / Bpxy; metric->setMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23), CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); diff --git a/examples/elm-pb/elm_pb.cxx b/examples/elm-pb/elm_pb.cxx index 2ab6f81467..f32a6bc9c1 100644 --- a/examples/elm-pb/elm_pb.cxx +++ b/examples/elm-pb/elm_pb.cxx @@ -1033,24 +1033,23 @@ class ELMpb : public PhysicsModel { /**************** CALCULATE METRICS ******************/ - MetricTensor::FieldMetric g11, g22, g33, g12, g13, g23; - g11 = SQ(Rxy * Bpxy); - g22 = 1.0 / SQ(hthe); - g33 = SQ(I) * g11 + SQ(B0) / g11; - g12 = 0.0; - g13 = -I * g11; - g23 = -Btxy / (hthe * Bpxy * Rxy); + auto g11 = SQ(Rxy * Bpxy); + auto g22 = 1.0 / SQ(hthe); + auto g33 = SQ(I) * g11 + SQ(B0) / g11; + auto g12 = 0.0; + auto g13 = -I * g11; + auto g23 = -Btxy / (hthe * Bpxy * Rxy); metric->setJ(hthe / Bpxy); metric->setBxy(B0); - MetricTensor::FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; - g_11 = 1.0 / g11 + SQ(I * Rxy); - g_22 = SQ(B0 * hthe / Bpxy); - g_33 = Rxy * Rxy; - g_12 = Btxy * hthe * I * Rxy / Bpxy; - g_13 = I * Rxy * Rxy; - g_23 = Btxy * hthe * Rxy / Bpxy; + auto g_11 = 1.0 / g11 + SQ(I * Rxy); + auto g_22 = SQ(B0 * hthe / Bpxy); + auto g_33 = Rxy * Rxy; + auto g_12 = Btxy * hthe * I * Rxy / Bpxy; + auto g_13 = I * Rxy * Rxy; + auto g_23 = Btxy * hthe * Rxy / Bpxy; + metric->setMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23), CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); diff --git a/examples/em-drift/2fluid.cxx b/examples/em-drift/2fluid.cxx index 41712d5e2f..b8614afbb8 100644 --- a/examples/em-drift/2fluid.cxx +++ b/examples/em-drift/2fluid.cxx @@ -178,23 +178,21 @@ class EMdrift : public PhysicsModel { /**************** CALCULATE METRICS ******************/ - MetricTensor::FieldMetric g11, g22, g33, g12, g13, g23; - g11 = SQ(Rxy * Bpxy); - g22 = 1.0 / SQ(hthe); - g33 = SQ(I) * g11 + SQ(coord->Bxy()) / g11; - g12 = 0.0; - g13 = -I * g11; - g23 = -Btxy / (hthe * Bpxy * Rxy); + auto g11 = SQ(Rxy * Bpxy); + auto g22 = 1.0 / SQ(hthe); + auto g33 = SQ(I) * g11 + SQ(coord->Bxy()) / g11; + auto g12 = 0.0; + auto g13 = -I * g11; + auto g23 = -Btxy / (hthe * Bpxy * Rxy); coord->setJ(hthe / Bpxy); - MetricTensor::FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; - g_11 = 1.0 / g11 + SQ(I * Rxy); - g_22 = SQ(coord->Bxy() * hthe / Bpxy); - g_33 = Rxy * Rxy; - g_12 = Btxy * hthe * I * Rxy / Bpxy; - g_13 = I * Rxy * Rxy; - g_23 = Btxy * hthe * Rxy / Bpxy; + auto g_11 = 1.0 / g11 + SQ(I * Rxy); + auto g_22 = SQ(coord->Bxy() * hthe / Bpxy); + auto g_33 = Rxy * Rxy; + auto g_12 = Btxy * hthe * I * Rxy / Bpxy; + auto g_13 = I * Rxy * Rxy; + auto g_23 = Btxy * hthe * Rxy / Bpxy; coord->setMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23), CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); diff --git a/examples/gyro-gem/gem.cxx b/examples/gyro-gem/gem.cxx index 7ffcc12586..7cae7f2436 100644 --- a/examples/gyro-gem/gem.cxx +++ b/examples/gyro-gem/gem.cxx @@ -369,24 +369,23 @@ class GEM : public PhysicsModel { // Metric components - MetricTensor::FieldMetric g11, g22, g33, g12, g13, g23; - g11 = SQ(Rxy * Bpxy); - g22 = 1.0 / SQ(hthe); - g33 = SQ(Bxy) / g11; - g12 = 0.0; - g13 = 0.; - g23 = -Btxy / (hthe * Bpxy * Rxy); + auto g11 = SQ(Rxy * Bpxy); + auto g22 = 1.0 / SQ(hthe); + auto g33 = SQ(Bxy) / g11; + auto g12 = 0.0; + auto g13 = 0.; + auto g23 = -Btxy / (hthe * Bpxy * Rxy); coord->setJ(hthe / Bpxy); coord->setBxy(Bxy); - MetricTensor::FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; - g_11 = 1.0 / g11; - g_22 = SQ(Bxy * hthe / Bpxy); - g_33 = Rxy * Rxy; - g_12 = 0.; - g_13 = 0.; - g_23 = Btxy * hthe * Rxy / Bpxy; + auto g_11 = 1.0 / g11; + auto g_22 = SQ(Bxy * hthe / Bpxy); + auto g_33 = Rxy * Rxy; + auto g_12 = 0.; + auto g_13 = 0.; + auto g_23 = Btxy * hthe * Rxy / Bpxy; + coord->setMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23), CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); diff --git a/examples/jorek-compare/jorek_compare.cxx b/examples/jorek-compare/jorek_compare.cxx index 739c1e7713..583f18c436 100644 --- a/examples/jorek-compare/jorek_compare.cxx +++ b/examples/jorek-compare/jorek_compare.cxx @@ -299,24 +299,22 @@ class Jorek : public PhysicsModel { ////////////////////////////////////////////////////////////// // CALCULATE METRICS - MetricTensor::FieldMetric g11, g22, g33, g12, g13, g23; - g11 = SQ(Rxy * Bpxy); - g22 = 1.0 / SQ(hthe); - g33 = SQ(I) * g11 + SQ(B0) / g11; - g12 = 0.0; - g13 = -I * g11; - g23 = -Btxy / (hthe * Bpxy * Rxy); + auto g11 = SQ(Rxy * Bpxy); + auto g22 = 1.0 / SQ(hthe); + auto g33 = SQ(I) * g11 + SQ(B0) / g11; + auto g12 = 0.0; + auto g13 = -I * g11; + auto g23 = -Btxy / (hthe * Bpxy * Rxy); coord->setJ(hthe / Bpxy); coord->setBxy(B0); - MetricTensor::FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; - g_11 = 1.0 / g11 + SQ(I * Rxy); - g_22 = SQ(B0 * hthe / Bpxy); - g_33 = Rxy * Rxy; - g_12 = Btxy * hthe * I * Rxy / Bpxy; - g_13 = I * Rxy * Rxy; - g_23 = Btxy * hthe * Rxy / Bpxy; + auto g_11 = 1.0 / g11 + SQ(I * Rxy); + auto g_22 = SQ(B0 * hthe / Bpxy); + auto g_33 = Rxy * Rxy; + auto g_12 = Btxy * hthe * I * Rxy / Bpxy; + auto g_13 = I * Rxy * Rxy; + auto g_23 = Btxy * hthe * Rxy / Bpxy; coord->setMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23), CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); diff --git a/examples/lapd-drift/lapd_drift.cxx b/examples/lapd-drift/lapd_drift.cxx index 740515dd9c..0e2a68415a 100644 --- a/examples/lapd-drift/lapd_drift.cxx +++ b/examples/lapd-drift/lapd_drift.cxx @@ -332,23 +332,22 @@ class LAPDdrift : public PhysicsModel { /**************** CALCULATE METRICS ******************/ - MetricTensor::FieldMetric g11, g22, g33, g12, g13, g23; - g11 = SQ(Rxy * Bpxy); - g22 = 1.0 / SQ(hthe); - g33 = SQ(I) * g11 + SQ(coord->Bxy()) / g11; - g12 = 0.0; - g13 = -I * g11; - g23 = -Btxy / (hthe * Bpxy * Rxy); + auto g11 = SQ(Rxy * Bpxy); + auto g22 = 1.0 / SQ(hthe); + auto g33 = SQ(I) * g11 + SQ(coord->Bxy()) / g11; + auto g12 = 0.0; + auto g13 = -I * g11; + auto g23 = -Btxy / (hthe * Bpxy * Rxy); coord->setJ(hthe / Bpxy); - MetricTensor::FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; - g_11 = 1.0 / g11 + SQ(I * Rxy); - g_22 = SQ(coord->Bxy() * hthe / Bpxy); - g_33 = Rxy * Rxy; - g_12 = Btxy * hthe * I * Rxy / Bpxy; - g_13 = I * Rxy * Rxy; - g_23 = Btxy * hthe * Rxy / Bpxy; + auto g_11 = 1.0 / g11 + SQ(I * Rxy); + auto g_22 = SQ(coord->Bxy() * hthe / Bpxy); + auto g_33 = Rxy * Rxy; + auto g_12 = Btxy * hthe * I * Rxy / Bpxy; + auto g_13 = I * Rxy * Rxy; + auto g_23 = Btxy * hthe * Rxy / Bpxy; + coord->setMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23), CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); diff --git a/examples/laplacexy/alfven-wave/alfven.cxx b/examples/laplacexy/alfven-wave/alfven.cxx index a435c5e88f..45a898dd20 100644 --- a/examples/laplacexy/alfven-wave/alfven.cxx +++ b/examples/laplacexy/alfven-wave/alfven.cxx @@ -202,23 +202,21 @@ class Alfven : public PhysicsModel { sbp = -1.0; } - MetricTensor::FieldMetric g11, g22, g33, g12, g13, g23; - g11 = SQ(Rxy * Bpxy); - g22 = 1.0 / SQ(hthe); - g33 = SQ(sinty) * g11 + SQ(coord->Bxy()) / g11; - g12 = 0.0; - g13 = -sinty * g11; - g23 = -sbp * Btxy / (hthe * Bpxy * Rxy); + auto g11 = SQ(Rxy * Bpxy); + auto g22 = 1.0 / SQ(hthe); + auto g33 = SQ(sinty) * g11 + SQ(coord->Bxy()) / g11; + auto g12 = 0.0; + auto g13 = -sinty * g11; + auto g23 = -sbp * Btxy / (hthe * Bpxy * Rxy); coord->setJ(hthe / Bpxy); - MetricTensor::FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; - g_11 = 1.0 / g11 + SQ(sinty * Rxy); - g_22 = SQ(coord->Bxy() * hthe / Bpxy); - g_33 = Rxy * Rxy; - g_12 = sbp * Btxy * hthe * sinty * Rxy / Bpxy; - g_13 = sinty * Rxy * Rxy; - g_23 = sbp * Btxy * hthe * Rxy / Bpxy; + auto g_11 = 1.0 / g11 + SQ(sinty * Rxy); + auto g_22 = SQ(coord->Bxy() * hthe / Bpxy); + auto g_33 = Rxy * Rxy; + auto g_12 = sbp * Btxy * hthe * sinty * Rxy / Bpxy; + auto g_13 = sinty * Rxy * Rxy; + auto g_23 = sbp * Btxy * hthe * Rxy / Bpxy; coord->setMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23), CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); diff --git a/examples/laplacexy/laplace_perp/test.cxx b/examples/laplacexy/laplace_perp/test.cxx index 7d5b27f11b..9dc8bf264e 100644 --- a/examples/laplacexy/laplace_perp/test.cxx +++ b/examples/laplacexy/laplace_perp/test.cxx @@ -25,24 +25,23 @@ int main(int argc, char** argv) { Coordinates* coord = mesh->getCoordinates(); // Calculate metrics - MetricTensor::FieldMetric g11, g22, g33, g12, g13, g23; - g11 = SQ(Rxy * Bpxy); - g22 = 1.0 / SQ(hthe); - g33 = SQ(I) * g11 + SQ(B0) / g11; - g12 = 0.0; - g13 = -I * g11; - g23 = -Btxy / (hthe * Bpxy * Rxy); + auto g11 = SQ(Rxy * Bpxy); + auto g22 = 1.0 / SQ(hthe); + auto g33 = SQ(I) * g11 + SQ(B0) / g11; + auto g12 = 0.0; + auto g13 = -I * g11; + auto g23 = -Btxy / (hthe * Bpxy * Rxy); coord->setJ(hthe / Bpxy); coord->setBxy(B0); - MetricTensor::FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; - g_11 = 1.0 / g11 + SQ(I * Rxy); - g_22 = SQ(B0 * hthe / Bpxy); - g_33 = Rxy * Rxy; - g_12 = Btxy * hthe * I * Rxy / Bpxy; - g_13 = I * Rxy * Rxy; - g_23 = Btxy * hthe * Rxy / Bpxy; + auto g_11 = 1.0 / g11 + SQ(I * Rxy); + auto g_22 = SQ(B0 * hthe / Bpxy); + auto g_33 = Rxy * Rxy; + auto g_12 = Btxy * hthe * I * Rxy / Bpxy; + auto g_13 = I * Rxy * Rxy; + auto g_23 = Btxy * hthe * Rxy / Bpxy; + coord->setMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23), CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); } diff --git a/examples/reconnect-2field/2field.cxx b/examples/reconnect-2field/2field.cxx index a2c7246cd7..5d4fc1cee0 100644 --- a/examples/reconnect-2field/2field.cxx +++ b/examples/reconnect-2field/2field.cxx @@ -163,23 +163,22 @@ class TwoField : public PhysicsModel { // CALCULATE METRICS - MetricTensor::FieldMetric g11, g22, g33, g12, g13, g23; - g11 = SQ(Rxy * Bpxy); - g22 = 1.0 / SQ(hthe); - g33 = SQ(coord->Bxy()) / g11; - g12 = 0.0; - g13 = 0.; - g23 = -Btxy / (hthe * Bpxy * Rxy); + auto g11 = SQ(Rxy * Bpxy); + auto g22 = 1.0 / SQ(hthe); + auto g33 = SQ(coord->Bxy()) / g11; + auto g12 = 0.0; + auto g13 = 0.; + auto g23 = -Btxy / (hthe * Bpxy * Rxy); coord->setJ(hthe / Bpxy); - MetricTensor::FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; - g_11 = 1.0 / g11; - g_22 = SQ(coord->Bxy() * hthe / Bpxy); - g_33 = Rxy * Rxy; - g_12 = 0.; - g_13 = 0.; - g_23 = Btxy * hthe * Rxy / Bpxy; + auto g_11 = 1.0 / g11; + auto g_22 = SQ(coord->Bxy() * hthe / Bpxy); + auto g_33 = Rxy * Rxy; + auto g_12 = 0.; + auto g_13 = 0.; + auto g_23 = Btxy * hthe * Rxy / Bpxy; + coord->setMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23), CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); diff --git a/examples/shear-alfven-wave/2fluid.cxx b/examples/shear-alfven-wave/2fluid.cxx index fc181a3b54..98fbff61e2 100644 --- a/examples/shear-alfven-wave/2fluid.cxx +++ b/examples/shear-alfven-wave/2fluid.cxx @@ -182,23 +182,22 @@ class ShearAlfven : public PhysicsModel { /**************** CALCULATE METRICS ******************/ - MetricTensor::FieldMetric g11, g22, g33, g12, g13, g23; - g11 = SQ(Rxy * Bpxy); - g22 = 1.0 / SQ(hthe); - g33 = SQ(I) * g11 + SQ(coord->Bxy()) / g11; - g12 = 0.0; - g13 = -I * g11; - g23 = -Btxy / (hthe * Bpxy * Rxy); + auto g11 = SQ(Rxy * Bpxy); + auto g22 = 1.0 / SQ(hthe); + auto g33 = SQ(I) * g11 + SQ(coord->Bxy()) / g11; + auto g12 = 0.0; + auto g13 = -I * g11; + auto g23 = -Btxy / (hthe * Bpxy * Rxy); coord->setJ(hthe / Bpxy); - MetricTensor::FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; - g_11 = 1.0 / g11 + SQ(I * Rxy); - g_22 = SQ(coord->Bxy() * hthe / Bpxy); - g_33 = Rxy * Rxy; - g_12 = Btxy * hthe * I * Rxy / Bpxy; - g_13 = I * Rxy * Rxy; - g_23 = Btxy * hthe * Rxy / Bpxy; + auto g_11 = 1.0 / g11 + SQ(I * Rxy); + auto g_22 = SQ(coord->Bxy() * hthe / Bpxy); + auto g_33 = Rxy * Rxy; + auto g_12 = Btxy * hthe * I * Rxy / Bpxy; + auto g_13 = I * Rxy * Rxy; + auto g_23 = Btxy * hthe * Rxy / Bpxy; + coord->setMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23), CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); diff --git a/examples/tokamak-2fluid/2fluid.cxx b/examples/tokamak-2fluid/2fluid.cxx index 3b8963ac99..6329e0f8ec 100644 --- a/examples/tokamak-2fluid/2fluid.cxx +++ b/examples/tokamak-2fluid/2fluid.cxx @@ -399,23 +399,22 @@ class TwoFluid : public PhysicsModel { //////////////////////////////////////////////////////// // CALCULATE METRICS - MetricTensor::FieldMetric g11, g22, g33, g12, g13, g23; - g11 = SQ(Rxy * Bpxy); - g22 = 1.0 / SQ(hthe); - g33 = SQ(I) * g11 + SQ(coord->Bxy()) / g11; - g12 = 0.0; - g13 = -I * g11; - g23 = -Btxy / (hthe * Bpxy * Rxy); + auto g11 = SQ(Rxy * Bpxy); + auto g22 = 1.0 / SQ(hthe); + auto g33 = SQ(I) * g11 + SQ(coord->Bxy()) / g11; + auto g12 = 0.0; + auto g13 = -I * g11; + auto g23 = -Btxy / (hthe * Bpxy * Rxy); coord->setJ(hthe / Bpxy); - MetricTensor::FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; - g_11 = 1.0 / g11 + SQ(I * Rxy); - g_22 = SQ(coord->Bxy() * hthe / Bpxy); - g_33 = Rxy * Rxy; - g_12 = Btxy * hthe * I * Rxy / Bpxy; - g_13 = I * Rxy * Rxy; - g_23 = Btxy * hthe * Rxy / Bpxy; + auto g_11 = 1.0 / g11 + SQ(I * Rxy); + auto g_22 = SQ(coord->Bxy() * hthe / Bpxy); + auto g_33 = Rxy * Rxy; + auto g_12 = Btxy * hthe * I * Rxy / Bpxy; + auto g_13 = I * Rxy * Rxy; + auto g_23 = Btxy * hthe * Rxy / Bpxy; + coord->setMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23), CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); diff --git a/examples/uedge-benchmark/ue_bmark.cxx b/examples/uedge-benchmark/ue_bmark.cxx index a3677e711a..0d3a3b5b28 100644 --- a/examples/uedge-benchmark/ue_bmark.cxx +++ b/examples/uedge-benchmark/ue_bmark.cxx @@ -140,23 +140,22 @@ class UedgeBenchmark : public PhysicsModel { /////////////// CALCULATE METRICS ///////////////// - MetricTensor::FieldMetric g11, g22, g33, g12, g13, g23; - g11 = pow(Rxy * Bpxy, 2.0); - g22 = 1.0 / pow(hthe, 2.0); - g33 = pow(coords->Bxy(), 2.0) / g11; - g12 = 0.0; - g13 = 0.0; - g23 = -Btxy / (hthe * Bpxy * Rxy); + auto g11 = pow(Rxy * Bpxy, 2.0); + auto g22 = 1.0 / pow(hthe, 2.0); + auto g33 = pow(coords->Bxy(), 2.0) / g11; + auto g12 = 0.0; + auto g13 = 0.0; + auto g23 = -Btxy / (hthe * Bpxy * Rxy); coords->setJ(hthe / Bpxy); - MetricTensor::FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; - g_11 = 1.0 / g11; - g_22 = pow(coords->Bxy() * hthe / Bpxy, 2.0); - g_33 = Rxy * Rxy; - g_12 = 0.0; - g_13 = 0.0; - g_23 = Btxy * hthe * Rxy / Bpxy; + auto g_11 = 1.0 / g11; + auto g_22 = pow(coords->Bxy() * hthe / Bpxy, 2.0); + auto g_33 = Rxy * Rxy; + auto g_12 = 0.0; + auto g_13 = 0.0; + auto g_23 = Btxy * hthe * Rxy / Bpxy; + coords->setMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23), CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); diff --git a/examples/wave-slab/wave_slab.cxx b/examples/wave-slab/wave_slab.cxx index b68547e84b..990d8672c9 100644 --- a/examples/wave-slab/wave_slab.cxx +++ b/examples/wave-slab/wave_slab.cxx @@ -31,23 +31,22 @@ class WaveTest : public PhysicsModel { mesh->get(I, "sinty"); } - MetricTensor::FieldMetric g11, g22, g33, g12, g13, g23; - g11 = pow(Rxy * Bpxy, 2.0); - g22 = 1.0 / pow(hthe, 2.0); - g33 = pow(I, 2.0) * g11 + pow(coords->Bxy(), 2.0) / g11; - g12 = 0.0; - g13 = -I * g11; - g23 = -Btxy / (hthe * Bpxy * Rxy); + auto g11 = pow(Rxy * Bpxy, 2.0); + auto g22 = 1.0 / pow(hthe, 2.0); + auto g33 = pow(I, 2.0) * g11 + pow(coords->Bxy(), 2.0) / g11; + auto g12 = 0.0; + auto g13 = -I * g11; + auto g23 = -Btxy / (hthe * Bpxy * Rxy); coords->setJ(hthe / Bpxy); - MetricTensor::FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; - g_11 = 1.0 / g11 + (pow(I * Rxy, 2.0)); - g_22 = pow(coords->Bxy() * hthe / Bpxy, 2.0); - g_33 = Rxy * Rxy; - g_12 = Btxy * hthe * I * Rxy / Bpxy; - g_13 = I * Rxy * Rxy; - g_23 = Btxy * hthe * Rxy / Bpxy; + auto g_11 = 1.0 / g11 + (pow(I * Rxy, 2.0)); + auto g_22 = pow(coords->Bxy() * hthe / Bpxy, 2.0); + auto g_33 = Rxy * Rxy; + auto g_12 = Btxy * hthe * I * Rxy / Bpxy; + auto g_13 = I * Rxy * Rxy; + auto g_23 = Btxy * hthe * Rxy / Bpxy; + coords->setMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23), CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); diff --git a/tests/integrated/test-drift-instability/2fluid.cxx b/tests/integrated/test-drift-instability/2fluid.cxx index 9dea9065d0..f2a881230c 100644 --- a/tests/integrated/test-drift-instability/2fluid.cxx +++ b/tests/integrated/test-drift-instability/2fluid.cxx @@ -217,23 +217,21 @@ class TwoFluid : public PhysicsModel { /**************** CALCULATE METRICS ******************/ - MetricTensor::FieldMetric g11, g22, g33, g12, g13, g23; - g11 = SQ(Rxy * Bpxy); - g22 = 1.0 / SQ(hthe); - g33 = SQ(I) * g11 + SQ(coord->Bxy()) / g11; - g12 = 0.0; - g13 = -I * g11; - g23 = -Btxy / (hthe * Bpxy * Rxy); + auto g11 = SQ(Rxy * Bpxy); + auto g22 = 1.0 / SQ(hthe); + auto g33 = SQ(I) * g11 + SQ(coord->Bxy()) / g11; + auto g12 = 0.0; + auto g13 = -I * g11; + auto g23 = -Btxy / (hthe * Bpxy * Rxy); coord->setJ(hthe / Bpxy); - MetricTensor::FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; - g_11 = 1.0 / g11 + SQ(I * Rxy); - g_22 = SQ(coord->Bxy() * hthe / Bpxy); - g_33 = Rxy * Rxy; - g_12 = Btxy * hthe * I * Rxy / Bpxy; - g_13 = I * Rxy * Rxy; - g_23 = Btxy * hthe * Rxy / Bpxy; + auto g_11 = 1.0 / g11 + SQ(I * Rxy); + auto g_22 = SQ(coord->Bxy() * hthe / Bpxy); + auto g_33 = Rxy * Rxy; + auto g_12 = Btxy * hthe * I * Rxy / Bpxy; + auto g_13 = I * Rxy * Rxy; + auto g_23 = Btxy * hthe * Rxy / Bpxy; coord->setMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23), CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); diff --git a/tests/integrated/test-interchange-instability/2fluid.cxx b/tests/integrated/test-interchange-instability/2fluid.cxx index 4bf52df4c6..8332eba4be 100644 --- a/tests/integrated/test-interchange-instability/2fluid.cxx +++ b/tests/integrated/test-interchange-instability/2fluid.cxx @@ -139,23 +139,22 @@ class Interchange : public PhysicsModel { /**************** CALCULATE METRICS ******************/ - MetricTensor::FieldMetric g11, g22, g33, g12, g13, g23; - g11 = SQ(Rxy * Bpxy); - g22 = 1.0 / SQ(hthe); - g33 = SQ(I) * g11 + SQ(coord->Bxy()) / g11; - g12 = 0.0; - g13 = -I * g11; - g23 = -Btxy / (hthe * Bpxy * Rxy); + auto g11 = SQ(Rxy * Bpxy); + auto g22 = 1.0 / SQ(hthe); + auto g33 = SQ(I) * g11 + SQ(coord->Bxy()) / g11; + auto g12 = 0.0; + auto g13 = -I * g11; + auto g23 = -Btxy / (hthe * Bpxy * Rxy); coord->setJ(hthe / Bpxy); - MetricTensor::FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; - g_11 = 1.0 / g11 + SQ(I * Rxy); - g_22 = SQ(coord->Bxy() * hthe / Bpxy); - g_33 = Rxy * Rxy; - g_12 = Btxy * hthe * I * Rxy / Bpxy; - g_13 = I * Rxy * Rxy; - g_23 = Btxy * hthe * Rxy / Bpxy; + auto g_11 = 1.0 / g11 + SQ(I * Rxy); + auto g_22 = SQ(coord->Bxy() * hthe / Bpxy); + auto g_33 = Rxy * Rxy; + auto g_12 = Btxy * hthe * I * Rxy / Bpxy; + auto g_13 = I * Rxy * Rxy; + auto g_23 = Btxy * hthe * Rxy / Bpxy; + coord->setMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23), CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); From e713a5ef85dd39fa7dad11ac13660e3a139f0d55 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Tue, 4 Jun 2024 13:13:26 +0100 Subject: [PATCH 429/491] Prefer const. --- examples/6field-simple/elm_6f.cxx | 24 +++++++++---------- examples/conducting-wall-mode/cwm.cxx | 24 +++++++++---------- examples/constraints/alfven-wave/alfven.cxx | 24 +++++++++---------- examples/dalf3/dalf3.cxx | 24 +++++++++---------- .../elm-pb-outerloop/elm_pb_outerloop.cxx | 24 +++++++++---------- examples/elm-pb/elm_pb.cxx | 24 +++++++++---------- examples/em-drift/2fluid.cxx | 24 +++++++++---------- examples/gyro-gem/gem.cxx | 24 +++++++++---------- examples/jorek-compare/jorek_compare.cxx | 24 +++++++++---------- examples/lapd-drift/lapd_drift.cxx | 24 +++++++++---------- examples/laplacexy/alfven-wave/alfven.cxx | 24 +++++++++---------- examples/laplacexy/laplace_perp/test.cxx | 24 +++++++++---------- examples/reconnect-2field/2field.cxx | 24 +++++++++---------- examples/shear-alfven-wave/2fluid.cxx | 24 +++++++++---------- examples/tokamak-2fluid/2fluid.cxx | 24 +++++++++---------- examples/uedge-benchmark/ue_bmark.cxx | 24 +++++++++---------- examples/wave-slab/wave_slab.cxx | 24 +++++++++---------- .../test-drift-instability/2fluid.cxx | 24 +++++++++---------- .../test-interchange-instability/2fluid.cxx | 24 +++++++++---------- 19 files changed, 228 insertions(+), 228 deletions(-) diff --git a/examples/6field-simple/elm_6f.cxx b/examples/6field-simple/elm_6f.cxx index 1eb09c293f..b3914c26a8 100644 --- a/examples/6field-simple/elm_6f.cxx +++ b/examples/6field-simple/elm_6f.cxx @@ -1051,22 +1051,22 @@ class Elm_6f : public PhysicsModel { /**************** CALCULATE METRICS ******************/ - auto g11 = SQ(Rxy * Bpxy); - auto g22 = 1.0 / SQ(hthe); - auto g33 = SQ(I) * g11 + SQ(B0) / g11; - auto g12 = 0.0; - auto g13 = -I * g11; - auto g23 = -Btxy / (hthe * Bpxy * Rxy); + const auto g11 = SQ(Rxy * Bpxy); + const auto g22 = 1.0 / SQ(hthe); + const auto g33 = SQ(I) * g11 + SQ(B0) / g11; + const auto g12 = 0.0; + const auto g13 = -I * g11; + const auto g23 = -Btxy / (hthe * Bpxy * Rxy); coord->setJ(hthe / Bpxy); coord->setBxy(B0); - auto g_11 = 1.0 / g11 + SQ(I * Rxy); - auto g_22 = SQ(B0 * hthe / Bpxy); - auto g_33 = Rxy * Rxy; - auto g_12 = Btxy * hthe * I * Rxy / Bpxy; - auto g_13 = I * Rxy * Rxy; - auto g_23 = Btxy * hthe * Rxy / Bpxy; + const auto g_11 = 1.0 / g11 + SQ(I * Rxy); + const auto g_22 = SQ(B0 * hthe / Bpxy); + const auto g_33 = Rxy * Rxy; + const auto g_12 = Btxy * hthe * I * Rxy / Bpxy; + const auto g_13 = I * Rxy * Rxy; + const auto g_23 = Btxy * hthe * Rxy / Bpxy; coord->setMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23), CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); diff --git a/examples/conducting-wall-mode/cwm.cxx b/examples/conducting-wall-mode/cwm.cxx index 83bf392767..486759c0d5 100644 --- a/examples/conducting-wall-mode/cwm.cxx +++ b/examples/conducting-wall-mode/cwm.cxx @@ -188,21 +188,21 @@ class CWM : public PhysicsModel { /**************** CALCULATE METRICS ******************/ - auto g11 = SQ(Rxy * Bpxy); - auto g22 = 1.0 / SQ(hthe); - auto g33 = SQ(I) * g11 + SQ(coord->Bxy()) / g11; - auto g12 = 0.0; - auto g13 = -I * g11; - auto g23 = -Btxy / (hthe * Bpxy * Rxy); + const auto g11 = SQ(Rxy * Bpxy); + const auto g22 = 1.0 / SQ(hthe); + const auto g33 = SQ(I) * g11 + SQ(coord->Bxy()) / g11; + const auto g12 = 0.0; + const auto g13 = -I * g11; + const auto g23 = -Btxy / (hthe * Bpxy * Rxy); coord->setJ(hthe / Bpxy); - auto g_11 = 1.0 / g11 + SQ(I * Rxy); - auto g_22 = SQ(coord->Bxy() * hthe / Bpxy); - auto g_33 = Rxy * Rxy; - auto g_12 = Btxy * hthe * I * Rxy / Bpxy; - auto g_13 = I * Rxy * Rxy; - auto g_23 = Btxy * hthe * Rxy / Bpxy; + const auto g_11 = 1.0 / g11 + SQ(I * Rxy); + const auto g_22 = SQ(coord->Bxy() * hthe / Bpxy); + const auto g_33 = Rxy * Rxy; + const auto g_12 = Btxy * hthe * I * Rxy / Bpxy; + const auto g_13 = I * Rxy * Rxy; + const auto g_23 = Btxy * hthe * Rxy / Bpxy; coord->setMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23), CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); diff --git a/examples/constraints/alfven-wave/alfven.cxx b/examples/constraints/alfven-wave/alfven.cxx index ca6c09d4f1..3f0442ed45 100644 --- a/examples/constraints/alfven-wave/alfven.cxx +++ b/examples/constraints/alfven-wave/alfven.cxx @@ -201,21 +201,21 @@ class Alfven : public PhysicsModel { // Calculate metric components - auto g11 = SQ(Rxy * Bpxy); - auto g22 = 1.0 / SQ(hthe); - auto g33 = SQ(sinty) * g11 + SQ(coord->Bxy()) / g11; - auto g12 = 0.0; - auto g13 = -sinty * g11; - auto g23 = -sbp * Btxy / (hthe * Bpxy * Rxy); + const auto g11 = SQ(Rxy * Bpxy); + const auto g22 = 1.0 / SQ(hthe); + const auto g33 = SQ(sinty) * g11 + SQ(coord->Bxy()) / g11; + const auto g12 = 0.0; + const auto g13 = -sinty * g11; + const auto g23 = -sbp * Btxy / (hthe * Bpxy * Rxy); coord->setJ(hthe / Bpxy); - auto g_11 = 1.0 / g11 + SQ(sinty * Rxy); - auto g_22 = SQ(coord->Bxy() * hthe / Bpxy); - auto g_33 = Rxy * Rxy; - auto g_12 = sbp * Btxy * hthe * sinty * Rxy / Bpxy; - auto g_13 = sinty * Rxy * Rxy; - auto g_23 = sbp * Btxy * hthe * Rxy / Bpxy; + const auto g_11 = 1.0 / g11 + SQ(sinty * Rxy); + const auto g_22 = SQ(coord->Bxy() * hthe / Bpxy); + const auto g_33 = Rxy * Rxy; + const auto g_12 = sbp * Btxy * hthe * sinty * Rxy / Bpxy; + const auto g_13 = sinty * Rxy * Rxy; + const auto g_23 = sbp * Btxy * hthe * Rxy / Bpxy; coord->setMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23), CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); diff --git a/examples/dalf3/dalf3.cxx b/examples/dalf3/dalf3.cxx index 1a61923c5c..80db2eec19 100644 --- a/examples/dalf3/dalf3.cxx +++ b/examples/dalf3/dalf3.cxx @@ -253,22 +253,22 @@ class DALF3 : public PhysicsModel { /////////////////////////////////////////////////// // CALCULATE METRICS - auto g11 = SQ(Rxy * Bpxy); - auto g22 = 1.0 / SQ(hthe); - auto g33 = SQ(I) * g11 + SQ(B0) / g11; - auto g12 = 0.0; - auto g13 = -I * g11; - auto g23 = -Btxy / (hthe * Bpxy * Rxy); + const auto g11 = SQ(Rxy * Bpxy); + const auto g22 = 1.0 / SQ(hthe); + const auto g33 = SQ(I) * g11 + SQ(B0) / g11; + const auto g12 = 0.0; + const auto g13 = -I * g11; + const auto g23 = -Btxy / (hthe * Bpxy * Rxy); coord->setJ(hthe / Bpxy); coord->setBxy(B0); - auto g_11 = 1.0 / g11 + SQ(I * Rxy); - auto g_22 = SQ(B0 * hthe / Bpxy); - auto g_33 = Rxy * Rxy; - auto g_12 = Btxy * hthe * I * Rxy / Bpxy; - auto g_13 = I * Rxy * Rxy; - auto g_23 = Btxy * hthe * Rxy / Bpxy; + const auto g_11 = 1.0 / g11 + SQ(I * Rxy); + const auto g_22 = SQ(B0 * hthe / Bpxy); + const auto g_33 = Rxy * Rxy; + const auto g_12 = Btxy * hthe * I * Rxy / Bpxy; + const auto g_13 = I * Rxy * Rxy; + const auto g_23 = Btxy * hthe * Rxy / Bpxy; coord->setMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23), CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); diff --git a/examples/elm-pb-outerloop/elm_pb_outerloop.cxx b/examples/elm-pb-outerloop/elm_pb_outerloop.cxx index cd0ce0f5bd..259fe9faca 100644 --- a/examples/elm-pb-outerloop/elm_pb_outerloop.cxx +++ b/examples/elm-pb-outerloop/elm_pb_outerloop.cxx @@ -1092,22 +1092,22 @@ class ELMpb : public PhysicsModel { /**************** CALCULATE METRICS ******************/ - auto g11 = SQ(Rxy * Bpxy); - auto g22 = 1.0 / SQ(hthe); - auto g33 = SQ(I) * g11 + SQ(B0) / g11; - auto g12 = 0.0; - auto g13 = -I * g11; - auto g23 = -Btxy / (hthe * Bpxy * Rxy); + const auto g11 = SQ(Rxy * Bpxy); + const auto g22 = 1.0 / SQ(hthe); + const auto g33 = SQ(I) * g11 + SQ(B0) / g11; + const auto g12 = 0.0; + const auto g13 = -I * g11; + const auto g23 = -Btxy / (hthe * Bpxy * Rxy); metric->setJ(hthe / Bpxy); metric->setBxy(B0); - auto g_11 = 1.0 / g11 + SQ(I * Rxy); - auto g_22 = SQ(B0 * hthe / Bpxy); - auto g_33 = Rxy * Rxy; - auto g_12 = Btxy * hthe * I * Rxy / Bpxy; - auto g_13 = I * Rxy * Rxy; - auto g_23 = Btxy * hthe * Rxy / Bpxy; + const auto g_11 = 1.0 / g11 + SQ(I * Rxy); + const auto g_22 = SQ(B0 * hthe / Bpxy); + const auto g_33 = Rxy * Rxy; + const auto g_12 = Btxy * hthe * I * Rxy / Bpxy; + const auto g_13 = I * Rxy * Rxy; + const auto g_23 = Btxy * hthe * Rxy / Bpxy; metric->setMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23), CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); diff --git a/examples/elm-pb/elm_pb.cxx b/examples/elm-pb/elm_pb.cxx index f32a6bc9c1..e7a3263278 100644 --- a/examples/elm-pb/elm_pb.cxx +++ b/examples/elm-pb/elm_pb.cxx @@ -1033,22 +1033,22 @@ class ELMpb : public PhysicsModel { /**************** CALCULATE METRICS ******************/ - auto g11 = SQ(Rxy * Bpxy); - auto g22 = 1.0 / SQ(hthe); - auto g33 = SQ(I) * g11 + SQ(B0) / g11; - auto g12 = 0.0; - auto g13 = -I * g11; - auto g23 = -Btxy / (hthe * Bpxy * Rxy); + const auto g11 = SQ(Rxy * Bpxy); + const auto g22 = 1.0 / SQ(hthe); + const auto g33 = SQ(I) * g11 + SQ(B0) / g11; + const auto g12 = 0.0; + const auto g13 = -I * g11; + const auto g23 = -Btxy / (hthe * Bpxy * Rxy); metric->setJ(hthe / Bpxy); metric->setBxy(B0); - auto g_11 = 1.0 / g11 + SQ(I * Rxy); - auto g_22 = SQ(B0 * hthe / Bpxy); - auto g_33 = Rxy * Rxy; - auto g_12 = Btxy * hthe * I * Rxy / Bpxy; - auto g_13 = I * Rxy * Rxy; - auto g_23 = Btxy * hthe * Rxy / Bpxy; + const auto g_11 = 1.0 / g11 + SQ(I * Rxy); + const auto g_22 = SQ(B0 * hthe / Bpxy); + const auto g_33 = Rxy * Rxy; + const auto g_12 = Btxy * hthe * I * Rxy / Bpxy; + const auto g_13 = I * Rxy * Rxy; + const auto g_23 = Btxy * hthe * Rxy / Bpxy; metric->setMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23), CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); diff --git a/examples/em-drift/2fluid.cxx b/examples/em-drift/2fluid.cxx index b8614afbb8..b4dbfdd676 100644 --- a/examples/em-drift/2fluid.cxx +++ b/examples/em-drift/2fluid.cxx @@ -178,21 +178,21 @@ class EMdrift : public PhysicsModel { /**************** CALCULATE METRICS ******************/ - auto g11 = SQ(Rxy * Bpxy); - auto g22 = 1.0 / SQ(hthe); - auto g33 = SQ(I) * g11 + SQ(coord->Bxy()) / g11; - auto g12 = 0.0; - auto g13 = -I * g11; - auto g23 = -Btxy / (hthe * Bpxy * Rxy); + const auto g11 = SQ(Rxy * Bpxy); + const auto g22 = 1.0 / SQ(hthe); + const auto g33 = SQ(I) * g11 + SQ(coord->Bxy()) / g11; + const auto g12 = 0.0; + const auto g13 = -I * g11; + const auto g23 = -Btxy / (hthe * Bpxy * Rxy); coord->setJ(hthe / Bpxy); - auto g_11 = 1.0 / g11 + SQ(I * Rxy); - auto g_22 = SQ(coord->Bxy() * hthe / Bpxy); - auto g_33 = Rxy * Rxy; - auto g_12 = Btxy * hthe * I * Rxy / Bpxy; - auto g_13 = I * Rxy * Rxy; - auto g_23 = Btxy * hthe * Rxy / Bpxy; + const auto g_11 = 1.0 / g11 + SQ(I * Rxy); + const auto g_22 = SQ(coord->Bxy() * hthe / Bpxy); + const auto g_33 = Rxy * Rxy; + const auto g_12 = Btxy * hthe * I * Rxy / Bpxy; + const auto g_13 = I * Rxy * Rxy; + const auto g_23 = Btxy * hthe * Rxy / Bpxy; coord->setMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23), CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); diff --git a/examples/gyro-gem/gem.cxx b/examples/gyro-gem/gem.cxx index 7cae7f2436..f8c42397a3 100644 --- a/examples/gyro-gem/gem.cxx +++ b/examples/gyro-gem/gem.cxx @@ -369,22 +369,22 @@ class GEM : public PhysicsModel { // Metric components - auto g11 = SQ(Rxy * Bpxy); - auto g22 = 1.0 / SQ(hthe); - auto g33 = SQ(Bxy) / g11; - auto g12 = 0.0; - auto g13 = 0.; - auto g23 = -Btxy / (hthe * Bpxy * Rxy); + const auto g11 = SQ(Rxy * Bpxy); + const auto g22 = 1.0 / SQ(hthe); + const auto g33 = SQ(Bxy) / g11; + const auto g12 = 0.0; + const auto g13 = 0.; + const auto g23 = -Btxy / (hthe * Bpxy * Rxy); coord->setJ(hthe / Bpxy); coord->setBxy(Bxy); - auto g_11 = 1.0 / g11; - auto g_22 = SQ(Bxy * hthe / Bpxy); - auto g_33 = Rxy * Rxy; - auto g_12 = 0.; - auto g_13 = 0.; - auto g_23 = Btxy * hthe * Rxy / Bpxy; + const auto g_11 = 1.0 / g11; + const auto g_22 = SQ(Bxy * hthe / Bpxy); + const auto g_33 = Rxy * Rxy; + const auto g_12 = 0.; + const auto g_13 = 0.; + const auto g_23 = Btxy * hthe * Rxy / Bpxy; coord->setMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23), CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); diff --git a/examples/jorek-compare/jorek_compare.cxx b/examples/jorek-compare/jorek_compare.cxx index 583f18c436..9b6d3fdbea 100644 --- a/examples/jorek-compare/jorek_compare.cxx +++ b/examples/jorek-compare/jorek_compare.cxx @@ -299,22 +299,22 @@ class Jorek : public PhysicsModel { ////////////////////////////////////////////////////////////// // CALCULATE METRICS - auto g11 = SQ(Rxy * Bpxy); - auto g22 = 1.0 / SQ(hthe); - auto g33 = SQ(I) * g11 + SQ(B0) / g11; - auto g12 = 0.0; - auto g13 = -I * g11; - auto g23 = -Btxy / (hthe * Bpxy * Rxy); + const auto g11 = SQ(Rxy * Bpxy); + const auto g22 = 1.0 / SQ(hthe); + const auto g33 = SQ(I) * g11 + SQ(B0) / g11; + const auto g12 = 0.0; + const auto g13 = -I * g11; + const auto g23 = -Btxy / (hthe * Bpxy * Rxy); coord->setJ(hthe / Bpxy); coord->setBxy(B0); - auto g_11 = 1.0 / g11 + SQ(I * Rxy); - auto g_22 = SQ(B0 * hthe / Bpxy); - auto g_33 = Rxy * Rxy; - auto g_12 = Btxy * hthe * I * Rxy / Bpxy; - auto g_13 = I * Rxy * Rxy; - auto g_23 = Btxy * hthe * Rxy / Bpxy; + const auto g_11 = 1.0 / g11 + SQ(I * Rxy); + const auto g_22 = SQ(B0 * hthe / Bpxy); + const auto g_33 = Rxy * Rxy; + const auto g_12 = Btxy * hthe * I * Rxy / Bpxy; + const auto g_13 = I * Rxy * Rxy; + const auto g_23 = Btxy * hthe * Rxy / Bpxy; coord->setMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23), CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); diff --git a/examples/lapd-drift/lapd_drift.cxx b/examples/lapd-drift/lapd_drift.cxx index 0e2a68415a..cb9596e690 100644 --- a/examples/lapd-drift/lapd_drift.cxx +++ b/examples/lapd-drift/lapd_drift.cxx @@ -332,21 +332,21 @@ class LAPDdrift : public PhysicsModel { /**************** CALCULATE METRICS ******************/ - auto g11 = SQ(Rxy * Bpxy); - auto g22 = 1.0 / SQ(hthe); - auto g33 = SQ(I) * g11 + SQ(coord->Bxy()) / g11; - auto g12 = 0.0; - auto g13 = -I * g11; - auto g23 = -Btxy / (hthe * Bpxy * Rxy); + const auto g11 = SQ(Rxy * Bpxy); + const auto g22 = 1.0 / SQ(hthe); + const auto g33 = SQ(I) * g11 + SQ(coord->Bxy()) / g11; + const auto g12 = 0.0; + const auto g13 = -I * g11; + const auto g23 = -Btxy / (hthe * Bpxy * Rxy); coord->setJ(hthe / Bpxy); - auto g_11 = 1.0 / g11 + SQ(I * Rxy); - auto g_22 = SQ(coord->Bxy() * hthe / Bpxy); - auto g_33 = Rxy * Rxy; - auto g_12 = Btxy * hthe * I * Rxy / Bpxy; - auto g_13 = I * Rxy * Rxy; - auto g_23 = Btxy * hthe * Rxy / Bpxy; + const auto g_11 = 1.0 / g11 + SQ(I * Rxy); + const auto g_22 = SQ(coord->Bxy() * hthe / Bpxy); + const auto g_33 = Rxy * Rxy; + const auto g_12 = Btxy * hthe * I * Rxy / Bpxy; + const auto g_13 = I * Rxy * Rxy; + const auto g_23 = Btxy * hthe * Rxy / Bpxy; coord->setMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23), CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); diff --git a/examples/laplacexy/alfven-wave/alfven.cxx b/examples/laplacexy/alfven-wave/alfven.cxx index 45a898dd20..7b22d5e887 100644 --- a/examples/laplacexy/alfven-wave/alfven.cxx +++ b/examples/laplacexy/alfven-wave/alfven.cxx @@ -202,21 +202,21 @@ class Alfven : public PhysicsModel { sbp = -1.0; } - auto g11 = SQ(Rxy * Bpxy); - auto g22 = 1.0 / SQ(hthe); - auto g33 = SQ(sinty) * g11 + SQ(coord->Bxy()) / g11; - auto g12 = 0.0; - auto g13 = -sinty * g11; - auto g23 = -sbp * Btxy / (hthe * Bpxy * Rxy); + const auto g11 = SQ(Rxy * Bpxy); + const auto g22 = 1.0 / SQ(hthe); + const auto g33 = SQ(sinty) * g11 + SQ(coord->Bxy()) / g11; + const auto g12 = 0.0; + const auto g13 = -sinty * g11; + const auto g23 = -sbp * Btxy / (hthe * Bpxy * Rxy); coord->setJ(hthe / Bpxy); - auto g_11 = 1.0 / g11 + SQ(sinty * Rxy); - auto g_22 = SQ(coord->Bxy() * hthe / Bpxy); - auto g_33 = Rxy * Rxy; - auto g_12 = sbp * Btxy * hthe * sinty * Rxy / Bpxy; - auto g_13 = sinty * Rxy * Rxy; - auto g_23 = sbp * Btxy * hthe * Rxy / Bpxy; + const auto g_11 = 1.0 / g11 + SQ(sinty * Rxy); + const auto g_22 = SQ(coord->Bxy() * hthe / Bpxy); + const auto g_33 = Rxy * Rxy; + const auto g_12 = sbp * Btxy * hthe * sinty * Rxy / Bpxy; + const auto g_13 = sinty * Rxy * Rxy; + const auto g_23 = sbp * Btxy * hthe * Rxy / Bpxy; coord->setMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23), CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); diff --git a/examples/laplacexy/laplace_perp/test.cxx b/examples/laplacexy/laplace_perp/test.cxx index 9dc8bf264e..766a16ca40 100644 --- a/examples/laplacexy/laplace_perp/test.cxx +++ b/examples/laplacexy/laplace_perp/test.cxx @@ -25,22 +25,22 @@ int main(int argc, char** argv) { Coordinates* coord = mesh->getCoordinates(); // Calculate metrics - auto g11 = SQ(Rxy * Bpxy); - auto g22 = 1.0 / SQ(hthe); - auto g33 = SQ(I) * g11 + SQ(B0) / g11; - auto g12 = 0.0; - auto g13 = -I * g11; - auto g23 = -Btxy / (hthe * Bpxy * Rxy); + const auto g11 = SQ(Rxy * Bpxy); + const auto g22 = 1.0 / SQ(hthe); + const auto g33 = SQ(I) * g11 + SQ(B0) / g11; + const auto g12 = 0.0; + const auto g13 = -I * g11; + const auto g23 = -Btxy / (hthe * Bpxy * Rxy); coord->setJ(hthe / Bpxy); coord->setBxy(B0); - auto g_11 = 1.0 / g11 + SQ(I * Rxy); - auto g_22 = SQ(B0 * hthe / Bpxy); - auto g_33 = Rxy * Rxy; - auto g_12 = Btxy * hthe * I * Rxy / Bpxy; - auto g_13 = I * Rxy * Rxy; - auto g_23 = Btxy * hthe * Rxy / Bpxy; + const auto g_11 = 1.0 / g11 + SQ(I * Rxy); + const auto g_22 = SQ(B0 * hthe / Bpxy); + const auto g_33 = Rxy * Rxy; + const auto g_12 = Btxy * hthe * I * Rxy / Bpxy; + const auto g_13 = I * Rxy * Rxy; + const auto g_23 = Btxy * hthe * Rxy / Bpxy; coord->setMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23), CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); diff --git a/examples/reconnect-2field/2field.cxx b/examples/reconnect-2field/2field.cxx index 5d4fc1cee0..d270d2af29 100644 --- a/examples/reconnect-2field/2field.cxx +++ b/examples/reconnect-2field/2field.cxx @@ -163,21 +163,21 @@ class TwoField : public PhysicsModel { // CALCULATE METRICS - auto g11 = SQ(Rxy * Bpxy); - auto g22 = 1.0 / SQ(hthe); - auto g33 = SQ(coord->Bxy()) / g11; - auto g12 = 0.0; - auto g13 = 0.; - auto g23 = -Btxy / (hthe * Bpxy * Rxy); + const auto g11 = SQ(Rxy * Bpxy); + const auto g22 = 1.0 / SQ(hthe); + const auto g33 = SQ(coord->Bxy()) / g11; + const auto g12 = 0.0; + const auto g13 = 0.; + const auto g23 = -Btxy / (hthe * Bpxy * Rxy); coord->setJ(hthe / Bpxy); - auto g_11 = 1.0 / g11; - auto g_22 = SQ(coord->Bxy() * hthe / Bpxy); - auto g_33 = Rxy * Rxy; - auto g_12 = 0.; - auto g_13 = 0.; - auto g_23 = Btxy * hthe * Rxy / Bpxy; + const auto g_11 = 1.0 / g11; + const auto g_22 = SQ(coord->Bxy() * hthe / Bpxy); + const auto g_33 = Rxy * Rxy; + const auto g_12 = 0.; + const auto g_13 = 0.; + const auto g_23 = Btxy * hthe * Rxy / Bpxy; coord->setMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23), CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); diff --git a/examples/shear-alfven-wave/2fluid.cxx b/examples/shear-alfven-wave/2fluid.cxx index 98fbff61e2..bec7acc1c4 100644 --- a/examples/shear-alfven-wave/2fluid.cxx +++ b/examples/shear-alfven-wave/2fluid.cxx @@ -182,21 +182,21 @@ class ShearAlfven : public PhysicsModel { /**************** CALCULATE METRICS ******************/ - auto g11 = SQ(Rxy * Bpxy); - auto g22 = 1.0 / SQ(hthe); - auto g33 = SQ(I) * g11 + SQ(coord->Bxy()) / g11; - auto g12 = 0.0; - auto g13 = -I * g11; - auto g23 = -Btxy / (hthe * Bpxy * Rxy); + const auto g11 = SQ(Rxy * Bpxy); + const auto g22 = 1.0 / SQ(hthe); + const auto g33 = SQ(I) * g11 + SQ(coord->Bxy()) / g11; + const auto g12 = 0.0; + const auto g13 = -I * g11; + const auto g23 = -Btxy / (hthe * Bpxy * Rxy); coord->setJ(hthe / Bpxy); - auto g_11 = 1.0 / g11 + SQ(I * Rxy); - auto g_22 = SQ(coord->Bxy() * hthe / Bpxy); - auto g_33 = Rxy * Rxy; - auto g_12 = Btxy * hthe * I * Rxy / Bpxy; - auto g_13 = I * Rxy * Rxy; - auto g_23 = Btxy * hthe * Rxy / Bpxy; + const auto g_11 = 1.0 / g11 + SQ(I * Rxy); + const auto g_22 = SQ(coord->Bxy() * hthe / Bpxy); + const auto g_33 = Rxy * Rxy; + const auto g_12 = Btxy * hthe * I * Rxy / Bpxy; + const auto g_13 = I * Rxy * Rxy; + const auto g_23 = Btxy * hthe * Rxy / Bpxy; coord->setMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23), CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); diff --git a/examples/tokamak-2fluid/2fluid.cxx b/examples/tokamak-2fluid/2fluid.cxx index 6329e0f8ec..a485360304 100644 --- a/examples/tokamak-2fluid/2fluid.cxx +++ b/examples/tokamak-2fluid/2fluid.cxx @@ -399,21 +399,21 @@ class TwoFluid : public PhysicsModel { //////////////////////////////////////////////////////// // CALCULATE METRICS - auto g11 = SQ(Rxy * Bpxy); - auto g22 = 1.0 / SQ(hthe); - auto g33 = SQ(I) * g11 + SQ(coord->Bxy()) / g11; - auto g12 = 0.0; - auto g13 = -I * g11; - auto g23 = -Btxy / (hthe * Bpxy * Rxy); + const auto g11 = SQ(Rxy * Bpxy); + const auto g22 = 1.0 / SQ(hthe); + const auto g33 = SQ(I) * g11 + SQ(coord->Bxy()) / g11; + const auto g12 = 0.0; + const auto g13 = -I * g11; + const auto g23 = -Btxy / (hthe * Bpxy * Rxy); coord->setJ(hthe / Bpxy); - auto g_11 = 1.0 / g11 + SQ(I * Rxy); - auto g_22 = SQ(coord->Bxy() * hthe / Bpxy); - auto g_33 = Rxy * Rxy; - auto g_12 = Btxy * hthe * I * Rxy / Bpxy; - auto g_13 = I * Rxy * Rxy; - auto g_23 = Btxy * hthe * Rxy / Bpxy; + const auto g_11 = 1.0 / g11 + SQ(I * Rxy); + const auto g_22 = SQ(coord->Bxy() * hthe / Bpxy); + const auto g_33 = Rxy * Rxy; + const auto g_12 = Btxy * hthe * I * Rxy / Bpxy; + const auto g_13 = I * Rxy * Rxy; + const auto g_23 = Btxy * hthe * Rxy / Bpxy; coord->setMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23), CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); diff --git a/examples/uedge-benchmark/ue_bmark.cxx b/examples/uedge-benchmark/ue_bmark.cxx index 0d3a3b5b28..1297244bcd 100644 --- a/examples/uedge-benchmark/ue_bmark.cxx +++ b/examples/uedge-benchmark/ue_bmark.cxx @@ -140,21 +140,21 @@ class UedgeBenchmark : public PhysicsModel { /////////////// CALCULATE METRICS ///////////////// - auto g11 = pow(Rxy * Bpxy, 2.0); - auto g22 = 1.0 / pow(hthe, 2.0); - auto g33 = pow(coords->Bxy(), 2.0) / g11; - auto g12 = 0.0; - auto g13 = 0.0; - auto g23 = -Btxy / (hthe * Bpxy * Rxy); + const auto g11 = pow(Rxy * Bpxy, 2.0); + const auto g22 = 1.0 / pow(hthe, 2.0); + const auto g33 = pow(coords->Bxy(), 2.0) / g11; + const auto g12 = 0.0; + const auto g13 = 0.0; + const auto g23 = -Btxy / (hthe * Bpxy * Rxy); coords->setJ(hthe / Bpxy); - auto g_11 = 1.0 / g11; - auto g_22 = pow(coords->Bxy() * hthe / Bpxy, 2.0); - auto g_33 = Rxy * Rxy; - auto g_12 = 0.0; - auto g_13 = 0.0; - auto g_23 = Btxy * hthe * Rxy / Bpxy; + const auto g_11 = 1.0 / g11; + const auto g_22 = pow(coords->Bxy() * hthe / Bpxy, 2.0); + const auto g_33 = Rxy * Rxy; + const auto g_12 = 0.0; + const auto g_13 = 0.0; + const auto g_23 = Btxy * hthe * Rxy / Bpxy; coords->setMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23), CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); diff --git a/examples/wave-slab/wave_slab.cxx b/examples/wave-slab/wave_slab.cxx index 990d8672c9..a0ebad3a31 100644 --- a/examples/wave-slab/wave_slab.cxx +++ b/examples/wave-slab/wave_slab.cxx @@ -31,21 +31,21 @@ class WaveTest : public PhysicsModel { mesh->get(I, "sinty"); } - auto g11 = pow(Rxy * Bpxy, 2.0); - auto g22 = 1.0 / pow(hthe, 2.0); - auto g33 = pow(I, 2.0) * g11 + pow(coords->Bxy(), 2.0) / g11; - auto g12 = 0.0; - auto g13 = -I * g11; - auto g23 = -Btxy / (hthe * Bpxy * Rxy); + const auto g11 = pow(Rxy * Bpxy, 2.0); + const auto g22 = 1.0 / pow(hthe, 2.0); + const auto g33 = pow(I, 2.0) * g11 + pow(coords->Bxy(), 2.0) / g11; + const auto g12 = 0.0; + const auto g13 = -I * g11; + const auto g23 = -Btxy / (hthe * Bpxy * Rxy); coords->setJ(hthe / Bpxy); - auto g_11 = 1.0 / g11 + (pow(I * Rxy, 2.0)); - auto g_22 = pow(coords->Bxy() * hthe / Bpxy, 2.0); - auto g_33 = Rxy * Rxy; - auto g_12 = Btxy * hthe * I * Rxy / Bpxy; - auto g_13 = I * Rxy * Rxy; - auto g_23 = Btxy * hthe * Rxy / Bpxy; + const auto g_11 = 1.0 / g11 + (pow(I * Rxy, 2.0)); + const auto g_22 = pow(coords->Bxy() * hthe / Bpxy, 2.0); + const auto g_33 = Rxy * Rxy; + const auto g_12 = Btxy * hthe * I * Rxy / Bpxy; + const auto g_13 = I * Rxy * Rxy; + const auto g_23 = Btxy * hthe * Rxy / Bpxy; coords->setMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23), CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); diff --git a/tests/integrated/test-drift-instability/2fluid.cxx b/tests/integrated/test-drift-instability/2fluid.cxx index f2a881230c..b1e24d3537 100644 --- a/tests/integrated/test-drift-instability/2fluid.cxx +++ b/tests/integrated/test-drift-instability/2fluid.cxx @@ -217,21 +217,21 @@ class TwoFluid : public PhysicsModel { /**************** CALCULATE METRICS ******************/ - auto g11 = SQ(Rxy * Bpxy); - auto g22 = 1.0 / SQ(hthe); - auto g33 = SQ(I) * g11 + SQ(coord->Bxy()) / g11; - auto g12 = 0.0; - auto g13 = -I * g11; - auto g23 = -Btxy / (hthe * Bpxy * Rxy); + const auto g11 = SQ(Rxy * Bpxy); + const auto g22 = 1.0 / SQ(hthe); + const auto g33 = SQ(I) * g11 + SQ(coord->Bxy()) / g11; + const auto g12 = 0.0; + const auto g13 = -I * g11; + const auto g23 = -Btxy / (hthe * Bpxy * Rxy); coord->setJ(hthe / Bpxy); - auto g_11 = 1.0 / g11 + SQ(I * Rxy); - auto g_22 = SQ(coord->Bxy() * hthe / Bpxy); - auto g_33 = Rxy * Rxy; - auto g_12 = Btxy * hthe * I * Rxy / Bpxy; - auto g_13 = I * Rxy * Rxy; - auto g_23 = Btxy * hthe * Rxy / Bpxy; + const auto g_11 = 1.0 / g11 + SQ(I * Rxy); + const auto g_22 = SQ(coord->Bxy() * hthe / Bpxy); + const auto g_33 = Rxy * Rxy; + const auto g_12 = Btxy * hthe * I * Rxy / Bpxy; + const auto g_13 = I * Rxy * Rxy; + const auto g_23 = Btxy * hthe * Rxy / Bpxy; coord->setMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23), CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); diff --git a/tests/integrated/test-interchange-instability/2fluid.cxx b/tests/integrated/test-interchange-instability/2fluid.cxx index 8332eba4be..0afde399ca 100644 --- a/tests/integrated/test-interchange-instability/2fluid.cxx +++ b/tests/integrated/test-interchange-instability/2fluid.cxx @@ -139,21 +139,21 @@ class Interchange : public PhysicsModel { /**************** CALCULATE METRICS ******************/ - auto g11 = SQ(Rxy * Bpxy); - auto g22 = 1.0 / SQ(hthe); - auto g33 = SQ(I) * g11 + SQ(coord->Bxy()) / g11; - auto g12 = 0.0; - auto g13 = -I * g11; - auto g23 = -Btxy / (hthe * Bpxy * Rxy); + const auto g11 = SQ(Rxy * Bpxy); + const auto g22 = 1.0 / SQ(hthe); + const auto g33 = SQ(I) * g11 + SQ(coord->Bxy()) / g11; + const auto g12 = 0.0; + const auto g13 = -I * g11; + const auto g23 = -Btxy / (hthe * Bpxy * Rxy); coord->setJ(hthe / Bpxy); - auto g_11 = 1.0 / g11 + SQ(I * Rxy); - auto g_22 = SQ(coord->Bxy() * hthe / Bpxy); - auto g_33 = Rxy * Rxy; - auto g_12 = Btxy * hthe * I * Rxy / Bpxy; - auto g_13 = I * Rxy * Rxy; - auto g_23 = Btxy * hthe * Rxy / Bpxy; + const auto g_11 = 1.0 / g11 + SQ(I * Rxy); + const auto g_22 = SQ(coord->Bxy() * hthe / Bpxy); + const auto g_33 = Rxy * Rxy; + const auto g_12 = Btxy * hthe * I * Rxy / Bpxy; + const auto g_13 = I * Rxy * Rxy; + const auto g_23 = Btxy * hthe * Rxy / Bpxy; coord->setMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23), CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); From 1ad60a6a0f41f7bd91b55cbbf280c75b3b1e4d45 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Tue, 4 Jun 2024 13:53:01 +0100 Subject: [PATCH 430/491] Fix copy-paste errors introduced during refactoring (more). --- src/field/vector2d.cxx | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/field/vector2d.cxx b/src/field/vector2d.cxx index bcffbcea8b..0fd17d6e9d 100644 --- a/src/field/vector2d.cxx +++ b/src/field/vector2d.cxx @@ -124,8 +124,8 @@ void Vector2D::toContravariant() { if (location == CELL_VSHIFT) { Coordinates* metric_x; - [[maybe_unused]] Coordinates* metric_y; - [[maybe_unused]] Coordinates* metric_z; + Coordinates* metric_y; + Coordinates* metric_z; metric_x = localmesh->getCoordinates(CELL_XLOW); metric_y = localmesh->getCoordinates(CELL_YLOW); @@ -147,10 +147,10 @@ void Vector2D::toContravariant() { BOUT_FOR(i, x.getRegion("RGN_ALL")) { x[i] = metric_x->g11()[i] * x[i] + metric_x->g12()[i] * y_at_x[i] + metric_x->g13()[i] * z_at_x[i]; - y[i] = metric_x->g22()[i] * y[i] + metric_x->g12()[i] * x_at_y[i] - + metric_x->g23()[i] * z_at_y[i]; - z[i] = metric_x->g33()[i] * z[i] + metric_x->g13()[i] * x_at_z[i] - + metric_x->g23()[i] * y_at_z[i]; + y[i] = metric_y->g22()[i] * y[i] + metric_y->g12()[i] * x_at_y[i] + + metric_y->g23()[i] * z_at_y[i]; + z[i] = metric_z->g33()[i] * z[i] + metric_z->g13()[i] * x_at_z[i] + + metric_z->g23()[i] * y_at_z[i]; }; } else { From de8abf36082b23a2043e0483f325622ddc8b2c7c Mon Sep 17 00:00:00 2001 From: tomc271 Date: Tue, 4 Jun 2024 13:59:00 +0100 Subject: [PATCH 431/491] Define local variables on assignment (and using auto). Other clang-tidy suggestions. --- src/field/vector2d.cxx | 31 +++++++++++++++---------------- src/field/vector3d.cxx | 15 +++++++-------- 2 files changed, 22 insertions(+), 24 deletions(-) diff --git a/src/field/vector2d.cxx b/src/field/vector2d.cxx index 0fd17d6e9d..9f3fa19d8c 100644 --- a/src/field/vector2d.cxx +++ b/src/field/vector2d.cxx @@ -65,12 +65,10 @@ void Vector2D::toCovariant() { Mesh* localmesh = getMesh(); if (location == CELL_VSHIFT) { - Coordinates* metric_x; - Coordinates* metric_y; - Coordinates* metric_z; - metric_x = localmesh->getCoordinates(CELL_XLOW); - metric_y = localmesh->getCoordinates(CELL_YLOW); - metric_z = localmesh->getCoordinates(CELL_ZLOW); + + const auto* metric_x = localmesh->getCoordinates(CELL_XLOW); + const auto* metric_y = localmesh->getCoordinates(CELL_YLOW); + const auto* metric_z = localmesh->getCoordinates(CELL_ZLOW); // Fields at different locations so we need to interpolate // Note : Could reduce peak memory requirement here by just @@ -94,10 +92,12 @@ void Vector2D::toCovariant() { + metric_z->g_23()[i] * y_at_z[i]; }; } else { - const auto metric = localmesh->getCoordinates(location); + const auto* metric = localmesh->getCoordinates(location); // Need to use temporary arrays to store result - Coordinates::FieldMetric gx{emptyFrom(x)}, gy{emptyFrom(y)}, gz{emptyFrom(z)}; + Coordinates::FieldMetric gx{emptyFrom(x)}; + Coordinates::FieldMetric gy{emptyFrom(y)}; + Coordinates::FieldMetric gz{emptyFrom(z)}; BOUT_FOR(i, x.getRegion("RGN_ALL")) { gx[i] = metric->g_11()[i] * x[i] + metric->g_12()[i] * y[i] @@ -123,13 +123,10 @@ void Vector2D::toContravariant() { Mesh* localmesh = getMesh(); if (location == CELL_VSHIFT) { - Coordinates* metric_x; - Coordinates* metric_y; - Coordinates* metric_z; - metric_x = localmesh->getCoordinates(CELL_XLOW); - metric_y = localmesh->getCoordinates(CELL_YLOW); - metric_z = localmesh->getCoordinates(CELL_ZLOW); + const auto* metric_x = localmesh->getCoordinates(CELL_XLOW); + const auto* metric_y = localmesh->getCoordinates(CELL_YLOW); + const auto* metric_z = localmesh->getCoordinates(CELL_ZLOW); // Fields at different locations so we need to interpolate // Note : Could reduce peak memory requirement here by just @@ -154,10 +151,12 @@ void Vector2D::toContravariant() { }; } else { - const auto metric = localmesh->getCoordinates(location); + const auto* metric = localmesh->getCoordinates(location); // Need to use temporary arrays to store result - Coordinates::FieldMetric gx{emptyFrom(x)}, gy{emptyFrom(y)}, gz{emptyFrom(z)}; + Coordinates::FieldMetric gx{emptyFrom(x)}; + Coordinates::FieldMetric gy{emptyFrom(y)}; + Coordinates::FieldMetric gz{emptyFrom(z)}; BOUT_FOR(i, x.getRegion("RGN_ALL")) { gx[i] = diff --git a/src/field/vector3d.cxx b/src/field/vector3d.cxx index 273c4c791f..a1a181854d 100644 --- a/src/field/vector3d.cxx +++ b/src/field/vector3d.cxx @@ -122,13 +122,10 @@ void Vector3D::toContravariant() { Mesh* localmesh = getMesh(); if (location == CELL_VSHIFT) { - Coordinates* metric_x; - Coordinates* metric_y; - Coordinates* metric_z; - metric_x = localmesh->getCoordinates(CELL_XLOW); - metric_y = localmesh->getCoordinates(CELL_YLOW); - metric_z = localmesh->getCoordinates(CELL_ZLOW); + const auto* metric_x = localmesh->getCoordinates(CELL_XLOW); + const auto* metric_y = localmesh->getCoordinates(CELL_YLOW); + const auto* metric_z = localmesh->getCoordinates(CELL_ZLOW); // Fields at different locations so we need to interpolate // Note : Could reduce peak memory requirement here by just @@ -153,10 +150,12 @@ void Vector3D::toContravariant() { }; } else { - const auto metric = localmesh->getCoordinates(location); + const auto* metric = localmesh->getCoordinates(location); // Need to use temporary arrays to store result - Field3D gx{emptyFrom(x)}, gy{emptyFrom(y)}, gz{emptyFrom(z)}; + Field3D gx{emptyFrom(x)}; + Field3D gy{emptyFrom(y)}; + Field3D gz{emptyFrom(z)}; BOUT_FOR(i, localmesh->getRegion3D("RGN_ALL")) { gx[i] = From 39285626999d2222f30c778566bb30f52bb9fbf3 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Tue, 4 Jun 2024 14:02:20 +0100 Subject: [PATCH 432/491] Revert "Clang-Tidy: Constructors that are callable with a single argument must be marked explicit to avoid unintentional implicit conversions." This reverts commit befabd5f73737432800d7d0d460b76f7c32762f9. --- include/bout/boundary_region.hxx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/bout/boundary_region.hxx b/include/bout/boundary_region.hxx index 0c55df5cf1..da0f5f0b38 100644 --- a/include/bout/boundary_region.hxx +++ b/include/bout/boundary_region.hxx @@ -39,7 +39,7 @@ constexpr BndryLoc BNDRY_PAR_BKWD_XOUT = BndryLoc::par_bkwd_xout; class BoundaryRegionBase { public: BoundaryRegionBase() = delete; - explicit BoundaryRegionBase(std::string name, Mesh* passmesh = nullptr) + BoundaryRegionBase(std::string name, Mesh* passmesh = nullptr) : localmesh(passmesh ? passmesh : bout::globals::mesh), label(std::move(name)) {} BoundaryRegionBase(std::string name, BndryLoc loc, Mesh* passmesh = nullptr) : localmesh(passmesh ? passmesh : bout::globals::mesh), label(std::move(name)), From 3f4abe0061828d7098ce91f22463842e1bc2c55d Mon Sep 17 00:00:00 2001 From: tomc271 Date: Tue, 4 Jun 2024 14:02:43 +0100 Subject: [PATCH 433/491] Revert "Use std::move() for constructor parameters." This reverts commit ac9cad6d105786761334a44c253919f00b7e8e5f. --- include/bout/boundary_region.hxx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/bout/boundary_region.hxx b/include/bout/boundary_region.hxx index da0f5f0b38..58de12045e 100644 --- a/include/bout/boundary_region.hxx +++ b/include/bout/boundary_region.hxx @@ -67,9 +67,9 @@ class BoundaryRegion : public BoundaryRegionBase { public: BoundaryRegion() = delete; BoundaryRegion(std::string name, BndryLoc loc, Mesh* passmesh = nullptr) - : BoundaryRegionBase(std::move(name), loc, passmesh) {} + : BoundaryRegionBase(name, loc, passmesh) {} BoundaryRegion(std::string name, int xd, int yd, Mesh* passmesh = nullptr) - : BoundaryRegionBase(std::move(name), passmesh), bx(xd), by(yd), width(2) {} + : BoundaryRegionBase(name, passmesh), bx(xd), by(yd), width(2) {} ~BoundaryRegion() override = default; int x, y; ///< Indices of the point in the boundary From 49deb09733c128a2f6bc9ad43d1a348832c45d8e Mon Sep 17 00:00:00 2001 From: tomc271 Date: Tue, 4 Jun 2024 14:03:42 +0100 Subject: [PATCH 434/491] Revert "Another unused #include directive." This reverts commit b2a3dadc623ae5e8da47a2bb868a18c8bbf07dc9. --- src/mesh/boundary_region.cxx | 1 + 1 file changed, 1 insertion(+) diff --git a/src/mesh/boundary_region.cxx b/src/mesh/boundary_region.cxx index 02a5310149..700ef8a91f 100644 --- a/src/mesh/boundary_region.cxx +++ b/src/mesh/boundary_region.cxx @@ -1,5 +1,6 @@ #include +#include #include #include From bf5e9e9b61a7ebb7bbfd33128cb9dcdd9592b974 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Tue, 4 Jun 2024 14:05:45 +0100 Subject: [PATCH 435/491] Revert "Clang-Tidy: Single-argument constructors must be marked explicit to avoid unintentional implicit conversions." This reverts commit 9adbfda5e3cd43c4271278e7b74606541539ecdc. --- include/bout/fieldgroup.hxx | 8 ++++---- src/mesh/impls/bout/boutmesh.hxx | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/include/bout/fieldgroup.hxx b/include/bout/fieldgroup.hxx index 32bc75e557..184766c6b8 100644 --- a/include/bout/fieldgroup.hxx +++ b/include/bout/fieldgroup.hxx @@ -26,10 +26,10 @@ public: FieldGroup& operator=(FieldGroup&& other) = default; /// Constructor with a single FieldData \p f - explicit FieldGroup(FieldData& f) { fvec.push_back(&f); } + FieldGroup(FieldData& f) { fvec.push_back(&f); } /// Constructor with a single Field3D \p f - explicit FieldGroup(Field3D& f) { + FieldGroup(Field3D& f) { fvec.push_back(&f); f3vec.push_back(&f); } @@ -37,7 +37,7 @@ public: /// Constructor with a single Vector2D \p v /// /// This is needed so that fvec only contains Field2D or Field3D - explicit FieldGroup(Vector2D& v) { + FieldGroup(Vector2D& v) { fvec.push_back(&v.x); fvec.push_back(&v.y); fvec.push_back(&v.z); @@ -46,7 +46,7 @@ public: /// Constructor with a single Vector3D \p v /// /// This is needed so that fvec only contains Field2D or Field3D - explicit FieldGroup(Vector3D& v) { + FieldGroup(Vector3D& v) { fvec.push_back(&v.x); fvec.push_back(&v.y); fvec.push_back(&v.z); diff --git a/src/mesh/impls/bout/boutmesh.hxx b/src/mesh/impls/bout/boutmesh.hxx index 546ae9e255..cc674d401a 100644 --- a/src/mesh/impls/bout/boutmesh.hxx +++ b/src/mesh/impls/bout/boutmesh.hxx @@ -19,7 +19,7 @@ /// conventions. class BoutMesh : public Mesh { public: - explicit BoutMesh(GridDataSource* s, Options* options = nullptr); + BoutMesh(GridDataSource* s, Options* options = nullptr); ~BoutMesh() override; /// Read in the mesh from data sources From ec6b7d3c7352e285fd3f47a29b149b6556fb1183 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Tue, 4 Jun 2024 14:10:04 +0100 Subject: [PATCH 436/491] Revert "Remove redundant semicolons." This reverts commit 40de3eef62122d25ef2ec63138dbc2ba3a549760. --- src/mesh/coordinates.cxx | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 0e925e6384..cfb39a08fd 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -135,8 +135,8 @@ Field2D Coordinates::interpolateAndExtrapolate( // this point should already be set correctly because the metric // components have been interpolated to here. if (extrap_start > 0 and f.getLocation() != location) { - ASSERT1(bndry->bx == 0 or localmesh->xstart > 1) - ASSERT1(bndry->by == 0 or localmesh->ystart > 1) + ASSERT1(bndry->bx == 0 or localmesh->xstart > 1); + ASSERT1(bndry->by == 0 or localmesh->ystart > 1); // note that either bx or by is >0 here result(bndry->x, bndry->y) = (9. @@ -1052,7 +1052,7 @@ Field3D Coordinates::DDX(const Field3D& f, CELL_LOC outloc, const std::string& m } return result; -} +}; Coordinates::FieldMetric Coordinates::DDY(const Field2D& f, CELL_LOC loc, const std::string& method, @@ -1341,7 +1341,7 @@ FieldPerp Coordinates::Delp2(const FieldPerp& f, CELL_LOC outloc, bool useFFT) { // yet // result = G1 * ::DDX(f, outloc) + G3 * ::DDZ(f, outloc) + g11 * ::D2DX2(f, outloc) // + g33 * ::D2DZ2(f, outloc) + 2 * g13 * ::D2DXDZ(f, outloc); - } + }; return result; } From 68fa3a4fc7eab3f314d14d0738b7136e8213cf8b Mon Sep 17 00:00:00 2001 From: tomc271 Date: Tue, 4 Jun 2024 14:14:39 +0100 Subject: [PATCH 437/491] Revert "More [[maybe_unused]] attributes." This reverts commit e37d6fe210c9e036f45ce7fc201391a7b0e7bbea. --- include/bout/optionsreader.hxx | 4 ++-- src/sys/optionsreader.cxx | 3 +-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/include/bout/optionsreader.hxx b/include/bout/optionsreader.hxx index 9069b5d2bf..41d068a9e2 100644 --- a/include/bout/optionsreader.hxx +++ b/include/bout/optionsreader.hxx @@ -100,8 +100,8 @@ public: /// ... /// return 0; /// } - static void parseCommandLine(Options* options, int argc, char** argv); - static void parseCommandLine(Options* options, const std::vector& argv); + void parseCommandLine(Options* options, int argc, char** argv); + void parseCommandLine(Options* options, const std::vector& argv); private: /// The instance of this singleton diff --git a/src/sys/optionsreader.cxx b/src/sys/optionsreader.cxx index fcbbddb0db..17e3bdbf54 100644 --- a/src/sys/optionsreader.cxx +++ b/src/sys/optionsreader.cxx @@ -43,8 +43,7 @@ void OptionsReader::write(Options* options, const std::string& filename) { OptionINI{}.write(options, filename); } -[[maybe_unused]] void OptionsReader::parseCommandLine(Options* options, int argc, - char** argv) { +void OptionsReader::parseCommandLine(Options* options, int argc, char** argv) { return parseCommandLine(options, std::vector(argv, argv + argc)); } From e3cb84e88e6fadbd846746f64f9e72167dfbdde8 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Tue, 4 Jun 2024 14:15:57 +0100 Subject: [PATCH 438/491] Revert "More unused #include directives." This reverts commit b719ad10fc93516897b876a03d601644eaea261c. --- src/sys/derivs.cxx | 5 +++++ src/sys/msg_stack.cxx | 1 + src/sys/options.cxx | 3 +++ src/sys/optionsreader.cxx | 1 + src/sys/output.cxx | 3 +++ 5 files changed, 13 insertions(+) diff --git a/src/sys/derivs.cxx b/src/sys/derivs.cxx index 9169781288..b48a7b0f49 100644 --- a/src/sys/derivs.cxx +++ b/src/sys/derivs.cxx @@ -41,8 +41,13 @@ #include #include #include +#include #include #include +#include +#include + +#include #include #include diff --git a/src/sys/msg_stack.cxx b/src/sys/msg_stack.cxx index 425f7e06ae..502836324c 100644 --- a/src/sys/msg_stack.cxx +++ b/src/sys/msg_stack.cxx @@ -27,6 +27,7 @@ #include "bout/openmpwrap.hxx" #include #include +#include #include #if BOUT_USE_OPENMP diff --git a/src/sys/options.cxx b/src/sys/options.cxx index b49d57c80b..1d18a0e462 100644 --- a/src/sys/options.cxx +++ b/src/sys/options.cxx @@ -10,7 +10,9 @@ #include "bout/output.hxx" #include "bout/sys/expressionparser.hxx" #include "bout/sys/gettext.hxx" +#include "bout/sys/type_name.hxx" #include "bout/sys/variant.hxx" +#include "bout/traits.hxx" #include "bout/unused.hxx" #include "bout/utils.hxx" @@ -19,6 +21,7 @@ #include #include +#include #include #include #include diff --git a/src/sys/optionsreader.cxx b/src/sys/optionsreader.cxx index 17e3bdbf54..3d60a6aa59 100644 --- a/src/sys/optionsreader.cxx +++ b/src/sys/optionsreader.cxx @@ -1,3 +1,4 @@ +#include #include #include #include diff --git a/src/sys/output.cxx b/src/sys/output.cxx index 5ce3852f65..c07cd53474 100644 --- a/src/sys/output.cxx +++ b/src/sys/output.cxx @@ -24,7 +24,10 @@ **************************************************************************/ #include +#include +#include #include +#include void Output::enable() { add(std::cout); From 3d293c4d589ae291c1b4fe7fb142dbacb0131f85 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Tue, 4 Jun 2024 14:16:48 +0100 Subject: [PATCH 439/491] Revert "Clang-Tidy: The 'empty' method should be used to check for emptiness instead of 'length'." This reverts commit 271825057a28ce430d35c0558486b90664a2ee43. --- src/sys/optionsreader.cxx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/sys/optionsreader.cxx b/src/sys/optionsreader.cxx index 3d60a6aa59..8624f646df 100644 --- a/src/sys/optionsreader.cxx +++ b/src/sys/optionsreader.cxx @@ -65,13 +65,13 @@ void OptionsReader::parseCommandLine(Options* options, options = Options::getRoot(); buffer = argv[i]; - if (buffer.empty()) { + if (buffer.length() == 0) { continue; } // Test if name starts with a '-', and remove if found if (buffer[0] == '-') { buffer = buffer.substr(1); // Remove the first character (-) - if (buffer.empty()) { + if (buffer.length() == 0) { throw BoutException( _("Invalid command line option '-' found - maybe check whitespace?")); } From 64ed06e8b7c121f3483dda4f9b71a565108544be Mon Sep 17 00:00:00 2001 From: tomc271 Date: Thu, 6 Jun 2024 07:55:09 +0000 Subject: [PATCH 440/491] Apply clang-format changes --- include/bout/christoffel_symbols.hxx | 1 - include/bout/coordinates.hxx | 23 +++--- src/mesh/christoffel_symbols.cxx | 10 +-- src/mesh/coordinates.cxx | 33 ++++----- src/mesh/g_values.cxx | 4 +- src/mesh/impls/bout/boutmesh.cxx | 4 +- src/mesh/metric_tensor.cxx | 4 +- tests/unit/mesh/test_coordinates.cxx | 104 +++++++++++++-------------- 8 files changed, 89 insertions(+), 94 deletions(-) diff --git a/include/bout/christoffel_symbols.hxx b/include/bout/christoffel_symbols.hxx index 7602bb4b20..a4db64d1ce 100644 --- a/include/bout/christoffel_symbols.hxx +++ b/include/bout/christoffel_symbols.hxx @@ -86,7 +86,6 @@ public: void communicate(Mesh* mesh); - private: FieldMetric G1_11_, G1_22_, G1_33_, G1_12_, G1_13_, G1_23_; FieldMetric G2_11_, G2_22_, G2_33_, G2_12_, G2_13_, G2_23_; diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index 8aa7554a15..743a0258ba 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -141,7 +141,7 @@ public: bool force_interpolate_from_centre = false); void setMetricTensor(const ContravariantMetricTensor& contravariant_metric_tensor, - const CovariantMetricTensor& covariant_metric_tensor); + const CovariantMetricTensor& covariant_metric_tensor); ///< Coordinate system Jacobian, so volume of cell is J*dx*dy*dz FieldMetric& J() const; @@ -309,15 +309,14 @@ public: bool force_interpolate_from_centre); FieldMetric recalculateJacobian() const; - - static void communicate(Field2D& f) ; + + static void communicate(Field2D& f); #if BOUT_USE_METRIC_3D // In this case we also need to be able to call with a Field3D - static void communicate(Field3D& f) ; + static void communicate(Field3D& f); #endif - private: int nz; // Size of mesh in Z. This is mesh->ngz-1 Mesh* localmesh; @@ -407,16 +406,16 @@ private: void fixZShiftGuards(Field2D& zShift) const; static Field2D interpolateAndExtrapolate(const Field2D& f, CELL_LOC location, - bool extrapolate_x, bool extrapolate_y, - bool no_extra_interpolate, - ParallelTransform* UNUSED_pt, - const std::string& region) ; + bool extrapolate_x, bool extrapolate_y, + bool no_extra_interpolate, + ParallelTransform* UNUSED_pt, + const std::string& region); #if BOUT_USE_METRIC_3D static Field3D interpolateAndExtrapolate(const Field3D& f_, CELL_LOC location, - bool extrapolate_x, bool extrapolate_y, - bool no_extra_interpolate, - ParallelTransform* pt_) ; + bool extrapolate_x, bool extrapolate_y, + bool no_extra_interpolate, + ParallelTransform* pt_); #endif // BOUT_USE_METRIC_3D }; diff --git a/src/mesh/christoffel_symbols.cxx b/src/mesh/christoffel_symbols.cxx index 5789ade8ec..5e21af8c87 100644 --- a/src/mesh/christoffel_symbols.cxx +++ b/src/mesh/christoffel_symbols.cxx @@ -1,8 +1,8 @@ #include "bout/christoffel_symbols.hxx" #include "bout/coordinates.hxx" -#include #include "bout/mesh.hxx" +#include ChristoffelSymbols::ChristoffelSymbols( FieldMetric G1_11, FieldMetric G1_22, FieldMetric G1_33, FieldMetric G1_12, @@ -142,9 +142,9 @@ void ChristoffelSymbols::applyToComponents( void ChristoffelSymbols::communicate(Mesh* mesh) { - output_progress.write("\tCommunicating connection terms\n"); + output_progress.write("\tCommunicating connection terms\n"); - mesh->communicate(G1_11_, G1_22_, G1_33_, G1_12_, G1_13_, G1_23_, - G2_11_, G2_22_, G2_33_, G2_12_, G2_13_, G2_23_, - G3_11_, G3_22_, G3_33_, G3_12_, G3_13_, G3_23_); + mesh->communicate(G1_11_, G1_22_, G1_33_, G1_12_, G1_13_, G1_23_, G2_11_, G2_22_, + G2_33_, G2_12_, G2_13_, G2_23_, G3_11_, G3_22_, G3_33_, G3_12_, + G3_13_, G3_23_); } diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index cfb39a08fd..6e7d010a7f 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -65,23 +65,22 @@ std::string getLocationSuffix(CELL_LOC location) { } // anonymous namespace - // Use sendY()/sendX() and wait() instead of Mesh::communicate() to ensure we // don't try to calculate parallel slices as Coordinates are not constructed yet void Coordinates::communicate(Field2D& f) { - FieldGroup g(f); - auto* h = f.getMesh()->sendY(g); - f.getMesh()->wait(h); - h = f.getMesh()->sendX(g); - f.getMesh()->wait(h); + FieldGroup g(f); + auto* h = f.getMesh()->sendY(g); + f.getMesh()->wait(h); + h = f.getMesh()->sendX(g); + f.getMesh()->wait(h); } #if BOUT_USE_METRIC_3D void Coordinates::communicate(Field3D& f) { - FieldGroup g(f); - auto* h = f.getMesh()->sendY(g); - f.getMesh()->wait(h); - h = f.getMesh()->sendX(g); - f.getMesh()->wait(h); + FieldGroup g(f); + auto* h = f.getMesh()->sendY(g); + f.getMesh()->wait(h); + h = f.getMesh()->sendX(g); + f.getMesh()->wait(h); } #endif @@ -485,7 +484,8 @@ void Coordinates::interpolateFieldsFromOtherCoordinates(Options* mesh_options, setParallelTransform(mesh_options); - setMetricTensor(coords_in->getContravariantMetricTensor(), coords_in->getCovariantMetricTensor()); + setMetricTensor(coords_in->getContravariantMetricTensor(), + coords_in->getCovariantMetricTensor()); std::function const interpolateAndExtrapolate_function = [this](const FieldMetric& component) { @@ -1561,10 +1561,11 @@ void Coordinates::setCovariantMetricTensor(const CovariantMetricTensor& metric_t recalculateAndReset(recalculate_staggered, force_interpolate_from_centre); } -void Coordinates::setMetricTensor(const ContravariantMetricTensor& contravariant_metric_tensor, - const CovariantMetricTensor& covariant_metric_tensor) { - contravariantMetricTensor.setMetricTensor(contravariant_metric_tensor); - covariantMetricTensor.setMetricTensor(covariant_metric_tensor); +void Coordinates::setMetricTensor( + const ContravariantMetricTensor& contravariant_metric_tensor, + const CovariantMetricTensor& covariant_metric_tensor) { + contravariantMetricTensor.setMetricTensor(contravariant_metric_tensor); + covariantMetricTensor.setMetricTensor(covariant_metric_tensor); } void Coordinates::applyToContravariantMetricTensor( diff --git a/src/mesh/g_values.cxx b/src/mesh/g_values.cxx index f14013183b..70760ee394 100644 --- a/src/mesh/g_values.cxx +++ b/src/mesh/g_values.cxx @@ -29,6 +29,4 @@ GValues::GValues(const Coordinates& coordinates) { setG3((coordinates.DDX(J * g13) + coordinates.DDY(tmp) + coordinates.DDZ(J * g33)) / J); } -void GValues::communicate(Mesh* mesh) { - mesh->communicate(G1_, G2_, G3_); -} +void GValues::communicate(Mesh* mesh) { mesh->communicate(G1_, G2_, G3_); } diff --git a/src/mesh/impls/bout/boutmesh.cxx b/src/mesh/impls/bout/boutmesh.cxx index bd6b35b9ad..a28511422a 100644 --- a/src/mesh/impls/bout/boutmesh.cxx +++ b/src/mesh/impls/bout/boutmesh.cxx @@ -1701,9 +1701,7 @@ int BoutMesh::getLocalYIndexNoBoundaries(int yglobal) const { return yglobal - PE_YIND * MYSUB + MYG; } -int BoutMesh::YGLOBAL(int yloc, int yproc) const { - return yloc + yproc * MYSUB - MYG; -} +int BoutMesh::YGLOBAL(int yloc, int yproc) const { return yloc + yproc * MYSUB - MYG; } int BoutMesh::YLOCAL(int yglo, int yproc) const { return yglo - yproc * MYSUB + MYG; } diff --git a/src/mesh/metric_tensor.cxx b/src/mesh/metric_tensor.cxx index 5bfab6cec1..aa578f5486 100644 --- a/src/mesh/metric_tensor.cxx +++ b/src/mesh/metric_tensor.cxx @@ -1,8 +1,8 @@ #include "bout/metric_tensor.hxx" +#include "bout/mesh.hxx" #include "bout/output.hxx" #include -#include "bout/mesh.hxx" MetricTensor::MetricTensor(FieldMetric g11, FieldMetric g22, FieldMetric g33, FieldMetric g12, FieldMetric g13, FieldMetric g23) @@ -149,5 +149,5 @@ MetricTensor MetricTensor::applyToComponents( } void MetricTensor::communicate(Mesh* mesh) { - mesh->communicate(g11_, g22_, g33_, g12_, g13_, g23_); + mesh->communicate(g11_, g22_, g33_, g12_, g13_, g23_); } diff --git a/tests/unit/mesh/test_coordinates.cxx b/tests/unit/mesh/test_coordinates.cxx index a84d790d31..80739555be 100644 --- a/tests/unit/mesh/test_coordinates.cxx +++ b/tests/unit/mesh/test_coordinates.cxx @@ -364,8 +364,8 @@ TEST_F(CoordinatesTest, SetContravariantMetricTensor) { TEST_F(CoordinatesTest, CheckCovariantCalculatedFromContravariant) { - // Set initial values for the metric tensor in the Coordinates constructor - Coordinates coords{mesh, + // Set initial values for the metric tensor in the Coordinates constructor + Coordinates coords{mesh, FieldMetric{1.0}, // dx FieldMetric{1.0}, // dy FieldMetric{1.0}, // dz @@ -386,36 +386,36 @@ TEST_F(CoordinatesTest, CheckCovariantCalculatedFromContravariant) { FieldMetric{0.0}, // ShiftTorsion FieldMetric{0.0}}; // IntShiftTorsion - // Modify contravariant components - constexpr double g11 = 1.0; - constexpr double g22 = 1.0; - constexpr double g33 = 1.0; - const double g12 = sqrt(3.0)/4.0; // (std::sqrt only constexpr since C++26) - constexpr double g13 = 0.5; - const double g23 = sqrt(3.0)/4.0; // (std::sqrt only constexpr since C++26) - auto updated_metric_tensor = ContravariantMetricTensor(g11, g22, g33, g12, g13, g23); - coords.setContravariantMetricTensor(updated_metric_tensor); - - // Check that the covariant components have been calculated corrected - constexpr double expected_g_11 = 13.0 / 9.0; - constexpr double expected_g_22 = 4.0 / 3.0; - constexpr double expected_g_33 = 13.0 / 9.0; - const double expected_g_12 = -2.0 * sqrt(3.0) / 9.0; - constexpr double expected_g_13 = -5.0 / 9.0; - const double expected_g_23 = -2.0 * sqrt(3.0) / 9.0; - - EXPECT_TRUE(IsFieldEqual(coords.g_11(), expected_g_11)); - EXPECT_TRUE(IsFieldEqual(coords.g_22(), expected_g_22)); - EXPECT_TRUE(IsFieldEqual(coords.g_33(), expected_g_33)); - EXPECT_TRUE(IsFieldEqual(coords.g_12(), expected_g_12)); - EXPECT_TRUE(IsFieldEqual(coords.g_13(), expected_g_13)); - EXPECT_TRUE(IsFieldEqual(coords.g_23(), expected_g_23)); + // Modify contravariant components + constexpr double g11 = 1.0; + constexpr double g22 = 1.0; + constexpr double g33 = 1.0; + const double g12 = sqrt(3.0) / 4.0; // (std::sqrt only constexpr since C++26) + constexpr double g13 = 0.5; + const double g23 = sqrt(3.0) / 4.0; // (std::sqrt only constexpr since C++26) + auto updated_metric_tensor = ContravariantMetricTensor(g11, g22, g33, g12, g13, g23); + coords.setContravariantMetricTensor(updated_metric_tensor); + + // Check that the covariant components have been calculated corrected + constexpr double expected_g_11 = 13.0 / 9.0; + constexpr double expected_g_22 = 4.0 / 3.0; + constexpr double expected_g_33 = 13.0 / 9.0; + const double expected_g_12 = -2.0 * sqrt(3.0) / 9.0; + constexpr double expected_g_13 = -5.0 / 9.0; + const double expected_g_23 = -2.0 * sqrt(3.0) / 9.0; + + EXPECT_TRUE(IsFieldEqual(coords.g_11(), expected_g_11)); + EXPECT_TRUE(IsFieldEqual(coords.g_22(), expected_g_22)); + EXPECT_TRUE(IsFieldEqual(coords.g_33(), expected_g_33)); + EXPECT_TRUE(IsFieldEqual(coords.g_12(), expected_g_12)); + EXPECT_TRUE(IsFieldEqual(coords.g_13(), expected_g_13)); + EXPECT_TRUE(IsFieldEqual(coords.g_23(), expected_g_23)); } TEST_F(CoordinatesTest, CheckContravariantCalculatedFromCovariant) { - // Set initial values for the metric tensor in the Coordinates constructor - Coordinates coords{mesh, + // Set initial values for the metric tensor in the Coordinates constructor + Coordinates coords{mesh, FieldMetric{1.0}, // dx FieldMetric{1.0}, // dy FieldMetric{1.0}, // dz @@ -436,30 +436,30 @@ TEST_F(CoordinatesTest, CheckContravariantCalculatedFromCovariant) { FieldMetric{0.0}, // ShiftTorsion FieldMetric{0.0}}; // IntShiftTorsion - // Modify covariant components - constexpr double g_11 = 1.0; - constexpr double g_22 = 1.0; - constexpr double g_33 = 1.0; - const double g_12 = sqrt(3.0)/4.0; // (std::sqrt only constexpr since C++26) - constexpr double g_13 = 0.5; - const double g_23 = sqrt(3.0)/4.0; // (std::sqrt only constexpr since C++26) - auto updated_metric_tensor = CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23); - coords.setCovariantMetricTensor(updated_metric_tensor); - - // Check that the contravariant components have been calculated corrected - constexpr double expected_g11 = 13.0 / 9.0; - constexpr double expected_g22 = 4.0 / 3.0; - constexpr double expected_g33 = 13.0 / 9.0; - const double expected_g12 = -2.0 * sqrt(3.0) / 9.0; - constexpr double expected_g13 = -5.0 / 9.0; - const double expected_g23 = -2.0 * sqrt(3.0) / 9.0; - - EXPECT_TRUE(IsFieldEqual(coords.g11(), expected_g11)); - EXPECT_TRUE(IsFieldEqual(coords.g22(), expected_g22)); - EXPECT_TRUE(IsFieldEqual(coords.g33(), expected_g33)); - EXPECT_TRUE(IsFieldEqual(coords.g12(), expected_g12)); - EXPECT_TRUE(IsFieldEqual(coords.g13(), expected_g13)); - EXPECT_TRUE(IsFieldEqual(coords.g23(), expected_g23)); + // Modify covariant components + constexpr double g_11 = 1.0; + constexpr double g_22 = 1.0; + constexpr double g_33 = 1.0; + const double g_12 = sqrt(3.0) / 4.0; // (std::sqrt only constexpr since C++26) + constexpr double g_13 = 0.5; + const double g_23 = sqrt(3.0) / 4.0; // (std::sqrt only constexpr since C++26) + auto updated_metric_tensor = CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23); + coords.setCovariantMetricTensor(updated_metric_tensor); + + // Check that the contravariant components have been calculated corrected + constexpr double expected_g11 = 13.0 / 9.0; + constexpr double expected_g22 = 4.0 / 3.0; + constexpr double expected_g33 = 13.0 / 9.0; + const double expected_g12 = -2.0 * sqrt(3.0) / 9.0; + constexpr double expected_g13 = -5.0 / 9.0; + const double expected_g23 = -2.0 * sqrt(3.0) / 9.0; + + EXPECT_TRUE(IsFieldEqual(coords.g11(), expected_g11)); + EXPECT_TRUE(IsFieldEqual(coords.g22(), expected_g22)); + EXPECT_TRUE(IsFieldEqual(coords.g33(), expected_g33)); + EXPECT_TRUE(IsFieldEqual(coords.g12(), expected_g12)); + EXPECT_TRUE(IsFieldEqual(coords.g13(), expected_g13)); + EXPECT_TRUE(IsFieldEqual(coords.g23(), expected_g23)); } TEST_F(CoordinatesTest, GetCovariantMetricTensor) { From 5ff4df398410191e96500881a6e98fb88fd6b166 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Thu, 6 Jun 2024 09:02:26 +0100 Subject: [PATCH 441/491] Restore semicolons after macros. --- include/bout/fv_ops.hxx | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/include/bout/fv_ops.hxx b/include/bout/fv_ops.hxx index d57f93ce7c..ff98b7ceae 100644 --- a/include/bout/fv_ops.hxx +++ b/include/bout/fv_ops.hxx @@ -192,15 +192,15 @@ template const Field3D Div_par(const Field3D& f_in, const Field3D& v_in, const Field3D& wave_speed_in, bool fixflux = true) { - ASSERT1_FIELDS_COMPATIBLE(f_in, v_in) - ASSERT1_FIELDS_COMPATIBLE(f_in, wave_speed_in) + ASSERT1_FIELDS_COMPATIBLE(f_in, v_in); + ASSERT1_FIELDS_COMPATIBLE(f_in, wave_speed_in); Mesh* mesh = f_in.getMesh(); CellEdges cellboundary; - ASSERT2(f_in.getDirectionY() == v_in.getDirectionY()) - ASSERT2(f_in.getDirectionY() == wave_speed_in.getDirectionY()) + ASSERT2(f_in.getDirectionY() == v_in.getDirectionY()); + ASSERT2(f_in.getDirectionY() == wave_speed_in.getDirectionY()); const bool are_unaligned = ((f_in.getDirectionY() == YDirectionType::Standard) and (v_in.getDirectionY() == YDirectionType::Standard) @@ -386,8 +386,8 @@ const Field3D Div_par(const Field3D& f_in, const Field3D& v_in, */ template const Field3D Div_f_v(const Field3D& n_in, const Vector3D& v, bool bndry_flux) { - ASSERT1(n_in.getLocation() == v.getLocation()) - ASSERT1_FIELDS_COMPATIBLE(n_in, v.x) + ASSERT1(n_in.getLocation() == v.getLocation()); + ASSERT1_FIELDS_COMPATIBLE(n_in, v.x); Mesh* mesh = n_in.getMesh(); From 187c08116e6d9033f55e5f684e3b34526d54482d Mon Sep 17 00:00:00 2001 From: tomc271 Date: Thu, 6 Jun 2024 09:05:21 +0100 Subject: [PATCH 442/491] Revert accidental renaming in docs. --- manual/sphinx/user_docs/new_in_v5.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/manual/sphinx/user_docs/new_in_v5.rst b/manual/sphinx/user_docs/new_in_v5.rst index 8d9820b55a..b9eb637cd5 100644 --- a/manual/sphinx/user_docs/new_in_v5.rst +++ b/manual/sphinx/user_docs/new_in_v5.rst @@ -74,7 +74,7 @@ non-zero ``g_{xz}`` terms. FIXME WHEN COORDINATES REFACTORED ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -In order to simplify a lot of code, the call to `Coordinates::calculateGeometry`, which +In order to simplify a lot of code, the call to `Coordinates::geometry`, which calculates the connection coefficients `Coordinates::G1_11` and so on, has been moved out of the `Coordinates` constructor. This is because computing the coefficients involves derivatives which requires `Coordinates` and causes all From e5d1c77a563c84c66f3961d166c8edb8434719d5 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Thu, 6 Jun 2024 09:07:56 +0100 Subject: [PATCH 443/491] Revert "Clang-Tidy: Method can be made static." This reverts commit 07078edd399b49e66f86a4b5b746a3d3c575979a. --- include/bout/mesh.hxx | 2 +- include/bout/optionsreader.hxx | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/include/bout/mesh.hxx b/include/bout/mesh.hxx index 2bf2ff3019..e2437535ba 100644 --- a/include/bout/mesh.hxx +++ b/include/bout/mesh.hxx @@ -658,7 +658,7 @@ public: /// Returns the non-CELL_CENTRE location /// allowed as a staggered location - static CELL_LOC getAllowedStaggerLoc(DIRECTION direction) { + CELL_LOC getAllowedStaggerLoc(DIRECTION direction) const { AUTO_TRACE(); switch (direction) { case (DIRECTION::X): diff --git a/include/bout/optionsreader.hxx b/include/bout/optionsreader.hxx index 41d068a9e2..de3d40514d 100644 --- a/include/bout/optionsreader.hxx +++ b/include/bout/optionsreader.hxx @@ -67,7 +67,7 @@ public: /// /// @param[inout] options The options section to insert values and subsections into /// @param[in] file The name of the file. printf style arguments can be used to create the file name. - static void read(Options* options, const std::string& filename); + void read(Options* options, const std::string& filename); template void read(Options* options, const S& format, const Args&... args) { @@ -78,7 +78,7 @@ public: /// /// @param[in] options The options tree to be written /// @param[in] file The name of the file to (over)write - static void write(Options* options, const std::string& filename); + void write(Options* options, const std::string& filename); template void write(Options* options, const S& format, const Args&... args) { From 12e769cdc0947cb7b59a695784ce691cd2f39512 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Thu, 6 Jun 2024 10:26:06 +0100 Subject: [PATCH 444/491] Revert "More unused #include directives." This reverts commit 0dfafd42d10259079f32be638898ab6123664d33. --- src/field/field.cxx | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/field/field.cxx b/src/field/field.cxx index eb913cd5b9..e48a8f3ef7 100644 --- a/src/field/field.cxx +++ b/src/field/field.cxx @@ -23,9 +23,12 @@ * **************************************************************************/ +#include #include #include #include +#include +#include #include Field::Field(Mesh* localmesh, CELL_LOC location_in, DirectionTypes directions_in) From b692beb9af1bff2690e54b9c4d94dd72958dadaf Mon Sep 17 00:00:00 2001 From: tomc271 Date: Thu, 6 Jun 2024 11:59:29 +0100 Subject: [PATCH 445/491] Clang-Tidy: Static member accessed through instance. --- src/mesh/g_values.cxx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/mesh/g_values.cxx b/src/mesh/g_values.cxx index 70760ee394..8b23363f0f 100644 --- a/src/mesh/g_values.cxx +++ b/src/mesh/g_values.cxx @@ -19,13 +19,13 @@ GValues::GValues(const Coordinates& coordinates) { const auto& g23 = contravariantMetricTensor.g23(); auto tmp = J * g12; - coordinates.communicate(tmp); + Coordinates::communicate(tmp); setG1((coordinates.DDX(J * g11) + coordinates.DDY(tmp) + coordinates.DDZ(J * g13)) / J); tmp = J * g22; - coordinates.communicate(tmp); + Coordinates::communicate(tmp); setG2((coordinates.DDX(J * g12) + coordinates.DDY(tmp) + coordinates.DDZ(J * g23)) / J); tmp = J * g23; - coordinates.communicate(tmp); + Coordinates::communicate(tmp); setG3((coordinates.DDX(J * g13) + coordinates.DDY(tmp) + coordinates.DDZ(J * g33)) / J); } From 804d77a5f3ce9bd926e16f8688bbba4a372d8a17 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Thu, 6 Jun 2024 12:04:31 +0100 Subject: [PATCH 446/491] Clang-Tidy: variable can be declared 'const'. --- src/mesh/mesh.cxx | 3 ++- tests/integrated/test-laplacexz/test-laplacexz.cxx | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/mesh/mesh.cxx b/src/mesh/mesh.cxx index 460498664e..f1007895f0 100644 --- a/src/mesh/mesh.cxx +++ b/src/mesh/mesh.cxx @@ -180,7 +180,8 @@ FieldMetric Mesh::get(const std::string& name, BoutReal def, bool communicate, auto var = FieldMetric(this, location); - bool failed_to_get_from_GridDataSource = !source->get(this, var, name, def, location); + const bool failed_to_get_from_GridDataSource = + !source->get(this, var, name, def, location); if (source == nullptr or failed_to_get_from_GridDataSource) { // set val to default in source==nullptr too: var = def; diff --git a/tests/integrated/test-laplacexz/test-laplacexz.cxx b/tests/integrated/test-laplacexz/test-laplacexz.cxx index 75b92c3f30..ac87b82576 100644 --- a/tests/integrated/test-laplacexz/test-laplacexz.cxx +++ b/tests/integrated/test-laplacexz/test-laplacexz.cxx @@ -28,9 +28,9 @@ int main(int argc, char** argv) { Field3D f = FieldFactory::get()->create3D("f", Options::getRoot(), bout::globals::mesh); // Calculate the Laplacian with non-zero g13 - Field3D g = + const Field3D g = coord->g11() * D2DX2(f) + coord->g13() * D2DXDZ(f) + coord->g33() * D2DZ2(f); - + inv->setCoefs(Field2D(1.0), Field2D(0.0)); Field3D f2 = inv->solve(g, 0.0); // Invert the Laplacian. From ae29dc8c093fea1684b22d73e65c382bc29d7b89 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Thu, 6 Jun 2024 11:11:32 +0000 Subject: [PATCH 447/491] Apply clang-format changes --- tests/integrated/test-laplacexz/test-laplacexz.cxx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/integrated/test-laplacexz/test-laplacexz.cxx b/tests/integrated/test-laplacexz/test-laplacexz.cxx index ac87b82576..1b3d22b2b1 100644 --- a/tests/integrated/test-laplacexz/test-laplacexz.cxx +++ b/tests/integrated/test-laplacexz/test-laplacexz.cxx @@ -30,7 +30,7 @@ int main(int argc, char** argv) { // Calculate the Laplacian with non-zero g13 const Field3D g = coord->g11() * D2DX2(f) + coord->g13() * D2DXDZ(f) + coord->g33() * D2DZ2(f); - + inv->setCoefs(Field2D(1.0), Field2D(0.0)); Field3D f2 = inv->solve(g, 0.0); // Invert the Laplacian. From 77896a3ee0734d6ac33f00212179e2415089f23a Mon Sep 17 00:00:00 2001 From: tomc271 Date: Fri, 7 Jun 2024 12:06:58 +0100 Subject: [PATCH 448/491] Rename setBoundaryCells() to readFromMesh(), and interpolateFieldsFromOtherCoordinates() to interpolateFromCoordinates(). --- include/bout/coordinates.hxx | 4 ++-- src/mesh/coordinates.cxx | 10 +++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index 743a0258ba..a9c70259e2 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -396,10 +396,10 @@ private: /// Non-uniform meshes. Need to use DDX, DDY void correctionForNonUniformMeshes(bool force_interpolate_from_centre); - void interpolateFieldsFromOtherCoordinates(Options* options, + void interpolateFromCoordinates(Options* options, const Coordinates* coords_in); - void setBoundaryCells(Options* options, const std::string& suffix); + void readFromMesh(Options* options, const std::string& suffix); FieldMetric getDzFromOptionsFile(Mesh* mesh, const std::string& suffix) const; diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 6e7d010a7f..c3a5a33ce4 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -454,7 +454,7 @@ Coordinates::Coordinates(Mesh* mesh, Options* mesh_options, const CELL_LOC loc, const std::string suffix = getLocationSuffix(location); - if (coords_in == nullptr || suffix.empty() + if (coords_in == nullptr || location == CELL_CENTRE || (!force_interpolate_from_centre && mesh->sourceHasVar("dx" + suffix))) { if (coords_in == nullptr) { @@ -462,14 +462,14 @@ Coordinates::Coordinates(Mesh* mesh, Options* mesh_options, const CELL_LOC loc, mesh->get(dy_, "dy", 1.0, false); } - setBoundaryCells(mesh_options, suffix); + readFromMesh(mesh_options, suffix); } else { - interpolateFieldsFromOtherCoordinates(mesh_options, coords_in); + interpolateFromCoordinates(mesh_options, coords_in); } } -void Coordinates::interpolateFieldsFromOtherCoordinates(Options* mesh_options, +void Coordinates::interpolateFromCoordinates(Options* mesh_options, const Coordinates* coords_in) { // Need to ensure parallel transform is set before differential operators are used, @@ -529,7 +529,7 @@ void Coordinates::interpolateFieldsFromOtherCoordinates(Options* mesh_options, // Note: If boundary cells were not loaded from the grid file, use // 'interpolateAndExtrapolate' to set them. Ensures that derivatives are // smooth at all the boundaries. -void Coordinates::setBoundaryCells(Options* mesh_options, const std::string& suffix) { +void Coordinates::readFromMesh(Options* mesh_options, const std::string& suffix) { const bool extrapolate_x = (*mesh_options)["extrapolate_x"].withDefault( not localmesh->sourceHasXBoundaryGuards()); From 7c684627a338245eb2190b57ccd9ee3890ea02c1 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Fri, 7 Jun 2024 15:04:19 +0100 Subject: [PATCH 449/491] Bug fix: can't assign to a const ref - Use a temporary variable with mesh->get(). --- examples/reconnect-2field/2field.cxx | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/examples/reconnect-2field/2field.cxx b/examples/reconnect-2field/2field.cxx index d270d2af29..a762a6d526 100644 --- a/examples/reconnect-2field/2field.cxx +++ b/examples/reconnect-2field/2field.cxx @@ -71,7 +71,8 @@ class TwoField : public PhysicsModel { // Load metrics GRID_LOAD(Rxy, Bpxy, Btxy, hthe); - mesh->get(coord->Bxy(), "Bxy"); + auto tmp = coord->Bxy(); + mesh->get(tmp, "Bxy"); // Set locations of staggered fields Apar.setLocation(CELL_YLOW); From 69eee9725cde07982f707566eade72b3d738aca6 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Fri, 7 Jun 2024 13:43:42 +0100 Subject: [PATCH 450/491] Remove const qualifier from declaration of temporary copy of Bxy. --- examples/lapd-drift/lapd_drift.cxx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/lapd-drift/lapd_drift.cxx b/examples/lapd-drift/lapd_drift.cxx index cb9596e690..506b97a6e2 100644 --- a/examples/lapd-drift/lapd_drift.cxx +++ b/examples/lapd-drift/lapd_drift.cxx @@ -415,7 +415,7 @@ class LAPDdrift : public PhysicsModel { SAVE_ONCE(Ni0, Te0, phi0, rho0); SAVE_ONCE(Rxy, Bpxy, Btxy, Zxy, hthe); - const FieldMetric tmp = coord->Bxy(); + FieldMetric tmp = coord->Bxy(); dump.addOnce(tmp, "Bxy"); dump.addOnce(my_ixseps, "ixseps"); From 541ff17b3e424f1eca57bf672ec650d274333218 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Fri, 7 Jun 2024 13:09:31 +0100 Subject: [PATCH 451/491] x, y, z are accessed through getters on BoundaryRegionPar. --- examples/fci-wave-logn/fci-wave.cxx | 20 +++++++++++--------- examples/fci-wave/fci-wave.cxx | 23 ++++++++++++++--------- 2 files changed, 25 insertions(+), 18 deletions(-) diff --git a/examples/fci-wave-logn/fci-wave.cxx b/examples/fci-wave-logn/fci-wave.cxx index d934b91ed3..2235d3b7db 100644 --- a/examples/fci-wave-logn/fci-wave.cxx +++ b/examples/fci-wave-logn/fci-wave.cxx @@ -30,9 +30,9 @@ class FCIwave : public PhysicsModel { const Field3D& B_next = Bxyz.ynext(reg->dir); for (reg->first(); !reg->isDone(); reg->next()) { - f_B_next(reg->x, reg->y + reg->dir, reg->z) = - f_next(reg->x, reg->y + reg->dir, reg->z) - / B_next(reg->x, reg->y + reg->dir, reg->z); + f_B_next(reg->ind().x(), reg->ind().y() + reg->dir, reg->ind().z()) = + f_next(reg->ind().x(), reg->ind().y() + reg->dir, reg->ind().z()) + / B_next(reg->ind().x(), reg->ind().y() + reg->dir, reg->ind().z()); } } @@ -108,15 +108,17 @@ class FCIwave : public PhysicsModel { const Field3D& v_next = v.ynext(reg->dir); for (reg->first(); !reg->isDone(); reg->next()) { - BoutReal n_b = exp(0.5 - * (logn_next(reg->x, reg->y + reg->dir, reg->z) - + logn(reg->x, reg->y, reg->z))); + BoutReal n_b = + exp(0.5 + * (logn_next(reg->ind().x(), reg->ind().y() + reg->dir, reg->ind().z()) + + logn(reg->ind().x(), reg->ind().y(), reg->ind().z()))); BoutReal v_b = 0.5 - * (v_next(reg->x, reg->y + reg->dir, reg->z) + v(reg->x, reg->y, reg->z)); + * (v_next(reg->ind().x(), reg->ind().y() + reg->dir, reg->ind().z()) + + v(reg->ind().x(), reg->ind().y(), reg->ind().z())); - nv_next(reg->x, reg->y + reg->dir, reg->z) = - 2. * n_b * v_b - nv(reg->x, reg->y, reg->z); + nv_next(reg->ind().x(), reg->ind().y() + reg->dir, reg->ind().z()) = + 2. * n_b * v_b - nv(reg->ind().x(), reg->ind().y(), reg->ind().z()); } } diff --git a/examples/fci-wave/fci-wave.cxx b/examples/fci-wave/fci-wave.cxx index 4bf2c7b714..220db59d37 100644 --- a/examples/fci-wave/fci-wave.cxx +++ b/examples/fci-wave/fci-wave.cxx @@ -31,9 +31,9 @@ class FCIwave : public PhysicsModel { const Field3D& B_next = Bxyz.ynext(reg->dir); for (reg->first(); !reg->isDone(); reg->next()) { - f_B_next(reg->x, reg->y + reg->dir, reg->z) = - f_next(reg->x, reg->y + reg->dir, reg->z) - / B_next(reg->x, reg->y + reg->dir, reg->z); + f_B_next(reg->ind().x(), reg->ind().y() + reg->dir, reg->ind().z()) = + f_next(reg->ind().x(), reg->ind().y() + reg->dir, reg->ind().z()) + / B_next(reg->ind().x(), reg->ind().y() + reg->dir, reg->ind().z()); } } @@ -131,16 +131,21 @@ class FCIwave : public PhysicsModel { // Note: If evolving density, this should interpolate logn // but neumann boundaries are used here anyway. BoutReal n_b = - 0.5 * (n_next(reg->x, reg->y + reg->dir, reg->z) + n(reg->x, reg->y, reg->z)); + 0.5 + * (n_next(reg->ind().x(), reg->ind().y() + reg->dir, reg->ind().z()) + + n(reg->ind().x(), reg->ind().y(), reg->ind().z())); // Velocity at the boundary BoutReal v_b = - 0.5 * (v_next(reg->x, reg->y + reg->dir, reg->z) + v(reg->x, reg->y, reg->z)); + 0.5 + * (v_next(reg->ind().x(), reg->ind().y() + reg->dir, reg->ind().z()) + + v(reg->ind().x(), reg->ind().y(), reg->ind().z())); - nv_next(reg->x, reg->y + reg->dir, reg->z) = - 2. * n_b * v_b - nv(reg->x, reg->y, reg->z); + nv_next(reg->ind().x(), reg->ind().y() + reg->dir, reg->ind().z()) = + 2. * n_b * v_b - nv(reg->ind().x(), reg->ind().y(), reg->ind().z()); - momflux_next(reg->x, reg->y + reg->dir, reg->z) = - 2. * n_b * v_b * v_b - momflux(reg->x, reg->y, reg->z); + momflux_next(reg->ind().x(), reg->ind().y() + reg->dir, reg->ind().z()) = + 2. * n_b * v_b * v_b + - momflux(reg->ind().x(), reg->ind().y(), reg->ind().z()); } } From ec1e0acca3d91c43f31beaf787cb51145027dbb0 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Fri, 7 Jun 2024 15:04:19 +0100 Subject: [PATCH 452/491] Bug fix: can't assign to a const ref - Use a temporary variable with mesh->get(). --- examples/shear-alfven-wave/2fluid.cxx | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/examples/shear-alfven-wave/2fluid.cxx b/examples/shear-alfven-wave/2fluid.cxx index bec7acc1c4..52384a24cf 100644 --- a/examples/shear-alfven-wave/2fluid.cxx +++ b/examples/shear-alfven-wave/2fluid.cxx @@ -82,7 +82,8 @@ class ShearAlfven : public PhysicsModel { GRID_LOAD(Bpxy); GRID_LOAD(Btxy); GRID_LOAD(hthe); - mesh->get(coord->Bxy(), "Bxy"); + auto tmp = coord->Bxy(); + mesh->get(tmp, "Bxy"); coord->setDx(mesh->get("dpsi")); mesh->get(I, "sinty"); From 67e4c3d2246e8cfb4c1b8abd85eba11b5b8913e7 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Mon, 10 Jun 2024 12:15:05 +0100 Subject: [PATCH 453/491] Define local variables on assignment (and using auto). --- src/mesh/coordinates.cxx | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index c3a5a33ce4..fe2eee818f 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -568,13 +568,12 @@ void Coordinates::readFromMesh(Options* mesh_options, const std::string& suffix) // grid data source has staggered fields, so read instead of interpolating // Diagonal components of metric tensor g^{ij} (default to 1) - FieldMetric g11, g22, g33, g12, g13, g23; - g11 = getAtLocOrUnaligned(localmesh, "g11", 1.0, suffix, location); - g22 = getAtLocOrUnaligned(localmesh, "g22", 1.0, suffix, location); - g33 = getAtLocOrUnaligned(localmesh, "g33", 1.0, suffix, location); - g12 = getAtLocOrUnaligned(localmesh, "g12", 0.0, suffix, location); - g13 = getAtLocOrUnaligned(localmesh, "g13", 0.0, suffix, location); - g23 = getAtLocOrUnaligned(localmesh, "g23", 0.0, suffix, location); + const auto g11 = getAtLocOrUnaligned(localmesh, "g11", 1.0, suffix, location); + const auto g22 = getAtLocOrUnaligned(localmesh, "g22", 1.0, suffix, location); + const auto g33 = getAtLocOrUnaligned(localmesh, "g33", 1.0, suffix, location); + const auto g12 = getAtLocOrUnaligned(localmesh, "g12", 0.0, suffix, location); + const auto g13 = getAtLocOrUnaligned(localmesh, "g13", 0.0, suffix, location); + const auto g23 = getAtLocOrUnaligned(localmesh, "g23", 0.0, suffix, location); contravariantMetricTensor.setMetricTensor( ContravariantMetricTensor(g11, g22, g33, g12, g13, g23)); @@ -602,13 +601,12 @@ void Coordinates::readFromMesh(Options* mesh_options, const std::string& suffix) if (std::all_of(begin(covariant_component_names), end(covariant_component_names), source_has_component)) { - FieldMetric g_11, g_22, g_33, g_12, g_13, g_23; - g_11 = getAtLocOrUnaligned(localmesh, "g_11", 1.0, suffix, location); - g_22 = getAtLocOrUnaligned(localmesh, "g_22", 1.0, suffix, location); - g_33 = getAtLocOrUnaligned(localmesh, "g_33", 1.0, suffix, location); - g_12 = getAtLocOrUnaligned(localmesh, "g_12", 0.0, suffix, location); - g_13 = getAtLocOrUnaligned(localmesh, "g_13", 0.0, suffix, location); - g_23 = getAtLocOrUnaligned(localmesh, "g_23", 0.0, suffix, location); + const auto g_11 = getAtLocOrUnaligned(localmesh, "g_11", 1.0, suffix, location); + const auto g_22 = getAtLocOrUnaligned(localmesh, "g_22", 1.0, suffix, location); + const auto g_33 = getAtLocOrUnaligned(localmesh, "g_33", 1.0, suffix, location); + const auto g_12 = getAtLocOrUnaligned(localmesh, "g_12", 0.0, suffix, location); + const auto g_13 = getAtLocOrUnaligned(localmesh, "g_13", 0.0, suffix, location); + const auto g_23 = getAtLocOrUnaligned(localmesh, "g_23", 0.0, suffix, location); covariantMetricTensor.setMetricTensor( CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); From 2e8807345b295a1bd8993a46994fefa8bcd095a1 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Mon, 10 Jun 2024 12:27:15 +0100 Subject: [PATCH 454/491] Explicity setting the jacobian has to come after setting the metric tensor because it will invalidate the (cached) jacobian, which will then be recalculated from the metric tensor). --- examples/6field-simple/elm_6f.cxx | 6 +++--- examples/conducting-wall-mode/cwm.cxx | 4 ++-- examples/constraints/alfven-wave/alfven.cxx | 5 +++-- examples/dalf3/dalf3.cxx | 6 +++--- examples/elm-pb-outerloop/elm_pb_outerloop.cxx | 6 +++--- examples/elm-pb/elm_pb.cxx | 6 +++--- examples/em-drift/2fluid.cxx | 4 ++-- examples/gyro-gem/gem.cxx | 6 +++--- examples/jorek-compare/jorek_compare.cxx | 6 +++--- examples/lapd-drift/lapd_drift.cxx | 4 ++-- examples/laplacexy/alfven-wave/alfven.cxx | 4 ++-- examples/laplacexy/laplace_perp/test.cxx | 6 +++--- examples/reconnect-2field/2field.cxx | 4 ++-- examples/shear-alfven-wave/2fluid.cxx | 4 ++-- examples/tokamak-2fluid/2fluid.cxx | 4 ++-- examples/uedge-benchmark/ue_bmark.cxx | 3 ++- examples/wave-slab/wave_slab.cxx | 4 ++-- tests/MMS/GBS/gbs.cxx | 6 ++++-- tests/MMS/elm-pb/elm_pb.cxx | 6 +++--- tests/MMS/tokamak/tokamak.cxx | 6 ++++-- tests/integrated/test-drift-instability/2fluid.cxx | 4 ++-- tests/integrated/test-interchange-instability/2fluid.cxx | 4 ++-- tests/integrated/test-laplacexy/loadmetric.cxx | 4 ++-- 23 files changed, 59 insertions(+), 53 deletions(-) diff --git a/examples/6field-simple/elm_6f.cxx b/examples/6field-simple/elm_6f.cxx index b3914c26a8..a38a1420e2 100644 --- a/examples/6field-simple/elm_6f.cxx +++ b/examples/6field-simple/elm_6f.cxx @@ -1058,9 +1058,6 @@ class Elm_6f : public PhysicsModel { const auto g13 = -I * g11; const auto g23 = -Btxy / (hthe * Bpxy * Rxy); - coord->setJ(hthe / Bpxy); - coord->setBxy(B0); - const auto g_11 = 1.0 / g11 + SQ(I * Rxy); const auto g_22 = SQ(B0 * hthe / Bpxy); const auto g_33 = Rxy * Rxy; @@ -1071,6 +1068,9 @@ class Elm_6f : public PhysicsModel { coord->setMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23), CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + coord->setJ(hthe / Bpxy); + coord->setBxy(B0); + // Set B field vector B0vec.covariant = false; diff --git a/examples/conducting-wall-mode/cwm.cxx b/examples/conducting-wall-mode/cwm.cxx index 486759c0d5..390980fe6a 100644 --- a/examples/conducting-wall-mode/cwm.cxx +++ b/examples/conducting-wall-mode/cwm.cxx @@ -195,8 +195,6 @@ class CWM : public PhysicsModel { const auto g13 = -I * g11; const auto g23 = -Btxy / (hthe * Bpxy * Rxy); - coord->setJ(hthe / Bpxy); - const auto g_11 = 1.0 / g11 + SQ(I * Rxy); const auto g_22 = SQ(coord->Bxy() * hthe / Bpxy); const auto g_33 = Rxy * Rxy; @@ -207,6 +205,8 @@ class CWM : public PhysicsModel { coord->setMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23), CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + coord->setJ(hthe / Bpxy); + /**************** SET EVOLVING VARIABLES *************/ // Tell BOUT++ which variables to evolve diff --git a/examples/constraints/alfven-wave/alfven.cxx b/examples/constraints/alfven-wave/alfven.cxx index 3f0442ed45..c22659df06 100644 --- a/examples/constraints/alfven-wave/alfven.cxx +++ b/examples/constraints/alfven-wave/alfven.cxx @@ -208,8 +208,6 @@ class Alfven : public PhysicsModel { const auto g13 = -sinty * g11; const auto g23 = -sbp * Btxy / (hthe * Bpxy * Rxy); - coord->setJ(hthe / Bpxy); - const auto g_11 = 1.0 / g11 + SQ(sinty * Rxy); const auto g_22 = SQ(coord->Bxy() * hthe / Bpxy); const auto g_33 = Rxy * Rxy; @@ -219,6 +217,9 @@ class Alfven : public PhysicsModel { coord->setMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23), CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + + coord->setJ(hthe / Bpxy); + } }; diff --git a/examples/dalf3/dalf3.cxx b/examples/dalf3/dalf3.cxx index 80db2eec19..ea1e7ae40a 100644 --- a/examples/dalf3/dalf3.cxx +++ b/examples/dalf3/dalf3.cxx @@ -260,9 +260,6 @@ class DALF3 : public PhysicsModel { const auto g13 = -I * g11; const auto g23 = -Btxy / (hthe * Bpxy * Rxy); - coord->setJ(hthe / Bpxy); - coord->setBxy(B0); - const auto g_11 = 1.0 / g11 + SQ(I * Rxy); const auto g_22 = SQ(B0 * hthe / Bpxy); const auto g_33 = Rxy * Rxy; @@ -273,6 +270,9 @@ class DALF3 : public PhysicsModel { coord->setMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23), CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + coord->setJ(hthe / Bpxy); + coord->setBxy(B0); + SOLVE_FOR3(Vort, Pe, Vpar); comms.add(Vort, Pe, Vpar); if (!(estatic && ZeroElMass)) { diff --git a/examples/elm-pb-outerloop/elm_pb_outerloop.cxx b/examples/elm-pb-outerloop/elm_pb_outerloop.cxx index 259fe9faca..bc52714b1c 100644 --- a/examples/elm-pb-outerloop/elm_pb_outerloop.cxx +++ b/examples/elm-pb-outerloop/elm_pb_outerloop.cxx @@ -1099,9 +1099,6 @@ class ELMpb : public PhysicsModel { const auto g13 = -I * g11; const auto g23 = -Btxy / (hthe * Bpxy * Rxy); - metric->setJ(hthe / Bpxy); - metric->setBxy(B0); - const auto g_11 = 1.0 / g11 + SQ(I * Rxy); const auto g_22 = SQ(B0 * hthe / Bpxy); const auto g_33 = Rxy * Rxy; @@ -1112,6 +1109,9 @@ class ELMpb : public PhysicsModel { metric->setMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23), CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + metric->setJ(hthe / Bpxy); + metric->setBxy(B0); + // Set B field vector B0vec.covariant = false; diff --git a/examples/elm-pb/elm_pb.cxx b/examples/elm-pb/elm_pb.cxx index e7a3263278..51969f68ec 100644 --- a/examples/elm-pb/elm_pb.cxx +++ b/examples/elm-pb/elm_pb.cxx @@ -1040,9 +1040,6 @@ class ELMpb : public PhysicsModel { const auto g13 = -I * g11; const auto g23 = -Btxy / (hthe * Bpxy * Rxy); - metric->setJ(hthe / Bpxy); - metric->setBxy(B0); - const auto g_11 = 1.0 / g11 + SQ(I * Rxy); const auto g_22 = SQ(B0 * hthe / Bpxy); const auto g_33 = Rxy * Rxy; @@ -1053,6 +1050,9 @@ class ELMpb : public PhysicsModel { metric->setMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23), CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + metric->setJ(hthe / Bpxy); + metric->setBxy(B0); + // Set B field vector B0vec.covariant = false; diff --git a/examples/em-drift/2fluid.cxx b/examples/em-drift/2fluid.cxx index b4dbfdd676..179838097e 100644 --- a/examples/em-drift/2fluid.cxx +++ b/examples/em-drift/2fluid.cxx @@ -185,8 +185,6 @@ class EMdrift : public PhysicsModel { const auto g13 = -I * g11; const auto g23 = -Btxy / (hthe * Bpxy * Rxy); - coord->setJ(hthe / Bpxy); - const auto g_11 = 1.0 / g11 + SQ(I * Rxy); const auto g_22 = SQ(coord->Bxy() * hthe / Bpxy); const auto g_33 = Rxy * Rxy; @@ -197,6 +195,8 @@ class EMdrift : public PhysicsModel { coord->setMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23), CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + coord->setJ(hthe / Bpxy); + /**************** SET EVOLVING VARIABLES *************/ // Tell BOUT++ which variables to evolve diff --git a/examples/gyro-gem/gem.cxx b/examples/gyro-gem/gem.cxx index f8c42397a3..cad585beb4 100644 --- a/examples/gyro-gem/gem.cxx +++ b/examples/gyro-gem/gem.cxx @@ -376,9 +376,6 @@ class GEM : public PhysicsModel { const auto g13 = 0.; const auto g23 = -Btxy / (hthe * Bpxy * Rxy); - coord->setJ(hthe / Bpxy); - coord->setBxy(Bxy); - const auto g_11 = 1.0 / g11; const auto g_22 = SQ(Bxy * hthe / Bpxy); const auto g_33 = Rxy * Rxy; @@ -389,6 +386,9 @@ class GEM : public PhysicsModel { coord->setMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23), CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + coord->setJ(hthe / Bpxy); + coord->setBxy(Bxy); + // Set B field vector B0vec.covariant = false; diff --git a/examples/jorek-compare/jorek_compare.cxx b/examples/jorek-compare/jorek_compare.cxx index 9b6d3fdbea..f9fcbe8df4 100644 --- a/examples/jorek-compare/jorek_compare.cxx +++ b/examples/jorek-compare/jorek_compare.cxx @@ -306,9 +306,6 @@ class Jorek : public PhysicsModel { const auto g13 = -I * g11; const auto g23 = -Btxy / (hthe * Bpxy * Rxy); - coord->setJ(hthe / Bpxy); - coord->setBxy(B0); - const auto g_11 = 1.0 / g11 + SQ(I * Rxy); const auto g_22 = SQ(B0 * hthe / Bpxy); const auto g_33 = Rxy * Rxy; @@ -319,6 +316,9 @@ class Jorek : public PhysicsModel { coord->setMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23), CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + coord->setJ(hthe / Bpxy); + coord->setBxy(B0); + // Set B field vector B0vec.covariant = false; B0vec.x = 0.; diff --git a/examples/lapd-drift/lapd_drift.cxx b/examples/lapd-drift/lapd_drift.cxx index 506b97a6e2..8c7058cc95 100644 --- a/examples/lapd-drift/lapd_drift.cxx +++ b/examples/lapd-drift/lapd_drift.cxx @@ -339,8 +339,6 @@ class LAPDdrift : public PhysicsModel { const auto g13 = -I * g11; const auto g23 = -Btxy / (hthe * Bpxy * Rxy); - coord->setJ(hthe / Bpxy); - const auto g_11 = 1.0 / g11 + SQ(I * Rxy); const auto g_22 = SQ(coord->Bxy() * hthe / Bpxy); const auto g_33 = Rxy * Rxy; @@ -351,6 +349,8 @@ class LAPDdrift : public PhysicsModel { coord->setMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23), CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + coord->setJ(hthe / Bpxy); + rho0 = Ni0 * Delp2(phi0) + Perp_Grad_dot_Grad(phi0, Ni0); /**************** SET EVOLVING VARIABLES *************/ diff --git a/examples/laplacexy/alfven-wave/alfven.cxx b/examples/laplacexy/alfven-wave/alfven.cxx index 7b22d5e887..93e7ad2814 100644 --- a/examples/laplacexy/alfven-wave/alfven.cxx +++ b/examples/laplacexy/alfven-wave/alfven.cxx @@ -209,8 +209,6 @@ class Alfven : public PhysicsModel { const auto g13 = -sinty * g11; const auto g23 = -sbp * Btxy / (hthe * Bpxy * Rxy); - coord->setJ(hthe / Bpxy); - const auto g_11 = 1.0 / g11 + SQ(sinty * Rxy); const auto g_22 = SQ(coord->Bxy() * hthe / Bpxy); const auto g_33 = Rxy * Rxy; @@ -220,6 +218,8 @@ class Alfven : public PhysicsModel { coord->setMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23), CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + + coord->setJ(hthe / Bpxy); } }; diff --git a/examples/laplacexy/laplace_perp/test.cxx b/examples/laplacexy/laplace_perp/test.cxx index 766a16ca40..8827810599 100644 --- a/examples/laplacexy/laplace_perp/test.cxx +++ b/examples/laplacexy/laplace_perp/test.cxx @@ -32,9 +32,6 @@ int main(int argc, char** argv) { const auto g13 = -I * g11; const auto g23 = -Btxy / (hthe * Bpxy * Rxy); - coord->setJ(hthe / Bpxy); - coord->setBxy(B0); - const auto g_11 = 1.0 / g11 + SQ(I * Rxy); const auto g_22 = SQ(B0 * hthe / Bpxy); const auto g_33 = Rxy * Rxy; @@ -44,6 +41,9 @@ int main(int argc, char** argv) { coord->setMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23), CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + + coord->setJ(hthe / Bpxy); + coord->setBxy(B0); } /////////////////////////////////////// diff --git a/examples/reconnect-2field/2field.cxx b/examples/reconnect-2field/2field.cxx index a762a6d526..824a2fff26 100644 --- a/examples/reconnect-2field/2field.cxx +++ b/examples/reconnect-2field/2field.cxx @@ -171,8 +171,6 @@ class TwoField : public PhysicsModel { const auto g13 = 0.; const auto g23 = -Btxy / (hthe * Bpxy * Rxy); - coord->setJ(hthe / Bpxy); - const auto g_11 = 1.0 / g11; const auto g_22 = SQ(coord->Bxy() * hthe / Bpxy); const auto g_33 = Rxy * Rxy; @@ -183,6 +181,8 @@ class TwoField : public PhysicsModel { coord->setMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23), CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + coord->setJ(hthe / Bpxy); + // Tell BOUT++ which variables to evolve SOLVE_FOR(U, Apar); diff --git a/examples/shear-alfven-wave/2fluid.cxx b/examples/shear-alfven-wave/2fluid.cxx index 52384a24cf..cfd7803952 100644 --- a/examples/shear-alfven-wave/2fluid.cxx +++ b/examples/shear-alfven-wave/2fluid.cxx @@ -190,8 +190,6 @@ class ShearAlfven : public PhysicsModel { const auto g13 = -I * g11; const auto g23 = -Btxy / (hthe * Bpxy * Rxy); - coord->setJ(hthe / Bpxy); - const auto g_11 = 1.0 / g11 + SQ(I * Rxy); const auto g_22 = SQ(coord->Bxy() * hthe / Bpxy); const auto g_33 = Rxy * Rxy; @@ -202,6 +200,8 @@ class ShearAlfven : public PhysicsModel { coord->setMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23), CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + coord->setJ(hthe / Bpxy); + /**************** SET EVOLVING VARIABLES *************/ // Tell BOUT++ which variables to evolve diff --git a/examples/tokamak-2fluid/2fluid.cxx b/examples/tokamak-2fluid/2fluid.cxx index a485360304..386587f87e 100644 --- a/examples/tokamak-2fluid/2fluid.cxx +++ b/examples/tokamak-2fluid/2fluid.cxx @@ -406,8 +406,6 @@ class TwoFluid : public PhysicsModel { const auto g13 = -I * g11; const auto g23 = -Btxy / (hthe * Bpxy * Rxy); - coord->setJ(hthe / Bpxy); - const auto g_11 = 1.0 / g11 + SQ(I * Rxy); const auto g_22 = SQ(coord->Bxy() * hthe / Bpxy); const auto g_33 = Rxy * Rxy; @@ -418,6 +416,8 @@ class TwoFluid : public PhysicsModel { coord->setMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23), CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + coord->setJ(hthe / Bpxy); + //////////////////////////////////////////////////////// // SET EVOLVING VARIABLES diff --git a/examples/uedge-benchmark/ue_bmark.cxx b/examples/uedge-benchmark/ue_bmark.cxx index 1297244bcd..b45d6689b4 100644 --- a/examples/uedge-benchmark/ue_bmark.cxx +++ b/examples/uedge-benchmark/ue_bmark.cxx @@ -147,7 +147,6 @@ class UedgeBenchmark : public PhysicsModel { const auto g13 = 0.0; const auto g23 = -Btxy / (hthe * Bpxy * Rxy); - coords->setJ(hthe / Bpxy); const auto g_11 = 1.0 / g11; const auto g_22 = pow(coords->Bxy() * hthe / Bpxy, 2.0); @@ -159,6 +158,8 @@ class UedgeBenchmark : public PhysicsModel { coords->setMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23), CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + coords->setJ(hthe / Bpxy); + //////////////// BOUNDARIES /////////////////////// // // We want to apply the relaxing boundries to total density, diff --git a/examples/wave-slab/wave_slab.cxx b/examples/wave-slab/wave_slab.cxx index a0ebad3a31..0bf4f1a9e7 100644 --- a/examples/wave-slab/wave_slab.cxx +++ b/examples/wave-slab/wave_slab.cxx @@ -38,8 +38,6 @@ class WaveTest : public PhysicsModel { const auto g13 = -I * g11; const auto g23 = -Btxy / (hthe * Bpxy * Rxy); - coords->setJ(hthe / Bpxy); - const auto g_11 = 1.0 / g11 + (pow(I * Rxy, 2.0)); const auto g_22 = pow(coords->Bxy() * hthe / Bpxy, 2.0); const auto g_33 = Rxy * Rxy; @@ -50,6 +48,8 @@ class WaveTest : public PhysicsModel { coords->setMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23), CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + coords->setJ(hthe / Bpxy); + solver->add(f, "f"); solver->add(g, "g"); diff --git a/tests/MMS/GBS/gbs.cxx b/tests/MMS/GBS/gbs.cxx index d03bbd04fa..9115fa24d1 100644 --- a/tests/MMS/GBS/gbs.cxx +++ b/tests/MMS/GBS/gbs.cxx @@ -355,16 +355,18 @@ void GBS::LoadMetric(BoutReal Lnorm, BoutReal Bnorm) { const auto g13 = -sinty * g11; const auto g23 = -sbp * Btxy / (hthe * Bpxy * Rxy); - coords->setJ(hthe / Bpxy); - const auto g_11 = 1.0 / g11 + SQ(sinty * Rxy); const auto g_22 = SQ(coords->Bxy() * hthe / Bpxy); const auto g_33 = Rxy * Rxy; const auto g_12 = sbp * Btxy * hthe * sinty * Rxy / Bpxy; const auto g_13 = sinty * Rxy * Rxy; const auto g_23 = sbp * Btxy * hthe * Rxy / Bpxy; + coords->setMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23), CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + + coords->setJ(hthe / Bpxy); + } // just define a macro for V_E dot Grad diff --git a/tests/MMS/elm-pb/elm_pb.cxx b/tests/MMS/elm-pb/elm_pb.cxx index 90504c2bf5..851b2d2a69 100644 --- a/tests/MMS/elm-pb/elm_pb.cxx +++ b/tests/MMS/elm-pb/elm_pb.cxx @@ -424,9 +424,6 @@ class ELMpb : public PhysicsModel { const auto g13 = -I * g11; const auto g23 = -Btxy / (hthe * Bpxy * Rxy); - coords->setJ(hthe / Bpxy); - coords->setBxy(B0); - const auto g_11 = 1.0 / g11 + (SQ(I * Rxy)); const auto g_22 = SQ(B0 * hthe / Bpxy); const auto g_33 = Rxy * Rxy; @@ -437,6 +434,9 @@ class ELMpb : public PhysicsModel { coords->setMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23), CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + coords->setJ(hthe / Bpxy); + coords->setBxy(B0); + // Set B field vector B0vec.covariant = false; diff --git a/tests/MMS/tokamak/tokamak.cxx b/tests/MMS/tokamak/tokamak.cxx index c264b137c2..e23b0177e0 100644 --- a/tests/MMS/tokamak/tokamak.cxx +++ b/tests/MMS/tokamak/tokamak.cxx @@ -87,16 +87,18 @@ class TokamakMMS : public PhysicsModel { const auto g13 = -sinty * g11; const auto g23 = -sbp * Btxy / (hthe * Bpxy * Rxy); - coords->setJ(hthe / Bpxy); - const auto g_11 = 1.0 / g11 + SQ(sinty * Rxy); const auto g_22 = SQ(coords->Bxy() * hthe / Bpxy); const auto g_33 = Rxy * Rxy; const auto g_12 = sbp * Btxy * hthe * sinty * Rxy / Bpxy; const auto g_13 = sinty * Rxy * Rxy; const auto g_23 = sbp * Btxy * hthe * Rxy / Bpxy; + coords->setMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23), CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + + coords->setJ(hthe / Bpxy); + } private: diff --git a/tests/integrated/test-drift-instability/2fluid.cxx b/tests/integrated/test-drift-instability/2fluid.cxx index b1e24d3537..f7b072cae5 100644 --- a/tests/integrated/test-drift-instability/2fluid.cxx +++ b/tests/integrated/test-drift-instability/2fluid.cxx @@ -224,8 +224,6 @@ class TwoFluid : public PhysicsModel { const auto g13 = -I * g11; const auto g23 = -Btxy / (hthe * Bpxy * Rxy); - coord->setJ(hthe / Bpxy); - const auto g_11 = 1.0 / g11 + SQ(I * Rxy); const auto g_22 = SQ(coord->Bxy() * hthe / Bpxy); const auto g_33 = Rxy * Rxy; @@ -236,6 +234,8 @@ class TwoFluid : public PhysicsModel { coord->setMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23), CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + coord->setJ(hthe / Bpxy); + /**************** SET EVOLVING VARIABLES *************/ // Tell BOUT++ which variables to evolve diff --git a/tests/integrated/test-interchange-instability/2fluid.cxx b/tests/integrated/test-interchange-instability/2fluid.cxx index 0afde399ca..cddc81e91c 100644 --- a/tests/integrated/test-interchange-instability/2fluid.cxx +++ b/tests/integrated/test-interchange-instability/2fluid.cxx @@ -146,8 +146,6 @@ class Interchange : public PhysicsModel { const auto g13 = -I * g11; const auto g23 = -Btxy / (hthe * Bpxy * Rxy); - coord->setJ(hthe / Bpxy); - const auto g_11 = 1.0 / g11 + SQ(I * Rxy); const auto g_22 = SQ(coord->Bxy() * hthe / Bpxy); const auto g_33 = Rxy * Rxy; @@ -158,6 +156,8 @@ class Interchange : public PhysicsModel { coord->setMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23), CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + coord->setJ(hthe / Bpxy); + // Tell BOUT++ which variables to evolve SOLVE_FOR2(rho, Ni); diff --git a/tests/integrated/test-laplacexy/loadmetric.cxx b/tests/integrated/test-laplacexy/loadmetric.cxx index 78999096a7..3fccd4acce 100644 --- a/tests/integrated/test-laplacexy/loadmetric.cxx +++ b/tests/integrated/test-laplacexy/loadmetric.cxx @@ -56,8 +56,6 @@ void LoadMetric(BoutReal Lnorm, BoutReal Bnorm) { const auto g13 = -sinty * g11; const auto g23 = -sbp * Btxy / (hthe * Bpxy * Rxy); - coords->setJ(hthe / Bpxy); - const auto g_11 = 1.0 / g11 + pow(sinty * Rxy, 2); const auto g_22 = pow(coords->Bxy() * hthe / Bpxy, 2); const auto g_33 = Rxy * Rxy; @@ -67,4 +65,6 @@ void LoadMetric(BoutReal Lnorm, BoutReal Bnorm) { coords->setMetricTensor(ContravariantMetricTensor(g11, g22, g33, g12, g13, g23), CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); + + coords->setJ(hthe / Bpxy); } From 33111b3fd64c8b41ee4c661e22c14add5ba14b1d Mon Sep 17 00:00:00 2001 From: tomc271 Date: Mon, 10 Jun 2024 12:38:04 +0000 Subject: [PATCH 455/491] Apply clang-format changes --- examples/constraints/alfven-wave/alfven.cxx | 1 - examples/uedge-benchmark/ue_bmark.cxx | 1 - include/bout/coordinates.hxx | 3 +-- src/mesh/coordinates.cxx | 2 +- tests/MMS/GBS/gbs.cxx | 1 - tests/MMS/tokamak/tokamak.cxx | 1 - 6 files changed, 2 insertions(+), 7 deletions(-) diff --git a/examples/constraints/alfven-wave/alfven.cxx b/examples/constraints/alfven-wave/alfven.cxx index c22659df06..2c12c9a568 100644 --- a/examples/constraints/alfven-wave/alfven.cxx +++ b/examples/constraints/alfven-wave/alfven.cxx @@ -219,7 +219,6 @@ class Alfven : public PhysicsModel { CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); coord->setJ(hthe / Bpxy); - } }; diff --git a/examples/uedge-benchmark/ue_bmark.cxx b/examples/uedge-benchmark/ue_bmark.cxx index b45d6689b4..b93b8b0cb6 100644 --- a/examples/uedge-benchmark/ue_bmark.cxx +++ b/examples/uedge-benchmark/ue_bmark.cxx @@ -147,7 +147,6 @@ class UedgeBenchmark : public PhysicsModel { const auto g13 = 0.0; const auto g23 = -Btxy / (hthe * Bpxy * Rxy); - const auto g_11 = 1.0 / g11; const auto g_22 = pow(coords->Bxy() * hthe / Bpxy, 2.0); const auto g_33 = Rxy * Rxy; diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index a9c70259e2..63f2c65418 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -396,8 +396,7 @@ private: /// Non-uniform meshes. Need to use DDX, DDY void correctionForNonUniformMeshes(bool force_interpolate_from_centre); - void interpolateFromCoordinates(Options* options, - const Coordinates* coords_in); + void interpolateFromCoordinates(Options* options, const Coordinates* coords_in); void readFromMesh(Options* options, const std::string& suffix); diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index fe2eee818f..d70a44ce36 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -470,7 +470,7 @@ Coordinates::Coordinates(Mesh* mesh, Options* mesh_options, const CELL_LOC loc, } void Coordinates::interpolateFromCoordinates(Options* mesh_options, - const Coordinates* coords_in) { + const Coordinates* coords_in) { // Need to ensure parallel transform is set before differential operators are used, // but setParallelTransform() requires that dz is already set! diff --git a/tests/MMS/GBS/gbs.cxx b/tests/MMS/GBS/gbs.cxx index 9115fa24d1..dc0b867f29 100644 --- a/tests/MMS/GBS/gbs.cxx +++ b/tests/MMS/GBS/gbs.cxx @@ -366,7 +366,6 @@ void GBS::LoadMetric(BoutReal Lnorm, BoutReal Bnorm) { CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); coords->setJ(hthe / Bpxy); - } // just define a macro for V_E dot Grad diff --git a/tests/MMS/tokamak/tokamak.cxx b/tests/MMS/tokamak/tokamak.cxx index e23b0177e0..74c89bb95d 100644 --- a/tests/MMS/tokamak/tokamak.cxx +++ b/tests/MMS/tokamak/tokamak.cxx @@ -98,7 +98,6 @@ class TokamakMMS : public PhysicsModel { CovariantMetricTensor(g_11, g_22, g_33, g_12, g_13, g_23)); coords->setJ(hthe / Bpxy); - } private: From 2dfdb42be156e9a745088f1b19eae79db006bbeb Mon Sep 17 00:00:00 2001 From: tomc Date: Tue, 16 Jul 2024 16:25:26 +0100 Subject: [PATCH 456/491] Add overload getters for dx, dy, and dz, that give the value at a particular x, y (or x, y, z) position. --- include/bout/coordinates.hxx | 10 ++++++ tests/unit/mesh/test_coordinates.cxx | 48 +++++++++++++++++++++++++++- 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index 63f2c65418..0bfd763ffb 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -80,6 +80,16 @@ public: const FieldMetric& dy() const { return dy_; } const FieldMetric& dz() const { return dz_; } +#if BOUT_USE_METRIC_3D + const BoutReal& dx(int x, int y, int z) const { return dx_(x, y, z); } + const BoutReal& dy(int x, int y, int z) const { return dy_(x, y, z); } + const BoutReal& dz(int x, int y, int z) const { return dz_(x, y, z); } +#else + const BoutReal& dx(int x, int y) const { return dx_(x, y); } + const BoutReal& dy(int x, int y) const { return dy_(x, y); } + const BoutReal& dz(int x, int y) const { return dz_(x, y); } +#endif + void setDx(FieldMetric dx) { dx_ = std::move(dx); } void setDy(FieldMetric dy) { dy_ = std::move(dy); } void setDz(FieldMetric dz) { dz_ = std::move(dz); } diff --git a/tests/unit/mesh/test_coordinates.cxx b/tests/unit/mesh/test_coordinates.cxx index 80739555be..fd37dcc94e 100644 --- a/tests/unit/mesh/test_coordinates.cxx +++ b/tests/unit/mesh/test_coordinates.cxx @@ -528,4 +528,50 @@ TEST_F(CoordinatesTest, SetCovariantMetricTensor) { EXPECT_TRUE(IsFieldEqual(coords.g_13(), 0.0)); EXPECT_TRUE(IsFieldEqual(coords.g_23(), 0.2)); } -} \ No newline at end of file +} + +TEST_F(CoordinatesTest, IndexedAccessors) { + + int x = mesh->xstart; + int y = mesh->ystart; +#if BOUT_USE_METRIC_3D + int z = mesh->LocalNz; +#endif + + output_info.disable(); + output_warn.disable(); + Coordinates coords(mesh); + output_warn.enable(); + output_info.enable(); + + const auto& dx = coords.dx(); + const auto& dy = coords.dy(); +#if BOUT_USE_METRIC_3D + const auto& dz = coords.dz(); +#endif + +#if not(BOUT_USE_METRIC_3D) + const BoutReal expected_dx = dx(x, y); + const BoutReal expected_dy = dy(x, y); +#else + const BoutReal expected_dx = dx(x, y, z); + const BoutReal expected_dy = dy(x, y, z); + const BoutReal expected_dz = dz(x, y, z); +#endif + +#if not(BOUT_USE_METRIC_3D) + const FieldMetric& actual_dx = coords.dx(x, y); + const FieldMetric& actual_dy = coords.dy(x, y); +#else + const Field3D& actual_dx = coords.dx(x, y, z); + const Field3D& actual_dy = coords.dy(x, y, z); + const Field3D& actual_dz = coords.dz(x, y, z); +#endif + + EXPECT_EQ(actual_dx, expected_dx); + EXPECT_EQ(actual_dy, expected_dy); +#if BOUT_USE_METRIC_3D + EXPECT_EQ(actual_dz, expected_dz); +#endif + +} From 4c2a1d1d56311a0e8688d7a11f93408b3ecc56a9 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Tue, 16 Jul 2024 20:53:21 +0100 Subject: [PATCH 457/491] Use overloaded getters for dx, dy, and dz. --- examples/conducting-wall-mode/cwm.cxx | 4 +- .../elm-pb-outerloop/elm_pb_outerloop.cxx | 2 +- examples/elm-pb/elm_pb.cxx | 2 +- examples/lapd-drift/lapd_drift.cxx | 12 +- examples/orszag-tang/mhd.cxx | 2 +- include/bout/fv_ops.hxx | 16 +-- .../impls/multigrid/multigrid_laplace.cxx | 20 +-- .../laplace/impls/petsc/petsc_laplace.cxx | 56 ++++---- .../laplace/impls/serial_band/serial_band.cxx | 44 +++---- src/invert/laplace/invert_laplace.cxx | 36 +++--- .../impls/cyclic/laplacexz-cyclic.cxx | 8 +- .../laplacexz/impls/petsc/laplacexz-petsc.cxx | 28 ++-- src/invert/parderiv/impls/cyclic/cyclic.cxx | 8 +- src/mesh/boundary_standard.cxx | 120 +++++++++--------- src/mesh/difops.cxx | 102 +++++++-------- src/mesh/fv_ops.cxx | 60 ++++----- tests/MMS/diffusion2/diffusion.cxx | 2 +- tests/MMS/spatial/diffusion/diffusion.cxx | 2 +- .../test_multigrid_laplace.cxx | 4 +- .../test_naulin_laplace.cxx | 4 +- tests/unit/mesh/test_coordinates_accessor.cxx | 2 +- 21 files changed, 267 insertions(+), 267 deletions(-) diff --git a/examples/conducting-wall-mode/cwm.cxx b/examples/conducting-wall-mode/cwm.cxx index 390980fe6a..0e2d65a040 100644 --- a/examples/conducting-wall-mode/cwm.cxx +++ b/examples/conducting-wall-mode/cwm.cxx @@ -307,7 +307,7 @@ class CWM : public PhysicsModel { for (int jz = 0; jz < mesh->LocalNz; jz++) { var(xrup.ind, jy, jz) = var(xrup.ind, jy - 1, jz) - + coord->dy()(xrup.ind, jy, jz) + + coord->dy(xrup.ind, jy, jz) * sqrt(coord->g_22()(xrup.ind, jy, jz)) * value(xrup.ind, jy, jz); } @@ -324,7 +324,7 @@ class CWM : public PhysicsModel { for (int jz = 0; jz < mesh->LocalNz; jz++) { var(xrdn.ind, jy, jz) = var(xrdn.ind, jy + 1, jz) - - coord->dy()(xrdn.ind, jy, jz) + - coord->dy(xrdn.ind, jy, jz) * sqrt(coord->g_22()(xrdn.ind, jy, jz)) * value(xrdn.ind, jy, jz); } diff --git a/examples/elm-pb-outerloop/elm_pb_outerloop.cxx b/examples/elm-pb-outerloop/elm_pb_outerloop.cxx index bc52714b1c..747d1f8f6e 100644 --- a/examples/elm-pb-outerloop/elm_pb_outerloop.cxx +++ b/examples/elm-pb-outerloop/elm_pb_outerloop.cxx @@ -769,7 +769,7 @@ class ELMpb : public PhysicsModel { BoutReal angle = rmp_m * pol_angle(jx, jy) - + rmp_n * ((BoutReal)jz) * mesh->getCoordinates()->dz()(jx, jy, jz); + + rmp_n * ((BoutReal)jz) * mesh->getCoordinates()->dz(jx, jy, jz); rmp_Psi0(jx, jy, jz) = (((BoutReal)(jx - 4)) / ((BoutReal)(mesh->LocalNx - 5))) * rmp_factor * cos(angle); diff --git a/examples/elm-pb/elm_pb.cxx b/examples/elm-pb/elm_pb.cxx index 51969f68ec..675970b3a8 100644 --- a/examples/elm-pb/elm_pb.cxx +++ b/examples/elm-pb/elm_pb.cxx @@ -710,7 +710,7 @@ class ELMpb : public PhysicsModel { BoutReal angle = rmp_m * pol_angle(jx, jy) - + rmp_n * ((BoutReal)jz) * mesh->getCoordinates()->dz()(jx, jy, jz); + + rmp_n * ((BoutReal)jz) * mesh->getCoordinates()->dz(jx, jy, jz); rmp_Psi0(jx, jy, jz) = (((BoutReal)(jx - 4)) / ((BoutReal)(mesh->LocalNx - 5))) * rmp_factor * cos(angle); diff --git a/examples/lapd-drift/lapd_drift.cxx b/examples/lapd-drift/lapd_drift.cxx index 8c7058cc95..883b026590 100644 --- a/examples/lapd-drift/lapd_drift.cxx +++ b/examples/lapd-drift/lapd_drift.cxx @@ -774,7 +774,7 @@ class LAPDdrift : public PhysicsModel { 0.25 * ((p(jx, jy, jzp) - p(jx, jy, jzm)) * (f(jx + 1, jy) - f(jx - 1, jy)) - (p(jx + 1, jy, jz) - p(jx - 1, jy, jz)) * (f(jx, jy) - f(jx, jy))) - / (coord->dx()(jx, jy, jz) * coord->dz()(jx, jy, jz)); + / (coord->dx(jx, jy, jz) * coord->dz(jx, jy, jz)); // J+x BoutReal Jpx = 0.25 @@ -782,14 +782,14 @@ class LAPDdrift : public PhysicsModel { - f(jx - 1, jy) * (p(jx - 1, jy, jzp) - p(jx - 1, jy, jzm)) - f(jx, jy) * (p(jx + 1, jy, jzp) - p(jx - 1, jy, jzp)) + f(jx, jy) * (p(jx + 1, jy, jzm) - p(jx - 1, jy, jzm))) - / (coord->dx()(jx, jy, jz) * coord->dz()(jx, jy, jz)); + / (coord->dx(jx, jy, jz) * coord->dz(jx, jy, jz)); // Jx+ BoutReal Jxp = 0.25 * (f(jx + 1, jy) * (p(jx, jy, jzp) - p(jx + 1, jy, jz)) - f(jx - 1, jy) * (p(jx - 1, jy, jz) - p(jx, jy, jzm)) - f(jx - 1, jy) * (p(jx, jy, jzp) - p(jx - 1, jy, jz)) + f(jx + 1, jy) * (p(jx + 1, jy, jz) - p(jx, jy, jzm))) - / (coord->dx()(jx, jy, jz) * coord->dz()(jx, jy, jz)); + / (coord->dx(jx, jy, jz) * coord->dz(jx, jy, jz)); result(jx, jy, jz) = (Jpp + Jpx + Jxp) / 3.; } @@ -840,7 +840,7 @@ class LAPDdrift : public PhysicsModel { * (f(jx + 1, jy, jz) - f(jx - 1, jy, jz)) - (p(jx + 1, jy, jz) - p(jx - 1, jy, jz)) * (f(jx, jy, jzp) - f(jx, jy, jzm))) - / (coord->dx()(jx, jy, jz) * coord->dz()(jx, jy, jz)); + / (coord->dx(jx, jy, jz) * coord->dz(jx, jy, jz)); // J+x BoutReal Jpx = @@ -849,14 +849,14 @@ class LAPDdrift : public PhysicsModel { - f(jx - 1, jy, jz) * (p(jx - 1, jy, jzp) - p(jx - 1, jy, jzm)) - f(jx, jy, jzp) * (p(jx + 1, jy, jzp) - p(jx - 1, jy, jzp)) + f(jx, jy, jzm) * (p(jx + 1, jy, jzm) - p(jx - 1, jy, jzm))) - / (coord->dx()(jx, jy, jz) * coord->dz()(jx, jy, jz)); + / (coord->dx(jx, jy, jz) * coord->dz(jx, jy, jz)); // Jx+ BoutReal Jxp = 0.25 * (f(jx + 1, jy, jzp) * (p(jx, jy, jzp) - p(jx + 1, jy, jz)) - f(jx - 1, jy, jzm) * (p(jx - 1, jy, jz) - p(jx, jy, jzm)) - f(jx - 1, jy, jzp) * (p(jx, jy, jzp) - p(jx - 1, jy, jz)) + f(jx + 1, jy, jzm) * (p(jx + 1, jy, jz) - p(jx, jy, jzm))) - / (coord->dx()(jx, jy, jz) * coord->dz()(jx, jy, jz)); + / (coord->dx(jx, jy, jz) * coord->dz(jx, jy, jz)); result(jx, jy, jz) = (Jpp + Jpx + Jxp) / 3.; } diff --git a/examples/orszag-tang/mhd.cxx b/examples/orszag-tang/mhd.cxx index 6274d9d24f..0e12db0cf4 100644 --- a/examples/orszag-tang/mhd.cxx +++ b/examples/orszag-tang/mhd.cxx @@ -48,7 +48,7 @@ class MHD : public PhysicsModel { Coordinates* coord = mesh->getCoordinates(); output.write("dx(0,0,0) = {:e}, dy(0,0,0) = {:e}, dz(0,0,0) = {:e}\n", - coord->dx()(0, 0, 0), coord->dy()(0, 0, 0), coord->dz()(0, 0, 0)); + coord->dx(0, 0, 0), coord->dy(0, 0, 0), coord->dz(0, 0, 0)); SAVE_REPEAT(divB); diff --git a/include/bout/fv_ops.hxx b/include/bout/fv_ops.hxx index ff98b7ceae..7d5ec5d76a 100644 --- a/include/bout/fv_ops.hxx +++ b/include/bout/fv_ops.hxx @@ -247,17 +247,17 @@ const Field3D Div_par(const Field3D& f_in, const Field3D& v_in, (coord->J()(i, j) + coord->J()(i, j + 1)) / (sqrt(coord->g_22()(i, j)) + sqrt(coord->g_22()(i, j + 1))); - BoutReal flux_factor_rc = common_factor / (coord->dy()(i, j) * coord->J()(i, j)); + BoutReal flux_factor_rc = common_factor / (coord->dy(i, j) * coord->J()(i, j)); BoutReal flux_factor_rp = - common_factor / (coord->dy()(i, j + 1) * coord->J()(i, j + 1)); + common_factor / (coord->dy(i, j + 1) * coord->J()(i, j + 1)); // For left cell boundaries common_factor = (coord->J()(i, j) + coord->J()(i, j - 1)) / (sqrt(coord->g_22()(i, j)) + sqrt(coord->g_22()(i, j - 1))); - BoutReal flux_factor_lc = common_factor / (coord->dy()(i, j) * coord->J()(i, j)); + BoutReal flux_factor_lc = common_factor / (coord->dy(i, j) * coord->J()(i, j)); BoutReal flux_factor_lm = - common_factor / (coord->dy()(i, j - 1) * coord->J()(i, j - 1)); + common_factor / (coord->dy(i, j - 1) * coord->J()(i, j - 1)); #endif for (int k = 0; k < mesh->LocalNz; k++) { #if BOUT_USE_METRIC_3D @@ -267,9 +267,9 @@ const Field3D Div_par(const Field3D& f_in, const Field3D& v_in, / (sqrt(coord->g_22()(i, j, k)) + sqrt(coord->g_22()(i, j + 1, k))); BoutReal flux_factor_rc = - common_factor / (coord->dy()(i, j, k) * coord->J()(i, j, k)); + common_factor / (coord->dy(i, j, k) * coord->J()(i, j, k)); BoutReal flux_factor_rp = - common_factor / (coord->dy()(i, j + 1, k) * coord->J()(i, j + 1, k)); + common_factor / (coord->dy(i, j + 1, k) * coord->J()(i, j + 1, k)); // For left cell boundaries common_factor = @@ -277,9 +277,9 @@ const Field3D Div_par(const Field3D& f_in, const Field3D& v_in, / (sqrt(coord->g_22()(i, j, k)) + sqrt(coord->g_22()(i, j - 1, k))); BoutReal flux_factor_lc = - common_factor / (coord->dy()(i, j, k) * coord->J()(i, j, k)); + common_factor / (coord->dy(i, j, k) * coord->J()(i, j, k)); BoutReal flux_factor_lm = - common_factor / (coord->dy()(i, j - 1, k) * coord->J()(i, j - 1, k)); + common_factor / (coord->dy(i, j - 1, k) * coord->J()(i, j - 1, k)); #endif //////////////////////////////////////////// diff --git a/src/invert/laplace/impls/multigrid/multigrid_laplace.cxx b/src/invert/laplace/impls/multigrid/multigrid_laplace.cxx index c4e68a40a4..2ce914d38a 100644 --- a/src/invert/laplace/impls/multigrid/multigrid_laplace.cxx +++ b/src/invert/laplace/impls/multigrid/multigrid_laplace.cxx @@ -286,7 +286,7 @@ FieldPerp LaplaceMultigrid::solve(const FieldPerp& b_in, const FieldPerp& x0) { int k2 = k - 1; x[k] = -x0(localmesh->xstart - 1, k2) * sqrt(coords->g_11()(localmesh->xstart, yindex)) - * coords->dx()(localmesh->xstart, yindex); + * coords->dx(localmesh->xstart, yindex); } } else { // zero gradient inner boundary condition @@ -330,7 +330,7 @@ FieldPerp LaplaceMultigrid::solve(const FieldPerp& b_in, const FieldPerp& x0) { int k2 = k - 1; x[(lxx + 1) * lz2 + k] = x0(localmesh->xend + 1, k2) * sqrt(coords->g_11()(localmesh->xend, yindex)) - * coords->dx()(localmesh->xend, yindex); + * coords->dx(localmesh->xend, yindex); // this is the value to set the gradient to at the outer boundary } } else { @@ -489,7 +489,7 @@ FieldPerp LaplaceMultigrid::solve(const FieldPerp& b_in, const FieldPerp& x0) { result(i2, k2) = x[lz2 + k] - x0(localmesh->xstart - 1, k2) * sqrt(coords->g_11()(localmesh->xstart, yindex)) - * coords->dx()(localmesh->xstart, yindex); + * coords->dx(localmesh->xstart, yindex); } } else { // zero gradient inner boundary condition @@ -537,7 +537,7 @@ FieldPerp LaplaceMultigrid::solve(const FieldPerp& b_in, const FieldPerp& x0) { result(i2, k2) = x[lxx * lz2 + k] + x0(localmesh->xend + 1, k2) * sqrt(coords->g_11()(localmesh->xend, yindex)) - * coords->dx()(localmesh->xend, yindex); + * coords->dx(localmesh->xend, yindex); } } else { // zero gradient outer boundary condition @@ -597,21 +597,21 @@ void LaplaceMultigrid::generateMatrixF(int level) { int k2p = (k2 + 1) % Nz_global; int k2m = (k2 + Nz_global - 1) % Nz_global; - BoutReal dz = coords->dz()(i2, yindex); + BoutReal dz = coords->dz(i2, yindex); BoutReal ddx_C = (C2(i2 + 1, yindex, k2) - C2(i2 - 1, yindex, k2)) / 2. - / coords->dx()(i2, yindex) / C1(i2, yindex, k2); + / coords->dx(i2, yindex) / C1(i2, yindex, k2); BoutReal ddz_C = (C2(i2, yindex, k2p) - C2(i2, yindex, k2m)) / 2. / dz / C1(i2, yindex, k2); BoutReal ddx = D(i2, yindex, k2) * coords->g11()(i2, yindex) - / coords->dx()(i2, yindex) / coords->dx()(i2, yindex); + / coords->dx(i2, yindex) / coords->dx(i2, yindex); // coefficient of 2nd derivative stencil (x-direction) BoutReal ddz = D(i2, yindex, k2) * coords->g33()(i2, yindex) / SQ(dz); // coefficient of 2nd derivative stencil (z-direction) BoutReal dxdz = D(i2, yindex, k2) * 2. * coords->g13()(i2, yindex) - / coords->dx()(i2, yindex) / dz; + / coords->dx(i2, yindex) / dz; // coefficient of mixed derivative stencil (could assume zero, at least initially, // if easier; then check this is true in constructor) @@ -621,11 +621,11 @@ void LaplaceMultigrid::generateMatrixF(int level) { + coords->g13()(i2, yindex) * ddz_C // (could assume zero, at least initially, if easier; then check this is true in constructor) ) - / coords->dx()(i2, + / coords->dx(i2, yindex); // coefficient of 1st derivative stencil (x-direction) if (nonuniform) { // add correction for non-uniform dx - dxd += D(i2, yindex, k2) * coords->d1_dx()(i2, yindex); + dxd += D(i2, yindex, k2) * coords->d1_dx(i2, yindex); } BoutReal dzd = diff --git a/src/invert/laplace/impls/petsc/petsc_laplace.cxx b/src/invert/laplace/impls/petsc/petsc_laplace.cxx index 35d2ac5ef2..94714a0a15 100644 --- a/src/invert/laplace/impls/petsc/petsc_laplace.cxx +++ b/src/invert/laplace/impls/petsc/petsc_laplace.cxx @@ -420,33 +420,33 @@ FieldPerp LaplacePetsc::solve(const FieldPerp& b, const FieldPerp& x0) { if (fourth_order) { // Fourth Order Accuracy on Boundary Element(i, x, z, 0, 0, - -25.0 / (12.0 * coords->dx()(x, y, z)) + -25.0 / (12.0 * coords->dx(x, y, z)) / sqrt(coords->g_11()(x, y, z)), MatA); Element(i, x, z, 1, 0, - 4.0 / coords->dx()(x, y, z) / sqrt(coords->g_11()(x, y, z)), MatA); + 4.0 / coords->dx(x, y, z) / sqrt(coords->g_11()(x, y, z)), MatA); Element(i, x, z, 2, 0, - -3.0 / coords->dx()(x, y, z) / sqrt(coords->g_11()(x, y, z)), MatA); + -3.0 / coords->dx(x, y, z) / sqrt(coords->g_11()(x, y, z)), MatA); Element(i, x, z, 3, 0, - 4.0 / (3.0 * coords->dx()(x, y, z)) / sqrt(coords->g_11()(x, y, z)), + 4.0 / (3.0 * coords->dx(x, y, z)) / sqrt(coords->g_11()(x, y, z)), MatA); Element(i, x, z, 4, 0, - -1.0 / (4.0 * coords->dx()(x, y, z)) + -1.0 / (4.0 * coords->dx(x, y, z)) / sqrt(coords->g_11()(x, y, z)), MatA); } else { // Second Order Accuracy on Boundary - // Element(i,x,z, 0, 0, -3.0 / (2.0*coords->dx()(x,y)), MatA ); - // Element(i,x,z, 1, 0, 2.0 / coords->dx()(x,y), MatA ); - // Element(i,x,z, 2, 0, -1.0 / (2.0*coords->dx()(x,y)), MatA ); + // Element(i,x,z, 0, 0, -3.0 / (2.0*coords->dx(x,y)), MatA ); + // Element(i,x,z, 1, 0, 2.0 / coords->dx(x,y), MatA ); + // Element(i,x,z, 2, 0, -1.0 / (2.0*coords->dx(x,y)), MatA ); // Element(i,x,z, 3, 0, 0.0, MatA ); // Reset these elements to 0 // in case 4th order flag was used previously: not allowed now // Element(i,x,z, 4, 0, 0.0, MatA ); // Second Order Accuracy on Boundary, set half-way between grid points Element(i, x, z, 0, 0, - -1.0 / coords->dx()(x, y, z) / sqrt(coords->g_11()(x, y, z)), MatA); + -1.0 / coords->dx(x, y, z) / sqrt(coords->g_11()(x, y, z)), MatA); Element(i, x, z, 1, 0, - 1.0 / coords->dx()(x, y, z) / sqrt(coords->g_11()(x, y, z)), MatA); + 1.0 / coords->dx(x, y, z) / sqrt(coords->g_11()(x, y, z)), MatA); Element(i, x, z, 2, 0, 0.0, MatA); // Element(i,x,z, 3, 0, 0.0, MatA ); // Reset // these elements to 0 in case 4th order flag was @@ -508,9 +508,9 @@ FieldPerp LaplacePetsc::solve(const FieldPerp& b, const FieldPerp& x0) { // Set the matrix coefficients Coeffs(x, y, z, A1, A2, A3, A4, A5); - BoutReal dx = coords->dx()(x, y, z); + BoutReal dx = coords->dx(x, y, z); BoutReal dx2 = SQ(dx); - BoutReal dz = coords->dz()(x, y, z); + BoutReal dz = coords->dz(x, y, z); BoutReal dz2 = SQ(dz); BoutReal dxdz = dx * dz; @@ -687,34 +687,34 @@ FieldPerp LaplacePetsc::solve(const FieldPerp& b, const FieldPerp& x0) { if (fourth_order) { // Fourth Order Accuracy on Boundary Element(i, x, z, 0, 0, - 25.0 / (12.0 * coords->dx()(x, y, z)) + 25.0 / (12.0 * coords->dx(x, y, z)) / sqrt(coords->g_11()(x, y, z)), MatA); Element(i, x, z, -1, 0, - -4.0 / coords->dx()(x, y, z) / sqrt(coords->g_11()(x, y, z)), MatA); + -4.0 / coords->dx(x, y, z) / sqrt(coords->g_11()(x, y, z)), MatA); Element(i, x, z, -2, 0, - 3.0 / coords->dx()(x, y, z) / sqrt(coords->g_11()(x, y, z)), MatA); + 3.0 / coords->dx(x, y, z) / sqrt(coords->g_11()(x, y, z)), MatA); Element(i, x, z, -3, 0, - -4.0 / (3.0 * coords->dx()(x, y, z)) + -4.0 / (3.0 * coords->dx(x, y, z)) / sqrt(coords->g_11()(x, y, z)), MatA); Element(i, x, z, -4, 0, - 1.0 / (4.0 * coords->dx()(x, y, z)) / sqrt(coords->g_11()(x, y, z)), + 1.0 / (4.0 * coords->dx(x, y, z)) / sqrt(coords->g_11()(x, y, z)), MatA); } else { // // Second Order Accuracy on Boundary - // Element(i,x,z, 0, 0, 3.0 / (2.0*coords->dx()(x,y)), MatA ); - // Element(i,x,z, -1, 0, -2.0 / coords->dx()(x,y), MatA ); - // Element(i,x,z, -2, 0, 1.0 / (2.0*coords->dx()(x,y)), MatA ); + // Element(i,x,z, 0, 0, 3.0 / (2.0*coords->dx(x,y)), MatA ); + // Element(i,x,z, -1, 0, -2.0 / coords->dx(x,y), MatA ); + // Element(i,x,z, -2, 0, 1.0 / (2.0*coords->dx(x,y)), MatA ); // Element(i,x,z, -3, 0, 0.0, MatA ); // Reset these elements to 0 // in case 4th order flag was used previously: not allowed now // Element(i,x,z, -4, 0, 0.0, MatA ); // Second Order Accuracy on Boundary, set half-way between grid // points Element(i, x, z, 0, 0, - 1.0 / coords->dx()(x, y, z) / sqrt(coords->g_11()(x, y, z)), MatA); + 1.0 / coords->dx(x, y, z) / sqrt(coords->g_11()(x, y, z)), MatA); Element(i, x, z, -1, 0, - -1.0 / coords->dx()(x, y, z) / sqrt(coords->g_11()(x, y, z)), MatA); + -1.0 / coords->dx(x, y, z) / sqrt(coords->g_11()(x, y, z)), MatA); Element(i, x, z, -2, 0, 0.0, MatA); // Element(i,x,z, -3, 0, 0.0, MatA ); // Reset these elements to 0 // in case 4th order flag was used previously: not allowed now @@ -1025,8 +1025,8 @@ void LaplacePetsc::Coeffs(int x, int y, int z, BoutReal& coef1, BoutReal& coef2, // non-uniform mesh correction if ((x != 0) && (x != (localmesh->LocalNx - 1))) { coef4 -= 0.5 - * ((coords->dx()(x + 1, y, z) - coords->dx()(x - 1, y, z)) - / SQ(coords->dx()(x, y, z))) + * ((coords->dx(x + 1, y, z) - coords->dx(x - 1, y, z)) + / SQ(coords->dx(x, y, z))) * coef1; // BOUT-06 term } } @@ -1074,17 +1074,17 @@ void LaplacePetsc::Coeffs(int x, int y, int z, BoutReal& coef1, BoutReal& coef2, // Fourth order discretization of C in x ddx_C = (-C2(x + 2, y, z) + 8. * C2(x + 1, y, z) - 8. * C2(x - 1, y, z) + C2(x - 2, y, z)) - / (12. * coords->dx()(x, y, z) * (C1(x, y, z))); + / (12. * coords->dx(x, y, z) * (C1(x, y, z))); // Fourth order discretization of C in z ddz_C = (-C2(x, y, zpp) + 8. * C2(x, y, zp) - 8. * C2(x, y, zm) + C2(x, y, zmm)) - / (12. * coords->dz()(x, y, z) * (C1(x, y, z))); + / (12. * coords->dz(x, y, z) * (C1(x, y, z))); } else { // Second order discretization of C in x ddx_C = (C2(x + 1, y, z) - C2(x - 1, y, z)) - / (2. * coords->dx()(x, y, z) * (C1(x, y, z))); + / (2. * coords->dx(x, y, z) * (C1(x, y, z))); // Second order discretization of C in z ddz_C = - (C2(x, y, zp) - C2(x, y, zm)) / (2. * coords->dz()(x, y, z) * (C1(x, y, z))); + (C2(x, y, zp) - C2(x, y, zm)) / (2. * coords->dz(x, y, z) * (C1(x, y, z))); } coef4 += coords->g11()(x, y, z) * ddx_C + coords->g13()(x, y, z) * ddz_C; diff --git a/src/invert/laplace/impls/serial_band/serial_band.cxx b/src/invert/laplace/impls/serial_band/serial_band.cxx index f292667e1c..beb034f281 100644 --- a/src/invert/laplace/impls/serial_band/serial_band.cxx +++ b/src/invert/laplace/impls/serial_band/serial_band.cxx @@ -179,8 +179,8 @@ FieldPerp LaplaceSerialBand::solve(const FieldPerp& b, const FieldPerp& x0) { // non-uniform localmesh correction if ((ix != 0) && (ix != ncx)) { coef4 += coords->g11()(ix, jy) - * ((1.0 / coords->dx()(ix + 1, jy)) - (1.0 / coords->dx()(ix - 1, jy))) - / (2.0 * coords->dx()(ix, jy)); + * ((1.0 / coords->dx(ix + 1, jy)) - (1.0 / coords->dx(ix - 1, jy))) + / (2.0 * coords->dx(ix, jy)); } } @@ -190,14 +190,14 @@ FieldPerp LaplaceSerialBand::solve(const FieldPerp& b, const FieldPerp& x0) { coef4 += coords->g11()(ix, jy) * (Ccoef(ix - 2, jy) - 8. * Ccoef(ix - 1, jy) + 8. * Ccoef(ix + 1, jy) - Ccoef(ix + 2, jy)) - / (12. * coords->dx()(ix, jy) * (Ccoef(ix, jy))); + / (12. * coords->dx(ix, jy) * (Ccoef(ix, jy))); } // Put into matrix - coef1 /= 12. * SQ(coords->dx()(ix, jy)); + coef1 /= 12. * SQ(coords->dx(ix, jy)); coef2 *= SQ(kwave); - coef3 *= kwave / (12. * coords->dx()(ix, jy)); - coef4 /= 12. * coords->dx()(ix, jy); + coef3 *= kwave / (12. * coords->dx(ix, jy)); + coef4 /= 12. * coords->dx(ix, jy); coef5 *= kwave; A(ix, 0) = dcomplex(-coef1 + coef4, coef3); @@ -214,9 +214,9 @@ FieldPerp LaplaceSerialBand::solve(const FieldPerp& b, const FieldPerp& x0) { int ix = 1; auto kwave = kwave_(ix, jy); - coef1 = coords->g11()(ix, jy) / (SQ(coords->dx()(ix, jy))); + coef1 = coords->g11()(ix, jy) / (SQ(coords->dx(ix, jy))); coef2 = coords->g33()(ix, jy); - coef3 = kwave * coords->g13()(ix, jy) / (2. * coords->dx()(ix, jy)); + coef3 = kwave * coords->g13()(ix, jy) / (2. * coords->dx(ix, jy)); // Multiply Delp2 component by a factor coef1 *= Dcoef(ix, jy); @@ -231,9 +231,9 @@ FieldPerp LaplaceSerialBand::solve(const FieldPerp& b, const FieldPerp& x0) { ix = ncx - 1; - coef1 = coords->g11()(ix, jy) / (SQ(coords->dx()(ix, jy))); + coef1 = coords->g11()(ix, jy) / (SQ(coords->dx(ix, jy))); coef2 = coords->g33()(ix, jy); - coef3 = kwave * coords->g13()(ix, jy) / (2. * coords->dx()(ix, jy)); + coef3 = kwave * coords->g13()(ix, jy) / (2. * coords->dx(ix, jy)); A(ix, 0) = 0.0; A(ix, 1) = dcomplex(coef1, -coef3); @@ -272,8 +272,8 @@ FieldPerp LaplaceSerialBand::solve(const FieldPerp& b, const FieldPerp& x0) { for (int ix = 0; ix < xbndry; ix++) { A(ix, 0) = 0.; A(ix, 1) = 0.; - A(ix, 2) = -.5 / sqrt(coords->g_11()(ix, jy)) / coords->dx()(ix, jy); - A(ix, 3) = .5 / sqrt(coords->g_11()(ix, jy)) / coords->dx()(ix, jy); + A(ix, 2) = -.5 / sqrt(coords->g_11()(ix, jy)) / coords->dx(ix, jy); + A(ix, 3) = .5 / sqrt(coords->g_11()(ix, jy)) / coords->dx(ix, jy); A(ix, 4) = 0.; } @@ -337,18 +337,18 @@ FieldPerp LaplaceSerialBand::solve(const FieldPerp& b, const FieldPerp& x0) { int ix = 1; auto kwave = kwave_(ix, jy); - coef1 = coords->g11()(ix, jy) / (12. * SQ(coords->dx()(ix, jy))); + coef1 = coords->g11()(ix, jy) / (12. * SQ(coords->dx(ix, jy))); coef2 = coords->g33()(ix, jy); - coef3 = kwave * coords->g13()(ix, jy) / (2. * coords->dx()(ix, jy)); + coef3 = kwave * coords->g13()(ix, jy) / (2. * coords->dx(ix, jy)); coef4 = Acoef(ix, jy); // Combine 4th order at 1 with 2nd order at 0 A(1, 0) = 0.0; // Not used A(1, 1) = dcomplex((14. - - SQ(coords->dx()(0, jy) * kwave) * coords->g33()(0, jy) + - SQ(coords->dx(0, jy) * kwave) * coords->g33()(0, jy) / coords->g11()(0, jy)) * coef1, -coef3); @@ -356,9 +356,9 @@ FieldPerp LaplaceSerialBand::solve(const FieldPerp& b, const FieldPerp& x0) { A(1, 3) = dcomplex(16. * coef1, coef3); A(1, 4) = dcomplex(-coef1, 0.0); - coef1 = coords->g11()(ix, jy) / (SQ(coords->dx()(ix, jy))); + coef1 = coords->g11()(ix, jy) / (SQ(coords->dx(ix, jy))); coef2 = coords->g33()(ix, jy); - coef3 = kwave * coords->g13()(ix, jy) / (2. * coords->dx()(ix, jy)); + coef3 = kwave * coords->g13()(ix, jy) / (2. * coords->dx(ix, jy)); // Use 2nd order at 1 A(0, 0) = 0.0; // Should never be used @@ -381,12 +381,12 @@ FieldPerp LaplaceSerialBand::solve(const FieldPerp& b, const FieldPerp& x0) { int ix = ncx - 1; - coef1 = coords->g11()(ix, jy) / (12. * SQ(coords->dx()(ix, jy))); + coef1 = coords->g11()(ix, jy) / (12. * SQ(coords->dx(ix, jy))); coef2 = coords->g33()(ix, jy); auto kwave = kwave_(ix, jy); - coef3 = kwave * coords->g13()(ix, jy) / (2. * coords->dx()(ix, jy)); + coef3 = kwave * coords->g13()(ix, jy) / (2. * coords->dx(ix, jy)); coef4 = Acoef(ix, jy); @@ -395,15 +395,15 @@ FieldPerp LaplaceSerialBand::solve(const FieldPerp& b, const FieldPerp& x0) { A(ix, 1) = dcomplex(16. * coef1, -coef3); A(ix, 2) = dcomplex(-29. * coef1 - SQ(kwave) * coef2 + coef4, 0.0); A(ix, 3) = dcomplex((14. - - SQ(coords->dx()(ncx, jy) * kwave) * coords->g33()(ncx, jy) + - SQ(coords->dx(ncx, jy) * kwave) * coords->g33()(ncx, jy) / coords->g11()(ncx, jy)) * coef1, coef3); A(ix, 4) = 0.0; // Not used - coef1 = coords->g11()(ix, jy) / (SQ(coords->dx()(ix, jy))); + coef1 = coords->g11()(ix, jy) / (SQ(coords->dx(ix, jy))); coef2 = coords->g33()(ix, jy); - coef3 = kwave * coords->g13()(ix, jy) / (2. * coords->dx()(ix, jy)); + coef3 = kwave * coords->g13()(ix, jy) / (2. * coords->dx(ix, jy)); // Use 2nd order at ncx - 1 A(ncx, 0) = dcomplex(coef1, -coef3); diff --git a/src/invert/laplace/invert_laplace.cxx b/src/invert/laplace/invert_laplace.cxx index 98ee41922f..57594953a7 100644 --- a/src/invert/laplace/invert_laplace.cxx +++ b/src/invert/laplace/invert_laplace.cxx @@ -349,8 +349,8 @@ void Laplacian::tridagCoefs(int jx, int jy, BoutReal kwave, dcomplex& a, dcomple // non-uniform mesh correction if ((jx != 0) && (jx != (localmesh->LocalNx - 1))) { coef4 -= 0.5 - * ((localcoords->dx()(jx + 1, jy) - localcoords->dx()(jx - 1, jy)) - / SQ(localcoords->dx()(jx, jy))) + * ((localcoords->dx(jx + 1, jy) - localcoords->dx(jx - 1, jy)) + / SQ(localcoords->dx(jx, jy))) * coef1; } } @@ -359,7 +359,7 @@ void Laplacian::tridagCoefs(int jx, int jy, BoutReal kwave, dcomplex& a, dcomple // First derivative terms if ((jx > 0) && (jx < (localmesh->LocalNx - 1))) { BoutReal dc2dx_over_c1 = ((*c2coef)(jx + 1, jy) - (*c2coef)(jx - 1, jy)) - / (2. * localcoords->dx()(jx, jy) * ((*c1coef)(jx, jy))); + / (2. * localcoords->dx(jx, jy) * ((*c1coef)(jx, jy))); coef4 += localcoords->g11()(jx, jy) * dc2dx_over_c1; coef5 += localcoords->g13()(jx, jy) * dc2dx_over_c1; } @@ -373,9 +373,9 @@ void Laplacian::tridagCoefs(int jx, int jy, BoutReal kwave, dcomplex& a, dcomple coef3 = 0.0; // This cancels out } - coef1 /= SQ(localcoords->dx()(jx, jy)); - coef3 /= 2. * localcoords->dx()(jx, jy); - coef4 /= 2. * localcoords->dx()(jx, jy); + coef1 /= SQ(localcoords->dx(jx, jy)); + coef3 /= 2. * localcoords->dx(jx, jy); + coef4 /= 2. * localcoords->dx(jx, jy); a = dcomplex(coef1 - coef4, -kwave * coef3); b = dcomplex(-2.0 * coef1 - SQ(kwave) * coef2, kwave * coef5); @@ -511,8 +511,8 @@ void Laplacian::tridagMatrix(dcomplex* avec, dcomplex* bvec, dcomplex* cvec, dco // Zero gradient at inner boundary for (int ix = 0; ix < inbndry; ix++) { avec[ix] = 0.; - bvec[ix] = -1. / sqrt(coords->g_11()(ix, jy)) / coords->dx()(ix, jy); - cvec[ix] = 1. / sqrt(coords->g_11()(ix, jy)) / coords->dx()(ix, jy); + bvec[ix] = -1. / sqrt(coords->g_11()(ix, jy)) / coords->dx(ix, jy); + cvec[ix] = 1. / sqrt(coords->g_11()(ix, jy)) / coords->dx(ix, jy); } } else if (inner_boundary_flags & INVERT_DC_GRAD) { // Zero gradient at inner boundary @@ -546,7 +546,7 @@ void Laplacian::tridagMatrix(dcomplex* avec, dcomplex* bvec, dcomplex* cvec, dco for (int ix = 0; ix < inbndry; ix++) { avec[ix] = 0.; bvec[ix] = 1.; - cvec[ix] = -exp(-k * coords->dx()(ix, jy) / sqrt(coords->g11()(ix, jy))); + cvec[ix] = -exp(-k * coords->dx(ix, jy) / sqrt(coords->g11()(ix, jy))); } } else if (inner_boundary_flags & INVERT_IN_CYLINDER) { // Condition for inner radial boundary for cylindrical coordinates @@ -608,9 +608,9 @@ void Laplacian::tridagMatrix(dcomplex* avec, dcomplex* bvec, dcomplex* cvec, dco for (int ix = 0; ix < inbndry; ix++) { avec[ix] = dcomplex(0., 0.); bvec[ix] = - dcomplex(-1., 0.) / sqrt(coords->g_11()(ix, jy)) / coords->dx()(ix, jy); + dcomplex(-1., 0.) / sqrt(coords->g_11()(ix, jy)) / coords->dx(ix, jy); cvec[ix] = - dcomplex(1., 0.) / sqrt(coords->g_11()(ix, jy)) / coords->dx()(ix, jy); + dcomplex(1., 0.) / sqrt(coords->g_11()(ix, jy)) / coords->dx(ix, jy); } } else if (inner_boundary_flags & INVERT_AC_GRAD) { // Zero gradient at inner boundary @@ -625,7 +625,7 @@ void Laplacian::tridagMatrix(dcomplex* avec, dcomplex* bvec, dcomplex* cvec, dco avec[ix] = 0.0; bvec[ix] = 1.0; cvec[ix] = -exp(-1.0 * sqrt(coords->g33()(ix, jy) / coords->g11()(ix, jy)) - * kwave * coords->dx()(ix, jy)); + * kwave * coords->dx(ix, jy)); } } else if (inner_boundary_flags & INVERT_IN_CYLINDER) { // Condition for inner radial boundary for cylindrical coordinates @@ -670,9 +670,9 @@ void Laplacian::tridagMatrix(dcomplex* avec, dcomplex* bvec, dcomplex* cvec, dco // Zero gradient at outer boundary for (int ix = 0; ix < outbndry; ix++) { avec[ncx - ix] = dcomplex(-1., 0.) / sqrt(coords->g_11()(xe - ix, jy)) - / coords->dx()(xe - ix, jy); + / coords->dx(xe - ix, jy); bvec[ncx - ix] = dcomplex(1., 0.) / sqrt(coords->g_11()(xe - ix, jy)) - / coords->dx()(xe - ix, jy); + / coords->dx(xe - ix, jy); cvec[ncx - ix] = dcomplex(0., 0.); } } else if (outer_boundary_flags & INVERT_DC_GRAD) { @@ -708,7 +708,7 @@ void Laplacian::tridagMatrix(dcomplex* avec, dcomplex* bvec, dcomplex* cvec, dco cvec[ncx - ix] = 0.; bvec[ncx - ix] = 1.; avec[ncx - ix] = - -exp(-k * coords->dx()(xe - ix, jy) / sqrt(coords->g11()(xe - ix, jy))); + -exp(-k * coords->dx(xe - ix, jy) / sqrt(coords->g11()(xe - ix, jy))); } } else { // Order 2 dirichlet BC (boundary half between points) @@ -728,9 +728,9 @@ void Laplacian::tridagMatrix(dcomplex* avec, dcomplex* bvec, dcomplex* cvec, dco // Zero gradient at outer boundary for (int ix = 0; ix < outbndry; ix++) { avec[ncx - ix] = dcomplex(-1., 0.) / sqrt(coords->g_11()(xe - ix, jy)) - / coords->dx()(xe - ix, jy); + / coords->dx(xe - ix, jy); bvec[ncx - ix] = dcomplex(1., 0.) / sqrt(coords->g_11()(xe - ix, jy)) - / coords->dx()(xe - ix, jy); + / coords->dx(xe - ix, jy); cvec[ncx - ix] = dcomplex(0., 0.); } } else if (outer_boundary_flags & INVERT_AC_GRAD) { @@ -745,7 +745,7 @@ void Laplacian::tridagMatrix(dcomplex* avec, dcomplex* bvec, dcomplex* cvec, dco for (int ix = 0; ix < outbndry; ix++) { avec[ncx - ix] = -exp(-1.0 * sqrt(coords->g33()(xe - ix, jy) / coords->g11()(xe - ix, jy)) - * kwave * coords->dx()(xe - ix, jy)); + * kwave * coords->dx(xe - ix, jy)); bvec[ncx - ix] = 1.0; cvec[ncx - ix] = 0.0; } diff --git a/src/invert/laplacexz/impls/cyclic/laplacexz-cyclic.cxx b/src/invert/laplacexz/impls/cyclic/laplacexz-cyclic.cxx index 5f64919d15..3f482a167d 100644 --- a/src/invert/laplacexz/impls/cyclic/laplacexz-cyclic.cxx +++ b/src/invert/laplacexz/impls/cyclic/laplacexz-cyclic.cxx @@ -118,10 +118,10 @@ void LaplaceXZcyclic::setCoefs(const Field2D& A2D, const Field2D& B2D) { // Metrics on x+1/2 boundary BoutReal J = 0.5 * (coord->J()(x, y) + coord->J()(x + 1, y)); BoutReal g11 = 0.5 * (coord->g11()(x, y) + coord->g11()(x + 1, y)); - BoutReal dx = 0.5 * (coord->dx()(x, y) + coord->dx()(x + 1, y)); + BoutReal dx = 0.5 * (coord->dx(x, y) + coord->dx(x + 1, y)); BoutReal A = 0.5 * (A2D(x, y) + A2D(x + 1, y)); - BoutReal val = A * J * g11 / (coord->J()(x, y) * dx * coord->dx()(x, y)); + BoutReal val = A * J * g11 / (coord->J()(x, y) * dx * coord->dx(x, y)); ccoef(ind, x - xstart) += val; bcoef(ind, x - xstart) -= val; @@ -129,10 +129,10 @@ void LaplaceXZcyclic::setCoefs(const Field2D& A2D, const Field2D& B2D) { // Metrics on x-1/2 boundary J = 0.5 * (coord->J()(x, y) + coord->J()(x - 1, y)); g11 = 0.5 * (coord->g11()(x, y) + coord->g11()(x - 1, y)); - dx = 0.5 * (coord->dx()(x, y) + coord->dx()(x - 1, y)); + dx = 0.5 * (coord->dx(x, y) + coord->dx(x - 1, y)); A = 0.5 * (A2D(x, y) + A2D(x - 1, y)); - val = A * J * g11 / (coord->J()(x, y) * dx * coord->dx()(x, y)); + val = A * J * g11 / (coord->J()(x, y) * dx * coord->dx(x, y)); acoef(ind, x - xstart) += val; bcoef(ind, x - xstart) -= val; diff --git a/src/invert/laplacexz/impls/petsc/laplacexz-petsc.cxx b/src/invert/laplacexz/impls/petsc/laplacexz-petsc.cxx index c4f7016e04..b79154e4ce 100644 --- a/src/invert/laplacexz/impls/petsc/laplacexz-petsc.cxx +++ b/src/invert/laplacexz/impls/petsc/laplacexz-petsc.cxx @@ -405,11 +405,11 @@ void LaplaceXZpetsc::setCoefs(const Field3D& Ain, const Field3D& Bin) { const BoutReal J = 0.5 * (coords->J()(x, y, z) + coords->J()(x + 1, y, z)); const BoutReal g11 = 0.5 * (coords->g11()(x, y, z) + coords->g11()(x + 1, y, z)); - const BoutReal dx = 0.5 * (coords->dx()(x, y, z) + coords->dx()(x + 1, y, z)); + const BoutReal dx = 0.5 * (coords->dx(x, y, z) + coords->dx(x + 1, y, z)); const BoutReal Acoef = 0.5 * (A(x, y, z) + A(x + 1, y, z)); const BoutReal val = - Acoef * J * g11 / (coords->J()(x, y, z) * dx * coords->dx()(x, y, z)); + Acoef * J * g11 / (coords->J()(x, y, z) * dx * coords->dx(x, y, z)); xp = val; c = -val; } @@ -419,11 +419,11 @@ void LaplaceXZpetsc::setCoefs(const Field3D& Ain, const Field3D& Bin) { const BoutReal J = 0.5 * (coords->J()(x, y, z) + coords->J()(x - 1, y, z)); const BoutReal g11 = 0.5 * (coords->g11()(x, y, z) + coords->g11()(x - 1, y, z)); - const BoutReal dx = 0.5 * (coords->dx()(x, y, z) + coords->dx()(x - 1, y, z)); + const BoutReal dx = 0.5 * (coords->dx(x, y, z) + coords->dx(x - 1, y, z)); const BoutReal Acoef = 0.5 * (A(x, y, z) + A(x - 1, y, z)); const BoutReal val = - Acoef * J * g11 / (coords->J()(x, y, z) * dx * coords->dx()(x, y, z)); + Acoef * J * g11 / (coords->J()(x, y, z) * dx * coords->dx(x, y, z)); xm = val; c -= val; } @@ -437,7 +437,7 @@ void LaplaceXZpetsc::setCoefs(const Field3D& Ain, const Field3D& Bin) { const BoutReal J = 0.5 * (coords->J()(x, y, z) + coords->J()(x, y, zplus)); const BoutReal g33 = 0.5 * (coords->g33()(x, y, z) + coords->g33()(x, y, zplus)); - const BoutReal dz = 0.5 * (coords->dz()(x, y, z) + coords->dz()(x, y, zplus)); + const BoutReal dz = 0.5 * (coords->dz(x, y, z) + coords->dz(x, y, zplus)); // Metrics on z+1/2 boundary const BoutReal Acoef = 0.5 * (A(x, y, z) + A(x, y, zplus)); @@ -451,7 +451,7 @@ void LaplaceXZpetsc::setCoefs(const Field3D& Ain, const Field3D& Bin) { const BoutReal g33 = 0.5 * (coords->g33()(x, y, z) + coords->g33()(x, y, zminus)); const BoutReal Acoef = 0.5 * (A(x, y, z) + A(x, y, zminus)); - const BoutReal dz = 0.5 * (coords->dz()(x, y, z) + coords->dz()(x, y, zminus)); + const BoutReal dz = 0.5 * (coords->dz(x, y, z) + coords->dz(x, y, zminus)); const BoutReal val = Acoef * J * g33 / (coords->J()(x, y, z) * dz * dz); zm = val; @@ -477,11 +477,11 @@ void LaplaceXZpetsc::setCoefs(const Field3D& Ain, const Field3D& Bin) { const BoutReal J = 0.5 * (coords->J()(x, y, z) + coords->J()(x + 1, y, z)); const BoutReal g13 = 0.5 * (coords->g13()(x, y, z) + coords->g13()(x + 1, y, z)); - const BoutReal dz = 0.5 * (coords->dz()(x, y, z) + coords->dz()(x + 1, y, z)); + const BoutReal dz = 0.5 * (coords->dz(x, y, z) + coords->dz(x + 1, y, z)); const BoutReal Acoef = 0.5 * (A(x, y, z) + A(x + 1, y, z)); const BoutReal val = - Acoef * J * g13 / (coords->J()(x, y, z) * dz * coords->dx()(x, y, z)); + Acoef * J * g13 / (coords->J()(x, y, z) * dz * coords->dx(x, y, z)); // This val coefficient is multiplied by the (x+1/2,z+1/2) corner // and (x+1/2,z-1/2) corner @@ -514,11 +514,11 @@ void LaplaceXZpetsc::setCoefs(const Field3D& Ain, const Field3D& Bin) { const BoutReal J = 0.5 * (coords->J()(x, y, z) + coords->J()(x - 1, y, z)); const BoutReal g13 = 0.5 * (coords->g13()(x, y, z) + coords->g13()(x - 1, y, z)); - const BoutReal dz = 0.5 * (coords->dz()(x, y, z) + coords->dz()(x - 1, y, z)); + const BoutReal dz = 0.5 * (coords->dz(x, y, z) + coords->dz(x - 1, y, z)); const BoutReal Acoef = 0.5 * (A(x, y, z) + A(x - 1, y, z)); const BoutReal val = - -Acoef * J * g13 / (coords->J()(x, y, z) * dz * coords->dx()(x, y, z)); + -Acoef * J * g13 / (coords->J()(x, y, z) * dz * coords->dx(x, y, z)); // (x+1/2,z+1/2) xpzp += 0.25 * val; @@ -548,11 +548,11 @@ void LaplaceXZpetsc::setCoefs(const Field3D& Ain, const Field3D& Bin) { const BoutReal J = 0.5 * (coords->J()(x, y, z) + coords->J()(x, y, zplus)); const BoutReal g13 = 0.5 * (coords->g13()(x, y, z) + coords->g13()(x, y, zplus)); - const BoutReal dx = 0.5 * (coords->dx()(x, y, z) + coords->dx()(x, y, zplus)); + const BoutReal dx = 0.5 * (coords->dx(x, y, z) + coords->dx(x, y, zplus)); const BoutReal Acoef = 0.5 * (A(x, y, z) + A(x, y, zplus)); const BoutReal val = - Acoef * J * g13 / (coords->J()(x, y, z) * dx * coords->dz()(x, y, z)); + Acoef * J * g13 / (coords->J()(x, y, z) * dx * coords->dz(x, y, z)); // (x+1/2,z+1/2) //zp += 0.25 * val; Note cancels @@ -582,11 +582,11 @@ void LaplaceXZpetsc::setCoefs(const Field3D& Ain, const Field3D& Bin) { const BoutReal J = 0.5 * (coords->J()(x, y, z) + coords->J()(x, y, zminus)); const BoutReal g13 = 0.5 * (coords->g13()(x, y, z) + coords->g13()(x, y, zminus)); - const BoutReal dx = 0.5 * (coords->dx()(x, y, z) + coords->dx()(x, y, zminus)); + const BoutReal dx = 0.5 * (coords->dx(x, y, z) + coords->dx(x, y, zminus)); const BoutReal Acoef = 0.5 * (A(x, y, z) + A(x, y, zminus)); const BoutReal val = - -Acoef * J * g13 / (coords->J()(x, y, z) * dx * coords->dz()(x, y, z)); + -Acoef * J * g13 / (coords->J()(x, y, z) * dx * coords->dz(x, y, z)); // (x+1/2,z-1/2) xpzm += 0.25 * val; diff --git a/src/invert/parderiv/impls/cyclic/cyclic.cxx b/src/invert/parderiv/impls/cyclic/cyclic.cxx index bc3e4a2846..47fc28fd82 100644 --- a/src/invert/parderiv/impls/cyclic/cyclic.cxx +++ b/src/invert/parderiv/impls/cyclic/cyclic.cxx @@ -161,12 +161,12 @@ const Field3D InvertParCR::solve(const Field3D& f) { + sg(x, y + local_ystart) * B(x, y + local_ystart); // ddy if (coord->non_uniform()) { - ecoef += bcoef * coord->d1_dy()(x, y + local_ystart); + ecoef += bcoef * coord->d1_dy(x, y + local_ystart); } - bcoef /= SQ(coord->dy()(x, y + local_ystart)); - ccoef /= coord->dy()(x, y + local_ystart); - ecoef /= coord->dy()(x, y + local_ystart); + bcoef /= SQ(coord->dy(x, y + local_ystart)); + ccoef /= coord->dy(x, y + local_ystart); + ecoef /= coord->dy(x, y + local_ystart); // const d2dy2 d2dydz d2dz2 ddy // ----- ----- ------ ----- --- diff --git a/src/mesh/boundary_standard.cxx b/src/mesh/boundary_standard.cxx index 58f69f5c71..3296ffc995 100644 --- a/src/mesh/boundary_standard.cxx +++ b/src/mesh/boundary_standard.cxx @@ -1618,7 +1618,7 @@ void BoundaryNeumann_NonOrthogonal::apply(Field2D& f) { if (bndry->bx != 0 && bndry->by == 0) { // x boundaries only - BoutReal delta = bndry->bx * metric->dx()(bndry->x, bndry->y); + BoutReal delta = bndry->bx * metric->dx(bndry->x, bndry->y); f(bndry->x, bndry->y) = f(bndry->x - bndry->bx, bndry->y) + delta / g11shift * (val - xshift); if (bndry->bx == 2) { @@ -1628,7 +1628,7 @@ void BoundaryNeumann_NonOrthogonal::apply(Field2D& f) { } else if (bndry->by != 0 && bndry->bx == 0) { // y boundaries only // no need to shift this b/c we want parallel nuemann not theta - BoutReal delta = bndry->by * metric->dy()(bndry->x, bndry->y); + BoutReal delta = bndry->by * metric->dy(bndry->x, bndry->y); f(bndry->x, bndry->y) = f(bndry->x, bndry->y - bndry->by) + delta * val; if (bndry->width == 2) { f(bndry->x, bndry->y + bndry->by) = @@ -1685,7 +1685,7 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { + g13shift * dfdz(bndry->x - bndry->bx, bndry->y, z); if (bndry->bx != 0 && bndry->by == 0) { // x boundaries only - BoutReal delta = bndry->bx * metric->dx()(bndry->x, bndry->y, z); + BoutReal delta = bndry->bx * metric->dx(bndry->x, bndry->y, z); f(bndry->x, bndry->y, z) = f(bndry->x - bndry->bx, bndry->y, z) + delta / g11shift * (val - xshift); if (bndry->width == 2) { @@ -1696,7 +1696,7 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { } else if (bndry->by != 0 && bndry->bx == 0) { // y boundaries only // no need to shift this b/c we want parallel nuemann not theta - BoutReal delta = bndry->by * metric->dy()(bndry->x, bndry->y, z); + BoutReal delta = bndry->by * metric->dy(bndry->x, bndry->y, z); f(bndry->x, bndry->y, z) = f(bndry->x, bndry->y - bndry->by, z) + delta * val; if (bndry->width == 2) { f(bndry->x, bndry->y + bndry->by, z) = @@ -1767,7 +1767,7 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { if (fg) { val = fg->generate(Context(bndry, loc, t, mesh)) - * metric->dx()(bndry->x, bndry->y); + * metric->dx(bndry->x, bndry->y); } f(bndry->x, bndry->y) = (4. * f(bndry->x - bndry->bx, bndry->y) @@ -1793,7 +1793,7 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { if (fg) { val = fg->generate(Context(bndry, loc, t, mesh)) - * metric->dx()(bndry->x, bndry->y); + * metric->dx(bndry->x, bndry->y); } f(bndry->x - bndry->bx, bndry->y) = @@ -1819,8 +1819,8 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { // y boundaries for (bndry->first(); !bndry->isDone(); bndry->next1d()) { - BoutReal delta = bndry->bx * metric->dx()(bndry->x, bndry->y) - + bndry->by * metric->dy()(bndry->x, bndry->y); + BoutReal delta = bndry->bx * metric->dx(bndry->x, bndry->y) + + bndry->by * metric->dy(bndry->x, bndry->y); if (fg) { val = fg->generate(Context(bndry, loc, t, mesh)); @@ -1844,7 +1844,7 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { for (; !bndry->isDone(); bndry->next1d()) { if (fg) { val = fg->generate(Context(bndry, loc, t, mesh)) - * metric->dy()(bndry->x, bndry->y); + * metric->dy(bndry->x, bndry->y); } f(bndry->x, bndry->y) = (4. * f(bndry->x, bndry->y - bndry->by) - f(bndry->x, bndry->y - 2 * bndry->by) + 2. * val) @@ -1870,7 +1870,7 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { if (fg) { val = fg->generate(Context(bndry, loc, t, mesh)) - * metric->dy()(bndry->x, bndry->y - bndry->by); + * metric->dy(bndry->x, bndry->y - bndry->by); } f(bndry->x, bndry->y - bndry->by) = (4. * f(bndry->x, bndry->y - 2 * bndry->by) @@ -1894,8 +1894,8 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { if (bndry->bx != 0) { // x boundaries for (bndry->first(); !bndry->isDone(); bndry->next1d()) { - BoutReal delta = bndry->bx * metric->dx()(bndry->x, bndry->y) - + bndry->by * metric->dy()(bndry->x, bndry->y); + BoutReal delta = bndry->bx * metric->dx(bndry->x, bndry->y) + + bndry->by * metric->dy(bndry->x, bndry->y); if (fg) { val = fg->generate(Context(bndry, loc, t, mesh)); @@ -1915,8 +1915,8 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { // Non-staggered, standard case for (bndry->first(); !bndry->isDone(); bndry->next1d()) { - BoutReal delta = bndry->bx * metric->dx()(bndry->x, bndry->y) - + bndry->by * metric->dy()(bndry->x, bndry->y); + BoutReal delta = bndry->bx * metric->dx(bndry->x, bndry->y) + + bndry->by * metric->dy(bndry->x, bndry->y); if (fg) { val = fg->generate(Context(bndry, loc, t, mesh)); @@ -1970,7 +1970,7 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { for (int zk = 0; zk < mesh->LocalNz; zk++) { if (fg) { val = fg->generate(Context(bndry, zk, loc, t, mesh)) - * metric->dx()(bndry->x, bndry->y, zk); + * metric->dx(bndry->x, bndry->y, zk); } f(bndry->x, bndry->y, zk) = @@ -1999,7 +1999,7 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { for (int zk = 0; zk < mesh->LocalNz; zk++) { if (fg) { val = fg->generate(Context(bndry, zk, loc, t, mesh)) - * metric->dx()(bndry->x - bndry->bx, bndry->y, zk); + * metric->dx(bndry->x - bndry->bx, bndry->y, zk); } f(bndry->x - bndry->bx, bndry->y, zk) = @@ -2025,13 +2025,13 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { if (bndry->by != 0) { for (; !bndry->isDone(); bndry->next1d()) { #if not(BOUT_USE_METRIC_3D) - BoutReal delta = bndry->bx * metric->dx()(bndry->x, bndry->y) - + bndry->by * metric->dy()(bndry->x, bndry->y); + BoutReal delta = bndry->bx * metric->dx(bndry->x, bndry->y) + + bndry->by * metric->dy(bndry->x, bndry->y); #endif for (int zk = 0; zk < mesh->LocalNz; zk++) { #if BOUT_USE_METRIC_3D - BoutReal delta = bndry->bx * metric->dx()(bndry->x, bndry->y, zk) - + bndry->by * metric->dy()(bndry->x, bndry->y, zk); + BoutReal delta = bndry->bx * metric->dx(bndry->x, bndry->y, zk) + + bndry->by * metric->dy(bndry->x, bndry->y, zk); #endif if (fg) { val = fg->generate(Context(bndry, zk, loc, t, mesh)); @@ -2055,7 +2055,7 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { for (int zk = 0; zk < mesh->LocalNz; zk++) { if (fg) { val = fg->generate(Context(bndry, zk, loc, t, mesh)) - * metric->dy()(bndry->x, bndry->y, zk); + * metric->dy(bndry->x, bndry->y, zk); } f(bndry->x, bndry->y, zk) = (4. * f(bndry->x, bndry->y - bndry->by, zk) @@ -2083,7 +2083,7 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { for (int zk = 0; zk < mesh->LocalNz; zk++) { if (fg) { val = fg->generate(Context(bndry, zk, loc, t, mesh)) - * metric->dy()(bndry->x, bndry->y - bndry->by, zk); + * metric->dy(bndry->x, bndry->y - bndry->by, zk); } f(bndry->x, bndry->y - bndry->by, zk) = @@ -2111,13 +2111,13 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { for (; !bndry->isDone(); bndry->next1d()) { #if not(BOUT_USE_METRIC_3D) int zk = 0; - BoutReal delta = bndry->bx * metric->dx()(bndry->x, bndry->y, zk) - + bndry->by * metric->dy()(bndry->x, bndry->y, zk); + BoutReal delta = bndry->bx * metric->dx(bndry->x, bndry->y, zk) + + bndry->by * metric->dy(bndry->x, bndry->y, zk); #endif for (int zk = 0; zk < mesh->LocalNz; zk++) { #if BOUT_USE_METRIC_3D - BoutReal delta = bndry->bx * metric->dx()(bndry->x, bndry->y, zk) - + bndry->by * metric->dy()(bndry->x, bndry->y, zk); + BoutReal delta = bndry->bx * metric->dx(bndry->x, bndry->y, zk) + + bndry->by * metric->dy(bndry->x, bndry->y, zk); #endif if (fg) { val = fg->generate(Context(bndry, zk, loc, t, mesh)); @@ -2146,8 +2146,8 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { + mesh->GlobalY(bndry->y - bndry->by)); // the grid cell for (int zk = 0; zk < mesh->LocalNz; zk++) { - BoutReal delta = bndry->bx * metric->dx()(bndry->x, bndry->y, zk) - + bndry->by * metric->dy()(bndry->x, bndry->y, zk); + BoutReal delta = bndry->bx * metric->dx(bndry->x, bndry->y, zk) + + bndry->by * metric->dy(bndry->x, bndry->y, zk); if (fg) { val = fg->generate(xnorm, TWOPI * ynorm, TWOPI * (zk - 0.5) / (mesh->LocalNz), t); @@ -2168,11 +2168,11 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { for (; !bndry->isDone(); bndry->next1d()) { #if BOUT_USE_METRIC_3D for (int zk = 0; zk < mesh->LocalNz; zk++) { - BoutReal delta = bndry->bx * metric->dx()(bndry->x, bndry->y, zk) - + bndry->by * metric->dy()(bndry->x, bndry->y, zk); + BoutReal delta = bndry->bx * metric->dx(bndry->x, bndry->y, zk) + + bndry->by * metric->dy(bndry->x, bndry->y, zk); #else - BoutReal delta = bndry->bx * metric->dx()(bndry->x, bndry->y) - + bndry->by * metric->dy()(bndry->x, bndry->y); + BoutReal delta = bndry->bx * metric->dx(bndry->x, bndry->y) + + bndry->by * metric->dy(bndry->x, bndry->y); for (int zk = 0; zk < mesh->LocalNz; zk++) { #endif if (fg) { @@ -2250,8 +2250,8 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { Coordinates* coords = f.getCoordinates(); for (bndry->first(); !bndry->isDone(); bndry->next1d()) { - BoutReal delta = bndry->bx * coords->dx()(bndry->x, bndry->y) - + bndry->by * coords->dy()(bndry->x, bndry->y); + BoutReal delta = bndry->bx * coords->dx(bndry->x, bndry->y) + + bndry->by * coords->dy(bndry->x, bndry->y); if (fg) { val = fg->generate(Context(bndry, loc, t, mesh)); @@ -2300,8 +2300,8 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { Coordinates* coords = f.getCoordinates(); for (; !bndry->isDone(); bndry->next1d()) { for (int zk = 0; zk < mesh->LocalNz; zk++) { - BoutReal delta = bndry->bx * coords->dx()(bndry->x, bndry->y, zk) - + bndry->by * coords->dy()(bndry->x, bndry->y, zk); + BoutReal delta = bndry->bx * coords->dx(bndry->x, bndry->y, zk) + + bndry->by * coords->dy(bndry->x, bndry->y, zk); if (fg) { val = fg->generate(Context(bndry, zk, loc, t, mesh)); } @@ -2361,8 +2361,8 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { // grid cell to be val This sets the value of the co-ordinate derivative, i.e. DDX/DDY // not Grad_par/Grad_perp.x for (bndry->first(); !bndry->isDone(); bndry->next1d()) { - BoutReal delta = -(bndry->bx * metric->dx()(bndry->x, bndry->y) - + bndry->by * metric->dy()(bndry->x, bndry->y)); + BoutReal delta = -(bndry->bx * metric->dx(bndry->x, bndry->y) + + bndry->by * metric->dy(bndry->x, bndry->y)); f(bndry->x, bndry->y) = 12. * delta / 11. * val + 17. / 22. * f(bndry->x - bndry->bx, bndry->y - bndry->by) @@ -2395,8 +2395,8 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { // not Grad_par/Grad_perp.x for (bndry->first(); !bndry->isDone(); bndry->next1d()) { for (int z = 0; z < mesh->LocalNz; z++) { - BoutReal delta = -(bndry->bx * metric->dx()(bndry->x, bndry->y, z) - + bndry->by * metric->dy()(bndry->x, bndry->y, z)); + BoutReal delta = -(bndry->bx * metric->dx(bndry->x, bndry->y, z) + + bndry->by * metric->dy(bndry->x, bndry->y, z)); f(bndry->x, bndry->y, z) = 12. * delta / 11. * val + 17. / 22. * f(bndry->x - bndry->bx, bndry->y - bndry->by, z) @@ -2610,10 +2610,10 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { for (bndry->first(); !bndry->isDone(); bndry->nextY()) { int x = bndry->x; int y = bndry->y; - BoutReal g = (f(x - bx, y) - f(x - 2 * bx, y)) / metric->dx()(x - bx, y); + BoutReal g = (f(x - bx, y) - f(x - 2 * bx, y)) / metric->dx(x - bx, y); // Loop in X towards edge of domain do { - f(x, y) = f(x - bx, y) + g * metric->dx()(x, y); + f(x, y) = f(x - bx, y) + g * metric->dx(x, y); bndry->nextX(); x = bndry->x; y = bndry->y; @@ -2666,7 +2666,7 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { // kz != 0 solution BoutReal coef = - -1.0 * sqrt(metric->g33()(x, y) / metric->g11()(x, y)) * metric->dx()(x, y); + -1.0 * sqrt(metric->g33()(x, y) / metric->g11()(x, y)) * metric->dx(x, y); for (int jz = 1; jz <= ncz / 2; jz++) { BoutReal kwave = jz * 2.0 * PI / metric->zlength()(x, y); // wavenumber in [rad^-1] @@ -2713,10 +2713,10 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { for (bndry->first(); !bndry->isDone(); bndry->nextY()) { int x = bndry->x; int y = bndry->y; - BoutReal g = (f(x - bx, y) - f(x - 2 * bx, y)) / metric->dx()(x - bx, y); + BoutReal g = (f(x - bx, y) - f(x - 2 * bx, y)) / metric->dx(x - bx, y); // Loop in X towards edge of domain do { - f(x, y) = f(x - bx, y) + g * metric->dx()(x, y); + f(x, y) = f(x - bx, y) + g * metric->dx(x, y); bndry->nextX(); x = bndry->x; y = bndry->y; @@ -2858,7 +2858,7 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { rfft(f(x - bx, y), ncz, c0.begin()); rfft(f(x - 2 * bx, y), ncz, c1.begin()); rfft(f(x - 3 * bx, y), ncz, c2.begin()); - dcomplex k0lin = (c1[0] - c0[0]) / metric->dx()(x - bx, y); // for kz=0 solution + dcomplex k0lin = (c1[0] - c0[0]) / metric->dx(x - bx, y); // for kz=0 solution // Calculate Delp2 on point MXG+1 (and put into c1) for (int jz = 0; jz <= ncz / 2; jz++) { @@ -2876,12 +2876,12 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { // Loop in X towards edge of domain do { // kz = 0 solution - xpos -= metric->dx()(x, y); + xpos -= metric->dx(x, y); c2[0] = c0[0] + k0lin * xpos + 0.5 * c1[0] * xpos * xpos / metric->g11()(x - bx, y); // kz != 0 solution BoutReal coef = -1.0 * sqrt(metric->g33()(x - bx, y) / metric->g11()(x - bx, y)) - * metric->dx()(x - bx, y); + * metric->dx(x - bx, y); for (int jz = 1; jz <= ncz / 2; jz++) { BoutReal kwave = jz * 2.0 * PI / getUniform(metric->zlength()); // wavenumber in [rad^-1] @@ -2949,24 +2949,24 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { // dB_x / dy tmp = (var.x(jx - 1, jy + 1, jz) - var.x(jx - 1, jy - 1, jz)) - / (metric->dy()(jx - 1, jy - 1) + metric->dy()(jx - 1, jy)); + / (metric->dy(jx - 1, jy - 1) + metric->dy(jx - 1, jy)); var.y(jx, jy, jz) = var.y(jx - 2, jy, jz) - + (metric->dx()(jx - 2, jy) + metric->dx()(jx - 1, jy)) * tmp; + + (metric->dx(jx - 2, jy) + metric->dx(jx - 1, jy)) * tmp; if (mesh->xstart == 2) { // 4th order to get last point - var.y(jx + 1, jy, jz) = var.y(jx - 3, jy, jz) + 4. * metric->dx()(jx, jy) * tmp; + var.y(jx + 1, jy, jz) = var.y(jx - 3, jy, jz) + 4. * metric->dx(jx, jy) * tmp; } // dB_z / dx = dB_x / dz tmp = (var.x(jx - 1, jy, jzp) - var.x(jx - 1, jy, jzm)) - / (2. * metric->dz()(jx - 1, jy)); + / (2. * metric->dz(jx - 1, jy)); var.z(jx, jy, jz) = var.z(jx - 2, jy, jz) - + (metric->dx()(jx - 2, jy) + metric->dx()(jx - 1, jy)) * tmp; + + (metric->dx(jx - 2, jy) + metric->dx(jx - 1, jy)) * tmp; if (mesh->xstart == 2) { - var.z(jx + 1, jy, jz) = var.z(jx - 3, jy, jz) + 4. * metric->dx()(jx, jy) * tmp; + var.z(jx + 1, jy, jz) = var.z(jx - 3, jy, jz) + 4. * metric->dx(jx, jy) * tmp; } // d/dx( Jmetric->g11 B_x ) = - d/dx( Jmetric->g12 B_y + Jmetric->g13 B_z) @@ -2978,8 +2978,8 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { * var.y(jx - 2, jy, jz) + metric->J()(jx - 2, jy) * metric->g13()(jx - 2, jy) * var.z(jx - 2, jy, jz)) - / (metric->dx()(jx - 2, jy) - + metric->dx()(jx - 1, + / (metric->dx(jx - 2, jy) + + metric->dx(jx - 1, jy)); // First term (d/dx) using vals calculated above tmp -= (metric->J()(jx - 1, jy + 1) * metric->g12()(jx - 1, jy + 1) @@ -2994,23 +2994,23 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { * var.z(jx - 1, jy + 1, jz) - metric->J()(jx - 1, jy - 1) * metric->g23()(jx - 1, jy - 1) * var.z(jx - 1, jy - 1, jz)) - / (metric->dy()(jx - 1, jy - 1) + metric->dy()(jx - 1, jy)); // second (d/dy) + / (metric->dy(jx - 1, jy - 1) + metric->dy(jx - 1, jy)); // second (d/dy) tmp -= (metric->J()(jx - 1, jy) * metric->g13()(jx - 1, jy) * (var.x(jx - 1, jy, jzp) - var.x(jx - 1, jy, jzm)) + metric->J()(jx - 1, jy) * metric->g23()(jx - 1, jy) * (var.y(jx - 1, jy, jzp) - var.y(jx - 1, jy, jzm)) + metric->J()(jx - 1, jy) * metric->g33()(jx - 1, jy) * (var.z(jx - 1, jy, jzp) - var.z(jx - 1, jy, jzm))) - / (2. * metric->dz()(jx - 1, jy)); + / (2. * metric->dz(jx - 1, jy)); var.x(jx, jy, jz) = (metric->J()(jx - 2, jy) * metric->g11()(jx - 2, jy) * var.x(jx - 2, jy, jz) - + (metric->dx()(jx - 2, jy) + metric->dx()(jx - 1, jy)) * tmp) + + (metric->dx(jx - 2, jy) + metric->dx(jx - 1, jy)) * tmp) / metric->J()(jx, jy) * metric->g11()(jx, jy); if (mesh->xstart == 2) { var.x(jx + 1, jy, jz) = (metric->J()(jx - 3, jy) * metric->g11()(jx - 3, jy) * var.x(jx - 3, jy, jz) - + 4. * metric->dx()(jx, jy) * tmp) + + 4. * metric->dx(jx, jy) * tmp) / metric->J()(jx + 1, jy) * metric->g11()(jx + 1, jy); } } diff --git a/src/mesh/difops.cxx b/src/mesh/difops.cxx index b8fe671640..1317c01ec7 100644 --- a/src/mesh/difops.cxx +++ b/src/mesh/difops.cxx @@ -95,8 +95,8 @@ Field3D Grad_parP(const Field3D& apar, const Field3D& f) { for (int y = 1; y <= mesh->LocalNy - 2; y++) { for (int z = 0; z < ncz; z++) { gys(x, y, z) = (f.yup()(x, y + 1, z) - f.ydown()(x, y - 1, z)) - / (0.5 * metric->dy()(x, y + 1, z) + metric->dy()(x, y, z) - + 0.5 * metric->dy()(x, y - 1, z)); + / (0.5 * metric->dy(x, y + 1, z) + metric->dy(x, y, z) + + 0.5 * metric->dy(x, y - 1, z)); } } } @@ -111,73 +111,73 @@ Field3D Grad_parP(const Field3D& apar, const Field3D& f) { // bx = -DDZ(apar) BoutReal const bx = (apar(x, y, zm) - apar(x, y, zp)) - / (0.5 * metric->dz()(x, y, zm) + metric->dz()(x, y, z) - + 0.5 * metric->dz()(x, y, zp)); + / (0.5 * metric->dz(x, y, zm) + metric->dz(x, y, z) + + 0.5 * metric->dz(x, y, zp)); // bz = DDX(f) BoutReal const bz = (apar(x + 1, y, z) - apar(x - 1, y, z)) - / (0.5 * metric->dx()(x - 1, y, z) + metric->dx()(x, y, z) - + 0.5 * metric->dx()(x + 1, y, z)); + / (0.5 * metric->dx(x - 1, y, z) + metric->dx(x, y, z) + + 0.5 * metric->dx(x + 1, y, z)); // Now calculate (bx*d/dx + by*d/dy + bz*d/dz) f // Length dl for predictor - BoutReal dl = fabs(metric->dx()(x, y, z)) / (fabs(bx) + 1e-16); - dl = BOUTMIN(dl, fabs(metric->dy()(x, y, z)) / (fabs(by) + 1e-16)); - dl = BOUTMIN(dl, fabs(metric->dz()(x, y, z)) / (fabs(bz) + 1e-16)); + BoutReal dl = fabs(metric->dx(x, y, z)) / (fabs(bx) + 1e-16); + dl = BOUTMIN(dl, fabs(metric->dy(x, y, z)) / (fabs(by) + 1e-16)); + dl = BOUTMIN(dl, fabs(metric->dz(x, y, z)) / (fabs(bz) + 1e-16)); BoutReal fp, fm; // X differencing fp = f(x + 1, y, z) - + (0.25 * dl / metric->dz()(x, y, z)) * bz + + (0.25 * dl / metric->dz(x, y, z)) * bz * (f(x + 1, y, zm) - f(x + 1, y, zp)) - 0.5 * dl * by * gys(x + 1, y, z); fm = f(x - 1, y, z) - + (0.25 * dl / metric->dz()(x, y, z)) * bz + + (0.25 * dl / metric->dz(x, y, z)) * bz * (f(x - 1, y, zm) - f(x - 1, y, zp)) - 0.5 * dl * by * gys(x - 1, y, z); result(x, y, z) = bx * (fp - fm) - / (0.5 * metric->dx()(x - 1, y, z) + metric->dx()(x, y, z) - + 0.5 * metric->dx()(x + 1, y, z)); + / (0.5 * metric->dx(x - 1, y, z) + metric->dx(x, y, z) + + 0.5 * metric->dx(x + 1, y, z)); // Z differencing fp = f(x, y, zp) - + (0.25 * dl / metric->dx()(x, y, z)) * bx + + (0.25 * dl / metric->dx(x, y, z)) * bx * (f(x - 1, y, zp) - f(x + 1, y, zp)) - 0.5 * dl * by * gys(x, y, zp); fm = f(x, y, zm) - + (0.25 * dl / metric->dx()(x, y, z)) * bx + + (0.25 * dl / metric->dx(x, y, z)) * bx * (f(x - 1, y, zm) - f(x + 1, y, zm)) - 0.5 * dl * by * gys(x, y, zm); result(x, y, z) += bz * (fp - fm) - / (0.5 * metric->dz()(x, y, zm) + metric->dz()(x, y, z) - + 0.5 * metric->dz()(x, y, zp)); + / (0.5 * metric->dz(x, y, zm) + metric->dz(x, y, z) + + 0.5 * metric->dz(x, y, zp)); // Y differencing fp = f.yup()(x, y + 1, z) - 0.5 * dl * bx * (f.yup()(x + 1, y + 1, z) - f.yup()(x - 1, y + 1, z)) - / (0.5 * metric->dx()(x - 1, y, z) + metric->dx()(x, y, z) - + 0.5 * metric->dx()(x + 1, y, z)) + / (0.5 * metric->dx(x - 1, y, z) + metric->dx(x, y, z) + + 0.5 * metric->dx(x + 1, y, z)) - + (0.25 * dl / metric->dz()(x, y, z)) * bz + + (0.25 * dl / metric->dz(x, y, z)) * bz * (f.yup()(x, y + 1, zm) - f.yup()(x, y + 1, zp)); fm = f.ydown()(x, y - 1, z) - 0.5 * dl * bx * (f.ydown()(x + 1, y - 1, z) - f.ydown()(x - 1, y - 1, z)) - / (0.5 * metric->dx()(x - 1, y, z) + metric->dx()(x, y, z) - + 0.5 * metric->dx()(x + 1, y, z)) - + (0.25 * dl / metric->dz()(x, y, z)) * bz + / (0.5 * metric->dx(x - 1, y, z) + metric->dx(x, y, z) + + 0.5 * metric->dx(x + 1, y, z)) + + (0.25 * dl / metric->dz(x, y, z)) * bz * (f.ydown()(x, y - 1, zm) - f.ydown()(x, y - 1, zp)); result(x, y, z) += by * (fp - fm) - / (0.5 * metric->dy()(x, y - 1, z) + metric->dy()(x, y, z) - + 0.5 * metric->dy()(x, y + 1, z)); + / (0.5 * metric->dy(x, y - 1, z) + metric->dy(x, y, z) + + 0.5 * metric->dy(x, y + 1, z)); } } } @@ -268,7 +268,7 @@ Field3D Div_par(const Field3D& f, const Field3D& v) { / (sqrt(coord->g_22()(i, j, k)) + sqrt(coord->g_22()(i, j - 1, k))); result(i, j, k) = - (fluxRight - fluxLeft) / (coord->dy()(i, j, k) * coord->J()(i, j, k)); + (fluxRight - fluxLeft) / (coord->dy(i, j, k) * coord->J()(i, j, k)); } } } @@ -672,10 +672,10 @@ Field3D bracket(const Field3D& f, const Field2D& g, BRACKET_METHOD method, BoutReal gp, gm; // Vx = DDZ(f) - BoutReal const vx = (f(x, y, zp) - f(x, y, zm)) / (2. * metric->dz()(x, y, z)); + BoutReal const vx = (f(x, y, zp) - f(x, y, zm)) / (2. * metric->dz(x, y, z)); // Set stability condition - solver->setMaxTimestep(metric->dx()(x, y, z) / (fabs(vx) + 1e-16)); + solver->setMaxTimestep(metric->dx(x, y, z) / (fabs(vx) + 1e-16)); // X differencing if (vx > 0.0) { @@ -689,7 +689,7 @@ Field3D bracket(const Field3D& f, const Field2D& g, BRACKET_METHOD method, gm = g(x, y); } - result(x, y, z) = vx * (gp - gm) / metric->dx()(x, y, z); + result(x, y, z) = vx * (gp - gm) / metric->dx(x, y, z); } } } @@ -777,8 +777,8 @@ Field3D bracket(const Field3D& f, const Field2D& g, BRACKET_METHOD method, BOUT_OMP_PERF(parallel for) for (int jx = mesh->xstart; jx <= mesh->xend; jx++) { for (int jy = mesh->ystart; jy <= mesh->yend; jy++) { - const BoutReal partialFactor = 1.0 / (12 * metric->dz()(jx, jy)); - const BoutReal spacingFactor = partialFactor / metric->dx()(jx, jy); + const BoutReal partialFactor = 1.0 / (12 * metric->dz(jx, jy)); + const BoutReal spacingFactor = partialFactor / metric->dx(jx, jy); for (int jz = 0; jz < mesh->LocalNz; jz++) { const int jzp = jz + 1 < ncz ? jz + 1 : 0; // Above is alternative to const int jzp = (jz + 1) % ncz; @@ -910,15 +910,15 @@ Field3D bracket(const Field3D& f, const Field3D& g, BRACKET_METHOD method, int const zp = (z + 1) % ncz; // Vx = DDZ(f) - vx(x, z) = (f(x, y, zp) - f(x, y, zm)) / (2. * metric->dz()(x, y, z)); + vx(x, z) = (f(x, y, zp) - f(x, y, zm)) / (2. * metric->dz(x, y, z)); // Vz = -DDX(f) vz(x, z) = (f(x - 1, y, z) - f(x + 1, y, z)) - / (0.5 * metric->dx()(x - 1, y) + metric->dx()(x, y) - + 0.5 * metric->dx()(x + 1, y)); + / (0.5 * metric->dx(x - 1, y) + metric->dx(x, y) + + 0.5 * metric->dx(x + 1, y)); // Set stability condition - solver->setMaxTimestep(fabs(metric->dx()(x, y)) / (fabs(vx(x, z)) + 1e-16)); - solver->setMaxTimestep(metric->dz()(x, y) / (fabs(vz(x, z)) + 1e-16)); + solver->setMaxTimestep(fabs(metric->dx(x, y)) / (fabs(vx(x, z)) + 1e-16)); + solver->setMaxTimestep(metric->dz(x, y) / (fabs(vz(x, z)) + 1e-16)); } } @@ -934,53 +934,53 @@ Field3D bracket(const Field3D& f, const Field3D& g, BRACKET_METHOD method, // X differencing if (vx(x, z) > 0.0) { gp = g(x, y, z) - + (0.5 * dt / metric->dz()(x, y)) + + (0.5 * dt / metric->dz(x, y)) * ((vz(x, z) > 0) ? vz(x, z) * (g(x, y, zm) - g(x, y, z)) : vz(x, z) * (g(x, y, z) - g(x, y, zp))); gm = g(x - 1, y, z) - + (0.5 * dt / metric->dz()(x, y)) + + (0.5 * dt / metric->dz(x, y)) * ((vz(x, z) > 0) ? vz(x, z) * (g(x - 1, y, zm) - g(x - 1, y, z)) : vz(x, z) * (g(x - 1, y, z) - g(x - 1, y, zp))); } else { gp = g(x + 1, y, z) - + (0.5 * dt / metric->dz()(x, y)) + + (0.5 * dt / metric->dz(x, y)) * ((vz(x, z) > 0) ? vz(x, z) * (g(x + 1, y, zm) - g(x + 1, y, z)) : vz[x][z] * (g(x + 1, y, z) - g(x + 1, y, zp))); gm = g(x, y, z) - + (0.5 * dt / metric->dz()(x, y)) + + (0.5 * dt / metric->dz(x, y)) * ((vz(x, z) > 0) ? vz(x, z) * (g(x, y, zm) - g(x, y, z)) : vz(x, z) * (g(x, y, z) - g(x, y, zp))); } - result(x, y, z) = vx(x, z) * (gp - gm) / metric->dx()(x, y); + result(x, y, z) = vx(x, z) * (gp - gm) / metric->dx(x, y); // Z differencing if (vz(x, z) > 0.0) { gp = g(x, y, z) - + (0.5 * dt / metric->dx()(x, y)) + + (0.5 * dt / metric->dx(x, y)) * ((vx[x][z] > 0) ? vx[x][z] * (g(x - 1, y, z) - g(x, y, z)) : vx[x][z] * (g(x, y, z) - g(x + 1, y, z))); gm = g(x, y, zm) - + (0.5 * dt / metric->dx()(x, y)) + + (0.5 * dt / metric->dx(x, y)) * ((vx(x, z) > 0) ? vx(x, z) * (g(x - 1, y, zm) - g(x, y, zm)) : vx(x, z) * (g(x, y, zm) - g(x + 1, y, zm))); } else { gp = g(x, y, zp) - + (0.5 * dt / metric->dx()(x, y)) + + (0.5 * dt / metric->dx(x, y)) * ((vx(x, z) > 0) ? vx(x, z) * (g(x - 1, y, zp) - g(x, y, zp)) : vx(x, z) * (g(x, y, zp) - g(x + 1, y, zp))); gm = g(x, y, z) - + (0.5 * dt / metric->dx()(x, y)) + + (0.5 * dt / metric->dx(x, y)) * ((vx(x, z) > 0) ? vx(x, z) * (g(x - 1, y, z) - g(x, y, z)) : vx(x, z) * (g(x, y, z) - g(x + 1, y, z))); } - result(x, y, z) += vz(x, z) * (gp - gm) / metric->dz()(x, y); + result(x, y, z) += vz(x, z) * (gp - gm) / metric->dz(x, y); } } } @@ -1018,7 +1018,7 @@ Field3D bracket(const Field3D& f, const Field3D& g, BRACKET_METHOD method, const int jzm = ncz - 1; #if BOUT_USE_METRIC_3D const BoutReal spacingFactor = - 1.0 / (12 * metric->dz()(jx, jy, jz) * metric->dx()(jx, jy, jz)); + 1.0 / (12 * metric->dz(jx, jy, jz) * metric->dx(jx, jy, jz)); #endif // J++ = DDZ(f)*DDX(g) - DDX(f)*DDZ(g) @@ -1041,7 +1041,7 @@ Field3D bracket(const Field3D& f, const Field3D& g, BRACKET_METHOD method, for (int jz = 1; jz < mesh->LocalNz - 1; jz++) { #if BOUT_USE_METRIC_3D const BoutReal spacingFactor = - 1.0 / (12 * metric->dz()(jx, jy, jz) * metric->dx()(jx, jy, jz)); + 1.0 / (12 * metric->dz(jx, jy, jz) * metric->dx(jx, jy, jz)); #endif const int jzp = jz + 1; const int jzm = jz - 1; @@ -1069,7 +1069,7 @@ Field3D bracket(const Field3D& f, const Field3D& g, BRACKET_METHOD method, const int jzm = ncz - 2; #if BOUT_USE_METRIC_3D const BoutReal spacingFactor = - 1.0 / (12 * metric->dz()(jx, jy, jz) * metric->dx()(jx, jy, jz)); + 1.0 / (12 * metric->dz(jx, jy, jz) * metric->dx(jx, jy, jz)); #endif // J++ = DDZ(f)*DDX(g) - DDX(f)*DDZ(g) @@ -1106,7 +1106,7 @@ Field3D bracket(const Field3D& f, const Field3D& g, BRACKET_METHOD method, for (int jy = mesh->ystart; jy <= mesh->yend; jy++) { #if not(BOUT_USE_METRIC_3D) const BoutReal spacingFactor = - 1.0 / (12 * metric->dz()(jx, jy) * metric->dx()(jx, jy)); + 1.0 / (12 * metric->dz(jx, jy) * metric->dx(jx, jy)); #endif const BoutReal* Fxm = f_temp(jx - 1, jy); const BoutReal* Fx = f_temp(jx, jy); @@ -1117,7 +1117,7 @@ Field3D bracket(const Field3D& f, const Field3D& g, BRACKET_METHOD method, for (int jz = 0; jz < mesh->LocalNz; jz++) { #if BOUT_USE_METRIC_3D const BoutReal spacingFactor = - 1.0 / (12 * metric->dz()(jx, jy, jz) * metric->dx()(jx, jy, jz)); + 1.0 / (12 * metric->dz(jx, jy, jz) * metric->dx(jx, jy, jz)); #endif const int jzp = jz + 1 < ncz ? jz + 1 : 0; // Above is alternative to const int jzp = (jz + 1) % ncz; diff --git a/src/mesh/fv_ops.cxx b/src/mesh/fv_ops.cxx index d7b3caddbe..0180dd9c84 100644 --- a/src/mesh/fv_ops.cxx +++ b/src/mesh/fv_ops.cxx @@ -55,11 +55,11 @@ Field3D Div_a_Grad_perp(const Field3D& a, const Field3D& f) { * (coord->J()(i, j, k) * coord->g11()(i, j, k) + coord->J()(i + 1, j, k) * coord->g11()(i + 1, j, k)) * (f(i + 1, j, k) - f(i, j, k)) - / (coord->dx()(i, j, k) + coord->dx()(i + 1, j, k)); + / (coord->dx(i, j, k) + coord->dx(i + 1, j, k)); - result(i, j, k) += fout / (coord->dx()(i, j, k) * coord->J()(i, j, k)); + result(i, j, k) += fout / (coord->dx(i, j, k) * coord->J()(i, j, k)); result(i + 1, j, k) -= - fout / (coord->dx()(i + 1, j, k) * coord->J()(i + 1, j, k)); + fout / (coord->dx(i + 1, j, k) * coord->J()(i + 1, j, k)); } } } @@ -286,7 +286,7 @@ const Field3D D4DY4(const Field3D& d_in, const Field3D& f_in) { for (int j = ystart; j <= yend; j++) { for (int k = 0; k < mesh->LocalNz; k++) { - BoutReal const dy3 = SQ(coord->dz()(i, j, k)) * coord->dz()(i, j, k); + BoutReal const dy3 = SQ(coord->dz(i, j, k)) * coord->dz(i, j, k); // 3rd derivative at upper boundary BoutReal const d3fdy3 = @@ -296,9 +296,9 @@ const Field3D D4DY4(const Field3D& d_in, const Field3D& f_in) { BoutReal const flux = 0.5 * (d(i, j, k) + d(i, j + 1, k)) * (coord->J()(i, j, k) + coord->J()(i, j + 1, k)) * d3fdy3; - result(i, j, k) += flux / (coord->J()(i, j, k) * coord->dz()(i, j, k)); + result(i, j, k) += flux / (coord->J()(i, j, k) * coord->dz(i, j, k)); result(i, j + 1, k) -= - flux / (coord->J()(i, j + 1, k) * coord->dz()(i, j + 1, k)); + flux / (coord->J()(i, j + 1, k) * coord->dz(i, j + 1, k)); } } } @@ -336,13 +336,13 @@ const Field3D D4DY4_Index(const Field3D& f_in, bool bndry_flux) { for (int k = 0; k < mesh->LocalNz; k++) { // Right boundary common factors const BoutReal common_factor = - 0.25 * (coord->dz()(i, j, k) + coord->dz()(i, j + 1, k)) + 0.25 * (coord->dz(i, j, k) + coord->dz(i, j + 1, k)) * (coord->J()(i, j, j) + coord->J()(i, j + 1, k)); const BoutReal factor_rc = - common_factor / (coord->J()(i, j, k) * coord->dz()(i, j, k)); + common_factor / (coord->J()(i, j, k) * coord->dz(i, j, k)); const BoutReal factor_rp = - common_factor / (coord->J()(i, j + 1, k) * coord->dz()(i, j + 1, k)); + common_factor / (coord->J()(i, j + 1, k) * coord->dz(i, j + 1, k)); // Not on domain boundary // 3rd derivative at right cell boundary @@ -360,13 +360,13 @@ const Field3D D4DY4_Index(const Field3D& f_in, bool bndry_flux) { for (int k = 0; k < mesh->LocalNz; k++) { // Right boundary common factors const BoutReal common_factor = - 0.25 * (coord->dz()(i, j, k) + coord->dz()(i, j + 1, k)) + 0.25 * (coord->dz(i, j, k) + coord->dz(i, j + 1, k)) * (coord->J()(i, j, j) + coord->J()(i, j + 1, k)); const BoutReal factor_rc = - common_factor / (coord->J()(i, j, k) * coord->dz()(i, j, k)); + common_factor / (coord->J()(i, j, k) * coord->dz(i, j, k)); const BoutReal factor_rp = - common_factor / (coord->J()(i, j + 1, k) * coord->dz()(i, j + 1, k)); + common_factor / (coord->J()(i, j + 1, k) * coord->dz(i, j + 1, k)); const BoutReal d3fdx3 = -((16. / 5) * 0.5 * (f(i, j + 1, k) + f(i, j, k)) // Boundary value f_b @@ -389,13 +389,13 @@ const Field3D D4DY4_Index(const Field3D& f_in, bool bndry_flux) { if (j != mesh->ystart || !has_lower_boundary) { for (int k = 0; k < mesh->LocalNz; k++) { const BoutReal common_factor = - 0.25 * (coord->dz()(i, j, k) + coord->dz()(i, j + 1, k)) + 0.25 * (coord->dz(i, j, k) + coord->dz(i, j + 1, k)) * (coord->J()(i, j, k) + coord->J()(i, j - 1, k)); const BoutReal factor_lc = - common_factor / (coord->J()(i, j, k) * coord->dz()(i, j, k)); + common_factor / (coord->J()(i, j, k) * coord->dz(i, j, k)); const BoutReal factor_lm = - common_factor / (coord->J()(i, j - 1, k) * coord->dz()(i, j - 1, k)); + common_factor / (coord->J()(i, j - 1, k) * coord->dz(i, j - 1, k)); // Not on a domain boundary const BoutReal d3fdx3 = @@ -408,13 +408,13 @@ const Field3D D4DY4_Index(const Field3D& f_in, bool bndry_flux) { // On a domain (Y) boundary for (int k = 0; k < mesh->LocalNz; k++) { const BoutReal common_factor = - 0.25 * (coord->dz()(i, j, k) + coord->dz()(i, j + 1, k)) + 0.25 * (coord->dz(i, j, k) + coord->dz(i, j + 1, k)) * (coord->J()(i, j, k) + coord->J()(i, j - 1, k)); const BoutReal factor_lc = - common_factor / (coord->J()(i, j, k) * coord->dz()(i, j, k)); + common_factor / (coord->J()(i, j, k) * coord->dz(i, j, k)); const BoutReal factor_lm = - common_factor / (coord->J()(i, j - 1, k) * coord->dz()(i, j - 1, k)); + common_factor / (coord->J()(i, j - 1, k) * coord->dz(i, j - 1, k)); const BoutReal d3fdx3 = -(-(16. / 5) * 0.5 * (f(i, j - 1, k) + f(i, j, k)) // Boundary value f_b + 6. * f(i, j, k) // f_0 @@ -516,50 +516,50 @@ Field3D Div_Perp_Lap(const Field3D& a, const Field3D& f, CELL_LOC outloc) { BoutReal const gR = (coords->g11()(i, j, k) + coords->g11()(i + 1, j, k)) * (f(i + 1, j, k) - f(i, j, k)) - / (coords->dx()(i + 1, j, k) + coords->dx()(i, j, k)) + / (coords->dx(i + 1, j, k) + coords->dx(i, j, k)) + 0.5 * (coords->g13()(i, j, k) + coords->g13()(i + 1, j, k)) * (f(i + 1, j, kp) - f(i + 1, j, km) + f(i, j, kp) - f(i, j, km)) - / (4. * coords->dz()(i, j, k)); + / (4. * coords->dz(i, j, k)); BoutReal const gL = (coords->g11()(i - 1, j, k) + coords->g11()(i, j, k)) * (f(i, j, k) - f(i - 1, j, k)) - / (coords->dx()(i - 1, j, k) + coords->dx()(i, j, k)) + / (coords->dx(i - 1, j, k) + coords->dx(i, j, k)) + 0.5 * (coords->g13()(i - 1, j, k) + coords->g13()(i, j, k)) * (f(i - 1, j, kp) - f(i - 1, j, km) + f(i, j, kp) - f(i, j, km)) - / (4 * coords->dz()(i, j, k)); + / (4 * coords->dz(i, j, k)); BoutReal const gD = coords->g13()(i, j, k) * (f(i + 1, j, km) - f(i - 1, j, km) + f(i + 1, j, k) - f(i - 1, j, k)) - / (4. * coords->dx()(i, j, k)) - + coords->g33()(i, j, k) * (f(i, j, k) - f(i, j, km)) / coords->dz()(i, j, k); + / (4. * coords->dx(i, j, k)) + + coords->g33()(i, j, k) * (f(i, j, k) - f(i, j, km)) / coords->dz(i, j, k); BoutReal const gU = coords->g13()(i, j, k) * (f(i + 1, j, kp) - f(i - 1, j, kp) + f(i + 1, j, k) - f(i - 1, j, k)) - / (4. * coords->dx()(i, j, k)) - + coords->g33()(i, j, k) * (f(i, j, kp) - f(i, j, k)) / coords->dz()(i, j, k); + / (4. * coords->dx(i, j, k)) + + coords->g33()(i, j, k) * (f(i, j, kp) - f(i, j, k)) / coords->dz(i, j, k); // Flow right BoutReal flux = gR * 0.25 * (coords->J()(i + 1, j, k) + coords->J()(i, j, k)) * (a(i + 1, j, k) + a(i, j, k)); - result(i, j, k) += flux / (coords->dx()(i, j, k) * coords->J()(i, j, k)); + result(i, j, k) += flux / (coords->dx(i, j, k) * coords->J()(i, j, k)); // Flow left flux = gL * 0.25 * (coords->J()(i - 1, j, k) + coords->J()(i, j, k)) * (a(i - 1, j, k) + a(i, j, k)); - result(i, j, k) -= flux / (coords->dx()(i, j, k) * coords->J()(i, j, k)); + result(i, j, k) -= flux / (coords->dx(i, j, k) * coords->J()(i, j, k)); // Flow up flux = gU * 0.25 * (coords->J()(i, j, k) + coords->J()(i, j, kp)) * (a(i, j, k) + a(i, j, kp)); - result(i, j, k) += flux / (coords->dz()(i, j, k) * coords->J()(i, j, k)); + result(i, j, k) += flux / (coords->dz(i, j, k) * coords->J()(i, j, k)); // Flow down flux = gD * 0.25 * (coords->J()(i, j, km) + coords->J()(i, j, k)) * (a(i, j, km) + a(i, j, k)); - result(i, j, k) += flux / (coords->dz()(i, j, k) * coords->J()(i, j, k)); + result(i, j, k) += flux / (coords->dz(i, j, k) * coords->J()(i, j, k)); } } } diff --git a/tests/MMS/diffusion2/diffusion.cxx b/tests/MMS/diffusion2/diffusion.cxx index e60dc4bd4b..cb94719c44 100644 --- a/tests/MMS/diffusion2/diffusion.cxx +++ b/tests/MMS/diffusion2/diffusion.cxx @@ -26,7 +26,7 @@ class Diffusion : public PhysicsModel { coords->setDy(Ly / (mesh->GlobalNy - 2 * mesh->ystart)); output.write("SIZES: {:d}, {:d}, {:e}\n", mesh->GlobalNy, - (mesh->GlobalNy - 2 * mesh->ystart), coords->dy()(0, 0, 0)); + (mesh->GlobalNy - 2 * mesh->ystart), coords->dy(0, 0, 0)); SAVE_ONCE2(Lx, Ly); diff --git a/tests/MMS/spatial/diffusion/diffusion.cxx b/tests/MMS/spatial/diffusion/diffusion.cxx index 6e425a38d6..918dcd79cb 100644 --- a/tests/MMS/spatial/diffusion/diffusion.cxx +++ b/tests/MMS/spatial/diffusion/diffusion.cxx @@ -27,7 +27,7 @@ class Diffusion : public PhysicsModel { coords->setDy(Ly / (mesh->GlobalNy - 2 * mesh->ystart)); output.write("SIZES: {:d}, {:d}, {:e}\n", mesh->GlobalNy, - (mesh->GlobalNy - 2 * mesh->ystart), coords->dy()(0, 0, 0)); + (mesh->GlobalNy - 2 * mesh->ystart), coords->dy(0, 0, 0)); SAVE_ONCE2(Lx, Ly) diff --git a/tests/integrated/test-multigrid_laplace/test_multigrid_laplace.cxx b/tests/integrated/test-multigrid_laplace/test_multigrid_laplace.cxx index 2cf2ac7dee..50a3cbfc1a 100644 --- a/tests/integrated/test-multigrid_laplace/test_multigrid_laplace.cxx +++ b/tests/integrated/test-multigrid_laplace/test_multigrid_laplace.cxx @@ -241,7 +241,7 @@ int main(int argc, char** argv) { for (int k = 0; k < mesh->LocalNz; k++) { x0(mesh->xstart - 1, mesh->ystart, k) = (f4(mesh->xstart, mesh->ystart, k) - f4(mesh->xstart - 1, mesh->ystart, k)) - / mesh->getCoordinates()->dx()(mesh->xstart, mesh->ystart, k) + / mesh->getCoordinates()->dx(mesh->xstart, mesh->ystart, k) / sqrt(mesh->getCoordinates()->g_11()(mesh->xstart, mesh->ystart, k)); } } @@ -249,7 +249,7 @@ int main(int argc, char** argv) { for (int k = 0; k < mesh->LocalNz; k++) { x0(mesh->xend + 1, mesh->ystart, k) = (f4(mesh->xend + 1, mesh->ystart, k) - f4(mesh->xend, mesh->ystart, k)) - / mesh->getCoordinates()->dx()(mesh->xend, mesh->ystart, k) + / mesh->getCoordinates()->dx(mesh->xend, mesh->ystart, k) / sqrt(mesh->getCoordinates()->g_11()(mesh->xend, mesh->ystart, k)); } } diff --git a/tests/integrated/test-naulin-laplace/test_naulin_laplace.cxx b/tests/integrated/test-naulin-laplace/test_naulin_laplace.cxx index b4813bcedf..83b589d145 100644 --- a/tests/integrated/test-naulin-laplace/test_naulin_laplace.cxx +++ b/tests/integrated/test-naulin-laplace/test_naulin_laplace.cxx @@ -245,7 +245,7 @@ int main(int argc, char** argv) { for (int k = 0; k < mesh->LocalNz; k++) { x0(mesh->xstart - 1, mesh->ystart, k) = (f4(mesh->xstart, mesh->ystart, k) - f4(mesh->xstart - 1, mesh->ystart, k)) - / mesh->getCoordinates()->dx()(mesh->xstart, mesh->ystart, k) + / mesh->getCoordinates()->dx(mesh->xstart, mesh->ystart, k) / sqrt(mesh->getCoordinates()->g_11()(mesh->xstart, mesh->ystart, k)); } } @@ -253,7 +253,7 @@ int main(int argc, char** argv) { for (int k = 0; k < mesh->LocalNz; k++) { x0(mesh->xend + 1, mesh->ystart, k) = (f4(mesh->xend + 1, mesh->ystart, k) - f4(mesh->xend, mesh->ystart, k)) - / mesh->getCoordinates()->dx()(mesh->xend, mesh->ystart, k) + / mesh->getCoordinates()->dx(mesh->xend, mesh->ystart, k) / sqrt(mesh->getCoordinates()->g_11()(mesh->xend, mesh->ystart, k)); } } diff --git a/tests/unit/mesh/test_coordinates_accessor.cxx b/tests/unit/mesh/test_coordinates_accessor.cxx index 62f3db6104..0e4c594643 100644 --- a/tests/unit/mesh/test_coordinates_accessor.cxx +++ b/tests/unit/mesh/test_coordinates_accessor.cxx @@ -23,7 +23,7 @@ TEST_F(CoordinatesAccessorTest, CreateAccessor) { // Basic sanity checks EXPECT_TRUE(acc.mesh_nz == mesh->LocalNz); EXPECT_TRUE(acc.data != nullptr); - EXPECT_FLOAT_EQ(mesh->getCoordinates()->dx()(0, 0, 0), acc.dx(0)); + EXPECT_FLOAT_EQ(mesh->getCoordinates()->dx(0, 0, 0), acc.dx(0)); } TEST_F(CoordinatesAccessorTest, CreateTwoAccessors) { From dc2b0c3c6c0f22bc8cd6c989b93916d59d776e22 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Mon, 8 Jul 2024 11:44:45 +0100 Subject: [PATCH 458/491] Merge remote-tracking branch 'remotes/boutproject/next' into refactor-coordinates --- bin/bout-build-deps.sh | 14 +- include/bout/invert_laplace.hxx | 38 +- manual/sphinx/user_docs/advanced_install.rst | 15 +- requirements.txt | 10 +- .../laplace/impls/cyclic/cyclic_laplace.cxx | 65 +- .../laplace/impls/hypre3d/hypre3d_laplace.cxx | 12 +- .../iterative_parallel_tri.cxx | 9 +- .../iterative_parallel_tri.hxx | 8 - .../impls/multigrid/multigrid_laplace.cxx | 43 +- .../laplace/impls/naulin/naulin_laplace.cxx | 8 +- src/invert/laplace/impls/pcr/pcr.cxx | 48 +- src/invert/laplace/impls/pcr/pcr.hxx | 8 - .../laplace/impls/pcr_thomas/pcr_thomas.cxx | 48 +- .../laplace/impls/pcr_thomas/pcr_thomas.hxx | 8 - .../laplace/impls/petsc/petsc_laplace.cxx | 91 +- .../laplace/impls/petsc/petsc_laplace.hxx | 9 +- .../laplace/impls/petsc3damg/petsc3damg.cxx | 24 +- .../laplace/impls/serial_band/serial_band.cxx | 36 +- .../laplace/impls/serial_tri/serial_tri.cxx | 17 +- src/invert/laplace/impls/spt/spt.cxx | 49 +- src/invert/laplace/impls/spt/spt.hxx | 17 +- src/invert/laplace/invert_laplace.cxx | 83 +- .../test-petsc_laplace/test_petsc_laplace.cxx | 1277 +++++++---------- 23 files changed, 846 insertions(+), 1091 deletions(-) diff --git a/bin/bout-build-deps.sh b/bin/bout-build-deps.sh index 19e3b2a0d3..d96d500dc9 100755 --- a/bin/bout-build-deps.sh +++ b/bin/bout-build-deps.sh @@ -98,7 +98,7 @@ netcdf() { nccxx() { cd $BUILD - wget -c ftp://ftp.unidata.ucar.edu/pub/netcdf/netcdf-cxx4-$NCCXXVER.tar.gz || : + wget -c https://downloads.unidata.ucar.edu/netcdf-cxx/$NCCXXVER/netcdf-cxx4-$NCCXXVER.tar.gz || : tar -xf netcdf-cxx4-$NCCXXVER.tar.gz cd netcdf-cxx4-$NCCXXVER CPPFLAGS="-I$PREFIX/include" LDFLAGS="-L$PREFIX/lib/" ./configure --prefix=$PREFIX $NCCXXFLAGS @@ -286,17 +286,17 @@ set -x ## Setup folders and links setup ## Build and install hdf5 -hdf5 +test $NO_HDF5 || hdf5 ## Build and install netcdf -netcdf +test $NO_NETCDF || netcdf ## Build and install C++ interface for netcdf -nccxx +test $NO_NCXX || nccxx ## Build and install FFTW -fftw +test $NO_FFTW || fftw ## Build and install Sundials -sundials +test $NO_SUNDIALS || sundials ## Build and install PETSc -petsc +test $NO_PETSC || petsc ## Download BOUT++ submodules submod # Install python packages diff --git a/include/bout/invert_laplace.hxx b/include/bout/invert_laplace.hxx index f7b9501a81..0b416d4aab 100644 --- a/include/bout/invert_laplace.hxx +++ b/include/bout/invert_laplace.hxx @@ -238,6 +238,10 @@ public: virtual void setInnerBoundaryFlags(int f) { inner_boundary_flags = f; } virtual void setOuterBoundaryFlags(int f) { outer_boundary_flags = f; } + virtual int getGlobalFlags() const { return global_flags; } + virtual int getInnerBoundaryFlags() const { return inner_boundary_flags; } + virtual int getOuterBoundaryFlags() const { return outer_boundary_flags; } + /// Does this solver use Field3D coefficients (true) or only their DC component (false) virtual bool uses3DCoefs() const { return false; } @@ -308,9 +312,23 @@ protected: int extra_yguards_lower; ///< exclude some number of points at the lower boundary, useful for staggered grids or when boundary conditions make inversion redundant int extra_yguards_upper; ///< exclude some number of points at the upper boundary, useful for staggered grids or when boundary conditions make inversion redundant - int global_flags; ///< Default flags - int inner_boundary_flags; ///< Flags to set inner boundary condition - int outer_boundary_flags; ///< Flags to set outer boundary condition + /// Return true if global/default \p flag is set + bool isGlobalFlagSet(int flag) const { return (global_flags & flag) != 0; } + /// Return true if \p flag is set for the inner boundary condition + bool isInnerBoundaryFlagSet(int flag) const { + return (inner_boundary_flags & flag) != 0; + } + /// Return true if \p flag is set for the outer boundary condition + bool isOuterBoundaryFlagSet(int flag) const { + return (outer_boundary_flags & flag) != 0; + } + + /// Return true if \p flag is set for the inner boundary condition + /// and this is the first proc in X direction + bool isInnerBoundaryFlagSetOnFirstX(int flag) const; + /// Return true if \p flag is set for the outer boundary condition + /// and this the last proc in X direction + bool isOuterBoundaryFlagSetOnLastX(int flag) const; void tridagCoefs(int jx, int jy, BoutReal kwave, dcomplex& a, dcomplex& b, dcomplex& c, const Field2D* ccoef = nullptr, const Field2D* d = nullptr, @@ -322,15 +340,13 @@ protected: CELL_LOC loc = CELL_DEFAULT); void tridagMatrix(dcomplex* avec, dcomplex* bvec, dcomplex* cvec, dcomplex* bk, int jy, - int kz, BoutReal kwave, int flags, int inner_boundary_flags, - int outer_boundary_flags, const Field2D* a, const Field2D* ccoef, + int kz, BoutReal kwave, const Field2D* a, const Field2D* ccoef, const Field2D* d, bool includeguards = true, bool zperiodic = true) { - tridagMatrix(avec, bvec, cvec, bk, jy, kz, kwave, flags, inner_boundary_flags, - outer_boundary_flags, a, ccoef, ccoef, d, includeguards, zperiodic); + tridagMatrix(avec, bvec, cvec, bk, jy, kz, kwave, a, ccoef, ccoef, d, includeguards, + zperiodic); } void tridagMatrix(dcomplex* avec, dcomplex* bvec, dcomplex* cvec, dcomplex* bk, int jy, - int kz, BoutReal kwave, int flags, int inner_boundary_flags, - int outer_boundary_flags, const Field2D* a, const Field2D* c1coef, + int kz, BoutReal kwave, const Field2D* a, const Field2D* c1coef, const Field2D* c2coef, const Field2D* d, bool includeguards = true, bool zperiodic = true); CELL_LOC location; ///< staggered grid location of this solver @@ -339,6 +355,10 @@ protected: /// localmesh->getCoordinates(location) once private: + int global_flags; ///< Default flags + int inner_boundary_flags; ///< Flags to set inner boundary condition + int outer_boundary_flags; ///< Flags to set outer boundary condition + /// Singleton instance static std::unique_ptr instance; /// Name for writing performance infomation; default taken from diff --git a/manual/sphinx/user_docs/advanced_install.rst b/manual/sphinx/user_docs/advanced_install.rst index 57ac5f8e3f..048a26a6e3 100644 --- a/manual/sphinx/user_docs/advanced_install.rst +++ b/manual/sphinx/user_docs/advanced_install.rst @@ -145,13 +145,12 @@ where ```` is the path to the build directory MPCDF HPC Systems ~~~~~~~~~~~~~~~~~ +After cloning BOUT-dev and checking out the branch you want (e.g. db-outer), run: .. code-block:: bash - module purge # or at least onload intel and impi and mkl - module load gcc/10 cmake/3.18 openmpi/4 - # ensure python3 is >= python3.6 - skip if you have a newer python3 loaded - mkdir -p $HOME/bin ; test -e $HOME/bin/python3 || ln -s $(which python3.6) $HOME/bin/python3 - BUILD=/ptmp/$USER/bout-deps bin/bout-build-deps.sh + module purge # or at least onload intel + module load gcc/13 anaconda/3/2021.11 impi/2021.9 hdf5-serial/1.12.2 mkl/2022.0 netcdf-serial/4.8.1 fftw-mpi/3.3.10 + BUILD=/ptmp/$USER/bout-deps NO_HDF5=1 NO_NETCDF=1 NO_FFTW=1 bin/bout-build-deps.sh and follow the instructions for configuring BOUT++. To enable openMP for a production run use: @@ -159,11 +158,11 @@ for a production run use: .. code-block:: bash module load bout-dep - cmake .. -DBOUT_USE_NETCDF=ON -DnetCDF_ROOT=$BOUT_DEP -DnetCDFCxx_ROOT=$BOUT_DEP \ + cmake .. -DBOUT_USE_NETCDF=ON -DnetCDFCxx_ROOT=$BOUT_DEP \ -DBOUT_USE_PETSC=ON -DPETSC_DIR=$BOUT_DEP \ - -DBOUT_USE_FFTW=ON -DFFTW_ROOT=$BOUT_DEP \ + -DBOUT_USE_FFTW=ON \ -DBOUT_USE_SUNDIALS=ON -DSUNDIALS_ROOT=$BOUT_DEP \ - -DBOUT_ENABLE_OPENMP=ON \ + -DBOUT_ENABLE_OPENMP=OFF \ -DCMAKE_BUILD_TYPE=Release diff --git a/requirements.txt b/requirements.txt index 75358b10db..dcbe5cef5c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,8 +1,8 @@ -Jinja2>=2.11.3 -numpy>=1.14.1 -scipy>=1.0.0 -netcdf4~=1.6.0 -matplotlib>=2.0.0 +Jinja2~=3.1.0 +numpy~=2.0.0 +scipy>=1.11.0 +netcdf4~=1.7.1 +matplotlib>=3.7.0 Cython~=3.0.0 boututils~=0.2.1 boutdata~=0.2.1 diff --git a/src/invert/laplace/impls/cyclic/cyclic_laplace.cxx b/src/invert/laplace/impls/cyclic/cyclic_laplace.cxx index 42918cc6f3..12626a4cdb 100644 --- a/src/invert/laplace/impls/cyclic/cyclic_laplace.cxx +++ b/src/invert/laplace/impls/cyclic/cyclic_laplace.cxx @@ -33,11 +33,13 @@ * */ -#include "cyclic_laplace.hxx" -#include "bout/build_config.hxx" +#include "bout/build_defines.hxx" #if not BOUT_USE_METRIC_3D +#include "cyclic_laplace.hxx" +#include "bout/assert.hxx" +#include "bout/bout_types.hxx" #include #include #include @@ -47,7 +49,7 @@ #include #include -#include "cyclic_laplace.hxx" +#include LaplaceCyclic::LaplaceCyclic(Options* opt, const CELL_LOC loc, Mesh* mesh_in, Solver* UNUSED(solver)) @@ -120,13 +122,13 @@ FieldPerp LaplaceCyclic::solve(const FieldPerp& rhs, const FieldPerp& x0) { // If the flags to assign that only one guard cell should be used is set int inbndry = localmesh->xstart, outbndry = localmesh->xstart; - if (((global_flags & INVERT_BOTH_BNDRY_ONE) != 0) || (localmesh->xstart < 2)) { + if (isGlobalFlagSet(INVERT_BOTH_BNDRY_ONE) || (localmesh->xstart < 2)) { inbndry = outbndry = 1; } - if ((inner_boundary_flags & INVERT_BNDRY_ONE) != 0) { + if (isInnerBoundaryFlagSet(INVERT_BNDRY_ONE)) { inbndry = 1; } - if ((outer_boundary_flags & INVERT_BNDRY_ONE) != 0) { + if (isOuterBoundaryFlagSet(INVERT_BNDRY_ONE)) { outbndry = 1; } @@ -143,9 +145,9 @@ FieldPerp LaplaceCyclic::solve(const FieldPerp& rhs, const FieldPerp& x0) { for (int ix = xs; ix <= xe; ix++) { // Take DST in Z direction and put result in k1d - if (((ix < inbndry) && (inner_boundary_flags & INVERT_SET) && localmesh->firstX()) + if (((ix < inbndry) && isInnerBoundaryFlagSetOnFirstX(INVERT_SET)) || ((localmesh->LocalNx - ix - 1 < outbndry) - && (outer_boundary_flags & INVERT_SET) && localmesh->lastX())) { + && isOuterBoundaryFlagSetOnLastX(INVERT_SET))) { // Use the values in x0 in the boundary DST(x0[ix] + 1, localmesh->LocalNz - 2, std::begin(k1d)); } else { @@ -169,8 +171,7 @@ FieldPerp LaplaceCyclic::solve(const FieldPerp& rhs, const FieldPerp& x0) { tridagMatrix(&a(kz, 0), &b(kz, 0), &c(kz, 0), &bcmplx(kz, 0), jy, kz, // wave number index kwave, // kwave (inverse wave length) - global_flags, inner_boundary_flags, outer_boundary_flags, &Acoef, - &C1coef, &C2coef, &Dcoef, + &Acoef, &C1coef, &C2coef, &Dcoef, false, // Don't include guard cells in arrays false); // Z domain not periodic } @@ -218,9 +219,9 @@ FieldPerp LaplaceCyclic::solve(const FieldPerp& rhs, const FieldPerp& x0) { for (int ix = xs; ix <= xe; ix++) { // Take FFT in Z direction, apply shift, and put result in k1d - if (((ix < inbndry) && (inner_boundary_flags & INVERT_SET) && localmesh->firstX()) + if (((ix < inbndry) && isInnerBoundaryFlagSetOnFirstX(INVERT_SET)) || ((localmesh->LocalNx - ix - 1 < outbndry) - && (outer_boundary_flags & INVERT_SET) && localmesh->lastX())) { + && isOuterBoundaryFlagSetOnLastX(INVERT_SET))) { // Use the values in x0 in the boundary rfft(x0[ix], localmesh->LocalNz, std::begin(k1d)); } else { @@ -241,8 +242,7 @@ FieldPerp LaplaceCyclic::solve(const FieldPerp& rhs, const FieldPerp& x0) { tridagMatrix(&a(kz, 0), &b(kz, 0), &c(kz, 0), &bcmplx(kz, 0), jy, kz, // True for the component constant (DC) in Z kwave, // Z wave number - global_flags, inner_boundary_flags, outer_boundary_flags, &Acoef, - &C1coef, &C2coef, &Dcoef, + &Acoef, &C1coef, &C2coef, &Dcoef, false); // Don't include guard cells in arrays } } @@ -275,7 +275,7 @@ FieldPerp LaplaceCyclic::solve(const FieldPerp& rhs, const FieldPerp& x0) { // ZFFT routine expects input of this length auto k1d = Array((localmesh->LocalNz) / 2 + 1); - const bool zero_DC = (global_flags & INVERT_ZERO_DC) != 0; + const bool zero_DC = isGlobalFlagSet(INVERT_ZERO_DC); BOUT_OMP_PERF(for nowait) for (int ix = xs; ix <= xe; ix++) { @@ -316,13 +316,13 @@ Field3D LaplaceCyclic::solve(const Field3D& rhs, const Field3D& x0) { // If the flags to assign that only one guard cell should be used is set int inbndry = localmesh->xstart, outbndry = localmesh->xstart; - if (((global_flags & INVERT_BOTH_BNDRY_ONE) != 0) || (localmesh->xstart < 2)) { + if (isGlobalFlagSet(INVERT_BOTH_BNDRY_ONE) || (localmesh->xstart < 2)) { inbndry = outbndry = 1; } - if ((inner_boundary_flags & INVERT_BNDRY_ONE) != 0) { + if (isInnerBoundaryFlagSet(INVERT_BNDRY_ONE)) { inbndry = 1; } - if ((outer_boundary_flags & INVERT_BNDRY_ONE) != 0) { + if (isOuterBoundaryFlagSet(INVERT_BNDRY_ONE)) { outbndry = 1; } @@ -350,6 +350,9 @@ Field3D LaplaceCyclic::solve(const Field3D& rhs, const Field3D& x0) { const int nsys = nmode * ny; // Number of systems of equations to solve const int nxny = nx * ny; // Number of points in X-Y + // This is just to silence static analysis + ASSERT0(ny > 0); + auto a3D = Matrix(nsys, nx); auto b3D = Matrix(nsys, nx); auto c3D = Matrix(nsys, nx); @@ -374,10 +377,9 @@ Field3D LaplaceCyclic::solve(const Field3D& rhs, const Field3D& x0) { // Take DST in Z direction and put result in k1d - if (((ix < inbndry) && ((inner_boundary_flags & INVERT_SET) != 0) - && localmesh->firstX()) + if (((ix < inbndry) && isInnerBoundaryFlagSetOnFirstX(INVERT_SET)) || ((localmesh->LocalNx - ix - 1 < outbndry) - && ((outer_boundary_flags & INVERT_SET) != 0) && localmesh->lastX())) { + && isOuterBoundaryFlagSetOnLastX(INVERT_SET))) { // Use the values in x0 in the boundary DST(x0(ix, iy) + 1, localmesh->LocalNz - 2, std::begin(k1d)); } else { @@ -405,8 +407,7 @@ Field3D LaplaceCyclic::solve(const Field3D& rhs, const Field3D& x0) { tridagMatrix(&a3D(ind, 0), &b3D(ind, 0), &c3D(ind, 0), &bcmplx3D(ind, 0), iy, kz, // wave number index kwave, // kwave (inverse wave length) - global_flags, inner_boundary_flags, outer_boundary_flags, &Acoef, - &C1coef, &C2coef, &Dcoef, + &Acoef, &C1coef, &C2coef, &Dcoef, false, // Don't include guard cells in arrays false); // Z domain not periodic } @@ -462,10 +463,9 @@ Field3D LaplaceCyclic::solve(const Field3D& rhs, const Field3D& x0) { // Take FFT in Z direction, apply shift, and put result in k1d - if (((ix < inbndry) && ((inner_boundary_flags & INVERT_SET) != 0) - && localmesh->firstX()) + if (((ix < inbndry) && isInnerBoundaryFlagSetOnFirstX(INVERT_SET)) || ((localmesh->LocalNx - ix - 1 < outbndry) - && ((outer_boundary_flags & INVERT_SET) != 0) && localmesh->lastX())) { + && isOuterBoundaryFlagSetOnLastX(INVERT_SET))) { // Use the values in x0 in the boundary rfft(x0(ix, iy), localmesh->LocalNz, std::begin(k1d)); } else { @@ -490,8 +490,7 @@ Field3D LaplaceCyclic::solve(const Field3D& rhs, const Field3D& x0) { tridagMatrix(&a3D(ind, 0), &b3D(ind, 0), &c3D(ind, 0), &bcmplx3D(ind, 0), iy, kz, // True for the component constant (DC) in Z kwave, // Z wave number - global_flags, inner_boundary_flags, outer_boundary_flags, &Acoef, - &C1coef, &C2coef, &Dcoef, + &Acoef, &C1coef, &C2coef, &Dcoef, false); // Don't include guard cells in arrays } } @@ -502,9 +501,8 @@ Field3D LaplaceCyclic::solve(const Field3D& rhs, const Field3D& x0) { if (localmesh->periodicX) { // Subtract X average of kz=0 mode - BoutReal local[ny + 1]; + std::vector local(ny + 1, 0.0); for (int y = 0; y < ny; y++) { - local[y] = 0.0; for (int ix = xs; ix <= xe; ix++) { local[y] += xcmplx3D(y * nmode, ix - xs).real(); } @@ -512,8 +510,9 @@ Field3D LaplaceCyclic::solve(const Field3D& rhs, const Field3D& x0) { local[ny] = static_cast(xe - xs + 1); // Global reduce - BoutReal global[ny + 1]; - MPI_Allreduce(local, global, ny + 1, MPI_DOUBLE, MPI_SUM, localmesh->getXcomm()); + std::vector global(ny + 1, 0.0); + MPI_Allreduce(local.data(), global.data(), ny + 1, MPI_DOUBLE, MPI_SUM, + localmesh->getXcomm()); // Subtract average from kz=0 modes for (int y = 0; y < ny; y++) { BoutReal avg = global[y] / global[ny]; @@ -530,7 +529,7 @@ Field3D LaplaceCyclic::solve(const Field3D& rhs, const Field3D& x0) { auto k1d = Array((localmesh->LocalNz) / 2 + 1); // ZFFT routine expects input of this length - const bool zero_DC = (global_flags & INVERT_ZERO_DC) != 0; + const bool zero_DC = isGlobalFlagSet(INVERT_ZERO_DC); BOUT_OMP_PERF(for nowait) for (int ind = 0; ind < nxny; ++ind) { // Loop over X and Y diff --git a/src/invert/laplace/impls/hypre3d/hypre3d_laplace.cxx b/src/invert/laplace/impls/hypre3d/hypre3d_laplace.cxx index f41894ad38..24448da5e3 100644 --- a/src/invert/laplace/impls/hypre3d/hypre3d_laplace.cxx +++ b/src/invert/laplace/impls/hypre3d/hypre3d_laplace.cxx @@ -99,7 +99,7 @@ LaplaceHypre3d::LaplaceHypre3d(Options* opt, const CELL_LOC loc, Mesh* mesh_in, // Set up boundary conditions in operator BOUT_FOR_SERIAL(i, indexer->getRegionInnerX()) { - if (inner_boundary_flags & INVERT_AC_GRAD) { + if (isInnerBoundaryFlagSet(INVERT_AC_GRAD)) { // Neumann on inner X boundary operator3D(i, i) = -1. / coords->dx()[i] / sqrt(coords->g_11()[i]); operator3D(i, i.xp()) = 1. / coords->dx()[i] / sqrt(coords->g_11()[i]); @@ -111,7 +111,7 @@ LaplaceHypre3d::LaplaceHypre3d(Options* opt, const CELL_LOC loc, Mesh* mesh_in, } BOUT_FOR_SERIAL(i, indexer->getRegionOuterX()) { - if (outer_boundary_flags & INVERT_AC_GRAD) { + if (isOuterBoundaryFlagSet(INVERT_AC_GRAD)) { // Neumann on outer X boundary operator3D(i, i) = 1. / coords->dx()[i] / sqrt(coords->g_11()[i]); operator3D(i, i.xm()) = -1. / coords->dx()[i] / sqrt(coords->g_11()[i]); @@ -180,9 +180,9 @@ Field3D LaplaceHypre3d::solve(const Field3D& b_in, const Field3D& x0) { // Adjust vectors to represent boundary conditions and check that // boundary cells are finite BOUT_FOR_SERIAL(i, indexer->getRegionInnerX()) { - const BoutReal val = (inner_boundary_flags & INVERT_SET) ? x0[i] : 0.; + const BoutReal val = isInnerBoundaryFlagSet(INVERT_SET) ? x0[i] : 0.; ASSERT1(std::isfinite(val)); - if (!(inner_boundary_flags & INVERT_RHS)) { + if (!(isInnerBoundaryFlagSet(INVERT_RHS))) { b[i] = val; } else { ASSERT1(std::isfinite(b[i])); @@ -190,9 +190,9 @@ Field3D LaplaceHypre3d::solve(const Field3D& b_in, const Field3D& x0) { } BOUT_FOR_SERIAL(i, indexer->getRegionOuterX()) { - const BoutReal val = (outer_boundary_flags & INVERT_SET) ? x0[i] : 0.; + const BoutReal val = (isOuterBoundaryFlagSet(INVERT_SET)) ? x0[i] : 0.; ASSERT1(std::isfinite(val)); - if (!(outer_boundary_flags & INVERT_RHS)) { + if (!(isOuterBoundaryFlagSet(INVERT_RHS))) { b[i] = val; } else { ASSERT1(std::isfinite(b[i])); diff --git a/src/invert/laplace/impls/iterative_parallel_tri/iterative_parallel_tri.cxx b/src/invert/laplace/impls/iterative_parallel_tri/iterative_parallel_tri.cxx index b09b67611b..f79463769a 100644 --- a/src/invert/laplace/impls/iterative_parallel_tri/iterative_parallel_tri.cxx +++ b/src/invert/laplace/impls/iterative_parallel_tri/iterative_parallel_tri.cxx @@ -293,10 +293,8 @@ FieldPerp LaplaceIPT::solve(const FieldPerp& b, const FieldPerp& x0) { */ auto bcmplx = Matrix(nmode, ncx); - const bool invert_inner_boundary = - isInnerBoundaryFlagSet(INVERT_SET) and localmesh->firstX(); - const bool invert_outer_boundary = - isOuterBoundaryFlagSet(INVERT_SET) and localmesh->lastX(); + const bool invert_inner_boundary = isInnerBoundaryFlagSetOnFirstX(INVERT_SET); + const bool invert_outer_boundary = isOuterBoundaryFlagSetOnLastX(INVERT_SET); BOUT_OMP_PERF(parallel for) for (int ix = 0; ix < ncx; ix++) { @@ -345,8 +343,7 @@ FieldPerp LaplaceIPT::solve(const FieldPerp& b, const FieldPerp& x0) { kz, // wave number (different from kz only if we are taking a part // of the z-domain [and not from 0 to 2*pi]) - kz * kwaveFactor, global_flags, inner_boundary_flags, - outer_boundary_flags, &A, &C, &D); + kz * kwaveFactor, &A, &C, &D); // Patch up internal boundaries if (not localmesh->lastX()) { diff --git a/src/invert/laplace/impls/iterative_parallel_tri/iterative_parallel_tri.hxx b/src/invert/laplace/impls/iterative_parallel_tri/iterative_parallel_tri.hxx index 1c6bb7a02e..02e3eca06c 100644 --- a/src/invert/laplace/impls/iterative_parallel_tri/iterative_parallel_tri.hxx +++ b/src/invert/laplace/impls/iterative_parallel_tri/iterative_parallel_tri.hxx @@ -234,14 +234,6 @@ private: /// First and last interior points xstart, xend int xs, xe; - - bool isGlobalFlagSet(int flag) const { return (global_flags & flag) != 0; } - bool isInnerBoundaryFlagSet(int flag) const { - return (inner_boundary_flags & flag) != 0; - } - bool isOuterBoundaryFlagSet(int flag) const { - return (outer_boundary_flags & flag) != 0; - } }; #endif // BOUT_USE_METRIC_3D diff --git a/src/invert/laplace/impls/multigrid/multigrid_laplace.cxx b/src/invert/laplace/impls/multigrid/multigrid_laplace.cxx index 2ce914d38a..36bf12aea2 100644 --- a/src/invert/laplace/impls/multigrid/multigrid_laplace.cxx +++ b/src/invert/laplace/impls/multigrid/multigrid_laplace.cxx @@ -84,19 +84,18 @@ LaplaceMultigrid::LaplaceMultigrid(Options* opt, const CELL_LOC loc, Mesh* mesh_ // Initialize, allocate memory, etc. comms_tagbase = 385; // Some random number - int implemented_global_flags = INVERT_START_NEW; - if (global_flags & ~implemented_global_flags) { + constexpr int implemented_global_flags = INVERT_START_NEW; + if (isGlobalFlagSet(~implemented_global_flags)) { throw BoutException("Attempted to set Laplacian inversion flag that is not " "implemented in LaplaceMultigrid."); } - int implemented_boundary_flags = - INVERT_AC_GRAD + INVERT_SET - + INVERT_DC_GRAD; // INVERT_DC_GRAD does not actually do anything, but harmless to set while comparing to Fourier solver with Neumann boundary conditions - if (inner_boundary_flags & ~implemented_boundary_flags) { + // INVERT_DC_GRAD does not actually do anything, but harmless to set while comparing to Fourier solver with Neumann boundary conditions + constexpr int implemented_boundary_flags = INVERT_AC_GRAD + INVERT_SET + INVERT_DC_GRAD; + if (isInnerBoundaryFlagSet(~implemented_boundary_flags)) { throw BoutException("Attempted to set Laplacian inner boundary inversion flag that " "is not implemented in LaplaceMultigrid."); } - if (outer_boundary_flags & ~implemented_boundary_flags) { + if (isOuterBoundaryFlagSet(~implemented_boundary_flags)) { throw BoutException("Attempted to set Laplacian outer boundary inversion flag that " "is not implemented in LaplaceMultigrid."); } @@ -242,7 +241,7 @@ FieldPerp LaplaceMultigrid::solve(const FieldPerp& b_in, const FieldPerp& x0) { int lz2 = lzz + 2; int lxx = kMG->lnx[level]; - if (global_flags & INVERT_START_NEW) { + if (isGlobalFlagSet(INVERT_START_NEW)) { // set initial guess to zero BOUT_OMP_PERF(parallel default(shared)) BOUT_OMP_PERF(for collapse(2)) @@ -276,9 +275,9 @@ FieldPerp LaplaceMultigrid::solve(const FieldPerp& b_in, const FieldPerp& x0) { } if (localmesh->firstX()) { - if (inner_boundary_flags & INVERT_AC_GRAD) { + if (isInnerBoundaryFlagSet(INVERT_AC_GRAD)) { // Neumann boundary condition - if (inner_boundary_flags & INVERT_SET) { + if (isInnerBoundaryFlagSet(INVERT_SET)) { // guard cells of x0 specify gradient to set at inner boundary BOUT_OMP_PERF(parallel default(shared)) BOUT_OMP_PERF(for) @@ -299,7 +298,7 @@ FieldPerp LaplaceMultigrid::solve(const FieldPerp& b_in, const FieldPerp& x0) { } } else { // Dirichlet boundary condition - if (inner_boundary_flags & INVERT_SET) { + if (isInnerBoundaryFlagSet(INVERT_SET)) { // guard cells of x0 specify value to set at inner boundary BOUT_OMP_PERF(parallel default(shared)) BOUT_OMP_PERF(for) @@ -320,9 +319,9 @@ FieldPerp LaplaceMultigrid::solve(const FieldPerp& b_in, const FieldPerp& x0) { } } if (localmesh->lastX()) { - if (outer_boundary_flags & INVERT_AC_GRAD) { + if (isOuterBoundaryFlagSet(INVERT_AC_GRAD)) { // Neumann boundary condition - if (inner_boundary_flags & INVERT_SET) { + if (isInnerBoundaryFlagSet(INVERT_SET)) { // guard cells of x0 specify gradient to set at outer boundary BOUT_OMP_PERF(parallel default(shared)) BOUT_OMP_PERF(for) @@ -344,7 +343,7 @@ FieldPerp LaplaceMultigrid::solve(const FieldPerp& b_in, const FieldPerp& x0) { } } else { // Dirichlet boundary condition - if (outer_boundary_flags & INVERT_SET) { + if (isOuterBoundaryFlagSet(INVERT_SET)) { // guard cells of x0 specify value to set at outer boundary BOUT_OMP_PERF(parallel default(shared)) BOUT_OMP_PERF(for) @@ -477,9 +476,9 @@ FieldPerp LaplaceMultigrid::solve(const FieldPerp& b_in, const FieldPerp& x0) { } } if (localmesh->firstX()) { - if (inner_boundary_flags & INVERT_AC_GRAD) { + if (isInnerBoundaryFlagSet(INVERT_AC_GRAD)) { // Neumann boundary condition - if (inner_boundary_flags & INVERT_SET) { + if (isInnerBoundaryFlagSet(INVERT_SET)) { // guard cells of x0 specify gradient to set at inner boundary int i2 = -1 + localmesh->xstart; BOUT_OMP_PERF(parallel default(shared)) @@ -503,7 +502,7 @@ FieldPerp LaplaceMultigrid::solve(const FieldPerp& b_in, const FieldPerp& x0) { } } else { // Dirichlet boundary condition - if (inner_boundary_flags & INVERT_SET) { + if (isInnerBoundaryFlagSet(INVERT_SET)) { // guard cells of x0 specify value to set at inner boundary int i2 = -1 + localmesh->xstart; BOUT_OMP_PERF(parallel default(shared)) @@ -525,9 +524,9 @@ FieldPerp LaplaceMultigrid::solve(const FieldPerp& b_in, const FieldPerp& x0) { } } if (localmesh->lastX()) { - if (outer_boundary_flags & INVERT_AC_GRAD) { + if (isOuterBoundaryFlagSet(INVERT_AC_GRAD)) { // Neumann boundary condition - if (inner_boundary_flags & INVERT_SET) { + if (isInnerBoundaryFlagSet(INVERT_SET)) { // guard cells of x0 specify gradient to set at outer boundary int i2 = lxx + localmesh->xstart; BOUT_OMP_PERF(parallel default(shared)) @@ -551,7 +550,7 @@ FieldPerp LaplaceMultigrid::solve(const FieldPerp& b_in, const FieldPerp& x0) { } } else { // Dirichlet boundary condition - if (outer_boundary_flags & INVERT_SET) { + if (isOuterBoundaryFlagSet(INVERT_SET)) { // guard cells of x0 specify value to set at outer boundary int i2 = lxx + localmesh->xstart; BOUT_OMP_PERF(parallel default(shared)) @@ -654,7 +653,7 @@ void LaplaceMultigrid::generateMatrixF(int level) { // Here put boundary conditions if (kMG->rProcI == 0) { - if (inner_boundary_flags & INVERT_AC_GRAD) { + if (isInnerBoundaryFlagSet(INVERT_AC_GRAD)) { // Neumann boundary condition BOUT_OMP_PERF(parallel default(shared)) BOUT_OMP_PERF(for) @@ -689,7 +688,7 @@ void LaplaceMultigrid::generateMatrixF(int level) { } } if (kMG->rProcI == kMG->xNP - 1) { - if (outer_boundary_flags & INVERT_AC_GRAD) { + if (isOuterBoundaryFlagSet(INVERT_AC_GRAD)) { // Neumann boundary condition BOUT_OMP_PERF(parallel default(shared)) BOUT_OMP_PERF(for) diff --git a/src/invert/laplace/impls/naulin/naulin_laplace.cxx b/src/invert/laplace/impls/naulin/naulin_laplace.cxx index d4f9998f32..6b3fbd803a 100644 --- a/src/invert/laplace/impls/naulin/naulin_laplace.cxx +++ b/src/invert/laplace/impls/naulin/naulin_laplace.cxx @@ -174,9 +174,9 @@ LaplaceNaulin::LaplaceNaulin(Options* opt, const CELL_LOC loc, Mesh* mesh_in, // invert Delp2 and we will not converge ASSERT0(delp2type == "cyclic" || delp2type == "spt" || delp2type == "tri"); // Use same flags for FFT solver as for NaulinSolver - delp2solver->setGlobalFlags(global_flags); - delp2solver->setInnerBoundaryFlags(inner_boundary_flags); - delp2solver->setOuterBoundaryFlags(outer_boundary_flags); + delp2solver->setGlobalFlags(getGlobalFlags()); + delp2solver->setInnerBoundaryFlags(getInnerBoundaryFlags()); + delp2solver->setOuterBoundaryFlags(getOuterBoundaryFlags()); static int naulinsolver_count = 1; setPerformanceName(fmt::format("{}{}", "naulinsolver", ++naulinsolver_count)); @@ -258,7 +258,7 @@ Field3D LaplaceNaulin::solve(const Field3D& rhs, const Field3D& x0) { // Note take a copy of the 'b' argument, because we want to return a copy of it in the // result - if ((inner_boundary_flags & INVERT_SET) || (outer_boundary_flags & INVERT_SET)) { + if (isInnerBoundaryFlagSet(INVERT_SET) || isOuterBoundaryFlagSet(INVERT_SET)) { // This passes in the boundary conditions from x0's guard cells copy_x_boundaries(x_guess, x0, localmesh); } diff --git a/src/invert/laplace/impls/pcr/pcr.cxx b/src/invert/laplace/impls/pcr/pcr.cxx index 7fa2cf8d83..6cac74ee92 100644 --- a/src/invert/laplace/impls/pcr/pcr.cxx +++ b/src/invert/laplace/impls/pcr/pcr.cxx @@ -149,13 +149,13 @@ FieldPerp LaplacePCR::solve(const FieldPerp& rhs, const FieldPerp& x0) { // If the flags to assign that only one guard cell should be used is set inbndry = localmesh->xstart; outbndry = localmesh->xstart; - if (((global_flags & INVERT_BOTH_BNDRY_ONE) != 0) || (localmesh->xstart < 2)) { + if (isGlobalFlagSet(INVERT_BOTH_BNDRY_ONE) || (localmesh->xstart < 2)) { inbndry = outbndry = 1; } - if ((inner_boundary_flags & INVERT_BNDRY_ONE) != 0) { + if (isInnerBoundaryFlagSet(INVERT_BNDRY_ONE)) { inbndry = 1; } - if ((outer_boundary_flags & INVERT_BNDRY_ONE) != 0) { + if (isOuterBoundaryFlagSet(INVERT_BNDRY_ONE)) { outbndry = 1; } @@ -173,10 +173,9 @@ FieldPerp LaplacePCR::solve(const FieldPerp& rhs, const FieldPerp& x0) { for (int ix = xs; ix <= xe; ix++) { // Take DST in Z direction and put result in k1d - if (((ix < inbndry) && ((inner_boundary_flags & INVERT_SET) != 0) - && localmesh->firstX()) + if (((ix < inbndry) && isInnerBoundaryFlagSetOnFirstX(INVERT_SET)) || ((localmesh->LocalNx - ix - 1 < outbndry) - && ((outer_boundary_flags & INVERT_SET) != 0) && localmesh->lastX())) { + && isOuterBoundaryFlagSetOnLastX(INVERT_SET))) { // Use the values in x0 in the boundary DST(x0[ix] + 1, localmesh->LocalNz - 2, std::begin(k1d)); } else { @@ -199,8 +198,7 @@ FieldPerp LaplacePCR::solve(const FieldPerp& rhs, const FieldPerp& x0) { tridagMatrix(&a(kz, 0), &b(kz, 0), &c(kz, 0), &bcmplx(kz, 0), jy, kz, // wave number index kwave, // kwave (inverse wave length) - global_flags, inner_boundary_flags, outer_boundary_flags, &Acoef, - &C1coef, &C2coef, &Dcoef, + &Acoef, &C1coef, &C2coef, &Dcoef, false); // Don't include guard cells in arrays } } // BOUT_OMP_PERF(parallel) @@ -245,10 +243,9 @@ FieldPerp LaplacePCR::solve(const FieldPerp& rhs, const FieldPerp& x0) { for (int ix = xs; ix <= xe; ix++) { // Take FFT in Z direction, apply shift, and put result in k1d - if (((ix < inbndry) && ((inner_boundary_flags & INVERT_SET) != 0) - && localmesh->firstX()) + if (((ix < inbndry) && isInnerBoundaryFlagSetOnFirstX(INVERT_SET)) || ((localmesh->LocalNx - ix - 1 < outbndry) - && ((outer_boundary_flags & INVERT_SET) != 0) && localmesh->lastX())) { + && isOuterBoundaryFlagSetOnLastX(INVERT_SET))) { // Use the values in x0 in the boundary rfft(x0[ix], localmesh->LocalNz, std::begin(k1d)); } else { @@ -269,8 +266,7 @@ FieldPerp LaplacePCR::solve(const FieldPerp& rhs, const FieldPerp& x0) { tridagMatrix(&a(kz, 0), &b(kz, 0), &c(kz, 0), &bcmplx(kz, 0), jy, kz, // True for the component constant (DC) in Z kwave, // Z wave number - global_flags, inner_boundary_flags, outer_boundary_flags, &Acoef, - &C1coef, &C2coef, &Dcoef, + &Acoef, &C1coef, &C2coef, &Dcoef, false); // Don't include guard cells in arrays } } // BOUT_OMP_PERF(parallel) @@ -285,7 +281,7 @@ FieldPerp LaplacePCR::solve(const FieldPerp& rhs, const FieldPerp& x0) { auto k1d = Array((localmesh->LocalNz) / 2 + 1); // ZFFT routine expects input of this length - const bool zero_DC = (global_flags & INVERT_ZERO_DC) != 0; + const bool zero_DC = isGlobalFlagSet(INVERT_ZERO_DC); BOUT_OMP_PERF(for nowait) for (int ix = xs; ix <= xe; ix++) { @@ -327,13 +323,13 @@ Field3D LaplacePCR::solve(const Field3D& rhs, const Field3D& x0) { // If the flags to assign that only one guard cell should be used is set inbndry = localmesh->xstart; outbndry = localmesh->xstart; - if (((global_flags & INVERT_BOTH_BNDRY_ONE) != 0) || (localmesh->xstart < 2)) { + if (isGlobalFlagSet(INVERT_BOTH_BNDRY_ONE) || (localmesh->xstart < 2)) { inbndry = outbndry = 1; } - if ((inner_boundary_flags & INVERT_BNDRY_ONE) != 0) { + if (isInnerBoundaryFlagSet(INVERT_BNDRY_ONE)) { inbndry = 1; } - if ((outer_boundary_flags & INVERT_BNDRY_ONE) != 0) { + if (isOuterBoundaryFlagSet(INVERT_BNDRY_ONE)) { outbndry = 1; } @@ -387,10 +383,9 @@ Field3D LaplacePCR::solve(const Field3D& rhs, const Field3D& x0) { // Take DST in Z direction and put result in k1d - if (((ix < inbndry) && ((inner_boundary_flags & INVERT_SET) != 0) - && localmesh->firstX()) + if (((ix < inbndry) && isInnerBoundaryFlagSetOnFirstX(INVERT_SET)) || ((localmesh->LocalNx - ix - 1 < outbndry) - && ((outer_boundary_flags & INVERT_SET) != 0) && localmesh->lastX())) { + && isOuterBoundaryFlagSetOnLastX(INVERT_SET))) { // Use the values in x0 in the boundary DST(x0(ix, iy) + 1, localmesh->LocalNz - 2, std::begin(k1d)); } else { @@ -417,8 +412,7 @@ Field3D LaplacePCR::solve(const Field3D& rhs, const Field3D& x0) { tridagMatrix(&a3D(ind, 0), &b3D(ind, 0), &c3D(ind, 0), &bcmplx3D(ind, 0), iy, kz, // wave number index kwave, // kwave (inverse wave length) - global_flags, inner_boundary_flags, outer_boundary_flags, &Acoef, - &C1coef, &C2coef, &Dcoef, + &Acoef, &C1coef, &C2coef, &Dcoef, false); // Don't include guard cells in arrays } } // BOUT_OMP_PERF(parallel) @@ -472,10 +466,9 @@ Field3D LaplacePCR::solve(const Field3D& rhs, const Field3D& x0) { // Take FFT in Z direction, apply shift, and put result in k1d - if (((ix < inbndry) && ((inner_boundary_flags & INVERT_SET) != 0) - && localmesh->firstX()) + if (((ix < inbndry) && isInnerBoundaryFlagSetOnFirstX(INVERT_SET)) || ((localmesh->LocalNx - ix - 1 < outbndry) - && ((outer_boundary_flags & INVERT_SET) != 0) && localmesh->lastX())) { + && isOuterBoundaryFlagSetOnLastX(INVERT_SET))) { // Use the values in x0 in the boundary rfft(x0(ix, iy), localmesh->LocalNz, std::begin(k1d)); } else { @@ -500,8 +493,7 @@ Field3D LaplacePCR::solve(const Field3D& rhs, const Field3D& x0) { tridagMatrix(&a3D(ind, 0), &b3D(ind, 0), &c3D(ind, 0), &bcmplx3D(ind, 0), iy, kz, // True for the component constant (DC) in Z kwave, // Z wave number - global_flags, inner_boundary_flags, outer_boundary_flags, &Acoef, - &C1coef, &C2coef, &Dcoef, + &Acoef, &C1coef, &C2coef, &Dcoef, false); // Don't include guard cells in arrays } } // BOUT_OMP_PERF(parallel) @@ -516,7 +508,7 @@ Field3D LaplacePCR::solve(const Field3D& rhs, const Field3D& x0) { auto k1d = Array((localmesh->LocalNz) / 2 + 1); // ZFFT routine expects input of this length - const bool zero_DC = (global_flags & INVERT_ZERO_DC) != 0; + const bool zero_DC = isGlobalFlagSet(INVERT_ZERO_DC); BOUT_OMP_PERF(for nowait) for (int ind = 0; ind < nxny; ++ind) { // Loop over X and Y diff --git a/src/invert/laplace/impls/pcr/pcr.hxx b/src/invert/laplace/impls/pcr/pcr.hxx index 38b7c356d3..ec4637f56c 100644 --- a/src/invert/laplace/impls/pcr/pcr.hxx +++ b/src/invert/laplace/impls/pcr/pcr.hxx @@ -172,14 +172,6 @@ private: /// First and last interior points xstart, xend int xs, xe; - bool isGlobalFlagSet(int flag) const { return (global_flags & flag) != 0; } - bool isInnerBoundaryFlagSet(int flag) const { - return (inner_boundary_flags & flag) != 0; - } - bool isOuterBoundaryFlagSet(int flag) const { - return (outer_boundary_flags & flag) != 0; - } - bool dst{false}; }; diff --git a/src/invert/laplace/impls/pcr_thomas/pcr_thomas.cxx b/src/invert/laplace/impls/pcr_thomas/pcr_thomas.cxx index 6f7aa799f1..50474f9670 100644 --- a/src/invert/laplace/impls/pcr_thomas/pcr_thomas.cxx +++ b/src/invert/laplace/impls/pcr_thomas/pcr_thomas.cxx @@ -145,13 +145,13 @@ FieldPerp LaplacePCR_THOMAS::solve(const FieldPerp& rhs, const FieldPerp& x0) { // If the flags to assign that only one guard cell should be used is set int inbndry = localmesh->xstart; int outbndry = localmesh->xstart; - if (((global_flags & INVERT_BOTH_BNDRY_ONE) != 0) || (localmesh->xstart < 2)) { + if (isGlobalFlagSet(INVERT_BOTH_BNDRY_ONE) || (localmesh->xstart < 2)) { inbndry = outbndry = 1; } - if ((inner_boundary_flags & INVERT_BNDRY_ONE) != 0) { + if (isInnerBoundaryFlagSet(INVERT_BNDRY_ONE)) { inbndry = 1; } - if ((outer_boundary_flags & INVERT_BNDRY_ONE) != 0) { + if (isOuterBoundaryFlagSet(INVERT_BNDRY_ONE)) { outbndry = 1; } @@ -169,10 +169,9 @@ FieldPerp LaplacePCR_THOMAS::solve(const FieldPerp& rhs, const FieldPerp& x0) { for (int ix = xs; ix <= xe; ix++) { // Take DST in Z direction and put result in k1d - if (((ix < inbndry) && ((inner_boundary_flags & INVERT_SET) != 0) - && localmesh->firstX()) + if (((ix < inbndry) && isInnerBoundaryFlagSetOnFirstX(INVERT_SET)) || ((localmesh->LocalNx - ix - 1 < outbndry) - && ((outer_boundary_flags & INVERT_SET) != 0) && localmesh->lastX())) { + && isOuterBoundaryFlagSetOnLastX(INVERT_SET))) { // Use the values in x0 in the boundary DST(x0[ix] + 1, localmesh->LocalNz - 2, std::begin(k1d)); } else { @@ -195,8 +194,7 @@ FieldPerp LaplacePCR_THOMAS::solve(const FieldPerp& rhs, const FieldPerp& x0) { tridagMatrix(&a(kz, 0), &b(kz, 0), &c(kz, 0), &bcmplx(kz, 0), jy, kz, // wave number index kwave, // kwave (inverse wave length) - global_flags, inner_boundary_flags, outer_boundary_flags, &Acoef, - &C1coef, &C2coef, &Dcoef, + &Acoef, &C1coef, &C2coef, &Dcoef, false); // Don't include guard cells in arrays } } @@ -241,10 +239,9 @@ FieldPerp LaplacePCR_THOMAS::solve(const FieldPerp& rhs, const FieldPerp& x0) { for (int ix = xs; ix <= xe; ix++) { // Take FFT in Z direction, apply shift, and put result in k1d - if (((ix < inbndry) && ((inner_boundary_flags & INVERT_SET) != 0) - && localmesh->firstX()) + if (((ix < inbndry) && isInnerBoundaryFlagSetOnFirstX(INVERT_SET)) || ((localmesh->LocalNx - ix - 1 < outbndry) - && ((outer_boundary_flags & INVERT_SET) != 0) && localmesh->lastX())) { + && isOuterBoundaryFlagSetOnLastX(INVERT_SET))) { // Use the values in x0 in the boundary rfft(x0[ix], localmesh->LocalNz, std::begin(k1d)); } else { @@ -265,8 +262,7 @@ FieldPerp LaplacePCR_THOMAS::solve(const FieldPerp& rhs, const FieldPerp& x0) { tridagMatrix(&a(kz, 0), &b(kz, 0), &c(kz, 0), &bcmplx(kz, 0), jy, kz, // True for the component constant (DC) in Z kwave, // Z wave number - global_flags, inner_boundary_flags, outer_boundary_flags, &Acoef, - &C1coef, &C2coef, &Dcoef, + &Acoef, &C1coef, &C2coef, &Dcoef, false); // Don't include guard cells in arrays } } @@ -281,7 +277,7 @@ FieldPerp LaplacePCR_THOMAS::solve(const FieldPerp& rhs, const FieldPerp& x0) { auto k1d = Array((localmesh->LocalNz) / 2 + 1); // ZFFT routine expects input of this length - const bool zero_DC = (global_flags & INVERT_ZERO_DC) != 0; + const bool zero_DC = isGlobalFlagSet(INVERT_ZERO_DC); BOUT_OMP_PERF(for nowait) for (int ix = xs; ix <= xe; ix++) { @@ -323,13 +319,13 @@ Field3D LaplacePCR_THOMAS::solve(const Field3D& rhs, const Field3D& x0) { // If the flags to assign that only one guard cell should be used is set int inbndry = localmesh->xstart; int outbndry = localmesh->xstart; - if (((global_flags & INVERT_BOTH_BNDRY_ONE) != 0) || (localmesh->xstart < 2)) { + if (isGlobalFlagSet(INVERT_BOTH_BNDRY_ONE) || (localmesh->xstart < 2)) { inbndry = outbndry = 1; } - if ((inner_boundary_flags & INVERT_BNDRY_ONE) != 0) { + if (isInnerBoundaryFlagSet(INVERT_BNDRY_ONE)) { inbndry = 1; } - if ((outer_boundary_flags & INVERT_BNDRY_ONE) != 0) { + if (isOuterBoundaryFlagSet(INVERT_BNDRY_ONE)) { outbndry = 1; } @@ -383,10 +379,9 @@ Field3D LaplacePCR_THOMAS::solve(const Field3D& rhs, const Field3D& x0) { // Take DST in Z direction and put result in k1d - if (((ix < inbndry) && ((inner_boundary_flags & INVERT_SET) != 0) - && localmesh->firstX()) + if (((ix < inbndry) && isInnerBoundaryFlagSetOnFirstX(INVERT_SET)) || ((localmesh->LocalNx - ix - 1 < outbndry) - && ((outer_boundary_flags & INVERT_SET) != 0) && localmesh->lastX())) { + && isOuterBoundaryFlagSetOnLastX(INVERT_SET))) { // Use the values in x0 in the boundary DST(x0(ix, iy) + 1, localmesh->LocalNz - 2, std::begin(k1d)); } else { @@ -413,8 +408,7 @@ Field3D LaplacePCR_THOMAS::solve(const Field3D& rhs, const Field3D& x0) { tridagMatrix(&a3D(ind, 0), &b3D(ind, 0), &c3D(ind, 0), &bcmplx3D(ind, 0), iy, kz, // wave number index kwave, // kwave (inverse wave length) - global_flags, inner_boundary_flags, outer_boundary_flags, &Acoef, - &C1coef, &C2coef, &Dcoef, + &Acoef, &C1coef, &C2coef, &Dcoef, false); // Don't include guard cells in arrays } } @@ -468,10 +462,9 @@ Field3D LaplacePCR_THOMAS::solve(const Field3D& rhs, const Field3D& x0) { // Take FFT in Z direction, apply shift, and put result in k1d - if (((ix < inbndry) && ((inner_boundary_flags & INVERT_SET) != 0) - && localmesh->firstX()) + if (((ix < inbndry) && isInnerBoundaryFlagSetOnFirstX(INVERT_SET)) || ((localmesh->LocalNx - ix - 1 < outbndry) - && ((outer_boundary_flags & INVERT_SET) != 0) && localmesh->lastX())) { + && isOuterBoundaryFlagSetOnLastX(INVERT_SET))) { // Use the values in x0 in the boundary rfft(x0(ix, iy), localmesh->LocalNz, std::begin(k1d)); } else { @@ -497,8 +490,7 @@ Field3D LaplacePCR_THOMAS::solve(const Field3D& rhs, const Field3D& x0) { tridagMatrix(&a3D(ind, 0), &b3D(ind, 0), &c3D(ind, 0), &bcmplx3D(ind, 0), iy, kz, // True for the component constant (DC) in Z kwave, // Z wave number - global_flags, inner_boundary_flags, outer_boundary_flags, &Acoef, - &C1coef, &C2coef, &Dcoef, + &Acoef, &C1coef, &C2coef, &Dcoef, false); // Don't include guard cells in arrays } } @@ -513,7 +505,7 @@ Field3D LaplacePCR_THOMAS::solve(const Field3D& rhs, const Field3D& x0) { auto k1d = Array((localmesh->LocalNz) / 2 + 1); // ZFFT routine expects input of this length - const bool zero_DC = (global_flags & INVERT_ZERO_DC) != 0; + const bool zero_DC = isGlobalFlagSet(INVERT_ZERO_DC); BOUT_OMP_PERF(for nowait) for (int ind = 0; ind < nxny; ++ind) { // Loop over X and Y diff --git a/src/invert/laplace/impls/pcr_thomas/pcr_thomas.hxx b/src/invert/laplace/impls/pcr_thomas/pcr_thomas.hxx index 009a1def2b..e12a647789 100644 --- a/src/invert/laplace/impls/pcr_thomas/pcr_thomas.hxx +++ b/src/invert/laplace/impls/pcr_thomas/pcr_thomas.hxx @@ -175,14 +175,6 @@ private: /// First and last interior points xstart, xend int xs, xe; - bool isGlobalFlagSet(int flag) const { return (global_flags & flag) != 0; } - bool isInnerBoundaryFlagSet(int flag) const { - return (inner_boundary_flags & flag) != 0; - } - bool isOuterBoundaryFlagSet(int flag) const { - return (outer_boundary_flags & flag) != 0; - } - bool dst{false}; }; diff --git a/src/invert/laplace/impls/petsc/petsc_laplace.cxx b/src/invert/laplace/impls/petsc/petsc_laplace.cxx index 94714a0a15..ae105e2994 100644 --- a/src/invert/laplace/impls/petsc/petsc_laplace.cxx +++ b/src/invert/laplace/impls/petsc/petsc_laplace.cxx @@ -23,7 +23,8 @@ * along with BOUT++. If not, see . * **************************************************************************/ -#include "bout/build_config.hxx" + +#include "bout/build_defines.hxx" #if BOUT_HAS_PETSC @@ -32,6 +33,8 @@ #include #include #include +#include +#include #include #include @@ -49,14 +52,13 @@ #define KSP_PREONLY "preonly" static PetscErrorCode laplacePCapply(PC pc, Vec x, Vec y) { - int ierr; + PetscFunctionBegin; // NOLINT - // Get the context - LaplacePetsc* s; - ierr = PCShellGetContext(pc, reinterpret_cast(&s)); + LaplacePetsc* laplace = nullptr; + const int ierr = PCShellGetContext(pc, reinterpret_cast(&laplace)); // NOLINT CHKERRQ(ierr); - PetscFunctionReturn(s->precon(x, y)); + PetscFunctionReturn(laplace->precon(x, y)); // NOLINT } LaplacePetsc::LaplacePetsc(Options* opt, const CELL_LOC loc, Mesh* mesh_in, @@ -79,28 +81,9 @@ LaplacePetsc::LaplacePetsc(Options* opt, const CELL_LOC loc, Mesh* mesh_in, } #if CHECK > 0 - // These are the implemented flags - implemented_flags = INVERT_START_NEW; - implemented_boundary_flags = INVERT_AC_GRAD + INVERT_SET + INVERT_RHS; // Checking flags are set to something which is not implemented - // This is done binary (which is possible as each flag is a power of 2) - if (global_flags & ~implemented_flags) { - if (global_flags & INVERT_4TH_ORDER) { - output << "For PETSc based Laplacian inverter, use 'fourth_order=true' instead of " - "setting INVERT_4TH_ORDER flag" - << endl; - } - throw BoutException("Attempted to set Laplacian inversion flag that is not " - "implemented in petsc_laplace.cxx"); - } - if (inner_boundary_flags & ~implemented_boundary_flags) { - throw BoutException("Attempted to set Laplacian inversion boundary flag that is not " - "implemented in petsc_laplace.cxx"); - } - if (outer_boundary_flags & ~implemented_boundary_flags) { - throw BoutException("Attempted to set Laplacian inversion boundary flag that is not " - "implemented in petsc_laplace.cxx"); - } + checkFlags(); + if (localmesh->periodicX) { throw BoutException("LaplacePetsc does not work with periodicity in the x direction " "(localmesh->PeriodicX == true). Change boundary conditions or " @@ -360,25 +343,7 @@ FieldPerp LaplacePetsc::solve(const FieldPerp& b, const FieldPerp& x0) { ASSERT1(x0.getLocation() == location); #if CHECK > 0 - // Checking flags are set to something which is not implemented (see - // constructor for details) - if (global_flags & !implemented_flags) { - if (global_flags & INVERT_4TH_ORDER) { - output << "For PETSc based Laplacian inverter, use 'fourth_order=true' instead of " - "setting INVERT_4TH_ORDER flag" - << endl; - } - throw BoutException("Attempted to set Laplacian inversion flag that is not " - "implemented in petsc_laplace.cxx"); - } - if (inner_boundary_flags & ~implemented_boundary_flags) { - throw BoutException("Attempted to set Laplacian inversion boundary flag that is not " - "implemented in petsc_laplace.cxx"); - } - if (outer_boundary_flags & ~implemented_boundary_flags) { - throw BoutException("Attempted to set Laplacian inversion boundary flag that is not " - "implemented in petsc_laplace.cxx"); - } + checkFlags(); #endif int y = b.getIndex(); // Get the Y index @@ -415,7 +380,7 @@ FieldPerp LaplacePetsc::solve(const FieldPerp& b, const FieldPerp& x0) { for (int z = 0; z < localmesh->LocalNz; z++) { PetscScalar val; // Value of element to be set in the matrix // If Neumann Boundary Conditions are set. - if (inner_boundary_flags & INVERT_AC_GRAD) { + if (isInnerBoundaryFlagSet(INVERT_AC_GRAD)) { // Set values corresponding to nodes adjacent in x if (fourth_order) { // Fourth Order Accuracy on Boundary @@ -474,9 +439,9 @@ FieldPerp LaplacePetsc::solve(const FieldPerp& b, const FieldPerp& x0) { // Set Components of RHS // If the inner boundary value should be set by b or x0 - if (inner_boundary_flags & INVERT_RHS) { + if (isInnerBoundaryFlagSet(INVERT_RHS)) { val = b[x][z]; - } else if (inner_boundary_flags & INVERT_SET) { + } else if (isInnerBoundaryFlagSet(INVERT_SET)) { val = x0[x][z]; } @@ -682,7 +647,7 @@ FieldPerp LaplacePetsc::solve(const FieldPerp& b, const FieldPerp& x0) { Element(i, x, z, 0, 0, val, MatA); // If Neumann Boundary Conditions are set. - if (outer_boundary_flags & INVERT_AC_GRAD) { + if (isOuterBoundaryFlagSet(INVERT_AC_GRAD)) { // Set values corresponding to nodes adjacent in x if (fourth_order) { // Fourth Order Accuracy on Boundary @@ -737,9 +702,9 @@ FieldPerp LaplacePetsc::solve(const FieldPerp& b, const FieldPerp& x0) { // Set Components of RHS // If the inner boundary value should be set by b or x0 val = 0; - if (outer_boundary_flags & INVERT_RHS) { + if (isOuterBoundaryFlagSet(INVERT_RHS)) { val = b[x][z]; - } else if (outer_boundary_flags & INVERT_SET) { + } else if (isOuterBoundaryFlagSet(INVERT_SET)) { val = x0[x][z]; } @@ -816,7 +781,7 @@ FieldPerp LaplacePetsc::solve(const FieldPerp& b, const FieldPerp& x0) { KSPSetTolerances(ksp, rtol, atol, dtol, maxits); // If the initial guess is not set to zero - if (!(global_flags & INVERT_START_NEW)) { + if (!isGlobalFlagSet(INVERT_START_NEW)) { KSPSetInitialGuessNonzero(ksp, static_cast(true)); } @@ -1198,4 +1163,24 @@ int LaplacePetsc::precon(Vec x, Vec y) { return 0; } +void LaplacePetsc::checkFlags() { + if (isGlobalFlagSet(~implemented_flags)) { + if (isGlobalFlagSet(INVERT_4TH_ORDER)) { + output_error.write( + "For PETSc based Laplacian inverter, use 'fourth_order=true' instead of " + "setting INVERT_4TH_ORDER flag\n"); + } + throw BoutException("Attempted to set Laplacian inversion flag that is not " + "implemented in petsc_laplace.cxx"); + } + if (isInnerBoundaryFlagSet(~implemented_boundary_flags)) { + throw BoutException("Attempted to set Laplacian inversion boundary flag that is not " + "implemented in petsc_laplace.cxx"); + } + if (isOuterBoundaryFlagSet(~implemented_boundary_flags)) { + throw BoutException("Attempted to set Laplacian inversion boundary flag that is not " + "implemented in petsc_laplace.cxx"); + } +} + #endif // BOUT_HAS_PETSC_3_3 diff --git a/src/invert/laplace/impls/petsc/petsc_laplace.hxx b/src/invert/laplace/impls/petsc/petsc_laplace.hxx index 011f8971df..55482644be 100644 --- a/src/invert/laplace/impls/petsc/petsc_laplace.hxx +++ b/src/invert/laplace/impls/petsc/petsc_laplace.hxx @@ -254,10 +254,11 @@ private: void vecToField(Vec x, FieldPerp& f); // Copy a vector into a fieldperp void fieldToVec(const FieldPerp& f, Vec x); // Copy a fieldperp into a vector -#if CHECK > 0 - int implemented_flags; - int implemented_boundary_flags; -#endif + static constexpr int implemented_flags = INVERT_START_NEW; + static constexpr int implemented_boundary_flags = + INVERT_AC_GRAD | INVERT_SET | INVERT_RHS; + + void checkFlags(); }; #endif //BOUT_HAS_PETSC diff --git a/src/invert/laplace/impls/petsc3damg/petsc3damg.cxx b/src/invert/laplace/impls/petsc3damg/petsc3damg.cxx index 3e85c2ed20..d6e3e56c53 100644 --- a/src/invert/laplace/impls/petsc3damg/petsc3damg.cxx +++ b/src/invert/laplace/impls/petsc3damg/petsc3damg.cxx @@ -84,12 +84,12 @@ LaplacePetsc3dAmg::LaplacePetsc3dAmg(Options* opt, const CELL_LOC loc, Mesh* mes #if CHECK > 0 // Checking flags are set to something which is not implemented // This is done binary (which is possible as each flag is a power of 2) - if (flagSet(global_flags, INVERT_4TH_ORDER)) { + if (isGlobalFlagSet(INVERT_4TH_ORDER)) { output.write("For PETSc based Laplacian inverter, use 'fourth_order=true' instead of " "setting INVERT_4TH_ORDER flag\n"); } - if (flagSet(global_flags, ~implemented_flags)) { + if (isGlobalFlagSet(~implemented_flags)) { throw BoutException("Attempted to set global Laplacian inversion flag that is not " "implemented in petsc_laplace.cxx"); } @@ -102,8 +102,8 @@ LaplacePetsc3dAmg::LaplacePetsc3dAmg(Options* opt, const CELL_LOC loc, Mesh* mes name); } }; - unimplementedBoundaryFlag(inner_boundary_flags, "inner"); - unimplementedBoundaryFlag(outer_boundary_flags, "outer"); + unimplementedBoundaryFlag(getInnerBoundaryFlags(), "inner"); + unimplementedBoundaryFlag(getOuterBoundaryFlags(), "outer"); unimplementedBoundaryFlag(lower_boundary_flags, "lower"); unimplementedBoundaryFlag(upper_boundary_flags, "upper"); @@ -119,7 +119,7 @@ LaplacePetsc3dAmg::LaplacePetsc3dAmg(Options* opt, const CELL_LOC loc, Mesh* mes } // Set up boundary conditions in operator - const bool inner_X_neumann = flagSet(inner_boundary_flags, INVERT_AC_GRAD); + const bool inner_X_neumann = isInnerBoundaryFlagSet(INVERT_AC_GRAD); const auto inner_X_BC = inner_X_neumann ? -1. / coords->dx() / sqrt(coords->g_11()) : 0.5; const auto inner_X_BC_plus = inner_X_neumann ? -inner_X_BC : 0.5; @@ -129,7 +129,7 @@ LaplacePetsc3dAmg::LaplacePetsc3dAmg(Options* opt, const CELL_LOC loc, Mesh* mes operator3D(i, i.xp()) = inner_X_BC_plus[i]; } - const bool outer_X_neumann = flagSet(outer_boundary_flags, INVERT_AC_GRAD); + const bool outer_X_neumann = isOuterBoundaryFlagSet(INVERT_AC_GRAD); const auto outer_X_BC = outer_X_neumann ? 1. / coords->dx() / sqrt(coords->g_11()) : 0.5; const auto outer_X_BC_minus = outer_X_neumann ? -outer_X_BC : 0.5; @@ -195,8 +195,8 @@ Field3D LaplacePetsc3dAmg::solve(const Field3D& b_in, const Field3D& x0) { // Adjust vectors to represent boundary conditions and check that // boundary cells are finite - setBC(rhs, b_in, indexer->getRegionInnerX(), inner_boundary_flags, x0); - setBC(rhs, b_in, indexer->getRegionOuterX(), outer_boundary_flags, x0); + setBC(rhs, b_in, indexer->getRegionInnerX(), getInnerBoundaryFlags(), x0); + setBC(rhs, b_in, indexer->getRegionOuterX(), getOuterBoundaryFlags(), x0); setBC(rhs, b_in, indexer->getRegionLowerY(), lower_boundary_flags, x0); setBC(rhs, b_in, indexer->getRegionUpperY(), upper_boundary_flags, x0); @@ -372,7 +372,7 @@ void LaplacePetsc3dAmg::updateMatrix3D() { C_df_dy += (coords->g12()[l] * dc_dx[l] + (coords->g22()[l] - 1. / coords->g_22()[l]) * dc_dy[l] + coords->g23()[l] * dc_dz[l]) - / C1[l]; + / C1[l]; } BoutReal C_d2f_dy2 = (coords->g22()[l] - 1.0 / coords->g_22()[l]); @@ -395,8 +395,8 @@ void LaplacePetsc3dAmg::updateMatrix3D() { C_d2f_dy2 /= SQ(coords->dy()[l]); C_d2f_dxdy /= 4 * coords->dx()[l]; // NOTE: This value is not completed here. It needs to - // be divide by dx(i +/- 1, j, k) when using to set a - // matrix element + // be divide by dx(i +/- 1, j, k) when using to set a + // matrix element C_d2f_dydz /= 4 * coords->dy()[l] * coords->dz()[l]; // The values stored in the y-boundary are already interpolated @@ -464,7 +464,7 @@ void LaplacePetsc3dAmg::updateMatrix3D() { KSPSetTolerances(ksp, rtol, atol, dtol, maxits); // If the initial guess is not set to zero - if ((global_flags & INVERT_START_NEW) == 0) { + if (!isGlobalFlagSet(INVERT_START_NEW)) { KSPSetInitialGuessNonzero(ksp, (PetscBool) true); } diff --git a/src/invert/laplace/impls/serial_band/serial_band.cxx b/src/invert/laplace/impls/serial_band/serial_band.cxx index beb034f281..9ab35314ca 100644 --- a/src/invert/laplace/impls/serial_band/serial_band.cxx +++ b/src/invert/laplace/impls/serial_band/serial_band.cxx @@ -99,7 +99,7 @@ FieldPerp LaplaceSerialBand::solve(const FieldPerp& b, const FieldPerp& x0) { int xbndry = localmesh->xstart; // Width of the x boundary // If the flags to assign that only one guard cell should be used is set - if ((global_flags & INVERT_BOTH_BNDRY_ONE) || (localmesh->xstart < 2)) { + if (isGlobalFlagSet(INVERT_BOTH_BNDRY_ONE) || (localmesh->xstart < 2)) { xbndry = 1; } @@ -107,8 +107,8 @@ FieldPerp LaplaceSerialBand::solve(const FieldPerp& b, const FieldPerp& x0) { for (int ix = 0; ix < localmesh->LocalNx; ix++) { // for fixed ix,jy set a complex vector rho(z) - if (((ix < xbndry) && (inner_boundary_flags & INVERT_SET)) - || ((ncx - ix < xbndry) && (outer_boundary_flags & INVERT_SET))) { + if (((ix < xbndry) && isInnerBoundaryFlagSet(INVERT_SET)) + || ((ncx - ix < xbndry) && (isOuterBoundaryFlagSet(INVERT_SET)))) { // Use the values in x0 in the boundary rfft(x0[ix], ncz, &bk(ix, 0)); } else { @@ -247,10 +247,10 @@ FieldPerp LaplaceSerialBand::solve(const FieldPerp& b, const FieldPerp& x0) { for (int ix = 0; ix < xbndry; ix++) { // Set zero-value. Change to zero-gradient if needed - if (!(inner_boundary_flags & (INVERT_RHS | INVERT_SET))) { + if (!isInnerBoundaryFlagSet(INVERT_RHS | INVERT_SET)) { bk1d[ix] = 0.0; } - if (!(outer_boundary_flags & (INVERT_RHS | INVERT_SET))) { + if (!isOuterBoundaryFlagSet(INVERT_RHS | INVERT_SET)) { bk1d[ncx - ix] = 0.0; } @@ -265,8 +265,8 @@ FieldPerp LaplaceSerialBand::solve(const FieldPerp& b, const FieldPerp& x0) { // DC // Inner boundary - if (inner_boundary_flags & (INVERT_DC_GRAD + INVERT_SET) - || inner_boundary_flags & (INVERT_DC_GRAD + INVERT_RHS)) { + if (isInnerBoundaryFlagSet(INVERT_DC_GRAD + INVERT_SET) + || isInnerBoundaryFlagSet(INVERT_DC_GRAD + INVERT_RHS)) { // Zero gradient at inner boundary. 2nd-order accurate // Boundary at midpoint for (int ix = 0; ix < xbndry; ix++) { @@ -277,7 +277,7 @@ FieldPerp LaplaceSerialBand::solve(const FieldPerp& b, const FieldPerp& x0) { A(ix, 4) = 0.; } - } else if (inner_boundary_flags & INVERT_DC_GRAD) { + } else if (isInnerBoundaryFlagSet(INVERT_DC_GRAD)) { // Zero gradient at inner boundary. 2nd-order accurate // Boundary at midpoint for (int ix = 0; ix < xbndry; ix++) { @@ -288,7 +288,7 @@ FieldPerp LaplaceSerialBand::solve(const FieldPerp& b, const FieldPerp& x0) { A(ix, 4) = 0.; } - } else if (inner_boundary_flags & INVERT_DC_GRADPAR) { + } else if (isInnerBoundaryFlagSet(INVERT_DC_GRADPAR)) { for (int ix = 0; ix < xbndry; ix++) { A(ix, 0) = 0.; A(ix, 1) = 0.; @@ -296,7 +296,7 @@ FieldPerp LaplaceSerialBand::solve(const FieldPerp& b, const FieldPerp& x0) { A(ix, 3) = 4. / sqrt(coords->g_22()(ix + 1, jy)); A(ix, 4) = -1. / sqrt(coords->g_22()(ix + 2, jy)); } - } else if (inner_boundary_flags & INVERT_DC_GRADPARINV) { + } else if (isInnerBoundaryFlagSet(INVERT_DC_GRADPARINV)) { for (int ix = 0; ix < xbndry; ix++) { A(ix, 0) = 0.; A(ix, 1) = 0.; @@ -304,7 +304,7 @@ FieldPerp LaplaceSerialBand::solve(const FieldPerp& b, const FieldPerp& x0) { A(ix, 3) = 4. * sqrt(coords->g_22()(ix + 1, jy)); A(ix, 4) = -sqrt(coords->g_22()(ix + 2, jy)); } - } else if (inner_boundary_flags & INVERT_DC_LAP) { + } else if (isInnerBoundaryFlagSet(INVERT_DC_LAP)) { for (int ix = 0; ix < xbndry; ix++) { A(ix, 0) = 0.; A(ix, 1) = 0.; @@ -315,7 +315,7 @@ FieldPerp LaplaceSerialBand::solve(const FieldPerp& b, const FieldPerp& x0) { } // Outer boundary - if (outer_boundary_flags & INVERT_DC_GRAD) { + if (isOuterBoundaryFlagSet(INVERT_DC_GRAD)) { // Zero gradient at outer boundary for (int ix = 0; ix < xbndry; ix++) { A(ncx - ix, 1) = -1.0; @@ -326,12 +326,12 @@ FieldPerp LaplaceSerialBand::solve(const FieldPerp& b, const FieldPerp& x0) { // AC // Inner boundarySQ(kwave)*coef2 - if (inner_boundary_flags & INVERT_AC_GRAD) { + if (isInnerBoundaryFlagSet(INVERT_AC_GRAD)) { // Zero gradient at inner boundary for (int ix = 0; ix < xbndry; ix++) { A(ix, 3) = -1.0; } - } else if (inner_boundary_flags & INVERT_AC_LAP) { + } else if (isInnerBoundaryFlagSet(INVERT_AC_LAP)) { // Enforce zero laplacian for 2nd and 4th-order int ix = 1; @@ -369,12 +369,12 @@ FieldPerp LaplaceSerialBand::solve(const FieldPerp& b, const FieldPerp& x0) { } // Outer boundary - if (outer_boundary_flags & INVERT_AC_GRAD) { + if (isOuterBoundaryFlagSet(INVERT_AC_GRAD)) { // Zero gradient at outer boundary for (int ix = 0; ix < xbndry; ix++) { A(ncx - ix, 1) = -1.0; } - } else if (outer_boundary_flags & INVERT_AC_LAP) { + } else if (isOuterBoundaryFlagSet(INVERT_AC_LAP)) { // Enforce zero laplacian for 2nd and 4th-order // NOTE: Currently ignoring XZ term and coef4 assumed zero on boundary // FIX THIS IF IT WORKS @@ -417,7 +417,7 @@ FieldPerp LaplaceSerialBand::solve(const FieldPerp& b, const FieldPerp& x0) { // Perform inversion cband_solve(A, localmesh->LocalNx, 2, 2, bk1d); - if ((global_flags & INVERT_KX_ZERO) && (iz == 0)) { + if (isGlobalFlagSet(INVERT_KX_ZERO) && (iz == 0)) { // Set the Kx = 0, n = 0 component to zero. For now just subtract // Should do in the inversion e.g. Sherman-Morrison formula @@ -440,7 +440,7 @@ FieldPerp LaplaceSerialBand::solve(const FieldPerp& b, const FieldPerp& x0) { // Done inversion, transform back for (int ix = 0; ix <= ncx; ix++) { - if (global_flags & INVERT_ZERO_DC) { + if (isGlobalFlagSet(INVERT_ZERO_DC)) { xk(ix, 0) = 0.0; } diff --git a/src/invert/laplace/impls/serial_tri/serial_tri.cxx b/src/invert/laplace/impls/serial_tri/serial_tri.cxx index 909a47f856..f46a0a46e5 100644 --- a/src/invert/laplace/impls/serial_tri/serial_tri.cxx +++ b/src/invert/laplace/impls/serial_tri/serial_tri.cxx @@ -91,13 +91,13 @@ FieldPerp LaplaceSerialTri::solve(const FieldPerp& b, const FieldPerp& x0) { int inbndry = localmesh->xstart, outbndry = localmesh->xstart; // If the flags to assign that only one guard cell should be used is set - if ((global_flags & INVERT_BOTH_BNDRY_ONE) || (localmesh->xstart < 2)) { + if (isGlobalFlagSet(INVERT_BOTH_BNDRY_ONE) || (localmesh->xstart < 2)) { inbndry = outbndry = 1; } - if (inner_boundary_flags & INVERT_BNDRY_ONE) { + if (isInnerBoundaryFlagSet(INVERT_BNDRY_ONE)) { inbndry = 1; } - if (outer_boundary_flags & INVERT_BNDRY_ONE) { + if (isOuterBoundaryFlagSet(INVERT_BNDRY_ONE)) { outbndry = 1; } @@ -140,8 +140,8 @@ FieldPerp LaplaceSerialTri::solve(const FieldPerp& b, const FieldPerp& x0) { * If the INVERT_SET flag is set (meaning that x0 will be used to set the * bounadry values), */ - if (((ix < inbndry) && (inner_boundary_flags & INVERT_SET)) - || ((ncx - 1 - ix < outbndry) && (outer_boundary_flags & INVERT_SET))) { + if (((ix < inbndry) && isInnerBoundaryFlagSet(INVERT_SET)) + || ((ncx - 1 - ix < outbndry) && (isOuterBoundaryFlagSet(INVERT_SET)))) { // Use the values in x0 in the boundary // x0 is the input @@ -185,8 +185,7 @@ FieldPerp LaplaceSerialTri::solve(const FieldPerp& b, const FieldPerp& x0) { kz, // wave number (different from kz only if we are taking a part // of the z-domain [and not from 0 to 2*pi]) - kz * kwaveFactor, global_flags, inner_boundary_flags, - outer_boundary_flags, &A, &C, &D); + kz * kwaveFactor, &A, &C, &D); ///////// PERFORM INVERSION ///////// if (!localmesh->periodicX) { @@ -208,7 +207,7 @@ FieldPerp LaplaceSerialTri::solve(const FieldPerp& b, const FieldPerp& x0) { } // If the global flag is set to INVERT_KX_ZERO - if ((global_flags & INVERT_KX_ZERO) && (kz == 0)) { + if (isGlobalFlagSet(INVERT_KX_ZERO) && (kz == 0)) { dcomplex offset(0.0); for (int ix = localmesh->xstart; ix <= localmesh->xend; ix++) { offset += xk1d[ix]; @@ -228,7 +227,7 @@ FieldPerp LaplaceSerialTri::solve(const FieldPerp& b, const FieldPerp& x0) { // Done inversion, transform back for (int ix = 0; ix < ncx; ix++) { - if (global_flags & INVERT_ZERO_DC) { + if (isGlobalFlagSet(INVERT_ZERO_DC)) { xk(ix, 0) = 0.0; } diff --git a/src/invert/laplace/impls/spt/spt.cxx b/src/invert/laplace/impls/spt/spt.cxx index 56ac496271..2e4c844c94 100644 --- a/src/invert/laplace/impls/spt/spt.cxx +++ b/src/invert/laplace/impls/spt/spt.cxx @@ -65,10 +65,9 @@ LaplaceSPT::LaplaceSPT(Options* opt, const CELL_LOC loc, Mesh* mesh_in, ye = localmesh->LocalNy - 1; // Contains upper boundary } - alldata = new SPT_data[ye - ys + 1]; - alldata -= ys; // Re-number indices to start at ys + alldata.reallocate(ye - ys + 1); for (int jy = ys; jy <= ye; jy++) { - alldata[jy].comm_tag = SPT_DATA + jy; // Give each one a different tag + alldata[jy - ys].comm_tag = SPT_DATA + jy; // Give each one a different tag } // Temporary array for taking FFTs @@ -76,11 +75,6 @@ LaplaceSPT::LaplaceSPT(Options* opt, const CELL_LOC loc, Mesh* mesh_in, dc1d.reallocate(ncz / 2 + 1); } -LaplaceSPT::~LaplaceSPT() { - alldata += ys; // Return to index from 0 - delete[] alldata; -} - FieldPerp LaplaceSPT::solve(const FieldPerp& b) { return solve(b, b); } FieldPerp LaplaceSPT::solve(const FieldPerp& b, const FieldPerp& x0) { @@ -90,15 +84,15 @@ FieldPerp LaplaceSPT::solve(const FieldPerp& b, const FieldPerp& x0) { FieldPerp x{emptyFrom(b)}; - if ((inner_boundary_flags & INVERT_SET) || (outer_boundary_flags & INVERT_SET)) { + if (isInnerBoundaryFlagSet(INVERT_SET) || isOuterBoundaryFlagSet(INVERT_SET)) { FieldPerp bs = copy(b); int xbndry = localmesh->xstart; // If the flags to assign that only one guard cell should be used is set - if ((global_flags & INVERT_BOTH_BNDRY_ONE) || (localmesh->xstart < 2)) { + if (isGlobalFlagSet(INVERT_BOTH_BNDRY_ONE) || (localmesh->xstart < 2)) { xbndry = 1; } - if ((inner_boundary_flags & INVERT_SET) && localmesh->firstX()) { + if (isInnerBoundaryFlagSetOnFirstX(INVERT_SET)) { // Copy x0 inner boundary into bs for (int ix = 0; ix < xbndry; ix++) { for (int iz = 0; iz < localmesh->LocalNz; iz++) { @@ -106,7 +100,7 @@ FieldPerp LaplaceSPT::solve(const FieldPerp& b, const FieldPerp& x0) { } } } - if ((outer_boundary_flags & INVERT_SET) && localmesh->lastX()) { + if (isOuterBoundaryFlagSetOnLastX(INVERT_SET)) { // Copy x0 outer boundary into bs for (int ix = localmesh->LocalNx - 1; ix >= localmesh->LocalNx - xbndry; ix--) { for (int iz = 0; iz < localmesh->LocalNz; iz++) { @@ -141,29 +135,29 @@ Field3D LaplaceSPT::solve(const Field3D& b) { for (int jy = ys; jy <= ye; jy++) { // And start another one going - start(sliceXZ(b, jy), alldata[jy]); + start(sliceXZ(b, jy), alldata[jy - ys]); // Move each calculation along one processor for (int jy2 = ys; jy2 < jy; jy2++) { - next(alldata[jy2]); + next(alldata[jy2 - ys]); } } bool running = true; - do { + while (running) { // Move each calculation along until the last one is finished - for (int jy = ys; jy <= ye; jy++) { - running = next(alldata[jy]) == 0; + for (auto& data : alldata) { + running = next(data) == 0; } - } while (running); + } FieldPerp xperp(localmesh); xperp.setLocation(location); xperp.allocate(); // All calculations finished. Get result - for (int jy = ys; jy <= ye; jy++) { - finish(alldata[jy], xperp); + for (auto& data : alldata) { + finish(data, xperp); x = xperp; } @@ -173,17 +167,17 @@ Field3D LaplaceSPT::solve(const Field3D& b) { Field3D LaplaceSPT::solve(const Field3D& b, const Field3D& x0) { ASSERT1(localmesh == b.getMesh() && localmesh == x0.getMesh()); - if (((inner_boundary_flags & INVERT_SET) && localmesh->firstX()) - || ((outer_boundary_flags & INVERT_SET) && localmesh->lastX())) { + if ((isInnerBoundaryFlagSetOnFirstX(INVERT_SET)) + || isOuterBoundaryFlagSetOnLastX(INVERT_SET)) { Field3D bs = copy(b); int xbndry = localmesh->xstart; // If the flags to assign that only one guard cell should be used is set - if ((global_flags & INVERT_BOTH_BNDRY_ONE) || (localmesh->xstart < 2)) { + if (isGlobalFlagSet(INVERT_BOTH_BNDRY_ONE) || (localmesh->xstart < 2)) { xbndry = 1; } - if ((inner_boundary_flags & INVERT_SET) && localmesh->firstX()) { + if (isInnerBoundaryFlagSetOnFirstX(INVERT_SET)) { // Copy x0 inner boundary into bs for (int ix = 0; ix < xbndry; ix++) { for (int iy = 0; iy < localmesh->LocalNy; iy++) { @@ -193,7 +187,7 @@ Field3D LaplaceSPT::solve(const Field3D& b, const Field3D& x0) { } } } - if ((outer_boundary_flags & INVERT_SET) && localmesh->lastX()) { + if (isOuterBoundaryFlagSetOnLastX(INVERT_SET)) { // Copy x0 outer boundary into bs for (int ix = localmesh->LocalNx - 1; ix >= localmesh->LocalNx - xbndry; ix--) { for (int iy = 0; iy < localmesh->LocalNy; iy++) { @@ -323,8 +317,7 @@ int LaplaceSPT::start(const FieldPerp& b, SPT_data& data) { /// Set matrix elements for (int kz = 0; kz <= maxmode; kz++) { tridagMatrix(&data.avec(kz, 0), &data.bvec(kz, 0), &data.cvec(kz, 0), &data.bk(kz, 0), - data.jy, kz, kz * kwaveFactor, global_flags, inner_boundary_flags, - outer_boundary_flags, &Acoef, &Ccoef, &Dcoef); + data.jy, kz, kz * kwaveFactor, &Acoef, &Ccoef, &Dcoef); } data.proc = 0; //< Starts at processor 0 @@ -516,7 +509,7 @@ void LaplaceSPT::finish(SPT_data& data, FieldPerp& x) { dc1d[kz] = 0.0; } - if (global_flags & INVERT_ZERO_DC) { + if (isGlobalFlagSet(INVERT_ZERO_DC)) { dc1d[0] = 0.0; } diff --git a/src/invert/laplace/impls/spt/spt.hxx b/src/invert/laplace/impls/spt/spt.hxx index c6aa8fd404..a9d5b2583f 100644 --- a/src/invert/laplace/impls/spt/spt.hxx +++ b/src/invert/laplace/impls/spt/spt.hxx @@ -69,7 +69,6 @@ class LaplaceSPT : public Laplacian { public: LaplaceSPT(Options* opt = nullptr, const CELL_LOC = CELL_CENTRE, Mesh* mesh_in = nullptr, Solver* solver = nullptr); - ~LaplaceSPT(); using Laplacian::setCoefA; void setCoefA(const Field2D& val) override { @@ -106,17 +105,15 @@ public: Field3D solve(const Field3D& b, const Field3D& x0) override; private: - enum { SPT_DATA = 1123 }; ///< 'magic' number for SPT MPI messages + constexpr static int SPT_DATA = 1123; ///< 'magic' number for SPT MPI messages Field2D Acoef, Ccoef, Dcoef; /// Data structure for SPT algorithm struct SPT_data { - SPT_data() : comm_tag(SPT_DATA) {} void allocate(int mm, int nx); // Allocates memory - ~SPT_data(){}; // Free memory - int jy; ///< Y index + int jy = 0; ///< Y index Matrix bk; ///< b vector in Fourier space Matrix xk; @@ -125,19 +122,19 @@ private: Matrix avec, bvec, cvec; ///< Diagonal bands of matrix - int proc; // Which processor has this reached? - int dir; // Which direction is it going? + int proc = 0; // Which processor has this reached? + int dir = 1; // Which direction is it going? - comm_handle recv_handle; // Handle for receives + comm_handle recv_handle = nullptr; // Handle for receives - int comm_tag; // Tag for communication + int comm_tag = SPT_DATA; // Tag for communication Array buffer; }; int ys, ye; // Range of Y indices SPT_data slicedata; // Used to solve for a single FieldPerp - SPT_data* alldata; // Used to solve a Field3D + Array alldata; // Used to solve a Field3D Array dc1d; ///< 1D in Z for taking FFTs diff --git a/src/invert/laplace/invert_laplace.cxx b/src/invert/laplace/invert_laplace.cxx index 57594953a7..e317c767e5 100644 --- a/src/invert/laplace/invert_laplace.cxx +++ b/src/invert/laplace/invert_laplace.cxx @@ -424,20 +424,16 @@ void Laplacian::tridagCoefs(int jx, int jy, BoutReal kwave, dcomplex& a, dcomple #if BOUT_USE_METRIC_3D void Laplacian::tridagMatrix(dcomplex* /*avec*/, dcomplex* /*bvec*/, dcomplex* /*cvec*/, dcomplex* /*bk*/, int /*jy*/, int /*kz*/, BoutReal /*kwave*/, - int /*global_flags*/, int /*inner_boundary_flags*/, - int /*outer_boundary_flags*/, const Field2D* /*a*/, - const Field2D* /*c1coef*/, const Field2D* /*c2coef*/, - const Field2D* /*d*/, bool /*includeguards*/, - bool /*zperiodic*/) { + const Field2D* /*a*/, const Field2D* /*c1coef*/, + const Field2D* /*c2coef*/, const Field2D* /*d*/, + bool /*includeguards*/, bool /*zperiodic*/) { throw BoutException("Error: tridagMatrix does not yet work with 3D metric."); } #else void Laplacian::tridagMatrix(dcomplex* avec, dcomplex* bvec, dcomplex* cvec, dcomplex* bk, - int jy, int kz, BoutReal kwave, int global_flags, - int inner_boundary_flags, int outer_boundary_flags, - const Field2D* a, const Field2D* c1coef, - const Field2D* c2coef, const Field2D* d, bool includeguards, - bool zperiodic) { + int jy, int kz, BoutReal kwave, const Field2D* a, + const Field2D* c1coef, const Field2D* c2coef, + const Field2D* d, bool includeguards, bool zperiodic) { ASSERT1(a->getLocation() == location); ASSERT1(c1coef->getLocation() == location); ASSERT1(c2coef->getLocation() == location); @@ -469,13 +465,13 @@ void Laplacian::tridagMatrix(dcomplex* avec, dcomplex* bvec, dcomplex* cvec, dco int inbndry = localmesh->xstart, outbndry = localmesh->xstart; // If the flags to assign that only one guard cell should be used is set - if ((global_flags & INVERT_BOTH_BNDRY_ONE) || (localmesh->xstart < 2)) { + if (isGlobalFlagSet(INVERT_BOTH_BNDRY_ONE) || (localmesh->xstart < 2)) { inbndry = outbndry = 1; } - if (inner_boundary_flags & INVERT_BNDRY_ONE) { + if (isInnerBoundaryFlagSet(INVERT_BNDRY_ONE)) { inbndry = 1; } - if (outer_boundary_flags & INVERT_BNDRY_ONE) { + if (isOuterBoundaryFlagSet(INVERT_BNDRY_ONE)) { outbndry = 1; } @@ -497,7 +493,7 @@ void Laplacian::tridagMatrix(dcomplex* avec, dcomplex* bvec, dcomplex* cvec, dco // If no user specified value is set on inner boundary, set the first // element in b (in the equation AX=b) to 0 - if (!(inner_boundary_flags & (INVERT_RHS | INVERT_SET))) { + if (!isInnerBoundaryFlagSet(INVERT_RHS | INVERT_SET)) { for (int ix = 0; ix < inbndry; ix++) { bk[ix] = 0.; } @@ -506,34 +502,35 @@ void Laplacian::tridagMatrix(dcomplex* avec, dcomplex* bvec, dcomplex* cvec, dco // DC i.e. kz = 0 (the offset mode) if (kz == 0) { - if (inner_boundary_flags & INVERT_DC_GRAD - && (inner_boundary_flags & INVERT_SET || inner_boundary_flags & INVERT_RHS)) { + if (isInnerBoundaryFlagSet(INVERT_DC_GRAD) + && (isInnerBoundaryFlagSet(INVERT_SET) + || isInnerBoundaryFlagSet(INVERT_RHS))) { // Zero gradient at inner boundary for (int ix = 0; ix < inbndry; ix++) { avec[ix] = 0.; bvec[ix] = -1. / sqrt(coords->g_11()(ix, jy)) / coords->dx(ix, jy); cvec[ix] = 1. / sqrt(coords->g_11()(ix, jy)) / coords->dx(ix, jy); } - } else if (inner_boundary_flags & INVERT_DC_GRAD) { + } else if (isInnerBoundaryFlagSet(INVERT_DC_GRAD)) { // Zero gradient at inner boundary for (int ix = 0; ix < inbndry; ix++) { avec[ix] = 0.; bvec[ix] = -1.; cvec[ix] = 1.; } - } else if (inner_boundary_flags & INVERT_DC_GRADPAR) { + } else if (isInnerBoundaryFlagSet(INVERT_DC_GRADPAR)) { for (int ix = 0; ix < inbndry; ix++) { avec[ix] = 0.0; bvec[ix] = 1.0 / sqrt(coords->g_22()(ix, jy)); cvec[ix] = -1.0 / sqrt(coords->g_22()(ix + 1, jy)); } - } else if (inner_boundary_flags & INVERT_DC_GRADPARINV) { + } else if (isInnerBoundaryFlagSet(INVERT_DC_GRADPARINV)) { for (int ix = 0; ix < inbndry; ix++) { avec[ix] = 0.0; bvec[ix] = sqrt(coords->g_22()(ix, jy)); cvec[ix] = -sqrt(coords->g_22()(ix + 1, jy)); } - } else if (inner_boundary_flags & INVERT_DC_LAP) { + } else if (isInnerBoundaryFlagSet(INVERT_DC_LAP)) { // Decaying boundary conditions BoutReal k = 0.0; if (a != nullptr) { @@ -548,7 +545,7 @@ void Laplacian::tridagMatrix(dcomplex* avec, dcomplex* bvec, dcomplex* cvec, dco bvec[ix] = 1.; cvec[ix] = -exp(-k * coords->dx(ix, jy) / sqrt(coords->g11()(ix, jy))); } - } else if (inner_boundary_flags & INVERT_IN_CYLINDER) { + } else if (isInnerBoundaryFlagSet(INVERT_IN_CYLINDER)) { // Condition for inner radial boundary for cylindrical coordinates /* Explanation: * The discrete fourier transform is defined as @@ -602,8 +599,9 @@ void Laplacian::tridagMatrix(dcomplex* avec, dcomplex* bvec, dcomplex* cvec, dco // AC i.e. kz =/= 0 (all other modes than the offset mode) else { - if (inner_boundary_flags & INVERT_AC_GRAD - && (inner_boundary_flags & INVERT_SET || inner_boundary_flags & INVERT_RHS)) { + if (isInnerBoundaryFlagSet(INVERT_AC_GRAD) + && (isInnerBoundaryFlagSet(INVERT_SET) + || isInnerBoundaryFlagSet(INVERT_RHS))) { // Zero gradient at inner boundary for (int ix = 0; ix < inbndry; ix++) { avec[ix] = dcomplex(0., 0.); @@ -612,14 +610,14 @@ void Laplacian::tridagMatrix(dcomplex* avec, dcomplex* bvec, dcomplex* cvec, dco cvec[ix] = dcomplex(1., 0.) / sqrt(coords->g_11()(ix, jy)) / coords->dx(ix, jy); } - } else if (inner_boundary_flags & INVERT_AC_GRAD) { + } else if (isInnerBoundaryFlagSet(INVERT_AC_GRAD)) { // Zero gradient at inner boundary for (int ix = 0; ix < inbndry; ix++) { avec[ix] = dcomplex(0., 0.); bvec[ix] = dcomplex(-1., 0.); cvec[ix] = dcomplex(1., 0.); } - } else if (inner_boundary_flags & INVERT_AC_LAP) { + } else if (isInnerBoundaryFlagSet(INVERT_AC_LAP)) { // Use decaying zero-Laplacian solution in the boundary for (int ix = 0; ix < inbndry; ix++) { avec[ix] = 0.0; @@ -627,9 +625,9 @@ void Laplacian::tridagMatrix(dcomplex* avec, dcomplex* bvec, dcomplex* cvec, dco cvec[ix] = -exp(-1.0 * sqrt(coords->g33()(ix, jy) / coords->g11()(ix, jy)) * kwave * coords->dx(ix, jy)); } - } else if (inner_boundary_flags & INVERT_IN_CYLINDER) { + } else if (isInnerBoundaryFlagSet(INVERT_IN_CYLINDER)) { // Condition for inner radial boundary for cylindrical coordinates - // Explanation under "if (inner_boundary_flags & INVERT_IN_CYLINDER)" + // Explanation under "if (isInnerBoundaryFlagSet(INVERT_IN_CYLINDER))" for (int ix = 0; ix < inbndry; ix++) { avec[ix] = 0.; bvec[ix] = 1.; @@ -656,7 +654,7 @@ void Laplacian::tridagMatrix(dcomplex* avec, dcomplex* bvec, dcomplex* cvec, dco // If no user specified value is set on outer boundary, set the last // element in b (in the equation AX=b) to 0 - if (!(outer_boundary_flags & (INVERT_RHS | INVERT_SET))) { + if (!isOuterBoundaryFlagSet(INVERT_RHS | INVERT_SET)) { for (int ix = 0; ix < outbndry; ix++) { bk[ncx - ix] = 0.; } @@ -665,8 +663,9 @@ void Laplacian::tridagMatrix(dcomplex* avec, dcomplex* bvec, dcomplex* cvec, dco // DC i.e. kz = 0 (the offset mode) if (kz == 0) { - if (outer_boundary_flags & INVERT_DC_GRAD - && (outer_boundary_flags & INVERT_SET || outer_boundary_flags & INVERT_RHS)) { + if (isOuterBoundaryFlagSet(INVERT_DC_GRAD) + && (isOuterBoundaryFlagSet(INVERT_SET) + || isOuterBoundaryFlagSet(INVERT_RHS))) { // Zero gradient at outer boundary for (int ix = 0; ix < outbndry; ix++) { avec[ncx - ix] = dcomplex(-1., 0.) / sqrt(coords->g_11()(xe - ix, jy)) @@ -675,26 +674,26 @@ void Laplacian::tridagMatrix(dcomplex* avec, dcomplex* bvec, dcomplex* cvec, dco / coords->dx(xe - ix, jy); cvec[ncx - ix] = dcomplex(0., 0.); } - } else if (outer_boundary_flags & INVERT_DC_GRAD) { + } else if (isOuterBoundaryFlagSet(INVERT_DC_GRAD)) { // Zero gradient at outer boundary for (int ix = 0; ix < outbndry; ix++) { avec[ncx - ix] = dcomplex(1., 0.); bvec[ncx - ix] = dcomplex(-1., 0.); cvec[ncx - ix] = dcomplex(0., 0.); } - } else if (outer_boundary_flags & INVERT_DC_GRADPAR) { + } else if (isOuterBoundaryFlagSet(INVERT_DC_GRADPAR)) { for (int ix = 0; ix < inbndry; ix++) { avec[ncx - ix] = 1.0 / sqrt(coords->g_22()(xe - ix - 1, jy)); bvec[ncx - ix] = -1.0 / sqrt(coords->g_22()(xe - ix, jy)); cvec[ncx - ix] = 0.0; } - } else if (outer_boundary_flags & INVERT_DC_GRADPARINV) { + } else if (isOuterBoundaryFlagSet(INVERT_DC_GRADPARINV)) { for (int ix = 0; ix < inbndry; ix++) { avec[ncx - ix] = sqrt(coords->g_22()(xe - ix - 1, jy)); bvec[ncx - ix] = -sqrt(coords->g_22()(xe - ix, jy)); cvec[ncx - ix] = 0.0; } - } else if (outer_boundary_flags & INVERT_DC_LAP) { + } else if (isOuterBoundaryFlagSet(INVERT_DC_LAP)) { // Decaying boundary conditions BoutReal k = 0.0; if (a != nullptr) { @@ -723,8 +722,9 @@ void Laplacian::tridagMatrix(dcomplex* avec, dcomplex* bvec, dcomplex* cvec, dco // AC i.e. kz =/= 0 (all other modes than the offset mode) else { - if (outer_boundary_flags & INVERT_AC_GRAD - && (outer_boundary_flags & INVERT_SET || outer_boundary_flags & INVERT_RHS)) { + if (isOuterBoundaryFlagSet(INVERT_AC_GRAD) + && (isOuterBoundaryFlagSet(INVERT_SET) + || isOuterBoundaryFlagSet(INVERT_RHS))) { // Zero gradient at outer boundary for (int ix = 0; ix < outbndry; ix++) { avec[ncx - ix] = dcomplex(-1., 0.) / sqrt(coords->g_11()(xe - ix, jy)) @@ -733,14 +733,14 @@ void Laplacian::tridagMatrix(dcomplex* avec, dcomplex* bvec, dcomplex* cvec, dco / coords->dx(xe - ix, jy); cvec[ncx - ix] = dcomplex(0., 0.); } - } else if (outer_boundary_flags & INVERT_AC_GRAD) { + } else if (isOuterBoundaryFlagSet(INVERT_AC_GRAD)) { // Zero gradient at outer boundary for (int ix = 0; ix < outbndry; ix++) { avec[ncx - ix] = dcomplex(1., 0.); bvec[ncx - ix] = dcomplex(-1., 0.); cvec[ncx - ix] = dcomplex(0., 0.); } - } else if (outer_boundary_flags & INVERT_AC_LAP) { + } else if (isOuterBoundaryFlagSet(INVERT_AC_LAP)) { // Use decaying zero-Laplacian solution in the boundary for (int ix = 0; ix < outbndry; ix++) { avec[ncx - ix] = @@ -796,6 +796,13 @@ void Laplacian::LaplacianMonitor::outputVars(Options& output_options, laplacian->outputVars(output_options, time_dimension); } +bool Laplacian::isInnerBoundaryFlagSetOnFirstX(int flag) const { + return isInnerBoundaryFlagSet(flag) and localmesh->firstX(); +} +bool Laplacian::isOuterBoundaryFlagSetOnLastX(int flag) const { + return isOuterBoundaryFlagSet(flag) and localmesh->lastX(); +} + /********************************************************************************** * LEGACY INTERFACE * diff --git a/tests/integrated/test-petsc_laplace/test_petsc_laplace.cxx b/tests/integrated/test-petsc_laplace/test_petsc_laplace.cxx index bfd394194f..1e3cdde310 100644 --- a/tests/integrated/test-petsc_laplace/test_petsc_laplace.cxx +++ b/tests/integrated/test-petsc_laplace/test_petsc_laplace.cxx @@ -23,15 +23,90 @@ * **************************************************************************/ -#include -#include -// #include -#include -#include -#include +#include "bout/bout.hxx" // NOLINT +#include "bout/bout_types.hxx" +#include "bout/boutexception.hxx" +#include "bout/constants.hxx" +#include "bout/difops.hxx" +#include "bout/field2d.hxx" +#include "bout/field3d.hxx" +#include "bout/invert_laplace.hxx" +#include "bout/options.hxx" +#include "bout/options_io.hxx" +#include "bout/output.hxx" +#include "bout/traits.hxx" + +#include "fmt/core.h" +#include + #include +#include BoutReal max_error_at_ystart(const Field3D& error); +void apply_flat_boundary(Field3D& bcoef); + +template +void check_laplace(int test_num, std::string_view test_name, Laplacian& invert, + int inner_flags, int outer_flags, const T& acoef, const T& ccoef, + const T& dcoef, const U& bcoef, const Field3D& field, int ystart, + Options& dump) { + static_assert(bout::utils::is_Field_v, "check_laplace requires Field2D or Field3D"); + static_assert(bout::utils::is_Field_v, "check_laplace requires Field2D or Field3D"); + + invert.setInnerBoundaryFlags(inner_flags); + invert.setOuterBoundaryFlags(outer_flags); + invert.setCoefA(acoef); + invert.setCoefC(ccoef); + invert.setCoefD(dcoef); + + checkData(bcoef); + + Field3D sol; + Field3D error; + Field3D abs_error; + BoutReal max_error = -1; + + try { + sol = invert.solve(sliceXZ(bcoef, ystart)); + error = (field - sol) / field; + abs_error = field - sol; + max_error = max_error_at_ystart(abs(abs_error)); + } catch (BoutException& err) { + output.write("BoutException occured in invert->solve(b1): {}\n", err.what()); + } + + output.write("\nTest {}: {}\n", test_num, test_name); + output.write("Magnitude of maximum absolute error is {}\n", max_error); + + dump[fmt::format("a{}", test_num)] = acoef; + dump[fmt::format("b{}", test_num)] = bcoef; + dump[fmt::format("c{}", test_num)] = ccoef; + dump[fmt::format("d{}", test_num)] = dcoef; + dump[fmt::format("f{}", test_num)] = field; + dump[fmt::format("sol{}", test_num)] = sol; + dump[fmt::format("error{}", test_num)] = error; + dump[fmt::format("absolute_error{}", test_num)] = abs_error; + dump[fmt::format("max_error{}", test_num)] = max_error; +} + +template +Field3D forward_laplace(const Field3D& field, const T& acoef, const T& ccoef, + const T& dcoef) { + auto bcoef = + dcoef * Delp2(field) + Grad_perp(ccoef) * Grad_perp(field) / ccoef + acoef * field; + apply_flat_boundary(bcoef); + return bcoef; +} + +Field3D generate_f1(const Mesh& mesh); +Field3D generate_a1(const Mesh& mesh); +Field3D generate_c1(const Mesh& mesh); +Field3D generate_d1(const Mesh& mesh); + +Field3D generate_f5(const Mesh& mesh); +Field3D generate_a5(const Mesh& mesh); +Field3D generate_c5(const Mesh& mesh); +Field3D generate_d5(const Mesh& mesh); int main(int argc, char** argv) { @@ -42,829 +117,553 @@ int main(int argc, char** argv) { options = Options::getRoot()->getSection("petsc4th"); auto invert_4th = Laplacian::create(options); - // Solving equations of the form d*Delp2(f) + 1/c*Grad_perp(c).Grad_perp(f) + a*f = b for various f, a, c, d - Field3D f1, a1, b1, c1, d1, sol1; - BoutReal p, q; //Use to set parameters in constructing trial functions - Field3D error1, - absolute_error1; //Absolute value of relative error: abs( (f1-sol1)/f1 ) - BoutReal max_error1; //Output of test + Options dump; + // Solving equations of the form d*Delp2(f) + 1/c*Grad_perp(c).Grad_perp(f) + a*f = b for various f, a, c, d using bout::globals::mesh; // Only Neumann x-boundary conditions are implemented so far, so test functions should be Neumann in x and periodic in z. // Use Field3D's, but solver only works on FieldPerp slices, so only use 1 y-point - BoutReal nx = mesh->GlobalNx - 2 * mesh->xstart - 1; - BoutReal nz = mesh->GlobalNz; - //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + ///////////////////////////////////////////////////// // Test 1: Gaussian x-profiles, 2nd order Krylov - p = 0.39503274; - q = 0.20974396; - f1.allocate(); - for (int jx = mesh->xstart; jx <= mesh->xend; jx++) { - for (int jy = 0; jy < mesh->LocalNy; jy++) { - for (int jz = 0; jz < mesh->LocalNz; jz++) { - BoutReal x = BoutReal(mesh->getGlobalXIndex(jx) - mesh->xstart) / nx; - BoutReal z = BoutReal(jz) / nz; - f1(jx, jy, jz) = - 0. + exp(-(100. * pow(x - p, 2) + 1. - cos(2. * PI * (z - q)))) - - 50. - * (2. * p * exp(-100. * pow(-p, 2)) * x - + (-p * exp(-100. * pow(-p, 2)) - - (1 - p) * exp(-100. * pow(1 - p, 2))) - * pow(x, 2)) - * exp(-( - 1. - - cos(2. * PI - * (z - q)))) //make the gradients zero at both x-boundaries - ; - ASSERT0(finite(f1(jx, jy, jz))); - } - } - } - if (mesh->firstX()) { - for (int jx = mesh->xstart - 1; jx >= 0; jx--) { - for (int jy = 0; jy < mesh->LocalNy; jy++) { - for (int jz = 0; jz < mesh->LocalNz; jz++) { - BoutReal x = BoutReal(mesh->getGlobalXIndex(jx) - mesh->xstart) / nx; - BoutReal z = BoutReal(jz) / nz; - f1(jx, jy, jz) = - 0. + exp(-(60. * pow(x - p, 2) + 1. - cos(2. * PI * (z - q)))) - - 50. - * (2. * p * exp(-60. * pow(-p, 2)) * x - + (-p * exp(-60. * pow(-p, 2)) - - (1 - p) * exp(-60. * pow(1 - p, 2))) - * pow(x, 2)) - * exp(-( - 1. - - cos( - 2. * PI - * (z - q)))); //make the gradients zero at both x-boundaries - ASSERT0(finite(f1(jx, jy, jz))); - } - } - } - } - if (mesh->lastX()) { - for (int jx = mesh->xend + 1; jx < mesh->LocalNx; jx++) { - for (int jy = 0; jy < mesh->LocalNy; jy++) { - for (int jz = 0; jz < mesh->LocalNz; jz++) { - BoutReal x = BoutReal(mesh->getGlobalXIndex(jx) - mesh->xstart) / nx; - BoutReal z = BoutReal(jz) / nz; - f1(jx, jy, jz) = - 0. + exp(-(60. * pow(x - p, 2) + 1. - cos(2. * PI * (z - q)))) - - 50. - * (2. * p * exp(-60. * pow(-p, 2)) * x - + (-p * exp(-60. * pow(-p, 2)) - - (1 - p) * exp(-60. * pow(1 - p, 2))) - * pow(x, 2)) - * exp(-( - 1. - - cos( - 2. * PI - * (z - q)))); //make the gradients zero at both x-boundaries - ASSERT0(finite(f1(jx, jy, jz))); - } - } - } - } + Field3D f_1 = generate_f1(*mesh); + Field3D a_1 = generate_a1(*mesh); + Field3D c_1 = generate_c1(*mesh); + Field3D d_1 = generate_d1(*mesh); - f1.applyBoundary("neumann"); - - p = 0.512547; - q = 0.30908712; - d1.allocate(); - for (int jx = mesh->xstart; jx <= mesh->xend; jx++) { - for (int jy = 0; jy < mesh->LocalNy; jy++) { - for (int jz = 0; jz < mesh->LocalNz; jz++) { - BoutReal x = BoutReal(mesh->getGlobalXIndex(jx) - mesh->xstart) / nx; - BoutReal z = BoutReal(jz) / nz; - d1(jx, jy, jz) = - 1. + 0.2 * exp(-50. * pow(x - p, 2) / 4.) * sin(2. * PI * (z - q) * 3.); - } - } - } - if (mesh->firstX()) { - for (int jx = mesh->xstart - 1; jx >= 0; jx--) { - for (int jy = 0; jy < mesh->LocalNy; jy++) { - for (int jz = 0; jz < mesh->LocalNz; jz++) { - BoutReal x = BoutReal(mesh->getGlobalXIndex(jx) - mesh->xstart) / nx; - BoutReal z = BoutReal(jz) / nz; - d1(jx, jy, jz) = - 1. + 0.2 * exp(-50. * pow(x - p, 2) / 4.) * sin(2. * PI * (z - q) * 3.); - // d1(jx, jy, jz) = d1(jx+1, jy, jz); - } - } - } - } - if (mesh->lastX()) { - for (int jx = mesh->xend + 1; jx < mesh->LocalNx; jx++) { - for (int jy = 0; jy < mesh->LocalNy; jy++) { - for (int jz = 0; jz < mesh->LocalNz; jz++) { - BoutReal x = BoutReal(mesh->getGlobalXIndex(jx) - mesh->xstart) / nx; - BoutReal z = BoutReal(jz) / nz; - d1(jx, jy, jz) = - 1. + 0.2 * exp(-50. * pow(x - p, 2) / 4.) * sin(2. * PI * (z - q) * 3.); - // d1(jx, jy, jz) = d1(jx-1, jy, jz); - } - } - } - } + mesh->communicate(f_1, a_1, c_1, d_1); - p = 0.18439023; - q = 0.401089473; - c1.allocate(); - for (int jx = mesh->xstart; jx <= mesh->xend; jx++) { - for (int jy = 0; jy < mesh->LocalNy; jy++) { - for (int jz = 0; jz < mesh->LocalNz; jz++) { - BoutReal x = BoutReal(mesh->getGlobalXIndex(jx) - mesh->xstart) / nx; - BoutReal z = BoutReal(jz) / nz; - c1(jx, jy, jz) = - 1. + 0.15 * exp(-50. * pow(x - p, 2) * 2.) * sin(2. * PI * (z - q) * 2.); - } - } - } - if (mesh->firstX()) { - for (int jx = mesh->xstart - 1; jx >= 0; jx--) { - for (int jy = 0; jy < mesh->LocalNy; jy++) { - for (int jz = 0; jz < mesh->LocalNz; jz++) { - BoutReal x = BoutReal(mesh->getGlobalXIndex(jx) - mesh->xstart) / nx; - BoutReal z = BoutReal(jz) / nz; - c1(jx, jy, jz) = - 1. + 0.15 * exp(-50. * pow(x - p, 2) * 2.) * sin(2. * PI * (z - q) * 2.); - // c1(jx, jy, jz) = c1(jx+1, jy, jz); - } - } - } - } - if (mesh->lastX()) { - for (int jx = mesh->xend + 1; jx < mesh->LocalNx; jx++) { - for (int jy = 0; jy < mesh->LocalNy; jy++) { - for (int jz = 0; jz < mesh->LocalNz; jz++) { - BoutReal x = BoutReal(mesh->getGlobalXIndex(jx) - mesh->xstart) / nx; - BoutReal z = BoutReal(jz) / nz; - c1(jx, jy, jz) = - 1. + 0.15 * exp(-50. * pow(x - p, 2) * 2.) * sin(2. * PI * (z - q) * 2.); - // c1(jx, jy, jz) = c1(jx-1, jy, jz); - } - } + const Field3D b_1 = forward_laplace(f_1, a_1, c_1, d_1); + + int test_num = 0; + check_laplace(++test_num, "PETSc 2nd order", *invert, INVERT_AC_GRAD, INVERT_AC_GRAD, + a_1, c_1, d_1, b_1, f_1, mesh->ystart, dump); + + ///////////////////////////////////////////////// + // Test 2: Gaussian x-profiles, 4th order Krylov + + check_laplace(++test_num, "PETSc 4th order", *invert_4th, INVERT_AC_GRAD, + INVERT_AC_GRAD, a_1, c_1, d_1, b_1, f_1, mesh->ystart, dump); + + //////////////////////////////////////////////////////////////////////////////////////// + // Test 3+4: Gaussian x-profiles, z-independent coefficients and compare with SPT method + + const Field2D a_3 = DC(a_1); + const Field2D c_3 = DC(c_1); + const Field2D d_3 = DC(d_1); + const Field3D b_3 = forward_laplace(f_1, a_3, c_3, d_3); + + check_laplace(++test_num, "with coefficients constant in z, PETSc 2nd order", *invert, + INVERT_AC_GRAD, INVERT_AC_GRAD, a_3, c_3, d_3, b_3, f_1, mesh->ystart, + dump); + + Options* SPT_options = Options::getRoot()->getSection("SPT"); + auto invert_SPT = Laplacian::create(SPT_options); + + check_laplace(++test_num, "with coefficients constant in z, default solver", + *invert_SPT, INVERT_AC_GRAD, INVERT_AC_GRAD | INVERT_DC_GRAD, a_3, c_3, + d_3, b_3, f_1, mesh->ystart, dump); + + ////////////////////////////////////////////// + // Test 5: Cosine x-profiles, 2nd order Krylov + Field3D f_5 = generate_f5(*mesh); + Field3D a_5 = generate_a5(*mesh); + Field3D c_5 = generate_c5(*mesh); + Field3D d_5 = generate_d5(*mesh); + + mesh->communicate(f_5, a_5, c_5, d_5); + + const Field3D b_5 = forward_laplace(f_5, a_5, c_5, d_5); + + check_laplace(++test_num, "different profiles, PETSc 2nd order", *invert, + INVERT_AC_GRAD, INVERT_AC_GRAD, a_5, c_5, d_5, b_5, f_5, mesh->ystart, + dump); + + ////////////////////////////////////////////// + // Test 6: Cosine x-profiles, 4th order Krylov + + check_laplace(++test_num, "different profiles, PETSc 4th order", *invert_4th, + INVERT_AC_GRAD, INVERT_AC_GRAD, a_5, c_5, d_5, b_5, f_5, mesh->ystart, + dump); + + ////////////////////////////////////////////////////////////////////////////////////// + // Test 7+8: Cosine x-profiles, z-independent coefficients and compare with SPT method + + const Field2D a_7 = DC(a_5); + const Field2D c_7 = DC(c_5); + const Field2D d_7 = DC(d_5); + const Field3D b_7 = forward_laplace(f_5, a_7, c_7, d_7); + + check_laplace(++test_num, + "different profiles, with coefficients constant in z, PETSc 2nd order", + *invert, INVERT_AC_GRAD, INVERT_AC_GRAD, a_7, c_7, d_7, b_7, f_5, + mesh->ystart, dump); + + check_laplace(++test_num, + "different profiles, with coefficients constant in z, default solver", + *invert_SPT, INVERT_AC_GRAD, INVERT_AC_GRAD | INVERT_DC_GRAD, a_7, c_7, + d_7, b_7, f_5, mesh->ystart, dump); + + // Write and close the output file + bout::writeDefaultOutputFile(dump); + + MPI_Barrier(BoutComm::get()); // Wait for all processors to write data + } + + bout::checkForUnusedOptions(); + + BoutFinalise(); + return 0; +} + +BoutReal max_error_at_ystart(const Field3D& error) { + const auto* mesh = error.getMesh(); + BoutReal local_max_error = error(mesh->xstart, mesh->ystart, 0); + + for (int jx = mesh->xstart; jx <= mesh->xend; jx++) { + for (int jz = 0; jz < mesh->LocalNz; jz++) { + if (local_max_error < error(jx, mesh->ystart, jz)) { + local_max_error = error(jx, mesh->ystart, jz); } } + } - p = 0.612547; - q = 0.30908712; - a1.allocate(); - for (int jx = mesh->xstart; jx <= mesh->xend; jx++) { - for (int jy = 0; jy < mesh->LocalNy; jy++) { - for (int jz = 0; jz < mesh->LocalNz; jz++) { - BoutReal x = BoutReal(mesh->getGlobalXIndex(jx) - mesh->xstart) / nx; - BoutReal z = BoutReal(jz) / nz; - a1(jx, jy, jz) = - -1. + 0.1 * exp(-50. * pow(x - p, 2) * 2.5) * sin(2. * PI * (z - q) * 7.); + BoutReal max_error = BoutNaN; + + MPI_Allreduce(&local_max_error, &max_error, 1, MPI_DOUBLE, MPI_MAX, BoutComm::get()); + + return max_error; +} + +void apply_flat_boundary(Field3D& bcoef) { + const Mesh& mesh = *bcoef.getMesh(); + if (mesh.firstX()) { + for (int jx = mesh.xstart - 1; jx >= 0; jx--) { + for (int jy = 0; jy < mesh.LocalNy; jy++) { + for (int jz = 0; jz < mesh.LocalNz; jz++) { + bcoef(jx, jy, jz) = bcoef(jx + 1, jy, jz); } } } - if (mesh->firstX()) { - for (int jx = mesh->xstart - 1; jx >= 0; jx--) { - for (int jy = 0; jy < mesh->LocalNy; jy++) { - for (int jz = 0; jz < mesh->LocalNz; jz++) { - BoutReal x = BoutReal(mesh->getGlobalXIndex(jx) - mesh->xstart) / nx; - BoutReal z = BoutReal(jz) / nz; - a1(jx, jy, jz) = - -1. + 0.1 * exp(-50. * pow(x - p, 2) * 2.5) * sin(2. * PI * (z - q) * 7.); - // a1(jx, jy, jz) = a1(jx+1, jy, jz); - } + } + if (mesh.lastX()) { + for (int jx = mesh.xend + 1; jx < mesh.LocalNx; jx++) { + for (int jy = 0; jy < mesh.LocalNy; jy++) { + for (int jz = 0; jz < mesh.LocalNz; jz++) { + bcoef(jx, jy, jz) = bcoef(jx - 1, jy, jz); } } } - if (mesh->lastX()) { - for (int jx = mesh->xend + 1; jx < mesh->LocalNx; jx++) { - for (int jy = 0; jy < mesh->LocalNy; jy++) { - for (int jz = 0; jz < mesh->LocalNz; jz++) { - BoutReal x = BoutReal(mesh->getGlobalXIndex(jx) - mesh->xstart) / nx; - BoutReal z = BoutReal(jz) / nz; - a1(jx, jy, jz) = - -1. + 0.1 * exp(-50. * pow(x - p, 2) * 2.5) * sin(2. * PI * (z - q) * 7.); - // a1(jx, jy, jz) = a1(jx-1, jy, jz); - } - } + } +} + +Field3D generate_f1(const Mesh& mesh) { + const BoutReal nx = mesh.GlobalNx - 2 * mesh.xstart - 1; + const BoutReal nz = mesh.GlobalNz; + + constexpr BoutReal p = 0.39503274; // NOLINT + constexpr BoutReal q = 0.20974396; // NOLINT + + Field3D result; + result.allocate(); + for (int jx = mesh.xstart; jx <= mesh.xend; jx++) { + const BoutReal x = BoutReal(mesh.getGlobalXIndex(jx) - mesh.xstart) / nx; + for (int jy = 0; jy < mesh.LocalNy; jy++) { + for (int jz = 0; jz < mesh.LocalNz; jz++) { + const BoutReal z = BoutReal(jz) / nz; + //make the gradients zero at both x-boundaries + result(jx, jy, jz) = 0. + + exp(-(100. * pow(x - p, 2) + 1. - cos(2. * PI * (z - q)))) + - 50. + * (2. * p * exp(-100. * pow(-p, 2)) * x + + (-p * exp(-100. * pow(-p, 2)) + - (1 - p) * exp(-100. * pow(1 - p, 2))) + * pow(x, 2)) + * exp(-(1. - cos(2. * PI * (z - q)))); } } + } + if (mesh.firstX()) { + for (int jx = mesh.xstart - 1; jx >= 0; jx--) { + const BoutReal x = BoutReal(mesh.getGlobalXIndex(jx) - mesh.xstart) / nx; - checkData(f1); - checkData(a1); - checkData(c1); - checkData(d1); - - mesh->communicate(f1, a1, c1, d1); - - b1 = d1 * Delp2(f1) + Grad_perp(c1) * Grad_perp(f1) / c1 + a1 * f1; - - if (mesh->firstX()) { - for (int jx = mesh->xstart - 1; jx >= 0; jx--) { - for (int jy = 0; jy < mesh->LocalNy; jy++) { - for (int jz = 0; jz < mesh->LocalNz; jz++) { - b1(jx, jy, jz) = b1(jx + 1, jy, jz); - } + for (int jy = 0; jy < mesh.LocalNy; jy++) { + for (int jz = 0; jz < mesh.LocalNz; jz++) { + const BoutReal z = BoutReal(jz) / nz; + //make the gradients zero at both x-boundaries + result(jx, jy, jz) = 0. + + exp(-(60. * pow(x - p, 2) + 1. - cos(2. * PI * (z - q)))) + - 50. + * (2. * p * exp(-60. * pow(-p, 2)) * x + + (-p * exp(-60. * pow(-p, 2)) + - (1 - p) * exp(-60. * pow(1 - p, 2))) + * pow(x, 2)) + * exp(-(1. - cos(2. * PI * (z - q)))); } } } - if (mesh->lastX()) { - for (int jx = mesh->xend + 1; jx < mesh->LocalNx; jx++) { - for (int jy = 0; jy < mesh->LocalNy; jy++) { - for (int jz = 0; jz < mesh->LocalNz; jz++) { - b1(jx, jy, jz) = b1(jx - 1, jy, jz); - } + } + if (mesh.lastX()) { + for (int jx = mesh.xend + 1; jx < mesh.LocalNx; jx++) { + const BoutReal x = BoutReal(mesh.getGlobalXIndex(jx) - mesh.xstart) / nx; + for (int jy = 0; jy < mesh.LocalNy; jy++) { + for (int jz = 0; jz < mesh.LocalNz; jz++) { + const BoutReal z = BoutReal(jz) / nz; + //make the gradients zero at both x-boundaries + result(jx, jy, jz) = 0. + + exp(-(60. * pow(x - p, 2) + 1. - cos(2. * PI * (z - q)))) + - 50. + * (2. * p * exp(-60. * pow(-p, 2)) * x + + (-p * exp(-60. * pow(-p, 2)) + - (1 - p) * exp(-60. * pow(1 - p, 2))) + * pow(x, 2)) + * exp(-(1. - cos(2. * PI * (z - q)))); } } } + } - invert->setInnerBoundaryFlags(INVERT_AC_GRAD); - invert->setOuterBoundaryFlags(INVERT_AC_GRAD); - invert->setCoefA(a1); - invert->setCoefC(c1); - invert->setCoefD(d1); - - checkData(b1); - - try { - sol1 = invert->solve(sliceXZ(b1, mesh->ystart)); - error1 = (f1 - sol1) / f1; - absolute_error1 = f1 - sol1; - // max_error1 = max_error_at_ystart(abs(error1)); - max_error1 = max_error_at_ystart(abs(absolute_error1)); - } catch (BoutException& err) { - output << "BoutException occured in invert->solve(b1): " << err.what() << endl; - max_error1 = -1; - } + checkData(result); + result.applyBoundary("neumann"); + return result; +} - output << endl << "Test 1: PETSc 2nd order" << endl; - // output<<"Time to set up is "<setInnerBoundaryFlags(INVERT_AC_GRAD); - invert_4th->setOuterBoundaryFlags(INVERT_AC_GRAD); - invert_4th->setGlobalFlags(INVERT_4TH_ORDER); - invert_4th->setCoefA(a1); - invert_4th->setCoefC(c1); - invert_4th->setCoefD(d1); - - try { - sol2 = invert_4th->solve(sliceXZ(b1, mesh->ystart)); - error2 = (f1 - sol2) / f1; - absolute_error2 = f1 - sol2; - // max_error2 = max_error_at_ystart(abs(error2)); - max_error2 = max_error_at_ystart(abs(absolute_error2)); - } catch (BoutException& err) { - output << "BoutException occured in invert->solve(b1): " << err.what() << endl; - max_error2 = -1; + constexpr BoutReal p = 0.512547; // NOLINT + constexpr BoutReal q = 0.30908712; // NOLINT + Field3D result; + result.allocate(); + for (int jx = mesh.xstart; jx <= mesh.xend; jx++) { + const BoutReal x = BoutReal(mesh.getGlobalXIndex(jx) - mesh.xstart) / nx; + for (int jy = 0; jy < mesh.LocalNy; jy++) { + for (int jz = 0; jz < mesh.LocalNz; jz++) { + const BoutReal z = BoutReal(jz) / nz; + result(jx, jy, jz) = + 1. + 0.2 * exp(-50. * pow(x - p, 2) / 4.) * sin(2. * PI * (z - q) * 3.); + } } - - output << endl << "Test 2: PETSc 4th order" << endl; - // output<<"Time to set up is "<firstX()) { - for (int jx = mesh->xstart - 1; jx >= 0; jx--) { - for (int jy = 0; jy < mesh->LocalNy; jy++) { - for (int jz = 0; jz < mesh->LocalNz; jz++) { - b3(jx, jy, jz) = b3(jx + 1, jy, jz); - } + } + if (mesh.firstX()) { + for (int jx = mesh.xstart - 1; jx >= 0; jx--) { + const BoutReal x = BoutReal(mesh.getGlobalXIndex(jx) - mesh.xstart) / nx; + for (int jy = 0; jy < mesh.LocalNy; jy++) { + for (int jz = 0; jz < mesh.LocalNz; jz++) { + const BoutReal z = BoutReal(jz) / nz; + result(jx, jy, jz) = + 1. + 0.2 * exp(-50. * pow(x - p, 2) / 4.) * sin(2. * PI * (z - q) * 3.); } } } - if (mesh->lastX()) { - for (int jx = mesh->xend + 1; jx < mesh->LocalNx; jx++) { - for (int jy = 0; jy < mesh->LocalNy; jy++) { - for (int jz = 0; jz < mesh->LocalNz; jz++) { - b3(jx, jy, jz) = b3(jx - 1, jy, jz); - } + } + if (mesh.lastX()) { + for (int jx = mesh.xend + 1; jx < mesh.LocalNx; jx++) { + const BoutReal x = BoutReal(mesh.getGlobalXIndex(jx) - mesh.xstart) / nx; + for (int jy = 0; jy < mesh.LocalNy; jy++) { + for (int jz = 0; jz < mesh.LocalNz; jz++) { + const BoutReal z = BoutReal(jz) / nz; + result(jx, jy, jz) = + 1. + 0.2 * exp(-50. * pow(x - p, 2) / 4.) * sin(2. * PI * (z - q) * 3.); } } } + } + checkData(result); + return result; +} - invert->setInnerBoundaryFlags(INVERT_AC_GRAD); - invert->setOuterBoundaryFlags(INVERT_AC_GRAD); - invert->setCoefA(a3); - invert->setCoefC(c3); - invert->setCoefD(d3); - - try { - sol3 = invert->solve(sliceXZ(b3, mesh->ystart)); - error3 = (f1 - sol3) / f1; - absolute_error3 = f1 - sol3; - // max_error3 = max_error_at_ystart(abs(error3)); - max_error3 = max_error_at_ystart(abs(absolute_error3)); - } catch (BoutException& err) { - output << "BoutException occured in invert->solve(b3): " << err.what() << endl; - max_error3 = -1; - } +Field3D generate_c1(const Mesh& mesh) { + const BoutReal nx = mesh.GlobalNx - 2 * mesh.xstart - 1; + const BoutReal nz = mesh.GlobalNz; - output << endl << "Test 3: with coefficients constant in z, PETSc 2nd order" << endl; - // output<<"Time to set up is "<getSection("SPT"); - auto invert_SPT = Laplacian::create(SPT_options); - invert_SPT->setInnerBoundaryFlags(INVERT_AC_GRAD); - invert_SPT->setOuterBoundaryFlags(INVERT_AC_GRAD | INVERT_DC_GRAD); - invert_SPT->setCoefA(a3); - invert_SPT->setCoefC(c3); - invert_SPT->setCoefD(d3); - - sol4 = invert_SPT->solve(sliceXZ(b3, mesh->ystart)); - error4 = (f1 - sol4) / f1; - absolute_error4 = f1 - sol4; - // max_error4 = max_error_at_ystart(abs(error4)); - max_error4 = max_error_at_ystart(abs(absolute_error4)); - - output << endl << "Test 4: with coefficients constant in z, default solver" << endl; - // output<<"Time to set up is "<xstart; jx <= mesh->xend; jx++) { - for (int jy = 0; jy < mesh->LocalNy; jy++) { - for (int jz = 0; jz < mesh->LocalNz; jz++) { - BoutReal x = BoutReal(mesh->getGlobalXIndex(jx) - mesh->xstart) / nx; - BoutReal z = BoutReal(jz) / nz; - f5(jx, jy, jz) = - 0. + exp(-(50. * pow(x - p, 2) + 1. - cos(2. * PI * (z - q)))) - - 50. - * (2. * p * exp(-50. * pow(-p, 2)) * x - + (-p * exp(-50. * pow(-p, 2)) - - (1 - p) * exp(-50. * pow(1 - p, 2))) - * pow(x, 2)) - * exp(-( - 1. - - cos(2. * PI - * (z - q)))) //make the gradients zero at both x-boundaries - ; - } + constexpr BoutReal p = 0.18439023; // NOLINT + constexpr BoutReal q = 0.401089473; // NOLINT + Field3D result; + result.allocate(); + for (int jx = mesh.xstart; jx <= mesh.xend; jx++) { + const BoutReal x = BoutReal(mesh.getGlobalXIndex(jx) - mesh.xstart) / nx; + for (int jy = 0; jy < mesh.LocalNy; jy++) { + for (int jz = 0; jz < mesh.LocalNz; jz++) { + const BoutReal z = BoutReal(jz) / nz; + result(jx, jy, jz) = + 1. + 0.15 * exp(-50. * pow(x - p, 2) * 2.) * sin(2. * PI * (z - q) * 2.); } } - if (mesh->firstX()) { - for (int jx = mesh->xstart - 1; jx >= 0; jx--) { - for (int jy = 0; jy < mesh->LocalNy; jy++) { - for (int jz = 0; jz < mesh->LocalNz; jz++) { - BoutReal x = BoutReal(mesh->getGlobalXIndex(jx) - mesh->xstart) / nx; - BoutReal z = BoutReal(jz) / nz; - f5(jx, jy, jz) = - 0. + exp(-(50. * pow(x - p, 2) + 1. - cos(2. * PI * (z - q)))) - - 50. - * (2. * p * exp(-50. * pow(-p, 2)) * x - + (-p * exp(-50. * pow(-p, 2)) - - (1 - p) * exp(-50. * pow(1 - p, 2))) - * pow(x, 2)) - * exp(-( - 1. - - cos( - 2. * PI - * (z - q)))); //make the gradients zero at both x-boundaries - } + } + if (mesh.firstX()) { + for (int jx = mesh.xstart - 1; jx >= 0; jx--) { + const BoutReal x = BoutReal(mesh.getGlobalXIndex(jx) - mesh.xstart) / nx; + for (int jy = 0; jy < mesh.LocalNy; jy++) { + for (int jz = 0; jz < mesh.LocalNz; jz++) { + const BoutReal z = BoutReal(jz) / nz; + result(jx, jy, jz) = + 1. + 0.15 * exp(-50. * pow(x - p, 2) * 2.) * sin(2. * PI * (z - q) * 2.); } } } - if (mesh->lastX()) { - for (int jx = mesh->xend + 1; jx < mesh->LocalNx; jx++) { - for (int jy = 0; jy < mesh->LocalNy; jy++) { - for (int jz = 0; jz < mesh->LocalNz; jz++) { - BoutReal x = BoutReal(mesh->getGlobalXIndex(jx) - mesh->xstart) / nx; - BoutReal z = BoutReal(jz) / nz; - f5(jx, jy, jz) = - 0. + exp(-(50. * pow(x - p, 2) + 1. - cos(2. * PI * (z - q)))) - - 50. - * (2. * p * exp(-50. * pow(-p, 2)) * x - + (-p * exp(-50. * pow(-p, 2)) - - (1 - p) * exp(-50. * pow(1 - p, 2))) - * pow(x, 2)) - * exp(-( - 1. - - cos( - 2. * PI - * (z - q)))); //make the gradients zero at both x-boundaries - } + } + if (mesh.lastX()) { + for (int jx = mesh.xend + 1; jx < mesh.LocalNx; jx++) { + const BoutReal x = BoutReal(mesh.getGlobalXIndex(jx) - mesh.xstart) / nx; + for (int jy = 0; jy < mesh.LocalNy; jy++) { + for (int jz = 0; jz < mesh.LocalNz; jz++) { + const BoutReal z = BoutReal(jz) / nz; + result(jx, jy, jz) = + 1. + 0.15 * exp(-50. * pow(x - p, 2) * 2.) * sin(2. * PI * (z - q) * 2.); } } } + } - p = 0.63298589; - q = 0.889237890; - d5.allocate(); - for (int jx = mesh->xstart; jx <= mesh->xend; jx++) { - for (int jy = 0; jy < mesh->LocalNy; jy++) { - for (int jz = 0; jz < mesh->LocalNz; jz++) { - BoutReal x = BoutReal(mesh->getGlobalXIndex(jx) - mesh->xstart) / nx; - BoutReal z = BoutReal(jz) / nz; - d5(jx, jy, jz) = 1. + p * cos(2. * PI * x) * sin(2. * PI * (z - q) * 3.); - } + checkData(result); + return result; +} + +Field3D generate_a1(const Mesh& mesh) { + const BoutReal nx = mesh.GlobalNx - 2 * mesh.xstart - 1; + const BoutReal nz = mesh.GlobalNz; + + constexpr BoutReal p = 0.612547; // NOLINT + constexpr BoutReal q = 0.30908712; // NOLINT + Field3D result; + result.allocate(); + for (int jx = mesh.xstart; jx <= mesh.xend; jx++) { + const BoutReal x = BoutReal(mesh.getGlobalXIndex(jx) - mesh.xstart) / nx; + for (int jy = 0; jy < mesh.LocalNy; jy++) { + for (int jz = 0; jz < mesh.LocalNz; jz++) { + const BoutReal z = BoutReal(jz) / nz; + result(jx, jy, jz) = + -1. + 0.1 * exp(-50. * pow(x - p, 2) * 2.5) * sin(2. * PI * (z - q) * 7.); } } - if (mesh->firstX()) { - for (int jx = mesh->xstart - 1; jx >= 0; jx--) { - for (int jy = 0; jy < mesh->LocalNy; jy++) { - for (int jz = 0; jz < mesh->LocalNz; jz++) { - BoutReal x = BoutReal(mesh->getGlobalXIndex(jx) - mesh->xstart) / nx; - BoutReal z = BoutReal(jz) / nz; - d5(jx, jy, jz) = 1. + p * cos(2. * PI * x) * sin(2. * PI * (z - q) * 3.); - } + } + if (mesh.firstX()) { + for (int jx = mesh.xstart - 1; jx >= 0; jx--) { + const BoutReal x = BoutReal(mesh.getGlobalXIndex(jx) - mesh.xstart) / nx; + for (int jy = 0; jy < mesh.LocalNy; jy++) { + for (int jz = 0; jz < mesh.LocalNz; jz++) { + const BoutReal z = BoutReal(jz) / nz; + result(jx, jy, jz) = + -1. + 0.1 * exp(-50. * pow(x - p, 2) * 2.5) * sin(2. * PI * (z - q) * 7.); } } } - if (mesh->lastX()) { - for (int jx = mesh->xend + 1; jx < mesh->LocalNx; jx++) { - for (int jy = 0; jy < mesh->LocalNy; jy++) { - for (int jz = 0; jz < mesh->LocalNz; jz++) { - BoutReal x = BoutReal(mesh->getGlobalXIndex(jx) - mesh->xstart) / nx; - BoutReal z = BoutReal(jz) / nz; - d5(jx, jy, jz) = 1. + p * cos(2. * PI * x) * sin(2. * PI * (z - q) * 3.); - } + } + if (mesh.lastX()) { + for (int jx = mesh.xend + 1; jx < mesh.LocalNx; jx++) { + const BoutReal x = BoutReal(mesh.getGlobalXIndex(jx) - mesh.xstart) / nx; + for (int jy = 0; jy < mesh.LocalNy; jy++) { + for (int jz = 0; jz < mesh.LocalNz; jz++) { + const BoutReal z = BoutReal(jz) / nz; + result(jx, jy, jz) = + -1. + 0.1 * exp(-50. * pow(x - p, 2) * 2.5) * sin(2. * PI * (z - q) * 7.); } } } + } - p = 0.160983834; - q = 0.73050121087; - c5.allocate(); - for (int jx = mesh->xstart; jx <= mesh->xend; jx++) { - for (int jy = 0; jy < mesh->LocalNy; jy++) { - for (int jz = 0; jz < mesh->LocalNz; jz++) { - BoutReal x = BoutReal(mesh->getGlobalXIndex(jx) - mesh->xstart) / nx; - BoutReal z = BoutReal(jz) / nz; - c5(jx, jy, jz) = 1. + p * cos(2. * PI * x * 5) * sin(2. * PI * (z - q) * 2.); - } + checkData(result); + return result; +} + +Field3D generate_f5(const Mesh& mesh) { + const BoutReal nx = mesh.GlobalNx - 2 * mesh.xstart - 1; + const BoutReal nz = mesh.GlobalNz; + constexpr BoutReal p = 0.623901; // NOLINT + constexpr BoutReal q = 0.01209489; // NOLINT + Field3D result; + result.allocate(); + for (int jx = mesh.xstart; jx <= mesh.xend; jx++) { + const BoutReal x = BoutReal(mesh.getGlobalXIndex(jx) - mesh.xstart) / nx; + for (int jy = 0; jy < mesh.LocalNy; jy++) { + for (int jz = 0; jz < mesh.LocalNz; jz++) { + const BoutReal z = BoutReal(jz) / nz; + //make the gradients zero at both x-boundaries + result(jx, jy, jz) = + 0. + exp(-(50. * pow(x - p, 2) + 1. - cos(2. * PI * (z - q)))) + - 50. + * (2. * p * exp(-50. * pow(-p, 2)) * x + + (-p * exp(-50. * pow(-p, 2)) - (1 - p) * exp(-50. * pow(1 - p, 2))) + * pow(x, 2)) + * exp(-(1. - cos(2. * PI * (z - q)))); } } - if (mesh->firstX()) { - for (int jx = mesh->xstart - 1; jx >= 0; jx--) { - for (int jy = 0; jy < mesh->LocalNy; jy++) { - for (int jz = 0; jz < mesh->LocalNz; jz++) { - BoutReal x = BoutReal(mesh->getGlobalXIndex(jx) - mesh->xstart) / nx; - BoutReal z = BoutReal(jz) / nz; - c5(jx, jy, jz) = 1. + p * cos(2. * PI * x * 5) * sin(2. * PI * (z - q) * 2.); - } + } + if (mesh.firstX()) { + for (int jx = mesh.xstart - 1; jx >= 0; jx--) { + const BoutReal x = BoutReal(mesh.getGlobalXIndex(jx) - mesh.xstart) / nx; + for (int jy = 0; jy < mesh.LocalNy; jy++) { + for (int jz = 0; jz < mesh.LocalNz; jz++) { + const BoutReal z = BoutReal(jz) / nz; + //make the gradients zero at both x-boundaries + result(jx, jy, jz) = 0. + + exp(-(50. * pow(x - p, 2) + 1. - cos(2. * PI * (z - q)))) + - 50. + * (2. * p * exp(-50. * pow(-p, 2)) * x + + (-p * exp(-50. * pow(-p, 2)) + - (1 - p) * exp(-50. * pow(1 - p, 2))) + * pow(x, 2)) + * exp(-(1. - cos(2. * PI * (z - q)))); } } } - if (mesh->lastX()) { - for (int jx = mesh->xend + 1; jx < mesh->LocalNx; jx++) { - for (int jy = 0; jy < mesh->LocalNy; jy++) { - for (int jz = 0; jz < mesh->LocalNz; jz++) { - BoutReal x = BoutReal(mesh->getGlobalXIndex(jx) - mesh->xstart) / nx; - BoutReal z = BoutReal(jz) / nz; - c5(jx, jy, jz) = 1. + p * cos(2. * PI * x * 5) * sin(2. * PI * (z - q) * 2.); - } + } + if (mesh.lastX()) { + for (int jx = mesh.xend + 1; jx < mesh.LocalNx; jx++) { + const BoutReal x = BoutReal(mesh.getGlobalXIndex(jx) - mesh.xstart) / nx; + for (int jy = 0; jy < mesh.LocalNy; jy++) { + for (int jz = 0; jz < mesh.LocalNz; jz++) { + const BoutReal z = BoutReal(jz) / nz; + //make the gradients zero at both x-boundaries + result(jx, jy, jz) = 0. + + exp(-(50. * pow(x - p, 2) + 1. - cos(2. * PI * (z - q)))) + - 50. + * (2. * p * exp(-50. * pow(-p, 2)) * x + + (-p * exp(-50. * pow(-p, 2)) + - (1 - p) * exp(-50. * pow(1 - p, 2))) + * pow(x, 2)) + * exp(-(1. - cos(2. * PI * (z - q)))); } } } + } + result.applyBoundary("neumann"); + checkData(result); + return result; +} - p = 0.5378950; - q = 0.2805870; - a5.allocate(); - for (int jx = mesh->xstart; jx <= mesh->xend; jx++) { - for (int jy = 0; jy < mesh->LocalNy; jy++) { - for (int jz = 0; jz < mesh->LocalNz; jz++) { - BoutReal x = BoutReal(mesh->getGlobalXIndex(jx) - mesh->xstart) / nx; - BoutReal z = BoutReal(jz) / nz; - a5(jx, jy, jz) = -1. + p * cos(2. * PI * x * 2.) * sin(2. * PI * (z - q) * 7.); - } +Field3D generate_d5(const Mesh& mesh) { + const BoutReal nx = mesh.GlobalNx - 2 * mesh.xstart - 1; + const BoutReal nz = mesh.GlobalNz; + constexpr BoutReal p = 0.63298589; // NOLINT + constexpr BoutReal q = 0.889237890; // NOLINT + Field3D result; + result.allocate(); + for (int jx = mesh.xstart; jx <= mesh.xend; jx++) { + const BoutReal x = BoutReal(mesh.getGlobalXIndex(jx) - mesh.xstart) / nx; + for (int jy = 0; jy < mesh.LocalNy; jy++) { + for (int jz = 0; jz < mesh.LocalNz; jz++) { + const BoutReal z = BoutReal(jz) / nz; + result(jx, jy, jz) = 1. + p * cos(2. * PI * x) * sin(2. * PI * (z - q) * 3.); } } - if (mesh->firstX()) { - for (int jx = mesh->xstart - 1; jx >= 0; jx--) { - for (int jy = 0; jy < mesh->LocalNy; jy++) { - for (int jz = 0; jz < mesh->LocalNz; jz++) { - BoutReal x = BoutReal(mesh->getGlobalXIndex(jx) - mesh->xstart) / nx; - BoutReal z = BoutReal(jz) / nz; - a5(jx, jy, jz) = - -1. + p * cos(2. * PI * x * 2.) * sin(2. * PI * (z - q) * 7.); - } + } + if (mesh.firstX()) { + for (int jx = mesh.xstart - 1; jx >= 0; jx--) { + const BoutReal x = BoutReal(mesh.getGlobalXIndex(jx) - mesh.xstart) / nx; + for (int jy = 0; jy < mesh.LocalNy; jy++) { + for (int jz = 0; jz < mesh.LocalNz; jz++) { + const BoutReal z = BoutReal(jz) / nz; + result(jx, jy, jz) = 1. + p * cos(2. * PI * x) * sin(2. * PI * (z - q) * 3.); } } } - if (mesh->lastX()) { - for (int jx = mesh->xend + 1; jx < mesh->LocalNx; jx++) { - for (int jy = 0; jy < mesh->LocalNy; jy++) { - for (int jz = 0; jz < mesh->LocalNz; jz++) { - BoutReal x = BoutReal(mesh->getGlobalXIndex(jx) - mesh->xstart) / nx; - BoutReal z = BoutReal(jz) / nz; - a5(jx, jy, jz) = - -1. + p * cos(2. * PI * x * 2.) * sin(2. * PI * (z - q) * 7.); - } + } + if (mesh.lastX()) { + for (int jx = mesh.xend + 1; jx < mesh.LocalNx; jx++) { + const BoutReal x = BoutReal(mesh.getGlobalXIndex(jx) - mesh.xstart) / nx; + for (int jy = 0; jy < mesh.LocalNy; jy++) { + for (int jz = 0; jz < mesh.LocalNz; jz++) { + const BoutReal z = BoutReal(jz) / nz; + result(jx, jy, jz) = 1. + p * cos(2. * PI * x) * sin(2. * PI * (z - q) * 3.); } } } + } + checkData(result); + return result; +} - f5.applyBoundary("neumann"); - mesh->communicate(f5, a5, c5, d5); +Field3D generate_c5(const Mesh& mesh) { + const BoutReal nx = mesh.GlobalNx - 2 * mesh.xstart - 1; + const BoutReal nz = mesh.GlobalNz; + constexpr BoutReal p = 0.160983834; // NOLINT + constexpr BoutReal q = 0.73050121087; // NOLINT - b5 = d5 * Delp2(f5) + Grad_perp(c5) * Grad_perp(f5) / c5 + a5 * f5; - if (mesh->firstX()) { - for (int jx = mesh->xstart - 1; jx >= 0; jx--) { - for (int jy = 0; jy < mesh->LocalNy; jy++) { - for (int jz = 0; jz < mesh->LocalNz; jz++) { - b5(jx, jy, jz) = b5(jx + 1, jy, jz); - } - } + Field3D result; + + result.allocate(); + for (int jx = mesh.xstart; jx <= mesh.xend; jx++) { + const BoutReal x = BoutReal(mesh.getGlobalXIndex(jx) - mesh.xstart) / nx; + for (int jy = 0; jy < mesh.LocalNy; jy++) { + for (int jz = 0; jz < mesh.LocalNz; jz++) { + const BoutReal z = BoutReal(jz) / nz; + result(jx, jy, jz) = 1. + p * cos(2. * PI * x * 5) * sin(2. * PI * (z - q) * 2.); } } - if (mesh->lastX()) { - for (int jx = mesh->xend + 1; jx < mesh->LocalNx; jx++) { - for (int jy = 0; jy < mesh->LocalNy; jy++) { - for (int jz = 0; jz < mesh->LocalNz; jz++) { - b5(jx, jy, jz) = b5(jx - 1, jy, jz); - } + } + if (mesh.firstX()) { + for (int jx = mesh.xstart - 1; jx >= 0; jx--) { + const BoutReal x = BoutReal(mesh.getGlobalXIndex(jx) - mesh.xstart) / nx; + for (int jy = 0; jy < mesh.LocalNy; jy++) { + for (int jz = 0; jz < mesh.LocalNz; jz++) { + const BoutReal z = BoutReal(jz) / nz; + result(jx, jy, jz) = + 1. + p * cos(2. * PI * x * 5) * sin(2. * PI * (z - q) * 2.); } } } - - invert->setInnerBoundaryFlags(INVERT_AC_GRAD); - invert->setOuterBoundaryFlags(INVERT_AC_GRAD); - invert->setCoefA(a5); - invert->setCoefC(c5); - invert->setCoefD(d5); - - try { - sol5 = invert->solve(sliceXZ(b5, mesh->ystart)); - error5 = (f5 - sol5) / f5; - absolute_error5 = f5 - sol5; - // max_error5 = max_error_at_ystart(abs(error5)); - max_error5 = max_error_at_ystart(abs(absolute_error5)); - } catch (BoutException& err) { - output << "BoutException occured in invert->solve(b5): " << err.what() << endl; - max_error5 = -1; - } - - output << endl << "Test 5: different profiles, PETSc 2nd order" << endl; - // output<<"Time to set up is "<setInnerBoundaryFlags(INVERT_AC_GRAD); - invert_4th->setOuterBoundaryFlags(INVERT_AC_GRAD); - invert_4th->setGlobalFlags(INVERT_4TH_ORDER); - invert_4th->setCoefA(a5); - invert_4th->setCoefC(c5); - invert_4th->setCoefD(d5); - - try { - sol6 = invert_4th->solve(sliceXZ(b5, mesh->ystart)); - error6 = (f5 - sol6) / f5; - absolute_error6 = f5 - sol6; - // max_error6 = max_error_at_ystart(abs(error6)); - max_error6 = max_error_at_ystart(abs(absolute_error6)); - } catch (BoutException& err) { - output - << "BoutException occured in invert->solve(b6): Laplacian inversion failed to " - "converge (probably)" - << endl; - max_error6 = -1; + } + if (mesh.lastX()) { + for (int jx = mesh.xend + 1; jx < mesh.LocalNx; jx++) { + const BoutReal x = BoutReal(mesh.getGlobalXIndex(jx) - mesh.xstart) / nx; + for (int jy = 0; jy < mesh.LocalNy; jy++) { + for (int jz = 0; jz < mesh.LocalNz; jz++) { + const BoutReal z = BoutReal(jz) / nz; + result(jx, jy, jz) = + 1. + p * cos(2. * PI * x * 5) * sin(2. * PI * (z - q) * 2.); + } + } } + } + checkData(result); + return result; +} - output << endl << "Test 6: different profiles, PETSc 4th order" << endl; - // output<<"Time to set up is "<firstX()) { - for (int jx = mesh->xstart - 1; jx >= 0; jx--) { - for (int jy = 0; jy < mesh->LocalNy; jy++) { - for (int jz = 0; jz < mesh->LocalNz; jz++) { - b7(jx, jy, jz) = b7(jx + 1, jy, jz); - } - } +Field3D generate_a5(const Mesh& mesh) { + const BoutReal nx = mesh.GlobalNx - 2 * mesh.xstart - 1; + const BoutReal nz = mesh.GlobalNz; + constexpr BoutReal p = 0.5378950; // NOLINT + constexpr BoutReal q = 0.2805870; // NOLINT + Field3D result; + result.allocate(); + for (int jx = mesh.xstart; jx <= mesh.xend; jx++) { + const BoutReal x = BoutReal(mesh.getGlobalXIndex(jx) - mesh.xstart) / nx; + for (int jy = 0; jy < mesh.LocalNy; jy++) { + for (int jz = 0; jz < mesh.LocalNz; jz++) { + const BoutReal z = BoutReal(jz) / nz; + result(jx, jy, jz) = + -1. + p * cos(2. * PI * x * 2.) * sin(2. * PI * (z - q) * 7.); } } - if (mesh->lastX()) { - for (int jx = mesh->xend + 1; jx < mesh->LocalNx; jx++) { - for (int jy = 0; jy < mesh->LocalNy; jy++) { - for (int jz = 0; jz < mesh->LocalNz; jz++) { - b7(jx, jy, jz) = b7(jx - 1, jy, jz); - } + } + if (mesh.firstX()) { + for (int jx = mesh.xstart - 1; jx >= 0; jx--) { + const BoutReal x = BoutReal(mesh.getGlobalXIndex(jx) - mesh.xstart) / nx; + for (int jy = 0; jy < mesh.LocalNy; jy++) { + for (int jz = 0; jz < mesh.LocalNz; jz++) { + const BoutReal z = BoutReal(jz) / nz; + result(jx, jy, jz) = + -1. + p * cos(2. * PI * x * 2.) * sin(2. * PI * (z - q) * 7.); } } } - - invert->setInnerBoundaryFlags(INVERT_AC_GRAD); - invert->setOuterBoundaryFlags(INVERT_AC_GRAD); - invert->setCoefA(a7); - invert->setCoefC(c7); - invert->setCoefD(d7); - - try { - sol7 = invert->solve(sliceXZ(b7, mesh->ystart)); - error7 = (f5 - sol7) / f5; - absolute_error7 = f5 - sol7; - // max_error7 = max_error_at_ystart(abs(error7)); - max_error7 = max_error_at_ystart(abs(absolute_error7)); - } catch (BoutException& err) { - output << "BoutException occured in invert->solve(b7): " << err.what() << endl; - max_error7 = -1; - } - - output - << endl - << "Test 7: different profiles, with coefficients constant in z, PETSc 2nd order" - << endl; - // output<<"Time to set up is "<setInnerBoundaryFlags(INVERT_AC_GRAD); - invert_SPT->setOuterBoundaryFlags(INVERT_AC_GRAD | INVERT_DC_GRAD); - invert_SPT->setCoefA(a7); - invert_SPT->setCoefC(c7); - invert_SPT->setCoefD(d7); - - sol8 = invert_SPT->solve(sliceXZ(b7, mesh->ystart)); - error8 = (f5 - sol8) / f5; - absolute_error8 = f5 - sol8; - // max_error8 = max_error_at_ystart(abs(error8)); - max_error8 = max_error_at_ystart(abs(absolute_error8)); - - output - << endl - << "Test 8: different profiles, with coefficients constant in z, default solver" - << endl; - // output<<"Time to set up is "<xstart, mesh->ystart, 0); - - for (int jx = mesh->xstart; jx <= mesh->xend; jx++) { - for (int jz = 0; jz < mesh->LocalNz; jz++) { - if (local_max_error < error(jx, mesh->ystart, jz)) { - local_max_error = error(jx, mesh->ystart, jz); + if (mesh.lastX()) { + for (int jx = mesh.xend + 1; jx < mesh.LocalNx; jx++) { + const BoutReal x = BoutReal(mesh.getGlobalXIndex(jx) - mesh.xstart) / nx; + for (int jy = 0; jy < mesh.LocalNy; jy++) { + for (int jz = 0; jz < mesh.LocalNz; jz++) { + const BoutReal z = BoutReal(jz) / nz; + result(jx, jy, jz) = + -1. + p * cos(2. * PI * x * 2.) * sin(2. * PI * (z - q) * 7.); + } } } } - - BoutReal max_error; - - MPI_Allreduce(&local_max_error, &max_error, 1, MPI_DOUBLE, MPI_MAX, BoutComm::get()); - - return max_error; + checkData(result); + return result; } From fa24eb8869e0531dbbb6c666875ff4a2bac38e25 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Mon, 23 Sep 2024 11:00:50 +0100 Subject: [PATCH 459/491] Add overloaded getters for d1_dx, d1_dy, and d1_dz, that give the value at a particular x, y (or x, y, z) position. --- include/bout/coordinates.hxx | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index 0bfd763ffb..b8fe4a6499 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -80,15 +80,14 @@ public: const FieldMetric& dy() const { return dy_; } const FieldMetric& dz() const { return dz_; } -#if BOUT_USE_METRIC_3D const BoutReal& dx(int x, int y, int z) const { return dx_(x, y, z); } const BoutReal& dy(int x, int y, int z) const { return dy_(x, y, z); } const BoutReal& dz(int x, int y, int z) const { return dz_(x, y, z); } -#else + const BoutReal& dx(int x, int y) const { return dx_(x, y); } const BoutReal& dy(int x, int y) const { return dy_(x, y); } const BoutReal& dz(int x, int y) const { return dz_(x, y); } -#endif + void setDx(FieldMetric dx) { dx_ = std::move(dx); } void setDy(FieldMetric dy) { dy_ = std::move(dy); } @@ -116,6 +115,16 @@ public: const FieldMetric& d1_dy() const { return d1_dy_; } const FieldMetric& d1_dz() const { return d1_dz_; } + #if BOUT_USE_METRIC_3D + const BoutReal& d1_dx(int x, int y, int z) const { return d1_dx_(x, y, z); } + const BoutReal& d1_dy(int x, int y, int z) const { return d1_dy_(x, y, z); } + const BoutReal& d1_dz(int x, int y, int z) const { return d1_dz_(x, y, z); } +#else + const BoutReal& d1_dx(int x, int y) const { return d1_dx_(x, y); } + const BoutReal& d1_dy(int x, int y) const { return d1_dy_(x, y); } + const BoutReal& d1_dz(int x, int y) const { return d1_dz_(x, y); } +#endif + /// Covariant metric tensor const MetricTensor::FieldMetric& g_11() const { return covariantMetricTensor.g11(); } const MetricTensor::FieldMetric& g_22() const { return covariantMetricTensor.g22(); } From 541da1272730a321aa4f6adc6cec5e69cb2914dc Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 25 Sep 2024 12:20:56 +0100 Subject: [PATCH 460/491] The (x, y) indexing methods return a pointer when BOUT_USE_METRIC_3D and a reference otherwise). --- include/bout/coordinates.hxx | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index b8fe4a6499..e6d67cd98a 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -84,11 +84,16 @@ public: const BoutReal& dy(int x, int y, int z) const { return dy_(x, y, z); } const BoutReal& dz(int x, int y, int z) const { return dz_(x, y, z); } +#if BOUT_USE_METRIC_3D + const BoutReal* dx(int x, int y) const { return dx_(x, y); } + const BoutReal* dy(int x, int y) const { return dy_(x, y); } + const BoutReal* dz(int x, int y) const { return dz_(x, y); } +#else const BoutReal& dx(int x, int y) const { return dx_(x, y); } const BoutReal& dy(int x, int y) const { return dy_(x, y); } const BoutReal& dz(int x, int y) const { return dz_(x, y); } - - +#endif + void setDx(FieldMetric dx) { dx_ = std::move(dx); } void setDy(FieldMetric dy) { dy_ = std::move(dy); } void setDz(FieldMetric dz) { dz_ = std::move(dz); } From a2b9584f6b019c109d4a7ee44498ba8090c19f22 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Tue, 24 Sep 2024 13:36:53 +0100 Subject: [PATCH 461/491] Add overloaded getters for metric tensor components, that give the value at a particular x, y (or x, y, z) position. --- include/bout/coordinates.hxx | 16 ++++++++++++++++ include/bout/metric_tensor.hxx | 7 +++++++ 2 files changed, 23 insertions(+) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index e6d67cd98a..8205bca8c0 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -146,6 +146,22 @@ public: const MetricTensor::FieldMetric& g13() const { return contravariantMetricTensor.g13(); } const MetricTensor::FieldMetric& g23() const { return contravariantMetricTensor.g23(); } + /// Covariant metric tensor + BoutReal g_11(int x, int y, int z) const { return covariantMetricTensor.g11(x, y, z); } + BoutReal g_22(int x, int y, int z) const { return covariantMetricTensor.g22(x, y, z); } + BoutReal g_33(int x, int y, int z) const { return covariantMetricTensor.g33(x, y, z); } + BoutReal g_12(int x, int y, int z) const { return covariantMetricTensor.g12(x, y, z); } + BoutReal g_13(int x, int y, int z) const { return covariantMetricTensor.g13(x, y, z); } + BoutReal g_23(int x, int y, int z) const { return covariantMetricTensor.g23(x, y, z); } + + /// Contravariant metric tensor (g^{ij}) + BoutReal g11(int x, int y, int z) const { return contravariantMetricTensor.g11(x, y, z); } + BoutReal g22(int x, int y, int z) const { return contravariantMetricTensor.g22(x, y, z); } + BoutReal g33(int x, int y, int z) const { return contravariantMetricTensor.g33(x, y, z); } + BoutReal g12(int x, int y, int z) const { return contravariantMetricTensor.g12(x, y, z); } + BoutReal g13(int x, int y, int z) const { return contravariantMetricTensor.g13(x, y, z); } + BoutReal g23(int x, int y, int z) const { return contravariantMetricTensor.g23(x, y, z); } + const ContravariantMetricTensor& getContravariantMetricTensor() const { return contravariantMetricTensor; } diff --git a/include/bout/metric_tensor.hxx b/include/bout/metric_tensor.hxx index 4039142870..f006475563 100644 --- a/include/bout/metric_tensor.hxx +++ b/include/bout/metric_tensor.hxx @@ -32,6 +32,13 @@ public: const FieldMetric& g13() const { return g13_; } const FieldMetric& g23() const { return g23_; } + BoutReal g11(int x, int y, int z) const { return g11_(x, y, z); } + BoutReal g22(int x, int y, int z) const { return g22_(x, y, z); } + BoutReal g33(int x, int y, int z) const { return g33_(x, y, z); } + BoutReal g12(int x, int y, int z) const { return g12_(x, y, z); } + BoutReal g13(int x, int y, int z) const { return g13_(x, y, z); } + BoutReal g23(int x, int y, int z) const { return g23_(x, y, z); } + void setMetricTensor(const MetricTensor& metric_tensor) { g11_ = metric_tensor.g11(); From 4fb48ba28d2cbd4563f5a0469433e5cdef9f1c5a Mon Sep 17 00:00:00 2001 From: tomc271 Date: Tue, 24 Sep 2024 14:09:55 +0100 Subject: [PATCH 462/491] Use overloaded getters for metric tensor components. --- .../impls/multigrid/multigrid_laplace.cxx | 14 ++--- .../laplace/impls/petsc/petsc_laplace.cxx | 12 ++-- .../laplace/impls/serial_band/serial_band.cxx | 54 ++++++++-------- src/invert/laplace/invert_laplace.cxx | 20 +++--- .../impls/cyclic/laplacexz-cyclic.cxx | 6 +- .../laplacexz/impls/petsc/laplacexz-petsc.cxx | 16 ++--- src/mesh/boundary_standard.cxx | 62 +++++++++---------- src/mesh/fv_ops.cxx | 20 +++--- tests/MMS/GBS/gbs.cxx | 6 +- 9 files changed, 105 insertions(+), 105 deletions(-) diff --git a/src/invert/laplace/impls/multigrid/multigrid_laplace.cxx b/src/invert/laplace/impls/multigrid/multigrid_laplace.cxx index 36bf12aea2..302a176cc9 100644 --- a/src/invert/laplace/impls/multigrid/multigrid_laplace.cxx +++ b/src/invert/laplace/impls/multigrid/multigrid_laplace.cxx @@ -602,22 +602,22 @@ void LaplaceMultigrid::generateMatrixF(int level) { BoutReal ddz_C = (C2(i2, yindex, k2p) - C2(i2, yindex, k2m)) / 2. / dz / C1(i2, yindex, k2); - BoutReal ddx = D(i2, yindex, k2) * coords->g11()(i2, yindex) + BoutReal ddx = D(i2, yindex, k2) * coords->g11(i2, yindex) / coords->dx(i2, yindex) / coords->dx(i2, yindex); // coefficient of 2nd derivative stencil (x-direction) - BoutReal ddz = D(i2, yindex, k2) * coords->g33()(i2, yindex) / SQ(dz); + BoutReal ddz = D(i2, yindex, k2) * coords->g33(i2, yindex) / SQ(dz); // coefficient of 2nd derivative stencil (z-direction) - BoutReal dxdz = D(i2, yindex, k2) * 2. * coords->g13()(i2, yindex) + BoutReal dxdz = D(i2, yindex, k2) * 2. * coords->g13(i2, yindex) / coords->dx(i2, yindex) / dz; // coefficient of mixed derivative stencil (could assume zero, at least initially, // if easier; then check this is true in constructor) BoutReal dxd = (D(i2, yindex, k2) * coords->G1()(i2, yindex) - + coords->g11()(i2, yindex) * ddx_C - + coords->g13()(i2, yindex) + + coords->g11(i2, yindex) * ddx_C + + coords->g13(i2, yindex) * ddz_C // (could assume zero, at least initially, if easier; then check this is true in constructor) ) / coords->dx(i2, @@ -629,8 +629,8 @@ void LaplaceMultigrid::generateMatrixF(int level) { BoutReal dzd = (D(i2, yindex, k2) * coords->G3()(i2, yindex) - + coords->g33()(i2, yindex) * ddz_C - + coords->g13()(i2, yindex) + + coords->g33(i2, yindex) * ddz_C + + coords->g13(i2, yindex) * ddx_C // (could assume zero, at least initially, if easier; then check // this is true in constructor) ) diff --git a/src/invert/laplace/impls/petsc/petsc_laplace.cxx b/src/invert/laplace/impls/petsc/petsc_laplace.cxx index ae105e2994..15eb3cbaa8 100644 --- a/src/invert/laplace/impls/petsc/petsc_laplace.cxx +++ b/src/invert/laplace/impls/petsc/petsc_laplace.cxx @@ -971,9 +971,9 @@ void LaplacePetsc::Element(int i, int x, int z, int xshift, int zshift, PetscSca void LaplacePetsc::Coeffs(int x, int y, int z, BoutReal& coef1, BoutReal& coef2, BoutReal& coef3, BoutReal& coef4, BoutReal& coef5) { - coef1 = coords->g11()(x, y, z); // X 2nd derivative coefficient - coef2 = coords->g33()(x, y, z); // Z 2nd derivative coefficient - coef3 = 2. * coords->g13()(x, y, z); // X-Z mixed derivative coefficient + coef1 = coords->g11(x, y, z); // X 2nd derivative coefficient + coef2 = coords->g33(x, y, z); // Z 2nd derivative coefficient + coef3 = 2. * coords->g13(x, y, z); // X-Z mixed derivative coefficient coef4 = 0.0; coef5 = 0.0; @@ -998,7 +998,7 @@ void LaplacePetsc::Coeffs(int x, int y, int z, BoutReal& coef1, BoutReal& coef2, if (localmesh->IncIntShear) { // d2dz2 term - coef2 += coords->g11()(x, y, z) * coords->IntShiftTorsion()(x, y, z) + coef2 += coords->g11(x, y, z) * coords->IntShiftTorsion()(x, y, z) * coords->IntShiftTorsion()(x, y, z); // Mixed derivative coef3 = 0.0; // This cancels out @@ -1052,8 +1052,8 @@ void LaplacePetsc::Coeffs(int x, int y, int z, BoutReal& coef1, BoutReal& coef2, (C2(x, y, zp) - C2(x, y, zm)) / (2. * coords->dz(x, y, z) * (C1(x, y, z))); } - coef4 += coords->g11()(x, y, z) * ddx_C + coords->g13()(x, y, z) * ddz_C; - coef5 += coords->g13()(x, y, z) * ddx_C + coords->g33()(x, y, z) * ddz_C; + coef4 += coords->g11(x, y, z) * ddx_C + coords->g13(x, y, z) * ddz_C; + coef5 += coords->g13(x, y, z) * ddx_C + coords->g33(x, y, z) * ddz_C; } } diff --git a/src/invert/laplace/impls/serial_band/serial_band.cxx b/src/invert/laplace/impls/serial_band/serial_band.cxx index 9ab35314ca..4a62254a23 100644 --- a/src/invert/laplace/impls/serial_band/serial_band.cxx +++ b/src/invert/laplace/impls/serial_band/serial_band.cxx @@ -158,9 +158,9 @@ FieldPerp LaplaceSerialBand::solve(const FieldPerp& b, const FieldPerp& x0) { A(ix, 4) = 0.; #else // Set coefficients - coef1 = coords->g11()(ix, jy); // X 2nd derivative - coef2 = coords->g33()(ix, jy); // Z 2nd derivative - coef3 = coords->g13()(ix, jy); // X-Z mixed derivatives + coef1 = coords->g11(ix, jy); // X 2nd derivative + coef2 = coords->g33(ix, jy); // Z 2nd derivative + coef3 = coords->g13(ix, jy); // X-Z mixed derivatives coef4 = 0.0; // X 1st derivative coef5 = 0.0; // Z 1st derivative coef6 = Acoef(ix, jy); // Constant @@ -178,7 +178,7 @@ FieldPerp LaplaceSerialBand::solve(const FieldPerp& b, const FieldPerp& x0) { if (nonuniform) { // non-uniform localmesh correction if ((ix != 0) && (ix != ncx)) { - coef4 += coords->g11()(ix, jy) + coef4 += coords->g11(ix, jy) * ((1.0 / coords->dx(ix + 1, jy)) - (1.0 / coords->dx(ix - 1, jy))) / (2.0 * coords->dx(ix, jy)); } @@ -187,7 +187,7 @@ FieldPerp LaplaceSerialBand::solve(const FieldPerp& b, const FieldPerp& x0) { // A first order derivative term (1/c)\nabla_perp c\cdot\nabla_\perp x if ((ix > 1) && (ix < (localmesh->LocalNx - 2))) { - coef4 += coords->g11()(ix, jy) + coef4 += coords->g11(ix, jy) * (Ccoef(ix - 2, jy) - 8. * Ccoef(ix - 1, jy) + 8. * Ccoef(ix + 1, jy) - Ccoef(ix + 2, jy)) / (12. * coords->dx(ix, jy) * (Ccoef(ix, jy))); @@ -214,9 +214,9 @@ FieldPerp LaplaceSerialBand::solve(const FieldPerp& b, const FieldPerp& x0) { int ix = 1; auto kwave = kwave_(ix, jy); - coef1 = coords->g11()(ix, jy) / (SQ(coords->dx(ix, jy))); - coef2 = coords->g33()(ix, jy); - coef3 = kwave * coords->g13()(ix, jy) / (2. * coords->dx(ix, jy)); + coef1 = coords->g11(ix, jy) / (SQ(coords->dx(ix, jy))); + coef2 = coords->g33(ix, jy); + coef3 = kwave * coords->g13(ix, jy) / (2. * coords->dx(ix, jy)); // Multiply Delp2 component by a factor coef1 *= Dcoef(ix, jy); @@ -231,9 +231,9 @@ FieldPerp LaplaceSerialBand::solve(const FieldPerp& b, const FieldPerp& x0) { ix = ncx - 1; - coef1 = coords->g11()(ix, jy) / (SQ(coords->dx(ix, jy))); - coef2 = coords->g33()(ix, jy); - coef3 = kwave * coords->g13()(ix, jy) / (2. * coords->dx(ix, jy)); + coef1 = coords->g11(ix, jy) / (SQ(coords->dx(ix, jy))); + coef2 = coords->g33(ix, jy); + coef3 = kwave * coords->g13(ix, jy) / (2. * coords->dx(ix, jy)); A(ix, 0) = 0.0; A(ix, 1) = dcomplex(coef1, -coef3); @@ -337,28 +337,28 @@ FieldPerp LaplaceSerialBand::solve(const FieldPerp& b, const FieldPerp& x0) { int ix = 1; auto kwave = kwave_(ix, jy); - coef1 = coords->g11()(ix, jy) / (12. * SQ(coords->dx(ix, jy))); + coef1 = coords->g11(ix, jy) / (12. * SQ(coords->dx(ix, jy))); - coef2 = coords->g33()(ix, jy); + coef2 = coords->g33(ix, jy); - coef3 = kwave * coords->g13()(ix, jy) / (2. * coords->dx(ix, jy)); + coef3 = kwave * coords->g13(ix, jy) / (2. * coords->dx(ix, jy)); coef4 = Acoef(ix, jy); // Combine 4th order at 1 with 2nd order at 0 A(1, 0) = 0.0; // Not used A(1, 1) = dcomplex((14. - - SQ(coords->dx(0, jy) * kwave) * coords->g33()(0, jy) - / coords->g11()(0, jy)) + - SQ(coords->dx(0, jy) * kwave) * coords->g33(0, jy) + / coords->g11(0, jy)) * coef1, -coef3); A(1, 2) = dcomplex(-29. * coef1 - SQ(kwave) * coef2 + coef4, 0.0); A(1, 3) = dcomplex(16. * coef1, coef3); A(1, 4) = dcomplex(-coef1, 0.0); - coef1 = coords->g11()(ix, jy) / (SQ(coords->dx(ix, jy))); - coef2 = coords->g33()(ix, jy); - coef3 = kwave * coords->g13()(ix, jy) / (2. * coords->dx(ix, jy)); + coef1 = coords->g11(ix, jy) / (SQ(coords->dx(ix, jy))); + coef2 = coords->g33(ix, jy); + coef3 = kwave * coords->g13(ix, jy) / (2. * coords->dx(ix, jy)); // Use 2nd order at 1 A(0, 0) = 0.0; // Should never be used @@ -381,12 +381,12 @@ FieldPerp LaplaceSerialBand::solve(const FieldPerp& b, const FieldPerp& x0) { int ix = ncx - 1; - coef1 = coords->g11()(ix, jy) / (12. * SQ(coords->dx(ix, jy))); + coef1 = coords->g11(ix, jy) / (12. * SQ(coords->dx(ix, jy))); - coef2 = coords->g33()(ix, jy); + coef2 = coords->g33(ix, jy); auto kwave = kwave_(ix, jy); - coef3 = kwave * coords->g13()(ix, jy) / (2. * coords->dx(ix, jy)); + coef3 = kwave * coords->g13(ix, jy) / (2. * coords->dx(ix, jy)); coef4 = Acoef(ix, jy); @@ -395,15 +395,15 @@ FieldPerp LaplaceSerialBand::solve(const FieldPerp& b, const FieldPerp& x0) { A(ix, 1) = dcomplex(16. * coef1, -coef3); A(ix, 2) = dcomplex(-29. * coef1 - SQ(kwave) * coef2 + coef4, 0.0); A(ix, 3) = dcomplex((14. - - SQ(coords->dx(ncx, jy) * kwave) * coords->g33()(ncx, jy) - / coords->g11()(ncx, jy)) + - SQ(coords->dx(ncx, jy) * kwave) * coords->g33(ncx, jy) + / coords->g11(ncx, jy)) * coef1, coef3); A(ix, 4) = 0.0; // Not used - coef1 = coords->g11()(ix, jy) / (SQ(coords->dx(ix, jy))); - coef2 = coords->g33()(ix, jy); - coef3 = kwave * coords->g13()(ix, jy) / (2. * coords->dx(ix, jy)); + coef1 = coords->g11(ix, jy) / (SQ(coords->dx(ix, jy))); + coef2 = coords->g33(ix, jy); + coef3 = kwave * coords->g13(ix, jy) / (2. * coords->dx(ix, jy)); // Use 2nd order at ncx - 1 A(ncx, 0) = dcomplex(coef1, -coef3); diff --git a/src/invert/laplace/invert_laplace.cxx b/src/invert/laplace/invert_laplace.cxx index e317c767e5..a5e6ff4dd1 100644 --- a/src/invert/laplace/invert_laplace.cxx +++ b/src/invert/laplace/invert_laplace.cxx @@ -324,9 +324,9 @@ void Laplacian::tridagCoefs(int jx, int jy, BoutReal kwave, dcomplex& a, dcomple BoutReal coef1, coef2, coef3, coef4, coef5; - coef1 = localcoords->g11()(jx, jy); ///< X 2nd derivative coefficient - coef2 = localcoords->g33()(jx, jy); ///< Z 2nd derivative coefficient - coef3 = 2. * localcoords->g13()(jx, jy); ///< X-Z mixed derivative coefficient + coef1 = localcoords->g11(jx, jy); ///< X 2nd derivative coefficient + coef2 = localcoords->g33(jx, jy); ///< Z 2nd derivative coefficient + coef3 = 2. * localcoords->g13(jx, jy); ///< X-Z mixed derivative coefficient coef4 = 0.0; coef5 = 0.0; @@ -360,14 +360,14 @@ void Laplacian::tridagCoefs(int jx, int jy, BoutReal kwave, dcomplex& a, dcomple if ((jx > 0) && (jx < (localmesh->LocalNx - 1))) { BoutReal dc2dx_over_c1 = ((*c2coef)(jx + 1, jy) - (*c2coef)(jx - 1, jy)) / (2. * localcoords->dx(jx, jy) * ((*c1coef)(jx, jy))); - coef4 += localcoords->g11()(jx, jy) * dc2dx_over_c1; - coef5 += localcoords->g13()(jx, jy) * dc2dx_over_c1; + coef4 += localcoords->g11(jx, jy) * dc2dx_over_c1; + coef5 += localcoords->g13(jx, jy) * dc2dx_over_c1; } } if (localmesh->IncIntShear) { // d2dz2 term - coef2 += localcoords->g11()(jx, jy) * localcoords->IntShiftTorsion()(jx, jy) + coef2 += localcoords->g11(jx, jy) * localcoords->IntShiftTorsion()(jx, jy) * localcoords->IntShiftTorsion()(jx, jy); // Mixed derivative coef3 = 0.0; // This cancels out @@ -543,7 +543,7 @@ void Laplacian::tridagMatrix(dcomplex* avec, dcomplex* bvec, dcomplex* cvec, dco for (int ix = 0; ix < inbndry; ix++) { avec[ix] = 0.; bvec[ix] = 1.; - cvec[ix] = -exp(-k * coords->dx(ix, jy) / sqrt(coords->g11()(ix, jy))); + cvec[ix] = -exp(-k * coords->dx(ix, jy) / sqrt(coords->g11(ix, jy))); } } else if (isInnerBoundaryFlagSet(INVERT_IN_CYLINDER)) { // Condition for inner radial boundary for cylindrical coordinates @@ -622,7 +622,7 @@ void Laplacian::tridagMatrix(dcomplex* avec, dcomplex* bvec, dcomplex* cvec, dco for (int ix = 0; ix < inbndry; ix++) { avec[ix] = 0.0; bvec[ix] = 1.0; - cvec[ix] = -exp(-1.0 * sqrt(coords->g33()(ix, jy) / coords->g11()(ix, jy)) + cvec[ix] = -exp(-1.0 * sqrt(coords->g33(ix, jy) / coords->g11(ix, jy)) * kwave * coords->dx(ix, jy)); } } else if (isInnerBoundaryFlagSet(INVERT_IN_CYLINDER)) { @@ -707,7 +707,7 @@ void Laplacian::tridagMatrix(dcomplex* avec, dcomplex* bvec, dcomplex* cvec, dco cvec[ncx - ix] = 0.; bvec[ncx - ix] = 1.; avec[ncx - ix] = - -exp(-k * coords->dx(xe - ix, jy) / sqrt(coords->g11()(xe - ix, jy))); + -exp(-k * coords->dx(xe - ix, jy) / sqrt(coords->g11(xe - ix, jy))); } } else { // Order 2 dirichlet BC (boundary half between points) @@ -744,7 +744,7 @@ void Laplacian::tridagMatrix(dcomplex* avec, dcomplex* bvec, dcomplex* cvec, dco // Use decaying zero-Laplacian solution in the boundary for (int ix = 0; ix < outbndry; ix++) { avec[ncx - ix] = - -exp(-1.0 * sqrt(coords->g33()(xe - ix, jy) / coords->g11()(xe - ix, jy)) + -exp(-1.0 * sqrt(coords->g33(xe - ix, jy) / coords->g11(xe - ix, jy)) * kwave * coords->dx(xe - ix, jy)); bvec[ncx - ix] = 1.0; cvec[ncx - ix] = 0.0; diff --git a/src/invert/laplacexz/impls/cyclic/laplacexz-cyclic.cxx b/src/invert/laplacexz/impls/cyclic/laplacexz-cyclic.cxx index 3f482a167d..99697e9f63 100644 --- a/src/invert/laplacexz/impls/cyclic/laplacexz-cyclic.cxx +++ b/src/invert/laplacexz/impls/cyclic/laplacexz-cyclic.cxx @@ -117,7 +117,7 @@ void LaplaceXZcyclic::setCoefs(const Field2D& A2D, const Field2D& B2D) { // Metrics on x+1/2 boundary BoutReal J = 0.5 * (coord->J()(x, y) + coord->J()(x + 1, y)); - BoutReal g11 = 0.5 * (coord->g11()(x, y) + coord->g11()(x + 1, y)); + BoutReal g11 = 0.5 * (coord->g11(x, y) + coord->g11(x + 1, y)); BoutReal dx = 0.5 * (coord->dx(x, y) + coord->dx(x + 1, y)); BoutReal A = 0.5 * (A2D(x, y) + A2D(x + 1, y)); @@ -128,7 +128,7 @@ void LaplaceXZcyclic::setCoefs(const Field2D& A2D, const Field2D& B2D) { // Metrics on x-1/2 boundary J = 0.5 * (coord->J()(x, y) + coord->J()(x - 1, y)); - g11 = 0.5 * (coord->g11()(x, y) + coord->g11()(x - 1, y)); + g11 = 0.5 * (coord->g11(x, y) + coord->g11(x - 1, y)); dx = 0.5 * (coord->dx(x, y) + coord->dx(x - 1, y)); A = 0.5 * (A2D(x, y) + A2D(x - 1, y)); @@ -137,7 +137,7 @@ void LaplaceXZcyclic::setCoefs(const Field2D& A2D, const Field2D& B2D) { bcoef(ind, x - xstart) -= val; // ZZ component - bcoef(ind, x - xstart) -= A2D(x, y) * SQ(kwave) * coord->g33()(x, y); + bcoef(ind, x - xstart) -= A2D(x, y) * SQ(kwave) * coord->g33(x, y); } // Outer X boundary diff --git a/src/invert/laplacexz/impls/petsc/laplacexz-petsc.cxx b/src/invert/laplacexz/impls/petsc/laplacexz-petsc.cxx index b79154e4ce..6116b0cac8 100644 --- a/src/invert/laplacexz/impls/petsc/laplacexz-petsc.cxx +++ b/src/invert/laplacexz/impls/petsc/laplacexz-petsc.cxx @@ -404,7 +404,7 @@ void LaplaceXZpetsc::setCoefs(const Field3D& Ain, const Field3D& Bin) { // Metrics on x+1/2 boundary const BoutReal J = 0.5 * (coords->J()(x, y, z) + coords->J()(x + 1, y, z)); const BoutReal g11 = - 0.5 * (coords->g11()(x, y, z) + coords->g11()(x + 1, y, z)); + 0.5 * (coords->g11(x, y, z) + coords->g11(x + 1, y, z)); const BoutReal dx = 0.5 * (coords->dx(x, y, z) + coords->dx(x + 1, y, z)); const BoutReal Acoef = 0.5 * (A(x, y, z) + A(x + 1, y, z)); @@ -418,7 +418,7 @@ void LaplaceXZpetsc::setCoefs(const Field3D& Ain, const Field3D& Bin) { // Metrics on x-1/2 boundary const BoutReal J = 0.5 * (coords->J()(x, y, z) + coords->J()(x - 1, y, z)); const BoutReal g11 = - 0.5 * (coords->g11()(x, y, z) + coords->g11()(x - 1, y, z)); + 0.5 * (coords->g11(x, y, z) + coords->g11(x - 1, y, z)); const BoutReal dx = 0.5 * (coords->dx(x, y, z) + coords->dx(x - 1, y, z)); const BoutReal Acoef = 0.5 * (A(x, y, z) + A(x - 1, y, z)); @@ -436,7 +436,7 @@ void LaplaceXZpetsc::setCoefs(const Field3D& Ain, const Field3D& Bin) { { const BoutReal J = 0.5 * (coords->J()(x, y, z) + coords->J()(x, y, zplus)); const BoutReal g33 = - 0.5 * (coords->g33()(x, y, z) + coords->g33()(x, y, zplus)); + 0.5 * (coords->g33(x, y, z) + coords->g33(x, y, zplus)); const BoutReal dz = 0.5 * (coords->dz(x, y, z) + coords->dz(x, y, zplus)); // Metrics on z+1/2 boundary const BoutReal Acoef = 0.5 * (A(x, y, z) + A(x, y, zplus)); @@ -449,7 +449,7 @@ void LaplaceXZpetsc::setCoefs(const Field3D& Ain, const Field3D& Bin) { // Metrics on z-1/2 boundary const BoutReal J = 0.5 * (coords->J()(x, y, z) + coords->J()(x, y, zminus)); const BoutReal g33 = - 0.5 * (coords->g33()(x, y, z) + coords->g33()(x, y, zminus)); + 0.5 * (coords->g33(x, y, z) + coords->g33(x, y, zminus)); const BoutReal Acoef = 0.5 * (A(x, y, z) + A(x, y, zminus)); const BoutReal dz = 0.5 * (coords->dz(x, y, z) + coords->dz(x, y, zminus)); @@ -476,7 +476,7 @@ void LaplaceXZpetsc::setCoefs(const Field3D& Ain, const Field3D& Bin) { // Metrics const BoutReal J = 0.5 * (coords->J()(x, y, z) + coords->J()(x + 1, y, z)); const BoutReal g13 = - 0.5 * (coords->g13()(x, y, z) + coords->g13()(x + 1, y, z)); + 0.5 * (coords->g13(x, y, z) + coords->g13(x + 1, y, z)); const BoutReal dz = 0.5 * (coords->dz(x, y, z) + coords->dz(x + 1, y, z)); const BoutReal Acoef = 0.5 * (A(x, y, z) + A(x + 1, y, z)); @@ -513,7 +513,7 @@ void LaplaceXZpetsc::setCoefs(const Field3D& Ain, const Field3D& Bin) { const BoutReal J = 0.5 * (coords->J()(x, y, z) + coords->J()(x - 1, y, z)); const BoutReal g13 = - 0.5 * (coords->g13()(x, y, z) + coords->g13()(x - 1, y, z)); + 0.5 * (coords->g13(x, y, z) + coords->g13(x - 1, y, z)); const BoutReal dz = 0.5 * (coords->dz(x, y, z) + coords->dz(x - 1, y, z)); const BoutReal Acoef = 0.5 * (A(x, y, z) + A(x - 1, y, z)); @@ -547,7 +547,7 @@ void LaplaceXZpetsc::setCoefs(const Field3D& Ain, const Field3D& Bin) { const BoutReal J = 0.5 * (coords->J()(x, y, z) + coords->J()(x, y, zplus)); const BoutReal g13 = - 0.5 * (coords->g13()(x, y, z) + coords->g13()(x, y, zplus)); + 0.5 * (coords->g13(x, y, z) + coords->g13(x, y, zplus)); const BoutReal dx = 0.5 * (coords->dx(x, y, z) + coords->dx(x, y, zplus)); const BoutReal Acoef = 0.5 * (A(x, y, z) + A(x, y, zplus)); @@ -581,7 +581,7 @@ void LaplaceXZpetsc::setCoefs(const Field3D& Ain, const Field3D& Bin) { const BoutReal J = 0.5 * (coords->J()(x, y, z) + coords->J()(x, y, zminus)); const BoutReal g13 = - 0.5 * (coords->g13()(x, y, z) + coords->g13()(x, y, zminus)); + 0.5 * (coords->g13(x, y, z) + coords->g13(x, y, zminus)); const BoutReal dx = 0.5 * (coords->dx(x, y, z) + coords->dx(x, y, zminus)); const BoutReal Acoef = 0.5 * (A(x, y, z) + A(x, y, zminus)); diff --git a/src/mesh/boundary_standard.cxx b/src/mesh/boundary_standard.cxx index 3296ffc995..785225b1d4 100644 --- a/src/mesh/boundary_standard.cxx +++ b/src/mesh/boundary_standard.cxx @@ -1606,11 +1606,11 @@ void BoundaryNeumann_NonOrthogonal::apply(Field2D& f) { for (bndry->first(); !bndry->isDone(); bndry->next1d()) { // Interpolate (linearly) metrics to halfway between last cell and boundary cell BoutReal g11shift = 0.5 - * (metric->g11()(bndry->x, bndry->y) - + metric->g11()(bndry->x - bndry->bx, bndry->y)); + * (metric->g11(bndry->x, bndry->y) + + metric->g11(bndry->x - bndry->bx, bndry->y)); BoutReal g12shift = 0.5 - * (metric->g12()(bndry->x, bndry->y) - + metric->g12()(bndry->x - bndry->bx, bndry->y)); + * (metric->g12(bndry->x, bndry->y) + + metric->g12(bndry->x - bndry->bx, bndry->y)); // Have to use derivatives at last gridpoint instead of derivatives on boundary layer // because derivative values don't exist in boundary region // NOTE: should be fixed to interpolate to boundary line @@ -1666,14 +1666,14 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { #endif // Interpolate (linearly) metrics to halfway between last cell and boundary cell BoutReal g11shift = 0.5 - * (metric->g11()(bndry->x, bndry->y, z) - + metric->g11()(bndry->x - bndry->bx, bndry->y, z)); + * (metric->g11(bndry->x, bndry->y, z) + + metric->g11(bndry->x - bndry->bx, bndry->y, z)); BoutReal g12shift = 0.5 - * (metric->g12()(bndry->x, bndry->y, z) - + metric->g12()(bndry->x - bndry->bx, bndry->y, z)); + * (metric->g12(bndry->x, bndry->y, z) + + metric->g12(bndry->x - bndry->bx, bndry->y, z)); BoutReal g13shift = 0.5 - * (metric->g13()(bndry->x, bndry->y, z) - + metric->g13()(bndry->x - bndry->bx, bndry->y, z)); + * (metric->g13(bndry->x, bndry->y, z) + + metric->g13(bndry->x - bndry->bx, bndry->y, z)); // Have to use derivatives at last gridpoint instead of derivatives on boundary // layer // because derivative values don't exist in boundary region @@ -2666,7 +2666,7 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { // kz != 0 solution BoutReal coef = - -1.0 * sqrt(metric->g33()(x, y) / metric->g11()(x, y)) * metric->dx(x, y); + -1.0 * sqrt(metric->g33(x, y) / metric->g11(x, y)) * metric->dx(x, y); for (int jz = 1; jz <= ncz / 2; jz++) { BoutReal kwave = jz * 2.0 * PI / metric->zlength()(x, y); // wavenumber in [rad^-1] @@ -2878,16 +2878,16 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { // kz = 0 solution xpos -= metric->dx(x, y); c2[0] = - c0[0] + k0lin * xpos + 0.5 * c1[0] * xpos * xpos / metric->g11()(x - bx, y); + c0[0] + k0lin * xpos + 0.5 * c1[0] * xpos * xpos / metric->g11(x - bx, y); // kz != 0 solution - BoutReal coef = -1.0 * sqrt(metric->g33()(x - bx, y) / metric->g11()(x - bx, y)) + BoutReal coef = -1.0 * sqrt(metric->g33(x - bx, y) / metric->g11(x - bx, y)) * metric->dx(x - bx, y); for (int jz = 1; jz <= ncz / 2; jz++) { BoutReal kwave = jz * 2.0 * PI / getUniform(metric->zlength()); // wavenumber in [rad^-1] c0[jz] *= exp(coef * kwave); // The decaying solution only // Add the particular solution - c2[jz] = c0[jz] - c1[jz] / (metric->g33()(x - bx, y) * kwave * kwave); + c2[jz] = c0[jz] - c1[jz] / (metric->g33(x - bx, y) * kwave * kwave); } // Reverse FFT irfft(c2.begin(), ncz, f(x, y)); @@ -2972,46 +2972,46 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { // d/dx( Jmetric->g11 B_x ) = - d/dx( Jmetric->g12 B_y + Jmetric->g13 B_z) // - d/dy( JB^y ) - d/dz( JB^z ) - tmp = -(metric->J()(jx, jy) * metric->g12()(jx, jy) * var.y(jx, jy, jz) - + metric->J()(jx, jy) * metric->g13()(jx, jy) * var.z(jx, jy, jz) - - metric->J()(jx - 2, jy) * metric->g12()(jx - 2, jy) + tmp = -(metric->J()(jx, jy) * metric->g12(jx, jy) * var.y(jx, jy, jz) + + metric->J()(jx, jy) * metric->g13(jx, jy) * var.z(jx, jy, jz) + - metric->J()(jx - 2, jy) * metric->g12(jx - 2, jy) * var.y(jx - 2, jy, jz) - + metric->J()(jx - 2, jy) * metric->g13()(jx - 2, jy) + + metric->J()(jx - 2, jy) * metric->g13(jx - 2, jy) * var.z(jx - 2, jy, jz)) / (metric->dx(jx - 2, jy) + metric->dx(jx - 1, jy)); // First term (d/dx) using vals calculated above tmp -= - (metric->J()(jx - 1, jy + 1) * metric->g12()(jx - 1, jy + 1) + (metric->J()(jx - 1, jy + 1) * metric->g12(jx - 1, jy + 1) * var.x(jx - 1, jy + 1, jz) - - metric->J()(jx - 1, jy - 1) * metric->g12()(jx - 1, jy - 1) + - metric->J()(jx - 1, jy - 1) * metric->g12(jx - 1, jy - 1) * var.x(jx - 1, jy - 1, jz) - + metric->J()(jx - 1, jy + 1) * metric->g22()(jx - 1, jy + 1) + + metric->J()(jx - 1, jy + 1) * metric->g22(jx - 1, jy + 1) * var.y(jx - 1, jy + 1, jz) - - metric->J()(jx - 1, jy - 1) * metric->g22()(jx - 1, jy - 1) + - metric->J()(jx - 1, jy - 1) * metric->g22(jx - 1, jy - 1) * var.y(jx - 1, jy - 1, jz) - + metric->J()(jx - 1, jy + 1) * metric->g23()(jx - 1, jy + 1) + + metric->J()(jx - 1, jy + 1) * metric->g23(jx - 1, jy + 1) * var.z(jx - 1, jy + 1, jz) - - metric->J()(jx - 1, jy - 1) * metric->g23()(jx - 1, jy - 1) + - metric->J()(jx - 1, jy - 1) * metric->g23(jx - 1, jy - 1) * var.z(jx - 1, jy - 1, jz)) / (metric->dy(jx - 1, jy - 1) + metric->dy(jx - 1, jy)); // second (d/dy) - tmp -= (metric->J()(jx - 1, jy) * metric->g13()(jx - 1, jy) + tmp -= (metric->J()(jx - 1, jy) * metric->g13(jx - 1, jy) * (var.x(jx - 1, jy, jzp) - var.x(jx - 1, jy, jzm)) - + metric->J()(jx - 1, jy) * metric->g23()(jx - 1, jy) + + metric->J()(jx - 1, jy) * metric->g23(jx - 1, jy) * (var.y(jx - 1, jy, jzp) - var.y(jx - 1, jy, jzm)) - + metric->J()(jx - 1, jy) * metric->g33()(jx - 1, jy) + + metric->J()(jx - 1, jy) * metric->g33(jx - 1, jy) * (var.z(jx - 1, jy, jzp) - var.z(jx - 1, jy, jzm))) / (2. * metric->dz(jx - 1, jy)); var.x(jx, jy, jz) = - (metric->J()(jx - 2, jy) * metric->g11()(jx - 2, jy) * var.x(jx - 2, jy, jz) + (metric->J()(jx - 2, jy) * metric->g11(jx - 2, jy) * var.x(jx - 2, jy, jz) + (metric->dx(jx - 2, jy) + metric->dx(jx - 1, jy)) * tmp) - / metric->J()(jx, jy) * metric->g11()(jx, jy); + / metric->J()(jx, jy) * metric->g11(jx, jy); if (mesh->xstart == 2) { var.x(jx + 1, jy, jz) = - (metric->J()(jx - 3, jy) * metric->g11()(jx - 3, jy) * var.x(jx - 3, jy, jz) + (metric->J()(jx - 3, jy) * metric->g11(jx - 3, jy) * var.x(jx - 3, jy, jz) + 4. * metric->dx(jx, jy) * tmp) - / metric->J()(jx + 1, jy) * metric->g11()(jx + 1, jy); + / metric->J()(jx + 1, jy) * metric->g11(jx + 1, jy); } } } diff --git a/src/mesh/fv_ops.cxx b/src/mesh/fv_ops.cxx index 0180dd9c84..fc58239959 100644 --- a/src/mesh/fv_ops.cxx +++ b/src/mesh/fv_ops.cxx @@ -52,8 +52,8 @@ Field3D Div_a_Grad_perp(const Field3D& a, const Field3D& f) { // Calculate flux from i to i+1 BoutReal const fout = 0.5 * (a(i, j, k) + a(i + 1, j, k)) - * (coord->J()(i, j, k) * coord->g11()(i, j, k) - + coord->J()(i + 1, j, k) * coord->g11()(i + 1, j, k)) + * (coord->J()(i, j, k) * coord->g11(i, j, k) + + coord->J()(i + 1, j, k) * coord->g11(i + 1, j, k)) * (f(i + 1, j, k) - f(i, j, k)) / (coord->dx(i, j, k) + coord->dx(i + 1, j, k)); @@ -514,32 +514,32 @@ Field3D Div_Perp_Lap(const Field3D& a, const Field3D& f, CELL_LOC outloc) { // Calculate gradients on cell faces -- assumes constant grid spacing BoutReal const gR = - (coords->g11()(i, j, k) + coords->g11()(i + 1, j, k)) + (coords->g11(i, j, k) + coords->g11(i + 1, j, k)) * (f(i + 1, j, k) - f(i, j, k)) / (coords->dx(i + 1, j, k) + coords->dx(i, j, k)) - + 0.5 * (coords->g13()(i, j, k) + coords->g13()(i + 1, j, k)) + + 0.5 * (coords->g13(i, j, k) + coords->g13(i + 1, j, k)) * (f(i + 1, j, kp) - f(i + 1, j, km) + f(i, j, kp) - f(i, j, km)) / (4. * coords->dz(i, j, k)); BoutReal const gL = - (coords->g11()(i - 1, j, k) + coords->g11()(i, j, k)) + (coords->g11(i - 1, j, k) + coords->g11(i, j, k)) * (f(i, j, k) - f(i - 1, j, k)) / (coords->dx(i - 1, j, k) + coords->dx(i, j, k)) - + 0.5 * (coords->g13()(i - 1, j, k) + coords->g13()(i, j, k)) + + 0.5 * (coords->g13(i - 1, j, k) + coords->g13(i, j, k)) * (f(i - 1, j, kp) - f(i - 1, j, km) + f(i, j, kp) - f(i, j, km)) / (4 * coords->dz(i, j, k)); BoutReal const gD = - coords->g13()(i, j, k) + coords->g13(i, j, k) * (f(i + 1, j, km) - f(i - 1, j, km) + f(i + 1, j, k) - f(i - 1, j, k)) / (4. * coords->dx(i, j, k)) - + coords->g33()(i, j, k) * (f(i, j, k) - f(i, j, km)) / coords->dz(i, j, k); + + coords->g33(i, j, k) * (f(i, j, k) - f(i, j, km)) / coords->dz(i, j, k); BoutReal const gU = - coords->g13()(i, j, k) + coords->g13(i, j, k) * (f(i + 1, j, kp) - f(i - 1, j, kp) + f(i + 1, j, k) - f(i - 1, j, k)) / (4. * coords->dx(i, j, k)) - + coords->g33()(i, j, k) * (f(i, j, kp) - f(i, j, k)) / coords->dz(i, j, k); + + coords->g33(i, j, k) * (f(i, j, kp) - f(i, j, k)) / coords->dz(i, j, k); // Flow right BoutReal flux = gR * 0.25 * (coords->J()(i + 1, j, k) + coords->J()(i, j, k)) diff --git a/tests/MMS/GBS/gbs.cxx b/tests/MMS/GBS/gbs.cxx index dc0b867f29..65cd65cfd0 100644 --- a/tests/MMS/GBS/gbs.cxx +++ b/tests/MMS/GBS/gbs.cxx @@ -298,9 +298,9 @@ int GBS::init(bool restarting) { output.write("dx = {:e}, dy = {:e}, dz = {:e}\n", (coords->dx())(2, 2), (coords->dy())(2, 2), coords->dz()); - output.write("g11 = {:e}, g22 = {:e}, g33 = {:e}\n", coords->g11()(2, 2), - coords->g22()(2, 2), coords->g33()(2, 2)); - output.write("g12 = {:e}, g23 = {:e}\n", coords->g12()(2, 2), coords->g23()(2, 2)); + output.write("g11 = {:e}, g22 = {:e}, g33 = {:e}\n", coords->g11(2, 2), + coords->g22(2, 2), coords->g33(2, 2)); + output.write("g12 = {:e}, g23 = {:e}\n", coords->g12(2, 2), coords->g23(2, 2)); output.write("g_11 = {:e}, g_22 = {:e}, g_33 = {:e}\n", coords->g_11()(2, 2), coords->g_22()(2, 2), coords->g_33()(2, 2)); output.write("g_12 = {:e}, g_23 = {:e}\n", coords->g_12()(2, 2), coords->g_23()(2, 2)); From 7041398271a07f69644ca1adc806fb0274b9fec2 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Tue, 24 Sep 2024 14:33:41 +0100 Subject: [PATCH 463/491] Getters for metric tensor components given (x, y) indices. --- include/bout/coordinates.hxx | 38 +++++++++++++++++++++++----------- include/bout/metric_tensor.hxx | 19 +++++++++++------ 2 files changed, 39 insertions(+), 18 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index 8205bca8c0..e393839ec7 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -147,20 +147,34 @@ public: const MetricTensor::FieldMetric& g23() const { return contravariantMetricTensor.g23(); } /// Covariant metric tensor - BoutReal g_11(int x, int y, int z) const { return covariantMetricTensor.g11(x, y, z); } - BoutReal g_22(int x, int y, int z) const { return covariantMetricTensor.g22(x, y, z); } - BoutReal g_33(int x, int y, int z) const { return covariantMetricTensor.g33(x, y, z); } - BoutReal g_12(int x, int y, int z) const { return covariantMetricTensor.g12(x, y, z); } - BoutReal g_13(int x, int y, int z) const { return covariantMetricTensor.g13(x, y, z); } - BoutReal g_23(int x, int y, int z) const { return covariantMetricTensor.g23(x, y, z); } + const BoutReal& g_11(int x, int y, int z) const { return covariantMetricTensor.g11(x, y, z); } + const BoutReal& g_22(int x, int y, int z) const { return covariantMetricTensor.g22(x, y, z); } + const BoutReal& g_33(int x, int y, int z) const { return covariantMetricTensor.g33(x, y, z); } + const BoutReal& g_12(int x, int y, int z) const { return covariantMetricTensor.g12(x, y, z); } + const BoutReal& g_13(int x, int y, int z) const { return covariantMetricTensor.g13(x, y, z); } + const BoutReal& g_23(int x, int y, int z) const { return covariantMetricTensor.g23(x, y, z); } + + const BoutReal& g_11(int x, int y) const { return covariantMetricTensor.g11(x, y); } + const BoutReal& g_22(int x, int y) const { return covariantMetricTensor.g22(x, y); } + const BoutReal& g_33(int x, int y) const { return covariantMetricTensor.g33(x, y); } + const BoutReal& g_12(int x, int y) const { return covariantMetricTensor.g12(x, y); } + const BoutReal& g_13(int x, int y) const { return covariantMetricTensor.g13(x, y); } + const BoutReal& g_23(int x, int y) const { return covariantMetricTensor.g23(x, y); } /// Contravariant metric tensor (g^{ij}) - BoutReal g11(int x, int y, int z) const { return contravariantMetricTensor.g11(x, y, z); } - BoutReal g22(int x, int y, int z) const { return contravariantMetricTensor.g22(x, y, z); } - BoutReal g33(int x, int y, int z) const { return contravariantMetricTensor.g33(x, y, z); } - BoutReal g12(int x, int y, int z) const { return contravariantMetricTensor.g12(x, y, z); } - BoutReal g13(int x, int y, int z) const { return contravariantMetricTensor.g13(x, y, z); } - BoutReal g23(int x, int y, int z) const { return contravariantMetricTensor.g23(x, y, z); } + const BoutReal& g11(int x, int y, int z) const { return contravariantMetricTensor.g11(x, y, z); } + const BoutReal& g22(int x, int y, int z) const { return contravariantMetricTensor.g22(x, y, z); } + const BoutReal& g33(int x, int y, int z) const { return contravariantMetricTensor.g33(x, y, z); } + const BoutReal& g12(int x, int y, int z) const { return contravariantMetricTensor.g12(x, y, z); } + const BoutReal& g13(int x, int y, int z) const { return contravariantMetricTensor.g13(x, y, z); } + const BoutReal& g23(int x, int y, int z) const { return contravariantMetricTensor.g23(x, y, z); } + + const BoutReal& g11(int x, int y) const { return contravariantMetricTensor.g11(x, y); } + const BoutReal& g22(int x, int y) const { return contravariantMetricTensor.g22(x, y); } + const BoutReal& g33(int x, int y) const { return contravariantMetricTensor.g33(x, y); } + const BoutReal& g12(int x, int y) const { return contravariantMetricTensor.g12(x, y); } + const BoutReal& g13(int x, int y) const { return contravariantMetricTensor.g13(x, y); } + const BoutReal& g23(int x, int y) const { return contravariantMetricTensor.g23(x, y); } const ContravariantMetricTensor& getContravariantMetricTensor() const { return contravariantMetricTensor; diff --git a/include/bout/metric_tensor.hxx b/include/bout/metric_tensor.hxx index f006475563..7224efb651 100644 --- a/include/bout/metric_tensor.hxx +++ b/include/bout/metric_tensor.hxx @@ -32,12 +32,19 @@ public: const FieldMetric& g13() const { return g13_; } const FieldMetric& g23() const { return g23_; } - BoutReal g11(int x, int y, int z) const { return g11_(x, y, z); } - BoutReal g22(int x, int y, int z) const { return g22_(x, y, z); } - BoutReal g33(int x, int y, int z) const { return g33_(x, y, z); } - BoutReal g12(int x, int y, int z) const { return g12_(x, y, z); } - BoutReal g13(int x, int y, int z) const { return g13_(x, y, z); } - BoutReal g23(int x, int y, int z) const { return g23_(x, y, z); } + const BoutReal& g11(int x, int y, int z) const { return g11_(x, y, z); } + const BoutReal& g22(int x, int y, int z) const { return g22_(x, y, z); } + const BoutReal& g33(int x, int y, int z) const { return g33_(x, y, z); } + const BoutReal& g12(int x, int y, int z) const { return g12_(x, y, z); } + const BoutReal& g13(int x, int y, int z) const { return g13_(x, y, z); } + const BoutReal& g23(int x, int y, int z) const { return g23_(x, y, z); } + + const BoutReal& g11(int x, int y) const { return g11_(x, y); } + const BoutReal& g22(int x, int y) const { return g22_(x, y); } + const BoutReal& g33(int x, int y) const { return g33_(x, y); } + const BoutReal& g12(int x, int y) const { return g12_(x, y); } + const BoutReal& g13(int x, int y) const { return g13_(x, y); } + const BoutReal& g23(int x, int y) const { return g23_(x, y); } void setMetricTensor(const MetricTensor& metric_tensor) { From f2e01abceb2959ac40ff5f1c8097bc30a17b617d Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 25 Sep 2024 13:58:59 +0100 Subject: [PATCH 464/491] The (x, y) indexing methods return a pointer when BOUT_USE_METRIC_3D and a reference otherwise). --- include/bout/coordinates.hxx | 22 ++++++++++++++++++++-- include/bout/metric_tensor.hxx | 9 +++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index e393839ec7..4a5478ed75 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -154,12 +154,21 @@ public: const BoutReal& g_13(int x, int y, int z) const { return covariantMetricTensor.g13(x, y, z); } const BoutReal& g_23(int x, int y, int z) const { return covariantMetricTensor.g23(x, y, z); } +#if BOUT_USE_METRIC_3D + const BoutReal* g_11(int x, int y) const { return covariantMetricTensor.g11(x, y); } + const BoutReal* g_22(int x, int y) const { return covariantMetricTensor.g22(x, y); } + const BoutReal* g_33(int x, int y) const { return covariantMetricTensor.g33(x, y); } + const BoutReal* g_12(int x, int y) const { return covariantMetricTensor.g12(x, y); } + const BoutReal* g_13(int x, int y) const { return covariantMetricTensor.g13(x, y); } + const BoutReal* g_23(int x, int y) const { return covariantMetricTensor.g23(x, y); } +#else const BoutReal& g_11(int x, int y) const { return covariantMetricTensor.g11(x, y); } const BoutReal& g_22(int x, int y) const { return covariantMetricTensor.g22(x, y); } const BoutReal& g_33(int x, int y) const { return covariantMetricTensor.g33(x, y); } const BoutReal& g_12(int x, int y) const { return covariantMetricTensor.g12(x, y); } const BoutReal& g_13(int x, int y) const { return covariantMetricTensor.g13(x, y); } const BoutReal& g_23(int x, int y) const { return covariantMetricTensor.g23(x, y); } +#endif /// Contravariant metric tensor (g^{ij}) const BoutReal& g11(int x, int y, int z) const { return contravariantMetricTensor.g11(x, y, z); } @@ -168,14 +177,23 @@ public: const BoutReal& g12(int x, int y, int z) const { return contravariantMetricTensor.g12(x, y, z); } const BoutReal& g13(int x, int y, int z) const { return contravariantMetricTensor.g13(x, y, z); } const BoutReal& g23(int x, int y, int z) const { return contravariantMetricTensor.g23(x, y, z); } - + +#if BOUT_USE_METRIC_3D + const BoutReal* g11(int x, int y) const { return contravariantMetricTensor.g11(x, y); } + const BoutReal* g22(int x, int y) const { return contravariantMetricTensor.g22(x, y); } + const BoutReal* g33(int x, int y) const { return contravariantMetricTensor.g33(x, y); } + const BoutReal* g12(int x, int y) const { return contravariantMetricTensor.g12(x, y); } + const BoutReal* g13(int x, int y) const { return contravariantMetricTensor.g13(x, y); } + const BoutReal* g23(int x, int y) const { return contravariantMetricTensor.g23(x, y); } +#else const BoutReal& g11(int x, int y) const { return contravariantMetricTensor.g11(x, y); } const BoutReal& g22(int x, int y) const { return contravariantMetricTensor.g22(x, y); } const BoutReal& g33(int x, int y) const { return contravariantMetricTensor.g33(x, y); } const BoutReal& g12(int x, int y) const { return contravariantMetricTensor.g12(x, y); } const BoutReal& g13(int x, int y) const { return contravariantMetricTensor.g13(x, y); } const BoutReal& g23(int x, int y) const { return contravariantMetricTensor.g23(x, y); } - +#endif + const ContravariantMetricTensor& getContravariantMetricTensor() const { return contravariantMetricTensor; } diff --git a/include/bout/metric_tensor.hxx b/include/bout/metric_tensor.hxx index 7224efb651..c7dc85a105 100644 --- a/include/bout/metric_tensor.hxx +++ b/include/bout/metric_tensor.hxx @@ -39,12 +39,21 @@ public: const BoutReal& g13(int x, int y, int z) const { return g13_(x, y, z); } const BoutReal& g23(int x, int y, int z) const { return g23_(x, y, z); } +#if BOUT_USE_METRIC_3D + const BoutReal* g11(int x, int y) const { return g11_(x, y); } + const BoutReal* g22(int x, int y) const { return g22_(x, y); } + const BoutReal* g33(int x, int y) const { return g33_(x, y); } + const BoutReal* g12(int x, int y) const { return g12_(x, y); } + const BoutReal* g13(int x, int y) const { return g13_(x, y); } + const BoutReal* g23(int x, int y) const { return g23_(x, y); } +#else const BoutReal& g11(int x, int y) const { return g11_(x, y); } const BoutReal& g22(int x, int y) const { return g22_(x, y); } const BoutReal& g33(int x, int y) const { return g33_(x, y); } const BoutReal& g12(int x, int y) const { return g12_(x, y); } const BoutReal& g13(int x, int y) const { return g13_(x, y); } const BoutReal& g23(int x, int y) const { return g23_(x, y); } +#endif void setMetricTensor(const MetricTensor& metric_tensor) { From 35d0912e0ffc3fbf77efedb72f3bb712531a802e Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 25 Sep 2024 14:25:25 +0100 Subject: [PATCH 465/491] Disable metric tensor getter with (x, y) arguments when BOUT_USE_METRIC_3D. --- include/bout/coordinates.hxx | 20 +++----------------- 1 file changed, 3 insertions(+), 17 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index 4a5478ed75..b964c62fce 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -154,14 +154,7 @@ public: const BoutReal& g_13(int x, int y, int z) const { return covariantMetricTensor.g13(x, y, z); } const BoutReal& g_23(int x, int y, int z) const { return covariantMetricTensor.g23(x, y, z); } -#if BOUT_USE_METRIC_3D - const BoutReal* g_11(int x, int y) const { return covariantMetricTensor.g11(x, y); } - const BoutReal* g_22(int x, int y) const { return covariantMetricTensor.g22(x, y); } - const BoutReal* g_33(int x, int y) const { return covariantMetricTensor.g33(x, y); } - const BoutReal* g_12(int x, int y) const { return covariantMetricTensor.g12(x, y); } - const BoutReal* g_13(int x, int y) const { return covariantMetricTensor.g13(x, y); } - const BoutReal* g_23(int x, int y) const { return covariantMetricTensor.g23(x, y); } -#else +#if BOUT_USE_METRIC_3D != 1 const BoutReal& g_11(int x, int y) const { return covariantMetricTensor.g11(x, y); } const BoutReal& g_22(int x, int y) const { return covariantMetricTensor.g22(x, y); } const BoutReal& g_33(int x, int y) const { return covariantMetricTensor.g33(x, y); } @@ -177,15 +170,8 @@ public: const BoutReal& g12(int x, int y, int z) const { return contravariantMetricTensor.g12(x, y, z); } const BoutReal& g13(int x, int y, int z) const { return contravariantMetricTensor.g13(x, y, z); } const BoutReal& g23(int x, int y, int z) const { return contravariantMetricTensor.g23(x, y, z); } - -#if BOUT_USE_METRIC_3D - const BoutReal* g11(int x, int y) const { return contravariantMetricTensor.g11(x, y); } - const BoutReal* g22(int x, int y) const { return contravariantMetricTensor.g22(x, y); } - const BoutReal* g33(int x, int y) const { return contravariantMetricTensor.g33(x, y); } - const BoutReal* g12(int x, int y) const { return contravariantMetricTensor.g12(x, y); } - const BoutReal* g13(int x, int y) const { return contravariantMetricTensor.g13(x, y); } - const BoutReal* g23(int x, int y) const { return contravariantMetricTensor.g23(x, y); } -#else + +#if BOUT_USE_METRIC_3D != 1 const BoutReal& g11(int x, int y) const { return contravariantMetricTensor.g11(x, y); } const BoutReal& g22(int x, int y) const { return contravariantMetricTensor.g22(x, y); } const BoutReal& g33(int x, int y) const { return contravariantMetricTensor.g33(x, y); } From 4852b0098e98c312a423fc5ac6401700fd5f65b1 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Fri, 27 Sep 2024 11:44:49 +0100 Subject: [PATCH 466/491] Use overloaded getters for metric tensor components. --- examples/conducting-wall-mode/cwm.cxx | 4 +-- include/bout/fv_ops.hxx | 8 ++--- .../impls/multigrid/multigrid_laplace.cxx | 8 ++--- .../laplace/impls/petsc/petsc_laplace.cxx | 28 ++++++++-------- .../laplace/impls/serial_band/serial_band.cxx | 16 +++++----- src/invert/laplace/invert_laplace.cxx | 32 +++++++++---------- src/invert/parderiv/impls/cyclic/cyclic.cxx | 2 +- src/mesh/boundary_standard.cxx | 8 ++--- src/mesh/difops.cxx | 6 ++-- tests/MMS/GBS/gbs.cxx | 6 ++-- .../test_multigrid_laplace.cxx | 4 +-- .../test_naulin_laplace.cxx | 4 +-- 12 files changed, 63 insertions(+), 63 deletions(-) diff --git a/examples/conducting-wall-mode/cwm.cxx b/examples/conducting-wall-mode/cwm.cxx index 0e2d65a040..d3f0f1ba57 100644 --- a/examples/conducting-wall-mode/cwm.cxx +++ b/examples/conducting-wall-mode/cwm.cxx @@ -308,7 +308,7 @@ class CWM : public PhysicsModel { var(xrup.ind, jy, jz) = var(xrup.ind, jy - 1, jz) + coord->dy(xrup.ind, jy, jz) - * sqrt(coord->g_22()(xrup.ind, jy, jz)) + * sqrt(coord->g_22(xrup.ind, jy, jz)) * value(xrup.ind, jy, jz); } } @@ -325,7 +325,7 @@ class CWM : public PhysicsModel { var(xrdn.ind, jy, jz) = var(xrdn.ind, jy + 1, jz) - coord->dy(xrdn.ind, jy, jz) - * sqrt(coord->g_22()(xrdn.ind, jy, jz)) + * sqrt(coord->g_22(xrdn.ind, jy, jz)) * value(xrdn.ind, jy, jz); } } diff --git a/include/bout/fv_ops.hxx b/include/bout/fv_ops.hxx index 7d5ec5d76a..ea3832c1be 100644 --- a/include/bout/fv_ops.hxx +++ b/include/bout/fv_ops.hxx @@ -245,7 +245,7 @@ const Field3D Div_par(const Field3D& f_in, const Field3D& v_in, // For right cell boundaries BoutReal common_factor = (coord->J()(i, j) + coord->J()(i, j + 1)) - / (sqrt(coord->g_22()(i, j)) + sqrt(coord->g_22()(i, j + 1))); + / (sqrt(coord->g_22(i, j)) + sqrt(coord->g_22(i, j + 1))); BoutReal flux_factor_rc = common_factor / (coord->dy(i, j) * coord->J()(i, j)); BoutReal flux_factor_rp = @@ -253,7 +253,7 @@ const Field3D Div_par(const Field3D& f_in, const Field3D& v_in, // For left cell boundaries common_factor = (coord->J()(i, j) + coord->J()(i, j - 1)) - / (sqrt(coord->g_22()(i, j)) + sqrt(coord->g_22()(i, j - 1))); + / (sqrt(coord->g_22(i, j)) + sqrt(coord->g_22(i, j - 1))); BoutReal flux_factor_lc = common_factor / (coord->dy(i, j) * coord->J()(i, j)); BoutReal flux_factor_lm = @@ -264,7 +264,7 @@ const Field3D Div_par(const Field3D& f_in, const Field3D& v_in, // For right cell boundaries BoutReal common_factor = (coord->J()(i, j, k) + coord->J()(i, j + 1, k)) - / (sqrt(coord->g_22()(i, j, k)) + sqrt(coord->g_22()(i, j + 1, k))); + / (sqrt(coord->g_22(i, j, k)) + sqrt(coord->g_22(i, j + 1, k))); BoutReal flux_factor_rc = common_factor / (coord->dy(i, j, k) * coord->J()(i, j, k)); @@ -274,7 +274,7 @@ const Field3D Div_par(const Field3D& f_in, const Field3D& v_in, // For left cell boundaries common_factor = (coord->J()(i, j, k) + coord->J()(i, j - 1, k)) - / (sqrt(coord->g_22()(i, j, k)) + sqrt(coord->g_22()(i, j - 1, k))); + / (sqrt(coord->g_22(i, j, k)) + sqrt(coord->g_22(i, j - 1, k))); BoutReal flux_factor_lc = common_factor / (coord->dy(i, j, k) * coord->J()(i, j, k)); diff --git a/src/invert/laplace/impls/multigrid/multigrid_laplace.cxx b/src/invert/laplace/impls/multigrid/multigrid_laplace.cxx index 302a176cc9..c5ddfc1631 100644 --- a/src/invert/laplace/impls/multigrid/multigrid_laplace.cxx +++ b/src/invert/laplace/impls/multigrid/multigrid_laplace.cxx @@ -284,7 +284,7 @@ FieldPerp LaplaceMultigrid::solve(const FieldPerp& b_in, const FieldPerp& x0) { for (int k = 1; k < lzz + 1; k++) { int k2 = k - 1; x[k] = -x0(localmesh->xstart - 1, k2) - * sqrt(coords->g_11()(localmesh->xstart, yindex)) + * sqrt(coords->g_11(localmesh->xstart, yindex)) * coords->dx(localmesh->xstart, yindex); } } else { @@ -328,7 +328,7 @@ FieldPerp LaplaceMultigrid::solve(const FieldPerp& b_in, const FieldPerp& x0) { for (int k = 1; k < lzz + 1; k++) { int k2 = k - 1; x[(lxx + 1) * lz2 + k] = x0(localmesh->xend + 1, k2) - * sqrt(coords->g_11()(localmesh->xend, yindex)) + * sqrt(coords->g_11(localmesh->xend, yindex)) * coords->dx(localmesh->xend, yindex); // this is the value to set the gradient to at the outer boundary } @@ -487,7 +487,7 @@ FieldPerp LaplaceMultigrid::solve(const FieldPerp& b_in, const FieldPerp& x0) { int k2 = k - 1; result(i2, k2) = x[lz2 + k] - x0(localmesh->xstart - 1, k2) - * sqrt(coords->g_11()(localmesh->xstart, yindex)) + * sqrt(coords->g_11(localmesh->xstart, yindex)) * coords->dx(localmesh->xstart, yindex); } } else { @@ -535,7 +535,7 @@ FieldPerp LaplaceMultigrid::solve(const FieldPerp& b_in, const FieldPerp& x0) { int k2 = k - 1; result(i2, k2) = x[lxx * lz2 + k] + x0(localmesh->xend + 1, k2) - * sqrt(coords->g_11()(localmesh->xend, yindex)) + * sqrt(coords->g_11(localmesh->xend, yindex)) * coords->dx(localmesh->xend, yindex); } } else { diff --git a/src/invert/laplace/impls/petsc/petsc_laplace.cxx b/src/invert/laplace/impls/petsc/petsc_laplace.cxx index 15eb3cbaa8..5b899dc262 100644 --- a/src/invert/laplace/impls/petsc/petsc_laplace.cxx +++ b/src/invert/laplace/impls/petsc/petsc_laplace.cxx @@ -386,18 +386,18 @@ FieldPerp LaplacePetsc::solve(const FieldPerp& b, const FieldPerp& x0) { // Fourth Order Accuracy on Boundary Element(i, x, z, 0, 0, -25.0 / (12.0 * coords->dx(x, y, z)) - / sqrt(coords->g_11()(x, y, z)), + / sqrt(coords->g_11(x, y, z)), MatA); Element(i, x, z, 1, 0, - 4.0 / coords->dx(x, y, z) / sqrt(coords->g_11()(x, y, z)), MatA); + 4.0 / coords->dx(x, y, z) / sqrt(coords->g_11(x, y, z)), MatA); Element(i, x, z, 2, 0, - -3.0 / coords->dx(x, y, z) / sqrt(coords->g_11()(x, y, z)), MatA); + -3.0 / coords->dx(x, y, z) / sqrt(coords->g_11(x, y, z)), MatA); Element(i, x, z, 3, 0, - 4.0 / (3.0 * coords->dx(x, y, z)) / sqrt(coords->g_11()(x, y, z)), + 4.0 / (3.0 * coords->dx(x, y, z)) / sqrt(coords->g_11(x, y, z)), MatA); Element(i, x, z, 4, 0, -1.0 / (4.0 * coords->dx(x, y, z)) - / sqrt(coords->g_11()(x, y, z)), + / sqrt(coords->g_11(x, y, z)), MatA); } else { // Second Order Accuracy on Boundary @@ -409,9 +409,9 @@ FieldPerp LaplacePetsc::solve(const FieldPerp& b, const FieldPerp& x0) { // Element(i,x,z, 4, 0, 0.0, MatA ); // Second Order Accuracy on Boundary, set half-way between grid points Element(i, x, z, 0, 0, - -1.0 / coords->dx(x, y, z) / sqrt(coords->g_11()(x, y, z)), MatA); + -1.0 / coords->dx(x, y, z) / sqrt(coords->g_11(x, y, z)), MatA); Element(i, x, z, 1, 0, - 1.0 / coords->dx(x, y, z) / sqrt(coords->g_11()(x, y, z)), MatA); + 1.0 / coords->dx(x, y, z) / sqrt(coords->g_11(x, y, z)), MatA); Element(i, x, z, 2, 0, 0.0, MatA); // Element(i,x,z, 3, 0, 0.0, MatA ); // Reset // these elements to 0 in case 4th order flag was @@ -653,18 +653,18 @@ FieldPerp LaplacePetsc::solve(const FieldPerp& b, const FieldPerp& x0) { // Fourth Order Accuracy on Boundary Element(i, x, z, 0, 0, 25.0 / (12.0 * coords->dx(x, y, z)) - / sqrt(coords->g_11()(x, y, z)), + / sqrt(coords->g_11(x, y, z)), MatA); Element(i, x, z, -1, 0, - -4.0 / coords->dx(x, y, z) / sqrt(coords->g_11()(x, y, z)), MatA); + -4.0 / coords->dx(x, y, z) / sqrt(coords->g_11(x, y, z)), MatA); Element(i, x, z, -2, 0, - 3.0 / coords->dx(x, y, z) / sqrt(coords->g_11()(x, y, z)), MatA); + 3.0 / coords->dx(x, y, z) / sqrt(coords->g_11(x, y, z)), MatA); Element(i, x, z, -3, 0, -4.0 / (3.0 * coords->dx(x, y, z)) - / sqrt(coords->g_11()(x, y, z)), + / sqrt(coords->g_11(x, y, z)), MatA); Element(i, x, z, -4, 0, - 1.0 / (4.0 * coords->dx(x, y, z)) / sqrt(coords->g_11()(x, y, z)), + 1.0 / (4.0 * coords->dx(x, y, z)) / sqrt(coords->g_11(x, y, z)), MatA); } else { // // Second Order Accuracy on Boundary @@ -677,9 +677,9 @@ FieldPerp LaplacePetsc::solve(const FieldPerp& b, const FieldPerp& x0) { // Second Order Accuracy on Boundary, set half-way between grid // points Element(i, x, z, 0, 0, - 1.0 / coords->dx(x, y, z) / sqrt(coords->g_11()(x, y, z)), MatA); + 1.0 / coords->dx(x, y, z) / sqrt(coords->g_11(x, y, z)), MatA); Element(i, x, z, -1, 0, - -1.0 / coords->dx(x, y, z) / sqrt(coords->g_11()(x, y, z)), MatA); + -1.0 / coords->dx(x, y, z) / sqrt(coords->g_11(x, y, z)), MatA); Element(i, x, z, -2, 0, 0.0, MatA); // Element(i,x,z, -3, 0, 0.0, MatA ); // Reset these elements to 0 // in case 4th order flag was used previously: not allowed now diff --git a/src/invert/laplace/impls/serial_band/serial_band.cxx b/src/invert/laplace/impls/serial_band/serial_band.cxx index 4a62254a23..aced0e55b2 100644 --- a/src/invert/laplace/impls/serial_band/serial_band.cxx +++ b/src/invert/laplace/impls/serial_band/serial_band.cxx @@ -272,8 +272,8 @@ FieldPerp LaplaceSerialBand::solve(const FieldPerp& b, const FieldPerp& x0) { for (int ix = 0; ix < xbndry; ix++) { A(ix, 0) = 0.; A(ix, 1) = 0.; - A(ix, 2) = -.5 / sqrt(coords->g_11()(ix, jy)) / coords->dx(ix, jy); - A(ix, 3) = .5 / sqrt(coords->g_11()(ix, jy)) / coords->dx(ix, jy); + A(ix, 2) = -.5 / sqrt(coords->g_11(ix, jy)) / coords->dx(ix, jy); + A(ix, 3) = .5 / sqrt(coords->g_11(ix, jy)) / coords->dx(ix, jy); A(ix, 4) = 0.; } @@ -292,17 +292,17 @@ FieldPerp LaplaceSerialBand::solve(const FieldPerp& b, const FieldPerp& x0) { for (int ix = 0; ix < xbndry; ix++) { A(ix, 0) = 0.; A(ix, 1) = 0.; - A(ix, 2) = -3. / sqrt(coords->g_22()(ix, jy)); - A(ix, 3) = 4. / sqrt(coords->g_22()(ix + 1, jy)); - A(ix, 4) = -1. / sqrt(coords->g_22()(ix + 2, jy)); + A(ix, 2) = -3. / sqrt(coords->g_22(ix, jy)); + A(ix, 3) = 4. / sqrt(coords->g_22(ix + 1, jy)); + A(ix, 4) = -1. / sqrt(coords->g_22(ix + 2, jy)); } } else if (isInnerBoundaryFlagSet(INVERT_DC_GRADPARINV)) { for (int ix = 0; ix < xbndry; ix++) { A(ix, 0) = 0.; A(ix, 1) = 0.; - A(ix, 2) = -3. * sqrt(coords->g_22()(ix, jy)); - A(ix, 3) = 4. * sqrt(coords->g_22()(ix + 1, jy)); - A(ix, 4) = -sqrt(coords->g_22()(ix + 2, jy)); + A(ix, 2) = -3. * sqrt(coords->g_22(ix, jy)); + A(ix, 3) = 4. * sqrt(coords->g_22(ix + 1, jy)); + A(ix, 4) = -sqrt(coords->g_22(ix + 2, jy)); } } else if (isInnerBoundaryFlagSet(INVERT_DC_LAP)) { for (int ix = 0; ix < xbndry; ix++) { diff --git a/src/invert/laplace/invert_laplace.cxx b/src/invert/laplace/invert_laplace.cxx index a5e6ff4dd1..16c2964a97 100644 --- a/src/invert/laplace/invert_laplace.cxx +++ b/src/invert/laplace/invert_laplace.cxx @@ -508,8 +508,8 @@ void Laplacian::tridagMatrix(dcomplex* avec, dcomplex* bvec, dcomplex* cvec, dco // Zero gradient at inner boundary for (int ix = 0; ix < inbndry; ix++) { avec[ix] = 0.; - bvec[ix] = -1. / sqrt(coords->g_11()(ix, jy)) / coords->dx(ix, jy); - cvec[ix] = 1. / sqrt(coords->g_11()(ix, jy)) / coords->dx(ix, jy); + bvec[ix] = -1. / sqrt(coords->g_11(ix, jy)) / coords->dx(ix, jy); + cvec[ix] = 1. / sqrt(coords->g_11(ix, jy)) / coords->dx(ix, jy); } } else if (isInnerBoundaryFlagSet(INVERT_DC_GRAD)) { // Zero gradient at inner boundary @@ -521,14 +521,14 @@ void Laplacian::tridagMatrix(dcomplex* avec, dcomplex* bvec, dcomplex* cvec, dco } else if (isInnerBoundaryFlagSet(INVERT_DC_GRADPAR)) { for (int ix = 0; ix < inbndry; ix++) { avec[ix] = 0.0; - bvec[ix] = 1.0 / sqrt(coords->g_22()(ix, jy)); - cvec[ix] = -1.0 / sqrt(coords->g_22()(ix + 1, jy)); + bvec[ix] = 1.0 / sqrt(coords->g_22(ix, jy)); + cvec[ix] = -1.0 / sqrt(coords->g_22(ix + 1, jy)); } } else if (isInnerBoundaryFlagSet(INVERT_DC_GRADPARINV)) { for (int ix = 0; ix < inbndry; ix++) { avec[ix] = 0.0; - bvec[ix] = sqrt(coords->g_22()(ix, jy)); - cvec[ix] = -sqrt(coords->g_22()(ix + 1, jy)); + bvec[ix] = sqrt(coords->g_22(ix, jy)); + cvec[ix] = -sqrt(coords->g_22(ix + 1, jy)); } } else if (isInnerBoundaryFlagSet(INVERT_DC_LAP)) { // Decaying boundary conditions @@ -606,9 +606,9 @@ void Laplacian::tridagMatrix(dcomplex* avec, dcomplex* bvec, dcomplex* cvec, dco for (int ix = 0; ix < inbndry; ix++) { avec[ix] = dcomplex(0., 0.); bvec[ix] = - dcomplex(-1., 0.) / sqrt(coords->g_11()(ix, jy)) / coords->dx(ix, jy); + dcomplex(-1., 0.) / sqrt(coords->g_11(ix, jy)) / coords->dx(ix, jy); cvec[ix] = - dcomplex(1., 0.) / sqrt(coords->g_11()(ix, jy)) / coords->dx(ix, jy); + dcomplex(1., 0.) / sqrt(coords->g_11(ix, jy)) / coords->dx(ix, jy); } } else if (isInnerBoundaryFlagSet(INVERT_AC_GRAD)) { // Zero gradient at inner boundary @@ -668,9 +668,9 @@ void Laplacian::tridagMatrix(dcomplex* avec, dcomplex* bvec, dcomplex* cvec, dco || isOuterBoundaryFlagSet(INVERT_RHS))) { // Zero gradient at outer boundary for (int ix = 0; ix < outbndry; ix++) { - avec[ncx - ix] = dcomplex(-1., 0.) / sqrt(coords->g_11()(xe - ix, jy)) + avec[ncx - ix] = dcomplex(-1., 0.) / sqrt(coords->g_11(xe - ix, jy)) / coords->dx(xe - ix, jy); - bvec[ncx - ix] = dcomplex(1., 0.) / sqrt(coords->g_11()(xe - ix, jy)) + bvec[ncx - ix] = dcomplex(1., 0.) / sqrt(coords->g_11(xe - ix, jy)) / coords->dx(xe - ix, jy); cvec[ncx - ix] = dcomplex(0., 0.); } @@ -683,14 +683,14 @@ void Laplacian::tridagMatrix(dcomplex* avec, dcomplex* bvec, dcomplex* cvec, dco } } else if (isOuterBoundaryFlagSet(INVERT_DC_GRADPAR)) { for (int ix = 0; ix < inbndry; ix++) { - avec[ncx - ix] = 1.0 / sqrt(coords->g_22()(xe - ix - 1, jy)); - bvec[ncx - ix] = -1.0 / sqrt(coords->g_22()(xe - ix, jy)); + avec[ncx - ix] = 1.0 / sqrt(coords->g_22(xe - ix - 1, jy)); + bvec[ncx - ix] = -1.0 / sqrt(coords->g_22(xe - ix, jy)); cvec[ncx - ix] = 0.0; } } else if (isOuterBoundaryFlagSet(INVERT_DC_GRADPARINV)) { for (int ix = 0; ix < inbndry; ix++) { - avec[ncx - ix] = sqrt(coords->g_22()(xe - ix - 1, jy)); - bvec[ncx - ix] = -sqrt(coords->g_22()(xe - ix, jy)); + avec[ncx - ix] = sqrt(coords->g_22(xe - ix - 1, jy)); + bvec[ncx - ix] = -sqrt(coords->g_22(xe - ix, jy)); cvec[ncx - ix] = 0.0; } } else if (isOuterBoundaryFlagSet(INVERT_DC_LAP)) { @@ -727,9 +727,9 @@ void Laplacian::tridagMatrix(dcomplex* avec, dcomplex* bvec, dcomplex* cvec, dco || isOuterBoundaryFlagSet(INVERT_RHS))) { // Zero gradient at outer boundary for (int ix = 0; ix < outbndry; ix++) { - avec[ncx - ix] = dcomplex(-1., 0.) / sqrt(coords->g_11()(xe - ix, jy)) + avec[ncx - ix] = dcomplex(-1., 0.) / sqrt(coords->g_11(xe - ix, jy)) / coords->dx(xe - ix, jy); - bvec[ncx - ix] = dcomplex(1., 0.) / sqrt(coords->g_11()(xe - ix, jy)) + bvec[ncx - ix] = dcomplex(1., 0.) / sqrt(coords->g_11(xe - ix, jy)) / coords->dx(xe - ix, jy); cvec[ncx - ix] = dcomplex(0., 0.); } diff --git a/src/invert/parderiv/impls/cyclic/cyclic.cxx b/src/invert/parderiv/impls/cyclic/cyclic.cxx index 47fc28fd82..00e35b2e7e 100644 --- a/src/invert/parderiv/impls/cyclic/cyclic.cxx +++ b/src/invert/parderiv/impls/cyclic/cyclic.cxx @@ -154,7 +154,7 @@ const Field3D InvertParCR::solve(const Field3D& f) { BoutReal acoef = A(x, y + local_ystart); // Constant BoutReal bcoef = - B(x, y + local_ystart) / coord->g_22()(x, y + local_ystart); // d2dy2 + B(x, y + local_ystart) / coord->g_22(x, y + local_ystart); // d2dy2 BoutReal ccoef = C(x, y + local_ystart); // d2dydz BoutReal dcoef = D(x, y + local_ystart); // d2dz2 BoutReal ecoef = E(x, y + local_ystart) diff --git a/src/mesh/boundary_standard.cxx b/src/mesh/boundary_standard.cxx index 785225b1d4..cbd335c8c8 100644 --- a/src/mesh/boundary_standard.cxx +++ b/src/mesh/boundary_standard.cxx @@ -2451,8 +2451,8 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { for (bndry->first(); !bndry->isDone(); bndry->next()) { f(bndry->x, bndry->y) = f(bndry->x - bndry->bx, bndry->y - bndry->by) - * sqrt(metric->g_22()(bndry->x, bndry->y) - / metric->g_22()(bndry->x - bndry->bx, bndry->y - bndry->by)); + * sqrt(metric->g_22(bndry->x, bndry->y) + / metric->g_22(bndry->x - bndry->bx, bndry->y - bndry->by)); } #else throw BoutException("Applying boundary condition 'neumannpar' to Field2D not " @@ -2468,8 +2468,8 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { for (int z = 0; z < mesh->LocalNz; z++) { f(bndry->x, bndry->y, z) = f(bndry->x - bndry->bx, bndry->y - bndry->by, z) - * sqrt(metric->g_22()(bndry->x, bndry->y, z) - / metric->g_22()(bndry->x - bndry->bx, bndry->y - bndry->by, z)); + * sqrt(metric->g_22(bndry->x, bndry->y, z) + / metric->g_22(bndry->x - bndry->bx, bndry->y - bndry->by, z)); } } } diff --git a/src/mesh/difops.cxx b/src/mesh/difops.cxx index 1317c01ec7..0b0af95560 100644 --- a/src/mesh/difops.cxx +++ b/src/mesh/difops.cxx @@ -104,7 +104,7 @@ Field3D Grad_parP(const Field3D& apar, const Field3D& f) { for (int x = 1; x <= mesh->LocalNx - 2; x++) { for (int y = mesh->ystart; y <= mesh->yend; y++) { for (int z = 0; z < ncz; z++) { - BoutReal by = 1. / sqrt(metric->g_22()(x, y, z)); + BoutReal by = 1. / sqrt(metric->g_22(x, y, z)); // Z indices zm and zp int const zm = (z - 1 + ncz) % ncz; int const zp = (z + 1) % ncz; @@ -260,12 +260,12 @@ Field3D Div_par(const Field3D& f, const Field3D& v) { // Calculate flux at right boundary (y+1/2) BoutReal const fluxRight = fR * vR * (coord->J()(i, j, k) + coord->J()(i, j + 1, k)) - / (sqrt(coord->g_22()(i, j, k)) + sqrt(coord->g_22()(i, j + 1, k))); + / (sqrt(coord->g_22(i, j, k)) + sqrt(coord->g_22(i, j + 1, k))); // Calculate at left boundary (y-1/2) BoutReal const fluxLeft = fL * vL * (coord->J()(i, j, k) + coord->J()(i, j - 1, k)) - / (sqrt(coord->g_22()(i, j, k)) + sqrt(coord->g_22()(i, j - 1, k))); + / (sqrt(coord->g_22(i, j, k)) + sqrt(coord->g_22(i, j - 1, k))); result(i, j, k) = (fluxRight - fluxLeft) / (coord->dy(i, j, k) * coord->J()(i, j, k)); diff --git a/tests/MMS/GBS/gbs.cxx b/tests/MMS/GBS/gbs.cxx index 65cd65cfd0..9fed76c119 100644 --- a/tests/MMS/GBS/gbs.cxx +++ b/tests/MMS/GBS/gbs.cxx @@ -301,9 +301,9 @@ int GBS::init(bool restarting) { output.write("g11 = {:e}, g22 = {:e}, g33 = {:e}\n", coords->g11(2, 2), coords->g22(2, 2), coords->g33(2, 2)); output.write("g12 = {:e}, g23 = {:e}\n", coords->g12(2, 2), coords->g23(2, 2)); - output.write("g_11 = {:e}, g_22 = {:e}, g_33 = {:e}\n", coords->g_11()(2, 2), - coords->g_22()(2, 2), coords->g_33()(2, 2)); - output.write("g_12 = {:e}, g_23 = {:e}\n", coords->g_12()(2, 2), coords->g_23()(2, 2)); + output.write("g_11 = {:e}, g_22 = {:e}, g_33 = {:e}\n", coords->g_11(2, 2), + coords->g_22(2, 2), coords->g_33(2, 2)); + output.write("g_12 = {:e}, g_23 = {:e}\n", coords->g_12(2, 2), coords->g_23(2, 2)); std::shared_ptr gen = FieldFactory::get()->parse("source", Options::getRoot()->getSection("ne")); diff --git a/tests/integrated/test-multigrid_laplace/test_multigrid_laplace.cxx b/tests/integrated/test-multigrid_laplace/test_multigrid_laplace.cxx index 50a3cbfc1a..55d93c746e 100644 --- a/tests/integrated/test-multigrid_laplace/test_multigrid_laplace.cxx +++ b/tests/integrated/test-multigrid_laplace/test_multigrid_laplace.cxx @@ -242,7 +242,7 @@ int main(int argc, char** argv) { x0(mesh->xstart - 1, mesh->ystart, k) = (f4(mesh->xstart, mesh->ystart, k) - f4(mesh->xstart - 1, mesh->ystart, k)) / mesh->getCoordinates()->dx(mesh->xstart, mesh->ystart, k) - / sqrt(mesh->getCoordinates()->g_11()(mesh->xstart, mesh->ystart, k)); + / sqrt(mesh->getCoordinates()->g_11(mesh->xstart, mesh->ystart, k)); } } if (mesh->lastX()) { @@ -250,7 +250,7 @@ int main(int argc, char** argv) { x0(mesh->xend + 1, mesh->ystart, k) = (f4(mesh->xend + 1, mesh->ystart, k) - f4(mesh->xend, mesh->ystart, k)) / mesh->getCoordinates()->dx(mesh->xend, mesh->ystart, k) - / sqrt(mesh->getCoordinates()->g_11()(mesh->xend, mesh->ystart, k)); + / sqrt(mesh->getCoordinates()->g_11(mesh->xend, mesh->ystart, k)); } } diff --git a/tests/integrated/test-naulin-laplace/test_naulin_laplace.cxx b/tests/integrated/test-naulin-laplace/test_naulin_laplace.cxx index 83b589d145..def323adf0 100644 --- a/tests/integrated/test-naulin-laplace/test_naulin_laplace.cxx +++ b/tests/integrated/test-naulin-laplace/test_naulin_laplace.cxx @@ -246,7 +246,7 @@ int main(int argc, char** argv) { x0(mesh->xstart - 1, mesh->ystart, k) = (f4(mesh->xstart, mesh->ystart, k) - f4(mesh->xstart - 1, mesh->ystart, k)) / mesh->getCoordinates()->dx(mesh->xstart, mesh->ystart, k) - / sqrt(mesh->getCoordinates()->g_11()(mesh->xstart, mesh->ystart, k)); + / sqrt(mesh->getCoordinates()->g_11(mesh->xstart, mesh->ystart, k)); } } if (mesh->lastX()) { @@ -254,7 +254,7 @@ int main(int argc, char** argv) { x0(mesh->xend + 1, mesh->ystart, k) = (f4(mesh->xend + 1, mesh->ystart, k) - f4(mesh->xend, mesh->ystart, k)) / mesh->getCoordinates()->dx(mesh->xend, mesh->ystart, k) - / sqrt(mesh->getCoordinates()->g_11()(mesh->xend, mesh->ystart, k)); + / sqrt(mesh->getCoordinates()->g_11(mesh->xend, mesh->ystart, k)); } } From dca85987a4df8c6bdbd0f1d0c41b78362841a3ec Mon Sep 17 00:00:00 2001 From: tomc271 Date: Tue, 1 Oct 2024 13:26:52 +0100 Subject: [PATCH 467/491] Improve readability of preprocessor directive --- include/bout/coordinates.hxx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index b964c62fce..078912a3ac 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -154,7 +154,7 @@ public: const BoutReal& g_13(int x, int y, int z) const { return covariantMetricTensor.g13(x, y, z); } const BoutReal& g_23(int x, int y, int z) const { return covariantMetricTensor.g23(x, y, z); } -#if BOUT_USE_METRIC_3D != 1 +#if not(BOUT_USE_METRIC_3D) const BoutReal& g_11(int x, int y) const { return covariantMetricTensor.g11(x, y); } const BoutReal& g_22(int x, int y) const { return covariantMetricTensor.g22(x, y); } const BoutReal& g_33(int x, int y) const { return covariantMetricTensor.g33(x, y); } From 078a51b301b8f5a97cf9a6050d5e7392e6d37580 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Tue, 1 Oct 2024 13:42:23 +0100 Subject: [PATCH 468/491] Add overloaded getter for IntShiftTorsion that gives the value at a particular x, y (or x, y, z) position. --- include/bout/coordinates.hxx | 12 ++++++++++-- src/invert/laplace/impls/petsc/petsc_laplace.cxx | 4 ++-- src/invert/laplace/invert_laplace.cxx | 4 ++-- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index 078912a3ac..e0d3fcb657 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -93,7 +93,15 @@ public: const BoutReal& dy(int x, int y) const { return dy_(x, y); } const BoutReal& dz(int x, int y) const { return dz_(x, y); } #endif - + + const BoutReal& IntShiftTorsion(int x, int y, int z) const { + return IntShiftTorsion_(x, y, z); } + +#if not(BOUT_USE_METRIC_3D) + const BoutReal& IntShiftTorsion(int x, int y) const { + return IntShiftTorsion_(x, y); } +#endif + void setDx(FieldMetric dx) { dx_ = std::move(dx); } void setDy(FieldMetric dy) { dy_ = std::move(dy); } void setDz(FieldMetric dz) { dz_ = std::move(dz); } @@ -120,7 +128,7 @@ public: const FieldMetric& d1_dy() const { return d1_dy_; } const FieldMetric& d1_dz() const { return d1_dz_; } - #if BOUT_USE_METRIC_3D +#if BOUT_USE_METRIC_3D const BoutReal& d1_dx(int x, int y, int z) const { return d1_dx_(x, y, z); } const BoutReal& d1_dy(int x, int y, int z) const { return d1_dy_(x, y, z); } const BoutReal& d1_dz(int x, int y, int z) const { return d1_dz_(x, y, z); } diff --git a/src/invert/laplace/impls/petsc/petsc_laplace.cxx b/src/invert/laplace/impls/petsc/petsc_laplace.cxx index 5b899dc262..738b8fe5b7 100644 --- a/src/invert/laplace/impls/petsc/petsc_laplace.cxx +++ b/src/invert/laplace/impls/petsc/petsc_laplace.cxx @@ -998,8 +998,8 @@ void LaplacePetsc::Coeffs(int x, int y, int z, BoutReal& coef1, BoutReal& coef2, if (localmesh->IncIntShear) { // d2dz2 term - coef2 += coords->g11(x, y, z) * coords->IntShiftTorsion()(x, y, z) - * coords->IntShiftTorsion()(x, y, z); + coef2 += coords->g11(x, y, z) * coords->IntShiftTorsion(x, y, z) + * coords->IntShiftTorsion(x, y, z); // Mixed derivative coef3 = 0.0; // This cancels out } diff --git a/src/invert/laplace/invert_laplace.cxx b/src/invert/laplace/invert_laplace.cxx index 16c2964a97..df74a760b9 100644 --- a/src/invert/laplace/invert_laplace.cxx +++ b/src/invert/laplace/invert_laplace.cxx @@ -367,8 +367,8 @@ void Laplacian::tridagCoefs(int jx, int jy, BoutReal kwave, dcomplex& a, dcomple if (localmesh->IncIntShear) { // d2dz2 term - coef2 += localcoords->g11(jx, jy) * localcoords->IntShiftTorsion()(jx, jy) - * localcoords->IntShiftTorsion()(jx, jy); + coef2 += localcoords->g11(jx, jy) * localcoords->IntShiftTorsion(jx, jy) + * localcoords->IntShiftTorsion(jx, jy); // Mixed derivative coef3 = 0.0; // This cancels out } From f49eb92cc62d0779b7139fbb6f5c6b783a456b0a Mon Sep 17 00:00:00 2001 From: tomc271 Date: Tue, 1 Oct 2024 14:11:39 +0100 Subject: [PATCH 469/491] Add overloaded getter for J() that gives the value at a particular x, y (or x, y, z) position --- include/bout/coordinates.hxx | 8 +++ include/bout/fv_ops.hxx | 24 ++++----- .../impls/cyclic/laplacexz-cyclic.cxx | 8 +-- .../laplacexz/impls/petsc/laplacexz-petsc.cxx | 32 +++++------ src/mesh/boundary_standard.cxx | 34 ++++++------ src/mesh/difops.cxx | 6 +-- src/mesh/fv_ops.cxx | 54 +++++++++---------- 7 files changed, 87 insertions(+), 79 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index e0d3fcb657..6c1c9ae93c 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -102,6 +102,14 @@ public: return IntShiftTorsion_(x, y); } #endif + const BoutReal& J(int x, int y, int z) const { + return J()(x, y, z); } + +#if not(BOUT_USE_METRIC_3D) + const BoutReal& J(int x, int y) const { + return J()(x, y); } +#endif + void setDx(FieldMetric dx) { dx_ = std::move(dx); } void setDy(FieldMetric dy) { dy_ = std::move(dy); } void setDz(FieldMetric dz) { dz_ = std::move(dz); } diff --git a/include/bout/fv_ops.hxx b/include/bout/fv_ops.hxx index ea3832c1be..380a536be6 100644 --- a/include/bout/fv_ops.hxx +++ b/include/bout/fv_ops.hxx @@ -244,42 +244,42 @@ const Field3D Div_par(const Field3D& f_in, const Field3D& v_in, #if not(BOUT_USE_METRIC_3D) // For right cell boundaries BoutReal common_factor = - (coord->J()(i, j) + coord->J()(i, j + 1)) + (coord->J(i, j) + coord->J(i, j + 1)) / (sqrt(coord->g_22(i, j)) + sqrt(coord->g_22(i, j + 1))); - BoutReal flux_factor_rc = common_factor / (coord->dy(i, j) * coord->J()(i, j)); + BoutReal flux_factor_rc = common_factor / (coord->dy(i, j) * coord->J(i, j)); BoutReal flux_factor_rp = - common_factor / (coord->dy(i, j + 1) * coord->J()(i, j + 1)); + common_factor / (coord->dy(i, j + 1) * coord->J(i, j + 1)); // For left cell boundaries - common_factor = (coord->J()(i, j) + coord->J()(i, j - 1)) + common_factor = (coord->J(i, j) + coord->J(i, j - 1)) / (sqrt(coord->g_22(i, j)) + sqrt(coord->g_22(i, j - 1))); - BoutReal flux_factor_lc = common_factor / (coord->dy(i, j) * coord->J()(i, j)); + BoutReal flux_factor_lc = common_factor / (coord->dy(i, j) * coord->J(i, j)); BoutReal flux_factor_lm = - common_factor / (coord->dy(i, j - 1) * coord->J()(i, j - 1)); + common_factor / (coord->dy(i, j - 1) * coord->J(i, j - 1)); #endif for (int k = 0; k < mesh->LocalNz; k++) { #if BOUT_USE_METRIC_3D // For right cell boundaries BoutReal common_factor = - (coord->J()(i, j, k) + coord->J()(i, j + 1, k)) + (coord->J(i, j, k) + coord->J(i, j + 1, k)) / (sqrt(coord->g_22(i, j, k)) + sqrt(coord->g_22(i, j + 1, k))); BoutReal flux_factor_rc = - common_factor / (coord->dy(i, j, k) * coord->J()(i, j, k)); + common_factor / (coord->dy(i, j, k) * coord->J(i, j, k)); BoutReal flux_factor_rp = - common_factor / (coord->dy(i, j + 1, k) * coord->J()(i, j + 1, k)); + common_factor / (coord->dy(i, j + 1, k) * coord->J(i, j + 1, k)); // For left cell boundaries common_factor = - (coord->J()(i, j, k) + coord->J()(i, j - 1, k)) + (coord->J(i, j, k) + coord->J(i, j - 1, k)) / (sqrt(coord->g_22(i, j, k)) + sqrt(coord->g_22(i, j - 1, k))); BoutReal flux_factor_lc = - common_factor / (coord->dy(i, j, k) * coord->J()(i, j, k)); + common_factor / (coord->dy(i, j, k) * coord->J(i, j, k)); BoutReal flux_factor_lm = - common_factor / (coord->dy(i, j - 1, k) * coord->J()(i, j - 1, k)); + common_factor / (coord->dy(i, j - 1, k) * coord->J(i, j - 1, k)); #endif //////////////////////////////////////////// diff --git a/src/invert/laplacexz/impls/cyclic/laplacexz-cyclic.cxx b/src/invert/laplacexz/impls/cyclic/laplacexz-cyclic.cxx index 99697e9f63..318498a161 100644 --- a/src/invert/laplacexz/impls/cyclic/laplacexz-cyclic.cxx +++ b/src/invert/laplacexz/impls/cyclic/laplacexz-cyclic.cxx @@ -116,23 +116,23 @@ void LaplaceXZcyclic::setCoefs(const Field2D& A2D, const Field2D& B2D) { // XX component // Metrics on x+1/2 boundary - BoutReal J = 0.5 * (coord->J()(x, y) + coord->J()(x + 1, y)); + BoutReal J = 0.5 * (coord->J(x, y) + coord->J(x + 1, y)); BoutReal g11 = 0.5 * (coord->g11(x, y) + coord->g11(x + 1, y)); BoutReal dx = 0.5 * (coord->dx(x, y) + coord->dx(x + 1, y)); BoutReal A = 0.5 * (A2D(x, y) + A2D(x + 1, y)); - BoutReal val = A * J * g11 / (coord->J()(x, y) * dx * coord->dx(x, y)); + BoutReal val = A * J * g11 / (coord->J(x, y) * dx * coord->dx(x, y)); ccoef(ind, x - xstart) += val; bcoef(ind, x - xstart) -= val; // Metrics on x-1/2 boundary - J = 0.5 * (coord->J()(x, y) + coord->J()(x - 1, y)); + J = 0.5 * (coord->J(x, y) + coord->J(x - 1, y)); g11 = 0.5 * (coord->g11(x, y) + coord->g11(x - 1, y)); dx = 0.5 * (coord->dx(x, y) + coord->dx(x - 1, y)); A = 0.5 * (A2D(x, y) + A2D(x - 1, y)); - val = A * J * g11 / (coord->J()(x, y) * dx * coord->dx(x, y)); + val = A * J * g11 / (coord->J(x, y) * dx * coord->dx(x, y)); acoef(ind, x - xstart) += val; bcoef(ind, x - xstart) -= val; diff --git a/src/invert/laplacexz/impls/petsc/laplacexz-petsc.cxx b/src/invert/laplacexz/impls/petsc/laplacexz-petsc.cxx index 6116b0cac8..5672977ac8 100644 --- a/src/invert/laplacexz/impls/petsc/laplacexz-petsc.cxx +++ b/src/invert/laplacexz/impls/petsc/laplacexz-petsc.cxx @@ -402,28 +402,28 @@ void LaplaceXZpetsc::setCoefs(const Field3D& Ain, const Field3D& Bin) { // XX component { // Metrics on x+1/2 boundary - const BoutReal J = 0.5 * (coords->J()(x, y, z) + coords->J()(x + 1, y, z)); + const BoutReal J = 0.5 * (coords->J(x, y, z) + coords->J(x + 1, y, z)); const BoutReal g11 = 0.5 * (coords->g11(x, y, z) + coords->g11(x + 1, y, z)); const BoutReal dx = 0.5 * (coords->dx(x, y, z) + coords->dx(x + 1, y, z)); const BoutReal Acoef = 0.5 * (A(x, y, z) + A(x + 1, y, z)); const BoutReal val = - Acoef * J * g11 / (coords->J()(x, y, z) * dx * coords->dx(x, y, z)); + Acoef * J * g11 / (coords->J(x, y, z) * dx * coords->dx(x, y, z)); xp = val; c = -val; } { // Metrics on x-1/2 boundary - const BoutReal J = 0.5 * (coords->J()(x, y, z) + coords->J()(x - 1, y, z)); + const BoutReal J = 0.5 * (coords->J(x, y, z) + coords->J(x - 1, y, z)); const BoutReal g11 = 0.5 * (coords->g11(x, y, z) + coords->g11(x - 1, y, z)); const BoutReal dx = 0.5 * (coords->dx(x, y, z) + coords->dx(x - 1, y, z)); const BoutReal Acoef = 0.5 * (A(x, y, z) + A(x - 1, y, z)); const BoutReal val = - Acoef * J * g11 / (coords->J()(x, y, z) * dx * coords->dx(x, y, z)); + Acoef * J * g11 / (coords->J(x, y, z) * dx * coords->dx(x, y, z)); xm = val; c -= val; } @@ -434,26 +434,26 @@ void LaplaceXZpetsc::setCoefs(const Field3D& Ain, const Field3D& Bin) { const int zplus = (z + 1) % (localmesh->LocalNz); { - const BoutReal J = 0.5 * (coords->J()(x, y, z) + coords->J()(x, y, zplus)); + const BoutReal J = 0.5 * (coords->J(x, y, z) + coords->J(x, y, zplus)); const BoutReal g33 = 0.5 * (coords->g33(x, y, z) + coords->g33(x, y, zplus)); const BoutReal dz = 0.5 * (coords->dz(x, y, z) + coords->dz(x, y, zplus)); // Metrics on z+1/2 boundary const BoutReal Acoef = 0.5 * (A(x, y, z) + A(x, y, zplus)); - const BoutReal val = Acoef * J * g33 / (coords->J()(x, y, z) * dz * dz); + const BoutReal val = Acoef * J * g33 / (coords->J(x, y, z) * dz * dz); zp = val; c -= val; } { // Metrics on z-1/2 boundary - const BoutReal J = 0.5 * (coords->J()(x, y, z) + coords->J()(x, y, zminus)); + const BoutReal J = 0.5 * (coords->J(x, y, z) + coords->J(x, y, zminus)); const BoutReal g33 = 0.5 * (coords->g33(x, y, z) + coords->g33(x, y, zminus)); const BoutReal Acoef = 0.5 * (A(x, y, z) + A(x, y, zminus)); const BoutReal dz = 0.5 * (coords->dz(x, y, z) + coords->dz(x, y, zminus)); - const BoutReal val = Acoef * J * g33 / (coords->J()(x, y, z) * dz * dz); + const BoutReal val = Acoef * J * g33 / (coords->J(x, y, z) * dz * dz); zm = val; c -= val; } @@ -474,14 +474,14 @@ void LaplaceXZpetsc::setCoefs(const Field3D& Ain, const Field3D& Bin) { // so metrics at (x+1/2,z) // Metrics - const BoutReal J = 0.5 * (coords->J()(x, y, z) + coords->J()(x + 1, y, z)); + const BoutReal J = 0.5 * (coords->J(x, y, z) + coords->J(x + 1, y, z)); const BoutReal g13 = 0.5 * (coords->g13(x, y, z) + coords->g13(x + 1, y, z)); const BoutReal dz = 0.5 * (coords->dz(x, y, z) + coords->dz(x + 1, y, z)); const BoutReal Acoef = 0.5 * (A(x, y, z) + A(x + 1, y, z)); const BoutReal val = - Acoef * J * g13 / (coords->J()(x, y, z) * dz * coords->dx(x, y, z)); + Acoef * J * g13 / (coords->J(x, y, z) * dz * coords->dx(x, y, z)); // This val coefficient is multiplied by the (x+1/2,z+1/2) corner // and (x+1/2,z-1/2) corner @@ -511,14 +511,14 @@ void LaplaceXZpetsc::setCoefs(const Field3D& Ain, const Field3D& Bin) { // Taking derivative in z (between corners marked x) // so metrics at (x-1/2,z) - const BoutReal J = 0.5 * (coords->J()(x, y, z) + coords->J()(x - 1, y, z)); + const BoutReal J = 0.5 * (coords->J(x, y, z) + coords->J(x - 1, y, z)); const BoutReal g13 = 0.5 * (coords->g13(x, y, z) + coords->g13(x - 1, y, z)); const BoutReal dz = 0.5 * (coords->dz(x, y, z) + coords->dz(x - 1, y, z)); const BoutReal Acoef = 0.5 * (A(x, y, z) + A(x - 1, y, z)); const BoutReal val = - -Acoef * J * g13 / (coords->J()(x, y, z) * dz * coords->dx(x, y, z)); + -Acoef * J * g13 / (coords->J(x, y, z) * dz * coords->dx(x, y, z)); // (x+1/2,z+1/2) xpzp += 0.25 * val; @@ -545,14 +545,14 @@ void LaplaceXZpetsc::setCoefs(const Field3D& Ain, const Field3D& Bin) { // Taking derivative in x (between corners marked x) // so metrics at (x,z+1/2) - const BoutReal J = 0.5 * (coords->J()(x, y, z) + coords->J()(x, y, zplus)); + const BoutReal J = 0.5 * (coords->J(x, y, z) + coords->J(x, y, zplus)); const BoutReal g13 = 0.5 * (coords->g13(x, y, z) + coords->g13(x, y, zplus)); const BoutReal dx = 0.5 * (coords->dx(x, y, z) + coords->dx(x, y, zplus)); const BoutReal Acoef = 0.5 * (A(x, y, z) + A(x, y, zplus)); const BoutReal val = - Acoef * J * g13 / (coords->J()(x, y, z) * dx * coords->dz(x, y, z)); + Acoef * J * g13 / (coords->J(x, y, z) * dx * coords->dz(x, y, z)); // (x+1/2,z+1/2) //zp += 0.25 * val; Note cancels @@ -579,14 +579,14 @@ void LaplaceXZpetsc::setCoefs(const Field3D& Ain, const Field3D& Bin) { // Taking derivative in x (between corners marked x) // so metrics at (x,z-1/2) - const BoutReal J = 0.5 * (coords->J()(x, y, z) + coords->J()(x, y, zminus)); + const BoutReal J = 0.5 * (coords->J(x, y, z) + coords->J(x, y, zminus)); const BoutReal g13 = 0.5 * (coords->g13(x, y, z) + coords->g13(x, y, zminus)); const BoutReal dx = 0.5 * (coords->dx(x, y, z) + coords->dx(x, y, zminus)); const BoutReal Acoef = 0.5 * (A(x, y, z) + A(x, y, zminus)); const BoutReal val = - -Acoef * J * g13 / (coords->J()(x, y, z) * dx * coords->dz(x, y, z)); + -Acoef * J * g13 / (coords->J(x, y, z) * dx * coords->dz(x, y, z)); // (x+1/2,z-1/2) xpzm += 0.25 * val; diff --git a/src/mesh/boundary_standard.cxx b/src/mesh/boundary_standard.cxx index cbd335c8c8..e98f0ea376 100644 --- a/src/mesh/boundary_standard.cxx +++ b/src/mesh/boundary_standard.cxx @@ -2972,46 +2972,46 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { // d/dx( Jmetric->g11 B_x ) = - d/dx( Jmetric->g12 B_y + Jmetric->g13 B_z) // - d/dy( JB^y ) - d/dz( JB^z ) - tmp = -(metric->J()(jx, jy) * metric->g12(jx, jy) * var.y(jx, jy, jz) - + metric->J()(jx, jy) * metric->g13(jx, jy) * var.z(jx, jy, jz) - - metric->J()(jx - 2, jy) * metric->g12(jx - 2, jy) + tmp = -(metric->J(jx, jy) * metric->g12(jx, jy) * var.y(jx, jy, jz) + + metric->J(jx, jy) * metric->g13(jx, jy) * var.z(jx, jy, jz) + - metric->J(jx - 2, jy) * metric->g12(jx - 2, jy) * var.y(jx - 2, jy, jz) - + metric->J()(jx - 2, jy) * metric->g13(jx - 2, jy) + + metric->J(jx - 2, jy) * metric->g13(jx - 2, jy) * var.z(jx - 2, jy, jz)) / (metric->dx(jx - 2, jy) + metric->dx(jx - 1, jy)); // First term (d/dx) using vals calculated above tmp -= - (metric->J()(jx - 1, jy + 1) * metric->g12(jx - 1, jy + 1) + (metric->J(jx - 1, jy + 1) * metric->g12(jx - 1, jy + 1) * var.x(jx - 1, jy + 1, jz) - - metric->J()(jx - 1, jy - 1) * metric->g12(jx - 1, jy - 1) + - metric->J(jx - 1, jy - 1) * metric->g12(jx - 1, jy - 1) * var.x(jx - 1, jy - 1, jz) - + metric->J()(jx - 1, jy + 1) * metric->g22(jx - 1, jy + 1) + + metric->J(jx - 1, jy + 1) * metric->g22(jx - 1, jy + 1) * var.y(jx - 1, jy + 1, jz) - - metric->J()(jx - 1, jy - 1) * metric->g22(jx - 1, jy - 1) + - metric->J(jx - 1, jy - 1) * metric->g22(jx - 1, jy - 1) * var.y(jx - 1, jy - 1, jz) - + metric->J()(jx - 1, jy + 1) * metric->g23(jx - 1, jy + 1) + + metric->J(jx - 1, jy + 1) * metric->g23(jx - 1, jy + 1) * var.z(jx - 1, jy + 1, jz) - - metric->J()(jx - 1, jy - 1) * metric->g23(jx - 1, jy - 1) + - metric->J(jx - 1, jy - 1) * metric->g23(jx - 1, jy - 1) * var.z(jx - 1, jy - 1, jz)) / (metric->dy(jx - 1, jy - 1) + metric->dy(jx - 1, jy)); // second (d/dy) - tmp -= (metric->J()(jx - 1, jy) * metric->g13(jx - 1, jy) + tmp -= (metric->J(jx - 1, jy) * metric->g13(jx - 1, jy) * (var.x(jx - 1, jy, jzp) - var.x(jx - 1, jy, jzm)) - + metric->J()(jx - 1, jy) * metric->g23(jx - 1, jy) + + metric->J(jx - 1, jy) * metric->g23(jx - 1, jy) * (var.y(jx - 1, jy, jzp) - var.y(jx - 1, jy, jzm)) - + metric->J()(jx - 1, jy) * metric->g33(jx - 1, jy) + + metric->J(jx - 1, jy) * metric->g33(jx - 1, jy) * (var.z(jx - 1, jy, jzp) - var.z(jx - 1, jy, jzm))) / (2. * metric->dz(jx - 1, jy)); var.x(jx, jy, jz) = - (metric->J()(jx - 2, jy) * metric->g11(jx - 2, jy) * var.x(jx - 2, jy, jz) + (metric->J(jx - 2, jy) * metric->g11(jx - 2, jy) * var.x(jx - 2, jy, jz) + (metric->dx(jx - 2, jy) + metric->dx(jx - 1, jy)) * tmp) - / metric->J()(jx, jy) * metric->g11(jx, jy); + / metric->J(jx, jy) * metric->g11(jx, jy); if (mesh->xstart == 2) { var.x(jx + 1, jy, jz) = - (metric->J()(jx - 3, jy) * metric->g11(jx - 3, jy) * var.x(jx - 3, jy, jz) + (metric->J(jx - 3, jy) * metric->g11(jx - 3, jy) * var.x(jx - 3, jy, jz) + 4. * metric->dx(jx, jy) * tmp) - / metric->J()(jx + 1, jy) * metric->g11(jx + 1, jy); + / metric->J(jx + 1, jy) * metric->g11(jx + 1, jy); } } } diff --git a/src/mesh/difops.cxx b/src/mesh/difops.cxx index 0b0af95560..55a38a3077 100644 --- a/src/mesh/difops.cxx +++ b/src/mesh/difops.cxx @@ -259,16 +259,16 @@ Field3D Div_par(const Field3D& f, const Field3D& v) { // Calculate flux at right boundary (y+1/2) BoutReal const fluxRight = - fR * vR * (coord->J()(i, j, k) + coord->J()(i, j + 1, k)) + fR * vR * (coord->J(i, j, k) + coord->J(i, j + 1, k)) / (sqrt(coord->g_22(i, j, k)) + sqrt(coord->g_22(i, j + 1, k))); // Calculate at left boundary (y-1/2) BoutReal const fluxLeft = - fL * vL * (coord->J()(i, j, k) + coord->J()(i, j - 1, k)) + fL * vL * (coord->J(i, j, k) + coord->J(i, j - 1, k)) / (sqrt(coord->g_22(i, j, k)) + sqrt(coord->g_22(i, j - 1, k))); result(i, j, k) = - (fluxRight - fluxLeft) / (coord->dy(i, j, k) * coord->J()(i, j, k)); + (fluxRight - fluxLeft) / (coord->dy(i, j, k) * coord->J(i, j, k)); } } } diff --git a/src/mesh/fv_ops.cxx b/src/mesh/fv_ops.cxx index fc58239959..38d3858677 100644 --- a/src/mesh/fv_ops.cxx +++ b/src/mesh/fv_ops.cxx @@ -52,14 +52,14 @@ Field3D Div_a_Grad_perp(const Field3D& a, const Field3D& f) { // Calculate flux from i to i+1 BoutReal const fout = 0.5 * (a(i, j, k) + a(i + 1, j, k)) - * (coord->J()(i, j, k) * coord->g11(i, j, k) - + coord->J()(i + 1, j, k) * coord->g11(i + 1, j, k)) + * (coord->J(i, j, k) * coord->g11(i, j, k) + + coord->J(i + 1, j, k) * coord->g11(i + 1, j, k)) * (f(i + 1, j, k) - f(i, j, k)) / (coord->dx(i, j, k) + coord->dx(i + 1, j, k)); - result(i, j, k) += fout / (coord->dx(i, j, k) * coord->J()(i, j, k)); + result(i, j, k) += fout / (coord->dx(i, j, k) * coord->J(i, j, k)); result(i + 1, j, k) -= - fout / (coord->dx(i + 1, j, k) * coord->J()(i + 1, j, k)); + fout / (coord->dx(i + 1, j, k) * coord->J(i + 1, j, k)); } } } @@ -294,11 +294,11 @@ const Field3D D4DY4(const Field3D& d_in, const Field3D& f_in) { / dy3; BoutReal const flux = 0.5 * (d(i, j, k) + d(i, j + 1, k)) - * (coord->J()(i, j, k) + coord->J()(i, j + 1, k)) * d3fdy3; + * (coord->J(i, j, k) + coord->J(i, j + 1, k)) * d3fdy3; - result(i, j, k) += flux / (coord->J()(i, j, k) * coord->dz(i, j, k)); + result(i, j, k) += flux / (coord->J(i, j, k) * coord->dz(i, j, k)); result(i, j + 1, k) -= - flux / (coord->J()(i, j + 1, k) * coord->dz(i, j + 1, k)); + flux / (coord->J(i, j + 1, k) * coord->dz(i, j + 1, k)); } } } @@ -337,12 +337,12 @@ const Field3D D4DY4_Index(const Field3D& f_in, bool bndry_flux) { // Right boundary common factors const BoutReal common_factor = 0.25 * (coord->dz(i, j, k) + coord->dz(i, j + 1, k)) - * (coord->J()(i, j, j) + coord->J()(i, j + 1, k)); + * (coord->J(i, j, j) + coord->J(i, j + 1, k)); const BoutReal factor_rc = - common_factor / (coord->J()(i, j, k) * coord->dz(i, j, k)); + common_factor / (coord->J(i, j, k) * coord->dz(i, j, k)); const BoutReal factor_rp = - common_factor / (coord->J()(i, j + 1, k) * coord->dz(i, j + 1, k)); + common_factor / (coord->J(i, j + 1, k) * coord->dz(i, j + 1, k)); // Not on domain boundary // 3rd derivative at right cell boundary @@ -361,12 +361,12 @@ const Field3D D4DY4_Index(const Field3D& f_in, bool bndry_flux) { // Right boundary common factors const BoutReal common_factor = 0.25 * (coord->dz(i, j, k) + coord->dz(i, j + 1, k)) - * (coord->J()(i, j, j) + coord->J()(i, j + 1, k)); + * (coord->J(i, j, j) + coord->J(i, j + 1, k)); const BoutReal factor_rc = - common_factor / (coord->J()(i, j, k) * coord->dz(i, j, k)); + common_factor / (coord->J(i, j, k) * coord->dz(i, j, k)); const BoutReal factor_rp = - common_factor / (coord->J()(i, j + 1, k) * coord->dz(i, j + 1, k)); + common_factor / (coord->J(i, j + 1, k) * coord->dz(i, j + 1, k)); const BoutReal d3fdx3 = -((16. / 5) * 0.5 * (f(i, j + 1, k) + f(i, j, k)) // Boundary value f_b @@ -390,12 +390,12 @@ const Field3D D4DY4_Index(const Field3D& f_in, bool bndry_flux) { for (int k = 0; k < mesh->LocalNz; k++) { const BoutReal common_factor = 0.25 * (coord->dz(i, j, k) + coord->dz(i, j + 1, k)) - * (coord->J()(i, j, k) + coord->J()(i, j - 1, k)); + * (coord->J(i, j, k) + coord->J(i, j - 1, k)); const BoutReal factor_lc = - common_factor / (coord->J()(i, j, k) * coord->dz(i, j, k)); + common_factor / (coord->J(i, j, k) * coord->dz(i, j, k)); const BoutReal factor_lm = - common_factor / (coord->J()(i, j - 1, k) * coord->dz(i, j - 1, k)); + common_factor / (coord->J(i, j - 1, k) * coord->dz(i, j - 1, k)); // Not on a domain boundary const BoutReal d3fdx3 = @@ -409,12 +409,12 @@ const Field3D D4DY4_Index(const Field3D& f_in, bool bndry_flux) { for (int k = 0; k < mesh->LocalNz; k++) { const BoutReal common_factor = 0.25 * (coord->dz(i, j, k) + coord->dz(i, j + 1, k)) - * (coord->J()(i, j, k) + coord->J()(i, j - 1, k)); + * (coord->J(i, j, k) + coord->J(i, j - 1, k)); const BoutReal factor_lc = - common_factor / (coord->J()(i, j, k) * coord->dz(i, j, k)); + common_factor / (coord->J(i, j, k) * coord->dz(i, j, k)); const BoutReal factor_lm = - common_factor / (coord->J()(i, j - 1, k) * coord->dz(i, j - 1, k)); + common_factor / (coord->J(i, j - 1, k) * coord->dz(i, j - 1, k)); const BoutReal d3fdx3 = -(-(16. / 5) * 0.5 * (f(i, j - 1, k) + f(i, j, k)) // Boundary value f_b + 6. * f(i, j, k) // f_0 @@ -542,24 +542,24 @@ Field3D Div_Perp_Lap(const Field3D& a, const Field3D& f, CELL_LOC outloc) { + coords->g33(i, j, k) * (f(i, j, kp) - f(i, j, k)) / coords->dz(i, j, k); // Flow right - BoutReal flux = gR * 0.25 * (coords->J()(i + 1, j, k) + coords->J()(i, j, k)) + BoutReal flux = gR * 0.25 * (coords->J(i + 1, j, k) + coords->J(i, j, k)) * (a(i + 1, j, k) + a(i, j, k)); - result(i, j, k) += flux / (coords->dx(i, j, k) * coords->J()(i, j, k)); + result(i, j, k) += flux / (coords->dx(i, j, k) * coords->J(i, j, k)); // Flow left - flux = gL * 0.25 * (coords->J()(i - 1, j, k) + coords->J()(i, j, k)) + flux = gL * 0.25 * (coords->J(i - 1, j, k) + coords->J(i, j, k)) * (a(i - 1, j, k) + a(i, j, k)); - result(i, j, k) -= flux / (coords->dx(i, j, k) * coords->J()(i, j, k)); + result(i, j, k) -= flux / (coords->dx(i, j, k) * coords->J(i, j, k)); // Flow up - flux = gU * 0.25 * (coords->J()(i, j, k) + coords->J()(i, j, kp)) + flux = gU * 0.25 * (coords->J(i, j, k) + coords->J(i, j, kp)) * (a(i, j, k) + a(i, j, kp)); - result(i, j, k) += flux / (coords->dz(i, j, k) * coords->J()(i, j, k)); + result(i, j, k) += flux / (coords->dz(i, j, k) * coords->J(i, j, k)); // Flow down - flux = gD * 0.25 * (coords->J()(i, j, km) + coords->J()(i, j, k)) + flux = gD * 0.25 * (coords->J(i, j, km) + coords->J(i, j, k)) * (a(i, j, km) + a(i, j, k)); - result(i, j, k) += flux / (coords->dz(i, j, k) * coords->J()(i, j, k)); + result(i, j, k) += flux / (coords->dz(i, j, k) * coords->J(i, j, k)); } } } From 8ad42cc05cf15778912b0e9193dbe00c2cf2ef08 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Tue, 1 Oct 2024 14:26:15 +0100 Subject: [PATCH 470/491] Add overloaded getter for zlength() that gives the value at a particular x, y (or x, y, z) position --- include/bout/coordinates.hxx | 3 +++ src/field/field3d.cxx | 2 +- src/invert/laplace/invert_laplace.cxx | 2 +- src/mesh/boundary_standard.cxx | 2 +- tests/unit/mesh/parallel/test_shiftedmetric.cxx | 2 +- 5 files changed, 7 insertions(+), 4 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index 6c1c9ae93c..be632f8c65 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -127,6 +127,9 @@ public: /// Length of the Z domain. Used for FFTs const Field2D& zlength() const; + const BoutReal& zlength(int x, int y) const { + return zlength()(x, y); } + /// True if corrections for non-uniform mesh spacing should be included in operators bool non_uniform() const { return non_uniform_; } void setNon_uniform(bool non_uniform) { non_uniform_ = non_uniform; } diff --git a/src/field/field3d.cxx b/src/field/field3d.cxx index 4ed9641f44..d2de7343aa 100644 --- a/src/field/field3d.cxx +++ b/src/field/field3d.cxx @@ -714,7 +714,7 @@ void shiftZ(Field3D& var, int jx, int jy, double zangle) { rfft(&(var(jx, jy, 0)), ncz, v.begin()); // Forward FFT - BoutReal zlength = var.getCoordinates()->zlength()(jx, jy); + BoutReal zlength = var.getCoordinates()->zlength(jx, jy); // Apply phase shift for (int jz = 1; jz <= ncz / 2; jz++) { diff --git a/src/invert/laplace/invert_laplace.cxx b/src/invert/laplace/invert_laplace.cxx index df74a760b9..f0a145b8d3 100644 --- a/src/invert/laplace/invert_laplace.cxx +++ b/src/invert/laplace/invert_laplace.cxx @@ -269,7 +269,7 @@ void Laplacian::tridagCoefs(int jx, int jy, int jz, dcomplex& a, dcomplex& b, dc ASSERT1(ccoef == nullptr || ccoef->getLocation() == loc); ASSERT1(d == nullptr || d->getLocation() == loc); - BoutReal kwave = jz * 2.0 * PI / coords->zlength()(jx, jy); // wave number is 1/[rad] + BoutReal kwave = jz * 2.0 * PI / coords->zlength(jx, jy); // wave number is 1/[rad] tridagCoefs(jx, jy, kwave, a, b, c, ccoef, d, loc); } diff --git a/src/mesh/boundary_standard.cxx b/src/mesh/boundary_standard.cxx index e98f0ea376..3702cd6ee7 100644 --- a/src/mesh/boundary_standard.cxx +++ b/src/mesh/boundary_standard.cxx @@ -2669,7 +2669,7 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { -1.0 * sqrt(metric->g33(x, y) / metric->g11(x, y)) * metric->dx(x, y); for (int jz = 1; jz <= ncz / 2; jz++) { BoutReal kwave = - jz * 2.0 * PI / metric->zlength()(x, y); // wavenumber in [rad^-1] + jz * 2.0 * PI / metric->zlength(x, y); // wavenumber in [rad^-1] c0[jz] *= exp(coef * kwave); // The decaying solution only } // Reverse FFT diff --git a/tests/unit/mesh/parallel/test_shiftedmetric.cxx b/tests/unit/mesh/parallel/test_shiftedmetric.cxx index 02086c3e4d..bbf6664637 100644 --- a/tests/unit/mesh/parallel/test_shiftedmetric.cxx +++ b/tests/unit/mesh/parallel/test_shiftedmetric.cxx @@ -45,7 +45,7 @@ class ShiftedMetricTest : public ::testing::Test { auto coords = mesh->getCoordinates(); coords->setParallelTransform(bout::utils::make_unique( - *mesh, CELL_CENTRE, zShift, coords->zlength()(0, 0))); + *mesh, CELL_CENTRE, zShift, coords->zlength(0, 0))); Field3D input_temp{mesh}; From 10bf54a63c7060d112cd9c4ceeb98b38b94cda1a Mon Sep 17 00:00:00 2001 From: tomc271 Date: Tue, 1 Oct 2024 15:11:58 +0100 Subject: [PATCH 471/491] Add overloaded getters for G1, G2, G3, that give the value at a particular x, y (or x, y, z) position --- include/bout/coordinates.hxx | 16 ++++++++++++++++ .../impls/multigrid/multigrid_laplace.cxx | 4 ++-- src/invert/laplace/impls/petsc/petsc_laplace.cxx | 4 ++-- .../laplace/impls/serial_band/serial_band.cxx | 4 ++-- src/invert/laplace/invert_laplace.cxx | 4 ++-- 5 files changed, 24 insertions(+), 8 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index be632f8c65..774530c7f1 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -373,6 +373,22 @@ public: void setG2(const FieldMetric& G2) const { g_values().setG2(G2); } void setG3(const FieldMetric& G3) const { g_values().setG3(G3); } + const BoutReal& G1(int x, int y, int z) const { + return G1()(x, y, z); } + const BoutReal& G2(int x, int y, int z) const { + return G2()(x, y, z); } + const BoutReal& G3(int x, int y, int z) const { + return G3()(x, y, z); } + +#if not(BOUT_USE_METRIC_3D) + const BoutReal& G1(int x, int y) const { + return G1()(x, y); } + const BoutReal& G2(int x, int y) const { + return G2()(x, y); } + const BoutReal& G3(int x, int y) const { + return G3()(x, y); } +#endif + const FieldMetric& Grad2_par2_DDY_invSg(CELL_LOC outloc, const std::string& method) const; diff --git a/src/invert/laplace/impls/multigrid/multigrid_laplace.cxx b/src/invert/laplace/impls/multigrid/multigrid_laplace.cxx index c5ddfc1631..b80e5ae71e 100644 --- a/src/invert/laplace/impls/multigrid/multigrid_laplace.cxx +++ b/src/invert/laplace/impls/multigrid/multigrid_laplace.cxx @@ -615,7 +615,7 @@ void LaplaceMultigrid::generateMatrixF(int level) { // if easier; then check this is true in constructor) BoutReal dxd = - (D(i2, yindex, k2) * coords->G1()(i2, yindex) + (D(i2, yindex, k2) * coords->G1(i2, yindex) + coords->g11(i2, yindex) * ddx_C + coords->g13(i2, yindex) * ddz_C // (could assume zero, at least initially, if easier; then check this is true in constructor) @@ -628,7 +628,7 @@ void LaplaceMultigrid::generateMatrixF(int level) { } BoutReal dzd = - (D(i2, yindex, k2) * coords->G3()(i2, yindex) + (D(i2, yindex, k2) * coords->G3(i2, yindex) + coords->g33(i2, yindex) * ddz_C + coords->g13(i2, yindex) * ddx_C // (could assume zero, at least initially, if easier; then check diff --git a/src/invert/laplace/impls/petsc/petsc_laplace.cxx b/src/invert/laplace/impls/petsc/petsc_laplace.cxx index 738b8fe5b7..ea633ba276 100644 --- a/src/invert/laplace/impls/petsc/petsc_laplace.cxx +++ b/src/invert/laplace/impls/petsc/petsc_laplace.cxx @@ -979,8 +979,8 @@ void LaplacePetsc::Coeffs(int x, int y, int z, BoutReal& coef1, BoutReal& coef2, coef5 = 0.0; // If global flag all_terms are set (true by default) if (all_terms) { - coef4 = coords->G1()(x, y, z); // X 1st derivative - coef5 = coords->G3()(x, y, z); // Z 1st derivative + coef4 = coords->G1(x, y, z); // X 1st derivative + coef5 = coords->G3(x, y, z); // Z 1st derivative ASSERT3(finite(coef4)); ASSERT3(finite(coef5)); diff --git a/src/invert/laplace/impls/serial_band/serial_band.cxx b/src/invert/laplace/impls/serial_band/serial_band.cxx index aced0e55b2..edd069c2e9 100644 --- a/src/invert/laplace/impls/serial_band/serial_band.cxx +++ b/src/invert/laplace/impls/serial_band/serial_band.cxx @@ -171,8 +171,8 @@ FieldPerp LaplaceSerialBand::solve(const FieldPerp& b, const FieldPerp& x0) { coef3 *= Dcoef(ix, jy); if (all_terms) { - coef4 = coords->G1()(ix, jy); - coef5 = coords->G3()(ix, jy); + coef4 = coords->G1(ix, jy); + coef5 = coords->G3(ix, jy); } if (nonuniform) { diff --git a/src/invert/laplace/invert_laplace.cxx b/src/invert/laplace/invert_laplace.cxx index f0a145b8d3..feb2a182a6 100644 --- a/src/invert/laplace/invert_laplace.cxx +++ b/src/invert/laplace/invert_laplace.cxx @@ -332,8 +332,8 @@ void Laplacian::tridagCoefs(int jx, int jy, BoutReal kwave, dcomplex& a, dcomple coef5 = 0.0; // If global flag all_terms are set (true by default) if (all_terms) { - coef4 = localcoords->G1()(jx, jy); // X 1st derivative - coef5 = localcoords->G3()(jx, jy); // Z 1st derivative + coef4 = localcoords->G1(jx, jy); // X 1st derivative + coef5 = localcoords->G3(jx, jy); // Z 1st derivative } if (d != nullptr) { From 3d8198fe65f0c7189b78662abfec177fcef95db8 Mon Sep 17 00:00:00 2001 From: tomc271 Date: Wed, 2 Oct 2024 12:24:23 +0000 Subject: [PATCH 472/491] Apply clang-format changes --- include/bout/coordinates.hxx | 81 +++++++++++-------- include/bout/fv_ops.hxx | 10 +-- .../impls/multigrid/multigrid_laplace.cxx | 16 ++-- .../laplace/impls/petsc/petsc_laplace.cxx | 12 +-- .../laplace/impls/petsc3damg/petsc3damg.cxx | 12 +-- .../laplace/impls/serial_band/serial_band.cxx | 16 ++-- src/invert/laplace/invert_laplace.cxx | 7 +- .../laplacexz/impls/petsc/laplacexz-petsc.cxx | 24 ++---- src/invert/parderiv/impls/cyclic/cyclic.cxx | 4 +- src/mesh/boundary_standard.cxx | 52 ++++++------ src/mesh/difops.cxx | 32 ++++---- src/mesh/fv_ops.cxx | 30 ++++--- tests/unit/mesh/test_coordinates.cxx | 1 - 13 files changed, 145 insertions(+), 152 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index 774530c7f1..a190f43c53 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -95,19 +95,17 @@ public: #endif const BoutReal& IntShiftTorsion(int x, int y, int z) const { - return IntShiftTorsion_(x, y, z); } + return IntShiftTorsion_(x, y, z); + } #if not(BOUT_USE_METRIC_3D) - const BoutReal& IntShiftTorsion(int x, int y) const { - return IntShiftTorsion_(x, y); } + const BoutReal& IntShiftTorsion(int x, int y) const { return IntShiftTorsion_(x, y); } #endif - const BoutReal& J(int x, int y, int z) const { - return J()(x, y, z); } + const BoutReal& J(int x, int y, int z) const { return J()(x, y, z); } #if not(BOUT_USE_METRIC_3D) - const BoutReal& J(int x, int y) const { - return J()(x, y); } + const BoutReal& J(int x, int y) const { return J()(x, y); } #endif void setDx(FieldMetric dx) { dx_ = std::move(dx); } @@ -127,8 +125,7 @@ public: /// Length of the Z domain. Used for FFTs const Field2D& zlength() const; - const BoutReal& zlength(int x, int y) const { - return zlength()(x, y); } + const BoutReal& zlength(int x, int y) const { return zlength()(x, y); } /// True if corrections for non-uniform mesh spacing should be included in operators bool non_uniform() const { return non_uniform_; } @@ -166,12 +163,24 @@ public: const MetricTensor::FieldMetric& g23() const { return contravariantMetricTensor.g23(); } /// Covariant metric tensor - const BoutReal& g_11(int x, int y, int z) const { return covariantMetricTensor.g11(x, y, z); } - const BoutReal& g_22(int x, int y, int z) const { return covariantMetricTensor.g22(x, y, z); } - const BoutReal& g_33(int x, int y, int z) const { return covariantMetricTensor.g33(x, y, z); } - const BoutReal& g_12(int x, int y, int z) const { return covariantMetricTensor.g12(x, y, z); } - const BoutReal& g_13(int x, int y, int z) const { return covariantMetricTensor.g13(x, y, z); } - const BoutReal& g_23(int x, int y, int z) const { return covariantMetricTensor.g23(x, y, z); } + const BoutReal& g_11(int x, int y, int z) const { + return covariantMetricTensor.g11(x, y, z); + } + const BoutReal& g_22(int x, int y, int z) const { + return covariantMetricTensor.g22(x, y, z); + } + const BoutReal& g_33(int x, int y, int z) const { + return covariantMetricTensor.g33(x, y, z); + } + const BoutReal& g_12(int x, int y, int z) const { + return covariantMetricTensor.g12(x, y, z); + } + const BoutReal& g_13(int x, int y, int z) const { + return covariantMetricTensor.g13(x, y, z); + } + const BoutReal& g_23(int x, int y, int z) const { + return covariantMetricTensor.g23(x, y, z); + } #if not(BOUT_USE_METRIC_3D) const BoutReal& g_11(int x, int y) const { return covariantMetricTensor.g11(x, y); } @@ -183,12 +192,24 @@ public: #endif /// Contravariant metric tensor (g^{ij}) - const BoutReal& g11(int x, int y, int z) const { return contravariantMetricTensor.g11(x, y, z); } - const BoutReal& g22(int x, int y, int z) const { return contravariantMetricTensor.g22(x, y, z); } - const BoutReal& g33(int x, int y, int z) const { return contravariantMetricTensor.g33(x, y, z); } - const BoutReal& g12(int x, int y, int z) const { return contravariantMetricTensor.g12(x, y, z); } - const BoutReal& g13(int x, int y, int z) const { return contravariantMetricTensor.g13(x, y, z); } - const BoutReal& g23(int x, int y, int z) const { return contravariantMetricTensor.g23(x, y, z); } + const BoutReal& g11(int x, int y, int z) const { + return contravariantMetricTensor.g11(x, y, z); + } + const BoutReal& g22(int x, int y, int z) const { + return contravariantMetricTensor.g22(x, y, z); + } + const BoutReal& g33(int x, int y, int z) const { + return contravariantMetricTensor.g33(x, y, z); + } + const BoutReal& g12(int x, int y, int z) const { + return contravariantMetricTensor.g12(x, y, z); + } + const BoutReal& g13(int x, int y, int z) const { + return contravariantMetricTensor.g13(x, y, z); + } + const BoutReal& g23(int x, int y, int z) const { + return contravariantMetricTensor.g23(x, y, z); + } #if BOUT_USE_METRIC_3D != 1 const BoutReal& g11(int x, int y) const { return contravariantMetricTensor.g11(x, y); } @@ -373,20 +394,14 @@ public: void setG2(const FieldMetric& G2) const { g_values().setG2(G2); } void setG3(const FieldMetric& G3) const { g_values().setG3(G3); } - const BoutReal& G1(int x, int y, int z) const { - return G1()(x, y, z); } - const BoutReal& G2(int x, int y, int z) const { - return G2()(x, y, z); } - const BoutReal& G3(int x, int y, int z) const { - return G3()(x, y, z); } + const BoutReal& G1(int x, int y, int z) const { return G1()(x, y, z); } + const BoutReal& G2(int x, int y, int z) const { return G2()(x, y, z); } + const BoutReal& G3(int x, int y, int z) const { return G3()(x, y, z); } #if not(BOUT_USE_METRIC_3D) - const BoutReal& G1(int x, int y) const { - return G1()(x, y); } - const BoutReal& G2(int x, int y) const { - return G2()(x, y); } - const BoutReal& G3(int x, int y) const { - return G3()(x, y); } + const BoutReal& G1(int x, int y) const { return G1()(x, y); } + const BoutReal& G2(int x, int y) const { return G2()(x, y); } + const BoutReal& G3(int x, int y) const { return G3()(x, y); } #endif const FieldMetric& Grad2_par2_DDY_invSg(CELL_LOC outloc, diff --git a/include/bout/fv_ops.hxx b/include/bout/fv_ops.hxx index 380a536be6..612d61436e 100644 --- a/include/bout/fv_ops.hxx +++ b/include/bout/fv_ops.hxx @@ -243,9 +243,8 @@ const Field3D Div_par(const Field3D& f_in, const Field3D& v_in, // Pre-calculate factors which multiply fluxes #if not(BOUT_USE_METRIC_3D) // For right cell boundaries - BoutReal common_factor = - (coord->J(i, j) + coord->J(i, j + 1)) - / (sqrt(coord->g_22(i, j)) + sqrt(coord->g_22(i, j + 1))); + BoutReal common_factor = (coord->J(i, j) + coord->J(i, j + 1)) + / (sqrt(coord->g_22(i, j)) + sqrt(coord->g_22(i, j + 1))); BoutReal flux_factor_rc = common_factor / (coord->dy(i, j) * coord->J(i, j)); BoutReal flux_factor_rp = @@ -272,9 +271,8 @@ const Field3D Div_par(const Field3D& f_in, const Field3D& v_in, common_factor / (coord->dy(i, j + 1, k) * coord->J(i, j + 1, k)); // For left cell boundaries - common_factor = - (coord->J(i, j, k) + coord->J(i, j - 1, k)) - / (sqrt(coord->g_22(i, j, k)) + sqrt(coord->g_22(i, j - 1, k))); + common_factor = (coord->J(i, j, k) + coord->J(i, j - 1, k)) + / (sqrt(coord->g_22(i, j, k)) + sqrt(coord->g_22(i, j - 1, k))); BoutReal flux_factor_lc = common_factor / (coord->dy(i, j, k) * coord->J(i, j, k)); diff --git a/src/invert/laplace/impls/multigrid/multigrid_laplace.cxx b/src/invert/laplace/impls/multigrid/multigrid_laplace.cxx index b80e5ae71e..700c1bc3bd 100644 --- a/src/invert/laplace/impls/multigrid/multigrid_laplace.cxx +++ b/src/invert/laplace/impls/multigrid/multigrid_laplace.cxx @@ -602,34 +602,32 @@ void LaplaceMultigrid::generateMatrixF(int level) { BoutReal ddz_C = (C2(i2, yindex, k2p) - C2(i2, yindex, k2m)) / 2. / dz / C1(i2, yindex, k2); - BoutReal ddx = D(i2, yindex, k2) * coords->g11(i2, yindex) - / coords->dx(i2, yindex) / coords->dx(i2, yindex); + BoutReal ddx = D(i2, yindex, k2) * coords->g11(i2, yindex) / coords->dx(i2, yindex) + / coords->dx(i2, yindex); // coefficient of 2nd derivative stencil (x-direction) BoutReal ddz = D(i2, yindex, k2) * coords->g33(i2, yindex) / SQ(dz); // coefficient of 2nd derivative stencil (z-direction) - BoutReal dxdz = D(i2, yindex, k2) * 2. * coords->g13(i2, yindex) - / coords->dx(i2, yindex) / dz; + BoutReal dxdz = + D(i2, yindex, k2) * 2. * coords->g13(i2, yindex) / coords->dx(i2, yindex) / dz; // coefficient of mixed derivative stencil (could assume zero, at least initially, // if easier; then check this is true in constructor) BoutReal dxd = - (D(i2, yindex, k2) * coords->G1(i2, yindex) - + coords->g11(i2, yindex) * ddx_C + (D(i2, yindex, k2) * coords->G1(i2, yindex) + coords->g11(i2, yindex) * ddx_C + coords->g13(i2, yindex) * ddz_C // (could assume zero, at least initially, if easier; then check this is true in constructor) ) / coords->dx(i2, - yindex); // coefficient of 1st derivative stencil (x-direction) + yindex); // coefficient of 1st derivative stencil (x-direction) if (nonuniform) { // add correction for non-uniform dx dxd += D(i2, yindex, k2) * coords->d1_dx(i2, yindex); } BoutReal dzd = - (D(i2, yindex, k2) * coords->G3(i2, yindex) - + coords->g33(i2, yindex) * ddz_C + (D(i2, yindex, k2) * coords->G3(i2, yindex) + coords->g33(i2, yindex) * ddz_C + coords->g13(i2, yindex) * ddx_C // (could assume zero, at least initially, if easier; then check // this is true in constructor) diff --git a/src/invert/laplace/impls/petsc/petsc_laplace.cxx b/src/invert/laplace/impls/petsc/petsc_laplace.cxx index ea633ba276..f06f4c7de6 100644 --- a/src/invert/laplace/impls/petsc/petsc_laplace.cxx +++ b/src/invert/laplace/impls/petsc/petsc_laplace.cxx @@ -385,8 +385,7 @@ FieldPerp LaplacePetsc::solve(const FieldPerp& b, const FieldPerp& x0) { if (fourth_order) { // Fourth Order Accuracy on Boundary Element(i, x, z, 0, 0, - -25.0 / (12.0 * coords->dx(x, y, z)) - / sqrt(coords->g_11(x, y, z)), + -25.0 / (12.0 * coords->dx(x, y, z)) / sqrt(coords->g_11(x, y, z)), MatA); Element(i, x, z, 1, 0, 4.0 / coords->dx(x, y, z) / sqrt(coords->g_11(x, y, z)), MatA); @@ -396,8 +395,7 @@ FieldPerp LaplacePetsc::solve(const FieldPerp& b, const FieldPerp& x0) { 4.0 / (3.0 * coords->dx(x, y, z)) / sqrt(coords->g_11(x, y, z)), MatA); Element(i, x, z, 4, 0, - -1.0 / (4.0 * coords->dx(x, y, z)) - / sqrt(coords->g_11(x, y, z)), + -1.0 / (4.0 * coords->dx(x, y, z)) / sqrt(coords->g_11(x, y, z)), MatA); } else { // Second Order Accuracy on Boundary @@ -652,16 +650,14 @@ FieldPerp LaplacePetsc::solve(const FieldPerp& b, const FieldPerp& x0) { if (fourth_order) { // Fourth Order Accuracy on Boundary Element(i, x, z, 0, 0, - 25.0 / (12.0 * coords->dx(x, y, z)) - / sqrt(coords->g_11(x, y, z)), + 25.0 / (12.0 * coords->dx(x, y, z)) / sqrt(coords->g_11(x, y, z)), MatA); Element(i, x, z, -1, 0, -4.0 / coords->dx(x, y, z) / sqrt(coords->g_11(x, y, z)), MatA); Element(i, x, z, -2, 0, 3.0 / coords->dx(x, y, z) / sqrt(coords->g_11(x, y, z)), MatA); Element(i, x, z, -3, 0, - -4.0 / (3.0 * coords->dx(x, y, z)) - / sqrt(coords->g_11(x, y, z)), + -4.0 / (3.0 * coords->dx(x, y, z)) / sqrt(coords->g_11(x, y, z)), MatA); Element(i, x, z, -4, 0, 1.0 / (4.0 * coords->dx(x, y, z)) / sqrt(coords->g_11(x, y, z)), diff --git a/src/invert/laplace/impls/petsc3damg/petsc3damg.cxx b/src/invert/laplace/impls/petsc3damg/petsc3damg.cxx index 0fab8f4171..7f4bcbd4b2 100644 --- a/src/invert/laplace/impls/petsc3damg/petsc3damg.cxx +++ b/src/invert/laplace/impls/petsc3damg/petsc3damg.cxx @@ -120,7 +120,8 @@ LaplacePetsc3dAmg::LaplacePetsc3dAmg(Options* opt, const CELL_LOC loc, Mesh* mes // Set up boundary conditions in operator const bool inner_X_neumann = isInnerBoundaryFlagSet(INVERT_AC_GRAD); - const auto inner_X_BC = inner_X_neumann ? -1. / coords->dx() / sqrt(coords->g_11()) : 0.5; + const auto inner_X_BC = + inner_X_neumann ? -1. / coords->dx() / sqrt(coords->g_11()) : 0.5; const auto inner_X_BC_plus = inner_X_neumann ? -inner_X_BC : 0.5; BOUT_FOR_SERIAL(i, indexer->getRegionInnerX()) { @@ -129,7 +130,8 @@ LaplacePetsc3dAmg::LaplacePetsc3dAmg(Options* opt, const CELL_LOC loc, Mesh* mes } const bool outer_X_neumann = isOuterBoundaryFlagSet(INVERT_AC_GRAD); - const auto outer_X_BC = outer_X_neumann ? 1. / coords->dx() / sqrt(coords->g_11()) : 0.5; + const auto outer_X_BC = + outer_X_neumann ? 1. / coords->dx() / sqrt(coords->g_11()) : 0.5; const auto outer_X_BC_minus = outer_X_neumann ? -outer_X_BC : 0.5; BOUT_FOR_SERIAL(i, indexer->getRegionOuterX()) { @@ -370,7 +372,7 @@ void LaplacePetsc3dAmg::updateMatrix3D() { C_df_dy += (coords->g12()[l] * dc_dx[l] + (coords->g22()[l] - 1. / coords->g_22()[l]) * dc_dy[l] + coords->g23()[l] * dc_dz[l]) - / C1[l]; + / C1[l]; } BoutReal C_d2f_dy2 = (coords->g22()[l] - 1.0 / coords->g_22()[l]); @@ -393,8 +395,8 @@ void LaplacePetsc3dAmg::updateMatrix3D() { C_d2f_dy2 /= SQ(coords->dy()[l]); C_d2f_dxdy /= 4 * coords->dx()[l]; // NOTE: This value is not completed here. It needs to - // be divide by dx(i +/- 1, j, k) when using to set a - // matrix element + // be divide by dx(i +/- 1, j, k) when using to set a + // matrix element C_d2f_dydz /= 4 * coords->dy()[l] * coords->dz()[l]; // The values stored in the y-boundary are already interpolated diff --git a/src/invert/laplace/impls/serial_band/serial_band.cxx b/src/invert/laplace/impls/serial_band/serial_band.cxx index edd069c2e9..4e7bb4c63f 100644 --- a/src/invert/laplace/impls/serial_band/serial_band.cxx +++ b/src/invert/laplace/impls/serial_band/serial_band.cxx @@ -161,9 +161,9 @@ FieldPerp LaplaceSerialBand::solve(const FieldPerp& b, const FieldPerp& x0) { coef1 = coords->g11(ix, jy); // X 2nd derivative coef2 = coords->g33(ix, jy); // Z 2nd derivative coef3 = coords->g13(ix, jy); // X-Z mixed derivatives - coef4 = 0.0; // X 1st derivative - coef5 = 0.0; // Z 1st derivative - coef6 = Acoef(ix, jy); // Constant + coef4 = 0.0; // X 1st derivative + coef5 = 0.0; // Z 1st derivative + coef6 = Acoef(ix, jy); // Constant // Multiply Delp2 component by a factor coef1 *= Dcoef(ix, jy); @@ -347,11 +347,11 @@ FieldPerp LaplaceSerialBand::solve(const FieldPerp& b, const FieldPerp& x0) { // Combine 4th order at 1 with 2nd order at 0 A(1, 0) = 0.0; // Not used - A(1, 1) = dcomplex((14. - - SQ(coords->dx(0, jy) * kwave) * coords->g33(0, jy) - / coords->g11(0, jy)) - * coef1, - -coef3); + A(1, 1) = dcomplex( + (14. + - SQ(coords->dx(0, jy) * kwave) * coords->g33(0, jy) / coords->g11(0, jy)) + * coef1, + -coef3); A(1, 2) = dcomplex(-29. * coef1 - SQ(kwave) * coef2 + coef4, 0.0); A(1, 3) = dcomplex(16. * coef1, coef3); A(1, 4) = dcomplex(-coef1, 0.0); diff --git a/src/invert/laplace/invert_laplace.cxx b/src/invert/laplace/invert_laplace.cxx index feb2a182a6..e9008cf4f0 100644 --- a/src/invert/laplace/invert_laplace.cxx +++ b/src/invert/laplace/invert_laplace.cxx @@ -607,8 +607,7 @@ void Laplacian::tridagMatrix(dcomplex* avec, dcomplex* bvec, dcomplex* cvec, dco avec[ix] = dcomplex(0., 0.); bvec[ix] = dcomplex(-1., 0.) / sqrt(coords->g_11(ix, jy)) / coords->dx(ix, jy); - cvec[ix] = - dcomplex(1., 0.) / sqrt(coords->g_11(ix, jy)) / coords->dx(ix, jy); + cvec[ix] = dcomplex(1., 0.) / sqrt(coords->g_11(ix, jy)) / coords->dx(ix, jy); } } else if (isInnerBoundaryFlagSet(INVERT_AC_GRAD)) { // Zero gradient at inner boundary @@ -622,8 +621,8 @@ void Laplacian::tridagMatrix(dcomplex* avec, dcomplex* bvec, dcomplex* cvec, dco for (int ix = 0; ix < inbndry; ix++) { avec[ix] = 0.0; bvec[ix] = 1.0; - cvec[ix] = -exp(-1.0 * sqrt(coords->g33(ix, jy) / coords->g11(ix, jy)) - * kwave * coords->dx(ix, jy)); + cvec[ix] = -exp(-1.0 * sqrt(coords->g33(ix, jy) / coords->g11(ix, jy)) * kwave + * coords->dx(ix, jy)); } } else if (isInnerBoundaryFlagSet(INVERT_IN_CYLINDER)) { // Condition for inner radial boundary for cylindrical coordinates diff --git a/src/invert/laplacexz/impls/petsc/laplacexz-petsc.cxx b/src/invert/laplacexz/impls/petsc/laplacexz-petsc.cxx index 5672977ac8..8ddb058ab2 100644 --- a/src/invert/laplacexz/impls/petsc/laplacexz-petsc.cxx +++ b/src/invert/laplacexz/impls/petsc/laplacexz-petsc.cxx @@ -403,8 +403,7 @@ void LaplaceXZpetsc::setCoefs(const Field3D& Ain, const Field3D& Bin) { { // Metrics on x+1/2 boundary const BoutReal J = 0.5 * (coords->J(x, y, z) + coords->J(x + 1, y, z)); - const BoutReal g11 = - 0.5 * (coords->g11(x, y, z) + coords->g11(x + 1, y, z)); + const BoutReal g11 = 0.5 * (coords->g11(x, y, z) + coords->g11(x + 1, y, z)); const BoutReal dx = 0.5 * (coords->dx(x, y, z) + coords->dx(x + 1, y, z)); const BoutReal Acoef = 0.5 * (A(x, y, z) + A(x + 1, y, z)); @@ -417,8 +416,7 @@ void LaplaceXZpetsc::setCoefs(const Field3D& Ain, const Field3D& Bin) { { // Metrics on x-1/2 boundary const BoutReal J = 0.5 * (coords->J(x, y, z) + coords->J(x - 1, y, z)); - const BoutReal g11 = - 0.5 * (coords->g11(x, y, z) + coords->g11(x - 1, y, z)); + const BoutReal g11 = 0.5 * (coords->g11(x, y, z) + coords->g11(x - 1, y, z)); const BoutReal dx = 0.5 * (coords->dx(x, y, z) + coords->dx(x - 1, y, z)); const BoutReal Acoef = 0.5 * (A(x, y, z) + A(x - 1, y, z)); @@ -435,8 +433,7 @@ void LaplaceXZpetsc::setCoefs(const Field3D& Ain, const Field3D& Bin) { { const BoutReal J = 0.5 * (coords->J(x, y, z) + coords->J(x, y, zplus)); - const BoutReal g33 = - 0.5 * (coords->g33(x, y, z) + coords->g33(x, y, zplus)); + const BoutReal g33 = 0.5 * (coords->g33(x, y, z) + coords->g33(x, y, zplus)); const BoutReal dz = 0.5 * (coords->dz(x, y, z) + coords->dz(x, y, zplus)); // Metrics on z+1/2 boundary const BoutReal Acoef = 0.5 * (A(x, y, z) + A(x, y, zplus)); @@ -448,8 +445,7 @@ void LaplaceXZpetsc::setCoefs(const Field3D& Ain, const Field3D& Bin) { { // Metrics on z-1/2 boundary const BoutReal J = 0.5 * (coords->J(x, y, z) + coords->J(x, y, zminus)); - const BoutReal g33 = - 0.5 * (coords->g33(x, y, z) + coords->g33(x, y, zminus)); + const BoutReal g33 = 0.5 * (coords->g33(x, y, z) + coords->g33(x, y, zminus)); const BoutReal Acoef = 0.5 * (A(x, y, z) + A(x, y, zminus)); const BoutReal dz = 0.5 * (coords->dz(x, y, z) + coords->dz(x, y, zminus)); @@ -475,8 +471,7 @@ void LaplaceXZpetsc::setCoefs(const Field3D& Ain, const Field3D& Bin) { // Metrics const BoutReal J = 0.5 * (coords->J(x, y, z) + coords->J(x + 1, y, z)); - const BoutReal g13 = - 0.5 * (coords->g13(x, y, z) + coords->g13(x + 1, y, z)); + const BoutReal g13 = 0.5 * (coords->g13(x, y, z) + coords->g13(x + 1, y, z)); const BoutReal dz = 0.5 * (coords->dz(x, y, z) + coords->dz(x + 1, y, z)); const BoutReal Acoef = 0.5 * (A(x, y, z) + A(x + 1, y, z)); @@ -512,8 +507,7 @@ void LaplaceXZpetsc::setCoefs(const Field3D& Ain, const Field3D& Bin) { // so metrics at (x-1/2,z) const BoutReal J = 0.5 * (coords->J(x, y, z) + coords->J(x - 1, y, z)); - const BoutReal g13 = - 0.5 * (coords->g13(x, y, z) + coords->g13(x - 1, y, z)); + const BoutReal g13 = 0.5 * (coords->g13(x, y, z) + coords->g13(x - 1, y, z)); const BoutReal dz = 0.5 * (coords->dz(x, y, z) + coords->dz(x - 1, y, z)); const BoutReal Acoef = 0.5 * (A(x, y, z) + A(x - 1, y, z)); @@ -546,8 +540,7 @@ void LaplaceXZpetsc::setCoefs(const Field3D& Ain, const Field3D& Bin) { // so metrics at (x,z+1/2) const BoutReal J = 0.5 * (coords->J(x, y, z) + coords->J(x, y, zplus)); - const BoutReal g13 = - 0.5 * (coords->g13(x, y, z) + coords->g13(x, y, zplus)); + const BoutReal g13 = 0.5 * (coords->g13(x, y, z) + coords->g13(x, y, zplus)); const BoutReal dx = 0.5 * (coords->dx(x, y, z) + coords->dx(x, y, zplus)); const BoutReal Acoef = 0.5 * (A(x, y, z) + A(x, y, zplus)); @@ -580,8 +573,7 @@ void LaplaceXZpetsc::setCoefs(const Field3D& Ain, const Field3D& Bin) { // so metrics at (x,z-1/2) const BoutReal J = 0.5 * (coords->J(x, y, z) + coords->J(x, y, zminus)); - const BoutReal g13 = - 0.5 * (coords->g13(x, y, z) + coords->g13(x, y, zminus)); + const BoutReal g13 = 0.5 * (coords->g13(x, y, z) + coords->g13(x, y, zminus)); const BoutReal dx = 0.5 * (coords->dx(x, y, z) + coords->dx(x, y, zminus)); const BoutReal Acoef = 0.5 * (A(x, y, z) + A(x, y, zminus)); diff --git a/src/invert/parderiv/impls/cyclic/cyclic.cxx b/src/invert/parderiv/impls/cyclic/cyclic.cxx index 00e35b2e7e..7228dbe0e3 100644 --- a/src/invert/parderiv/impls/cyclic/cyclic.cxx +++ b/src/invert/parderiv/impls/cyclic/cyclic.cxx @@ -155,8 +155,8 @@ const Field3D InvertParCR::solve(const Field3D& f) { BoutReal acoef = A(x, y + local_ystart); // Constant BoutReal bcoef = B(x, y + local_ystart) / coord->g_22(x, y + local_ystart); // d2dy2 - BoutReal ccoef = C(x, y + local_ystart); // d2dydz - BoutReal dcoef = D(x, y + local_ystart); // d2dz2 + BoutReal ccoef = C(x, y + local_ystart); // d2dydz + BoutReal dcoef = D(x, y + local_ystart); // d2dz2 BoutReal ecoef = E(x, y + local_ystart) + sg(x, y + local_ystart) * B(x, y + local_ystart); // ddy diff --git a/src/mesh/boundary_standard.cxx b/src/mesh/boundary_standard.cxx index 3702cd6ee7..a2bcc15a36 100644 --- a/src/mesh/boundary_standard.cxx +++ b/src/mesh/boundary_standard.cxx @@ -1605,12 +1605,12 @@ void BoundaryNeumann_NonOrthogonal::apply(Field2D& f) { // Loop over all elements and set equal to the next point in for (bndry->first(); !bndry->isDone(); bndry->next1d()) { // Interpolate (linearly) metrics to halfway between last cell and boundary cell - BoutReal g11shift = 0.5 - * (metric->g11(bndry->x, bndry->y) - + metric->g11(bndry->x - bndry->bx, bndry->y)); - BoutReal g12shift = 0.5 - * (metric->g12(bndry->x, bndry->y) - + metric->g12(bndry->x - bndry->bx, bndry->y)); + BoutReal g11shift = + 0.5 + * (metric->g11(bndry->x, bndry->y) + metric->g11(bndry->x - bndry->bx, bndry->y)); + BoutReal g12shift = + 0.5 + * (metric->g12(bndry->x, bndry->y) + metric->g12(bndry->x - bndry->bx, bndry->y)); // Have to use derivatives at last gridpoint instead of derivatives on boundary layer // because derivative values don't exist in boundary region // NOTE: should be fixed to interpolate to boundary line @@ -2669,7 +2669,7 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { -1.0 * sqrt(metric->g33(x, y) / metric->g11(x, y)) * metric->dx(x, y); for (int jz = 1; jz <= ncz / 2; jz++) { BoutReal kwave = - jz * 2.0 * PI / metric->zlength(x, y); // wavenumber in [rad^-1] + jz * 2.0 * PI / metric->zlength(x, y); // wavenumber in [rad^-1] c0[jz] *= exp(coef * kwave); // The decaying solution only } // Reverse FFT @@ -2877,8 +2877,7 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { do { // kz = 0 solution xpos -= metric->dx(x, y); - c2[0] = - c0[0] + k0lin * xpos + 0.5 * c1[0] * xpos * xpos / metric->g11(x - bx, y); + c2[0] = c0[0] + k0lin * xpos + 0.5 * c1[0] * xpos * xpos / metric->g11(x - bx, y); // kz != 0 solution BoutReal coef = -1.0 * sqrt(metric->g33(x - bx, y) / metric->g11(x - bx, y)) * metric->dx(x - bx, y); @@ -2974,27 +2973,24 @@ void BoundaryNeumann_NonOrthogonal::apply(Field3D& f) { tmp = -(metric->J(jx, jy) * metric->g12(jx, jy) * var.y(jx, jy, jz) + metric->J(jx, jy) * metric->g13(jx, jy) * var.z(jx, jy, jz) - - metric->J(jx - 2, jy) * metric->g12(jx - 2, jy) - * var.y(jx - 2, jy, jz) - + metric->J(jx - 2, jy) * metric->g13(jx - 2, jy) - * var.z(jx - 2, jy, jz)) + - metric->J(jx - 2, jy) * metric->g12(jx - 2, jy) * var.y(jx - 2, jy, jz) + + metric->J(jx - 2, jy) * metric->g13(jx - 2, jy) * var.z(jx - 2, jy, jz)) / (metric->dx(jx - 2, jy) + metric->dx(jx - 1, - jy)); // First term (d/dx) using vals calculated above - tmp -= - (metric->J(jx - 1, jy + 1) * metric->g12(jx - 1, jy + 1) - * var.x(jx - 1, jy + 1, jz) - - metric->J(jx - 1, jy - 1) * metric->g12(jx - 1, jy - 1) - * var.x(jx - 1, jy - 1, jz) - + metric->J(jx - 1, jy + 1) * metric->g22(jx - 1, jy + 1) - * var.y(jx - 1, jy + 1, jz) - - metric->J(jx - 1, jy - 1) * metric->g22(jx - 1, jy - 1) - * var.y(jx - 1, jy - 1, jz) - + metric->J(jx - 1, jy + 1) * metric->g23(jx - 1, jy + 1) - * var.z(jx - 1, jy + 1, jz) - - metric->J(jx - 1, jy - 1) * metric->g23(jx - 1, jy - 1) - * var.z(jx - 1, jy - 1, jz)) - / (metric->dy(jx - 1, jy - 1) + metric->dy(jx - 1, jy)); // second (d/dy) + jy)); // First term (d/dx) using vals calculated above + tmp -= (metric->J(jx - 1, jy + 1) * metric->g12(jx - 1, jy + 1) + * var.x(jx - 1, jy + 1, jz) + - metric->J(jx - 1, jy - 1) * metric->g12(jx - 1, jy - 1) + * var.x(jx - 1, jy - 1, jz) + + metric->J(jx - 1, jy + 1) * metric->g22(jx - 1, jy + 1) + * var.y(jx - 1, jy + 1, jz) + - metric->J(jx - 1, jy - 1) * metric->g22(jx - 1, jy - 1) + * var.y(jx - 1, jy - 1, jz) + + metric->J(jx - 1, jy + 1) * metric->g23(jx - 1, jy + 1) + * var.z(jx - 1, jy + 1, jz) + - metric->J(jx - 1, jy - 1) * metric->g23(jx - 1, jy - 1) + * var.z(jx - 1, jy - 1, jz)) + / (metric->dy(jx - 1, jy - 1) + metric->dy(jx - 1, jy)); // second (d/dy) tmp -= (metric->J(jx - 1, jy) * metric->g13(jx - 1, jy) * (var.x(jx - 1, jy, jzp) - var.x(jx - 1, jy, jzm)) + metric->J(jx - 1, jy) * metric->g23(jx - 1, jy) diff --git a/src/mesh/difops.cxx b/src/mesh/difops.cxx index 55a38a3077..78c1ef35a1 100644 --- a/src/mesh/difops.cxx +++ b/src/mesh/difops.cxx @@ -128,15 +128,15 @@ Field3D Grad_parP(const Field3D& apar, const Field3D& f) { BoutReal fp, fm; // X differencing - fp = f(x + 1, y, z) - + (0.25 * dl / metric->dz(x, y, z)) * bz - * (f(x + 1, y, zm) - f(x + 1, y, zp)) - - 0.5 * dl * by * gys(x + 1, y, z); + fp = + f(x + 1, y, z) + + (0.25 * dl / metric->dz(x, y, z)) * bz * (f(x + 1, y, zm) - f(x + 1, y, zp)) + - 0.5 * dl * by * gys(x + 1, y, z); - fm = f(x - 1, y, z) - + (0.25 * dl / metric->dz(x, y, z)) * bz - * (f(x - 1, y, zm) - f(x - 1, y, zp)) - - 0.5 * dl * by * gys(x - 1, y, z); + fm = + f(x - 1, y, z) + + (0.25 * dl / metric->dz(x, y, z)) * bz * (f(x - 1, y, zm) - f(x - 1, y, zp)) + - 0.5 * dl * by * gys(x - 1, y, z); result(x, y, z) = bx * (fp - fm) / (0.5 * metric->dx(x - 1, y, z) + metric->dx(x, y, z) @@ -144,15 +144,15 @@ Field3D Grad_parP(const Field3D& apar, const Field3D& f) { // Z differencing - fp = f(x, y, zp) - + (0.25 * dl / metric->dx(x, y, z)) * bx - * (f(x - 1, y, zp) - f(x + 1, y, zp)) - - 0.5 * dl * by * gys(x, y, zp); + fp = + f(x, y, zp) + + (0.25 * dl / metric->dx(x, y, z)) * bx * (f(x - 1, y, zp) - f(x + 1, y, zp)) + - 0.5 * dl * by * gys(x, y, zp); - fm = f(x, y, zm) - + (0.25 * dl / metric->dx(x, y, z)) * bx - * (f(x - 1, y, zm) - f(x + 1, y, zm)) - - 0.5 * dl * by * gys(x, y, zm); + fm = + f(x, y, zm) + + (0.25 * dl / metric->dx(x, y, z)) * bx * (f(x - 1, y, zm) - f(x + 1, y, zm)) + - 0.5 * dl * by * gys(x, y, zm); result(x, y, z) += bz * (fp - fm) / (0.5 * metric->dz(x, y, zm) + metric->dz(x, y, z) diff --git a/src/mesh/fv_ops.cxx b/src/mesh/fv_ops.cxx index 38d3858677..96a95f4cd9 100644 --- a/src/mesh/fv_ops.cxx +++ b/src/mesh/fv_ops.cxx @@ -58,8 +58,7 @@ Field3D Div_a_Grad_perp(const Field3D& a, const Field3D& f) { / (coord->dx(i, j, k) + coord->dx(i + 1, j, k)); result(i, j, k) += fout / (coord->dx(i, j, k) * coord->J(i, j, k)); - result(i + 1, j, k) -= - fout / (coord->dx(i + 1, j, k) * coord->J(i + 1, j, k)); + result(i + 1, j, k) -= fout / (coord->dx(i + 1, j, k) * coord->J(i + 1, j, k)); } } } @@ -297,8 +296,7 @@ const Field3D D4DY4(const Field3D& d_in, const Field3D& f_in) { * (coord->J(i, j, k) + coord->J(i, j + 1, k)) * d3fdy3; result(i, j, k) += flux / (coord->J(i, j, k) * coord->dz(i, j, k)); - result(i, j + 1, k) -= - flux / (coord->J(i, j + 1, k) * coord->dz(i, j + 1, k)); + result(i, j + 1, k) -= flux / (coord->J(i, j + 1, k) * coord->dz(i, j + 1, k)); } } } @@ -335,9 +333,9 @@ const Field3D D4DY4_Index(const Field3D& f_in, bool bndry_flux) { for (int k = 0; k < mesh->LocalNz; k++) { // Right boundary common factors - const BoutReal common_factor = - 0.25 * (coord->dz(i, j, k) + coord->dz(i, j + 1, k)) - * (coord->J(i, j, j) + coord->J(i, j + 1, k)); + const BoutReal common_factor = 0.25 + * (coord->dz(i, j, k) + coord->dz(i, j + 1, k)) + * (coord->J(i, j, j) + coord->J(i, j + 1, k)); const BoutReal factor_rc = common_factor / (coord->J(i, j, k) * coord->dz(i, j, k)); @@ -359,9 +357,9 @@ const Field3D D4DY4_Index(const Field3D& f_in, bool bndry_flux) { for (int k = 0; k < mesh->LocalNz; k++) { // Right boundary common factors - const BoutReal common_factor = - 0.25 * (coord->dz(i, j, k) + coord->dz(i, j + 1, k)) - * (coord->J(i, j, j) + coord->J(i, j + 1, k)); + const BoutReal common_factor = 0.25 + * (coord->dz(i, j, k) + coord->dz(i, j + 1, k)) + * (coord->J(i, j, j) + coord->J(i, j + 1, k)); const BoutReal factor_rc = common_factor / (coord->J(i, j, k) * coord->dz(i, j, k)); @@ -388,9 +386,9 @@ const Field3D D4DY4_Index(const Field3D& f_in, bool bndry_flux) { if (j != mesh->ystart || !has_lower_boundary) { for (int k = 0; k < mesh->LocalNz; k++) { - const BoutReal common_factor = - 0.25 * (coord->dz(i, j, k) + coord->dz(i, j + 1, k)) - * (coord->J(i, j, k) + coord->J(i, j - 1, k)); + const BoutReal common_factor = 0.25 + * (coord->dz(i, j, k) + coord->dz(i, j + 1, k)) + * (coord->J(i, j, k) + coord->J(i, j - 1, k)); const BoutReal factor_lc = common_factor / (coord->J(i, j, k) * coord->dz(i, j, k)); @@ -407,9 +405,9 @@ const Field3D D4DY4_Index(const Field3D& f_in, bool bndry_flux) { } else { // On a domain (Y) boundary for (int k = 0; k < mesh->LocalNz; k++) { - const BoutReal common_factor = - 0.25 * (coord->dz(i, j, k) + coord->dz(i, j + 1, k)) - * (coord->J(i, j, k) + coord->J(i, j - 1, k)); + const BoutReal common_factor = 0.25 + * (coord->dz(i, j, k) + coord->dz(i, j + 1, k)) + * (coord->J(i, j, k) + coord->J(i, j - 1, k)); const BoutReal factor_lc = common_factor / (coord->J(i, j, k) * coord->dz(i, j, k)); diff --git a/tests/unit/mesh/test_coordinates.cxx b/tests/unit/mesh/test_coordinates.cxx index fd37dcc94e..e0c301c574 100644 --- a/tests/unit/mesh/test_coordinates.cxx +++ b/tests/unit/mesh/test_coordinates.cxx @@ -573,5 +573,4 @@ TEST_F(CoordinatesTest, IndexedAccessors) { #if BOUT_USE_METRIC_3D EXPECT_EQ(actual_dz, expected_dz); #endif - } From cbce1b411f3e42858150c0d1b494bdb06dbeaa6f Mon Sep 17 00:00:00 2001 From: Peter Hill Date: Wed, 23 Oct 2024 11:59:12 +0100 Subject: [PATCH 473/491] Revert "x, y, z are accessed through getters on BoundaryRegionPar." This reverts commit 541ff17b3e424f1eca57bf672ec650d274333218. Issue is fixed in separate PR --- examples/fci-wave-logn/fci-wave.cxx | 20 +++++++++----------- examples/fci-wave/fci-wave.cxx | 23 +++++++++-------------- 2 files changed, 18 insertions(+), 25 deletions(-) diff --git a/examples/fci-wave-logn/fci-wave.cxx b/examples/fci-wave-logn/fci-wave.cxx index 2235d3b7db..d934b91ed3 100644 --- a/examples/fci-wave-logn/fci-wave.cxx +++ b/examples/fci-wave-logn/fci-wave.cxx @@ -30,9 +30,9 @@ class FCIwave : public PhysicsModel { const Field3D& B_next = Bxyz.ynext(reg->dir); for (reg->first(); !reg->isDone(); reg->next()) { - f_B_next(reg->ind().x(), reg->ind().y() + reg->dir, reg->ind().z()) = - f_next(reg->ind().x(), reg->ind().y() + reg->dir, reg->ind().z()) - / B_next(reg->ind().x(), reg->ind().y() + reg->dir, reg->ind().z()); + f_B_next(reg->x, reg->y + reg->dir, reg->z) = + f_next(reg->x, reg->y + reg->dir, reg->z) + / B_next(reg->x, reg->y + reg->dir, reg->z); } } @@ -108,17 +108,15 @@ class FCIwave : public PhysicsModel { const Field3D& v_next = v.ynext(reg->dir); for (reg->first(); !reg->isDone(); reg->next()) { - BoutReal n_b = - exp(0.5 - * (logn_next(reg->ind().x(), reg->ind().y() + reg->dir, reg->ind().z()) - + logn(reg->ind().x(), reg->ind().y(), reg->ind().z()))); + BoutReal n_b = exp(0.5 + * (logn_next(reg->x, reg->y + reg->dir, reg->z) + + logn(reg->x, reg->y, reg->z))); BoutReal v_b = 0.5 - * (v_next(reg->ind().x(), reg->ind().y() + reg->dir, reg->ind().z()) - + v(reg->ind().x(), reg->ind().y(), reg->ind().z())); + * (v_next(reg->x, reg->y + reg->dir, reg->z) + v(reg->x, reg->y, reg->z)); - nv_next(reg->ind().x(), reg->ind().y() + reg->dir, reg->ind().z()) = - 2. * n_b * v_b - nv(reg->ind().x(), reg->ind().y(), reg->ind().z()); + nv_next(reg->x, reg->y + reg->dir, reg->z) = + 2. * n_b * v_b - nv(reg->x, reg->y, reg->z); } } diff --git a/examples/fci-wave/fci-wave.cxx b/examples/fci-wave/fci-wave.cxx index 220db59d37..4bf2c7b714 100644 --- a/examples/fci-wave/fci-wave.cxx +++ b/examples/fci-wave/fci-wave.cxx @@ -31,9 +31,9 @@ class FCIwave : public PhysicsModel { const Field3D& B_next = Bxyz.ynext(reg->dir); for (reg->first(); !reg->isDone(); reg->next()) { - f_B_next(reg->ind().x(), reg->ind().y() + reg->dir, reg->ind().z()) = - f_next(reg->ind().x(), reg->ind().y() + reg->dir, reg->ind().z()) - / B_next(reg->ind().x(), reg->ind().y() + reg->dir, reg->ind().z()); + f_B_next(reg->x, reg->y + reg->dir, reg->z) = + f_next(reg->x, reg->y + reg->dir, reg->z) + / B_next(reg->x, reg->y + reg->dir, reg->z); } } @@ -131,21 +131,16 @@ class FCIwave : public PhysicsModel { // Note: If evolving density, this should interpolate logn // but neumann boundaries are used here anyway. BoutReal n_b = - 0.5 - * (n_next(reg->ind().x(), reg->ind().y() + reg->dir, reg->ind().z()) - + n(reg->ind().x(), reg->ind().y(), reg->ind().z())); + 0.5 * (n_next(reg->x, reg->y + reg->dir, reg->z) + n(reg->x, reg->y, reg->z)); // Velocity at the boundary BoutReal v_b = - 0.5 - * (v_next(reg->ind().x(), reg->ind().y() + reg->dir, reg->ind().z()) - + v(reg->ind().x(), reg->ind().y(), reg->ind().z())); + 0.5 * (v_next(reg->x, reg->y + reg->dir, reg->z) + v(reg->x, reg->y, reg->z)); - nv_next(reg->ind().x(), reg->ind().y() + reg->dir, reg->ind().z()) = - 2. * n_b * v_b - nv(reg->ind().x(), reg->ind().y(), reg->ind().z()); + nv_next(reg->x, reg->y + reg->dir, reg->z) = + 2. * n_b * v_b - nv(reg->x, reg->y, reg->z); - momflux_next(reg->ind().x(), reg->ind().y() + reg->dir, reg->ind().z()) = - 2. * n_b * v_b * v_b - - momflux(reg->ind().x(), reg->ind().y(), reg->ind().z()); + momflux_next(reg->x, reg->y + reg->dir, reg->z) = + 2. * n_b * v_b * v_b - momflux(reg->x, reg->y, reg->z); } } From fa6663da34db0d3be4d1f6958824e07236c49afd Mon Sep 17 00:00:00 2001 From: Peter Hill Date: Wed, 23 Oct 2024 13:29:49 +0100 Subject: [PATCH 474/491] Fix clang-tidy const-correctness warnings --- examples/laplace-petsc3d/test-laplace3d.cxx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/laplace-petsc3d/test-laplace3d.cxx b/examples/laplace-petsc3d/test-laplace3d.cxx index 9f21a69be1..5e40b42495 100644 --- a/examples/laplace-petsc3d/test-laplace3d.cxx +++ b/examples/laplace-petsc3d/test-laplace3d.cxx @@ -136,7 +136,7 @@ int main(int argc, char** argv) { /////////////////////////////////////////////////////////////////////////////////////// // Calculate error /////////////////////////////////////////////////////////////////////////////////////// - auto& g_22 = mesh->getCoordinates()->g_22(); + const auto& g_22 = mesh->getCoordinates()->g_22(); Field3D rhs_check = D * this_Laplace_perp(f) + (Grad(f) * Grad(C2) - DDY(C2) * DDY(f) / g_22) / C1 + A * f; // The usual way to do this would be From 5b61d2b6d6796315ee6d3c568d0f4092a6f464e9 Mon Sep 17 00:00:00 2001 From: Peter Hill Date: Wed, 23 Oct 2024 18:04:38 +0100 Subject: [PATCH 475/491] Rename metric tensor component members --- include/bout/christoffel_symbols.hxx | 82 +++++++++---------- include/bout/g_values.hxx | 14 ++-- include/bout/metric_tensor.hxx | 78 +++++++++--------- src/mesh/christoffel_symbols.cxx | 117 ++++++++++++++------------- src/mesh/g_values.cxx | 10 +-- src/mesh/metric_tensor.cxx | 94 ++++++++++----------- 6 files changed, 201 insertions(+), 194 deletions(-) diff --git a/include/bout/christoffel_symbols.hxx b/include/bout/christoffel_symbols.hxx index a4db64d1ce..87d293616f 100644 --- a/include/bout/christoffel_symbols.hxx +++ b/include/bout/christoffel_symbols.hxx @@ -26,26 +26,26 @@ public: // ChristoffelSymbols(BoutReal g11, BoutReal g22, BoutReal g33, BoutReal g12, BoutReal g13, // BoutReal g23, Mesh* mesh); - const FieldMetric& G1_11() const { return G1_11_; } - const FieldMetric& G1_22() const { return G1_22_; } - const FieldMetric& G1_33() const { return G1_33_; } - const FieldMetric& G1_12() const { return G1_12_; } - const FieldMetric& G1_13() const { return G1_13_; } - const FieldMetric& G1_23() const { return G1_23_; } - - const FieldMetric& G2_11() const { return G2_11_; } - const FieldMetric& G2_22() const { return G2_22_; } - const FieldMetric& G2_33() const { return G2_33_; } - const FieldMetric& G2_12() const { return G2_12_; } - const FieldMetric& G2_13() const { return G2_13_; } - const FieldMetric& G2_23() const { return G2_23_; } - - const FieldMetric& G3_11() const { return G3_11_; } - const FieldMetric& G3_22() const { return G3_22_; } - const FieldMetric& G3_33() const { return G3_33_; } - const FieldMetric& G3_12() const { return G3_12_; } - const FieldMetric& G3_13() const { return G3_13_; } - const FieldMetric& G3_23() const { return G3_23_; } + const FieldMetric& G1_11() const { return G1_11_m; } + const FieldMetric& G1_22() const { return G1_22_m; } + const FieldMetric& G1_33() const { return G1_33_m; } + const FieldMetric& G1_12() const { return G1_12_m; } + const FieldMetric& G1_13() const { return G1_13_m; } + const FieldMetric& G1_23() const { return G1_23_m; } + + const FieldMetric& G2_11() const { return G2_11_m; } + const FieldMetric& G2_22() const { return G2_22_m; } + const FieldMetric& G2_33() const { return G2_33_m; } + const FieldMetric& G2_12() const { return G2_12_m; } + const FieldMetric& G2_13() const { return G2_13_m; } + const FieldMetric& G2_23() const { return G2_23_m; } + + const FieldMetric& G3_11() const { return G3_11_m; } + const FieldMetric& G3_22() const { return G3_22_m; } + const FieldMetric& G3_33() const { return G3_33_m; } + const FieldMetric& G3_12() const { return G3_12_m; } + const FieldMetric& G3_13() const { return G3_13_m; } + const FieldMetric& G3_23() const { return G3_23_m; } void setChristoffelSymbols(const FieldMetric& G1_11, const FieldMetric& G1_22, const FieldMetric& G1_33, const FieldMetric& G1_12, @@ -56,24 +56,24 @@ public: const FieldMetric& G3_11, const FieldMetric& G3_22, const FieldMetric& G3_33, const FieldMetric& G3_12, const FieldMetric& G3_13, const FieldMetric& G3_23) { - G1_11_ = G1_11; - G1_22_ = G1_22; - G1_33_ = G1_33; - G1_12_ = G1_12; - G1_13_ = G1_13; - G1_23_ = G1_23; - G2_11_ = G2_11; - G2_22_ = G2_22; - G2_33_ = G2_33; - G2_12_ = G2_12; - G2_13_ = G2_13; - G2_23_ = G2_23; - G3_11_ = G3_11; - G3_22_ = G3_22; - G3_33_ = G3_33; - G3_12_ = G3_12; - G3_13_ = G3_13; - G3_23_ = G3_23; + G1_11_m = G1_11; + G1_22_m = G1_22; + G1_33_m = G1_33; + G1_12_m = G1_12; + G1_13_m = G1_13; + G1_23_m = G1_23; + G2_11_m = G2_11; + G2_22_m = G2_22; + G2_33_m = G2_33; + G2_12_m = G2_12; + G2_13_m = G2_13; + G2_23_m = G2_23; + G3_11_m = G3_11; + G3_22_m = G3_22; + G3_33_m = G3_33; + G3_12_m = G3_12; + G3_13_m = G3_13; + G3_23_m = G3_23; } // void Allocate(); @@ -87,9 +87,9 @@ public: void communicate(Mesh* mesh); private: - FieldMetric G1_11_, G1_22_, G1_33_, G1_12_, G1_13_, G1_23_; - FieldMetric G2_11_, G2_22_, G2_33_, G2_12_, G2_13_, G2_23_; - FieldMetric G3_11_, G3_22_, G3_33_, G3_12_, G3_13_, G3_23_; + FieldMetric G1_11_m, G1_22_m, G1_33_m, G1_12_m, G1_13_m, G1_23_m; + FieldMetric G2_11_m, G2_22_m, G2_33_m, G2_12_m, G2_13_m, G2_23_m; + FieldMetric G3_11_m, G3_22_m, G3_33_m, G3_12_m, G3_13_m, G3_23_m; }; #endif //BOUT_CHRISTOFFELSYMBOLS_HXX diff --git a/include/bout/g_values.hxx b/include/bout/g_values.hxx index 1f4f20ab4a..0fbb25950d 100644 --- a/include/bout/g_values.hxx +++ b/include/bout/g_values.hxx @@ -14,18 +14,18 @@ public: explicit GValues(const Coordinates& coordinates); - const FieldMetric& G1() const { return G1_; } - const FieldMetric& G2() const { return G2_; } - const FieldMetric& G3() const { return G3_; } + const FieldMetric& G1() const { return G1_m; } + const FieldMetric& G2() const { return G2_m; } + const FieldMetric& G3() const { return G3_m; } - void setG1(const FieldMetric& G1) { G1_ = G1; } - void setG2(const FieldMetric& G2) { G2_ = G2; } - void setG3(const FieldMetric& G3) { G3_ = G3; } + void setG1(const FieldMetric& G1) { G1_m = G1; } + void setG2(const FieldMetric& G2) { G2_m = G2; } + void setG3(const FieldMetric& G3) { G3_m = G3; } void communicate(Mesh* mesh); private: - FieldMetric G1_, G2_, G3_; + FieldMetric G1_m, G2_m, G3_m; }; #endif //BOUT_GVALUES_HXX diff --git a/include/bout/metric_tensor.hxx b/include/bout/metric_tensor.hxx index c7dc85a105..aaa3d43ee6 100644 --- a/include/bout/metric_tensor.hxx +++ b/include/bout/metric_tensor.hxx @@ -25,53 +25,53 @@ public: // check that tensors are positive (if expected) and finite (always) void check(int ystart); - const FieldMetric& g11() const { return g11_; } - const FieldMetric& g22() const { return g22_; } - const FieldMetric& g33() const { return g33_; } - const FieldMetric& g12() const { return g12_; } - const FieldMetric& g13() const { return g13_; } - const FieldMetric& g23() const { return g23_; } - - const BoutReal& g11(int x, int y, int z) const { return g11_(x, y, z); } - const BoutReal& g22(int x, int y, int z) const { return g22_(x, y, z); } - const BoutReal& g33(int x, int y, int z) const { return g33_(x, y, z); } - const BoutReal& g12(int x, int y, int z) const { return g12_(x, y, z); } - const BoutReal& g13(int x, int y, int z) const { return g13_(x, y, z); } - const BoutReal& g23(int x, int y, int z) const { return g23_(x, y, z); } + const FieldMetric& g11() const { return g11_m; } + const FieldMetric& g22() const { return g22_m; } + const FieldMetric& g33() const { return g33_m; } + const FieldMetric& g12() const { return g12_m; } + const FieldMetric& g13() const { return g13_m; } + const FieldMetric& g23() const { return g23_m; } + + const BoutReal& g11(int x, int y, int z) const { return g11_m(x, y, z); } + const BoutReal& g22(int x, int y, int z) const { return g22_m(x, y, z); } + const BoutReal& g33(int x, int y, int z) const { return g33_m(x, y, z); } + const BoutReal& g12(int x, int y, int z) const { return g12_m(x, y, z); } + const BoutReal& g13(int x, int y, int z) const { return g13_m(x, y, z); } + const BoutReal& g23(int x, int y, int z) const { return g23_m(x, y, z); } #if BOUT_USE_METRIC_3D - const BoutReal* g11(int x, int y) const { return g11_(x, y); } - const BoutReal* g22(int x, int y) const { return g22_(x, y); } - const BoutReal* g33(int x, int y) const { return g33_(x, y); } - const BoutReal* g12(int x, int y) const { return g12_(x, y); } - const BoutReal* g13(int x, int y) const { return g13_(x, y); } - const BoutReal* g23(int x, int y) const { return g23_(x, y); } + const BoutReal* g11(int x, int y) const { return g11_m(x, y); } + const BoutReal* g22(int x, int y) const { return g22_m(x, y); } + const BoutReal* g33(int x, int y) const { return g33_m(x, y); } + const BoutReal* g12(int x, int y) const { return g12_m(x, y); } + const BoutReal* g13(int x, int y) const { return g13_m(x, y); } + const BoutReal* g23(int x, int y) const { return g23_m(x, y); } #else - const BoutReal& g11(int x, int y) const { return g11_(x, y); } - const BoutReal& g22(int x, int y) const { return g22_(x, y); } - const BoutReal& g33(int x, int y) const { return g33_(x, y); } - const BoutReal& g12(int x, int y) const { return g12_(x, y); } - const BoutReal& g13(int x, int y) const { return g13_(x, y); } - const BoutReal& g23(int x, int y) const { return g23_(x, y); } + const BoutReal& g11(int x, int y) const { return g11_m(x, y); } + const BoutReal& g22(int x, int y) const { return g22_m(x, y); } + const BoutReal& g33(int x, int y) const { return g33_m(x, y); } + const BoutReal& g12(int x, int y) const { return g12_m(x, y); } + const BoutReal& g13(int x, int y) const { return g13_m(x, y); } + const BoutReal& g23(int x, int y) const { return g23_m(x, y); } #endif void setMetricTensor(const MetricTensor& metric_tensor) { - g11_ = metric_tensor.g11(); - g22_ = metric_tensor.g22(); - g33_ = metric_tensor.g33(); - g12_ = metric_tensor.g12(); - g13_ = metric_tensor.g13(); - g23_ = metric_tensor.g23(); + g11_m = metric_tensor.g11(); + g22_m = metric_tensor.g22(); + g33_m = metric_tensor.g33(); + g12_m = metric_tensor.g12(); + g13_m = metric_tensor.g13(); + g23_m = metric_tensor.g23(); } void setLocation(const CELL_LOC location) { - g11_.setLocation(location); - g22_.setLocation(location); - g33_.setLocation(location); - g12_.setLocation(location); - g13_.setLocation(location); - g23_.setLocation(location); + g11_m.setLocation(location); + g22_m.setLocation(location); + g33_m.setLocation(location); + g12_m.setLocation(location); + g13_m.setLocation(location); + g23_m.setLocation(location); } MetricTensor inverse(const std::string& region = "RGN_ALL"); @@ -84,8 +84,8 @@ public: void communicate(Mesh* mesh); -protected: - FieldMetric g11_, g22_, g33_, g12_, g13_, g23_; +private: + FieldMetric g11_m, g22_m, g33_m, g12_m, g13_m, g23_m; }; class CovariantMetricTensor : public MetricTensor { diff --git a/src/mesh/christoffel_symbols.cxx b/src/mesh/christoffel_symbols.cxx index 5e21af8c87..20e86a0a0f 100644 --- a/src/mesh/christoffel_symbols.cxx +++ b/src/mesh/christoffel_symbols.cxx @@ -10,12 +10,12 @@ ChristoffelSymbols::ChristoffelSymbols( FieldMetric G2_33, FieldMetric G2_12, FieldMetric G2_13, FieldMetric G2_23, FieldMetric G3_11, FieldMetric G3_22, FieldMetric G3_33, FieldMetric G3_12, FieldMetric G3_13, FieldMetric G3_23) - : G1_11_(std::move(G1_11)), G1_22_(std::move(G1_22)), G1_33_(std::move(G1_33)), - G1_12_(std::move(G1_12)), G1_13_(std::move(G1_13)), G1_23_(std::move(G1_23)), - G2_11_(std::move(G2_11)), G2_22_(std::move(G2_22)), G2_33_(std::move(G2_33)), - G2_12_(std::move(G2_12)), G2_13_(std::move(G2_13)), G2_23_(std::move(G2_23)), - G3_11_(std::move(G3_11)), G3_22_(std::move(G3_22)), G3_33_(std::move(G3_33)), - G3_12_(std::move(G3_12)), G3_13_(std::move(G3_13)), G3_23_(std::move(G3_23)){}; + : G1_11_m(std::move(G1_11)), G1_22_m(std::move(G1_22)), G1_33_m(std::move(G1_33)), + G1_12_m(std::move(G1_12)), G1_13_m(std::move(G1_13)), G1_23_m(std::move(G1_23)), + G2_11_m(std::move(G2_11)), G2_22_m(std::move(G2_22)), G2_33_m(std::move(G2_33)), + G2_12_m(std::move(G2_12)), G2_13_m(std::move(G2_13)), G2_23_m(std::move(G2_23)), + G3_11_m(std::move(G3_11)), G3_22_m(std::move(G3_22)), G3_33_m(std::move(G3_33)), + G3_12_m(std::move(G3_12)), G3_13_m(std::move(G3_13)), G3_23_m(std::move(G3_23)) {}; ChristoffelSymbols::ChristoffelSymbols(Coordinates& coordinates) { // Calculate Christoffel symbol terms (18 independent values) @@ -41,24 +41,25 @@ ChristoffelSymbols::ChristoffelSymbols(Coordinates& coordinates) { const auto& g_13 = covariantMetricTensor.g13(); const auto& g_23 = covariantMetricTensor.g23(); - G1_11_ = 0.5 * g11 * coordinates.DDX(g_11) - + g12 * (coordinates.DDX(g_12) - 0.5 * coordinates.DDY(g_11)) - + g13 * (coordinates.DDX(g_13) - 0.5 * coordinates.DDZ(g_11)); - G1_22_ = g11 * (coordinates.DDY(g_12) - 0.5 * coordinates.DDX(g_22)) - + 0.5 * g12 * coordinates.DDY(g_22) - + g13 * (coordinates.DDY(g_23) - 0.5 * coordinates.DDZ(g_22)); - G1_33_ = g11 * (coordinates.DDZ(g_13) - 0.5 * coordinates.DDX(g_33)) - + g12 * (coordinates.DDZ(g_23) - 0.5 * coordinates.DDY(g_33)) - + 0.5 * g13 * coordinates.DDZ(g_33); - G1_12_ = + G1_11_m = 0.5 * g11 * coordinates.DDX(g_11) + + g12 * (coordinates.DDX(g_12) - 0.5 * coordinates.DDY(g_11)) + + g13 * (coordinates.DDX(g_13) - 0.5 * coordinates.DDZ(g_11)); + G1_22_m = g11 * (coordinates.DDY(g_12) - 0.5 * coordinates.DDX(g_22)) + + 0.5 * g12 * coordinates.DDY(g_22) + + g13 * (coordinates.DDY(g_23) - 0.5 * coordinates.DDZ(g_22)); + G1_33_m = g11 * (coordinates.DDZ(g_13) - 0.5 * coordinates.DDX(g_33)) + + g12 * (coordinates.DDZ(g_23) - 0.5 * coordinates.DDY(g_33)) + + 0.5 * g13 * coordinates.DDZ(g_33); + G1_12_m = 0.5 * g11 * coordinates.DDY(g_11) + 0.5 * g12 * coordinates.DDX(g_22) + 0.5 * g13 * (coordinates.DDY(g_13) + coordinates.DDX(g_23) - coordinates.DDZ(g_12)); - G1_13_ = 0.5 * g11 * coordinates.DDZ(g_11) - + 0.5 * g12 - * (coordinates.DDZ(g_12) + coordinates.DDX(g_23) - coordinates.DDY(g_13)) - + 0.5 * g13 * coordinates.DDX(g_33); - G1_23_ = + G1_13_m = + 0.5 * g11 * coordinates.DDZ(g_11) + + 0.5 * g12 + * (coordinates.DDZ(g_12) + coordinates.DDX(g_23) - coordinates.DDY(g_13)) + + 0.5 * g13 * coordinates.DDX(g_33); + G1_23_m = 0.5 * g11 * (coordinates.DDZ(g_12) + coordinates.DDY(g_13) - coordinates.DDX(g_23)) + 0.5 * g12 * (coordinates.DDZ(g_22) + coordinates.DDY(g_23) - coordinates.DDY(g_23)) @@ -66,20 +67,20 @@ ChristoffelSymbols::ChristoffelSymbols(Coordinates& coordinates) { // which equals + 0.5 * g13 * coordinates.DDY(g_33); - G2_11_ = 0.5 * g12 * coordinates.DDX(g_11) - + g22 * (coordinates.DDX(g_12) - 0.5 * coordinates.DDY(g_11)) - + g23 * (coordinates.DDX(g_13) - 0.5 * coordinates.DDZ(g_11)); - G2_22_ = g12 * (coordinates.DDY(g_12) - 0.5 * coordinates.DDX(g_22)) - + 0.5 * g22 * coordinates.DDY(g_22) - + g23 * (coordinates.DDY(g23) - 0.5 * coordinates.DDZ(g_22)); - G2_33_ = g12 * (coordinates.DDZ(g_13) - 0.5 * coordinates.DDX(g_33)) - + g22 * (coordinates.DDZ(g_23) - 0.5 * coordinates.DDY(g_33)) - + 0.5 * g23 * coordinates.DDZ(g_33); - G2_12_ = + G2_11_m = 0.5 * g12 * coordinates.DDX(g_11) + + g22 * (coordinates.DDX(g_12) - 0.5 * coordinates.DDY(g_11)) + + g23 * (coordinates.DDX(g_13) - 0.5 * coordinates.DDZ(g_11)); + G2_22_m = g12 * (coordinates.DDY(g_12) - 0.5 * coordinates.DDX(g_22)) + + 0.5 * g22 * coordinates.DDY(g_22) + + g23 * (coordinates.DDY(g23) - 0.5 * coordinates.DDZ(g_22)); + G2_33_m = g12 * (coordinates.DDZ(g_13) - 0.5 * coordinates.DDX(g_33)) + + g22 * (coordinates.DDZ(g_23) - 0.5 * coordinates.DDY(g_33)) + + 0.5 * g23 * coordinates.DDZ(g_33); + G2_12_m = 0.5 * g12 * coordinates.DDY(g_11) + 0.5 * g22 * coordinates.DDX(g_22) + 0.5 * g23 * (coordinates.DDY(g_13) + coordinates.DDX(g_23) - coordinates.DDZ(g_12)); - G2_13_ = + G2_13_m = // 0.5 *g21*(coordinates.DDZ(g_11) + coordinates.DDX(covariantMetricTensor.Getg13()) - coordinates.DDX(g_13)) // which equals 0.5 * g12 * (coordinates.DDZ(g_11) + coordinates.DDX(g_13) - coordinates.DDX(g_13)) @@ -90,20 +91,20 @@ ChristoffelSymbols::ChristoffelSymbols(Coordinates& coordinates) { // + 0.5 *g23*(coordinates.DDZ(covariantMetricTensor.Getg31()) + coordinates.DDX(g_33) - coordinates.DDZ(g_13)); // which equals + 0.5 * g23 * coordinates.DDX(g_33); - G2_23_ = + G2_23_m = 0.5 * g12 * (coordinates.DDZ(g_12) + coordinates.DDY(g_13) - coordinates.DDX(g_23)) + 0.5 * g22 * coordinates.DDZ(g_22) + 0.5 * g23 * coordinates.DDY(g_33); - G3_11_ = 0.5 * g13 * coordinates.DDX(g_11) - + g23 * (coordinates.DDX(g_12) - 0.5 * coordinates.DDY(g_11)) - + g33 * (coordinates.DDX(g_13) - 0.5 * coordinates.DDZ(g_11)); - G3_22_ = g13 * (coordinates.DDY(g_12) - 0.5 * coordinates.DDX(g_22)) - + 0.5 * g23 * coordinates.DDY(g_22) - + g33 * (coordinates.DDY(g_23) - 0.5 * coordinates.DDZ(g_22)); - G3_33_ = g13 * (coordinates.DDZ(g_13) - 0.5 * coordinates.DDX(g_33)) - + g23 * (coordinates.DDZ(g_23) - 0.5 * coordinates.DDY(g_33)) - + 0.5 * g33 * coordinates.DDZ(g_33); - G3_12_ = + G3_11_m = 0.5 * g13 * coordinates.DDX(g_11) + + g23 * (coordinates.DDX(g_12) - 0.5 * coordinates.DDY(g_11)) + + g33 * (coordinates.DDX(g_13) - 0.5 * coordinates.DDZ(g_11)); + G3_22_m = g13 * (coordinates.DDY(g_12) - 0.5 * coordinates.DDX(g_22)) + + 0.5 * g23 * coordinates.DDY(g_22) + + g33 * (coordinates.DDY(g_23) - 0.5 * coordinates.DDZ(g_22)); + G3_33_m = g13 * (coordinates.DDZ(g_13) - 0.5 * coordinates.DDX(g_33)) + + g23 * (coordinates.DDZ(g_23) - 0.5 * coordinates.DDY(g_33)) + + 0.5 * g33 * coordinates.DDZ(g_33); + G3_12_m = // 0.5 *g31*(coordinates.DDY(g_11) + coordinates.DDX(covariantMetricTensor.Getg12()) - coordinates.DDX(g_12)) // which equals to 0.5 * g13 * coordinates.DDY(g_11) @@ -114,25 +115,27 @@ ChristoffelSymbols::ChristoffelSymbols(Coordinates& coordinates) { // which equals to + 0.5 * g33 * (coordinates.DDY(g_13)) + coordinates.DDX(g_23) - coordinates.DDZ(g_12); - G3_13_ = 0.5 * g13 * coordinates.DDZ(g_11) - + 0.5 * g23 - * (coordinates.DDZ(g_12) + coordinates.DDX(g_23) - coordinates.DDY(g_13)) - + 0.5 * g33 * coordinates.DDX(g_33); - G3_23_ = 0.5 * g13 * (coordinates.DDZ(g_12) + coordinates.DDY(g_13)) - - coordinates.DDX(g_23) + 0.5 * g23 * coordinates.DDZ(g_22) - + 0.5 * g33 * coordinates.DDY(g_33); + G3_13_m = + 0.5 * g13 * coordinates.DDZ(g_11) + + 0.5 * g23 + * (coordinates.DDZ(g_12) + coordinates.DDX(g_23) - coordinates.DDY(g_13)) + + 0.5 * g33 * coordinates.DDX(g_33); + G3_23_m = 0.5 * g13 * (coordinates.DDZ(g_12) + coordinates.DDY(g_13)) + - coordinates.DDX(g_23) + 0.5 * g23 * coordinates.DDZ(g_22) + + 0.5 * g33 * coordinates.DDY(g_33); } void ChristoffelSymbols::applyToComponents( const std::function& function) { - const auto components_in = std::vector{ - G1_11_, G1_22_, G1_33_, G1_12_, G1_13_, G1_23_, G2_11_, G2_22_, G2_33_, - G2_12_, G2_13_, G2_23_, G3_11_, G3_22_, G3_33_, G3_12_, G3_13_, G3_23_}; + const auto components_in = std::array{ + G1_11_m, G1_22_m, G1_33_m, G1_12_m, G1_13_m, G1_23_m, G2_11_m, G2_22_m, G2_33_m, + G2_12_m, G2_13_m, G2_23_m, G3_11_m, G3_22_m, G3_33_m, G3_12_m, G3_13_m, G3_23_m}; FieldMetric components_out[18]; - std::transform(components_in.begin(), components_in.end(), components_out, function); + std::transform(components_in.begin(), components_in.end(), components_out.begin(), + function); auto [G1_11, G1_22, G1_33, G1_12, G1_13, G1_23, G2_11, G2_22, G2_33, G2_12, G2_13, G2_23, G3_11, G3_22, G3_33, G3_12, G3_13, G3_23] = components_out; @@ -144,7 +147,7 @@ void ChristoffelSymbols::communicate(Mesh* mesh) { output_progress.write("\tCommunicating connection terms\n"); - mesh->communicate(G1_11_, G1_22_, G1_33_, G1_12_, G1_13_, G1_23_, G2_11_, G2_22_, - G2_33_, G2_12_, G2_13_, G2_23_, G3_11_, G3_22_, G3_33_, G3_12_, - G3_13_, G3_23_); + mesh->communicate(G1_11_m, G1_22_m, G1_33_m, G1_12_m, G1_13_m, G1_23_m, G2_11_m, + G2_22_m, G2_33_m, G2_12_m, G2_13_m, G2_23_m, G3_11_m, G3_22_m, + G3_33_m, G3_12_m, G3_13_m, G3_23_m); } diff --git a/src/mesh/g_values.cxx b/src/mesh/g_values.cxx index 8b23363f0f..2b52ca9a68 100644 --- a/src/mesh/g_values.cxx +++ b/src/mesh/g_values.cxx @@ -4,7 +4,7 @@ #include "bout/mesh.hxx" GValues::GValues(FieldMetric G1, FieldMetric G2, FieldMetric G3) - : G1_(std::move(G1)), G2_(std::move(G2)), G3_(std::move(G3)){}; + : G1_m(std::move(G1)), G2_m(std::move(G2)), G3_m(std::move(G3)) {}; GValues::GValues(const Coordinates& coordinates) { @@ -20,13 +20,13 @@ GValues::GValues(const Coordinates& coordinates) { auto tmp = J * g12; Coordinates::communicate(tmp); - setG1((coordinates.DDX(J * g11) + coordinates.DDY(tmp) + coordinates.DDZ(J * g13)) / J); + G1_m = (coordinates.DDX(J * g11) + coordinates.DDY(tmp) + coordinates.DDZ(J * g13)) / J; tmp = J * g22; Coordinates::communicate(tmp); - setG2((coordinates.DDX(J * g12) + coordinates.DDY(tmp) + coordinates.DDZ(J * g23)) / J); + G2_m = (coordinates.DDX(J * g12) + coordinates.DDY(tmp) + coordinates.DDZ(J * g23)) / J; tmp = J * g23; Coordinates::communicate(tmp); - setG3((coordinates.DDX(J * g13) + coordinates.DDY(tmp) + coordinates.DDZ(J * g33)) / J); + G3_m = (coordinates.DDX(J * g13) + coordinates.DDY(tmp) + coordinates.DDZ(J * g33)) / J; } -void GValues::communicate(Mesh* mesh) { mesh->communicate(G1_, G2_, G3_); } +void GValues::communicate(Mesh* mesh) { mesh->communicate(G1_m, G2_m, G3_m); } diff --git a/src/mesh/metric_tensor.cxx b/src/mesh/metric_tensor.cxx index aa578f5486..c0883c7bad 100644 --- a/src/mesh/metric_tensor.cxx +++ b/src/mesh/metric_tensor.cxx @@ -1,4 +1,3 @@ - #include "bout/metric_tensor.hxx" #include "bout/mesh.hxx" #include "bout/output.hxx" @@ -6,61 +5,65 @@ MetricTensor::MetricTensor(FieldMetric g11, FieldMetric g22, FieldMetric g33, FieldMetric g12, FieldMetric g13, FieldMetric g23) - : g11_(std::move(g11)), g22_(std::move(g22)), g33_(std::move(g33)), - g12_(std::move(g12)), g13_(std::move(g13)), g23_(std::move(g23)) {} + : g11_m(std::move(g11)), g22_m(std::move(g22)), g33_m(std::move(g33)), + g12_m(std::move(g12)), g13_m(std::move(g13)), g23_m(std::move(g23)) {} MetricTensor::MetricTensor(const BoutReal g11, const BoutReal g22, const BoutReal g33, const BoutReal g12, const BoutReal g13, const BoutReal g23, Mesh* mesh) - : g11_(g11, mesh), g22_(g22, mesh), g33_(g33, mesh), g12_(g12, mesh), g13_(g13, mesh), - g23_(g23, mesh) {} + : g11_m(g11, mesh), g22_m(g22, mesh), g33_m(g33, mesh), g12_m(g12, mesh), + g13_m(g13, mesh), g23_m(g23, mesh) {} void MetricTensor::check(int ystart) { + const bool non_identity_parallel_transform = + g11_m.hasParallelSlices() && &g11_m.ynext(1) != &g11_m; + // Diagonal metric components should be finite - bout::checkFinite(g11_, "g11", "RGN_NOCORNERS"); - bout::checkFinite(g22_, "g22", "RGN_NOCORNERS"); - bout::checkFinite(g33_, "g33", "RGN_NOCORNERS"); - if (g11_.hasParallelSlices() && &g11_.ynext(1) != &g11_) { + bout::checkFinite(g11_m, "g11", "RGN_NOCORNERS"); + bout::checkFinite(g22_m, "g22", "RGN_NOCORNERS"); + bout::checkFinite(g33_m, "g33", "RGN_NOCORNERS"); + if (non_identity_parallel_transform) { for (int dy = 1; dy <= ystart; ++dy) { for (const auto sign : {1, -1}) { - bout::checkFinite(g11_.ynext(sign * dy), "g11.ynext", + bout::checkFinite(g11_m.ynext(sign * dy), "g11.ynext", fmt::format("RGN_YPAR_{:+d}", sign * dy)); - bout::checkFinite(g22_.ynext(sign * dy), "g22.ynext", + bout::checkFinite(g22_m.ynext(sign * dy), "g22.ynext", fmt::format("RGN_YPAR_{:+d}", sign * dy)); - bout::checkFinite(g33_.ynext(sign * dy), "g33.ynext", + bout::checkFinite(g33_m.ynext(sign * dy), "g33.ynext", fmt::format("RGN_YPAR_{:+d}", sign * dy)); } } } + // Diagonal metric components should be positive - bout::checkPositive(g11_, "g11", "RGN_NOCORNERS"); - bout::checkPositive(g22_, "g22", "RGN_NOCORNERS"); - bout::checkPositive(g33_, "g33", "RGN_NOCORNERS"); - if (g11_.hasParallelSlices() && &g11_.ynext(1) != &g11_) { + bout::checkPositive(g11_m, "g11", "RGN_NOCORNERS"); + bout::checkPositive(g22_m, "g22", "RGN_NOCORNERS"); + bout::checkPositive(g33_m, "g33", "RGN_NOCORNERS"); + if (non_identity_parallel_transform) { for (int dy = 1; dy <= ystart; ++dy) { for (const auto sign : {1, -1}) { - bout::checkPositive(g11_.ynext(sign * dy), "g11.ynext", + bout::checkPositive(g11_m.ynext(sign * dy), "g11.ynext", fmt::format("RGN_YPAR_{:+d}", sign * dy)); - bout::checkPositive(g22_.ynext(sign * dy), "g22.ynext", + bout::checkPositive(g22_m.ynext(sign * dy), "g22.ynext", fmt::format("RGN_YPAR_{:+d}", sign * dy)); - bout::checkPositive(g33_.ynext(sign * dy), "g33.ynext", + bout::checkPositive(g33_m.ynext(sign * dy), "g33.ynext", fmt::format("RGN_YPAR_{:+d}", sign * dy)); } } } // Off-diagonal metric components should be finite - bout::checkFinite(g12_, "g12", "RGN_NOCORNERS"); - bout::checkFinite(g13_, "g13", "RGN_NOCORNERS"); - bout::checkFinite(g23_, "g23", "RGN_NOCORNERS"); - if (g23_.hasParallelSlices() && &g23_.ynext(1) != &g23_) { + bout::checkFinite(g12_m, "g12", "RGN_NOCORNERS"); + bout::checkFinite(g13_m, "g13", "RGN_NOCORNERS"); + bout::checkFinite(g23_m, "g23", "RGN_NOCORNERS"); + if (non_identity_parallel_transform) { for (int dy = 1; dy <= ystart; ++dy) { for (const auto sign : {1, -1}) { - bout::checkFinite(g12_.ynext(sign * dy), "g12.ynext", + bout::checkFinite(g12_m.ynext(sign * dy), "g12.ynext", fmt::format("RGN_YPAR_{:+d}", sign * dy)); - bout::checkFinite(g13_.ynext(sign * dy), "g13.ynext", + bout::checkFinite(g13_m.ynext(sign * dy), "g13.ynext", fmt::format("RGN_YPAR_{:+d}", sign * dy)); - bout::checkFinite(g23_.ynext(sign * dy), "g23.ynext", + bout::checkFinite(g23_m.ynext(sign * dy), "g23.ynext", fmt::format("RGN_YPAR_{:+d}", sign * dy)); } } @@ -76,21 +79,21 @@ MetricTensor MetricTensor::inverse(const std::string& region) { auto a = Matrix(3, 3); - FieldMetric g_11 = emptyFrom(g11_); - FieldMetric g_22 = emptyFrom(g22_); - FieldMetric g_33 = emptyFrom(g33_); - FieldMetric g_12 = emptyFrom(g12_); - FieldMetric g_13 = emptyFrom(g13_); - FieldMetric g_23 = emptyFrom(g23_); + FieldMetric g_11 = emptyFrom(g11_m); + FieldMetric g_22 = emptyFrom(g22_m); + FieldMetric g_33 = emptyFrom(g33_m); + FieldMetric g_12 = emptyFrom(g12_m); + FieldMetric g_13 = emptyFrom(g13_m); + FieldMetric g_23 = emptyFrom(g23_m); - BOUT_FOR_SERIAL(i, g11_.getRegion(region)) { - a(0, 0) = g11_[i]; - a(1, 1) = g22_[i]; - a(2, 2) = g33_[i]; + BOUT_FOR_SERIAL(i, g11_m.getRegion(region)) { + a(0, 0) = g11_m[i]; + a(1, 1) = g22_m[i]; + a(2, 2) = g33_m[i]; - a(0, 1) = a(1, 0) = g12_[i]; - a(1, 2) = a(2, 1) = g23_[i]; - a(0, 2) = a(2, 0) = g13_[i]; + a(0, 1) = a(1, 0) = g12_m[i]; + a(1, 2) = a(2, 1) = g23_m[i]; + a(0, 2) = a(2, 0) = g13_m[i]; if (invert3x3(a)) { const auto error_message = "\tERROR: metric tensor is singular at ({:d}, {:d})\n"; @@ -120,7 +123,7 @@ MetricTensor MetricTensor::inverse(const std::string& region) { output_info.write("\tMaximum error in off-diagonal inversion is {:e}\n", maxerr); auto other_representation = MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23); - const auto location = g11_.getLocation(); + const auto location = g11_m.getLocation(); other_representation.setLocation(location); return other_representation; } @@ -130,15 +133,16 @@ void MetricTensor::map( const MetricTensor updated_metric_tensor = applyToComponents(function); - setMetricTensor(MetricTensor(updated_metric_tensor.g11_, updated_metric_tensor.g22_, - updated_metric_tensor.g33_, updated_metric_tensor.g12_, - updated_metric_tensor.g13_, updated_metric_tensor.g23_)); + setMetricTensor(MetricTensor(updated_metric_tensor.g11_m, updated_metric_tensor.g22_m, + updated_metric_tensor.g33_m, updated_metric_tensor.g12_m, + updated_metric_tensor.g13_m, updated_metric_tensor.g23_m)); } MetricTensor MetricTensor::applyToComponents( const std::function& function) const { - const auto components_in = std::vector{g11_, g22_, g33_, g12_, g13_, g23_}; + const auto components_in = + std::vector{g11_m, g22_m, g33_m, g12_m, g13_m, g23_m}; FieldMetric components_out[6]; @@ -149,5 +153,5 @@ MetricTensor MetricTensor::applyToComponents( } void MetricTensor::communicate(Mesh* mesh) { - mesh->communicate(g11_, g22_, g33_, g12_, g13_, g23_); + mesh->communicate(g11_m, g22_m, g33_m, g12_m, g13_m, g23_m); } From e2d3c52f5ea3952340840a1c0f4d2df906360293 Mon Sep 17 00:00:00 2001 From: Peter Hill Date: Wed, 23 Oct 2024 18:08:32 +0100 Subject: [PATCH 476/491] Use `std::array` instead of C array --- src/mesh/christoffel_symbols.cxx | 4 +++- src/mesh/metric_tensor.cxx | 11 +++++++---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/mesh/christoffel_symbols.cxx b/src/mesh/christoffel_symbols.cxx index 20e86a0a0f..462a0a8115 100644 --- a/src/mesh/christoffel_symbols.cxx +++ b/src/mesh/christoffel_symbols.cxx @@ -2,6 +2,8 @@ #include "bout/christoffel_symbols.hxx" #include "bout/coordinates.hxx" #include "bout/mesh.hxx" + +#include #include ChristoffelSymbols::ChristoffelSymbols( @@ -132,7 +134,7 @@ void ChristoffelSymbols::applyToComponents( G1_11_m, G1_22_m, G1_33_m, G1_12_m, G1_13_m, G1_23_m, G2_11_m, G2_22_m, G2_33_m, G2_12_m, G2_13_m, G2_23_m, G3_11_m, G3_22_m, G3_33_m, G3_12_m, G3_13_m, G3_23_m}; - FieldMetric components_out[18]; + std::array components_out; std::transform(components_in.begin(), components_in.end(), components_out.begin(), function); diff --git a/src/mesh/metric_tensor.cxx b/src/mesh/metric_tensor.cxx index c0883c7bad..b06a55590a 100644 --- a/src/mesh/metric_tensor.cxx +++ b/src/mesh/metric_tensor.cxx @@ -1,6 +1,8 @@ #include "bout/metric_tensor.hxx" #include "bout/mesh.hxx" #include "bout/output.hxx" + +#include #include MetricTensor::MetricTensor(FieldMetric g11, FieldMetric g22, FieldMetric g33, @@ -141,12 +143,13 @@ void MetricTensor::map( MetricTensor MetricTensor::applyToComponents( const std::function& function) const { - const auto components_in = - std::vector{g11_m, g22_m, g33_m, g12_m, g13_m, g23_m}; + const std::array components_in{g11_m, g22_m, g33_m, + g12_m, g13_m, g23_m}; - FieldMetric components_out[6]; + std::array components_out; - std::transform(components_in.begin(), components_in.end(), components_out, function); + std::transform(components_in.begin(), components_in.end(), components_out.begin(), + function); auto [g_11, g_22, g_33, g_12, g_13, g_23] = components_out; return MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23); From 687f0c4714d87439f53ed83237c0de9022e310a4 Mon Sep 17 00:00:00 2001 From: Peter Hill Date: Wed, 23 Oct 2024 18:16:56 +0100 Subject: [PATCH 477/491] Fix some clang-tidy warnings in metric_tensor --- include/bout/metric_tensor.hxx | 4 ++ src/mesh/metric_tensor.cxx | 69 ++++++++++++++++++++-------------- 2 files changed, 44 insertions(+), 29 deletions(-) diff --git a/include/bout/metric_tensor.hxx b/include/bout/metric_tensor.hxx index aaa3d43ee6..038f48ed37 100644 --- a/include/bout/metric_tensor.hxx +++ b/include/bout/metric_tensor.hxx @@ -2,10 +2,14 @@ #ifndef BOUT_METRIC_TENSOR_HXX #define BOUT_METRIC_TENSOR_HXX +#include "bout/build_defines.hxx" #include "bout/field2d.hxx" #include "bout/field3d.hxx" #include + #include +#include +#include class MetricTensor { diff --git a/src/mesh/metric_tensor.cxx b/src/mesh/metric_tensor.cxx index b06a55590a..be149e7b19 100644 --- a/src/mesh/metric_tensor.cxx +++ b/src/mesh/metric_tensor.cxx @@ -1,10 +1,20 @@ #include "bout/metric_tensor.hxx" +#include "fmt/core.h" +#include "bout/bout_types.hxx" +#include "bout/boutexception.hxx" +#include "bout/field2d.hxx" #include "bout/mesh.hxx" #include "bout/output.hxx" +#include "bout/region.hxx" +#include "bout/utils.hxx" +#include #include +#include +#include #include + MetricTensor::MetricTensor(FieldMetric g11, FieldMetric g22, FieldMetric g33, FieldMetric g12, FieldMetric g13, FieldMetric g23) : g11_m(std::move(g11)), g22_m(std::move(g22)), g33_m(std::move(g33)), @@ -77,9 +87,7 @@ MetricTensor MetricTensor::inverse(const std::string& region) { TRACE("MetricTensor::inverse"); // Perform inversion of g{ij} to get g^{ij}, or vice versa - // NOTE: Currently this bit assumes that metric terms are Field2D objects - - auto a = Matrix(3, 3); + auto matrix = Matrix(3, 3); FieldMetric g_11 = emptyFrom(g11_m); FieldMetric g_22 = emptyFrom(g22_m); @@ -89,40 +97,43 @@ MetricTensor MetricTensor::inverse(const std::string& region) { FieldMetric g_23 = emptyFrom(g23_m); BOUT_FOR_SERIAL(i, g11_m.getRegion(region)) { - a(0, 0) = g11_m[i]; - a(1, 1) = g22_m[i]; - a(2, 2) = g33_m[i]; - - a(0, 1) = a(1, 0) = g12_m[i]; - a(1, 2) = a(2, 1) = g23_m[i]; - a(0, 2) = a(2, 0) = g13_m[i]; - - if (invert3x3(a)) { - const auto error_message = "\tERROR: metric tensor is singular at ({:d}, {:d})\n"; - output_error.write(error_message, i.x(), i.y()); + matrix(0, 0) = g11_m[i]; + matrix(1, 1) = g22_m[i]; + matrix(2, 2) = g33_m[i]; + + matrix(0, 1) = matrix(1, 0) = g12_m[i]; + matrix(1, 2) = matrix(2, 1) = g23_m[i]; + matrix(0, 2) = matrix(2, 0) = g13_m[i]; + + if (invert3x3(matrix) != 0) { + const auto error_message = fmt::format( + "\tERROR: metric tensor is singular at ({:d}, {:d})\n", i.x(), i.y()); + output_error.write(error_message); throw BoutException(error_message); } - g_11[i] = a(0, 0); - g_22[i] = a(1, 1); - g_33[i] = a(2, 2); - g_12[i] = a(0, 1); - g_13[i] = a(0, 2); - g_23[i] = a(1, 2); + g_11[i] = matrix(0, 0); + g_22[i] = matrix(1, 1); + g_33[i] = matrix(2, 2); + g_12[i] = matrix(0, 1); + g_13[i] = matrix(0, 2); + g_23[i] = matrix(1, 2); } - BoutReal maxerr; - maxerr = BOUTMAX(max(abs((g_11 * g_11 + g_12 * g_12 + g_13 * g_13) - 1)), - max(abs((g_12 * g_12 + g_22 * g_22 + g_23 * g_23) - 1)), - max(abs((g_13 * g_13 + g_23 * g_23 + g_33 * g_33) - 1))); + const BoutReal diagonal_maxerr = + BOUTMAX(max(abs((g_11 * g_11 + g_12 * g_12 + g_13 * g_13) - 1)), + max(abs((g_12 * g_12 + g_22 * g_22 + g_23 * g_23) - 1)), + max(abs((g_13 * g_13 + g_23 * g_23 + g_33 * g_33) - 1))); - output_info.write("\tMaximum error in diagonal inversion is {:e}\n", maxerr); + output_info.write("\tMaximum error in diagonal inversion is {:e}\n", diagonal_maxerr); - maxerr = BOUTMAX(max(abs(g_11 * g_12 + g_12 * g_22 + g_13 * g_23)), - max(abs(g_11 * g_13 + g_12 * g_23 + g_13 * g_33)), - max(abs(g_12 * g_13 + g_22 * g_23 + g_23 * g_33))); + const BoutReal off_diagonal_maxerr = + BOUTMAX(max(abs(g_11 * g_12 + g_12 * g_22 + g_13 * g_23)), + max(abs(g_11 * g_13 + g_12 * g_23 + g_13 * g_33)), + max(abs(g_12 * g_13 + g_22 * g_23 + g_23 * g_33))); - output_info.write("\tMaximum error in off-diagonal inversion is {:e}\n", maxerr); + output_info.write("\tMaximum error in off-diagonal inversion is {:e}\n", + off_diagonal_maxerr); auto other_representation = MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23); const auto location = g11_m.getLocation(); From 2411ab20bb0734b952f1fca155f802c4ab69fe51 Mon Sep 17 00:00:00 2001 From: Peter Hill Date: Wed, 23 Oct 2024 18:21:54 +0100 Subject: [PATCH 478/491] Reduce duplication in metric tensor between 2D/3D metric a little --- include/bout/metric_tensor.hxx | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/include/bout/metric_tensor.hxx b/include/bout/metric_tensor.hxx index 038f48ed37..795e51d65a 100644 --- a/include/bout/metric_tensor.hxx +++ b/include/bout/metric_tensor.hxx @@ -16,8 +16,10 @@ class MetricTensor { public: #if BOUT_USE_METRIC_3D using FieldMetric = Field3D; + using Metric2DSlice = const BoutReal*; #else using FieldMetric = Field2D; + using Metric2DSlice = const BoutReal&; #endif MetricTensor(FieldMetric g11, FieldMetric g22, FieldMetric g33, FieldMetric g12, @@ -43,21 +45,12 @@ public: const BoutReal& g13(int x, int y, int z) const { return g13_m(x, y, z); } const BoutReal& g23(int x, int y, int z) const { return g23_m(x, y, z); } -#if BOUT_USE_METRIC_3D - const BoutReal* g11(int x, int y) const { return g11_m(x, y); } - const BoutReal* g22(int x, int y) const { return g22_m(x, y); } - const BoutReal* g33(int x, int y) const { return g33_m(x, y); } - const BoutReal* g12(int x, int y) const { return g12_m(x, y); } - const BoutReal* g13(int x, int y) const { return g13_m(x, y); } - const BoutReal* g23(int x, int y) const { return g23_m(x, y); } -#else - const BoutReal& g11(int x, int y) const { return g11_m(x, y); } - const BoutReal& g22(int x, int y) const { return g22_m(x, y); } - const BoutReal& g33(int x, int y) const { return g33_m(x, y); } - const BoutReal& g12(int x, int y) const { return g12_m(x, y); } - const BoutReal& g13(int x, int y) const { return g13_m(x, y); } - const BoutReal& g23(int x, int y) const { return g23_m(x, y); } -#endif + Metric2DSlice g11(int x, int y) const { return g11_m(x, y); } + Metric2DSlice g22(int x, int y) const { return g22_m(x, y); } + Metric2DSlice g33(int x, int y) const { return g33_m(x, y); } + Metric2DSlice g12(int x, int y) const { return g12_m(x, y); } + Metric2DSlice g13(int x, int y) const { return g13_m(x, y); } + Metric2DSlice g23(int x, int y) const { return g23_m(x, y); } void setMetricTensor(const MetricTensor& metric_tensor) { From 12130353b80ca6d78b6eefea4a7f9271261a57f2 Mon Sep 17 00:00:00 2001 From: Peter Hill Date: Wed, 23 Oct 2024 18:55:59 +0100 Subject: [PATCH 479/491] Inline metric tensor function application `map` --- include/bout/christoffel_symbols.hxx | 56 +++++++++++----------------- include/bout/coordinates.hxx | 9 ----- include/bout/metric_tensor.hxx | 14 ++++--- src/mesh/christoffel_symbols.cxx | 19 ---------- src/mesh/coordinates.cxx | 26 +++---------- src/mesh/metric_tensor.cxx | 28 -------------- 6 files changed, 35 insertions(+), 117 deletions(-) diff --git a/include/bout/christoffel_symbols.hxx b/include/bout/christoffel_symbols.hxx index 87d293616f..7b6a9033a9 100644 --- a/include/bout/christoffel_symbols.hxx +++ b/include/bout/christoffel_symbols.hxx @@ -47,42 +47,28 @@ public: const FieldMetric& G3_13() const { return G3_13_m; } const FieldMetric& G3_23() const { return G3_23_m; } - void setChristoffelSymbols(const FieldMetric& G1_11, const FieldMetric& G1_22, - const FieldMetric& G1_33, const FieldMetric& G1_12, - const FieldMetric& G1_13, const FieldMetric& G1_23, - const FieldMetric& G2_11, const FieldMetric& G2_22, - const FieldMetric& G2_33, const FieldMetric& G2_12, - const FieldMetric& G2_13, const FieldMetric& G2_23, - const FieldMetric& G3_11, const FieldMetric& G3_22, - const FieldMetric& G3_33, const FieldMetric& G3_12, - const FieldMetric& G3_13, const FieldMetric& G3_23) { - G1_11_m = G1_11; - G1_22_m = G1_22; - G1_33_m = G1_33; - G1_12_m = G1_12; - G1_13_m = G1_13; - G1_23_m = G1_23; - G2_11_m = G2_11; - G2_22_m = G2_22; - G2_33_m = G2_33; - G2_12_m = G2_12; - G2_13_m = G2_13; - G2_23_m = G2_23; - G3_11_m = G3_11; - G3_22_m = G3_22; - G3_33_m = G3_33; - G3_12_m = G3_12; - G3_13_m = G3_13; - G3_23_m = G3_23; - } - - // void Allocate(); - // - // void setLocation(CELL_LOC location); - // Transforms the ChristoffelSymbols by applying the given function to every element - void - applyToComponents(const std::function& function); + template + void map(F function) { + G1_11_m = function(G1_11_m); + G1_22_m = function(G1_22_m); + G1_33_m = function(G1_33_m); + G1_12_m = function(G1_12_m); + G1_13_m = function(G1_13_m); + G1_23_m = function(G1_23_m); + G2_11_m = function(G2_11_m); + G2_22_m = function(G2_22_m); + G2_33_m = function(G2_33_m); + G2_12_m = function(G2_12_m); + G2_13_m = function(G2_13_m); + G2_23_m = function(G2_23_m); + G3_11_m = function(G3_11_m); + G3_22_m = function(G3_22_m); + G3_33_m = function(G3_33_m); + G3_12_m = function(G3_12_m); + G3_13_m = function(G3_13_m); + G3_23_m = function(G3_23_m); + } void communicate(Mesh* mesh); diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index a190f43c53..3cca83746b 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -464,15 +464,6 @@ private: /// `g_values` needs renaming, when we know what the name should be mutable std::unique_ptr g_values_cache{nullptr}; - void applyToContravariantMetricTensor( - const std::function& function); - - void applyToCovariantMetricTensor( - const std::function& function); - - void applyToChristoffelSymbols( - const std::function& function); - mutable std::unique_ptr jacobian_cache{nullptr}; FieldMetric Bxy_; ///< Magnitude of B = nabla z times nabla x diff --git a/include/bout/metric_tensor.hxx b/include/bout/metric_tensor.hxx index 795e51d65a..04fcd04e77 100644 --- a/include/bout/metric_tensor.hxx +++ b/include/bout/metric_tensor.hxx @@ -7,7 +7,6 @@ #include "bout/field3d.hxx" #include -#include #include #include @@ -74,10 +73,15 @@ public: MetricTensor inverse(const std::string& region = "RGN_ALL"); // Transforms the MetricTensor by applying the given function to every component - void map(const std::function& function); - - MetricTensor applyToComponents( - const std::function& function) const; + template + void map(F function) { + g11_m = function(g11_m); + g22_m = function(g22_m); + g33_m = function(g33_m); + g12_m = function(g12_m); + g13_m = function(g13_m); + g23_m = function(g23_m); + } void communicate(Mesh* mesh); diff --git a/src/mesh/christoffel_symbols.cxx b/src/mesh/christoffel_symbols.cxx index 462a0a8115..7da19b72a8 100644 --- a/src/mesh/christoffel_symbols.cxx +++ b/src/mesh/christoffel_symbols.cxx @@ -3,7 +3,6 @@ #include "bout/coordinates.hxx" #include "bout/mesh.hxx" -#include #include ChristoffelSymbols::ChristoffelSymbols( @@ -127,24 +126,6 @@ ChristoffelSymbols::ChristoffelSymbols(Coordinates& coordinates) { + 0.5 * g33 * coordinates.DDY(g_33); } -void ChristoffelSymbols::applyToComponents( - const std::function& function) { - - const auto components_in = std::array{ - G1_11_m, G1_22_m, G1_33_m, G1_12_m, G1_13_m, G1_23_m, G2_11_m, G2_22_m, G2_33_m, - G2_12_m, G2_13_m, G2_23_m, G3_11_m, G3_22_m, G3_33_m, G3_12_m, G3_13_m, G3_23_m}; - - std::array components_out; - - std::transform(components_in.begin(), components_in.end(), components_out.begin(), - function); - auto [G1_11, G1_22, G1_33, G1_12, G1_13, G1_23, G2_11, G2_22, G2_33, G2_12, G2_13, - G2_23, G3_11, G3_22, G3_33, G3_12, G3_13, G3_23] = components_out; - - setChristoffelSymbols(G1_11, G1_22, G1_33, G1_12, G1_13, G1_23, G2_11, G2_22, G2_33, - G2_12, G2_13, G2_23, G3_11, G3_22, G3_33, G3_12, G3_13, G3_23); -} - void ChristoffelSymbols::communicate(Mesh* mesh) { output_progress.write("\tCommunicating connection terms\n"); diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index d70a44ce36..a8ad828458 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -492,9 +492,8 @@ void Coordinates::interpolateFromCoordinates(Options* mesh_options, return interpolateAndExtrapolate(component, location, true, true, false, transform.get()); }; - applyToContravariantMetricTensor(interpolateAndExtrapolate_function); - applyToCovariantMetricTensor(interpolateAndExtrapolate_function); - + contravariantMetricTensor.map(interpolateAndExtrapolate_function); + covariantMetricTensor.map(interpolateAndExtrapolate_function); // Check input metrics checkContravariant(); checkCovariant(); @@ -586,7 +585,7 @@ void Coordinates::readFromMesh(Options* mesh_options, const std::string& suffix) return interpolateAndExtrapolate(component, location, extrapolate_x, extrapolate_y, false, transform.get()); }; - applyToContravariantMetricTensor(interpolateAndExtrapolate_function); + contravariantMetricTensor.map(interpolateAndExtrapolate_function); // Check input metrics checkContravariant(); @@ -618,7 +617,7 @@ void Coordinates::readFromMesh(Options* mesh_options, const std::string& suffix) "Calculating all from the contravariant tensor\n"); } - applyToCovariantMetricTensor(interpolateAndExtrapolate_function); + covariantMetricTensor.map(interpolateAndExtrapolate_function); // Check covariant metrics checkCovariant(); @@ -900,7 +899,7 @@ void Coordinates::extrapolateChristoffelSymbols() { transform.get()); }; - applyToChristoffelSymbols(interpolateAndExtrapolate_function); + christoffel_symbols().map(interpolateAndExtrapolate_function); } void Coordinates::extrapolateGValues() { @@ -1537,11 +1536,6 @@ void Coordinates::setBxy(FieldMetric Bxy) { Bxy_ = std::move(Bxy); } -void Coordinates::applyToChristoffelSymbols( - const std::function& function) { - christoffel_symbols().applyToComponents(function); -} - void Coordinates::setContravariantMetricTensor( const ContravariantMetricTensor& metric_tensor, const std::string& region, bool recalculate_staggered, bool force_interpolate_from_centre) { @@ -1566,16 +1560,6 @@ void Coordinates::setMetricTensor( covariantMetricTensor.setMetricTensor(covariant_metric_tensor); } -void Coordinates::applyToContravariantMetricTensor( - const std::function& function) { - contravariantMetricTensor.map(function); -} - -void Coordinates::applyToCovariantMetricTensor( - const std::function& function) { - covariantMetricTensor.map(function); -} - void Coordinates::invalidateAndRecalculateCachedVariables() { zlength_cache.reset(); Grad2_par2_DDY_invSgCache.clear(); diff --git a/src/mesh/metric_tensor.cxx b/src/mesh/metric_tensor.cxx index be149e7b19..eacce98763 100644 --- a/src/mesh/metric_tensor.cxx +++ b/src/mesh/metric_tensor.cxx @@ -8,10 +8,7 @@ #include "bout/region.hxx" #include "bout/utils.hxx" -#include -#include #include -#include #include @@ -141,31 +138,6 @@ MetricTensor MetricTensor::inverse(const std::string& region) { return other_representation; } -void MetricTensor::map( - const std::function& function) { - - const MetricTensor updated_metric_tensor = applyToComponents(function); - - setMetricTensor(MetricTensor(updated_metric_tensor.g11_m, updated_metric_tensor.g22_m, - updated_metric_tensor.g33_m, updated_metric_tensor.g12_m, - updated_metric_tensor.g13_m, updated_metric_tensor.g23_m)); -} - -MetricTensor MetricTensor::applyToComponents( - const std::function& function) const { - - const std::array components_in{g11_m, g22_m, g33_m, - g12_m, g13_m, g23_m}; - - std::array components_out; - - std::transform(components_in.begin(), components_in.end(), components_out.begin(), - function); - auto [g_11, g_22, g_33, g_12, g_13, g_23] = components_out; - - return MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23); -} - void MetricTensor::communicate(Mesh* mesh) { mesh->communicate(g11_m, g22_m, g33_m, g12_m, g13_m, g23_m); } From e5010e466102609da4cc0fc4b045585ab4b7e992 Mon Sep 17 00:00:00 2001 From: Peter Hill Date: Wed, 23 Oct 2024 19:00:44 +0100 Subject: [PATCH 480/491] Use free-function derivatives in metric tensor classes --- src/mesh/christoffel_symbols.cxx | 121 ++++++++++++------------------- src/mesh/g_values.cxx | 8 +- 2 files changed, 52 insertions(+), 77 deletions(-) diff --git a/src/mesh/christoffel_symbols.cxx b/src/mesh/christoffel_symbols.cxx index 7da19b72a8..2ef4142711 100644 --- a/src/mesh/christoffel_symbols.cxx +++ b/src/mesh/christoffel_symbols.cxx @@ -1,6 +1,6 @@ - #include "bout/christoffel_symbols.hxx" #include "bout/coordinates.hxx" +#include "bout/derivs.hxx" #include "bout/mesh.hxx" #include @@ -42,88 +42,63 @@ ChristoffelSymbols::ChristoffelSymbols(Coordinates& coordinates) { const auto& g_13 = covariantMetricTensor.g13(); const auto& g_23 = covariantMetricTensor.g23(); - G1_11_m = 0.5 * g11 * coordinates.DDX(g_11) - + g12 * (coordinates.DDX(g_12) - 0.5 * coordinates.DDY(g_11)) - + g13 * (coordinates.DDX(g_13) - 0.5 * coordinates.DDZ(g_11)); - G1_22_m = g11 * (coordinates.DDY(g_12) - 0.5 * coordinates.DDX(g_22)) - + 0.5 * g12 * coordinates.DDY(g_22) - + g13 * (coordinates.DDY(g_23) - 0.5 * coordinates.DDZ(g_22)); - G1_33_m = g11 * (coordinates.DDZ(g_13) - 0.5 * coordinates.DDX(g_33)) - + g12 * (coordinates.DDZ(g_23) - 0.5 * coordinates.DDY(g_33)) - + 0.5 * g13 * coordinates.DDZ(g_33); - G1_12_m = - 0.5 * g11 * coordinates.DDY(g_11) + 0.5 * g12 * coordinates.DDX(g_22) - + 0.5 * g13 - * (coordinates.DDY(g_13) + coordinates.DDX(g_23) - coordinates.DDZ(g_12)); - G1_13_m = - 0.5 * g11 * coordinates.DDZ(g_11) - + 0.5 * g12 - * (coordinates.DDZ(g_12) + coordinates.DDX(g_23) - coordinates.DDY(g_13)) - + 0.5 * g13 * coordinates.DDX(g_33); - G1_23_m = - 0.5 * g11 * (coordinates.DDZ(g_12) + coordinates.DDY(g_13) - coordinates.DDX(g_23)) - + 0.5 * g12 - * (coordinates.DDZ(g_22) + coordinates.DDY(g_23) - coordinates.DDY(g_23)) - // + 0.5 *g13*(coordinates.DDZ(g_32) + coordinates.DDY(g_33) - coordinates.DDZ(g_23)); - // which equals - + 0.5 * g13 * coordinates.DDY(g_33); + G1_11_m = 0.5 * g11 * DDX(g_11) + g12 * (DDX(g_12) - 0.5 * DDY(g_11)) + + g13 * (DDX(g_13) - 0.5 * DDZ(g_11)); + G1_22_m = g11 * (DDY(g_12) - 0.5 * DDX(g_22)) + 0.5 * g12 * DDY(g_22) + + g13 * (DDY(g_23) - 0.5 * DDZ(g_22)); + G1_33_m = g11 * (DDZ(g_13) - 0.5 * DDX(g_33)) + g12 * (DDZ(g_23) - 0.5 * DDY(g_33)) + + 0.5 * g13 * DDZ(g_33); + G1_12_m = 0.5 * g11 * DDY(g_11) + 0.5 * g12 * DDX(g_22) + + 0.5 * g13 * (DDY(g_13) + DDX(g_23) - DDZ(g_12)); + G1_13_m = 0.5 * g11 * DDZ(g_11) + 0.5 * g12 * (DDZ(g_12) + DDX(g_23) - DDY(g_13)) + + 0.5 * g13 * DDX(g_33); + G1_23_m = 0.5 * g11 * (DDZ(g_12) + DDY(g_13) - DDX(g_23)) + + 0.5 * g12 * (DDZ(g_22) + DDY(g_23) - DDY(g_23)) + // + 0.5 *g13*(DDZ(g_32) + DDY(g_33) - DDZ(g_23)); + // which equals + + 0.5 * g13 * DDY(g_33); - G2_11_m = 0.5 * g12 * coordinates.DDX(g_11) - + g22 * (coordinates.DDX(g_12) - 0.5 * coordinates.DDY(g_11)) - + g23 * (coordinates.DDX(g_13) - 0.5 * coordinates.DDZ(g_11)); - G2_22_m = g12 * (coordinates.DDY(g_12) - 0.5 * coordinates.DDX(g_22)) - + 0.5 * g22 * coordinates.DDY(g_22) - + g23 * (coordinates.DDY(g23) - 0.5 * coordinates.DDZ(g_22)); - G2_33_m = g12 * (coordinates.DDZ(g_13) - 0.5 * coordinates.DDX(g_33)) - + g22 * (coordinates.DDZ(g_23) - 0.5 * coordinates.DDY(g_33)) - + 0.5 * g23 * coordinates.DDZ(g_33); - G2_12_m = - 0.5 * g12 * coordinates.DDY(g_11) + 0.5 * g22 * coordinates.DDX(g_22) - + 0.5 * g23 - * (coordinates.DDY(g_13) + coordinates.DDX(g_23) - coordinates.DDZ(g_12)); + G2_11_m = 0.5 * g12 * DDX(g_11) + g22 * (DDX(g_12) - 0.5 * DDY(g_11)) + + g23 * (DDX(g_13) - 0.5 * DDZ(g_11)); + G2_22_m = g12 * (DDY(g_12) - 0.5 * DDX(g_22)) + 0.5 * g22 * DDY(g_22) + + g23 * (DDY(g23) - 0.5 * DDZ(g_22)); + G2_33_m = g12 * (DDZ(g_13) - 0.5 * DDX(g_33)) + g22 * (DDZ(g_23) - 0.5 * DDY(g_33)) + + 0.5 * g23 * DDZ(g_33); + G2_12_m = 0.5 * g12 * DDY(g_11) + 0.5 * g22 * DDX(g_22) + + 0.5 * g23 * (DDY(g_13) + DDX(g_23) - DDZ(g_12)); G2_13_m = - // 0.5 *g21*(coordinates.DDZ(g_11) + coordinates.DDX(covariantMetricTensor.Getg13()) - coordinates.DDX(g_13)) + // 0.5 *g21*(DDZ(g_11) + DDX(g13()) - DDX(g_13)) // which equals - 0.5 * g12 * (coordinates.DDZ(g_11) + coordinates.DDX(g_13) - coordinates.DDX(g_13)) - // + 0.5 *g22*(coordinates.DDZ(covariantMetricTensor.Getg21()) + coordinates.DDX(g_23) - coordinates.DDY(g_13)) + 0.5 * g12 * (DDZ(g_11) + DDX(g_13) - DDX(g_13)) + // + 0.5 *g22*(DDZ(g21()) + DDX(g_23) - DDY(g_13)) // which equals - + 0.5 * g22 - * (coordinates.DDZ(g_12) + coordinates.DDX(g_23) - coordinates.DDY(g_13)) - // + 0.5 *g23*(coordinates.DDZ(covariantMetricTensor.Getg31()) + coordinates.DDX(g_33) - coordinates.DDZ(g_13)); + + 0.5 * g22 * (DDZ(g_12) + DDX(g_23) - DDY(g_13)) + // + 0.5 *g23*(DDZ(g31()) + DDX(g_33) - DDZ(g_13)); // which equals - + 0.5 * g23 * coordinates.DDX(g_33); - G2_23_m = - 0.5 * g12 * (coordinates.DDZ(g_12) + coordinates.DDY(g_13) - coordinates.DDX(g_23)) - + 0.5 * g22 * coordinates.DDZ(g_22) + 0.5 * g23 * coordinates.DDY(g_33); + + 0.5 * g23 * DDX(g_33); + G2_23_m = 0.5 * g12 * (DDZ(g_12) + DDY(g_13) - DDX(g_23)) + 0.5 * g22 * DDZ(g_22) + + 0.5 * g23 * DDY(g_33); - G3_11_m = 0.5 * g13 * coordinates.DDX(g_11) - + g23 * (coordinates.DDX(g_12) - 0.5 * coordinates.DDY(g_11)) - + g33 * (coordinates.DDX(g_13) - 0.5 * coordinates.DDZ(g_11)); - G3_22_m = g13 * (coordinates.DDY(g_12) - 0.5 * coordinates.DDX(g_22)) - + 0.5 * g23 * coordinates.DDY(g_22) - + g33 * (coordinates.DDY(g_23) - 0.5 * coordinates.DDZ(g_22)); - G3_33_m = g13 * (coordinates.DDZ(g_13) - 0.5 * coordinates.DDX(g_33)) - + g23 * (coordinates.DDZ(g_23) - 0.5 * coordinates.DDY(g_33)) - + 0.5 * g33 * coordinates.DDZ(g_33); + G3_11_m = 0.5 * g13 * DDX(g_11) + g23 * (DDX(g_12) - 0.5 * DDY(g_11)) + + g33 * (DDX(g_13) - 0.5 * DDZ(g_11)); + G3_22_m = g13 * (DDY(g_12) - 0.5 * DDX(g_22)) + 0.5 * g23 * DDY(g_22) + + g33 * (DDY(g_23) - 0.5 * DDZ(g_22)); + G3_33_m = g13 * (DDZ(g_13) - 0.5 * DDX(g_33)) + g23 * (DDZ(g_23) - 0.5 * DDY(g_33)) + + 0.5 * g33 * DDZ(g_33); G3_12_m = - // 0.5 *g31*(coordinates.DDY(g_11) + coordinates.DDX(covariantMetricTensor.Getg12()) - coordinates.DDX(g_12)) + // 0.5 *g31*(DDY(g_11) + DDX(g12()) - DDX(g_12)) // which equals to - 0.5 * g13 * coordinates.DDY(g_11) - // + 0.5 *g32*(coordinates.DDY(covariantMetricTensor.Getg21()) + coordinates.DDX(g_22) - coordinates.DDY(g_12)) + 0.5 * g13 * DDY(g_11) + // + 0.5 *g32*(DDY(g21()) + DDX(g_22) - DDY(g_12)) // which equals to - + 0.5 * g23 * coordinates.DDX(g_22) - //+ 0.5 *g33*(coordinates.DDY(covariantMetricTensor.Getg31()) + coordinates.DDX(covariantMetricTensor.Getg32()) - coordinates.DDZ(g_12)); + + 0.5 * g23 * DDX(g_22) + //+ 0.5 *g33*(DDY(g31()) + DDX(g32()) - DDZ(g_12)); // which equals to - + 0.5 * g33 * (coordinates.DDY(g_13)) + coordinates.DDX(g_23) - - coordinates.DDZ(g_12); - G3_13_m = - 0.5 * g13 * coordinates.DDZ(g_11) - + 0.5 * g23 - * (coordinates.DDZ(g_12) + coordinates.DDX(g_23) - coordinates.DDY(g_13)) - + 0.5 * g33 * coordinates.DDX(g_33); - G3_23_m = 0.5 * g13 * (coordinates.DDZ(g_12) + coordinates.DDY(g_13)) - - coordinates.DDX(g_23) + 0.5 * g23 * coordinates.DDZ(g_22) - + 0.5 * g33 * coordinates.DDY(g_33); + + 0.5 * g33 * (DDY(g_13)) + DDX(g_23) - DDZ(g_12); + G3_13_m = 0.5 * g13 * DDZ(g_11) + 0.5 * g23 * (DDZ(g_12) + DDX(g_23) - DDY(g_13)) + + 0.5 * g33 * DDX(g_33); + G3_23_m = 0.5 * g13 * (DDZ(g_12) + DDY(g_13)) - DDX(g_23) + 0.5 * g23 * DDZ(g_22) + + 0.5 * g33 * DDY(g_33); } void ChristoffelSymbols::communicate(Mesh* mesh) { diff --git a/src/mesh/g_values.cxx b/src/mesh/g_values.cxx index 2b52ca9a68..6f142f32cd 100644 --- a/src/mesh/g_values.cxx +++ b/src/mesh/g_values.cxx @@ -1,6 +1,6 @@ - #include "bout/g_values.hxx" #include "bout/coordinates.hxx" +#include "bout/derivs.hxx" #include "bout/mesh.hxx" GValues::GValues(FieldMetric G1, FieldMetric G2, FieldMetric G3) @@ -20,13 +20,13 @@ GValues::GValues(const Coordinates& coordinates) { auto tmp = J * g12; Coordinates::communicate(tmp); - G1_m = (coordinates.DDX(J * g11) + coordinates.DDY(tmp) + coordinates.DDZ(J * g13)) / J; + G1_m = (DDX(J * g11) + DDY(tmp) + DDZ(J * g13)) / J; tmp = J * g22; Coordinates::communicate(tmp); - G2_m = (coordinates.DDX(J * g12) + coordinates.DDY(tmp) + coordinates.DDZ(J * g23)) / J; + G2_m = (DDX(J * g12) + DDY(tmp) + DDZ(J * g23)) / J; tmp = J * g23; Coordinates::communicate(tmp); - G3_m = (coordinates.DDX(J * g13) + coordinates.DDY(tmp) + coordinates.DDZ(J * g33)) / J; + G3_m = (DDX(J * g13) + DDY(tmp) + DDZ(J * g33)) / J; } void GValues::communicate(Mesh* mesh) { mesh->communicate(G1_m, G2_m, G3_m); } From a926df1cbf58436c6cf22eb59eb36ae396c2f674 Mon Sep 17 00:00:00 2001 From: Peter Hill Date: Thu, 24 Oct 2024 09:38:25 +0100 Subject: [PATCH 481/491] Remove some unused ctors from metric tensor classes --- include/bout/christoffel_symbols.hxx | 10 ---------- include/bout/coordinates.hxx | 4 ---- include/bout/g_values.hxx | 13 ++++++------- src/mesh/christoffel_symbols.cxx | 15 --------------- src/mesh/coordinates.cxx | 8 ++++---- src/mesh/g_values.cxx | 3 --- 6 files changed, 10 insertions(+), 43 deletions(-) diff --git a/include/bout/christoffel_symbols.hxx b/include/bout/christoffel_symbols.hxx index 7b6a9033a9..f03d942ab8 100644 --- a/include/bout/christoffel_symbols.hxx +++ b/include/bout/christoffel_symbols.hxx @@ -14,18 +14,8 @@ class Coordinates; class ChristoffelSymbols { public: - ChristoffelSymbols(FieldMetric G1_11, FieldMetric G1_22, FieldMetric G1_33, - FieldMetric G1_12, FieldMetric G1_13, FieldMetric G1_23, - FieldMetric G2_11, FieldMetric G2_22, FieldMetric G2_33, - FieldMetric G2_12, FieldMetric G2_13, FieldMetric G2_23, - FieldMetric G3_11, FieldMetric G3_22, FieldMetric G3_33, - FieldMetric G3_12, FieldMetric G3_13, FieldMetric G3_23); - explicit ChristoffelSymbols(Coordinates& coordinates); - // ChristoffelSymbols(BoutReal g11, BoutReal g22, BoutReal g33, BoutReal g12, BoutReal g13, - // BoutReal g23, Mesh* mesh); - const FieldMetric& G1_11() const { return G1_11_m; } const FieldMetric& G1_22() const { return G1_22_m; } const FieldMetric& G1_33() const { return G1_33_m; } diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index 3cca83746b..7b4d26009f 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -390,10 +390,6 @@ public: const FieldMetric& G2() const { return g_values().G2(); } const FieldMetric& G3() const { return g_values().G3(); } - void setG1(const FieldMetric& G1) const { g_values().setG1(G1); } - void setG2(const FieldMetric& G2) const { g_values().setG2(G2); } - void setG3(const FieldMetric& G3) const { g_values().setG3(G3); } - const BoutReal& G1(int x, int y, int z) const { return G1()(x, y, z); } const BoutReal& G2(int x, int y, int z) const { return G2()(x, y, z); } const BoutReal& G3(int x, int y, int z) const { return G3()(x, y, z); } diff --git a/include/bout/g_values.hxx b/include/bout/g_values.hxx index 0fbb25950d..72c03716bb 100644 --- a/include/bout/g_values.hxx +++ b/include/bout/g_values.hxx @@ -8,21 +8,20 @@ using FieldMetric = MetricTensor::FieldMetric; /// `GValues` needs renaming, when we know what the name should be class GValues { - public: - GValues(FieldMetric G1, FieldMetric G2, FieldMetric G3); - explicit GValues(const Coordinates& coordinates); const FieldMetric& G1() const { return G1_m; } const FieldMetric& G2() const { return G2_m; } const FieldMetric& G3() const { return G3_m; } - void setG1(const FieldMetric& G1) { G1_m = G1; } - void setG2(const FieldMetric& G2) { G2_m = G2; } - void setG3(const FieldMetric& G3) { G3_m = G3; } - void communicate(Mesh* mesh); + template + void map(F function) { + G1_m = function(G1_m); + G2_m = function(G2_m); + G3_m = function(G3_m); + } private: FieldMetric G1_m, G2_m, G3_m; diff --git a/src/mesh/christoffel_symbols.cxx b/src/mesh/christoffel_symbols.cxx index 2ef4142711..9c6410a452 100644 --- a/src/mesh/christoffel_symbols.cxx +++ b/src/mesh/christoffel_symbols.cxx @@ -3,21 +3,6 @@ #include "bout/derivs.hxx" #include "bout/mesh.hxx" -#include - -ChristoffelSymbols::ChristoffelSymbols( - FieldMetric G1_11, FieldMetric G1_22, FieldMetric G1_33, FieldMetric G1_12, - FieldMetric G1_13, FieldMetric G1_23, FieldMetric G2_11, FieldMetric G2_22, - FieldMetric G2_33, FieldMetric G2_12, FieldMetric G2_13, FieldMetric G2_23, - FieldMetric G3_11, FieldMetric G3_22, FieldMetric G3_33, FieldMetric G3_12, - FieldMetric G3_13, FieldMetric G3_23) - : G1_11_m(std::move(G1_11)), G1_22_m(std::move(G1_22)), G1_33_m(std::move(G1_33)), - G1_12_m(std::move(G1_12)), G1_13_m(std::move(G1_13)), G1_23_m(std::move(G1_23)), - G2_11_m(std::move(G2_11)), G2_22_m(std::move(G2_22)), G2_33_m(std::move(G2_33)), - G2_12_m(std::move(G2_12)), G2_13_m(std::move(G2_13)), G2_23_m(std::move(G2_23)), - G3_11_m(std::move(G3_11)), G3_22_m(std::move(G3_22)), G3_33_m(std::move(G3_33)), - G3_12_m(std::move(G3_12)), G3_13_m(std::move(G3_13)), G3_23_m(std::move(G3_23)) {}; - ChristoffelSymbols::ChristoffelSymbols(Coordinates& coordinates) { // Calculate Christoffel symbol terms (18 independent values) // Note: This calculation is completely general: metric diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index a8ad828458..2d1b275c48 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -903,10 +903,10 @@ void Coordinates::extrapolateChristoffelSymbols() { } void Coordinates::extrapolateGValues() { - - setG1(interpolateAndExtrapolate(G1(), location, true, true, true, transform.get())); - setG2(interpolateAndExtrapolate(G2(), location, true, true, true, transform.get())); - setG3(interpolateAndExtrapolate(G3(), location, true, true, true, transform.get())); + g_values().map([this](const FieldMetric& component) { + return interpolateAndExtrapolate(component, location, true, true, true, + transform.get()); + }); } MetricTensor::FieldMetric Coordinates::recalculateJacobian() const { diff --git a/src/mesh/g_values.cxx b/src/mesh/g_values.cxx index 6f142f32cd..9bc0d304c9 100644 --- a/src/mesh/g_values.cxx +++ b/src/mesh/g_values.cxx @@ -3,9 +3,6 @@ #include "bout/derivs.hxx" #include "bout/mesh.hxx" -GValues::GValues(FieldMetric G1, FieldMetric G2, FieldMetric G3) - : G1_m(std::move(G1)), G2_m(std::move(G2)), G3_m(std::move(G3)) {}; - GValues::GValues(const Coordinates& coordinates) { const auto& contravariantMetricTensor = coordinates.getContravariantMetricTensor(); From ff3fdefe2ced9e132195872e4394ca7d7d654453 Mon Sep 17 00:00:00 2001 From: Peter Hill Date: Thu, 24 Oct 2024 11:26:51 +0100 Subject: [PATCH 482/491] Ensure metric derivatives are always communicated --- include/bout/christoffel_symbols.hxx | 2 -- include/bout/g_values.hxx | 1 - src/mesh/christoffel_symbols.cxx | 9 +++------ src/mesh/coordinates.cxx | 6 ++++-- src/mesh/g_values.cxx | 4 ++-- 5 files changed, 9 insertions(+), 13 deletions(-) diff --git a/include/bout/christoffel_symbols.hxx b/include/bout/christoffel_symbols.hxx index f03d942ab8..0e0459eb9a 100644 --- a/include/bout/christoffel_symbols.hxx +++ b/include/bout/christoffel_symbols.hxx @@ -60,8 +60,6 @@ public: G3_23_m = function(G3_23_m); } - void communicate(Mesh* mesh); - private: FieldMetric G1_11_m, G1_22_m, G1_33_m, G1_12_m, G1_13_m, G1_23_m; FieldMetric G2_11_m, G2_22_m, G2_33_m, G2_12_m, G2_13_m, G2_23_m; diff --git a/include/bout/g_values.hxx b/include/bout/g_values.hxx index 72c03716bb..eac2194bd3 100644 --- a/include/bout/g_values.hxx +++ b/include/bout/g_values.hxx @@ -15,7 +15,6 @@ public: const FieldMetric& G2() const { return G2_m; } const FieldMetric& G3() const { return G3_m; } - void communicate(Mesh* mesh); template void map(F function) { G1_m = function(G1_m); diff --git a/src/mesh/christoffel_symbols.cxx b/src/mesh/christoffel_symbols.cxx index 9c6410a452..b6dde00a28 100644 --- a/src/mesh/christoffel_symbols.cxx +++ b/src/mesh/christoffel_symbols.cxx @@ -84,13 +84,10 @@ ChristoffelSymbols::ChristoffelSymbols(Coordinates& coordinates) { + 0.5 * g33 * DDX(g_33); G3_23_m = 0.5 * g13 * (DDZ(g_12) + DDY(g_13)) - DDX(g_23) + 0.5 * g23 * DDZ(g_22) + 0.5 * g33 * DDY(g_33); -} - -void ChristoffelSymbols::communicate(Mesh* mesh) { output_progress.write("\tCommunicating connection terms\n"); - mesh->communicate(G1_11_m, G1_22_m, G1_33_m, G1_12_m, G1_13_m, G1_23_m, G2_11_m, - G2_22_m, G2_33_m, G2_12_m, G2_13_m, G2_23_m, G3_11_m, G3_22_m, - G3_33_m, G3_12_m, G3_13_m, G3_23_m); + G1_11_m.getMesh()->communicate(G1_11_m, G1_22_m, G1_33_m, G1_12_m, G1_13_m, G1_23_m, + G2_11_m, G2_22_m, G2_33_m, G2_12_m, G2_13_m, G2_23_m, + G3_11_m, G3_22_m, G3_33_m, G3_12_m, G3_13_m, G3_23_m); } diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 2d1b275c48..fcf57c1e77 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -4,6 +4,8 @@ * given the contravariant metric tensor terms **************************************************************************/ +#include "bout/field2d.hxx" +#include "bout/g_values.hxx" #include #include #include @@ -788,11 +790,11 @@ void Coordinates::recalculateAndReset(bool recalculate_staggered, checkCovariant(); christoffel_symbols_cache.reset(); - christoffel_symbols().communicate(localmesh); + christoffel_symbols(); extrapolateChristoffelSymbols(); g_values_cache.reset(); - g_values().communicate(localmesh); + g_values(); extrapolateGValues(); correctionForNonUniformMeshes(force_interpolate_from_centre); diff --git a/src/mesh/g_values.cxx b/src/mesh/g_values.cxx index 9bc0d304c9..c019ab1c41 100644 --- a/src/mesh/g_values.cxx +++ b/src/mesh/g_values.cxx @@ -24,6 +24,6 @@ GValues::GValues(const Coordinates& coordinates) { tmp = J * g23; Coordinates::communicate(tmp); G3_m = (DDX(J * g13) + DDY(tmp) + DDZ(J * g33)) / J; -} -void GValues::communicate(Mesh* mesh) { mesh->communicate(G1_m, G2_m, G3_m); } + G1_m.getMesh()->communicate(G1_m, G2_m, G3_m); +} From 676f0a04493469f587a23d53c935f88ed1981cae Mon Sep 17 00:00:00 2001 From: Peter Hill Date: Thu, 24 Oct 2024 13:14:44 +0100 Subject: [PATCH 483/491] Remove `MetricTensor::setLocation` Location will already be set from initialising using `emptyFrom` --- include/bout/metric_tensor.hxx | 9 --------- src/mesh/metric_tensor.cxx | 6 +----- 2 files changed, 1 insertion(+), 14 deletions(-) diff --git a/include/bout/metric_tensor.hxx b/include/bout/metric_tensor.hxx index 04fcd04e77..cad82099f3 100644 --- a/include/bout/metric_tensor.hxx +++ b/include/bout/metric_tensor.hxx @@ -61,15 +61,6 @@ public: g23_m = metric_tensor.g23(); } - void setLocation(const CELL_LOC location) { - g11_m.setLocation(location); - g22_m.setLocation(location); - g33_m.setLocation(location); - g12_m.setLocation(location); - g13_m.setLocation(location); - g23_m.setLocation(location); - } - MetricTensor inverse(const std::string& region = "RGN_ALL"); // Transforms the MetricTensor by applying the given function to every component diff --git a/src/mesh/metric_tensor.cxx b/src/mesh/metric_tensor.cxx index eacce98763..7094d8eaad 100644 --- a/src/mesh/metric_tensor.cxx +++ b/src/mesh/metric_tensor.cxx @@ -11,7 +11,6 @@ #include #include - MetricTensor::MetricTensor(FieldMetric g11, FieldMetric g22, FieldMetric g33, FieldMetric g12, FieldMetric g13, FieldMetric g23) : g11_m(std::move(g11)), g22_m(std::move(g22)), g33_m(std::move(g33)), @@ -132,10 +131,7 @@ MetricTensor MetricTensor::inverse(const std::string& region) { output_info.write("\tMaximum error in off-diagonal inversion is {:e}\n", off_diagonal_maxerr); - auto other_representation = MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23); - const auto location = g11_m.getLocation(); - other_representation.setLocation(location); - return other_representation; + return MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23); } void MetricTensor::communicate(Mesh* mesh) { From ff12e4a5ee85dd618f594a698c850bf425c49924 Mon Sep 17 00:00:00 2001 From: Peter Hill Date: Thu, 24 Oct 2024 13:17:42 +0100 Subject: [PATCH 484/491] Pull out duplicated region name --- src/mesh/metric_tensor.cxx | 30 ++++++++++++------------------ 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/src/mesh/metric_tensor.cxx b/src/mesh/metric_tensor.cxx index 7094d8eaad..d74e6b05a5 100644 --- a/src/mesh/metric_tensor.cxx +++ b/src/mesh/metric_tensor.cxx @@ -33,12 +33,10 @@ void MetricTensor::check(int ystart) { if (non_identity_parallel_transform) { for (int dy = 1; dy <= ystart; ++dy) { for (const auto sign : {1, -1}) { - bout::checkFinite(g11_m.ynext(sign * dy), "g11.ynext", - fmt::format("RGN_YPAR_{:+d}", sign * dy)); - bout::checkFinite(g22_m.ynext(sign * dy), "g22.ynext", - fmt::format("RGN_YPAR_{:+d}", sign * dy)); - bout::checkFinite(g33_m.ynext(sign * dy), "g33.ynext", - fmt::format("RGN_YPAR_{:+d}", sign * dy)); + const auto region = fmt::format("RGN_YPAR_{:+d}", sign * dy); + bout::checkFinite(g11_m.ynext(sign * dy), "g11.ynext", region); + bout::checkFinite(g22_m.ynext(sign * dy), "g22.ynext", region); + bout::checkFinite(g33_m.ynext(sign * dy), "g33.ynext", region); } } } @@ -50,12 +48,10 @@ void MetricTensor::check(int ystart) { if (non_identity_parallel_transform) { for (int dy = 1; dy <= ystart; ++dy) { for (const auto sign : {1, -1}) { - bout::checkPositive(g11_m.ynext(sign * dy), "g11.ynext", - fmt::format("RGN_YPAR_{:+d}", sign * dy)); - bout::checkPositive(g22_m.ynext(sign * dy), "g22.ynext", - fmt::format("RGN_YPAR_{:+d}", sign * dy)); - bout::checkPositive(g33_m.ynext(sign * dy), "g33.ynext", - fmt::format("RGN_YPAR_{:+d}", sign * dy)); + const auto region = fmt::format("RGN_YPAR_{:+d}", sign * dy); + bout::checkPositive(g11_m.ynext(sign * dy), "g11.ynext", region); + bout::checkPositive(g22_m.ynext(sign * dy), "g22.ynext", region); + bout::checkPositive(g33_m.ynext(sign * dy), "g33.ynext", region); } } } @@ -67,12 +63,10 @@ void MetricTensor::check(int ystart) { if (non_identity_parallel_transform) { for (int dy = 1; dy <= ystart; ++dy) { for (const auto sign : {1, -1}) { - bout::checkFinite(g12_m.ynext(sign * dy), "g12.ynext", - fmt::format("RGN_YPAR_{:+d}", sign * dy)); - bout::checkFinite(g13_m.ynext(sign * dy), "g13.ynext", - fmt::format("RGN_YPAR_{:+d}", sign * dy)); - bout::checkFinite(g23_m.ynext(sign * dy), "g23.ynext", - fmt::format("RGN_YPAR_{:+d}", sign * dy)); + const auto region = fmt::format("RGN_YPAR_{:+d}", sign * dy); + bout::checkFinite(g12_m.ynext(sign * dy), "g12.ynext", region); + bout::checkFinite(g13_m.ynext(sign * dy), "g13.ynext", region); + bout::checkFinite(g23_m.ynext(sign * dy), "g23.ynext", region); } } } From 8a1d040a2439a6b43f5246e7e0c01c6d5b80c0fe Mon Sep 17 00:00:00 2001 From: Peter Hill Date: Thu, 24 Oct 2024 13:27:18 +0100 Subject: [PATCH 485/491] Remove specialised `setDy`, `setJ` overloads --- include/bout/coordinates.hxx | 12 ------------ src/mesh/coordinates.cxx | 18 ------------------ tests/integrated/test-snb/test_snb.cxx | 18 +++++++++++++----- 3 files changed, 13 insertions(+), 35 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index 7b4d26009f..018078b706 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -112,12 +112,6 @@ public: void setDy(FieldMetric dy) { dy_ = std::move(dy); } void setDz(FieldMetric dz) { dz_ = std::move(dz); } -#if BOUT_USE_METRIC_3D - void setDy(BoutReal value, int x, int y, int z) { dy_(x, y, z) = value; } -#else - void setDy(BoutReal value, int x, int y) { dy_(x, y) = value; } -#endif - void setD1_dx(FieldMetric d1_dx) { d1_dx_ = std::move(d1_dx); } void setD1_dy(FieldMetric d1_dy) { d1_dy_ = std::move(d1_dy); } void setD1_dz(FieldMetric d1_dz) { d1_dz_ = std::move(d1_dz); } @@ -249,12 +243,6 @@ public: void setJ(const FieldMetric& J); -#if BOUT_USE_METRIC_3D - void setJ(BoutReal value, int x, int y, int z); -#else - void setJ(BoutReal value, int x, int y); -#endif - void setBxy(FieldMetric Bxy); /// d pitch angle / dx. Needed for vector differentials (Curl) diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index fcf57c1e77..232164d7a9 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -1515,24 +1515,6 @@ void Coordinates::setJ(const FieldMetric& J) { jacobian_cache = std::move(ptr); } -#if BOUT_USE_METRIC_3D -void Coordinates::setJ(BoutReal value, int x, int y, int z) { - //TODO: Calculate J and check value is close - auto f = J(); - f(x, y, z) = value; - auto ptr = std::make_unique(f); - jacobian_cache = std::move(ptr); -} -#else -void Coordinates::setJ(BoutReal value, int x, int y) { - //TODO: Calculate J and check value is close - auto f = J(); - f(x, y) = value; - auto ptr = std::make_unique(f); - jacobian_cache = std::move(ptr); -} -#endif - void Coordinates::setBxy(FieldMetric Bxy) { //TODO: Calculate Bxy and check value is close Bxy_ = std::move(Bxy); diff --git a/tests/integrated/test-snb/test_snb.cxx b/tests/integrated/test-snb/test_snb.cxx index b81c3d8904..96b053a183 100644 --- a/tests/integrated/test-snb/test_snb.cxx +++ b/tests/integrated/test-snb/test_snb.cxx @@ -1,6 +1,8 @@ #include #include #include +#include +#include #include #include #include @@ -207,13 +209,19 @@ int main(int argc, char** argv) { // Change the mesh spacing and cell volume (Jdy) Coordinates* coord = Te.getCoordinates(); - for (int x = mesh->xstart; x <= mesh->xend; x++) { - for (int y = mesh->ystart; y <= mesh->yend; y++) { - double yn = (double(y) + 0.5) / double(mesh->yend + 1); + { + auto dy = emptyFrom(coord->dx()); + auto J = emptyFrom(coord->J()); + for (int x = mesh->xstart; x <= mesh->xend; x++) { + for (int y = mesh->ystart; y <= mesh->yend; y++) { + const double y_n = (double(y) + 0.5) / double(mesh->yend + 1); - coord->setDy((1. - 0.9 * yn), x, y); - coord->setJ((1. + yn * yn), x, y); + dy(x, y) = 1. - 0.9 * y_n; + J(x, y) = 1. + y_n * y_n; + } } + coord->setDy(dy); + coord->setJ(J); } HeatFluxSNB snb; From 0663ea8a0f518273eafa01e2cf2ddcd5c163bbb4 Mon Sep 17 00:00:00 2001 From: Peter Hill Date: Thu, 24 Oct 2024 13:46:05 +0100 Subject: [PATCH 486/491] Ensure grid spacings, Bxy, J, are always communicated --- include/bout/coordinates.hxx | 6 +-- src/mesh/coordinates.cxx | 51 ++++++++++++++------- tests/unit/mesh/test_coordinates.cxx | 9 ++-- tools/pylib/_boutpp_build/boutcpp.pxd.jinja | 1 - tools/pylib/_boutpp_build/helper.cxx.jinja | 1 - 5 files changed, 41 insertions(+), 27 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index 018078b706..a62c79f622 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -108,9 +108,9 @@ public: const BoutReal& J(int x, int y) const { return J()(x, y); } #endif - void setDx(FieldMetric dx) { dx_ = std::move(dx); } - void setDy(FieldMetric dy) { dy_ = std::move(dy); } - void setDz(FieldMetric dz) { dz_ = std::move(dz); } + void setDx(FieldMetric dx); + void setDy(FieldMetric dy); + void setDz(FieldMetric dz); void setD1_dx(FieldMetric d1_dx) { d1_dx_ = std::move(d1_dx); } void setD1_dy(FieldMetric d1_dy) { d1_dy_ = std::move(d1_dy); } diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 232164d7a9..589b15d765 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -567,6 +567,10 @@ void Coordinates::readFromMesh(Options* mesh_options, const std::string& suffix) dy_ = interpolateAndExtrapolate(dy_, location, extrapolate_x, extrapolate_y, false, transform.get()); + setDx(dx_); + setDy(dy_); + setDz(dz_); + // grid data source has staggered fields, so read instead of interpolating // Diagonal components of metric tensor g^{ij} (default to 1) const auto g11 = getAtLocOrUnaligned(localmesh, "g11", 1.0, suffix, location); @@ -758,27 +762,39 @@ const Field2D& Coordinates::zlength() const { return *zlength_cache; } -int Coordinates::communicateAndCheckMeshSpacing() { - TRACE("Coordinates::communicateAndCheckMeshSpacing"); - - localmesh->communicate(dx_, dy_, dz_, Bxy_, J()); - covariantMetricTensor.communicate(localmesh); - contravariantMetricTensor.communicate(localmesh); - - output_progress.write("Calculating differential geometry terms\n"); - - if (min(abs(dx())) < 1e-8) { +void Coordinates::setDx(FieldMetric dx) { + if (min(abs(dx)) < 1e-8) { throw BoutException("dx magnitude less than 1e-8"); } - if (min(abs(dy())) < 1e-8) { + dx_ = std::move(dx); + localmesh->communicate(dx_); +} + +void Coordinates::setDy(FieldMetric dy) { + if (min(abs(dy)) < 1e-8) { throw BoutException("dy magnitude less than 1e-8"); } - if (min(abs(dz())) < 1e-8) { + dy_ = std::move(dy); + localmesh->communicate(dy_); +} + +void Coordinates::setDz(FieldMetric dz) { + if (min(abs(dz)) < 1e-8) { throw BoutException("dz magnitude less than 1e-8"); } + dz_ = std::move(dz); + localmesh->communicate(dz_); +} + +int Coordinates::communicateAndCheckMeshSpacing() { + TRACE("Coordinates::communicateAndCheckMeshSpacing"); + + covariantMetricTensor.communicate(localmesh); + contravariantMetricTensor.communicate(localmesh); + return 0; } @@ -1503,21 +1519,24 @@ void Coordinates::checkContravariant() { FieldMetric& Coordinates::J() const { if (jacobian_cache == nullptr) { const auto j = recalculateJacobian(); - auto ptr = std::make_unique(j); - jacobian_cache = std::move(ptr); + jacobian_cache = std::make_unique(j); } return *jacobian_cache; } void Coordinates::setJ(const FieldMetric& J) { + bout::checkFinite(J, "J", "RGN_NOCORNERS"); + bout::checkPositive(J, "J", "RGN_NOCORNERS"); + //TODO: Calculate J and check value is close - auto ptr = std::make_unique(J); - jacobian_cache = std::move(ptr); + jacobian_cache = std::make_unique(J); + localmesh->communicate(*jacobian_cache); } void Coordinates::setBxy(FieldMetric Bxy) { //TODO: Calculate Bxy and check value is close Bxy_ = std::move(Bxy); + localmesh->communicate(Bxy_); } void Coordinates::setContravariantMetricTensor( diff --git a/tests/unit/mesh/test_coordinates.cxx b/tests/unit/mesh/test_coordinates.cxx index e0c301c574..2b3f8c7887 100644 --- a/tests/unit/mesh/test_coordinates.cxx +++ b/tests/unit/mesh/test_coordinates.cxx @@ -231,12 +231,9 @@ TEST_F(CoordinatesTest, SmallMeshSpacing) { static_cast(bout::globals::mesh) ->setGridDataSource(new FakeGridDataSource({{"dx", 1e-9}})); - output_info.disable(); - output_warn.disable(); - Coordinates coords(mesh); - EXPECT_THROW(coords.communicateAndCheckMeshSpacing(), BoutException); - output_warn.enable(); - output_info.enable(); + WithQuietOutput quiet_info{output_info}; + WithQuietOutput quiet_warn{output_warn}; + EXPECT_THROW(Coordinates{mesh}, BoutException); } TEST_F(CoordinatesTest, ConstructWithDiagonalContravariantMetric) { diff --git a/tools/pylib/_boutpp_build/boutcpp.pxd.jinja b/tools/pylib/_boutpp_build/boutcpp.pxd.jinja index aab9926e3a..fab475c52a 100644 --- a/tools/pylib/_boutpp_build/boutcpp.pxd.jinja +++ b/tools/pylib/_boutpp_build/boutcpp.pxd.jinja @@ -78,7 +78,6 @@ cdef extern from "bout/coordinates.hxx": {{ metric_field }} G1, G2, G3 {{ metric_field }} ShiftTorsion {{ metric_field }} IntShiftTorsion - int communicateAndCheckMeshSpacing() cdef extern from "bout/fieldgroup.hxx": cppclass FieldGroup: diff --git a/tools/pylib/_boutpp_build/helper.cxx.jinja b/tools/pylib/_boutpp_build/helper.cxx.jinja index d7f13b84ea..a611bc813d 100644 --- a/tools/pylib/_boutpp_build/helper.cxx.jinja +++ b/tools/pylib/_boutpp_build/helper.cxx.jinja @@ -173,5 +173,4 @@ void c_mesh_normalise(Mesh * msh, double norm){ coord->setDx(coord->dx() / norm); coord->setDy(coord->dy() / norm); coord->setDz(coord->dz() / norm); - coord->communicateAndCheckMeshSpacing(); } From a4fe574a92e90e0889153cd7af9664723e85070a Mon Sep 17 00:00:00 2001 From: Peter Hill Date: Thu, 24 Oct 2024 15:30:47 +0100 Subject: [PATCH 487/491] Ensure metric terms are always communicated --- include/bout/coordinates.hxx | 2 -- include/bout/metric_tensor.hxx | 2 -- src/mesh/christoffel_symbols.cxx | 2 -- src/mesh/coordinates.cxx | 9 --------- src/mesh/metric_tensor.cxx | 5 +---- 5 files changed, 1 insertion(+), 19 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index a62c79f622..7d9cfccb3e 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -255,8 +255,6 @@ public: IntShiftTorsion_ = std::move(IntShiftTorsion); } - int communicateAndCheckMeshSpacing(); - /////////////////////////////////////////////////////////// // Parallel transforms /////////////////////////////////////////////////////////// diff --git a/include/bout/metric_tensor.hxx b/include/bout/metric_tensor.hxx index cad82099f3..38c7d7e775 100644 --- a/include/bout/metric_tensor.hxx +++ b/include/bout/metric_tensor.hxx @@ -74,8 +74,6 @@ public: g23_m = function(g23_m); } - void communicate(Mesh* mesh); - private: FieldMetric g11_m, g22_m, g33_m, g12_m, g13_m, g23_m; }; diff --git a/src/mesh/christoffel_symbols.cxx b/src/mesh/christoffel_symbols.cxx index b6dde00a28..a124b64c47 100644 --- a/src/mesh/christoffel_symbols.cxx +++ b/src/mesh/christoffel_symbols.cxx @@ -8,8 +8,6 @@ ChristoffelSymbols::ChristoffelSymbols(Coordinates& coordinates) { // Note: This calculation is completely general: metric // tensor can be 2D or 3D. For 2D, all DDZ terms are zero - coordinates.communicateAndCheckMeshSpacing(); - const auto& contravariantMetricTensor = coordinates.getContravariantMetricTensor(); const auto& covariantMetricTensor = coordinates.getCovariantMetricTensor(); diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 589b15d765..ae1a64a104 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -789,15 +789,6 @@ void Coordinates::setDz(FieldMetric dz) { localmesh->communicate(dz_); } -int Coordinates::communicateAndCheckMeshSpacing() { - TRACE("Coordinates::communicateAndCheckMeshSpacing"); - - covariantMetricTensor.communicate(localmesh); - contravariantMetricTensor.communicate(localmesh); - - return 0; -} - void Coordinates::recalculateAndReset(bool recalculate_staggered, bool force_interpolate_from_centre) { diff --git a/src/mesh/metric_tensor.cxx b/src/mesh/metric_tensor.cxx index d74e6b05a5..81df132d11 100644 --- a/src/mesh/metric_tensor.cxx +++ b/src/mesh/metric_tensor.cxx @@ -125,9 +125,6 @@ MetricTensor MetricTensor::inverse(const std::string& region) { output_info.write("\tMaximum error in off-diagonal inversion is {:e}\n", off_diagonal_maxerr); + g_11.getMesh()->communicate(g_11, g_22, g_33, g_12, g_13, g_23); return MetricTensor(g_11, g_22, g_33, g_12, g_13, g_23); } - -void MetricTensor::communicate(Mesh* mesh) { - mesh->communicate(g11_m, g22_m, g33_m, g12_m, g13_m, g23_m); -} From b276d746a1238b098fc58f80672fe99fef9420b0 Mon Sep 17 00:00:00 2001 From: Peter Hill Date: Thu, 24 Oct 2024 15:31:17 +0100 Subject: [PATCH 488/491] Simplify `recalculateJacobian` a little --- src/mesh/coordinates.cxx | 26 +++++++++----------------- 1 file changed, 9 insertions(+), 17 deletions(-) diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index ae1a64a104..44ffa74e89 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -923,24 +923,16 @@ MetricTensor::FieldMetric Coordinates::recalculateJacobian() const { TRACE("Coordinates::jacobian"); try { // calculate Jacobian using g^-1 = det[g^ij], J = sqrt(g) - auto g = contravariantMetricTensor.g11() * contravariantMetricTensor.g22() - * contravariantMetricTensor.g33() - + 2.0 * contravariantMetricTensor.g12() * contravariantMetricTensor.g13() - * contravariantMetricTensor.g23() - - contravariantMetricTensor.g11() * contravariantMetricTensor.g23() - * contravariantMetricTensor.g23() - - contravariantMetricTensor.g22() * contravariantMetricTensor.g13() - * contravariantMetricTensor.g13() - - contravariantMetricTensor.g33() * contravariantMetricTensor.g12() - * contravariantMetricTensor.g12(); - - // Check that g is positive - bout::checkPositive(g, "The determinant of g^ij", "RGN_NOBNDRY"); - - return 1. / sqrt(g); - } catch (BoutException&) { + auto g_matrix = g11() * g22() * g33() + 2.0 * g12() * g13() * g23() + - g11() * g23() * g23() - g22() * g13() * g13() + - g33() * g12() * g12(); + + bout::checkPositive(g_matrix, "The determinant of g^ij", "RGN_NOBNDRY"); + + return 1. / sqrt(g_matrix); + } catch (const BoutException& e) { output_error.write("\tError in jacobian call\n"); - throw; + throw e; } } From 61196bfbcabafc89284c70731d03e6e3b2046bbd Mon Sep 17 00:00:00 2001 From: Peter Hill Date: Thu, 24 Oct 2024 15:38:06 +0100 Subject: [PATCH 489/491] Eliminate some unnecessary local variables --- src/mesh/coordinates.cxx | 9 +++------ src/mesh/mesh.cxx | 13 +++++-------- 2 files changed, 8 insertions(+), 14 deletions(-) diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index 44ffa74e89..a4f86cc1ad 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -1450,16 +1450,14 @@ Field2D Coordinates::Laplace_perpXY([[maybe_unused]] const Field2D& A, ChristoffelSymbols& Coordinates::christoffel_symbols() { if (christoffel_symbols_cache == nullptr) { - auto ptr = std::make_unique(*this); - christoffel_symbols_cache = std::move(ptr); + christoffel_symbols_cache = std::make_unique(*this); } return *christoffel_symbols_cache; } GValues& Coordinates::g_values() const { if (g_values_cache == nullptr) { - auto ptr = std::make_unique(*this); - g_values_cache = std::move(ptr); + g_values_cache = std::make_unique(*this); } return *g_values_cache; } @@ -1501,8 +1499,7 @@ void Coordinates::checkContravariant() { FieldMetric& Coordinates::J() const { if (jacobian_cache == nullptr) { - const auto j = recalculateJacobian(); - jacobian_cache = std::make_unique(j); + jacobian_cache = std::make_unique(recalculateJacobian()); } return *jacobian_cache; } diff --git a/src/mesh/mesh.cxx b/src/mesh/mesh.cxx index f1007895f0..856ad3661e 100644 --- a/src/mesh/mesh.cxx +++ b/src/mesh/mesh.cxx @@ -578,17 +578,14 @@ std::shared_ptr Mesh::createDefaultCoordinates(const CELL_LOC location, bool force_interpolate_from_centre) { - std::shared_ptr new_coordinates; if (location == CELL_CENTRE || location == CELL_DEFAULT) { // Initialize coordinates from input - new_coordinates = std::make_shared(this, options); - } else { - // Interpolate coordinates from CELL_CENTRE version - new_coordinates = std::make_shared(this, options, location, - getCoordinates(CELL_CENTRE), - force_interpolate_from_centre); + return std::make_shared(this, options); } - return new_coordinates; + // Interpolate coordinates from CELL_CENTRE version + return std::make_shared(this, options, location, + getCoordinates(CELL_CENTRE), + force_interpolate_from_centre); } const Region<>& Mesh::getRegion3D(const std::string& region_name) const { From 42dc6ee7b3491529bdd9689c4fdcf3a7e0643325 Mon Sep 17 00:00:00 2001 From: Peter Hill Date: Thu, 24 Oct 2024 15:37:44 +0100 Subject: [PATCH 490/491] Inline some single-use functions --- include/bout/coordinates.hxx | 6 ---- src/mesh/coordinates.cxx | 63 +++++++++++++----------------------- 2 files changed, 22 insertions(+), 47 deletions(-) diff --git a/include/bout/coordinates.hxx b/include/bout/coordinates.hxx index 7d9cfccb3e..d100e2d797 100644 --- a/include/bout/coordinates.hxx +++ b/include/bout/coordinates.hxx @@ -450,8 +450,6 @@ private: FieldMetric Bxy_; ///< Magnitude of B = nabla z times nabla x - void invalidateAndRecalculateCachedVariables(); - /// Set the parallel (y) transform from the options file. /// Used in the constructor to create the transform object. void setParallelTransform(Options* options); @@ -468,10 +466,6 @@ private: FieldMetric getUnaligned(const std::string& name, BoutReal default_value); - void extrapolateChristoffelSymbols(); - - void extrapolateGValues(); - FieldMetric recalculateBxy() const; /// Non-uniform meshes. Need to use DDX, DDY diff --git a/src/mesh/coordinates.cxx b/src/mesh/coordinates.cxx index a4f86cc1ad..d4df92b275 100644 --- a/src/mesh/coordinates.cxx +++ b/src/mesh/coordinates.cxx @@ -797,12 +797,7 @@ void Coordinates::recalculateAndReset(bool recalculate_staggered, checkCovariant(); christoffel_symbols_cache.reset(); - christoffel_symbols(); - extrapolateChristoffelSymbols(); - g_values_cache.reset(); - g_values(); - extrapolateGValues(); correctionForNonUniformMeshes(force_interpolate_from_centre); @@ -811,7 +806,9 @@ void Coordinates::recalculateAndReset(bool recalculate_staggered, localmesh->recalculateStaggeredCoordinates(); } - invalidateAndRecalculateCachedVariables(); + zlength_cache.reset(); + Grad2_par2_DDY_invSgCache.clear(); + invSgCache.reset(); } void Coordinates::correctionForNonUniformMeshes(bool force_interpolate_from_centre) { @@ -889,35 +886,6 @@ void Coordinates::correctionForNonUniformMeshes(bool force_interpolate_from_cent localmesh->communicate(d1_dx_, d1_dy_, d1_dz_); } -void Coordinates::extrapolateChristoffelSymbols() { - - // Set boundary guard cells of Christoffel symbol terms - // Ideally, when location is staggered, we would set the upper/outer boundary point - // correctly rather than by extrapolating here: e.g. if location==CELL_YLOW and we are - // at the upper y-boundary the x- and z-derivatives at yend+1 at the boundary can be - // calculated because the guard cells are available, while the y-derivative could be - // calculated from the CELL_CENTRE metric components (which have guard cells available - // past the boundary location). This would avoid the problem that the y-boundary on the - // CELL_YLOW grid is at a 'guard cell' location (yend+1). - // However, the above would require lots of special handling, so just extrapolate for - // now. - - std::function const - interpolateAndExtrapolate_function = [this](const FieldMetric& component) { - return interpolateAndExtrapolate(component, location, true, true, false, - transform.get()); - }; - - christoffel_symbols().map(interpolateAndExtrapolate_function); -} - -void Coordinates::extrapolateGValues() { - g_values().map([this](const FieldMetric& component) { - return interpolateAndExtrapolate(component, location, true, true, true, - transform.get()); - }); -} - MetricTensor::FieldMetric Coordinates::recalculateJacobian() const { TRACE("Coordinates::jacobian"); @@ -1451,6 +1419,21 @@ Field2D Coordinates::Laplace_perpXY([[maybe_unused]] const Field2D& A, ChristoffelSymbols& Coordinates::christoffel_symbols() { if (christoffel_symbols_cache == nullptr) { christoffel_symbols_cache = std::make_unique(*this); + // Set boundary guard cells of Christoffel symbol terms + // Ideally, when location is staggered, we would set the upper/outer boundary point + // correctly rather than by extrapolating here: e.g. if location==CELL_YLOW and we are + // at the upper y-boundary the x- and z-derivatives at yend+1 at the boundary can be + // calculated because the guard cells are available, while the y-derivative could be + // calculated from the CELL_CENTRE metric components (which have guard cells available + // past the boundary location). This would avoid the problem that the y-boundary on the + // CELL_YLOW grid is at a 'guard cell' location (yend+1). + // However, the above would require lots of special handling, so just extrapolate for + // now. + + christoffel_symbols_cache->map([this](const FieldMetric& component) { + return interpolateAndExtrapolate(component, location, true, true, false, + transform.get()); + }); } return *christoffel_symbols_cache; } @@ -1458,6 +1441,10 @@ ChristoffelSymbols& Coordinates::christoffel_symbols() { GValues& Coordinates::g_values() const { if (g_values_cache == nullptr) { g_values_cache = std::make_unique(*this); + g_values_cache->map([this](const FieldMetric& component) { + return interpolateAndExtrapolate(component, location, true, true, true, + transform.get()); + }); } return *g_values_cache; } @@ -1542,9 +1529,3 @@ void Coordinates::setMetricTensor( contravariantMetricTensor.setMetricTensor(contravariant_metric_tensor); covariantMetricTensor.setMetricTensor(covariant_metric_tensor); } - -void Coordinates::invalidateAndRecalculateCachedVariables() { - zlength_cache.reset(); - Grad2_par2_DDY_invSgCache.clear(); - invSgCache.reset(); -} From 47cff9e4b1863e4db193c987b16ea565a81e9a54 Mon Sep 17 00:00:00 2001 From: Peter Hill Date: Thu, 24 Oct 2024 17:35:00 +0100 Subject: [PATCH 491/491] Fix wrong type in MMS/gbs --- tests/MMS/GBS/gbs.hxx | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/MMS/GBS/gbs.hxx b/tests/MMS/GBS/gbs.hxx index 468a5e579c..da7a971389 100644 --- a/tests/MMS/GBS/gbs.hxx +++ b/tests/MMS/GBS/gbs.hxx @@ -88,8 +88,9 @@ private: const Field3D D(const Field3D& f, BoutReal d); // Diffusion operator const Field3D H(const Field3D& f, BoutReal h); // Hyper-diffusion // Powers of the mesh spacing for H operator - Field2D dx4, dy4; - BoutReal dz4; + Field2D dx4; + Field2D dy4; + Field2D dz4; // Laplacian solver std::unique_ptr phiSolver{nullptr};