def test1(): print('generating graphene structure') graphene = GrapheneGenerator(armchair_edge_length=5, zigzag_edge_length=5) lattice = graphene.lattice print('graphene.bounds:\n{}'.format(graphene.bounds)) print('graphene.centroid:\n{}'.format(graphene.centroid)) print('graphene.lattice:\n{}'.format(lattice)) print('graphene.lattice.a1:\n{}'.format(lattice.a1)) print('graphene.lattice.a2:\n{}'.format(lattice.a2)) print('graphene.lattice.a3:\n{}'.format(lattice.a3)) print('graphene.lattice.orientation_matrix:\n{}'.format( lattice.orientation_matrix)) print('rotating graphene') graphene.rotate(angle=-np.pi/2, axis='x') print('graphene.bounds:\n{}'.format(graphene.bounds)) print('graphene.centroid:\n{}'.format(graphene.centroid)) print('graphene.lattice:\n{}'.format(lattice)) print('graphene.lattice.a1:\n{}'.format(lattice.a1)) print('graphene.lattice.a2:\n{}'.format(lattice.a2)) print('graphene.lattice.a3:\n{}'.format(lattice.a3)) print('graphene.lattice.orientation_matrix:\n{}'.format( lattice.orientation_matrix)) assert_true(np.allclose(lattice.angles, 3 * [90.0])) lattice_region = Cuboid(pmax=lattice.lengths) # lattice_region = Parallelepiped(u=lattice.a * xhat, # v=lattice.b * yhat, # w=lattice.c * zhat) assert_equal(lattice_region.a, lattice.a) assert_equal(lattice_region.b, lattice.b) assert_equal(lattice_region.c, lattice.c) print('lattice_region:\n{}'.format(lattice_region)) print('lattice_region.centroid:\n{}'.format(lattice_region.centroid)) print('\nrotating lattice_region') lattice_region.rotate(transform_matrix=lattice.orientation_matrix) # assert_equal(lattice_region.a, lattice.a) # assert_equal(lattice_region.b, lattice.b) # assert_equal(lattice_region.c, lattice.c) print('lattice_region:\n{}'.format(lattice_region)) print('lattice_region.centroid:\n{}'.format(lattice_region.centroid)) print('\ncentering lattice_region on graphene centroid') tvec = Vector(Point(graphene.centroid) - lattice_region.centroid) lattice_region.translate(tvec) # assert_equal(lattice_region.a, lattice.a) # assert_equal(lattice_region.b, lattice.b) # assert_equal(lattice_region.c, lattice.c) print('lattice_region:\n{}'.format(lattice_region)) print('lattice_region.centroid:\n{}'.format(lattice_region.centroid)) bounding_box = generate_bounding_box(from_lattice=lattice, center=graphene.centroid, verbose=True) print('bounding_box:\n{}'.format(bounding_box)) assert_equal(bounding_box, lattice_region) print('lattice_region.lengths: {}, {}, {}'.format( lattice_region.a, lattice_region.b, lattice_region.c))
def test_cuboid(): r = Cuboid() assert_is_instance(r, Cuboid) assert_equal(r.centroid, Point([0.5, 0.5, 0.5])) assert_true(np.allclose(r.measure, 1.0)) assert_true(r.contains(r.centroid)) assert_false(r.contains([-0.1, 0, 0])) assert_is_instance(r.centroid, Point)
def bounds(self): """Return bounds of `Molecules`.""" return Cuboid(pmin=[self.x.min(), self.y.min(), self.z.min()], pmax=[self.x.max(), self.y.max(), self.z.max()])
def generate(self): """Generate structure data.""" super().generate() if self.L0 is not None and self.fix_Lz: Lz_cutoff = 10 * self.L0 + 1 region_bounds = Cuboid(pmin=Point([-np.inf, -np.inf, 0]), pmax=Point([np.inf, np.inf, Lz_cutoff])) self.atoms.clip_bounds(region_bounds)
def _parse_bounding_box(self): self.bounding_box = Cuboid() for dim in ('x', 'y', 'z'): bounds = \ self.header_data[' '.join([dim + lim for lim in ('lo', 'hi')])] [ setattr(self.bounding_box, dim + lim, value) for lim, value in zip(('min', 'max'), bounds) ] self.kwargs['bounding_box'] = self.bounding_box
def bounds(self): """Bounds of `Atoms`. Returns ------- :class:`~sknano.core.geometric_regions.Cuboid`""" return Cuboid(pmin=[self.x.min(), self.y.min(), self.z.min()], pmax=[self.x.max(), self.y.max(), self.z.max()])
def test1(): print('generating graphene structure') graphene = GrapheneGenerator(armchair_edge_length=5, zigzag_edge_length=5) lattice = graphene.lattice print('graphene.bounds:\n{}'.format(graphene.bounds)) print('graphene.centroid:\n{}'.format(graphene.centroid)) print('graphene.lattice:\n{}'.format(lattice)) print('graphene.lattice.a1:\n{}'.format(lattice.a1)) print('graphene.lattice.a2:\n{}'.format(lattice.a2)) print('graphene.lattice.a3:\n{}'.format(lattice.a3)) print('graphene.lattice.orientation_matrix:\n{}'.format( lattice.orientation_matrix)) print('rotating graphene') graphene.rotate(angle=-np.pi / 2, axis='x') print('graphene.bounds:\n{}'.format(graphene.bounds)) print('graphene.centroid:\n{}'.format(graphene.centroid)) print('graphene.lattice:\n{}'.format(lattice)) print('graphene.lattice.a1:\n{}'.format(lattice.a1)) print('graphene.lattice.a2:\n{}'.format(lattice.a2)) print('graphene.lattice.a3:\n{}'.format(lattice.a3)) print('graphene.lattice.orientation_matrix:\n{}'.format( lattice.orientation_matrix)) assert_true(np.allclose(lattice.angles, 3 * [90.0])) lattice_region = Cuboid(pmax=lattice.lengths) # lattice_region = Parallelepiped(u=lattice.a * xhat, # v=lattice.b * yhat, # w=lattice.c * zhat) assert_equal(lattice_region.a, lattice.a) assert_equal(lattice_region.b, lattice.b) assert_equal(lattice_region.c, lattice.c) print('lattice_region:\n{}'.format(lattice_region)) print('lattice_region.centroid:\n{}'.format(lattice_region.centroid)) print('\nrotating lattice_region') lattice_region.rotate(transform_matrix=lattice.orientation_matrix) # assert_equal(lattice_region.a, lattice.a) # assert_equal(lattice_region.b, lattice.b) # assert_equal(lattice_region.c, lattice.c) print('lattice_region:\n{}'.format(lattice_region)) print('lattice_region.centroid:\n{}'.format(lattice_region.centroid)) print('\ncentering lattice_region on graphene centroid') tvec = Vector(Point(graphene.centroid) - lattice_region.centroid) lattice_region.translate(tvec) # assert_equal(lattice_region.a, lattice.a) # assert_equal(lattice_region.b, lattice.b) # assert_equal(lattice_region.c, lattice.c) print('lattice_region:\n{}'.format(lattice_region)) print('lattice_region.centroid:\n{}'.format(lattice_region.centroid)) bounding_box = generate_bounding_box(from_lattice=lattice, center=graphene.centroid, verbose=True) print('bounding_box:\n{}'.format(bounding_box)) assert_equal(bounding_box, lattice_region) print('lattice_region.lengths: {}, {}, {}'.format(lattice_region.a, lattice_region.b, lattice_region.c))
def test2(): graphene = GrapheneGenerator(armchair_edge_length=5, zigzag_edge_length=5) graphene.rotate(angle=-np.pi/2, axis='x') lattice = graphene.lattice bounding_box1 = Cuboid() bounding_box2 = Cuboid() lattice_region1 = Cuboid(pmax=lattice.lengths) bounding_box1.pmin = lattice_region1.pmin bounding_box1.pmax = lattice_region1.pmax a, b, c = lattice.lengths cos_alpha, cos_beta, cos_gamma = np.cos(np.radians(lattice.angles)) lx = a xy = b * cos_gamma xz = c * cos_beta ly = np.sqrt(b ** 2 - xy ** 2) yz = (b * c * cos_alpha - xy * xz) / ly lz = np.sqrt(c ** 2 - xz ** 2 - yz ** 2) lattice_region2 = \ Parallelepiped(u=Vector(lattice.ortho_matrix[:, 0].A.flatten()), v=Vector(lattice.ortho_matrix[:, 1].A.flatten()), w=Vector(lattice.ortho_matrix[:, 2].A.flatten())) xlo, ylo, zlo = lattice_region2.o print('xy={}, xz={}, yz={}'.format(xy, xz, yz)) print('lx={}, ly={}, lz={}'.format(lx, ly, lz)) print('xlo={}, ylo={}, zlo={}'.format(xlo, ylo, zlo)) xlo_bound = xlo + min(0.0, xy, xz, xy + xz) xhi_bound = xlo + lx + max(0.0, xy, xz, xy + xz) ylo_bound = ylo + min(0.0, yz) yhi_bound = ylo + ly + max(0.0, yz) zlo_bound = zlo zhi_bound = zlo + lz bounding_box2.pmin = [xlo_bound, ylo_bound, zlo_bound] bounding_box2.pmax = [xhi_bound, yhi_bound, zhi_bound] print(bounding_box1) print(bounding_box2) [bounding_box.rotate(transform_matrix=lattice.orientation_matrix) for bounding_box in (bounding_box1, bounding_box2)] [bounding_box.translate(Vector(Point(graphene.centroid) - bounding_box.centroid)) for bounding_box in (bounding_box1, bounding_box2)] [assert_true(bounding_box.pmin <= bounding_box.pmax) for bounding_box in (bounding_box1, bounding_box2)] assert_equal(bounding_box1, bounding_box2) print(bounding_box1) print(bounding_box2)
def read_snapshot(self, f): try: snapshot = Snapshot(self.trajectory) f.readline() snapshot.timestep = int(f.readline().strip().split()[0]) f.readline() snapshot.Natoms = int(f.readline().strip()) snapshot.atom_selection = np.zeros(snapshot.Natoms, dtype=bool) item = f.readline().strip() try: snapshot.boxstr = item.split('BOUNDS')[1].strip() except IndexError: snapshot.boxstr = '' snapshot.triclinic = False snapshot.bounding_box = Cuboid() if 'xy' in snapshot.boxstr: snapshot.triclinic = True for dim, tilt_factor in zip(('x', 'y', 'z'), ('xy', 'xz', 'yz')): bounds = f.readline().strip().split() setattr(snapshot, dim + 'lo', float(bounds[0])) setattr(snapshot, dim + 'hi', float(bounds[1])) setattr(snapshot.bounding_box, dim + 'min', getattr(snapshot, dim + 'lo')) setattr(snapshot.bounding_box, dim + 'max', getattr(snapshot, dim + 'hi')) try: setattr(snapshot, tilt_factor, float(bounds[2])) except IndexError: setattr(snapshot, tilt_factor, 0.0) if not self.dumpattrs: xflag = yflag = zflag = None attrs = f.readline().strip().split()[2:] for i, attr in enumerate(attrs): if attr in ('x', 'xu', 'xs', 'xsu'): self.dumpattrs['x'] = i if attr in ('x', 'xu'): xflag = False else: xflag = True elif attr in ('y', 'yu', 'ys', 'ysu'): self.dumpattrs['y'] = i if attr in ('y', 'yu'): yflag = False else: yflag = True elif attr in ('z', 'zu', 'zs', 'zsu'): self.dumpattrs['z'] = i if attr in ('z', 'zu'): zflag = False else: zflag = True else: self.dumpattrs[attr] = i self.scale_original = None if all([flag is False for flag in (xflag, yflag, zflag)]): self.scale_original = False if all([flag for flag in (xflag, yflag, zflag)]): self.scale_original = True self.atomattrs = \ sorted(self.dumpattrs, key=self.dumpattrs.__getitem__) self.attr_dtypes = [attr_dtypes[attr] if attr in attr_dtypes else float for attr in self.atomattrs] if self.attrmap is not None: self.remap_atomattr_names(self.attrmap) self.attrmap = None self.unknown_attrs = \ {attr: self.atomattrs.index(attr) for attr in set(self.atomattrs) - set(dir(Atom()))} [self.atomattrs.remove(attr) for attr in self.unknown_attrs] else: f.readline() snapshot.atomattrs = self.atomattrs snapshot.attr_dtypes = self.attr_dtypes atoms = \ np.zeros((snapshot.Natoms, len(self.atomattrs)), dtype=float) for n in range(snapshot.Natoms): line = [float(value) for col, value in enumerate(f.readline().strip().split()) if col not in self.unknown_attrs.values()] atoms[n] = line snapshot.atoms = atoms return snapshot except IndexError: return None
def test2(): graphene = GrapheneGenerator(armchair_edge_length=5, zigzag_edge_length=5) graphene.rotate(angle=-np.pi / 2, axis='x') lattice = graphene.lattice bounding_box1 = Cuboid() bounding_box2 = Cuboid() lattice_region1 = Cuboid(pmax=lattice.lengths) bounding_box1.pmin = lattice_region1.pmin bounding_box1.pmax = lattice_region1.pmax a, b, c = lattice.lengths cos_alpha, cos_beta, cos_gamma = np.cos(np.radians(lattice.angles)) lx = a xy = b * cos_gamma xz = c * cos_beta ly = np.sqrt(b**2 - xy**2) yz = (b * c * cos_alpha - xy * xz) / ly lz = np.sqrt(c**2 - xz**2 - yz**2) lattice_region2 = \ Parallelepiped(u=Vector(lattice.ortho_matrix[:, 0].A.flatten()), v=Vector(lattice.ortho_matrix[:, 1].A.flatten()), w=Vector(lattice.ortho_matrix[:, 2].A.flatten())) xlo, ylo, zlo = lattice_region2.o print('xy={}, xz={}, yz={}'.format(xy, xz, yz)) print('lx={}, ly={}, lz={}'.format(lx, ly, lz)) print('xlo={}, ylo={}, zlo={}'.format(xlo, ylo, zlo)) xlo_bound = xlo + min(0.0, xy, xz, xy + xz) xhi_bound = xlo + lx + max(0.0, xy, xz, xy + xz) ylo_bound = ylo + min(0.0, yz) yhi_bound = ylo + ly + max(0.0, yz) zlo_bound = zlo zhi_bound = zlo + lz bounding_box2.pmin = [xlo_bound, ylo_bound, zlo_bound] bounding_box2.pmax = [xhi_bound, yhi_bound, zhi_bound] print(bounding_box1) print(bounding_box2) [ bounding_box.rotate(transform_matrix=lattice.orientation_matrix) for bounding_box in (bounding_box1, bounding_box2) ] [ bounding_box.translate( Vector(Point(graphene.centroid) - bounding_box.centroid)) for bounding_box in (bounding_box1, bounding_box2) ] [ assert_true(bounding_box.pmin <= bounding_box.pmax) for bounding_box in (bounding_box1, bounding_box2) ] assert_equal(bounding_box1, bounding_box2) print(bounding_box1) print(bounding_box2)