|
| 1 | +#!/usr/bin/python3 |
| 2 | +import numpy as np |
| 3 | + |
| 4 | +from ..unit import ( |
| 5 | + EnergyConversion, |
| 6 | + ForceConversion, |
| 7 | + LengthConversion, |
| 8 | + PressureConversion, |
| 9 | +) |
| 10 | + |
| 11 | +ry2ev = EnergyConversion("rydberg", "eV").value() |
| 12 | +kbar2evperang3 = PressureConversion("kbar", "eV/angstrom^3").value() |
| 13 | + |
| 14 | +length_convert = LengthConversion("bohr", "angstrom").value() |
| 15 | +energy_convert = EnergyConversion("hartree", "eV").value() |
| 16 | +force_convert = ForceConversion("hartree/bohr", "eV/angstrom").value() |
| 17 | + |
| 18 | +from collections import OrderedDict |
| 19 | + |
| 20 | +### iterout.c from OpenMX soure code: column numbers and physical quantities ### |
| 21 | +# /* 1: */ |
| 22 | +# /* 2,3,4: */ |
| 23 | +# /* 5,6,7: force * |
| 24 | +# /* 8: x-component of velocity */ |
| 25 | +# /* 9: y-component of velocity */ |
| 26 | +# /* 10: z-component of velocity */ |
| 27 | +# /* 11: Net charge, electron charge is defined to be negative. */ |
| 28 | +# /* 12: magnetic moment (muB) */ |
| 29 | +# /* 13,14: angles of spin */ |
| 30 | + |
| 31 | + |
| 32 | +def load_atom(lines): |
| 33 | + atom_names = [] |
| 34 | + atom_names_mode = False |
| 35 | + for line in lines: |
| 36 | + if "<Atoms.SpeciesAndCoordinates" in line: |
| 37 | + atom_names_mode = True |
| 38 | + elif "Atoms.SpeciesAndCoordinates>" in line: |
| 39 | + atom_names_mode = False |
| 40 | + elif atom_names_mode: |
| 41 | + parts = line.split() |
| 42 | + atom_names.append(parts[1]) |
| 43 | + natoms = len(atom_names) |
| 44 | + atom_names_original = atom_names |
| 45 | + atom_names = list(OrderedDict.fromkeys(set(atom_names))) # Python>=3.7 |
| 46 | + atom_names = sorted( |
| 47 | + atom_names, key=atom_names_original.index |
| 48 | + ) # Unique ordering of atomic species |
| 49 | + ntypes = len(atom_names) |
| 50 | + atom_numbs = [0] * ntypes |
| 51 | + atom_types = [] |
| 52 | + atom_types_mode = False |
| 53 | + for line in lines: |
| 54 | + if "<Atoms.SpeciesAndCoordinates" in line: |
| 55 | + atom_types_mode = True |
| 56 | + elif "Atoms.SpeciesAndCoordinates>" in line: |
| 57 | + atom_types_mode = False |
| 58 | + elif atom_types_mode: |
| 59 | + parts = line.split() |
| 60 | + for i, atom_name in enumerate(atom_names): |
| 61 | + if parts[1] == atom_name: |
| 62 | + atom_numbs[i] += 1 |
| 63 | + atom_types.append(i) |
| 64 | + atom_types = np.array(atom_types) |
| 65 | + return atom_names, atom_types, atom_numbs |
| 66 | + |
| 67 | + |
| 68 | +def load_cells(lines): |
| 69 | + cell, cells = [], [] |
| 70 | + for index, line in enumerate(lines): |
| 71 | + if "Cell_Vectors=" in line: |
| 72 | + parts = line.split() |
| 73 | + cell.append([float(parts[12]), float(parts[13]), float(parts[14])]) |
| 74 | + cell.append([float(parts[15]), float(parts[16]), float(parts[17])]) |
| 75 | + cell.append([float(parts[18]), float(parts[19]), float(parts[20])]) |
| 76 | + cells.append(cell) |
| 77 | + cell = [] |
| 78 | + cells = np.array(cells) |
| 79 | + return cells |
| 80 | + |
| 81 | + |
| 82 | +# load atom_names, atom_numbs, atom_types, cells |
| 83 | +def load_param_file(fname, mdname): |
| 84 | + with open(fname) as dat_file: |
| 85 | + lines = dat_file.readlines() |
| 86 | + atom_names, atom_types, atom_numbs = load_atom(lines) |
| 87 | + |
| 88 | + with open(mdname) as md_file: |
| 89 | + lines = md_file.readlines() |
| 90 | + cells = load_cells(lines) |
| 91 | + return atom_names, atom_numbs, atom_types, cells |
| 92 | + |
| 93 | + |
| 94 | +def load_coords(lines, atom_names, natoms): |
| 95 | + cnt = 0 |
| 96 | + coord, coords = [], [] |
| 97 | + for index, line in enumerate(lines): |
| 98 | + if "time=" in line: |
| 99 | + continue |
| 100 | + for atom_name in atom_names: |
| 101 | + atom_name += " " |
| 102 | + if atom_name in line: |
| 103 | + cnt += 1 |
| 104 | + parts = line.split() |
| 105 | + for_line = [float(parts[1]), float(parts[2]), float(parts[3])] |
| 106 | + coord.append(for_line) |
| 107 | + if cnt == natoms: |
| 108 | + coords.append(coord) |
| 109 | + cnt = 0 |
| 110 | + coord = [] |
| 111 | + coords = np.array(coords) |
| 112 | + return coords |
| 113 | + |
| 114 | + |
| 115 | +def load_data(mdname, atom_names, natoms): |
| 116 | + with open(mdname) as md_file: |
| 117 | + lines = md_file.readlines() |
| 118 | + coords = load_coords(lines, atom_names, natoms) |
| 119 | + steps = [str(i) for i in range(1, coords.shape[0] + 1)] |
| 120 | + return coords, steps |
| 121 | + |
| 122 | + |
| 123 | +def to_system_data(fname, mdname): |
| 124 | + data = {} |
| 125 | + ( |
| 126 | + data["atom_names"], |
| 127 | + data["atom_numbs"], |
| 128 | + data["atom_types"], |
| 129 | + data["cells"], |
| 130 | + ) = load_param_file(fname, mdname) |
| 131 | + data["coords"], steps = load_data( |
| 132 | + mdname, |
| 133 | + data["atom_names"], |
| 134 | + np.sum(data["atom_numbs"]), |
| 135 | + ) |
| 136 | + data["orig"] = np.zeros(3) |
| 137 | + return data, steps |
| 138 | + |
| 139 | + |
| 140 | +def load_energy(lines): |
| 141 | + energy = [] |
| 142 | + for line in lines: |
| 143 | + if "time=" in line: |
| 144 | + parts = line.split() |
| 145 | + ene_line = float(parts[4]) # Hartree |
| 146 | + energy.append(ene_line) |
| 147 | + continue |
| 148 | + energy = energy_convert * np.array(energy) # Hartree -> eV |
| 149 | + return energy |
| 150 | + |
| 151 | + |
| 152 | +def load_force(lines, atom_names, atom_numbs): |
| 153 | + cnt = 0 |
| 154 | + field, fields = [], [] |
| 155 | + for index, line in enumerate(lines): |
| 156 | + if "time=" in line: |
| 157 | + continue |
| 158 | + for atom_name in atom_names: |
| 159 | + atom_name += " " |
| 160 | + if atom_name in line: |
| 161 | + cnt += 1 |
| 162 | + parts = line.split() |
| 163 | + for_line = [float(parts[4]), float(parts[5]), float(parts[6])] |
| 164 | + field.append(for_line) |
| 165 | + if cnt == np.sum(atom_numbs): |
| 166 | + fields.append(field) |
| 167 | + cnt = 0 |
| 168 | + field = [] |
| 169 | + force = force_convert * np.array(fields) |
| 170 | + return force |
| 171 | + |
| 172 | + |
| 173 | +# load energy, force |
| 174 | +def to_system_label(fname, mdname): |
| 175 | + atom_names, atom_numbs, atom_types, cells = load_param_file(fname, mdname) |
| 176 | + with open(mdname) as md_file: |
| 177 | + lines = md_file.readlines() |
| 178 | + energy = load_energy(lines) |
| 179 | + force = load_force(lines, atom_names, atom_numbs) |
| 180 | + return energy, force |
| 181 | + |
| 182 | + |
| 183 | +if __name__ == "__main__": |
| 184 | + file_name = "Cdia" |
| 185 | + fname = f"{file_name}.dat" |
| 186 | + mdname = f"{file_name}.md" |
| 187 | + atom_names, atom_numbs, atom_types, cells = load_param_file(fname, mdname) |
| 188 | + coords, steps = load_data(mdname, atom_names, np.sum(atom_numbs)) |
| 189 | + data, steps = to_system_data(fname, mdname) |
| 190 | + energy, force = to_system_label(fname, mdname) |
| 191 | + print(atom_names) |
| 192 | + print(atom_numbs) |
| 193 | + print(atom_types) |
| 194 | + # print(cells.shape) |
| 195 | + # print(coords.shape) |
| 196 | + # print(len(energy)) |
| 197 | + # print(force.shape) |
0 commit comments