计算化学公社

标题: Multiwfn免环境变量加载配置文件settings.ini [打印本页]

作者
Author:
slxc920113    时间: 2019-10-16 16:56
标题: Multiwfn免环境变量加载配置文件settings.ini
本帖最后由 slxc920113 于 2019-10-16 17:05 编辑

Linux系统从命令行直接执行Multiwfn,或者在window中直接将文件拖到Multiwfn.exe程序文件上打开的时候,由于程序的启动路径是当前shell或文件所在路径,在相对路径下无法找到settings.ini配置文件,这个时候就会导致程序使用默认的配置参数,某些功能(比如用cubegen加速表面静电势的计算)会失效。
Multiwfn的源码fileIO.f90中加载settings.ini的代码如下:
  1. inquire(file="settings.ini",exist=alive)
  2. if (alive==.true.) then
  3.         settingpath="settings.ini"
  4. else if (alive==.false.) then
  5.         call getenv("Multiwfnpath",c80tmp)
  6.         if (isys==1) then
  7.                 settingpath=trim(c80tmp)//"\settings.ini"
  8.         else if (isys==2) then
  9.                 settingpath=trim(c80tmp)//"/settings.ini"
  10.         end if
  11.         inquire(file=settingpath,exist=alive)
  12.         if (alive==.false.) then
  13.                 write(*,"(a)") " Warning: ""settings.ini"" was found neither in current folder nor in the path defined by ""Multiwfnpath"" &
  14.                 environment variable. Now using default settings instead"
  15.                 write(*,*)
  16.                 return
  17.         end if
  18. end if
复制代码


假设程序安装在/opt/software/Multiwfn路径下
Linux系统下,可以添加Multiwfnpath这个环境变量来解决问题
  1. export Multiwfnpath=/opt/software/Multiwfn
复制代码

window下也可以添加Multiwfnpath环境变量,或者用一个更加简单的方法吗,创建一个快捷方式。
右击快捷方式>属性,然后将起始位置改成settings.ini所在的路径(默认)
但是,这又会带来一个问题,输出各种数据文件的时候,都会保存在程序所在路径,非常不方便。

真正一劳永逸的方法:直接修改源代码。
程序执行参数中第一个参数就是程序自身所在的路径,所以只需要从Multiwfn所在的绝对路径加载settings.ini就能解决这个问题
将上面那段代码稍作修改
  1. inquire(file="settings.ini",exist=alive)
  2. if (alive==.true.) then
  3.         settingpath="settings.ini"
  4. else if (alive==.false.) then
  5.         call getenv("Multiwfnpath",c80tmp)
  6.         if (isys==1) then
  7.                 settingpath=trim(c80tmp)//"\settings.ini"
  8.         else if (isys==2) then
  9.                 settingpath=trim(c80tmp)//"/settings.ini"
  10.         end if
  11.         inquire(file=settingpath,exist=alive)
  12.         if (alive==.false.) then
  13.                 !get prigram path of Multiwfn
  14.                 call getarg(0,c80tmp)
  15.                 if (isys==1) then
  16.                         !replace window path of Multiwfn.exe with settings.ini
  17.                         settingpath=c80tmp(:index(c80tmp,'\',.true.))//"settings.ini"
  18.                 else if (isys==2) then
  19.                         !replace *nix path of Multiwfn with settings.ini
  20.                         settingpath=c80tmp(:index(c80tmp,'/',.true.))//"settings.ini"
  21.                 end if
  22.                 inquire(file=settingpath,exist=alive)
  23.                 if (alive==.false.) then
  24.                         write(*,"(a)") " Warning: ""settings.ini"" was found neither in current folder nor in the path defined by ""Multiwfnpath"" &
  25.                         environment variable. Now using default settings instead"
  26.                         write(*,*)
  27.                         return
  28.                 end if
  29.         end if
  30. end if
复制代码


重新编译源码即可。
作者
Author:
march_happy    时间: 2020-6-21 17:22
补充一下,事实上 Windows 里也能设置 Multiwfnpath 环境变量。直接在搜索框里打“高级系统设置”(Advanced System Settings)打开设置窗口:

(, 下载次数 Times of downloads: 75)

在“高级”选项卡里有环境变量的选项。

(, 下载次数 Times of downloads: 60)

建议作为用户环境变量设置。如果是使用你自己写的 Powershell 脚本的话可以参考微软相关的文档:

https://docs.microsoft.com/en-us ... view=powershell-5.1






欢迎光临 计算化学公社 (http://bbs.keinsci.com/) Powered by Discuz! X3.3