def run(): """ :return: """ persistent_globals = CompuCellSetup.persistent_globals simulation_initialized = persistent_globals.simulation_initialized if not simulation_initialized: initialize_cc3d() check_for_cpp_errors(CompuCellSetup.persistent_globals.simulator) persistent_globals.steppable_registry.core_init() check_for_cpp_errors(CompuCellSetup.persistent_globals.simulator) # initializing extra visualization fields field_registry = persistent_globals.field_registry potts = persistent_globals.simulator.getPotts() cell_field = potts.getCellFieldG() dim = cell_field.getDim() field_registry.dim = dim field_registry.simthread = persistent_globals.simthread field_registry.create_fields() simulator = CompuCellSetup.persistent_globals.simulator simthread = CompuCellSetup.persistent_globals.simthread steppable_registry = CompuCellSetup.persistent_globals.steppable_registry main_loop_fcn = determine_main_loop_fcn() main_loop_fcn(simulator, simthread=simthread, steppable_registry=steppable_registry)
def initialize_cc3d(): """ :return: """ CompuCellSetup.persistent_globals.simulator, \ CompuCellSetup.persistent_globals.simthread = get_core_simulation_objects() check_for_cpp_errors(CompuCellSetup.persistent_globals.simulator) simulator = CompuCellSetup.persistent_globals.simulator # CompuCellSetup.persistent_globals.steppable_registry.simulator = simulator CompuCellSetup.persistent_globals.steppable_registry.simulator = weakref.ref( simulator) CompuCellSetup.persistent_globals.simulation_initialized = True
def main_loop_player(sim, simthread=None, steppable_registry=None): """ main loop for GUI based simulations :param sim: :param simthread: :param steppable_registry: :return: """ t1 = time.time() compiled_code_run_time = 0.0 pg = CompuCellSetup.persistent_globals steppable_registry = pg.steppable_registry simthread = pg.simthread pg.restart_manager = RestartManager.RestartManager(sim) restart_manager = pg.restart_manager restart_manager.output_frequency = pg.restart_snapshot_frequency restart_manager.allow_multiple_restart_directories = pg.restart_multiple_snapshots init_using_restart_snapshot_enabled = restart_manager.restart_enabled() # init_using_restart_snapshot_enabled = False sim.setRestartEnabled(init_using_restart_snapshot_enabled) check_nanohub_and_count() if init_using_restart_snapshot_enabled: print('WILL RESTART SIMULATION') restart_manager.loadRestartFiles() check_for_cpp_errors(CompuCellSetup.persistent_globals.simulator) else: print('WILL RUN SIMULATION FROM BEGINNING') extra_init_simulation_objects( sim, simthread, init_using_restart_snapshot_enabled=init_using_restart_snapshot_enabled ) check_for_cpp_errors(CompuCellSetup.persistent_globals.simulator) # simthread.waitForInitCompletion() # simthread.waitForPlayerTaskToFinish() if not steppable_registry is None: steppable_registry.init(sim) # called in extraInitSimulationObjects # sim.start() if not steppable_registry is None and not init_using_restart_snapshot_enabled: steppable_registry.start() simthread.steppablePostStartPrep() run_finish_flag = True restart_manager.prepare_restarter() beginning_step = restart_manager.get_restart_step() if init_using_restart_snapshot_enabled: steppable_registry.restart_steering_panel() cur_step = beginning_step # initial drawing - we use mcs = -2 as a sentinel for mcs right after start function of steppables is completed simthread.loopWork(-2) simthread.loopWorkPostEvent(-2) while cur_step < sim.getNumSteps(): simthread.beforeStep(_mcs=cur_step) if simthread.getStopSimulation( ) or CompuCellSetup.persistent_globals.user_stop_simulation_flag: run_finish_flag = False break if steppable_registry is not None: steppable_registry.stepRunBeforeMCSSteppables(cur_step) compiled_code_begin = time.time() sim.step(cur_step) # steering using steppables check_for_cpp_errors(CompuCellSetup.persistent_globals.simulator) compiled_code_end = time.time() compiled_code_run_time += (compiled_code_end - compiled_code_begin) * 1000 # steering using GUI. GUI steering overrides steering done in the steppables simthread.steerUsingGUI(sim) if not steppable_registry is None: steppable_registry.step(cur_step) # restart manager will decide whether to output files or not based on its settings restart_manager.output_restart_files(cur_step) # passing Python-script-made changes in XML to C++ code incorporate_script_steering_changes(simulator=sim) # steer application will only update modules that uses requested using updateCC3DModule function from simulator sim.steer() check_for_cpp_errors(CompuCellSetup.persistent_globals.simulator) screen_update_frequency = simthread.getScreenUpdateFrequency() screenshot_frequency = simthread.getScreenshotFrequency() screenshot_output_flag = simthread.getImageOutputFlag() if pg.screenshot_manager is not None and pg.screenshot_manager.has_ad_hoc_screenshots( ): simthread.loopWork(cur_step) simthread.loopWorkPostEvent(cur_step) elif (screen_update_frequency > 0 and cur_step % screen_update_frequency == 0) or (screenshot_output_flag and screenshot_frequency > 0 and cur_step % screenshot_frequency == 0): simthread.loopWork(cur_step) simthread.loopWorkPostEvent(cur_step) cur_step += 1 if run_finish_flag: # # we emit request to finish simulation simthread.emitFinishRequest() # # then we wait for GUI thread to unlock the finishMutex - it will only happen when all tasks # in the GUI thread are completed (especially those that need simulator object to stay alive) print("CALLING FINISH") simthread.waitForFinishingTasksToConclude() simthread.waitForPlayerTaskToFinish() steppable_registry.finish() sim.cleanAfterSimulation() simthread.simulationFinishedPostEvent(True) steppable_registry.clean_after_simulation() else: steppable_registry.on_stop() sim.cleanAfterSimulation() # # sim.unloadModules() print("CALLING UNLOAD MODULES NEW PLAYER") if simthread is not None: simthread.sendStopSimulationRequest() simthread.simulationFinishedPostEvent(True) steppable_registry.clean_after_simulation() t2 = time.time() print_profiling_report( py_steppable_profiler_report=steppable_registry.get_profiler_report(), compiled_code_run_time=compiled_code_run_time, total_run_time=(t2 - t1) * 1000.0)
def main_loop(sim, simthread, steppable_registry=None): """ main loop for CML simulation :param sim: :param simthread: :param steppable_registry: :return: """ t1 = time.time() compiled_code_run_time = 0.0 pg = CompuCellSetup.persistent_globals steppable_registry = pg.steppable_registry pg.restart_manager = RestartManager.RestartManager(sim) restart_manager = pg.restart_manager restart_manager.output_frequency = pg.restart_snapshot_frequency restart_manager.allow_multiple_restart_directories = pg.restart_multiple_snapshots init_using_restart_snapshot_enabled = restart_manager.restart_enabled() # init_using_restart_snapshot_enabled = False sim.setRestartEnabled(init_using_restart_snapshot_enabled) check_for_cpp_errors(CompuCellSetup.persistent_globals.simulator) check_nanohub_and_count() if init_using_restart_snapshot_enabled: print('WILL RESTART SIMULATION') restart_manager.loadRestartFiles() check_for_cpp_errors(CompuCellSetup.persistent_globals.simulator) else: print('WILL RUN SIMULATION FROM BEGINNING') extra_init_simulation_objects( sim, simthread, init_using_restart_snapshot_enabled=init_using_restart_snapshot_enabled ) check_for_cpp_errors(CompuCellSetup.persistent_globals.simulator) if steppable_registry is not None: steppable_registry.init(sim) init_lattice_snapshot_objects() init_screenshot_manager() if steppable_registry is not None and not init_using_restart_snapshot_enabled: steppable_registry.start() run_finish_flag = True restart_manager.prepare_restarter() beginning_step = restart_manager.get_restart_step() cur_step = beginning_step while cur_step < sim.getNumSteps(): if CompuCellSetup.persistent_globals.user_stop_simulation_flag: run_finish_flag = False break if steppable_registry is not None: steppable_registry.stepRunBeforeMCSSteppables(cur_step) compiled_code_begin = time.time() sim.step(cur_step) # steering using steppables check_for_cpp_errors(CompuCellSetup.persistent_globals.simulator) compiled_code_end = time.time() compiled_code_run_time += (compiled_code_end - compiled_code_begin) * 1000 if steppable_registry is not None: steppable_registry.step(cur_step) # restart manager will decide whether to output files or not based on its settings restart_manager.output_restart_files(cur_step) store_lattice_snapshot(cur_step=cur_step) store_screenshots(cur_step=cur_step) # passing Python-script-made changes in XML to C++ code incorporate_script_steering_changes(simulator=sim) # steer application will only update modules that uses requested using updateCC3DModule function from simulator sim.steer() check_for_cpp_errors(CompuCellSetup.persistent_globals.simulator) cur_step += 1 if run_finish_flag: print("CALLING FINISH") steppable_registry.finish() else: steppable_registry.on_stop() t2 = time.time() print_profiling_report( py_steppable_profiler_report=steppable_registry.get_profiler_report(), compiled_code_run_time=compiled_code_run_time, total_run_time=(t2 - t1) * 1000.0)