Skip to content

Commit

Permalink
Merge pull request #149 from NREL/batt_functions_for_reopt_sizing
Browse files Browse the repository at this point in the history
Batt functions for reopt sizing
  • Loading branch information
brtietz authored May 10, 2023
2 parents 93b4086 + e5905e9 commit f82d637
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 6 deletions.
4 changes: 4 additions & 0 deletions modules/Battery.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

#include "PySAM_utils.h"

#include "Battery_eqns.c"


/*
* Simulation Group
Expand Down Expand Up @@ -6046,6 +6048,8 @@ static PyMethodDef Battery_methods[] = {
PyDoc_STR("value(name, optional value) -> Union[None, float, dict, sequence, str]\n Get or set by name a value in any of the variable groups.")},
{"unassign", (PyCFunction)Battery_unassign, METH_VARARGS,
PyDoc_STR("unassign(name) -> None\n Unassign a value in any of the variable groups.")},
{"Reopt_size_standalone_battery_post", (PyCFunction)Reopt_size_standalone_battery_post, METH_VARARGS | METH_KEYWORDS,
Reopt_size_standalone_battery_post_doc},
{NULL, NULL} /* sentinel */
};

Expand Down
47 changes: 47 additions & 0 deletions src/Battery_eqns.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include "SAM_eqns.h"

char Reopt_size_standalone_battery_post_doc[] =
"Given a PV system with Utilityrate5 parameters, get the optimal battery size.\n"
"Maps SAM compute module inputs to those of the ReOpt API:\n"
"Pvsamv1 model linked with Battery, Utilityrate5, with Cashloan optional.\n\n"
"For more information on which PySAM variables are used, see: https://github.com/NREL/ssc/blob/develop/ssc/cmod_battery_eqns.h\n\n"
"For an example, see the 'Examples' readthedocs page.";

static PyObject* Reopt_size_standalone_battery_post(PyObject *self, PyObject *args, PyObject *keywds)
{

CmodObject* self_obj = (CmodObject*)self;

SAM_table data = self_obj->data_ptr;

SAM_error error = new_error();

PyObject* reopt_post_obj = NULL;
error = new_error();
SAM_Reopt_size_standalone_battery_post_eqn(data, &error);

if (PySAM_has_error(error)){
return NULL;
}

error = new_error();
SAM_table reopt_post = SAM_table_get_table(data, "reopt_scenario", &error);
if (PySAM_has_error(error))
goto cleanup;

error = new_error();
const char* log_msg = SAM_table_get_string(data, "log", &error);
if (PySAM_has_error(error))
goto cleanup;

SAM_table results = SAM_table_construct(NULL);
SAM_table_set_table(results, "reopt_post", reopt_post, NULL);
SAM_table_set_string(results, "messages", log_msg, NULL);
reopt_post_obj = PySAM_table_to_dict(results);
SAM_table_destruct(results, NULL);

cleanup:
SAM_table_unassign_entry(data, "reopt_scenario", NULL);
SAM_table_unassign_entry(data, "log", NULL);
return reopt_post_obj;
}
4 changes: 2 additions & 2 deletions src/Pvsamv1_eqns.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

char Reopt_size_battery_post_doc[] =
"Given a PV system with Utilityrate5 parameters, get the optimal battery size. Wind and additional PV are disabled.\n"
"Maps SAM compute module inputs to those of the ReOpt Lite API:\n"
"Maps SAM compute module inputs to those of the ReOpt API:\n"
"Pvsamv1 model linked with Battery, Utilityrate5, with Cashloan optional.\n\n"
"For more information on which PySAM variables are used, see: https://github.com/NREL/ssc/blob/develop/ssc/cmod_pvsamv1_eqns.h\n\n"
"For an example, see the 'Examples' readthedocs page.";
Expand Down Expand Up @@ -39,7 +39,7 @@ static PyObject* Reopt_size_battery_post(PyObject *self, PyObject *args, PyObjec
return NULL;

if (strlen(resource_file) == 0){
PyErr_SetString(PyExc_Exception, "Reopt_size_battery_pot error: solar_resource_file or solar_resource_data must be provided.");
PyErr_SetString(PyExc_Exception, "Reopt_size_battery_post error: solar_resource_file or solar_resource_data must be provided.");
return NULL;
}
SAM_table ssc_data = SAM_table_construct(NULL);
Expand Down
4 changes: 2 additions & 2 deletions src/Pvwattsv7_eqns.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

char Reopt_size_battery_post_doc[] =
"Given a PV system with Utilityrate5 parameters, get the optimal battery size. Wind and additional PV are disabled.\n"
"Maps SAM compute module inputs to those of the ReOpt Lite API:\n"
"Maps SAM compute module inputs to those of the ReOpt API:\n"
"Pvwattsv5 module linked with Battwatts, Utilityrate5, with Cashloan optional.\n\n"
"For more information on which PySAM variables are used, see: https://github.com/NREL/ssc/blob/develop/ssc/cmod_pvsamv1_eqns.h\n\n"
"For an example, see the 'Examples' readthedocs page.";
Expand Down Expand Up @@ -39,7 +39,7 @@ static PyObject* Reopt_size_battery_post(PyObject *self, PyObject *args, PyObjec
return NULL;

if (strlen(resource_file) == 0){
PyErr_SetString(PyExc_Exception, "Reopt_size_battery_pot error: solar_resource_file or solar_resource_data must be provided.");
PyErr_SetString(PyExc_Exception, "Reopt_size_battery_post error: solar_resource_file or solar_resource_data must be provided.");
return NULL;
}
SAM_table ssc_data = SAM_table_construct(NULL);
Expand Down
4 changes: 2 additions & 2 deletions src/Pvwattsv8_eqns.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

char Reopt_size_battery_post_doc[] =
"Given a PV system with Utilityrate5 parameters, get the optimal battery size. Wind and additional PV are disabled.\n"
"Maps SAM compute module inputs to those of the ReOpt Lite API:\n"
"Maps SAM compute module inputs to those of the ReOpt API:\n"
"Pvwattsv5 module linked with Battwatts, Utilityrate5, with Cashloan optional.\n\n"
"For more information on which PySAM variables are used, see: https://github.com/NREL/ssc/blob/develop/ssc/cmod_pvsamv1_eqns.h\n\n"
"For an example, see the 'Examples' readthedocs page.";
Expand Down Expand Up @@ -39,7 +39,7 @@ static PyObject* Reopt_size_battery_post(PyObject *self, PyObject *args, PyObjec
return NULL;

if (strlen(resource_file) == 0){
PyErr_SetString(PyExc_Exception, "Reopt_size_battery_pot error: solar_resource_file or solar_resource_data must be provided.");
PyErr_SetString(PyExc_Exception, "Reopt_size_battery_post error: solar_resource_file or solar_resource_data must be provided.");
return NULL;
}
SAM_table ssc_data = SAM_table_construct(NULL);
Expand Down
2 changes: 2 additions & 0 deletions stubs/stubs/Battery.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class Battery(object):
def __init__(self, *args, **kwargs):
pass

def Reopt_size_standalone_battery_post(self, args):
pass
class Simulation(object):
def assign(self):
pass
Expand Down
10 changes: 10 additions & 0 deletions tests/test_Reopt_sizing.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import PySAM.Utilityrate5 as ur
import PySAM.Pvsamv1 as pvsam
import PySAM.Battery as stbt
import PySAM.Cashloan as loan


@pytest.fixture
Expand Down Expand Up @@ -50,3 +51,12 @@ def test_reopt_sizing_pvsam(solar_resource):
assert('Scenario' in post['reopt_post'])
assert(post['reopt_post']['Scenario']['Site']['latitude'] == pytest.approx(33.6, 0.1))

def test_repot_sizing_battery():
batt = stbt.default("GenericBatteryCommercial")
rate = ur.from_existing(batt, "GenericBatteryCommercial")
fin = loan.from_existing(batt, "GenericBatteryCommercial")

post = batt.Reopt_size_standalone_battery_post()

assert('Scenario' in post['reopt_post'])
assert(post['reopt_post']['Scenario']['Site']['Storage']['soc_init_pct'] == pytest.approx(0.5, 0.1))

0 comments on commit f82d637

Please sign in to comment.