Cumulative formula script

grasshopper

(Ian van D) #1

Hi,

I am trying to recreate an excel process in Grasshopper using a script. I was initially looking at foreach loops in a C# component but am struggling. To be honest i don’t mind which language its in, so long as it works!

Below is an image of the excel sheet.
image

The process outputs the data in column C based on all other data which is predefined (ideally as lists/numerical inputs into a Grasshopper component). The formula in C5 is a unique initialisation =A5-B5-B1 then all subsequent items on the list follow the pattern as per C6 which is =IF(C5+A6-B6-$B$1>$B$2,$B$2,C5+A6-B6-$B$1).

Any assistance greatly appreciated

Regards,

Ian


(Lando Schumpich) #2

I assume this should do the trick: cumulativeform.gh (10.9 KB)

Don’t know C# sadly :confused:


(Ian van D) #3

So simple when you know how!

Thanks for the ridiculously fast and accurate response @lando.schumpich


(Ian van D) #4

Hi again,

I’ve been having fun with loops stemming from the conversation above but have run into the following error which has stumped me:

Runtime error (TypeErrorException): ‘float’ object is unsubscriptable
Traceback:
line 126, in script

I’ve attached the .gh below with internalised data for reference. Is there an easy fix to this issue?

Regards

Ian

unscriptable.gh (143.6 KB)


(Lando Schumpich) #5

The problem python is complaining about is this part:

    if chp1Cap[i] == 0:
    
    effCHP1check = genCHP1[i] / chp1Cap

I’m not sure what you are trying to do here.
chp1Cap is supplied by your input as a single float 0.00 so you cant iterate over it with chp1Cap[i].
The if-statement contradicts itself in a way because afterwards you try to divide by chp1Cap which is either a list or 0 which then gives an DivideByZeroError.

I think there might be missing some data can you explain more detailed what you try to achieve with the definition?


(Ian van D) #6

Apologies Lando - heres some more information. the script in that area should actually read:

for i in range(0, len(genCHP1), 1):

effCHP1check = genCHP1[i]

if chp1Cap > 0:
    
    effCHP1check = genCHP1[i] / chp1Cap

but i was just changing conditions to see if it would affect the unsubscriptable error.

I want chp1Cap to be a single number (which i change via a slider) and what i am trying to do is skip over this part of the script if chp1Cap = 0.

hope that makes sense!


(Ian van D) #7

Further, here is a cleaner version of the error - all i would like to happen is for outCHP1 and loadCHP1 to output a list of 8760 0’s if chp1Cap = 0. you will see there is no error if chp1Cap is > 0…

unscriptable2.gh (99.2 KB)


(Lando Schumpich) #8

unscriptable2.gh (98.5 KB)

If you use a similar logic as your first if-statement from the demandelec loop it works.


(Ian van D) #9

Couldn’t see the woods for the trees :slight_smile: Thanks!