]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Check for imaginary parts
authorDaniel Arndt <daniel.arndt@iwr.uni-heidelberg.de>
Mon, 18 Feb 2019 11:44:20 +0000 (12:44 +0100)
committerDaniel Arndt <daniel.arndt@iwr.uni-heidelberg.de>
Wed, 20 Feb 2019 12:46:19 +0000 (13:46 +0100)
include/deal.II/lac/read_write_vector.templates.h

index 1444b00856d5fee5e4aed28cef4067d73d4f6c31..cfbbc7db890b74c06d1dcbffa1049eecf509ea0b 100644 (file)
@@ -581,15 +581,39 @@ namespace LinearAlgebra
           break;
 
         case VectorOperation::min:
+          // To ensure that this code also compiles with complex
+          // numbers, we only compare the real part of the
+          // variable. Note that min/max do not make sense with complex
+          // numbers.
           for (size_type i = 0; i < size; ++i)
-            if (std::real(new_values[i]) - std::real(values[i]) < 0.0)
-              values[i] = new_values[i];
+            {
+              Assert(
+                std::imag(new_values[i]) == 0.,
+                ExcMessage(
+                  "VectorOperation::min is not defined if there is an imaginary part!)"));
+              Assert(
+                std::imag(values[i]) == 0.,
+                ExcMessage(
+                  "VectorOperation::min is not defined if there is an imaginary part!)"));
+              if (std::real(new_values[i]) - std::real(values[i]) < 0.0)
+                values[i] = new_values[i];
+            }
           break;
 
         case VectorOperation::max:
           for (size_type i = 0; i < size; ++i)
-            if (std::real(new_values[i]) - std::real(values[i]) > 0.0)
-              values[i] = new_values[i];
+            {
+              Assert(
+                std::imag(new_values[i]) == 0.,
+                ExcMessage(
+                  "VectorOperation::max is not defined if there is an imaginary part!)"));
+              Assert(
+                std::imag(values[i]) == 0.,
+                ExcMessage(
+                  "VectorOperation::max is not defined if there is an imaginary part!)"));
+              if (std::real(new_values[i]) - std::real(values[i]) > 0.0)
+                values[i] = new_values[i];
+            }
           break;
 
         default:
@@ -697,8 +721,18 @@ namespace LinearAlgebra
         // variable. Note that min/max do not make sense with complex
         // numbers.
         for (int i = 0; i < size; ++i)
-          if (std::real(new_values[i]) - std::real(values[i]) < 0.0)
-            values[i] = new_values[i];
+          {
+            Assert(
+              std::imag(new_values[i]) == 0.,
+              ExcMessage(
+                "VectorOperation::min is not defined if there is an imaginary part!)"));
+            Assert(
+              std::imag(values[i]) == 0.,
+              ExcMessage(
+                "VectorOperation::min is not defined if there is an imaginary part!)"));
+            if (std::real(new_values[i]) - std::real(values[i]) < 0.0)
+              values[i] = new_values[i];
+          }
       }
     else if (operation == VectorOperation::max)
       {
@@ -713,8 +747,18 @@ namespace LinearAlgebra
                ExcInternalError("Import failed."));
 
         for (int i = 0; i < size; ++i)
-          if (std::real(new_values[i]) - std::real(values[i]) > 0.0)
-            values[i] = new_values[i];
+          {
+            Assert(
+              std::imag(new_values[i]) == 0.,
+              ExcMessage(
+                "VectorOperation::max is not defined if there is an imaginary part!)"));
+            Assert(
+              std::imag(values[i]) == 0.,
+              ExcMessage(
+                "VectorOperation::max is not defined if there is an imaginary part!)"));
+            if (std::real(new_values[i]) - std::real(values[i]) > 0.0)
+              values[i] = new_values[i];
+          }
       }
     else
       AssertThrow(false, ExcNotImplemented());
@@ -828,8 +872,18 @@ namespace LinearAlgebra
         // variable. Note that min/max do not make sense with complex
         // numbers.
         for (unsigned int i = 0; i < n_elements; ++i)
-          if (std::real(tmp[i]) - std::real(values[i]) < 0.0)
-            values[i] = tmp[i];
+          {
+            Assert(
+              std::imag(new_values[i]) == 0.,
+              ExcMessage(
+                "VectorOperation::min is not defined if there is an imaginary part!)"));
+            Assert(
+              std::imag(values[i]) == 0.,
+              ExcMessage(
+                "VectorOperation::min is not defined if there is an imaginary part!)"));
+            if (std::real(tmp[i]) - std::real(values[i]) < 0.0)
+              values[i] = tmp[i];
+          }
       }
     else if (operation == VectorOperation::max)
       {
@@ -842,8 +896,18 @@ namespace LinearAlgebra
         AssertCuda(error_code);
 
         for (unsigned int i = 0; i < n_elements; ++i)
-          if (std::real(tmp[i]) - std::real(values[i]) > 0.0)
-            values[i] = tmp[i];
+          {
+            Assert(
+              std::imag(new_values[i]) == 0.,
+              ExcMessage(
+                "VectorOperation::max is not defined if there is an imaginary part!)"));
+            Assert(
+              std::imag(values[i]) == 0.,
+              ExcMessage(
+                "VectorOperation::max is not defined if there is an imaginary part!)"));
+            if (std::real(tmp[i]) - std::real(values[i]) > 0.0)
+              values[i] = tmp[i];
+          }
       }
     else
       AssertThrow(false, ExcNotImplemented());

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.