def test_Write(f): """Construct and save a simple meshfunction.""" f = f f[0] = 1 f[1] = 2 file = XDMFFile(f.mesh().mpi_comm(), "saved_mesh_function.xdmf") file.write(f)
def compute_rates(): "Compute convergence rates for degrees 1 and 2." gdim = 2 tdim = gdim for coordinate_degree in (1, 2): for element_degree in (1, 2): print("\nUsing coordinate degree %d, element degree %d" % (coordinate_degree, element_degree)) ufile = XDMFFile(MPI.comm_world, "poisson-disc-degree-x%d-e%d.xdmf" % (coordinate_degree, element_degree)) encoding = XDMFFile.Encoding.HDF5 if has_hdf5() else XDMFFile.Encoding.ASCII preverr = None prevh = None for i, nsteps in enumerate((1, 8, 64)): err, h, area, num_cells, u = compute(nsteps, coordinate_degree, element_degree, gdim) if preverr is None: conv = 0.0 print("conv = N/A, h = %.3e, err = %.3e, area = %.16f, num_cells = %d" % (h, err, area, num_cells)) else: conv = math.log(preverr/err, prevh/h) print("conv = %1.2f, h = %.3e, err = %.3e, area = %.16f, num_cells = %d" % (conv, h, err, area, num_cells)) preverr = err prevh = h # Save solution to file u.rename('u') if MPI.size(MPI.comm_world) > 1 and encoding == XDMFFile.Encoding.ASCII: print("XDMF file output not supported in parallel without HDF5") else: ufile.write(u, encoding=encoding)
# Set some parameters solver.parameters["method"] = "tron" solver.parameters["monitor_convergence"] = True solver.parameters["report"] = True # Uncomment this line to see the available parameters # info(parameters, True) # Parse (PETSc) parameters parameters.parse() # Solve the problem solver.solve(BucklingProblem(), u.vector(), u_min.vector(), u_max.vector()) # Save solution in XDMF format if available out = XDMFFile(mesh.mpi_comm(), "u.xdmf") if has_hdf5(): out.write(u) elif MPI.size(mesh.mpi_comm()) == 1: encoding = XDMFFile.Encoding.ASCII out.write(u, encoding) else: # Save solution in vtk format out = File("u.pvd") out << u # Plot the current configuration plot(u, mode="displacement", wireframe=True, title="Displacement field") plt.show()
# Step in time t = 0.0 # Check if we are running on CI server and reduce run time if "CI" in os.environ.keys(): T = 3 * dt else: T = 50 * dt u.vector.copy(result=u0.vector) u0.vector.ghostUpdate(addv=PETSc.InsertMode.INSERT, mode=PETSc.ScatterMode.FORWARD) while (t < T): t += dt r = solver.solve(problem, u.vector) print("Step, num iterations:", int(t / dt), r[0]) u.vector.copy(result=u0.vector) file.write(u.sub(0), t) # The string ``"compressed"`` indicates that the output data should be # compressed to reduce the file size. Within the time stepping loop, the # solution vector associated with ``u`` is copied to ``u0`` at the # beginning of each time step, and the nonlinear problem is solved by # calling # :py:func:`solver.solve(problem,u.vector)<dolfin.cpp.NewtonSolver.solve>`, # with the new solution vector returned in # :py:func:`u.vector<dolfin.cpp.Function.vector>`. The ``c`` component # of the solution (the first component of ``u``) is then written to file # at every time step.
# Create CG Krylov solver and turn convergence monitoring on solver = PETSc.KSP().create(MPI.comm_world) solver.setFromOptions() # Set matrix operator solver.setOperators(A) # Compute solution solver.setMonitor(lambda ksp, its, rnorm: print( "Iteration: {}, rel. residual: {}".format(its, rnorm))) solver.solve(b, u.vector) solver.view() # Save solution to XDMF format file = XDMFFile(MPI.comm_world, "elasticity.xdmf") file.write(u) unorm = u.vector.norm() if MPI.rank(mesh.mpi_comm()) == 0: print("Solution vector norm:", unorm) # Save colored mesh partitions in VTK format if running in parallel # if MPI.size(mesh.mpi_comm()) > 1: # File("partitions.pvd") << MeshFunction("size_t", mesh, mesh.topology.dim, \ # MPI.rank(mesh.mpi_comm())) # Project and write stress field to post-processing file # W = TensorFunctionSpace(mesh, "Discontinuous Lagrange", 0) # stress = project(sigma(u), V=W) # File("stress.pvd") << stress