Esempio n. 1
0
 def LoadFromFile(self, filename):
     """ Loads an old run from a file
     
     Parameters
     ----------
     filename : string
         Name of the file that everything will be saved to
     
     Returns
     -------
     None
         
     """
     
     data = np.load(filename, allow_pickle=True).item()
     from FEM import FEM
     self.fem = FEM()
     self.fem.Load(data['fem'])
     
     if data['update']['type'] == 'OC':
         self.update = Update.OCUpdateScheme(0.2, 0.5, 0.5 * np.ones(self.fem.nElem),
                                   np.zeros(self.fem.nElem), np.ones(self.fem.nElem))
     elif data['update']['type'] == 'MMA':
         self.update = Update.MMA(0.5 * np.ones(self.fem.nElem), 1,
                     np.zeros(self.fem.nElem), np.ones(self.fem.nElem))
     self.update.Load(data['update'])
             
     self.Filter = data['opt']['Filter']
     try:
         self.R = data['opt']['R']
     except:
         pass
     import Functions as Funcs
     for objective in data['opt']['objectives']:
         self.AddFunction(getattr(Funcs, objective['function']),
                          objective['weight'], objective['min'],
                          objective['max'], 'objective')
     for constraint in data['opt']['constraints']:
         self.AddFunction(getattr(Funcs, constraint['function']),
                          constraint['constraint'], constraint['min'],
                          constraint['max'], 'constraint')
Esempio n. 2
0
 def LoadPetsc(self, folder, appendix=None, Endian='=', update='MMA'):
     """ Create PyOpt structure from PETSc code results
     
     Parameters
     ----------
     folder : str
         folder containing all of the Petsc results
     appendix : str
         Appendix for result values to restart from, if none picks highest penalty
     Endian : char
         Indicates byte ordering ('=':default, '<':little Endian, '>':big Endian)
     update : str
         Which updte scheme to use (MMA or OC)
     
     Returns
     -------
     None
         
     """
     
     from os import listdir
     from os.path import sep
     from PetscBinaryIO import PetscBinaryRead
     import Functions_Timing as Funcs
     
     # Load FEM data
     from FEM import FEM
     self.fem = FEM()
     self.fem.LoadPetsc(folder, Endian=Endian)
     
     # Load update data
     if update == 'OC':
         self.update = Update.OCUpdateScheme(0.2, 0.5, 0.5 * np.ones(self.fem.nElem),
                                   np.zeros(self.fem.nElem), np.ones(self.fem.nElem))
     elif update == 'MMA':
         self.update = Update.MMA(0.5 * np.ones(self.fem.nElem), 1,
                     np.zeros(self.fem.nElem), np.ones(self.fem.nElem))
     self.update.LoadPetsc(folder, appendix=appendix, Endian=Endian)
           
     # Load filter matrics
     self.Filter = PetscBinaryRead(folder + sep + "Filter.bin")
     try:
         self.R = PetscBinaryRead(folder + sep + "Max_Filter.bin")
         edge = PetscBinaryRead(folder + sep + "Void_Edge_Volume.bin")
         self.R = self.R.tocoo()
         self.R = sparse.csr_matrix((np.concatenate([self.R.data, edge]),
                                    (np.concatenate([self.R.row, np.arange(self.R.shape[0])]),
                                     np.concatenate([self.R.col, self.R.shape[0]*np.ones(self.R.shape[0], dtype=int)]))))
     except:
         self.R = sparse.dia_matrix((np.ones(self.fem.nElem), np.zeros(self.fem.nElem)))
     
     # Set up functions and material properties
     inputFile = [file for file in listdir(folder) if '_Input' in file][0]
     active = False
     name = None
     fType = None
     value = None
     minimum = None
     maximum = None
     E0, Nu0, Density = None, None, None
     with open(folder + sep + inputFile, 'r') as fh:
         for line in fh:
             line = line.strip()
             if line[:3] == 'E0:':
                 E0 = float(line.split(':')[-1])
             elif line[:4] == 'Nu0:':
                 Nu0 = float(line.split(':')[-1])
             elif line[:8] == 'Density:':
                 Density = float(line.split(':')[-1])
             elif '[Functions]' in line:
                 active = True
             elif '[/Functions]' in line:
                 active = False
             elif active:
                 if line in ['Compliance', 'Stability', 'Frequencey', 'Volume']:
                     name = line
                 elif line in ['Objective', 'Constraint']:
                     fType = line
                 elif 'Values:' in line:
                     value = [float(val) for val in line.split(':')[-1].split(',')][0]
                 elif 'Range:' in line:
                     minimum, maximum = [float(val) for val in line.split(':')[-1].split(',')]
                 if name is not None and fType is not None and value is not None and minimum is not None:
                     self.AddFunction(getattr(Funcs, name), value, minimum, maximum, fType)
                     name = None
                     fType = None
                     value = None
                     minimum = None
                     maximum = None
       
     if self.fem.nDof == 2:             
         self.fem.SetMaterial(Material.PlaneStressElastic(E0, Nu0))
     else:
         self.fem.SetMaterial(Material.Elastic3D(E0, Nu0))