]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Add test for SD scalar substitution and evaluation
authorJean-Paul Pelteret <jppelteret@gmail.com>
Tue, 7 May 2019 20:00:42 +0000 (22:00 +0200)
committerJean-Paul Pelteret <jppelteret@gmail.com>
Sun, 12 May 2019 19:24:02 +0000 (21:24 +0200)
tests/symengine/symengine_scalar_operations_03.cc [new file with mode: 0644]
tests/symengine/symengine_scalar_operations_03.output [new file with mode: 0644]

diff --git a/tests/symengine/symengine_scalar_operations_03.cc b/tests/symengine/symengine_scalar_operations_03.cc
new file mode 100644 (file)
index 0000000..51bf24c
--- /dev/null
@@ -0,0 +1,97 @@
+// ---------------------------------------------------------------------
+//
+// Copyright (C) 2019 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.
+//
+// ---------------------------------------------------------------------
+
+
+// Check that symbol substitution works for scalars
+
+#include <deal.II/differentiation/sd.h>
+
+#include "../tests.h"
+
+using namespace dealii;
+namespace SD = Differentiation::SD;
+
+
+int
+main()
+{
+  initlog();
+
+  using SD_number_t = SD::Expression;
+
+  const double a = 1.0;
+  const double b = 2.0;
+
+  const SD_number_t symb_a = SD::make_symbol("a");
+  const SD_number_t symb_b = SD::make_symbol("b");
+
+  deallog.push("Substitution: Individual");
+  {
+    const SD_number_t symb_a_plus_b = symb_a + symb_b;
+    deallog << "Symbolic a+b: " << symb_a_plus_b << std::endl;
+
+    const SD_number_t symb_a_plus_b_1 =
+      SD::substitute(symb_a_plus_b, symb_a, a);
+    deallog << "Symbolic a+b (a=1): " << symb_a_plus_b_1 << std::endl;
+
+    const SD_number_t symb_a_plus_b_subs =
+      SD::substitute(symb_a_plus_b_1, symb_b, b);
+    deallog << "Symbolic a+b (a=1, b=2): " << symb_a_plus_b_subs << std::endl;
+  }
+  deallog.pop();
+
+  deallog.push("Substitution: Vector");
+  {
+    const SD_number_t symb_a_plus_b = symb_a + symb_b;
+
+    const std::vector<std::pair<SD_number_t, double>> symbol_values{
+      std::make_pair(symb_a, a), std::make_pair(symb_b, b)};
+
+    const SD_number_t symb_a_plus_b_subs =
+      SD::substitute(symb_a_plus_b, symbol_values);
+    deallog << "Symbolic a+b (a=1, b=2): " << symb_a_plus_b_subs << std::endl;
+  }
+  deallog.pop();
+
+  deallog.push("Substitution: Map");
+  {
+    const SD_number_t symb_a_plus_b = symb_a + symb_b;
+
+    const SD::types::substitution_map substitution_map =
+      SD::make_substitution_map(std::make_pair(symb_a, a),
+                                std::make_pair(symb_b, b));
+
+    const SD_number_t symb_a_plus_b_subs =
+      SD::substitute(symb_a_plus_b, substitution_map);
+    deallog << "Symbolic a+b (a=1, b=2): " << symb_a_plus_b_subs << std::endl;
+  }
+  deallog.pop();
+
+  deallog.push("Substitution with evaluation");
+  {
+    const SD_number_t symb_a_plus_b = symb_a + symb_b;
+
+    const SD::types::substitution_map substitution_map =
+      SD::make_substitution_map(std::make_pair(symb_a, a),
+                                std::make_pair(symb_b, b));
+
+    const double symb_a_plus_b_subs =
+      SD::substitute_and_evaluate<double>(symb_a_plus_b, substitution_map);
+    Assert(symb_a_plus_b_subs == (a + b), ExcInternalError());
+  }
+  deallog.pop();
+
+  deallog << "OK" << std::endl;
+}
diff --git a/tests/symengine/symengine_scalar_operations_03.output b/tests/symengine/symengine_scalar_operations_03.output
new file mode 100644 (file)
index 0000000..f654013
--- /dev/null
@@ -0,0 +1,7 @@
+
+DEAL:Substitution: Individual::Symbolic a+b: a + b
+DEAL:Substitution: Individual::Symbolic a+b (a=1): 1.0 + b
+DEAL:Substitution: Individual::Symbolic a+b (a=1, b=2): 3.0
+DEAL:Substitution: Vector::Symbolic a+b (a=1, b=2): 3.0
+DEAL:Substitution: Map::Symbolic a+b (a=1, b=2): 3.0
+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.