DPsim
Loading...
Searching...
No Matches
MNASolverFactory.h
Go to the documentation of this file.
1/* Copyright 2017-2021 Institute for Automation of Complex Power Systems,
2* EONERC, RWTH Aachen University
3*
4* This Source Code Form is subject to the terms of the Mozilla Public
5* License, v. 2.0. If a copy of the MPL was not distributed with this
6* file, You can obtain one at https://mozilla.org/MPL/2.0/.
7*********************************************************************************/
8
9#pragma once
10
11#include <dpsim/DataLogger.h>
14#include <dpsim/MNASolver.h>
15#ifdef WITH_HIP
16#include <dpsim/HipDenseAdapter.h>
17#include <dpsim/HipSparseBiCGStabAdapter.h>
18#include <dpsim/HipSparseQrAdapter.h>
19#ifdef WITH_KLU
20#include <dpsim/HipSparseLURefactorAdapter.h>
21#endif
22#endif
24#ifdef WITH_SPARSE
26#endif
27#ifdef WITH_KLU
28#include <dpsim/KLUAdapter.h>
29#endif
30#ifdef WITH_CUDA
32#ifdef WITH_CUDA_SPARSE
34#endif
35#ifdef WITH_MAGMA
37#endif
38#endif
39#ifdef WITH_MNASOLVERPLUGIN
41#endif
42
43namespace DPsim {
44
46public:
48 static const std::vector<DirectLinearSolverImpl> mSupportedSolverImpls(void) {
49 static std::vector<DirectLinearSolverImpl> ret = {
50#ifdef WITH_MNASOLVERPLUGIN
52#endif //WITH_MNASOLVERPLUGIN
53#ifdef WITH_CUDA
55#ifdef WITH_CUDA_SPARSE
56#endif // WITH_CUDA_SPARSE
58#ifdef WITH_MAGMA
60#endif // WITH_MAGMA
61#endif // WITH_CUDA
62#ifdef WITH_METAL
65#endif // WITH_METAL
66#ifdef WITH_HIP
70#ifdef WITH_KLU
72#endif
73#endif
75#ifdef WITH_SPARSE
77#endif // WITH_SPARSE
78#ifdef WITH_KLU
80#endif // WITH_KLU
81 };
82 return ret;
83 }
84
86 template <typename VarType>
87 static std::shared_ptr<MnaSolver<VarType>> factory(
88 String name, CPS::Domain domain = CPS::Domain::DP,
89 CPS::Logger::Level logLevel = CPS::Logger::Level::info,
90 DirectLinearSolverImpl implementation = mSupportedSolverImpls().back(),
91 String pluginName = "plugin.so") {
92 //To avoid regression we use KLU in case of undefined implementation
93 if (implementation == DirectLinearSolverImpl::Undef) {
94#ifdef WITH_KLU
95 implementation = DirectLinearSolverImpl::KLU;
96#else
97 implementation = mSupportedSolverImpls().back();
98#endif
99 }
101 "MnaSolverFactory", CPS::Logger::Level::info, CPS::Logger::Level::info);
102
103 switch (implementation) {
104 /* TODO: have only one "solver" object of type MnaSolverDirect and only use setDirectLinearSolverImplementation in the switch-case.
105 * This is not done now, since MnaSolverDirect and MnaSolver are distinct classes - and someone might add another subclass of MnaSolver
106 * to the project (MnaSolverIterative?). It is planned to merge MnaSolverDirect and MnaSolver anyway, so this won't happen.
107 */
108#ifdef WITH_SPARSE
110 SPDLOG_LOGGER_INFO(log, "creating SparseLUAdapter solver implementation");
111 std::shared_ptr<MnaSolverDirect<VarType>> sparseSolver =
112 std::make_shared<MnaSolverDirect<VarType>>(name, domain, logLevel);
113 sparseSolver->setDirectLinearSolverImplementation(
115 return sparseSolver;
116 }
117#endif // WITH_SPARSE
119 SPDLOG_LOGGER_INFO(log, "creating DenseLUAdapter solver implementation");
120 std::shared_ptr<MnaSolverDirect<VarType>> denseSolver =
121 std::make_shared<MnaSolverDirect<VarType>>(name, domain, logLevel);
122 denseSolver->setDirectLinearSolverImplementation(
124 return denseSolver;
125 }
126#ifdef WITH_KLU
128 SPDLOG_LOGGER_INFO(log, "creating KLUAdapter solver implementation");
129 std::shared_ptr<MnaSolverDirect<VarType>> kluSolver =
130 std::make_shared<MnaSolverDirect<VarType>>(name, domain, logLevel);
131 kluSolver->setDirectLinearSolverImplementation(
133 return kluSolver;
134 }
135#endif
136#ifdef WITH_CUDA
138 SPDLOG_LOGGER_INFO(log, "creating GpuDenseAdapter solver implementation");
139 std::shared_ptr<MnaSolverDirect<VarType>> gpuDenseSolver =
140 std::make_shared<MnaSolverDirect<VarType>>(name, domain, logLevel);
141 gpuDenseSolver->setDirectLinearSolverImplementation(
143 return gpuDenseSolver;
144 }
145#ifdef WITH_CUDA_SPARSE
147 SPDLOG_LOGGER_INFO(log,
148 "creating GpuSparseAdapter solver implementation");
149 std::shared_ptr<MnaSolverDirect<VarType>> gpuSparseSolver =
150 std::make_shared<MnaSolverDirect<VarType>>(name, domain, logLevel);
151 gpuSparseSolver->setDirectLinearSolverImplementation(
153 return gpuSparseSolver;
154 }
155#endif
156#ifdef WITH_MAGMA
158 SPDLOG_LOGGER_INFO(log, "creating GpuMagmaAdapter solver implementation");
159 std::shared_ptr<MnaSolverDirect<VarType>> gpuMagmaSolver =
160 std::make_shared<MnaSolverDirect<VarType>>(name, domain, logLevel);
161 gpuMagmaSolver->setDirectLinearSolverImplementation(
163 return gpuMagmaSolver;
164 }
165#endif
166#endif
167#ifdef WITH_METAL
169 SPDLOG_LOGGER_INFO(log,
170 "creating MetalDenseAdapter solver implementation");
171 std::shared_ptr<MnaSolverDirect<VarType>> metalSolver =
172 std::make_shared<MnaSolverDirect<VarType>>(name, domain, logLevel);
173 metalSolver->setDirectLinearSolverImplementation(
175 return metalSolver;
176 }
178 SPDLOG_LOGGER_INFO(
179 log, "creating MetalSparseBiCGSTABAdapter solver implementation");
180 std::shared_ptr<MnaSolverDirect<VarType>> metalSolver =
181 std::make_shared<MnaSolverDirect<VarType>>(name, domain, logLevel);
182 metalSolver->setDirectLinearSolverImplementation(
184 return metalSolver;
185 }
186#endif
187#ifdef WITH_HIP
189 SPDLOG_LOGGER_INFO(log,
190 "creating HipSparseQrAdapter solver implementation");
191 std::shared_ptr<MnaSolverDirect<VarType>> hipSparseQrSolver =
192 std::make_shared<MnaSolverDirect<VarType>>(name, domain, logLevel);
193 hipSparseQrSolver->setDirectLinearSolverImplementation(
195 return hipSparseQrSolver;
196 }
198 SPDLOG_LOGGER_INFO(
199 log, "creating HipSparseBiCGStabAdapter solver implementation");
200 std::shared_ptr<MnaSolverDirect<VarType>> hipSparseSolver =
201 std::make_shared<MnaSolverDirect<VarType>>(name, domain, logLevel);
202 hipSparseSolver->setDirectLinearSolverImplementation(
204 return hipSparseSolver;
205 }
207#ifdef WITH_KLU
208 SPDLOG_LOGGER_INFO(
209 log, "creating HipSparseLURefactorAdapter solver implementation");
210 std::shared_ptr<MnaSolverDirect<VarType>> hipSparseSolver =
211 std::make_shared<MnaSolverDirect<VarType>>(name, domain, logLevel);
212 hipSparseSolver->setDirectLinearSolverImplementation(
214 return hipSparseSolver;
215#else
216 throw CPS::SystemError(
217 "HIP sparse LU refactorization requires the KLU seed solver");
218#endif
219 }
221 SPDLOG_LOGGER_INFO(log, "creating HipDenseAdapter solver implementation");
222 std::shared_ptr<MnaSolverDirect<VarType>> hipDenseSolver =
223 std::make_shared<MnaSolverDirect<VarType>>(name, domain, logLevel);
224 hipDenseSolver->setDirectLinearSolverImplementation(
226 return hipDenseSolver;
227 }
228#endif
229#ifdef WITH_MNASOLVERPLUGIN
231 SPDLOG_LOGGER_INFO(log, "creating Plugin solver implementation");
232 return std::make_shared<MnaSolverPlugin<VarType>>(pluginName, name,
233 domain, logLevel);
234#endif
235 default:
236 throw CPS::SystemError("unsupported MNA implementation.");
237 }
238 }
239};
240} // namespace DPsim
spdlog::level::level_enum Level
Definition Logger.h:33
std::shared_ptr< spdlog::logger > Log
Definition Logger.h:34
static Log get(const std::string &name, Level filelevel=Level::info, Level clilevel=Level::off)
Definition Logger.cpp:139
static const std::vector< DirectLinearSolverImpl > mSupportedSolverImpls(void)
MNA implementations supported by this compilation.
static std::shared_ptr< MnaSolver< VarType > > factory(String name, CPS::Domain domain=CPS::Domain::DP, CPS::Logger::Level logLevel=CPS::Logger::Level::info, DirectLinearSolverImpl implementation=mSupportedSolverImpls().back(), String pluginName="plugin.so")
sovlerImpl: choose the most advanced solver implementation available by default
CPS::String String
Definition Definitions.h:20
DirectLinearSolverImpl
@ HIPSparseLURefactor
@ HIPSparseBiCGStab
@ MetalSparseBiCGSTAB