def UpdateSolution( self, r, x ): self.R.appendleft( deepcopy(r) ) ## For the first iteration, do relaxation only if self.initial_iteration: self.initial_iteration = False alpha = min( self.alpha_old, self.init_alpha_max ) if self.echo_level > 3: cs_tools.cs_print_info(self._ClassName(), ": Doing relaxation in the first iteration with initial factor = {}".format(alpha)) return alpha * r else: r_diff = self.R[0] - self.R[1] numerator = np.inner( self.R[1], r_diff ) denominator = np.inner( r_diff, r_diff ) alpha = -self.alpha_old * numerator/denominator if self.echo_level > 3: cs_tools.cs_print_info(self._ClassName(), ": Doing relaxation with factor = {}".format(alpha)) if alpha > self.alpha_max: alpha = self.alpha_max if self.echo_level > 0: cs_tools.cs_print_warning(self._ClassName(), "dynamic relaxation factor reaches upper bound: {}".format(self.alpha_max)) elif alpha < self.alpha_min: alpha = self.alpha_min if self.echo_level > 0: cs_tools.cs_print_warning(self._ClassName(), "dynamic relaxation factor reaches lower bound: {}".format(self.alpha_min)) delta_x = alpha * self.R[0] self.alpha_old = alpha return delta_x
def ComputeUpdate( self, r, x ): self.R.appendleft( deepcopy(r) ) k = len( self.R ) - 1 ## For the first iteration, do relaxation only if k == 0: alpha = min( self.alpha_old, self.init_alpha_max ) if self.echo_level > 3: cs_tools.cs_print_info(self._Name(), ": Doing relaxation in the first iteration with initial factor = " + "{0:.1g}".format(alpha)) return alpha * r else: r_diff = self.R[0] - self.R[1] numerator = np.inner( self.R[1], r_diff ) denominator = np.inner( r_diff, r_diff ) alpha = -self.alpha_old * numerator/denominator if self.echo_level > 3: cs_tools.cs_print_info(self._Name(), ": Doing relaxation with factor = " + "{0:.1g}".format(alpha)) if alpha > 20: alpha = 20 if self.echo_level > 0: cs_tools.cs_print_warning(self._Name(), "dynamic relaxation factor reaches upper bound: 20") elif alpha < -2: alpha = -2 if self.echo_level > 0: cs_tools.cs_print_warning(self._Name(), "dynamic relaxation factor reaches lower bound: -2") delta_x = alpha * self.R[0] self.alpha_old = alpha return delta_x
def Initialize(self): # This can only be called after the ModelPart are read, i.e. after the solvers are initialized self.model_part = self.model[self.settings["model_part_name"].GetString()] # dimensionality of the data self.dimension = self.settings["dimension"].GetInt() if self.is_scalar_variable: if self.dimension != -1: raise Exception('"dimension" cannot be specifed for scalar variables!') self.dimension = 1 # needed in other places, e.g. for "Size" else: if self.dimension < 1: raise Exception('"dimension" has to be specifed for vector variables!') else: if self.variable_type == "Array" and self.dimension not in [1,2,3]: raise Exception('"dimension" can only be 1,2,3 when using variables of type "Array"') domain_size = self.GetModelPart().ProcessInfo[KM.DOMAIN_SIZE] if domain_size == 0: cs_tools.cs_print_warning('CouplingInterfaceData', 'No "DOMAIN_SIZE" was specified for ModelPart "{}"'.format(self.GetModelPart().Name)) if domain_size != self.dimension: cs_tools.cs_print_warning('CouplingInterfaceData', '"DOMAIN_SIZE" ({}) of ModelPart "{}" does not match dimension ({})'.format(domain_size, self.GetModelPart().Name, self.dimension)) if self.location == "node_historical": if not self.GetModelPart().HasNodalSolutionStepVariable(self.variable): raise Exception('"{}" is missing as SolutionStepVariable in ModelPart "{}"'.format(self.variable.Name(), self.GetModelPart().Name))
def __init__(self, cosim_settings, models=None): # Note: deliberately NOT calling the base-class constructor since arguments are different if not isinstance(cosim_settings, KM.Parameters): raise Exception( "Input is expected to be provided as a Kratos Parameters object" ) self.cosim_settings = cosim_settings self.models = models # this contains only the optional parameters, not the ones that have to be specified problem_data_defaults = KM.Parameters("""{ "problem_name" : "default_co_simulation", "print_colors" : false, "echo_level" : 1 }""") problem_data = cosim_settings["problem_data"] problem_data.AddMissingParameters(problem_data_defaults) colors.PRINT_COLORS = problem_data["print_colors"].GetBool() self.echo_level = problem_data["echo_level"].GetInt() self.parallel_type = problem_data["parallel_type"].GetString() is_distributed_run = KM.IsDistributedRun() if self.parallel_type == "OpenMP": if is_distributed_run: cs_tools.cs_print_warning( "Parallel Type", 'Specified "OpenMP" as "parallel_type", but Kratos is running in "MPI", please check your setup!' ) elif self.parallel_type == "MPI": if not is_distributed_run: cs_tools.cs_print_warning( "Parallel Type", 'Specified "MPI" as "parallel_type", but Kratos is running in "OpenMP", please check your setup!' ) else: raise Exception( 'The "parallel_type" can be either "OpenMP" or "MPI"') if problem_data.Has("flush_stdout"): self.flush_stdout = problem_data["flush_stdout"].GetBool() else: # flush by default only in OpenMP, can decrease performance in MPI self.flush_stdout = (self.parallel_type == "OpenMP") self._GetSolver() # this creates the solver
def Initialize(self): # This can only be called after the ModelPart are read, i.e. after the solvers are initialized self.model_part = self.model[self.settings["model_part_name"].GetString()] # variable used to identify data variable_name = self.settings["variable_name"].GetString() self.variable_type = KM.KratosGlobals.GetVariableType(variable_name) admissible_scalar_variable_types = ["Bool", "Integer", "Unsigned Integer", "Double", "Component"] admissible_vector_variable_types = ["Array"] if not self.variable_type in admissible_scalar_variable_types and not self.variable_type in admissible_vector_variable_types: raise Exception('The input for "variable" "{}" is of variable type "{}" which is not allowed, only the following variable types are allowed:\n{}, {}'.format(variable_name, self.variable_type, ", ".join(admissible_scalar_variable_types), ", ".join(admissible_vector_variable_types))) self.variable = KM.KratosGlobals.GetVariable(variable_name) self.dtype = GetNumpyDataType(self.variable_type) # required for numpy array creation self.is_scalar_variable = self.variable_type in admissible_scalar_variable_types # dimensionality of the data self.dimension = self.settings["dimension"].GetInt() if self.is_scalar_variable: if self.dimension != -1: raise Exception('"dimension" cannot be specifed for scalar variables!') self.dimension = 1 # needed in other places, e.g. for "Size" else: if self.dimension < 1: raise Exception('"dimension" has to be specifed for vector variables!') else: if self.variable_type == "Array" and self.dimension not in [1,2,3]: raise Exception('"dimension" can only be 1,2,3 when using variables of type "Array"') domain_size = self.GetModelPart().ProcessInfo[KM.DOMAIN_SIZE] if domain_size == 0: cs_tools.cs_print_warning('CouplingInterfaceData', 'No "DOMAIN_SIZE" was specified for ModelPart "{}"'.format(self.GetModelPart().Name)) if domain_size != self.dimension: cs_tools.cs_print_warning('CouplingInterfaceData', '"DOMAIN_SIZE" ({}) of ModelPart "{}" does not match dimension ({})'.format(domain_size, self.GetModelPart().Name, self.dimension)) # location of data on ModelPart self.location = self.settings["location"].GetString() admissible_locations = ["node_historical", "node_non_historical","element","condition","process_info","model_part"] if not self.location in admissible_locations: raise Exception('"{}" is not allowed as "location", only the following options are possible:\n{}'.format(self.location, ", ".join(admissible_locations))) if self.location == "node_historical": if not self.GetModelPart().HasNodalSolutionStepVariable(self.variable): raise Exception('"{}" is missing as SolutionStepVariable in ModelPart "{}"'.format(variable_name, self.GetModelPart().Name))
def Check(self): cs_tools.cs_print_warning("Convergence Criteria", colors.bold(self._ClassName()), 'does not implement "Check"')
def __init__(self, custom_settings, model, name="default", solver_name="default_solver"): custom_settings.ValidateAndAssignDefaults(self.GetDefaultParameters()) self.settings = custom_settings self.model = model self.name = name self.solver_name = solver_name self.model_part_name = self.settings["model_part_name"].GetString() # checking names if self.name == "" or "." in self.name or " " in self.name: self.__RaiseException( 'The name cannot be empty, contain whitespaces or "."!') if self.model_part_name == "": self.__RaiseException('No "model_part_name" was specified!') # variable used to identify data variable_name = self.settings["variable_name"].GetString() if variable_name == "": self.__RaiseException('No "variable_name" was specified!') if not KM.KratosGlobals.HasVariable(variable_name): # TODO here maybe we could construct a new var if necessary (maybe clashes with delayed app-import ...?) self.__RaiseException( 'Variable "{}" does not exist!'.format(variable_name)) self.variable_type = KM.KratosGlobals.GetVariableType(variable_name) admissible_scalar_variable_types = [ "Bool", "Integer", "Unsigned Integer", "Double" ] admissible_vector_variable_types = ["Array"] if not self.variable_type in admissible_scalar_variable_types and not self.variable_type in admissible_vector_variable_types: self.__RaiseException( 'The input for "variable" "{}" is of variable type "{}" which is not allowed, only the following variable types are allowed:\n{}, {}' .format(variable_name, self.variable_type, ", ".join(admissible_scalar_variable_types), ", ".join(admissible_vector_variable_types))) self.variable = KM.KratosGlobals.GetVariable(variable_name) self.dtype = GetNumpyDataType( self.variable_type) # required for numpy array creation self.is_scalar_variable = self.variable_type in admissible_scalar_variable_types # location of data on ModelPart self.location = self.settings["location"].GetString() admissible_locations = [ "node_historical", "node_non_historical", "element", "condition", "model_part" ] if not self.location in admissible_locations: self.__RaiseException( '"{}" is not allowed as "location", only the following options are possible:\n{}' .format(self.location, ", ".join(admissible_locations))) self.model_part = self.model[self.model_part_name] # dimensionality of the data self.dimension = self.settings["dimension"].GetInt() if self.is_scalar_variable: if self.dimension != -1: self.__RaiseException( '"dimension" cannot be specifed for scalar variables!') self.dimension = 1 # needed in other places, e.g. for "Size" else: if self.dimension < 1: self.__RaiseException( '"dimension" has to be specifed for vector variables!') else: if self.variable_type == "Array" and self.dimension not in [ 1, 2, 3 ]: self.__RaiseException( '"dimension" can only be 1,2,3 when using variables of type "Array"' ) if not KM.DOMAIN_SIZE in self.model_part.ProcessInfo: cs_tools.cs_print_warning( 'CouplingInterfaceData', 'No "DOMAIN_SIZE" was specified for ModelPart "{}"'. format(self.model_part_name)) else: domain_size = self.model_part.ProcessInfo[KM.DOMAIN_SIZE] if domain_size != self.dimension: cs_tools.cs_print_warning( 'CouplingInterfaceData', '"DOMAIN_SIZE" ({}) of ModelPart "{}" does not match dimension ({})' .format(domain_size, self.model_part_name, self.dimension)) if self.location == "node_historical" and not self.model_part.HasNodalSolutionStepVariable( self.variable): self.__RaiseException( '"{}" is missing as SolutionStepVariable in ModelPart "{}"'. format(self.variable.Name(), self.model_part_name))