From b7b1cb7ab2ce958676a6a20bac67208a40495fea Mon Sep 17 00:00:00 2001
From: "Joachim Wuttke (h)" <j.wuttke@fz-juelich.de>
Date: Sun, 3 Apr 2022 23:01:57 +0200
Subject: [PATCH] Example/ff/Pyramid4.py covered by test

---
 Device/Data/DataUtils.cpp                     |   2 +-
 Device/Histo/HistoUtils.cpp                   |  24 ++++++++++--------
 Examples/ff/Pyramid4.py                       |   3 ++-
 Tests/Examples/CMakeLists.txt                 |   2 ++
 Tests/Examples/PyPersistence.py.in            |  13 +++-------
 .../ExamplesMini/Pyramid4.int.gz              | Bin 0 -> 351 bytes
 Wrap/Python/ff_demo.py                        |   7 +++--
 7 files changed, 26 insertions(+), 25 deletions(-)
 create mode 100644 Tests/ReferenceData/ExamplesMini/Pyramid4.int.gz

diff --git a/Device/Data/DataUtils.cpp b/Device/Data/DataUtils.cpp
index 7002d4001ff..d22cc4d67df 100644
--- a/Device/Data/DataUtils.cpp
+++ b/Device/Data/DataUtils.cpp
@@ -43,7 +43,7 @@ double DataUtils::Data::relativeDataDifference(const OutputData<double>& dat,
         diff += Numeric::GetRelativeDifference(dat[i], ref[i]);
     diff /= dat.getAllocatedSize();
     if (std::isnan(diff))
-        throw std::runtime_error("diff=NaN!");
+        throw std::runtime_error("diff=NaN");
     return diff;
 }
 
diff --git a/Device/Histo/HistoUtils.cpp b/Device/Histo/HistoUtils.cpp
index a44512186a1..e8e2bdec40d 100644
--- a/Device/Histo/HistoUtils.cpp
+++ b/Device/Histo/HistoUtils.cpp
@@ -90,18 +90,22 @@ bool DataUtils::Histo::checkRelativeDifference(const OutputData<double>& dat,
         return false;
     }
 
-    const double diff = DataUtils::Data::relativeDataDifference(dat, ref);
-    if (diff > threshold) {
-        std::cerr << "FAILED: relative deviation of dat from ref is " << diff
-                  << ", above given threshold " << threshold << std::endl;
+    try {
+        const double diff = DataUtils::Data::relativeDataDifference(dat, ref);
+        if (diff > threshold) {
+            std::cerr << "FAILED: relative deviation of dat from ref is " << diff
+                      << ", above given threshold " << threshold << std::endl;
+            return false;
+        }
+        if (diff)
+            std::cerr << "- OK: relative deviation of dat from ref is " << diff
+                      << ", within given threshold " << threshold << std::endl;
+        else
+            std::cout << "- OK: dat = ref\n";
+        return true;
+    } catch (...) {
         return false;
     }
-    if (diff)
-        std::cerr << "- OK: relative deviation of dat from ref is " << diff
-                  << ", within given threshold " << threshold << std::endl;
-    else
-        std::cout << "- OK: dat = ref\n";
-    return true;
 }
 
 bool DataUtils::Histo::agreesWithReference(const SimulationResult& dat,
diff --git a/Examples/ff/Pyramid4.py b/Examples/ff/Pyramid4.py
index 3e583f53c38..aecc04eff25 100755
--- a/Examples/ff/Pyramid4.py
+++ b/Examples/ff/Pyramid4.py
@@ -3,4 +3,5 @@ import bornagain as ba
 from bornagain import deg, nm, ff_demo
 
 ff = ba.FormFactorPyramid4(10*nm, 5*nm, 54.73*deg)
-ff_demo.run_and_plot_sas_ff(ff)
+if __name__ == '__main__':
+    ff_demo.run_and_plot_sas_ff(ff)
diff --git a/Tests/Examples/CMakeLists.txt b/Tests/Examples/CMakeLists.txt
index 669c3e40ba9..acaa9169db0 100644
--- a/Tests/Examples/CMakeLists.txt
+++ b/Tests/Examples/CMakeLists.txt
@@ -64,6 +64,8 @@ endfunction()
 #  Persistence tests
 ####################################################################################################
 
+test_example(ff/Pyramid4 2e-10)
+
 test_example(scatter2d/ApproximationDA 2e-10)
 test_example(scatter2d/ApproximationLMA 2e-10)
 test_example(scatter2d/ApproximationSSCA 2e-10)
diff --git a/Tests/Examples/PyPersistence.py.in b/Tests/Examples/PyPersistence.py.in
index a0834b27ff5..85d2b1b7b18 100644
--- a/Tests/Examples/PyPersistence.py.in
+++ b/Tests/Examples/PyPersistence.py.in
@@ -24,14 +24,6 @@ example = __import__(EXAMPLE_NAME)
 simulationObject = None
 
 
-def get_simulation_SpecularSimulation():
-    """
-    Returns custom simulation for SpecularSimulation.py.
-    """
-    simulation = example.get_simulation(example.get_sample(), scan_size=10)
-    return simulation
-
-
 def get_simulation_DepthProbe():
     """
     Returns custom simulation in the case of depth probe.
@@ -82,7 +74,10 @@ def get_minified_simulation():
     Returns a simulation constructed from example simulation with smaller detector.
     """
     if "specular" in EXAMPLE_DIR:
-        return get_simulation_SpecularSimulation()
+        return example.get_simulation(example.get_sample(), scan_size=10)
+
+    if "ff" in EXAMPLE_DIR:
+        return ba.ff_demo.get_simulation(ba.ff_demo.get_sample(example.ff), 10)
 
     elif EXAMPLE_NAME == "RectangularGrating":
         return get_simulation_RectangularGrating()
diff --git a/Tests/ReferenceData/ExamplesMini/Pyramid4.int.gz b/Tests/ReferenceData/ExamplesMini/Pyramid4.int.gz
new file mode 100644
index 0000000000000000000000000000000000000000..09da9c1decf9ecb323ed313f25c6df8e47f598ce
GIT binary patch
literal 351
zcmb2|=3oGW|93;Zvkn`G+<7gcc5};x8Q-VhGg>H8#pCSs@mQ-`_m^)|IrF$`Pch~^
zX>Ct@b~a+$w!05MXWy#ld3<HB|Mbfxa{Bl0zAlgq^17Y3qwl%wlI<U>tmKYm#>>w)
zGv=5*o$uk&)!vNr>UT;Me6;^=Azg6(D3?N)=H*QGrK`oi9>~#fJrbey$~enbnL)cS
z?0k7%q0@Pm&wCf_WY=8PDjM)P*(5Aur|aaSnnBuafqD_s)3nMHMV8IEY1yr>@$E_2
z%%cnVVlUPN7Z|RZb#(3<Ln-Yed23HD5?OiGUq#2Zdg2?EC2pq$ZdpsMC`uHI-66U*
zHOO&w<`;f*H|EwGYeg-+CO*;?EWg>+_#jU&h&Sy@9v9!V;#|p9iLP6Fk8y2`ey!`S
z8T3c=$Zhp^dqta1*q$$4be8wDvPqxJ&ZCo$YF?AgyL!9msr?jDdDhi-M=taGaP0dk
I&&j|50Oxq2VE_OC

literal 0
HcmV?d00001

diff --git a/Wrap/Python/ff_demo.py b/Wrap/Python/ff_demo.py
index 17dba359239..ba7d2831cbc 100644
--- a/Wrap/Python/ff_demo.py
+++ b/Wrap/Python/ff_demo.py
@@ -30,15 +30,14 @@ def get_sample(ff):
     return sample
 
 
-def get_simulation(sample):
+def get_simulation(sample, nPix):
     # Beam from above (perpendicular to sample):
     beam = ba.Beam(1, 0.4*nm, ba.Direction(90*deg, 0))
 
     # Detector opposite to source:
     detPos = 2000  # distance from sample center to detector in mm
     detWid = 500  # detector width in mm
-    detPix = 200  # number of pixels per direction
-    det = ba.RectangularDetector(detPix, detWid, detPix, detWid)
+    det = ba.RectangularDetector(nPix, detWid, nPix, detWid)
     det.setPerpendicularToDirectBeam(detPos, detWid/2, detWid/2)
 
     return ba.ScatteringSimulation(beam, sample, det)
@@ -46,5 +45,5 @@ def get_simulation(sample):
 
 def run_and_plot_sas_ff(ff):
     sample = get_sample(ff)
-    simulation = get_simulation(sample)
+    simulation = get_simulation(sample, 200)
     ba_plot.run_and_plot(simulation)
-- 
GitLab