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

#### NOTE
[Go to the end](#sphx-glr-download-auto-examples-readers-writers-plot-convert-for-bond-swapping-py)
to download the full example code.

<a id="sphx-glr-auto-examples-readers-writers-plot-convert-for-bond-swapping-py"></a>

# Convert for Bond Swapping

LAMMPS bond swapping can significantly speed up equilibration for polymer networks.
However, to preserve the strand lengths, you need to ensure that the molecule indices are set correctly.
Here’s how you could re-set the molecule indices for bond swapping with constant chain lengths.

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

from pylimer_tools_cpp import DataFileWriter, UniverseSequence

if not os.path.exists("generated_structures"):
    os.makedirs("generated_structures")


# Load original system
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()
input_file = str(_fixtures_dir / "structure/melt_83_a_100.structure.out")
sequence = UniverseSequence()
sequence.initialize_from_data_sequence([input_file])
universe = sequence.at_index(0)

# Configure for optimal chain length distribution
output_file = os.path.join(
    os.getcwd(), "generated_structures", "for_swap_" + os.path.basename(input_file)
)

writer = DataFileWriter(universe)
writer.config_include_angles(True)
writer.config_crosslinker_type(2)  # Needed for decomposition to chains
writer.config_molecule_idx_for_swap(True)  # Key setting

writer.write_to_file(output_file)
print(f"Optimized system capable of bond swapping written to: {output_file}")
```

```none
Optimized system capable of bond swapping written to: /home/runner/work/pylimer-tools/pylimer-tools/examples/readers_writers/generated_structures/for_swap_melt_83_a_100.structure.out
```

<!-- GENERATED FROM PYTHON SOURCE LINES 52-54 -->

To convert it back to the molecule index relating to their current connectivity,
simply repeat the process with `config_molecule_idx_for_swap(False)`

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

<a id="sphx-glr-download-auto-examples-readers-writers-plot-convert-for-bond-swapping-py"></a>

[`Download Jupyter notebook: plot_convert_for_bond_swapping.ipynb`](_downloads/2e728147fc88d45b5ad914954901a029/plot_convert_for_bond_swapping.ipynb)

[`Download Python source code: plot_convert_for_bond_swapping.py`](_downloads/953551f7c6d4f4b2c609a6c736192bf7/plot_convert_for_bond_swapping.py)

[`Download zipped: plot_convert_for_bond_swapping.zip`](_downloads/5b38566522fb42b8edf867d14577716d/plot_convert_for_bond_swapping.zip)

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