In [1]:
# jLM CME Imports
from jLM.CME import CMESimulation
from jLM.units import *
import os
In [2]:
### Define our own solver class derived from the LM solver:
class MyOwnSolver(lm.GillespieDSolver):

	def onBeginTrajectory(self):
		print(" *************************************")
		print("          H E L L O ! ! ! ! ! ! !")
		print(" *************************************")
		return 0

	def onEndTrajectory(self):
		print(" *************************************")
		print("    G O O D B Y E ! ! ! ! ! ! ! !")
		print(" *************************************")
		return 0


	# The hookSimulation method defined here will be called at every frame write
	# time.  The return value is either 0 or 1, which will indicate if we
	# changed the state or not and need the lattice to be copied back to the GPU
	# before continuing.  If you do not return 1, your changes will not be
	# reflected.
	def hookSimulation(self, time):
		print("In Hook Simulation:",time)
		speciesCounts = self.getSpeciesCountView()
		print("A: %d\tB: %d\tC: %d"%(speciesCounts[0],speciesCounts[1],speciesCounts[2]))
		if time > 0.5:
			speciesCounts[2] += 1
		return 1
# End of MyOwnSolver class.  That's it!
In [3]:
outputFile = "extendcme.lm"
sim = CMESimulation()
In [4]:
# define our chemical species
species = ['A', 'B', 'C']
sim.defineSpecies(species)
In [5]:
# Modify the cytoplasm to add diffusion rates and reactions
sim.addReaction(reactant='A', product='B', rate=0.5)

sim.addParticles(species='A', count=1000)
sim.addParticles(species='B', count=1000)
In [6]:
# Set simulation Parameters
sim.setWriteInterval(ms(100))
sim.setHookInterval(ms(250))
sim.setSimulationTime(1)
In [7]:
os.system("rm -rf %s"%(outputFile))
sim.save(outputFile)
lm.setVerbosityLevel(10)
In [8]:
# Create an instance of our local solver
solver=MyOwnSolver()
# Call the 'runSolver' method with the supplied solver
sim.runSolver(outputFile, solver=solver)
  0%|          | 0/1 [00:00<?, ?it/s]
2025-09-02 15:31:48) Debug: interactive process started.
2025-09-02 15:31:48) Info: Using 32 processor(s) and 1 CUDA device(s) per process.
2025-09-02 15:31:48) Info: Assigning 1.00 processor(s) and 1.00 CUDA device(s) per replicate.
2025-09-02 15:31:48) Debug: Started thread 3785360960.
2025-09-02 15:31:48) Info: Data output thread running.
2025-09-02 15:31:48) Debug: Data output thread looping: 0 data sets to write.
2025-09-02 15:31:48) Debug: Starting replicate 1 (cpu_id=0/cuda_id=0).
2025-09-02 15:31:48) Info: Seeding xorwow rng with top word 1 and bottom word 314596461
2025-09-02 15:31:48) Debug: Running Gillespie direct simulation with 3 species, 1 reactions, 0 species limits, and write mode 1
 *************************************
          H E L L O ! ! ! ! ! ! !
 *************************************
In Hook Simulation: 0.2507987443027733
A: 880	B: 1120	C: 0
In Hook Simulation: 0.5058771326485831
A: 766	B: 1234	C: 0
In Hook Simulation: 0.7521608272749652
A: 682	B: 1318	C: 1
 *************************************
    G O O D B Y E ! ! ! ! ! ! ! !
 *************************************
2025-09-02 15:31:48) Debug: Generated trajectory for replicate 1 in 407 steps.
2025-09-02 15:31:48) Debug: Finished with time 1.000000e+00 (1.000000e+00)
2025-09-02 15:31:48) Debug: Recording event at time 1.000000e+00 (1.000000e+00)
2025-09-02 15:31:48) Debug: Done recording events at time 1.100000e+00 (1.000000e+00)
2025-09-02 15:31:48) Debug: Data output thread looping: 2 data sets to write.
2025-09-02 15:31:48) Debug: Stopping worker threads.
2025-09-02 15:31:48) Debug: Saving data set from replicate 1 of type 10 (total 175, message 151, payload 0)
2025-09-02 15:31:48) Debug: Read species count 11 entries for 3 species with first time 0.000000
2025-09-02 15:31:48) Debug: Stopping worker thread: 3785360960
2025-09-02 15:31:48) Debug: Stopping thread 3785360960.
2025-09-02 15:31:48) Debug: Data output thread looping: 1 data sets to write.
2025-09-02 15:31:48) Debug: Saving data set from replicate 1 of type 11 (total 4102, message 4078, payload 0)
2025-09-02 15:31:48) Debug: Read 407 fpt(s) for species 0 with first count 1000 and time 0.000000
2025-09-02 15:31:48) Debug: Data output thread looping: 0 data sets to write.
2025-09-02 15:31:48) Info: Data output thread finished.
2025-09-02 15:31:48) Debug: Thread 3785360960 stopped.
2025-09-02 15:31:48) Debug: Stopped worker thread: 3785360960
2025-09-02 15:31:48) Debug: Closing file extendcme.lm, 0 open objects remaining.
2025-09-02 15:31:48) Info: Simulation file closed.
2025-09-02 15:31:48) Debug: Master process finished.
100%|##########| 1/1 [00:00<00:00,  4.00it/s]