def IsConverged(self, residual, current_data):
        abs_norm = la.norm(residual) / np.sqrt(residual.size)

        if self.initial_iteration:
            self.initial_iteration = False
            self.initial_norm = abs_norm

        rel_norm = abs_norm / self.initial_norm

        is_converged = abs_norm < self.abs_tolerance or rel_norm < self.rel_tolerance

        info_msg = ""

        if self.echo_level > 1:
            info_msg = 'Convergence '

            if self.label != "":
                info_msg += 'for "{}": '.format(self.label)

            if is_converged:
                info_msg += colors.green("ACHIEVED")
            else:
                info_msg += colors.red("NOT ACHIEVED")

        if self.echo_level > 2:
            info_msg += '\n\t abs-norm = {:.2e} | abs-tol = {} || rel-norm = {:.2e} | rel-tol = {}'.format(
                abs_norm, self.abs_tolerance, rel_norm, self.rel_tolerance)

        if info_msg != "":
            cs_tools.cs_print_info(self._ClassName(), info_msg)

        return is_converged
    def IsConverged(self):
        new_data = self.interface_data.GetData()

        residual = new_data - self.prev_data
        res_norm = la.norm(residual)
        norm_new_data = la.norm(new_data)

        if norm_new_data < 1e-15:
            norm_new_data = 1.0  # to avoid division by zero

        abs_norm = res_norm / np.sqrt(residual.size)
        rel_norm = res_norm / norm_new_data

        is_converged = abs_norm < self.abs_tolerance or rel_norm < self.rel_tolerance

        if self.echo_level > 1:
            info_msg = 'Convergence for "' + colors.bold(
                self.interface_data.variable.Name()) + '": '
            if is_converged:
                info_msg += colors.green("ACHIEVED")
            else:
                info_msg += colors.red("NOT ACHIEVED")
            cs_tools.cs_print_info(self._Name(), info_msg)
        if self.echo_level > 2:
            info_msg = colors.bold("abs_norm") + " = " + str(abs_norm) + " | "
            info_msg += colors.bold("abs_tol") + " = " + str(
                self.abs_tolerance) + " || "
            info_msg += colors.bold("rel_norm") + " = " + str(rel_norm) + " | "
            info_msg += colors.bold("rel_tol") + " = " + str(
                self.rel_tolerance)
            cs_tools.cs_print_info(self._Name(), info_msg)

        return is_converged
    def IsConverged(self):
        new_data = self.interface_data.GetData()

        residual = new_data - self.prev_data

        abs_norm = la.norm(residual) / np.sqrt(residual.size)

        if self.initial_iteration:
            self.initial_iteration = False
            self.initial_norm = abs_norm

        rel_norm = abs_norm / self.initial_norm

        is_converged = abs_norm < self.abs_tolerance or rel_norm < self.rel_tolerance

        if self.echo_level > 1:
            info_msg = 'Convergence for "' + colors.bold(
                self.interface_data.variable.Name()) + '": '
            if is_converged:
                info_msg += colors.green("ACHIEVED")
            else:
                info_msg += colors.red("NOT ACHIEVED")
            cs_tools.cs_print_info(self._ClassName(), info_msg)
        if self.echo_level > 2:
            info_msg = colors.bold("abs_norm") + " = " + str(abs_norm) + " | "
            info_msg += colors.bold("abs_tol") + " = " + str(
                self.abs_tolerance) + " || "
            info_msg += colors.bold("rel_norm") + " = " + str(rel_norm) + " | "
            info_msg += colors.bold("rel_tol") + " = " + str(
                self.rel_tolerance)
            cs_tools.cs_print_info(self._ClassName(), info_msg)

        return is_converged
    def IsConverged(self, residual, current_data):
        res_norm = la.norm(residual)
        norm_new_data = la.norm(current_data)

        if norm_new_data < 1e-15:
            norm_new_data = 1.0  # to avoid division by zero

        abs_norm = res_norm / np.sqrt(residual.size)
        rel_norm = res_norm / norm_new_data

        is_converged = abs_norm < self.abs_tolerance or rel_norm < self.rel_tolerance

        info_msg = ""

        if self.echo_level > 1:
            info_msg = 'Convergence '

            if self.label != "":
                info_msg += 'for "{}": '.format(self.label)

            if is_converged:
                info_msg += colors.green("ACHIEVED")
            else:
                info_msg += colors.red("NOT ACHIEVED")

        if self.echo_level > 2:
            info_msg += '\n\t abs-norm = {:.2e} | abs-tol = {} || rel-norm = {:.2e} | rel-tol = {}'.format(
                abs_norm, self.abs_tolerance, rel_norm, self.rel_tolerance)

        if info_msg != "":
            cs_tools.cs_print_info(self._ClassName(), info_msg)

        return is_converged
Пример #5
0
    def SolveSolutionStep(self):
        for k in range(self.num_coupling_iterations):
            if self.echo_level > 0:
                cs_tools.cs_print_info(
                    self._ClassName(), colors.cyan("Coupling iteration:"),
                    colors.bold(
                        str(k + 1) + " / " +
                        str(self.num_coupling_iterations)))

            for coupling_op in self.coupling_operations_dict.values():
                coupling_op.InitializeCouplingIteration()

            for conv_acc in self.convergence_accelerators_list:
                conv_acc.InitializeNonLinearIteration()

            for conv_crit in self.convergence_criteria_list:
                conv_crit.InitializeNonLinearIteration()

            for solver_name, solver in self.solver_wrappers.items():
                self._SynchronizeInputData(solver_name)
                solver.SolveSolutionStep()
                self._SynchronizeOutputData(solver_name)

            for coupling_op in self.coupling_operations_dict.values():
                coupling_op.FinalizeCouplingIteration()

            for conv_acc in self.convergence_accelerators_list:
                conv_acc.FinalizeNonLinearIteration()

            for conv_crit in self.convergence_criteria_list:
                conv_crit.FinalizeNonLinearIteration()

            is_converged = all([
                conv_crit.IsConverged()
                for conv_crit in self.convergence_criteria_list
            ])

            if is_converged:
                if self.echo_level > 0:
                    cs_tools.cs_print_info(
                        self._ClassName(),
                        colors.green("### CONVERGENCE WAS ACHIEVED ###"))
                self.__CommunicateStateOfConvergence(True)
                return True

            if k + 1 >= self.num_coupling_iterations and self.echo_level > 0:
                cs_tools.cs_print_info(
                    self._ClassName(),
                    colors.red("XXX CONVERGENCE WAS NOT ACHIEVED XXX"))
                self.__CommunicateStateOfConvergence(
                    True
                )  # True because max number of iterations is achieved. Otherwise external solver is stuck in time
                return False

            # if it reaches here it means that the coupling has not converged and this was not the last coupling iteration
            self.__CommunicateStateOfConvergence(False)

            # do relaxation only if this iteration is not the last iteration of this timestep
            for conv_acc in self.convergence_accelerators_list:
                conv_acc.ComputeAndApplyUpdate()
Пример #6
0
    def SolveSolutionStep(self):
        for k in range(self.num_coupling_iterations):
            if self.echo_level > 0:
                cs_tools.cs_print_info(
                    self._ClassName(), colors.cyan("Coupling iteration:"),
                    colors.bold(
                        str(k + 1) + " / " +
                        str(self.num_coupling_iterations)))

            for coupling_op in self.coupling_operations_dict.values():
                coupling_op.InitializeCouplingIteration()

            for conv_acc in self.convergence_accelerators_list:
                conv_acc.InitializeNonLinearIteration()

            for conv_crit in self.convergence_criteria_list:
                conv_crit.InitializeNonLinearIteration()

            for solver_name, solver in self.solver_wrappers.items():
                self._SynchronizeInputData(solver_name)
                solver.SolveSolutionStep()
                self._SynchronizeOutputData(solver_name)

            for coupling_op in self.coupling_operations_dict.values():
                coupling_op.FinalizeCouplingIteration()

            for conv_acc in self.convergence_accelerators_list:
                conv_acc.FinalizeNonLinearIteration()

            for conv_crit in self.convergence_criteria_list:
                conv_crit.FinalizeNonLinearIteration()

            is_converged = all([
                conv_crit.IsConverged()
                for conv_crit in self.convergence_criteria_list
            ])

            self.__CommunicateStateOfConvergence(is_converged)

            if is_converged:
                if self.echo_level > 0:
                    cs_tools.cs_print_info(
                        self._ClassName(),
                        colors.green("### CONVERGENCE WAS ACHIEVED ###"))
                return True
            else:
                # TODO I think this should not be done in the last iterations if the solution does not converge in this timestep
                for conv_acc in self.convergence_accelerators_list:
                    conv_acc.ComputeAndApplyUpdate()

            if k + 1 >= self.num_coupling_iterations and self.echo_level > 0:
                cs_tools.cs_print_info(
                    self._ClassName(),
                    colors.red("XXX CONVERGENCE WAS NOT ACHIEVED XXX"))
                return False
    def IsConverged(self):
        # Compute energy scalar on interface
        current_data = 0.0

        for solver_index in range(0, len(self.interface_data)):
            #check length of data vectors are the same
            interface_energy = 0.0
            data_1 = self.interface_data[solver_index][0].GetData()
            data_2 = self.interface_data[solver_index][1].GetData()
            if len(data_1) != len(data_2):
                self.__RaiseException(
                    'Data vector lengths for conjugate criteria composition must be identical, but they are different!'
                )
            else:
                for i in range(0, len(data_1)):
                    interface_energy += data_1[i] * data_2[i]
            if solver_index == 0:
                current_data = interface_energy
            else:
                current_data -= self.second_domain_data_sign * interface_energy  #assumes domain_difference

        abs_norm = la.norm(current_data)

        if self.ignore_first_convergence and self.iteration == 1:
            is_converged = False
        else:
            is_converged = abs_norm < self.abs_tolerance

        self.iteration += 1

        info_msg = ""

        if self.echo_level > 1:
            info_msg = 'Convergence '

            if self.label != "":
                info_msg += 'for "{}": '.format(self.label)

            if is_converged:
                info_msg += colors.green("ACHIEVED")
            else:
                info_msg += colors.red("NOT ACHIEVED")

        if self.echo_level > 2:
            info_msg += '\n\t abs-norm = {:.2e} | abs-tol = {}'.format(
                abs_norm, self.abs_tolerance)

        if info_msg != "":
            cs_tools.cs_print_info(self._ClassName(), info_msg)

        return is_converged
Пример #8
0
    def SolveSolutionStep(self):
        num_coupling_interfaces = self.settings["coupling_interfaces"].size()

        self.__CustomPrint(
            1,
            colors.green("Starting import") + " of CouplingInterfaceData ...")

        for i in range(num_coupling_interfaces):
            coupling_interface_settings = self.settings["coupling_interfaces"][
                i]
            if not coupling_interface_settings.Has("data_field_recv"):
                continue
            sub_model_part_name = coupling_interface_settings[
                "sub_model_part_name"].GetString()
            data_field_settings = coupling_interface_settings[
                "data_field_recv"]

            data_field_name = data_field_settings["data_field_name"].GetString(
            )

            self.__CustomPrint(
                2,
                colors.green('Importing') +
                ' data-field "{}" on ModelPart "{}"'.format(
                    data_field_name, sub_model_part_name))

            variables = GenerateVariableListFromInput(
                data_field_settings["variables"])

            if not self.dry_run:
                KratosCoSim.EMPIRE_API.EMPIRE_API_recvDataField(
                    self.model["ExtSolver." + sub_model_part_name],
                    data_field_name,
                    *variables)  # passing all varibales from the list
            else:
                self.__CustomPrint(2, colors.magenta('... skipped'))

        self.__CustomPrint(
            1,
            colors.green("Finished import") + " of CouplingInterfaceData")

        if self.echo_level > 0: print()  # newline
        self.__CustomPrint(1, colors.blue("Solving ..."))
        time.sleep(self.solving_time)
        self.__CustomPrint(2,
                           "Solving took {} [sec]".format(self.solving_time))
        if self.echo_level > 0: print()  # newline
        # TODO implement random values ... ?

        self.__CustomPrint(
            1,
            colors.cyan("Starting export") + " of CouplingInterfaceData ...")

        for i in range(num_coupling_interfaces):
            coupling_interface_settings = self.settings["coupling_interfaces"][
                i]
            if not coupling_interface_settings.Has("data_field_send"):
                continue
            sub_model_part_name = coupling_interface_settings[
                "sub_model_part_name"].GetString()
            data_field_settings = coupling_interface_settings[
                "data_field_send"]

            data_field_name = data_field_settings["data_field_name"].GetString(
            )

            self.__CustomPrint(
                2,
                colors.cyan('Exporting') +
                ' data-field "{}" on ModelPart "{}"'.format(
                    data_field_name, sub_model_part_name))

            variables = GenerateVariableListFromInput(
                data_field_settings["variables"])

            if not self.dry_run:
                KratosCoSim.EMPIRE_API.EMPIRE_API_sendDataField(
                    self.model["ExtSolver." + sub_model_part_name],
                    data_field_name,
                    *variables)  # passing all varibales from the list
            else:
                self.__CustomPrint(2, colors.magenta('... skipped'))

        self.__CustomPrint(
            1,
            colors.cyan("Finished export") + " of CouplingInterfaceData")