def test_StatisticsProbe_vector_3D(): mesh = UnitCubeMesh(mpi_comm_self(), 4, 4, 4) V = VectorFunctionSpace(mesh, 'CG', 1) u0 = interpolate(Expression(('x[0]', 'x[1]', 'x[2]')), V) x = array([0.5, 0.25, 0.25]) p = StatisticsProbe(x, V) for i in range(5): p(u0) assert p.number_of_evaluations() == 5 assert p.value_size() == 9 mean = p.mean() var = p.variance() nose.tools.assert_almost_equal(p[0][0], 2.5) nose.tools.assert_almost_equal(p[0][4], 0.3125) nose.tools.assert_almost_equal(p[1][0], 0.5) nose.tools.assert_almost_equal(p[1][1], 0.25) nose.tools.assert_almost_equal(p[1][2], 0.25) nose.tools.assert_almost_equal(mean[0], 0.5) nose.tools.assert_almost_equal(mean[1], 0.25) nose.tools.assert_almost_equal(mean[2], 0.25) nose.tools.assert_almost_equal(var[0], 0.25) nose.tools.assert_almost_equal(var[1], 0.0625) nose.tools.assert_almost_equal(var[2], 0.0625) nose.tools.assert_almost_equal(var[3], 0.125) nose.tools.assert_almost_equal(var[4], 0.125)
def test_StatisticsProbe_segregated_2D(): mesh = UnitSquareMesh(mpi_comm_self(), 4, 4) V = FunctionSpace(mesh, 'CG', 1) u0 = interpolate(Expression('x[0]'), V) v0 = interpolate(Expression('x[1]'), V) x = array([0.5, 0.25]) p = StatisticsProbe(x, V, True) for i in range(5): p(u0, v0) assert p.number_of_evaluations() == 5 assert p.value_size() == 5 mean = p.mean() var = p.variance() nose.tools.assert_almost_equal(p[0][0], 2.5) nose.tools.assert_almost_equal(p[0][4], 0.625) nose.tools.assert_almost_equal(p[1][0], 0.5) nose.tools.assert_almost_equal(p[1][1], 0.25) nose.tools.assert_almost_equal(mean[0], 0.5) nose.tools.assert_almost_equal(mean[1], 0.25) nose.tools.assert_almost_equal(var[0], 0.25) nose.tools.assert_almost_equal(var[1], 0.0625) nose.tools.assert_almost_equal(var[2], 0.125)
def nonzero_values(function): if has_pybind11(): serialized_vector = Vector(MPI.COMM_SELF) else: serialized_vector = Vector(mpi_comm_self()) function.vector().gather(serialized_vector, array(range(function.function_space().dim()), "intc")) indices = nonzero(serialized_vector.get_local()) return sort(serialized_vector.get_local()[indices])
def create_communicators(): """ Create communicators for simple coarse-grained parallelism where all PDEs are solved on a single core, and partition occured at the level of the source terms and in the time-summations for the grad and the Hessian-vector product """ mpicomm_local = dl.mpi_comm_self() mpicomm_global = dl.mpi_comm_world() return mpicomm_local, mpicomm_global
def compute_searchdirection(objfctal, parameters_in=[],\ comm=dl.mpi_comm_self(), BFGSop=[]): """ Compute search direction for Line Search Arguments: objfctal = objective functional (compute cost, grad, Hessian-vect) parameters_in = options for the Newton solver comm = MPI communicator to average z in CGSolverSteihaug """ parameters = {} parameters['method'] = 'Newton' parameters['tolcg'] = 1e-8 parameters['tolGxD'] = -1e-24 parameters['max_iter'] = 1000 parameters.update(parameters_in) method = parameters['method'] tolcg = parameters['tolcg'] tolGxD = parameters['tolGxD'] maxiter = parameters['max_iter'] if method == 'steepest': objfctal.srchdir.vector().zero() objfctal.srchdir.vector().axpy(-1.0, objfctal.MGv) return 0, 0.0, 0 elif method == 'Newton': solver = CGSolverSteihaug() solver.set_operator(objfctal) solver.set_preconditioner(objfctal.getprecond()) solver.parameters["rel_tolerance"] = tolcg solver.parameters["zero_initial_guess"] = True solver.parameters['max_iter'] = maxiter solver.parameters["print_level"] = -2 solver.solve(objfctal.srchdir.vector(), -1.0 * objfctal.MGv, comm) # all cpu time spent here siter = solver.iter fnorm = solver.final_norm rid = solver.reasonid elif method == 'BFGS': BFGSop.solve(objfctal.srchdir.vector(), -1.0 * objfctal.MGv) siter = -1 fnorm = -1 rid = 0 else: raise ValueError("Wrong keyword") # check it is a descent direction GradxDir = objfctal.MGv.inner(objfctal.srchdir.vector()) assert GradxDir < tolGxD, \ "Search direction not a descent direction: {}".format(GradxDir) return siter, fnorm, rid
prior.sample(noise, mtrue) return mtrue # define the PDE def pde_varf(u, m, p): return dl.exp(m) * dl.inner(dl.nabla_grad(u), dl.nabla_grad( p)) * dl.dx - dl.Constant(0.0) * p * dl.dx dl.set_log_active(False) sep = "\n" + "#" * 80 + "\n" ndim = 2 nx = 32 ny = 32 mesh = dl.UnitSquareMesh(dl.mpi_comm_self(), nx, ny) Vh2 = dl.FunctionSpace(mesh, 'Lagrange', 1) Vh1 = dl.FunctionSpace(mesh, 'Lagrange', 1) Vh = [Vh2, Vh1, Vh2] ndofs = [Vh[STATE].dim(), Vh[PARAMETER].dim(), Vh[ADJOINT].dim()] if rank == 0: print(sep, "Set up the mesh and finite element spaces", sep) print( "Number of dofs: STATE={0}, PARAMETER={1}, ADJOINT={2}".format(*ndofs)) u_bdr = dl.Expression("x[1]", degree=1) u_bdr0 = dl.Constant(0.0) bc = dl.DirichletBC(Vh[STATE], u_bdr, u_boundary) bc0 = dl.DirichletBC(Vh[STATE], u_bdr0, u_boundary)
) # or x[0] > 1.0 - dl.DOLFIN_EPS def pde_varf(u, m, p): return dl.inner(dl.nabla_grad(u), dl.nabla_grad(p)) * dl.dx + u * p * dl.dx - m * p * dl.dx # return u * p * dl.dx - m * p * dl.dx # create mesh dl.set_log_active(False) sep = "\n" + "#" * 80 + "\n" nx = 256 mesh = dl.IntervalMesh(dl.mpi_comm_self(), nx, 0., 1.) # define function space Vh2 = dl.FunctionSpace(mesh, 'Lagrange', 1) Vh1 = dl.FunctionSpace(mesh, 'Lagrange', 1) Vh = [Vh2, Vh1, Vh2] ndofs = [Vh[STATE].dim(), Vh[PARAMETER].dim(), Vh[ADJOINT].dim()] if rank == 0: print(sep, "Set up the mesh and finite element spaces", sep) print( "Number of dofs: STATE={0}, PARAMETER={1}, ADJOINT={2}".format(*ndofs)) # define boundary conditions and PDE u_bdr = dl.Expression("1-x[0]", degree=1) u_bdr0 = dl.Constant(0.0)
def u_boundary(x, on_boundary): return on_boundary and (x[1] < dl.DOLFIN_EPS or x[1] > 1.0 - dl.DOLFIN_EPS) def v_boundary(x, on_boundary): return on_boundary and (x[0] < dl.DOLFIN_EPS or x[0] > 1.0 - dl.DOLFIN_EPS) if __name__ == "__main__": dl.set_log_active(False) assert dlversion() >= (2016, 2, 0) world_comm = dl.mpi_comm_world() self_comm = dl.mpi_comm_self() ndim = 2 nx = 16 ny = 16 mesh = dl.UnitSquareMesh(self_comm, nx, ny) rank = dl.MPI.rank(world_comm) nproc = dl.MPI.size(world_comm) Vh2 = dl.FunctionSpace(mesh, 'Lagrange', 2) Vh1 = dl.FunctionSpace(mesh, 'Lagrange', 1) Vh = [Vh2, Vh1, Vh2] ndofs = [Vh[STATE].dim(), Vh[PARAMETER].dim(), Vh[ADJOINT].dim()] if rank == 0:
""" Code should be run in serial and in parallel to check that both produces the same set of eigenvalues """ import dolfin as dl from fenicstools.linalg.miscroutines import compute_eigfenics mesh = dl.UnitSquareMesh(5, 5) V = dl.FunctionSpace(mesh, 'CG', 1) test = dl.TestFunction(V) trial = dl.TrialFunction(V) M = dl.assemble(dl.inner(dl.nabla_grad(test), dl.nabla_grad(trial)) * dl.dx) if mesh.mpi_comm() == dl.mpi_comm_self(): compute_eigfenics(M, 'eigM-serial.out') else: compute_eigfenics(M, 'eigM-parallel.out')