Commit 89eb7b68 authored by Louis Beauloye's avatar Louis Beauloye Committed by Olivier Lantsoght
Browse files

[Ctypes] set and get options for dirdyn

parent cf862a62
......@@ -19,6 +19,7 @@ the memory.
import os
import imp
import ctypes
import numpy as np
......@@ -310,8 +311,319 @@ class MbsDirdyn(object):
libmodules.mbs_run_dirdyn(self.mbs_dirdyn_ptr, self.mbs.mbs_data_ptr)
def set_options(self, **kwargs):
"""
Set the specified options for Dirdyn module
Parameters
----------
t0 : float
initial time of the simulation, default = 0.0
tf : float
final time of the simulation, default = 5.0
dt0 : float
initial value of the integration step size, default = 0.001
save2file : int
Determine whether results are written to files on disk (in resultsR folder):
1: results are saved
0: results are not saved
default = 1
resfilename : str
The keyword used for determining the name of result files
respath : str
Path in which result file are saved.
Default: the resultsR folder of the project
animpath : str
Path in which anim file is saved.
Default: the animationR folder of the project
save_anim : int
1 to save the anim file
0 otherwise (only valid if 'save2file' is set to 1)
default = 1
save_visu : int
1 to save the visualizazion file (as it appears in 'user_realtime_visu.c')
0 otherwise (only valid if 'save2file' is set to 1)
default = 0
framerate : int
number of frame per second for the .anim file
default = 1000
saveperiod : int
The number of time steps between two buffer records
default: 1 (every time step are recorded)
max_save_user : int
The maximal number of user variables saved
default: 12
buffersize : int
The number of time step that can be recorded in the buffer.
Results are written to disk when the buffer is full.
default: -1
compute the buffer size for saving results only once at the end according to dt0, t0 and tf.
realtime : int
1 to activate to real-time features
0 to deactivate them
default = 0
accelred : int
1 to use accelred
0 otherwise
default = 0
flag_compute_Qc : int
choose to compute (or not) the Qc for driven variables
For dirdyn only
==1 by default
this allows to go faster
compute_Qc : numpy.ndarray
If options->flag_compute_Qc is set to 0, the vector allows to compute only the needed Qc
For dirdyn only.
Empty vector by default
If compute_Qc = [njoint 1 0 0 1 0], we will compute Qc(1) and Qc(4) only.
length by default to njoint+1, compute_Qc[0] = njoint
integrator : str
Set integrator to use, available value: "RK4", "Dopri5", "Rosenbrock","EulerEx", "Eulaire","EulerIm","Bader", "WMethods", "Custom"
default = "RK4".
verbose : int
1 to get print indications related to time adaptive integrator
0 otherwise
default = 1
flag_stop_stiff : int
1 to stop integration if it become stiff
0 (default value) otherwise
default = 0
flag_precise_dynamics : int
= 1 to call the direct dynamics at the beginning of each time step of integration
default = 1
Not calling it (flag = 0) will be faster to compute but will create small errors in the results
Options related to waypoints: force integrator to stop at fixed time
flag_waypoint : int
1 to use waypoints
0 otherwise
default =0
flag_solout_wp : int
1 to call solout only at required waypoints (only if flag_waypoint activated)
default = 0
delta_t_wp : float
time interval between two waypoints [s]
default = 1.0e-3
nmax : int
maximal number of steps [-]
default = 1e9
toler : float
mixed error tolerances [-]
default = 1.0e-3
rtoler : float
relative error tolerances [-]
default = 1.0e-3
atoler : float
absolute error tolerances [-]
default = 1.0e-6
dt_max : float
maximal time step [s]
default = 1.0e-3
n_freeze : int
number of time step when the jacobian is freezed (computed once at the start of the n_freeze time steps)
default = 0
"""
for key, value in kwargs.items():
if key == "t0":
self.mbs_dirdyn_ptr.contents.options.contents.t0 = value
elif key == "tf":
self.mbs_dirdyn_ptr.contents.options.contents.tf = value
elif key == "dt0":
self.mbs_dirdyn_ptr.contents.options.contents.dt0 = value
elif key == "save2file":
self.mbs_dirdyn_ptr.contents.options.contents.save2file = value
elif key == "resfilename":
self.mbs_dirdyn_ptr.contents.options.contents.resfilename = value.encode('utf-8')
elif key == "respath":
self.mbs_dirdyn_ptr.contents.options.contents.respath = value.encode('utf-8')
elif key == "animpath":
self.mbs_dirdyn_ptr.contents.options.contents.animpath = value.encode('utf-8')
elif key == "save_anim":
self.mbs_dirdyn_ptr.contents.options.contents.save_anim = value
elif key == "save_visu":
self.mbs_dirdyn_ptr.contents.options.contents.save_visu = value
elif key == "framerate":
self.mbs_dirdyn_ptr.contents.options.contents.framerate = value
elif key == "saveperiod":
self.mbs_dirdyn_ptr.contents.options.contents.saveperiod= value
elif key == "max_save_user":
self.mbs_dirdyn_ptr.contents.options.contents.max_save_user = value
elif key == "buffersize":
self.mbs_dirdyn_ptr.contents.options.contents.buffersize = value
elif key == "realtime":
self.mbs_dirdyn_ptr.contents.options.contents.realtime = value
elif key == "accelred":
self.mbs_dirdyn_ptr.contents.options.contents.accelred = value
elif key == "flag_compute_Qc":
self.mbs_dirdyn_ptr.contents.options.contents.flag_compute_Qc = value
elif key == "compute_Qc":
if (type(value) is np.ndarray) or (type(value) is list):
if np.size(value) == self.mbs.mbs_data_ptr.contents.njoint + 1:
for i, val in enumerate(value[1:]):
self.mbs_dirdyn_ptr.contents.options.contents.compute_Qc[i+1] = val
else:
print(">>DIRDYN>> The size of compute_Qc is not consistent with njoint")
else:
print(">>DIRDYN>> The specified value for compute_Qc is not a list or a np.array")
elif key == "integrator":
if (type(value) is int) and (value>=0 and value<=8):
self.mbs_dirdyn_ptr.contents.options.contents.integrator = value
elif type(value) is str:
if value == "RK4":
self.mbs_dirdyn_ptr.contents.options.contents.integrator = 0
elif value == "Dopri5":
self.mbs_dirdyn_ptr.contents.options.contents.integrator = 1
elif value == "Rosenbrock":
self.mbs_dirdyn_ptr.contents.options.contents.integrator = 2
elif value == "EulerEx":
self.mbs_dirdyn_ptr.contents.options.contents.integrator = 3
elif value == "Eulaire":
self.mbs_dirdyn_ptr.contents.options.contents.integrator = 4
elif value == "EulerIm":
self.mbs_dirdyn_ptr.contents.options.contents.integrator = 5
elif value == "Bader":
self.mbs_dirdyn_ptr.contents.options.contents.integrator = 6
elif value == "WMethods":
self.mbs_dirdyn_ptr.contents.options.contents.integrator = 7
elif value == "Custom":
self.mbs_dirdyn_ptr.contents.options.contents.integrator = 8
else:
print(">>DIRDYN>> " + value + " is not an integrator")
else:
print(">>DIRDYN>> " + value + " is not an integrator")
elif key == "verbose":
self.mbs_dirdyn_ptr.contents.options.contents.verbose = value
elif key == "flag_stop_stiff":
self.mbs_dirdyn_ptr.contents.options.contents.flag_stop_stiff = value
elif key == "flag_precise_dynamics":
self.mbs_dirdyn_ptr.contents.options.contents.flag_precise_dynamics = value
elif key == "flag_waypoint":
self.mbs_dirdyn_ptr.contents.options.contents.flag_waypoint = value
elif key == "flag_solout_wp":
self.mbs_dirdyn_ptr.contents.options.contents.flag_solout_wp= value
elif key == "delta_t_wp":
self.mbs_dirdyn_ptr.contents.options.contents.delta_t_wp = value
elif key == "nmax":
self.mbs_dirdyn_ptr.contents.options.contents.nmax = value
elif key == "toler":
self.mbs_dirdyn_ptr.contents.options.contents.toler = value
elif key == "rtoler":
self.mbs_dirdyn_ptr.contents.options.contents.rtoler = value
elif key == "atoler":
self.mbs_dirdyn_ptr.contents.options.contents.atoler = value
elif key == "dt_max":
self.mbs_dirdyn_ptr.contents.options.contents.dt_max = value
elif key == "n_freeze":
self.mbs_dirdyn_ptr.contents.options.contents.n_freeze = value
else:
print(">>DIRDYN>> The option "+key+ " is not defined in this module")
def get_options(self, *args):
"""
Get the specified options for Dirdyn module
Parameters
----------
The different options specifed in the documentation of set_options
Returns
-------
The value of the different options specifed in the documentation of set_options
"""
options = []
for key in args:
if key == "t0":
options.append(self.mbs_dirdyn_ptr.contents.options.contents.t0)
elif key == "tf":
options.append(self.mbs_dirdyn_ptr.contents.options.contents.tf)
elif key == "dt0":
options.append(self.mbs_dirdyn_ptr.contents.options.contents.dt0)
elif key == "save2file":
options.append(self.mbs_dirdyn_ptr.contents.options.contents.save2file)
elif key == "resfilename":
options.append(ctypes.string_at(self.mbs_dirdyn_ptr.contents.options.contents.resfilename).decode("utf-8"))
elif key == "respath":
options.append(ctypes.string_at(self.mbs_dirdyn_ptr.contents.options.contents.respath).decode("utf-8"))
elif key == "animpath":
options.append(ctypes.string_at(self.mbs_dirdyn_ptr.contents.options.contents.animpath).decode("utf-8"))
elif key == "save_anim":
options.append(self.mbs_dirdyn_ptr.contents.options.contents.save_anim)
elif key == "save_visu":
options.append(self.mbs_dirdyn_ptr.contents.options.contents.save_visu)
elif key == "framerate":
options.append(self.mbs_dirdyn_ptr.contents.options.contents.framerate)
elif key == "saveperiod":
options.append(self.mbs_dirdyn_ptr.contents.options.contents.saveperiod)
elif key == "max_save_user":
options.append(self.mbs_dirdyn_ptr.contents.options.contents.max_save_user)
elif key == "buffersize":
options.append(self.mbs_dirdyn_ptr.contents.options.contents.buffersize)
elif key == "realtime":
options.append(self.mbs_dirdyn_ptr.contents.options.contents.realtime)
elif key == "accelred":
options.append(self.mbs_dirdyn_ptr.contents.options.contents.accelred)
elif key == "flag_compute_Qc":
options.append(self.mbs_dirdyn_ptr.contents.options.contents.flag_compute_Qc)
elif key == "compute_Qc":
compute_Qc_py = np.ctypeslib.as_array(self.mbs_dirdyn_ptr.contents.options.contents.compute_Qc, (self.mbs.mbs_data_ptr.contents.njoint+1,))
options.append(compute_Qc_py)
elif key == "integrator":
if self.mbs_dirdyn_ptr.contents.options.contents.integrator == 0:
options.append("RK4")
elif self.mbs_dirdyn_ptr.contents.options.contents.integrator == 1:
options.append("Dopri5")
elif self.mbs_dirdyn_ptr.contents.options.contents.integrator == 2:
options.append("Rosenbrock")
elif self.mbs_dirdyn_ptr.contents.options.contents.integrator == 3:
options.append("EulerEx")
elif self.mbs_dirdyn_ptr.contents.options.contents.integrator == 4:
options.append("Eulaire")
elif self.mbs_dirdyn_ptr.contents.options.contents.integrator == 5:
options.append("EulerIm")
elif self.mbs_dirdyn_ptr.contents.options.contents.integrator == 6:
options.append("Bader")
elif self.mbs_dirdyn_ptr.contents.options.contents.integrator == 7:
options.append("WMethods")
elif self.mbs_dirdyn_ptr.contents.options.contents.integrator == 8:
options.append("Custom")
elif key == "verbose":
options.append(self.mbs_dirdyn_ptr.contents.options.contents.verbose)
elif key == "flag_stop_stiff":
options.append(self.mbs_dirdyn_ptr.contents.options.contents.flag_stop_stiff)
elif key == "flag_precise_dynamics":
options.append(self.mbs_dirdyn_ptr.contents.options.contents.flag_precise_dynamics)
elif key == "flag_waypoint":
options.append(self.mbs_dirdyn_ptr.contents.options.contents.flag_waypoint)
elif key == "flag_solout_wp":
options.append(self.mbs_dirdyn_ptr.contents.options.contents.flag_solout_wp)
elif key == "delta_t_wp":
options.append(self.mbs_dirdyn_ptr.contents.options.contents.delta_t_wp)
elif key == "nmax":
options.append(self.mbs_dirdyn_ptr.contents.options.contents.nmax)
elif key == "toler":
options.append(self.mbs_dirdyn_ptr.contents.options.contents.toler)
elif key == "rtoler":
options.append(self.mbs_dirdyn_ptr.contents.options.contents.rtoler)
elif key == "atoler":
options.append(self.mbs_dirdyn_ptr.contents.options.contents.atoler)
elif key == "dt_max":
options.append(self.mbs_dirdyn_ptr.contents.options.contents.dt_max)
elif key == "n_freeze":
options.append(self.mbs_dirdyn_ptr.contents.options.contents.n_freeze)
else:
print(">>DIRDYN>> The option "+key+ " is not defined in this module")
return tuple(options)
#==========================================================================
# Defining properties
......@@ -326,4 +638,4 @@ class MbsDirdyn(object):
@property
def mbs_name(self):
return self.mbs.mbs_name
\ No newline at end of file
return self.mbs.mbs_name
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment