各位高手中午好!先向各位问好!
我定义了一个type,用来存储一个特定的化学反应类型的各个数据,如下
[Fortran] 纯文本查看 复制代码
type :: L2R2
!===============================================================================
! a1*L1^CL1 + a2*L2^CL2 + e*E^(-1) <---> b1*R1^CR1 + b2*R2^CR2 , p = XXX V
!
! for example:
!
! No = 1
! idn= 'L2R2_001'
! Rx = 'O3 + 2 H^(+) + 2 e^(-) <---> O2 + H2O' Pot = +2.075 V
!
! a1 = 1 ; L1 = 'O3' ; CL1 = 0
! a2 = 2 ; L2 = 'H' ; CL1 = 1
! e = 2 ;
! b1 = 1 ; R1 = 'O2' ; CR1 = 0
! b2 = 1 ; R1 = 'H2O' ; CL1 = 0
! p = 2.075
!==============================================================================
integer :: No
character(10) :: idn
character(100) :: Rx
integer :: a1
character(20) :: L1
integer :: CL1
integer :: a2
character(20) :: L2
integer :: CL2
integer :: e
integer :: b1
character(20) :: R1
integer :: CR1
integer :: b2
character(20) :: R2
integer :: CR2
real :: p
end type L2R2
然后,两种赋值方式
第一种:
[Fortran] 纯文本查看 复制代码
type (L2R2) :: L2R2_003
!==============================================================================
! No = 3 Rx = 'ZnO2^(2-) + 2 H2O + 2 e^(-) <---> Zn + 4 OH^(-)' Pot = -1.285 V
!==============================================================================
L2R2_003%No = 3
L2R2_003%idn = 'L2R2_003'
L2R2_003%Rx = 'ZnO2^(2-) + 2 H2O + 2 e^(-) <---> Zn + 4 OH^(-)'
L2R2_003%a1 = 1
L2R2_003%L1 = 'ZnO2'
L2R2_003%CL1 = -2
L2R2_003%a2 = 2
L2R2_003%L2 = 'H2O'
L2R2_003%CL2 = 0
L2R2_003%e = 2
L2R2_003%b1 = 1
L2R2_003%R1 = 'Zn'
L2R2_003%CR1 = 0
L2R2_003%b2 = 4
L2R2_003%R2 = 'OH'
L2R2_003%CR2 = -1
L2R2_003%p = -1.285
这个没有任何问题。
问题是第二种,出错了:
[Fortran] 纯文本查看 复制代码
L2R2_003 = (/ 3 , "L2R2_003" , "ZnO2^(2-) + 2 H2O + 2 e^(-) <---> Zn + 4 OH^(-)" , &
1 , "ZnO2" , -2 , 2 , "H2O" , 0 , 2 , 1 , "Zn" , 0 , 4 , "OH" , -1 , -1.285 /)
编译时候的错误是
- Compiling ..\..\..\..\Desktop\ZZZ\hello.f90
- ..\..\..\..\Desktop\ZZZ\hello.f90:104.25:
- L2R2_003 = (/ 3 , "L2R2_003" , "ZnO2^(2-) + 2 H2O + 2 e^(-) <---> Zn + 4 OH^(-)" , &
- 1
- Error: Element in INTEGER(4) array constructor at (1) is CHARACTER(1)
- Error(E42): Last command making (build\hello.o) returned a bad status
- Error(E02): Make execution terminated
- * Failed *
复制代码
所用环境就是Simply Fortran 2.17 Build 1758
实在不能理解这个错误发生的原因,还请明示!十分感谢
我自己的理解是:( / ...... / )这种方式,实际上是先造一个数组或列表出来,然后依次对应到派生类型中的各个变量里。
而fortran不允许数组内各元素类型不同,他会去认第一个元素的类型,一看,是整形,3,于是整个数组开始被建立,是个整形的数组。但是读到第二个元素时,却变成了"L2R2_003",是字符串,好,报错!
像L2R2_003%No=3,L2R2_003%idn='L2R2_003'这样的赋值方式,太繁琐。那么,有什么好办法么?
啰嗦啰嗦写了这么多,谢谢您耐心看完,再次感谢所有提出意见的人!
|