Working with Variables¶
A compute module’s input and output variables are organized into groups. You can find group names and other information about variables in the documentation of each compute module in List of SSC Compute Modules.
The following code examples show how to get and set a variable’s value, and how to work with groups.
For these examples, first import and create an instance of the Pvwattsv8 module with default values for the PVWatts – No Financial configuration:
import PySAM.Pvwattsv8 as pv
system_model = pv.default('PVWattsNone')
Get a variable’s value¶
There are three ways to get a variables value:
Access variables directly with
<class>.<subclass>.<variable>
.For example, to get the value of
array_type
variable, which is in theSystemDesign
group:x = system_model.SystemDesign.array_type print(x)
Use the get attributes function
getattr()
:group_object = getattr(system_model,'SystemDesign') variable = getattr(group_object,'array_type') print(group_object) print(variable) # equivalent with one line of code print( getattr(getattr(system_model, 'SystemDesign'), 'array_type') )
Use
value()
, which does not require the group name:x = system_model.value('array_type') print(x)
Set a variable value¶
Use
setattr()
:old_array_type = system_model.SystemDesign.array_type new_array_type = 0 system_design_group_object = getattr(system_model,'SystemDesign') setattr(system_design_group_object, 'array_type', new_array_type ) print(old_array_type) print(system_model.SystemDesign.array_type)
Use
value()
:old_array_type = system_model.SystemDesign.array_type new_array_type = 0 system_model.value('array_type', new_array_type); print(old_array_type) print(system_model.SystemDesign.array_type)
Get and Set Variable Values by Group¶
Use the assign()
and export()
functions to work with groups.
To get a dictionary of all variables in a group, use export()
:
x = system_model.SystemDesign.export()
print(x)
Use assign()
to set variable values by group. For example, this code first uses export()
to get a dictionary x
of variables in the SystemDesign
group so you can see what the group variables and values are. It then uses assign()
to change the value of the azimuth
and gcr
variables in that group, and gets a new dictionary y
:
x = system_model.SystemDesign.export()
print(x)
inputs_to_change = {'azimuth':100, 'gcr': .5}
system_model.SystemDesign.assign(inputs_to_change)
y = system_model.SystemDesign.export()
print(y)