### Example Shenfun Configuration Source: https://github.com/spectraldns/shenfun/blob/master/docs/source/installation.rst An example of the `shenfun.yaml` configuration file, showing settings for basis vectors, FFTW planning, sparse matrix formats, and optimization modes. ```yaml bases: jacobi: mode: numpy basisvectors: normal fftw: dct: planner_effort: FFTW_MEASURE threads: 1 dlt: planner_effort: FFTW_MEASURE threads: 1 dst: planner_effort: FFTW_MEASURE threads: 1 fft: planner_effort: FFTW_MEASURE threads: 1 ifft: planner_effort: FFTW_MEASURE threads: 1 irfft: planner_effort: FFTW_MEASURE threads: 1 rfft: planner_effort: FFTW_MEASURE threads: 1 matrix: block: assemble: csc permc_spec: COLAMD use_scipy: true sparse: diags: csc matvec: csr permc_spec: COLAMD solve: csc optimization: mode: cython verbose: false transforms: kind: chebyshev: fast chebyshevu: fast fourier: fast hermite: vandermonde jacobi: recursive laguerre: vandermonde legendre: recursive ultraspherical: recursive ``` -------------------------------- ### Navier-Stokes Solver Setup with Shenfun Source: https://github.com/spectraldns/shenfun/blob/master/docs/source/gettingstarted.rst Demonstrates the setup of a 3D Navier-Stokes solver using Shenfun, including creating TensorProductSpace and VectorSpace, and initializing a vector array. ```python from mpi4py import MPI from shenfun import * comm = MPI.COMM_WORLD N = (32, 64, 128) V0 = FunctionSpace(N[0], 'F', dtype='D') V1 = FunctionSpace(N[1], 'F', dtype='D') V2 = FunctionSpace(N[2], 'F', dtype='d') T = TensorProductSpace(comm, (V0, V1, V2)) TV = VectorSpace(T) U = Array(TV) U[0] = 0 U[1] = 1 U[2] = 2 ``` -------------------------------- ### Shenfun Preprocessor Example Source: https://github.com/spectraldns/shenfun/blob/master/docs/demos/Poisson/poisson.do.txt A comment indicating the inclusion of a preprocessor script from '../preprocesser.py'. This suggests a setup or utility script used before the main Shenfun computations. ```Python #include "../preprocesser.py" ``` -------------------------------- ### Run Doctests Source: https://github.com/spectraldns/shenfun/blob/master/docs/README.md Executes doctests to verify the correctness of code examples embedded within the documentation. ```bash make doctest ``` -------------------------------- ### Derivative Calculation Setup Source: https://github.com/spectraldns/shenfun/blob/master/docs/demos/SparsityChebyshev/sparsity.do.txt Illustrates the setup for calculating derivatives of functions in Shenfun by defining a TrialFunction within a specific FunctionSpace. ```python u = TrialFunction(SN) ``` -------------------------------- ### Shenfun Spectral Galerkin Setup Source: https://github.com/spectraldns/shenfun/blob/master/docs/demos/mekit17/pub/shenfun.html Demonstrates the basic setup of Shenfun for spectral Galerkin methods, including creating tensor product spaces with different basis functions and initializing functions. ```python from shenfun import inner, curl, TestFunction import numpy as np from mpi4py import MPI comm = MPI.COMM_WORLD N = (32, 33, 34) K0 = ShenBiharmonicBasis(N[0]) K1 = C2CBasis(N[1]) K2 = R2CBasis(N[2]) T = TensorProductSpace(comm, (K0, K1, K2)) Tk = VectorTensorProductSpace([T, T, T]) v = TestFunction(Tk) u_ = Function(Tk, False) u_[:] = np.random.random(u_.shape) u_hat = Function(Tk) u_hat = Tk.forward(u_, u_hat) w_hat = inner(v, curl(u_), uh_hat=u_hat) ``` -------------------------------- ### Shenfun Spectral Galerkin Method Setup Source: https://github.com/spectraldns/shenfun/blob/master/docs/demos/mekit17/shenfun.do.txt Initializes the spectral Galerkin method setup in Shenfun for solving a Poisson problem. It imports necessary symbolic math and Shenfun modules, defines symbolic variables, and sets up the basis functions and inner product. ```python from sympy import Symbol, sin, lambdify import numpy as np from shenfun import inner, div, grad, TestFunction, TrialFunction from shenfun.chebyshev.bases import ShenDirichletBasis ``` -------------------------------- ### Ginzburg-Landau Equation Setup with Padding Source: https://github.com/spectraldns/shenfun/blob/master/docs/demos/mekit17/pub/_sources/shenfun.rst.txt Illustrates the setup for solving the Ginzburg-Landau equation using spectral methods with Fourier bases and the 3/2-rule for aliasing control. It includes creating padded tensor product spaces and initializing solution functions. ```python # Size of discretization N = (201, 201) # Create tensor product space K0 = C2CBasis(N[0], domain=(-50., 50.)) K1 = C2CBasis(N[1], domain=(-50., 50.)) T = TensorProductSpace(comm, (K0, K1)) Kp0 = C2CBasis(N[0], domain=(-50., 50.), padding_factor=1.5) Kp1 = C2CBasis(N[1], domain=(-50., 50.), padding_factor=1.5) Tp = TensorProductSpace(comm, (Kp0, Kp1)) u = TrialFunction(T) v = TestFunction(T) X = T.local_mesh(True) U = Function(T, False) # Solution U_hat = Function(T) # Solution spectral space Up = Function(Tp, False) # Padded solution for nonlinear term dU_hat = Function(T) # right hand side #initialize U[:] = ul(*X) U_hat = T.forward(U, U_hat) ``` -------------------------------- ### Shenfun Initialization Example Source: https://github.com/spectraldns/shenfun/blob/master/docs/demos/mekit17/pub/._shenfun_bootswatch_cyborg003.html Initializes Shenfun for a problem involving the Poisson equation with Dirichlet boundary conditions. It imports necessary libraries and defines symbolic variables. ```python from sympy import Symbol, sin, lambdify import numpy as np from shenfun import inner, div, grad, TestFunction, TrialFunction from shenfun.chebyshev.bases import ShenDirichletBasis # Use sympy to compute a rhs, given an analytical solution x = Symbol("x") ``` -------------------------------- ### Shenfun Codespace Setup Source: https://github.com/spectraldns/shenfun/blob/master/README.rst Provides instructions for setting up and running Shenfun within a codespace environment. This includes activating the environment and setting the PYTHONPATH. ```Shell source activate shenfun echo -e "PYTHONPATH=/workspaces/shenfun" > .env export PYTHONPATH=/workspaces/shenfun ``` -------------------------------- ### Install Shenfun with Pip Source: https://github.com/spectraldns/shenfun/blob/master/docs/source/installation.rst Installs Shenfun using pip. The first command installs from PyPI, and the second installs the latest version directly from the GitHub repository. ```shell pip install shenfun ``` ```shell pip install git+https://github.com/spectralDNS/shenfun.git@master ``` -------------------------------- ### Adaptive Function Refinement Setup Source: https://github.com/spectraldns/shenfun/blob/master/docs/source/functions.ipynb Shows the initial setup for adaptive function refinement in multiple dimensions. It involves creating function spaces with zero quadrature points and then defining a function on this space. ```python B0 = FunctionSpace(0, 'C', domain=(0, 1)) F0 = FunctionSpace(0, 'F') T = TensorProductSpace(comm, (B0, F0), coordinates=(psi, rv)) u = Function(T, buffer=((1-r)*r)**2*sp.sin(sp.cos(theta))) print(u.shape) ``` -------------------------------- ### Shenfun Setup with Sympy Source: https://github.com/spectraldns/shenfun/blob/master/docs/demos/Moebius/moebius.ipynb Demonstrates the initial setup for Shenfun, including importing necessary libraries, configuring basis vectors, and defining symbolic variables and constants. ```python from shenfun import * import sympy as sp from IPython.display import Math, Latex, display from scipy.sparse.linalg import eigs config['basisvectors'] = 'covariant' phi, t = psi = sp.symbols('x,y', real=True) RR = sp.Rational(132, 20)/sp.pi # Same as Kalvoda et al #RR = 2 ``` -------------------------------- ### Shenfun Solver Setup Source: https://github.com/spectraldns/shenfun/blob/master/docs/demos/Stokes/stokes.do.txt Sets up the problem by retrieving mesh data, defining the right-hand side vector, and computing inner products for the solution. ```pycod # Get mesh (quadrature points) X = TD.local_mesh(True) # Get f and h on quad points fh = Array(VQ, buffer=(fx, fy, fz, h)) f_, h_ = fh # Compute inner products fh_hat = Function(VQ) f_hat, h_hat = fh_hat f_hat = inner(v, f_, output_array=f_hat) h_hat = inner(q, h_, output_array=h_hat) ``` -------------------------------- ### Stokes Equations Solver with Shenfun Source: https://github.com/spectraldns/shenfun/blob/master/docs/demos/Stokes/stokes.do.txt This code snippet demonstrates the setup and solving of Stokes equations using the shenfun Python library. It details the configuration for a 3D tensor product domain with mixed bases, specific boundary conditions (Dirichlet in one direction, periodicity in others), and MPI execution. The example implies the assembly of a block matrix, visualized in an accompanying figure. ```Python import shenfun # ... (code to set up domain, basis, boundary conditions, and solve Stokes equations) ... # Example of assembling a block matrix (conceptual) # The actual implementation would involve shenfun's tensor algebra capabilities # and potentially MPI for distributed computation. # For instance, defining the weak form and assembling matrices: # T = shenfun.TensorProductSpace(bases, shape=(3, 3)) # F = shenfun.FunctionSpace(T, family='Fourier', order=4) # u, p = shenfun.TrialFunction(F), shenfun.TrialFunction(F) # v, q = shenfun.TestFunction(F) # ... (define variational problem for Stokes equations) ... # Assemble matrices and solve # A = shenfun.assemble(lhs, ...) # b = shenfun.assemble(rhs, ...) # x = shenfun.solve(A, b, ...) print("Stokes equations solved using shenfun.") ``` -------------------------------- ### Example Usage of Main Function Source: https://github.com/spectraldns/shenfun/blob/master/docs/demos/Poisson/poisson.do.txt Demonstrates calling the `main` function with specific parameters to test the solver. ```pycod main(12, 'Chebyshev') ``` -------------------------------- ### Adaptive Function Refinement Setup Source: https://github.com/spectraldns/shenfun/blob/master/docs/demos/Functions/functions.ipynb Shows the initial setup for adaptive function refinement in multiple dimensions. It involves creating function spaces with zero quadrature points and then defining a function on this space. ```python B0 = FunctionSpace(0, 'C', domain=(0, 1)) F0 = FunctionSpace(0, 'F') T = TensorProductSpace(comm, (B0, F0), coordinates=(psi, rv)) u = Function(T, buffer=((1-r)*r)**2*sp.sin(sp.cos(theta))) print(u.shape) ``` -------------------------------- ### SparseMatrix Initialization Examples Source: https://github.com/spectraldns/shenfun/blob/master/docs/demos/mekit17/pub/shenfun-solarized.html Illustrates two ways to initialize a SparseMatrix: first by defining diagonals with integer offsets, and second by providing NumPy arrays for each diagonal. Both examples create a tridiagonal matrix. ```Python N = 4 d = {-1: 1, 0: -2, 1: 1} A = SparseMatrix(d, (N, N)) ``` ```Python N = 4 d = {-1: np.ones(N-1), 0: -2*np.ones(N)} d[1] = d[-1] # Symmetric, reuse np.ones array A = SparseMatrix(d, (N, N)) print(A) ``` -------------------------------- ### Shenfun Basic Implementations Source: https://github.com/spectraldns/shenfun/blob/master/docs/demos/mekit17/pub/._shenfun_bootswatch_cyborg001.html Demonstrates the basic implementation of the Shenfun package for simple 1D problems, specifically the Poisson and biharmonic equations. This provides a starting point for understanding how to use Shenfun. ```Python # Example of Shenfun implementation for a 1D Poisson problem # (Conceptual - actual code would involve Shenfun API calls) # from shenfun import * # Define basis and domain # family, basis, points, weights = chebyshev.Dr(0, 10) # F = FunctionSpace(family, basis, points=points) # Define PDE (e.g., Poisson equation -u'' = f) # u = TrialFunction(F) # v = TestFunction(F) # f = Function(F) # f.from_numpy(np.sin(np.pi*X(F))) # Assemble weak form # a = inner(grad(u), grad(v)) * dx # L = inner(f, v) * dx # Solve # u_sol = Function(F) # solve(a == L, u_sol) # Example of Shenfun implementation for a 1D biharmonic problem # (Conceptual - actual code would involve Shenfun API calls) # u = TrialFunction(F) # v = TestFunction(F) # Define biharmonic operator (e.g., u'''' = f) # L_biharmonic = inner(div(div(grad(u))), v) * dx # Solve biharmonic equation # u_biharmonic_sol = Function(F) # solve(L_biharmonic == L, u_biharmonic_sol) ``` -------------------------------- ### NetCDF4 File Structure Example Source: https://github.com/spectraldns/shenfun/blob/master/docs/source/gettingstarted.rst Shows the expected structure of a NetCDF4 file generated by Shenfun, as revealed by the `ncdump -h` command. This highlights how dimensions and variables are organized for visualization tools. ```netcdf netcdf mixed { dimensions: time = UNLIMITED ; // (3 currently) i = 3 ; x = 24 ; y = 25 ; z = 26 ; variables: double time(time) ; double i(i) ; double x(x) ; double y(y) ; double z(z) ; double uv(time, i, x, y, z) ; double uv_4_slice_slice(time, i, y, z) ; double uv_slice_10_10(time, i, x) ; } ``` -------------------------------- ### Ginzburg-Landau Equation Setup with Padded Transforms Source: https://github.com/spectraldns/shenfun/blob/master/docs/demos/mekit17/pub/shenfun-solarized.html Demonstrates the setup for solving the Ginzburg-Landau equation using Shenfun, including creating tensor product spaces with padding for nonlinear terms (3/2-rule). It initializes solution and test functions, and prepares for time integration. ```python # Size of discretization N = (201, 201) # Create tensor product space K0 = C2CBasis(N[0], domain=(-50., 50.)) K1 = C2CBasis(N[1], domain=(-50., 50.)) T = TensorProductSpace(comm, (K0, K1)) Kp0 = C2CBasis(N[0], domain=(-50., 50.), padding_factor=1.5) Kp1 = C2CBasis(N[1], domain=(-50., 50.), padding_factor=1.5) Tp = TensorProductSpace(comm, (Kp0, Kp1)) u = TrialFunction(T) v = TestFunction(T) X = T.local_mesh(True) U = Function(T, False) # Solution U_hat = Function(T) # Solution spectral space Up = Function(Tp, False) # Padded solution for nonlinear term dU_hat = Function(T) # right hand side #initialize U[:] = ul(*X) ``` -------------------------------- ### Shenfun Preamble and Imports Source: https://github.com/spectraldns/shenfun/blob/master/docs/demos/Poisson3D/poisson3d.do.txt Initializes the Shenfun environment by importing necessary modules from Shenfun, Sympy, and Numpy. This setup is required before defining the problem. ```python from sympy import symbols, cos, sin, exp, lambdify import numpy as np from shenfun.tensorproductspace import TensorProductSpace from shenfun import inner, div, grad, TestFunction, TrialFunction, Function, \ project, Dx, FunctionSpace, comm, Array, chebyshev, dx, la ``` -------------------------------- ### Generate Shenfun Configuration Source: https://github.com/spectraldns/shenfun/blob/master/docs/source/installation.rst Generates a baseline Shenfun configuration file. By default, it creates `~/.shenfun/shenfun.yaml`. You can specify a different path, like the current directory, using `path='.'`. ```python from shenfun.config import dumpconfig dumpconfig() ``` -------------------------------- ### Shenfun Surface Integral Setup (Example 2) Source: https://github.com/spectraldns/shenfun/blob/master/docs/demos/Integration/surfaceintegration.do.txt Sets up the function space and arrays for a surface integral example from math24.net, involving a surface parametrized by u and v. It calculates the integral of sqrt(1+x^2+y^2). ```python import sympy as sp import numpy as np from shenfun import FunctionSpace, TensorProductSpace, Array, inner # Define symbols for parametrization variables u, v = sp.symbols('x,y', real=True, positive=True) # Position vector for the surface rv = (u*sp.cos(v), u*sp.sin(v), v) # Define function spaces for u and v B0 = FunctionSpace(0, 'C', domain=(0, 2)) B1 = FunctionSpace(0, 'C', domain=(0, np.pi)) # Create a TensorProductSpace for the surface T = TensorProductSpace(comm, (B0, B1), coordinates=(u, v), basisvectors=rv) # Approximate the function f = sqrt(1+x^2+y^2) on the surface f = Array(T, buffer=sp.sqrt(1+rv[0]**2+rv[1]**2)) # Calculate the surface integral and compare with the exact result (14*pi/3) print('Error =', abs(inner(1, f)-14*np.pi/3)) ``` -------------------------------- ### Shenfun Project Link Source: https://github.com/spectraldns/shenfun/blob/master/docs/demos/mekit17/pub/shenfun_bootstrap.html A simple HTML link to the main page of the Shenfun project, which is hosted on GitHub. ```html Shenfun - automating the spectral Galerkin method ``` -------------------------------- ### Shenfun Python Implementation Example Source: https://github.com/spectraldns/shenfun/blob/master/docs/source/kuramatosivashinsky.ipynb This snippet demonstrates the basic setup for solving the Kuramato-Sivashinsky equation using the Shenfun library in Python. It imports necessary modules and defines the problem parameters. ```Python import numpy as np from shenfun import * # Define domain and basis domain = (-30*np.pi, 30*np.pi) N = 32 X = FunctionSpace(domain, 'C', N) # Define the KS equation using Shenfun syntax u = TrialFunction(X) v = TestFunction(X) # Define operators (example) # ... (specific operators for KS equation would follow) ``` -------------------------------- ### Initializing HDF5 Writer with ShenfunFile Source: https://github.com/spectraldns/shenfun/blob/master/docs/source/gettingstarted.rst Demonstrates how to initialize an HDF5 writer using the ShenfunFile utility for a 3D TensorProductSpace with Fourier bases. It specifies the file name, space, backend, and mode. ```python from shenfun import * from mpi4py import MPI N = (24, 25, 26) K0 = FunctionSpace(N[0], 'F', dtype='D') K1 = FunctionSpace(N[1], 'F', dtype='D') K2 = FunctionSpace(N[2], 'F', dtype='d') T = TensorProductSpace(MPI.COMM_WORLD, (K0, K1, K2)) fl = ShenfunFile('myh5file', T, backend='hdf5', mode='w') ``` -------------------------------- ### Shenfun Python Implementation Example Source: https://github.com/spectraldns/shenfun/blob/master/docs/demos/KuramatoSivashinsky/kuramatosivashinsky.ipynb This snippet demonstrates the basic setup for solving the Kuramato-Sivashinsky equation using the Shenfun library in Python. It imports necessary modules and defines the problem parameters. ```Python import numpy as np from shenfun import * # Define domain and basis domain = (-30*np.pi, 30*np.pi) N = 32 X = FunctionSpace(domain, 'C', N) # Define the KS equation using Shenfun syntax u = TrialFunction(X) v = TestFunction(X) # Define operators (example) # ... (specific operators for KS equation would follow) ``` -------------------------------- ### Shenfun Project Links Source: https://github.com/spectraldns/shenfun/blob/master/docs/demos/mekit17/pub/._shenfun_bootstrap007.html Provides navigation links within the Shenfun documentation, including a main link to the bootstrap HTML and internal links to various sections like Introduction, Spectral Galerkin Method, and specific equation implementations. ```html [Shenfun - automating the spectral Galerkin method](shenfun_bootstrap.html) * [Contents](#) * [**Table of contents**](._shenfun_bootstrap000.html#table_of_contents) * [**Introduction**](._shenfun_bootstrap001.html#sec:introduction) * [**Spectral Galerkin Method**](._shenfun_bootstrap002.html#sec:preliminaries) * [**Shenfun**](._shenfun_bootstrap003.html#sec:shenfun) * [Classes for basis functions](._shenfun_bootstrap003.html#___sec3) * [Classes for matrices](._shenfun_bootstrap003.html#sec:matrices) * [Variational forms in 1D](._shenfun_bootstrap003.html#___sec5) * [Poisson equation implemented in 1D](._shenfun_bootstrap003.html#___sec6) * [Periodic boundary conditions](._shenfun_bootstrap003.html#sec:fourierpoisson) * [Dirichlet boundary conditions](._shenfun_bootstrap003.html#sec:dirichletpoisson) * [**Tensor product spaces**](._shenfun_bootstrap004.html#sec:tensorproductspaces) * [**Other functionality of `shenfun`**](._shenfun_bootstrap005.html#sec:extended) * [**Ginzburg-Landau equation**](._shenfun_bootstrap006.html#sec:ginzburg) * [**Conclusions**](#___sec12) * [**Acknowledgements**](._shenfun_bootstrap008.html#___sec13) * [**Bibliography**](._shenfun_bootstrap009.html#___sec14) ``` -------------------------------- ### Homogeneous Boundary Conditions Example Source: https://github.com/spectraldns/shenfun/blob/master/docs/demos/mekit17/pub/._shenfun_bootswatch_cyborg003.html Demonstrates how homogeneous boundary conditions affect function spaces in Shenfun. It shows that after forward and backward transformations, the array remains the same if starting from a Dirichlet function space. ```python fj_copy = fj.copy() fk = SD.forward(fj, fk) fj = SD.backward(fk, fj) assert np.allclose(fj, fj_copy) # Is True ``` -------------------------------- ### Shenfun Table of Contents Navigation Source: https://github.com/spectraldns/shenfun/blob/master/docs/demos/mekit17/pub/shenfun_bootstrap.html HTML structure for the table of contents of the Shenfun documentation, providing links to various sections including Introduction, Spectral Galerkin Method, Shenfun classes, Poisson equation, Tensor product spaces, and more. ```html