In [1]:
# !/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on 20191125
@author: zhangji
test the linear relationship
U_t =?= U_sh + U_wm
U_t is the total velocity
U_sh is the velocity induced by shear flow
U_wm is the active velocity.
"""
# %pylab inline
# pylab.rcParams['figure.figsize'] = (25, 11)
# fontsize = 40
# import numpy as np
# import scipy as sp
# from scipy.optimize import leastsq, curve_fit
# from scipy import interpolate
# from scipy.interpolate import interp1d
# from scipy.io import loadmat, savemat
# # import scipy.misc
# import matplotlib
# from matplotlib import pyplot as plt
# from matplotlib import animation, rc
# import matplotlib.ticker as mtick
# from mpl_toolkits.axes_grid1.inset_locator import inset_axes, zoomed_inset_axes
# from mpl_toolkits.mplot3d import Axes3D, axes3d
# from sympy import symbols, simplify, series, exp
# from sympy.matrices import Matrix
# from sympy.solvers import solve
# from IPython.display import display, HTML
# from tqdm import tqdm_notebook as tqdm
# import pandas as pd
# import re
# from scanf import scanf
# import os
# import glob
# from codeStore import support_fun as spf
# from src.support_class import *
# from src import stokes_flow as sf
# rc('animation', html='html5')
# PWD = os.getcwd()
# font = {'size': 20}
# matplotlib.rc('font', **font)
# np.set_printoptions(linewidth=90, precision=5)
import os
import glob
import re
import pandas as pd
from scanf import scanf
import natsort
import numpy as np
import scipy as sp
from scipy.optimize import leastsq, curve_fit
from scipy import interpolate
from scipy import spatial
# from scipy.interpolate import interp1d
from scipy.io import loadmat, savemat
# import scipy.misc
import importlib
from IPython.display import display, HTML
import pickle
import matplotlib
from matplotlib import pyplot as plt
from matplotlib import colors as mcolors
from matplotlib import animation, rc
import matplotlib.ticker as mtick
from mpl_toolkits.axes_grid1.inset_locator import inset_axes, zoomed_inset_axes
from mpl_toolkits.mplot3d import Axes3D, axes3d
from matplotlib import ticker, cm
from mpl_toolkits.axes_grid1 import make_axes_locatable
from time import time
from src import support_class as spc
from src import jeffery_model as jm
from codeStore import support_fun as spf
from codeStore import support_fun_table as spf_tb
# %matplotlib notebook
%matplotlib inline
rc('animation', html='html5')
rc('text', usetex=True)
params = {'text.latex.preamble': [r'\usepackage{bm}', r'\usepackage{amsmath}']}
plt.rcParams.update(params)
fontsize = 40
figsize = (30, 16)
PWD = os.getcwd()
In [2]:
fig = plt.figure(figsize=(2, 2))
fig.patch.set_facecolor('white')
ax0 = fig.add_subplot(1, 1, 1)
In [3]:
fft_pickle = 'ellipse_alpha3'
In [4]:
# do 2D IFFT myself and compare with numpy version
use_idx_max = 6
# ((psi, ((theta, phi, ui))))
with open('%s.pickle' % fft_pickle, 'rb') as handle:
pickle_data = pickle.load(handle)
ttheta, tphi = pickle_data[0][1][0][:2]
tpsi = np.array([ti[0] for ti in pickle_data])
tw = pickle_data[0][1][3][2].values
tw_fft = np.fft.fft2(tw)
th_freq = np.fft.fftfreq(ttheta.size, 1 / ttheta.size)
ph_freq = np.fft.fftshift(np.fft.fftfreq(tphi.size, 1 / tphi.size))
idx_max = np.dstack(np.unravel_index(np.argsort(np.abs(tw_fft).ravel()), tw_fft.shape))[0][::-1]
# numpy version IFFT
idx = np.zeros_like(tw_fft, dtype=bool)
for ti in idx_max[:use_idx_max]:
idx[ti[0], ti[1]] = 1
tw_fft2 = tw_fft * idx
tw2 = np.fft.ifft2(tw_fft2)
# do IFFT myself
tw3 = np.zeros_like(tw2)
tM, tN = tw_fft.shape
tm, tn = np.meshgrid(np.arange(tM), np.arange(tN), indexing='ij')
for tk, tl in idx_max[:use_idx_max]:
tAkl = tw_fft[tk, tl]
tw3 = tw3 + tAkl * np.exp(2 * np.pi * 1j * (tm * tk / tM + tn * tl / tN)) / (tM * tN)
print(tk, tl, tAkl)
print((np.abs(tw3.imag) / np.abs(tw3)).max())
print((tw3 - tw).max(), (tw3 - tw2).max())
In [37]:
tw_fft[0, 0]
Out[37]:
In [36]:
# print values of primary frequence
print_idx_max = 20
# ((psi, ((theta, phi, ui))))
with open('%s.pickle' % fft_pickle, 'rb') as handle:
pickle_data = pickle.load(handle)
ttheta, tphi = pickle_data[0][1][0][:2]
tpsi = np.array([ti[0] for ti in pickle_data])
tw = pickle_data[0][1][5][2].values
tw_fft = np.fft.fft2(tw)
th_freq = np.fft.fftfreq(ttheta.size, 1 / ttheta.size)
ph_freq = np.fft.fftshift(np.fft.fftfreq(tphi.size, 1 / tphi.size))
idx_max = np.dstack(np.unravel_index(np.argsort(np.abs(tw_fft).ravel()), tw_fft.shape))[0][::-1]
for i0, (tk, tl) in enumerate(idx_max[:print_idx_max]):
if not np.mod(i0, 2):
print('*', tk, tl, tw_fft[tk, tl])
else:
print(' ', tk, tl, tw_fft[tk, tl])
print(' abs() =', np.abs(tw_fft[tk, tl]))
# show the decay of the amplitude in the frequence space.
tabs = [np.abs(tw_fft[tk, tl]) for tk, tl in idx_max]
fig = plt.figure(dpi=200)
fig.patch.set_facecolor('white')
ax0 = fig.add_subplot(1, 1, 1)
ax0.loglog(np.arange(idx_max.shape[0])+1, tabs, '.')
Out[36]:
In [26]:
# 2D fft of Wx, Wy, Wz
importlib.reload(spf_tb)
use_uidx = 3 # 3: wx, 4: wy, 5: wz
tktl_list = [[0, 2], [1, 125], [63, 125], [0, 3]] # [[ki, li], ]
# use_idx_list = [[0], [1, 2], [3, 4], [5, 6]]
# ((psi, ((theta, phi, ui))))
with open('%s.pickle' % fft_pickle, 'rb') as handle:
pickle_data = pickle.load(handle)
ttheta, tphi = pickle_data[0][1][0][:2]
tpsi = np.array([ti[0] for ti in pickle_data])
tw = pickle_data[0][1][use_uidx][2].values
for tktl in tktl_list:
if np.sum(tktl) < 1:
spf_tb.show_fft_major(tw, (tktl, ), ttheta, tphi)
else:
spf_tb.show_fft_fit(tw, tktl, ttheta, tphi)
spf_tb.show_fft_major(tw, tktl_list[:-1], ttheta, tphi)
Out[26]:
In [ ]:
In [4]:
importlib.reload(spf_tb)
fft_pickle = 'ellipse_alpha3'
with open('%s.pickle' % fft_pickle, 'rb') as handle:
pickle_data = pickle.load(handle)
ttheta_all, tphi_all = pickle_data[0][1][0][:2]
tpsi_all = np.array([ti[0] for ti in pickle_data])
U_all = [[] for i in range(6)]
for _, table_psi_data in pickle_data:
for (ttheta, tphi, tU), Ui in zip(table_psi_data, U_all):
Ui.append(tU)
In [23]:
pickle_name = 'ellipsoid_baseFlow'
with open('%s.pickle' % pickle_name, 'rb') as handle:
pickle_dict = pickle.load(handle)
uEbase_list = np.vstack(pickle_dict['uw_Base_list'])[1:6, 0:3]
uSbase_list = np.vstack(pickle_dict['uw_Base_list'])[6:9, 0:3]
wEbase_list = np.vstack(pickle_dict['uw_Base_list'])[1:6, 3:6]
wSbase_list = np.vstack(pickle_dict['uw_Base_list'])[6:9, 3:6]
print(wEbase_list)
print(wEbase_list)
In [12]:
pickle_name = 'ellipsoid_baseFlow'
with open('%s.pickle' % pickle_name, 'rb') as handle:
pickle_dict = pickle.load(handle)
uEbase_list = np.vstack(pickle_dict['uw_Base_list'])[1:6, 0:3]
uSbase_list = np.vstack(pickle_dict['uw_Base_list'])[6:9, 0:3]
wEbase_list = np.vstack(pickle_dict['uw_Base_list'])[1:6, 3:6]
wSbase_list = np.vstack(pickle_dict['uw_Base_list'])[6:9, 3:6]
U_cal_all = [[] for i in range(6)]
uS_cal_list = []
for i0 in range(tpsi_all.size):
tpsi = tpsi_all[i0]
tux = U_all[0][i0]
tuy = U_all[1][i0]
tuz = U_all[2][i0]
tux_cal = tux.copy()
tuy_cal = tuy.copy()
tuz_cal = tuz.copy()
for i1 in range(ttheta_all.size):
ttheta = ttheta_all[i1]
for i2 in range(tphi_all.size):
tphi = tphi_all[i2]
Eij_loc = spf_tb.Eij_loc(ttheta, tphi, tpsi)
Sij_loc = spf_tb.Sij_loc(ttheta, tphi, tpsi)
Ebase_fct = np.array([Eij_loc[0, 0], Eij_loc[2, 2], Eij_loc[0, 1], Eij_loc[0, 2], Eij_loc[1, 2]])
Sbase_fct = np.array([Sij_loc[2, 1], Sij_loc[0, 2], Sij_loc[1, 0]])
uE_loc = np.sum([a * b for a, b in zip(Ebase_fct, uEbase_list)], axis=0)
uE = np.dot(spf_tb.Rrot(ttheta, tphi, tpsi), uE_loc)
uS_loc = np.sum([a * b for a, b in zip(Sbase_fct, uSbase_list)], axis=0)
uS = np.dot(spf_tb.Rrot(ttheta, tphi, tpsi), uS_loc)
uS_cal_list.append(uS)
# uS = np.array((0, 0, 0))
tu_cal = uE + uS
tux_cal.iloc[i1].iloc[i2] = tu_cal[0]
tuy_cal.iloc[i1].iloc[i2] = tu_cal[1]
tuz_cal.iloc[i1].iloc[i2] = tu_cal[2]
U_cal_all[0].append(tux_cal)
U_cal_all[1].append(tuy_cal)
U_cal_all[2].append(tuz_cal)
# print(np.linalg.norm(np.vstack(uS_cal_list) - np.array((0, 0, 0)), axis=-1).max())
wS_cal_list = []
wE_err_list = []
for i0 in range(tpsi_all.size):
tpsi = tpsi_all[i0]
twx = U_all[3][i0]
twy = U_all[4][i0]
twz = U_all[5][i0]
twx_cal = twx.copy()
twy_cal = twy.copy()
twz_cal = twz.copy()
for i1 in range(ttheta_all.size):
ttheta = ttheta_all[i1]
for i2 in range(tphi_all.size):
tphi = tphi_all[i2]
Eij_loc = spf_tb.Eij_loc(ttheta, tphi, tpsi)
Sij_loc = spf_tb.Sij_loc(ttheta, tphi, tpsi)
Ebase_fct = np.array([Eij_loc[0, 0], Eij_loc[2, 2], Eij_loc[0, 1], Eij_loc[0, 2], Eij_loc[1, 2]])
Sbase_fct = np.array([Sij_loc[2, 1], Sij_loc[0, 2], Sij_loc[1, 0]])
wE_loc = np.sum([a * b for a, b in zip(Ebase_fct, wEbase_list)], axis=0)
wE = np.dot(spf_tb.Rrot(ttheta, tphi, tpsi), wE_loc)
# wE2 = wE_fun(ttheta, tphi, tpsi)
# wE_err_list.append(wE - wE2)
wS_loc = np.sum([a * b for a, b in zip(Sbase_fct, wSbase_list)], axis=0)
wS = np.dot(spf_tb.Rrot(ttheta, tphi, tpsi), wS_loc)
wS_cal_list.append(wS)
# wS = np.array((0, 0.5, 0))
tw_cal = wE + wS
twx_cal.iloc[i1].iloc[i2] = tw_cal[0]
twy_cal.iloc[i1].iloc[i2] = tw_cal[1]
twz_cal.iloc[i1].iloc[i2] = tw_cal[2]
U_cal_all[3].append(twx_cal)
U_cal_all[4].append(twy_cal)
U_cal_all[5].append(twz_cal)
# print(np.linalg.norm(np.vstack(wS_cal_list) - np.array((0, 0.5, 0)), axis=-1).max())
# print(np.linalg.norm(np.vstack(wE_err_list), axis=-1).max())
In [26]:
importlib.reload(spf_tb)
for use_uidx in (0, 1, 2, 3, 4, 5):
tu_cal = np.dstack(U_cal_all[use_uidx])
spf_tb.show_ui_psi(tu_cal, ttheta_all, tphi_all, tpsi_all)
In [21]:
importlib.reload(spf_tb)
for use_uidx in (3, 4, 5):
tu = np.dstack(U_all[use_uidx])
tu_cal = np.dstack(U_cal_all[use_uidx])
t1 = tu_cal - tu
print(np.abs(t1).max())
spf_tb.show_ui_psi(t1, ttheta_all, tphi_all, tpsi_all)