def load_diffs(): date_2011 = datetime.datetime(year=2011, month=1, day=1) date_2012 = datetime.datetime(year=2012, month=1, day=1) diff_2012 = [ Diff(DiffType.ADD, position=4, add='u'), Diff(DiffType.REMOVE, position=4, remove=1) ] title_12_subsections = [ Structure(1, Representation.ROMAN, 'Part'), Structure(3, Representation.ROMAN, 'Part') ] title_12_subsections[0].subsections = [ Structure(1, Representation.UPPERCASE, 'Section'), Structure(2, Representation.UPPERCASE, 'Section') ] title_12 = Structure(12, Representation.NUMERIC, 'Title', dates=[date_2011, date_2012], texts={ date_2011: 'Hello', date_2012: 'Hellu' }, diffs={ date_2011: [], date_2012: diff_2012 }, subsections=title_12_subsections) return {12: title_12}
def load_us_code(src): ## Invariant: for the meta-structure, order = title #. file_ = open(src, 'r') file_.close() mar1 = datetime.date(2018, 3, 1) structure = Structure('Title', '18', 18, subsections=[Structure('Section', '2', 2, dates=[mar1], texts={mar1:''}, diffs={mar1:[]})]) structure = Structure('US Code', '0', 0, subsections=[structure]) return structure
def update(self, event): new = False if 'structures' in event: # print('structure update') for id_, value in event['structures'].iteritems(): # print('%s -- %s' % (id_, value)) if id_ in self._structures: self._structures[id_].update(value) else: new = True self._structures.update( {id_: Structure(id_, data=value, nestapi=self)}) if 'devices' in event: # print(event) for cat, device in event['devices'].iteritems(): for k, v in device.iteritems(): k = bytes(k) if k in self._devices: self._devices[k].update(v) else: new = True self._devices.update({k: get_device(cat)( k, data=v, nestapi=self)}) if new: self.upnp_devices = self._structures.values() +\ self._devices.values() if self.parent: self.parent.update()
def domain_defect(complex, strand_num, domain_num, structure): """ Calculates the defect of the binding of the given domain in the complex against the domain's expected binding given in the specified target structure. If the domain is not completely bound or unbound, the results are not very meaningful. The defect is calculated as a fraction of the domain's length.""" from structure import Structure strandlist = [lst[:] for lst in structure.to_strandlist()] strands = structure.strands domain_start = sum( [d.length for d in strands[strand_num].domains[:domain_num]]) domain_end = domain_start + strands[strand_num].domains[domain_num].length n = 0 for strand_n, strand_struct in enumerate(strandlist): for i, bound in enumerate(strand_struct): is_domain = (strand_n == strand_num and domain_start <= i < domain_end) is_bound_to_domain = (bound != None and bound != '?' and bound[0] == strand_num and domain_start <= bound[1] < domain_end) if not is_domain and not is_bound_to_domain: strandlist[strand_n][i] = '?' else: n += 1 new_struct = Structure(structure=strandlist, strands=structure.strands) return defect(complex, new_struct) / float(n)
def _parseNotes(self): """Parses the notes of the file (if it has any) and stores them in self.notes.""" self.notes = list() noteSecs = self.getSectionsByType(SECTION_TYPES["SHT_NOTE"]) structure = "Elf32_Nhdr" if self.is32() else "Elf64_Nhdr" for sec in noteSecs: offset = 0 if sec.dataLen == 0: continue data = sec.read() while True: try: s = Structure(structure) s._unpack(data[offset:offset + s.sizeof()]) offset += s.sizeof() s._keys.append(["name"]) s._keys.append(["desc"]) s._fieldOffsets["name"] = s.sizeof() s._fieldOffsets[ "desc"] = s._fieldOffsets["name"] + s.n_namesz s.name = data[offset:offset + s.n_namesz].strip(b"\0") offset += s.n_namesz s.desc = data[offset:offset + s.n_descsz].strip(b"\0") offset += s.n_descsz self.notes.append(s) except: break
def get_result(self, result_name, sim): result = obj() input = self.input if result_name == 'structure': # get structure from CONTCAR ccfile = os.path.join(self.locdir, self.identifier + '.CONTCAR') if not os.path.exists(ccfile): self.error( 'CONTCAR file does not exist for relax simulation at ' + self.locdir) #end if contcar = Poscar(ccfile) structure = Structure() if contcar.elem is not None: structure.read_poscar(ccfile) else: elem, elem_count = self.system.structure.order_by_species() structure.read_poscar(ccfile, elem=elem) #end if if input.poscar.dynamic is not None: structure.freeze(input.poscar.dynamic, negate=True) #end if result.structure = structure else: self.error('ability to get result ' + result_name + ' has not been implemented') #end if return result
def make_movie(self, filename, filepath=None): if 'structures' in self: from structure import Structure if filepath == None: filepath = os.path.join(self.abspath, filename) else: filepath = os.path.join(filepath, filename) #end if movie = '' structures = self.structures print structures aA = convert(self.input.system['celldm(1)'], 'B', 'A') cell = self.input.cell_parameters.vectors for i in range(len(structures)): s = structures[i] struct = Structure(elem=s.atoms, pos=s.positions, axes=cell, scale=aA, units='A') struct = struct.tile(2, 2, 2) ss = struct.write_xyz() movie += ss open(filepath, 'w').write(movie)
def groFrameIterator(stream): """Read a GRO file stream frame by frame""" if type(stream) == str: if stream.endswith(".gz"): stream = gzip.open(stream) else: stream = open(stream) # For efficiency, we need to know the precision precision = None while True: try: title = stream.next() except StopIteration: break natoms = stream.next().strip() if not natoms: break natoms = int(natoms) if not precision: a = stream.next() prec = len(a.split(".")[-2]) + 1 atoms = [groAtom(a, precision)] atoms.extend( [groAtom(stream.next(), precision) for i in range(natoms - 1)]) else: atoms = [groAtom(stream.next(), precision) for i in range(natoms)] box = groBoxRead(stream.next()) yield Structure(title=title, atoms=atoms, box=box)
def conventional(self, symprec=1e-5): """ conventional structure Arugments: symprec (symmetry tolerance): distance tolerance in Cartesian coordinates to find crystal symmetry """ cell = self.structure.formatting('cell') cell = (cell['lattice'], cell['positions'], cell['numbers']) # method 1 lattice, scaled_positions, numbers = spglib.standardize_cell( cell, symprec=symprec) newcell = (lattice, scaled_positions, numbers) # method 2 #newcell=spglib.standardize_cell(cell, symprec=symprec) if newcell == None: raise StructureFactoryError('The search is failed') else: poscar = self.__toPOSCAR(newcell) self.structure.pop() # delete old object self.structure = Structure().append(poscar) return self.structure
def read_xsf(self, filepath, component=None): component = self.process_component_name(component) vlog('Reading density data from XSF file for component "{}"'.format( component), time=True) if isinstance(filepath, XsfFile): vlog('XSF file already loaded, reusing data.') xsf = filepath copy_values = True else: vlog('Loading data from file', n=1, time=True) vlog('file location: {}'.format(filepath), n=2) vlog('memory before: ', n=2, mem=True) xsf = XsfFile(filepath) vlog('load complete', n=2, time=True) vlog('memory after: ', n=2, mem=True) copy_values = False #end if # read structure if not self.has_attribute('structure'): vlog('Reading structure from XSF data', n=1, time=True) s = Structure() s.read_xsf(xsf) self.set_attribute('structure', s) #end if # read grid if not self.has_attribute('grid'): vlog('Reading grid from XSF data', n=1, time=True) g = read_grid(xsf) self.set_attribute('grid', g) self.set_attribute('distance_units', 'B') #end if # read values xsf.remove_ghost() d = xsf.get_density() values = d.values_noghost.ravel() if copy_values: values = values.copy() #end if # create grid function for component vlog('Constructing grid function from XSF data', n=1, time=True) f = grid_function( type='parallelotope', grid=self.grid, values=values, copy=False, ) self.set_attribute(component, f) self.set_attribute('distance_units', 'A') vlog('Read complete', n=1, time=True) vlog('Current memory:', n=1, mem=True)
def __init__(self,structure=None,net_charge=0,net_spin=0,particles=None,**valency): self.pseudized = False if structure is None: self.structure = Structure() else: self.structure = structure #end if if particles is None: self.particles = Particles() else: self.particles = particles.copy() #end if self.folded_system = None if self.structure.has_folded(): if self.structure.is_tiled(): vratio = structure.volume()/structure.folded_structure.volume() ncells = int(round(vratio)) if abs(vratio-ncells)>1e-4: self.error('volume of system does not divide evenly into folded system') #end if if net_charge%ncells!=0: self.error('net charge of system does not divide evenly into folded system') #end if if isinstance(net_spin,str): net_spin_fold = net_spin elif net_spin%ncells!=0: self.error('net_spin of system does not divide evenly into folded system') else: net_spin_fold = net_spin/ncells #end if net_charge_fold = net_charge/ncells elif not self.structure.has_axes(): # folded molecule # net charge/spin are not physically meaningful # for a point group folded molecule # set them to safe values; they will not be used later net_charge_fold = 0 net_spin_fold = 'low' else: self.error('folded structure is not correctly integrated with full structure\nfolded physical system cannot be constructed') #end if self.folded_system = PhysicalSystem( structure = structure.folded_structure, net_charge = net_charge_fold, net_spin = net_spin_fold, particles = particles, **valency ) #end if self.valency_in = obj(**valency) self.net_charge_in = net_charge self.net_spin_in = net_spin self.update_particles(clear=False) self.check_folded_system()
def example_structure_h4(): # hydrogen at rs=1.31 from structure import Structure natom = 4 alat = 3.3521298178767225 axes = alat * np.eye(3) elem = ['H'] * natom pos = np.array([[0, 0, 0], [alat / 2., 0, 0], [0, alat / 2, 0], [0, 0, alat / 2]]) s1 = Structure(axes=axes, elem=elem, pos=pos, units='B') return s1
def __init__(self, structure=None, net_charge=0, net_spin=0, particles=None, **valency): self.pseudized = False if structure is None: self.structure = Structure() else: self.structure = structure #end if if particles is None: self.particles = Particles() else: self.particles = particles.copy() #end if self.folded_system = None if self.structure.folded_structure != None: vratio = structure.volume() / structure.folded_structure.volume() ncells = int(round(vratio)) if abs(vratio - ncells) > 1e-4: self.error( 'volume of system does not divide evenly into folded system' ) #end if if net_charge % ncells != 0: self.error( 'net charge of system does not divide evenly into folded system' ) #end if if net_spin % ncells != 0: self.error( 'net_spin of system does not divide evenly into folded system' ) #end if self.folded_system = PhysicalSystem( structure=structure.folded_structure, net_charge=net_charge / ncells, net_spin=net_spin / ncells, particles=particles, **valency) #end if self.valency_in = obj(**valency) self.net_charge_in = net_charge self.net_spin_in = net_spin self.update_particles(clear=False) self.check_folded_system()
def getBooStructure(self): natoms=sum(self.atoms) strcVecs=Structure(self.elements,self.atoms,self.positions,self.lattice).getStructure() keys=strcVecs.keys() booStructure={} for key in keys: vecs=strcVecs[key] boos=np.array([]) for vec in vecs: boos=np.append(boos,Boo(vec).getBoo()) booStructure[key]=boos return booStructure
def __init__(self, url): '''Construct a new Firebase reference from a full Firebase URL.''' self.connection = Connection(url, self) self.base_url = url self.structure = Structure(self) self.subscriptions = {} self.history = [] self.connection.daemon = True self.connection.start() self._keep_alive() atexit.register(self.close) DataRef.__init__(self, self, '')
def pdbFrameIterator(stream): if type(stream) == str: if stream.endswith(".gz"): stream = gzip.open(stream) else: stream = open(stream) title, atoms, box = [], [], [] for i in stream: if i.startswith("ENDMDL"): yield Structure(title="".join(title), atoms=atoms, box=box) title, atoms, box = [], [], None elif i.startswith("TITLE"): title.append(i) elif i.startswith("CRYST1"): box = pdbReadBox(i) elif i.startswith("ATOM") or i.startswith("HETATM"): atoms.append(pdbAtom(i)) if atoms: yield Structure(title="".join(title), atoms=atoms, box=box)
def get_reference_structures(): from generic import obj from structure import Structure if len(reference_structures) == 0: ref_in = get_reference_inputs() ref = reference_structures for name, inputs in ref_in.items(): if 'cell' not in inputs: ref[name] = Structure(**inputs) #end if #end for #end if return obj(reference_structures)
def supercell(self, dim): """ create supercell Arguments: dim: size of supercell. i.e. [2,2,2] Return: object of supercell structure. """ # step: # 1. move atom # 2. change the lattice vector # 3. call formatting # 4. pop old # 5. append new # check dim if (sum(1 for x in dim if x <= 0) >= 1) or (sum( 1 for x in dim if not isinstance(x, int)) >= 1): raise StructureFactoryError('invalid dim') atom_set = self.structure.atoms.all() for i in xrange(0, dim[0]): # x for j in xrange(0, dim[1]): # y for k in xrange(0, dim[2]): # z for atom in atom_set: Atom.objects.create(structure=self.structure, element=atom.element, x=(atom.x + i) / dim[0], y=(atom.y + j) / dim[1], z=(atom.z + k) / dim[2]) # delete old atoms for atom in atom_set: atom.delete() lattice = self.structure.lattice for i in xrange(0, lattice.shape[0]): lattice[i] = lattice[i] * dim[i] self.structure.lattice = lattice self.structure.save() #poscar=self.structure.formatting(isConstraint=constrained) poscar = self.structure.formatting(isConstraint=False) self.structure.pop() # delete old object self.structure = Structure().append(poscar) return self.structure
def sizeof(form): try: from data import structures except: from .data import structures try: from structure import Structure except: from .structure import Structure if isinstance(form, str): form = getattr(structures, form) h = Structure(form) return h.sizeof()
def getBooStructure(self): bondsDics = Structure(self.lattice, self.positions, self.elements, self.atoms).getStructure() keys = bondsDics.keys() #the keys are the atoms. booStructure = {} for key in keys: vecs = bondsDics[key] boos = [] for vec in vecs: boos.append(Boo(vec).getBoo()) booStructure[key] = np.array(boos) return booStructure
def __init__(self, tokenizer, transformer, vocab_tgt, config, mode): self.pms = config['list_puncuation_marks'] self.tokenizer = tokenizer self.transformer = transformer self.mode = mode self.batch_size = config[mode]['batch_size'] self.max_seq_len = config['int_max_length'] self.vocab_tgt = vocab_tgt self.config = config self.extra_vocab = config['list_extra_words'] self.pinyin_sampler = PinYinSampler( list(vocab_tgt.token_to_idx.keys()), self.extra_vocab, self.config) # self.actions = actions self.map_pms_idx = {pm: idx for idx, pm in enumerate(self.pms)} # self.map_actions_idx = { action : idx for idx, action in enumerate(self.actions) } self.structure = Structure() self.data = self._load_cache()[mode]
def getImmediateSurroundings(self, map): surroundings = [] xTile = int(int(self.xPos + 10) / 50) # + 10 is for the base width correction yTile = int(int(self.yPos + 30) / 50) # + 30 is for the hight width correction for y in range(3): for x in range(3): # to prevent us from checking map blocks that dont exist if ((yTile - 1 + y) < len(map) and (yTile - 1 + y) >= 0 and (xTile - 1 + x) < len(map[0]) and (xTile - 1 + x) >= 0): value = map[yTile - 1 + y][xTile - 1 + x] if value is not self.floorTile: surroundings.append( Structure(value, (xTile - 1 + x) * 50, (yTile - 1 + y) * 50, None, False, None)) return surroundings
def detect_domain_and_pattern_stats(column): pattern_dict = {} for idx in range(len(column)): extract_result = extract_cell_pattern(column[idx]) cell_pattern = extract_result[0] if cell_pattern == 'EMPTY': continue value_component = extract_result[1] if cell_pattern in pattern_dict.keys(): structure_instance = pattern_dict.get(cell_pattern) structure_instance.add_match_row_index(idx) structure_instance.add_value_component(value_component) pattern_dict[cell_pattern] = structure_instance else: structure_instance = Structure(cell_pattern) structure_instance.add_match_row_index(idx) structure_instance.add_value_component(value_component) pattern_dict[cell_pattern] = structure_instance return pattern_dict
def get_result(self, result_name, sim): result = obj() input = self.input if result_name == 'structure': # OUTCAR structure is not as precise as CONTCAR structure #pa = self.load_analyzer_image() #elem = input.poscar.elem #elem_count = input.poscar.elem_count #atoms = [] #for i in range(len(elem)): # atoms += elem_count[i]*[elem[i]] ##end for #structure = Structure( # units = 'A', # axes = pa.lattice_vectors.copy(), # elem = atoms, # pos = pa.position.copy() # ) # get structure from CONTCAR ccfile = os.path.join(self.locdir, self.identifier + '.CONTCAR') if not os.path.exists(ccfile): self.error( 'CONTCAR file does not exist for relax simulation at ' + self.locdir) #end if contcar = Poscar(ccfile) structure = Structure() if contcar.elem != None: structure.read_poscar(ccfile) else: elem, elem_count = self.system.structure.order_by_species() structure.read_poscar(ccfile, elem=elem) #end if if input.poscar.dynamic != None: structure.freeze(input.poscar.dynamic, negate=True) #end if result.structure = structure else: self.error('ability to get result ' + result_name + ' has not been implemented') #end if return result
def primitive(self, symprec=1e-5): """ primitive structure Arugments: symprec (symmetry tolerance): distance tolerance in Cartesian coordinates to find crystal symmetry """ cell = self.structure.formatting('cell') cell = (cell['lattice'], cell['positions'], cell['numbers']) newcell = spglib.find_primitive(cell, symprec=symprec) if newcell == None: raise StructureFactoryError('the search is filed') else: poscar = self.__toPOSCAR(newcell) self.structure.pop() # delete old object self.structure = Structure().append(poscar) return self.structure
def __init__(self,structure=None,net_charge=0,net_spin=0,particles=None,**valency): self.structure = structure self.particles = particles if structure==None: self.structure = Structure() #end if if particles==None: self.particles = Particles() #end if self.folded_system = None if self.structure.folded_structure!=None: self.folded_system = PhysicalSystem( structure = structure.folded_structure, net_charge = net_charge, net_spin = net_spin, particles = particles, **valency ) #end if #add ions pc = dict() elem = list(self.structure.elem) for ion in set(elem): pc[ion] = elem.count(ion) #end for self.add_particles(**pc) #pseudize if len(valency)>0: self.pseudize(**valency) #end if #add electrons self.generate_electrons(net_charge,net_spin) self.check_folded_system()
def refine(self, symprec=1e-5, angle_tolerance=-1.0): """ refine structure which can change the cell's shape Arguments: symprec (symmetry tolerance): distance tolerance in Cartesian coordinates to find crystal symmetry angle_tolerance: An experimental argument that controls angle tolerance between basis vectors. Normally it is not recommended to use this argument. """ cell = self.structure.formatting('cell') cell = (cell['lattice'], cell['positions'], cell['numbers']) newcell = spglib.refine_cell(cell, symprec=symprec, angle_tolerance=angle_tolerance) if newcell == None: raise StructureFactoryError('the search is filed') else: poscar = self.__toPOSCAR(newcell) self.structure.pop() # delete old object self.structure = Structure().append(poscar) return self.structure
def return_system(self): ibrav = self.system.ibrav if ibrav!=0: self.error('ability to handle non-zero ibrav not yet implemented') #end if scale,axes,kaxes = self.get_common_vars('scale','axes','kaxes') elem = list(self.atomic_positions.atoms) ap = self.atomic_positions.copy() ap.change_specifier('bohr',self) pos = ap.positions kp = self.k_points.copy() kp.change_specifier('tpiba',self) kpoints = kp.kpoints*(2*pi)/scale center = axes.sum(0)/2 structure = Structure( axes = axes, elem = elem, scale = scale, pos = pos, center = center, kpoints = kpoints, units = 'B', rescale = False ) structure.zero_corner() structure.recenter() ion_charge = 0 valency = dict() atoms = list(self.atomic_positions.atoms) for atom in self.atomic_species.atoms: pseudo_file = self.atomic_species.pseudopotentials[atom] if self.pseudopotentials!=None and pseudo_file in self.pseudopotentials: pseudopot = self.pseudopotentials[pseudo_file] element = pseudopot.element valence = int(pseudopot.Z) ion_charge += atoms.count(atom)*valence valency[atom] = valence else: self.error('file '+pseudo_file+' was not listed in Pseudopotentials object\n please specify pseudopotentials with the settings function',trace=False) #end if #end for if 'nelup' in self.system: nup = self.system.nelup ndn = self.system.neldw net_charge = ion_charge - nup - ndn net_spin = nup - ndn elif 'tot_magnetization' in self.system: net_spin = self.system.tot_magnetization if 'nelec' in self.system: net_charge = ion_charge - self.system.nelec else: net_charge = 0 #end if else: net_spin = 0 if 'nelec' in self.system: net_charge = ion_charge - self.system.nelec else: net_charge = 0 #end if #end if system = PhysicalSystem(structure,net_charge,net_spin,**valency) return system
# whether pyramids are upright or inverted is relative to front incidence. # so if the same etch is applied to both sides of a slab of silicon, one surface # will have 'upright' pyramids and the other side will have 'not upright' (inverted) # pyramids in the model surf = regular_pyramids(elevation_angle=55, size=5, upright=True) front_surf = Interface('RT_TMM', texture=surf, layers=[Layer(si('0.1nm'), Air)], name='pyramids' + str(options['n_rays'])) back_surf = Interface('Lambertian', layers=[], name='lambertian') bulk_Si = BulkLayer(201.8e-6, Si, name='Si_bulk') # bulk thickness in m SC = Structure([front_surf, bulk_Si, back_surf], incidence=Air, transmission=Air) process_structure(SC, options) results = calculate_RAT(SC, options) RAT = results[0] results_per_pass = results[1] # load OPTOS/measured data sim = np.loadtxt('data/optos_fig7_sim.csv', delimiter=',') plt.figure() plt.plot(wavelengths * 1e9, RAT['R'][0], label='R') plt.plot(wavelengths * 1e9, RAT['T'][0], label='T')
a = [ BandBended(side=1, screening_length=screening_length, space_charge=space_charge, potential=0.0, effective_mass=m['Pt'], thickness=1), Polarized(potential=4.5, effective_mass=0.5, thickness=THICKNESS), BandBended(side=0, screening_length=screening_length, space_charge=-space_charge, potential=0.0, effective_mass=m['Pt'], thickness=1), ] s = Structure(a, 200) s.plot_structure() # exit() E = np.linspace(-1.0, 1.0, 200) * nu.eV STEPS = 50 V = np.linspace(-0.01, 0.01, STEPS) a[0].set_space_charge(-space_charge) a[2].set_space_charge(space_charge) s.update_potentials() I_minus = s.compute_IV(V, E, a[1]) a[0].set_space_charge(space_charge) a[2].set_space_charge(-space_charge) s.update_potentials() I_plus = s.compute_IV(V, E, a[1])