カスタムシミュレーション

シンプルな例

高度なモデリング要素を作成して、モデルの構築作業を簡素化できるのと同様に、目的に合わせた独自の関数やオブジェクトを作成することで、複雑なシミュレーションを構成できます。上記の例は、2つの連続したシミュレーションが実行される様子を示しています。

次の例に示すように、独自の関数を作成し、実行時にこれを呼び出すことができます。
# Create a model
m = sliding_block("lugre1")

# Define a custom simulation function
def mySlidingBlockSimulation (m):
m.simulate (type="DYNAMICS", end=4, dtout=.01)
m.lugre.mus=0.5
m.simulate (type="DYNAMICS", end=8, dtout=.01)

# Perform a custom simulation on the model
mySlidingBlockSimulation (m)

Modelオブジェクトは、より複雑なカスタムシミュレーションシナリオを容易にします。これについて以下に説明します。

Modelオブジェクトを使用した複雑なシミュレーション

def sliding_block (out_name):
 Line 6: m = Model (output=out_name)

独自のモデリング要素を用いたモデルの構築の例の6行目で、関数sliding_block()によってModelオブジェクトのmが作成されます。Modelオブジェクトは、モデル全体を格納するコンテナです。Modelオブジェクトは、そのコンテンツを操作できるいくつかのメソッドを備えています。

  • sendToSolver – モデルをソルバーに送信します。
  • simulate – モデルのシミュレーションを実行します。
  • save – モデルとシミュレーションの条件をディスクに保存します。
  • reload – 保存されたモデルを再び読み込みます。
  • control – CONSUBを実行します。
  • reset – シミュレーションをリセットして、次のシミュレーションがT=0から開始されるようにします。

ModelメソッドをModelオブジェクトで使用することで、かなり複雑なシミュレーションを処理できます。

以下は、前出のシミュレーションスクリプトを拡張したものです。

  • このスクリプトでは、LuGre摩擦要素のμstatic(静摩擦係数)を指定するように求められます。
  • このスクリプトではこの値を適用して、シミュレーションが実行されます。
  • このプロセスが繰り返されて、ユーザーはμstaticの新しい値を指定し続けることができます。
  • このプロセスは、STOPコマンドを発行すると停止します。
  • その後このスクリプトは、ユーザーによって指定されたさまざまなμstaticの値に対応するいくつかの主要な結果をプロットしてから、実行を終了します。
以下にシミュレーションスクリプトを示します。
# Initialize a few variables: duration for each simulation, # simulations done, a string
dur = 4.0
num_simulations = 0
textstr = '$\mu=$'

# Read the model
m = sliding_block()

###############################################################################
# Loop until user asks you to stop #
###############################################################################
while True:

 # Get a value for mu static or a command to stop
mus = raw_input ("Please enter a value for mu static or stop: ")
if mus.lower ().startswith ("s"):
break
else:
num_simulations +=1

 # Apply the value of mu static the user provided and do a simulation
m.lugre.mus=mus
run = m.simulate (type="DYNAMICS", returnResults=True, duration=dur, dtout=0.01)
textstr += ', %.2f '%(m.lugre.mus)

# Get the request time history from the run object
disp = run.getObject (m.r1)
velo = run.getObject (m.r3)
force = run.getObject (m.r2)

###############################################################################
# Plot some interesting results #
###############################################################################
import matplotlib.pyplot as plt

# Friction force vs. Time
plt.subplot (3, 1, 1)
plt.plot (force.times, force.getComponent (3))
plt.title ('Friction force')
plt.ylabel ('force [N]')
plt.text (0.4, 3, textstr)
plt.grid (True)

# Block Velocity vs. Time
plt.subplot (3, 1, 2)
plt.plot (velo.times, velo.getComponent (3), 'r')
plt.title ('Block Velocity')
plt.ylabel ('velocity [m/s]')
plt.grid (True)

# Block Displacement vs. Time
plt.subplot (3, 1, 3)
plt.plot (disp.times, disp.getComponent (3), 'g')
plt.title ('Block Displacement')
plt.xlabel ('time (s)')
plt.ylabel ('travel [m]')
plt.grid (True)

plt.show()

The results of this simulation are shown below.
以下にこのシミュレーションの結果を示します。


図 1.