Skip to content

Commit

Permalink
add unstructured grid
Browse files Browse the repository at this point in the history
  • Loading branch information
henrij22 committed May 16, 2024
1 parent 0490a4d commit a145487
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 4 deletions.
11 changes: 11 additions & 0 deletions dune/iga/hierarchicpatch/patchgridfactory.hh
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <nlohmann/json.hpp>

#include <dune/grid/common/gridfactory.hh>
#include <dune/iga/io/createUnstructuredGrid.hh>

namespace Dune {

Expand Down Expand Up @@ -63,6 +64,16 @@ public:
return grid;
}

template <typename UnstructuredGrid>
std::unique_ptr<UnstructuredGrid> createUnstructedGrid() const {
if (patchTrimData_.has_value()) {
auto patchGrid = std::make_unique<PatchGrid>(patchData_, patchTrimData_, parameters_);
return createUnstructuredGridImpl<UnstructuredGrid>(patchGrid.get());
}
auto patchGrid = std::make_unique<PatchGrid>(patchData_);
return createUnstructuredGridImpl<UnstructuredGrid>(patchGrid.get());
}

IGA::NURBSPatchData<dim, dimworld, ctype> patchData_;
std::optional<PatchTrimData> patchTrimData_;
std::string json_;
Expand Down
78 changes: 78 additions & 0 deletions dune/iga/io/createUnstructuredGrid.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// SPDX-FileCopyrightText: 2023 The dune-iga developers [email protected]
// SPDX-License-Identifier: LGPL-3.0-or-later

#pragma once

#include <dune/iga/io/vtk/vtkrefinedgeometries.hh>

namespace Dune::IGA {

namespace Impl {
struct Comparator
{
bool operator()(const Dune::FieldVector<double, 2>& p1, const Dune::FieldVector<double, 2>& p2) const {
if (p1[0] < p2[0])
return true;
if (p1[0] > p2[0])
return false;
return p1[1] < p2[1];
};
};
} // namespace Impl

template <typename UnstructuredGrid, typename PatchGrid>
std::unique_ptr<UnstructuredGrid> createUnstructuredGridImpl(PatchGrid* patchGrid) {
auto gridView = patchGrid->leafGridView();
auto& idSet = patchGrid->globalIdSet();

IGARefinedGeometries geometries(gridView, 0, 0);

std::set<FieldVector<double, 2>, Impl::Comparator> vertices;

for (const auto& element : elements(gridView)) {
auto id = idSet.id(element);
auto geometry = element.geometry();

std::ranges::transform(geometries.getVertices(id), std::inserter(vertices, vertices.begin()),
[&](const auto& v) { return geometry.global(v); });
}

auto gridFactory = Dune::GridFactory<UnstructuredGrid>();

for (const auto& v : vertices) {
gridFactory.insertVertex(v);
}


// Reconstruct grid
for (const auto& element : elements(gridView)) {
auto geometry = element.geometry();
auto id = idSet.id(element);

auto gt = geometries.geometryType(id);
unsigned int nSubI = gt == GeometryTypes::simplex(2) ? 3 : 4;

auto& eleVertices = geometries.getVertices(id);

for (auto subEleIdx : std::views::iota(0u, geometries.nElements(id))) {
std::vector<unsigned int> elementVertices;

for (auto subEntityIndex : std::views::iota(0u, nSubI)) {
auto localVertexIdx = geometries.vertexSubIndex(id, subEleIdx, subEntityIndex);
Dune::FieldVector<double, 2> vertex = geometry.global(eleVertices[localVertexIdx]);

// Find Idx
auto it = vertices.find(vertex);
assert(it != vertices.end());

elementVertices.push_back(std::distance(vertices.begin(), it));
}
gridFactory.insertElement(gt, elementVertices);
}
}

return gridFactory.createGrid();


}
} // namespace Dune::IGA
1 change: 1 addition & 0 deletions dune/iga/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ set(TESTS
testvtkdatacollector
testnurbsbasistrimmed
testboundarysegmentidx
testunstructuredgridcreation
)
option(ENABLE_TEST_COVERAGE "Enable test coverage" OFF)

Expand Down
8 changes: 4 additions & 4 deletions dune/iga/test/testibrareader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,10 @@ int main(int argc, char** argv) try {
Preferences::getInstance().targetAccuracy(1e-3);

t.subTest(testIbraReader<true>());
// t.subTest(testIbraReader3d<true>());
//
// t.subTest(testIbraReader<false>());
// t.subTest(testIbraReader3d<false>());
t.subTest(testIbraReader3d<true>());

t.subTest(testIbraReader<false>());
t.subTest(testIbraReader3d<false>());

t.report();

Expand Down
68 changes: 68 additions & 0 deletions dune/iga/test/testunstructuredgridcreation.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// SPDX-FileCopyrightText: 2023 The Ikarus Developers [email protected]
// SPDX-License-Identifier: LGPL-2.1-or-later
#define DUNE_CHECK_BOUNDS
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "testhelper.hh"

#include <cfenv>

#include <dune/common/exceptions.hh>
#include <dune/common/float_cmp.hh>
#include <dune/common/fvector.hh>
#include <dune/common/parallel/mpihelper.hh>
#include <dune/common/test/testsuite.hh>
#include <dune/iga/hierarchicpatch/patchgridfactory.hh>
#include <dune/iga/io/griddrawer.hh>
#include <dune/iga/io/vtk/igadatacollector.hh>
#include <dune/iga/patchgrid.hh>
#include <dune/iga/trimmer/defaulttrimmer/trimmer.hh>
#include <dune/iga/trimmer/identitytrimmer/trimmer.hh>
#include <dune/vtk/vtkwriter.hh>
#include <dune/grid/uggrid.hh>

using namespace Dune::IGA;


auto testUnstructuredGridCreation() {
Dune::TestSuite t("", Dune::TestSuite::ThrowPolicy::ThrowOnRequired);

using PatchGrid = PatchGrid<2, 2, DefaultTrim::PatchGridFamily>;
using GridFactory = Dune::GridFactory<PatchGrid>;

auto gridFactory = GridFactory();
gridFactory.insertTrimParameters(typename GridFactory::TrimParameterType{100});

gridFactory.insertJson("auxiliaryfiles/quarter_plate.ibra", true, {3, 3});

auto grid = gridFactory.createUnstructedGrid<Dune::UGGrid<2>>();

Dune::Vtk::VtkWriter writer(grid->leafGridView());
writer.write("out/unstructuredGrid");

return t;
}


int main(int argc, char** argv) try {
// feenableexcept(FE_ALL_EXCEPT & ~FE_INEXACT);

// Initialize MPI, if necessary
Dune::MPIHelper::instance(argc, argv);
Dune::TestSuite t("", Dune::TestSuite::ThrowPolicy::ThrowOnRequired);

createOutputFolder("out");
Preferences::getInstance().targetAccuracy(1e-3);

t.subTest(testUnstructuredGridCreation());


t.report();

return t.exit();
} catch (Dune::Exception& e) {
std::cerr << "Dune reported error: " << e << std::endl;
return 1;
}

0 comments on commit a145487

Please sign in to comment.