diff --git a/emi_passive.py b/emi_passive.py index 75e09d7..5ba1771 100644 --- a/emi_passive.py +++ b/emi_passive.py @@ -21,8 +21,47 @@ import scifem +def get_shape_marker( + shape_type: str, **kwargs +) -> callable: + """ + Returns a marker function for dolfinx based on the requested shape + """ + if shape_type == "rectangle": + x_L = kwargs.get("x_L", 0.25) + x_U = kwargs.get("x_U", 0.75) + y_L = kwargs.get("y_L", 0.25) + y_U = kwargs.get("y_U", 0.75) + + def marker(x): + return (x[0] >= x_L - 1e-12) & (x[0] <= x_U + 1e-12) & \ + (x[1] >= y_L - 1e-12) & (x[1] <= y_U + 1e-12) + return marker + + elif shape_type == "irregular_cell": + x_c = kwargs.get("x_c", 0.5) + y_c = kwargs.get("y_c", 0.5) + base_r = kwargs.get("base_r", 0.2) + amplitudes = kwargs.get("amplitudes", [0.03, 0.02, 0.01]) + phases = kwargs.get("phases", [0.0, 1.5, 3.14]) + + def marker(x): + dx = x[0] - x_c + dy = x[1] - y_c + theta = np.arctan2(dy, dx) + dist = np.sqrt(dx**2 + dy**2) + + r_theta = base_r + for k, (A, phi) in enumerate(zip(amplitudes, phases), start=1): + r_theta += A * np.sin(k * theta + phi) + + return dist <= r_theta + 1e-12 + return marker + else: + raise ValueError(f"Unknown shape_type: {shape_type}") + def create_geometry( - M: int, x_L: float, x_U: float, y_L: float, y_U: float, + M: int, marker_function: callable, interior_marker: int, exterior_marker: int, interface_marker: int, boundary_marker: int, comm=MPI.COMM_WORLD ) -> tuple: @@ -31,22 +70,6 @@ def create_geometry( and extracts the submeshes for the intracellular and extracellular domains. """ - # --- 1. Define the bounds of the cell --- - def lower_bound(x, i, bound, tol=1e-12): - return x[i] >= bound - tol - - def upper_bound(x, i, bound, tol=1e-12): - return x[i] <= bound + tol - - def omega_interior_marker(x, tol=1e-12): - # Defines a rectangular box representing the cell based on the given coordinates - return ( - lower_bound(x, 0, x_L, tol=tol) - & lower_bound(x, 1, y_L, tol=tol) - & upper_bound(x, 0, x_U, tol=tol) - & upper_bound(x, 1, y_U, tol=tol) - ) - # --- 2. Create the main mesh --- omega = dolfinx.mesh.create_unit_square( comm, M, M, ghost_mode=dolfinx.mesh.GhostMode.shared_facet @@ -54,7 +77,7 @@ def omega_interior_marker(x, tol=1e-12): # --- 3. Locate and tag subdomains (Cell Tags) interior_cells = dolfinx.mesh.locate_entities( - omega, omega.topology.dim, omega_interior_marker + omega, omega.topology.dim, marker_function ) cell_map = omega.topology.index_map(omega.topology.dim) @@ -197,7 +220,7 @@ def setup_mms_sources( # --- 1. Define the exact solution --- x, y = SpatialCoordinate(omega) - ue_exact = sin(pi * (x + y)) + ue_exact = x + sin(pi * x) * sin(pi * y) ui_exact = sigma_e / sigma_i * ue_exact + cos(pi * (x - x_L) * (x - x_U)) * cos( pi * (y - y_L) * (y - y_U) ) @@ -436,7 +459,7 @@ def plot_convergence( if __name__ == "__main__": # --- CONTROL PANEL --- - M_values = [32] + M_values = [16, 32, 64, 128, 256] h_values = [] errors_ui = [] errors_ue = [] @@ -470,9 +493,19 @@ def plot_convergence( PETSc.Sys.Print(f"==========================================") # --- PIPELINE --- - # 1. Build geometry + + # 1a. Generate the shape marker ("rectangle" or "irregular_cell") + my_marker = get_shape_marker( + shape_type = "rectangle", + x_L = 0.25, x_U = 0.75, y_L = 0.25, y_U = 0.75 + #x_c = 0.5, y_c = 0.5, base_r = 0.25, + #amplitudes = [0.05, 0.03, 0.02, 0.01], + #phases = [0.0, 1.2, 2.5, 0.8] + ) + + # 1b. Build geometry (omega, omega_e, omega_i, ct, ft, interior_to_parent, exterior_to_parent, e_vertex_to_parent - ) = create_geometry(M, x_L, x_U, y_L, y_U, interior_marker, exterior_marker, interface_marker, boundary_marker) + ) = create_geometry(M, my_marker, interior_marker, exterior_marker, interface_marker, boundary_marker) # Compute T and define sigma_e and sigma_i as dolfinx.fem.Constants (T, sigma_e, sigma_i) = create_physical_constants(omega, omega_e, omega_i, val_sigma_e, val_sigma_i, val_gm) @@ -482,13 +515,13 @@ def plot_convergence( ) = setup_spaces_and_measures(omega, omega_e, omega_i, ct, ft, interior_marker, exterior_marker, interface_marker, fem_element) # 3a. Find the source terms, given an exact solution - """ (f, f_e, f_i, ue_exact, ui_exact ) = setup_mms_sources(omega, omega_e, omega_i, x_L, x_U, y_L, y_U, sigma_e, sigma_i, T, e_res) - """ # 3b. Define source terms. Alternative to setup_mms_sources + """ (f, f_e, f_i) = setup_realistic_sources(omega, omega_e, omega_i) + """ # 4. Create variational form and boundary conditions (a, L, bc @@ -504,7 +537,7 @@ def plot_convergence( if M == M_values[-1]: save_results(ue_sol, ui_sol, omega_e, omega_i) - """ + # 7. Computing L2 error using the new function entity_maps = [interior_to_parent, exterior_to_parent] L2_ue, L2_ui = compute_l2_error(ue_sol, ui_sol, ue_exact, ui_exact, dxE, dxI, entity_maps) @@ -514,11 +547,10 @@ def plot_convergence( h_values.append(1.0 / M) errors_ui.append(L2_ui) errors_ue.append(L2_ue) - """ + - """ + # 8. Plot and save the result plot_convergence(h_values, errors_ue, errors_ui) - """ - + \ No newline at end of file diff --git a/error_convergence.png b/error_convergence.png index ab9c887..cd68514 100644 Binary files a/error_convergence.png and b/error_convergence.png differ diff --git a/generate_dataset.py b/generate_dataset.py index ee86848..f766979 100644 --- a/generate_dataset.py +++ b/generate_dataset.py @@ -6,6 +6,7 @@ # Import solver functions directly from emi_passive.py from emi_passive import ( + get_shape_marker, create_geometry, create_physical_constants, setup_realistic_sources, @@ -15,36 +16,37 @@ ) def geometry_randomizer( - min_size: float = 0.2, - max_size: float = 0.6 + min_r: float = 0.15, + max_r: float = 0.35 ) -> tuple: """ - Picks a random rectangular cell geometry ensuring it stays completely - inside the global domain [0, 1] x [0, 1]. + Picks parameters for a random irregular cell geometry ensuring it + stays completely inside the global domain [0,1] x [0,1]. """ - # 1. Pick a random width and height within allowed limits - width = np.random.uniform(min_size, max_size) - height = np.random.uniform(min_size, max_size) - # 2. Pick a random center point (x, y) such that the rectangle fits inside [0,1]^2 - x_center = np.random.uniform(width / 2.0, 1.0 - (width / 2.0)) - y_center = np.random.uniform(height / 2.0, 1.0 - (height / 2.0)) - - # 3. Calculate resulting boundaries - x_L = x_center - (width / 2.0) - x_U = x_center + (width / 2.0) - y_L = y_center - (height / 2.0) - y_U = y_center + (height / 2.0) + base_r = np.random.uniform(min_r, max_r) + + # Generate 3 to 5 random irregular bumps + num_bumps = np.random.randint(3,6) + amplitudes = np.random.uniform(0.01, 0.04, num_bumps).tolist() + phases = np.random.uniform(0.0, 2 * np.pi, num_bumps).tolist() + + # Calculate the absolute maximum radius to prevent boundary clipping + max_radius = base_r + sum(amplitudes) + 1e-3 + + # Pick a random center point ensuring the cell fits inside [0,1]^2 + x_c = np.random.uniform(max_radius, 1.0 - max_radius) + y_c = np.random.uniform(max_radius, 1.0 - max_radius) + - # Returns the boundaries of the rectangle - return (x_L, x_U, y_L, y_U) + return (x_c, y_c, base_r, amplitudes, phases) def sample_submesh_at_points( mesh: dolfinx.mesh.Mesh, function: dolfinx.fem.Function, points_3d: np.ndarray, tree: dolfinx.geometry.BoundingBoxTree = None, - colliding_cells: dolfinx.geometry.CollisionList = None + colliding_cells: dolfinx.graph.adjacencylist = None ) -> np.ndarray: """ Helper function to robustly evaluate a FEniCSx function at arbitrary 2D grid points. @@ -68,7 +70,11 @@ def sample_submesh_at_points( vals = np.zeros(len(points_3d)) if len(eval_points) > 0: - eval_vals = function.eval(np.array(eval_points), np.array(eval_cells)) + # Enforce exact data types for the C++ backend + pts_array = np.array(eval_points, dtype = np.float64) + cells_array = np.array(eval_cells, dtype = np.int32) + + eval_vals = function.eval(pts_array, cells_array) vals[point_indices] = eval_vals.flatten() # Returns the evaluated function values mapped to the target point indices @@ -79,13 +85,11 @@ def mesh_to_pixels( ui_sol: dolfinx.fem.Function, omega_e: dolfinx.mesh.Mesh, omega_i: dolfinx.mesh.Mesh, - boundaries: tuple, grid_resolution: int ) -> tuple: """ Turns the irregular FEniCSx triangular submesh outputs into a structured pixel grid. """ - x_L, x_U, y_L, y_U = boundaries x_lin = np.linspace(0.0, 1.0, grid_resolution) y_lin = np.linspace(0.0, 1.0, grid_resolution) @@ -107,11 +111,11 @@ def mesh_to_pixels( phi_flat = np.where(inside_mask, 1.0, 0.0) u_flat = np.zeros_like(phi_flat) - inside_coords = flat_points[inside_mask] - outside_coords = flat_points[~inside_mask] + inside_coords = points_3d[inside_mask] + outside_coords = points_3d[~inside_mask] if len(inside_coords) > 0: - u_flat[inside_mask] = sample_submesh_at_points(omega_i, ui_sol, inside_coords, tree_i, colliding_cells_i) + u_flat[inside_mask] = sample_submesh_at_points(omega_i, ui_sol, inside_coords) if len(outside_coords) > 0: u_flat[~inside_mask] = sample_submesh_at_points(omega_e, ue_sol, outside_coords) @@ -143,13 +147,19 @@ def solve_single_sample( fem_element = ("Lagrange", 1) # Step 1: Generate a random cell shape - bounds = geometry_randomizer(min_size=0.15, max_size=0.55) - x_L, x_U, y_L, y_U = bounds + shape_params = geometry_randomizer(min_r=0.15, max_r=0.25) + x_c, y_c, base_r, amplitudes, phases = shape_params # Step 2: Build geometry mesh and extract submeshes via emi_passive + marker_fn = get_shape_marker( + shape_type = "irregular_cell", + x_c = x_c, y_c = y_c, base_r = base_r, + amplitudes = amplitudes, phases = phases + ) + (omega, omega_e, omega_i, ct, ft, interior_to_parent, exterior_to_parent, e_vertex_to_parent ) = create_geometry( - mesh_density, x_L, x_U, y_L, y_U, int_marker, ext_marker, inter_marker, bnd_marker, comm=MPI.COMM_SELF + mesh_density, marker_fn, int_marker, ext_marker, inter_marker, bnd_marker, comm=MPI.COMM_SELF ) # Step 3: Define physical constants @@ -181,7 +191,7 @@ def solve_single_sample( ) # Step 8: Sample continuous mesh results onto uniform pixel arrays - phi, u_grid = mesh_to_pixels(ue_sol, ui_sol, omega_e, omega_i, bounds, grid_res) + phi, u_grid = mesh_to_pixels(ue_sol, ui_sol, omega_e, omega_i, grid_res) # Returns the structured geometry mask and voltage field arrays for a single generated sample return phi, u_grid @@ -261,8 +271,8 @@ def build_fno_dataset_parallel( # Control panel total_samples = 1000 - grid_res = 64 - mesh_density = 32 + grid_res = 128 + mesh_density = 128 # Build the dataset # Run code: diff --git a/sample_verification.png b/sample_verification.png index 1da4da5..acc8042 100644 Binary files a/sample_verification.png and b/sample_verification.png differ diff --git a/uh_e.bp/data.0 b/uh_e.bp/data.0 index 3723e96..9ac3af8 100644 Binary files a/uh_e.bp/data.0 and b/uh_e.bp/data.0 differ diff --git a/uh_e.bp/md.0 b/uh_e.bp/md.0 index 89c48b7..83aee0e 100644 Binary files a/uh_e.bp/md.0 and b/uh_e.bp/md.0 differ diff --git a/uh_e.bp/profiling.json b/uh_e.bp/profiling.json index c483b75..59b5c85 100644 --- a/uh_e.bp/profiling.json +++ b/uh_e.bp/profiling.json @@ -1,3 +1,3 @@ [ -{ "rank":0, "start":"Mon_Jul_27_11:19:35_2026","ES_MDAgg_GatherWriteMeta_MDBlocks_mus": 6, "ES_MDAgg_GatherWriteMeta_MDBlocks":{"mus":6, "nCalls":1},"ES_MDAgg_GatherWriteMeta_mus": 8255, "ES_MDAgg_GatherWriteMeta":{"mus":8255, "nCalls":1},"ES_MDAgg_AggInfo_SelectMetaInfoGather_mus": 6, "ES_MDAgg_AggInfo_SelectMetaInfoGather":{"mus":6, "nCalls":1},"ES_WriteData_mus": 1985, "ES_WriteData":{"mus":1985, "nCalls":1},"WriteMmD_mus": 6505, "WriteMmD":{"mus":6505, "nCalls":1},"ES_MDAgg_mus": 8305, "ES_MDAgg":{"mus":8305, "nCalls":1},"InitAgg-tls_mus": 38, "InitAgg-tls":{"mus":38, "nCalls":1},"ES_mus": 10453, "ES":{"mus":10453, "nCalls":1},"WriteData_mus": 1980, "WriteData":{"mus":1980, "nCalls":1},"WriteMD_mus": 1005, "WriteMD":{"mus":1005, "nCalls":1},"DC_mus": 3994, "DC":{"mus":3994, "nCalls":1},"PP_mus": 0, "PP":{"mus":0, "nCalls":1},"ES_CloseTS_mus": 147, "ES_CloseTS":{"mus":147, "nCalls":1},"ES_MDAgg_AggInfo_mus": 40, "ES_MDAgg_AggInfo":{"mus":40, "nCalls":1},"BS_mus": 7, "BS":{"mus":7, "nCalls":1},"ES_MDAgg_AggInfo_FixedMetaInfoGather_mus": 0, "ES_MDAgg_AggInfo_FixedMetaInfoGather":{"mus":0, "nCalls":1},"ES_MDAgg_AggInfo_MetaInfoBcast_mus": 1, "ES_MDAgg_AggInfo_MetaInfoBcast":{"mus":1, "nCalls":1}, "databytes":0, "metadatabytes":0, "metametadatabytes":0, "transport_0":{"type":"File_POSIX", "name":"data.0", "wbytes":84576, "close":{"mus":793, "nCalls":1}, "write":{"mus":1898, "nCalls":1}, "open":{"mus":1833, "nCalls":1}}, "transport_1":{"type":"File_POSIX", "name":"md.0", "wbytes":1976, "close":{"mus":528, "nCalls":1}, "write":{"mus":996, "nCalls":1}, "open":{"mus":5324, "nCalls":1}} } +{ "rank":0, "start":"Sat_Aug_01_08:00:07_2026","ES_MDAgg_GatherWriteMeta_MDBlocks_mus": 15, "ES_MDAgg_GatherWriteMeta_MDBlocks":{"mus":15, "nCalls":1},"ES_MDAgg_GatherWriteMeta_mus": 4543, "ES_MDAgg_GatherWriteMeta":{"mus":4543, "nCalls":1},"ES_MDAgg_AggInfo_SelectMetaInfoGather_mus": 5, "ES_MDAgg_AggInfo_SelectMetaInfoGather":{"mus":5, "nCalls":1},"ES_WriteData_mus": 21545, "ES_WriteData":{"mus":21545, "nCalls":1},"WriteMmD_mus": 3766, "WriteMmD":{"mus":3766, "nCalls":1},"ES_MDAgg_mus": 4590, "ES_MDAgg":{"mus":4590, "nCalls":1},"InitAgg-tls_mus": 49, "InitAgg-tls":{"mus":49, "nCalls":1},"ES_mus": 26259, "ES":{"mus":26259, "nCalls":1},"WriteData_mus": 21541, "WriteData":{"mus":21541, "nCalls":1},"WriteMD_mus": 271, "WriteMD":{"mus":271, "nCalls":1},"DC_mus": 2656, "DC":{"mus":2656, "nCalls":1},"PP_mus": 0, "PP":{"mus":0, "nCalls":1},"ES_CloseTS_mus": 108, "ES_CloseTS":{"mus":108, "nCalls":1},"ES_MDAgg_AggInfo_mus": 35, "ES_MDAgg_AggInfo":{"mus":35, "nCalls":1},"BS_mus": 6, "BS":{"mus":6, "nCalls":1},"ES_MDAgg_AggInfo_FixedMetaInfoGather_mus": 0, "ES_MDAgg_AggInfo_FixedMetaInfoGather":{"mus":0, "nCalls":1},"ES_MDAgg_AggInfo_MetaInfoBcast_mus": 1, "ES_MDAgg_AggInfo_MetaInfoBcast":{"mus":1, "nCalls":1}, "databytes":0, "metadatabytes":0, "metametadatabytes":0, "transport_0":{"type":"File_POSIX", "name":"data.0", "wbytes":5192448, "close":{"mus":413, "nCalls":1}, "write":{"mus":21206, "nCalls":1}, "open":{"mus":2325, "nCalls":1}}, "transport_1":{"type":"File_POSIX", "name":"md.0", "wbytes":1976, "close":{"mus":408, "nCalls":1}, "write":{"mus":261, "nCalls":1}, "open":{"mus":3721, "nCalls":1}} } ] diff --git a/uh_i.bp/data.0 b/uh_i.bp/data.0 index 543b42d..5f8ad81 100644 Binary files a/uh_i.bp/data.0 and b/uh_i.bp/data.0 differ diff --git a/uh_i.bp/md.0 b/uh_i.bp/md.0 index 8181137..955d1c0 100644 Binary files a/uh_i.bp/md.0 and b/uh_i.bp/md.0 differ diff --git a/uh_i.bp/profiling.json b/uh_i.bp/profiling.json index 3bcf33f..20ef6d5 100644 --- a/uh_i.bp/profiling.json +++ b/uh_i.bp/profiling.json @@ -1,3 +1,3 @@ [ -{ "rank":0, "start":"Mon_Jul_27_11:19:35_2026","ES_MDAgg_GatherWriteMeta_MDBlocks_mus": 15, "ES_MDAgg_GatherWriteMeta_MDBlocks":{"mus":15, "nCalls":1},"ES_MDAgg_GatherWriteMeta_mus": 6707, "ES_MDAgg_GatherWriteMeta":{"mus":6707, "nCalls":1},"ES_MDAgg_AggInfo_SelectMetaInfoGather_mus": 20, "ES_MDAgg_AggInfo_SelectMetaInfoGather":{"mus":20, "nCalls":1},"ES_WriteData_mus": 968, "ES_WriteData":{"mus":968, "nCalls":1},"WriteMmD_mus": 5427, "WriteMmD":{"mus":5427, "nCalls":1},"ES_MDAgg_mus": 6803, "ES_MDAgg":{"mus":6803, "nCalls":1},"InitAgg-tls_mus": 135, "InitAgg-tls":{"mus":135, "nCalls":1},"ES_mus": 7939, "ES":{"mus":7939, "nCalls":1},"WriteData_mus": 965, "WriteData":{"mus":965, "nCalls":1},"WriteMD_mus": 727, "WriteMD":{"mus":727, "nCalls":1},"DC_mus": 3759, "DC":{"mus":3759, "nCalls":1},"PP_mus": 0, "PP":{"mus":0, "nCalls":1},"ES_CloseTS_mus": 151, "ES_CloseTS":{"mus":151, "nCalls":1},"ES_MDAgg_AggInfo_mus": 81, "ES_MDAgg_AggInfo":{"mus":81, "nCalls":1},"BS_mus": 7, "BS":{"mus":7, "nCalls":1},"ES_MDAgg_AggInfo_FixedMetaInfoGather_mus": 1, "ES_MDAgg_AggInfo_FixedMetaInfoGather":{"mus":1, "nCalls":1},"ES_MDAgg_AggInfo_MetaInfoBcast_mus": 3, "ES_MDAgg_AggInfo_MetaInfoBcast":{"mus":3, "nCalls":1}, "databytes":0, "metadatabytes":0, "metametadatabytes":0, "transport_0":{"type":"File_POSIX", "name":"data.0", "wbytes":28240, "close":{"mus":668, "nCalls":1}, "write":{"mus":883, "nCalls":1}, "open":{"mus":1782, "nCalls":1}}, "transport_1":{"type":"File_POSIX", "name":"md.0", "wbytes":1976, "close":{"mus":468, "nCalls":1}, "write":{"mus":697, "nCalls":1}, "open":{"mus":4252, "nCalls":1}} } +{ "rank":0, "start":"Sat_Aug_01_08:00:07_2026","ES_MDAgg_GatherWriteMeta_MDBlocks_mus": 14, "ES_MDAgg_GatherWriteMeta_MDBlocks":{"mus":14, "nCalls":1},"ES_MDAgg_GatherWriteMeta_mus": 3700, "ES_MDAgg_GatherWriteMeta":{"mus":3700, "nCalls":1},"ES_MDAgg_AggInfo_SelectMetaInfoGather_mus": 6, "ES_MDAgg_AggInfo_SelectMetaInfoGather":{"mus":6, "nCalls":1},"ES_WriteData_mus": 11116, "ES_WriteData":{"mus":11116, "nCalls":1},"WriteMmD_mus": 3239, "WriteMmD":{"mus":3239, "nCalls":1},"ES_MDAgg_mus": 3752, "ES_MDAgg":{"mus":3752, "nCalls":1},"InitAgg-tls_mus": 31, "InitAgg-tls":{"mus":31, "nCalls":1},"ES_mus": 14996, "ES":{"mus":14996, "nCalls":1},"WriteData_mus": 11112, "WriteData":{"mus":11112, "nCalls":1},"WriteMD_mus": 230, "WriteMD":{"mus":230, "nCalls":1},"DC_mus": 4991, "DC":{"mus":4991, "nCalls":1},"PP_mus": 0, "PP":{"mus":0, "nCalls":1},"ES_CloseTS_mus": 110, "ES_CloseTS":{"mus":110, "nCalls":1},"ES_MDAgg_AggInfo_mus": 39, "ES_MDAgg_AggInfo":{"mus":39, "nCalls":1},"BS_mus": 3, "BS":{"mus":3, "nCalls":1},"ES_MDAgg_AggInfo_FixedMetaInfoGather_mus": 0, "ES_MDAgg_AggInfo_FixedMetaInfoGather":{"mus":0, "nCalls":1},"ES_MDAgg_AggInfo_MetaInfoBcast_mus": 1, "ES_MDAgg_AggInfo_MetaInfoBcast":{"mus":1, "nCalls":1}, "databytes":0, "metadatabytes":0, "metametadatabytes":0, "transport_0":{"type":"File_POSIX", "name":"data.0", "wbytes":1730864, "close":{"mus":633, "nCalls":1}, "write":{"mus":10948, "nCalls":1}, "open":{"mus":1638, "nCalls":1}}, "transport_1":{"type":"File_POSIX", "name":"md.0", "wbytes":1976, "close":{"mus":468, "nCalls":1}, "write":{"mus":210, "nCalls":1}, "open":{"mus":6410, "nCalls":1}} } ]