# Perfect Polygon Crystal Initializer (Triangle Motifs with Center Patch)

This script generates a perfect 2D lattice of regular polygon motifs (in the default setup: triangles) with an additional patch placed at the center of each polygon.  
The outputs are written to simple text files and can be used as initialization data for HOOMD-blue or other simulation codes.

## What this script does

The script:

- constructs a **regular n-gon** (default: triangle, `n_sides = 3`) with a given circumradius,
- tiles this motif on a 2D lattice using a user-defined spacing,
- assigns per-vertex parameters (inner/outer length scales, polygon “volume”, box scale),
- defines **rigid-body entries** for:
  - polygon vertices (type 1),
  - a central patch (type 2) at each polygon center,
- writes two text files:
  - one for vertex coordinates and per-vertex parameters,
  - one for rigid-body definitions including the center patches,
- prints a suggested in-plane simulation box size for use in HOOMD.

This enables “perfect crystal” initial conditions composed of identical polygonal units with a central interaction patch.

## Outputs

The script writes its outputs in a directory named by the number of sides:

```text
N<n_sides>/
    polygon_verts.txt
    polygon_rigid.txt
```

### `polygon_verts.txt`

Each line corresponds to one polygon vertex and contains:

```text
x  y  z  sigma_in  sigma_out  v_poly  Lbox
```

where:

- `x, y, z` are the vertex coordinates,
- `sigma_out` is taken equal to the polygon circumradius,
- `sigma_in` is an inner length scale (e.g. in-radius),
- `v_poly` is a scalar based on the polygon geometry (e.g. area for an n‑gon),
- `Lbox` is a box-length scale carried along for convenience.

These fields are intended as generic parameters that downstream scripts can interpret as needed (for example, to set particle sizes or reference areas).

### `polygon_rigid.txt`

Each line has the format:

```text
type  x  y  z  patch_size
```

where:

- `type = 1` denotes a vertex element of the rigid polygon motif,
- `type = 2` denotes the **center patch** at the polygon center,
- `x, y, z` are positions,
- `patch_size` is a scalar that can be mapped to patch radius or interaction range.

All vertices and the center patch for each motif appear in one combined list, suitable for conversion to a rigid-body definition in a simulation script.

## Parameters

At the top of the script, you can control:

- `n_sides`  
  Number of polygon sides (3 = triangle). In principle can be extended to 4, 5, … for other regular polygons.

- `circumradius`  
  Distance from polygon center to each vertex.

- `buffer`  
  Extra spacing between neighboring motifs on the lattice.

- `angle_offset`  
  Global rotation angle applied to all vertices (e.g., to place a base edge horizontally).

- `patch_size`  
  Size parameter for the center patch (e.g. used as a diameter or interaction range).

### Tiling (crystal) parameters

- `n_tiles_x`, `n_tiles_y`  
  Number of motifs in the x and y directions.

- `lattice_a`  
  Lattice spacing between motif centers.  
  By default set as `2 * circumradius + buffer`.

From these, the script also prints a suggested in-plane box size:

```text
Lx = n_tiles_x * lattice_a
Ly = n_tiles_y * lattice_a
```

to help you define a simulation box that contains the full motif lattice.

## How to run

1. Place the script (for example `generate_polygon_lattice.m`) in your MATLAB working directory.
2. Run it:

```matlab
run('generate_polygon_lattice.m')
```

3. After running, you should see a folder:

```text
N3/
    polygon_verts.txt
    polygon_rigid.txt
```

You can then use these files as input to your HOOMD or analysis scripts that:

- convert the vertex list into particles and rigid bodies,
- attach interaction parameters to vertex types and center patches,
- simulate perfect polygon lattices and their perturbations.

## Use in hierarchical folding context

In the broader project, this “perfect crystal” initializer complements scripts that:

- generate **linear block copolymer chains**,
- simulate their folding into polygon-like motifs,
- assemble those motifs into membrane-like structures.

This script provides a reference **ideal polygon crystal** configuration, which can be compared to the structures obtained from hierarchical folding simulations (e.g., to quantify defects, distortions, or domain ordering).

## Notes

- The output path uses the current working directory by default (`base_path = pwd`), so no user-specific filesystem paths are hard-coded.
- By changing `n_sides` and a few geometric parameters, you can generate regular 4‑, 5‑, …‑gons with central patches using the same script structure.
- The file formats are intentionally simple (plain text) so that they are easy to parse in Python, MATLAB, or C++ analysis tools.