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

#### NOTE
[Go to the end](#sphx-glr-download-auto-examples-structure-analysis-plot-end-to-end-distribution-py)
to download the full example code.

<a id="sphx-glr-auto-examples-structure-analysis-plot-end-to-end-distribution-py"></a>

# End-to-End Distribution

This example demonstrates how to analyze the end-to-end distribution of polymer chains
using pylimer-tools. It reads a structure file, computes the end-to-end distances,
and plots the distribution of these distances.

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

import matplotlib.pyplot as plt

from pylimer_tools.io.read_lammps_output_file import read_data_file
from pylimer_tools_cpp import Universe

# Replace with your crosslinker type
crosslinker_type = 2


# Load your network (replace path accordingly)
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()
universe = read_data_file(
    str(_fixtures_dir / "structure/network_100_a_46.structure.out")
)
assert isinstance(universe, Universe)

# Compute end-to-end distances for all chains,
# including crosslinkers.
# Two options are available:
end_to_end_distances_derived = [
    m.compute_end_to_end_distance_with_derived_image_flags()
    for m in universe.get_chains_with_crosslinker(crosslinker_type)
]
end_to_end_distances = [
    m.compute_end_to_end_distance()
    for m in universe.get_chains_with_crosslinker(crosslinker_type)
]

# Plot the end-to-end distance distribution
plt.figure()
plt.hist(
    end_to_end_distances_derived,
    bins=50,
    alpha=0.5,
    label="End-to-End Distances (Derived Image Flags)",
)
plt.hist(
    end_to_end_distances,
    bins=50,
    alpha=0.5,
    label="End-to-End Distances (Original Image Flags)",
)
plt.xlabel("End-to-End Distance")
plt.ylabel("Frequency")
plt.show()
```

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

<a id="sphx-glr-download-auto-examples-structure-analysis-plot-end-to-end-distribution-py"></a>

[`Download Jupyter notebook: plot_end_to_end_distribution.ipynb`](_downloads/b16387431e9b9e37a924b860f0c52f55/plot_end_to_end_distribution.ipynb)

[`Download Python source code: plot_end_to_end_distribution.py`](_downloads/5d785a30c2c538854433711b98b5cacb/plot_end_to_end_distribution.py)

[`Download zipped: plot_end_to_end_distribution.zip`](_downloads/40c40441682b4b36d3efb2e6b64ed7a4/plot_end_to_end_distribution.zip)

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