<!-- DO NOT EDIT. -->
<!-- THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. -->
<!-- TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: -->
<!-- "auto_examples/dpd_simulations/plot_dpd_simulation.py" -->
<!-- LINE NUMBERS ARE GIVEN BELOW. -->

#### NOTE
[Go to the end](#sphx-glr-download-auto-examples-dpd-simulations-plot-dpd-simulation-py)
to download the full example code.

<a id="sphx-glr-auto-examples-dpd-simulations-plot-dpd-simulation-py"></a>

# Dissipative Particle Dynamics (DPD) Simulations

This example demonstrates how to set up and run a DPD simulation with slip-springs using pylimer-tools.

References: Langeloth *et al.* [[LMBohmMullerPlathe13](../../acknowledgements.html.md#id11)] and Schneider *et al.* [[SFKarimiVarzanehMullerPlathe21](../../acknowledgements.html.md#id10)].

Assuming your `pylimer_tools` version was compiled with OpenMP,
to run this in parallel, you can use the typical OpenMP environment variables,
e.g., in the terminal before running your script: `export OMP_NUM_THREADS=4` to use 4 threads.

<!-- GENERATED FROM PYTHON SOURCE LINES 13-52 -->
```Python
import os
from pathlib import Path

import pandas as pd

from pylimer_tools.io.read_lammps_output_file import read_data_file
from pylimer_tools_cpp import (
    AtomStyle,
    ComputedDoubleValues,
    ComputedIntValues,
    DPDSimulator,
    OutputConfiguration,
)


# Load a LAMMPS data file (replace with your file)
def _find_fixtures_dir() -> Path:
    for cur in (
        [Path(__file__).resolve().parent] if "__file__" in globals() else []
    ) + [Path.cwd()]:
        for p in [cur] + list(cur.parents):
            candidate = p / "tests" / "pylimer_tools" / "fixtures"
            if candidate.is_dir():
                return candidate
    return Path("tests/pylimer_tools/fixtures").resolve()


_fixtures_dir = _find_fixtures_dir()
filePath = str(_fixtures_dir / "structure/melt_83_a_100.structure.out")
universe = read_data_file(
    filePath, atom_style=[AtomStyle.HYBRID, AtomStyle.BOND, AtomStyle.EDPD]
)

# Create the DPD simulator
dpd_simulator = DPDSimulator(
    universe=universe,
    seed="12345",  # Set a random seed for reproducibility*
)
```

<!-- GENERATED FROM PYTHON SOURCE LINES 53-56 -->

Note that the random seed does not guarantee the same results across different runs,
as the DPD algorithm is inherently stochastic and will produce different results
if you use more than one processor.

<!-- GENERATED FROM PYTHON SOURCE LINES 56-99 -->
```Python
# Randomly sample slip-springs
dpd_simulator.create_slip_springs(num=100)

# Configure what to output during the simulation on each step
step_output_config = OutputConfiguration()
step_output_config.int_values = [
    ComputedIntValues.STEP,  # Output the simulation step
]
step_output_config.double_values = [
    ComputedDoubleValues.TEMPERATURE,  # Output the temperature
    ComputedDoubleValues.PRESSURE,  # Output the pressure
    ComputedDoubleValues.STRESS_XX,  # Output the xx component of the stress tensor
    ComputedDoubleValues.STRESS_YY,  # Output the yy component of the stress tensor
    ComputedDoubleValues.STRESS_ZZ,  # Output the zz component of the stress tensor
]
step_output_config.output_every = 10  # Output every 10 steps
# temporary filename for output
step_output_config.filename = "dpd_simulation_output.txt"
dpd_simulator.config_step_output([step_output_config])

# Configure what to average during the simulation
avg_output_config = OutputConfiguration()
avg_output_config.int_values = [
    ComputedIntValues.STEP,  # Output the simulation step
]
avg_output_config.double_values = [
    ComputedDoubleValues.TEMPERATURE,  # Output the temperature
    ComputedDoubleValues.PRESSURE,  # Output the pressure
]
avg_output_config.output_every = 10
step_output_config.use_every = 1
avg_output_config.filename = "dpd_simulation_avg_output.txt"
dpd_simulator.config_average_output([avg_output_config])

dpd_simulator.run_simulation(
    n_steps=1000,  # Number of simulation steps
    dt=0.01,  # Time step size
    with_MC=True,  # Enable Monte Carlo moves of slip-springs
)

print("DPD simulation completed.")
```

```none
DPD simulation completed.
```

<!-- GENERATED FROM PYTHON SOURCE LINES 100-101 -->

read the output file and actually plot something

<!-- GENERATED FROM PYTHON SOURCE LINES 101-115 -->
```Python
df = pd.read_csv("dpd_simulation_output.txt", sep="\\s+")
_ = df.plot(
    x="Step",
    y=[
        "Temperature",
        "Pressure",
        "Stress[0,0]",
        "Stress[1,1]",
        "Stress[2,2]",
    ],
    subplots=True,
    layout=(3, 2),
    figsize=(10, 10),
)
```

**Total running time of the script:** (0 minutes 3.146 seconds)

<a id="sphx-glr-download-auto-examples-dpd-simulations-plot-dpd-simulation-py"></a>

[`Download Jupyter notebook: plot_dpd_simulation.ipynb`](_downloads/7ea8883241a6011e02c758aef99af3f6/plot_dpd_simulation.ipynb)

[`Download Python source code: plot_dpd_simulation.py`](_downloads/1c1487cbd2deec0433f6e971e8ec5acf/plot_dpd_simulation.py)

[`Download zipped: plot_dpd_simulation.zip`](_downloads/fc465a2cb3da2f85f7d05c08404f3566/plot_dpd_simulation.zip)

[Gallery generated by Sphinx-Gallery](https://sphinx-gallery.github.io)
