标签: fortran90

在fortran90中智能打印整数

几年前,我在简要介绍Fortran77之后,正在学习Fortran90.在Fortran中打印整数时,必须指定要为打印整数保留的空格数.考虑这个程序......

implicit none

integer :: i
i = 123

write(*, '(A, I3, A)')  "'", i, "'"  !3 spaces for output = no padding
write(*, '(A, I5, A)')  "'", i, "'"  !5 is too many, so output is padded
write(*, '(A, I2, A)')  "'", i, "'"  !2 is too few, so output is jibberish
write(*, '(A, I:, A)')  "'", i, "'"  !Default behavior

end program
Run Code Online (Sandbox Code Playgroud)

...生成以下输出.

'123'
'  123'
'**'
'         123'
Run Code Online (Sandbox Code Playgroud)

当我不知道整数中有多少位数时,如何为整数打印分配正确的空间量?

更新:如果您的编译器符合F95,您可以使用I0编辑描述符(即,在上面的示例'(A, I0, A)'中为 …

format fortran fortran90 fortran95

6
推荐指数
1
解决办法
3110
查看次数

Fortran 90中有更好的双精度分配吗?

在Fortran 90中(在Mac OS X上使用gfortran)如果我将一个值赋值给一个双精度变量而没有明确地添加一种类型,则精度不会"采用".我的意思是,如果我运行以下程序:

program sample_dp

implicit none

integer, parameter :: sp = kind(1.0)
integer, parameter :: dp = kind(1.0d0)

real(sp) :: a = 0.
real(dp) :: b = 0., c = 0., d = 0.0_dp, e = 0_dp

! assign values
a = 0.12345678901234567890
b = 0.12345678901234567890
c = DBLE(0.12345678901234567890)
d = 0.12345678901234567890_dp

write(*,101) a, b, c, d
101 format(1x, 'Single precision: ',  T27, F17.15, / &
           1x, 'Double precisison: ', T27, F17.15, / &
           1x, 'Double precision (DBLE): …
Run Code Online (Sandbox Code Playgroud)

fortran fortran90

6
推荐指数
2
解决办法
1万
查看次数

测试目录是否存在

我正在尝试使用Fortan90验证目录是否存在.在我发现的各种网站上:

logical :: dir_e
inquire(file='./docs/.', exist=dir_e)

if ( dir_e ) then
  write(*,*) "dir exists!"
else
  ! workaround: it calls an extern program...
  call system('mkdir docs')
end if
Run Code Online (Sandbox Code Playgroud)

但是,inquire返回False目录是否存在,如果我执行此代码两次,我收到一条错误消息

无法制作dir,文件已经存在

如果我使用:

inquire(file='./docs/test', exist=dir_e)
Run Code Online (Sandbox Code Playgroud)

使用现有文件测试,inquire返回true.

如何检查目录的存在?我正在使用ubuntu 11.04和ifort编译器.

fortran fortran90 intel-fortran

6
推荐指数
2
解决办法
1万
查看次数

在Fortran 90中使用2d数组与派生类型数组

假设您需要一个数组列表,每个数组都具有相同的大小.使用2D数组在性能方面是否更好:

integer, allocatable :: data(:,:)
Run Code Online (Sandbox Code Playgroud)

或派生类型数组:

type test
    integer, allocatable :: content(:)
end type
type(test), allocatable :: data(:)
Run Code Online (Sandbox Code Playgroud)

当然,对于不同大小的阵列,我们没有选择.但是如何在两种情况下管理内存?还有,其中一个很好的代码实践?

fortran multidimensional-array fortran90 derived-types

6
推荐指数
2
解决办法
696
查看次数

专业的Fortran代码开发:日志文件创建

我开发了一个Fortran代码,它具有以下特征:

  1. 全局变量
  2. 13个带有多个子程序的模块
  3. 独立的子程序
  4. 将英特尔MKL库用于LAPACK库(线性代数)
  5. 读写文本文件

代码变得非常大.尽管在这个阶段我试图得到正确的答案,但仍需要执行代码的速度.

我正在编写带有标签的文本日志文件,例如ERROR: message或者INFO: message到目前为止.但是写太多信息会减慢代码的速度.我知道在Java开发中我们使用log4j库来高效地编写日志文件,我们可以打开或关闭各种级别的日志记录.因此,一旦代码干净,我们就可以关闭低级日志并保留高级日志.

我想知道其他程序员在Fortran 90+中处理这个问题的最佳方法是什么.

fortran fortran90

6
推荐指数
1
解决办法
1293
查看次数

POINTER和ALLOCATABLE之间的区别

这两个代码之间有什么区别

type Foo
   real, allocatable :: bar(:)
end type
Run Code Online (Sandbox Code Playgroud)

type Foo
   real, pointer :: bar(:)
end type
Run Code Online (Sandbox Code Playgroud)

特别是当涉及到以下代码时:

type(Foo) :: myfoo
allocate(myfoo%bar(10))
Run Code Online (Sandbox Code Playgroud)

fortran fortran90

5
推荐指数
1
解决办法
5152
查看次数

Fortran 2003绑定到C中的库:如何翻译枚举和#defines?

我正在为C库编写Fortran绑定.

使用枚举翻译定义的常量(在库头中)的最佳实践是什么,例如

typedef enum cufftType_t {
  CUFFT_R2C = 0x2a,     // Real to Complex (interleaved)
  CUFFT_C2R = 0x2c,     // Complex (interleaved) to Real
  CUFFT_C2C = 0x29,     // Complex to Complex, interleaved
  CUFFT_D2Z = 0x6a,     // Double to Double-Complex
  CUFFT_Z2D = 0x6c,     // Double-Complex to Double
  CUFFT_Z2Z = 0x69      // Double-Complex to Double-Complex
} cufftType;
Run Code Online (Sandbox Code Playgroud)

以及如何使用翻译常数#define,例如

#define CUFFT_FORWARD -1 // Forward FFT
#define CUFFT_INVERSE  1 // Inverse FFT
Run Code Online (Sandbox Code Playgroud)

或这些的组合

typedef enum cufftCompatibility_t {
    CUFFT_COMPATIBILITY_NATIVE          = 0x00, …
Run Code Online (Sandbox Code Playgroud)

c fortran fortran90

5
推荐指数
1
解决办法
906
查看次数

在`function`语句中定义结果数据类型

好吧,我先说一下我为什么要这样做.我经常用C/C++编写代码,所以对我来说定义函数非常自然:

vector<int> TestFunct (int a, int b){
<some code here>

return <result>;}
Run Code Online (Sandbox Code Playgroud)

现在我正在学习Fortran,所以我声明这样的函数:

function TestFunc(a,b)
        integer, dimension(:)    :: TestFunc
        integer                  :: a
        integer                  :: b
        <some code here>
endfunction TestFunc
Run Code Online (Sandbox Code Playgroud)

但我最近了解到结果的数据类型可以在函数语句中定义,例如:<data_type> function TestFunc(a,b),这对我来说更自然,因为我习惯了类似的C++声明.

问题是,当我'尝试定义一个向量(实际上是一个integer, dimension(:)严格说话)作为结果数据类型时,我有ifort错误#5082(我将在下一行详述).

在一个例子中,对于代码:

real, dimension(:) function TestFunc(a,b)
         integer, intent(in)    :: a
         integer, intent(in)    :: b

         <more code here>

endfunction Testfunc
Run Code Online (Sandbox Code Playgroud)

我得到输出:

main.f90(23): error #5082: Syntax error, found ',' when expecting one of: ( * ) ( :: %FILL , …
Run Code Online (Sandbox Code Playgroud)

fortran function variable-declaration fortran90 intel-fortran

5
推荐指数
1
解决办法
953
查看次数

Fortran 90声明可分配数组的差异

两者之间有区别吗?

integer, intent(in) :: n
integer, dimension(:), allocatable :: a
allocate(a(n))
Run Code Online (Sandbox Code Playgroud)

integer, intent(in) :: n
integer, dimension(n) :: a
Run Code Online (Sandbox Code Playgroud)

在哪种情况下我们会使用第一个版本?也许我误解了可分配的数组,第二个版本甚至是可分配的数组?

arrays fortran memory-management gfortran fortran90

5
推荐指数
1
解决办法
402
查看次数

数组构造函数中不同的CHARACTER长度(3/4),如何修剪字符串-fortran

根据对类似问题的回答,我已声明字符,如此处所示gfortran不允许使用长度可变的字符数组。但是,我想使用trim函数,因为我需要添加空格来手动填充名称,然后在代码的另一部分中使用这些变量。我可以在创建阵列的同时修剪吗?

错误:数组构造函数的(1)处的CHARACTER长度(3/4)不同

如果我添加随机字符以使其具有相同的长度,则可以使用,但是出于明显的原因,我不能这样做。我已经用gfortran和mpif90编译了相同的结果

use mod_maxdims , only : maxstr
integer, parameter :: nvars_ncep = 12

character(len=maxstr), parameter, dimension(nvars_ncep) :: vars_ncep =                  &
                           (/ 'air'              & ! Air temperature                  [      K]
                            , 'pres'             & ! Pressure                         [     Pa]
                            , 'rhum'             & ! Relative humidity                [      %]
                            , 'uwnd'             & ! Zonal wind                       [    m/s]
                            , 'vwnd'             & ! Zonal wind                       [    m/s]
                            , 'pres'             & ! Pressure                         [     Pa]
                            , 'dlwrf'            & ! Downward long wave radiation     [   W/m2] …
Run Code Online (Sandbox Code Playgroud)

fortran gfortran fortran90

5
推荐指数
1
解决办法
2430
查看次数