3.5: Adding Functionality to a GH Component
In order to make the component do something one has to add code to the “SolveInstance” function in “TensionElimComponents”:
1
protected override void SolveInstance(IGH_DataAccess DA)
2
{
3
GH_Model in_gh_model = null;
4
if (!DA.GetData<GH_Model>(0, ref in_gh_model)) return;
5
var model = in_gh_model.Value;
6
7
// maximum number of iterations
8
int max_iter = 10;
9
DA.GetData<int>(1, ref max_iter);
10
11
// load case to consider for elimination of elements
12
int lc_num = 0;
13
14
// clone model to avoid side effects
15
model = (Karamba.Models.Model)model.Clone();
16
17
// clone its elements to avoid side effects
18
model.cloneElements();
19
20
// clone the feb-model to avoid side effects
21
model.deepCloneFEModel();
22
23
string singular_system_msg = "The stiffness matrix of the system is singular.";
24
25
// do the iteration and remove elements with tensile axial forces
26
for (int iter = 0; iter < max_iter; iter++)
27
{
28
// create an analysis and response object for calculating and retrieving results
29
feb.Deform analysis = new feb.Deform(model.febmodel);
30
feb.Response response = new feb.Response(analysis);
31
32
try
33
{
34
// calculate the displacements
35
response.updateNodalDisplacements();
36
// calculate the member forces
37
response.updateMemberForces();
38
}
39
catch
40
{
41
// send an error message in case something went wrong
42
throw new Exception(singular_system_msg);
43
}
44
45
// check the normal force of each element and deactivate those under tension
46
double N, V, M;
47
bool has_changed = false;
48
foreach (var elem in model.elems)
49
{
50
// retrieve resultant cross section forces
51
elem.resultantCroSecForces(model, new LCSuperPosition(lc_num, model),
52
out N, out V, out M);
53
// check whether normal force is tensile
54
if (!(N >= 0)) continue;
55
// set element inactive
56
elem.set_is_active(model, false);
57
has_changed = true;
58
}
59
60
// leave iteration loop if nothing changed
61
if (!has_changed) break;
62
63
// if something changed inform the feb-model about it (otherwise it won't recalculate)
64
model.febmodel.touch();
65
66
// this guards the objects from being freed prematurely
67
GC.KeepAlive(analysis);
68
GC.KeepAlive(response);
69
}
70
71
// update model to its final state
72
double max_disp = 0;
73
try
74
{
75
// create an analysis and response object for calculating and retrieving results
76
feb.Deform analysis = new feb.Deform(model.febmodel);
77
feb.Response response = new feb.Response(analysis);
78
79
// calculate the displacements
80
response.updateNodalDisplacements();
81
// calculate the member forces
82
response.updateMemberForces();
83
84
max_disp = response.maxDisplacement();
85
86
// this guards the objects from being freed prematurely
87
GC.KeepAlive(analysis);
88
GC.KeepAlive(response);
89
}
90
catch
91
{
92
// send an error message in case something went wrong
93
throw new Exception(singular_system_msg);
94
}
95
96
// set up list of true/false values that corresponds to the element states
97
List<bool> elem_activity = new List<bool>();
98
foreach (var elem in model.elems)
99
{
100
elem_activity.Add(elem.IsActive);
101
}
102
103
DA.SetData(0, new GH_Model(model));
104
DA.SetDataList(1, elem_activity);
105
DA.SetData(2, max_disp);
106
}
Copied!
In line 3 a wrapper-object for a Karamba3D model gets initialized to null and set to the value of the input plug of index “0”. The “if” statement in line 4 checks whether there is any data to process. In line 5 the Karamba3D-model gets retrieved from the GH-wrapper. In lines 8 to 9 the value of the “maxiter” plug-in gets read.
All the rest down to line 101 constitutes more or less a replica of the code of section 2.4.2. Lines 102 to 104 transfer the results of the algorithm to the output-plugs.
The example “ActivationDeactivationOfElements_CustomComponent” (see fig. 3.5.1) shows that the results using the GH custom component are the same as in section 2.4.2.
A comparison with the listing in section 2 shows that only the first and last parts differ significantly:
Fig. 3.5.1: Elimination of elements under tension. this time using a custom GH-component.
Visual Studio Project File:
TensionElim.zip
10KB
Binary
Last modified 2mo ago
Copy link