TREX-CoE / trexio

TREX I/O library
https://trex-coe.github.io/trexio/
BSD 3-Clause "New" or "Revised" License
49 stars 14 forks source link

Loss of precision in Python #116

Closed scemama closed 1 year ago

scemama commented 1 year ago

I wrote the nuclear repulsion in a TREXIO file. The nuclear repulsion computed by quantum package is

* Nuclear repulsion energy                           9.189533758614488    

I store it in a TREXIO file, and when I read it back in Python I get:

trexio.read_nucleus_repulsion(f)
9.189534187316895

The value given by Python is the same with both the text and the HDF5 back-ends.

When I inspect the text files produced by the text backend, I can see that the value of the nuclear repulsion is correct:

nucleus_repulsion   9.1895337586144876e+00                                                   

So I believe there is a conversion from float64 to float32 in the python interface after reading, but I could not figure out where it comes from...

It seems that I get the problem only for scalars. Arrays seem to be OK.

scemama commented 1 year ago

Note: When I read the value in C, it is correct.

#include <trexio.h>
#include <stdio.h>

int main() {
  trexio_exit_code rc;
  trexio_t* trexio_file = trexio_open("h2o_dz.h5", 'r', TREXIO_AUTO, &rc);

  double rep;
  rc = trexio_read_nucleus_repulsion(trexio_file, &rep);                                     
  printf("%25.20f\n", rep);
}
$ ./a.out
   9.18953375861448762407
q-posev commented 1 year ago

@scemama You can try to replace the following line with the following %apply double *OUTPUT { double* const num}; and re-install the Python API

scemama commented 1 year ago

Thanks! It works! :-)