Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a function to return the number of elapsed timesteps in Simulation class object #2337

Merged
merged 3 commits into from
Dec 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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()