@@ -452,3 +452,68 @@ def initialize(self):
452452 model .execute ("initialize" , {})
453453
454454 assert model .state [("baz" , "actual" )] == Frozen ({("foo" , "a" ): 1 , ("bar" , "b" ): 2 })
455+
456+
457+ def test_main_clock_access ():
458+ @xs .process
459+ class Foo :
460+ a = xs .variable (intent = "out" , dims = xs .MAIN_CLOCK )
461+ b = xs .variable (intent = "out" , dims = xs .MAIN_CLOCK )
462+
463+ @xs .runtime (args = ["main_clock_values" , "main_clock_dataarray" ])
464+ def initialize (self , clock_values , clock_array ):
465+ self .a = clock_values * 2
466+ np .testing .assert_equal (self .a , [0 , 2 , 4 , 6 ])
467+ self .b = clock_array * 2
468+ assert clock_array .dims [0 ] == "clock"
469+ assert all (clock_array [clock_array .dims [0 ]].data == [0 , 1 , 2 , 3 ])
470+
471+ @xs .runtime (args = ["step_delta" , "step" ])
472+ def run_step (self , dt , n ):
473+ assert self .a [n ] == 2 * n
474+ self .a [n ] += 1
475+
476+ model = xs .Model ({"foo" : Foo })
477+ ds_in = xs .create_setup (
478+ model = model ,
479+ clocks = {"clock" : range (4 )},
480+ input_vars = {},
481+ output_vars = {"foo__a" : None },
482+ )
483+ ds_out = ds_in .xsimlab .run (model = model )
484+ assert all (ds_out .foo__a .data == [1 , 3 , 5 , 6 ])
485+
486+ # test for error when another dim has the same name as xs.MAIN_CLOCK
487+ @xs .process
488+ class DoubleMainClockDim :
489+ a = xs .variable (intent = "out" , dims = ("clock" , xs .MAIN_CLOCK ))
490+
491+ def initialize (self ):
492+ self .a = [[1 , 2 , 3 ], [3 , 4 , 5 ]]
493+
494+ def run_step (self ):
495+ self .a += self .a
496+
497+ model = xs .Model ({"foo" : DoubleMainClockDim })
498+ with pytest .raises (ValueError , match = r"Main clock:*" ):
499+ xs .create_setup (
500+ model = model ,
501+ clocks = {"clock" : [0 , 1 , 2 , 3 ]},
502+ input_vars = {},
503+ output_vars = {"foo__a" : None },
504+ ).xsimlab .run (model )
505+
506+ # test for error when trying to put xs.MAIN_CLOCK as a dim in an input var
507+ with pytest .raises (
508+ ValueError , match = "Do not pass xs.MAIN_CLOCK into input vars dimensions"
509+ ):
510+ a = xs .variable (intent = "in" , dims = xs .MAIN_CLOCK )
511+
512+ with pytest .raises (
513+ ValueError , match = "Do not pass xs.MAIN_CLOCK into input vars dimensions"
514+ ):
515+ b = xs .variable (intent = "in" , dims = (xs .MAIN_CLOCK ,))
516+ with pytest .raises (
517+ ValueError , match = "Do not pass xs.MAIN_CLOCK into input vars dimensions"
518+ ):
519+ c = xs .variable (intent = "in" , dims = ["a" , ("a" , xs .MAIN_CLOCK )])
0 commit comments