From 93b5c3d4c168ab568ffb86c6c1476300d6838546 Mon Sep 17 00:00:00 2001 From: Jean-Paul Pelteret Date: Thu, 16 Nov 2017 08:33:00 +0100 Subject: [PATCH] Add Adol-C math header This imports the Adol-C math operations into standard namespace. It gives us the ability to use them (e.g. sqrt) within the Tensor class, and it also allows the user to write generic code and switch between AD number types. --- .../automatic_and_symbolic_differentiation.h | 2 + include/deal.II/differentiation/ad.h | 1 + .../deal.II/differentiation/ad/adolc_math.h | 142 ++++++++++++++++++ 3 files changed, 145 insertions(+) create mode 100644 include/deal.II/differentiation/ad/adolc_math.h diff --git a/doc/doxygen/headers/automatic_and_symbolic_differentiation.h b/doc/doxygen/headers/automatic_and_symbolic_differentiation.h index ab712e6122..9b6195edb8 100644 --- a/doc/doxygen/headers/automatic_and_symbolic_differentiation.h +++ b/doc/doxygen/headers/automatic_and_symbolic_differentiation.h @@ -67,6 +67,8 @@ * provide a uniform interface to the classes through the NumberTraits and ADNumberTraits * classes which are extensively used throughout of drivers. We also provide some mechanisms * to easily query select properties of these numbers, i.e. some type traits. + * - adolc_math.h: Extension of the Adol-C math operations that allow these numbers to be used + * consistently throughout the library. * - adolc_number_types.h: Implementation of the internal classes that define how we * use Adol-C numbers. * - adolc_product_types.h: Defines some product and scalar types that allow the use of diff --git a/include/deal.II/differentiation/ad.h b/include/deal.II/differentiation/ad.h index aa8a7b8df7..ccfbc517b6 100644 --- a/include/deal.II/differentiation/ad.h +++ b/include/deal.II/differentiation/ad.h @@ -23,6 +23,7 @@ #include #include +#include #include #include diff --git a/include/deal.II/differentiation/ad/adolc_math.h b/include/deal.II/differentiation/ad/adolc_math.h new file mode 100644 index 0000000000..82f3cc6f25 --- /dev/null +++ b/include/deal.II/differentiation/ad/adolc_math.h @@ -0,0 +1,142 @@ +// --------------------------------------------------------------------- +// +// Copyright (C) 2016 - 2017 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 at +// the top level of the deal.II distribution. +// +// --------------------------------------------------------------------- + +#ifndef dealii_differentiation_ad_adolc_math_h +#define dealii_differentiation_ad_adolc_math_h + +#include + +#ifdef DEAL_II_WITH_ADOLC + +DEAL_II_DISABLE_EXTRA_DIAGNOSTICS +#include +#include // Taped double math functions + +#include // Taped double +#include // Tapeless double +DEAL_II_ENABLE_EXTRA_DIAGNOSTICS + + +#ifndef DOXYGEN + +/** + * Import Adol-C math operations into standard namespace. This gives us the + * ability to use them within the Tensor class, and it also allows the user + * to write generic code and switch between AD number types. + * + * The math functions to be exposed come from the Adol-C's taped + * (anonymous) and tapeless namespaces. + */ +namespace std +{ + + /** + * Make a unary function with one name available under a different name + */ +#define DEAL_II_EXPOSE_ADOLC_UNARY_MATH_FUNCTION_COPY(func_to, func_from) \ + inline adouble func_to(const adouble &x) {return func_from(static_cast(x));} \ + inline adtl::adouble func_to(const adtl::adouble &x) {return adtl::func_from(x);} + + /** + * Expose a unary function + */ +#define DEAL_II_EXPOSE_ADOLC_UNARY_MATH_FUNCTION(func) \ + DEAL_II_EXPOSE_ADOLC_UNARY_MATH_FUNCTION_COPY(func,func) + + /** + * Make a binary function with one name available under a different name + */ +#define DEAL_II_EXPOSE_ADOLC_BINARY_MATH_FUNCTION_COPY(func_to,func_from) \ + inline adouble func_to(const adouble &x, const adouble &y) {return func_from(static_cast(x),static_cast(y));} \ + inline adouble func_to(const double &x, const adouble &y) {return func_from(x,static_cast(y));} \ + inline adouble func_to(const adouble &x, const double &y) {return func_from(static_cast(x),y);} \ + inline adtl::adouble func_to(const adtl::adouble &x, const adtl::adouble &y) {return adtl::func_from(x,y);} \ + inline adtl::adouble func_to(const double &x, const adtl::adouble &y) {return adtl::func_from(x,y);} \ + inline adtl::adouble func_to(const adtl::adouble &x, const double &y) {return adtl::func_from(x,y);} + + /** + * Expose a binary function + */ +#define DEAL_II_EXPOSE_ADOLC_BINARY_MATH_FUNCTION(func) \ + DEAL_II_EXPOSE_ADOLC_BINARY_MATH_FUNCTION_COPY(func,func) + + /** + * Expose a binary function + */ +#define DEAL_II_EXPOSE_ADOLC_BINARY_MATH_FUNCTION_2(func) \ + inline adouble func(const adouble &x, const adouble &y) {return func(static_cast(x),static_cast(y));} \ + inline adtl::adouble func(const adtl::adouble &x, const adtl::adouble &y) {return adtl::func(x,y);} + + // See + // https://gitlab.com/adol-c/adol-c/blob/master/ADOL-C/include/adolc/adouble.h + // https://gitlab.com/adol-c/adol-c/blob/master/ADOL-C/include/adolc/internal/paramfunc.h + + DEAL_II_EXPOSE_ADOLC_BINARY_MATH_FUNCTION(pow) + DEAL_II_EXPOSE_ADOLC_BINARY_MATH_FUNCTION(fmax) + DEAL_II_EXPOSE_ADOLC_BINARY_MATH_FUNCTION_COPY(max,fmax) + DEAL_II_EXPOSE_ADOLC_BINARY_MATH_FUNCTION(fmin) + DEAL_II_EXPOSE_ADOLC_BINARY_MATH_FUNCTION_COPY(min,fmin) + + DEAL_II_EXPOSE_ADOLC_UNARY_MATH_FUNCTION(exp) + DEAL_II_EXPOSE_ADOLC_UNARY_MATH_FUNCTION(log) + DEAL_II_EXPOSE_ADOLC_UNARY_MATH_FUNCTION(log10) + DEAL_II_EXPOSE_ADOLC_UNARY_MATH_FUNCTION(sqrt) +#if defined(DEAL_II_ADOLC_WITH_ATRIG_ERF) + DEAL_II_EXPOSE_ADOLC_UNARY_MATH_FUNCTION(erf) + inline adouble erfc(const adouble &x) + { + return 1.0 - std::erf(x); + } \ + inline adtl::adouble erfc(const adtl::adouble &x) + { + return 1.0 - std::erf(x); + } +#endif + + DEAL_II_EXPOSE_ADOLC_UNARY_MATH_FUNCTION(fabs) + DEAL_II_EXPOSE_ADOLC_UNARY_MATH_FUNCTION_COPY(abs,fabs) + DEAL_II_EXPOSE_ADOLC_UNARY_MATH_FUNCTION(ceil) + DEAL_II_EXPOSE_ADOLC_UNARY_MATH_FUNCTION(floor) + + DEAL_II_EXPOSE_ADOLC_UNARY_MATH_FUNCTION(sin) + DEAL_II_EXPOSE_ADOLC_UNARY_MATH_FUNCTION(cos) + DEAL_II_EXPOSE_ADOLC_UNARY_MATH_FUNCTION(tan) + DEAL_II_EXPOSE_ADOLC_UNARY_MATH_FUNCTION(asin) + DEAL_II_EXPOSE_ADOLC_UNARY_MATH_FUNCTION(acos) + DEAL_II_EXPOSE_ADOLC_UNARY_MATH_FUNCTION(atan) + DEAL_II_EXPOSE_ADOLC_BINARY_MATH_FUNCTION_2(atan2) + + DEAL_II_EXPOSE_ADOLC_UNARY_MATH_FUNCTION(sinh) + DEAL_II_EXPOSE_ADOLC_UNARY_MATH_FUNCTION(cosh) + DEAL_II_EXPOSE_ADOLC_UNARY_MATH_FUNCTION(tanh) +#if defined(DEAL_II_ADOLC_WITH_ATRIG_ERF) + DEAL_II_EXPOSE_ADOLC_UNARY_MATH_FUNCTION(asinh) + DEAL_II_EXPOSE_ADOLC_UNARY_MATH_FUNCTION(acosh) + DEAL_II_EXPOSE_ADOLC_UNARY_MATH_FUNCTION(atanh) +#endif + +#undef DEAL_II_EXPOSE_ADOLC_BINARY_MATH_FUNCTION_2 +#undef DEAL_II_EXPOSE_ADOLC_BINARY_MATH_FUNCTION +#undef DEAL_II_EXPOSE_ADOLC_BINARY_MATH_FUNCTION_COPY +#undef DEAL_II_EXPOSE_ADOLC_UNARY_MATH_FUNCTION +#undef DEAL_II_EXPOSE_ADOLC_UNARY_MATH_FUNCTION_COPY + +} // namespace std + +#endif // DOXYGEN + +#endif // DEAL_II_WITH_ADOLC + +#endif -- 2.39.5