Scripting Guide 2.2.0

Search…

2. Scripting with Karamba3D inside Grasshopper

3. How to create your own Karamba3D Grasshopper-component

2.4.1: Cross section Optimization

When it comes to modifying an existing Karamba3D model keep to these general rules:

- In order to avoid side-effects, clone objects before modifying them. This applies recursively to the objects which contain objects to be modified.
- The Karamba3D API for modifications allows for changes which can make the C++-model crash. So make it a habit to regularly save your work.

The example “**CrossSectionOptimization.gh**” (see fig. 2.4.1.1) shows how to optimize beam cross sections under arbitrary loads. The C#-component takes a model and an ordered list of cross sections as the main input, chooses the optimum cross sections according to the given cross section forces and outputs the optimized model as well as its maximum displacement. The input-plug **“niter”** lets one chose the number of iteration steps. Each consists of model evaluation and cross section selection. With **“lcind”** the index of a load-case to be considered for cross section optimization can be selected. The algorithm inside the component neglects buckling and calculates the maximum stress in the cross section based on the assumption that there are only normal forces and bending moments about the local Y-axis.

Fig. 2.4.1.1: Modification of cross sections.

1

...

2

using Karamba.Models;

3

using Karamba.CrossSections;

4

using Karamba.Elements;

5

using Karamba.Results;

6

...

7

private void RunScript(object Model_in, List<object> CroSecs_in, int niter, int lcind, ref object Model_out, ref object Disp_out)

8

{

9

var model = Model_in as Model;

10

if (model == null) {

11

throw new ArgumentException("The input in 'Model_in' is not of type Karamba.Models.Model!");

12

}

13

14

var crosecs = new List<CroSec_Beam>(CroSecs_in.Count);

15

foreach (var item in CroSecs_in) {

16

var crosec = item as CroSec_Beam;

17

if (crosec == null) {

18

throw new ArgumentException("The input in 'CroSecs_in' contains objects which are not of type Karamba.CrossSections.CroSec_Beam!");

19

}

20

crosecs.Add(crosec);

21

}

22

23

var k3d = new KarambaCommon.Toolkit();

24

List<double> max_disp;

25

List<double> out_g;

26

List<double> out_comp;

27

string message;

28

List<List<double>> N;

29

List<List<double>> V;

30

List<List<double>> M;

31

32

// avoid side effects

33

model = model.Clone();

34

model.cloneElements();

35

36

for (int i = 0; i < niter; ++i) {

37

model = k3d.Algorithms.AnalyzeThI(model, out max_disp, out out_g, out out_comp, out message);

38

39

for (int elem_ind = 0; elem_ind < model.elems.Count; ++elem_ind) {

40

var beam = model.elems[elem_ind] as ModelBeam;

41

if (beam == null) continue;

42

43

// avoid side effects

44

beam = (ModelBeam) beam.Clone();

45

model.elems[elem_ind] = beam;

46

47

BeamResultantForces.solve(model, new List<string> {"" + elem_ind}, model.lc_combinator.load_case_ids[lcind], 100, 1,

48

out N, out V, out M);

49

50

for (int crosec_ind = 0; crosec_ind < crosecs.Count; ++crosec_ind) {

51

var crosec = crosecs[crosec_ind];

52

beam.crosec = crosec;

53

var max_sigma = Math.Abs(N[lcind][0]) / crosec.A + M[lcind][0] / crosec.Wely_z_pos;

54

if (max_sigma < crosec.material.ft()) break;

55

}

56

}

57

58

model.initMaterialCroSecLists();

59

model.buildFEModel();

60

}

61

62

model = k3d.Algorithms.AnalyzeThI(model, out max_disp, out out_g, out out_comp, out message);

63

64

Disp_out = new GH_Number(max_disp[lcind]);

65

66

Model_out = new Karamba.GHopper.Models.GH_Model(model);

67

}

Copied!

In the first two paragraphs of source-code the type of the **“Model_in”** and **“CroSecs_in”** input gets checked. The third block consists of instantiations of objects which are used later when looping over the model’s elements.

When C# assigns an object to a variable it actually assigns a reference. Changes made on the object are thus visible in all variables that also reference that object. In Grasshopper this breaks the data flow logic that an object is only influenced by upstream operations. Whenever an object gets plugged into two different components a change in one object would magically transfer to the other object. In order to avoid this, the actual data and not only references need to be assigned and copied. This is done in line 33 for the model and in line 36 for its list of elements.

The loop in line 39 cycles over the optimization iterations and starts with calculating the system response. Starting in line 41 all elements get checked whether they epitomize beam-elements. If yes the beam gets cloned (see line 44) to avoid side-effects and re-enlisted in the model.

For cross section-optimization the beam’s resultant cross section forces are determine in line 47. One could have used **“Karamba.Results.BeamForces.solve()”** here instead for considering e.g. bi-axial bending. The loop which follows goes through the list of user-provided cross sections and chooses an appropriate one by comparing the maximum stress in a cross section against the cross section’s material strength.

After completing cross section selection, the iteration step ends with initializing the model’s lists of cross sections and materials (line 58) and recreation of the C++ model on which the C# model depends for result evaluation (line 59).

Before handing over the model to the output-plug an analysis step updates the structural response and determines the model’s maximum displacement.

The above C# script could be improved in many ways i.e. by providing a stop criteria for the optimization iterations or more elaborate design procedures for determining the load-bearing capacity of the elements. Yet this was omitted for the the sake of simplicity.

CrossSectionOptimization.gh

29KB

Binary

2. Scripting with Karamba3D inside Grasshopper - Previous

2.4: How to Modify Structural Models

Next

2.4.2: Activation and Deactivation of Elements

Last modified 16d ago

Copy link