|
| 1 | +r""" |
| 2 | + One-dimensional IMEX acoustic-advection |
| 3 | + ========================= |
| 4 | + |
| 5 | + Integrate the linear 1D acoustic-advection problem: |
| 6 | + |
| 7 | + .. math:: |
| 8 | + u_t + U u_x + c p_x & = 0 \\ |
| 9 | + p_t + U p_x + c u_x & = 0. |
| 10 | + |
| 11 | +""" |
| 12 | + |
| 13 | +import numpy as np |
| 14 | +import scipy.sparse as sp |
| 15 | +import scipy.sparse.linalg as LA |
| 16 | + |
| 17 | +from pySDC.Problem import ptype |
| 18 | +from pySDC.datatype_classes.mesh import mesh, rhs_imex_mesh |
| 19 | + |
| 20 | +from buildWave1DMatrix import getWave1DMatrix, getWave1DAdvectionMatrix |
| 21 | + |
| 22 | +class acoustic_1d_imex(ptype): |
| 23 | + """ |
| 24 | + Example implementing the forced 1D heat equation with Dirichlet-0 BC in [0,1] |
| 25 | +
|
| 26 | + Attributes: |
| 27 | + solver: Sharpclaw solver |
| 28 | + state: Sharclaw state |
| 29 | + domain: Sharpclaw domain |
| 30 | + """ |
| 31 | + |
| 32 | + def __init__(self, cparams, dtype_u, dtype_f): |
| 33 | + """ |
| 34 | + Initialization routine |
| 35 | +
|
| 36 | + Args: |
| 37 | + cparams: custom parameters for the example |
| 38 | + dtype_u: particle data type (will be passed parent class) |
| 39 | + dtype_f: acceleration data type (will be passed parent class) |
| 40 | + """ |
| 41 | + |
| 42 | + # these parameters will be used later, so assert their existence |
| 43 | + assert 'nvars' in cparams |
| 44 | + assert 'cs' in cparams |
| 45 | + assert 'cadv' in cparams |
| 46 | + assert 'order_adv' in cparams |
| 47 | + assert 'multiscale' in cparams |
| 48 | + |
| 49 | + # add parameters as attributes for further reference |
| 50 | + for k,v in cparams.items(): |
| 51 | + setattr(self,k,v) |
| 52 | + |
| 53 | + # invoke super init, passing number of dofs, dtype_u and dtype_f |
| 54 | + super(acoustic_1d_imex,self).__init__(self.nvars,dtype_u,dtype_f) |
| 55 | + |
| 56 | + self.mesh = np.linspace(0.0, 1.0, self.nvars[1], endpoint=False) |
| 57 | + self.dx = self.mesh[1] - self.mesh[0] |
| 58 | + |
| 59 | + self.Dx = -self.cadv*getWave1DAdvectionMatrix(self.nvars[1], self.dx, self.order_adv) |
| 60 | + self.Id, A = getWave1DMatrix(self.nvars[1], self.dx, ['periodic','periodic'], ['periodic','periodic']) |
| 61 | + self.A = -self.cs*A |
| 62 | + |
| 63 | + def solve_system(self,rhs,factor,u0,t): |
| 64 | + """ |
| 65 | + Simple linear solver for (I-dtA)u = rhs |
| 66 | +
|
| 67 | + Args: |
| 68 | + rhs: right-hand side for the nonlinear system |
| 69 | + factor: abbrev. for the node-to-node stepsize (or any other factor required) |
| 70 | + u0: initial guess for the iterative solver (not used here so far) |
| 71 | + t: current time (e.g. for time-dependent BCs) |
| 72 | +
|
| 73 | + Returns: |
| 74 | + solution as mesh |
| 75 | + """ |
| 76 | + |
| 77 | + M = self.Id - factor*self.A |
| 78 | + |
| 79 | + b = np.concatenate( (rhs.values[0,:], rhs.values[1,:]) ) |
| 80 | + |
| 81 | + sol = LA.spsolve(M, b) |
| 82 | + |
| 83 | + me = mesh(self.nvars) |
| 84 | + me.values[0,:], me.values[1,:] = np.split(sol, 2) |
| 85 | + |
| 86 | + return me |
| 87 | + |
| 88 | + |
| 89 | + def __eval_fexpl(self,u,t): |
| 90 | + """ |
| 91 | + Helper routine to evaluate the explicit part of the RHS |
| 92 | +
|
| 93 | + Args: |
| 94 | + u: current values (not used here) |
| 95 | + t: current time |
| 96 | +
|
| 97 | + Returns: |
| 98 | + explicit part of RHS |
| 99 | + """ |
| 100 | + |
| 101 | + |
| 102 | + b = np.concatenate( (u.values[0,:], u.values[1,:]) ) |
| 103 | + sol = self.Dx.dot(b) |
| 104 | + |
| 105 | + fexpl = mesh(self.nvars) |
| 106 | + fexpl.values[0,:], fexpl.values[1,:] = np.split(sol, 2) |
| 107 | + |
| 108 | + return fexpl |
| 109 | + |
| 110 | + |
| 111 | + def __eval_fimpl(self,u,t): |
| 112 | + """ |
| 113 | + Helper routine to evaluate the implicit part of the RHS |
| 114 | +
|
| 115 | + Args: |
| 116 | + u: current values |
| 117 | + t: current time (not used here) |
| 118 | +
|
| 119 | + Returns: |
| 120 | + implicit part of RHS |
| 121 | + """ |
| 122 | + |
| 123 | + b = np.concatenate( (u.values[0,:], u.values[1,:]) ) |
| 124 | + sol = self.A.dot(b) |
| 125 | + |
| 126 | + fimpl = mesh(self.nvars,val=0) |
| 127 | + fimpl.values[0,:], fimpl.values[1,:] = np.split(sol, 2) |
| 128 | + |
| 129 | + return fimpl |
| 130 | + |
| 131 | + |
| 132 | + def eval_f(self,u,t): |
| 133 | + """ |
| 134 | + Routine to evaluate both parts of the RHS |
| 135 | +
|
| 136 | + Args: |
| 137 | + u: current values |
| 138 | + t: current time |
| 139 | +
|
| 140 | + Returns: |
| 141 | + the RHS divided into two parts |
| 142 | + """ |
| 143 | + |
| 144 | + f = rhs_imex_mesh(self.nvars) |
| 145 | + f.impl = self.__eval_fimpl(u,t) |
| 146 | + f.expl = self.__eval_fexpl(u,t) |
| 147 | + return f |
| 148 | + |
| 149 | + def u_exact(self,t): |
| 150 | + """ |
| 151 | + Routine to compute the exact solution at time t |
| 152 | +
|
| 153 | + Args: |
| 154 | + t: current time |
| 155 | +
|
| 156 | + Returns: |
| 157 | + exact solution |
| 158 | + """ |
| 159 | + |
| 160 | + sigma_0 = 0.1 |
| 161 | + k = 7.0*2.0*np.pi |
| 162 | + x_0 = 0.75 |
| 163 | + x_1 = 0.25 |
| 164 | + |
| 165 | + ms = 0.0 |
| 166 | + if self.multiscale: |
| 167 | + ms = 1.0 |
| 168 | + |
| 169 | + me = mesh(self.nvars) |
| 170 | + me.values[0,:] = np.exp(-np.square(self.mesh-x_0-self.cs*t)/(sigma_0*sigma_0)) + ms*np.exp(-np.square(self.mesh-x_1-self.cs*t)/(sigma_0*sigma_0))*np.cos(k*(self.mesh-self.cs*t)/sigma_0) |
| 171 | + me.values[1,:] = me.values[0,:] |
| 172 | + return me |
| 173 | + |
| 174 | + |
0 commit comments