Skip to content

Instantly share code, notes, and snippets.

View Ionizing's full-sized avatar
😵
Being defeated by DALAOs

Ionizing Ionizing

😵
Being defeated by DALAOs
  • Mars
View GitHub Profile
@Ionizing
Ionizing / gpaw_coulomb_correction.py
Created October 15, 2023 13:50
First order and second order coulomb correction for PAW method, extracted from GPAW.
#!/usr/bin/env python3
import gzip
from xml.etree import ElementTree as ET
from glob import glob
import numpy as np
import numpy.typing as npt
@Ionizing
Ionizing / overloading.rs
Created July 26, 2023 15:48
Function overloading in Rust
// Credits to (Telegram):
// - @QC_Grove (zh) | blog.quarticcat.com (en/zh)
// - @bdbai_chat
trait Foo<T, U> {
type Output;
fn foo(a: T, b: U) -> Self::Output;
}
impl Foo<i32, f64> for () {
@Ionizing
Ionizing / str2fn_nom.rs
Last active May 26, 2023 09:08
Parse string and return an Fn object
use std::fmt;
use std::fmt::{Debug, Display, Formatter};
use nom::{
branch::alt,
bytes::complete::tag,
character::complete::multispace0 as multispace,
number::complete::double,
combinator::map,
multi::many0,
@Ionizing
Ionizing / incbin.c
Last active November 27, 2024 05:21 — forked from mmozeiko/incbin.c
Include binary file with gcc/clang
#define STR2(x) #x
#define STR(x) STR2(x)
#ifdef __APPLE__
#define USTR(x) "_" STR(x)
#else
#define USTR(x) STR(x)
#endif
#ifdef _WIN32
@Ionizing
Ionizing / semver_parse.sh
Last active December 5, 2022 13:33
Parse the sematic version string with bash
# Let VER_STR be the version string
VER_STR="v1.14.514-rc-v1.919.810-tnok.."
# Use `sed` to extract the MAJOR, MINOR, PATCH number
echo $VER_STR | sed -nE 's/^v([0-9]+).([0-9]+).([0-9]+)(.*)$/\1 \2 \3/p'
# prints "1 14 514"
# Convert "1 14 514" to an array of ["1", "14", "514"]
VER=($(echo $VER_STR | sed -nE 's/^v([0-9]+).([0-9]+).([0-9]+)(.*)$/\1 \2 \3/p'))
echo ${VER[0]} # prints 1
@Ionizing
Ionizing / ising2d.jl
Created November 23, 2022 09:20
Simulating 2D Ising model.
#!/usr/bin/env julia
using Printf: @printf;
function bc(x, L)
mod(x-1, L) + 1;
end
function totalEnergy(grid ::Matrix{Int64}) ::Float64
(x, y) = size(grid);
@Ionizing
Ionizing / Makefile
Created November 6, 2022 06:53
Benchmark of linear search and binary search, implemented in Fortran.
FC = gfortran
FFLAGS = -O3 -fbounds-check -Wall -Wextra -ffree-form -ffree-line-length-none
test: main.x
./$<
main.x: main.f90
$(FC) -o $@ $^ $(FFLAGS)
clean:
@Ionizing
Ionizing / expm.f90
Created November 1, 2022 13:12
Calculate the matrix exponential "exp(H)" in fortran, lapack(or openblas) needed.
program main
use lapack_interfaces
implicit none
integer :: n = 2
complex(8) :: h(2, 2)
integer :: lda = 2
real(8) :: w(2)
complex(8) :: work(3)
@Ionizing
Ionizing / input.nml
Created October 27, 2022 16:01
Example to show fortran with lapack to solve eigenvalue problem.
&meta
ndim = 4
/
&matrix
H(1,:) = (1, 0), (2, 0), (3, 0), (1, 0),
H(2,:) = (3, 0), (2, 0), (9, 0), (1, 0),
H(3,:) = (8, 0), (2, 0), (2, 0), (1, 0),
H(4,:) = (9, 0), (2, 0), (0, 0), (1, 0),
/
@Ionizing
Ionizing / spin_density.py
Last active May 31, 2023 02:30
Calculate spin density and save it to SPIN_DENSITY.vasp
#!/usr/bin/env python3
from ase.calculators.vasp import VaspChargeDensity
print("READING CHGCAR ...")
chgcar = VaspChargeDensity("CHGCAR")
assert chgcar.is_spin_polarized()
if len(chgcar.chgdiff) == 1:
spin_density = chgcar.chgdiff[0].copy()