def main():
    parser = OptionParser()
    parser.add_option("-f", "--file", dest='networkName',
                      help="open file with networkName", metavar="FILE")
    parser.add_option("-s", "--save", dest="save", 
                      help="save solution data, 0 = False, 1 = True")
    
    parser.add_option("-n", "--dataNumber", dest='dataNumber',
                      help="number of the solution data (last number in filename), default = 1, max 999", metavar = "FILE")
    
    parser.add_option('-v', '--vizBool', dest='vizBool',
                      help="choose visualisation mode, 0: no visualisation, 1: 2d and 3d, 2: 2d plots, 3: 3d visualisation default = 1")
    
    (options, args) = parser.parse_args()
    
    networkName = 'oldNetwork1'
    if options.networkName != None:
        networkName = options.networkName
    else: 
        print " no networkName passed, visualisation could not be intialized"
        print " use -f networkName to define an file you want to visualize"
        exit()
    filename = str(networkName+'.xml')
    
    dataNumber = '001'
    if options.dataNumber != None:
        dataNumber = options.dataNumber
        if len(dataNumber) == 1:
            dataNumber = '00'+dataNumber
        if len(dataNumber) == 2:
            dataNumber = '0'+dataNumber  
    
    save = False
    if options.save != None:
        if options.save == '0':
            save = False
        elif options.save == '1':
            save = True
        
    vizBool = '1'
    vizBool2d  = True
    vizBool3d = True
    print options.vizBool
    if options.vizBool != None:
        if options.vizBool == '0':
            vizBool2d = False
            vizBool3d = False
        elif options.vizBool == '1':
            vizBool2d = True
            vizBool3d = True
        elif options.vizBool == '2':
            vizBool2d = True
            vizBool3d = False
        elif options.vizBool == '3':
            vizBool2d = False
            vizBool3d = True
      
    # load network from the path!
    Lstart = time.clock()
    vascularNetwork = loadNetworkFromXML(filename=filename) 
    
    if vascularNetwork == None: exit()
    # update the connections of the network, as they prob weren't saved correct
    vascularNetwork.evaluateConnections()
    Lend = time.clock()
    print" needed %1.3f  sec to load" %(Lend-Lstart)
    
    vascularNetwork.initialize()
    
    #create visualisation
    if vizBool3d == True:
        visualisation = Visualisation(vascularNetwork=vascularNetwork)
    
    
    #print '====================================='
    #print '_________ Simulation Context _________'
    #print '%-25s %-20s' % ('Numerical scheme',networkSolver['NumScheme'])
    #print '%-25s %4d' % ('Equation system',int(networkSolver['EqSystem']))
    #print '%-25s %4d' % ('Characteristic system',int(networkSolver['CharSystem']))
    #print '________ Visualisation Context _______'
    
    
    Cstart = time.clock()
    flowSolver = FlowSolver(vascularNetwork)
    P,Q,A = flowSolver.solve()
    print '\n____________ Solver time _____________'
    minutes = int((time.clock()-Cstart)/60.)
    secs = (time.clock()-Cstart)-float(minutes)*60.
    print '%d %s %f %s' % (minutes ,'min',secs ,'sec')   
    
    if save == True:
        #save solution data in c pickle
        #standart solution path
        networkPath = str(cur+"/NetworkFiles/")
        networkDirectory = filename.split('.')[0]
        solutionPath = str(networkPath+networkDirectory+'/')
        # data to save
        solutionDataSave = [{'Pressure': P, 'Flow': Q, 'Area': A, 'WaveSpeed':Q,'Name': str('simulation_'+dataNumber) }]
        #create file with filename in soultionPath
        endName = "_SolutionData_"+dataNumber+".pickle"
        FILE = open(str(solutionPath+vascularNetwork.name+endName),"w")
        # store pickle
        cPickle.dump(solutionDataSave, FILE, protocol=2)
        FILE.close()
        
    if vizBool2d == True:
        string = ' '.join(['python',cur+'/Visualisation/class2dVisualisationSimple.py','-f',vascularNetwork.name, '-n 1','-i 0'])                
        subprocess.call(string, shell=True)
    
    if vizBool3d == True:
        # create solution data
        solutionData = [ {'Pressure': P, 'Flow': Q, 'Area': A, 'Name': str('simulation '+dataNumber) }]
        #set the solution data
        visualisation.setSolutionData(solutionData)
        visualisation.factor = 10
        # set the coefficient for the velocity profile
        visualisation.powerLawCoefficient = 2       
        visualisation.visualize()
        # clear memory
        visualisation.__del__()
        del(visualisation)
def main():
    # set graphs directory
    graphPath = str(cur+'/../vascular1Dflow_v0.2/NetworkFiles/')
    
    # create a new window
    window = MyDotWindow()    
    window.connect('destroy',gtk.main_quit)
    window.show()
    
    #create the main graph instance of a graph class
    mainGraph = Graph()
    
    #START THE MAIN LOOP
    menuInput = ""
    subMenuInput = ''
    
    # create vascularNetwork instance
    vascularNetwork = VascularNetwork()
    filename = None
    networkTopologyMode = True
    networkTopologyModes = {0: 'node Description', 1:'mother-daugther Description'}
    k = None
    while menuInput != "q":
        menuInput = ""
        print ""
        print "vnc 2.1 - menu"
        print ""
        print " [a] - add vessel to network"
        print " [d] - delete vessel in network"
        print " [n] - new network"
        print " [b] - set boundary conditions"
        print " [l] - load network"
        print " [s] - save network"
        print " [u] - update XML form CSV file"
        print " [g] - print network graph"
        print " [f] - print network informations"
        print " [m] - change network topology mode; current: ", networkTopologyModes[networkTopologyMode]
        print " [q] - quit"
        print ""
        print '  current network: ', filename
        while  menuInput not in ("l","b","q","a","s","g","f","d","u",'n','m'): #(menuInput != "l") and (menuInput != "b") and (menuInput != "q") and (menuInput != "a") and (menuInput != "s") and (menuInput != "g") and (menuInput != "d") and (menuInput != "f"):
            menuInput = raw_input("what to do? ")
        
        if menuInput == "a": 
            print "Add new vessel"
            
            existing = False
            vesselID = raw_input(" enter the vessel id:  ")
            while True:
                try:
                     vesselID = int(vesselID)
                     if vesselID not in vascularNetwork.vessels:
                         break
                     else:
                         existing = True
                except ValueError:
                    print "TYPE-ERROR: vessel id must be type(int) not type(string)"
                    vesselID = raw_input(" enter non existing id: ")
                if existing == True:
                    print " the vessel id exists already enter a new one"
                    vesselID = raw_input(" enter non existing id: ")
                    existing = False
            
            
            if not networkTopologyMode:
                # define start node ony bifurctaion are allowed
                startNode = int(raw_input(" enter starting node:  "))
                while bifTest(vascularNetwork.vessels, startNode) == False:
                    print "   only bifurcations possible!"
                    startNode = int(raw_input(" enter starting node:  "))
                # define end node
                endNode = int(raw_input (" enter ending node:    "))
                # create vessel obj and save them in vessels-dict
                
                dataDict = {'end': endNode, 'start': startNode}
                vascularNetwork.addVessel(vesselID, dataDict)
                
            else:
                if vascularNetwork.vessels != {}:
#                    mother = int(raw_input(" enter existing mother id:  "))
#                    while mother not in vascularNetwork.vessels or (mother in vascularNetwork.vessels and vascularNetwork.vessels[mother].rightDaughter != None):
#                        if mother not in vascularNetwork.vessels: print " there exists no vessel with this ID"
#                        else: print "   only bifurcations possible!"
#                        mother = int(raw_input(" enter existing mother id: "))
                        
                    existing = False
                    mother = raw_input(" enter existing mother id:  ")
                    while True:
                        try:
                             mother = int(mother)
                             if mother in vascularNetwork.vessels and vascularNetwork.vessels[mother].rightDaughter == None:
                                 break
                             else:
                                 existing = True
                        except ValueError:
                            print "TYPE-ERROR: mother id must be type(int) not type(string)"
                            mother = raw_input(" enter existing mother id:  ")
                        if existing == True:
                            if mother not in vascularNetwork.vessels: print " there exists no vessel with this id"
                            else: print "   only bifurcations possible!"
                            mother = raw_input(" enter existing mother id:  ")
                            existing = False
                        
                    if vascularNetwork.vessels[mother].leftDaughter == None: vascularNetwork.vessels[mother].leftDaughter = vesselID
                    else: vascularNetwork.vessels[mother].rightDaughter = vesselID
                    vascularNetwork.vessels[mother].end = None
                
                vascularNetwork.addVessel(vesselID)
            mainGraph.update_graph(vascularNetwork, window)
                    
        if menuInput == "d":
            print "Delete a vessel and all its daugthers"
            if vascularNetwork.vessels.keys() != []:
                
#                vesselID = int(raw_input(" enter the vessel id: "))
#                while (vesselID in vascularNetwork.vessels) == False:
#                    print " the vessel does not exist"
#                    vesselID = int(raw_input(" enter existing vessel id: "))
                
                existing = False
                vesselID = raw_input(" enter existing vessel id: ")
                while True:
                    try:
                         vesselID = int(vesselID)
                         if vesselID in vascularNetwork.vessels:
                             break
                         else:
                             existing = True
                    except ValueError:
                        print "TYPE-ERROR: vessel id must be type(int) not type(string)"
                        vesselID = raw_input(" enter existing vessel id: ")
                    if existing == True:
                        print " the vessel does not exist"
                        vesselID = raw_input(" enter existing vessel id: ")
                        existing = False
                
                #travers the tree starting with the vessel and collect all ids
                toDelete = findAllDaughters(vascularNetwork,vesselID)
                
                toDelete.append(vesselID)
                for vesselToDelete in toDelete:
                    vascularNetwork.deleteVessel(vesselToDelete)
                    
                # empty the graph to redraw it
                mainGraph.update_graph(vascularNetwork, window)
                
            else:
                print " there are no vessels to delete"
                
        elif menuInput == "n":
            print "new network"
            question = raw_input(" are u sure to delete all current data? [n] - yes: ")
            if question == 'n':
                # delet vascularNetwork
                del vascularNetwork
                # create vascularNetwork instance
                vascularNetwork = VascularNetwork()
                mainGraph.update_graph(vascularNetwork, window)
            
        elif menuInput == "m":
            networkTopologyMode = not networkTopologyMode
        
        elif menuInput == "f":
            vascularNetwork.showVessels()
            vascularNetwork.showNetwork()
            vascularNetwork.calculateNetworkResistance()
            
            
        elif menuInput == "g":
            print mainGraph.getGraph()
            
        elif menuInput == "b":
            subMenuInput = ''
            
            print 'create/load/save/delete/show BoundaryCondistions, not implemented yet'
#           
#            while  subMenuInput not in ['1','2','3','4','5','b']:         
#                # evaluate boundarys in Network
#                boundarys = []
#                notDefinedBoundarys = []
#                
#                boundarys.extend(vascularNetwork.ends)
#                if vascularNetwork.root != [] and vascularNetwork.root[0] not in boundarys: 
#                    boundarys.extend(vascularNetwork.root)
#                    
#                boundarysSaved = vascularNetwork.boundaryConditions.keys()
#                
#                # update saved boundary conditions
#                for boundarysCurrent in boundarys:
#                    if boundarysCurrent not in boundarysSaved:
#                        print " boundary added to vascularNetwork"
#                        vascularNetwork.boundaryConditions[boundarysCurrent] = {}
#                        boundarysSaved.append(boundarysCurrent)
#                        
#                    if vascularNetwork.boundaryConditions[boundarysCurrent] == {}:
#                        notDefinedBoundarys.append(boundarysCurrent)
#                    
#                nonBoundarys = list(set(boundarys).symmetric_difference(set(boundarysSaved)))
#                for nonBoundary in nonBoundarys:
#                    print " boundary removed from vacularNetwork"
#                    del(vascularNetwork.boundaryConditions[nonBoundary])
#                    if nonBoundary in notDefinedBoundarys: notDefinedBoundarys.remove(nonBoundary)
#                    
#                vascularNetwork.evaluateConnections()
#                print ""
#                print "    sub menu: boundary conditions"
#                print ""
#                print "     [1] - show  boundary conditions"
#                print "     [2] - add   boundary condition "
#                print "     [3] - del   boundary condition "
#                print "     [4] - load  boundary conditions from CSV"
#                print "     [5] - write boundary conditions to CSV"
#                print "     [b] - back to the main menu"
#                print ""     
#                subMenuInput = raw_input("     what to do? ") 
#                
#                if subMenuInput == '1':
#                    print "     boundary conditions"
#                    pprint.pprint(vascularNetwork.boundaryConditions)
#                    subMenuInput = ''
#                    
#                elif subMenuInput == '2' and vascularNetwork.root != []:
#                    
#                    print "     add   boundary condition"
#                    print ""
#                    
#                    definedBoundarys = list(set(notDefinedBoundarys).symmetric_difference(set(vascularNetwork.boundaryConditions.keys())))
#                    print "     vessels with defined boundary condition:"
#                    print "       ",'  '.join(str(i) for i in definedBoundarys)
#                    
#                    print "     vessels with undefined boundary condition:"
#                    print "       ",'  '.join(str(i) for i in notDefinedBoundarys)
#                    print ""
#                                        
#                    existing = False
#                    vesselID = raw_input(" enter existing vessel id: ")
#                    while True:
#                        try:
#                             vesselID = int(vesselID)
#                             if vesselID in vascularNetwork.vessels:
#                                 break
#                             else:
#                                 existing = True
#                        except ValueError:
#                            print " TYPE-ERROR: vessel id must be type(int) not type(string)"
#                            vesselID = raw_input(" enter existing vessel id: ")
#                        if existing == True:
#                            print " the vessel does not exist"
#                            vesselID = raw_input(" enter existing vessel id: ")
#                            existing = False
#                    
#                    
#                    inType = '0'
#                    print "     add boundary condition type"
#                    print ""
#                    print "      'Qa'   'Q2a'   'Qm'   'Q2m'   'Pa'    'P2a'   'Pm'   'P2m'   'Ue' "
#                    print "       [1]    [2]    [3]     [4]     [5]     [6]     [7]    [8]     [9]"
#                    print ""
#                    print "      'Rt'    'R'    'WK2'  'WK3'   'QPhy'  'Lnet'  'Fou'   'D'    'PhyQ' "
#                    print "      [10]    [11]   [12]    [13]    [14]    [15]    [16]   [17]    [18]"
#                    print ""
#                    
#                    types = ['Qa','Q2a','Qm','Q2m','Pa','P2a','Pm','P2m','Ue','Rt','R','WK2','WK3','QPhy','Lnet','Fou','D','PhyQ']
#                    
#                    existing = False
#                    inType = raw_input ("      choose type ")
#                    while True:
#                        try:
#                             inType = int(inType)
#                             if inType in np.linspace(1,18,18):
#                                 break
#                             else:
#                                 existing = True
#                        except ValueError:
#                            print "      TYPE-ERROR: vessel id must be type(int) not type(string)"
#                            inType = (raw_input ("      choose type "))
#                        if existing == True:
#                            print "       the type does not exist"
#                            inType = (raw_input ("      choose type "))
#                            existing = False
#                    
#                    
#                    type = types[int(inType)-1]
#                     
#                    bcTags = {'Rt':['Rt'],
#                              'R':['Rc'],
#                              'WK2':['Rc','C'],
#                              'WK3':['Rc','C','R1','RT'],
#                              'QPhy':['Q0_a','Npulse','freq','Tpulse','Tspace'],
#                              'Qa':['Q0_a','Npulse','freq'],
#                              'Q2a':['Q0_a','Npulse','freq','Tpulse'],
#                              'Qm':['Q0_m','Tmean','Traise'],
#                              'Q2m':['Q0_m','Tmean','Traise'],
#                              'Ue':['Upeak','C','Traise'],
#                              'Pa':['P0_a','Npulse','freq'],
#                              'P2a':['P0_a','Npulse','freq','Tpulse'],
#                              'Pm':['P0_m','Tmean','Traise'],
#                              'P2m':['P0_m','Tmean','Traise'],
#                              'Fou':['P0_m','Tmean','Traise','scale','Npulse','Tpulse'],
#                              'D':[''],
#                              'PhyQ':[''],
#                              'Lnet':['Z','C']}
#                    
#                    
#                    bcList = []
#                    print "      set values for the BC condition"
#                    print "          enter 'b' for the first value to skip this procedure"
#                    question = True
#                    for arg in bcTags[type]:
#                        if question == True: 
#                            currValue = raw_input (str("            set value for "+str(arg)+' '))
#                            if currValue == 'b': question=False
#                            test = True
#                            try: float(currValue)
#                            except:
#                                print '            VALUE or TYPE ERROR, set to None'
#                                test = False
#                            if test == True: bcList.append(float(currValue))
#                            else: bcList.append(None)
#                        else: bcList.append(None)
#                    if len(vascularNetwork.boundaryConditions.keys()) == 1:
#                        print "      set position of the BC condition"
#                        position = '2'
#                        while position not in ['1','0']:
#                            position = raw_input ("          enter '0' for the start or '1' for the end of the vessel ")
#                        if position == '1':
#                            type = ''.join(['_',type])
#                    
#                    vascularNetwork.boundaryConditions[vesselID].update({type : bcList})
#                    mainGraph.update_graph(vascularNetwork, window)
#                    subMenuInput = ''
#                        
#                elif subMenuInput == '3' and vascularNetwork.root != []:
#                    print "     delete boundary condition"
#                    print ""
#                    pprint.pprint(vascularNetwork.boundaryConditions)
#                    
#                    vesselID = -1
#                    while vesselID not in vascularNetwork.boundaryConditions.keys():
#                        vesselID = int(raw_input ("      choose vessel id "))
#                    
#                    bcs = vascularNetwork.boundaryConditions[vesselID].keys()
#                    if bcs != []:                 
#                        print ""
#                        print '        ','  '.join(str(' '+i+' ') for i in bcs)
#                        print '        ','   '.join(str('['+str(int(i))+']') for i in np.linspace(1,len(bcs),len(bcs)))
#                        print ""
#                        inType = '0'
#                        while inType not in np.linspace(1,len(bcs),len(bcs)):
#                            inType = int(raw_input ("      choose condition to delete "))
#                            
#                        vascularNetwork.boundaryConditions[vesselID].pop(bcs[inType-1])
#                        print ""
#                        print "     boundary condition ",bcs[inType-1]," removed!"
#                        print ""
#                        mainGraph.update_graph(vascularNetwork, window)
#                    else:
#                        print "     nothing to delete!"
#                    subMenuInput = ''
#                
#                elif subMenuInput == '4' and vascularNetwork.root != []:
#                    
#                    print "     load  boundary conditions from CSV"
#                    print ""
#                    filename = enterFilename(filename,'')
#                    vascularNetwork.boundaryConditions = readBCFromCSV(filename = ''.join([filename,'BC','.csv']))
#                    mainGraph.update_graph(vascularNetwork, window)
#                                                           
#                elif subMenuInput == '5' and vascularNetwork.root != []:
#                    print "     write boundary conditions to CSV"
#                    print ""
#                    filename = enterFilename(filename,'')
#                    writeBCToCSV(vascularNetwork.boundaryConditions,filename = ''.join([filename,'BC','.csv']))
#                                                         
#                    
#                elif subMenuInput == 'b':
#                    break
            
        elif menuInput == "l":
            try:
                FILE = open('recentFilenames.pickle',"rb")
                # store pickle
                recentFilenames = cPickle.load(FILE)
                FILE.close()
            except:
                recentFilenames = []
                
            subMenuInput = ''
            print ""
            print "    sub menu: load data"
            print ""
            print "     [1] - load network from XML"
            print "     [2] - load vessel data from CSV"
            print "     [3] - load vessel data and boundary conditions from CSV"
            print "     [b] - back to the main menu"
            print ""
            while  subMenuInput not in ["1","2","3","b"]:
                subMenuInput = raw_input("what to do? ")
            
                print ""
                print "         resent used networks"
                i = 1
                for name in recentFilenames:
                    print "          [",i,'] - ',name
                    i = 1+i
                print ""
                if subMenuInput == '1':
                    print "     load from XML"
                    
                    filename = enterFilename(filename,'.xml',recentFilenames = recentFilenames)
                    if filename == None:break
                    # delete the old network
                    del vascularNetwork
                    #load the new network
                    vascularNetwork = loadNetworkFromXML(filename= filename)
                    if vascularNetwork == None:
                        vascularNetwork = VascularNetwork()
                        mainGraph.update_graph(vascularNetwork, window)
                        filename = None
                        break
                    
                    mainGraph.update_graph(vascularNetwork, window)
                    filename, value = filename.split(".",1)
                    break
                
                elif subMenuInput == '2':
                    print "     load vessel data from CSV - non existing vessels are added automatically"
                    print ""
                    filename = enterFilename(filename,'.csv',recentFilenames = recentFilenames)
                    if filename == None:break
                    vascularNetwork.updateNetwork(readVesselDataFromCSV(filename=filename))
                    
                    mainGraph.update_graph(vascularNetwork, window)
                    filename, value = filename.split(".",1)
                    break
                
                elif subMenuInput == '3':
                    print "     load vessel data and boundary conditions from CSV"
                    filename = enterFilename(filename,'.csv',recentFilenames = recentFilenames)
                    if filename == None:break
                    vascularNetwork.updateNetwork(readVesselDataFromCSV(filename=filename))
                    mainGraph.update_graph(vascularNetwork, window)
                    filename, value = filename.split(".",1)
                    
                    vascularNetwork.boundaryConditions = readBCFromCSV(filename = ''.join([filename,'BC','.csv']))
                    mainGraph.update_graph(vascularNetwork, window)
                    break
                
                elif subMenuInput == 'b':
                    break
            
            if filename != None:    
                if filename not in recentFilenames: recentFilenames.insert(0,filename)
                else: 
                    recentFilenames.remove(filename)
                    recentFilenames.insert(0,filename)
                if len(recentFilenames) > 5: recentFilenames.pop(-1)
                            
                FILE = open('recentFilenames.pickle',"w")
                # store pickle
                cPickle.dump(recentFilenames, FILE, protocol=2)
                FILE.close()
            
        elif menuInput == "s":
            subMenuInput = ''
            print ""
            print "    sub menu: save data"
            print ""
            print "     [1] - write to XML"
            print "     [2] - write vessel data to CSV"
            print "     [3] - write vessel data and boundary conditions to CSV"
            print "     [4] - write graph to .png and .dot files"
            print "     [b] - back to the main menu"
            print ""
            while subMenuInput not in ["1","2","3","b"]:
                subMenuInput = raw_input("what to do? ")
                     
                if subMenuInput == '1':
                    print "     write to XML"
                    filename = enterFilename(filename,'.xml')
                    if filename == None:break
                    writeNetworkToXML(vascularNetwork,filename = filename)
                    filename, value = filename.split(".",1)
                    break
                    
                elif subMenuInput == '2':
                    print "     write vessel data to CSV"
                    filename = enterFilename(filename,'.csv') 
                    if filename == None:break
                    writeVesselDataToCSV(vessels = vascularNetwork.vessels, filename = filename)
                    filename, value = filename.split(".",1)
                    break
                
                elif subMenuInput == '3':
                    print "     write vessel data and boundary conditions to CSV"
                    filename = enterFilename(filename,'.csv') 
                    if filename == None:break
                    writeVesselDataToCSV(vessels = vascularNetwork.vessels, filename = filename)
                    filename, value = filename.split(".",1)
                    writeBCToCSV(vascularNetwork.boundaryConditions,filename = ''.join([filename,'BC','.csv']))
                    break
                
                elif subMenuInput == '4':
                    print "     write graph to .png and .dot files"
                    pictureName = str(raw_input("     enter pictureName (only!):"))
                    if pictureName == "":
                        pictureName = 'pydotTest'
                    mainGraph.graph.write(graphPath+filename+'/'+pictureName+'.dot')
                    mainGraph.graph.write_png(graphPath+filename+'/'+pictureName+'.png')
                    break
                    
                if subMenuInput == 'b':
                    break
        
        elif menuInput == "u":
            subMenuInput = ''
            print ""
            print "    sub menu: update XML from CSV"
            print ""
            print "     load from XML"
            
            filename = enterFilename(filename,'.xml')
            if filename == None:break
            # delete the old network
            del vascularNetwork
            #load the new network
            vascularNetwork = loadNetworkFromXML(filename= filename)
            if vascularNetwork == None:
                vascularNetwork = VascularNetwork()
                break
            
            mainGraph.update_graph(vascularNetwork, window)
            
            filename, value = filename.split(".",1)
            
            print "     load vessel data from CSV - non existing vessels are added automatically"
            filenameCSV = ''.join([filename,'.csv'])
            vascularNetwork.updateNetwork(readVesselDataFromCSV(filename=filenameCSV))
            
            mainGraph.update_graph(vascularNetwork, window)
            
            print "     write to XML"
            filenameXML = ''.join([filename,'.xml'])
            writeNetworkToXML(vascularNetwork,filename = filenameXML)
Beispiel #3
0
def main2Dplot():
        
    parser = OptionParser()
    parser.add_option("-f", "--file", dest='networkName',
                      help="open file with networkName", metavar="FILE")
    parser.add_option("-i", "--vesselId", dest="vesselId", 
                      help="id of the vessel to visualize")
    parser.add_option("-n", "--dataNumber", dest='dataNumber',
                      help="number of the solution data (last number in filename), default = 1, max 999", metavar = "FILE")
    (options, args) = parser.parse_args()
    
    networkName = 'oldNetwork2'
    if options.networkName != None:
        networkName = options.networkName
    else: 
        print " no networkName passed, visualisation could not be intialized"
        print " use -f networkName to define an file you want to visualize"
        exit()
    filename = str(networkName+'.xml')
    
    dataNumber = '001'
    if options.dataNumber != None:
        dataNumber = options.dataNumber
        if len(dataNumber) == 1:
            dataNumber = '00'+dataNumber
        if len(dataNumber) == 2:
            dataNumber = '0'+dataNumber  
    
    vesselId = 0
    updateVesselId = False
    if options.vesselId != None:
        vesselId = int(options.vesselId)
    else:
        updateVesselId = True
    
    vascularNetwork = None
    vascularNetwork = loadNetworkFromXML(filename=filename)
    if vascularNetwork == None: exit()
    vascularNetwork.evaluateConnections()
    vascularNetwork.initialize()
    
    if vesselId not in vascularNetwork.vessels.keys():
        "No vessel with given id in current vascular network"
        sys.exit(1)
    vesselName = vascularNetwork.vessels[vesselId].name

    solutionData = []
    endName1 = "_SolutionData_"
    endName2 = ".pickle"
    networkPath = str(cur+'/../'+"/NetworkFiles/")
    filePath= ''.join([networkPath,networkName,'/',networkName,endName1,dataNumber,endName2])
    try:
        FILE = open(filePath,"rb")
        solutionData = cPickle.load(FILE)
    except:
        print "no solution file with given data number"
        exit()
        
    pRef = vascularNetwork.globalFluid['pref']
    totalTime = vascularNetwork.simulationContext['totalTime']
    
    save = True
    
    saveDirectory = ''.join([networkPath,networkName,'/2dTimePlots/'])
    
    for dir in [saveDirectory]: 
        if not os.path.exists(dir): os.makedirs(dir) 

    nodeID = 0

    nNodes = int(vascularNetwork.vessels[vesselId].N)
    
    filenames = []
    
    Pmax = np.max(solutionData[0]['Pressure'][vesselId])/133.32
    Pmin = np.min(solutionData[0]['Pressure'][vesselId])/133.32
    Qmax = np.max(solutionData[0]['Flow'][vesselId])*1e6*60
    Qmin = np.min(solutionData[0]['Flow'][vesselId])*1e6*60
    
    for n in range(0,nNodes):
    
        Psol = solutionData[0]['Pressure'][vesselId][:,[n]]
        Qsol = solutionData[0]['Flow'][vesselId][:,[n]]
    
        timeNormal = np.linspace(0,totalTime,len(Psol))
        
        fig = plt.figure(1, edgecolor='k',dpi=80)
        fig.subplots_adjust(hspace=0.5)   
        fig.set_figwidth(8.27)
        fig.set_figheight((11.69/3)*2)
        ax = plt.subplot(2,1,1)    
        ax.plot(timeNormal,Psol/133.32,color='b' ,linestyle = '-',label='total', linewidth = 1.5) 
        ax.set_ylabel('Pressure [mmHg]')
        ax.set_xlabel('Time [t]')
        ax.set_xlim(0,totalTime)
        ax.set_ylim(Pmin,Pmax)
        
        ax2 = plt.subplot(2,1,2)    
        ax2.plot(timeNormal,Qsol*1e6*60,color='r' ,linestyle = '-',label='total', linewidth = 1.5) 
        ax2.set_ylabel('Flow [ml/min]')
        ax2.set_xlabel('Time [t]')
        ax2.set_xlim(0,totalTime)
        ax2.set_ylim(Qmin,Qmax)
        
        savePath = ''.join([saveDirectory,str(vesselId).zfill(2),str(n).zfill(2),'.png'])
        filenames.append(savePath)
        plt.savefig(savePath)
        plt.clf()

    PyApp(nNodes,filenames,vesselName,updateVesselId)
    
    gtk.main()
Beispiel #4
0
def main():
    parser = OptionParser()
    parser.add_option("-f",
                      "--file",
                      dest='networkName',
                      help="open file with networkName",
                      metavar="FILE")
    parser.add_option("-s",
                      "--save",
                      dest="save",
                      help="save solution data, 0 = False, 1 = True")

    parser.add_option(
        "-n",
        "--dataNumber",
        dest='dataNumber',
        help=
        "number of the solution data (last number in filename), default = 1, max 999",
        metavar="FILE")

    parser.add_option(
        '-v',
        '--vizBool',
        dest='vizBool',
        help=
        "choose visualisation mode, 0: no visualisation, 1: 2d and 3d, 2: 2d plots, 3: 3d visualisation default = 1"
    )

    (options, args) = parser.parse_args()

    networkName = 'oldNetwork1'
    if options.networkName != None:
        networkName = options.networkName
    else:
        print " no networkName passed, visualisation could not be intialized"
        print " use -f networkName to define an file you want to visualize"
        exit()
    filename = str(networkName + '.xml')

    dataNumber = '001'
    if options.dataNumber != None:
        dataNumber = options.dataNumber
        if len(dataNumber) == 1:
            dataNumber = '00' + dataNumber
        if len(dataNumber) == 2:
            dataNumber = '0' + dataNumber

    save = False
    if options.save != None:
        if options.save == '0':
            save = False
        elif options.save == '1':
            save = True

    vizBool = '1'
    vizBool2d = True
    vizBool3d = True
    print options.vizBool
    if options.vizBool != None:
        if options.vizBool == '0':
            vizBool2d = False
            vizBool3d = False
        elif options.vizBool == '1':
            vizBool2d = True
            vizBool3d = True
        elif options.vizBool == '2':
            vizBool2d = True
            vizBool3d = False
        elif options.vizBool == '3':
            vizBool2d = False
            vizBool3d = True

    # load network from the path!
    Lstart = time.clock()
    vascularNetwork = loadNetworkFromXML(filename=filename)

    if vascularNetwork == None: exit()
    # update the connections of the network, as they prob weren't saved correct
    vascularNetwork.evaluateConnections()
    Lend = time.clock()
    print " needed %1.3f  sec to load" % (Lend - Lstart)

    vascularNetwork.initialize()

    #create visualisation
    if vizBool3d == True:
        visualisation = Visualisation(vascularNetwork=vascularNetwork)

    #print '====================================='
    #print '_________ Simulation Context _________'
    #print '%-25s %-20s' % ('Numerical scheme',networkSolver['NumScheme'])
    #print '%-25s %4d' % ('Equation system',int(networkSolver['EqSystem']))
    #print '%-25s %4d' % ('Characteristic system',int(networkSolver['CharSystem']))
    #print '________ Visualisation Context _______'

    Cstart = time.clock()
    flowSolver = FlowSolver(vascularNetwork)
    P, Q, A = flowSolver.solve()
    print '\n____________ Solver time _____________'
    minutes = int((time.clock() - Cstart) / 60.)
    secs = (time.clock() - Cstart) - float(minutes) * 60.
    print '%d %s %f %s' % (minutes, 'min', secs, 'sec')

    if save == True:
        #save solution data in c pickle
        #standart solution path
        networkPath = str(cur + "/NetworkFiles/")
        networkDirectory = filename.split('.')[0]
        solutionPath = str(networkPath + networkDirectory + '/')
        # data to save
        solutionDataSave = [{
            'Pressure': P,
            'Flow': Q,
            'Area': A,
            'WaveSpeed': Q,
            'Name': str('simulation_' + dataNumber)
        }]
        #create file with filename in soultionPath
        endName = "_SolutionData_" + dataNumber + ".pickle"
        FILE = open(str(solutionPath + vascularNetwork.name + endName), "w")
        # store pickle
        cPickle.dump(solutionDataSave, FILE, protocol=2)
        FILE.close()

    if vizBool2d == True:
        string = ' '.join([
            'python', cur + '/Visualisation/class2dVisualisationSimple.py',
            '-f', vascularNetwork.name, '-n 1', '-i 0'
        ])
        subprocess.call(string, shell=True)

    if vizBool3d == True:
        # create solution data
        solutionData = [{
            'Pressure': P,
            'Flow': Q,
            'Area': A,
            'Name': str('simulation ' + dataNumber)
        }]
        #set the solution data
        visualisation.setSolutionData(solutionData)
        visualisation.factor = 10
        # set the coefficient for the velocity profile
        visualisation.powerLawCoefficient = 2
        visualisation.visualize()
        # clear memory
        visualisation.__del__()
        del (visualisation)