Skip to content

Commit

Permalink
moving home, named multimesh default injection broken
Browse files Browse the repository at this point in the history
  • Loading branch information
mtao committed Nov 26, 2024
1 parent 858bb3a commit 4c1b344
Show file tree
Hide file tree
Showing 6 changed files with 213 additions and 56 deletions.
99 changes: 81 additions & 18 deletions applications/convert/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <filesystem>
#include <nlohmann/json.hpp>
#include <wmtk/applications/utils/element_count_report.hpp>
#include <wmtk/applications/utils/get_integration_test_data_root.hpp>

#include <wmtk/Mesh.hpp>
#include <wmtk/utils/Logger.hpp>
Expand Down Expand Up @@ -81,25 +82,27 @@ std::shared_ptr<wmtk::Mesh> merge_meshes(
}
} // namespace

int run(const fs::path& config_path /*, const std::optional<fs::path>& name_spec_file*/)
int run_js(
const std::string_view& app_name,
const nlohmann::json& j,
const std::optional<fs::path>& name_spec_file,
const std::optional<fs::path>& integration_test_config_file)
{
nlohmann::json j;
{
std::ifstream ifs(config_path);
j = nlohmann::json::parse(ifs);
// if (name_spec_file.has_value()) {
// j["name"] = nlohmann::json::parse(std::ifstream(name_spec_file.value()));
// }
}

spdlog::warn("{}", j.dump(2));

// if (name_spec_file.has_value()) {
// j["name"] = nlohmann::json::parse(std::ifstream(name_spec_file.value()));
// }
wmtk::components::multimesh::MeshCollection meshes;
components::utils::PathResolver path_resolver;

if (j.contains("root")) {
path_resolver = j["root"];
}
if (integration_test_config_file.has_value()) {
auto path = wmtk::applications::utils::get_integration_test_data_root(
integration_test_config_file.value(),
app_name);
path_resolver.add_path(path);
}

std::shared_ptr<wmtk::Mesh> output_mesh;
if (j["input"].is_array()) {
Expand Down Expand Up @@ -133,14 +136,15 @@ int run(const fs::path& config_path /*, const std::optional<fs::path>& name_spec


if (j.contains("report")) {
nlohmann::json jnew = j;
const std::string report = j["report"];
meshes.make_canonical();
if (!report.empty()) {
nlohmann::json out_json;
auto& stats = out_json["stats"];
stats = wmtk::applications::utils::element_count_report_named(meshes);
j.erase("report");
out_json["input"] = j;
jnew.erase("report");
out_json["input"] = jnew;


std::ofstream ofs(report);
Expand All @@ -150,6 +154,18 @@ int run(const fs::path& config_path /*, const std::optional<fs::path>& name_spec
return 0;
}

int run(
const std::string_view& app_name,
const fs::path& config_path,
const std::optional<fs::path>& name_spec_file,
const std::optional<fs::path>& integration_test_config_file)
{
nlohmann::json j;
std::ifstream ifs(config_path);
j = nlohmann::json::parse(ifs);

return run_js(app_name, j, name_spec_file, integration_test_config_file);
}

int main(int argc, char* argv[])
{
Expand All @@ -158,21 +174,50 @@ int main(int argc, char* argv[])
app.ignore_case();

fs::path json_input_file;
std::optional<fs::path> json_integration_config_file;
std::optional<std::string> json_integration_app_name;
std::optional<fs::path> name_spec_file;

CLI::App* run_cmd; // = app.add_subcommand("run", "Run application");
run_cmd = &app;
auto add_it_path = [&](CLI::App& a) {
a.add_option(
"-c, --integration-test-config",
json_integration_config_file,
"Test config file for integration test")
->check(CLI::ExistingFile);
a.add_option(
"-a, --integration-test-app",
json_integration_config_file,
"Test config file for integration test")
->check(CLI::ExistingFile);
};

CLI::App* run_cmd = app.add_subcommand("run", "Run application");
run_cmd->add_option("-j, --json", json_input_file, "json specification file")
->required(true)
->check(CLI::ExistingFile);

add_it_path(*run_cmd);


fs::path input;
fs::path output;
std::string type;
CLI::App* type_cmd = app.add_subcommand("type", "Convert mesh to another type");
type_cmd->add_option("-i, --input", input, "input file")
->required(true)
->check(CLI::ExistingFile);

type_cmd->add_option("-o, --output", output, "output file");
type_cmd->add_option("-t, --type", type, "output file type");
add_it_path(*type_cmd);


// run_cmd->add_option("-n, --name_spec", name_spec_file, "json specification file")
// ->check(CLI::ExistingFile);

CLI11_PARSE(app, argc, argv);

// someday may add other suboptions
assert(run_cmd->parsed());

// if (!json_input_file.has_value() && !fill_config_path.has_value()) {
// wmtk::logger().error("An input json file with [-j] is required unless blank config "
Expand All @@ -187,7 +232,25 @@ int main(int argc, char* argv[])
// assert(json_input_file.has_value());
// exit_mode = run(json_input_file.value());
// });
exit_mode = run(json_input_file /*, name_spec_file*/);
if (run_cmd->parsed()) {
exit_mode = run(argv[0], json_input_file, name_spec_file, json_integration_config_file);
} else {
wmtk::components::input::InputOptions in;
in.path = input;
wmtk::components::output::OutputOptions out;
out.path = output;
out.position_attribute = "vertices";
if (!type.empty() && type[0] != '.') {
type = '.' + type;
}
out.type = type;

nlohmann::json js;
js["input"] = in;
js["output"][""] = out;

exit_mode = run_js(argv[0], js, name_spec_file, json_integration_config_file);
}


assert(exit_mode != -1); // "Some subcommand should have updated the exit mode"
Expand Down
4 changes: 2 additions & 2 deletions components/input/tests/input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ TEST_CASE("component_input", "[components][input]")
auto a = wmtk::components::input::input(input_file, false, {});

json component_json = {
{"file", input_file.string()},
{"path", input_file.string()},
{"old_mode", true},
{"ignore_z", false},
{"tetrahedron_attributes", json::array()}};
Expand Down Expand Up @@ -58,7 +58,7 @@ TEST_CASE("component_input", "[components][input]")
// json component_json = {
// {"type", "input"},
// {"name", "input_mesh"},
// {"file", "In case you ever name your file like that: What is wrong with
// {"path", "In case you ever name your file like that: What is wrong with
// you?"},
// {"ignore_z", false},
// {"tetrahedron_attributes", json::array()}};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <span>
#include <vector>
#include <wmtk/Mesh.hpp>
#include <wmtk/utils/Logger.hpp>
#include "internal/split_path.hpp"
namespace wmtk::components::multimesh {

Expand Down Expand Up @@ -77,7 +78,11 @@ struct NamedMultiMesh::Node
{
m_child_indexer.clear();
for (size_t j = 0; j < m_children.size(); ++j) {
m_child_indexer.emplace(m_children[j]->name, j);
const auto& n = m_children[j]->name;
if (m_child_indexer.contains(n)) {
throw std::runtime_error(fmt::format("Child indexer saw the name {} twice", name));
}
m_child_indexer.emplace(n, j);
}
}
friend void to_json(
Expand Down Expand Up @@ -111,6 +116,7 @@ NamedMultiMesh::NamedMultiMesh(Mesh& m, const nlohmann::json& root_name)
void NamedMultiMesh::set_root(Mesh& m)
{
m_root = m.shared_from_this();
populate_default_names();
}

void NamedMultiMesh::set_mesh(Mesh& m)
Expand Down Expand Up @@ -157,14 +163,17 @@ auto NamedMultiMesh::get_id(const std::string_view& path) const -> std::vector<i
Node const* cur_mesh = m_name_root.get();
assert(*split.begin() == cur_mesh->name || *split.begin() == "");
for (const auto& token : std::ranges::views::drop(split, 1)) {
// try {
int64_t index = cur_mesh->m_child_indexer.at(std::string(token));
//} catch(const std::runtime_error& e) {
// wmtk::logger().warn("Failed to find mesh named {} in mesh list. Path was ", nmm_name,
// path); throw e;
//}
indices.emplace_back(index);
cur_mesh = cur_mesh->m_children[index].get();
try {
int64_t index = cur_mesh->m_child_indexer.at(std::string(token));
indices.emplace_back(index);
cur_mesh = cur_mesh->m_children[index].get();
} catch (const std::runtime_error& e) {
wmtk::logger().warn(
"Failed to find mesh named {} in mesh list. Path was {}",
token,
path);
throw e;
}
}

return indices;
Expand Down Expand Up @@ -201,13 +210,49 @@ void NamedMultiMesh::set_names(const nlohmann::json& js)
}
}

void NamedMultiMesh::populate_default_names()
{
std::function<void(Node&, const Mesh& m)> sdn;
sdn = [&sdn](Node& n, const Mesh& m) {
const auto& children = m.get_child_meshes();
spdlog::info("Injecting into {} children", children.size());
if (n.m_children.size() < children.size()) {
n.m_children.resize(children.size());
}

for (size_t j = 0; j < children.size(); ++j) {
auto& nnptr = n.m_children[j];
if (!bool(nnptr)) {
nnptr = std::make_unique<Node>();
}
auto& nn = *nnptr;
if (nn.name.empty()) {
nn.name = fmt::format("{}", j);
spdlog::info("Gave child name {}", nn.name);
}
}
for (size_t j = 0; j < children.size(); ++j) {
sdn(*n.m_children[j], *children[j]);
}
};
assert(bool(m_name_root));
if (m_name_root->name.empty()) {
m_name_root->name = "0";
}
sdn(*m_name_root, *m_root);
}

std::string_view NamedMultiMesh::root_name() const
{
assert(bool(m_name_root));

return m_name_root->name;
}
std::string NamedMultiMesh::name(const std::vector<int64_t>& id) const
{
return get_name(id);
}
std::string NamedMultiMesh::get_name(const std::vector<int64_t>& id) const
{
std::vector<std::string_view> names;
Node const* cur_mesh = m_name_root.get();
Expand All @@ -219,6 +264,23 @@ std::string NamedMultiMesh::name(const std::vector<int64_t>& id) const
return fmt::format("{}", fmt::join(names, "."));
}

auto NamedMultiMesh::get_node(const std::vector<int64_t>& id) const -> const Node&
{
Node const* cur_mesh = m_name_root.get();
for (const auto& index : id) {
cur_mesh = cur_mesh->m_children[index].get();
}
return *cur_mesh;
}
auto NamedMultiMesh::get_node(const std::vector<int64_t>& id) -> Node&
{
Node* cur_mesh = m_name_root.get();
for (const auto& index : id) {
cur_mesh = cur_mesh->m_children[index].get();
}
return *cur_mesh;
}

NamedMultiMesh::NamedMultiMesh()
: m_name_root(std::make_unique<Node>())
{}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace wmtk::components::multimesh {
// The IDs are loaded through a json format, and hte root node can either have a name or be hte
// empty string. Even if hte root it has a name, typing it can be skipped by a preceding '.' before
// a path
// By default each mesh is named by its index number
class NamedMultiMesh
{
public:
Expand All @@ -35,13 +36,15 @@ class NamedMultiMesh
/// sets just the name of the root mesh, keeping child names the same
void set_name(const std::string_view& root_name = "");
void set_names(const nlohmann::json& js);
void populate_default_names();
void set_root(Mesh& m);
void append_child_mesh_names(const Mesh& parent, const NamedMultiMesh& o);

std::unique_ptr<nlohmann::json> get_names_json() const;

std::string_view root_name() const;
std::string name(const std::vector<int64_t>& id) const;
std::string get_name(const std::vector<int64_t>& id) const;

/// Navigates to the root of the multimesh
void set_mesh(Mesh& m);
Expand All @@ -59,8 +62,13 @@ class NamedMultiMesh
// returns the name of a mesh if it lies in this multimesh
std::string get_name(const Mesh& m) const;


private:
struct Node;
const Node& get_node(const std::vector<int64_t>& id) const;
Node& get_node(const std::vector<int64_t>& id);

private:
std::shared_ptr<Mesh> m_root;
std::unique_ptr<Node> m_name_root;
};
Expand Down
Loading

0 comments on commit 4c1b344

Please sign in to comment.