private ICrystal ReadCrystal(ICrystal crystal) { string line = input.ReadLine(); bool end_found = false; while (line != null && !end_found) { /* is line continued? */ if (line.Length > 0 && line.Substring(line.Length - 1).Equals("=", StringComparison.Ordinal)) { /* yes, line is continued */ line = line + input.ReadLine(); } /* determine ShelX command */ string command; try { command = line.Substring(0, 4); } catch (ArgumentOutOfRangeException) { // disregard this line break; } Debug.WriteLine($"command: {command}"); var u_command = command.ToUpperInvariant(); if (u_command.StartsWith("REM", StringComparison.Ordinal)) { /* line is comment, disregard */ /* 7.1 Crystal data and general instructions */ } else if (u_command.StartsWith("END", StringComparison.Ordinal)) { end_found = true; } else { switch (u_command) { case "TITL": break; case "CELL": { // example: CELL 1.54184 23.56421 7.13203 18.68928 90.0000 // 109.3799 90.0000 CELL 1.54184 7.11174 21.71704 30.95857 // 90.000 90.000 90.000 var st = Strings.Tokenize(line); //st[0]; // string command_again //st[1]; // string wavelength string sa = st[2]; string sb = st[3]; string sc = st[4]; string salpha = st[5]; string sbeta = st[6]; string sgamma = st[7]; Debug.WriteLine($"a: {sa}"); Debug.WriteLine($"b: {sb}"); Debug.WriteLine($"c: {sc}"); Debug.WriteLine($"alpha: {salpha}"); Debug.WriteLine($"beta : {sbeta}"); Debug.WriteLine($"gamma: {sgamma}"); double a = FortranFormat.Atof(sa); double b = FortranFormat.Atof(sb); double c = FortranFormat.Atof(sc); double alpha = FortranFormat.Atof(salpha); double beta = FortranFormat.Atof(sbeta); double gamma = FortranFormat.Atof(sgamma); Vector3[] axes = CrystalGeometryTools.NotionalToCartesian(a, b, c, alpha, beta, gamma); crystal.A = axes[0]; crystal.B = axes[1]; crystal.C = axes[2]; } break; case "ZERR": case "LATT": case "SYMM": case "SFAC": case "DISP": case "UNIT": case "LAUE": case "REM ": case "MORE": case "TIME": /* 7.2 Reflection data input */ case "HKLF": case "OMIT": case "SHEL": case "BASF": case "TWIN": case "EXTI": case "SWAT": case "HOPE": case "MERG": /* 7.3 Atom list and least-squares constraints */ case "SPEC": case "RESI": case "MOVE": case "ANIS": case "AFIX": case "HFIX": case "FRAG": case "FEND": case "EXYZ": //case "EXTI": case "EADP": case "EQIV": /* 7.4 The connectivity list */ case "CONN": case "PART": case "BIND": case "FREE": /* 7.5 Least-squares restraints */ case "DFIX": case "DANG": case "BUMP": case "SAME": case "SADI": case "CHIV": case "FLAT": case "DELU": case "SIMU": case "DEFS": case "ISOR": case "NCSY": case "SUMP": /* 7.6 Least-squares organization */ case "L.S.": case "CGLS": case "BLOC": case "DAMP": case "STIR": case "WGHT": case "FVAR": /* 7.7 Lists and tables */ case "BOND": case "CONF": case "MPLA": case "RTAB": case "HTAB": case "LIST": case "ACTA": case "SIZE": case "TEMP": case "WPDB": /* 7.8 Fouriers, peak search and lineprinter plots */ case "FMAP": case "GRID": case "PLAN": break; case "MOLE": /* NOT DOCUMENTED BUT USED BY PLATON */ break; case "SPGR": { // Line added by PLATON stating the spacegroup var st = Strings.Tokenize(line); //st[0]; // string command_again string spacegroup = st[1]; crystal.SpaceGroup = spacegroup; } break; case " ": { Debug.WriteLine($"Disrgarding line assumed to be added by PLATON: {line}"); /* All other is atom */ } break; default: { //Debug.WriteLine($"Assumed to contain an atom: {line}"); // this line gives an atom, because all lines not starting with // a ShelX command is an atom (that sucks!) var st = Strings.Tokenize(line); string atype = st[0]; //st[1]; // string scatt_factor string sa = st[2]; string sb = st[3]; string sc = st[4]; // skip the rest if (char.IsDigit(atype[1])) { // atom type has a one letter code atype = atype.Substring(0, 1); } else { var sb2 = new StringBuilder(); sb2.Append(atype[1]); atype = atype.Substring(0, 1) + sb2.ToString().ToLowerInvariant(); } double[] frac = new double[3]; frac[0] = FortranFormat.Atof(sa); // fractional coordinates frac[1] = FortranFormat.Atof(sb); frac[2] = FortranFormat.Atof(sc); Debug.WriteLine("fa,fb,fc: " + frac[0] + ", " + frac[1] + ", " + frac[2]); if (string.Equals(atype, "Q", StringComparison.OrdinalIgnoreCase)) { // ingore atoms named Q } else { Trace.TraceInformation("Adding atom: " + atype + ", " + frac[0] + ", " + frac[1] + ", " + frac[2]); IAtom atom = crystal.Builder.NewAtom(atype); atom.FractionalPoint3D = new Vector3(frac[0], frac[1], frac[2]); crystal.Atoms.Add(atom); Debug.WriteLine($"Atom added: {atom}"); } } break; } } line = input.ReadLine(); } return(crystal); }
private IChemFile readChemFile(IChemFile file) { IChemSequence seq = file.Builder.newChemSequence(); IChemModel model = file.Builder.newChemModel(); ICrystal crystal = null; int lineNumber = 0; Vector3d a, b, c; try { System.String line = input.ReadLine(); while (input.Peek() != -1 && line != null) { //logger.debug((lineNumber++) + ": ", line); if (line.StartsWith("frame:")) { //logger.debug("found new frame"); model = file.Builder.newChemModel(); crystal = file.Builder.newCrystal(); // assume the file format is correct //logger.debug("reading spacegroup"); line = input.ReadLine(); //logger.debug((lineNumber++) + ": ", line); crystal.SpaceGroup = line; //logger.debug("reading unit cell axes"); Vector3d axis = new Vector3d(); //logger.debug("parsing A: "); line = input.ReadLine(); //logger.debug((lineNumber++) + ": ", line); axis.x = FortranFormat.atof(line); line = input.ReadLine(); //logger.debug((lineNumber++) + ": ", line); axis.y = FortranFormat.atof(line); line = input.ReadLine(); //logger.debug((lineNumber++) + ": ", line); axis.z = FortranFormat.atof(line); crystal.A = axis; axis = new Vector3d(); //logger.debug("parsing B: "); line = input.ReadLine(); //logger.debug((lineNumber++) + ": ", line); axis.x = FortranFormat.atof(line); line = input.ReadLine(); //logger.debug((lineNumber++) + ": ", line); axis.y = FortranFormat.atof(line); line = input.ReadLine(); //logger.debug((lineNumber++) + ": ", line); axis.z = FortranFormat.atof(line); crystal.B = axis; axis = new Vector3d(); //logger.debug("parsing C: "); line = input.ReadLine(); //logger.debug((lineNumber++) + ": ", line); axis.x = FortranFormat.atof(line); line = input.ReadLine(); //logger.debug((lineNumber++) + ": ", line); axis.y = FortranFormat.atof(line); line = input.ReadLine(); //logger.debug((lineNumber++) + ": ", line); axis.z = FortranFormat.atof(line); crystal.C = axis; //logger.debug("Crystal: ", crystal); a = crystal.A; b = crystal.B; c = crystal.C; //logger.debug("Reading number of atoms"); line = input.ReadLine(); //logger.debug((lineNumber++) + ": ", line); int atomsToRead = System.Int32.Parse(line); //logger.debug("Reading no molecules in assym unit cell"); line = input.ReadLine(); //logger.debug((lineNumber++) + ": ", line); int Z = System.Int32.Parse(line); crystal.Z = Z; System.String symbol; double charge; Point3d cart; for (int i = 1; i <= atomsToRead; i++) { cart = new Point3d(); line = input.ReadLine(); //logger.debug((lineNumber++) + ": ", line); symbol = line.Substring(0, (line.IndexOf(":")) - (0)); charge = System.Double.Parse(line.Substring(line.IndexOf(":") + 1)); line = input.ReadLine(); //logger.debug((lineNumber++) + ": ", line); cart.x = System.Double.Parse(line); // x line = input.ReadLine(); //logger.debug((lineNumber++) + ": ", line); cart.y = System.Double.Parse(line); // y line = input.ReadLine(); //logger.debug((lineNumber++) + ": ", line); cart.z = System.Double.Parse(line); // z IAtom atom = file.Builder.newAtom(symbol); atom.setCharge(charge); // convert cartesian coords to fractional Point3d frac = CrystalGeometryTools.cartesianToFractional(a, b, c, cart); atom.setFractionalPoint3d(frac); crystal.addAtom(atom); //logger.debug("Added atom: ", atom); } model.Crystal = crystal; seq.addChemModel(model); } else { //logger.debug("Format seems broken. Skipping these lines:"); while (!line.StartsWith("frame:") && input.Peek() != -1 && line != null) { line = input.ReadLine(); //logger.debug(lineNumber++ + ": ", line); } //logger.debug("Ok, resynched: found new frame"); } } file.addChemSequence(seq); } catch (System.Exception exception) { //UPGRADE_TODO: The equivalent in .NET for method 'java.lang.Throwable.getMessage' may return a different value. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1043'" System.String message = "Error while parsing CrystClust file: " + exception.Message; //logger.error(message); //logger.debug(exception); throw new CDKException(message, exception); } return(file); }
private IChemFile ReadChemFile(IChemFile file) { IChemSequence seq = file.Builder.NewChemSequence(); IChemModel model = file.Builder.NewChemModel(); ICrystal crystal = null; int lineNumber = 0; Vector3 a, b, c; try { string line = input.ReadLine(); while (line != null) { Debug.WriteLine($"{lineNumber++}: {line}"); if (line.StartsWith("frame:", StringComparison.Ordinal)) { Debug.WriteLine("found new frame"); model = file.Builder.NewChemModel(); crystal = file.Builder.NewCrystal(); // assume the file format is correct Debug.WriteLine("reading spacegroup"); line = input.ReadLine(); Debug.WriteLine($"{lineNumber++}: {line}"); crystal.SpaceGroup = line; Debug.WriteLine("reading unit cell axes"); Vector3 axis = new Vector3(); Debug.WriteLine("parsing A: "); line = input.ReadLine(); Debug.WriteLine($"{lineNumber++}: {line}"); axis.X = FortranFormat.Atof(line); line = input.ReadLine(); Debug.WriteLine($"{lineNumber++}: {line}"); axis.Y = FortranFormat.Atof(line); line = input.ReadLine(); Debug.WriteLine($"{lineNumber++}: {line}"); axis.Z = FortranFormat.Atof(line); crystal.A = axis; axis = new Vector3(); Debug.WriteLine("parsing B: "); line = input.ReadLine(); Debug.WriteLine($"{lineNumber++}: {line}"); axis.X = FortranFormat.Atof(line); line = input.ReadLine(); Debug.WriteLine($"{lineNumber++}: {line}"); axis.Y = FortranFormat.Atof(line); line = input.ReadLine(); Debug.WriteLine($"{lineNumber++}: {line}"); axis.Z = FortranFormat.Atof(line); crystal.B = axis; axis = new Vector3(); Debug.WriteLine("parsing C: "); line = input.ReadLine(); Debug.WriteLine($"{lineNumber++}: {line}"); axis.X = FortranFormat.Atof(line); line = input.ReadLine(); Debug.WriteLine($"{lineNumber++}: {line}"); axis.Y = FortranFormat.Atof(line); line = input.ReadLine(); Debug.WriteLine($"{lineNumber++}: {line}"); axis.Z = FortranFormat.Atof(line); crystal.C = axis; Debug.WriteLine($"Crystal: {crystal}"); a = crystal.A; b = crystal.B; c = crystal.C; Debug.WriteLine("Reading number of atoms"); line = input.ReadLine(); Debug.WriteLine($"{lineNumber++}: {line}"); int atomsToRead = int.Parse(line, NumberFormatInfo.InvariantInfo); Debug.WriteLine("Reading no molecules in assym unit cell"); line = input.ReadLine(); Debug.WriteLine($"{lineNumber++}: {line}"); int Z = int.Parse(line, NumberFormatInfo.InvariantInfo); crystal.Z = Z; string symbol; double charge; Vector3 cart; for (int i = 1; i <= atomsToRead; i++) { cart = new Vector3(); line = input.ReadLine(); Debug.WriteLine($"{lineNumber++}: {line}"); symbol = line.Substring(0, line.IndexOf(':')); charge = double.Parse(line.Substring(line.IndexOf(':') + 1), NumberFormatInfo.InvariantInfo); line = input.ReadLine(); Debug.WriteLine($"{lineNumber++}: {line}"); cart.X = double.Parse(line, NumberFormatInfo.InvariantInfo); // x line = input.ReadLine(); Debug.WriteLine($"{lineNumber++}: {line}"); cart.Y = double.Parse(line, NumberFormatInfo.InvariantInfo); // y line = input.ReadLine(); Debug.WriteLine($"{lineNumber++}: {line}"); cart.Z = double.Parse(line, NumberFormatInfo.InvariantInfo); // z IAtom atom = file.Builder.NewAtom(symbol); atom.Charge = charge; // convert Cartesian coords to fractional Vector3 frac = CrystalGeometryTools.CartesianToFractional(a, b, c, cart); atom.FractionalPoint3D = frac; crystal.Atoms.Add(atom); Debug.WriteLine($"Added atom: {atom}"); } model.Crystal = crystal; seq.Add(model); } else { Debug.WriteLine("Format seems broken. Skipping these lines:"); while (line != null && !line.StartsWith("frame:", StringComparison.Ordinal)) { line = input.ReadLine(); Debug.WriteLine($"{lineNumber++}: {line}"); } Debug.WriteLine("Ok, resynched: found new frame"); } } file.Add(seq); } catch (Exception exception) { if (!(exception is IOException || exception is ArgumentException)) { throw; } string message = "Error while parsing CrystClust file: " + exception.Message; Trace.TraceError(message); Debug.WriteLine(exception); throw new CDKException(message, exception); } return(file); }
private IChemSequence readChemSequence(IChemSequence sequence) { IChemModel chemModel = sequence.Builder.newChemModel(); ICrystal crystal = null; // Get the info line (first token of the first line) //UPGRADE_ISSUE: Method 'java.io.BufferedReader.mark' was not converted. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1000_javaioBufferedReadermark_int'" //inputBuffer.mark(255); //long pos = inputBuffer.BaseStream.Position; info = nextVASPToken(false); //System.out.println(info); //UPGRADE_ISSUE: Method 'java.io.BufferedReader.reset' was not converted. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1000_javaioBufferedReaderreset'" //inputBuffer.reset(); // Get the number of different atom "NCLASS=X" //UPGRADE_ISSUE: Method 'java.io.BufferedReader.mark' was not converted. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1000_javaioBufferedReadermark_int'" //inputBuffer.mark(255); nextVASPTokenFollowing("NCLASS"); ntype = System.Int32.Parse(fieldVal); //System.out.println("NCLASS= " + ntype); //UPGRADE_ISSUE: Method 'java.io.BufferedReader.reset' was not converted. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1000_javaioBufferedReaderreset'" //inputBuffer.reset(); // Get the different atom names anames = new System.String[ntype]; nextVASPTokenFollowing("ATOM"); for (int i = 0; i < ntype; i++) { anames[i] = fieldVal; nextVASPToken(false); } // Get the number of atom of each type int[] natom_type = new int[ntype]; natom = 0; for (int i = 0; i < ntype; i++) { natom_type[i] = System.Int32.Parse(fieldVal); nextVASPToken(false); natom = natom + natom_type[i]; } // Get the representation type of the primitive vectors // only "Direct" is recognize now. representation = fieldVal; if (representation.Equals("Direct")) { //logger.info("Direct representation"); // DO NOTHING } else { throw new CDKException("This VASP file is not supported. Please contact the Jmol developpers"); } while (nextVASPToken(false) != null) { //logger.debug("New crystal started..."); crystal = sequence.Builder.newCrystal(); chemModel = sequence.Builder.newChemModel(); // Get acell for (int i = 0; i < 3; i++) { acell[i] = FortranFormat.atof(fieldVal); // all the same FIX? } // Get primitive vectors for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { nextVASPToken(false); rprim[i][j] = FortranFormat.atof(fieldVal); } } // Get atomic position int[] atomType = new int[natom]; double[][] xred = new double[natom][]; for (int i2 = 0; i2 < natom; i2++) { xred[i2] = new double[3]; } int atomIndex = 0; for (int i = 0; i < ntype; i++) { for (int j = 0; j < natom_type[i]; j++) { try { atomType[atomIndex] = IsotopeFactory.getInstance(sequence.Builder).getElement(anames[i]).AtomicNumber; } catch (System.Exception exception) { throw new CDKException("Could not determine atomic number!", exception); } //logger.debug("aname: " + anames[i]); //logger.debug("atomType: " + atomType[atomIndex]); nextVASPToken(false); xred[atomIndex][0] = FortranFormat.atof(fieldVal); nextVASPToken(false); xred[atomIndex][1] = FortranFormat.atof(fieldVal); nextVASPToken(false); xred[atomIndex][2] = FortranFormat.atof(fieldVal); atomIndex = atomIndex + 1; // FIXME: store atom } } crystal.A = new Vector3d(rprim[0][0] * acell[0], rprim[0][1] * acell[0], rprim[0][2] * acell[0]); crystal.B = new Vector3d(rprim[1][0] * acell[1], rprim[1][1] * acell[1], rprim[1][2] * acell[1]); crystal.C = new Vector3d(rprim[2][0] * acell[2], rprim[2][1] * acell[2], rprim[2][2] * acell[2]); for (int i = 0; i < atomType.Length; i++) { System.String symbol = "Du"; try { symbol = IsotopeFactory.getInstance(sequence.Builder).getElement(atomType[i]).Symbol; } catch (System.Exception exception) { throw new CDKException("Could not determine element symbol!", exception); } IAtom atom = sequence.Builder.newAtom(symbol); atom.AtomicNumber = atomType[i]; // convert fractional to cartesian double[] frac = new double[3]; frac[0] = xred[i][0]; frac[1] = xred[i][1]; frac[2] = xred[i][2]; atom.setFractionalPoint3d(new Point3d(frac[0], frac[1], frac[2])); crystal.addAtom(atom); } crystal.setProperty(CDKConstants.REMARK, info); chemModel.Crystal = crystal; //logger.info("New Frame set!"); sequence.addChemModel(chemModel); } //end while return(sequence); }