-
Notifications
You must be signed in to change notification settings - Fork 61
Fix FSILS RCS diagonal cancellation and general block scaling #647
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
zasexton
wants to merge
4
commits into
SimVascular:main
Choose a base branch
from
zasexton:issue-646
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
136 changes: 136 additions & 0 deletions
136
tests/unitTests/linear_solver_tests/test_precond_rcs.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| // SPDX-FileCopyrightText: Copyright (c) Stanford University, The Regents of the University of California, and others. | ||
| // SPDX-License-Identifier: BSD-3-Clause | ||
|
|
||
| #include "precond.h" | ||
| #include "FE/Math/DenseLinearAlgebra.h" | ||
|
|
||
| #include <gtest/gtest.h> | ||
|
|
||
| #include <cmath> | ||
| #include <tuple> | ||
| #include <vector> | ||
|
|
||
| namespace { | ||
|
|
||
| // Boundary modes: excluded face, first component fixed, all components fixed. | ||
| using RcsParameters = std::tuple<int, double, int>; | ||
| class RcsPreconditioner : public testing::TestWithParam<RcsParameters> {}; | ||
|
|
||
| TEST_P(RcsPreconditioner, PreservesConstrainedSystem) { | ||
| const auto [dof, scale, boundary] = GetParam(); | ||
| const int n = 3 * dof; | ||
| const auto fixed = [=](int row) { | ||
| return row < dof && boundary != 0 && (boundary == 2 || row == 0); | ||
| }; | ||
|
|
||
| // Three-node tridiagonal stencil with coupled, positive-definite blocks. | ||
| std::vector<double> original(n * n, 0.0), exact(n), original_rhs(n, 0.0); | ||
| for (int row = 0; row < n; ++row) { | ||
| exact[row] = fixed(row) ? 0.0 : row + 1.0; | ||
| for (int col = 0; col < n; ++col) { | ||
| const int a = row / dof; | ||
| const int b = col / dof; | ||
| if (std::abs(a - b) <= 1) { | ||
| original[row * n + col] = scale * (a == b ? 2.0 : -1.0) | ||
| * (row % dof == col % dof ? 4.0 : 0.25); | ||
| } | ||
| } | ||
| } | ||
| for (int row = 0; row < n; ++row) { | ||
| for (int col = 0; col < n; ++col) { | ||
| original_rhs[row] += original[row * n + col] * exact[col]; | ||
| } | ||
| } | ||
|
|
||
| fsi_linear_solver::FSILS_lhsType lhs{}; | ||
| lhs.nNo = lhs.gnNo = lhs.mynNo = 3; | ||
| lhs.nnz = 7; | ||
| lhs.commu.nTasks = 1; | ||
| lhs.rowPtr.resize(2, 3); | ||
| lhs.colPtr = Vector<int>{0, 1, 0, 1, 2, 1, 2}; | ||
| lhs.diagPtr = Vector<int>{0, 3, 6}; | ||
| const int starts[] = {0, 2, 5}; | ||
| const int ends[] = {1, 4, 6}; | ||
| for (int a = 0; a < 3; ++a) { | ||
| lhs.rowPtr(0, a) = starts[a]; | ||
| lhs.rowPtr(1, a) = ends[a]; | ||
| } | ||
| lhs.nFaces = 1; | ||
| lhs.face.resize(1); | ||
| auto& face = lhs.face[0]; | ||
| face.incFlag = boundary != 0; | ||
| face.bGrp = fsi_linear_solver::BcType::BC_TYPE_Dir; | ||
| face.nNo = 1; | ||
| face.dof = dof; | ||
| face.glob = Vector<int>{0}; | ||
| face.val.resize(dof, 1); | ||
| for (int i = 0; i < dof; ++i) { | ||
| face.val(i, 0) = (boundary == 2 || i == 0) ? 0.0 : 1.0; | ||
| } | ||
|
|
||
| Array<double> val(dof * dof, lhs.nnz), rhs(dof, 3); | ||
| Array<double> left(dof, 3), right(dof, 3); | ||
| for (int a = 0; a < 3; ++a) { | ||
| for (int i = 0; i < dof; ++i) { | ||
| rhs(i, a) = original_rhs[a * dof + i]; | ||
| } | ||
| for (int k = starts[a]; k <= ends[a]; ++k) { | ||
| for (int i = 0; i < dof; ++i) { | ||
| for (int j = 0; j < dof; ++j) { | ||
| val(i * dof + j, k) = original[(a * dof + i) * n + lhs.colPtr(k) * dof + j]; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| precond::precond_rcs(lhs, lhs.rowPtr, lhs.colPtr, lhs.diagPtr, | ||
| dof, val, rhs, left, right); | ||
|
|
||
| std::vector<double> matrix(n * n, 0.0), solution(n); | ||
| for (int a = 0; a < 3; ++a) { | ||
| for (int i = 0; i < dof; ++i) { | ||
| ASSERT_TRUE(std::isfinite(left(i, a))); | ||
| ASSERT_TRUE(std::isfinite(right(i, a))); | ||
| ASSERT_GT(left(i, a), 0.0); | ||
| ASSERT_GT(right(i, a), 0.0); | ||
| const int row = a * dof + i; | ||
| const double expected_rhs = fixed(row) ? 0.0 : original_rhs[row]; | ||
| ASSERT_NEAR(rhs(i, a) / left(i, a) / scale, expected_rhs / scale, 1e-12); | ||
| solution[row] = rhs(i, a); | ||
| for (int k = starts[a]; k <= ends[a]; ++k) { | ||
| for (int j = 0; j < dof; ++j) { | ||
| const int col = lhs.colPtr(k) * dof + j; | ||
| const bool constrained = fixed(row) || fixed(col); | ||
| const double expected = constrained ? (row == col ? 1.0 : 0.0) | ||
| : original[row * n + col]; | ||
| const double restored = val(i * dof + j, k) / left(i, a) / right(j, lhs.colPtr(k)); | ||
| // Normalize by coefficient scale so erased small entries cannot pass. | ||
| const double reference_scale = constrained && row == col ? 1.0 : scale; | ||
| ASSERT_NEAR(restored / reference_scale, expected / reference_scale, 1e-12) | ||
| << "row=" << row << ", col=" << col; | ||
| matrix[row * n + col] = val(i * dof + j, k); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| svmp::FE::math::factor_dense_matrix(matrix, n, "RCS regression").solve_in_place(solution); | ||
| for (int row = 0; row < n; ++row) { | ||
| solution[row] *= right(row % dof, row / dof); | ||
| EXPECT_NEAR(solution[row], exact[row], 1e-11); | ||
| } | ||
| for (int row = 0; row < n; ++row) { | ||
| if (fixed(row)) continue; | ||
| double residual = -original_rhs[row]; | ||
| for (int col = 0; col < n; ++col) { | ||
| residual += original[row * n + col] * solution[col]; | ||
| } | ||
| EXPECT_NEAR(residual / scale, 0.0, 1e-11); | ||
| } | ||
| } | ||
|
|
||
| INSTANTIATE_TEST_SUITE_P(Blocks, RcsPreconditioner, | ||
| testing::Combine(testing::Values(1, 2, 3, 4, 5), testing::Values(1.0, 1e-11, 1e-15, 1e-17, 1e-20), | ||
| testing::Values(0, 1, 2))); | ||
|
|
||
| } // namespace |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seeing that this is the first unit test we have for the linear solver, maybe this could be a separate issue with a dedicated plan in creating a usable template.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do not think this needs to be in a separate PR. I do think that it would be nice to have a bit of documentation of what the test is doing so that it can be more easily adopted as a template/example to build other tests on.
I would recommend to expand the test's Doxygen documentation for this (Doxygen has a
@testcommand for this purpose, I believe).