From 6422d8a8a38736bc13c7bb9130a04963a4bc614a Mon Sep 17 00:00:00 2001 From: DennisGlaeser Date: Sat, 2 Dec 2017 13:14:59 +0100 Subject: [PATCH 1/4] [tpfa][scv, scvf] return position vectors by const ref --- .../cellcentered/subcontrolvolume.hh | 29 ++++++++++++------- .../cellcentered/tpfa/subcontrolvolumeface.hh | 13 +++++---- 2 files changed, 25 insertions(+), 17 deletions(-) diff --git a/dumux/discretization/cellcentered/subcontrolvolume.hh b/dumux/discretization/cellcentered/subcontrolvolume.hh index a9d1dc4db8..30bab78c22 100644 --- a/dumux/discretization/cellcentered/subcontrolvolume.hh +++ b/dumux/discretization/cellcentered/subcontrolvolume.hh @@ -24,7 +24,7 @@ #define DUMUX_DISCRETIZATION_CC_SUBCONTROLVOLUME_HH #include -#include +#include namespace Dumux { @@ -48,7 +48,11 @@ public: // the contructor in the cc case CCSubControlVolume(Geometry&& geometry, GridIndexType elementIndex) - : ParentType(), geometry_(std::forward(geometry)), elementIndex_(elementIndex) {} + : ParentType() + , geometry_(std::move(geometry)) + , center_(geometry_.value().center()) + , elementIndex_(elementIndex) + {} //! The copy constrcutor CCSubControlVolume(const CCSubControlVolume& other) = default; @@ -62,28 +66,30 @@ public: // We want to use the default copy/move assignment. // But since geometry is not copy assignable :( we // have to construct it again - geometry_.release(); + geometry_.reset(); geometry_.emplace(other.geometry_.value()); + center_ = other.center_; elementIndex_ = other.elementIndex_; return *this; } //! The move assignment operator - CCSubControlVolume& operator=(CCSubControlVolume&& other) + CCSubControlVolume& operator=(CCSubControlVolume&& other) noexcept { // We want to use the default copy/move assignment. // But since geometry is not copy assignable :( we // have to construct it again - geometry_.release(); - geometry_.emplace(other.geometry_.value()); + geometry_.reset(); + geometry_.emplace(std::move(other.geometry_.value())); + center_ = std::move(other.center_); elementIndex_ = std::move(other.elementIndex_); return *this; } //! The center of the sub control volume - GlobalPosition center() const + const GlobalPosition& center() const { - return geometry().center(); + return center_; } //! The volume of the sub control volume @@ -113,9 +119,9 @@ public: } // The position of the dof this scv is embedded in - GlobalPosition dofPosition() const + const GlobalPosition& dofPosition() const { - return geometry().center(); + return center_; } //! The global index of the element this scv is embedded in @@ -133,7 +139,8 @@ public: private: // Work around the fact that geometry is not default constructible - Optional geometry_; + Dune::Std::optional geometry_; + GlobalPosition center_; GridIndexType elementIndex_; }; diff --git a/dumux/discretization/cellcentered/tpfa/subcontrolvolumeface.hh b/dumux/discretization/cellcentered/tpfa/subcontrolvolumeface.hh index e71d5b4315..a2e800d988 100644 --- a/dumux/discretization/cellcentered/tpfa/subcontrolvolumeface.hh +++ b/dumux/discretization/cellcentered/tpfa/subcontrolvolumeface.hh @@ -81,13 +81,13 @@ public: } //! The center of the sub control volume face - GlobalPosition center() const + const GlobalPosition& center() const { return center_; } //! The integration point for flux evaluations in global coordinates - GlobalPosition ipGlobal() const + const GlobalPosition& ipGlobal() const { // Return center for now return center_; @@ -106,7 +106,7 @@ public: } //! The unit outer normal of the sub control volume face - GlobalPosition unitOuterNormal() const + const GlobalPosition& unitOuterNormal() const { return unitOuterNormal_; } @@ -136,10 +136,11 @@ public: return scvfIndex_; } - GlobalPosition corner(unsigned int localIdx) const + //! return the i-th corner of this sub control volume face + const GlobalPosition& corner(int i) const { - assert(localIdx < corners_.size() && "provided index exceeds the number of corners"); - return corners_[localIdx]; + assert(i < corners_.size() && "provided index exceeds the number of corners"); + return corners_[i]; } //! The geometry of the sub control volume face -- GitLab From a8d36faeff61a0b09087933585261e04d774bfd4 Mon Sep 17 00:00:00 2001 From: DennisGlaeser Date: Sat, 2 Dec 2017 13:18:15 +0100 Subject: [PATCH 2/4] [tpfa][fvelemgeom] make outside geometries only once for each neighbor --- .../cellcentered/tpfa/fvelementgeometry.hh | 25 ++++++++++++++----- 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/dumux/discretization/cellcentered/tpfa/fvelementgeometry.hh b/dumux/discretization/cellcentered/tpfa/fvelementgeometry.hh index 679ddc663a..a79239e690 100644 --- a/dumux/discretization/cellcentered/tpfa/fvelementgeometry.hh +++ b/dumux/discretization/cellcentered/tpfa/fvelementgeometry.hh @@ -252,13 +252,27 @@ public: void bind(const Element& element) { bindElement(element); + + neighborScvs_.reserve(element.subEntities(1)); + neighborScvfIndices_.reserve(element.subEntities(1)); + neighborScvfs_.reserve(element.subEntities(1)); + + std::vector handledNeighbors; + handledNeighbors.reserve(element.subEntities(1)); for (const auto& intersection : intersections(fvGridGeometry().gridView(), element)) { - neighborScvs_.reserve(element.subEntities(1)); - neighborScvfIndices_.reserve(element.subEntities(1)); - neighborScvfs_.reserve(element.subEntities(1)); if (intersection.neighbor()) - makeNeighborGeometries(intersection.outside()); + { + const auto outside = intersection.outside(); + const auto outsideIdx = fvGridGeometry().elementMapper().index(outside); + + // make outside geometries only if not done yet (could happen on non-conforming grids) + if ( std::find(handledNeighbors.begin(), handledNeighbors.end(), outsideIdx) == handledNeighbors.end() ) + { + makeNeighborGeometries(outside, outsideIdx); + handledNeighbors.push_back(outsideIdx); + } + } } if (dim < dimWorld) @@ -402,10 +416,9 @@ private: } //! create the necessary scvs and scvfs of the neighbor elements to the bound elements - void makeNeighborGeometries(const Element& element) + void makeNeighborGeometries(const Element& element, const IndexType eIdx) { // create the neighbor scv - const auto eIdx = fvGridGeometry().elementMapper().index(element); neighborScvs_.emplace_back(element.geometry(), eIdx); neighborScvIndices_.push_back(eIdx); -- GitLab From a44eae5b273c1654dc015c1ceff317352599cb36 Mon Sep 17 00:00:00 2001 From: DennisGlaeser Date: Sat, 2 Dec 2017 13:23:51 +0100 Subject: [PATCH 3/4] remove obsolete dumux optional class --- dumux/common/optional.hh | 154 ------------------ .../freeflow/subcontrolvolumeface.hh | 1 - 2 files changed, 155 deletions(-) delete mode 100644 dumux/common/optional.hh diff --git a/dumux/common/optional.hh b/dumux/common/optional.hh deleted file mode 100644 index 0207724e75..0000000000 --- a/dumux/common/optional.hh +++ /dev/null @@ -1,154 +0,0 @@ -// -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- -// vi: set et ts=4 sw=4 sts=4: -/***************************************************************************** - * See the file COPYING for full copying permissions. * - * * - * This program is free software: you can redistribute it and/or modify * - * it under the terms of the GNU General Public License as published by * - * the Free Software Foundation, either version 2 of the License, or * - * (at your option) any later version. * - * * - * This program is distributed in the hope that it will be useful, * - * but WITHOUT ANY WARRANTY; without even the implied warranty of * - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * - * GNU General Public License for more details. * - * * - * You should have received a copy of the GNU General Public License * - * along with this program. If not, see . * - *****************************************************************************/ -/*! - * \file - * \brief A wrapper that can either contain an object of T or be empty. - * This might be used as a workaround for non-default constructible classes. - * \note This was adopted from dune-functions. - */ -#ifndef DUMUX_COMMON_OPTIONAL_HH -#define DUMUX_COMMON_OPTIONAL_HH - -#include - -#include - -namespace Dumux { - -/** - * \brief A wrapper that can either contain an object of T or be empty - * - * \ingroup Utility - * - * \tparam T Type of wrapped objects - */ -template -class Optional -{ -public: - - Optional() : - p_(nullptr) - {} - - template = 0> - Optional(TT&& t) : - p_(nullptr) - { - emplace(std::forward(t)); - } - - Optional(Optional&& other) - { - if (other) - p_ = new (buffer_) T(std::move(other.value())); - else - p_ = nullptr; - } - - Optional(const Optional& other) - { - if (other) - p_ = new (buffer_) T(other.value()); - else - p_ = nullptr; - } - - ~Optional() - { - if (operator bool()) - p_->~T(); - } - - template = 0 > - Optional& operator=(TT&& t) - { - if (operator bool()) - *p_ = std::forward(t); - else - p_ = new (buffer_) T(std::forward(t)); - return *this; - } - - Optional& operator=(const Optional& other) - { - if (other) - *this = other.value(); - else if (operator bool()) - { - p_->~T(); - p_ = nullptr; - } - return *this; - } - - Optional& operator=(Optional&& other) - { - if (other) - *this = std::move(other.value()); - else if (operator bool()) - { - p_->~T(); - p_ = nullptr; - } - return *this; - } - - explicit operator bool() const - { - return p_; - } - - const T& value() const - { - return *p_; - } - - T& value() - { - return *p_; - } - - template< class... Args > - void emplace(Args&&... args) - { - if (operator bool()) - p_->~T(); - p_ = new (buffer_) T(std::forward(args)...); - } - - void release() - { - if (operator bool()) - { - p_->~T(); - p_ = nullptr; - } - } - -private: - - alignas(T) char buffer_[sizeof(T)]; - T* p_; -}; - - -} // namespace Dumux - -#endif // DUMUX_COMMON_OPTIONAL_HH diff --git a/dumux/discretization/staggered/freeflow/subcontrolvolumeface.hh b/dumux/discretization/staggered/freeflow/subcontrolvolumeface.hh index 6dd9cc52b9..d4a27f0920 100644 --- a/dumux/discretization/staggered/freeflow/subcontrolvolumeface.hh +++ b/dumux/discretization/staggered/freeflow/subcontrolvolumeface.hh @@ -27,7 +27,6 @@ #include #include #include -#include #include -- GitLab From 87128181727f21ef6d98b254445d87970987277d Mon Sep 17 00:00:00 2001 From: DennisGlaeser Date: Mon, 4 Dec 2017 11:27:20 +0100 Subject: [PATCH 4/4] reintroduce Dumux optional We still want to be compatible with Dune 2.5 where the optional class is not yet available. --- dumux/common/CMakeLists.txt | 1 + dumux/common/optional.hh | 154 ++++++++++++++++++ .../cellcentered/subcontrolvolume.hh | 8 +- 3 files changed, 159 insertions(+), 4 deletions(-) create mode 100644 dumux/common/optional.hh diff --git a/dumux/common/CMakeLists.txt b/dumux/common/CMakeLists.txt index c0d33e16a6..203c636756 100644 --- a/dumux/common/CMakeLists.txt +++ b/dumux/common/CMakeLists.txt @@ -9,6 +9,7 @@ exceptions.hh fixedlengthspline_.hh intersectionmapper.hh math.hh +optional.hh parameters.hh propertysystem.hh quad.hh diff --git a/dumux/common/optional.hh b/dumux/common/optional.hh new file mode 100644 index 0000000000..0207724e75 --- /dev/null +++ b/dumux/common/optional.hh @@ -0,0 +1,154 @@ +// -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- +// vi: set et ts=4 sw=4 sts=4: +/***************************************************************************** + * See the file COPYING for full copying permissions. * + * * + * This program is free software: you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation, either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program. If not, see . * + *****************************************************************************/ +/*! + * \file + * \brief A wrapper that can either contain an object of T or be empty. + * This might be used as a workaround for non-default constructible classes. + * \note This was adopted from dune-functions. + */ +#ifndef DUMUX_COMMON_OPTIONAL_HH +#define DUMUX_COMMON_OPTIONAL_HH + +#include + +#include + +namespace Dumux { + +/** + * \brief A wrapper that can either contain an object of T or be empty + * + * \ingroup Utility + * + * \tparam T Type of wrapped objects + */ +template +class Optional +{ +public: + + Optional() : + p_(nullptr) + {} + + template = 0> + Optional(TT&& t) : + p_(nullptr) + { + emplace(std::forward(t)); + } + + Optional(Optional&& other) + { + if (other) + p_ = new (buffer_) T(std::move(other.value())); + else + p_ = nullptr; + } + + Optional(const Optional& other) + { + if (other) + p_ = new (buffer_) T(other.value()); + else + p_ = nullptr; + } + + ~Optional() + { + if (operator bool()) + p_->~T(); + } + + template = 0 > + Optional& operator=(TT&& t) + { + if (operator bool()) + *p_ = std::forward(t); + else + p_ = new (buffer_) T(std::forward(t)); + return *this; + } + + Optional& operator=(const Optional& other) + { + if (other) + *this = other.value(); + else if (operator bool()) + { + p_->~T(); + p_ = nullptr; + } + return *this; + } + + Optional& operator=(Optional&& other) + { + if (other) + *this = std::move(other.value()); + else if (operator bool()) + { + p_->~T(); + p_ = nullptr; + } + return *this; + } + + explicit operator bool() const + { + return p_; + } + + const T& value() const + { + return *p_; + } + + T& value() + { + return *p_; + } + + template< class... Args > + void emplace(Args&&... args) + { + if (operator bool()) + p_->~T(); + p_ = new (buffer_) T(std::forward(args)...); + } + + void release() + { + if (operator bool()) + { + p_->~T(); + p_ = nullptr; + } + } + +private: + + alignas(T) char buffer_[sizeof(T)]; + T* p_; +}; + + +} // namespace Dumux + +#endif // DUMUX_COMMON_OPTIONAL_HH diff --git a/dumux/discretization/cellcentered/subcontrolvolume.hh b/dumux/discretization/cellcentered/subcontrolvolume.hh index 30bab78c22..f8875526ff 100644 --- a/dumux/discretization/cellcentered/subcontrolvolume.hh +++ b/dumux/discretization/cellcentered/subcontrolvolume.hh @@ -24,7 +24,7 @@ #define DUMUX_DISCRETIZATION_CC_SUBCONTROLVOLUME_HH #include -#include +#include namespace Dumux { @@ -66,7 +66,7 @@ public: // We want to use the default copy/move assignment. // But since geometry is not copy assignable :( we // have to construct it again - geometry_.reset(); + geometry_.release(); geometry_.emplace(other.geometry_.value()); center_ = other.center_; elementIndex_ = other.elementIndex_; @@ -79,7 +79,7 @@ public: // We want to use the default copy/move assignment. // But since geometry is not copy assignable :( we // have to construct it again - geometry_.reset(); + geometry_.release(); geometry_.emplace(std::move(other.geometry_.value())); center_ = std::move(other.center_); elementIndex_ = std::move(other.elementIndex_); @@ -139,7 +139,7 @@ public: private: // Work around the fact that geometry is not default constructible - Dune::Std::optional geometry_; + Optional geometry_; GlobalPosition center_; GridIndexType elementIndex_; }; -- GitLab