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

#### NOTE
[Go to the end](#sphx-glr-download-auto-examples-readers-writers-plot-data-file-writer-py)
to download the full example code.

<a id="sphx-glr-auto-examples-readers-writers-plot-data-file-writer-py"></a>

# Data File Writer

The [`DataFileWriter`](../../api/pylimer_tools_cpp.DataFileWriter.html.md#pylimer_tools_cpp.DataFileWriter) generates LAMMPS-compatible data files.

<!-- GENERATED FROM PYTHON SOURCE LINES 8-40 -->
```Python
import os
import random

from pylimer_tools_cpp import AtomStyle, DataFileWriter, Universe

# create folder generated_structures/
if not os.path.exists("generated_structures"):
    os.makedirs("generated_structures")

# Generate a universe to write
universe = Universe(10.0, 10.0, 10.0)
universe.add_atoms(
    list(range(100)),
    [1 for _ in range(100)],
    [random.random() * 10.0 for _ in range(100)],
    [random.random() * 10.0 for _ in range(100)],
    [random.random() * 10.0 for _ in range(100)],
    [0 for _ in range(100)],
    [0 for _ in range(100)],
    [0 for _ in range(100)],
)
# universe.add_bonds(
#   …
# )

# Create writer for a universe
writer = DataFileWriter(universe)

# Basic output
writer.write_to_file("generated_structures/output.structure.dat")
```

<!-- GENERATED FROM PYTHON SOURCE LINES 41-43 -->

## Advanced Configuration

<!-- GENERATED FROM PYTHON SOURCE LINES 43-65 -->
```Python
# Configure output options
writer = DataFileWriter(universe)

# Include topology information
writer.config_include_angles(True)
writer.config_include_dihedral_angles(True)

# Control atom numbering
writer.config_reindex_atoms(True)
writer.config_crosslinker_type(2)

# Handle periodic boundaries
writer.config_move_into_box(True)
writer.config_attempt_image_reset(True)

# Include velocities
writer.config_include_velocities(True)

# Write configured output
writer.write_to_file("generated_structures/configured_output.data")
```

<!-- GENERATED FROM PYTHON SOURCE LINES 66-70 -->

## Custom Atom Styles

You can specify custom atom styles and formats using the following format:

<!-- GENERATED FROM PYTHON SOURCE LINES 70-78 -->
```Python
# Set predefined atom style
writer.config_atom_style(AtomStyle.FULL)

# Or define custom format
custom_format = "$atomId $moleculeId $atomType $charge $x $y $z"
writer.set_custom_atom_format(custom_format)
```

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

Note that currently, the atom styles `tdpd` and `hybrid` are not supported
without you specifying this custom format.

<!-- GENERATED FROM PYTHON SOURCE LINES 83-87 -->

## Molecule Indexing Control

Control how molecules are numbered in the output

<!-- GENERATED FROM PYTHON SOURCE LINES 87-97 -->
```Python
# Standard molecular indexing: sets the molecule index for each atom
# based on the decomposition at the junctions
writer.config_molecule_idx_for_swap(False)

# Optimize for LAMMPS' ``bond/swap`` operations
# Sets the molecule index for each atom based on the distance from the
# next junction
writer.config_molecule_idx_for_swap(True)
writer.config_molecule_idx_for_swap(True)
```

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

<a id="sphx-glr-download-auto-examples-readers-writers-plot-data-file-writer-py"></a>

[`Download Jupyter notebook: plot_data_file_writer.ipynb`](_downloads/0631f75f6838d78a5faf4611c88f7bcd/plot_data_file_writer.ipynb)

[`Download Python source code: plot_data_file_writer.py`](_downloads/748057cfd1b0bb073a3921a395a9c7f8/plot_data_file_writer.py)

[`Download zipped: plot_data_file_writer.zip`](_downloads/fd35ff0298534bb5f09a37f322230ad0/plot_data_file_writer.zip)

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