Skip to content

Instantly share code, notes, and snippets.

@GCBallesteros
GCBallesteros / gaussian_beam.scm
Last active February 16, 2018 10:57
Gaussian Beam Function
(define (gaussian-beam zR k z) (lambda (x)
(let
((z-prime (/ z zR))
(k-norm (vector3-norm k))
(x2 (vector3-dot x x)))
(*
(/ 1 (sqrt (+ 1 (* z-prime z-prime))))
(exp (+
(/ (* -1 k-norm x2) (* 2 zR (+ 1 (* z-prime z-prime))))
(/ (* 0-1i z-prime k-norm x2) (* 2 zR (+ 1 (* z-prime z-prime))))
@GCBallesteros
GCBallesteros / frequency_field_monitors.scm
Last active February 16, 2018 11:59
Frequency Field Monitors
(define (make-volume x-min x-max y-min y-max)
(volume
(center (/ (+ x-max x-min) 2) (/ (+ y-max y-min) 2) 0)
(size (- x-max x-min) (- y-max y-min) 0)))
(define (freq-field-monitor monitor-name dt vol output-comp)
(at-every dt (to-appended monitor-name (in-volume vol output-comp))))
@GCBallesteros
GCBallesteros / add_attributes_to_h5.scm
Last active February 16, 2018 12:14
How to add attributes to an h5 file from within a Meep control file.
; HDF5 METADATA helpers
(define (attr->string attr-pair)
(string-append " -attr " (car attr-pair) " " (number->string (exact->inexact (second attr-pair)))))
(define (add-attributes fname attributes)
(let
((command (string-append "./add_attr.py -fname " (get-filename-prefix) "-" fname ".h5 "))
(attribute-string (string-join (map attr->string attributes))))
(string-append command attribute-string)))
@GCBallesteros
GCBallesteros / fft_fields.py
Last active February 16, 2018 22:29
Fourier Transform Of Fields
# May need to pad with zeros the input fields to FFT to get the
# desired amount of frequency points.
n_sample_points = 2 * int(n_freq_points/(max_freq - min_freq) * max_freq)
fft_field = abs(np.fft.fft(field, n=n_sample_points, axis=-1).real)
fft_field = fft_field[:, :, :fft_field.shape[-1]//2]
freqs = np.fft.fftfreq(n_sample_points, d=dt)[:(n_sample_points/2)]
# We are only interested on the frequencies inside the source bandwidth
mask = (freqs >= min_freq) * (freqs <= max_freq)
@GCBallesteros
GCBallesteros / gaussian_beam_source.scm
Last active March 2, 2018 08:38
Gaussian Source Definition
(set! sources
(list
(make source
(src (make continuous-src (frequency fcen) (fwidth df)))
(component Ez)
(center x-pos y-pos)
(size 0 sy-source)
(amp-func (gaussian-beam zR k z)))))
@GCBallesteros
GCBallesteros / build_dbr.scm
Last active March 5, 2018 21:33
Return a list of blocks that makes the DBR
(define* (add-DBR n1 n2 n-pairs wl y-pos #:optional (growth-dir +) (dbr-width infinity))
(let
((layer1-t (* 0.25 (/ wl n1)))
(layer2-t (* 0.25 (/ wl n2)))
(make-layer (lambda (layer-t index pos) (make block
(center 0 (growth-dir pos (/ layer-t 2)) 0)
(material (make dielectric (epsilon (* index index))))
(size dbr-width layer-t dbr-width)))))
(let
((make-pair (lambda (pos) (list
@GCBallesteros
GCBallesteros / git_autocommit.py
Last active September 27, 2018 21:21
MLFlow AutoCommits
import os
import sys
import git
def _get_main_file(repo):
path_to_dir = os.path.dirname(os.path.abspath(__file__))
abs_path = os.path.join(path_to_dir, os.path.split(sys.argv[0])[-1])
return os.path.relpath(abs_path, repo.working_tree_dir)
@GCBallesteros
GCBallesteros / vanilla_monte_carlo.py
Created May 31, 2019 09:22
Vanilla Monte Carlo Integration
def MonteCarloIntegral(f, N, dim, fractional_error_tolerance=0.05):
relative_error = 1.
function_evaluations = list()
while relative_error > fractional_error_tolerance:
for _ in range(int(N)):
F = f(np.random.rand(dim))
function_evaluations.append(F)
@GCBallesteros
GCBallesteros / miser_1.py
Created June 14, 2019 21:34
init and main methods of the MISER class
from numpy.random import uniform
class MISER:
def __init__(self, miser_params):
"""
Parameters
----------
MNBS : int
If less than MNBS evaluations are left we do
vanilla MC.
@GCBallesteros
GCBallesteros / latex_template.tex
Last active July 19, 2019 22:11
LaTex Template For cg.py
\documentclass{article}
\usepackage{amsmath}
\begin{document}
Transformation matrix from $|jm>$ to $|m_1 m_2>$ basis
This is not the actual matrix; elements are represented as in the
CG tables. Do element-wise sqrt but keep the sign for the matrix.