Esempio n. 1
0
File: nls.py Progetto: lokik/sfepy
    def __init__(self, conf, pmtx=None, prhs=None, comm=None, **kwargs):
        if comm is None:
            try:
                import petsc4py
                petsc4py.init([])
            except ImportError:
                msg = 'cannot import petsc4py!'
                raise ImportError(msg)

        from petsc4py import PETSc as petsc

        converged_reasons = {}
        for key, val in six.iteritems(petsc.SNES.ConvergedReason.__dict__):
            if isinstance(val, int):
                converged_reasons[val] = key

        ksp_converged_reasons = {}
        for key, val in six.iteritems(petsc.KSP.ConvergedReason.__dict__):
            if isinstance(val, int):
                ksp_converged_reasons[val] = key

        NonlinearSolver.__init__(self, conf, petsc=petsc,
                                 pmtx=pmtx, prhs=prhs, comm=comm,
                                 converged_reasons=converged_reasons,
                                 ksp_converged_reasons=ksp_converged_reasons,
                                 **kwargs)
Esempio n. 2
0
    def __init__( self, conf, **kwargs ):
        try:
            import petsc4py
            petsc4py.init([])
            from petsc4py import PETSc
        except ImportError:
            msg = 'cannot import petsc4py!'
            raise ImportError( msg )

        LinearSolver.__init__(self, conf, eps_a=conf.eps_a, eps_r=conf.eps_r,
                              petsc=PETSc, pmtx=None, **kwargs)

        ksp = PETSc.KSP().create()

        ksp.setType( self.conf.method )
        ksp.getPC().setType( self.conf.precond )
        side = self._precond_sides[self.conf.precond_side]
        if side is not None:
            ksp.setPCSide(side)
        self.ksp = ksp

        self.converged_reasons = {}
        for key, val in ksp.ConvergedReason.__dict__.iteritems():
            if isinstance(val, int):
                self.converged_reasons[val] = key
Esempio n. 3
0
    def updatesolution(self,state):
        r"""
        Compute slution at the new time level for the implicit 1D
        Lax-Wendroff scheme, i.e.

        q^(n+1) = q^(n) + R(q^(n+1)),

        where q^(n)      = solution at the current time level
              q^(n+1)    = solution at the new time level (solution of the nonlinear system)
              R(q^(n+1)) = nonlinear function arising from the spatial/time discretization
        """

        import numpy as np
        import sys, petsc4py
        petsc4py.init(sys.argv)
        from petsc4py import PETSc
        
        
        # Define aux array
        maux = state.maux
        mx = state.grid.ng[0]
        mwaves,mbc = self.mwaves,self.mbc

        if maux>0:
            aux = self.auxbc(state)
        else:
            aux=np.empty((maux,mx+2*mbc))

        # Create application context (appc) and PETSc nonlinear solver
        appc = ImplicitLW1D(state,mwaves,mbc,self.method,self.mthlim,self.dt,aux,self.kernel_language)
        snes = PETSc.SNES().create()
        
        # Define the vector in charge of containing the solution of the 
        # nonlinear system. The initial guess is qnew = q^n, i.e. solution at 
        # the current time level t^n. 
        qnew = state.qbc.copy()
                
        # Define the function in charge of computing the nonlinear residual.
        f = PETSc.Vec().createSeq(qnew.size)

        # Define the constant part of the equation.
        # For the implicit LW scheme this could either zero or the solution at 
        # the current time level (q^n). In this case we set it equal to the 
        # solution at the current time level.
        b = qnew.copy()

        #  Register the function in charge of computing the nonlinear residual
        snes.setFunction(appc.evalNonLinearFunction, f)
         
        # Configure the nonlinear solver to use a matrix-free Jacobian
        snes.setUseMF(True)
        snes.getKSP().setType('cg')
        snes.setFromOptions()

        # Solve the nonlinear problem
        snes.solve(b, qnew)

        # Assign to q the new value qnew.
        state.qbc = qnew
Esempio n. 4
0
def run_app_from_main(application,setplot=None):
    r"""
    Runs an application from pyclaw/examples/, automatically parsing command line keyword
    arguments (key=value) as parameters to the application, with positional
    arguments being passed to PETSc (if it is enabled).

    Perhaps we should take the PETSc approach of having a database of PyClaw
    options that can be queried for options on specific objects within the
    PyClaw runtime instead of front-loading everything through the application
    main...
    """

    # Arguments to the PyClaw should be keyword based, positional arguments
    # will be passed to PETSc
    petsc_args, pyclaw_kwargs = _info_from_argv(sys.argv)

    if 'use_petsc' in pyclaw_kwargs and pyclaw_kwargs['use_petsc']:
        import petsc4py
        petsc_args = [arg.replace('--','-') for arg in sys.argv[1:] if '=' not in arg]
        petsc4py.init(petsc_args)
        from clawpack import petclaw as pyclaw
    else:
        from clawpack import pyclaw

    if sys.version_info >= (2, 7):
        app_kwargs = {key: value for key, value in pyclaw_kwargs.items() 
                      if not key in ('htmlplot','iplot')}
    else:
        # the above fails with Python < 2.7, so write it out...
        app_kwargs = {}
        for key,value in pyclaw_kwargs.items():
            if key not in ('htmlplot','iplot'):
                app_kwargs[key] = value

    claw=application(**app_kwargs)

    # Solve
    status = claw.run()

    # Plot results
    htmlplot = pyclaw_kwargs.get('htmlplot',False)
    iplot    = pyclaw_kwargs.get('iplot',False)
    outdir   = pyclaw_kwargs.get('outdir','./_output')
    if htmlplot:  
        if setplot is not None:
            pyclaw.plot.html_plot(outdir=outdir,setplot=setplot)
        else:
            pyclaw.plot.html_plot(outdir=outdir)
    if iplot:     
        if setplot is not None:
            pyclaw.plot.interactive_plot(outdir=outdir,setplot=setplot)
        else:
            pyclaw.plot.interactive_plot(outdir=outdir)

    return claw
Esempio n. 5
0
    def __init__(self, conf, **kwargs):
        try:
            import petsc4py
            petsc4py.init([])
            from petsc4py import PETSc
        except ImportError:
            msg = 'cannot import petsc4py!'
            raise ImportError(msg)

        LinearSolver.__init__(self, conf, petsc=PETSc, pmtx=None,
                              converged_reasons=None, **kwargs)
def generate_commandline(filename):
    infile = open(filename, 'r')
    args = [line[:-1] for line in infile if not line.strip().startswith('#')]
    infile.close()

    if any('use_old_options_style' in line for line in args):
        raise NotImplementedError("Old style options can't be used here... generate a PETSc-style options file and try again")
    else:
        import petsc4py
        petsc4py.init(' '.join(args))
        from petsc4py import PETSc
    return
Esempio n. 7
0
File: nls.py Progetto: Gkdnz/sfepy
    def __init__(self, conf, pmtx=None, prhs=None, comm=None, **kwargs):
        if comm is None:
            try:
                import petsc4py
                petsc4py.init([])
            except ImportError:
                msg = 'cannot import petsc4py!'
                raise ImportError(msg)

        from petsc4py import PETSc as petsc

        NonlinearSolver.__init__(self, conf, petsc=petsc,
                                 pmtx=pmtx, prhs=prhs, comm=comm, **kwargs)
    def solve_petsc(self, solver_data_list, rhs_list, nu, *args, **kwargs ):
        #try catch for the petsc4py use in multiple rhs solve
        try:
            import petsc4py
            petsc4py.init(sys.argv)
            from petsc4py import PETSc
            from pysit.util.wrappers.petsc import PetscWrapper

        except ImportError:
            raise ImportError('petsc4py is not installed, please install it and try again')


        petsc = kwargs['petsc']
        if petsc is not None:
            if len(solver_data_list) != len(rhs_list):
                raise ValueError('solver and right hand side list must be the same size')
            else:
                #Building the Helmholtz operator for petsc
                H = self._build_helmholtz_operator(nu)
                ndof = H.shape[1]
                nshot = len(rhs_list)

                # creating the B rhs Matrix            
                B = PETSc.Mat().createDense([ndof, nshot])
                B.setUp()
                for i in range(nshot):
                    # added by zhilong [0:ndof]
                    B.setValues(list(range(0, ndof)), [i], rhs_list[i][0:ndof])

                B.assemblyBegin()
                B.assemblyEnd()

                # call the wrapper to solve the system
                wrapper = PetscWrapper()
                try:
                    linear_solver = wrapper.factorize(H, petsc, PETSc.COMM_WORLD)
                    Uhat = linear_solver(B.getDenseArray())
                except:
                    raise SyntaxError('petsc = '+str(petsc)+' is not a correct solver you can only use \'superlu_dist\', \'mumps\' or \'mkl_pardiso\' ')
                

                numb = 0
                for solver_data in solver_data_list:
                    u = Uhat[:,numb]
                    u = np.append(u, np.zeros(2*ndof)) # added by zhilong, assume that we do not need the auxiliary wavefields
                    u.shape = solver_data.k.data.shape 
                    solver_data.k.data = u
                    numb += 1
Esempio n. 9
0
def help(args=None):
    import sys, shlex
    # program name
    try:
        prog = sys.argv[0]
    except Exception:
        prog = getattr(sys, 'executable', 'python')
    if args is None:
        args = sys.argv[1:]
    elif isinstance(args, str):
        args = shlex.split(args)
    else:
        args = [str(a) for a in args]
    import petsc4py
    petsc4py.init([prog, '-help'] + args)
    from petsc4py import PETSc
    COMM = PETSc.COMM_SELF
    if 'vec' in args:
        vec = PETSc.Vec().create(comm=COMM)
        vec.setSizes(0)
        vec.setFromOptions()
        del vec
    if 'mat' in args:
        mat = PETSc.Mat().create(comm=COMM)
        mat.setSizes([0, 0])
        mat.setFromOptions()
        del mat
    if 'ksp' in args:
        ksp = PETSc.KSP().create(comm=COMM)
        ksp.setFromOptions()
        del ksp
    if 'pc' in args:
        pc = PETSc.PC().create(comm=COMM)
        pc.setFromOptions()
        del pc
    if 'snes' in args:
        snes = PETSc.SNES().create(comm=COMM)
        snes.setFromOptions()
        del snes
    if 'ts' in args:
        ts = PETSc.TS().create(comm=COMM)
        ts.setFromOptions()
        del ts
    if 'da' in args:
        da = PETSc.DA().create(comm=COMM)
        da.setFromOptions()
        del da
Esempio n. 10
0
def main(args):
   try:
      import mpi4py
      from mpi4py import MPI 
   except:
      raise mpi4pyError('mpi4py must be installed and in PYTHONPATH')
   try:
      import petsc4py
      comm = MPI.COMM_WORLD      
      petsc4py.init([],comm=comm)
      from petsc4py import PETSc
   except:
      raise petsc4pyError('petsc4py must be installed and in PYTHONPATH')

   p = _reconparser.Parser(args[0])
   solve = _reconsolver.Solver(p)
   comm.Barrier()
   solve.PPS(p,comm)
Esempio n. 11
0
    def __init__(self, conf, comm=None, **kwargs):
        if comm is None:
            try:
                import petsc4py
                petsc4py.init([])
            except ImportError:
                msg = 'cannot import petsc4py!'
                raise ImportError(msg)

        from petsc4py import PETSc as petsc

        converged_reasons = {}
        for key, val in petsc.KSP.ConvergedReason.__dict__.iteritems():
            if isinstance(val, int):
                converged_reasons[val] = key

        LinearSolver.__init__(self, conf, petsc=petsc, comm=comm,
                              converged_reasons=converged_reasons, **kwargs)
    def solve_petsc_uhat(self, solver, rhs_list, frequency, petsc='mumps', *args, **kwargs):
        #try catch for the petsc4py use in multiple rhs solve
        #use only in the data generation where we do not need to compute
        #the system for the whole auxiliary fields
        try:
            import petsc4py
            petsc4py.init(sys.argv)
            from petsc4py import PETSc
            from pysit.util.wrappers.petsc import PetscWrapper
        except ImportError:
            raise ImportError('petsc4py is not installed, please install it and try again')

        #Building the Helmholtz operator for petsc
        H = self._build_helmholtz_operator(frequency)
        
        ndof = H.shape[1]
        nshot = len(rhs_list)
        # if the compact operator is used we do not need to slice the solution
        if self.compact:
            usize = ndof
        else:
            nwfield = len(self.WavefieldVector.aux_names) + 1
            usize = ndof/nwfield

        # creating the B rhs Matrix
        B = PETSc.Mat().createDense([ndof, nshot])
        B.setUp()
        for i in range(nshot):
            B.setValues(range(0, ndof), [i], rhs_list[i])

        B.assemblyBegin()
        B.assemblyEnd()

        # call the wrapper to solve the system
        wrapper = PetscWrapper()
        try:
            linear_solver = wrapper.factorize(H, petsc, PETSc.COMM_WORLD)
            Uhat = linear_solver(B.getDenseArray())
        except:
            raise SyntaxError('petsc = '+str(petsc)+' is not a correct solver you can only use \'superlu_dist\', \'mumps\' or \'mkl_pardiso\' ')               
        
        Uhat = Uhat[xrange(usize),:]

        return Uhat
Esempio n. 13
0
def init():
    global comm, petscInitialized
    if not petscInitialized:
        petsc4py.init(argv)
        petscInitialized = True
    else:
        from petsc4py import PETSc
        OptDB=PETSc.Options()
        narg = len(argv)
        for i,s in enumerate(argv):
            if len(s) > 0 and s[0] is '-':
                name = s
                if i+1 < narg and  argv[i+1][0] is not '-':
                    value = argv[i+1]
                    OptDB.setValue(name,value)
    new_comm = Comm()
    if not comm:
        comm = new_comm
    return new_comm
Esempio n. 14
0
def init(petscDatabaseFilename=None,argv=sys.argv):
    global initial_communicator,isInitialized
    if isInitialized is 0:
        petsc4py.init(argv)
        isInitialized=1
    else:
        from petsc4py import PETSc
        narg = len(argv)
        for i,s in enumerate(argv):
            if len(s) > 0 and s[0] is '-':
                name = s
                if i+1 < narg and  argv[i+1][0] is not '-':
                    value = argv[i+1]
                    PETSc.Options.SetValue(name,value)
    if isinstance(petscDatabaseFilename,str):
        comm = flcbdfWrappers.DaetkPetscSys(isInitialized,argv,petscDatabaseFilename)
    else:
        comm = flcbdfWrappers.DaetkPetscSys(isInitialized,argv)
    if initial_communicator == None:
        initial_communicator = comm
    return comm
Esempio n. 15
0
def init():
    global comm, petscInitialized
    if not petscInitialized:
        petsc4py.init(argv)
        petscInitialized = True
    else:
        from petsc4py import PETSc
        OptDB=PETSc.Options()
        narg = len(argv)
        for i,s in enumerate(argv):
            if len(s) > 0 and s[0] is '-':
                name = s
                if i+1 < narg and  argv[i+1][0] is not '-':
                    value = argv[i+1]
                    OptDB.setValue(name,value)
        if not OptDB.hasName("options_left"):
            OptDB.setValue("options_left",False)
    petsc4py.PETSc.Log.Stage('proteus').push()
    new_comm = Comm()
    if not comm:
        comm = new_comm
    return new_comm
Esempio n. 16
0
def run_app_from_main(application):
    r"""
    Runs an application from apps/, automatically parsing command line keyword
    arguments (key=value) as parameters to the application, with positional
    arguments being passed to PETSc (if it is enabled).

    Perhaps we should take the PETSc approach of having a database of PyClaw
    options that can be queried for options on specific objects within the
    PyClaw runtime instead of front-loading everything through the application
    main...
    """

    # Arguments to the PyClaw should be keyword based, positional arguments
    # will be passed to PETSc
    petsc_args, app_kwargs = _info_from_argv(sys.argv)

    if 'use_petsc' in app_kwargs and app_kwargs['use_petsc']:
        import petsc4py
        petsc_args = [arg.replace('--','-') for arg in sys.argv[1:] if '=' not in arg]
        petsc4py.init(petsc_args)

    output=application(**app_kwargs)
    return output
Esempio n. 17
0
    def __init__( self, conf, **kwargs ):
        try:
            import petsc4py
            petsc4py.init([])
            from petsc4py import PETSc
        except ImportError:
            msg = 'cannot import petsc4py!'
            raise ImportError( msg )

        LinearSolver.__init__( self, conf, petsc = PETSc, pmtx = None, **kwargs )

        ksp = PETSc.KSP().create()

        ksp.setType( self.conf.method )
        ksp.getPC().setType( self.conf.precond )

        if hasattr( self, 'mtx' ):
            if self.mtx is not None:
                self.pmtx, self.sol, self.rhs = self.set_matrix( self.mtx )
                ksp.setOperators( self.pmtx ) # set the matrix
                ksp.setFromOptions()

        self.ksp = ksp
Esempio n. 18
0
def main(*args):
    if args:
        args = list(args)
    else:
        args = sys.argv
    commandlineArguments = parse_commandline(args[1:])
    petsc4py.init(args=(args[0:1] + commandlineArguments.petsc))
    logMAIN('commandlineArguments.petsc', commandlineArguments.petsc)
    #
    # The following must not be done until petsc4py.init has been
    # called.
    #
    from petsc4py import PETSc
    catch_signals()
    comm = MPI.COMM_WORLD
    periodic = not commandlineArguments.noperiodic
    if not periodic:
        raise KSFDException(
            '--periodic=false not implements'
        )
    ps = SolutionParameters(commandlineArguments)
    logMAIN('list(ps.groups.ligands())',
            list(ps.groups.ligands()))
    kgen = Generator(seed=commandlineArguments.seed, comm=comm)
    if (commandlineArguments.showparams):
        for n,p,d,h in ps.params0.params():
            print(
                '{n}={val} -- {h}'.format(n=n, val=p(), h=h)
            )
        if not in_notebook():
            sys.exit()
    grid = Grid(
        dim=ps.dim,
        dof=ps.nligands+1,      # rho, ligands
        width=ps.width, height=ps.height, depth=ps.depth,
        nx=ps.nwidth, ny=ps.nheight, nz=ps.ndepth
    )
    sources = decode_sources(commandlineArguments.source, ps, grid)
    logMAIN('sources', sources)
    vec0, t = initial_values(commandlineArguments, grid, ps)
    logMAIN('ps.params0', ps.params0)
    logMAIN('ps.values0', ps.values0)
    if commandlineArguments.save:
        tseries = TimeSeries(
            basename=commandlineArguments.save,
            grid=grid,
            mode='w',
            mpiok=commandlineArguments.mpiok,
            retries=commandlineArguments.series_retries,
            retry_interval=commandlineArguments.series_retry_interval,
        )
        tseries.info['commandlineArguments'] = dillnp(commandlineArguments)
        tseries.info['SolutionParameters'] = dillnp(ps, recurse=True)
        tseries.info['sources'] = dillnp(sources)
        tseries.info['dt'] = float(ps.params0['dt'])
        if 'lastvart' in ps.params0:
            tseries.info['lastvart'] = float(ps.params0['lastvart'])
        tseries.flush()
    else:
        tseries = None
    vec0.assemble()
    v0a = vec0.array.reshape(grid.Vlshape, order='F')
    lvec0 = grid.Vdmda.createLocalVec()
    derivs = Derivatives(ps, grid, sources=sources, u0=vec0)
    # UJacobian_arrays = derivs.UJacobian_arrays(t=ps.t0)
    grid.Vdmda.globalToLocal(vec0, lvec0)
    lv0a = lvec0.array.reshape(grid.Vashape, order='F')
    logMAIN('lv0a[:]', lv0a[:])
    options = PETSc.Options()
    # options.setValue('ts_max_snes_failures', 100)
    resuming = commandlineArguments.resume or commandlineArguments.restart
    if commandlineArguments.onestep:
        truemaxsteps = 1
    else:
        truemaxsteps = ps.params0['maxsteps']
    ts = implicitTS(derivs,
                    t0 = t,
                    restart = not bool(resuming),
                    rtol = ps.params0['rtol'],
                    atol = ps.params0['atol'],
                    dt = ps.params0['dt'],
                    tmax = ps.params0['tmax'],
                    maxsteps = truemaxsteps)
    logMAIN('ts', str(ts))
    ts.setMonitor(ts.printMonitor)
    logMAIN('printMonitor set')
    if commandlineArguments.save:
        saveMonitor, closeMonitor = ts.makeSaveMonitor(
            timeseries=tseries
        )
        ts.setMonitor(saveMonitor)
        logMAIN('saveMonitor set')
    if commandlineArguments.check:
        ts.setMonitor(
            ts.checkpointMonitor,
            (),
            {
                'prefix': commandlineArguments.check,
                'mpiok': commandlineArguments.mpiok
            }
        )
        logMAIN('checkpointMonitor set')
    try:
        logMAIN('calling ts.solve', ts.solve)
        ts.solve()
    except KeyboardInterrupt as e:
        print('KeyboardInterrupt:', str(e))
    except Exception as e:
        print('Exception:', str(e))
        einfo = sys.exc_info()
        sys.excepthook(*einfo)
    if commandlineArguments.save:
        closeMonitor()
        tseries.close()
        logMAIN('saveMonitor closed')
    ts.cleanup()
    grid.cleanup()
    try:
        vec0.destroy()
    except:
        pass
    try:
        lvec0.destroy()
    except:
        pass
    if MPI.COMM_WORLD.rank == 0:
        print("SNES failures = ", ts.getSNESFailures())
    # try:
    #     PETSc._finalize()
    # except PETSc.Error:
    #     pass
    logMAIN('returning 0 from main')
    return 0
Esempio n. 19
0
def KalmanFilterMRTI(**kwargs):
  """
  kalman filtered MRTI 
  """
  # import needed modules
  import petsc4py, numpy
  PetscOptions =  sys.argv
  PetscOptions.append("-ksp_monitor")
  PetscOptions.append("-ksp_rtol")
  PetscOptions.append("1.0e-15")
  PetscOptions.append("-solver")
  #PetscOptions.append("petsc")
  PetscOptions.append("superlu_dist")
  PetscOptions.append("-log_summary")
  PetscOptions.append("-override_max")
  PetscOptions.append("-modelcov")
  PetscOptions.append( kwargs['cv']['modelcov'] )
  #PetscOptions.append("-verify_inverse")
  #PetscOptions.append("-write_system_matrix")
  #PetscOptions.append("-ksp_inverse_pc")
  #PetscOptions.append("ilu")
  #PetscOptions.append("lu")
  # ROI info
  #PetscOptions.append("-nx")
  #PetscOptions.append("-35")
  #PetscOptions.append("-ny")
  #PetscOptions.append("-35")
  #PetscOptions.append("-ix")
  #PetscOptions.append("-125")
  #PetscOptions.append("-iy")
  #PetscOptions.append("-115")
  #PetscOptions.append("-help")
  petsc4py.init(PetscOptions)
  
  from petsc4py import PETSc
  
  # create stages for logging
  PredictionStage = PETSc.Log.Stage("Prediction")
  CorrectionStage = PETSc.Log.Stage("Correction")
  
  # break processors into separate communicators
  petscRank = PETSc.COMM_WORLD.getRank()
  petscSize = PETSc.COMM_WORLD.Get_size()
  sys.stdout.write("petsc rank %d petsc nproc %d\n" % (petscRank, petscSize))
  
  # set shell context
  # TODO import vtk should be called after femLibrary ???? 
  # FIXME WHY IS THIS????
  import femLibrary
  # initialize libMesh data structures
  libMeshInit = femLibrary.PyLibMeshInit(PetscOptions,PETSc.COMM_WORLD) 
    
  # store control variables
  getpot = femLibrary.PylibMeshGetPot(PetscOptions) 
  #getpot.SetIniValue( "thermal_conductivity/k_0_probe","1.0e8") 
  #getpot.SetIniValue( "thermal_conductivity/k_0_tumor","1.0e8") 
  getpot.SetIniValue( "thermal_conductivity/k_0_healthy","0.45" )
  #getpot.SetIniValue( "perfusion/w_0_healthy", "9.0"  )
  getpot.SetIniValue( "perfusion/w_0_healthy", "4.0"  ) 
  # tumor = applicator
  getpot.SetIniValue( "perfusion/w_0_tumor", "0.0"  ) 
  #getpot.SetIniValue( "optical/mu_a_healthy",  "5.0e2") 
  getpot.SetIniValue( "optical/mu_a_healthy",  "3.2e2")
  #getpot.SetIniValue( "optical/mu_s_healthy","140.0e2") 
  getpot.SetIniValue( "optical/mu_s_healthy","4.69e4")
  getpot.SetIniValue( "optical/anfact"      ,  "0.88" ) 
  # from Duck table 2.15
  getpot.SetIniValue( "material/specific_heat","3840.0" ) 
  # set ambient temperature 
  u_init = 37.0
  probe_init = 21.0
  probe_init = u_init 
  getpot.SetIniValue( "initial_condition/u_init","%f" % u_init ) 
  getpot.SetIniValue( "initial_condition/probe_init","%f" % probe_init ) 
  getpot.SetIniValue( "bc/u_dirichletid","1" ) #apply dirichlet data on applicator domain
  # set probe domain for heating
  getpot.SetIniValue( "probe/domain" , "2" ) 
  
  # root directory of data
  # FIXME Josh update with your data directory
  workDir = "/share/work/fuentes/data/biotex/090318_751642_treat/"
  workDir = "/work/00131/jyung/data/biotex/KalmanLocalization/"
  workDir = "/work/00131/fuentes/data/biotex/090318_751642_treat/"
  workDir = "/data/fuentes/biotex/090318_751642_treat/"
  workDir = "/work/01642/jyung/data/biotex/090318_751642_treat/KalmanLocalization/SDATemp/"

  dataRoot = "%s/Processed/s1%03d%03d" % ( workDir, kwargs['cv']['uniform'] , kwargs['cv']['roi'] )
  tmapRoot = "%s/Processed/s1%03d%03d" % ( workDir,          0              ,          0          )

  # load vtk modules to read imaging
  import vtk 
  import vtk.util.numpy_support as vtkNumPy 
  # read imaging data geometry that will be used to project FEM data onto
  #vtkReader = vtk.vtkXMLImageDataReader() 
  vtkReader = vtk.vtkDataSetReader() 
  vtkReader.SetFileName('%s/tmap.fem_data.0001.e.0000.vtk' % tmapRoot )
  vtkReader.Update()
  templateImage = vtkReader.GetOutput()
  dimensions = templateImage.GetDimensions()
  # dimensions should already be in meters
  spacing = [ dx * 1.000 for dx in templateImage.GetSpacing() ] 
  origin  = [ x0 * 1.000 for x0 in templateImage.GetOrigin() ] 
  origin  = [-0.095223505, -0.058349645000000006, 0.049946399000000002]
  print spacing, origin, dimensions
  femImaging = femLibrary.PytttkImaging(getpot, dimensions ,origin,spacing) 

  #image_roi = [[130,155],[123,148],[0,0]]
  image_roi = [[120,145],[123,148],[0,0]]
  size_roi  = [ image_roi[0][1]-image_roi[0][0]+1,
                image_roi[1][1]-image_roi[1][0]+1,
                image_roi[2][1]-image_roi[2][0]+1]
  roiOrigin =  [ origin[0] + image_roi[0][0] * spacing[0],
                 origin[1] + image_roi[1][0] * spacing[1],
                 origin[2] + image_roi[2][0] * spacing[2]]

  # create ROI template
  roi_array = numpy.zeros(dimensions,dtype=numpy.double,order='C')
  #roi_array[118:153,125:160] = 1.e4 
  # TODO row/column major ordering never seems to work...
  # FIXME notice the indices are reversed to account to the transpose 
  roi_array[image_roi[1][0]:image_roi[1][1]+1,
            image_roi[0][0]:image_roi[0][1]+1] = 1.e4 
  roi_vec = PETSc.Vec().createWithArray( roi_array, comm=PETSc.COMM_SELF)
  
  # initialize FEM Mesh
  femMesh = femLibrary.PylibMeshMesh()
  RotationMatrix = [[1.,0.,0.],
                    [0.,1.,0.],
                    [0.,0.,1.]]
  Translation =     [0.,0.,0.]
  #Setup Affine Transformation for registration
  AffineTransform = vtk.vtkTransform()
  #AffineTransform.Translate( [0.050,0.080, origin[2]] )
  #AffineTransform.Translate( [0.051,0.080, 0.0509] )
  AffineTransform.Translate( [0.038,0.075, 0.050] )
  #AffineTransform.RotateZ( 29.0 )
  AffineTransform.RotateZ( 0.0 )
  #AffineTransform.RotateY( 86.0 )
  AffineTransform.RotateY( 270.0 )
  #AffineTransform.RotateY( 90.0 )
  AffineTransform.RotateX(  0.0 )
  AffineTransform.Scale([1.,1.,1.])
  matrix = AffineTransform.GetConcatenatedTransform(0).GetMatrix()
  RotationMatrix = [[matrix.GetElement(0,0),matrix.GetElement(0,1),matrix.GetElement(0,2)],
                    [matrix.GetElement(1,0),matrix.GetElement(1,1),matrix.GetElement(1,2)],
                    [matrix.GetElement(2,0),matrix.GetElement(2,1),matrix.GetElement(2,2)]]
  Translation =     [matrix.GetElement(0,3),matrix.GetElement(1,3),matrix.GetElement(2,3)] 
  # set intial mesh
  #femMesh.SetupUnStructuredGrid( "%s/meshTemplate1.e" % workDir ,0,RotationMatrix, Translation  ) 
  femMesh.SetupUnStructuredGrid( "%s/meshTemplate2.NoAppBoundary.e" % ".." ,0,RotationMatrix, Translation  ) 
  #femMesh.SetupUnStructuredGrid( "%s/meshTemplate2.WithAppBoundary.e" % ".." ,0,RotationMatrix, Translation  ) 
  #femMesh.SetupStructuredGrid( (60,60,2), 
  #                             [.0216,.0825],[.0432,.104],[-.001,.001],[2,2,2,2,3,2]) 
  #femMesh.ReadFile("magnitudeROI.e")
  
  # add the data structures for the Background System Solve
  # set deltat, number of time steps, power profile, and add system
  #acquisitionTime = 5.00
  acquisitionTime = 1.00
  deltat = acquisitionTime 
  #ntime  = 128 
  ntime = 596
  eqnSystems =  femLibrary.PylibMeshEquationSystems(femMesh,getpot)
  #getpot.SetIniPower(1, [ [17,27,39,69,ntime],[0.0,4.05,0.0,10.05,0.0] ])
  getpot.SetIniPower(1, [ [35,135,195,ntime],[0.0,4.05,0.0,10.05,0.0] ]) 
  # instantiate Kalman class
  kalmanFilter = None
  if ( kwargs['cv']['algebra'] == 0  ):
    kalmanFilter = femLibrary.PytttkDenseKalmanFilterMRTI(eqnSystems, deltat) 
  elif ( kwargs['cv']['algebra'] == 1  ):
    kalmanFilter = femLibrary.PytttkUncorrelatedKalmanFilterMRTI(eqnSystems, deltat) 
  elif ( kwargs['cv']['algebra'] == 2  ):
    kalmanFilter = femLibrary.PytttkSparseKalmanFilterMRTI(eqnSystems, deltat) 
  else:
    raise RuntimeError("\n\n unknown linear algebra... ")
  kalmanFilter.systems["StateSystem"].AddStorageVectors(ntime)
  
  # add systems to plot covariance 
  CovCol = [0,1,4,5,8,10,100,101,104,150,151,154,200,201,204,250,251,254,300,301,304,350,351,354,400,401,404,450,451,454,504,505,510,900,901,904,905,908,910,1000,1001,1004,1050,1051,1054,1100,1101,1104,1150,1151,1154,1200,1201,1204,1250,1251,1254,1300,1301,1304,1350,1351,1354,1400,1401,1404,1450,1451,1454,1500,1501,1504,1550,1551,1554,1600,1601,1604,1650,1651,1654,1700,1701,1704,1750,1751,1754,1802,1803,1805,1807,1832,1835,1836,1839,1908,1911,1946,1949,1984,1987,2022,2025,2060,2063,2098,2101,2136,2139,2174,2177,2216,2219,2516,2519,2520,2523,2592,2595,2630,2633,2668,2671,2706,2709,2744,2747,2782,2785,2820,2823,2858,2861,2896,2899,2934,2937,2972,2975,3010,3013,3048,3051,3086,3089,3124,3127,3162,3165,3201,3203,3217,3219,3221,3223,3303,3305,3346,3348,3389,3391,3432,3434,3475,3477,3518]
  CovCol = [3520,3561,3563,3604,3606,3649,3653,3991,3993,3995,3997,4077,4079,4120,4122,4163,4165,4206,4208,4248,4250,4291,4293,4334,4336,4377,4379,4420,4422,4463,4465,4506,4508,4549,4551,4592,4594,4635,4637,4678,4680,4721,4723,4766,4767,4780,4782,4842,4873,4904,4935,4966,4997,5028,5059,5092,5338,5340,5400,5431,5462,5493,5524,5555,5586,5617,5648,5679,5710,5741,5772,5803,5834,5865,5897,5904,5905,5906,5907,5908,5909,5910,5911,5912,5913,5914,5915,5916,5917,5918,5919,5920,5921,5922,5923,5924,5925,5926,5927,5928,5929,5930,5931,5932,5933,5934,5935,5936,5937,5938,5939,5940,5941,5942,5943,5944,5945,5946,5947,5948,5949,5950,5951,5952,5953,5954,5955,5956,5957,5958,5959,5960,5961,5962,5963,5964,5965,5966,5967,5968,5969,5970,5971,5972,5973,5974,5975,5976,5977,5978,5979,5980,5981,5982,5983,5984,5985,5986,5987,5988,5989,5990,5991,5992,5993,5994,5995,5996,5997,5998,5999,6000]
  CovCol = [6001,6002,6003,6004,6005,6006,6007,6008,6009,6010,6011,6012,6013,6014,6015,6016,6017,6018,6019,6020,6021,6022,6023,6024,6025,6026,6027,6028,6029,6030,6031,6032,6033,6034,6035,6036,6037,6038,6039,6040,6041,6042,6043,6044,6045,6046,6047,6048,6049,6050,6051,6052,6053,6054,6055,6056,6057,6058,6059,6060,6061,6062,6063,6064,6065,6066,6067,6068,6069,6070,6071,6072,6073,6074,6075,6076,6077,6078,6079,6080,6081,6082,6083,6084,6085,6086,6087,6088,6089,6090,6091,6092,6093,6094,6095,6096,6097,6098,6099,6100,6101,6102,6103,6104,6105,6106,6107,6108,6109,6110,6111,6112,6113,6114,6115,6116,6117,6118,6119,6120,6121,6122,6123,6124,6125,6126,6127,6128,6129,6130,6131,6132,6133,6134,6135,6136,6137,6138,6139,6140,6141,6142,6143,6144,6145,6146,6147,6148,6149,6150,6151,6152,6153,6154,6155]
  CovCol = [33,187,3469]
  CovCol = CovCol + [40,69,111,222,555,654,807,911,987,5454]

  for column in CovCol: 
   tmpCovSystem =  eqnSystems.AddExplicitSystem( "Cov%04d" % column ) 
   tmpCovSystem.AddFirstLagrangeVariable( "col%04d" % column ) 
  
  preUpdateSystem =  eqnSystems.AddExplicitSystem( "preUpdate" ) 
  preUpdateSystem.AddFirstLagrangeVariable( "predict" ) 
  preUpdateSystem.AddFirstLagrangeVariable( "damage" ) 
  preUpdateSystem.AddFirstLagrangeVariable( "damderiv" ) 

  roiSystem = eqnSystems.AddExplicitSystem( "ROISystem" ) 
  roiSystem.AddFirstLagrangeVariable( "roi" ) 

  # initialize libMesh data structures
  eqnSystems.init( ) 
  
  # print info
  eqnSystems.PrintSelf() 
  
  # project ROI 
  femImaging.ProjectImagingToFEMMesh("ROISystem",0.0,roi_vec,eqnSystems)  

  # show interpolations range
  roi_vec.set(1.e4)
  femImaging.ProjectImagingToFEMMesh("MRTIMean",0.0,roi_vec,eqnSystems)  

  # choose system to create measurement from
  #numMeasurement = kalmanFilter.CreateMeasurementMapFromImaging("MRTIMean", 8 )
  #numMeasurement = kalmanFilter.CreateMeasurementMapFromImaging("ROISystem", 8 )

  MeasurementMapNodeSet = 7
  MeasurementMapNodeSet = 8
  FEMMeshIsImagingMesh = False
  if ( FEMMeshIsImagingMesh ):
    # create identity map if needed 
    MeasurementMapNodeSet = 9
    numMeasurement = kalmanFilter.CreateIdentityMeasurementMap( MeasurementMapNodeSet )
    kalmanFilter.Setup( numMeasurement )
    kalmanFilter.CreateROINodeSetMeasurementMap( MeasurementMapNodeSet )
  else : # default is unstructured FEM Mesh
    # initialize petsc data structures
    MeasurementMapNodeSet = 5 # use node set ID as the averaging number
    numMeasurement = size_roi[0]* size_roi[1]* size_roi[2] 
    kalmanFilter.Setup( numMeasurement )
    kalmanFilter.CreateROIAverageMeasurementMap(image_roi, MeasurementMapNodeSet , 
                                                [origin[0], origin[1], 0.0469], spacing )

  # get covariance diagonal and and covariance entries for plotting
  for column in CovCol:
    kalmanFilter.ExtractCoVarianceForPlotting("Cov%04d" % column,column)
  kalmanFilter.ExtractVarianceForPlotting("StateStdDev")

  # set output file
  MeshOutputFile = "fem_data%s%d.%04d.e" % (kalmanFilter.LinAlgebra,
                                            MeasurementMapNodeSet , kwargs['fileID'] )

  # write IC
  exodusII_IO = femLibrary.PylibMeshExodusII_IO(femMesh)
  exodusII_IO.WriteTimeStep(MeshOutputFile,eqnSystems, 1, 0.0 )  
  
  # NOTE This is after IC write
  # error check that can recover a constant field
  roiVec = roiSystem.GetSolutionVector()
  roiVec.set(1000.0)
  imagetest = kalmanFilter.ProjectMeasurementMatrix(roiVec)

  # show transpose measurement matrix map
  kalmanFilter.MeasurementMatrixTranspose("ROISystem",1.e3)

  # loop over time steps and solve
  #for timeID in range(35,70):
  for timeID in range(1,ntime):
     print "time step = " ,timeID
     eqnSystems.UpdatePetscFEMSystemTimeStep("StateSystem",timeID ) 
     PredictionStage.push() # log prediction stage
     # use model to predict the state and covariance
     kalmanFilter.StatePredict(timeID)
     kalmanFilter.CovariancePredict()
     PredictionStage.pop() # log prediction stage

     # copy from state system
     preUpdateSystem.CopySolutionVector( kalmanFilter.systems["StateSystem"] )
  
     CorrectionStage.push() # log correction stage
  
     # read MRTI Data
     vtkTmapReader = vtk.vtkDataSetReader() 
     vtkTmapReader.SetFileName('%s/tmap.fem_data.0001.e.%04d.vtk' % (tmapRoot,timeID) )
     vtkTmapReader.Update() 
     tmap_cells = vtkTmapReader.GetOutput().GetPointData()
     #tmap_array = u_init + vtkNumPy.vtk_to_numpy( tmap_cells.GetArray('scalars') ) 
     tmap_array = vtkNumPy.vtk_to_numpy( tmap_cells.GetArray('scalars') )
     tmap_vec = PETSc.Vec().createWithArray( tmap_array, comm=PETSc.COMM_SELF)
     femImaging.ProjectImagingToFEMMesh("MRTIMean",u_init,tmap_vec,eqnSystems)  
  
     # read in SNR base uncertainty measurement
     measurementCov = 2.0 * 2.0 ; 
     vtkSTDReader = vtk.vtkDataSetReader() 
     vtkSTDReader.SetFileName('%s/snruncert.fem_data.0001.e.%04d.vtk' % (dataRoot,timeID) )
     vtkSTDReader.Update() 
     std_cells = vtkSTDReader.GetOutput().GetPointData() 
     snr_array = vtkNumPy.vtk_to_numpy(std_cells.GetArray('scalars')) 
     snr_vec = PETSc.Vec().createWithArray( snr_array, comm=PETSc.COMM_SELF )
     femImaging.ProjectImagingToFEMMesh("MRTIStdDev",measurementCov,snr_vec,eqnSystems)  
  
     # extract data to kalman data structures
     #mrtiSoln = kalmanFilter.systems["MRTIMean"].GetSolutionVector( )
     #mrtiROI = kalmanFilter.ProjectMeasurementMatrix("MRTIMean")
     mrtiROI = PETSc.Vec().createWithArray( 
                    tmap_array.reshape(dimensions)[image_roi[1][0]:image_roi[1][1]+1,
                                                   image_roi[0][0]:image_roi[0][1]+1],comm=PETSc.COMM_SELF)
     kalmanFilter.ExtractMeasurementData(mrtiROI,"MRTIStdDev")
     #kalmanFilter.systems["MRTIMean"].ApplyDirichletData()
     #kalmanFilter.systems["MRTIStdDev"].ApplyDirichletData()
  
     # save prefem to disk for dbg
     PreFEMsoln = eqnSystems.GetPetscFEMSystemSolnSubVector( "StateSystem", 0)
     predictFEM = kalmanFilter.ProjectMeasurementMatrix(PreFEMsoln)

     vtkPreFEMImage = ConvertNumpyVTKImage(predictFEM[...],"prefem",size_roi,spacing,roiOrigin)
     vtkPreFEMWriter = vtk.vtkXMLImageDataWriter()
     vtkPreFEMWriter.SetFileName( "prefemROI%d.%04d.%04d.vti" % (MeasurementMapNodeSet, kwargs['fileID'],timeID) )
     vtkPreFEMWriter.SetInput( vtkPreFEMImage )
     vtkPreFEMWriter.Update()

     # save mrti to disk for compare
     vtkMRTImage = ConvertNumpyVTKImage(mrtiROI[...],"mrti",size_roi,spacing,roiOrigin)
     vtkMRTIWriter = vtk.vtkXMLImageDataWriter()
     vtkMRTIWriter.SetFileName( "mrtiROI%d.%04d.%04d.vti" % (MeasurementMapNodeSet, kwargs['fileID'],timeID) )
     vtkMRTIWriter.SetInput( vtkMRTImage )
     vtkMRTIWriter.Update()

     # save stddev to disk for rms
     stddevROI = PETSc.Vec().createWithArray( 
                    snr_array.reshape(dimensions)[image_roi[1][0]:image_roi[1][1]+1,
                                                  image_roi[0][0]:image_roi[0][1]+1],comm=PETSc.COMM_SELF)
     vtkstdDevImage = ConvertNumpyVTKImage(stddevROI[...],"stddev",size_roi,spacing,roiOrigin)
     vtkstdDevWriter = vtk.vtkXMLImageDataWriter()
     vtkstdDevWriter.SetFileName( "stddevROI%d.%04d.%04d.vti" % (MeasurementMapNodeSet, kwargs['fileID'],timeID) )
     vtkstdDevWriter.SetInput( vtkstdDevImage )
     vtkstdDevWriter.Update()

     # set UpdatePrediction = False to study model propagation only
     UpdatePrediction = False
     UpdatePrediction = True
     if( UpdatePrediction ):
       # update the state vector and covariance 
       kalmanGainSolver = "petsc"
       kalmanGainSolver = "superlu_dist"
       kalmanFilter.StateUpdate( kalmanGainSolver )
       kalmanFilter.CovarianceUpdate()
  
     CorrectionStage.pop() # log correction stage
  
     # save prefem to disk for dbg
     UpdateFEMsoln = eqnSystems.GetPetscFEMSystemSolnSubVector( "StateSystem", 0)
     updateFEM = kalmanFilter.ProjectMeasurementMatrix(UpdateFEMsoln)

     vtkUpdateFEMImage = ConvertNumpyVTKImage(updateFEM[...],"updatefem",size_roi,spacing,roiOrigin)
     vtkUpdateFEMWriter = vtk.vtkXMLImageDataWriter()
     vtkUpdateFEMWriter.SetFileName( "updatefemROI%d.%04d.%04d.vti" % (MeasurementMapNodeSet, kwargs['fileID'],timeID) )
     vtkUpdateFEMWriter.SetInput( vtkUpdateFEMImage )
     vtkUpdateFEMWriter.Update()

     # get covariance diagonal and and covariance entries for plotting
     for column in CovCol:
       kalmanFilter.ExtractCoVarianceForPlotting("Cov%04d" % column,column)
     kalmanFilter.ExtractVarianceForPlotting("StateStdDev")
  
     # compute l2 norm of difference
     print femLibrary.WeightedL2Norm(
                         kalmanFilter.systems["StateSystem"], "u0",
                           kalmanFilter.systems["MRTIMean"], "u0*",
                         kalmanFilter.systems["MRTIStdDev"],"du0*") 
     print femLibrary.WeightedL2Norm(
                         kalmanFilter.systems["StateSystem"], "u0",
                           kalmanFilter.systems["MRTIMean"], "u0*",
                        kalmanFilter.systems["StateStdDev"], "du0") 
     #eqnSystems.StoreTransientSystemTimeStep("StateSystem",timeID ) 
     exodusII_IO.WriteTimeStep(MeshOutputFile,eqnSystems, timeID+1, timeID*deltat )  
  retval = dict([])
  retval['fns'] = [0.0]
  retval['rank'] = petscRank 
  return(retval)
"""
background correction model 
"""

# import needed modules
import petsc4py, numpy, sys
import scipy.io as scipyio
PetscOptions = sys.argv
PetscOptions.append("-ksp_monitor")
PetscOptions.append("-ksp_rtol")
PetscOptions.append("1.e-10")
# need to solve w/ constant null space for Neumann problem
#PetscOptions.append("-ksp_constant_null_space")
#PetscOptions.append("-help")
petsc4py.init(PetscOptions)

# get rank
from petsc4py import PETSc
petscRank = PETSc.COMM_WORLD.getRank()
petscSize = PETSc.COMM_WORLD.Get_size()
sys.stdout.write("petsc rank %d petsc nproc %d\n" % (petscRank, petscSize))

# set shell context
# TODO import vtk should be called after femLibrary ????
# FIXME WHY IS THIS????
import femLibrary
# initialize libMesh data structures
libMeshInit = femLibrary.PyLibMeshInit(PetscOptions, PETSc.COMM_WORLD)

# load vtk modules to read imaging
import vtk
Esempio n. 21
0
def help(args=None):
    import sys, shlex
    # program name
    try:
        prog = sys.argv[0]
    except Exception:
        prog = getattr(sys, 'executable', 'python')
    # arguments
    if args is None:
        args = sys.argv[1:]
    elif isinstance(args, str):
        args = shlex.split(args)
    else:
        args = [str(a) for a in args]
    # import and initialize
    import petsc4py
    petsc4py.init([prog, '-help'] + args)
    from petsc4py import PETSc
    # help dispatcher
    COMM = PETSc.COMM_SELF
    if 'vec' in args:
        vec = PETSc.Vec().create(comm=COMM)
        vec.setSizes(0)
        vec.setFromOptions()
        vec.destroy()
    if 'mat' in args:
        mat = PETSc.Mat().create(comm=COMM)
        mat.setSizes([0, 0])
        mat.setFromOptions()
        mat.destroy()
    if 'pc' in args:
        pc = PETSc.PC().create(comm=COMM)
        pc.setFromOptions()
        pc.destroy()
    if 'ksp' in args:
        ksp = PETSc.KSP().create(comm=COMM)
        ksp.setFromOptions()
        ksp.destroy()
    if 'snes' in args:
        snes = PETSc.SNES().create(comm=COMM)
        snes.setFromOptions()
        snes.destroy()
    if 'ts' in args:
        ts = PETSc.TS().create(comm=COMM)
        ts.setFromOptions()
        ts.destroy()
    if 'tao' in args:
        tao = PETSc.TAO().create(comm=COMM)
        tao.setFromOptions()
        tao.destroy()
    #if 'dm' in args:
    #    dm = PETSc.DM().create(comm=COMM)
    #    dm.setFromOptions()
    #    dm.destroy()
    if 'dmda' in args:
        dmda = PETSc.DMDA().create(comm=COMM)
        dmda.setFromOptions()
        dmda.destroy()
    if 'dmplex' in args:
        dmplex = PETSc.DMPlex().create(comm=COMM)
        dmplex.setFromOptions()
        dmplex.destroy()
Esempio n. 22
0
import os, petsc4py
petsc4py.init(os.sys.argv)
from petsc4py import PETSc

import scipy as sp
import scipy.sparse



def pol2car(r,tho):
    return r*sp.cos(tho),r*sp.sin(tho)

def car2pol(x,y):
    return sp.sqrt(x**2+y**2),sp.arctan2(y,x)

def ComputeCP(x):
    '''Compute CP for circle'''
    tho = car2pol(x[:,0],x[:,1])[1]
    xx,yy = pol2car(1.0,tho)
    rslt = sp.zeros((x.shape))
    rslt[:,0] = xx
    rslt[:,1] = yy
    return rslt

def GenerateDiffMatNP(k,m,n):
    mat = scipy.sparse.lil_matrix((m*n,(m+2)*(n+2)))
    def helper_base(i,j,base,value,rm=m,rn=n,m=mat):
        m[i*rm+j,(i+1)*(rm+2)+j+1+base]=value
    def helper_1((i,j)):
        helper_base(i,j,0,-4*k)
    def helper_2((i,j)):
Esempio n. 23
0
# --------------------------------------------------------------------

if __name__ == "__main__":
    import sys, petsc4py

    petsc4py.init(sys.argv + ["-log_summary"])

# --------------------------------------------------------------------

from petsc4py import PETSc
import unittest

# --------------------------------------------------------------------


class TestLog(unittest.TestCase):
    def setUp(self):
        # PETSc.Log.begin()
        # register stages
        self.stage1 = PETSc.Log.Stage("Stage 1")
        self.stage2 = PETSc.Log.Stage("Stage 2")
        # register classes
        self.klassA = PETSc.Log.Class("Class A")
        self.klassB = PETSc.Log.Class("Class B")
        # register events
        self.event1 = PETSc.Log.Event("Event 1")  # no class
        self.event2 = PETSc.Log.Event("Event 2")  # no class
        self.eventA = PETSc.Log.Event("Event A", self.klassA)
        self.eventB = PETSc.Log.Event("Event B", self.klassB)

    def testGetName(self):
"""
   default LU Facto
"""
import sys, petsc4py
PetscOptions =  sys.argv
PetscOptions.append("-log_summary")
petsc4py.init(PetscOptions)

from petsc4py import PETSc

import numpy
import scipy.io as scipyio

# create stages for logging
LogSetup = PETSc.Log.Stage("Setup")
LogFactor= PETSc.Log.Stage("Factor")
LogSolve = PETSc.Log.Stage("Solve")

LogSetup.push()
# read in matrix
#sumcov = scipyio.loadmat("/home/fuentes/DDDAS/trunk/dddas/SumCov.mat" )
#n = len(sumcov['Mat_0'])
#AMat = PETSc.Mat().createDense([n, n], array=sumcov['Mat_0'],comm=PETSc.COMM_WORLD)
n = 7000
AMat = PETSc.Mat().createDense([n, n], array=numpy.random.rand(n,n), comm=PETSc.COMM_WORLD)
AMat.assemblyBegin();AMat.assemblyEnd()

# storage space for solution
AInv = PETSc.Mat().createDense([n, n], array=numpy.zeros((n,n)), comm=PETSc.COMM_WORLD)
AInv.assemblyBegin();AInv.assemblyEnd()
Esempio n. 25
0
from scipy.signal import correlate
import glob
import os
import sys
import h5py
import matplotlib
import matplotlib.gridspec as gridspec
import matplotlib.patches as patches
from matplotlib.collections import LineCollection
from matplotlib import transforms, colors
matplotlib.use('agg')
import pylab as pl
#import yt
#yt.enable_parallelism()

import petsc4py, sys; petsc4py.init(sys.argv)
from petsc4py import PETSc
import PetscBinaryIO

import domain
#import boundary_conditions
#import params
#import initialize
#import coords


def line(x, m, c):
    return (m*x+c)


# Optimized plot parameters to make beautiful plots:
Esempio n. 26
0
    parser.add_argument('--implicit')
    parser.add_argument('--imex')
    parser.add_argument('--explicit')
    parser.add_argument('--solver', default='gmres')
    parser.add_argument('--seed', type=int, default=793817931)
    commandlineArguments = parser.parse_args(namespace=commandlineArguments)

parse_commandline()
import petsc4py, sys
from KSDG import *
import fenics as fe
import numpy as np
#
# this needs to be done before importing PETSc
#
petsc4py.init(sys.argv[0:1] + commandlineArguments.petsc)
from petsc4py import PETSc
fe.parameters.parse(sys.argv[0:1] + commandlineArguments.fenics)
fe.parameters['ghost_mode'] = 'shared_facet'
                
def main():
    nelements = 8
    dim = 1
    degree = 2
    params = {
        'alpha': 1,
        'beta': 1,
        'mu': 0.4,
        'Umax': 1,
        'Ufac': 4,
        'sU': 1,
Esempio n. 27
0
def main():
    comm = MPI.COMM_WORLD
    if comm.size > 1:
        raise KSFDException('tsmerge must be run sequentially.')
    petsc4py.init()
    parser = ArgumentParser(description='Merge time series', allow_abbrev=True)
    parser.add_argument('-o', '--outfile', help='merged file basename')
    parser.add_argument('-s',
                        '--start',
                        type=float,
                        default=0.0,
                        help='start time')
    parser.add_argument('-e', '--end', type=float, help='end time')
    parser.add_argument('infiles', nargs='+', help='files to merge')
    parser.add_argument('-v', '--verbose', action='count')
    clargs = parser.parse_args()
    times = np.empty((0), dtype=float)
    steps = np.empty((0), dtype=int)
    files = np.empty((0), dtype=int)
    grid = None
    for f, name in enumerate(clargs.infiles):
        if clargs.verbose > 0:
            print('collecting times from {name}'.format(name=name), flush=True)
        g = Gatherer(name)
        if grid is None:
            grid = g.grid
        st = g.sorted_times()
        if clargs.verbose > 1:
            print('times:', st, flush=True)
        steps = np.append(steps, g.steps())
        times = np.append(times, st)
        files = np.append(files, np.full_like(st, f, dtype=int))
        g.close()
    out = TimeSeries(clargs.outfile, grid, comm=MPI.COMM_SELF, mode='w')
    # order = times.argsort()
    # files = files[order]
    # steps = steps[order]
    # times = times[order]
    k = 0
    del out.tsf['/info']
    for f, name in enumerate(clargs.infiles):
        if clargs.verbose > 0:
            print('collecting data from {name}'.format(name=name), flush=True)
        fmatch = files == f
        # forder = order[fmatch]
        fsteps = steps[fmatch]
        ftimes = times[fmatch]
        g = Gatherer(name)
        if '/info' not in out.tsf:
            g.tsf.copy(source='/info',
                       dest=out.tsf,
                       name='/info',
                       shallow=False)
        for s in g:
            if clargs.verbose > 0:
                print(str(s.tsf), flush=True)
            for k, t in zip(fsteps, ftimes):
                if t < clargs.start or (clargs.end and t > clargs.end):
                    continue
                vals = s.retrieve_by_number(k)
                if clargs.verbose > 1:
                    print('point {k}, time {t}'.format(k=k, t=t), flush=True)
                out.store_slice(s.ranges, vals, t)
        g.close()
    out.close()
def pennesModeling(**kwargs):
  """
  treatment planning model 
  """
  # import petsc and numpy
  import petsc4py, numpy
  # init petsc
  PetscOptions =  sys.argv
  PetscOptions.append("-snes_monitor")
  PetscOptions.append("-snes_converged_reason")
  PetscOptions.append("-ksp_monitor")
  PetscOptions.append("-ksp_converged_reason")
  PetscOptions.append("-ksp_rtol")
  PetscOptions.append("1.0e-15")
  #PetscOptions.append("-help")
  #PetscOptions.append("-idb")
  petsc4py.init(PetscOptions)
  
  # break processors into separate communicators
  from petsc4py import PETSc
  petscRank = PETSc.COMM_WORLD.getRank()
  petscSize = PETSc.COMM_WORLD.Get_size()
  sys.stdout.write("petsc rank %d petsc nproc %d\n" % (petscRank, petscSize))

  # the original configuration ini file should be stored
  config = kwargs['config_parser']

  # set shell context
  # TODO import vtk should be called after femLibrary ???? 
  # FIXME WHY IS THIS????
  import femLibrary
  # initialize libMesh data structures
  libMeshInit = femLibrary.PyLibMeshInit(PetscOptions,PETSc.COMM_WORLD) 
  
  # store control variables
  getpot = femLibrary.PylibMeshGetPot(PetscOptions) 

  # copy all values from the input file
  for section in config.sections():
    for name,value in  config.items(section):
      #print "%s/%s" % (section,name) , value
      getpot.SetIniValue( "%s/%s" % (section,name) , value ) 

  #####################################################
  # update values from dakota
  #####################################################
  # thermal conductivity from Duck/CRC Handbook
  try:
    getpot.SetIniValue( "thermal_conductivity/k_0_healthy",
                              kwargs['cv']['k_0_healthy'] ) 
  except KeyError:
    pass #use default value
  # nonlinear conductivity
  try:
    getpot.SetIniValue( "thermal_conductivity/k_1",
                                kwargs['cv']['k_1'] ) 
  except KeyError:
    pass #use default value
  # tumor conductivity
  try:
    getpot.SetIniValue( "thermal_conductivity/k_0_tumor",
                                kwargs['cv']['k_0_tumor'] ) 
  except KeyError:
    pass #use default value
  # perfusion from Duck/CRC Handbook
  try:
    getpot.SetIniValue( "perfusion/w_0_healthy",
                              kwargs['cv']['w_0_healthy'] ) 
  except KeyError:
    pass #use default value
  try:
    getpot.SetIniValue( "perfusion/w_1",
                     kwargs['cv']['w_1_coag'] ) 
  except KeyError:
    pass #use default value
  try:
    getpot.SetIniValue( "perfusion/w_0_tumor",
                     kwargs['cv']['w_0_tumor'] ) 
  except KeyError:
    pass #use default value
  # water properties from Duck
  # Adult white mater
  # 3.2 1/cm
  # Beek et al., 1993a
  # Adult grey mater
  # 5.0 1/cm
  try:
    getpot.SetIniValue( "optical/mu_a_healthy",
                   kwargs['cv']['mu_a_healthy'] ) 
  except KeyError:
    pass #use default value
  try:
    getpot.SetIniValue( "optical/mu_a_1",
                   kwargs['cv']['mu_a_coag'] ) 
  except KeyError:
    pass #use default value
  # 1-300
  try:
    getpot.SetIniValue( "optical/mu_a_tumor",
                   kwargs['cv']['mu_a_tumor'] ) 
  except KeyError:
    pass #use default value
  # FIXME large mu_s (> 30) in agar causing negative fluence to satisfy BC 
  try:
    getpot.SetIniValue( "optical/mu_s_healthy",
                   kwargs['cv']['mu_s_healthy'] ) 
  except KeyError:
    pass #use default value
  try:
    getpot.SetIniValue( "optical/mu_s_1",
                   kwargs['cv']['mu_s_coag'] ) 
  except KeyError:
    pass #use default value
  # 1-300
  try:
    getpot.SetIniValue( "optical/mu_s_tumor",
                   kwargs['cv']['mu_s_tumor'] ) 
  except KeyError:
    pass #use default value

  # from AE paper
  #http://scitation.aip.org/journals/doc/MPHYA6-ft/vol_36/iss_4/1351_1.html#F3
  # .9  - .99
  try:
    getpot.SetIniValue( "optical/anfact",
                   kwargs['cv']['anfact'] ) 
  except KeyError:
    pass #use default value
  #
  #  given the original orientation as two points along the centerline z = x2 -x1
  #     the transformed orienteation would be \hat{z} = A x2 + b - A x1 - b = A z
  #  ie transformation w/o translation which is exactly w/ vtk has implemented w/ TransformVector
  #  TransformVector = TransformPoint - the transation
  #Setup Affine Transformation for registration
  RotationMatrix = [[1.,0.,0.],
                    [0.,1.,0.],
                    [0.,0.,1.]]
  Translation =     [0.,0.,0.] 
  
  # initialize FEM Mesh
  femMesh = femLibrary.PylibMeshMesh()
  #femMesh.SetupUnStructuredGrid(kwargs['mesh_file'],0,RotationMatrix, Translation  ) 
  femMesh.ReadFile(kwargs['mesh_file'])
  MeshOutputFile = MeshOutputTemplate % kwargs['fileID'] 
  #fem.SetupStructuredGrid( (10,10,4) ,[0.0,1.0],[0.0,1.0],[0.0,1.0]) 
  
  # add the data structures for the Background System Solve
  # set deltat, number of time steps, power profile, and add system
  eqnSystems =  femLibrary.PylibMeshEquationSystems(femMesh,getpot)
    
  #getpot.SetIniPower(1,[[19,119],[90.0,100.0]] )
  getpot.SetIniPower(1,kwargs['powerHistory'] )
  # AddPennesSDASystem
  # AddPennesRFSystem
  # AddPennesDeltaPSystem
  pennesSystem = eval("eqnSystems.%s('StateSystem',kwargs['deltat'])" %  kwargs['physics'])
  pennesSystem.AddStorageVectors( kwargs['ntime']+1 ) 
  
  # initialize libMesh data structures
  eqnSystems.init( ) 
  
  # quick error check
  #errCheckSoln = fem.GetSolutionVector( "StateSystem" )[...]
  #if (  errCheckSoln.size + 1 != kwargs['functions'] ):
  #  print "ERROR!! number of response functions incorrect!!"
  #  raise RuntimeError("soln vec + 1 = %d .NE. num_response = %d"%(errCheckSoln.size+1,kwargs['functions']) )

  # print info
  eqnSystems.PrintSelf() 
  
  # setup IC 
  pennesSystem.PetscFEMSystemSetupInitialConditions( ) 

  # get responses for list of variable
  responseLevelVarList = kwargs['responseLevelVarList'] 

  # write IC
  exodusII_IO = femLibrary.PylibMeshExodusII_IO(femMesh)
  exodusII_IO.WriteTimeStep(MeshOutputFile,eqnSystems, 1, 0.0 )  
  
  ObjectiveFunction = 0.0
  # loop over time steps and solve
  print  "deltat = ",kwargs['deltat']
  #for timeID in range(1,2):
  for timeID in range(1,kwargs['ntime']+1):
     print "time step = " ,timeID
     #for subTimeID in range(1):
     for subTimeID in range(kwargs['nsubstep']):
       pennesSystem.PetscFEMSystemUpdateTimeStep( timeID ) 
       pennesSystem.SystemSolve( ) 
     #fem.StoreTransientSystemTimeStep("StateSystem",timeID ) 
     exodusII_IO.WriteTimeStep(MeshOutputFile ,eqnSystems, timeID+1, timeID*kwargs['acquisitionTime'])  
  retval = dict([])
  retval['fns'] = [ObjectiveFunction]
  retval['rank'] = petscRank 
  return(retval)
Esempio n. 29
0
#!/usr/bin/env python
# provides an easy interface with petsc
import numpy as np
import scipy.sparse
import logging
import modest
import matplotlib.pyplot as plt
try:
  import petsc4py
  petsc4py.init()
  from petsc4py import PETSc
except ImportError:
  print(
    'could not import PETSc. '
    'PETSc can be installed by following the instructions at '
    'https://www.mcs.anl.gov/petsc. Interfacing with PETSc requires '
    'petsc4py which can be found at https://bitbucket.org/petsc/petsc4py. '
    'Installing the latest version of petsc4py can be done with the command\n\n'
    '  pip install https://bitbucket.org/petsc/petsc4py/get/master.tar.gz\n')

  raise    

logger = logging.getLogger(__name__)


def _monitor(solver, its, fgnorm):
  ''' 
  this function is called for each iteration of a KSP solver
  '''
  logger.info('preconditioned residual norm at iteration %s: %.5e' % (its,fgnorm))
Esempio n. 30
0
# Stiff 3-variable ODE system from chemical reactions,
# due to Robertson (1966),
# problem ROBER in Hairer&Wanner, ODE 2, 1996

import sys, petsc4py
petsc4py.init(sys.argv)

from petsc4py import PETSc


class Rober(object):
    n = 3
    comm = PETSc.COMM_SELF

    def evalSolution(self, t, x):
        assert t == 0.0, "only for t=0.0"
        x[:] = [1, 0, 0]
        x.assemble()

    def evalFunction(self, ts, t, x, xdot, f):
        f[:] = [
            xdot[0] + 0.04 * x[0] - 1e4 * x[1] * x[2],
            xdot[1] - 0.04 * x[0] + 1e4 * x[1] * x[2] + 3e7 * x[1]**2,
            xdot[2] - 3e7 * x[1]**2
        ]
        f.assemble()

    def evalJacobian(self, ts, t, x, xdot, a, A, B):
        J = B
        J[:, :] = [[a + 0.04, -1e4 * x[2], -1e4 * x[1]],
                   [-0.04, a + 1e4 * x[2] + 3e7 * 2 * x[1], 1e4 * x[1]],
Esempio n. 31
0
if True:
    import numpy as np
    import sys
    from numpy import pi
    from math import floor
    import json
    from sympy.physics.wigner import gaunt, wigner_3j
    import Module as Mod 
    import Potential as Pot
    
         
if True:
    import petsc4py
    from petsc4py import PETSc
    petsc4py.init(sys.argv)
    petsc4py.init(comm=PETSc.COMM_WORLD)
    from mpi4py import MPI
    comm = MPI.COMM_WORLD
    rank = comm.Get_rank()


def Dipole_Z_Matrix(input_par):
    index_map_l_m, index_map_box = Mod.Index_Map_M_Block(input_par)
    grid = Mod.Make_Grid(input_par["grid_spacing"], input_par["grid_size"], input_par["grid_spacing"])
    matrix_size = grid.size * len(index_map_l_m)
    h = abs(grid[1] - grid[0]) 

    Dipole_Matrix = PETSc.Mat().createAIJ([matrix_size, matrix_size], nnz=2, comm=PETSc.COMM_WORLD)
    istart, iend = Dipole_Matrix.getOwnershipRange() 
    for i in range(istart, iend):
        l_block = index_map_l_m[floor(i/grid.size)][1]
Esempio n. 32
0
# Mostafa Mollaali
# Vahid

from fenics import *
from dolfin import *
from mshr import *
from dolfin_utils.meshconvert import meshconvert
from scipy.interpolate import UnivariateSpline
from scipy.interpolate import LinearNDInterpolator
import matplotlib.pyplot as plt
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import sympy, sys, math, os, subprocess, shutil
import petsc4py
petsc4py.init()
from petsc4py import PETSc

from math import hypot, atan2, erfc


#=======================================================================================
def vec(z):
    if isinstance(z, dolfin.cpp.Function):
        return dolfin.as_backend_type(z.vector()).vec()
    else:
        return dolfin.as_backend_type(z).vec()


def mat(A):
    return dolfin.as_backend_type(A).mat()
Esempio n. 33
0
"""
Functions for a high-level PETSc-based parallelization.
"""
from __future__ import absolute_import
import time

import numpy as nm

import sys, petsc4py
from six.moves import range
argv = [arg for arg in sys.argv if arg not in ['-h', '--help']]
petsc4py.init(argv)

from petsc4py import PETSc
from mpi4py import MPI

from sfepy.base.base import assert_, output, ordered_iteritems, Struct
from sfepy.discrete.common.region import Region
from sfepy.discrete.fem.fe_surface import FESurface


def partition_mesh(mesh, n_parts, use_metis=True, verbose=False):
    """
    Partition the mesh cells into `n_parts` subdomains, using metis, if
    available.
    """
    output('partitioning mesh into %d subdomains...' % n_parts,
           verbose=verbose)
    tt = time.clock()

    if use_metis:
Esempio n. 34
0
   def PPS(self,p):
      """
      Parallel solver implementing a PETSc KSP linear routine over the entire scene as a 
      single patch (PPS = Parallel PETSc Single)
      """
      from _reconutils import get_gcd_vals, laplace_builder
      try:
         import mpi4py
         from mpi4py import MPI
      except:
         print 'PPS requires that mpi4py be installed and in the Python path'
         sys.exit()
      try:
         import petsc4py
         petsc4py.init([],comm=MPI.COMM_WORLD)
         from petsc4py import PETSc
      except:
         print 'PPS requires that petsc4py be installed and in the Python path'
         sys.exit()
 
      PETSc.Sys.Print('Running PPS:  Parallel PETSc (linear) Solver on a single patch')

      comm=PETSc.COMM_WORLD
      size, rank = comm.Get_size(), comm.Get_rank()
      cols = p.gen.cols
      try:
         p.gen.gmat_rows
      except:
         fid = open('.gmat_rows','r')
         p.gen.gmat_rows = np.int32(''.join(fid.readlines()))
         fid.close()

      if rank == 0:
         address = self._read_address(p)
         metfile, modlen = p.gen.metfile, 3*len(address)
         PETSc.Sys.Print('retrieving gmat, cdi, and dvec vals')
         
         ### get gmat, Cd and d values and send segments to other processors  
         gmatvals, gmatij, cdiv_temp, dvec_temp = get_gcd_vals(metfile,len(p.scenes),
                  p.gen.doline,p.gen.cols,address,p.gen.gmat_rows)
         gmatij = gmatij.reshape(-1,3*p.gen.gmat_rows)
         if gmatij.dtype != np.int32:  gmatij = gmatij.astype(np.int32)
         if p.gen.cmlambda < 1.e-12:  del address
        
         ### initialize arrays to store start and end row indices for all procs 
         rowblocks_gmat = np.empty([2,size],dtype=np.int32)
         rowblocks_cdiv = np.empty([2,size],dtype=np.int32)
         rowblocks_dvec = np.empty([2,size],dtype=np.int32)

      modlen = comm.bcast(modlen,root=0)

      PETSc.Sys.Print('building sparse matrices')
      ### initialize 
      gmat = PETSc.Mat().createAIJ([p.gen.gmat_rows,modlen],nnz=p.gen.gmat_rows*3,comm=comm)
      cdiv = PETSc.Mat().createAIJ([p.gen.gmat_rows,p.gen.gmat_rows],nnz=p.gen.gmat_rows,comm=comm)
      dvec = PETSc.Vec().createMPI(p.gen.gmat_rows,comm=comm)
         
      ### get the block of rows owned by this processor 
      sr_gmat, er_gmat = gmat.getOwnershipRange()
      sr_cdiv, er_cdiv = cdiv.getOwnershipRange()
      sr_dvec, er_dvec = dvec.getOwnershipRange()

      ### send start/end rows to root proc and receive gmat, cdiv, and dvec entries 
      base_tag, base_stag = 777, 999
      if rank == 0:
         rowblocks_gmat[0,0], rowblocks_gmat[1,0] = sr_gmat, er_gmat
         rowblocks_cdiv[0,0], rowblocks_cdiv[1,0] = sr_cdiv, er_cdiv
         rowblocks_dvec[0,0], rowblocks_dvec[1,0] = sr_dvec, er_dvec
         tag,stag = base_tag, base_stag
         for i in np.arange(1,size):
            comm.Recv([rowblocks_gmat[:,i],2,MPI.INT],source=i,tag=tag); tag += 1
            comm.Recv([rowblocks_cdiv[:,i],2,MPI.INT],source=i,tag=tag); tag += 1
            comm.Recv([rowblocks_dvec[:,i],2,MPI.INT],source=i,tag=tag); tag += 1
            
            svec = gmatvals[3*rowblocks_gmat[0,i]:3*(rowblocks_gmat[1,i])]  
            comm.Send([svec,len(svec), MPI.FLOAT], dest=i, tag=stag); stag += 1
            svec = gmatij[:,3*rowblocks_gmat[0,i]:3*(rowblocks_gmat[1,i])].flatten()
            comm.Send([svec,len(svec), MPI.INT], dest=i, tag=stag); stag += 1
            svec = cdiv_temp[rowblocks_cdiv[0,i]:rowblocks_cdiv[1,i]]
            comm.Send([svec,len(svec), MPI.FLOAT], dest=i, tag=stag); stag += 1
            svec = dvec_temp[rowblocks_dvec[0,i]:rowblocks_dvec[1,i]]
            comm.Send([svec,len(svec), MPI.FLOAT], dest=i, tag=stag); stag += 1
         del svec
         gmatvals = gmatvals[3*rowblocks_gmat[0,0]:3*(rowblocks_gmat[1,0])]
         gmatij = gmatij[:,3*rowblocks_gmat[0,0]:3*(rowblocks_gmat[1,0])]
         cdiv_temp = cdiv_temp[rowblocks_cdiv[0,0]:rowblocks_cdiv[1,0]]
         dvec_temp = dvec_temp[rowblocks_dvec[0,0]:rowblocks_dvec[1,0]]

      else:
         tag = base_tag + (rank-1)*3
         comm.Send([np.array([sr_gmat, er_gmat]),2,MPI.INT], dest=0, tag=tag); tag += 1
         comm.Send([np.array([sr_cdiv, er_cdiv]),2,MPI.INT], dest=0, tag=tag); tag += 1
         comm.Send([np.array([sr_dvec, er_dvec]),2,MPI.INT], dest=0, tag=tag); tag += 1

         stag = base_stag + (rank-1)*3

         gmatvals = np.empty(3*(er_gmat-sr_gmat), dtype=np.float32)
         gmatij = np.empty(2*3*(er_gmat-sr_gmat), dtype=np.int32)
         cdiv_temp = np.empty((er_gmat-sr_gmat), dtype=np.float32)
         dvec_temp = np.empty((er_gmat-sr_gmat), dtype=np.float32)

         comm.Recv([gmatvals,len(gmatvals),MPI.FLOAT],source=0, tag=stag); stag += 1
         comm.Recv([gmatij,len(gmatij),MPI.INT],source=0, tag=stag); stag += 1
         comm.Recv([cdiv_temp,len(cdiv_temp),MPI.FLOAT],source=0, tag=stag); stag += 1
         comm.Recv([dvec_temp,len(dvec_temp),MPI.FLOAT],source=0, tag=stag); stag += 1
         gmatij = gmatij.reshape(2,-1)

      ### load and assemble gmat, cdiv, and dvec
      for i in np.arange(len(dvec_temp)):
         threei = 3*i
         gmat.setValue(gmatij[0,threei],gmatij[1,threei],gmatvals[threei])
         gmat.setValue(gmatij[0,threei],gmatij[1,threei+1],gmatvals[threei+1])
         gmat.setValue(gmatij[0,threei],gmatij[1,threei+2],gmatvals[threei+2])
         cdiv.setValue(gmatij[0,threei],gmatij[0,threei],cdiv_temp[i])
         dvec.setValue(gmatij[0,threei],dvec_temp[i])
      comm.Barrier()
      dvec.assemblyBegin(); dvec.assemblyEnd()
      gmat.assemblyBegin(assembly=FINAL); gmat.assemblyEnd(assembly=FINAL)
      cdiv.assemblyBegin(assembly=FINAL); cdiv.assemblyEnd(assembly=FINAL)
      comm.Barrier()

      del gmatvals, gmatij, cdiv_temp, dvec_temp

      ### build A and gtilde (=dtilde)
      omega = PETSc.Mat().createAIJ([modlen,p.gen.gmat_rows],comm=comm)
      omega = gmat.matTransposeMult(cdiv,omega)
      cdiv.destroy()

      gtg = PETSc.Mat().createAIJ([modlen,modlen],comm=comm)
      mtilde,gtilde = gtg.getVecs() 
      omega.mult(dvec,gtilde)
      gtg = omega.matMult(gmat,gtg)
      dvec.destroy(); omega.destroy(); gmat.destroy()
      mtilde.set(0)

      if p.gen.cmlambda > 1.e-12:

         PETSc.Sys.Print('building fmat')

         if rank == 0:
            try:
               aa = len(address)
               if 3*aa != modlen: 
                  address = self._read_address(p)
            except:
               address = self._read_address(p)
            fmatval, fmatij = laplace_builder(p.gen.cols,address)
            fmatval = fmatval[:np.argmin(fmatval)]
            fmatij = fmatij.reshape(2,-1)[:,:len(fmatval)]
            if fmatij.dtype != np.int32: fmatij = fmatij.astype(np.int32)
            del address

            rowblocks_fmat = np.empty([2,size],dtype=np.int32)

         fmat = gtg.duplicate(copy=False) 
         fmat.setPreallocationNNZ(nnz=len(fmatval))

         ### get the row addresses for this processor 
         sr_fmat, er_fmat = fmat.getOwnershipRange()

         base_tag, base_stag = 77, 444
         if rank == 0:
            rowblocks_fmat[0,0], rowblocks_fmat[1,0] = sr_fmat, er_fmat
            tag, stag = base_tag, base_stag
            for i in np.arange(1,size):
               comm.Recv([rowblocks_fmat[:,i],2,MPI.INT],source=i,tag=tag); tag += 1
               # get indexes for fmatval and fmatij
               left  = np.argmin(np.abs(fmatij[0,:]-rowblocks_fmat[0,i]))
               right = np.argmin(np.abs(fmatij[0,:]-rowblocks_fmat[1,i]))
               svec = fmatval[left:right]
               comm.Send([len(svec),1,MPI.INT], dest=i, tag=stag); stag += 1
               comm.Send([svec,len(svec),MPI.FLOAT], dest=i, tag=stag); stag += 1                
               svec = fmatij[:,left:right].flatten()
               comm.Send([svec,len(svec),MPI.INT], dest=i, tag=stag); stag += 1
            fmatval = fmatval[sr_fmat:er_fmat]
            fmatij = fmatij[:,sr_fmat:er_fmat]
         else:
            tag, stag = base_tag + (rank-1), base_stag + (rank-1)*3
            comm.Send([np.array([sr_fmat, er_fmat]),2,MPI.INT], dest=0, tag=tag); tag += 1

            comm.Recv([lenrec,1,MPI.INT], source=0, tag=stag); stag += 1

            fmatval = np.empty(lenrec,dtype=np.float32)
            fmatij  = np.empty(2*lenrec,dtype=np.int32)

            comm.Recv([fmatval,lenrec,MPI.FLOAT], source=0, tag=stag); stag += 1
            comm.Recv([fmatij,2*lenrec,MPI.INT], source=0, tag=stag); stag += 1 
            fmatij = fmatij.reshape(2,-1)

         PETSc.Sys.Print('loading sparse fmat...')
         for i in np.arange(len(fmatval)):
            fmat.setValue(fmatij[0,i],fmatij[1,i],fmatval[i])
         comm.Barrier()
         fmat.assemblyBegin(assembly=FINAL); fmat.assemblyEnd(assembly=FINAL)
         comm.Barrier()
         del fmatval, fmatij

         PETSc.Sys.Print('loading cli...')
         cliv = PETSc.Vec().createMPI(modlen,comm=comm)
         gtg.getDiagonal(cliv)
         cli = gtg.duplicate(copy=False)
         cli.setDiagonal(cliv)
         cliv.destroy()

         PETSc.Sys.Print('building C_m...')
         rhs = gtg.duplicate(copy=False)
         rhs = fmat.matTransposeMult(cli,rhs)
         rhs = rhs.matMult(fmat)
         cli.destroy(); fmat.destroy()

         PETSc.Sys.Print('building A...\n')
         gtg.axpy(p.gen.cmlambda,rhs)
         rhs.destroy()
       

      PETSc.Sys.Print('inverting using PETSc lsqr...') 
      ksp = PETSc.KSP().create(comm)
      ksp.setType('lsqr')
      ksp.pc.setType('none')
      ksp.setOperators(gtg)
      ksp.setFromOptions()
      t0 = time.time()
      ksp.solve(gtilde,mtilde)

      gtg.destroy(); gtilde.destroy(); ksp.destroy()

      PETSc.Sys.Print('lsqr took %10f seconds'%(time.time()-t0))
      PETSc.Sys.Print(' Converged in %d iterations '%(ksp.getIterationNumber()))
      PETSc.Sys.Print(' Tolerance Asked: %e %e %d %d'%(ksp.getTolerances()))
      PETSc.Sys.Print(' Converged Reason: %d'%(ksp.getConvergedReason()))
      PETSc.Sys.Print(' ')

      comm.Barrier() 
      if rank == 0:    
         mtildenump = mtilde[...]; mtilde.destroy()
         self._write_post_model(model=mtildenump,p=p)
      comm.Barrier()

      PETSc._finalize
      MPI.Finalize
def pennesModeling(**kwargs):
  """
  treatment planning model 
  """
  # import needed modules
  import petsc4py, numpy, sys
  PetscOptions =  sys.argv
  PetscOptions.append("-ksp_monitor")
  PetscOptions.append("-ksp_rtol")
  PetscOptions.append("1.0e-15")
  #PetscOptions.append("-idb")
  petsc4py.init(PetscOptions)
  
  from petsc4py import PETSc
  
  # break processors into separate communicators
  petscRank = PETSc.COMM_WORLD.getRank()
  petscSize = PETSc.COMM_WORLD.Get_size()
  sys.stdout.write("petsc rank %d petsc nproc %d\n" % (petscRank, petscSize))
  
  # set shell context
  import femLibrary
  # initialize libMesh data structures
  libMeshInit = femLibrary.PyLibMeshInit(PetscOptions,PETSc.COMM_WORLD) 
  
  # the original configuration ini file should be stored
  config = kwargs['config_parser']

  # store control variables
  getpot = femLibrary.PylibMeshGetPot(PetscOptions) 

  # copy all values from the input file
  for section in config.sections():
    for name,value in  config.items(section):
      #print "%s/%s" % (section,name) , value
      getpot.SetIniValue( "%s/%s" % (section,name) , value ) 
  
  # nodeset 1 will be treated as dirichlet
  dirichletID = 1
  getpot.SetIniValue( "bc/u_dirichlet" , '%d' % dirichletID )

  # set tissue lookup tables
  k_0Table  = {"default":config.getfloat("thermal_conductivity","k_0_healthy")  ,
               "vessel" :config.getfloat("thermal_conductivity","k_0_healthy")  ,
               "grey"   :config.getfloat("thermal_conductivity","k_0_grey"   )  ,
               "white"  :config.getfloat("thermal_conductivity","k_0_white"  )  ,
               "csf"    :config.getfloat("thermal_conductivity","k_0_csf"    )  ,
               "tumor"  :config.getfloat("thermal_conductivity","k_0_tumor"  )  }
  w_0Table  = {"default":config.getfloat("perfusion","w_0_healthy")  ,
               "vessel" :config.getfloat("perfusion","w_0_healthy")  ,
               "grey"   :config.getfloat("perfusion","w_0_grey"   )  ,
               "white"  :config.getfloat("perfusion","w_0_white"  )  ,
               "csf"    :config.getfloat("perfusion","w_0_csf"    )  ,
               "tumor"  :config.getfloat("perfusion","w_0_tumor"  )  }
  mu_aTable = {"default":config.getfloat("optical","mu_a_healthy")  ,
               "vessel" :config.getfloat("optical","mu_a_healthy")  ,
               "grey"   :config.getfloat("optical","mu_a_grey"   )  ,
               "white"  :config.getfloat("optical","mu_a_white"  )  ,
               "csf"    :config.getfloat("optical","mu_a_csf"    )  ,
               "tumor"  :config.getfloat("optical","mu_a_tumor"  )  }
  mu_sTable = {"default":config.getfloat("optical","mu_s_healthy")  ,
               "vessel" :config.getfloat("optical","mu_s_healthy")  ,
               "grey"   :config.getfloat("optical","mu_s_grey"   )  ,
               "white"  :config.getfloat("optical","mu_s_white"  )  ,
               "csf"    :config.getfloat("optical","mu_s_csf"    )  ,
               "tumor"  :config.getfloat("optical","mu_s_tumor"  )  }
  labelTable= {config.get("labels","greymatter" ):"grey" , 
               config.get("labels","whitematter"):"white", 
               config.get("labels","csf"        ):"csf"  , 
               config.get("labels","tumor"      ):"tumor", 
               config.get("labels","vessel"     ):"vessel"}
  labelCount= {"default":0,
               "grey"   :0, 
               "white"  :0, 
               "csf"    :0, 
               "tumor"  :0, 
               "vessel" :0}
  # imaging params
  import vtk 
  import vtk.util.numpy_support as vtkNumPy 
  SegmentFile=config.get("exec","segment_file")
  # set the default reader based on extension
  if( SegmentFile.split(".").pop() == "vtk"):
     vtkImageReader = vtk.vtkDataSetReader
  elif( SegmentFile.split(".").pop() == "vti"):
     vtkImageReader = vtk.vtkXMLImageDataReader
  else:
     raise RuntimeError("uknown file")
  
  # get dimension info from header
  vtkSetupReader = vtkImageReader() 
  vtkSetupReader.SetFileName(SegmentFile ) 
  vtkSetupReader.Update() 
  vtkImageMask = vtkSetupReader.GetOutput()
  dimensions = vtkSetupReader.GetOutput().GetDimensions()
  numberPointsImage =  vtkSetupReader.GetOutput().GetNumberOfPoints()
  spacing_mm = vtkSetupReader.GetOutput().GetSpacing()
  origin_mm  = vtkSetupReader.GetOutput().GetOrigin()
  # convert to meters
  spacing = [dx*.001 for dx in spacing_mm]
  origin  = [x0*.001 for x0 in  origin_mm]
  
  # pass pointer to c++
  image_cells = vtkImageMask.GetPointData() 
  data_array = vtkNumPy.vtk_to_numpy( image_cells.GetArray(0) ) 
  # need to pass numpy array's w/ Fortran storage... ie painful to debug
  imageDataVec = PETSc.Vec().createWithArray(numpy.ravel(data_array,order='F'), comm=PETSc.COMM_SELF)

  # FIXME - center around quadrature in out-of-plane direction
  # FIXME - need better out of plane cooling model
  quadratureOffset  = 1./numpy.sqrt(3.0) * spacing[2]/2.0
  # expecting roi and subsample of the form:
  #     roi = [(40,210),(30,220),(6,78)]
  #     subsample = [3,3,2]
  ROI = eval(config.get('exec','roi'))
  subsample = eval(config.get('exec','subsample'))
  nelemROI  = [ (pixel[1] - pixel[0] - 1 )/sub for pixel,sub in zip(ROI,subsample)] 
  xbounds = [ origin[0]+spacing[0]*(ROI[0][0]+0.5),origin[0]+spacing[0]*(ROI[0][1]+0.5) ]
  ybounds = [ origin[1]+spacing[1]*(ROI[1][0]+0.5),origin[1]+spacing[1]*(ROI[1][1]+0.5) ]
  zbounds = [ origin[2]+spacing[2]*(ROI[2][0]+0.5),origin[2]+spacing[2]*(ROI[2][1]+0.5) ]
  if( petscRank ==0 ):
    print "#points",numberPointsImage , "dimensions ",dimensions , "spacing ",spacing , "origin ",origin 
    print "ROI",ROI , "nelemROI ",nelemROI , "bounds", xbounds, ybounds, zbounds

  #set to steady state solve 
  getpot.SetIniValue("steadystate/domain_0","true") 

  # initialize FEM Mesh
  femMesh = femLibrary.PylibMeshMesh()
  #femMesh.SetupUnStructuredGrid(kwargs['mesh_file'],0,RotationMatrix, Translation  ) 
  #femMesh.ReadFile(kwargs['mesh_file'])

  femMesh.SetupStructuredGrid(nelemROI,xbounds,ybounds,zbounds,
                              [2,2,2,2,2,2]) 
  # get output file name else set default name
  try:
    MeshOutputFile = config.get("exec","exodus_file" )
  except ConfigParser.NoOptionError:
    MeshOutputFile = "fem.e"

  # add the data structures for the Background System Solve
  # set deltat, number of time steps, power profile, and add system
  eqnSystems =  femLibrary.PylibMeshEquationSystems(femMesh,getpot)
    
  #getpot.SetIniPower(1,[[19,119],[90.0,100.0]] )
  getpot.SetIniPower(1,kwargs['powerHistory'] )
  # AddPennesSDASystem
  # AddPennesRFSystem
  # AddPennesDeltaPSystem
  pennesSystem = eval("eqnSystems.%s('StateSystem',kwargs['deltat'])" %  kwargs['physics'])
  pennesSystem.AddStorageVectors( kwargs['ntime']+1 ) 
  
  # add system for labels 
  # FIXME: need both nodal, for dirichlet bc, and element version, for parameter masks
  maskElemSystem = eqnSystems.AddExplicitSystem( "ElemImageMask" ) 
  maskElemSystem.AddConstantMonomialVariable( "maskElem" ) 

  # initialize libMesh data structures
  eqnSystems.init( ) 
  
  # print info
  eqnSystems.PrintSelf() 
  
  # setup imaging to interpolate onto FEM mesh
  femImaging = femLibrary.PytttkImaging(getpot, dimensions ,origin,spacing) 
  # Project imaging onto libMesh data structures
  femImaging.ProjectImagingToFEMMesh("ElemImageMask" ,0.0,imageDataVec,eqnSystems)  
  femImaging.ProjectImagingToFEMMesh("StateSystem"   ,0.0,imageDataVec,eqnSystems)  

  # create dirichlet nodes from this mask
  numNodes = pennesSystem.PetscFEMSystemCreateNodeSetFromMask(config.getfloat("labels","vessel"),dirichletID)
  print "# of dirichlet nodes %d" %numNodes 

  # get image label as numpy array
  imageLabel = maskElemSystem.GetSolutionVector()[...]
  #imageLabel = numpy.floor(10.0*imageLabel.copy())+1
  k_0Label  = imageLabel.copy()
  w_0Label  = imageLabel.copy()
  mu_aLabel = imageLabel.copy()
  mu_sLabel = imageLabel.copy()
  for (idpos,label) in enumerate(imageLabel):
     try:
       tissueType = labelTable["%d"%int(label)]
     except KeyError:
       tissueType = "default"
     labelCount[tissueType] = labelCount[tissueType] + 1
     k_0Label[ idpos] = k_0Table[ tissueType]
     w_0Label[ idpos] = w_0Table[ tissueType]
     mu_aLabel[idpos] = mu_aTable[tissueType]
     mu_sLabel[idpos] = mu_sTable[tissueType]
  # create array of imaging data as petsc vec
  k_0Vec  = PETSc.Vec().createWithArray(k_0Label , comm=PETSc.COMM_SELF)
  w_0Vec  = PETSc.Vec().createWithArray(w_0Label , comm=PETSc.COMM_SELF)
  mu_aVec = PETSc.Vec().createWithArray(mu_aLabel, comm=PETSc.COMM_SELF)
  mu_sVec = PETSc.Vec().createWithArray(mu_sLabel, comm=PETSc.COMM_SELF)
  # copy material properties to the system
  eqnSystems.GetSystem("k_0").SetSolutionVector( k_0Vec )
  eqnSystems.GetSystem("w_0").SetSolutionVector( w_0Vec )
  eqnSystems.GetSystem("mu_a").SetSolutionVector(mu_aVec)
  eqnSystems.GetSystem("mu_s").SetSolutionVector(mu_sVec)

  # write IC
  exodusII_IO = femLibrary.PylibMeshExodusII_IO(femMesh)
  exodusII_IO.WriteTimeStep(MeshOutputFile,eqnSystems, 1, 0.0 )  
  exodusII_IO.WriteParameterSystems(eqnSystems)  
  
  # setup IC 
  pennesSystem.PetscFEMSystemSetupInitialConditions( ) 

  # create list of laser positions
  laserPositionList = [ ((.015,.015,2*spacing[2]+quadratureOffset),
                         (.018,.018,2*spacing[2]+quadratureOffset) ),
                        ((.015,.015,2*spacing[2]+quadratureOffset),
                         (.016,.018,2*spacing[2]+quadratureOffset) ),
                        ((.015,.015,1*spacing[2]+quadratureOffset),
                         (.014,.014,1*spacing[2]+quadratureOffset) ),
                        ((.015,.015,1*spacing[2]+quadratureOffset),
                         (.015,.018,3*spacing[2]+quadratureOffset) )]
  # time stamp
  import pickle,time
  timeStamp =0 
  # set power id to turn laser on 
  pennesSystem.PetscFEMSystemUpdateTimeStep( 1 ) 
  # loop over laser position and solve steady state equations for each position
  #for (idpos,(pos0,pos1)) in enumerate(laserPositionList):
  # loop and read new laser parameters
  while(True):
    if(os.path.getmtime(fem_params['ini_filename']) > timeStamp):
      timeStamp = os.path.getmtime(fem_params['ini_filename'] ) 
      newIni = ConfigParser.SafeConfigParser({})
      newIni.read(fem_params['ini_filename'])

      laserParams = {}
      laserParams['position1'] = [newIni.getfloat("probe",varID ) for varID in ["x_0","y_0","z_0"] ]
      laserParams['position2'] = [newIni.getfloat("probe",varID ) for varID in ["x_1","y_1","z_1"] ]

      print "laser position = ", newIni.getfloat("timestep","power") , laserParams
      pennesSystem.PennesSDASystemUpdateLaserPower(newIni.getfloat("timestep","power"),1)
      pennesSystem.PennesSDASystemUpdateLaserPosition(laserParams['position1'],laserParams['position2'])
      pennesSystem.SystemSolve( ) 
      #fem.StoreTransientSystemTimeStep("StateSystem",timeID ) 
      #exodusII_IO.WriteTimeStep(MeshOutputFile ,eqnSystems, idpos+2, idpos*kwargs['deltat'])  
      exodusII_IO.WriteTimeStep(MeshOutputFile ,eqnSystems, 2, 1.0)  
      exodusII_IO.WriteParameterSystems(eqnSystems)  
      # write to txt file
      if( petscRank ==0 ):
        time.sleep(1)
        vtkExodusIIReader = vtk.vtkExodusIIReader()
        print "opening %s " % MeshOutputFile 
        vtkExodusIIReader.SetFileName( MeshOutputFile )
        vtkExodusIIReader.Update()
        ntime  = vtkExodusIIReader.GetNumberOfTimeSteps()
        variableID = "u0"
        print "ntime %d %s " % (ntime,variableID)
        vtkExodusIIReader.SetTimeStep(1) 
        vtkExodusIIReader.SetPointResultArrayStatus(variableID,1)
        vtkExodusIIReader.Update()
        curInput = None
        # multi block
        if vtkExodusIIReader.GetOutput().IsA("vtkMultiBlockDataSet"):
          iter = vtkExodusIIReader.GetOutput().NewIterator()
          iter.UnRegister(None)
          iter.InitTraversal()
          curInput = iter.GetCurrentDataObject()
        else: 
          curInput = vtkExodusIIReader.GetOutput()
        #fem_point_data= curInput.GetPointData().GetArray('u0') 
        #Soln=vtkNumPy.vtk_to_numpy(fem_point_data)
        #numpy.savetxt( "temperature.txt" ,Soln)

        # FIXME  notice that order of operations is IMPORTANT
        # FIXME   translation followed by rotation will give different results
        # FIXME   than rotation followed by translation
        # FIXME  Translate -> RotateZ -> RotateY -> RotateX -> Scale seems to be the order of paraview
        # scale back to millimeter
        # TODO verify that the data sets are registered in the native slicer coordinate system
        # TODO   segmented data MUST be read in with:
        # TODO add data -> volume -> show options -> ignore orientation
        # TODO add data -> volume -> show options -> ignore orientation
        # TODO add data -> volume -> show options -> ignore orientation
        AffineTransform = vtk.vtkTransform()
        AffineTransform.Translate([ 0.0,0.0,0.0])
        AffineTransform.RotateZ( 0.0  )
        AffineTransform.RotateY( 0.0  )
        AffineTransform.RotateX( 0.0  )
        AffineTransform.Scale([1000.,1000.,1000.])
        # scale to millimeter
        transformFilter = vtk.vtkTransformFilter()
        transformFilter.SetInput(curInput) 
        transformFilter.SetTransform(AffineTransform) 
        transformFilter.Update()

        # setup contour filter
        vtkContour = vtk.vtkContourFilter()
        vtkContour.SetInput( transformFilter.GetOutput() )
        # TODO: not sure why this works...
        # set the array to process at the temperature == u0
        vtkContour.SetInputArrayToProcess(0,0,0,0,'u0')
        contourValuesList  = eval(newIni.get('exec','contours'))
        vtkContour.SetNumberOfContours( len(contourValuesList ) )
        print "plotting array:", vtkContour.GetArrayComponent( )
        for idContour,contourValue in enumerate(contourValuesList):
           print "plotting contour:",idContour,contourValue
           vtkContour.SetValue( idContour,contourValue )
        vtkContour.Update( )

        # setup threshold filter
        vtkThreshold = vtk.vtkThreshold()
        vtkThreshold.SetInput( transformFilter.GetOutput() )
        vtkThreshold.ThresholdByLower( contourValuesList[0] )
        #vtkThreshold.SetSelectedComponent( 0 ) 
        vtkThreshold.SetComponentModeToUseAny( ) 
        vtkThreshold.Update()

        # resample onto image
        vtkResample = vtk.vtkCompositeDataProbeFilter()
        vtkResample.SetInput( vtkImageMask )
        vtkResample.SetSource( vtkThreshold.GetOutput() )
        vtkResample.Update()

        # write output
        print "writing fem.vtk "
        vtkImageWriter = vtk.vtkDataSetWriter()
        vtkImageWriter.SetFileTypeToBinary()
        vtkImageWriter.SetFileName("fem.vtk")
        vtkImageWriter.SetInput(vtkResample.GetOutput())
        vtkImageWriter.Update()

        # write stl file
        stlWriter = vtk.vtkSTLWriter()
        stlWriter.SetInput(vtkContour.GetOutput( ))
        stlWriter.SetFileName("fem.stl")
        stlWriter.SetFileTypeToBinary()
        stlWriter.Write()
        # write support file to signal full stl file is written
        #  Slicer module will wait for this file to be written
        #  before trying to open stl file to avoid incomplete reads
        with open('./fem.finish', 'w') as signalFile:
          signalFile.write("the stl file has been written\n")
 
    else:
      print "waiting on user input.."
      # echo lookup table
      if( petscRank ==0 ):
         print "lookup tables"
         print "labeled %d voxels" % len(imageLabel)
         print "labels"      , labelTable
         print "counts"      , labelCount
         print "conductivity", k_0Table  
         print "perfusion"   , w_0Table  
         print "absorption"  , mu_aTable  
         print "scattering"  , mu_sTable  
      time.sleep(2)
Esempio n. 36
0
def solve_for_combo(c):
    import time
    time_start = -time.time()
    import FVF_loglib as flog
    import FVF_plotlib as fplt
    from . import analyze as fana
    from . import utilities as util
    import petsc4py
    petsc4py.init()
    import slepc4py
    slepc4py.init()
    import dill

    print('working on combination {0}/{1}'.format(c['iter_num'], c['total_iter']))

    data_dir = c['data_dir']
    T = c['T_list']

    # Set up directory to store solution data
    try:
        out_dir = futil.get_out_dir(out_dir_base, data_dir, len(cfg.data_dir), T, len(cfg.T_list))
        futil.ensure_dir(out_dir)
    except:
        print('problem setting out directory')
        return

    # Set up logger
    try:
        logger = flog.setup_custom_logger(dir_name=out_dir, filename='run.log', verbose=cfg.verbose)
    except:
        print('problem creating logger')
        return

    try:
        # Store config file for later reference
        logger.info('used config file {0}.py'.format(config_file))
        futil.store_config_file(config_file, out_dir_base)

        logger.info('Main output directory set to {0}'.format(out_dir_base))
        logger.info('Output subdirectory set to {0}'.format(out_dir))

        # Convert Time in years to model frequency
        t_star = (23.9345*3600)/(2*np.pi)
        Target_j = 2*np.pi/(T*365.25*24*3600/t_star)*1j
        Target = Target_j + Target_j*1j/(2*cfg.target_Q)

        # Find which CC matrix to use
        dCyr_list = futil.find_available_skin_depths(data_dir)
        dCyr_use = futil.find_closest_CC(T, dCyr_list)
        logger.info('{0} dCyr used'.format(dCyr_use))
    except:
        problem = "problem storing config file or finding correct magnetic skin depth"
        logger.error(problem, exc_info=1)
        print('combination {0}/{1} broke, {2}'.format(c['iter_num'], c['total_iter'], problem))

        return

    # %% Load Matrices and model from files
    #==============================================================================
    try:
        viewer = petsc4py.PETSc.Viewer().createBinary(data_dir+fileA+str(dCyr_use)+'.dat', 'r')
        A = petsc4py.PETSc.Mat().load(viewer)
        viewer = petsc4py.PETSc.Viewer().createBinary(data_dir+fileB+'.dat', 'r')
        B = petsc4py.PETSc.Mat().load(viewer)
        try:
            model = dill.load(open(data_dir+filemodel,'rb'))
        except:
            model = dill.load(open(data_dir+filemodel,'rb'),encoding='latin1')
        logger.info('A'+str(dCyr_use)+' matrix used')
        logger.info('matrices and model loaded into memory from ' + data_dir)
    except:
        problem = "Problem loading matrices from file."
        logger.error(problem, exc_info=1)
        print('combination {0}/{1} broke, {2}'.format(c['iter_num'], c['total_iter'], problem))
        return

    # %% Set up SLEPc Solver
    #==============================================================================
    try:
        EPS = slepc4py.SLEPc.EPS().create()
        EPS.setDimensions(num_solutions_to_calculate, petsc4py.PETSc.DECIDE)
        EPS.setOperators(A, B)
        EPS.setType(EPS.Type.KRYLOVSCHUR)
        EPS.setProblemType(slepc4py.SLEPc.EPS.ProblemType.PGNHEP)
        EPS.setTarget(Target)
        EPS.setWhichEigenpairs(EPS.Which.TARGET_MAGNITUDE)
        EPS.setTolerances(tol)
        EPS.setFromOptions()
        ST = EPS.getST()
        ST.setType(slepc4py.SLEPc.ST.Type.SINVERT)
        logger.info('Solver set up, Target Period = {0:.1f}, Number of solutions to calculate = {1}'.format(T, num_solutions_to_calculate))
    except:
        problem = "Problem setting up SLEPc Solver."
        logger.error(problem, exc_info=1)
        print('combination {0}/{1} broke, {2}'.format(c['iter_num'], c['total_iter'], problem))
        return

    # %% Solve Problem
    #==============================================================================
    try:
        EPS.solve()
        logger.info('problem solved')
    except:
        problem = "Problem Solving Eigenvalues."
        logger.error(problem, exc_info=1)
        print('combination {0}/{1} broke, {2}'.format(c['iter_num'], c['total_iter'], problem))
        return
    try:
        # Save Computed Solutions
        conv = EPS.getConverged()
        logger.info('{0} eigenvalues converged'.format(conv))
        vals = []
        vecs = []
        for ind in range(conv):
            vs, ws = petsc4py.PETSc.Mat.getVecs(A)
            v = EPS.getEigenpair(ind, ws)
            vals.append(v)
            vecs.append(ws.getArray())
        Periods = (2*np.pi/np.array([x.imag for x in vals]))*model.t_star/(24.*3600.*365.25)
        Period_max = Periods.max()
        Period_min = Periods.min()
        logger.info('min Period = {0:.1f}yrs, max Period = {1:.1f}yrs'.format(Period_min, Period_max))
    except:
        problem = "Problem Computing Eigenvalues."
        logger.error(problem, exc_info=1)
        print('combination {0}/{1} broke, {2}'.format(c['iter_num'], c['total_iter'], problem))
        return

    #%% Filter Solutions
    #==============================================================================
    try:
        logger.info('Filtering Eigenvalues:')

        # filter results to keep only those that satisfy requirements specified in filter_dict
        fvals, fvecs = fana.filter_results(model, vals, vecs, cfg.filter_dict)

        # Sort by fit to given parameter choices
        svals, svecs = fana.sort_by_total_misfit(model, fvals, fvecs, cfg.sort_dict)

    except:
        try:
            problem = "Problem Saving Eigenvalues."
            logger.error(problem, exc_info=1)
            print('combination {0}/{1} broke, {2}'.format(c['iter_num'], c['total_iter'], problem))
            svals = vals
            svecs = vecs
        except:
            svals = []
            svecs = []
            problem = "Problem Saving Eigenvalues."
            logger.error(problem, exc_info=1)
            print('combination {0}/{1} broke, {2}'.format(c['iter_num'], c['total_iter'], problem))

    # %% Save Filtered Eigenvectors
    #==============================================================================
    try:
        if savefile:
            dill.dump({'vals': svals, 'vecs': svecs, 'model':model},open(out_dir + savefile, 'wb'))
            logger.info('saved {0:d} vals and vecs saved to '.format(len(svals)) + out_dir + savefile)
    except:
        problem = "Problem Saving Eigenvalues."
        logger.error(problem, exc_info=1)
        print('combination {0}/{1} broke, {2}'.format(c['iter_num'], c['total_iter'], problem))
        return
    # %% Plot Filtered Eigenvectors
    #==============================================================================
    try:
        logger.info('Plotting:')

        for ind in range(min(cfg.num_solutions_to_plot,len(svals))):
            val = svals[ind]
            vec = fana.shift_vec_real(model, svecs[ind], var='vth')
            vec = fana.normalize_vec(vec, 10)
            Period = fana.get_period(model, val)
            Q = fana.get_Q(val)
            r_ord = fana.get_order_r(model, vec)
            th_ord = fana.get_order_th(model, vec)
            if abs(Period) < 1.0:
                title = ('{0:03d} k={3}, l={4}, m={5}, T={1:.2f}dys, Q={2:.2f}'.format(ind, Period*365.25, Q, r_ord, th_ord, model.m)
                         + '\nH={:.0f}km, B={:.2f}mT, N={:.2f} pvbc'.format(model.h/1e3, np.mean(model.Br)*model.B_star*1e3, np.mean(model.N)))
            else:
                title = ('{0:03d} k={3}, l={4}, m={5}, T={1:.2f}yrs, Q={2:.2f}'.format(ind, Period, Q, r_ord, th_ord, model.m)
                         + '\nH={:.0f}km, B={:.2f}mT, N={:.2f} pvbc'.format(model.h/1e3, np.mean(model.Br)*model.B_star*1e3, np.mean(model.N)))
            if (model.Nk > 1):
                # fplt.plot_fast_solution(model, vec, title=title, dir_name=out_dir)
                fplt.plot_full_solution(model, val, vec, title=title, dir_name=out_dir, layer_boundary=3380)
            else:
                if('br' in model.model_variables):
                    fplt.plot_1D(model, vec, val, ind, dir_name=out_dir, title=title)
                else:
                    fplt.plot_1D_noB(model, vec, val, ind, dir_name=out_dir, title=title)
            logger.info('\t plotted ind={0}, T={1:.2f}yrs (eig={2:.2e})'.format(ind, Period, val))
        logger.info('run complete')
    except:
        problem = "Problem Plotting Eigenvalues."
        logger.error(problem, exc_info=1)
        print('combination {0}/{1} broke, {2}'.format(c['iter_num'], c['total_iter'], problem))

        return
    dtime = (time_start + time.time())/60.
    print('done with combination {0}/{1} in {2:.2f}min'.format(c['iter_num'], c['total_iter'], dtime))
Esempio n. 37
0
"""
Functions for a high-level PETSc-based parallelization.
"""
from __future__ import absolute_import
import time
import os

import numpy as nm

import sys, petsc4py
from six.moves import range
argv = [arg for arg in sys.argv if arg not in ['-h', '--help']]
petsc4py.init(argv)

from petsc4py import PETSc
from mpi4py import MPI

from sfepy.base.base import assert_, output, ordered_iteritems, Struct
from sfepy.discrete.common.region import Region
from sfepy.discrete.fem.fe_surface import FESurface

def partition_mesh(mesh, n_parts, use_metis=True, verbose=False):
    """
    Partition the mesh cells into `n_parts` subdomains, using metis, if
    available.
    """
    output('partitioning mesh into %d subdomains...' % n_parts, verbose=verbose)
    tt = time.clock()

    if use_metis:
        try:
Esempio n. 38
0
# --------------------------------------------------------------------

if __name__ == "__main__":
    import sys, petsc4py
    petsc4py.init(sys.argv+['-log_summary'])

# --------------------------------------------------------------------

from petsc4py import PETSc
import unittest

# --------------------------------------------------------------------

class TestLog(unittest.TestCase):

    def setUp(self):
        #PETSc.Log.begin()
        # register stages
        self.stage1 = PETSc.Log.Stage('Stage 1')
        self.stage2 = PETSc.Log.Stage('Stage 2')
        # register classes
        self.klassA = PETSc.Log.Class('Class A')
        self.klassB = PETSc.Log.Class('Class B')
        # register events
        self.event1 = PETSc.Log.Event('Event 1') # no class
        self.event2 = PETSc.Log.Event('Event 2') # no class
        self.eventA = PETSc.Log.Event('Event A', self.klassA)
        self.eventB = PETSc.Log.Event('Event B', self.klassB)

    def testGetName(self):
        self.assertEqual(self.klassA.name, 'Class A')
# Kjoring:      python2.6 backward_wave_solver.py

# Import and initialize
import petsc4py, sys, numpy
petsc4py.init(sys.argv)   # kan man legge til input her for aa velge mellom mpich og OpenMPI?
from petsc4py import PETSc

if (len(sys.argv) > 1):
	n = int(sys.argv[1])
else:
	n = 100



# Make temporary spacing under warning
print '\n\n\n'

Nt = 100   # Number of timesteps
C2 = (2/1)**2    # C = dt/dx

### These are big matrices and vectors, so we must eventually use sparse types.
### Using dense for now. Use small n

# Need our two matrices. One tridiag and one diag of size n-by-n
tridiag = PETSc.Mat().createDense(n)
tridiag.setDiagonal(PETSc.Vec().createWithArray([1+2*C2]*n))
for i in range(n-1):   # filling the off-diagonal entries
	tridiag.setValue(i+1,i,-C2)
	tridiag.setValue(i,i+1,-C2)

diagm1 = PETSc.Mat().createDense(n)
Esempio n. 40
0
# FENaPack is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# FENaPack is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with FENaPack.  If not, see <http://www.gnu.org/licenses/>.

# Let PETSc print memory usage at the end
import petsc4py
petsc4py.init(("python", "-malloc_info"))

from defcon import *
from dolfin import *
from matplotlib import pyplot

from fenapack import PCDKSP
from fenapack import PCDAssembler
from fenapack import StabilizationParameterSD

import sys
import argparse


class NavierStokesProblem(BifurcationProblem):
    def __init__(self):
Esempio n. 41
0
   def PPS(self,p,comm):
      """
      Parallel solver implementing a PETSc KSP linear routine over the entire scene as a 
      single patch (PPS = Parallel PETSc Single)
      """
      from _reconutils import get_gcd_vals, laplace_builder, pixelxpixelsolver
      try:
         import mpi4py
         from mpi4py import MPI 
      except:
         sys.stdout.write('mpi4py must be installed and in PYTHONPATH\n')
         sys.exit()
      try:
         import petsc4py
         petsc4py.init([],comm=MPI.COMM_WORLD)
         from petsc4py import PETSc
      except:
         sys.stdout.write('petsc4py must be installed and in PYTHONPATH\n')
         sys.exit()

      PETSc.Sys.Print('Running PPS:  Parallel PETSc (linear) Solver')

      size, rank = comm.Get_size(), comm.Get_rank()
      cols = p.gen.cols

      if rank == 0:
         self.define_solution_space(p)

         address = self._read_address(p)
         metfile, modlen = p.gen.metfile, 3*len(address)
         
         ### get gmat, Cd and d values and send segments to other processors  
         PETSc.Sys.Print('retrieving gmat, cdi, and dvec vals...')
         gmatvals, gmatij, cdiv_temp, dvec_temp = get_gcd_vals(metfile,len(p.scenes),
                  p.gen.doline,p.gen.cols,address,p.gen.gmat_rows)

         ### estimate posterior model pixel x pixel (for PETSc initial guess)
         PETSc.Sys.Print('estimating posterior model...')
         mtilde_temp = pixelxpixelsolver(metfile,len(p.scenes),
                  p.gen.doline,p.gen.cols,address)
         gmatij = gmatij.reshape(-1,3*p.gen.gmat_rows)
         if gmatij.dtype != np.int32:  gmatij = gmatij.astype(np.int32)
         if p.gen.cmlambda < 1.e-12:  del address
        
         ### initialize arrays to store start and end row indices for all procs 
         rowblocks_gmat = np.empty([2,size],dtype=np.int32)
         rowblocks_cdiv = np.empty([2,size],dtype=np.int32)
         rowblocks_dvec = np.empty([2,size],dtype=np.int32)
         rowblocks_mtil = np.empty([2,size],dtype=np.int32)
      
         tag = 1234
         for i in np.arange(1,size):
            comm.Send([np.array([modlen,p.gen.gmat_rows]),MPI.INT], dest=i, tag=tag); tag += 1
      else:
         tag, pararray = 1234 + (rank-1), np.array([-1,-1])
         comm.Recv([pararray,MPI.INT], source=0, tag=tag)
         modlen, p.gen.gmat_rows = pararray[0], pararray[1]
         if modlen < 0 or p.gen.gmat_rows < 0:
            print('\npararray receive command failed in rank '+str(rank))
            PETSc._finalize; MPI.Finalize; sys.exit()

      PETSc.Sys.Print('building gmat, cdiv, and dvec arrays...')
      ### initialize...nnz --> [DIAGONAL,OFF-DIAGONAL] (see PETSc documentation)
      gmat = PETSc.Mat().createAIJ([p.gen.gmat_rows,modlen],nnz=[3,3],comm=comm)
      cdiv = PETSc.Mat().createAIJ([p.gen.gmat_rows,p.gen.gmat_rows],nnz=[1,1],comm=comm)
      dvec = PETSc.Vec().createMPI(p.gen.gmat_rows,comm=comm)
      mtilde_pre = PETSc.Vec().createMPI(modlen,comm=comm)
      ### get the block of rows owned by this processor 
      sr_gmat, er_gmat = gmat.getOwnershipRange()
      sr_cdiv, er_cdiv = cdiv.getOwnershipRange()
      sr_dvec, er_dvec = dvec.getOwnershipRange()
      sr_mtil, er_mtil = mtilde_pre.getOwnershipRange()

      ### send start/end rows to root proc and receive gmat, cdiv, and dvec entries 
      base_tag, base_stag = 777, 999
      if rank == 0:
         rowblocks_gmat[0,0], rowblocks_gmat[1,0] = sr_gmat, er_gmat
         rowblocks_cdiv[0,0], rowblocks_cdiv[1,0] = sr_cdiv, er_cdiv
         rowblocks_dvec[0,0], rowblocks_dvec[1,0] = sr_dvec, er_dvec
         rowblocks_mtil[0.0], rowblocks_mtil[1,0] = sr_mtil, er_mtil
         tag,stag = base_tag, base_stag
         recsimple = np.array([-1,-1])
         for i in np.arange(1,size):
            comm.Recv([recsimple,MPI.INT],source=i,tag=tag); tag += 1
            rowblocks_gmat[0,i], rowblocks_gmat[1,i] = recsimple[0], recsimple[1]
            comm.Recv([recsimple,MPI.INT],source=i,tag=tag); tag += 1
            rowblocks_cdiv[0,i], rowblocks_cdiv[1,i] = recsimple[0], recsimple[1]
            comm.Recv([recsimple,MPI.INT],source=i,tag=tag); tag += 1
            rowblocks_dvec[0,i], rowblocks_dvec[1,i] = recsimple[0], recsimple[1]
            comm.Recv([recsimple,MPI.INT],source=i,tag=tag); tag += 1
            rowblocks_mtil[0,i], rowblocks_mtil[1,i] = recsimple[0], recsimple[1]

            svec = gmatvals[3*rowblocks_gmat[0,i]:3*rowblocks_gmat[1,i]]  
            comm.Send([svec, MPI.FLOAT], dest=i, tag=stag); stag += 1

            svec = gmatij[:,3*rowblocks_gmat[0,i]:3*rowblocks_gmat[1,i]].flatten()
            comm.Send([svec, MPI.INT], dest=i, tag=stag); stag += 1

            svec = cdiv_temp[rowblocks_cdiv[0,i]:rowblocks_cdiv[1,i]]
            comm.Send([svec, MPI.FLOAT], dest=i, tag=stag); stag += 1

            svec = dvec_temp[rowblocks_dvec[0,i]:rowblocks_dvec[1,i]]
            comm.Send([svec, MPI.FLOAT], dest=i, tag=stag); stag += 1

            svec = mtilde_temp[rowblocks_mtil[0,i]:rowblocks_mtil[1,i]]
            comm.Send([svec, MPI.FLOAT], dest=i, tag=stag); stag += 1

         if size > 1: del svec

         gmatvals = gmatvals[3*rowblocks_gmat[0,0]:3*(rowblocks_gmat[1,0])]
         gmatij = gmatij[:,3*rowblocks_gmat[0,0]:3*(rowblocks_gmat[1,0])]
         cdiv_temp = cdiv_temp[rowblocks_cdiv[0,0]:rowblocks_cdiv[1,0]]
         dvec_temp = dvec_temp[rowblocks_dvec[0,0]:rowblocks_dvec[1,0]]
         mtilde_temp = mtilde_temp[rowblocks_mtil[0,0]:rowblocks_mtil[1,0]]

      else:
         tag = base_tag + (rank-1)*4
         comm.Send([np.array([sr_gmat, er_gmat]),MPI.INT], dest=0, tag=tag); tag += 1
         comm.Send([np.array([sr_cdiv, er_cdiv]),MPI.INT], dest=0, tag=tag); tag += 1
         comm.Send([np.array([sr_dvec, er_dvec]),MPI.INT], dest=0, tag=tag); tag += 1
         comm.Send([np.array([sr_mtil, er_mtil]),MPI.INT], dest=0, tag=tag); tag += 1

         stag = base_stag + (rank-1)*5

         gmatvals = np.empty(3*(er_gmat-sr_gmat), dtype=np.float32)
         gmatij = np.empty(2*3*(er_gmat-sr_gmat), dtype=np.int32)
         cdiv_temp = np.empty((er_cdiv-sr_cdiv), dtype=np.float32)
         dvec_temp = np.empty((er_dvec-sr_dvec), dtype=np.float32)
         mtilde_temp = np.empty((er_mtil-sr_mtil), dtype=np.float32)

         comm.Recv([gmatvals,MPI.FLOAT],source=0, tag=stag); stag += 1
         comm.Recv([gmatij,MPI.INT],source=0, tag=stag); stag += 1
         comm.Recv([cdiv_temp,MPI.FLOAT],source=0, tag=stag); stag += 1
         comm.Recv([dvec_temp,MPI.FLOAT],source=0, tag=stag); stag += 1
         comm.Recv([mtilde_temp,MPI.FLOAT],source=0, tag=stag); stag += 1
         gmatij = gmatij.reshape(2,-1)

      PETSc.Sys.Print('loading gmat, cdiv, and dvec sparse matrices...')
      for i in np.arange(len(dvec_temp)):
         threei = 3*i
         gmat.setValue(gmatij[0,threei], gmatij[1,threei], gmatvals[threei])
         gmat.setValue(gmatij[0,threei], gmatij[1,threei+1], gmatvals[threei+1])
         gmat.setValue(gmatij[0,threei], gmatij[1,threei+2], gmatvals[threei+2])
         cdiv.setValue(gmatij[0,threei], gmatij[0,threei], cdiv_temp[i])
         dvec.setValue(gmatij[0,threei], dvec_temp[i])
      comm.Barrier()
      dvec.assemblyBegin(); dvec.assemblyEnd()  # MAT_FINAL_ASSEMBLY is implied 
      gmat.assemblyBegin(); gmat.assemblyEnd()
      cdiv.assemblyBegin(); cdiv.assemblyEnd()
      comm.Barrier()
      for i in np.arange(len(mtilde_temp)):
         mtilde_pre.setValue(i+sr_mtil, mtilde_temp[i])
      comm.Barrier()
      mtilde_pre.assemblyBegin(); mtilde_pre.assemblyEnd()
      comm.Barrier()
      del gmatvals, gmatij, cdiv_temp, dvec_temp, mtilde_temp

      ### build A and gtilde (=dtilde)

      omega = gmat.transposeMatMult(cdiv)
      cdiv.destroy()

      gtg = omega.matMult(gmat)
      mtilde,gtilde = gtg.getVecs() 
      omega.mult(dvec,gtilde)
      mtilde_pre.copy(mtilde)
      dvec.destroy(); omega.destroy(); gmat.destroy(); mtilde_pre.destroy()

      if p.gen.cmlambda > 1.e-12:

         PETSc.Sys.Print('building Laplacian...')

         if rank == 0:
            try:
               aa = len(address)
               if 3*aa != modlen: 
                  address = self._read_address(p)
            except:
               address = self._read_address(p)
            fmatval, fmatij = laplace_builder(p.gen.cols,address)

            fmatval = fmatval[:np.argmin(fmatval)]
            fmatij = fmatij.reshape(2,-1)[:,:len(fmatval)]
            if fmatij.dtype != np.int32: fmatij = fmatij.astype(np.int32)
            del address

            rowblocks_fmat = np.empty([2,size],dtype=np.int32)

         fmat = PETSc.Mat().createAIJ([modlen, modlen],nnz=[5,5],comm=comm) 

         ### get the row addresses for this processor 
         sr_gtg, er_gtg = gtg.getOwnershipRange()
         sr_fmat, er_fmat = fmat.getOwnershipRange()

         base_tag, base_stag = 77, 444
         if rank == 0:
            rowblocks_fmat[0,0], rowblocks_fmat[1,0] = sr_fmat, er_fmat
            tag, stag = base_tag, base_stag
            recarray = np.array([-1,-1])
            for i in np.arange(1,size):
               comm.Recv([recarray,MPI.INT],source=i,tag=tag); tag += 1
               rowblocks_fmat[:,i] = recarray
               # get indexes for fmatval and fmatij
               left  = np.argmin(np.abs(fmatij[0,:]-rowblocks_fmat[0,i]))
               right = np.argmin(np.abs(fmatij[0,:]-rowblocks_fmat[1,i]))

               svec = fmatval[left:right]
               lensvec = np.array([len(svec)],dtype=np.int32)

               comm.Send([lensvec,MPI.INT], dest=i, tag=stag); stag += 1
               comm.Send([svec,MPI.FLOAT], dest=i, tag=stag); stag += 1                

               svec = fmatij[:,left:right].flatten()
               comm.Send([svec,MPI.INT], dest=i, tag=stag); stag += 1

            if size > 1: del svec
            left  = np.argmin(np.abs(fmatij[0,:]-rowblocks_fmat[0,0]))
            right = np.argmin(np.abs(fmatij[0,:]-rowblocks_fmat[1,0]))
            fmatval = fmatval[left:right]
            fmatij = fmatij[:,left:right]

         else:
            tag, stag = base_tag + (rank-1), base_stag + (rank-1)*3
            lenrec = np.array([-1],dtype=np.int32)
            comm.Send([np.array([sr_fmat, er_fmat]),MPI.INT], dest=0, tag=tag); tag += 1
            comm.Recv([lenrec,MPI.INT], source=0, tag=stag); stag += 1
            lenrec = lenrec[0]

            fmatval = np.empty(lenrec,dtype=np.float32)
            fmatij  = np.empty(2*lenrec,dtype=np.int32)

            comm.Recv([fmatval,MPI.FLOAT], source=0, tag=stag); stag += 1
            comm.Recv([fmatij,MPI.INT], source=0, tag=stag); stag += 1 
            fmatij = fmatij.reshape(2,-1)


         PETSc.Sys.Print('loading sparse Laplacian...')
         for i in np.arange(len(fmatval)):
            fmat.setValue(fmatij[0,i],fmatij[1,i],fmatval[i])
         comm.Barrier()
         fmat.assemblyBegin(); fmat.assemblyEnd()
         comm.Barrier()
         del fmatval, fmatij

         PETSc.Sys.Print('loading cli...')
         cliv = PETSc.Vec().createMPI(modlen,comm=comm)
         gtg.getDiagonal(cliv)
         cli = gtg.duplicate(copy=False)
         cli.setDiagonal(cliv)
         cliv.destroy()

         PETSc.Sys.Print('building C_m...')
         rhs = fmat.transposeMatMult(cli)
         rhs = rhs.matMult(fmat)
         cli.destroy(); fmat.destroy()

         PETSc.Sys.Print('building A...')
         gtg.axpy(p.gen.cmlambda,rhs)
         rhs.destroy()

      #if p.gen.outputs['Output diag(G^T*W*G)^-1)']:
         #PETSc.Sys.Print('Output diag(G^T*W*G)^-1) option not implemented...run vector_disp for estimate')
      '''
         ### this part of the code works, but the inverse operator is not implemented due to expense 
         PETSc.Sys.Print('writing diagonal of posterior model covariance matrix')
         cliv = PETSc.Vec().createMPI(modlen,comm=comm)
         gtg.getDiagonal(cliv)
         
         base_tag, base_stag = 2222, 3333
         sr_mt, er_mt = cliv.getOwnershipRange()   
         if rank == 0:
            clivnump = np.empty(modlen, dtype=np.float32)
            clivnump[sr_mt:er_mt] = cliv[...]; cliv.destroy()

            rowarr = np.array([-1,-1], dtype=np.int32)  
            tag = base_tag
            for i in np.arange(1,size):
               comm.Recv([rowarr,MPI.INT], source=i, tag=tag); tag += 1
               comm.Recv([clivnump[rowarr[0]:rowarr[1]], MPI.FLOAT], source=i, tag=tag); tag += 1

            self._write_post_model(model=clivnump,p=p,form=1)
         else:
            rowarr, tag = np.array([sr_mt, er_mt], dtype=np.int32), base_tag + (rank-1)*2
            clivnump = cliv[...]; cliv.destroy()

            if clivnump.dtype != np.float32:  clivnump = clivnump.astype(np.float32)
            comm.Send([rowarr,MPI.INT], dest=0, tag=tag); tag += 1
            comm.Send([clivnump, MPI.FLOAT], dest=0, tag=tag); tag += 1
      '''

      PETSc.Sys.Print('inverting using PETSc lsqr...') 
      ksp = PETSc.KSP().create(comm)
      ksp.setType('lsqr')
      ksp.pc.setType('none')
      ksp.setOperators(gtg) 
      self._get_ksp_tols(ksp=ksp,p=p)
      ksp.setTolerances(rtol=self.rtol,atol=self.atol,divtol=self.dtol,max_it=self.max_it)
      t0 = time.time()
      ksp.solve(gtilde,mtilde)
      tf = time.time()

      gtg.destroy(); gtilde.destroy()

      PETSc.Sys.Print('  lsqr took %10.2f seconds' % (tf-t0))
      PETSc.Sys.Print('  Converged in %d iterations ' % (ksp.getIterationNumber()))
      PETSc.Sys.Print('  Tolerances: %e %e %d %d' % (ksp.getTolerances()))
      PETSc.Sys.Print('  Convergance code (< 0 --> diverged): %d\n' % (ksp.getConvergedReason()))

      ksp.destroy()

      base_tag, base_stag = 2222, 3333
      sr_mt, er_mt = mtilde.getOwnershipRange() 
      if rank == 0:    
         mtildenump = np.empty(modlen, dtype=np.float32)
         mtildenump[sr_mt:er_mt] = mtilde[...]; mtilde.destroy()
         mtildenump *= p.gen.timefac * p.gen.distfac * p.gen.r2dsp

         rowarr = np.array([-1,-1], dtype=np.int32)
         tag = base_tag
         for i in np.arange(1,size):
            comm.Recv([rowarr,MPI.INT], source=i, tag=tag); tag += 1
            comm.Recv([mtildenump[rowarr[0]:rowarr[1]], MPI.FLOAT], source=i, tag=tag); tag += 1
         self._write_post_model(model=mtildenump,p=p)

         self._cleanup(p=p)
      else:
         rowarr, tag = np.array([sr_mt, er_mt], dtype=np.int32), base_tag + (rank-1)*2
         mtildenump = mtilde[...]; mtilde.destroy()
         mtildenump *= p.gen.timefac * p.gen.distfac * p.gen.r2dsp

         if mtildenump.dtype != np.float32:  mtildenump = mtildenump.astype(np.float32) 
         comm.Send([rowarr,MPI.INT], dest=0, tag=tag); tag += 1
         comm.Send([mtildenump, MPI.FLOAT], dest=0, tag=tag); tag += 1

      PETSc._finalize
      MPI.Finalize
def deltapModeling(**kwargs):
  """
  treatment planning model 
  """
  # import petsc and numpy
  import petsc4py, numpy
  # init petsc
  PetscOptions =  sys.argv
  PetscOptions.append("-ksp_monitor")
  PetscOptions.append("-ksp_rtol")
  PetscOptions.append("1.0e-15")
  #PetscOptions.append("-help")
  #PetscOptions.append("-idb")
  petsc4py.init(PetscOptions)
  #
  # break processors into separate communicators
  from petsc4py import PETSc
  petscRank = PETSc.COMM_WORLD.getRank()
  petscSize = PETSc.COMM_WORLD.Get_size()
  sys.stdout.write("petsc rank %d petsc nproc %d\n" % (petscRank, petscSize))

  # set shell context
  # TODO import vtk should be called after femLibrary ???? 
  # FIXME WHY IS THIS????
  import femLibrary
  # initialize libMesh data structures
  libMeshInit = femLibrary.PyLibMeshInit(PetscOptions,PETSc.COMM_WORLD) 
  
  # store control variables
  getpot = femLibrary.PylibMeshGetPot(PetscOptions) 
  # from Duck table 2.11 pig dermis 
  getpot.SetIniValue( "material/specific_heat","3280.0" ) 
  # set ambient temperature 
  getpot.SetIniValue( "initial_condition/u_init","0.0" ) 
  # from Duck 2.7 Rat tumor measured in vivo
  getpot.SetIniValue( "thermal_conductivity/k_0_tumor","0.32" ) 
  getpot.SetIniValue( "thermal_conductivity/k_0_healthy",kwargs['cv']['k_0_healthy'] ) 
  # water properties at http://www.d-a-instruments.com/light_absorption.html
  getpot.SetIniValue( "optical/mu_a_healthy", kwargs['cv']['mu_a_healthy'] ) 
  # FIXME large mu_s (> 30) in agar causing negative fluence to satisfy BC 
  getpot.SetIniValue( "optical/mu_s_healthy",
              kwargs['cv']['mu_s_healthy'] ) 
  # from AE paper
  #http://scitation.aip.org/journals/doc/MPHYA6-ft/vol_36/iss_4/1351_1.html#F3

  #For SPIOs
  #getpot.SetIniValue( "optical/guass_radius","0.0035" ) 
  #For NR/NS
  getpot.SetIniValue( "optical/guass_radius",".003" )

  # cauchy BC
  getpot.SetIniValue( "bc/u_infty","0.0" )
  getpot.SetIniValue( "bc/newton_coeff","1.0e5" )

  # 1-300
  getpot.SetIniValue( "optical/mu_a_tumor",
              kwargs['cv']['mu_a_tumor'] ) 
  # 1-300
  getpot.SetIniValue( "optical/mu_s_tumor",
              kwargs['cv']['mu_s_tumor'] ) 
  #getpot.SetIniValue( "optical/mu_a_tumor","71.0" ) 
  #getpot.SetIniValue( "optical/mu_s_tumor","89.0" ) 
  # .9  - .99
  getpot.SetIniValue( "optical/anfact",kwargs['cv']['anfact'] ) 
  #agar length

  #for SPIOs
  #getpot.SetIniValue( "optical/agar_length","0.0206" ) 
  #for NR/NS
  getpot.SetIniValue( "optical/agar_length","0.023" )

  getpot.SetIniValue("optical/refractive_index","1.0")
  #
  #  given the original orientation as two points along the centerline z = x2 -x1
  #     the transformed orienteation would be \hat{z} = A x2 + b - A x1 - b = A z
  #  ie transformation w/o translation which is exactly w/ vtk has implemented w/ TransformVector
  #  TransformVector = TransformPoint - the transation
  #Setup Affine Transformation for registration
  RotationMatrix = [[1.,0.,0.],
                    [0.,1.,0.],
                    [0.,0.,1.]]
  Translation =     [0.,0.,0.]
  # original coordinate system laser input
  # For SPIOs 67_11
  #laserTip         =  [0.,-.000625,.035] 
  # For SPIOs 67_10
  #laserTip         =  [0.,-.0001562,.035]
  # For SPIOs 335_11
  #laserTip         =  [0.,-.0014062,.035]
  # For NR		
  #laserTip         =  [0.,.0002,.035]
  # For NS
  laserTip         =  [0.00001,.000001,.000001]

  laserOrientation =  [0.0,0.,-1.0] 
  
  import vtk
  import vtk.util.numpy_support as vtkNumPy 
  # echo vtk version info
  print "using vtk version", vtk.vtkVersion.GetVTKVersion()
  # FIXME  notice that order of operations is IMPORTANT
  # FIXME   translation followed by rotation will give different results
  # FIXME   than rotation followed by translation
  # FIXME  Translate -> RotateZ -> RotateY -> RotateX -> Scale seems to be the order of paraview
  AffineTransform = vtk.vtkTransform()
  # should be in meters

  # For NS
  #AffineTransform.Translate([ .01320,-.0037,0.00000010])
  #AffineTransform.Translate([ .005,-.0035,0.0])
  AffineTransform.Translate([-.006,-.0022,0.0])
  AffineTransform.RotateZ( 0.0 )
  AffineTransform.RotateY( -90.0 )
  AffineTransform.RotateX( 0.0 )
  AffineTransform.Scale([1.,1.,1.])


  # get homogenius 4x4 matrix  of the form
  #               A | b
  #    matrix =   -----
  #               0 | 1
  #   
  matrix = AffineTransform.GetConcatenatedTransform(0).GetMatrix()
  #print matrix 
  RotationMatrix = [[matrix.GetElement(0,0),matrix.GetElement(0,1),matrix.GetElement(0,2)],
                    [matrix.GetElement(1,0),matrix.GetElement(1,1),matrix.GetElement(1,2)],
                    [matrix.GetElement(2,0),matrix.GetElement(2,1),matrix.GetElement(2,2)]]
  Translation =     [matrix.GetElement(0,3),matrix.GetElement(1,3),matrix.GetElement(2,3)] 
  #print RotationMatrix ,Translation 
  

  laserTip         =  AffineTransform.TransformPoint(  laserTip )
  laserOrientation =  AffineTransform.TransformVector( laserOrientation )

  # set laser orientation values
  getpot.SetIniValue( "probe/x_0","%f" % laserTip[0]) 
  getpot.SetIniValue( "probe/y_0","%f" % laserTip[1]) 
  getpot.SetIniValue( "probe/z_0","%f" % laserTip[2]) 
  getpot.SetIniValue( "probe/x_orientation","%f" % laserOrientation[0] ) 
  getpot.SetIniValue( "probe/y_orientation","%f" % laserOrientation[1] ) 
  getpot.SetIniValue( "probe/z_orientation","%f" % laserOrientation[2] ) 
  
  # initialize FEM Mesh
  femMesh = femLibrary.PylibMeshMesh()
  # must setup Ini File first
  #For SPIOs
  #femMesh.SetupUnStructuredGrid( "/work/01741/cmaclell/data/mdacc/deltap_phantom_oct10/phantomMeshFullTess.e",0,RotationMatrix, Translation  ) 
  #For NS/NR
  RotationMatrix = [[1,0,0],
                    [0,1,0],
                    [0,0,1]]
  Translation =     [.0000001,.00000001,.000001]
  #
  femMesh.SetupUnStructuredGrid( "./sphereMesh.e",0,RotationMatrix, Translation  )
  #femMesh.SetupUnStructuredGrid( "phantomMesh.e",0,RotationMatrix, Translation  )

  MeshOutputFile = "fem_data.%04d.e" % kwargs['fileID'] 
  #femMes.SetupStructuredGrid( (10,10,4) ,[0.0,1.0],[0.0,2.0],[0.0,1.0]) 
  
  # add the data structures for the background system solve
  # set deltat, number of time steps, power profile, and add system
  nsubstep = 1
  #for SPIOs
  #acquisitionTime = 4.9305
  #for NS/NR
  acquisitionTime = 5.09
  deltat = acquisitionTime / nsubstep
  ntime  = 60 
  eqnSystems =  femLibrary.PylibMeshEquationSystems(femMesh,getpot)
  #For SPIOs
  #getpot.SetIniPower(nsubstep,  [ [1,6,30,ntime],[1.0,0.0,4.5,0.0] ])
  #For NS/NR
  getpot.SetIniPower(nsubstep,  [ [1,6,42,ntime],[1.0,0.0,1.13,0.0] ])
  deltapSystem = eqnSystems.AddPennesDeltaPSystem("StateSystem",deltat) 
  deltapSystem.AddStorageVectors(ntime)

  # hold imaging
  mrtiSystem = eqnSystems.AddExplicitSystem( "MRTI" ) 
  mrtiSystem.AddFirstLagrangeVariable( "u0*" ) 
  mrtiSystem.AddStorageVectors(ntime)
  maskSystem = eqnSystems.AddExplicitSystem( "ImageMask" ) 
  maskSystem.AddFirstLagrangeVariable( "mask" ) 
  
  # initialize libMesh data structures
  eqnSystems.init( ) 
  
  # print info
  eqnSystems.PrintSelf() 
  
  # write IC
  exodusII_IO = femLibrary.PylibMeshExodusII_IO(femMesh)
  exodusII_IO.WriteTimeStep(MeshOutputFile,eqnSystems, 1, 0.0 )  
  
  # read imaging data geometry that will be used to project FEM data onto
  #For 67_11
  #vtkReader.SetFileName('/work/01741/cmaclell/data/mdacc/deltap_phantom_oct10/spio/spioVTK/67_11/tmap_67_11.0000.vtk')
  #For 67_10
  #vtkReader.SetFileName('/work/01741/cmaclell/data/mdacc/deltap_phantom_oct10/spio/spioVTK/67_10/tmap_67_10.0000.vtk')
  #For 335_11 
  #vtkReader.SetFileName('/work/01741/cmaclell/data/mdacc/deltap_phantom_oct10/spio/spioVTK/335_11/tmap_335_11.0000.vtk')
  #For NS
  #vtkReader.SetFileName('/work/01741/cmaclell/data/mdacc/deltap_phantom_oct10/nrtmapsVTK/S695/S695.0000.vtk') 
  #For NR
  imageFileNameTemplate = '/work/01741/cmaclell/data/mdacc/NanoMouseJune12/matlab_VTK/control_1_tmap.%04d.vtk'
  imageFileNameTemplate = '/FUS4/data2/CJM/SPIO_mice/matlab_VTK/control_1_tmap.%04d.vtk'
  #imageFileNameTemplate = "/share/work/fuentes/deltap_phantom_oct10/nrtmapsVTK/R695/R695.%04d.vtk"
  #imageFileNameTemplate = "/data/fuentes/mdacc/deltap_phantom_oct10/nrtmapsVTK/R695/R695.%04d.vtk"

  #vtkReader = vtk.vtkXMLImageDataReader() 
  vtkReader = vtk.vtkDataSetReader() 
  vtkReader.SetFileName( imageFileNameTemplate % 0 )
  vtkReader.Update()
  templateImage = vtkReader.GetOutput()
  dimensions = templateImage.GetDimensions()
  spacing = templateImage.GetSpacing()
  origin  = templateImage.GetOrigin()
  print spacing, origin, dimensions
  femImaging = femLibrary.PytttkImaging(getpot, dimensions ,origin,spacing) 
  
  ObjectiveFunction = 0.0
  # loop over time steps and solve
  for timeID in range(1,ntime*nsubstep):
  #for timeID in range(1,10):
     # project imaging onto fem mesh
     vtkImageReader = vtk.vtkDataSetReader() 
     vtkImageReader.SetFileName( imageFileNameTemplate % timeID )
     vtkImageReader.Update() 
     image_cells = vtkImageReader.GetOutput().GetPointData() 
     data_array = vtkNumPy.vtk_to_numpy(image_cells.GetArray('scalars')) 
     v1 = PETSc.Vec().createWithArray(data_array, comm=PETSc.COMM_SELF)
     femImaging.ProjectImagingToFEMMesh("MRTI",0.0,v1,eqnSystems)  
     mrtiSystem.StoreSystemTimeStep(timeID ) 
  
     # create image mask 
     #  The = operator for numpy arrays just copies by reference
     #   .copy() provides a deep copy (ie physical memory copy)
     image_mask = data_array.copy().reshape(dimensions,order='F')
     # Set all image pixels to large value
     largeValue = 1.e6
     image_mask[:,:] = 1 
     # RMS error will be computed within this ROI/VOI imagemask[xcoords/column,ycoords/row]
     #image_mask[93:153,52:112] = 1.0
     #image_mask[98:158,46:106] = 1.0
     v2 = PETSc.Vec().createWithArray(image_mask, comm=PETSc.COMM_SELF)
     femImaging.ProjectImagingToFEMMesh("ImageMask",largeValue,v2,eqnSystems)  
     #print mrti_array
     #print type(mrti_array)

     print "time step = " ,timeID
     eqnSystems.UpdatePetscFEMSystemTimeStep("StateSystem",timeID ) 
     deltapSystem.SystemSolve()
     #eqnSystems.StoreTransientSystemTimeStep("StateSystem",timeID ) 
  
     # accumulate objective function
     # 
     # qoi  = ( (FEM - MRTI) / ImageMask)^2
     # 
     qoi = femLibrary.WeightedL2Norm( deltapSystem,"u0",
                                      mrtiSystem,"u0*",
                                      maskSystem,"mask" ) 
     ObjectiveFunction =( ObjectiveFunction + qoi) 
    
     # control write output
     writeControl = True
     if ( timeID%nsubstep == 0 and writeControl ):
       # write exodus file
       exodusII_IO.WriteTimeStep(MeshOutputFile,eqnSystems, timeID+1, timeID*deltat )  
       # Interpolate FEM onto imaging data structures
       if (vtk != None):
         vtkExodusIIReader = vtk.vtkExodusIIReader()
         vtkExodusIIReader.SetFileName(MeshOutputFile )
         vtkExodusIIReader.SetPointResultArrayStatus("u0",1)
         vtkExodusIIReader.SetTimeStep(timeID-1) 
         vtkExodusIIReader.Update()
         # reflect
         vtkReflect = vtk.vtkReflectionFilter()
         vtkReflect.SetPlaneToYMax()
         vtkReflect.SetInput( vtkExodusIIReader.GetOutput() )
         vtkReflect.Update()
         # reuse ShiftScale Geometry
         vtkResample = vtk.vtkCompositeDataProbeFilter()
         vtkResample.SetInput( vtkImageReader.GetOutput() )
         vtkResample.SetSource( vtkReflect.GetOutput() ) 
         vtkResample.Update()
         fem_point_data= vtkResample.GetOutput().GetPointData() 
         fem_array = vtkNumPy.vtk_to_numpy(fem_point_data.GetArray('u0')) 
         #print fem_array 
         #print type(fem_array )
       # only rank 0 should write
       #if ( petscRank == 0 ):
       #   print "writing ", timeID
       #   vtkTemperatureWriter = vtk.vtkDataSetWriter()
       #   vtkTemperatureWriter.SetFileTypeToBinary()
       #   vtkTemperatureWriter.SetFileName("invspio_67_11.%04d.vtk" % timeID )
       #   vtkTemperatureWriter.SetInput(vtkResample.GetOutput())
       #   vtkTemperatureWriter.Update()
  print 'Objective Fn'
  print ObjectiveFunction
  retval = dict([])
  retval['fns'] = [ObjectiveFunction *10000000.]
  retval['rank'] = petscRank 
  return(retval)
Esempio n. 43
0
def test_petgem():
    # ---------------------------------------------------------------
    # PETSc init
    # ---------------------------------------------------------------
    petsc4py.init(sys.argv)
    # ---------------------------------------------------------------
    # Load python modules
    # ---------------------------------------------------------------
    # ---------------------------------------------------------------
    # Load petgem modules (BSC)
    # ---------------------------------------------------------------
    from petgem.common import Print, InputParameters, Timers
    from petgem.parallel import MPIEnvironment
    from petgem.preprocessing import Preprocessing
    from petgem.solver import Solver

    # ---------------------------------------------------------------
    # Load system setup (both parameters and dataset configuration)
    # ---------------------------------------------------------------
    # Obtain the MPI environment
    parEnv = MPIEnvironment()

    # Import parameters file
    inputSetup = InputParameters('tests/data/params.yaml', parEnv)

    # Initialize timers
    Timers(inputSetup.output.directory)

    # ---------------------------------------------------------------
    # Print header
    # ---------------------------------------------------------------
    Print.header()

    # ---------------------------------------------------------------
    # Initialize preprocessing and timers
    # ---------------------------------------------------------------
    Print.master(' ')
    Print.master('  Data preprocessing')

    # Create a preprocessing instance and output directory
    preprocessing = Preprocessing()

    # Run preprocessing
    preprocessing.run(inputSetup)

    # ---------------------------------------------------------------
    # Initialize and execute the solver
    # ---------------------------------------------------------------
    Print.master(' ')
    Print.master('  Run modelling')

    # Create a solver instance
    csem_solver = Solver()

    # Setup solver (import files from preprocessing stage)
    csem_solver.setup(inputSetup)

    # Assembly linear system
    csem_solver.assembly(inputSetup)

    # Set dirichlet boundary conditions
    csem_solver.solve()

    # Compute electromagnetic responses
    csem_solver.postprocess(inputSetup)

    # Remove output directory
    shutil.rmtree(inputSetup.output.directory)
Esempio n. 44
0
#!/usr/bin/python

# interpolate scalar gradient onto nedelec space
from dolfin import *

import petsc4py
import sys

petsc4py.init(sys.argv)

from petsc4py import PETSc
Print = PETSc.Sys.Print
# from MatrixOperations import *
import numpy as np
#import matplotlib.pylab as plt
import PETScIO as IO
import common
import scipy
import scipy.io
import time

import BiLinear as forms
import IterOperations as Iter
import MatrixOperations as MO
import CheckPetsc4py as CP
import ExactSol
import Solver as S
import MHDmatrixPrecondSetup as PrecondSetup
import NSprecondSetup
import MHDallatonce as MHDpreconditioner
m = 7
Esempio n. 45
0
        else:
            namespace[name] = getattr(module, name)


if not imported_from_sphinx:
    import petsc4py

    try:
        # Look if petsc4py has already been initialized
        PETSc = petsc4py.__getattribute__('PETSc')
    except AttributeError:
        # If not, initialize petsc4py with the PETSc that PISM was compiled against.
        import sys
        import PISM.version_info
        try:
            petsc4py.init(sys.argv, arch=PISM.version_info.PETSC_ARCH)
        except TypeError:
            # petsc4py on Debian 9 does not recognize the PETSC_ARCH of PETSc in the .deb package
            petsc4py.init(sys.argv)
        from petsc4py import PETSc

    import PISM.cpp

    import_symbols(PISM.cpp, globals())

else:  # pragma: no cover
    # The following constants will be imported from 'cpp' if we are not
    # running inside sphinx.  But if we are inside sphinx, then we'll
    # need them to be able to import submodules.
    WITH_GHOSTS = True
    WITHOUT_GHOSTS = False