Exemplo n.º 1
0
    def run(self, globdat):

        # ADVANCE
        logging.info("Advancing to the next load step")
        globdat.i += 1
        ndof = globdat.get("ndof")
        globdat.set("fext", np.zeros(ndof))
        globdat.set("loadScale", globdat.i)

        globdat.model.takeAction(Action.ADVANCE, globdat)
        globdat.model.takeAction(Action.GET_MATRIX_0, globdat)
        globdat.model.takeAction(Action.GET_EXT_VECTOR, globdat)
        globdat.model.takeAction(Action.GET_CONSTRAINTS, globdat)

        # INITIAL RESIDUAL
        mbuild = globdat.get("mbuild")
        fext = globdat.get("fext")
        cons = globdat.get("cons")
        disp = globdat.get("solu")
        ndof = globdat.get("ndof")

        old_disp = deepcopy(disp)
        cons.updateSolution(disp)
        Du = globdat.set("Du", disp - old_disp)

        K = mbuild.getDenseMatrix()
        r = fext - np.array(K).dot(Du)

        solver = Solver(self.type, cons)
        solver.solve(K, disp, r, mbuild.hbw)

        return Status.EXIT
Exemplo n.º 2
0
 def calculation(self):
     while True:
         try:
             calculator = Solver(self.type_method, self.xy, self.x)
             calculator.solve()
             del calculator
             break
         except TypeError:
             break
         except ValueError:
             break
Exemplo n.º 3
0
def p_convergence(config: ConfigParser, solver: Solver, sol: GridFunction, var: str) -> None:
    """
    Function to check p (interpolat polynomial order) convergence and print results.

    Args:
        config: Config file from which to grab
        solver: The solver used
        sol: Gridfunction that contains the current solution
        var: The variable of interest.
    """
    num_refinements = config.get_item(['ERROR ANALYSIS', 'num_refinements'], int)
    average_lst = config.get_list(['ERROR ANALYSIS', 'error_average'], str, quiet=True)
    component = solver.model.model_components[var]
    average = component in average_lst

    # Reload the model's mesh and finite element space so convergence tests can be chained and don't affect each other.
    solver.model.load_mesh_fes(mesh=True, fes=True)

    # First solve used the default settings.
    if component is None:
        err = norm('l2_norm', sol, solver.model.ref_sol['ref_sols'][var],
                   solver.model.mesh, solver.model.fes, average)
    else:
        err = norm('l2_norm', sol.components[component], solver.model.ref_sol['ref_sols'][var],
                   solver.model.mesh, solver.model.fes.components[component], average)

    # Track the convergence information.
    num_dofs_lst = [solver.model.fes.ndof]
    interp_ord_lst = [solver.model.interp_ord[var]]
    error_lst = [err]

    # Then run through a series of interpolant refinements.
    for n in range(num_refinements):
        solver.model.interp_ord = {key: val + 1 for key, val in solver.model.interp_ord.items()}
        solver.model.load_mesh_fes(mesh=False, fes=True)
        solver.reset_model()
        sol = solver.solve()
        if component is None:
            err = norm('l2_norm', sol, solver.model.ref_sol['ref_sols'][var],
                       solver.model.mesh, solver.model.fes, average)
        else:
            err = norm('l2_norm', sol.components[component], solver.model.ref_sol['ref_sols'][var],
                       solver.model.mesh, solver.model.fes.components[component], average)

        num_dofs_lst.append(solver.model.fes.ndof)
        interp_ord_lst.append(solver.model.interp_ord[var])
        error_lst.append(err)

        print('L2 norm at refinement {0}: {1}'.format(n, err))

    # Display the results nicely.
    convergence_table = [['Interpolant Order', 'DOFs', 'Error', 'Convergence Rate']]
    convergence_table.append([interp_ord_lst[0], num_dofs_lst[0], error_lst[0], 0])

    for n in range(num_refinements):
        # TODO: Not sure if this is the correct equation for p-convergence.
        convergence_rate = math.log(error_lst[n] / error_lst[n + 1]) / math.log(num_dofs_lst[n + 1] / num_dofs_lst[n])
        convergence_table.append([interp_ord_lst[n + 1], num_dofs_lst[n + 1], error_lst[n + 1], convergence_rate])

    print(tabulate.tabulate(convergence_table, headers='firstrow', floatfmt=['.1f', '.1f', '.3e', '.2f']))
Exemplo n.º 4
0
def solve(solver_name, problem_name, options):
    'Solve the problem by solver with options.'

    # Set cpp_compiler options
    parameters["form_compiler"]["cpp_optimize"] = True

    # Set debug level
    set_log_active(options['debug'])

    # Set refinement level
    options['N'] = mesh_sizes[options['refinement_level']]

    # Create problem and solver
    problem = Problem(problem_name, options)
    solver = Solver(solver_name, options)

    time_step = solver.get_timestep(problem)[0]

    if MPI.process_number() == 0 and options['verbose']:
        print 'Problem: ' + str(problem)
        print 'Solver:  ' + str(solver)

    # Solve problem with solver
    wct = time.time()
    u, p = solver.solve(problem)

    # Compute elapsed time
    wct = time.time() - wct

    # Compute number of degrees of freedom
    num_dofs = u.vector().size() + p.vector().size()

    # Get the mesh size
    mesh_size = u.function_space().mesh().hmin()

    # Get functional value and error
    functional, error = solver.eval()

    # Save results
    cpu_time = solver.cputime()
    save_results(problem, solver, num_dofs, mesh_size, time_step, functional,
                 error)

    return 0
Exemplo n.º 5
0
def solve(solver_name, problem_name, options):
  'Solve the problem by solver with options.'

  # Set cpp_compiler options
  parameters["form_compiler"]["cpp_optimize"] = True

  # Set debug level
  set_log_active(options['debug'])

  # Set refinement level
  options['N'] = mesh_sizes[options['refinement_level']]

  # Create problem and solver
  problem = Problem(problem_name, options)
  solver = Solver(solver_name, options)

  time_step = solver.get_timestep(problem)[0]

  if MPI.process_number() == 0 and options['verbose']:
    print 'Problem: ' + str(problem)
    print 'Solver:  ' + str(solver)

  # Solve problem with solver
  wct = time.time()
  u, p = solver.solve(problem)

  # Compute elapsed time
  wct = time.time() - wct

  # Compute number of degrees of freedom
  num_dofs = u.vector().size() + p.vector().size()

  # Get the mesh size
  mesh_size = u.function_space().mesh().hmin()

  # Get functional value and error
  functional, error = solver.eval()

  # Save results
  cpu_time = solver.cputime()
  save_results(problem, solver, num_dofs, mesh_size, time_step, functional, error)

  return 0
Exemplo n.º 6
0
def h_convergence(config: ConfigParser, solver: Solver, sol: GridFunction, var: str) -> None:
    """
    Function to check h (mesh element size) convergence and print results.

    Args:
        config: Config file from which to grab
        solver: The solver used
        sol: Gridfunction that contains the current solution
        var: The variable of interest.
    """
    num_refinements = config.get_item(['ERROR ANALYSIS', 'num_refinements'], int)
    average_lst = config.get_list(['ERROR ANALYSIS', 'error_average'], str, quiet=True)
    component = solver.model.model_components[var]
    average = component in average_lst

    # Reload the model's mesh and finite element space so convergence tests can be chained and don't affect each other.
    solver.model.load_mesh_fes(mesh=True, fes=True)

    # First solve used the default settings.
    if component is None:
        err = norm('l2_norm', sol, solver.model.ref_sol['ref_sols'][var],
                   solver.model.mesh, solver.model.fes, average)
    else:
        err = norm('l2_norm', sol.components[component], solver.model.ref_sol['ref_sols'][var],
                   solver.model.mesh, solver.model.fes.components[component], average)

    # Track the convergence information.
    num_dofs_lst = [solver.model.fes.ndof]
    error_lst = [err]

    # Then run through a series of mesh refinements and resolve on each
    # refined mesh.
    for n in range(num_refinements):
        solver.model.mesh.Refine()
        solver.model.fes.Update()
        solver.reset_model()
        sol = solver.solve()

        if component is None:
            err = norm('l2_norm', sol, solver.model.ref_sol['ref_sols'][var],
                       solver.model.mesh, solver.model.fes, average)
        else:
            err = norm('l2_norm', sol.components[component], solver.model.ref_sol['ref_sols'][var],
                       solver.model.mesh, solver.model.fes.components[component], average)

        num_dofs_lst.append(solver.model.fes.ndof)
        error_lst.append(err)

        print('L2 norm at refinement {0}: {1}'.format(n, err))

    # Display the results nicely.
    convergence_table = [['Refinement Level', 'DOFs', 'Error', 'Convergence Rate']]
    convergence_table.append([1, num_dofs_lst[0], error_lst[0], 0])

    for n in range(num_refinements):
        ref_level = '1/{}'.format(int(2 ** (n + 1)))
        convergence_rate = math.log(error_lst[n] / error_lst[n + 1]) / \
                           (math.log(num_dofs_lst[n + 1] / (num_dofs_lst[n] * 2.0 ** (solver.model.mesh.dim - 1))))
        convergence_table.append([ref_level, num_dofs_lst[n + 1], error_lst[n + 1], convergence_rate])

    print(tabulate.tabulate(convergence_table, headers='firstrow', floatfmt=('.1f', '.1f', '.3e', '.2f')))
Exemplo n.º 7
0
    def run(self, globdat):

        # ADVANCE
        logging.info("Advancing to the next load step")
        globdat.i += 1
        ndof = globdat.get("ndof")
        globdat.set("fext", np.zeros(ndof))
        globdat.set("loadScale", globdat.i)
        globdat.model.takeAction(Action.ADVANCE, globdat)
        globdat.model.takeAction(Action.GET_MATRIX_0, globdat)
        globdat.model.takeAction(Action.GET_EXT_VECTOR, globdat)
        globdat.model.takeAction(Action.GET_CONSTRAINTS, globdat)

        mbuild = globdat.get("mbuild")
        fext = globdat.get("fext")
        fint = globdat.get("fint")
        cons = globdat.get("cons")
        disp = globdat.get("solu")

        old_disp = deepcopy(disp)
        cons.updateSolution(disp)

        du = disp - old_disp
        K = mbuild.getDenseMatrix()
        r = fext - fint - np.array(K).dot(du)

        solver = Solver(self.type, cons)
        fdof = cons.getFdof()

        for iter in range(self.niter):

            # Update displacement vector
            solver.solve(K, du, r, mbuild.hbw)
            disp[fdof] += du[fdof]

            # Find interal force vector
            globdat.set("fint", np.zeros(ndof))

            if self.nrkey == "full":
                globdat.model.takeAction(Action.GET_MATRIX_0, globdat)
            elif self.nrkey == "mod" or self.nrkey == "LE":
                globdat.model.takeAction(Action.GET_INT_VECTOR, globdat)
            else:
                raise ValueError("{} not implemented !".format(self.nrkey))

            # Find out-of-balance force vector
            r = fext - globdat.get("fint")
            nrm = norm(r[fdof])
            logging.info("    Iteration {}: norm = {:.10f} ".format(iter, nrm))

            # Check convergence in first iteration
            if iter == 0 and nrm <= self.tiny:
                logging.info("    Converged in {} iterations".format(iter + 1))
                return Status.OK
            elif iter == 0 and nrm > self.tiny:
                nrm1 = deepcopy(nrm)

            # Check convergence in later iterations
            if nrm < self.tol * nrm1:
                logging.info("    Converged in {} iterations".format(iter + 1))
                return Status.OK
Exemplo n.º 8
0
    def run(self, globdat):

        # ADVANCE
        logging.info("Advancing to the next load step")
        globdat.i += 1
        ndof = globdat.get("ndof")
        globdat.set("du", np.zeros(ndof))
        globdat.set("Du", np.zeros(ndof))
        globdat.set("fext", np.zeros(ndof))
        globdat.set("fint", np.zeros(ndof))
        globdat.set("loadScale", globdat.i)

        globdat.model.takeAction(Action.ADVANCE, globdat)
        globdat.model.takeAction(Action.GET_MATRIX_0, globdat)
        globdat.model.takeAction(Action.GET_EXT_VECTOR, globdat)
        globdat.model.takeAction(Action.GET_CONSTRAINTS, globdat)

        mbuild = globdat.get("mbuild")
        fext = globdat.get("fext")
        fint = globdat.get("fint")
        cons = globdat.get("cons")
        disp = globdat.get("solu")

        old_disp = deepcopy(disp)
        cons.updateSolution(disp)
        du = globdat.set("du", disp - old_disp)
        Du = globdat.set("Du", deepcopy(du))

        K = mbuild.getDenseMatrix()
        r = fext - fint - np.array(K).dot(Du)

        solver = Solver(self.type, cons)
        fdof = cons.getFdof()
        nrm1 = 0.0

        for iter in range(self.niter):

            # Update displacement vector
            solver.solve(K, du, r, mbuild.hbw)
            disp[fdof] += du[fdof]
            Du[fdof] += du[fdof]

            # Find interal force vector
            globdat.set("fint", np.zeros(ndof))
            globdat.model.takeAction(self.action, globdat)

            # Find out-of-balance force vector
            r = fext - globdat.get("fint")
            nrm = norm(r[fdof])
            logging.info("    Iteration {}: norm = {:.10f} ".format(iter, nrm))

            # Check convergence
            if (iter == 0 and nrm <= self.tiny) or (iter > 0
                                                    and nrm < self.tol * nrm1):
                logging.info("    Converged in {} iterations".format(iter + 1))
                globdat.model.takeAction(Action.COMMIT, globdat)
                return Status.OK
            elif iter == 0 and nrm > self.tiny:
                nrm1 = deepcopy(nrm)

        return Status.EXIT