Ejemplo n.º 1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="other"></param>
        /// <returns></returns>
        public KeyedVector <TKey> CrossProduct(KeyedVector <TKey> other)
        {
            Guard.AgainstNullArgument(other, "other");
            Guard.AgainstBadArgument(
                "other",
                () => { return(other.Count != 3); },
                "Cross product can only be carried out with a 3 dimensional vector");
            Guard.AgainstInvalidState(
                () => { return(this.Count != 3); },
                "Cross product can only be carried out with a 3 dimensional vector. Number of dimensions of this vectors are {0}",
                this.Count);

            KeyCompatibilityValidator <TKey, TKey> kcv = new KeyCompatibilityValidator <TKey, TKey>(this.Keys, other.Keys);

            kcv.ThrowIfInvalid();
            ////TODO If the keys match but are in the wrong order, swap the other vector items and keys to match exactly

            IList <TKey> keyz = this.Keys;
            TKey         key0 = keyz[0];
            TKey         key1 = keyz[1];
            TKey         key2 = keyz[2];

            KeyedVector <TKey> result = new KeyedVector <TKey>(keyz);

            result[key0] = (this[key1] * other[key2]) - (this[key2] * other[key1]);
            result[key1] = (this[key2] * other[key0]) - (this[key0] * other[key2]);
            result[key2] = (this[key0] * other[key1]) - (this[key1] * other[key0]);

            return(result);
        }
        protected override KeyedVector <NodalDegreeOfFreedom> Solve(StiffnessMatrix stiffnessMatrix, KeyedVector <NodalDegreeOfFreedom> forceVector)
        {
            KeyedSquareMatrix <NodalDegreeOfFreedom> inverse  = stiffnessMatrix.Inverse();
            KeyedVector <NodalDegreeOfFreedom>       solution = inverse.Multiply(forceVector);

            return(solution);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Creates a new keyed matrix from keyed vectors representing the rows of the new matrix
        /// </summary>
        /// <param name="axis1">The vector representing the first row</param>
        /// <param name="axis2">The vector representing the second row</param>
        /// <param name="axis3">The vector representing the third row</param>
        /// <returns>A matrix built from the vectors</returns>
        protected static KeyedSquareMatrix <DegreeOfFreedom> CreateFromRows(KeyedVector <DegreeOfFreedom> axis1, KeyedVector <DegreeOfFreedom> axis2, KeyedVector <DegreeOfFreedom> axis3)
        {
            ////TODO this should be devolved to the KeyedMatrix class
            Guard.AgainstBadArgument(
                "axis1",
                () => { return(axis1.Count != 3); },
                "All axes should be 3D, i.e. have 3 items");
            Guard.AgainstBadArgument(
                "axis2",
                () => { return(axis2.Count != 3); },
                "All axes should be 3D, i.e. have 3 items");
            Guard.AgainstBadArgument(
                "axis3",
                () => { return(axis3.Count != 3); },
                "All axes should be 3D, i.e. have 3 items");
            Guard.AgainstBadArgument(
                "axis1",
                () => { return(axis1.SumMagnitudes().IsApproximatelyEqualTo(0.0)); },
                string.Format(
                    System.Globalization.CultureInfo.InvariantCulture,
                    "Axis should not be zero: {0}",
                    axis1));
            Guard.AgainstBadArgument(
                "axis2",
                () => { return(axis2.SumMagnitudes().IsApproximatelyEqualTo(0.0)); },
                string.Format(
                    System.Globalization.CultureInfo.InvariantCulture,
                    "Axis should not be zero: {0}",
                    axis2));
            Guard.AgainstBadArgument(
                "axis3",
                () => { return(axis3.SumMagnitudes().IsApproximatelyEqualTo(0.0)); },
                string.Format(
                    System.Globalization.CultureInfo.InvariantCulture,
                    "Axis should not be zero: {0}",
                    axis3));

            KeyedVector <DegreeOfFreedom> axis1Norm = axis1.Normalize(2);
            KeyedVector <DegreeOfFreedom> axis2Norm = axis2.Normalize(2);
            KeyedVector <DegreeOfFreedom> axis3Norm = axis3.Normalize(2);

            IList <DegreeOfFreedom> dof = new List <DegreeOfFreedom>(3)
            {
                DegreeOfFreedom.X, DegreeOfFreedom.Y, DegreeOfFreedom.Z
            };

            KeyedSquareMatrix <DegreeOfFreedom> result = new KeyedSquareMatrix <DegreeOfFreedom>(dof);

            result.At(DegreeOfFreedom.X, DegreeOfFreedom.X, axis1Norm[DegreeOfFreedom.X]);
            result.At(DegreeOfFreedom.X, DegreeOfFreedom.Y, axis1Norm[DegreeOfFreedom.Y]);
            result.At(DegreeOfFreedom.X, DegreeOfFreedom.Z, axis1Norm[DegreeOfFreedom.Z]);
            result.At(DegreeOfFreedom.Y, DegreeOfFreedom.X, axis2Norm[DegreeOfFreedom.X]);
            result.At(DegreeOfFreedom.Y, DegreeOfFreedom.Y, axis2Norm[DegreeOfFreedom.Y]);
            result.At(DegreeOfFreedom.Y, DegreeOfFreedom.Z, axis2Norm[DegreeOfFreedom.Z]);
            result.At(DegreeOfFreedom.Z, DegreeOfFreedom.X, axis3Norm[DegreeOfFreedom.X]);
            result.At(DegreeOfFreedom.Z, DegreeOfFreedom.Y, axis3Norm[DegreeOfFreedom.Y]);
            result.At(DegreeOfFreedom.Z, DegreeOfFreedom.Z, axis3Norm[DegreeOfFreedom.Z]);

            return(result);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Calculates the combined forces for each requested node and degree of freedom combination, and returns these values as a vector.
        /// The index of the vector corresponds to the same index of the provided nodalDegreesOfFreedoms parameter.
        /// </summary>
        /// <param name="nodalDegreeOfFreedoms">A list of all the <see cref="NodalDegreeOfFreedom">Nodal Degree of Freedoms</see> for which we wish to get a force.</param>
        /// <returns>A vector of forces at each node for each degree of freedom on that node.</returns>
        public KeyedVector <NodalDegreeOfFreedom> GetCombinedForcesFor(IList <NodalDegreeOfFreedom> nodalDegreeOfFreedoms)
        {
            Guard.AgainstNullArgument(nodalDegreeOfFreedoms, "nodalDegreeOfFreedoms");

            KeyedVector <NodalDegreeOfFreedom>            result = new KeyedVector <NodalDegreeOfFreedom>(nodalDegreeOfFreedoms);
            IDictionary <IFiniteElementNode, ForceVector> cache  = new Dictionary <IFiniteElementNode, ForceVector>();
            ForceVector combinedForceOnNode;

            foreach (NodalDegreeOfFreedom item in nodalDegreeOfFreedoms)
            {
                if (item == null || item.Node == null)
                {
                    // garbage in, garbage out!
                    result[item] = 0;
                    continue;
                }

                if (cache.ContainsKey(item.Node))
                {
                    combinedForceOnNode = cache[item.Node];
                    result[item]        = combinedForceOnNode[item.DegreeOfFreedom];
                }
                else
                {
                    result[item] = this.GetCombinedForceFor(item, out combinedForceOnNode);
                    cache.Add(item.Node, combinedForceOnNode);
                }
            }

            return(result);
        }
Ejemplo n.º 5
0
 protected KeyedVector(KeyedVector <TKey> vectorToClone)
     : this(vectorToClone.Keys)
 {
     foreach (KeyValuePair <TKey, double> kvp in vectorToClone.store)
     {
         this[kvp.Key] = kvp.Value;
     }
 }
Ejemplo n.º 6
0
        /// <summary>
        /// Adds multiple reaction to this set of results
        /// </summary>
        /// <param name="reactions">The value of the reactions.  The order of this vector matches the order of the identifiers in the corresponding parameter.</param>
        public void AddMultipleReactions(KeyedVector <NodalDegreeOfFreedom> react)
        {
            Guard.AgainstNullArgument(reactions, "reactions");

            foreach (KeyValuePair <NodalDegreeOfFreedom, double> kvp in react)
            {
                this.AddReaction(kvp.Key, kvp.Value);
            }
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Adds multiple displacements to the set of results
        /// </summary>
        /// <param name="displacements">The value of the displacements.  The order of this vector matches the order of the identifiers in the corresponding parameter</param>
        public void AddMultipleDisplacements(KeyedVector <NodalDegreeOfFreedom> disp)
        {
            Guard.AgainstNullArgument(disp, "displacements");

            foreach (KeyValuePair <NodalDegreeOfFreedom, double> kvp in disp)
            {
                this.AddDisplacement(kvp.Key, kvp.Value);
            }
        }
Ejemplo n.º 8
0
 public ForceVector(KeyedVector <DegreeOfFreedom> vectorToClone)
     : base(vectorToClone.Keys)
 {
     this.Id = Guid.NewGuid();
     foreach (KeyValuePair <DegreeOfFreedom, double> kvp in vectorToClone)
     {
         this.SetValue(kvp.Key, kvp.Value);
     }
 }
Ejemplo n.º 9
0
        public KeyedVector <TKey> Negate()
        {
            KeyedVector <TKey> result = new KeyedVector <TKey>(this.Keys);

            foreach (KeyValuePair <TKey, double> kvp in this.store)
            {
                result[kvp.Key] = -1.0 * kvp.Value;
            }
            return(result);
        }
Ejemplo n.º 10
0
        public KeyedVector <TKey> Multiply(double scalar)
        {
            IList <TKey>       keyz   = this.Keys;
            KeyedVector <TKey> result = Clone();

            foreach (TKey key in keyz)
            {
                result[key] *= scalar;
            }
            return(result);
        }
Ejemplo n.º 11
0
        public double DotProduct(KeyedVector <TKey> other)
        {
            KeyCompatibilityValidator <TKey, TKey> kcv = new KeyCompatibilityValidator <TKey, TKey>(this.Keys, other.Keys);

            kcv.ThrowIfInvalid();

            double sum = 0.0;

            sum = this.store.Sum(kvp => kvp.Value * other[kvp.Key]);

            return(sum);
        }
Ejemplo n.º 12
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="rightSide"></param>
        /// <returns></returns>
        public KeyedVector <TRowKey> Multiply(KeyedVector <TColumnKey> rightSide)
        {
            KeyCompatibilityValidator <TColumnKey, TColumnKey> kcv = new KeyCompatibilityValidator <TColumnKey, TColumnKey>(this.ColumnKeys, rightSide.Keys);

            kcv.ThrowIfInvalid();

            ////FIXME If the column keys and vector keys are compatible but are stored or returned in the wrong order then this will return the wrong results.  Need to swap the vector items and keys to match exactly

            Vector <double> result = this.underlyingMatrix.Multiply(rightSide.ToVector());

            return(new KeyedVector <TRowKey>(this.RowKeys, result));
        }
Ejemplo n.º 13
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="other"></param>
        /// <returns></returns>
        public KeyedVector <TKey> Add(KeyedVector <TKey> other)
        {
            var kcv = new KeyCompatibilityValidator <TKey, TKey>(this.Keys, other.Keys);

            kcv.ThrowIfInvalid();

            IList <TKey>       keyz   = this.Keys;
            KeyedVector <TKey> result = Clone();

            foreach (TKey key in keyz)
            {
                result[key] += other[key];
            }
            return(result);
        }
Ejemplo n.º 14
0
        /// <summary>
        /// This gets a force component value for each of the node and degree of freedom combinations of known forces.
        /// The index of each value corresponds to the index of each <see cref="NodalDegreeOfFreedom" /> returned by DegreeOfFreedomWithKnownForce.
        /// </summary>
        /// <returns>Vector of values of all the known force components.</returns>
        public KeyedVector <NodalDegreeOfFreedom> KnownForceVector()
        {
            IList <NodalDegreeOfFreedom> knownForces = this.UnconstrainedNodalDegreeOfFreedoms;
            //TODO purge unconnected nodes

            KeyedVector <NodalDegreeOfFreedom> result = new KeyedVector <NodalDegreeOfFreedom>(knownForces);
            ForceVector nodalForce;

            foreach (NodalDegreeOfFreedom currentNodalDegreeOfFreedom in knownForces)
            {
                nodalForce = this.forces.GetCombinedForceOn(currentNodalDegreeOfFreedom.Node);
                result[currentNodalDegreeOfFreedom] = nodalForce[currentNodalDegreeOfFreedom.DegreeOfFreedom];
            }

            return(result);
        }
Ejemplo n.º 15
0
        public bool Equals(KeyedVector <TKey> other)
        {
            if (other == null)
            {
                return(false);
            }

            foreach (KeyValuePair <TKey, double> kvp in this)
            {
                if (!other[kvp.Key].IsApproximatelyEqualTo(kvp.Value))
                {
                    return(false);
                }
            }

            return(true);
        }
Ejemplo n.º 16
0
        public KeyedVector <TKey> Normalize(double p)
        {
            if (p < 0.0)
            {
                throw new ArgumentOutOfRangeException("p");
            }

            double             norm  = Norm(p);
            KeyedVector <TKey> clone = Clone();

            if (norm.IsApproximatelyEqualTo(0.0))
            {
                return(clone);
            }

            clone = clone.Multiply(1.0 / norm);

            return(clone);
        }
Ejemplo n.º 17
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="stiffnessMatrix"></param>
        /// <param name="forceVector"></param>
        /// <returns></returns>
        protected override KeyedVector <NodalDegreeOfFreedom> Solve(StiffnessMatrix stiffnessMatrix, KeyedVector <NodalDegreeOfFreedom> forceVector)
        {
            Svd <NodalDegreeOfFreedom, NodalDegreeOfFreedom> svd = new Svd <NodalDegreeOfFreedom, NodalDegreeOfFreedom>(stiffnessMatrix, true);

            return(svd.Solve(forceVector));
        }
Ejemplo n.º 18
0
        public override bool Equals(object obj)
        {
            KeyedVector <TKey> other = obj as KeyedVector <TKey>;

            return(this.Equals(other));
        }