public void derivation_test() { var p = new SingleVariablePolynomial(9, 8, 7, 6, 5, 4, 3, 2, 1, 0); var p1_1 = p.GetDerivative(2); var p1_2 = p.GetDerivative(1).GetDerivative(1); Assert.AreEqual(p1_2, p1_1, ""); }
/// <inheritdoc/> public Matrix GetNMatrixAt(Element targetElement, params double[] isoCoords) { //note: this method gives the shape function on variable node count beam with variable constraint on each node SingleVariablePolynomial[] nss = null; SingleVariablePolynomial[] mss = null; var bar = targetElement as BarElement; if (!GetShapeFunctions(targetElement, out nss, out mss)) { throw new Exception(); } var n = bar.NodeCount; var xi = isoCoords[0]; var buf = targetElement.MatrixPool.Allocate(4, 2 * n); for (var i = 0; i < n; i++) { if (nss[i] == null) { nss[i] = new SingleVariablePolynomial(); } if (mss[i] == null) { mss[i] = new SingleVariablePolynomial(); } } for (var i = 0; i < n; i++) { var ni = nss[i]; var mi = mss[i]; for (var ii = 0; ii < 4; ii++) { buf[ii, 2 * i + 0] = ni.EvaluateDerivative(xi, ii); buf[ii, 2 * i + 1] = mi.EvaluateDerivative(xi, ii); } } return(buf); }
public void interpolation_test() { var tpls = new Tuple <double, double>[] { Tuple.Create(-5.0, 3.0), Tuple.Create(-4.0, 4.0), Tuple.Create(-3.0, 7.0), Tuple.Create(1.0, 0.0), Tuple.Create(5.0, 0.0) }; var p = SingleVariablePolynomial.FromPoints(tpls); var epsilon = 1e-10; foreach (var tpl in tpls) { var diff = Math.Abs(p.Evaluate(tpl.Item1) - tpl.Item2); Assert.IsTrue(diff < epsilon); } }
public Matrix GetNMatrixAt(Element targetElement, params double[] isoCoords) { var br = targetElement as BarElement; var xi = isoCoords[0]; SingleVariablePolynomial[] ns = null; { //retrieve or generate shapefunctions var nsKey = "011F9D96-6398-4126-BC4E-FFDA7F91D6E3"; //a random unified key for store truss shape functions for bar element object obj; targetElement.TryGetCache(nsKey, out ns); if (ns == null) { ns = new SingleVariablePolynomial[targetElement.Nodes.Length]; for (var i = 0; i < ns.Length; i++) { ns[i] = GetN_i(targetElement, i); } targetElement.SetCache(nsKey, ns); } } //var buf = new Matrix(2, br.Nodes.Length); var buf = //new Matrix(2, ns.Length); targetElement.MatrixPool.Allocate(2, ns.Length); {//fill buff for (var i = 0; i < ns.Length; i++) { buf[0, i] = ns[i].EvaluateDerivative(xi, 0); buf[1, i] = ns[i].EvaluateDerivative(xi, 1); } } return(buf); }
public SingleVariablePolynomial GetN_i(Element targetElement, int ith) { var bar = targetElement as BarElement; if (bar == null) { return(null); } var n = bar.NodeCount; var xis = new Func <int, double>(i => { var delta = 2.0 / (n - 1); return(-1 + delta * i); }); var conditions = new List <Tuple <double, double> >(); for (var i = 0; i < n; i++) { if (bar._nodalReleaseConditions[i].DX == DofConstraint.Fixed) { conditions.Add(Tuple.Create(xis(i), ith == i ? 1.0 : 0.0)); } } var condCount = conditions.Count; var condMtx = new Matrix(condCount, condCount); var rMtx = new Matrix(condCount, 1); for (var i = 0; i < condCount; i++) { var rw = new double[condCount]; var cond = conditions[i]; for (var j = 0; j < condCount; j++) { var origPow = condCount - 1 - j; rw[j] = Math.Pow(cond.Item1, origPow); } condMtx.SetRow(i, rw); rMtx.SetRow(i, cond.Item2); } var res = condMtx.Inverse() * rMtx; var buf = new SingleVariablePolynomial(res.CoreArray); { //test var epsilon = 0.0; for (var i = 0; i < condCount; i++) { var cond = conditions[i]; var d = buf.Evaluate(cond.Item1) - cond.Item2; epsilon = Math.Max(epsilon, Math.Abs(d)); } if (epsilon > 1e-7) { throw new Exception(); } } return(buf); }
//private readonly string mssKey = "82069A88-26BD-4902-9CA6-7AE324193FE3:X"; //private readonly string nssKey = "0EECCFF2-8CAE-4D65-935F-15E1AF31709B:X" ; public bool GetShapeFunctions(Element targetElement, out SingleVariablePolynomial[] nss, out SingleVariablePolynomial[] mss) { nss = null; mss = null; var mssKey = "7AE324193FE3:X" + this.Direction; var nssKey = "15E1AF31709B:X" + this.Direction; var sb = new StringBuilder(); var bar = targetElement as BarElement; var n = bar.NodeCount; { var xis = new Func <int, double>(i => { var delta = 2.0 / (n - 1); return(-1 + delta * i); }); bar.TryGetCache(mssKey, out mss); bar.TryGetCache(nssKey, out nss); if (nss != null && mss != null) { return(true); } } var cnds = GetShapeFunctionConditions(bar); var grpd = cnds.GroupBy(i => Tuple.Create(i.NodeNumber, i.Type)).ToArray(); CondsListPool.Free(cnds); mss = new SingleVariablePolynomial[n]; nss = new SingleVariablePolynomial[n]; foreach (var grp in grpd) { var nodeNum = grp.Key.Item1; var tp = grp.Key.Item2; var condCount = grp.Count(); var mtx = bar.MatrixPool.Allocate(condCount, condCount); //new Matrix(condCount, condCount); var rightSide = bar.MatrixPool.Allocate(condCount, 1); //new Matrix(condCount, 1); var arr = grp.ToArray(); for (var i = 0; i < arr.Length; i++) { var itm = arr[i]; mtx.SetRow(i, Diff(itm.Xi, condCount - 1, itm.DifferentialDegree)); rightSide.SetRow(i, itm.RightSide); } SingleVariablePolynomial pl; if (arr.Length != 0) { //var cfs = mtx.Inverse2() * rightSide; var cfs = mtx.Solve(rightSide.CoreArray);//.Inverse2() * rightSide; pl = new SingleVariablePolynomial(cfs); } else { pl = new SingleVariablePolynomial(); } //bar.ReturnMatrixToPool(rightSide, mtx); if (tp == Condition.FunctionType.M) { mss[nodeNum] = pl; } if (tp == Condition.FunctionType.N) { nss[nodeNum] = pl; } mtx.ReturnToPool(); rightSide.ReturnToPool(); } for (var i = 0; i < n; i++) { if (nss[i] == null) { nss[i] = new SingleVariablePolynomial(); } if (mss[i] == null) { mss[i] = new SingleVariablePolynomial(); } } { bar.SetCache(nssKey, nss); bar.SetCache(mssKey, mss); } return(true); }