NREL / pysam

Python Wrapper for the System Advisor Model
http://nrel-pysam.readthedocs.io
BSD 3-Clause "New" or "Revised" License
108 stars 49 forks source link

PySAM: CEC performance model with user entered specifications #176

Closed TessBroos closed 13 hours ago

TessBroos commented 2 days ago

0

I'm working in PySAM by creating a model in SAM, then exporting the JSON for use as inputs in my PySAM model in Python. However, I'm encountering an issue when trying to define a custom PV module. Although the module_model is set to 2 (indicating it should take values from user-specified inputs), it still appears to be using specifications from the module database.

From the JSON file, I can confirm it is referencing the specified values. According to the documentation, setting module_model to 1 should pull values from the database, while setting it to 2 should utilize user-specified inputs. Despite setting module_model to 2, it seems to default to the database values. untitled.json

Assign values from the single JSON to the respective models
for key, value in data.items():

Attempt to assign based on which model has the key

assigned = False
for model_name, model_instance in models.items():
    for group_name, group in model_instance.export().items():
        if key in group:
            setattr(getattr(model_instance, group_name), key, value)
            assigned = True
            break
    if assigned:
        break
cpaulgilman commented 1 day ago

I think the problem is 6par_ variable names instead of sixpar_. A quick fix should be to replace "6par" in the JSON file with "sixpar".

Here's more detail.

When you use SAM's code generator with the "PySAM JSON" option, it generates two JSON files: One with the case name, such as "untitled.json" and one with the case name and "_pvsamv1" appended, such as "untiltedpvsamv1.json". The latter file uses `sixpar` as required by PySAM, and indicated in the documentation of the Pvsamv1 user-specified module parameters variables.

Here's a Python script you can use to test that. First, in SAM, create a default Detailed PV / No Financial case, and then go to the Module page and choose the CEC Performance Model with User Entered Specifications option from the blue list at the top of the page. Then, on the Case menu, click Generate code, PySAM JSON.

import json
import PySAM.Pvsamv1 as PV

# create a new pvsamv1 model
pv = PV.new()

# assign inputs from JSON generated by SAM
with open("untitled_pvsamv1.json", "r") as file:
    data = json.load(file)
    # loop through each key-value pair
    for k, v in data.items():
        if 'adjust_' in k: # workaround for https://github.com/NREL/pysam/issues/164
            k = k.replace('adjust_', '')
        if k != "number_inputs":
            pv.value(k, v)

# print inputs for user-specified module parameters option to verify "sixpar" in variable names
print(pv.CECPerformanceModelWithUserEnteredSpecifications.export())

# run pvsamv1 for no bifacial
pv.execute()

# print some results
print(pv.value("annual_dc_net"))
print(pv.value("annual_ac_gross"))

# change user-specified module bifacial parameter
pv.value("sixpar_is_bifacial", 1)

# print inputs to confirm bifacial mode is enabled
print(pv.CECPerformanceModelWithUserEnteredSpecifications.export())

# run pvsamv1 for bifacial
pv.execute()

# print results -- should be higher than non-bifacial
print(pv.value("annual_dc_net"))
print(pv.value("annual_ac_gross"))

Let me know if that fixes the problem.

Best regards, Paul.

TessBroos commented 18 hours ago

Dear Paul,

That indeed fixed the problem.

Thank you for your help!

Kind regards, Tess Broos

cpaulgilman commented 13 hours ago

Hi Tess,

Thank you for confirming. I posted this solution on the SAM SDK/PySAM forum on the SAM website: https://sam.nrel.gov/forum/forum-sdk/4781-pvsamv1-cec-performance-model-with-user-entered-specifications-option-and-pysam.html

Please consider posting future questions about PySAM there. The GitHub issues is best for reporting bugs.

Thanks, Paul.