]> https://gitweb.dealii.org/ - dealii-svn.git/commitdiff
Patch by Fahad Alrashed: Save and restore print state in PETScWrappers::VectorBase...
authorbangerth <bangerth@0785d39b-7218-0410-832d-ea1e28bc413d>
Tue, 23 Apr 2013 11:36:38 +0000 (11:36 +0000)
committerbangerth <bangerth@0785d39b-7218-0410-832d-ea1e28bc413d>
Tue, 23 Apr 2013 11:36:38 +0000 (11:36 +0000)
git-svn-id: https://svn.dealii.org/trunk@29372 0785d39b-7218-0410-832d-ea1e28bc413d

deal.II/doc/news/changes.h
deal.II/source/lac/petsc_vector_base.cc
tests/petsc/vector_print.cc [new file with mode: 0644]
tests/petsc/vector_print/cmp/generic [new file with mode: 0644]

index 044582e2b95314d6fae760c10e441725344a8812..399aac68cd0b96eab536b18eec98152497020b5f 100644 (file)
@@ -111,6 +111,14 @@ this function.
 
 <ol>
 
+<li> 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.
+<br>
+(Fahad Alrashed, 2013/04/22)
+</li>
+
 <li> 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.
index 2deb0cc26f32cd8874f5e78cb808852dbb298a18..0f8deef8afced3690cd46e7d63e4545815aec742 100644 (file)
@@ -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 (file)
index 0000000..50860c8
--- /dev/null
@@ -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 <deal.II/lac/petsc_vector.h>    
+#include <fstream>
+#include <iostream>
+#include <vector>
+
+
+void test (PETScWrappers::Vector &v,
+           PETScWrappers::Vector &w)
+{
+                                   // set the first vector
+  for (unsigned int i=0; i<v.size(); ++i)
+    v(i) = i;
+
+                                   // copy elements by reference
+  for (unsigned int i=0; i<v.size(); ++i)
+    w(i) = v(i);
+
+                                   // check that they're equal
+  Assert (v==w, ExcInternalError());
+
+  deallog << "OK" << std::endl;
+}
+
+
+
+int main (int argc, char **argv)
+{
+  std::ofstream logfile("vector_print/output");
+  deallog.attach(logfile);
+  deallog.depth_console(0);
+  deallog.threshold_double(1.e-10);
+
+  try
+    {
+      Utilities::MPI::MPI_InitFinalize mpi_initialization(argc, argv, 1);
+      {
+        PETScWrappers::Vector v (5);
+       for (unsigned int i=0; i<v.size(); ++i)
+         v(i) = i*1.2345678901234567;
+
+       // print with old precision
+       deallog << numbers::PI << std::endl;
+
+       // print with prescribed precision
+       deallog << "across=false" << std::endl;
+       v.print (logfile, 10, true, false);
+       
+       deallog << "across=true" << std::endl;
+       v.print (logfile, 10, true, true);
+
+       // print once more. should be the old precision again
+       deallog << numbers::PI << std::endl;
+      }
+      
+    }
+  catch (std::exception &exc)
+    {
+      std::cerr << std::endl << std::endl
+               << "----------------------------------------------------"
+               << std::endl;
+      std::cerr << "Exception on processing: " << std::endl
+               << exc.what() << std::endl
+               << "Aborting!" << std::endl
+               << "----------------------------------------------------"
+               << std::endl;
+      
+      return 1;
+    }
+  catch (...) 
+    {
+      std::cerr << std::endl << std::endl
+               << "----------------------------------------------------"
+               << std::endl;
+      std::cerr << "Unknown exception!" << std::endl
+               << "Aborting!" << std::endl
+               << "----------------------------------------------------"
+               << std::endl;
+      return 1;
+    };
+}
diff --git a/tests/petsc/vector_print/cmp/generic b/tests/petsc/vector_print/cmp/generic
new file mode 100644 (file)
index 0000000..c7ed048
--- /dev/null
@@ -0,0 +1,12 @@
+
+DEAL::3.14159
+DEAL::across=false
+0.0000000000e+00
+1.2345678901e+00
+2.4691357802e+00
+3.7037036704e+00
+4.9382715605e+00
+
+DEAL::across=true
+0.0000000000e+00 1.2345678901e+00 2.4691357802e+00 3.7037036704e+00 4.9382715605e+00 
+DEAL::3.14159

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.