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

#### NOTE
[Go to the end](#sphx-glr-download-auto-examples-network-generator-plot-polydisperse-system-py)
to download the full example code.

<a id="sphx-glr-auto-examples-network-generator-plot-polydisperse-system-py"></a>

# Generate a Polydisperse Polymer Network

This example demonstrates how to use the [`MCUniverseGenerator`](../../api/pylimer_tools_cpp.MCUniverseGenerator.html.md#pylimer_tools_cpp.MCUniverseGenerator)
to create a polydisperse end-linked polymer network with varying strand lengths.

For strand length distribution, in this example,
we show how to use the log-normal distribution as proposed by Tsimouri *et al.* [[TCG21](../../acknowledgements.html.md#id24)].
Alternatives were proposed as well, such as the inverse Gaussian or a ternary distribution.

<!-- GENERATED FROM PYTHON SOURCE LINES 13-74 -->
```Python
import math
import random

import matplotlib.pyplot as plt

from pylimer_tools.io.bead_spring_parameter_provider import (
    ParameterType,
    get_parameters_for_polymer,
)
from pylimer_tools_cpp import MCUniverseGenerator

# Get parameters for PDMS polymer density, bead distance, and
# characteristic ratio
params = get_parameters_for_polymer(
    "PDMS", parameter_type=ParameterType.GAUSSIAN)
C_inf = params.get_characteristic_ratio()

# setup strand lengths
n_strands = 1000

average_length = 50
polydispersity = 1.25

# Calculate parameters for log-normal distribution
s = math.sqrt(math.log(polydispersity))
m = math.log(average_length) - 0.5 * s**2

# sample strand lengths from log-normal distribution
strand_lengths = [int(random.lognormvariate(m, s)) for _ in range(n_strands)]

n_atoms_total = sum(strand_lengths) + 0.5 * n_strands  # add crosslinker sites
volume = n_atoms_total / params.get_bead_density()

generator = MCUniverseGenerator(
    volume ** (1 / 3),
    volume ** (1 / 3),
    volume ** (1 / 3),
)

generator.set_bead_distance(
    params.get("<b>").to(params.get("distance_units")).magnitude
)
generator.add_strands(nr_of_strands=n_strands, strand_lengths=strand_lengths)
# 4-functional crosslinkers
generator.add_crosslinkers(
    int(0.5 * n_strands), crosslinker_functionality=4, crosslinker_type=2
)
generator.link_strands_to_conversion(
    crosslinker_conversion=0.925,  # 92.5% of crosslinker sites used
)

universe = generator.get_universe()
universe.set_masses({1: 1.0, 2: 1.0})  # Set masses for LAMMPS

assert math.isclose(
    universe.compute_polydispersity_index(2),
    polydispersity,
    rel_tol=0.1,
)
```

<!-- GENERATED FROM PYTHON SOURCE LINES 75-79 -->

Refer to [File I/O: Readers & Writers](../readers_writers/index.html.md#sphx-glr-auto-examples-readers-writers) for how you can save the universe to a file.

For now, we will show that this produces a variety of different strand
lengths:

<!-- GENERATED FROM PYTHON SOURCE LINES 79-90 -->
```Python
strand_lengths = [m.get_nr_of_atoms()
                  for m in universe.get_chains_with_crosslinker(2)]

# Plot the distribution of strand lengths
plt.figure()
plt.hist(strand_lengths, bins=50, alpha=0.5, label="Strand Lengths")
plt.xlabel("Strand Length")
plt.ylabel("Frequency")
plt.title("Distribution of Strand Lengths in Polydisperse Network")
plt.show()
```

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

<a id="sphx-glr-download-auto-examples-network-generator-plot-polydisperse-system-py"></a>

[`Download Jupyter notebook: plot_polydisperse_system.ipynb`](_downloads/e553e202e2ab4659d7d6656402682c89/plot_polydisperse_system.ipynb)

[`Download Python source code: plot_polydisperse_system.py`](_downloads/18a5a519189fe3448555dd1bae70d560/plot_polydisperse_system.py)

[`Download zipped: plot_polydisperse_system.zip`](_downloads/e44d44775dac690411169184092b0185/plot_polydisperse_system.zip)

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