Skip to content

Commit

Permalink
ecco params parsing impl
Browse files Browse the repository at this point in the history
  • Loading branch information
restenb committed Jan 20, 2025
1 parent 60d4fcc commit 7c859a6
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 4 deletions.
58 changes: 55 additions & 3 deletions src/cosim/osp_config_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,32 @@

#include "osp_system_structure_schema.hpp"

#include "cosim/algorithm.hpp"
#include "cosim/function/linear_transformation.hpp"
#include "cosim/function/vector_sum.hpp"
#include "cosim/log/logger.hpp"
#include "cosim/uri.hpp"
#include "cosim/algorithm.hpp"

#include <boost/lexical_cast.hpp>
#include <xercesc/dom/DOM.hpp>
#include <xercesc/framework/MemBufInputSource.hpp>
#include <xercesc/framework/StdOutFormatTarget.hpp>
#include <xercesc/framework/Wrapper4InputSource.hpp>
#include <xercesc/framework/XMLFormatter.hpp>
#include <xercesc/util/PlatformUtils.hpp>
#include <xercesc/util/XMLString.hpp>
#include <xercesc/util/XMLUni.hpp>

#include <ios>
#include <iostream>
#include <memory>
#include <optional>
#include <sstream>
#include <stdexcept>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
#include <iostream>
#include <optional>


namespace cosim
Expand Down Expand Up @@ -117,13 +120,28 @@ class osp_config_parser
const cosim::filesystem::path& configPath);
~osp_config_parser() noexcept;

struct EccoConfiguration
{
double safetyFactor = 0.99;
double stepSize = 1e-3;
double minimumStepSize = 1e-5;
double maximumStepSize = 1e-2;
double minimumChangeRate = 0.2;
double maximumChangeRate = 1.5;
double proportionalGain = 0.2;
double integralGain = 0.15;
double relativeTolerance = 1e-6;
double absoluteTolerance = 1e-6;
};

struct SimulationInformation
{
std::string description;
std::string algorithm;
double stepSize = 0.1;
double startTime = 0.0;
std::optional<double> endTime;
std::optional<EccoConfiguration> eccoConfiguration;
};

const SimulationInformation& get_simulation_information() const;
Expand Down Expand Up @@ -272,6 +290,16 @@ osp_config_parser::osp_config_parser(
xercesc::DOMImplementationLS::MODE_SYNCHRONOUS,
tc("http://www.w3.org/2001/XMLSchema").get());

auto serializer = ((xercesc::DOMImplementationLS*)domImpl)->createLSSerializer();
if (serializer->getDomConfig()->canSetParameter(xercesc::XMLUni::fgDOMWRTFormatPrettyPrint, true)) {
serializer->getDomConfig()->setParameter(xercesc::XMLUni::fgDOMWRTFormatPrettyPrint, true);
}
serializer->setNewLine(xercesc::XMLString::transcode("\r\n"));

auto outputTarget = ((xercesc::DOMImplementationLS*)domImpl)->createLSOutput();
auto outputFormat = new xercesc::StdOutFormatTarget();
outputTarget->setByteStream(outputFormat);

error_handler errorHandler;

std::string xsd_str = osp_xsd;
Expand Down Expand Up @@ -445,6 +473,28 @@ osp_config_parser::osp_config_parser(
simulationInformation_.algorithm = std::string(tc(saNodes->item(0)->getTextContent()).get());
}

auto eccoConfigurationElement = static_cast<xercesc::DOMElement*>(rootElement->getElementsByTagName(tc("EccoConfiguration").get())->item(0));
if (eccoConfigurationElement) {
serializer->write(eccoConfigurationElement, outputTarget);
const auto elem = eccoConfigurationElement->getFirstElementChild();
serializer->write(elem, outputTarget);


/*
std::vector<InitialValue> initialValues;
const auto initValsElement = static_cast<xercesc::DOMElement*>(element->getElementsByTagName(tc("InitialValues").get())->item(0));
if (initValsElement) {
for (auto initValElement = initValsElement->getFirstElementChild(); initValElement != nullptr; initValElement = initValElement->getNextElementSibling()) {
std::string varName = tc(initValElement->getAttribute(tc("variable").get())).get();
variable_type varType = parse_variable_type(std::string(tc(initValElement->getFirstElementChild()->getNodeName()).get()));
std::string varValue = tc(initValElement->getFirstElementChild()->getAttribute(tc("value").get())).get();
*/

// auto safetyFactor = eccoNodes->getElementsByTagName(tc("SafetyFactor").get());
// std::cout << "Safety factor: " << safetyFactor << std::endl;
}

auto connectionsElement = static_cast<xercesc::DOMElement*>(rootElement->getElementsByTagName(tc("Connections").get())->item(0));
if (connectionsElement) {
auto variableConnectionsElement = connectionsElement->getElementsByTagName(tc("VariableConnection").get());
Expand Down Expand Up @@ -515,6 +565,8 @@ osp_config_parser::osp_config_parser(
signalGroupConnections_.push_back({signal, variable});
}
}

serializer->release();
}

osp_config_parser::~osp_config_parser() noexcept = default;
Expand Down
2 changes: 2 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
set(tests
"fixed_step_algorithm_test"
"ecco_algorithm_test"
"ecco_algorithm_multi_bond_test"
"ecco_algorithm_from_system_structure_test"
"file_observer_dynamic_logging_test"
"file_observer_logging_test"
"file_observer_logging_from_config_test"
Expand Down
38 changes: 38 additions & 0 deletions tests/ecco_algorithm_from_system_structure_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include <cosim/algorithm.hpp>
#include <cosim/log/simple.hpp>
#include <cosim/observer/last_value_observer.hpp>
#include <cosim/observer/time_series_observer.hpp>
#include <cosim/time.hpp>
#include <cosim/fs_portability.hpp>
#include <cosim/orchestration.hpp>
#include <cosim/osp_config_parser.hpp>

#include <exception>
#include <memory>
#include <stdexcept>
#include <cstdlib>

// A helper macro to test various assertions
#define REQUIRE(test) \
if (!(test)) throw std::runtime_error("Requirement not satisfied: " #test)

int main()
{
try {
const auto testDataDir = std::getenv("TEST_DATA_DIR");
REQUIRE(testDataDir);
cosim::log::setup_simple_console_logging();
cosim::log::set_global_output_level(cosim::log::debug);

auto resolver = cosim::default_model_uri_resolver();
auto configPath = cosim::filesystem::path(testDataDir) / "ecco" / "quarter_truck";
std::cout << "Config path is " << configPath << std::endl;

const auto config = cosim::load_osp_config(configPath, *resolver);
} catch (const std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl;
return 1;
}

return 0;
}
2 changes: 1 addition & 1 deletion tests/ecco_algorithm_multi_bond_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ int main()
auto chassisIndex = entityMaps.simulators.at("chassis");
auto wheelIndex = entityMaps.simulators.at("wheel");

auto gravity = cosim::variable_id{chassisIndex, cosim::variable_type::real, 2};
//auto gravity = cosim::variable_id{chassisIndex, cosim::variable_type::real, 2};
auto chassisForce = cosim::variable_id{chassisIndex, cosim::variable_type::real, 19};
auto chassisVel = cosim::variable_id{chassisIndex, cosim::variable_type::real, 22};
auto wheelCForce = cosim::variable_id{wheelIndex, cosim::variable_type::real, 26};
Expand Down

0 comments on commit 7c859a6

Please sign in to comment.