Skip to content

Commit

Permalink
merge: Merge branch 'refactor/pybind_enum' into 'main'
Browse files Browse the repository at this point in the history
Use enum in arguments type and exports enum to python

See merge request numerics/solver/comma!47
  • Loading branch information
Riccardo Milani committed Apr 9, 2024
2 parents e0b238b + 576ec6e commit 710c255
Show file tree
Hide file tree
Showing 9 changed files with 210 additions and 126 deletions.
60 changes: 35 additions & 25 deletions examples/scripts/ex_aniso_lines.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@
anisotropic lines using the related features of `dualGPy`.
"""
# import dualGPy.Utils as dGut
import CoMMA
import meshio
import meshio as mio
import numpy as np
from CoMMA import agglomerate_one_level
from dualGPy.Graph import Graph2D
from dualGPy.Mesh import Mesh2D

Expand All @@ -28,13 +27,16 @@
prepare_meshio_celldata,
)

neigh_type_types = ["Extended", "Pure front advancing"]
neigh_type_types = {
CoMMA.Neighbourhood.EXTENDED: "Extended",
CoMMA.Neighbourhood.PURE_FRONT: "Pure front advancing",
}

seed_ordering_types = {
0: "Boundary priority",
1: "Neighbourhood priority",
10: "Boundary priority with point initialization",
11: "Neighbourhood priority with point initialization",
CoMMA.SeedsPool.BOUNDARY: "Boundary priority",
CoMMA.SeedsPool.NEIGHBOURHOOD: "Neighbourhood priority",
CoMMA.SeedsPool.BOUNDARY_POINT_INIT: "Boundary priority with point initialization", # noqa: E501
CoMMA.SeedsPool.NEIGHBOURHOOD_POINT_INIT: "Neighbourhood priority with point initialization", # noqa: E501
}

# USER PARAMETERS
Expand All @@ -48,10 +50,18 @@
correction = False
threshold_anisotropy = 1.5
odd_line_length = True
neigh_type = 0 # 0 = Extended (standard), 1 = Pure front advancing
seed_order = 0 # 0 = Boundary priority, 1 = Neighbourhood priority,
# 10 = Boundary priority with point initialization
# 11 = Neighbourhood priority with point initialization
# Seeds pool ordering choices:
# - SeedsPool.BOUNDARY: Boundary priority, 0
# - SeedsPool.NEIGHBOURHOOD: Neighbourhood priority, 1
# - SeedsPool.BOUNDARY_POINT_INIT: Boundary priority with point initialization,
# 10
# - SeedsPool.NEIGHBOURHOOD_POINT_INIT: Neighbourhood priority with point
# initialization, 11
seed_order = CoMMA.SeedsPool.BOUNDARY
# Neighbourhood type choices:
# - Neighbourhood.EXTENDED: Extended, 0, standard
# - Neighbourhood.PURE_FRONT: Pure front advancing, 1, experimental
neigh_type = CoMMA.Neighbourhood.EXTENDED
# Threshold cardinality for a coarse cell to be considered singular
sing_card = 1
# Max cells in an anisotropic line
Expand Down Expand Up @@ -98,9 +108,9 @@
print()

# CoMMATypes-like
CoMMAIndex = np.uint # Unsigned long
CoMMAInt = int
CoMMAWeight = np.double
CoMMAIndex = np.uint # We could have used CoMMA.IndexType = Unsigned long
CoMMAInt = CoMMA.IntType # = int
CoMMAWeight = CoMMA.WeightType # = double

print("Creating mesh and dual graph...", flush=True, end="")
ref_pts = np.zeros((7, 2), dtype=float)
Expand All @@ -126,7 +136,7 @@
for i, nd in enumerate(btlxnd):
cells[i, :] += nd
# print(cells)
mio_m = mio.Mesh(points=points, cells={"quad": cells})
mio_m = meshio.Mesh(points=points, cells={"quad": cells})
# mio_m.write('aniso_3cell.msh', file_format = "ansys")
m = Mesh2D(mio_m)
m.get_boundary_faces()
Expand All @@ -146,11 +156,11 @@
weights = np.arange(start=nb_fc - 1, stop=0, step=-1, dtype=CoMMAWeight)
fc_to_cc = np.empty(nb_fc, dtype=CoMMAIndex)
anisoCompliantCells = np.arange(nb_fc, dtype=CoMMAIndex)
agglomerationLines_Idx = np.array([0], dtype=CoMMAIndex)
agglomerationLines = np.array([0], dtype=CoMMAIndex)
aniso_lines_idx = np.array([0], dtype=CoMMAIndex)
aniso_lines = np.array([0], dtype=CoMMAIndex)

print("CoMMA call...", flush=True, end="")
fc_to_cc_res, alines_Idx, alines = agglomerate_one_level(
fc_to_cc_res, alines_idx, alines = CoMMA.agglomerate_one_level(
adjMatrix_row_ptr,
adjMatrix_col_ind,
adjMatrix_areaValues,
Expand All @@ -165,8 +175,8 @@
threshold_anisotropy,
seed_order,
fc_to_cc,
agglomerationLines_Idx,
agglomerationLines,
aniso_lines_idx,
aniso_lines,
correction,
dimension,
goalCard,
Expand All @@ -187,24 +197,24 @@
shuffle=shuffle_coarse,
)
out_lines = prepare_meshio_celldata(
assign_anisotropic_line_data_to_cells(alines_Idx, alines, fc_to_cc),
assign_anisotropic_line_data_to_cells(alines_idx, alines, fc_to_cc),
m.mesh.cells,
)

f2c = np.asarray(fc_to_cc_res, dtype=int)
# Building a dictionary 'aniso line ID' : [fine cells in line]
# One-liner:
dic = {
i: np.flatnonzero(np.isin(f2c, alines[alines_Idx[i] : alines_Idx[i + 1]]))
for i in range(len(alines_Idx) - 1)
i: np.flatnonzero(np.isin(f2c, alines[alines_idx[i] : alines_idx[i + 1]]))
for i in range(len(alines_idx) - 1)
}
# Second option, better explained
# dic = {}
# # For every line...
# for i in range(len(alines_Idx)-1):
# for i in range(len(alines_idx)-1):
# mask = np.full(len(fc_to_cc_res), False)
# # For every coarse cell in the line...
# for cc in alines[alines_Idx[i]:alines_Idx[i+1]]:
# for cc in alines[alines_idx[i]:alines_idx[i+1]]:
# # Take all the fine cells that are in the coarse cell
# mask |= f2c == cc
# dic[i] = np.flatnonzero(mask)
Expand Down
51 changes: 31 additions & 20 deletions examples/scripts/ex_default_dualGPy.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,24 @@
export the result.
"""
# import dualGPy.Utils as dGut
import CoMMA
import meshio
import numpy as np
from CoMMA import agglomerate_one_level
from dualGPy.Graph import Graph2D
from dualGPy.Mesh import Mesh2D, Mesh3D

from comma_tools import prepare_meshio_agglomeration_data

neigh_type_types = ["Extended", "Pure front advancing"]
neigh_type_types = {
CoMMA.Neighbourhood.EXTENDED: "Extended",
CoMMA.Neighbourhood.PURE_FRONT: "Pure front advancing",
}

seed_ordering_types = {
0: "Boundary priority",
1: "Neighbourhood priority",
10: "Boundary priority with point initialization",
11: "Neighbourhood priority with point initialization",
CoMMA.SeedsPool.BOUNDARY: "Boundary priority",
CoMMA.SeedsPool.NEIGHBOURHOOD: "Neighbourhood priority",
CoMMA.SeedsPool.BOUNDARY_POINT_INIT: "Boundary priority with point initialization", # noqa: E501
CoMMA.SeedsPool.NEIGHBOURHOOD_POINT_INIT: "Neighbourhood priority with point initialization", # noqa: E501
}

# USER PARAMETERS
Expand All @@ -49,10 +52,18 @@
correction = False
threshold_anisotropy = 4.0
odd_line_length = True
neigh_type = 0 # 0 = Extended (standard), 1 = Pure front advancing
seed_order = 0 # 0 = Boundary priority, 1 = Neighbourhood priority,
# 10 = Boundary priority with point initialization
# 11 = Neighbourhood priority with point initialization
# Seeds pool ordering choices:
# - SeedsPool.BOUNDARY: Boundary priority, 0
# - SeedsPool.NEIGHBOURHOOD: Neighbourhood priority, 1
# - SeedsPool.BOUNDARY_POINT_INIT: Boundary priority with point initialization,
# 10
# - SeedsPool.NEIGHBOURHOOD_POINT_INIT: Neighbourhood priority with point
# initialization, 11
seed_order = CoMMA.SeedsPool.BOUNDARY
# Neighbourhood type choices:
# - Neighbourhood.EXTENDED: Extended, 0, standard
# - Neighbourhood.PURE_FRONT: Pure front advancing, 1, experimental
neigh_type = CoMMA.Neighbourhood.EXTENDED
# Threshold cardinality for a coarse cell to be considered singular
sing_card = 1
# Max cells in an anisotropic line
Expand Down Expand Up @@ -108,9 +119,9 @@
)

# CoMMATypes-like
CoMMAIndex = np.uint # Unsigned long
CoMMAInt = int
CoMMAWeight = np.double
CoMMAIndex = np.uint # We could have used CoMMA.IndexType = Unsigned long
CoMMAInt = CoMMA.IntType # = int
CoMMAWeight = CoMMA.WeightType # = double

print("Creating mesh and dual graph...", flush=True, end="")
mesh = Mesh2D(n, anisotropic) if dimension == 2 else Mesh3D(n, anisotropic)
Expand All @@ -131,15 +142,15 @@
n_bnd_faces = np.array(mesh.boundary_cells, dtype=CoMMAInt)
fc_to_cc = np.empty(nb_fc, dtype=CoMMAIndex)
anisoCompliantCells = np.arange(nb_fc, dtype=CoMMAIndex)
agglomerationLines_Idx = np.array([0], dtype=CoMMAIndex)
agglomerationLines = np.array([0], dtype=CoMMAIndex)
aniso_lines_idx = np.array([0], dtype=CoMMAIndex)
aniso_lines = np.array([0], dtype=CoMMAIndex)

print("CoMMA call...", flush=True, end="")
(
fc_to_cc_res,
agglomerationLines_Idx_res_iso,
agglomerationLines_res_iso,
) = agglomerate_one_level(
aniso_lines_idx_res_iso,
aniso_lines_res_iso,
) = CoMMA.agglomerate_one_level(
adjMatrix_row_ptr,
adjMatrix_col_ind,
adjMatrix_areaValues,
Expand All @@ -154,8 +165,8 @@
threshold_anisotropy,
seed_order,
fc_to_cc,
agglomerationLines_Idx,
agglomerationLines,
aniso_lines_idx,
aniso_lines,
correction,
dimension,
goalCard,
Expand Down
59 changes: 34 additions & 25 deletions examples/scripts/ex_rae.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@
import os
from math import floor, log # noqa: F401

import CoMMA
import meshio

# import dualGPy.Utils as dGut
import numpy as np
from CoMMA import agglomerate_one_level
from dualGPy.Graph import Graph2D
from dualGPy.Mesh import Mesh2D

Expand All @@ -47,13 +45,16 @@ def limit_line_length(idxs, cells, max_cells_in_line):
return ret_idxs, ret_cells


neigh_type_types = ["Extended", "Pure front advancing"]
neigh_type_types = {
CoMMA.Neighbourhood.EXTENDED: "Extended",
CoMMA.Neighbourhood.PURE_FRONT: "Pure front advancing",
}

seed_ordering_types = {
0: "Boundary priority",
1: "Neighbourhood priority",
10: "Boundary priority with point initialization",
11: "Neighbourhood priority with point initialization",
CoMMA.SeedsPool.BOUNDARY: "Boundary priority",
CoMMA.SeedsPool.NEIGHBOURHOOD: "Neighbourhood priority",
CoMMA.SeedsPool.BOUNDARY_POINT_INIT: "Boundary priority with point initialization", # noqa: E501
CoMMA.SeedsPool.NEIGHBOURHOOD_POINT_INIT: "Neighbourhood priority with point initialization", # noqa: E501
}

# USER PARAMETERS
Expand All @@ -69,10 +70,18 @@ def limit_line_length(idxs, cells, max_cells_in_line):
correction = True
threshold_anisotropy = -4.0
odd_line_length = True
neigh_type = 0 # 0 = Extended (standard), 1 = Pure front advancing
seed_order = 0 # 0 = Boundary priority, 1 = Neighbourhood priority,
# 10 = Boundary priority with point initialization
# 11 = Neighbourhood priority with point initialization
# Seeds pool ordering choices:
# - SeedsPool.BOUNDARY: Boundary priority, 0
# - SeedsPool.NEIGHBOURHOOD: Neighbourhood priority, 1
# - SeedsPool.BOUNDARY_POINT_INIT: Boundary priority with point initialization,
# 10
# - SeedsPool.NEIGHBOURHOOD_POINT_INIT: Neighbourhood priority with point
# initialization, 11
seed_order = CoMMA.SeedsPool.BOUNDARY
# Neighbourhood type choices:
# - Neighbourhood.EXTENDED: Extended, 0, standard
# - Neighbourhood.PURE_FRONT: Pure front advancing, 1, experimental
neigh_type = CoMMA.Neighbourhood.EXTENDED
# Threshold cardinality for a coarse cell to be considered singular
sing_card = 1
# Max cells in an anisotropic line
Expand Down Expand Up @@ -126,9 +135,9 @@ def limit_line_length(idxs, cells, max_cells_in_line):
print()

# CoMMATypes-like
CoMMAIndex = np.uint # Unsigned long
CoMMAInt = int
CoMMAWeight = np.double
CoMMAIndex = np.uint # We could have used CoMMA.IndexType = Unsigned long
CoMMAInt = CoMMA.IntType # = int
CoMMAWeight = CoMMA.WeightType # = double

outname = (
"raebis_"
Expand Down Expand Up @@ -175,8 +184,8 @@ def limit_line_length(idxs, cells, max_cells_in_line):

fc_to_cc = np.empty(nb_fc, dtype=CoMMAIndex)

agglomerationLines_Idx = np.array([0], dtype=CoMMAIndex)
agglomerationLines = np.array([0], dtype=CoMMAIndex)
aniso_lines_idx = np.array([0], dtype=CoMMAIndex)
aniso_lines = np.array([0], dtype=CoMMAIndex)
print("OK")

# Storage for the all the agglomeration levels
Expand Down Expand Up @@ -227,20 +236,20 @@ def limit_line_length(idxs, cells, max_cells_in_line):
# max_cells = (
# n_cells_in_aniso_line * 2**(agglomeration_levels - 1 - level)
# )
line_length = agglomerationLines_Idx[1] - agglomerationLines_Idx[0]
line_length = aniso_lines_idx[1] - aniso_lines_idx[0]
# Reduce by 2 so that the excluded cells will be agglomerated with
# their neighbours (also excluded) and form a nice coarse with
# cardinality 4
max_cells = max(2, line_length - 2)
# Closest smallest power of 2, since the agglomeration is always 2-by-2
# max_cells = 2**int(floor(log(line_length, 2)))
agglomerationLines_Idx, agglomerationLines = limit_line_length(
agglomerationLines_Idx, agglomerationLines, max_cells
aniso_lines_idx, aniso_lines = limit_line_length(
aniso_lines_idx, aniso_lines, max_cells
)
print("OK")

print(" - CoMMA call...", flush=True, end="")
fc_to_cc, agglomerationLines_Idx, agglomerationLines = agglomerate_one_level(
fc_to_cc, aniso_lines_idx, aniso_lines = CoMMA.agglomerate_one_level(
adjMatrix_row_ptr,
adjMatrix_col_ind,
adjMatrix_areaValues,
Expand All @@ -255,8 +264,8 @@ def limit_line_length(idxs, cells, max_cells_in_line):
threshold_anisotropy,
seed_order,
fc_to_cc,
agglomerationLines_Idx,
agglomerationLines,
aniso_lines_idx,
aniso_lines,
correction,
dimension,
goalCard,
Expand All @@ -283,8 +292,8 @@ def limit_line_length(idxs, cells, max_cells_in_line):
if level == 0:
out_data["anisotropic_lines"] = prepare_meshio_celldata(
assign_anisotropic_line_data_to_cells(
agglomerationLines_Idx,
agglomerationLines,
aniso_lines_idx,
aniso_lines,
fc_to_cc,
modulo_renumbering=renumber_coarse,
shuffle=shuffle_coarse,
Expand Down
Loading

0 comments on commit 710c255

Please sign in to comment.