【问题标题】:passing derived data type assumed shape arrays from a subroutine to a function in fortran 90将派生数据类型假定形状数组从子例程传递给fortran 90中的函数
【发布时间】:2012-06-20 20:39:52
【问题描述】:

我对 fortran 很陌生。而且我使用的是 fortran 内置的库,该库有许多 TYPE 数组。 我尝试使用以下方法通过 c 程序将值分配给 lib 中的 TYPE 数组。 我已经构建了一个 c-fortran 接口,我从 sqlite 数据库获取值到 c prg 中的 ac 结构数组中。然后将此结构数组传递给一个 fortran 子例程,在其中我将其声明为派生类型,匹配声明的 TYPE 变量的定义在 lib 中。然后我将传递的数组中的值复制到 lib 中声明的实际 TYPE 数组中,并将其传递给 fortran 函数。

发生的事情是数组中的值从 c 传递到 fortran 子例程,我打印它们以在 fortran 子例程中检查它们,但是当数组从子例程传递到函数时,值变得乱码。我将数组作为假定的形状数组传递。该函数在模块内声明,因此我认为调用子例程不需要接口。

我不完全理解发生了什么,我也尝试使用 TYPE 声明中的序列。 我正在使用 g95 , gcc 4.0.3 编译器。 数组中的所有值都是 REAL(KIND =8) 类型,c 程序中的等价物是 double 。

考虑一个声明了 TYPE(something), TYPE(Something2) 的库。我将 lib 作为模块导入到 fortran 子例程中。

假设

           TYPE(something_lib) is

              REAL(kind =8) ::A 
              REAL(kind=8)  ::B

            END TYPE

在库中

 TYPE(SOMETHING2_lib) !this is also declare in the lib


           !I have a C program  in which

            ! in which 

/////////////////////////////////////// ///////////////////////////////////////////////p>

     // C program

  struct SomethingC    

   {
    double a


     double b

 } ;

  struct SomethingC  x[2]

  struct  something2C s[2]  // something similar to the first struct


//i fill the values in x ,s from database in proper format.(doubles).

 //i call the fortran subroutine in the c program

   A_(x,s);   //call to fortran subroutine 

/////////////////////////////////////// //////////////////////////////////// // fortan 子程序

     SUBROUTINE A (x,s)

        USE Lib_module       ! this LIB_Module also contains the function func


         TYPE G

            REAL(kind =8)        ! this is defined similar to TYPE something(in lib) by me
             REAL(kind =8)

         END TYPE G


          TYPE G2

            similar to TYPE Something2 in lib

          END TYPE G2


         TYPE(something_lib) :: D(2)      !derived type declared in lib
         TYPE(Something2_lib)::E(2)       ! derived type declared in lib
         TYPE(G)::x(2)            
         TYPE(G2)::s(2)           


  ! x, s are struct arrays from c which are now declared in the fortran function


          copy code for 
          copying values from
            x to D
            s to E

          print all values of 
             D

          Print all values of 
            E


         !this prints the values correct as expected for both x,d


          func(D,E)    ! this function is defined in the lib . The function is in the                      

                     !  LIB_module                    
                     ! so no interface will be required (i think)


         ! IN the  Function


           FUNCTION func(D,E) (while debugging)


            TYPE(something_lib) :: INTENT (IN) D(:)
            TYPE (something2_lib)::INTENT (IN) E(:)


              when i try to print values of D , E in the 
              function i get garbled values like 

                1180333333

               2.33419537006E-313

        !when  out of the function  and back in the subroutine i.e after the call(while                        debugging)
                ! if I print the values of D,E here  they print ok

  END SUBROUTINE

因此它们在传入函数后会出现乱码,但在 子程序。 我的问题是为什么会这样? 我该如何解决?

【问题讨论】:

  • 我们并不完全了解您在做什么。在你的代码中很难看出问题。
  • 在这一点上进行的一种有效方法是给我们代码,包含相关部分的代码的截断版本,或者代码的骨架/示意图版本。当然,还有执行时产生的任何错误。
  • @High Performance 我将编辑我的问题以提供一个框架。
  • @codefor: yukkkk,请正确格式化您的代码。
  • @codefor: yukkkk,请正确格式化您的代码。

标签: arrays fortran derived-types


【解决方案1】:

我建议使用 ISO C 绑定,它使 C 和 Fortran 之间的变量传递成为 Fortran 语言标准的一部分。这将需要 gcc/gfortran 4.3 或更高版本;我不确定g95版本。但是,不支持将假定形状的数组作为 C 的参数。假定形状的数组是高级别的,不仅包含数组,还包含有关大小的信息,将它们传递给 C 可能需要了解特定 Fortran 编译器的内部结构。

【讨论】:

  • hI 感谢您的回复。但我并不想将假定的形状数组传递给 C 。我正在尝试将它从 fortran 子例程传递给 fortran 函数,该函数位于模块的包含中。
猜你喜欢
  • 2011-07-03
  • 1970-01-01
  • 2017-04-04
  • 2014-04-11
  • 2016-12-15
  • 2014-05-08
  • 2013-08-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多