diff --git a/doc/docs/Python_User_Interface.md.in b/doc/docs/Python_User_Interface.md.in index 157cf79ac..fe454fca4 100644 --- a/doc/docs/Python_User_Interface.md.in +++ b/doc/docs/Python_User_Interface.md.in @@ -85,6 +85,7 @@ or set the output folder, with these methods of the `Simulation` class: The `Simulation` class provides the following time-related methods: @@ Simulation.meep_time @@ +@@ Simulation.timestep @@ @@ Simulation.print_times @@ @@ Simulation.time_spent_on @@ @@ Simulation.mean_time_spent_on @@ diff --git a/python/simulation.py b/python/simulation.py index eb4894215..cc7d2fdb9 100644 --- a/python/simulation.py +++ b/python/simulation.py @@ -2429,6 +2429,13 @@ def meep_time(self): self.init_sim() return self.fields.time() + def timestep(self) -> int: + """Return the number of elapsed timesteps.""" + + if self.fields is None: + self.init_sim() + return self.fields.t + def round_time(self): if self.fields is None: self.init_sim() diff --git a/python/tests/test_simulation.py b/python/tests/test_simulation.py index a0c3b3b7a..271dc1617 100644 --- a/python/tests/test_simulation.py +++ b/python/tests/test_simulation.py @@ -828,6 +828,17 @@ def test_iterable_as_v3(self): self.assertAlmostEqual(pt1, expected) self.assertAlmostEqual(pt2, expected) + def test_time(self): + sim = self.init_simple_simulation() + + num_timesteps = 212 + sim.init_sim() + end_t = sim.fields.dt * num_timesteps + sim.run(until=end_t) + + self.assertAlmostEqual(sim.meep_time(), end_t) + self.assertEqual(sim.timestep(), num_timesteps) + if __name__ == "__main__": unittest.main()