Skip to content

Commit a71e70c

Browse files
authored
Merge pull request #10 from itpplasma/7-make-mfem-optional-dependency
Make MFEM optional dependency
2 parents 351c8be + ed0fbb8 commit a71e70c

7 files changed

+51
-28
lines changed

CMakeLists.txt

+21-8
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_SOURCE_DIR}/cmake)
55
### Define the project
66
project(MEPHIT LANGUAGES C CXX Fortran)
77

8+
### Define options
9+
option(WITH_MFEM "Compile with MFEM" FALSE)
10+
811
### Specify paths
912
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)
1013
set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib)
@@ -90,7 +93,10 @@ add_link_options(${NETCDF_FLIBS})
9093
include(ExternalProject)
9194
include(SetupCODE)
9295
include(SetupTriangle)
93-
include(SetupMFEM)
96+
if(WITH_MFEM)
97+
include(SetupMFEM)
98+
add_definitions(-DUSE_MFEM)
99+
endif()
94100
include(Util)
95101
find_or_fetch(libneo)
96102

@@ -128,9 +134,9 @@ set_source_files_properties(${COMMON_FORTRAN_SRC_FILES}
128134
set(MEPHIT_FORTRAN_SRC_FILES
129135
src/mephit_conf.f90
130136
src/mephit_util.f90
131-
src/mephit_mesh.f90
137+
src/mephit_mesh.F90
132138
src/mephit_pert.f90
133-
src/mephit_iter.f90
139+
src/mephit_iter.F90
134140
)
135141
set_source_files_properties(src/mephit_test.f90 ${MEPHIT_FORTRAN_SRC_FILES}
136142
PROPERTIES COMPILE_FLAGS "${CMAKE_Fortran_FLAGS} ${WARNING_FLAGS}")
@@ -142,9 +148,13 @@ set(MEPHIT_C_SRC_FILES
142148
)
143149
set(MEPHIT_CPP_SRC_FILES
144150
src/mephit_fem.cpp
145-
src/magnetic_differential_equation.cpp
146-
src/miscellaneous.cpp
147151
)
152+
if(WITH_MFEM)
153+
set(MEPHIT_CPP_SRC_FILES ${MEPHIT_CPP_SRC_FILES}
154+
src/magnetic_differential_equation.cpp
155+
src/miscellaneous.cpp
156+
)
157+
endif()
148158
set_source_files_properties(src/mephit_run.c ${MEPHIT_C_SRC_FILES} ${MEPHIT_CPP_SRC_FILES}
149159
PROPERTIES COMPILE_FLAGS "${CMAKE_C_FLAGS} -DREAL=double -I${TRIANGLE_INCLUDE_DIR} -I${SUITESPARSE_INCLUDE_DIRS} -L${TRIANGLE_LIB_DIR}")
150160

@@ -157,11 +167,10 @@ add_library(mephit SHARED
157167
${MEPHIT_C_SRC_FILES}
158168
${MEPHIT_CPP_SRC_FILES}
159169
)
160-
add_dependencies(mephit MFEM TRIANGLE)
170+
add_dependencies(mephit TRIANGLE)
161171
target_include_directories(mephit PUBLIC
162172
${magfie_include_dir}
163173
${NETCDF_INCLUDES}
164-
${MFEM_INCLUDE_DIRS}
165174
${NETCDF_INCLUDES}
166175
${TRIANGLE_INCLUDE_DIR}
167176
)
@@ -171,7 +180,6 @@ set(MEPHIT_LIBS
171180
hdf5::hdf5_fortran
172181
hdf5::hdf5_hl_fortran
173182
GSL::gsl
174-
${MFEM_LIBRARIES}
175183
BLAS::BLAS
176184
LAPACK::LAPACK
177185
${FFTW_LIBRARIES}
@@ -186,6 +194,11 @@ if(LEGACY_SUITESPARSE)
186194
else()
187195
target_link_libraries(mephit SuiteSparse::umfpack)
188196
endif()
197+
if(WITH_MFEM)
198+
add_dependencies(mephit MFEM)
199+
target_include_directories(mephit PUBLIC ${MFEM_INCLUDE_DIRS})
200+
target_link_libraries(mephit ${MFEM_LIBRARIES})
201+
endif()
189202

190203
### Define executables
191204
add_executable(mephit_run.x src/mephit_run.c)

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Prerequisites from external sources for running MEPHIT are as follows.
1515
- [Triangle](https://www.cs.cmu.edu/~quake/triangle.html)
1616
- [Boost](https://www.boost.org/)
1717
- [FreeFem++](https://github.com/FreeFem/FreeFem-sources)
18-
- [MFEM](https://mfem.org/)
18+
- [MFEM](https://mfem.org/) is optional
1919
- [HDF5](https://www.hdfgroup.org/downloads/hdf5)
2020
- [NetCDF](https://github.com/Unidata/netcdf-fortran)
2121
- Python 3 including packages listed in [`requirements.txt`](requirements.txt) for plotting

src/mephit_conf.f90

-5
Original file line numberDiff line numberDiff line change
@@ -198,9 +198,6 @@ module mephit_conf
198198
!> Enable debugging of initial iterations without plasma response. Defaults to true.
199199
logical :: debug_initial = .true.
200200

201-
!> Enable MFEM interface. Defaults to false.
202-
logical :: debug_mfem = .false.
203-
204201
!> Single poloidal mode used in comparison with KiLCA code. Defaults to 0 (ASDEX
205202
!> geometry).
206203
integer :: kilca_pol_mode = 0
@@ -348,8 +345,6 @@ subroutine config_export_hdf5(config, file, group)
348345
comment = 'offset added to radial electric field', unit = 'statV cm^-1')
349346
call h5_add(h5id_root, grp // '/debug_initial', config%debug_initial, &
350347
comment = 'enable debugging of initial runs without plasma response')
351-
call h5_add(h5id_root, grp // '/debug_MFEM', config%debug_MFEM, &
352-
comment = 'enable MFEM interface')
353348
call h5_add(h5id_root, grp // '/kilca_pol_mode', config%kilca_pol_mode, &
354349
comment = 'single poloidal mode used in comparison with KiLCA code')
355350
call h5_add(h5id_root, grp // '/kilca_scale_factor', config%kilca_scale_factor, &

src/mephit_fem.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include "mephit_fem.h"
2+
#ifdef USE_MFEM
23
#include "mfem.hpp"
34
#include "magnetic_differential_equation.h"
5+
#endif // USE_MFEM
46
#include <boost/geometry.hpp>
57
#include <boost/geometry/geometries/point_xy.hpp>
68
#include <boost/geometry/index/rtree.hpp>
@@ -39,6 +41,8 @@ extern "C" void Rtree_query(double R, double Z, int *result_size, int **results)
3941
*results = query_results.data();
4042
}
4143

44+
#ifdef USE_MFEM
45+
4246
typedef std::map<std::pair<double, double>, size_t> points_2D;
4347

4448
/*
@@ -142,3 +146,5 @@ extern "C" int FEM_test(const char *mesh_file,
142146
}
143147
return 0;
144148
}
149+
150+
#endif // USE_MFEM

src/mephit_fem.h

+3
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,15 @@ typedef void real_vector_field(const double R,
5050
typedef void complex_scalar_field(const double R,
5151
const double Z,
5252
complex_double *scalar);
53+
54+
#ifdef USE_MFEM
5355
int FEM_test(const char *mesh_file,
5456
const int tor_mode,
5557
const int n_dof,
5658
complex_double *dof,
5759
real_vector_field *unit_B0,
5860
complex_scalar_field *MDE_inhom);
61+
#endif // USE_MFEM
5962

6063
#ifdef __cplusplus
6164
}

src/mephit_iter.f90 renamed to src/mephit_iter.F90

+15-11
Original file line numberDiff line numberDiff line change
@@ -541,12 +541,14 @@ subroutine perteq_iterate(perteq, precond, fdm)
541541
write (postfix, postfix_fmt) kiter
542542
Bn_prev%DOF(:) = perteq%Bn%DOF
543543
Bn_prev%comp_phi(:) = perteq%Bn%comp_phi
544-
! compute B_(n+1) = K * B_n + B_vac ... different from next_iteration_arnoldi
545-
if (kiter <= 1 .and. conf%debug_MFEM) then
544+
#ifdef USE_MFEM
545+
if (kiter <= 1) then
546546
call MFEM_test(perteq%pn)
547547
call perteq_write('("iter/", a, "MFEM_' // postfix // '")', &
548548
' (after MFEM iteration)', presn = perteq%pn, presmn = perteq%pn)
549549
end if
550+
#endif
551+
! compute B_(n+1) = K * B_n + B_vac ... different from next_iteration_arnoldi
550552
call compute_presn(perteq, fdm, conf%damp)
551553
if (kiter <= 1) then
552554
call perteq_write('("iter/", a, "' // postfix // '")', &
@@ -634,16 +636,16 @@ subroutine debug_initial_iteration(perteq, fdm)
634636
if (conf%debug_initial) then
635637
perteq%Bn%DOF(:) = vac%Bn%DOF
636638
perteq%Bn%comp_phi(:) = vac%Bn%comp_phi
637-
if (conf%debug_MFEM) then
638-
call MFEM_test(perteq%pn)
639-
call perteq_write('("debug_MFEM_initial/MFEM_", a)', &
640-
' (initial MFEM iteration)', presn = perteq%pn, presmn = perteq%pn)
641-
end if
639+
#ifdef USE_MFEM
640+
call MFEM_test(perteq%pn)
641+
call perteq_write('("debug_MFEM_initial/MFEM_", a)', &
642+
' (initial MFEM iteration)', presn = perteq%pn, presmn = perteq%pn)
643+
#endif
642644
call compute_presn(perteq, fdm, .false.)
643-
if (conf%debug_MFEM) then
644-
call perteq_write('("debug_MFEM_initial/", a)', &
645-
' (initial iteration)', presn = perteq%pn, presmn = perteq%pn)
646-
end if
645+
#ifdef USE_MFEM
646+
call perteq_write('("debug_MFEM_initial/", a)', &
647+
' (initial iteration)', presn = perteq%pn, presmn = perteq%pn)
648+
#endif
647649
call compute_currn(perteq, fdm, .false., .true.)
648650
perteq%Bn%DOF(:) = vac%Bn%DOF
649651
perteq%Bn%comp_phi(:) = vac%Bn%comp_phi
@@ -705,6 +707,7 @@ subroutine presn_inhom(R, Z, scalar) bind(C, name = 'presn_inhom')
705707
scalar = -dp0_dpsi * (B_n(1) * B_0(3) - B_n(3) * B_0(1)) * R / sqrt(sum(B_0 * B_0))
706708
end subroutine presn_inhom
707709

710+
#ifdef USE_MFEM
708711
subroutine MFEM_test(pn)
709712
use iso_c_binding, only: c_int, c_null_char, c_loc, c_funloc
710713
use mephit_conf, only: conf, logger, basename_suffix, decorate_filename
@@ -720,6 +723,7 @@ subroutine MFEM_test(pn)
720723
call logger%write_msg
721724
end if
722725
end subroutine MFEM_test
726+
#endif
723727

724728
subroutine FDM_init(fdm, nnz)
725729
type(FDM_t), intent(inout) :: fdm

src/mephit_mesh.f90 renamed to src/mephit_mesh.F90

+5-3
Original file line numberDiff line numberDiff line change
@@ -1004,9 +1004,9 @@ subroutine generate_mesh
10041004
call init_flux_variables
10051005
call compare_gpec_coordinates
10061006
call connect_mesh_points
1007-
if (conf%debug_MFEM) then
1008-
call mesh_write_MFEM
1009-
end if
1007+
#ifdef USE_MFEM
1008+
call mesh_write_MFEM
1009+
#endif
10101010
call write_FreeFem_mesh
10111011
call cache_init(cache, 4)
10121012
call cache_equilibrium_field
@@ -2843,6 +2843,7 @@ subroutine mesh_write(mesh, file, dataset)
28432843
call h5_close(h5id_root)
28442844
end subroutine mesh_write
28452845

2846+
#ifdef USE_MFEM
28462847
subroutine mesh_write_MFEM
28472848
use mephit_conf, only: basename_suffix, decorate_filename
28482849
integer :: fid, ktri, kp, kpoi
@@ -2870,6 +2871,7 @@ subroutine mesh_write_MFEM
28702871
end do
28712872
close(fid)
28722873
end subroutine mesh_write_MFEM
2874+
#endif
28732875

28742876
subroutine write_FreeFem_mesh
28752877
use iso_c_binding, only: c_null_char

0 commit comments

Comments
 (0)