From 5769e654f653703ae051186c501fbe7afd949a12 Mon Sep 17 00:00:00 2001 From: alberto sartori Date: Thu, 17 Sep 2015 18:32:31 +0200 Subject: [PATCH] added sacado_product_type.h --- include/deal.II/base/sacado_product_type.h | 131 ++++++++++++++++++ tests/base/sacado_product_type_01.cc | 98 +++++++++++++ ...acado_product_type_01.with_trilinos.output | 2 + tests/base/sacado_product_type_02.cc | 99 +++++++++++++ ...acado_product_type_02.with_trilinos.output | 2 + tests/base/sacado_product_type_03.cc | 69 +++++++++ ...acado_product_type_03.with_trilinos.output | 2 + 7 files changed, 403 insertions(+) create mode 100644 include/deal.II/base/sacado_product_type.h create mode 100644 tests/base/sacado_product_type_01.cc create mode 100644 tests/base/sacado_product_type_01.with_trilinos.output create mode 100644 tests/base/sacado_product_type_02.cc create mode 100644 tests/base/sacado_product_type_02.with_trilinos.output create mode 100644 tests/base/sacado_product_type_03.cc create mode 100644 tests/base/sacado_product_type_03.with_trilinos.output diff --git a/include/deal.II/base/sacado_product_type.h b/include/deal.II/base/sacado_product_type.h new file mode 100644 index 0000000000..e5c3de993d --- /dev/null +++ b/include/deal.II/base/sacado_product_type.h @@ -0,0 +1,131 @@ +// --------------------------------------------------------------------- +// +// Copyright (C) 2015 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__sacado_product_type_h +#define dealii__sacado_product_type_h + +#include +#include + +#ifdef DEAL_II_WITH_TRILINOS +#include "Sacado.hpp" + +DEAL_II_NAMESPACE_OPEN + +template +struct ProductType, float> +{ + typedef Sacado::Fad::DFad type; +}; + +template +struct ProductType > +{ + typedef Sacado::Fad::DFad type; +}; + +template +struct ProductType, double> +{ + typedef Sacado::Fad::DFad type; +}; + +template +struct ProductType > +{ + typedef Sacado::Fad::DFad type; +}; + +template +struct ProductType, int> +{ + typedef Sacado::Fad::DFad type; +}; + +template +struct ProductType > +{ + typedef Sacado::Fad::DFad type; +}; + +template +struct ProductType, Sacado::Fad::DFad > +{ + typedef Sacado::Fad::DFad::type > type; +}; + +template +struct EnableIfScalar > +{ + typedef Sacado::Fad::DFad type; +}; + + +/** + * Provide an operator* for a scalar multiplication of a + * sacado type with a different non-sacado type. + * + * @relates EnableIfScalar + * @relates ProductType + */ + +template +typename ProductType, typename EnableIfScalar::type>::type +inline +operator*(const Sacado::Fad::DFad &left, const U &right) +{ + typedef typename ProductType, U>::type result_type; + return static_cast(left) * static_cast(right); +} + + +/** + * Provide an operator* for a scalar multiplication of non-sacado type with a sacado type. + * + * @relates EnableIfScalar + * @relates ProductType + */ + +template +typename ProductType::type, Sacado::Fad::DFad >::type +inline +operator*(const T &left, const Sacado::Fad::DFad &right) +{ + typedef typename ProductType >::type result_type; + return static_cast(left) * static_cast(right); +} + +/** + * Provide an operator* for a scalar multiplication of mixed + * sacado types. + * + * @relates EnableIfScalar + * @relates ProductType + */ + +template +typename ProductType, Sacado::Fad::DFad >::type +inline +operator*(const Sacado::Fad::DFad &left, const Sacado::Fad::DFad &right) +{ + typedef typename ProductType, Sacado::Fad::DFad >::type result_type; + return static_cast(left) * static_cast(right); +} + +#endif // DEAL_II_WITH_TRILINOS + +DEAL_II_NAMESPACE_CLOSE + +#endif diff --git a/tests/base/sacado_product_type_01.cc b/tests/base/sacado_product_type_01.cc new file mode 100644 index 0000000000..3db0e57184 --- /dev/null +++ b/tests/base/sacado_product_type_01.cc @@ -0,0 +1,98 @@ +// --------------------------------------------------------------------- +// +// Copyright (C) 2015 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. +// +// --------------------------------------------------------------------- + + +// test ProductType with sacado + +#include +#include +#include +#include +#include + +#include +#include + +#include "../tests.h" + +template +void check() +{ + AssertThrow (typeid(typename ProductType::type) == typeid(CompareType), + ExcInternalError()); + AssertThrow (typeid(typename ProductType::type) == typeid(T() * U()), + ExcInternalError()); +} + + +int main() +{ + typedef Sacado::Fad::DFad Sdouble; + std::ofstream logfile("output"); + deallog.attach(logfile); + deallog.depth_console(0); + deallog.threshold_double(1.e-10); + + + // check product of scalars + check(); + check(); + + check(); + check(); + + check(); + check(); + + // check product with Tensor<1,dim> + check,Sdouble,Tensor<1,2,Sdouble> >(); + check,double,Tensor<1,2,Sdouble> >(); + + check,Tensor<1,2,Sdouble> >(); + check,Tensor<1,2,Sdouble> >(); + + check,Sdouble,Tensor<1,2,Sdouble> >(); + check,float,Tensor<1,2,Sdouble> >(); + + check,Tensor<1,2,Sdouble> >(); + check,Tensor<1,2,Sdouble> >(); + + check,Sdouble,Tensor<1,2,Sdouble> >(); + check,int,Tensor<1,2,Sdouble> >(); + + check,Tensor<1,2,Sdouble> >(); + check,Tensor<1,2,Sdouble> >(); + + // check product with Tensor<2,dim> (which is a different class) + check,Sdouble,Tensor<2,2,Sdouble> >(); + check,double,Tensor<2,2,Sdouble> >(); + + check,Tensor<2,2,Sdouble> >(); + check,Tensor<2,2,Sdouble> >(); + + check,Sdouble,Tensor<2,2,Sdouble> >(); + check,float,Tensor<2,2,Sdouble> >(); + + check,Tensor<2,2,Sdouble> >(); + check,Tensor<2,2,Sdouble> >(); + + check,Sdouble,Tensor<2,2,Sdouble> >(); + check,int,Tensor<2,2,Sdouble> >(); + + check,Tensor<2,2,Sdouble> >(); + check,Tensor<2,2,Sdouble> >(); + + deallog << "OK" << std::endl; +} diff --git a/tests/base/sacado_product_type_01.with_trilinos.output b/tests/base/sacado_product_type_01.with_trilinos.output new file mode 100644 index 0000000000..0fd8fc12f0 --- /dev/null +++ b/tests/base/sacado_product_type_01.with_trilinos.output @@ -0,0 +1,2 @@ + +DEAL::OK diff --git a/tests/base/sacado_product_type_02.cc b/tests/base/sacado_product_type_02.cc new file mode 100644 index 0000000000..573e60876c --- /dev/null +++ b/tests/base/sacado_product_type_02.cc @@ -0,0 +1,99 @@ +// --------------------------------------------------------------------- +// +// Copyright (C) 2015 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. +// +// --------------------------------------------------------------------- + + +// test ProductType with sacado + +#include +#include +#include +#include +#include + +#include +#include + +#include "../tests.h" + +template +void check() +{ + AssertThrow (typeid(typename ProductType::type) == typeid(CompareType), + ExcInternalError()); + AssertThrow (typeid(typename ProductType::type) == typeid(T() * U()), + ExcInternalError()); +} + + +int main() +{ + typedef Sacado::Fad::DFad Sdouble; + typedef Sacado::Fad::DFad SSdouble; + std::ofstream logfile("output"); + deallog.attach(logfile); + deallog.depth_console(0); + deallog.threshold_double(1.e-10); + + + // check product of scalars + check(); + check(); + + check(); + check(); + + check(); + check(); + + // check product with Tensor<1,dim> + check,SSdouble,Tensor<1,2,SSdouble> >(); + check,double,Tensor<1,2,SSdouble> >(); + + check,Tensor<1,2,SSdouble> >(); + check,Tensor<1,2,SSdouble> >(); + + check,SSdouble,Tensor<1,2,SSdouble> >(); + check,float,Tensor<1,2,SSdouble> >(); + + check,Tensor<1,2,SSdouble> >(); + check,Tensor<1,2,SSdouble> >(); + + check,SSdouble,Tensor<1,2,SSdouble> >(); + check,int,Tensor<1,2,SSdouble> >(); + + check,Tensor<1,2,SSdouble> >(); + check,Tensor<1,2,SSdouble> >(); + + // check product with Tensor<2,dim> (which is a different class) + check,SSdouble,Tensor<2,2,SSdouble> >(); + check,double,Tensor<2,2,SSdouble> >(); + + check,Tensor<2,2,SSdouble> >(); + check,Tensor<2,2,SSdouble> >(); + + check,SSdouble,Tensor<2,2,SSdouble> >(); + check,float,Tensor<2,2,SSdouble> >(); + + check,Tensor<2,2,SSdouble> >(); + check,Tensor<2,2,SSdouble> >(); + + check,SSdouble,Tensor<2,2,SSdouble> >(); + check,int,Tensor<2,2,SSdouble> >(); + + check,Tensor<2,2,SSdouble> >(); + check,Tensor<2,2,SSdouble> >(); + + deallog << "OK" << std::endl; +} diff --git a/tests/base/sacado_product_type_02.with_trilinos.output b/tests/base/sacado_product_type_02.with_trilinos.output new file mode 100644 index 0000000000..0fd8fc12f0 --- /dev/null +++ b/tests/base/sacado_product_type_02.with_trilinos.output @@ -0,0 +1,2 @@ + +DEAL::OK diff --git a/tests/base/sacado_product_type_03.cc b/tests/base/sacado_product_type_03.cc new file mode 100644 index 0000000000..680be7bde4 --- /dev/null +++ b/tests/base/sacado_product_type_03.cc @@ -0,0 +1,69 @@ +// --------------------------------------------------------------------- +// +// Copyright (C) 2015 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. +// +// --------------------------------------------------------------------- + + +// test ProductType with sacado + +#include +#include +#include +#include +#include + +#include +#include + +#include "../tests.h" + +template +void check() +{ + AssertThrow (typeid(typename ProductType::type) == typeid(CompareType), + ExcInternalError()); + AssertThrow (typeid(typename ProductType::type) == typeid(T() * U()), + ExcInternalError()); +} + + +int main() +{ + typedef Sacado::Fad::DFad Sdouble; + typedef Sacado::Fad::DFad SSdouble; + std::ofstream logfile("output"); + deallog.attach(logfile); + deallog.depth_console(0); + deallog.threshold_double(1.e-10); + + + // check product of scalars + check(); + check(); + + // check product with Tensor<1,dim> + check,SSdouble,Tensor<1,2,SSdouble> >(); + check,Sdouble,Tensor<1,2,SSdouble> >(); + + check,Tensor<1,2,SSdouble> >(); + check,Tensor<1,2,SSdouble> >(); + + // check product with Tensor<2,dim> (which is a different class) + check,SSdouble,Tensor<2,2,SSdouble> >(); + check,Sdouble,Tensor<2,2,SSdouble> >(); + + check,Tensor<2,2,SSdouble> >(); + check,Tensor<2,2,SSdouble> >(); + + deallog << "OK" << std::endl; +} diff --git a/tests/base/sacado_product_type_03.with_trilinos.output b/tests/base/sacado_product_type_03.with_trilinos.output new file mode 100644 index 0000000000..0fd8fc12f0 --- /dev/null +++ b/tests/base/sacado_product_type_03.with_trilinos.output @@ -0,0 +1,2 @@ + +DEAL::OK -- 2.39.5