Skip to content

Commit

Permalink
Add a function to return the number of elapsed timesteps in `Simulati…
Browse files Browse the repository at this point in the history
…on` class object (#2337)

* add a function to return the number of elapsed timesteps in Simulation

* specify runtime based on number of timesteps and sim.fields.dt

* Update doc/docs/Python_User_Interface.md.in

Co-authored-by: Steven G. Johnson <[email protected]>
  • Loading branch information
oskooi and stevengj authored Dec 15, 2022
1 parent dd0e8c9 commit 6e44cb8
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
1 change: 1 addition & 0 deletions doc/docs/Python_User_Interface.md.in
Original file line number Diff line number Diff line change
Expand Up @@ -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 @@
Expand Down
7 changes: 7 additions & 0 deletions python/simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
11 changes: 11 additions & 0 deletions python/tests/test_simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

0 comments on commit 6e44cb8

Please sign in to comment.