Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fortran IO read size inconsistent #465

Open
lsawade opened this issue Feb 11, 2025 · 0 comments · Fixed by #467
Open

Fortran IO read size inconsistent #465

lsawade opened this issue Feb 11, 2025 · 0 comments · Fixed by #467

Comments

@lsawade
Copy link
Collaborator

lsawade commented Feb 11, 2025

Describe the bug

specfem_setup.hpp defines sizes of some numbers:

using type_real = float;
const static int ndim{ 2 };
const static int fint{ 4 }, fdouble{ 8 }, fbool{ 4 }, fchar{ 512 };

and fortran_read_value(type_real *value, ....) is defined as follows.

void specfem::IO::fortran_read_value(type_real *value, std::ifstream &stream,
                                     int &buffer_length) {

  buffer_length -= fdouble;         // <-------------------------
  char *ivalue = new char[fdouble]; // <-------------------------
  if (buffer_length < 0) {
    throw std::runtime_error("Error reading fortran file in type_real");
  }
  stream.read(ivalue, freal);
  *value = *reinterpret_cast<type_real *>(ivalue);
  delete[] ivalue;
  return;
}

This worked for meshfem2d (fortran) because the "in-repo"'s mesher has CUSTOM_REAL set to SIZE_DOUBLE aka 8.

** Solution **

Here, we really want to define the the parameters using enable_double_precision in the cmake file.

Fix the 2d meshing and reading thereof by assuming a double?

using type_real = float;
const static int ndim{ 2 };
const static int fint{ 4 }, ffloat{ 4 }, fdouble{ 8 }, fbool{ 4 }, fchar{ 512 };
constexpr static int freal = sizeof(type_real);

And as a consequence:

...
void fortran_read_value(type_real *value, std::ifstream &stream,
                        int &buffer_length);
#if freal != ffloat
void fortran_read_value(float *value, std::ifstream &stream, int &buffer_length);
#else
void fortran_read_value(double *value, std::ifstream &stream, int &buffer_length);
#endif
void fortran_read_value(int *value, std::ifstream &stream, int &buffer_length);
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant