From a79491359cd9943d6b5123a6f15a47252fe10466 Mon Sep 17 00:00:00 2001 From: "Mohan, Jaya" Date: Tue, 19 Mar 2024 10:38:17 -0700 Subject: [PATCH 01/36] add new branch and pytests repo --- Projects.code-workspace | 7 +++ import ctypes.py | 105 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 112 insertions(+) create mode 100644 Projects.code-workspace create mode 100644 import ctypes.py diff --git a/Projects.code-workspace b/Projects.code-workspace new file mode 100644 index 00000000..2a0ed79b --- /dev/null +++ b/Projects.code-workspace @@ -0,0 +1,7 @@ +{ + "folders": [ + { + "path": ".." + } + ] +} \ No newline at end of file diff --git a/import ctypes.py b/import ctypes.py new file mode 100644 index 00000000..1fddb64c --- /dev/null +++ b/import ctypes.py @@ -0,0 +1,105 @@ +import ctypes +import mpi4py.MPI as MPI # Assuming you have mpi4py installed + +# Load the C libraries +libtcopflow = ctypes.CDLL("libtcopflow.so") # Adjust the library name as needed + +# Define necessary types and constants +PetscErrorCode = ctypes.c_int +PetscBool = ctypes.c_int + +# Function prototypes from tcopflow.h +libtcopflow.ExaGOInitialize.argtypes = [MPI.Comm, ctypes.POINTER(ctypes.c_int), ctypes.POINTER(ctypes.c_char_p), ctypes.c_char_p, ctypes.c_char_p] +libtcopflow.ExaGOInitialize.restype = PetscErrorCode + +libtcopflow.PetscOptionsGetBool.argtypes = [ctypes.c_void_p, ctypes.c_void_p, ctypes.c_char_p, ctypes.POINTER(PetscBool), ctypes.c_void_p] +libtcopflow.PetscOptionsGetBool.restype = PetscErrorCode + +libtcopflow.TCOPFLOWCreate.argtypes = [MPI.Comm, ctypes.POINTER(ctypes.c_void_p)] +libtcopflow.TCOPFLOWCreate.restype = PetscErrorCode + +libtcopflow.TCOPFLOWSetNetworkData.argtypes = [ctypes.c_void_p, ctypes.c_char_p] +libtcopflow.TCOPFLOWSetNetworkData.restype = PetscErrorCode + +# Define necessary variables and parameters +argc = ctypes.c_int(0) +argv = ctypes.POINTER(ctypes.c_char_p)() +appname = b"tcopflow" +help_msg = b"User example calling TCOPFLOW.\n\n" + +comm = MPI.COMM_WORLD +file_path = ctypes.create_string_buffer(PETSC_MAX_PATH_LEN) +ploadprofile_path = ctypes.create_string_buffer(PETSC_MAX_PATH_LEN) +qloadprofile_path = ctypes.create_string_buffer(PETSC_MAX_PATH_LEN) +windgenprofile_path = ctypes.create_string_buffer(PETSC_MAX_PATH_LEN) +print_output = PetscBool(0) +save_output = PetscBool(0) +flg = PetscBool(0) +flg1 = PetscBool(0) +flg2 = PetscBool(0) +flg3 = PetscBool(0) + +# Initialize ExaGO application +ierr = libtcopflow.ExaGOInitialize(comm, ctypes.byref(argc), ctypes.byref(argv), appname, help_msg) +if ierr: + print(f"Could not initialize ExaGO application {appname}.") + exit(ierr) + +# Get command line options +ierr = libtcopflow.PetscOptionsGetBool(None, None, b"-print_output", ctypes.byref(print_output), None) +if ierr: + print("Error getting command line option -print_output.") + exit(ierr) + +ierr = libtcopflow.PetscOptionsGetBool(None, None, b"-save_output", ctypes.byref(save_output), None) +if ierr: + print("Error getting command line option -save_output.") + exit(ierr) + +# Create TCOPFLOW object +tcopflow = ctypes.c_void_p(0) +ierr = libtcopflow.TCOPFLOWCreate(MPI.COMM_WORLD, ctypes.byref(tcopflow)) +if ierr: + print("Error creating TCOPFLOW object.") + exit(ierr) + +# Set network data file +ierr = libtcopflow.TCOPFLOWSetNetworkData(tcopflow, file_path) +if ierr: + print("Error setting network data file.") + exit(ierr) + +# Set loadp and loadq files +if flg1 and flg2: + ierr = libtcopflow.TCOPFLOWSetLoadProfiles(tcopflow, ploadprofile_path, qloadprofile_path) +elif flg1: + ierr = libtcopflow.TCOPFLOWSetLoadProfiles(tcopflow, ploadprofile_path, None) +elif flg2: + ierr = libtcopflow.TCOPFLOWSetLoadProfiles(tcopflow, None, qloadprofile_path) +if ierr: + print("Error setting load profiles.") + exit(ierr) + +# Set windgen profile +if flg3: + ierr = libtcopflow.TCOPFLOWSetWindGenProfiles(tcopflow, windgenprofile_path) +if ierr: + print("Error setting windgen profile.") + exit(ierr) + +# Solve TCOPFLOW +ierr = libtcopflow.TCOPFLOWSolve(tcopflow) +if ierr: + print("Error solving TCOPFLOW.") + exit(ierr) + +# Print solution if requested +if print_output: + ierr = libtcopflow.TCOPFLOWPrintSolution(tcopflow, 0) + if ierr: + print("Error printing TCOPFLOW solution.") + exit(ierr) + +# Save solution if requested +if save_output: + ierr = libtcopflow.TCOPFLOWS From b86a6758bee14ab3b3a84252017582d83f55e594 Mon Sep 17 00:00:00 2001 From: "Mohan, Jaya" Date: Tue, 26 Mar 2024 11:08:17 -0700 Subject: [PATCH 02/36] add tecopflow files --- tcopflow1.py | 40 ++++++++++++++++++++++++++++++++++++++++ tcopflow2.py | 0 2 files changed, 40 insertions(+) create mode 100644 tcopflow1.py create mode 100644 tcopflow2.py diff --git a/tcopflow1.py b/tcopflow1.py new file mode 100644 index 00000000..e14f3f0b --- /dev/null +++ b/tcopflow1.py @@ -0,0 +1,40 @@ +import exago + +def initialize(appname): + exago.initialize(appname) + +def finalize(): + exago.finalize() + +class TCOPFLOW: + def __init__(self): + self.tcopflow = None + + def create(self, netfile, ploadprofile=None, qloadprofile=None, windgenprofile=None): + self.tcopflow = exago.TCOPFLOW() + self.tcopflow.set_network_data(netfile) + + if ploadprofile is not None or qloadprofile is not None: + self.tcopflow.set_load_profiles(ploadprofile, qloadprofile) + + if windgenprofile is not None: + self.tcopflow.set_wind_gen_profiles(windgenprofile) + + def solve(self): + self.tcopflow.solve() + + def print_solution(self, index=0): + self.tcopflow.print_solution(index) + + def save_solution(self, file_name): + self.tcopflow.save_solution_all(exago.MATPOWER, file_name) + + +initialize("tcopflow") +tcopflow = TCOPFLOW() +tcopflow.create("datafiles/case9/case9mod.m") +tcopflow.solve() +tcopflow.print_solution() +tcopflow.save_solution("tcopflowout") +del tcopflow +finalize() \ No newline at end of file diff --git a/tcopflow2.py b/tcopflow2.py new file mode 100644 index 00000000..e69de29b From b7d26944c5866b7de2e1f29828db6c1312c87c53 Mon Sep 17 00:00:00 2001 From: "Mohan, Jaya" Date: Tue, 26 Mar 2024 11:20:03 -0700 Subject: [PATCH 03/36] clean dir and add new tcopflow files --- Projects.code-workspace | 7 -- import ctypes.py | 105 --------------------- tcopflow1.py => tcopflow-bind/tcopflow1.py | 0 tcopflow2.py => tcopflow-bind/tcopflow2.py | 0 4 files changed, 112 deletions(-) delete mode 100644 Projects.code-workspace delete mode 100644 import ctypes.py rename tcopflow1.py => tcopflow-bind/tcopflow1.py (100%) rename tcopflow2.py => tcopflow-bind/tcopflow2.py (100%) diff --git a/Projects.code-workspace b/Projects.code-workspace deleted file mode 100644 index 2a0ed79b..00000000 --- a/Projects.code-workspace +++ /dev/null @@ -1,7 +0,0 @@ -{ - "folders": [ - { - "path": ".." - } - ] -} \ No newline at end of file diff --git a/import ctypes.py b/import ctypes.py deleted file mode 100644 index 1fddb64c..00000000 --- a/import ctypes.py +++ /dev/null @@ -1,105 +0,0 @@ -import ctypes -import mpi4py.MPI as MPI # Assuming you have mpi4py installed - -# Load the C libraries -libtcopflow = ctypes.CDLL("libtcopflow.so") # Adjust the library name as needed - -# Define necessary types and constants -PetscErrorCode = ctypes.c_int -PetscBool = ctypes.c_int - -# Function prototypes from tcopflow.h -libtcopflow.ExaGOInitialize.argtypes = [MPI.Comm, ctypes.POINTER(ctypes.c_int), ctypes.POINTER(ctypes.c_char_p), ctypes.c_char_p, ctypes.c_char_p] -libtcopflow.ExaGOInitialize.restype = PetscErrorCode - -libtcopflow.PetscOptionsGetBool.argtypes = [ctypes.c_void_p, ctypes.c_void_p, ctypes.c_char_p, ctypes.POINTER(PetscBool), ctypes.c_void_p] -libtcopflow.PetscOptionsGetBool.restype = PetscErrorCode - -libtcopflow.TCOPFLOWCreate.argtypes = [MPI.Comm, ctypes.POINTER(ctypes.c_void_p)] -libtcopflow.TCOPFLOWCreate.restype = PetscErrorCode - -libtcopflow.TCOPFLOWSetNetworkData.argtypes = [ctypes.c_void_p, ctypes.c_char_p] -libtcopflow.TCOPFLOWSetNetworkData.restype = PetscErrorCode - -# Define necessary variables and parameters -argc = ctypes.c_int(0) -argv = ctypes.POINTER(ctypes.c_char_p)() -appname = b"tcopflow" -help_msg = b"User example calling TCOPFLOW.\n\n" - -comm = MPI.COMM_WORLD -file_path = ctypes.create_string_buffer(PETSC_MAX_PATH_LEN) -ploadprofile_path = ctypes.create_string_buffer(PETSC_MAX_PATH_LEN) -qloadprofile_path = ctypes.create_string_buffer(PETSC_MAX_PATH_LEN) -windgenprofile_path = ctypes.create_string_buffer(PETSC_MAX_PATH_LEN) -print_output = PetscBool(0) -save_output = PetscBool(0) -flg = PetscBool(0) -flg1 = PetscBool(0) -flg2 = PetscBool(0) -flg3 = PetscBool(0) - -# Initialize ExaGO application -ierr = libtcopflow.ExaGOInitialize(comm, ctypes.byref(argc), ctypes.byref(argv), appname, help_msg) -if ierr: - print(f"Could not initialize ExaGO application {appname}.") - exit(ierr) - -# Get command line options -ierr = libtcopflow.PetscOptionsGetBool(None, None, b"-print_output", ctypes.byref(print_output), None) -if ierr: - print("Error getting command line option -print_output.") - exit(ierr) - -ierr = libtcopflow.PetscOptionsGetBool(None, None, b"-save_output", ctypes.byref(save_output), None) -if ierr: - print("Error getting command line option -save_output.") - exit(ierr) - -# Create TCOPFLOW object -tcopflow = ctypes.c_void_p(0) -ierr = libtcopflow.TCOPFLOWCreate(MPI.COMM_WORLD, ctypes.byref(tcopflow)) -if ierr: - print("Error creating TCOPFLOW object.") - exit(ierr) - -# Set network data file -ierr = libtcopflow.TCOPFLOWSetNetworkData(tcopflow, file_path) -if ierr: - print("Error setting network data file.") - exit(ierr) - -# Set loadp and loadq files -if flg1 and flg2: - ierr = libtcopflow.TCOPFLOWSetLoadProfiles(tcopflow, ploadprofile_path, qloadprofile_path) -elif flg1: - ierr = libtcopflow.TCOPFLOWSetLoadProfiles(tcopflow, ploadprofile_path, None) -elif flg2: - ierr = libtcopflow.TCOPFLOWSetLoadProfiles(tcopflow, None, qloadprofile_path) -if ierr: - print("Error setting load profiles.") - exit(ierr) - -# Set windgen profile -if flg3: - ierr = libtcopflow.TCOPFLOWSetWindGenProfiles(tcopflow, windgenprofile_path) -if ierr: - print("Error setting windgen profile.") - exit(ierr) - -# Solve TCOPFLOW -ierr = libtcopflow.TCOPFLOWSolve(tcopflow) -if ierr: - print("Error solving TCOPFLOW.") - exit(ierr) - -# Print solution if requested -if print_output: - ierr = libtcopflow.TCOPFLOWPrintSolution(tcopflow, 0) - if ierr: - print("Error printing TCOPFLOW solution.") - exit(ierr) - -# Save solution if requested -if save_output: - ierr = libtcopflow.TCOPFLOWS diff --git a/tcopflow1.py b/tcopflow-bind/tcopflow1.py similarity index 100% rename from tcopflow1.py rename to tcopflow-bind/tcopflow1.py diff --git a/tcopflow2.py b/tcopflow-bind/tcopflow2.py similarity index 100% rename from tcopflow2.py rename to tcopflow-bind/tcopflow2.py From b96646c36e2ec9eac01081c77f5e2a86b2f74027 Mon Sep 17 00:00:00 2001 From: cameronrutherford Date: Tue, 26 Mar 2024 18:22:33 +0000 Subject: [PATCH 04/36] Apply pre-commmit fixes --- tcopflow-bind/tcopflow1.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/tcopflow-bind/tcopflow1.py b/tcopflow-bind/tcopflow1.py index e14f3f0b..37df163e 100644 --- a/tcopflow-bind/tcopflow1.py +++ b/tcopflow-bind/tcopflow1.py @@ -1,11 +1,14 @@ import exago + def initialize(appname): exago.initialize(appname) + def finalize(): exago.finalize() + class TCOPFLOW: def __init__(self): self.tcopflow = None @@ -13,10 +16,10 @@ def __init__(self): def create(self, netfile, ploadprofile=None, qloadprofile=None, windgenprofile=None): self.tcopflow = exago.TCOPFLOW() self.tcopflow.set_network_data(netfile) - + if ploadprofile is not None or qloadprofile is not None: self.tcopflow.set_load_profiles(ploadprofile, qloadprofile) - + if windgenprofile is not None: self.tcopflow.set_wind_gen_profiles(windgenprofile) @@ -37,4 +40,4 @@ def save_solution(self, file_name): tcopflow.print_solution() tcopflow.save_solution("tcopflowout") del tcopflow -finalize() \ No newline at end of file +finalize() From 14a4f34838f7dc5bbdf3f76d05f2bc86c1f01336 Mon Sep 17 00:00:00 2001 From: Jaelyn Litzinger Date: Fri, 29 Mar 2024 11:55:18 -0700 Subject: [PATCH 05/36] add tcopflow files to python bindings --- interfaces/python/CMakeLists.txt | 4 +- interfaces/python/exago_python_tcopflow.cpp | 44 +++++++++++++++++++++ interfaces/python/exago_python_tcopflow.hpp | 9 +++++ interfaces/python/example_tcopflow.py | 0 tests/interfaces/python/CMakeLists.txt | 2 +- tests/interfaces/python/test_5_tcopflow.py | 44 +++++++++++++++++++++ 6 files changed, 100 insertions(+), 3 deletions(-) create mode 100644 interfaces/python/exago_python_tcopflow.cpp create mode 100644 interfaces/python/exago_python_tcopflow.hpp create mode 100644 interfaces/python/example_tcopflow.py create mode 100644 tests/interfaces/python/test_5_tcopflow.py diff --git a/interfaces/python/CMakeLists.txt b/interfaces/python/CMakeLists.txt index 8f410aa8..55d7d553 100644 --- a/interfaces/python/CMakeLists.txt +++ b/interfaces/python/CMakeLists.txt @@ -18,8 +18,8 @@ if(EXAGO_ENABLE_IPOPT OR EXAGO_ENABLE_HIOP) endif() if(EXAGO_ENABLE_IPOPT) - list(APPEND EXAGO_PY_SRCS exago_python_scopflow.cpp exago_python_sopflow.cpp) - list(APPEND EXAGO_PY_LIBS ExaGO::SOPFLOW ExaGO::SCOPFLOW) + list(APPEND EXAGO_PY_SRCS exago_python_scopflow.cpp exago_python_sopflow.cpp exago_python_tcopflow.cpp) + list(APPEND EXAGO_PY_LIBS ExaGO::SOPFLOW ExaGO::SCOPFLOW ExaGO::TCOPFLOW) endif() # New true python bindings diff --git a/interfaces/python/exago_python_tcopflow.cpp b/interfaces/python/exago_python_tcopflow.cpp new file mode 100644 index 00000000..892d0438 --- /dev/null +++ b/interfaces/python/exago_python_tcopflow.cpp @@ -0,0 +1,44 @@ +#include "exago_python_tcopflow.hpp" + +// ------------------------------------------------------------- +// class TCOPFLOW_wrapper +// +// Wrap to make sure allocation and destruction is handled correctly +// ------------------------------------------------------------- +class TCOPFLOW_wrapper { +public: + TCOPFLOW tcopf; + TCOPFLOW_wrapper(void) { + PetscErrorCode ierr; + MPI_Comm communicator; + ierr = ExaGOGetSelfCommunicator(&communicator); + ExaGOCheckError(ierr); + ierr = TCOPFLOWCreate(communicator, &tcopf); + ExaGOCheckError(ierr); + } + ~TCOPFLOW_wrapper(void) { + PetscErrorCode ierr; + ierr = TCOPFLOWDestroy(&tcopf); + ExaGOCheckError(ierr); + } +}; + +// ------------------------------------------------------------- +// init_exago_tcopflow +// ------------------------------------------------------------- +void init_exago_tcopflow(pybind11::module &m) { + + pybind11::class_(m, "TCOPFLOW") + .def(pybind11::init()) + + /* Setters */ + + // Example + // .def("set_model", + // [](SOPFLOW_wrapper &w, std::string model) { + // PetscErrorCode ierr; + // ierr = SOPFLOWSetModel(w.sopf, model.c_str()); + // ExaGOCheckError(ierr); + // }) + ; +} \ No newline at end of file diff --git a/interfaces/python/exago_python_tcopflow.hpp b/interfaces/python/exago_python_tcopflow.hpp new file mode 100644 index 00000000..c5eb068d --- /dev/null +++ b/interfaces/python/exago_python_tcopflow.hpp @@ -0,0 +1,9 @@ +#ifndef _exago_python_tcopflow_hpp_ +#define _exago_python_tcopflow_hpp_ + +#include "exago_python_opflow.hpp" + +#include +#include + +#endif \ No newline at end of file diff --git a/interfaces/python/example_tcopflow.py b/interfaces/python/example_tcopflow.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/interfaces/python/CMakeLists.txt b/tests/interfaces/python/CMakeLists.txt index 6c8ad4ce..09553ad8 100644 --- a/tests/interfaces/python/CMakeLists.txt +++ b/tests/interfaces/python/CMakeLists.txt @@ -22,7 +22,7 @@ endif() # The SCOPFLOW and SOPFLOW API tests are currently written only for IPOPT if(EXAGO_ENABLE_IPOPT) - string(APPEND TEST_FILES " " "test_3_scopflow.py" " " "test_4_sopflow.py") + string(APPEND TEST_FILES " " "test_3_scopflow.py" " " "test_4_sopflow.py" " " "test_5_tcopflow.py") endif() string(APPEND TEST_FILES " " "test_9_finalize.py") diff --git a/tests/interfaces/python/test_5_tcopflow.py b/tests/interfaces/python/test_5_tcopflow.py new file mode 100644 index 00000000..fc39da41 --- /dev/null +++ b/tests/interfaces/python/test_5_tcopflow.py @@ -0,0 +1,44 @@ +import tempfile +import os +import shutil +import pytest +from check_preconditions import check_preconditions +import mpi4py.rc +mpi4py.rc.threads = False +from mpi4py import MPI # noqa +import exago # noqa + +check_preconditions() + +exago_ignore = -1000000 + +@pytest.mark.nocomm +@pytest.mark.MPI +def test_creating_tcopflow(): + '''Testing creation of tcopflow object''' + tcopf = exago.OPFLOW() + + +@pytest.mark.nocomm +@pytest.mark.MPI +def test_get_prefix(): + '''Test retrieving datafile path''' + path = exago.prefix() + assert isinstance(path, str) + +''' example +@pytest.mark.nocomm +@pytest.mark.MPI +def test_set_model_and_solver(): + Testing setting model + opf = exago.OPFLOW() + opf.set_solver("HIOP") + solver = opf.get_solver() + assert solver == "HIOP" + opf.set_model("POWER_BALANCE_HIOP") + model = opf.get_model() + assert model == "POWER_BALANCE_HIOP" + path = exago.prefix() + opf.read_mat_power_data(os.path.join( + path, 'share', 'exago', 'datafiles', 'case9', 'case9mod.m')) +''' From 55836c548aac4d0ffb8313553e7bb7247cf178be Mon Sep 17 00:00:00 2001 From: Jaelyn Litzinger Date: Fri, 29 Mar 2024 11:56:05 -0700 Subject: [PATCH 06/36] rename python files from test to example --- interfaces/python/{test_opflow.py => example_opflow.py} | 0 interfaces/python/{test_pflow.py => example_pflow.py} | 0 interfaces/python/{test_scopflow.py => example_scopflow.py} | 0 interfaces/python/{test_sopflow.py => example_sopflow.py} | 0 4 files changed, 0 insertions(+), 0 deletions(-) rename interfaces/python/{test_opflow.py => example_opflow.py} (100%) rename interfaces/python/{test_pflow.py => example_pflow.py} (100%) rename interfaces/python/{test_scopflow.py => example_scopflow.py} (100%) rename interfaces/python/{test_sopflow.py => example_sopflow.py} (100%) diff --git a/interfaces/python/test_opflow.py b/interfaces/python/example_opflow.py similarity index 100% rename from interfaces/python/test_opflow.py rename to interfaces/python/example_opflow.py diff --git a/interfaces/python/test_pflow.py b/interfaces/python/example_pflow.py similarity index 100% rename from interfaces/python/test_pflow.py rename to interfaces/python/example_pflow.py diff --git a/interfaces/python/test_scopflow.py b/interfaces/python/example_scopflow.py similarity index 100% rename from interfaces/python/test_scopflow.py rename to interfaces/python/example_scopflow.py diff --git a/interfaces/python/test_sopflow.py b/interfaces/python/example_sopflow.py similarity index 100% rename from interfaces/python/test_sopflow.py rename to interfaces/python/example_sopflow.py From d08d4910d475e38f888bcffb4326742624313742 Mon Sep 17 00:00:00 2001 From: jaelynlitz Date: Fri, 29 Mar 2024 18:56:55 +0000 Subject: [PATCH 07/36] Apply pre-commmit fixes --- interfaces/python/CMakeLists.txt | 4 +++- interfaces/python/exago_python_tcopflow.cpp | 21 ++++++++++----------- interfaces/python/exago_python_tcopflow.hpp | 2 +- tests/interfaces/python/CMakeLists.txt | 11 ++++++++++- tests/interfaces/python/test_5_tcopflow.py | 2 ++ 5 files changed, 26 insertions(+), 14 deletions(-) diff --git a/interfaces/python/CMakeLists.txt b/interfaces/python/CMakeLists.txt index 55d7d553..5bbefcc9 100644 --- a/interfaces/python/CMakeLists.txt +++ b/interfaces/python/CMakeLists.txt @@ -18,7 +18,9 @@ if(EXAGO_ENABLE_IPOPT OR EXAGO_ENABLE_HIOP) endif() if(EXAGO_ENABLE_IPOPT) - list(APPEND EXAGO_PY_SRCS exago_python_scopflow.cpp exago_python_sopflow.cpp exago_python_tcopflow.cpp) + list(APPEND EXAGO_PY_SRCS exago_python_scopflow.cpp exago_python_sopflow.cpp + exago_python_tcopflow.cpp + ) list(APPEND EXAGO_PY_LIBS ExaGO::SOPFLOW ExaGO::SCOPFLOW ExaGO::TCOPFLOW) endif() diff --git a/interfaces/python/exago_python_tcopflow.cpp b/interfaces/python/exago_python_tcopflow.cpp index 892d0438..9323bb9b 100644 --- a/interfaces/python/exago_python_tcopflow.cpp +++ b/interfaces/python/exago_python_tcopflow.cpp @@ -28,17 +28,16 @@ class TCOPFLOW_wrapper { // ------------------------------------------------------------- void init_exago_tcopflow(pybind11::module &m) { - pybind11::class_(m, "TCOPFLOW") - .def(pybind11::init()) + pybind11::class_(m, "TCOPFLOW").def(pybind11::init()) /* Setters */ - // Example - // .def("set_model", - // [](SOPFLOW_wrapper &w, std::string model) { - // PetscErrorCode ierr; - // ierr = SOPFLOWSetModel(w.sopf, model.c_str()); - // ExaGOCheckError(ierr); - // }) - ; -} \ No newline at end of file + // Example + // .def("set_model", + // [](SOPFLOW_wrapper &w, std::string model) { + // PetscErrorCode ierr; + // ierr = SOPFLOWSetModel(w.sopf, model.c_str()); + // ExaGOCheckError(ierr); + // }) + ; +} diff --git a/interfaces/python/exago_python_tcopflow.hpp b/interfaces/python/exago_python_tcopflow.hpp index c5eb068d..d885e324 100644 --- a/interfaces/python/exago_python_tcopflow.hpp +++ b/interfaces/python/exago_python_tcopflow.hpp @@ -6,4 +6,4 @@ #include #include -#endif \ No newline at end of file +#endif diff --git a/tests/interfaces/python/CMakeLists.txt b/tests/interfaces/python/CMakeLists.txt index 09553ad8..18169e7a 100644 --- a/tests/interfaces/python/CMakeLists.txt +++ b/tests/interfaces/python/CMakeLists.txt @@ -22,7 +22,16 @@ endif() # The SCOPFLOW and SOPFLOW API tests are currently written only for IPOPT if(EXAGO_ENABLE_IPOPT) - string(APPEND TEST_FILES " " "test_3_scopflow.py" " " "test_4_sopflow.py" " " "test_5_tcopflow.py") + string( + APPEND + TEST_FILES + " " + "test_3_scopflow.py" + " " + "test_4_sopflow.py" + " " + "test_5_tcopflow.py" + ) endif() string(APPEND TEST_FILES " " "test_9_finalize.py") diff --git a/tests/interfaces/python/test_5_tcopflow.py b/tests/interfaces/python/test_5_tcopflow.py index fc39da41..8cbda893 100644 --- a/tests/interfaces/python/test_5_tcopflow.py +++ b/tests/interfaces/python/test_5_tcopflow.py @@ -12,6 +12,7 @@ exago_ignore = -1000000 + @pytest.mark.nocomm @pytest.mark.MPI def test_creating_tcopflow(): @@ -26,6 +27,7 @@ def test_get_prefix(): path = exago.prefix() assert isinstance(path, str) + ''' example @pytest.mark.nocomm @pytest.mark.MPI From 93973418e1ec79d2c873973d0fb7963704ad2dea Mon Sep 17 00:00:00 2001 From: Jaya Mohan Date: Tue, 16 Apr 2024 15:03:48 -0700 Subject: [PATCH 08/36] rebase on origin branch --- interfaces/python/exago_python_tcopflow.cpp | 143 +++++++++++++++++--- interfaces/python/exago_python_tcopflow.hpp | 1 - interfaces/python/test_tcopflow.py | 43 ++++++ 3 files changed, 170 insertions(+), 17 deletions(-) create mode 100644 interfaces/python/test_tcopflow.py diff --git a/interfaces/python/exago_python_tcopflow.cpp b/interfaces/python/exago_python_tcopflow.cpp index 9323bb9b..0f6b2f8e 100644 --- a/interfaces/python/exago_python_tcopflow.cpp +++ b/interfaces/python/exago_python_tcopflow.cpp @@ -1,24 +1,24 @@ #include "exago_python_tcopflow.hpp" // ------------------------------------------------------------- -// class TCOPFLOW_wrapper +// class SOPFLOW_wrapper // // Wrap to make sure allocation and destruction is handled correctly // ------------------------------------------------------------- class TCOPFLOW_wrapper { public: - TCOPFLOW tcopf; - TCOPFLOW_wrapper(void) { + TCOPFLOW sopf; + TCPFLOW_wrapper(void) { PetscErrorCode ierr; MPI_Comm communicator; ierr = ExaGOGetSelfCommunicator(&communicator); ExaGOCheckError(ierr); - ierr = TCOPFLOWCreate(communicator, &tcopf); + ierr = TCOPFLOWCreate(communicator, &sopf); ExaGOCheckError(ierr); } ~TCOPFLOW_wrapper(void) { PetscErrorCode ierr; - ierr = TCOPFLOWDestroy(&tcopf); + ierr = TCOPFLOWDestroy(&sopf); ExaGOCheckError(ierr); } }; @@ -28,16 +28,127 @@ class TCOPFLOW_wrapper { // ------------------------------------------------------------- void init_exago_tcopflow(pybind11::module &m) { - pybind11::class_(m, "TCOPFLOW").def(pybind11::init()) + pybind11::class_(m, "TCOPFLOW") + .def("set_model", + [](SOPFLOW_wrapper &w, std::string model) { + PetscErrorCode ierr; + ierr = TCOPFLOWSetModel(w.tcopf, model.c_str()); + ExaGOCheckError(ierr); - /* Setters */ + .def("set_solver", + [](TCOPFLOW_wrapper &w, std::string solver) { + PetscErrorCode ierr; + ierr = TCOPFLOWSetSolver(w.tcopf, solver.c_str()); + ExaGOCheckError(ierr); + }) + .def("set_network_data", + [](TCOPFLOW_wrapper &w, std::string filename) { + PetscErrorCode ierr; + ierr = TCOPFLOWSetNetworkData(w.tcopf, filename.c_str()); + ExaGOCheckError(ierr); + .def("setup", + [](TCOPFLOW_wrapper &w) { + PetscErrorCode ierr; + ierr = TCOPFLOWSetUp(w.tcopf); + ExaGOCheckError(ierr); + }) + .def("solve", + [](TCOPFLOW_wrapper &w) { + PetscErrorCode ierr; + ierr = TCOPFLOWSolve(w.tcopf); + ExaGOCheckError(ierr); + }) + .def("set_load_profiles", + [](TCOPFLOW_wrapper &w, const std::string &pload, + const std::string &qload) { + PetscErrorCode ierr; + ierr = + TCOPFLOWSetLoadProfiles(w.tcopf, pload.c_str(), qload.c_str()); + ExaGOCheckError(ierr); + }) + .def("set_time_step_and_duration", + [](TCOPFLOW_wrapper &w, double dt, double tmax) { + PetscErrorCode ierr; + ierr = TCOPFLOWSetTimeStepandDuration(w.tcopf, dt, tmax); + ExaGOCheckError(ierr); + }) + .def("set_wind_gen_profile", + [](TCOPFLOW_wrapper &w, std::string filename) { + PetscErrorCode ierr; + ierr = TCOPFLOWSetWindGenProfile(w.tcopf, filename.c_str()); + ExaGOCheckError(ierr); + }) + .def("get_objective", + [](TCOPFLOW_wrapper &w) -> double { + PetscErrorCode ierr; + double obj; + ierr = TCOPFLOWGetObjective(w.tcopf, &obj); + ExaGOCheckError(ierr); + return obj; + }) + .def("get_solution", + [](TCOPFLOW_wrapper &w) { + PetscErrorCode ierr; + ierr = OPFLOWGETSolution(w.tcopf); + ExaGOCheckError(ierr); + }) + .def("print_solution", + [](TCOPFLOW_wrapper &w) { + PetscErrorCode ierr; + ierr = OPFLOWPrintSolution(w.tcopf); + ExaGOCheckError(ierr); + }) + .def("save_solution", + [](TCOPFLOW_wrapper &w, OutputFormat fmt, std::string outfile) { + PetscErrorCode ierr; + ierr = OPFLOWSaveSolution(w.tcopf, fmt, outfile.c_str()); + ExaGOCheckError(ierr); + }) + .def("save_solution_all", + [](TCCOPFLOW_wrapper &w, OutputFormat fmt, std::string outdir) { + PetscErrorCode ierr; + ierr = SCOPFLOWSaveSolutionAll(w.tcopf, fmt, outdir.c_str()); + ExaGOCheckError(ierr); + }) + + /* Getters */ + .def("get_convergence_status", + [](TCOPFLOW_wrapper &w) -> bool { + PetscErrorCode ierr; + PetscBool flag; + ierr = TCCOPFLOWGetConvergenceStatus(w.tcopf, &flag); + ExaGOCheckError(ierr); + return flag; + }) + .def("get_num_iterations", + [](TCCOPFLOW_wrapper &w) -> int { + PetscErrorCode ierr; + PetscInt n; + ierr = TCOPFLOWGetNumIterations(w.tcopf, &n); + ExaGOCheckError(ierr); + return n; + }) + .def("set_tolerance", + [](TCOPFLOW_wrapper &w, double tol) { + PetscErrorCode ierr; + ierr = TCOPFLOWSetTolerance(w.tcopf, tol); + ExaGOCheckError(ierr); + }) + .def("get_tolerance", + [](TCOPFLOW_wrapper &w) -> double { + PetscErrorCode ierr; + PetscReal tol; + ierr = TCOPFLOWGetTolerance(w.tcopf, &tol); + ExaGOCheckError(ierr); + return tol; + }); + } + + + + + + + + - // Example - // .def("set_model", - // [](SOPFLOW_wrapper &w, std::string model) { - // PetscErrorCode ierr; - // ierr = SOPFLOWSetModel(w.sopf, model.c_str()); - // ExaGOCheckError(ierr); - // }) - ; -} diff --git a/interfaces/python/exago_python_tcopflow.hpp b/interfaces/python/exago_python_tcopflow.hpp index d885e324..77ae084f 100644 --- a/interfaces/python/exago_python_tcopflow.hpp +++ b/interfaces/python/exago_python_tcopflow.hpp @@ -2,7 +2,6 @@ #define _exago_python_tcopflow_hpp_ #include "exago_python_opflow.hpp" - #include #include diff --git a/interfaces/python/test_tcopflow.py b/interfaces/python/test_tcopflow.py new file mode 100644 index 00000000..61cb3bfb --- /dev/null +++ b/interfaces/python/test_tcopflow.py @@ -0,0 +1,43 @@ +import exago + + +def initialize(appname): + exago.initialize(appname) + + +def finalize(): + exago.finalize() + + +class TCOPFLOW: + def __init__(self): + self.tcopflow = None + + def create(self, netfile, ploadprofile=None, qloadprofile=None, windgenprofile=None): + self.tcopflow = exago.TCOPFLOW() + self.tcopflow.set_network_data(netfile) + + if ploadprofile is not None or qloadprofile is not None: + self.tcopflow.set_load_profiles(ploadprofile, qloadprofile) + + if windgenprofile is not None: + self.tcopflow.set_wind_gen_profiles(windgenprofile) + + def solve(self): + self.tcopflow.solve() + + def print_solution(self, index=0): + self.tcopflow.print_solution(index) + + def save_solution(self, file_name): + self.tcopflow.save_solution_all(exago.MATPOWER, file_name) + + +initialize("tcopflow") +tcopflow = TCOPFLOW() +tcopflow.create("datafiles/case9/case9mod.m") +tcopflow.solve() +tcopflow.print_solution() +tcopflow.save_solution("tcopflowout") +del tcopflow +finalize() \ No newline at end of file From f4cc2d18ddaff2ef3be07f043fdfeda92ba00478 Mon Sep 17 00:00:00 2001 From: Jaya Mohan Date: Fri, 26 Apr 2024 16:02:57 -0700 Subject: [PATCH 09/36] test_tcopflow.cpp --- interfaces/python/exago_python.cpp | 4 + interfaces/python/exago_python_tcopflow.cpp | 108 +------------------- tests/interfaces/python/test_5_tcopflow.py | 12 ++- 3 files changed, 20 insertions(+), 104 deletions(-) diff --git a/interfaces/python/exago_python.cpp b/interfaces/python/exago_python.cpp index e3cdc3c9..13905fab 100644 --- a/interfaces/python/exago_python.cpp +++ b/interfaces/python/exago_python.cpp @@ -64,5 +64,9 @@ PYBIND11_MODULE(exago, m) { extern void init_exago_sopflow(pybind11::module & m); init_exago_sopflow(m); + + extern void init_exago_tcopflow(pybind11::module & m); + init_exago_tcopflow(m); + #endif } diff --git a/interfaces/python/exago_python_tcopflow.cpp b/interfaces/python/exago_python_tcopflow.cpp index 0f6b2f8e..372f457d 100644 --- a/interfaces/python/exago_python_tcopflow.cpp +++ b/interfaces/python/exago_python_tcopflow.cpp @@ -7,18 +7,18 @@ // ------------------------------------------------------------- class TCOPFLOW_wrapper { public: - TCOPFLOW sopf; - TCPFLOW_wrapper(void) { + TCOPFLOW tcopf; + TCOPFLOW_wrapper(void) { PetscErrorCode ierr; MPI_Comm communicator; ierr = ExaGOGetSelfCommunicator(&communicator); ExaGOCheckError(ierr); - ierr = TCOPFLOWCreate(communicator, &sopf); + ierr = TCOPFLOWCreate(communicator, &tcopf); ExaGOCheckError(ierr); } ~TCOPFLOW_wrapper(void) { PetscErrorCode ierr; - ierr = TCOPFLOWDestroy(&sopf); + ierr = TCOPFLOWDestroy(&tcopf); ExaGOCheckError(ierr); } }; @@ -29,105 +29,7 @@ class TCOPFLOW_wrapper { void init_exago_tcopflow(pybind11::module &m) { pybind11::class_(m, "TCOPFLOW") - .def("set_model", - [](SOPFLOW_wrapper &w, std::string model) { - PetscErrorCode ierr; - ierr = TCOPFLOWSetModel(w.tcopf, model.c_str()); - ExaGOCheckError(ierr); - - .def("set_solver", - [](TCOPFLOW_wrapper &w, std::string solver) { - PetscErrorCode ierr; - ierr = TCOPFLOWSetSolver(w.tcopf, solver.c_str()); - ExaGOCheckError(ierr); - }) - .def("set_network_data", - [](TCOPFLOW_wrapper &w, std::string filename) { - PetscErrorCode ierr; - ierr = TCOPFLOWSetNetworkData(w.tcopf, filename.c_str()); - ExaGOCheckError(ierr); - .def("setup", - [](TCOPFLOW_wrapper &w) { - PetscErrorCode ierr; - ierr = TCOPFLOWSetUp(w.tcopf); - ExaGOCheckError(ierr); - }) - .def("solve", - [](TCOPFLOW_wrapper &w) { - PetscErrorCode ierr; - ierr = TCOPFLOWSolve(w.tcopf); - ExaGOCheckError(ierr); - }) - .def("set_load_profiles", - [](TCOPFLOW_wrapper &w, const std::string &pload, - const std::string &qload) { - PetscErrorCode ierr; - ierr = - TCOPFLOWSetLoadProfiles(w.tcopf, pload.c_str(), qload.c_str()); - ExaGOCheckError(ierr); - }) - .def("set_time_step_and_duration", - [](TCOPFLOW_wrapper &w, double dt, double tmax) { - PetscErrorCode ierr; - ierr = TCOPFLOWSetTimeStepandDuration(w.tcopf, dt, tmax); - ExaGOCheckError(ierr); - }) - .def("set_wind_gen_profile", - [](TCOPFLOW_wrapper &w, std::string filename) { - PetscErrorCode ierr; - ierr = TCOPFLOWSetWindGenProfile(w.tcopf, filename.c_str()); - ExaGOCheckError(ierr); - }) - .def("get_objective", - [](TCOPFLOW_wrapper &w) -> double { - PetscErrorCode ierr; - double obj; - ierr = TCOPFLOWGetObjective(w.tcopf, &obj); - ExaGOCheckError(ierr); - return obj; - }) - .def("get_solution", - [](TCOPFLOW_wrapper &w) { - PetscErrorCode ierr; - ierr = OPFLOWGETSolution(w.tcopf); - ExaGOCheckError(ierr); - }) - .def("print_solution", - [](TCOPFLOW_wrapper &w) { - PetscErrorCode ierr; - ierr = OPFLOWPrintSolution(w.tcopf); - ExaGOCheckError(ierr); - }) - .def("save_solution", - [](TCOPFLOW_wrapper &w, OutputFormat fmt, std::string outfile) { - PetscErrorCode ierr; - ierr = OPFLOWSaveSolution(w.tcopf, fmt, outfile.c_str()); - ExaGOCheckError(ierr); - }) - .def("save_solution_all", - [](TCCOPFLOW_wrapper &w, OutputFormat fmt, std::string outdir) { - PetscErrorCode ierr; - ierr = SCOPFLOWSaveSolutionAll(w.tcopf, fmt, outdir.c_str()); - ExaGOCheckError(ierr); - }) - - /* Getters */ - .def("get_convergence_status", - [](TCOPFLOW_wrapper &w) -> bool { - PetscErrorCode ierr; - PetscBool flag; - ierr = TCCOPFLOWGetConvergenceStatus(w.tcopf, &flag); - ExaGOCheckError(ierr); - return flag; - }) - .def("get_num_iterations", - [](TCCOPFLOW_wrapper &w) -> int { - PetscErrorCode ierr; - PetscInt n; - ierr = TCOPFLOWGetNumIterations(w.tcopf, &n); - ExaGOCheckError(ierr); - return n; - }) + .def(pybind11::init()) .def("set_tolerance", [](TCOPFLOW_wrapper &w, double tol) { PetscErrorCode ierr; diff --git a/tests/interfaces/python/test_5_tcopflow.py b/tests/interfaces/python/test_5_tcopflow.py index 8cbda893..915303c0 100644 --- a/tests/interfaces/python/test_5_tcopflow.py +++ b/tests/interfaces/python/test_5_tcopflow.py @@ -17,7 +17,7 @@ @pytest.mark.MPI def test_creating_tcopflow(): '''Testing creation of tcopflow object''' - tcopf = exago.OPFLOW() + tcopf = exago.TCOPFLOW() @pytest.mark.nocomm @@ -44,3 +44,13 @@ def test_set_model_and_solver(): opf.read_mat_power_data(os.path.join( path, 'share', 'exago', 'datafiles', 'case9', 'case9mod.m')) ''' +@pytest.mark.nocomm +@pytest.mark.MPI +def test_set_tolerance(): + '''test get/set tolerance for tcopflow''' + tcopf = exago.TCOPFLOW() + tcopf.set_tolerance(-6) + tolerance = tcopf.get_tolerance() + assert isinstance (tolerance, float) + assert tolerance == -6 + From 870ddb50414d7a8da0f3a26d579cdb8a11c7df9d Mon Sep 17 00:00:00 2001 From: Jaya Mohan Date: Mon, 29 Apr 2024 14:47:23 -0700 Subject: [PATCH 10/36] sort tcopflow functions --- interfaces/python/exago_python_tcopflow.cpp | 56 ++++++++++++++++++--- 1 file changed, 49 insertions(+), 7 deletions(-) diff --git a/interfaces/python/exago_python_tcopflow.cpp b/interfaces/python/exago_python_tcopflow.cpp index 372f457d..ba24772d 100644 --- a/interfaces/python/exago_python_tcopflow.cpp +++ b/interfaces/python/exago_python_tcopflow.cpp @@ -30,20 +30,62 @@ void init_exago_tcopflow(pybind11::module &m) { pybind11::class_(m, "TCOPFLOW") .def(pybind11::init()) - .def("set_tolerance", - [](TCOPFLOW_wrapper &w, double tol) { + .def("set_network_data", + [](TCOPFLOW_wrapper &w, std::string filename) { PetscErrorCode ierr; - ierr = TCOPFLOWSetTolerance(w.tcopf, tol); + ierr = TCOPFLOWSetNetworkData(w.tcopf, filename.c_str()); ExaGOCheckError(ierr); + .def("set_solver", + [](TCOPFLOW_wrapper &w, std::string solver) { + PetscErrorCode ierr; + ierr = TCOPFLOWSetSolver(w.tcopf, solver.c_str()); + ExaGO + }); + + .def("setup", + [](TCOPFLOW_wrapper &w) { + PetscErrorCode ierr; + ierr = TCOPFLOWSetUp(w.tcopf); + ExaGOCheckError(ierr); + }); + + .def("get_convergence_status", + [](TCOPFLOW_wrapper &w) -> bool { + PetscErrorCode ierr; + PetscBool flag; + ierr = TCOPFLOWGetConvergenceStatus(w.tcopf, &flag); + ExaGOCheckError(ierr); + return flag; }) - .def("get_tolerance", + .def("get_objective", [](TCOPFLOW_wrapper &w) -> double { PetscErrorCode ierr; - PetscReal tol; - ierr = TCOPFLOWGetTolerance(w.tcopf, &tol); + double obj; + ierr = TCOPFLOWGetObjective(w.tcopf, &obj); + ExaGOCheckError(ierr); + return obj; + }) + .def("get_num_iterations", + [](TCCOPFLOW_wrapper &w) -> int { + PetscErrorCode ierr; + PetscInt n; + ierr = TCOPFLOWGetNumIterations(w.tcopf, &n); + ExaGOCheckError(ierr); + return n; + }) + .def("save_solution", + [](TCOPFLOW_wrapper &w, OutputFormat fmt, std::string outfile) { + PetscErrorCode ierr; + ierr = TCOPFLOWSaveSolution(w.tcopf, fmt, outfile.c_str()); ExaGOCheckError(ierr); - return tol; }); + .def("save_solution_all", + [](TCCOPFLOW_wrapper &w, OutputFormat fmt, std::string outdir) { + PetscErrorCode ierr; + ierr = TCCOPFLOWSaveSolutionAll(w.tcopf, fmt, outdir.c_str()); + ExaGOCheckError(ierr); + }); + } From 392f196c8679abf6ff42e10f9511ee590914f987 Mon Sep 17 00:00:00 2001 From: Jayapreethi Date: Mon, 29 Apr 2024 23:00:48 +0000 Subject: [PATCH 11/36] Apply pre-commmit fixes --- interfaces/python/exago_python_tcopflow.cpp | 109 +++++++++----------- interfaces/python/test_tcopflow.py | 2 +- tests/interfaces/python/test_5_tcopflow.py | 5 +- 3 files changed, 53 insertions(+), 63 deletions(-) diff --git a/interfaces/python/exago_python_tcopflow.cpp b/interfaces/python/exago_python_tcopflow.cpp index ba24772d..0dd9b4a9 100644 --- a/interfaces/python/exago_python_tcopflow.cpp +++ b/interfaces/python/exago_python_tcopflow.cpp @@ -32,67 +32,56 @@ void init_exago_tcopflow(pybind11::module &m) { .def(pybind11::init()) .def("set_network_data", [](TCOPFLOW_wrapper &w, std::string filename) { - PetscErrorCode ierr; - ierr = TCOPFLOWSetNetworkData(w.tcopf, filename.c_str()); - ExaGOCheckError(ierr); - .def("set_solver", - [](TCOPFLOW_wrapper &w, std::string solver) { - PetscErrorCode ierr; - ierr = TCOPFLOWSetSolver(w.tcopf, solver.c_str()); - ExaGO - }); + PetscErrorCode ierr; + ierr = TCOPFLOWSetNetworkData(w.tcopf, filename.c_str()); + ExaGOCheckError(ierr); + .def("set_solver", [](TCOPFLOW_wrapper &w, std::string solver) { + PetscErrorCode ierr; + ierr = TCOPFLOWSetSolver(w.tcopf, solver.c_str()); + ExaGO + }); - .def("setup", - [](TCOPFLOW_wrapper &w) { - PetscErrorCode ierr; - ierr = TCOPFLOWSetUp(w.tcopf); - ExaGOCheckError(ierr); - }); + .def("setup", [](TCOPFLOW_wrapper &w) { + PetscErrorCode ierr; + ierr = TCOPFLOWSetUp(w.tcopf); + ExaGOCheckError(ierr); + }); - .def("get_convergence_status", - [](TCOPFLOW_wrapper &w) -> bool { - PetscErrorCode ierr; - PetscBool flag; - ierr = TCOPFLOWGetConvergenceStatus(w.tcopf, &flag); - ExaGOCheckError(ierr); - return flag; - }) - .def("get_objective", - [](TCOPFLOW_wrapper &w) -> double { - PetscErrorCode ierr; - double obj; - ierr = TCOPFLOWGetObjective(w.tcopf, &obj); - ExaGOCheckError(ierr); - return obj; - }) - .def("get_num_iterations", - [](TCCOPFLOW_wrapper &w) -> int { - PetscErrorCode ierr; - PetscInt n; - ierr = TCOPFLOWGetNumIterations(w.tcopf, &n); - ExaGOCheckError(ierr); - return n; - }) - .def("save_solution", - [](TCOPFLOW_wrapper &w, OutputFormat fmt, std::string outfile) { - PetscErrorCode ierr; - ierr = TCOPFLOWSaveSolution(w.tcopf, fmt, outfile.c_str()); - ExaGOCheckError(ierr); - }); - .def("save_solution_all", - [](TCCOPFLOW_wrapper &w, OutputFormat fmt, std::string outdir) { - PetscErrorCode ierr; - ierr = TCCOPFLOWSaveSolutionAll(w.tcopf, fmt, outdir.c_str()); - ExaGOCheckError(ierr); - }); + .def("get_convergence_status", + [](TCOPFLOW_wrapper &w) -> bool { + PetscErrorCode ierr; + PetscBool flag; + ierr = TCOPFLOWGetConvergenceStatus(w.tcopf, &flag); + ExaGOCheckError(ierr); + return flag; + }) + .def("get_objective", + [](TCOPFLOW_wrapper &w) -> double { + PetscErrorCode ierr; + double obj; + ierr = TCOPFLOWGetObjective(w.tcopf, &obj); + ExaGOCheckError(ierr); + return obj; + }) + .def("get_num_iterations", + [](TCCOPFLOW_wrapper &w) -> int { + PetscErrorCode ierr; + PetscInt n; + ierr = TCOPFLOWGetNumIterations(w.tcopf, &n); + ExaGOCheckError(ierr); + return n; + }) + .def("save_solution", + [](TCOPFLOW_wrapper &w, OutputFormat fmt, std::string outfile) { + PetscErrorCode ierr; + ierr = TCOPFLOWSaveSolution(w.tcopf, fmt, outfile.c_str()); + ExaGOCheckError(ierr); + }); + .def("save_solution_all", + [](TCCOPFLOW_wrapper &w, OutputFormat fmt, std::string outdir) { + PetscErrorCode ierr; + ierr = TCCOPFLOWSaveSolutionAll(w.tcopf, fmt, outdir.c_str()); + ExaGOCheckError(ierr); + }); } - - - - - - - - - diff --git a/interfaces/python/test_tcopflow.py b/interfaces/python/test_tcopflow.py index 61cb3bfb..37df163e 100644 --- a/interfaces/python/test_tcopflow.py +++ b/interfaces/python/test_tcopflow.py @@ -40,4 +40,4 @@ def save_solution(self, file_name): tcopflow.print_solution() tcopflow.save_solution("tcopflowout") del tcopflow -finalize() \ No newline at end of file +finalize() diff --git a/tests/interfaces/python/test_5_tcopflow.py b/tests/interfaces/python/test_5_tcopflow.py index 915303c0..53c5b9a3 100644 --- a/tests/interfaces/python/test_5_tcopflow.py +++ b/tests/interfaces/python/test_5_tcopflow.py @@ -44,6 +44,8 @@ def test_set_model_and_solver(): opf.read_mat_power_data(os.path.join( path, 'share', 'exago', 'datafiles', 'case9', 'case9mod.m')) ''' + + @pytest.mark.nocomm @pytest.mark.MPI def test_set_tolerance(): @@ -51,6 +53,5 @@ def test_set_tolerance(): tcopf = exago.TCOPFLOW() tcopf.set_tolerance(-6) tolerance = tcopf.get_tolerance() - assert isinstance (tolerance, float) + assert isinstance(tolerance, float) assert tolerance == -6 - From c790be8256d141b12e2d281cbeeec2959842a161 Mon Sep 17 00:00:00 2001 From: Jaya Mohan Date: Mon, 29 Apr 2024 16:22:48 -0700 Subject: [PATCH 12/36] test tcopflow functions --- tests/interfaces/python/test_5_tcopflow.py | 71 ++++++++++++++++------ 1 file changed, 51 insertions(+), 20 deletions(-) diff --git a/tests/interfaces/python/test_5_tcopflow.py b/tests/interfaces/python/test_5_tcopflow.py index 53c5b9a3..997b6840 100644 --- a/tests/interfaces/python/test_5_tcopflow.py +++ b/tests/interfaces/python/test_5_tcopflow.py @@ -12,20 +12,62 @@ exago_ignore = -1000000 - -@pytest.mark.nocomm -@pytest.mark.MPI +''' cannot run just creating tcopflow object bc opflow sub objects + won't be allocated (done in TCOPFLOWSetUp) so destructor seg faults def test_creating_tcopflow(): - '''Testing creation of tcopflow object''' tcopf = exago.TCOPFLOW() +''' + + +def run_tcopflow(solver): + tcopf = exago.TCOPFLOW() + path = exago.prefix() + + tcopf.set_tolerance(1.0E-03) + tcopf.set_network_data(os.path.join( + path, 'share', 'exago', 'datafiles', 'case9', 'case9mod_gen3_wind.m')) + tcopf.set_solver(solver) + # set_time_step_and_duration + # set_load_profiles + # set_wind_gen_profiles + + tcopf.setup() + tcopf.solve() + + assert tcopf.get_convergence_status() + + obj = tcopf.get_objective() + assert isinstance(obj, float) + n = tcopf.get_num_iterations() + assert isinstance(n, int) + # get_solution + # print_solution + + oname = os.path.join(tempfile.gettempdir(), "tcopflow_test_solution.csv") + tcopf.save_solution(0, exago.OutputFormat.CSV, oname) + assert os.path.exists(oname) + os.unlink(oname) + + oname = os.path.join(tempfile.gettempdir(), "tcopflow_test_solution.m") + tcopf.save_solution(0, exago.OutputFormat.MATPOWER, oname) + assert os.path.exists(oname) + os.unlink(oname) + + oname = os.path.join(tempfile.gettempdir(), "tcopflow_test_solution_dir") + tcopf.save_solution_all(exago.OutputFormat.MATPOWER, oname) + assert os.path.exists(oname) + shutil.rmtree(oname) + + +# From sopflow_multicontingency.toml: +# description = +# 'datafiles/case9/case9mod_gen3_wind.m IPOPT 3scen contingencies SOPFLOW' @pytest.mark.nocomm @pytest.mark.MPI -def test_get_prefix(): - '''Test retrieving datafile path''' - path = exago.prefix() - assert isinstance(path, str) +def test_tcopflow_1(): + run_tcopflow('IPOPT') ''' example @@ -43,15 +85,4 @@ def test_set_model_and_solver(): path = exago.prefix() opf.read_mat_power_data(os.path.join( path, 'share', 'exago', 'datafiles', 'case9', 'case9mod.m')) -''' - - -@pytest.mark.nocomm -@pytest.mark.MPI -def test_set_tolerance(): - '''test get/set tolerance for tcopflow''' - tcopf = exago.TCOPFLOW() - tcopf.set_tolerance(-6) - tolerance = tcopf.get_tolerance() - assert isinstance(tolerance, float) - assert tolerance == -6 +''' \ No newline at end of file From 5d3f16dcab03877a0fd80f5ab65c5f4f19dd55d4 Mon Sep 17 00:00:00 2001 From: Jaya Mohan Date: Mon, 29 Apr 2024 16:45:30 -0700 Subject: [PATCH 13/36] first passing tcopflow test --- interfaces/python/exago_python_tcopflow.cpp | 46 +++++++++++++-------- tests/interfaces/python/test_5_tcopflow.py | 4 +- 2 files changed, 31 insertions(+), 19 deletions(-) diff --git a/interfaces/python/exago_python_tcopflow.cpp b/interfaces/python/exago_python_tcopflow.cpp index 0dd9b4a9..f9245b76 100644 --- a/interfaces/python/exago_python_tcopflow.cpp +++ b/interfaces/python/exago_python_tcopflow.cpp @@ -31,21 +31,33 @@ void init_exago_tcopflow(pybind11::module &m) { pybind11::class_(m, "TCOPFLOW") .def(pybind11::init()) .def("set_network_data", - [](TCOPFLOW_wrapper &w, std::string filename) { - PetscErrorCode ierr; - ierr = TCOPFLOWSetNetworkData(w.tcopf, filename.c_str()); - ExaGOCheckError(ierr); - .def("set_solver", [](TCOPFLOW_wrapper &w, std::string solver) { - PetscErrorCode ierr; - ierr = TCOPFLOWSetSolver(w.tcopf, solver.c_str()); - ExaGO - }); - + [](TCOPFLOW_wrapper &w, std::string filename) { + PetscErrorCode ierr; + ierr = TCOPFLOWSetNetworkData(w.tcopf, filename.c_str()); + ExaGOCheckError(ierr); + }) + .def("set_solver", [](TCOPFLOW_wrapper &w, std::string solver) { + PetscErrorCode ierr; + ierr = TCOPFLOWSetSolver(w.tcopf, solver.c_str()); + ExaGOCheckError(ierr); + }) + .def("set_tolerance", + [](TCOPFLOW_wrapper &w, double tol) { + PetscErrorCode ierr; + ierr = TCOPFLOWSetTolerance(w.tcopf, tol); + ExaGOCheckError(ierr); + }) .def("setup", [](TCOPFLOW_wrapper &w) { PetscErrorCode ierr; ierr = TCOPFLOWSetUp(w.tcopf); ExaGOCheckError(ierr); - }); + }) + .def("solve", + [](TCOPFLOW_wrapper &w) { + PetscErrorCode ierr; + ierr = TCOPFLOWSolve(w.tcopf); + ExaGOCheckError(ierr); + }) .def("get_convergence_status", [](TCOPFLOW_wrapper &w) -> bool { @@ -64,24 +76,24 @@ void init_exago_tcopflow(pybind11::module &m) { return obj; }) .def("get_num_iterations", - [](TCCOPFLOW_wrapper &w) -> int { + [](TCOPFLOW_wrapper &w) -> int { PetscErrorCode ierr; PetscInt n; ierr = TCOPFLOWGetNumIterations(w.tcopf, &n); ExaGOCheckError(ierr); return n; - }) - .def("save_solution", + }); + /*.def("save_solution", [](TCOPFLOW_wrapper &w, OutputFormat fmt, std::string outfile) { PetscErrorCode ierr; ierr = TCOPFLOWSaveSolution(w.tcopf, fmt, outfile.c_str()); ExaGOCheckError(ierr); - }); + }) .def("save_solution_all", - [](TCCOPFLOW_wrapper &w, OutputFormat fmt, std::string outdir) { + [](TCOPFLOW_wrapper &w, OutputFormat fmt, std::string outdir) { PetscErrorCode ierr; ierr = TCCOPFLOWSaveSolutionAll(w.tcopf, fmt, outdir.c_str()); ExaGOCheckError(ierr); - }); + })*/ } diff --git a/tests/interfaces/python/test_5_tcopflow.py b/tests/interfaces/python/test_5_tcopflow.py index 997b6840..0366985d 100644 --- a/tests/interfaces/python/test_5_tcopflow.py +++ b/tests/interfaces/python/test_5_tcopflow.py @@ -45,7 +45,7 @@ def run_tcopflow(solver): # get_solution # print_solution - oname = os.path.join(tempfile.gettempdir(), "tcopflow_test_solution.csv") + ''' oname = os.path.join(tempfile.gettempdir(), "tcopflow_test_solution.csv") tcopf.save_solution(0, exago.OutputFormat.CSV, oname) assert os.path.exists(oname) os.unlink(oname) @@ -58,7 +58,7 @@ def run_tcopflow(solver): oname = os.path.join(tempfile.gettempdir(), "tcopflow_test_solution_dir") tcopf.save_solution_all(exago.OutputFormat.MATPOWER, oname) assert os.path.exists(oname) - shutil.rmtree(oname) + shutil.rmtree(oname)''' # From sopflow_multicontingency.toml: From 08060ca675c5311b353dc946a94a721dd2a1ee91 Mon Sep 17 00:00:00 2001 From: Jayapreethi Date: Mon, 29 Apr 2024 23:46:27 +0000 Subject: [PATCH 14/36] Apply pre-commmit fixes --- interfaces/python/exago_python_tcopflow.cpp | 114 ++++++++++---------- tests/interfaces/python/test_5_tcopflow.py | 2 +- 2 files changed, 58 insertions(+), 58 deletions(-) diff --git a/interfaces/python/exago_python_tcopflow.cpp b/interfaces/python/exago_python_tcopflow.cpp index f9245b76..7f3000dd 100644 --- a/interfaces/python/exago_python_tcopflow.cpp +++ b/interfaces/python/exago_python_tcopflow.cpp @@ -29,71 +29,71 @@ class TCOPFLOW_wrapper { void init_exago_tcopflow(pybind11::module &m) { pybind11::class_(m, "TCOPFLOW") - .def(pybind11::init()) - .def("set_network_data", - [](TCOPFLOW_wrapper &w, std::string filename) { - PetscErrorCode ierr; - ierr = TCOPFLOWSetNetworkData(w.tcopf, filename.c_str()); - ExaGOCheckError(ierr); - }) - .def("set_solver", [](TCOPFLOW_wrapper &w, std::string solver) { - PetscErrorCode ierr; - ierr = TCOPFLOWSetSolver(w.tcopf, solver.c_str()); - ExaGOCheckError(ierr); - }) - .def("set_tolerance", + .def(pybind11::init()) + .def("set_network_data", + [](TCOPFLOW_wrapper &w, std::string filename) { + PetscErrorCode ierr; + ierr = TCOPFLOWSetNetworkData(w.tcopf, filename.c_str()); + ExaGOCheckError(ierr); + }) + .def("set_solver", + [](TCOPFLOW_wrapper &w, std::string solver) { + PetscErrorCode ierr; + ierr = TCOPFLOWSetSolver(w.tcopf, solver.c_str()); + ExaGOCheckError(ierr); + }) + .def("set_tolerance", [](TCOPFLOW_wrapper &w, double tol) { PetscErrorCode ierr; ierr = TCOPFLOWSetTolerance(w.tcopf, tol); ExaGOCheckError(ierr); }) - .def("setup", [](TCOPFLOW_wrapper &w) { - PetscErrorCode ierr; - ierr = TCOPFLOWSetUp(w.tcopf); - ExaGOCheckError(ierr); - }) - .def("solve", + .def("setup", + [](TCOPFLOW_wrapper &w) { + PetscErrorCode ierr; + ierr = TCOPFLOWSetUp(w.tcopf); + ExaGOCheckError(ierr); + }) + .def("solve", [](TCOPFLOW_wrapper &w) { PetscErrorCode ierr; ierr = TCOPFLOWSolve(w.tcopf); ExaGOCheckError(ierr); }) - .def("get_convergence_status", - [](TCOPFLOW_wrapper &w) -> bool { - PetscErrorCode ierr; - PetscBool flag; - ierr = TCOPFLOWGetConvergenceStatus(w.tcopf, &flag); - ExaGOCheckError(ierr); - return flag; - }) - .def("get_objective", - [](TCOPFLOW_wrapper &w) -> double { - PetscErrorCode ierr; - double obj; - ierr = TCOPFLOWGetObjective(w.tcopf, &obj); - ExaGOCheckError(ierr); - return obj; - }) - .def("get_num_iterations", - [](TCOPFLOW_wrapper &w) -> int { - PetscErrorCode ierr; - PetscInt n; - ierr = TCOPFLOWGetNumIterations(w.tcopf, &n); - ExaGOCheckError(ierr); - return n; - }); - /*.def("save_solution", - [](TCOPFLOW_wrapper &w, OutputFormat fmt, std::string outfile) { - PetscErrorCode ierr; - ierr = TCOPFLOWSaveSolution(w.tcopf, fmt, outfile.c_str()); - ExaGOCheckError(ierr); - }) - .def("save_solution_all", - [](TCOPFLOW_wrapper &w, OutputFormat fmt, std::string outdir) { - PetscErrorCode ierr; - ierr = TCCOPFLOWSaveSolutionAll(w.tcopf, fmt, outdir.c_str()); - ExaGOCheckError(ierr); - })*/ - - } + .def("get_convergence_status", + [](TCOPFLOW_wrapper &w) -> bool { + PetscErrorCode ierr; + PetscBool flag; + ierr = TCOPFLOWGetConvergenceStatus(w.tcopf, &flag); + ExaGOCheckError(ierr); + return flag; + }) + .def("get_objective", + [](TCOPFLOW_wrapper &w) -> double { + PetscErrorCode ierr; + double obj; + ierr = TCOPFLOWGetObjective(w.tcopf, &obj); + ExaGOCheckError(ierr); + return obj; + }) + .def("get_num_iterations", [](TCOPFLOW_wrapper &w) -> int { + PetscErrorCode ierr; + PetscInt n; + ierr = TCOPFLOWGetNumIterations(w.tcopf, &n); + ExaGOCheckError(ierr); + return n; + }); + /*.def("save_solution", + [](TCOPFLOW_wrapper &w, OutputFormat fmt, std::string outfile) { + PetscErrorCode ierr; + ierr = TCOPFLOWSaveSolution(w.tcopf, fmt, outfile.c_str()); + ExaGOCheckError(ierr); + }) +.def("save_solution_all", + [](TCOPFLOW_wrapper &w, OutputFormat fmt, std::string outdir) { + PetscErrorCode ierr; + ierr = TCCOPFLOWSaveSolutionAll(w.tcopf, fmt, outdir.c_str()); + ExaGOCheckError(ierr); + })*/ +} diff --git a/tests/interfaces/python/test_5_tcopflow.py b/tests/interfaces/python/test_5_tcopflow.py index 0366985d..ac1dcec7 100644 --- a/tests/interfaces/python/test_5_tcopflow.py +++ b/tests/interfaces/python/test_5_tcopflow.py @@ -85,4 +85,4 @@ def test_set_model_and_solver(): path = exago.prefix() opf.read_mat_power_data(os.path.join( path, 'share', 'exago', 'datafiles', 'case9', 'case9mod.m')) -''' \ No newline at end of file +''' From 666b0289d854dfd890f3efa577aec4198eb45165 Mon Sep 17 00:00:00 2001 From: Jaya Mohan Date: Tue, 30 Apr 2024 12:50:12 -0700 Subject: [PATCH 15/36] clean tcopflow test file --- tests/interfaces/python/test_5_tcopflow.py | 53 ++-------------------- 1 file changed, 3 insertions(+), 50 deletions(-) diff --git a/tests/interfaces/python/test_5_tcopflow.py b/tests/interfaces/python/test_5_tcopflow.py index ac1dcec7..f80c23f5 100644 --- a/tests/interfaces/python/test_5_tcopflow.py +++ b/tests/interfaces/python/test_5_tcopflow.py @@ -5,18 +5,13 @@ from check_preconditions import check_preconditions import mpi4py.rc mpi4py.rc.threads = False -from mpi4py import MPI # noqa -import exago # noqa +from mpi4py import MPI +import exago check_preconditions() exago_ignore = -1000000 -''' cannot run just creating tcopflow object bc opflow sub objects - won't be allocated (done in TCOPFLOWSetUp) so destructor seg faults -def test_creating_tcopflow(): - tcopf = exago.TCOPFLOW() -''' def run_tcopflow(solver): @@ -27,10 +22,7 @@ def run_tcopflow(solver): tcopf.set_network_data(os.path.join( path, 'share', 'exago', 'datafiles', 'case9', 'case9mod_gen3_wind.m')) tcopf.set_solver(solver) - # set_time_step_and_duration - # set_load_profiles - # set_wind_gen_profiles - + tcopf.setup() tcopf.solve() @@ -42,47 +34,8 @@ def run_tcopflow(solver): n = tcopf.get_num_iterations() assert isinstance(n, int) - # get_solution - # print_solution - - ''' oname = os.path.join(tempfile.gettempdir(), "tcopflow_test_solution.csv") - tcopf.save_solution(0, exago.OutputFormat.CSV, oname) - assert os.path.exists(oname) - os.unlink(oname) - - oname = os.path.join(tempfile.gettempdir(), "tcopflow_test_solution.m") - tcopf.save_solution(0, exago.OutputFormat.MATPOWER, oname) - assert os.path.exists(oname) - os.unlink(oname) - - oname = os.path.join(tempfile.gettempdir(), "tcopflow_test_solution_dir") - tcopf.save_solution_all(exago.OutputFormat.MATPOWER, oname) - assert os.path.exists(oname) - shutil.rmtree(oname)''' - -# From sopflow_multicontingency.toml: -# description = -# 'datafiles/case9/case9mod_gen3_wind.m IPOPT 3scen contingencies SOPFLOW' @pytest.mark.nocomm @pytest.mark.MPI def test_tcopflow_1(): run_tcopflow('IPOPT') - - -''' example -@pytest.mark.nocomm -@pytest.mark.MPI -def test_set_model_and_solver(): - Testing setting model - opf = exago.OPFLOW() - opf.set_solver("HIOP") - solver = opf.get_solver() - assert solver == "HIOP" - opf.set_model("POWER_BALANCE_HIOP") - model = opf.get_model() - assert model == "POWER_BALANCE_HIOP" - path = exago.prefix() - opf.read_mat_power_data(os.path.join( - path, 'share', 'exago', 'datafiles', 'case9', 'case9mod.m')) -''' From f6aada797ecd655645fc091ed109477628be3e6e Mon Sep 17 00:00:00 2001 From: Jayapreethi Date: Tue, 30 Apr 2024 23:04:36 +0000 Subject: [PATCH 16/36] Apply pre-commmit fixes --- tests/interfaces/python/test_5_tcopflow.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/tests/interfaces/python/test_5_tcopflow.py b/tests/interfaces/python/test_5_tcopflow.py index f80c23f5..e83d1b57 100644 --- a/tests/interfaces/python/test_5_tcopflow.py +++ b/tests/interfaces/python/test_5_tcopflow.py @@ -1,3 +1,5 @@ +import exago +from mpi4py import MPI import tempfile import os import shutil @@ -5,15 +7,12 @@ from check_preconditions import check_preconditions import mpi4py.rc mpi4py.rc.threads = False -from mpi4py import MPI -import exago check_preconditions() exago_ignore = -1000000 - def run_tcopflow(solver): tcopf = exago.TCOPFLOW() path = exago.prefix() @@ -22,7 +21,7 @@ def run_tcopflow(solver): tcopf.set_network_data(os.path.join( path, 'share', 'exago', 'datafiles', 'case9', 'case9mod_gen3_wind.m')) tcopf.set_solver(solver) - + tcopf.setup() tcopf.solve() From 2bdb686d1f423fd6ecf907ce90846bb64c154346 Mon Sep 17 00:00:00 2001 From: Jaya Mohan Date: Tue, 30 Apr 2024 16:18:57 -0700 Subject: [PATCH 17/36] file clean up and comment added to test_tcopflow.py --- interfaces/python/exago_python_tcopflow.cpp | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-) diff --git a/interfaces/python/exago_python_tcopflow.cpp b/interfaces/python/exago_python_tcopflow.cpp index 7f3000dd..2bbdf207 100644 --- a/interfaces/python/exago_python_tcopflow.cpp +++ b/interfaces/python/exago_python_tcopflow.cpp @@ -1,7 +1,7 @@ #include "exago_python_tcopflow.hpp" // ------------------------------------------------------------- -// class SOPFLOW_wrapper +// class TCOPFLOW_wrapper // // Wrap to make sure allocation and destruction is handled correctly // ------------------------------------------------------------- @@ -84,16 +84,4 @@ void init_exago_tcopflow(pybind11::module &m) { ExaGOCheckError(ierr); return n; }); - /*.def("save_solution", - [](TCOPFLOW_wrapper &w, OutputFormat fmt, std::string outfile) { - PetscErrorCode ierr; - ierr = TCOPFLOWSaveSolution(w.tcopf, fmt, outfile.c_str()); - ExaGOCheckError(ierr); - }) -.def("save_solution_all", - [](TCOPFLOW_wrapper &w, OutputFormat fmt, std::string outdir) { - PetscErrorCode ierr; - ierr = TCCOPFLOWSaveSolutionAll(w.tcopf, fmt, outdir.c_str()); - ExaGOCheckError(ierr); - })*/ } From 9f1b8aba6d2891c2191260eedc970639ac39181a Mon Sep 17 00:00:00 2001 From: Jaya Mohan Date: Tue, 30 Apr 2024 16:21:34 -0700 Subject: [PATCH 18/36] removed tcopflow_bind files --- interfaces/python/test_tcopflow.py | 43 ------------------------------ tcopflow-bind/tcopflow1.py | 43 ------------------------------ tcopflow-bind/tcopflow2.py | 0 3 files changed, 86 deletions(-) delete mode 100644 interfaces/python/test_tcopflow.py delete mode 100644 tcopflow-bind/tcopflow1.py delete mode 100644 tcopflow-bind/tcopflow2.py diff --git a/interfaces/python/test_tcopflow.py b/interfaces/python/test_tcopflow.py deleted file mode 100644 index 37df163e..00000000 --- a/interfaces/python/test_tcopflow.py +++ /dev/null @@ -1,43 +0,0 @@ -import exago - - -def initialize(appname): - exago.initialize(appname) - - -def finalize(): - exago.finalize() - - -class TCOPFLOW: - def __init__(self): - self.tcopflow = None - - def create(self, netfile, ploadprofile=None, qloadprofile=None, windgenprofile=None): - self.tcopflow = exago.TCOPFLOW() - self.tcopflow.set_network_data(netfile) - - if ploadprofile is not None or qloadprofile is not None: - self.tcopflow.set_load_profiles(ploadprofile, qloadprofile) - - if windgenprofile is not None: - self.tcopflow.set_wind_gen_profiles(windgenprofile) - - def solve(self): - self.tcopflow.solve() - - def print_solution(self, index=0): - self.tcopflow.print_solution(index) - - def save_solution(self, file_name): - self.tcopflow.save_solution_all(exago.MATPOWER, file_name) - - -initialize("tcopflow") -tcopflow = TCOPFLOW() -tcopflow.create("datafiles/case9/case9mod.m") -tcopflow.solve() -tcopflow.print_solution() -tcopflow.save_solution("tcopflowout") -del tcopflow -finalize() diff --git a/tcopflow-bind/tcopflow1.py b/tcopflow-bind/tcopflow1.py deleted file mode 100644 index 37df163e..00000000 --- a/tcopflow-bind/tcopflow1.py +++ /dev/null @@ -1,43 +0,0 @@ -import exago - - -def initialize(appname): - exago.initialize(appname) - - -def finalize(): - exago.finalize() - - -class TCOPFLOW: - def __init__(self): - self.tcopflow = None - - def create(self, netfile, ploadprofile=None, qloadprofile=None, windgenprofile=None): - self.tcopflow = exago.TCOPFLOW() - self.tcopflow.set_network_data(netfile) - - if ploadprofile is not None or qloadprofile is not None: - self.tcopflow.set_load_profiles(ploadprofile, qloadprofile) - - if windgenprofile is not None: - self.tcopflow.set_wind_gen_profiles(windgenprofile) - - def solve(self): - self.tcopflow.solve() - - def print_solution(self, index=0): - self.tcopflow.print_solution(index) - - def save_solution(self, file_name): - self.tcopflow.save_solution_all(exago.MATPOWER, file_name) - - -initialize("tcopflow") -tcopflow = TCOPFLOW() -tcopflow.create("datafiles/case9/case9mod.m") -tcopflow.solve() -tcopflow.print_solution() -tcopflow.save_solution("tcopflowout") -del tcopflow -finalize() diff --git a/tcopflow-bind/tcopflow2.py b/tcopflow-bind/tcopflow2.py deleted file mode 100644 index e69de29b..00000000 From 8679ebf951ab53da2d3990a825a94162d268aef7 Mon Sep 17 00:00:00 2001 From: Jaya Mohan Date: Thu, 2 May 2024 17:54:56 -0700 Subject: [PATCH 19/36] add examples to example_tcopflow.pu --- interfaces/python/example_tcopflow.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/interfaces/python/example_tcopflow.py b/interfaces/python/example_tcopflow.py index e69de29b..b1618e29 100644 --- a/interfaces/python/example_tcopflow.py +++ b/interfaces/python/example_tcopflow.py @@ -0,0 +1,17 @@ +import os +import exago + +# Initialize a given application to be run +exago.initialize("tcopflow_test") +tcopf = exago.TCOPFLOW() +path = exago.prefix() +tcopf.set_network_data( + os.path.join(path, 'share', 'exago', 'datafiles', 'case9', 'case9mod.m')) +tcopf.set_solver("IPOPT") +tcopf.solve() +tcopf.print_solution(0) + +# Delete instance before finalization (try, at least) +del tcopf + +exago.finalize() \ No newline at end of file From e48546d7426538d529704f1cf9eb67777e50a115 Mon Sep 17 00:00:00 2001 From: Jayapreethi Date: Fri, 3 May 2024 00:56:57 +0000 Subject: [PATCH 20/36] Apply pre-commmit fixes --- interfaces/python/example_tcopflow.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/interfaces/python/example_tcopflow.py b/interfaces/python/example_tcopflow.py index b1618e29..5dd63185 100644 --- a/interfaces/python/example_tcopflow.py +++ b/interfaces/python/example_tcopflow.py @@ -14,4 +14,4 @@ # Delete instance before finalization (try, at least) del tcopf -exago.finalize() \ No newline at end of file +exago.finalize() From ed702327188bda67d42a2b0a8e2fb430a33a1780 Mon Sep 17 00:00:00 2001 From: Jaya Mohan Date: Tue, 7 May 2024 11:23:05 -0700 Subject: [PATCH 21/36] remove u unused value on test_5_tcopflow.py --- tests/interfaces/python/test_0_initialize.py | 4 +++- tests/interfaces/python/test_5_tcopflow.py | 5 +---- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/tests/interfaces/python/test_0_initialize.py b/tests/interfaces/python/test_0_initialize.py index 9fb49ad1..36cf9b7f 100644 --- a/tests/interfaces/python/test_0_initialize.py +++ b/tests/interfaces/python/test_0_initialize.py @@ -1,10 +1,12 @@ # test initializing exago only once import os import pytest -from check_preconditions import check_preconditions import mpi4py.rc mpi4py.rc.threads = False from mpi4py import MPI # noqa +from check_preconditions import check_preconditions +check_preconditions() +import exago import exago # noqa diff --git a/tests/interfaces/python/test_5_tcopflow.py b/tests/interfaces/python/test_5_tcopflow.py index e83d1b57..37f6ee01 100644 --- a/tests/interfaces/python/test_5_tcopflow.py +++ b/tests/interfaces/python/test_5_tcopflow.py @@ -10,9 +10,6 @@ check_preconditions() -exago_ignore = -1000000 - - def run_tcopflow(solver): tcopf = exago.TCOPFLOW() path = exago.prefix() @@ -24,7 +21,7 @@ def run_tcopflow(solver): tcopf.setup() tcopf.solve() - + assert tcopf.get_convergence_status() obj = tcopf.get_objective() From b24dbd93b0be11b145d2c1401b1248102664ff63 Mon Sep 17 00:00:00 2001 From: Jayapreethi Date: Tue, 7 May 2024 18:25:30 +0000 Subject: [PATCH 22/36] Apply pre-commmit fixes --- tests/interfaces/python/test_0_initialize.py | 4 ++-- tests/interfaces/python/test_5_tcopflow.py | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/tests/interfaces/python/test_0_initialize.py b/tests/interfaces/python/test_0_initialize.py index 36cf9b7f..2ac9ef38 100644 --- a/tests/interfaces/python/test_0_initialize.py +++ b/tests/interfaces/python/test_0_initialize.py @@ -1,12 +1,12 @@ # test initializing exago only once +import exago +from check_preconditions import check_preconditions import os import pytest import mpi4py.rc mpi4py.rc.threads = False from mpi4py import MPI # noqa -from check_preconditions import check_preconditions check_preconditions() -import exago import exago # noqa diff --git a/tests/interfaces/python/test_5_tcopflow.py b/tests/interfaces/python/test_5_tcopflow.py index 37f6ee01..f73d030f 100644 --- a/tests/interfaces/python/test_5_tcopflow.py +++ b/tests/interfaces/python/test_5_tcopflow.py @@ -10,6 +10,7 @@ check_preconditions() + def run_tcopflow(solver): tcopf = exago.TCOPFLOW() path = exago.prefix() @@ -21,7 +22,7 @@ def run_tcopflow(solver): tcopf.setup() tcopf.solve() - + assert tcopf.get_convergence_status() obj = tcopf.get_objective() From 6809d6d2c0768a34affbd2340d29567eff062d14 Mon Sep 17 00:00:00 2001 From: Jaya Mohan Date: Wed, 8 May 2024 09:37:40 -0700 Subject: [PATCH 23/36] checked preconditions before importing exago on test_5_tcopflow.py --- tests/interfaces/python/test_5_tcopflow.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/interfaces/python/test_5_tcopflow.py b/tests/interfaces/python/test_5_tcopflow.py index f73d030f..0749c521 100644 --- a/tests/interfaces/python/test_5_tcopflow.py +++ b/tests/interfaces/python/test_5_tcopflow.py @@ -1,10 +1,10 @@ -import exago from mpi4py import MPI import tempfile import os import shutil import pytest from check_preconditions import check_preconditions +import exago import mpi4py.rc mpi4py.rc.threads = False From 3f31ecd9027619ef63f0bbca56301474719f3d69 Mon Sep 17 00:00:00 2001 From: Jaya Mohan Date: Thu, 9 May 2024 11:22:17 -0700 Subject: [PATCH 24/36] check preconditions for all tests --- tests/interfaces/python/test_0_initialize.py | 3 +-- tests/interfaces/python/test_1_pflow.py | 6 ++++-- tests/interfaces/python/test_2_opflow.py | 8 ++++---- tests/interfaces/python/test_3_scopflow.py | 5 +++-- tests/interfaces/python/test_4_sopflow.py | 6 ++++-- tests/interfaces/python/test_5_tcopflow.py | 5 +++-- tests/interfaces/python/test_9_finalize.py | 4 +++- 7 files changed, 22 insertions(+), 15 deletions(-) diff --git a/tests/interfaces/python/test_0_initialize.py b/tests/interfaces/python/test_0_initialize.py index 2ac9ef38..bc3c4692 100644 --- a/tests/interfaces/python/test_0_initialize.py +++ b/tests/interfaces/python/test_0_initialize.py @@ -1,11 +1,10 @@ # test initializing exago only once -import exago -from check_preconditions import check_preconditions import os import pytest import mpi4py.rc mpi4py.rc.threads = False from mpi4py import MPI # noqa +from check_preconditions import check_preconditions check_preconditions() import exago # noqa diff --git a/tests/interfaces/python/test_1_pflow.py b/tests/interfaces/python/test_1_pflow.py index 829e1e82..22520243 100644 --- a/tests/interfaces/python/test_1_pflow.py +++ b/tests/interfaces/python/test_1_pflow.py @@ -1,10 +1,12 @@ import os import pytest -from check_preconditions import check_preconditions import mpi4py.rc mpi4py.rc.threads = False from mpi4py import MPI # noqa -import exago # noqa +from check_preconditions import check_preconditions +check_preconditions() +import exago # noqa + check_preconditions() diff --git a/tests/interfaces/python/test_2_opflow.py b/tests/interfaces/python/test_2_opflow.py index 1c2ab9ae..fc7974e2 100644 --- a/tests/interfaces/python/test_2_opflow.py +++ b/tests/interfaces/python/test_2_opflow.py @@ -1,10 +1,10 @@ import os import pytest -from check_preconditions import check_preconditions -import mpi4py.rc -mpi4py.rc.threads = False from mpi4py import MPI # noqa -import exago # noqa +mpi4py.rc.threads = False +from check_preconditions import check_preconditions +check_preconditions() +import exago # noqa check_preconditions() diff --git a/tests/interfaces/python/test_3_scopflow.py b/tests/interfaces/python/test_3_scopflow.py index cbf3811c..4cc54875 100644 --- a/tests/interfaces/python/test_3_scopflow.py +++ b/tests/interfaces/python/test_3_scopflow.py @@ -2,11 +2,12 @@ import os import shutil import pytest -from check_preconditions import check_preconditions import mpi4py.rc mpi4py.rc.threads = False from mpi4py import MPI # noqa -import exago # noqa +from check_preconditions import check_preconditions +check_preconditions() +import exago # noqa check_preconditions() diff --git a/tests/interfaces/python/test_4_sopflow.py b/tests/interfaces/python/test_4_sopflow.py index adea974d..43fe225e 100644 --- a/tests/interfaces/python/test_4_sopflow.py +++ b/tests/interfaces/python/test_4_sopflow.py @@ -2,11 +2,13 @@ import os import shutil import pytest -from check_preconditions import check_preconditions import mpi4py.rc mpi4py.rc.threads = False from mpi4py import MPI # noqa -import exago # noqa +from check_preconditions import check_preconditions +check_preconditions() +import exago # noqa + check_preconditions() diff --git a/tests/interfaces/python/test_5_tcopflow.py b/tests/interfaces/python/test_5_tcopflow.py index 0749c521..395c2714 100644 --- a/tests/interfaces/python/test_5_tcopflow.py +++ b/tests/interfaces/python/test_5_tcopflow.py @@ -3,10 +3,11 @@ import os import shutil import pytest -from check_preconditions import check_preconditions -import exago import mpi4py.rc mpi4py.rc.threads = False +from check_preconditions import check_preconditions +check_preconditions() +import exago check_preconditions() diff --git a/tests/interfaces/python/test_9_finalize.py b/tests/interfaces/python/test_9_finalize.py index 0fd4229f..de0213c6 100644 --- a/tests/interfaces/python/test_9_finalize.py +++ b/tests/interfaces/python/test_9_finalize.py @@ -1,7 +1,9 @@ # test only run once things here import pytest -import exago from check_preconditions import check_preconditions +check_preconditions() +import exago + check_preconditions() From 307458e316357a8fe555d1e77b9c5c84bd112b20 Mon Sep 17 00:00:00 2001 From: Jayapreethi Date: Thu, 9 May 2024 18:23:06 +0000 Subject: [PATCH 25/36] Apply pre-commmit fixes --- tests/interfaces/python/test_0_initialize.py | 2 +- tests/interfaces/python/test_1_pflow.py | 4 ++-- tests/interfaces/python/test_2_opflow.py | 4 ++-- tests/interfaces/python/test_3_scopflow.py | 4 ++-- tests/interfaces/python/test_4_sopflow.py | 4 ++-- tests/interfaces/python/test_5_tcopflow.py | 4 ++-- tests/interfaces/python/test_9_finalize.py | 2 +- 7 files changed, 12 insertions(+), 12 deletions(-) diff --git a/tests/interfaces/python/test_0_initialize.py b/tests/interfaces/python/test_0_initialize.py index bc3c4692..322116e3 100644 --- a/tests/interfaces/python/test_0_initialize.py +++ b/tests/interfaces/python/test_0_initialize.py @@ -1,10 +1,10 @@ # test initializing exago only once +from check_preconditions import check_preconditions import os import pytest import mpi4py.rc mpi4py.rc.threads = False from mpi4py import MPI # noqa -from check_preconditions import check_preconditions check_preconditions() import exago # noqa diff --git a/tests/interfaces/python/test_1_pflow.py b/tests/interfaces/python/test_1_pflow.py index 22520243..70168fec 100644 --- a/tests/interfaces/python/test_1_pflow.py +++ b/tests/interfaces/python/test_1_pflow.py @@ -1,11 +1,11 @@ +from check_preconditions import check_preconditions import os import pytest import mpi4py.rc mpi4py.rc.threads = False from mpi4py import MPI # noqa -from check_preconditions import check_preconditions check_preconditions() -import exago # noqa +import exago # noqa check_preconditions() diff --git a/tests/interfaces/python/test_2_opflow.py b/tests/interfaces/python/test_2_opflow.py index fc7974e2..c7fab17d 100644 --- a/tests/interfaces/python/test_2_opflow.py +++ b/tests/interfaces/python/test_2_opflow.py @@ -1,10 +1,10 @@ +from check_preconditions import check_preconditions import os import pytest from mpi4py import MPI # noqa mpi4py.rc.threads = False -from check_preconditions import check_preconditions check_preconditions() -import exago # noqa +import exago # noqa check_preconditions() diff --git a/tests/interfaces/python/test_3_scopflow.py b/tests/interfaces/python/test_3_scopflow.py index 4cc54875..9c851ce5 100644 --- a/tests/interfaces/python/test_3_scopflow.py +++ b/tests/interfaces/python/test_3_scopflow.py @@ -1,3 +1,4 @@ +from check_preconditions import check_preconditions import tempfile import os import shutil @@ -5,9 +6,8 @@ import mpi4py.rc mpi4py.rc.threads = False from mpi4py import MPI # noqa -from check_preconditions import check_preconditions check_preconditions() -import exago # noqa +import exago # noqa check_preconditions() diff --git a/tests/interfaces/python/test_4_sopflow.py b/tests/interfaces/python/test_4_sopflow.py index 43fe225e..b9a2a100 100644 --- a/tests/interfaces/python/test_4_sopflow.py +++ b/tests/interfaces/python/test_4_sopflow.py @@ -1,3 +1,4 @@ +from check_preconditions import check_preconditions import tempfile import os import shutil @@ -5,9 +6,8 @@ import mpi4py.rc mpi4py.rc.threads = False from mpi4py import MPI # noqa -from check_preconditions import check_preconditions check_preconditions() -import exago # noqa +import exago # noqa check_preconditions() diff --git a/tests/interfaces/python/test_5_tcopflow.py b/tests/interfaces/python/test_5_tcopflow.py index 395c2714..8ba5da1a 100644 --- a/tests/interfaces/python/test_5_tcopflow.py +++ b/tests/interfaces/python/test_5_tcopflow.py @@ -1,3 +1,5 @@ +import exago +from check_preconditions import check_preconditions from mpi4py import MPI import tempfile import os @@ -5,9 +7,7 @@ import pytest import mpi4py.rc mpi4py.rc.threads = False -from check_preconditions import check_preconditions check_preconditions() -import exago check_preconditions() diff --git a/tests/interfaces/python/test_9_finalize.py b/tests/interfaces/python/test_9_finalize.py index de0213c6..427b52de 100644 --- a/tests/interfaces/python/test_9_finalize.py +++ b/tests/interfaces/python/test_9_finalize.py @@ -1,8 +1,8 @@ # test only run once things here +import exago import pytest from check_preconditions import check_preconditions check_preconditions() -import exago check_preconditions() From ac9af8468e654fc7d938b698a9bd0054f5ae5a07 Mon Sep 17 00:00:00 2001 From: Jaya Mohan Date: Tue, 14 May 2024 10:23:10 -0700 Subject: [PATCH 26/36] documented tcopflow function on python_bindings.md file and tested the functions --- docs/python_bindings.md | 16 ++++++++++++++++ tests/interfaces/python/test_2_opflow.py | 2 +- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/docs/python_bindings.md b/docs/python_bindings.md index 054b0fab..820ab2c8 100644 --- a/docs/python_bindings.md +++ b/docs/python_bindings.md @@ -267,6 +267,22 @@ The following table assumes `sopflow = exago.SOPFLOW()`. | ` SOPFLOWSaveSolution` | `save_solution` | | | ` SOPFLOWSaveSolutionAll` | `save_solution_all` | | +#### TCOPFLOW + +The following table assumes `tcopflow = exago.TCOPFLOW()`. + +| C++ API | Python API | Notes | +|---|---|---| +| `TCCOPFLOW` | `exago.TCCOPFLOW` class | | +| ` TCOPFLOWSetNetworkData` | `set_network_data` | | +| ` TCOPFLOWSetSolver` | `set_solver` | | +| ` TCOPFLOWSetTolerance` | `set_tolerance` | | +| ` TCOPFLOWSetup` | `setup` | | +| ` TCOPFLOWSolve` | `solve` | | +| ` TCOPFLOWGetConvergenceStatus` | `get_convergence_status` | | +| ` TCOPFLOWGetObjective` | `get_objective` | | +| ` TCOPFLOWGetNumIterations` | `get_num_iterations` | | + ### Enums `OutputFormat` is the enum that specifies the output format in functions like `opflow.save_solution`. diff --git a/tests/interfaces/python/test_2_opflow.py b/tests/interfaces/python/test_2_opflow.py index c7fab17d..af6ef030 100644 --- a/tests/interfaces/python/test_2_opflow.py +++ b/tests/interfaces/python/test_2_opflow.py @@ -1,7 +1,7 @@ from check_preconditions import check_preconditions import os import pytest -from mpi4py import MPI # noqa +import mpi4py.rc mpi4py.rc.threads = False check_preconditions() import exago # noqa From 60ecf8f608806082d14f7e4c571f367c47a29330 Mon Sep 17 00:00:00 2001 From: Jaya Mohan Date: Thu, 16 May 2024 12:08:59 -0700 Subject: [PATCH 27/36] Added functions to exago_python_tcopflow.cpp and updated the documentation --- docs/python_bindings.md | 14 ++++- interfaces/python/exago_python_tcopflow.cpp | 58 ++++++++++++++++++++- tests/interfaces/python/test_3_scopflow.py | 1 - tests/interfaces/python/test_4_sopflow.py | 2 - tests/interfaces/python/test_5_tcopflow.py | 7 +-- tests/interfaces/python/test_9_finalize.py | 2 - 6 files changed, 70 insertions(+), 14 deletions(-) diff --git a/docs/python_bindings.md b/docs/python_bindings.md index 820ab2c8..543dc7ef 100644 --- a/docs/python_bindings.md +++ b/docs/python_bindings.md @@ -178,7 +178,7 @@ The following table assumes `opflow = exago.OPFLOW()`. | `OPFLOWReadMatPowerData` | `opflow.read_mat_power_data` | | | `OPFLOWSolutionToPS` | `opflow.solution_to_ps` | | | `OPFLOWSetUpPS` | `opflow.set_up_ps` | | -| `OPFLOWSkipOptions` | `opflow.skip_options | | +| `OPFLOWSkipOptions` | `opflow.skip_options` | | | `OPFLOWSetLinesMonitored` | | Implemented as two different methods: | | | |`opflow.set_lines_monitored([...])` | Specify a list of line kvlevels (type `float`) to monitor | | |`opflow.set_lines_monitored(n, "file")` | Read `n` line kvlevels from a file (`n=-1` for all). | @@ -273,15 +273,25 @@ The following table assumes `tcopflow = exago.TCOPFLOW()`. | C++ API | Python API | Notes | |---|---|---| -| `TCCOPFLOW` | `exago.TCCOPFLOW` class | | +| `TCOPFLOW` | `exago.TCOPFLOW` class | | +| ` TCOPFLOWSetModel` | `set_model` | | | ` TCOPFLOWSetNetworkData` | `set_network_data` | | | ` TCOPFLOWSetSolver` | `set_solver` | | | ` TCOPFLOWSetTolerance` | `set_tolerance` | | +| ` TCOPFLOWSetTimeStepandDuration` | `set_timestep_and_duration` | | +| ` TCOPFLOWSetLoadProfiles` | `set_load_profiles` | | +| ` TCOPFLOWSetWindGenProfiles` | `wind_gen_profiles` | | | ` TCOPFLOWSetup` | `setup` | | | ` TCOPFLOWSolve` | `solve` | | | ` TCOPFLOWGetConvergenceStatus` | `get_convergence_status` | | | ` TCOPFLOWGetObjective` | `get_objective` | | | ` TCOPFLOWGetNumIterations` | `get_num_iterations` | | +| ` TCOPFLOWSetTolerance` | `set_tolerance` | | +| ` TCOPFLOWGetTolerance` | `get_tolerance` | | +| ` TCOPFLOWPrintSolution` | `print_solution` | | +| ` TCOPFLOWSaveSolution` | `save_solution` | | +| ` TCOPFLOWSavesolutionAll` | `save_solution_all` || + ### Enums diff --git a/interfaces/python/exago_python_tcopflow.cpp b/interfaces/python/exago_python_tcopflow.cpp index 2bbdf207..c8dd672d 100644 --- a/interfaces/python/exago_python_tcopflow.cpp +++ b/interfaces/python/exago_python_tcopflow.cpp @@ -54,13 +54,39 @@ void init_exago_tcopflow(pybind11::module &m) { ierr = TCOPFLOWSetUp(w.tcopf); ExaGOCheckError(ierr); }) + .def("set_model", + [](TCOPFLOW_wrapper &w, std::string model) { + PetscErrorCode ierr; + ierr = TCOPFLOWSetModel(w.tcopf, model.c_str()); + ExaGOCheckError(ierr); + }) + .def("solve", [](TCOPFLOW_wrapper &w) { PetscErrorCode ierr; ierr = TCOPFLOWSolve(w.tcopf); ExaGOCheckError(ierr); }) - + .def("set_time_step_and_duration", + [](TCOPFLOW_wrapper &w, double dt, double tmax) { + PetscErrorCode ierr; + ierr = TCOPFLOWSetTimeStepandDuration(w.tcopf, dt, tmax); + ExaGOCheckError(ierr); + }) + .def("set_load_profiles", + [](TCOPFLOW_wrapper &w, const std::string &pload, + const std::string &qload) { + PetscErrorCode ierr; + ierr = + TCOPFLOWSetLoadProfiles(w.tcopf, pload.c_str(), qload.c_str()); + ExaGOCheckError(ierr); + }) + .def("set_wind_gen_profiles", + [](TCOPFLOW_wrapper &w, std::string filename) { + PetscErrorCode ierr; + ierr = TCOPFLOWSetWindGenProfiles(w.tcopf, filename.c_str()); + ExaGOCheckError(ierr); + }) .def("get_convergence_status", [](TCOPFLOW_wrapper &w) -> bool { PetscErrorCode ierr; @@ -83,5 +109,33 @@ void init_exago_tcopflow(pybind11::module &m) { ierr = TCOPFLOWGetNumIterations(w.tcopf, &n); ExaGOCheckError(ierr); return n; - }); + }) + .def("get_tolerance", + [](TCOPFLOW_wrapper &w) -> double { + PetscErrorCode ierr; + double tol; + ierr = TCOPFLOWGetTolerance(w.tcopf, &tol); + ExaGOCheckError(ierr); + return tol; + }) + .def("print_solution", + [](TCOPFLOW_wrapper &w, int isol) { + PetscErrorCode ierr; + ierr = TCOPFLOWPrintSolution(w.tcopf, isol); + ExaGOCheckError(ierr); + }) + .def( + "save_solution", + [](TCOPFLOW_wrapper &w, int n, OutputFormat fmt, std::string outfile) { + PetscErrorCode ierr; + ierr = TCOPFLOWSaveSolution(w.tcopf, n, fmt, outfile.c_str()); + ExaGOCheckError(ierr); + }) + .def("save_solution_all", + [](TCOPFLOW_wrapper &w, OutputFormat fmt, std::string outdir) { + PetscErrorCode ierr; + ierr = TCOPFLOWSaveSolutionAll(w.tcopf, fmt, outdir.c_str()); + ExaGOCheckError(ierr); + }); + } diff --git a/tests/interfaces/python/test_3_scopflow.py b/tests/interfaces/python/test_3_scopflow.py index 9c851ce5..6484e863 100644 --- a/tests/interfaces/python/test_3_scopflow.py +++ b/tests/interfaces/python/test_3_scopflow.py @@ -9,7 +9,6 @@ check_preconditions() import exago # noqa -check_preconditions() exago_ignore = -1000000 diff --git a/tests/interfaces/python/test_4_sopflow.py b/tests/interfaces/python/test_4_sopflow.py index b9a2a100..a5d7c0ef 100644 --- a/tests/interfaces/python/test_4_sopflow.py +++ b/tests/interfaces/python/test_4_sopflow.py @@ -10,8 +10,6 @@ import exago # noqa -check_preconditions() - exago_ignore = -1000000 # SCOPFLOW has serious destructor problems, so it can't do this diff --git a/tests/interfaces/python/test_5_tcopflow.py b/tests/interfaces/python/test_5_tcopflow.py index 8ba5da1a..97600dcc 100644 --- a/tests/interfaces/python/test_5_tcopflow.py +++ b/tests/interfaces/python/test_5_tcopflow.py @@ -1,16 +1,13 @@ -import exago from check_preconditions import check_preconditions -from mpi4py import MPI import tempfile import os import shutil import pytest import mpi4py.rc mpi4py.rc.threads = False +from mpi4py import MPI # noqa check_preconditions() - -check_preconditions() - +import exago # noqa def run_tcopflow(solver): tcopf = exago.TCOPFLOW() diff --git a/tests/interfaces/python/test_9_finalize.py b/tests/interfaces/python/test_9_finalize.py index 427b52de..4f295c90 100644 --- a/tests/interfaces/python/test_9_finalize.py +++ b/tests/interfaces/python/test_9_finalize.py @@ -5,8 +5,6 @@ check_preconditions() -check_preconditions() - @pytest.mark.nocomm @pytest.mark.MPI From 0296d1b6c228c24ff17668c8685b099ad50d91a4 Mon Sep 17 00:00:00 2001 From: Jayapreethi Date: Thu, 16 May 2024 19:09:37 +0000 Subject: [PATCH 28/36] Apply pre-commmit fixes --- interfaces/python/exago_python_tcopflow.cpp | 38 ++++++++++----------- tests/interfaces/python/test_5_tcopflow.py | 1 + tests/interfaces/python/test_9_finalize.py | 1 - 3 files changed, 20 insertions(+), 20 deletions(-) diff --git a/interfaces/python/exago_python_tcopflow.cpp b/interfaces/python/exago_python_tcopflow.cpp index c8dd672d..886589cd 100644 --- a/interfaces/python/exago_python_tcopflow.cpp +++ b/interfaces/python/exago_python_tcopflow.cpp @@ -59,8 +59,8 @@ void init_exago_tcopflow(pybind11::module &m) { PetscErrorCode ierr; ierr = TCOPFLOWSetModel(w.tcopf, model.c_str()); ExaGOCheckError(ierr); - }) - + }) + .def("solve", [](TCOPFLOW_wrapper &w) { PetscErrorCode ierr; @@ -103,14 +103,15 @@ void init_exago_tcopflow(pybind11::module &m) { ExaGOCheckError(ierr); return obj; }) - .def("get_num_iterations", [](TCOPFLOW_wrapper &w) -> int { - PetscErrorCode ierr; - PetscInt n; - ierr = TCOPFLOWGetNumIterations(w.tcopf, &n); - ExaGOCheckError(ierr); - return n; - }) - .def("get_tolerance", + .def("get_num_iterations", + [](TCOPFLOW_wrapper &w) -> int { + PetscErrorCode ierr; + PetscInt n; + ierr = TCOPFLOWGetNumIterations(w.tcopf, &n); + ExaGOCheckError(ierr); + return n; + }) + .def("get_tolerance", [](TCOPFLOW_wrapper &w) -> double { PetscErrorCode ierr; double tol; @@ -118,24 +119,23 @@ void init_exago_tcopflow(pybind11::module &m) { ExaGOCheckError(ierr); return tol; }) - .def("print_solution", + .def("print_solution", [](TCOPFLOW_wrapper &w, int isol) { PetscErrorCode ierr; ierr = TCOPFLOWPrintSolution(w.tcopf, isol); ExaGOCheckError(ierr); }) - .def( - "save_solution", - [](TCOPFLOW_wrapper &w, int n, OutputFormat fmt, std::string outfile) { - PetscErrorCode ierr; - ierr = TCOPFLOWSaveSolution(w.tcopf, n, fmt, outfile.c_str()); - ExaGOCheckError(ierr); - }) + .def("save_solution", + [](TCOPFLOW_wrapper &w, int n, OutputFormat fmt, + std::string outfile) { + PetscErrorCode ierr; + ierr = TCOPFLOWSaveSolution(w.tcopf, n, fmt, outfile.c_str()); + ExaGOCheckError(ierr); + }) .def("save_solution_all", [](TCOPFLOW_wrapper &w, OutputFormat fmt, std::string outdir) { PetscErrorCode ierr; ierr = TCOPFLOWSaveSolutionAll(w.tcopf, fmt, outdir.c_str()); ExaGOCheckError(ierr); }); - } diff --git a/tests/interfaces/python/test_5_tcopflow.py b/tests/interfaces/python/test_5_tcopflow.py index 97600dcc..d46ec2d6 100644 --- a/tests/interfaces/python/test_5_tcopflow.py +++ b/tests/interfaces/python/test_5_tcopflow.py @@ -9,6 +9,7 @@ check_preconditions() import exago # noqa + def run_tcopflow(solver): tcopf = exago.TCOPFLOW() path = exago.prefix() diff --git a/tests/interfaces/python/test_9_finalize.py b/tests/interfaces/python/test_9_finalize.py index 4f295c90..e97cd7ff 100644 --- a/tests/interfaces/python/test_9_finalize.py +++ b/tests/interfaces/python/test_9_finalize.py @@ -5,7 +5,6 @@ check_preconditions() - @pytest.mark.nocomm @pytest.mark.MPI def test_finalize(): From 7bbf504c8751aa1cad8b7c3f31aaaf69a742ad81 Mon Sep 17 00:00:00 2001 From: Jaya Mohan Date: Thu, 16 May 2024 13:29:53 -0700 Subject: [PATCH 29/36] checked preconditions() before importing exago --- tests/interfaces/python/test_9_finalize.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tests/interfaces/python/test_9_finalize.py b/tests/interfaces/python/test_9_finalize.py index e97cd7ff..e270dbf8 100644 --- a/tests/interfaces/python/test_9_finalize.py +++ b/tests/interfaces/python/test_9_finalize.py @@ -1,12 +1,11 @@ # test only run once things here -import exago import pytest from check_preconditions import check_preconditions check_preconditions() - +import exago @pytest.mark.nocomm @pytest.mark.MPI def test_finalize(): '''Test calling ExaGOFinalize''' - exago.finalize() + exago.finalize() \ No newline at end of file From 0ec093d4b14a475e7916909fb27deedb26680b6d Mon Sep 17 00:00:00 2001 From: Jaya Mohan Date: Thu, 16 May 2024 18:42:55 -0700 Subject: [PATCH 30/36] fix pre-commit checks --- tests/interfaces/python/test_9_finalize.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/interfaces/python/test_9_finalize.py b/tests/interfaces/python/test_9_finalize.py index e270dbf8..d206f500 100644 --- a/tests/interfaces/python/test_9_finalize.py +++ b/tests/interfaces/python/test_9_finalize.py @@ -1,11 +1,11 @@ # test only run once things here +import exago import pytest from check_preconditions import check_preconditions check_preconditions() -import exago @pytest.mark.nocomm @pytest.mark.MPI def test_finalize(): '''Test calling ExaGOFinalize''' - exago.finalize() \ No newline at end of file + exago.finalize() From a96f99d888a5fe71ad86367e18e2fc0497fd3dc1 Mon Sep 17 00:00:00 2001 From: Jayapreethi Date: Fri, 17 May 2024 01:44:21 +0000 Subject: [PATCH 31/36] Apply pre-commmit fixes --- tests/interfaces/python/test_9_finalize.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/interfaces/python/test_9_finalize.py b/tests/interfaces/python/test_9_finalize.py index d206f500..e97cd7ff 100644 --- a/tests/interfaces/python/test_9_finalize.py +++ b/tests/interfaces/python/test_9_finalize.py @@ -4,6 +4,7 @@ from check_preconditions import check_preconditions check_preconditions() + @pytest.mark.nocomm @pytest.mark.MPI def test_finalize(): From 63506a9ef69d74d0c97f8fb340def748719e35d3 Mon Sep 17 00:00:00 2001 From: Jaya Mohan Date: Fri, 17 May 2024 09:32:43 -0700 Subject: [PATCH 32/36] check preconditions and import exago --- tests/interfaces/python/test_1_pflow.py | 3 --- tests/interfaces/python/test_2_opflow.py | 2 -- tests/interfaces/python/test_9_finalize.py | 2 +- 3 files changed, 1 insertion(+), 6 deletions(-) diff --git a/tests/interfaces/python/test_1_pflow.py b/tests/interfaces/python/test_1_pflow.py index 70168fec..513d2232 100644 --- a/tests/interfaces/python/test_1_pflow.py +++ b/tests/interfaces/python/test_1_pflow.py @@ -8,9 +8,6 @@ import exago # noqa -check_preconditions() - - @pytest.mark.nocomm @pytest.mark.MPI def test_get_datafile_path(): diff --git a/tests/interfaces/python/test_2_opflow.py b/tests/interfaces/python/test_2_opflow.py index af6ef030..acb443a5 100644 --- a/tests/interfaces/python/test_2_opflow.py +++ b/tests/interfaces/python/test_2_opflow.py @@ -6,8 +6,6 @@ check_preconditions() import exago # noqa -check_preconditions() - exago_ignore = -1000000 diff --git a/tests/interfaces/python/test_9_finalize.py b/tests/interfaces/python/test_9_finalize.py index e97cd7ff..2933b5dd 100644 --- a/tests/interfaces/python/test_9_finalize.py +++ b/tests/interfaces/python/test_9_finalize.py @@ -1,8 +1,8 @@ # test only run once things here -import exago import pytest from check_preconditions import check_preconditions check_preconditions() +import exago @pytest.mark.nocomm From a555fb691871d99afc9178750eede1b2d798b195 Mon Sep 17 00:00:00 2001 From: Jaya Mohan Date: Fri, 17 May 2024 09:40:14 -0700 Subject: [PATCH 33/36] check preconditions and import exago on test_9_finalize.py --- tests/interfaces/python/test_9_finalize.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/interfaces/python/test_9_finalize.py b/tests/interfaces/python/test_9_finalize.py index 2933b5dd..4f295c90 100644 --- a/tests/interfaces/python/test_9_finalize.py +++ b/tests/interfaces/python/test_9_finalize.py @@ -1,8 +1,9 @@ # test only run once things here +import exago import pytest from check_preconditions import check_preconditions check_preconditions() -import exago + @pytest.mark.nocomm From d316898e8fce08d14a3684c3b05f62dfc04ecfd1 Mon Sep 17 00:00:00 2001 From: Jayapreethi Date: Fri, 17 May 2024 17:57:41 +0000 Subject: [PATCH 34/36] Apply pre-commmit fixes --- tests/interfaces/python/test_9_finalize.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/interfaces/python/test_9_finalize.py b/tests/interfaces/python/test_9_finalize.py index 4f295c90..e97cd7ff 100644 --- a/tests/interfaces/python/test_9_finalize.py +++ b/tests/interfaces/python/test_9_finalize.py @@ -5,7 +5,6 @@ check_preconditions() - @pytest.mark.nocomm @pytest.mark.MPI def test_finalize(): From 5700ee219b9b4ac443eee31d7cae8582868800df Mon Sep 17 00:00:00 2001 From: Jaya Mohan Date: Fri, 17 May 2024 13:55:43 -0700 Subject: [PATCH 35/36] remove duplicate check_preconditions() on test_0_initialize.py --- tests/interfaces/python/test_0_initialize.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/tests/interfaces/python/test_0_initialize.py b/tests/interfaces/python/test_0_initialize.py index 322116e3..78aa0b68 100644 --- a/tests/interfaces/python/test_0_initialize.py +++ b/tests/interfaces/python/test_0_initialize.py @@ -9,9 +9,6 @@ import exago # noqa -check_preconditions() - - @pytest.mark.nocomm def test_initialize_nocomm(): '''Test call ExaGOInitialize without a communicator''' From 434b0ec442a51a9a542ca21f50a65fce04e3d226 Mon Sep 17 00:00:00 2001 From: Jaya Mohan Date: Mon, 20 May 2024 11:59:24 -0700 Subject: [PATCH 36/36] import exago after pre_conditions check --- tests/interfaces/python/test_9_finalize.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/interfaces/python/test_9_finalize.py b/tests/interfaces/python/test_9_finalize.py index e97cd7ff..d03a427e 100644 --- a/tests/interfaces/python/test_9_finalize.py +++ b/tests/interfaces/python/test_9_finalize.py @@ -1,8 +1,8 @@ # test only run once things here -import exago import pytest from check_preconditions import check_preconditions check_preconditions() +import exago # noqa @pytest.mark.nocomm