Using IBAMR with your application when IBAMR was set up with CMake
If you used CMake to compile and install IBAMR then you can manage it like a
normal CMake-based dependency. You can configure your project (here assumed to
consist of a single file main.cpp
) with the following CMakeLists.txt
script:
PROJECT(mysolver)
CMAKE_MINIMUM_REQUIRED(VERSION 3.15.0)
# List your source files here - this example has just one.
SET(SOURCE_FILES main.cpp)
ADD_EXECUTABLE(main2d ${SOURCE_FILES})
FIND_PACKAGE(IBAMR 0.13.0 REQUIRED)
TARGET_LINK_LIBRARIES(main2d IBAMR::IBAMR2d)
# IBAMR saves the flags it used to compile - you can reuse them if you want to
SET(CMAKE_CXX_FLAGS ${IBAMR_CXX_FLAGS})
If you are working in 3D then you should add
TARGET_LINK_LIBRARIES(main3d IBAMR::IBAMR3d)
instead. Having both lines will set up executables main2d
and main3d
in 2D
and 3D, respectively. Listing IBAMR::IBAMR2d
or IBAMR::IBAMR3d
automatically
sets up the relevant CMake target, in this case one of the executables, to
depend on IBAMR and its dependencies and also sets NDIM
to the correct value.
You can add more CMake-based dependencies with TARGET_LINK_LIBRARIES()
in the
same way.
Note Under most circumstances you should use the same C++ compiler for your
project as the one used to set up IBAMR. Here, the C++ compiler is specified to
cmake with the -DCMAKE_CXX_COMPILER=$(which mpic++)
command-line argument,
which in turn uses the which
command to find the executable named mpic++
. If
you loaded a module to set up the MPI environment for compiling IBAMR, then you
should load the same module before running that command so that you use the same
MPI compiler wrappers. The sample code below assumes that the relevant MPI
module has already been loaded and that the C++ MPI compiler wrapper is
available in the current path.
Once you have written a CMakeLists.txt
script, in the same directory as your
code create a build directory and run cmake as
mkdir build
cd build
cmake -DIBAMR_ROOT=/path/to/ibamr/installation -DCMAKE_CXX_COMPILER=$(which mpic++) ../
in which /path/to/ibamr/installation
should be replaced by the directory path
(either relative or absolute) to the installation location of IBAMR.
Using IBAMR with your application when IBAMR was set up with ./configure
See Examples for how to use one of the examples as a scaffold for your application.
To compile an application that depends on the IBAMR library, it is necessary to link to the IBAMR library. The following Makefile for 2D application demonstrates one way to do this:
2D Makefile
######################################################################
## Here specify the location of the IBAMR source and the location
## where IBAMR has been built.
IBAMR_SRC_DIR = /path/to/IBAMR_SRC_DIR
IBAMR_BUILD_DIR = /path/to/IBAMR_BUILD_DIR
######################################################################
## Include variables specific to the particular IBAMR build.
include $(IBAMR_BUILD_DIR)/config/make.inc
## Needed for Xcode to capture compiler errors and warnings.
ifdef XCODE_VERSION_ACTUAL
CXXFLAGS += -fno-color-diagnostics
endif
######################################################################
## Build the application.
##
## NOTE: The following assumes that all .cpp files in the present
## directory are used to build the executable.
SRC = $(wildcard *.cpp)
CPPFLAGS += -MD -MP
PDIM = 2
OBJS = $(SRC:%.cpp=%.o) $(IBAMR_LIB_2D) $(IBTK_LIB_2D)
main2d: $(OBJS)
$(CXX) $(CXXFLAGS) $(OBJS) $(LDFLAGS) $(LIBS) -DNDIM=$(PDIM) -o $@
clean:
$(RM) main2d
$(RM) *.o *.lo *.objs *.ii *.int.c
$(RM) -r .libs
-include $(SRC:%.cpp=%.d)
Then, for this example, make main2d
would compile the application using the 2D
libraries.
If you instead set PDIM = 3 and other instances of 2D
to 3D
, then the application would compile against the 3D libraries. (See below)
3D Makefile
######################################################################
## Here specify the location of the IBAMR source and the location
## where IBAMR has been built.
IBAMR_SRC_DIR = /home/elijah/sfw/ibamr/IBAMR
IBAMR_BUILD_DIR = /home/elijah/sfw/ibamr/ibamr_master_build
######################################################################
## Include variables specific to the particular IBAMR build.
include $(IBAMR_BUILD_DIR)/config/make.inc
## Needed for Xcode to capture compiler errors and warnings.
ifdef XCODE_VERSION_ACTUAL
CXXFLAGS += -fno-color-diagnostics
endif
######################################################################
## Build the application.
##
## NOTE: The following assumes that all .cpp files in the present
## directory are used to build the executable.
SRC = $(wildcard *.cpp)
CPPFLAGS += -MD -MP
PDIM = 3
OBJS = $(SRC:%.cpp=%.o) $(IBAMR_LIB_3D) $(IBTK_LIB_3D)
main3d: $(OBJS)
$(CXX) $(CXXFLAGS) $(LDFLAGS) $(OBJS) $(LDFLAGS) $(LIBS) -DNDIM=$(PDIM) -o main3d
clean:
$(RM) main3d_thin exe
$(RM) *.o *.lo *.objs *.ii *.int.c
$(RM) -r .libs
-include $(SRC:%.cpp=%.d)