Skip to content

Commit

Permalink
adjust code style
Browse files Browse the repository at this point in the history
- remove commented code and adjust parameter names for "setParameters" in "EMT_Ph3_CurrentSource.cpp"
- rename member variables in SSN components (camel-casing, 'm'-prefix)
- change access specifiers from protected to private for SSN component attributes and "ssnUpdateState()"
- remove "isLinear()" for linear SSN components
- set linear SSN component classes as final

Signed-off-by: Marvin Tollnitsch <[email protected]>
  • Loading branch information
MarvinTollnitschRWTH committed May 14, 2024
1 parent 7ed93a4 commit 5335eee
Show file tree
Hide file tree
Showing 10 changed files with 241 additions and 266 deletions.
27 changes: 12 additions & 15 deletions dpsim-models/include/dpsim-models/EMT/EMT_Ph1_SSN_Full_Serial_RLC.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,6 @@ namespace SSN {
public Base::Ph1::Capacitor,
public Base::Ph1::Resistor
{
protected:
Matrix State = Matrix::Zero(2, 1);
Matrix yHistory = Matrix::Zero(1, 1);

Matrix Dufour_u_n_t = Matrix::Zero(1, 1);

Matrix Dufour_A_k_hat = Matrix::Zero(2, 2);
Matrix Dufour_B_k_hat = Matrix::Zero(2, 1);
Matrix Dufour_B_k_n_hat = Matrix::Zero(2, 1);
Matrix Dufour_W_k_n = Matrix::Zero(1, 1);
Matrix Dufour_C_k_n = Matrix(1, 2);
public:
/// Defines UID, name, component parameters and logging level
Full_Serial_RLC(String uid, String name, Logger::Level logLevel = Logger::Level::off);
Expand Down Expand Up @@ -79,12 +68,20 @@ namespace SSN {
void mnaCompAddPreStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes) override;
/// Add MNA post step dependencies
void mnaCompAddPostStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes, Attribute<Matrix>::Ptr &leftVector) override;

private:
Matrix mState = Matrix::Zero(2, 1);
Matrix mYHistory = Matrix::Zero(1, 1);

Matrix mDufourUNT = Matrix::Zero(1, 1);

Matrix mDufourAKHat = Matrix::Zero(2, 2);
Matrix mDufourBKHat = Matrix::Zero(2, 1);
Matrix mDufourBKNHat = Matrix::Zero(2, 1);
Matrix mDufourWKN = Matrix::Zero(1, 1);
Matrix mDufourCKN = Matrix(1, 2);

void ssnUpdateState();
bool isLinear() const
{
return true;
}
};
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,7 @@ namespace CPS {
/// Initializes component from power flow data
void initializeFromNodesAndTerminals(Real frequency);
/// Setter for reference Current
void setParameters(MatrixComp CurrentRef, Real SrcFreq = 50.0);
/// Setter for reference voltage
//void setParameters(MatrixComp voltageRef, Real srcFreq = 50.0);
/// Setter for reference signal of type frequency ramp
//void setParameters(MatrixComp voltageRef, Real freqStart, Real rocof, Real timeStart, Real duration, bool smoothRamp = true);
/// Setter for reference signal of type cosine frequency modulation
//void setParameters(MatrixComp voltageRef, Real modulationFrequency, Real modulationAmplitude, Real baseFrequency = 50.0, bool zigzag = false);
void setParameters(MatrixComp currentRef, Real srcFreq = 50.0);

// #### MNA section ####
/// Initializes internal variables of the component
Expand Down
19 changes: 7 additions & 12 deletions dpsim-models/include/dpsim-models/EMT/EMT_Ph3_SSN_Capacitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,10 @@ namespace CPS {
/// over this if focussing on SSN model implementation is not the circuit's goal since
/// this model increases the system matrix dimensions by 1x1 (added virtual node,
/// real voltage source MNA scheme).
class Capacitor :
class Capacitor final:
public MNASimPowerComp<Real>,
public Base::Ph3::Capacitor,
public SharedFactory<Capacitor> {
protected:
Matrix Dufour_B_k_hat = Matrix::Zero(3, 3);
Matrix Dufour_W_k_n = Matrix::Zero(3, 3);
//rightsideVector history term
Matrix mHistoricVoltage = Matrix::Zero(3, 1);

public SharedFactory<Capacitor> {
public:
/// Defines UID, name and logging level
Capacitor(String uid, String name, Logger::Level logLevel = Logger::Level::off);
Expand Down Expand Up @@ -71,10 +65,11 @@ namespace CPS {
/// Add MNA post step dependencies
void mnaCompAddPostStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes, Attribute<Matrix>::Ptr &leftVector) override;

bool isLinear() const
{
return true;
}
private:
Matrix mDufourBKHat = Matrix::Zero(3, 3);
Matrix mDufourWKN = Matrix::Zero(3, 3);
//rightsideVector history term
Matrix mHistoricVoltage = Matrix::Zero(3, 1);
};
}
}
Expand Down
30 changes: 13 additions & 17 deletions dpsim-models/include/dpsim-models/EMT/EMT_Ph3_SSN_Full_Serial_RLC.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,13 @@ namespace CPS{
/// across the whole circuit. States and past inputs are updated after each
/// time step and are used to calculate the current (input) voltage,
/// represented as MNA node voltages.
class Full_Serial_RLC:
class Full_Serial_RLC final:
public MNASimPowerComp<Real>,
public SharedFactory<Full_Serial_RLC>,
public Base::Ph3::Resistor,
public Base::Ph3::Inductor,
public Base::Ph3::Capacitor
{
protected:
Matrix State = Matrix::Zero(6, 1);
Matrix yHistory = Matrix::Zero(3, 1);

Matrix Dufour_u_n_t = Matrix::Zero(3, 1);

Matrix Dufour_A_k_hat = Matrix::Zero(6, 6);
Matrix Dufour_B_k_hat = Matrix::Zero(6, 3);
Matrix Dufour_B_k_n_hat = Matrix::Zero(6, 3);
Matrix Dufour_W_k_n = Matrix::Zero(3, 3);
Matrix Dufour_C_k_n = Matrix(3, 6);
public:
/// Defines UID, name, component parameters and logging level
Full_Serial_RLC(String uid, String name, Logger::Level logLevel = Logger::Level::off);
Expand Down Expand Up @@ -80,12 +69,19 @@ namespace CPS{
/// Add MNA post step dependencies
void mnaCompAddPostStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes, Attribute<Matrix>::Ptr &leftVector) override;

void ssnUpdateState();
private:
Matrix mState = Matrix::Zero(6, 1);
Matrix mYHistory = Matrix::Zero(3, 1);

bool isLinear() const
{
return true;
}
Matrix mDufourUNT = Matrix::Zero(3, 1);

Matrix mDufourAKHat = Matrix::Zero(6, 6);
Matrix mDufourBKHat = Matrix::Zero(6, 3);
Matrix mDufourBKNHat = Matrix::Zero(6, 3);
Matrix mDufourWKN = Matrix::Zero(3, 3);
Matrix mDufourCKN = Matrix(3, 6);

void ssnUpdateState();
};
}
}
Expand Down
18 changes: 7 additions & 11 deletions dpsim-models/include/dpsim-models/EMT/EMT_Ph3_SSN_Inductor.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,11 @@ namespace CPS{
/// state update timings differ. This component is meant to show a V-type SSN model
/// implementation based on a simple example element. The RC model should be used
/// over this otherwise.
class Inductor:
class Inductor final:
public MNASimPowerComp<Real>,
public Base::Ph3::Inductor,
public SharedFactory<Inductor>
{
protected:
//rightsideVector history term
Matrix historicCurrent = Matrix::Zero(3, 1);
//dependency on latest Voltage, represented by Conductance in system matrix
Matrix Dufour_B_k_hat = Matrix::Zero(3, 3);
Matrix Dufour_W_k_n = Matrix::Zero(3, 3);
public:
/// Defines UID, name, component parameters and logging level
Inductor(String uid, String name, Logger::Level logLevel = Logger::Level::off);
Expand Down Expand Up @@ -71,10 +65,12 @@ namespace CPS{
/// Add MNA post step dependencies
void mnaCompAddPostStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes, Attribute<Matrix>::Ptr &leftVector) override;

bool isLinear() const
{
return true;
}
private:
//rightsideVector history term
Matrix mHistoricCurrent = Matrix::Zero(3, 1);
//dependency on latest Voltage, represented by Conductance in system matrix
Matrix mDufourBKHat = Matrix::Zero(3, 3);
Matrix mDufourWKN = Matrix::Zero(3, 3);
};
}
}
Expand Down
58 changes: 29 additions & 29 deletions dpsim-models/src/EMT/EMT_Ph1_SSN_Full_Serial_RLC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,33 +51,33 @@ void EMT::Ph1::SSN::Full_Serial_RLC::mnaCompInitialize(Real omega, Real timeStep

updateMatrixNodeIndices();

State = Matrix::Zero(2, 1);
yHistory = Matrix::Zero(1, 1);
mState = Matrix::Zero(2, 1);
mYHistory = Matrix::Zero(1, 1);

Dufour_A_k_hat = Matrix::Zero(2, 2);
Dufour_B_k_hat = Matrix::Zero(2, 1);
Dufour_B_k_n_hat = Matrix::Zero(2, 1);
Dufour_W_k_n = Matrix::Zero(1, 1);
Dufour_C_k_n = Matrix(1, 2);
mDufourAKHat = Matrix::Zero(2, 2);
mDufourBKHat = Matrix::Zero(2, 1);
mDufourBKNHat = Matrix::Zero(2, 1);
mDufourWKN = Matrix::Zero(1, 1);
mDufourCKN = Matrix(1, 2);

Dufour_A_k_hat(0, 0) = 1.- (( 2.*(timeStep*timeStep))/(4.* (**mInductance)* (**mCapacitance) +
mDufourAKHat(0, 0) = 1.- (( 2.*(timeStep*timeStep))/(4.* (**mInductance)* (**mCapacitance) +
2. * timeStep * (**mCapacitance) * (**mResistance) +
timeStep*timeStep)
);
Dufour_A_k_hat(0, 1) = (timeStep/(2. * (**mCapacitance))) * (1. + ((4. * (**mInductance) * (**mCapacitance) -
mDufourAKHat(0, 1) = (timeStep/(2. * (**mCapacitance))) * (1. + ((4. * (**mInductance) * (**mCapacitance) -
2. * timeStep * (**mResistance) * (**mCapacitance) -
(timeStep*timeStep))/
(
4. * (**mInductance) * (**mCapacitance) +
2. * timeStep * (**mResistance) * (**mCapacitance) +
(timeStep*timeStep))
));
Dufour_A_k_hat(1, 0) = -1. *((4. * (**mCapacitance) * timeStep)/(
mDufourAKHat(1, 0) = -1. *((4. * (**mCapacitance) * timeStep)/(
4. * (**mInductance) * (**mCapacitance) +
2. * timeStep* (**mCapacitance)* (**mResistance) +
(timeStep*timeStep)
));
Dufour_A_k_hat(1, 1) = (4. * (**mInductance) * (**mCapacitance) -
mDufourAKHat(1, 1) = (4. * (**mInductance) * (**mCapacitance) -
2. * timeStep * (**mResistance) * (**mCapacitance) -
(timeStep*timeStep))/
(
Expand All @@ -86,31 +86,31 @@ void EMT::Ph1::SSN::Full_Serial_RLC::mnaCompInitialize(Real omega, Real timeStep
(timeStep*timeStep)
);

Dufour_B_k_hat(0, 0) = (timeStep*timeStep)/
mDufourBKHat(0, 0) = (timeStep*timeStep)/
(4. * (**mInductance) * (**mCapacitance) +
2. * timeStep * (**mCapacitance) * (**mResistance) +
(timeStep * timeStep));

Dufour_B_k_hat(1, 0) = (timeStep * 2. * (**mCapacitance))/
mDufourBKHat(1, 0) = (timeStep * 2. * (**mCapacitance))/
(4. * (**mInductance) * (**mCapacitance) +
2. * timeStep * (**mCapacitance) * (**mResistance) +
(timeStep * timeStep));

Dufour_B_k_n_hat = Dufour_B_k_hat;
mDufourBKNHat = mDufourBKHat;

Dufour_C_k_n(0, 1) = 1.;
mDufourCKN(0, 1) = 1.;

Dufour_W_k_n = Dufour_C_k_n * Dufour_B_k_n_hat;
mDufourWKN = mDufourCKN * mDufourBKNHat;

///FIXME: mIntfCurrent is state 2 and is potentially directly initialized by other initialization methodes (e.g. FromNodesAndTerminals).
/// State 1, which is Voltage over the capacitor, is not directly initialized and has to be calculated from the states. This is why
/// the current must be reset as it would be altered as well. However, the old value of state one (time step "-1") is unknown or "zero"
/// in this case, so calculation of state 1 would always assume zero as the past value of state 1 and also takes mIntfCurrent
/// for the calculation ->State 1 is ahead of state 2 by one step, but also always (wrongly?) assumes past state 1 to be zero.
/// How to handle properly?
State(1, 0) = (**mIntfCurrent)(0, 0);
mState(1, 0) = (**mIntfCurrent)(0, 0);
ssnUpdateState();
State(1, 0) = (**mIntfCurrent)(0, 0);
mState(1, 0) = (**mIntfCurrent)(0, 0);

**mRightVector = Matrix::Zero(leftVector->get().rows(), 1);

Expand All @@ -126,27 +126,27 @@ void EMT::Ph1::SSN::Full_Serial_RLC::mnaCompInitialize(Real omega, Real timeStep

void EMT::Ph1::SSN::Full_Serial_RLC::mnaCompApplySystemMatrixStamp(SparseMatrixRow& systemMatrix) {
if (terminalNotGrounded(0))
Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0), matrixNodeIndex(0), Dufour_W_k_n(0,0));
Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0), matrixNodeIndex(0), mDufourWKN(0,0));
if (terminalNotGrounded(1))
Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1), matrixNodeIndex(1), Dufour_W_k_n(0,0));
Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1), matrixNodeIndex(1), mDufourWKN(0,0));
if (terminalNotGrounded(0) && terminalNotGrounded(1)) {
Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0), matrixNodeIndex(1), -Dufour_W_k_n(0,0));
Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1), matrixNodeIndex(0), -Dufour_W_k_n(0,0));
Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0), matrixNodeIndex(1), -mDufourWKN(0,0));
Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1), matrixNodeIndex(0), -mDufourWKN(0,0));
}
}

void EMT::Ph1::SSN::Full_Serial_RLC::mnaCompApplyRightSideVectorStamp(Matrix& rightVector) {
// Update internal state
yHistory = Dufour_C_k_n * (Dufour_A_k_hat * State + Dufour_B_k_hat * **mIntfVoltage);
mYHistory = mDufourCKN * (mDufourAKHat * mState + mDufourBKHat * **mIntfVoltage);

if (terminalNotGrounded(0))
Math::setVectorElement(rightVector, matrixNodeIndex(0), yHistory(0,0));
Math::setVectorElement(rightVector, matrixNodeIndex(0), mYHistory(0,0));
if (terminalNotGrounded(1))
Math::setVectorElement(rightVector, matrixNodeIndex(1), -yHistory(0,0));
Math::setVectorElement(rightVector, matrixNodeIndex(1), -mYHistory(0,0));

SPDLOG_LOGGER_DEBUG(mSLog,
"\nHistory current term (mnaCompApplyRightSideVectorStamp): {:s}",
Logger::matrixToString(yHistory));
Logger::matrixToString(mYHistory));
mSLog->flush();
}

Expand Down Expand Up @@ -175,7 +175,7 @@ void EMT::Ph1::SSN::Full_Serial_RLC::mnaCompPostStep(Real time, Int timeStepCoun

void EMT::Ph1::SSN::Full_Serial_RLC::mnaCompUpdateVoltage(const Matrix& leftVector) {
// v1 - v0
Dufour_u_n_t = **mIntfVoltage;
mDufourUNT = **mIntfVoltage;
(**mIntfVoltage)(0,0) = 0.;
if (terminalNotGrounded(1))
(**mIntfVoltage)(0,0) = Math::realFromVectorElement(leftVector, matrixNodeIndex(1));
Expand All @@ -190,7 +190,7 @@ void EMT::Ph1::SSN::Full_Serial_RLC::mnaCompUpdateVoltage(const Matrix& leftVect

void EMT::Ph1::SSN::Full_Serial_RLC::mnaCompUpdateCurrent(const Matrix& leftVector) {

**mIntfCurrent = yHistory + Dufour_W_k_n * **mIntfVoltage;
**mIntfCurrent = mYHistory + mDufourWKN * **mIntfVoltage;

SPDLOG_LOGGER_DEBUG(mSLog,
"\nUpdate Current: {:s}",
Expand All @@ -209,5 +209,5 @@ void EMT::Ph1::SSN::Full_Serial_RLC::setParameters(Real resistance, Real inducta

void EMT::Ph1::SSN::Full_Serial_RLC::ssnUpdateState()
{
State = Dufour_A_k_hat * State + Dufour_B_k_hat * Dufour_u_n_t + Dufour_B_k_n_hat * **mIntfVoltage;
mState = mDufourAKHat * mState + mDufourBKHat * mDufourUNT + mDufourBKNHat * **mIntfVoltage;
}
51 changes: 24 additions & 27 deletions dpsim-models/src/EMT/EMT_Ph3_CurrentSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,30 @@ EMT::Ph3::CurrentSource::CurrentSource(String uid, String name, Logger::Level lo
**mIntfCurrent = Matrix::Zero(3, 1);
}

SimPowerComp<Real>::Ptr EMT::Ph3::CurrentSource::clone(String name) {
auto copy = CurrentSource::make(name, mLogLevel);
// TODO: implement setParameters
// copy->setParameters(attributeTyped<MatrixComp>("I_ref")->get(), attributeTyped<Real>("f_src")->get());
return copy;
}

void EMT::Ph3::CurrentSource::setParameters(MatrixComp currentRef, Real srcFreq)
{
auto srcSigSine = Signal::SineWaveGenerator::make(**mName + "_sw");
// Complex(1,0) is used as initialPhasor for signal generator as only phase is used
srcSigSine->setParameters(Complex(1,0), srcFreq);
mSrcSig = srcSigSine;

**mCurrentRef = currentRef;
mSrcFreq->setReference(mSrcSig->mFreq);

mSLog->info("\nCurrent reference phasor [I]: {:s}"
"\nFrequency [Hz]: {:s}",
Logger::matrixCompToString(currentRef),
Logger::realToString(srcFreq));

mParametersSet = true;
}

void EMT::Ph3::CurrentSource::initializeFromNodesAndTerminals(Real frequency) {
SPDLOG_LOGGER_INFO(mSLog, "\n--- Initialization from node voltages and terminal ---");
Expand Down Expand Up @@ -64,14 +88,6 @@ void EMT::Ph3::CurrentSource::initializeFromNodesAndTerminals(Real frequency) {
mSLog->flush();
}

SimPowerComp<Real>::Ptr EMT::Ph3::CurrentSource::clone(String name) {
auto copy = CurrentSource::make(name, mLogLevel);
// TODO: implement setParameters
// copy->setParameters(attributeTyped<MatrixComp>("I_ref")->get(), attributeTyped<Real>("f_src")->get());
return copy;
}


void EMT::Ph3::CurrentSource::mnaCompInitialize(Real omega, Real timeStep, Attribute<Matrix>::Ptr leftVector) {
updateMatrixNodeIndices();
}
Expand Down Expand Up @@ -138,23 +154,4 @@ void EMT::Ph3::CurrentSource::mnaCompUpdateVoltage(const Matrix& leftVector) {
(**mIntfVoltage)(1, 0) = (**mIntfVoltage)(1, 0) - Math::realFromVectorElement(leftVector, matrixNodeIndex(0, 1));
(**mIntfVoltage)(2, 0) = (**mIntfVoltage)(2, 0) - Math::realFromVectorElement(leftVector, matrixNodeIndex(0, 2));
}
}


void EMT::Ph3::CurrentSource::setParameters(MatrixComp CurrentRef, Real SrcFreq)
{
auto srcSigSine = Signal::SineWaveGenerator::make(**mName + "_sw");
// Complex(1,0) is used as initialPhasor for signal generator as only phase is used
srcSigSine->setParameters(Complex(1,0), SrcFreq);
mSrcSig = srcSigSine;

**mCurrentRef = CurrentRef;
mSrcFreq->setReference(mSrcSig->mFreq);

mSLog->info("\nCurrent reference phasor [I]: {:s}"
"\nFrequency [Hz]: {:s}",
Logger::matrixCompToString(CurrentRef),
Logger::realToString(SrcFreq));

mParametersSet = true;
}
Loading

0 comments on commit 5335eee

Please sign in to comment.