def ffcn_bar(self, t, x, x_bar, f, f_bar, p, p_bar, u, u_bar): if self.traced == False: dims = {'x': x.size, 'p': p.size, 'u': u.size} self.trace(dims) v = self.txpu_to_v(t,x,p,u) adolc.zos_forward(123, v, 1) v_bar = adolc.fos_reverse(123, f_bar) self.bar_v_to_txpu(v, v_bar, x, x_bar, p, p_bar, u, u_bar)
def reduced_functional(m): """ Apply the Okada model by unrolling the tape and compute the QoI. """ J = sum(adolc.zos_forward(tape_tag, m, keep=1)) op.J_progress.append(J) return J
def fov_reverse(self,arg): (m,q,u,n,z,x,y) = arg adolc.set_nested_ctx(self.tag_ext_fct,1) ny = adolc.zos_forward(self.tag_ext_fct,2,2,1,x) nz = adolc.fov_reverse(self.tag_ext_fct,2,2,q,u) np.copyto(z,nz) return 1
def zos_forward(self,arg): (n,yin,m,yout) = arg adolc.set_nested_ctx(self.tag_ext_fct,1) ny = adolc.zos_forward(self.tag_ext_fct,2,2,0,yin) adolc.set_nested_ctx(self.tag_ext_fct,0) np.copyto(yout,ny) return 1
def okada_source(m, keep=1): """ Compute the dislocation field due to a (flattened) array of active control parameters by replaying pyadolc's tape. :kwarg keep: toggle whether to flag for a reverse propagation. """ return adolc.zos_forward(tape_tag, m, keep=1)
def set_initial_condition(self, prob, annotate_source=False, unroll_tape=False, **kwargs): """ Set initial condition using the Okada parametrisation [Okada 85]. Uses code from GeoCLAW found in `geoclaw/src/python/geoclaw/dtopotools.py`. [Okada 85] Yoshimitsu Okada, "Surface deformation due to shear and tensile faults in a half-space", Bulletin of the Seismological Society of America, Vol. 75, No. 4, pp.1135--1154, (1985). :arg prob: :class:`AdaptiveTsunamiProblem` solver object. :kwarg annotate_source: toggle annotation of the rupture process using pyadolc. :kwarg tag: non-negative integer label for tape. """ # separate_faults = kwargs.get('separate_faults', True) separate_faults = kwargs.get('separate_faults', False) subtract_from_bathymetry = kwargs.pop('subtract_from_bathymetry', True) tag = kwargs.get('tag', 0) # Create fault topography... if unroll_tape: # ... by unrolling ADOL-C's tape import adolc F = adolc.zos_forward(tag, self.input_vector, keep=1) if separate_faults: F = np.sum(F.reshape(self.num_subfaults, len(self.indices)), axis=0) surf = self.interpolate_dislocation_field(prob, data=F) else: # ... by running the Okada model self.create_topography(annotate=annotate_source, **kwargs) surf = self.interpolate_dislocation_field(prob) # Assume zero initial velocity and interpolate into the elevation space u, eta = prob.fwd_solutions[0].split() u.assign(0.0) eta.interpolate(surf) # Subtract initial surface from the bathymetry field if subtract_from_bathymetry: self.subtract_surface_from_bathymetry(prob, surf=surf) return surf
Np = size(p) Nq = size(q) Nv = size(v) Nm = 100 ts = linspace(0,10,Nm) # TAPING THE INTEGRATOR adolc.trace_on(1) av = adolc.adouble(v) adolc.independent(av) ax = explicit_euler(av[0],f,ts,av[:Np],av[Np:]) adolc.dependent(ax) adolc.trace_off() # COMPUTING FUNCTION AND JACOBIAN FROM THE TAPE y = adolc.zos_forward(1,v,0) J = adolc.jacobian(1,v) x_plot = plot(ts, y,'b') x_analytical_plot = plot(ts,phi(ts,p,q),'b.') xp0_plot = plot(ts, J[:,0], 'g') xp0_analytical_plot = plot(ts, phip0(ts,p,q), 'g.') xp1_plot = plot(ts, J[:,1], 'r') xp1_analytical_plot = plot(ts, phip1(ts,p,q), 'r.') show() print J
# PERFORM PARAMETER ESTIMATION def dFdp(p, q, ts, Sigma, etas): v[:Np] = p[:] return adolc.jacobian(1, v)[:, :Np] res = scipy.optimize.leastsq(F, p, args=(q, ts, Sigma, etas), Dfun=dFdp, full_output=True) # plotting solution of parameter estimation and starting point p[0] += 3 p[1] += 2. x = explicit_euler(p[0], f, ts, p, q) y = adolc.zos_forward(2, v, 0) p[0] -= 3 p[1] -= 2. starting_plot = plot(ts, x) x = explicit_euler(p[0], f, ts, p, q) correct_plot = plot(ts, x, 'b.') meas_plot = plot(ts, etas, 'r.') x = explicit_euler(res[0][0], f, ts, res[0], q) est_plot = plot(ts, x) plot(ts, etas, 'r.') hplot = plot(ts, y, 'g.') xlabel(r'time $t$ []') ylabel(r'measurement function $h(t,x,p,q)$') legend((meas_plot, starting_plot, correct_plot, est_plot, hplot),
Np = size(p) Nq = size(q) Nv = size(v) Nm = 100 ts = linspace(0, 10, Nm) # TAPING THE INTEGRATOR adolc.trace_on(1) av = adolc.adouble(v) adolc.independent(av) ax = explicit_euler(av[0], f, ts, av[:Np], av[Np:]) adolc.dependent(ax) adolc.trace_off() # COMPUTING FUNCTION AND JACOBIAN FROM THE TAPE y = adolc.zos_forward(1, v, 0) J = adolc.jacobian(1, v) x_plot = plot(ts, y, 'b') x_analytical_plot = plot(ts, phi(ts, p, q), 'b.') xp0_plot = plot(ts, J[:, 0], 'g') xp0_analytical_plot = plot(ts, phip0(ts, p, q), 'g.') xp1_plot = plot(ts, J[:, 1], 'r') xp1_analytical_plot = plot(ts, phip1(ts, p, q), 'r.') show() print J
ax = explicit_euler(av[0],f,ts,av[:Np],av[Np:]) ay = measurement_model(ax, av[:Np],av[Np:]) for m in range(Nm): y[m] = adolc.depends_on(ay[m]) adolc.trace_off() # PERFORM PARAMETER ESTIMATION def dFdp(p,q,ts,Sigma, etas): v[:Np] = p[:] return adolc.jacobian(1,v)[:,:Np] res = scipy.optimize.leastsq(F,p,args=(q,ts,Sigma,etas), Dfun = dFdp, full_output = True) # plotting solution of parameter estimation and starting point p[0]+=3; p[1] += 2. x = explicit_euler(p[0],f,ts,p,q) y = adolc.zos_forward(2,v,0) p[0]-= 3; p[1] -= 2. starting_plot = plot(ts,x) x = explicit_euler(p[0],f,ts,p,q) correct_plot = plot(ts,x,'b.') meas_plot = plot(ts,etas,'r.') x = explicit_euler(res[0][0],f,ts,res[0],q) est_plot = plot(ts,x) plot(ts,etas,'r.') hplot = plot(ts,y,'g.') xlabel(r'time $t$ []') ylabel(r'measurement function $h(t,x,p,q)$') legend((meas_plot,starting_plot,correct_plot,est_plot,hplot),('measurements','initial guess','true','estimated','measurement model')) title('Parameter Estimation')