> For the complete documentation index, see [llms.txt](https://anufrievroman.gitbook.io/freepaths/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://anufrievroman.gitbook.io/freepaths/theory/algorithm.md).

# Algorithm

The algorithm runs the simulations step-by-step and phonon-by-phonon. For each phonon, the algorithm calculates a trajectory, with a time step *dt* and for as many time steps as required for the phonon to reach the cold side or until a maximum number of time steps is reached.

<figure><img src="/files/kOieStR4Ofkdk9uEmjZu" alt=""><figcaption><p>Scheme of the simulated system, indicating some of the parameters set in the input files.</p></figcaption></figure>

### Initialization

At the beginning of time, each phonon is generated at the hot side. Each phonon is assigned a frequency (wavelength) and the direction according to the source type. The frequency is assigned according to the Planck distribution of phonon frequencies at this temperature (*T*). See the picture on the [main page](/freepaths/readme.md) for the example of the Planck distribution function. From the assigned frequency, the algorithm determines the phonon group velocity (*v*) from the phonon dispersion in the given material. If more than one polarization branch is available at this frequency, it is chosen randomly.

The phonon starts moving step-by-step in the assigned direction according to the following equations:

$$
\begin{aligned}
\Delta x &= \sin(\theta)\cdot|\cos(\phi)|\cdot v\cdot dt \\
\Delta y &= \cos(\theta)\cdot|\cos(\phi)|\cdot v\cdot dt \\
\Delta z &= \sin(\phi)\cdot v\cdot dt
\end{aligned}
$$

where θ is the angle between the projection to *x-y* plane and *y*-axis, and φ is the angle to the horizontal plane, *v* is the speed, and *dt* is the time step.

### Scattering on boundaries

At each time step *dt*, the algorithm checks if phonon crossed any of the boundaries. The boundaries include the top and bottom of the simulation, left and right walls, or walls of the holes or pillars. If the boundary is crossed, the corresponding function calculates at which angle *a* phonon hits the boundary. Then, the algorithm calculates the specular scattering probability, determined by Soffer's equation:

$$
p = exp(-16 \pi ^2 \sigma^2 cos^2(\alpha) / \lambda ^2)
$$

where *p* is the specularity probability (number between zero and one), σ is the surface roughness, α is the angle to the surface, and λ is the wavelength of the phonon. Then the algorithm draws a random number between zero and one. If the number is greater than p, then the scattering is diffuse, otherwise specular. If the scattering is specular, the phonon is reflected elastically, from the surface. If the scattering is diffuse, the phonon is reflected in a random direction, but using the [Lambert cosine distribution](https://en.wikipedia.org/wiki/Lambert's_cosine_law) of probability. After the scattering, the phonon continues the movement and keeps moving and being scattered until it either reaches the cold side, or returns to the hot side, or the time of the simulation for each phonon is over.

### Internal scattering

Besides the surface scattering, phonon can also experience [internal scattering](https://en.wikipedia.org/wiki/Phonon_scattering). The internal scattering means all scattering processes like impurity scattering, phonon-phonon scattering (including Normal and Umklapp scatterings). In the simulation, this kind of scattering is programmed to occur when a certain time (relaxation time) has passed since the last scattering of any kind. In other words, if phonon runs for too long without any scatterings, it's getting more and more likely to be scattered due to the phonon-phonon process. The relaxation time is calculated depending on the phonon frequency, temperature, and polarization.

### Distributions and statistics

Once the simulation is over for a given phonon, the algorithm collects all the phonon properties (frequency, speed, path etc.), its entrance and exit angles, and other results and stores it in files. Then, the process repeats for the required number of phonons. After simulations for all phonons are done, the algorithm calculates various distributions and statistical facts about the simulation. For example, it calculates phonon spectrum at the beginning, phonon trajectories, phonon exit angle distribution, group velocities etc. The examples of the distribution are shown above in the picture. Then, it outputs all this information into the graphs and into .csv files. Examples below show scattering maps and statistics obtained using the Monte Carlo code for a [serpentine nanowire](https://pubs.rsc.org/en/content/articlelanding/2019/NR/C9NR03863A).

<figure><img src="/files/VxGd7t6xFcYKmLN0QHa9" alt=""><figcaption><p>Examples of scattering maps and various distributions.</p></figcaption></figure>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://anufrievroman.gitbook.io/freepaths/theory/algorithm.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
