Skip to content

Commit

Permalink
Updated to do unittests for power flow and dispatch models.
Browse files Browse the repository at this point in the history
  • Loading branch information
Castillo committed May 22, 2019
1 parent 14d57d4 commit 3baaa95
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 54 deletions.
61 changes: 43 additions & 18 deletions egret/models/tests/test_acopf.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,20 @@
from pyomo.opt import SolverFactory, TerminationCondition
from egret.models.acopf import *
from egret.data.model_data import ModelData
from parameterized import parameterized

current_dir = os.path.dirname(os.path.abspath(__file__))
case_names = ['pglib_opf_case3_lmbd','pglib_opf_case30_ieee','pglib_opf_case300_ieee','pglib_opf_case3012wp_k','pglib_opf_case13659_pegase']
case_names = ['pglib_opf_case3_lmbd','pglib_opf_case30_ieee','pglib_opf_case300_ieee']#,'pglib_opf_case3012wp_k','pglib_opf_case13659_pegase']
test_cases = [os.path.join(current_dir, 'transmission_test_instances', '{}.json'.format(i)) for i in case_names]
soln_cases = [os.path.join(current_dir, 'transmission_test_instances', 'acopf_solution_files', '{}_acopf_solution.json'.format(i)) for i in case_names]


def _test_acopf_model(acopf_model):
for test_case, soln_case in zip(test_cases, soln_cases):
class TestPSVACOPF(unittest.TestCase):
show_output = True

@parameterized.expand(zip(test_cases, soln_cases))
def test_acopf_model(self, test_case, soln_case):
acopf_model = create_psv_acopf_model

md_soln = ModelData()
md_soln.read_from_json(soln_case)
Expand All @@ -34,30 +39,50 @@ def _test_acopf_model(acopf_model):

md, results = solve_acopf(md_dict, "ipopt", acopf_model_generator=acopf_model, solver_tee=False, return_results=True)

assert results.solver.termination_condition == TerminationCondition.optimal
assert math.isclose(md.data['system']['total_cost'], md_soln.data['system']['total_cost'], rel_tol=1e04)
self.assertTrue(results.solver.termination_condition == TerminationCondition.optimal)
comparison = math.isclose(md.data['system']['total_cost'], md_soln.data['system']['total_cost'], rel_tol=1e04)
self.assertTrue(comparison)


class TestRSVACOPF(unittest.TestCase):
show_output = True

@unittest.skipUnless(SolverFactory('ipopt').available(), "Ipopt solver is not available")
@parameterized.expand(zip(test_cases, soln_cases))
def test_acopf_model(self, test_case, soln_case):
acopf_model = create_rsv_acopf_model

md_soln = ModelData()
md_soln.read_from_json(soln_case)

md_dict = ModelData()
md_dict.read_from_json(test_case)

def test_all_acopf_models():
_test_acopf_model(create_psv_acopf_model)
_test_acopf_model(create_rsv_acopf_model)
_test_acopf_model(create_riv_acopf_model)
md, results = solve_acopf(md_dict, "ipopt", acopf_model_generator=acopf_model, solver_tee=False, return_results=True)

self.assertTrue(results.solver.termination_condition == TerminationCondition.optimal)
comparison = math.isclose(md.data['system']['total_cost'], md_soln.data['system']['total_cost'], rel_tol=1e04)
self.assertTrue(comparison)

def test_psv_acopf_model():
_test_acopf_model(create_psv_acopf_model)

class TestRIVACOPF(unittest.TestCase):
show_output = True

def test_rsv_acopf_model():
_test_acopf_model(create_rsv_acopf_model)
@parameterized.expand(zip(test_cases, soln_cases))
def test_acopf_model(self, test_case, soln_case):
acopf_model = create_riv_acopf_model

md_soln = ModelData()
md_soln.read_from_json(soln_case)

md_dict = ModelData()
md_dict.read_from_json(test_case)

md, results = solve_acopf(md_dict, "ipopt", acopf_model_generator=acopf_model, solver_tee=False, return_results=True)

def test_riv_acopf_model():
_test_acopf_model(create_riv_acopf_model)
self.assertTrue(results.solver.termination_condition == TerminationCondition.optimal)
comparison = math.isclose(md.data['system']['total_cost'], md_soln.data['system']['total_cost'], rel_tol=1e04)
self.assertTrue(comparison)


# if __name__ == '__main__':
# test_all_acopf_models()
if __name__ == '__main__':
unittest.main()
31 changes: 12 additions & 19 deletions egret/models/tests/test_copperplate_dispatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,37 +16,30 @@
from pyomo.opt import SolverFactory, TerminationCondition
from egret.models.copperplate_dispatch import *
from egret.data.model_data import ModelData
from parameterized import parameterized

current_dir = os.path.dirname(os.path.abspath(__file__))
case_names = ['pglib_opf_case3_lmbd','pglib_opf_case30_ieee','pglib_opf_case300_ieee','pglib_opf_case3012wp_k','pglib_opf_case13659_pegase']
case_names = ['pglib_opf_case3_lmbd','pglib_opf_case30_ieee','pglib_opf_case300_ieee']#,'pglib_opf_case3012wp_k','pglib_opf_case13659_pegase']
test_cases = [os.path.join(current_dir, 'transmission_test_instances', '{}.json'.format(i)) for i in case_names]
soln_cases = [os.path.join(current_dir, 'transmission_test_instances', 'dcopf_solution_files', '{}_dcopf_solution.json'.format(i)) for i in case_names]

class TestCopperPlateDispatch(unittest.TestCase):
show_output = True

def _test_copperplate_dispatch_model(copperplate_dispatch_model):
for test_case, soln_case in zip(test_cases, soln_cases):

@parameterized.expand(zip(test_cases, soln_cases))
def test_copperplate_dispatch_model(self, test_case, soln_case):
md_soln = ModelData()
md_soln.read_from_json(soln_case)

md_dict = ModelData()
md_dict.read_from_json(test_case)

md, results = solve_copperplate_dispatch(md_dict, "gurobi", copperplate_dispatch_model_generator=copperplate_dispatch_model, solver_tee=False, return_results=True)

assert results.solver.termination_condition == TerminationCondition.optimal
assert math.isclose(md.data['system']['total_cost'], md_soln.data['system']['total_cost'], rel_tol=1e04)


@unittest.skipUnless(SolverFactory('gurobi').available(), "Gurobi solver is not available")


def test_all_copperplate_dispatch_models():
_test_copperplate_dispatch_model(create_copperplate_dispatch_approx_model)
md, results = solve_copperplate_dispatch(md_dict, "gurobi", solver_tee=False, return_results=True)

self.assertTrue(results.solver.termination_condition == TerminationCondition.optimal)
comparison = math.isclose(md.data['system']['total_cost'], md_soln.data['system']['total_cost'], rel_tol=1e04)
self.assertTrue(comparison)

def test_copperplate_dispatch_approx_model():
_test_copperplate_dispatch_model(create_copperplate_dispatch_approx_model)

# if __name__ == '__main__':
# test_all_copperplate_dispatch_models()
if __name__ == '__main__':
unittest.main()
29 changes: 12 additions & 17 deletions egret/models/tests/test_dcopf.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,19 @@
from pyomo.opt import SolverFactory, TerminationCondition
from egret.models.dcopf import *
from egret.data.model_data import ModelData
from parameterized import parameterized

current_dir = os.path.dirname(os.path.abspath(__file__))
case_names = ['pglib_opf_case3_lmbd','pglib_opf_case30_ieee','pglib_opf_case300_ieee','pglib_opf_case3012wp_k','pglib_opf_case13659_pegase']
case_names = ['pglib_opf_case3_lmbd','pglib_opf_case30_ieee','pglib_opf_case300_ieee']#,'pglib_opf_case3012wp_k','pglib_opf_case13659_pegase']
test_cases = [os.path.join(current_dir, 'transmission_test_instances', '{}.json'.format(i)) for i in case_names]
soln_cases = [os.path.join(current_dir, 'transmission_test_instances', 'dcopf_solution_files', '{}_dcopf_solution.json'.format(i)) for i in case_names]

class TestBThetaDCOPF(unittest.TestCase):
show_output = True

def _test_dcopf_model(dcopf_model):
for test_case, soln_case in zip(test_cases, soln_cases):
@parameterized.expand(zip(test_cases, soln_cases))
def test_btheta_dcopf_model(self, test_case, soln_case):
dcopf_model = create_btheta_dcopf_model

md_soln = ModelData()
md_soln.read_from_json(soln_case)
Expand All @@ -34,19 +38,10 @@ def _test_dcopf_model(dcopf_model):

md, results = solve_dcopf(md_dict, "gurobi", dcopf_model_generator=dcopf_model, solver_tee=False, return_results=True)

assert results.solver.termination_condition == TerminationCondition.optimal
assert math.isclose(md.data['system']['total_cost'], md_soln.data['system']['total_cost'], rel_tol=1e04)
self.assertTrue(results.solver.termination_condition == TerminationCondition.optimal)
comparison = math.isclose(md.data['system']['total_cost'], md_soln.data['system']['total_cost'], rel_tol=1e04)
self.assertTrue(comparison)


@unittest.skipUnless(SolverFactory('gurobi').available(), "Gurobi solver is not available")


def test_all_dcopf_models():
_test_dcopf_model(create_btheta_dcopf_model)


def test_btheta_dcopf_model():
_test_dcopf_model(create_btheta_dcopf_model)

# if __name__ == '__main__':
# test_all_dcopf_models()
if __name__ == '__main__':
unittest.main()

0 comments on commit 3baaa95

Please sign in to comment.