From 8c8e24bc5c415d33324981755d0a2aca8267022f Mon Sep 17 00:00:00 2001
From: Joachim Wuttke <j.wuttke@fz-juelich.de>
Date: Sat, 3 Sep 2022 08:40:36 +0200
Subject: [PATCH] + INode::requestIn

---
 Param/Node/INode.cpp                 |  9 ++++
 Param/Node/INode.h                   |  3 ++
 Sample/Correlations/Profiles1D.cpp   |  3 +-
 Sample/Correlations/Profiles2D.cpp   |  3 +-
 Sample/Interface/LayerRoughness.cpp  |  3 +-
 auto/Wrap/libBornAgainParam.py       |  9 ++++
 auto/Wrap/libBornAgainParam_wrap.cpp | 66 ++++++++++++++++++++++++++++
 7 files changed, 90 insertions(+), 6 deletions(-)

diff --git a/Param/Node/INode.cpp b/Param/Node/INode.cpp
index 516b53a4115..b26e4e44088 100644
--- a/Param/Node/INode.cpp
+++ b/Param/Node/INode.cpp
@@ -55,6 +55,15 @@ void INode::requestGe0(
         errs.push_back("negative " + name + "=" + std::to_string(val));
 }
 
+void INode::requestIn(
+    std::vector<std::string>& errs, const double& val, const std::string& name,
+    double min, double max)
+{
+    if (val<min || val>max)
+        errs.push_back("parameter " + name + "=" + std::to_string(val)
+                       + " not in [" + std::to_string(min) + ", " + std::to_string(max) + "]");
+}
+
 std::string INode::jointError(const std::vector<std::string> errs) const
 {
     return "{ " + className() + ": [ " + BaseUtils::String::join(errs, ", ") + " ] }";
diff --git a/Param/Node/INode.h b/Param/Node/INode.h
index e0c5d9e8199..bb84b4ef77f 100644
--- a/Param/Node/INode.h
+++ b/Param/Node/INode.h
@@ -56,6 +56,9 @@ public:
         std::vector<std::string>& errs, const double& val, const std::string& name);
     static void requestGe0(
         std::vector<std::string>& errs, const double& val, const std::string& name);
+    static void requestIn(
+        std::vector<std::string>& errs, const double& val, const std::string& name,
+        double min, double max);
     std::string jointError(const std::vector<std::string> errs) const;
 
     virtual std::string validate() const { return ""; }
diff --git a/Sample/Correlations/Profiles1D.cpp b/Sample/Correlations/Profiles1D.cpp
index 189ffaec253..192db810c52 100644
--- a/Sample/Correlations/Profiles1D.cpp
+++ b/Sample/Correlations/Profiles1D.cpp
@@ -320,8 +320,7 @@ std::string Profile1DVoigt::validate() const
 {
     std::vector<std::string> errs;
     requestGt0(errs, m_omega, "omega");
-    if (m_eta<0 || m_eta>1)
-        errs.push_back("eta="+std::to_string(m_eta)+" outside interval 0..1");
+    requestIn(errs, m_eta, "eta", 0, 1);
     if (!errs.empty())
         return jointError(errs);
     m_validated = true;
diff --git a/Sample/Correlations/Profiles2D.cpp b/Sample/Correlations/Profiles2D.cpp
index 50c1f7a5c54..e6696a10db4 100644
--- a/Sample/Correlations/Profiles2D.cpp
+++ b/Sample/Correlations/Profiles2D.cpp
@@ -268,8 +268,7 @@ std::string Profile2DVoigt::validate() const
     std::vector<std::string> errs;
     requestGt0(errs, m_omega_x, "omega_x");
     requestGt0(errs, m_omega_y, "omega_y");
-    if (m_eta<0 || m_eta>1)
-        errs.push_back("eta="+std::to_string(m_eta)+" outside interval 0..1");
+    requestIn(errs, m_eta, "eta", 0, 1);
     if (!errs.empty())
         return jointError(errs);
     m_validated = true;
diff --git a/Sample/Interface/LayerRoughness.cpp b/Sample/Interface/LayerRoughness.cpp
index b113c25d611..6835579a112 100644
--- a/Sample/Interface/LayerRoughness.cpp
+++ b/Sample/Interface/LayerRoughness.cpp
@@ -76,8 +76,7 @@ std::string LayerRoughness::validate() const
     std::vector<std::string> errs;
     requestGe0(errs, m_sigma, "sigma");
     if (m_sigma>0) {
-        if (m_hurstParameter<0 || m_hurstParameter>1)
-            errs.push_back("hurst="+std::to_string(m_hurstParameter) +" out of range 0..1");
+        requestIn(errs, m_hurstParameter, "hurst", 0, 1);
         requestGe0(errs, m_lateralCorrLength, "lateralCorrLength"); // TODO why not gt0 ?
     }
     if (!errs.empty())
diff --git a/auto/Wrap/libBornAgainParam.py b/auto/Wrap/libBornAgainParam.py
index 2760c8cd832..6b9d0f6710d 100644
--- a/auto/Wrap/libBornAgainParam.py
+++ b/auto/Wrap/libBornAgainParam.py
@@ -2123,6 +2123,11 @@ class INode(object):
         r"""requestGe0(vector_string_t errs, double const & val, std::string const & name)"""
         return _libBornAgainParam.INode_requestGe0(errs, val, name)
 
+    @staticmethod
+    def requestIn(errs, val, name, min, max):
+        r"""requestIn(vector_string_t errs, double const & val, std::string const & name, double min, double max)"""
+        return _libBornAgainParam.INode_requestIn(errs, val, name, min, max)
+
     def jointError(self, errs):
         r"""
         jointError(INode self, vector_string_t errs) -> std::string
@@ -2162,6 +2167,10 @@ def INode_requestGe0(errs, val, name):
     r"""INode_requestGe0(vector_string_t errs, double const & val, std::string const & name)"""
     return _libBornAgainParam.INode_requestGe0(errs, val, name)
 
+def INode_requestIn(errs, val, name, min, max):
+    r"""INode_requestIn(vector_string_t errs, double const & val, std::string const & name, double min, double max)"""
+    return _libBornAgainParam.INode_requestIn(errs, val, name, min, max)
+
 class IDistribution1D(libBornAgainBase.ICloneable, INode):
     r"""
 
diff --git a/auto/Wrap/libBornAgainParam_wrap.cpp b/auto/Wrap/libBornAgainParam_wrap.cpp
index a13099ccf14..78d8cbc2485 100644
--- a/auto/Wrap/libBornAgainParam_wrap.cpp
+++ b/auto/Wrap/libBornAgainParam_wrap.cpp
@@ -28125,6 +28125,71 @@ fail:
 }
 
 
+SWIGINTERN PyObject *_wrap_INode_requestIn(PyObject *SWIGUNUSEDPARM(self), PyObject *args) {
+  PyObject *resultobj = 0;
+  std::vector< std::string,std::allocator< std::string > > *arg1 = 0 ;
+  double *arg2 = 0 ;
+  std::string *arg3 = 0 ;
+  double arg4 ;
+  double arg5 ;
+  void *argp1 = 0 ;
+  int res1 = 0 ;
+  double temp2 ;
+  double val2 ;
+  int ecode2 = 0 ;
+  int res3 = SWIG_OLDOBJ ;
+  double val4 ;
+  int ecode4 = 0 ;
+  double val5 ;
+  int ecode5 = 0 ;
+  PyObject *swig_obj[5] ;
+  
+  if (!SWIG_Python_UnpackTuple(args, "INode_requestIn", 5, 5, swig_obj)) SWIG_fail;
+  res1 = SWIG_ConvertPtr(swig_obj[0], &argp1, SWIGTYPE_p_std__vectorT_std__string_std__allocatorT_std__string_t_t,  0 );
+  if (!SWIG_IsOK(res1)) {
+    SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "INode_requestIn" "', argument " "1"" of type '" "std::vector< std::string,std::allocator< std::string > > &""'"); 
+  }
+  if (!argp1) {
+    SWIG_exception_fail(SWIG_ValueError, "invalid null reference " "in method '" "INode_requestIn" "', argument " "1"" of type '" "std::vector< std::string,std::allocator< std::string > > &""'"); 
+  }
+  arg1 = reinterpret_cast< std::vector< std::string,std::allocator< std::string > > * >(argp1);
+  ecode2 = SWIG_AsVal_double(swig_obj[1], &val2);
+  if (!SWIG_IsOK(ecode2)) {
+    SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "INode_requestIn" "', argument " "2"" of type '" "double""'");
+  } 
+  temp2 = static_cast< double >(val2);
+  arg2 = &temp2;
+  {
+    std::string *ptr = (std::string *)0;
+    res3 = SWIG_AsPtr_std_string(swig_obj[2], &ptr);
+    if (!SWIG_IsOK(res3)) {
+      SWIG_exception_fail(SWIG_ArgError(res3), "in method '" "INode_requestIn" "', argument " "3"" of type '" "std::string const &""'"); 
+    }
+    if (!ptr) {
+      SWIG_exception_fail(SWIG_ValueError, "invalid null reference " "in method '" "INode_requestIn" "', argument " "3"" of type '" "std::string const &""'"); 
+    }
+    arg3 = ptr;
+  }
+  ecode4 = SWIG_AsVal_double(swig_obj[3], &val4);
+  if (!SWIG_IsOK(ecode4)) {
+    SWIG_exception_fail(SWIG_ArgError(ecode4), "in method '" "INode_requestIn" "', argument " "4"" of type '" "double""'");
+  } 
+  arg4 = static_cast< double >(val4);
+  ecode5 = SWIG_AsVal_double(swig_obj[4], &val5);
+  if (!SWIG_IsOK(ecode5)) {
+    SWIG_exception_fail(SWIG_ArgError(ecode5), "in method '" "INode_requestIn" "', argument " "5"" of type '" "double""'");
+  } 
+  arg5 = static_cast< double >(val5);
+  INode::requestIn(*arg1,(double const &)*arg2,(std::string const &)*arg3,arg4,arg5);
+  resultobj = SWIG_Py_Void();
+  if (SWIG_IsNewObj(res3)) delete arg3;
+  return resultobj;
+fail:
+  if (SWIG_IsNewObj(res3)) delete arg3;
+  return NULL;
+}
+
+
 SWIGINTERN PyObject *_wrap_INode_jointError(PyObject *SWIGUNUSEDPARM(self), PyObject *args) {
   PyObject *resultobj = 0;
   INode *arg1 = (INode *) 0 ;
@@ -34932,6 +34997,7 @@ static PyMethodDef SwigMethods[] = {
 		""},
 	 { "INode_requestGt0", _wrap_INode_requestGt0, METH_VARARGS, "INode_requestGt0(vector_string_t errs, double const & val, std::string const & name)"},
 	 { "INode_requestGe0", _wrap_INode_requestGe0, METH_VARARGS, "INode_requestGe0(vector_string_t errs, double const & val, std::string const & name)"},
+	 { "INode_requestIn", _wrap_INode_requestIn, METH_VARARGS, "INode_requestIn(vector_string_t errs, double const & val, std::string const & name, double min, double max)"},
 	 { "INode_jointError", _wrap_INode_jointError, METH_VARARGS, "\n"
 		"INode_jointError(INode self, vector_string_t errs) -> std::string\n"
 		"std::string INode::jointError(const std::vector< std::string > errs) const\n"
-- 
GitLab