Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor/ssp-move-algorithm-to-DefaultExperiment #447

Merged
merged 11 commits into from
Nov 4, 2019
65 changes: 21 additions & 44 deletions src/cpp/ssp_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,11 @@ class ssp_parser
{
double startTime = 0.0;
std::optional<double> stopTime;
std::shared_ptr<cse::algorithm> algorithm;
};

const DefaultExperiment& get_default_experiment() const;

struct SimulationInformation
{
std::string description;
double stepSize;
};

bool has_simulation_information() const;
const SimulationInformation& get_simulation_information() const;

struct SystemDescription
{
std::string name;
Expand Down Expand Up @@ -98,21 +90,18 @@ class ssp_parser
const std::vector<Connection>& get_connections() const;

private:
bool hasSimulationInformation_;

boost::filesystem::path xmlPath_;
boost::property_tree::ptree pt_;

SystemDescription systemDescription_;
DefaultExperiment defaultExperiment_;
SimulationInformation simulationInformation_;

std::vector<Component> elements_;
std::vector<Connection> connections_;
};

ssp_parser::ssp_parser(const boost::filesystem::path& xmlPath)
: hasSimulationInformation_(false)
, xmlPath_(xmlPath)
: xmlPath_(xmlPath)
{
// Root node
std::string path = "ssd:SystemStructureDescription";
Expand All @@ -128,28 +117,28 @@ ssp_parser::ssp_parser(const boost::filesystem::path& xmlPath)
if (const auto defaultExperiment = tmpTree.get_child_optional("ssd:DefaultExperiment")) {
defaultExperiment_.startTime = get_attribute<double>(*defaultExperiment, "startTime", 0.0);
defaultExperiment_.stopTime = get_optional_attribute<double>(*defaultExperiment, "stopTime");
}


tmpTree = pt_.get_child(path + ".ssd:System");
systemDescription_.systemName = get_attribute<std::string>(tmpTree, "name");
systemDescription_.systemDescription = get_attribute<std::string>(tmpTree, "description");

if (const auto annotations = tmpTree.get_child_optional("ssd:Annotations")) {
for (const auto& annotation : *annotations) {
const auto& annotationType = get_attribute<std::string>(annotation.second, "type");
if (annotationType == "org.open-simulation-platform") {
for (const auto& infos : annotation.second.get_child("osp:SimulationInformation")) {
hasSimulationInformation_ = true;
if (infos.first == "osp:FixedStepMaster") {
simulationInformation_.description = get_attribute<std::string>(infos.second, "description");
simulationInformation_.stepSize = get_attribute<double>(infos.second, "stepSize");
if (const auto annotations = defaultExperiment->get_child_optional("ssd:Annotations")) {
for (const auto& annotation : *annotations) {
const auto& annotationType = get_attribute<std::string>(annotation.second, "type");
if (annotationType == "com.opensimulationplatform") {
for (const auto& algorithm : annotation.second.get_child("osp:Algorithm")) {
if (algorithm.first == "osp:FixedStepAlgorithm") {
double stepSize = get_attribute<double>(algorithm.second, "stepSize");
defaultExperiment_.algorithm = std::make_unique<cse::fixed_step_algorithm>(cse::to_duration(stepSize));
} else {
throw std::runtime_error("Unknown algorithm: " + algorithm.first);
}
}
}
}
}
}

tmpTree = pt_.get_child(path + ".ssd:System");
systemDescription_.systemName = get_attribute<std::string>(tmpTree, "name");
systemDescription_.systemDescription = get_attribute<std::string>(tmpTree, "description");

for (const auto& component : tmpTree.get_child("ssd:Elements")) {

auto& e = elements_.emplace_back();
Expand Down Expand Up @@ -208,16 +197,6 @@ ssp_parser::ssp_parser(const boost::filesystem::path& xmlPath)

ssp_parser::~ssp_parser() noexcept = default;

bool ssp_parser::has_simulation_information() const
{
return hasSimulationInformation_;
}

const ssp_parser::SimulationInformation& ssp_parser::get_simulation_information() const
{
return simulationInformation_;
}

const std::vector<ssp_parser::Component>& ssp_parser::get_elements() const
{
return elements_;
Expand Down Expand Up @@ -317,12 +296,10 @@ std::pair<execution, simulator_map> load_ssp(
std::shared_ptr<cse::algorithm> algorithm;
if (overrideAlgorithm != nullptr) {
algorithm = overrideAlgorithm;
} else if (parser.has_simulation_information()) {
const auto& simInfo = parser.get_simulation_information();
const cse::duration stepSize = cse::to_duration(simInfo.stepSize);
algorithm = std::move(std::make_unique<cse::fixed_step_algorithm>(stepSize));
} else if (parser.get_default_experiment().algorithm != nullptr) {
algorithm = parser.get_default_experiment().algorithm;
} else {
CSE_PANIC_M("No co-simulation algorithm specified!");
throw std::invalid_argument("SSP contains no default co-simulation algorithm, nor has one been explicitly specified!");
}

const auto startTime = overrideStartTime ? *overrideStartTime : get_default_start_time(parser);
Expand Down
19 changes: 19 additions & 0 deletions test/data/ssp/OSPAnnotations.xsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8" ?>
kyllingstad marked this conversation as resolved.
Show resolved Hide resolved
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"
targetNamespace="http://opensimulationplatform.com/SSP/OSPAnnotations">

<xs:element name="Algorithm">
<xs:complexType>
<xs:sequence>
<xs:any processContents="skip"/>
eidekrist marked this conversation as resolved.
Show resolved Hide resolved
</xs:sequence>
</xs:complexType>
</xs:element>

<xs:element name="FixedStepAlgorithm">
<xs:complexType>
<xs:attribute name="stepSize" type="xs:double" use="required"/>
</xs:complexType>
</xs:element>

</xs:schema>
187 changes: 91 additions & 96 deletions test/data/ssp/demo/SystemStructure.ssd
Original file line number Diff line number Diff line change
Expand Up @@ -2,104 +2,99 @@
<ssd:SystemStructureDescription name="simple-cse-example" version="1.0"
xmlns:ssd="http://ssp-standard.org/SSP1/SystemStructureDescription"
xmlns:ssc="http://ssp-standard.org/SSP1/SystemStructureCommon"
xmlns:ssv="http://ssp-standard.org/SSP1/SystemStructureParameterValues">
<!-- This is a comment -->
xmlns:ssv="http://ssp-standard.org/SSP1/SystemStructureParameterValues"
xmlns:osp="http://opensimulationplatform.com/SSP/OSPAnnotations">

<!-- This is a comment -->
<ssd:System name="cse-instance" description="An example of how to use Core Simulation Environment">
<!-- This is a comment -->
<ssd:Annotations>
<ssc:Annotation type="org.open-simulation-platform">
<osp:SimulationInformation>
<osp:FixedStepMaster description="FixedStepAlgorithm" stepSize="1e-4"/>
</osp:SimulationInformation>
</ssc:Annotation>
</ssd:Annotations>
<ssd:System name="cse-instance" description="An example of how to use Core Simulation Environment">

<ssd:Elements>
<!-- This is a comment -->
<ssd:Component name="CraneController" source="CraneController.fmu">
<!-- This is a comment -->
<ssd:Connectors>
<!-- Seems to match nicely with our consept of "expose" -->
<ssd:Connector name="p_Crane.e[1]" kind="output">
<ssc:Real/>
</ssd:Connector>
<ssd:Connector name="p_Crane.e[2]" kind="output">
<ssc:Real/>
</ssd:Connector>
<ssd:Connector name="p_Crane.e[3]" kind="output">
<ssc:Real/>
</ssd:Connector>
<ssd:Connector name="p_Crane.f[1]" kind="input">
<ssc:Real/>
</ssd:Connector>
<!-- This is a comment -->
<ssd:Connector name="p_Crane.f[2]" kind="input">
<ssc:Real/>
</ssd:Connector>
<ssd:Connector name="p_Crane.f[3]" kind="input">
<ssc:Real/>
</ssd:Connector>
</ssd:Connectors>
</ssd:Component>
<ssd:Component name="KnuckleBoomCrane" source="KnuckleBoomCrane.fmu">
<!-- This is a comment -->
<ssd:Connectors>
<!-- This is a comment -->
<ssd:Connector name="p_Crane.e[1]" kind="input">
<ssc:Real/></ssd:Connector>
<ssd:Connector name="p_Crane.e[2]" kind="input">
<ssc:Real/>
</ssd:Connector>
<ssd:Connector name="p_Crane.e[3]" kind="input">
<ssc:Real/>
</ssd:Connector>
<ssd:Connector name="p_Crane.f[1]" kind="output">
<ssc:Real/>
</ssd:Connector>
<!-- This is a comment -->
<ssd:Connector name="p_Crane.f[2]" kind="output">
<ssc:Real/>
</ssd:Connector>
<ssd:Connector name="p_Crane.f[3]" kind="output">
<ssc:Real/>
</ssd:Connector>
<!-- This is a comment -->
</ssd:Connectors>
<ssd:ParameterBindings>
<ssd:ParameterBinding>
<ssd:ParameterValues>
<ssv:ParameterSet version="1.0" name="initialValues">
<ssv:Parameters>
<ssv:Parameter name="Spring_Joint.k">
<ssv:Real value="0.005" />
</ssv:Parameter>
<ssv:Parameter name="mt0_init">
<ssv:Real value="69" />
</ssv:Parameter>
</ssv:Parameters>
</ssv:ParameterSet>
</ssd:ParameterValues>
</ssd:ParameterBinding>
</ssd:ParameterBindings>
</ssd:Component>
</ssd:Elements>
<ssd:Elements>
<ssd:Component name="CraneController" source="CraneController.fmu">
<ssd:Connectors>
<!-- Seems to match nicely with our consept of "expose" -->
<ssd:Connector name="p_Crane.e[1]" kind="output">
<ssc:Real/>
</ssd:Connector>
<ssd:Connector name="p_Crane.e[2]" kind="output">
<ssc:Real/>
</ssd:Connector>
<ssd:Connector name="p_Crane.e[3]" kind="output">
<ssc:Real/>
</ssd:Connector>
<ssd:Connector name="p_Crane.f[1]" kind="input">
<ssc:Real/>
</ssd:Connector>
<ssd:Connector name="p_Crane.f[2]" kind="input">
<ssc:Real/>
</ssd:Connector>
<ssd:Connector name="p_Crane.f[3]" kind="input">
<ssc:Real/>
</ssd:Connector>
</ssd:Connectors>
</ssd:Component>
<ssd:Component name="KnuckleBoomCrane" source="KnuckleBoomCrane.fmu">
<ssd:Connectors>
<ssd:Connector name="p_Crane.e[1]" kind="input">
<ssc:Real/>
</ssd:Connector>
<ssd:Connector name="p_Crane.e[2]" kind="input">
<ssc:Real/>
</ssd:Connector>
<ssd:Connector name="p_Crane.e[3]" kind="input">
<ssc:Real/>
</ssd:Connector>
<ssd:Connector name="p_Crane.f[1]" kind="output">
<ssc:Real/>
</ssd:Connector>
<ssd:Connector name="p_Crane.f[2]" kind="output">
<ssc:Real/>
</ssd:Connector>
<ssd:Connector name="p_Crane.f[3]" kind="output">
<ssc:Real/>
</ssd:Connector>
</ssd:Connectors>
<ssd:ParameterBindings>
<ssd:ParameterBinding>
<ssd:ParameterValues>
<ssv:ParameterSet version="1.0" name="initialValues">
<ssv:Parameters>
<ssv:Parameter name="Spring_Joint.k">
<ssv:Real value="0.005" />
</ssv:Parameter>
<ssv:Parameter name="mt0_init">
<ssv:Real value="69" />
</ssv:Parameter>
</ssv:Parameters>
</ssv:ParameterSet>
</ssd:ParameterValues>
</ssd:ParameterBinding>
</ssd:ParameterBindings>
</ssd:Component>
</ssd:Elements>

<ssd:Connections>
<!-- This is a comment -->
<ssd:Connection startElement="CraneController" startConnector="p_Crane.e[1]" endElement="KnuckleBoomCrane" endConnector="p_Crane.e[1]"/>
<ssd:Connection startElement="CraneController" startConnector="p_Crane.e[2]" endElement="KnuckleBoomCrane" endConnector="p_Crane.e[2]"/>
<ssd:Connection startElement="CraneController" startConnector="p_Crane.e[3]" endElement="KnuckleBoomCrane" endConnector="p_Crane.e[3]"/>
<ssd:Connection startElement="KnuckleBoomCrane" startConnector="p_Crane.f[1]" endElement="CraneController" endConnector="p_Crane.f[1]"/>
<!-- This is a comment -->
<ssd:Connection startElement="KnuckleBoomCrane" startConnector="p_Crane.f[2]" endElement="CraneController" endConnector="p_Crane.f[2]"/>
<ssd:Connection startElement="KnuckleBoomCrane" startConnector="p_Crane.f[3]" endElement="CraneController" endConnector="p_Crane.f[3]"/>
<ssd:Connection startElement="KnuckleBoomCrane" startConnector="Act_Limits[1]" endElement="CraneController" endConnector="Act_Limits[1]"/>
<ssd:Connection startElement="KnuckleBoomCrane" startConnector="Act_Limits[2]" endElement="CraneController" endConnector="Act_Limits[2]"/>
<ssd:Connection startElement="KnuckleBoomCrane" startConnector="Act_Limits[3]" endElement="CraneController" endConnector="Act_Limits[3]"/>
</ssd:Connections>
<ssd:Connections>
<ssd:Connection startElement="CraneController" startConnector="p_Crane.e[1]" endElement="KnuckleBoomCrane" endConnector="p_Crane.e[1]"/>
<ssd:Connection startElement="CraneController" startConnector="p_Crane.e[2]" endElement="KnuckleBoomCrane" endConnector="p_Crane.e[2]"/>
<ssd:Connection startElement="CraneController" startConnector="p_Crane.e[3]" endElement="KnuckleBoomCrane" endConnector="p_Crane.e[3]"/>

<ssd:Connection startElement="KnuckleBoomCrane" startConnector="p_Crane.f[1]" endElement="CraneController" endConnector="p_Crane.f[1]"/>
<ssd:Connection startElement="KnuckleBoomCrane" startConnector="p_Crane.f[2]" endElement="CraneController" endConnector="p_Crane.f[2]"/>
<ssd:Connection startElement="KnuckleBoomCrane" startConnector="p_Crane.f[3]" endElement="CraneController" endConnector="p_Crane.f[3]"/>

<ssd:Connection startElement="KnuckleBoomCrane" startConnector="Act_Limits[1]" endElement="CraneController" endConnector="Act_Limits[1]"/>
<ssd:Connection startElement="KnuckleBoomCrane" startConnector="Act_Limits[2]" endElement="CraneController" endConnector="Act_Limits[2]"/>
<ssd:Connection startElement="KnuckleBoomCrane" startConnector="Act_Limits[3]" endElement="CraneController" endConnector="Act_Limits[3]"/>
</ssd:Connections>

</ssd:System>

<ssd:DefaultExperiment startTime="0.0" stopTime="2e-4">
<ssd:Annotations>
<ssc:Annotation type="com.opensimulationplatform">
<osp:Algorithm>
<osp:FixedStepAlgorithm stepSize="1e-4"/>
</osp:Algorithm>
</ssc:Annotation>
</ssd:Annotations>
</ssd:DefaultExperiment>

</ssd:System>
<ssd:DefaultExperiment startTime="0.0" stopTime="2e-4"/>
</ssd:SystemStructureDescription>
Loading