Skip to content

Commit

Permalink
load scene when both client and webpage are ready
Browse files Browse the repository at this point in the history
  • Loading branch information
PythonFZ committed Oct 4, 2023
1 parent b95df17 commit 0574d08
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 22 deletions.
105 changes: 105 additions & 0 deletions examples/md.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Molecular Dynamics Simulation\n",
"\n",
"In this example we will run an MD simulation using ASE and visualise it using ZnDraw."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from zndraw import ZnDraw\n",
"\n",
"from ase.lattice.cubic import FaceCenteredCubic\n",
"from ase.md.velocitydistribution import MaxwellBoltzmannDistribution\n",
"from ase.md.verlet import VelocityVerlet\n",
"from ase import units\n",
"\n",
"from ase.calculators.emt import EMT\n",
"size = 2\n",
"\n",
"# Set up a crystal\n",
"atoms = FaceCenteredCubic(directions=[[1, 0, 0], [0, 1, 0], [0, 0, 1]],\n",
" symbol=\"Cu\",\n",
" size=(size, size, size),\n",
" pbc=True)\n",
"\n",
"# Describe the interatomic interactions with the Effective Medium Theory\n",
"atoms.calc = EMT()\n",
"\n",
"# Set the momenta corresponding to T=300K\n",
"MaxwellBoltzmannDistribution(atoms, temperature_K=300)\n",
"\n",
"# We want to run MD with constant energy using the VelocityVerlet algorithm.\n",
"dyn = VelocityVerlet(atoms, 5 * units.fs) # 5 fs time step.\n",
"\n",
"\n",
"def printenergy(a=atoms): # store a reference to atoms in the definition.\n",
" \"\"\"Function to print the potential, kinetic and total energy.\"\"\"\n",
" epot = a.get_potential_energy() / len(a)\n",
" ekin = a.get_kinetic_energy() / len(a)\n",
" print('Energy per atom: Epot = %.3feV Ekin = %.3feV (T=%3.0fK) '\n",
" 'Etot = %.3feV' % (epot, ekin, ekin / (1.5 * units.kB), epot + ekin))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"vis = ZnDraw(url=\"http://localhost:1234\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Now run the dynamics\n",
"\n",
"# dyn.attach(printenergy, interval=1)\n",
"dyn.attach(lambda: vis.append(atoms), interval=1)\n",
"printenergy()\n",
"dyn.run(200)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "zndraw",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.11"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}
9 changes: 3 additions & 6 deletions zndraw/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,12 @@ def on_join(data):
"""Join a room."""
try:
join_room(session["uuid"])
print(f"Client for webpage joined room {session['uuid']}")
except KeyError:
session["uuid"] = data["uuid"]
join_room(session["uuid"])
print(f"Client from 'zndraw.ZnDraw' joined room {session['uuid']}")
emit("join", {}, include_self=False, to=session["uuid"])


@app.route("/exit")
Expand Down Expand Up @@ -186,12 +189,6 @@ class Scene(BaseModel):
return schema


@io.on("atoms:request")
def atoms_request(url):
"""Return the atoms."""
emit("atoms:request", url, include_self=False, to=session["uuid"])


@io.on("modifier:run")
def modifier_run(data):
emit("modifier:run", data, include_self=False, to=session["uuid"])
Expand Down
16 changes: 4 additions & 12 deletions zndraw/static/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,15 @@ function main() {

// creata a function displayIncomingAtoms that calls cache.get_length(), if larger 1 call world.setStep(0), else setTimerout(displayIncomingAtoms, 1000)

const displayIncomingAtoms = () => {
cache.get_length();
if (cache.get_length() > 0) {
world.setStep(0);
document.getElementById("atom-spinner").style.display = "none";
} else {
setTimeout(displayIncomingAtoms, 100);
}
};

socket.emit("atoms:request", window.location.href, () => {
displayIncomingAtoms();
socket.on("join", () => {
socket.emit("join", {}); // we send an ACK to the server, so that it can send us the atoms
});

socket.on("message:log", (msg) => {
console.log(msg);
});

document.getElementById("atom-spinner").style.display = "none";
}

main();
8 changes: 4 additions & 4 deletions zndraw/zndraw.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def on_answer(data):

@dataclasses.dataclass
class FileIO:
name: str
name: str = None
start: int = 0
stop: int = None
step: int = 1
Expand All @@ -59,7 +59,7 @@ class ZnDraw(collections.abc.MutableSequence):
bonds_calculator: ASEComputeBonds = dataclasses.field(
default_factory=ASEComputeBonds
)
file: FileIO = None
file: FileIO = dataclasses.field(default_factory=FileIO)
wait: bool = False
token: str = "default"

Expand Down Expand Up @@ -99,8 +99,8 @@ def __post_init__(self):
)

self.socket.on(
"atoms:request",
lambda url: self.read(
"join",
lambda *args: self.read(
self.file.name, self.file.start, self.file.stop, self.file.step
),
)
Expand Down

0 comments on commit 0574d08

Please sign in to comment.