/// <summary> /// Fem入力データをファイルへ書き込み /// </summary> /// <param name="filename">ファイル名(*.fem)</param> /// <param name="nodes">節点リスト</param> /// <param name="elements">要素リスト</param> /// <param name="ports">ポートの節点番号リストのリスト</param> /// <param name="forceBCNodes">強制境界節点番号リスト</param> /// <param name="incidentPortNo">入射ポート番号</param> /// <param name="medias">媒質情報リスト</param> /// <param name="firstWaveLength">計算開始波長</param> /// <param name="lastWaveLength">計算終了波長</param> /// <param name="calcCnt">計算件数</param> /// <param name="wgStructureDv">導波路構造区分</param> /// <param name="waveModeDv">波のモード区分</param> /// <param name="lsEqnSolverDv">線形方程式解法区分</param> /// <param name="waveguideWidthForEPlane">導波路幅(E面解析用)</param> /// <returns></returns> public static void SaveToFile (string filename, IList <FemNode> nodes, IList <FemElement> elements, IList <IList <int> > ports, IList <int> forceBCNodes, int incidentPortNo, MediaInfo[] medias, double firstWaveLength, double lastWaveLength, int calcCnt, FemSolver.WGStructureDV wgStructureDv, FemSolver.WaveModeDV waveModeDv, FemSolver.LinearSystemEqnSoverDV lsEqnSolverDv, double waveguideWidthForEPlane ) { int nodeCnt = nodes.Count; IList <double[]> doubleCoords = new List <double[]>(); foreach (FemNode femNode in nodes) { doubleCoords.Add(femNode.Coord); } int elementCnt = elements.Count; IList <int[]> in_elements = new List <int[]>(); foreach (FemElement femElement in elements) { int cnt = 2 + femElement.NodeNumbers.Length; int[] in_element = new int[cnt]; in_element[0] = femElement.No; in_element[1] = femElement.MediaIndex; for (int ino = 0; ino < femElement.NodeNumbers.Length; ino++) { in_element[2 + ino] = femElement.NodeNumbers[ino]; } in_elements.Add(in_element); } int portCnt = ports.Count; int[] forceBCNodeNumbers = forceBCNodes.ToArray(); SaveToFileFromCad( filename, nodeCnt, doubleCoords, elementCnt, in_elements, portCnt, ports, forceBCNodeNumbers, incidentPortNo, medias, firstWaveLength, lastWaveLength, calcCnt, wgStructureDv, waveModeDv, lsEqnSolverDv, waveguideWidthForEPlane); }
/// <summary> /// Fem入力データをファイルへ書き込み /// </summary> /// <param name="filename">ファイル名(*.fem)</param> /// <param name="nodes">節点リスト</param> /// <param name="elements">要素リスト</param> /// <param name="ports">ポートの節点番号リストのリスト</param> /// <param name="forceBCNodes">強制境界節点番号リスト</param> /// <param name="incidentPortNo">入射ポート番号</param> /// <param name="medias">媒質情報リスト</param> /// <param name="firstWaveLength">計算開始波長</param> /// <param name="lastWaveLength">計算終了波長</param> /// <param name="calcCnt">計算件数</param> /// <param name="waveModeDv">波のモード区分</param> /// <returns></returns> public static void SaveToFile (string filename, IList <FemNode> nodes, IList <FemElement> elements, IList <IList <int> > ports, IList <int> forceBCNodes, IList <IList <uint> > elemNoPeriodicList, IList <IList <IList <int> > > nodePeriodicBList, IList <IList <int> > defectNodePeriodicList, int incidentPortNo, MediaInfo[] medias, double firstWaveLength, double lastWaveLength, int calcCnt, FemSolver.WaveModeDV waveModeDv ) { int nodeCnt = nodes.Count; IList <double[]> doubleCoords = new List <double[]>(); foreach (FemNode femNode in nodes) { doubleCoords.Add(femNode.Coord); } int elementCnt = elements.Count; IList <int[]> in_elements = new List <int[]>(); foreach (FemElement femElement in elements) { int cnt = 2 + femElement.NodeNumbers.Length; int[] in_element = new int[cnt]; in_element[0] = femElement.No; in_element[1] = femElement.MediaIndex; for (int ino = 0; ino < femElement.NodeNumbers.Length; ino++) { in_element[2 + ino] = femElement.NodeNumbers[ino]; } in_elements.Add(in_element); } int portCnt = ports.Count; int[] forceBCNodeNumbers = forceBCNodes.ToArray(); SaveToFileFromCad( filename, nodeCnt, doubleCoords, elementCnt, in_elements, portCnt, ports, forceBCNodeNumbers, elemNoPeriodicList, nodePeriodicBList, defectNodePeriodicList, incidentPortNo, medias, firstWaveLength, lastWaveLength, calcCnt, waveModeDv); }
/// <summary> /// コンストラクタ /// </summary> /// <param name="normalizedFreq1">計算開始規格化周波数</param> /// <param name="normalizedFreq2">計算終了規格化周波数</param> /// <param name="calcFreqCnt">計算点数</param> /// <param name="waveModeDv">モード区分</param> /// <param name="elemShapeDv">要素形状区分</param> /// <param name="elemOrder">要素次数</param> public CalcSettingFrm(double normalizedFreq1, double normalizedFreq2, int calcFreqCnt, FemSolver.WaveModeDV waveModeDv, Constants.FemElementShapeDV elemShapeDv, int elemOrder) { InitializeComponent(); DialogResult = DialogResult.None; // フィールドに格納 NormalizedFreq1 = normalizedFreq1; NormalizedFreq2 = normalizedFreq2; CalcFreqCnt = calcFreqCnt; WaveModeDv = waveModeDv; ElemShapeDv = elemShapeDv; ElemOrder = elemOrder; if (CalcFreqCnt == 0) { // 既定値を設定 NormalizedFreq1 = Constants.DefNormalizedFreqRange[0]; NormalizedFreq2 = Constants.DefNormalizedFreqRange[1]; CalcFreqCnt = Constants.DefCalcFreqencyPointCount; } // GUIにセット // 計算範囲 textBoxMinFreq.Text = string.Format("{0:F5}", NormalizedFreq1); textBoxMaxFreq.Text = string.Format("{0:F5}", NormalizedFreq2); double delta = (NormalizedFreq2 - NormalizedFreq1) / CalcFreqCnt; textBoxDeltaFreq.Text = string.Format("{0:F5}", delta); // 計算モード RadioBtnModeDvs = new RadioButton[] { radioBtnWaveModeDvTE, radioBtnWaveModeDvTM }; FemSolver.WaveModeDV[] waveModeDvOf_radioBtnModeDvs = { FemSolver.WaveModeDV.TE, FemSolver.WaveModeDV.TM }; for (int i = 0; i < RadioBtnModeDvs.Length; i++) { RadioBtnModeDvs[i].Tag = waveModeDvOf_radioBtnModeDvs[i]; if ((FemSolver.WaveModeDV)RadioBtnModeDvs[i].Tag == WaveModeDv) { RadioBtnModeDvs[i].Checked = true; } } // 要素形状・次数 ElemShapeStruct[] esList = { new ElemShapeStruct(Constants.FemElementShapeDV.Triangle, Constants.SecondOrder, "2次三角形要素"), new ElemShapeStruct(Constants.FemElementShapeDV.Triangle, Constants.FirstOrder, "1次三角形要素"), }; foreach (ElemShapeStruct es in esList) { cboxElemShapeDv.Items.Add(es); if (es.ElemShapeDv == ElemShapeDv && es.Order == ElemOrder) { cboxElemShapeDv.SelectedItem = es; } } }
/// <summary> /// [実行]ボタンクリックイベントハンドラ /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnRun_Click(object sender, EventArgs e) { // GUIからデータを取得 // 計算範囲 double minFreq = double.Parse(textBoxMinFreq.Text); double maxFreq = double.Parse(textBoxMaxFreq.Text); double deltaFreq = double.Parse(textBoxDeltaFreq.Text); FemSolver.WaveModeDV waveModeDv = WaveModeDv; foreach (RadioButton rbtn in RadioBtnModeDvs) { if (rbtn.Checked) { waveModeDv = (FemSolver.WaveModeDV)rbtn.Tag; break; } } // 要素形状・次数 ElemShapeStruct selectedEs = (ElemShapeStruct)cboxElemShapeDv.SelectedItem; if (maxFreq - minFreq < Constants.PrecisionLowerLimit) { MessageBox.Show("開始と終了が同じか逆転しています"); return; } if (deltaFreq < Constants.PrecisionLowerLimit) { MessageBox.Show("計算間隔が設定されていません"); return; } //int cnt = (int)((double)(maxFreq - minFreq) / deltaFreq); //int cnt = (int)Math.Ceiling((double)(maxFreq - minFreq) / deltaFreq); int cnt = (int)Math.Round((double)(maxFreq - minFreq) / deltaFreq); if (cnt < 2) { //return; cnt = 1; // 1箇所で計算 } // 設定された計算範囲を格納 CalcFreqCnt = cnt; NormalizedFreq1 = minFreq; NormalizedFreq2 = maxFreq; WaveModeDv = waveModeDv; ElemShapeDv = selectedEs.ElemShapeDv; ElemOrder = selectedEs.Order; DialogResult = DialogResult.OK; }
/// <summary> /// 導波管幅(E面解析用)の有効・無効設定 /// </summary> private void setEnableGUI() { // モード区分 FemSolver.WaveModeDV waveModeDv = WaveModeDv; foreach (RadioButton rbtn in RadioBtnModeDvs) { if (rbtn.Checked) { waveModeDv = (FemSolver.WaveModeDV)rbtn.Tag; break; } } // 導波路構造区分 if (cboxWGStructureDv.SelectedItem == null) { return; } WGStructureDVStruct wgStructureDvStruct = (WGStructureDVStruct)cboxWGStructureDv.SelectedItem; // 導波管幅(E面解析用) bool enabled; enabled = (wgStructureDvStruct.WGStructureDv == FemSolver.WGStructureDV.EPlane2D || (wgStructureDvStruct.WGStructureDv == FemSolver.WGStructureDV.HPlane2D && waveModeDv == FemSolver.WaveModeDV.TM) ); this.textBoxWaveguideWidthForEPlane.ReadOnly = !enabled; // 無効の時ReadOnlyはtrue //this.textBoxWaveguideWidthForEPlane.Visible = enabled; //this.labelWaveguideWidthForEPlane.Visible = enabled; //this.labelWaveguideWidthUnit.Visible = enabled; // TMモードは平行平板のみ対応 enabled = (wgStructureDvStruct.WGStructureDv == FemSolver.WGStructureDV.ParaPlate2D); foreach (RadioButton rbtn in RadioBtnModeDvs) { //rbtn.Enabled = enabled; rbtn.Visible = enabled; } labelWaveModeDv.Visible = enabled; }
/// <summary> /// コピー /// </summary> /// <param name="src"></param> public virtual void CP(FemElement src) { if (src == this) { return; } No = src.No; NodeNumbers = null; if (src.NodeNumbers != null) { NodeNumbers = new int[src.NodeNumbers.Length]; for (int i = 0; i < src.NodeNumbers.Length; i++) { NodeNumbers[i] = src.NodeNumbers[i]; } } MediaIndex = src.MediaIndex; LineColor = src.LineColor; BackColor = src.BackColor; // 内部使用のフィールドはコピーしない _Nodes = null; _FValues = null; _RotXFValues = null; _RotYFValues = null; _PoyntingXFValues = null; _PoyntingYFValues = null; _FactorForRot = 1.0; _EigenFValues = null; _media_Q = new double[, ] { { 1.0, 0.0, 0.0 }, { 0.0, 1.0, 0.0 }, { 0.0, 0.0, 1.0 }, }; _WaveModeDv = FemSolver.WaveModeDV.TE; IsCoarseFieldMesh = false; }
/// <summary> /// コンストラクタ /// </summary> public FemElement() { No = 0; NodeNumbers = null; MediaIndex = 0; LineColor = Color.Black; BackColor = Color.White; _Nodes = null; _FValues = null; _RotXFValues = null; _RotYFValues = null; _PoyntingXFValues = null; _PoyntingYFValues = null; _FactorForRot = 1.0; _EigenFValues = null; _media_Q = new double[, ] { { 1.0, 0.0, 0.0 }, { 0.0, 1.0, 0.0 }, { 0.0, 0.0, 1.0 }, }; _WaveModeDv = FemSolver.WaveModeDV.TE; IsCoarseFieldMesh = false; }
/* 数値積分版 * /// <summary> * /// ヘルムホルツ方程式に対する有限要素マトリクス作成 * /// </summary> * /// <param name="waveLength">波長</param> * /// <param name="toSorted">ソートされた節点インデックス( 2D節点番号→ソート済みリストインデックスのマップ)</param> * /// <param name="element">有限要素</param> * /// <param name="Nodes">節点リスト</param> * /// <param name="Medias">媒質リスト</param> * /// <param name="ForceNodeNumberH">強制境界節点ハッシュ</param> * /// <param name="WaveModeDv">計算する波のモード区分</param> * /// <param name="mat">マージされる全体行列</param> * public static void AddElementMat(double waveLength, * Dictionary<int, int> toSorted, * FemElement element, * IList<FemNode> Nodes, * MediaInfo[] Medias, * Dictionary<int, bool> ForceNodeNumberH, * FemSolver.WaveModeDv WaveModeDv, * ref MyComplexMatrix mat) * { * // 定数 * const double pi = Constants.pi; * const double c0 = Constants.c0; * // 波数 * double k0 = 2.0 * pi / waveLength; * // 角周波数 * double omega = k0 * c0; * * // 要素頂点数 * const int vertexCnt = Constants.QuadVertexCnt; //4; * // 要素内節点数 * const int nno = Constants.QuadNodeCnt_SecondOrder_Type2; //8; // 2次セレンディピティ * // 座標次元数 * const int ndim = Constants.CoordDim2D; //2; * * int[] nodeNumbers = element.NodeNumbers; * int[] no_c = new int[nno]; * MediaInfo media = Medias[element.MediaIndex]; * double[,] media_P = null; * double[,] media_Q = null; * if (WaveModeDv == FemSolver.WaveModeDv.TE) * { * media_P = media.P; * media_Q = media.Q; * } * else if (WaveModeDv == FemSolver.WaveModeDv.TM) * { * media_P = media.Q; * media_Q = media.P; * } * else * { * System.Diagnostics.Debug.Assert(false); * } * // [p]は逆数をとる * media_P = MyMatrixUtil.matrix_Inverse(media_P); * * // 節点座標(IFの都合上配列の配列形式の2次元配列を作成) * double[][] pp = new double[nno][]; * for (int ino = 0; ino < nno; ino++) * { * int nodeNumber = nodeNumbers[ino]; * int nodeIndex = nodeNumber - 1; * FemNode node = Nodes[nodeIndex]; * * no_c[ino] = nodeNumber; * pp[ino] = new double[ndim]; * for (int n = 0; n < ndim; n++) * { * pp[ino][n] = node.Coord[n]; * } * } * * //// 四角形の辺の長さを求める * //double[] le = new double[4]; * //le[0] = FemMeshLogic.GetDistance(pp[0], pp[1]); * //le[1] = FemMeshLogic.GetDistance(pp[1], pp[2]); * //le[2] = FemMeshLogic.GetDistance(pp[2], pp[3]); * //le[3] = FemMeshLogic.GetDistance(pp[3], pp[0]); * * // 要素節点座標( 局所r,s成分 ) * // s * // | * // 3+ 6 +2 * // | | | * // ---7---+---5-->r * // | | | * // 0+ 4 +1 * // | * // * double[][] n_pts = * { * // r, s * new double[] {-1.0, -1.0}, //0 * new double[] { 1.0, -1.0}, //1 * new double[] { 1.0, 1.0}, //2 * new double[] {-1.0, 1.0}, //3 * new double[] { 0, -1.0}, //4 * new double[] { 1.0, 0}, //5 * new double[] { 0, 1.0}, //6 * new double[] {-1.0, 0}, //7 * }; * * * // ガウスルジャンドルの積分公式 * double[][] g_pts = new double[5][] * { * // ポイント(ξ: [-1 +1]区間)、重み * new double[] { -0.90617985, 0.23692689}, * new double[] { -0.53846931, 0.47862867}, * new double[] {0.0, 0.56888889}, * new double[] {0.53846931, 0.47862867}, * new double[] {0.90617985, 0.23692689} * }; * * // 要素剛性行列を作る * double[,] emat = new Complex[nno, nno]; * for (int ino = 0; ino < nno; ino++) * { * for (int jno = 0; jno < nno; jno++) * { * emat[ino, jno] = 0.0; * double detjsum = 0; //check * foreach (double[] s_g_pt in g_pts) * { * foreach (double[] r_g_pt in g_pts) * { * // 積分点 * double r = r_g_pt[0]; * double s = s_g_pt[0]; * // 重み(2次元) * double weight = r_g_pt[1] * s_g_pt[1]; * // 形状関数 * double[] N = new double[nno]; * // 形状関数のr, s方向微分 * double[] dNdr = new double[nno]; * double[] dNds = new double[nno]; * // 節点0~3 : 四角形の頂点 * for (int i = 0; i < 4; i++) * { * // 節点の局所座標 * double ri = n_pts[i][0]; * double si = n_pts[i][1]; * // 形状関数N * N[i] = 0.25 * (1.0 + ri * r) * (1.0 + si * s) * (ri* r + si * s - 1.0); * // 形状関数のr方向微分 * dNdr[i] = 0.25 * ri * (1.0 + si * s) * (2.0 * ri * r + si * s); * // 形状関数のs方向微分 * dNds[i] = 0.25 * si * (1.0 + ri * r) * (ri * r + 2.0 * si * s); * } * // 節点4,6 : r方向辺上中点 * foreach (int i in new int[]{ 4, 6}) * { * // 節点の局所座標 * double ri = n_pts[i][0]; * double si = n_pts[i][1]; * // 形状関数N * N[i] = 0.5 * (1.0 - r * r) * (1.0 + si * s); * // 形状関数のr方向微分 * dNdr[i] = -1.0 * r * (1.0 + si * s); * // 形状関数のs方向微分 * dNds[i] = 0.5 * si * (1.0 - r * r); * } * // 節点5,7 : s方向辺上中点 * foreach (int i in new int[] { 5, 7 }) * { * // 節点の局所座標 * double ri = n_pts[i][0]; * double si = n_pts[i][1]; * // 形状関数N * N[i] = 0.5 * (1.0 + ri * r) * (1.0 - s * s); * // 形状関数のr方向微分 * dNdr[i] = 0.5 * ri * (1.0 - s * s); * // 形状関数のs方向微分 * dNds[i] = -1.0 * s * (1.0 + ri * r); * } * * // ヤコビアン行列 * double j11; * double j12; * double j21; * double j22; * j11 = 0; * j12 = 0; * j21 = 0; * j22 = 0; * * //for (int i = 0; i < vertexCnt; i++) * //{ * // // 頂点の座標の微分 * // // 座標の形状関数は一次四角形のものを使用する * // // 節点の局所座標 * // double ri = n_pts[i][0]; * // double si = n_pts[i][1]; * // double dNdr_1stOrder = 0.25 * ri * (1.0 + si * s); * // double dNds_1stOrder = 0.25 * (1.0 + ri * r) * si; * // j11 += dNdr_1stOrder * pp[i][0]; * // j12 += dNdr_1stOrder * pp[i][1]; * // j21 += dNds_1stOrder * pp[i][0]; * // j22 += dNds_1stOrder * pp[i][1]; * //} * * for (int i = 0; i < nno; i++) * { * j11 += dNdr[i] * pp[i][0]; * j12 += dNdr[i] * pp[i][1]; * j21 += dNds[i] * pp[i][0]; * j22 += dNds[i] * pp[i][1]; * } * // ヤコビアン * double detj = j11 * j22 - j12 * j21; * detjsum += detj * weight; * //System.Diagnostics.Debug.WriteLine("det:{0}", detj); * * // gradr[0] : gradrのx成分 grad[1] : gradrのy成分 * // grads[0] : gradsのx成分 grads[1] : gradsのy成分 * double[] gradr = new double[2]; * double[] grads = new double[2]; * gradr[0] = j22 / detj; * gradr[1] = - j21 / detj; * grads[0] = - j12 / detj; * grads[1] = j11 / detj; * * // 形状関数のx, y方向微分 * double[,] dNdX = new double[ndim, nno]; * for (int i = 0; i < nno; i++) * { * for (int direction = 0; direction < ndim; direction++) * { * dNdX[direction, i] = dNdr[i] * gradr[direction] + dNds[i] * grads[direction]; * } * } * * // 汎関数 * double functional = media_P[0, 0] * dNdX[1, ino] * dNdX[1, jno] + media_P[1, 1] * dNdX[0, ino] * dNdX[0, jno] * - k0 * k0 * media_Q[2, 2] * N[ino] * N[jno]; * emat[ino, jno] += detj * weight * functional; * } * } * //System.Diagnostics.Debug.WriteLine("detsum: {0}", detjsum); * } * } * * // 要素剛性行列にマージする * for (int ino = 0; ino < nno; ino++) * { * int iNodeNumber = no_c[ino]; * if (ForceNodeNumberH.ContainsKey(iNodeNumber)) continue; * int inoGlobal = toSorted[iNodeNumber]; * for (int jno = 0; jno < nno; jno++) * { * int jNodeNumber = no_c[jno]; * if (ForceNodeNumberH.ContainsKey(jNodeNumber)) continue; * int jnoGlobal = toSorted[jNodeNumber]; * * mat[inoGlobal, jnoGlobal] += emat[ino, jno]; * } * } * } */ /// <summary> /// ヘルムホルツ方程式に対する有限要素マトリクス作成 /// </summary> /// <param name="waveLength">波長</param> /// <param name="toSorted">ソートされた節点インデックス( 2D節点番号→ソート済みリストインデックスのマップ)</param> /// <param name="element">有限要素</param> /// <param name="Nodes">節点リスト</param> /// <param name="Medias">媒質リスト</param> /// <param name="ForceNodeNumberH">強制境界節点ハッシュ</param> /// <param name="WGStructureDv">導波路構造区分</param> /// <param name="WaveModeDv">計算する波のモード区分</param> /// <param name="waveguideWidthForEPlane">導波路幅(E面解析用)</param> /// <param name="mat">マージされる全体行列(clapack使用時)</param> /// <param name="mat_cc">マージされる全体行列(DelFEM使用時)</param> /// <param name="res_c">マージされる残差ベクトル(DelFEM使用時)</param> /// <param name="tmpBuffer">一時バッファ(DelFEM使用時)</param> public static void AddElementMat(double waveLength, Dictionary <int, int> toSorted, FemElement element, IList <FemNode> Nodes, MediaInfo[] Medias, Dictionary <int, bool> ForceNodeNumberH, FemSolver.WGStructureDV WGStructureDv, FemSolver.WaveModeDV WaveModeDv, double waveguideWidthForEPlane, ref MyComplexMatrix mat, ref DelFEM4NetMatVec.CZMatDia_BlkCrs_Ptr mat_cc, ref DelFEM4NetMatVec.CZVector_Blk_Ptr res_c, ref int[] tmpBuffer) { // 定数 const double pi = Constants.pi; const double c0 = Constants.c0; // 波数 double k0 = 2.0 * pi / waveLength; // 角周波数 double omega = k0 * c0; // 要素頂点数 //const int vertexCnt = Constants.QuadVertexCnt; //4; // 要素内節点数 const int nno = Constants.QuadNodeCnt_SecondOrder_Type2; //8; // 2次セレンディピティ // 座標次元数 const int ndim = Constants.CoordDim2D; //2; int[] nodeNumbers = element.NodeNumbers; int[] no_c = new int[nno]; MediaInfo media = Medias[element.MediaIndex]; double[,] media_P = null; double[,] media_Q = null; // ヘルムホルツ方程式のパラメータP,Qを取得する FemSolver.GetHelmholtzMediaPQ( k0, media, WGStructureDv, WaveModeDv, waveguideWidthForEPlane, out media_P, out media_Q); // 節点座標(IFの都合上配列の配列形式の2次元配列を作成) double[][] pp = new double[nno][]; for (int ino = 0; ino < nno; ino++) { int nodeNumber = nodeNumbers[ino]; int nodeIndex = nodeNumber - 1; FemNode node = Nodes[nodeIndex]; no_c[ino] = nodeNumber; pp[ino] = new double[ndim]; for (int n = 0; n < ndim; n++) { pp[ino][n] = node.Coord[n]; } } // 四角形の辺の長さを求める double[] le = new double[4]; le[0] = FemMeshLogic.GetDistance(pp[0], pp[1]); le[1] = FemMeshLogic.GetDistance(pp[1], pp[2]); le[2] = FemMeshLogic.GetDistance(pp[2], pp[3]); le[3] = FemMeshLogic.GetDistance(pp[3], pp[0]); System.Diagnostics.Debug.Assert(Math.Abs(le[0] - le[2]) < Constants.PrecisionLowerLimit); System.Diagnostics.Debug.Assert(Math.Abs(le[1] - le[3]) < Constants.PrecisionLowerLimit); double lx = le[0]; double ly = le[1]; // 要素節点座標( 局所r,s成分 ) // s // | // 3+ 6 +2 // | | | // ---7---+---5-->r // | | | // 0+ 4 +1 // | // double[][] n_pts = { // r, s new double[] { -1.0, -1.0 }, //0 new double[] { 1.0, -1.0 }, //1 new double[] { 1.0, 1.0 }, //2 new double[] { -1.0, 1.0 }, //3 new double[] { 0, -1.0 }, //4 new double[] { 1.0, 0 }, //5 new double[] { 0, 1.0 }, //6 new double[] { -1.0, 0 }, //7 }; // Ni = a0(r^2*s) + a1(r^2) + a2(r) + a3(rs) + a4(rs^2) + a5(s^2) + a6(s) + a7 double[,] Ni_a = new double[nno, 8]; for (int i = 0; i < 4; i++) { // 節点の局所座標 double ri = n_pts[i][0]; double si = n_pts[i][1]; Ni_a[i, 0] = 0.25 * ri * ri * si; Ni_a[i, 1] = 0.25 * ri * ri; Ni_a[i, 2] = 0.0; Ni_a[i, 3] = 0.25 * ri * si; Ni_a[i, 4] = 0.25 * ri * si * si; Ni_a[i, 5] = 0.25 * si * si; Ni_a[i, 6] = 0.0; Ni_a[i, 7] = -0.25; } foreach (int i in new int[] { 4, 6 }) { // 節点の局所座標 double ri = n_pts[i][0]; double si = n_pts[i][1]; Ni_a[i, 0] = -0.5 * si; Ni_a[i, 1] = -0.5; Ni_a[i, 2] = 0.0; Ni_a[i, 3] = 0.0; Ni_a[i, 4] = 0.0; Ni_a[i, 5] = 0.0; Ni_a[i, 6] = 0.5 * si; Ni_a[i, 7] = 0.5; } foreach (int i in new int[] { 5, 7 }) { // 節点の局所座標 double ri = n_pts[i][0]; double si = n_pts[i][1]; Ni_a[i, 0] = 0.0; Ni_a[i, 1] = 0.0; Ni_a[i, 2] = 0.5 * ri; Ni_a[i, 3] = 0.0; Ni_a[i, 4] = -0.5 * ri; Ni_a[i, 5] = -0.5; Ni_a[i, 6] = 0.0; Ni_a[i, 7] = 0.5; } // dNidr = a0(r^2*s) + a1(r^2) + a2(r) + a3(rs) + a4(rs^2) + a5(s^2) + a6(s) + a7 double[,] dNidr_a = new double[nno, 8]; for (int i = 0; i < 4; i++) { // 節点の局所座標 double ri = n_pts[i][0]; double si = n_pts[i][1]; dNidr_a[i, 0] = 0.0; dNidr_a[i, 1] = 0.0; // r^2 dNidr_a[i, 2] = 0.25 * 2.0 * ri * ri; // r dNidr_a[i, 3] = 0.25 * 2.0 * ri * ri * si; // rs dNidr_a[i, 4] = 0.0; dNidr_a[i, 5] = 0.25 * ri * si * si; // s^2 dNidr_a[i, 6] = 0.25 * ri * si; // s dNidr_a[i, 7] = 0.0; //1 } foreach (int i in new int[] { 4, 6 }) { // 節点の局所座標 double ri = n_pts[i][0]; double si = n_pts[i][1]; dNidr_a[i, 0] = 0.0; dNidr_a[i, 1] = 0.0; // r^2 dNidr_a[i, 2] = -1.0; // r dNidr_a[i, 3] = -si; // rs dNidr_a[i, 4] = 0.0; dNidr_a[i, 5] = 0.0; // s^2 dNidr_a[i, 6] = 0.0; // s dNidr_a[i, 7] = 0.0; // 1 } foreach (int i in new int[] { 5, 7 }) { // 節点の局所座標 double ri = n_pts[i][0]; double si = n_pts[i][1]; dNidr_a[i, 0] = 0.0; dNidr_a[i, 1] = 0.0; // r^2 dNidr_a[i, 2] = 0.0; // r dNidr_a[i, 3] = 0.0; // rs dNidr_a[i, 4] = 0.0; dNidr_a[i, 5] = -0.5 * ri; // s^2 dNidr_a[i, 6] = 0.0; // s dNidr_a[i, 7] = 0.5 * ri; // 1 } // dNids = a0(r^2*s) + a1(r^2) + a2(r) + a3(rs) + a4(rs^2) + a5(s^2) + a6(s) + a7 double[,] dNids_a = new double[nno, 8]; for (int i = 0; i < 4; i++) { // 節点の局所座標 double ri = n_pts[i][0]; double si = n_pts[i][1]; dNids_a[i, 0] = 0.0; dNids_a[i, 1] = 0.25 * ri * ri * si; // r^2 dNids_a[i, 2] = 0.25 * ri * si; // r dNids_a[i, 3] = 0.25 * 2.0 * ri * si * si; // rs dNids_a[i, 4] = 0.0; dNids_a[i, 5] = 0.0; // s^2 dNids_a[i, 6] = 0.25 * 2.0 * si * si; // s dNids_a[i, 7] = 0.0; //1 } foreach (int i in new int[] { 4, 6 }) { // 節点の局所座標 double ri = n_pts[i][0]; double si = n_pts[i][1]; dNids_a[i, 0] = 0.0; dNids_a[i, 1] = -0.5 * si; // r^2 dNids_a[i, 2] = 0.0; // r dNids_a[i, 3] = 0.0; // rs dNids_a[i, 4] = 0.0; dNids_a[i, 5] = 0.0; // s^2 dNids_a[i, 6] = 0.0; // s dNids_a[i, 7] = 0.5 * si; //1 } foreach (int i in new int[] { 5, 7 }) { // 節点の局所座標 double ri = n_pts[i][0]; double si = n_pts[i][1]; dNids_a[i, 0] = 0.0; dNids_a[i, 1] = 0.0; // r^2 dNids_a[i, 2] = 0.0; // r dNids_a[i, 3] = -ri; // rs dNids_a[i, 4] = 0.0; dNids_a[i, 5] = 0.0; // s^2 dNids_a[i, 6] = -1.0; // s dNids_a[i, 7] = 0.0; //1 } // ∫dN/dndN/dn dxdy // integralDNDX[n, ino, jno] n = 0 --> ∫dN/dxdN/dx dxdy // n = 1 --> ∫dN/dydN/dy dxdy double[, ,] integralDNDX = new double[ndim, nno, nno]; // ∫N N dxdy double[,] integralN = new double[nno, nno]; for (int ino = 0; ino < nno; ino++) { for (int jno = 0; jno < nno; jno++) { integralN[ino, jno] = lx * ly / 4.0 * ( // r^4s^2 4.0 / 15.0 * Ni_a[ino, 0] * Ni_a[jno, 0] // r^2s^2 + 4.0 / 9.0 * (Ni_a[ino, 6] * Ni_a[jno, 0] + Ni_a[ino, 5] * Ni_a[jno, 1] + Ni_a[ino, 4] * Ni_a[jno, 2] + Ni_a[ino, 3] * Ni_a[jno, 3] + Ni_a[ino, 2] * Ni_a[jno, 4] + Ni_a[ino, 1] * Ni_a[jno, 5] + Ni_a[ino, 0] * Ni_a[jno, 6]) // r^4 + 4.0 / 5.0 * Ni_a[ino, 1] * Ni_a[jno, 1] // r^2 + 4.0 / 3.0 * (Ni_a[ino, 7] * Ni_a[jno, 1] + Ni_a[ino, 2] * Ni_a[jno, 2] + Ni_a[ino, 1] * Ni_a[jno, 7]) // r^2s^4 + 4.0 / 15.0 * Ni_a[ino, 4] * Ni_a[jno, 4] // s^4 + 4.0 / 5.0 * Ni_a[ino, 5] * Ni_a[jno, 5] // s^2 + 4.0 / 3.0 * (Ni_a[ino, 7] * Ni_a[jno, 5] + Ni_a[ino, 6] * Ni_a[jno, 6] + Ni_a[ino, 5] * Ni_a[jno, 7]) // 1 + 4.0 * Ni_a[ino, 7] * Ni_a[jno, 7] ); integralDNDX[0, ino, jno] = ly / lx * ( // r^4s^2 4.0 / 15.0 * dNidr_a[ino, 0] * dNidr_a[jno, 0] // r^2s^2 + 4.0 / 9.0 * (dNidr_a[ino, 6] * dNidr_a[jno, 0] + dNidr_a[ino, 5] * dNidr_a[jno, 1] + dNidr_a[ino, 4] * dNidr_a[jno, 2] + dNidr_a[ino, 3] * dNidr_a[jno, 3] + dNidr_a[ino, 2] * dNidr_a[jno, 4] + dNidr_a[ino, 1] * dNidr_a[jno, 5] + dNidr_a[ino, 0] * dNidr_a[jno, 6]) // r^4 + 4.0 / 5.0 * dNidr_a[ino, 1] * dNidr_a[jno, 1] // r^2 + 4.0 / 3.0 * (dNidr_a[ino, 7] * dNidr_a[jno, 1] + dNidr_a[ino, 2] * dNidr_a[jno, 2] + dNidr_a[ino, 1] * dNidr_a[jno, 7]) // r^2s^4 + 4.0 / 15.0 * dNidr_a[ino, 4] * dNidr_a[jno, 4] // s^4 + 4.0 / 5.0 * dNidr_a[ino, 5] * dNidr_a[jno, 5] // s^2 + 4.0 / 3.0 * (dNidr_a[ino, 7] * dNidr_a[jno, 5] + dNidr_a[ino, 6] * dNidr_a[jno, 6] + dNidr_a[ino, 5] * dNidr_a[jno, 7]) // 1 + 4.0 * dNidr_a[ino, 7] * dNidr_a[jno, 7] ); integralDNDX[1, ino, jno] = lx / ly * ( // r^4s^2 4.0 / 15.0 * dNids_a[ino, 0] * dNids_a[jno, 0] // r^2s^2 + 4.0 / 9.0 * (dNids_a[ino, 6] * dNids_a[jno, 0] + dNids_a[ino, 5] * dNids_a[jno, 1] + dNids_a[ino, 4] * dNids_a[jno, 2] + dNids_a[ino, 3] * dNids_a[jno, 3] + dNids_a[ino, 2] * dNids_a[jno, 4] + dNids_a[ino, 1] * dNids_a[jno, 5] + dNids_a[ino, 0] * dNids_a[jno, 6]) // r^4 + 4.0 / 5.0 * dNids_a[ino, 1] * dNids_a[jno, 1] // r^2 + 4.0 / 3.0 * (dNids_a[ino, 7] * dNids_a[jno, 1] + dNids_a[ino, 2] * dNids_a[jno, 2] + dNids_a[ino, 1] * dNids_a[jno, 7]) // r^2s^4 + 4.0 / 15.0 * dNids_a[ino, 4] * dNids_a[jno, 4] // s^4 + 4.0 / 5.0 * dNids_a[ino, 5] * dNids_a[jno, 5] // s^2 + 4.0 / 3.0 * (dNids_a[ino, 7] * dNids_a[jno, 5] + dNids_a[ino, 6] * dNids_a[jno, 6] + dNids_a[ino, 5] * dNids_a[jno, 7]) // 1 + 4.0 * dNids_a[ino, 7] * dNids_a[jno, 7] ); } } // 要素剛性行列を作る double[,] emat = new double[nno, nno]; for (int ino = 0; ino < nno; ino++) { for (int jno = 0; jno < nno; jno++) { emat[ino, jno] = media_P[0, 0] * integralDNDX[1, ino, jno] + media_P[1, 1] * integralDNDX[0, ino, jno] - k0 * k0 * media_Q[2, 2] * integralN[ino, jno]; } } // 要素剛性行列にマージする if (mat_cc != null) { // 全体節点番号→要素内節点インデックスマップ Dictionary <uint, int> inoGlobalDic = new Dictionary <uint, int>(); for (int ino = 0; ino < nno; ino++) { int iNodeNumber = no_c[ino]; if (ForceNodeNumberH.ContainsKey(iNodeNumber)) { continue; } uint inoGlobal = (uint)toSorted[iNodeNumber]; inoGlobalDic.Add(inoGlobal, ino); } // マージ用の節点番号リスト uint[] no_c_tmp = inoGlobalDic.Keys.ToArray <uint>(); // マージする節点数("col"と"row"のサイズ) uint ncolrow_tmp = (uint)no_c_tmp.Length; // Note: // 要素の節点がすべて強制境界の場合がある.その場合は、ncolrow_tmpが0 if (ncolrow_tmp > 0) { // マージする要素行列 DelFEM4NetCom.Complex[] ematBuffer = new DelFEM4NetCom.Complex[ncolrow_tmp * ncolrow_tmp]; for (int ino_tmp = 0; ino_tmp < ncolrow_tmp; ino_tmp++) { int ino = inoGlobalDic[no_c_tmp[ino_tmp]]; for (int jno_tmp = 0; jno_tmp < ncolrow_tmp; jno_tmp++) { int jno = inoGlobalDic[no_c_tmp[jno_tmp]]; double value = emat[ino, jno]; DelFEM4NetCom.Complex cvalueDelFEM = new DelFEM4NetCom.Complex(value, 0); // ematBuffer[ino_tmp, jno_tmp] 横ベクトルを先に埋める(clapack方式でないことに注意) ematBuffer[ino_tmp * ncolrow_tmp + jno_tmp] = cvalueDelFEM; } } // 全体行列に要素行列をマージする mat_cc.Mearge(ncolrow_tmp, no_c_tmp, ncolrow_tmp, no_c_tmp, 1, ematBuffer, ref tmpBuffer); } } else if (mat != null) { for (int ino = 0; ino < nno; ino++) { int iNodeNumber = no_c[ino]; if (ForceNodeNumberH.ContainsKey(iNodeNumber)) { continue; } int inoGlobal = toSorted[iNodeNumber]; for (int jno = 0; jno < nno; jno++) { int jNodeNumber = no_c[jno]; if (ForceNodeNumberH.ContainsKey(jNodeNumber)) { continue; } int jnoGlobal = toSorted[jNodeNumber]; //mat[inoGlobal, jnoGlobal] += emat[ino, jno]; //mat._body[inoGlobal + jnoGlobal * mat.RowSize] += emat[ino, jno]; // 実数部に加算する //mat._body[inoGlobal + jnoGlobal * mat.RowSize].Real += emat[ino, jno]; // バンドマトリクス対応 mat._body[mat.GetBufferIndex(inoGlobal, jnoGlobal)].Real += emat[ino, jno]; } } } }
/// <summary> /// Fem入力データをファイルから読み込み /// </summary> /// <param name="filename">ファイル名(*.fem)</param> /// <param name="nodes">節点リスト</param> /// <param name="elements">要素リスト</param> /// <param name="ports">ポートの節点番号リストのリスト</param> /// <param name="forceBCNodes">強制境界節点番号リスト</param> /// <param name="incidentPortNo">入射ポート番号</param> /// <param name="medias">媒質情報リスト</param> /// <param name="firstWaveLength">計算開始波長</param> /// <param name="lastWaveLength">計算終了波長</param> /// <param name="calcCnt">計算件数</param> /// <param name="wgStructureDv">導波路構造区分</param> /// <param name="waveModeDv">波のモード区分</param> /// <param name="lsEqnSoverDv">線形方程式解法区分</param> /// <param name="waveguideWidthForEPlane">導波管幅(E面解析用)</param> /// <returns></returns> public static bool LoadFromFile( string filename, out IList <FemNode> nodes, out IList <FemElement> elements, out IList <IList <int> > ports, out IList <int> forceBCNodes, out int incidentPortNo, out MediaInfo[] medias, out double firstWaveLength, out double lastWaveLength, out int calcCnt, out FemSolver.WGStructureDV wgStructureDv, out FemSolver.WaveModeDV waveModeDv, out FemSolver.LinearSystemEqnSoverDV lsEqnSoverDv, out double waveguideWidthForEPlane ) { int eNodeCnt = 0; nodes = new List <FemNode>(); elements = new List <FemElement>(); ports = new List <IList <int> >(); forceBCNodes = new List <int>(); incidentPortNo = 1; medias = new MediaInfo[Constants.MaxMediaCount]; for (int i = 0; i < medias.Length; i++) { MediaInfo media = new MediaInfo(); media.BackColor = CadLogic.MediaBackColors[i]; medias[i] = media; } firstWaveLength = 0.0; lastWaveLength = 0.0; calcCnt = 0; wgStructureDv = Constants.DefWGStructureDv; waveModeDv = Constants.DefWaveModeDv; lsEqnSoverDv = Constants.DefLsEqnSolverDv; waveguideWidthForEPlane = 0; if (!File.Exists(filename)) { return(false); } // 入力データ読み込み try { using (StreamReader sr = new StreamReader(filename)) { const char delimiter = ','; string line; string[] tokens; line = sr.ReadLine(); tokens = line.Split(delimiter); if (tokens.Length != 2 || tokens[0] != "Nodes") { MessageBox.Show("節点情報がありません", "", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } int nodeCnt = int.Parse(tokens[1]); for (int i = 0; i < nodeCnt; i++) { line = sr.ReadLine(); tokens = line.Split(delimiter); if (tokens.Length != 3) { MessageBox.Show("節点情報が不正です", "", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } int no = int.Parse(tokens[0]); if (no != i + 1) { MessageBox.Show("節点番号が不正です", "", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } FemNode femNode = new FemNode(); femNode.No = no; femNode.Coord = new double[2]; femNode.Coord[0] = double.Parse(tokens[1]); femNode.Coord[1] = double.Parse(tokens[2]); nodes.Add(femNode); } line = sr.ReadLine(); tokens = line.Split(delimiter); if (tokens.Length != 2 || tokens[0] != "Elements") { MessageBox.Show("要素情報がありません", "", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } int elementCnt = int.Parse(tokens[1]); for (int i = 0; i < elementCnt; i++) { line = sr.ReadLine(); tokens = line.Split(delimiter); if ((tokens.Length != 1 + Constants.TriNodeCnt_SecondOrder) && (tokens.Length != 2 + Constants.TriNodeCnt_SecondOrder) && // ver1.1.0.0で媒質インデックスを番号の後に挿入 (tokens.Length != 2 + Constants.QuadNodeCnt_SecondOrder_Type2) && (tokens.Length != 2 + Constants.TriNodeCnt_FirstOrder) && (tokens.Length != 2 + Constants.QuadNodeCnt_FirstOrder) ) { MessageBox.Show("要素情報が不正です", "", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } int elemNo = int.Parse(tokens[0]); int mediaIndex = 0; int indexOffset = 1; // ver1.0.0.0 int workENodeCnt = Constants.TriNodeCnt_SecondOrder; if (tokens.Length == 1 + Constants.TriNodeCnt_SecondOrder) { // 媒質インデックスのない古い形式(ver1.0.0.0) } else { // ver1.1.0.0で媒質インデックスを追加 mediaIndex = int.Parse(tokens[1]); indexOffset = 2; workENodeCnt = tokens.Length - 2; } if (workENodeCnt <= 0) { MessageBox.Show("要素節点数が不正です", "", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } if (eNodeCnt == 0) { // 最初の要素の節点数を格納(チェックに利用) eNodeCnt = workENodeCnt; } else { // 要素の節点数が変わった? if (workENodeCnt != eNodeCnt) { MessageBox.Show("要素節点数が不正です", "", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } } //FemElement femElement = new FemElement(); FemElement femElement = FemMeshLogic.CreateFemElementByElementNodeCnt(eNodeCnt); femElement.No = elemNo; femElement.MediaIndex = mediaIndex; femElement.NodeNumbers = new int[eNodeCnt]; for (int n = 0; n < femElement.NodeNumbers.Length; n++) { femElement.NodeNumbers[n] = int.Parse(tokens[n + indexOffset]); } elements.Add(femElement); } line = sr.ReadLine(); tokens = line.Split(delimiter); if (tokens.Length != 2 || tokens[0] != "Ports") { MessageBox.Show("入出力ポート情報がありません", "", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } int portCnt = int.Parse(tokens[1]); for (int i = 0; i < portCnt; i++) { line = sr.ReadLine(); tokens = line.Split(delimiter); if (tokens.Length != 2) { MessageBox.Show("入出力ポート情報が不正です", "", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } int portNo = int.Parse(tokens[0]); int portNodeCnt = int.Parse(tokens[1]); if (portNo != i + 1) { MessageBox.Show("ポート番号が不正です", "", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } IList <int> portNodes = new List <int>(); for (int n = 0; n < portNodeCnt; n++) { line = sr.ReadLine(); tokens = line.Split(delimiter); if (tokens.Length != 2) { MessageBox.Show("ポートの節点情報が不正です", "", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } int portNodeNumber = int.Parse(tokens[0]); int nodeNumber = int.Parse(tokens[1]); if (portNodeNumber != n + 1) { MessageBox.Show("ポートの節点番号が不正です", "", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } portNodes.Add(nodeNumber); } ports.Add(portNodes); } line = sr.ReadLine(); tokens = line.Split(delimiter); if (tokens.Length != 2 || tokens[0] != "Force") { MessageBox.Show("強制境界情報がありません", "", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } int forceNodeCnt = int.Parse(tokens[1]); for (int i = 0; i < forceNodeCnt; i++) { line = sr.ReadLine(); int nodeNumber = int.Parse(line); forceBCNodes.Add(nodeNumber); } line = sr.ReadLine(); tokens = line.Split(delimiter); if (tokens.Length != 2 || tokens[0] != "IncidentPortNo") { MessageBox.Show("入射ポート番号がありません", "", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } incidentPortNo = int.Parse(tokens[1]); ////////////////////////////////////////// //// Ver1.1.0.0からの追加情報 ////////////////////////////////////////// line = sr.ReadLine(); if (line == null || line.Length == 0) { // 媒質情報なし // ver1.0.0.0 } else { // 媒質情報? // ver1.1.0.0 tokens = line.Split(delimiter); if (tokens[0] != "Medias") { MessageBox.Show("媒質情報がありません"); return(false); } int cnt = int.Parse(tokens[1]); if (cnt > Constants.MaxMediaCount) { MessageBox.Show("媒質情報の個数が不正です"); return(false); } for (int i = 0; i < cnt; i++) { line = sr.ReadLine(); if (line.Length == 0) { MessageBox.Show("媒質情報が不正です"); return(false); } tokens = line.Split(delimiter); if (tokens.Length != 1 + 9 + 9) { MessageBox.Show("媒質情報が不正です"); return(false); } int mediaIndex = int.Parse(tokens[0]); System.Diagnostics.Debug.Assert(mediaIndex == i); double[,] p = new double[3, 3]; for (int m = 0; m < p.GetLength(0); m++) { for (int n = 0; n < p.GetLength(1); n++) { p[m, n] = double.Parse(tokens[1 + m * p.GetLength(1) + n]); } } medias[i].SetP(p); double[,] q = new double[3, 3]; for (int m = 0; m < q.GetLength(0); m++) { for (int n = 0; n < q.GetLength(1); n++) { q[m, n] = double.Parse(tokens[1 + 9 + m * q.GetLength(1) + n]); } } medias[i].SetQ(q); } } line = sr.ReadLine(); if (line == null || line.Length == 0) { } else { tokens = line.Split(delimiter); if (tokens.Length != 4 || tokens[0] != "WaveLengthRange") { MessageBox.Show("計算対象周波数情報がありません"); return(false); } firstWaveLength = double.Parse(tokens[1]); lastWaveLength = double.Parse(tokens[2]); calcCnt = int.Parse(tokens[3]); } line = sr.ReadLine(); if (line == null || line.Length == 0) { } else { tokens = line.Split(delimiter); if (tokens.Length != 2 || tokens[0] != "LsEqnSolverDv") { MessageBox.Show("線形方程式解法区分情報がありません"); return(false); } string value = tokens[1]; lsEqnSoverDv = FemSolver.StrToLinearSystemEqnSolverDV(value); } line = sr.ReadLine(); if (line == null || line.Length == 0) { } else { tokens = line.Split(delimiter); if (tokens.Length != 2 || tokens[0] != "WaveModeDv") { MessageBox.Show("計算対象モード区分情報がありません"); return(false); } if (tokens[1] == "TE") { waveModeDv = FemSolver.WaveModeDV.TE; } else if (tokens[1] == "TM") { waveModeDv = FemSolver.WaveModeDV.TM; } else { MessageBox.Show("計算対象モード区分情報が不正です"); return(false); } } line = sr.ReadLine(); if (line == null || line.Length == 0) { } else { tokens = line.Split(delimiter); if (tokens.Length != 2 || tokens[0] != "WGStructureDv") { MessageBox.Show("計算対象導波路構造区分情報がありません"); return(false); } wgStructureDv = FemSolver.StrToWGStructureDV(tokens[1]); } line = sr.ReadLine(); if (line == null || line.Length == 0) { } else { tokens = line.Split(delimiter); if (tokens.Length != 2 || tokens[0] != "WaveguideWidthForEPlane") { MessageBox.Show("E面解析用導波路幅がありません"); return(false); } waveguideWidthForEPlane = double.Parse(tokens[1]); } } } catch (Exception exception) { System.Diagnostics.Debug.WriteLine(exception.Message + " " + exception.StackTrace); MessageBox.Show(exception.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } return(true); }
/// <summary> /// 1Dヘルムホルツ方程式固有値問題の要素行列を加算する /// </summary> /// <param name="waveLength">波長(E面の場合のみ使用する)</param> /// <param name="element">線要素</param> /// <param name="coords">座標リスト</param> /// <param name="toSorted">節点番号→ソート済み節点インデックスマップ</param> /// <param name="Medias">媒質情報リスト</param> /// <param name="WaveModeDv">計算する波のモード区分</param> /// <param name="txx_1d">txx行列</param> /// <param name="ryy_1d">ryy行列</param> /// <param name="uzz_1d">uzz行列</param> public static void AddElementMatOf1dEigenValueProblem( double waveLength, FemLineElement element, IList <double> coords, Dictionary <int, int> toSorted, MediaInfo[] Medias, FemSolver.WGStructureDV WGStructureDv, FemSolver.WaveModeDV WaveModeDv, double waveguideWidthForEPlane, ref MyDoubleMatrix txx_1d, ref MyDoubleMatrix ryy_1d, ref MyDoubleMatrix uzz_1d) { // 定数 const double pi = Constants.pi; const double c0 = Constants.c0; // 波数 double k0 = 2.0 * pi / waveLength; // 角周波数 double omega = k0 * c0; // 1次線要素 const int nno = Constants.LineNodeCnt_FirstOrder; // 2; int[] nodeNumbers = element.NodeNumbers; System.Diagnostics.Debug.Assert(nno == nodeNumbers.Length); // 座標の取得 double[] elementCoords = new double[nno]; for (int n = 0; n < nno; n++) { int nodeIndex = nodeNumbers[n] - 1; elementCoords[n] = coords[nodeIndex]; } // 線要素の長さ double elen = Math.Abs(elementCoords[1] - elementCoords[0]); // 媒質インデックス int mediaIndex = element.MediaIndex; // 媒質 MediaInfo media = Medias[mediaIndex]; double[,] media_P = null; double[,] media_Q = null; // ヘルムホルツ方程式のパラメータP,Qを取得する FemSolver.GetHelmholtzMediaPQ( k0, media, WGStructureDv, WaveModeDv, waveguideWidthForEPlane, out media_P, out media_Q); double[,] integralN = new double[nno, nno] { { elen / 3.0, elen / 6.0 }, { elen / 6.0, elen / 3.0 }, }; double[,] integralDNDY = new double[nno, nno] { { 1.0 / elen, -1.0 / elen }, { -1.0 / elen, 1.0 / elen }, }; for (int ino = 0; ino < nno; ino++) { int inoBoundary = nodeNumbers[ino]; int inoSorted; if (!toSorted.ContainsKey(inoBoundary)) { continue; } inoSorted = toSorted[inoBoundary]; for (int jno = 0; jno < nno; jno++) { int jnoBoundary = nodeNumbers[jno]; int jnoSorted; if (!toSorted.ContainsKey(jnoBoundary)) { continue; } jnoSorted = toSorted[jnoBoundary]; // 対称バンド行列対応 if (ryy_1d is MyDoubleSymmetricBandMatrix && jnoSorted < inoSorted) { continue; } double e_txx_1d_inojno = media_P[0, 0] * integralDNDY[ino, jno]; double e_ryy_1d_inojno = media_P[1, 1] * integralN[ino, jno]; double e_uzz_1d_inojno = media_Q[2, 2] * integralN[ino, jno]; //txx_1d[inoSorted, jnoSorted] += e_txx_1d_inojno; //ryy_1d[inoSorted, jnoSorted] += e_ryy_1d_inojno; //uzz_1d[inoSorted, jnoSorted] += e_uzz_1d_inojno; txx_1d._body[txx_1d.GetBufferIndex(inoSorted, jnoSorted)] += e_txx_1d_inojno; ryy_1d._body[ryy_1d.GetBufferIndex(inoSorted, jnoSorted)] += e_ryy_1d_inojno; uzz_1d._body[uzz_1d.GetBufferIndex(inoSorted, jnoSorted)] += e_uzz_1d_inojno; } } }
/// <summary> /// フィールド値をセットする /// </summary> /// <param name="valuesAll"></param> /// <param name="nodesRegionToIndex"></param> /// <param name="factorForRot">回転に掛ける因子(磁界または電界への変換)</param> public virtual void SetFieldValueFromAllValues(Complex[] valuesAll, Dictionary <int, int> nodesRegionToIndex, Complex factorForRot, double[,] media_Q, FemSolver.WaveModeDV waveModeDv) { _FValues = new Complex[NodeNumbers.Length]; for (int ino = 0; ino < NodeNumbers.Length; ino++) { int nodeNumber = NodeNumbers[ino]; if (nodesRegionToIndex.ContainsKey(nodeNumber)) { int nodeIndex = nodesRegionToIndex[nodeNumber]; //_FValues[ino] = valuesAll[nodeIndex]; _FValues[ino].Real = valuesAll[nodeIndex].Real; _FValues[ino].Imaginary = valuesAll[nodeIndex].Imaginary; } else { // 強制境界とみなす //_FValues[ino] = new Complex(); } } _FactorForRot = factorForRot; for (int i = 0; i < _media_Q.GetLength(0); i++) { for (int j = 0; j < _media_Q.GetLength(1); j++) { _media_Q[i, j] = media_Q[i, j]; } } _WaveModeDv = waveModeDv; // フィールドの回転を求める calcRotField(out _RotXFValues, out _RotYFValues); // 複素共役を格納 //if (_RotXFValues != null && _RotYFValues != null) //{ // int nno = NodeNumbers.Length; // for (int ino = 0; ino < nno; ino++) // { // _RotXFValues[ino] = Complex.Conjugate(_RotXFValues[ino]); // _RotYFValues[ino] = Complex.Conjugate(_RotYFValues[ino]); // } //} // 回転を計算できたら(実装されていたら)、複素ポインティングベクトルを計算する _PoyntingXFValues = null; _PoyntingYFValues = null; if (_RotXFValues != null && _RotYFValues != null) { int nno = NodeNumbers.Length; _PoyntingXFValues = new Complex[nno]; _PoyntingYFValues = new Complex[nno]; for (int ino = 0; ino < nno; ino++) { { if (_WaveModeDv == FemSolver.WaveModeDV.TM) { // F:磁界(Z成分) // G:電界(XY成分) // E x H* = rot x (fz)* = { (fz)*(roty), - (fz)* (rotx) } (rotは_FactorForRotを乗算済み) //_PoyntingXFValues[ino] = -1.0 * Complex.Conjugate(_FValues[ino]) * _RotYFValues[ino]; //_PoyntingYFValues[ino] = Complex.Conjugate(_FValues[ino]) * _RotXFValues[ino]; _PoyntingXFValues[ino] = -1.0 * _FValues[ino] * Complex.Conjugate(_RotYFValues[ino]); _PoyntingYFValues[ino] = _FValues[ino] * Complex.Conjugate(_RotXFValues[ino]); } else { // F:電界(Z成分) // G:磁界(XY成分) // (E x H*) = (fz x (rot)*) = { - fz(roty)*, fz (rotx)* } (rotは_FactorForRotを乗算済み) _PoyntingXFValues[ino] = _FValues[ino] * Complex.Conjugate(_RotYFValues[ino]); //_PoyntingYFValues[ino] = -1.0 * _FValues[ino] * Complex.Conjugate(_RotXFValues[ino]); _PoyntingYFValues[ino] = _FValues[ino] * Complex.Conjugate(_RotXFValues[ino]); } } } } }
/// <summary> /// 初期化処理(入力) /// </summary> private void initInput() { Nodes = null; Elements = null; Medias = null; Ports = null; ForceNodes = null; WaveguideWidth = FemSolver.DefWaveguideWidth; IncidentPortNo = 1; CalcFreqCnt = 0; FirstWaveLength = 0.0; LastWaveLength = 0.0; WaveModeDv = FemSolver.WaveModeDV.TE; _IsCoarseFieldMesh = false; }
/// <summary> /// 入出力ポート境界条件の追加 /// </summary> /// <param name="waveLength"></param> /// <param name="isInputPort"></param> /// <param name="nodesBoundary"></param> /// <param name="ryy_1d"></param> /// <param name="eigenValues"></param> /// <param name="eigenVecs"></param> /// <param name="nodesRegion"></param> /// <param name="mat"></param> /// <param name="resVec"></param> public static void AddPortBC( FemSolver.WaveModeDV WaveModeDv, double waveLength, bool isInputPort, int IncidentModeIndex, int[] nodesBoundary, MyDoubleMatrix ryy_1d, Complex[] eigenValues, Complex[,] eigenVecs, int[] nodesRegion, IList <FemElement> Elements, MediaInfo[] Medias, Dictionary <int, bool> ForceNodeNumberH, MyComplexMatrix mat, Complex[] resVec) { double k0 = 2.0 * pi / waveLength; double omega = k0 / Math.Sqrt(mu0 * eps0); // 境界上の節点数(1次線要素を想定) int nodeCnt = nodesBoundary.Length; // 考慮するモード数 int maxMode = eigenValues.Length; // 全体剛性行列の作成 MyComplexMatrix matB = new MyComplexMatrix(nodeCnt, nodeCnt); for (int imode = 0; imode < maxMode; imode++) { Complex betam = eigenValues[imode]; Complex[] fmVec = MyMatrixUtil.matrix_GetRowVec(eigenVecs, (int)imode); // 2Dの境界積分 Complex[] veci = MyMatrixUtil.product(ryy_1d, fmVec); // モード電力規格化の積分(1Dの積分) // [ryy]が実数の場合 Complex[] vecj = MyMatrixUtil.product(ryy_1d, MyMatrixUtil.vector_Conjugate(fmVec)); for (int inoB = 0; inoB < nodeCnt; inoB++) { for (int jnoB = 0; jnoB < nodeCnt; jnoB++) { //Complex cvalue = (Complex.ImaginaryOne / (omega * myu0)) * betam * Complex.Abs(betam) * veci[inoB] * vecj[jnoB]; Complex cvalue; { // H面、平行平板 if (WaveModeDv == FemSolver.WaveModeDV.TM) { cvalue = (Complex.ImaginaryOne / (omega * eps0)) * betam * Complex.Abs(betam) * veci[inoB] * vecj[jnoB]; } else { cvalue = (Complex.ImaginaryOne / (omega * mu0)) * betam * Complex.Abs(betam) * veci[inoB] * vecj[jnoB]; } } matB._body[inoB + jnoB * matB.RowSize] += cvalue; } } } // check 対称行列 for (int inoB = 0; inoB < matB.RowSize; inoB++) { for (int jnoB = inoB; jnoB < matB.ColumnSize; jnoB++) { if (Math.Abs(matB[inoB, jnoB].Real - matB[jnoB, inoB].Real) >= Constants.PrecisionLowerLimit) { System.Diagnostics.Debug.Assert(false); } if (Math.Abs(matB[inoB, jnoB].Imaginary - matB[jnoB, inoB].Imaginary) >= Constants.PrecisionLowerLimit) { System.Diagnostics.Debug.Assert(false); } } } //MyMatrixUtil.printMatrix("matB", matB); // 残差ベクトルの作成 Complex[] resVecB = new Complex[nodeCnt]; if (isInputPort) { int imode = IncidentModeIndex; Complex betam = eigenValues[imode]; Complex[] fmVec = MyMatrixUtil.matrix_GetRowVec(eigenVecs, (int)imode); // 2Dの境界積分 Complex[] veci = MyMatrixUtil.product(ryy_1d, fmVec); for (int inoB = 0; inoB < nodeCnt; inoB++) { // H面、平行平板、E面 Complex cvalue = 2.0 * Complex.ImaginaryOne * betam * veci[inoB]; resVecB[inoB].Real = cvalue.Real; resVecB[inoB].Imaginary = cvalue.Imaginary; } } //printVec("resVecB", resVecB); // 2D節点番号→ソート済みリストインデックスのマップ Dictionary <int, int> toSorted = new Dictionary <int, int>(); // 2D節点番号→ソート済みリストインデックスのマップ作成 for (int i = 0; i < nodesRegion.Length; i++) { int nodeNumber = nodesRegion[i]; if (!toSorted.ContainsKey(nodeNumber)) { toSorted.Add(nodeNumber, i); } } // 要素剛性行列にマージ // この定式化では行列のスパース性は失われている(隣接していない要素の節点間にも関連がある) // 要素剛性行列にマージする for (int inoB = 0; inoB < nodeCnt; inoB++) { int iNodeNumber = nodesBoundary[inoB]; if (ForceNodeNumberH.ContainsKey(iNodeNumber)) { continue; } int inoGlobal = toSorted[iNodeNumber]; for (int jnoB = 0; jnoB < nodeCnt; jnoB++) { int jNodeNumber = nodesBoundary[jnoB]; if (ForceNodeNumberH.ContainsKey(jNodeNumber)) { continue; } int jnoGlobal = toSorted[jNodeNumber]; // Note: matBは一般行列 matはバンド行列 mat._body[mat.GetBufferIndex(inoGlobal, jnoGlobal)] += matB._body[inoB + jnoB * matB.RowSize]; } } // 残差ベクトルにマージ for (int inoB = 0; inoB < nodeCnt; inoB++) { int iNodeNumber = nodesBoundary[inoB]; if (ForceNodeNumberH.ContainsKey(iNodeNumber)) { continue; } int inoGlobal = toSorted[iNodeNumber]; resVec[inoGlobal] += resVecB[inoB]; } }
/// <summary> /// 散乱行列の計算 /// </summary> /// <param name="waveLength"></param> /// <param name="iMode"></param> /// <param name="isIncidentMode"></param> /// <param name="nodesBoundary"></param> /// <param name="ryy_1d"></param> /// <param name="eigenValues"></param> /// <param name="eigenVecs"></param> /// <param name="nodesRegion"></param> /// <param name="valuesAll"></param> /// <returns></returns> public static Complex GetWaveguidePortReflectionCoef( FemSolver.WaveModeDV WaveModeDv, double waveLength, int iMode, bool isIncidentMode, int[] nodesBoundary, MyDoubleMatrix ryy_1d, Complex[] eigenValues, Complex[,] eigenVecs, int[] nodesRegion, IList <FemElement> Elements, MediaInfo[] Medias, Complex[] valuesAll) { // 2D節点番号→ソート済みリストインデックスのマップ Dictionary <int, int> toSorted = new Dictionary <int, int>(); // 2D節点番号→ソート済みリストインデックスのマップ作成 for (int i = 0; i < nodesRegion.Length; i++) { int nodeNumber = nodesRegion[i]; if (!toSorted.ContainsKey(nodeNumber)) { toSorted.Add(nodeNumber, i); } } // ポート上の界を取得する int nodeCnt = nodesBoundary.Length; Complex[] valuesB = new Complex[nodeCnt]; for (int ino = 0; ino < nodeCnt; ino++) { int nodeNumber = nodesBoundary[ino]; int inoGlobal = toSorted[nodeNumber]; valuesB[ino] = valuesAll[inoGlobal]; } double k0 = 2.0 * pi / waveLength; double omega = k0 * c0; Complex s11 = new Complex(0.0, 0.0); int maxMode = eigenValues.Length; // {tmp_vec}*t = {fm}*t[ryy]*t // {tmp_vec}* = [ryy]* {fm}* // ([ryy]*)t = [ryy]* // [ryy]が実数のときは、[ryy]* -->[ryy] Complex[] fmVec = MyMatrixUtil.matrix_GetRowVec(eigenVecs, iMode); // ryyが実数のとき Complex[] tmp_vec = MyMatrixUtil.product(ryy_1d, MyMatrixUtil.vector_Conjugate(fmVec)); // s11 = {tmp_vec}t {value_all} s11 = MyMatrixUtil.vector_Dot(tmp_vec, valuesB); Complex betam = eigenValues[iMode]; { // H面、平行平板 if (WaveModeDv == FemSolver.WaveModeDV.TM) { s11 *= (Complex.Abs(betam) / (omega * eps0)); if (isIncidentMode) { s11 += -1.0; } } else { s11 *= (Complex.Abs(betam) / (omega * mu0)); if (isIncidentMode) { s11 += -1.0; } } } return(s11); }
/// <summary> /// ヘルムホルツ方程式に対する有限要素マトリクス作成 /// </summary> /// <param name="waveLength">波長</param> /// <param name="toSorted">ソートされた節点インデックス( 2D節点番号→ソート済みリストインデックスのマップ)</param> /// <param name="element">有限要素</param> /// <param name="Nodes">節点リスト</param> /// <param name="Medias">媒質リスト</param> /// <param name="ForceNodeNumberH">強制境界節点ハッシュ</param> /// <param name="WGStructureDv">導波路構造区分</param> /// <param name="WaveModeDv">計算する波のモード区分</param> /// <param name="waveguideWidthForEPlane">導波路幅(E面解析用)</param> /// <param name="mat">マージされる全体行列(clapack使用時)</param> /// <param name="mat_cc">マージされる全体行列(DelFEM使用時)</param> /// <param name="res_c">マージされる残差ベクトル(DelFEM使用時)</param> /// <param name="tmpBuffer">一時バッファ(DelFEM使用時)</param> public static void AddElementMat(double waveLength, Dictionary <int, int> toSorted, FemElement element, IList <FemNode> Nodes, MediaInfo[] Medias, Dictionary <int, bool> ForceNodeNumberH, FemSolver.WGStructureDV WGStructureDv, FemSolver.WaveModeDV WaveModeDv, double waveguideWidthForEPlane, ref MyComplexMatrix mat, ref DelFEM4NetMatVec.CZMatDia_BlkCrs_Ptr mat_cc, ref DelFEM4NetMatVec.CZVector_Blk_Ptr res_c, ref int[] tmpBuffer) { // 定数 const double pi = Constants.pi; const double c0 = Constants.c0; // 波数 double k0 = 2.0 * pi / waveLength; // 角周波数 double omega = k0 * c0; // 要素頂点数 //const int vertexCnt = Constants.QuadVertexCnt; //4; // 要素内節点数 const int nno = Constants.QuadNodeCnt_FirstOrder; //4; // 1次セレンディピティ // 座標次元数 const int ndim = Constants.CoordDim2D; //2; int[] nodeNumbers = element.NodeNumbers; int[] no_c = new int[nno]; MediaInfo media = Medias[element.MediaIndex]; double[,] media_P = null; double[,] media_Q = null; // ヘルムホルツ方程式のパラメータP,Qを取得する FemSolver.GetHelmholtzMediaPQ( k0, media, WGStructureDv, WaveModeDv, waveguideWidthForEPlane, out media_P, out media_Q); // 節点座標(IFの都合上配列の配列形式の2次元配列を作成) double[][] pp = new double[nno][]; for (int ino = 0; ino < nno; ino++) { int nodeNumber = nodeNumbers[ino]; int nodeIndex = nodeNumber - 1; FemNode node = Nodes[nodeIndex]; no_c[ino] = nodeNumber; pp[ino] = new double[ndim]; for (int n = 0; n < ndim; n++) { pp[ino][n] = node.Coord[n]; } } // 四角形の辺の長さを求める double[] le = new double[4]; le[0] = FemMeshLogic.GetDistance(pp[0], pp[1]); le[1] = FemMeshLogic.GetDistance(pp[1], pp[2]); le[2] = FemMeshLogic.GetDistance(pp[2], pp[3]); le[3] = FemMeshLogic.GetDistance(pp[3], pp[0]); System.Diagnostics.Debug.Assert(Math.Abs(le[0] - le[2]) < Constants.PrecisionLowerLimit); System.Diagnostics.Debug.Assert(Math.Abs(le[1] - le[3]) < Constants.PrecisionLowerLimit); double lx = le[0]; double ly = le[1]; // 要素節点座標( 局所r,s成分 ) // s // | // 3+ + +2 // | | | // ---+---+---+-->r // | | | // 0+ + +1 // | // double[][] n_pts = { // r, s new double[] { -1.0, -1.0 }, //0 new double[] { 1.0, -1.0 }, //1 new double[] { 1.0, 1.0 }, //2 new double[] { -1.0, 1.0 }, //3 }; // ∫dN/dndN/dn dxdy // integralDNDX[n, ino, jno] n = 0 --> ∫dN/dxdN/dx dxdy // n = 1 --> ∫dN/dydN/dy dxdy double[, ,] integralDNDX = new double[ndim, nno, nno] { { { 2.0 * ly / (6.0 * lx), -2.0 * ly / (6.0 * lx), -1.0 * ly / (6.0 * lx), 1.0 * ly / (6.0 * lx) }, { -2.0 * ly / (6.0 * lx), 2.0 * ly / (6.0 * lx), 1.0 * ly / (6.0 * lx), -1.0 * ly / (6.0 * lx) }, { -1.0 * ly / (6.0 * lx), 1.0 * ly / (6.0 * lx), 2.0 * ly / (6.0 * lx), -2.0 * ly / (6.0 * lx) }, { 1.0 * ly / (6.0 * lx), -1.0 * ly / (6.0 * lx), -2.0 * ly / (6.0 * lx), 2.0 * ly / (6.0 * lx) }, }, { { 2.0 * lx / (6.0 * ly), 1.0 * lx / (6.0 * ly), -1.0 * lx / (6.0 * ly), -2.0 * lx / (6.0 * ly) }, { 1.0 * lx / (6.0 * ly), 2.0 * lx / (6.0 * ly), -2.0 * lx / (6.0 * ly), -1.0 * lx / (6.0 * ly) }, { -1.0 * lx / (6.0 * ly), -2.0 * lx / (6.0 * ly), 2.0 * lx / (6.0 * ly), 1.0 * lx / (6.0 * ly) }, { -2.0 * lx / (6.0 * ly), -1.0 * lx / (6.0 * ly), 1.0 * lx / (6.0 * ly), 2.0 * lx / (6.0 * ly) }, } }; // ∫N N dxdy double[,] integralN = new double[nno, nno] { { 4.0 * lx * ly / 36.0, 2.0 * lx * ly / 36.0, 1.0 * lx * ly / 36.0, 2.0 * lx * ly / 36.0 }, { 2.0 * lx * ly / 36.0, 4.0 * lx * ly / 36.0, 2.0 * lx * ly / 36.0, 1.0 * lx * ly / 36.0 }, { 1.0 * lx * ly / 36.0, 2.0 * lx * ly / 36.0, 4.0 * lx * ly / 36.0, 2.0 * lx * ly / 36.0 }, { 2.0 * lx * ly / 36.0, 1.0 * lx * ly / 36.0, 2.0 * lx * ly / 36.0, 4.0 * lx * ly / 36.0 }, }; // 要素剛性行列を作る double[,] emat = new double[nno, nno]; for (int ino = 0; ino < nno; ino++) { for (int jno = 0; jno < nno; jno++) { emat[ino, jno] = media_P[0, 0] * integralDNDX[1, ino, jno] + media_P[1, 1] * integralDNDX[0, ino, jno] - k0 * k0 * media_Q[2, 2] * integralN[ino, jno]; } } // 要素剛性行列にマージする if (mat_cc != null) { // 全体節点番号→要素内節点インデックスマップ Dictionary <uint, int> inoGlobalDic = new Dictionary <uint, int>(); for (int ino = 0; ino < nno; ino++) { int iNodeNumber = no_c[ino]; if (ForceNodeNumberH.ContainsKey(iNodeNumber)) { continue; } uint inoGlobal = (uint)toSorted[iNodeNumber]; inoGlobalDic.Add(inoGlobal, ino); } // マージ用の節点番号リスト uint[] no_c_tmp = inoGlobalDic.Keys.ToArray <uint>(); // マージする節点数("col"と"row"のサイズ) uint ncolrow_tmp = (uint)no_c_tmp.Length; // Note: // 要素の節点がすべて強制境界の場合がある.その場合は、ncolrow_tmpが0 if (ncolrow_tmp > 0) { // マージする要素行列 DelFEM4NetCom.Complex[] ematBuffer = new DelFEM4NetCom.Complex[ncolrow_tmp * ncolrow_tmp]; for (int ino_tmp = 0; ino_tmp < ncolrow_tmp; ino_tmp++) { int ino = inoGlobalDic[no_c_tmp[ino_tmp]]; for (int jno_tmp = 0; jno_tmp < ncolrow_tmp; jno_tmp++) { int jno = inoGlobalDic[no_c_tmp[jno_tmp]]; double value = emat[ino, jno]; DelFEM4NetCom.Complex cvalueDelFEM = new DelFEM4NetCom.Complex(value, 0); // ematBuffer[ino_tmp, jno_tmp] 横ベクトルを先に埋める(clapack方式でないことに注意) ematBuffer[ino_tmp * ncolrow_tmp + jno_tmp] = cvalueDelFEM; } } // 全体行列に要素行列をマージする mat_cc.Mearge(ncolrow_tmp, no_c_tmp, ncolrow_tmp, no_c_tmp, 1, ematBuffer, ref tmpBuffer); } } else if (mat != null) { for (int ino = 0; ino < nno; ino++) { int iNodeNumber = no_c[ino]; if (ForceNodeNumberH.ContainsKey(iNodeNumber)) { continue; } int inoGlobal = toSorted[iNodeNumber]; for (int jno = 0; jno < nno; jno++) { int jNodeNumber = no_c[jno]; if (ForceNodeNumberH.ContainsKey(jNodeNumber)) { continue; } int jnoGlobal = toSorted[jNodeNumber]; //mat[inoGlobal, jnoGlobal] += emat[ino, jno]; //mat._body[inoGlobal + jnoGlobal * mat.RowSize] += emat[ino, jno]; // 実数部に加算する //mat._body[inoGlobal + jnoGlobal * mat.RowSize].Real += emat[ino, jno]; // バンドマトリクス対応 mat._body[mat.GetBufferIndex(inoGlobal, jnoGlobal)].Real += emat[ino, jno]; } } } }
/// <summary> /// Fem入力データファイルへ保存 /// I/FがCadの内部データ寄りになっているので、変更したいが後回し /// </summary> /// <param name="filename">ファイル名(*.fem)</param> /// <param name="nodeCnt">節点数</param> /// <param name="doubleCoords">節点座標リスト</param> /// <param name="elementCnt">要素数</param> /// <param name="elements">要素リスト</param> /// <param name="portCnt">ポート数</param> /// <param name="portList">ポートの節点番号リストのリスト</param> /// <param name="forceBCNodeNumbers">強制境界節点番号のリスト</param> /// <param name="incidentPortNo">入射ポート番号</param> /// <param name="medias">媒質情報リスト</param> /// <param name="firstWaveLength">計算開始波長</param> /// <param name="lastWaveLength">計算終了波長</param> /// <param name="calcCnt">計算周波数件数</param> /// <param name="wgStructureDv">導波路構造区分</param> /// <param name="waveModeDv">波のモード区分</param> /// <param name="lsEqnSolverDv">線形方程式解法区分</param> /// <param name="waveguideWidthForEPlane">導波路幅(E面解析用)</param> public static void SaveToFileFromCad (string filename, int nodeCnt, IList <double[]> doubleCoords, int elementCnt, IList <int[]> elements, int portCnt, IList <IList <int> > portList, int[] forceBCNodeNumbers, int incidentPortNo, MediaInfo[] medias, double firstWaveLength, double lastWaveLength, int calcCnt, FemSolver.WGStructureDV wgStructureDv, FemSolver.WaveModeDV waveModeDv, FemSolver.LinearSystemEqnSoverDV lsEqnSolverDv, double waveguideWidthForEPlane) { ////////////////////////////////////////// // ファイル出力 ////////////////////////////////////////// try { using (StreamWriter sw = new StreamWriter(filename)) { string line; // 節点番号と座標の出力 line = string.Format("Nodes,{0}", nodeCnt); sw.WriteLine(line); for (int i = 0; i < doubleCoords.Count; i++) { double[] doubleCoord = doubleCoords[i]; int nodeNumber = i + 1; line = string.Format("{0},{1},{2}", nodeNumber, doubleCoord[0], doubleCoord[1]); sw.WriteLine(line); } // 要素番号と要素を構成する節点の全体節点番号の出力 line = string.Format("Elements,{0}", elementCnt); sw.WriteLine(line); foreach (int[] element in elements) { line = ""; foreach (int k in element) { line += string.Format("{0},", k); } line = line.Substring(0, line.Length - 1); // 最後の,を削除 sw.WriteLine(line); } // ポート境界条件節点 int portCounter = 0; line = string.Format("Ports,{0}", portList.Count); sw.WriteLine(line); foreach (IList <int> nodes in portList) { line = string.Format("{0},{1}", ++portCounter, nodes.Count); sw.WriteLine(line); int portNodeNumber = 0; foreach (int nodeNumber in nodes) { line = string.Format("{0},{1}", ++portNodeNumber, nodeNumber); sw.WriteLine(line); } } // 強制境界節点 line = string.Format("Force,{0}", forceBCNodeNumbers.Length); sw.WriteLine(line); foreach (int nodeNumber in forceBCNodeNumbers) { line = string.Format("{0}", nodeNumber); sw.WriteLine(line); } // 入射ポート番号 line = string.Format("IncidentPortNo,{0}", incidentPortNo); sw.WriteLine(line); ////////////////////////////////////////// //// Ver1.1.0.0からの追加情報 ////////////////////////////////////////// // 媒質情報の個数 sw.WriteLine("Medias,{0}", medias.Length); // 媒質情報の書き込み for (int i = 0; i < medias.Length; i++) { MediaInfo media = medias[i]; line = string.Format("{0},", i); double[,] p = media.P; for (int m = 0; m < p.GetLength(0); m++) { for (int n = 0; n < p.GetLength(1); n++) { line += string.Format("{0},", p[m, n]); } } double[,] q = media.Q; for (int m = 0; m < q.GetLength(0); m++) { for (int n = 0; n < q.GetLength(1); n++) { line += string.Format("{0},", q[m, n]); } } line = line.Remove(line.Length - 1); // 最後の,を削除 sw.WriteLine(line); } // 計算対象周波数 sw.WriteLine("WaveLengthRange,{0},{1},{2}", firstWaveLength, lastWaveLength, calcCnt); // 線形方程式解法区分 sw.WriteLine("LsEqnSolverDv,{0}", FemSolver.LinearSystemEqnSolverDVToStr(lsEqnSolverDv)); // 計算対象モード区分 sw.WriteLine("WaveModeDv,{0}", ((waveModeDv == FemSolver.WaveModeDV.TM) ? "TM" : "TE")); // 導波路構造区分 sw.WriteLine("WGStructureDv,{0}", FemSolver.WGStructureDVToStr(wgStructureDv)); // 導波路幅(E面解析用) sw.WriteLine("WaveguideWidthForEPlane,{0}", waveguideWidthForEPlane); } } catch (Exception exception) { System.Diagnostics.Debug.WriteLine(exception.Message + " " + exception.StackTrace); MessageBox.Show(exception.Message); } }
/// <summary> /// ポート固有値解析 /// </summary> public static void SolvePortWaveguideEigen( FemSolver.WaveModeDV WaveModeDv, double waveLength, int maxModeSpecified, IList <FemNode> Nodes, Dictionary <string, IList <int> > EdgeToElementNoH, IList <FemElement> Elements, MediaInfo[] Medias, Dictionary <int, bool> ForceNodeNumberH, IList <int> portNodes, out int[] nodesBoundary, out MyDoubleMatrix ryy_1d, out Complex[] eigenValues, out Complex[,] eigenVecs) { //System.Diagnostics.Debug.WriteLine("solvePortWaveguideEigen: {0},{1}", waveLength, portNo); nodesBoundary = null; ryy_1d = null; eigenValues = null; eigenVecs = null; // 2D次元数 const int ndim2d = Constants.CoordDim2D; //2; // 波数 double k0 = 2.0 * pi / waveLength; // 角周波数 double omega = k0 * c0; // 節点番号リスト(要素インデックス: 1D節点番号 - 1 要素:2D節点番号) IList <int> nodes = portNodes; // 2D→1D節点番号マップ Dictionary <int, int> to1dNodes = new Dictionary <int, int>(); // 節点座標リスト IList <double> coords = new List <double>(); // 要素リスト IList <FemLineElement> elements = new List <FemLineElement>(); // 1D節点番号リスト(ソート済み) IList <int> sortedNodes = new List <int>(); // 1D節点番号→ソート済みリストインデックスのマップ Dictionary <int, int> toSorted = new Dictionary <int, int>(); // 2Dの要素から次数を取得する Constants.FemElementShapeDV elemShapeDv2d; int order; int vertexCnt2d; FemMeshLogic.GetElementShapeDvAndOrderByElemNodeCnt(Elements[0].NodeNumbers.Length, out elemShapeDv2d, out order, out vertexCnt2d); // 2D→1D節点番号マップ作成 for (int i = 0; i < nodes.Count; i++) { int nodeNumber2d = nodes[i]; if (!to1dNodes.ContainsKey(nodeNumber2d)) { to1dNodes.Add(nodeNumber2d, i + 1); } } // 原点 int nodeNumber0 = nodes[0]; int nodeIndex0 = nodeNumber0 - 1; FemNode node0 = Nodes[nodeIndex0]; double[] coord0 = new double[ndim2d]; coord0[0] = node0.Coord[0]; coord0[1] = node0.Coord[1]; // 座標リスト作成 double[] coord = new double[ndim2d]; foreach (int nodeNumber in nodes) { int nodeIndex = nodeNumber - 1; FemNode node = Nodes[nodeIndex]; coord[0] = node.Coord[0]; coord[1] = node.Coord[1]; double x = FemMeshLogic.GetDistance(coord, coord0); //System.Diagnostics.Debug.WriteLine("{0},{1},{2},{3}", nodeIndex, coord[0], coord[1], x); coords.Add(x); } // 線要素を作成する if (order == Constants.FirstOrder) { // 1次線要素 FemMat_Line_First.MkElements( nodes, EdgeToElementNoH, Elements, ref elements); } else { // 2次線要素 FemMat_Line_Second.MkElements( nodes, EdgeToElementNoH, Elements, ref elements); } // 強制境界節点と内部領域節点を分離 foreach (int nodeNumber2d in nodes) { int nodeNumber = to1dNodes[nodeNumber2d]; if (ForceNodeNumberH.ContainsKey(nodeNumber2d)) { System.Diagnostics.Debug.WriteLine("{0}: {1} {2}", nodeNumber, Nodes[nodeNumber2d - 1].Coord[0], Nodes[nodeNumber2d - 1].Coord[1]); } else { sortedNodes.Add(nodeNumber); toSorted.Add(nodeNumber, sortedNodes.Count - 1); } } // 対称バンド行列のパラメータを取得する int rowcolSize = 0; int subdiaSize = 0; int superdiaSize = 0; { bool[,] matPattern = null; GetMatNonzeroPatternForEigen(elements, toSorted, out matPattern); GetBandMatrixSubDiaSizeAndSuperDiaSizeForEigen(matPattern, out rowcolSize, out subdiaSize, out superdiaSize); } // ソート済み1D節点インデックス→2D節点番号マップ nodesBoundary = new int[sortedNodes.Count]; for (int i = 0; i < sortedNodes.Count; i++) { int nodeNumber = sortedNodes[i]; int nodeIndex = nodeNumber - 1; int nodeNumber2d = nodes[nodeIndex]; nodesBoundary[i] = nodeNumber2d; } // 節点数 int nodeCnt = sortedNodes.Count; // 固有値、固有ベクトル int maxMode = maxModeSpecified; if (maxMode > nodeCnt) { maxMode = nodeCnt; } eigenValues = new Complex[maxMode]; eigenVecs = new Complex[maxMode, nodeCnt]; // 固有モード解析でのみ使用するuzz_1d, txx_1d MyDoubleMatrix txx_1d = new MyDoubleSymmetricBandMatrix(nodeCnt, subdiaSize, superdiaSize); MyDoubleMatrix uzz_1d = new MyDoubleSymmetricBandMatrix(nodeCnt, subdiaSize, superdiaSize); // ryy_1dマトリクス (線要素) ryy_1d = new MyDoubleSymmetricBandMatrix(nodeCnt, subdiaSize, superdiaSize); for (int elemIndex = 0; elemIndex < elements.Count; elemIndex++) { // 線要素 FemLineElement element = elements[elemIndex]; // 1Dヘルムホルツ方程式固有値問題の要素行列を加算する if (order == Constants.FirstOrder) { // 1次線要素 FemMat_Line_First.AddElementMatOf1dEigenValueProblem( waveLength, // E面の場合のみ使用 element, coords, toSorted, Medias, WaveModeDv, ref txx_1d, ref ryy_1d, ref uzz_1d); } else { // 2次線要素 FemMat_Line_Second.AddElementMatOf1dEigenValueProblem( waveLength, // E面の場合のみ使用 element, coords, toSorted, Medias, WaveModeDv, ref txx_1d, ref ryy_1d, ref uzz_1d); } } // [A] = [Txx] - k0 * k0 *[Uzz] //メモリ節約 //MyDoubleMatrix matA = new MyDoubleMatrix(nodeCnt, nodeCnt); MyDoubleSymmetricBandMatrix matA = new MyDoubleSymmetricBandMatrix(nodeCnt, subdiaSize, superdiaSize); for (int ino = 0; ino < nodeCnt; ino++) { for (int jno = 0; jno < nodeCnt; jno++) { // 対称バンド行列対応 if (matA is MyDoubleSymmetricBandMatrix && ino > jno) { continue; } // 剛性行列 //matA[ino, jno] = txx_1d[ino, jno] - (k0 * k0) * uzz_1d[ino, jno]; // 質量行列matBが正定値行列となるように剛性行列matAの方の符号を反転する matA[ino, jno] = -(txx_1d[ino, jno] - (k0 * k0) * uzz_1d[ino, jno]); } } // ( [txx] - k0^2[uzz] + β^2[ryy]){Ez} = {0}より // [A]{x} = λ[B]{x}としたとき、λ = β^2 とすると[B] = -[ryy] //MyDoubleMatrix matB = MyMatrixUtil.product(-1.0, ryy_1d); // 質量行列が正定値となるようにするため、上記符号反転を剛性行列の方に反映し、質量行列はryy_1dをそのまま使用する //MyDoubleMatrix matB = new MyDoubleMatrix(ryy_1d); MyDoubleSymmetricBandMatrix matB = new MyDoubleSymmetricBandMatrix((MyDoubleSymmetricBandMatrix)ryy_1d); // 一般化固有値問題を解く Complex[] evals = null; Complex[,] evecs = null; try { // 固有値、固有ベクトルを求める solveEigen(matA, matB, out evals, out evecs); // 固有値のソート Sort1DEigenMode(k0, evals, evecs); } catch (Exception exception) { System.Diagnostics.Debug.WriteLine(exception.Message + " " + exception.StackTrace); System.Diagnostics.Debug.Assert(false); } for (int imode = 0; imode < evecs.GetLength(0); imode++) { KrdLab.clapack.Complex phaseShift = 1.0; double maxAbs = double.MinValue; KrdLab.clapack.Complex fValueAtMaxAbs = 0.0; { // 境界上で位相調整する for (int ino = 0; ino < evecs.GetLength(1); ino++) { KrdLab.clapack.Complex cvalue = evecs[imode, ino]; double abs = KrdLab.clapack.Complex.Abs(cvalue); if (abs > maxAbs) { maxAbs = abs; fValueAtMaxAbs = cvalue; } } } if (maxAbs >= MyUtilLib.Matrix.Constants.PrecisionLowerLimit) { phaseShift = fValueAtMaxAbs / maxAbs; } //System.Diagnostics.Debug.WriteLine("phaseShift: {0} (°)", Math.Atan2(phaseShift.Imaginary, phaseShift.Real) * 180.0 / pi); for (int ino = 0; ino < evecs.GetLength(1); ino++) { evecs[imode, ino] /= phaseShift; } } for (int imode = 0; imode < maxMode; imode++) { eigenValues[imode] = 0; } for (int tagtModeIdx = evals.Length - 1, imode = 0; tagtModeIdx >= 0 && imode < maxMode; tagtModeIdx--) { // 伝搬定数は固有値のsqrt Complex betam = Complex.Sqrt(evals[tagtModeIdx]); // 定式化BUGFIX // 減衰定数は符号がマイナス(β = -jα) bool isConjugateMode = false; if (betam.Imaginary >= 0.0) { betam = new Complex(betam.Real, -betam.Imaginary); isConjugateMode = true; } // 固有ベクトル Complex[] evec = MyMatrixUtil.matrix_GetRowVec(evecs, tagtModeIdx); if (isConjugateMode) { evec = MyMatrixUtil.vector_Conjugate(evec); } // 規格化定数を求める // 実数の場合 [ryy]*t = [ryy]t ryyは対称行列より[ryy]t = [ryy] Complex[] workVec = MyMatrixUtil.product(ryy_1d, evec); Complex dm = MyMatrixUtil.vector_Dot(MyMatrixUtil.vector_Conjugate(evec), workVec); { // H面、平行平板 if (WaveModeDv == FemSolver.WaveModeDV.TM) { dm = Complex.Sqrt(omega * eps0 / Complex.Abs(betam) / dm); } else { dm = Complex.Sqrt(omega * mu0 / Complex.Abs(betam) / dm); } } //System.Diagnostics.Debug.WriteLine("dm = " + dm); // 伝搬定数の格納 eigenValues[imode] = betam; // check if (imode < 5) { //System.Diagnostics.Debug.WriteLine("eigenValues [ " + imode + "] = " + betam.Real + " + " + betam.Imaginary + " i " + " tagtModeIdx :" + tagtModeIdx + " " ); System.Diagnostics.Debug.WriteLine("β/k0 [ " + imode + "] = " + betam.Real / k0 + " + " + betam.Imaginary / k0 + " i " + " tagtModeIdx :" + tagtModeIdx + " "); } // 固有ベクトルの格納(規格化定数を掛ける) for (int inoSorted = 0; inoSorted < nodeCnt; inoSorted++) { Complex fm = dm * evec[inoSorted]; eigenVecs[imode, inoSorted] = fm; //System.Diagnostics.Debug.WriteLine("eigenVecs [ " + imode + ", " + inoSorted + "] = " + fm.Real + " + " + fm.Imaginary + " i Abs:" + Complex.Abs(fm)); } imode++; } }
/// <summary> /// コンストラクタ /// </summary> public FemElement() { No = 0; NodeNumbers = null; MediaIndex = 0; LineColor = Color.Black; BackColor = Color.White; _Nodes = null; _FValues = null; _RotXFValues = null; _RotYFValues = null; _PoyntingXFValues = null; _PoyntingYFValues = null; _FactorForRot = 1.0; _media_Q = new double[,] { {1.0, 0.0, 0.0}, {0.0, 1.0, 0.0}, {0.0, 0.0, 1.0}, }; _WaveModeDv = FemSolver.WaveModeDV.TE; _WGStructureDv = FemSolver.WGStructureDV.HPlane2D; IsCoarseFieldMesh = false; }
/// <summary> /// ヘルムホルツ方程式に対する有限要素マトリクス作成 /// </summary> /// <param name="waveLength">波長</param> /// <param name="toSorted">ソートされた節点インデックス( 2D節点番号→ソート済みリストインデックスのマップ)</param> /// <param name="element">有限要素</param> /// <param name="Nodes">節点リスト</param> /// <param name="Medias">媒質リスト</param> /// <param name="ForceNodeNumberH">強制境界節点ハッシュ</param> /// <param name="WGStructureDv">導波路構造区分</param> /// <param name="WaveModeDv">計算する波のモード区分</param> /// <param name="waveguideWidthForEPlane">導波路幅(E面解析用)</param> /// <param name="mat">マージされる全体行列(clapack使用時)</param> /// <param name="mat_cc">マージされる全体行列(DelFEM使用時)</param> /// <param name="res_c">マージされる残差ベクトル(DelFEM使用時)</param> /// <param name="tmpBuffer">一時バッファ(DelFEM使用時)</param> public static void AddElementMat(double waveLength, Dictionary <int, int> toSorted, FemElement element, IList <FemNode> Nodes, MediaInfo[] Medias, Dictionary <int, bool> ForceNodeNumberH, FemSolver.WGStructureDV WGStructureDv, FemSolver.WaveModeDV WaveModeDv, double waveguideWidthForEPlane, ref MyComplexMatrix mat, ref DelFEM4NetMatVec.CZMatDia_BlkCrs_Ptr mat_cc, ref DelFEM4NetMatVec.CZVector_Blk_Ptr res_c, ref int[] tmpBuffer) { // 定数 const double pi = Constants.pi; const double c0 = Constants.c0; // 波数 double k0 = 2.0 * pi / waveLength; // 角周波数 double omega = k0 * c0; // 要素頂点数 //const int vertexCnt = Constants.TriVertexCnt; //3; // 要素内節点数 const int nno = Constants.TriNodeCnt_FirstOrder; //3; // 1次三角形要素 // 座標次元数 const int ndim = Constants.CoordDim2D; //2; int[] nodeNumbers = element.NodeNumbers; int[] no_c = new int[nno]; MediaInfo media = Medias[element.MediaIndex]; // ver1.1.0.0 媒質情報の取得 double[,] media_P = null; double[,] media_Q = null; // ヘルムホルツ方程式のパラメータP,Qを取得する FemSolver.GetHelmholtzMediaPQ( k0, media, WGStructureDv, WaveModeDv, waveguideWidthForEPlane, out media_P, out media_Q); // 節点座標(IFの都合上配列の配列形式の2次元配列を作成) double[][] pp = new double[nno][]; for (int ino = 0; ino < nno; ino++) { int nodeNumber = nodeNumbers[ino]; int nodeIndex = nodeNumber - 1; FemNode node = Nodes[nodeIndex]; no_c[ino] = nodeNumber; pp[ino] = new double[ndim]; for (int n = 0; n < ndim; n++) { pp[ino][n] = node.Coord[n]; } } // 面積を求める double area = KerEMatTri.TriArea(pp[0], pp[1], pp[2]); //System.Diagnostics.Debug.WriteLine("Elem No {0} area: {1}", element.No, area); System.Diagnostics.Debug.Assert(area >= 0.0); // 面積座標の微分を求める // dldx[k, n] k面積座標Lkのn方向微分 double[,] dldx = null; double[] const_term = null; KerEMatTri.TriDlDx(out dldx, out const_term, pp[0], pp[1], pp[2]); // ∫dN/dndN/dn dxdy // integralDNDX[n, ino, jno] n = 0 --> ∫dN/dxdN/dx dxdy // n = 1 --> ∫dN/dydN/dy dxdy double[, ,] integralDNDX = new double[ndim, nno, nno]; for (int ino = 0; ino < nno; ino++) { for (int jno = 0; jno < nno; jno++) { integralDNDX[0, ino, jno] = area * dldx[ino, 0] * dldx[jno, 0]; integralDNDX[1, ino, jno] = area * dldx[ino, 1] * dldx[jno, 1]; } } // ∫N N dxdy double[,] integralN = new double[nno, nno] { { area / 6.0, area / 12.0, area / 12.0 }, { area / 12.0, area / 6.0, area / 12.0 }, { area / 12.0, area / 12.0, area / 6.0 }, }; // 要素剛性行列を作る double[,] emat = new double[nno, nno]; for (int ino = 0; ino < nno; ino++) { for (int jno = 0; jno < nno; jno++) { emat[ino, jno] = media_P[0, 0] * integralDNDX[1, ino, jno] + media_P[1, 1] * integralDNDX[0, ino, jno] - k0 * k0 * media_Q[2, 2] * integralN[ino, jno]; } } // 要素剛性行列にマージする if (mat_cc != null) { // 全体節点番号→要素内節点インデックスマップ Dictionary <uint, int> inoGlobalDic = new Dictionary <uint, int>(); for (int ino = 0; ino < nno; ino++) { int iNodeNumber = no_c[ino]; if (ForceNodeNumberH.ContainsKey(iNodeNumber)) { continue; } uint inoGlobal = (uint)toSorted[iNodeNumber]; inoGlobalDic.Add(inoGlobal, ino); } // マージ用の節点番号リスト uint[] no_c_tmp = inoGlobalDic.Keys.ToArray <uint>(); // マージする節点数("col"と"row"のサイズ) uint ncolrow_tmp = (uint)no_c_tmp.Length; // Note: // 要素の節点がすべて強制境界の場合がある.その場合は、ncolrow_tmpが0 if (ncolrow_tmp > 0) { // マージする要素行列 DelFEM4NetCom.Complex[] ematBuffer = new DelFEM4NetCom.Complex[ncolrow_tmp * ncolrow_tmp]; for (int ino_tmp = 0; ino_tmp < ncolrow_tmp; ino_tmp++) { int ino = inoGlobalDic[no_c_tmp[ino_tmp]]; for (int jno_tmp = 0; jno_tmp < ncolrow_tmp; jno_tmp++) { int jno = inoGlobalDic[no_c_tmp[jno_tmp]]; double value = emat[ino, jno]; DelFEM4NetCom.Complex cvalueDelFEM = new DelFEM4NetCom.Complex(value, 0); // ematBuffer[ino_tmp, jno_tmp] 横ベクトルを先に埋める(clapack方式でないことに注意) ematBuffer[ino_tmp * ncolrow_tmp + jno_tmp] = cvalueDelFEM; } } // 全体行列に要素行列をマージする mat_cc.Mearge(ncolrow_tmp, no_c_tmp, ncolrow_tmp, no_c_tmp, 1, ematBuffer, ref tmpBuffer); } } else if (mat != null) { for (int ino = 0; ino < nno; ino++) { int iNodeNumber = no_c[ino]; if (ForceNodeNumberH.ContainsKey(iNodeNumber)) { continue; } int inoGlobal = toSorted[iNodeNumber]; for (int jno = 0; jno < nno; jno++) { int jNodeNumber = no_c[jno]; if (ForceNodeNumberH.ContainsKey(jNodeNumber)) { continue; } int jnoGlobal = toSorted[jNodeNumber]; //mat[inoGlobal, jnoGlobal] += emat[ino, jno]; //mat._body[inoGlobal + jnoGlobal * mat.RowSize] += emat[ino, jno]; // 実数部に加算する //mat._body[inoGlobal + jnoGlobal * mat.RowSize].Real += emat[ino, jno]; // バンドマトリクス対応 mat._body[mat.GetBufferIndex(inoGlobal, jnoGlobal)].Real += emat[ino, jno]; } } } }
/// <summary> /// コピー /// </summary> /// <param name="src"></param> public virtual void CP(FemElement src) { if (src == this) { return; } No = src.No; NodeNumbers = null; if (src.NodeNumbers != null) { NodeNumbers = new int[src.NodeNumbers.Length]; for (int i = 0; i < src.NodeNumbers.Length; i++) { NodeNumbers[i] = src.NodeNumbers[i]; } } MediaIndex = src.MediaIndex; LineColor = src.LineColor; BackColor = src.BackColor; // 内部使用のフィールドはコピーしない _Nodes = null; _FValues = null; _RotXFValues = null; _RotYFValues = null; _PoyntingXFValues = null; _PoyntingYFValues = null; _FactorForRot = 1.0; _media_Q = new double[,] { {1.0, 0.0, 0.0}, {0.0, 1.0, 0.0}, {0.0, 0.0, 1.0}, }; _WaveModeDv = FemSolver.WaveModeDV.TE; _WGStructureDv = FemSolver.WGStructureDV.HPlane2D; IsCoarseFieldMesh = false; }
/// <summary> /// 入出力データの初期化 /// </summary> public void InitData( FemSolver solver, Panel CadPanel, Panel FValuePanel, Panel FValueLegendPanel, Label labelFreqValue, Chart SMatChart, Chart BetaChart, Chart EigenVecChart ) { initInput(); initOutput(); // 一度だけの初期化処理 initDataOnce(FValueLegendPanel, labelFreqValue); // ポストプロセッサに入力データをコピー // 入力データの取得 solver.GetFemInputInfo(out Nodes, out Elements, out Medias, out Ports, out ForceNodes, out IncidentPortNo, out WaveguideWidth, out LatticeA); // チャートの設定用に開始終了波長を取得 FirstWaveLength = solver.FirstWaveLength; LastWaveLength = solver.LastWaveLength; CalcFreqCnt = solver.CalcFreqCnt; // 波のモード区分を取得 WaveModeDv = solver.WaveModeDv; //if (isInputDataReady()) // ポートが指定されていなくてもメッシュを表示できるように条件を変更 if (Elements != null && Elements.Length > 0 && Nodes != null && Nodes.Length > 0 && Medias != null && Medias.Length > 0) { // 各要素に節点情報を補完する foreach (FemElement element in Elements) { element.SetNodesFromAllNodes(Nodes); element.LineColor = Color.Black; element.BackColor = Medias[element.MediaIndex].BackColor; } } // メッシュ描画 //using (Graphics g = CadPanel.CreateGraphics()) //{ // DrawMesh(g, CadPanel); //} //CadPanel.Invalidate(); if (!IsAutoCalc) { // チャート初期化 ResetSMatChart(SMatChart); // 等高線図の凡例 UpdateFValueLegend(FValueLegendPanel, labelFreqValue); // 等高線図 //FValuePanel.Invalidate(); FValuePanel.Refresh(); // 固有値チャート初期化 // この段階ではMaxModeの値が0なので、後に計算値ロード後一回だけ初期化する ResetEigenValueChart(BetaChart); // 固有ベクトル表示(空のデータで初期化) SetEigenVecToChart(EigenVecChart); } }
/// <summary> /// フィールド値をセットする /// </summary> /// <param name="valuesAll"></param> /// <param name="nodesRegionToIndex"></param> /// <param name="factorForRot">回転に掛ける因子(磁界または電界への変換)</param> public virtual void SetFieldValueFromAllValues(Complex[] valuesAll, Dictionary<int, int> nodesRegionToIndex, Complex factorForRot, double[,] media_Q, FemSolver.WGStructureDV wgStructureDv, FemSolver.WaveModeDV waveModeDv) { _FValues = new Complex[NodeNumbers.Length]; for (int ino = 0; ino < NodeNumbers.Length; ino++) { int nodeNumber = NodeNumbers[ino]; if (nodesRegionToIndex.ContainsKey(nodeNumber)) { int nodeIndex = nodesRegionToIndex[nodeNumber]; //_FValues[ino] = valuesAll[nodeIndex]; _FValues[ino].Real = valuesAll[nodeIndex].Real; _FValues[ino].Imaginary = valuesAll[nodeIndex].Imaginary; } else { // 強制境界とみなす //_FValues[ino] = new Complex(); } } _FactorForRot = factorForRot; for (int i = 0; i < _media_Q.GetLength(0); i++) { for (int j = 0; j < _media_Q.GetLength(1); j++) { _media_Q[i, j] = media_Q[i, j]; } } _WGStructureDv = wgStructureDv; _WaveModeDv = waveModeDv; // フィールドの回転を求める calcRotField(out _RotXFValues, out _RotYFValues); // 複素共役を格納 //if (_RotXFValues != null && _RotYFValues != null) //{ // int nno = NodeNumbers.Length; // for (int ino = 0; ino < nno; ino++) // { // _RotXFValues[ino] = Complex.Conjugate(_RotXFValues[ino]); // _RotYFValues[ino] = Complex.Conjugate(_RotYFValues[ino]); // } //} // 回転を計算できたら(実装されていたら)、複素ポインティングベクトルを計算する _PoyntingXFValues = null; _PoyntingYFValues = null; if (_RotXFValues != null && _RotYFValues != null) { int nno = NodeNumbers.Length; _PoyntingXFValues = new Complex[nno]; _PoyntingYFValues = new Complex[nno]; for (int ino = 0; ino < nno; ino++) { if (_WGStructureDv == FemSolver.WGStructureDV.EPlane2D) { if (_WaveModeDv == FemSolver.WaveModeDV.TM) { // F:電界(Z成分) // G:磁界(XY成分) // (E x H*) = (fz x (rot)*) = { - fz(roty)*, fz (rotx)* } (rotは_FactorForRotを乗算済み) _PoyntingXFValues[ino] = _FValues[ino] * Complex.Conjugate(_RotYFValues[ino]); _PoyntingYFValues[ino] = -1.0 * _FValues[ino] * Complex.Conjugate(_RotXFValues[ino]); } else { // F:磁界(Z成分) // G:電界(XY成分) // E x H* = rot x (fz)* = { (fz)*(roty), - (fz)* (rotx) } (rotは_FactorForRotを乗算済み) //_PoyntingXFValues[ino] = -1.0 * Complex.Conjugate(_FValues[ino]) * _RotYFValues[ino]; //_PoyntingYFValues[ino] = Complex.Conjugate(_FValues[ino]) * _RotXFValues[ino]; _PoyntingXFValues[ino] = -1.0 * _FValues[ino] * Complex.Conjugate(_RotYFValues[ino]); _PoyntingYFValues[ino] = _FValues[ino] * Complex.Conjugate(_RotXFValues[ino]); } } else { if (_WaveModeDv == FemSolver.WaveModeDV.TM) { // F:磁界(Z成分) // G:電界(XY成分) // E x H* = rot x (fz)* = { (fz)*(roty), - (fz)* (rotx) } (rotは_FactorForRotを乗算済み) //_PoyntingXFValues[ino] = -1.0 * Complex.Conjugate(_FValues[ino]) * _RotYFValues[ino]; //_PoyntingYFValues[ino] = Complex.Conjugate(_FValues[ino]) * _RotXFValues[ino]; _PoyntingXFValues[ino] = -1.0 * _FValues[ino] * Complex.Conjugate(_RotYFValues[ino]); _PoyntingYFValues[ino] = _FValues[ino] * Complex.Conjugate(_RotXFValues[ino]); } else { // F:電界(Z成分) // G:磁界(XY成分) // (E x H*) = (fz x (rot)*) = { - fz(roty)*, fz (rotx)* } (rotは_FactorForRotを乗算済み) _PoyntingXFValues[ino] = _FValues[ino] * Complex.Conjugate(_RotYFValues[ino]); _PoyntingYFValues[ino] = -1.0 * _FValues[ino] * Complex.Conjugate(_RotXFValues[ino]); } } } } }
/// <summary> /// 周期構造導波路固有値問題用FEM行列の追加 /// </summary> /// <param name="isYDirectionPeriodic"></param> /// <param name="waveLength"></param> /// <param name="nodeCntPeriodic"></param> /// <param name="freeNodeCntPeriodic_0"></param> /// <param name="toSortedPeriodic"></param> /// <param name="element"></param> /// <param name="Nodes"></param> /// <param name="Medias"></param> /// <param name="ForceNodeNumberH"></param> /// <param name="WaveModeDv"></param> /// <param name="KMat"></param> public static void AddElementMatPeriodic( bool isYDirectionPeriodic, bool isSVEA, double waveLength, int nodeCntPeriodic, int freeNodeCntPeriodic_0, Dictionary <int, int> toSortedPeriodic, FemElement element, IList <FemNode> Nodes, MediaInfo[] Medias, Dictionary <int, bool> ForceNodeNumberH, FemSolver.WaveModeDV WaveModeDv, ref double[] KMat, ref double[] CMat, ref double[] MMat) { // 定数 const double pi = Constants.pi; const double c0 = Constants.c0; // 波数 double k0 = 2.0 * pi / waveLength; // 角周波数 double omega = k0 * c0; // 要素頂点数 const int vertexCnt = Constants.TriVertexCnt; //3; // 要素内節点数 const int nno = Constants.TriNodeCnt_SecondOrder; //6; // 2次三角形要素 // 座標次元数 const int ndim = Constants.CoordDim2D; //2; int[] nodeNumbers = element.NodeNumbers; int[] no_c = new int[nno]; MediaInfo media = Medias[element.MediaIndex]; // ver1.1.0.0 媒質情報の取得 double[,] media_P = null; double[,] media_Q = null; // ヘルムホルツ方程式のパラメータP,Qを取得する FemSolver.GetHelmholtzMediaPQ( k0, media, WaveModeDv, out media_P, out media_Q); // 節点座標(IFの都合上配列の配列形式の2次元配列を作成) double[][] pp = new double[nno][]; for (int ino = 0; ino < nno; ino++) { int nodeNumber = nodeNumbers[ino]; int nodeIndex = nodeNumber - 1; FemNode node = Nodes[nodeIndex]; no_c[ino] = nodeNumber; pp[ino] = new double[ndim]; for (int n = 0; n < ndim; n++) { pp[ino][n] = node.Coord[n]; } } // 面積を求める double area = KerEMatTri.TriArea(pp[0], pp[1], pp[2]); //System.Diagnostics.Debug.WriteLine("Elem No {0} area: {1}", element.No, area); System.Diagnostics.Debug.Assert(area >= 0.0); // 面積座標の微分を求める // dldx[k, n] k面積座標Lkのn方向微分 double[,] dldx = null; double[] const_term = null; KerEMatTri.TriDlDx(out dldx, out const_term, pp[0], pp[1], pp[2]); // 形状関数の微分の係数を求める // dndxC[ino,n,k] ino節点のn方向微分のLk(k面積座標)の係数 // dNino/dn = dndxC[ino, n, 0] * L0 + dndxC[ino, n, 1] * L1 + dndxC[ino, n, 2] * L2 + dndxC[ino, n, 3] double[, ,] dndxC = new double[nno, ndim, vertexCnt + 1] { { { 4.0 * dldx[0, 0], 0.0, 0.0, -1.0 * dldx[0, 0] }, { 4.0 * dldx[0, 1], 0.0, 0.0, -1.0 * dldx[0, 1] }, }, { { 0.0, 4.0 * dldx[1, 0], 0.0, -1.0 * dldx[1, 0] }, { 0.0, 4.0 * dldx[1, 1], 0.0, -1.0 * dldx[1, 1] }, }, { { 0.0, 0.0, 4.0 * dldx[2, 0], -1.0 * dldx[2, 0] }, { 0.0, 0.0, 4.0 * dldx[2, 1], -1.0 * dldx[2, 1] }, }, { { 4.0 * dldx[1, 0], 4.0 * dldx[0, 0], 0.0, 0.0 }, { 4.0 * dldx[1, 1], 4.0 * dldx[0, 1], 0.0, 0.0 }, }, { { 0.0, 4.0 * dldx[2, 0], 4.0 * dldx[1, 0], 0.0 }, { 0.0, 4.0 * dldx[2, 1], 4.0 * dldx[1, 1], 0.0 }, }, { { 4.0 * dldx[2, 0], 0.0, 4.0 * dldx[0, 0], 0.0 }, { 4.0 * dldx[2, 1], 0.0, 4.0 * dldx[0, 1], 0.0 }, }, }; // ∫dN/dndN/dn dxdy // integralDNDX[n, ino, jno] n = 0 --> ∫dN/dxdN/dx dxdy // n = 1 --> ∫dN/dydN/dy dxdy double[, ,] integralDNDX = new double[ndim, nno, nno]; for (int n = 0; n < ndim; n++) { for (int ino = 0; ino < nno; ino++) { for (int jno = 0; jno < nno; jno++) { integralDNDX[n, ino, jno] = area / 6.0 * (dndxC[ino, n, 0] * dndxC[jno, n, 0] + dndxC[ino, n, 1] * dndxC[jno, n, 1] + dndxC[ino, n, 2] * dndxC[jno, n, 2]) + area / 12.0 * (dndxC[ino, n, 0] * dndxC[jno, n, 1] + dndxC[ino, n, 0] * dndxC[jno, n, 2] + dndxC[ino, n, 1] * dndxC[jno, n, 0] + dndxC[ino, n, 1] * dndxC[jno, n, 2] + dndxC[ino, n, 2] * dndxC[jno, n, 0] + dndxC[ino, n, 2] * dndxC[jno, n, 1]) + area / 3.0 * (dndxC[ino, n, 0] * dndxC[jno, n, 3] + dndxC[ino, n, 1] * dndxC[jno, n, 3] + dndxC[ino, n, 2] * dndxC[jno, n, 3] + dndxC[ino, n, 3] * dndxC[jno, n, 0] + dndxC[ino, n, 3] * dndxC[jno, n, 1] + dndxC[ino, n, 3] * dndxC[jno, n, 2]) + area * dndxC[ino, n, 3] * dndxC[jno, n, 3]; } } } // ∫(dNi/dx)Nj dxdy // ∫(dNi/dy)Nj dxdy double[, ,] integralDNDXL = new double[2, nno, nno]; for (int ino = 0; ino < nno; ino++) { for (int n = 0; n < ndim; n++) { integralDNDXL[n, ino, 0] = (area / 30.0) * dndxC[ino, n, 0] - (area / 60.0) * (dndxC[ino, n, 1] + dndxC[ino, n, 2]); integralDNDXL[n, ino, 1] = (area / 30.0) * dndxC[ino, n, 1] - (area / 60.0) * (dndxC[ino, n, 0] + dndxC[ino, n, 2]); integralDNDXL[n, ino, 2] = (area / 30.0) * dndxC[ino, n, 2] - (area / 60.0) * (dndxC[ino, n, 0] + dndxC[ino, n, 1]); integralDNDXL[n, ino, 3] = (area / 15.0) * (2.0 * dndxC[ino, n, 0] + 2.0 * dndxC[ino, n, 1] + dndxC[ino, n, 2] + 5.0 * dndxC[ino, n, 3]); integralDNDXL[n, ino, 4] = (area / 15.0) * (dndxC[ino, n, 0] + 2.0 * dndxC[ino, n, 1] + 2.0 * dndxC[ino, n, 2] + 5.0 * dndxC[ino, n, 3]); integralDNDXL[n, ino, 5] = (area / 15.0) * (2.0 * dndxC[ino, n, 0] + dndxC[ino, n, 1] + 2.0 * dndxC[ino, n, 2] + 5.0 * dndxC[ino, n, 3]); } } // ∫N N dxdy double[,] integralN = new double[nno, nno] { { 6.0 * area / 180.0, -1.0 * area / 180.0, -1.0 * area / 180.0, 0.0, -4.0 * area / 180.0, 0.0 }, { -1.0 * area / 180.0, 6.0 * area / 180.0, -1.0 * area / 180.0, 0.0, 0.0, -4.0 * area / 180.0 }, { -1.0 * area / 180.0, -1.0 * area / 180.0, 6.0 * area / 180.0, -4.0 * area / 180.0, 0.0, 0.0 }, { 0.0, 0.0, -4.0 * area / 180.0, 32.0 * area / 180.0, 16.0 * area / 180.0, 16.0 * area / 180.0 }, { -4.0 * area / 180.0, 0.0, 0.0, 16.0 * area / 180.0, 32.0 * area / 180.0, 16.0 * area / 180.0 }, { 0.0, -4.0 * area / 180.0, 0.0, 16.0 * area / 180.0, 16.0 * area / 180.0, 32.0 * area / 180.0 }, }; // 要素剛性行列を作る double[,] eKMat = new double[nno, nno]; double[,] eCMat = new double[nno, nno]; double[,] eMMat = new double[nno, nno]; for (int ino = 0; ino < nno; ino++) { for (int jno = 0; jno < nno; jno++) { eKMat[ino, jno] = -media_P[0, 0] * integralDNDX[1, ino, jno] - media_P[1, 1] * integralDNDX[0, ino, jno] + k0 * k0 * media_Q[2, 2] * integralN[ino, jno]; if (isSVEA) { if (isYDirectionPeriodic) { eCMat[ino, jno] = -media.P[1, 1] * (integralDNDXL[1, ino, jno] - integralDNDXL[1, jno, ino]); } else { eCMat[ino, jno] = -media.P[1, 1] * (integralDNDXL[0, ino, jno] - integralDNDXL[0, jno, ino]); } // 要素質量行列 eMMat[ino, jno] = media.P[1, 1] * integralN[ino, jno]; } } } // 要素剛性行列にマージする for (int ino = 0; ino < nno; ino++) { int iNodeNumber = no_c[ino]; if (ForceNodeNumberH.ContainsKey(iNodeNumber)) { continue; } int inoGlobal = toSortedPeriodic[iNodeNumber]; for (int jno = 0; jno < nno; jno++) { int jNodeNumber = no_c[jno]; if (ForceNodeNumberH.ContainsKey(jNodeNumber)) { continue; } int jnoGlobal = toSortedPeriodic[jNodeNumber]; KMat[inoGlobal + freeNodeCntPeriodic_0 * jnoGlobal] += eKMat[ino, jno]; if (isSVEA) { CMat[inoGlobal + freeNodeCntPeriodic_0 * jnoGlobal] += eCMat[ino, jno]; MMat[inoGlobal + freeNodeCntPeriodic_0 * jnoGlobal] += eMMat[ino, jno]; } } } }
/* 数値積分版 * /// <summary> * /// ヘルムホルツ方程式に対する有限要素マトリクス作成 * /// </summary> * /// <param name="waveLength">波長</param> * /// <param name="toSorted">ソートされた節点インデックス( 2D節点番号→ソート済みリストインデックスのマップ)</param> * /// <param name="element">有限要素</param> * /// <param name="Nodes">節点リスト</param> * /// <param name="Medias">媒質リスト</param> * /// <param name="ForceNodeNumberH">強制境界節点ハッシュ</param> * /// <param name="WaveModeDv">計算する波のモード区分</param> * /// <param name="mat">マージされる全体行列</param> * public static void AddElementMat(double waveLength, * Dictionary<int, int> toSorted, * FemElement element, * IList<FemNode> Nodes, * MediaInfo[] Medias, * Dictionary<int, bool> ForceNodeNumberH, * FemSolver.WaveModeDv WaveModeDv, * ref MyComplexMatrix mat) * { * // 定数 * const double pi = Constants.pi; * const double c0 = Constants.c0; * // 波数 * double k0 = 2.0 * pi / waveLength; * // 角周波数 * double omega = k0 * c0; * * // 要素頂点数 * const int vertexCnt = Constants.QuadVertexCnt; //4; * // 要素内節点数 * const int nno = Constants.QuadNodeCnt_SecondOrder_Type2; //8; // 2次セレンディピティ * // 座標次元数 * const int ndim = Constants.CoordDim2D; //2; * * int[] nodeNumbers = element.NodeNumbers; * int[] no_c = new int[nno]; * MediaInfo media = Medias[element.MediaIndex]; * double[,] media_P = null; * double[,] media_Q = null; * if (WaveModeDv == FemSolver.WaveModeDv.TE) * { * media_P = media.P; * media_Q = media.Q; * } * else if (WaveModeDv == FemSolver.WaveModeDv.TM) * { * media_P = media.Q; * media_Q = media.P; * } * else * { * System.Diagnostics.Debug.Assert(false); * } * // [p]は逆数をとる * media_P = MyMatrixUtil.matrix_Inverse(media_P); * * // 節点座標(IFの都合上配列の配列形式の2次元配列を作成) * double[][] pp = new double[nno][]; * for (int ino = 0; ino < nno; ino++) * { * int nodeNumber = nodeNumbers[ino]; * int nodeIndex = nodeNumber - 1; * FemNode node = Nodes[nodeIndex]; * * no_c[ino] = nodeNumber; * pp[ino] = new double[ndim]; * for (int n = 0; n < ndim; n++) * { * pp[ino][n] = node.Coord[n]; * } * } * * //// 四角形の辺の長さを求める * //double[] le = new double[4]; * //le[0] = FemMeshLogic.GetDistance(pp[0], pp[1]); * //le[1] = FemMeshLogic.GetDistance(pp[1], pp[2]); * //le[2] = FemMeshLogic.GetDistance(pp[2], pp[3]); * //le[3] = FemMeshLogic.GetDistance(pp[3], pp[0]); * * // 要素節点座標( 局所r,s成分 ) * // s * // | * // 3+ 6 +2 * // | | | * // ---7---+---5-->r * // | | | * // 0+ 4 +1 * // | * // * double[][] n_pts = * { * // r, s * new double[] {-1.0, -1.0}, //0 * new double[] { 1.0, -1.0}, //1 * new double[] { 1.0, 1.0}, //2 * new double[] {-1.0, 1.0}, //3 * new double[] { 0, -1.0}, //4 * new double[] { 1.0, 0}, //5 * new double[] { 0, 1.0}, //6 * new double[] {-1.0, 0}, //7 * }; * * * // ガウスルジャンドルの積分公式 * double[][] g_pts = new double[5][] * { * // ポイント(ξ: [-1 +1]区間)、重み * new double[] { -0.90617985, 0.23692689}, * new double[] { -0.53846931, 0.47862867}, * new double[] {0.0, 0.56888889}, * new double[] {0.53846931, 0.47862867}, * new double[] {0.90617985, 0.23692689} * }; * * // 要素剛性行列を作る * double[,] emat = new Complex[nno, nno]; * for (int ino = 0; ino < nno; ino++) * { * for (int jno = 0; jno < nno; jno++) * { * emat[ino, jno] = 0.0; * double detjsum = 0; //check * foreach (double[] s_g_pt in g_pts) * { * foreach (double[] r_g_pt in g_pts) * { * // 積分点 * double r = r_g_pt[0]; * double s = s_g_pt[0]; * // 重み(2次元) * double weight = r_g_pt[1] * s_g_pt[1]; * // 形状関数 * double[] N = new double[nno]; * // 形状関数のr, s方向微分 * double[] dNdr = new double[nno]; * double[] dNds = new double[nno]; * // 節点0~3 : 四角形の頂点 * for (int i = 0; i < 4; i++) * { * // 節点の局所座標 * double ri = n_pts[i][0]; * double si = n_pts[i][1]; * // 形状関数N * N[i] = 0.25 * (1.0 + ri * r) * (1.0 + si * s) * (ri* r + si * s - 1.0); * // 形状関数のr方向微分 * dNdr[i] = 0.25 * ri * (1.0 + si * s) * (2.0 * ri * r + si * s); * // 形状関数のs方向微分 * dNds[i] = 0.25 * si * (1.0 + ri * r) * (ri * r + 2.0 * si * s); * } * // 節点4,6 : r方向辺上中点 * foreach (int i in new int[]{ 4, 6}) * { * // 節点の局所座標 * double ri = n_pts[i][0]; * double si = n_pts[i][1]; * // 形状関数N * N[i] = 0.5 * (1.0 - r * r) * (1.0 + si * s); * // 形状関数のr方向微分 * dNdr[i] = -1.0 * r * (1.0 + si * s); * // 形状関数のs方向微分 * dNds[i] = 0.5 * si * (1.0 - r * r); * } * // 節点5,7 : s方向辺上中点 * foreach (int i in new int[] { 5, 7 }) * { * // 節点の局所座標 * double ri = n_pts[i][0]; * double si = n_pts[i][1]; * // 形状関数N * N[i] = 0.5 * (1.0 + ri * r) * (1.0 - s * s); * // 形状関数のr方向微分 * dNdr[i] = 0.5 * ri * (1.0 - s * s); * // 形状関数のs方向微分 * dNds[i] = -1.0 * s * (1.0 + ri * r); * } * * // ヤコビアン行列 * double j11; * double j12; * double j21; * double j22; * j11 = 0; * j12 = 0; * j21 = 0; * j22 = 0; * * //for (int i = 0; i < vertexCnt; i++) * //{ * // // 頂点の座標の微分 * // // 座標の形状関数は一次四角形のものを使用する * // // 節点の局所座標 * // double ri = n_pts[i][0]; * // double si = n_pts[i][1]; * // double dNdr_1stOrder = 0.25 * ri * (1.0 + si * s); * // double dNds_1stOrder = 0.25 * (1.0 + ri * r) * si; * // j11 += dNdr_1stOrder * pp[i][0]; * // j12 += dNdr_1stOrder * pp[i][1]; * // j21 += dNds_1stOrder * pp[i][0]; * // j22 += dNds_1stOrder * pp[i][1]; * //} * * for (int i = 0; i < nno; i++) * { * j11 += dNdr[i] * pp[i][0]; * j12 += dNdr[i] * pp[i][1]; * j21 += dNds[i] * pp[i][0]; * j22 += dNds[i] * pp[i][1]; * } * // ヤコビアン * double detj = j11 * j22 - j12 * j21; * detjsum += detj * weight; * //System.Diagnostics.Debug.WriteLine("det:{0}", detj); * * // gradr[0] : gradrのx成分 grad[1] : gradrのy成分 * // grads[0] : gradsのx成分 grads[1] : gradsのy成分 * double[] gradr = new double[2]; * double[] grads = new double[2]; * gradr[0] = j22 / detj; * gradr[1] = - j21 / detj; * grads[0] = - j12 / detj; * grads[1] = j11 / detj; * * // 形状関数のx, y方向微分 * double[,] dNdX = new double[ndim, nno]; * for (int i = 0; i < nno; i++) * { * for (int direction = 0; direction < ndim; direction++) * { * dNdX[direction, i] = dNdr[i] * gradr[direction] + dNds[i] * grads[direction]; * } * } * * // 汎関数 * double functional = media_P[0, 0] * dNdX[1, ino] * dNdX[1, jno] + media_P[1, 1] * dNdX[0, ino] * dNdX[0, jno] * - k0 * k0 * media_Q[2, 2] * N[ino] * N[jno]; * emat[ino, jno] += detj * weight * functional; * } * } * //System.Diagnostics.Debug.WriteLine("detsum: {0}", detjsum); * } * } * * // 要素剛性行列にマージする * for (int ino = 0; ino < nno; ino++) * { * int iNodeNumber = no_c[ino]; * if (ForceNodeNumberH.ContainsKey(iNodeNumber)) continue; * int inoGlobal = toSorted[iNodeNumber]; * for (int jno = 0; jno < nno; jno++) * { * int jNodeNumber = no_c[jno]; * if (ForceNodeNumberH.ContainsKey(jNodeNumber)) continue; * int jnoGlobal = toSorted[jNodeNumber]; * * mat[inoGlobal, jnoGlobal] += emat[ino, jno]; * } * } * } */ /// <summary> /// ヘルムホルツ方程式に対する有限要素マトリクス作成 /// </summary> /// <param name="waveLength">波長</param> /// <param name="toSorted">ソートされた節点インデックス( 2D節点番号→ソート済みリストインデックスのマップ)</param> /// <param name="element">有限要素</param> /// <param name="Nodes">節点リスト</param> /// <param name="Medias">媒質リスト</param> /// <param name="ForceNodeNumberH">強制境界節点ハッシュ</param> /// <param name="WGStructureDv">導波路構造区分</param> /// <param name="WaveModeDv">計算する波のモード区分</param> /// <param name="waveguideWidthForEPlane">導波路幅(E面解析用)</param> /// <param name="mat">マージされる全体行列</param> public static void AddElementMat(double waveLength, Dictionary <int, int> toSorted, FemElement element, IList <FemNode> Nodes, MediaInfo[] Medias, Dictionary <int, bool> ForceNodeNumberH, FemSolver.WGStructureDV WGStructureDv, FemSolver.WaveModeDV WaveModeDv, double waveguideWidthForEPlane, ref MyComplexMatrix mat) { // 定数 const double pi = Constants.pi; const double c0 = Constants.c0; // 波数 double k0 = 2.0 * pi / waveLength; // 角周波数 double omega = k0 * c0; // 要素頂点数 //const int vertexCnt = Constants.QuadVertexCnt; //4; // 要素内節点数 const int nno = Constants.QuadNodeCnt_SecondOrder_Type2; //8; // 2次セレンディピティ // 座標次元数 const int ndim = Constants.CoordDim2D; //2; int[] nodeNumbers = element.NodeNumbers; int[] no_c = new int[nno]; MediaInfo media = Medias[element.MediaIndex]; double[,] media_P = null; double[,] media_Q = null; // ヘルムホルツ方程式のパラメータP,Qを取得する FemSolver.GetHelmholtzMediaPQ( k0, media, WGStructureDv, WaveModeDv, waveguideWidthForEPlane, out media_P, out media_Q); // 節点座標(IFの都合上配列の配列形式の2次元配列を作成) double[][] pp = new double[nno][]; for (int ino = 0; ino < nno; ino++) { int nodeNumber = nodeNumbers[ino]; int nodeIndex = nodeNumber - 1; FemNode node = Nodes[nodeIndex]; no_c[ino] = nodeNumber; pp[ino] = new double[ndim]; for (int n = 0; n < ndim; n++) { pp[ino][n] = node.Coord[n]; } } // 四角形の辺の長さを求める double[] le = new double[4]; le[0] = FemMeshLogic.GetDistance(pp[0], pp[1]); le[1] = FemMeshLogic.GetDistance(pp[1], pp[2]); le[2] = FemMeshLogic.GetDistance(pp[2], pp[3]); le[3] = FemMeshLogic.GetDistance(pp[3], pp[0]); System.Diagnostics.Debug.Assert(Math.Abs(le[0] - le[2]) < Constants.PrecisionLowerLimit); System.Diagnostics.Debug.Assert(Math.Abs(le[1] - le[3]) < Constants.PrecisionLowerLimit); double lx = le[0]; double ly = le[1]; // 要素節点座標( 局所r,s成分 ) // s // | // 3+ 6 +2 // | | | // ---7---+---5-->r // | | | // 0+ 4 +1 // | // double[][] n_pts = { // r, s new double[] { -1.0, -1.0 }, //0 new double[] { 1.0, -1.0 }, //1 new double[] { 1.0, 1.0 }, //2 new double[] { -1.0, 1.0 }, //3 new double[] { 0, -1.0 }, //4 new double[] { 1.0, 0 }, //5 new double[] { 0, 1.0 }, //6 new double[] { -1.0, 0 }, //7 }; // Ni = a0(r^2*s) + a1(r^2) + a2(r) + a3(rs) + a4(rs^2) + a5(s^2) + a6(s) + a7 double[,] Ni_a = new double[nno, 8]; for (int i = 0; i < 4; i++) { // 節点の局所座標 double ri = n_pts[i][0]; double si = n_pts[i][1]; Ni_a[i, 0] = 0.25 * ri * ri * si; Ni_a[i, 1] = 0.25 * ri * ri; Ni_a[i, 2] = 0.0; Ni_a[i, 3] = 0.25 * ri * si; Ni_a[i, 4] = 0.25 * ri * si * si; Ni_a[i, 5] = 0.25 * si * si; Ni_a[i, 6] = 0.0; Ni_a[i, 7] = -0.25; } foreach (int i in new int[] { 4, 6 }) { // 節点の局所座標 double ri = n_pts[i][0]; double si = n_pts[i][1]; Ni_a[i, 0] = -0.5 * si; Ni_a[i, 1] = -0.5; Ni_a[i, 2] = 0.0; Ni_a[i, 3] = 0.0; Ni_a[i, 4] = 0.0; Ni_a[i, 5] = 0.0; Ni_a[i, 6] = 0.5 * si; Ni_a[i, 7] = 0.5; } foreach (int i in new int[] { 5, 7 }) { // 節点の局所座標 double ri = n_pts[i][0]; double si = n_pts[i][1]; Ni_a[i, 0] = 0.0; Ni_a[i, 1] = 0.0; Ni_a[i, 2] = 0.5 * ri; Ni_a[i, 3] = 0.0; Ni_a[i, 4] = -0.5 * ri; Ni_a[i, 5] = -0.5; Ni_a[i, 6] = 0.0; Ni_a[i, 7] = 0.5; } // dNidr = a0(r^2*s) + a1(r^2) + a2(r) + a3(rs) + a4(rs^2) + a5(s^2) + a6(s) + a7 double[,] dNidr_a = new double[nno, 8]; for (int i = 0; i < 4; i++) { // 節点の局所座標 double ri = n_pts[i][0]; double si = n_pts[i][1]; dNidr_a[i, 0] = 0.0; dNidr_a[i, 1] = 0.0; // r^2 dNidr_a[i, 2] = 0.25 * 2.0 * ri * ri; // r dNidr_a[i, 3] = 0.25 * 2.0 * ri * ri * si; // rs dNidr_a[i, 4] = 0.0; dNidr_a[i, 5] = 0.25 * ri * si * si; // s^2 dNidr_a[i, 6] = 0.25 * ri * si; // s dNidr_a[i, 7] = 0.0; //1 } foreach (int i in new int[] { 4, 6 }) { // 節点の局所座標 double ri = n_pts[i][0]; double si = n_pts[i][1]; dNidr_a[i, 0] = 0.0; dNidr_a[i, 1] = 0.0; // r^2 dNidr_a[i, 2] = -1.0; // r dNidr_a[i, 3] = -si; // rs dNidr_a[i, 4] = 0.0; dNidr_a[i, 5] = 0.0; // s^2 dNidr_a[i, 6] = 0.0; // s dNidr_a[i, 7] = 0.0; // 1 } foreach (int i in new int[] { 5, 7 }) { // 節点の局所座標 double ri = n_pts[i][0]; double si = n_pts[i][1]; dNidr_a[i, 0] = 0.0; dNidr_a[i, 1] = 0.0; // r^2 dNidr_a[i, 2] = 0.0; // r dNidr_a[i, 3] = 0.0; // rs dNidr_a[i, 4] = 0.0; dNidr_a[i, 5] = -0.5 * ri; // s^2 dNidr_a[i, 6] = 0.0; // s dNidr_a[i, 7] = 0.5 * ri; // 1 } // dNids = a0(r^2*s) + a1(r^2) + a2(r) + a3(rs) + a4(rs^2) + a5(s^2) + a6(s) + a7 double[,] dNids_a = new double[nno, 8]; for (int i = 0; i < 4; i++) { // 節点の局所座標 double ri = n_pts[i][0]; double si = n_pts[i][1]; dNids_a[i, 0] = 0.0; dNids_a[i, 1] = 0.25 * ri * ri * si; // r^2 dNids_a[i, 2] = 0.25 * ri * si; // r dNids_a[i, 3] = 0.25 * 2.0 * ri * si * si; // rs dNids_a[i, 4] = 0.0; dNids_a[i, 5] = 0.0; // s^2 dNids_a[i, 6] = 0.25 * 2.0 * si * si; // s dNids_a[i, 7] = 0.0; //1 } foreach (int i in new int[] { 4, 6 }) { // 節点の局所座標 double ri = n_pts[i][0]; double si = n_pts[i][1]; dNids_a[i, 0] = 0.0; dNids_a[i, 1] = -0.5 * si; // r^2 dNids_a[i, 2] = 0.0; // r dNids_a[i, 3] = 0.0; // rs dNids_a[i, 4] = 0.0; dNids_a[i, 5] = 0.0; // s^2 dNids_a[i, 6] = 0.0; // s dNids_a[i, 7] = 0.5 * si; //1 } foreach (int i in new int[] { 5, 7 }) { // 節点の局所座標 double ri = n_pts[i][0]; double si = n_pts[i][1]; dNids_a[i, 0] = 0.0; dNids_a[i, 1] = 0.0; // r^2 dNids_a[i, 2] = 0.0; // r dNids_a[i, 3] = -ri; // rs dNids_a[i, 4] = 0.0; dNids_a[i, 5] = 0.0; // s^2 dNids_a[i, 6] = -1.0; // s dNids_a[i, 7] = 0.0; //1 } // ∫dN/dndN/dn dxdy // integralDNDX[n, ino, jno] n = 0 --> ∫dN/dxdN/dx dxdy // n = 1 --> ∫dN/dydN/dy dxdy double[, ,] integralDNDX = new double[ndim, nno, nno]; // ∫N N dxdy double[,] integralN = new double[nno, nno]; for (int ino = 0; ino < nno; ino++) { for (int jno = 0; jno < nno; jno++) { integralN[ino, jno] = lx * ly / 4.0 * ( // r^4s^2 4.0 / 15.0 * Ni_a[ino, 0] * Ni_a[jno, 0] // r^2s^2 + 4.0 / 9.0 * (Ni_a[ino, 6] * Ni_a[jno, 0] + Ni_a[ino, 5] * Ni_a[jno, 1] + Ni_a[ino, 4] * Ni_a[jno, 2] + Ni_a[ino, 3] * Ni_a[jno, 3] + Ni_a[ino, 2] * Ni_a[jno, 4] + Ni_a[ino, 1] * Ni_a[jno, 5] + Ni_a[ino, 0] * Ni_a[jno, 6]) // r^4 + 4.0 / 5.0 * Ni_a[ino, 1] * Ni_a[jno, 1] // r^2 + 4.0 / 3.0 * (Ni_a[ino, 7] * Ni_a[jno, 1] + Ni_a[ino, 2] * Ni_a[jno, 2] + Ni_a[ino, 1] * Ni_a[jno, 7]) // r^2s^4 + 4.0 / 15.0 * Ni_a[ino, 4] * Ni_a[jno, 4] // s^4 + 4.0 / 5.0 * Ni_a[ino, 5] * Ni_a[jno, 5] // s^2 + 4.0 / 3.0 * (Ni_a[ino, 7] * Ni_a[jno, 5] + Ni_a[ino, 6] * Ni_a[jno, 6] + Ni_a[ino, 5] * Ni_a[jno, 7]) // 1 + 4.0 * Ni_a[ino, 7] * Ni_a[jno, 7] ); integralDNDX[0, ino, jno] = ly / lx * ( // r^4s^2 4.0 / 15.0 * dNidr_a[ino, 0] * dNidr_a[jno, 0] // r^2s^2 + 4.0 / 9.0 * (dNidr_a[ino, 6] * dNidr_a[jno, 0] + dNidr_a[ino, 5] * dNidr_a[jno, 1] + dNidr_a[ino, 4] * dNidr_a[jno, 2] + dNidr_a[ino, 3] * dNidr_a[jno, 3] + dNidr_a[ino, 2] * dNidr_a[jno, 4] + dNidr_a[ino, 1] * dNidr_a[jno, 5] + dNidr_a[ino, 0] * dNidr_a[jno, 6]) // r^4 + 4.0 / 5.0 * dNidr_a[ino, 1] * dNidr_a[jno, 1] // r^2 + 4.0 / 3.0 * (dNidr_a[ino, 7] * dNidr_a[jno, 1] + dNidr_a[ino, 2] * dNidr_a[jno, 2] + dNidr_a[ino, 1] * dNidr_a[jno, 7]) // r^2s^4 + 4.0 / 15.0 * dNidr_a[ino, 4] * dNidr_a[jno, 4] // s^4 + 4.0 / 5.0 * dNidr_a[ino, 5] * dNidr_a[jno, 5] // s^2 + 4.0 / 3.0 * (dNidr_a[ino, 7] * dNidr_a[jno, 5] + dNidr_a[ino, 6] * dNidr_a[jno, 6] + dNidr_a[ino, 5] * dNidr_a[jno, 7]) // 1 + 4.0 * dNidr_a[ino, 7] * dNidr_a[jno, 7] ); integralDNDX[1, ino, jno] = lx / ly * ( // r^4s^2 4.0 / 15.0 * dNids_a[ino, 0] * dNids_a[jno, 0] // r^2s^2 + 4.0 / 9.0 * (dNids_a[ino, 6] * dNids_a[jno, 0] + dNids_a[ino, 5] * dNids_a[jno, 1] + dNids_a[ino, 4] * dNids_a[jno, 2] + dNids_a[ino, 3] * dNids_a[jno, 3] + dNids_a[ino, 2] * dNids_a[jno, 4] + dNids_a[ino, 1] * dNids_a[jno, 5] + dNids_a[ino, 0] * dNids_a[jno, 6]) // r^4 + 4.0 / 5.0 * dNids_a[ino, 1] * dNids_a[jno, 1] // r^2 + 4.0 / 3.0 * (dNids_a[ino, 7] * dNids_a[jno, 1] + dNids_a[ino, 2] * dNids_a[jno, 2] + dNids_a[ino, 1] * dNids_a[jno, 7]) // r^2s^4 + 4.0 / 15.0 * dNids_a[ino, 4] * dNids_a[jno, 4] // s^4 + 4.0 / 5.0 * dNids_a[ino, 5] * dNids_a[jno, 5] // s^2 + 4.0 / 3.0 * (dNids_a[ino, 7] * dNids_a[jno, 5] + dNids_a[ino, 6] * dNids_a[jno, 6] + dNids_a[ino, 5] * dNids_a[jno, 7]) // 1 + 4.0 * dNids_a[ino, 7] * dNids_a[jno, 7] ); } } // 要素剛性行列を作る double[,] emat = new double[nno, nno]; for (int ino = 0; ino < nno; ino++) { for (int jno = 0; jno < nno; jno++) { emat[ino, jno] = media_P[0, 0] * integralDNDX[1, ino, jno] + media_P[1, 1] * integralDNDX[0, ino, jno] - k0 * k0 * media_Q[2, 2] * integralN[ino, jno]; } } // 要素剛性行列にマージする for (int ino = 0; ino < nno; ino++) { int iNodeNumber = no_c[ino]; if (ForceNodeNumberH.ContainsKey(iNodeNumber)) { continue; } int inoGlobal = toSorted[iNodeNumber]; for (int jno = 0; jno < nno; jno++) { int jNodeNumber = no_c[jno]; if (ForceNodeNumberH.ContainsKey(jNodeNumber)) { continue; } int jnoGlobal = toSorted[jNodeNumber]; //mat[inoGlobal, jnoGlobal] += emat[ino, jno]; //mat._body[inoGlobal + jnoGlobal * mat.RowSize] += emat[ino, jno]; // 実数部に加算する //mat._body[inoGlobal + jnoGlobal * mat.RowSize].Real += emat[ino, jno]; // バンドマトリクス対応 mat._body[mat.GetBufferIndex(inoGlobal, jnoGlobal)].Real += emat[ino, jno]; } } }
/// <summary> /// ヘルムホルツ方程式に対する有限要素マトリクス作成 /// </summary> /// <param name="waveLength">波長</param> /// <param name="toSorted">ソートされた節点インデックス( 2D節点番号→ソート済みリストインデックスのマップ)</param> /// <param name="element">有限要素</param> /// <param name="Nodes">節点リスト</param> /// <param name="Medias">媒質リスト</param> /// <param name="ForceNodeNumberH">強制境界節点ハッシュ</param> /// <param name="WGStructureDv">導波路構造区分</param> /// <param name="WaveModeDv">計算する波のモード区分</param> /// <param name="waveguideWidthForEPlane">導波路幅(E面解析用)</param> /// <param name="mat">マージされる全体行列</param> public static void AddElementMat(double waveLength, Dictionary <int, int> toSorted, FemElement element, IList <FemNode> Nodes, MediaInfo[] Medias, Dictionary <int, bool> ForceNodeNumberH, FemSolver.WGStructureDV WGStructureDv, FemSolver.WaveModeDV WaveModeDv, double waveguideWidthForEPlane, ref MyComplexMatrix mat) { // 定数 const double pi = Constants.pi; const double c0 = Constants.c0; // 波数 double k0 = 2.0 * pi / waveLength; // 角周波数 double omega = k0 * c0; // 要素頂点数 const int vertexCnt = Constants.TriVertexCnt; //3; // 要素内節点数 const int nno = Constants.TriNodeCnt_SecondOrder; //6; // 2次三角形要素 // 座標次元数 const int ndim = Constants.CoordDim2D; //2; int[] nodeNumbers = element.NodeNumbers; int[] no_c = new int[nno]; MediaInfo media = Medias[element.MediaIndex]; // ver1.1.0.0 媒質情報の取得 double[,] media_P = null; double[,] media_Q = null; // ヘルムホルツ方程式のパラメータP,Qを取得する FemSolver.GetHelmholtzMediaPQ( k0, media, WGStructureDv, WaveModeDv, waveguideWidthForEPlane, out media_P, out media_Q); // 節点座標(IFの都合上配列の配列形式の2次元配列を作成) double[][] pp = new double[nno][]; for (int ino = 0; ino < nno; ino++) { int nodeNumber = nodeNumbers[ino]; int nodeIndex = nodeNumber - 1; FemNode node = Nodes[nodeIndex]; no_c[ino] = nodeNumber; pp[ino] = new double[ndim]; for (int n = 0; n < ndim; n++) { pp[ino][n] = node.Coord[n]; } } // 面積を求める double area = KerEMatTri.TriArea(pp[0], pp[1], pp[2]); //System.Diagnostics.Debug.WriteLine("Elem No {0} area: {1}", element.No, area); System.Diagnostics.Debug.Assert(area >= 0.0); // 面積座標の微分を求める // dldx[k, n] k面積座標Lkのn方向微分 double[,] dldx = null; double[] const_term = null; KerEMatTri.TriDlDx(out dldx, out const_term, pp[0], pp[1], pp[2]); // 形状関数の微分の係数を求める // dndxC[ino,n,k] ino節点のn方向微分のLk(k面積座標)の係数 // dNino/dn = dndxC[ino, n, 0] * L0 + dndxC[ino, n, 1] * L1 + dndxC[ino, n, 2] * L2 + dndxC[ino, n, 3] double[, ,] dndxC = new double[nno, ndim, vertexCnt + 1] { { { 4.0 * dldx[0, 0], 0.0, 0.0, -1.0 * dldx[0, 0] }, { 4.0 * dldx[0, 1], 0.0, 0.0, -1.0 * dldx[0, 1] }, }, { { 0.0, 4.0 * dldx[1, 0], 0.0, -1.0 * dldx[1, 0] }, { 0.0, 4.0 * dldx[1, 1], 0.0, -1.0 * dldx[1, 1] }, }, { { 0.0, 0.0, 4.0 * dldx[2, 0], -1.0 * dldx[2, 0] }, { 0.0, 0.0, 4.0 * dldx[2, 1], -1.0 * dldx[2, 1] }, }, { { 4.0 * dldx[1, 0], 4.0 * dldx[0, 0], 0.0, 0.0 }, { 4.0 * dldx[1, 1], 4.0 * dldx[0, 1], 0.0, 0.0 }, }, { { 0.0, 4.0 * dldx[2, 0], 4.0 * dldx[1, 0], 0.0 }, { 0.0, 4.0 * dldx[2, 1], 4.0 * dldx[1, 1], 0.0 }, }, { { 4.0 * dldx[2, 0], 0.0, 4.0 * dldx[0, 0], 0.0 }, { 4.0 * dldx[2, 1], 0.0, 4.0 * dldx[0, 1], 0.0 }, }, }; // ∫dN/dndN/dn dxdy // integralDNDX[n, ino, jno] n = 0 --> ∫dN/dxdN/dx dxdy // n = 1 --> ∫dN/dydN/dy dxdy double[, ,] integralDNDX = new double[ndim, nno, nno]; for (int n = 0; n < ndim; n++) { for (int ino = 0; ino < nno; ino++) { for (int jno = 0; jno < nno; jno++) { integralDNDX[n, ino, jno] = area / 6.0 * (dndxC[ino, n, 0] * dndxC[jno, n, 0] + dndxC[ino, n, 1] * dndxC[jno, n, 1] + dndxC[ino, n, 2] * dndxC[jno, n, 2]) + area / 12.0 * (dndxC[ino, n, 0] * dndxC[jno, n, 1] + dndxC[ino, n, 0] * dndxC[jno, n, 2] + dndxC[ino, n, 1] * dndxC[jno, n, 0] + dndxC[ino, n, 1] * dndxC[jno, n, 2] + dndxC[ino, n, 2] * dndxC[jno, n, 0] + dndxC[ino, n, 2] * dndxC[jno, n, 1]) + area / 3.0 * (dndxC[ino, n, 0] * dndxC[jno, n, 3] + dndxC[ino, n, 1] * dndxC[jno, n, 3] + dndxC[ino, n, 2] * dndxC[jno, n, 3] + dndxC[ino, n, 3] * dndxC[jno, n, 0] + dndxC[ino, n, 3] * dndxC[jno, n, 1] + dndxC[ino, n, 3] * dndxC[jno, n, 2]) + area * dndxC[ino, n, 3] * dndxC[jno, n, 3]; } } } // ∫N N dxdy double[,] integralN = new double[nno, nno] { { 6.0 * area / 180.0, -1.0 * area / 180.0, -1.0 * area / 180.0, 0.0, -4.0 * area / 180.0, 0.0 }, { -1.0 * area / 180.0, 6.0 * area / 180.0, -1.0 * area / 180.0, 0.0, 0.0, -4.0 * area / 180.0 }, { -1.0 * area / 180.0, -1.0 * area / 180.0, 6.0 * area / 180.0, -4.0 * area / 180.0, 0.0, 0.0 }, { 0.0, 0.0, -4.0 * area / 180.0, 32.0 * area / 180.0, 16.0 * area / 180.0, 16.0 * area / 180.0 }, { -4.0 * area / 180.0, 0.0, 0.0, 16.0 * area / 180.0, 32.0 * area / 180.0, 16.0 * area / 180.0 }, { 0.0, -4.0 * area / 180.0, 0.0, 16.0 * area / 180.0, 16.0 * area / 180.0, 32.0 * area / 180.0 }, }; // 要素剛性行列を作る double[,] emat = new double[nno, nno]; for (int ino = 0; ino < nno; ino++) { for (int jno = 0; jno < nno; jno++) { emat[ino, jno] = media_P[0, 0] * integralDNDX[1, ino, jno] + media_P[1, 1] * integralDNDX[0, ino, jno] - k0 * k0 * media_Q[2, 2] * integralN[ino, jno]; } } // 要素剛性行列にマージする for (int ino = 0; ino < nno; ino++) { int iNodeNumber = no_c[ino]; if (ForceNodeNumberH.ContainsKey(iNodeNumber)) { continue; } int inoGlobal = toSorted[iNodeNumber]; for (int jno = 0; jno < nno; jno++) { int jNodeNumber = no_c[jno]; if (ForceNodeNumberH.ContainsKey(jNodeNumber)) { continue; } int jnoGlobal = toSorted[jNodeNumber]; //mat[inoGlobal, jnoGlobal] += emat[ino, jno]; //mat._body[inoGlobal + jnoGlobal * mat.RowSize] += emat[ino, jno]; // 実数部に加算する //mat._body[inoGlobal + jnoGlobal * mat.RowSize].Real += emat[ino, jno]; // バンドマトリクス対応 mat._body[mat.GetBufferIndex(inoGlobal, jnoGlobal)].Real += emat[ino, jno]; } } }
/// <summary> /// Fem入力データをファイルから読み込み /// </summary> /// <param name="filename">ファイル名(*.fem)</param> /// <param name="nodes">節点リスト</param> /// <param name="elements">要素リスト</param> /// <param name="ports">ポートの節点番号リストのリスト</param> /// <param name="forceBCNodes">強制境界節点番号リスト</param> /// <param name="incidentPortNo">入射ポート番号</param> /// <param name="medias">媒質情報リスト</param> /// <param name="firstWaveLength">計算開始波長</param> /// <param name="lastWaveLength">計算終了波長</param> /// <param name="calcCnt">計算件数</param> /// <param name="wgStructureDv">導波路構造区分</param> /// <param name="waveModeDv">波のモード区分</param> /// <returns></returns> public static bool LoadFromFile( string filename, out IList <FemNode> nodes, out IList <FemElement> elements, out IList <IList <int> > ports, out IList <int> forceBCNodes, out IList <IList <uint> > elemNoPeriodicList, out IList <IList <IList <int> > > nodePeriodicBList, out IList <IList <int> > defectNodePeriodicList, out int incidentPortNo, out MediaInfo[] medias, out double firstWaveLength, out double lastWaveLength, out int calcCnt, out FemSolver.WaveModeDV waveModeDv ) { int eNodeCnt = 0; nodes = new List <FemNode>(); elements = new List <FemElement>(); ports = new List <IList <int> >(); forceBCNodes = new List <int>(); elemNoPeriodicList = new List <IList <uint> >(); nodePeriodicBList = new List <IList <IList <int> > >(); defectNodePeriodicList = new List <IList <int> >(); incidentPortNo = 1; medias = new MediaInfo[Constants.MaxMediaCount]; Color[] workColorList = { CadLogic.VacumnBackColor, CadLogic.RodBackColor }; for (int i = 0; i < medias.Length; i++) { MediaInfo media = new MediaInfo(); media.BackColor = workColorList[i]; medias[i] = media; } firstWaveLength = 0.0; lastWaveLength = 0.0; calcCnt = 0; waveModeDv = Constants.DefWaveModeDv; if (!File.Exists(filename)) { return(false); } // 入力データ読み込み try { using (StreamReader sr = new StreamReader(filename)) { const char delimiter = ','; string line; string[] tokens; line = sr.ReadLine(); tokens = line.Split(delimiter); if (tokens.Length != 2 || tokens[0] != "Nodes") { MessageBox.Show("節点情報がありません", "", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } int nodeCnt = int.Parse(tokens[1]); for (int i = 0; i < nodeCnt; i++) { line = sr.ReadLine(); tokens = line.Split(delimiter); if (tokens.Length != 3) { MessageBox.Show("節点情報が不正です", "", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } int no = int.Parse(tokens[0]); if (no != i + 1) { MessageBox.Show("節点番号が不正です", "", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } FemNode femNode = new FemNode(); femNode.No = no; femNode.Coord = new double[2]; femNode.Coord[0] = double.Parse(tokens[1]); femNode.Coord[1] = double.Parse(tokens[2]); nodes.Add(femNode); } line = sr.ReadLine(); tokens = line.Split(delimiter); if (tokens.Length != 2 || tokens[0] != "Elements") { MessageBox.Show("要素情報がありません", "", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } int elementCnt = int.Parse(tokens[1]); for (int i = 0; i < elementCnt; i++) { line = sr.ReadLine(); tokens = line.Split(delimiter); if ((tokens.Length != 1 + Constants.TriNodeCnt_SecondOrder) && (tokens.Length != 2 + Constants.TriNodeCnt_SecondOrder) && // ver1.1.0.0で媒質インデックスを番号の後に挿入 (tokens.Length != 2 + Constants.TriNodeCnt_FirstOrder) ) { MessageBox.Show("要素情報が不正です", "", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } int elemNo = int.Parse(tokens[0]); int mediaIndex = 0; int indexOffset = 1; // ver1.0.0.0 int workENodeCnt = Constants.TriNodeCnt_SecondOrder; if (tokens.Length == 1 + Constants.TriNodeCnt_SecondOrder) { // 媒質インデックスのない古い形式(ver1.0.0.0) } else { // ver1.1.0.0で媒質インデックスを追加 mediaIndex = int.Parse(tokens[1]); indexOffset = 2; workENodeCnt = tokens.Length - 2; } if (workENodeCnt <= 0) { MessageBox.Show("要素節点数が不正です", "", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } if (eNodeCnt == 0) { // 最初の要素の節点数を格納(チェックに利用) eNodeCnt = workENodeCnt; } else { // 要素の節点数が変わった? if (workENodeCnt != eNodeCnt) { MessageBox.Show("要素節点数が不正です", "", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } } //FemElement femElement = new FemElement(); FemElement femElement = FemMeshLogic.CreateFemElementByElementNodeCnt(eNodeCnt); femElement.No = elemNo; femElement.MediaIndex = mediaIndex; femElement.NodeNumbers = new int[eNodeCnt]; for (int n = 0; n < femElement.NodeNumbers.Length; n++) { femElement.NodeNumbers[n] = int.Parse(tokens[n + indexOffset]); } elements.Add(femElement); } line = sr.ReadLine(); tokens = line.Split(delimiter); if (tokens.Length != 2 || tokens[0] != "Ports") { MessageBox.Show("入出力ポート情報がありません", "", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } int portCnt = int.Parse(tokens[1]); for (int i = 0; i < portCnt; i++) { line = sr.ReadLine(); tokens = line.Split(delimiter); if (tokens.Length != 2) { MessageBox.Show("入出力ポート情報が不正です", "", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } int portNo = int.Parse(tokens[0]); int portNodeCnt = int.Parse(tokens[1]); if (portNo != i + 1) { MessageBox.Show("ポート番号が不正です", "", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } IList <int> portNodes = new List <int>(); for (int n = 0; n < portNodeCnt; n++) { line = sr.ReadLine(); tokens = line.Split(delimiter); if (tokens.Length != 2) { MessageBox.Show("ポートの節点情報が不正です", "", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } int portNodeNumber = int.Parse(tokens[0]); int nodeNumber = int.Parse(tokens[1]); if (portNodeNumber != n + 1) { MessageBox.Show("ポートの節点番号が不正です", "", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } portNodes.Add(nodeNumber); } ports.Add(portNodes); } line = sr.ReadLine(); tokens = line.Split(delimiter); if (tokens.Length != 2 || tokens[0] != "Force") { MessageBox.Show("強制境界情報がありません", "", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } int forceNodeCnt = int.Parse(tokens[1]); for (int i = 0; i < forceNodeCnt; i++) { line = sr.ReadLine(); int nodeNumber = int.Parse(line); forceBCNodes.Add(nodeNumber); } // 周期構造領域内要素番号 for (int portIndex = 0; portIndex < portCnt; portIndex++) { line = sr.ReadLine(); tokens = line.Split(delimiter); if (tokens.Length != 3 || tokens[0] != "elemNoPeriodic") { MessageBox.Show("周期構造領域要素情報がありません", "", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } int tmpIndex = int.Parse(tokens[1]); System.Diagnostics.Debug.Assert(tmpIndex == portIndex); int cnt = int.Parse(tokens[2]); IList <uint> elemNoPeriodic = new List <uint>(); for (int i = 0; i < cnt; i++) { line = sr.ReadLine(); tokens = line.Split(delimiter); if (tokens.Length != 1) { MessageBox.Show("周期構造領域要素番号情報がありません", "", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } uint elemNo = uint.Parse(tokens[0]); elemNoPeriodic.Add(elemNo); } elemNoPeriodicList.Add(elemNoPeriodic); } // 周期構造境界節点番号 for (int portIndex = 0; portIndex < portCnt; portIndex++) { IList <IList <int> > nodePeriodicB = new List <IList <int> >(); for (int boundaryIndex = 0; boundaryIndex < 2; boundaryIndex++) { line = sr.ReadLine(); tokens = line.Split(delimiter); if (tokens.Length != 4 || tokens[0] != "nodePeriodicB") { MessageBox.Show("周期構造境界節点番号情報がありません", "", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } int tmpIndex1 = int.Parse(tokens[1]); System.Diagnostics.Debug.Assert(tmpIndex1 == portIndex); int tmpIndex2 = int.Parse(tokens[2]); System.Diagnostics.Debug.Assert(tmpIndex2 == boundaryIndex); int cnt = int.Parse(tokens[3]); IList <int> workNodesB = new List <int>(); for (int i = 0; i < cnt; i++) { line = sr.ReadLine(); tokens = line.Split(delimiter); if (tokens.Length != 1) { MessageBox.Show("周期構造境界節点番号情報がありません", "", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } int nodeNumber = int.Parse(tokens[0]); workNodesB.Add(nodeNumber); } nodePeriodicB.Add(workNodesB); } nodePeriodicBList.Add(nodePeriodicB); } // 周期構造領域内要素番号 for (int portIndex = 0; portIndex < portCnt; portIndex++) { line = sr.ReadLine(); tokens = line.Split(delimiter); if (tokens.Length != 3 || tokens[0] != "defectNodePeriodic") { MessageBox.Show("周期構造領域欠陥部節点情報がありません", "", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } int tmpIndex = int.Parse(tokens[1]); System.Diagnostics.Debug.Assert(tmpIndex == portIndex); int cnt = int.Parse(tokens[2]); IList <int> defectNodePeriodic = new List <int>(); for (int i = 0; i < cnt; i++) { line = sr.ReadLine(); tokens = line.Split(delimiter); if (tokens.Length != 1) { MessageBox.Show("周期構造領域欠陥部節点番号情報がありません", "", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } int nodeNumber = int.Parse(tokens[0]); defectNodePeriodic.Add(nodeNumber); } defectNodePeriodicList.Add(defectNodePeriodic); } line = sr.ReadLine(); tokens = line.Split(delimiter); if (tokens.Length != 2 || tokens[0] != "IncidentPortNo") { MessageBox.Show("入射ポート番号がありません", "", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } incidentPortNo = int.Parse(tokens[1]); line = sr.ReadLine(); if (line == null || line.Length == 0) { // 媒質情報なし } else { // 媒質情報? tokens = line.Split(delimiter); if (tokens[0] != "Medias") { MessageBox.Show("媒質情報がありません"); return(false); } int cnt = int.Parse(tokens[1]); if (cnt > Constants.MaxMediaCount) { MessageBox.Show("媒質情報の個数が不正です"); return(false); } for (int i = 0; i < cnt; i++) { line = sr.ReadLine(); if (line.Length == 0) { MessageBox.Show("媒質情報が不正です"); return(false); } tokens = line.Split(delimiter); if (tokens.Length != 1 + 9 + 9) { MessageBox.Show("媒質情報が不正です"); return(false); } int mediaIndex = int.Parse(tokens[0]); System.Diagnostics.Debug.Assert(mediaIndex == i); double[,] p = new double[3, 3]; for (int m = 0; m < p.GetLength(0); m++) { for (int n = 0; n < p.GetLength(1); n++) { p[m, n] = double.Parse(tokens[1 + m * p.GetLength(1) + n]); } } medias[i].SetP(p); double[,] q = new double[3, 3]; for (int m = 0; m < q.GetLength(0); m++) { for (int n = 0; n < q.GetLength(1); n++) { q[m, n] = double.Parse(tokens[1 + 9 + m * q.GetLength(1) + n]); } } medias[i].SetQ(q); } } line = sr.ReadLine(); if (line == null || line.Length == 0) { } else { tokens = line.Split(delimiter); if (tokens.Length != 4 || tokens[0] != "WaveLengthRange") { MessageBox.Show("計算対象周波数情報がありません"); return(false); } firstWaveLength = double.Parse(tokens[1]); lastWaveLength = double.Parse(tokens[2]); calcCnt = int.Parse(tokens[3]); } line = sr.ReadLine(); if (line == null || line.Length == 0) { } else { tokens = line.Split(delimiter); if (tokens.Length != 2 || tokens[0] != "WaveModeDv") { MessageBox.Show("計算対象モード区分情報がありません"); return(false); } if (tokens[1] == "TE") { waveModeDv = FemSolver.WaveModeDV.TE; } else if (tokens[1] == "TM") { waveModeDv = FemSolver.WaveModeDV.TM; } else { MessageBox.Show("計算対象モード区分情報が不正です"); return(false); } } } } catch (Exception exception) { System.Diagnostics.Debug.WriteLine(exception.Message + " " + exception.StackTrace); MessageBox.Show(exception.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } return(true); }
/// <summary> /// コンストラクタ /// </summary> /// <param name="normalizedFreq1">計算開始規格化周波数</param> /// <param name="normalizedFreq2">計算終了規格化周波数</param> /// <param name="calcFreqCnt">計算点数</param> /// <param name="wgStructureDv">導波路構造区分</param> /// <param name="waveModeDv">モード区分</param> /// <param name="elemShapeDv">要素形状区分</param> /// <param name="elemOrder">要素次数</param> /// <param name="lsEqnSolverDv">線形方程式解法区分</param> /// <param name="waveguideWidthForEPlane">導波路幅(E面解析用)</param> public CalcSettingFrm(double normalizedFreq1, double normalizedFreq2, int calcFreqCnt, FemSolver.WGStructureDV wgStructureDv, FemSolver.WaveModeDV waveModeDv, Constants.FemElementShapeDV elemShapeDv, int elemOrder, FemSolver.LinearSystemEqnSoverDV lsEqnSolverDv, double waveguideWidthForEPlane) { InitializeComponent(); DialogResult = DialogResult.None; // フィールドに格納 NormalizedFreq1 = normalizedFreq1; NormalizedFreq2 = normalizedFreq2; CalcFreqCnt = calcFreqCnt; WGStructureDv = wgStructureDv; WaveModeDv = waveModeDv; ElemShapeDv = elemShapeDv; ElemOrder = elemOrder; LsEqnSolverDv = lsEqnSolverDv; if (CalcFreqCnt == 0) { // 既定値を設定 NormalizedFreq1 = Constants.DefNormalizedFreqRange[0]; NormalizedFreq2 = Constants.DefNormalizedFreqRange[1]; CalcFreqCnt = Constants.DefCalcFreqencyPointCount; } // GUIにセット // 計算範囲 textBoxMinFreq.Text = string.Format("{0:F3}", NormalizedFreq1); textBoxMaxFreq.Text = string.Format("{0:F3}", NormalizedFreq2); double delta = (NormalizedFreq2 - NormalizedFreq1) / CalcFreqCnt; textBoxDeltaFreq.Text = string.Format("{0:F3}", delta); // 計算モード RadioBtnModeDvs = new RadioButton[] { radioBtnWaveModeDvTE, radioBtnWaveModeDvTM }; FemSolver.WaveModeDV[] waveModeDvOf_radioBtnModeDvs = { FemSolver.WaveModeDV.TE, FemSolver.WaveModeDV.TM }; for (int i = 0; i < RadioBtnModeDvs.Length; i++) { RadioBtnModeDvs[i].Tag = waveModeDvOf_radioBtnModeDvs[i]; if ((FemSolver.WaveModeDV)RadioBtnModeDvs[i].Tag == WaveModeDv) { RadioBtnModeDvs[i].Checked = true; } } // 導波路構造区分 WGStructureDVStruct[] wgStructureDvStructList = { new WGStructureDVStruct(FemSolver.WGStructureDV.HPlane2D, "H面導波管"), new WGStructureDVStruct(FemSolver.WGStructureDV.EPlane2D, "E面導波管"), new WGStructureDVStruct(FemSolver.WGStructureDV.ParaPlate2D, "平行平板導波路"), }; foreach (WGStructureDVStruct wgStructureDvStruct in wgStructureDvStructList) { cboxWGStructureDv.Items.Add(wgStructureDvStruct); if (wgStructureDvStruct.WGStructureDv == WGStructureDv) { cboxWGStructureDv.SelectedItem = wgStructureDvStruct; } } // 導波路幅(E面解析用) this.textBoxWaveguideWidthForEPlane.Text = string.Format("{0:F4}", waveguideWidthForEPlane); // 要素形状・次数 ElemShapeStruct[] esList = { new ElemShapeStruct(Constants.FemElementShapeDV.Triangle, Constants.SecondOrder, "2次三角形要素"), new ElemShapeStruct(Constants.FemElementShapeDV.QuadType2, Constants.SecondOrder, "2次四角形要素"), new ElemShapeStruct(Constants.FemElementShapeDV.Triangle, Constants.FirstOrder, "1次三角形要素"), new ElemShapeStruct(Constants.FemElementShapeDV.QuadType2, Constants.FirstOrder, "1次四角形要素"), }; foreach (ElemShapeStruct es in esList) { cboxElemShapeDv.Items.Add(es); if (es.ElemShapeDv == ElemShapeDv && es.Order == ElemOrder) { cboxElemShapeDv.SelectedItem = es; } } // 線形方程式解法 LinearSystemEqnSolverStruct[] lsList = { //new LinearSystemEqnSolverStruct(FemSolver.LinearSystemEqnSoverDV.PCOCG, "PCOCG"), new LinearSystemEqnSolverStruct(FemSolver.LinearSystemEqnSoverDV.Zgbsv, "zgbsv(バンド行列)"), new LinearSystemEqnSolverStruct(FemSolver.LinearSystemEqnSoverDV.Zgesv, "zgesv(一般行列)"), }; foreach (LinearSystemEqnSolverStruct ls in lsList) { cboxLsEqnSolverDv.Items.Add(ls); if (ls.LsEqnSolverDv == LsEqnSolverDv) { cboxLsEqnSolverDv.SelectedItem = ls; } } }
/// <summary> /// Fem入力データファイルへ保存 /// I/FがCadの内部データ寄りになっているので、変更したいが後回し /// </summary> /// <param name="filename">ファイル名(*.fem)</param> /// <param name="nodeCnt">節点数</param> /// <param name="doubleCoords">節点座標リスト</param> /// <param name="elementCnt">要素数</param> /// <param name="elements">要素リスト</param> /// <param name="portCnt">ポート数</param> /// <param name="portList">ポートの節点番号リストのリスト</param> /// <param name="forceBCNodeNumbers">強制境界節点番号のリスト</param> /// <param name="incidentPortNo">入射ポート番号</param> /// <param name="medias">媒質情報リスト</param> /// <param name="firstWaveLength">計算開始波長</param> /// <param name="lastWaveLength">計算終了波長</param> /// <param name="calcCnt">計算周波数件数</param> /// <param name="waveModeDv">波のモード区分</param> public static void SaveToFileFromCad (string filename, int nodeCnt, IList <double[]> doubleCoords, int elementCnt, IList <int[]> elements, int portCnt, IList <IList <int> > portList, int[] forceBCNodeNumbers, IList <IList <uint> > elemNoPeriodicList, IList <IList <IList <int> > > nodePeriodicBList, IList <IList <int> > defectNodePeriodicList, int incidentPortNo, MediaInfo[] medias, double firstWaveLength, double lastWaveLength, int calcCnt, FemSolver.WaveModeDV waveModeDv) { ////////////////////////////////////////// // ファイル出力 ////////////////////////////////////////// try { using (StreamWriter sw = new StreamWriter(filename)) { string line; // 節点番号と座標の出力 line = string.Format("Nodes,{0}", nodeCnt); sw.WriteLine(line); for (int i = 0; i < doubleCoords.Count; i++) { double[] doubleCoord = doubleCoords[i]; int nodeNumber = i + 1; line = string.Format("{0},{1},{2}", nodeNumber, doubleCoord[0], doubleCoord[1]); sw.WriteLine(line); } // 要素番号と要素を構成する節点の全体節点番号の出力 line = string.Format("Elements,{0}", elementCnt); sw.WriteLine(line); foreach (int[] element in elements) { line = ""; foreach (int k in element) { line += string.Format("{0},", k); } line = line.Substring(0, line.Length - 1); // 最後の,を削除 sw.WriteLine(line); } // ポート境界条件節点 int portCounter = 0; line = string.Format("Ports,{0}", portList.Count); sw.WriteLine(line); foreach (IList <int> nodes in portList) { line = string.Format("{0},{1}", ++portCounter, nodes.Count); sw.WriteLine(line); int portNodeNumber = 0; foreach (int nodeNumber in nodes) { line = string.Format("{0},{1}", ++portNodeNumber, nodeNumber); sw.WriteLine(line); } } // 強制境界節点 line = string.Format("Force,{0}", forceBCNodeNumbers.Length); sw.WriteLine(line); foreach (int nodeNumber in forceBCNodeNumbers) { line = string.Format("{0}", nodeNumber); sw.WriteLine(line); } // 周期構造領域内要素番号 System.Diagnostics.Debug.Assert(portCnt == elemNoPeriodicList.Count); for (int portIndex = 0; portIndex < portCnt; portIndex++) { IList <uint> elemNoPeriodic = elemNoPeriodicList[portIndex]; line = string.Format("elemNoPeriodic,{0},{1}", portIndex, elemNoPeriodic.Count); sw.WriteLine(line); for (int i = 0; i < elemNoPeriodic.Count; i++) { line = string.Format("{0}", elemNoPeriodic[i]); sw.WriteLine(line); } } // 周期構造境界節点番号 System.Diagnostics.Debug.Assert(portCnt == nodePeriodicBList.Count); for (int portIndex = 0; portIndex < portCnt; portIndex++) { IList <IList <int> > nodePeriodicB = nodePeriodicBList[portIndex]; for (int boundaryIndex = 0; boundaryIndex < 2; boundaryIndex++) { IList <int> workNodesB = nodePeriodicB[boundaryIndex]; line = string.Format("nodePeriodicB,{0},{1},{2}", portIndex, boundaryIndex, workNodesB.Count); sw.WriteLine(line); for (int i = 0; i < workNodesB.Count; i++) { line = string.Format("{0}", workNodesB[i]); sw.WriteLine(line); } } } // 周期構造領域内欠陥部の節点番号 System.Diagnostics.Debug.Assert(portCnt == defectNodePeriodicList.Count); for (int portIndex = 0; portIndex < portCnt; portIndex++) { IList <int> defectNodePeriodic = defectNodePeriodicList[portIndex]; line = string.Format("defectNodePeriodic,{0},{1}", portIndex, defectNodePeriodic.Count); sw.WriteLine(line); for (int i = 0; i < defectNodePeriodic.Count; i++) { line = string.Format("{0}", defectNodePeriodic[i]); sw.WriteLine(line); } } // 入射ポート番号 line = string.Format("IncidentPortNo,{0}", incidentPortNo); sw.WriteLine(line); // 媒質情報の個数 sw.WriteLine("Medias,{0}", medias.Length); // 媒質情報の書き込み for (int i = 0; i < medias.Length; i++) { MediaInfo media = medias[i]; line = string.Format("{0},", i); double[,] p = media.P; for (int m = 0; m < p.GetLength(0); m++) { for (int n = 0; n < p.GetLength(1); n++) { line += string.Format("{0},", p[m, n]); } } double[,] q = media.Q; for (int m = 0; m < q.GetLength(0); m++) { for (int n = 0; n < q.GetLength(1); n++) { line += string.Format("{0},", q[m, n]); } } line = line.Remove(line.Length - 1); // 最後の,を削除 sw.WriteLine(line); } // 計算対象周波数 sw.WriteLine("WaveLengthRange,{0},{1},{2}", firstWaveLength, lastWaveLength, calcCnt); // 計算対象モード区分 sw.WriteLine("WaveModeDv,{0}", ((waveModeDv == FemSolver.WaveModeDV.TM) ? "TM" : "TE")); } } catch (Exception exception) { System.Diagnostics.Debug.WriteLine(exception.Message + " " + exception.StackTrace); MessageBox.Show(exception.Message); } }
/// <summary> /// [実行]ボタンクリックイベントハンドラ /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnRun_Click(object sender, EventArgs e) { // GUIからデータを取得 // 計算範囲 double minFreq = double.Parse(textBoxMinFreq.Text); double maxFreq = double.Parse(textBoxMaxFreq.Text); double deltaFreq = double.Parse(textBoxDeltaFreq.Text); FemSolver.WaveModeDV waveModeDv = WaveModeDv; foreach (RadioButton rbtn in RadioBtnModeDvs) { if (rbtn.Checked) { waveModeDv = (FemSolver.WaveModeDV)rbtn.Tag; break; } } // 導波路構造区分 WGStructureDVStruct wgStructureDvStruct = (WGStructureDVStruct)cboxWGStructureDv.SelectedItem; double waveguideWidthForEPlane = double.Parse(textBoxWaveguideWidthForEPlane.Text); // 要素形状・次数 ElemShapeStruct selectedEs = (ElemShapeStruct)cboxElemShapeDv.SelectedItem; // 線形方程式解法 LinearSystemEqnSolverStruct selectedLs = (LinearSystemEqnSolverStruct)cboxLsEqnSolverDv.SelectedItem; if (maxFreq - minFreq < Constants.PrecisionLowerLimit) { MessageBox.Show("開始と終了が同じか逆転しています"); return; } if (deltaFreq < Constants.PrecisionLowerLimit) { MessageBox.Show("計算間隔が設定されていません"); return; } if (wgStructureDvStruct.WGStructureDv == FemSolver.WGStructureDV.EPlane2D && Math.Abs(waveguideWidthForEPlane) < Constants.PrecisionLowerLimit) { MessageBox.Show("導波路幅(E面解析用)が指定されていません"); return; } //int cnt = (int)((double)(maxFreq - minFreq) / deltaFreq); int cnt = (int)Math.Ceiling((double)(maxFreq - minFreq) / deltaFreq); if (cnt < 2) { //return; cnt = 1; // 1箇所で計算 } // TMモードは平行平板以外では計算できない --> TEモードをセットする if (wgStructureDvStruct.WGStructureDv != FemSolver.WGStructureDV.ParaPlate2D) { if (waveModeDv == FemSolver.WaveModeDV.TM) { //MessageBox.Show("TMモードでは計算できません。TEモードに変更します。", "", MessageBoxButtons.OK); waveModeDv = FemSolver.WaveModeDV.TE; } } // 設定された計算範囲を格納 CalcFreqCnt = cnt; NormalizedFreq1 = minFreq; NormalizedFreq2 = maxFreq; WGStructureDv = wgStructureDvStruct.WGStructureDv; WaveModeDv = waveModeDv; ElemShapeDv = selectedEs.ElemShapeDv; ElemOrder = selectedEs.Order; LsEqnSolverDv = selectedLs.LsEqnSolverDv; WaveguideWidthForEPlane = waveguideWidthForEPlane; DialogResult = DialogResult.OK; }
/// <summary> /// ヘルムホルツ方程式に対する有限要素マトリクス作成 /// </summary> /// <param name="waveLength">波長</param> /// <param name="toSorted">ソートされた節点インデックス( 2D節点番号→ソート済みリストインデックスのマップ)</param> /// <param name="element">有限要素</param> /// <param name="Nodes">節点リスト</param> /// <param name="Medias">媒質リスト</param> /// <param name="ForceNodeNumberH">強制境界節点ハッシュ</param> /// <param name="WGStructureDv">導波路構造区分</param> /// <param name="WaveModeDv">計算する波のモード区分</param> /// <param name="waveguideWidthForEPlane">導波路幅(E面解析用)</param> /// <param name="mat">マージされる全体行列</param> public static void AddElementMat(double waveLength, Dictionary <int, int> toSorted, FemElement element, IList <FemNode> Nodes, MediaInfo[] Medias, Dictionary <int, bool> ForceNodeNumberH, FemSolver.WGStructureDV WGStructureDv, FemSolver.WaveModeDV WaveModeDv, double waveguideWidthForEPlane, ref MyComplexMatrix mat) { // 定数 const double pi = Constants.pi; const double c0 = Constants.c0; // 波数 double k0 = 2.0 * pi / waveLength; // 角周波数 double omega = k0 * c0; // 要素頂点数 //const int vertexCnt = Constants.QuadVertexCnt; //4; // 要素内節点数 const int nno = Constants.QuadNodeCnt_FirstOrder; //4; // 1次セレンディピティ // 座標次元数 const int ndim = Constants.CoordDim2D; //2; int[] nodeNumbers = element.NodeNumbers; int[] no_c = new int[nno]; MediaInfo media = Medias[element.MediaIndex]; double[,] media_P = null; double[,] media_Q = null; // ヘルムホルツ方程式のパラメータP,Qを取得する FemSolver.GetHelmholtzMediaPQ( k0, media, WGStructureDv, WaveModeDv, waveguideWidthForEPlane, out media_P, out media_Q); // 節点座標(IFの都合上配列の配列形式の2次元配列を作成) double[][] pp = new double[nno][]; for (int ino = 0; ino < nno; ino++) { int nodeNumber = nodeNumbers[ino]; int nodeIndex = nodeNumber - 1; FemNode node = Nodes[nodeIndex]; no_c[ino] = nodeNumber; pp[ino] = new double[ndim]; for (int n = 0; n < ndim; n++) { pp[ino][n] = node.Coord[n]; } } // 四角形の辺の長さを求める double[] le = new double[4]; le[0] = FemMeshLogic.GetDistance(pp[0], pp[1]); le[1] = FemMeshLogic.GetDistance(pp[1], pp[2]); le[2] = FemMeshLogic.GetDistance(pp[2], pp[3]); le[3] = FemMeshLogic.GetDistance(pp[3], pp[0]); System.Diagnostics.Debug.Assert(Math.Abs(le[0] - le[2]) < Constants.PrecisionLowerLimit); System.Diagnostics.Debug.Assert(Math.Abs(le[1] - le[3]) < Constants.PrecisionLowerLimit); double lx = le[0]; double ly = le[1]; // 要素節点座標( 局所r,s成分 ) // s // | // 3+ + +2 // | | | // ---+---+---+-->r // | | | // 0+ + +1 // | // double[][] n_pts = { // r, s new double[] { -1.0, -1.0 }, //0 new double[] { 1.0, -1.0 }, //1 new double[] { 1.0, 1.0 }, //2 new double[] { -1.0, 1.0 }, //3 }; // ∫dN/dndN/dn dxdy // integralDNDX[n, ino, jno] n = 0 --> ∫dN/dxdN/dx dxdy // n = 1 --> ∫dN/dydN/dy dxdy double[, ,] integralDNDX = new double[ndim, nno, nno] { { { 2.0 * ly / (6.0 * lx), -2.0 * ly / (6.0 * lx), -1.0 * ly / (6.0 * lx), 1.0 * ly / (6.0 * lx) }, { -2.0 * ly / (6.0 * lx), 2.0 * ly / (6.0 * lx), 1.0 * ly / (6.0 * lx), -1.0 * ly / (6.0 * lx) }, { -1.0 * ly / (6.0 * lx), 1.0 * ly / (6.0 * lx), 2.0 * ly / (6.0 * lx), -2.0 * ly / (6.0 * lx) }, { 1.0 * ly / (6.0 * lx), -1.0 * ly / (6.0 * lx), -2.0 * ly / (6.0 * lx), 2.0 * ly / (6.0 * lx) }, }, { { 2.0 * lx / (6.0 * ly), 1.0 * lx / (6.0 * ly), -1.0 * lx / (6.0 * ly), -2.0 * lx / (6.0 * ly) }, { 1.0 * lx / (6.0 * ly), 2.0 * lx / (6.0 * ly), -2.0 * lx / (6.0 * ly), -1.0 * lx / (6.0 * ly) }, { -1.0 * lx / (6.0 * ly), -2.0 * lx / (6.0 * ly), 2.0 * lx / (6.0 * ly), 1.0 * lx / (6.0 * ly) }, { -2.0 * lx / (6.0 * ly), -1.0 * lx / (6.0 * ly), 1.0 * lx / (6.0 * ly), 2.0 * lx / (6.0 * ly) }, } }; // ∫N N dxdy double[,] integralN = new double[nno, nno] { { 4.0 * lx * ly / 36.0, 2.0 * lx * ly / 36.0, 1.0 * lx * ly / 36.0, 2.0 * lx * ly / 36.0 }, { 2.0 * lx * ly / 36.0, 4.0 * lx * ly / 36.0, 2.0 * lx * ly / 36.0, 1.0 * lx * ly / 36.0 }, { 1.0 * lx * ly / 36.0, 2.0 * lx * ly / 36.0, 4.0 * lx * ly / 36.0, 2.0 * lx * ly / 36.0 }, { 2.0 * lx * ly / 36.0, 1.0 * lx * ly / 36.0, 2.0 * lx * ly / 36.0, 4.0 * lx * ly / 36.0 }, }; // 要素剛性行列を作る double[,] emat = new double[nno, nno]; for (int ino = 0; ino < nno; ino++) { for (int jno = 0; jno < nno; jno++) { emat[ino, jno] = media_P[0, 0] * integralDNDX[1, ino, jno] + media_P[1, 1] * integralDNDX[0, ino, jno] - k0 * k0 * media_Q[2, 2] * integralN[ino, jno]; } } // 要素剛性行列にマージする for (int ino = 0; ino < nno; ino++) { int iNodeNumber = no_c[ino]; if (ForceNodeNumberH.ContainsKey(iNodeNumber)) { continue; } int inoGlobal = toSorted[iNodeNumber]; for (int jno = 0; jno < nno; jno++) { int jNodeNumber = no_c[jno]; if (ForceNodeNumberH.ContainsKey(jNodeNumber)) { continue; } int jnoGlobal = toSorted[jNodeNumber]; //mat[inoGlobal, jnoGlobal] += emat[ino, jno]; //mat._body[inoGlobal + jnoGlobal * mat.RowSize] += emat[ino, jno]; // 実数部に加算する //mat._body[inoGlobal + jnoGlobal * mat.RowSize].Real += emat[ino, jno]; // バンドマトリクス対応 mat._body[mat.GetBufferIndex(inoGlobal, jnoGlobal)].Real += emat[ino, jno]; } } }