def test01_alt(self):
     """Set directory"""
     if isdir(self.myoutdir):
         os.system('rm -rf {0}'.format(self.myoutdir))
     myplot = PlotFenics()
     myplot.set_outdir(self.myoutdir)
     self.assertTrue(isdir(self.myoutdir))
 def test06(self):
     """Gather plots"""
     myplot = PlotFenics(self.myoutdir)
     myplot.set_varname('testm')
     myplot.indices = self.indexlist
     myplot.gather_vtkplots()
     self.assertTrue(isfile(self.myoutdir + 'testm.pvd'))
 def test05(self):
     """Indices list properly created"""
     myplot = PlotFenics(self.myoutdir)
     myplot.set_varname('testm')
     for ii in self.indexlist:
         self.m.vector()[:] = ii * np.ones(self.lenm, dtype='float')
         myplot.plot_vtk(self.m, ii)
     self.assertTrue(myplot.indices == self.indexlist)
 def test04(self):
     """Plot parameter m"""
     myplot = PlotFenics(self.myoutdir)
     myplot.set_varname('testm')
     self.m.vector()[:] = 20 * np.ones(self.lenm, dtype='float')
     myplot.plot_vtk(self.m, 20)
     self.assertTrue(isfile(self.myoutdir+'testm_20.pvd') and \
     isfile(self.myoutdir+'testm_20000000.vtu'))
 def test09(self):
     """Indices are reset when varname changed"""
     myplot = PlotFenics(self.myoutdir)
     myplot.set_varname('testm')
     indexlist = [1, 3]
     for ii in indexlist:
         self.m.vector()[:] = ii * np.ones(self.lenm, dtype='float')
         myplot.plot_vtk(self.m, ii)
     myplot.set_varname('testm2')
     self.assertTrue((myplot.varname == 'testm2') and \
     (myplot.indices == []))
 def test03(self):
     """Need index list defined to gather"""
     myplot = PlotFenics(self.myoutdir)
     myplot.set_varname('testm')
     self.assertRaises(NoIndicesError, myplot.gather_vtkplots)
 def test02_alt(self):
     """Need varname defined to plot"""
     myplot = PlotFenics(self.myoutdir)
     self.m.vector()[:] = np.ones(self.lenm)
     self.assertRaises(NoVarnameError, myplot.plot_vtk, self.m)
 def test02(self):
     """Instantiate class when dir exists"""
     myplot = PlotFenics(self.myoutdir)
     myplot.set_outdir(self.myoutdir + 'Test1/')
     self.assertTrue(myplot.outdir == self.myoutdir + 'Test1/')
 def test01(self):
     """Instantiate class when dir does not exist"""
     if isdir(self.myoutdir):
         os.system('rm -rf {0}'.format(self.myoutdir))
     myplot = PlotFenics(self.myoutdir)
     self.assertTrue(isdir(self.myoutdir))
 def test00_defdir(self):
     """Check default directory"""
     if isdir(self.defoutdir):
         os.system('rm -rf {0}'.format(self.defoutdir))
     myplot = PlotFenics()
     self.assertTrue(isdir(self.defoutdir))
 def test00_inst(self):
     """Default instantiation"""
     myplot = PlotFenics()
 def test08(self):
     """Fenics_vtu_correction variable"""
     myplot = PlotFenics(self.myoutdir)
     self.assertTrue(isfile(self.myoutdir+'testm_20'+\
     myplot.Fenics_vtu_correction+'.vtu'))
Beispiel #13
0
    def __init__(self,
                 CGdeg,
                 regularizationtype,
                 h=1.0,
                 parameters=[],
                 image='image.dat'):
        class Image(dl.Expression):
            def __init__(self, Lx, Ly, data):
                self.data = data
                self.hx = Lx / float(self.data.shape[1] - 1)
                self.hy = Ly / float(self.data.shape[0] - 1)

            def eval(self, values, x):
                j = math.floor(x[0] / self.hx)
                i = math.floor(x[1] / self.hy)
                values[0] = self.data[i, j]

        data = np.loadtxt(image, delimiter=',')
        #Lx, Ly = float(data.shape[1])/float(data.shape[0]), 1.
        Lx, Ly = 2., 1.
        scaling = 100. * h  # =1.0 => h~0.01
        Lx, Ly = scaling * Lx, scaling * Ly
        np.random.seed(seed=1)
        noise_std_dev = 0.3
        noise = noise_std_dev * np.random.randn(data.shape[0], data.shape[1])
        print '||noise||={}'.format(np.linalg.norm(noise))
        mesh = dl.RectangleMesh(dl.Point(0, 0), dl.Point(Lx, Ly), 200, 100)
        mcoord = mesh.coordinates()
        print 'hx={}, hy={}'.format((mcoord[-1][0] - mcoord[0][0]) / 200.,
                                    (mcoord[-1][1] - mcoord[0][1]) / 100.)
        V = dl.FunctionSpace(mesh, 'Lagrange', CGdeg)
        trueImage = Image(Lx, Ly, data)
        noisyImage = Image(Lx, Ly, data + noise)
        print 'min(data)={}, max(data)={}'.format(np.amin(data), np.amax(data))
        print 'min(data+noise)={}, max(data+noise)={}'.format(
            np.amin(data + noise), np.amax(data + noise))
        self.u_true = dl.interpolate(trueImage, V)
        self.u_0 = dl.interpolate(noisyImage, V)

        self.u = dl.Function(V)
        self.ucopy = dl.Function(V)
        self.G = dl.Function(V)
        self.du = dl.Function(V)
        u_test = dl.TestFunction(V)
        u_trial = dl.TrialFunction(V)

        Mweak = dl.inner(u_test, u_trial) * dl.dx
        self.M = dl.assemble(Mweak)
        self.solverM = dl.LUSolver('petsc')
        self.solverM.parameters['symmetric'] = True
        self.solverM.parameters['reuse_factorization'] = True
        self.solverM.set_operator(self.M)

        self.regul = regularizationtype
        if self.regul == 'tikhonov':
            self.Regul = LaplacianPrior({'Vm': V, 'gamma': 1.0, 'beta': 0.0})
        elif self.regul == 'TV':
            paramTV = {'Vm': V, 'k': 1.0, 'eps': 1e-4, 'GNhessian': True}
            paramTV.update(parameters)
            self.Regul = TV(paramTV)
            self.inexact = False
        elif self.regul == 'TVPD':
            paramTV = {'Vm': V, 'k': 1.0, 'eps': 1e-4, 'exact': False}
            paramTV.update(parameters)
            self.Regul = TVPD(paramTV)
            self.inexact = False
        self.alpha = 1.0

        self.Hess = self.M

        self.parametersLS = {'alpha0':1.0, 'rho':0.5, 'c':5e-5, \
        'max_backtrack':12, 'cgtol':0.5, 'maxiter':50000}

        filename, ext = os.path.splitext(sys.argv[0])
        #if os.path.isdir(filename + '/'):   shutil.rmtree(filename + '/')
        self.myplot = PlotFenics(filename)

        try:
            solver = PETScKrylovSolver('cg', 'ml_amg')
            self.precond = 'ml_amg'
        except:
            print '*** WARNING: ML not installed -- using petsc_amg instead'
            self.precond = 'petsc_amg'