Skip to content

Instantly share code, notes, and snippets.

@yangyushi
yangyushi / check_tracking_example.py
Last active August 31, 2020 14:58
Handy function to check 3D particle tracking results
import numpy as np
import matplotlib.pyplot as plt
from scipy import ndimage
from matplotlib.pyplot import plot, scatter, imshow
def get_fake(size, number, radius, blur):
"""
Generate a fake image for ideal gas (random positions).
@yangyushi
yangyushi / neighbour_list.py
Last active April 3, 2020 18:07
Algorithm of neighbour list introduced in book "computer simulation of liquids" chapter 5
#!/usr/bin/env python3
import math
from itertools import product
import numpy as np
from numba import njit
from numba.typed import List
@njit
def build_verlet_nlist(nlist: List, point: List, particles: np.ndarray, rl:float):
@yangyushi
yangyushi / rotation_opt.py
Last active March 31, 2020 12:15
This gist shows a `numba.njit` accelerated code is faster than its `einsum` equivalent for composing/applying a lot of rotation matrices
from time import time
import numpy as np
from numba import njit, prange
@njit(parallel=True)
def ndot1(A, B):
"""
Calculate C with
C[i, j, k] = sum_q( A[i, j, q] · B[i, q, k] )
@yangyushi
yangyushi / parse_xyz.cpp
Last active April 7, 2022 14:55
Parse .xyz file and load to std::vector
#include <iostream>
#include <fstream>
#include <regex>
#include <string>
using XYZ = std::array<double, 3>;
using Coord3D = std::vector<XYZ>;
@yangyushi
yangyushi / random_sphere_surface.py
Last active May 13, 2020 22:56
generate random points on the surface of a sphere
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.pyplot import plot, scatter, imshow
from mpl_toolkits.mplot3d import Axes3D
N = 1000
z = np.random.uniform(-1, 1, N)
phi = np.random.uniform(-np.pi, np.pi, N)
rxy = np.sqrt(1 - z**2)
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
b1 = np.array([
[1, 0, 0],
[0, 1, 0],
@yangyushi
yangyushi / join_pairs.cpp
Last active July 13, 2020 15:19
Join overlapped labels recursively, eg [(2, 3), (3, 5), (2, 6), (8, 9), (9, 10)] → [(2, 3, 5, 6), (8, 9, 10)]
#include <iostream>
#include <vector>
#include <set>
using namespace std;
using Pair = vector<int>;
using Pairs = vector<Pair>;
bool should_join(Pair p1, Pair p2){
for (int n1 : p1){
@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
@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.
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)