Skip to content

Commit 1e3d4fb

Browse files
ZnDraw visualizer (#4976)
Fixes #4961 Description of changes: * ZnDraw incorporated in charged system, polymers, and Lennard-Jones tutorials
2 parents 8db11b0 + 1c64748 commit 1e3d4fb

File tree

3 files changed

+141
-37
lines changed

3 files changed

+141
-37
lines changed

doc/tutorials/charged_system/charged_system.ipynb

+56-24
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
"import espressomd.observables\n",
4242
"import espressomd.accumulators\n",
4343
"import espressomd.math\n",
44+
"import espressomd.zn\n",
45+
"\n",
4446
"\n",
4547
"espressomd.assert_features(['ELECTROSTATICS', 'P3M', 'WCA'])\n",
4648
"\n",
@@ -543,46 +545,49 @@
543545
},
544546
{
545547
"cell_type": "markdown",
546-
"id": "459fd605",
548+
"id": "1ecbb2d5-e5e0-467b-acc2-45173dab3bc6",
547549
"metadata": {},
548550
"source": [
549-
"## Production run and analysis\n",
550-
"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."
551+
"## Sytem Visualization"
551552
]
552553
},
553554
{
554555
"cell_type": "code",
555556
"execution_count": null,
556-
"id": "9049f459",
557+
"id": "831c8a39-68e0-4ed5-85a4-f1fa7265f3e2",
557558
"metadata": {},
558559
"outputs": [],
559560
"source": [
560-
"runs = [{'params': {'counterion_valency': 2, 'rod_charge_dens': 1},\n",
561-
" 'histogram': None},\n",
562-
" {'params': {'counterion_valency': 1, 'rod_charge_dens': 2},\n",
563-
" 'histogram': None}\n",
564-
" ]\n",
565-
"N_SAMPLES = 1500"
561+
"# Visualizer Parameters\n",
562+
"color = {ROD_TYPE: \"#0911e8\", COUNTERION_TYPE: \"#f70519\"} # Particle color by type\n",
563+
"radii = {ROD_TYPE: ROD_RADIUS, COUNTERION_TYPE: ION_DIAMETER/2.0} # Particle size by type\n",
564+
"print(\"System visualization for case (a) counterion valency: 2, rod_charge_density: 1\")\n",
565+
"# Initializing Visualizer\n",
566+
"vis = espressomd.zn.Visualizer(system, colors=color, radii=radii, bonds=True)"
566567
]
567568
},
568569
{
569570
"cell_type": "markdown",
570-
"id": "be2b297f",
571+
"id": "459fd605",
571572
"metadata": {},
572573
"source": [
573-
"For longer simulation runs it will be convenient to have a progress bar"
574+
"## Production run and analysis\n",
575+
"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."
574576
]
575577
},
576578
{
577579
"cell_type": "code",
578580
"execution_count": null,
579-
"id": "4b873263",
581+
"id": "9049f459",
580582
"metadata": {},
581583
"outputs": [],
582584
"source": [
583-
"def integrate_system(system, n_steps):\n",
584-
" for i in tqdm.trange(100):\n",
585-
" system.integrator.run(n_steps // 100)\n"
585+
"runs = [{'params': {'counterion_valency': 2, 'rod_charge_dens': 1},\n",
586+
" 'histogram': None},\n",
587+
" {'params': {'counterion_valency': 1, 'rod_charge_dens': 2},\n",
588+
" 'histogram': None}\n",
589+
" ]\n",
590+
"N_SAMPLES = 1500"
586591
]
587592
},
588593
{
@@ -617,10 +622,21 @@
617622
" remove_overlap(system)\n",
618623
" system.thermostat.set_langevin(**LANGEVIN_PARAMS)\n",
619624
" print('', end='', flush=True)\n",
620-
" integrate_system(system, WARMUP_STEPS)\n",
625+
"\n",
626+
" # For longer simulation runs it will be convenient to have a progress bar\n",
627+
" for i in tqdm.trange(100):\n",
628+
" system.integrator.run(WARMUP_STEPS // 100)\n",
629+
" \n",
621630
" radial_profile_accs, bin_edges = setup_profile_calculation(\n",
622631
" system, STEPS_PER_SAMPLE, [COUNTERION_TYPE], r_min, N_RADIAL_BINS)\n",
623-
" integrate_system(system, N_SAMPLES * STEPS_PER_SAMPLE)\n",
632+
" for i in tqdm.trange(100):\n",
633+
" system.integrator.run(N_SAMPLES * STEPS_PER_SAMPLE // 100)\n",
634+
"\n",
635+
" # Updating frames for the visualizer\n",
636+
" if(run['params']['counterion_valency']==2):\n",
637+
" vis.update()\n",
638+
" \n",
639+
" \n",
624640
"\n",
625641
" run['histogram'] = radial_profile_accs[COUNTERION_TYPE].mean()\n",
626642
" print(f'simulation for parameters {run[\"params\"]} done\\n')"
@@ -836,10 +852,12 @@
836852
{
837853
"cell_type": "code",
838854
"execution_count": null,
839-
"id": "953f34fd",
855+
"id": "b59e6290-f535-42b1-92b6-78b9597963b3",
840856
"metadata": {},
841857
"outputs": [],
842858
"source": [
859+
"# Resetting system with salt ions\n",
860+
"\n",
843861
"clear_system(system)\n",
844862
"system.box_l = 3 * [ROD_LENGTH]\n",
845863
"counterions = setup_rod_and_counterions(\n",
@@ -852,11 +870,25 @@
852870
"system.electrostatics.solver = p3m\n",
853871
"remove_overlap(system)\n",
854872
"system.thermostat.set_langevin(**LANGEVIN_PARAMS)\n",
855-
"print('', end='', flush=True)\n",
856-
"integrate_system(system, WARMUP_STEPS)\n",
873+
"print('', end='', flush=True)"
874+
]
875+
},
876+
{
877+
"cell_type": "code",
878+
"execution_count": null,
879+
"id": "953f34fd",
880+
"metadata": {},
881+
"outputs": [],
882+
"source": [
883+
"\n",
884+
"for i in tqdm.trange(100):\n",
885+
" system.integrator.run(WARMUP_STEPS // 100)\n",
857886
"radial_profile_accs, bin_edges = setup_profile_calculation(\n",
858887
" system, STEPS_PER_SAMPLE_SALT, all_ion_types, r_min, N_RADIAL_BINS)\n",
859-
"integrate_system(system, N_SAMPLES_SALT * STEPS_PER_SAMPLE_SALT)"
888+
"\n",
889+
"for i in tqdm.trange(100):\n",
890+
" system.integrator.run(N_SAMPLES_SALT * STEPS_PER_SAMPLE_SALT // 100)\n",
891+
" \n"
860892
]
861893
},
862894
{
@@ -939,7 +971,7 @@
939971
],
940972
"metadata": {
941973
"kernelspec": {
942-
"display_name": "Python 3",
974+
"display_name": "Python 3 (ipykernel)",
943975
"language": "python",
944976
"name": "python3"
945977
},
@@ -953,7 +985,7 @@
953985
"name": "python",
954986
"nbconvert_exporter": "python",
955987
"pygments_lexer": "ipython3",
956-
"version": "3.8.10"
988+
"version": "3.10.12"
957989
}
958990
},
959991
"nbformat": 4,

doc/tutorials/lennard_jones/lennard_jones.ipynb

+21-3
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,8 @@
210210
"import espressomd.observables\n",
211211
"import espressomd.accumulators\n",
212212
"import espressomd.analyze\n",
213+
"import espressomd.zn \n",
214+
"\n",
213215
"required_features = [\"LENNARD_JONES\"]\n",
214216
"espressomd.assert_features(required_features)"
215217
]
@@ -632,13 +634,28 @@
632634
"system.thermostat.set_langevin(kT=TEMPERATURE, gamma=GAMMA, seed=42)"
633635
]
634636
},
637+
{
638+
"cell_type": "markdown",
639+
"id": "10510b16-bc19-4c16-a881-a3b88cc298f6",
640+
"metadata": {},
641+
"source": [
642+
"### System visualization "
643+
]
644+
},
635645
{
636646
"cell_type": "code",
637647
"execution_count": null,
638648
"id": "42c573df",
639649
"metadata": {},
640650
"outputs": [],
641-
"source": []
651+
"source": [
652+
"# Visualizer Parameters\n",
653+
"color = {0: \"#00f0f0\"} # Particle color by type\n",
654+
"radii = {0: LJ_SIG/2.0} # Particle size by type\n",
655+
"\n",
656+
"# Initializing Visualizer\n",
657+
"vis = espressomd.zn.Visualizer(system, colors=color, radii=radii)"
658+
]
642659
},
643660
{
644661
"cell_type": "markdown",
@@ -696,6 +713,7 @@
696713
" e_total[i] = energy['total']\n",
697714
" e_kin[i] = energy['kinetic']\n",
698715
" system.integrator.run(STEPS_PER_SAMPLE)\n",
716+
" vis.update()\n",
699717
"T_inst = 2. / 3. * e_kin / N_PART"
700718
]
701719
},
@@ -1235,7 +1253,7 @@
12351253
],
12361254
"metadata": {
12371255
"kernelspec": {
1238-
"display_name": "Python 3",
1256+
"display_name": "Python 3 (ipykernel)",
12391257
"language": "python",
12401258
"name": "python3"
12411259
},
@@ -1249,7 +1267,7 @@
12491267
"name": "python",
12501268
"nbconvert_exporter": "python",
12511269
"pygments_lexer": "ipython3",
1252-
"version": "3.8.10"
1270+
"version": "3.10.12"
12531271
}
12541272
},
12551273
"nbformat": 4,

doc/tutorials/polymers/polymers.ipynb

+64-10
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@
321321
{
322322
"cell_type": "code",
323323
"execution_count": null,
324-
"id": "dd4005bb",
324+
"id": "c9366d00-4982-4ed9-a824-709b5774fba0",
325325
"metadata": {},
326326
"outputs": [],
327327
"source": [
@@ -337,11 +337,20 @@
337337
"import espressomd.accumulators\n",
338338
"import espressomd.observables\n",
339339
"import espressomd.polymer\n",
340+
"import espressomd.zn \n",
340341
"\n",
341342
"logging.basicConfig(level=logging.INFO, stream=sys.stdout)\n",
342343
"\n",
343-
"espressomd.assert_features(['LENNARD_JONES'])\n",
344-
"\n",
344+
"espressomd.assert_features(['LENNARD_JONES'])"
345+
]
346+
},
347+
{
348+
"cell_type": "code",
349+
"execution_count": null,
350+
"id": "d24096b4-c4af-49b7-bd25-6754a2bac9bd",
351+
"metadata": {},
352+
"outputs": [],
353+
"source": [
345354
"# Setup constants\n",
346355
"BOX_L = 12.0\n",
347356
"TIME_STEP = 0.01\n",
@@ -371,8 +380,16 @@
371380
"\n",
372381
"# Fene interaction\n",
373382
"fene = espressomd.interactions.FeneBond(k=7, r_0=1, d_r_max=2)\n",
374-
"system.bonded_inter.add(fene)\n",
375-
"\n",
383+
"system.bonded_inter.add(fene)"
384+
]
385+
},
386+
{
387+
"cell_type": "code",
388+
"execution_count": null,
389+
"id": "d86b9ad0-58f0-47b9-9b58-d5c7dac665e7",
390+
"metadata": {},
391+
"outputs": [],
392+
"source": [
376393
"N_MONOMERS = np.array([6, 8, 10])\n",
377394
"\n",
378395
"com_pos_tau_results = []\n",
@@ -381,7 +398,43 @@
381398
"com_vel_acf_results = []\n",
382399
"rh_results = []\n",
383400
"rf_results = []\n",
384-
"rg_results = []\n",
401+
"rg_results = []"
402+
]
403+
},
404+
{
405+
"cell_type": "code",
406+
"execution_count": null,
407+
"id": "cc807f20-5f3c-412a-a06d-6816256e6ee6",
408+
"metadata": {},
409+
"outputs": [],
410+
"source": [
411+
"### System visualization "
412+
]
413+
},
414+
{
415+
"cell_type": "code",
416+
"execution_count": null,
417+
"id": "142bf210-0273-4b4c-a6fe-641da9d43856",
418+
"metadata": {},
419+
"outputs": [],
420+
"source": [
421+
"print(\"Simulation video for polymer of size :\", N_MONOMERS[2])\n",
422+
"\n",
423+
"# Visualizer Parameters\n",
424+
"color = {0: \"blue\"} # Particle color by type\n",
425+
"radii = {0: LJ_SIGMA/2.0} # Particle size by type\n",
426+
"\n",
427+
"# Initializing Visualizer\n",
428+
"vis = espressomd.zn.Visualizer(system, colors=color, radii=radii, bonds=True)\n"
429+
]
430+
},
431+
{
432+
"cell_type": "code",
433+
"execution_count": null,
434+
"id": "dd4005bb",
435+
"metadata": {},
436+
"outputs": [],
437+
"source": [
385438
"for N in N_MONOMERS:\n",
386439
" logging.info(f\"Polymer size: {N}\")\n",
387440
" build_polymer(system, N, POLYMER_PARAMS, fene)\n",
@@ -405,7 +458,6 @@
405458
" logging.info(\"Equilibration finished.\")\n",
406459
"\n",
407460
" system.thermostat.turn_off()\n",
408-
"\n",
409461
" if POLYMER_MODEL == 'Rouse':\n",
410462
" solvent_langevin(system, KT, GAMMA)\n",
411463
" elif POLYMER_MODEL == 'Zimm':\n",
@@ -422,13 +474,15 @@
422474
" # configure Green-Kubo correlator\n",
423475
" com_vel_cor = correlator_gk(np.arange(N), LOOPS * STEPS)\n",
424476
" system.auto_update_accumulators.add(com_vel_cor)\n",
425-
"\n",
477+
" \n",
426478
" logging.info(\"Sampling started.\")\n",
427479
" rhs = np.zeros(LOOPS)\n",
428480
" rfs = np.zeros(LOOPS)\n",
429481
" rgs = np.zeros(LOOPS)\n",
430482
" for i in tqdm.trange(LOOPS):\n",
431483
" system.integrator.run(STEPS)\n",
484+
" if N == N_MONOMERS[2]:\n",
485+
" vis.update()\n",
432486
" rhs[i] = system.analysis.calc_rh(\n",
433487
" chain_start=0,\n",
434488
" number_of_chains=1,\n",
@@ -969,7 +1023,7 @@
9691023
],
9701024
"metadata": {
9711025
"kernelspec": {
972-
"display_name": "Python 3",
1026+
"display_name": "Python 3 (ipykernel)",
9731027
"language": "python",
9741028
"name": "python3"
9751029
},
@@ -983,7 +1037,7 @@
9831037
"name": "python",
9841038
"nbconvert_exporter": "python",
9851039
"pygments_lexer": "ipython3",
986-
"version": "3.8.5"
1040+
"version": "3.10.12"
9871041
}
9881042
},
9891043
"nbformat": 4,

0 commit comments

Comments
 (0)