Software
OpenFOAM example - own applications
Example of how to compile your own OpenFOAM application
This example is a slightly modified version of the one found on the webpages for PhD course in CFD with OpenSource software, at Chalmers: 'Copy and compile an application, and a deeper look in icoFoam' [PDF]. In addition to loading the OpenFOAM module (and its prerequisites), you need to do
source "$FOAM_BASH"
for the environment variables to be available.
The following works for OpenFOAM v. 4.0, but the changes needed for the other versions should be minimal.
- The applications can be found in $WM_PROJECT_DIR/applications
- Create $WM_PROJECT_USER_DIR
- Copy an application that is similar to what you want to achieve. In this example we look at the icoFoam solver
cd $WM_PROJECT_DIR cp -riuv --parents --backup applications/solvers/incompressible/icoFoam/ $WM_PROJECT_USER_DIR
- Go to the directory you just copied, and rename the application - both the directory and the C++ file.
cd $WM_PROJECT_USER_DIR/applications/solvers/incompressible/ mv icoFoam/ passiveScalarFoam cd passiveScalarFoam wclean mv icoFoam.C passiveScalarFoam.C
- Now it is time to modify the Makefile
- change Make/files to contain the following
passiveScalarFoam.C EXE = $(FOAM_USER_APPBIN)/passiveScalarFoam
- change Make/files to contain the following
- Run wmake in the passiveScalarFoam directory
- Add a volScalarField s (same style as for p) in createFields.H
Info<< "Reading field s\n" << endl; volScalarField s ( IOobject ( "s", runTime.timeName(), mesh, IOobject::MUST_READ, IOobject::AUTO_WRITE ), mesh );
- Add
solve(fvm::ddt(s) + fvm::ddt(s) + fvm::div(phi, s));
before
runTime.write();
in passiveScalarFoam.C
- Compile with wmake (be prepared for a lot of warnings. They should be inconsequential.)
- Begin by creating $FOAM_RUN in $WM_PROJECT_USER_DIR
cd $WM_PROJECT_USER_DIR mkdir $FOAM_RUN
- In order to use the new application, we must modify one of the cases. Here we will look at icoFoam/cavity
- Note that the below path is correct for OpenFOAM version 4.x. Earlier versions have one less subdirectory (i.e. for the example below, it would be $FOAM_TUTORIALS/incompressible/icoFoam/cavity)
cd $FOAM_RUN cp -r $FOAM_TUTORIALS/incompressible/icoFoam/cavity/cavity passiveCavity cd passiveCavity/ cp 0/p 0/s
- Modify p to s in the file you just created
- Normally, you would also need to choose appropriate dimensions for the scalar field, but this is not important now, as this is only an example
- In system/fvSchemes (under the passiveCavity directory), add (in function divSchemes, under div(phi,U) Gauss linear;)
div(phi,s) Gauss linearUpwind Gauss;
- In system/fvSolution, add (in function solvers)
s { solver PBiCG; preconditioner DILU; tolerance 1e-05; relTol 0; };
- Time to initialize and run the case
- Copy setFieldsDict from the damBreak tutorial (Note that the below path is correct for OpenFOAM 4.x. Version 3.x and earlier has one less subdirectory (i.e. for the example below, it would be $FOAM_TUTORIALS/multiphase/interFoam/laminar/damBreak/system/setFieldsDict))
cp $FOAM_TUTORIALS/multiphase/interFoam/laminar/damBreak/damBreak/system/setFieldsDict .
- In setFieldsDict, set volScalarFieldValue s 0 like this:
defaultFieldValues ( volScalarFieldValue s 0 );
- Modify the bounding box to:
box (0.03 0.03 -1) (0.06 0.06 1);
- Set fieldValues to
volScalarFieldValue s 1
- Run the case:
cd .. blockMesh setFields passiveScalarFoam >& log
- Copy setFieldsDict from the damBreak tutorial (Note that the below path is correct for OpenFOAM 4.x. Version 3.x and earlier has one less subdirectory (i.e. for the example below, it would be $FOAM_TUTORIALS/multiphase/interFoam/laminar/damBreak/system/setFieldsDict))
Updated: 2024-11-01, 13:56