From 6752a1cae3748d0347edf24e790b53680072910d Mon Sep 17 00:00:00 2001 From: bangerth Date: Tue, 23 Apr 2013 11:36:38 +0000 Subject: [PATCH] Patch by Fahad Alrashed: Save and restore print state in PETScWrappers::VectorBase::print. git-svn-id: https://svn.dealii.org/trunk@29372 0785d39b-7218-0410-832d-ea1e28bc413d --- deal.II/doc/news/changes.h | 8 ++ deal.II/source/lac/petsc_vector_base.cc | 9 +++ tests/petsc/vector_print.cc | 98 +++++++++++++++++++++++++ tests/petsc/vector_print/cmp/generic | 12 +++ 4 files changed, 127 insertions(+) create mode 100644 tests/petsc/vector_print.cc create mode 100644 tests/petsc/vector_print/cmp/generic diff --git a/deal.II/doc/news/changes.h b/deal.II/doc/news/changes.h index 044582e2b9..399aac68cd 100644 --- a/deal.II/doc/news/changes.h +++ b/deal.II/doc/news/changes.h @@ -111,6 +111,14 @@ this function.
    +
  1. Fixed: PETScWrappers::VectorBase::print now saves and restores +the precision +and width associated with the stream it prints to around setting +the values passed as arguments. +
    +(Fahad Alrashed, 2013/04/22) +
  2. +
  3. Fixed: FullMatrix::print now saves and restores the precision and width associated with the stream it prints to around setting the values passed as arguments. diff --git a/deal.II/source/lac/petsc_vector_base.cc b/deal.II/source/lac/petsc_vector_base.cc index 2deb0cc26f..0f8deef8af 100644 --- a/deal.II/source/lac/petsc_vector_base.cc +++ b/deal.II/source/lac/petsc_vector_base.cc @@ -1022,6 +1022,11 @@ namespace PETScWrappers int ierr = VecGetArray (vector, &val); AssertThrow (ierr == 0, ExcPETScError(ierr)); + + // save the state of out stream + const std::ios::fmtflags old_flags = out.flags(); + const unsigned int old_precision = out.precision (precision); + out.precision (precision); if (scientific) out.setf (std::ios::scientific, std::ios::floatfield); @@ -1036,6 +1041,10 @@ namespace PETScWrappers out << val[i] << std::endl; out << std::endl; + // reset output format + out.flags (old_flags); + out.precision(old_precision); + // restore the representation of the // vector ierr = VecRestoreArray (vector, &val); diff --git a/tests/petsc/vector_print.cc b/tests/petsc/vector_print.cc new file mode 100644 index 0000000000..50860c8f47 --- /dev/null +++ b/tests/petsc/vector_print.cc @@ -0,0 +1,98 @@ +//---------------------------- petsc_vector_print.cc --------------------------- +// $Id$ +// Version: $Name$ +// +// Copyright (C) 2004, 2005, 2013 by the deal.II authors +// +// This file is subject to QPL and may not be distributed +// without copyright and license information. Please refer +// to the file deal.II/doc/license.html for the text and +// further information on this license. +// +//---------------------------- petsc_vector_print.cc --------------------------- + + +// verify that VectorBase::print uses the precision parameter correctly and +// restores the previous value of the stream precision + +#include "../tests.h" +#include +#include +#include +#include + + +void test (PETScWrappers::Vector &v, + PETScWrappers::Vector &w) +{ + // set the first vector + for (unsigned int i=0; i