0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【メモ】PythonでiRICのソルバーを作る際の格子の配列について

Last updated at Posted at 2024-09-04

はじめに

よく忘れるので自分用のメモです。

本編

CGNSから読み込んだ際の格子点の属性の配列は1次元の配列で返ってきます。
その時のデータのインデックスは以下のようになり、列優先で返ってくる仕様となっています。

# --------------------------------------------------
# CGNSから読み込む時は1次元配列、順番は以下
# --------------------------------------------------
#      j
#      ↑
#     4| 24, 25, 26, 27, 28, 29
#     3| 18, 19, 20, 21, 22, 23
#     2| 12, 13, 14, 15, 16, 17
#     1|  6,  7,  8,  9, 10, 11
#     0|  0,  1,  2,  3,  4,  5
#       ----------------------- → i
#         0   1   2   3   4   5
# --------------------------------------------------

試しに下記のようなコードで、以下の(11*6)の格子の格子点のx座標、y座標を読み込んでみます。

image.png

import iric

# ファイル名を指定(デバッグ用)
cgns_name = ".\project_py\Case1.cgn"

# CGNSを開く
fid = iric.cg_iRIC_Open(cgns_name, iric.IRIC_MODE_MODIFY)

# 格子サイズの読み込み
isize, jsize = iric.cg_iRIC_Read_Grid2d_Str_Size(fid)
print("isize=" + str(isize) + " jsize=" + str(jsize))

# 座標の読み込み
grid_x_arr, grid_y_arr = iric.cg_iRIC_Read_Grid2d_Coords(fid)
print("grid_x_arr")
print(grid_x_arr)
print("grid_y_arr")
print(grid_y_arr)

すると11行6列の格子点の座標は以下のように列優先で読み込まれていることが確認できました。

isize=11 jsize=6
grid_x_arr
[ 0.  1.  2.  3.  4.  5.  6.  7.  8.  9. 10.  0.  1.  2.  3.  4.  5.  6.
  7.  8.  9. 10.  0.  1.  2.  3.  4.  5.  6.  7.  8.  9. 10.  0.  1.  2.
  3.  4.  5.  6.  7.  8.  9. 10.  0.  1.  2.  3.  4.  5.  6.  7.  8.  9.
 10.  0.  1.  2.  3.  4.  5.  6.  7.  8.  9. 10.]
grid_y_arr
[0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.2 0.2 0.2 0.2 0.2 0.2 0.2
 0.2 0.2 0.2 0.2 0.4 0.4 0.4 0.4 0.4 0.4 0.4 0.4 0.4 0.4 0.4 0.6 0.6 0.6
 0.6 0.6 0.6 0.6 0.6 0.6 0.6 0.6 0.8 0.8 0.8 0.8 0.8 0.8 0.8 0.8 0.8 0.8
 0.8 1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1. ]

ただ、実際にプログラム内で計算する時は$(i,j)$で指定したいことが多いので2次元配列に変換したい。
しかしpythonは行優先の言語のため変換する際には行列を逆に変換したうえで、行列を入れ替えることで2次元はいい列に変換を行う。

# 格子の形に整形
grid_x_arr = grid_x_arr.reshape(jsize, isize).T
grid_y_arr = grid_y_arr.reshape(jsize, isize).T

print("Reshaped grid_x_arr")
print(grid_x_arr)
print("Reshaped grid_y_arr")
print(grid_y_arr)

出力結果
Reshaped grid_x_arr
[[ 0.  0.  0.  0.  0.  0.]
 [ 1.  1.  1.  1.  1.  1.]
 [ 2.  2.  2.  2.  2.  2.]
 [ 3.  3.  3.  3.  3.  3.]
 [ 4.  4.  4.  4.  4.  4.]
 [ 5.  5.  5.  5.  5.  5.]
 [ 6.  6.  6.  6.  6.  6.]
 [ 7.  7.  7.  7.  7.  7.]
 [ 8.  8.  8.  8.  8.  8.]
 [ 9.  9.  9.  9.  9.  9.]
 [10. 10. 10. 10. 10. 10.]]
Reshaped grid_y_arr
[[0.  0.2 0.4 0.6 0.8 1. ]
 [0.  0.2 0.4 0.6 0.8 1. ]
 [0.  0.2 0.4 0.6 0.8 1. ]
 [0.  0.2 0.4 0.6 0.8 1. ]
 [0.  0.2 0.4 0.6 0.8 1. ]
 [0.  0.2 0.4 0.6 0.8 1. ]
 [0.  0.2 0.4 0.6 0.8 1. ]
 [0.  0.2 0.4 0.6 0.8 1. ]
 [0.  0.2 0.4 0.6 0.8 1. ]
 [0.  0.2 0.4 0.6 0.8 1. ]
 [0.  0.2 0.4 0.6 0.8 1. ]]
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?