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:
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:
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
Comment
Which one is better grasshopper vs matlab if we have time to learn only one software?
Feels great seeing this after years. Wouldn't it be safe to say ARCH 632 002 by our dear Prof. Yun Kyu Yi was when Ladybug actually started? And the rest is history.
Amazing
Thanks for the share Mostapha :) this is great!
Hi Mostapha
I need to make a nonlinear interpolation between multiple(~10) data lookups from GH using matlab.
I see. If you don't need the results of each run for the next one, then animating a slider should does it for you. If that's not the case then I would use another instance of Grasshopper to initiate the loop using an external text file. I don't know the process is going to be any different. Why do you actually want to start the process from Grasshopper and not Matlab? [Thinking loud obviously! :)]
I don't need to optimize, I just need to run various matlab functions.. e.g. Matrix calculations, nonlinear interpolation etc
Hi Thomas, This process is around 2.5 years old. Now you can replace Matlab with Octopus, and start the process from the Grasshopper/Octopus.
Would it be possible to modify this method and start the process in Grasshopper instead of matlab? If it is possible how would it be done? :-)
Perhaps a single C# or Python component:
input 1: Function in libery to call/run
input 2: Input parametres
output 1: Function results
Hi Estefania! Good to know that it helps.
Hi Lara! How long do you pause the system? There are indeed more sophisticated ways to check for the .txt/.csv file. I don't have Matlab on my system to check it right now but you can either watch for the changes (similar to what grasshopper read file component does) or alternatively make a while loop and check for the size of the file (if you know how big the file is). In the cases that I have tried it usually is not big of a deal to have a short pause in the process, as the analyses part takes for few minutes for each case.
Hope it helps.
Added by Parametric House 1 Comment 0 Likes
Added by Parametric House 1 Comment 0 Likes
© 2020 Created by Scott Davidson. Powered by
You need to be a member of Grasshopper to add comments!
Join Grasshopper