def Run(self):
        num_coupling_interfaces = self.settings["coupling_interfaces"].size()
        self.__CustomPrint(
            1,
            colors.cyan("Starting export") + " of CouplingInterfaces ...")

        for i in range(num_coupling_interfaces):
            coupling_interface_settings = self.settings["coupling_interfaces"][
                i]
            sub_model_part_name = coupling_interface_settings[
                "sub_model_part_name"].GetString()
            comm_name = coupling_interface_settings["comm_name"].GetString()

            receive_interface = False
            operation = "Exporting"
            if coupling_interface_settings.Has("receive_interface"):
                receive_interface = coupling_interface_settings[
                    "receive_interface"].GetBool()
                operation = "Importing"

            self.__CustomPrint(
                2,
                colors.cyan(operation) +
                ' coupling-interface "{}" on ModelPart "{}"'.format(
                    comm_name, sub_model_part_name))

            if not self.dry_run:
                if receive_interface:
                    # use an aux-modelpart, which will not be used again
                    KratosCoSim.EMPIRE_API.EMPIRE_API_recvMesh(
                        self.model.CreateModelPart(sub_model_part_name),
                        comm_name)
                    # TODO maybe restructure and do all receives after all sends
                else:
                    KratosCoSim.EMPIRE_API.EMPIRE_API_sendMesh(
                        self.model["ExtSolver." + sub_model_part_name],
                        comm_name)
            else:
                self.__CustomPrint(2, colors.magenta('... skipped'))

            self.__CustomPrint(
                1,
                colors.cyan("Finished " + operation) +
                " of CouplingInterfaces")

        # time loop
        self.__CustomPrint(1, "Starting Solution Loop")
        for i in range(self.settings["num_steps"].GetInt()):
            if self.echo_level > 0: print()  # newline
            self.__CustomPrint(
                1,
                colors.bold('Step: {}/{}'.format(
                    i + 1, self.settings["num_steps"].GetInt())))
            self.SolveSolutionStep()
        self.__CustomPrint(1, "Finished")
Example #2
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()
    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 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")