Skip to content

Commit ef4828c

Browse files
committed
Tests are passing with the fixed reading of the specfem2d mesh
1 parent f70b8f0 commit ef4828c

File tree

9 files changed

+66
-43
lines changed

9 files changed

+66
-43
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ else(ENABLE_DOUBLE_PRECISION)
178178
set(TYPE_REAL "float")
179179
endif(ENABLE_DOUBLE_PRECISION)
180180

181+
# Configure the setup headers
181182
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/setup/specfem_setup.hpp.in
182183
${CMAKE_BINARY_DIR}/include/specfem_setup.hpp)
183184
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/setup/constants.hpp.in

include/IO/fortranio/fortran_io.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ void fortran_IO(std::ifstream &stream, int &buffer_length);
1414
void fortran_read_value(bool *value, std::ifstream &stream, int &buffer_length);
1515
void fortran_read_value(std::string *value, std::ifstream &stream,
1616
int &buffer_length);
17-
void fortran_read_value(type_real *value, std::ifstream &stream,
17+
void fortran_read_value(float *value, std::ifstream &stream,
18+
int &buffer_length);
19+
void fortran_read_value(double *value, std::ifstream &stream,
1820
int &buffer_length);
1921
void fortran_read_value(int *value, std::ifstream &stream, int &buffer_length);
2022
} // namespace IO

src/IO/fortranio/fortran_io.cpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,24 @@ void specfem::IO::fortran_read_value(int *value, std::ifstream &stream,
4141
return;
4242
}
4343

44-
void specfem::IO::fortran_read_value(type_real *value, std::ifstream &stream,
44+
void specfem::IO::fortran_read_value(float *value, std::ifstream &stream,
45+
int &buffer_length) {
46+
47+
float *temp;
48+
buffer_length -= ffloat;
49+
char *ivalue = new char[ffloat];
50+
if (buffer_length < 0) {
51+
std::cout << "buffer_length: " << buffer_length << std::endl;
52+
throw std::runtime_error("Error reading fortran file");
53+
}
54+
stream.read(ivalue, ffloat);
55+
temp = reinterpret_cast<float *>(ivalue);
56+
*value = *temp;
57+
delete[] ivalue;
58+
return;
59+
}
60+
61+
void specfem::IO::fortran_read_value(double *value, std::ifstream &stream,
4562
int &buffer_length) {
4663

4764
double *temp;
@@ -53,7 +70,7 @@ void specfem::IO::fortran_read_value(type_real *value, std::ifstream &stream,
5370
}
5471
stream.read(ivalue, fdouble);
5572
temp = reinterpret_cast<double *>(ivalue);
56-
*value = static_cast<type_real>(*temp);
73+
*value = *temp;
5774
delete[] ivalue;
5875
return;
5976
}

src/IO/mesh/impl/fortran/dim2/read_elements.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ specfem::IO::mesh::impl::fortran::dim2::read_axial_elements(
2929
specfem::mesh::elements::tangential_elements<specfem::dimension::type::dim2>
3030
specfem::IO::mesh::impl::fortran::dim2::read_tangential_elements(
3131
std::ifstream &stream, const int nnodes_tangential_curve) {
32-
type_real xread, yread;
32+
double xread, yread;
3333

3434
auto tangential_elements = specfem::mesh::elements::tangential_elements<
3535
specfem::dimension::type::dim2>(nnodes_tangential_curve);
@@ -41,8 +41,8 @@ specfem::IO::mesh::impl::fortran::dim2::read_tangential_elements(
4141
if (nnodes_tangential_curve > 0) {
4242
for (int inum = 0; inum < nnodes_tangential_curve; inum++) {
4343
specfem::IO::fortran_read_line(stream, &xread, &yread);
44-
tangential_elements.x(inum) = xread;
45-
tangential_elements.y(inum) = yread;
44+
tangential_elements.x(inum) = static_cast<type_real>(xread);
45+
tangential_elements.y(inum) = static_cast<type_real>(yread);
4646
}
4747
} else {
4848
tangential_elements.force_normal_to_surface = false;

src/IO/mesh/impl/fortran/dim2/read_material_properties.cpp

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ constexpr auto acoustic = specfem::element::medium_tag::acoustic;
1616

1717
struct input_holder {
1818
// Struct to hold temporary variables read from database file
19-
type_real val0, val1, val2, val3, val4, val5, val6, val7, val8, val9, val10,
19+
double val0, val1, val2, val3, val4, val5, val6, val7, val8, val9, val10,
2020
val11, val12;
2121
int n, indic;
2222
};
@@ -90,11 +90,12 @@ std::vector<specfem::mesh::materials::material_specification> read_materials(
9090

9191
// Acoustic Material
9292
if (read_values.val2 == 0) {
93-
const type_real density = read_values.val0;
94-
const type_real cp = read_values.val1;
95-
const type_real compaction_grad = read_values.val3;
96-
const type_real Qkappa = read_values.val5;
97-
const type_real Qmu = read_values.val6;
93+
const type_real density = static_cast<type_real>(read_values.val0);
94+
const type_real cp = static_cast<type_real>(read_values.val1);
95+
const type_real compaction_grad =
96+
static_cast<type_real>(read_values.val3);
97+
const type_real Qkappa = static_cast<type_real>(read_values.val5);
98+
const type_real Qmu = static_cast<type_real>(read_values.val6);
9899

99100
specfem::medium::material<acoustic, isotropic>
100101
acoustic_isotropic_holder(density, cp, Qkappa, Qmu,
@@ -113,12 +114,13 @@ std::vector<specfem::mesh::materials::material_specification> read_materials(
113114

114115
} else {
115116

116-
const type_real density = read_values.val0;
117-
const type_real cp = read_values.val1;
118-
const type_real cs = read_values.val2;
119-
const type_real compaction_grad = read_values.val3;
120-
const type_real Qkappa = read_values.val5;
121-
const type_real Qmu = read_values.val6;
117+
const type_real density = static_cast<type_real>(read_values.val0);
118+
const type_real cp = static_cast<type_real>(read_values.val1);
119+
const type_real cs = static_cast<type_real>(read_values.val2);
120+
const type_real compaction_grad =
121+
static_cast<type_real>(read_values.val3);
122+
const type_real Qkappa = static_cast<type_real>(read_values.val5);
123+
const type_real Qmu = static_cast<type_real>(read_values.val6);
122124

123125
specfem::medium::material<elastic, isotropic> elastic_isotropic_holder(
124126
density, cs, cp, Qkappa, Qmu, compaction_grad);
@@ -137,18 +139,18 @@ std::vector<specfem::mesh::materials::material_specification> read_materials(
137139
}
138140
// Ansotropic material
139141
else if (read_values.indic == 2) {
140-
const type_real density = read_values.val0;
141-
const type_real c11 = read_values.val1;
142-
const type_real c13 = read_values.val2;
143-
const type_real c15 = read_values.val3;
144-
const type_real c33 = read_values.val4;
145-
const type_real c35 = read_values.val5;
146-
const type_real c55 = read_values.val6;
147-
const type_real c12 = read_values.val7;
148-
const type_real c23 = read_values.val8;
149-
const type_real c25 = read_values.val9;
150-
const type_real Qkappa = read_values.val11;
151-
const type_real Qmu = read_values.val12;
142+
const type_real density = static_cast<type_real>(read_values.val0);
143+
const type_real c11 = static_cast<type_real>(read_values.val1);
144+
const type_real c13 = static_cast<type_real>(read_values.val2);
145+
const type_real c15 = static_cast<type_real>(read_values.val3);
146+
const type_real c33 = static_cast<type_real>(read_values.val4);
147+
const type_real c35 = static_cast<type_real>(read_values.val5);
148+
const type_real c55 = static_cast<type_real>(read_values.val6);
149+
const type_real c12 = static_cast<type_real>(read_values.val7);
150+
const type_real c23 = static_cast<type_real>(read_values.val8);
151+
const type_real c25 = static_cast<type_real>(read_values.val9);
152+
const type_real Qkappa = static_cast<type_real>(read_values.val11);
153+
const type_real Qmu = static_cast<type_real>(read_values.val12);
152154

153155
specfem::medium::material<elastic, anisotropic>
154156
elastic_anisotropic_holder(density, c11, c13, c15, c33, c35, c55, c12,

src/IO/mesh/impl/fortran/dim2/read_mesh_database.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ specfem::IO::mesh::impl::fortran::dim2::read_mesh_database_header(
1515
// This subroutine reads header values of the database which are skipped
1616
std::string dummy_s;
1717
int dummy_i, dummy_i1, dummy_i2;
18-
type_real dummy_d, dummy_d1;
18+
double dummy_d, dummy_d1; // there is no type_real in the meshfem fortran code
1919
bool dummy_b, dummy_b1, dummy_b2, dummy_b3;
2020
int nspec, npgeo, nproc;
2121

@@ -150,7 +150,7 @@ specfem::IO::mesh::impl::fortran::dim2::read_coorg_elements(
150150

151151
int ipoin = 0;
152152

153-
type_real coorgi, coorgj;
153+
double coorgi, coorgj;
154154
specfem::kokkos::HostView2d<type_real> coorg("specfem::mesh::coorg", ndim,
155155
npgeo);
156156

@@ -161,8 +161,8 @@ specfem::IO::mesh::impl::fortran::dim2::read_coorg_elements(
161161
}
162162
// coorg stores the x,z for every control point
163163
// coorg([0, 2), i) = [x, z]
164-
coorg(0, ipoin - 1) = coorgi;
165-
coorg(1, ipoin - 1) = coorgj;
164+
coorg(0, ipoin - 1) = static_cast<type_real>(coorgi);
165+
coorg(1, ipoin - 1) = static_cast<type_real>(coorgj);
166166
}
167167

168168
return coorg;
@@ -173,7 +173,7 @@ specfem::IO::mesh::impl::fortran::dim2::read_mesh_database_attenuation(
173173
std::ifstream &stream, const specfem::MPI::MPI *mpi) {
174174

175175
int n_sls;
176-
type_real attenuation_f0_reference;
176+
double attenuation_f0_reference;
177177
bool read_velocities_at_f0;
178178
specfem::IO::fortran_read_line(stream, &n_sls, &attenuation_f0_reference,
179179
&read_velocities_at_f0);
@@ -184,6 +184,7 @@ specfem::IO::mesh::impl::fortran::dim2::read_mesh_database_attenuation(
184184
// "because it is used to assign some arrays");
185185
// }
186186

187-
return std::make_tuple(n_sls, attenuation_f0_reference,
187+
return std::make_tuple(n_sls,
188+
static_cast<type_real>(attenuation_f0_reference),
188189
read_velocities_at_f0);
189190
}

tests/unit-tests/compute/partial_derivatives/compute_partial_derivatives_tests.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,13 @@ TEST(COMPUTE_TESTS, compute_partial_derivatives) {
8484
const int ngllz = compute_mesh.quadratures.gll.N;
8585
const int ngllx = compute_mesh.quadratures.gll.N;
8686

87-
specfem::testing::array3d<type_real, Kokkos::LayoutRight> xix_ref(
87+
specfem::testing::array3d<double, Kokkos::LayoutRight> xix_ref(
8888
test_config.xix_file, nspec, ngllz, ngllx);
89-
specfem::testing::array3d<type_real, Kokkos::LayoutRight> gammax_ref(
89+
specfem::testing::array3d<double, Kokkos::LayoutRight> gammax_ref(
9090
test_config.gammax_file, nspec, ngllz, ngllx);
91-
specfem::testing::array3d<type_real, Kokkos::LayoutRight> gammaz_ref(
91+
specfem::testing::array3d<double, Kokkos::LayoutRight> gammaz_ref(
9292
test_config.gammaz_file, nspec, ngllz, ngllx);
93-
specfem::testing::array3d<type_real, Kokkos::LayoutRight> jacobian_ref(
93+
specfem::testing::array3d<double, Kokkos::LayoutRight> jacobian_ref(
9494
test_config.jacobian_file, nspec, ngllz, ngllx);
9595

9696
for (int ix = 0; ix < ngllx; ++ix) {

tests/unit-tests/domain/rmass_inverse_tests.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ TEST(DOMAIN_TESTS, rmass_inverse) {
166166
<< std::endl;
167167

168168
if (Test.database.elastic_mass_matrix != "NULL") {
169-
specfem::testing::array2d<type_real, Kokkos::LayoutRight>
169+
specfem::testing::array2d<double, Kokkos::LayoutRight>
170170
h_mass_matrix_global(Test.database.elastic_mass_matrix, nglob, 2);
171171

172172
specfem::testing::array3d<int, Kokkos::LayoutRight> index_mapping(
@@ -226,7 +226,7 @@ TEST(DOMAIN_TESTS, rmass_inverse) {
226226
}
227227

228228
if (Test.database.acoustic_mass_matrix != "NULL") {
229-
specfem::testing::array2d<type_real, Kokkos::LayoutRight>
229+
specfem::testing::array2d<double, Kokkos::LayoutRight>
230230
h_mass_matrix_global(Test.database.acoustic_mass_matrix, nglob, 1);
231231

232232
specfem::testing::array3d<int, Kokkos::LayoutRight> index_mapping(

tests/unit-tests/fortran_io/fortranio_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ TEST(iotests, fortran_io) {
1616
int ival;
1717
bool bval;
1818
std::string sval;
19-
type_real dval;
19+
double dval;
2020
std::vector<int> vval(100, 0);
2121

2222
stream.open(filename);

0 commit comments

Comments
 (0)