26 #include "petsc_utils.h"
37 #include <initializer_list>
38 #include <sys/resource.h>
42 #include "caliper/cali.h"
51 template<
class Assemble>
52 void assemble_with_exact_preallocation(std::initializer_list<sf_mat*> matrices, Assemble assemble)
54 bool any_supported =
false;
55 bool all_supported =
true;
56 bool saw_matrix =
false;
58 for(
sf_mat* mat : matrices) {
59 if(mat ==
nullptr)
continue;
62 const bool supported = mat->begin_exact_preallocation();
63 any_supported = any_supported || supported;
64 all_supported = all_supported && supported;
67 if(!saw_matrix)
return;
72 assert(any_supported == all_supported);
78 for(
sf_mat* mat : matrices) {
79 if(mat !=
nullptr) mat->finalize_exact_preallocation();
88 void log_emi_petsc_matrix_preallocation_report(std::initializer_list<std::pair<const char*, sf_mat*>> matrices)
91 PetscBool enabled = PETSC_FALSE;
92 PetscOptionsHasName(NULL, NULL,
"-mat_view_info", &enabled);
101 MPI_Comm_rank(PETSC_COMM_WORLD, &rank);
103 log_msg(NULL, 0, 0,
"\nEMI PETSc matrix preallocation report");
104 log_msg(NULL, 0, 0,
"matrix alloc/used used_est allocated_est overalloc_est");
107 constexpr
double bytes_per_nz = double(
sizeof(PetscScalar) +
sizeof(PetscInt));
108 constexpr
double gib = 1024.0 * 1024.0 * 1024.0;
109 double total_used_gib = 0.0;
110 double total_allocated_gib = 0.0;
112 for(
const auto& item : matrices) {
113 auto* petsc_mat =
dynamic_cast<SF::petsc_matrix*
>(item.second);
114 if(petsc_mat ==
nullptr || petsc_mat->data ==
nullptr)
continue;
117 MatGetInfo(petsc_mat->data, MAT_GLOBAL_SUM, &info);
119 const double used =
static_cast<double>(info.nz_used);
120 const double allocated =
static_cast<double>(info.nz_allocated);
121 const double ratio = used > 0.0 ? allocated / used : 0.0;
122 const double used_gib = used * bytes_per_nz / gib;
123 const double allocated_gib = allocated * bytes_per_nz / gib;
124 const double overallocated_gib = allocated_gib - used_gib;
125 total_used_gib += used_gib;
126 total_allocated_gib += allocated_gib;
130 "%-18s alloc/used=%5.2f used_est=%6.2fG allocated_est=%6.2fG overalloc_est=%6.2fG",
131 item.first, ratio, used_gib, allocated_gib, overallocated_gib);
136 const double total_ratio = total_used_gib > 0.0 ? total_allocated_gib / total_used_gib : 0.0;
138 "%-18s alloc/used=%5.2f used_est=%6.2fG allocated_est=%6.2fG overalloc_est=%6.2fG",
139 "TOTAL:", total_ratio, total_used_gib, total_allocated_gib,
140 total_allocated_gib - total_used_gib);
144 getrusage(RUSAGE_SELF, &usage);
146 const double local_peak_rss_gib = double(usage.ru_maxrss) / gib;
148 const double local_peak_rss_gib = double(usage.ru_maxrss) * 1024.0 / gib;
150 double summed_peak_rss_gib = 0.0;
151 double max_rank_peak_rss_gib = 0.0;
152 MPI_Reduce(&local_peak_rss_gib, &summed_peak_rss_gib, 1, MPI_DOUBLE, MPI_SUM, 0, PETSC_COMM_WORLD);
153 MPI_Reduce(&local_peak_rss_gib, &max_rank_peak_rss_gib, 1, MPI_DOUBLE, MPI_MAX, 0, PETSC_COMM_WORLD);
157 "process peak RSS estimate: summed ranks=%6.2fG max rank=%6.2fG",
158 summed_peak_rss_gib, max_rank_peak_rss_gib);
160 "external peak memory from mprof --include-children is still the recommended whole-run reference.\n");
169 void log_mesh_local_element_ranges(
const sf_mesh& emi_mesh,
170 const sf_mesh& emi_surfmesh_w_counter_face,
171 const sf_mesh& emi_surfmesh_unique_face)
175 MPI_Comm_rank(emi_surfmesh_w_counter_face.comm, &rank);
176 MPI_Comm_size(emi_surfmesh_w_counter_face.comm, &comm_size);
178 const size_t local_emi_elems = emi_mesh.l_numelem;
179 const size_t local_both_face_elems = emi_surfmesh_w_counter_face.l_numelem;
180 const size_t local_unique_face_elems = emi_surfmesh_unique_face.l_numelem;
182 std::vector<size_t> all_emi_elems;
183 std::vector<size_t> all_both_face_elems;
184 std::vector<size_t> all_unique_face_elems;
186 all_emi_elems.resize(comm_size, 0);
187 all_both_face_elems.resize(comm_size, 0);
188 all_unique_face_elems.resize(comm_size, 0);
191 const MPI_Datatype size_mpi_t = mpi_datatype<size_t>();
192 MPI_Gather(&local_emi_elems, 1, size_mpi_t,
193 rank == 0 ? all_emi_elems.data() :
nullptr, 1, size_mpi_t,
194 0, emi_surfmesh_w_counter_face.comm);
195 MPI_Gather(&local_both_face_elems, 1, size_mpi_t,
196 rank == 0 ? all_both_face_elems.data() :
nullptr, 1, size_mpi_t,
197 0, emi_surfmesh_w_counter_face.comm);
198 MPI_Gather(&local_unique_face_elems, 1, size_mpi_t,
199 rank == 0 ? all_unique_face_elems.data() :
nullptr, 1, size_mpi_t,
200 0, emi_surfmesh_w_counter_face.comm);
202 if (rank != 0)
return;
204 const auto print_min_max = [](
const char* label,
const std::vector<size_t>& counts) {
205 if (counts.empty())
return;
207 size_t min_val = counts[0];
208 size_t max_val = counts[0];
212 for (
int r = 1; r < static_cast<int>(counts.size()); ++r) {
213 if (counts[r] < min_val) {
217 if (counts[r] > max_val) {
223 log_msg(NULL, 0, 0,
" %s: \n\t\t min=%zu on rank=%d, \n\t\t max=%zu on rank=%d\n",
224 label, min_val, min_rank, max_val, max_rank);
226 log_msg(NULL, 0, 0,
"\n**********************************");
227 log_msg(NULL, 0, 0,
"min/max number of local-element ranges:");
228 print_min_max(
"emi_mesh", all_emi_elems);
229 print_min_max(
"emi_surfmesh_w_counter_face", all_both_face_elems);
230 print_min_max(
"emi_surfmesh_unique_face", all_unique_face_elems);
231 log_msg(NULL, 0, 0,
"**********************************");
234 #ifdef EMI_DEBUG_MESH
241 if (param_globals::flavor != std::string(
"petsc"))
return;
243 auto* petsc_mat =
dynamic_cast<SF::petsc_matrix*
>(mat);
244 if (petsc_mat ==
nullptr)
return;
246 Vec x = NULL, y = NULL;
247 MatCreateVecs(petsc_mat->data, &x, &y);
249 PetscRandom rnd = NULL;
250 PetscRandomCreate(PETSC_COMM_WORLD, &rnd);
251 PetscRandomSetFromOptions(rnd);
254 MPI_Comm_rank(PETSC_COMM_WORLD, &rank);
256 PetscReal min_q = PETSC_MAX_REAL;
257 PetscReal max_q = -PETSC_MAX_REAL;
258 PetscInt nonpos_count = 0;
260 for (
int i = 0; i < num_trials; ++i) {
261 VecSetRandom(x, rnd);
262 MatMult(petsc_mat->data, x, y);
264 PetscScalar q_scalar = 0.0;
265 VecDot(x, y, &q_scalar);
267 const PetscReal q = PetscRealPart(q_scalar);
270 if (q <= 0.0) nonpos_count++;
274 PetscPrintf(PETSC_COMM_SELF,
275 "%s: SPD probe with %d random vectors: min(x^T A x)=%g, max(x^T A x)=%g, nonpositive=%d\n",
276 stage, num_trials,
double(min_q),
double(max_q),
int(nonpos_count));
279 PetscRandomDestroy(&rnd);
284 PetscScalar emi_probe_vector_entry(
const PetscInt gid,
const int probe_id)
286 const double x =
static_cast<double>(gid + 1);
290 return std::sin(1.0e-3 * x) + 0.5 * std::cos(3.0e-3 * x);
292 return std::cos(7.0e-4 * x) - 0.35 * std::sin(2.0e-3 * x);
294 return 0.75 * std::sin(1.3e-3 * x) + 0.25 * std::cos(4.0e-3 * x);
304 if (param_globals::flavor != std::string(
"petsc"))
return;
306 auto* petsc_mat =
dynamic_cast<SF::petsc_matrix*
>(mat);
307 if (petsc_mat ==
nullptr)
return;
309 Vec x = NULL, y = NULL;
310 MatCreateVecs(petsc_mat->data, &x, &y);
312 PetscInt i_start = 0, i_end = 0;
313 VecGetOwnershipRange(x, &i_start, &i_end);
315 PetscScalar* x_arr = NULL;
317 MPI_Comm_rank(PETSC_COMM_WORLD, &rank);
319 for (
int probe_id = 0; probe_id < num_probes; ++probe_id) {
320 VecGetArray(x, &x_arr);
321 for (PetscInt i = i_start; i < i_end; ++i) {
322 x_arr[i - i_start] = emi_probe_vector_entry(i, probe_id);
324 VecRestoreArray(x, &x_arr);
326 MatMult(petsc_mat->data, x, y);
328 PetscReal y_norm2 = 0.0;
329 PetscReal y_norminf = 0.0;
330 PetscScalar y_sum = 0.0;
331 PetscScalar xAy = 0.0;
332 VecNorm(y, NORM_2, &y_norm2);
333 VecNorm(y, NORM_INFINITY, &y_norminf);
337 PetscScalar weighted_checksum_local = 0.0;
338 const PetscScalar* y_arr = NULL;
339 VecGetArrayRead(y, &y_arr);
340 for (PetscInt i = i_start; i < i_end; ++i) {
341 const PetscScalar weight =
static_cast<PetscScalar
>(i + 1);
342 weighted_checksum_local += weight * y_arr[i - i_start];
344 VecRestoreArrayRead(y, &y_arr);
346 PetscScalar weighted_checksum = 0.0;
347 MPI_Allreduce(&weighted_checksum_local, &weighted_checksum, 1, MPIU_SCALAR, MPI_SUM, PETSC_COMM_WORLD);
350 PetscPrintf(PETSC_COMM_SELF,
351 "%s: operator probe %d ||Ax||_2=%g, ||Ax||_inf=%g, sum(Ax)=%g, x^T A x=%g, weighted_checksum=%g\n",
352 stage, probe_id + 1,
double(y_norm2),
double(y_norminf),
double(PetscRealPart(y_sum)),
353 double(PetscRealPart(xAy)),
double(PetscRealPart(weighted_checksum)));
364 if (param_globals::flavor != std::string(
"petsc"))
return;
366 auto* petsc_vec =
dynamic_cast<SF::petsc_vector*
>(vec);
367 if (petsc_vec ==
nullptr)
return;
369 PetscReal norm2 = 0.0;
370 PetscReal norminf = 0.0;
371 PetscScalar
sum = 0.0;
372 VecNorm(petsc_vec->data, NORM_2, &norm2);
373 VecNorm(petsc_vec->data, NORM_INFINITY, &norminf);
374 VecSum(petsc_vec->data, &
sum);
376 PetscInt i_start = 0, i_end = 0;
377 VecGetOwnershipRange(petsc_vec->data, &i_start, &i_end);
379 PetscScalar weighted_checksum_local = 0.0;
380 const PetscScalar* arr = NULL;
381 VecGetArrayRead(petsc_vec->data, &arr);
382 for (PetscInt i = i_start; i < i_end; ++i) {
383 const PetscScalar weight =
static_cast<PetscScalar
>(i + 1);
384 weighted_checksum_local += weight * arr[i - i_start];
386 VecRestoreArrayRead(petsc_vec->data, &arr);
388 PetscScalar weighted_checksum = 0.0;
389 MPI_Allreduce(&weighted_checksum_local, &weighted_checksum, 1, MPIU_SCALAR, MPI_SUM, PETSC_COMM_WORLD);
392 MPI_Comm_rank(PETSC_COMM_WORLD, &rank);
394 PetscPrintf(PETSC_COMM_SELF,
395 "%s: rhs probe ||b||_2=%g, ||b||_inf=%g, sum(b)=%g, weighted_checksum=%g\n",
396 stage,
double(norm2),
double(norminf),
double(PetscRealPart(
sum)),
397 double(PetscRealPart(weighted_checksum)));
406 if (param_globals::flavor != std::string(
"petsc"))
return;
408 auto* petsc_mat =
dynamic_cast<SF::petsc_matrix*
>(mat);
409 auto* petsc_vec =
dynamic_cast<SF::petsc_vector*
>(vec);
410 if (petsc_mat ==
nullptr || petsc_vec ==
nullptr)
return;
412 Vec x = NULL, ax = NULL, residual = NULL;
413 MatCreateVecs(petsc_mat->data, &x, &ax);
414 VecDuplicate(ax, &residual);
416 PetscInt i_start = 0, i_end = 0;
417 VecGetOwnershipRange(x, &i_start, &i_end);
420 MPI_Comm_rank(PETSC_COMM_WORLD, &rank);
422 for (
int probe_id = 0; probe_id < num_probes; ++probe_id) {
423 PetscScalar* x_arr = NULL;
424 VecGetArray(x, &x_arr);
425 for (PetscInt i = i_start; i < i_end; ++i) {
426 x_arr[i - i_start] = emi_probe_vector_entry(i, probe_id);
428 VecRestoreArray(x, &x_arr);
430 MatMult(petsc_mat->data, x, ax);
431 VecWAXPY(residual, -1.0, petsc_vec->data, ax);
433 PetscReal residual_norm2 = 0.0;
434 PetscReal residual_norminf = 0.0;
435 PetscScalar residual_sum = 0.0;
436 PetscScalar xTResidual = 0.0;
437 VecNorm(residual, NORM_2, &residual_norm2);
438 VecNorm(residual, NORM_INFINITY, &residual_norminf);
439 VecSum(residual, &residual_sum);
440 VecDot(x, residual, &xTResidual);
442 PetscScalar weighted_checksum_local = 0.0;
443 const PetscScalar* residual_arr = NULL;
444 VecGetArrayRead(residual, &residual_arr);
445 for (PetscInt i = i_start; i < i_end; ++i) {
446 const PetscScalar weight =
static_cast<PetscScalar
>(i + 1);
447 weighted_checksum_local += weight * residual_arr[i - i_start];
449 VecRestoreArrayRead(residual, &residual_arr);
451 PetscScalar weighted_checksum = 0.0;
452 MPI_Allreduce(&weighted_checksum_local, &weighted_checksum, 1, MPIU_SCALAR, MPI_SUM, PETSC_COMM_WORLD);
455 PetscPrintf(PETSC_COMM_SELF,
456 "%s: linear-system probe %d ||Ax-b||_2=%g, ||Ax-b||_inf=%g, sum(Ax-b)=%g, x^T(Ax-b)=%g, weighted_checksum=%g\n",
457 stage, probe_id + 1,
double(residual_norm2),
double(residual_norminf),
458 double(PetscRealPart(residual_sum)),
double(PetscRealPart(xTResidual)),
459 double(PetscRealPart(weighted_checksum)));
465 VecDestroy(&residual);
480 MaterialType *m = mtype;
483 m->regions.resize(param_globals::num_gregions);
485 const char* grid_name =
"emi_grid_domain";
486 log_msg(logger, 0, 0,
"Setting up %s tissue poperties for %d regions ..", grid_name,
487 param_globals::num_gregions);
490 RegionSpecs* reg = m->regions.data();
496 for (
size_t i=0; i<m->regions.size(); i++)
498 for (
int j=0;j<param_globals::gregion[i].num_IDs;j++)
500 int tag = param_globals::gregion[i].ID[j];
503 if(extra_tags_default.
find(tag) != extra_tags_default.
end())
504 extra_tags_default.
erase(tag);
506 if(intra_tags_default.
find(tag) != intra_tags_default.
end())
507 intra_tags_default.
erase(tag);
511 for (
size_t i=0; i<m->regions.size(); i++, reg++)
513 if(!strcmp(param_globals::gregion[i].name,
"")) {
514 snprintf(buf,
sizeof buf,
", gregion_%d",
int(i));
515 param_globals::gregion[i].name =
dupstr(buf);
519 reg->regname = strdup(param_globals::gregion[i].name);
523 reg->nsubregs = extra_tags_default.
size();
525 reg->nsubregs = intra_tags_default.
size();
527 reg->nsubregs = param_globals::gregion[i].num_IDs;
530 reg->subregtags = NULL;
533 reg->subregtags =
new int[reg->nsubregs];
537 for (
int tag : extra_tags_default) {
538 reg->subregtags[j] = tag;
544 for (
int tag : intra_tags_default) {
545 reg->subregtags[j] = tag;
550 for (
int j=0;j<reg->nsubregs;j++)
551 reg->subregtags[j] = param_globals::gregion[i].ID[j];
556 elecMaterial *emat =
new elecMaterial();
560 emat->InVal[0] = param_globals::gregion[i].g_bath;
561 emat->InVal[1] = param_globals::gregion[i].g_bath;
562 emat->InVal[2] = param_globals::gregion[i].g_bath;
564 emat->ExVal[0] = param_globals::gregion[i].g_bath;
565 emat->ExVal[1] = param_globals::gregion[i].g_bath;
566 emat->ExVal[2] = param_globals::gregion[i].g_bath;
568 emat->BathVal[0] = param_globals::gregion[i].g_bath;
569 emat->BathVal[1] = param_globals::gregion[i].g_bath;
570 emat->BathVal[2] = param_globals::gregion[i].g_bath;
573 for (
int j=0; j<3; j++) {
574 emat->InVal[j] *= 1e-3 * param_globals::gregion[i].g_mult;
575 emat->ExVal[j] *= 1e-3 * param_globals::gregion[i].g_mult;
576 emat->BathVal[j] *= 1e-3 * param_globals::gregion[i].g_mult;
578 reg->material = emat;
581 if (strlen(param_globals::gi_scale_vec))
585 void parabolic_solver_emi::init()
590 const int log_flag = param_globals::output_level > 1 ?
ECHO : 0;
591 const auto log_init_timing = [&](
const char* label,
double start) {
592 log_msg(NULL, 0, log_flag,
"EMI solver init: %s in %.5f seconds.", label,
float(MPI_Wtime() - start));
595 double phase_t = MPI_Wtime();
596 stats.init_logger(
"par_stats.dat");
600 log_init_timing(
"linear solver object", phase_t);
611 phase_t = MPI_Wtime();
613 log_init_timing(
"maximum nodal edge counts", phase_t);
616 MPI_Comm_rank(emi_surfmesh_w_counter_face.comm, &rank);
626 phase_t = MPI_Wtime();
633 SF::init_vector(&vb_both_face, emi_surfmesh_w_counter_face, dpn, alg_surface_type);
634 SF::init_vector(&vb_unique_face, emi_surfmesh_unique_face, dpn, alg_surface_type);
635 SF::init_vector(&Ib_both_face, emi_surfmesh_w_counter_face, dpn, alg_surface_type);
636 SF::init_vector(&Ib_unique_face, emi_surfmesh_unique_face, dpn, alg_surface_type);
637 log_init_timing(
"vectors", phase_t);
643 const bool use_petsc_exact_preallocation = param_globals::flavor == std::string(
"petsc");
644 const int petsc_initial_prealloc = 1;
648 mesh_int_t M = emi_surfmesh_w_counter_face.g_numelem;
650 mesh_int_t m = emi_surfmesh_w_counter_face.l_numelem;
651 mesh_int_t m_one_side = emi_surfmesh_one_side.l_numelem;
652 mesh_int_t M_one_side = emi_surfmesh_one_side.g_numelem;
653 mesh_int_t m_unique_face = emi_surfmesh_unique_face.l_numelem;
654 mesh_int_t M_unique_face = emi_surfmesh_unique_face.g_numelem;
657 if (param_globals::output_level > 1) {
658 log_msg(NULL, 0, 0,
"\n**********************************");
659 log_msg(NULL, 0, 0,
"#elements of emi surfmesh unique face: %zu", emi_surfmesh_unique_face.g_numelem);
660 log_msg(NULL, 0, 0,
"#elements of emi surfmesh one side: %zu", emi_surfmesh_one_side.g_numelem);
661 log_msg(NULL, 0, 0,
"#elements of emi surfmesh: %zu", emi_surfmesh_w_counter_face.g_numelem);
662 log_msg(NULL, 0, 0,
"#elements of emi mesh: %zu", emi_mesh.g_numelem);
663 log_msg(NULL, 0, 0,
"#dofs for emi_mesh: %zu", emi_mesh.g_numpts);
664 log_msg(NULL, 0, 0,
"#max_row_entries_emi: %zu", max_row_entries_emi);
665 log_msg(NULL, 0, 0,
"**********************************\n");
666 log_mesh_local_element_ranges(emi_mesh, emi_surfmesh_w_counter_face, emi_surfmesh_unique_face);
670 SF::layout_from_count<long int>(emi_surfmesh_w_counter_face.l_numelem, layout, emi_surfmesh_w_counter_face.comm);
672 mesh_int_t n_l = emi_mesh.pl.algebraic_layout()[rank];
676 SF::layout_from_count<long int>(emi_surfmesh_one_side.l_numelem, layout_one_side, emi_surfmesh_one_side.comm);
677 mesh_int_t m_one_side_l = layout_one_side[rank];
680 SF::layout_from_count<long int>(emi_surfmesh_unique_face.l_numelem, layout_unique_face, emi_surfmesh_unique_face.comm);
681 mesh_int_t m_unique_face_l = layout_unique_face[rank];
689 phase_t = MPI_Wtime();
691 B->init(M, N, m, n, m_l, use_petsc_exact_preallocation ? petsc_initial_prealloc : max_row_entries_emi*2);
694 Bi->init(M, N, m, n, m_l, use_petsc_exact_preallocation ? petsc_initial_prealloc : max_row_entries_emi*2);
697 BsM->init(N, M, n, m, n_l, use_petsc_exact_preallocation ? petsc_initial_prealloc : max_row_entries_emi*2);
699 log_init_timing(
"EMI coupling matrices", phase_t);
702 phase_t = MPI_Wtime();
703 SF::construct_direct_unique_both_operators(operator_unique_to_both_faces,
704 operator_both_to_unique_face,
705 map_elem_uniqueFace_to_elem_bothface,
706 map_elem_uniqueFace_to_elem_oneface,
707 vec_both_to_one_face,
708 emi_surfmesh_w_counter_face,
709 emi_surfmesh_unique_face,
713 log_init_timing(
"unique/both face transfer operators", phase_t);
716 phase_t = MPI_Wtime();
717 assemble_with_exact_preallocation({B, Bi, BsM}, [&]() {
718 SF::assemble_restrict_operator(*B, *Bi, *BsM, elemTag_surface_w_counter_mesh, map_vertex_tag_to_dof_petsc, line_face, tri_face, quad_face, emi_surfmesh_w_counter_face, emi_mesh,
UM2_to_CM2);
720 log_init_timing(
"restriction operators", phase_t);
725 phase_t = MPI_Wtime();
733 lhs_emi->init(emi_mesh, dpn, dpn, use_petsc_exact_preallocation ? petsc_initial_prealloc : max_row_entries_emi*3);
734 stiffness_emi->init(emi_mesh, dpn, dpn, use_petsc_exact_preallocation ? petsc_initial_prealloc : max_row_entries_emi*2);
735 mass_emi->init(emi_mesh, dpn, dpn, use_petsc_exact_preallocation ? petsc_initial_prealloc : max_row_entries_emi*3);
736 mass_surf_emi->init(emi_mesh, dpn, dpn, use_petsc_exact_preallocation ? petsc_initial_prealloc : max_row_entries_emi*3);
737 log_init_timing(
"system matrices", phase_t);
740 #ifdef EMI_DEBUG_MESH
743 MPI_Comm_rank(emi_mesh.comm, &local_rank);
745 fprintf(stderr,
"RANK %d MESH SIZES: one_side=%zu, counter=%zu, unique=%zu\n",
746 local_rank, emi_surfmesh_one_side.l_numelem,
747 emi_surfmesh_w_counter_face.l_numelem, emi_surfmesh_unique_face.l_numelem);
749 fprintf(stderr,
"RANK %d MAP SIZES: uniqueFace_to_oneface=%zu\n",
750 local_rank, map_elem_uniqueFace_to_elem_oneface.size());
755 decltype(map_elem_uniqueFace_to_elem_oneface)().swap(map_elem_uniqueFace_to_elem_oneface);
756 decltype(map_elem_uniqueFace_to_elem_bothface)().swap(map_elem_uniqueFace_to_elem_bothface);
762 phase_t = MPI_Wtime();
767 if(!(vb_ptr != NULL && Ib_ptr != NULL)) {
768 log_msg(0,5,0,
"%s error: global Vb and Ib vectors not properly set up! Ionics seem invalid! Aborting!",
774 vb->shallow_copy(*vb_ptr);
776 Ib->shallow_copy(*Ib_ptr);
778 parab_tech =
static_cast<parabolic_solver_emi::parabolic_t
>(param_globals::parab_solve_emi);
779 log_init_timing(
"ionic face vectors", phase_t);
782 log_msg(NULL, 0, log_flag,
"EMI solver init total in %.5f seconds.",
float(dur));
788 double start, end, period;
791 mass_integrator mass_integ;
792 mass_integrator mass_integ_emi;
795 int log_flag = param_globals::output_level > 1 ?
ECHO : 0;
796 MaterialType & mt = mtype[0];
798 const bool use_petsc_exact_preallocation = param_globals::flavor == std::string(
"petsc");
799 const bool reuse_petsc_fem_preallocation = use_petsc_exact_preallocation && fem_matrices_exact_preallocated;
814 log_msg(NULL, 0, 0,
"assemble stiffness matrix");
816 elec_stiffness_integrator stfn_integ_emi(mt);
817 auto assemble_stiffness_emi = [&]() {
818 stiffness_emi->zero();
821 if(reuse_petsc_fem_preallocation) {
822 assemble_stiffness_emi();
824 assemble_with_exact_preallocation({stiffness_emi}, assemble_stiffness_emi);
827 log_msg(logger,0,log_flag,
"Computed parabolic stiffness matrix in %.5f seconds.",
float(dur));
829 log_msg(NULL, 0, 0,
"assemble mass matrix on the volumetric mesh");
831 mass_integrator mass_integ;
832 auto assemble_mass_emi = [&]() {
836 auto* petsc_mass_emi = use_petsc_exact_preallocation ?
dynamic_cast<SF::petsc_matrix*
>(mass_emi) :
nullptr;
837 auto* petsc_stiffness_emi = use_petsc_exact_preallocation ?
dynamic_cast<SF::petsc_matrix*
>(stiffness_emi) :
nullptr;
838 if(reuse_petsc_fem_preallocation) {
840 }
else if(petsc_mass_emi !=
nullptr && petsc_stiffness_emi !=
nullptr) {
842 petsc_mass_emi->duplicate_pattern(*petsc_stiffness_emi);
845 assemble_with_exact_preallocation({mass_emi}, assemble_mass_emi);
848 log_msg(logger,0,log_flag,
"Computed volumetric mass matrix in %.5f seconds.",
float(dur));
850 log_msg(NULL, 0, 0,
"assemble LHS matrix and mass matrix on the surface mesh");
852 auto assemble_lhs_and_surface_mass = [&]() {
854 mass_surf_emi->zero();
856 SF::assemble_lhs_emi(*lhs_emi, *mass_surf_emi, mesh, emi_surfmesh_w_counter_face, map_vertex_tag_to_dof_petsc, line_face, tri_face, quad_face, stfn_integ_emi, mass_integ_emi, -1.,
UM2_to_CM2 / Dt);
858 if(reuse_petsc_fem_preallocation) {
859 assemble_lhs_and_surface_mass();
861 assemble_with_exact_preallocation({lhs_emi, mass_surf_emi}, assemble_lhs_and_surface_mass);
864 log_msg(logger,0,log_flag,
"Computed parabolic mass matrix in %.5f seconds.",
float(dur));
868 bool same_nonzero =
false;
872 log_msg(logger,0,log_flag,
"lhs matrix enforcing Dirichlet boundaries.");
876 dbc =
new dbc_manager(*lhs_emi, stimuli);
878 dbc->recompute_dbcs();
880 dbc->enforce_dbc_lhs();
882 log_msg(logger,0,log_flag,
"lhs matrix Dirichlet enforcing done in %.5f seconds.",
float(dur));
885 log_msg(logger,0,
ECHO,
"without enforcing Dirichlet boundaries on the lhs matrix!");
887 phie_mat_has_nullspace =
true;
891 log_emi_petsc_matrix_preallocation_report({
895 {
"unique_to_both:", operator_unique_to_both_faces},
896 {
"both_to_unique:", operator_both_to_unique_face},
897 {
"stiffness_emi:", stiffness_emi},
898 {
"mass_emi:", mass_emi},
899 {
"mass_surf_emi:", mass_surf_emi},
900 {
"lhs_emi:", lhs_emi},
902 if(use_petsc_exact_preallocation) fem_matrices_exact_preallocated =
true;
904 setup_linear_solver(logger);
906 log_msg(logger,0,log_flag,
"Initializing parabolic solver in %.5f seconds.",
float(dur));
909 period =
timing(end, start);
912 void parabolic_solver_emi::setup_linear_solver(
FILE_SPEC logger)
914 tol = param_globals::cg_tol_parab;
915 max_it = param_globals::cg_maxit_parab;
917 std::string default_opts;
918 std::string solver_file;
919 solver_file = param_globals::parab_options_file;
920 if (param_globals::flavor == std::string(
"ginkgo")) {
921 default_opts = std::string(
924 "type": "solver::Cg",
926 "type": "solver::Multigrid",
927 "min_coarse_rows": 8,
929 "default_initial_guess": "zero",
932 "type": "multigrid::Pgm",
933 "deterministic": false
937 "type": "preconditioner::Schwarz",
939 "type": "preconditioner::Jacobi"
955 "type": "ResidualNorm",
956 "reduction_factor": 1e-4
961 } else if (param_globals::flavor == std::string(
"petsc")) {
962 default_opts = std::string(
"-ksp_type cg -pc_type gamg -options_left");
964 lin_solver->setup_solver(*lhs_emi, tol, max_it * 100, param_globals::cg_norm_parab,
965 "parabolic PDE", phie_mat_has_nullspace, logger, solver_file.c_str(),
966 default_opts.c_str());
969 void parabolic_solver_emi::solve()
971 switch (parab_tech) {
972 case SEMI_IMPLICIT: solve_semiImplicit();
break;
976 void parabolic_solver_emi::solve_semiImplicit()
983 dbc->enforce_dbc_rhs(*ui);
991 stiffness_emi->mult(*ui, *Iij_temp);
998 if (Iij_stim->mag() > 0.0) {
1000 mass_emi->mult(*Iij_stim, *Iij_temp);
1012 (*lin_solver)(*dui, *Irhs);
1016 if(lin_solver->reason < 0) {
1017 log_msg(0, 5, 0,
"%s solver diverged. Reason: %s.", lin_solver->name.c_str(),
1018 petsc_get_converged_reason_str(lin_solver->reason));
1024 ui_pre->add_scaled(*dui, 1.0);
1026 ui->add_scaled(*ui_pre, 1.0);
1033 dbc->enforce_dbc_rhs(*ui);
1038 stats.slvtime +=
timing(t1, t0);
1039 stats.update_iter(lin_solver->niter);
1052 hashmap::unordered_map<std::pair<mesh_int_t,mesh_int_t>, std::pair<mesh_int_t,mesh_int_t>> & map_vertex_tag_to_dof_petsc,
1053 std::vector<std::string> & tags_data)
1062 for(
size_t i=0; i<rnod.
size(); i++){
1066 for(
size_t eidx=0; eidx<emi_surfmesh_w_counter_face.l_numelem; eidx++)
1068 std::vector<mesh_int_t> elem_nodes;
1069 mesh_int_t tag = emi_surfmesh_w_counter_face.tag[eidx];
1070 for (
int n = emi_surfmesh_w_counter_face.dsp[eidx]; n < emi_surfmesh_w_counter_face.dsp[eidx+1];n++)
1072 mesh_int_t l_idx = emi_surfmesh_w_counter_face.con[n];
1074 std::pair <mesh_int_t,mesh_int_t> Index_tag_old;
1075 Index_tag_old = std::make_pair(l2g[l_idx],tags[eidx]);
1076 mesh_int_t dof = map_vertex_tag_to_dof[Index_tag_old];
1077 elem_nodes.push_back(dof);
1082 std::string result_first;
1083 std::string result_second;
1084 std::sort(elem_nodes.begin(),elem_nodes.end());
1087 if(elem_nodes.size()==2){
1090 key.
v1 = elem_nodes[0];
1091 key.
v2 = elem_nodes[1];
1092 std::pair<SF::emi_face<mesh_int_t,SF::tuple<mesh_int_t>>,
1093 SF::emi_face<mesh_int_t,SF::tuple<mesh_int_t>>> value = line_face[key];
1095 tag_first = value.first.tag;
1096 tag_second = value.second.tag;
1097 result_first = std::to_string(tag_first) +
":" + std::to_string(tag_second);
1098 result_second = std::to_string(tag_second) +
":" + std::to_string(tag_first);
1100 else if(elem_nodes.size()==3){
1102 key.
v1 = elem_nodes[0];
1103 key.
v2 = elem_nodes[1];
1104 key.
v3 = elem_nodes[2];
1105 std::pair<SF::emi_face<mesh_int_t,SF::triple<mesh_int_t>>,
1106 SF::emi_face<mesh_int_t,SF::triple<mesh_int_t>>> value = tri_face[key];
1108 tag_first = value.first.tag;
1109 tag_second = value.second.tag;
1110 result_first = std::to_string(tag_first) +
":" + std::to_string(tag_second);
1111 result_second = std::to_string(tag_second) +
":" + std::to_string(tag_first);
1113 else if(elem_nodes.size()==4){
1115 key.
v1 = elem_nodes[0];
1116 key.
v2 = elem_nodes[1];
1117 key.
v3 = elem_nodes[2];
1118 key.
v4 = elem_nodes[3];
1119 std::pair<SF::emi_face<mesh_int_t,SF::quadruple<mesh_int_t>>,
1120 SF::emi_face<mesh_int_t,SF::quadruple<mesh_int_t>>> value = quad_face[key];
1122 tag_first = value.first.tag;
1123 tag_second = value.second.tag;
1124 result_first = std::to_string(tag_first) +
":" + std::to_string(tag_second);
1125 result_second = std::to_string(tag_second) +
":" + std::to_string(tag_first);
1128 tags_data.push_back(result_first);
1129 tags_data.push_back(result_second);
1133 void EMI::initialize()
1141 logger =
f_open(
"emi.log", param_globals::experiment != 4 ?
"w" :
"r");
1142 const int verb = param_globals::output_level;
1143 const auto log_init_timing = [&](
const char* label,
double start) {
1145 log_msg(logger, 0,
ECHO,
"EMI init: %s in %.5f seconds.", label,
float(MPI_Wtime() - start));
1148 double phase_t = MPI_Wtime();
1154 log_init_timing(
"mesh setup", phase_t);
1158 phase_t = MPI_Wtime();
1160 log_init_timing(
"mesh mappings", phase_t);
1164 phase_t = MPI_Wtime();
1165 ion.logger = logger;
1167 ion.set_surface_mesh_data(parab_solver.line_face,
1168 parab_solver.tri_face,
1169 parab_solver.quad_face,
1170 parab_solver.map_vertex_tag_to_dof);
1173 std::vector<std::string> tags_data;
1174 tags_onFace(parab_solver.line_face,
1175 parab_solver.tri_face,
1176 parab_solver.quad_face,
1177 parab_solver.map_vertex_tag_to_dof,
1178 parab_solver.map_vertex_tag_to_dof_petsc,
1181 log_init_timing(
"ionic face metadata", phase_t);
1183 ion.set_tags_onFace(tags_data);
1185 ion.set_face_region_data(parab_solver.intra_tags, parab_solver.map_elem_uniqueFace_to_tags);
1186 phase_t = MPI_Wtime();
1188 log_init_timing(
"ionic model initialization", phase_t);
1190 phase_t = MPI_Wtime();
1192 set_elec_tissue_properties_emi_volume(mtype_vol, parab_solver.extra_tags, parab_solver.intra_tags, logger);
1196 region_mask(
emi_msh, mtype_vol[0].regions, mtype_vol[0].regionIDs,
true,
"gregion_vol",
false);
1201 param_globals::dt, 0,
"elec::ref_dt",
"TS");
1202 log_init_timing(
"tissue properties and timers", phase_t);
1206 phase_t = MPI_Wtime();
1207 param_globals::operator_splitting = 0;
1209 log_init_timing(
"stimuli", phase_t);
1215 phase_t = MPI_Wtime();
1217 log_init_timing(
"solver setup", phase_t);
1220 phase_t = MPI_Wtime();
1222 balance_electrodes();
1224 scale_total_stimulus_current(stimuli, *parab_solver.mass_emi, *parab_solver.mass_surf_emi, logger);
1225 log_init_timing(
"stimulus current scaling", phase_t);
1230 phase_t = MPI_Wtime();
1236 parab_solver.operator_unique_to_both_faces->mult(*parab_solver.vb, *parab_solver.vb_both_face);
1237 SF::assign_resting_potential_from_ionic_models_on_myocyte(*parab_solver.ui,
1238 parab_solver.vb_both_face,
1239 parab_solver.elemTag_emi_mesh,
1240 parab_solver.map_vertex_tag_to_dof_petsc,
1241 parab_solver.line_face, parab_solver.tri_face, parab_solver.quad_face,
1242 emi_surfmesh_w_counter_face, emi_mesh);
1244 *parab_solver.vb_unique_face = *parab_solver.vb;
1245 log_init_timing(
"initial membrane state projection", phase_t);
1248 phase_t = MPI_Wtime();
1252 log_init_timing(
"output setup", phase_t);
1255 const double init_dur =
timing(t2, t1);
1256 this->initialize_time += init_dur;
1258 log_msg(logger, 0,
ECHO,
"EMI init total in %.5f seconds.",
float(init_dur));
1261 void EMI::setup_mappings()
1269 log_msg(logger, 0, 0,
"%s: Setting up intracellular algebraic-to-nodal scattering.", __func__);
1274 log_msg(logger, 0, 0,
"%s: Setting up intracellular PETSc to canonical permutation.", __func__);
1279 void EMI::compute_step()
1288 const int verb = param_globals::output_level;
1295 apply_dbc_stimulus();
1304 apply_current_stimulus();
1308 parab_solver.operator_unique_to_both_faces->mult(*parab_solver.Ib, *parab_solver.Ib_both_face);
1309 parab_solver.BsM->mult(*parab_solver.Ib_both_face, *parab_solver.Irhs);
1313 parab_solver.solve();
1316 parab_solver.B->mult(*parab_solver.ui, *parab_solver.vb_both_face);
1318 parab_solver.operator_both_to_unique_face->mult(*parab_solver.vb_both_face, *parab_solver.vb_unique_face);
1319 *parab_solver.vb = *parab_solver.vb_unique_face;
1327 this->compute_time +=
timing(t2, t1);
1334 void EMI::output_step()
1339 output_manager.write_data();
1341 double curtime =
timing(t2, t1);
1342 this->output_time += curtime;
1345 IO_stats.tot_time += curtime;
1361 output_manager.close_files_and_cleanup();
1367 void EMI::setup_stimuli()
1372 stimuli.
resize(param_globals::num_stim);
1373 for (
int i = 0; i < param_globals::num_stim; i++) {
1375 stimulus & s = stimuli[i];
1381 s.associated_intra_mesh =
emi_msh, s.associated_extra_mesh =
emi_msh;
1385 if (s.phys.type ==
Illum) {
1403 }
else if (s.phys.type ==
I_tm) {
1405 SF::restrict_to_membrane(s.electrode.vertices, dof2ptsData, mesh);
1417 if (s.electrode.dump_vtx) {
1422 if(param_globals::stim[i].pulse.dumpTrace &&
get_rank() == 0) {
1424 s.pulse.wave.write_trace(s.name+
".trc");
1430 void EMI::apply_dbc_stimulus()
1432 parabolic_solver_emi& ps = parab_solver;
1436 bool dbcs_have_updated = ps.dbc !=
nullptr && ps.dbc->dbc_update();
1439 if (dbcs_have_updated && time_not_final) {
1440 parab_solver.rebuild_matrices(mtype_vol, *ion.miif, stimuli, logger);
1444 void EMI::apply_current_stimulus()
1446 parabolic_solver_emi& ps = parab_solver;
1447 ps.Iij_stim->set(0.0);
1450 for(stimulus & s : stimuli) {
1452 switch (s.phys.type) {
1455 ps.Bi->mult(*ps.Iij_temp, *ps.Ib_both_face);
1456 ps.operator_both_to_unique_face->mult(*ps.Ib_both_face, *ps.Ib_unique_face);
1457 ps.Ib->add_scaled(*ps.Ib_unique_face, -0.5);
1471 void EMI::balance_electrodes()
1473 for (
int i = 0; i < param_globals::num_stim; i++) {
1474 if (param_globals::stim[i].crct.balance != -1) {
1475 int from = param_globals::stim[i].crct.balance;
1478 log_msg(NULL, 0, 0,
"Balancing stimulus %d with %d %s-wise.", from, to,
1479 is_current(stimuli[from].phys.type) ?
"current" :
"voltage");
1481 stimulus& s_from = stimuli[from];
1482 stimulus& s_to = stimuli[to];
1484 s_to.pulse = s_from.pulse;
1485 s_to.ptcl = s_from.ptcl;
1486 s_to.phys = s_from.phys;
1487 s_to.pulse.strength *= -1.0;
1489 if (s_from.phys.type ==
I_ex || s_from.phys.type ==
I_in) {
1493 if (!s_from.phys.total_current) {
1494 sf_mat& mass = *parab_solver.mass_emi;
1498 s_to.pulse.strength *= fabs(vol0 / vol1);
1510 for (stimulus & s : stimuli){
1511 if(
is_current(s.phys.type) && s.phys.total_current){
1512 switch (s.phys.type) {
1523 float scale = 1.e12 / vol;
1525 s.pulse.strength *= scale;
1528 "%s [Stimulus %d]: current density scaled to %.4g uA/cm^3\n",
1529 s.name.c_str(), s.idx, s.pulse.strength);
1543 s.pulse.strength /= surf;
1545 "%s [Stimulus %d]: current density scaled to %.4g uA/cm^2\n",
1546 s.name.c_str(), s.idx, s.pulse.strength);
1557 static void assign_deterministic_elem_numbering(
sf_mesh & mesh)
1559 const int KEY_SIZE = 6;
1560 int rank = 0, size = 0;
1561 MPI_Comm_rank(mesh.comm, &rank);
1562 MPI_Comm_size(mesh.comm, &size);
1564 auto make_key = [&](
size_t i) {
1565 std::array<mesh_int_t, KEY_SIZE> k;
1567 k[0] =
static_cast<mesh_int_t>(mesh.type[i]);
1571 if (mesh.type[i] ==
SF::Line) nn = 2;
1572 else if (mesh.type[i] ==
SF::Tri) nn = 3;
1573 else if (mesh.type[i] ==
SF::Quad) nn = 4;
1576 std::vector<mesh_int_t> nodes;
1578 size_t off = mesh.dsp[i];
1579 for (
int j = 0; j < nn; j++) {
1580 nodes.push_back(mesh.con[off + j]);
1582 std::sort(nodes.begin(), nodes.end());
1583 for (
int j = 0; j < (int)nodes.size(); j++) {
1584 k[2 + j] = nodes[j];
1590 std::vector<mesh_int_t> local_keys(mesh.l_numelem * KEY_SIZE, -1);
1591 for (
size_t i = 0; i < mesh.l_numelem; i++) {
1592 auto k = make_key(i);
1593 for (
int j = 0; j < KEY_SIZE; j++) local_keys[i * KEY_SIZE + j] = k[j];
1597 std::vector<int> counts(size, 0), displs(size, 0);
1598 int local_count = (int)local_keys.size();
1599 MPI_Allgather(&local_count, 1, MPI_INT, counts.data(), 1, MPI_INT, mesh.comm);
1601 for (
int r = 0; r < size; r++) {
1606 std::vector<mesh_int_t> all_keys;
1607 if (rank == 0) all_keys.resize(total, -1);
1608 const MPI_Datatype key_mpi_t = mpi_datatype<mesh_int_t>();
1609 MPI_Gatherv(local_keys.data(), local_count, key_mpi_t,
1610 rank == 0 ? all_keys.data() :
nullptr, counts.data(), displs.data(), key_mpi_t,
1614 std::vector<std::array<mesh_int_t, KEY_SIZE>> sorted_keys;
1616 const int nkeys = total / KEY_SIZE;
1617 sorted_keys.resize(nkeys);
1618 for (
int i = 0; i < nkeys; i++) {
1619 std::array<mesh_int_t, KEY_SIZE> k;
1620 for (
int j = 0; j < KEY_SIZE; j++) k[j] = all_keys[i * KEY_SIZE + j];
1623 std::sort(sorted_keys.begin(), sorted_keys.end());
1628 if (rank == 0) nkeys = (int)sorted_keys.size();
1629 MPI_Bcast(&nkeys, 1, MPI_INT, 0, mesh.comm);
1630 std::vector<mesh_int_t> flat_sorted(nkeys * KEY_SIZE, -1);
1632 for (
int i = 0; i < nkeys; i++) {
1633 for (
int j = 0; j < KEY_SIZE; j++) flat_sorted[i * KEY_SIZE + j] = sorted_keys[i][j];
1636 MPI_Bcast(flat_sorted.data(), (
int)flat_sorted.size(), key_mpi_t, 0, mesh.comm);
1640 sorted_keys.resize(nkeys);
1641 for (
int i = 0; i < nkeys; i++) {
1642 std::array<mesh_int_t, KEY_SIZE> k;
1643 for (
int j = 0; j < KEY_SIZE; j++) k[j] = flat_sorted[i * KEY_SIZE + j];
1651 nbr_ref.
resize(mesh.l_numelem);
1652 nbr_sub.
resize(mesh.l_numelem);
1653 for (
size_t i = 0; i < mesh.l_numelem; i++) {
1654 auto k = make_key(i);
1655 auto it = std::lower_bound(sorted_keys.begin(), sorted_keys.end(), k);
1656 if (it == sorted_keys.end() || *it != k) {
1657 log_msg(0, 5, 0,
"deterministic numbering failed to find key (rank %d, elem %zu)", rank, i);
1666 void EMI::setup_output()
1668 std::string output_base =
get_basename(param_globals::meshname);
1671 const bool write_binary =
1672 SF::fileExists(std::string(param_globals::meshname) +
".belem") ||
1677 const int gridout_emi = param_globals::gridout_emi;
1681 if(gridout_emi & 2) {
1682 std::string output_file = output_base +
"_e";
1683 log_msg(0, 0, 0,
"Writing \"%s\" mesh: %s (%s)", mesh.name.c_str(), output_file.c_str(), write_binary ?
"binary" :
"text");
1684 const double t0 = MPI_Wtime();
1686 log_msg(0, 0, 0,
"Wrote \"%s\" mesh in %.5f seconds.", mesh.name.c_str(),
float(MPI_Wtime() - t0));
1688 else if(param_globals::output_level > 1) {
1689 log_msg(0, 0, 0,
"Skipping \"%s\" mesh output.", mesh.name.c_str());
1692 output_manager.register_output(parab_solver.ui,
emi_msh, 1, param_globals::phiefile,
"mV", NULL);
1695 mesh_m.name =
"Membrane";
1696 if(gridout_emi & 1) {
1697 std::string output_file = output_base +
"_m";
1698 log_msg(0, 0, 0,
"Writing \"%s\" mesh: %s (%s)", mesh_m.name.c_str(), output_file.c_str(), write_binary ?
"binary" :
"text");
1699 const double t0 = MPI_Wtime();
1701 log_msg(0, 0, 0,
"Wrote \"%s\" mesh in %.5f seconds.", mesh_m.name.c_str(),
float(MPI_Wtime() - t0));
1703 else if(param_globals::output_level > 1) {
1704 log_msg(0, 0, 0,
"Skipping \"%s\" mesh output.", mesh_m.name.c_str());
1711 output_manager.register_output(parab_solver.vb_unique_face,
emi_surface_unique_face_msh, 1, param_globals::vofile,
"mV", NULL,
true);
1713 if(param_globals::num_trace) {
1715 open_trace(ion.miif, param_globals::num_trace, param_globals::trace_node, NULL, &imesh);
1719 IO_stats.init_logger(
"IO_stats.dat");
1722 void EMI::dump_matrices()
1724 std::string bsname = param_globals::dump_basename;
1729 fn = bsname +
"_lhs.bin";
1730 parab_solver.lhs_emi->write(fn.c_str());
1732 fn = bsname +
"_K.bin";
1733 parab_solver.stiffness_emi->write(fn.c_str());
1735 fn = bsname +
"_B.bin";
1736 parab_solver.B->write(fn.c_str());
1738 fn = bsname +
"_Bi.bin";
1739 parab_solver.Bi->write(fn.c_str());
1741 fn = bsname +
"_BsM.bin";
1742 parab_solver.BsM->write(fn.c_str());
1744 fn = bsname +
"_M.bin";
1745 parab_solver.mass_emi->write(fn.c_str());
1747 fn = bsname +
"_Ms.bin";
1748 parab_solver.mass_surf_emi->write(fn.c_str());
1754 double EMI::timer_val(
const int timer_id)
1760 stimuli[sidx].value(val);
1763 val = std::nan(
"NaN");
1770 std::string EMI::timer_unit(
const int timer_id)
1777 s_unit = stimuli[sidx].pulse.wave.f_unit;
1782 void EMI::setup_solvers()
1785 const int log_flag = param_globals::output_level > 1 ?
ECHO : 0;
1786 double t0 = MPI_Wtime();
1787 parab_solver.init();
1788 log_msg(logger, 0, log_flag,
"EMI setup_solvers: parabolic solver init in %.5f seconds.",
float(MPI_Wtime() - t0));
1790 parab_solver.rebuild_matrices(mtype_vol, *ion.miif, stimuli, logger);
1791 log_msg(logger, 0, log_flag,
"EMI setup_solvers: matrix assembly and linear solver setup in %.5f seconds.",
float(MPI_Wtime() - t0));
1793 if(param_globals::dump2MatLab)
1801 MPI_Comm_size(comm, &size);
1802 MPI_Comm_rank(comm, &rank);
1815 MPI_Comm_size(comm, &size); MPI_Comm_rank(comm, &rank);
1816 divide(num_tags, size, num_tags_per_rank);
1819 void EMI::setup_EMI_mesh()
1821 log_msg(0,0,0,
"\n *** Processing EMI mesh ***\n");
1823 const std::string basename = param_globals::meshname;
1824 const int verb = param_globals::output_level;
1826 assert(mesh_registry.count(
emi_msh) == 1);
1835 MPI_Comm comm = emi_mesh.
comm;
1838 double t1, t2, s1, s2;
1839 const double total_setup_t0 = MPI_Wtime();
1840 MPI_Comm_size(comm, &size); MPI_Comm_rank(comm, &rank);
1845 if (emi_mesh.l_numelem > 0) {
1850 const char* type_name = (first_elem ==
SF::Line) ?
"1D (Line)" :
1851 (first_elem ==
SF::Tri) ?
"2D (Tri)" :
1854 log_msg(0, 5, 0,
"\n*** ERROR: EMI model requires a 3D volumetric mesh!");
1855 log_msg(0, 5, 0,
"*** Current mesh element type: %s", type_name);
1856 log_msg(0, 5, 0,
"*** EMI only supports 3D element types: Tetra, Pyramid, Prism, Hexa");
1857 log_msg(0, 5, 0,
"*** Please provide a 3D mesh with volume elements.\n");
1866 int total_num_tags = 0;
1867 if(verb)
log_msg(NULL, 0, 0,
"\nReading tags for extra and intra regions from input files");
1873 if(verb)
log_msg(NULL, 0, 0,
"Read extracellular tags");
1876 parab_solver.extra_tags.insert(tag);
1879 if(verb)
log_msg(NULL, 0, 0,
"Read intracellular tags");
1882 parab_solver.intra_tags.insert(tag);
1885 total_num_tags = parab_solver.extra_tags.size() + parab_solver.intra_tags.size();
1886 if(total_num_tags < size){
1887 log_msg(0,5,0,
"\nThe number of unique tags on EMI mesh is smaller than number of processors!");
1890 if(verb)
log_msg(NULL, 0, 0,
"\nextra_tags=%lu, intra_tags=%lu", parab_solver.extra_tags.size(), parab_solver.intra_tags.size());
1893 if(verb)
log_msg(NULL, 0, 0,
"Done in %f sec.",
float(t2 - t1));
1901 if(verb)
log_msg(NULL, 0, 0,
"\nReading points with data on each vertex");
1905 assert(ptsidx.
size()==ptsData.
size());
1907 if(verb)
log_msg(NULL, 0, 0,
"Done in %f sec.",
float(t2 - t1));
1909 std::list< sf_mesh* > meshlist;
1910 meshlist.push_back(&emi_mesh);
1915 if(verb)
log_msg(NULL, 0, 0,
"\nDistribute mesh based on tags");
1918 distribute_elements_based_tags(emi_mesh, total_num_tags);
1920 if(verb)
log_msg(NULL, 0, 0,
"Done in %f sec.",
float(t2 - t1));
1926 if(verb)
log_msg(NULL, 0, 0,
"\nInserting points");
1930 if(verb)
log_msg(NULL, 0, 0,
"Done in %f sec.",
float(t2 - t1));
1935 if(verb)
log_msg(NULL, 0, 0,
"\nCompute location of all DOFs on EMI mesh (inner DOFs, membrane, gap junction) saved into ptsData from original mesh");
1937 compute_ptsdata_from_original_mesh( emi_mesh,
1940 parab_solver.extra_tags,
1941 parab_solver.intra_tags);
1943 if(verb)
log_msg(NULL, 0, 0,
"Done in %f sec.",
float(t2 - t1));
1951 if(verb)
log_msg(NULL, 0, 0,
"\nExtract EMI surface mesh");
1953 extract_face_based_tags(emi_mesh,
SF::NBR_REF, vertex2ptsdata,
1954 parab_solver.line_face,
1955 parab_solver.tri_face,
1956 parab_solver.quad_face,
1957 parab_solver.extra_tags,
1958 parab_solver.intra_tags,
1959 emi_surfmesh_one_side, emi_surfmesh_w_counter_face, emi_surfmesh_unique_face,
1960 parab_solver.map_elem_uniqueFace_to_elem_oneface,
1961 unused_map_elem_oneface_to_elem_uniqueFace);
1962 meshlist.push_back(&emi_surfmesh_one_side);
1964 if(verb)
log_msg(NULL, 0, 0,
"Done in %f sec.",
float(t2 - t1));
1968 compute_surface_mesh_with_counter_face(emi_surfmesh_w_counter_face,
SF::NBR_REF,
1969 parab_solver.line_face,
1970 parab_solver.tri_face,
1971 parab_solver.quad_face);
1973 compute_surface_mesh_with_unique_face(emi_surfmesh_unique_face,
SF::NBR_REF,
1974 parab_solver.line_face,
1975 parab_solver.tri_face,
1976 parab_solver.quad_face,
1977 parab_solver.map_elem_uniqueFace_to_tags);
1982 SF::create_reverse_elem_mapping_between_surface_meshes(parab_solver.line_face,
1983 parab_solver.tri_face,
1984 parab_solver.quad_face,
1985 parab_solver.vec_both_to_one_face,
1988 if(verb)
log_msg(NULL, 0, 0,
"Done in %f sec.",
float(t2 - t1));
1993 if(verb)
log_msg(NULL, 0, 0,
"\ncompute global number of interface");
1994 size_t global_count_surf = 0;
1995 size_t numelem_surface = emi_surfmesh_one_side.l_numelem;
1996 size_t local_count_surf = numelem_surface;
1997 MPI_Reduce(&local_count_surf, &global_count_surf, 1, mpi_datatype<size_t>(), MPI_SUM, 0, MPI_COMM_WORLD);
1998 if(verb && rank==0) fprintf(stdout,
"global number of interfaces = %zu\n", global_count_surf);
2005 sub_numbering(emi_mesh);
2006 emi_mesh.generate_par_layout();
2018 if(verb)
log_msg(NULL, 0, 0,
"\ndecouple emi interfaces");
2019 if(verb)
log_msg(NULL, 0, 0,
"\tcompute map oldIdx to dof");
2020 compute_map_vertex_to_dof(emi_mesh,
SF::NBR_REF, vertex2ptsdata, parab_solver.extra_tags, parab_solver.map_vertex_tag_to_dof);
2025 if(verb)
log_msg(NULL, 0, 0,
"\tcomplete map oldIdx to dof with counter interface");
2027 complete_map_vertex_to_dof_with_counter_face(parab_solver.line_face, parab_solver.tri_face, parab_solver.quad_face, parab_solver.map_vertex_tag_to_dof);
2032 if(verb)
log_msg(NULL, 0, 0,
"\tupdate mesh with dof");
2033 update_emi_mesh_with_dofs(emi_mesh,
SF::NBR_REF, parab_solver.map_vertex_tag_to_dof, parab_solver.dof2vertex);
2035 if(verb)
log_msg(NULL, 0, 0,
"Done in %f sec.",
float(t2 - t1));
2041 if(verb)
log_msg(NULL, 0, 0,
"\nInitialize petsc =0 for map<oldIdx,tag> -><dof, petsc>");
2043 for(
const auto & key_value : parab_solver.map_vertex_tag_to_dof)
2045 mesh_int_t gIndex_old = key_value.first.first;
2049 std::pair <mesh_int_t,mesh_int_t> dof_petsc = std::make_pair(dof,-1);
2050 parab_solver.map_vertex_tag_to_dof_petsc.insert({key_value.first,dof_petsc});
2053 if(verb)
log_msg(NULL, 0, 0,
"Done in %f sec.",
float(t2 - t1));
2059 if(verb)
log_msg(NULL, 0, 0,
"Inserting points and ptsData of dofs to emi_mesh");
2060 insert_points_ptsData_to_dof(tmesh_backup_old, emi_mesh,
SF::NBR_REF, parab_solver.dof2vertex, vertex2ptsdata, dof2ptsData);
2062 if(verb)
log_msg(NULL, 0, 0,
"Done in %f sec.",
float(t2 - t1));
2068 if(verb)
log_msg(NULL, 0, 0,
"Generating unique PETSc numberings");
2071 sub_numbering(emi_mesh);
2072 emi_mesh.generate_par_layout();
2075 if(verb)
log_msg(NULL, 0, 0,
"Done in %f sec.",
float(t2 - t1));
2081 if(verb)
log_msg(NULL, 0, 0,
"Generating unique PETSc numberings");
2084 petsc_numbering(emi_mesh);
2087 if(verb)
log_msg(NULL, 0, 0,
"Done in %f sec.",
float(t2 - t1));
2092 if(verb)
log_msg(NULL, 0, 0,
"Updating the map between indices to PETSc numberings");
2094 update_map_indices_to_petsc(emi_mesh,
SF::NBR_REF,
SF::NBR_PETSC, parab_solver.extra_tags, parab_solver.map_vertex_tag_to_dof_petsc, parab_solver.dof2vertex, parab_solver.elemTag_emi_mesh);
2096 if(verb)
log_msg(NULL, 0, 0,
"Done in %f sec.",
float(t2 - t1));
2102 if(verb)
log_msg(NULL, 0, 0,
"Updating surface mesh with dof");
2103 update_faces_on_surface_mesh_after_decoupling_with_dofs(emi_surfmesh_one_side, parab_solver.map_vertex_tag_to_dof, parab_solver.line_face, parab_solver.tri_face, parab_solver.quad_face);
2105 if(verb)
log_msg(NULL, 0, 0,
"Done in %f sec.",
float(t2 - t1));
2111 if(verb)
log_msg(NULL, 0, 0,
"Layout for element of EMI surfmesh");
2114 SF::layout_from_count<long int>(emi_surfmesh_one_side.l_numelem, layout, emi_surfmesh_one_side.comm);
2115 size_t count = layout[rank+1] - layout[rank];
2117 for (
int i = 0; i <
count; ++i){
2118 emi_surfmesh_elem[i] = layout[rank]+i;
2124 if(verb)
log_msg(NULL, 0, 0,
"Done in %f sec.",
float(t2 - t1));
2130 if(verb)
log_msg(NULL, 0, 0,
"Layout for element of EMI surfmesh w counter face");
2133 SF::layout_from_count<long int>(emi_surfmesh_w_counter_face.l_numelem, layout_counter, emi_surfmesh_w_counter_face.comm);
2134 size_t count_counter = layout_counter[rank+1] - layout_counter[rank];
2135 emi_surfmesh_counter_elem.
resize(count_counter);
2136 for (
int i = 0; i < count_counter; ++i){
2137 emi_surfmesh_counter_elem[i] = layout_counter[rank]+i;
2139 emi_surfmesh_w_counter_face.localize(
SF::NBR_REF);
2142 if(verb)
log_msg(NULL, 0, 0,
"Done in %f sec.",
float(t2 - t1));
2149 if(verb)
log_msg(NULL, 0, 0,
"Layout for element of EMI unique-face surfmesh");
2152 SF::layout_from_count<long int>(emi_surfmesh_unique_face.l_numelem, layout_unique, emi_surfmesh_unique_face.comm);
2153 size_t count_unique = layout_unique[rank+1] - layout_unique[rank];
2154 emi_surfmesh_unique_elem.
resize(count_unique);
2155 for (
int i = 0; i < count_unique; ++i){
2156 emi_surfmesh_unique_elem[i] = layout_unique[rank]+i;
2161 if(verb)
log_msg(NULL, 0, 0,
"Done in %f sec.",
float(t2 - t1));
2167 if(verb)
log_msg(NULL, 0, 0,
"Inserting points to EMI surfmesh");
2168 insert_points_to_surface_mesh(tmesh_backup_old, emi_surfmesh_one_side,
SF::NBR_REF, parab_solver.dof2vertex, parab_solver.extra_tags, parab_solver.elemTag_surface_mesh);
2169 insert_points_to_surface_mesh(tmesh_backup_old, emi_surfmesh_w_counter_face,
SF::NBR_REF, parab_solver.dof2vertex, parab_solver.extra_tags, parab_solver.elemTag_surface_w_counter_mesh);
2170 insert_points_to_surface_mesh(tmesh_backup_old, emi_surfmesh_unique_face,
SF::NBR_REF, parab_solver.dof2vertex);
2172 if(verb)
log_msg(NULL, 0, 0,
"Done in %f sec.",
float(t2 - t1));
2178 if(verb)
log_msg(NULL, 0, 0,
"Generating submesh_numbering and PETSc numberings for surface mesh");
2181 sub_numbering(emi_surfmesh_one_side);
2182 emi_surfmesh_one_side.generate_par_layout();
2185 petsc_numbering(emi_surfmesh_one_side);
2190 sub_numbering(emi_surfmesh_w_counter_face);
2191 emi_surfmesh_w_counter_face.generate_par_layout();
2194 petsc_numbering(emi_surfmesh_w_counter_face);
2199 sub_numbering(emi_surfmesh_unique_face);
2200 emi_surfmesh_unique_face.generate_par_layout();
2203 petsc_numbering(emi_surfmesh_unique_face);
2205 assign_deterministic_elem_numbering(emi_surfmesh_unique_face);
2208 if(verb)
log_msg(NULL, 0, 0,
"Done in %f sec.",
float(t2 - t1));
2214 if(verb)
log_msg(NULL, 0, 0,
"assign PETSc numbering for new faces");
2215 SF::assign_petsc_on_counter_face(parab_solver.map_vertex_tag_to_dof_petsc,comm);
2218 for (it = parab_solver.map_vertex_tag_to_dof_petsc.begin(); it != parab_solver.map_vertex_tag_to_dof_petsc.end(); it++)
2220 std::pair <mesh_int_t,mesh_int_t> Index_tag_old = it->first;
2221 std::pair <mesh_int_t,mesh_int_t> dof_petsc = it->second;
2222 parab_solver.dof2petsc[dof_petsc.first] = dof_petsc.second;
2223 parab_solver.petsc2dof[dof_petsc.second] = dof_petsc.first;
2228 #ifdef EMI_DEBUG_MESH
2230 int invalid_count = 0;
2231 for (
const auto& [key, val] : parab_solver.map_vertex_tag_to_dof_petsc) {
2232 if (val.second < 0) {
2234 if (invalid_count <= 3) {
2235 fprintf(stderr,
"RANK %d INVALID: vertex=%ld tag=%ld dof=%ld petsc=%ld\n",
2236 rank, (
long)key.first, (
long)key.second, (
long)val.first, (
long)val.second);
2240 fprintf(stderr,
"RANK %d: After Step 26: %d invalid PETSc indices out of %zu total\n",
2241 rank, invalid_count, parab_solver.map_vertex_tag_to_dof_petsc.size());
2247 if(verb)
log_msg(NULL, 0, 0,
"Done in %f sec.",
float(t2 - t1));
2252 added_counter_faces_to_map(parab_solver.line_face, parab_solver.tri_face, parab_solver.quad_face);
2253 const double total_setup = MPI_Wtime() - total_setup_t0;
2254 log_msg(0,0,0,
"Total setup_EMI_mesh processing time: %.5f sec.",
float(total_setup));
2256 log_msg(0,0,0,
"\n *** EMI mesh processing Done ***\n");
2262 MPI_Comm comm = mesh.
comm;
2264 double t1, t2, s1, s2;
2265 MPI_Comm_size(comm, &size); MPI_Comm_rank(comm, &rank);
2266 const int verb = param_globals::output_level;
2268 if(total_num_tags < size)
2270 PetscPrintf(PETSC_COMM_WORLD,
"\nThe number of processors should be less than the number of tags, size = %d & ntags = %d !!!\n",
2271 size, total_num_tags);
2275 if(verb==10)
log_msg(NULL, 0, 0,
"\ncompute the number of tags which belongs to one rank");
2279 compute_tags_per_rank(total_num_tags, ntags_per_rank);
2281 if(verb==10)
log_msg(NULL, 0, 0,
"Done in %f sec.",
float(t2 - t1));
2287 partition_based_tags(total_num_tags, mesh.
tag, ntags_per_rank, part_based_Tags);
2290 permute_mesh_locally_based_on_tag_elemIdx(mesh);
2292 if(verb==10)
log_msg(NULL, 0, 0,
"Done in %f sec.",
float(t2 - t1));
2297 void partition_based_tags(
int num_tags,
2302 const int verb = param_globals::output_level;
2305 MPI_Comm_size(comm, &size);
2306 MPI_Comm_rank(comm, &rank);
2312 if (!load_partitions_from_file(tags_to_rank_map, num_tags, comm)) {
2313 if(verb==10)
log_msg(NULL, 0, 0,
"\ncompute the number of unique tags");
2315 double t1 = MPI_Wtime();
2316 extract_unique_tag(unique_tags);
2317 double t2 = MPI_Wtime();
2318 if(verb==10)
log_msg(NULL, 0, 0,
"Done in %f sec.",
float(t2 - t1));
2320 if (unique_tags.
size() !=
static_cast<size_t>(num_tags)) {
2321 log_msg(0,5,0,
"\nerror: the number of tags in the EMI mesh does not match the total tags from *.extra and *.intra");
2324 map_tags_to_rank(size, unique_tags, num_tags_per_rank, tags_to_rank_map);
2327 for (
size_t i = 0; i < part.
size(); ++i) {
2328 if(tags_to_rank_map.
count(tag[i]))
2329 part[i] = tags_to_rank_map[tag[i]];
2337 for (
size_t r = 0; r < size; ++r) {
2338 for (
size_t count = 0;
count < num_tags_per_rank[r];) {
2342 tags_to_rank_map.
insert({tag, r});
2351 int expected_num_tags,
2355 MPI_Comm_size(comm, &size);
2356 MPI_Comm_rank(comm, &rank);
2359 const std::string basename = param_globals::meshname;
2360 FILE* fd = fopen((basename +
".part").c_str(),
"r");
2366 if (parts.
size() == 0)
return false;
2368 if (parts.
size() % 2 != 0) {
2369 log_msg(0,5,0,
"\nThe part file should contain 2 lines per tag, one for the tag number and the next for its associated partition number.!");
2375 for(
int i = 0; i < parts.
size(); i+=2) {
2376 min_part =
std::min(min_part, parts[i + 1]);
2377 max_part =
std::max(max_part, parts[i + 1]);
2378 tags_to_rank_map.
insert({parts[i], parts[i + 1]});
2381 if (tags_to_rank_map.
size() !=
static_cast<size_t>(expected_num_tags)) {
2382 log_msg(0,5,0,
"\nerror: the number of tags in the .part file does not match the total tags from *.extra and *.intra");
2386 if (min_part < 0 || max_part >= size) {
2388 "\nerror: EMI partition file %s.part is incompatible with this run.\n"
2389 "The file contains partition IDs in [%d, %d], but the current MPI communicator has %d rank(s).\n"
2390 "Remove/regenerate the .part file or run with a matching number of MPI tasks.",
2391 basename.c_str(), min_part, max_part, size);
2395 if (rank == 0 && max_part + 1 != size) {
2397 "Warning: EMI partition file %s.part uses %d partition ID(s), but the current run uses %d MPI rank(s).",
2398 basename.c_str(), max_part + 1, size);
opencarp::local_index_t mesh_int_t
#define SF_COMM
the default SlimFem MPI communicator
opencarp::real_t SF_real
Global scalar type.
#define CALI_CXX_MARK_FUNCTION
#define CALI_MARK_BEGIN(_str)
#define CALI_MARK_END(_str)
void globalize(SF_nbr nbr_type)
Localize the connectivity data w.r.t. a given numbering.
size_t l_numelem
local number of elements
void localize(SF_nbr nbr_type)
Localize the connectivity data w.r.t. a given numbering.
MPI_Comm comm
the parallel mesh is defined on a MPI world
vector< T > & get_numbering(SF_nbr nbr_type)
Get the vector defining a certain numbering.
vector< T > tag
element tag
Functor class generating a numbering optimized for PETSc.
Functor class applying a submesh renumbering.
A vector storing arbitrary data.
size_t size() const
The current size of the vector.
void resize(size_t n)
Resize a vector.
hm_int count(const K &key) const
Check if key exists.
void insert(InputIterator first, InputIterator last)
Insert Iterator range.
iterator find(const K &key)
hm_int erase(const K &key)
long d_time
current time instance index
double time_step
global reference time step
int add_eq_timer(double istart, double iend, int ntrig, double iintv, double idur, const char *iname, const char *poolname=nullptr)
Add a equidistant step timer to the array of timers.
long d_end
final index in multiples of dt
EMI model based on computed current on the faces, main EMI physics class.
void init_solver(SF::abstract_linear_solver< T, S > **sol)
void read_points(const std::string basename, const MPI_Comm comm, vector< S > &pts, vector< T > &ptsidx)
Read the points and insert them into a list of meshes.
void interval(vector< T > &vec, size_t start, size_t end)
Create an integer interval between start and end.
void make_global(const vector< T > &vec, vector< T > &out, MPI_Comm comm)
make a parallel vector global
void permute_mesh(const meshdata< T, S > &inmesh, meshdata< T, S > &outmesh, const vector< T > &perm)
Permute the element data of a mesh based on a given permutation.
T sum(const vector< T > &vec)
Compute sum of a vector's entries.
void unique_resize(vector< T > &_P)
void divide(const size_t gsize, const size_t num_parts, vector< T > &loc_sizes)
divide gsize into num_parts local parts with even distribution of the remainder
void count(const vector< T > &data, vector< S > &cnt)
Count number of occurrences of indices.
void insert_points(const vector< S > &pts, const vector< T > &ptsidx, std::list< meshdata< T, S > * > &meshlist)
Insert the points from the read-in buffers into a list of distributed meshes.
void assemble_matrix(abstract_matrix< T, S > &mat, meshdata< mesh_int_t, mesh_real_t > &domain, matrix_integrator< mesh_int_t, mesh_real_t > &integrator)
Generalized matrix assembly.
int max_nodal_edgecount(const meshdata< T, S > &mesh)
Compute the maximum number of node-to-node edges for a mesh.
void redistribute_elements(meshdata< T, S > &mesh, meshdata< T, S > &sendbuff, vector< T > &part)
Redistribute the element data of a parallel mesh among the ranks based on a partitioning.
void restrict_to_set(vector< T > &v, const hashmap::unordered_set< T > &set)
bool fileExists(std::string filename)
Function which checks if a given file exists.
void init_vector(SF::abstract_vector< T, S > **vec)
void binary_sort(vector< T > &_V)
void init_matrix(SF::abstract_matrix< T, S > **mat)
void write_mesh_parallel(const meshdata< T, S > &mesh, bool binary, std::string basename)
Write a parallel mesh to harddisk without gathering it on one rank.
void binary_sort_sort_copy(vector< T > &_V, vector< T > &_W, vector< S > &_A)
@ NBR_PETSC
PETSc numbering of nodes.
@ NBR_ELEM_REF
The element numbering of the reference mesh (the one stored on HD).
@ NBR_REF
The nodal numbering of the reference mesh (the one stored on HD).
@ NBR_SUBMESH
Submesh nodal numbering: The globally ascending sorted reference indices are reindexed.
@ NBR_ELEM_SUBMESH
Submesh element numbering: The globally ascending sorted reference indices are reindexed.
constexpr T min(T a, T b)
constexpr T max(T a, T b)
void dump_trace(MULTI_IF *MIIF, limpet::Real time)
void open_trace(MULTI_IF *MIIF, int n_traceNodes, int *traceNodes, int *label, opencarp::sf_mesh *imesh)
Set up ionic model traces at some global node numbers.
timer_manager * tm_manager
a manager for the various physics timers
bool using_legacy_stimuli
flag storing whether legacy stimuli are used
std::map< mesh_t, sf_mesh > mesh_reg
Registry for the different meshes used in a multi-physics simulation.
int stimidx_from_timeridx(const SF::vector< stimulus > &stimuli, const int timer_id)
determine link between timer and stimulus
sf_vec * get_data(datavec_t d)
Retrieve a petsc data vector from the data registry.
SF::scattering * get_scattering(const int from, const int to, const SF::SF_nbr nbr, const int dpn)
Get a scattering from the global scatter registry.
void set_cond_type(MaterialType &m, cond_t type)
void read_el_scale_vec(const char *file, mesh_t mt, SF::vector< double > &el_scale, int &el_scale_dpn)
SF_real get_volume_from_nodes(sf_mat &mass, SF::vector< mesh_int_t > &local_idx)
sf_mesh & get_mesh(const mesh_t gt)
Get a mesh by specifying the gridID.
SF::scattering * register_scattering(const int from, const int to, const SF::SF_nbr nbr, const int dpn)
Register a scattering between to grids, or between algebraic and nodal representation of data on the ...
cond_t
description of electrical tissue properties
SF::scattering * get_permutation(const int mesh_id, const int perm_id, const int dpn)
Get the PETSC to canonical permutation scattering for a given mesh and number of dpn.
void region_mask(mesh_t meshspec, SF::vector< RegionSpecs > ®spec, SF::vector< int > ®ionIDs, bool mask_elem, const char *reglist, bool warn_on_default_tags)
classify elements/points as belonging to a region
SF::meshdata< mesh_int_t, mesh_real_t > sf_mesh
void apply_stim_to_vector(const stimulus &s, sf_vec &vec, bool add)
void read_indices_global(SF::vector< T > &idx, const std::string filename, MPI_Comm comm)
int get_rank(MPI_Comm comm=PETSC_COMM_WORLD)
void indices_from_region_tags(SF::vector< mesh_int_t > &idx, const sf_mesh &mesh, const hashmap::unordered_set< int > &tags)
Populate vertex data with the vertices of multiple tag regions.
void init_stim_info(void)
uses potential for stimulation
bool is_extra(stim_t type)
whether stimulus is on extra grid (or on intra)
FILE_SPEC f_open(const char *fname, const char *mode)
Open a FILE_SPEC.
bool have_dbc_stims(const SF::vector< stimulus > &stimuli)
return wheter any stimuli require dirichlet boundary conditions
SF::scattering * register_permutation(const int mesh_id, const int perm_id, const int dpn)
Register a permutation between two orderings for a mesh.
bool is_current(stim_t type)
uses current as stimulation
char * dupstr(const char *old_str)
void log_msg(FILE_SPEC out, int level, unsigned char flag, const char *fmt,...)
@ emi_surface_unique_face_msh
@ emi_surface_counter_msh
void get_time(double &tm)
bool mesh_is_registered(const mesh_t gt)
check wheter a SF mesh is set
SF::abstract_vector< SF_int, SF_real > sf_vec
SF::abstract_matrix< SF_int, SF_real > sf_mat
V timing(V &t2, const V &t1)
std::string get_basename(const std::string &path)
void f_close(FILE_SPEC &f)
Close a FILE_SPEC.
#define UM2_to_CM2
convert um^2 to cm^2
#define PETSC_TO_CANONICAL
Permute algebraic data from PETSC to canonical ordering.
#define ALG_TO_NODAL
Scatter algebraic to nodal.
#define ELEM_PETSC_TO_CANONICAL
Permute algebraic element data from PETSC to canonical ordering.
Electrical stimulation functions.