Beispiel #1
0
        public void ReadFromXml(string filename)
        {
            XmlDocument xmlDocument = new XmlDocument();

            string[] splitedFilename = filename.Split('.');
            string   suffix          = splitedFilename[splitedFilename.Length - 1];

            if (suffix == "gz")
            {
                FileStream fileStream           = new FileStream(filename, System.IO.FileMode.Open);
                GZipStream compressedFileStream = new GZipStream(fileStream, CompressionMode.Decompress);
                xmlDocument.Load(compressedFileStream);
                compressedFileStream.Close();
                fileStream.Close();
            }
            else
            {
                xmlDocument.Load(filename);
            }



            XmlNode xmlDomain               = xmlDocument.DocumentElement.SelectSingleNode("domain");
            XmlNode xmlParameters           = xmlDomain.SelectSingleNode("parameters");
            XmlNode xmlPhases               = xmlDomain.SelectSingleNode("phases");
            XmlNode xmlInterPhaseParameters = xmlDomain.SelectSingleNode("interphase-parameters");
            XmlNode xmlParticles            = xmlDocument.DocumentElement.SelectSingleNode("particles");


            double domainSizeX    = 0.0;
            double domainSizeY    = 0.0;
            int    numberOfCellsX = 0;
            int    numberOfCellsY = 0;

            foreach (XmlNode xmlParameter in xmlParameters)
            {
                var     parameterName = xmlParameter.Attributes["name"].Value;
                var     parameterType = xmlParameter.Attributes["type"].Value;
                dynamic parameterValue;
                if (parameterType == "double")
                {
                    parameterValue = Convert.ToDouble(xmlParameter.InnerText, CultureInfo.InvariantCulture);
                }
                else if (parameterType == "int")
                {
                    parameterValue = Convert.ToInt32(xmlParameter.InnerText, CultureInfo.InvariantCulture);
                }
                else
                {
                    parameterValue = Convert.ToString(xmlParameter.InnerText, CultureInfo.InvariantCulture);
                }

                switch (parameterName)
                {
                case "HDR":
                    HDR = parameterValue;
                    break;

                case "XCV":
                    domainSizeX = parameterValue;
                    break;

                case "YCV":
                    domainSizeY = parameterValue;
                    break;

                case "NCX":
                    numberOfCellsX = parameterValue;
                    break;

                case "NCY":
                    numberOfCellsY = parameterValue;
                    break;

                default:
                    AddParameter(parameterName, parameterValue);
                    break;
                }
            }
            SetSize(domainSizeX, domainSizeY, numberOfCellsX, numberOfCellsY);

            _phases = new List <Phase>(xmlPhases.ChildNodes.Count);
            Phase.ResetNumberOfCreatedPhases();

            foreach (XmlNode xmlPhase in xmlPhases)
            {
                var    phaseName     = xmlPhase.Attributes["name"].Value;
                var    phaseId       = Convert.ToInt32(xmlPhase.Attributes["id"].Value);
                var    phaseLocation = xmlPhase["location"].InnerText.Trim();
                byte[] color         = new byte[4];
                foreach (XmlAttribute xmlAttributeColor in xmlPhase["color"].Attributes)
                {
                    switch (xmlAttributeColor.Name)
                    {
                    case "alpha":
                        color[0] = Convert.ToByte(xmlAttributeColor.Value);
                        break;

                    case "red":
                        color[1] = Convert.ToByte(xmlAttributeColor.Value);
                        break;

                    case "green":
                        color[2] = Convert.ToByte(xmlAttributeColor.Value);
                        break;

                    case "blue":
                        color[3] = Convert.ToByte(xmlAttributeColor.Value);
                        break;
                    }
                }

                Phase phase = AddPhase(phaseId, phaseName, phaseLocation, color);

                XmlNode xmlPhaseFields = xmlPhase.SelectSingleNode("fields");
                foreach (XmlNode xmlPhaseField in xmlPhaseFields)
                {
                    var phaseFieldName  = xmlPhaseField.Attributes["name"].Value;
                    var phaseFieldType  = xmlPhaseField.Attributes["type"].Value;
                    var phaseFieldValue = xmlPhaseField.InnerText;
                    phase.AddField(phaseFieldName, phaseFieldType, phaseFieldValue);
                }
            }

            XmlNode xmlSurfaceTensionCoefficient = xmlInterPhaseParameters.SelectSingleNode("surface-tension-coefficient");
            InterPhaseCoefficients surfaceTensionCoefficients = AddInterPhaseParameter("surface-tension-coefficient");

            surfaceTensionCoefficients.Update();
            foreach (XmlNode xmlValue in xmlSurfaceTensionCoefficient)
            {
                int    phaseId1 = Convert.ToInt32(xmlValue.Attributes["phaseId1"].Value);
                int    phaseId2 = Convert.ToInt32(xmlValue.Attributes["phaseId2"].Value);
                double value    = Convert.ToDouble(xmlValue.InnerText, CultureInfo.InvariantCulture);
                Phase  phase1   = GetPhase(phaseId1);
                Phase  phase2   = GetPhase(phaseId2);
                surfaceTensionCoefficients.Set(phase1.Name, phase2.Name, value);
            }

            XmlNode xmlInterfaceCorrectionCoefficient = xmlInterPhaseParameters.SelectSingleNode("interface-correction-coefficient");
            InterPhaseCoefficients interfaceCorrectionCoefficients = AddInterPhaseParameter("interface-correction-coefficient");

            interfaceCorrectionCoefficients.Update();
            foreach (XmlNode xmlValue in xmlInterfaceCorrectionCoefficient)
            {
                int    phaseId1 = Convert.ToInt32(xmlValue.Attributes["phaseId1"].Value);
                int    phaseId2 = Convert.ToInt32(xmlValue.Attributes["phaseId2"].Value);
                double value    = Convert.ToDouble(xmlValue.InnerText, CultureInfo.InvariantCulture);
                Phase  phase1   = GetPhase(phaseId1);
                Phase  phase2   = GetPhase(phaseId2);
                interfaceCorrectionCoefficients.Set(phase1.Name, phase2.Name, value);
            }

            List <string>   particleFields      = new List <string>();
            List <string>   particleTypes       = new List <string>();
            List <string[]> particleFieldValues = new List <string[]>();

            foreach (XmlNode xmlParticleField in xmlParticles)
            {
                particleFields.Add(xmlParticleField.Attributes["name"].Value);
                particleTypes.Add(xmlParticleField.Attributes["type"].Value);
                particleFieldValues.Add(xmlParticleField.InnerText.Trim().Split());
            }

            List <Particle> particles = new List <Particle>(particleFieldValues[0].Length);

            for (int i = 0; i < particleFieldValues[0].Length; i++)
            {
                Hashtable fields = new Hashtable();
                for (int j = 0; j < particleFields.Count; j++)
                {
                    string  particleFieldName = particleFields[j];
                    string  particleFieldType = particleTypes[j];
                    dynamic particleFieldValue;
                    switch (particleFieldType)
                    {
                    case "double":
                        particleFieldValue = Convert.ToDouble(particleFieldValues[j][i], CultureInfo.InvariantCulture);
                        break;

                    case "int":
                        particleFieldValue = Convert.ToInt32(particleFieldValues[j][i], CultureInfo.InvariantCulture);
                        break;

                    default:
                        particleFieldValue = particleFieldValues[j][i];
                        break;
                    }

                    fields.Add(particleFieldName, particleFieldValue);
                }
                particles.Add(new Particle(fields));
            }

            foreach (Phase phase in _phases)
            {
                List <Particle> particlesInPhase = new List <Particle>();
                int             phaseId          = phase.Id;
                foreach (Particle particle in particles)
                {
                    if (particle.PhaseId == phaseId)
                    {
                        particlesInPhase.Add(particle);
                    }
                }
                phase.Particles = particlesInPhase;
            }
        }
Beispiel #2
0
        private void ButtonDesignerAddPhase_Clicked(object sender, RoutedEventArgs e)
        {
            string phaseName = "New phase " + Convert.ToString(_defaultPhaseNoIndicator);
            //ListBoxDesignerPhases.Items.Add(phaseName);
            TextBlock textBlock = new TextBlock();

            textBlock.Text = phaseName;
            ListBoxDesignerPhases.Items.Add(textBlock);
            Phase phase = _domain.AddPhase(phaseName);

            phase.AddField("x-velocity", "double", "0.0");
            phase.AddField("y-velocity", "double", "0.0");
            phase.AddField("z-velocity", "double", "0.0");
            phase.AddField("density", "double", "1.0");
            phase.AddField("initial density", "double", "1.0");
            phase.AddField("dynamic viscosity", "double", "0.01");
            phase.AddField("sound speed", "double", "10.0");
            phase.AddField("equation of state coefficient", "double", "15.0");
            phase.AddField("gamma", "double", "7.0");
            phase.AddField("temperature", "double", "0.0");
            phase.AddField("heat capacity", "double", "0.0");
            phase.AddField("heat conductivity", "double", "0.0");
            phase.AddField("color function", "double", phase.Id.ToString());
            _defaultPhaseNoIndicator++;
        }