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.

  1. The applications can be found in $WM_PROJECT_DIR/applications
  2. Create $WM_PROJECT_USER_DIR
  3. 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
    
  4. 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
    
  5. Now it is time to modify the Makefile
    1. change Make/files to contain the following
      passiveScalarFoam.C
      EXE = $(FOAM_USER_APPBIN)/passiveScalarFoam
      
  6. Run wmake in the passiveScalarFoam directory
  7. 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
    );
    
  8. Add
    solve(fvm::ddt(s) + fvm::ddt(s) + fvm::div(phi, s));
    

    before

    runTime.write();
    

    in passiveScalarFoam.C

  9. Compile with wmake (be prepared for a lot of warnings. They should be inconsequential.)
  10. Begin by creating $FOAM_RUN in $WM_PROJECT_USER_DIR
    cd $WM_PROJECT_USER_DIR
    mkdir $FOAM_RUN
    
  11. In order to use the new application, we must modify one of the cases. Here we will look at icoFoam/cavity
  12. 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
    
  13. Modify p to s in the file you just created
  14. 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
  15. In system/fvSchemes (under the passiveCavity directory), add (in function divSchemes, under div(phi,U)   Gauss linear;)
    div(phi,s)     Gauss linearUpwind Gauss;
    
  16. In system/fvSolution, add (in function solvers)
    s
    {
       solver            PBiCG; 
       preconditioner    DILU; 
       tolerance         1e-05; 
       relTol            0;
    };
    
  17. Time to initialize and run the case
    1. 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 .
      
    2. In setFieldsDict, set volScalarFieldValue s 0 like this:
      defaultFieldValues
      (
          volScalarFieldValue s 0
      );
      
    3. Modify the bounding box to:
      box (0.03 0.03 -1) (0.06 0.06 1);
      
    4. Set fieldValues to  
      volScalarFieldValue s 1
      
    5. Run the case:
      cd ..
      blockMesh
      setFields
      passiveScalarFoam >& log
      
Updated: 2024-03-21, 12:31