Esempio n. 1
0
    def _simulate(self,event):
        if self.params.getParam(parameters.Mode_is_structured).getValue():
            studyName = self.params.getParam(parameters.Title).getValue()
            dataBase = self.params.getParam(parameters.CurrentDatabasePath).getValue()
            darcy = self.params.getParam(parameters.DarcyVelocity_list).getValue()
            if darcy != []:
                darcyVelocity = str(darcy[0])+","+str(darcy[1])+","+str(darcy[2])
            initialTime = self.params.getParam(parameters.Iterate_InitialTime).getValue()
            finalTime = self.params.getParam(parameters.Iterate_SimulationTime).getValue()
            simulationTime = "["+str(initialTime)+","+str(finalTime)+"]"
            structuredCase = open (studyName+".py",'w')
            
            structuredCase.write("from constant import epspH\n")
            structuredCase.write("from datamodel import *\n")
            structuredCase.write("import fields\n")
            structuredCase.write("from listtools import normMaxListComparison, subtractLists\n")
            structuredCase.write("from chemicaltransport import *\n")
            structuredCase.write("from chemicaltransportmodule import *\n")

            structuredCase.write("from mt3d import Mt3d              # mt3d\n")
            structuredCase.write("from cartesianmesh import *        # Cartesian mesh\n")
            structuredCase.write("from phreeqc import *              # phreeqc\n")
            structuredCase.write("\n")
            structuredCase.write("import os\n")

            structuredCase.write("Phreeqc_file = \""+studyName+".txt\"      # bounded to Phreeqc\n")
            structuredCase.write("ProblemName  = \""+studyName+"\"          # Phreeqc file \n")
            structuredCase.write("mesh = CartesianMesh2D(\"global\",\"XY\")\n")
            nx = self.params.getParam(parameters.MeshLineInINumberOfCells).getValue()
            ny = self.params.getParam(parameters.MeshLineInJNumberOfCells).getValue()
            structuredCase.write("nx  = "+str(nx)+"\n")
            structuredCase.write("ny  = "+str(ny)+"\n")
#
# writing the mesh extension
#            
            structuredCase.write("#~~~~~~~~~~~~~~~~~~\n")
            structuredCase.write("#~ Mesh extension ~\n")
            structuredCase.write("#~~~~~~~~~~~~~~~~~~\n")
            structuredCase.write("deltax = []\n")
            xlist = self.params.getParam(parameters.Mesh_list_InI).getValue()
            ind = 0
            while xlist[ind][0] != 0:
                structuredCase.write("dx = ["+str(xlist[ind][1])+"/"+str(xlist[ind][0])+"]*"+str(xlist[ind][0])+"\n")
                structuredCase.write("deltax.extend(dx)\n")
                ind+=1
            structuredCase.write("deltay = []\n")
            ylist = self.params.getParam(parameters.Mesh_list_InJ).getValue()
            ind = 0
            while ylist[ind][0] != 0:
                structuredCase.write("dy = ["+str(ylist[ind][1])+"/"+str(ylist[ind][0])+"]*"+str(ylist[ind][0])+"\n")
                structuredCase.write("deltay.extend(dy)\n")
                ind+=1
            structuredCase.write("mesh.setdAxis(\"X\",deltax)\n")
            structuredCase.write("mesh.setdAxis(\"Y\",deltay)\n")
            zone_list = self.params.getParam(parameters.Zones_list).getValue()
            zone_Material_AqueousState_list = self.params.getParam(parameters.Zone_Material_AqueousState_list).getValue()
            ind = 0
            for zone in zone_list:
                structuredCase.write(str(zone)+str(ind)+"Body = CartesianMesh2D(\""+str(zone)+"\", \"XY\")\n")
                imin = zone_Material_AqueousState_list[ind][2]
                imax = zone_Material_AqueousState_list[ind][3]
                jmin = zone_Material_AqueousState_list[ind][4]
                jmax = zone_Material_AqueousState_list[ind][5]
                string = str(zone)+str(ind)
                structuredCase.write(string+"Body.setZone(\""+str(zone)+"\",index_min = Index2D ("+str(imin)+","+str(jmin)+\
                "), index_max = Index2D ("+str(imax)+","+str(jmax)+"))\n")
                ind+=1
                
            structuredCase.write("#~~~~~~~~~~~~~\n")
            structuredCase.write("#~ Materials ~\n")
            structuredCase.write("#~~~~~~~~~~~~~\n")
            
            materials = self.params.getParamValue(parameters.CUSTOM_MATERIAL_DB).getMaterials()
            for toto in materials.keys():
                materialName = str(toto)+"Material"
                longDisp =  str(materials[toto]["LongitudinalDispersivity"])
                transDisp = str(materials[toto]["TransverseDispersivity"])
                structuredCase.write(materialName+" = Material (name = \""+str(toto)+"\",")
                structuredCase.write("effectiveDiffusion = EffectiveDiffusion ("+str(materials[toto]["EffectiveDiffusion"])+",unit=\"m**2/s\"),\\")
                structuredCase.write("\npermeability = Permeability(value = "+str(materials[toto]["KxPermeability"])+"),\\")
                structuredCase.write("\nporosity = Porosity(value = "+str(materials[toto]["EffectivePorosity"])+"),\\")
                structuredCase.write("\nkinematicDispersion = KinematicDispersion ("+longDisp+","+transDisp+"))\n")
                
#            print materials

            structuredCase.write("\n#~~~~~~~~~~~\n")
            structuredCase.write("#~ Regions ~\n")
            structuredCase.write("#~~~~~~~~~~~\n")
             
            ind = 0
            regionList = "["
            for zone in zone_list:
                if ind!=0: regionList+=","
                materialName = str(zone_Material_AqueousState_list[ind][0])+"Material"
                structuredCase.write(str(zone)+str(ind)+"Region = Region(support="+str(zone)+str(ind)+"Body, material= "+materialName+")\n")
                regionList+=str(zone)+str(ind)+"Region"
                ind+=1
            regionList+="]"
#
#
            _chemical(self.params,structuredCase)
#
#
#
#chemical_state_quartz = ChemicalState ("column", AqueousSolution_column, columnMineralPhase)
#
            structuredCase.write("#~~~~~~~~~~~~~~~~~~~~\n")
            structuredCase.write("# Initial condition ~\n")
            structuredCase.write("#~~~~~~~~~~~~~~~~~~~~\n")
            initialConditionList = self.params.getParam(parameters.InitialConditions_list).getValue()
            print "initialConditionList: ",initialConditionList
            boundaryConditionList = self.params.getParam(parameters.BoundaryConditions_list).getValue()
            iCLS = "["
            for iC in initialConditionList:
                iCName = str(iC)+"IC"
                iCLS+=iCName+","
                iCParameters = zone_Material_AqueousState_list[zone_list.index(iC)]
                materialName = str(iCParameters[0]) + "Material"
                aqueousStateName = str(iCParameters[1])+"ChemicalState"
                structuredCase.write(iCName+" = InitialCondition (body  = "+str(iC)+str(zone_list.index(iC))+"Body, value = "+aqueousStateName+")\n")
            iCLS = iCLS[0:len(iCLS)-1]
            iCLS += "]"
            structuredCase.write("#~~~~~~~~~~~~~~~~~~~~~\n")
            structuredCase.write("# Boundary condition ~\n")
            structuredCase.write("#~~~~~~~~~~~~~~~~~~~~~\n")
            bCLS = "["
            for iC in boundaryConditionList:
                iCName = str(iC)+"BC"
                bCLS+=iCName+","
                iCParameters = zone_Material_AqueousState_list[zone_list.index(iC)]
                materialName = str(iCParameters[0])+"Material"
                aqueousStateName = str(iCParameters[1])+"ChemicalState"
                structuredCase.write(iCName+" = BoundaryCondition (boundary = "+str(iC)+str(zone_list.index(iC))+"Body, btype=\"Dirichlet\", value = "+aqueousStateName+")\n")
                
#bIC = BoundaryCondition (boundary = b0Zone, btype='Dirichlet', value = sodaChemicalState)
                
            bCLS = bCLS[0:len(bCLS)-1]
            bCLS += "]"
            structuredCase.write("#~~~~~~~~~~~~~~~~~~~\n")
            structuredCase.write("# Expected outputs ~\n")
            structuredCase.write("#~~~~~~~~~~~~~~~~~~~\n")
            outputList = self.params.getParam(parameters.ExpectedOutputs_list).getValue()
#            print "outputList",outputList
            if outputList != []:
                string = "expectedOutputs = ["
                for out in outputList:
                    if str(out) == "pH":
                        string+="ExpectedOutput(\"pH\",format=\"table\",name=\"pH_output\"),\n"
                    else:
                        string+="ExpectedOutput(\"Concentration\",\""+out+"\",format=\"table\",name=\""+out+"Output\"),\n"
                string = string[0:-2]+"]\n"
                structuredCase.write(string)
            else:        
                structuredCase.write("expectedOutputs = []\n")
                
            structuredCase.write("#~~~~~~~~~\n")
            structuredCase.write("# Module ~\n")
            structuredCase.write("#~~~~~~~~~\n")
            
            structuredCase.write("module = ChemicalTransportModule()\n")

            structuredCase.write("problem  = ChemicalTransportProblem(name               = \""+\
            str(studyName)+"\",\\")
            structuredCase.write("\n                                    regions            = "+regionList+",\\\n")
            structuredCase.write("                                    initialConditions  = "+iCLS+",\\\n")
            structuredCase.write("                                    boundaryConditions = "+bCLS+",\\\n")
            structuredCase.write("                                    calculationTimes   = "+simulationTime+",\\\n")
            structuredCase.write("                                    sources            = None,\\\n")
            if darcy == []:
                structuredCase.write("                                    darcyVelocity      = None,\\\n")
            else:
                structuredCase.write("                                    darcyVelocity      = Velocity(Vector(["+darcyVelocity+"])),\\\n")
            #
            # The database should be placed in the Phreeqc_dat directory
            #
            dataBase = os.path.split(dataBase)[-1]
            structuredCase.write("                                    chemistryDB        = \""+dataBase+"\",\\\n")
            structuredCase.write("                                    speciesBaseAddenda = speciesAddenda,\\\n")
            structuredCase.write("                                    kineticLaws        = None,\\\n")
            structuredCase.write("                                    activityLaw        = None,\\\n")
            structuredCase.write("                                    outputs            = expectedOutputs)\n")

            algorithm = self.params.getParam(parameters.Iterate_Algorithm).getValue()
            structuredCase.write("module.setData (problem, trace = 0, mesh = mesh, algorithm=\""+algorithm+"\")\n")
            structuredCase.write("\nmodule.setComponent(\"mt3d\",\"phreeqc\")\n") 
            scheme = self.params.getParam(parameters.Mt3d_advection).getValue()
            structuredCase.write("module.setTransportParameters(\""+scheme+"\")\n")
            preconditionner = self.params.getParam(parameters.Mt3d_ConjugateGradientPreconditioner).getValue()
            epsPred = str(self.params.getParam(parameters.Mt3d_cclose).getValue())
            structuredCase.write("module.setTransportParameters(\""+preconditionner+"\","+epsPred+")\n")

            if str(algorithm) == "NI":
                minStepSize = self.params.getParam(parameters.Iterate_InitialTimeStepSize).getValue()
                maxStepSize = self.params.getParam(parameters.Iterate_InitialTimeStepSize).getValue()
            else:
                minStepSize = self.params.getParam(parameters.Iterate_MinTimeStep).getValue()
                maxStepSize = self.params.getParam(parameters.Iterate_MaxTimeStep).getValue()

            structuredCase.write("module.setCouplingParameter(initialTimeStep        = "+\
            str(self.params.getParam(parameters.Iterate_InitialTimeStepSize).getValue())+","+\
            "\n                            minTimeStep            = "+\
            str(minStepSize)+","\
            "\n                            maxTimeStep            = "+\
            str(maxStepSize)+","\
            "\n                            couplingPrecision      = "+\
            str(self.params.getParam(parameters.Iterate_CouplingPrecision).getValue())+","\
            "\n                            optimalIterationNumber = "+\
            str(self.params.getParam(parameters.Iterate_PicardTargetNumber).getValue())+","\
            "\n                            maxIterationNumber     = "+\
            str(self.params.getParam(parameters.Iterate_PicardMaxOfIterations).getValue())+","\
            "\n                            decreaTimeStepCoef     = "+\
            str(self.params.getParam(parameters.Iterate_RelaxationMinFactor).getValue())+","\
            "\n                            increaTimeStepCoef     = "+\
            str(self.params.getParam(parameters.Iterate_RelaxationMaxFactor).getValue())+")")
            if self.params.getParam(parameters.PostprocessingContours_AqueousComponentsToPlot_list).getValue() != []:
                structuredCase.write("\n#~~~~~~~~~~~\n")
                structuredCase.write("# Contours ~\n")
                structuredCase.write("#~~~~~~~~~~~\n")
                string = "module.setVtkOutputsParameters(["
                for species in self.params.getParam(parameters.PostprocessingContours_AqueousComponentsToPlot_list).getValue():
                    string += "\""+str(species)+"\","
                string = string[0:len(string)-1]+"],"
                string += "\""+str(self.params.getParam(parameters.PostprocessingContours_Parameters_list).getValue()[1])+"\","
                string += str(self.params.getParam(parameters.PostprocessingContours_Parameters_list).getValue()[0])+")\n"
                structuredCase.write(string)
#           module.setVtkOutputsParameters(["Na"],"days",2)
            structuredCase.write("\nmodule.run()\n")
            if string!= "":
                structuredCase.write("#~~~~~~~~~~~~~~~~~~\n")
                structuredCase.write("# Post processing ~\n")
                structuredCase.write("#~~~~~~~~~~~~~~~~~~\n")
                string = self.params.getParam(parameters.PyOutputHandler).getValue()
                structuredCase.write(string)
            lenString = len("        End of the ")+len(studyName)+len(" case ~")
            en = "~"*lenString   
            structuredCase.write("\nprint \""+en+"\"\n")
            structuredCase.write("print \"        End of the "+studyName+" case ~\"") 
            structuredCase.write("\nprint \""+en+"\"\n")
            structuredCase.write("\nmodule.end()\n")
            
            structuredCase.close()
            #
            # Running the generated file in a PyShell
            #
            from wx import py
            import wx.py as py
            #namespace = { 'wx'    : wx,
            #              'app'   : wx.GetApp(),
            #              'frame' : self,
            #              }
            #DEFAULT_SHELL_WINDOW_SIZE = (640,480)
            #self.shell = py.shell.ShellFrame(None, title = " interactive launch browser", locals=namespace)
            #self.shell.SetSize( DEFAULT_SHELL_WINDOW_SIZE )
            #self.shell.Show()
            os.system("python "+studyName+".py")
            #toto  = Myne("Coupling browser",studyName+".py")
        else:  # case of an unstructured file. In that case, we should restructure that if/elif statement
            print("noch zu machen")
        return True
Esempio n. 2
0
    def _simulate(self,event):
#        if not os.path.isdir("/home/david/Wrapper/Api/Geoi/src/geoi/Simulation"):
#     	    os.mkdir("/home/david/Wrapper/Api/Geoi/src/geoi/Simulation")    
#     	else :
#     		pass

     	nameFile = self.params.getParam(parameters.Title).getValue()
     	params=self.params
     	self.retpython = None
#     	directory = params.getParam(parameters.ResultDirectory).getValue()
     	 
#     	os.chdir(directory)
#     	print directory
     	pythonFile = os.getcwd()+"/"+nameFile+".py"
     	f1=open(pythonFile,"w") 
#
# elmer imports
#
        _elmerimport(f1)
      	
     	f1.write("setProblemType(\"ChemicalTransport\")\n\n")
#
# elmer mesh import
#
     	f1.write("#~~~~~~~~~~~~~~~~~~~~~~\n")
     	f1.write("#   Mesh definition\n")
     	f1.write("#~~~~~~~~~~~~~~~~~~~~~~\n")
#
     	_meshimport(self.params,f1)
#     	
     	zone_list = self.params.getParam(parameters.Zones_list).getValue()
     	i=0
     	for zone in zone_list:
     	    f1.write(str(zone)+str(i)+"Body = mesh.getBody(\""+str(zone)+"\")\n")
     	    f1.write(str(zone)+str(i)+"Body.getNodesNumber()\n")
     	    i+=1

        f1.write("numberOfVertices = mesh._getNumberOfVertices()\n")
#
# materials
#
        f1.write("#~~~~~~~~~~~~~~~~~~~~~~\n")
        f1.write("#   Material\n")
        f1.write("#~~~~~~~~~~~~~~~~~~~~~~\n")
#
        _materials(self.params,f1)
#
# regions
#
        f1.write("\n#~~~~~~~~~~~\n")
        f1.write("#~ Regions ~\n")
        f1.write("#~~~~~~~~~~~\n")
#
        zone_Material_AqueousState_list, regionList = _regions(self.params,f1)     
#
# chemistry
#
        _chemical(self.params,f1)
#
#
#
#chemical_state_quartz = ChemicalState ("column", AqueousSolution_column, columnMineralPhase)
#
        f1.write("#~~~~~~~~~~~~~~~~~~~~\n")
        f1.write("# Initial condition ~\n")
        f1.write("#~~~~~~~~~~~~~~~~~~~~\n")
        initialConditionList = self.params.getParam(parameters.InitialConditions_list).getValue()

        boundaryConditionList = self.params.getParam(parameters.BoundaryConditions_list).getValue()
        iCLS = "["
        for iC in initialConditionList:
            iCName = str(iC)+"IC"
            iCLS+=iCName+","
            iCParameters = zone_Material_AqueousState_list[zone_list.index(iC)]
            materialName = str(iCParameters[0]) + "Material"
            aqueousStateName = str(iCParameters[1])+"ChemicalState"
            f1.write(iCName+" = InitialCondition (body  = "+str(iC)+str(zone_list.index(iC))+"Body, value = "+aqueousStateName+")\n")
        if iCLS != "[": iCLS = iCLS[0:len(iCLS)-1]
        iCLS += "]"
        f1.write("#~~~~~~~~~~~~~~~~~~~~~\n")
        f1.write("# Boundary condition ~\n")
        f1.write("#~~~~~~~~~~~~~~~~~~~~~\n")
        bCLS = "["
        for iC in boundaryConditionList:
            iCName = str(iC)+"BC"
            bCLS+=iCName+","
            iCParameters = zone_Material_AqueousState_list[zone_list.index(iC)]
            materialName = str(iCParameters[0])+"Material"
            aqueousStateName = str(iCParameters[1])+"ChemicalState"
            f1.write(iCName+" = BoundaryCondition (boundary = "+str(iC)+str(zone_list.index(iC))+"Body, btype=\"Dirichlet\", value = "+aqueousStateName+")\n")
            
#bIC = BoundaryCondition (boundary = b0Zone, btype='Dirichlet', value = sodaChemicalState)
            
        if bCLS != "[": bCLS = bCLS[0:len(bCLS)-1]
        bCLS += "]"
#
# outputs
#
        f1.write("#~~~~~~~~~~~~~~~~~~~\n")
        f1.write("# Expected outputs ~\n")
        f1.write("#~~~~~~~~~~~~~~~~~~~\n")
#
        _outputs(self.params,f1)
#
# module
#            
        f1.write("#~~~~~~~~~\n")
        f1.write("# Module ~\n")
        f1.write("#~~~~~~~~~\n")
#
        _problem(self.params,f1, regionList, iCLS, bCLS)
        
        _chemicaltransportmodule(self.params,f1,"Elmer")
        
        species = self.params.getParam(parameters.PostprocessingContours_AqueousComponentsToPlot_list).getValue()
        frequency = self.params.getParam(parameters.PostprocessingContours_Parameters_list).getValue()
        if len(species)>0 :
            f1.write("module.setVtkOutputsParameters([") 
            for ind in range(len(species)-1) :
                f1.write("\""+species[ind]+"\",")
            f1.write("\""+species[len(species)-1]+"\"]," +  "\"" + str(frequency[1]) + "\"," + frequency[0] + ")\n\n")   
#        
# algebraic resolution
#        
        _algebraicResolution(self.params,f1)

#
# running module 
#

#        f1.write("#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n")
#        f1.write("module.run()\n")
#        f1.write("#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n")
        
        f1.write("#~~~~~~~~~~~~~~~~~~~~~~~~~~\n")
        f1.write("#  transient algorithm    ~\n")
        f1.write("#~~~~~~~~~~~~~~~~~~~~~~~~~~\n")
        
        _moduleLooping(self.params,f1)

#        
# post processing
#        
        f1.write("\n\n")
        f1.write("#~~~~~~~~~~~~~~~~~~~~\n")
        f1.write("#  Post Processing  ~\n")
        f1.write("#~~~~~~~~~~~~~~~~~~~~\n")
      
        _postProcessing(self.params,f1)
        
        f1.flush()
        f1.close()
        while f1.closed == False:
            pass
        
#        wx.MessageDialog(self.parent, "Regarde le Terminal", "Warning", wx.OK | wx.ICON_WARNING).ShowModal()
#        os.chdir(directory)
        
#        try:
#            retcode = subprocess.Popen("rm -f commandfile.eg", shell = True)
#            if retcode < 0:
#                print >>sys.stderr, "Child was terminated by signal", -retcode
#            else:
#                print >>sys.stderr, "Child returned", retcode
#        except OSError, e:
#            print >>sys.stderr, "Execution failed:", e

#        try:
#            retcode = subprocess.Popen("rm -rf " + nameFile , shell = True)
#            if retcode < 0:
#                print >>sys.stderr, "Child was terminated by signal", -retcode
#            else:
#                print >>sys.stderr, "Child returned", retcode
#        except OSError, e:
#            print >>sys.stderr, "Execution failed:", e    
        self.retpython = subprocess.Popen("python  " +nameFile+".py", shell = True)