def solver_nonlinear(G, d_, w_, wd_, bcs, T, dt, action=None, **namespace): dis_x = []; dis_y = []; time = [] solver_parameters = {"newton_solver": \ {"relative_tolerance": 1E-8, "absolute_tolerance": 1E-8, "maximum_iterations": 100, #"krylov_solver": {"monitor_convergence": True}, #"linear_solver": {"monitor_convergence": True}, "relaxation_parameter": 0.9}} t = 0 while t < (T - dt*DOLFIN_EPS): solve(G == 0, wd_["n"], bcs, solver_parameters=solver_parameters) # Update solution times = ["n-3", "n-2", "n-1", "n"] for i, t_tmp in enumerate(times[:-1]): wd_[t_tmp].vector().zero() wd_[t_tmp].vector().axpy(1, wd_[times[i+1]].vector()) w_[t_tmp], d_[t_tmp] = wd_[t_tmp].split(True) # Get displacement if callable(action): action(wd_, t) t += dt if MPI.rank(mpi_comm_world()) == 0: print "Time: ",t #,"dis_x: ", d(coord)[0], "dis_y: ", d(coord)[1] return dis_x, dis_y, time
def Newton_manual(F, udp, bcs, atol, rtol, max_it, lmbda, udp_res, VVQ): #Reset counters Iter = 0 residual = 1 rel_res = residual dw = TrialFunction(VVQ) Jac = derivative(F, udp, dw) # Jacobi while rel_res > rtol and residual > atol and Iter < max_it: A = assemble(Jac) A.ident_zeros() b = assemble(-F) [bc.apply(A, b, udp.vector()) for bc in bcs] #solve(A, udp_res.vector(), b, "superlu_dist") solve(A, udp_res.vector(), b) #, "mumps") udp.vector()[:] = udp.vector()[:] + lmbda * udp_res.vector()[:] #udp.vector().axpy(1., udp_res.vector()) [bc.apply(udp.vector()) for bc in bcs] rel_res = norm(udp_res, 'l2') residual = b.norm('l2') if MPI.rank(mpi_comm_world()) == 0: print "Newton iteration %d: r (atol) = %.3e (tol = %.3e), r (rel) = %.3e (tol = %.3e) " \ % (Iter, residual, atol, rel_res, rtol) Iter += 1 return udp
def solver_nonlinear(G, d_, w_, wd_, bcs, T, dt, action=None, **namespace): dis_x = []; dis_y = []; time = [] solver_parameters = {"newton_solver": \ {"relative_tolerance": 1E-8, "absolute_tolerance": 1E-8, "maximum_iterations": 100, "relaxation_parameter": 1.0}} t = 0 while t <= T: solve(G == 0, wd_["n"], bcs, solver_parameters=solver_parameters) # Update solution times = ["n-3", "n-2", "n-1", "n"] for i, t_tmp in enumerate(times[:-1]): wd_[t_tmp].vector().zero() wd_[t_tmp].vector().axpy(1, wd_[times[i+1]].vector()) w_[t_tmp], d_[t_tmp] = wd_[t_tmp].split(True) # Get displacement if callable(action): action(wd_, t) t += dt if MPI.rank(mpi_comm_world()) == 0: print "Time: ", t
def Newton_manual(F, vd, bcs, J, atol, rtol, max_it, lmbda\ , vd_res): #Reset counters Iter = 0 residual = 1 rel_res = residual while rel_res > rtol and residual > atol and Iter < max_it: A = assemble(J, keep_diagonal = True) A.ident_zeros() b = assemble(-F) [bc.apply(A, b, vd.vector()) for bc in bcs] #solve(A, vd_res.vector(), b, "superlu_dist") #solve(A, vd_res.vector(), b, "mumps") solve(A, vd_res.vector(), b) vd.vector().axpy(1., vd_res.vector()) [bc.apply(vd.vector()) for bc in bcs] rel_res = norm(vd_res, 'l2') residual = b.norm('l2') if MPI.rank(mpi_comm_world()) == 0: print "Newton iteration %d: r (atol) = %.3e (tol = %.3e), r (rel) = %.3e (tol = %.3e) " \ % (Iter, residual, atol, rel_res, rtol) Iter += 1 #Reset residual = 1 rel_res = residual Iter = 0 return vd
def print_text(text, color='white', atrb=0, cls=None): """ Print text <text> from calling class <cl> to the screen. """ if cls is not None: color = cls.color() if MPI.rank(mpi_comm_world())==0: if atrb != 0: text = ('%s%s' + text + '%s') % (fg(color), attr(atrb), attr(0)) else: text = ('%s' + text + '%s') % (fg(color), attr(0)) print text
def get_text(text, color='white', atrb=0, cls=None): """ Returns text <text> from calling class <cl> for later printing. """ if cls is not None: color = cls.color() if MPI.rank(mpi_comm_world())==0: if atrb != 0: text = ('%s%s' + text + '%s') % (fg(color), attr(atrb), attr(0)) else: text = ('%s' + text + '%s') % (fg(color), attr(0)) return text
def viz(results, runs): if MPI.rank(mpi_comm_world()) != 0: pass # TODO: Get minimum time of all to plot over consistent plt.figure() for i, r in enumerate(results): displacement_x = np.array(r[0]) displacement_y = np.array(r[1]) time = np.array(r[2]) simulation_parameters = runs[i] E = simulation_parameters["E"] dt = simulation_parameters["dt"] name = simulation_parameters["name"] case_path = simulation_parameters["case_path"] plt.plot(time, displacement_y, label=name) plt.ylabel("Displacement y") plt.xlabel("Time") plt.legend(loc=3) plt.hold("on") plt.title("implementation: %s, dt = %g, y_displacement" % (name, dt)) # TODO: Move this to solver and use fenicsprobe if MPI.rank(mpi_comm_world()) == 0: if not path.exists(case_path): makedirs(case_path) print case_path displacement_x.dump(path.join(case_path, "dis_x.np")) displacement_t.dump(path.join(case_path, "dis_y.np")) time.dump(path.join(case_path, "time.np")) # Store simulation parameters f = open(path.join(case_path, "param.dat", 'w')) cPickle.dump(simulation_parameters, f) f.close() # FIXME: store in a consistent manner plt.savefig("test.png")
def solver_linear(G, d_, w_, wd_, bcs, T, dt, action=None, **namespace): a = lhs(G); L = rhs(G) A = assemble(a) b = assemble(L) t = 0 #d_prec = PETScPreconditioner("default") #d_solver = PETScKrylovSolver("gmres", d_prec) #d_solver.parameters["monitor_convergence"] = True #from IPython import embed; embed() #d_solver.prec = d_prec # Solver loop while t < (T - dt*DOLFIN_EPS): t += dt # Assemble assemble(a, tensor=A) assemble(L, tensor=b) # Apply BC for bc in bcs: bc.apply(A, b) # Solve #d_solver.solve(A, wd_["n"].vector(), b) solve(A, wd_["n"].vector(), b) # Update solution times = ["n-3", "n-2", "n-1", "n"] for i, t_tmp in enumerate(times[:-1]): wd_[t_tmp].vector().zero() wd_[t_tmp].vector().axpy(1, wd_[times[i+1]].vector()) # Get displacement if callable(action): action(wd_, t) if MPI.rank(mpi_comm_world()) == 0: print "Time: ",t
t0 = timer.time() #try: if r["space"] == "mixedspace": problem_mix(**vars()) elif r["space"] == "singlespace": problem_single() else: print("Problem type %s is not implemented, only mixedspace " + "and singlespace are valid options") % r["space"] sys.exit(0) #except Exception as e: # print "Problems for solver", r["name"] # print "Unexpected error:", sys.exc_info()[0] # print e # print "Move on the the next solver" r["number_of_cores"] = MPI.max(mpi_comm_world(), MPI.rank( mpi_comm_world())) + 1 r["solution_time"] = MPI.sum(mpi_comm_world(), timer.time() - t0) / (r["number_of_cores"]) displacement = probe.array() probe.clear() # Store results if MPI.rank(mpi_comm_world()) == 0 and not load: results.append( (displacement[0, :], displacement[1, :], np.array(time))) if not path.exists(r["case_path"]): makedirs(r["case_path"]) results[-1][0].dump(path.join(r["case_path"], "dis_x.np")) results[-1][1].dump(path.join(r["case_path"], "dis_y.np")) results[-1][2].dump(path.join(r["case_path"], "time.np"))
a, L = lhs(F), rhs(F) # Time-stepping u_np1 = Function(V) u_np1.rename("Temperature", "") t = 0 # reference solution at t=0 u_ref = interpolate(u_D, V) u_ref.rename("reference", " ") # mark mesh w.r.t ranks mesh_rank = MeshFunction("size_t", mesh, mesh.topology().dim()) if problem is ProblemType.NEUMANN: mesh_rank.set_all(MPI.rank(MPI.comm_world) + 4) else: mesh_rank.set_all(MPI.rank(MPI.comm_world) + 0) mesh_rank.rename("myRank", "") # Generating output files temperature_out = File("out/%s.pvd" % precice.get_participant_name()) ref_out = File("out/ref%s.pvd" % precice.get_participant_name()) error_out = File("out/error%s.pvd" % precice.get_participant_name()) ranks = File("out/ranks%s.pvd" % precice.get_participant_name()) # output solution and reference solution at t=0, n=0 n = 0 print('output u^%d and u_ref^%d' % (n, n)) temperature_out << u_n ref_out << u_ref