def release_complex_code(code_strs, code_place, xde_dict, ges_dict): cplx_expr = code_strs.replace('Cplx_Asgn: ', '') left_vara, righ_expr = cplx_expr.split('=')[:2] left_vara, righ_expr = left_vara.strip(), righ_expr.strip().strip(';') # if complex expres is a tensor expres, make summation first if left_vara.find('_') != -1 \ or righ_expr.find('_') != -1 : expr_list = idx_summation(left_vara, righ_expr, xde_dict) for expres in expr_list: cplx_list = expres.split('=') cplx_objt = cmplx_expr(cplx_list[1]) for ri, cmplexpr in zip(['r', 'i'], cplx_objt.complex_list): ges_dict['code'][code_place] \ .append(f'$cc {cplx_list[0]}{ri}={cmplexpr};\n') else: cplx_objt = cmplx_expr(righ_expr) for ri, cmplexpr in zip(['r', 'i'], cplx_objt.complex_list): ges_dict['code'][code_place] \ .append(f'$cc {left_vara}{ri}={cmplexpr};\n')
def release_code(xde_lists,code_place,pfelacpath,code_use_dict): for code_strs in xde_lists['code'][code_place]: regxrp = regx.search(r'Insr|Tnsr|Cplx|Oprt|Func',code_strs,regx.I) if regxrp == None: code_use_dict[code_place].append(code_strs+'\n') else: # Insert C code if regxrp.group() == 'Insr': code_use_dict[code_place].append(code_strs.replace('Insr_Code:','$cc')+'\n') # Tensor expres summation elif regxrp.group() == 'Tnsr': vect_expr = code_strs.replace('Tnsr_Asgn: ','') left_vara, righ_expr = vect_expr.split('=')[:2] left_vara, righ_expr = left_vara.strip(), righ_expr.strip().strip(';') expr_list = idx_summation(left_vara, righ_expr, xde_lists) for expres in expr_list: code_use_dict[code_place].append('$cc '+expres+';\n') # complex expres expansion elif regxrp.group() == 'Cplx': cplx_expr = code_strs.replace('Cplx_Asgn: ','') left_vara, righ_expr = cplx_expr.split('=')[:2] left_vara, righ_expr = left_vara.strip(), righ_expr.strip().strip(';') # if complex expres is a tensor expres, make summation first if left_vara.find('_') != -1 \ or righ_expr.find('_') != -1 : expr_list = idx_summation(left_vara, righ_expr, xde_lists) for expres in expr_list: cplx_list = expres.split('=') cplx_objt = cmplx_expr(cplx_list[1]) for ri,cmplexpr in zip(['r','i'], cplx_objt.complex_list): code_use_dict[code_place] \ .append('$cc {}{}={};\n'.format(cplx_list[0],ri,cmplexpr)) else: cplx_objt = cmplx_expr(righ_expr) for ri,cmplexpr in zip(['r','i'], cplx_objt.complex_list): code_use_dict[code_place] \ .append('$cc {}{}={};\n'.format(left_vara,ri,cmplexpr)) # the operator resault assignment elif regxrp.group() == 'Oprt': path_oprt = pfelacpath+'ges/pde.lib' file_oprt = open(path_oprt, mode='r') oprt_expr = code_strs.replace('Oprt_Asgn: ','') # singularity and volume operators for oprt_key in ['singular','vol']: if oprt_expr.find(oprt_key) != -1: oprt_strs, oprt_find = '', 0 for line in file_oprt.readlines(): oprt_start_file = regx.match('sub '+oprt_expr,line,regx.I) oprt_end_file = regx.match('end '+oprt_expr,line,regx.I) if oprt_start_file != None: oprt_find = 1 continue if oprt_end_file != None: oprt_find = 0 continue if oprt_find == 1: oprt_strs+=line xde_lists[oprt_key] = oprt_strs # other operators as grad, div, curl... if oprt_expr.find('singular') == -1 \ and oprt_expr.find('vol') == -1 : # split aa=grad.xy(x,y,u) to aa, grad.xy, [x,y,u] left_vara, righ_expr = oprt_expr.split('=')[:2] oprt_name, oprt_vars = righ_expr.split('(')[:2] oprt_vars = oprt_vars.rstrip(')').split(',') temp_vars = [] # expand vector variable list [x_i,a_i...] --> [x,y,z,a1,a2,...] for vara in oprt_vars: if vara.count('_') == 0: temp_vars.append(vara) elif vara.count('_') == 1: vect_var = vara.split('_')[0] temp_list = xde_lists['vect'][vect_var].copy() temp_list.pop(0) temp_vars += temp_list elif vara.count('_') == 2: matr_var = vara.split('_')[0] temp_list = xde_lists['matrix'][matr_var].copy() temp_list.pop(0) temp_list.pop(0) for matr_row in temp_list: temp_vars += matr_row oprt_vars = temp_vars.copy() oprt_strs,oprt_find = '',0 # find operator in pde.lib for line in file_oprt.readlines(): oprt_start_file = regx.search('sub '+oprt_name,line,regx.I) oprt_end_file = regx.search('end '+oprt_name,line,regx.I) if oprt_start_file != None: oprt_find = 1 # find variables of operator in pde.lib temp_vars = line.split('(')[1].rstrip().rstrip(')').split(',').copy() continue if oprt_end_file != None: oprt_find = 0 continue if oprt_find == 1: oprt_strs+=line # replace default variable by operator variable if len(oprt_vars) == len(temp_vars): for oprt_var, temp_var in zip(oprt_vars, temp_vars): oprt_strs = oprt_strs.replace(temp_var,oprt_var) # assign to a temporary list type of 'fvect' or 'fmatr' # used for derivative of 'disp' variables in Func_Asgn step # Oprt_Asgn: [a] = opr(*,*) ----------- @L opr f a * * if left_vara[0] == '[' \ and left_vara[-1] == ']' : expr_list = oprt_strs.rstrip().split('\n') if left_vara.count('_') == 0: # may be fault tackle expres = left_vara.lstrip('[').rstrip(']') + '=' for strs in expr_list: expres += strs code_use_dict[code_place].append(expres) elif left_vara.count('_') == 1: var = left_vara.lstrip('[').rstrip(']').split('_')[0] for ii in range(len(expr_list)): xde_lists['fvect'][var][ii+1] = expr_list[ii] elif left_vara.count('_') == 2: var = left_vara.lstrip('[').rstrip(']').split('_')[0] row, clm = list(map(int,xde_lists['fmatr'][var][:2])) for ii in range(row): for jj in range(clm): xde_lists['fmatr'][var][ii+2][jj]=expr_list[ii*row+jj] # assign to derivative of distributed known variables such as last step 'disp' resault # Oprt_Asgn: a = opr(*,*) ----------- @L opr [svm] a * * elif left_vara[0] != '[' \ and left_vara[-1] != ']' : oprt_strs = oprt_strs.replace('[','{').replace(']','}') expr_list = oprt_strs.rstrip().split('\n') if left_vara.count('_') == 0: expres = left_vara.lstrip('[').rstrip(']')+'=' for strs in expr_list: expres += strs code_use_dict[code_place].append('$cv '+expres) elif left_vara.count('_') == 1: expres = left_vara.lstrip('[').rstrip(']').split('_')[0] if len(xde_lists['vect'][expres]) == len(expr_list)+1: for ii in range(len(expr_list)): code_use_dict[code_place] \ .append('$cv '+ xde_lists['vect'][expres][ii+1] + '=' + expr_list[ii]) elif left_vara.count('_') == 2: expres = left_vara.lstrip('[').rstrip(']').split('_')[0] matr_len = int(xde_lists['matrix'][expres][0]) \ * int(xde_lists['matrix'][expres][1]) temp_list = xde_lists['matrix'][expres].copy() temp_list.pop(0) temp_list.pop(0) if matr_len == len(expr_list): ii = 0 for lists in temp_list: for strs in lists: code_use_dict[code_place].append('$cv '+strs+'='+expr_list[ii]+'\n') ii += 1 file_oprt.close() elif regxrp.group() == 'Func': tnsr_expr = code_strs.replace('Func_Asgn: ','') left_vara, righ_expr = tnsr_expr.split('=')[:2] left_vara, righ_expr = left_vara.strip(), righ_expr.strip() # assign the temporary list type of 'fvect' or 'fmatr' # to the list type of 'vect' or 'matrix' if left_vara[0] != '[' \ and left_vara[-1] != ']' : # Func_Asgn: a = b[*,*] --------- @W a b * * if regx.search(r'[a-z]+[0-9a-z]*\[([0-9],)*[0-9]?\]',righ_expr,regx.I) != None \ and righ_expr[-1] == ']': # read right variable index list righ_vara = righ_expr.split('[')[0] if righ_expr[-1] == ']' and righ_expr[-2] == '[': if 'fvect' in xde_lists \ and righ_vara in xde_lists['fvect']: righ_idxs = list(range(int(xde_lists['fvect'][righ_vara][0]))) elif 'fmatr' in xde_lists \ and righ_vara in xde_lists['fmatr']: righ_idxs = list(range(int(xde_lists['fvect'][righ_vara][0]) \ *int(xde_lists['fvect'][righ_vara][1]))) righ_idxs = [x + 1 for x in righ_idxs] else: righ_idxs = righ_expr.split('[')[1].rstrip(']').split(',') if left_vara.count('_') == 0: expres = left_vara + '=' if 'fvect' in xde_lists \ and righ_vara in xde_lists['fvect']: for idx in righ_idxs: expres += xde_lists['fvect'][righ_vara][int(idx)] elif 'fmatr' in xde_lists \ and righ_vara in xde_lists['fmatr']: row, clm = list(map(int,xde_lists['fmatr'][righ_vara][:2])) fmatr = xde_lists['fmatr'][righ_vara][2:len(xde_lists['fmatr'][righ_vara])] for idx in righ_idxs: expres += fmatr[math.ceil(int(idx)/clm)-1][int(idx)%clm-1] code_use_dict[code_place].append(expres+'\n\n') elif left_vara.count('_') == 1: left_name = left_vara.split('_')[0] expr_list = [] temp_list = xde_lists['vect'][left_name].copy() temp_list.pop(0) if len(temp_list) == len(righ_idxs): if 'fvect' in xde_lists \ and righ_vara in xde_lists['fvect']: for vara,idx in zip(temp_list,righ_idxs): expr_list.append(vara+'='+xde_lists['fvect'][righ_vara][int(idx)]+'\n\n') elif 'fmatr' in xde_lists \ and righ_vara in xde_lists['fmatr']: row,clm = list(map(int,xde_lists['fmatr'][righ_vara][:2])) fmatr = xde_lists['fmatr'][righ_vara][2:len(xde_lists['fmatr'][righ_vara])] for vara,idx in zip(temp_list,righ_idxs): expr_list.append(vara+'='+fmatr[math.ceil(int(idx)/clm)-1][int(idx)%clm-1]+'\n\n') code_use_dict[code_place] += expr_list elif left_vara.count('_') == 2: left_name = left_vara.split('_')[0] expr_list = [] matr_len = int(xde_lists['matrix'][left_name][0]) \ * int(xde_lists['matrix'][left_name][1]) temp_list = xde_lists['matrix'][left_name].copy() temp_list.pop(0) temp_list.pop(0) if matr_len == len(righ_idxs): if 'fvect' in xde_lists \ and righ_vara in xde_lists['fvect']: idx = 0 for lists in temp_list: for vara in lists: idx+=1 expr_list.append(vara+'='+xde_lists['fvect'][righ_vara][idx]+'\n\n') elif 'fmatr' in xde_lists \ and righ_vara in xde_lists['fmatr']: fmatr = xde_lists['fmatr'][righ_vara][2:len(xde_lists['fmatr'][righ_vara])] i = 0 for matr_row in temp_list: for matr_vara in matr_row: idx = righ_idxs[i] expr_list.append(matr_vara+'='+fmatr[math.ceil(int(idx)/clm)-1][int(idx)%clm-1]+'\n\n') i += 1 code_use_dict[code_place] += expr_list # assign the temporary list type of 'fvect' or 'fmatr' # to the list type of 'fvect' or 'fmatr' elif left_vara[0] == '[' \ and left_vara[-1] == ']' : left_vara = left_vara.lstrip('[').rstrip(']') # Func_Asgn: [a] = b[*,*] --------- @S a b * * if regx.search(r'[a-z]+[0-9a-z]*\[([0-9],)*[0-9]?\]',righ_expr,regx.I) != None \ and righ_expr[-1] == ']': # read right variable index list righ_vara = righ_expr.split('[')[0] if righ_expr[-1] == ']' and righ_expr[-2] == '[': if 'fvect' in xde_lists \ and righ_vara in xde_lists['fvect']: righ_idxs = list(range(int(xde_lists['fvect'][righ_vara][0]))) elif 'fmatr' in xde_lists \ and righ_vara in xde_lists['fmatr']: righ_idxs = list(range(int(xde_lists['fvect'][righ_vara][0]) \ *int(xde_lists['fvect'][righ_vara][1]))) righ_idxs = [x + 1 for x in righ_idxs] else: righ_idxs = righ_expr.split('[')[1].rstrip(']').split(',') if left_vara.count('_') == 0: expres = left_vara + '=' if 'fvect' in xde_lists \ and righ_vara in xde_lists['fvect']: for idx in righ_idxs: expres += xde_lists['fvect'][righ_vara][int(idx)] elif 'fmatr' in xde_lists \ and righ_vara in xde_lists['fmatr']: row,clm = list(map(int,xde_lists['fmatr'][righ_vara][:2])) fmatr = xde_lists['fmatr'][righ_vara][2:len(xde_lists['fmatr'][righ_vara])] for idx in righ_idxs: expres += fmatr[math.ceil(int(idx)/clm)-1][int(idx)%clm-1] code_use_dict[code_place].append(expres+'\n\n') elif left_vara.count('_') == 1: left_name = left_vara.split('_')[0] vect_len = len(xde_lists['fvect'][left_name]) if vect_len == len(righ_idxs) + 1: if 'fvect' in xde_lists \ and righ_vara in xde_lists['fvect']: for idx,ii in zip(righ_idxs,range(vect_len)): xde_lists['fvect'][left_name][ii+1] = \ xde_lists['fvect'][righ_vara][int(idx)] elif 'fmatr' in xde_lists \ and righ_vara in xde_lists['fmatr']: row, clm = list(map(int,xde_lists['fmatr'][righ_vara][:2])) for idx,ii in zip(righ_idxs,range(vect_len)): xde_lists['fvect'][left_name][ii+1] = \ xde_lists['fmatr'][righ_vara][math.ceil(int(idx)/clm)+1][int(idx)%clm-1] elif left_vara.count('_') == 2: left_name = left_vara.split('_')[0] expr_list = [] matr_len = int(xde_lists['fmatr'][left_name][0]) \ * int(xde_lists['fmatr'][left_name][1]) if matr_len == len(righ_idxs): if 'fvect' in xde_lists \ and righ_vara in xde_lists['fvect']: for ii,idx in zip(range(matr_len),righ_idxs): xde_lists['fmatr'][left_name][math.ceil(ii/clm)+1][ii%clm-1] = \ xde_lists['fvect'][righ_vara][int(idx)] elif 'fmatr' in xde_lists \ and righ_vara in xde_lists['fmatr']: for ii,idx in zip(range(matr_len),righ_idxs): xde_lists['fmatr'][left_name][math.ceil(ii/clm)+1][ii%clm-1] = \ xde_lists['fmatr'][righ_vara][math.ceil(int(idx)/clm)+1][int(idx)%clm-1] else: righ_expr = righ_expr.replace('[','').replace(']','') expr_list = idx_summation(left_vara,righ_expr,xde_lists) if left_vara.count('_') == 1: left_vara = left_vara.split('_')[0] row = int(xde_lists['fvect'][left_vara][0]) for ii in range(row): xde_lists['fvect'][left_vara][ii+1] = \ expr_list[ii].split('=')[1].replace('++','+').replace('-+','-') elif left_vara.count('_') == 2: left_vara = left_vara.split('_')[0] row,clm = list(map(int,xde_lists['fmatr'][left_vara][:2])) for ii in range(row): for jj in range(clm): xde_lists['fmatr'][left_vara][ii+2][jj] = \ expr_list[ii*row+jj].split('=')[1].replace('++','+').replace('-+','-')