Skip to content

Commit

Permalink
update htool 0.9.0
Browse files Browse the repository at this point in the history
  • Loading branch information
PierreMarchand20 committed Aug 7, 2024
1 parent ca3f58b commit 2207e6c
Show file tree
Hide file tree
Showing 37 changed files with 455 additions and 1,050 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ if("${BLA_VENDOR}" STREQUAL "Intel10_32"
target_compile_definitions(htool PRIVATE "-DHPDDM_MKL -DHTOOL_MKL")
endif()

target_compile_definitions(Htool PRIVATE "-DPYTHON_INTERFACE" "-DWITH_HPDDM")
target_compile_definitions(Htool PRIVATE "-DHTOOL_WITH_PYTHON_INTERFACE" "-DHTOOL_WITH_HPDDM")

if(CODE_COVERAGE AND (CMAKE_C_COMPILER_ID MATCHES "GNU" OR CMAKE_CXX_COMPILER_ID MATCHES "GNU"))
target_compile_options(Htool PRIVATE -fprofile-arcs -ftest-coverage)
Expand Down
97 changes: 0 additions & 97 deletions example/ddm_solver.py

This file was deleted.

61 changes: 4 additions & 57 deletions example/define_custom_generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,66 +2,13 @@
import numpy as np


class CustomGenerator(Htool.VirtualGenerator):
def __init__(self, target_cluster, target_points, source_cluster, source_points):
class CustomGenerator(Htool.VirtualGeneratorInUserNumbering):
def __init__(self, target_points, source_points):
super().__init__()
self.target_points = target_points
self.target_permutation = target_cluster.get_permutation()
self.source_points = source_points
self.source_permutation = source_cluster.get_permutation()
self.nb_rows = len(self.target_permutation)
self.nb_cols = len(self.source_permutation)

def get_coef(self, i, j):
return 1.0 / (
1e-1
+ np.linalg.norm(
self.target_points[:, self.target_permutation[i]]
- self.source_points[:, self.source_permutation[j]]
)
)

def build_submatrix(self, row_offset, col_offset, mat):
for j in range(0, mat.shape[0]):
for k in range(0, mat.shape[1]):
mat[j, k] = 1.0 / (
1.0e-1
+ np.linalg.norm(
self.target_points[:, self.target_permutation[j + row_offset]]
- self.source_points[:, self.source_permutation[k + col_offset]]
)
)

def mat_vec(self, x):
y = np.zeros(self.nb_rows)
for i in range(0, self.nb_rows):
for j in range(0, self.nb_cols):
y[self.target_permutation[i]] += (
self.get_coef(i, j) * x[self.source_permutation[j]]
)
return y

def mat_mat(self, X):
Y = np.zeros((self.nb_rows, X.shape[1]))

for i in range(0, self.nb_rows):
for j in range(0, X.shape[1]):
for k in range(0, self.nb_cols):
Y[self.target_permutation[i], j] += (
self.get_coef(i, k) * X[self.source_permutation[k], j]
)
return Y


class CustomGeneratorWithPermutation(Htool.VirtualGeneratorWithPermutation):
def __init__(
self, target_permutation, target_points, source_permutation, source_points
):
super().__init__(target_permutation, source_permutation)
self.target_points = target_points
self.source_points = source_points
self.nb_rows = len(target_permutation)
self.nb_cols = len(source_permutation)
self.nb_rows = target_points.shape[1]
self.nb_cols = source_points.shape[1]

def get_coef(self, i, j):
return 1.0 / (
Expand Down
12 changes: 10 additions & 2 deletions example/define_custom_local_operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
class CustomLocalOperator(Htool.LocalOperator):
def __init__(
self,
generator: Htool.VirtualGenerator,
generator: Htool.VirtualGeneratorInUserNumbering,
target_cluster: Htool.Cluster,
source_cluster: Htool.Cluster,
symmetry: str = "N",
Expand All @@ -23,7 +23,15 @@ def __init__(
)
self.data = np.zeros((target_cluster.get_size(), source_cluster.get_size()))
generator.build_submatrix(
target_cluster.get_offset(), source_cluster.get_offset(), self.data
target_cluster.get_permutation()[
target_cluster.get_offset() : target_cluster.get_offset()
+ target_cluster.get_size()
],
source_cluster.get_permutation()[
source_cluster.get_offset() : source_cluster.get_offset()
+ source_cluster.get_size()
],
self.data,
)

def add_vector_product(
Expand Down
13 changes: 7 additions & 6 deletions example/define_custom_low_rank_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@


class CustomSVD(Htool.VirtualLowRankGenerator):
def build_low_rank_approximation(
self, generator, target_size, source_size, target_offset, source_offset, epsilon
):
submat = np.zeros((target_size, source_size), order="F")
generator.build_submatrix(target_offset, source_offset, submat)
def __init__(self, generator: Htool.VirtualGeneratorInUserNumbering):
super().__init__(generator)

def build_low_rank_approximation(self, rows, cols, epsilon):
submat = np.zeros((len(rows), len(cols)), order="F")
self.build_submatrix(rows, cols, submat)
u, s, vh = np.linalg.svd(submat, full_matrices=False)

norm = np.linalg.norm(submat)
Expand All @@ -18,7 +19,7 @@ def build_low_rank_approximation(
while count > 0 and math.sqrt(svd_norm) / norm < epsilon:
svd_norm += s[count] ** 2
count -= 1
count = min(count + 1, min(target_size, source_size))
count = min(count + 1, min(len(rows), len(cols)))
self.set_U(u[:, 0:count] * s[0:count])
self.set_V(vh[0:count, :])
self.set_rank(count)
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import copy

import Htool
import matplotlib.pyplot as plt
import mpi4py
Expand Down Expand Up @@ -26,7 +28,7 @@
)

# Build generator
generator = CustomGenerator(cluster, points, cluster, points)
generator = CustomGenerator(points, points)

# Build distributed operator
default_approximation = Htool.DefaultApproximationBuilder(
Expand All @@ -41,9 +43,10 @@
)

# Solver with block Jacobi preconditionner
default_solver_builder = Htool.DefaultSolverBuilder(
default_approximation.distributed_operator,
default_approximation.block_diagonal_hmatrix,
block_diagonal_hmatrix = copy.deepcopy(default_approximation.block_diagonal_hmatrix)
test = default_approximation.block_diagonal_hmatrix
default_solver_builder = Htool.DDMSolverBuilder(
default_approximation.distributed_operator, block_diagonal_hmatrix
)
solver = default_solver_builder.solver

Expand Down Expand Up @@ -79,10 +82,6 @@
print(solver_information)

fig = plt.figure()
ax1 = None
ax2 = None
ax3 = None
ax4 = None
if dimension == 2:
ax1 = fig.add_subplot(2, 2, 1)
ax2 = fig.add_subplot(2, 2, 2)
Expand Down
Loading

0 comments on commit 2207e6c

Please sign in to comment.