Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Iterative IC generation #66

Open
wants to merge 69 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 27 commits
Commits
Show all changes
69 commits
Select commit Hold shift + click to select a range
0403bdd
initial commit of a working version
mladenivkovic Jul 22, 2020
fbdfd6b
moved globals to a dict
mladenivkovic Jul 23, 2020
292b2fe
added 3D kernels
mladenivkovic Jul 23, 2020
769d14c
added quick kernel test
mladenivkovic Jul 23, 2020
37867ee
import errors
mladenivkovic Jul 23, 2020
a900d86
x is now 3D array always. Differentiating between icSimParams and icR…
mladenivkovic Jul 24, 2020
3f1c01b
using scipy cKDTree now for smoothing lengths and neighbour searches.
mladenivkovic Jul 24, 2020
4ce5c8b
boxsize is now unyt array
mladenivkovic Jul 25, 2020
2049912
naive coordinate generation now works with unyt
mladenivkovic Jul 25, 2020
1b572c1
bugfixes and cleanups
mladenivkovic Jul 26, 2020
c0fa221
updated docstrings
mladenivkovic Jul 26, 2020
3c664d7
added tmp dir for simplicity
mladenivkovic Jul 26, 2020
9b87ac1
formatting
mladenivkovic Jul 26, 2020
8692901
cleanup with pycharm
mladenivkovic Jul 26, 2020
3d9536f
added random seed parameter
mladenivkovic Jul 26, 2020
110f1d6
added reduction of redistribution fraction
mladenivkovic Jul 26, 2020
2390ae7
line breaks
mladenivkovic Jul 26, 2020
7dbd736
added niter to stats dict
mladenivkovic Jul 26, 2020
4627862
delta_init can be generated automatically now
mladenivkovic Jul 26, 2020
408353e
added 3D case plotting
mladenivkovic Jul 26, 2020
7795323
boxsizeToUse is now numpy array, not unyt
mladenivkovic Jul 26, 2020
9db5cf6
small fixes
mladenivkovic Jul 27, 2020
6f674fe
added intermediate file dump
mladenivkovic Jul 27, 2020
b7746df
renamed dict keywords
mladenivkovic Jul 27, 2020
1108895
added example script
mladenivkovic Jul 27, 2020
f7ba8ec
formatting
mladenivkovic Jul 27, 2020
b516efb
small fix
mladenivkovic Jul 27, 2020
fab1396
adapted kernels following Josh's suggestions
mladenivkovic Jul 27, 2020
3242b6b
variable name conventions
mladenivkovic Jul 27, 2020
63dce5a
various small fixes
mladenivkovic Jul 27, 2020
b6f5ad8
small fixes & parameters are objects now everywhere. Code works.
mladenivkovic Jul 27, 2020
7a25eb1
added filename option for intermediate dumps
mladenivkovic Jul 27, 2020
33688a2
moved plotting to its own file
mladenivkovic Jul 27, 2020
e4d3abe
added check for boxsize being unyt_array
mladenivkovic Jul 27, 2020
9531049
fix for files in temp
mladenivkovic Jul 27, 2020
767e37f
first commit for full class
mladenivkovic Jul 28, 2020
5e5b377
better version of OOP
mladenivkovic Jul 29, 2020
d79de0f
cleanup and improvements
mladenivkovic Jul 29, 2020
bc723f7
sped up plotting
mladenivkovic Jul 29, 2020
448dc80
added unit system instead of unit l
mladenivkovic Jul 29, 2020
c8e4fb0
small fixes, added warning if max displ > 5
mladenivkovic Jul 29, 2020
14651db
small fixes; added stats object
mladenivkovic Jul 29, 2020
16288e4
added iter_min parameter
mladenivkovic Jul 29, 2020
f575a9e
renamed functions
mladenivkovic Jul 29, 2020
4e16732
docstrings - read'em and weep!
mladenivkovic Jul 29, 2020
b9ca5ff
forgot kernel data in __init__
mladenivkovic Jul 29, 2020
f4fe3b2
added restarting feature
mladenivkovic Jul 31, 2020
f45568f
cleanups for Josh <3
mladenivkovic Jul 31, 2020
d89c74d
added documentation
mladenivkovic Aug 3, 2020
23f2a04
removing tmp folder
mladenivkovic Aug 3, 2020
994c961
documentation fixes, separated in multiple files to be included
mladenivkovic Aug 3, 2020
496092f
small fixes
mladenivkovic Aug 3, 2020
173904f
initial commit unit tests for IC
mladenivkovic Aug 3, 2020
599c7f2
minor fix
mladenivkovic Aug 3, 2020
cab44ff
travis is being a beautiful intelligent tremendously clever helper
mladenivkovic Aug 3, 2020
032571c
small fix
mladenivkovic Aug 4, 2020
c955912
added check for particle proximity. fixes travis issue.
mladenivkovic Aug 5, 2020
b78ac23
cleaned up printing, plotting, and documentation
mladenivkovic Aug 6, 2020
922b07d
adapted some variable names
mladenivkovic Aug 7, 2020
7c82509
Cleaned up ParticleGenerator
JBorrow Aug 7, 2020
057f70c
Cleaned up the rest
JBorrow Aug 7, 2020
04ed329
Removed cash back to unyt arrays - not necessary if we modify the und…
JBorrow Aug 7, 2020
6f20ecc
Merge pull request #70 from SWIFTSIM/ICJosh
mladenivkovic Aug 7, 2020
b1825a4
various fixes, but mainly random generator seed is local now
mladenivkovic Aug 7, 2020
8e70ed8
small fixes for consistency
mladenivkovic Aug 9, 2020
403d0ae
Merge pull request #71 from SWIFTSIM/ICmladen
mladenivkovic Aug 9, 2020
57d2abf
fix for boxsize for tree
mladenivkovic Aug 24, 2020
2a3e51e
removed initial random seed
mladenivkovic Aug 24, 2020
bebf529
small fix for consistency with documentation
mladenivkovic Jun 25, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
186 changes: 186 additions & 0 deletions swiftsimio/initial_conditions/IC_kernel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
#!/usr/bin/env python3

"""
Kernel functions and constants following the Dehnen & Aly 2012 conventions

"""

# ------------------------------------
# Kernel related stuffs
# ------------------------------------

import numpy as np
from typing import Union


def W_cubic_spline(r: np.ndarray, H: Union[float, np.ndarray]):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't feel comfortable having so many different types of kernels like this.

We should centralise them (the ones used for visualisation, and the ones used for this) somewhere.

The one problem with that is inlining them within the compiled (future) versions of these functions. We'll have to think about that later, but let's leave the kernels here for now - we will deal with that last.

"""
Cubic spline kernel.


Parameters
------------------

r: np.ndarray
array of distances between particles.

H: np.ndarray or float
compact support radius of kernel. Scalar or numpy array of same shape
as ``r``.


Returns
------------------
W: np.ndarray
evaluated kernel functions with same shape as ``r``.


Note
------------------
+ The return value is not normalized. It needs to be divided by ``H^ndim``
and multiplied by the kernel norm to get the proper values.
"""

q = r / H
W = np.zeros(q.shape)

# if q <= 1.:
mladenivkovic marked this conversation as resolved.
Show resolved Hide resolved
# W += (1. - q)**3
# if q <= 0.5:
# W -= 4*(0.5 - q)**3
W[q <= 1] += (1.0 - q[q <= 1]) ** 3
W[q <= 0.5] -= 4 * (0.5 - q[q < 0.5]) ** 3

return W


def dWdr_cubic_spline(r: np.ndarray, H: Union[float, np.ndarray]):
"""
Cubic spline kernel derivative.


Parameters
------------------

r: np.ndarray
array of distances between particles.

H: np.ndarray or float
compact support radius of kernel. Scalar or numpy array of same shape
as ``r``


Returns
------------------

dWdr: np.ndarray
evaluated kernel derivative functions with same shape as ``r``.


Note
------------------
+ The return value is not normalized. It needs to be divided by
``H^(ndim+1)`` and multiplied by the kernel norm to get the proper values
"""

q = r / H
dW = np.zeros(q.shape)

# if q <= 1.:
# W -= 3 * (1. - q)**2
# if q <= 0.5:
# W += 12*(0.5 - q)**2
dW[q <= 1] -= 3 * (1.0 - q[q <= 1]) ** 2
dW[q <= 0.5] += 12 * (0.5 - q[q <= 0.5]) ** 2

return dW


# dictionary "pointing" to the correct functions to call
kernel_funcs = {"cubic spline": W_cubic_spline}

kernel_derivatives = {"cubic spline": dWdr_cubic_spline}

# Constants are from Dehnen & Aly 2012

kernel_gamma_1D = {"cubic spline": 1.732051}

kernel_norm_1D = {"cubic spline": 2.666667}

kernel_gamma_2D = {"cubic spline": 1.778002}

kernel_norm_2D = {"cubic spline": 3.637827}

kernel_gamma_3D = {"cubic spline": 1.825742}

kernel_norm_3D = {"cubic spline": 5.092958}


def get_kernel_data(kernel: str, ndim: int):
"""
Picks the correct kernel functions and constants for you.


Parameters
------------------

kernel: string {'cubic spline'}
which kernel you want to use

ndim: int
dimensionality of the kernel that you want


Returns
--------------------

W(r, H): callable
normalized kernel function with two positional arguments:

- r: np.ndarray
array of distances between particles.

- H: np.ndarray or float
compact support radius of kernel. Scalar or numpy array of same shape
as ``r``.

dWdr(r, H): callable
normalized kernel derivative function with two positional arguments:

- r: np.ndarray
array of distances between particles.

- H: np.ndarray or float
compact support radius of kernel. Scalar or numpy array of same
shape as ``r``.

kernel_gamma: float
H/h (compact support radius / smoothing length) for given kernel and
dimension

"""
JBorrow marked this conversation as resolved.
Show resolved Hide resolved
if ndim == 1:
W = lambda r, H: kernel_norm_1D[kernel] / H * kernel_funcs[kernel](r, H)
dWdr = (
lambda r, H: kernel_norm_1D[kernel]
/ H ** 2
* kernel_derivatives[kernel](r, H)
)
kernel_gamma = kernel_gamma_1D[kernel]
elif ndim == 2:
W = lambda r, H: kernel_norm_2D[kernel] / H ** 2 * kernel_funcs[kernel](r, H)
dWdr = (
lambda r, H: kernel_norm_2D[kernel]
/ H ** 3
* kernel_derivatives[kernel](r, H)
)
kernel_gamma = kernel_gamma_2D[kernel]
elif ndim == 3:
W = lambda r, H: kernel_norm_3D[kernel] / H ** 3 * kernel_funcs[kernel](r, H)
dWdr = (
lambda r, H: kernel_norm_3D[kernel]
/ H ** 4
* kernel_derivatives[kernel](r, H)
)
kernel_gamma = kernel_gamma_3D[kernel]
return W, dWdr, kernel_gamma
2 changes: 2 additions & 0 deletions swiftsimio/initial_conditions/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"""
Initial conditions generation.
"""

from .generate_particles import *
Loading