DPsim
Loading...
Searching...
No Matches
MNASolverFactory.h
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>
12#include <dpsim/DenseLUAdapter.h>
13#include <dpsim/DirectLinearSolverConfiguration.h>
14#include <dpsim/MNASolver.h>
15#include <dpsim/MNASolverDirect.h>
16#ifdef WITH_SPARSE
17#include <dpsim/SparseLUAdapter.h>
18#endif
19#ifdef WITH_KLU
20#include <dpsim/KLUAdapter.h>
21#endif
22#ifdef WITH_CUDA
23#include <dpsim/GpuDenseAdapter.h>
24#ifdef WITH_CUDA_SPARSE
25#include <dpsim/GpuSparseAdapter.h>
26#endif
27#ifdef WITH_MAGMA
28#include <dpsim/GpuMagmaAdapter.h>
29#endif
30#endif
31#ifdef WITH_MNASOLVERPLUGIN
32#include <dpsim/MNASolverPlugin.h>
33#endif
34
35namespace DPsim {
36
38public:
40 static const std::vector<DirectLinearSolverImpl> mSupportedSolverImpls(void) {
41 static std::vector<DirectLinearSolverImpl> ret = {
42#ifdef WITH_MNASOLVERPLUGIN
43 DirectLinearSolverImpl::Plugin,
44#endif //WITH_MNASOLVERPLUGIN
45#ifdef WITH_CUDA
46 DirectLinearSolverImpl::CUDADense,
47#ifdef WITH_CUDA_SPARSE
48#endif // WITH_CUDA_SPARSE
49 DirectLinearSolverImpl::CUDASparse,
50#ifdef WITH_MAGMA
51 DirectLinearSolverImpl::CUDAMagma,
52#endif // WITH_MAGMA
53#endif // WITH_CUDA
54 DirectLinearSolverImpl::DenseLU,
55#ifdef WITH_SPARSE
56 DirectLinearSolverImpl::SparseLU,
57#endif // WITH_SPARSE
58#ifdef WITH_KLU
59 DirectLinearSolverImpl::KLU
60#endif // WITH_KLU
61 };
62 return ret;
63 }
64
66 template <typename VarType>
67 static std::shared_ptr<MnaSolver<VarType>> factory(
68 String name, CPS::Domain domain = CPS::Domain::DP,
69 CPS::Logger::Level logLevel = CPS::Logger::Level::info,
70 DirectLinearSolverImpl implementation = mSupportedSolverImpls().back(),
71 String pluginName = "plugin.so") {
72 //To avoid regression we use KLU in case of undefined implementation
73 if (implementation == DirectLinearSolverImpl::Undef) {
74#ifdef WITH_KLU
75 implementation = DirectLinearSolverImpl::KLU;
76#else
77 implementation = mSupportedSolverImpls().back();
78#endif
79 }
80 CPS::Logger::Log log = CPS::Logger::get(
81 "MnaSolverFactory", CPS::Logger::Level::info, CPS::Logger::Level::info);
82
83 switch (implementation) {
84 /* TODO: have only one "solver" object of type MnaSolverDirect and only use setDirectLinearSolverImplementation in the switch-case.
85 * This is not done now, since MnaSolverDirect and MnaSolver are distinct classes - and someone might add another subclass of MnaSolver
86 * to the project (MnaSolverIterative?). It is planned to merge MnaSolverDirect and MnaSolver anyway, so this won't happen.
87 */
88#ifdef WITH_SPARSE
89 case DirectLinearSolverImpl::SparseLU: {
90 SPDLOG_LOGGER_INFO(log, "creating SparseLUAdapter solver implementation");
91 std::shared_ptr<MnaSolverDirect<VarType>> sparseSolver =
92 std::make_shared<MnaSolverDirect<VarType>>(name, domain, logLevel);
93 sparseSolver->setDirectLinearSolverImplementation(
94 DirectLinearSolverImpl::SparseLU);
95 return sparseSolver;
96 }
97#endif // WITH_SPARSE
98 case DirectLinearSolverImpl::DenseLU: {
99 SPDLOG_LOGGER_INFO(log, "creating DenseLUAdapter solver implementation");
100 std::shared_ptr<MnaSolverDirect<VarType>> denseSolver =
101 std::make_shared<MnaSolverDirect<VarType>>(name, domain, logLevel);
102 denseSolver->setDirectLinearSolverImplementation(
103 DirectLinearSolverImpl::DenseLU);
104 return denseSolver;
105 }
106#ifdef WITH_KLU
107 case DirectLinearSolverImpl::KLU: {
108 SPDLOG_LOGGER_INFO(log, "creating KLUAdapter solver implementation");
109 std::shared_ptr<MnaSolverDirect<VarType>> kluSolver =
110 std::make_shared<MnaSolverDirect<VarType>>(name, domain, logLevel);
111 kluSolver->setDirectLinearSolverImplementation(
112 DirectLinearSolverImpl::KLU);
113 return kluSolver;
114 }
115#endif
116#ifdef WITH_CUDA
117 case DirectLinearSolverImpl::CUDADense: {
118 SPDLOG_LOGGER_INFO(log, "creating GpuDenseAdapter solver implementation");
119 std::shared_ptr<MnaSolverDirect<VarType>> gpuDenseSolver =
120 std::make_shared<MnaSolverDirect<VarType>>(name, domain, logLevel);
121 gpuDenseSolver->setDirectLinearSolverImplementation(
122 DirectLinearSolverImpl::CUDADense);
123 return gpuDenseSolver;
124 }
125#ifdef WITH_CUDA_SPARSE
126 case DirectLinearSolverImpl::CUDASparse: {
127 SPDLOG_LOGGER_INFO(log,
128 "creating GpuSparseAdapter solver implementation");
129 std::shared_ptr<MnaSolverDirect<VarType>> gpuSparseSolver =
130 std::make_shared<MnaSolverDirect<VarType>>(name, domain, logLevel);
131 gpuSparseSolver->setDirectLinearSolverImplementation(
132 DirectLinearSolverImpl::CUDASparse);
133 return gpuSparseSolver;
134 }
135#endif
136#ifdef WITH_MAGMA
137 case DirectLinearSolverImpl::CUDAMagma: {
138 SPDLOG_LOGGER_INFO(log, "creating GpuMagmaAdapter solver implementation");
139 std::shared_ptr<MnaSolverDirect<VarType>> gpuMagmaSolver =
140 std::make_shared<MnaSolverDirect<VarType>>(name, domain, logLevel);
141 gpuMagmaSolver->setDirectLinearSolverImplementation(
142 DirectLinearSolverImpl::CUDAMagma);
143 return gpuMagmaSolver;
144 }
145#endif
146#endif
147#ifdef WITH_MNASOLVERPLUGIN
148 case DirectLinearSolverImpl::Plugin:
149 SPDLOG_LOGGER_INFO(log, "creating Plugin solver implementation");
150 return std::make_shared<MnaSolverPlugin<VarType>>(pluginName, name,
151 domain, logLevel);
152#endif
153 default:
154 throw CPS::SystemError("unsupported MNA implementation.");
155 }
156 }
157};
158} // namespace DPsim
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