-
Notifications
You must be signed in to change notification settings - Fork 114
Move batt stateful sizing to ssc equations #617
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
9df4c94
Convert two functions currently in pysam's battery tools to ssc equat…
brtietz df61bb5
Merge branch 'develop' of https://github.com/NREL/ssc into move_state…
brtietz 9941360
fix embarassing file inclusion issue, add comments to ssc_equations.h
brtietz 655856e
Merge branch 'develop' of https://github.com/NREL/ssc into move_state…
brtietz f363e32
add error checking if less than 1e-7 (trying to catch zeroes
brtietz 7e81687
Merge branch 'develop' of https://github.com/NREL/ssc into move_state…
brtietz b713f30
first cut at changing function signature - looks like we have to eith…
brtietz fd56177
refactor ssc_equation fxn return type as Bool
dguittet d079c09
Merge branch 'develop' into move_stateful_sizing_to_ssc_eqns
dguittet 1157e5e
Merge branch 'develop' into move_stateful_sizing_to_ssc_eqns
dguittet 3b6456c
fix windpower_eqns
dguittet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,99 @@ | ||
| /** | ||
| BSD-3-Clause | ||
| Copyright 2019 Alliance for Sustainable Energy, LLC | ||
| Redistribution and use in source and binary forms, with or without modification, are permitted provided | ||
| that the following conditions are met : | ||
| 1. Redistributions of source code must retain the above copyright notice, this list of conditions | ||
| and the following disclaimer. | ||
| 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions | ||
| and the following disclaimer in the documentation and/or other materials provided with the distribution. | ||
| 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse | ||
| or promote products derived from this software without specific prior written permission. | ||
|
|
||
| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, | ||
| INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT HOLDER, CONTRIBUTORS, UNITED STATES GOVERNMENT OR UNITED STATES | ||
| DEPARTMENT OF ENERGY, NOR ANY OF THEIR EMPLOYEES, BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, | ||
| OR CONSEQUENTIAL DAMAGES(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
| LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | ||
| WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT | ||
| OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| */ | ||
| #include "cmod_battery_eqns.h" | ||
|
|
||
| #include "core.h" | ||
| #include "vartab.h" | ||
|
|
||
| #include <cmath> | ||
|
|
||
| bool Size_batterystateful(ssc_data_t data) { | ||
| auto vt = static_cast<var_table*>(data); | ||
| char errmsg[250]; | ||
| if (!vt) { | ||
| return false; | ||
| } | ||
|
|
||
| double nominal_energy, desired_voltage, desired_capacity; | ||
|
|
||
| vt_get_number(vt, "nominal_energy", &nominal_energy); | ||
| vt_get_number(vt, "desired_voltage", &desired_voltage); | ||
| vt_get_number(vt, "desired_capacity", &desired_capacity); | ||
|
|
||
| // Cannot specify energy of zero (less than mW, really) due to resulting errors in scaling factors | ||
| if (nominal_energy < 1e-7) { | ||
| sprintf(errmsg, "nominal_energy cannot be less than 1e-7. Current value: %f", nominal_energy); | ||
| vt->assign("error", std::string(errmsg)); | ||
| return false; | ||
| } | ||
|
|
||
| if (desired_capacity < 1e-7) { | ||
| sprintf(errmsg, "desired_capacity cannot be less than 1e-7. Current value: %f", desired_capacity); | ||
| vt->assign("error", std::string(errmsg)); | ||
| return false; | ||
| } | ||
|
|
||
| vt->assign("original_capacity", nominal_energy); | ||
|
|
||
| bool thermal_success = Calculate_thermal_params(data); | ||
|
|
||
| vt->assign("nominal_energy", desired_capacity); | ||
| vt->assign("nominal_voltage", desired_voltage); | ||
|
|
||
| return thermal_success; | ||
| } | ||
|
|
||
| bool Calculate_thermal_params(ssc_data_t data) { | ||
| auto vt = static_cast<var_table*>(data); | ||
| if (!vt) { | ||
| return false; | ||
| } | ||
|
|
||
| double mass, surface_area, original_capacity, desired_capacity, module_capacity, module_surface_area; | ||
|
|
||
| vt_get_number(vt, "mass", &mass); | ||
| vt_get_number(vt, "surface_area", &surface_area); | ||
| vt_get_number(vt, "original_capacity", &original_capacity); | ||
| vt_get_number(vt, "desired_capacity", &desired_capacity); | ||
|
|
||
| double mass_per_specific_energy = mass / original_capacity; | ||
|
|
||
| double volume = std::pow((surface_area / 6.0), (3.0 / 2.0)); | ||
|
|
||
| double volume_per_specific_energy = volume / original_capacity; | ||
|
|
||
| mass = mass_per_specific_energy * desired_capacity; | ||
|
|
||
| surface_area = std::pow((volume_per_specific_energy * desired_capacity), (2.0 / 3.0)) * 6; | ||
|
|
||
| if (vt->is_assigned("module_capacity") && vt->is_assigned("module_surface_area")) { | ||
| vt_get_number(vt, "module_capacity", &module_capacity); | ||
| vt_get_number(vt, "module_surface_area", &module_surface_area); | ||
| surface_area = module_surface_area * desired_capacity / module_capacity; | ||
| } | ||
|
|
||
| vt->assign("mass", mass); | ||
| vt->assign("surface_area", surface_area); | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
This file contains hidden or 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,65 @@ | ||
| /** | ||
| BSD-3-Clause | ||
| Copyright 2019 Alliance for Sustainable Energy, LLC | ||
| Redistribution and use in source and binary forms, with or without modification, are permitted provided | ||
| that the following conditions are met : | ||
| 1. Redistributions of source code must retain the above copyright notice, this list of conditions | ||
| and the following disclaimer. | ||
| 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions | ||
| and the following disclaimer in the documentation and/or other materials provided with the distribution. | ||
| 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse | ||
| or promote products derived from this software without specific prior written permission. | ||
|
|
||
| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, | ||
| INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT HOLDER, CONTRIBUTORS, UNITED STATES GOVERNMENT OR UNITED STATES | ||
| DEPARTMENT OF ENERGY, NOR ANY OF THEIR EMPLOYEES, BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, | ||
| OR CONSEQUENTIAL DAMAGES(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
| LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | ||
| WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT | ||
| OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| */ | ||
|
|
||
| #ifndef _CMOD_BATTERY_EQNS_H_ | ||
| #define _CMOD_BATTERY_EQNS_H_ | ||
|
|
||
| #include "sscapi.h" | ||
|
|
||
| #ifdef __cplusplus | ||
| extern "C" { | ||
| #endif | ||
|
|
||
| static const char* size_batterystateful_doc = | ||
| "Resizes the battery for a battery_stateful data object \\n\\n" | ||
| "Input: var_table with key-value pairs\\n" | ||
| " 'nominal_energy': double [kWh]\\n" | ||
| " 'desired_capacity': double [kWh]\\n" | ||
| " 'desired_voltage': double [V]\\n" | ||
| " 'mass': double [kg] \\n" | ||
| " 'surface_area': double [m^2],\\n" | ||
| " 'module_capacity': double [kWh], optional\\n" | ||
| " 'module_surface_area': double [m^2], optional\\n" | ||
| "Output: key-value pairs added to var_table, mass, surface_area, and nominal_energy will be modified\\n" | ||
| " 'original_capacity': kWh [kWh]\\n"; | ||
|
|
||
|
|
||
| SSCEXPORT bool Size_batterystateful(ssc_data_t data); | ||
|
|
||
| static const char* calculate_thermal_params_doc = | ||
| "Resizes the battery for a battery_stateful data object \\n\\n" | ||
| "Input: var_table with key-value pairs\\n" | ||
| " 'original_capacity': double [kWh]\\n" | ||
| " 'desired_capacity': double [kWh]\\n" | ||
| " 'mass': double [kg] \\n" | ||
| " 'surface_area': double [m^2],\\n" | ||
| " 'module_capacity': double [kWh], optional\\n" | ||
| " 'module_surface_area': double [m^2], optional\\n" | ||
| "Output: mass and surface_area will be modified\\n"; | ||
|
|
||
| SSCEXPORT bool Calculate_thermal_params(ssc_data_t data); | ||
|
|
||
| #ifdef __cplusplus | ||
| } | ||
| #endif | ||
|
|
||
| #endif |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is an edge case where the original_capacity is 0 and then resized to non-zero. This happens when you size the battery to 0, perhaps as part of a loop across a range of numbers, then the mass and surface area will get set to 0 also. Then for later sizing calls, the function will no longer be able to use prior information about the mass to calculate an appropriate mass, and here it'll cause a divide by 0.
Other cmods are not able to run with a strictly 0 system capacity (pvsamv1, windpower) while others can (pvwatts). While this is a still-unfixed issue in the PySAM code, I think the simplest way to handle this is to not let the desired_capacity be 0.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great point! In the latest commit I used the same error convention as cmod_windpower_eqns - writing the error to an "error" variable in the vartable. Am I correct to assume it's the responsibility of the calling code to check this value? When I tried to throw an exec error, it crashed Julia. Given the difficulty of error handling in the SDK, I assume that's not a valid option.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, you can't throw exceptions across a DLL boundary so that's not possible. The error will have to be done via a return string. Perhaps we could change the function signature of SSC equations so the return type is bool instead of void. I don't think that would be too difficult and would make error handling easier
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let me know if you want me to change the function signature to bool. Otherwise you have to check if error len > 0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That would be super helpful. I tried to do the update just now and ran into a type issue:
It looks like we either need to change them all or define a new signature with bool. I changed the battery stateful equations in the most recent push to get that error, if you're able to do the others I'd appreciate it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Apologies for the delay, but I've fixed up all the equations: changing the return type, fixing up the error handling from exceptions to error strings.