-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathProgram.cs
197 lines (146 loc) · 6.55 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
using BriefFiniteElementNet;
using BriefFiniteElementNet.Elements;
using BriefFiniteElementNet.Materials;
namespace example;
// example-0023
// Show 1-D fem element displacement.
class Program
{
/// <summary>
/// Simple Cantilever FEM model<br/>
/// https://github.com/BriefFiniteElementNet/BriefFiniteElement.Net/blob/b7173d2e3ab8e3896f365d5f1c5ee0943a3d307d/Samples/Examples.CSharp/SimpleCantilever.cs
/// </summary>
static Model BuildFEM_Model()
{
var model = new Model();
var l = 5;
var n1 = new Node(0, 0, 0);
var n2 = new Node(0, 0, l);
var axialLoad = 1000;
var horizontalLoad = 100000;
var f = new Force(horizontalLoad, 0, axialLoad, 0, 0, 0);
/**/
var h = 0.1;
var w = 0.05;
var a = h * w;
var iy = h * h * h * w / 12;
var iz = w * w * w * h / 12;
var j = iy + iz;
var e = 210e9;
var nu = 0.3;
var g = e / (2 * 1 + nu);
/**/
var sec = new BriefFiniteElementNet.Sections.UniformParametric1DSection(a, iy, iz, j);
var mat = UniformIsotropicMaterial.CreateFromYoungShear(e, g);
var belm = new BarElement(n1, n2)
{
Material = mat,
Section = sec,
Behavior = BarElementBehaviours.FullFrame
};
model.Elements.Add(belm);
model.Nodes.Add(n1, n2);
n1.Constraints = Constraints.Fixed;
n2.Loads.Add(new NodalLoad(f));
model.Solve_MPC();
var d = model.Nodes[1].GetNodalDisplacement();
var expectedDx = (horizontalLoad * l * l * l) / (3 * e * iy);
var expectedRy = (horizontalLoad * l * l) / (2 * e * iy);
var expectedDz = axialLoad * l / (e * a);
// var epsilon = 0.0;
// if (Math.Abs(d.DX - expectedDx) > epsilon) throw new NotImplementedException();
// if (Math.Abs(d.RY - expectedRy) > epsilon) throw new NotImplementedException();
// if (Math.Abs(d.DZ - expectedDz) > epsilon) throw new NotImplementedException();
return model;
}
static void Main(string[] args)
{
InitAvalonia();
var w = GLWindow.Create();
w.GLModel.BuildModel = (glCtl, isInitial) =>
{
if (!isInitial) return;
var glModel = glCtl.GLModel;
glModel.Clear();
var fmodel = BuildFEM_Model();
glModel.AddFigure(MakeWCSFigure());
foreach (var node in fmodel.Nodes)
{
var nodeFig = new GLPointFigure(node.ToVector3());
glModel.AddFigure(nodeFig);
var nodeDelta = node.GetNodalDisplacement().ToVector3();
var dnodeFig = new GLPointFigure(node.ToVector3() + nodeDelta).SetColor(Color.Yellow);
glModel.AddFigure(dnodeFig);
foreach (var nodeLoad in node.Loads)
{
var fx = nodeLoad.Force.Fx.ToFloat();
var fy = nodeLoad.Force.Fy.ToFloat();
var fz = nodeLoad.Force.Fz.ToFloat();
var normalizedForce = Vector3.Normalize(new Vector3(fx, fy, fz)) * .3f;
var arrow = new Arrow(node.ToVector3(), node.ToVector3() + normalizedForce, diameterFactor: .3f);
glModel.AddFigure(arrow.Figure(lineMode: true).SetColor(Color.Red));
}
}
foreach (var fel in fmodel.Elements)
{
if (fel is BarElement bar)
{
var glLine = GLLine.FromTo(
fel.Nodes[0].ToVector3(),
fel.Nodes[1].ToVector3(),
Color.Blue);
glModel.AddFigure(new GLLineFigure(glLine));
var N = 10;
{
var section_off = 0d;
var section_off_step = 1d / N;
Vector3? prevDisp = null;
Vector3? barPrevPos = null;
var lines = new List<GLLine>();
var dpts = new List<Vector3>();
var barLine = Line.FromTo(bar.StartNode.ToVector3(), bar.EndNode.ToVector3());
// Iso Parametric Coordination system for BarElement with two nodes
// ref: https://bfenet.readthedocs.io/en/latest/elements/finiteElements/Bar/coords.html#iso-parametric-coordination-system-for-barelement-with-two-nodes
for (int i = 0; i < N + 1; ++i)
{
var xi = (2 * section_off) - 1;
var disp = bar.GetGlobalDisplacementAt(xi).ToVector3();
Debug.WriteLine(Invariant($"section off:[{section_off}] iso ξ:[{xi}] g.disp:[{disp}]"));
var barPos = barLine.From + barLine.V * (float)section_off;
var dpt = barPos + disp;
dpts.Add(dpt);
if (prevDisp is not null && barPrevPos is not null)
lines.Add(GLLine.FromTo(barPrevPos.Value + prevDisp.Value, dpt));
glModel.AddFigure(glModel.MakeTextFigure(
new GLText(XZCS.Move(dpt + .2f * Vector3.UnitX),
Invariant($"{disp}"),
height: .2f,
alignment: GLTextVHAlignment.MiddleLeft)));
prevDisp = disp;
barPrevPos = barPos;
section_off += section_off_step;
}
glModel.AddFigure(new GLLineFigure(lines).SetColor(Color.Yellow));
glModel.AddFigure(new GLPointFigure(dpts).SetPointSize(5).SetColor(Color.Orange));
}
}
}
glCtl.Perspective = false;
glCtl.CameraView(CameraViewType.Front);
};
w.ShowSync();
}
}
public static partial class Ext
{
/// <summary>
/// retrieve location x,y,z as Vector3
/// </summary>
public static Vector3 ToVector3(this BriefFiniteElementNet.Node node) =>
new Vector3(node.Location.X.ToFloat(), node.Location.Y.ToFloat(), node.Location.Z.ToFloat());
/// <summary>
/// retrieve dx,dy,dz as Vector3
/// </summary>
public static Vector3 ToVector3(this Displacement disp) =>
new Vector3(disp.DX.ToFloat(), disp.DY.ToFloat(), disp.DZ.ToFloat());
}