From 4ee11b49a19155c20a19e1c0ee75278efe46060c Mon Sep 17 00:00:00 2001 From: Philip Cardiff Date: Sat, 16 Nov 2024 23:56:58 +0000 Subject: [PATCH] Generalise generation of Taylor series terms --- .../higherOrderGrad/higherOrderGrad.C | 162 ++++++++++-------- .../higherOrderGrad/higherOrderGrad.H | 7 + 2 files changed, 94 insertions(+), 75 deletions(-) diff --git a/src/solids4FoamModels/solidModels/linGeomTotalDispSolid/higherOrderGrad/higherOrderGrad.C b/src/solids4FoamModels/solidModels/linGeomTotalDispSolid/higherOrderGrad/higherOrderGrad.C index 257c3f47d..143570e7d 100644 --- a/src/solids4FoamModels/solidModels/linGeomTotalDispSolid/higherOrderGrad/higherOrderGrad.C +++ b/src/solids4FoamModels/solidModels/linGeomTotalDispSolid/higherOrderGrad/higherOrderGrad.C @@ -157,6 +157,42 @@ const List> higherOrderGrad::stencils() const } +void higherOrderGrad::generateExponents +( + const label N, + DynamicList>& exponents +) const +{ + // Estimate the number of terms to set the capacity + const label estimatedSize = (N + 1)*(N + 2)*(N + 3)/6; + exponents.setCapacity(estimatedSize); + + // Add the constant term first + exponents.append(FixedList{0, 0, 0}); + + for (label n = 1; n <= N; ++n) + { + for (label i = n; i >= 0; --i) + { + for (label j = n - i; j >= 0; --j) + { + label k = n - i - j; + if (i == 0 && j == 0 && k == 0) + { + // Skip the constant term as it's already added + continue; + } + FixedList exponent = {i, j, k}; + exponents.append(exponent); + } + } + } + + // Adjust capacity to actual size + exponents.shrink(); +} + + void higherOrderGrad::calcCoeffs() const { Info<< "higherOrderGrad::calcCoeffs()" << endl; @@ -190,9 +226,22 @@ void higherOrderGrad::calcCoeffs() const const vectorField& CI = mesh.C(); const vectorField& CfI = mesh.Cf(); - // Number of terms in Taylor expression - // 1 for zero order, 4 for 1 order, 10 for second order - const label Np = factorial(N_ + 3)/(factorial(N_)*factorial(3)); + // Calculate Taylor series exponents + // 1 for zero order, 4 for 1 order, 10 for second order, etc. + DynamicList> exponents; + generateExponents(N_, exponents); + const label Np = exponents.size(); + if (debug) + { + Info<< "Np = " << Np << endl; + } + + // Precompute factorials up to N + List factorials(N_ + 1, 1.0); + for (label n = 1; n <= N_; ++n) + { + factorials[n] = factorials[n - 1]*n; + } if (calcConditionNumber_) { @@ -229,11 +278,6 @@ void higherOrderGrad::calcCoeffs() const << "N must be at least 1!" << exit(FatalError); } - if (N_ > 2) - { - notImplemented("Orders higher that quadratic not implemented"); - } - forAll(stencils, cellI) { const DynamicList