-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathQCJSONReader.java
More file actions
377 lines (348 loc) · 12.9 KB
/
Copy pathQCJSONReader.java
File metadata and controls
377 lines (348 loc) · 12.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
package org.jmol.adapter.readers.quantum;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Hashtable;
import java.util.Map;
import javajs.util.AU;
import javajs.util.Lst;
import javajs.util.SB;
import org.jmol.adapter.smarter.Atom;
import org.jmol.api.JmolAdapter;
import org.jmol.util.Logger;
import org.qcschema.QCSchemaUnits;
/**
* A molecular structure and orbital reader for MolDen files.
* See http://www.cmbi.ru.nl/molden/molden_format.html
*
* updated by Bob Hanson <hansonr@stolaf.edu> for Jmol 12.0/12.1
*
* adding [spacegroup] [operators] [cell] [cellaxes] for Jmol 14.3.7
*
* @author Matthew Zwier <mczwier@gmail.com>
*/
public class QCJSONReader extends MoldenReader {
private Map<String, Object> job;
private int jobCount;
private int modelCount;
@SuppressWarnings("unchecked")
@Override
protected void initializeReader() {
super.initializeReader();
SB sb = new SB();
try {
while (rd() != null)
sb.append(line);
Lst<Object> json = vwr.parseJSONArray(sb.toString());
// first record is version tag
Logger.info(json.get(0).toString());
// second record is Jmol info; not used here
jobCount = json.size() - 2;
for (int i = 0; i < jobCount; i++)
processJob((Map<String, Object>)json.get(i + 2));
} catch (Exception e) {
e.printStackTrace();
}
continuing = false;
}
/**
* @param job
* @throws Exception
*/
private void processJob(Map<String, Object> job) throws Exception {
this.job = job;
readSteps();
/*
if (loadVibrations)
readFreqsAndModes();
if (loadGeometries)
readGeometryOptimization();
checkSymmetry();
if (asc.atomSetCount == 1 && moData != null)
finalizeMOData(moData);
*/
}
@Override
public void finalizeSubclassReader() throws Exception {
finalizeReaderASCR();
}
private void readSteps() throws Exception {
ArrayList<Object> steps = QCSchemaUnits.getList(job, "steps");
int nSteps = steps.size();
for (int iStep = 0; iStep < nSteps; iStep++) {
if (!doGetModel(++modelCount, null)) {
if (!checkLastModel())
return;
continue;
}
asc.newAtomSet();
@SuppressWarnings("unchecked")
Map<String, Object> step = (Map<String, Object>) steps.get(iStep);
Map<String, Object> topology = getMapSafely(step, "topology");
Map<String, Object> atoms = getMapSafely(topology, "atoms");
// one or the other of these is required:
String[] symbols = QCSchemaUnits.getStringArray(atoms, "symbol");
int[] atomNumbers = QCSchemaUnits.getIntArray(atoms, "atom_number");
String[] atom_names = QCSchemaUnits.getStringArray(atoms, "atom_names");
double[] coords = QCSchemaUnits.getDoubleArray(atoms, "coords");
modelAtomCount = coords.length / 3;
double f = QCSchemaUnits.getConversionFactor(atoms, "coords", QCSchemaUnits.UNITS_ANGSTROMS);
boolean isFractional = (f == 0);
setFractionalCoordinates(isFractional);
if (isFractional) {
f = QCSchemaUnits.getConversionFactor(atoms, "unit_cell", QCSchemaUnits.UNITS_ANGSTROMS);
double[] cell = QCSchemaUnits.getDoubleArray(atoms, "unit_cell");
// a b c alpha beta gamma
// m.m00, m.m10, m.m20, // Va
// m.m01, m.m11, m.m21, // Vb
// m.m02, m.m12, m.m22, // Vc
// dimension, (float) volume,
if (cell == null) {
Logger.error("topology.unit_cell is missing even though atoms are listed as fractional");
} else {
for (int i = 0; i < 6; i++) {
switch (i) {
case 3:
f = 1;
//$FALL-THROUGH$
default:
setUnitCellItem(i, (float)(cell[i] * f));
break;
}
}
}
}
for (int i = 0, pt = 0; i < modelAtomCount; i++) {
Atom atom = asc.addNewAtom();
setAtomCoordXYZ(atom, (float)(coords[pt++] * f), (float)(coords[pt++] * f), (float) (coords[pt++]
* f));
String sym = (symbols == null ? JmolAdapter
.getElementSymbol(atomNumbers[i]) : symbols[i]);
atom.atomName = (atom_names == null ? sym : atom_names[i]);
atom.elementNumber = (short) (atomNumbers == null ? JmolAdapter
.getElementNumber(sym) : atomNumbers[i]);
}
if (doReadMolecularOrbitals) {
readMolecularOrbitals(getMapSafely(step, "molecular_orbitals"));
clearOrbitals();
}
applySymmetryAndSetTrajectory();
if (loadVibrations) {
readFreqsAndModes(QCSchemaUnits.getList(step, "vibrations"));
}
}
}
private boolean readFreqsAndModes(ArrayList<Object> vibrations) throws Exception {
// "frequency":{"value":-0.00,"units":["cm^-1","?"]},
// "ir_intensity":{"value":0.000005,"units":["au",1]},
// "vectors":[
if (vibrations != null) {
int n = vibrations.size();
for (int i = 0; i < n; i++) {
@SuppressWarnings("unchecked")
Map<String, Object> vib = (Map<String, Object>) vibrations.get(i);
double freq = QCSchemaUnits.getDouble(vib, "frequency", QCSchemaUnits.UNITS_CM_1);
double[] vectors = QCSchemaUnits.getDoubleArray(vib, "vectors");
if (i > 0)
asc.cloneLastAtomSet();
asc.setAtomSetFrequency(null, null, "" + freq, QCSchemaUnits.UNITS_CM_1);
int i0 = asc.getLastAtomSetAtomIndex();
for (int j = 0, pt = 0; j < modelAtomCount; j++) {
asc.addVibrationVector(j + i0, (float) (vectors[pt++] * ANGSTROMS_PER_BOHR),
(float) (vectors[pt++] * ANGSTROMS_PER_BOHR), (float) (vectors[pt++]
* ANGSTROMS_PER_BOHR));
}
}
}
return true;
}
private boolean haveEnergy = true;
/**
* Read basis and orbital information.
*
* @param molecular_orbitals
* @return true if successful
*
* @throws Exception
*/
private boolean readMolecularOrbitals(Map<String, Object> molecular_orbitals) throws Exception {
if (molecular_orbitals == null)
return false;
String moBasisID = molecular_orbitals.get("basis_id").toString();//:"MOBASIS_1"
if (!readBasis(moBasisID))
return false;
Boolean isNormalized = (Boolean) molecular_orbitals.get("__jmol_normalized");
if (isNormalized != null && isNormalized.booleanValue())
moData.put("isNormalized", isNormalized);
calculationType = (String) molecular_orbitals.get("__jmol_calculation_type");
if (calculationType == null)
calculationType = "?";
moData.put("calculationType", calculationType);
ArrayList<Object> mos = QCSchemaUnits.getList(molecular_orbitals, "orbitals");
int n = mos.size();
for (int i = 0; i < n; i++) {
@SuppressWarnings("unchecked")
Map<String, Object> thisMO = (Map<String, Object>) mos.get(i);
double energy = QCSchemaUnits.getDouble(thisMO, "energy", "ev");
double occupancy = QCSchemaUnits.getDouble(thisMO, "occupancy", null);
String symmetry = (String) thisMO.get("symmetry");
String spin = (String) thisMO.get("type");
if (spin != null) {
if (spin.indexOf("beta") >= 0)
alphaBeta = "beta";
else if (spin.indexOf("alpha") >= 0)
alphaBeta = "alpha";
}
float[] coefs = toFloatArray(QCSchemaUnits.getDoubleArray(thisMO, "coefficients"));
line = "" + symmetry;
if (filterMO()) {
Map<String, Object> mo = new Hashtable<String, Object>();
mo.put("coefficients", coefs);
if (Double.isNaN(energy)) {
haveEnergy = false;
} else {
mo.put("energy", Float.valueOf((float) energy));
}
if (!Double.isNaN(occupancy))
mo.put("occupancy", Float.valueOf((float) occupancy));
if (symmetry != null)
mo.put("symmetry", symmetry);
if (alphaBeta.length() > 0)
mo.put("type", alphaBeta);
setMO(mo);
if (debugging) {
Logger.debug(coefs.length + " coefficients in MO " + orbitals.size());
}
}
}
if (debugging)
Logger.debug("read " + orbitals.size() + " MOs");
ArrayList<Object> units = QCSchemaUnits.getList(molecular_orbitals, "orbitals_energy_units");
String sunits = (units == null ? null : units.get(0).toString());
setMOs(sunits == null || sunits.equals("?") ? "?" : sunits);
if (haveEnergy && doSort)
sortMOs();
return false;
}
private float[] toFloatArray(double[] da) {
float[] fa = new float[da.length];
for (int j = da.length; --j >= 0;)
fa[j] = (float) da[j];
return fa;
}
String lastBasisID = null;
private boolean readBasis(String moBasisID) throws Exception {
Map<String, Object> moBasisData = getMapSafely(job, "mo_bases");
Map<String, Object> moBasis = getMapSafely(moBasisData, moBasisID);
if (moBasis == null) {
Logger.error("No job.mo_bases entry for " + moBasisID);
return false;
}
if (moBasisID == lastBasisID)
return true;
lastBasisID = moBasisID;
ArrayList<Object> listG = QCSchemaUnits.getList(moBasis, "gaussians");
ArrayList<Object> listS = QCSchemaUnits.getList(moBasis, "shells");
if (listG == null && listS == null) {
listG = listS = QCSchemaUnits.getList(moBasis, "slaters");
}
if ((listG == null) != (listS == null)) {
Logger.error("gaussians/shells or slaters missing");
return false;
}
if (listG == listS) {
readSlaterBasis(listS);
} else {
readGaussianBasis(listG, listS);
}
return true;
}
boolean readSlaterBasis(ArrayList<Object> listS) throws Exception {
/*
1 0 0 0 1 1.5521451600 0.9776767193
1 1 0 0 0 1.5521451600 1.6933857512
1 0 1 0 0 1.5521451600 1.6933857512
1 0 0 1 0 1.5521451600 1.6933857512
2 0 0 0 0 1.4738648100 1.0095121222
3 0 0 0 0 1.4738648100 1.0095121222
*/
nCoef = listS.size();
for (int i = 0; i < nCoef; i++) {
double[] a = QCSchemaUnits.getDoubleArray(listS.get(i), null);
addSlater((int) a[0], (int) a[1], (int) a[2], (int) a[3], (int) a[4], (float) a[5], (float) a[6]);
}
setSlaters(false, false);
return true;
}
private boolean readGaussianBasis(ArrayList<Object> listG, ArrayList<Object> listS) throws Exception {
shells = new Lst<int[]>();
for (int i = 0; i < listS.size(); i++)
shells.addLast(QCSchemaUnits.getIntArray(listS.get(i), null));
int gaussianPtr = listG.size();
float[][] garray = AU.newFloat2(gaussianPtr);
// [[exp, coef], [exp, coef],...] with sp [exp, coef1, coef2]
for (int i = 0; i < gaussianPtr; i++)
garray[i] = toFloatArray(QCSchemaUnits.getDoubleArray(listG.get(i), null));
moData.put("shells", shells);
moData.put("gaussians", garray);
Logger.info(shells.size() + " slater shells read");
Logger.info(garray.length + " gaussian primitives read");
//Logger.info(nCoef + " MO coefficients expected for orbital type " + orbitalType);
asc.setCurrentModelInfo("moData", moData);
return false;
}
@SuppressWarnings("unchecked")
private void sortMOs() {
Object[] list = orbitals.toArray(new Object[orbitals.size()]);
Arrays.sort(list, new MOEnergySorter());
orbitals.clear();
for (int i = 0; i < list.length; i++)
orbitals.addLast((Map<String, Object>)list[i]);
}
/**
* Safely get a Map from a Map using a key.
* @param map
* @param key
* @return the Map or null
*/
@SuppressWarnings("unchecked")
private static Map<String, Object> getMapSafely(Map<String, Object> map, String key) {
return (map == null ? null : (Map<String, Object>) map.get(key));
}
/////////////////// from Molden reader -- TODO /////////////////
// private boolean checkSymmetry() throws Exception {
// // extension for symmetry
// if (line.startsWith("[SPACEGROUP]")) {
// setSpaceGroupName(rd());
// rd();
// return true;
// }
// if (line.startsWith("[OPERATORS]")) {
// while (rd() != null && line.indexOf("[") < 0)
// if (line.length() > 0) {
// Logger.info("adding operator " + line);
// setSymmetryOperator(line);
// }
// return true;
// }
// if (line.startsWith("[CELL]")) {
// rd();
// Logger.info("setting cell dimensions " + line);
// // ANGS assumed here
// next[0] = 0;
// for (int i = 0; i < 6; i++)
// setUnitCellItem(i, parseFloat());
// rd();
// return true;
// }
// if (line.startsWith("[CELLAXES]")) {
// float[] f = new float[9];
// fillFloatArray(null, 0, f);
// addExplicitLatticeVector(0, f, 0);
// addExplicitLatticeVector(1, f, 3);
// addExplicitLatticeVector(2, f, 6);
// return true;
// }
// return false;
// }
//
}