Repository: CUQI-DTU/CUQIpy-FEniCS (language: python)

Issue:
Add demo for FEniCSPoisson2D and UQ method (ready for review)



Discussion / hints:

Fix patch:
diff --git a/cuqipy_fenics/__init__.py b/cuqipy_fenics/__init__.py
index 9019582..12954be 100644
--- a/cuqipy_fenics/__init__.py
+++ b/cuqipy_fenics/__init__.py
@@ -10,6 +10,6 @@
 __version__ = _version.get_versions()['version']
 
 #TODO: This logic need to be removed after _get_identity_geometries 
-# is removed from cuqipy 
+# is removed from cuqipy
 from cuqi.geometry import _identity_geometries
 _identity_geometries.append(geometry.FEniCSContinuous)
diff --git a/demos/demo_FEniCSPoisson2D.py b/demos/demo_FEniCSPoisson2D.py
new file mode 100644
index 0000000..bee0c5f
--- /dev/null
+++ b/demos/demo_FEniCSPoisson2D.py
@@ -0,0 +1,15 @@
+import cuqipy_fenics
+
+Ns = 100 # Number of samples
+poisson_problem = cuqipy_fenics.testproblem.FEniCSPoisson2D((32, 32), 
+                                    bc_types=['Dirichlet', 'Neumann', 'Dirichlet', 'Neumann'],
+                                    bc_values=[0, 0, 0, 0],
+                                    source_term=1.0,
+                                    field_type='KL',
+                                    mapping='exponential',
+                                    field_params={'num_terms': 32, 'length_scale': 0.1})
+
+# UQ method samples the posterior distribution of the poisson_problem
+# and plots the credible interval, the mean and the variance of the solution.
+# The sampling here might take a while (~13 min).
+samples = poisson_problem.UQ(Ns, percent=97)


Test patch:
diff --git a/tests/test_testproblem.py b/tests/test_testproblem.py
index a497217..2cf9b79 100644
--- a/tests/test_testproblem.py
+++ b/tests/test_testproblem.py
@@ -77,9 +77,10 @@ def test_FEniCSPoisson2D_source_term(source_term, valid):
     "prior, case",
     [(None, 0.02, None, None, None, None, "valid"),
      (None, 0.02, None, None, None, None, "unknown_field"),
-     (None, 0.05, None, None, "exponential", Gaussian(0, np.ones(21*21)), "valid"),
+     (None, 0.05, None, None, "exponential",
+      Gaussian(np.zeros(21*21), 1), "valid"),
      (lambda x: x[0]+x[1]+0.1, 0.02, None, None, "exponential",
-      Gaussian(0, np.ones(21*21), name='x'), "valid"),
+      Gaussian(np.zeros(21*21), 1, name='x'), "valid"),
      (None, 0.05, None, None, lambda m: m+1, None, "valid"),
      (None, 0.1, "KL", None, "exponential", None, "valid"),
      (np.random.randn(10), 0.02, "KL",
@@ -89,6 +90,9 @@ def test_FEniCSPoisson2D_setup(exactSolution, noise_level,
                                prior, case):
     """Test creating a FEniCSPoisson2D testproblem with different 
     parametrization, exact solution and noise level"""
+    # set the random seed
+    np.random.seed(0)
+
     if case == "valid":
         testproblem = FEniCSPoisson2D(
             (20, 20),
@@ -102,6 +106,7 @@ def test_FEniCSPoisson2D_setup(exactSolution, noise_level,
         testproblem.data.plot()
         testproblem.exactData.plot()
         testproblem.exactSolution.plot()
+        testproblem.UQ(Ns=20, percent=90)
 
     if case == "unknown_field":
         with pytest.raises(ValueError, match=r"Unknown field type"):