Skip to content
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

Add interface linear solvers #204

Closed
wants to merge 23 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
e57c7e8
Add the PETSc interface code from https://github.com/mrp089/svFSIplus…
ktbolt Feb 6, 2024
ca3f17b
Add a LinearSolver abstract interface and a derived class to interfac…
ktbolt Feb 7, 2024
c16b9c2
Rename files and classes.
ktbolt Feb 8, 2024
b42e9ed
Clean up original PETSc interface code to look like C++.
ktbolt Feb 8, 2024
3f61fb1
Add fsils and trilinos LinearAlgebra classes and implementation for s…
ktbolt Feb 9, 2024
525ee43
Add XML Linear_algebra parameter.
ktbolt Feb 9, 2024
f28140b
Add using trilinos preconditioner and assembly by fsils.
ktbolt Feb 13, 2024
45bf792
Add some debug statements.
ktbolt Feb 14, 2024
ae7dd6e
Reorganize code a bit, fix creating LinearAlgebra opbject for parallel.
ktbolt Feb 15, 2024
2614c89
Modify code to support PETSc solve for each equation.
ktbolt Feb 22, 2024
99de67f
Add some error checking and comments.
ktbolt Feb 23, 2024
ab44a5b
Fix trilinos impl problems when not building with Trilinos.
ktbolt Feb 23, 2024
2ad1872
Fix classes to reproduce the ifdef versions, add more error checking.
ktbolt Feb 28, 2024
e6090c2
Replace all of the #ifdef WITH_TRILINOS with eq.linear_algebra->asse…
ktbolt Feb 28, 2024
3cbc564
Add ustruct assembly check.
ktbolt Feb 29, 2024
8f4cec9
Add petsc preconditioners.
ktbolt Mar 1, 2024
4cbeb84
Add some comments and clean up code.
ktbolt Mar 1, 2024
b0941ed
Add a check for <Linear_algebra> and add <Linear_algebra> to all tests.
ktbolt Mar 5, 2024
b473f74
Merge branch 'main' into Add-interface-linear-solvers_30
ktbolt Mar 6, 2024
5c7df40
Declare dof as local.
ktbolt Mar 6, 2024
320b9fa
Change return type from bool to void.
ktbolt Mar 6, 2024
90f239e
Fix XML tests, remove Precondition and extra Tolerance parameters.
ktbolt Mar 6, 2024
d4bdd0b
Add Linear_algebra section.
ktbolt Mar 6, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 0 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ include(SimVascularFunctionCheckCompilerFlags)
# These variables must later be add to 'ExternalProject_Add(svFSI' as -D options.
#
set(SV_USE_TRILINOS OFF CACHE BOOL "Build with the Trilinos linear algebra package")
#set(SV_USE_PETSC OFF CACHE BOOL "Build with the PETSc linear algebra package")
set(SV_PETSC_DIR "" CACHE STRING "Path to a local install of the PETSc linear algebra package")
set(ENABLE_COVERAGE OFF CACHE BOOL "Enable code coverage")
set(ENABLE_ARRAY_INDEX_CHECKING OFF CACHE BOOL "Enable Array index checking")
Expand Down Expand Up @@ -144,7 +143,6 @@ ExternalProject_Add(svFSI
-DSV_EXTERNALS_USE_TOPLEVEL_DIR:BOOL=ON
-DSV_EXTERNALS_TOPLEVEL_DIR:PATH=${SV_EXTERNALS_TOPLEVEL_DIR}
-DSV_USE_TRILINOS:BOOL=${SV_USE_TRILINOS}
#-DSV_USE_PETSC:BOOL=${SV_USE_PETSC}
-DSV_PETSC_DIR:STRING=${SV_PETSC_DIR}
-DENABLE_COVERAGE:BOOL=${ENABLE_COVERAGE}
-DENABLE_ARRAY_INDEX_CHECKING:BOOL=${ENABLE_ARRAY_INDEX_CHECKING}
Expand Down
43 changes: 36 additions & 7 deletions Code/Source/svFSI/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,27 @@
# Copyright (c) 2014-2015 The Regents of the University of California.
# All Rights Reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject
# to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
# IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
# PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
# OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED True)
Expand Down Expand Up @@ -84,11 +108,12 @@ if(NOT "${SV_PETSC_DIR}" STREQUAL "")
message(" PETSC_LIBRARY_DIRS = ${PETSC_LIBRARY_DIRS}")
message(" PETSC_INCLUDE_DIRS = ${PETSC_INCLUDE_DIRS}")

# Set PETSc include directory.
# Set PETSc include and library directories.
include_directories(${PETSC_INCLUDE_DIRS})
#link_directories(${PETSC_LIBRARY_DIRS})

# Set C++ directive to use PETSc in svFSIplus code.
ADD_DEFINITIONS(-DWITH_PETSC)
# Set C++ directive to use PETSc.
ADD_DEFINITIONS(-DUSE_PETSC)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pedantic -std=c99")

set(USE_PETSC 1)
Expand Down Expand Up @@ -123,6 +148,10 @@ set(lib ${SV_LIB_SVFSI_NAME})
set(CSRCS
Array3.h Array3.cpp
Array.h Array.cpp
LinearAlgebra.h LinearAlgebra.cpp
FsilsLinearAlgebra.h FsilsLinearAlgebra.cpp
PetscLinearAlgebra.h PetscLinearAlgebra.cpp
TrilinosLinearAlgebra.h TrilinosLinearAlgebra.cpp
Tensor4.h Tensor4.cpp
Vector.h Vector.cpp

Expand Down Expand Up @@ -192,12 +221,12 @@ set(CSRCS
)

# Set PETSc interace code.
if(USE_PETSC)
#set(CSRCS ${CSRCS} petsc_linear_solver.c)
endif()
#if(USE_PETSC)
# set(CSRCS ${CSRCS} petsc_linear_solver.h petsc_linear_solver.c)
#endif()

if(USE_TRILINOS)
set(CSRCS ${CSRCS} trilinos_linear_solver.cpp)
#set(CSRCS ${CSRCS} trilinos_linear_solver.cpp)

# trilinos directories and libraries
include_directories(${Trilinos_TPL_INCLUDE_DIRS})
Expand Down
41 changes: 17 additions & 24 deletions Code/Source/svFSI/ComMod.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@
#include <string>
#include <vector>

class LinearAlgebra;

/// @brief Fourier coefficients that are used to specify unsteady BCs
//
class fcType
Expand Down Expand Up @@ -613,7 +615,6 @@ class outputType
std::string name;
};


/// @brief Linear system of equations solver type
//
class lsType
Expand All @@ -623,9 +624,6 @@ class lsType
/// @brief LS solver (IN)
consts::SolverType LS_type = consts::SolverType::lSolver_NA;

/// @brief Preconditioner (IN)
consts::PreconditionerType PREC_Type = consts::PreconditionerType::PREC_NONE;

/// @brief Successful solving (OUT)
bool suc = false;

Expand Down Expand Up @@ -667,6 +665,9 @@ class lsType

/// @brief Calling duration (OUT)
double callD = 0.0;

//@brief Configuration file for linear solvers (Trilinos, PETSc)
std::string config;
};


Expand Down Expand Up @@ -1067,6 +1068,18 @@ class eqType
/// @brief type of linear solver
lsType ls;

/// @brief The type of interface to a numerical linear algebra library.
consts::LinearAlgebraType linear_algebra_type;

/// @brief The type of assembly interface to a numerical linear algebra library.
consts::LinearAlgebraType linear_algebra_assembly_type;

/// @brief The type of preconditioner used by the interface to a numerical linear algebra library.
consts::PreconditionerType linear_algebra_preconditioner = consts::PreconditionerType::PREC_FSILS;

/// @brief Interface to a numerical linear algebra library.
LinearAlgebra* linear_algebra = nullptr;

/// @brief FSILS type of linear solver
fsi_linear_solver::FSILS_lsType FSILS;

Expand Down Expand Up @@ -1285,23 +1298,6 @@ class ibType
ibCommType cm;
};

/// @brief Data type for Trilinos Linear Solver related arrays
//
class tlsType
{
public:

/// @brief Local to global mapping
Vector<int> ltg;

/// @brief Factor for Dirichlet BCs
Array<double> W;

/// @brief Residual
Array<double> R;
};


/// @brief The ComMod class duplicates the data structures in the Fortran COMMOD module
/// defined in MOD.f.
///
Expand Down Expand Up @@ -1582,9 +1578,6 @@ class ComMod {
/// @brief IB: Immersed boundary data structure
ibType ib;

/// @brief Trilinos Linear Solver data type
tlsType tls;

bool debug_active = false;

Timer timer;
Expand Down
149 changes: 149 additions & 0 deletions Code/Source/svFSI/FsilsLinearAlgebra.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
/* Copyright (c) Stanford University, The Regents of the University of California, and others.
*
* All Rights Reserved.
*
* See Copyright-SimVascular.txt for additional details.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject
* to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include "FsilsLinearAlgebra.h"
#include "fsils_api.hpp"
#include "lhsa.h"
#include <iostream>

/////////////////////////////////////////////////////////////////
// F s i l s L i n e a r A l g e b r a //
/////////////////////////////////////////////////////////////////
// The following methods implement the FSILS LinearAlgebra interface.

std::set<consts::LinearAlgebraType> FsilsLinearAlgebra::valid_assemblers = {
consts::LinearAlgebraType::none,
consts::LinearAlgebraType::fsils,
};

FsilsLinearAlgebra::FsilsLinearAlgebra()
{
interface_type = consts::LinearAlgebraType::fsils;
assembly_type = consts::LinearAlgebraType::fsils;
preconditioner_type = consts::PreconditionerType::PREC_FSILS;
}

/// @brief Allocate data arrays.
void FsilsLinearAlgebra::alloc(ComMod& com_mod, eqType& lEq)
{
#define n_debug_alloc
#ifdef debug_alloc
std::cout << "[FsilsLinearAlgebra::alloc] ---------- alloc ---------- " << std::endl;
#endif
int dof = com_mod.dof;

com_mod.Val.resize(dof*dof, com_mod.lhs.nnz);
}

/// @brief Assemble local element arrays.
void FsilsLinearAlgebra::assemble(ComMod& com_mod, const int num_elem_nodes, const Vector<int>& eqN,
const Array3<double>& lK, const Array<double>& lR)
{
#define n_debug_assemble
#ifdef debug_assemble
std::cout << "[FsilsLinearAlgebra::assemble] ---------- assemble ---------- " << std::endl;
std::cout << "[FsilsLinearAlgebra::assemble] num_elem_nodes: " << num_elem_nodes << std::endl;
std::cout << "[FsilsLinearAlgebra::assemble] eqN.size(): " << eqN.size() << std::endl;
std::cout << "[FsilsLinearAlgebra::assemble] lK.size(): " << lK.size() << std::endl;
std::cout << "[FsilsLinearAlgebra::assemble] lR.size(): " << lR.size() << std::endl;
#endif

lhsa_ns::do_assem(com_mod, num_elem_nodes, eqN, lK, lR);
}

/// @brief Check the validity of the preconditioner and assembly types options.
void FsilsLinearAlgebra::check_options(const consts::PreconditionerType prec_cond_type,
const consts::LinearAlgebraType assembly_type)
{
using namespace consts;
auto prec_cond_type_name = consts::preconditioner_type_to_name.at(prec_cond_type);
auto assembly_type_name = LinearAlgebra::type_to_name.at(assembly_type);
std::string error_msg;

if (valid_assemblers.count(assembly_type) == 0) {
error_msg = "fsils linear algebra can't use '" + assembly_type_name + "' for assembly.";

Check warning on line 91 in Code/Source/svFSI/FsilsLinearAlgebra.cpp

View check run for this annotation

Codecov / codecov/patch

Code/Source/svFSI/FsilsLinearAlgebra.cpp#L91

Added line #L91 was not covered by tests
}

if (fsils_preconditioners.count(prec_cond_type) == 0) {
error_msg = "fsils linear algebra can't use '" + prec_cond_type_name + "' for a preconditioner.";

Check warning on line 95 in Code/Source/svFSI/FsilsLinearAlgebra.cpp

View check run for this annotation

Codecov / codecov/patch

Code/Source/svFSI/FsilsLinearAlgebra.cpp#L95

Added line #L95 was not covered by tests
}

if (error_msg != "") {
throw std::runtime_error("[svFSIplus] ERROR: " + error_msg);

Check warning on line 99 in Code/Source/svFSI/FsilsLinearAlgebra.cpp

View check run for this annotation

Codecov / codecov/patch

Code/Source/svFSI/FsilsLinearAlgebra.cpp#L99

Added line #L99 was not covered by tests
}
}

/// @brief Initialize framework.
void FsilsLinearAlgebra::initialize(ComMod& com_mod, eqType& lEq)
{
// Nothing is needed to initialize FSILS.
}

/// @brief Set the linear algebra package for assmbly.
void FsilsLinearAlgebra::set_assembly(consts::LinearAlgebraType atype)

Check warning on line 110 in Code/Source/svFSI/FsilsLinearAlgebra.cpp

View check run for this annotation

Codecov / codecov/patch

Code/Source/svFSI/FsilsLinearAlgebra.cpp#L110

Added line #L110 was not covered by tests
{
if (atype == consts::LinearAlgebraType::none) {

Check warning on line 112 in Code/Source/svFSI/FsilsLinearAlgebra.cpp

View check run for this annotation

Codecov / codecov/patch

Code/Source/svFSI/FsilsLinearAlgebra.cpp#L112

Added line #L112 was not covered by tests
return;
}

if (valid_assemblers.count(atype) == 0) {
auto str_type = LinearAlgebra::type_to_name.at(atype);
throw std::runtime_error("[FsilsLinearAlgebra] ERROR: Can't set fsils linear algebra to use '" +
str_type + "' for assembly.");
}

Check warning on line 120 in Code/Source/svFSI/FsilsLinearAlgebra.cpp

View check run for this annotation

Codecov / codecov/patch

Code/Source/svFSI/FsilsLinearAlgebra.cpp#L116-L120

Added lines #L116 - L120 were not covered by tests

assembly_type = atype;

Check warning on line 122 in Code/Source/svFSI/FsilsLinearAlgebra.cpp

View check run for this annotation

Codecov / codecov/patch

Code/Source/svFSI/FsilsLinearAlgebra.cpp#L122

Added line #L122 was not covered by tests
}

/// @brief Set the preconditioner.
void FsilsLinearAlgebra::set_preconditioner(consts::PreconditionerType prec_type)
{
if (consts::fsils_preconditioners.count(prec_type) == 0) {
auto prec_cond_type_name = consts::preconditioner_type_to_name.at(prec_type);
throw std::runtime_error("[FsilsLinearAlgebra] ERROR: fsils linear algebra can't use '" +
prec_cond_type_name + "' for a preconditioner.");

Check warning on line 131 in Code/Source/svFSI/FsilsLinearAlgebra.cpp

View check run for this annotation

Codecov / codecov/patch

Code/Source/svFSI/FsilsLinearAlgebra.cpp#L129-L131

Added lines #L129 - L131 were not covered by tests
return;
}

Check warning on line 133 in Code/Source/svFSI/FsilsLinearAlgebra.cpp

View check run for this annotation

Codecov / codecov/patch

Code/Source/svFSI/FsilsLinearAlgebra.cpp#L133

Added line #L133 was not covered by tests

preconditioner_type = prec_type;
}

/// @brief Solve a system of linear equations.
void FsilsLinearAlgebra::solve(ComMod& com_mod, eqType& lEq, const Vector<int>& incL, const Vector<double>& res)
{
auto& lhs = com_mod.lhs;
int dof = com_mod.dof;
auto& R = com_mod.R;
auto& Val = com_mod.Val;
auto preconditioner = lEq.linear_algebra_preconditioner;

fsi_linear_solver::fsils_solve(lhs, lEq.FSILS, dof, R, Val, preconditioner, incL, res);
}

60 changes: 60 additions & 0 deletions Code/Source/svFSI/FsilsLinearAlgebra.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/* Copyright (c) Stanford University, The Regents of the University of California, and others.
*
* All Rights Reserved.
*
* See Copyright-SimVascular.txt for additional details.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject
* to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef FSILS_LINEAR_ALGEBRA_H
#define FSILS_LINEAR_ALGEBRA_H

#include "LinearAlgebra.h"

/// @brief The FsilsLinearAlgebra class implements the LinearAlgebra
/// interface for the FSILS numerical linear algebra included in svFSIplus.
///
class FsilsLinearAlgebra : public virtual LinearAlgebra {

public:
FsilsLinearAlgebra();
~FsilsLinearAlgebra() { };

Check warning on line 43 in Code/Source/svFSI/FsilsLinearAlgebra.h

View check run for this annotation

Codecov / codecov/patch

Code/Source/svFSI/FsilsLinearAlgebra.h#L43

Added line #L43 was not covered by tests

virtual void alloc(ComMod& com_mod, eqType& lEq);
virtual void assemble(ComMod& com_mod, const int num_elem_nodes, const Vector<int>& eqN,
const Array3<double>& lK, const Array<double>& lR);
virtual void check_options(const consts::PreconditionerType prec_cond_type, const consts::LinearAlgebraType assembly_type);
virtual void initialize(ComMod& com_mod, eqType& lEq);
virtual void solve(ComMod& com_mod, eqType& lEq, const Vector<int>& incL, const Vector<double>& res);
virtual void set_assembly(consts::LinearAlgebraType atype);
virtual void set_preconditioner(consts::PreconditionerType prec_type);

private:
/// @brief A list of linear algebra interfaces that can be used for assembly.
static std::set<consts::LinearAlgebraType> valid_assemblers;
};

#endif