Skip to content

Instantly share code, notes, and snippets.

View danshapero's full-sized avatar

Daniel Shapero danshapero

View GitHub Profile
@danshapero
danshapero / photochem.py
Created February 6, 2023 22:00
photochemistry
import numpy as np
from numpy import pi as π
K_1 = 1e-2
k_2 = 1e5
k_3 = 1e-16
t_d = 24 * 60 * 60
def f(c, t):
k_1 = K_1 * max(0, np.sin(2 * π * t / t_d))
@danshapero
danshapero / quadratic-disc-test.py
Last active January 12, 2023 00:18
Test for reading 2nd-order gmsh meshes into PETSc
import subprocess
from petsc4py import PETSc
geo_text = """
Point(1) = {0, 0, 0, 1.0};
Point(2) = {-0.5, -0, 0, 1.0};
Point(3) = {0, -0.5, 0, 1.0};
Point(4) = {0, 0.5, 0, 1.0};
Point(5) = {0.5, -0, 0, 1.0};
Characteristic Length { 1,2,3,4,5 } = 0.25;
@danshapero
danshapero / firedrake-movie-test.py
Last active February 17, 2021 17:12
test for making movies with Firedrake
import argparse
import firedrake
from firedrake import (
max_value, sqrt, Constant, inner, as_vector, grad, dx, ds, dS
)
import numpy as np
from numpy import pi as π
parser = argparse.ArgumentParser()
parser.add_argument('--method', choices=['good', 'bad'])
@danshapero
danshapero / multi_iterator.cpp
Last active June 19, 2016 02:12
C++11 multi-iterator
#include <tuple>
/* ==============
* Multi-iterator
* ============== */
template <size_t> struct int_{};
template <class... Args>
@danshapero
danshapero / read_deal_matrix.py
Created August 26, 2015 17:15
reading deal.II matrices into Python
# -*- coding: utf-8 -*-
"""
Created on Tue Oct 28 16:28:02 2014
@author: nicola
"""
import numpy as np
from scipy.sparse import *
def read_sparsity_pattern(filename):
@danshapero
danshapero / mesh_to_poly.py
Last active August 29, 2015 14:23
mesh_to_poly
import subprocess
import numpy as np
from scipy.sparse import lil_matrix
from matplotlib.path import Path
# ------------------------------
def read_triangle_mesh(filename):
"""
Read in a mesh in Triangle's format.
@danshapero
danshapero / process_image.py
Created June 3, 2015 06:07
python script to process Landsat imagery
import os
import glob
import sys
"""
This script processes the raw Landsat imagery into an RGB image.
The stem of the filename is passed as the only command-line argument.
"""
if __name__ == "__main__":
@danshapero
danshapero / iterators.clj
Last active August 29, 2015 14:17
Some clojure macros for functional iteration
(defmacro iloop [[i istart iend] [sym init] expr]
`(loop [~i ~istart
~sym ~init]
(if (= ~i ~iend)
~sym
(recur (inc ~i)
~expr))))
(defmacro iter [forms pred ret]
(let [syms (take-nth 3 forms)
@danshapero
danshapero / geodat.py
Created February 11, 2015 00:56
Python scripts I use for drawing meshes from QGIS shapefiles
import numpy as np
from scipy.io import *
import struct
import sys
import math
def read_geodat(filename):
"""
Read in one of Ian's geodat files.
@danshapero
danshapero / shp_to_geo.py
Last active August 29, 2015 14:15
Script to convert shapefile to .geo format for QGIS
import numpy as np
import shapefile
import itertools
from matplotlib.path import Path
import sys
class GeometryError(Exception):
pass