/// <summary> /// Build a parameter collection from all properties and their values to a list of parameters /// </summary> /// <param name="groupName">Group name for this object</param> /// <param name="obj"></param> /// <param name="_public_param_">true if get public set-able-properties (input), /// false if get non-setable-public properties (output)</param> /// <returns></returns> public static ParametersCollection FromObject(String groupName, object obj) { if (obj == null) { return(null); } ParametersCollection pc = new ParametersCollection(); Type type = obj.GetType(); PropertyInfo[] properties = type.GetProperties(); foreach (PropertyInfo pi in properties) { if (pi.GetSetMethod() == null) { continue; } // if exclude in params, then ignore bool exludeThisProperty = false; foreach (ExcludeFromParamsAttribute a in pi.GetCustomAttributes(typeof(ExcludeFromParamsAttribute), false)) { exludeThisProperty = true; } if (exludeThisProperty) { continue; } // new one Parameter ppp = new Parameter(); ppp.group = groupName; ppp.name = pi.Name; object value = pi.GetValue(obj, null); if (value != null) { ppp.text = value.ToString(); ppp.value = value; } foreach (ParamInfoAttribute a in pi.GetCustomAttributes(typeof(ParamInfoAttribute), false)) { ppp.desc = a.HelpText; } ppp.valueType = pi.PropertyType; ppp.status = "-"; pc.Add(ppp); } return(pc); }
public void doSweep() { // update ID and MD5 in params table before do sweep updateID_MD5_inParamsTable(); // copy params from original ParametersCollection swParams = MotorToParams(CurrentMotor); // new result table ResultsTable = new DataTable(); ResultsTable.TableName = RESULTS_TABLE; ResultsTable.Columns.Add(COL_ID, typeof(int)); ResultsTable.Columns.Add(COL_MD5, typeof(string)); ResultsTable.Columns.Add(COL_STATUS, typeof(string)); bool newlyTable = true; // apply sweeping param from table // each variant foreach (DataRow paramRow in ParamsTable.Rows) { int variantNum = ParamsTable.Rows.IndexOf(paramRow); // update params from this row (variant) foreach (DataColumn dc in ParamsTable.Columns) { String colname = dc.ColumnName; foreach (Parameter p in swParams) { if (p.fullname == colname) { p.text = paramRow[colname].ToString(); p.EvaluateValue(); break; } } } // create new motor from parameters AbstractMotor motor = ParamsToMotor(swParams); // motor calculation motor.CalcPointsCoordinates(); // if params invalid, just move on if (!motor.IsParamsValid()) { log.Error(String.Format("Variant {0} invalid:", variantNum)); paramRow[COL_STATUS] = "Error: "; foreach (ParamValidationInfo pvi in motor.ListParamsValidation) { if (pvi.msgType == ParamValidationInfo.MessageType.Error) { log.Error("\t+ " + pvi.message); } //update error in param row paramRow[COL_STATUS] += pvi.message + "; "; } //resultRow also DataRow rr = ResultsTable.NewRow(); rr[COL_ID] = paramRow[COL_ID]; rr[COL_MD5] = paramRow[COL_MD5]; rr[COL_STATUS] = paramRow[COL_STATUS]; ResultsTable.Rows.Add(rr); continue; } else { paramRow[COL_STATUS] = "OK"; } //// organize results ParametersCollection pc = new ParametersCollection(); // calc gmc AbstractAnalyticalAnalyser aa = motor.GetAnalyticalAnalyser(); aa.RunAnalysis(); pc.AddRange(ParametersCollection.FromObject("analytic", aa.Results)); // femm if (EnableFEMAnalysis) { String outdir = OutputFolder; String femFile = outdir + "\\" + motor.GetMD5String() + ".FEM"; Directory.CreateDirectory(outdir); if (!motor.isFEMModelReady(femFile)) { motor.BuildFEMModel(femFile); } AbstractStaticAnalyser staticAnalyser = motor.GetStaticAnalyser(); staticAnalyser.Path_OriginalFEMFile = femFile; staticAnalyser.OutputPath = outdir; staticAnalyser.DoAnalysisOnOriginalFEMFile = true; staticAnalyser.RunAnalysis(); pc.AddRange(ParametersCollection.FromObject("fem", staticAnalyser.Results)); } Dictionary <String, int> countsParam = new Dictionary <string, int>(); // find params match by name between analytic and fem foreach (Parameter p in pc) { if (countsParam.ContainsKey(p.name)) { countsParam[p.name]++; } else { countsParam[p.name] = 1; } } // add comparison columns foreach (String name in countsParam.Keys) { if (countsParam[name] == 2) { Parameter p = new Parameter(); p.group = "xCompare"; p.name = name; double d1 = (double)pc.FindParameter("analytic", name).value; double d2 = (double)pc.FindParameter("fem", name).value; p.value = String.Format("{0:F3}%", (d1 - d2) / d2 * 100.0); p.valueType = typeof(String); pc.Add(p); } } // sort columns pc = new ParametersCollection(pc.OrderBy(param => param.name).ThenBy(param => param.group).ToList()); // 1 times create if (newlyTable) { foreach (Parameter p in pc) { if (!p.valueType.IsArray)//don't care about array (airgap flux-density distribution) for now { ResultsTable.Columns.Add(p.fullname, p.valueType); } } newlyTable = false; } // assign this variant results to row of results table DataRow resultRow = ResultsTable.NewRow(); foreach (Parameter p in pc) { if (resultRow.Table.Columns.Contains(p.fullname)) { resultRow[p.fullname] = p.value; } } resultRow[COL_ID] = paramRow[COL_ID]; resultRow[COL_MD5] = paramRow[COL_MD5]; resultRow[COL_STATUS] = paramRow[COL_STATUS]; ResultsTable.Rows.Add(resultRow); }// for each variant // raise event ParamSweeperResults r = new ParamSweeperResults(); r.ResultsTable = ResultsTable; OnFinishSweep(this, r); saveDataToDisk(); }