BriefFiniteElementNet / BriefFiniteElement.Net

BriefFiniteElementDotNET (BFE.NET) is a library for linear-static Finite Element Method (FEM) analysis of solids and structures in .NET

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

v2.0.7 NullReferenceException with TrussExample

MovGP0 opened this issue · comments

Describe the bug

Getting a NullReferenceException when trying the Small 3D Truss Example from the documentation.

Note there seem to be serval errors in the documentation:

  • the class TrussElement2Node as described in the documentation does not exist
  • the material is set to null in the example. Maybe something else is missing in the documentation too?
  • the examples from the documentation should be verified by an unit test. I was unable to find one.

To Reproduce

using BriefFiniteElementNet;
using BriefFiniteElementNet.Elements;
using BriefFiniteElementNet.Materials;
using BriefFiniteElementNet.Sections;
using UnitsNet.Units;
// Initiating Model, Nodes and Members
var model = new Model();

var n1 = new Node(1, 1, 0) { Label = "n1" };
var n2 = new Node(-1, 1, 0) { Label = "n2" };
var n3 = new Node(1, -1, 0) { Label = "n3" };
var n4 = new Node(-1, -1, 0) { Label = "n4" };
var n5 = new Node(0, 0, 1) { Label = "n5" };

/*Young's Modulus*/
var e = UnitsNet.Pressure.FromGigapascals(120.0).Pascals;

/*Density*/
var rho = UnitsNet.Density.FromGramsPerCubicCentimeter(2.2).KilogramsPerCubicMeter;

/*Shear Modulus*/
var mu = UnitsNet.Pressure.FromGigapascals(20).Pascals;

/*Poisson's Ratio*/
var nu = 0.25;

var carbonFibre = new UniformAnisotropicMaterial
{
	Ex = e,
	Ey = e,
	Ez = e,
	Rho = rho,
	Mu = mu,
	NuXy = nu,
	NuXz = nu,
	NuYx = nu,
	NuYz = nu,
	NuZx = nu,
	NuZy = nu
};

var e1 = new BarElement(n1, n5) { Label = "e1", Behavior = BarElementBehaviours.Truss, Material = carbonFibre };
var e2 = new BarElement(n2, n5) { Label = "e2", Behavior = BarElementBehaviours.Truss, Material = carbonFibre };
var e3 = new BarElement(n3, n5) { Label = "e3", Behavior = BarElementBehaviours.Truss, Material = carbonFibre };
var e4 = new BarElement(n4, n5) { Label = "e4", Behavior = BarElementBehaviours.Truss, Material = carbonFibre };

model.Nodes.Add(n1, n2, n3, n4, n5);
model.Elements.Add(e1, e2, e3, e4);

//Applying restrains
n1.Constraints = n2.Constraints = n3.Constraints = n4.Constraints = Constraints.Fixed;
n5.Constraints = Constraints.RotationFixed;

//Adds a NodalLoad with Default LoadCase
var force = new Force(fx: 0, fy: 1000, fz: -1000, /*momentum*/mx: 0, my: 0, mz: 0);
n5.Loads.Add(new NodalLoad(force)); //adds a load with LoadCase of DefaultLoadCase to node loads

// Solve model
model.Solve();

var r1 = n1.GetSupportReaction();
var r2 = n2.GetSupportReaction();
var r3 = n3.GetSupportReaction();
var r4 = n4.GetSupportReaction();

var rt = r1 + r2 + r3 + r4;

Console.WriteLine($"Total reactions SUM: {rt}");

Expected behavior

The reaction forces should be calculated.

Additional context

model.Solve(); throws a NullReferenceException

Stacktrace

 at BriefFiniteElementNet.ElementHelpers.ElementHelperExtensions.CalcLocalKMatrix_Bar(IElementHelper helper, Element targetElement)
   at BriefFiniteElementNet.ElementHelpers.TrussHelper.CalcLocalStiffnessMatrix(Element targetElement)
   at BriefFiniteElementNet.Elements.BarElement.GetLocalStifnessMatrix()
   at BriefFiniteElementNet.Elements.BarElement.GetGlobalStifnessMatrix()
   at BriefFiniteElementNet.MatrixAssemblerUtil.AssembleFullStiffnessMatrix(Model model)
   at BriefFiniteElementNet.StaticLinearAnalysisResult.AddAnalysisResult(LoadCase loadCase)
   at BriefFiniteElementNet.StaticLinearAnalysisResult.AddAnalysisResultIfNotExists(LoadCase cse)
   at BriefFiniteElementNet.Model.Solve(SolverConfiguration config)
   at BriefFiniteElementNet.Model.Solve(ISolverFactory factory)
   at BriefFiniteElementNet.Model.Solve(BuiltInSolverType solverType)
   at BriefFiniteElementNet.Model.Solve()
   at UserQuery.Main(), line 54

Hi there,
I think you should also define a section for your element like this:

var section = new UniformParametric1DSection() {A = 0.01*0.01};//for a 1cm by 1cm section

e1.Section = section;

The material property defines the mechanical properties of element, but section defines geometrical properties of bar. both are needed.

Can you please have a check?
thanks

The following code works:

var sectionWidth = UnitsNet.Length.FromCentimeters(1).Meters;
var section = new UniformParametric1DSection() {A = sectionWidth * sectionWidth};

var e1 = new BarElement(n1, n5) { Label = "e1", Behavior = BarElementBehaviours.Truss, Material = carbonFibre, Section = section };
var e2 = new BarElement(n2, n5) { Label = "e2", Behavior = BarElementBehaviours.Truss, Material = carbonFibre, Section = section };
var e3 = new BarElement(n3, n5) { Label = "e3", Behavior = BarElementBehaviours.Truss, Material = carbonFibre, Section = section };
var e4 = new BarElement(n4, n5) { Label = "e4", Behavior = BarElementBehaviours.Truss, Material = carbonFibre, Section = section };

Guess the documentation needs an update in that regard.

actually documentation uses TrussElement2Node which is prior version. the BarElement is a little different. example needs to be updated with BarElement.
Thanks anyways.

@MovGP0 are you using example from CodeProject article?
this is newer version:
https://bfenet.readthedocs.io/en/latest/example/truss3d/index.html