Solving equation iteratively

Hi everyone,

I am trying to solve an equation iteratively in Grasshopper.

The process would be the following:

  • having an initial guess (first iteration) or input value
  • calculating output value
  • if output is equal to the input by a defined tolerance end process, if not repeat process with output value till convergence is achieved

How would you approach this? I tried to define a C# script but cannot think if anything without recursion…

I also heard of Hoopsnake, would it work for this? (if there is a solution within Grasshopper native components or easy scripting would be better)

Thanks,

Javier

The equation calculates the flow through a pipe based on the pressure at the end points.

This simple equation will later be used to calculate the flow through a more complex system of pipes (which will have to be also solve iteratively, hopefully I can apply the same methodology)

For an equation with one unknown value, you can plug it into Wolfram Alpha
eg https://www.wolframalpha.com/input/?i=solve+x^2%2By+%3D+3+for+x

To solve it iteratively in Grasshopper you could use Galapagos or Goat.

1 Like

Thanks Daniel, I will take a lok at Galapagos and Goat now.

I cant use Wolfram as it needs to be whithin Grasshopper, this little definition will need to be part of a bigger system, all defined in Grasshopper.

BTW, thanks a lot for all your contributions to the forum. I am still learing Grasshopper and I found them very usefull.

Also, I want to add that I am seeking for something computationally efficient. In the final system form, there will be potentially thousands of “pipes”, and each one will need to be solved for every iteration of the system solution.

If you can explain the calculation you need to do it would be easier to help.
Iterations/loops in C# are pretty easy and handy.

Also, sometime i found tricky/cool to solve math by geometric constructions…

I’m suggesting using Alpha once to get an equation, which can involve multiple input parameters, and then make an ‘Evaluate’ component with those inputs.
You wouldn’t need to use Alpha each time if the equation stays the same, and the input values can still change.

For example:

3 Likes

Hi Ricardo,

It is based on the Darcy equation, that calculates pressure loss:

image

Although in my case the pressure differential is driving the flow. In the formula the friction factor also depends on the flow (velocity), and that’s why I cant solve it directly.

pressure_iterative.gh (16.5 KB)

Ok, I didnt understand you before. That makes sense and would be the most effiecient way if the there is a solution for the system of equations… will give it a try.

Anyway, I am also interested in exploring a iterative solution as I would eventually need it for a interconnected system of pipes.

Is this the part you want to iterate/loop?
Can you attach this?
Shouldn’t be that hard converting it in c#…

(You could even use anemone)

Yes sure, it is already attached on my previous comment.

I hope it makes sense what I am trying to do. It is solving for “v”, so at the beggining a “v” value is input and resulting in a “v” output, if this ouput is used as input in a a loop a solution for “v” will be convereged in a number of iterations.

I can’t find it inside that file… can you check?

You are righ! Wrong file!!! :sweat_smile: It should be the correct one now.

Almost just copy-pasting your formulas…
2019-10-11%2016_51_00-Window
pressure_iterative_re.gh (16.2 KB)

private void RunScript(double v, double D, double A, double B, double L, double P1, double P2, double rho, double nu, int i, ref object Final_v) {
for(int counter = 0; counter < i;counter++){
  double Re = (v * D * rho) / nu;
  double f_D = Math.Pow((A * Re), B);
  v = Math.Pow((2 * (P1 - P2) * D / (f_D * L * rho)), 0.5);
}
Final_v = v;
}

You can see how your value stabilize by increasing the loop count slider.
Calc is light-weight, you could even shoot a fixed i=100 and forget about it…

(is importat that your loop have a count limit, otherwise it would loop endlessly and freeze c#, grasshopper, rhino… )

5 Likes

Thanks Riccardo, much appreciated.

I wonder if there is a way of doing this using the original components (formulas), so the script just controls the data flow through the components rather than the formulas being defined in the script? It is what I have in my head originally… still curious if it is possible.

Anemone.

1 Like