diff --git a/tbox/src/wGmshExport.cpp b/tbox/src/wGmshExport.cpp index 6487d0dbf0d63741bc4dc4d6443500b99397f75f..e9df48979c39e53a2b704380ddad1393698842cc 100644 --- a/tbox/src/wGmshExport.cpp +++ b/tbox/src/wGmshExport.cpp @@ -298,6 +298,73 @@ void GmshExport::save(std::string const &fname, Results const &r) const std::cout << "done" << std::endl; } +/** + * @brief Save results as a list view, i.e. with the underlying mesh, on disk + * @todo Check if useful with newer Gmsh versions + * @todo Remove if remeshing is not done through Gmsh + */ +void GmshExport::saveList(std::string const &fname, Results const &r) const +{ + std::string wname = fname + ".pos"; + std::cout << "writing file: " << wname << "... " << std::flush; + + FILE *file; + file = fopen(wname.c_str(), "wt"); + if (!file) + throw std::runtime_error("tbox::GmshExport::saveList Error opening file!\n"); + + if (!r.scalars_at_elems.empty() || !r.vectors_at_nodes.empty() || !r.vectors_at_elems.empty() || !r.tensors_at_nodes.empty() || !r.tensors_at_elems.empty()) + throw std::runtime_error("tbox::GmshExport::saveList Only scalars_at_nodes Result type is supported!\n"); + + for (auto &p : r.scalars_at_nodes) + { + // name of view + fprintf(file, "View \"%s\" {\n", p.first.c_str()); + // element and data + for (auto e : msh->elems) + { + std::string str; + if (e->type() == ElType::POINT1) + { + str = "SP("; + } + else if (e->type() == ElType::LINE2) + { + str = "SL("; + } + else if (e->type() == ElType::TRI3) + { + str = "ST("; + } + else if (e->type() == ElType::TETRA4) + { + str = "SS("; + } + else + throw std::runtime_error("tbox::GmshExport::saveList Element type not supported!\n"); + // node coordinates and values + for (auto n : e->nodes) + str += std::to_string(n->pos(0)) + "," + std::to_string(n->pos(1)) + "," + std::to_string(n->pos(2)) + ","; + str.erase(str.length() - 1); + str += "){"; + for (auto n : e->nodes) + { + if (std::isnan((*(p.second))[n->row])) + str += "-nan,"; // find a nicer way to handle nan + else + str += std::to_string((*(p.second))[n->row]) + ","; + } + str.erase(str.length() - 1); + str += "};\n"; + fprintf(file, "%s", str.c_str()); + } + fprintf(file, "};\n"); + } + // close file + fclose(file); + std::cout << "done" << std::endl; +} + void GmshExport::write(std::ostream &out) const { out << "GmshExport on " << *msh << std::endl; diff --git a/tbox/src/wGmshExport.h b/tbox/src/wGmshExport.h index dc74bf3ebb68693991f9e7f82bba716344b45f9c..0d15cbf8bf76c3775989342e73b4b62d46106523 100644 --- a/tbox/src/wGmshExport.h +++ b/tbox/src/wGmshExport.h @@ -39,6 +39,7 @@ public: virtual void save(std::string const &fname) const override; #ifndef SWIG virtual void save(std::string const &fname, Results const &r) const override; + virtual void saveList(std::string const &fname, Results const &r) const override; #endif virtual void write(std::ostream &out) const override; diff --git a/tbox/src/wMshExport.cpp b/tbox/src/wMshExport.cpp index 3eca10437b8c7a593ebc04431d422fcec8f1268f..6327e30fa12b2f4488c3f55507d25e608f60a63a 100644 --- a/tbox/src/wMshExport.cpp +++ b/tbox/src/wMshExport.cpp @@ -32,3 +32,7 @@ void MshExport::save(std::string const &fname, Results const &r) const { throw std::runtime_error("tbox::MshExport::save not implemented!\n"); } +void MshExport::saveList(std::string const &fname, Results const &r) const +{ + throw std::runtime_error("tbox::MshExport::save_ not implemented!\n"); +} diff --git a/tbox/src/wMshExport.h b/tbox/src/wMshExport.h index 19d6e3a056fac6a8092d599a5c1cc5715576d858..ec732a200ea7ff46bc94950e1357d82659aadb9b 100644 --- a/tbox/src/wMshExport.h +++ b/tbox/src/wMshExport.h @@ -54,6 +54,7 @@ public: virtual void save(std::string const &fname) const; #ifndef SWIG virtual void save(std::string const &fname, Results const &r) const; + virtual void saveList(std::string const &fname, Results const &r) const; #endif };