From: young Date: Thu, 17 Oct 2013 09:42:20 +0000 (+0000) Subject: Finalise a PetscScalar-std complex test. X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1b32a3fcc6f5234615ef05a2bf36e8117a8c5a01;p=dealii-svn.git Finalise a PetscScalar-std complex test. git-svn-id: https://svn.dealii.org/branches/branch_petscscalar_complex@31278 0785d39b-7218-0410-832d-ea1e28bc413d --- diff --git a/tests/petsc_scalar_complex/00-manipulate_petsc_scalar.cc b/tests/petsc_scalar_complex/00-manipulate_petsc_scalar.cc index 07f565e606..3c353b83a4 100644 --- a/tests/petsc_scalar_complex/00-manipulate_petsc_scalar.cc +++ b/tests/petsc_scalar_complex/00-manipulate_petsc_scalar.cc @@ -1,56 +1,72 @@ - -// PETSc includes +// PETSc includes: We would like to do this cleanly +// #include #include #include +// +// though a horrible warning message +// +// warning: imaginary constants are a GCC extension [enabled by default] +// +// appears. This seems to be related to the direct useage of +// PetscScalar in accordance with gcc.gnu.org bug 7263. + +// deal.II includes +#include #include #include -#include #include -// The entire point of these tests is to check that -// std::complex and PetscScalar complex are compatible without -// explicitly casting. Suprisingly, it seems they are. +#ifdef PETSC_USE_COMPLEX -// Initialize a std::complex number from an PETSc complex number +std::ofstream logfile ("00-manipulate_petsc_scalar/output"); + +// These tests were used while building extensions for +// PetscSclar=complex. They check that std::complex and +// PetscScalar complex are compatible without explicitly casting. +// +// These tests are archaic and produce unusual verbose output for +// historical reasons... + +// Divide a PETSc complex number by a real 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; - + logfile << " 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; - + + logfile << " 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 PetscScalar alpha = 1.0 + 2.0i; const std::complex beta = alpha; - - std::cout << " make std::complex from petsc complex: " - << std::real (beta) - << "+" << std::imag (beta) - << "i" - << std::endl; - + + logfile << " make std::complex from petsc complex: " + << std::real (beta) + << "+" << std::imag (beta) + << "i" + << std::endl; + // even this works assert (alpha==beta); } @@ -61,12 +77,12 @@ void make_petsc_complex_from_std_complex () const std::complex beta (1,2); const PetscScalar alpha = beta; - std::cout << " make petsc complex from std::complex: " - << PetscRealPart (alpha) - << "+" << PetscImaginaryPart (alpha) - << "i" - << std::endl; - + logfile << " make petsc complex from std::complex: " + << PetscRealPart (alpha) + << "+" << PetscImaginaryPart (alpha) + << "i" + << std::endl; + assert (alpha==beta); } @@ -75,96 +91,100 @@ 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; + logfile << " make petsc complex: " + << PetscRealPart (alpha) + << "+" << PetscImaginaryPart (alpha) + << "i" + << " should be 1+2i" + << std::endl; } -// Initialize a PETSc complex number directly only. +// Initialize a PETSc complex number directly only, check he is +// initialised to 0+0i. void init_petsc_complex () { const PetscScalar alpha; - std::cout << " make petsc complex: " - << PetscRealPart (alpha) - << "+" << PetscImaginaryPart (alpha) - << "i" - << " should be 0+0i" - << std::endl; - + logfile << " 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; + logfile << " make petsc complex: " + << PetscRealPart (beta) + << "+" << PetscImaginaryPart (beta) + << "i" + << " should be 0+0i" + << std::endl; + + assert (alpha==beta); } - + int main (int argc, char **argv) { + dealii::deallog.attach (logfile); + dealii::deallog.depth_console (1); + try { - // PetscInitialize (&argc, &argv, (char*) 0, (char*) 0); - + 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: "; + logfile << " make petsc complex from std::complex: "; const std::complex number1 (1,2); PetscScalar alpha1; alpha1 += number1; - std::cout << PetscRealPart (alpha1) - << "+" << PetscImaginaryPart (alpha1) - << "i" - << " should be 1+2i" - << std::endl; - + logfile << 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: "; + logfile << " make std::complex from petsc complex: "; const PetscScalar alpha2 = 1.0 + 2.0i; std::complex 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: "; + + logfile << std::real (number2) + << "+" << std::imag (number2) + << "i" + << " should be 1+2i" + << std::endl; + + logfile << " make std::complex from petsc complex: "; const PetscScalar alpha3 = 1.0 - 2.0i; number2 += alpha3; - - std::cout << std::real (number2) - << "+" << std::imag (number2) + + logfile << std::real (number2) + << "+" << std::imag (number2) << "i" - << " should be 2+0i" - << std::endl; - + << " should be 2+0i" + << std::endl; + // Let's try some other things... divide_petsc_complex_by_a_number (); } - - // PetscFinalize (); + PetscFinalize (); } - - + + catch (std::exception &exc) { std::cerr << std::endl << std::endl @@ -175,7 +195,7 @@ int main (int argc, char **argv) << "Aborting!" << std::endl << "----------------------------------------------------" << std::endl; - + return 1; } catch (...) @@ -189,10 +209,10 @@ int main (int argc, char **argv) << std::endl; return 1; } - - std::cout << std::endl; - + + logfile << std::endl; + return 0; } - +#endif // PETSC_USE_COMPLEX diff --git a/tests/petsc_scalar_complex/00-manipulate_petsc_scalar/generic b/tests/petsc_scalar_complex/00-manipulate_petsc_scalar/generic new file mode 100644 index 0000000000..d76579b0c3 --- /dev/null +++ b/tests/petsc_scalar_complex/00-manipulate_petsc_scalar/generic @@ -0,0 +1,12 @@ + + make petsc complex: 0.00000+0.00000i should be 0+0i + make petsc complex: 0.00000+0.00000i should be 0+0i + make petsc complex: 1.00000+2.00000i should be 1+2i + make petsc complex from std::complex: 1.00000+2.00000i + make std::complex from petsc complex: 1.00000+2.00000i + make petsc complex from std::complex: 1.00000+2.00000i should be 1+2i + make std::complex from petsc complex: 1.00000+2.00000i should be 1+2i + make std::complex from petsc complex: 2.00000+0.00000i should be 2+0i + divide a petsc complex by 2.: 0.500000+1.00000i should be 0.5+1i + divide-equals a petsc complex by 2.: 0.500000+1.00000i should be 0.5+1i +