In order to make the component do something one has to add code to the “SolveInstance” function in “TensionElimComponents”:
protectedoverridevoidSolveInstance(IGH_DataAccess DA){GH_Model in_gh_model =null;if (!DA.GetData<GH_Model>(0,ref in_gh_model)) return;var model =in_gh_model.Value; // maximum number of iterationsint max_iter =10;DA.GetData<int>(1,ref max_iter); // load case to consider for elimination of elementsint lc_num =0; // clone model to avoid side effects model = (Karamba.Models.Model)model.Clone(); // clone its elements to avoid side effectsmodel.cloneElements(); // clone the feb-model to avoid side effectsmodel.deepCloneFEModel();string singular_system_msg ="The stiffness matrix of the system is singular."; // do the iteration and remove elements with tensile axial forcesfor (int iter =0; iter < max_iter; iter++) { // create an analysis and response object for calculating and retrieving resultsfeb.Deform analysis =newfeb.Deform(model.febmodel);feb.Response response =newfeb.Response(analysis);try { // calculate the displacementsresponse.updateNodalDisplacements(); // calculate the member forcesresponse.updateMemberForces(); }catch { // send an error message in case something went wrongthrownewException(singular_system_msg); } // check the normal force of each element and deactivate those under tensiondouble N, V, M;bool has_changed =false;foreach (var elem inmodel.elems) { // retrieve resultant cross section forceselem.resultantCroSecForces(model, lc_num,out N,out V,out M); // check whether normal force is tensileif (!(N >=0)) continue; // set element inactiveelem.set_is_active(model,false); has_changed =true; } // leave iteration loop if nothing changedif (!has_changed) break; // if something changed inform the feb-model about it (otherwise it won't recalculate)model.febmodel.touch(); // this guards the objects from being freed prematurelyGC.KeepAlive(analysis);GC.KeepAlive(response); } // update model to its final statedouble max_disp =0;try { // create an analysis and response object for calculating and retrieving resultsfeb.Deform analysis =newfeb.Deform(model.febmodel);feb.Response response =newfeb.Response(analysis); // calculate the displacementsresponse.updateNodalDisplacements(); // calculate the member forcesresponse.updateMemberForces(); max_disp =response.maxDisplacement(); // this guards the objects from being freed prematurelyGC.KeepAlive(analysis);GC.KeepAlive(response); }catch { // send an error message in case something went wrongthrownewException(singular_system_msg); } // set up list of true/false values that corresponds to the element statesList<bool> elem_activity =newList<bool>();foreach (var elem inmodel.elems) {elem_activity.Add(elem.is_active); }DA.SetData(0,newGH_Model(model));DA.SetDataList(1, elem_activity);DA.SetData(2, max_disp);}
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: