Grasshopper

algorithmic modeling for Rhino

Hi Everybody,

I think I have a "rookie" mistake, but I can´t find the way to fix it. I am just trying to do an iterative process, which involves a function that I have create it to solve a non-linear system of equations (Conjugate Gradient Method).

The point is, that I am trying to make an iterative process to find a solution, which involves invoking this function in a for loop. When I do this, the program saves the new values on my previous variable for the solution, although I have created a new variable to save the new solution.

I attach the code as a text file. In this case (is a simplification of what I want to do) I have two vectors of forces (b and f, with their respectives components X,Y and Z) and two solutions "one" and "two" (with their respectives components X,Y,Z). When I try to recover the value of "one", the program gives me the value of "two".

If someone can help me I will really appreciate it. I know that is a "rookie" mistake and it has something to do with the declaration of variables, but I can´t find the solution (I am not a programmer).

Thank you in advance.

Views: 550

Attachments:

Replies to This Discussion

Hi Jose,

you're using Array as a parameter type*, your functions don't have a return type code** and your variables have really awkward names such as n_row, aux_1, iai, iai1, Dp, pDp, b_x, b_y, c, x_rel, x_sol etc. You'll have to clean up your code until the function and variables names are either self-documenting or have documentation comments associated with them. My brain refuses to parse code like this.

* Don't use Array as a function argument unless you absolutely have to. Double() or Int32() is to be preferred.

** Your functions should always include an As clause with return type code.

--

David Rutten

david@mcneel.com

Tirol, Austria

Hi David,

thank you for your advices. It is a really awkward code, I know. I just try to prove that the method works and I have a lot of work to do cleaning it. I will try to follow your indications. I am very new on coding, so I expect to improve it.

Thank you again.

Hi David,

I just realized that I have some problems to deal qith the variables of VB. Can you recommend me a manual to learn the basics? I would really appreciate that. I don´t really understand why it is prefereable not to use Arrays. My program deals with some vectors and matrices and I thought that was the proper way to store the information, but surely I am wrong.

Thank you again for the tips!

Regards.

While in RhinoCommon, there are specific types for points and vectors and they are not based on arrays. Matrices often are, and of course you can represent points and vectors as arrays, but it is not a good idea to use the Array type directly. It's not type-safe. Use Double() instead since it specifically represents an Array of Doubles.

Here are my rules for declaring and using variables in VB.NET. Some of them are personal preference, others are widely accepted standards:

  • Declare variables as late as possible. Don't put all of them at the top of a function, but rather declare them just before you use them for the first time.
  • Declare iteration directly in the loop if possible. I.e. instead of:
      Dim i As Int32
      For i = 0 To N
    Do this:
      For i As Int32 = 0 To N
  • Give your variables descriptive names. Sometimes it's ok to use names like i, k, c or r, but by and large it's better to use iteration, kConstant, colour and radius.
  • Local and argument variables should be camel cased, starting with a lower case letter. Thus iterationCount and curveOffset rather than Iteration_Count and crvoffset.
  • Don't use abbreviations or acronyms like arr, crv, srf when you could just as well use array, curve and surface.
  • Declare your variable types as loosely as possible, but no looser. Always keep them type-safe whenever possible. If a function iterates over a list of doubles using the For Each loop, don't ask for an Array, Double(), List<Double>, IList<Double>, ..., but ask for IEnumerable<Double>.
  • I prefer to use the m_ prefix for declaring class level variables, but note that this is discouraged in the .NET naming conventions. It's a left-over from hungarian notation that I've stuck with. You're probably better off using pascal casing to differentiate them from method level variables.

There's probably more but I can't think of any right now.

--

David Rutten

david@mcneel.com

Tirol, Austria

Hi Jose,

how did your work go further? I am actually trying the same thing as you did. But I wanted to use an already existing library in C# (like alglib). It turns out to be quite tricky to implement these libraries and I hope to be able to do it in time. How long did it take you to get the script working?

Cheers
Benjamin

RSS

About

Translate

Search

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service