def implementationOne(): # constants u0 = 1.0 C_f = 1.0 C_s = 1.0 # create the blocks, with no parameters or parameter functions b0 = b.Block( '0', # name {'u': u0}) # boundary state, this is fixed b1 = b.Block( '1', # name { 'u': 0, 'v': 0 }) # states, and their initial guesses b2 = b.Block( '2', # name {'u': 0}) # states, and their initial guesses # create the fluxes # flux from 0 to 1 F_P = {'C_f': C_f} # dictionary of parameters for the flux # flux is from b0, function is F_u f_u01 = f.Flux(b0, F_u, F_P, 'f_u01') # flux is from b1, function is F_u f_u12 = f.Flux(b1, F_u, F_P, 'f_u12') # flux is from b2, function is F_v # parameter argument is optional, if not used # we don't technically need a name, they really exist # for debugging, and if we want to do something that # requires us to remove the flux, here it is defaulted to '' f_v21 = f.Flux(b2, F_v) # add these fluxes to the right blocks b1.addFlux(f_u01) b1.addFlux(f_v21) b2.addFlux(f_u12) # create the sources S_uP = {'C_s': C_s} S_vP = {'g': g} s_u1 = s.Source(S_u, S_uP, 's_u1') s_u2 = s.Source(S_u, S_uP, 's_u2') # again name argument is optional s_v1 = s.Source(S_v, S_vP) b1.addSource(s_u1) b2.addSource(s_u2) # create and solve the problem problem = p.Problem([b1, b2]) problem.solve() # the blocks now contain their own values, # which are the correct values print "Running first implementation of example code" print b1 print b2
def implementationTwo(): # constants u0 = 1.0 C_f = 1.0 C_s = 1.0 # create the blocks, with no parameters or parameter functions b1 = b.Block( '1', # name { 'u': 0, 'v': 0 }) # states, and their initial guesses b2 = b.Block( '2', # name {'u': 0}) # states, and their initial guesses # create the fluxes # flux from 0 to 1 F_P = {'C_f': C_f} # dictionary of parameters for the flux # flux is from b1, function is F_u f_u12 = f.Flux(b1, F_u, F_P, 'f_u12') # flux is from b2, function is F_v # name argument is optional # parameter argument is optional too, if not used f_v21 = f.Flux(b2, F_v) # add these fluxes to the right blocks b1.addFlux(f_v21) b2.addFlux(f_u12) # create the sources # these are parameters for the new boundary source S_0P = {'C_f': C_f, 'u_0': u0} S_uP = {'C_s': C_s} S_vP = {'g': g} s_u0 = s.Source(S_0, S_0P, 'boundary') s_u1 = s.Source(S_u, S_uP, 's_u1') s_u2 = s.Source(S_u, S_uP, 's_u2') # again name argument is optional s_v1 = s.Source(S_v, S_vP) # add the boundary source in with the other ones b1.addSource(s_u0) b1.addSource(s_u1) b2.addSource(s_u2) # create and solve the problem problem = p.Problem([b1, b2]) problem.solve() # the blocks now contain their own values, # which are the correct values print "Running second implementation of example code" print b1 print b2
def diffusion2D(N): d = 2./float(N) # spacing, delta # initialize with exact solution at t = 0 B = [] for i in range(-1,N+1): for j in range(-1,N+1): x = (i*d+d/2) y = (j*d+d/2) # block has a simple name name = '('+str(i)+','+str(j)+')' B.append(b.Block(name, # initialize to exact solution {'u':math.sin(u_a*math.pi*x)*math.sin(u_b*math.pi*y), 'v':math.sin(v_a*math.pi*x)*math.sin(v_b*math.pi*y)}, None, # no parameter functions {'x':x,'y':y})) # Flux geometry P = {'d':d*d} n = N+2 # add two for the boundaries for i in range(1,n-1): for j in range(1,n-1): # Add fluxes, looping around the edges for k in [(i-1)*n+j, i*n+j-1,(i+1)*n+j, i*n+j+1]: B[i*n+j].addFlux(f.Flux(B[k],difference,P)) interiorBlocks = [B[i*n+j] for i in range(1,n-1) for j in range(1,n-1)] # sort out the boundary blocks boundaryBlocks = [] bcRange = [j for j in range(1,n-1)] + \ [(n-1)*n+j for j in range(1,n-1)] + \ [i*n for i in range(0,n)] + \ [i*n+n-1 for i in range(0,n)] for k in bcRange: B[k].addSource(s.Source(time,None,B[k].name)) boundaryBlocks.append(B[k]) # solve the problem on the interior blocks, # pass in the boundary blocks so they can be updated # at each needed time. # solve the unstead problem at the following timesteps P = p.Problem(interiorBlocks,boundaryBlocks) P.solveUnst(np.linspace(0,tf,10)) # calculate the error for accuracy checking Eu = 0 Ev = 0 for bb in interiorBlocks: (x,y) = (bb.p['x'],bb.p['y']) ue = math.exp(-nu_u*math.pi*math.pi*(u_a*u_a+u_b*u_b)*tf)*math.sin(u_a*math.pi*x)*math.sin(u_b*math.pi*y) ve = math.exp(-nu_v*math.pi*math.pi*(v_a*v_a+v_b*v_b)*tf)*math.sin(v_a*math.pi*x)*math.sin(v_b*math.pi*y) Eu += (bb['u']-ue)**2 Ev += (bb['v']-ve)**2 return (math.sqrt(Eu/(n-2)/(n-2)),math.sqrt(Ev)/(n-2)/(n-2))
def poisson2D(N): """ lets define a uniform square mesh on [-1, 1] x [1, 1] and create boundary blocks as we go, initializing based on the exact solution, and naming the block by its coordinates """ d = 2. / float(N) # spacing, delta X B = [ b.Block('(' + str(i * d - d / 2 - 1) + ',' + str(j * d - d / 2 - 1) + ')', None, u=math.exp((i * d - d / 2 - 1) * (j * d - d / 2 - 1)), v=math.exp((i * d - d / 2 - 1)**2 + (j * d - d / 2 - 1)**2)) for i in range(0, N + 2) for j in range(0, N + 2) ] # interior sources # use the name to get the source values for block in B: (x, y) = eval(block.name) block.addSource( s.Source('const', u=-(x * x + y * y) * math.exp(x * y), v=-4.0 * (x * x + y * y + 1.0) * math.exp(x * x + y * y))) # Flux geometry G = {'type': 'edge', 'd': d * d, 'm': []} n = N + 2 # add two for the boundaries for i in range(1, n - 1): for j in range(1, n - 1): # Add fluxes, no "normals" so we cheat and define them cleverly for k in [(i - 1) * n + j, i * n + j - 1, (i + 1) * n + j, i * n + j + 1]: B[i * n + j].addFlux(f.Flux(B[k], 'difference', G)) B[i * n + j].state['u'] = 0. B[i * n + j].state['v'] = 0. interiorBlocks = [ B[i * n + j] for i in range(1, n - 1) for j in range(1, n - 1) ] # solve the problem on the interior blocks P = p.Problem(interiorBlocks) P.solve() Eu = math.sqrt( sum([(math.exp(eval(block.name)[0] * eval(block.name)[1]) - block.state['u'])**2 for block in interiorBlocks]) / (n - 2) / (n - 2)) Ev = math.sqrt( sum([(math.exp(eval(block.name)[0]**2 + eval(block.name)[1]**2) - block.state['v'])**2 for block in interiorBlocks]) / (n - 2) / (n - 2)) return (Eu, Ev)
def diff2D(N): """ lets define a uniform square mesh on [-1, 1] x [1, 1] and create boundary blocks as we go, initializing based on the exact solution, and naming the block by its coordinates """ u_a = 1 u_b = 1 nu_u = 1 v_a = 1 v_b = 1 nu_v = 0.5 tf = 1 d = 2. / float(N) # spacing, delta # initialize with exact solution at t = 0 B = [b.Block('('+str(i*d-d/2-1)+','+str(j*d-d/2-1)+')',None, u = math.sin(u_a*math.pi*(i*d-d/2-1))*math.sin(u_b*math.pi*(j*d-d/2-1)), v = math.sin(v_a*math.pi*(i*d-d/2-1))*math.sin(v_b*math.pi*(j*d-d/2-1))) \ for i in range(0,N+2) for j in range(0,N+2)] # Flux geometry G = {'type': 'edge', 'd': d * d, 'm': []} n = N + 2 # add two for the boundaries for i in range(1, n - 1): for j in range(1, n - 1): # Add fluxes, no "normals" so we cheat and define them cleverly for k in [(i - 1) * n + j, i * n + j - 1, (i + 1) * n + j, i * n + j + 1]: B[i * n + j].addFlux(f.Flux(B[k], 'difference', G)) B[i * n + j].state['u'] = 0. B[i * n + j].state['v'] = 0. interiorBlocks = [ B[i * n + j] for i in range(1, n - 1) for j in range(1, n - 1) ] boundaryBlocks = [] bcRange = [j for j in range(1,n-1)] + \ [(n-1)*n+j for j in range(1,n-1)] + \ [i*n for i in range(0,n)] + \ [i*n+n-1 for i in range(0,n)] for k in bcRange: (x, y) = eval(B[k].name) B[k].addSource(s.Source('time', \ u = lambda t: math.exp(-nu_u*math.pi*math.pi*(u_a*u_a+u_b*u_b)*t)*math.sin(u_a*math.pi*x)*math.sin(u_b*math.pi*y), \ v = lambda t: math.exp(-nu_v*math.pi*math.pi*(v_a*v_a+v_b*v_b)*t)*math.sin(v_a*math.pi*x)*math.sin(v_b*math.pi*y))) boundaryBlocks.append(B[k]) # solve the problem on the interior blocks P = p.Problem(interiorBlocks, boundaryBlocks) P.solveUnst(0, tf, 10) # P.printSolution() Eu = 0 Ev = 0 t = tf for bb in interiorBlocks: (x, y) = eval(bb.name) ue = math.exp(-nu_u * math.pi * math.pi * (u_a * u_a + u_b * u_b) * t) * math.sin( u_a * math.pi * x) * math.sin(u_b * math.pi * y) ve = math.exp(-nu_v * math.pi * math.pi * (v_a * v_a + v_b * v_b) * t) * math.sin( v_a * math.pi * x) * math.sin(v_b * math.pi * y) Eu += (bb.state['u'] - ue)**2 Ev += (bb.state['v'] - ve)**2 # Eu = math.sqrt(sum([(math.exp(-nu_u*math.pi*math.pi*(u_a*u_a+u_b*u_b)*tf)*math.sin(u_a*math.pi*block.name[0])*math.sin(u_b*math.pi*block.name[1]) \ # -block.state['u'])**2 for block in interiorBlocks])/(n-2)/(n-2)) # Ev = math.sqrt(sum([(math.exp(-nu_v*math.pi*math.pi*(v_a*v_a+v_b*v_b)*tf)*math.sin(v_a*math.pi*block.name[0])*math.sin(v_b*math.pi*block.name[1]) \ # -block.state['v'])**2 for block in interiorBlocks])/(n-2)/(n-2)) return (math.sqrt(Eu / (n - 2) / (n - 2)), math.sqrt(Ev) / (n - 2) / (n - 2))
a0.mdot = 0.005 # All these boundary blocks need are temperatures # define Exterior boundary condition aExt = b.Block('Exterior', 'air', T=25.0) # define Interior boundary condition aInt = b.Block('Interior', 'air', T=22.5) """ Sources used in even numbered blocks """ # Here, constant sources are defined using the optional arguments # to pass in information about the source variable (Temperature) # and its value qa = -0.008 # Heat flow into water from Module Heat Receiver qw = -0.003 # Heat flow into air from Heat Loss from the Module Sa = s.Source('const', T=qa) Sw = s.Source('const', T=qw) """ Block Initialization """ w1 = b.Block('water1', 'constWater', T=13) w2 = b.Block('water2', 'constWater', T=13) a1 = b.Block('air1', 'constAir', T=20) a2 = b.Block('air2', 'constAir', T=20) w1.mdot = w0.mdot w2.mdot = w1.mdot a1.mdot = a0.mdot a2.mdot = a1.mdot a2.addSource(Sa) w2.addSource(Sw)
def implementationThree(): # constants u0 = 1.0 C_f = 1.0 C_s = 1.0 # create the blocks, with no parameters or parameter functions b0 = b.Block( '0', # name {'u': u0}) # boundary state, this is fixed b1 = b.Block( '1', # name { 'u': 0, 'v': 0 }, # states, and their initial guesses {'g': g}, # parameter functions { 'C_f': C_f, 'C_s': C_s }) # parameter constants b2 = b.Block( '2', # name {'u': 0}, # states, and their initial guesses None, # no parameter functions are needed in this block { 'C_f': C_f, 'C_s': C_s }) # parameter constants # create the fluxes # flux from 0 to 1 # flux is from b0, function is F_u # no parameter dictionaries needed f_u01 = f.Flux(b0, F_u3) # flux is from b1, function is F_u f_u12 = f.Flux(b1, F_u3) # flux is from b2, function is F_v # name argument is optional # parameter argument is optional too, if not used f_v21 = f.Flux(b2, F_v) # add these fluxes to the right blocks b1.addFlux(f_u01) b1.addFlux(f_v21) b2.addFlux(f_u12) # create the sources # no parameters or names needed, # they come from the plots s_u1 = s.Source(S_u3) s_u2 = s.Source(S_u3) s_v1 = s.Source(S_v3) b1.addSource(s_u1) b2.addSource(s_u2) # create and solve the problem problem = p.Problem([b1, b2]) problem.solve() # the blocks now contain their own values, # which are the correct values print "Running third implementation of example code" print b1 print b2
def poisson2D(N): """ lets define a uniform square mesh on [-1, 1] x [1, 1] and create boundary blocks as we go, initializing based on the exact solution, and naming the block by its coordinates """ d = 2. / float(N) # spacing, Delta x = Delta y # For N blocks in each direction, N+2 blocks since we have a constant block on each end surrounding the domain # a ghost cell approach, there are no parameters in this case, or parameter functions needed # the center of the cell is at (i*d+d/2,j*d+d/2) B = [] for i in range(-1, N + 1): for j in range(-1, N + 1): x = (i * d + d / 2) y = (j * d + d / 2) # block has a simple name name = '(' + str(i) + ',' + str(j) + ')' B.append( b.Block( name, { 'u': math.exp(x * y), 'v': math.exp(x**2 + y**2) }, # initialize to exact solution None, # no parameter functions { 'x': x, 'y': y })) # parameter of coordinates # interior sources, -f['u'] and -f['v'] for block in B: (x, y) = (block.p['x'], block.p['y']) block.addSource( s.Source( s.constant, # standard function defined in source.py { 'u': -(x * x + y * y) * math.exp(x * y), 'v': -4.0 * (x * x + y * y + 1.0) * math.exp(x * x + y * y) }, # parameters 'constant')) # name # Flux geometry P = {'d': d * d} # divide by delta x^2 in the end n = N + 2 # add two for the boundaries for i in range(1, N + 1): for j in range(1, N + 1): # Add fluxes, figuring out which neighbors to connect to for k in [(i - 1) * n + j, i * n + j - 1, (i + 1) * n + j, i * n + j + 1]: B[i * n + j].addFlux(f.Flux(B[k], difference, P)) interiorBlocks = [ B[i * n + j] for i in range(1, N + 1) for j in range(1, N + 1) ] # solve the problem on the interior blocks P = p.Problem(interiorBlocks) P.solve() # compute the L-2 error against the exact solution for both variables Eu = math.sqrt( sum([(math.exp(block.p['x'] * block.p['y']) - block['u'])**2 for block in interiorBlocks]) / (n - 2) / (n - 2)) Ev = math.sqrt( sum([(math.exp(block.p['x']**2 + block.p['y']**2) - block['v'])**2 for block in interiorBlocks]) / (n - 2) / (n - 2)) return (Eu, Ev)
import numpy as np from src import point, vector, line, source # Initializing Wifi source at random 3D location, which we don't know sx, sy, sz = np.random.randint(0, 100, 1)[0], np.random.randint( 0, 100, 1)[0], np.random.randint(0, 100, 1)[0] wifi = source.Source(sx, sy, sz) # Initializing our Detector at 0,0,0 initx, inity, initz = 0, 0, 0 # Getting Intensity of WIfi Signal start = point.Point(initx, inity, initz) start.Intensity(wifi) #################################### XY PLANE ########################################### # Moving to 1,1,0 from 0,0,0 init_p1, init_p2 = point.Point(initx, inity, initz), point.Point(1, 1, initz) # Moving Along XY Plane # Creating a line path where our detector should be moving to detect the source init_line = line.Line(init_p1, init_p2, axis=2) # Moving along above defined line to find 2D location interation = 100 found = False counter = 0 step = 1