PeriDEM 0.3.0
PeriDEM -- Peridynamics-based high-fidelity model for granular media
Loading...
Searching...
No Matches
testMeshGen.cpp
Go to the documentation of this file.
1/*
2 * -------------------------------------------
3 * Copyright (c) 2021 - 2026 Prashant K. Jha
4 * -------------------------------------------
5 * PeriDEM https://github.com/prashjha/PeriDEM
6 *
7 * Distributed under the Boost Software License, Version 1.0. (See accompanying
8 * file LICENSE)
9 */
10
13#include "geom/geomObjects.h"
15#include "inp/meshDeck.h"
16#include "inp/modelDeck.h"
17#include "mesh/mesh.h"
18#include "util/io.h"
19#include "util/point.h"
20#include <cmath>
21#include <filesystem>
22#include <format>
23#include <memory>
24#include <vector>
25
26namespace fs = std::filesystem;
27
28namespace {
29
33size_t meshDimForBuiltin(const std::string &deckName, const std::shared_ptr<geom::GeomObject> &obj) {
34 if (obj->d_name == "annulus_object")
35 return static_cast<const geom::AnnulusGeomObject &>(*obj).d_dim;
36 return geom::getGeomTypeToDim(deckName);
37}
38
43 util::io::log("Testing circle mesh file output (gmsh_builtin_mesh)...\n");
44
45 const std::vector<double> center = {0.0, 0.0, 0.0};
46 const double radius = 0.001;
47 const double meshSize = radius / 5.0;
48
49 const fs::path outputDir = "test_output/mesh_gen";
50 fs::create_directories(outputDir);
51
52 try {
53 auto circle =
54 std::make_shared<geom::Circle>(radius, util::Point(center[0], center[1], center[2]));
55 mesh_gen::generateBuiltinParticleMeshGmsh(circle, meshSize, (outputDir / "circle").string(),
56 true, true, nullptr, nullptr, nullptr);
57
58 const bool mshOk = fs::exists(outputDir / "circle.msh");
59 const bool vtkOk = fs::exists(outputDir / "circle.vtk");
60 if (!mshOk || !vtkOk) {
61 util::io::log("Error: Expected circle.msh and circle.vtk under test_output/mesh_gen.\n");
62 if (!mshOk)
63 util::io::log("Missing: circle.msh\n");
64 if (!vtkOk)
65 util::io::log("Missing: circle.vtk\n");
66 return false;
67 }
68
69 util::io::log("Circle mesh file output test passed.\n");
70 return true;
71 } catch (const std::exception &e) {
72 util::io::log(std::format("Error in mesh generation: {}\n", e.what()));
73 return false;
74 }
75}
76
81bool builtinMeshCheckForGeometry(const std::string &geomName, double s) {
82 try {
83 auto geom = geom::makeExampleGeomObject(geomName, util::Point(0., 0., 0.), s);
84 const double meshSize = s / 5.0;
85
86 inp::MeshDeck meshDeck;
87 meshDeck.d_hMeshing = meshSize;
88 meshDeck.d_filename = std::string("mesh_") + geomName + ".msh";
89
90 const size_t dim = meshDimForBuiltin(geomName, geom);
91 const auto modelJson =
93 json{{"Dimension", dim},
94 {"Final_Time", 0.001},
95 {"Time_Steps", 10},
96 {"Discretization_Type", json{{"Spatial", "finite_difference"}, {"Time", "central_difference"}}},
97 {"Populate_ElementNodeConnectivity", true},
98 {"Quad_Approximation_Order", 2},
99 {"Particle_Sim_Type", "Multi_Particle"},
100 {"Seed", 0}});
101 inp::ModelDeck modelDeck(modelJson);
102
104 mesh_gen::generateBuiltinParticleMeshGmsh(geom, meshSize, "", false, false, &mesh, &meshDeck,
105 &modelDeck);
106
107 if (mesh.getNodes().empty()) {
108 util::io::log(std::format("Error: mesh check: {} produced zero nodes.\n", geomName));
109 return false;
110 }
111
112 const auto box = geom->box();
113 const double tol = 4.0 * meshSize;
114 for (const auto &p : mesh.getNodes()) {
115 if (p.d_x < box.first.d_x - tol || p.d_y < box.first.d_y - tol || p.d_z < box.first.d_z - tol ||
116 p.d_x > box.second.d_x + tol || p.d_y > box.second.d_y + tol || p.d_z > box.second.d_z + tol) {
117 util::io::log(std::format(
118 "Error: mesh check {}: node ({},{},{}) outside geometry box (tol {}).\n", geomName, p.d_x,
119 p.d_y, p.d_z, tol));
120 return false;
121 }
122 }
123
124 util::io::log(std::format(" mesh check [ {} ] passed.\n", geomName));
125 return true;
126 } catch (const std::exception &e) {
127 util::io::log(std::format("Error: mesh check {}: {}\n", geomName, e.what()));
128 return false;
129 }
130}
131
132} // namespace
133
134int main() {
135 util::io::log("Starting mesh generator tests (loop over geom::getAcceptableGeometries)...\n");
136
137 bool allTestsPassed = true;
138
140 util::io::log("Circle mesh file output test failed.\n");
141 allTestsPassed = false;
142 }
143
144 const double s = 0.001;
145 util::io::log("Builtin Gmsh mesh checks (exampleGeomParams + bounding box)...\n");
146 for (const std::string &name : geom::getAcceptableGeometries()) {
147 // "complex" needs subtype/flag vectors; not covered by exampleGeomParams.
148 if (name == "complex")
149 continue;
150 if (!builtinMeshCheckForGeometry(name, s)) {
151 util::io::log(std::format("Mesh check failed for geometry: {}\n", name));
152 allTestsPassed = false;
153 }
154 }
155
156 if (allTestsPassed) {
157 util::io::log("All mesh generator tests passed.\n");
158 return 0;
159 }
160 util::io::log("Some mesh generator tests failed.\n");
161 return 1;
162}
Defines annulus rectangle.
size_t d_dim
Dimension objects live in.
A class for mesh data.
Definition mesh.h:53
nlohmann::ordered_json json
bool testCircleMeshWritesFiles()
Circle: write .msh and .vtk via the built-in Gmsh path (file I/O check).
bool builtinMeshCheckForGeometry(const std::string &geomName, double s)
For one acceptable geometry name: exampleGeomParams → mesh in memory → non-empty nodes,...
size_t meshDimForBuiltin(const std::string &deckName, const std::shared_ptr< geom::GeomObject > &obj)
Model dimension for mesh deck (annulus deck names map to annulus_object with d_dim set).
std::shared_ptr< GeomObject > makeExampleGeomObject(const std::string &geom_type, const util::Point &c, double s)
One-liner: createGeomObject(exampleGeomParams(...)). Prefer this over per-class static factories so a...
const std::vector< std::string > & getAcceptableGeometries()
Returns list of acceptable geometries for PeriDEM simulation.
size_t getGeomTypeToDim(std::string type)
Returns list of acceptable geometries for PeriDEM simulation.
Definition geomObjects.h:47
void generateBuiltinParticleMeshGmsh(const std::shared_ptr< geom::GeomObject > &geomObj, double h, const std::string &filenameStem, bool vtk_out, bool write_mesh_file, mesh::Mesh *out_mesh, const inp::MeshDeck *meshDeck, const inp::ModelDeck *modelDeck)
In-process Gmsh mesh from geom::GeomObject (after createGeomObject on deck / Particle data).
Collection of methods and data related to finite element and mesh.
Definition mesh.cpp:29
void log(std::ostringstream &oss, bool screen_out=false, int printMpiRank=print_default_mpi_rank)
Global method to log the message.
Definition io.cpp:41
Structure to read and store mesh related input data.
Definition meshDeck.h:28
double d_hMeshing
Target spacing for in-built meshers only (Gmsh uniform / uniform rectangle grid).
Definition meshDeck.h:40
std::string d_filename
Filename to read mesh data.
Definition meshDeck.h:31
Structure to read and store model related input data.
Definition modelDeck.h:27
static json getExampleJson(const json &given=json::object())
Returns the block with the given fields set.
Definition modelDeck.h:217
A structure to represent 3d vectors.
Definition point.h:30
int main()