@@ -121,11 +121,83 @@ def test_lambda_built_into_the_system_must_not_change(tmp_path, toy_factory, mon
121121 assert len (_create (tmp_path , {"LJSoftCore" : [1.0 , 0.5 ], "BuiltInLambda" : [1.0 , 1.0 ]}, shared_context = True ).alchemical_states ) == 2
122122
123123
124- def test_inherited_lambda_built_into_the_system_is_rejected (tmp_path , toy_factory , monkeypatch ):
125- """EMLE added as a dependency takes MLInterpolation's lambda into its calculator: it cannot change by parameter."""
126- from fes_ml .alchemical .modifications .ml_interpolation import MLInterpolationModification
124+ class _Source (BaseModification ):
125+ """λ as an OpenMM parameter; adds _Follower, which inherits its λ (like MLInterpolation and EMLE)."""
127126
128- monkeypatch .setattr (MLInterpolationModification , "post_dependencies" , [* MLInterpolationModification .post_dependencies , "EMLEPotential" ])
127+ NAME = "FakeSource"
128+ LAMBDA_PARAMETER = "lambda_fake"
129+ requires_lambda = True
130+ post_dependencies = ["FakeFollower" ]
131+
132+ def apply (self , system , alchemical_atoms , lambda_value , * args , ** kwargs ):
133+ force = mm .CustomExternalForce ("0*lambda_fake*x" )
134+ force .addGlobalParameter ("lambda_fake" , lambda_value )
135+ force .addParticle (0 , [])
136+ system .addForce (force )
137+ return system
138+
139+
140+ class _Follower (BaseModification ):
141+ """Holds λ outside OpenMM, like EMLE's calculator; with a lever unless WITH_LEVER is False."""
142+
143+ NAME = "FakeFollower"
144+ LAMBDA_FROM = "FakeSource"
145+ LAMBDA_LEVER = "lambda_follower"
146+ WITH_LEVER = True
147+ seen : list = []
148+
149+ def apply (self , system , alchemical_atoms , lambda_value = None , * args , ** kwargs ):
150+ if self .WITH_LEVER :
151+ self ._lambda_levers = {"lambda_fake" : _Follower .seen .append }
152+ return system
153+
154+
155+ @pytest .fixture
156+ def fake_pair (monkeypatch ):
157+ for name in ("FakeSource" , "FakeFollower" ):
158+ monkeypatch .setitem (Alchemist ._modification_factories , name , None )
159+ Alchemist .register_modification ("FakeSource" , _Source )
160+ Alchemist .register_modification ("FakeFollower" , _Follower )
161+ monkeypatch .setattr (_Follower , "seen" , [])
162+
163+
164+ def test_inherited_lambda_outside_openmm_follows_the_windows (tmp_path , monkeypatch , reference_platform , fake_pair ):
165+ """A modification holding its λ outside OpenMM gets each window's value through its lever (EMLE)."""
166+
167+ def create (strategy_name , alchemical_atoms , lambda_schedule , modifications_kwargs = None , ** kwargs ):
168+ system = oracle .copy_system (_toy_system (periodic = False ))
169+ alchemist = Alchemist ()
170+ alchemist .create_alchemical_graph (lambda_schedule )
171+ alchemist .apply_modifications (system , LIGAND , modifications_kwargs or {})
172+ state = toy_alchemical_state (system , reference_platform , _positions (), modifications = dict (lambda_schedule ))
173+ levers : dict = {}
174+ for modification in alchemist .modifications ():
175+ for name , lever in modification .lambda_levers .items ():
176+ levers .setdefault (name , []).append (lever )
177+ state .levers = levers or None
178+ return state
179+
180+ monkeypatch .setattr (fes_module .alchemical_factory , "create_alchemical_state" , create )
181+ states = _create (tmp_path , {"FakeSource" : [0.0 , 0.5 , 1.0 ]}, shared_context = True ).alchemical_states
182+ _Follower .seen .clear ()
183+
184+ states [2 ].activate ()
185+ assert _Follower .seen == [1.0 ] and states [2 ].context .getParameter ("lambda_fake" ) == 1.0
186+ _Follower .seen .clear ()
187+ reduced_potentials (states , _positions (), [mm .Vec3 (3 , 0 , 0 ), mm .Vec3 (0 , 3 , 0 ), mm .Vec3 (0 , 0 , 3 )] * unit .nanometer , KT )
188+ assert _Follower .seen == [0.0 , 0.5 , 1.0 , 1.0 ] # every window, then back to the active one
189+
190+
191+ def test_inherited_lambda_without_a_lever_is_rejected (tmp_path , toy_factory , monkeypatch , fake_pair ):
192+ """EMLE's Torch backend keeps λ fixed: it cannot follow the windows."""
193+ monkeypatch .setattr (_Follower , "WITH_LEVER" , False )
194+
195+ with pytest .raises (ValueError , match = r"FakeFollower:default.*no lever" ):
196+ _create (tmp_path , {"FakeSource" : [0.0 , 1.0 ]}, shared_context = True )
197+
198+
199+ def test_inherited_lambda_built_in_without_lever_support_is_rejected (tmp_path , toy_factory , monkeypatch , fake_pair ):
200+ monkeypatch .setattr (_Follower , "LAMBDA_LEVER" , None )
129201
130- with pytest .raises (ValueError , match = r"EMLEPotential :default \(λ from MLInterpolation \)" ):
131- _create (tmp_path , {"MLInterpolation " : [0.0 , 1.0 ]}, shared_context = True )
202+ with pytest .raises (ValueError , match = r"FakeFollower :default \(λ from FakeSource \)" ):
203+ _create (tmp_path , {"FakeSource " : [0.0 , 1.0 ]}, shared_context = True )
0 commit comments