Commit fa24f5f8 by Praetorius, Simon

FileWriter cleaned up

parent ce0fece2
 ... ... @@ -29,7 +29,7 @@ The corresponding weak form of the equation reads: with \f$u\in V_g \f$. Thus, we need to define a grid (discretization of \f$\Omega \f$) and a finite element space (discretization of \f$V_0 \f$ and \f$V_g \f$). This needs to be element space (discretization of \f$V_0 \f$ and \f$V_g \f$). This cab be provided as Traits parameter in the ProblemStat class: ~~~~~~~~~~~~~~~{.cpp} ... ... @@ -37,7 +37,7 @@ using Grid = Dune::AlbertaGrid; using Traits = LagrangeBasis; ~~~~~~~~~~~~~~~ This defines and AlbertaGrid as grid and a Lagrange Finite-Element space with where AlbertaGrid defines a grid and Traits a Lagrange Finite-Element space with local polynomial degree 1 on the elements. All AMDiS programs start with initialization of the library, using AMDiS::init ... ... @@ -62,7 +62,7 @@ prob.initialize(INIT_ALL); Operators specify the (bi-)linear-form and the coefficient function in the term, see \ref operators for a list of possible types. The bilinear-form in the Poisson equation consists of second-order term with coefficitn = 1 and the linear-form equation consists of a second-order term with coefficient = 1 and the linear-form includes the function \f$f(x)=-1 \f$: ~~~~~~~~~~~~~~~{.cpp} ... ... @@ -74,8 +74,8 @@ prob.addVectorOperator(opF, 0); ~~~~~~~~~~~~~~~ Boundary conditions, in the example above a Dirichlet condition, is specified by defining a predicate for the boundary \f$\partial\Omega \f$ and the values on the boundary \f$g(x) = 0 \f$: defining a predicate for the boundary \f$\Gamma\subset\partial\Omega \f$ and the values on the boundary \f$g(x) = 0 \f$: ~~~~~~~~~~~~~~~{.cpp} auto predicate = [](auto const& x){ return x[0] < 1.e-8 || x[1] < 1.e-8; }; ... ... @@ -93,7 +93,7 @@ adapt.adapt(); // assemble and solve Finally, finish the AMDiS program with AMDiS::finish(). In complete program then reads: The complete program then reads: ~~~~~~~~~~~~~~~{.cpp} #include #include ... ...
 ... ... @@ -10,20 +10,48 @@ #include #include #include #include #include #include #include namespace AMDiS { namespace Impl { template struct VTKFieldTypeImpl; template <> struct VTKFieldTypeImpl { static const Dune::VTK::FieldInfo::Type value = Dune::VTK::FieldInfo::Type::scalar; }; template <> struct VTKFieldTypeImpl { static const Dune::VTK::FieldInfo::Type value = Dune::VTK::FieldInfo::Type::vector; }; template <> struct VTKFieldTypeImpl { static const Dune::VTK::FieldInfo::Type value = Dune::VTK::FieldInfo::Type::tensor; }; } // end namespace Impl template constexpr Dune::VTK::FieldInfo::Type VTKFieldType = Impl::VTKFieldTypeImpl>::value; template constexpr std::size_t VTKFieldSize = Size; template class FileWriter : public FileWriterInterface { private: // typedefs and static constants using GlobalBasis = typename Traits::GlobalBasis; using GridView = typename GlobalBasis::GridView; using Vector = DOFVectorConstView; using Range = typename Vector::Range; /// Dimension of the mesh static constexpr int dim = GridView::dimension; ... ... @@ -31,117 +59,57 @@ namespace AMDiS /// Dimension of the world static constexpr int dow = GridView::dimensionworld; using Vector = DOFVector; public: /// Constructor. FileWriter(std::string baseName, std::shared_ptr const& basis, TreePath const& tp, std::shared_ptr const& vector) Vector const& dofvector) : FileWriterInterface(baseName) , basis_(basis) , treePath_(tp) , vector_(vector) , dofvector_(dofvector) { //int subSampling = Parameters::get(baseName + "->subsampling").value_or(0); int m = Parameters::get(baseName + "->ParaView mode").value_or(0); mode_ = (m == 0 ? Dune::VTK::ascii : Dune::VTK::appendedraw); //if (subSampling > 0) // vtkWriter_ = std::make_shared>(basis_->gridView(), subSampling); //else vtkWriter_ = std::make_shared>(basis_->gridView()); int subSampling = Parameters::get(baseName + "->subsampling").value_or(0); vtkSeqWriter_ = std::make_shared>(vtkWriter_, filename_, dir_, ""); if (subSampling > 0) vtkWriter_ = std::make_shared>(gridView(), subSampling); else vtkWriter_ = std::make_shared>(gridView()); mode_ = Parameters::get(baseName + "->ParaView mode").value_or(0); vtkSeqWriter_ = std::make_shared>(vtkWriter_, filename_, dir_, ""); vtkWriter_->addVertexData(dofvector_, Dune::VTK::FieldInfo(name_, VTKFieldType, VTKFieldSize)); } /// Implements \ref FileWriterInterface::writeFiles virtual void writeFiles(AdaptInfo& adaptInfo, bool force) override { using Tree = typename GlobalBasis::LocalView::Tree; using Node = typename Dune::TypeTree::ChildForTreePath; writeVertexData(typename Node::NodeTag{}, index_, [&,this]() { if (mode_ == 0) vtkSeqWriter_->write(adaptInfo.getTime(), Dune::VTK::ascii); else if (mode_ == 1) vtkSeqWriter_->write(adaptInfo.getTime(), Dune::VTK::appendedraw); }); vtkSeqWriter_->write(adaptInfo.getTime(), mode_); } protected: template void writeVertexData(Dune::TypeTree::LeafNodeTag, index_t<0>, W write) GridView const& gridView() const { using Dune::Functions::BasisBuilder::makeBasis; using Dune::Functions::BasisBuilder::lagrange; auto fct = makeDiscreteFunctionPtr(basis_,treePath_,vector_); auto p1basis = makeBasis(basis_->gridView(), lagrange<1>()); auto data = makeDOFVector(p1basis, name_); interpolate(p1basis, data, *fct); auto dataFct = Dune::Functions::makeDiscreteGlobalBasisFunction(p1basis,data); vtkWriter_->addVertexData(dataFct, Dune::VTK::FieldInfo(name_, Dune::VTK::FieldInfo::Type::scalar, 1)); write(); vtkWriter_->clear(); } template void writeVertexData(Dune::TypeTree::PowerNodeTag, index_t, W write) { using Dune::Functions::BasisBuilder::makeBasis; using Dune::Functions::BasisBuilder::lagrange; using Dune::Functions::BasisBuilder::power; using Dune::Functions::BasisBuilder::flatLexicographic; assert( C == dow ); auto fct = makeDiscreteFunctionPtr(basis_,treePath_,vector_); auto p1basis = makeBasis(basis_->gridView(), power(lagrange<1>(), flatLexicographic())); auto data = makeDOFVector(p1basis, name_); interpolate(p1basis, data, *fct); using Range = Dune::FieldVector; auto dataFct = Dune::Functions::makeDiscreteGlobalBasisFunction(p1basis,data); vtkWriter_->addVertexData(dataFct, Dune::VTK::FieldInfo(name_, Dune::VTK::FieldInfo::Type::vector, C)); write(); vtkWriter_->clear(); return dofvector_.basis().gridView(); } template void writeVertexData(NodeTag, index_t, W) {} private: std::shared_ptr basis_; TreePath treePath_; std::shared_ptr vector_; Vector dofvector_; std::shared_ptr> vtkWriter_; std::shared_ptr> vtkSeqWriter_; // std::vector data_vector; // represents VTK::OutputType: 0...ascii, 1...appendedraw int mode_; // represents VTK::OutputType: ascii, appendedraw Dune::VTK::OutputType mode_; }; template template std::shared_ptr> makeFileWriterPtr( std::string baseName, std::shared_ptr const& basis, TreePath const& tp, std::shared_ptr const& vector) DOFVectorConstView const& dofvector) { return std::make_shared>(baseName, basis, tp, vector); return std::make_shared>(baseName, dofvector); } } // end namespace AMDiS
 ... ... @@ -173,29 +173,42 @@ namespace AMDiS : GridFunctionOperatorBase(gridFct, 0, quadCreator) {} /// Assemble a local element vector on the element that is bound. /// \brief Assemble a local element vector on the element that is bound. /** * \param contextGeometry Container for context related data * \param quad A quadrature formula * \param elementVector The output element vector * \param node The node of the test-basis **/ template void calculateElementVector(Context const&, //< container for context related data QuadratureRule const&, //< a quadrature formula ElementVector&, //< the output element vector Node const& //< the node of the test-basis ) void calculateElementVector(Context const& contextGeometry, QuadratureRule const& quad, ElementVector& elementVector, Node const& node) { error_exit("Needs to be implemented!"); } /// Assemble a local element matrix on the element that is bound. /// \brief Assemble a local element matrix on the element that is bound. /** * \param contextGeometry Container for context related data * \param quad A quadrature formula * \param elementMatrix The output element matrix * \param rowNode The node of the test-basis * \param colNode The node of the trial-basis * \param flag1 finiteelement is the same for test and trial * \param flag2 nodes are the same in the basis-tree **/ template void calculateElementMatrix(Context const&, //< container for context related data QuadratureRule const&, //< a quadrature formula ElementMatrix&, //< the output element matrix RowNode const&, //< the node of the test-basis ColNode const&, //< the node of the trial-basis std::integral_constant, //< finiteelement is the same for test and trial std::integral_constant //< nodes are the same in the basis-tree ) void calculateElementMatrix(Context const& contextGeometry, QuadratureRule const& quad, ElementMatrix& elementMatrix, RowNode const& rowNode, ColNode const& colNode, std::integral_constant flag1, std::integral_constant flag2) { error_exit("Needs to be implemented!"); } ... ... @@ -261,28 +274,28 @@ namespace AMDiS /// Store tag and expression to create a \ref GridFunctionOperator template auto makeOperator(Tag t, Expr const& expr) auto makeOperator(Tag tag, Expr const& expr) { using RawExpr = Underlying_t; static_assert(Concepts::HasGridFunctionOrder, "Polynomial degree of expression can not be deduced. You need to provide an explicit value for polynomial order or a quadrature rule in makeOperator()."); return ExpressionPreOperator{t, expr}; return ExpressionPreOperator{tag, expr}; } /// Store tag and expression and polynomial order of expression to create a \ref GridFunctionOperator template auto makeOperator(Tag t, Expr const& expr, int order, auto makeOperator(Tag tag, Expr const& expr, int order, Dune::QuadratureType::Enum qt = Dune::QuadratureType::GaussLegendre) { return ExpressionPreOperator{t, expr, order, qt}; return ExpressionPreOperator{tag, expr, order, qt}; } /// Store tag and expression and a quadrature rule to create a \ref GridFunctionOperator template auto makeOperator(Tag t, Expr const& expr, Dune::QuadratureRule const& rule) auto makeOperator(Tag tag, Expr const& expr, Dune::QuadratureRule const& rule) { return ExpressionPreOperator>{t, expr, rule}; return ExpressionPreOperator>{tag, expr, rule}; } /** @} **/ ... ...
 ... ... @@ -20,17 +20,17 @@ * \brief Defines operators to be assembled in the matrix/vector * * An Operator is a class providing methods necessary for assembling: * - void bind(Element, Geometry) and void unbind() for binding an unbinding the * - bind(Element, Geometry) and unbind() for binding an unbinding the * element to (from) an GridView entity of codim 0. Additionally the Geometry * object of the element is provided. * - Dune::QuadratureRule getQuadratureRule(Nodes...) factory for the * - getQuadratureRule(Nodes...) factory for the * quadrature rules used in assembling the operator on the element. Nodes... * is either {RowNode, ColNode} for Matrix-Operators or {Node} for a * Vector-Operator. * - void calculateElementVector(ContextGeometry, QuadratureRule, ElementVector, Node) * - calculateElementVector(ContextGeometry, QuadratureRule, ElementVector, Node) * where the ContextGeometry provides a reference to the ElementGeometry and * geometry of the LocalContext (that can be different), *or* * - void calculateElementMatrix(ContextGeometry, QuadratureRule, ElementMatrix, RowNode, ColNode, Flags...) * - calculateElementMatrix(ContextGeometry, QuadratureRule, ElementMatrix, RowNode, ColNode, Flags...) * Same as for calculateElementVector but additionally two optimization flags * are provided as bool_t<...> type: * + sameFE: the FiniteElementSpace of RowNode and ColNode are the same. ... ...
 ... ... @@ -122,8 +122,8 @@ void ProblemStat::createFileWriter() if (!Parameters::get(componentName + "->filename")) return; auto writer = makeFileWriterPtr(componentName, globalBasis, treePath, solution); filewriter.push_back(writer); auto writer = makeFileWriterPtr(componentName, this->getSolution(treePath)); filewriter.push_back(std::move(writer)); }); } ... ...
 ... ... @@ -13,45 +13,48 @@ namespace AMDiS { // some arithmetic operations with Dune::FieldVector using Dune::FieldVector; using Dune::FieldMatrix; // some arithmetic operations with FieldVector template ) > Dune::FieldVector operator*(Dune::FieldVector v, S factor) FieldVector operator*(FieldVector v, S factor) { return v *= factor; } template ) > Dune::FieldVector operator*(S factor, Dune::FieldVector v) FieldVector operator*(S factor, FieldVector v) { return v *= factor; } template ) > Dune::FieldVector operator/(Dune::FieldVector v, S factor) FieldVector operator/(FieldVector v, S factor) { return v /= factor; } // some arithmetic operations with Dune::FieldMatrix // some arithmetic operations with FieldMatrix template Dune::FieldMatrix operator+(Dune::FieldMatrix A, Dune::FieldMatrix const& B) FieldMatrix operator+(FieldMatrix A, FieldMatrix const& B) { return A += B; } template Dune::FieldMatrix operator-(Dune::FieldMatrix A, Dune::FieldMatrix const& B) FieldMatrix operator-(FieldMatrix A, FieldMatrix const& B) { return A -= B; } template Dune::FieldVector operator*(Dune::FieldMatrix const& mat, Dune::FieldVector const& vec) FieldVector operator*(FieldMatrix const& mat, FieldVector const& vec) { return Dune::FMatrixHelp::mult(mat, vec); } ... ... @@ -60,14 +63,14 @@ namespace AMDiS /// Cross-product a 2d-vector = orthogonal vector template Dune::FieldVector cross(Dune::FieldVector const& a) FieldVector cross(FieldVector const& a) { return {{ a[1], -a[0] }}; } /// Cross-product of two vectors (in 3d only) template Dune::FieldVector cross(Dune::FieldVector const& a, Dune::FieldVector const& b) FieldVector cross(FieldVector const& a, FieldVector const& b) { return {{ a[1]*b[2] - a[2]*b[1], a[2]*b[0] - a[0]*b[2], ... ... @@ -76,7 +79,7 @@ namespace AMDiS /// Dot product (vec1^T * vec2) template auto dot(Dune::FieldVector const& vec1, Dune::FieldVector const& vec2) auto dot(FieldVector const& vec1, FieldVector const& vec2) { return vec1.dot(vec2); } ... ... @@ -86,7 +89,7 @@ namespace AMDiS namespace Impl { template T accumulate(Dune::FieldVector const& x, Operation op) T accumulate(FieldVector const& x, Operation op) { T result = 0; for (int i = 0; i < N; ++i) ... ... @@ -98,7 +101,7 @@ namespace AMDiS /// Sum of vector entires. template T sum(Dune::FieldVector const& x) T sum(FieldVector const& x) { return Impl::accumulate(x, Operation::Plus{}); } ... ... @@ -106,7 +109,7 @@ namespace AMDiS /// Dot-product with the vector itself template auto unary_dot(Dune::FieldVector const& x) auto unary_dot(FieldVector const& x) { auto op = [](auto const& a, auto const& b) { return a + Math::sqr(std::abs(b)); }; return Impl::accumulate(x, op); ... ... @@ -114,28 +117,28 @@ namespace AMDiS /// Maximum over all vector entries template auto max(Dune::FieldVector const& x) auto max(FieldVector const& x) { return Impl::accumulate(x, Operation::Max{}); } /// Minimum over all vector entries template auto min(Dune::FieldVector const& x) auto min(FieldVector const& x) { return Impl::accumulate(x, Operation::Min{}); } /// Maximum of the absolute values of vector entries template auto abs_max(Dune::FieldVector const& x) auto abs_max(FieldVector const& x) { return Impl::accumulate(x, Operation::AbsMax{}); } /// Minimum of the absolute values of vector entries template auto abs_min(Dune::FieldVector const& x) auto abs_min(FieldVector const& x) { return Impl::accumulate(x, Operation::AbsMin{}); } ... ... @@ -146,7 +149,7 @@ namespace AMDiS * \brief The 1-norm of a vector = sum_i |x_i| **/ template auto one_norm(Dune::FieldVector const& x) auto one_norm(FieldVector const& x) { auto op = [](auto const& a, auto const& b) { return a + std::abs(b); }; return Impl::accumulate(x, op); ... ... @@ -156,7 +159,7 @@ namespace AMDiS * \brief The euklidean 2-norm of a vector = sqrt( sum_i |x_i|^2 ) **/ template auto two_norm(Dune::FieldVector const& x) auto two_norm(FieldVector const& x) { return std::sqrt(unary_dot(x)); } ... ... @@ -165,7 +168,7 @@ namespace AMDiS * \brief The p-norm of a vector = ( sum_i |x_i|^p )^(1/p) **/ template auto p_norm(Dune::FieldVector const& x) auto p_norm(FieldVector const& x) { auto op = [](auto const& a, auto const& b) { return a + Math::pow

(std::abs(b)); }; return std::pow( Impl::accumulate(x, op), 1.0/p ); ... ... @@ -175,7 +178,7 @@ namespace AMDiS * \brief The infty-norm of a vector = max_i |x_i| = alias for \ref abs_max **/ template auto infty_norm(Dune::FieldVector const& x) auto infty_norm(FieldVector const& x) { return abs_max(x); } ... ... @@ -184,7 +187,7 @@ namespace AMDiS /// The euklidean distance between two vectors = |lhs-rhs|_2 template T distance(Dune::FieldVector const& lhs, Dune::FieldVector const& rhs) T distance(FieldVector const& lhs, FieldVector const& rhs) { T result = 0; for (int i = 0; i < N; ++i) ... ... @@ -196,9 +199,9 @@ namespace AMDiS /// Outer product (vec1 * vec2^T) template auto outer(Dune::FieldMatrix const& vec1, Dune::FieldMatrix const& vec2) auto outer(FieldMatrix const& vec1, FieldMatrix const& vec2) { using result_type = Dune::FieldMatrix() * std::declval() ), N, M>; using result_type = FieldMatrix() * std::declval() ), N, M>; result_type mat; for (int i = 0; i < N; ++i) for (int j = 0; j < M; ++j) ... ... @@ -209,28 +212,28 @@ namespace AMDiS // ---------------------------------------------------------------------------- template T det(Dune::FieldMatrix const& /*mat*/) T det(FieldMatrix const& /*mat*/) { return 0; } /// Determinant of a 1x1 matrix template T det(Dune::FieldMatrix const& mat) T det(FieldMatrix const& mat) { return mat[0][0]; } /// Determinant of a 2x2 matrix template T det(Dune::FieldMatrix const& mat) T det(FieldMatrix const& mat) { return mat[0][0]*mat[1][1] - mat[0][1]*mat[1][0]; } /// Determinant of a 3x3 matrix template T det(Dune::FieldMatrix const& mat) T det(FieldMatrix const& mat) { return mat[0][0]*mat[1][1]*mat[2][2] + mat[0][1]*mat[1][2]*mat[2][0] + mat[0][2]*mat[1][0]*mat[2][1] - (mat[0][2]*mat[1][1]*mat[2][0] + mat[0][1]*mat[1][0]*mat[2][2] + mat[0][0]*mat[1][2]*mat[2][1]); ... ... @@ -238,14 +241,14 @@ namespace AMDiS /// Determinant of a NxN matrix template T det(Dune::FieldMatrix const& mat) T det(FieldMatrix const& mat) { return mat.determinant();