SERiF 0.0.1a
3+1D Stellar Structure and Evolution
Loading...
Searching...
No Matches
meshIO.cpp
Go to the documentation of this file.
1/* ***********************************************************************
2//
3// Copyright (C) 2025 -- The 4D-STAR Collaboration
4// File Author: Emily Boudreaux
5// Last Modified: March 18, 2025
6//
7// 4DSSE is free software; you can use it and/or modify
8// it under the terms and restrictions the GNU General Library Public
9// License version 3 (GPLv3) as published by the Free Software Foundation.
10//
11// 4DSSE is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14// See the GNU Library General Public License for more details.
15//
16// You should have received a copy of the GNU Library General Public License
17// along with this software; if not, write to the Free Software
18// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19//
20// *********************************************************************** */
21#include "mfem.hpp"
22#include <string>
23#include <iostream>
24#include <fstream>
25#include <stdexcept>
26
27#include "meshIO.h"
28
29namespace serif::mesh {
30 MeshIO::MeshIO(const std::string &mesh_file, double scale_factor)
31 {
32 mesh_file_ = mesh_file;
33 std::ifstream mesh_stream(mesh_file);
34 if (!mesh_stream)
35 {
36 throw std::runtime_error("Mesh file not found: " + mesh_file);
37 }
38 m_mesh = std::make_unique<mfem::Mesh>(mesh_stream, 1, 10);
39
40 bool discont = false;
41 int space_dim = m_mesh->SpaceDimension();
42 int ordering = mfem::Ordering::byVDIM;
43 // m_mesh->SetCurvature(3, discont, space_dim, ordering);
44 m_mesh->Finalize(false, true);
45 loaded_ = true;
46 if (scale_factor != 1.0) { LinearRescale(scale_factor); }
47 }
48
49 MeshIO::~MeshIO() = default;
50
51 void MeshIO::LinearRescale(const double scale_factor) const {
52 if (!loaded_) {
53 throw std::runtime_error("Mesh not loaded before rescaling.");
54 }
55
56 if (scale_factor <= 0.0) {
57 throw std::invalid_argument("Scale factor must be positive.");
58 }
59
60 // Ensure there are nodes even for linear order meshes
61 m_mesh->EnsureNodes();
62 const mfem::GridFunction *nodes = m_mesh->GetNodes();
63 double *node_data = nodes->GetData();
64 const int data_size = nodes->Size();
65
66 for (int i = 0; i < data_size; ++i) {
67 node_data[i] *= scale_factor;
68 }
69
70 // nodes->Update(); /updates the fespace
71 m_mesh->NodesUpdated();
72 }
73
74 bool MeshIO::IsLoaded() const
75 {
76 return loaded_;
77 }
78
79 mfem::Mesh &MeshIO::GetMesh() const noexcept {
80 return *m_mesh;
81 }
82}
void LinearRescale(double scale_factor) const
Rescale the mesh by a linear factor.
Definition meshIO.cpp:51
~MeshIO()
Destructor for the MeshIO class.
std::string mesh_file_
Filename of the mesh file.
Definition meshIO.h:34
MeshIO(const std::string &mesh_file, double scale_factor=1.0)
Constructor that initializes the MeshIO object with a mesh file.
Definition meshIO.cpp:30
bool IsLoaded() const
Check if the mesh is loaded.
Definition meshIO.cpp:74
mfem::Mesh & GetMesh() const noexcept
Get the mesh object.
Definition meshIO.cpp:79
std::unique_ptr< mfem::Mesh > m_mesh
The mesh object.
Definition meshIO.h:35
bool loaded_
Flag to indicate if the mesh is loaded.
Definition meshIO.h:33