feat(cpp-object-oriented-modular-refactoring): step 3 - foundation-google-style
This commit is contained in:
@@ -50,10 +50,10 @@ EulerBeam3D requireBeam(const Node& firstNode,
|
||||
const GeneralBeamSection& section,
|
||||
const LinearElasticMaterial& material) {
|
||||
auto result = EulerBeam3D::create(firstNode, secondNode, section, material);
|
||||
if (!result.hasValue()) {
|
||||
if (!result.HasValue()) {
|
||||
throw std::runtime_error{"Expected a valid EulerBeam3D fixture."};
|
||||
}
|
||||
return std::move(result.value());
|
||||
return std::move(result.Value());
|
||||
}
|
||||
|
||||
EulerBeam3D alignedBeam(double length,
|
||||
@@ -68,8 +68,8 @@ EulerBeam3D alignedBeam(double length,
|
||||
|
||||
double maximumAbsoluteEntry(const Matrix& matrix) {
|
||||
double maximum = 0.0;
|
||||
for (std::size_t row = 0; row < matrix.rows(); ++row) {
|
||||
for (std::size_t column = 0; column < matrix.columns(); ++column) {
|
||||
for (std::size_t row = 0; row < matrix.Rows(); ++row) {
|
||||
for (std::size_t column = 0; column < matrix.Columns(); ++column) {
|
||||
maximum = (std::max)(maximum, std::abs(matrix(row, column)));
|
||||
}
|
||||
}
|
||||
@@ -77,8 +77,8 @@ double maximumAbsoluteEntry(const Matrix& matrix) {
|
||||
}
|
||||
|
||||
bool matrixIsFinite(const Matrix& matrix) {
|
||||
for (std::size_t row = 0; row < matrix.rows(); ++row) {
|
||||
for (std::size_t column = 0; column < matrix.columns(); ++column) {
|
||||
for (std::size_t row = 0; row < matrix.Rows(); ++row) {
|
||||
for (std::size_t column = 0; column < matrix.Columns(); ++column) {
|
||||
if (!std::isfinite(matrix(row, column))) {
|
||||
return false;
|
||||
}
|
||||
@@ -88,13 +88,13 @@ bool matrixIsFinite(const Matrix& matrix) {
|
||||
}
|
||||
|
||||
double normalizedMatrixError(const Matrix& actual, const Matrix& expected) {
|
||||
if (actual.rows() != expected.rows() || actual.columns() != expected.columns()) {
|
||||
if (actual.Rows() != expected.Rows() || actual.Columns() != expected.Columns()) {
|
||||
throw std::invalid_argument{"Matrix comparison requires equal shapes."};
|
||||
}
|
||||
|
||||
double maximumDifference = 0.0;
|
||||
for (std::size_t row = 0; row < actual.rows(); ++row) {
|
||||
for (std::size_t column = 0; column < actual.columns(); ++column) {
|
||||
for (std::size_t row = 0; row < actual.Rows(); ++row) {
|
||||
for (std::size_t column = 0; column < actual.Columns(); ++column) {
|
||||
maximumDifference = (std::max)(
|
||||
maximumDifference,
|
||||
std::abs(actual(row, column) - expected(row, column)));
|
||||
@@ -109,16 +109,16 @@ double normalizedMatrixError(const Matrix& actual, const Matrix& expected) {
|
||||
|
||||
double vectorNorm(const Vector& vector) {
|
||||
double sum = 0.0;
|
||||
for (std::size_t index = 0; index < vector.size(); ++index) {
|
||||
for (std::size_t index = 0; index < vector.Size(); ++index) {
|
||||
sum += vector[index] * vector[index];
|
||||
}
|
||||
return std::sqrt(sum);
|
||||
}
|
||||
|
||||
double quadraticEnergy(const Matrix& matrix, const Vector& vector) {
|
||||
const Vector product = matrix.multiply(vector);
|
||||
const Vector product = matrix.Multiply(vector);
|
||||
double value = 0.0;
|
||||
for (std::size_t index = 0; index < vector.size(); ++index) {
|
||||
for (std::size_t index = 0; index < vector.Size(); ++index) {
|
||||
value += vector[index] * product[index];
|
||||
}
|
||||
return value;
|
||||
@@ -328,14 +328,14 @@ Vector solveFixedFirstNode(const Matrix& stiffness,
|
||||
}
|
||||
|
||||
Vector solveDenseSystem(Matrix matrix, Vector rightHandSide) {
|
||||
if (matrix.rows() != matrix.columns() ||
|
||||
matrix.rows() != rightHandSide.size()) {
|
||||
if (matrix.Rows() != matrix.Columns() ||
|
||||
matrix.Rows() != rightHandSide.Size()) {
|
||||
throw std::invalid_argument{"Dense test solve requires a square system."};
|
||||
}
|
||||
|
||||
for (std::size_t pivot = 0; pivot < matrix.rows(); ++pivot) {
|
||||
for (std::size_t pivot = 0; pivot < matrix.Rows(); ++pivot) {
|
||||
std::size_t pivotRow = pivot;
|
||||
for (std::size_t row = pivot + 1U; row < matrix.rows(); ++row) {
|
||||
for (std::size_t row = pivot + 1U; row < matrix.Rows(); ++row) {
|
||||
if (std::abs(matrix(row, pivot)) >
|
||||
std::abs(matrix(pivotRow, pivot))) {
|
||||
pivotRow = row;
|
||||
@@ -345,22 +345,22 @@ Vector solveDenseSystem(Matrix matrix, Vector rightHandSide) {
|
||||
!std::isfinite(matrix(pivotRow, pivot))) {
|
||||
throw std::runtime_error{"Uniform-load test fixture is singular."};
|
||||
}
|
||||
for (std::size_t column = pivot; column < matrix.columns(); ++column) {
|
||||
for (std::size_t column = pivot; column < matrix.Columns(); ++column) {
|
||||
std::swap(matrix(pivot, column), matrix(pivotRow, column));
|
||||
}
|
||||
std::swap(rightHandSide[pivot], rightHandSide[pivotRow]);
|
||||
|
||||
const double pivotValue = matrix(pivot, pivot);
|
||||
for (std::size_t column = pivot; column < matrix.columns(); ++column) {
|
||||
for (std::size_t column = pivot; column < matrix.Columns(); ++column) {
|
||||
matrix(pivot, column) /= pivotValue;
|
||||
}
|
||||
rightHandSide[pivot] /= pivotValue;
|
||||
for (std::size_t row = 0; row < matrix.rows(); ++row) {
|
||||
for (std::size_t row = 0; row < matrix.Rows(); ++row) {
|
||||
if (row == pivot) {
|
||||
continue;
|
||||
}
|
||||
const double factor = matrix(row, pivot);
|
||||
for (std::size_t column = pivot; column < matrix.columns(); ++column) {
|
||||
for (std::size_t column = pivot; column < matrix.Columns(); ++column) {
|
||||
matrix(row, column) -= factor * matrix(pivot, column);
|
||||
}
|
||||
rightHandSide[row] -= factor * rightHandSide[pivot];
|
||||
@@ -489,12 +489,12 @@ Matrix transformationFromKnownRows(
|
||||
}
|
||||
|
||||
Vector transposeMultiply(const Matrix& matrix, const Vector& vector) {
|
||||
if (matrix.rows() != vector.size()) {
|
||||
if (matrix.Rows() != vector.Size()) {
|
||||
throw std::invalid_argument{"Transpose multiply dimension mismatch."};
|
||||
}
|
||||
Vector result{matrix.columns()};
|
||||
for (std::size_t column = 0; column < matrix.columns(); ++column) {
|
||||
for (std::size_t row = 0; row < matrix.rows(); ++row) {
|
||||
Vector result{matrix.Columns()};
|
||||
for (std::size_t column = 0; column < matrix.Columns(); ++column) {
|
||||
for (std::size_t row = 0; row < matrix.Rows(); ++row) {
|
||||
result[column] += matrix(row, column) * vector[row];
|
||||
}
|
||||
}
|
||||
@@ -595,9 +595,9 @@ TEST(EulerBeam3D, TwoPointGaussMatchesClosedStiffness) {
|
||||
const Matrix closed = expectedClosedStiffness(length, section, material);
|
||||
EXPECT_LE(normalizedMatrixError(actual, closed), kMatrixTolerance);
|
||||
|
||||
Matrix transpose{actual.rows(), actual.columns()};
|
||||
for (std::size_t row = 0; row < actual.rows(); ++row) {
|
||||
for (std::size_t column = 0; column < actual.columns(); ++column) {
|
||||
Matrix transpose{actual.Rows(), actual.Columns()};
|
||||
for (std::size_t row = 0; row < actual.Rows(); ++row) {
|
||||
for (std::size_t column = 0; column < actual.Columns(); ++column) {
|
||||
transpose(row, column) = actual(column, row);
|
||||
}
|
||||
}
|
||||
@@ -629,7 +629,7 @@ TEST(EulerBeam3D, HasSixRigidModesRankSixAndPositiveDeformationEnergy) {
|
||||
const double stiffnessScale = (std::max)(1.0, maximumAbsoluteEntry(stiffness));
|
||||
for (const Vector& mode : rigidModes) {
|
||||
const double normalizedResidual =
|
||||
vectorNorm(stiffness.multiply(mode)) /
|
||||
vectorNorm(stiffness.Multiply(mode)) /
|
||||
(stiffnessScale * (std::max)(1.0, vectorNorm(mode)));
|
||||
EXPECT_LE(normalizedResidual, kRigidTolerance);
|
||||
}
|
||||
@@ -695,7 +695,7 @@ TEST(EulerBeam3D, RotatedTransformPreservesWorkAndEnergy) {
|
||||
const Matrix global = beam.globalStiffness();
|
||||
|
||||
Matrix expectedGlobal{kElementDofCount, kElementDofCount};
|
||||
const Matrix localTimesTransform = local.multiply(transformation);
|
||||
const Matrix localTimesTransform = local.Multiply(transformation);
|
||||
for (std::size_t row = 0; row < kElementDofCount; ++row) {
|
||||
for (std::size_t column = 0; column < kElementDofCount; ++column) {
|
||||
for (std::size_t inner = 0; inner < kElementDofCount; ++inner) {
|
||||
@@ -707,12 +707,12 @@ TEST(EulerBeam3D, RotatedTransformPreservesWorkAndEnergy) {
|
||||
EXPECT_LE(normalizedMatrixError(global, expectedGlobal), kMatrixTolerance);
|
||||
|
||||
Vector localDisplacement{kElementDofCount};
|
||||
for (std::size_t index = 0; index < localDisplacement.size(); ++index) {
|
||||
for (std::size_t index = 0; index < localDisplacement.Size(); ++index) {
|
||||
localDisplacement[index] = 0.01 * static_cast<double>(index + 1U) - 0.04;
|
||||
}
|
||||
const Vector globalDisplacement = transposeMultiply(transformation, localDisplacement);
|
||||
const Vector localForce = local.multiply(localDisplacement);
|
||||
const Vector globalForce = global.multiply(globalDisplacement);
|
||||
const Vector localForce = local.Multiply(localDisplacement);
|
||||
const Vector globalForce = global.Multiply(globalDisplacement);
|
||||
const Vector expectedGlobalForce = transposeMultiply(transformation, localForce);
|
||||
for (std::size_t index = 0; index < kElementDofCount; ++index) {
|
||||
expectScaledNear(globalForce[index], expectedGlobalForce[index], kMatrixTolerance);
|
||||
@@ -723,13 +723,13 @@ TEST(EulerBeam3D, RotatedTransformPreservesWorkAndEnergy) {
|
||||
kMatrixTolerance);
|
||||
|
||||
Vector globalVariation{kElementDofCount};
|
||||
for (std::size_t index = 0; index < globalVariation.size(); ++index) {
|
||||
for (std::size_t index = 0; index < globalVariation.Size(); ++index) {
|
||||
globalVariation[index] = 0.03 - 0.002 * static_cast<double>(index);
|
||||
}
|
||||
const Vector localVariation = transformation.multiply(globalVariation);
|
||||
const Vector localVariation = transformation.Multiply(globalVariation);
|
||||
expectScaledNear(
|
||||
globalVariation.dot(globalForce),
|
||||
localVariation.dot(localForce),
|
||||
globalVariation.Dot(globalForce),
|
||||
localVariation.Dot(localForce),
|
||||
kMatrixTolerance);
|
||||
|
||||
const BeamRecovery recovery = beam.recover(globalDisplacement);
|
||||
@@ -746,7 +746,7 @@ TEST(EulerBeam3D, ConstantLineLoadMatchesAllSignedComponents) {
|
||||
const std::array<double, kElementDofCount> expected = {
|
||||
5.0, -6.0, 11.0, -14.0, -22.0 / 3.0, -4.0,
|
||||
5.0, -6.0, 11.0, -14.0, 22.0 / 3.0, 4.0};
|
||||
ASSERT_EQ(equivalent.size(), expected.size());
|
||||
ASSERT_EQ(equivalent.Size(), expected.size());
|
||||
for (std::size_t index = 0; index < expected.size(); ++index) {
|
||||
expectScaledNear(equivalent[index], expected[index], kMatrixTolerance);
|
||||
}
|
||||
@@ -931,17 +931,17 @@ TEST(EulerBeam3D, RejectsInvalidGeometryAndProperties) {
|
||||
|
||||
const auto expectFailure = [](const Result<EulerBeam3D>& result,
|
||||
const std::string& code) {
|
||||
if (result.hasValue()) {
|
||||
const Matrix stiffness = result.value().localStiffness();
|
||||
if (result.HasValue()) {
|
||||
const Matrix stiffness = result.Value().localStiffness();
|
||||
ADD_FAILURE()
|
||||
<< "Invalid fixture was accepted; local stiffness finite="
|
||||
<< matrixIsFinite(stiffness)
|
||||
<< ", maximum absolute entry=" << maximumAbsoluteEntry(stiffness);
|
||||
return;
|
||||
}
|
||||
EXPECT_EQ(result.status().failureCategory(), FailureCategory::model);
|
||||
ASSERT_EQ(result.status().diagnostics().size(), 1U);
|
||||
EXPECT_EQ(result.status().diagnostics()[0U].code, code);
|
||||
EXPECT_EQ(result.GetStatus().Category(), FailureCategory::kModel);
|
||||
ASSERT_EQ(result.GetStatus().Diagnostics().size(), 1U);
|
||||
EXPECT_EQ(result.GetStatus().Diagnostics()[0U].code, code);
|
||||
};
|
||||
|
||||
expectFailure(
|
||||
@@ -964,7 +964,7 @@ TEST(EulerBeam3D, RejectsInvalidGeometryAndProperties) {
|
||||
"invalid-beam-length");
|
||||
EXPECT_TRUE(EulerBeam3D::create(
|
||||
scaledFirst, aboveThreshold, validSection, validMaterial)
|
||||
.hasValue());
|
||||
.HasValue());
|
||||
|
||||
auto parallelGuide = validSection;
|
||||
parallelGuide.firstAxis = {1.0, 0.0, 0.0};
|
||||
@@ -978,7 +978,7 @@ TEST(EulerBeam3D, RejectsInvalidGeometryAndProperties) {
|
||||
"invalid-beam-guide-vector");
|
||||
auto guideAboveThreshold = validSection;
|
||||
guideAboveThreshold.firstAxis = {1.0, 2.0e-12, 0.0};
|
||||
EXPECT_TRUE(EulerBeam3D::create(origin, unitX, guideAboveThreshold, validMaterial).hasValue());
|
||||
EXPECT_TRUE(EulerBeam3D::create(origin, unitX, guideAboveThreshold, validMaterial).HasValue());
|
||||
|
||||
auto invalidMaterial = validMaterial;
|
||||
invalidMaterial.youngsModulus = 0.0;
|
||||
|
||||
Reference in New Issue
Block a user