Skip to content

Commit 0607503

Browse files
committed
Adding SLEPc solver for k-eigenvalue problems.
1 parent 61a9334 commit 0607503

31 files changed

Lines changed: 1153 additions & 195 deletions

cmake/FindPETSc.cmake

Lines changed: 107 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,127 @@
1-
# Find PETSc
1+
# FindPETSc.cmake
22
#
33
# Once done this will define
44
# PETSC_FOUND - System has PETSc
55
# PETSC_INCLUDE_DIR - The PETSc include directory
66
# PETSC_LIBRARY - The PETSc library
77
# PETSC_VERSION - The PETSc version
8+
# SLEPC_FOUND - PETSc has SLEPc
9+
# SLEPC_INCLUDE_DIR - The SLEPc include directory
10+
# SLEPC_LIBRARY - The SLEPc library
11+
# SLEPC_VERSION
812

9-
find_path(
10-
PETSC_INCLUDE_DIR
11-
petsc.h
12-
PATHS
13-
$ENV{PETSC_DIR}/include
14-
$ENV{PETSC_ROOT}/include
13+
include(FindPackageHandleStandardArgs)
14+
15+
find_package(PETSc CONFIG QUIET
16+
PATHS
17+
$ENV{PETSC_DIR}/$ENV{PETSC_ARCH}/lib/cmake/PETSc
18+
$ENV{PETSC_ROOT}/$ENV{PETSC_ARCH}/lib/cmake/PETSc
1519
)
1620

17-
find_library(
18-
PETSC_LIBRARY
19-
petsc
21+
if (NOT PETSc_FOUND)
22+
find_path(PETSC_INCLUDE_DIR
23+
NAMES petsc.h
2024
PATHS
21-
$ENV{PETSC_DIR}/$ENV{PETSC_ARCH}/lib
22-
$ENV{PETSC_DIR}/lib
23-
$ENV{PETSC_ROOT}/$ENV{PETSC_ARCH}/lib
24-
$ENV{PETSC_ROOT}/lib
25-
)
25+
$ENV{PETSC_DIR}/include
26+
$ENV{PETSC_ROOT}/include
27+
DOC "PETSc include directory"
28+
)
29+
30+
find_library(PETSC_LIBRARY
31+
NAMES petsc
32+
PATHS
33+
$ENV{PETSC_DIR}/$ENV{PETSC_ARCH}/lib
34+
$ENV{PETSC_DIR}/lib
35+
$ENV{PETSC_ROOT}/$ENV{PETSC_ARCH}/lib
36+
$ENV{PETSC_ROOT}/lib
37+
DOC "PETSc library"
38+
)
39+
40+
set(PETSc_FOUND
41+
${PETSC_INCLUDE_DIR}
42+
AND
43+
${PETSC_LIBRARY}
44+
)
45+
endif()
46+
47+
if (TARGET PETSc::PETSc)
48+
get_target_property(_petsc_version PETSc::PETSc INTERFACE_VERSION)
49+
set(PETSC_VERSION ${_petsc_version})
50+
endif()
2651

27-
set(PETSC_VERSION "unknown")
28-
find_file(PETSCVERSION_H petscversion.h
52+
if(NOT DEFINED PETSC_VERSION)
53+
set(PETSC_VERSION "unknown")
54+
find_file(PETSCVERSION_H
55+
NAMES petscversion.h
2956
PATHS
30-
$ENV{PETSC_DIR}/include
31-
$ENV{PETSC_ROOT}/include
32-
)
33-
mark_as_advanced(PETSCVERSION_H)
34-
if (PETSCVERSION_H)
35-
file(READ ${PETSCVERSION_H} PETSC_VERSION_FILE)
36-
string(REGEX MATCH "define[ ]+PETSC_VERSION_MAJOR[ ]+([0-9]+)" TMP "${PETSC_VERSION_FILE}")
57+
$ENV{PETSC_DIR}/include
58+
$ENV{PETSC_ROOT}/include
59+
)
60+
if(PETSCVERSION_H)
61+
file(READ ${PETSCVERSION_H} _petscver)
62+
string(REGEX MATCH "define[ ]+PETSC_VERSION_MAJOR[ ]+([0-9]+)" _ "${_petscver}")
3763
set(PETSC_VERSION_MAJOR ${CMAKE_MATCH_1})
38-
string(REGEX MATCH "define[ ]+PETSC_VERSION_MINOR[ ]+([0-9]+)" TMP "${PETSC_VERSION_FILE}")
64+
string(REGEX MATCH "define[ ]+PETSC_VERSION_MINOR[ ]+([0-9]+)" _ "${_petscver}")
3965
set(PETSC_VERSION_MINOR ${CMAKE_MATCH_1})
40-
string(REGEX MATCH "define[ ]+PETSC_VERSION_SUBMINOR[ ]+([0-9]+)" TMP "${PETSC_VERSION_FILE}")
66+
string(REGEX MATCH "define[ ]+PETSC_VERSION_SUBMINOR[ ]+([0-9]+)" _ "${_petscver}")
4167
set(PETSC_VERSION_PATCH ${CMAKE_MATCH_1})
4268
set(PETSC_VERSION "${PETSC_VERSION_MAJOR}.${PETSC_VERSION_MINOR}.${PETSC_VERSION_PATCH}")
69+
endif()
70+
endif()
71+
72+
find_path(SLEPC_INCLUDE_DIR
73+
NAMES slepc.h
74+
PATHS
75+
$ENV{PETSC_DIR}/include/slepc
76+
$ENV{PETSC_ROOT}/include/slepc
77+
$ENV{SLEPC_DIR}/include
78+
$ENV{SLEPC_ROOT}/include
79+
DOC "SLEPc include directory"
80+
)
81+
82+
find_library(SLEPC_LIBRARY
83+
NAMES slepc
84+
PATHS
85+
$ENV{PETSC_DIR}/$ENV{PETSC_ARCH}/lib
86+
$ENV{PETSC_DIR}/lib
87+
$ENV{PETSC_ROOT}/$ENV{PETSC_ARCH}/lib
88+
$ENV{PETSC_ROOT}/lib
89+
$ENV{SLEPC_DIR}/$ENV{SLEPC_ARCH}/lib
90+
$ENV{SLEPC_ROOT}/$ENV{SLEPC_ARCH}/lib
91+
$ENV{SLEPC_DIR}/lib
92+
$ENV{SLEPC_ROOT}/lib
93+
DOC "SLEPc library"
94+
)
95+
96+
set(SLEPC_VERSION "unknown")
97+
find_file(SLEPCVERSION_H
98+
NAMES slepcversion.h
99+
PATHS
100+
$ENV{PETSC_DIR}/include/slepc
101+
$ENV{PETSC_ROOT}/include/slepc
102+
$ENV{SLEPC_DIR}/include
103+
$ENV{SLEPC_ROOT}/include
104+
)
105+
if(SLEPCVERSION_H)
106+
file(READ ${SLEPCVERSION_H} _slepcver)
107+
string(REGEX MATCH "define[ ]+SLEPC_VERSION_MAJOR[ ]+([0-9]+)" _ "${_slepcver}")
108+
set(SLEPC_VERSION_MAJOR ${CMAKE_MATCH_1})
109+
string(REGEX MATCH "define[ ]+SLEPC_VERSION_MINOR[ ]+([0-9]+)" _ "${_slepcver}")
110+
set(SLEPC_VERSION_MINOR ${CMAKE_MATCH_1})
111+
string(REGEX MATCH "define[ ]+SLEPC_VERSION_SUBMINOR[ ]+([0-9]+)" _ "${_slepcver}")
112+
set(SLEPC_VERSION_PATCH ${CMAKE_MATCH_1})
113+
set(SLEPC_VERSION "${SLEPC_VERSION_MAJOR}.${SLEPC_VERSION_MINOR}.${SLEPC_VERSION_PATCH}")
114+
endif()
115+
116+
if (NOT SLEPC_INCLUDE_DIR)
117+
message(FATAL_ERROR "PETSc has not been configured with SLEPc support.")
43118
endif()
119+
if (NOT SLEPC_LIBRARY)
120+
message(FATAL_ERROR "PETSc has not been configured with SLEPc support.")
121+
endif()
122+
set(SLEPc_FOUND TRUE)
123+
124+
list(APPEND PETSC_LIBRARY ${SLEPC_LIBRARY})
44125

45126
include(FindPackageHandleStandardArgs)
46127
find_package_handle_standard_args(
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// SPDX-FileCopyrightText: 2025 The OpenSn Authors <https://open-sn.github.io/opensn/>
2+
// SPDX-License-Identifier: MIT
3+
4+
#pragma once
5+
6+
#include "framework/math/linear_solver/linear_solver.h"
7+
#include "framework/math/linear_solver/linear_solver_context.h"
8+
9+
namespace opensn
10+
{
11+
12+
class LinearEigenSolver : public LinearSolver
13+
{
14+
public:
15+
enum class IterativeMethod : int
16+
{
17+
KRYLOV_SCHUR,
18+
POWER,
19+
ARNOLDI,
20+
SUBSPACE
21+
};
22+
23+
LinearEigenSolver(IterativeMethod method, std::shared_ptr<LinearEigenContext> ctx)
24+
: LinearSolver(ctx), method_(method)
25+
{
26+
}
27+
28+
std::string GetIterativeMethodName()
29+
{
30+
switch (method_)
31+
{
32+
case IterativeMethod::KRYLOV_SCHUR:
33+
return "KRYLOV-SCHUR";
34+
case IterativeMethod::POWER:
35+
return "POWER ITERATION";
36+
case IterativeMethod::ARNOLDI:
37+
return "ARNOLDI";
38+
case IterativeMethod::SUBSPACE:
39+
return "SUBSPACE";
40+
default:
41+
return "UNKNOWN ITERATIVE METHOD";
42+
}
43+
}
44+
45+
protected:
46+
IterativeMethod method_;
47+
};
48+
49+
} // namespace opensn

framework/math/linear_solver/linear_solver.cc

Lines changed: 0 additions & 29 deletions
This file was deleted.

framework/math/linear_solver/linear_solver.h

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,36 +14,23 @@ struct LinearSolverContext;
1414
class LinearSolver
1515
{
1616
public:
17-
enum class IterativeMethod : int
18-
{
19-
NONE = 0,
20-
CLASSIC_RICHARDSON = 1, ///< Classic Richardson (source iteration)
21-
PETSC_RICHARDSON = 3, ///< PETSc Richardson iteration
22-
PETSC_GMRES = 2, ///< PETSc GMRES iterative algorithm
23-
PETSC_BICGSTAB = 4, ///< PETSc BiCGStab iterative algorithm
24-
};
25-
26-
LinearSolver(IterativeMethod iterative_method, std::shared_ptr<LinearSolverContext> context_ptr)
27-
: iterative_method_(iterative_method), context_ptr_(context_ptr)
17+
explicit LinearSolver(std::shared_ptr<LinearSolverContext> context_ptr)
18+
: context_ptr_(context_ptr)
2819
{
2920
}
3021

31-
virtual ~LinearSolver() {}
32-
33-
std::shared_ptr<LinearSolverContext> GetContext() { return context_ptr_; }
22+
virtual ~LinearSolver() = default;
3423

3524
/// Set up the linaer solver
3625
virtual void Setup() {}
3726

3827
/// Solve the system
3928
virtual void Solve() = 0;
4029

30+
std::shared_ptr<LinearSolverContext> GetContext() { return context_ptr_; }
31+
4132
protected:
42-
const IterativeMethod iterative_method_;
4333
std::shared_ptr<LinearSolverContext> context_ptr_;
44-
45-
public:
46-
static std::string IterativeMethodName(IterativeMethod iterative_method);
4734
};
4835

4936
} // namespace opensn

framework/math/linear_solver/linear_solver_context.h

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,23 @@ enum class ResidualScaleType
1717
};
1818

1919
struct LinearSolverContext
20+
{
21+
virtual int MatrixAction(Mat& matrix, Vec& vector, Vec& action) { return 0; }
22+
23+
virtual ~LinearSolverContext() = default;
24+
};
25+
26+
struct LinearSystemContext : public LinearSolverContext
2027
{
2128
double rhs_norm = 0.0;
2229
double rhs_preconditioned_norm = 0.0;
2330
double custom_residual_scale = 1.0;
2431
ResidualScaleType residual_scale_type = ResidualScaleType::NONE;
32+
};
2533

26-
virtual int MatrixAction(Mat& matrix, Vec& vector, Vec& action) { return 0; }
27-
28-
virtual ~LinearSolverContext() = default;
34+
struct LinearEigenContext : public LinearSolverContext
35+
{
36+
double eigenvalue = 0.0;
2937
};
3038

3139
} // namespace opensn
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// SPDX-FileCopyrightText: 2025 The OpenSn Authors <https://open-sn.github.io/opensn/>
2+
// SPDX-License-Identifier: MIT
3+
4+
#pragma once
5+
6+
#include "framework/math/linear_solver/linear_solver.h"
7+
#include "framework/math/linear_solver/linear_solver_context.h"
8+
9+
namespace opensn
10+
{
11+
12+
class LinearSystemSolver : public LinearSolver
13+
{
14+
public:
15+
enum class IterativeMethod : int
16+
{
17+
NONE,
18+
CLASSIC_RICHARDSON,
19+
PETSC_RICHARDSON,
20+
PETSC_GMRES,
21+
PETSC_BICGSTAB
22+
};
23+
24+
LinearSystemSolver(IterativeMethod method, std::shared_ptr<LinearSystemContext> ctx)
25+
: LinearSolver(ctx), method_(method)
26+
{
27+
}
28+
29+
std::string GetIterativeMethodName()
30+
{
31+
switch (method_)
32+
{
33+
case IterativeMethod::NONE:
34+
return "NONE";
35+
case IterativeMethod::CLASSIC_RICHARDSON:
36+
return "CLASSIC_RICHARDSON";
37+
case IterativeMethod::PETSC_RICHARDSON:
38+
return "PETSC_RICHARDSON";
39+
case IterativeMethod::PETSC_GMRES:
40+
return "PETSC_GMRES";
41+
case IterativeMethod::PETSC_BICGSTAB:
42+
return "PETSC_BICGSTAB";
43+
default:
44+
return "UNKNOWN ITERATIVE METHOD";
45+
}
46+
}
47+
48+
protected:
49+
IterativeMethod method_;
50+
};
51+
52+
} // namespace opensn

0 commit comments

Comments
 (0)