Skip to content

Commit

Permalink
fix some small errors and move member mSolverParams from Solver to su…
Browse files Browse the repository at this point in the history
…bclasses

Signed-off-by: Martin Moraga <[email protected]>
  • Loading branch information
martinmoraga committed Jul 5, 2023
1 parent 817f68f commit a2e6859
Show file tree
Hide file tree
Showing 20 changed files with 485 additions and 711 deletions.
15 changes: 5 additions & 10 deletions dpsim/include/dpsim/MNASolver.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@
#include <bitset>

#include <dpsim/Config.h>
#include <dpsim/DataLogger.h>
#include <dpsim/Solver.h>
#include <dpsim/SolverParametersMNA.h>
#include <dpsim/SolverParameters.h>
#include <dpsim/DataLogger.h>
#include <dpsim-models/AttributeList.h>
#include <dpsim-models/Solver/MNASwitchInterface.h>
#include <dpsim-models/Solver/MNAVariableCompInterface.h>
Expand All @@ -39,6 +38,9 @@ namespace DPsim {
template <typename VarType>
class MnaSolver : public Solver {
protected:
/// ### Solver Parameters
std::shared_ptr<SolverParametersMNA> mSolverParams;

// #### General simulation settings ####
/// Simulation domain, which can be dynamic phasor (DP) or EMT
CPS::Domain mDomain;
Expand All @@ -61,10 +63,6 @@ namespace DPsim {
/// List of index pairs of varying matrix entries
std::vector<std::pair<UInt, UInt>> mListVariableSystemMatrixEntries;


using Solver::mSolverParams;


/// System topology
CPS::SystemTopology mSystem;
/// List of simulation nodes
Expand Down Expand Up @@ -129,6 +127,7 @@ namespace DPsim {
/// Constructor should not be called by users but by Simulation
MnaSolver(String name,
CPS::Domain domain = CPS::Domain::DP,
std::shared_ptr<SolverParametersMNA> solverParams = SolverParametersMNA(),
CPS::Logger::Level logLevel = CPS::Logger::Level::info);

/// Initialization of individual components
Expand Down Expand Up @@ -217,9 +216,5 @@ namespace DPsim {
Matrix& rightSideVector() { return mRightSideVector; }
///
virtual CPS::Task::List getTasks() override;
/// Gets Solver Parameters for Modified Nodal Analysis (MNA)
std::shared_ptr<SolverParametersMNA> getMNAParameters() { return std::dynamic_pointer_cast<SolverParametersMNA>(mSolverParams); }


};
}
3 changes: 2 additions & 1 deletion dpsim/include/dpsim/MNASolverDirect.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ namespace DPsim {
/// LU factorization configuration
DirectLinearSolverConfiguration mConfigurationInUse;

using Solver::mSolverParams;
using MnaSolver<VarType>::mSolverParams;
using MnaSolver<VarType>::mSwitches;
using MnaSolver<VarType>::mMNAIntfSwitches;
using MnaSolver<VarType>::mMNAComponents;
Expand Down Expand Up @@ -144,6 +144,7 @@ namespace DPsim {
/// sovlerImpl: choose the most advanced solver implementation available by default
MnaSolverDirect(String name,
CPS::Domain domain = CPS::Domain::DP,
std::shared_ptr<SolverParametersMNA> solverParams = SolverParametersMNA(),
CPS::Logger::Level logLevel = CPS::Logger::Level::info);

/// Destructor
Expand Down
25 changes: 13 additions & 12 deletions dpsim/include/dpsim/MNASolverFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,39 +63,40 @@ class MnaSolverFactory {
template <typename VarType>
static std::shared_ptr<MnaSolver<VarType>> factory(String name,
CPS::Domain domain = CPS::Domain::DP,
std::shared_ptr<SolverParametersMNA> solverParams = SolverParametersMNA(),
CPS::Logger::Level logLevel = CPS::Logger::Level::info,
CPS::DirectLinearSolverImpl implementation = CPS::DirectLinearSolverImpl::SparseLU,
String pluginName = "plugin.so")
{
//To avoid regression we use SparseLU in case of undefined implementation
if (implementation == CPS::DirectLinearSolverImpl::Undef) {
implementation = CPS::DirectLinearSolverImpl::SparseLU;
}
if (solverParams->mDirectImpl == CPS::DirectLinearSolverImpl::Undef)
solverParams->setDirectLinearSolverImplementation(CPS::DirectLinearSolverImpl::SparseLU);

//
CPS::Logger::Log log = CPS::Logger::get("MnaSolverFactory", CPS::Logger::Level::info, CPS::Logger::Level::info);

switch(implementation) {
switch(solverParams->mDirectImpl) {
/* TODO: have only one "solver" object of type MnaSolverDirect and only use setDirectLinearSolverImplementation in the switch-case.
* This is not done now, since MnaSolverDirect and MnaSolver are distinct classes - and someone might add another subclass of MnaSolver
* to the project (MnaSolverIterative?). It is planned to merge MnaSolverDirect and MnaSolver anyway, so this won't happen. */
case CPS::DirectLinearSolverImpl::SparseLU:
{
log->info("creating SparseLUAdapter solver implementation");
std::shared_ptr<MnaSolverDirect<VarType>> sparseSolver = std::make_shared<MnaSolverDirect<VarType>>(name, domain, logLevel);
std::shared_ptr<MnaSolverDirect<VarType>> sparseSolver = std::make_shared<MnaSolverDirect<VarType>>(name, domain, solverParams, logLevel);
sparseSolver->setDirectLinearSolverImplementation(CPS::DirectLinearSolverImpl::SparseLU);
return sparseSolver;
}
case CPS::DirectLinearSolverImpl::DenseLU:
{
log->info("creating DenseLUAdapter solver implementation");
std::shared_ptr<MnaSolverDirect<VarType>> denseSolver = std::make_shared<MnaSolverDirect<VarType>>(name, domain, logLevel);
std::shared_ptr<MnaSolverDirect<VarType>> denseSolver = std::make_shared<MnaSolverDirect<VarType>>(name, domain, solverParams, logLevel);
denseSolver->setDirectLinearSolverImplementation(CPS::DirectLinearSolverImpl::DenseLU);
return denseSolver;
}
#ifdef WITH_KLU
case CPS::DirectLinearSolverImpl::KLU:
{
log->info("creating KLUAdapter solver implementation");
std::shared_ptr<MnaSolverDirect<VarType>> kluSolver = std::make_shared<MnaSolverDirect<VarType>>(name, domain, logLevel);
std::shared_ptr<MnaSolverDirect<VarType>> kluSolver = std::make_shared<MnaSolverDirect<VarType>>(name, domain, solverParams, logLevel);
kluSolver->setDirectLinearSolverImplementation(CPS::DirectLinearSolverImpl::KLU);
return kluSolver;
}
Expand All @@ -104,15 +105,15 @@ class MnaSolverFactory {
case CPS::DirectLinearSolverImpl::CUDADense:
{
log->info("creating GpuDenseAdapter solver implementation");
std::shared_ptr<MnaSolverDirect<VarType>> gpuDenseSolver = std::make_shared<MnaSolverDirect<VarType>>(name, domain, logLevel);
std::shared_ptr<MnaSolverDirect<VarType>> gpuDenseSolver = std::make_shared<MnaSolverDirect<VarType>>(name, domain, solverParams, logLevel);
gpuDenseSolver->setDirectLinearSolverImplementation(CPS::DirectLinearSolverImpl::CUDADense);
return gpuDenseSolver;
}
#ifdef WITH_CUDA_SPARSE
case CPS::DirectLinearSolverImpl::CUDASparse:
{
log->info("creating GpuSparseAdapter solver implementation");
std::shared_ptr<MnaSolverDirect<VarType>> gpuSparseSolver = std::make_shared<MnaSolverDirect<VarType>>(name, domain, logLevel);
std::shared_ptr<MnaSolverDirect<VarType>> gpuSparseSolver = std::make_shared<MnaSolverDirect<VarType>>(name, domain, solverParams, logLevel);
gpuSparseSolver->setDirectLinearSolverImplementation(CPS::DirectLinearSolverImpl::CUDASparse);
return gpuSparseSolver;
}
Expand All @@ -121,7 +122,7 @@ class MnaSolverFactory {
case CPS::DirectLinearSolverImpl::CUDAMagma:
{
log->info("creating GpuMagmaAdapter solver implementation");
std::shared_ptr<MnaSolverDirect<VarType>> gpuMagmaSolver = std::make_shared<MnaSolverDirect<VarType>>(name, domain, logLevel);
std::shared_ptr<MnaSolverDirect<VarType>> gpuMagmaSolver = std::make_shared<MnaSolverDirect<VarType>>(name, domain, solverParams, logLevel);
gpuMagmaSolver->setDirectLinearSolverImplementation(CPS::DirectLinearSolverImpl::CUDAMagma);
return gpuMagmaSolver;
}
Expand All @@ -130,7 +131,7 @@ class MnaSolverFactory {
#ifdef WITH_MNASOLVERPLUGIN
case CPS::DirectLinearSolverImpl::Plugin:
log->info("creating Plugin solver implementation");
return std::make_shared<MnaSolverPlugin<VarType>>(pluginName, name, domain, logLevel);
return std::make_shared<MnaSolverPlugin<VarType>>(pluginName, name, domain, solverParams, logLevel);
#endif
default:
throw CPS::SystemError("unsupported MNA implementation.");
Expand Down
3 changes: 3 additions & 0 deletions dpsim/include/dpsim/MNASolverPlugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

#include <dpsim/MNASolverDirect.h>
#include <dpsim/MNASolverDynInterface.h>
#include <dpsim/SolverParametersMNA.h>


namespace DPsim {

Expand All @@ -30,6 +32,7 @@ namespace DPsim {
MnaSolverPlugin(String pluginName,
String name,
CPS::Domain domain = CPS::Domain::DP,
std::shared_ptr<SolverParametersMNA> solverParams = SolverParametersMNA(),
CPS::Logger::Level logLevel = CPS::Logger::Level::info);

virtual ~MnaSolverPlugin();
Expand Down
6 changes: 5 additions & 1 deletion dpsim/include/dpsim/PFSolver.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ namespace DPsim {
/// Solver class using the nonlinear powerflow (PF) formulation.
class PFSolver: public Solver {
protected:
/// ### Solver Parameters
std::shared_ptr<SolverParametersMNA> mSolverParams;

/// Number of PQ nodes
UInt mNumPQBuses = 0;
/// Number of PV nodes
Expand Down Expand Up @@ -138,6 +141,7 @@ namespace DPsim {
PFSolver(CPS::String name,
CPS::SystemTopology system,
Real timeStep,
std::shared_ptr<SolverParametersMNA> solverParams,
CPS::Logger::Level logLevel);
///
virtual ~PFSolver() { };
Expand All @@ -149,7 +153,7 @@ namespace DPsim {
/// set solver and component to initialization or simulation behaviour
void setSolverAndComponentBehaviour(Solver::Behaviour behaviour);
/// Gets Solver Parameters for Modified Nodal Analysis (MNA)
std::shared_ptr<SolverParametersMNA> getMNAParameters() { return std::dynamic_pointer_cast<SolverParametersMNA>(mSolverParams); }
//std::shared_ptr<SolverParametersMNA> getMNAParameters() { return std::dynamic_pointer_cast<SolverParametersMNA>(mSolverParams); }

class SolveTask : public CPS::Task {
public:
Expand Down
2 changes: 1 addition & 1 deletion dpsim/include/dpsim/PFSolverPowerPolar.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ namespace DPsim {
void calculateNodalInjection();
public:
/// Constructor to be used in simulation examples.
PFSolverPowerPolar(CPS::String name, const CPS::SystemTopology &system, CPS::Real timeStep, CPS::Logger::Level logLevel);
PFSolverPowerPolar(CPS::String name, const CPS::SystemTopology &system, CPS::Real timeStep, std::shared_ptr<SolverParametersMNA> solverParams, CPS::Logger::Level logLevel);
///
virtual ~PFSolverPowerPolar() { };
};
Expand Down
26 changes: 14 additions & 12 deletions dpsim/include/dpsim/Simulation.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,22 @@

#pragma once

#include "dpsim/MNASolverFactory.h"
#include <vector>

#include <dpsim/Config.h>
#include <dpsim/DataLogger.h>
#include <dpsim/MNASolverFactory.h>
#include <dpsim/Solver.h>
#include <dpsim/SolverParameters.h>
#include <dpsim/Scheduler.h>
#include <dpsim/Event.h>
#include <dpsim-models/Attribute.h>
#include <dpsim-models/Definitions.h>
#include <dpsim/Interface.h>
#include <dpsim-models/Logger.h>
#include <dpsim-models/SystemTopology.h>
#include <dpsim-models/SimNode.h>
#include <dpsim-models/Attribute.h>
#include <dpsim/Interface.h>

#include <nlohmann/json.hpp>

#ifdef WITH_GRAPHVIZ
Expand Down Expand Up @@ -146,27 +147,28 @@ namespace DPsim {
///
void setFinalTime(Real finalTime) { **mFinalTime = finalTime; }
///
void setSimulationParameters(CPS::Real timeStep, CPS::Real finalTime) {
**mTimeStep = timeStep;
**mFinalTime = finalTime;
}
///
void setDomain(CPS::Domain domain = CPS::Domain::DP) { mDomain = domain; }
///
void setSolverType(Solver::Type solverType = Solver::Type::MNA) { mSolverType = solverType; }
///
void setDirectLinearSolverConfiguration(const DirectLinearSolverConfiguration& configuration) { mDirectLinearSolverConfiguration = configuration; }
void setSolverParameters(CPS::Domain domain = CPS::Domain::DP, Solver::Type type = Solver::Type::MNA,
std::shared_ptr<SolverParameters> solverParameters = nullptr);
///
void setDirectLinearSolverConfiguration(const DirectLinearSolverConfiguration& configuration) { mDirectLinearSolverConfiguration = configuration; }
///
void setTearingComponents(CPS::IdentifiedObject::List tearComponents = CPS::IdentifiedObject::List()) {
mTearComponents = tearComponents;
}
///
void setSimulationParameters(CPS::Real a, CPS::Real b) {}
///
void setSolverParameters(CPS::Domain domain, Solver::Type type, std::shared_ptr<SolverParameters> &solverParameters);



/// Set the scheduling method
void setScheduler(const std::shared_ptr<Scheduler> &scheduler) {
mScheduler = scheduler;
}




// #### Simulation Control ####
Expand Down
5 changes: 0 additions & 5 deletions dpsim/include/dpsim/Solver.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
#include <list>

#include <dpsim/Definitions.h>
#include <dpsim/SolverParameters.h>
#include <dpsim/Config.h>
#include <dpsim/DirectLinearSolverConfiguration.h>
#include <dpsim-models/Logger.h>
Expand Down Expand Up @@ -45,8 +44,6 @@ namespace DPsim {
CPS::Logger::Level mLogLevel;
/// Logger
CPS::Logger::Log mSLog;
/// Solver Parameters
std::shared_ptr<SolverParameters> mSolverParams;
/// Time step for fixed step solvers
Real mTimeStep;

Expand Down Expand Up @@ -78,8 +75,6 @@ namespace DPsim {
void setTimeStep(Real timeStep) { mTimeStep = timeStep; }
///
void doSystemMatrixRecomputation(Bool value) { mSystemMatrixRecomputation = value; }

void setSolverParameters(std::shared_ptr<SolverParameters> &solverParameters){ mSolverParams = solverParameters; }

// #### Initialization ####
///
Expand Down
13 changes: 4 additions & 9 deletions dpsim/include/dpsim/SolverParameters.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,25 @@ namespace DPsim {
class SolverParameters{
public:
/// Time step for fixed step solvers
Real mTimeStep;
Real mTimeStep = 0.001;

// #### Initialization ####

/// If this is false, all voltages are initialized with zero
Bool mInitFromNodesAndTerminals = true;



public:
SolverParameters() {}

virtual ~SolverParameters() { }

void setTimeStep(Real timeStep) { mTimeStep = timeStep; }

// #### Initialization ####
// #### Setters ####
/// activate powerflow initialization
void setInitFromNodesAndTerminals(Bool f) { mInitFromNodesAndTerminals = f; }


// #### Getter ####
Real getTimeStep() {return mTimeStep;}

///
Bool getInitFromNodesAndTerminals() {return mInitFromNodesAndTerminals;}

};
}
Loading

0 comments on commit a2e6859

Please sign in to comment.