From 576ec6e56cbd08dc3abaa7039feb2fcae2ff6b90 Mon Sep 17 00:00:00 2001 From: Riccardo Milani Date: Tue, 9 Apr 2024 09:27:46 +0200 Subject: [PATCH] refactor: Use enum in arguments type and exports enum to python --- examples/scripts/ex_aniso_lines.py | 60 ++++++++++++--------- examples/scripts/ex_default_dualGPy.py | 51 +++++++++++------- examples/scripts/ex_rae.py | 59 +++++++++++--------- examples/scripts/ex_read_mesh.py | 51 +++++++++++------- examples/scripts/ex_several_agglo_levels.py | 50 ++++++++++------- include/CoMMA/Agglomerator.h | 6 +-- include/CoMMA/Args.h | 8 +-- include/CoMMA/CoMMA.h | 4 +- src/CoMMA.cpp | 47 +++++++++++++--- 9 files changed, 210 insertions(+), 126 deletions(-) diff --git a/examples/scripts/ex_aniso_lines.py b/examples/scripts/ex_aniso_lines.py index 1994016..041a849 100644 --- a/examples/scripts/ex_aniso_lines.py +++ b/examples/scripts/ex_aniso_lines.py @@ -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 @@ -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 @@ -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 @@ -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) @@ -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() @@ -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, @@ -165,8 +175,8 @@ threshold_anisotropy, seed_order, fc_to_cc, - agglomerationLines_Idx, - agglomerationLines, + aniso_lines_idx, + aniso_lines, correction, dimension, goalCard, @@ -187,7 +197,7 @@ 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, ) @@ -195,16 +205,16 @@ # 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) diff --git a/examples/scripts/ex_default_dualGPy.py b/examples/scripts/ex_default_dualGPy.py index 43ed144..a019089 100644 --- a/examples/scripts/ex_default_dualGPy.py +++ b/examples/scripts/ex_default_dualGPy.py @@ -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 @@ -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 @@ -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) @@ -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, @@ -154,8 +165,8 @@ threshold_anisotropy, seed_order, fc_to_cc, - agglomerationLines_Idx, - agglomerationLines, + aniso_lines_idx, + aniso_lines, correction, dimension, goalCard, diff --git a/examples/scripts/ex_rae.py b/examples/scripts/ex_rae.py index 0700797..c5860eb 100644 --- a/examples/scripts/ex_rae.py +++ b/examples/scripts/ex_rae.py @@ -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 @@ -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 @@ -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 @@ -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_" @@ -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 @@ -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, @@ -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, @@ -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, diff --git a/examples/scripts/ex_read_mesh.py b/examples/scripts/ex_read_mesh.py index 42df337..2c06dfe 100644 --- a/examples/scripts/ex_read_mesh.py +++ b/examples/scripts/ex_read_mesh.py @@ -18,21 +18,24 @@ import os # 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 @@ -54,10 +57,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 @@ -108,9 +119,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("Reading mesh and creating dual graph...", flush=True, end="") if not os.path.exists(input_mesh): @@ -140,15 +151,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, @@ -163,8 +174,8 @@ threshold_anisotropy, seed_order, fc_to_cc, - agglomerationLines_Idx, - agglomerationLines, + aniso_lines_idx, + aniso_lines, correction, dimension, goalCard, diff --git a/examples/scripts/ex_several_agglo_levels.py b/examples/scripts/ex_several_agglo_levels.py index 8e5a868..868feb8 100644 --- a/examples/scripts/ex_several_agglo_levels.py +++ b/examples/scripts/ex_several_agglo_levels.py @@ -13,23 +13,25 @@ Create a mesh with the default constructor of `dualGPy`, apply several levels of agglomeration with `CoMMA` and export the result. """ -import meshio - # 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 build_coarse_graph, 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 @@ -49,10 +51,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 @@ -102,9 +112,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="") outname = ( @@ -135,8 +145,8 @@ 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) # Storage for the all the agglomeration levels out_data = {} @@ -174,7 +184,7 @@ 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, @@ -189,8 +199,8 @@ threshold_anisotropy, seed_order, fc_to_cc, - agglomerationLines_Idx, - agglomerationLines, + aniso_lines_idx, + aniso_lines, correction, dimension, goalCard, diff --git a/include/CoMMA/Agglomerator.h b/include/CoMMA/Agglomerator.h index 6f5afbe..56442d8 100644 --- a/include/CoMMA/Agglomerator.h +++ b/include/CoMMA/Agglomerator.h @@ -791,7 +791,7 @@ class Agglomerator_Isotropic : cc_graph, std::shared_ptr> seeds_pool, - CoMMAIntType neighbourhood_type, + CoMMANeighbourhoodT neighbourhood_type, CoMMAIntType fc_iter, CoMMAIntType dimension = 3 ) : @@ -1062,7 +1062,7 @@ class Agglomerator_Biconnected : cc_graph, std::shared_ptr> seeds_pool, - CoMMAIntType neighbourhood_type, + CoMMANeighbourhoodT neighbourhood_type, CoMMAIntType fc_iter, CoMMAIntType dimension = 3 ) : @@ -1501,7 +1501,7 @@ class Agglomerator_Iterative : cc_graph, std::shared_ptr> seeds_pool, - CoMMAIntType neighbourhood_type, + CoMMANeighbourhoodT neighbourhood_type, CoMMAIntType fc_iter, CoMMAIntType dimension = 3 ) : diff --git a/include/CoMMA/Args.h b/include/CoMMA/Args.h index f2a15bb..299cbaa 100644 --- a/include/CoMMA/Args.h +++ b/include/CoMMA/Args.h @@ -111,7 +111,7 @@ class AgglomerationArgs { /** @brief Type of ordering for the seeds of the coarse cells (see \ref * CoMMASeedsPoolT) */ - CoMMAIntType seed_ordering_type; + CoMMASeedsPoolT seed_ordering_type{}; /** @brief Cardinality below which a coarse is considered as singular, hence, * compliant for correction */ CoMMAIntType singular_card_thresh; @@ -122,7 +122,7 @@ class AgglomerationArgs { /** @brief Type of neighbourhood to use when growing a coarse cell. See \ref * CoMMANeighbourhoodT for more details. */ - CoMMAIntType neighbourhood_type; + CoMMANeighbourhoodT neighbourhood_type{}; /** @brief Default constructor */ AgglomerationArgs() = default; @@ -150,10 +150,10 @@ class AgglomerationArgs { CoMMAIntType min_card, CoMMAIntType max_card, bool correction, - CoMMAIntType seed_ordering_type, + CoMMASeedsPoolT seed_ordering_type, CoMMAIntType singular_card_thresh = 1, CoMMAIntType fc_choice_iter = 1, - CoMMAIntType neighbourhood_type = CoMMANeighbourhoodT::EXTENDED + CoMMANeighbourhoodT neighbourhood_type = CoMMANeighbourhoodT::EXTENDED ) : goal_card(goal_card), min_card(min_card), diff --git a/include/CoMMA/CoMMA.h b/include/CoMMA/CoMMA.h index a850c75..5d331e6 100644 --- a/include/CoMMA/CoMMA.h +++ b/include/CoMMA/CoMMA.h @@ -148,7 +148,7 @@ void agglomerate_one_level( CoMMAWeightType threshold_anisotropy, // Seed ordering - const CoMMAIntType seed_ordering_type, + CoMMASeedsPoolT seed_ordering_type, // Outputs std::vector &fc_to_cc, // Out @@ -164,7 +164,7 @@ void agglomerate_one_level( CoMMAIntType singular_card_thresh = 1, std::optional max_cells_in_line = std::nullopt, CoMMAIntType fc_choice_iter = 1, - const CoMMAIntType neighbourhood_type = CoMMANeighbourhoodT::EXTENDED + CoMMANeighbourhoodT neighbourhood_type = CoMMANeighbourhoodT::EXTENDED ) { // NOTATION //====================================== diff --git a/src/CoMMA.cpp b/src/CoMMA.cpp index 01b9ae2..0e2f502 100644 --- a/src/CoMMA.cpp +++ b/src/CoMMA.cpp @@ -21,12 +21,14 @@ #include #include "CoMMA/CoMMAConfig.h" +#include "CoMMA/CoMMADefs.h" #define CoMMA_xstr(s) CoMMA_str(s) #define CoMMA_str(s) #s using namespace comma; // NOLINT using namespace pybind11::literals; // NOLINT Use _a for args literals +namespace py = pybind11; using namespace std; // NOLINT using CoMMASignedIndexT = make_signed::type; @@ -44,13 +46,43 @@ PYBIND11_MODULE(CoMMA, module_handle) { " - IndexType: type for indices\n" " - WeightType: type for weights\n" " - IntType: type for integers"; - module_handle.attr("__version__") = CoMMA_xstr(CoMMA_VERSION), + module_handle.attr("__version__") = CoMMA_xstr(CoMMA_VERSION); + // It's a bit hard to convert from C type to numpy type, // https://numpy.org/doc/stable/user/basics.types.html so we use just // suggestions - module_handle.attr("IndexType") = "ulong", - module_handle.attr("WeightType") = "double", - module_handle.attr("IntType") = "int", + module_handle.attr("IndexType") = "ulong"; + module_handle.attr("WeightType") = "double"; + module_handle.attr("IntType") = "int"; + + // Setting up fictitious sub-modules that will hold only the enum definitions + // for better calls, e.g., CoMMA.SeedsPool.BOUNDARY. + // This would not be necessary if the enum were inside classes as in the + // pybind11 example + // https://pybind11.readthedocs.io/en/stable/classes.html?highlight=enum#enumerations-and-internal-types + auto submodule_neigh = + module_handle.def_submodule("Neighbourhood", "Type of neighbourhood"); + auto submodule_seeds = + module_handle.def_submodule("SeedsPool", "Type of seeds pool ordering"); + // Exporting enumerator to be used in python + // https://stackoverflow.com/questions/47893832/pybind11-global-level-enum + py::enum_(submodule_neigh, "CoMMANeighbourhoodT") + .value("EXTENDED", CoMMANeighbourhoodT::EXTENDED) + .value("PURE_FRONT", CoMMANeighbourhoodT::PURE_FRONT) + .export_values(); + py::enum_(submodule_seeds, "CoMMASeedsPoolT") + .value("BOUNDARY", CoMMASeedsPoolT::BOUNDARY_PRIORITY) + .value("NEIGHBOURHOOD", CoMMASeedsPoolT::NEIGHBOURHOOD_PRIORITY) + .value( + "BOUNDARY_POINT_INIT", CoMMASeedsPoolT::BOUNDARY_PRIORITY_ONE_POINT_INIT + ) + .value( + "NEIGHBOURHOOD_POINT_INIT", + CoMMASeedsPoolT::NEIGHBOURHOOD_PRIORITY_ONE_POINT_INIT + ) + .export_values(); + + // Main function module_handle.def( "agglomerate_one_level", []( // Dual graph @@ -72,7 +104,7 @@ PYBIND11_MODULE(CoMMA, module_handle) { CoMMAWeightT threshold_anisotropy, // Seed ordering - const CoMMAIntT seed_ordering_type, + CoMMASeedsPoolT seed_ordering_type, // Outputs vector fc_to_cc, // Out @@ -88,7 +120,7 @@ PYBIND11_MODULE(CoMMA, module_handle) { CoMMAIntT singular_card_thresh, optional max_cells_in_line, CoMMAIntT fc_choice_iter, - const CoMMAIntT type_of_isotropic_agglomeration + CoMMANeighbourhoodT type_of_isotropic_agglomeration ) { agglomerate_one_level( adjMatrix_row_ptr, @@ -147,8 +179,9 @@ PYBIND11_MODULE(CoMMA, module_handle) { "singular_card_thresh"_a = 1, "max_cells_in_line"_a = std::nullopt, "fc_choice_iter"_a = 1, - "type_of_isotropic_agglomeration"_a = 0 + "type_of_isotropic_agglomeration"_a = CoMMANeighbourhoodT::EXTENDED ); + module_handle.def( "compute_neighbourhood_based_wall_distance", [](