CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   OpenFOAM Programming & Development (https://www.cfd-online.com/Forums/openfoam-programming-development/)
-   -   Including PETSc in an OF solver (https://www.cfd-online.com/Forums/openfoam-programming-development/185431-including-petsc-solver.html)

BaselAS March 25, 2017 08:59

Including PETSc in an OF solver
 
Hello friends,

I'd like to include PETSc's headers in my solver but I can't figure out how to make it compile.

A PETSc program's makefile is as follows:
Code:

include ${PETSC_DIR}/lib/petsc/conf/variables
include ${PETSC_DIR}/lib/petsc/conf/rules

all: src/LinearSystemSolver.o chkopts
        -${CLINKER} -o LinearSystemSolver src/LinearSystemSolver.o  ${PETSC_KSP_LIB}
        ${RM} LinearSystemSolver.o

And OF's solver's makefile is, as you may already know, split into two files('options' and 'files') under a directory called 'Make':
'options':
Code:

EXE_INC = \
    -I$(LIB_SRC)/finiteVolume/lnInclude \
    -I$(LIB_SRC)/meshTools/lnInclude
   
EXE_LIBS =        -lfiniteVolume \
                        -lmeshTools

'files':
Code:

myLaplacianFoam.C

EXE = App/myLaplacianFoam

How am I supposed to edit 'options' and 'files' to make it work?

I appreciate your time and thanks in advance!
Basel

DoQuocVu April 1, 2019 21:28

Hi Basel,


I am trying to do the same thing. Have you been able to link petsc with your solver?

DoQuocVu April 1, 2019 22:41

For someone who might need the answer, I was able to link the icoFoam solver with petsc by doing the following steps:


Note: I am using petsc-3.4.5 and it is installed in my /home directory.

1. Create a shared library of petsc by invoking the following command from any directory in the PETSc source
Code:

make BOPT=O shared
This command will generate libpetsc.so in the folder arch-linux-cxx-opt/lib and it will be included into Make/options.


2. Include petsc.h into icoFoam.C
Code:

#include "petsc.h"
3. Change the Make/options to:
Code:

PETSC_DIR=$(HOME)/petsc/petsc-3.4.5
EXE_INC = \
    -I$(LIB_SRC)/finiteVolume/lnInclude \
    -I$(LIB_SRC)/meshTools/lnInclude \
    -I$(PETSC_DIR)/include \
    -I$(PETSC_DIR)/include/mpiuni \
    -I$(PETSC_DIR)/$(PETSC_ARCH)/include

EXE_LIBS = \
    -lfiniteVolume \
    -lmeshTools \
    -L$(PETSC_DIR)/$(PETSC_ARCH)/lib -lpetsc

4. Compile:
Code:

export PETSC_ARCH=arch-linux-cxx-opt
wmake

I have no idea why putting the line PETSC_ARCH=arch-linux-cxx-opt into Make/options does not work. That's why I have to define the PETSC_ARCH variable explicitly and then wmake.


Hope this can helps

DoQuocVu April 10, 2019 04:04

I am sorry, eventhough the solver was successfully compiled, it does not work.


The error is that libpetsc.so could not be found, even though it is still exist in my $PETSC_ARCH/lib directory:


Code:

icoPetscFoam: error while loading shared libraries: libpetsc.so: cannot open shared object file: No such file or directory
I have been struggling with this problem for weeks but I have not been able to find a clue to fix it. I would appreciate any suggestion to help me solve this.

DoQuocVu April 10, 2019 20:24

Problem solved!


Just need to define the LD_LIBRARY_PATH environmental variable in $HOME/.bashrc



Code:

echo "export LD_LIBRARY_PATH=\$PETSC_DIR/\$PETSC_ARCH/lib">>$HOME/.bashrc


All times are GMT -4. The time now is 08:39.