Skip to content

Commit

Permalink
fypp example
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicholaswogan committed Apr 1, 2024
1 parent 301a9b2 commit a483bbf
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
run: |
sudo apt-get install valgrind
python -m pip install --upgrade pip
python -m pip install jax jaxlib
python -m pip install jax jaxlib fypp
- name: configure cmake
run: cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}}
Expand Down
35 changes: 35 additions & 0 deletions cmake/fypp.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# srcext [in]: File extension of the source files
# trgext [in]: File extension of the target files
# srcfiles [in]: List of the source files
# trgfiles [out]: Contains the list of the preprocessed files on exit
#
function(preprocess preproc preprocopts srcext trgext srcfiles trgfiles)

set(_trgfiles)
foreach(srcfile IN LISTS srcfiles)
string(REGEX REPLACE "\\.${srcext}$" ".${trgext}" trgfile ${srcfile})
add_custom_command(
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${trgfile}
COMMAND ${preproc} ${preprocopts} ${CMAKE_CURRENT_SOURCE_DIR}/${srcfile} ${CMAKE_CURRENT_BINARY_DIR}/${trgfile}
MAIN_DEPENDENCY ${CMAKE_CURRENT_SOURCE_DIR}/${srcfile})
list(APPEND _trgfiles ${CMAKE_CURRENT_BINARY_DIR}/${trgfile})
endforeach()
set(${trgfiles} ${_trgfiles} PARENT_SCOPE)

endfunction()

# Preprocesses fortran files with fypp.
#
# It assumes that source files have the ".fypp" extension. Target files will be
# created with the extension ".f90". The FYPP variable must contain the path to
# the fypp-preprocessor.
#
# Args:
# fyppopts [in]: Options to pass to fypp.
# fyppfiles [in]: Files to be processed by fypp
# f90files [out]: List of created f90 files on exit
#
function (fypp_f90 fyppopts fyppfiles f90files)
preprocess("${FYPP}" "${fyppopts}" "fypp" "f90" "${fyppfiles}" _f90files)
set(${f90files} ${_f90files} PARENT_SCOPE)
endfunction()
11 changes: 10 additions & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,13 @@ target_include_directories(test_forwarddiff PUBLIC ${CMAKE_Fortran_MODULE_DIRECT

add_executable(test_sparse test_sparse.f90)
target_link_libraries(test_sparse forwarddiff)
target_include_directories(test_sparse PUBLIC ${CMAKE_Fortran_MODULE_DIRECTORY})
target_include_directories(test_sparse PUBLIC ${CMAKE_Fortran_MODULE_DIRECTORY})

find_program(FYPP fypp REQUIRED)
include(${PROJECT_SOURCE_DIR}/cmake/fypp.cmake)

set(fppFiles fypp_example.fypp)
fypp_f90("" "${fppFiles}" outFiles)
add_executable(fypp_example ${outFiles})
target_link_libraries(fypp_example forwarddiff)
target_include_directories(fypp_example PUBLIC ${CMAKE_Fortran_MODULE_DIRECTORY})
61 changes: 61 additions & 0 deletions test/fypp_example.fypp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#:set TYPES = ['real(wp)', 'type(dual)']
#:set NAMES = ['real', 'dual']
#:set TYPES_NAMES = list(zip(TYPES, NAMES))

program example
use forwarddiff, only: wp, jacobian
implicit none
interface rhs_robber
procedure :: rhs_rober_real
procedure :: rhs_rober_dual
end interface

call main()

contains

subroutine main()
real(wp) :: u(3), f(3), dfdu(3,3)
character(:), allocatable :: err

! Compute the jacobian
call jacobian(rhs_rober_dual, u, f, dfdu, err=err)
if (allocated(err)) then
print*,'err'
stop 1
endif

end subroutine

#:for TYPE1, NAME in TYPES_NAMES
subroutine rhs_rober_${NAME}$(u, du)
#:if NAME == 'dual'
use forwarddiff
#:endif
${TYPE1}$, intent(in) :: u(:)
${TYPE1}$, intent(out) :: du(:)

${TYPE1}$ :: tmp1, tmp2

real(wp), parameter :: k1 = 0.04_wp, &
k2 = 3.0e7_wp, &
k3 = 1.0e4_wp

! An intermediate result
tmp1 = -k1*u(1)

! An intermediate dual needs to be initialized if
! first assignment is just a number
#:if NAME == 'dual'
tmp2 = dual(size(u(1)%der))
#:endif
tmp2 = 0.0_wp

du(1) = tmp1 + k3*u(2)*u(3)
du(2) = k1*u(1) - k2*u(2)**2.0_wp - k3*u(2)*u(3)
du(3) = k2*u(2)**2.0_wp + tmp2

end subroutine

#:endfor
end program

0 comments on commit a483bbf

Please sign in to comment.