Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Stephan Seitz
pystencils
Commits
607aafa8
Commit
607aafa8
authored
Jan 22, 2020
by
Michael Kuron
Browse files
fix more tests on Ubuntu
parent
01a56d31
Changes
11
Hide whitespace changes
Inline
Side-by-side
pystencils/astnodes.py
View file @
607aafa8
...
...
@@ -291,7 +291,10 @@ class Block(Node):
self
.
_nodes
=
nodes
self
.
parent
=
None
for
n
in
self
.
_nodes
:
n
.
parent
=
self
try
:
n
.
parent
=
self
except
AttributeError
:
pass
@
property
def
args
(
self
):
...
...
pystencils/backends/cbackend.py
View file @
607aafa8
...
...
@@ -386,6 +386,13 @@ class CustomSympyPrinter(CCodePrinter):
return
self
.
_print
(
expr
.
args
[
0
])
elif
isinstance
(
expr
,
fast_inv_sqrt
):
return
"({})"
.
format
(
self
.
_print
(
1
/
sp
.
sqrt
(
expr
.
args
[
0
])))
elif
isinstance
(
expr
,
sp
.
Abs
):
return
"abs({})"
.
format
(
self
.
_print
(
expr
.
args
[
0
]))
elif
isinstance
(
expr
,
sp
.
Mod
):
if
expr
.
is_integer
:
return
"({} % {})"
.
format
(
self
.
_print
(
expr
.
args
[
0
]),
self
.
_print
(
expr
.
args
[
1
]))
else
:
return
"fmod({}, {})"
.
format
(
self
.
_print
(
expr
.
args
[
0
]),
self
.
_print
(
expr
.
args
[
1
]))
elif
expr
.
func
in
infix_functions
:
return
"(%s %s %s)"
%
(
self
.
_print
(
expr
.
args
[
0
]),
infix_functions
[
expr
.
func
],
self
.
_print
(
expr
.
args
[
1
]))
elif
expr
.
func
==
int_power_of_2
:
...
...
pystencils_tests/test_boundary.py
View file @
607aafa8
...
...
@@ -3,6 +3,8 @@ from tempfile import TemporaryDirectory
import
numpy
as
np
import
pytest
from
pystencils
import
Assignment
,
create_kernel
from
pystencils.boundaries
import
BoundaryHandling
,
Neumann
,
add_neumann_boundary
from
pystencils.datahandling
import
SerialDataHandling
...
...
@@ -83,5 +85,6 @@ def test_kernel_vs_copy_boundary():
np
.
testing
.
assert_almost_equal
(
python_copy_result
,
handling_result
)
with
TemporaryDirectory
()
as
tmp_dir
:
pytest
.
importorskip
(
'pyevtk'
)
boundary_handling
.
geometry_to_vtk
(
file_name
=
os
.
path
.
join
(
tmp_dir
,
'test_output1'
),
ghost_layers
=
False
)
boundary_handling
.
geometry_to_vtk
(
file_name
=
os
.
path
.
join
(
tmp_dir
,
'test_output2'
),
ghost_layers
=
True
)
pystencils_tests/test_buffer_gpu.py
View file @
607aafa8
...
...
@@ -22,6 +22,7 @@ FIELD_SIZES = [(4, 3), (9, 3, 7)]
def
_generate_fields
(
dt
=
np
.
uint8
,
stencil_directions
=
1
,
layout
=
'numpy'
):
pytest
.
importorskip
(
'pycuda'
)
field_sizes
=
FIELD_SIZES
if
stencil_directions
>
1
:
field_sizes
=
[
s
+
(
stencil_directions
,)
for
s
in
field_sizes
]
...
...
@@ -44,7 +45,6 @@ def _generate_fields(dt=np.uint8, stencil_directions=1, layout='numpy'):
return
fields
@
pytest
.
mark
.
gpu
def
test_full_scalar_field
():
"""Tests fully (un)packing a scalar field (from)to a GPU buffer."""
fields
=
_generate_fields
()
...
...
@@ -73,7 +73,6 @@ def test_full_scalar_field():
np
.
testing
.
assert_equal
(
src_arr
,
dst_arr
)
@
pytest
.
mark
.
gpu
def
test_field_slice
():
"""Tests (un)packing slices of a scalar field (from)to a buffer."""
fields
=
_generate_fields
()
...
...
@@ -109,7 +108,6 @@ def test_field_slice():
np
.
testing
.
assert_equal
(
src_arr
[
pack_slice
],
dst_arr
[
unpack_slice
])
@
pytest
.
mark
.
gpu
def
test_all_cell_values
():
"""Tests (un)packing all cell values of the a field (from)to a buffer."""
num_cell_values
=
7
...
...
@@ -148,7 +146,6 @@ def test_all_cell_values():
np
.
testing
.
assert_equal
(
src_arr
,
dst_arr
)
@
pytest
.
mark
.
gpu
def
test_subset_cell_values
():
"""Tests (un)packing a subset of cell values of the a field (from)to a buffer."""
num_cell_values
=
7
...
...
@@ -190,7 +187,6 @@ def test_subset_cell_values():
np
.
testing
.
assert_equal
(
dst_arr
,
mask_arr
.
filled
(
int
(
0
)))
@
pytest
.
mark
.
gpu
def
test_field_layouts
():
num_cell_values
=
7
for
layout_str
in
[
'numpy'
,
'fzyx'
,
'zyxf'
,
'reverse_numpy'
]:
...
...
pystencils_tests/test_complex_numbers.py
View file @
607aafa8
...
...
@@ -57,6 +57,9 @@ def test_complex_numbers(assignment, scalar_dtypes, target):
print
(
code
)
assert
"Not supported"
not
in
code
if
target
==
'gpu'
:
pytest
.
importorskip
(
'pycuda'
)
kernel
=
ast
.
compile
()
assert
kernel
is
not
None
...
...
@@ -100,6 +103,9 @@ def test_complex_numbers_64(assignment, target):
print
(
code
)
assert
"Not supported"
not
in
code
if
target
==
'gpu'
:
pytest
.
importorskip
(
'pycuda'
)
kernel
=
ast
.
compile
()
assert
kernel
is
not
None
...
...
@@ -125,6 +131,7 @@ def test_complex_execution(dtype, target, with_complex_argument):
})
if
target
==
'gpu'
:
pytest
.
importorskip
(
'pycuda'
)
from
pycuda.gpuarray
import
zeros
x_arr
=
zeros
((
20
,
30
),
complex_dtype
)
y_arr
=
zeros
((
20
,
30
),
complex_dtype
)
...
...
pystencils_tests/test_cuda_known_functions.py
View file @
607aafa8
import
sympy
import
pytest
import
pystencils
from
pystencils.astnodes
import
get_dummy_symbol
from
pystencils.backends.cuda_backend
import
CudaSympyPrinter
...
...
@@ -19,6 +21,7 @@ def test_cuda_known_functions():
ast
=
pystencils
.
create_kernel
(
assignments
,
'gpu'
)
print
(
pystencils
.
show_code
(
ast
))
pytest
.
importorskip
(
'pycuda'
)
kernel
=
ast
.
compile
()
assert
(
kernel
is
not
None
)
...
...
pystencils_tests/test_datahandling.py
View file @
607aafa8
...
...
@@ -3,6 +3,8 @@ from tempfile import TemporaryDirectory
import
numpy
as
np
import
pytest
import
pystencils
as
ps
from
pystencils
import
create_data_handling
,
create_kernel
...
...
@@ -128,6 +130,7 @@ def kernel_execution_jacobi(dh, target):
def
vtk_output
(
dh
):
pytest
.
importorskip
(
'pyevtk'
)
dh
.
add_array
(
'scalar_field'
)
dh
.
add_array
(
'vector_field'
,
values_per_cell
=
dh
.
dim
)
dh
.
add_array
(
'multiple_scalar_field'
,
values_per_cell
=
9
)
...
...
@@ -223,6 +226,7 @@ def test_kernel_param(target):
def
test_vtk_output
():
pytest
.
importorskip
(
'pyevtk'
)
for
domain_shape
in
[(
4
,
5
),
(
3
,
4
,
5
)]:
dh
=
create_data_handling
(
domain_size
=
domain_shape
,
periodicity
=
True
)
vtk_output
(
dh
)
...
...
pystencils_tests/test_phasefield_dentritic_3D.ipynb
View file @
607aafa8
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import pytest\n",
"pytest.importorskip('pycuda')"
]
},
{
"cell_type": "code",
"execution_count": 1,
...
...
%% Cell type:code id: tags:
```
python
import
pytest
pytest
.
importorskip
(
'pycuda'
)
```
%% Cell type:code id: tags:
```
python
from
pystencils.session
import
*
sp
.
init_printing
()
frac
=
sp
.
Rational
```
%% Cell type:markdown id: tags:
# Phase-field simulation of dentritic solidification in 3D
This notebook tests the model presented in the dentritic growth tutorial in 3D.
%% Cell type:code id: tags:
```
python
target
=
'gpu'
gpu
=
target
==
'gpu'
domain_size
=
(
25
,
25
,
25
)
if
'is_test_run'
in
globals
()
else
(
300
,
300
,
300
)
dh
=
ps
.
create_data_handling
(
domain_size
=
domain_size
,
periodicity
=
True
,
default_target
=
target
)
φ_field
=
dh
.
add_array
(
'phi'
,
latex_name
=
'φ'
)
φ_delta_field
=
dh
.
add_array
(
'phidelta'
,
latex_name
=
'φ_D'
)
t_field
=
dh
.
add_array
(
'T'
)
```
%% Cell type:code id: tags:
```
python
ε
,
m
,
δ
,
j
,
θzero
,
α
,
γ
,
Teq
,
κ
,
τ
=
sp
.
symbols
(
"ε m δ j θ_0 α γ T_eq κ τ"
)
εb
=
sp
.
Symbol
(
"
\\
bar{
\\
epsilon}"
)
discretize
=
ps
.
fd
.
Discretization2ndOrder
(
dx
=
0.03
,
dt
=
1e-5
)
φ
=
φ_field
.
center
T
=
t_field
.
center
d
=
ps
.
fd
.
Diff
def
f
(
φ
,
m
):
return
φ
**
4
/
4
-
(
frac
(
1
,
2
)
-
m
/
3
)
*
φ
**
3
+
(
frac
(
1
,
4
)
-
m
/
2
)
*
φ
**
2
bulk_free_energy_density
=
f
(
φ
,
m
)
interface_free_energy_density
=
ε
**
2
/
2
*
(
d
(
φ
,
0
)
**
2
+
d
(
φ
,
1
)
**
2
+
d
(
φ
,
2
)
**
2
)
```
%% Cell type:markdown id: tags:
Here comes the major change, that has to be made for the 3D model: $
\e
psilon$ depends on the interface normal, which can not be computed simply as atan() as in the 2D case
%% Cell type:code id: tags:
```
python
n
=
sp
.
Matrix
([
d
(
φ
,
i
)
for
i
in
range
(
3
)])
nLen
=
sp
.
sqrt
(
sum
(
n_i
**
2
for
n_i
in
n
))
n
=
n
/
nLen
nVal
=
sum
(
n_i
**
4
for
n_i
in
n
)
σ
=
δ
*
nVal
εVal
=
εb
*
(
1
+
σ
)
εVal
```
%% Output
$\displaystyle \bar{\epsilon} \left(δ \left(\frac{{\partial_{0} {{φ}_{(0,0,0)}}}^{4}}{\left({\partial_{0} {{φ}_{(0,0,0)}}}^{2} + {\partial_{1} {{φ}_{(0,0,0)}}}^{2} + {\partial_{2} {{φ}_{(0,0,0)}}}^{2}\right)^{2}} + \frac{{\partial_{1} {{φ}_{(0,0,0)}}}^{4}}{\left({\partial_{0} {{φ}_{(0,0,0)}}}^{2} + {\partial_{1} {{φ}_{(0,0,0)}}}^{2} + {\partial_{2} {{φ}_{(0,0,0)}}}^{2}\right)^{2}} + \frac{{\partial_{2} {{φ}_{(0,0,0)}}}^{4}}{\left({\partial_{0} {{φ}_{(0,0,0)}}}^{2} + {\partial_{1} {{φ}_{(0,0,0)}}}^{2} + {\partial_{2} {{φ}_{(0,0,0)}}}^{2}\right)^{2}}\right) + 1\right)$
⎛ ⎛ 4
⎜ ⎜ D(φ[0,0,0])
\bar{\epsilon}⋅⎜δ⋅⎜───────────────────────────────────────────── + ───────────
⎜ ⎜ 2
⎜ ⎜⎛ 2 2 2⎞ ⎛
⎝ ⎝⎝D(φ[0,0,0]) + D(φ[0,0,0]) + D(φ[0,0,0]) ⎠ ⎝D(φ[0,0,0]
4 4
D(φ[0,0,0]) D(φ[0,0,0])
────────────────────────────────── + ─────────────────────────────────────────
2
2 2 2⎞ ⎛ 2 2
) + D(φ[0,0,0]) + D(φ[0,0,0]) ⎠ ⎝D(φ[0,0,0]) + D(φ[0,0,0]) + D(φ[0,0,0]
⎞ ⎞
⎟ ⎟
────⎟ + 1⎟
2⎟ ⎟
2⎞ ⎟ ⎟
) ⎠ ⎠ ⎠
%% Cell type:code id: tags:
```
python
def
m_func
(
temperature
):
return
(
α
/
sp
.
pi
)
*
sp
.
atan
(
γ
*
(
Teq
-
temperature
))
```
%% Cell type:code id: tags:
```
python
substitutions
=
{
m
:
m_func
(
T
),
ε
:
εVal
}
fe_i
=
interface_free_energy_density
.
subs
(
substitutions
)
fe_b
=
bulk_free_energy_density
.
subs
(
substitutions
)
μ_if
=
ps
.
fd
.
expand_diff_full
(
ps
.
fd
.
functional_derivative
(
fe_i
,
φ
),
functions
=
[
φ
])
μ_b
=
ps
.
fd
.
expand_diff_full
(
ps
.
fd
.
functional_derivative
(
fe_b
,
φ
),
functions
=
[
φ
])
```
%% Cell type:code id: tags:
```
python
dF_dφ
=
μ_b
+
sp
.
Piecewise
((
μ_if
,
nLen
**
2
>
1e-10
),
(
0
,
True
))
```
%% Cell type:code id: tags:
```
python
parameters
=
{
τ
:
0.0003
,
κ
:
1.8
,
εb
:
0.01
,
δ
:
0.3
,
γ
:
10
,
j
:
6
,
α
:
0.9
,
Teq
:
1.0
,
θzero
:
0.2
,
sp
.
pi
:
sp
.
pi
.
evalf
()
}
parameters
```
%% Output
$\displaystyle \left\{ \pi : 3.14159265358979, \ T_{eq} : 1.0, \ \bar{\epsilon} : 0.01, \ j : 6, \ α : 0.9, \ γ : 10, \ δ : 0.3, \ θ_{0} : 0.2, \ κ : 1.8, \ τ : 0.0003\right\}$
{π: 3.14159265358979, T_eq: 1.0, \bar{\epsilon}: 0.01, j: 6, α: 0.9, γ: 10, δ:
0.3, θ₀: 0.2, κ: 1.8, τ: 0.0003}
%% Cell type:code id: tags:
```
python
dφ_dt
=
-
dF_dφ
/
τ
assignments
=
[
ps
.
Assignment
(
φ_delta_field
.
center
,
discretize
(
dφ_dt
.
subs
(
parameters
))),
]
φEqs
=
ps
.
simp
.
sympy_cse_on_assignment_list
(
assignments
)
φEqs
.
append
(
ps
.
Assignment
(
φ
,
discretize
(
ps
.
fd
.
transient
(
φ
)
-
φ_delta_field
.
center
)))
temperatureEvolution
=
-
ps
.
fd
.
transient
(
T
)
+
ps
.
fd
.
diffusion
(
T
,
1
)
+
κ
*
φ_delta_field
.
center
temperatureEqs
=
[
ps
.
Assignment
(
T
,
discretize
(
temperatureEvolution
.
subs
(
parameters
)))
]
```
%% Cell type:code id: tags:
```
python
temperatureEqs
```
%% Output
$\displaystyle \left[ {{T}_{(0,0,0)}} \leftarrow 0.0111111111111111 {{T}_{(-1,0,0)}} + 0.0111111111111111 {{T}_{(0,-1,0)}} + 0.0111111111111111 {{T}_{(0,0,-1)}} + 0.933333333333333 {{T}_{(0,0,0)}} + 0.0111111111111111 {{T}_{(0,0,1)}} + 0.0111111111111111 {{T}_{(0,1,0)}} + 0.0111111111111111 {{T}_{(1,0,0)}} + 1.8 \cdot 10^{-5} {{φ_D}_{(0,0,0)}}\right]$
[T_C := 0.0111111111111111⋅T_W + 0.0111111111111111⋅T_S + 0.0111111111111111⋅T
_B + 0.933333333333333⋅T_C + 0.0111111111111111⋅T_T + 0.0111111111111111⋅T_N +
0.0111111111111111⋅T_E + 1.8e-5⋅phidelta_C]
%% Cell type:code id: tags:
```
python
φ_kernel
=
ps
.
create_kernel
(
φEqs
,
cpu_openmp
=
4
,
target
=
target
).
compile
()
temperatureKernel
=
ps
.
create_kernel
(
temperatureEqs
,
cpu_openmp
=
4
,
target
=
target
).
compile
()
```
%% Cell type:code id: tags:
```
python
def
time_loop
(
steps
):
φ_sync
=
dh
.
synchronization_function
([
'phi'
],
target
=
target
)
temperature_sync
=
dh
.
synchronization_function
([
'T'
],
target
=
target
)
dh
.
all_to_gpu
()
for
t
in
range
(
steps
):
φ_sync
()
dh
.
run_kernel
(
φ_kernel
)
temperature_sync
()
dh
.
run_kernel
(
temperatureKernel
)
dh
.
all_to_cpu
()
def
init
(
nucleus_size
=
np
.
sqrt
(
5
)):
for
b
in
dh
.
iterate
():
x
,
y
,
z
=
b
.
cell_index_arrays
x
,
y
,
z
=
x
-
b
.
shape
[
0
]
//
2
,
y
-
b
.
shape
[
1
]
//
2
,
z
-
b
.
shape
[
2
]
//
2
b
[
'phi'
].
fill
(
0
)
b
[
'phi'
][(
x
**
2
+
y
**
2
+
z
**
2
)
<
nucleus_size
**
2
]
=
1.0
b
[
'T'
].
fill
(
0.0
)
def
plot
(
slice_obj
=
ps
.
make_slice
[:,
:,
0.5
]):
plt
.
subplot
(
1
,
3
,
1
)
plt
.
scalar_field
(
dh
.
gather_array
(
'phi'
,
slice_obj
).
squeeze
())
plt
.
title
(
"φ"
)
plt
.
colorbar
()
plt
.
subplot
(
1
,
3
,
2
)
plt
.
title
(
"T"
)
plt
.
scalar_field
(
dh
.
gather_array
(
'T'
,
slice_obj
).
squeeze
())
plt
.
colorbar
()
plt
.
subplot
(
1
,
3
,
3
)
plt
.
title
(
"∂φ"
)
plt
.
scalar_field
(
dh
.
gather_array
(
'phidelta'
,
slice_obj
).
squeeze
())
plt
.
colorbar
()
```
%% Cell type:code id: tags:
```
python
init
()
plot
()
print
(
dh
)
```
%% Output
Name| Inner (min/max)| WithGl (min/max)
----------------------------------------------------
T| ( 0, 0)| ( 0, 0)
phi| ( 0, 1)| ( 0, 1)
phidelta| ( 0, 0)| ( 0, 0)
%% Cell type:code id: tags:
```
python
if
'is_test_run'
in
globals
():
time_loop
(
2
)
assert
np
.
isfinite
(
dh
.
max
(
'phi'
))
assert
np
.
isfinite
(
dh
.
max
(
'T'
))
assert
np
.
isfinite
(
dh
.
max
(
'phidelta'
))
else
:
from
time
import
perf_counter
vtk_writer
=
dh
.
create_vtk_writer
(
'dentritic_growth_large'
,
[
'phi'
])
last
=
perf_counter
()
for
i
in
range
(
300
):
time_loop
(
100
)
vtk_writer
(
i
)
print
(
"Step "
,
i
,
perf_counter
()
-
last
,
dh
.
max
(
'phi'
))
last
=
perf_counter
()
```
...
...
pystencils_tests/test_print_infinity.py
View file @
607aafa8
...
...
@@ -17,6 +17,9 @@ def test_print_infinity(type, negative, target):
assignment
=
pystencils
.
Assignment
(
x
.
center
,
oo
)
ast
=
pystencils
.
create_kernel
(
assignment
,
data_type
=
type
,
target
=
target
)
if
target
==
'gpu'
:
pytest
.
importorskip
(
'pycuda'
)
ast
.
compile
()
print
(
ast
.
compile
().
code
)
pystencils_tests/test_random.py
View file @
607aafa8
import
numpy
as
np
import
pytest
import
pystencils
as
ps
from
pystencils.rng
import
PhiloxFourFloats
,
PhiloxTwoDoubles
,
AESNIFourFloats
,
AESNITwoDoubles
...
...
@@ -12,6 +14,9 @@ philox_reference = np.array([[[3576608082, 1252663339, 1987745383, 348040302],
def
test_philox_double
():
for
target
in
(
'cpu'
,
'gpu'
):
if
target
==
'gpu'
:
pytest
.
importorskip
(
'pycuda'
)
dh
=
ps
.
create_data_handling
((
2
,
2
),
default_ghost_layers
=
0
,
default_target
=
target
)
f
=
dh
.
add_array
(
"f"
,
values_per_cell
=
2
)
...
...
@@ -39,6 +44,9 @@ def test_philox_double():
def
test_philox_float
():
for
target
in
(
'cpu'
,
'gpu'
):
if
target
==
'gpu'
:
pytest
.
importorskip
(
'pycuda'
)
dh
=
ps
.
create_data_handling
((
2
,
2
),
default_ghost_layers
=
0
,
default_target
=
target
)
f
=
dh
.
add_array
(
"f"
,
values_per_cell
=
4
)
...
...
pytest.ini
View file @
607aafa8
...
...
@@ -3,7 +3,6 @@ python_files = test_*.py *_test.py scenario_*.py
norecursedirs
=
*.egg-info .git .cache .ipynb_checkpoints htmlcov
addopts
=
--doctest-modules --durations=20 --cov-config pytest.ini
markers
=
gpu:
test
that
require
a
gpu
kerncraft:
tests
depending
on
kerncraft
[run]
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment