Repository: DCC-Lab/RayTracing

Issue:
GaussianBeam: q or w should be specified (not both None)
This can lead to some incoherence: we should check (if q and w are given) that q computed with w is relatively close to q given.

Also, we should not be able to set both as `None`. Having w and q has `None` does nothing good, only lead to issues with methods, because we take for granted that q is a complex number. It doesn't make sense to have a `GaussianBeam` where q and w are `None`.

Discussion:

Patch:
diff --git a/raytracing/gaussianbeam.py b/raytracing/gaussianbeam.py
index 77dbfab..3cfc6b9 100644
--- a/raytracing/gaussianbeam.py
+++ b/raytracing/gaussianbeam.py
@@ -12,12 +12,17 @@ class GaussianBeam(object):
 
     def __init__(self, q:complex=None, w:float=None, R:float=float("+Inf"), n:float=1.0, wavelength=632.8e-6, z=0):
         # Gaussian beam matrix formalism
+
         if q is not None:
             self.q = q
-        elif w is not None:
+        if w is not None:
             self.q = 1/( 1.0/R - complex(0,1)*wavelength/n/(math.pi*w*w))
-        else:
-            self.q = None 
+        if q is None and w is None:
+            raise ValueError("Please specify 'q' or 'w'.")
+
+        if q is not None and w is not None:
+            if not cmath.isclose(a=self.q, b=q, abs_tol=0.1):
+                raise ValueError("Mismatch between the given q and the computed q (10% tolerance).")
 
         self.wavelength = wavelength
         


Test patch:
diff --git a/raytracing/tests/testsGaussian.py b/raytracing/tests/testsGaussian.py
index 8c26cff..be32f22 100644
--- a/raytracing/tests/testsGaussian.py
+++ b/raytracing/tests/testsGaussian.py
@@ -6,7 +6,6 @@ inf = float("+inf")
 
 class TestBeam(unittest.TestCase):
     def testBeam(self):
-        beam = GaussianBeam()
         beam = GaussianBeam(w=1)
         self.assertEqual(beam.w, 1)
         self.assertEqual(beam.R, float("+Inf"))
@@ -19,10 +18,13 @@ class TestBeam(unittest.TestCase):
 
     def testInvalidParameters(self):
         with self.assertRaises(Exception) as context:
-	        beam = GaussianBeam(w=1,R=0)
+            beam = GaussianBeam()
+
+        with self.assertRaises(Exception) as context:
+            beam = GaussianBeam(w=1,R=0)
 
     def testMultiplicationBeam(self):
-    	# No default parameters
+        # No default parameters
         beamIn = GaussianBeam(w=0.01, R=1, n=1.5, wavelength=0.400e-3)
         beamOut = Space(d=0,n=1.5)*beamIn
         self.assertEqual(beamOut.q, beamIn.q)