Convert lk to Python 3

  • John
  • Topic Author
More
23 Feb 2022 18:32 #10620 by John
Convert lk to Python 3 was created by John
Paul,

Do you have a script that converts an .lk file to a Python 3 file?

I am trying to determine the most efficient way of running SAM batch simulations in Python. I have used the Python 3 Generate code option in the SAM GUI to create a file that I can run and manipulate in Python. (Great feature). I would like to vary parameters in Python and extract results. However, one of input variables I'm interested in looping over, batt_bank_size_ui, does not appear to be available in the Python code that I generate from the SAM GUI.

From the Parametrics tab in the SAM GUI, I can generate .lk scripts for all of the batches I'm interested in. Presumably, all inputs, calculated and user prescribed, are contained in these .lk files. It doesn't look like it would be too hard to convert these .lk files to Python 3 files (i.e., files with equivalent format to using the Python 3 Generate Code option in the SAM GUI). However, I'm wondering if someone has worked this out already and has sharable code, or if I'm approaching the problem in the wrong way.

Thanks,

John

Please Log in or Create an account to join the conversation.

  • Paul Gilman
More
25 Feb 2022 00:09 #10625 by Paul Gilman
Replied by Paul Gilman on topic Convert lk to Python 3
Hi John,

The Python code that SAM's code generator creates sets the value of inputs to SSC, which is the underlying "SAM Simulation Core" that performs simulations and generates results.

SAM's user interface also sets the value of SSC inputs, but it has some of its own inputs that are not inputs to SSC, such as the batt_bank_size_ui variable you mention. In this case, there are some SAM inputs on the Battery Cell and System input page that allow you to size the battery using a bank power in kW and bank capacity in kWh. Special code in the user interface converts those values into values of Cells in series, Strings in parallel, Max C-rate of charge and Max C-rate of discharge that are the battery sizing inputs to SSC.

When you run an LK script in the SAM user interface, you can access the SAM user interface inputs (like batt_bank_size_ui) and the SSC inputs (like batt_num_cells). But when you interact with SSC in Python, you cannot access the user interface inputs. That means that your idea to convert the LK script that SAM creates from the Parametrics page won't work because it may include SAM user interface inputs.

If you want to be able to specify the battery size using a bank power and bank capacity, you'll have to write your own code in Python to convert those values to SSC inputs.

SAM's user interface uses LK script to make these calculations. You can see that code on the SAM repository on GitHub . For example:

github.com/NREL/SAM/blob/062a09e263f34b32846c5d801ae23619f60092d9/deploy/runtime/ui/Battery%20Bank%20Sizing.txt#L1151

github.com/NREL/SAM/blob/062a09e263f34b32846c5d801ae23619f60092d9/deploy/runtime/ui/Battery%20Current%20and%20Capacity.txt#L2377

Best regards,
Paul.

Please Log in or Create an account to join the conversation.

  • John
  • Topic Author
More
26 Feb 2022 15:52 #10628 by John
Replied by John on topic Convert lk to Python 3
Paul,

Thanks very much for your reply. I appreciate your helpful explanations.

I have found an elaborate workaround:
-Use SAM GUI to generate Python 3 code for the base PV+BESS model I'm working with
-Use SAM GUI Parametrics to generate the list of simulation inputs and outputs of interest
-Use SAM's utility to convert this list to lk files
-Open lk files in Python and determine the differences between each file and the base file
-Use differences to update Python 3 base model code
-Iterate over updated Python 3 base model code and perform desired optimizations

John

Please Log in or Create an account to join the conversation.

Moderators: Paul Gilman
Powered by Kunena Forum