コード例 #1
0
        /// <summary>
        /// This is the method that actually does the work.
        /// </summary>
        /// <param name="DA">The DA object is used to retrieve from inputs and store in outputs.</param>
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            int sleeptime = 1500;

            if (!DA.GetData(3, ref sleeptime))
            {
                sleeptime = 1500;
            }

            int folderint = 0;

            if (!DA.GetData(4, ref folderint))
            {
                folderint = 0;
            }
            string path_in  = @"c:\eplus\EPOpti17\Input" + folderint + @"\";
            string path_out = @"c:\eplus\EPOpti17\Output" + folderint + @"\";
            string eplusexe = @"c:\eplus\EPOpti17\Input" + folderint + @"\ep\energyplus.exe";

            //get idf and weather files
            string idffile = @"blabla";

            if (!DA.GetData(0, ref idffile))
            {
                return;
            }
            string weatherfile = @"blabla";

            if (!DA.GetData(1, ref weatherfile))
            {
                return;
            }


            //RUN SIMU
            bool runit = false;

            if (!DA.GetData(2, ref runit))
            {
                return;
            }


            //get input parameters
            int dvar = 13;

            double[] x = new double[dvar];
            for (int i = 0; i < x.Length; i++)
            {
                if (!DA.GetData(i + 6, ref x[i]))
                {
                    return;
                }
            }
            ;


            if (runit == true)
            {
                //***********************************************************************************
                //***********************************************************************************
                //***********************************************************************************
                //modify idf file with parameters and save as new idf file
                string idfmodified = idffile + "_modi";

                //load idf into a huge string
                string[] lines;
                var      list       = new List <string>();
                var      fileStream = new FileStream(path_in + idffile + ".idf", FileMode.Open, FileAccess.Read);
                using (var streamReader = new StreamReader(fileStream))
                {
                    string line;
                    while ((line = streamReader.ReadLine()) != null)
                    {
                        list.Add(line);
                    }
                }
                lines = list.ToArray();
                fileStream.Close();

                //to be replaced
                string[] replacethis = new string[dvar + 1];
                for (int i = 0; i < replacethis.Length; i++)
                {
                    replacethis[i] = @"%" + i.ToString() + @"%";
                }


                //replacers
                string[] replacers = new string[replacethis.Length];
                for (int i = 0; i < dvar; i++)
                {
                    replacers[i] = x[i].ToString();
                }


                replacers[13] = (x[8] * 0.0006875 - 0.00025).ToString();


                //constraints
                bool[] gx = new bool[4];        //true means constraint violation
                if (x[0] - x[1] + 0.5488 >= 0)
                {
                    gx[0] = true;
                }
                if (x[2] - x[3] + 0.5488 >= 0)
                {
                    gx[1] = true;
                }
                if (x[4] - x[5] + 0.5488 >= 0)
                {
                    gx[2] = true;
                }
                if (x[6] - x[7] + 0.5488 >= 0)
                {
                    gx[3] = true;
                }


                //scan string for keywords and replace them with parameters
                for (int i = 0; i < lines.Length; i++)
                {
                    for (int u = 0; u < replacethis.Length; u++)
                    {
                        lines[i] = lines[i].Replace(replacethis[u], replacers[u]);
                    }
                }



                //write a new idf file
                File.WriteAllLines(path_in + idfmodified + ".idf", lines);
                string idffilenew    = path_in + idfmodified + ".idf";
                string weatherfilein = path_in + @"ep\WeatherData\" + weatherfile + ".epw";



                //***********************************************************************************
                //***********************************************************************************
                //***********************************************************************************
                //run eplus
                string command = @" -w " + weatherfilein + @" -d " + path_out + @" " + idffilenew;
                Misc.RunEplus(eplusexe, command);



                //***********************************************************************************
                //***********************************************************************************
                //***********************************************************************************
                //process Outputs
                string outputfile = "eplustbl.csv";
                while (!File.Exists(path_out + outputfile))
                {
                    Console.WriteLine("waiting");
                }



                //output result
                double result = double.NaN;
                //identify correct result file. load it. get the right numbers from it
                lines      = new string[] { };
                list       = new List <string>();
                fileStream = new FileStream(path_out + outputfile, FileMode.Open, FileAccess.Read);
                using (var streamReader = new StreamReader(fileStream))
                {
                    string line;
                    while ((line = streamReader.ReadLine()) != null)
                    {
                        list.Add(line);
                    }
                }
                lines = list.ToArray();
                fileStream.Close();

                double Fx;


                string[] split;
                char     delimiter = ',';

                split = lines[16].Split(delimiter);
                string MJm2 = split[4];

                Fx = (Convert.ToDouble(gx[0]) * 0.5 +
                      Convert.ToDouble(gx[1]) * 0.5 +
                      Convert.ToDouble(gx[2]) * 0.5 +
                      Convert.ToDouble(gx[3]) * 0.5 +
                      1) * Convert.ToDouble(MJm2);


                result = Fx;
                System.Threading.Thread.Sleep(sleeptime);
                System.IO.File.Delete(path_in + idfmodified + ".idf");
                System.IO.DirectoryInfo di = new DirectoryInfo(path_out);

                foreach (FileInfo file in di.GetFiles())
                {
                    file.Delete();
                }
                foreach (DirectoryInfo dir in di.GetDirectories())
                {
                    dir.Delete(true);
                }



                DA.SetData(0, result);
            }
        }