分享一个在任意文件夹中编译库或求解器的办法



  • 众所周知,OpenFOAM只允许用户把编译好的库(lib)和求解器(solver)放在 $FOAM_USER_APPBIN$FOAM_APPBIN 文件夹中。

    科研时间久了,某一个库经过太多次的修改,会有很多个不同版本的代码。如果需要使用其中的一个版本,就要重新编译。如果想同时保留两个版本的求解器,就只能在修改代码的过程中,精心安排每一个编译好的库的名字,比如把 Make/files 文件中的 libcombustionModels 命名为 libcombustionModelsNew libcombustionModelsNewNew…… 只有这样,才能保留不同求解器各自需要的库(.so)文件。改的库多了,工作量就会几何增长!(比如turbulenceModels 建立在thermophysicalModels的基础上,lagrangian 又基于湍流模型,每一个库里都有大量的lib,每一个修改过的lib都需要重新命名,牵一发动全身!如果改了fluidThermo,那简直是欲哭无泪)

    网上好像还没有人讨论过这个问题。经过研究,终于找到了一个打破僵局的办法。实现一个新的算法,只需要把新代码放在一个文件夹下,所有库的名字都不用修改。新生成的库,都会以旧代码的库名字保存在新的文件夹下,只要solver中指定需要哪个文件夹下的库就好了。

    首先打开terminal,进入到OpenFOAM的文件夹下

    $cd $WM_PROJECT_DIR/etc/
    

    bashrc文件中添加一行代码(在terminal中输入以下代码):

    $sed -i '/WM_PROJECT_USER_DIR_DEV/iexport WM_PROJECT_USER_DIR_DEV=$HOME/$WM_PROJECT/$USER-$WM_PROJECT_VERSION-dev' bashrc
    

    其中,WM_PROJECT_USER_DIR_DEV 是自己取的名字,$USER-$WM_PROJECT_VERSION-dev 是想要储存新代码和库的文件夹路径,这里是创建user-版本号-dev,和$FOAM_USER_APPBIN文件夹同级。

    之后,

    $cd $WM_PROJECT_DIR/etc/config
    $sed -i '/FOAM_USER_LIBBIN/iexport WM_PROJECT_USER_DIR_DEV=$WM_PROJECT_USER_DIR_DEV/platforms/$WM_OPTIONS/lib' settings.sh
    $sed -i -e 's=_foamAddLib  $FOAM_USER_LIBBIN\:=_foamAddLib  $FOAM_USER_LIBBIN_DEV:$FOAM_USER_LIBBIN=' settings.sh 
    

    完成在$FOAM_USER_LIBBIN这一行添加新的路径$FOAM_USER_LIBBIN_DEV,其位置在新代码所在的文件夹中。

    之后,只要参照下面的命令批量替换新代码中Make/optionsMake/filesOpenFOAM寻找库的路径,并保证求解器调用新路径下的库就好了!

    sed -i "s/$(FOAM_USER_LIBBIN/$(FOAM_USER_LIBBIN_DEV/g" `grep "lib<自己命名的库>" -rl ~/OpenFOAM/user-版本号-dev/src`
    

  • Linux讲师 OpenFOAM讲师

    我来分享一哈我的配置方法。总之就是把用户自定义的和官方的分开。省的乱了。至于命名,如果修改版本特别多,官方名加日期命名比较好。代码最好用版本管理器,还能加加些笔记标注每个版本做了什么修改,是从哪个版本来的。

    alias ofd='source $HOME/OpenFOAM/OpenFOAM-dev/etc/bashrc;
    export FOAM_USER_SRC=$HOME/$WM_PROJECT/$USER-$WM_PROJECT_VERSION/src;
    export FOAM_USER_APP=$HOME/$WM_PROJECT/$USER-$WM_PROJECT_VERSION/applications;
    export FOAM_USER_SOLVERS=$HOME/$WM_PROJECT/$USER-$WM_PROJECT_VERSION/applications/solvers;
    export FOAM_USER_UTILITIES=$HOME/$WM_PROJECT/$USER-$WM_PROJECT_VERSION/applications/utilities;
    export FOAM_USER_TUTORIALS=$HOME/$WM_PROJECT/$USER-$WM_PROJECT_VERSION/run/tutorials'
    
    alias of22x='source $HOME/OpenFOAM/OpenFOAM-2.2.x/etc/bashrc;
    export FOAM_USER_SRC=$HOME/$WM_PROJECT/$USER-$WM_PROJECT_VERSION/src;
    export FOAM_USER_APP=$HOME/$WM_PROJECT/$USER-$WM_PROJECT_VERSION/applications;
    export FOAM_USER_SOLVERS=$HOME/$WM_PROJECT/$USER-$WM_PROJECT_VERSION/applications/solvers;
    export FOAM_USER_UTILITIES=$HOME/$WM_PROJECT/$USER-$WM_PROJECT_VERSION/applications/utilities;
    export FOAM_USER_TUTORIALS=$HOME/$WM_PROJECT/$USER-$WM_PROJECT_VERSION/run/tutorials'
    
    alias of30x='source $HOME/OpenFOAM/OpenFOAM-3.0.x/etc/bashrc;
    export FOAM_USER_SRC=$HOME/$WM_PROJECT/$USER-$WM_PROJECT_VERSION/src;
    export FOAM_USER_APP=$HOME/$WM_PROJECT/$USER-$WM_PROJECT_VERSION/applications;
    export FOAM_USER_SOLVERS=$HOME/$WM_PROJECT/$USER-$WM_PROJECT_VERSION/applications/solvers;
    export FOAM_USER_UTILITIES=$HOME/$WM_PROJECT/$USER-$WM_PROJECT_VERSION/applications/utilities;
    export FOAM_USER_TUTORIALS=$HOME/$WM_PROJECT/$USER-$WM_PROJECT_VERSION/run/tutorials'
    
    alias of4x='source $HOME/OpenFOAM/OpenFOAM-4.x/etc/bashrc;
    export FOAM_USER_SRC=$HOME/$WM_PROJECT/$USER-$WM_PROJECT_VERSION/src;
    export FOAM_USER_APP=$HOME/$WM_PROJECT/$USER-$WM_PROJECT_VERSION/applications;
    export FOAM_USER_SOLVERS=$HOME/$WM_PROJECT/$USER-$WM_PROJECT_VERSION/applications/solvers;
    export FOAM_USER_UTILITIES=$HOME/$WM_PROJECT/$USER-$WM_PROJECT_VERSION/applications/utilities;
    export FOAM_USER_TUTORIALS=$HOME/$WM_PROJECT/$USER-$WM_PROJECT_VERSION/run/tutorials'
    
    alias of5x='source $HOME/OpenFOAM/OpenFOAM-5.x/etc/bashrc;
    export FOAM_USER_SRC=$HOME/$WM_PROJECT/$USER-$WM_PROJECT_VERSION/src;
    export FOAM_USER_APP=$HOME/$WM_PROJECT/$USER-$WM_PROJECT_VERSION/applications;
    export FOAM_USER_SOLVERS=$HOME/$WM_PROJECT/$USER-$WM_PROJECT_VERSION/applications/solvers;
    export FOAM_USER_UTILITIES=$HOME/$WM_PROJECT/$USER-$WM_PROJECT_VERSION/applications/utilities;
    export FOAM_USER_TUTORIALS=$HOME/$WM_PROJECT/$USER-$WM_PROJECT_VERSION/run/tutorials'
    
    alias of6='source $HOME/OpenFOAM/OpenFOAM-6/etc/bashrc;
    export FOAM_USER_SRC=$HOME/$WM_PROJECT/$USER-$WM_PROJECT_VERSION/src;
    export FOAM_USER_APP=$HOME/$WM_PROJECT/$USER-$WM_PROJECT_VERSION/applications;
    export FOAM_USER_SOLVERS=$HOME/$WM_PROJECT/$USER-$WM_PROJECT_VERSION/applications/solvers;
    export FOAM_USER_UTILITIES=$HOME/$WM_PROJECT/$USER-$WM_PROJECT_VERSION/applications/utilities;
    export FOAM_USER_TUTORIALS=$HOME/$WM_PROJECT/$USER-$WM_PROJECT_VERSION/run/tutorials'
    
    alias of7='source $HOME/OpenFOAM/OpenFOAM-7/etc/bashrc;
    export FOAM_USER_SRC=$HOME/$WM_PROJECT/$USER-$WM_PROJECT_VERSION/src;
    export FOAM_USER_APP=$HOME/$WM_PROJECT/$USER-$WM_PROJECT_VERSION/applications;
    export FOAM_USER_SOLVERS=$HOME/$WM_PROJECT/$USER-$WM_PROJECT_VERSION/applications/solvers;
    export FOAM_USER_UTILITIES=$HOME/$WM_PROJECT/$USER-$WM_PROJECT_VERSION/applications/utilities;
    export FOAM_USER_TUTORIALS=$HOME/$WM_PROJECT/$USER-$WM_PROJECT_VERSION/run/tutorials'
    
    # default version
    of6
    
    #对应OpenFOAM自带的src lib app sol util tut.
    alias msrc='cd $FOAM_USER_SRC'
    alias mlib='cd $FOAM_USER_LIBBIN'
    alias mapp='cd $FOAM_USER_APP'
    alias msol='cd $FOAM_USER_SOLVERS'
    alias mutil='cd $FOAM_USER_UTILITIES'
    alias mtut='cd $FOAM_USER_TUTORIALS'
    
    


  • 是的,就是为了给用户自定义的文件更好分类。
    我的方法是生成FOAM_USER_SRC_A,FOAM_USER_SRC_B,FOAM_USER_SRC_C,可以在每一个文件夹下各自存放自定义库和求解器,这样就避免了重命名或者版本管理。每一个SRC下的文件单独拖出来,都可以直接在cluster上编译运行



  • 感谢分享



  • 感谢分享



  • 感谢分享


Log in to reply
 

CFD中文网 2016 - 2020 | 京ICP备15017992号-2