]> https://gitweb.dealii.org/ - dealii-svn.git/commitdiff
Test real() and imag() work correctly on complex vectors.
authoryoung <young@0785d39b-7218-0410-832d-ea1e28bc413d>
Fri, 18 Oct 2013 15:06:54 +0000 (15:06 +0000)
committeryoung <young@0785d39b-7218-0410-832d-ea1e28bc413d>
Fri, 18 Oct 2013 15:06:54 +0000 (15:06 +0000)
git-svn-id: https://svn.dealii.org/branches/branch_petscscalar_complex@31305 0785d39b-7218-0410-832d-ea1e28bc413d

tests/petsc_scalar_complex/element_access.cc [new file with mode: 0644]
tests/petsc_scalar_complex/element_access/cmp/generic [new file with mode: 0644]

diff --git a/tests/petsc_scalar_complex/element_access.cc b/tests/petsc_scalar_complex/element_access.cc
new file mode 100644 (file)
index 0000000..aa64a6d
--- /dev/null
@@ -0,0 +1,145 @@
+
+// ---------------------------------------------------------------------
+// $Id: 
+//
+// Copyright (C) 2013 by the deal.II authors
+//
+// This file is part of the deal.II library.
+//
+// The deal.II library is free software; you can use it, redistribute
+// it, and/or modify it under the terms of the GNU Lesser General
+// Public License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+// The full text of the license can be found in the file LICENSE at
+// the top level of the deal.II distribution.
+//
+// ---------------------------------------------------------------------
+
+
+// deal.II includes
+#include "../tests.h"
+#include <deal.II/lac/petsc_vector.h>
+#include <deal.II/lac/petsc_sparse_matrix.h>
+    
+#include <fstream>
+#include <iostream>
+#include <cassert>
+
+#include <complex>
+
+// test dealii::internal::VectorReference::real() 
+// test dealii::internal::VectorReference::imag() 
+// on vector and matrix
+
+// vector elements
+void test_vector (PETScWrappers::Vector &v)
+{
+  deallog << "Check vector access" << std::endl;
+
+  // fill up a vector with some numbers
+  for (unsigned int k=0; k<v.size(); ++k)
+    v(k) = std::complex<double> (k,v.size()-k);
+
+  v.compress (VectorOperation::add);
+
+  // check that is what we get by casting PetscScalar to std::real()
+  // and std::imag()
+  for (unsigned int k=0; k<v.size(); ++k)
+    Assert ((static_cast<std::complex<double> > (v(k)).real ()==k) && 
+           (static_cast<std::complex<double> > (v(k)).imag ()==v.size()-k), 
+           ExcInternalError());
+  
+  // check that is what we get by
+  // dealii::internal::VectorReference::real() and
+  // dealii::internal::VectorReference::imag() 
+  for (unsigned int k=0; k<v.size(); ++k)
+    Assert ((v(k).real ()==k) && (v(k).imag ()==v.size()-k), 
+           ExcInternalError());
+  
+  deallog << "OK" << std::endl;
+}
+
+// sparse matrix elements
+void test_matrix (PETScWrappers::SparseMatrix &m)
+{
+  deallog << "Check matrix access" << std::endl;
+
+  // fill up a matrix with some numbers
+  for (unsigned int k=0; k<m.m(); ++k)
+    for (unsigned int l=0; l<m.n(); ++l)
+      m.set (k,l, std::complex<double> (k+l,-k-l));
+
+  m.compress (VectorOperation::add);
+
+  for (unsigned int k=0; k<m.m(); ++k)
+    for (unsigned int l=0; l<m.n(); ++l)
+      Assert ((static_cast<std::complex<double> > (m(k,l)).real ()==k+l) && 
+             (static_cast<std::complex<double> > (m(k,l)).imag ()==-k-l), 
+             ExcInternalError());
+            
+  for (unsigned int k=0; k<m.m(); ++k)
+    for (unsigned int l=0; l<m.n(); ++l)
+      Assert ((m(k,l).real ()==k+l) && (m(k,l).imag ()==-k-l), 
+             ExcInternalError());
+
+  deallog << "OK" << std::endl;
+}
+
+int main (int argc, char **argv)
+{
+  std::ofstream logfile ("element_access/output");
+  dealii::deallog.attach (logfile);
+  dealii::deallog.depth_console (0);
+  deallog.threshold_double(1.e-10);
+  
+  try
+    {
+      PetscInitialize (&argc, &argv, (char*) 0, (char*) 0);
+      {
+        PETScWrappers::Vector v (5);
+       test_vector (v);
+       
+       // Suprising the matrix part does not produce an
+       // error. However, something is VERY fishy here.
+       //
+       // @TODO Why does this test pass? I have no real() or imag()
+       // operators/functions defined.
+
+       // PETScWrappers::SparseMatrix m (5,5,5);
+       // test_matrix (m);
+      }
+      PetscFinalize ();
+    }
+  
+  
+  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;
+    }
+  
+  logfile << std::endl;
+  
+  return 0;
+}
+
+
diff --git a/tests/petsc_scalar_complex/element_access/cmp/generic b/tests/petsc_scalar_complex/element_access/cmp/generic
new file mode 100644 (file)
index 0000000..6a61861
--- /dev/null
@@ -0,0 +1,4 @@
+
+DEAL::Check vector access
+DEAL::OK
+

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.