load_if_needed#

FdtdSession.load_if_needed(filename: str) bool#

Load filename only if it isn’t already the active project.

Each call to fdtd.load(...) costs roughly 700 ms because Lumerical has to deserialize the entire project. Within a single gradient evaluation the same forward .fsp is typically loaded 3-5 times (in set_adj, get_jacobian, get_fwd_fields, get_adjoint_fields, …) and the adjoint .fsp 1-2 times. This helper checks whether the requested file is already loaded with the same on-disk contents (compared by absolute path + mtime) and, if so, skips the actual load.

Callers that mutate the in-memory state (switchtolayout, set, geometry edits, …) must invalidate the cache afterwards via invalidate_loaded_state() so subsequent load_if_needed calls do a real load. After fdtd.save(filename) succeeds, the in-memory state matches the file just written, so callers can call mark_as_loaded() to update the cache and avoid a round-trip on the next read.

Parameters:
filenamestr

Path to the .fsp file to load.

Returns:
bool

True if a real load happened, False if the cached state was reused.