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, lc_num, out N, out V, out M);
52
// check whether normal force is tensile
53
if (!(N >= 0)) continue;
54
// set element inactive
55
elem.set_is_active(model, false);
56
has_changed = true;
57
}
58
59
// leave iteration loop if nothing changed
60
if (!has_changed) break;
61
62
// if something changed inform the feb-model about it (otherwise it won't recalculate)
63
model.febmodel.touch();
64
65
// this guards the objects from being freed prematurely
66
GC.KeepAlive(analysis);
67
GC.KeepAlive(response);
68
}
69
70
// update model to its final state
71
double max_disp = 0;
72
try
73
{
74
// create an analysis and response object for calculating and retrieving results
75
feb.Deform analysis = new feb.Deform(model.febmodel);
76
feb.Response response = new feb.Response(analysis);
77
78
// calculate the displacements
79
response.updateNodalDisplacements();
80
// calculate the member forces
81
response.updateMemberForces();
82
83
max_disp = response.maxDisplacement();
84
85
// this guards the objects from being freed prematurely
86
GC.KeepAlive(analysis);
87
GC.KeepAlive(response);
88
}
89
catch
90
{
91
// send an error message in case something went wrong
92
throw new Exception(singular_system_msg);
93
}
94
95
// set up list of true/false values that corresponds to the element states
96
List<bool> elem_activity = new List<bool>();
97
foreach (var elem in model.elems)
98
{
99
elem_activity.Add(elem.is_active);
100
}
101
102
DA.SetData(0, new GH_Model(model));
103
DA.SetDataList(1, elem_activity);
104
DA.SetData(2, max_disp);
105
}
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.
Last modified 8mo ago
Copy link