-
Notifications
You must be signed in to change notification settings - Fork 42
/
bases.py
2359 lines (2031 loc) · 93 KB
/
bases.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Module for defining bases in the Chebyshev family
"""
from __future__ import division
import functools
import numpy as np
import sympy as sp
from numpy.polynomial import chebyshev as n_cheb
from scipy.special import eval_chebyt
from mpi4py_fft import fftw
from shenfun.spectralbase import SpectralBase, work, Transform, FuncWrap, \
islicedict, slicedict
from shenfun.optimization.cython import Cheb
from shenfun.utilities import inheritdocstrings
from shenfun.forms.arguments import Function
__all__ = ['ChebyshevBase', 'Basis', 'ShenDirichletBasis',
'ShenNeumannBasis', 'ShenBiharmonicBasis',
'SecondNeumannBasis', 'UpperDirichletBasis',
'ShenBiPolarBasis', 'BCBasis', 'BCBiharmonicBasis',
'DirichletNeumannBasis']
#pylint: disable=abstract-method, not-callable, method-hidden, no-self-use, cyclic-import
class DCTWrap(FuncWrap):
"""DCT for complex input"""
@property
def dct(self):
return object.__getattribute__(self, '_func')
def __call__(self, input_array=None, output_array=None, **kw):
dct_obj = self.dct
if input_array is not None:
self.input_array[...] = input_array
dct_obj.input_array[...] = self.input_array.real
dct_obj(None, None, **kw)
self.output_array.real[...] = dct_obj.output_array
dct_obj.input_array[...] = self.input_array.imag
dct_obj(None, None, **kw)
self.output_array.imag[...] = dct_obj.output_array
if output_array is not None:
output_array[...] = self.output_array
return output_array
return self.output_array
@inheritdocstrings
class ChebyshevBase(SpectralBase):
"""Abstract base class for all Chebyshev bases
Parameters
----------
N : int, optional
Number of quadrature points
quad : str, optional
Type of quadrature
- GL - Chebyshev-Gauss-Lobatto
- GC - Chebyshev-Gauss
domain : 2-tuple of floats, optional
The computational domain
dtype : data-type, optional
Type of input data in real physical space. Will be overloaded when
basis is part of a :class:`.TensorProductSpace`.
padding_factor : float, optional
Factor for padding backward transforms.
dealias_direct : bool, optional
Set upper 1/3 of coefficients to zero before backward transform
coordinates: 2-tuple (coordinate, position vector), optional
Map for curvilinear coordinatesystem.
The new coordinate variable in the new coordinate system is the first item.
Second item is a tuple for the Cartesian position vector as function of the
new variable in the first tuple. Example::
theta = sp.Symbols('x', real=True, positive=True)
rv = (sp.cos(theta), sp.sin(theta))
"""
def __init__(self, N, quad="GC", domain=(-1., 1.), dtype=np.float,
padding_factor=1, dealias_direct=False, coordinates=None):
assert quad in ('GC', 'GL')
SpectralBase.__init__(self, N, quad=quad, dtype=dtype, padding_factor=padding_factor,
dealias_direct=dealias_direct, domain=domain, coordinates=coordinates)
@staticmethod
def family():
return 'chebyshev'
def points_and_weights(self, N=None, map_true_domain=False, weighted=True, **kw):
if N is None:
N = self.N
if weighted:
if self.quad == "GL":
points = -(n_cheb.chebpts2(N)).astype(float)
weights = np.full(N, np.pi/(N-1))
weights[0] /= 2
weights[-1] /= 2
elif self.quad == "GC":
points, weights = n_cheb.chebgauss(N)
points = points.astype(float)
weights = weights.astype(float)
else:
if self.quad == "GL":
import quadpy
p = quadpy.line_segment.clenshaw_curtis(N)
points = -p.points
weights = p.weights
elif self.quad == "GC":
points = n_cheb.chebgauss(N)[0]
d = fftw.aligned(N, fill=0)
k = 2*(1 + np.arange((N-1)//2))
d[::2] = (2./N)/np.hstack((1., 1.-k*k))
w = fftw.aligned_like(d)
dct = fftw.dctn(w, axes=(0,), type=3)
weights = dct(d, w)
if map_true_domain is True:
points = self.map_true_domain(points)
return points, weights
def vandermonde(self, x):
return n_cheb.chebvander(x, int(self.N*self.padding_factor)-1)
def sympy_basis(self, i=0, x=sp.symbols('x', real=True)):
return sp.chebyshevt(i, x)
def sympy_weight(self, x=sp.symbols('x', real=True)):
return 1/sp.sqrt(1-x**2)
def evaluate_basis(self, x, i=0, output_array=None):
x = np.atleast_1d(x)
if output_array is None:
output_array = np.zeros(x.shape)
#output_array[:] = np.cos(i*np.arccos(x))
output_array[:] = eval_chebyt(i, x)
return output_array
def evaluate_basis_derivative_all(self, x=None, k=0, argument=0):
if x is None:
x = self.mesh(False, False)
V = self.vandermonde(x)
N, M = self.shape(False), self.shape(True)
if k > 0:
D = np.zeros((M, N))
D[:-k] = n_cheb.chebder(np.eye(M, N), k)
V = np.dot(V, D)
return self._composite_basis(V, argument=argument)
def evaluate_basis_all(self, x=None, argument=0):
if x is None:
x = self.mesh(False, False)
V = self.vandermonde(x)
return self._composite_basis(V, argument=argument)
def evaluate_basis_derivative(self, x=None, i=0, k=0, output_array=None):
if x is None:
x = self.mesh(False, False)
if output_array is None:
output_array = np.zeros(x.shape)
x = np.atleast_1d(x)
basis = np.zeros(self.shape(True))
basis[i] = 1
basis = n_cheb.Chebyshev(basis)
if k > 0:
basis = basis.deriv(k)
output_array[:] = basis(x)
return output_array
def _composite_basis(self, V, argument=0):
"""Return composite basis, where ``V`` is primary Vandermonde matrix."""
return V
def reference_domain(self):
return (-1., 1.)
def plan(self, shape, axis, dtype, options):
if isinstance(axis, tuple):
assert len(axis) == 1
axis = axis[-1]
if isinstance(self.forward, Transform):
if self.forward.input_array.shape == shape and self.axis == axis:
# Already planned
return
plan_fwd = self._xfftn_fwd
plan_bck = self._xfftn_bck
if 'builders' in self._xfftn_fwd.func.__module__: #pragma: no cover
opts = dict(
avoid_copy=True,
overwrite_input=True,
auto_align_input=True,
auto_contiguous=True,
planner_effort='FFTW_MEASURE',
threads=1,
)
opts.update(options)
U = fftw.aligned(shape, dtype=np.float)
xfftn_fwd = plan_fwd(U, axis=axis, **opts)
V = xfftn_fwd.output_array
xfftn_bck = plan_bck(V, axis=axis, **opts)
V.fill(0)
U.fill(0)
xfftn_fwd.update_arrays(U, V)
xfftn_bck.update_arrays(V, U)
else: # fftw wrapped with mpi4py-fft
opts = dict(
overwrite_input='FFTW_DESTROY_INPUT',
planner_effort='FFTW_MEASURE',
threads=1,
)
opts.update(options)
flags = (fftw.flag_dict[opts['planner_effort']],
fftw.flag_dict[opts['overwrite_input']])
threads = opts['threads']
U = fftw.aligned(shape, dtype=np.float)
xfftn_fwd = plan_fwd(U, axes=(axis,), threads=threads, flags=flags)
V = xfftn_fwd.output_array
xfftn_bck = plan_bck(V, axes=(axis,), threads=threads, flags=flags, output_array=U)
V.fill(0)
U.fill(0)
if np.dtype(dtype) is np.dtype('complex'):
# dct only works on real data, so need to wrap it
U = fftw.aligned(shape, dtype=np.complex)
V = fftw.aligned(shape, dtype=np.complex)
U.fill(0)
V.fill(0)
xfftn_fwd = DCTWrap(xfftn_fwd, U, V)
xfftn_bck = DCTWrap(xfftn_bck, V, U)
self.axis = axis
if self.padding_factor > 1.+1e-8:
trunc_array = self._get_truncarray(shape, V.dtype)
self.forward = Transform(self.forward, xfftn_fwd, U, V, trunc_array)
self.backward = Transform(self.backward, xfftn_bck, trunc_array, V, U)
self.backward_uniform = Transform(self.backward_uniform, xfftn_bck, trunc_array, V, U)
else:
self.forward = Transform(self.forward, xfftn_fwd, U, V, V)
self.backward = Transform(self.backward, xfftn_bck, V, V, U)
self.backward_uniform = Transform(self.backward_uniform, xfftn_bck, V, V, U)
self.scalar_product = Transform(self.scalar_product, xfftn_fwd, U, V, V)
self.si = islicedict(axis=self.axis, dimensions=self.dimensions)
self.sl = slicedict(axis=self.axis, dimensions=self.dimensions)
def get_orthogonal(self):
return Basis(self.N, quad=self.quad, domain=self.domain, coordinates=self.coors.coordinates)
@inheritdocstrings
class Basis(ChebyshevBase):
"""Basis for regular Chebyshev series
Parameters
----------
N : int, optional
Number of quadrature points
quad : str, optional
Type of quadrature
- GL - Chebyshev-Gauss-Lobatto
- GC - Chebyshev-Gauss
domain : 2-tuple of floats, optional
The computational domain
dtype : data-type, optional
Type of input data in real physical space. Will be overloaded when
basis is part of a :class:`.TensorProductSpace`.
padding_factor : float, optional
Factor for padding backward transforms.
dealias_direct : bool, optional
Set upper 1/3 of coefficients to zero before backward transform
coordinates: 2-tuple (coordinate, position vector), optional
Map for curvilinear coordinatesystem.
The new coordinate variable in the new coordinate system is the first item.
Second item is a tuple for the Cartesian position vector as function of the
new variable in the first tuple. Example::
theta = sp.Symbols('x', real=True, positive=True)
rv = (sp.cos(theta), sp.sin(theta))
"""
def __init__(self, N, quad='GC', domain=(-1., 1.), dtype=np.float, padding_factor=1,
dealias_direct=False, coordinates=None):
ChebyshevBase.__init__(self, N, quad=quad, domain=domain, dtype=dtype,
padding_factor=padding_factor, dealias_direct=dealias_direct,
coordinates=coordinates)
if quad == 'GC':
self._xfftn_fwd = functools.partial(fftw.dctn, type=2)
self._xfftn_bck = functools.partial(fftw.dctn, type=3)
else:
self._xfftn_fwd = functools.partial(fftw.dctn, type=1)
self._xfftn_bck = functools.partial(fftw.dctn, type=1)
self.plan((int(padding_factor*N),), 0, dtype, {})
@staticmethod
def derivative_coefficients(fk):
"""Return coefficients of Chebyshev series for c = f'(x)
Parameters
----------
fk : input array
Coefficients of regular Chebyshev series
Returns
-------
array
Coefficients of derivative of fk-series
"""
ck = np.zeros_like(fk)
if len(fk.shape) == 1:
ck = Cheb.derivative_coefficients(fk, ck)
elif len(fk.shape) == 3:
ck = Cheb.derivative_coefficients_3D(fk, ck)
return ck
def fast_derivative(self, fj):
"""Return derivative of :math:`f_j = f(x_j)` at quadrature points
:math:`x_j`
Parameters
----------
fj : input array
Function values on quadrature mesh
Returns
-------
array
Array with derivatives on quadrature mesh
"""
fk = self.forward(fj)
ck = self.derivative_coefficients(fk)
fd = self.backward(ck)
return fd.copy()
def apply_inverse_mass(self, array):
array *= (2/np.pi)
array[self.si[0]] /= 2
if self.quad == 'GL':
array[self.si[-1]] /= 2
return array
def evaluate_expansion_all(self, input_array, output_array, fast_transform=True):
if fast_transform is False:
SpectralBase.evaluate_expansion_all(self, input_array, output_array, False)
return
output_array = self.backward.xfftn()
if self.quad == "GC":
s0 = self.sl[slice(0, 1)]
output_array *= 0.5
output_array += input_array[s0]/2
elif self.quad == "GL":
output_array *= 0.5
output_array += input_array[self.sl[slice(0, 1)]]/2
s0 = self.sl[slice(-1, None)]
s2 = self.sl[slice(0, None, 2)]
output_array[s2] += input_array[s0]/2
s2 = self.sl[slice(1, None, 2)]
output_array[s2] -= input_array[s0]/2
def evaluate_scalar_product(self, input_array, output_array, fast_transform=True):
if fast_transform is False:
self.vandermonde_scalar_product(input_array, output_array)
return
#assert self.N == self.scalar_product.input_array.shape[self.axis]
out = self.scalar_product.xfftn()
if self.quad == "GC":
out *= (np.pi/(2*self.N*self.padding_factor))
elif self.quad == "GL":
out *= (np.pi/(2*(self.N*self.padding_factor-1)))
def eval(self, x, u, output_array=None):
x = np.atleast_1d(x)
if output_array is None:
output_array = np.zeros(x.shape, dtype=self.forward.output_array.dtype)
x = self.map_reference_domain(x)
output_array[:] = n_cheb.chebval(x, u)
return output_array
@property
def is_orthogonal(self):
return True
def get_orthogonal(self):
return self
@inheritdocstrings
class ShenDirichletBasis(ChebyshevBase):
"""Shen basis for Dirichlet boundary conditions
Parameters
----------
N : int, optional
Number of quadrature points
quad : str, optional
Type of quadrature
- GL - Chebyshev-Gauss-Lobatto
- GC - Chebyshev-Gauss
bc : 2-tuple of floats, optional
Boundary conditions at, respectively, x=(-1, 1).
domain : 2-tuple of floats, optional
The computational domain
dtype : data-type, optional
Type of input data in real physical space. Will be overloaded when
basis is part of a :class:`.TensorProductSpace`.
scaled : bool, optional
Whether or not to use scaled basis
padding_factor : float, optional
Factor for padding backward transforms.
dealias_direct : bool, optional
Set upper 1/3 of coefficients to zero before backward transform
coordinates: 2-tuple (coordinate, position vector), optional
Map for curvilinear coordinatesystem.
The new coordinate variable in the new coordinate system is the first item.
Second item is a tuple for the Cartesian position vector as function of the
new variable in the first tuple. Example::
theta = sp.Symbols('x', real=True, positive=True)
rv = (sp.cos(theta), sp.sin(theta))
Note
----
A test function is always using homogeneous boundary conditions.
"""
def __init__(self, N, quad="GC", bc=(0, 0), domain=(-1., 1.), dtype=np.float, scaled=False,
padding_factor=1, dealias_direct=False, coordinates=None):
ChebyshevBase.__init__(self, N, quad=quad, domain=domain, dtype=dtype,
padding_factor=padding_factor, dealias_direct=dealias_direct,
coordinates=coordinates)
from shenfun.tensorproductspace import BoundaryValues
self.CT = Basis(N, quad=quad, dtype=dtype, padding_factor=padding_factor, dealias_direct=dealias_direct)
self._scaled = scaled
self._factor = np.ones(1)
self.bc = BoundaryValues(self, bc=bc)
self.plan((int(padding_factor*N),), 0, dtype, {})
@staticmethod
def boundary_condition():
return 'Dirichlet'
@property
def has_nonhomogeneous_bcs(self):
return self.bc.has_nonhomogeneous_bcs()
def _composite_basis(self, V, argument=0):
P = np.zeros_like(V)
P[:, :-2] = V[:, :-2] - V[:, 2:]
if argument == 1: # if trial function
P[:, -1] = (V[:, 0] + V[:, 1])/2 # x = +1
P[:, -2] = (V[:, 0] - V[:, 1])/2 # x = -1
return P
def sympy_basis(self, i=0, x=sp.symbols('x', real=True)):
assert i < self.N
if i < self.N-2:
return sp.chebyshevt(i, x) - sp.chebyshevt(i+2, x)
if i == self.N-2:
return 0.5*(1-x)
return 0.5*(1+x)
def evaluate_basis(self, x, i=0, output_array=None):
x = np.atleast_1d(x)
if output_array is None:
output_array = np.zeros(x.shape)
if i < self.N-2:
w = np.arccos(x)
output_array[:] = np.cos(i*w) - np.cos((i+2)*w)
elif i == self.N-2:
output_array[:] = 0.5*(1-x)
elif i == self.N-1:
output_array[:] = 0.5*(1+x)
return output_array
def evaluate_basis_derivative(self, x=None, i=0, k=0, output_array=None):
if x is None:
x = self.mesh(False, False)
if output_array is None:
output_array = np.zeros(x.shape)
x = np.atleast_1d(x)
basis = np.zeros(self.shape(True))
basis[np.array([i, i+2])] = (1, -1)
basis = n_cheb.Chebyshev(basis)
if k > 0:
basis = basis.deriv(k)
output_array[:] = basis(x)
return output_array
def is_scaled(self):
"""Return True if scaled basis is used, otherwise False"""
return False
def vandermonde_scalar_product(self, input_array, output_array):
SpectralBase.vandermonde_scalar_product(self, input_array, output_array)
output_array[self.si[-2]] = 0
output_array[self.si[-1]] = 0
def evaluate_scalar_product(self, input_array, output_array, fast_transform=True):
if fast_transform is False:
self.vandermonde_scalar_product(input_array, output_array)
return
output = self.CT.scalar_product(fast_transform=fast_transform)
s0 = self.sl[slice(0, -2)]
s1 = self.sl[slice(2, None)]
output[s0] -= output[s1]
output[self.si[-2]] = 0
output[self.si[-1]] = 0
def evaluate_expansion_all(self, input_array, output_array, fast_transform=True):
if fast_transform is False:
self._padding_backward(input_array, self.backward.tmp_array)
SpectralBase.evaluate_expansion_all(self, self.backward.tmp_array, output_array, False)
return
w_hat = work[(input_array, 0, True)]
s0 = self.sl[slice(0, -2)]
s1 = self.sl[slice(2, None)]
w_hat[s0] = input_array[s0]
w_hat[s1] -= input_array[s0]
self.bc.add_to_orthogonal(w_hat, input_array) # Correct bc-values must be in input_array (as they should be before a backward transform)
self.CT.backward(w_hat)
assert output_array is self.CT.backward.output_array
def to_ortho(self, input_array, output_array=None):
if output_array is None:
output_array = Function(self.get_orthogonal())
else:
output_array.fill(0)
s0 = self.sl[slice(0, -2)]
s1 = self.sl[slice(2, None)]
output_array[s0] = input_array[s0]
output_array[s1] -= input_array[s0]
self.bc.add_to_orthogonal(output_array, input_array)
return output_array
def slice(self):
return slice(0, self.N-2)
def eval(self, x, u, output_array=None):
x = np.atleast_1d(x)
if output_array is None:
output_array = np.zeros(x.shape, dtype=self.dtype)
x = self.map_reference_domain(x)
w_hat = work[(u, 0, True)]
output_array[:] = n_cheb.chebval(x, u[:-2])
w_hat[2:] = u[:-2]
output_array -= n_cheb.chebval(x, w_hat)
output_array += 0.5*(u[-1]*(1+x)+u[-2]*(1-x))
return output_array
def forward(self, input_array=None, output_array=None, fast_transform=True):
self.scalar_product(input_array, fast_transform=fast_transform)
u = self.scalar_product.tmp_array
self.bc.add_mass_rhs(u)
self.apply_inverse_mass(u)
self._truncation_forward(u, self.forward.output_array)
self.bc.set_boundary_dofs(self.forward.output_array, False)
if output_array is not None:
output_array[...] = self.forward.output_array
return output_array
return self.forward.output_array
def backward(self, input_array=None, output_array=None, fast_transform=True):
if input_array is not None:
self.backward.input_array[...] = input_array
self.evaluate_expansion_all(self.backward.input_array,
self.backward.output_array,
fast_transform=fast_transform)
if output_array is not None:
output_array[...] = self.backward.output_array
return output_array
return self.backward.output_array
def plan(self, shape, axis, dtype, options):
if isinstance(axis, tuple):
assert len(axis) == 1
axis = axis[-1]
if isinstance(self.forward, Transform):
if self.forward.input_array.shape == shape and self.axis == axis:
# Already planned
return
self.CT.plan(shape, axis, dtype, options)
self.CT.tensorproductspace = self.tensorproductspace
xfftn_fwd = self.CT.forward.xfftn
xfftn_bck = self.CT.backward.xfftn
U = xfftn_fwd.input_array
V = xfftn_fwd.output_array
self.axis = axis
if self.padding_factor > 1.+1e-8:
trunc_array = self._get_truncarray(shape, V.dtype)
self.forward = Transform(self.forward, xfftn_fwd, U, V, trunc_array)
self.backward = Transform(self.backward, xfftn_bck, trunc_array, V, U)
self.backward_uniform = Transform(self.backward_uniform, xfftn_bck, trunc_array, V, U)
else:
self.forward = Transform(self.forward, xfftn_fwd, U, V, V)
self.backward = Transform(self.backward, xfftn_bck, V, V, U)
self.backward_uniform = Transform(self.backward_uniform, xfftn_bck, V, V, U)
self.scalar_product = Transform(self.scalar_product, xfftn_fwd, U, V, V)
self.si = islicedict(axis=self.axis, dimensions=self.dimensions)
self.sl = slicedict(axis=self.axis, dimensions=self.dimensions)
def get_bc_basis(self):
return BCBasis(self.N, quad=self.quad, domain=self.domain, coordinates=self.coors.coordinates)
def get_refined(self, N):
return ShenDirichletBasis(N,
quad=self.quad,
domain=self.domain,
dtype=self.dtype,
padding_factor=self.padding_factor,
dealias_direct=self.dealias_direct,
coordinates=self.coors.coordinates,
bc=self.bc.bc,
scaled=self._scaled)
def get_dealiased(self, padding_factor=1.5, dealias_direct=False):
return ShenDirichletBasis(self.N,
quad=self.quad,
domain=self.domain,
dtype=self.dtype,
padding_factor=padding_factor,
dealias_direct=dealias_direct,
coordinates=self.coors.coordinates,
bc=self.bc.bc,
scaled=self._scaled)
def _truncation_forward(self, padded_array, trunc_array):
if not id(trunc_array) == id(padded_array):
trunc_array.fill(0)
N = trunc_array.shape[self.axis]
s = self.sl[slice(0, N-2)]
trunc_array[s] = padded_array[s]
s = self.sl[slice(-2, None)]
trunc_array[s] = padded_array[s]
def _padding_backward(self, trunc_array, padded_array):
if not id(trunc_array) == id(padded_array):
padded_array.fill(0)
N = trunc_array.shape[self.axis]
_sn = self.sl[slice(0, N-2)]
padded_array[_sn] = trunc_array[_sn]
_sn = self.sl[slice(N-2, N)]
_sp = self.sl[slice(-2, None)]
padded_array[_sp] = trunc_array[_sn]
elif self.dealias_direct:
su = self.sl[slice(2*self.N//3, self.N-2)]
padded_array[su] = 0
@inheritdocstrings
class ShenNeumannBasis(ChebyshevBase):
"""Shen basis for homogeneous Neumann boundary conditions
Parameters
----------
N : int, optional
Number of quadrature points
quad : str, optional
Type of quadrature
- GL - Chebyshev-Gauss-Lobatto
- GC - Chebyshev-Gauss
mean : float, optional
Mean value
domain : 2-tuple of floats, optional
The computational domain
dtype : data-type, optional
Type of input data in real physical space. Will be overloaded when
basis is part of a :class:`.TensorProductSpace`.
padding_factor : float, optional
Factor for padding backward transforms.
dealias_direct : bool, optional
Set upper 1/3 of coefficients to zero before backward transform
coordinates: 2-tuple (coordinate, position vector), optional
Map for curvilinear coordinatesystem.
The new coordinate variable in the new coordinate system is the first item.
Second item is a tuple for the Cartesian position vector as function of the
new variable in the first tuple. Example::
theta = sp.Symbols('x', real=True, positive=True)
rv = (sp.cos(theta), sp.sin(theta))
"""
def __init__(self, N, quad="GC", mean=0, domain=(-1., 1.), dtype=np.float, padding_factor=1,
dealias_direct=False, coordinates=None):
ChebyshevBase.__init__(self, N, quad=quad, domain=domain, dtype=dtype,
padding_factor=padding_factor, dealias_direct=dealias_direct,
coordinates=coordinates)
self.mean = mean
self.CT = Basis(N, quad=quad, dtype=dtype, padding_factor=padding_factor, dealias_direct=dealias_direct)
self._factor = np.zeros(0)
self.plan(int(N*padding_factor), 0, dtype, {})
@staticmethod
def boundary_condition():
return 'Neumann'
def _composite_basis(self, V, argument=0):
assert self.N == V.shape[1]
P = np.zeros_like(V)
k = np.arange(self.N).astype(np.float)
P[:, :-2] = V[:, :-2] - (k[:-2]/(k[:-2]+2))**2*V[:, 2:]
return P
def sympy_basis(self, i=0, x=sp.symbols('x', real=True)):
if 0 < i < self.N-2:
return sp.chebyshevt(i, x) - (i/(i+2))**2*sp.chebyshevt(i+2, x)
return 0
def evaluate_basis(self, x, i=0, output_array=None):
x = np.atleast_1d(x)
if output_array is None:
output_array = np.zeros(x.shape)
w = np.arccos(x)
output_array[:] = np.cos(i*w) - (i*1./(i+2))**2*np.cos((i+2)*w)
return output_array
def evaluate_basis_derivative(self, x=None, i=0, k=0, output_array=None):
if x is None:
x = self.mesh(False, False)
if output_array is None:
output_array = np.zeros(x.shape)
x = np.atleast_1d(x)
basis = np.zeros(self.shape(True))
basis[np.array([i, i+2])] = (1, -(i*1./(i+2))**2)
basis = n_cheb.Chebyshev(basis)
if k > 0:
basis = basis.deriv(k)
output_array[:] = basis(x)
return output_array
def set_factor_array(self, v):
"""Set intermediate factor arrays"""
if not self._factor.shape == v.shape:
k = self.wavenumbers().astype(float)
self._factor = (k/(k+2))**2
def evaluate_scalar_product(self, input_array, output_array, fast_transform=True):
if fast_transform is False:
self.vandermonde_scalar_product(input_array, output_array)
return
output = self.CT.scalar_product(fast_transform=True)
self.set_factor_array(output)
sm2 = self.sl[slice(0, -2)]
s2p = self.sl[slice(2, None)]
output[sm2] -= self._factor * output[s2p]
def scalar_product(self, input_array=None, output_array=None, fast_transform=True):
if input_array is not None:
self.scalar_product.input_array[...] = input_array
self.evaluate_scalar_product(self.scalar_product.input_array,
self.scalar_product.output_array,
fast_transform=fast_transform)
output = self.scalar_product.output_array
output[self.si[0]] = self.mean*np.pi
output[self.sl[slice(-2, None)]] = 0
if output_array is not None:
output_array[...] = output
return output_array
return output
def evaluate_expansion_all(self, input_array, output_array, fast_transform=True):
if fast_transform is False:
self._padding_backward(input_array, self.backward.tmp_array)
SpectralBase.evaluate_expansion_all(self, self.backward.tmp_array, output_array, False)
return
w_hat = work[(input_array, 0, True)]
self.set_factor_array(input_array)
s0 = self.sl[slice(0, -2)]
s1 = self.sl[slice(2, None)]
w_hat[s0] = input_array[s0]
w_hat[s1] -= self._factor*input_array[s0]
self.CT.backward(w_hat)
assert output_array is self.CT.backward.output_array
def backward(self, input_array=None, output_array=None, fast_transform=True):
if input_array is not None:
self.backward.input_array[...] = input_array
self.evaluate_expansion_all(self.backward.input_array,
self.backward.output_array,
fast_transform=fast_transform)
if output_array is not None:
output_array[...] = self.backward.output_array
return output_array
return self.backward.output_array
def to_ortho(self, input_array, output_array=None):
if output_array is None:
output_array = Function(self.get_orthogonal())
else:
output_array.fill(0)
s0 = self.sl[slice(0, -2)]
s1 = self.sl[slice(2, None)]
self.set_factor_array(input_array)
output_array[s0] = input_array[s0]
output_array[s1] -= self._factor*input_array[s0]
return output_array
def slice(self):
return slice(0, self.N-2)
def eval(self, x, u, output_array=None):
x = np.atleast_1d(x)
if output_array is None:
output_array = np.zeros(x.shape, dtype=self.dtype)
x = self.map_reference_domain(x)
w_hat = work[(u, 0, True)]
self.set_factor_array(u)
output_array[:] = n_cheb.chebval(x, u[:-2])
w_hat[2:] = self._factor*u[:-2]
output_array -= n_cheb.chebval(x, w_hat)
return output_array
def plan(self, shape, axis, dtype, options):
if isinstance(axis, tuple):
assert len(axis) == 1
axis = axis[0]
if isinstance(self.forward, Transform):
if self.forward.input_array.shape == shape and self.axis == axis:
# Already planned
return
self.CT.plan(shape, axis, dtype, options)
self.CT.tensorproductspace = self.tensorproductspace
xfftn_fwd = self.CT.forward.xfftn
xfftn_bck = self.CT.backward.xfftn
U = xfftn_fwd.input_array
V = xfftn_fwd.output_array
self.axis = axis
if self.padding_factor > 1.+1e-8:
trunc_array = self._get_truncarray(shape, V.dtype)
self.forward = Transform(self.forward, xfftn_fwd, U, V, trunc_array)
self.backward = Transform(self.backward, xfftn_bck, trunc_array, V, U)
self.backward_uniform = Transform(self.backward_uniform, xfftn_bck, trunc_array, V, U)
else:
self.forward = Transform(self.forward, xfftn_fwd, U, V, V)
self.backward = Transform(self.backward, xfftn_bck, V, V, U)
self.backward_uniform = Transform(self.backward_uniform, xfftn_bck, V, V, U)
self.scalar_product = Transform(self.scalar_product, xfftn_fwd, U, V, V)
self.si = islicedict(axis=self.axis, dimensions=self.dimensions)
self.sl = slicedict(axis=self.axis, dimensions=self.dimensions)
def get_refined(self, N):
return ShenNeumannBasis(N,
quad=self.quad,
domain=self.domain,
dtype=self.dtype,
padding_factor=self.padding_factor,
dealias_direct=self.dealias_direct,
coordinates=self.coors.coordinates,
mean=self.mean)
def get_dealiased(self, padding_factor=1.5, dealias_direct=False):
return ShenNeumannBasis(self.N,
quad=self.quad,
domain=self.domain,
dtype=self.dtype,
padding_factor=padding_factor,
dealias_direct=dealias_direct,
coordinates=self.coors.coordinates,
mean=self.mean)
@inheritdocstrings
class ShenBiharmonicBasis(ChebyshevBase):
"""Shen biharmonic basis
Using 2 Dirichlet and 2 Neumann boundary conditions. All possibly
nonhomogeneous.
Parameters
----------
N : int, optional
Number of quadrature points
quad : str, optional
Type of quadrature
- GL - Chebyshev-Gauss-Lobatto
- GC - Chebyshev-Gauss
bc : 4-tuple of numbers
The values of the 4 boundary conditions at x=(-1, 1).
The two Dirichlet at (-1, 1) first and then the Neumann at (-1, 1).
domain : 2-tuple of floats, optional
The computational domain
dtype : data-type, optional
Type of input data in real physical space. Will be overloaded when
basis is part of a :class:`.TensorProductSpace`.
padding_factor : float, optional
Factor for padding backward transforms.
dealias_direct : bool, optional
Set upper 1/3 of coefficients to zero before backward transform
coordinates: 2-tuple (coordinate, position vector), optional
Map for curvilinear coordinatesystem.
The new coordinate variable in the new coordinate system is the first item.
Second item is a tuple for the Cartesian position vector as function of the
new variable in the first tuple. Example::
theta = sp.Symbols('x', real=True, positive=True)
rv = (sp.cos(theta), sp.sin(theta))
"""
def __init__(self, N, quad="GC", bc=(0, 0, 0, 0), domain=(-1., 1.), dtype=np.float,
padding_factor=1, dealias_direct=False, coordinates=None):
from shenfun.tensorproductspace import BoundaryValues
ChebyshevBase.__init__(self, N, quad=quad, domain=domain, dtype=dtype,
padding_factor=padding_factor, dealias_direct=dealias_direct,
coordinates=coordinates)
self.CT = Basis(N, quad=quad, dtype=dtype, padding_factor=padding_factor, dealias_direct=dealias_direct)
self._factor1 = np.zeros(0)
self._factor2 = np.zeros(0)
self.bc = BoundaryValues(self, bc=bc)
self.plan((int(padding_factor*N),), 0, dtype, {})
@staticmethod
def boundary_condition():
return 'Biharmonic'
@property
def has_nonhomogeneous_bcs(self):
return self.bc.has_nonhomogeneous_bcs()
def _composite_basis(self, V, argument=0):
P = np.zeros_like(V)
k = np.arange(V.shape[1]).astype(np.float)[:-4]
P[:, :-4] = V[:, :-4] - (2*(k+2)/(k+3))*V[:, 2:-2] + ((k+1)/(k+3))*V[:, 4:]
if argument == 1: # if trial function
P[:, -4:] = np.tensordot(V[:, :4], BCBiharmonicBasis.coefficient_matrix(), (1, 1))
return P
def sympy_basis(self, i=0, x=sp.symbols('x', real=True)):
if i < self.N-4:
f = sp.chebyshevt(i, x) - (2*(i+2)/(i+3))*sp.chebyshevt(i+2, x) + (i+1)/(i+3)*sp.chebyshevt(i+4, x)
else:
f = BCBiharmonicBasis.coefficient_matrix()[i]*np.array([sp.chebyshevt(j, x) for j in range(4)])
return f
def evaluate_basis(self, x, i=0, output_array=None):
x = np.atleast_1d(x)
if output_array is None:
output_array = np.zeros(x.shape)
w = np.arccos(x)
output_array[:] = np.cos(i*w) - (2*(i+2.)/(i+3.))*np.cos((i+2)*w) + ((i+1.)/(i+3.))*np.cos((i+4)*w)
return output_array
def evaluate_basis_derivative(self, x=None, i=0, k=0, output_array=None):
if x is None:
x = self.mesh(False, False)
if output_array is None:
output_array = np.zeros(x.shape)
x = np.atleast_1d(x)
basis = np.zeros(self.shape(True))
basis[np.array([i, i+2, i+4])] = (1, -(2*(i+2.)/(i+3.)), ((i+1.)/(i+3.)))
basis = n_cheb.Chebyshev(basis)
if k > 0:
basis = basis.deriv(k)
output_array[:] = basis(x)
return output_array
def set_factor_arrays(self, v):
"""Set intermediate factor arrays"""
s = self.sl[self.slice()]
if not self._factor1.shape == v[s].shape:
k = self.wavenumbers().astype(float)
self._factor1 = (-2*(k+2)/(k+3)).astype(float)
self._factor2 = ((k+1)/(k+3)).astype(float)
def evaluate_scalar_product(self, input_array, output_array, fast_transform=True):
if fast_transform is False:
self.vandermonde_scalar_product(input_array, output_array)
return
output = self.CT.scalar_product(fast_transform=fast_transform)