Grasshopper

algorithmic modeling for Rhino

Matlab + Grasshopper

I’m writing this post as a response to this discussion and the comments for this video.  I’ll briefly explain what’s happening in this video and then go through all the steps that you need to connect grasshopper to Matlab for a simple problem.

 

What is that video about?

The video shows an iterative, nearly automated process of architectural form finding based on performative indicators by coupling energy and lighting simulation with genetic algorithms.  Rhino/Grasshopper, Matlab,  EnergyPlus and RADIANCE are used in the process.

Grasshopper is used as the main interface for building the parametric architectural geometry; Radiance and EnergyPlus for evaluating daylighting, heating and cooling loads; and Matlab for importing the geometry, executing simulations, reading the results, and calculating the fitness of each option in relation to the defined objective function.

 

Credits: I learned the core concept of this method for coupling MATLAB and Grasshopper from Dr. Yun Kyu Yi at University of Pennsylvania. Read more here ( Yi Yun Kyu, Malkawi A, “Optimizing building form for energy performance based on hierarchical geometry relation,” In: Automation in Construction 18: 825–833. 2009)

 

OK. Got it!... How can I set-up a similar process for my problem?

Well! First we need to have a simple problem. Let’s do this:

We want Grasshopper to read X, y and Z values of a box generated by MATLAB; create the box, calculate surface area and volume of the box, and write it to a file. Matlab should read the area and the volume; plot the result, and generate new set of parameters for the next box. Let’s do this for 10 boxes.

 

Here is the outline of the process:

  1. The process starts from Matlab. Matlab writes a file that has X, Y and Z values for Grasshopper, and waits for the result file to be prepared by Grasshopper. I name the first file as Matlab2GH.csv, and the second file as GH2Matlab.csv
  2. Grasshopper reads the file (Matlab2GH.csv). Separate the values for X, Y and Z.
  3. Grasshopper creates the box. Calculates the area and the volume, and writes the results as GH2Matlab.csv.
  4. Matlab reads the new file. Deletes the file, plots the results, and then over-writes new values to Matlab2GH.csv
  5. Go back to 1!

 

Note: Obviously this method is not the most sophisticated method that you can use but it is simple enough to set-up easily and fast.

 

So what the steps above means in Matlab/Grasshopper languages:

1. You can generate and write the values to a file from Matlab using something like this:

% generate x, y, z

x = randi(10, 1);

y = randi(10, 1);

z = randi(10, 1);

parameters = [x, y, z];

% write the file

csvwrite(M2GHfileName, parameters)

 

2. Since this file is comma separated you can simply use the script below to read the values and then separate them by item numbers.

 

 

You can also use a Python Script like this to import the data:

parList = open(inputFileAddress, 'r')

 

lineCount = 0

for line in parList:

# in this case the file only has one line

# but you may have multiple lines for multiple parameter sets

# and append them to a list for example

parameters = line.split(',')

 

# you can write it all together as

# parameter0, parameter1, parameter2 = parameters

# but I thought it should be more clear to write it as below

parameter0 = float(parameters[0])

parameter1 = float(parameters[1])

parameter2 = float(parameters[2])

 

lineCount += 1

 

parList.close()

 

3. This is the simple script that calculates area and volume of the box in case you don't know how to do it in Grasshopper.

 

 

4. You can use a simple Python script like this to write the values to GH2Matlab.csv

 

fileName = 'c:\mostapha\GH2Matlab.csv'

 

if outputParameters:

 

# create the file to write

file = open(fileName, 'w')

 

# write the numbers

for number in outputParameters:

file.write(number+ ', ')

 

# close the file

file.close()

 

5. These are the lines that you need in your Matlab script to wait for a file, import the values, delete the file, and plot the result.

 

% wait for the result

checkForFile = 0;

while checkForFile < 1

checkForFile = checkForFile + exist(GH2MfileName,'file');

disp 'waiting for results...'

pause(1)

end

 

% read the results

result = csvread(GH2MfileName);

delete(GH2MfileName);

 

% plot the result

disp 'plot...'

barh(result(1:2))

pause(1)

 

5. Welcome to end of the loop! You don’t need to do anything more! Go back to 1.

 

How this will look all together?

 

Watch this video:

 

 

What can go wrong?

Possibly every single thing can go wrong in the process (??? Law) but the two major things that I remember right now are:

  1. Sometimes Grasshopper doesn’t update the values when Matlab overwrites the file. You can have a counter in Matlab script to email you if no file showed up after a certain amount of time. (Don’t do this except you are really in a rush! You don’t want to go back to office/school at 2 am when you just get home. You can fix it tomorrow! You may think I’m kidding but I’m not!)
  2. If you are running more advanced studies it is always good to have some lines in each loop to check the result and re-run the last study again if the result doesn’t make sense.

 

I uploaded the files so you can try it yourself. Make sure to change file names in Matlab and Grasshopper files.

Hope it helps!

Mostapha

 

simpleExample.m

simpleExample.gh

 

Views: 18404

Comment

You need to be a member of Grasshopper to add comments!

Comment by Lara Alegria Mira on June 11, 2013 at 10:04am

Hi everyone, 

For now I am successful in letting matlab and grasshopper communicate to each other through the writing and reading of .csv files or .txt files. 

I now want to go a step further and introduce this in an optimisation algorithm, meaning many loops from matlab to grasshopper and back. 

Matlab is writing a .csv file that is read into grasshopper. The output in grasshopper is saved as a .txt file and used in matlab. The problem is that I have to put a pause after the csv writing in matlab, because else matlab goes to fast and can't get the correct output from grasshopper. 

Due to this pause, it slows down the process and the optimisation algorithm a lot.

Is this something to be fixed or is it just inherent to the gh-matlab interaction?

Thanks

Lara

 

Comment by Mostapha Sadeghipour Roudsari on January 22, 2013 at 4:02pm

Hi Chintan,

What sort of details do you want to know?

I wrote few lines up there. There is also this diagram that shows the process. It is more recent from what you see in the video but the core concept is the same:

Comment by Chintan Pathak on January 22, 2013 at 4:05am

@Mostapha - Can you share the details about your work that is shown in the video, the Radiance + EP etc. 

Comment by Lara Alegria Mira on January 9, 2013 at 7:12am

Thanks a lot Mostapha for sharing this!

Comment by Dino Rossi on January 8, 2013 at 2:50pm

yes, right at the beginning and then forgot all about it.

everything works perfectly now!

Comment by Mostapha Sadeghipour Roudsari on January 8, 2013 at 8:53am

Hi Dino,

Did you enable Read File component? It seems since Grasshopper's Read File component is reading the file, the file cannot be overwritten.

I think it should work fine if you disable the Read File component.

Please let me know if it solves the problem.

Comment by Dino Rossi on January 8, 2013 at 6:02am

thank you for sharing this.

i downloaded your files and everything works except it does not iterate (the process stops after the first box (or second box on a good run)).

the error arrises in matlab and has to do with the csvwrite function.

this is the message from the command window:

....................... 2 7 8 8

Error using dlmwrite (line 130) Could not open file C:\GH_Matlab\Matlab2GH.csv

Error in csvwrite (line 43) dlmwrite(filename, m, ',', r, c);

Error in simpleExample (line 20) csvwrite(M2GHfileName, parameters)

i have tried to trace back the error through the matlab documentation, but i am not sure why the iteration stops. is there something about csvwrite that doesn't allow it to overwrite the existing file?

thanks,

dino

Comment by Kristoffer Negendahl on January 6, 2013 at 5:04am

Simply fantastic. So helpfull.

Comment by Mostapha Sadeghipour Roudsari on January 5, 2013 at 12:52pm

Nice! I don't know about that actually. I can't think of any use right now but it sounds interesting. I hope someone else can comment on that. Thanks for bringing up the idea.

Comment by Chintan Pathak on January 5, 2013 at 11:05am

@Mostapaha : I meant transferring or referring a non-value, geometrical object like a curve or a surface in Matlab (I am not sure if that makes any sense).

Probably something like in the method 2 illustrated in this post.

About

Translate

Search

Photos

  • Add Photos
  • View All

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service