Пример #1
0
        public void LoadResultsFromDisk()
        {
            if (Motor == null)
            {
                throw new InvalidOperationException("Motor hasn't been assigned");
            }

            generateOutputPath();

            Results = TransientResults.loadResultsFromFile(Path_TransResultsFile);

            // if load success, link it to current transient analysis
            // even they aren't match, no problem
            if (Results != null)
            {
                Results.TransAnalysis = this;
            }
        }
Пример #2
0
        public static TransientResults loadResultsFromFile(String fn)
        {
            if (!File.Exists(fn))
            {
                return(null);
            }

            using (StreamReader sr = new StreamReader(fn))
            {
                try
                {
                    TransientResults results = JsonConvert.Import <TransientResults>(sr);
                    return(results);
                }
                catch (Exception e)
                {
                    log.Error("Fail open " + fn + ".Error: " + e.Message);
                    return(null);
                }
            }
        }
Пример #3
0
        /// <summary>
        /// Start transient analysis.
        /// Assuming existed a FEMM file was created. This method only rotates the rotor and do analyze.
        /// If file doesn't existed, we are doom
        /// </summary>
        /// <param name="config"></param>
        public void RunAnalysisSync()
        {
            if (Motor == null)
            {
                throw new InvalidOperationException("Motor hasn't been assigned");
            }

            if (!File.Exists(Motor.Path_FEMMFile))
            {
                log.Error("File doesn't exist: " + Motor.Path_FEMMFile);
                return;
            }

            // create path first
            generateOutputPath();

            // create output directory
            Directory.CreateDirectory(OutDir);

            // write config to disk (for explorer manually can read and understand what the hell are in this folder)
            writeConfigToDisk();

            // load results from disk
            TransientResults existedResults = TransientResults.loadResultsFromFile(Path_TransResultsFile);

            log.Info("Output results file:" + Path_TransResultsFile);
            // if has
            if (existedResults != null)
            {
                if (GetMD5String() == existedResults.MD5String)
                {
                    log.Info("Transient analysis already been done.");
                    Results = existedResults;
                    Results.TransAnalysis = this;//this results is match with config, so link them
                    OnFinishedAnalysis(this, Results);
                    return;
                }
                log.Info("Transient analysis exists but different. New analysis will be done.");
            }

            // create new
            Results           = new TransientResults(this);
            Results.MD5String = GetMD5String();//assign md5 (signature of this)

            // for multi-thread config
            int threadCount = 4;

            ManualResetEvent[] MREs  = new ManualResetEvent[threadCount];
            Queue <int>        steps = new Queue <int>();

            for (int i = 0; i < StepCount + 1; i++)
            {
                steps.Enqueue(i);
            }

            // start all threads
            for (int i = 0; i < threadCount; i++)
            {
                MREs[i] = new ManualResetEvent(false);
                ManualResetEvent mre = MREs[i];
                new Thread(delegate()
                {
                    FEMM femm = new FEMM();
                    while (steps.Count > 0)
                    {
                        int step = steps.Dequeue();
                        AnalyzeOne(step, femm);
                    }

                    mre.Set();
                }).Start();
            }

            // wait for all thread to finish
            for (int i = 0; i < threadCount; i++)
            {
                MREs[i].WaitOne();
            }

            // save results data to disk
            Results.saveResultsToFile(Path_TransResultsFile);

            // finished event
            OnFinishedAnalysis(this, Results);
        }