예제 #1
0
 def compute_UB_vector(self, u):
     Q = self.SCM_approximation.truth_problem.Q["a"]
     inner_product = self.SCM_approximation.truth_problem.inner_product[0]
     UB_vector = OnlineVector(Q)
     norm_S_squared = transpose(u)*inner_product*u
     for q in range(Q):
         A_q = self.SCM_approximation.truth_problem.operator["a"][q]
         UB_vector[q] = (transpose(u)*A_q*u)/norm_S_squared
     return UB_vector
 def compute_upper_bound_vector(self, u):
     Q = self.SCM_approximation.truth_problem.Q[
         "stability_factor_left_hand_matrix"]
     A = self.SCM_approximation.truth_problem.operator[
         "stability_factor_left_hand_matrix"]
     B = self.SCM_approximation.truth_problem.operator[
         "stability_factor_right_hand_matrix"]
     assert len(B) == 1
     normalization = transpose(u) * B[0] * u
     upper_bound_vector = OnlineVector(Q)
     for q in range(Q):
         upper_bound_vector[q] = (transpose(u) * A[q] * u) / normalization
     return upper_bound_vector
예제 #3
0
def evaluate_sparse_vector_at_dofs(sparse_vector, dofs_list):
    vec = to_petsc4py(sparse_vector)
    row_start, row_end = vec.getOwnershipRange()
    out_size = len(dofs_list)
    out = OnlineVector(out_size)
    mpi_comm = vec.comm.tompi4py()
    for (index, dofs) in enumerate(dofs_list):
        assert len(dofs) == 1
        i = dofs[0]
        out_index = None
        vec_i_processor = -1
        if i >= row_start and i < row_end:
            out_index = vec.getValue(i)
            vec_i_processor = mpi_comm.rank
        vec_i_processor = mpi_comm.allreduce(vec_i_processor, op=MAX)
        assert vec_i_processor >= 0
        out[index] = mpi_comm.bcast(out_index, root=vec_i_processor)
    return out
예제 #4
0
def evaluate_and_vectorize_sparse_matrix_at_dofs(sparse_matrix, dofs_list):
    mat = to_petsc4py(sparse_matrix)
    row_start, row_end = mat.getOwnershipRange()
    out_size = len(dofs_list)
    out = OnlineVector(out_size)
    mpi_comm = mat.comm.tompi4py()
    for (index, dofs) in enumerate(dofs_list):
        assert len(dofs) == 2
        i = dofs[0]
        out_index = None
        mat_ij_processor = -1
        if i >= row_start and i < row_end:
            j = dofs[1]
            out_index = mat.getValue(i, j)
            mat_ij_processor = mpi_comm.rank
        mat_ij_processor = mpi_comm.allreduce(mat_ij_processor, op=MAX)
        assert mat_ij_processor >= 0
        out[index] = mpi_comm.bcast(out_index, root=mat_ij_processor)
    return out
예제 #5
0
 def _load_item_vector_dimension(self, full_directory):
     N = ItemVectorDimensionIO.load_file(full_directory,
                                         "item_vector_dimension")
     return OnlineVector(N)
예제 #6
0
class UpperBoundsList(list):
    def __init__(self):
        self._list = list()

    def append(self, element):
        self._list.append(element)

    @overload(list_of(OnlineVector.Type()))
    def extend(self, other_list):
        self._list.extend(other_list)

    @overload(lambda cls: cls)
    def extend(self, other_list):
        self._list.extend(other_list._list)

    def save(self, directory, filename):
        # Get full directory name
        full_directory = Folders.Folder(os.path.join(str(directory), filename))
        full_directory.create()
        # Save list length
        self._save_len(full_directory)
        # Save list item vector dimension
        self._save_item_vector_dimension(full_directory)
        # Save list
        self._save_list(full_directory)

    def _save_len(self, full_directory):
        LenIO.save_file(len(self._list), full_directory, "len")

    def _save_item_vector_dimension(self, full_directory):
        ItemVectorDimensionIO.save_file(self._list[0].N, full_directory,
                                        "item_vector_dimension")

    def _save_list(self, full_directory):
        for (index, item) in enumerate(self._list):
            online_export(item, full_directory, "item_" + str(index))

    def load(self, directory, filename):
        if len(self._list) > 0:  # avoid loading multiple times
            return True
        else:
            # Get full directory name
            full_directory = Folders.Folder(
                os.path.join(str(directory), filename))
            # Load list length
            len_ = self._load_len(full_directory)
            # Load list item vector dimension
            reference_vector = self._load_item_vector_dimension(full_directory)
            # Load list
            self._load_list(len_, reference_vector, full_directory)
            # Return
            return True

    def _load_len(self, full_directory):
        assert LenIO.exists_file(full_directory, "len")
        return LenIO.load_file(full_directory, "len")

    def _load_item_vector_dimension(self, full_directory):
        N = ItemVectorDimensionIO.load_file(full_directory,
                                            "item_vector_dimension")
        return OnlineVector(N)

    def _load_list(self, len_, reference_vector, full_directory):
        for index in range(len_):
            item = online_copy(reference_vector)
            vector_loaded = online_import_(item, full_directory,
                                           "item_" + str(index))
            assert vector_loaded
            self._list.append(item)

    def __getitem__(self, key):
        return self._list[key]

    def __setitem__(self, key, item):
        self._list[key] = item

    def __iter__(self):
        return iter(self._list)

    def __len__(self):
        return len(self._list)