<jupyter_start><jupyter_text>Load hdf file and post-processFor a given model (as guitar_model above), set internal data using hdf5 input. Useful for post-processing only. Prerequisite : a complete simulation saved in hdf file (e.g. results of run.py) Usage: * call load_model function : create a model (dynamical system, interactions and so on) and load results from a previous simu from hdf file* call post-processing tools<jupyter_code># Reload automatically all python modules before each cell exec
%load_ext autoreload
%autoreload 2
# standard python packages
import sys
import time
import os
import pickle
import numpy as np
from model_tools import load_model
# visu
import matplotlib.pyplot as plt
%matplotlib inline
import scipy.io
import h5py
import simulation_campaigns
import collections
import pickle
import subprocess
#pklfilename = 'campaign_1812.pkl'
pklfilename = 'campaign_2018.pkl'
#pkl_file = open('campaign_new_0612.pkl', 'rb')
pkl_file = open(pklfilename, 'rb')

remote_data = pickle.load(pkl_file)
for name in remote_data:
    print("Campaign name : " + name)
    if name.find('results') < 0:
        print("Available freqs:")
        print(list(remote_data[name].keys()))
        print("\n")<jupyter_output>Campaign name : bass_2018_0.0
Available freqs:
['62720', '4014080', '2007040', '125440', '1003520', '31360', '15680', '250880', '501760', '64225280', '16056320', '8028160', '256901120', '32112640', '128450560']


Campaign name : results_paths
Campaign name : bass_2018_0.9
Available freqs:
['62720', '15680', '16056320', '8028160', '1003520', '125440', '2007040', '501760', '256901120', '128450560', '250880', '31360', '64225280', '32112640', '4014080']


Campaign name : bass_2018_1.0
Available freqs:
['8028160', '250880', '1003520', '15680', '125440', '64225280', '501760', '128450560', '2007040', '4014080', '32112640', '31360', '62720', '256901120', '16056320']


Campaign name : fretless_2018_0.0
Available freqs:
['15680', '62720', '31360', '250880', '1003520', '125440', '16056320', '501760', '2007040', '4014080', '8028160', '64225280', '32112640']


Campaign name : fretless_2018_0.9
Available freqs:
['1003520', '125440', '250880', '16056320', '32112640', '62720', '15680', '8028160', '313[...]<jupyter_text>Select input file(s) and load model(s)<jupyter_code>import scipy.io as sio
res = sio.loadmat('one_contact/guitare_obst0_frequs.mat')
sig = sio.loadmat('one_contact/guitare_obst0_amortissements.mat')
hf = sio.loadmat('one_contact/guitare_obst0_h.mat')
def load(filename):
    if filename.find('bass') >= 0:
        matlab_input = './bass_guitar/pb2'
    elif filename.find('fretless') >= 0:
        matlab_input = './fretless_bass_guitar/bsf'
    elif filename.find('guitar_single') >=0 :
        matlab_input = './one_contact/guitare_obst0'
    elif filename.find('single') >=0 :
        matlab_input = './one_contact/pb1'

    m, s, f, e = load_model(filename)
    frets_file = matlab_input + '_h.mat'
    all_frets_positions = scipy.io.loadmat(frets_file)['h'][:, 0]
    print(m.modal_values)
    #if filename.find('converted') >= 0:
    #    m._convert[...]=False
    #    h5source = h5py.File(filename, 'r')
    #    restit = h5source.attrs['restit']
    #    h5source.close()
    #else:
    #    m.convert_modal_output(s)
    #    restit = None
    return m, s, f, all_frets_positions, e#, restit
camp = 'bass_one'
names = [name for name in remote_data.keys() if name.find(camp) >=0]
print(names)
index = 0
campaign = remote_data[names[index]]
results_path = remote_data["results_paths"][1]
fileslist = {}
for freq in campaign:
    fileslist[np.float(freq)] = os.path.join(results_path, campaign[freq][5])
fileslist = collections.OrderedDict(sorted(fileslist.items()))
print(names[index])
print('select one of the available freqs:')
for freq in fileslist:
    print(freq)
selected_freq = 4014080.0
print(fileslist[selected_freq])
reference_file = fileslist[selected_freq]
extra_freqs = [31360.0 ,62720.0, 125440.0, 250880.0,501760.0,  1003520.0, 2007040.0]
#5000.0, 100000.0, 10000000.0]
#extra_freqs = [fr for fr in fileslist if fr != selected_freq]#[10000.0, 10000000.0]

extra_files = {}
models, strings, frets = {}, {}, {}

guitar_model, guitar_string, guitar_frets, frets_pos, restit = load(reference_file)
for freq in extra_freqs:
    models[freq], strings[freq], frets[freq], fpos, restit =  load(fileslist[freq])

fig_path = 'figs_bass_onec'
if not os.path.exists(fig_path):
    os.mkdir(fig_path)<jupyter_output>Load model from file ./results_bass_2018/F_4014080_id_4915266/converted_guitar_single_e0.0_1001_4014080.h5
False
Load model from file ./results_bass_2018/F_31360_id_4915259/converted_guitar_single_e0.0_1001_31360.h5
False
Load model from file ./results_bass_2018/F_62720_id_4915260/converted_guitar_single_e0.0_1001_62720.h5
False
Load model from file ./results_bass_2018/F_125440_id_4915261/converted_guitar_single_e0.0_1001_125440.h5
False
Load model from file ./results_bass_2018/F_250880_id_4915262/converted_guitar_single_e0.0_1001_250880.h5
False
Load model from file ./results_bass_2018/F_501760_id_4915263/converted_guitar_single_e0.0_1001_501760.h5
False
Load model from file ./results_bass_2018/F_1003520_id_4915264/converted_guitar_single_e0.0_1001_1003520.h5
False
Load model from file ./results_bass_2018/F_2007040_id_4915265/converted_guitar_single_e0.0_1001_2007040.h5
False<jupyter_text>Plot trajectoriesUse plotTraj(dynamical_system, list of dof to be plotted)If list of dof is not set, all dof will be plotted.Result = dof as a function of time +  zoom specific ranges (as in the paper)To save the resulting figure(s), add argument filename when calling plotTrajArg 'iplot' is used to change figure number. Set same iplot if you want to plot all curves on the same figure.Default iplot=0.<jupyter_code>inter = guitar_frets[0]
inter.contact_index
guitar_model.time_step

fig_path = './fig_campaign_2018/one_contact/'
print(fig_path)
# Select dof to plot traj
dof = 990
#ground = frets_pos[dof]#- guitar_string.diameter*0.5
#print(ground)
freq = guitar_model.fs
filename = os.path.join(fig_path, 'traj_' + str(freq) + '_x=' + str(dof) + '.pdf')
guitar_model.plot_traj(guitar_string, dof=dof, iplot=0, filename=filename)
#m2.plot_traj(s2, dof=dof, iplot=0)#, filename=filename)

#current_model.plot_traj(current_string, dof=dof, iplot=0)#, filename=filename)
#m3.plot_traj(s3, dof=dof, iplot=0)#, filename=filename)


#dof = 9
#ground = frets_pos[dof]#- guitar_string.diameter*0.5
#filename = os.path.join(fig_path, 'traj_' + str(freq) + '_x=' + str(dof) + '.pdf')
#guitar_model.plot_traj(guitar_string, dof=dof, iplot=1, filename=filename)#, ground=ground)
#guitar_model2.plot_traj(guitar_string2, dof=dof, iplot=0, )#, ground=ground
ticks = ['x--', '^:', 'o--', 's', '*--', '--', ':'] * 10
ticks = ['--', ':', '--', '--', '--', '--', ':'] * 10
leg =[]


traj_ref = guitar_model.data_ds[guitar_string][990,:]
plt.plot(guitar_model.time, traj_ref,  ':k')
leg.append('ref')
i = 1
for freq in models:
    traj = models[freq].data_ds[strings[freq]][990,:]
    time = models[freq].time
    #plt.plot(time, (traj-traj_ref), ticks[i])
    leg.append(freq)
    plt.plot(time, traj, ticks[i])
    i += 1
plt.legend(leg)
plt.xlim(1.45,1.55)
plt.ylim(0.017,0.019)
filename = os.path.join(fig_path, 'traj_convergence_zoom.pdf')
plt.savefig(filename)
#plt.xlim(2.97, 3.05)
#plt.ylim(0.014, 0.02)
inter = guitar_model.data_interactions
ic = 0
time = guitar_model.time
y = inter[guitar_frets[ic]][0][:]
#ydot = inter[frets[ic]][:,1]
plt.figure()
for ic in range(len(guitar_frets)):
    lamb = inter[guitar_frets[ic]][1][:]
    #print(np.where(lamb >=1e-22))
    plt.plot(time, lamb, 'o')


    
#plt.plot(time, ydot,'--x')
plt.xlim(0,0.05)
#plt.ylim(-10,10)
ind = np.where(y<0)
ind2 = np.where(y>=0)

plt.figure(44)
#plt.clf()
plt.plot(time[ind2], y[ind2], 'k:o', time[ind], y[ind], 'rx:', )
plt.xlim(0.003, 0.009)
#plt.figure()

#plt.plot(time, lamb, 'k:o')<jupyter_output><empty_output><jupyter_text>Plot modesUse guitar_model.plot_modes(guitar_string,plot_shape)plot_shape = (x,y) where x,y is the resolution of the grid of plots.Example: plot_modes(guitar_string, (4,2)) : splits time range in 8 and plots dof=f(x) for those 8 time instants on 8 figuresSet filename arg to save resulting figure in a file.<jupyter_code># Plot 10 figures on two columns, for 10 time instants
guitar_model.plot_modes(guitar_string,plot_shape=[5,2], iplot=0)
plt.savefig(os.path.join(fig_path, 'modes.pdf'))
# Same thing but save output in modes.pdf
#guitar_model.plot_modes(guitar_string,plot_shape=[5,3], filename='modes.pdf', iplot=1)
# plot modes for time[12] and time[5000] on one column
#guitar_model.plot_modes(guitar_string, times =[12, 1200], plot_shape=(2,1), filename='modes2.pdf', iplot=2)<jupyter_output><empty_output><jupyter_text>Plot contactogram    !! Works only if enable_interactions_output=True in model (guitar_model) constructor<jupyter_code>guitar_model.contactogram(guitar_string, 33)
plt.savefig(os.path.join(fig_path, 'contacto.pdf'))
guitar_model.nb_time_steps_output<jupyter_output><empty_output><jupyter_text>Create a movie mode=f(time)<jupyter_code>moviename = os.path.basename(reference_file)
moviename = os.path.join(fig_path, os.path.splitext(moviename)[0] + '.mp4')
print(moviename)
#moviename = 'tmp.mp4'
guitar_model.make_movie(guitar_string, moviename)
if os.path.exists(('./last.mp4')):
    os.remove('./last.mp4')
os.symlink(moviename, 'last.mp4')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1479683377308707, 'fraction_numerical': 0.10385224274406332, 'mean_word_length': 5.749287749287749, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '56956', 'license': 'Apache-2.0', 'n_tokens_mistral': 4330, 'n_tokens_neox': 3701, 'n_words': 763, 'path': 'examples/mechanics/Music/one-contact-postprocessing.ipynb', 'repo_name': 'vacary/siconos-tutorials', 'size': '9475'}