DPsim
Loading...
Searching...
No Matches
MNASolver.cpp
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#include <algorithm>
10#include <dpsim/MNASolver.h>
11#include <dpsim/SequentialScheduler.h>
12#include <functional>
13#include <memory>
14#include <stdexcept>
15#include <type_traits>
16
17using namespace DPsim;
18using namespace CPS;
19
20namespace DPsim {
21
22template <typename VarType>
23MnaSolver<VarType>::MnaSolver(String name, CPS::Domain domain,
24 CPS::Logger::Level logLevel)
25 : Solver(name, logLevel), mDomain(domain) {
26
27 // Raw source and solution vector logging
28 mLeftVectorLog = std::make_shared<DataLogger>(
29 name + "_LeftVector", logLevel == CPS::Logger::Level::trace);
30 mRightVectorLog = std::make_shared<DataLogger>(
31 name + "_RightVector", logLevel == CPS::Logger::Level::trace);
32}
33
34template <typename VarType>
35void MnaSolver<VarType>::setSystem(const CPS::SystemTopology &system) {
36 mSystem = system;
37}
38
39template <typename VarType>
43
44template <typename VarType>
48 throw std::logic_error(
49 "MNA state-space extractor has not been initialized.");
50
52}
53
54template <typename VarType> void MnaSolver<VarType>::initialize() {
55 // TODO: check that every system matrix has the same dimensions
56 SPDLOG_LOGGER_INFO(mSLog, "---- Start initialization ----");
57 mLeftVectorLog->start();
58 mRightVectorLog->start();
59
60 // Register attribute for solution vector
62 // Best case we have some kind of sub-attributes for attribute vectors / tensor attributes...
64 SPDLOG_LOGGER_INFO(mSLog, "Computing network harmonics in parallel.");
65 for (Int freq = 0; freq < mSystem.mFrequencies.size(); ++freq) {
67 }
68 } else {
70 }
71
72 SPDLOG_LOGGER_INFO(mSLog, "-- Process topology");
73 for (auto comp : mSystem.mComponents)
74 SPDLOG_LOGGER_INFO(mSLog, "Added {:s} '{:s}' to simulation.", comp->type(),
75 comp->name());
76
77 // Otherwise LU decomposition will fail
78 if (mSystem.mComponents.size() == 0)
79 throw SolverException();
80
81 // We need to differentiate between power and signal components and
82 // ground nodes should be ignored.
84 // Ensure all subcomponents (and their virtual nodes) are registered before
85 // collectVirtualNodes() sizes the system matrices. Recurses into nested
86 // sub-components so e.g. SST -> Load -> {R, L, C} is fully created.
87 CPS::MNAInterface::List allMNAComps;
88 allMNAComps.insert(allMNAComps.end(), mMNAComponents.begin(),
89 mMNAComponents.end());
90 allMNAComps.insert(allMNAComps.end(), mMNAIntfVariableComps.begin(),
92 allMNAComps.insert(allMNAComps.end(), mMNAIntfSwitches.begin(),
93 mMNAIntfSwitches.end());
94
95 // Some composites (e.g. AvVoltageSourceInverterDQ) eagerly create and
96 // register their subcomponents in their own constructor, before those
97 // subcomponents are connected (connect() only happens later, in
98 // initializeParentFromNodesAndTerminals()). Calling createSubComponents()
99 // on such a not-yet-connected subcomponent crashes, since it relies on
100 // its own terminals being wired up. Only subcomponents that are newly
101 // registered as a direct result of THIS createSubComponents() call (the
102 // lazy create+connect+register pattern, e.g. SST -> Load -> {R, L, C})
103 // are guaranteed to already be connected, so only recurse into those.
104 std::function<void(CPS::MNAInterface::Ptr)> createSubComponentsRec =
105 [&](CPS::MNAInterface::Ptr comp) {
106 auto pComp =
107 std::dynamic_pointer_cast<CPS::SimPowerComp<VarType>>(comp);
108 typename CPS::SimPowerComp<VarType>::List subCompsBefore =
109 pComp ? pComp->subComponents()
110 : typename CPS::SimPowerComp<VarType>::List();
111
112 comp->createSubComponents();
113
114 if (pComp) {
115 for (auto subComp : pComp->subComponents()) {
116 bool isNew = std::find(subCompsBefore.begin(), subCompsBefore.end(),
117 subComp) == subCompsBefore.end();
118 if (!isNew)
119 continue;
120 if (auto subMna =
121 std::dynamic_pointer_cast<CPS::MNAInterface>(subComp))
122 createSubComponentsRec(subMna);
123 }
124 }
125 };
126 for (auto comp : allMNAComps)
127 createSubComponentsRec(comp);
128 // These steps complete the network information.
131
132 SPDLOG_LOGGER_INFO(mSLog, "-- Create empty MNA system matrices and vectors");
135
136 // Initialize components from powerflow solution and
137 // calculate MNA specific initialization values.
139
140 if (mSteadyStateInit) {
141 mIsInInitialization = true;
142 steadyStateInitialization();
143 }
144 mIsInInitialization = false;
145
146 // Some components feature a different behaviour for simulation and initialization
147 for (auto comp : mSystem.mComponents) {
148 auto powerComp = std::dynamic_pointer_cast<CPS::TopologicalPowerComp>(comp);
149 if (powerComp)
150 powerComp->setBehaviour(TopologicalPowerComp::Behaviour::MNASimulation);
151
152 auto sigComp = std::dynamic_pointer_cast<CPS::SimSignalComp>(comp);
153 if (sigComp)
154 sigComp->setBehaviour(SimSignalComp::Behaviour::Simulation);
155 }
156
157 // Initialize system matrices and source vector.
159
162
163 SPDLOG_LOGGER_INFO(mSLog, "--- Initialization finished ---");
164 SPDLOG_LOGGER_INFO(mSLog, "--- Initial system matrices and vectors ---");
166
167 mSLog->flush();
168}
169
170template <> void MnaSolver<Real>::initializeComponents() {
171 SPDLOG_LOGGER_INFO(mSLog, "-- Initialize components from power flow");
172
173 CPS::MNAInterface::List allMNAComps;
174 allMNAComps.insert(allMNAComps.end(), mMNAComponents.begin(),
175 mMNAComponents.end());
176 allMNAComps.insert(allMNAComps.end(), mMNAIntfVariableComps.begin(),
177 mMNAIntfVariableComps.end());
178
179 for (auto comp : allMNAComps) {
180 auto pComp = std::dynamic_pointer_cast<SimPowerComp<Real>>(comp);
181 if (!pComp)
182 continue;
183 pComp->checkForUnconnectedTerminals();
184 if (mInitFromNodesAndTerminals)
185 pComp->initializeFromNodesAndTerminals(mSystem.mSystemFrequency);
186 }
187
188 // Initialize signal components.
189 for (auto comp : mSimSignalComps)
190 comp->initialize(mSystem.mSystemOmega, mTimeStep);
191
192 // Initialize MNA specific parts of components.
193 for (auto comp : allMNAComps) {
194 comp->mnaInitialize(mSystem.mSystemOmega, mTimeStep, mLeftSideVector);
195 const Matrix &stamp = comp->getRightVector()->get();
196 if (stamp.size() != 0) {
197 mRightVectorStamps.push_back(&stamp);
198 }
199 }
200
201 for (auto comp : mMNAIntfSwitches)
202 comp->mnaInitialize(mSystem.mSystemOmega, mTimeStep, mLeftSideVector);
203
204 // Initialize nodes
205 for (UInt nodeIdx = 0; nodeIdx < mNodes.size(); ++nodeIdx)
206 mNodes[nodeIdx]->initialize();
207}
208
210 SPDLOG_LOGGER_INFO(mSLog, "-- Initialize components from power flow");
211
212 CPS::MNAInterface::List allMNAComps;
213 allMNAComps.insert(allMNAComps.end(), mMNAComponents.begin(),
214 mMNAComponents.end());
215 allMNAComps.insert(allMNAComps.end(), mMNAIntfVariableComps.begin(),
216 mMNAIntfVariableComps.end());
217
218 // Initialize power components with frequencies and from powerflow results
219 for (auto comp : allMNAComps) {
220 auto pComp = std::dynamic_pointer_cast<SimPowerComp<Complex>>(comp);
221 if (!pComp)
222 continue;
223 pComp->checkForUnconnectedTerminals();
224 if (mInitFromNodesAndTerminals)
225 pComp->initializeFromNodesAndTerminals(mSystem.mSystemFrequency);
226 }
227
228 // Initialize signal components.
229 for (auto comp : mSimSignalComps)
230 comp->initialize(mSystem.mSystemOmega, mTimeStep);
231
232 SPDLOG_LOGGER_INFO(mSLog, "-- Initialize MNA properties of components");
233 if (mFrequencyParallel) {
234 // Initialize MNA specific parts of components.
235 for (auto comp : mMNAComponents) {
236 // Initialize MNA specific parts of components.
237 comp->mnaInitializeHarm(mSystem.mSystemOmega, mTimeStep,
238 mLeftSideVectorHarm);
239 const Matrix &stamp = comp->getRightVector()->get();
240 if (stamp.size() != 0)
241 mRightVectorStamps.push_back(&stamp);
242 }
243 // Initialize nodes
244 for (UInt nodeIdx = 0; nodeIdx < mNodes.size(); ++nodeIdx) {
245 mNodes[nodeIdx]->mnaInitializeHarm(mLeftSideVectorHarm);
246 }
247 } else {
248 // Initialize MNA specific parts of components.
249 for (auto comp : allMNAComps) {
250 comp->mnaInitialize(mSystem.mSystemOmega, mTimeStep, mLeftSideVector);
251 const Matrix &stamp = comp->getRightVector()->get();
252 if (stamp.size() != 0) {
253 mRightVectorStamps.push_back(&stamp);
254 }
255 }
256
257 for (auto comp : mMNAIntfSwitches)
258 comp->mnaInitialize(mSystem.mSystemOmega, mTimeStep, mLeftSideVector);
259
260 // Initialize nodes
261 for (UInt nodeIdx = 0; nodeIdx < mNodes.size(); ++nodeIdx)
262 mNodes[nodeIdx]->initialize();
263 }
264}
265
266template <typename VarType> void MnaSolver<VarType>::initializeSystem() {
267 SPDLOG_LOGGER_INFO(mSLog,
268 "-- Initialize MNA system matrices and source vector");
269 mRightSideVector.setZero();
270
271 // just a sanity check in case we change the static
272 // initialization of the switch number in the future
273 if (mSwitches.size() > sizeof(std::size_t) * 8) {
274 throw SystemError("Too many Switches.");
275 }
276
281 else
283}
284
285template <typename VarType>
287 // iterate over all possible switch state combinations and frequencies
288 for (std::size_t sw = 0; sw < (1ULL << mSwitches.size()); ++sw) {
289 for (Int freq = 0; freq < mSystem.mFrequencies.size(); ++freq) {
290 switchedMatrixEmpty(sw, freq);
292 }
293 }
294
295 if (mSwitches.size() > 0)
297
298 // Initialize source vector
299 for (Int freq = 0; freq < mSystem.mFrequencies.size(); ++freq) {
300 for (auto comp : mMNAComponents)
301 comp->mnaApplyRightSideVectorStampHarm(mRightSideVectorHarm[freq], freq);
302 }
303}
304
305template <typename VarType>
307 // iterate over all possible switch state combinations
308 for (std::size_t i = 0; i < (1ULL << mSwitches.size()); i++) {
310 }
311
312 if (mSwitches.size() < 1) {
314 } else {
315 // Generate switching state dependent system matrices
316 for (std::size_t i = 0; i < (1ULL << mSwitches.size()); i++) {
318 }
320 }
321
322 // Initialize source vector for debugging
323 // CAUTION: this does not always deliver proper source vector initialization
324 // as not full pre-step is executed (not involving necessary electrical or signal
325 // subcomp updates before right vector calculation)
326 for (auto comp : mMNAComponents) {
327 comp->mnaApplyRightSideVectorStamp(mRightSideVector);
328 auto idObj = std::dynamic_pointer_cast<IdentifiedObject>(comp);
329 SPDLOG_LOGGER_DEBUG(mSLog, "Stamping {:s} {:s} into source vector",
330 idObj->type(), idObj->name());
331 if (mSLog->should_log(spdlog::level::trace))
332 mSLog->trace("\n{:s}", Logger::matrixToString(mRightSideVector));
333 }
334}
335
336template <typename VarType>
338
339 // Collect index pairs of varying matrix entries from components
340 for (auto varElem : mVariableComps)
341 for (auto varEntry : varElem->mVariableSystemMatrixEntries)
342 mListVariableSystemMatrixEntries.push_back(varEntry);
343 SPDLOG_LOGGER_INFO(mSLog, "List of index pairs of varying matrix entries: ");
344 for (auto indexPair : mListVariableSystemMatrixEntries)
345 SPDLOG_LOGGER_INFO(mSLog, "({}, {})", indexPair.first, indexPair.second);
346
348
349 // Initialize source vector for debugging
350 // CAUTION: this does not always deliver proper source vector initialization
351 // as not full pre-step is executed (not involving necessary electrical or signal
352 // subcomp updates before right vector calculation)
353 for (auto comp : mMNAComponents) {
354 comp->mnaApplyRightSideVectorStamp(mRightSideVector);
355 auto idObj = std::dynamic_pointer_cast<IdentifiedObject>(comp);
356 SPDLOG_LOGGER_DEBUG(mSLog, "Stamping {:s} {:s} into source vector",
357 idObj->type(), idObj->name());
358 if (mSLog->should_log(spdlog::level::trace))
359 mSLog->trace("\n{:s}", Logger::matrixToString(mRightSideVector));
360 }
361}
362
363template <typename VarType>
365 if (mDomain != CPS::Domain::EMT && mDomain != CPS::Domain::DP) {
366 throw std::logic_error(
367 "MNA state-space extraction supports EMT and DP domains only.");
368 }
369
370 if (mFrequencyParallel) {
371 throw std::logic_error(
372 "MNA state-space extraction does not support frequency-parallel "
373 "MNA systems.");
374 }
375
376 CPS::MNAInterface::List stateSpaceComponents;
377 stateSpaceComponents.insert(stateSpaceComponents.end(),
378 mMNAComponents.begin(), mMNAComponents.end());
379 stateSpaceComponents.insert(stateSpaceComponents.end(),
380 mMNAIntfVariableComps.begin(),
382
383 const UInt mnaVectorSize = static_cast<UInt>((**mLeftSideVector).rows());
384
385 mStateSpaceExtractor = std::make_shared<MNAStateSpaceExtractor>();
386 mStateSpaceExtractor->initialize(stateSpaceComponents, mnaVectorSize,
387 mTimeStep);
388
389 SPDLOG_LOGGER_INFO(
390 mSLog,
391 "Initialized MNA state-space extractor with {:d} extraction states.",
392 mStateSpaceExtractor->getStateCount());
393}
394
395template <typename VarType>
397 for (auto varElem : mVariableComps) {
398 if (varElem->hasParameterChanged()) {
399 auto idObj = std::dynamic_pointer_cast<IdentifiedObject>(varElem);
400 SPDLOG_LOGGER_DEBUG(
401 mSLog, "Component ({:s} {:s}) value changed -> Update System Matrix",
402 idObj->type(), idObj->name());
403 return true;
404 }
405 }
406 return false;
407}
408
409template <typename VarType> void MnaSolver<VarType>::updateSwitchStatus() {
410 for (UInt i = 0; i < mSwitches.size(); ++i) {
411 mCurrentSwitchStatus.set(i, mSwitches[i]->mnaIsClosed());
412 }
413}
414
415template <typename VarType> void MnaSolver<VarType>::identifyTopologyObjects() {
416 for (auto baseNode : mSystem.mNodes) {
417 // Add nodes to the list and ignore ground nodes.
418 if (!baseNode->isGround()) {
419 auto node = std::dynamic_pointer_cast<CPS::SimNode<VarType>>(baseNode);
420 mNodes.push_back(node);
421 SPDLOG_LOGGER_INFO(mSLog, "Added node {:s}", node->name());
422 }
423 }
424
425 for (auto comp : mSystem.mComponents) {
426
427 auto genComp = std::dynamic_pointer_cast<CPS::MNASyncGenInterface>(comp);
428 if (genComp) {
429 mSyncGen.push_back(genComp);
430 }
431
432 auto swComp = std::dynamic_pointer_cast<CPS::MNASwitchInterface>(comp);
433 if (swComp) {
434 mSwitches.push_back(swComp);
435 auto mnaComp = std::dynamic_pointer_cast<CPS::MNAInterface>(swComp);
436 if (mnaComp)
437 mMNAIntfSwitches.push_back(mnaComp);
438 }
439
440 auto varComp =
441 std::dynamic_pointer_cast<CPS::MNAVariableCompInterface>(comp);
442 if (varComp) {
443 mVariableComps.push_back(varComp);
444 auto mnaComp = std::dynamic_pointer_cast<CPS::MNAInterface>(varComp);
445 if (mnaComp)
446 mMNAIntfVariableComps.push_back(mnaComp);
447 }
448
449 if (!(swComp || varComp)) {
450 auto mnaComp = std::dynamic_pointer_cast<CPS::MNAInterface>(comp);
451 if (mnaComp)
452 mMNAComponents.push_back(mnaComp);
453
454 auto sigComp = std::dynamic_pointer_cast<CPS::SimSignalComp>(comp);
455 if (sigComp)
456 mSimSignalComps.push_back(sigComp);
457 }
458 }
459}
460
461template <typename VarType> void MnaSolver<VarType>::assignMatrixNodeIndices() {
462 UInt matrixNodeIndexIdx = 0;
463 for (UInt idx = 0; idx < mNodes.size(); ++idx) {
464 mNodes[idx]->setMatrixNodeIndex(0, matrixNodeIndexIdx);
465 SPDLOG_LOGGER_INFO(mSLog, "Assigned index {} to phase A of node {}",
466 matrixNodeIndexIdx, idx);
467 ++matrixNodeIndexIdx;
468 if (mNodes[idx]->phaseType() == CPS::PhaseType::ABC) {
469 mNodes[idx]->setMatrixNodeIndex(1, matrixNodeIndexIdx);
470 SPDLOG_LOGGER_INFO(mSLog, "Assigned index {} to phase B of node {}",
471 matrixNodeIndexIdx, idx);
472 ++matrixNodeIndexIdx;
473 mNodes[idx]->setMatrixNodeIndex(2, matrixNodeIndexIdx);
474 SPDLOG_LOGGER_INFO(mSLog, "Assigned index {} to phase C of node {}",
475 matrixNodeIndexIdx, idx);
476 ++matrixNodeIndexIdx;
477 }
478 // This should be true when the final network node is reached, not considering virtual nodes
479 if (idx == mNumNetNodes - 1)
480 mNumNetMatrixNodeIndices = matrixNodeIndexIdx;
481 }
482 // Total number of network nodes including virtual nodes is matrixNodeIndexIdx + 1, which is why the variable is incremented after assignment
483 mNumMatrixNodeIndices = matrixNodeIndexIdx;
487 static_cast<UInt>(mSystem.mFrequencies.size() - 1) *
490 static_cast<UInt>(mSystem.mFrequencies.size()) * mNumMatrixNodeIndices;
491
492 SPDLOG_LOGGER_INFO(mSLog, "Assigned simulation nodes to topology nodes:");
493 SPDLOG_LOGGER_INFO(mSLog, "Number of network simulation nodes: {:d}",
495 SPDLOG_LOGGER_INFO(mSLog, "Number of simulation nodes: {:d}",
497 SPDLOG_LOGGER_INFO(mSLog, "Number of harmonic simulation nodes: {:d}",
499}
500
501template <> void MnaSolver<Real>::createEmptyVectors() {
502 mRightSideVector = Matrix::Zero(mNumMatrixNodeIndices, 1);
503 **mLeftSideVector = Matrix::Zero(mNumMatrixNodeIndices, 1);
504}
505
507 if (mFrequencyParallel) {
508 for (Int freq = 0; freq < mSystem.mFrequencies.size(); ++freq) {
509 mRightSideVectorHarm.push_back(
510 Matrix::Zero(2 * (mNumMatrixNodeIndices), 1));
511 mLeftSideVectorHarm.push_back(AttributeStatic<Matrix>::make(
512 Matrix::Zero(2 * (mNumMatrixNodeIndices), 1)));
513 }
514 } else {
515 mRightSideVector = Matrix::Zero(
516 2 * (mNumMatrixNodeIndices + mNumHarmMatrixNodeIndices), 1);
517 **mLeftSideVector = Matrix::Zero(
518 2 * (mNumMatrixNodeIndices + mNumHarmMatrixNodeIndices), 1);
519 }
520}
521
522template <typename VarType> void MnaSolver<VarType>::collectVirtualNodes() {
523 // We have not added virtual nodes yet so the list has only network nodes
524 mNumNetNodes = (UInt)mNodes.size();
525 // virtual nodes are placed after network nodes
526 UInt virtualNode = mNumNetNodes - 1;
527
528 for (auto comp : mMNAComponents) {
529 auto pComp = std::dynamic_pointer_cast<SimPowerComp<VarType>>(comp);
530 if (!pComp)
531 continue;
532
533 // Check if component requires virtual node and if so get a reference
534 if (pComp->hasVirtualNodes()) {
535 for (UInt node = 0; node < pComp->virtualNodesNumber(); ++node) {
536 mNodes.push_back(pComp->virtualNode(node));
537 SPDLOG_LOGGER_INFO(mSLog, "Collected virtual node {} of {}",
538 virtualNode, node, pComp->name());
539 }
540 }
541
542 // Repeat the same steps for virtual nodes of sub components
543 // TODO: recursive behavior
544 if (pComp->hasSubComponents()) {
545 for (auto pSubComp : pComp->subComponents()) {
546 for (UInt node = 0; node < pSubComp->virtualNodesNumber(); ++node) {
547 auto vnode = pSubComp->virtualNode(node);
548 // Skip if already registered (e.g. parent reused its VN via
549 // setVirtualNodeAt).
550 bool alreadyRegistered = false;
551 for (auto registeredNode : mNodes) {
552 if (registeredNode == vnode) {
553 alreadyRegistered = true;
554 break;
555 }
556 }
557 if (alreadyRegistered)
558 continue;
559 mNodes.push_back(vnode);
560 SPDLOG_LOGGER_INFO(mSLog, "Collected virtual node {} of {}", node,
561 pSubComp->name());
562 }
563 }
564 }
565 }
566
567 // collect virtual nodes of variable components
568 for (auto comp : mVariableComps) {
569 auto pComp = std::dynamic_pointer_cast<SimPowerComp<VarType>>(comp);
570 if (!pComp)
571 continue;
572
573 // Check if component requires virtual node and if so get a reference
574 if (pComp->hasVirtualNodes()) {
575 for (UInt node = 0; node < pComp->virtualNodesNumber(); ++node) {
576 mNodes.push_back(pComp->virtualNode(node));
577 SPDLOG_LOGGER_INFO(mSLog,
578 "Collected virtual node {} of Varible Comp {}", node,
579 pComp->name());
580 }
581 }
582 }
583
584 // Update node number to create matrices and vectors
585 mNumNodes = (UInt)mNodes.size();
587 SPDLOG_LOGGER_INFO(mSLog, "Created virtual nodes:");
588 SPDLOG_LOGGER_INFO(mSLog, "Number of network nodes: {:d}", mNumNetNodes);
589 SPDLOG_LOGGER_INFO(mSLog, "Number of network and virtual nodes: {:d}",
590 mNumNodes);
591}
592
593template <typename VarType>
594void MnaSolver<VarType>::steadyStateInitialization() {
595 SPDLOG_LOGGER_INFO(mSLog, "--- Run steady-state initialization ---");
596
597 DataLogger initLeftVectorLog(mName + "_InitLeftVector",
598 mLogLevel != CPS::Logger::Level::off);
599 initLeftVectorLog.start();
600 DataLogger initRightVectorLog(mName + "_InitRightVector",
601 mLogLevel != CPS::Logger::Level::off);
602 initRightVectorLog.start();
603
604 TopologicalPowerComp::Behaviour initBehaviourPowerComps =
605 TopologicalPowerComp::Behaviour::Initialization;
606 SimSignalComp::Behaviour initBehaviourSignalComps =
607 SimSignalComp::Behaviour::Initialization;
608
609 // TODO: enable use of timestep distinct from simulation timestep
610 Real initTimeStep = mTimeStep;
611
612 Int timeStepCount = 0;
613 Real time = 0;
614 Real maxDiff = 1.0;
615 Real max = 1.0;
616 Matrix diff = Matrix::Zero(2 * mNumNodes, 1);
617 Matrix prevLeftSideVector = Matrix::Zero(2 * mNumNodes, 1);
618
619 SPDLOG_LOGGER_INFO(mSLog,
620 "Time step is {:f}s for steady-state initialization",
621 initTimeStep);
622
623 for (auto comp : mSystem.mComponents) {
624 auto powerComp = std::dynamic_pointer_cast<CPS::TopologicalPowerComp>(comp);
625 if (powerComp)
626 powerComp->setBehaviour(initBehaviourPowerComps);
627
628 auto sigComp = std::dynamic_pointer_cast<CPS::SimSignalComp>(comp);
629 if (sigComp)
630 sigComp->setBehaviour(initBehaviourSignalComps);
631 }
632
633 initializeSystem();
634 logSystemMatrices();
635
636 // Use sequential scheduler
638 CPS::Task::List tasks;
639 Scheduler::Edges inEdges, outEdges;
640
641 for (auto node : mNodes) {
642 for (auto task : node->mnaTasks())
643 tasks.push_back(task);
644 }
645 for (auto comp : mMNAComponents) {
646 for (auto task : comp->mnaTasks()) {
647 tasks.push_back(task);
648 }
649 }
650 // TODO signal components should be moved out of MNA solver
651 for (auto comp : mSimSignalComps) {
652 for (auto task : comp->getTasks()) {
653 tasks.push_back(task);
654 }
655 }
656 tasks.push_back(createSolveTask());
657
658 sched.resolveDeps(tasks, inEdges, outEdges);
659 sched.createSchedule(tasks, inEdges, outEdges);
660
661 while (time < mSteadStIniTimeLimit) {
662 // Reset source vector
663 mRightSideVector.setZero();
664
665 sched.step(time, timeStepCount);
666
667 if (mDomain == CPS::Domain::EMT) {
668 initLeftVectorLog.logEMTNodeValues(time, leftSideVector());
669 initRightVectorLog.logEMTNodeValues(time, rightSideVector());
670 } else {
671 initLeftVectorLog.logPhasorNodeValues(time, leftSideVector());
672 initRightVectorLog.logPhasorNodeValues(time, rightSideVector());
673 }
674
675 // Calculate new simulation time
676 time = time + initTimeStep;
677 ++timeStepCount;
678
679 // Calculate difference
680 diff = prevLeftSideVector - **mLeftSideVector;
681 prevLeftSideVector = **mLeftSideVector;
682 maxDiff = diff.lpNorm<Eigen::Infinity>();
683 max = (**mLeftSideVector).lpNorm<Eigen::Infinity>();
684 // If difference is smaller than some epsilon, break
685 if ((maxDiff / max) < mSteadStIniAccLimit)
686 break;
687 }
688
689 SPDLOG_LOGGER_INFO(mSLog, "Max difference: {:f} or {:f}% at time {:f}",
690 maxDiff, maxDiff / max, time);
691
692 // Reset system for actual simulation
693 mRightSideVector.setZero();
694
695 SPDLOG_LOGGER_INFO(mSLog, "--- Finished steady-state initialization ---");
696}
697
698template <typename VarType> Task::List MnaSolver<VarType>::getTasks() {
699 Task::List l;
700
701 for (auto comp : mMNAComponents) {
702 for (auto task : comp->mnaTasks()) {
703 l.push_back(task);
704 }
705 }
706 for (auto comp : mMNAIntfSwitches) {
707 for (auto task : comp->mnaTasks()) {
708 l.push_back(task);
709 }
710 }
711 for (auto node : mNodes) {
712 for (auto task : node->mnaTasks())
713 l.push_back(task);
714 }
715 // TODO signal components should be moved out of MNA solver
716 for (auto comp : mSimSignalComps) {
717 for (auto task : comp->getTasks()) {
718 l.push_back(task);
719 }
720 }
721 if (mFrequencyParallel) {
722 for (UInt i = 0; i < mSystem.mFrequencies.size(); ++i)
723 l.push_back(createSolveTaskHarm(i));
724 } else if (mSystemMatrixRecomputation) {
725 for (auto comp : this->mMNAIntfVariableComps) {
726 for (auto task : comp->mnaTasks())
727 l.push_back(task);
728 }
729 l.push_back(createSolveTaskRecomp());
731 l.push_back(createStateSpaceExtractionTask());
732 }
733 } else {
734 l.push_back(createSolveTask());
736 l.push_back(createStateSpaceExtractionTask());
737 }
738 l.push_back(createLogTask());
739 }
740 return l;
741}
742
743template <typename VarType>
744void MnaSolver<VarType>::log(Real time, Int timeStepCount) {
745 if (mLogLevel == Logger::Level::off)
746 return;
747
748 if (mDomain == CPS::Domain::EMT) {
749 mLeftVectorLog->logEMTNodeValues(time, leftSideVector());
750 mRightVectorLog->logEMTNodeValues(time, rightSideVector());
751 } else {
752 mLeftVectorLog->logPhasorNodeValues(time, leftSideVector());
753 mRightVectorLog->logPhasorNodeValues(time, rightSideVector());
754 }
755}
756
757} // namespace DPsim
758
759template class DPsim::MnaSolver<Real>;
760template class DPsim::MnaSolver<Complex>;
Solver class using Modified Nodal Analysis (MNA).
Definition MNASolver.h:39
std::bitset< SWITCH_NUM > mCurrentSwitchStatus
Current status of all switches encoded as bitset.
Definition MNASolver.h:79
CPS::Domain mDomain
Simulation domain, which can be dynamic phasor (DP) or EMT.
Definition MNASolver.h:43
void identifyTopologyObjects()
Identify Nodes and SimPowerComps and SimSignalComps.
std::vector< Matrix > mRightSideVectorHarm
Source vector of known quantities.
Definition MNASolver.h:90
Matrix mRightSideVector
Source vector of known quantities.
Definition MNASolver.h:84
Bool mStateSpaceExtraction
Enables extraction of the MNA-coupled discrete-time state matrix.
Definition MNASolver.h:126
CPS::SystemTopology mSystem
System topology.
Definition MNASolver.h:64
virtual std::shared_ptr< CPS::Task > createStateSpaceExtractionTask()=0
Create state-space extraction task for this solver implementation.
void initializeSystemWithVariableMatrix()
Initialization of system matrices and source vector.
virtual void initialize() override
Calls subroutines to set up everything that is required before simulation.
Definition MNASolver.cpp:54
virtual void logSystemMatrices()=0
Logging of system matrices and source vector.
virtual void initializeSystem()
Initialization of system matrices and source vector.
std::vector< CPS::Attribute< Matrix >::Ptr > mLeftSideVectorHarm
Solution vector of unknown quantities (parallel frequencies)
Definition MNASolver.h:211
Bool hasVariableComponentChanged()
Checks whether the status of variable MNA elements have changed.
CPS::MNAInterface::List mMNAIntfVariableComps
List of variable components if they must be accessed as MNAInterface objects.
Definition MNASolver.h:99
UInt mNumNetMatrixNodeIndices
Number of network nodes, considering individual phases.
Definition MNASolver.h:53
UInt mNumNetNodes
Number of network nodes, single line equivalent.
Definition MNASolver.h:47
virtual void switchedMatrixStamp(std::size_t index, std::vector< std::shared_ptr< CPS::MNAInterface > > &comp)=0
Applies a component stamp to the matrix with the given switch index.
MNAStateSpaceExtractor::Ptr mStateSpaceExtractor
Extractor for the MNA-coupled state-space model.
Definition MNASolver.h:129
virtual void log(Real time, Int timeStepCount) override
Logs left and right vector.
UInt mNumTotalMatrixNodeIndices
Total number of network and virtual nodes, considering individual phases and additional frequencies.
Definition MNASolver.h:59
UInt mNumVirtualMatrixNodeIndices
Number of virtual nodes, considering individual phases.
Definition MNASolver.h:55
CPS::MNASyncGenInterface::List mSyncGen
List of synchronous generators that need iterate to solve the differential equations.
Definition MNASolver.h:81
CPS::MNAInterface::List mMNAIntfSwitches
List of switches if they must be accessed as MNAInterface objects.
Definition MNASolver.h:75
std::shared_ptr< DataLogger > mRightVectorLog
Right side vector logger.
Definition MNASolver.h:115
virtual std::shared_ptr< CPS::Task > createSolveTaskHarm(UInt freqIdx)=0
Create a solve task for this solver implementation.
void initializeComponents()
Initialization of individual components.
void updateSwitchStatus()
Collects the status of switches to select correct system matrix.
std::vector< std::pair< UInt, UInt > > mListVariableSystemMatrixEntries
List of index pairs of varying matrix entries.
Definition MNASolver.h:61
CPS::MNAVariableCompInterface::List mVariableComps
Definition MNASolver.h:97
virtual std::shared_ptr< CPS::Task > createSolveTaskRecomp()=0
Create a solve task for recomputation solver.
CPS::MNAInterface::List mMNAComponents
List of MNA components with static stamp into system matrix.
Definition MNASolver.h:70
virtual void stampVariableSystemMatrix()=0
Stamps components into the variable system matrix.
std::shared_ptr< DataLogger > mLeftVectorLog
Left side vector logger.
Definition MNASolver.h:113
UInt mNumMatrixNodeIndices
Number of network and virtual nodes, considering individual phases.
Definition MNASolver.h:51
void initializeSystemWithParallelFrequencies()
Initialization of system matrices and source vector.
void collectVirtualNodes()
UInt mNumHarmMatrixNodeIndices
Number of nodes, excluding the primary frequency.
Definition MNASolver.h:57
UInt mNumNodes
Number of network and virtual nodes, single line equivalent.
Definition MNASolver.h:45
MnaSolver(String name, CPS::Domain domain=CPS::Domain::DP, CPS::Logger::Level logLevel=CPS::Logger::Level::info)
Constructor should not be called by users but by Simulation.
Definition MNASolver.cpp:23
void assignMatrixNodeIndices()
Assign simulation node index according to index in the vector.
UInt mNumVirtualNodes
Number of virtual nodes, single line equivalent.
Definition MNASolver.h:49
void initializeSystemWithPrecomputedMatrices()
Initialization of system matrices and source vector.
CPS::Attribute< Matrix >::Ptr mLeftSideVector
Solution vector of unknown quantities.
Definition MNASolver.h:208
CPS::SimSignalComp::List mSimSignalComps
List of signal type components that do not directly interact with the MNA solver.
Definition MNASolver.h:77
virtual void createEmptySystemMatrix()=0
Create system matrix.
virtual std::shared_ptr< CPS::Task > createLogTask()=0
Create a solve task for this solver implementation.
const MNAStateSpaceExtractor & getStateSpaceExtractor() const
Read-only access to the MNA state-space extractor.
Definition MNASolver.cpp:46
void doStateSpaceExtraction(Bool value=true)
Enable or disable MNA state-space extraction.
Definition MNASolver.cpp:40
CPS::MNASwitchInterface::List mSwitches
Definition MNASolver.h:73
void createEmptyVectors()
Create left and right side vector.
virtual void switchedMatrixEmpty(std::size_t index)=0
Sets all entries in the matrix with the given switch index to zero.
virtual std::shared_ptr< CPS::Task > createSolveTask()=0
Create a solve task for this solver implementation.
CPS::SimNode< VarType >::List mNodes
List of simulation nodes.
Definition MNASolver.h:66
void initializeStateSpaceExtractor()
Initialization of state-space extraction.
virtual CPS::Task::List getTasks() override
Get tasks for scheduler.
void resolveDeps(CPS::Task::List &tasks, Edges &inEdges, Edges &outEdges)
Definition Scheduler.cpp:78
std::unordered_map< CPS::Task::Ptr, std::deque< CPS::Task::Ptr > > Edges
Definition Scheduler.h:31
void step(Real time, Int timeStepCount)
Performs a single simulation step.
void createSchedule(const CPS::Task::List &tasks, const Edges &inEdges, const Edges &outEdges)
Creates the schedule for the given dependency graph.
Real mTimeStep
Time step for fixed step solvers.
Definition Solver.h:47
Bool mSystemMatrixRecomputation
Enable recomputation of system matrix during simulation.
Definition Solver.h:64
CPS::Logger::Log mSLog
Logger.
Definition Solver.h:45
CPS::Logger::Level mLogLevel
Logging level.
Definition Solver.h:41
Bool mIsInInitialization
Determines if solver is in initialization phase, which requires different behavior.
Definition Solver.h:59
Bool mFrequencyParallel
Activates parallelized computation of frequencies.
Definition Solver.h:49
Bool mSteadyStateInit
Activates steady state initialization.
Definition Solver.h:57