Skip to content

Commit e139533

Browse files
committed
Fix read-in of old .npz files
1 parent ecb669a commit e139533

File tree

4 files changed

+282
-231
lines changed

4 files changed

+282
-231
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@
2525
file, instead of overwriting the user provided file. The temporary file is
2626
removed after it has been parsed. Fixes #91 as noted by @ilfreddy.
2727

28+
### Fixed
29+
30+
- A bug in the initialization of a restart cavity from old `.npz` files.
31+
Currently, the `.npz` file saves sphere center, arcs and vertices of each
32+
finite element. This information is in fact needed to plot the cavity using
33+
the Python script in `tools`. Older `.npz` files did not contain this
34+
information and could not be read in. The additional information is read in
35+
as arrays of zeros in case it is not present on file.
36+
2837
## [Version 1.1.10] - 2017-03-27
2938

3039
### Changed

src/cavity/ICavity.cpp

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@
2929

3030
#include <Eigen/Core>
3131

32-
#include "utils/cnpy.hpp"
3332
#include "utils/MathUtils.hpp"
3433
#include "utils/Sphere.hpp"
3534
#include "utils/Symmetry.hpp"
35+
#include "utils/cnpy.hpp"
3636

3737
namespace pcm {
3838
ICavity::ICavity() : nElements_(0), built(false) {}
@@ -102,10 +102,15 @@ void ICavity::loadCavity(const std::string & fname) {
102102
PCMSOLVER_ERROR("elementArea_: incoherent dimensions read in");
103103
elementArea_ = cnpy::custom::npy_to_eigen<double>(raw_weights);
104104
// 2. Get the element sphere center
105-
cnpy::NpyArray raw_elSphCenter = loaded_cavity["elSphCenter"];
106-
if (raw_elSphCenter.shape[1] != nElements_)
107-
PCMSOLVER_ERROR("elementSphereCenter_: incoherent dimensions read in");
108-
elementSphereCenter_ = cnpy::custom::npy_to_eigen<double>(raw_elSphCenter);
105+
if (loaded_cavity.find("elSphCenter") == loaded_cavity.end()) {
106+
// Element sphere center was not found on file, fill it up with zeros
107+
elementSphereCenter_ = Eigen::Matrix3Xd::Zero(3, nElements_);
108+
} else {
109+
cnpy::NpyArray raw_elSphCenter = loaded_cavity["elSphCenter"];
110+
if (raw_elSphCenter.shape[1] != nElements_)
111+
PCMSOLVER_ERROR("elementSphereCenter_: incoherent dimensions read in");
112+
elementSphereCenter_ = cnpy::custom::npy_to_eigen<double>(raw_elSphCenter);
113+
}
109114
// 3. Get the element radius
110115
cnpy::NpyArray raw_elRadius = loaded_cavity["elRadius"];
111116
if (raw_elRadius.shape[0] != nElements_)
@@ -122,6 +127,9 @@ void ICavity::loadCavity(const std::string & fname) {
122127
PCMSOLVER_ERROR("elementNormal_: incoherent dimensions read in");
123128
elementNormal_ = cnpy::custom::npy_to_eigen<double>(raw_normals);
124129

130+
bool has_arcs = loaded_cavity.find("arcs_0") == loaded_cavity.end() ? false : true;
131+
bool has_vertices =
132+
loaded_cavity.find("vertices_0") == loaded_cavity.end() ? false : true;
125133
// Reconstruct the elements_ vector
126134
for (PCMSolverIndex i = 0; i < nElements_; ++i) {
127135
bool irr = false;
@@ -131,10 +139,20 @@ void ICavity::loadCavity(const std::string & fname) {
131139
Sphere sph(elementSphereCenter_.col(i), elementRadius_(i));
132140
Eigen::Matrix3Xd vertices, arcs;
133141
// 6. Get vertices and arcs
134-
cnpy::NpyArray raw_vertices = loaded_cavity["vertices_" + pcm::to_string(i)];
135-
vertices = cnpy::custom::npy_to_eigen<double>(raw_vertices);
136-
cnpy::NpyArray raw_arcs = loaded_cavity["arcs_" + pcm::to_string(i)];
137-
arcs = cnpy::custom::npy_to_eigen<double>(raw_arcs);
142+
if (has_vertices) {
143+
cnpy::NpyArray raw_vertices = loaded_cavity["vertices_" + pcm::to_string(i)];
144+
vertices = cnpy::custom::npy_to_eigen<double>(raw_vertices);
145+
} else {
146+
// Vertices were not found on file, fill them up with zeros
147+
vertices = Eigen::Matrix3Xd::Zero(3, 3);
148+
}
149+
if (has_arcs) {
150+
cnpy::NpyArray raw_arcs = loaded_cavity["arcs_" + pcm::to_string(i)];
151+
arcs = cnpy::custom::npy_to_eigen<double>(raw_arcs);
152+
} else {
153+
// Arcs were not found on file, fill them up with zeros
154+
arcs = Eigen::Matrix3Xd::Zero(3, 3);
155+
}
138156
if (arcs.cols() != vertices.cols())
139157
PCMSOLVER_ERROR("Inconsistent number of vertices read from file for element " +
140158
pcm::to_string(i));

src/cavity/RestartCavity.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ namespace pcm {
3535
namespace cavity {
3636
std::ostream & RestartCavity::printCavity(std::ostream & os) {
3737
os << "Cavity type: Restart" << std::endl;
38-
os << "Number of finite elements = " << nElements_;
38+
os << "Number of finite elements = " << nElements_ << std::endl;
3939
return os;
4040
}
4141

0 commit comments

Comments
 (0)