Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TCOPFLOW Python Bindings First Pass #131

Merged
merged 36 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
a794913
add new branch and pytests repo
Mar 19, 2024
b86a675
add tecopflow files
Mar 26, 2024
b7d2694
clean dir and add new tcopflow files
Mar 26, 2024
b96646c
Apply pre-commmit fixes
cameronrutherford Mar 26, 2024
14a4f34
add tcopflow files to python bindings
jaelynlitz Mar 29, 2024
55836c5
rename python files from test to example
jaelynlitz Mar 29, 2024
d08d491
Apply pre-commmit fixes
jaelynlitz Mar 29, 2024
9397341
rebase on origin branch
Apr 16, 2024
f4cc2d1
test_tcopflow.cpp
Apr 26, 2024
870ddb5
sort tcopflow functions
Apr 29, 2024
392f196
Apply pre-commmit fixes
Jayapreethi Apr 29, 2024
c790be8
test tcopflow functions
Apr 29, 2024
5d3f16d
first passing tcopflow test
Apr 29, 2024
08060ca
Apply pre-commmit fixes
Jayapreethi Apr 29, 2024
666b028
clean tcopflow test file
Apr 30, 2024
f6aada7
Apply pre-commmit fixes
Jayapreethi Apr 30, 2024
2bdb686
file clean up and comment added to test_tcopflow.py
Apr 30, 2024
9f1b8ab
removed tcopflow_bind files
Apr 30, 2024
8679ebf
add examples to example_tcopflow.pu
May 3, 2024
e48546d
Apply pre-commmit fixes
Jayapreethi May 3, 2024
ed70232
remove u unused value on test_5_tcopflow.py
May 7, 2024
b24dbd9
Apply pre-commmit fixes
Jayapreethi May 7, 2024
6809d6d
checked preconditions before importing exago on test_5_tcopflow.py
May 8, 2024
3f31ecd
check preconditions for all tests
May 9, 2024
307458e
Apply pre-commmit fixes
Jayapreethi May 9, 2024
ac9af84
documented tcopflow function on python_bindings.md file and tested th…
May 14, 2024
60ecf8f
Added functions to exago_python_tcopflow.cpp and updated the document…
May 16, 2024
0296d1b
Apply pre-commmit fixes
Jayapreethi May 16, 2024
7bbf504
checked preconditions() before importing exago
May 16, 2024
0ec093d
fix pre-commit checks
May 17, 2024
a96f99d
Apply pre-commmit fixes
Jayapreethi May 17, 2024
63506a9
check preconditions and import exago
May 17, 2024
a555fb6
check preconditions and import exago on test_9_finalize.py
May 17, 2024
d316898
Apply pre-commmit fixes
Jayapreethi May 17, 2024
5700ee2
remove duplicate check_preconditions() on test_0_initialize.py
May 17, 2024
434b0ec
import exago after pre_conditions check
May 20, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion docs/python_bindings.md
Original file line number Diff line number Diff line change
Expand Up @@ -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). |
Expand Down Expand Up @@ -267,6 +267,32 @@ 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 |
|---|---|---|
| `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

`OutputFormat` is the enum that specifies the output format in functions like `opflow.save_solution`.
Expand Down
6 changes: 4 additions & 2 deletions interfaces/python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ 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
cameronrutherford marked this conversation as resolved.
Show resolved Hide resolved
)
list(APPEND EXAGO_PY_LIBS ExaGO::SOPFLOW ExaGO::SCOPFLOW ExaGO::TCOPFLOW)
endif()

# New true python bindings
Expand Down
4 changes: 4 additions & 0 deletions interfaces/python/exago_python.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
141 changes: 141 additions & 0 deletions interfaces/python/exago_python_tcopflow.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
#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_<TCOPFLOW_wrapper>(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",
[](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("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;
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("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);
});
}
8 changes: 8 additions & 0 deletions interfaces/python/exago_python_tcopflow.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#ifndef _exago_python_tcopflow_hpp_
#define _exago_python_tcopflow_hpp_

#include "exago_python_opflow.hpp"
#include <tcopflow.h>
#include <private/tcopflowimpl.h>

#endif
File renamed without changes.
File renamed without changes.
File renamed without changes.
17 changes: 17 additions & 0 deletions interfaces/python/example_tcopflow.py
cameronrutherford marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -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)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wdym try, at least? Does this not work when running?

del tcopf

exago.finalize()
11 changes: 10 additions & 1 deletion tests/interfaces/python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
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")

Expand Down
3 changes: 2 additions & 1 deletion tests/interfaces/python/test_0_initialize.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# test initializing exago only once
from check_preconditions import check_preconditions
import os
import pytest
from check_preconditions import check_preconditions
import mpi4py.rc
mpi4py.rc.threads = False
from mpi4py import MPI # noqa
check_preconditions()
import exago # noqa


Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Eliminate duplicate check_preconditions()

Expand Down
4 changes: 3 additions & 1 deletion tests/interfaces/python/test_1_pflow.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from check_preconditions import check_preconditions
import os
import pytest
from check_preconditions import check_preconditions
import mpi4py.rc
mpi4py.rc.threads = False
from mpi4py import MPI # noqa
check_preconditions()
import exago # noqa


check_preconditions()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
check_preconditions()



Expand Down
4 changes: 2 additions & 2 deletions tests/interfaces/python/test_2_opflow.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from check_preconditions import check_preconditions
import os
import pytest
from check_preconditions import check_preconditions
import mpi4py.rc
mpi4py.rc.threads = False
from mpi4py import MPI # noqa
check_preconditions()
import exago # noqa

check_preconditions()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
check_preconditions()

Expand Down
4 changes: 2 additions & 2 deletions tests/interfaces/python/test_3_scopflow.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
from check_preconditions import check_preconditions
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
check_preconditions()
import exago # noqa

check_preconditions()

exago_ignore = -1000000

Expand Down
4 changes: 2 additions & 2 deletions tests/interfaces/python/test_4_sopflow.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
from check_preconditions import check_preconditions
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
check_preconditions()
import exago # noqa

check_preconditions()

exago_ignore = -1000000

Expand Down
37 changes: 37 additions & 0 deletions tests/interfaces/python/test_5_tcopflow.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from check_preconditions import check_preconditions
import tempfile
import os
import shutil
import pytest
import mpi4py.rc
mpi4py.rc.threads = False
from mpi4py import MPI # noqa
check_preconditions()
import exago # noqa


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)

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)


@pytest.mark.nocomm
@pytest.mark.MPI
def test_tcopflow_1():
run_tcopflow('IPOPT')
cameronrutherford marked this conversation as resolved.
Show resolved Hide resolved
3 changes: 1 addition & 2 deletions tests/interfaces/python/test_9_finalize.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
# test only run once things here
import pytest
import exago
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
import exago

import pytest
from check_preconditions import check_preconditions

check_preconditions()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
check_preconditions()
check_preconditions()
import exago



Expand Down