Polymer Builder Part 1: Chains, Copolymers and Sequences#

Build polymer chains from monomers in Python: homopolymers, random coils, tacticity, alternating, statistical and blocky copolymers from reactivity ratios, block and gradient copolymers, stars and combs. The monomers are defined with SMILES, with * marking the link points.

Expected duration: This notebook takes less than a minute to run.

Downloads: Notebook | Script ?

Estimated Time: 0.02 h

Requires: AMS2027 or later

Related examples
Related tutorials
Related documentation
../_images/polymer_builder_chains_28_0_e1aef60d.png

Overview#

This example introduces the polymer builder in scm.structures.builder.polymer by building single chains. A Monomer holds a molecular structure and its named link points. build_polymer joins monomers at those points and returns a ChemicalSystem that you can inspect, visualize, or use in a simulation.

There are two ways to describe the chain:

  • A link table gives the possible additions at each free link point and their relative weights. Use it for homopolymers and copolymers whose next monomer depends on the current chain end.

  • A sequence specifies the order or pattern of monomers. Use it for blocks, gradients, and branched architectures.

A copolymer simply contains more than one monomer type: it can be built with either method. Link tables make each addition from local rules at the current chain end, while sequence nodes describe a whole-chain pattern (which can itself include random choices). Choose the method based on the pattern you need to express.

In either case the builder removes the two dummy atoms at a join, bonds their neighboring atoms, and chooses a dihedral angle around the new bond to avoid overlaps.

from pprint import pprint

from scm.plams import view
from scm.structures.builder.polymer import Monomer, build_polymer

Monomers#

In the SMILES passed to Monomer.from_smiles, [*] marks a dummy atom where a bond can be made. Link points are named A, B, and so on in SMILES order. For a monomer with two link points, the first is its head and the second its tail. In a linear chain, each new head attaches to the previous tail.

Here styrene and ethylene are Monomer objects. Their system attributes contain the molecular structures; the displayed dummy atoms show where another monomer can attach.

styrene = Monomer.from_smiles("[*]CC([*])c1ccccc1")
ethylene = Monomer.from_smiles("[*]CC[*]")
print(styrene)
view(styrene.system, direction="along_pca2", width=300, height=300, fixed_atom_size=False)
Monomer(C8H8Xx2, link_points={'A': 0, 'B': 3}, head=A, tail=B)
image generated from notebook

A homopolymer#

The monomers dictionary maps descriptive names to Monomer objects. These names identify monomer types in link tables, sequences, and the regions of the resulting structure. They are distinct from the A and B link-point names within each monomer.

homopolymer_links(monomers, key) creates a head-to-tail link table for the selected monomer type, allowing chain growth at either end. The example below selects "styrene" and displays the resulting links.

from scm.structures.builder.polymer import homopolymer_links

monomers = {"styrene": styrene, "ethylene": ethylene}
links = homopolymer_links(monomers, "styrene")
pprint(links)
[Link(site=LinkPoint(monomer='styrene', name='B'),
      incoming=LinkPoint(monomer='styrene', name='A'),
      weight=1.0),
 Link(site=LinkPoint(monomer='styrene', name='A'),
      incoming=LinkPoint(monomer='styrene', name='B'),
      weight=1.0)]

Pass the dictionary, link table, and desired number of monomers to build_polymer. seed makes the random choices reproducible. The result is a ChemicalSystem; here we print its formula and view its geometry. Free link points at the chain ends are capped with hydrogen by default.

polystyrene = build_polymer(monomers, links, 20, seed=1)
print(polystyrene.formula())
view(polystyrene, direction="along_pca3", width=500, height=300, show_regions=True)
C160H162
image generated from notebook

By default the builder tries dihedral angles from low to high steric energy, giving a relatively extended chain. With temperature in kelvin, it samples dihedrals from a Boltzmann distribution, giving more varied conformations. Backbone bonds inside each monomer can rotate as well. optimize="uff" relaxes close contacts after building the chain.

coil = build_polymer(monomers, links, 20, temperature=300, seed=2, optimize="uff")
view(coil, direction="along_pca3", width=500, height=300, show_regions=True)
[10.11|15:15:59] JOB polymer_optimization STARTED


[10.11|15:15:59] JOB polymer_optimization RUNNING


[10.11|15:16:00] JOB polymer_optimization FINISHED


[10.11|15:16:00] JOB polymer_optimization SUCCESSFUL
image generated from notebook

Tacticity#

For this styrene monomer, with_mirror_images adds its mirror image under the key "styrene*". The tacticity_links helper then constructs the joins needed for an isotactic, syndiotactic, or atactic chain. The builder puts atoms from each monomer type in a region, so show_regions=True helps reveal their order.

from scm.structures.builder.polymer import tacticity_links, with_mirror_images

tactic_monomers = with_mirror_images({"styrene": styrene})
print(tactic_monomers.keys())
syndiotactic_links = tacticity_links(tactic_monomers, "styrene", "syndiotactic")
pprint(syndiotactic_links)
dict_keys(['styrene', 'styrene*'])
[Link(site=LinkPoint(monomer='styrene', name='B'),
      incoming=LinkPoint(monomer='styrene*', name='A'),
      weight=1.0),
 Link(site=LinkPoint(monomer='styrene*', name='A'),
      incoming=LinkPoint(monomer='styrene', name='B'),
      weight=1.0),
 Link(site=LinkPoint(monomer='styrene*', name='B'),
      incoming=LinkPoint(monomer='styrene', name='A'),
      weight=1.0),
 Link(site=LinkPoint(monomer='styrene', name='A'),
      incoming=LinkPoint(monomer='styrene*', name='B'),
      weight=1.0)]
syndiotactic = build_polymer(tactic_monomers, syndiotactic_links, 20, seed=2)
print(syndiotactic.region_names)
view(syndiotactic, direction="along_pca3", width=500, height=300, show_regions=True)
['Monomer_styrene', 'Monomer_styrene_mirror']
image generated from notebook

Sequences#

For a specified block order or another pattern that changes along the chain, use a sequence instead of a link table. A sequence is a small tree describing the chain’s pattern. Repeat repeats a monomer or another sequence node, while Seq joins its children in order. In the example below, Repeat("styrene", 8) gives a block of eight styrene units. The sequence= argument takes the place of a link table and repeat count.

from scm.structures.builder.polymer import Repeat, Seq

sequence = Seq([Repeat("styrene", 8), Repeat("ethylene", 16), Repeat("styrene", 8)])
triblock = build_polymer(monomers, sequence=sequence, temperature=300, seed=1)
view(triblock, direction="along_pca3", width=500, height=300, show_regions=True)
image generated from notebook

Sequence nodes can be nested. Poisson(6) draws a block length with mean 6 (at least one unit), so repeating a pair of such blocks produces a multiblock with variable lengths.

from scm.structures.builder.polymer import Poisson

multiblock_sequence = Repeat(Seq([Repeat("ethylene", Poisson(6)), Repeat("styrene", Poisson(3))]), 4)
multiblock_chain = build_polymer(monomers, sequence=multiblock_sequence, seed=3)
print(multiblock_chain.formula())
view(multiblock_chain, direction="along_pca3", width=500, height=300, show_regions=True)
C158H214
image generated from notebook

Gradient("ethylene", "styrene", 30) gives 30 units while the probability of styrene rises along the chain. Unlike the multiblock, this specifies a gradual change in composition.

from scm.structures.builder.polymer import Gradient

gradient_sequence = Gradient("ethylene", "styrene", 30)
gradient_chain = build_polymer(monomers, sequence=gradient_sequence, seed=3)
print(gradient_chain.formula())
view(gradient_chain, direction="along_pca3", width=500, height=300, show_regions=True)
C138H174
image generated from notebook

Branched polymers#

A monomer with more than two link points can start side chains. Star grows an arm from each link point of a core. Comb repeats a backbone monomer and attaches side chains at its branch point. The graft_styrene monomer below has a third link point on its phenyl ring; its head and tail identify the two points used for the backbone. Each arm or side chain is itself a sequence node.

from scm.structures.builder.polymer import Star

branched_monomers = {
    "ethylene": ethylene,
    "core": Monomer.from_smiles("C(C[*])(C[*])(C[*])C[*]"),
    "graft_styrene": Monomer.from_smiles("[*]CC([*])c1ccc([*])cc1", head="A", tail="B"),
}
star_sequence = Star("core", Repeat("ethylene", 8))
star = build_polymer(branched_monomers, sequence=star_sequence, temperature=300, seed=1)
view(star, direction="along_pca3", width=400, height=400, show_regions=True)
image generated from notebook
from scm.structures.builder.polymer import Comb

comb_sequence = Comb("graft_styrene", Repeat("ethylene", 5), 12, every=3)
comb = build_polymer(branched_monomers, sequence=comb_sequence, seed=1)
view(comb, direction="along_pca3", width=500, height=300, show_regions=True)
image generated from notebook

Caps and following the growth#

By default, free link points become hydrogens. The cap argument can instead supply a one-link-point Monomer such as methyl, another element, or None to keep the dummy atoms. With trajectory, the builder writes each growth step to an .rkf file for AMSmovie; progress receives updates as the chain grows.

from scm.structures.builder.polymer import print_progress

methyl = Monomer.from_smiles("[*]C")
capped = build_polymer(
    monomers,
    homopolymer_links(monomers, "styrene"),
    6,
    cap=methyl,
    seed=1,
    trajectory="polystyrene_growth.rkf",
    progress=print_progress,
)
view(capped, direction="along_pca3", width=400, height=300, show_regions=True)
POLYMER STEP 1/6


POLYMER FRAME 1


POLYMER STEP 2/6


POLYMER FRAME 2


POLYMER STEP 3/6


POLYMER FRAME 3


POLYMER STEP 4/6


POLYMER FRAME 4


POLYMER STEP 5/6


POLYMER FRAME 5


POLYMER STEP 6/6


POLYMER FRAME 6
image generated from notebook

See also#

Python Script#

#!/usr/bin/env python
# coding: utf-8

# ## Overview
# 
# This example introduces the polymer builder in ``scm.structures.builder.polymer`` by building single chains. A
# ``Monomer`` holds a molecular structure and its named link points. ``build_polymer`` joins monomers at those points
# and returns a ``ChemicalSystem`` that you can inspect, visualize, or use in a simulation.
# 
# There are two ways to describe the chain:
# 
# * A **link table** gives the possible additions at each free link point and their relative weights. Use it for
#   homopolymers and copolymers whose next monomer depends on the current chain end.
# * A **sequence** specifies the order or pattern of monomers. Use it for blocks, gradients, and branched architectures.
# 
# A *copolymer* simply contains more than one monomer type: it can be built with either method. Link tables make each addition from local rules at the current chain end, while sequence nodes describe a whole-chain pattern (which can itself include random choices). Choose the method based on the pattern you need to express.
# 
# In either case the builder removes the two dummy atoms at a join, bonds their neighboring atoms, and chooses a
# dihedral angle around the new bond to avoid overlaps.
# 

from pprint import pprint

from scm.plams import view
from scm.structures.builder.polymer import Monomer, build_polymer


# ## Monomers
# 
# In the SMILES passed to ``Monomer.from_smiles``, ``[*]`` marks a dummy atom where a bond can be made. Link points
# are named ``A``, ``B``, and so on in SMILES order. For a monomer with two link points, the first is its *head* and
# the second its *tail*. In a linear chain, each new head attaches to the previous tail.
# 
# Here ``styrene`` and ``ethylene`` are ``Monomer`` objects. Their ``system`` attributes contain the molecular
# structures; the displayed dummy atoms show where another monomer can attach.
# 

styrene = Monomer.from_smiles("[*]CC([*])c1ccccc1")
ethylene = Monomer.from_smiles("[*]CC[*]")
print(styrene)
view(styrene.system, direction="along_pca2", width=300, height=300, fixed_atom_size=False, picture_path="picture1.png")


# ## A homopolymer
# 
# The ``monomers`` dictionary maps descriptive names to ``Monomer`` objects. These names identify monomer types in
# link tables, sequences, and the regions of the resulting structure. They are distinct from the ``A`` and ``B``
# link-point names *within* each monomer.
# 
# ``homopolymer_links(monomers, key)`` creates a head-to-tail link table for the selected monomer type, allowing
# chain growth at either end. The example below selects ``"styrene"`` and displays the resulting links.
# 

from scm.structures.builder.polymer import homopolymer_links

monomers = {"styrene": styrene, "ethylene": ethylene}
links = homopolymer_links(monomers, "styrene")
pprint(links)


# Pass the dictionary, link table, and desired number of monomers to ``build_polymer``. ``seed`` makes the
# random choices reproducible. The result is a ``ChemicalSystem``; here we print its formula and view its geometry.
# Free link points at the chain ends are capped with hydrogen by default.
# 

polystyrene = build_polymer(monomers, links, 20, seed=1)
print(polystyrene.formula())
view(polystyrene, direction="along_pca3", width=500, height=300, show_regions=True, picture_path="picture2.png")


# By default the builder tries dihedral angles from low to high steric energy, giving a relatively extended
# chain. With ``temperature`` in kelvin, it samples dihedrals from a Boltzmann distribution, giving more varied
# conformations. Backbone bonds inside each monomer can rotate as well. ``optimize="uff"`` relaxes close contacts after building the chain.
# 

coil = build_polymer(monomers, links, 20, temperature=300, seed=2, optimize="uff")
view(coil, direction="along_pca3", width=500, height=300, show_regions=True, picture_path="picture3.png")


# ## Tacticity
# 
# For this styrene monomer, ``with_mirror_images`` adds its mirror image under the key ``"styrene*"``. The
# ``tacticity_links`` helper then constructs the joins needed for an isotactic, syndiotactic, or atactic chain.
# The builder puts atoms from each monomer type in a region, so ``show_regions=True`` helps reveal their order.
# 

from scm.structures.builder.polymer import tacticity_links, with_mirror_images

tactic_monomers = with_mirror_images({"styrene": styrene})
print(tactic_monomers.keys())
syndiotactic_links = tacticity_links(tactic_monomers, "styrene", "syndiotactic")
pprint(syndiotactic_links)


syndiotactic = build_polymer(tactic_monomers, syndiotactic_links, 20, seed=2)
print(syndiotactic.region_names)
view(syndiotactic, direction="along_pca3", width=500, height=300, show_regions=True, picture_path="picture4.png")


# ## Copolymers from a link table
# 
# A link table defines which monomers can join at each free link point and gives their relative weights. It
# determines each addition from the current chain end rather than prescribing the order of the entire chain.
# The following three examples use the same styrene and ethylene monomers with different link tables. Printing
# each table shows the allowed joins; colored regions in the resulting structure show where the monomers ended up.
# 

# ### Alternating copolymer
# 
# ``alternating_links`` permits a styrene unit to join an ethylene unit and vice versa. The chain therefore
# alternates between the two monomer types.
# 

from scm.structures.builder.polymer import alternating_links

alternating_link_table = alternating_links(monomers, "styrene", "ethylene")
pprint(alternating_link_table)
alternating_chain = build_polymer(monomers, alternating_link_table, 40, seed=2)
print(alternating_chain.formula())
view(alternating_chain, direction="along_pca3", width=500, height=300, show_regions=True, picture_path="picture5.png")


# ### Statistical copolymer
# 
# ``statistical_links`` chooses each new monomer from the supplied fractions, independently of the current chain
# end. Here styrene has a probability of 0.25 at each addition. The actual fraction in a finite chain can differ
# from that value.
# 

from scm.structures.builder.polymer import statistical_links

statistical_link_table = statistical_links(monomers, {"styrene": 0.25, "ethylene": 0.75})
pprint(statistical_link_table)
statistical_chain = build_polymer(monomers, statistical_link_table, 40, seed=2)
print(statistical_chain.formula())
view(statistical_chain, direction="along_pca3", width=500, height=300, show_regions=True, picture_path="picture6.png")


# ### Copolymer from reactivity ratios
# 
# ``copolymer_links`` uses monomer fractions in the feed and reactivity ratios for a terminal model: the next
# addition depends on the monomer at the chain end. Ratios much greater than one favor successive units of the
# same type, giving a blockier chain. Here both feed fractions are 0.5 and both ratios are 10.
# 

from scm.structures.builder.polymer import copolymer_links

terminal_link_table = copolymer_links(
    monomers=monomers,
    feed={"styrene": 0.5, "ethylene": 0.5},
    reactivity={"styrene": 10.0, "ethylene": 10.0},
)
pprint(terminal_link_table)
terminal_chain = build_polymer(monomers, terminal_link_table, 40, seed=2)
print(terminal_chain.formula())
view(terminal_chain, direction="along_pca3", width=500, height=300, show_regions=True, picture_path="picture7.png")


# ## Sequences
# 
# For a specified block order or another pattern that changes along the chain, use a sequence instead of a link
# table. A sequence is a small tree describing the chain's pattern. ``Repeat`` repeats a monomer or another
# sequence node, while ``Seq`` joins its children in order. In the example below, ``Repeat("styrene", 8)`` gives a
# block of eight styrene units. The ``sequence=`` argument takes the place of a link table and repeat count.
# 

from scm.structures.builder.polymer import Repeat, Seq

sequence = Seq([Repeat("styrene", 8), Repeat("ethylene", 16), Repeat("styrene", 8)])
triblock = build_polymer(monomers, sequence=sequence, temperature=300, seed=1)
view(triblock, direction="along_pca3", width=500, height=300, show_regions=True, picture_path="picture8.png")


# Sequence nodes can be nested. ``Poisson(6)`` draws a block length with mean 6 (at least one unit),
# so repeating a pair of such blocks produces a multiblock with variable lengths.
# 

from scm.structures.builder.polymer import Poisson

multiblock_sequence = Repeat(Seq([Repeat("ethylene", Poisson(6)), Repeat("styrene", Poisson(3))]), 4)
multiblock_chain = build_polymer(monomers, sequence=multiblock_sequence, seed=3)
print(multiblock_chain.formula())
view(multiblock_chain, direction="along_pca3", width=500, height=300, show_regions=True, picture_path="picture9.png")


# ``Gradient("ethylene", "styrene", 30)`` gives 30 units while the probability of styrene rises
# along the chain. Unlike the multiblock, this specifies a gradual change in composition.
# 

from scm.structures.builder.polymer import Gradient

gradient_sequence = Gradient("ethylene", "styrene", 30)
gradient_chain = build_polymer(monomers, sequence=gradient_sequence, seed=3)
print(gradient_chain.formula())
view(gradient_chain, direction="along_pca3", width=500, height=300, show_regions=True, picture_path="picture10.png")


# ## Branched polymers
# 
# A monomer with more than two link points can start side chains. ``Star`` grows an arm from each link point of a
# core. ``Comb`` repeats a backbone monomer and attaches side chains at its branch point. The ``graft_styrene``
# monomer below has a third link point on its phenyl ring; its ``head`` and ``tail`` identify the two points used
# for the backbone. Each arm or side chain is itself a sequence node.
# 

from scm.structures.builder.polymer import Star

branched_monomers = {
    "ethylene": ethylene,
    "core": Monomer.from_smiles("C(C[*])(C[*])(C[*])C[*]"),
    "graft_styrene": Monomer.from_smiles("[*]CC([*])c1ccc([*])cc1", head="A", tail="B"),
}
star_sequence = Star("core", Repeat("ethylene", 8))
star = build_polymer(branched_monomers, sequence=star_sequence, temperature=300, seed=1)
view(star, direction="along_pca3", width=400, height=400, show_regions=True, picture_path="picture11.png")


from scm.structures.builder.polymer import Comb

comb_sequence = Comb("graft_styrene", Repeat("ethylene", 5), 12, every=3)
comb = build_polymer(branched_monomers, sequence=comb_sequence, seed=1)
view(comb, direction="along_pca3", width=500, height=300, show_regions=True, picture_path="picture12.png")


# ## Caps and following the growth
# 
# By default, free link points become hydrogens. The ``cap`` argument can instead supply a one-link-point
# ``Monomer`` such as methyl, another element, or ``None`` to keep the dummy atoms. With ``trajectory``, the builder
# writes each growth step to an ``.rkf`` file for AMSmovie; ``progress`` receives updates as the chain grows.
# 

from scm.structures.builder.polymer import print_progress

methyl = Monomer.from_smiles("[*]C")
capped = build_polymer(
    monomers,
    homopolymer_links(monomers, "styrene"),
    6,
    cap=methyl,
    seed=1,
    trajectory="polystyrene_growth.rkf",
    progress=print_progress,
)
view(capped, direction="along_pca3", width=400, height=300, show_regions=True, picture_path="picture13.png")