質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
for

for文は、様々なプログラミング言語で使われている制御構造です。for文に定義している条件から外れるまで、for文内の命令文を繰り返し実行します。

Python 2.7

Python 2.7は2.xシリーズでは最後のメジャーバージョンです。Python3.1にある機能の多くが含まれています。

オブジェクト指向

オブジェクト指向プログラミング(Object-oriented programming;OOP)は「オブジェクト」を使用するプログラミングの概念です。オブジェクト指向プログラムは、カプセル化(情報隠蔽)とポリモーフィズム(多態性)で構成されています。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

1回答

372閲覧

末尾が連番の複数あるオブジェクトを一気に処理したい

aine_

総合スコア22

for

for文は、様々なプログラミング言語で使われている制御構造です。for文に定義している条件から外れるまで、for文内の命令文を繰り返し実行します。

Python 2.7

Python 2.7は2.xシリーズでは最後のメジャーバージョンです。Python3.1にある機能の多くが含まれています。

オブジェクト指向

オブジェクト指向プログラミング(Object-oriented programming;OOP)は「オブジェクト」を使用するプログラミングの概念です。オブジェクト指向プログラムは、カプセル化(情報隠蔽)とポリモーフィズム(多態性)で構成されています。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2018/11/05 10:33

編集2018/11/07 11:41

末尾が連番のものの処理を一気にしたいです。

python

1class DamBreak3DGeometry(object): 2 def __init__( 3 self, container_height=1.0, container_width=0.6, container_length=11.0, 4 fluid_column_height=0.50, fluid_column_width=0.6, fluid_column_length=1.60, 5 obstacle0_center_x=8.00, obstacle0_center_y=0, 6 obstacle0_r=0.05, obstacle0_height=0.8, 7 obstacle1_center_x=8.00, obstacle1_center_y=-0.06, 8 obstacle1_r=0.05, obstacle1_height=0.8, 9 nboundary_layers=5, with_obstacle0=True,with_obstacle1=True, 10 dx=0.02, hdx=1.2, rho0=1000.0): 11 12 # save the geometry detail 13 self.container_length = container_length 14 self.container_width = container_width 15 self.container_height = container_height 16 self.fluid_column_length=fluid_column_length 17 self.fluid_column_width=fluid_column_width 18 self.fluid_column_height=fluid_column_height 19 20 21 self.obstacle0_center_x = obstacle0_center_x 22 self.obstacle0_center_y = obstacle0_center_y 23 self.obstacle0_r = obstacle0_r 24 self.obstacle0_height = obstacle0_height 25 26 self.obstacle1_center_x = obstacle1_center_x 27 self.obstacle1_center_y = obstacle1_center_y 28 self.obstacle1_r = obstacle1_r 29 self.obstacle1_height = obstacle1_height 30 31 32 self.nboundary_layers=nboundary_layers 33 self.dx=dx 34 35 self.hdx = hdx 36 self.rho0 = rho0 37 self.with_obstacle0 = with_obstacle0 38 self.with_obstacle1 = with_obstacle1 39 40 def create_particles(self, **kwargs): 41 fluid_column_height=self.fluid_column_height 42 fluid_column_width=self.fluid_column_width 43 fluid_column_length=self.fluid_column_length 44 45 container_height = self.container_height 46 container_length = self.container_length 47 container_width = self.container_width 48 49 obstacle0_center_x = self.obstacle0_center_x 50 obstacle0_center_y = self.obstacle0_center_y 51 obstacle0_r = self.obstacle0_r 52 obstacle0_height = self.obstacle0_height 53 54 55 56 nboundary_layers = self.nboundary_layers 57 dx = self.dx 58 59 # get the domain limits 60 ghostlims = nboundary_layers * dx 61 62 xmin, xmax = 0.0 -ghostlims, container_length + ghostlims 63 zmin, zmax = 0.0 - ghostlims, container_height + ghostlims 64 65 cw2 = 0.5 * container_width 66 ymin, ymax = -cw2 - ghostlims, cw2 + ghostlims 67 68 # create all particles 69 eps = 0.1 * dx 70 xx, yy, zz = numpy.mgrid[xmin:xmax+eps:dx, 71 ymin:ymax+eps:dx, 72 zmin:zmax+eps:dx] 73 74 x = xx.ravel(); y = yy.ravel(); z = zz.ravel() 75 76 # create a dummy particle array from which we'll sort 77 pa = get_particle_array_wcsph(name='block', x=x, y=y, z=z) 78 79 # get the individual arrays 80 indices = [] 81 findices = [] 82 oindices = [] 83 84 85 86 obr0 = 2**obstacle0_r 87 obh0 = obstacle0_height 88 ocx0 = obstacle0_center_x 89 ocy0 = obstacle0_center_y 90 91 92 93 obr1 = 2**obstacle1_r 94 obh1 = obstacle1_height 95 ocx1 = obstacle1_center_x 96 ocy1 = obstacle1_center_y 97 98 99 for i in range(x.size): 100 xi = x[i]; yi = y[i]; zi = z[i] 101 102 # fluid 103 if ( (0 < xi <= fluid_column_length) and \ 104 (-cw2 < yi < cw2) and \ 105 (0 < zi <= fluid_column_height) ): 106 107 findices.append(i) 108 #obstacle 109 if ( (ocx0 - xi)**2 + (ocy0 - yi)**2 <= obr0 and \ 110 (0 < zi <= obh0) ): 111 112 oindices.append(i) 113 114 if ( (ocx1 - xi)**2 + (ocy1 - yi)**2 <= obr1 and \ 115 (0 < zi <= obh1) ): 116 117 oindices.append(i) 118

というコードがあり、これはまず円柱、箱、水の位置を定義しその範囲内に粒子を詰めることにより物理的影響などを計算するというものです。
この円柱を増やしたいためfor文で

python

1class DamBreak3DGeometry(object): 2 def __init__( 3 self, container_height=1.0, container_width=0.6, container_length=11.0, 4 fluid_column_height=0.50, fluid_column_width=0.6, fluid_column_length=1.60, 5 obstacle0_center_x=8.00, obstacle0_center_y=0, 6 obstacle0_r=0.05, obstacle0_height=0.8, 7 obstacle1_center_x=8.00, obstacle1_center_y=-0.06, 8 obstacle1_r=0.05, obstacle1_height=0.8, 9 nboundary_layers=5, with_obstacle0=True,with_obstacle1=True, 10 dx=0.02, hdx=1.2, rho0=1000.0): 11 12 # save the geometry detail 13 self.container_r = container_r 14 self.container_height = container_height 15 self.fluid_column_length=fluid_column_length 16 self.fluid_column_width=fluid_column_width 17 self.fluid_column_height=fluid_column_height 18 19 for k in range (1): 20 center_x = "obstacle%d_center_x"%(k) 21 self.center_x = center_x 22 center_y = "obstacle%d_center_y"%(k) 23 self.center_y = center_y 24 r = "obstacle%d_r%"(k) 25 self.r = r 26 height = "obstacle%d_height"%(k) 27 self.height = height 28 29 30 self.nboundary_layers=nboundary_layers 31 self.dx=dx 32 33 self.hdx = hdx 34 self.rho0 = rho0 35 self.with_obstacle0 = with_obstacle0 36 self.with_obstacle1 = with_obstacle1 37 38 def create_particles(self, **kwargs): 39 fluid_column_height=self.fluid_column_height 40 fluid_column_width=self.fluid_column_width 41 fluid_column_length=self.fluid_column_length 42 43 container_height = self.container_height 44 container_length = self.container_length 45 container_width = self.container_width 46 47 for k in range (1): 48 center_x = "obstacle%d_center_x"%(k) 49 center_x = self.center_x 50 center_y = "obstacle%d_center_y"%(k) 51 center_y = self.center_y 52 r = "obstacle%d_r"%(k) 53 r = self.r 54 height = "obstacle%d_height"%(k) 55 height = self.height 56 57 58 59 nboundary_layers = self.nboundary_layers 60 dx = self.dx 61 62 # get the domain limits 63 ghostlims = nboundary_layers * dx 64 65 xmin, xmax = 0.0 -ghostlims, container_length + ghostlims 66 zmin, zmax = 0.0 - ghostlims, container_height + ghostlims 67 68 cw2 = 0.5 * container_width 69 ymin, ymax = -cw2 - ghostlims, cw2 + ghostlims 70 71 # create all particles 72 eps = 0.1 * dx 73 xx, yy, zz = numpy.mgrid[xmin:xmax+eps:dx, 74 ymin:ymax+eps:dx, 75 zmin:zmax+eps:dx] 76 77 x = xx.ravel(); y = yy.ravel(); z = zz.ravel() 78 79 # create a dummy particle array from which we'll sort 80 pa = get_particle_array_wcsph(name='block', x=x, y=y, z=z) 81 82 # get the individual arrays 83 indices = [] 84 findices = [] 85 oindices = [] 86 87 88 for k in range (1): 89 obrk = "2 ** obstacle%d_r"%(k) 90 obhk = "obstacle%d_height"%(k) 91 ocxk = "obstacle%d_center_x"%(k) 92 ocyk = "obstacle%d_center_y"%(k) 93 94 95 for i in range(x.size): 96 xi = x[i]; yi = y[i]; zi = z[i] 97 98 # fluid 99 if ( (0 < xi <= fluid_column_length) and \ 100 (-cw2 < yi < cw2) and \ 101 (0 < zi <= fluid_column_height) ): 102 103 findices.append(i) 104 for k in range (1): 105 106 if ( (ocxk - xi)**2 + (ocyk - yi)**2 <= obrk and \ 107 (0 < zi <= obh) ): 108 109      oindices.append(i) 110 111 112

としてみたのですが、TypeError: unsupported operand type(s) for -: 'str' and 'str'
となってしまい実行できません。この場合最後のif文のループに問題があるのでしょうか?

コードについて修正させていただきました。
cotainarのなかにおいたobstacleに対しfluidがどのように作用するか、というプログラミングの一部抜粋です。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

can110

2018/11/05 19:44 編集

エラー再現できません(__init__関数にself引数がない、インデント数が空白8個と4個とが混在しているなどおかしな点が気になりますが。)実際にエラーが再現する最小限の完全なコードを提示されると回答得られやすくなります。
aine_

2018/11/06 03:59

申し訳ございません。抜粋したつもりでしたが稚拙すぎました。
guest

回答1

0

ベストアンサー

この場合最後のif文のループに問題があるのでしょうか?

そこまで分かっているのなら、実際に試してみれば充分解決するのでは。

Python

1>>> if True: 2... pass 3 File "<stdin>", line 2 4 pass 5 ^ 6IndentationError: expected an indented block 7>>> 8>>> if True: 9... pass 10... 11>>>

投稿2018/11/07 10:03

LouiS0616

総合スコア35658

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

aine_

2018/11/07 11:18

すみません、浅学なものでわからないのですがif文をそのようにすればよいということですか?
LouiS0616

2018/11/07 11:33

IndentationErrorですから、インデントが良くないと言うことです。 インデントを正しく付ければエラーは解消します。
aine_

2018/11/07 11:41

ありがとうございます。インデントエラーについては解決できました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問