-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TCOPFLOW Python Bindings First Pass (#131)
* add new branch and pytests repo * add tecopflow files * clean dir and add new tcopflow files * Apply pre-commmit fixes * add tcopflow files to python bindings * rename python files from test to example * Apply pre-commmit fixes * rebase on origin branch * test_tcopflow.cpp * sort tcopflow functions * Apply pre-commmit fixes * test tcopflow functions * first passing tcopflow test * Apply pre-commmit fixes * clean tcopflow test file * Apply pre-commmit fixes * file clean up and comment added to test_tcopflow.py * removed tcopflow_bind files * add examples to example_tcopflow.pu * Apply pre-commmit fixes * remove u unused value on test_5_tcopflow.py * Apply pre-commmit fixes * checked preconditions before importing exago on test_5_tcopflow.py * check preconditions for all tests * Apply pre-commmit fixes * documented tcopflow function on python_bindings.md file and tested the functions * Added functions to exago_python_tcopflow.cpp and updated the documentation * Apply pre-commmit fixes * checked preconditions() before importing exago * fix pre-commit checks * Apply pre-commmit fixes * check preconditions and import exago * check preconditions and import exago on test_9_finalize.py * Apply pre-commmit fixes * remove duplicate check_preconditions() on test_0_initialize.py * import exago after pre_conditions check --------- Co-authored-by: Mohan, Jaya <[email protected]> Co-authored-by: cameronrutherford <[email protected]> Co-authored-by: Jaelyn Litzinger <[email protected]> Co-authored-by: jaelynlitz <[email protected]> Co-authored-by: Jaya Mohan <[email protected]> Co-authored-by: Jaya Mohan <[email protected]> Co-authored-by: Jaya Mohan <[email protected]> Co-authored-by: Jayapreethi <[email protected]> Co-authored-by: Jaya Mohan <[email protected]>
- Loading branch information
1 parent
edc9e45
commit 84fdbf2
Showing
18 changed files
with
259 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
del tcopf | ||
|
||
exago.finalize() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters