-
Notifications
You must be signed in to change notification settings - Fork 12
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
mladenivkovic
wants to merge
69
commits into
master
Choose a base branch
from
IC
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
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 fbdfd6b
moved globals to a dict
mladenivkovic 292b2fe
added 3D kernels
mladenivkovic 769d14c
added quick kernel test
mladenivkovic 37867ee
import errors
mladenivkovic a900d86
x is now 3D array always. Differentiating between icSimParams and icR…
mladenivkovic 3f1c01b
using scipy cKDTree now for smoothing lengths and neighbour searches.
mladenivkovic 4ce5c8b
boxsize is now unyt array
mladenivkovic 2049912
naive coordinate generation now works with unyt
mladenivkovic 1b572c1
bugfixes and cleanups
mladenivkovic c0fa221
updated docstrings
mladenivkovic 3c664d7
added tmp dir for simplicity
mladenivkovic 9b87ac1
formatting
mladenivkovic 8692901
cleanup with pycharm
mladenivkovic 3d9536f
added random seed parameter
mladenivkovic 110f1d6
added reduction of redistribution fraction
mladenivkovic 2390ae7
line breaks
mladenivkovic 7dbd736
added niter to stats dict
mladenivkovic 4627862
delta_init can be generated automatically now
mladenivkovic 408353e
added 3D case plotting
mladenivkovic 7795323
boxsizeToUse is now numpy array, not unyt
mladenivkovic 9db5cf6
small fixes
mladenivkovic 6f674fe
added intermediate file dump
mladenivkovic b7746df
renamed dict keywords
mladenivkovic 1108895
added example script
mladenivkovic f7ba8ec
formatting
mladenivkovic b516efb
small fix
mladenivkovic fab1396
adapted kernels following Josh's suggestions
mladenivkovic 3242b6b
variable name conventions
mladenivkovic 63dce5a
various small fixes
mladenivkovic b6f5ad8
small fixes & parameters are objects now everywhere. Code works.
mladenivkovic 7a25eb1
added filename option for intermediate dumps
mladenivkovic 33688a2
moved plotting to its own file
mladenivkovic e4d3abe
added check for boxsize being unyt_array
mladenivkovic 9531049
fix for files in temp
mladenivkovic 767e37f
first commit for full class
mladenivkovic 5e5b377
better version of OOP
mladenivkovic d79de0f
cleanup and improvements
mladenivkovic bc723f7
sped up plotting
mladenivkovic 448dc80
added unit system instead of unit l
mladenivkovic c8e4fb0
small fixes, added warning if max displ > 5
mladenivkovic 14651db
small fixes; added stats object
mladenivkovic 16288e4
added iter_min parameter
mladenivkovic f575a9e
renamed functions
mladenivkovic 4e16732
docstrings - read'em and weep!
mladenivkovic b9ca5ff
forgot kernel data in __init__
mladenivkovic f4fe3b2
added restarting feature
mladenivkovic f45568f
cleanups for Josh <3
mladenivkovic d89c74d
added documentation
mladenivkovic 23f2a04
removing tmp folder
mladenivkovic 994c961
documentation fixes, separated in multiple files to be included
mladenivkovic 496092f
small fixes
mladenivkovic 173904f
initial commit unit tests for IC
mladenivkovic 599c7f2
minor fix
mladenivkovic cab44ff
travis is being a beautiful intelligent tremendously clever helper
mladenivkovic 032571c
small fix
mladenivkovic c955912
added check for particle proximity. fixes travis issue.
mladenivkovic b78ac23
cleaned up printing, plotting, and documentation
mladenivkovic 922b07d
adapted some variable names
mladenivkovic 7c82509
Cleaned up ParticleGenerator
JBorrow 057f70c
Cleaned up the rest
JBorrow 04ed329
Removed cash back to unyt arrays - not necessary if we modify the und…
JBorrow 6f20ecc
Merge pull request #70 from SWIFTSIM/ICJosh
mladenivkovic b1825a4
various fixes, but mainly random generator seed is local now
mladenivkovic 8e70ed8
small fixes for consistency
mladenivkovic 403d0ae
Merge pull request #71 from SWIFTSIM/ICmladen
mladenivkovic 57d2abf
fix for boxsize for tree
mladenivkovic 2a3e51e
removed initial random seed
mladenivkovic bebf529
small fix for consistency with documentation
mladenivkovic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]): | ||
""" | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
""" | ||
Initial conditions generation. | ||
""" | ||
|
||
from .generate_particles import * |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.