def test_solve_proj_sadpnt_smw(self): """check the sadpnt as projection""" n = self.n # check whether it is a projection AuvInvZ = lau.solve_sadpnt_smw(amat=self.A, jmat=self.J, rhsv=self.Z, jmatT=self.Jt, umat=self.U, vmat=self.V) auvAUVinv = self.A * AuvInvZ[:n, :] - \ lau.comp_uvz_spdns(self.U, self.V, AuvInvZ[:n, :]) AuvInv2Z = lau.solve_sadpnt_smw(amat=self.A, jmat=self.J, rhsv=auvAUVinv, jmatT=self.Jt, umat=self.U, vmat=self.V) self.assertTrue(np.allclose(AuvInvZ[:n, :], AuvInv2Z[:n, :]), msg='likely to fail because of ill cond') prjz = lau.app_prj_via_sadpnt(amat=self.A, jmat=self.J, rhsv=self.Z, jmatT=self.Jt) prprjz = lau.app_prj_via_sadpnt(amat=self.A, jmat=self.J, rhsv=self.Z, jmatT=self.Jt) # check projector self.assertTrue(np.allclose(prprjz, prjz)) # onto kernel J self.assertTrue(np.linalg.norm(prjz) > 1e-8) self.assertTrue( np.linalg.norm(self.J * prjz) / np.linalg.norm(prjz) < 1e-6) # check transpose idmat = np.eye(n) prj = lau.app_prj_via_sadpnt(amat=self.A, jmat=self.J, rhsv=idmat, jmatT=self.Jt) prjT = lau.app_prj_via_sadpnt(amat=self.A, jmat=self.J, rhsv=idmat, jmatT=self.Jt, transposedprj=True) self.assertTrue(np.allclose(prj, prjT.T))
def test_solve_proj_sadpnt_smw(self): """check the sadpnt as projection""" n = self.n # check whether it is a projection AuvInvZ = lau.solve_sadpnt_smw(amat=self.A, jmat=self.J, rhsv=self.Z, jmatT=self.Jt, umat=self.U, vmat=self.V) auvAUVinv = self.A * AuvInvZ[:n, :] - \ lau.comp_uvz_spdns(self.U, self.V, AuvInvZ[:n, :]) AuvInv2Z = lau.solve_sadpnt_smw(amat=self.A, jmat=self.J, rhsv=auvAUVinv, jmatT=self.Jt, umat=self.U, vmat=self.V) self.assertTrue(np.allclose(AuvInvZ[:n, :], AuvInv2Z[:n, :]), msg='likely to fail because of ill cond') prjz = lau.app_prj_via_sadpnt(amat=self.A, jmat=self.J, rhsv=self.Z, jmatT=self.Jt) prprjz = lau.app_prj_via_sadpnt(amat=self.A, jmat=self.J, rhsv=self.Z, jmatT=self.Jt) # check projector self.assertTrue(np.allclose(prprjz, prjz)) # onto kernel J self.assertTrue(np.linalg.norm(prjz) > 1e-8) self.assertTrue(np.linalg.norm(self.J*prjz)/np.linalg.norm(prjz) < 1e-6) # check transpose idmat = np.eye(n) prj = lau.app_prj_via_sadpnt(amat=self.A, jmat=self.J, rhsv=idmat, jmatT=self.Jt) prjT = lau.app_prj_via_sadpnt(amat=self.A, jmat=self.J, rhsv=idmat, jmatT=self.Jt, transposedprj=True) self.assertTrue(np.allclose(prj, prjT.T))
def test_output_opa(self): """ test the regularization of the output operator """ import dolfin import dolfin_to_nparrays as dtn import cont_obs_utils as cou from optcont_main import drivcav_fems dolfin.parameters.linear_algebra_backend = "uBLAS" N = 20 mesh = dolfin.UnitSquareMesh(N, N) V = dolfin.VectorFunctionSpace(mesh, "CG", 2) NY = 8 odcoo = dict(xmin=0.45, xmax=0.55, ymin=0.6, ymax=0.8) # get the system matrices femp = drivcav_fems(N) stokesmats = dtn.get_stokessysmats(femp['V'], femp['Q'], nu=1) # remove the freedom in the pressure stokesmats['J'] = stokesmats['J'][:-1, :][:, :] stokesmats['JT'] = stokesmats['JT'][:, :-1][:, :] # reduce the matrices by resolving the BCs # (stokesmatsc, # rhsd_stbc, # invinds, # bcinds, # bcvals) = dtn.condense_sysmatsbybcs(stokesmats, femp['diribcs']) # check the C MyC, My = cou.get_mout_opa(odcoo=odcoo, V=V, NY=NY) # exv = dolfin.Expression(('x[1]', 'x[1]')) import sympy as smp x, y = smp.symbols('x[0], x[1]') u_x = x * x * (1 - x) * (1 - x) * 2 * y * (1 - y) * (2 * y - 1) u_y = y * y * (1 - y) * (1 - y) * 2 * x * (1 - x) * (1 - 2 * x) from sympy.printing import ccode exv = dolfin.Expression((ccode(u_x), ccode(u_y))) testv = dolfin.interpolate(exv, V) # plot(testv) # the right inverse to C Cplus = cou.get_rightinv(MyC) self.assertTrue(np.allclose(np.eye(MyC.shape[0]), MyC * Cplus)) # MyCc = MyC[:, invinds] ptmct = lau.app_prj_via_sadpnt(amat=stokesmats['M'], jmat=stokesmats['J'], rhsv=MyC.T, transposedprj=True) testvi = testv.vector().array() testvi0 = np.atleast_2d(testv.vector().array()).T testvi0 = lau.app_prj_via_sadpnt(amat=stokesmats['M'], jmat=stokesmats['J'], rhsv=testvi0) # check if divfree part is not zero self.assertTrue(np.linalg.norm(testvi0) > 1e-8, msg='maybe nothing wrong, but this shouldnt be zero') testyv0 = np.atleast_2d(MyC * testvi0).T testry = np.dot(ptmct.T, testvi) self.assertTrue(np.allclose(testyv0, testry))