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