AdhityaRavi
AdhityaRavi

Reputation: 79

CGNS file 0 is not open error - Cannot write a cgns file using fortran

I am new to fortran. I am trying to write a Flow solution in CGNS format using fortran 95. I wrote the fortran code and created a library of this fortran code for python. I want to use this library to write my mesh and flow data which is in *.npy format to *.cgns format. My fortran code snippet is as follows

subroutine un_2d_tr(filename, zoneName)
 implicit none
 include 'cgnslib_f.h'

 character(*) fileName, zoneName 
 integer :: ier, cellDim, physDim, nelem_start, nelem_end, nbdyelem   
 integer :: iFile, iB, iCoordX, iCoordY, iSection, iFlow, iu, iv 
 integer, dimension(1,3) :: isize  
 character(len=32) :: basename, solname

 ! --------------------------------------------------------------------  
 ! open CGNS file to write OR edit and create/read base  
 basename = 'Base'  

 ! In 2D unstr.  
 cellDim=2
 physDim=2

 call cg_open_f(fileName,cg_mode_write,iFile,ier)   
 call check_cg_error_f(ier)

 ! write base    
 call cg_base_write_f(iFile,basename,cellDim,physDim, iB,ier)    
 call check_cg_error_f(ier)
 ...

And I continued the script to write the mesh and flow velocity into the cgns file. There is no error in opening a new file using cg_open_f() to write (error status ier is 0). But, when I try to write something using cg_write_f() or cg_base_write_f() (for writing the base flow) in the opened file, I am getting the following error

CGNS file 0 is not open

I can post the full fortran subroutine if needed. Does anyone have any suggestions on how to correct this error?. Could this be a problem with my linux distribution? if it helps, I am using Ubuntu 17.10. I have included the check_cg_error_f() below.

subroutine check_cg_error_f(ier)

 implicit none
 include 'cgnslib_f.h'
 integer ier

 if (ier .ne. CG_OK) then
   call cg_error_exit_f
 endif

end 

The cmake file I used to build the cgns library is as follows

BUILD_CGNSTOOLS                  OFF
CGNS_BUILD_SHARED                ON
CGNS_USE_SHARED                  ON
CMAKE_BUILD_TYPE                 Release
CMAKE_INSTALL_PREFIX             /home/adhitya/.local/cgns/3.1.4
ENABLE_64BIT                     ON
ENABLE_FORTRAN                   ON
ENABLE_HDF5                      ON
ENABLE_SCOPING                   OFF
ENABLE_TESTS                     OFF
FORTRAN_NAMING                   LOWERCASE_
HDF5_INCLUDE_PATH                /home/adhitya/.local/hdf5/1.8.16/include
HDF5_LIBRARY                     /home/adhitya/.local/hdf5/1.8.16/lib/libhdf5.so
HDF5_NEED_MPI                    OFF
HDF5_NEED_SZIP                   ON
HDF5_NEED_ZLIB                   ON
SZIP_LIBRARY                     /home/adhitya/.local/szip/lib/libsz.so
ZLIB_LIBRARY                     /home/adhitya/.local/zlib/lib/libz.so

cmake build for version 3.3.1

CGNS_BUILD_CGNSTOOLS             OFF                                          
CGNS_BUILD_SHARED                ON                                           
CGNS_BUILD_TESTING               OFF                                          
CGNS_ENABLE_64BIT                ON                                           
CGNS_ENABLE_BASE_SCOPE           OFF                                          
CGNS_ENABLE_FORTRAN              ON                                           
CGNS_ENABLE_HDF5                 ON                                           
CGNS_ENABLE_MEM_DEBUG            OFF                                          
CGNS_ENABLE_SCOPING              OFF                                          
CGNS_ENABLE_TESTS                OFF                                          
CGNS_USE_SHARED                  ON                                           
CMAKE_BUILD_TYPE                 Release                                      
CMAKE_INSTALL_PREFIX             /home/adhitya/.local/cgns/3.3.1              
HDF5_C_LIBRARY_dl                /usr/lib/x86_64-linux-gnu/libdl.so           
HDF5_C_LIBRARY_hdf5              /home/adhitya/.local/hdf5/lib/libhdf5.so     
HDF5_C_LIBRARY_m                 /usr/lib/x86_64-linux-gnu/libm.so            
HDF5_C_LIBRARY_sz                /usr/lib/x86_64-linux-gnu/libsz.so 
HDF5_C_LIBRARY_z                 /usr/lib/x86_64-linux-gnu/libz.so            
HDF5_DIR                         HDF5_DIR-NOTFOUND                            
HDF5_NEED_MPI                    OFF                                          
HDF5_NEED_SZIP                   ON                                           
HDF5_NEED_ZLIB                   ON                                           
SZIP_LIBRARY                     /home/adhitya/.local/szip/lib/libsz.so       
ZLIB_LIBRARY                     /usr/lib/x86_64-linux-gnu/libz.so                 

Upvotes: 0

Views: 732

Answers (2)

Timothy Brown
Timothy Brown

Reputation: 2280

I don't fully know what your error is. However I suspect it is some type mismatch. I highly recommend use cgns instead of include. It's 2018! Let the compiler tell you if your calling the routine in-correctly.

CGNS Test Program

Fortran

program test_cgns

    use, intrinsic :: iso_fortran_env, only: error_unit
    use cgns

    implicit none

    integer                             :: ierr      ! error status
    integer                             :: fid       ! file id
    integer                             :: bid       ! base id
    character(len=256)                  :: filename  ! file name
    character(len=256), parameter       :: base ='Base'
    integer,            parameter       :: dims(3) = [10, 10, 10]
    integer                             :: ndims

    call get_command_argument(1, filename)
    if (len_trim(filename) == 0) then
        write(error_unit, *) 'ERROR: Must supply a filename.'
    end if

    call cg_set_file_type_f(CG_FILE_HDF5, ierr)
    if (ierr /= CG_OK) then
        write(error_unit, *) 'Unable to set file type to HDF5'
        call cg_error_print_f
        stop
    end if

    call cg_open_f(filename, CG_MODE_WRITE, fid, ierr)
    if (ierr /= CG_OK) then
        write(error_unit, *) 'Unable to open: ' // trim(filename)
        call cg_error_print_f
        stop
    end if

    ndims = size(dims)    
    call cg_base_write_f(fid, base, ndims, ndims, bid, ierr)
    if (ierr /= CG_OK) then
        write(error_unit, *) 'Unable to create base: ' // trim(base)
        call cg_error_print_f
        stop
    end if

    call cg_close_f(fid, ierr)
    if (ierr /= CG_OK) then
        write(error_unit, *) 'Unable to close data file'
        call cg_error_print_f
    end if

end program test_cgns

C

#include <stdlib.h>
#include <stdio.h>
#include <err.h>
#include <cgnslib.h>

int
main(int argc, char **argv)
{
    int fid = 0;
    int bid = 0;
    int ndims = 3;
    char *filename = NULL;
    char *base = "Base";

    if (argc != 2) {
        errx(EXIT_FAILURE, "Must supply a filename");
    }
    filename = argv[1];

    if (cg_set_file_type(CG_FILE_HDF5)) {
        warnx("Unable to set file type to HDF5");
        cg_error_exit();
    }

    if (cg_open(filename, CG_MODE_WRITE, &fid)) {
        warnx("Unable to open file: %s", filename);
        cg_error_exit();
    }

    if (cg_base_write(fid, base, ndims, ndims, &bid)) {
        warnx("Unable to create base: %s", base);
        cg_error_exit();
    }

    if (cg_close(fid)) {
        warnx("Unable to close data file");
        cg_error_exit();
    }

    return(EXIT_SUCCESS);
}

Then to compile it, (please note you might have to add -I and -L flags to tell the compiler where the cgns.mod and libcgns.so are):

$ gfortran -o test_cgns test_cgns.f90 -lcgns

or

$ gcc -o test_cgns test_cgns.c -lcgns

Run the test and look at the output file:

$ ./test_cgns foo.h5
$ echo $?
 0
$ h5dump foo.h5
HDF5 "foo.h5" {
GROUP "/" {
   ATTRIBUTE "label" {
      DATATYPE  H5T_STRING {
         STRSIZE 33;
         STRPAD H5T_STR_NULLTERM;
         CSET H5T_CSET_ASCII;
         CTYPE H5T_C_S1;
      }
      DATASPACE  SCALAR
      DATA {
      (0): "Root Node of HDF5 File"
      }
   }
 ....

Are you able to compile and run this simple test program?


Building CGNS

I just grabbed the source and did an install on another cluster. Please note, I'm using the Intel Compiler Suite not GCC.

 CGNS_BUILD_CGNSTOOLS             OFF                                                                                                                     
 CGNS_BUILD_SHARED                ON                                                                                                                      
 CGNS_BUILD_TESTING               OFF                                                                                                                     
 CGNS_ENABLE_64BIT                ON                                                                                                                      
 CGNS_ENABLE_BASE_SCOPE           OFF                                                                                                                     
 CGNS_ENABLE_FORTRAN              ON                                                                                                                      
 CGNS_ENABLE_HDF5                 ON                                                                                                                      
 CGNS_ENABLE_MEM_DEBUG            OFF                                                                                                                     
 CGNS_ENABLE_SCOPING              OFF                                                                                                                     
 CGNS_ENABLE_TESTS                OFF                                                                                                                     
 CGNS_USE_SHARED                  ON                                                                                                                      
 CMAKE_BUILD_TYPE                 Release                                                                                                                 
 CMAKE_INSTALL_PREFIX             /home/tibr1099/cgns/3.3.1                                                                                               
 HDF5_C_LIBRARY_dl                /usr/lib64/libdl.so                                                                                                     
 HDF5_C_LIBRARY_hdf5              /curc/sw/hdf5/1.8.18/intel/17.4/lib/libhdf5.so                                                                          
 HDF5_C_LIBRARY_m                 /usr/lib64/libm.so                                                                                                      
 HDF5_C_LIBRARY_sz                /curc/sw/szip/2.1.1/intel/17.4/lib/libsz.so                                                                             
 HDF5_C_LIBRARY_z                 /curc/sw/zlib/1.2.11/intel/17.4/lib/libz.so                                                                             
 HDF5_NEED_MPI                    OFF                                                                                                                     
 HDF5_NEED_SZIP                   OFF                                                                                                                     
 HDF5_NEED_ZLIB                   OFF

Followed by the usual make install, then looking in the installed include directory:

$ ls ~/cgns/3.3.1/include/
cgns.mod  cgnsBuild.defs  cgns_io.h  cgnsconfig.h  cgnslib.h  cgnstypes.h  cgnstypes_f.h  cgnstypes_f03.h  cgnswin_f.h

Without seeing your build log, I would dare say delete your installation and start again. Also 3.1.4 is not the latest stable. How about grabbing 3.3.1.

Upvotes: 1

Aristotelis
Aristotelis

Reputation: 141

I am not familiar with CGNS but as a first check, I would suggest to verify that a sensible value is assigned to iFile variable after executing cg_open_f(). Just another thought, are you trying to write on a pre-existing file?

Upvotes: 0

Related Questions