Annual Energy Output for all years in VBA Code

  • Paul Gilman
More
05 Jan 2022 18:10 #10470 by Paul Gilman
Replied by Paul Gilman on topic Annual Energy Output for all years in VBA Code
Hi Soheb,

The system_to_grid variable is an array, so you should use VBASSCDataGetArray() instead of VBASSCDataGetNumber().

If you want to calculate the annual value of system_to_grid, you can add up the values in the array. The length of the array is determined by the simulation time step and the analysis period. For example, an hourly simulation with analysis_period = 25 would result in an array with 8760 hours/year * 25 years = 219,000 values. You could add up all 219,000 values to calculate the total value over the analysis period, or add up the first 8760 values to calculate the total for the first year.

The SSC guide available from the link on sam.nrel.gov/software-development-kit-sdk.html provides a reference to many of the SSC functions. The arrays for reading and writing to arrays are described in Section 3.7.2. The guide describes the functions as they are implemented in C. You can use the function definitions in the .bas file that SAM's code generator creates to see how the C functions are translated into VBA.

Best regards,
Paul.

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

  • Soheb Zaidi
  • Topic Author
More
06 Jan 2022 14:17 #10474 by Soheb Zaidi
Replied by Soheb Zaidi on topic Annual Energy Output for all years in VBA Code
Hi Paul,
Thanks for that. Unfortunately, that doesn't fixes the problem.

Changing from VBASSCDataGetNumber to VBASSCDataGetArray gives an error and fails to compile the code. (Screenshot attached)

If this function had been working it would have sorted all my problems.

Kind Regards,
Soheb

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

  • Paul Gilman
More
07 Jan 2022 23:07 #10492 by Paul Gilman
Replied by Paul Gilman on topic Annual Energy Output for all years in VBA Code
Hi Soheb,

Section 3.7.2 of the SSC Guide explains that the ssc_data_get_array() function has three parameters instead of two:
int len = 0;
const ssc_number_t *array = ssc_data_get_array( data, "xx", &len );

Here is a screenshot from Page 16 of the SSC Guide:



And, here is a screenshot the function definition in the .bas file from SAM's Code Generator, which should also be in your file:



Best regards,
Paul.

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

  • Soheb Zaidi
  • Topic Author
More
12 Jan 2022 05:00 #10504 by Soheb Zaidi
Replied by Soheb Zaidi on topic Annual Energy Output for all years in VBA Code
Hi Paul,

Thanks for the feedback, I tried using the function definition available in VBA, but was not able print output for VBASSCDataGetArray (p_data, name, data(0), count).

My concerns are as follows:

- What should I enter in place of data(0) here?
- How is p_data calculated/assigned ? we don't really see the back calculations going into p_data in the VBA code.
- Can you show me an example of an array output from a VBA code from SAM, that would be an ideal starting point to investigate this issue further.

Appreciate your help! 

Kind regards.

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

  • Paul Gilman
More
13 Jan 2022 00:39 - 13 Jan 2022 00:40 #10507 by Paul Gilman
Replied by Paul Gilman on topic Annual Energy Output for all years in VBA Code
Hi Soheb,

I'm not very experienced with VBA, but I did manage to get this to work:
    VBASSCDataGetArray p_data, "monthly_energy", ary
    Dim i As Integer
    i = 0
    Do
        Debug.Print "Monthly Energy for Month " & i + 1 & " = " & ary(i)
        i = i + 1
    Loop Until (i = 12)
    Exit Sub

I reused 'ary' from the lines of code before this. It is defined as:
Dim ary() As Double

As a note to others, to access the SSC library (ssc.dll) from VBA in Excel, you have to use the 64-bit version of Excel because ssc.dll is a 64-bit library.

Best regards,
Paul.
Last edit: 13 Jan 2022 00:40 by Paul Gilman.

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

  • Soheb Zaidi
  • Topic Author
More
17 Jan 2022 08:51 #10511 by Soheb Zaidi
Replied by Soheb Zaidi on topic Annual Energy Output for all years in VBA Code
Thanks for that Paul!

For any one struggling with this here is the VBA code to extract 8760 Hourly PV gen to grid.
Make sure to call the simulation before Call RunCase()

VBASSCDataGetArray p_data, "gen", ary()
        Dim pvgen As Variant
        Set pvgen = Sheets("Hourly").Range("A1")
'Set which spreadsheet you want to output the values.
        Dim i As Long
        For i = 0 To 8759 'Set this to 218999 to extract for 25 years.... 8760 x no. of years.
            pvgen.Offset(i, 0).value = ary(i)
        Next i

Happy Simulating & Coding.
Attachments:

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

Moderators: Paul Gilman
Powered by Kunena Forum