From b19acccd5ada310c7e5a4f21f868e1a06079883f Mon Sep 17 00:00:00 2001
From: Jean-Baptiste Bayle <j2b.bayle@gmail.com>
Date: Thu, 31 Mar 2022 15:41:20 +0200
Subject: [PATCH] Add unit tests for frequency plan

---
 .gitattributes                  |  2 +
 tests/esa-trailing-fplan-1-1.h5 |  3 ++
 tests/keplerian-fplan-1-1.h5    |  3 ++
 tests/test_instrument.py        | 68 +++++++++++++++++++++++++++++++++
 4 files changed, 76 insertions(+)
 mode change 100644 => 100755 .gitattributes
 create mode 100755 tests/esa-trailing-fplan-1-1.h5
 create mode 100755 tests/keplerian-fplan-1-1.h5
 mode change 100644 => 100755 tests/test_instrument.py

diff --git a/.gitattributes b/.gitattributes
old mode 100644
new mode 100755
index 5ba79e1..6ace612
--- a/.gitattributes
+++ b/.gitattributes
@@ -2,3 +2,5 @@ tests/keplerian-orbits-1-0-2.h5 filter=lfs diff=lfs merge=lfs -text
 tests/esa-orbits-1-0-2.h5 filter=lfs diff=lfs merge=lfs -text
 tests/keplerian-orbits-2-0-dev.h5 filter=lfs diff=lfs merge=lfs -text
 tests/esa-trailing-orbits-2-0-dev.h5 filter=lfs diff=lfs merge=lfs -text
+tests/keplerian-fplan-1-1.h5 filter=lfs diff=lfs merge=lfs -text
+tests/esa-trailing-fplan-1-1.h5 filter=lfs diff=lfs merge=lfs -text
diff --git a/tests/esa-trailing-fplan-1-1.h5 b/tests/esa-trailing-fplan-1-1.h5
new file mode 100755
index 0000000..1ed838a
--- /dev/null
+++ b/tests/esa-trailing-fplan-1-1.h5
@@ -0,0 +1,3 @@
+version https://git-lfs.github.com/spec/v1
+oid sha256:91cdf77a8b830999d7409d35e2cb5d44870eeaba9a86d516b5c6e8f87e75fa7a
+size 22160440
diff --git a/tests/keplerian-fplan-1-1.h5 b/tests/keplerian-fplan-1-1.h5
new file mode 100755
index 0000000..895e915
--- /dev/null
+++ b/tests/keplerian-fplan-1-1.h5
@@ -0,0 +1,3 @@
+version https://git-lfs.github.com/spec/v1
+oid sha256:f42b997aef2f2820582aaf35e102c16e59efc0d93f6379fa3355f4821ef810e2
+size 20610648
diff --git a/tests/test_instrument.py b/tests/test_instrument.py
old mode 100644
new mode 100755
index 54adb73..ece342a
--- a/tests/test_instrument.py
+++ b/tests/test_instrument.py
@@ -2,6 +2,7 @@
 # -*- coding: utf-8 -*-
 # pylint: disable=missing-module-docstring
 
+import numpy as np
 import pytest
 from lisainstrument import Instrument
 
@@ -48,6 +49,73 @@ def test_esa_trailing_orbits_2_0_dev():
     instru = Instrument(size=100, orbits='tests/esa-trailing-orbits-2-0-dev.h5')
     instru.simulate()
 
+def test_custom_fplan():
+    """Test that we can pass a custom frequency plan."""
+    # Constant equal fplan
+    instru = Instrument(size=100, fplan=42.0)
+    for mosa in instru.MOSAS:
+        assert instru.fplan[mosa] == 42.0
+    instru.simulate()
+    # Constant unequal fplan
+    fplan = {
+        '12': 8.1E6, '23': 9.2E6, '31': 10.3E6,
+        '13': 1.4E6, '32': -11.6E6, '21': -9.5E6,
+    }
+    instru = Instrument(size=100, fplan=fplan)
+    for mosa in instru.MOSAS:
+        assert instru.fplan[mosa] == fplan[mosa]
+    instru.simulate()
+    # Time-varying equal fplan
+    fplan = np.random.normal(size=(instru.physics_size))
+    instru = Instrument(size=100, fplan=fplan)
+    for mosa in instru.MOSAS:
+        assert instru.fplan[mosa] == pytest.approx(fplan)
+    instru.simulate()
+    # Time-varying unequal fplan
+    fplan = {mosa: np.random.normal(size=(instru.physics_size)) for mosa in instru.MOSAS}
+    instru = Instrument(size=100, fplan=fplan)
+    for mosa in instru.MOSAS:
+        assert instru.fplan[mosa] == pytest.approx(fplan[mosa])
+    instru.simulate()
+
+def test_keplerian_fplan_1_1():
+    """Test that simulations can run with Keplerian fplan v1.1."""
+    # Check with six lasers locked on cavity
+    instru = Instrument(size=100, lock='six', fplan='tests/keplerian-fplan-1-1.h5')
+    for mosa in instru.MOSAS:
+        assert instru.fplan[mosa] == 0.0
+    instru.simulate()
+    # Check standard lock configs
+    for topology in Instrument.LOCK_TOPOLOGIES:
+        for primary in Instrument.MOSAS:
+            instru = Instrument(
+                size=100, lock=f'{topology}-{primary}', fplan='tests/keplerian-fplan-1-1.h5')
+            assert instru.fplan[primary] == 0.0
+            instru.simulate()
+    # Should raise an error for non-standard lock config
+    with pytest.raises(ValueError):
+        lock = {'12': 'cavity', '13': 'cavity', '21': 'distant', '31': 'distant', '23': 'adjacent', '32': 'adjacent'}
+        Instrument(size=100, lock=lock, fplan='tests/keplerian-fplan-1-1.h5')
+
+def test_esa_trailing_fplan_1_1():
+    """Test that simulations can run with ESA trailing fplan v1.1."""
+    # Check with six lasers locked on cavity
+    instru = Instrument(size=100, lock='six', fplan='tests/esa-trailing-fplan-1-1.h5')
+    for mosa in instru.MOSAS:
+        assert instru.fplan[mosa] == 0.0
+    instru.simulate()
+    # Check standard lock configs
+    for topology in Instrument.LOCK_TOPOLOGIES:
+        for primary in Instrument.MOSAS:
+            instru = Instrument(
+                size=100, lock=f'{topology}-{primary}', fplan='tests/esa-trailing-fplan-1-1.h5')
+            assert instru.fplan[primary] == 0.0
+            instru.simulate()
+    # Should raise an error for non-standard lock config
+    with pytest.raises(ValueError):
+        lock = {'12': 'cavity', '13': 'cavity', '21': 'distant', '31': 'distant', '23': 'adjacent', '32': 'adjacent'}
+        Instrument(size=100, lock=lock, fplan='tests/esa-trailing-fplan-1-1.h5')
+
 def test_locking():
     """Test that simulations can run with various lock configurations."""
     # Test six free-running lasers
-- 
GitLab