Commit 7c6a1a36 authored by Praetorius, Simon's avatar Praetorius, Simon
Browse files

More examples added

parent afcb3282
Pipeline #6345 failed with stage
in 5 minutes and 18 seconds
...@@ -12,7 +12,7 @@ before_script: ...@@ -12,7 +12,7 @@ before_script:
- duneci-install-module https://gitlab.dune-project.org/staging/dune-typetree.git - duneci-install-module https://gitlab.dune-project.org/staging/dune-typetree.git
- duneci-install-module https://gitlab.dune-project.org/staging/dune-functions.git - duneci-install-module https://gitlab.dune-project.org/staging/dune-functions.git
- duneci-install-module https://gitlab.dune-project.org/extensions/dune-foamgrid.git - duneci-install-module https://gitlab.dune-project.org/extensions/dune-foamgrid.git
- duneci-install-module https://gitlab.mn.tu-dresden.de/spraetor/dune-curvedgeometry.git - duneci-install-module https://gitlab.mn.tu-dresden.de/iwr/dune-curvedgeometry.git
debian:11 gcc-9-20: debian:11 gcc-9-20:
<<: *common <<: *common
......
if (dune-foamgrid_FOUND OR dune-alugrid_FOUND) add_executable(example1 example1.cc)
# The examples require any grid that can be used as a surface grid target_link_dune_default_libraries(example1)
if (dune-functions_FOUND)
# This example required dune-functions
add_executable(example1 example1.cc)
target_compile_definitions(example1 PRIVATE
"DUNE_GRID_PATH=\"${PROJECT_SOURCE_DIR}/doc/grids/\"")
target_link_dune_default_libraries(example1)
endif ()
# we need a grid supporting worlddim != griddim in this example
if(Alberta_FOUND OR dune-foamgrid_FOUND OR dune-alugrid_FOUND)
add_executable(example2 example2.cc) add_executable(example2 example2.cc)
target_compile_definitions(example2 PRIVATE target_compile_definitions(example2 PRIVATE
"DUNE_GRID_PATH=\"${PROJECT_SOURCE_DIR}/doc/grids/\"") "DUNE_GRID_PATH=\"${PROJECT_SOURCE_DIR}/doc/grids/\"")
target_link_dune_default_libraries(example2) target_link_dune_default_libraries(example2)
add_dune_alberta_flags(example2 GRIDDIM 2 WORLDDIM 3)
endif()
# This example required dune-functions to define a localizable grid function
if(dune-functions_FOUND)
add_executable(example3 example3.cc)
target_link_dune_default_libraries(example3)
endif()
if (dune-functions_FOUND) # This example required dune-vtk
# This example required dune-functions if (dune-foamgrid_FOUND AND dune-vtk_FOUND)
add_executable(example3 example3.cc) add_executable(example4 example4.cc)
target_compile_definitions(example3 PRIVATE target_compile_definitions(example4 PRIVATE
"DUNE_GRID_PATH=\"${PROJECT_SOURCE_DIR}/doc/grids/\"") "DUNE_GRID_PATH=\"${PROJECT_SOURCE_DIR}/doc/grids/\"")
target_link_dune_default_libraries(example3) target_link_dune_default_libraries(example4)
endif () endif ()
if (dune-vtk_FOUND) # This example required dune-vtk
# This example required dune-vtk if (dune-vtk_FOUND)
add_executable(example4 example4.cc) add_executable(example5 example5.cc)
target_compile_definitions(example4 PRIVATE target_link_dune_default_libraries(example5)
"DUNE_GRID_PATH=\"${PROJECT_SOURCE_DIR}/doc/grids/\"")
target_link_dune_default_libraries(example4)
endif ()
endif () endif ()
add_executable(example6 example6.cc)
target_link_dune_default_libraries(example6)
add_executable(example7 example7.cc)
target_link_dune_default_libraries(example7)
#include "config.h" /**
* Example 1
* =========
* In this example show how to construct a curved grid from a reference grid. In the concrete
* setup we start with a 2d structured grid and make a torus parametrization. The resulting
* curved grid in then written to a VTK file by using a subsampling VTK writer.
**/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <bitset>
#include <cmath>
#include <dune/common/parallel/mpihelper.hh> #include <dune/common/parallel/mpihelper.hh>
#include <dune/curvedgrid/curvedgrid.hh> #include <dune/curvedgrid/curvedgrid.hh>
#include <dune/curvedgrid/gridfunctions/analyticdiscretefunction.hh> #include <dune/grid/yaspgrid.hh>
#include <dune/grid/io/file/gmshreader.hh> #include <dune/grid/io/file/vtk.hh>
using namespace Dune; using namespace Dune;
#if HAVE_DUNE_FOAMGRID
#include <dune/foamgrid/foamgrid.hh>
using GridType = FoamGrid<2,3>;
#elif HAVE_DUNE_ALUGRID
#include <dune/alugrid/grid.hh>
using GridType = Dune::ALUGrid<2,3,Dune::simplex,Dune::conforming>;
#endif
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
MPIHelper::instance(argc, argv); MPIHelper::instance(argc, argv);
// 1. Construct a reference grid // 1. Construct a (periodic) reference grid [0,2pi]x[0,2pi]
auto refGrid = GmshReader<GridType>::read(DUNE_GRID_PATH "sphere.msh"); Dune::YaspGrid<2> refGrid{ {2.0*M_PI, 2.0*M_PI}, {8, 16}, std::bitset<2>("11") };
// 2. Define the geometry mapping // 2. Define the geometry mapping
auto sphere = [](const auto& x) { return x / x.two_norm(); }; auto torus = [r1=2.0,r2=1.0](FieldVector<double,2> const& u) -> FieldVector<double,3> {
auto sphereGF = analyticDiscreteFunction(sphere, *refGrid, /*order*/ 3); return {
(r1 + r2*std::cos(u[0])) * std::cos(u[1]),
(r1 + r2*std::cos(u[0])) * std::sin(u[1]),
r2*std::sin(u[0])
};
};
// 3. Wrap the reference grid to build a curved grid, with Lagrange elements of order 3
CurvedGrid grid{refGrid, torus, 3};
// 3. Wrap the reference grid to build a curved grid // 4. Write grid to vtu file
CurvedGrid grid{*refGrid, sphereGF}; SubsamplingVTKWriter writer{grid.leafGridView(), refinementIntervals(3)};
writer.write("torus");
} }
\ No newline at end of file
#include "config.h" /**
* Example 2
* =========
* In this example we read a piecewise flat grid from a mesh file and use a simple projection
* in order to generate a curved surface grid. This is demonstrated with the sphere parametrization.
**/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <type_traits> #include <type_traits>
...@@ -8,7 +17,10 @@ ...@@ -8,7 +17,10 @@
using namespace Dune; using namespace Dune;
#if HAVE_DUNE_FOAMGRID #if HAVE_ALBERTA
#include <dune/grid/albertagrid.hh>
using GridType = AlbertaGrid<2,3>;
#elif HAVE_DUNE_FOAMGRID
#include <dune/foamgrid/foamgrid.hh> #include <dune/foamgrid/foamgrid.hh>
using GridType = FoamGrid<2,3>; using GridType = FoamGrid<2,3>;
#elif HAVE_DUNE_ALUGRID #elif HAVE_DUNE_ALUGRID
......
#include "config.h" /**
* Example 3
* =========
* We demonstrate how to construct a curved grid from a discrete function, i.e., a coefficient
* vector and a function-space basis. For this, we use dune-functions, but any other implementation
* of localizable (discrete) functions can be used instead. These discrete representations of the
* surface allow an simple implementation of evolving surfaces. Just the coefficient vector needs
* to be updated in order to move the surface nodes.
**/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <dune/common/parallel/mpihelper.hh> #include <dune/common/parallel/mpihelper.hh>
#include <dune/curvedgrid/curvedgrid.hh> #include <dune/curvedgrid/curvedgrid.hh>
#include <dune/curvedgrid/gridfunctions/discretegridviewfunction.hh> #include <dune/curvedgrid/gridfunctions/discretegridviewfunction.hh>
#include <dune/functions/functionspacebases/interpolate.hh> #include <dune/functions/functionspacebases/interpolate.hh>
#include <dune/grid/io/file/gmshreader.hh> #include <dune/grid/yaspgrid.hh>
#include <dune/grid/io/file/vtk.hh>
using namespace Dune; using namespace Dune;
#if HAVE_DUNE_FOAMGRID
#include <dune/foamgrid/foamgrid.hh>
using GridType = FoamGrid<2,3>;
#elif HAVE_DUNE_ALUGRID
#include <dune/alugrid/grid.hh>
using GridType = Dune::ALUGrid<2,3,Dune::simplex,Dune::conforming>;
#endif
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
MPIHelper::instance(argc, argv); MPIHelper::instance(argc, argv);
// Construct a reference grid // Construct a reference grid
auto refGrid = GmshReader<GridType>::read(DUNE_GRID_PATH "sphere.msh"); YaspGrid<2> refGrid{ {1.0,1.0}, {16,16} };
auto gv = refGrid->leafGridView();
// Define a discrete grid-function on the reference grid // Define a discrete grid-function on the reference grid
// with dim(range) = 3 and polynomial order k // with dim(range) = 3 and polynomial order k
auto f = discreteGridViewFunction<3>(gv, /*order*/ 3); auto f = discreteGridViewFunction<3>(refGrid.leafGridView(), /*order*/ 3);
// Interpolate the parametrization into the grid-function
auto sphere = [](const auto& x) { return x / x.two_norm(); };
Functions::interpolate(f.basis(), f.coefficients(), sphere);
// Wrap the reference grid and the grid-function // Wrap the reference grid and the grid-function
CurvedGrid grid{*refGrid, f}; CurvedGrid grid{refGrid, f};
// 4. Write grid to vtu file
auto gv = grid.leafGridView();
using GridView = decltype(gv);
auto writer = std::make_shared<SubsamplingVTKWriter<GridView>>(gv, refinementIntervals(3));
auto pvdwriter = VTKSequenceWriter<GridView>{writer, "evolving"};
for (double t = 0.0; t < 10.0; t += 0.1)
{
std::cout << "time t = " << t << std::endl;
// Interpolate the parametrization into the grid-function
// This updates the coefficients. Automatically, the curved grid gets
// a new shape.
Functions::interpolate(f.basis(), f.coefficients(),
[t](const FieldVector<double,2>& x) -> FieldVector<double,3>
{
return {x[0], x[1], 0.2*std::sin(2*x[0]*x[1] + t)};
});
pvdwriter.write(t);
}
} }
\ No newline at end of file
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- /**
// vi: set et ts=4 sw=2 sts=2: * Example 4
* =========
* This example demonstrates how to interact with the dune-vtk writers and readers for parametrized
* grids. This dune module provides grid writers that support Lagrange parametrized elements.
* Also a file reader is available that exports the local element parametrization via a grid function
* that can be used to construct a new parametrized (curved) grid.
**/
#ifdef HAVE_CONFIG_H #ifdef HAVE_CONFIG_H
# include "config.h" # include "config.h"
...@@ -8,9 +14,6 @@ ...@@ -8,9 +14,6 @@
#include <dune/common/parallel/mpihelper.hh> // An initializer of MPI #include <dune/common/parallel/mpihelper.hh> // An initializer of MPI
#include <dune/common/exceptions.hh> // We use exceptions #include <dune/common/exceptions.hh> // We use exceptions
#include <dune/grid/uggrid.hh>
#include <dune/grid/utility/structuredgridfactory.hh>
#include <dune/vtk/vtkreader.hh> #include <dune/vtk/vtkreader.hh>
#include <dune/vtk/gridcreators/lagrangegridcreator.hh> #include <dune/vtk/gridcreators/lagrangegridcreator.hh>
#include <dune/vtk/writers/vtkunstructuredgridwriter.hh> #include <dune/vtk/writers/vtkunstructuredgridwriter.hh>
......
/**
* Example 5
* =========
* This example demonstrates how construct a 2d-3d parametrized surface. We use the sphere and a
* torus as examples. A third example is a graph-representation of a surface.
**/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <dune/common/parallel/mpihelper.hh>
#include <dune/curvedgrid/curvedgrid.hh>
#include <dune/curvedgrid/gridfunctions/analyticdiscretefunction.hh>
#include <dune/grid/yaspgrid.hh>
#include <dune/grid/io/file/gmshreader.hh>
#include <dune/vtk/writers/vtkunstructuredgridwriter.hh>
#include <dune/vtk/datacollectors/lagrangedatacollector.hh>
using namespace Dune;
template <int order, class Grid>
void write_grid (const Grid& grid, std::string filename)
{
Vtk::LagrangeDataCollector dataCollector{grid.leafGridView(), order};
VtkUnstructuredGridWriter vtkWriter{dataCollector, Vtk::FormatTypes::ASCII};
vtkWriter.write(filename);
}
void sphere()
{
// 1. Construct a reference grid
YaspGrid<2, EquidistantOffsetCoordinates<double,2>> refGrid({0.0, 0.01}, {2.0*M_PI, M_PI-0.01}, {8, 8});
// 2. Define the geometry mapping
auto sphere = [](const FieldVector<double,2>& u)
{
return FieldVector<double,3>{
std::cos(u[0])*std::sin(u[1]),
std::sin(u[0])*std::sin(u[1]),
std::cos(u[1])
};
};
auto sphereGF = analyticDiscreteFunction(sphere, refGrid, /*order*/ 3);
// 3. Wrap the reference grid to build a curved grid
CurvedGrid grid{refGrid, sphereGF};
write_grid<3>(grid, "sphere.vtu");
}
void torus()
{
// 1. Construct a reference grid
YaspGrid<2> refGrid({2.0*M_PI, 2.0*M_PI}, {8, 8});
// 2. Define the geometry mapping
auto torus = [](const FieldVector<double,2>& u)
{
return FieldVector<double,3>{
(2.0 + std::cos(u[1]))*std::cos(u[0]),
(2.0 + std::cos(u[1]))*std::sin(u[0]),
std::sin(u[1])
};
};
auto torusGF = analyticDiscreteFunction(torus, refGrid, /*order*/ 3);
// 3. Wrap the reference grid to build a curved grid
CurvedGrid grid{refGrid, torusGF};
write_grid<3>(grid, "torus.vtu");
}
void graph()
{
// 1. Construct a reference grid
YaspGrid<2> refGrid({4.0*M_PI, 4.0*M_PI}, {8, 8});
// 2. Define the geometry mapping
auto torus = [](const FieldVector<double,2>& u)
{
return FieldVector<double,3>{u[0], u[1],
std::sin(u[0])*std::cos(u[1])
};
};
auto torusGF = analyticDiscreteFunction(torus, refGrid, /*order*/ 3);
// 3. Wrap the reference grid to build a curved grid
CurvedGrid grid{refGrid, torusGF};
write_grid<3>(grid, "graph.vtu");
}
int main(int argc, char** argv)
{
MPIHelper::instance(argc, argv);
/*
* This example shows parametrized surfaces, a sphere and a torus,
* given by a 2d parameter domain and a mapping into 3d.
* Note, to build a closed surface, the parameter domain must be
* equipped with some kind of periodicity constraint, not shown
* in this example. Use periodic grid wrappers or periodic basis
* wrappers when attaching a discrete function space to the grid.
*/
sphere();
torus();
graph();
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment