From ddcd21acd694b6867905ec6e3a506d03471277d9 Mon Sep 17 00:00:00 2001 From: Chengjiang Yin Date: Thu, 9 Nov 2023 01:15:17 +0800 Subject: [PATCH] add constexpr to Point ctor and member function with unit test --- include/deal.II/base/point.h | 110 ++++++++++++++++-------------- tests/base/point_constexpr.cc | 45 ++++++++++++ tests/base/point_constexpr.output | 2 + 3 files changed, 104 insertions(+), 53 deletions(-) create mode 100644 tests/base/point_constexpr.cc create mode 100644 tests/base/point_constexpr.output diff --git a/include/deal.II/base/point.h b/include/deal.II/base/point.h index d99152f39a..768040ccba 100644 --- a/include/deal.II/base/point.h +++ b/include/deal.II/base/point.h @@ -117,13 +117,13 @@ public: * * @note This function can also be used in @ref GlossDevice "device" code. */ - DEAL_II_HOST_DEVICE + constexpr DEAL_II_HOST_DEVICE Point(); /** * Convert a tensor to a point. */ - explicit DEAL_II_HOST_DEVICE + constexpr explicit DEAL_II_HOST_DEVICE Point(const Tensor<1, dim, Number> &); /** @@ -134,7 +134,7 @@ public: * * @note This function can also be used in @ref GlossDevice "device" code. */ - explicit DEAL_II_HOST_DEVICE + constexpr explicit DEAL_II_HOST_DEVICE Point(const Number x); /** @@ -146,7 +146,7 @@ public: * * @note This function can also be used in @ref GlossDevice "device" code. */ - DEAL_II_HOST_DEVICE + constexpr DEAL_II_HOST_DEVICE Point(const Number x, const Number y); /** @@ -158,7 +158,7 @@ public: * * @note This function can also be used in @ref GlossDevice "device" code. */ - DEAL_II_HOST_DEVICE + constexpr DEAL_II_HOST_DEVICE Point(const Number x, const Number y, const Number z); /** @@ -166,8 +166,9 @@ public: */ template = 0> - Point(const boost::geometry::model:: - point &boost_pt); + constexpr Point( + const boost::geometry::model:: + point &boost_pt); /** * Return a unit vector in coordinate direction i, i.e., a vector @@ -176,15 +177,15 @@ public: * * @note This function can also be used in @ref GlossDevice "device" code. */ - static DEAL_II_HOST_DEVICE Point - unit_vector(const unsigned int i); + static constexpr DEAL_II_HOST_DEVICE Point + unit_vector(const unsigned int i); /** * Read access to the indexth coordinate. * * @note This function can also be used in @ref GlossDevice "device" code. */ - DEAL_II_HOST_DEVICE Number + constexpr DEAL_II_HOST_DEVICE Number operator()(const unsigned int index) const; /** @@ -192,7 +193,7 @@ public: * * @note This function can also be used in @ref GlossDevice "device" code. */ - DEAL_II_HOST_DEVICE Number & + constexpr DEAL_II_HOST_DEVICE Number & operator()(const unsigned int index); /** @@ -201,7 +202,7 @@ public: * convertible to @p Number. */ template - Point & + constexpr Point & operator=(const Tensor<1, dim, OtherNumber> &p); /** @@ -214,8 +215,8 @@ public: * * @note This function can also be used in @ref GlossDevice "device" code. */ - DEAL_II_HOST_DEVICE Point - operator+(const Tensor<1, dim, Number> &) const; + constexpr DEAL_II_HOST_DEVICE Point + operator+(const Tensor<1, dim, Number> &) const; /** * Subtract two points, i.e., obtain the vector that connects the two. As @@ -226,8 +227,8 @@ public: * * @note This function can also be used in @ref GlossDevice "device" code. */ - DEAL_II_HOST_DEVICE Tensor<1, dim, Number> - operator-(const Point &) const; + constexpr DEAL_II_HOST_DEVICE Tensor<1, dim, Number> + operator-(const Point &) const; /** * Subtract a difference vector (represented by a Tensor@<1,dim@>) from the @@ -237,16 +238,16 @@ public: * * @note This function can also be used in @ref GlossDevice "device" code. */ - DEAL_II_HOST_DEVICE Point - operator-(const Tensor<1, dim, Number> &) const; + constexpr DEAL_II_HOST_DEVICE Point + operator-(const Tensor<1, dim, Number> &) const; /** * The opposite vector. * * @note This function can also be used in @ref GlossDevice "device" code. */ - DEAL_II_HOST_DEVICE Point - operator-() const; + constexpr DEAL_II_HOST_DEVICE Point + operator-() const; /** * @} @@ -265,7 +266,7 @@ public: * @relatesalso EnableIfScalar */ template - DEAL_II_HOST_DEVICE Point< + constexpr DEAL_II_HOST_DEVICE Point< dim, typename ProductType::type>::type> @@ -277,7 +278,7 @@ public: * @note This function can also be used in @ref GlossDevice "device" code. */ template - DEAL_II_HOST_DEVICE Point< + constexpr DEAL_II_HOST_DEVICE Point< dim, typename ProductType::type>::type> @@ -288,7 +289,7 @@ public: * * @note This function can also be used in @ref GlossDevice "device" code. */ - DEAL_II_HOST_DEVICE Number + constexpr DEAL_II_HOST_DEVICE Number operator*(const Tensor<1, dim, Number> &p) const; /** @@ -303,8 +304,9 @@ public: * * @note This function can also be used in @ref GlossDevice "device" code. */ - DEAL_II_HOST_DEVICE typename numbers::NumberTraits::real_type - square() const; + constexpr DEAL_II_HOST_DEVICE + typename numbers::NumberTraits::real_type + square() const; /** * Return the Euclidean distance of this point to the point @@ -322,8 +324,9 @@ public: * * @note This function can also be used in @ref GlossDevice "device" code. */ - DEAL_II_HOST_DEVICE typename numbers::NumberTraits::real_type - distance_square(const Point &p) const; + constexpr DEAL_II_HOST_DEVICE + typename numbers::NumberTraits::real_type + distance_square(const Point &p) const; /** * @} @@ -347,14 +350,14 @@ public: // and we can't use 'Point::Point () = default' here. template DEAL_II_CXX20_REQUIRES(dim >= 0) -inline DEAL_II_HOST_DEVICE Point::Point() // NOLINT +constexpr DEAL_II_HOST_DEVICE Point::Point() // NOLINT {} template DEAL_II_CXX20_REQUIRES(dim >= 0) -inline DEAL_II_HOST_DEVICE Point::Point( +constexpr DEAL_II_HOST_DEVICE Point::Point( const Tensor<1, dim, Number> &t) : Tensor<1, dim, Number>(t) {} @@ -363,7 +366,7 @@ inline DEAL_II_HOST_DEVICE Point::Point( template DEAL_II_CXX20_REQUIRES(dim >= 0) -inline DEAL_II_HOST_DEVICE Point::Point(const Number x) +constexpr DEAL_II_HOST_DEVICE Point::Point(const Number x) { Assert(dim == 1, ExcMessage( @@ -389,8 +392,8 @@ inline DEAL_II_HOST_DEVICE Point::Point(const Number x) template DEAL_II_CXX20_REQUIRES(dim >= 0) -inline DEAL_II_HOST_DEVICE Point::Point(const Number x, - const Number y) +constexpr DEAL_II_HOST_DEVICE Point::Point(const Number x, + const Number y) { Assert(dim == 2, ExcMessage( @@ -411,9 +414,9 @@ inline DEAL_II_HOST_DEVICE Point::Point(const Number x, template DEAL_II_CXX20_REQUIRES(dim >= 0) -inline DEAL_II_HOST_DEVICE Point::Point(const Number x, - const Number y, - const Number z) +constexpr DEAL_II_HOST_DEVICE Point::Point(const Number x, + const Number y, + const Number z) { Assert(dim == 3, ExcMessage( @@ -438,7 +441,7 @@ template DEAL_II_CXX20_REQUIRES(dim >= 0) template > -inline Point::Point( +constexpr Point::Point( const boost::geometry::model:: point &boost_pt) { @@ -458,8 +461,8 @@ inline Point::Point( template DEAL_II_CXX20_REQUIRES(dim >= 0) -inline DEAL_II_HOST_DEVICE Point Point::unit_vector( - unsigned int i) +constexpr DEAL_II_HOST_DEVICE + Point Point::unit_vector(unsigned int i) { Point p; p[i] = 1.; @@ -469,7 +472,7 @@ inline DEAL_II_HOST_DEVICE Point Point::unit_vector( template DEAL_II_CXX20_REQUIRES(dim >= 0) -inline DEAL_II_HOST_DEVICE Number Point::operator()( +constexpr DEAL_II_HOST_DEVICE Number Point::operator()( const unsigned int index) const { AssertIndexRange(static_cast(index), dim); @@ -480,7 +483,7 @@ inline DEAL_II_HOST_DEVICE Number Point::operator()( template DEAL_II_CXX20_REQUIRES(dim >= 0) -inline DEAL_II_HOST_DEVICE Number &Point::operator()( +constexpr DEAL_II_HOST_DEVICE Number &Point::operator()( const unsigned int index) { AssertIndexRange(static_cast(index), dim); @@ -492,8 +495,8 @@ inline DEAL_II_HOST_DEVICE Number &Point::operator()( template DEAL_II_CXX20_REQUIRES(dim >= 0) template -inline DEAL_II_ALWAYS_INLINE Point &Point::operator=( - const Tensor<1, dim, OtherNumber> &p) +constexpr DEAL_II_ALWAYS_INLINE Point + &Point::operator=(const Tensor<1, dim, OtherNumber> &p) { Tensor<1, dim, Number>::operator=(p); return *this; @@ -503,7 +506,7 @@ inline DEAL_II_ALWAYS_INLINE Point &Point::operator=( template DEAL_II_CXX20_REQUIRES(dim >= 0) -inline DEAL_II_HOST_DEVICE Point Point::operator+( +constexpr DEAL_II_HOST_DEVICE Point Point::operator+( const Tensor<1, dim, Number> &p) const { Point tmp = *this; @@ -515,8 +518,9 @@ inline DEAL_II_HOST_DEVICE Point Point::operator+( template DEAL_II_CXX20_REQUIRES(dim >= 0) -inline DEAL_II_HOST_DEVICE Tensor<1, dim, Number> Point::operator-( - const Point &p) const +constexpr DEAL_II_HOST_DEVICE + Tensor<1, dim, Number> Point::operator-( + const Point &p) const { return (Tensor<1, dim, Number>(*this) -= p); } @@ -525,7 +529,7 @@ inline DEAL_II_HOST_DEVICE Tensor<1, dim, Number> Point::operator-( template DEAL_II_CXX20_REQUIRES(dim >= 0) -inline DEAL_II_HOST_DEVICE Point Point::operator-( +constexpr DEAL_II_HOST_DEVICE Point Point::operator-( const Tensor<1, dim, Number> &p) const { Point tmp = *this; @@ -537,7 +541,7 @@ inline DEAL_II_HOST_DEVICE Point Point::operator-( template DEAL_II_CXX20_REQUIRES(dim >= 0) -inline DEAL_II_HOST_DEVICE Point Point::operator-() +constexpr DEAL_II_HOST_DEVICE Point Point::operator-() const { Point result; @@ -551,7 +555,7 @@ inline DEAL_II_HOST_DEVICE Point Point::operator-() template DEAL_II_CXX20_REQUIRES(dim >= 0) template -inline DEAL_II_HOST_DEVICE Point< +constexpr DEAL_II_HOST_DEVICE Point< dim, typename ProductType::type>:: type> Point::operator*(const OtherNumber factor) const @@ -567,7 +571,7 @@ inline DEAL_II_HOST_DEVICE Point< template DEAL_II_CXX20_REQUIRES(dim >= 0) template -inline DEAL_II_HOST_DEVICE Point< +constexpr DEAL_II_HOST_DEVICE Point< dim, typename ProductType::type>:: type> Point::operator/(const OtherNumber factor) const @@ -584,7 +588,7 @@ inline DEAL_II_HOST_DEVICE Point< template DEAL_II_CXX20_REQUIRES(dim >= 0) -inline DEAL_II_HOST_DEVICE Number Point::operator*( +constexpr DEAL_II_HOST_DEVICE Number Point::operator*( const Tensor<1, dim, Number> &p) const { Number res = Number(); @@ -596,7 +600,7 @@ inline DEAL_II_HOST_DEVICE Number Point::operator*( template DEAL_II_CXX20_REQUIRES(dim >= 0) -inline DEAL_II_HOST_DEVICE typename numbers::NumberTraits::real_type +constexpr DEAL_II_HOST_DEVICE typename numbers::NumberTraits::real_type Point::square() const { return this->norm_square(); @@ -616,7 +620,7 @@ inline DEAL_II_HOST_DEVICE typename numbers::NumberTraits::real_type template DEAL_II_CXX20_REQUIRES(dim >= 0) -inline DEAL_II_HOST_DEVICE typename numbers::NumberTraits::real_type +constexpr DEAL_II_HOST_DEVICE typename numbers::NumberTraits::real_type Point::distance_square(const Point &p) const { Number sum = internal::NumberType::value(0.0); @@ -655,7 +659,7 @@ inline void Point::serialize(Archive &ar, const unsigned int) * @relates Point */ template -inline DEAL_II_HOST_DEVICE +constexpr DEAL_II_HOST_DEVICE Point::type>::type> diff --git a/tests/base/point_constexpr.cc b/tests/base/point_constexpr.cc new file mode 100644 index 0000000000..0182a9a175 --- /dev/null +++ b/tests/base/point_constexpr.cc @@ -0,0 +1,45 @@ +// --------------------------------------------------------------------- +// +// Copyright (C) 2010 - 2018 by the deal.II authors +// +// This file is part of the deal.II library. +// +// The deal.II library is free software; you can use it, redistribute +// it, and/or modify it under the terms of the GNU Lesser General +// Public License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// The full text of the license can be found in the file LICENSE.md at +// the top level directory of deal.II. +// +// --------------------------------------------------------------------- + + +// test for constexpr Point + +#include + +#include "../tests.h" + + +template +constexpr bool val = + Point((Point::unit_vector(0) + Point::unit_vector(0)) - + 2. * Point::unit_vector(0))(0) == 0.; + + +int +main() +{ + initlog(); + deallog << std::setprecision(3); + + static_assert(val<1>); + static_assert(val<2>); + static_assert(val<3>); + + static_assert((Point<1>() / 1.).norm_square() == 0.); + static_assert(Point<2>{0., 1.}.distance_square(Point<2>{}) == 1.); + static_assert((-Point<3>{0., 0., 1.}).square() == 1.); + + deallog << "Ok" << std::endl; +} diff --git a/tests/base/point_constexpr.output b/tests/base/point_constexpr.output new file mode 100644 index 0000000000..6ea1f897a5 --- /dev/null +++ b/tests/base/point_constexpr.output @@ -0,0 +1,2 @@ + +DEAL::Ok -- 2.39.5