예제 #1
0
        private void PossiblySetCellParams(double a, double b, double c, double alpha, double beta, double gamma)
        {
            if (a != 0.0 && b != 0.0 && c != 0.0 && alpha != 0.0 && beta != 0.0 && gamma != 0.0)
            {
                Trace.TraceInformation("Found and set crystal cell parameters");
                var axes = CrystalGeometryTools.NotionalToCartesian(a, b, c, alpha, beta, gamma);

                crystal.A = axes[0];
                crystal.B = axes[1];
                crystal.C = axes[2];
            }
        }
예제 #2
0
        public void TestRoundTrip()
        {
            Crystal crystal = new Crystal();
            double  a       = 3.0;
            double  b       = 5.0;
            double  c       = 7.0;
            double  alpha   = 90.0;
            double  beta    = 110.0;
            double  gamma   = 100.0;
            var     axes    = CrystalGeometryTools.NotionalToCartesian(a, b, c, alpha, beta, gamma);

            crystal.A = axes[0];
            crystal.B = axes[1];
            crystal.C = axes[2];

            // serialazing
            StringWriter sWriter   = new StringWriter();
            ShelXWriter  resWriter = new ShelXWriter(sWriter);

            resWriter.Write(crystal);
            resWriter.Close();
            string resContent = sWriter.ToString();

            // deserialazing
            ShelXReader resReader = new ShelXReader(new StringReader(resContent));
            ICrystal    rCrystal  = (ICrystal)resReader.Read(new Crystal());

            // OK, do checking
            Assert.IsNotNull(rCrystal);
            Assert.AreEqual(crystal.A.X, rCrystal.A.X, 0.001);
            Assert.AreEqual(crystal.A.Y, rCrystal.A.Y, 0.001);
            Assert.AreEqual(crystal.A.Z, rCrystal.A.Z, 0.001);
            Assert.AreEqual(crystal.B.X, rCrystal.B.X, 0.001);
            Assert.AreEqual(crystal.B.Y, rCrystal.B.Y, 0.001);
            Assert.AreEqual(crystal.B.Z, rCrystal.B.Z, 0.001);
            Assert.AreEqual(crystal.C.X, rCrystal.C.X, 0.001);
            Assert.AreEqual(crystal.C.Y, rCrystal.C.Y, 0.001);
            Assert.AreEqual(crystal.C.Z, rCrystal.C.Z, 0.001);
        }
예제 #3
0
        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);
        }