From 03514fb0f096925e94431532f1d02529dac125ab Mon Sep 17 00:00:00 2001 From: Elias Pipping <elias.pipping@fu-berlin.de> Date: Sat, 19 Jul 2014 22:20:12 +0200 Subject: [PATCH] [Cleanup] Modularise MyGrid --- src/Makefile.am | 1 + src/sand-wedge-data/mygrid.cc | 205 +++++++++++++---------------- src/sand-wedge-data/mygrid.hh | 33 +++++ src/sand-wedge-data/mygrid_tmpl.cc | 3 + src/sand-wedge.cc | 1 - 5 files changed, 132 insertions(+), 111 deletions(-) diff --git a/src/Makefile.am b/src/Makefile.am index 0f184e85..6467a5d9 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -7,6 +7,7 @@ common_sources = \ enumparser.cc \ fixedpointiterator.cc \ friction_writer.cc \ + sand-wedge-data/mygrid.cc \ solverfactory.cc \ state.cc \ timestepping.cc \ diff --git a/src/sand-wedge-data/mygrid.cc b/src/sand-wedge-data/mygrid.cc index 61ecdada..8755a29c 100644 --- a/src/sand-wedge-data/mygrid.cc +++ b/src/sand-wedge-data/mygrid.cc @@ -5,24 +5,21 @@ #include "mygrid.hh" #include "midpoint.hh" -class SimplexManager { -public: - using SimplexList = std::vector<std::vector<unsigned int>>; - #if DIM == 3 - SimplexManager(unsigned int shift) : shift_(shift) {} +SimplexManager::SimplexManager(unsigned int shift) : shift_(shift) {} #endif - void addFromVertices(unsigned int U, unsigned int V, unsigned int W) { +void SimplexManager::addFromVertices(unsigned int U, unsigned int V, + unsigned int W) { #if DIM == 3 - unsigned int const U2 = U + shift_; - unsigned int const V2 = V + shift_; - unsigned int const W2 = W + shift_; + unsigned int const U2 = U + shift_; + unsigned int const V2 = V + shift_; + unsigned int const W2 = W + shift_; - // back-to-front, back-to-front, back-to-front - simplices_.push_back({ U, V, W, U2 }); - simplices_.push_back({ V, V2, W2, U2 }); - simplices_.push_back({ W, W2, U2, V }); + // back-to-front, back-to-front, back-to-front + simplices_.push_back({ U, V, W, U2 }); + simplices_.push_back({ V, V2, W2, U2 }); + simplices_.push_back({ W, W2, U2, V }); // back-to-front, front-to-back, back-to-front would be /* @@ -31,130 +28,118 @@ class SimplexManager { simplices_.push_back({ V2, W2, U2, W }); */ #else - simplices_.push_back({ U, V, W }); + simplices_.push_back({ U, V, W }); #endif - } - - SimplexList const &getSimplices() { return simplices_; } - -private: - SimplexList simplices_; +} -#if DIM == 3 - unsigned int const shift_; -#endif -}; +auto SimplexManager::getSimplices() -> SimplexList const &{ return simplices_; } -template <class Grid> class GridConstructor { -public: - GridConstructor() { - auto const &A = MyGeometry::A; - auto const &B = MyGeometry::B; - auto const &C = MyGeometry::C; +template <class Grid> GridConstructor<Grid>::GridConstructor() { + auto const &A = MyGeometry::A; + auto const &B = MyGeometry::B; + auto const &C = MyGeometry::C; - auto const AB = midPoint(A, B); - auto const AC = midPoint(A, C); - auto const BC = midPoint(B, C); + auto const AB = midPoint(A, B); + auto const AC = midPoint(A, C); + auto const BC = midPoint(B, C); - auto const AAB = midPoint(A, AB); - auto const AAC = midPoint(A, AC); - auto const ABB = midPoint(AB, B); - auto const ABC = midPoint(AB, C); // the interiour point - auto const ACC = midPoint(AC, C); + auto const AAB = midPoint(A, AB); + auto const AAC = midPoint(A, AC); + auto const ABB = midPoint(AB, B); + auto const ABC = midPoint(AB, C); // the interiour point + auto const ACC = midPoint(AC, C); - unsigned int const vc = 11; + unsigned int const vc = 11; #if DIM == 3 - Dune::FieldMatrix<double, 2 * vc, DIM> vertices; + Dune::FieldMatrix<double, 2 * vc, DIM> vertices; #else - Dune::FieldMatrix<double, vc, DIM> vertices; + Dune::FieldMatrix<double, vc, DIM> vertices; #endif - for (size_t i = 0; i < 2; ++i) { - size_t k = 0; - vertices[k++][i] = A[i]; - vertices[k++][i] = AAB[i]; - vertices[k++][i] = AB[i]; - vertices[k++][i] = ABB[i]; - vertices[k++][i] = B[i]; - vertices[k++][i] = AAC[i]; - vertices[k++][i] = AC[i]; - vertices[k++][i] = ABC[i]; - vertices[k++][i] = BC[i]; - vertices[k++][i] = ACC[i]; - vertices[k++][i] = C[i]; - assert(k == vc); + for (size_t i = 0; i < 2; ++i) { + size_t k = 0; + vertices[k++][i] = A[i]; + vertices[k++][i] = AAB[i]; + vertices[k++][i] = AB[i]; + vertices[k++][i] = ABB[i]; + vertices[k++][i] = B[i]; + vertices[k++][i] = AAC[i]; + vertices[k++][i] = AC[i]; + vertices[k++][i] = ABC[i]; + vertices[k++][i] = BC[i]; + vertices[k++][i] = ACC[i]; + vertices[k++][i] = C[i]; + assert(k == vc); #if DIM == 3 - vertices[k++][i] = A[i]; - vertices[k++][i] = AAB[i]; - vertices[k++][i] = AB[i]; - vertices[k++][i] = ABB[i]; - vertices[k++][i] = B[i]; - vertices[k++][i] = AAC[i]; - vertices[k++][i] = AC[i]; - vertices[k++][i] = ABC[i]; - vertices[k++][i] = BC[i]; - vertices[k++][i] = ACC[i]; - vertices[k++][i] = C[i]; - assert(k == 2 * vc); + vertices[k++][i] = A[i]; + vertices[k++][i] = AAB[i]; + vertices[k++][i] = AB[i]; + vertices[k++][i] = ABB[i]; + vertices[k++][i] = B[i]; + vertices[k++][i] = AAC[i]; + vertices[k++][i] = AC[i]; + vertices[k++][i] = ABC[i]; + vertices[k++][i] = BC[i]; + vertices[k++][i] = ACC[i]; + vertices[k++][i] = C[i]; + assert(k == 2 * vc); #endif - } + } #if DIM == 3 - for (size_t k = 0; k < vc; ++k) { - vertices[k][2] = -MyGeometry::depth / 2.0; - vertices[k + vc][2] = MyGeometry::depth / 2.0; - } + for (size_t k = 0; k < vc; ++k) { + vertices[k][2] = -MyGeometry::depth / 2.0; + vertices[k + vc][2] = MyGeometry::depth / 2.0; + } #endif - for (size_t i = 0; i < vertices.N(); ++i) - gridFactory.insertVertex(vertices[i]); + for (size_t i = 0; i < vertices.N(); ++i) + gridFactory.insertVertex(vertices[i]); - Dune::GeometryType cell; + Dune::GeometryType cell; #if DIM == 3 - cell.makeTetrahedron(); + cell.makeTetrahedron(); #else - cell.makeTriangle(); + cell.makeTriangle(); #endif #if DIM == 3 - SimplexManager sm(vc); + SimplexManager sm(vc); #else - SimplexManager sm; + SimplexManager sm; #endif - sm.addFromVertices(1, 5, 0); - sm.addFromVertices(1, 6, 5); - sm.addFromVertices(2, 6, 1); - sm.addFromVertices(7, 6, 2); - sm.addFromVertices(7, 2, 3); - sm.addFromVertices(7, 3, 8); - sm.addFromVertices(7, 8, 10); - sm.addFromVertices(7, 10, 9); - sm.addFromVertices(7, 9, 6); - sm.addFromVertices(8, 3, 4); - auto const &simplices = sm.getSimplices(); - - // sanity-check choices of simplices - for (size_t i = 0; i < simplices.size(); ++i) { - Dune::FieldMatrix<double, DIM, DIM> check; - for (size_t j = 0; j < DIM; ++j) - check[j] = vertices[simplices[i][j + 1]] - vertices[simplices[i][j]]; - assert(check.determinant() > 0); - gridFactory.insertElement(cell, simplices[i]); - } - } - - std::shared_ptr<Grid> getGrid() { - return std::shared_ptr<Grid>(gridFactory.createGrid()); + sm.addFromVertices(1, 5, 0); + sm.addFromVertices(1, 6, 5); + sm.addFromVertices(2, 6, 1); + sm.addFromVertices(7, 6, 2); + sm.addFromVertices(7, 2, 3); + sm.addFromVertices(7, 3, 8); + sm.addFromVertices(7, 8, 10); + sm.addFromVertices(7, 10, 9); + sm.addFromVertices(7, 9, 6); + sm.addFromVertices(8, 3, 4); + auto const &simplices = sm.getSimplices(); + + // sanity-check choices of simplices + for (size_t i = 0; i < simplices.size(); ++i) { + Dune::FieldMatrix<double, DIM, DIM> check; + for (size_t j = 0; j < DIM; ++j) + check[j] = vertices[simplices[i][j + 1]] - vertices[simplices[i][j]]; + assert(check.determinant() > 0); + gridFactory.insertElement(cell, simplices[i]); } +} - template <class GridView> - MyFaces<GridView> constructFaces(GridView const &gridView) { - return MyFaces<GridView>(gridView); - } +template <class Grid> std::shared_ptr<Grid> GridConstructor<Grid>::getGrid() { + return std::shared_ptr<Grid>(gridFactory.createGrid()); +} -private: - Dune::GridFactory<Grid> gridFactory; -}; +template <class Grid> +template <class GridView> +MyFaces<GridView> GridConstructor<Grid>::constructFaces( + GridView const &gridView) { + return MyFaces<GridView>(gridView); +} template <class GridView> template <class Vector> diff --git a/src/sand-wedge-data/mygrid.hh b/src/sand-wedge-data/mygrid.hh index cd17d085..9759252b 100644 --- a/src/sand-wedge-data/mygrid.hh +++ b/src/sand-wedge-data/mygrid.hh @@ -40,4 +40,37 @@ template <class GridView> struct MyFaces { template <class Vector> bool xyBetween(Vector const &v1, Vector const &v2, Vector const &x); }; + +class SimplexManager { +public: + using SimplexList = std::vector<std::vector<unsigned int>>; + +#if DIM == 3 + SimplexManager(unsigned int shift); +#endif + + void addFromVertices(unsigned int U, unsigned int V, unsigned int W); + + SimplexList const &getSimplices(); + +private: + SimplexList simplices_; + +#if DIM == 3 + unsigned int const shift_; +#endif +}; + +template <class Grid> class GridConstructor { +public: + GridConstructor(); + + std::shared_ptr<Grid> getGrid(); + + template <class GridView> + MyFaces<GridView> constructFaces(GridView const &gridView); + +private: + Dune::GridFactory<Grid> gridFactory; +}; #endif diff --git a/src/sand-wedge-data/mygrid_tmpl.cc b/src/sand-wedge-data/mygrid_tmpl.cc index 2668596c..ea5190a1 100644 --- a/src/sand-wedge-data/mygrid_tmpl.cc +++ b/src/sand-wedge-data/mygrid_tmpl.cc @@ -7,3 +7,6 @@ template class GridConstructor<Grid>; template struct MyFaces<GridView>; + +template MyFaces<GridView> GridConstructor<Grid>::constructFaces( + GridView const &gridView); diff --git a/src/sand-wedge.cc b/src/sand-wedge.cc index 520cfc4c..2b39dbdf 100644 --- a/src/sand-wedge.cc +++ b/src/sand-wedge.cc @@ -77,7 +77,6 @@ #include "sand-wedge-data/mygeometry.hh" #include "sand-wedge-data/mygeometry.cc" // FIXME #include "sand-wedge-data/myglobalfrictiondata.hh" -#include "sand-wedge-data/mygrid.cc" // FIXME #include "sand-wedge-data/mygrid.hh" #include "sand-wedge-data/special_writer.hh" #include "solverfactory.hh" -- GitLab