/
simulation.py
89 lines (63 loc) · 3.01 KB
/
simulation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import os
# 3rd party imports
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
# uraeus imports
from uraeus.nmbd.python import (import_source, multibody_system,
simulation, configuration)
# ================================================================== #
# Helpers
# ================================================================== #
# getting to the root project directory from this file directory
dir_name = os.path.dirname(__file__)
# creating the various needed directories references
project_dir = os.path.abspath(os.path.join(dir_name, '../../'))
symdata_dir = os.path.join(project_dir, 'symenv/data/')
numdata_dir = os.path.join(project_dir, 'numenv/python/src/')
results_dir = os.path.join(project_dir, 'simenv/results/')
# ================================================================== #
# Initializations
# ================================================================== #
# TODO : User Declared
model_name = MODEL_NAME
# getting the configuration .json file
# TODO : User Declared
config_file = os.path.join(symdata_dir, 'MODEL_NAME_cfg.json')
# Creating a numerical configuration instance
num_config = configuration('base')
# constructing the numerical configuration instance from
# imported JSON file
num_config.construct_from_json(config_file)
# Getting the numrical topology module
model = import_source(numdata_dir, model_name)
# Creating the numerical model from the imported module
num_model = multibody_system(model)
# Assigning this configuration instance to the numerical model
num_model.topology.config = num_config
# ================================================================== #
# Numerical Configuration of the System
# ================================================================== #
# Assembling the configuration and exporting a .json file that
# holds these numerical values
num_config.assemble()
num_config.export_json()
# ================================================================== #
# Creating the Simulation Instance
# ================================================================== #
sim = simulation('sim', num_model, 'dds')
# setting the simulation time grid
sim.set_time_array(20, 5e-3)
# Starting the simulation
sim.solve()
# Saving the results in the /results directory as csv and npz
sim.save_as_csv(results_dir, 'test_1')
sim.save_as_npz(results_dir, 'test_1')
# ================================================================== #
# Plotting the Simulation Results
# ================================================================== #
# TODO : User Declared
#sim.soln.pos_dataframe.plot(x='time', y=['rbs_body.z', 'rbs_body.y'], grid=True, figsize=(10,4))
#sim.soln.vel_dataframe.plot(x='time', y='rbs_body.z', grid=True, figsize=(10,4))
#sim.soln.acc_dataframe.plot(x='time', y='rbs_body.z', grid=True, figsize=(10,4))
plt.show()