Revision: 18396
          http://sourceforge.net/p/jmol/code/18396
Author:   hansonr
Date:     2013-07-02 06:07:51 +0000 (Tue, 02 Jul 2013)
Log Message:
-----------
block reader for magres reader

Modified Paths:
--------------
    trunk/Jmol/src/org/jmol/adapter/readers/xtal/MagResReader.java

Modified: trunk/Jmol/src/org/jmol/adapter/readers/xtal/MagResReader.java
===================================================================
--- trunk/Jmol/src/org/jmol/adapter/readers/xtal/MagResReader.java      
2013-07-01 22:25:55 UTC (rev 18395)
+++ trunk/Jmol/src/org/jmol/adapter/readers/xtal/MagResReader.java      
2013-07-02 06:07:51 UTC (rev 18396)
@@ -3,14 +3,17 @@
 /**
  * MagRes reader for magnetic resonance files produced by CASTEP
  * 
- * @author Bob Hanson hans...@stolaf.edu 6/27/2013
+ * @author Bob Hanson hans...@stolaf.edu, Simone Sturniolo  6/27/2013
  * 
  */
 
 import java.util.Hashtable;
 import java.util.Map;
 
+import jspecview.util.Logger;
+
 import org.jmol.util.JmolList;
+import org.jmol.util.SB;
 import org.jmol.util.Tensor;
 import org.jmol.util.TextFormat;
 
@@ -19,118 +22,181 @@
 
 public class MagResReader extends AtomSetCollectionReader {
 
+  private final static int BLOCK_NEW = -2;
+  private final static int BLOCK_NONE = -1;
+  private final static int BLOCK_CALC = 0;
+  private final static int BLOCK_ATOMS = 1;
+  private final static int BLOCK_MAGRES = 2;
+
+  private int currentBlock = BLOCK_NONE;
+
   private float[] cellParams;
-  private static float maxIso = 10000;
   private String tensorTypes = "";
-  private boolean isNew;
-  private Map<String, String> mapUnits = new Hashtable<String, String>();
+  //private static float maxIso = 10000; // the old code was checking for this.
+  
+  private Map<String, String> magresUnits = new Hashtable<String, String>();
   private JmolList<Tensor> interactionTensors = new JmolList<Tensor>();
+  private SB header = new SB();
+  
+  /**
+   * not sure how to work with symmetry here...
+   * 
+   */
   @Override
   protected void initializeReader() {
     setFractionalCoordinates(false);
     doApplySymmetry = false;
-    atomSetCollection.newAtomSet();
-    try {
-      readLine();
-      isNew = line.startsWith("#$magres");      
-      if (isNew) {
-        ignoreFileSpaceGroupName = true;
-        //setSpaceGroupName("P1");
-      }
-    } catch (Exception e) {
-    }
+    ignoreFileSpaceGroupName = true;
   }
 
+  /**
+   * Set final auxiliary info and symmetry, including "fileHeader", 
"magresUnits", and "interactionTensors";
+   * note that getProperty("auxiliaryInfo","magresUnits") should then return 
catalog of units
+   * 
+   */
   @Override
   protected void finalizeReader() throws Exception {
     doApplySymmetry = true;
+    atomSetCollection.setAtomSetCollectionAuxiliaryInfo("fileHeader",
+        header.toString());
     finalizeReaderASCR();
     if (interactionTensors.size() > 0)
       atomSetCollection.setAtomSetAuxiliaryInfo("interactionTensors", 
interactionTensors);
   }
 
+  /**
+   * Valid blocks include [calculation] [atoms] [magres]
+   * All magres entries must be prefaced with a corresponding unit
+   * 
+   * @return true to read another line (some readers return false because they 
have over-read a line)
+   */
   @Override
   protected boolean checkLine() throws Exception {
-    line = line.trim();
-    if (cellParams == null && line.startsWith("lattice")) {
-      readCellParams();
-      return true;
+    trimLine();
+    if (currentBlock != BLOCK_NONE && line.startsWith("units"))
+      return setUnits();
+    switch (checkBlock()) {
+    case BLOCK_CALC:
+      header.append(line).append("\n");
+      appendLoadNote(line);
+      break;
+    case BLOCK_ATOMS:
+      if (cellParams == null && line.startsWith("lattice"))
+        return readCellParams();
+      else if (line.startsWith("atom"))
+        return readAtom();
+      else if (line.startsWith("symmetry"))
+        return readSymmetry();
+      break;
+    case BLOCK_MAGRES:
+      return readTensor();
     }
-    if (isNew) {
-      if (line.startsWith("units")) {
-        setUnitsNew();
-      } else if (line.startsWith("atom")) {
-        readAtom(true);
-      } else if (line.startsWith("symmetry")) {
-        readSymmetryNew();
-      } else if (line.startsWith("ms") || line.startsWith("efg") || 
line.startsWith("isc")) {
-        readTensorNew();
-      } else if (line.startsWith("<magres_old>")) {
-        continuing = false;
-      }
-      return true;
-    }
-    if (line.contains("Coordinates")) {
-      readAtom(false);
-    } else if (line.contains("J-coupling Total")
-        || line.contains("TOTAL tensor")|| line.contains("TOTAL Shielding 
Tensor")) {
-      readTensorOld();
-    }
     return true;
   }
 
-  // ms H                  1          1.9115355485265077E+01         
-6.8441521786256319E+00          1.9869475943756368E-01         
-7.4231606832789883E+00          3.5078237789073569E+01          
1.6453141184608533E+00         -8.4492087560280138E-01          
1.4000600350356041E+00          1.7999188282948701E+01
-  // efg H                  1         -9.7305664267778647E-02         
-1.3880930041098827E-01          8.3161631703720738E-03         
-1.3880930041098827E-01          2.5187188360357782E-01         
-4.4856574290225361E-02          8.3161631703720738E-03         
-4.4856574290225361E-02         -1.5456621933580317E-01
-  // isc_fc C                   2 H                   3         
-1.0414024145274923E+00          5.9457737246691622E-02          
1.3323917584132525E-01          5.9457737246692129E-02         
-8.0480723469752380E-01          5.4194562595693906E-02          
1.3323917584132525E-01          5.4194562595693989E-02         
-8.1674287041188620E-01
+  /**
+   * All characters after hash ignored; lines are trimmed.
+   *
+   */
+  private void trimLine() {
+    int pt = line.indexOf("#");
+    if (pt >= 0)
+      line = line.substring(0, pt);
+    line = line.trim();
+  }
 
-  private void readTensorNew() throws Exception {
-    String[] tokens = getTokens();
-    String id = tokens[0];
-    String atomName1 = getAtomName(tokens[1], tokens[2]);
-    int pt = 3;
-    String atomName2 = (id.startsWith("isc") ? getAtomName(tokens[pt++], 
tokens[pt++]) : null);
-    // TODO: maxIso for isc?
-    double[][] a = new double[3][3];
-    for (int i = 0; i < 3; i++)
-      for (int j = 0; j < 3; j++)
-        a[i][j] = Double.valueOf(tokens[pt++]).doubleValue();
-    int index1 = atomSetCollection.getAtomIndexFromName(atomName1);
-    int index2;
-    Tensor t = Tensor.getTensorFromAsymmetricTensor(a, id);
-    if (atomName2 == null) {
-      index2 = -1;
-      atomSetCollection.getAtoms()[index1].addTensor(t, null);
-      interactionTensors.addLast(t);
-    } else {
-      index2 = atomSetCollection.getAtomIndexFromName(atomName2);
+  /**
+   * looking for tags here.
+   * 
+   * @return currentBlock if not a tag or BLOCK_NEW otherwise
+   */
+  private int checkBlock() {
+    // please don't use RegEx here, because of issues with JavaScript version
+    if (!(line.startsWith("<") && line.endsWith(">"))
+       && !(line.startsWith("[") && line.endsWith("]")))      
+      return currentBlock;
+    line = TextFormat.simpleReplace(line, "<", "[");
+    line = TextFormat.simpleReplace(line, ">", "]");
+    switch (("..............." +
+                    "[calculation].." +
+                    "[/calculation]." +
+                    "[atoms]........" +
+                    "[/atoms]......." +
+                    "[magres]......." +
+                    "[/magres]......").indexOf(line + ".") / 15) {
+    case 0:
+      Logger.info("block indicator ignored: " + line);
+      break;
+    case 1:
+      if (currentBlock == BLOCK_NONE)
+        currentBlock = BLOCK_CALC;
+      break;
+    case 2:
+      if (currentBlock == BLOCK_CALC)
+        currentBlock = BLOCK_NONE;
+      break;
+    case 3:
+      if (currentBlock == BLOCK_NONE) {
+        currentBlock = BLOCK_ATOMS;
+        atomSetCollection.newAtomSet();
+        magresUnits = new Hashtable<String, String>();
+      }
+      break;
+    case 4:
+      if (currentBlock == BLOCK_ATOMS)
+        currentBlock = BLOCK_NONE;
+      break;
+    case 5:
+      if (currentBlock == BLOCK_NONE) {
+        currentBlock = BLOCK_MAGRES;
+        magresUnits = new Hashtable<String, String>();
+        atomSetCollection.setAtomSetAuxiliaryInfo("magresUnits",
+            magresUnits);
+      }
+      break;
+    case 6:
+      if (currentBlock == BLOCK_MAGRES)
+        currentBlock = BLOCK_NONE;
+      break;
     }
-    t.setAtomIndexes(index1, index2);
-    String key = ";" + id +";";
-    if (tensorTypes.indexOf(key) < 0) {
-      tensorTypes += key;
-      appendLoadNote("Ellipsoids set \"" + id + "\": "
-          + (id.startsWith("ms") ? "Magnetic Shielding" : 
-            id.startsWith("efg") ? "Electric Field Gradient" : 
id.startsWith("isc") ? "J-Coupling" : "?"));
-    }
+   return BLOCK_NEW;    
   }
 
+  /**
+   * catalog units
+   * 
+   * @return true
+   */
+  private boolean setUnits() {
+    String[] tokens = getTokens();
+    magresUnits.put(tokens[1], tokens[2]);
+    return true;
+  }
+
   //  symmetry x,y,z
   //  symmetry x+1/2,-y+1/2,-z
   //  symmetry -x,y+1/2,-z+1/2
   //  symmetry -x+1/2,-y,z+1/2
 
-  private void readSymmetryNew() {
+  /**
+   * not doing anything with this -- P1 assumed
+   * @return true
+   */
+  private boolean readSymmetry() {
+    // not used
     setSymmetryOperator(getTokens()[1]);
+    return true;
   }
 
-  private void setUnitsNew() {
-    String[] tokens = getTokens();
-    mapUnits.put(tokens[1], tokens[2]);
-  }
+  //  lattice    6.0000000000000009E+00   0.0000000000000000E+00   
0.0000000000000000E+00   0.0000000000000000E+00   6.0000000000000009E+00   
0.0000000000000000E+00   0.0000000000000000E+00   0.0000000000000000E+00   
6.0000000000000009E+00
 
-  private void readCellParams() throws Exception {
-    String[] tokens = getTokens();
-    
+  /**
+   * 
+   * @return true;
+   * @throws Exception
+   */
+  private boolean readCellParams() throws Exception {
+    String[] tokens = getTokens();   
     cellParams = new float[9];
     for (int i = 0; i < 9; i++)
       cellParams[i] = parseFloatStr(tokens[i + 1]);
@@ -138,78 +204,89 @@
     addPrimitiveLatticeVector(1, cellParams, 3);
     addPrimitiveLatticeVector(2, cellParams, 6);
     setSpaceGroupName("P1");
+    return true;
   }
 
   /*
     C    1 Coordinates      2.054    0.000    0.000   A
    */
 
-  private Atom atom;
-
-  private void readAtom(boolean isNew) {
-    float f = ((isNew ? mapUnits.get("atom").startsWith("A") : line.trim()
-        .endsWith("A")) ? 1 : ANGSTROMS_PER_BOHR);
+  /**
+   * 
+   * Allowing for BOHR units here; probably unnecessary.
+   * 
+   * @return true
+   */
+  private boolean readAtom() {
+    String units = magresUnits.get("atom");
+    if (units == null)
+      return true;
+    float f = (units.startsWith("A") ? 1 : ANGSTROMS_PER_BOHR);
     String[] tokens = getTokens();
-    atom = new Atom();
-    int pt = (isNew ? 1 : 0);
-    atom.elementSymbol = tokens[isNew ? pt++ : pt];
+    Atom atom = new Atom();
+    int pt = 1;
+    atom.elementSymbol = tokens[pt++];
     atom.atomName = getAtomName(tokens[pt++], tokens[pt++]);
     atomSetCollection.addAtomWithMappedName(atom);
-    if (!isNew)
-      pt++;
     float x = parseFloatStr(tokens[pt++]) * f;
     float y = parseFloatStr(tokens[pt++]) * f;
     float z = parseFloatStr(tokens[pt++]) * f;
     atom.set(x, y, z);
     setAtomCoord(atom);
+    return true;
   }
 
-  private String getAtomName(String name, String index) {
+  /**
+   * combine name and index
+   * @param name
+   * @param index
+   * @return name_index
+   */
+  private static String getAtomName(String name, String index) {
     return name + "_" + index;
   }
 
-  /*
-         J-coupling Total
+  // ms H                  1          1.9115355485265077E+01         
-6.8441521786256319E+00          1.9869475943756368E-01         
-7.4231606832789883E+00          3.5078237789073569E+01          
1.6453141184608533E+00         -8.4492087560280138E-01          
1.4000600350356041E+00          1.7999188282948701E+01
+  // efg H                  1         -9.7305664267778647E-02         
-1.3880930041098827E-01          8.3161631703720738E-03         
-1.3880930041098827E-01          2.5187188360357782E-01         
-4.4856574290225361E-02          8.3161631703720738E-03         
-4.4856574290225361E-02         -1.5456621933580317E-01
+  // isc_fc C                   2 H                   3         
-1.0414024145274923E+00          5.9457737246691622E-02          
1.3323917584132525E-01          5.9457737246692129E-02         
-8.0480723469752380E-01          5.4194562595693906E-02          
1.3323917584132525E-01          5.4194562595693989E-02         
-8.1674287041188620E-01
 
-  W    1 Eigenvalue  sigma_xx -412163.5628
-  W    1 Eigenvector sigma_xx       0.1467     -0.9892      0.0000
-  W    1 Eigenvalue  sigma_yy -412163.6752
-  W    1 Eigenvector sigma_yy       0.9892      0.1467      0.0000
-  W    1 Eigenvalue  sigma_zz -432981.4974
-  W    1 Eigenvector sigma_zz       0.0000      0.0000      1.0000
-  
-  TOTAL tensor
-
-              -0.0216     -0.1561     -0.0137
-              -0.1561     -0.1236     -0.0359
-              -0.0137     -0.0359      0.1452
-
+  /**
+   * Read a tensor line. Note that a corresponding unit line must have 
appeared first.
+   * @return true;
+   * @throws Exception 
    */
-  private void readTensorOld() throws Exception {
-    line = line.trim();
-    if (tensorTypes.indexOf(line) < 0) {
-      tensorTypes += line;
-      appendLoadNote("Ellipsoids: " + line);
+  private boolean readTensor() throws Exception {
+    String[] tokens = getTokens();
+    String id = tokens[0];
+    String units = magresUnits.get(id);
+    if (units == null)
+      return true;
+    String atomName1 = getAtomName(tokens[1], tokens[2]);
+    int pt = 3;
+    String atomName2 = (id.startsWith("isc") ? getAtomName(tokens[pt++], 
tokens[pt++]) : null);
+    // TODO: maxIso for isc?
+    double[][] a = new double[3][3];
+    for (int i = 0; i < 3; i++)
+      for (int j = 0; j < 3; j++)
+        a[i][j] = Double.valueOf(tokens[pt++]).doubleValue();
+    int index1 = atomSetCollection.getAtomIndexFromName(atomName1);
+    int index2;
+    Tensor t = Tensor.getTensorFromAsymmetricTensor(a, id);
+    if (atomName2 == null) {
+      index2 = -1;
+      atomSetCollection.getAtoms()[index1].addTensor(t, null);
+      interactionTensors.addLast(t);
+    } else {
+      index2 = atomSetCollection.getAtomIndexFromName(atomName2);
     }
-    atomSetCollection.setAtomSetName(line);
-    String type = (line.indexOf("J-") >= 0 ? "isc" : line.indexOf("Shielding") 
>= 0 ? "ms" : "efg");
-    float[] data = new float[9];
-    readLine();
-    String s = TextFormat.simpleReplace(readLine() + readLine() + readLine(),
-        "-", " -");
-    fillFloatArray(s, 0, data);
-    //float f = 3;
-    if (type.equals("isc")) {
-      discardLinesUntilContains("Isotropic");
-      float iso = parseFloatStr(getTokens()[3]);
-      if (Math.abs(iso) > maxIso)
-        return;
-      //f = 0.04f;
+    t.setAtomIndexes(index1, index2);
+    String key = ";" + id +";";
+    if (tensorTypes.indexOf(key) < 0) {
+      tensorTypes += key;
+      appendLoadNote("Ellipsoids set \"" + id + "\": "
+          + (id.startsWith("ms") ? "Magnetic Shielding" : 
+            id.startsWith("efg") ? "Electric Field Gradient" : 
id.startsWith("isc") ? "J-Coupling" : "?"));
     }
-    double[][] a = new double[3][3];
-    for (int i = 0, pt = 0; i < 3; i++)
-      for (int j = 0; j < 3; j++)
-        a[i][j] = data[pt++];
-    atom.addTensor(Tensor.getTensorFromAsymmetricTensor(a, type), null);
+    return true;
   }
 }

This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.


------------------------------------------------------------------------------
This SF.net email is sponsored by Windows:

Build for Windows Store.

http://p.sf.net/sfu/windows-dev2dev
_______________________________________________
Jmol-commits mailing list
Jmol-commits@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/jmol-commits

Reply via email to