How to assign numerous series of input vectors to a model?


I have two problems with scripting in grasshopper;

1- I need to assign numerous pre-defined vectors of inputs to different target components. I generated my input space but I don’t know how to run the model (= target components) for each of vectors. To summarize the issue I created these simple components.

2- I think it is possible to call output parameters by their index instead of direct names but I don’t know how to do so.

I read many topics with the same issues in different forums but the high variety of solutions confused me. So I’d really appreciate any clues about the ways that exactly match to my issues.

Thanks in advance.

Hi @farzaneh.fth,

Welcome to the forums!

Instead of using lists with three items as vectors, you could save them as rhinocommon vectors:

import Rhino.Geometry as rg

input_space = [[8, 5, 3], [8, 6, 9], [15, 9, 12]]

input_vectors = []
for i in range(len(input_space)):
    x, y, z = input_space[i]
    input_vectors.append(rg.Vector3d(x, y, z))

print input_vectors

This outputs a list of three vectors that you can pass to the next Python component. Make sure to set the input to List Access, so that the component will read your input data as a list, and call it input_vectors (for the following code to work).

models = []
for vec in input_vectors:
    # You can iterate over the vector coordinates
    for i in vec:
        print i
    # Or simply call them by name
    model = vec.X * vec.Y * vec.Z

print models
1 Like

@diff-arch So many thanks for your time and consideration.

I can’t assign one vector to the target components since they are more than one, and I can’t edit them. for example:

Also, the number of vectors are more than 1000 with a large dimension (more than 20) so It’s not possible to run the model for all of the vectors simultaneously. Then every item within the vector should be generated separately and the model has to be run once for one vector (automatically). I can do this with existing components of the grasshopper in many ways, for example:

But it increases the risk of making mistakes during the simulation process due to the increased number of items to be controlled. So I’m looking for internal coding solutions.

According to my best understanding of the same topics, the “subprocess module” and “ghenv.Component.Params” are the main solutions for my problems, but I didn’t succeed in using them and it seems it takes time to learn to use these modules correctly, so I need to make sure about the selecting right solutions before learning. (of course, learning everything in this context is beneficial but I have time restrictions right now)

Thank you again for your kind response.

I don’t get what you try to do! Could you explain what your model uses the vectors for?
Couldn’t you simply deconstruct your vectors to get their coordinates, wherever they come from, and pass them along to the next components?
What internal coding solution are you exactly looking for?

The purpose is to sensitivity analysis of some quantitative models (e.g. building thermal comfort), as the model is combination of different components with many input parameters and only the final output is the quantity of interest I should deconstruct each set of inputs (=vectors) for feeding each of parameters separately, and then carry analysis on outputs according to input variations.

I’ve created the starting and the ending components and I can perform the whole analysis using other components or writing and reading CSV files during the process. But I want to enhance the quality and accuracy of the procedure by automating the process. To do so I need to return a new set of the inputs for each iteration automatically and record the results independent of the grasshopper recorders/CSV files because they need to be controlled each time and this alleviates the accuracy in case that I forget to control some items due to large volumes of the data.

Would generator expressions in Python help? These are defined in the same way as a function but yield values instead of return ing them.
They can be used to generate your vectors one by one to avoid having to hold them all in memory simultaneously. You can iterate over them like a list.

1 Like

@Dancergraham thank you for your suggestion. Before answering, I should apologize for the wrong report, I clicked on flag while reading the comment inadvertently and I don’t know how to undo :neutral_face: l’m really sorry and I hope there will be a way to undo

I tried the generators too, but no matter which way of generating is used, every script who written within the component just returns the last item.

I do know this that I should use some spacial modules (e.g. subprocess.popen) but learning each of them is too time-consuming for me and I should make sure that the selected solution is appropriate for my issue.