1function [dt,typNumOfNeighbors] = fixDtBasedOnTypNeighborsNum(maxNumNeighbors,src_dmn,W,M,dt,keepdt)
2% FIXDTBASEDONTYPNEIGHBORSNUM adjusts dt to make sure num of local
3% neighbors is less than a maximum value
4%
5% INPUTS:
6% maxNumNeighbors: maximum number of neighbors
7% [as,bs]: domain in which the sources are located
8% W: W*dt is the compact support of the window function
9% M: number of spring scatterers
10% dt: time step (first estimate)
11% N: 2pi/dt: number of Fourier modes
12% tFinal: final time.
13%
14% OUTPUTS:
15% dt: adjusted timestep
16% N: adjusted number of Fourier modes
17% typNumOfNeighbors: typical number of neighbors expected on average for
18% each source.
20as = src_dmn(1); bs = src_dmn(2);
22if(isnan(maxNumNeighbors))
23 maxNumNeighbors = ceil(sqrt(M*log(2*W*M))); % choose to balance otherContrib and finufft
24end
26typNumOfNeighbors = ceil(2*W*dt*M/(bs - as));
28if(typNumOfNeighbors>maxNumNeighbors && keepdt == 0)
29 dt = (bs - as)*maxNumNeighbors/(2*W*M);
30 fprintf('time step changed to dt = %1.2e to ensure maximum number of neighbors %d\n',dt,maxNumNeighbors);
31 typNumOfNeighbors = maxNumNeighbors;
32end
35end