Skip to content

remove math.net from main project #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 34 additions & 36 deletions NLES.Tests/Builder.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
using System;
using System.Linq;

using MathNet.Numerics.LinearAlgebra;
using MathNet.Numerics.LinearAlgebra.Double;

using NLES;
using NLES.Correction;
using NLES.Correction.Methods;
using NLES.Tests;

using Xunit;
using Vector = NLES.Vector;

namespace NonLinearEquationsSolver
{
Expand All @@ -19,14 +17,14 @@ public class Builder
public void RestoringArcLengthSolverIsCorrectlyBuild()
{
//Given
static Vector<double> Reaction(Vector<double> u) => new DenseVector(2);
static Vector Reaction(Vector u) => new Vector(2);

static ILinearSolver Stiffness(Vector<double> u)
static ILinearSolver Stiffness(Vector u)
=> new LinearSolverForTesting(new DenseMatrix(2, 2));
Vector<double> initialLoad = new DenseVector(2) { [0] = 1, [1] = 1 };
Vector<double> initialDisplacement = new DenseVector(2);
Vector initialLoad = new Vector(2) { [0] = 1, [1] = 1 };
Vector initialDisplacement = new Vector(2);
double initialLoadFactor = 1;
Vector<double> referenceLoad = new DenseVector(2) { [0] = 1, [1] = 1 };
Vector referenceLoad = new Vector(2) { [0] = 1, [1] = 1 };
double dispTol = 1e-3;
double eqTol = 1e-3;
double enTol = 1e-3;
Expand Down Expand Up @@ -62,14 +60,14 @@ static ILinearSolver Stiffness(Vector<double> u)
public void AngleArcLengthSolverIsCorrectlyBuild()
{
//Given
static Vector<double> Reaction(Vector<double> u) => new DenseVector(2);
static Vector Reaction(Vector u) => new Vector(2);

static ILinearSolver Stiffness(Vector<double> u)
static ILinearSolver Stiffness(Vector u)
=> new LinearSolverForTesting(new DenseMatrix(2, 2));
Vector<double> initialLoad = new DenseVector(2) { [0] = 1, [1] = 1 };
Vector<double> initialDisplacement = new DenseVector(2);
Vector initialLoad = new Vector(2) { [0] = 1, [1] = 1 };
Vector initialDisplacement = new Vector(2);
double initialLoadFactor = 1;
Vector<double> referenceLoad = new DenseVector(2) { [0] = 1, [1] = 1 };
Vector referenceLoad = new Vector(2) { [0] = 1, [1] = 1 };
double dispTol = 1e-3;
double eqTol = 1e-3;
double enTol = 1e-3;
Expand Down Expand Up @@ -105,14 +103,14 @@ static ILinearSolver Stiffness(Vector<double> u)
public void StandardSolverIsCorrectlyBuild()
{
//Given
static Vector<double> Reaction(Vector<double> u) => new DenseVector(2);
static Vector Reaction(Vector u) => new Vector(2);

static ILinearSolver Stiffness(Vector<double> u)
static ILinearSolver Stiffness(Vector u)
=> new LinearSolverForTesting(new DenseMatrix(2, 2));
Vector<double> initialLoad = new DenseVector(2) { [0] = 1, [1] = 1 };
Vector<double> initialDisplacement = new DenseVector(2);
Vector initialLoad = new Vector(2) { [0] = 1, [1] = 1 };
Vector initialDisplacement = new Vector(2);
double initialLoadFactor = 1;
Vector<double> referenceLoad = new DenseVector(2) { [0] = 1, [1] = 1 };
Vector referenceLoad = new Vector(2) { [0] = 1, [1] = 1 };
double dispTol = 1e-3;
double eqTol = 1e-3;
double enTol = 1e-3;
Expand Down Expand Up @@ -144,10 +142,10 @@ static ILinearSolver Stiffness(Vector<double> u)
public void StandardSolverWithoutReactionDoesNotLaunchException()
{
//Given
Vector<double> initialLoad = new DenseVector(2) { [0] = 1, [1] = 1 };
Vector<double> initialDisplacement = new DenseVector(2);
Vector initialLoad = new Vector(2) { [0] = 1, [1] = 1 };
Vector initialDisplacement = new Vector(2);
double initialLoadFactor = 1;
Vector<double> referenceLoad = new DenseVector(2) { [0] = 1, [1] = 1 };
Vector referenceLoad = new Vector(2) { [0] = 1, [1] = 1 };
double dispTol = 1e-3;
double eqTol = 1e-3;
double enTol = 1e-3;
Expand Down Expand Up @@ -177,14 +175,14 @@ public void StandardSolverWithoutReactionDoesNotLaunchException()
public void StandardSolverWithoutDefinedStopConditionsHasDefaultStopConditions()
{
//Given
static Vector<double> Reaction(Vector<double> u) => new DenseVector(2);
static Vector Reaction(Vector u) => new Vector(2);

static ILinearSolver Stiffness(Vector<double> u)
static ILinearSolver Stiffness(Vector u)
=> new LinearSolverForTesting(new DenseMatrix(2, 2));
Vector<double> initialLoad = new DenseVector(2) { [0] = 1, [1] = 1 };
Vector<double> initialDisplacement = new DenseVector(2);
Vector initialLoad = new Vector(2) { [0] = 1, [1] = 1 };
Vector initialDisplacement = new Vector(2);
double initialLoadFactor = 1;
Vector<double> referenceLoad = new DenseVector(2) { [0] = 1, [1] = 1 };
Vector referenceLoad = new Vector(2) { [0] = 1, [1] = 1 };
double dlambda = 0.1;

// When
Expand All @@ -210,14 +208,14 @@ static ILinearSolver Stiffness(Vector<double> u)
public void StandardSolverWithoutDefinedLoadFactorIncrementLaunchesException()
{
//Given
static Vector<double> Reaction(Vector<double> u) => new DenseVector(2);
static Vector Reaction(Vector u) => new Vector(2);

static ILinearSolver Stiffness(Vector<double> u)
static ILinearSolver Stiffness(Vector u)
=> new LinearSolverForTesting(new DenseMatrix(2, 2));
Vector<double> initialLoad = new DenseVector(2) { [0] = 1, [1] = 1 };
Vector<double> initialDisplacement = new DenseVector(2);
Vector initialLoad = new Vector(2) { [0] = 1, [1] = 1 };
Vector initialDisplacement = new Vector(2);
double initialLoadFactor = 1;
Vector<double> referenceLoad = new DenseVector(2) { [0] = 1, [1] = 1 };
Vector referenceLoad = new Vector(2) { [0] = 1, [1] = 1 };

bool exceptionLaunched = false;
try
Expand All @@ -241,14 +239,14 @@ static ILinearSolver Stiffness(Vector<double> u)
public void ArcLengthSolverWithoutDefinedRadiusLaunchesException()
{
//Given
static Vector<double> Reaction(Vector<double> u) => new DenseVector(2);
static Vector Reaction(Vector u) => new Vector(2);

static ILinearSolver Stiffness(Vector<double> u)
static ILinearSolver Stiffness(Vector u)
=> new LinearSolverForTesting(new DenseMatrix(2, 2));
Vector<double> initialLoad = new DenseVector(2) { [0] = 1, [1] = 1 };
Vector<double> initialDisplacement = new DenseVector(2);
Vector initialLoad = new Vector(2) { [0] = 1, [1] = 1 };
Vector initialDisplacement = new Vector(2);
double initialLoadFactor = 1;
Vector<double> referenceLoad = new DenseVector(2) { [0] = 1, [1] = 1 };
Vector referenceLoad = new Vector(2) { [0] = 1, [1] = 1 };

bool exceptionLaunched = false;
try
Expand Down
26 changes: 20 additions & 6 deletions NLES.Tests/LinearSolverForTesting.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using System;
using System.Collections.Generic;
using System.Text;
using MathNet.Numerics.LinearAlgebra;
using MathNet.Numerics.LinearAlgebra;
using MathNet.Numerics.LinearAlgebra.Double;

namespace NLES.Tests
{
Expand All @@ -11,7 +9,23 @@ internal class LinearSolverForTesting : ILinearSolver

public LinearSolverForTesting(Matrix<double> matrix) => this.matrix = matrix;

public Vector<double> Solve(Vector<double> input)
=> matrix.Solve(input);
public Vector Solve(Vector input)
{
Vector<double> v = new DenseVector(input.Dimension);
for (int i = 0; i < input.Dimension; i++)
{
v[i] = input[i];
}

Vector<double> sol = matrix.Solve(v);

Vector solution = new Vector(input.Dimension);
for (int i = 0; i < input.Dimension; i++)
{
solution[i] = sol[i];
}

return solution;
}
}
}
2 changes: 1 addition & 1 deletion NLES.Tests/NLES.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="MathNet.Numerics" Version="4.11.0" />
<PackageReference Include="MathNet.Numerics" Version="4.12.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.1" />
<PackageReference Include="morelinq" Version="3.3.2" />
<PackageReference Include="xunit" Version="2.4.1" />
Expand Down
58 changes: 28 additions & 30 deletions NLES.Tests/TestSolverResult.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;

using MathNet.Numerics.LinearAlgebra;
using MathNet.Numerics.LinearAlgebra.Double;

using NLES;
using NLES.Contracts;
using NLES.Tests;
using Xunit;
using Vector = NLES.Vector;

namespace NonLinearEquationsSolver
{
Expand All @@ -21,11 +19,11 @@ public class TestSolverResults
public void SolveLinearFunction()
{
// ARRANGE
static Vector<double> Reaction(Vector<double> u) => new DenseVector(2) { [0] = u[0], [1] = u[0] + 2 * u[1] };
static Vector Reaction(Vector u) => new Vector(2) { [0] = u[0], [1] = u[0] + 2 * u[1] };

static ILinearSolver Stiffness(Vector<double> u)
static ILinearSolver Stiffness(Vector u)
=> new LinearSolverForTesting(new DenseMatrix(2, 2) { [0, 0] = 1, [1, 0] = 1, [1, 1] = 2 });
DenseVector force = new DenseVector(2) { [0] = 1, [1] = 3 };
Vector force = new Vector(2) { [0] = 1, [1] = 3 };
NonLinearSolver Solver = NonLinearSolver.Builder
.Solve(2, Reaction, Stiffness)
.Under(force)
Expand All @@ -42,11 +40,11 @@ static ILinearSolver Stiffness(Vector<double> u)
public void SolveLinearFunctionSmallIncrements()
{
// ARRANGE
static Vector<double> Reaction(Vector<double> u) => new DenseVector(2) { [0] = u[0], [1] = u[0] + 2 * u[1] };
static Vector Reaction(Vector u) => new Vector(2) { [0] = u[0], [1] = u[0] + 2 * u[1] };

static ILinearSolver Stiffness(Vector<double> u)
static ILinearSolver Stiffness(Vector u)
=> new LinearSolverForTesting(new DenseMatrix(2, 2) { [0, 0] = 1, [1, 0] = 1, [1, 1] = 2 });
DenseVector force = new DenseVector(2) { [0] = 1, [1] = 3 };
Vector force = new Vector(2) { [0] = 1, [1] = 3 };
double inc = 1e-2;
NonLinearSolver Solver = NonLinearSolver.Builder
.Solve(2, Reaction, Stiffness)
Expand All @@ -66,11 +64,11 @@ static ILinearSolver Stiffness(Vector<double> u)
public void SolveLinearFunctionArcLength()
{
// ARRANGE
static Vector<double> Reaction(Vector<double> u) => new DenseVector(2) { [0] = u[0], [1] = u[0] + 2 * u[1] };
static Vector Reaction(Vector u) => new Vector(2) { [0] = u[0], [1] = u[0] + 2 * u[1] };

static ILinearSolver Stiffness(Vector<double> u)
static ILinearSolver Stiffness(Vector u)
=> new LinearSolverForTesting(new DenseMatrix(2, 2) { [0, 0] = 1, [1, 0] = 1, [1, 1] = 2 });
DenseVector force = new DenseVector(2) { [0] = 1, [1] = 3 };
Vector force = new Vector(2) { [0] = 1, [1] = 3 };
double radius = 1e-2;
NonLinearSolver Solver = NonLinearSolver.Builder
.Solve(2, Reaction, Stiffness)
Expand All @@ -89,25 +87,25 @@ static ILinearSolver Stiffness(Vector<double> u)
public void SolveQuadraticFunction()
{
// ARRANGE
static Vector<double> Reaction(Vector<double> u) => new DenseVector(2)
static Vector Reaction(Vector u) => new Vector(2)
{
[0] = u[0] * u[0] + 2 * u[1] * u[1],
[1] = 2 * u[0] * u[0] + u[1] * u[1]
};

static ILinearSolver Stiffness(Vector<double> u)
static ILinearSolver Stiffness(Vector u)
=> new LinearSolverForTesting(new DenseMatrix(2, 2)
{
[0, 0] = 2 * u[0],
[0, 1] = 4 * u[1],
[1, 0] = 4 * u[0],
[1, 1] = 2 * u[1]
});
DenseVector force = new DenseVector(2) { [0] = 3, [1] = 3 };
Vector force = new Vector(2) { [0] = 3, [1] = 3 };
NonLinearSolver Solver = NonLinearSolver.Builder
.Solve(2, Reaction, Stiffness)
.Under(force)
.WithInitialConditions(0.1, DenseVector.Create(2, 0), DenseVector.Create(2, 1))
.WithInitialConditions(0.1, new Vector(2, 0), new Vector(2, 1))
.Build();

// ACT
Expand All @@ -121,25 +119,25 @@ static ILinearSolver Stiffness(Vector<double> u)
public void SolveQuadraticFunctionSmallIncrements()
{
// ARRANGE
static Vector<double> Reaction(Vector<double> u) => new DenseVector(2)
static Vector Reaction(Vector u) => new Vector(2)
{
[0] = u[0] * u[0] + 2 * u[1] * u[1],
[1] = 2 * u[0] * u[0] + u[1] * u[1]
};

static ILinearSolver Stiffness(Vector<double> u)
static ILinearSolver Stiffness(Vector u)
=> new LinearSolverForTesting(new DenseMatrix(2, 2)
{
[0, 0] = 2 * u[0],
[0, 1] = 4 * u[1],
[1, 0] = 4 * u[0],
[1, 1] = 2 * u[1]
});
DenseVector force = new DenseVector(2) { [0] = 3, [1] = 3 };
Vector force = new Vector(2) { [0] = 3, [1] = 3 };
NonLinearSolver Solver = NonLinearSolver.Builder
.Solve(2, Reaction, Stiffness)
.Under(force)
.WithInitialConditions(0.1, DenseVector.Create(2, 0), DenseVector.Create(2, 1))
.WithInitialConditions(0.1, new Vector(2, 0), new Vector(2, 1))
.UsingStandardNewtonRaphsonScheme(0.01)
.Build();

Expand All @@ -154,25 +152,25 @@ static ILinearSolver Stiffness(Vector<double> u)
public void SolveQuadraticFunctionArcLength()
{
// ARRANGE
static Vector<double> Reaction(Vector<double> u) => new DenseVector(2)
static Vector Reaction(Vector u) => new Vector(2)
{
[0] = u[0] * u[0] + 2 * u[1] * u[1],
[1] = 2 * u[0] * u[0] + u[1] * u[1]
};

static ILinearSolver Stiffness(Vector<double> u)
static ILinearSolver Stiffness(Vector u)
=> new LinearSolverForTesting(new DenseMatrix(2, 2)
{
[0, 0] = 2 * u[0],
[0, 1] = 4 * u[1],
[1, 0] = 4 * u[0],
[1, 1] = 2 * u[1]
});
DenseVector force = new DenseVector(2) { [0] = 3, [1] = 3 };
Vector force = new Vector(2) { [0] = 3, [1] = 3 };
NonLinearSolver Solver = NonLinearSolver.Builder
.Solve(2, Reaction, Stiffness)
.Under(force)
.WithInitialConditions(0.1, DenseVector.Create(2, 0), DenseVector.Create(2, 1))
.WithInitialConditions(0.1, new Vector(2, 0), new Vector(2, 1))
.UsingArcLengthScheme(0.05)
.NormalizeLoadWith(0.01)
.Build();
Expand All @@ -184,18 +182,18 @@ static ILinearSolver Stiffness(Vector<double> u)
AssertSolutionsAreCorrect(Reaction, force, states);
}

void AssertSolutionIsCorrect(Vector<double> solution)
void AssertSolutionIsCorrect(Vector solution)
{
double first = solution.First();
double first = solution[0];
foreach (double d in solution)
{
Assert.Equal(first, d, decimalsPrecision);
}
}

void AssertSolutionsAreCorrect(
Func<Vector<double>, Vector<double>> reaction
, Vector<double> force
Func<Vector, Vector> reaction
, Vector force
, List<LoadState> states)
{
foreach (LoadState state in states)
Expand All @@ -206,8 +204,8 @@ Func<Vector<double>, Vector<double>> reaction
}

void AssertAreCloseEnough(
Vector<double> v1
, Vector<double> v2
Vector v1
, Vector v2
, double tolerance)
=> Assert.True((v1 - v2).Norm(2) <= tolerance);
}
Expand Down
Loading