Skip to content

Commit

Permalink
move method implementations into source files pt. 3
Browse files Browse the repository at this point in the history
Signed-off-by: Jonas Schroeder <jonas.schroeder1@rwth-aachen.de>
  • Loading branch information
JTS22 committed Oct 29, 2022
1 parent 8973cc2 commit bd38116
Show file tree
Hide file tree
Showing 25 changed files with 683 additions and 449 deletions.
21 changes: 4 additions & 17 deletions dpsim-models/include/dpsim-models/CIM/Reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,28 +169,15 @@ namespace CIM {
GeneratorType genType = GeneratorType::None);
///
SystemTopology loadCIM(Real systemFrequency, const std::list<CPS::String> &filenamesString, Domain domain = Domain::DP, PhaseType phase = PhaseType::Single,
GeneratorType genType = GeneratorType::None) {
std::list<fs::path> filenames;
for (auto f : filenamesString)
filenames.emplace_back(f);

return loadCIM(systemFrequency, filenames, domain, phase, genType);
}
GeneratorType genType = GeneratorType::None);

// #### shunt component settings ####
/// set shunt capacitor value
void setShuntCapacitor(Real v) {
mShuntCapacitorValue = v;
mSetShuntCapacitor = true;
}
void setShuntCapacitor(Real v);
/// set shunt conductance value
void setShuntConductance(Real v) {
mShuntConductanceValue = v;
mSetShuntConductance = true;
}

void setShuntConductance(Real v);
/// If set, some components like loads include protection switches
void useProtectionSwitches(Bool value = true) { mUseProtectionSwitches = value; }
void useProtectionSwitches(Bool value = true);
};
}
}
Expand Down
61 changes: 11 additions & 50 deletions dpsim-models/include/dpsim-models/Logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,59 +45,20 @@ namespace CPS {
///
static Log get(const std::string &name, Level filelevel = Level::info, Level clilevel = Level::off);
///
static void setLogLevel(std::shared_ptr<spdlog::logger> logger, Logger::Level level) {
logger->set_level(level);
}
static void setLogLevel(std::shared_ptr<spdlog::logger> logger, Logger::Level level);
///
static void setLogPattern(std::shared_ptr<spdlog::logger> logger, std::string pattern) {
logger->set_pattern(pattern);
}
static void setLogPattern(std::shared_ptr<spdlog::logger> logger, std::string pattern);

// #### to string methods ####
static String matrixToString(const Matrix& mat) {
std::stringstream ss;
ss << std::scientific << "\n" << mat;
return ss.str();
}

static String matrixCompToString(const MatrixComp& mat) {
std::stringstream ss;
ss << std::scientific << "\n" << mat;
return ss.str();
}

static String sparseMatrixToString(const SparseMatrix& mat) {
return matrixToString(Matrix(mat));
}

static String sparseMatrixCompToString(const SparseMatrixComp& mat) {
return matrixCompToString(MatrixComp(mat));
}

static String phasorMatrixToString(const MatrixComp& mat) {
std::stringstream ss;
ss << std::scientific << Math::abs(mat) << "\n\n" << Math::phase(mat);
return ss.str();
}

static String phasorToString(const Complex& num) {
std::stringstream ss;
ss << std::defaultfloat << Math::abs(num) << "<" << Math::phaseDeg(num);
return ss.str();
}

static String complexToString(const Complex& num) {
std::stringstream ss;
ss << std::defaultfloat << num.real() << "+j" << num.imag();
return ss.str();
}

static String realToString(const Real& num) {
std::stringstream ss;
ss << std::defaultfloat << num;
return ss.str();
}

static String matrixToString(const Matrix& mat);
static String matrixCompToString(const MatrixComp& mat);
static String sparseMatrixToString(const SparseMatrix& mat);
static String sparseMatrixCompToString(const SparseMatrixComp& mat);
static String phasorMatrixToString(const MatrixComp& mat);
static String phasorToString(const Complex& num);
static String complexToString(const Complex& num);
static String realToString(const Real& num);

static String getCSVColumnNames(std::vector<String> names);
static String getCSVLineFromData(Real time, Real data);
static String getCSVLineFromData(Real time, const Matrix& data);
Expand Down
202 changes: 36 additions & 166 deletions dpsim-models/include/dpsim-models/MathUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,59 +17,22 @@ namespace CPS {
typedef Real(*DeriveFnPtr) (Matrix inputs);

// #### Angular Operations ####
static Real radtoDeg(Real rad) {
return rad * 180 / PI;
}

static Real degToRad(Real deg) {
return deg * PI / 180;
}

static Real phase(Complex value) {
return std::arg(value);
}

static Real phaseDeg(Complex value) {
return radtoDeg(phase(value));
}

static Real abs(Complex value) {
return std::abs(value);
}

static Matrix abs(const MatrixComp& mat) {
size_t nRows = mat.rows();
size_t nCols = mat.cols();
Matrix res(mat.rows(), mat.cols());

for (size_t i = 0; i < nRows; ++i) {
for (size_t j = 0; j < nCols; ++j) {
res(i,j) = std::abs(mat(i,j));
}
}
return res;
}

static Matrix phase(const MatrixComp& mat) {
size_t nRows = mat.rows();
size_t nCols = mat.cols();
Matrix res(mat.rows(), mat.cols());

for (size_t i = 0; i < nRows; ++i) {
for (size_t j = 0; j < nCols; ++j) {
res(i,j) = std::arg(mat(i,j));
}
}
return res;
}

static Complex polar(Real abs, Real phase) {
return std::polar<Real>(abs, phase);
}

static Complex polarDeg(Real abs, Real phase) {
return std::polar<Real>(abs, radtoDeg(phase));
}
static Real radtoDeg(Real rad);

static Real degToRad(Real deg);

static Real phase(Complex value);

static Real phaseDeg(Complex value);

static Real abs(Complex value);

static Matrix abs(const MatrixComp& mat);

static Matrix phase(const MatrixComp& mat);

static Complex polar(Real abs, Real phase);
static Complex polarDeg(Real abs, Real phase);

// #### Vector Operations ####
//
Expand All @@ -78,107 +41,35 @@ namespace CPS {
// | Re(row,0)_harm2 | Re(row,colOffset)_harm2 |
// | Im(row,0)_harm2 | Im(row,colOffset)_harm2 |

static void setVectorElement(Matrix& mat, Matrix::Index row, Complex value, Int maxFreq = 1, Int freqIdx = 0, Matrix::Index colOffset = 0) {
Eigen::Index harmonicOffset = mat.rows() / maxFreq;
Eigen::Index complexOffset = harmonicOffset / 2;
Eigen::Index harmRow = row + harmonicOffset * freqIdx;

mat(harmRow, colOffset) = value.real();
mat(harmRow + complexOffset, colOffset) = value.imag();
}
static void setVectorElement(Matrix& mat, Matrix::Index row, Complex value, Int maxFreq = 1, Int freqIdx = 0, Matrix::Index colOffset = 0);

static void addToVectorElement(Matrix& mat, Matrix::Index row, Complex value, Int maxFreq = 1, Int freqIdx = 0) {
Eigen::Index harmonicOffset = mat.rows() / maxFreq;
Eigen::Index complexOffset = harmonicOffset / 2;
Eigen::Index harmRow = row + harmonicOffset * freqIdx;
static void addToVectorElement(Matrix& mat, Matrix::Index row, Complex value, Int maxFreq = 1, Int freqIdx = 0);

mat(harmRow, 0) = mat(harmRow, 0) + value.real();
mat(harmRow + complexOffset, 0) = mat(harmRow + complexOffset, 0) + value.imag();
}
static Complex complexFromVectorElement(const Matrix& mat, Matrix::Index row, Int maxFreq = 1, Int freqIdx = 0);

static Complex complexFromVectorElement(const Matrix& mat, Matrix::Index row, Int maxFreq = 1, Int freqIdx = 0) {
Eigen::Index harmonicOffset = mat.rows() / maxFreq;
Eigen::Index complexOffset = harmonicOffset / 2;
Eigen::Index harmRow = row + harmonicOffset * freqIdx;
static void addToVectorElement(Matrix& mat, Matrix::Index row, Real value);
static void setVectorElement(Matrix& mat, Matrix::Index row, Real value);

return Complex(mat(harmRow, 0), mat(harmRow + complexOffset, 0));
}

static void addToVectorElement(Matrix& mat, Matrix::Index row, Real value) {
mat(row, 0) = mat(row, 0) + value;
}

static void setVectorElement(Matrix& mat, Matrix::Index row, Real value) {
mat(row, 0) = value;
}

static Real realFromVectorElement(const Matrix& mat, Matrix::Index row) {
return mat(row, 0);
}
static Real realFromVectorElement(const Matrix& mat, Matrix::Index row);

// #### Matric Operations ####
//
// | Re-Re(row,col)_harm1 | Im-Re(row,col)_harm1 | Interharmonics harm1-harm2
// | Re-Im(row,col)_harm1 | Im-Im(row,col)_harm1 | Interharmonics harm1-harm2
// | Interharmonics harm1-harm2 | Re(row,col)_harm2 | Re(row,col)_harm2 |
// | Interharmonics harm1-harm2 | Im(row,col)_harm2 | Im(row,col)_harm2 |
static void setMatrixElement(Matrix& mat, Matrix::Index row, Matrix::Index column, Complex value, Int maxFreq = 1, Int freqIdx = 0);

static void addToMatrixElement(Matrix& mat, Matrix::Index row, Matrix::Index column, Complex value, Int maxFreq = 1, Int freqIdx = 0);

static void addToMatrixElement(Matrix& mat, Matrix::Index row, Matrix::Index column, Matrix value, Int maxFreq = 1, Int freqIdx = 0);

static void setMatrixElement(Matrix& mat, Matrix::Index row, Matrix::Index column, Real value);

static void addToMatrixElement(Matrix& mat, std::vector<UInt> rows, std::vector<UInt> columns, Complex value);

static void setMatrixElement(Matrix& mat, Matrix::Index row, Matrix::Index column, Complex value, Int maxFreq = 1, Int freqIdx = 0) {
// Assume square matrix
Eigen::Index harmonicOffset = mat.rows() / maxFreq;
Eigen::Index complexOffset = harmonicOffset / 2;
Eigen::Index harmRow = row + harmonicOffset * freqIdx;
Eigen::Index harmCol = column + harmonicOffset * freqIdx;

mat(harmRow, harmCol) = value.real();
mat(harmRow + complexOffset, harmCol + complexOffset) = value.real();
mat(harmRow, harmCol + complexOffset) = - value.imag();
mat(harmRow + complexOffset, harmCol) = value.imag();
}

static void addToMatrixElement(Matrix& mat, Matrix::Index row, Matrix::Index column, Complex value, Int maxFreq = 1, Int freqIdx = 0) {
// Assume square matrix
Eigen::Index harmonicOffset = mat.rows() / maxFreq;
Eigen::Index complexOffset = harmonicOffset / 2;
Eigen::Index harmRow = row + harmonicOffset * freqIdx;
Eigen::Index harmCol = column + harmonicOffset * freqIdx;

mat(harmRow, harmCol) = mat(harmRow, harmCol) + value.real();
mat(harmRow + complexOffset, harmCol + complexOffset) = mat(harmRow + complexOffset, harmCol + complexOffset) + value.real();
mat(harmRow, harmCol + complexOffset) = mat(harmRow, harmCol + complexOffset) - value.imag();
mat(harmRow + complexOffset, harmCol) = mat(harmRow + complexOffset, harmCol) + value.imag();
}

static void addToMatrixElement(Matrix& mat, Matrix::Index row, Matrix::Index column, Matrix value, Int maxFreq = 1, Int freqIdx = 0) {
// Assume square matrix
Eigen::Index harmonicOffset = mat.rows() / maxFreq;
Eigen::Index complexOffset = harmonicOffset / 2;
Eigen::Index harmRow = row + harmonicOffset * freqIdx;
Eigen::Index harmCol = column + harmonicOffset * freqIdx;

mat(harmRow, harmCol) = mat(harmRow, harmCol) + value(0,0);
mat(harmRow + complexOffset, harmCol + complexOffset) = mat(harmRow + complexOffset, harmCol + complexOffset) + value(1,1);
mat(harmRow, harmCol + complexOffset) = mat(harmRow, harmCol + complexOffset) + value(0,1);
mat(harmRow + complexOffset, harmCol) = mat(harmRow + complexOffset, harmCol) + value(1,0);
}

static void setMatrixElement(Matrix& mat, Matrix::Index row, Matrix::Index column, Real value) {
mat(row, column) = value;
}

static void addToMatrixElement(Matrix& mat, std::vector<UInt> rows, std::vector<UInt> columns, Complex value) {
for (UInt phase = 0; phase < rows.size(); phase++)
addToMatrixElement(mat, rows[phase], columns[phase], value);
}

static void addToMatrixElement(Matrix& mat, Matrix::Index row, Matrix::Index column, Real value) {
mat(row, column) = mat(row, column) + value;
}

static void addToMatrixElement(Matrix& mat, std::vector<UInt> rows, std::vector<UInt> columns, Real value) {
for (UInt phase = 0; phase < rows.size(); phase++)
addToMatrixElement(mat, rows[phase], columns[phase], value);
}
static void addToMatrixElement(Matrix& mat, Matrix::Index row, Matrix::Index column, Real value);
static void addToMatrixElement(Matrix& mat, std::vector<UInt> rows, std::vector<UInt> columns, Real value);

// #### Integration Methods ####
static Matrix StateSpaceTrapezoidal(Matrix states, Matrix A, Matrix B, Real dt, Matrix u_new, Matrix u_old);
Expand All @@ -200,33 +91,12 @@ namespace CPS {
static Complex rotatingFrame2to1(Complex f2, Real theta1, Real theta2);

/// To convert single phase complex variables (voltages, currents) to symmetrical three phase ones
static MatrixComp singlePhaseVariableToThreePhase(Complex var_1ph) {
MatrixComp var_3ph = MatrixComp::Zero(3, 1);
var_3ph <<
var_1ph,
var_1ph * SHIFT_TO_PHASE_B,
var_1ph * SHIFT_TO_PHASE_C;
return var_3ph;
}
static MatrixComp singlePhaseVariableToThreePhase(Complex var_1ph);

/// To convert single phase parameters to symmetrical three phase ones
static Matrix singlePhaseParameterToThreePhase(Real parameter) {
Matrix param_3ph = Matrix::Zero(3, 3);
param_3ph <<
parameter, 0., 0.,
0., parameter, 0.,
0, 0., parameter;
return param_3ph;
}
static Matrix singlePhaseParameterToThreePhase(Real parameter);

/// To convert single phase power to symmetrical three phase
static Matrix singlePhasePowerToThreePhase(Real power) {
Matrix power_3ph = Matrix::Zero(3, 3);
power_3ph <<
power/3., 0., 0.,
0., power/3., 0.,
0, 0., power/3.;
return power_3ph;
}
static Matrix singlePhasePowerToThreePhase(Real power);
};
}
12 changes: 6 additions & 6 deletions dpsim-models/include/dpsim-models/SP/SP_Ph1_Switch.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ namespace Ph1 {
void mnaAddPostStepDependencies(AttributeBase::List &prevStepDependencies,
AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes,
Attribute<Matrix>::Ptr &leftVector);

// #### MNA section for switch ####
/// Check if switch is closed
Bool mnaIsClosed();
/// Stamps system matrix considering the defined switch position
void mnaApplySwitchSystemMatrixStamp(Bool closed, Matrix& systemMatrix, Int freqIdx);

class MnaPostStep : public Task {
public:
Expand All @@ -69,12 +75,6 @@ namespace Ph1 {
Switch& mSwitch;
Attribute<Matrix>::Ptr mLeftVector;
};

// #### MNA section for switch ####
/// Check if switch is closed
Bool mnaIsClosed() { return isClosed(); }
/// Stamps system matrix considering the defined switch position
void mnaApplySwitchSystemMatrixStamp(Bool closed, Matrix& systemMatrix, Int freqIdx);
};
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ namespace Ph1 {
/// Update active & reactive power injection (VD bus)
void updatePowerInjection(Complex powerInj);
/// Get Apparent power of Powerflow solution
Complex getApparentPower() { return Complex (**mSetPointActivePower, **mSetPointReactivePower);}
Complex getApparentPower();
};
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ namespace Ph1 {
virtual ~SynchronGeneratorVBR();

/// Mark that parameter changes so that system matrix is updated
Bool hasParameterChanged() override { return 1; };
Bool hasParameterChanged() override;
};
}
}
Expand Down

0 comments on commit bd38116

Please sign in to comment.