Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions src/app/openemsh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "infra/serializers/serializer_to_plantuml.hpp"
#include "infra/serializers/serializer_to_prettyprint.hpp"
#include "utils/concepts.hpp"
#include "utils/expected_utils.hpp"
#include "utils/unreachable.hpp"

#include "openemsh.hpp"
Expand Down Expand Up @@ -99,17 +100,32 @@ void OpenEMSH::set_output_format(Params::OutputFormat format) {
}

//******************************************************************************
void OpenEMSH::parse() {
expected<void, string> OpenEMSH::parse() {
Caretaker::singleton().reset();
board = ParserFromCsx::run(params.input, static_cast<ParserFromCsx::Params const&>(params), params.override_from_cli);
UNWRAP(
ParserFromCsx::run(params.input, static_cast<ParserFromCsx::Params const&>(params), params.override_from_cli),
[this](auto& value) {
board = value;
});
Caretaker::singleton().remember_current_timepoint();
return {};
}

//******************************************************************************
void OpenEMSH::write() const {
bool OpenEMSH::is_about_overwriting() const {
return !params.force
&& params.output_format == Params::OutputFormat::CSX
&& (filesystem::exists(params.output)
|| (params.input == params.output
&& board
&& board->global_params->get_current_state().has_grid_already));
}

//******************************************************************************
expected<void, string> OpenEMSH::write() const {
switch(params.output_format) {
case Params::OutputFormat::CSX:
SerializerToCsx::run(*board, params.input, params.output, static_cast<SerializerToCsx::Params const&>(params));
return SerializerToCsx::run(*board, params.input, params.output, static_cast<SerializerToCsx::Params const&>(params));
break;
case Params::OutputFormat::PLANTUML: {
// SerializerToPlantuml::run(*board, static_cast<SerializerToPlantuml::Params const&>(params));
Expand All @@ -122,6 +138,7 @@ void OpenEMSH::write() const {
default:
::unreachable();
};
return {};
}

//******************************************************************************
Expand Down
7 changes: 4 additions & 3 deletions src/app/openemsh.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#pragma once

#include <expected>
#include <filesystem>
#include <functional>
#include <memory>
Expand Down Expand Up @@ -55,15 +56,15 @@ class OpenEMSH {
// TODO implement validation checks on params here.
// void check_x();

void parse();
std::expected<void, std::string> parse();
void run(std::set<Step> const& steps) const;
void run_all_steps() const;
void run_next_step() const;
void run_from_step(Step step) const;
void go_before(Step step) const;
void go_before_previous_step() const;
void write() const;

bool is_about_overwriting() const;
std::expected<void, std::string> write() const;
bool can_run_a_next_step() const;
bool can_go_before() const;
std::optional<Step> get_current_step() const;
Expand Down
1 change: 1 addition & 0 deletions src/domain/global.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

//******************************************************************************
struct Params {
bool has_grid_already = false; // TODO would better fit in infra layer?

Check warning on line 17 in src/domain/global.hpp

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Complete the task associated to this "TODO" comment.

See more on https://sonarcloud.io/project/issues?id=Open-RFlab_openemsh&issues=AZrsKIJAhc2agEIONJgv&open=AZrsKIJAhc2agEIONJgv&pullRequest=32
double proximity_limit = 1; // TODO must be linked to initial d
double smoothness = 2;
std::size_t lmin = 10;
Expand Down
50 changes: 39 additions & 11 deletions src/infra/parsers/parser_from_csx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

#include <map>
#include <optional>
#include <iostream>
#include <string_view>

#include <pugixml.hpp>

Expand All @@ -18,6 +18,7 @@
#include "csxcad_layer/point_3d.hpp"

#include "parser_from_csx.hpp"
#include "utils/expected_utils.hpp"
#include "utils/progress.hpp"
#include "utils/unreachable.hpp"
#include "utils/vector_utils.hpp"
Expand Down Expand Up @@ -51,9 +52,12 @@
ParserFromCsx::Params const& params;
std::map<pugi::xml_node const, size_t> primitives_ids;
Board::Builder board;
domain::Params domain_params;

Pimpl(ParserFromCsx::Params const& params);

expected<void, string> parse_grid(pugi::xml_node const& node);

shared_ptr<Material> parse_property(pugi::xml_node const& node);

bool parse_primitive(pugi::xml_node const& node, shared_ptr<Material> const& material);
Expand All @@ -68,6 +72,26 @@
: params(params)
{}

//******************************************************************************
expected<void, string> ParserFromCsx::Pimpl::parse_grid(pugi::xml_node const& node) {
std::size_t coord_system = node.attribute("CoordSystem").as_uint();

if(coord_system == 0) {

Check warning on line 79 in src/infra/parsers/parser_from_csx.cpp

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use the init-statement to declare "coord_system" inside the if statement.

See more on https://sonarcloud.io/project/issues?id=Open-RFlab_openemsh&issues=AZrsKIJhhc2agEIONJgy&open=AZrsKIJhhc2agEIONJgy&pullRequest=32
// First step : into bool has_grid_already
pugi::xml_node grid = node.child("RectilinearGrid");
string_view x_lines = grid.child_value("XLines");
string_view y_lines = grid.child_value("YLines");
string_view z_lines = grid.child_value("ZLines");
domain_params.has_grid_already = !x_lines.empty() || !y_lines.empty() || !z_lines.empty();
// TODO Second step : into fixed MLP

Check warning on line 86 in src/infra/parsers/parser_from_csx.cpp

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Complete the task associated to this "TODO" comment.

See more on https://sonarcloud.io/project/issues?id=Open-RFlab_openemsh&issues=AZrsKIJhhc2agEIONJgw&open=AZrsKIJhhc2agEIONJgw&pullRequest=32
// TODO Third step : into vizualisable set of meshlines for comparison

Check warning on line 87 in src/infra/parsers/parser_from_csx.cpp

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Complete the task associated to this "TODO" comment.

See more on https://sonarcloud.io/project/issues?id=Open-RFlab_openemsh&issues=AZrsKIJhhc2agEIONJgx&open=AZrsKIJhhc2agEIONJgx&pullRequest=32
// } else if(coord_system == 1) {
} else {
return unexpected("Unsupported CoordSystem");
}
return {};
}

//******************************************************************************
shared_ptr<Material> ParserFromCsx::Pimpl::parse_property(pugi::xml_node const& node) {
string name(node.attribute("Name").as_string());
Expand Down Expand Up @@ -249,21 +273,21 @@
}

//******************************************************************************
shared_ptr<Board> ParserFromCsx::run(std::filesystem::path const& input) {
expected<shared_ptr<Board>, string> ParserFromCsx::run(std::filesystem::path const& input) {
return ParserFromCsx::run(input, {});
}

//******************************************************************************
shared_ptr<Board> ParserFromCsx::run(std::filesystem::path const& input, Params params) {
expected<shared_ptr<Board>, string> ParserFromCsx::run(std::filesystem::path const& input, Params params) {
ParserFromCsx parser(input, std::move(params));
parser.parse();
TRY(parser.parse());
return parser.output();
}

//******************************************************************************
shared_ptr<Board> ParserFromCsx::run(std::filesystem::path const& input, Params params, std::function<void (domain::Params&)> const& override_domain_params) {
expected<shared_ptr<Board>, string> ParserFromCsx::run(std::filesystem::path const& input, Params params, std::function<void (domain::Params&)> const& override_domain_params) {

Check failure on line 288 in src/infra/parsers/parser_from_csx.cpp

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Replace this "std::function" with a template parameter.

See more on https://sonarcloud.io/project/issues?id=Open-RFlab_openemsh&issues=AZrsKIJhhc2agEIONJgz&open=AZrsKIJhhc2agEIONJgz&pullRequest=32
ParserFromCsx parser(input, std::move(params));
parser.parse();
TRY(parser.parse());
override_domain_params(parser.domain_params);
return parser.output();
}
Expand All @@ -285,17 +309,19 @@
ParserFromCsx::~ParserFromCsx() = default;

//******************************************************************************
void ParserFromCsx::parse() {
expected<void, string> ParserFromCsx::parse() {
pugi::xml_document doc;
pugi::xml_parse_result res = doc.load_file(input.native().c_str());

if(res.status != pugi::status_ok) {
cerr << res.description() << endl;
return;
if(auto res = doc.load_file(input.native().c_str())
; res.status != pugi::status_ok) {
return unexpected(res.description());
}

pugi::xpath_node fdtd = doc.select_node("/openEMS/FDTD");

pugi::xpath_node csx = doc.select_node("/openEMS/ContinuousStructure");
TRY(pimpl->parse_grid(csx.node()));

{
// Primitives' IDs grow disregarding properties.
size_t id = 0;
Expand Down Expand Up @@ -324,6 +350,8 @@

}
bar.complete();
domain_params = std::move(pimpl->domain_params);
return {};
}

//******************************************************************************
Expand Down
10 changes: 5 additions & 5 deletions src/infra/parsers/parser_from_csx.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#pragma once

#include <expected>
#include <filesystem>
#include <memory>
#include <string>
Expand All @@ -31,15 +32,15 @@ class ParserFromCsx {

~ParserFromCsx();

[[nodiscard]] static std::shared_ptr<domain::Board> run(std::filesystem::path const& input);
[[nodiscard]] static std::shared_ptr<domain::Board> run(std::filesystem::path const& input, Params params);
[[nodiscard]] static std::shared_ptr<domain::Board> run(std::filesystem::path const& input, Params params, std::function<void (domain::Params&)> const& override_domain_params);
[[nodiscard]] static std::expected<std::shared_ptr<domain::Board>, std::string> run(std::filesystem::path const& input);
[[nodiscard]] static std::expected<std::shared_ptr<domain::Board>, std::string> run(std::filesystem::path const& input, Params params);
[[nodiscard]] static std::expected<std::shared_ptr<domain::Board>, std::string> run(std::filesystem::path const& input, Params params, std::function<void (domain::Params&)> const& override_domain_params);

private:
ParserFromCsx(std::filesystem::path const& input);
ParserFromCsx(std::filesystem::path const& input, Params params);

void parse();
std::expected<void, std::string> parse();
[[nodiscard]] std::shared_ptr<domain::Board> output();

std::filesystem::path const input;
Expand All @@ -49,5 +50,4 @@ class ParserFromCsx {
// std::vector<std::unique_ptr<Polygon>> polygons;
class Pimpl;
std::unique_ptr<Pimpl> pimpl;

};
55 changes: 43 additions & 12 deletions src/infra/serializers/serializer_to_csx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
/// @author Thomas Lepoix <thomas.lepoix@protonmail.ch>
///*****************************************************************************

#include <iostream>

#include <pugixml.hpp>

#include "domain/mesh/meshline.hpp"
Expand All @@ -28,23 +26,27 @@ string to_xml_node(Axis const axis) noexcept {
}

//******************************************************************************
void SerializerToCsx::run(
expected<void, string> SerializerToCsx::run(
Board& board,
filesystem::path const& input,
filesystem::path const& output) {

SerializerToCsx::run(board, input, output, {});
return SerializerToCsx::run(board, input, output, {});
}

//******************************************************************************
void SerializerToCsx::run(
expected<void, string> SerializerToCsx::run(
Board& board,
filesystem::path const& input,
filesystem::path const& output,
Params params) {

SerializerToCsx serializer(input, output, std::move(params));
board.accept(serializer);

if(serializer.error)
return unexpected(serializer.error.value());
return {};
}

//******************************************************************************
Expand All @@ -54,17 +56,36 @@ SerializerToCsx::SerializerToCsx(filesystem::path const& input, filesystem::path
, output(output)
{}

//******************************************************************************
pugi::xml_node find_or_prepend_child(pugi::xml_node& node, char const* child) {
if(pugi::xml_node c = node.child(child); c.empty())
return node.prepend_child(child);
else
return c;
}

//******************************************************************************
pugi::xml_node find_or_append_child(pugi::xml_node& node, char const* child) {
if(pugi::xml_node c = node.child(child); c.empty())
return node.append_child(child);
else
return c;
}

//******************************************************************************
void SerializerToCsx::visit(Board& board) {
pugi::xml_document doc;
pugi::xml_parse_result const res = doc.load_file(input.native().c_str());

if(res.status != pugi::status_ok) {
cerr << res.description() << endl;
error = res.description();
return;
}

pugi::xpath_node const grid = doc.select_node("/openEMS/ContinuousStructure/RectilinearGrid");
pugi::xml_node oems = find_or_append_child(doc, "openEMS");
pugi::xml_node csx = find_or_append_child(oems, "ContinuousStructure");
pugi::xml_node grid = find_or_append_child(csx, "RectilinearGrid");
grid.remove_children();

auto const add_meshlines_to_xml_doc = [this, &grid, &board](Axis const axis) {
string out;
Expand All @@ -78,9 +99,7 @@ void SerializerToCsx::visit(Board& board) {
if(!out.empty())
out.pop_back();

cerr << "lines: " << out << endl << endl;
grid.node().select_node(to_xml_node(axis).c_str())
.node().text().set(out.c_str());
grid.append_child(to_xml_node(axis).c_str()).text().set(out.c_str());
};

if(params.with_axis_x)
Expand All @@ -90,6 +109,18 @@ void SerializerToCsx::visit(Board& board) {
if(params.with_axis_z)
add_meshlines_to_xml_doc(Z);

cerr << "output: " << output << endl;
doc.save_file(output.native().c_str());
if(params.with_oemsh_params) {
auto const& p = board.global_params->get_current_state();
pugi::xml_node oemsh = find_or_prepend_child(doc, "OpenEMSH");
pugi::xml_node global_params = find_or_append_child(oemsh, "GlobalParams");
global_params.remove_attributes();
global_params.append_attribute("ProximityLimit").set_value(p.proximity_limit);
global_params.append_attribute("Smoothness").set_value(p.smoothness);
global_params.append_attribute("dmax").set_value(p.dmax);
global_params.append_attribute("lmin").set_value(p.lmin);
} else {
doc.remove_child("OpenEMSH");
}

doc.save_file(output.native().c_str(), " ");
}
8 changes: 6 additions & 2 deletions src/infra/serializers/serializer_to_csx.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

#pragma once

#include <expected>
#include <filesystem>
#include <optional>
#include <string>

#include "domain/utils/entity_visitor.hpp"
Expand All @@ -22,13 +24,14 @@ class SerializerToCsx final : public domain::EntityVisitor {
bool with_axis_x = true;
bool with_axis_y = true;
bool with_axis_z = true;
bool with_oemsh_params = false;
};

static void run(
static std::expected<void, std::string> run(
domain::Board& board,
std::filesystem::path const& input,
std::filesystem::path const& output);
static void run(
static std::expected<void, std::string> run(
domain::Board& board,
std::filesystem::path const& input,
std::filesystem::path const& output,
Expand All @@ -47,4 +50,5 @@ class SerializerToCsx final : public domain::EntityVisitor {
Params const params;
std::filesystem::path const input;
std::filesystem::path const output;
std::optional<std::string> error;
};
Loading
Loading