Skip to content

Commit

Permalink
implement set_hp_flag function
Browse files Browse the repository at this point in the history
  • Loading branch information
kenichi-maeda committed Aug 15, 2024
1 parent 7e05715 commit 14a4753
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 33 deletions.
6 changes: 5 additions & 1 deletion examples/MeshAPI/BrainVerciseMesh_AdaptiveMeshRefinement.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@
],
"MaterialDistribution": {"MRIPath": "../../input_files/Butenko_segmask.nii.gz"},
"Mesh": {
"AdaptiveMeshRefinement": {"Active": True, "MaxIterations": 2},
"AdaptiveMeshRefinement": {
"Active": True,
"MaxIterations": 2,
"ErrorTolerance": 0.1,
},
"HPRefinement": {"Active": False, "Order": 1},
},
"StimulationSignal": {
Expand Down
36 changes: 9 additions & 27 deletions ossdbs/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
dielectric_models,
)
from ossdbs.electrodes import ELECTRODE_MODELS, ELECTRODE_PARAMETERS, ELECTRODES
from ossdbs.electrodes.electrode_model_template import ElectrodeModel
from ossdbs.fem import (
PRECONDITIONERS,
SOLVERS,
Expand Down Expand Up @@ -60,6 +59,10 @@ def create_bounding_box(box_parameters: dict) -> BoundingBox:
def generate_electrodes(settings: dict):
"""Generate an OCC electrode model from the settings dict."""
_logger.info("Generate electrode geometries")

hp_refinement = False
if "HPRefinement" in settings["Mesh"]:
hp_refinement = settings["Mesh"]["HPRefinement"]["Active"]
electrodes = []
for electrode_parameters in settings["Electrodes"]:
name = electrode_parameters["Name"]
Expand Down Expand Up @@ -95,18 +98,11 @@ def generate_electrodes(settings: dict):
rotation=rotation,
)

# Apply hp-refinement only on active contacts
if "HPRefinement" in settings["Mesh"]:
hp_settings = settings["Mesh"]["HPRefinement"]
if hp_settings["Active"] and "Contacts" in electrode_parameters:
for contact_info in electrode_parameters["Contacts"]:
if contact_info["Active"]:
contact_idx = contact_info["Contact_ID"]
order = hp_settings["Order"]
_set_edge_hp_flag(electrode, {f"Contact_{contact_idx}": order})
_set_vertex_hp_flag(
electrode, {f"Contact_{contact_idx}": order}
)
if hp_refinement:
electrode.set_hp_flag(
electrode_parameters=electrode_parameters,
hp_parameters=settings["Mesh"]["HPRefinement"],
)

if "EncapsulationLayer" in electrode_parameters:
electrode.encapsulation_thickness = electrode_parameters[
Expand Down Expand Up @@ -631,17 +627,3 @@ def run_PAM(settings):
scaling=settings["Scaling"],
scaling_index=settings["ScalingIndex"],
)


def _set_edge_hp_flag(electrode: ElectrodeModel, edge_sizes: dict) -> None:
"""Set flag on edges."""
for edge in electrode.geometry.edges:
if edge.name in edge_sizes:
edge.hpref = edge_sizes[edge.name]


def _set_vertex_hp_flag(electrode: ElectrodeModel, vertex_sizes) -> None:
"""Set flag on vertices."""
for vertex in electrode.geometry.vertices:
if vertex.name in vertex_sizes:
vertex.hpref = vertex_sizes[vertex.name]
22 changes: 22 additions & 0 deletions ossdbs/electrodes/electrode_model_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,3 +225,25 @@ def export_electrode(self, output_path, brain_dict, n_electrode) -> None:
filename=f"{output_path}/electrode_{n_electrode}",
subdivision=0,
).Do(vb=BND)

def set_hp_flag(self, electrode_parameters: dict, hp_parameters: dict):
"""Set hp-flags only on active contacts."""
if "Contacts" in electrode_parameters:
for contact_info in electrode_parameters["Contacts"]:
if contact_info["Active"]:
contact_idx = contact_info["Contact_ID"]
order = hp_parameters["Order"]
self._set_edge_hp_flag({f"Contact_{contact_idx}": order})
self._set_vertex_hp_flag({f"Contact_{contact_idx}": order})

def _set_edge_hp_flag(self, edge_sizes: dict) -> None:
"""Set flags on edges."""
for edge in self.geometry.edges:
if edge.name in edge_sizes:
edge.hpref = edge_sizes[edge.name]

def _set_vertex_hp_flag(self, vertex_sizes) -> None:
"""Set flags on vertices."""
for vertex in self.geometry.vertices:
if vertex.name in vertex_sizes:
vertex.hpref = vertex_sizes[vertex.name]
7 changes: 3 additions & 4 deletions ossdbs/fem/volume_conductor/volume_conductor_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,11 +260,10 @@ def run_full_analysis(
# currently: refine until impedance doesn't change by more than 0.1%
error = 100
refinements = 0
tolerance = adaptive_mesh_refinement["ErrorTolerance"]
max_iterations = adaptive_mesh_refinement["MaxIterations"]
# TODO write a meaningful algo
while (
error > 0.1
and refinements < adaptive_mesh_refinement["MaxIterations"]
):
while error > tolerance and refinements < max_iterations:
self.adaptive_mesh_refinement()
# solve on refined mesh
self.compute_solution(frequency)
Expand Down
6 changes: 5 additions & 1 deletion ossdbs/utils/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,11 @@ class Settings:
"MaxMeshSize": 1e6,
},
"HPRefinement": {"Active": False, "Order": 1},
"AdaptiveMeshRefinement": {"Active": False, "MaxIterations": 10},
"AdaptiveMeshRefinement": {
"Active": False,
"MaxIterations": 10,
"ErrorTolerance": 0.1,
},
"MeshSize": {"Edges": {}, "Faces": {}, "Volumes": {}},
"SaveMesh": False,
"SavePath": "mesh",
Expand Down

0 comments on commit 14a4753

Please sign in to comment.