import numpy as np
import exafmm.helmholtz as helmholtz
helmholtz.__doc__
"exafmm's submodule for Helmholtz kernel"
nsrcs = 100000
ntrgs = 200000
# generate random positions for particles
src_coords = np.random.random((nsrcs, 3))
trg_coords = np.random.random((nsrcs, 3))
# generate random charges for sources
src_charges = np.zeros(nsrcs, dtype=np.complex_)
src_charges.real = np.random.random(nsrcs)
src_charges.imag = np.random.random(nsrcs)
# create a list of source instances
sources = helmholtz.init_sources(src_coords, src_charges)
# create a list of target instances
targets = helmholtz.init_targets(trg_coords)
fmm = helmholtz.HelmholtzFmm(p=10, ncrit=300, wavek=10, filename='helmholtz_example_k10.dat')
Given sources, targets and FMM configurations, setup()
builds the tree and interaction list and pre-compute invariant matrices.
tree = helmholtz.setup(sources, targets, fmm)
ls *.dat
helmholtz_example_k10.dat test_file.dat
evaluate()
triggers the evaluation and returns a $n_{trg} \times 4$ numpy.ndarray
.
The $i$-th row of trg_values
starts with the potential value of the $i$-th target, followed by its three gradient values.
trg_values = helmholtz.evaluate(tree, fmm)
trg_values[6]
array([ -221.98960533 -761.47936505j, -865.25180217+2481.40881884j, 1290.96521265 -389.92609902j, -6337.67344409+2655.85947907j])
fmm.verify(tree.leafs)
returns L2-norm the relative error of potential and gradient in a list, compared with the values calculated from direct method.
fmm.verify(tree.leafs)
[3.177234172973424e-08, 5.900307387772e-07]
niters = 4
for i in range(niters):
print('-'*10 + ' iteration {} '.format(i) + '-'*10) # print divider between iterations
src_charges.real = np.random.random(nsrcs) # generate new random charges
src_charges.imag = np.random.random(nsrcs)
helmholtz.update_charges(tree, src_charges) # update charges
helmholtz.clear_values(tree) # clear values
trg_values = helmholtz.evaluate(tree, fmm) # evaluate potentials
print("Error: ", fmm.verify(tree.leafs)) # check accuracy
---------- iteration 0 ---------- Error: [3.1462242799274535e-08, 5.831925128982927e-07] ---------- iteration 1 ---------- Error: [3.1732536842029743e-08, 5.208091857409024e-07] ---------- iteration 2 ---------- Error: [3.180717719777336e-08, 5.515657504897553e-07] ---------- iteration 3 ---------- Error: [3.1376568787696064e-08, 5.542072823427909e-07]