コード例 #1
0
ファイル: calculs.py プロジェクト: Re-Lien/pyturbo
 def __getCalculator__(self):
     c = vtk.vtkArrayCalculator()
     c.AddCoordinateScalarVariable('coordx', 0)
     c.AddCoordinateScalarVariable('coordy', 1)
     c.AddCoordinateScalarVariable('coordz', 2)
     c.AddCoordinateVectorVariable('coords', 0, 1, 2)
     for scalar_description in self.variables_scalaires:
         c.AddScalarVariable(scalar_description[0], 
                 scalar_description[1], scalar_description[2])
     for vector_description in self.variables_vectorielles:
         c.AddVectorVariable(vector_description[0], 
                 vector_description[1], vector_description[2], 
                 vector_description[3], vector_description[4])
     c.SetFunction(self.formule)
     if self.resultat_en_coordonnees:
         c.SetCoordinateResults(1)
     else:
         c.SetResultArrayName(self.nom_du_resultat)
     c.ReplaceInvalidValuesOn()
     c.SetReplacementValue(0.0)
     return c
コード例 #2
0
ファイル: calculs.py プロジェクト: Re-Lien/pyturbo
    #_____________________________________________________________________________________
    def Update(self):
        # dans le cas ou une seule variable est demande, 
        # il faut quand meme que a_calculer soit un tuple
        if isinstance(self.a_calculer, str):
            self.a_calculer = [self.a_calculer]
        
        to_do = self.__get_what_to_do__()
        
        variables_to_have = []
        for i in to_do:
            variables_to_have += i[0].values()
        for i in variables_to_have:
            while variables_to_have.count(i) != 1:
                variables_to_have.remove(i)
        if len(variables_to_have) != 0:
            newCalculator = self.SimilarInstance()
            newCalculator.keepIntermediateVariables = True
            newCalculator.set('a_calculer', list(variables_to_have))
            try: 
                self.output = newCalculator.get_output()
            except:
                print "n'arrive pas a obtenir {0}".format(variables_to_have)
                raise IOError, "impossible de derouler le pipe de calcul"
        else:
            self.output = vtk_new_instance(self.input)
            if isinstance(self.output, vtk.vtkMultiBlockDataSet):
                for numbloc in get_numeros_blocs_non_vides(self.input):
                    bloc = vtk_new_shallowcopy(self.input.GetBlock(numbloc))
                    self.output.SetBlock(numbloc, bloc)
            else:
                self.output.ShallowCopy(self.input)
            self.output.SetFieldData(self.input.GetFieldData())
        
        for function in to_do:
            # si il y a quelque chose a faire, mais que la formule associee est vide
            # c'est que le calculateur est perdu
            if function[1] == '':
                raise IOError
            
            if function[2] in get_noms_arrays_presents(self.output):
                #si le array a calculer est deja present au noeuds de self.output, c'est pas la peine de 
                #le recalculer
                pass
            elif function[1] == 'UVParametrization':
                self.output = UVParametrization(self.output, 
                    hubFileName = self.hubFileName, tipFileName = self.tipFileName)
            #elif function[1] == 'Q_criterion':
                #self.output = Q_criterion(self.output, self.relativeVelocityArrayName)
            elif len(function[0]) == 1 and function[1] == 'grad(' + function[0].values()[0] + ')':
                for numbloc in get_numeros_blocs_non_vides(self.output) \
                        if isinstance(self.output, vtk.vtkMultiBlockDataSet) else [None]:
                    current_bloc = self.output.GetBlock(numbloc) if numbloc != None else self.output
                    gradient_calculator = vtk.vtkGradientFilter()
                    gradient_calculator.SetInput(current_bloc)
                    gradient_calculator.SetInputScalars(0, function[0].values()[0])
                    gradient_calculator.SetResultArrayName(function[2])
                    gradient_calculator.Update()
                    
                    current_bloc.ShallowCopy(gradient_calculator.GetOutput())
                # if isinstance(self.output, vtk.vtkMultiBlockDataSet):
                    # for numbloc in get_numeros_blocs_non_vides(self.output):
                        # gradient_calculator = vtk.vtkGradientFilter()
                        # gradient_calculator.SetInput(self.output.GetBlock(numbloc))
                        # gradient_calculator.SetInputScalars(0, function[0].values()[0])
                        # gradient_calculator.SetResultArrayName(function[2])
                        # gradient_calculator.Update()
                        # self.output.SetBlock(numbloc, gradient_calculator.GetOutput())
                # else:
                    # gradient_calculator = vtk.vtkGradientFilter()
                    # gradient_calculator.SetInput(self.output)
                    # gradient_calculator.SetInputScalars(0, function[0].values()[0])
                    # gradient_calculator.SetResultArrayName(function[2])
                    # gradient_calculator.Update()
                    # self.output = gradient_calculator.GetOutput()
            else:
                for numbloc in get_numeros_blocs_non_vides(self.output) \
                        if isinstance(self.output, vtk.vtkMultiBlockDataSet) else [None]:
                    current_bloc = self.output.GetBlock(numbloc) if numbloc != None else self.output
                    
                    calc = vtk.vtkArrayCalculator()
                    if self.use_cell_data:
                        calc.SetAttributeModeToUseCellData()
                    calc.SetInput(current_bloc)
                    calc.SetFunction(function[1])
                    for var_input in function[0].items():
                        if self.use_cell_data == True:
                            if current_bloc.GetCellData().GetArray(var_input[1]).GetNumberOfComponents() == 3:
                                calc.AddVectorVariable(var_input[0], var_input[1])
                            else:
                                calc.AddScalarVariable(var_input[0], var_input[1])
                        elif self.use_cell_data == False:
                            if current_bloc.GetPointData().GetArray(var_input[1]).GetNumberOfComponents() == 3:
                                calc.AddVectorVariable(var_input[0], var_input[1])
                            else:
                                calc.AddScalarVariable(var_input[0], var_input[1])
                    calc.AddCoordinateScalarVariable('coordx', 0)
                    calc.AddCoordinateScalarVariable('coordy', 1)
                    calc.AddCoordinateScalarVariable('coordz', 2)
                    calc.SetResultArrayName(function[2])
                    calc.ReplaceInvalidValuesOn()
                    calc.SetReplacementValue(0.0)
                    calc.Update()
                    current_bloc.ShallowCopy(calc.GetOutput())
        
        if self.keepIntermediateVariables == False:
            cleanOutput = vtk_new_shallowcopy(self.output)
            list_to_keep = list(self.a_calculer) + \
                get_noms_arrays_presents(self.input, loc = 'points')

            for quantity in get_noms_arrays_presents(cleanOutput, loc = 'points'):
                if not(quantity in list_to_keep):
                    if isinstance(cleanOutput, vtk.vtkMultiBlockDataSet):
                        for numbloc in get_numeros_blocs_non_vides(cleanOutput):
                            cleanOutput.GetBlock(numbloc).GetPointData().RemoveArray(quantity)
                    else:
                        cleanOutput.GetPointData().RemoveArray(quantity)
            self.output = cleanOutput
        
        #si un nom du resultat est donne, on change le nom de l'array au point
        if self.nom_resultat != None:
            #on convertit d'abord nom_result en une liste si ce n'en est pas une
            if not isinstance(self.nom_resultat, list):
                self.nom_resultat = [self.nom_resultat]
            
            #on verifie de nom_resultat et a_calculer font les memes longueurs
            if len(self.nom_resultat) != len(self.a_calculer):
                raise IOError, "il n'y a pas le meme nombre de a_calculer et nom_resultat"

            #cas multibloc
            for k in range(len(self.a_calculer)):
                avant = self.a_calculer[k]
                apres = self.nom_resultat[k]
                if isinstance(self.output, vtk.vtkMultiBlockDataSet):
                    for numbloc in get_numeros_blocs_non_vides(self.output):
                        self.output.GetBlock(numbloc).GetPointData().GetArray(avant).SetName(apres)
                else:
                    self.output.GetPointData().GetArray(avant).SetName(apres)
            
        self._mettre_a_jour = False