Skip to content

Commit

Permalink
GRIDEDIT-953 Chaged the profile of the compute function
Browse files Browse the repository at this point in the history
  • Loading branch information
BillSenior committed Jun 17, 2024
1 parent 11b2af1 commit 8d86872
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,6 @@ namespace meshkernel
class CurvilinearGridSplineToGrid
{
public:
/// @brief Generate the curvilinear grid from the set of splines
void Compute(const Splines& splines,
const CurvilinearParameters& curvilinearParameters,
CurvilinearGrid& grid) const;

/// @brief Generate the curvilinear grid from the set of splines
CurvilinearGrid Compute(const Splines& splines,
const CurvilinearParameters& curvilinearParameters) const;
Expand All @@ -55,6 +50,9 @@ namespace meshkernel
/// @brief The maximum number of checks for unlabeled splines
static const UInt MaximumCumulativeUnlabeledSplineCount = 1000;

/// @brief The maximum refinement factor.
static const int MaximumRefinementFactor = 1000;

/// @brief Array of doubles
using DoubleVector = std::vector<double>;

Expand Down
24 changes: 11 additions & 13 deletions libs/MeshKernel/src/CurvilinearGrid/CurvilinearGridSplineToGrid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@
#include <algorithm>
#include <iomanip>

void meshkernel::CurvilinearGridSplineToGrid::Compute(const Splines& splines,
const CurvilinearParameters& curvilinearParameters,
CurvilinearGrid& grid) const
meshkernel::CurvilinearGrid meshkernel::CurvilinearGridSplineToGrid::Compute(const Splines& splines,
const CurvilinearParameters& curvilinearParameters) const
{

if (splines.GetNumSplines() < 4)
{
throw ConstraintError("At least 4 splines are required to generate a grid, number of splines is {}", splines.GetNumSplines());
Expand All @@ -20,6 +18,14 @@ void meshkernel::CurvilinearGridSplineToGrid::Compute(const Splines& splines,
throw ConstraintError("One or more splines has one or fewer support points");
}

if (std::max(curvilinearParameters.m_refinement, curvilinearParameters.n_refinement) > MaximumRefinementFactor)
{
throw ConstraintError("Reduce the refinement factor to about 50 or less, currently n- and m-fac are: {} and {} respectively",
curvilinearParameters.n_refinement, curvilinearParameters.m_refinement);
}

CurvilinearGrid grid(splines.m_projection);

// Make copy of spline because the may be changed
// i.e. the order may change and the number of spline points may increase.
Splines splinesCopy(splines);
Expand All @@ -36,13 +42,7 @@ void meshkernel::CurvilinearGridSplineToGrid::Compute(const Splines& splines,
OrderSplines(splinesCopy, numMSplines, splineIntersections);
DetermineSplineOrientation(splinesCopy, numMSplines, splineIntersections, splineInteraction);
GenerateGrid(splinesCopy, splineIntersections, splineInteraction, numMSplines, mRefinement, nRefinement, grid);
}

meshkernel::CurvilinearGrid meshkernel::CurvilinearGridSplineToGrid::Compute(const Splines& splines,
const CurvilinearParameters& curvilinearParameters) const
{
CurvilinearGrid grid(splines.m_projection);
Compute(splines, curvilinearParameters, grid);
return grid;
}

Expand All @@ -69,7 +69,7 @@ void meshkernel::CurvilinearGridSplineToGrid::DetermineIntersection(Splines& spl
{
Point intersectionPoint;

// TODO Need way of getting the number of times the spline intersect.
// Need way of getting the number of times the spline intersect.
if (splines.GetSplinesIntersection(splineI, splineJ, crossProductOfIntersection, intersectionPoint, firstNormalisedIntersectionLength, secondNormalisedIntersectionLength))
{
numberTimesCrossing = 1;
Expand Down Expand Up @@ -123,7 +123,6 @@ bool meshkernel::CurvilinearGridSplineToGrid::ComputeAndCheckIntersection(Spline
double normalisedIntersectionSplineJ;
UInt numberTimesCrossing = 0;

// TODO need to know if two splines cross more than 1 time
DetermineIntersection(splines, splineI, splineJ, numberTimesCrossing, crossProduct, normalisedIntersectionSplineI, normalisedIntersectionSplineJ);

if (numberTimesCrossing == 1)
Expand Down Expand Up @@ -901,6 +900,5 @@ void meshkernel::CurvilinearGridSplineToGrid::GenerateGrid(const Splines& spline

GenerateGridPointsAlongSpline(splines, splineIntersections, splineInteraction, numMSplines, mRefinement, nRefinement, gridNodes);
FillPatchesWithPoints(splines, numMSplines, mRefinement, nRefinement, gridNodes);

grid.SetGridNodes(gridNodes);
}
53 changes: 42 additions & 11 deletions libs/MeshKernel/tests/src/CurvilinearGridFromSplinesTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1191,9 +1191,8 @@ TEST(CurvilinearGridFromSplines, GenerateSimpleGridFromSplines)
curvilinearParameters.n_refinement = 20;

mk::CurvilinearGridSplineToGrid splinesToGrid;
mk::CurvilinearGrid grid(Projection::cartesian);

splinesToGrid.Compute(*splines, curvilinearParameters, grid);
mk::CurvilinearGrid grid(splinesToGrid.Compute(*splines, curvilinearParameters));

lin_alg::Matrix<Point> gridNodes = grid.GetNodes();

Expand Down Expand Up @@ -1231,9 +1230,8 @@ TEST(CurvilinearGridFromSplines, GridFromSeventySplines)
curvilinearParameters.n_refinement = 5;

mk::CurvilinearGridSplineToGrid splinesToGrid;
mk::CurvilinearGrid grid(Projection::cartesian);

splinesToGrid.Compute(*splines, curvilinearParameters, grid);
mk::CurvilinearGrid grid(splinesToGrid.Compute(*splines, curvilinearParameters));

auto computedPoints = grid.ComputeNodes();
auto computedEdges = grid.ComputeEdges();
Expand Down Expand Up @@ -1289,9 +1287,8 @@ TEST(CurvilinearGridFromSplines, GenerateGridWithIllDefinedSplines)
curvilinearParameters.n_refinement = 10;

mk::CurvilinearGridSplineToGrid splinesToGrid;
mk::CurvilinearGrid grid(Projection::cartesian);

EXPECT_THROW(splinesToGrid.Compute(*splines, curvilinearParameters, grid), mk::AlgorithmError);
EXPECT_THROW([[maybe_unused]] auto grid = splinesToGrid.Compute(*splines, curvilinearParameters), mk::AlgorithmError);
}

TEST(CurvilinearGridFromSplines, GenerateGridWithDisconectedSpline)
Expand Down Expand Up @@ -1332,7 +1329,7 @@ TEST(CurvilinearGridFromSplines, GenerateGridWithDisconectedSpline)
mk::CurvilinearGridSplineToGrid splinesToGrid;
mk::CurvilinearGrid grid(Projection::cartesian);

EXPECT_THROW(splinesToGrid.Compute(*splines, curvilinearParameters, grid), mk::AlgorithmError);
EXPECT_THROW([[maybe_unused]] auto grid = splinesToGrid.Compute(*splines, curvilinearParameters), mk::AlgorithmError);
}

TEST(CurvilinearGridFromSplines, GenerateGridWithThreeSplines)
Expand All @@ -1358,9 +1355,8 @@ TEST(CurvilinearGridFromSplines, GenerateGridWithThreeSplines)
curvilinearParameters.n_refinement = 5;

mk::CurvilinearGridSplineToGrid splinesToGrid;
mk::CurvilinearGrid grid(Projection::cartesian);

EXPECT_THROW(splinesToGrid.Compute(*splines, curvilinearParameters, grid), mk::ConstraintError);
EXPECT_THROW([[maybe_unused]] auto grid = splinesToGrid.Compute(*splines, curvilinearParameters), mk::ConstraintError);
}

TEST(CurvilinearGridFromSplines, GenerateGridWithSplinesTooShort)
Expand Down Expand Up @@ -1388,7 +1384,42 @@ TEST(CurvilinearGridFromSplines, GenerateGridWithSplinesTooShort)
curvilinearParameters.n_refinement = 5;

mk::CurvilinearGridSplineToGrid splinesToGrid;
mk::CurvilinearGrid grid(Projection::cartesian);

EXPECT_THROW(splinesToGrid.Compute(*splines, curvilinearParameters, grid), mk::ConstraintError);
EXPECT_THROW([[maybe_unused]] auto grid = splinesToGrid.Compute(*splines, curvilinearParameters), mk::ConstraintError);
}

TEST(CurvilinearGridFromSplines, GenerateGridWithHighRefinementFactor)
{
// Attempt to generate a grid with 4 splines forming a square
// with the refinement factor being too high
// Should raise a ConstraintError exception

namespace mk = meshkernel;

std::vector<mk::Point> spline1{{-1.0, 0.0}, {11.0, 0.0}};
std::vector<mk::Point> spline2{{10.0, -1.0}, {10.0, 11.0}};
std::vector<mk::Point> spline3{{11.0, 10.0}, {-1.0, 10.0}};
std::vector<mk::Point> spline4{{0.0, 11.0}, {0.0, -1.0}};

auto splines = std::make_shared<Splines>(Projection::cartesian);
splines->AddSpline(spline1);
splines->AddSpline(spline2);
splines->AddSpline(spline3);
splines->AddSpline(spline4);

mk::CurvilinearParameters curvilinearParameters;

mk::CurvilinearGridSplineToGrid splinesToGrid;

// First check the m-refinement
curvilinearParameters.m_refinement = 2000;
curvilinearParameters.n_refinement = 10;

EXPECT_THROW([[maybe_unused]] auto gridM = splinesToGrid.Compute(*splines, curvilinearParameters), mk::ConstraintError);

// Then check the n-refinement
curvilinearParameters.m_refinement = 10;
curvilinearParameters.n_refinement = 2000;

EXPECT_THROW([[maybe_unused]] auto gridN = splinesToGrid.Compute(*splines, curvilinearParameters), mk::ConstraintError);
}

0 comments on commit 8d86872

Please sign in to comment.