Code Print

Download as pdf
Download as pdf
You are on page 1of 3
In [16]: import numpy a5 np inport natplotlib,pyplot as plt class ayorid: "a Finite-difference grid, 4 Constructor sethod for ayérid class def _init_{(self,mng:2,min=d.0,ymaxe.0): create a grid with mx nodes, ng ghost nodes (an each side), donain ranges fron [xan, max) ."** selfomin = ain selfomex = ax selfang = ng self.me = me 4 numpy arrays are zero-based 4 Make indices to find out where the real physical grid, dato values, # start ond end self. ibegin = ng # physical grid coordinates self.dx = (max = anin)/(nx - 1) selfxcoords = ain + (ap. arange(nx+2%ng)-ng)*self.dx # array for storing solution self.u = npzeros((self.nxs2%self.ng), dtypeenp floats) self uinit = np.reros((self.nxe2"self.ng), dtypesnp.floatsA) def array nenory allocator(self): allocate nenory for an arbitrary array having sane dimensions as the grid."™* return np,zeros((self.nx+2*self.ng), dtype=np. Floatéd) def apply BCs): "updating the single ghost node with periodic boundary conditions self.uself-ibgin-] = self.ufself.iend-1] # left ghost node self.u[self.iends] = self.ufself.ibeginet] # right ghost node ef alrance_fou{ox, cy Coy num periods=1.0, init condone): ‘ sclve the 1D Linear advection using FOU. Inputs: Pass a function f(g), where g is grid object, that sets up the initial condition, # create a grid object using nyorid class g = wycrid(m) + comute the tine step, dt at = Cotgatxfe t start = 0.0 ‘max = num periods®(gmax-g.xnin)/np.abs(c) ‘tLeurrent = t start, # initialize the data snitcond(g) g.uinit{:] = g.u[:] # Copy the initial condition to uinit # Tine integration ue = g,array_nenary allocator) hile t_current < t_max: 4€ Laurrent + dt > t max: dt = timax - t current co clet/g.dx +# apply the periodic boundary conditions B-apply_B3() # FOU update for i in range(g.ibegin, g.iende): unev{i] = gui] ~ Co (eof Li-1]) +# update the solution for the next tine step eof] = unenl:] # Increnent t current by dt ‘turrent 4= dt return g # Function to initialize the state of u # we will take a hat function def hat geul:] = 0.0 ulnp.logical_and(g.xcoords >= ./3, Bexcoords <= 2./3)] = 1.8 #5a00th Initial Condition def sine vave(g): g.u[:] = np.sin(2*np.pi*g.xcoords) def plat (g): fig = plt.figure() ax = Fig.add_subplot(111) ax.plot(g.xcoords(g. ibegin: ax,plot(g.xcoords g.ibegin: ax.legend() plt.show() iendst], g.uinit{g. iend+1], g.ulg.ibegin: .g-iendet], label=" nit cond") ‘iend+1]) # Try it out m= 128 cel = 0.95 g.= advance fou(rx, c, Co, num periods=1, init_cond=sine nave) plot(g) 100 — Init Cond 075 050 os 00 0B 050 05

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy