{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Mutation\n\nThis example shows how to mutate structures including Rattle, Strain, Swap, ChangeAtomType\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "from pynep.mutate import Combine, Rattle, Strain, Swap, ChangeAtomType\nfrom ase.io import read, write\nfrom pynep.io import load_nep\n\n\nframes = read('data.traj', '::10')\nprint(\"read {} structures\".format(len(frames)))\nrattle_mutate = Rattle(probability=0.8,               # rattle probability of each atom\n                       rattle_range=1.5,              # atoms will only rattle in this range\n                       d_ratio=0.7,                   # min distance controlled by d_ratio * covalent_radii\n                       min_dis_mat=None,              # min distance dict such as {('C', 'C'): 1.} \n                                                      # if this is given, d_ratio will be ignored \n                       attempt_number=20)\nstrain_mutate = Strain(cell_cut=1.,                   # max value of strain matrix\n                       sigma=0.1,                     # sigma of gaussian\n                       d_ratio=0.7, \n                       min_dis_mat={('C', 'C'): 1.},  # d_ratio=0.7 will be ignored\n                       attempt_number=20)\n\nmutate = Combine(rattle_mutate, strain_mutate)        # you can combine a series of mutations\nnewframes = []\nfor atoms in frames:\n    newframes.extend(mutate.generate(atoms, 10))\nprint(\"generate {} mutated structures\".format(len(newframes)))\nwrite('new.traj', newframes)\n\n\n# swap mutation swap atoms with different type, so the formula won't change\nframes = load_nep('PbTe.in')[::10]\nprint(\"read {} structures\".format(len(frames)))\n\nswap_mutate = Swap(swap_probability=0.1,   # max swap number is swap_probability * N_atoms\n                   d_ratio=0.7, \n                   min_dis_mat=None, \n                   attempt_number=20)\nnewframes = []\nfor atoms in frames:\n    newframes.extend(swap_mutate.generate(atoms, 5))\nprint(\"generate {} mutated structures\".format(len(newframes)))\nwrite('swap.traj', newframes)\n\n# change mutation just randomly change each atom's symbol to another so the formula may change\nchange_mutate = ChangeAtomType(change_probability=0.5,  # change probability of each atom\n                               d_ratio=0.7, \n                               min_dis_mat=None, \n                               attempt_number=20)\nnewframes = []\nfor atoms in frames:\n    newframes.extend(change_mutate.generate(atoms, 2))\nprint(\"generate {} mutated structures\".format(len(newframes)))\nwrite('change.traj', newframes)"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.9"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}