feat(meshIO): LinearRescale method added

Rescaling a mesh by a linear transformation is a useful option so that we can start with a single "base" mesh and then rescale it to the dimensions needed for our problem. This commit adds the LinearRescale option too meshIO so that a unit sphere can be turned into a sphere of arbitrary radius (as an example).
This commit is contained in:
2025-02-24 16:35:39 -05:00
parent 83213f961b
commit 6416d95260
3 changed files with 38 additions and 3 deletions

View File

@@ -2,11 +2,12 @@
#include <string>
#include <iostream>
#include <fstream>
#include <stdexcept>
#include "meshIO.h"
MeshIO::MeshIO(const std::string &mesh_file)
MeshIO::MeshIO(const std::string &mesh_file, double scale_factor)
{
mesh_file_ = mesh_file;
std::ifstream mesh_stream(mesh_file);
@@ -19,6 +20,7 @@ MeshIO::MeshIO(const std::string &mesh_file)
{
mesh_ = mfem::Mesh(mesh_stream, 1, 10);
loaded_ = true;
if (scale_factor != 1.0) { LinearRescale(scale_factor); }
}
}
@@ -26,6 +28,32 @@ MeshIO::~MeshIO()
{
}
void MeshIO::LinearRescale(double scale_factor) {
if (!loaded_) {
throw std::runtime_error("Mesh not loaded before rescaling.");
}
if (scale_factor <= 0.0) {
throw std::invalid_argument("Scale factor must be positive.");
}
// Ensure there are nodes even for linear order meshes
mesh_.EnsureNodes();
const mfem::GridFunction *nodes = mesh_.GetNodes();
double *node_data = nodes->GetData(); // THIS IS KEY
int data_size = nodes->Size();
for (int i = 0; i < data_size; ++i) {
node_data[i] *= scale_factor;
}
// nodes->Update(); /updates the fespace
mesh_.NodesUpdated();
}
bool MeshIO::IsLoaded() const
{
return loaded_;