Trying to increase precision of floating point values in step3 of tutorial

74 views
Skip to first unread message

Wasim Niyaz Munshi ce21d400

unread,
Sep 29, 2022, 4:24:49 AM9/29/22
to dea...@googlegroups.com
Hello everyone.
Is there a way to increase precision of floating point values in deal.ii? I am modifying step3 and checking my solution against a linear exact solution. I am getting machine precision (around 10^-16 ) for 4,16...4096 elements. However, when I refine further, I am getting an error of 10^-6 and 10^-5 for 16384 and 65536 elements. What I realized is that deal.ii outputs the solution vector values upto 7 decimal places only and hence the error begins to show up for very fine meshes.
Is there a way to increase my precision to say upto 14 decimal places?
I tried using long double instead of double for my various variables but it gives the following error:
error: no matching function for call to ‘interpolate_boundary_values(dealii::DoFHandler<2, 2>&, int, dealii::Functions::ConstantFunction<2>, std::map<unsigned int, long double>&)’
  296 |                                            boundary_values);

Thank You!

Bruno Turcksin

unread,
Sep 29, 2022, 8:43:28 AM9/29/22
to deal.II User Group
Hello,

What do you mean exactly by "deal.ii outputs the solution vector values upto 7 decimal"? Which function are you using? If you are using a function like std::cout to inspect the values in the vector, the default precision is 6 (I think) but it can be changed using std::setprecision (see https://en.cppreference.com/w/cpp/io/manip/setprecision)

Best,

Bruno

Wasim Niyaz Munshi ce21d400

unread,
Oct 10, 2022, 9:48:11 AM10/10/22
to deal.II User Group
Thank you.

I used the setprecision function to output the result to desired accuracy.
However, I noticed that deal.ii writes the output to vtk file only upto certain precision(6).
Is there a way to write output to the vtk file according to required precision?

Thank you

Wolfgang Bangerth

unread,
Oct 10, 2022, 9:52:44 AM10/10/22
to dea...@googlegroups.com
On 10/10/22 07:48, Wasim Niyaz Munshi ce21d400 wrote:
>
> I used the setprecision function to output the result to desired accuracy.

Can you show how you actually do that? I don't think that deal.II overrides
what you do, though I may be wrong.


> However, I noticed that deal.ii writes the output to vtk file only upto
> certain precision(6).
> Is there a way to write output to the vtk file according to required precision?

The easiest way may be to just use the VTU file format instead of the VTK
format. It is binary and so outputs as many digits as there are in a 32-bit
floating point number.

Best
W.


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

Peter Munch

unread,
Oct 10, 2022, 9:58:53 AM10/10/22
to deal.II User Group
> It is binary and so outputs as many digits as there are in a 32-bit floating point number.

@WB Which is equivalent to "deal.ii outputs the solution vector values upto 7 decimal" if I am not mistaken.

To be able to switch between double and single precision would be nice. It's not the first time I heard someone wanting this feature. If you would be willing to implement this would be great.

But for the time being, you could also quickly change some floats (e.g. https://github.com/dealii/dealii/blob/a95f7e5f2e7d15c10670ee93313dd357178a18b3/source/base/data_out_base.cc#L1547-L1548) to doubles.

Peter

Wasim Niyaz Munshi ce21d400

unread,
Oct 10, 2022, 1:45:46 PM10/10/22
to dea...@googlegroups.com
What I meant was that when I was using cout to print my solution vector, it was printed to 6 or 7 decimal only.
I had a feeling that deal.ii was evaluating the results accurately to machine precision but it was truncating it to 6 or 7 decimals, when writing it to the vtk file or outputting it on the terminal.
So I used set precision function when using the cout to print the solution vector.
With setprecision, the values are printed accurately to machine precision.
However, I was still unable to write the values accurately upto machine precision, to a vtk file.
I will have a look at vtu format.
Hope it will work.

Thanks 

Wasim Niyaz 

--
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/8Ud1gkzUbNQ/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/9cddced1-732c-a340-2b8e-ea40b4747363%40colostate.edu.

Wolfgang Bangerth

unread,
Oct 10, 2022, 3:57:54 PM10/10/22
to dea...@googlegroups.com
On 10/10/22 11:45, Wasim Niyaz Munshi ce21d400 wrote:
> So I used set precision function when using the cout to print the
> solution vector.
> With setprecision, the values are printed accurately to machine precision.
> However, I was still unable to write the values accurately upto machine
> precision, to a vtk file.

Did you use .setprecision(...) also on the stream that you give to
data_out.write_vtk()? You need to set the precision separately for each
stream on which you want to output.

Wasim Niyaz Munshi ce21d400

unread,
Oct 11, 2022, 12:47:58 AM10/11/22
to deal.II User Group
I used this to set precision of 12 for my solution vector
std::cout << "Solution" << std::setprecision(12) <<solution;

I had to add the following line of code also:
#include <iomanip>

I tried the following to write data to vtk file with a higher precision(12):
DataOut<2> data_out;
  data_out.attach_dof_handler(dof_handler);
  data_out.add_data_vector(std::setprecision(12) <<solution, "solution");
  data_out.build_patches();

  std::ofstream output("solution.vtk");
  data_out.write_vtk(output);

But it gives the following error:
error: no match for ‘operator<<’ (operand types are ‘std::_Setprecision’ and ‘const dealii::Vector<double>’)

Thanks

Bruno Turcksin

unread,
Oct 11, 2022, 8:30:55 AM10/11/22
to dea...@googlegroups.com
You want to do something like this:

  DataOut<2> data_out;
  data_out.attach_dof_handler(dof_handler);
  data_out.add_data_vector(solution, "solution");

  data_out.build_patches();

  std::ofstream output("solution.vtk");
  output.set_precisition(12);
  data_out.write_vtk(output);

Best,

Bruno

--
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/8Ud1gkzUbNQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to dealii+un...@googlegroups.com.

Wasim Niyaz Munshi ce21d400

unread,
Oct 11, 2022, 9:03:35 AM10/11/22
to deal.II User Group
Did you mean
output.set_precision(12);

It gives the following error
error: ‘std::ofstream’ {aka ‘class std::basic_ofstream<char>’} has no member named ‘set_precision’; did you mean ‘std::streamsize std::ios_base::_M_precision’? (not accessible from this context)

I also tried
output.setprecision(12);

It also gave the same error
error: ‘std::ofstream’ {aka ‘class std::basic_ofstream<char>’} has no member named ‘setprecision’; did you mean ‘std::streamsize std::ios_base::_M_precision’? (not accessible from this context)

Thanks

Bruno Turcksin

unread,
Oct 11, 2022, 9:32:05 AM10/11/22
to dea...@googlegroups.com
Sorry, it should be output.precision(12)

Bruno


Wasim Niyaz Munshi ce21d400

unread,
Oct 15, 2022, 5:51:38 AM10/15/22
to deal.II User Group
Thanks.
It is working

Wasim
Reply all
Reply to author
Forward
0 new messages