DPsim
Loading...
Searching...
No Matches
EMT_DC_CurrentSource.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Institute for Automation of Complex Power Systems, EONERC, RWTH Aachen University
2// SPDX-License-Identifier: MPL-2.0
3
5
6using namespace CPS;
7
9 Logger::Level logLevel)
10 : MNASimPowerComp<Real>(uid, name, true, true, logLevel),
11 mCurrentRef(mAttributes->createDynamic<Real>("I_ref")) {
14 **mIntfVoltage = Matrix::Zero(1, 1);
15 **mIntfCurrent = Matrix::Zero(1, 1);
16 **mCurrentRef = 0.0;
17}
18
21 copy->setParameters(**mCurrentRef);
22 return copy;
23}
24
26 if (!Math::isFinite(current))
27 throw std::invalid_argument("DC source current must be finite.");
28 **mCurrentRef = current;
29 mParametersSet = true;
30}
31
32void EMT::DC::CurrentSource::validateDCTerminals() const {
33 for (UInt terminalIdx = 0; terminalIdx < 2; ++terminalIdx) {
34 const auto terminalNode =
35 const_cast<CurrentSource *>(this)->node(terminalIdx);
36 if (!terminalNode->isGround() && terminalNode->phaseType() != PhaseType::DC)
37 throw std::invalid_argument(
38 "DC current source requires DC nodes or ground at both terminals.");
39 }
40}
41
43 if (!mParametersSet)
44 throw std::logic_error(
45 "setParameters() must be called before source initialization.");
46 validateDCTerminals();
47 (**mIntfCurrent)(0, 0) = **mCurrentRef;
49 if (!Math::isFinite(voltage) ||
50 std::abs(voltage.imag()) > std::numeric_limits<Real>::epsilon())
51 throw std::invalid_argument(
52 "DC initial node voltages must be finite real scalar values.");
53 (**mIntfVoltage)(0, 0) = voltage.real();
54}
55
58 if (!mParametersSet)
59 throw std::logic_error(
60 "setParameters() must be called before source initialization.");
61 validateDCTerminals();
63 (**mIntfCurrent)(0, 0) = **mCurrentRef;
64}
65
75
77 (**mIntfVoltage)(0, 0) = 0.0;
79 (**mIntfVoltage)(0, 0) =
82 (**mIntfVoltage)(0, 0) -=
84 if (!Math::isFinite((**mIntfVoltage)(0, 0)))
85 throw std::runtime_error(
86 "DC current-source voltage contains a non-finite value.");
87}
88
91 throw std::runtime_error("DC source current became non-finite.");
92 (**mIntfCurrent)(0, 0) = **mCurrentRef;
94}
95
100
102 AttributeBase::List &prevStepDependencies,
103 AttributeBase::List &attributeDependencies,
104 AttributeBase::List &modifiedAttributes) {
105 // Keep the post-step terminal-voltage reconstruction in the schedule even
106 // when no logger or downstream component consumes the public output.
107 prevStepDependencies.push_back(mIntfVoltage);
108 attributeDependencies.push_back(mCurrentRef);
109 modifiedAttributes.push_back(mRightVector);
110 modifiedAttributes.push_back(mIntfCurrent);
111}
112
114 AttributeBase::List &, AttributeBase::List &attributeDependencies,
115 AttributeBase::List &modifiedAttributes,
116 Attribute<Matrix>::Ptr &leftVector) {
117 attributeDependencies.push_back(leftVector);
118 modifiedAttributes.push_back(mIntfVoltage);
119}
std::vector< Ptr > List
Definition Attribute.h:123
AttributePointer< Attribute< T > > Ptr
Definition Attribute.h:250
void mnaCompApplyRightSideVectorStamp(Matrix &rightVector) override final
void mnaCompInitialize(Real omega, Real timeStep, Attribute< Matrix >::Ptr leftVector) override
void initializeFromNodesAndTerminals(Real frequency) override
Initializes Component variables according to power flow data stored in Nodes.
void mnaCompPreStep(Real time, Int timeStepCount) override final
void mnaCompPostStep(Real time, Int timeStepCount, Attribute< Matrix >::Ptr &leftVector) override final
void mnaCompAddPreStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes) override final
void mnaCompAddPostStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes, Attribute< Matrix >::Ptr &leftVector) override final
void mnaCompUpdateVoltage(const Matrix &leftVector) override final
SimPowerComp< Real >::Ptr clone(String name) override final
Returns a modified copy of the component with the given suffix added to the name and without.
const Attribute< Real >::Ptr mCurrentRef
CurrentSource(String uid, String name, Logger::Level logLevel=Logger::Level::off)
String uid()
Returns unique id.
AttributeList::Ptr mAttributes
Attribute List.
spdlog::level::level_enum Level
Definition Logger.h:33
MNASimPowerComp(String uid, String name, Bool hasPreStep, Bool hasPostStep, Logger::Level logLevel)
Attribute< Matrix >::Ptr mRightVector
static Real realFromVectorElement(const Matrix &mat, Matrix::Index row)
static void setVectorElement(Matrix &mat, Matrix::Index row, Complex value, Int maxFreq=1, Int freqIdx=0, Matrix::Index colOffset=0)
Definition MathUtils.cpp:73
static bool isFinite(Real value)
Definition MathUtils.cpp:63
UInt matrixNodeIndex(UInt nodeIndex)
const Attribute< MatrixVar< Real > >::Ptr mIntfCurrent
const Attribute< MatrixVar< Real > >::Ptr mIntfVoltage
std::shared_ptr< SimPowerComp< VarType > > Ptr
Bool terminalNotGrounded(UInt index)
Complex initialSingleVoltage(UInt index)
void setTerminalNumber(UInt num)
Logger::Level mLogLevel
Component logger control for internal variables.
bool mParametersSet
Flag indicating that parameters are set via setParameters() function.
static std::shared_ptr< CurrentSource > make(Args &&...args)
Definition PtrFactory.h:19
Eigen::Matrix< Real, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor > Matrix
Dense matrix for real numbers.
Definition Definitions.h:81
std::string String
Definition Definitions.h:65
double Real
Definition Definitions.h:62
int Int
Definition Definitions.h:61
std::complex< Real > Complex
Definition Definitions.h:63
unsigned int UInt
Definition Definitions.h:60