Skip to content

Instantly share code, notes, and snippets.

@jzrake
jzrake / h52vtk.py
Created September 25, 2012 20:47
Demonstrates the use of the h5py, numpy, and VTK Python modules to write a VTK structured data file.
"""
Demonstrates the use of the h5py, numpy, and VTK Python modules to write a VTK
structured data file.
"""
import vtk
import numpy as np
import h5py
fname = "chkpt.0012.h5"
dset = "prim/rho"
@jzrake
jzrake / lane-emden-potential.py
Created September 8, 2012 23:22
Python script which solves for the gravitational potential of a spherically symmetric star with a Lane-Emden polytrope density profile
import numpy as np
import matplotlib.pyplot as plt
def density(r):
if density.outside:
return 0.0
else:
z = density.rho_c * np.sin(r/R) / (r/R) if r > 1e-8 else 0.0
if z < 0.0:
density.outside = True
@jzrake
jzrake / matplot1.py
Created July 3, 2012 16:47
matplotlib tutorial 1
#!/usr/bin/env python
if __name__ == "__main__":
import sys
import numpy as np
from matplotlib import pyplot as plt
infile = sys.argv[1]
data = np.loadtxt(infile)
@jzrake
jzrake / h5_safe_Lexists.c
Created June 30, 2012 21:44
HDF5 safe, recursive H5Lexists
int H5Lexists_safe(hid_t base, const char *path)
// -----------------------------------------------------------------------------
// The HDF5 specification only allows H5Lexists to be called on an immediate
// child of the current object. However, you may wish to see whether a whole
// relative path exists, returning false if any of the intermediate links are
// not present. This function does that.
// http://www.hdfgroup.org/HDF5/doc/RM/RM_H5L.html#Link-Exists
// -----------------------------------------------------------------------------
{
@jzrake
jzrake / euler1d.c
Created May 15, 2012 20:05
Solves the 1-dimensional Euler equations using the method of lines and HLL Riemann solver
/*------------------------------------------------------------------------------
* FILE: euler.c
*
* Copyright (C) 2011 Jonathan Zrake, NYU CCPP: zrake <at> nyu <dot> edu
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
@jzrake
jzrake / zexcept.cpp
Created May 8, 2012 17:01
Quick tutorial on runtime exceptions in C++
#include <iostream>
#include <stdexcept>
class ZrakeFailed : public std::runtime_error
{
public:
ZrakeFailed(const std::string &msg) : std::runtime_error("zraker failed. " + msg) { }
@jzrake
jzrake / srevec.py
Created May 7, 2012 21:43
SR hydrodynamics eigenvectors
#!/usr/bin/env python
# ------------------------------------------------------------------------------
#
# Authors: Jonathan Zrake, Bez Laderman: NYU CCPP
# Date: May 7th, 2012
#
# This piece of code implements the left and right eigenvectors of the ideal
# special relativistic hydrodynamics equations. The formulas are an exact
# translation of those given in the literature:
@jzrake
jzrake / glut_ppm.c
Created May 1, 2012 18:10
PPM screenshot with GLUT
void TakeScreenshot(const char *fname)
{
printf("writing a screenshot to %s\n", fname);
int dimx = glutGet(GLUT_WINDOW_WIDTH);
int dimy = glutGet(GLUT_WINDOW_HEIGHT);
size_t imsize = 3*dimx*dimy;
char *pixels = (char*) malloc(imsize*sizeof(char));
glReadPixels(0, 0, dimx, dimy, GL_RGB, GL_UNSIGNED_BYTE, pixels);
@jzrake
jzrake / tetrun.cpp
Created April 28, 2012 22:21
Example code demonstrating use of tetgen
/* -----------------------------------------------------------------------------
*
* AUTHOR: Jonathan Zrake, NYU CCPP: zrake@nyu.edu
*
*
* USAGE: $> ./tetrun [number of trials]
*
*
* DESCRIPTION:
@jzrake
jzrake / vbo.c
Created April 20, 2012 04:24
OpenGL VBO example
void draw_vbo()
{
GLuint vbo,ibo;
GLfloat verts[8][3] = {{0.0, 0.0, 0.0},
{0.0, 0.0, 0.1},
{0.0, 0.1, 0.0},
{0.0, 0.1, 0.1},
{0.1, 0.0, 0.0},
{0.1, 0.0, 0.1},
{0.1, 0.1, 0.0},