21#include <spdlog/sinks/stdout_color_sinks.h>
77 Eigen::setNbThreads(1);
123 std::shared_ptr<PFSolver> pfSolver;
124#if defined(WITH_KLU) || defined(WITH_SPARSE)
126 pfSolver = std::make_shared<PFSolverPowerPolarSparse>(
129 pfSolver = std::make_shared<PFSolverPowerPolar>(**
mName,
mSystem,
133 pfSolver = std::make_shared<PFSolverPowerPolar>(**
mName,
mSystem,
149 solver->initialize();
162 for (
auto comp :
mSystem.mComponents) {
163 auto odeComp = std::dynamic_pointer_cast<ODEInterface>(comp);
166 auto odeSolver = std::make_shared<ODESolver>(
167 odeComp->mAttributeList->attributeTyped<
String>(
"name")->get() +
179 std::vector<SystemTopology> subnets;
183 mSystem.splitSubnets<VarType>(subnets);
187 for (
UInt net = 0; net < subnets.size(); ++net) {
189 if (subnets.size() > 1)
190 copySuffix =
"_" + std::to_string(net);
196 throw std::logic_error(
"MNA state-space extraction does not support "
197 "Diakoptics/tearing.");
200 solver = std::make_shared<DiakopticsSolver<VarType>>(
217 solver->setSystem(subnets[net]);
221 solver->setDirectLinearSolverConfiguration(
224 solver->initialize();
233 SPDLOG_LOGGER_INFO(
mLog,
"Start synchronization with remotes on interfaces");
241 SPDLOG_LOGGER_INFO(
mLog,
"Synchronized simulation start with remotes");
249 for (
auto t : solver->getTasks()) {
255 for (
auto t : intf->getTasks()) {
261 mTasks.push_back(logger->getTask());
264 mScheduler = std::make_shared<SequentialScheduler>();
270 SPDLOG_LOGGER_INFO(
mLog,
"Scheduling tasks.");
273 SPDLOG_LOGGER_INFO(
mLog,
"Scheduling done.");
281 std::map<CPS::Task::Ptr, Scheduler::TaskTime> avgTimes;
282 std::map<CPS::Task::Ptr, String> fillColors;
289 static std::map<std::type_index, String> colorMap;
290 auto tid = std::type_index(
typeid(task.get()));
292 if (colorMap.find(tid) != colorMap.end()) {
294 String(
"/paired9/") + std::to_string(1 + colorMap.size() % 9);
297 return colorMap[tid];
300 auto avgTimeWorst = Scheduler::TaskTime::min();
301 for (
auto task :
mTasks) {
302 avgTimes[task] =
mScheduler->getAveragedMeasurement(task);
304 if (avgTimes[task] > avgTimeWorst)
305 avgTimeWorst = avgTimes[task];
312 Graph::Graph g(
"dependencies", Graph::Type::directed);
313 for (
auto task :
mTasks) {
317 auto *n = g.addNode(
name);
319 std::stringstream label;
322 label <<
"<FONT POINT-SIZE=\"10\" COLOR=\"gray28\">"
325 if (isScheduled(task))
326 label <<
"Avg. time: " << avgTimes[task].count() <<
"ns<BR/>";
328 label <<
"Unscheduled"
333 n->set(
"color", getColor(task));
334 n->set(
"label", label.str(),
true);
335 n->set(
"style",
"rounded,filled,bold");
336 n->set(
"shape",
"rectangle");
338 if (isScheduled(task)) {
340 auto grad = (float)avgTimes[task].count() / avgTimeWorst.count();
342 SPDLOG_LOGGER_INFO(
mLog,
"{} {}", task->toString(),
346 n->set(
"fillcolor",
"white");
349 for (
auto from :
mTasks) {
351 g.addEdge(
"", g.node(from->toString()), g.node(to->toString()));
355 g.set(
"splines",
"ortho");
411 SPDLOG_LOGGER_INFO(
mLog,
"Initialize simulation: {}", **
mName);
418 SPDLOG_LOGGER_INFO(
mLog,
"Opening interfaces.");
425 SPDLOG_LOGGER_INFO(
mLog,
"Start simulation: {}", **
mName);
447 SPDLOG_LOGGER_INFO(
mLog,
"Simulation calculation time: {:.6f}",
458 SPDLOG_LOGGER_INFO(
mLog,
"Simulation finished.");
479 const Real roundedStepRatio = std::round(totalStepRatio);
480 const Bool hasIntegralStepCount =
481 std::isfinite(totalStepRatio) &&
482 std::abs(totalStepRatio - roundedStepRatio) <=
483 8.0 * std::numeric_limits<Real>::epsilon() *
484 std::max<Real>(1.0, std::abs(totalStepRatio));
486 mEvents.empty() && hasIntegralStepCount && roundedStepRatio >= 0.0 &&
487 roundedStepRatio <=
static_cast<Real>(std::numeric_limits<UInt>::max()) &&
489 static_cast<UInt>(roundedStepRatio))) {
490 mTime += roundedStepRatio * stepSize;
504 std::chrono::steady_clock::time_point
start;
506 start = std::chrono::steady_clock::now();
516 auto end = std::chrono::steady_clock::now();
517 std::chrono::duration<double> diff = end -
start;
524 auto stepTimeLog =
Logger::get(logName, Logger::Level::info);
526 SPDLOG_LOGGER_WARN(
mLog,
"Collection of step times has been disabled.");
530 SPDLOG_LOGGER_INFO(stepTimeLog,
"step_time");
532 Real stepTimeSum = 0;
535 SPDLOG_LOGGER_INFO(stepTimeLog,
"{:.9f}", meas);
537 SPDLOG_LOGGER_INFO(
mLog,
"Average step time: {:.9f}",
542 auto stepTimeLog =
Logger::get(logName, Logger::Level::info);
544 SPDLOG_LOGGER_INFO(stepTimeLog,
"overruns");
550 SPDLOG_LOGGER_INFO(
mLog,
"overrun detected {}: {:.9f}", overruns, meas);
553 SPDLOG_LOGGER_INFO(
mLog,
"Detected {} overruns.", overruns);
558 solver->logLUTimes();
564 if (solverIndex >=
mSolvers.size()) {
565 throw std::out_of_range(
566 "Simulation::getStateSpaceExtractor(): solver index out of range.");
569 if (
const auto realMnaSolver =
571 return realMnaSolver->getStateSpaceExtractor();
574 if (
const auto complexMnaSolver =
577 return complexMnaSolver->getStateSpaceExtractor();
580 throw std::logic_error(
581 "Simulation::getStateSpaceExtractor(): selected solver is not an "
598 mLog,
"Attribute with name {} not found on component {}", attr, comp);
602 SPDLOG_LOGGER_ERROR(
mLog,
"Component or node with name {} not found", comp);
617 throw SystemError(
"Cannot log attributes when no logger is configured for "
AttributePointer< AttributeBase > Ptr
std::shared_ptr< IdentifiedObject > Ptr
spdlog::level::level_enum Level
static void setLogPattern(std::shared_ptr< spdlog::logger > logger, std::string pattern)
static Log get(const std::string &name, Level filelevel=Level::info, Level clilevel=Level::off)
std::shared_ptr< Task > Ptr
static std::shared_ptr< MnaSolver< VarType > > factory(String name, CPS::Domain domain=CPS::Domain::DP, CPS::Logger::Level logLevel=CPS::Logger::Level::info, DirectLinearSolverImpl implementation=mSupportedSolverImpls().back(), String pluginName="plugin.so")
sovlerImpl: choose the most advanced solver implementation available by default
Solver class using Modified Nodal Analysis (MNA).
std::chrono::steady_clock::duration TaskTime
Time measurement for the task execution.
void logIdObjAttribute(const String &comp, const String &attr)
Bool mStateSpaceExtraction
Enable extraction of the MNA-coupled discrete-time state matrix.
CPS::UInt getPFMaxIterations() const
std::chrono::duration< double > mSimulationCalculationTime
Measured calculation time for simulation.
void logLUTimes()
Write LU decomposition times measurements to log file.
void sync() const
Synchronize simulation with remotes by exchanging intial state over interfaces.
CPS::IdentifiedObject::List mTearComponents
CPS::Logger::Level mLogLevel
Simulation log level.
void setPFSolverBaseVoltageLooseTolerance(Real tolerance)
DirectLinearSolverConfiguration mDirectLinearSolverConfiguration
Bool mInitFromNodesAndTerminals
virtual Real step()
Solve system A * x = z for x and current time.
Scheduler::Edges mTaskInEdges
Task dependencies as incoming / outgoing edges.
const CPS::Attribute< Real >::Ptr mTimeStep
Simulation timestep.
const CPS::Attribute< CPS::UInt >::Ptr mPFMaxIterations
Maximum number of Newton-Raphson iterations for the PF solver.
void setPFKeepLastSolution(Bool value)
const CPS::Attribute< Bool >::Ptr mSteadyStateInit
const CPS::Attribute< Real >::Ptr mPFBaseVoltageStrictTolerance
Strict tolerance between authoritative base-voltage sources in a zone; default 0.01.
Real next()
Run until next time step.
void logStepTimes(String logName)
Write step time measurements to log file.
Bool getPFSolverEnforceReactiveLimits() const
String mSolverPluginName
If there we use a solver plugin, this specifies its name (excluding .so)
void initialize()
Create solver instances etc.
const CPS::Attribute< Bool >::Ptr mPFKeepLastSolution
void setPFMaxIterations(CPS::UInt value)
void setPFBaseApparentPowerFallback(Real value)
Bool getPFSolverUseSparse() const
std::vector< Real > mStepTimes
(Real) time needed for the timesteps
const CPS::Attribute< Real >::Ptr mFinalTime
Final time of the simulation.
void prepSchedule()
Prepare schedule for simulation.
Int mTimeStepCount
Number of step which have been executed for this simulation.
const CPS::Attribute< Real >::Ptr mPFBaseApparentPowerFallback
Real mSteadStIniTimeLimit
steady state initialization time limit
Solver::SystemMatrixRecomputationMode mSystemMatrixRecomputationMode
Requested system-matrix recomputation mode.
Solver::Behaviour mSolverBehaviour
Real getPFBaseApparentPowerFallback() const
DirectLinearSolverImpl mDirectImpl
CPS::Task::List mTasks
List of all tasks to be scheduled.
const CPS::Attribute< Bool >::Ptr mSplitSubnets
std::shared_ptr< Scheduler > mScheduler
Scheduler used for task scheduling.
Bool getPFKeepLastSolution() const
std::vector< Interface::Ptr > mInterfaces
Vector of Interfaces.
void setPFSolverUseSparse(Bool value)
Bool mLogStepTimes
activate collection of step times
void create()
Helper function for constructors.
void setPFSolverEnforceReactiveLimits(Bool value)
CPS::SystemTopology mSystem
System list.
void checkForOverruns(String logName)
Check for overruns.
Real getPFSolverBaseVoltageLooseTolerance() const
Simulation(String name, CommandLineArgs &args)
Creates simulation with name and CommandLineArgs.
const CPS::Attribute< Bool >::Ptr mPFEnforceReactiveLimits
CPS::AttributeBase::Ptr getIdObjAttribute(const String &comp, const String &attr)
CHECK: Can these be deleted? getIdObjAttribute + "**attr =" should suffice.
void start()
Start simulation without advancing in time.
EventQueue mEvents
The simulation event queue.
void schedule()
Create the schedule for the independent tasks.
const CPS::Attribute< Bool >::Ptr mPFSolverUseSparse
std::chrono::time_point< std::chrono::steady_clock > mSimulationStartTimePoint
Start time point to measure calculation time.
Scheduler::Edges mTaskOutEdges
Real mTime
Time variable that is incremented at every step.
Real mSteadStIniAccLimit
steady state initialization accuracy limit
const CPS::Attribute< Real >::Ptr mPFBaseVoltageLooseTolerance
Loose tolerance for a zone's Load base-voltage vs. its rating; default 0.1.
void createMNASolver()
Subroutine for MNA only because there are many MNA options.
void run()
Run simulation until total time is elapsed.
void setPFSolverBaseVoltageStrictTolerance(Real tolerance)
void logAttribute(String name, CPS::AttributeBase::Ptr attr)
CHECK: Can we store the attribute name / UID intrinsically inside the attribute?
DataLoggerInterface::List mLoggers
The data loggers.
const MNAStateSpaceExtractor & getStateSpaceExtractor(UInt solverIndex=0) const
Bool mZeroSkipping
Skip exact zero matrix contributions. Enabled by default for all solvers.
void createSolvers()
Create solvers depending on simulation settings.
const CPS::Attribute< String >::Ptr mName
Simulation name.
Real getPFSolverBaseVoltageStrictTolerance() const
CPS::Logger::Log mLog
Simulation logger.
void stop()
Stop simulation including scheduler and interfaces.
std::chrono::time_point< std::chrono::steady_clock > mSimulationEndTimePoint
End time point to measure calculation time.
std::shared_ptr< Solver > Ptr
static std::shared_ptr< AttributeList > make(Args &&...args)
String className(T *ptr, const String &prefix="CPS::")
String encodeXml(String &data)
static Rgb gradient(double x)