Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            // Дано квадратное уравнение вида
            // 5x-3=0
            var vars = new Variables()
            {
                A = 5,
                B = 3
            };

            //Инструмент для решения линейного уравнения
            var solver = new LinearSolver();

            //Результат решения линейного уравнения
            var root = solver.Solve(vars);

            //Вывод результата программы
            if (root.HasRoots)
            {
                Console.WriteLine($"x = {root.Root}");
            }
            else
            {
                Console.WriteLine("a=0, вычислеие невозможно");
            }

            Console.ReadKey();
        }
Ejemplo n.º 2
0
        public void SolveExplicit()
        {
            double[,] hatMassMatrix = CalculateHatMMatrix();
            explicitSolution.Add(-1, CalculatePreviousDisplacementVector());
            explicitSolution.Add(0, InitialValues.InitialDisplacementVector);
            explicitAcceleration.Add(0, CalculateInitialAccelerations());
            TimeAtEachStep.Add(-1, -1 * timeStep + InitialValues.InitialTime);
            TimeAtEachStep.Add(0, 0.0);
            double[] nextSolution;
            for (int i = 1; i < timeStepsNumber; i++)
            {
                double time = i * timeStep + InitialValues.InitialTime;

                if (ActivateNonLinearSolution == false)
                {
                    double[] hatRVector = CalculateHatRVector(i);
                    nextSolution = LinearSolver.Solve(hatMassMatrix, hatRVector);
                    Console.WriteLine("Solution for Load Step {0} is:", i);
                    VectorOperations.PrintVector(nextSolution);
                }
                else
                {
                    double[] hatRVector = CalculateHatRVectorNL(i);
                    nextSolution = LinearSolver.Solve(hatMassMatrix, hatRVector);
                    //nextSolution = NewtonIterations(hatRVector);
                    Console.WriteLine("Solution for Load Step {0} is:", i);
                    VectorOperations.PrintVector(nextSolution);
                }
                explicitSolution.Add(i, nextSolution);
                explicitAcceleration.Add(i, CalculateAccelerations());
                TimeAtEachStep.Add(i, time);
            }
            ExportToFile.ExportExplicitResults(explicitSolution, TimeAtEachStep, 1, 1);
        }
Ejemplo n.º 3
0
        public void SolveNewmark()
        {
            List <double> aConstants = CalculateIntegrationConstantsNewmark();

            double[,] hatStiffnessMatrixNewmark = CalculateHatKMatrixNewmark(aConstants);
            explicitSolution.Add(0, InitialValues.InitialDisplacementVector);
            explicitVelocity.Add(0, InitialValues.InitialVelocityVector);
            explicitAcceleration.Add(0, CalculateInitialAccelerationsNewmark());
            TimeAtEachStep.Add(0, 0.0);
            double[] nextSolution;
            for (int i = 1; i < timeStepsNumber; i++)
            {
                double time = i * timeStep + InitialValues.InitialTime;

                if (ActivateNonLinearSolution == false)
                {
                    double[] hatRVectorNewmark = CalculateHatRVectorNewmark(i, aConstants);
                    nextSolution = LinearSolver.Solve(hatStiffnessMatrixNewmark, hatRVectorNewmark);
                    Console.WriteLine("Solution for Load Step {0} is:", i);
                    VectorOperations.PrintVector(nextSolution);
                }
                else
                {
                    nextSolution = NewtonIterationsNewmark(ExternalForcesVector, i, aConstants);
                    Console.WriteLine("Solution for Load Step {0} is:", i);
                    VectorOperations.PrintVector(nextSolution);
                }
                explicitSolution.Add(i, nextSolution);
                explicitAcceleration.Add(i, CalculateAccelerationNewmark(i, aConstants));
                explicitVelocity.Add(i, CalculateVelocityNewmark(i, aConstants));
                TimeAtEachStep.Add(i, time);
            }
            ExportToFile.ExportExplicitResults(explicitSolution, TimeAtEachStep, 1, 1);
            //ShowToGUI.ShowResults(explicitSolution, TimeAtEachStep, 1, 1);
        }
Ejemplo n.º 4
0
        private double[] LoadControlledNR(double[] forceVector)
        {
            lambda = 1.0 / numberOfLoadSteps;
            double[] incrementDf    = VectorOperations.VectorScalarProductNew(forceVector, lambda);
            double[] solutionVector = new double[forceVector.Length];
            double[] incrementalExternalForcesVector = new double[forceVector.Length];
            double[] tempSolutionVector = new double[solutionVector.Length];
            double[] deltaU             = new double[solutionVector.Length];
            double[] internalForcesTotalVector;
            double[] dU;
            double[] residual;
            double   residualNorm;

            //Assembler.UpdateAccelerations(CalculateAccelerations(InitialValues.InitialAccelerationVector));

            for (int i = 0; i < numberOfLoadSteps; i++)
            {
                incrementalExternalForcesVector = VectorOperations.VectorVectorAddition(incrementalExternalForcesVector, incrementDf);
                Assembler.UpdateDisplacements(solutionVector);

                Assembler.UpdateAccelerations(explicitAcceleration.Values.Last());

                internalForcesTotalVector = Assembler.CreateTotalInternalForcesVector();

                double[,] tangentMatrix = CalculateHatMMatrix();
                dU             = LinearSolver.Solve(tangentMatrix, incrementDf);
                solutionVector = VectorOperations.VectorVectorAddition(solutionVector, dU);

                Assembler.UpdateDisplacements(solutionVector);
                tangentMatrix             = CalculateHatMMatrix();
                internalForcesTotalVector = Assembler.CreateTotalInternalForcesVector();

                residual     = VectorOperations.VectorVectorSubtraction(internalForcesTotalVector, incrementalExternalForcesVector);
                residualNorm = VectorOperations.VectorNorm2(residual);
                int iteration = 0;
                Array.Clear(deltaU, 0, deltaU.Length);
                while (residualNorm > tolerance && iteration < maxIterations)
                {
                    tangentMatrix      = CalculateHatMMatrix();
                    deltaU             = VectorOperations.VectorVectorSubtraction(deltaU, LinearSolver.Solve(tangentMatrix, residual));
                    tempSolutionVector = VectorOperations.VectorVectorAddition(solutionVector, deltaU);
                    Assembler.UpdateDisplacements(tempSolutionVector);

                    //Assembler.UpdateAccelerations(CalculateAccelerations(solutionVector));

                    internalForcesTotalVector = Assembler.CreateTotalInternalForcesVector();
                    residual     = VectorOperations.VectorVectorSubtraction(internalForcesTotalVector, incrementalExternalForcesVector);
                    residualNorm = VectorOperations.VectorNorm2(residual);
                    iteration    = iteration + 1;
                }
                solutionVector = VectorOperations.VectorVectorAddition(solutionVector, deltaU);
                if (iteration >= maxIterations)
                {
                    Console.WriteLine("Newton-Raphson: Solution not converged at current iterations");
                }
            }

            return(solutionVector);
        }
Ejemplo n.º 5
0
        public long GetSolution1(String path)
        {
            var input = System.IO.File.ReadLines(path);

            var solver = new LinearSolver();

            return(input.Sum(x => (long)solver.Solve(x)));
        }
Ejemplo n.º 6
0
        private double[] NewtonIterationsNewmark(double[] forceVector, int stepNumber, List <double> aConstants)
        {
            lambda = 1.0 / numberOfLoadSteps;
            //double[] incrementDf = VectorOperations.VectorScalarProductNew(forceVector, lambda);
            double[] solutionVector = new double[forceVector.Length];
            double[] deltaU         = new double[solutionVector.Length];
            double[] internalForcesTotalVector;
            double[] residual;
            double   residualNorm;

            double[] hatR;

            solutionVector = explicitSolution.Values.Last();

            Assembler.UpdateDisplacements(solutionVector);

            //Assembler.UpdateAccelerations(explicitAcceleration.Values.Last());
            hatR = CalculateHatRVectorNewmarkNL(stepNumber, aConstants, solutionVector);
            internalForcesTotalVector = Assembler.CreateTotalInternalForcesVector();
            residual = VectorOperations.VectorVectorSubtraction(hatR, internalForcesTotalVector);
            residual = VectorOperations.VectorVectorSubtraction(forceVector, Assembler.CreateTotalInternalForcesVector());
            int iteration = 0;

            Array.Clear(deltaU, 0, deltaU.Length);

            for (int i = 0; i < maxIterations; i++)
            {
                double[,] tangentMatrix = CalculateHatKMatrixNewmark(aConstants);
                deltaU         = LinearSolver.Solve(tangentMatrix, residual);
                solutionVector = VectorOperations.VectorVectorAddition(solutionVector, deltaU);
                Assembler.UpdateDisplacements(solutionVector);
                //Assembler.UpdateAccelerations(CalculateAccelerations());
                hatR = CalculateHatRVectorNewmarkNL(stepNumber, aConstants, solutionVector);
                internalForcesTotalVector = Assembler.CreateTotalInternalForcesVector();
                residual     = VectorOperations.VectorVectorSubtraction(hatR, internalForcesTotalVector);
                residualNorm = VectorOperations.VectorNorm2(residual);
                if (residualNorm < tolerance)
                {
                    break;
                }
                iteration = iteration + 1;
            }
            //Console.WriteLine(iteration);
            if (iteration >= maxIterations)
            {
                Console.WriteLine("Newton-Raphson: Solution not converged at current iterations");
            }

            return(solutionVector);
        }
Ejemplo n.º 7
0
        private double[] CalculateInitialAccelerationsNewmark() //Bathe page 771
        {
            if (CustomStiffnessMatrix != null)
            {
                return(InitialValues.InitialAccelerationVector);
            }
            int step = explicitSolution.Count - 1;

            Assembler.UpdateDisplacements(explicitSolution[step]);
            double[,] stiffness = Assembler.CreateTotalStiffnessMatrix();
            double[,] mass      = Assembler.CreateTotalMassMatrix();

            double[] Ku  = VectorOperations.MatrixVectorProduct(stiffness, explicitSolution[step]);
            double[] RHS = VectorOperations.VectorVectorSubtraction(ExternalForcesVector, Ku);

            double[] acceleration = LinearSolver.Solve(mass, RHS);

            return(acceleration);
        }
Ejemplo n.º 8
0
        private static IEquationSolutions Solve(string variable, IExpression expression, ClassificationResult classification)
        {
            if (classification.EquationType == EquationTypes.Undefined)
            {
                return(new EquationSolutions());
            }

            if (classification.SearchResult.ElementAt(0).Key != variable)
            {
                //var expression = equation.Left;
                //var classification = equation.Classification;
                //var clone = expression.Clone();
                //substitute = new Tuple<string, string>(variable, GetNewVariableForSub(equation));
                //var sub = new VariableExpression(substitute.Item2, 1);
                //clone.Substitute(ref clone, sub, classification.SearchResult.ElementAt(0).Key);

                var sols = Solve(classification.SearchResult.ElementAt(0).Key, expression, classification);
                return(sols);
            }
            else
            {
                if (classification.EquationType == EquationTypes.Linear)
                {
                    var solver = new LinearSolver();
                    return(solver.Solve(expression, variable, classification));
                }
                else if (classification.EquationType == EquationTypes.Quadratic)
                {
                    var solver = new QuadraticSolver();
                    return(solver.Solve(expression, variable, classification));
                }
                else if (classification.EquationType == EquationTypes.Trigonometric)
                {
                    var solver = new TrigonometricSolver();
                    return(solver.Solve(expression, variable, classification));
                }
                else
                {
                    return(null);
                }
            }
        }
Ejemplo n.º 9
0
        public void Solve()
        {
            IFiniteElementSolver solver = new LinearSolver(this.Model);

            this.Results = solver.Solve();
        }
    private void SolveEquation11(float a_SpringConstant, float a_DampingConstant)
    {
        ParticlesGetVelocities(qdot);
        ValidateVector(qdot);
        //double[] M = ParticlesMassMatrix();
        //ValidateVector(M);
        ParticlesInverseMassMatrix(W);
        ValidateVector(W);
        ParticlesGetForces(Q);
        ValidateVector(Q);
        ConstraintsGetValues(C);
        ValidateVector(C);
        //double[] CMass = ConstraintsGetAvgMasses();
        //ValidateVector(CMass);
        ConstraintsGetDerivativeValues(CDot);
        ValidateVector(CDot);
        // JDot times qdot.
        for (int i = 0; i < JDotqdot.Length; ++i)
        {
            JDotqdot[i] = 0;
        }
        m_JDot.MatrixTimesVector(qdot, JDotqdot);
        ValidateVector(JDotqdot);
        // W times Q.
        for (int i = 0; i < WQ.Length; ++i)
        {
            WQ[i] = W[i] * Q[i];
        }
        ValidateVector(WQ);
        // J times WQ.
        for (int i = 0; i < JWQ.Length; ++i)
        {
            JWQ[i] = 0;
        }
        m_J.MatrixTimesVector(WQ, JWQ);
        ValidateVector(JWQ);
        // Compute the RHS of equation 11.
        for (int i = 0; i < RHS.Length; ++i)
        {
            RHS[i] = -JDotqdot[i] - JWQ[i] - a_SpringConstant * C[i] - a_DampingConstant * CDot[i];
            if (double.IsNaN(RHS[i]) || double.IsNaN(RHS[i]))
            {
                throw new System.Exception("NaN or Inf in RHS of eq 11");
            }
        }
        // Set up implicit matrix of LHS and solve.
        int stepsPerformed = 0;

        m_Solver.Solve(LHS, lambda, RHS, out stepsPerformed);
        ValidateVector(lambda);
        //Debug.Log("Nr of iterations in conjgrad solver: " + stepsPerformed);
        for (int i = 0; i < QHat.Length; ++i)
        {
            QHat[i] = 0;
        }
        m_J.MatrixTransposeTimesVector(lambda, QHat);
        ValidateVector(QHat);
        if (QHat.Length != m_Particles.Count << 1)
        {
            throw new Exception("QHat does not match particles!");
        }
        for (int i = 0; i < m_Particles.Count; ++i)
        {
            m_Particles[i].ForceAccumulator += new Vector2((float)QHat[i * 2], (float)QHat[(i * 2) + 1]);
            Vector2 newForce = m_Particles[i].ForceAccumulator;
            if (double.IsNaN(newForce.x) || double.IsNaN(newForce.y) || double.IsInfinity(newForce.x) || double.IsInfinity(newForce.y))
            {
                throw new Exception("NaN or Inf in accumulated force after eq 11");
            }
        }
    }
Ejemplo n.º 11
0
        private double[] NewtonIterationsExplicit(int timeStep, double[] forceVector, double[,] tangentMatrix)
        {
            //lambda = 1.0 / numberOfLoadSteps;
            //double[] incrementDf = VectorOperations.VectorScalarProductNew(forceVector, lambda);
            double[] solutionVector = new double[forceVector.Length];
            double[] deltaU         = new double[solutionVector.Length];
            //double[] internalForcesTotalVector;
            double[] residual;
            double   residualNorm;

            double[] hatRPrevious;
            double[] hatRNext;

            solutionVector = explicitSolution.Values.Last();

            Assembler.UpdateDisplacements(solutionVector);

            Assembler.UpdateAccelerations(explicitAcceleration.Values.Last());
            hatRPrevious = CalculateHatRVectorNL(timeStep);
            hatRNext     = hatRPrevious;
            //internalForcesTotalVector = Assembler.CreateTotalInternalForcesVector();
            //residual = VectorOperations.VectorVectorSubtraction(hatR, internalForcesTotalVector);
            residual = hatRPrevious;
            int iteration = 0;

            Array.Clear(deltaU, 0, deltaU.Length);

            for (int i = 0; i < maxIterations; i++)
            {
                if (i == 0)
                {
                    //deltaU = LinearSolver.Solve(tangentMatrix, residual);
                    //solutionVector = VectorOperations.VectorVectorAddition(solutionVector, deltaU);
                    solutionVector = LinearSolver.Solve(tangentMatrix, residual);
                    Assembler.UpdateDisplacements(solutionVector);
                    //Assembler.UpdateAccelerations(CalculateAccelerations());
                    hatRNext = CalculateHatRVectorNL(timeStep);
                    //internalForcesTotalVector = Assembler.CreateTotalInternalForcesVector();
                    residual     = VectorOperations.VectorVectorSubtraction(hatRNext, hatRPrevious);
                    residualNorm = VectorOperations.VectorNorm2(residual);
                    if (residualNorm < tolerance)
                    {
                        break;
                    }
                    iteration    = iteration + 1;
                    hatRPrevious = hatRNext;
                }
                else
                {
                    deltaU         = LinearSolver.Solve(tangentMatrix, residual);
                    solutionVector = VectorOperations.VectorVectorAddition(solutionVector, deltaU);
                    //solutionVector = LinearSolver.Solve(tangentMatrix, residual);
                    Assembler.UpdateDisplacements(solutionVector);
                    //Assembler.UpdateAccelerations(CalculateAccelerations());
                    hatRNext = CalculateHatRVectorNL(timeStep);
                    //internalForcesTotalVector = Assembler.CreateTotalInternalForcesVector();
                    residual     = VectorOperations.VectorVectorSubtraction(hatRNext, hatRPrevious);
                    residualNorm = VectorOperations.VectorNorm2(residual);
                    if (residualNorm < tolerance)
                    {
                        break;
                    }
                    iteration    = iteration + 1;
                    hatRPrevious = hatRNext;
                }
            }
            //Console.WriteLine(iteration);
            if (iteration >= maxIterations)
            {
                Console.WriteLine("Newton-Raphson: Solution not converged at current iterations");
            }

            return(solutionVector);
        }
Ejemplo n.º 12
0
 public void Solve()
 {
     IFiniteElementSolver solver = new LinearSolver(this.Model);
     this.Results = solver.Solve();
 }