예제 #1
0
def release_tensor_code(code_strs, code_place, xde_lists, code_use_dict):
    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')
예제 #2
0
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')
예제 #3
0
def write_load(xde_lists, gesfile):
    gesfile.write('\n')
    left_vara = 'load'
    righ_expr = ''.join(xde_lists['load'])
    expr_list = idx_summation(left_vara, righ_expr, xde_lists)
    expr_list = split_bracket_expr(expr_list[0])
    for strs in expr_list:
        if strs == 'load=':
            gesfile.write(strs)
        else:
            gesfile.write(strs + '\n')


# end write_load()
예제 #4
0
def release_tensor_code(code_strs, code_place, xde_dict, ges_dict):

    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_dict)

    for expres in expr_list:
        if  expres.find('{') != -1 \
        and expres.find('}') != -1:
            ges_dict['code'][code_place].append('$cv ' + expres + ';\n')
        else:
            ges_dict['code'][code_place].append('$cc ' + expres + ';\n')
예제 #5
0
def write_load(xde_lists, gesfile):

    gesfile.write('\n')
    left_vara = 'load'
    righ_expr = ''.join(xde_lists['load'])
    expr_list = idx_summation(left_vara, righ_expr, xde_lists)
    expr_list = split_bracket_expr(expr_list[0])
    gesfile.write(expr_list[0])

    for strs in expr_list[1:]:

        if 'cmplx_tag' in xde_lists and xde_lists['cmplx_tag'] == 1:

            weak_item = regx.search(r'\[\w+\]', strs, regx.I)
            cplx_item = regx.search(r'\(?\|[+-]?\w+\;[+-]?\w+\|\)?', strs,
                                    regx.I)

            if weak_item != None:
                weak_item = weak_item.group()

            if cplx_item != None:
                cplx_item = cplx_item.group()

            weak_var = weak_item.lstrip('[').rstrip(']')
            cplx_real_var, cplx_imag_var = cplx_item.lstrip('(').rstrip(
                ')').strip('|').split(';')

            weak_list = [weak_var + 'r', weak_var + 'i']
            cplx_list = [cplx_real_var, cplx_imag_var]

            for i in range(2):

                if regx.match(r'[+-]?0(?:\.0*)?|[+-]?\.0+', cplx_list[i] ,regx.I) != None \
                and cplx_list[i][-1] == '0' :
                    continue

                gesfile.write(strs.replace(weak_item, f'[{weak_list[i]}]') \
                                  .replace(cplx_item, f'({cplx_list[i]})')+'\n')

        else:
            gesfile.write(strs + '\n')


# end write_load()
예제 #6
0
def write_weak(weak, code_use_dict, xde_lists, gesfile):
    if xde_lists[weak][0].lower() != 'null' or weak in code_use_dict:
        gesfile.write('\n{}\n'.format(weak))
    else:
        return

    if weak in code_use_dict:
        for strs in code_use_dict[weak]:
            gesfile.write(strs)

    if xde_lists[weak][0] == 'dist':
        left_vara = xde_lists[weak][0]
        righ_expr = ''
        for ii in range(1, len(xde_lists[weak])):
            righ_expr += xde_lists[weak][ii]
        expr_list = idx_summation(left_vara, righ_expr, xde_lists)
        expr_list = split_bracket_expr(expr_list[0])
        for strs in expr_list:
            if strs == 'dist=':
                gesfile.write(strs)
            else:
                gesfile.write(strs + '\n')

    elif xde_lists[weak][0] == 'lump':
        if len(xde_lists[weak]) == 1:
            xde_lists[weak].append('1.0')

        lump_expr = {}
        if len(xde_lists[weak]) == 2: pass

        gesfile.write('lump =\n')
        for shaps in xde_lists['shap']:
            nodn = regx.search(r'\d+', shaps, regx.I).group()
            for vara in xde_lists['shap'][shaps]:
                for ii in range(int(nodn)):
                    gesfile.write('+[{}]{}{}\n' \
                        .format(xde_lists[weak][1], vara, ii+1))
예제 #7
0
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('-+','-')
예제 #8
0
def xde2ges(gesname, coor_type, xde_lists, list_addr, gesfile):

    # 0 prepare
    ges_shap_type = regx.search(r'[ltqwc][1-9]+',gesname,regx.I).group()
    ges_gaus_type = regx.search(r'g[1-9]+',gesname,regx.I)
    if  ges_gaus_type != None:
        ges_gaus_type = ges_gaus_type.group()

    dim = regx.search(r'[1-9]+',coor_type,regx.I).group()
    axi = coor_type.split('d')[1]

    ges_shap_nodn = regx.search(r'[1-9]+',ges_shap_type,regx.I).group()
    ges_shap_form = ges_shap_type[0]

    pfelacpath = os.environ['pfelacpath']

    code_use_dict = {} # use to deal with @L, @A, vol, singular, ...
    for code_key in ['BFmate','AFmate','func','stif','mass','damp']:
        if code_key in xde_lists['code']:
            code_use_dict[code_key] = []
            release_code(xde_lists,code_key,pfelacpath,code_use_dict)

    gesfile.write(gesname+'\ndefi\n')
    base_shap_type = list(xde_lists['shap'].keys())[0]
    base_shap_nodn = regx.search(r'[1-9]+',base_shap_type,regx.I).group()
    base_shap_form = base_shap_type[0]

    # 1 write disp and var declare
    if 'disp' in xde_lists:

        # 1.1 write disp declare
        gesfile.write('disp ')
        for strs in xde_lists['disp']:
            gesfile.write(strs+',')
        gesfile.write('\n')

        # 1.2 parsing var
        var_dict = {}
        for shap in xde_lists['shap'].keys():
            nodn = int(regx.search(r'[1-9]+', shap, regx.I).group())

            for var in xde_lists['shap'][shap]:
                var_dict[var] = [var+str(ii+1) for ii in range(nodn)]

        # 1.3 write var declare
        i = 0
        gesfile.write('var')
        for nodi in range(int(base_shap_nodn)):
            for strs in xde_lists['disp']:
                if nodi >= len(var_dict[strs]):
                    continue
                gesfile.write(' '+var_dict[strs][nodi])
                i += 1
                if i == 10:
                    gesfile.write('\nvar')
                    i = 0
        gesfile.write('\n')

    # 2 write refc, coor and coef declare
    if 'coor' in xde_lists:
        gesfile.write('refc ')
        for strs in xde_lists['coor']:
            gesfile.write('r'+strs+',')
        gesfile.write('\ncoor ')
        for strs in xde_lists['coor']:
            gesfile.write(strs+',')
        gesfile.write('\n')

    if 'coef' in xde_lists:
        gesfile.write('coef ')
        for strs in xde_lists['coef']:
            gesfile.write(strs+',')
        gesfile.write('\n')

    # 3 write func declare
    if 'func' in xde_lists:
        gesfile.write('func = ')
        for strs in xde_lists['func']:
            gesfile.write(strs+',')
        gesfile.write('\n')

    # 4 write dord and node declare
    if 'disp' in xde_lists:
        gesfile.write('dord ')
        for strs in xde_lists['disp']:
            gesfile.write('1'+',')
        gesfile.write('\n')

        gesfile.write('node '+str(base_shap_nodn)+'\n')

    # 5 write code before mate declaration
    if 'BFmate' in code_use_dict:
        for strs in code_use_dict['BFmate']:
            gesfile.write(strs)

    # 6 write mate line
    if 'mate' in xde_lists:
        gesfile.write('mate')
        for var in xde_lists['mate']['default'].keys():
            gesfile.write(' '+var)
        for var in xde_lists['mate']['default'].keys():
            gesfile.write(' '+xde_lists['mate']['default'][var])
        gesfile.write('\n')

    # 7 write code after mate declaration
    if 'AFmate' in code_use_dict:
        for strs in code_use_dict['AFmate']:
            gesfile.write(strs)

    # 8 write 'singular' operator declaration
    if 'singular' in xde_lists:
        gesfile.write(xde_lists['singular'])

    # 9 write shap and tran paragraph
    if 'shap' in xde_lists:
        gesfile.write('\nshap\n')
        geslib_coor = ['x','y','z']
        base_shap_strs = ''

        # 9.1 write shap
        for shap in xde_lists['shap'].keys():

            shap_func = 'd' + dim + shap + '.sub'
            path_shap = pfelacpath + 'ges/ges.lib'
            file_shap = open(path_shap, mode='r')
            shap_find, shap_strs = 0, ''

            # 9.1.1 find shap function in ges.lib
            for line in file_shap.readlines():
                shap_start_file = regx.search('sub '+shap_func,line,regx.I)
                shap_end_file   = regx.search('end '+shap_func,line,regx.I)
                if shap_start_file != None:
                    shap_find = 1
                    continue
                if shap_end_file   != None:
                    shap_find = 0
                    continue
                if shap_find == 1:
                    shap_strs += line
            file_shap.close()

            # note: save the base shap function for mix element when write tran
            if shap == base_shap_type:
                base_shap_strs = shap_strs

            # 9.1.2 replace shap func's coor by xde's coor
            for coor_i, coor_str in enumerate(xde_lists['coor']):
                shap_strs = shap_strs.replace(geslib_coor[coor_i],'r'+coor_str)

            # 9.1.3 replace shap func's disp by xde's disp and write
            for shap_var in xde_lists['shap'][shap]:
                temp_strs = shap_strs.replace('u',shap_var)
                gesfile.write(shap_var+'=\n')
                gesfile.write(temp_strs)
                gesfile.write('\n')

        # 9.2 write tran
        gesfile.write('tran\n')
        shap_expr_list = base_shap_strs.split('\n')
        shap_expr_list.remove('')
        tran_expr_list = []

        # 9.2.1 add '()'
        for shap_expr in shap_expr_list:
            shap_var,shap_exp = shap_expr.split('=')[:2]
            shap_num = regx.search(r'[0-9]+', shap_var, regx.I).group()
            shap_var = shap_var.replace(shap_num, '('+shap_num+')')
            tran_expr_list.append(shap_var + '=' + shap_exp)

        # 9.2.2 replace shap func's coor by xde's coor
        for coor_i, coor_str in enumerate(xde_lists['coor']) :
            for ii in range(len(tran_expr_list)):
                tran_expr_list[ii] = \
                    tran_expr_list[ii].replace(geslib_coor[coor_i],'r'+coor_str)

        # 9.2.3 replace shap func's disp by by xde's coor and write
        for coor_str in xde_lists['coor']:
            gesfile.write(coor_str + '=\n')
            for tran_expr in tran_expr_list:
                gesfile.write(tran_expr.replace('u', coor_str) + '\n')
            gesfile.write('\n')

    # 9.3 write coef shap
    if 'coef_shap' in xde_lists:
        gesfile.write('coef\n')
        for shap in xde_lists['coef_shap'].keys():

            shap_func = 'd' + dim + shap + '.sub'
            path_shap = pfelacpath + 'ges/ges.lib'
            file_shap = open(path_shap, mode='r')
            shap_find, shap_strs = 0, ''

            # 9.3.1 find shap function in ges.lib
            for line in file_shap.readlines():
                shap_start_file = regx.search('sub '+shap_func,line,regx.I)
                shap_end_file   = regx.search('end '+shap_func,line,regx.I)
                if shap_start_file != None:
                    shap_find = 1
                    continue
                if shap_end_file     != None:
                    shap_find = 0
                    continue
                if shap_find == 1:
                    shap_strs += line
            file_shap.close()

            # 9.2.1 add '()'
            shap_expr_list = shap_strs.split('\n')
            shap_expr_list.remove('')
            coef_expr_list = []

            for shap_expr in shap_expr_list:
                shap_var, shap_exp  = shap_expr.split('=')[:2]
                shap_num  = regx.search(r'[0-9]+', shap_var, regx.I).group()
                shap_var  = shap_var.replace(shap_num, '('+shap_num+')')
                coef_expr_list.append(shap_var + '=' + shap_exp)

            shap_strs = '\n'.join(coef_expr_list)+'\n'

            # 9.3.2 replace shap func's coor by xde's coor
            for coor_i,coor_str in enumerate(xde_lists['coor']):
                shap_strs = shap_strs.replace(geslib_coor[coor_i],'r'+coor_str)

            # 9.3.3 replace shap func's disp by xde's disp and write
            for shap_var in xde_lists['coef_shap'][shap]:
                temp_string = shap_strs.replace('u', shap_var)
                gesfile.write(shap_var + '=\n')
                gesfile.write(temp_string)
                gesfile.write('\n')

    # 9 write gaus paragraph
    if 'gaus' in xde_lists:

        # 9.1 Gaussian integral
        if xde_lists['gaus'][0] == 'g':

            gaus_degree = regx.search(r'[0-9]+',xde_lists['gaus'],regx.I).group()

            # 9.1.1 line square or cube shap
            if ges_shap_type[0].lower() in ['l','q','c']:

                path_gaus = pfelacpath+'ges/gaus.pnt'
                file_gaus = open(path_gaus, mode='r')
                gaus_find, gaus_axis, gaus_weit = 0, [], []

                # 9.1.1.1 read gaus axis and weight in gaus.pnt
                for line in file_gaus.readlines():
                    gaus_start_file = regx.search('n='+gaus_degree,line,regx.I)
                    if gaus_start_file != None:
                        gaus_find = 1
                        continue
                    if gaus_find == 1 and line=='\n':
                        gaus_find = 0
                        continue
                    if gaus_find == 1:
                        gaus_strs = line.split()
                        if  gaus_strs[0][0] not in ['-' ,'+'] :
                            gaus_strs[0] = ' '+gaus_strs[0]
                        gaus_axis.append(gaus_strs[0])
                        gaus_weit.append(gaus_strs[1])

                file_gaus.close()

                # 9.1.1.2 write line square or cube's gaussian integra
                gesfile.write('gaus = '+str(len(gaus_weit)**int(dim))+'\n')

                if  ges_shap_type[0].lower()=='l':
                    for axis_i in range(len(gaus_axis)):
                        gesfile.write(gaus_axis[axis_i]+' ' \
                                     +gaus_weit[axis_i]+'\n')

                elif ges_shap_type[0].lower()=='q':
                    for axis_i in range(len(gaus_axis)):
                        for axis_j in range(len(gaus_axis)):
                            weight = float(gaus_weit[axis_i]) \
                                    *float(gaus_weit[axis_j])
                            gesfile.write (gaus_axis[axis_i]+' ' \
                                          +gaus_axis[axis_j]+' ' \
                                          +str(weight)+'\n')
                                        
                elif ges_shap_type[0].lower()=='c':
                    for axis_i in range(len(gaus_axis)):
                        for axis_j in range(len(gaus_axis)):
                            for axis_k in range(len(gaus_axis)):
                                weight = float(gaus_weit[axis_i]) \
                                        *float(gaus_weit[axis_j]) \
                                        *float(gaus_weit[axis_k])
                                gesfile.write (gaus_axis[axis_i]+' ' \
                                              +gaus_axis[axis_j]+' ' \
                                              +gaus_axis[axis_k]+' ' \
                                              +str(weight)+'\n')

            # 9.1.2 triangle shap
            elif ges_shap_type[0].lower()=='t':

                path_gaus = pfelacpath+'ges/gaust.pnt'
                file_gaus = open(path_gaus, mode='r')
                gaus_find, gaus_strs = 0, ''

                # 9.1.2.1 tackle the gaussian degree
                if gaus_degree == '6':
                    gaus_degree = '5'
                elif int(gaus_degree) > 12 and int(gaus_degree) < 17:
                    gaus_degree = '12'
                elif int(gaus_degree) > 17:
                    gaus_degree = '17'

                # 9.1.2.2 read gaus axis and weight in gaust.pnt and write
                for line in file_gaus.readlines():
                    gaus_start_file = regx.search('P'+gaus_degree,line,regx.I)
                    if gaus_start_file != None:
                        gaus_find = 1
                        continue
                    if gaus_find == 1 and line=='\n':
                        gaus_find = 0
                        continue
                    if gaus_find == 1:
                        gaus_strs += line

                file_gaus.close()
                gesfile.write(gaus_strs)

            # 9.1.3 tetrahedron shap
            elif ges_shap_type[0].lower()=='w':

                path_gaus = pfelacpath+'ges/gausw.pnt'
                file_gaus = open(path_gaus, mode='r')
                gaus_find, gaus_strs = 0, ''

                # 9.1.3.1 tackle the gaussian degree
                if gaus_degree == '4':
                    gaus_degree = '3'
                elif gaus_degree == '6':
                    gaus_degree = '5'
                elif int(gaus_degree) > 7:
                    gaus_degree = '7'

                # 9.1.2.2 read gaus axis and weight in gausw.pnt and write
                for line in file_gaus.readlines():
                    gaus_start_file = regx.search('P'+gaus_degree,line,regx.I)
                    if gaus_start_file != None:
                        gaus_find = 1
                        continue
                    if gaus_find == 1 and line=='\n':
                        gaus_find = 0
                        continue
                    if gaus_find == 1:
                        gaus_strs += line

                file_gaus.close()
                gesfile.write(gaus_strs)

            else: pass

        # 9.2 node integral
        else:
            path_gaus = pfelacpath+'ges/ges.lib'
            file_gaus = open(path_gaus, mode='r')
            gaus_find, gaus_strs = 0, ''

            # 9.2.1 read gaus axis and weight in ges.lib and write
            for line in file_gaus.readlines():
                gaus_name = ' d'+ dim + xde_lists['gaus']+'.gau'
                gaus_start_file = regx.search('sub' + gaus_name, line,regx.I)
                gaus_end_file   = regx.search('end' + gaus_name, line,regx.I)
                if gaus_start_file != None:
                    gaus_find = 1
                    continue
                if gaus_end_file != None:
                    gaus_find = 0
                    continue
                if gaus_find == 1:
                    gaus_strs += line

            file_gaus.close()
            gesfile.write(gaus_strs)

    # 10 write func paragraph
    if 'func' in code_use_dict \
    or 'vol'  in xde_lists :
        gesfile.write('\nfunc\n')
        if 'vol'  in xde_lists :
            gesfile.write(xde_lists['vol'])
        if 'func' in code_use_dict:
            for strs in code_use_dict['func']:
                if strs[0] != '$':
                    gesfile.write(strs)
                else:
                    if strs.find('('):
                        expr_objt = expr(strs.split('=')[1])
                        righ_expr = expr_objt.bracket_expand(expr_objt.expr_head)
                        gesfile.write(strs.split('=')[0]+'='+righ_expr)
                    else: gesfile.write(strs)


    # 11 write stif, mass, damp paragraph
    for weak in ['stif', 'mass', 'damp']:
        if weak in xde_lists:
            gesfile.write('\n{}\n'.format(weak))
            if weak in code_use_dict:
                for strs in code_use_dict[weak]:
                    gesfile.write(strs)
            if xde_lists[weak][0] == 'dist':
                left_vara = xde_lists[weak][0]
                righ_expr = ''
                for ii in range(1,len(xde_lists[weak])):
                    righ_expr += xde_lists[weak][ii]

                expr_list = idx_summation(left_vara,righ_expr,xde_lists)
                expr_list = split_expr(expr_list[0])
                for strs in expr_list:
                    if strs == 'dist=':
                        gesfile.write(strs)
                    else:
                        gesfile.write(strs+'\n')
            elif xde_lists[weak][0] == 'lump':
                if len(xde_lists[weak]) == 1:
                    xde_lists[weak].append('1.0')
                gesfile.write('lump =\n')
                for shaps in xde_lists['shap']:
                    nodn = regx.search(r'\d+',shaps,regx.I).group()
                    for vara in xde_lists['shap'][shaps]:
                        for ii in range(int(nodn)):
                            gesfile.write('+[{}]{}{}\n' \
                                .format(xde_lists[weak][1], vara, ii+1))

    # 14 write load paragraph
    gesfile.write('\nload\n')
    if 'load' in xde_lists:
        left_vara = 'load'
        righ_expr = ''.join(xde_lists['load'])
        expr_list = idx_summation(left_vara,righ_expr,xde_lists)
        expr_list = split_expr(expr_list[0])
        for strs in expr_list:
            if strs == 'load=':
                gesfile.write(strs)
            else:
                gesfile.write(strs+'\n')
    else:
        print('error: no load declared')

    gesfile.write('\nend')

    gesfile.close()

    return False
예제 #9
0
def parse_weak(weak, xde_dict, ges_dict):

    if xde_dict[weak][0].lower() != 'null' \
    or weak in ges_dict['code']:
        ges_dict[weak] = []

    else:
        return

    if xde_dict[weak][0] == 'dist':

        left_vara = xde_dict[weak][0]
        righ_expr = ''

        for weak_strs in xde_dict[weak][1:]:
            righ_expr += weak_strs

        expr_list = idx_summation(left_vara, righ_expr, xde_dict)
        expr_list = split_bracket_expr(expr_list[0])
        ges_dict[weak].append(expr_list[0].replace('=', ''))

        for strs in expr_list[1:]:

            if 'cmplx_tag' in xde_dict and xde_dict['cmplx_tag'] == 1:

                weak_item = re.search(r'\[\w+\;\w+\]', strs, re.I)
                cplx_item = re.search(r'\(?\|[+-]?\w+\;[+-]?\w+\|\)?', strs,
                                      re.I)

                if weak_item != None:
                    weak_item = weak_item.group()
                if cplx_item != None:
                    cplx_item = cplx_item.group()

                weak_left_var, weak_righ_var = weak_item.lstrip('[').rstrip(
                    ']').split(';')
                cplx_real_var, cplx_imag_var = cplx_item.lstrip('(').rstrip(
                    ')').strip('|').split(';')

                weak_left_list = [
                    weak_left_var + 'r', weak_left_var + 'i',
                    weak_left_var + 'r', weak_left_var + 'i'
                ]
                weak_righ_list = [
                    weak_righ_var + 'r', weak_righ_var + 'i',
                    weak_righ_var + 'i', weak_righ_var + 'r'
                ]
                cplx_list = [
                    cplx_real_var, cplx_real_var, cplx_imag_var, cplx_imag_var
                ]

                if cplx_list[2][0] == '+':
                    cplx_list[2] = cplx_list[2].replace('+', '-')

                elif cplx_list[2][0] == '-':
                    cplx_list[2] = cplx_list[2].replace('-', '+')

                else:
                    cplx_list[2] = '-' + cplx_list[2]

                for i in range(4):
                    if re.match(r'[+-]?0(?:\.0*)?|[+-]?\.0+', cplx_list[i] ,re.I) != None \
                    and cplx_list[i][-1] == '0' :
                        continue

                    ges_dict[weak].append(strs.replace(weak_item, f'[{weak_left_list[i]};{weak_righ_list[i]}]') \
                                               .replace(cplx_item, f'({cplx_list[i]})') )

            else:
                ges_dict[weak].append(strs)

    elif xde_dict[weak][0] == 'lump':

        if len(xde_dict[weak]) == 1:
            xde_dict[weak].append('1.0')

        if len(xde_dict[weak]) == 2: pass

        ges_dict[weak].append('lump')
        for shaps in xde_dict['shap']:
            nodn = re.search(r'\d+', shaps, re.I).group()

            for vara in xde_dict['shap'][shaps]:
                for ii in range(int(nodn)):
                    ges_dict[weak].append(
                        f'+[{xde_dict[weak][1]}]{vara}{ii+1}')
예제 #10
0
def release_funcasgn_code(code_strs, code_place, xde_dict, ges_dict):

    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 re.search(r'[a-z]+[0-9a-z]*\[([0-9],)*[0-9]?\]',righ_expr,re.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_dict \
                and righ_vara in xde_dict['fvect']:
                    righ_idxs = list(
                        range(int(xde_dict['fvect'][righ_vara][0])))

                elif 'fmatr' in xde_dict \
                and righ_vara in xde_dict['fmatr']:
                    righ_idxs = list(range(int(xde_dict['fvect'][righ_vara][0]) \
                                          *int(xde_dict['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_dict \
                and righ_vara in xde_dict['fvect']:
                    for idx in righ_idxs:
                        expres += xde_dict['fvect'][righ_vara][int(idx)]

                elif 'fmatr' in xde_dict \
                and righ_vara in xde_dict['fmatr']:

                    row, clm = list(map(int, xde_dict['fmatr'][righ_vara][:2]))
                    fmatr = xde_dict['fmatr'][righ_vara][2:]

                    for idx in righ_idxs:
                        expres += fmatr[math.ceil(int(idx) / clm) -
                                        1][int(idx) % clm - 1]

                ges_dict['code'][code_place].append(expres + '\n\n')

            elif left_vara.count('_') == 1:

                left_name = left_vara.split('_')[0]
                expr_list = []
                temp_list = xde_dict['vect'][left_name][1:].copy()

                if len(temp_list) == len(righ_idxs):

                    if 'fvect' in xde_dict and righ_vara in xde_dict['fvect']:
                        for vara, idx in zip(temp_list, righ_idxs):
                            expr_list.append( vara + '=' + \
                                xde_dict['fvect'][righ_vara][int(idx)] + '\n\n')

                    elif 'fmatr' in xde_dict and righ_vara in xde_dict['fmatr']:

                        row, clm = list(
                            map(int, xde_dict['fmatr'][righ_vara][:2]))
                        fmatr = xde_dict['fmatr'][righ_vara][2:]

                        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')

                ges_dict['code'][code_place] += expr_list

            elif left_vara.count('_') == 2:

                left_name = left_vara.split('_')[0]
                expr_list = []
                matr_len = int(xde_dict['matrix'][left_name][0]) \
                         * int(xde_dict['matrix'][left_name][1])

                temp_list = xde_dict['matrix'][left_name][2:].copy()

                if matr_len == len(righ_idxs):

                    if 'fvect' in xde_dict \
                        and righ_vara in xde_dict['fvect']:

                        idx = 0
                        for lists in temp_list:
                            for vara in lists:
                                idx += 1
                                expr_list.append( vara + '=' + \
                                    xde_dict['fvect'][righ_vara][idx] + '\n\n')

                elif 'fmatr' in xde_dict and righ_vara in xde_dict['fmatr']:

                    row, clm = list(map(int, xde_dict['fmatr'][righ_vara][:2]))
                    fmatr = xde_dict['fmatr'][righ_vara][2:]

                    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

                ges_dict['code'][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 re.search(r'[a-z]+[0-9a-z]*\[([0-9],)*[0-9]?\]',righ_expr,re.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_dict \
                and righ_vara in xde_dict['fvect']:
                    righ_idxs = list(
                        range(int(xde_dict['fvect'][righ_vara][0])))

                elif 'fmatr' in xde_dict \
                and righ_vara in xde_dict['fmatr']:
                    righ_idxs = list(range(int(xde_dict['fvect'][righ_vara][0]) \
                                          *int(xde_dict['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_dict \
                and righ_vara in xde_dict['fvect']:
                    for idx in righ_idxs:
                        expres += xde_dict['fvect'][righ_vara][int(idx)]

                elif 'fmatr' in xde_dict \
                and righ_vara in xde_dict['fmatr']:

                    row, clm = list(map(int, xde_dict['fmatr'][righ_vara][:2]))
                    fmatr = xde_dict['fmatr'][righ_vara][2:]

                    for idx in righ_idxs:
                        expres += fmatr[math.ceil(int(idx) / clm) -
                                        1][int(idx) % clm - 1]

                ges_dict['code'][code_place].append(expres + '\n\n')

            elif left_vara.count('_') == 1:
                left_name = left_vara.split('_')[0]
                vect_len = len(xde_dict['fvect'][left_name])

                if vect_len == len(righ_idxs) + 1:

                    if   'fvect' in xde_dict \
                    and righ_vara in xde_dict['fvect']:

                        for idx, ii in zip(righ_idxs, range(vect_len)):
                            xde_dict['fvect'][left_name][ii+1] = \
                            xde_dict['fvect'][righ_vara][int(idx)]

                    elif 'fmatr' in xde_dict \
                    and righ_vara in xde_dict['fmatr']:

                        row, clm = list(
                            map(int, xde_dict['fmatr'][righ_vara][:2]))
                        for idx, ii in zip(righ_idxs, range(vect_len)):
                            xde_dict['fvect'][left_name][ii+1] = \
                            xde_dict['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_dict['fmatr'][left_name][0]) \
                         * int(xde_dict['fmatr'][left_name][1])
                lrow, lclm = list(map(int, xde_dict['fmatr'][left_name][:2]))

                if matr_len == len(righ_idxs):
                    if   'fvect' in xde_dict \
                    and righ_vara in xde_dict['fvect']:

                        for ii, idx in zip(range(matr_len), righ_idxs):
                            xde_dict['fmatr'][left_name][math.ceil(ii/lclm)+1][ii%lclm-1] = \
                                xde_dict['fvect'][righ_vara][int(idx)]

                    elif 'fmatr' in xde_dict \
                    and righ_vara in xde_dict['fmatr']:

                        row, clm = list(
                            map(int, xde_dict['fmatr'][righ_vara][:2]))
                        for ii, idx in zip(range(matr_len), righ_idxs):
                            xde_dict['fmatr'][left_name][math.ceil(ii/lclm)+1][ii%lclm-1] = \
                                xde_dict['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_dict)

            if left_vara.count('_') == 1:

                left_vara = left_vara.split('_')[0]
                row = int(xde_dict['fvect'][left_vara][0])

                for ii in range(row):
                    xde_dict['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_dict['fmatr'][left_vara][:2]))

                for ii in range(row):
                    for jj in range(clm):
                        xde_dict['fmatr'][left_vara][ii+2][jj] = \
                            expr_list[ii*row+jj].split('=')[1].replace('++','+').replace('-+','-')
예제 #11
0
def xde2ges(gesname, coortype, keywd_tag, xde_lists, list_addr, keyws_reg,
            file):

    # 0 prepare
    shap_tag = regx.search(r'[ltqwc][1-9]+', gesname, regx.I).group()
    gaus_tag = regx.search(r'g[1-9]+', gesname, regx.I)
    if gaus_tag != None:
        gaus_tag = gaus_tag.group()

    dim = regx.search(r'[1-9]+', coortype, regx.I).group()
    axi = coortype.split('d')[1]

    file.write(gesname + '\ndefi\n')
    pfelacpath = os.environ['pfelacpath']

    code_use_dict = {}  # use to deal with @L, @A, vol, singular, ...
    for code_key in ['BFmate', 'AFmate', 'func', 'stif', 'mass', 'damp']:
        if code_key in xde_lists['code']:
            code_use_dict[code_key] = []
            release_code(xde_lists, code_key, pfelacpath, code_use_dict)

    #for strs in code_use_dict.keys():
    #    print(code_use_dict[strs])

    # 1 write disp and var declare
    # 1.1 parsing var
    var_dict = {}
    if 'disp' in xde_lists:

        for shap_type in xde_lists['shap'].keys():

            nodn = int(regx.search(r'[1-9]+', shap_type, regx.I).group())

            for var in xde_lists['shap'][shap_type]:
                var_dict[var] = []
                for ii in range(nodn):
                    var_dict[var].append(var + str(ii + 1))

    # 1.2 write disp declare
    if 'disp' in xde_lists:
        file.write('disp ')
        for strs in xde_lists['disp']:
            file.write(strs + ',')
        file.write('\n')
        i = 0

    # 1.3 write var declare
    nodn = int(regx.search(r'[1-9]+', shap_tag, regx.I).group())
    if 'disp' in xde_lists:
        file.write('var')
        for nodi in range(nodn):
            for strs in xde_lists['disp']:
                if nodi >= len(var_dict[strs]):
                    continue
                file.write(' ' + var_dict[strs][nodi])
                i += 1
                if i == 10:
                    file.write('\nvar')
                    i = 0
        file.write('\n')

    # 2 write refc, coor and coef declare
    if 'coor' in xde_lists:
        file.write('refc ')
        for strs in xde_lists['coor']:
            file.write('r' + strs + ',')
        file.write('\n')
        file.write('coor ')
        for strs in xde_lists['coor']:
            file.write(strs + ',')
        file.write('\n')

    if 'coef' in xde_lists:
        file.write('coef ')
        for strs in xde_lists['coef']:
            file.write(strs + ',')
        file.write('\n')

    # 3 write func declare
    if 'func' in xde_lists:
        file.write('func = ')
        for strs in xde_lists['func']:
            file.write(strs + ',')
        file.write('\n')

    # 4 write dord declare
    if 'disp' in xde_lists:
        file.write('dord ')
        for strs in xde_lists['disp']:
            file.write('1' + ',')
        file.write('\n')

        file.write('node ' + str(nodn) + '\n')

    # 5 write code before mate declaration
    if 'BFmate' in code_use_dict:
        for strs in code_use_dict['BFmate']:
            file.write(strs)

    # 6 write mate line
    if 'mate' in xde_lists:
        file.write('mate')
        for var in xde_lists['mate']['default'].keys():
            file.write(' ' + var)
        for var in xde_lists['mate']['default'].keys():
            file.write(' ' + xde_lists['mate']['default'][var])
        file.write('\n')

    # 7 write code after mate declaration
    if 'AFmate' in code_use_dict:
        for strs in code_use_dict['AFmate']:
            file.write(strs)

    # 8 write 'singular' operator declaration
    if 'singular' in xde_lists:
        file.write(xde_lists['singular'])

    # 9 write shap and tran paragraph
    file.write('\nshap\n')
    if 'shap' in xde_lists:
        geslib_coor = ['x', 'y', 'z']
        main_shap_string = ''

        # 9.1 write shap
        for shap_type in xde_lists['shap'].keys():

            shap_func = 'd' + dim + shap_type + '.sub'
            path_shap = pfelacpath + 'ges/ges.lib'
            file_shap = open(path_shap, mode='r')
            shap_find = 0
            shap_string = ''

            # 9.1.1 find shap function in ges.lib
            for line in file_shap.readlines():
                shap_start_file = regx.search('sub ' + shap_func, line, regx.I)
                shap_end_file = regx.search('end ' + shap_func, line, regx.I)
                if shap_start_file != None:
                    shap_find = 1
                    continue
                if shap_end_file != None:
                    shap_find = 0
                    continue
                if shap_find == 1:
                    shap_string += line
            file_shap.close()

            # note: save the main shap function for mix element when write tran
            if shap_type == shap_tag:
                main_shap_string = shap_string

            # 9.1.2 replace shap func's coor by xde's coor
            coor_i = 0
            for strs in xde_lists['coor']:
                shap_string = shap_string.replace(geslib_coor[coor_i],
                                                  'r' + strs)
                coor_i += 1

            # 9.1.3 replace shap func's disp by xde's disp and write
            for strs in xde_lists['shap'][shap_type]:
                temp_string = shap_string.replace('u', strs)
                file.write(strs + '=\n')
                file.write(temp_string)
                file.write('\n')

        # 9.2 write tran
        file.write('tran\n')
        trans_list = main_shap_string.split('\n')
        trans_list.remove('')
        tran_list = []

        # 9.2.1 add '()'
        for strs in trans_list:
            temp_list = strs.split('=')
            temp_num = regx.search(r'[0-9]+', temp_list[0], regx.I).group()
            temp_list[0] = temp_list[0].replace(temp_num, '(' + temp_num + ')')
            tran_list.append(temp_list[0] + '=' + temp_list[1])

        # 9.2.2 replace shap func's coor by xde's coor
        coor_i = 0
        for coor_str in xde_lists['coor']:
            for ii in range(len(tran_list)):
                tran_list[ii] = tran_list[ii].replace(geslib_coor[coor_i],
                                                      'r' + coor_str)
            coor_i += 1

        # 9.2.3 replace shap func's disp by by xde's coor and write
        shap_i = 0
        for strs in xde_lists['coor']:
            file.write(strs + '=\n')
            for strss in tran_list:
                file.write(strss.replace('u', strs) + '\n')
            file.write('\n')
            shap_i += 1

    # 9.3 write coef shap
    if 'coef_shap' in xde_lists:
        file.write('coef\n')
        for shap_type in xde_lists['coef_shap'].keys():

            shap_func = 'd' + dim + shap_type + '.sub'
            path_shap = pfelacpath + 'ges/ges.lib'
            file_shap = open(path_shap, mode='r')
            shap_find = 0
            shap_string = ''

            # 9.3.1 find shap function in ges.lib
            for line in file_shap.readlines():
                shap_start_file = regx.search('sub ' + shap_func, line, regx.I)
                shap_end_file = regx.search('end ' + shap_func, line, regx.I)
                if shap_start_file != None:
                    shap_find = 1
                    continue
                if shap_end_file != None:
                    shap_find = 0
                    continue
                if shap_find == 1:
                    shap_string += line
            file_shap.close()

            # 9.2.1 add '()'
            tran_list = []
            for strs in shap_string.split('\n'):
                if strs != '':
                    temp_list = strs.split('=')
                    temp_num = regx.search(r'[0-9]+', temp_list[0],
                                           regx.I).group()
                    temp_list[0] = temp_list[0].replace(
                        temp_num, '(' + temp_num + ')')
                    tran_list.append(temp_list[0] + '=' + temp_list[1])

            shap_string = ''
            for strs in tran_list:
                shap_string += strs + '\n'

            # 9.3.2 replace shap func's coor by xde's coor
            coor_i = 0
            for strs in xde_lists['coor']:
                shap_string = shap_string.replace(geslib_coor[coor_i],
                                                  'r' + strs)
                coor_i += 1

            # 9.3.3 replace shap func's disp by xde's disp and write
            for strs in xde_lists['coef_shap'][shap_type]:
                temp_string = shap_string.replace('u', strs)
                file.write(strs + '=\n')
                file.write(temp_string)
                file.write('\n')

    # 9 write gaus paragraph
    if 'gaus' in xde_lists:

        # 9.1 Gaussian integral
        if xde_lists['gaus'][0] == 'g':

            gaus_degree = regx.search(r'[0-9]+', xde_lists['gaus'],
                                      regx.I).group()

            # 9.1.1 line square or cube shap
            if shap_tag[0].lower()=='l' \
            or shap_tag[0].lower()=='q' \
            or shap_tag[0].lower()=='c':

                path_gaus = pfelacpath + 'ges/gaus.pnt'
                file_gaus = open(path_gaus, mode='r')
                gaus_find = 0
                gaus_axis = []
                gaus_weit = []

                # 9.1.1.1 read gaus axis and weight in gaus.pnt
                for line in file_gaus.readlines():
                    gaus_start_file = regx.search('n=' + gaus_degree, line,
                                                  regx.I)
                    if gaus_start_file != None:
                        gaus_find = 1
                        continue
                    if gaus_find == 1 and line == '\n':
                        gaus_find = 0
                        continue
                    if gaus_find == 1:
                        gaus_string = line.split()
                        if gaus_string[0][0] != '-':
                            gaus_string[0] = ' ' + gaus_string[0]
                        gaus_axis.append(gaus_string[0])
                        gaus_weit.append(gaus_string[1])

                file_gaus.close()

                # 9.1.1.2 write line square or cube's gaussian integra
                file.write('gaus = ' + str(len(gaus_weit)**int(dim)) + '\n')

                if shap_tag[0].lower() == 'l':
                    for axis_i in range(len(gaus_axis)):
                        file.write(gaus_axis[axis_i]+' ' \
                                  +gaus_weit[axis_i]+'\n')

                elif shap_tag[0].lower() == 'q':
                    for axis_i in range(len(gaus_axis)):
                        for axis_j in range(len(gaus_axis)):
                            weight = float(gaus_weit[axis_i]) \
                                    *float(gaus_weit[axis_j])
                            file.write(gaus_axis[axis_i]+' ' \
                                      +gaus_axis[axis_j]+' ' \
                                      +str(weight)+'\n')

                elif shap_tag[0].lower() == 'c':
                    for axis_i in range(len(gaus_axis)):
                        for axis_j in range(len(gaus_axis)):
                            for axis_k in range(len(gaus_axis)):
                                weight = float(gaus_weit[axis_i]) \
                                        *float(gaus_weit[axis_j]) \
                                        *float(gaus_weit[axis_k])
                                file.write(gaus_axis[axis_i]+' ' \
                                          +gaus_axis[axis_j]+' ' \
                                          +gaus_axis[axis_k]+' ' \
                                          +str(weight)+'\n')

            # 9.1.2 triangle shap
            elif shap_tag[0].lower() == 't':

                path_gaus = pfelacpath + 'ges/gaust.pnt'
                file_gaus = open(path_gaus, mode='r')
                gaus_find = 0
                gaus_string = ''

                # 9.1.2.1 tackle the gaussian degree
                if gaus_degree == '6':
                    gaus_degree = '5'
                elif int(gaus_degree) > 12 and int(gaus_degree) < 17:
                    gaus_degree = '12'
                elif int(gaus_degree) > 17:
                    gaus_degree = 17

                # 9.1.2.2 read gaus axis and weight in gaust.pnt and write
                for line in file_gaus.readlines():
                    gaus_start_file = regx.search('P' + gaus_degree, line,
                                                  regx.I)
                    if gaus_start_file != None:
                        gaus_find = 1
                        continue
                    if gaus_find == 1 and line == '\n':
                        gaus_find = 0
                        continue
                    if gaus_find == 1:
                        gaus_string += line

                file_gaus.close()
                file.write(gaus_string)

            # 9.1.3  tetrahedron shap
            elif shap_tag[0].lower() == 'w':

                path_gaus = pfelacpath + 'ges/gausw.pnt'
                file_gaus = open(path_gaus, mode='r')
                gaus_find = 0
                gaus_string = ''

                # 9.1.3.1 tackle the gaussian degree
                if gaus_degree == '4':
                    gaus_degree = '3'
                elif gaus_degree == '6':
                    gaus_degree = '5'
                elif int(gaus_degree) > 7:
                    gaus_degree = '7'

                # 9.1.2.2 read gaus axis and weight in gausw.pnt and write
                for line in file_gaus.readlines():
                    gaus_start_file = regx.search('P' + gaus_degree, line,
                                                  regx.I)
                    if gaus_start_file != None:
                        gaus_find = 1
                        continue
                    if gaus_find == 1 and line == '\n':
                        gaus_find = 0
                        continue
                    if gaus_find == 1:
                        gaus_string += line

                file_gaus.close()
                file.write(gaus_string)

            else:
                pass

        # 9.2 node integral
        else:
            path_gaus = pfelacpath + 'ges/ges.lib'
            file_gaus = open(path_gaus, mode='r')
            gaus_find = 0
            gaus_string = ''

            # 9.2.1 read gaus axis and weight in ges.lib and write
            for line in file_gaus.readlines():
                gaus_start_file = regx.search(
                    'sub d' + dim + xde_lists['gaus'] + '.gau', line, regx.I)
                gaus_end_file = regx.search(
                    'end d' + dim + xde_lists['gaus'] + '.gau', line, regx.I)
                if gaus_start_file != None:
                    gaus_find = 1
                    continue
                if gaus_end_file != None:
                    gaus_find = 0
                    continue
                if gaus_find == 1:
                    gaus_string += line

            file_gaus.close()
            file.write(gaus_string)

    # 10 write func paragraph
    if 'func' in code_use_dict \
    or 'vol'  in xde_lists :
        file.write('\nfunc\n')
        if 'vol' in xde_lists:
            file.write(xde_lists['vol'])
        if 'func' in code_use_dict:
            for strs in code_use_dict['func']:
                file.write(strs)

    # 11 write stif, mass, damp paragraph
    for weak in ['stif', 'mass', 'damp']:
        if weak in xde_lists:
            file.write('\n{}\n'.format(weak))
            if weak in code_use_dict:
                for strs in code_use_dict[weak]:
                    file.write(strs)
            if xde_lists[weak][0] == 'dist':
                left_var = xde_lists[weak][0]
                righ_expr = ''
                for ii in range(1, len(xde_lists[weak])):
                    righ_expr += xde_lists[weak][ii]

                expr_list = idx_summation(left_var, righ_expr, xde_lists)
                expr_list = split_expr(expr_list[0])
                for strs in expr_list:
                    if strs == 'dist=':
                        file.write(strs)
                    else:
                        file.write(strs + '\n')
            elif xde_lists[weak][0] == 'lump':
                file.write('lump =\n')
                for shaps in xde_lists['shap']:
                    nodn = regx.search(r'\d+', shaps, regx.I).group()
                    for vara in xde_lists['shap'][shaps]:
                        for ii in range(int(nodn)):
                            file.write('+[{}]{}{}\n'.format(
                                xde_lists[weak][1], vara, ii + 1))

    # 14 write load paragraph
    file.write('\nload\n')
    if 'load' in xde_lists:
        left_var = 'load'
        righ_expr = ''
        for strs in xde_lists['load']:
            righ_expr += strs
        expr_list = idx_summation(left_var, righ_expr, xde_lists)
        expr_list = split_expr(expr_list[0])
        for strs in expr_list:
            if strs == 'load=':
                file.write(strs)
            else:
                file.write(strs + '\n')
    else:
        print('error: no load declared')

    file.write('\nend')

    file.close()

    import json
    file = open('../1ges_target/code_use_dict.json', mode='w')
    file.write(json.dumps(code_use_dict, indent=4))
    file.close()