Fortran90:理解函数测试输入文件参数的存在或值

Fortran90:理解函数测试输入文件参数的存在或值,fortran,boolean,gfortran,fortran90,Fortran,Boolean,Gfortran,Fortran90,我有一个关于Fortran90中输入文件参数值的问题: 输入文件的相关部分是: # If use_phyical set physical densities in baryons, CDM and neutrinos + Omega_k use_physical = F ombh2 = 0.022445 omch2 = 0.12055785438700001 omnuh2 = 0.000645145613 omk =

我有一个关于Fortran90中输入文件参数值的问题:

输入文件的相关部分是:

# If use_phyical set physical densities in baryons, CDM and neutrinos + Omega_k
use_physical   = F
ombh2          = 0.022445
omch2          = 0.12055785438700001
omnuh2         = 0.000645145613
omk            = 0.0
hubble         = 67.0

#if use_physical = F set parameters as here
omega_baryon   = 0.05
omega_cdm      = 0.2685627651944076
omega_lambda   = 0.6800000648055924
omega_neutrino = 0.00143717
暗示问题的参数是use_physical,我想在每次运行时默认设置为F实际上,我通过改变omega_cdm参数进行了多次运行

下面是读取输入参数的代码部分,请参见上文

write(*,*) '0) Ini%Read_Double use_physical = ', Ini%Read_Logical('use_physical', .false.)
if (Ini%Read_Logical('use_physical', .false.) .eqv. .false.) then
    write(*,*) '1.1) Ini%Read_Double use_physical = ', Ini%Read_Logical('use_physical', .false.)
    P%ombh2 = Ini%Read_Double('omega_baryon')*(P%H0/100)**2
    P%omch2 = Ini%Read_Double('omega_cdm')*(P%H0/100)**2
    P%omnuh2 = Ini%Read_Double('omega_neutrino')*(P%H0/100)**2
    P%omk = ((P%H0/100)**2 - P%ombh2 - P%omch2 - P%omnuh2 - Ini%Read_Double('omega_lambda')*(P%H0/100)**2)/(P%H0/100)**2
    write(*,*) 'Computing with use_physical = false'
    write(*,*) '1.2) Ini%Read_Double use_physical = ', Ini%Read_Logical('use_physical', .false.)
else
    write(*,*) '2) Ini%Read_Double use_physical = ', Ini%Read_Logical('use_physical', .false.)
    P%ombh2 = Ini%Read_Double('ombh2')
    P%omch2 = Ini%Read_Double('omch2')
    P%omnuh2 = Ini%Read_Double('omnuh2')
    P%omk = Ini%Read_Double('omk')
end if
在执行上述程序时,如果我设置use_physical=F,我将获得以下使用不同write*,*的输出:

 0) Ini%Read_Double use_physical =  F
 1.1) Ini%Read_Double use_physical =  F
 Computing with use_physical = false
 1.2) Ini%Read_Double use_physical =  F
我不理解Ini%Read_Logical'use_physical'的值。错误:如果use_physical=F,这应该等于真布尔值,不是吗

为什么以下条件与use_physical=F相等:

对我来说,这应该是:

如果Ini%读取逻辑“使用物理”,则为1。false。方程。对

或同等标准:

2如果Ini%读取逻辑“使用物理”,则为false

1和2都表示使用物理=F,但上面的write*输出似乎是相反的:如果Ini%Read\u Logical'use\u physical',则条件为false。方程。错误的当使用_physical=F时为真

问题1我不明白,如果有人能找到这个问题的解释

问题2从您的角度来看,返回的类型是什么:

Ini%Read_Logical('use_physical', .false.)
我尝试使用以下条件:

if (Ini%Read_Logical('use_physical', .false.) == 'T')

但我有编译错误

PS:抱歉,我没有立即找到Ini%Read\u Logicalroutine的实现,但这是说Ini%Read\u Logical“use\u physical”的逻辑。错误。当使用_physical=F时为真,而不是相反

更新1:我认为我发现函数Ini%Read\u的定义符合逻辑:

但这有点难理解

您是否更好地理解了为什么在执行上述程序时,如果我设置use_physical=F,我会得到以下使用不同write*,*的输出

 0) Ini%Read_Double use_physical =  F
 1.1) Ini%Read_Double use_physical =  F
 Computing with use_physical = false
 1.2) Ini%Read_Double use_physical =  F

谢谢

如果用户在ini文件中提供了一个值,函数ini%Read\u Logical将返回该值被解释为逻辑值的值。如果用户未提供任何值,函数Ini%Read_Logical将返回默认参数的值,即过程绑定引用中的第二个实际参数

您在ini文件中提供了一个值F,该值被解释为.FALSE。F是False的第一个字母,因此Ini%Read\u逻辑返回。False。。未使用默认参数的值

.错。方程。错误的为true,因此代码将在if语句之后立即执行语句块


Ini%Read\u返回的值的类型是逻辑的。要比较逻辑值,请使用.EQV。运算符是逻辑等价的缩写,或.NEQV。不相等。未定义用于数字和字符类型的==和/=内部运算符。

为什么您认为将Ini%读为逻辑“使用物理”是合乎逻辑的。false。当使用_physical=F时是否正确,而不是相反?谢谢你的回答。事实上,如果我已经很好地理解了,设置use_physical=F或设置nothing将得到相同的结果,即Init%Read_Logical将返回.FALSE,不是吗?相反,设置use_physical=T将执行原始问题的elseof my condition if then else块?当做
 if (Ini%Read_Logical('use_physical', .false.) .eq. .true)
function Ini_Read_Logical(this, Key, Default)
class(TIniFile) :: this
logical Ini_Read_Logical
logical, optional, intent(IN) :: Default
character(LEN=*), intent(IN) :: Key
character(LEN=:), pointer :: S
integer status

S => this%Read_String(Key,.not. present(Default))
if (S == '') then
    call this%EmptyCheckDefault(Key,Default)
    Ini_Read_Logical = Default
    call  this%ReadValues%Add(Key, Default)
else
    if (verify(trim(S),'10TF') /= 0) then
        status=1
    else
        read (S,*, iostat=status) Ini_Read_Logical
    end if
    if (status/=0) call this%Error('error reading logical',Key)
end if

end function Ini_Read_Logical
 0) Ini%Read_Double use_physical =  F
 1.1) Ini%Read_Double use_physical =  F
 Computing with use_physical = false
 1.2) Ini%Read_Double use_physical =  F