예제 #1
0
    def test_all(self):
        cls = self.__class__
        rank = cls.comm.getRank()

        file_dir = os.path.dirname(__file__)

        # Create an Albany problem:
        filename = 'input_conductivity_dist_paramT.yaml'
        problem = Utils.createAlbanyProblem(file_dir+'/'+filename, cls.parallelEnv)

        parameter_map    = problem.getParameterMap(0)
        parameter        = Tpetra.MultiVector(parameter_map, 1, dtype="d")
        num_elems        = parameter_map.getNodeNumElements()
        parameter[0, :]  = 2.0*np.ones(num_elems)
    

        problem.performSolve()
        state_map    = problem.getStateMap()
        state        = Tpetra.MultiVector(state_map, 1, dtype="d")
        state[0, :]  = problem.getState()
        state_ref    = Utils.loadMVector('state_ref', 1, state_map, distributedFile=False, useBinary=False, readOnRankZero=True)
        

        stackedTimer = problem.getStackedTimer()
        setup_time = stackedTimer.accumulatedTime("PyAlbany: Setup Time")
        print("setup_time = " + str(setup_time))
        tol = 1.e-8
        self.assertTrue(np.linalg.norm(state_ref[0, :] - state[0,:]) < tol)
예제 #2
0
    def test_all(self):
        cls = self.__class__
        rank = cls.comm.getRank()

        file_dir = os.path.dirname(__file__)

        # Create an Albany problem:
        filename = 'input_conductivity_dist_paramT.yaml'
        problem = Utils.createAlbanyProblem(file_dir+'/'+filename, cls.parallelEnv)

        n_vecs = 4
        parameter_map = problem.getParameterMap(0)
        num_elems     = parameter_map.getNodeNumElements()
        
        # generate vectors with random entries
        omega = Tpetra.MultiVector(parameter_map, n_vecs, dtype="d")
        for i in range(n_vecs):
            omega[i,:] = np.random.randn(num_elems)
        
        # call the orthonormalization method
        wpa.orthogTpMVecs(omega, 2)
        
        # check that the vectors are now orthonormal
        tol = 1.e-12
        for i in range(n_vecs):
            for j in range(i+1):
                omegaiTomegaj = Utils.inner(omega[i,:], omega[j,:], cls.comm)
                if rank == 0:
                    if i == j:
                        self.assertTrue(abs(omegaiTomegaj - 1.0) < tol)
                    else:
                        self.assertTrue(abs(omegaiTomegaj-0.0) < tol)
예제 #3
0
    def test_write_non_distributed_npy(self):
        cls = self.__class__
        rank = cls.comm.getRank()
        nproc = cls.comm.getSize()
        if nproc > 1:
            mvector_filename = 'out_mvector_write_test_' + str(nproc)
        else:
            mvector_filename = 'out_mvector_write_test'

        file_dir = os.path.dirname(__file__)

        filename = 'input.yaml'
        problem = Utils.createAlbanyProblem(file_dir + '/' + filename,
                                            cls.parallelEnv)

        n_cols = 4
        parameter_map = problem.getParameterMap(0)
        mvector = Tpetra.MultiVector(parameter_map, n_cols, dtype="d")

        mvector[0, :] = 1. * (rank + 1)
        mvector[1, :] = -1. * (rank + 1)
        mvector[2, :] = 3.26 * (rank + 1)
        mvector[3, :] = -3.1 * (rank + 1)

        Utils.writeMVector(file_dir + '/' + mvector_filename,
                           mvector,
                           distributedFile=False,
                           useBinary=True)
예제 #4
0
def main(parallelEnv):
    comm = Teuchos.DefaultComm.getComm()
    filename = 'input_conductivity_dist_paramT.yaml'
    problem = Utils.createAlbanyProblem(filename, parallelEnv)

    # We can get from the Albany problem the map of a distributed parameter:
    parameter_map = problem.getParameterMap(0)

    # This map can then be used to construct an RCP to a Tpetra::Multivector:
    m_directions = 4
    directions = Tpetra.MultiVector(parameter_map, m_directions, dtype="d")

    # Numpy operations, such as assignments, can then be performed on the local entries:
    directions[0, :] = 1.  # Set all entries of v_0 to   1
    directions[1, :] = -1.  # Set all entries of v_1 to  -1
    directions[2, :] = 3.  # Set all entries of v_2 to   3
    directions[3, :] = -3.  # Set all entries of v_3 to  -3

    # Now that we have an RCP to the directions, we provide it to the Albany problem:
    problem.setDirections(0, directions)

    # Finally, we can solve the problem (which includes applying the Hessian to the directions)
    # and get the Hessian-vector products:
    problem.performSolve()
    hessian = problem.getReducedHessian(0, 0)
예제 #5
0
 def _matvec(self, x):
     parameter_map = self.problem.getParameterMap(self.parameter_id)
     direction = Tpetra.MultiVector(parameter_map, 1, dtype="d")
     direction[0, :] = x
     self.problem.setDirections(self.parameter_id, direction)
     self.problem.performSolve()
     hessian = self.problem.getReducedHessian(self.response_id,
                                              self.parameter_id)
     return hessian[0, :]
예제 #6
0
    def test_all(self):
        cls = self.__class__
        rank = cls.comm.getRank()

        file_dir = os.path.dirname(__file__)

        # Create an Albany problem:
        filename = 'input_conductivity_dist_paramT.yaml'
        problem = Utils.createAlbanyProblem(file_dir+'/'+filename, cls.parallelEnv)

        n_directions = 4
        parameter_map = problem.getParameterMap(0)
        directions = Tpetra.MultiVector(parameter_map, n_directions, dtype="d")

        directions[0,:] = 1.
        directions[1,:] = -1.
        directions[2,:] = 3.
        directions[3,:] = -3.

        problem.setDirections(0, directions)

        problem.performSolve()

        response = problem.getResponse(0)
        sensitivity = problem.getSensitivity(0, 0)
        hessian = problem.getReducedHessian(0, 0)

        g_target = 3.23754626955999991e-01
        norm_target = 8.94463776843999921e-03
        h_target = np.array([0.009195356672103817, 0.009195356672103817, 0.027586070971800013, 0.027586070971800013])

        g_data = response.getData()
        norm = Utils.norm(sensitivity.getData(0), cls.comm)

        print("g_target = " + str(g_target))
        print("g_data[0] = " + str(g_data[0]))
        print("norm = " + str(norm))
        print("norm_target = " + str(norm_target))

        hessian_norms = np.zeros((n_directions,))
        for i in range(0,n_directions):
            hessian_norms[i] = Utils.norm(hessian.getData(i), cls.comm)

        tol = 1e-8
        if rank == 0:
            self.assertTrue(np.abs(g_data[0]-g_target) < tol)
            self.assertTrue(np.abs(norm-norm_target) < tol)
            for i in range(0,n_directions):
                self.assertTrue(np.abs(hessian_norms[i]-h_target[i]) < tol)
예제 #7
0
파일: steadyHeat.py 프로젝트: koomie/Albany
    def test_all(self):
        comm = Teuchos.DefaultComm.getComm()
        rank = comm.getRank()

        file_dir = os.path.dirname(__file__)

        # Create an Albany problem:
        filename = 'input_conductivity_dist_paramT.yaml'
        problem = Utils.createAlbanyProblem(file_dir + '/' + filename)

        n_directions = 4
        parameter_map = problem.getParameterMap(0)
        directions = Tpetra.MultiVector(parameter_map, n_directions, dtype="d")

        directions[0, :] = 1.
        directions[1, :] = -1.
        directions[2, :] = 3.
        directions[3, :] = -3.

        problem.setDirections(0, directions)

        problem.performSolve()

        response = problem.getResponse(0)
        sensitivity = problem.getSensitivity(0, 0)
        hessian = problem.getReducedHessian(0, 0)

        g_target = 3.23754626955999991e-01
        norm_target = 8.94463776843999921e-03
        h_target = np.array([
            4.2121719763904516e-05, -4.21216874727712e-05,
            0.00012636506241831498, -0.00012636506241831496
        ])

        g_data = response.getData()
        norm = Utils.norm(sensitivity.getData(0), comm)

        print("g_target = " + str(g_target))
        print("g_data[0] = " + str(g_data[0]))
        print("norm = " + str(norm))
        print("norm_target = " + str(norm_target))

        tol = 1e-8
        if rank == 0:
            self.assertTrue(np.abs(g_data[0] - g_target) < tol)
            self.assertTrue(np.abs(norm - norm_target) < tol)
            for i in range(0, n_directions):
                self.assertTrue(np.abs(hessian[i, 0] - h_target[i]) < tol)
예제 #8
0
    def _matvec(self, x):
        tmp1 = self.C_sqr.dot(self.R.dot(self.P.transpose().dot(x)))

        parameter_map = self.problem.getParameterMap(self.parameter_id)
        direction = Tpetra.MultiVector(parameter_map, 1, dtype="d")

        direction[0, :] = tmp1
        self.problem.setDirections(self.parameter_id, direction)
        self.problem.performSolve()
        hessian = self.problem.getReducedHessian(self.response_id,
                                                 self.parameter_id)

        tmp2 = hessian[0, :]
        tmp3 = self.P.dot(self.R.transpose().dot(
            self.C_sqr.transpose().dot(tmp2)))
        return tmp3
예제 #9
0
def main(parallelEnv):
    comm = MPI.COMM_WORLD
    nMaxProcs = comm.Get_size()
    myGlobalRank = comm.rank

    timerNames = [
        "PyAlbany: Create Albany Problem", "PyAlbany: Set directions",
        "PyAlbany: Perform Solve", "PyAlbany: Total"
    ]

    nTimers = len(timerNames)

    # number of times that the test is repeated for a fixed
    # number of MPI processes
    N = 10

    timers_sec = np.zeros((nMaxProcs, nTimers, N))
    mean_timers_sec = np.zeros((nMaxProcs, nTimers))

    speedUp = np.zeros((nMaxProcs, nTimers))

    for nProcs in range(1, nMaxProcs + 1):
        newGroup = comm.group.Incl(np.arange(0, nProcs))
        newComm = comm.Create_group(newGroup)

        if myGlobalRank < nProcs:
            parallelEnv.comm = Teuchos.MpiComm(newComm)

            for i_test in range(0, N):
                timers = Utils.createTimers(timerNames)
                timers[3].start()
                timers[0].start()

                filename = 'input_conductivity_dist_paramT.yaml'
                problem = Utils.createAlbanyProblem(filename, parallelEnv)
                timers[0].stop()

                timers[1].start()
                n_directions = 4
                parameter_map = problem.getParameterMap(0)
                directions = Tpetra.MultiVector(parameter_map,
                                                n_directions,
                                                dtype="d")

                directions[0, :] = 1.
                directions[1, :] = -1.
                directions[2, :] = 3.
                directions[3, :] = -3.

                problem.setDirections(0, directions)
                timers[1].stop()

                timers[2].start()
                problem.performSolve()
                timers[2].stop()
                timers[3].stop()

                if myGlobalRank == 0:
                    for j in range(0, nTimers):
                        timers_sec[nProcs - 1, j,
                                   i_test] = timers[j].totalElapsedTime()

    if myGlobalRank == 0:
        for i in range(0, nMaxProcs):
            for j in range(0, nTimers):
                mean_timers_sec[i, j] = np.mean(timers_sec[i, j, :])
            speedUp[i, :] = mean_timers_sec[0, :] / (mean_timers_sec[i, :])

        print('timers')
        print(mean_timers_sec)

        print('speed up')
        print(speedUp)
        if printPlot:
            fig = plt.figure(figsize=(10, 6))
            plt.plot(np.arange(1, nMaxProcs + 1), np.arange(1, nMaxProcs + 1),
                     '--')
            for j in range(0, nTimers):
                plt.plot(np.arange(1, nMaxProcs + 1),
                         speedUp[:, j],
                         'o-',
                         label=timerNames[j])
            plt.ylabel('speed up')
            plt.xlabel('number of MPI processes')
            plt.grid(True)
            plt.legend()
            plt.savefig('speedup.jpeg', dpi=800)
            plt.close()
예제 #10
0
def loadMVector(filename,
                n_cols,
                map,
                distributedFile=True,
                useBinary=True,
                readOnRankZero=True,
                dtype="d"):
    """@brief Loads distributed a multivector stored using numpy format.
    
    \param filename [in] Base name of the file(s) to load.
    \param n_cols [in] Number of columns of the multivector.
    \param map [in] Tpetra map of the multivector which has to be loaded.
    \param distributedFile (default: True) [in] Bool which specifies whether each MPI process reads a different
    file (if distributedFile==True, MPI process i reads filename+"_"+str(i)) or if all the entries are stored inside
    a unique file.
    \param useBinary (default: True) [in] Bool which specifies if the function reads a binary or a text file.
    \param readOnRankZero (default: True) [in] Bool which specifies if the file is read by the rank 0 and scattered or if
    it is read by all the MPI processes.
    \param dtype (default: "d") [in] Data type of the entries of the multivector.
    """
    rank = map.getComm().getRank()
    nproc = map.getComm().getSize()
    mvector = Tpetra.MultiVector(map, n_cols, dtype=dtype)
    if nproc == 1:
        if useBinary:
            mVectorNP = np.load(filename + '.npy')
        else:
            mVectorNP = np.loadtxt(filename + '.txt')

        if (mVectorNP.ndim == 1 and n_cols == 1):
            mvector[0, :] = mVectorNP
        else:
            for i in range(0, n_cols):
                mvector[i, :] = mVectorNP[i, :]
    elif distributedFile:
        if useBinary:
            mVectorNP = np.load(filename + '_' + str(rank) + '.npy')
        else:
            mVectorNP = np.loadtxt(filename + '_' + str(rank) + '.txt')
        for i in range(0, n_cols):
            mvector[i, :] = mVectorNP[i, :]
    else:
        if readOnRankZero:
            map0 = wpa.getRankZeroMap(map)
            mvector0 = Tpetra.MultiVector(map0, n_cols, dtype=dtype)
            if rank == 0:
                if useBinary:
                    mVectorNP = np.load(filename + '.npy')
                else:
                    mVectorNP = np.loadtxt(filename + '.txt')

                if (mVectorNP.ndim == 1 and n_cols == 1):
                    mvector0[0, :] = mVectorNP
                else:
                    for i in range(0, n_cols):
                        mvector0[i, :] = mVectorNP[i, :]
            mvector = wpa.scatterMVector(mvector0, map)
        else:
            if useBinary:
                mVectorNP = np.load(filename + '.npy')
            else:
                mVectorNP = np.loadtxt(filename + '.txt')
            for lid in range(0, map.getNodeNumElements()):
                gid = map.getGlobalElement(lid)
                mvector[:, lid] = mVectorNP[:, gid]
    return mvector