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 23 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
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
}
87 changes: 87 additions & 0 deletions interfaces/python/exago_python_tcopflow.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#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("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;
});
}
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
4 changes: 3 additions & 1 deletion tests/interfaces/python/test_0_initialize.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# test initializing exago only once
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

Don't import exago before check_preconditions

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
38 changes: 38 additions & 0 deletions tests/interfaces/python/test_5_tcopflow.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
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

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()



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
Loading