Skip to content

Commit f82d637

Browse files
authored
Merge pull request #149 from NREL/batt_functions_for_reopt_sizing
Batt functions for reopt sizing
2 parents 93b4086 + e5905e9 commit f82d637

File tree

7 files changed

+69
-6
lines changed

7 files changed

+69
-6
lines changed

modules/Battery.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
#include "PySAM_utils.h"
77

8+
#include "Battery_eqns.c"
9+
810

911
/*
1012
* Simulation Group
@@ -6046,6 +6048,8 @@ static PyMethodDef Battery_methods[] = {
60466048
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.")},
60476049
{"unassign", (PyCFunction)Battery_unassign, METH_VARARGS,
60486050
PyDoc_STR("unassign(name) -> None\n Unassign a value in any of the variable groups.")},
6051+
{"Reopt_size_standalone_battery_post", (PyCFunction)Reopt_size_standalone_battery_post, METH_VARARGS | METH_KEYWORDS,
6052+
Reopt_size_standalone_battery_post_doc},
60496053
{NULL, NULL} /* sentinel */
60506054
};
60516055

src/Battery_eqns.c

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include "SAM_eqns.h"
2+
3+
char Reopt_size_standalone_battery_post_doc[] =
4+
"Given a PV system with Utilityrate5 parameters, get the optimal battery size.\n"
5+
"Maps SAM compute module inputs to those of the ReOpt API:\n"
6+
"Pvsamv1 model linked with Battery, Utilityrate5, with Cashloan optional.\n\n"
7+
"For more information on which PySAM variables are used, see: https://github.com/NREL/ssc/blob/develop/ssc/cmod_battery_eqns.h\n\n"
8+
"For an example, see the 'Examples' readthedocs page.";
9+
10+
static PyObject* Reopt_size_standalone_battery_post(PyObject *self, PyObject *args, PyObject *keywds)
11+
{
12+
13+
CmodObject* self_obj = (CmodObject*)self;
14+
15+
SAM_table data = self_obj->data_ptr;
16+
17+
SAM_error error = new_error();
18+
19+
PyObject* reopt_post_obj = NULL;
20+
error = new_error();
21+
SAM_Reopt_size_standalone_battery_post_eqn(data, &error);
22+
23+
if (PySAM_has_error(error)){
24+
return NULL;
25+
}
26+
27+
error = new_error();
28+
SAM_table reopt_post = SAM_table_get_table(data, "reopt_scenario", &error);
29+
if (PySAM_has_error(error))
30+
goto cleanup;
31+
32+
error = new_error();
33+
const char* log_msg = SAM_table_get_string(data, "log", &error);
34+
if (PySAM_has_error(error))
35+
goto cleanup;
36+
37+
SAM_table results = SAM_table_construct(NULL);
38+
SAM_table_set_table(results, "reopt_post", reopt_post, NULL);
39+
SAM_table_set_string(results, "messages", log_msg, NULL);
40+
reopt_post_obj = PySAM_table_to_dict(results);
41+
SAM_table_destruct(results, NULL);
42+
43+
cleanup:
44+
SAM_table_unassign_entry(data, "reopt_scenario", NULL);
45+
SAM_table_unassign_entry(data, "log", NULL);
46+
return reopt_post_obj;
47+
}

src/Pvsamv1_eqns.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

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

4141
if (strlen(resource_file) == 0){
42-
PyErr_SetString(PyExc_Exception, "Reopt_size_battery_pot error: solar_resource_file or solar_resource_data must be provided.");
42+
PyErr_SetString(PyExc_Exception, "Reopt_size_battery_post error: solar_resource_file or solar_resource_data must be provided.");
4343
return NULL;
4444
}
4545
SAM_table ssc_data = SAM_table_construct(NULL);

src/Pvwattsv7_eqns.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

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

4141
if (strlen(resource_file) == 0){
42-
PyErr_SetString(PyExc_Exception, "Reopt_size_battery_pot error: solar_resource_file or solar_resource_data must be provided.");
42+
PyErr_SetString(PyExc_Exception, "Reopt_size_battery_post error: solar_resource_file or solar_resource_data must be provided.");
4343
return NULL;
4444
}
4545
SAM_table ssc_data = SAM_table_construct(NULL);

src/Pvwattsv8_eqns.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

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

4141
if (strlen(resource_file) == 0){
42-
PyErr_SetString(PyExc_Exception, "Reopt_size_battery_pot error: solar_resource_file or solar_resource_data must be provided.");
42+
PyErr_SetString(PyExc_Exception, "Reopt_size_battery_post error: solar_resource_file or solar_resource_data must be provided.");
4343
return NULL;
4444
}
4545
SAM_table ssc_data = SAM_table_construct(NULL);

stubs/stubs/Battery.pyi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ class Battery(object):
2020
def __init__(self, *args, **kwargs):
2121
pass
2222

23+
def Reopt_size_standalone_battery_post(self, args):
24+
pass
2325
class Simulation(object):
2426
def assign(self):
2527
pass

tests/test_Reopt_sizing.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import PySAM.Utilityrate5 as ur
77
import PySAM.Pvsamv1 as pvsam
88
import PySAM.Battery as stbt
9+
import PySAM.Cashloan as loan
910

1011

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

54+
def test_repot_sizing_battery():
55+
batt = stbt.default("GenericBatteryCommercial")
56+
rate = ur.from_existing(batt, "GenericBatteryCommercial")
57+
fin = loan.from_existing(batt, "GenericBatteryCommercial")
58+
59+
post = batt.Reopt_size_standalone_battery_post()
60+
61+
assert('Scenario' in post['reopt_post'])
62+
assert(post['reopt_post']['Scenario']['Site']['Storage']['soc_init_pct'] == pytest.approx(0.5, 0.1))

0 commit comments

Comments
 (0)