def get_func_deriv(k): """ Get the energy value and its derivative for given k value """ f = np.pi spr = Spring(k) u = spr.solve(0, f) E = spr.F(u) dRdk = spr.dRdk(u) dFdk = spr.dFdk(u) dRdq = spr.dRdq(u) dFdq = spr.dFdq(u) lam = spr.adjoint_solve(u) DFDx = dFdk + lam * dRdk return E, DFDx
for q in pc.quadrature_map.keys(): # Quadrature node and weight yq = pc.Y(q, 'name') wq = pc.W(q) # Set the paramter values into the element dspr.setStiffness(yq['K']) # Create space for fetching deterministic # jacobian, and state vectors that go as input uq = np.zeros((1)) # dspr.nddof for k in range(sspr.nterms): psiky = pc.evalOrthoNormalBasis(k, q) uq[:] += U[k * 1:(k + 1) * 1] * psiky fq = dspr.F(uq) dfdxq = dspr.dFdk(uq) dRdxq = dspr.dRdk(uq) # Form u for this quadrature point 'y' # Project the determinic element jacobian onto the # stochastic basis and place in the global matrix psiziw = wq * pc.evalOrthoNormalBasis(i, q) # F(q,q(y))*psi(y)*rho(y) E[i] += fq * psiziw dFdX[i] += dfdxq * psiziw dRdX[i] += dRdxq * psiziw fmean = E[0] fvar = np.sum(E[1:]**2)