]> https://gitweb.dealii.org/ - dealii-svn.git/commitdiff
Record a petscscalar complex tests that was useful.
authoryoung <young@0785d39b-7218-0410-832d-ea1e28bc413d>
Fri, 11 Oct 2013 14:02:08 +0000 (14:02 +0000)
committeryoung <young@0785d39b-7218-0410-832d-ea1e28bc413d>
Fri, 11 Oct 2013 14:02:08 +0000 (14:02 +0000)
git-svn-id: https://svn.dealii.org/branches/branch_petscscalar_complex@31206 0785d39b-7218-0410-832d-ea1e28bc413d

tests/petsc_scalar_complex/00-manipulate_petsc_scalar.cc [new file with mode: 0644]
tests/petsc_scalar_complex/Makefile [new file with mode: 0644]

diff --git a/tests/petsc_scalar_complex/00-manipulate_petsc_scalar.cc b/tests/petsc_scalar_complex/00-manipulate_petsc_scalar.cc
new file mode 100644 (file)
index 0000000..07f565e
--- /dev/null
@@ -0,0 +1,198 @@
+                           
+
+// PETSc includes
+#include <petscksp.h>
+#include <petscsys.h>
+#include <petscmath.h>
+    
+#include <fstream>
+#include <iostream>
+#include <complex> 
+#include <cassert>
+
+// The entire point of these tests is to check that
+// std::complex<double> and PetscScalar complex are compatible without
+// explicitly casting. Suprisingly, it seems they are.
+
+// Initialize a std::complex number from an PETSc complex number
+void divide_petsc_complex_by_a_number ()
+{
+  PetscScalar alpha      = 1.0 + 2.0i;
+  const PetscScalar beta = alpha/2.;
+
+  std::cout << "   divide a petsc complex by 2.: "
+           << PetscRealPart (beta)
+           << "+" << PetscImaginaryPart (beta)
+           << "i"
+           << " should be 0.5+1i"
+           << std::endl;
+
+  alpha /= 2.;
+
+  std::cout << "   divide-equals a petsc complex by 2.: "
+           << PetscRealPart (alpha)
+           << "+" << PetscImaginaryPart (alpha)
+           << "i"
+           << " should be 0.5+1i"
+           << std::endl;
+
+  assert (alpha==beta);
+}
+
+// Initialize a std::complex number from an PETSc complex number
+void make_std_complex_from_petsc_complex ()
+{
+  const PetscScalar alpha = 1.0 + 2.0i;
+  const std::complex<double> beta = alpha;
+
+  std::cout << "   make std::complex from petsc complex: "
+           << std::real (beta)
+           << "+" << std::imag (beta)
+           << "i"
+           << std::endl;
+
+  // even this works
+  assert (alpha==beta);
+}
+
+// Initialize a PETSc complex number from an std::complex number
+void make_petsc_complex_from_std_complex ()
+{
+  const std::complex<double> beta (1,2);
+  const PetscScalar alpha = beta; 
+  
+  std::cout << "   make petsc complex from std::complex: "
+           << PetscRealPart (alpha)
+           << "+" << PetscImaginaryPart (alpha)
+           << "i"
+           << std::endl;
+
+  assert (alpha==beta);
+}
+
+// Initialize a PETSc complex number directly.
+void make_petsc_complex ()
+{
+  const PetscScalar alpha = 1.0 + 2.0i; 
+  
+  std::cout << "   make petsc complex: "
+           << PetscRealPart (alpha)
+           << "+" << PetscImaginaryPart (alpha)
+           << "i"
+           << " should be 1+2i"
+           << std::endl;
+}
+
+
+// Initialize a PETSc complex number directly only.
+void init_petsc_complex ()
+{
+  const PetscScalar alpha; 
+  
+  std::cout << "   make petsc complex: "
+           << PetscRealPart (alpha)
+           << "+" << PetscImaginaryPart (alpha)
+           << "i"
+           << " should be 0+0i"
+           << std::endl;
+
+  const PetscScalar beta = 0.; 
+  
+  std::cout << "   make petsc complex: "
+           << PetscRealPart (beta)
+           << "+" << PetscImaginaryPart (beta)
+           << "i"
+           << " should be 0+0i"
+           << std::endl;
+}               
+     
+int main (int argc, char **argv)
+{
+  try
+    {
+      //      PetscInitialize (&argc, &argv, (char*) 0, (char*) 0);
+
+      {
+       // some initialisations makes and so on...
+       init_petsc_complex ();
+       make_petsc_complex ();
+
+       // first do conversions explicitly
+       make_petsc_complex_from_std_complex ();
+       make_std_complex_from_petsc_complex ();
+
+       // then try to use operators to do the same thing
+       std::cout << "   make petsc complex from std::complex: ";
+       const std::complex<double> number1 (1,2);
+       PetscScalar alpha1;
+       alpha1 += number1; 
+
+       std::cout << PetscRealPart (alpha1)
+                 << "+" << PetscImaginaryPart (alpha1)
+                 << "i"
+                 << " should be 1+2i"
+                 << std::endl;
+
+       // then try to use operators to do the same thing - except it
+       // seems to work without(!)
+       std::cout << "   make std::complex from petsc complex: ";
+       const PetscScalar alpha2 = 1.0 + 2.0i; 
+       std::complex<double> number2;
+       number2 += alpha2; 
+
+       std::cout << std::real (number2)
+                 << "+" << std::imag (number2)
+                 << "i"
+                 << " should be 1+2i"
+                 << std::endl;
+
+       std::cout << "   make std::complex from petsc complex: ";
+       const PetscScalar alpha3 = 1.0 - 2.0i; 
+       number2 += alpha3; 
+
+       std::cout << std::real (number2)
+                 << "+" << std::imag (number2)
+                 << "i"
+                 << " should be 2+0i"
+                 << std::endl;
+
+       // Let's try some other things...
+       divide_petsc_complex_by_a_number ();
+
+      }
+
+      //      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;
+    }
+
+  std::cout << std::endl;
+
+  return 0;
+}
+
+
diff --git a/tests/petsc_scalar_complex/Makefile b/tests/petsc_scalar_complex/Makefile
new file mode 100644 (file)
index 0000000..3dc8d02
--- /dev/null
@@ -0,0 +1,10 @@
+############################################################
+# Makefile,v 1.15 2002/06/13 12:51:13 hartmann Exp
+# Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 by the deal.II authors
+############################################################
+
+include ../Makefile.paths
+include $D/common/Make.global_options
+include ../Makefile.rules
+-include Makefile.depend
+-include Makefile.tests

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.