Package: pyspimosim
A python backend for SpiMoSimhttps://github.com/pasgra/spimosimcan provide a user interface to
models written in javascript that run directly in the browser or communicate
with a server that runs the model as implemented in this repository.Example: Ising Modelmodels/ising_model.pycontains an example implementation of the Ising model
in python.www/isingcontains model specific static files that are
directly served by a webserver. The data produced the Ising model
implemented in python is send via a websocket to the browser and shown in the
browser as a "video" with "up" and "down" spins on a 2D lattice. A plot shows
the ratio of "up" and "down" spins, also called "magnetisation".How to useStart the python backend (this starts a webserver for serving files
and a websocket server to communicate with the simulation):> python3 -m pyspimosim ising-modelor> python3 models/ising_model.pyBoth do the same.Openhttp://localhost:8080/in your browser or the address printed by
the command above.Different command line flags exist for ports, etc. Run> python3 -m pyspimosim --helpfor details.Writing custom modelsThe primary purpose of this repository is not to implement the Ising model
but to provide you with everything to create your own "model". A "model"
can be a "spin model" like the Ising model or anything else that has some
settings and produces data over time that you want to show in a webbrowser.
This could be a simulation but also something completely different like a
piece of hardware.Try out the SpiMoSim creator to get a first impression what is possible in
SpiMoSim or to even write your whole model in the webbrowser. The
SpiMoSim creator is available athttp://www.pascalgrafe.net/spimosim/spimosimCreatorand also part of the
main SpiMoSim repository if you want to run it locally. The SpiMoSim creator
is currently limited to models written in Javascript, but replacing a
Javascript model with a Python one is simple:Load the SimulationBackend module 'server' by adding/uncommenting
the folling line inising/index.html:<script src="lib/modules/SimulationBackend/server.js"></script>Set the simulation backend to 'server' and set the websocket URL
by settingmodelConfig.simulation.backendinmodel-config.jsto:{
  type: "server",
  url: "ws://localhost:8090"
}Write a custom model in python: Create a file with a name ending in-model.py. The file needs to define a Model inherited frompyspimosim.base_model.BaseModelthat overwrites the methodsasync def step(self, vars_config, t)orasync def steps(self, vars_config, t, protocol, save_interval, next_send_time)andasync def change_settings(self, model_settings, restart=False).Also define the propertiesname: strmulti_step : boolsave_state_after_init : boolA dataclassModelBackendSettingsis also needed for the model.
It should inherit frompyspimosim.base_model.ModelBackendSettingsis used to define model specific command line options.pyspimosim.base_model.ModelBackendSettingscan also just be imported
and left as is.Start the server.python3 -m pyspimosimhas the optional argument--model_backend_dirto specify where you put your python model. This
is not necessary if you make model an executable python script like
the Ising model implementation. Use--www_model_rootto specify the
location of model specific files to be servered by the webserver.