【问题标题】:CGNS file 0 is not open error - Cannot write a cgns file using fortranCGNS 文件 0 未打开错误 - 无法使用 fortran 写入 cgns 文件
【发布时间】:2018-01-25 20:38:27
【问题描述】:

我是 fortran 新手。我正在尝试使用 fortran 95 以 CGNS 格式编写 Flow 解决方案。我编写了 fortran 代码并为 python 创建了这个 fortran 代码的库。我想使用这个库将*.npy 格式的网格和流数据写入*.cgns 格式。我的fortran代码sn-p如下

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)
 ...

然后我继续脚本将网格和流速写入 cgns 文件。使用cg_open_f()写入打开新文件没有错误(错误状态ier为0)。但是,当我尝试在打开的文件中使用cg_write_f()cg_base_write_f()(用于编写基本流程)编写内容时,我收到以下错误

CGNS file 0 is not open

如果需要,我可以发布完整的 fortran 子例程。有没有人对如何纠正这个错误有任何建议?这可能是我的 linux 发行版的问题吗?如果有帮助,我使用的是 Ubuntu 17.10。我在下面包含了check_cg_error_f()

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 

我用来搭建cgns库的cmake文件如下

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

3.3.1 版本的 cmake 构建

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                 

【问题讨论】:

  • 你能显示 check_cg_error_f 吗?我无法在 CGNS 文档中或通过网络搜索找到它。
  • 我用check_cg_error_f() 子程序编辑了我的帖子。
  • 如果所有这些 cg 例程的源代码都可用,这里的链接会很有用。我想知道cg_open 只是做一个open 还是验证路径的有效性等等。
  • @agentp 其他 cg 例程来自 CGNS 库。可以找到这些cg套路的描述链接here
  • 是的,找到了顶级描述符,正在寻找源代码。

标签: python linux fortran cgns


【解决方案1】:

我不完全知道你的错误是什么。但是我怀疑这是某种类型不匹配。我强烈推荐use cgns 而不是include。现在是 2018 年!让编译器告诉您是否正确调用了例程。

CGNS 测试程序

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);
}

然后编译它,(请注意,您可能需要添加 -I 和 -L 标志来告诉编译器 cgns.modlibcgns.so 在哪里):

$ gfortran -o test_cgns test_cgns.f90 -lcgns

$ gcc -o test_cgns test_cgns.c -lcgns

运行测试并查看输出文件:

$ ./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"
      }
   }
 ....

你能编译和运行这个简单的测试程序吗?


构建 CGNS

我刚刚获取了源并在另一个集群上进行了安装。请注意,我使用的是英特尔编译器套件而不是 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

后面跟着通常的make install,然后查看安装的包含目录:

$ 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

如果没有看到您的构建日志,我敢说删除您的安装并重新开始。 3.1.4 也不是最新的稳定版。抢3.3.1怎么样。

【讨论】:

  • 我的 cgns 包的 include 文件夹中似乎没有任何 cgns.mod。我尝试重新安装cgns,它没有工作。我也是 CGNS 的新手。因此,我对 fortran 和 cgns 的了解非常有限。如果有帮助,我已经用我用来构建我的 cgns 库的 cmake 文件编辑了我的问题。
  • 当我尝试安装 CGNS v3.3.1 时,在 cmake 文件中,我收到以下错误 HDF5_DIR HDF5_DIR-NOTFOUND
  • 和cmake的输出如下
  • Could not find a package configuration file provided by "HDF5" with any of the following names: hdf5Config.cmake hdf5-config.cmake Add the installation prefix of "HDF5" to CMAKE_PREFIX_PATH or set "HDF5_DIR" to a directory containing one of the above files. If "HDF5" provides a separate development package or SDK, be sure it has been installed.
  • 附言。对不起,我无法正确格式化我的 cmets。我现在在我的问题中包含了 CGNS v3.3.1 的 cmake 构建。
【解决方案2】:

我不熟悉 CGNS,但作为第一次检查,我建议在执行 cg_open_f() 后验证是否为 iFile 变量分配了一个合理的值。 再想想,你是不是想在一个预先存在的文件上写?

【讨论】:

  • iFile 是cg_open_f 的输出。因此,它不需要预先分配。我正在创建一个新文件,而不是修改现有文件。
  • 我问的原因是因为你提到有一个错误说CGNS file 0 is not open。这让我想起了与您对文件的访问级别相关的问题。而且,我没有建议预先分配变量 iFile,只是为了手动检查运行 cg_open_f 后,它得到一个非零值。
  • 是的,我明白了。按照您的建议,我已经尝试了很多事情。但没有任何效果。所以,现在我认为这可能是我的 linux 发行版或 CGNS 库版本的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-09
相关资源
最近更新 更多