diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index b44cf288d..14a92b417 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -5,6 +5,20 @@ set(GRIDKIT_EXAMPLES_INSTALL_ROOT "share/gridkit/examples") +# +# gridkit_example_current_install_path() +# +# Get the full installation destination for the current example directory. +# +# Result is stored in +# +macro(gridkit_example_current_install_path _out) + cmake_path(RELATIVE_PATH CMAKE_CURRENT_SOURCE_DIR + BASE_DIRECTORY ${CMAKE_SOURCE_DIR}/examples + OUTPUT_VARIABLE _rel_path) + set(${_out} ${GRIDKIT_EXAMPLES_INSTALL_ROOT}/${_rel_path}) +endmacro() + # # gridkit_example_add_file() # @@ -23,10 +37,7 @@ macro(gridkit_example_add_file _filename) endif() set(_binary_filepath ${CMAKE_CURRENT_BINARY_DIR}/${__fname}) configure_file(${_source_filepath} ${_binary_filepath} COPYONLY) - cmake_path(RELATIVE_PATH CMAKE_CURRENT_SOURCE_DIR - BASE_DIRECTORY ${CMAKE_SOURCE_DIR}/examples - OUTPUT_VARIABLE _rel_path) - set(_ex_install_path ${GRIDKIT_EXAMPLES_INSTALL_ROOT}/${_rel_path}) + gridkit_example_current_install_path(_ex_install_path) install(FILES ${_filename} DESTINATION ${_ex_install_path}) endmacro() diff --git a/examples/PhasorDynamics/Tiny/TwoBus/Basic/CMakeLists.txt b/examples/PhasorDynamics/Tiny/TwoBus/Basic/CMakeLists.txt index 3eda866b0..e1046f816 100644 --- a/examples/PhasorDynamics/Tiny/TwoBus/Basic/CMakeLists.txt +++ b/examples/PhasorDynamics/Tiny/TwoBus/Basic/CMakeLists.txt @@ -1,21 +1,25 @@ +gridkit_example_current_install_path(_install_path) + add_executable(TwoBusBasic TwoBusBasic.cpp) target_link_libraries(TwoBusBasic GRIDKIT::phasor_dynamics_components GRIDKIT::solvers_dyn) -install(TARGETS TwoBusBasic RUNTIME DESTINATION bin) +install(TARGETS TwoBusBasic RUNTIME DESTINATION ${_install_path}) add_executable(TwoBusBasicJson TwoBusBasicJson.cpp) -target_link_libraries(TwoBusBasicJson +target_link_libraries(TwoBusBasicJson GRIDKIT::phasor_dynamics_components GRIDKIT::solvers_dyn) target_include_directories(TwoBusBasicJson PRIVATE ${CMAKE_SOURCE_DIR}/third-party/nlohmann-json/include) target_include_directories(TwoBusBasicJson PRIVATE ${CMAKE_SOURCE_DIR}/third-party/magic-enum/include) -install(TARGETS TwoBusBasicJson RUNTIME DESTINATION bin) +install(TARGETS TwoBusBasicJson RUNTIME DESTINATION ${_install_path}) gridkit_example_add_file(TwoBusBasic.json) -add_test(NAME TwoBusBasicTest COMMAND TwoBusBasic) -add_test(NAME TwoBusBasicJsonTest - COMMAND TwoBusBasicJson TwoBusBasic.json +add_test(NAME TwoBusBasic COMMAND TwoBusBasic) +add_test(NAME TwoBusBasicJson + COMMAND TwoBusBasicJson ${CMAKE_CURRENT_BINARY_DIR}/TwoBusBasic.json) +add_test(NAME TwoBusBasicJson_no_arg + COMMAND TwoBusBasicJson WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) diff --git a/examples/PhasorDynamics/Tiny/TwoBus/Basic/TwoBusBasicJson.cpp b/examples/PhasorDynamics/Tiny/TwoBus/Basic/TwoBusBasicJson.cpp index 1bc5d619a..12c31e8ef 100644 --- a/examples/PhasorDynamics/Tiny/TwoBus/Basic/TwoBusBasicJson.cpp +++ b/examples/PhasorDynamics/Tiny/TwoBus/Basic/TwoBusBasicJson.cpp @@ -30,20 +30,40 @@ int main(int argc, const char* argv[]) using real_type = double; using index_type = size_t; + // + // Input file + // + + std::filesystem::path input_file; if (argc < 2) { - throw std::runtime_error( - "\n\nUsage:\n" - "\tTwoBusBasicJson \n"); + if (std::filesystem::exists("TwoBusBasic.json")) + { + input_file = std::filesystem::current_path() / "TwoBusBasic.json"; + } + else + { + std::cout << "\n" + "ERROR: No input file found or provided.\n" + "\n" + "Usage:\n" + " TwoBusBasicJson \n" + "\n" + "Please provide a JSON input file as a positional command-line \n" + "argument.\n" + "\n" + "By default this example will look for \"TwoBusBasic.json\" in the \n" + "current working directory and use that if found.\n" + "\n"; + exit(1); + } + } + else + { + input_file = argv[1]; } std::cout << "Example: TwoBusBasicJson\n"; - - // - // Input file - // - - auto input_file = std::filesystem::path(argv[1]); std::cout << "Input file: " << input_file << '\n'; // diff --git a/examples/PhasorDynamics/Tiny/TwoBus/Ieeet1/CMakeLists.txt b/examples/PhasorDynamics/Tiny/TwoBus/Ieeet1/CMakeLists.txt index b23e499ea..d1d5538aa 100644 --- a/examples/PhasorDynamics/Tiny/TwoBus/Ieeet1/CMakeLists.txt +++ b/examples/PhasorDynamics/Tiny/TwoBus/Ieeet1/CMakeLists.txt @@ -1,9 +1,11 @@ +gridkit_example_current_install_path(_install_path) + add_executable(TwoBusIeeet1 TwoBusIeeet1.cpp) target_link_libraries(TwoBusIeeet1 GRIDKIT::phasor_dynamics_components GRIDKIT::phasor_dynamics_signal GRIDKIT::solvers_dyn) -install(TARGETS TwoBusIeeet1 RUNTIME DESTINATION bin) +install(TARGETS TwoBusIeeet1 RUNTIME DESTINATION ${_install_path}) add_executable(TwoBusIeeet1Json TwoBusIeeet1Json.cpp) target_link_libraries(TwoBusIeeet1Json @@ -14,10 +16,12 @@ target_include_directories(TwoBusIeeet1Json PRIVATE ${CMAKE_SOURCE_DIR}/third-party/nlohmann-json/include) target_include_directories(TwoBusIeeet1Json PRIVATE ${CMAKE_SOURCE_DIR}/third-party/magic-enum/include) -install(TARGETS TwoBusIeeet1Json RUNTIME DESTINATION bin) +install(TARGETS TwoBusIeeet1Json RUNTIME DESTINATION ${_install_path}) gridkit_example_add_file(TwoBusIeeet1.json) add_test(NAME GenrouTest1_Ieeet1 COMMAND TwoBusIeeet1) add_test(NAME GenrouTest1_Ieeet1_Json - COMMAND TwoBusIeeet1Json TwoBusIeeet1.json + COMMAND TwoBusIeeet1Json ${CMAKE_CURRENT_BINARY_DIR}/TwoBusIeeet1.json) +add_test(NAME GenrouTest1_Ieeet1_Json_no_arg + COMMAND TwoBusIeeet1Json WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) diff --git a/examples/PhasorDynamics/Tiny/TwoBus/Ieeet1/TwoBusIeeet1Json.cpp b/examples/PhasorDynamics/Tiny/TwoBus/Ieeet1/TwoBusIeeet1Json.cpp index aeb5d159c..c9a7a7447 100644 --- a/examples/PhasorDynamics/Tiny/TwoBus/Ieeet1/TwoBusIeeet1Json.cpp +++ b/examples/PhasorDynamics/Tiny/TwoBus/Ieeet1/TwoBusIeeet1Json.cpp @@ -29,20 +29,40 @@ int main(int argc, const char* argv[]) using real_type = double; using index_type = size_t; - if (argc < 2) - { - throw std::runtime_error( - "\n\nUsage:\n" - "\tTwoBusBasicJson \n"); - } - - std::cout << "Example: TwoBusTgov1 + IEEET1 Exciter\n"; - // // Input file // - auto input_file = std::filesystem::path(argv[1]); + std::filesystem::path input_file; + if (argc < 2) + { + if (std::filesystem::exists("TwoBusIeeet1.json")) + { + input_file = std::filesystem::current_path() / "TwoBusIeeet1.json"; + } + else + { + std::cout << "\n" + "ERROR: No input file found or provided.\n" + "\n" + "Usage:\n" + " TwoBusIeeet1Json \n" + "\n" + "Please provide a JSON input file as a positional command-line \n" + "argument.\n" + "\n" + "By default this example will look for \"TwoBusIeeet1.json\" in the \n" + "current working directory and use that if found.\n" + "\n"; + exit(1); + } + } + else + { + input_file = argv[1]; + } + + std::cout << "Example: TwoBusIeeet1Json\n"; std::cout << "Input file: " << input_file << '\n'; // diff --git a/examples/PhasorDynamics/Tiny/TwoBus/Tgov1/CMakeLists.txt b/examples/PhasorDynamics/Tiny/TwoBus/Tgov1/CMakeLists.txt index 6c3d5d553..a287494b3 100644 --- a/examples/PhasorDynamics/Tiny/TwoBus/Tgov1/CMakeLists.txt +++ b/examples/PhasorDynamics/Tiny/TwoBus/Tgov1/CMakeLists.txt @@ -1,12 +1,14 @@ +gridkit_example_current_install_path(_install_path) + add_executable(TwoBusTgov1 TwoBusTgov1.cpp) -target_link_libraries(TwoBusTgov1 +target_link_libraries(TwoBusTgov1 GRIDKIT::phasor_dynamics_components GRIDKIT::phasor_dynamics_signal GRIDKIT::solvers_dyn) -install(TARGETS TwoBusTgov1 RUNTIME DESTINATION bin) +install(TARGETS TwoBusTgov1 RUNTIME DESTINATION ${_install_path}) add_executable(TwoBusTgov1Json TwoBusTgov1Json.cpp) -target_link_libraries(TwoBusTgov1Json +target_link_libraries(TwoBusTgov1Json GRIDKIT::phasor_dynamics_components GRIDKIT::phasor_dynamics_signal GRIDKIT::solvers_dyn) @@ -14,10 +16,12 @@ target_include_directories(TwoBusTgov1Json PRIVATE ${CMAKE_SOURCE_DIR}/third-party/nlohmann-json/include) target_include_directories(TwoBusTgov1Json PRIVATE ${CMAKE_SOURCE_DIR}/third-party/magic-enum/include) -install(TARGETS TwoBusTgov1Json RUNTIME DESTINATION bin) +install(TARGETS TwoBusTgov1Json RUNTIME DESTINATION ${_install_path}) gridkit_example_add_file(TwoBusTgov1.json) add_test(NAME GenrouTest1_tgov1 COMMAND TwoBusTgov1) add_test(NAME GenrouTest1_tgov1_json - COMMAND TwoBusTgov1Json TwoBusTgov1.json + COMMAND TwoBusTgov1Json ${CMAKE_CURRENT_BINARY_DIR}/TwoBusTgov1.json) +add_test(NAME GenrouTest1_tgov1_json_no_arg + COMMAND TwoBusTgov1Json WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) diff --git a/examples/PhasorDynamics/Tiny/TwoBus/Tgov1/TwoBusTgov1Json.cpp b/examples/PhasorDynamics/Tiny/TwoBus/Tgov1/TwoBusTgov1Json.cpp index 51d9e4fb2..d8b5486d4 100644 --- a/examples/PhasorDynamics/Tiny/TwoBus/Tgov1/TwoBusTgov1Json.cpp +++ b/examples/PhasorDynamics/Tiny/TwoBus/Tgov1/TwoBusTgov1Json.cpp @@ -33,20 +33,40 @@ int main(int argc, const char* argv[]) using real_type = double; using index_type = size_t; - if (argc < 2) - { - throw std::runtime_error( - "\n\nUsage:\n" - "\tTwoBusBasicJson \n"); - } - - std::cout << "Example: TwoBusTgov1Json \n"; - // // Input file // - auto input_file = std::filesystem::path(argv[1]); + std::filesystem::path input_file; + if (argc < 2) + { + if (std::filesystem::exists("TwoBusTgov1.json")) + { + input_file = std::filesystem::current_path() / "TwoBusTgov1.json"; + } + else + { + std::cout << "\n" + "ERROR: No input file found or provided.\n" + "\n" + "Usage:\n" + " TwoBusTgov1Json \n" + "\n" + "Please provide a JSON input file as a positional command-line \n" + "argument.\n" + "\n" + "By default this example will look for \"TwoBusTgov1.json\" in the \n" + "current working directory and use that if found.\n" + "\n"; + exit(1); + } + } + else + { + input_file = argv[1]; + } + + std::cout << "Example: TwoBusTgov1Json\n"; std::cout << "Input file: " << input_file << '\n'; //