def __init__(self, surface, chains, n_chains, seed, fractions=None, backfill=None, **kwargs): super(SurfaceMonolayer, self).__init__() if surface.name == 'SilicaInterface' and n_chains > 0: pattern = mb.Random2DPattern(n_chains, seed=seed) elif surface.name == 'SilicaTip': shift = 0.25 surface.translate_to([0, 0, 0]) surface.translate([0, 0, -1 * min(surface.xyz[:,2]) - shift]) radius = max(surface.xyz[:,2]) - min(surface.xyz[:,2]) pattern = RandomHemispherePattern(n=n_chains, seed=seed) pattern.scale(radius) elif surface.name == 'SilicaAsperity': pass else: pattern = mb.Random2DPattern(n_chains, seed=seed) if chains and n_chains > 0: monolayer = Monolayer(surface=surface, chains=chains, pattern=pattern, fractions=fractions, backfill=backfill, **kwargs) else: monolayer = Monolayer(surface=surface, chains=backfill, guest_port_name='up', **kwargs) self.add(monolayer)
def test_apply_to_compound(self, betacristobalite, propyl, ch3): pattern = mb.Random2DPattern(90) chains, backfills = pattern.apply_to_compound( guest=propyl, host=betacristobalite, backfill=ch3) assert len(chains) == 90 assert len(backfills) == 10 with pytest.raises(AssertionError): pattern = mb.Random2DPattern(101) chains, backfills = pattern.apply_to_compound( guest=propyl, host=betacristobalite, backfill=ch3)
def build_monolayer(chain_length, n_molecules, **kwargs): from mbuild.examples import AlkaneMonolayer pattern = mb.Random2DPattern(n_molecules) monolayer = AlkaneMonolayer(pattern, tile_x=1, tile_y=1, chain_length=chain_length) monolayer.name = 'alkane_n-{}_l-{}'.format(n_molecules, chain_length) mb.translate(monolayer, [0, 0, 2]) return monolayer
def __init__(self, surface, chains, fractions=None, backfill=None, pattern=None, tile_x=1, tile_y=1, rotate=True, seed=12345, **kwargs): super(Monolayer, self).__init__() # Replicate the surface. tiled_compound = mb.recipes.TiledCompound(surface, n_tiles=(tile_x, tile_y, 1)) self.add(tiled_compound, label='tiled_surface') if pattern is None: # Fill the surface. pattern = mb.Random2DPattern(len(tiled_compound.referenced_ports())) if isinstance(chains, mb.Compound): chains = [chains] if fractions: fractions = list(fractions) if len(chains) != len(fractions): raise ValueError("Number of fractions does not match the number" " of chain types provided") n_chains = len(pattern.points) # Attach chains of each type to binding sites based on # respective fractions. for chain, fraction in zip(chains[:-1], fractions[:-1]): # Create sub-pattern for this chain type subpattern = deepcopy(pattern) n_points = int(round(fraction * n_chains)) pick = np.random.choice(subpattern.points.shape[0], n_points, replace=False) points = subpattern.points[pick] subpattern.points = points # Remove now-occupied points from overall pattern pattern.points = np.array([point for point in pattern.points.tolist() if point not in subpattern.points.tolist()]) # Attach chains to the surface attached_chains, _ = subpattern.apply_to_compound( guest=chain, host=self['tiled_surface'], backfill=None, **kwargs) self.add(attached_chains) else: warn("\n No fractions provided. Assuming a single chain type.") attached_chains, backfills = pattern.apply_to_compound(guest=chains[-1], host=self['tiled_surface'], backfill=backfill, **kwargs) self.add(attached_chains) self.add(backfills) if rotate: np.random.seed(seed) for chain in attached_chains: rotation = np.random.random() * np.pi * 2.0 chain.spin(rotation, [0, 0, 1])
def build_monolayer(chain_length, n_molecules, **kwargs): from mbuild.examples import AlkaneMonolayer pattern = mb.Random2DPattern(n_molecules) monolayer = AlkaneMonolayer(pattern, tile_x=1, tile_y=1, chain_length=chain_length) monolayer.name = 'alkane_n-{}_l-{}'.format(n_molecules, chain_length) mb.translate(monolayer, [0, 0, 2]) box = monolayer.boundingbox monolayer.periodicity += np.array([0, 0, 5 * box.lengths[2]]) return monolayer
def test_random_2d_seed(self): pattern_a = mb.Random2DPattern(100, seed=12345) pattern_b = mb.Random2DPattern(100, seed=12345) assert np.array_equal(pattern_a, pattern_b)
def test_random_2d(self): pattern = mb.Random2DPattern(100) assert len(pattern) == 100
def alkane_monolayer(self): from mbuild.examples import AlkaneMonolayer pattern = mb.Random2DPattern(50, seed=1) monolayer = AlkaneMonolayer(pattern=pattern) return monolayer