interpolate_boundary_values and multiple scalar components

50 views
Skip to first unread message

Chad

unread,
Oct 14, 2023, 11:01:58 PM10/14/23
to deal.II User Group
So I think I finally figured out the issue but I still wanted to bring this up to help others and b/c the documentation seems a bit unintuitive to me. I took step-20 and modified it to have:
  • 2 scalar diffusion equations with FE_Q for both components
  • Dirichlet BC on the left wall (boundary ID 1) with Functions::ConstantFunction()
  • Neumann BC on the right wall (boundary ID 2)
When trying to implement the Dirichlet BCs I used:

dealii::FEValuesExtractors::Scalar const var1(0);
dealii::FEValuesExtractors::Scalar const var2(1);
dealii::ComponentMask const var1Mask = fe.component_mask(var1);
dealii::ComponentMask const var2Mask = fe.component_mask(var2);

dealii::VectorTools::interpolate_boundary_values(dof_handler, 1, dealii::Functions::ConstantFunction<dim>(1.0), boundary_values, var1Mask);
dealii::VectorTools::interpolate_boundary_values(dof_handler, 1, dealii::Functions::ConstantFunction<dim>(2.0), boundary_values, var2Mask);

Var1 displays the correct BC in the vtu file but Var2 displays results as if a BC of 0.0 was used instead of 2.0. The fix seems to be that you need to actually use:

dealii::VectorTools::interpolate_boundary_values(dof_handler, 1, dealii::Functions::ConstantFunction<dim>(2.0, X), boundary_values, var2Mask);

where X >= (varIndex+1); i.e. 1 & 2 for var1 & var2 respectively. Looking at the documentation for ConstantFunction I'd assume that the default value of 1 along with the ComponentMask would be sufficient to apply the BC since I only need a ConstantFunction of size 1 and the ComponentMask would then determine which component(s) to apply this value to. However, it appears you need a ConstantFunction with a size of ComponentMask+1 so the mask can access the ConstantFunction component(s) matching the ComponentMask(s).

I also attached my modified step-20 for others to see where I was playing around with interpolate_boundary_values() in make_grid_and_dofs().

Hope this helps,
Chad
step-20b.tar.gz

Wolfgang Bangerth

unread,
Oct 15, 2023, 10:28:21 PM10/15/23
to dea...@googlegroups.com
On 10/14/23 21:01, Chad wrote:
>
> dealii::VectorTools::interpolate_boundary_values(dof_handler, 1,
> dealii::Functions::ConstantFunction<dim>(1.0), boundary_values, var1Mask);
> dealii::VectorTools::interpolate_boundary_values(dof_handler, 1,
> dealii::Functions::ConstantFunction<dim>(2.0), boundary_values, var2Mask);
>
> Var1 displays the correct BC in the vtu file but Var2 displays results as if a
> BC of 0.0 was used instead of 2.0.

I am surprised this actually works. The boundary value function needs to have
the same number of vector components as the finite element in use. Are you
running your program in debug mode? You should have gotten an error message
with the code above.

> The fix seems to be that you need to
> actually use:
>
> dealii::VectorTools::interpolate_boundary_values(dof_handler, 1,
> dealii::Functions::ConstantFunction<dim>(2.0, *X*), boundary_values, var2Mask);
>
> where X >= (varIndex+1); i.e. 1 & 2 for var1 & var2 respectively. Looking at
> the documentation for ConstantFunction
> <https://www.dealii.org/current/doxygen/deal.II/classFunctions_1_1ConstantFunction.html#a97c9eeab728d22be4b3dddb351d8e754> I'd assume that the default value of 1 along with the ComponentMask would be sufficient to apply the BC since I only need a ConstantFunction of size 1 and the ComponentMask would then determine which component(s) to apply this value to. However, it appears you need a ConstantFunction with a size of ComponentMask+1 so the mask can access the ConstantFunction component(s) matching the ComponentMask(s).

The general rule is that you need to pass function objects that have the same
number of components as the finite element in question -- regardless of
whether or not you are only evaluating some of the components. That's because
oftentimes you create function objects that will be used in a variety of
contexts (for boundary values of different kinds, computing the error,
evaluating error estimators, etc.) where sometimes you might apply a mask and
othertimes you don't.

Best
W.

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


Chad Balen

unread,
Oct 16, 2023, 12:35:18 AM10/16/23
to dea...@googlegroups.com
Hi Wolfgang,

> Are you running your program in debug mode? You should have gotten an error message with the code above.

I wasn't b/c it typically runs slower than release mode... Ok I just finished installing a debug version and it gives an error message. Wow, that would've really helped me solve this problem sooner.

Thanks for the help,
Chad


--
The deal.II project is located at http://www.dealii.org/
For mailing list/forum options, see https://groups.google.com/d/forum/dealii?hl=en
---
You received this message because you are subscribed to a topic in the Google Groups "deal.II User Group" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/dealii/XaO4TZYrhCQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to dealii+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/dealii/9f8d708a-053d-5ee3-eee1-d17380509a79%40colostate.edu.

Wolfgang Bangerth

unread,
Oct 16, 2023, 10:19:21 AM10/16/23
to dea...@googlegroups.com
On 10/15/23 22:34, Chad Balen wrote:
>
> I wasn't b/c it typically runs slower than release mode... Ok I just finished
> installing a debug version and it gives an error message. Wow, that would've
> really helped me solve this problem sooner.

There are 14795 checks for this kind of mistake in deal.II. You really deprive
yourself of a valuable source of debugging help if you don't run a program in
debug mode until you know for a fact that it's working correctly.
Reply all
Reply to author
Forward
0 new messages