CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > Software User Forums > OpenFOAM > OpenFOAM Programming & Development

How to develop OpenFOAM with CMake and popular IDEs

Register Blogs Community New Posts Updated Threads Search

Like Tree7Likes
  • 7 Post By cosscholar

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   March 16, 2022, 16:17
Post How to develop OpenFOAM with CMake and popular IDEs
  #1
New Member
 
zhikui guo
Join Date: Feb 2018
Posts: 2
Rep Power: 0
cosscholar is on a distinguished road
As we know, wmake is an excellent tool to build OpenFOAM libs and apps, and it works very well in macOS and Linux. While CMake would be beneficial if one would like to program or debug OpenFOAM code in popular IDEs, e.g., XCode (macOS IDE) CLion (cross-platform IDE). The workflow mainly contains four steps: (1) activate OpenFOAM environment, e.g. source /YourOpenFOAMPath/etc/bashrc; (2) prepare CMakeLists.txt file; (3) generate project file according to your preferred IDE; (4) Develop & Debug in your favourite IDE. The (2) step should be the most important. See below for the details. A wiki page with high-resolution images can be found at GitLab wiki and the tutorial video is here: https://youtube.com/playlist?list=PL...TGvnocA3e08fzv.

1. Prepare CMakeLists.txt
It is straightforward to construct a project file for any IDE once the CMakeLists.txt is done. The basic configuration of OF is below. It automatically detects the OF environment (support both the Foundation version and the ESI version, which works on both macOS and Linux systems).

Code:
# the Basic configuration of OpenFOAM in CMake

# Check valid OpenFOAM
if(DEFINED ENV{WM_PROJECT_DIR})
	MESSAGE(STATUS "OpenFOAM: " $ENV{WM_PROJECT_DIR})
else()
	message(FATAL_ERROR "The OpenFOAM bashrc is not sourced")
endif(DEFINED ENV{WM_PROJECT_DIR})
set(OpenFOAM_VERSION $ENV{WM_PROJECT_VERSION}) 
set(OpenFOAM_DIR $ENV{WM_PROJECT_DIR})
set(OpenFOAM_LIB_DIR $ENV{FOAM_LIBBIN})
set(OpenFOAM_SRC $ENV{FOAM_SRC})
# ====================== Some difference between ESI version and Foundation version ==================
# Of course you can change some parameters here, e.g., some pre-settings in ~/.OpenFOAM/prefs.h
set(PATH_LIB_OPENMPI "openmpi-system")  # Foundation version
set(DEFINITIONS_COMPILE "-std=c++11 -DWM_ARCH_OPTION=64 -DWM_DP -DWM_LABEL_SIZE=32 -Wall -Wextra -Wno-unused-parameter -Wno-overloaded-virtual -Wno-unused-variable -Wno-unused-local-typedef -Wno-invalid-offsetof -Wno-deprecated-register -Wno-undefined-var-template -O0 -g -DFULLDEBUG -DNoRepository -ftemplate-depth-100 -fPIC")

if(${OpenFOAM_VERSION} MATCHES "v([0-9]*)")       # ESI
    set(PATH_LIB_OPENMPI "sys-openmpi")
    set(DEFINITIONS_COMPILE "-std=c++14 -m64 -pthread -ftrapping-math -DOPENFOAM=2106 -DWM_DP -DWM_LABEL_SIZE=32 -Wall -Wextra -Wold-style-cast -Wnon-virtual-dtor -Wno-unused-parameter -Wno-invalid-offsetof -Wno-undefined-var-template -Wno-unknown-warning-option  -O3  -DNoRepository -ftemplate-depth-100 -fPIC -DIMPLEMENT_ACTIVATION -Wl,-execute,-undefined,dynamic_lookup")
endif()
# =====================================================================================================
# Compiling configure
add_definitions("${DEFINITIONS_COMPILE}")
# ======== OS specific setting =============
if(APPLE)
    add_definitions(" -Ddarwin64 ")
else()
    add_definitions("-Dlinux64")
endif(APPLE)

# ==========================================

include_directories(. 
                    ${OpenFOAM_SRC}/OpenFOAM/lnInclude  
                    ${OpenFOAM_SRC}/OSspecific/POSIX/lnInclude 
                    ${OpenFOAM_SRC}/finiteVolume/lnInclude 
                    )

link_directories(${OpenFOAM_LIB_DIR} ${OpenFOAM_LIB_DIR}/dummy ${OpenFOAM_LIB_DIR}/${PATH_LIB_OPENMPI})
The above CMake code is the basic configuration of OF, which is required for every OF project, so the elegant way is to save it as a CMake module and include it in your main CMakeLists.txt. See the example of laplacianFoam, please download the example and check it out.

The main CMakeLists.txt in the example case is shown below,

Code:
cmake_minimum_required(VERSION 3.20 FATAL_ERROR)
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
set(CMAKE_XCODE_GENERATE_SCHEME TRUE)  #Set this if you want to use XCode as IDE

project(laplacianFoam LANGUAGES CXX C)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# =====================================================================================
#             OpenFOAM configurations: elegant way!
# -------------------------------------------------------------------------------------
include(OpenFOAM)
# =====================================================================================
#             Include path configuration, similar to that in Make/options
include_directories(${OpenFOAM_SRC}/meshTools/lnInclude)
# =====================================================================================
set(PATH_SRC "src_Foundation")
if(${OpenFOAM_VERSION} MATCHES "v([0-9]*)") 
    set(PATH_SRC "src_ESI")
endif()
add_subdirectory(${PATH_SRC})
include_directories(${PATH_SRC})

add_executable(${PROJECT_NAME} ${PATH_SRC}/laplacianFoam.C)

# dynamic link
target_link_libraries(${PROJECT_NAME} OpenFOAM dl m Pstream finiteVolume fvOptions meshTools)

# =====================================================================================
#             XCode scheme configurations
# -------------------------------------------------------------------------------------
if (CMAKE_GENERATOR MATCHES "Xcode")
    message(STATUS "Set xcode scheme-run-arguments")
    set_property (TARGET ${PROJECT_NAME} PROPERTY XCODE_SCHEME_ARGUMENTS "-case ${CMAKE_SOURCE_DIR}/testCase")
    set_property(TARGET ${PROJECT_NAME} PROPERTY XCODE_SCHEME_ENVIRONMENT WM_PROJECT_DIR=$ENV{WM_PROJECT_DIR} WM_PROJECT=$ENV{WM_PROJECT} WM_PROJECT_VERSION=$ENV{WM_PROJECT_VERSION})
endif ()
# =====================================================================================
# ======================= Message out ===========================
message(STATUS "Configuration type: " ${CMAKE_CONFIGURATION_TYPES})

2. Generate project

This step is pretty easy if the above step is finished. Assuming you have already downloaded the example case files mentioned above, you will get a folder named cmakeLaplacianFoam.

2.1 Xcode on macOS

Just run the following command, and you will get the Xcode project file.

Code:
cd cmakeLaplacianFoam
mkdir build
cd build
cmake -GXcode ..




2.1.1 Build the solver and run a case in Xcode
Just simply double-click the generated Xcode project file laplacianFoam.xcodeproj and click the black triangle button shown in the following snapshot.



2.1.2 Debug OpenFOAM code in Xcode

Okay, let's do something fancier! Now you could set some breakpoints or do whatever debug processes in the Xcode. More details about Xcode debug features can be found at Apple tutorial of Xcode debugging website. For example, I set some breakpoints in the laplacianFoam.C and click the black triangle button to start the debug process. One can inspect the variables, memories, ... see the following snapshot.



2.2 CLion

It is a little bit different from what we did in the Xcode above if one would like to develop & debug the OpenFOAM code in CLion. Because the CMake (<=3.22) does not support to generating CLion project so far. The good news is CLion can directly open CMakeLists.txt to construct a project, see the following snapshots,




2.2.1 Build the solver and run a case in CLion
One could get construct the CLion project of the laplacianFoam as shown in the following snapshot. Then just simply click the hammer-like button to build the solver and click the triangle button to run a case.



2.2.2 Debug OpenFOAM code in CLion

Similar to what we did in the Xcode (2.1.2) above, just click the debug button (on the right side of the triangle button) to do debug process, see the snapshot below,



All right, that's all the key points. enjoy!
cosscholar is offline   Reply With Quote

Reply

Tags
clion, cmake, macos, openfoam, xcode


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 00:15.