#!/usr/bin/env python # coding: utf-8 # ## Overview # # This example builds amorphous polymer cells: many chains in a periodic box at a given density, as a starting point # for molecular dynamics. The polymer builder grows all chains at the same time, one monomer per chain in turn, and # checks for overlaps across the periodic boundaries. # # Random growth gets stuck well below the density of a real polymer melt, especially for bulky monomers. Dense cells # are therefore grown at a lower density and then compressed with a short molecular dynamics run that shrinks the # lattice. import numpy as np from scm.plams import view from scm.structures.builder.polymer import * def density(system): """Density in g/cm^3 of a system with an orthorhombic lattice.""" mass = sum(atom.element.mass for atom in system.atoms) return mass * 1.66054 / np.prod(np.diag(system.lattice.vectors)) # ## Chains in a box # # Each ``Chain`` describes one kind of chain, either from a link table with a number of monomers, or from a sequence. # ``count`` is the number of such chains. With ``density`` the builder sizes a cubic box for the expected mass. Every # chain is put in its own region. # # When no placement of a monomer fits, the builder may accept one with a few close contacts, and warns about it. # ``optimize="uff"`` removes them with a short geometry optimization in AMS. ethylene = Monomer.from_smiles("[*]CC[*]") styrene = Monomer.from_smiles("[*]CC([*])c1ccccc1") monomers = {"E": ethylene, "S": styrene} chains = [Chain(homopolymer_links(monomers, "E"), 30, count=8)] polyethylene = build_polymers(monomers, chains, density=0.4, temperature=300, optimize="uff", seed=1) print(f"{polyethylene.num_atoms} atoms, {polyethylene.num_molecules()} chains, density {density(polyethylene):.2f} g/cm^3") view(polyethylene, direction="tilt_x", width=400, height=400, picture_path="picture1.png") # ## Chain length distributions # # The number of monomers can be a distribution. Every chain draws its own length, here from a Schulz-Zimm # distribution with a number average of 20 monomers and a dispersity of 1.5. Different kinds of chains give a blend. chains = [ Chain(sequence=Repeat("E", SchulzZimm(20, 1.5)), count=6), Chain(sequence=Repeat("S", 8), count=3), ] blend = build_polymers(monomers, chains, density=0.4, temperature=300, optimize="uff", seed=2) lengths = [len(blend.get_atoms_in_region(f"Chain{i}")) for i in range(1, 10)] print("atoms per chain:", lengths) view(blend, direction="tilt_x", width=400, height=400, picture_path="picture2.png") # ## A dense melt # # To reach the density of a melt, ``growth_density`` grows the chains in a larger box at a low density first. They are # then compressed to ``density`` with molecular dynamics that deforms the lattice (here with UFF), and optimized. # This runs AMS, which takes a minute or two. chains = [Chain(sequence=Repeat("S", 12), count=6)] melt = build_polymers( monomers, chains, density=1.0, growth_density=0.4, compress_steps=4000, optimize="uff", temperature=300, seed=1, ) print(f"{melt.num_atoms} atoms, density {density(melt):.2f} g/cm^3") view(melt, direction="tilt_x", width=400, height=400, picture_path="picture3.png") # ## Crosslinking # # After the growth, free link points can be bonded to each other. Here the backbone monomer has an extra link point # ``C`` on the phenyl ring. Pairs of these link points whose atoms are within ``crosslink_distance`` and that point at # each other are bonded, closest first. The new bonds are strained, so the result is optimized. crosslinker = {"G": Monomer.from_smiles("[*]CC([*])c1ccc([*])cc1", head="A", tail="B")} chains = [Chain(homopolymer_links(crosslinker, "G"), 10, count=6)] crosslinks = [Link(LinkPoint("G", "C"), LinkPoint("G", "C"))] network = build_polymers( crosslinker, chains, density=0.3, crosslinks=crosslinks, crosslink_distance=6.0, optimize="uff", temperature=300, seed=2, ) print(f"6 chains, {network.num_molecules()} molecules after crosslinking") view(network, direction="tilt_x", width=400, height=400, picture_path="picture4.png")