Skip to content

Commit

Permalink
ZnDraw visualizer (#4976)
Browse files Browse the repository at this point in the history
Fixes #4961

Description of changes:
* ZnDraw incorporated in charged system, polymers, and Lennard-Jones tutorials
  • Loading branch information
kodiakhq[bot] authored Aug 30, 2024
2 parents 8db11b0 + 1c64748 commit 1e3d4fb
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 37 deletions.
80 changes: 56 additions & 24 deletions doc/tutorials/charged_system/charged_system.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
"import espressomd.observables\n",
"import espressomd.accumulators\n",
"import espressomd.math\n",
"import espressomd.zn\n",
"\n",
"\n",
"espressomd.assert_features(['ELECTROSTATICS', 'P3M', 'WCA'])\n",
"\n",
Expand Down Expand Up @@ -543,46 +545,49 @@
},
{
"cell_type": "markdown",
"id": "459fd605",
"id": "1ecbb2d5-e5e0-467b-acc2-45173dab3bc6",
"metadata": {},
"source": [
"## Production run and analysis\n",
"Now we are finally ready to run the simulations and produce the data we can compare to the Poisson-Boltzmann predictions. First we define the parameters and then loop over them."
"## Sytem Visualization"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9049f459",
"id": "831c8a39-68e0-4ed5-85a4-f1fa7265f3e2",
"metadata": {},
"outputs": [],
"source": [
"runs = [{'params': {'counterion_valency': 2, 'rod_charge_dens': 1},\n",
" 'histogram': None},\n",
" {'params': {'counterion_valency': 1, 'rod_charge_dens': 2},\n",
" 'histogram': None}\n",
" ]\n",
"N_SAMPLES = 1500"
"# Visualizer Parameters\n",
"color = {ROD_TYPE: \"#0911e8\", COUNTERION_TYPE: \"#f70519\"} # Particle color by type\n",
"radii = {ROD_TYPE: ROD_RADIUS, COUNTERION_TYPE: ION_DIAMETER/2.0} # Particle size by type\n",
"print(\"System visualization for case (a) counterion valency: 2, rod_charge_density: 1\")\n",
"# Initializing Visualizer\n",
"vis = espressomd.zn.Visualizer(system, colors=color, radii=radii, bonds=True)"
]
},
{
"cell_type": "markdown",
"id": "be2b297f",
"id": "459fd605",
"metadata": {},
"source": [
"For longer simulation runs it will be convenient to have a progress bar"
"## Production run and analysis\n",
"Now we are finally ready to run the simulations and produce the data we can compare to the Poisson-Boltzmann predictions. First we define the parameters and then loop over them."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4b873263",
"id": "9049f459",
"metadata": {},
"outputs": [],
"source": [
"def integrate_system(system, n_steps):\n",
" for i in tqdm.trange(100):\n",
" system.integrator.run(n_steps // 100)\n"
"runs = [{'params': {'counterion_valency': 2, 'rod_charge_dens': 1},\n",
" 'histogram': None},\n",
" {'params': {'counterion_valency': 1, 'rod_charge_dens': 2},\n",
" 'histogram': None}\n",
" ]\n",
"N_SAMPLES = 1500"
]
},
{
Expand Down Expand Up @@ -617,10 +622,21 @@
" remove_overlap(system)\n",
" system.thermostat.set_langevin(**LANGEVIN_PARAMS)\n",
" print('', end='', flush=True)\n",
" integrate_system(system, WARMUP_STEPS)\n",
"\n",
" # For longer simulation runs it will be convenient to have a progress bar\n",
" for i in tqdm.trange(100):\n",
" system.integrator.run(WARMUP_STEPS // 100)\n",
" \n",
" radial_profile_accs, bin_edges = setup_profile_calculation(\n",
" system, STEPS_PER_SAMPLE, [COUNTERION_TYPE], r_min, N_RADIAL_BINS)\n",
" integrate_system(system, N_SAMPLES * STEPS_PER_SAMPLE)\n",
" for i in tqdm.trange(100):\n",
" system.integrator.run(N_SAMPLES * STEPS_PER_SAMPLE // 100)\n",
"\n",
" # Updating frames for the visualizer\n",
" if(run['params']['counterion_valency']==2):\n",
" vis.update()\n",
" \n",
" \n",
"\n",
" run['histogram'] = radial_profile_accs[COUNTERION_TYPE].mean()\n",
" print(f'simulation for parameters {run[\"params\"]} done\\n')"
Expand Down Expand Up @@ -836,10 +852,12 @@
{
"cell_type": "code",
"execution_count": null,
"id": "953f34fd",
"id": "b59e6290-f535-42b1-92b6-78b9597963b3",
"metadata": {},
"outputs": [],
"source": [
"# Resetting system with salt ions\n",
"\n",
"clear_system(system)\n",
"system.box_l = 3 * [ROD_LENGTH]\n",
"counterions = setup_rod_and_counterions(\n",
Expand All @@ -852,11 +870,25 @@
"system.electrostatics.solver = p3m\n",
"remove_overlap(system)\n",
"system.thermostat.set_langevin(**LANGEVIN_PARAMS)\n",
"print('', end='', flush=True)\n",
"integrate_system(system, WARMUP_STEPS)\n",
"print('', end='', flush=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "953f34fd",
"metadata": {},
"outputs": [],
"source": [
"\n",
"for i in tqdm.trange(100):\n",
" system.integrator.run(WARMUP_STEPS // 100)\n",
"radial_profile_accs, bin_edges = setup_profile_calculation(\n",
" system, STEPS_PER_SAMPLE_SALT, all_ion_types, r_min, N_RADIAL_BINS)\n",
"integrate_system(system, N_SAMPLES_SALT * STEPS_PER_SAMPLE_SALT)"
"\n",
"for i in tqdm.trange(100):\n",
" system.integrator.run(N_SAMPLES_SALT * STEPS_PER_SAMPLE_SALT // 100)\n",
" \n"
]
},
{
Expand Down Expand Up @@ -939,7 +971,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
Expand All @@ -953,7 +985,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.10"
"version": "3.10.12"
}
},
"nbformat": 4,
Expand Down
24 changes: 21 additions & 3 deletions doc/tutorials/lennard_jones/lennard_jones.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,8 @@
"import espressomd.observables\n",
"import espressomd.accumulators\n",
"import espressomd.analyze\n",
"import espressomd.zn \n",
"\n",
"required_features = [\"LENNARD_JONES\"]\n",
"espressomd.assert_features(required_features)"
]
Expand Down Expand Up @@ -632,13 +634,28 @@
"system.thermostat.set_langevin(kT=TEMPERATURE, gamma=GAMMA, seed=42)"
]
},
{
"cell_type": "markdown",
"id": "10510b16-bc19-4c16-a881-a3b88cc298f6",
"metadata": {},
"source": [
"### System visualization "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "42c573df",
"metadata": {},
"outputs": [],
"source": []
"source": [
"# Visualizer Parameters\n",
"color = {0: \"#00f0f0\"} # Particle color by type\n",
"radii = {0: LJ_SIG/2.0} # Particle size by type\n",
"\n",
"# Initializing Visualizer\n",
"vis = espressomd.zn.Visualizer(system, colors=color, radii=radii)"
]
},
{
"cell_type": "markdown",
Expand Down Expand Up @@ -696,6 +713,7 @@
" e_total[i] = energy['total']\n",
" e_kin[i] = energy['kinetic']\n",
" system.integrator.run(STEPS_PER_SAMPLE)\n",
" vis.update()\n",
"T_inst = 2. / 3. * e_kin / N_PART"
]
},
Expand Down Expand Up @@ -1235,7 +1253,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
Expand All @@ -1249,7 +1267,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.10"
"version": "3.10.12"
}
},
"nbformat": 4,
Expand Down
74 changes: 64 additions & 10 deletions doc/tutorials/polymers/polymers.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "dd4005bb",
"id": "c9366d00-4982-4ed9-a824-709b5774fba0",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -337,11 +337,20 @@
"import espressomd.accumulators\n",
"import espressomd.observables\n",
"import espressomd.polymer\n",
"import espressomd.zn \n",
"\n",
"logging.basicConfig(level=logging.INFO, stream=sys.stdout)\n",
"\n",
"espressomd.assert_features(['LENNARD_JONES'])\n",
"\n",
"espressomd.assert_features(['LENNARD_JONES'])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d24096b4-c4af-49b7-bd25-6754a2bac9bd",
"metadata": {},
"outputs": [],
"source": [
"# Setup constants\n",
"BOX_L = 12.0\n",
"TIME_STEP = 0.01\n",
Expand Down Expand Up @@ -371,8 +380,16 @@
"\n",
"# Fene interaction\n",
"fene = espressomd.interactions.FeneBond(k=7, r_0=1, d_r_max=2)\n",
"system.bonded_inter.add(fene)\n",
"\n",
"system.bonded_inter.add(fene)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d86b9ad0-58f0-47b9-9b58-d5c7dac665e7",
"metadata": {},
"outputs": [],
"source": [
"N_MONOMERS = np.array([6, 8, 10])\n",
"\n",
"com_pos_tau_results = []\n",
Expand All @@ -381,7 +398,43 @@
"com_vel_acf_results = []\n",
"rh_results = []\n",
"rf_results = []\n",
"rg_results = []\n",
"rg_results = []"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cc807f20-5f3c-412a-a06d-6816256e6ee6",
"metadata": {},
"outputs": [],
"source": [
"### System visualization "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "142bf210-0273-4b4c-a6fe-641da9d43856",
"metadata": {},
"outputs": [],
"source": [
"print(\"Simulation video for polymer of size :\", N_MONOMERS[2])\n",
"\n",
"# Visualizer Parameters\n",
"color = {0: \"blue\"} # Particle color by type\n",
"radii = {0: LJ_SIGMA/2.0} # Particle size by type\n",
"\n",
"# Initializing Visualizer\n",
"vis = espressomd.zn.Visualizer(system, colors=color, radii=radii, bonds=True)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "dd4005bb",
"metadata": {},
"outputs": [],
"source": [
"for N in N_MONOMERS:\n",
" logging.info(f\"Polymer size: {N}\")\n",
" build_polymer(system, N, POLYMER_PARAMS, fene)\n",
Expand All @@ -405,7 +458,6 @@
" logging.info(\"Equilibration finished.\")\n",
"\n",
" system.thermostat.turn_off()\n",
"\n",
" if POLYMER_MODEL == 'Rouse':\n",
" solvent_langevin(system, KT, GAMMA)\n",
" elif POLYMER_MODEL == 'Zimm':\n",
Expand All @@ -422,13 +474,15 @@
" # configure Green-Kubo correlator\n",
" com_vel_cor = correlator_gk(np.arange(N), LOOPS * STEPS)\n",
" system.auto_update_accumulators.add(com_vel_cor)\n",
"\n",
" \n",
" logging.info(\"Sampling started.\")\n",
" rhs = np.zeros(LOOPS)\n",
" rfs = np.zeros(LOOPS)\n",
" rgs = np.zeros(LOOPS)\n",
" for i in tqdm.trange(LOOPS):\n",
" system.integrator.run(STEPS)\n",
" if N == N_MONOMERS[2]:\n",
" vis.update()\n",
" rhs[i] = system.analysis.calc_rh(\n",
" chain_start=0,\n",
" number_of_chains=1,\n",
Expand Down Expand Up @@ -969,7 +1023,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
Expand All @@ -983,7 +1037,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.5"
"version": "3.10.12"
}
},
"nbformat": 4,
Expand Down

0 comments on commit 1e3d4fb

Please sign in to comment.