From 2848b3c3388e87a8bd14de71c6b077a62bb857df Mon Sep 17 00:00:00 2001 From: Hakim Hadj-djilani Date: Tue, 31 Mar 2026 16:52:08 +0200 Subject: [PATCH] cmake: add option NOPLOT to disable C++ matplotlib at building time. Usage: cmake -DNOPLOT=ON to enable, -DNOPLOT=OFF to disable (default). --- CMakeLists.txt | 36 ++++++++++++++++++++++++++++-------- src/graphics/matplotlib.cpp | 3 +++ src/main/polytrope1d.cpp | 3 +++ src/main/read_config.cpp | 13 ++++++++++++- src/main/star1d.cpp | 8 ++++++++ src/main/star2d.cpp | 13 +++++++++++-- src/star/star1d_class.cpp | 7 +++++++ src/star/star1d_solvers.cpp | 3 +++ src/star/star2d_class.cpp | 8 ++++++++ src/star/star_map.cpp | 9 +++++++++ src/star/star_phys.cpp | 3 +++ src/utils/debug.cpp | 22 ++++++++++++++++++---- 12 files changed, 113 insertions(+), 15 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 64f6f055..54103c17 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,6 +14,9 @@ set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE) set(CMAKE_Fortran_MODULE_DIRECTORY ${PROJECT_BINARY_DIR}/modules) +option("NOPLOT" "Disable matplotlib C++ code at compiling time." OFF) + + if(CMAKE_Fortran_COMPILER_ID STREQUAL "GNU") string(CONCAT CMAKE_Fortran_FLAGS "-g -O3" @@ -55,6 +58,10 @@ else() add_compile_definitions(ENABLE_PLT) endif() +if(NOPLOT) + add_compile_definitions(NOPLOT) +endif() + ### # Add the new C++ source file @@ -128,11 +135,9 @@ add_compile_definitions(VERSION="${PROJECT_VERSION}") add_compile_definitions(ESTER_DATADIR="${CMAKE_INSTALL_PREFIX}/share") add_compile_definitions(PACKAGE_VERSION="${PROJECT_VERSION}") -#add_library(ester SHARED # was previous -#add_library(ester STATIC -add_library(ester SHARED + +set(ESTER_CPP_FILES src/global/global.cpp - src/graphics/matplotlib.cpp src/solver/RKF_solver.cpp src/main/read_config.cpp src/solver/SDIRK_solver.cpp @@ -190,16 +195,31 @@ add_library(ester SHARED src/physics/opa_opmesa.cpp src/physics/mesa_opacity_modules.F90 src/physics/cubic_interpolator.f90 - src/physics/read_hdf5_data.cpp) - #src/physics/opa_mesa_wrapper.cpp) # blocked for fixing other issues - + src/physics/read_hdf5_data.cpp + #src/physics/opa_mesa_wrapper.cpp # blocked for fixing other issues + ) + +if(NOT NOPLOT) + list(PREPEND ESTER_CPP_FILES src/graphics/matplotlib.cpp) + # NOTE: only used for matplotlib + list(APPEND OPTIONAL_LIBS ${PYTHON_LIBRARIES}) +endif() + +#add_library(ester SHARED # was previous +#add_library(ester STATIC +add_library(ester SHARED + ${ESTER_CPP_FILES} +) + # Link libmesac libraries using --whole-archive to include all symbols #set(LIBMESAC_LIBS "-Wl,--whole-archive $ENV{LIBMESAC_ROOT}/lib/libkapcmesa.a $ENV{LIBMESAC_ROOT}/lib/libchemcmesa.a $ENV{LIBMESAC_ROOT}/lib/libutilscmesa.a -#Wl,--no-whole-archive ${SDK_LIBS}") # test lines 2 --> needed? message(LIBMESAC_LIBS="${LIBMESAC_LIBS}") target_link_libraries(ester cesam freeeos opint - ${OPTIONAL_LIBS} ${LAPACK_LIBRARIES} ${BLAS_LIBRARIES} ${CBLAS_LIBRARIES} ${PYTHON_LIBRARIES} ${HDF5_LIBRARIES} ${LIBMESAC_LIBS}) + ${OPTIONAL_LIBS} ${LAPACK_LIBRARIES} ${BLAS_LIBRARIES} ${CBLAS_LIBRARIES} ${HDF5_LIBRARIES} ${LIBMESAC_LIBS}) + + # Set properties for ester #set_target_properties(ester PROPERTIES diff --git a/src/graphics/matplotlib.cpp b/src/graphics/matplotlib.cpp index 6e8b844e..7dff83db 100644 --- a/src/graphics/matplotlib.cpp +++ b/src/graphics/matplotlib.cpp @@ -1,7 +1,10 @@ #ifndef WITH_CMAKE #include "ester-config.h" #endif +#ifndef NOPLOT #include "matplotlib.h" +#endif + #if ENABLE_PLT diff --git a/src/main/polytrope1d.cpp b/src/main/polytrope1d.cpp index 8bef5098..122ef904 100644 --- a/src/main/polytrope1d.cpp +++ b/src/main/polytrope1d.cpp @@ -2,7 +2,10 @@ #include "ester-config.h" #endif #include "utils.h" +#ifndef NOPLOT #include "matplotlib.h" +#endif + #include "star.h" #include "read_config.h" diff --git a/src/main/read_config.cpp b/src/main/read_config.cpp index 3ab8fc32..2ab0dcb6 100644 --- a/src/main/read_config.cpp +++ b/src/main/read_config.cpp @@ -6,7 +6,11 @@ #include "utils.h" #include "parser.h" #include "read_config.h" +#ifndef NOPLOT #include "matplotlib.h" +#endif + + configuration::configuration(int argc,char *argv[]) { @@ -54,8 +58,11 @@ configuration::configuration(int argc,char *argv[]) { } cmd.close(); + +#ifndef NOPLOT if (noplot == false) - plt::init(); + plt::init(); +#endif } int configuration::check_arg(const char *arg,const char *val) { @@ -100,8 +107,12 @@ int configuration::check_arg(const char *arg,const char *val) { } else if(!strcmp(arg,"noplot")) { strcpy(plot_device,"/NULL"); +#ifndef NOPLOT noplot = true; plt::init(true); +#else + ester_warn("matplotlib has been disabled at building time."); +#endif } else if(!strcmp(arg,"maxit")) { if(val==NULL) return 2; diff --git a/src/main/star1d.cpp b/src/main/star1d.cpp index 8fe90773..c7dbf62e 100644 --- a/src/main/star1d.cpp +++ b/src/main/star1d.cpp @@ -2,7 +2,10 @@ #include "ester-config.h" #endif #include "utils.h" +#ifndef NOPLOT #include "matplotlib.h" +#endif + #include "star.h" #include "read_config.h" @@ -92,12 +95,15 @@ int main(int argc,char *argv[]) { int last_plot_it = -100; + +#ifndef NOPLOT if (config.noplot == false) { plt::init(); plt::figure(1, 10, 4); A.plot(error_map.block(0, nit-1, 0 ,0)); plt::show(); } +#endif while(!last_it) { @@ -156,7 +162,9 @@ int main(int argc,char *argv[]) { if(config.verbose) printf("%2.2f seconds\n",t.value()); +#ifndef NOPLOT plt::show(true); +#endif return 0; } diff --git a/src/main/star2d.cpp b/src/main/star2d.cpp index 8bfc7430..9bd76d74 100644 --- a/src/main/star2d.cpp +++ b/src/main/star2d.cpp @@ -4,7 +4,10 @@ #include "utils.h" #include "star.h" #include "read_config.h" +#ifndef NOPLOT #include "matplotlib.h" +#endif + #include #include @@ -29,7 +32,9 @@ int main(int argc,char *argv[]) { signal(SIGINT,sig_handler); - plt::figure(1, 10, 4); +#ifndef NOPLOT + plt::figure(1, 10, 4); +#endif t.start(); @@ -118,11 +123,13 @@ int main(int argc,char *argv[]) { t_plot=tt(nit-1); #else static bool plotOpen = false; +#ifndef NOPLOT if (plotOpen == false) { plt::show(); plotOpen = true; } A.plot(error_map.block(0, nit-1, 0, 0)); +#endif // NOPLOT #endif } @@ -166,7 +173,9 @@ int main(int argc,char *argv[]) { if(config.verbose) printf("%2.2f seconds\n",t.value()); - plt::show(true); +#ifndef NOPLOT + plt::show(true); +#endif return 0; } diff --git a/src/star/star1d_class.cpp b/src/star/star1d_class.cpp index de99c4b7..112b649c 100644 --- a/src/star/star1d_class.cpp +++ b/src/star/star1d_class.cpp @@ -3,7 +3,10 @@ #endif #include "utils.h" #include "star.h" +#ifndef NOPLOT #include "matplotlib.h" +#endif + #include #include @@ -303,6 +306,7 @@ matrix star1d::spectrum(const matrix& var) { void star1d::plot(const matrix_map& error) { +#ifndef NOPLOT plt::clf(); plt::subplot(231); @@ -369,4 +373,7 @@ void star1d::plot(const matrix_map& error) { plt::draw(); plt::pause(); +#else + ester_warn("matplotlib has been disabled at building time."); +#endif } diff --git a/src/star/star1d_solvers.cpp b/src/star/star1d_solvers.cpp index 23a1907e..691cbd58 100644 --- a/src/star/star1d_solvers.cpp +++ b/src/star/star1d_solvers.cpp @@ -6,7 +6,10 @@ #include #include "symbolic.h" +#ifndef NOPLOT #include "matplotlib.h" +#endif + //--------------------------------------------------------------------- void star1d::fill() { Y0=1.-X0-Z0; diff --git a/src/star/star2d_class.cpp b/src/star/star2d_class.cpp index e77ad81f..904d7320 100644 --- a/src/star/star2d_class.cpp +++ b/src/star/star2d_class.cpp @@ -8,7 +8,10 @@ #include #include #include +#ifndef NOPLOT #include "matplotlib.h" +#endif + #include "physics.h" #include @@ -983,6 +986,7 @@ void star2d::dump_info() { } void star2d::plot(const matrix_map& error) { +#ifndef NOPLOT matrix theta = vector(0, 2*M_PI, 64); matrix r = map.leg.eval_00(this->r, theta); @@ -1045,4 +1049,8 @@ void star2d::plot(const matrix_map& error) { plt::draw(); plt::pause(); + +#else + ester_warn("matplotlib has been disabled at building time."); +#endif } diff --git a/src/star/star_map.cpp b/src/star/star_map.cpp index 5035ab15..5cd98caf 100644 --- a/src/star/star_map.cpp +++ b/src/star/star_map.cpp @@ -2,7 +2,10 @@ #include "ester-config.h" #endif #include "star.h" +#ifndef NOPLOT #include "matplotlib.h" +#endif + extern "C" { #include @@ -215,15 +218,21 @@ matrix star2d::find_boundaries(const matrix &logTi) const { zj+=dzj; nit++; if(nit>100) { +#ifndef NOPLOT plt::clf(); plt::plot(r, log(T), "$T$"); +#endif for (int i=0; i diff --git a/src/utils/debug.cpp b/src/utils/debug.cpp index e67fa7f3..1df5f111 100644 --- a/src/utils/debug.cpp +++ b/src/utils/debug.cpp @@ -3,7 +3,10 @@ #endif #include "debug.h" #include "star.h" +#ifndef NOPLOT #include "matplotlib.h" +#endif + #include #include @@ -39,7 +42,9 @@ void repl::print() { debugger::debugger(int argc, char * argv[], star1d& s) : repl("ESTER Debugger:"), star(s) { - plt::close(); +#ifndef NOPLOT + plt::close(); +#endif } std::string repl::read() { @@ -67,9 +72,9 @@ std::vector repl::get_args(const std::string& cmd) { } void debugger::help() { - std::cout << "Commands availiable:\n"; + std::cout << "Commands available:\n"; std::cout << " help # prints this help\n"; - std::cout << " plot var # plots varaible `var'\n"; + std::cout << " plot var # plots variable `var'\n"; std::cout << " show # shows plots\n"; std::cout << " save # saves model to file \"debug.h5\"\n"; std::cout << " exit # exits debugger\n"; @@ -79,23 +84,29 @@ int debugger::eval(const std::string& _cmd) { auto args = get_args(_cmd); std::string cmd = args[0]; if (cmd == "clear") { +#ifndef NOPLOT plt::clf(); +#endif } else if (cmd == "help") { help(); } else if (cmd == "show") { +#ifndef NOPLOT plt::show(true); +#endif } else if (cmd == "plot") { if (args.size() < 2) { - LOGE("Missong argument to command `%s\'\n", args[0].c_str()); + LOGE("Missing argument to command `%s\'\n", args[0].c_str()); return 1; } std::string var = args[1]; matrix m = get_var(var); +#ifndef NOPLOT plt::plot(star.r, m, "$" + var + "$"); plt::legend(); +#endif } else if (cmd == "exit" || cmd == "\0") { return 0; @@ -111,8 +122,11 @@ int debugger::eval(const std::string& _cmd) { std::string var = args[1]; matrix m = get_var(var); + +#ifndef NOPLOT plt::semilogy(star.spectrum(m), "spectrum " + var); plt::legend(); +#endif } else if (cmd == "") { }