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 =
92 inp::ModelDeck::getExampleJson(dim, 0.001, 10, "finite_difference", "central_difference", true,
93 2, "Multi_Particle", 0);
94 inp::ModelDeck modelDeck(modelJson);
95
97 mesh_gen::generateBuiltinParticleMeshGmsh(geom, meshSize, "", false, false, &mesh, &meshDeck,
98 &modelDeck);
99
100 if (mesh.getNodes().empty()) {
101 util::io::log(std::format("Error: mesh check: {} produced zero nodes.\n", geomName));
102 return false;
103 }
104
105 const auto box = geom->box();
106 const double tol = 4.0 * meshSize;
107 for (const auto &p : mesh.getNodes()) {
108 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 ||
109 p.d_x > box.second.d_x + tol || p.d_y > box.second.d_y + tol || p.d_z > box.second.d_z + tol) {
110 util::io::log(std::format(
111 "Error: mesh check {}: node ({},{},{}) outside geometry box (tol {}).\n", geomName, p.d_x,
112 p.d_y, p.d_z, tol));
113 return false;
114 }
115 }
116
117 util::io::log(std::format(" mesh check [ {} ] passed.\n", geomName));
118 return true;
119 } catch (const std::exception &e) {
120 util::io::log(std::format("Error: mesh check {}: {}\n", geomName, e.what()));
121 return false;
122 }
123}
124
125} // namespace
126
127int main() {
128 util::io::log("Starting mesh generator tests (loop over geom::getAcceptableGeometries)...\n");
129
130 bool allTestsPassed = true;
131
133 util::io::log("Circle mesh file output test failed.\n");
134 allTestsPassed = false;
135 }
136
137 const double s = 0.001;
138 util::io::log("Builtin Gmsh mesh checks (exampleGeomParams + bounding box)...\n");
139 for (const std::string &name : geom::getAcceptableGeometries()) {
140 // "complex" needs subtype/flag vectors; not covered by exampleGeomParams.
141 if (name == "complex")
142 continue;
143 if (!builtinMeshCheckForGeometry(name, s)) {
144 util::io::log(std::format("Mesh check failed for geometry: {}\n", name));
145 allTestsPassed = false;
146 }
147 }
148
149 if (allTestsPassed) {
150 util::io::log("All mesh generator tests passed.\n");
151 return 0;
152 }
153 util::io::log("Some mesh generator tests failed.\n");
154 return 1;
155}
Defines annulus rectangle.
size_t d_dim
Dimension objects live in.
A class for mesh data.
Definition mesh.h:53
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:28
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:26
double d_hMeshing
Target spacing for in-built meshers only (Gmsh uniform / uniform rectangle grid).
Definition meshDeck.h:38
std::string d_filename
Filename to read mesh data.
Definition meshDeck.h:29
Structure to read and store model related input data.
Definition modelDeck.h:25
static json getExampleJson(size_t dim=2, double tFinal=1.0, size_t Nt=10, std::string spatialDiscretization="finite_difference", std::string timeDiscretization="central_difference", bool populateElementNodeConnectivity=true, size_t quadOrder=2, std::string particleSimType="Multi_Particle", int seed=0)
Returns example JSON object for ModelDeck configuration.
Definition modelDeck.h:164
A structure to represent 3d vectors.
Definition point.h:30
int main()