From: alberto sartori Date: Thu, 17 Sep 2015 16:32:31 +0000 (+0200) Subject: added sacado_product_type.h X-Git-Tag: v8.4.0-rc2~388^2 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F1631%2Fhead;p=dealii.git added sacado_product_type.h --- 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