import subprocess import re import os import numpy as np import matplotlib.pyplot as plt import math import fileinput import time import matplotlib.ticker as tickers import matplotlib as mpl from matplotlib.ticker import MultipleLocator,FormatStrFormatter,MaxNLocator import codecs import sys import threading # # Schreibe input datei für Parameter # def SetParametersCellProblem(ParameterSet, ParsetFilePath, outputPath): # print('----set Parameters -----') # with open(ParsetFilePath, 'r') as file: # filedata = file.read() # filedata = re.sub('(?m)^materialFunction\s?=.*','materialFunction = '+str(ParameterSet.materialFunction),filedata) # filedata = re.sub('(?m)^gamma\s?=.*','gamma='+str(ParameterSet.gamma),filedata) # filedata = re.sub('(?m)^numLevels\s?=.*','numLevels='+str(ParameterSet.numLevels)+' '+str(ParameterSet.numLevels) ,filedata) # filedata = re.sub('(?m)^outputPath\s?=\s?.*','outputPath='+str(outputPath),filedata) # f = open(ParsetFilePath,'w') # f.write(filedata) # f.close() # # Ändere Parameter der MaterialFunction # def SetParameterMaterialFunction(materialFunction, parameterName, parameterValue): # with open(Path+"/"+materialFunction+'.py', 'r') as file: # filedata = file.read() # filedata = re.sub('(?m)^'+str(parameterName)+'\s?=.*',str(parameterName)+' = '+str(parameterValue),filedata) # f = open(Path+"/"+materialFunction+'.py','w') # f.write(filedata) # f.close() def SetParameterMaterialFunction(inputFunction, parameterName, parameterValue): with open(inputFunction+'.py', 'r') as file: filedata = file.read() filedata = re.sub('(?m)^'+str(parameterName)+'\s?=.*',str(parameterName)+' = '+str(parameterValue),filedata) f = open(inputFunction+'.py','w') f.write(filedata) f.close() # # Rufe Programm zum Lösen des Cell-Problems auf # def run_CellProblem(executable, parset,write_LOG): # print('----- RUN Cell-Problem ----') # processList = [] # LOGFILE = "Cell-Problem_output.log" # print('LOGFILE:',LOGFILE) # print('executable:',executable) # if write_LOG: # p = subprocess.Popen(executable + parset # + " | tee " + LOGFILE, shell=True) # else: # p = subprocess.Popen(executable + parset # + " | tee " + LOGFILE, shell=True) # p.wait() # wait # processList.append(p) # exit_codes = [p.wait() for p in processList] # return # Read effective quantites def ReadEffectiveQuantities(QFilePath = os.path.dirname(os.getcwd()) + '/outputs/QMatrix.txt', BFilePath = os.path.dirname(os.getcwd())+ '/outputs/BMatrix.txt'): # Read Output Matrices (effective quantities) # From Cell-Problem output Files : ../outputs/Qmatrix.txt , ../outputs/Bmatrix.txt # -- Read Matrix Qhom X = [] # with codecs.open(path + '/outputs/QMatrix.txt', encoding='utf-8-sig') as f: with codecs.open(QFilePath, encoding='utf-8-sig') as f: for line in f: s = line.split() X.append([float(s[i]) for i in range(len(s))]) Q = np.array([[X[0][2], X[1][2], X[2][2]], [X[3][2], X[4][2], X[5][2]], [X[6][2], X[7][2], X[8][2]] ]) # -- Read Beff (as Vector) X = [] # with codecs.open(path + '/outputs/BMatrix.txt', encoding='utf-8-sig') as f: with codecs.open(BFilePath, encoding='utf-8-sig') as f: for line in f: s = line.split() X.append([float(s[i]) for i in range(len(s))]) B = np.array([X[0][2], X[1][2], X[2][2]]) return Q, B # Function for evaluating the energy in terms of kappa, alpha and Q, B def eval_energy(kappa,alpha,Q,B) : G=kappa*np.array([[np.cos(alpha)**2],[np.sin(alpha)**2],[np.sqrt(2)*np.cos(alpha)*np.sin(alpha)]])-B return np.matmul(np.transpose(G),np.matmul(Q,G))[0,0] #------------------------------------------------------------------------------------------------------- ######################## #### SET PARAMETERS #### ######################## # ----- Setup Paths ----- # write_LOG = True # writes Cell-Problem output-LOG in "Cell-Problem_output.log" # path='/home/klaus/Desktop/Dune_release/dune-microstructure/experiment/wood-bilayer/results/' # pythonPath = '/home/klaus/Desktop/Dune_release/dune-microstructure/experiment/wood-bilayer' path = os.getcwd() + '/experiment/perforated-bilayer/results/' pythonPath = os.getcwd() + '/experiment/perforated-bilayer' pythonModule = "perforated_wood_upper" # pythonModule = "perforated_wood_lower" executable = os.getcwd() + '/build-cmake/src/Cell-Problem' # --------------------------------- # Setup Experiment # --------------------------------- gamma = 1.0 # ----- Define Parameters for Material Function -------------------- # [r, h, omega_flat, omega_target, theta, experimental_kappa] # r = (thickness upper layer)/(thickness) # h = thickness [meter] # omega_flat = moisture content in the flat state before drying [%] # omega_target = moisture content in the target state [%] # theta = rotation angle (not implemented and used) # beta = design parameter for perforation = ratio of Volume of (cylindrical) perforation to Volume of active/passive layer #Experiment: Perforate "active" bilayer phase materialFunctionParameter=[ [0.22, 0.0053, 17.17547062, 8.959564147, 0.0, 0.0 ], [0.22, 0.0053, 17.17547062, 8.959564147, 0.0, 0.05 ], [0.22, 0.0053, 17.17547062, 8.959564147, 0.0, 0.1 ], [0.22, 0.0053, 17.17547062, 8.959564147, 0.0, 0.15 ], [0.22, 0.0053, 17.17547062, 8.959564147, 0.0, 0.2 ], [0.22, 0.0053, 17.17547062, 8.959564147, 0.0, 0.25 ], [0.22, 0.0053, 17.17547062, 8.959564147, 0.0, 0.3 ] ] # ------ Loops through Parameters for Material Function ----------- for i in range(0,np.shape(materialFunctionParameter)[0]): print("------------------") print("New Loop") print("------------------") # Check output directory outputPath = path + str(i) isExist = os.path.exists(outputPath) if not isExist: # Create a new directory because it does not exist os.makedirs(outputPath) print("The new directory " + outputPath + " is created!") # thread = threading.Thread(target=run_CellProblem(executable, pythonModule, pythonPath, LOGFILE)) # thread.start() #TODO: apperently its not possible to pass a variable via subprocess and "calculate" another input value inside the python file. # Therefore we use this instead. SetParameterMaterialFunction(pythonPath + "/" + pythonModule, "param_r",materialFunctionParameter[i][0]) SetParameterMaterialFunction(pythonPath + "/" + pythonModule, "param_h",materialFunctionParameter[i][1]) SetParameterMaterialFunction(pythonPath + "/" + pythonModule, "param_omega_flat",materialFunctionParameter[i][2]) SetParameterMaterialFunction(pythonPath + "/" + pythonModule, "param_omega_target",materialFunctionParameter[i][3]) SetParameterMaterialFunction(pythonPath + "/" + pythonModule, "param_theta",materialFunctionParameter[i][4]) SetParameterMaterialFunction(pythonPath + "/" + pythonModule, "param_beta",materialFunctionParameter[i][5]) LOGFILE = outputPath + "/" + pythonModule + "_output" + "_" + str(i) + ".log" processList = [] p = subprocess.Popen(executable + " " + pythonPath + " " + pythonModule + " -outputPath " + outputPath + " -gamma " + str(gamma) + " | tee " + LOGFILE, shell=True) # p = subprocess.Popen(executable + " " + pythonPath + " " + pythonModule # + " -outputPath " + outputPath # + " -gamma " + str(gamma) # + " -param_r " + str(materialFunctionParameter[i][0]) # + " -param_h " + str(materialFunctionParameter[i][1]) # + " -param_omega_flat " + str(materialFunctionParameter[i][2]) # + " -param_omega_target " + str(materialFunctionParameter[i][3]) # + " -phase2_angle " + str(materialFunctionParameter[i][4]) # + " | tee " + LOGFILE, shell=True) p.wait() # wait processList.append(p) exit_codes = [p.wait() for p in processList] # --------------------------------------------------- # wait here for the result to be available before continuing # thread.join() f = open(outputPath+"/parameter.txt", "w") f.write("r = "+str(materialFunctionParameter[i][0])+"\n") f.write("h = "+str(materialFunctionParameter[i][1])+"\n") f.write("omega_flat = "+str(materialFunctionParameter[i][2])+"\n") f.write("omega_target = "+str(materialFunctionParameter[i][3])+"\n") f.write("param_beta = "+str(materialFunctionParameter[i][5])+"\n") f.close() #