Home My Page Projects Code Snippets Project Openings diderot
Summary Activity Tracker Tasks SCM

SCM Repository

[diderot] View of /branches/ein16/synth/d2/test_createField.py
ViewVC logotype

View of /branches/ein16/synth/d2/test_createField.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4236 - (download) (as text) (annotate)
Wed Jul 20 03:02:00 2016 UTC (3 years, 6 months ago) by cchiw
File size: 10463 byte(s)
added generic cases for trace|det and added test cases
import sys
import re
import os

from obj_apply import *
from obj_ty import *
from obj_operator import *
from obj_field import *
#constants
branch="/Users/chariseechiw/diderot/vis12/bin/diderotc --exec "

#convert coeffs
def llist(cmd, coeff):
    a=" -"+cmd
    for i in coeff:
        a+=" "+str(i)
    return a

#call program due to field type
def progName(itype, outSize):
    #call program due to field type
    if (fty.isEq_id(itype,ty_scalarF_d1)):
        #print "kitten-made a 1-d field"
        return ("symb_fsd1"," -which 0 "," -s 1 "+str(outSize))          # 1-d scalar field
    elif (fty.isEq_id(itype,ty_scalarF_d2)):
        return ("symb_fsd2"," -which 0 "," -s 1 "+str(outSize * outSize))          # 2-d scalar field
    elif(fty.isEq_id(itype,ty_vec2F_d2)):                      # 2-d length 2 vector field
        return ("symb_fv2d2"," -which 0 "," -s 1 "+str(2*outSize *outSize))         # creates field with coefficients for each axis
    elif(fty.isEq_id(itype, ty_mat2x2F_d2)):                      # 2-d matrx 2x2
        return ("symb_fm2x2d2"," -which 0 "," -s 1 "+str(4*outSize *outSize))
        # creates field with coefficients for each axis
    elif (fty.isEq_id(itype,ty_scalarF_d3)):
        return ("symb_fsd3"," -which 0 "," -s 1 "+str(outSize * outSize * outSize))          # 3-d scalar field
    elif(fty.isEq_id(itype,ty_vec3F_d3)):                      # 2-d length 2 vector field
        size = 3* outSize *outSize*outSize
        return ("symb_fv3d3"," -which 0 "," -s 1 "+str(size))         # creates field with coefficients for each axis
    elif(fty.isEq_id(itype, ty_mat2x2F_d3)):
        # 3-d matrix field
        size = 4* outSize *outSize*outSize
        return ("symb_fm2x2d3"," -which 0 "," -s 1 "+str(size))
    elif(fty.isEq_id(itype, ty_mat3x3F_d3)):
        # 3-d matrix field
        size = 9* outSize *outSize*outSize
        return ("symb_fm3x3d3"," -which 0 "," -s 1 "+str(size))
    else:
        raise Exception("type not supported in program name:"+itype.name)


# base,xsq,ysq,diag-coeffs
def coeffToBase(coeff, str):
    # print "coeff: ",len(coeff), " -",coeff
    [a,b,c,d,e,f,g,h,i]= coeff
    base = [b,c]
    xsq=[d,g]
    ysq=[f,h]
    diag=[a,e,i]
    COEFF = llist("base"+str,base)+llist("xsq"+str,xsq)+llist("ysq"+str,ysq)+llist("diag"+str,diag) #convert coeff to strings
    return COEFF

def coeffToBase2(coeff, str):
    # print "coeff: ",len(coeff), " -",coeff
    if (coeff== [0,0,0,0,0,0,0,0,0]):
        return ""
    else:
        [a,b,c,d,e,f,g,h,i]= coeff
        s=""
        base = [b,c]
        if (base == [0,0]):
            1
        else:
            s+= llist("base"+str,base)
        xsq=[d,g]
        if (xsq == [0,0]):
            1
        else:
            xsq=[d,g]
            s+= llist("xsq"+str,xsq)
        ysq=[f,h]
        if (ysq == [0,0]):
            1
        else:
            ysq=[f,h]
            s+= llist("ysq"+str,ysq)
        diag=[a,e,i]
        if (diag==[0,0,0]):
            1
        else:
            s+=llist("diag"+str,diag) #convert coeff to strings
        return s



# shape-input field
# outSize-samples
# orig- name of synthetic field created with coefficients
def createSingleField(itype,outSize,orig,  coeff):
    #print "inside create singlefield: "+orig+" ty: "+itype.name,"coeff:",coeff
    outputnrrd=orig+".nrrd"
    sout=str(outSize)
    def get_Params():
        if(fty.get_dim(itype)==1):
            [a,b,c]=coeff
            COEFF ="-base "+str(a)+" "+str(b)+" "+str(c)#convert coeff to strings
            SIZE=" -sz0 "+ sout +" -width 1"    # giving sample size
            return COEFF + SIZE
        elif(fty.get_dim(itype)==2):
            #2d vector field
            if(fty.isEq_id(itype,  ty_mat2x2F_d2)):
                [m0,m1]=coeff
                [v1,v3] = m0
                [v2,v4] = m1
                COEFF = coeffToBase(v1, "1")+coeffToBase(v2, "2")+coeffToBase(v3, "3")+coeffToBase(v4, "4")#convert coeff to strings
                SIZE=" -sz0 "+ sout +" -sz1 "+ sout +" -width 1"    # giving sample size
                return COEFF + SIZE
            elif(fty.isEq_id(itype, ty_vec2F_d2)):
                [v0,v1]=coeff
                COEFF = coeffToBase(v0, "1")+coeffToBase(v1, "2")#convert coeff to strings
                SIZE=" -sz0 "+ sout +" -sz1 "+ sout +" -width 1"    # giving sample size
                return COEFF + SIZE
            elif(fty.isEq_id(itype, ty_scalarF_d2)):
                # scalar field
                COEFF = coeffToBase(coeff, "")#convert coeff to strings
                SIZE=" -sz0 "+ sout +" -sz1 "+ sout +" -width 1"    # giving sample size
                return COEFF + SIZE
            else:
                raise "shape for dimension 2 is not supported"
        elif(fty.get_dim(itype)==3):
            if(fty.isEq_id(itype, ty_scalarF_d3)):
                [z0,z1,z2]=coeff
                COEFF=coeffToBase(z0, "_z0")+coeffToBase(z1, "_z1")+coeffToBase(z2, "_z2")
                SIZE=" -sz0 "+ sout +" -sz1 "+ sout+" -sz2 "+ sout +" -width 1"    # giving sample size
                return COEFF + SIZE
            elif(fty.isEq_id(itype,ty_vec3F_d3)):
                [[az0,az1,az2],[bz0,bz1,bz2],[cz0,cz1,cz2]]=coeff
                COEFF=coeffToBase(az0, "1_z0")+coeffToBase(az1, "1_z1")+coeffToBase(az2, "1_z2")
                COEFF+=coeffToBase(bz0, "2_z0")+coeffToBase(bz1, "2_z1")+coeffToBase(bz2, "2_z2")
                COEFF+=coeffToBase(cz0, "3_z0")+coeffToBase(cz1, "3_z1")+coeffToBase(cz2, "3_z2")
                SIZE=" -sz0 "+ sout +" -sz1 "+ sout+" -sz2 "+ sout +" -width 1"    # giving sample size
                return COEFF + SIZE
            elif(fty.isEq_id(itype, ty_mat2x2F_d3)):
                [m0, m1] = coeff
                [[az0, az1, az2], [cz0, cz1, cz2]] = m0
                [[bz0, bz1, bz2], [dz0, dz1, dz2]] = m1
                COEFF= coeffToBase(az0, "1_z0")+coeffToBase(az1, "1_z1")+coeffToBase(az2, "1_z2")
                COEFF+=coeffToBase(bz0, "2_z0")+coeffToBase(bz1, "2_z1")+coeffToBase(bz2, "2_z2")
                COEFF+=coeffToBase(cz0, "3_z0")+coeffToBase(cz1, "3_z1")+coeffToBase(cz2, "3_z2")
                COEFF+=coeffToBase(dz0, "4_z0")+coeffToBase(dz1, "4_z1")+coeffToBase(dz2, "4_z2")
                SIZE=" -sz0 "+ sout +" -sz1 "+ sout+" -sz2 "+ sout +" -width 1"    # giving sample size
                return COEFF + SIZE
    
            elif(fty.isEq_id(itype, ty_mat3x3F_d3)):
                [m0, _,_] = coeff
                # [[z0_1, z0_2, z0_3],[z1_1, z1_2, z1_3],[z2_1, z2_2, z2_3]]= m0
                
                [[z0_1, z1_1, z2_1],[z0_2, z1_2, z2_2],[z0_3, z1_3, z2_3]]= m0
                #[[z0_4, z1_2, z2_2],[z0_5, z1_5, z2_5],[z0_6, z1_8, z2_8]]= m1
                # [[z0_7, z1_3, z2_3],[z0_8, z1_6, z2_6],[z0_9, z1_9, z2_9]]= m2
                print ("\n\n\n")
                print ("\n\ncoeff-m0_1_z0:", z0_1)
                print ("coeef", coeffToBase2(z0_1, "1_z0"))
                print ("\n\n\n")
                print ("\n\ncoeff-m0_1_z1:", z1_1)
                print ("coeff",coeffToBase2(z1_1, "1_z1"))
                print ("\n\n\n")
                print ("\n\ncoeff-z2_1:", z2_1)
                print ("coeff",coeffToBase2(z2_1, "1_z2"))
                print ("\n\n\n")
                print ("\n\n\n")
                COEFF=coeffToBase2(z0_1, "1_z0")+coeffToBase2(z1_1, "1_z1")+coeffToBase2(z2_1, "1_z2")
                print ("coeffs", COEFF)
                print ("\n\n\n")
                print ("\n\ncoeff-z0_2 :", z0_2)
                print ("coeff",coeffToBase2(z0_2, "2_z0"))
                print ("\n\n\n")
                



                COEFF+=coeffToBase2(z0_2, "2_z0")+coeffToBase2(z1_2, "2_z1")+coeffToBase2(z2_2, "2_z2")
                COEFF+=coeffToBase2(z0_3, "3_z0")+coeffToBase2(z1_3, "3_z1")+coeffToBase2(z2_3, "3_z2")
               
                # COEFF+=coeffToBase(z0_4, "4_z0")+coeffToBase(z1_4, "4_z1")+coeffToBase(z2_4, "4_z2")
                # COEFF+=coeffToBase(z0_5, "5_z0")+coeffToBase(z1_5, "5_z1")+coeffToBase(z2_5, "5_z2")
                # COEFF+=coeffToBase(z0_6, "6_z0")+coeffToBase(z1_5, "6_z1")+coeffToBase(z2_6, "6_z2")

                # COEFF+=coeffToBase(z0_7, "7_z0")+coeffToBase(z1_7, "7_z1")+coeffToBase(z2_7, "7_z2")
                # COEFF+=coeffToBase(z0_8, "8_z0")+coeffToBase(z1_8, "8_z1")+coeffToBase(z2_8, "8_z2")
                # COEFF+=coeffToBase(z0_9, "9_z0")+coeffToBase(z1_9, "9_z1")+coeffToBase(z2_9, "9_z2")
                #print ("cmd",COEFF)
                SIZE=" -sz0 "+ sout +" -sz1 "+ sout+" -sz2 "+ sout +" -width 1"    # giving sample size
                return COEFF + SIZE
            else:
                raise "shape for dimension 3 is not supported"
        else:
            raise "dim not supported:"
    PARAMS = get_Params()
    # get program name and which command
    #print "create field A"
    (e_Orig,w_coeff,w_shape) = progName(itype, outSize)
    #print "create field B"
    p_Orig=  e_Orig+".diderot"
    #compile program
    os.system("cp symb/"+ p_Orig +" "+ p_Orig)
    #print "create field C",(branch+p_Orig)
    os.system(branch+p_Orig)
    #execute program
    #print "create field D"
    #print "+e_Orig+w_coeff+PARAMS-0./",e_Orig+w_coeff+PARAMS
    os.system("./"+e_Orig+w_coeff+PARAMS)
    #print "+e_Orig+w_coeff+PARAMS-1"
    #print "create field E"
    #print ("./"+e_Orig+w_coeff+PARAMS+"| unu save -f nrrd -o "+outputnrrd)
    os.system("./"+e_Orig+w_coeff+PARAMS+"| unu save -f nrrd -o "+outputnrrd)
    #print "create field F"
    #print ("unu reshape -i "+outputnrrd+w_shape+" | unu save -f text -o "+orig+".txt")
    os.system("unu reshape -i "+outputnrrd+w_shape+" | unu save -f text -o "+orig+".txt")
    #save nrrd file
    os.system("rm *.o")
    os.system("rm *.h")
    os.system("rm *.c")
    os.system("rm "+e_Orig)

def createField(appC,outSize, coeffs):
    #app = apply.get_all_Fields(appC)
    #itypes = apply.get_types(app)
    #exps =  apply.get_exps(app)
    flds = apply.get_all_Fields(appC)
    itypes = []
    exps = []
    # print "fields-length", len(flds)
    for j in range(len(flds)):
        i = flds[j]
        itypes.append(i.fldty)
        #print "j:",j,"coeffs",coeffs[j],"itypes",itypes[j].name
    
    exps = flds
    for (i,c,s)in zip(exps, coeffs,itypes):
        # print field.toStr(i)
        if(field.get_isField(i)): # not a tensor type

            createSingleField(s,outSize,i.inputfile, c)

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0