Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from itertools import permutations, combinations_with_replacement
- _profiling = True # profiling toggle
- if _profiling:
- from cProfile import Profile
- from pstats import Stats
- prof = Profile()
- prof.disable()
- def join_adjacent_repeated_materials(potential_structure):
- """
- Self-explanitory...
- """
- #print potential_structure
- new_layers = [] # List to hold re-cast structure
- for layer in potential_structure:
- if len(new_layers) > 0: # if not the first item in the list of layers
- last_layer=new_layers[-1] # last element of existing layer list
- if layer[0] == last_layer[0]: # true is the two layers are the same material
- combined_layer = (layer[0], layer[1] + last_layer[1])
- new_layers[len(new_layers)-1] = combined_layer
- else: # adjcent layers are different material so no comibantion is possible
- new_layers.append(layer)
- else: # for the first layer
- new_layers.append(layer)
- return tuple(new_layers)
- def calculate_unique_structure_lengths(thicknesses, materials, maximum_number_of_layers,\
- maximum_individual_layer_thicknesses, \
- maximum_total_material_thicknesses):
- """
- Create a set on all possible multilayer combinations.
- thicknesses : if this contains '0' the total number of layers will vary
- from 0 to maximum_number_of_layers, otherwise, the
- number total number layers will always be maximum_number_of_layers
- e.g. arange(0 , 100, 5)
- materials : list of materials used
- e.g. ['Metal', 'Dielectric']
- maximum_number_of_layers : pretty self-explanitory...
- e.g. 5
- maximum_individual_layer_thicknesses : filters the created the multilayer structures
- preventing the inclusion layers that are too thick
- - this is important after the joining of
- adjacent materials
- e.g. (('Metal',30),('Dielectric',20))
- maximum_total_material_thicknesses : similar to the above but filters structures where the total
- amount of a particular material is exceeded
- e.g. (('Metal',50),('Dielectric',100))
- """
- # generate all possible thickness combinations and material combinations
- all_possible_thickness_sets = set(permutations(combinations_with_replacement(thicknesses, maximum_number_of_layers)))
- all_possible_layer_material_orders = set(permutations(combinations_with_replacement(materials, maximum_number_of_layers)))
- first_set = set() # Create set object (list of unique elements, no repeats)
- for layer_material_order in all_possible_layer_material_orders:
- for layer_thickness_set in all_possible_thickness_sets:
- potential_structure = [] # list to hold this structure
- for layer, thickness in zip(layer_material_order[0], layer_thickness_set[0]): # combine the layer thickness with its material
- if thickness != 0: # layers of zero thickness are not added to potential_structure
- potential_structure.append((layer, thickness))
- first_set.add(tuple(potential_structure)) # add this potential_structure to the first_set set
- #print('first_set')
- #for struct in first_set:
- # print struct
- ## join adjacent repeated materials
- second_set = set() # create new set
- for potential_structure in first_set:
- second_set.add(join_adjacent_repeated_materials(potential_structure))
- ## remove structures where a layer is too thick
- third_set = set()
- for potential_structure in second_set: # check all the structures in the set
- conditions_satisfied=True # default
- for max_condition in maximum_individual_layer_thicknesses: # check this structure using each condition
- for layer in potential_structure: # examine each layer
- if layer[0] == max_condition[0]: # match condition with material
- if layer[1] > max_condition[1]: # test thickness condition
- conditions_satisfied=False
- if conditions_satisfied:
- third_set.add(potential_structure)
- ##remove structures that contain too much of a certain material
- fourth_set = set()
- for potential_structure in second_set: # check all the structures in the set
- conditions_satisfied=True # default
- for max_condition in maximum_total_material_thicknesses: # check this structure using each condition
- amount_of_material_in_this_structure = 0 # initialise a counter
- for layer in potential_structure: # examine each layer
- if layer[0] == max_condition[0]: # match condition with material
- amount_of_material_in_this_structure += layer[1]
- if amount_of_material_in_this_structure > max_condition[1]: # test thickness condition
- conditions_satisfied=False
- if conditions_satisfied:
- fourth_set.add(potential_structure)
- return fourth_set
- thicknesses = [0,1,2]
- materials = ('A', 'B') # Tuple cannot be accidentally appended to later
- maximum_number_of_layers = 3
- maximum_individual_layer_thicknesses=(('A',30),('B',20))
- maximum_total_material_thicknesses=(('A',20),('B',15))
- if _profiling:
- prof.enable()
- calculate_unique_structure_lengths(thicknesses, materials, maximum_number_of_layers,\
- maximum_individual_layer_thicknesses = maximum_individual_layer_thicknesses, \
- maximum_total_material_thicknesses = maximum_total_material_thicknesses)
- if _profiling:
- prof.disable()
- prof.dump_stats('unique.stats')
- with open('unique_stats.txt', 'wt') as output:
- stats = Stats('unique.stats', stream=output)
- stats.strip_dirs().sort_stats('cumulative', 'time')
- stats.sort_stats('cumulative', 'time')
- stats.print_stats()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement