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')
bc = [0, 0] bcSpecs = [{'lower':lower, 'upper':upper, 'disp':bc}] fem.AddBC(bcSpecs) # springpoly = np.array([[-1, 0.5-ofst], [2, 0.5-ofst], [2, 0.5+ofst], [-1, 0.5+ofst]]) # stiff = [1, 1] # springSpecs = [{'poly':springpoly, 'stiff':stiff}] # fem.AddSprings(springSpecs) #fem.Plot() fem.SetMaterial(Material.PlaneStressElastic(15e9, 0.3)) #update = Update.OCUpdateScheme(0.2, 0.5, np.linspace(0, 1, fem.nElem, endpoint=False),#0.5 * np.ones(fem.nElem), # np.zeros(fem.nElem), np.ones(fem.nElem)) update = Update.MMA(0.5 * np.ones(fem.nElem), 1, np.zeros(fem.nElem), np.ones(fem.nElem)) opt = PyOpt(fem, update) # Minimum feature size filter radius = 1.5 * (Dimensions[1] - Dimensions[0]) / Nelx opt.Filter = opt.ConstructDensityFilter(radius=radius, nElx=[Nelx, Nely]) # Maximum feature size filter radius = 0.5 * (Dimensions[1] - Dimensions[0]) / Nelx opt.R = opt.ConstructDensityFilter(radius=radius, nElx=[Nelx, Nely]) rowsum = opt.R.indptr[1:] - opt.R.indptr[:-1] edge = rowsum.max() - rowsum opt.R = opt.R.tocoo() opt.R = sparse.csr_matrix((np.concatenate([1+0*opt.R.data, edge]), (np.concatenate([opt.R.row, np.arange(opt.R.shape[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))