Skip to content

Instantly share code, notes, and snippets.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@yangyushi
yangyushi / draw_many_fish.py
Created August 24, 2020 15:50
draw 50 fish in 3D using Matplotlib
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.path import Path
from matplotlib.patches import PathPatch
import mpl_toolkits.mplot3d.art3d as art3d
positions = np.random.random((3, 50)) * 10
fig = plt.figure()
@yangyushi
yangyushi / list_comprehension_wat.py
Created August 23, 2020 11:15
if you try to use nested for loop in a list comprehension in python, wat
A = [1, 2, 3, 4]
B = ['a', 'b', 'c']
# saw this version on a Coursera Course
# If you actually use this crappy style, wat
print([a for b in B for a in A])
# Equivilant
result = []
for b in B:
@yangyushi
yangyushi / interface.py
Last active August 7, 2020 21:35
get the interface along x-axis for 2D active matter system. Avoid internal bubbles. Very cool.
import re
import freud
import numpy as np
import networkx as nx
from io import StringIO
from scipy.spatial.distance import squareform, pdist
def should_join(p1, p2):
return 0 in pdist(np.concatenate((p1, p2))[:, None])
@yangyushi
yangyushi / cr_ideal.py
Last active July 7, 2020 19:25
The negative connected correlation function because of the momentum conservation
import numpy as np
from scipy.spatial.distance import pdist
import matplotlib.pyplot as plt
from scipy.stats import binned_statistic
from numba import njit
@njit
def pairwise_dot(x):
"""
@yangyushi
yangyushi / pdist_pbc.py
Created July 6, 2020 15:09
pairwise distance calculation for n-dimensional coordinates
import numpy as np
from scipy.spatial.distance import pdist, squareform
import matplotlib.pyplot as plt
def pdist_pbc(positions, box):
"""
Get the pair-wise distances of particles in a priodic boundary box
Args:
@yangyushi
yangyushi / solve.py
Created June 26, 2020 14:18
introductory of linear programming in using python or Pulp
"""
This module is a demonstartion of using scipy to solve LP problem
following this tutorial: https://realpython.com/linear-programming-python
The problem os as following
maximize z = x + 2 y, same as minimizing -z = -x - 2y
subject to 2x + y <= 20
-4x + 5y <= 10
-x + 2y >= -2, same as x - 2y <= 2 for scipy
import numpy as np
import matplotlib.pyplot as plt
from scipy import ndimage
from matplotlib.pyplot import plot, scatter, imshow
def simulate_2d(N):
theta = np.random.uniform(-np.pi, np.pi, (1000, N))
x = np.cos(theta).mean(-1) # shape (1000,)
y = np.sin(theta).mean(-1) # shape (1000,)
order = np.linalg.norm((x, y), axis=0)
@yangyushi
yangyushi / tcc_analysis.ipynb
Last active June 12, 2020 10:39
demonstration on using TCC to analyse a LAMMPS dumped file
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@yangyushi
yangyushi / xyz.py
Last active July 4, 2020 09:52
get all the frames from an xyz file
import numpy as np
import re
def get_frames_from_xyz(filename, use_cols):
"""
Get all data from different frames from an xyz file
Args:
filename (str): the path of the xyz file to parse