Templating dealii::Function with ADtypes

59 views
Skip to first unread message

Pranshul Thakur

unread,
Dec 22, 2022, 12:27:19 PM12/22/22
to deal.II User Group
Hi,

I am trying to template dealii's Function (https://www.dealii.org/current/doxygen/deal.II/classFunction.html) with an ADType i.e. I am planning to use
template class dealii::Function<dim, Sacado::Fad::DFad<double>>;


Just wondering if there is a way to use AD with Function by using another header file.

Best,
Pranshul.


Wolfgang Bangerth

unread,
Dec 22, 2022, 8:10:22 PM12/22/22
to dea...@googlegroups.com

Pranshul:

> I am trying to template dealii's Function
> (https://www.dealii.org/current/doxygen/deal.II/classFunction.html) with an
> ADType i.e. I am planning to use
> template class dealii::Function<dim, Sacado::Fad::DFad<double>>;
>
> However, this line encounters an error as this line in function.templates.h
> (https://github.com/dealii/dealii/blob/85f093567b355e1f8173598f2a479593a0506630/include/deal.II/base/function.templates.h#L75-L76) triggers a static_assert  by dealii::Vector (in this line: https://github.com/dealii/dealii/blob/85f093567b355e1f8173598f2a479593a0506630/include/deal.II/lac/vector.h#L118-L122).
Can you show what the error you get is? Are you actually using the
Function::vector_value() function that produces the error?

Best
W.

--
------------------------------------------------------------------------
Wolfgang Bangerth email: bang...@colostate.edu
www: http://www.math.colostate.edu/~bangerth/


Pranshul Thakur

unread,
Dec 23, 2022, 2:06:13 PM12/23/22
to deal.II User Group
Hi Prof. Bangerth,

Just a simple declaration of Function (i.e. without using Function::vector_value()) gives the error. I have attached the error along with a simplified sample code which produces it. As seen from the code, double works fine but an ADType triggers static_assert while compiling.

Thank you,
Pranshul.
sample_code.cpp
Error_message.png

Wolfgang Bangerth

unread,
Dec 24, 2022, 1:16:27 PM12/24/22
to dea...@googlegroups.com
On 12/23/22 12:06, Pranshul Thakur wrote:
>
> Just a simple declaration of Function (i.e. without using
> Function::vector_value()) gives the error. I have attached the error along
> with a simplified sample code which produces it. As seen from the code, double
> works fine but an ADType triggers static_assert while compiling.

Pranshul,
I have played with this a bit but can't make it work even if I allowed myself
to change the Function base class to be incompatible. C++ simply wants that
all functions declared as virtual are always available, and one can't remove
certain virtual functions from the interface depending on a type argument.

The question, though, is what you want to do with the Function class you are
declaring. Function<dim> is only useful if you can pass an object of this type
to a function that knows what to do with such functions. This works in deal.II
for scalar types that are real numbers or complex numbers, but not for AD
types. So even if you could declare an object of type Function<dim,AD>, there
is nothing you can do with such an object.

Pranshul Thakur

unread,
Dec 25, 2022, 3:52:29 PM12/25/22
to deal.II User Group
Hi Prof. Bangerth,

Thanks for trying the code. Yes, just checked that we aren't really using a specific deal.II function with AD and wouldn't need to derive our class from Function<dim>. Function<dim, AD> compiled fine with deal.II version 9.2, but it doesn't after this PR improved the check (which is a good check if functions taking in Function<dim> expect real/complex types).  

Have a great day.
Best,
Pranshul.

Wolfgang Bangerth

unread,
Dec 28, 2022, 6:22:59 PM12/28/22
to dea...@googlegroups.com
On 12/25/22 13:52, Pranshul Thakur wrote:
>
> Thanks for trying the code. Yes, just checked that we aren't really using a
> specific deal.II function with AD and wouldn't need to derive our class from
> Function<dim>. Function<dim, AD> compiled fine with deal.II version 9.2, but
> it doesn't after this PR
> <https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fdealii%2Fdealii%2Fpull%2F14211&data=05%7C01%7CWolfgang.Bangerth%40colostate.edu%7Ce92ce00ce73b43e61ba308dae6b9f1a0%7Cafb58802ff7a4bb1ab21367ff2ecfc8b%7C0%7C0%7C638075984677313930%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=9dQ24fgI17PdU3YTUoVMnKP7m1XmyoJa%2FyB8oJckDzQ%3D&reserved=0> improved the check (which is a good check if functions taking in Function<dim> expect real/complex types).

I have to admit that I find it mildly annoying that the compiler forces the
entire class to use arithmetic types for RangeNumberType (as opposed to only
requiring them for those functions that actually use the Vector class). At the
same time, in your case the solution simply is to not derive your class from
Function<dim,...> since there is nothing you could do with a reference- or
pointer-to-base.

Pranshul Thakur

unread,
Dec 29, 2022, 2:22:36 PM12/29/22
to deal.II User Group
Yes, not deriving from Function works. Thanks!
-Pranshul.

Reply all
Reply to author
Forward
0 new messages