Skip to content

Commit

Permalink
resolve rebase conflicts
Browse files Browse the repository at this point in the history
Signed-off-by: Martin Moraga <[email protected]>
  • Loading branch information
martinmoraga committed Mar 4, 2024
1 parent 940c242 commit 214a8c7
Show file tree
Hide file tree
Showing 6 changed files with 338 additions and 374 deletions.
208 changes: 109 additions & 99 deletions dpsim-models/include/dpsim-models/SP/SP_Ph1_NetworkInjection.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,105 +16,115 @@
namespace CPS {
namespace SP {
namespace Ph1 {
/// \brief Network injection model
///
/// This model represents network injections by an ideal voltage source.
/// The voltage source can be configured to use different types of SignalGenerators using the various setParameters functions
/// When the SineWaveGenerator is configured via the void setParameters(Complex voltageRef, Real srcFreq = 0.0) function,
/// the frequency, magnitude and phase of the sine wave can be modified through the mVoltageRef and mSrcFreq attributes.
/// See SP_Ph1_VoltageSource.h for more details.
class NetworkInjection:
public CompositePowerComp<Complex>,
public SharedFactory<NetworkInjection>,
public PFSolverInterfaceBus,
public DAEInterface {

private:
// ### Electrical Subcomponents ###
/// Voltage source
std::shared_ptr<SP::Ph1::VoltageSource> mSubVoltageSource;

// #### solver ####
/// Vector to collect subcomponent right vector stamps
std::vector<const Matrix*> mRightVectorStamps;

// #### Powerflow section ####
/// Base voltage [V]
const Attribute<Real>::Ptr mBaseVoltage;

public:
const Attribute<Complex>::Ptr mVoltageRef;
const Attribute<Real>::Ptr mSrcFreq;

// #### Powerflow section ####
/// Voltage set point [V]
const Attribute<Real>::Ptr mVoltageSetPoint;
/// Voltage set point [pu]
const Attribute<Real>::Ptr mVoltageSetPointPerUnit;
/// Active Power Injection [W]
const Attribute<Real>::Ptr mActivePowerInjection;
/// Reactive Power Injection [Var]
const Attribute<Real>::Ptr mReactivePowerInjection;

/// Defines UID, name and logging level
NetworkInjection(String uid, String name, Logger::Level logLevel = Logger::Level::off);
/// Defines name and logging level
NetworkInjection(String name, Logger::Level logLevel = Logger::Level::off)
: NetworkInjection(name, name, logLevel) { }
///
SimPowerComp<Complex>::Ptr clone(String name) override;

// #### General ####
/// Initializes component from power flow data
void initializeFromNodesAndTerminals(Real frequency) override;

// #### Powerflow section ####
/// Set parameters relevant for PF solver
void setParameters(Real vSetPointPerUnit);
/// Set base voltage
void setBaseVoltage(Real baseVoltage);
/// Calculates component's parameters in specified per-unit system
void calculatePerUnitParameters(Real baseApparentPower, Real baseOmega);
/// Modify powerflow bus type
void modifyPowerFlowBusType(PowerflowBusType powerflowBusType) override;
/// Update power injection
void updatePowerInjection(Complex powerInj);

// #### MNA Section ####
/// Setter for reference voltage and frequency with a sine wave generator
/// This will initialize the values of mVoltageRef and mSrcFreq to match the given parameters
/// However, the attributes can be modified during the simulation to dynamically change the magnitude, frequency, and phase of the sine wave.
void setParameters(Complex voltageRef, Real srcFreq = 0.0);
/// Setter for reference signal of type frequency ramp
/// This will create a FrequencyRampGenerator which will not react to external changes to mVoltageRef or mSrcFreq!
void setParameters(Complex initialPhasor, Real freqStart, Real rocof, Real timeStart, Real duration, bool smoothRamp = true);
/// Setter for reference signal of type cosine frequency modulation
/// This will create a CosineFMGenerator which will not react to external changes to mVoltageRef or mSrcFreq!
void setParameters(Complex initialPhasor, Real modulationFrequency, Real modulationAmplitude, Real baseFrequency = 0.0, bool zigzag = false);

/// Stamps right side (source) vector
void mnaParentApplyRightSideVectorStamp(Matrix& rightVector) override;
/// Returns current through the component
void mnaCompUpdateCurrent(const Matrix& leftVector) override;
/// Updates voltage across component
void mnaCompUpdateVoltage(const Matrix& leftVector) override;
/// MNA pre step operations
void mnaParentPreStep(Real time, Int timeStepCount) override;
/// MNA post step operations
void mnaParentPostStep(Real time, Int timeStepCount, Attribute<Matrix>::Ptr &leftVector) override;
/// Add MNA pre step dependencies
void mnaParentAddPreStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes) override;
/// Add MNA post step dependencies
void mnaParentAddPostStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes, Attribute<Matrix>::Ptr &leftVector) override;

// #### DAE Section ####
/// Residual function for DAE Solver
void daeResidual(double ttime, const double state[], const double dstate_dt[], double resid[], std::vector<int>& off) override;
///Voltage Getter
Complex daeInitialize() override;


/// \brief Network injection model
///
/// This model represents network injections by an ideal voltage source.
/// The voltage source can be configured to use different types of SignalGenerators using the various setParameters functions
/// When the SineWaveGenerator is configured via the void setParameters(Complex voltageRef, Real srcFreq = 0.0) function,
/// the frequency, magnitude and phase of the sine wave can be modified through the mVoltageRef and mSrcFreq attributes.
/// See SP_Ph1_VoltageSource.h for more details.
class NetworkInjection : public CompositePowerComp<Complex>,
public SharedFactory<NetworkInjection>,
public PFSolverInterfaceBus,
public DAEInterface {

private:
// ### Electrical Subcomponents ###
/// Voltage source
std::shared_ptr<SP::Ph1::VoltageSource> mSubVoltageSource;

// #### solver ####
/// Vector to collect subcomponent right vector stamps
std::vector<const Matrix *> mRightVectorStamps;

// #### Powerflow section ####
/// Base voltage [V]
const Attribute<Real>::Ptr mBaseVoltage;

public:
const Attribute<Complex>::Ptr mVoltageRef;
const Attribute<Real>::Ptr mSrcFreq;

// #### Powerflow section ####
/// Voltage set point [V]
const Attribute<Real>::Ptr mVoltageSetPoint;
/// Voltage set point [pu]
const Attribute<Real>::Ptr mVoltageSetPointPerUnit;
/// Active Power Injection [W]
const Attribute<Real>::Ptr mActivePowerInjection;
/// Reactive Power Injection [Var]
const Attribute<Real>::Ptr mReactivePowerInjection;

/// Defines UID, name and logging level
NetworkInjection(String uid, String name,
Logger::Level logLevel = Logger::Level::off);
/// Defines name and logging level
NetworkInjection(String name, Logger::Level logLevel = Logger::Level::off)
: NetworkInjection(name, name, logLevel) {}
///
SimPowerComp<Complex>::Ptr clone(String name) override;

// #### General ####
/// Initializes component from power flow data
void initializeFromNodesAndTerminals(Real frequency) override;

// #### Powerflow section ####
/// Set parameters relevant for PF solver
void setParameters(Real vSetPointPerUnit);
/// Set base voltage
void setBaseVoltage(Real baseVoltage);
/// Calculates component's parameters in specified per-unit system
void calculatePerUnitParameters(Real baseApparentPower, Real baseOmega);
/// Modify powerflow bus type
void modifyPowerFlowBusType(PowerflowBusType powerflowBusType) override;
/// Update power injection
void updatePowerInjection(Complex powerInj);

// #### MNA Section ####
/// Setter for reference voltage and frequency with a sine wave generator
/// This will initialize the values of mVoltageRef and mSrcFreq to match the given parameters
/// However, the attributes can be modified during the simulation to dynamically change the magnitude, frequency, and phase of the sine wave.
void setParameters(Complex voltageRef, Real srcFreq = 0.0);
/// Setter for reference signal of type frequency ramp
/// This will create a FrequencyRampGenerator which will not react to external changes to mVoltageRef or mSrcFreq!
void setParameters(Complex initialPhasor, Real freqStart, Real rocof,
Real timeStart, Real duration, bool smoothRamp = true);
/// Setter for reference signal of type cosine frequency modulation
/// This will create a CosineFMGenerator which will not react to external changes to mVoltageRef or mSrcFreq!
void setParameters(Complex initialPhasor, Real modulationFrequency,
Real modulationAmplitude, Real baseFrequency = 0.0,
bool zigzag = false);

/// Stamps right side (source) vector
void mnaParentApplyRightSideVectorStamp(Matrix &rightVector) override;
/// Returns current through the component
void mnaCompUpdateCurrent(const Matrix &leftVector) override;
/// Updates voltage across component
void mnaCompUpdateVoltage(const Matrix &leftVector) override;
/// MNA pre step operations
void mnaParentPreStep(Real time, Int timeStepCount) override;
/// MNA post step operations
void mnaParentPostStep(Real time, Int timeStepCount,
Attribute<Matrix>::Ptr &leftVector) override;
/// Add MNA pre step dependencies
void mnaParentAddPreStepDependencies(
AttributeBase::List &prevStepDependencies,
AttributeBase::List &attributeDependencies,
AttributeBase::List &modifiedAttributes) override;
/// Add MNA post step dependencies
void
mnaParentAddPostStepDependencies(AttributeBase::List &prevStepDependencies,
AttributeBase::List &attributeDependencies,
AttributeBase::List &modifiedAttributes,
Attribute<Matrix>::Ptr &leftVector) override;

// #### DAE Section ####
/// Residual function for DAE Solver
void daeResidual(double ttime, const double state[], const double dstate_dt[],
double resid[], std::vector<int> &off) override;
///Voltage Getter
Complex daeInitialize() override;
};
} // namespace Ph1
} // namespace SP
} // namespace CPS
} // namespace CPS
13 changes: 6 additions & 7 deletions dpsim-models/src/SP/SP_Ph1_Load.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,13 @@ void SP::Ph1::Load::setParameters(Real activePower, Real reactivePower,
**mReactivePower = reactivePower;
**mNomVoltage = nominalVoltage;

SPDLOG_LOGGER_INFO(mSLog, "Active Power={} [W] Reactive Power={} [VAr]",
**mActivePower, **mReactivePower);
SPDLOG_LOGGER_INFO(
mSLog,
"Active Power={}[W]\nReactive Power={} [VAr]\nNominal Voltage={} [V]",
**mActivePower, **mReactivePower, **mNomVoltage);
mSLog->flush();

SPDLOG_LOGGER_INFO(mSLog, "Active Power={}[W]\nReactive Power={} [VAr]\nNominal Voltage={} [V]", **mActivePower, **mReactivePower, **mNomVoltage);
mSLog->flush();

mParametersSet = true;
mParametersSet = true;
}

// #### Powerflow section ####
Expand Down Expand Up @@ -199,4 +198,4 @@ void SP::Ph1::Load::mnaCompUpdateCurrent(const Matrix &leftVector) {
for (auto &subc : mSubComponents) {
(**mIntfCurrent)(0, 0) += subc->intfCurrent()(0, 0);
}
}
}
70 changes: 38 additions & 32 deletions dpsim-models/src/SP/SP_Ph1_NetworkInjection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,36 @@
using namespace CPS;

SP::Ph1::NetworkInjection::NetworkInjection(String uid, String name,
Logger::Level logLevel) : CompositePowerComp<Complex>(uid, name, true, true, logLevel),
mVoltageRef(mAttributes->createDynamic<Complex>("V_ref")),
mSrcFreq(mAttributes->createDynamic<Real>("f_src")),
mBaseVoltage(mAttributes->create<Real>("base_Voltage")),
mVoltageSetPoint(mAttributes->create<Real>("V_set")),
mVoltageSetPointPerUnit(mAttributes->create<Real>("V_set_pu", 1.0)),
mActivePowerInjection(mAttributes->create<Real>("p_inj")),
mReactivePowerInjection(mAttributes->create<Real>("q_inj")) {

SPDLOG_LOGGER_INFO(mSLog, "Create {} of type {}", **mName, this->type());
mSLog->flush();
**mIntfVoltage = MatrixComp::Zero(1, 1);
**mIntfCurrent = MatrixComp::Zero(1, 1);
setVirtualNodeNumber(0);
setTerminalNumber(1);

// Create electrical sub components
mSubVoltageSource = std::make_shared<SP::Ph1::VoltageSource>(**mName + "_vs", mLogLevel);
addMNASubComponent(mSubVoltageSource, MNA_SUBCOMP_TASK_ORDER::TASK_BEFORE_PARENT, MNA_SUBCOMP_TASK_ORDER::TASK_BEFORE_PARENT, true);
SPDLOG_LOGGER_INFO(mSLog, "Electrical subcomponents: ");
for (auto subcomp: mSubComponents)
SPDLOG_LOGGER_INFO(mSLog, "- {}", subcomp->name());

// MNA attributes
mSubVoltageSource->mVoltageRef->setReference(mVoltageRef);
mSubVoltageSource->mSrcFreq->setReference(mSrcFreq);
Logger::Level logLevel)
: CompositePowerComp<Complex>(uid, name, true, true, logLevel),
mVoltageRef(mAttributes->createDynamic<Complex>("V_ref")),
mSrcFreq(mAttributes->createDynamic<Real>("f_src")),
mBaseVoltage(mAttributes->create<Real>("base_Voltage")),
mVoltageSetPoint(mAttributes->create<Real>("V_set")),
mVoltageSetPointPerUnit(mAttributes->create<Real>("V_set_pu", 1.0)),
mActivePowerInjection(mAttributes->create<Real>("p_inj")),
mReactivePowerInjection(mAttributes->create<Real>("q_inj")) {

SPDLOG_LOGGER_INFO(mSLog, "Create {} of type {}", **mName, this->type());
mSLog->flush();
**mIntfVoltage = MatrixComp::Zero(1, 1);
**mIntfCurrent = MatrixComp::Zero(1, 1);
setVirtualNodeNumber(0);
setTerminalNumber(1);

// Create electrical sub components
mSubVoltageSource =
std::make_shared<SP::Ph1::VoltageSource>(**mName + "_vs", mLogLevel);
addMNASubComponent(mSubVoltageSource,
MNA_SUBCOMP_TASK_ORDER::TASK_BEFORE_PARENT,
MNA_SUBCOMP_TASK_ORDER::TASK_BEFORE_PARENT, true);
SPDLOG_LOGGER_INFO(mSLog, "Electrical subcomponents: ");
for (auto subcomp : mSubComponents)
SPDLOG_LOGGER_INFO(mSLog, "- {}", subcomp->name());

// MNA attributes
mSubVoltageSource->mVoltageRef->setReference(mVoltageRef);
mSubVoltageSource->mSrcFreq->setReference(mSrcFreq);
}

// #### Powerflow section ####
Expand Down Expand Up @@ -89,14 +93,16 @@ void SP::Ph1::NetworkInjection::setParameters(Complex initialPhasor,
}

void SP::Ph1::NetworkInjection::setBaseVoltage(Real baseVoltage) {
**mBaseVoltage = baseVoltage;
**mBaseVoltage = baseVoltage;
}

void SP::Ph1::NetworkInjection::calculatePerUnitParameters(Real baseApparentPower, Real baseOmega) {
SPDLOG_LOGGER_INFO(mSLog, "#### Calculate Per Unit Parameters for {}", **mName);
SPDLOG_LOGGER_INFO(mSLog, "Base Voltage={} [V]", **mBaseVoltage);
void SP::Ph1::NetworkInjection::calculatePerUnitParameters(
Real baseApparentPower, Real baseOmega) {
SPDLOG_LOGGER_INFO(mSLog, "#### Calculate Per Unit Parameters for {}",
**mName);
SPDLOG_LOGGER_INFO(mSLog, "Base Voltage={} [V]", **mBaseVoltage);

**mVoltageSetPointPerUnit = **mVoltageSetPoint / **mBaseVoltage;
**mVoltageSetPointPerUnit = **mVoltageSetPoint / **mBaseVoltage;

SPDLOG_LOGGER_INFO(mSLog, "Voltage Set-Point ={} [pu]",
**mVoltageSetPointPerUnit);
Expand Down Expand Up @@ -224,4 +230,4 @@ void SP::Ph1::NetworkInjection::daeResidual(double ttime, const double state[],
Complex SP::Ph1::NetworkInjection::daeInitialize() {
(**mIntfVoltage)(0, 0) = (**mSubVoltageSource->mIntfVoltage)(0, 0);
return (**mSubVoltageSource->mIntfVoltage)(0, 0);
}
}
Loading

0 comments on commit 214a8c7

Please sign in to comment.