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())


0 2 (19.689928571048856-397.6540794590543j)
0.9999999999995848
0 125 (19.689928571048856+397.6540794590543j)
5.207878623086054e-14
1 125 (-0.0784554113569564-203.79946384833755j)
0.9999994329229642
63 2 (-0.0784554113569564+203.79946384833755j)
1.3966372617458553e-11
63 125 (-20.053667081540922-202.80972578960876j)
0.9999937440108351
1 2 (-20.053667081540922+202.80972578960876j)
1.0442065253424116e-07
(0.009799623993600355+6.938893903907228e-18j) (9.369241493750735e-15+4.004014165409091e-16j)

In [37]:
tw_fft[0, 0]


Out[37]:
(2.5730941811472495e-05+0j)

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, '.')


* 1 126 (-401.42512568170434-9.778261531377852j)
  63 1 (-401.42512568170434+9.778261531377852j)
    abs() = 401.54420171028266
* 63 126 (400.4463944128781-29.61145432067587j)
  1 1 (400.4463944128781+29.61145432067587j)
    abs() = 401.5397278293392
* 62 1 (8.646221807170715-0.6425134261126614j)
  2 126 (8.646221807170715+0.6425134261126614j)
    abs() = 8.670061997559733
* 62 126 (-8.604007163611655+1.0611160133089181j)
  2 1 (-8.604007163611655-1.0611160133089181j)
    abs() = 8.669192953509645
* 61 1 (4.824944201223073-0.5937347707408168j)
  3 126 (4.824944201223073+0.5937347707408168j)
    abs() = 4.861338038328832
* 3 1 (-4.785035471622752-0.8260291930993335j)
  61 126 (-4.785035471622752+0.8260291930993335j)
    abs() = 4.855809787516425
* 63 2 (4.290021284188578+0.0015880759870444032j)
  1 125 (4.290021284188578-0.0015880759870444032j)
    abs() = 4.290021578124795
* 63 125 (-4.269051799758564+0.4220387874469599j)
  1 2 (-4.269051799758564-0.4220387874469599j)
    abs() = 4.289862469489149
* 4 126 (3.405284271683408+0.5862590931102543j)
  60 1 (3.405284271683408-0.5862590931102543j)
    abs() = 3.4553814109630294
* 60 126 (-3.367696097901838+0.7570067925836736j)
  4 1 (-3.367696097901838-0.7570067925836736j)
    abs() = 3.451729463883444
Out[36]:
[<matplotlib.lines.Line2D at 0x7f9fc02447b8>]

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)


use frequence pairs 19.689929-397.654079i and 19.689929+397.654079i at (0, 2) and (0, 125)
absolute abs of imag part is 4.4909067849407265e-17
/anaconda3/lib/python3.6/site-packages/scipy/optimize/minpack.py:795: OptimizeWarning: Covariance of the parameters could not be estimated
  category=OptimizeWarning)
use frequence pairs -0.078455-203.799464i and -0.078455+203.799464i at (1, 125) and (63, 2)
absolute abs of imag part is 3.534187274843275e-17
use frequence pairs -20.053667-202.809726i and -20.053667+202.809726i at (63, 125) and (1, 2)
absolute abs of imag part is 3.151368595413928e-17
use frequence pairs -0.572063+7.694662i and -0.572063-7.694662i at (0, 3) and (0, 124)
absolute abs of imag part is 7.566584181528067e-19
/anaconda3/lib/python3.6/site-packages/scipy/optimize/minpack.py:795: OptimizeWarning: Covariance of the parameters could not be estimated
  category=OptimizeWarning)
use frequence pairs 19.689929-397.654079i and 19.689929+397.654079i at (0, 2) and (0, 125)
use frequence pairs -0.078455-203.799464i and -0.078455+203.799464i at (1, 125) and (63, 2)
use frequence pairs -20.053667-202.809726i and -20.053667+202.809726i at (63, 125) and (1, 2)
absolute abs of imag part is 1.1882220855160698e-16
Out[26]:
True

In [ ]:

obtain gijk (hijk) from ux, uy, and uz (wx, wy, and wz)


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)


[[-8.17946192e-09  5.55590119e-10  1.78707389e-06]
 [ 2.03683951e-09  2.83163306e-06  8.24410317e-06]
 [ 1.72933314e-09  3.98491753e-08 -4.75103931e-04]
 [-4.75044988e-06  7.99701432e-01 -2.32668153e-10]
 [-7.99333058e-01  3.13973823e-07  7.74879213e-10]]
[[-8.17946192e-09  5.55590119e-10  1.78707389e-06]
 [ 2.03683951e-09  2.83163306e-06  8.24410317e-06]
 [ 1.72933314e-09  3.98491753e-08 -4.75103931e-04]
 [-4.75044988e-06  7.99701432e-01 -2.32668153e-10]
 [-7.99333058e-01  3.13973823e-07  7.74879213e-10]]

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)


0.0002573706176153129
0.0007904006659624763
0.000709647864990684