-
Jan Habscheid authoredJan Habscheid authored
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
01_GenerateMicrostructures.py 6.36 KiB
import numpy as np
import argparse
import splinepy as sp
import pandas as pd
import os
import shutil
from gismo.gismo_export import AdditionalBlocks, export
from gismo.GenerateMicrostructures_fun import generate
EPS = 1e-8
BOX_LENGTH = 0.14
BOX_HEIGHT = 0.041
SHOW_MICROSTRUCTURE = False
FILENAME = "microstructure.xml"
N_THREADS = 1
sp.settings.NTHREADS = N_THREADS
# Simulation parameters
TILING = [6, 3]#[5, 3]
N_REFINEMENTS = 0
parser = argparse.ArgumentParser(
description="Script that reads the Elevation Degrees from CMD"
)
parser.add_argument("--e", required=False, type=int)
parser.add_argument("--r", required=False, type=int)
args = parser.parse_args()
if args.e is not None:
DEGREE_ELEVATIONS = args.e
else:
DEGREE_ELEVATIONS = 1
if args.r is not None:
H_REFINEMENTS = args.r
else:
H_REFINEMENTS = 0
print('DEGREE_ELEVATIONS:', DEGREE_ELEVATIONS)
print('H_REFINEMENTS:', H_REFINEMENTS)
if not os.path.exists(f'Data/H_REFINEMENTS_{H_REFINEMENTS}'):
os.mkdir(f'Data/H_REFINEMENTS_{H_REFINEMENTS}')
INLET_BOUNDARY_ID = 2
OUTLET_BOUNDARY_ID = 3
INLET_PEAK_VELOCITY = 1.4336534897721067
CLOSING_FACE = "x"
# OBJECTIVE_FUNCTION = [1, 3]
# OBJECTIVE_FUNCTION_WEIGHTS = [49927, 10.027]
OBJECTIVE_FUNCTION = [2, 3]
# OBJECTIVE_FUNCTION_WEIGHTS = [6.673e13, 5.700225]
OBJECTIVE_FUNCTION_WEIGHTS = [7e5, 2e-3]
microtile_string = 'HollowOctagon'
MICROTILE = sp.microstructure.tiles.HollowOctagon()
# ! Use random sizes
SIZES_lower_bound = 0.04
SIZES_upper_bound = 0.36
SIZES_TRAIN = [0.04, 0.12, 0.20, 0.28, 0.36]
SIZES_TEST = [0.08, 0.16, 0.24, 0.32]
# Dictionary to store the setup
data_setup = {
'EPS': EPS,
'BOX_LENGTH': BOX_LENGTH,
'BOX_HEIGHT': BOX_HEIGHT,
'SHOW_MICROSTRUCTURE': SHOW_MICROSTRUCTURE,
'FILENAME': FILENAME,
'N_THREADS': N_THREADS,
# Simulation parameters
'TILING': TILING,
'N_REFINEMENTS': N_REFINEMENTS,
'DEGREE_ELEVATIONS': DEGREE_ELEVATIONS,
'H_REFINEMENTS': H_REFINEMENTS,
'INLET_BOUNDARY_ID': INLET_BOUNDARY_ID,
'OUTLET_BOUNDARY_ID': OUTLET_BOUNDARY_ID,
'INLET_PEAK_VELOCITY': INLET_PEAK_VELOCITY,
'CLOSING_FACE': CLOSING_FACE,
'OBJECTIVE_FUNCTION': OBJECTIVE_FUNCTION,
'OBJECTIVE_FUNCTION_WEIGHTS': OBJECTIVE_FUNCTION_WEIGHTS,
'MICROTILE': microtile_string,
'N_SIZES_TRAIN': len(SIZES_TRAIN)**TILING[1],
'N_SIZES_TEST': len(SIZES_TEST)**TILING[1],
}
data_sizes = {
'size_1': [],
'size_2': [],
'size_3': [],
}
if __name__ == "__main__":
# Define microstructure deformation function
initial_macro_spline = sp.helpme.create.box(BOX_LENGTH, BOX_HEIGHT)
# x0_max, x1_max = initial_macro_spline.cps.max(axis=0)
# print(f'x0_max: {x0_max}, x1_max: {x1_max}')
# Define identifier functions for microstructure boundaries
def identifier_inlet(points):
return points[:, 0] < EPS
def identifier_outlet(points):
return points[:, 0] > BOX_LENGTH - EPS
boundary_identifier_dict = {
identifier_inlet: INLET_BOUNDARY_ID,
identifier_outlet: OUTLET_BOUNDARY_ID,
}
knots_y = np.linspace(0, 1, TILING[1] * 2 + 1)[1:-1:2]
# print(f'knots_y: {knots_y}')
train_index = 0
# Train models
if not os.path.exists(f'Data/H_REFINEMENTS_{H_REFINEMENTS}'): os.mkdir(f'Data/H_REFINEMENTS_{H_REFINEMENTS}')
if os.path.exists(f'Data/H_REFINEMENTS_{H_REFINEMENTS}/train'): shutil.rmtree(f'Data/H_REFINEMENTS_{H_REFINEMENTS}/train')
os.mkdir(f'Data/H_REFINEMENTS_{H_REFINEMENTS}/train')
if os.path.exists(f'Data/H_REFINEMENTS_{H_REFINEMENTS}/train/microstructure'): shutil.rmtree(f'Data/H_REFINEMENTS_{H_REFINEMENTS}/train/microstructure/')
os.mkdir(f'Data/H_REFINEMENTS_{H_REFINEMENTS}/train/microstructure')
if os.path.exists(f'Data/H_REFINEMENTS_{H_REFINEMENTS}/test'): shutil.rmtree(f'Data/H_REFINEMENTS_{H_REFINEMENTS}/test')
os.mkdir(f'Data/H_REFINEMENTS_{H_REFINEMENTS}/test')
if os.path.exists(f'Data/H_REFINEMENTS_{H_REFINEMENTS}/test/microstructure'): shutil.rmtree(f'Data/H_REFINEMENTS_{H_REFINEMENTS}/test/microstructure/')
os.mkdir(f'Data/H_REFINEMENTS_{H_REFINEMENTS}/test/microstructure')
for size_1 in SIZES_TRAIN:
for size_2 in SIZES_TRAIN:
for size_3 in SIZES_TRAIN:
data_sizes['size_1'].append(size_1)
data_sizes['size_2'].append(size_2)
data_sizes['size_3'].append(size_3)
microstructure, gismo_export_options = generate(
size_1, size_2, size_3, BOX_LENGTH, BOX_HEIGHT, EPS, INLET_BOUNDARY_ID, OUTLET_BOUNDARY_ID, knots_y, TILING, CLOSING_FACE, MICROTILE
)
export(
fname=f'Data/H_REFINEMENTS_{H_REFINEMENTS}/train/microstructure/index_{train_index}.xml', multipatch=microstructure,
indent=True,
additional_blocks=gismo_export_options,
as_base64=False
)
train_index+=1
data_setup['N_SIZES_TRAIN'] = train_index
df_sizes = pd.DataFrame(data_sizes)
df_sizes.to_excel(f'Data/H_REFINEMENTS_{H_REFINEMENTS}/train/parameter_input.xlsx', index=True)
# Test models
test_index = 0
for size_1 in SIZES_TEST:
for size_2 in SIZES_TEST:
for size_3 in SIZES_TEST:
data_sizes['size_1'].append(size_1)
data_sizes['size_2'].append(size_2)
data_sizes['size_3'].append(size_3)
microstructure, gismo_export_options = generate(
size_1, size_2, size_3, BOX_LENGTH, BOX_HEIGHT, EPS, INLET_BOUNDARY_ID, OUTLET_BOUNDARY_ID, knots_y, TILING, CLOSING_FACE, MICROTILE
)
export(
fname=f'Data/H_REFINEMENTS_{H_REFINEMENTS}/test/microstructure/index_{test_index}.xml', multipatch=microstructure,
indent=True,
additional_blocks=gismo_export_options,
as_base64=False
)
test_index+=1
data_setup['N_SIZES_TEST'] = test_index
df_sizes = pd.DataFrame(data_sizes).iloc[train_index:]
df_sizes.to_excel(f'Data/H_REFINEMENTS_{H_REFINEMENTS}/test/parameter_input.xlsx', index=True)
# df = pd.DataFrame(data)
df_setup = pd.DataFrame.from_dict(data_setup, orient='index')
df_setup = df_setup.transpose()
df_setup.to_excel(f'Data/H_REFINEMENTS_{H_REFINEMENTS}/HollowOctagon_Setup.xlsx', index=False)