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

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

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

# Loop Finding and Analysis

This example demonstrates how to find and analyze loops in polymer networks using pylimer-tools.

<!-- GENERATED FROM PYTHON SOURCE LINES 7-59 -->
```none
Number of loops found (max length 2*(46+1)): 2
Loop length counts: {322: 1, 92: 1, 46: 2, 138: 1}
Shortest loop involving atom 1: []
```

<br/>
```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 'network.gml' 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()
universe = read_data_file(
    str(_fixtures_dir / "structure/network_100_a_46.structure.out")
)
assert isinstance(universe, Universe)

# 1. Find loops in the network
loops = universe.find_loops(
    crosslinker_type, skip_self_loops=True, max_length=2 * (46 + 1)
)  # restrict for large networks

print(f"Number of loops found (max length 2*(46+1)): {len(loops)}")

# 2. Count loop lengths
loop_length_counts = universe.count_loop_lengths(max_length=7 * (46 + 1))
print(f"Loop length counts: {loop_length_counts}")

# plot the loop length counts
plt.bar(list(loop_length_counts.keys()), list(loop_length_counts.values()))
plt.xlabel("Loop Length")
plt.ylabel("Count")
plt.show()

# 3. Find shortest loop for a specific atom (e.g., atom 0)
shortest_loop = universe.find_minimal_order_loop_from(
    loop_start=1, loop_step1=1)
print(f"Shortest loop involving atom 1: {shortest_loop}")
```

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

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

[`Download Jupyter notebook: plot_loop_analysis.ipynb`](_downloads/2597fa1b568165054f9ecb58cad97e96/plot_loop_analysis.ipynb)

[`Download Python source code: plot_loop_analysis.py`](_downloads/517fdf36ddecba1a12587474d4a82381/plot_loop_analysis.py)

[`Download zipped: plot_loop_analysis.zip`](_downloads/6199f131c5a0e9e3f49d9c04d17856b4/plot_loop_analysis.zip)

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