26 #include "petsc_utils.h"
36 #include "caliper/cali.h"
43 void log_mesh_local_element_ranges(
const sf_mesh& emi_mesh,
44 const sf_mesh& emi_surfmesh_w_counter_face,
45 const sf_mesh& emi_surfmesh_unique_face)
49 MPI_Comm_rank(emi_surfmesh_w_counter_face.comm, &rank);
50 MPI_Comm_size(emi_surfmesh_w_counter_face.comm, &comm_size);
52 const unsigned long long local_emi_elems =
53 static_cast<unsigned long long>(emi_mesh.l_numelem);
54 const unsigned long long local_both_face_elems =
55 static_cast<unsigned long long>(emi_surfmesh_w_counter_face.l_numelem);
56 const unsigned long long local_unique_face_elems =
57 static_cast<unsigned long long>(emi_surfmesh_unique_face.l_numelem);
59 std::vector<unsigned long long> all_emi_elems;
60 std::vector<unsigned long long> all_both_face_elems;
61 std::vector<unsigned long long> all_unique_face_elems;
63 all_emi_elems.resize(comm_size, 0);
64 all_both_face_elems.resize(comm_size, 0);
65 all_unique_face_elems.resize(comm_size, 0);
68 MPI_Gather(&local_emi_elems, 1, MPI_UNSIGNED_LONG_LONG,
69 rank == 0 ? all_emi_elems.data() :
nullptr, 1, MPI_UNSIGNED_LONG_LONG,
70 0, emi_surfmesh_w_counter_face.comm);
71 MPI_Gather(&local_both_face_elems, 1, MPI_UNSIGNED_LONG_LONG,
72 rank == 0 ? all_both_face_elems.data() :
nullptr, 1, MPI_UNSIGNED_LONG_LONG,
73 0, emi_surfmesh_w_counter_face.comm);
74 MPI_Gather(&local_unique_face_elems, 1, MPI_UNSIGNED_LONG_LONG,
75 rank == 0 ? all_unique_face_elems.data() :
nullptr, 1, MPI_UNSIGNED_LONG_LONG,
76 0, emi_surfmesh_w_counter_face.comm);
78 if (rank != 0)
return;
80 const auto print_min_max = [](
const char* label,
const std::vector<unsigned long long>& counts) {
81 if (counts.empty())
return;
83 unsigned long long min_val = counts[0];
84 unsigned long long max_val = counts[0];
88 for (
int r = 1; r < static_cast<int>(counts.size()); ++r) {
89 if (counts[r] < min_val) {
93 if (counts[r] > max_val) {
99 log_msg(NULL, 0, 0,
" %s: \n\t\t min=%llu on rank=%d, \n\t\t max=%llu on rank=%d\n",
100 label, min_val, min_rank, max_val, max_rank);
102 log_msg(NULL, 0, 0,
"\n**********************************");
103 log_msg(NULL, 0, 0,
"min/max number of local-element ranges:");
104 print_min_max(
"emi_mesh", all_emi_elems);
105 print_min_max(
"emi_surfmesh_w_counter_face", all_both_face_elems);
106 print_min_max(
"emi_surfmesh_unique_face", all_unique_face_elems);
107 log_msg(NULL, 0, 0,
"**********************************");
113 MaterialType *m = mtype;
116 m->regions.resize(param_globals::num_gregions);
118 const char* grid_name =
"emi_grid_domain";
119 log_msg(logger, 0, 0,
"Setting up %s tissue poperties for %d regions ..", grid_name,
120 param_globals::num_gregions);
123 RegionSpecs* reg = m->regions.data();
129 for (
size_t i=0; i<m->regions.size(); i++)
131 for (
int j=0;j<param_globals::gregion[i].num_IDs;j++)
133 int tag = param_globals::gregion[i].ID[j];
136 if(extra_tags_default.
find(tag) != extra_tags_default.
end())
137 extra_tags_default.
erase(tag);
139 if(intra_tags_default.
find(tag) != intra_tags_default.
end())
140 intra_tags_default.
erase(tag);
144 for (
size_t i=0; i<m->regions.size(); i++, reg++)
146 if(!strcmp(param_globals::gregion[i].name,
"")) {
147 snprintf(buf,
sizeof buf,
", gregion_%d",
int(i));
148 param_globals::gregion[i].name =
dupstr(buf);
152 reg->regname = strdup(param_globals::gregion[i].name);
156 reg->nsubregs = extra_tags_default.
size();
158 reg->nsubregs = intra_tags_default.
size();
160 reg->nsubregs = param_globals::gregion[i].num_IDs;
163 reg->subregtags = NULL;
166 reg->subregtags =
new int[reg->nsubregs];
170 for (
int tag : extra_tags_default) {
171 reg->subregtags[j] = tag;
177 for (
int tag : intra_tags_default) {
178 reg->subregtags[j] = tag;
183 for (
int j=0;j<reg->nsubregs;j++)
184 reg->subregtags[j] = param_globals::gregion[i].ID[j];
189 elecMaterial *emat =
new elecMaterial();
193 emat->InVal[0] = param_globals::gregion[i].g_bath;
194 emat->InVal[1] = param_globals::gregion[i].g_bath;
195 emat->InVal[2] = param_globals::gregion[i].g_bath;
197 emat->ExVal[0] = param_globals::gregion[i].g_bath;
198 emat->ExVal[1] = param_globals::gregion[i].g_bath;
199 emat->ExVal[2] = param_globals::gregion[i].g_bath;
201 emat->BathVal[0] = param_globals::gregion[i].g_bath;
202 emat->BathVal[1] = param_globals::gregion[i].g_bath;
203 emat->BathVal[2] = param_globals::gregion[i].g_bath;
206 for (
int j=0; j<3; j++) {
207 emat->InVal[j] *= 1e-3 * param_globals::gregion[i].g_mult;
208 emat->ExVal[j] *= 1e-3 * param_globals::gregion[i].g_mult;
209 emat->BathVal[j] *= 1e-3 * param_globals::gregion[i].g_mult;
211 reg->material = emat;
214 if(strlen(param_globals::gi_scale_vec))
218 const char* file = param_globals::gi_scale_vec;
222 if(num_file_entries != mesh.g_numelem)
223 log_msg(0,4,0,
"%s warning: number of %s conductivity scaling entries does not match number of elements!",
229 escale->read_ascii(param_globals::gi_scale_vec);
242 m->el_scale.assign(p, p + escale->lsize());
243 escale->release_ptr(p);
247 void parabolic_solver_emi::init()
252 stats.init_logger(
"par_stats.dat");
271 MPI_Comm_rank(emi_surfmesh_w_counter_face.comm, &rank);
287 SF::init_vector(&vb_both_face, emi_surfmesh_w_counter_face, dpn, alg_surface_type);
288 SF::init_vector(&vb_unique_face, emi_surfmesh_unique_face, dpn, alg_surface_type);
289 SF::init_vector(&Ib_both_face, emi_surfmesh_w_counter_face, dpn, alg_surface_type);
290 SF::init_vector(&Ib_unique_face, emi_surfmesh_unique_face, dpn, alg_surface_type);
300 lhs_emi->init(emi_mesh, dpn, dpn, max_row_entries_emi*3);
301 stiffness_emi->init(emi_mesh, dpn, dpn, max_row_entries_emi*2);
302 mass_emi->init(emi_mesh, dpn, dpn, max_row_entries_emi*3);
303 mass_surf_emi->init(emi_mesh, dpn, dpn, max_row_entries_emi*3);
307 mesh_int_t M = emi_surfmesh_w_counter_face.g_numelem;
309 mesh_int_t m = emi_surfmesh_w_counter_face.l_numelem;
310 mesh_int_t m_one_side = emi_surfmesh_one_side.l_numelem;
311 mesh_int_t M_one_side = emi_surfmesh_one_side.g_numelem;
312 mesh_int_t m_unique_face = emi_surfmesh_unique_face.l_numelem;
313 mesh_int_t M_unique_face = emi_surfmesh_unique_face.g_numelem;
316 log_msg(NULL, 0, 0,
"\n**********************************");
317 log_msg(NULL, 0, 0,
"#elements of emi surfmesh unique face: %zu", emi_surfmesh_unique_face.g_numelem);
318 log_msg(NULL, 0, 0,
"#elements of emi surfmesh one side: %zu", emi_surfmesh_one_side.g_numelem);
319 log_msg(NULL, 0, 0,
"#elements of emi surfmesh: %zu", emi_surfmesh_w_counter_face.g_numelem);
320 log_msg(NULL, 0, 0,
"#elements of emi mesh: %zu", emi_mesh.g_numelem);
321 log_msg(NULL, 0, 0,
"#dofs for emi_mesh: %zu", emi_mesh.g_numpts);
322 log_msg(NULL, 0, 0,
"#max_row_entries_emi: %zu", max_row_entries_emi);
323 log_msg(NULL, 0, 0,
"**********************************\n");
324 log_mesh_local_element_ranges(emi_mesh, emi_surfmesh_w_counter_face, emi_surfmesh_unique_face);
327 SF::layout_from_count<long int>(emi_surfmesh_w_counter_face.l_numelem, layout, emi_surfmesh_w_counter_face.comm);
329 mesh_int_t n_l = emi_mesh.pl.algebraic_layout()[rank];
333 SF::layout_from_count<long int>(emi_surfmesh_one_side.l_numelem, layout_one_side, emi_surfmesh_one_side.comm);
334 mesh_int_t m_one_side_l = layout_one_side[rank];
337 SF::layout_from_count<long int>(emi_surfmesh_unique_face.l_numelem, layout_unique_face, emi_surfmesh_unique_face.comm);
338 mesh_int_t m_unique_face_l = layout_unique_face[rank];
345 B->init(M, N, m, n, m_l, max_row_entries_emi*2);
348 Bi->init(M, N, m, n, m_l, max_row_entries_emi*2);
351 BsM->init(N, M, n, m, n_l, max_row_entries_emi*2);
354 Vm_myocyte_emi->init(N, N, n, n, n_l, max_row_entries_emi*2);
355 Vm_myocyte_emi->zero();
358 SF::construct_direct_unique_both_operators(operator_unique_to_both_faces,
359 operator_both_to_unique_face,
360 map_elem_uniqueFace_to_elem_bothface,
361 map_elem_uniqueFace_to_elem_oneface,
362 vec_both_to_one_face,
363 emi_surfmesh_w_counter_face,
364 emi_surfmesh_unique_face,
370 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);
373 #ifdef EMI_DEBUG_MESH
376 MPI_Comm_rank(emi_mesh.comm, &local_rank);
378 fprintf(stderr,
"RANK %d MESH SIZES: one_side=%zu, counter=%zu, unique=%zu\n",
379 local_rank, emi_surfmesh_one_side.l_numelem,
380 emi_surfmesh_w_counter_face.l_numelem, emi_surfmesh_unique_face.l_numelem);
382 fprintf(stderr,
"RANK %d MAP SIZES: uniqueFace_to_oneface=%zu\n",
383 local_rank, map_elem_uniqueFace_to_elem_oneface.size());
395 if(!(vb_ptr != NULL && Ib_ptr != NULL)) {
396 log_msg(0,5,0,
"%s error: global Vb and Ib vectors not properly set up! Ionics seem invalid! Aborting!",
402 vb->shallow_copy(*vb_ptr);
404 Ib->shallow_copy(*Ib_ptr);
406 parab_tech =
static_cast<parabolic_solver_emi::parabolic_t
>(param_globals::parab_solve_emi);
414 double start, end, period;
417 mass_integrator mass_integ;
418 mass_integrator mass_integ_emi;
421 int log_flag = param_globals::output_level > 1 ?
ECHO : 0;
422 MaterialType & mt = mtype[0];
438 log_msg(NULL, 0, 0,
"assemble stiffness matrix");
440 elec_stiffness_integrator stfn_integ_emi(mt);
441 stiffness_emi->zero();
444 log_msg(logger,0,log_flag,
"Computed parabolic stiffness matrix in %.5f seconds.",
float(dur));
446 log_msg(NULL, 0, 0,
"assemble mass matrix on the volumetric mesh");
448 mass_integrator mass_integ;
452 log_msg(logger,0,log_flag,
"Computed volumetric mass matrix in %.5f seconds.",
float(dur));
454 log_msg(NULL, 0, 0,
"assemble LHS matrix and mass matrix on the surface mesh");
458 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);
460 log_msg(logger,0,log_flag,
"Computed parabolic mass matrix in %.5f seconds.",
float(dur));
464 bool same_nonzero =
false;
468 log_msg(logger,0,log_flag,
"lhs matrix enforcing Dirichlet boundaries.");
472 dbc =
new dbc_manager(*lhs_emi, stimuli);
474 dbc->recompute_dbcs();
476 dbc->enforce_dbc_lhs();
479 log_msg(logger,0,log_flag,
"lhs matrix Dirichlet enforcing done in %.5f seconds.",
float(dur));
482 log_msg(logger,0,
ECHO,
"without enforcing Dirichlet boundaries on the lhs matrix!");
484 phie_mat_has_nullspace =
true;
489 setup_linear_solver(logger);
491 log_msg(logger,0,log_flag,
"Initializing parabolic solver in %.5f seconds.",
float(dur));
494 period =
timing(end, start);
497 void parabolic_solver_emi::setup_linear_solver(
FILE_SPEC logger)
499 tol = param_globals::cg_tol_parab;
500 max_it = param_globals::cg_maxit_parab;
502 std::string default_opts;
503 std::string solver_file;
504 solver_file = param_globals::parab_options_file;
505 if (param_globals::flavor == std::string(
"ginkgo")) {
506 default_opts = std::string(
509 "type": "solver::Cg",
511 "type": "solver::Multigrid",
512 "min_coarse_rows": 8,
514 "default_initial_guess": "zero",
517 "type": "multigrid::Pgm",
518 "deterministic": false
522 "type": "preconditioner::Schwarz",
524 "type": "preconditioner::Jacobi"
540 "type": "ResidualNorm",
541 "reduction_factor": 1e-4
546 } else if (param_globals::flavor == std::string(
"petsc")) {
547 default_opts = std::string(
"-ksp_type cg -pc_type gamg -options_left");
549 lin_solver->setup_solver(*lhs_emi, tol, max_it * 100, param_globals::cg_norm_parab,
550 "parabolic PDE", phie_mat_has_nullspace, logger, solver_file.c_str(),
551 default_opts.c_str());
554 void parabolic_solver_emi::solve()
556 switch (parab_tech) {
557 case SEMI_IMPLICIT: solve_semiImplicit();
break;
561 void parabolic_solver_emi::solve_semiImplicit()
568 dbc->enforce_dbc_rhs(*ui);
576 stiffness_emi->mult(*ui, *Iij_temp);
583 if (Iij_stim->mag() > 0.0) {
585 mass_emi->mult(*Iij_stim, *Iij_temp);
597 (*lin_solver)(*dui, *Irhs);
601 if(lin_solver->reason < 0) {
602 log_msg(0, 5, 0,
"%s solver diverged. Reason: %s.", lin_solver->name.c_str(),
603 petsc_get_converged_reason_str(lin_solver->reason));
609 ui_pre->add_scaled(*dui, 1.0);
611 ui->add_scaled(*ui_pre, 1.0);
618 dbc->enforce_dbc_rhs(*ui);
623 stats.slvtime +=
timing(t1, t0);
624 stats.update_iter(lin_solver->niter);
637 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,
638 std::vector<std::string> & tags_data)
647 for(
size_t i=0; i<rnod.
size(); i++){
651 for(
size_t eidx=0; eidx<emi_surfmesh_w_counter_face.l_numelem; eidx++)
653 std::vector<int> elem_nodes;
654 mesh_int_t tag = emi_surfmesh_w_counter_face.tag[eidx];
655 for (
int n = emi_surfmesh_w_counter_face.dsp[eidx]; n < emi_surfmesh_w_counter_face.dsp[eidx+1];n++)
657 mesh_int_t l_idx = emi_surfmesh_w_counter_face.con[n];
659 std::pair <mesh_int_t,mesh_int_t> Index_tag_old;
660 Index_tag_old = std::make_pair(l2g[l_idx],tags[eidx]);
661 mesh_int_t dof = map_vertex_tag_to_dof[Index_tag_old];
662 elem_nodes.push_back(dof);
667 std::string result_first;
668 std::string result_second;
669 std::sort(elem_nodes.begin(),elem_nodes.end());
672 if(elem_nodes.size()==2){
675 key.
v1 = elem_nodes[0];
676 key.
v2 = elem_nodes[1];
677 std::pair<SF::emi_face<mesh_int_t,SF::tuple<mesh_int_t>>,
678 SF::emi_face<mesh_int_t,SF::tuple<mesh_int_t>>> value = line_face[key];
680 tag_first = value.first.tag;
681 tag_second = value.second.tag;
682 result_first = std::to_string(tag_first) +
":" + std::to_string(tag_second);
683 result_second = std::to_string(tag_second) +
":" + std::to_string(tag_first);
685 else if(elem_nodes.size()==3){
687 key.
v1 = elem_nodes[0];
688 key.
v2 = elem_nodes[1];
689 key.
v3 = elem_nodes[2];
690 std::pair<SF::emi_face<mesh_int_t,SF::triple<mesh_int_t>>,
691 SF::emi_face<mesh_int_t,SF::triple<mesh_int_t>>> value = tri_face[key];
693 tag_first = value.first.tag;
694 tag_second = value.second.tag;
695 result_first = std::to_string(tag_first) +
":" + std::to_string(tag_second);
696 result_second = std::to_string(tag_second) +
":" + std::to_string(tag_first);
698 else if(elem_nodes.size()==4){
700 key.
v1 = elem_nodes[0];
701 key.
v2 = elem_nodes[1];
702 key.
v3 = elem_nodes[2];
703 key.
v4 = elem_nodes[3];
704 std::pair<SF::emi_face<mesh_int_t,SF::quadruple<mesh_int_t>>,
705 SF::emi_face<mesh_int_t,SF::quadruple<mesh_int_t>>> value = quad_face[key];
707 tag_first = value.first.tag;
708 tag_second = value.second.tag;
709 result_first = std::to_string(tag_first) +
":" + std::to_string(tag_second);
710 result_second = std::to_string(tag_second) +
":" + std::to_string(tag_first);
713 tags_data.push_back(result_first);
714 tags_data.push_back(result_second);
718 void EMI::initialize()
726 logger =
f_open(
"emi.log", param_globals::experiment != 4 ?
"w" :
"r");
727 const int verb = param_globals::output_level;
742 ion.set_surface_mesh_data(parab_solver.line_face,
743 parab_solver.tri_face,
744 parab_solver.quad_face,
745 parab_solver.map_vertex_tag_to_dof);
748 std::vector<std::string> tags_data;
749 tags_onFace(parab_solver.line_face,
750 parab_solver.tri_face,
751 parab_solver.quad_face,
752 parab_solver.map_vertex_tag_to_dof,
753 parab_solver.map_vertex_tag_to_dof_petsc,
757 ion.set_tags_onFace(tags_data);
759 ion.set_face_region_data(parab_solver.intra_tags, parab_solver.map_elem_uniqueFace_to_tags);
763 set_elec_tissue_properties_emi_volume(mtype_vol, parab_solver.extra_tags, parab_solver.intra_tags, logger);
764 region_mask(
emi_msh, mtype_vol[0].regions, mtype_vol[0].regionIDs,
true,
"gregion_vol");
769 param_globals::dt, 0,
"elec::ref_dt",
"TS");
773 param_globals::operator_splitting = 0;
784 balance_electrodes();
786 scale_total_stimulus_current(stimuli, *parab_solver.mass_emi, *parab_solver.mass_surf_emi, logger);
804 parab_solver.operator_unique_to_both_faces->mult(*parab_solver.vb, *parab_solver.vb_both_face);
805 SF::assign_resting_potential_from_ionic_models_on_myocyte(*parab_solver.Vm_myocyte_emi,
806 parab_solver.vb_both_face,
807 parab_solver.elemTag_emi_mesh,
808 parab_solver.map_vertex_tag_to_dof_petsc,
809 parab_solver.line_face, parab_solver.tri_face, parab_solver.quad_face,
810 emi_surfmesh_w_counter_face, emi_mesh);
812 parab_solver.Vm_myocyte_emi->get_diagonal(*parab_solver.ui);
813 *parab_solver.vb_unique_face = *parab_solver.vb;
821 this->initialize_time +=
timing(t2, t1);
824 void EMI::setup_mappings()
832 log_msg(logger, 0, 0,
"%s: Setting up intracellular algebraic-to-nodal scattering.", __func__);
837 log_msg(logger, 0, 0,
"%s: Setting up intracellular PETSc to canonical permutation.", __func__);
842 void EMI::compute_step()
851 const int verb = param_globals::output_level;
858 apply_dbc_stimulus();
867 apply_current_stimulus();
871 parab_solver.operator_unique_to_both_faces->mult(*parab_solver.Ib, *parab_solver.Ib_both_face);
872 parab_solver.BsM->mult(*parab_solver.Ib_both_face, *parab_solver.Irhs);
876 parab_solver.solve();
879 parab_solver.B->mult(*parab_solver.ui, *parab_solver.vb_both_face);
881 parab_solver.operator_both_to_unique_face->mult(*parab_solver.vb_both_face, *parab_solver.vb_unique_face);
882 *parab_solver.vb = *parab_solver.vb_unique_face;
890 this->compute_time +=
timing(t2, t1);
897 void EMI::output_step()
902 output_manager.write_data();
904 double curtime =
timing(t2, t1);
905 this->output_time += curtime;
908 IO_stats.tot_time += curtime;
924 output_manager.close_files_and_cleanup();
930 void EMI::setup_stimuli()
935 stimuli.
resize(param_globals::num_stim);
936 for (
int i = 0; i < param_globals::num_stim; i++) {
938 stimulus & s = stimuli[i];
944 s.associated_intra_mesh =
emi_msh, s.associated_extra_mesh =
emi_msh;
948 if (s.phys.type ==
Illum) {
966 }
else if (s.phys.type ==
I_tm) {
968 SF::restrict_to_membrane(s.electrode.vertices, dof2ptsData, mesh);
980 if (s.electrode.dump_vtx) {
985 if(param_globals::stim[i].pulse.dumpTrace &&
get_rank() == 0) {
987 s.pulse.wave.write_trace(s.name+
".trc");
993 void EMI::apply_dbc_stimulus()
995 parabolic_solver_emi& ps = parab_solver;
998 bool dbcs_have_updated = ps.dbc !=
nullptr && ps.dbc->dbc_update();
1001 if (dbcs_have_updated && time_not_final) {
1002 parab_solver.rebuild_matrices(mtype_vol, *ion.miif, stimuli, logger);
1006 void EMI::apply_current_stimulus()
1008 parabolic_solver_emi& ps = parab_solver;
1009 ps.Iij_stim->set(0.0);
1012 for(stimulus & s : stimuli) {
1014 switch (s.phys.type) {
1017 ps.Bi->mult(*ps.Iij_temp, *ps.Ib_both_face);
1018 ps.operator_both_to_unique_face->mult(*ps.Ib_both_face, *ps.Ib_unique_face);
1019 ps.Ib->add_scaled(*ps.Ib_unique_face, -0.5);
1033 void EMI::balance_electrodes()
1035 for (
int i = 0; i < param_globals::num_stim; i++) {
1036 if (param_globals::stim[i].crct.balance != -1) {
1037 int from = param_globals::stim[i].crct.balance;
1040 log_msg(NULL, 0, 0,
"Balancing stimulus %d with %d %s-wise.", from, to,
1041 is_current(stimuli[from].phys.type) ?
"current" :
"voltage");
1043 stimulus& s_from = stimuli[from];
1044 stimulus& s_to = stimuli[to];
1046 s_to.pulse = s_from.pulse;
1047 s_to.ptcl = s_from.ptcl;
1048 s_to.phys = s_from.phys;
1049 s_to.pulse.strength *= -1.0;
1051 if (s_from.phys.type ==
I_ex || s_from.phys.type ==
I_in) {
1055 if (!s_from.phys.total_current) {
1056 sf_mat& mass = *parab_solver.mass_emi;
1060 s_to.pulse.strength *= fabs(vol0 / vol1);
1072 for (stimulus & s : stimuli){
1073 if(
is_current(s.phys.type) && s.phys.total_current){
1074 switch (s.phys.type) {
1085 float scale = 1.e12 / vol;
1087 s.pulse.strength *= scale;
1090 "%s [Stimulus %d]: current density scaled to %.4g uA/cm^3\n",
1091 s.name.c_str(), s.idx, s.pulse.strength);
1105 s.pulse.strength /= surf;
1107 "%s [Stimulus %d]: current density scaled to %.4g uA/cm^2\n",
1108 s.name.c_str(), s.idx, s.pulse.strength);
1119 static void assign_deterministic_elem_numbering(
sf_mesh & mesh)
1121 const int KEY_SIZE = 6;
1122 int rank = 0, size = 0;
1123 MPI_Comm_rank(mesh.comm, &rank);
1124 MPI_Comm_size(mesh.comm, &size);
1126 auto make_key = [&](
size_t i) {
1127 std::array<long long, KEY_SIZE> k;
1129 k[0] = (
long long)mesh.type[i];
1130 k[1] = (
long long)mesh.tag[i];
1133 if (mesh.type[i] ==
SF::Line) nn = 2;
1134 else if (mesh.type[i] ==
SF::Tri) nn = 3;
1135 else if (mesh.type[i] ==
SF::Quad) nn = 4;
1138 std::vector<long long> nodes;
1140 size_t off = mesh.dsp[i];
1141 for (
int j = 0; j < nn; j++) {
1142 nodes.push_back((
long long)mesh.con[off + j]);
1144 std::sort(nodes.begin(), nodes.end());
1145 for (
int j = 0; j < (int)nodes.size(); j++) {
1146 k[2 + j] = nodes[j];
1152 std::vector<long long> local_keys(mesh.l_numelem * KEY_SIZE, -1);
1153 for (
size_t i = 0; i < mesh.l_numelem; i++) {
1154 auto k = make_key(i);
1155 for (
int j = 0; j < KEY_SIZE; j++) local_keys[i * KEY_SIZE + j] = k[j];
1159 std::vector<int> counts(size, 0), displs(size, 0);
1160 int local_count = (int)local_keys.size();
1161 MPI_Allgather(&local_count, 1, MPI_INT, counts.data(), 1, MPI_INT, mesh.comm);
1163 for (
int r = 0; r < size; r++) {
1168 std::vector<long long> all_keys;
1169 if (rank == 0) all_keys.resize(total, -1);
1170 MPI_Gatherv(local_keys.data(), local_count, MPI_LONG_LONG,
1171 rank == 0 ? all_keys.data() :
nullptr, counts.data(), displs.data(), MPI_LONG_LONG,
1175 std::vector<std::array<long long, KEY_SIZE>> sorted_keys;
1177 const int nkeys = total / KEY_SIZE;
1178 sorted_keys.resize(nkeys);
1179 for (
int i = 0; i < nkeys; i++) {
1180 std::array<long long, KEY_SIZE> k;
1181 for (
int j = 0; j < KEY_SIZE; j++) k[j] = all_keys[i * KEY_SIZE + j];
1184 std::sort(sorted_keys.begin(), sorted_keys.end());
1189 if (rank == 0) nkeys = (int)sorted_keys.size();
1190 MPI_Bcast(&nkeys, 1, MPI_INT, 0, mesh.comm);
1191 std::vector<long long> flat_sorted(nkeys * KEY_SIZE, -1);
1193 for (
int i = 0; i < nkeys; i++) {
1194 for (
int j = 0; j < KEY_SIZE; j++) flat_sorted[i * KEY_SIZE + j] = sorted_keys[i][j];
1197 MPI_Bcast(flat_sorted.data(), (
int)flat_sorted.size(), MPI_LONG_LONG, 0, mesh.comm);
1201 sorted_keys.resize(nkeys);
1202 for (
int i = 0; i < nkeys; i++) {
1203 std::array<long long, KEY_SIZE> k;
1204 for (
int j = 0; j < KEY_SIZE; j++) k[j] = flat_sorted[i * KEY_SIZE + j];
1212 nbr_ref.
resize(mesh.l_numelem);
1213 nbr_sub.
resize(mesh.l_numelem);
1214 for (
size_t i = 0; i < mesh.l_numelem; i++) {
1215 auto k = make_key(i);
1216 auto it = std::lower_bound(sorted_keys.begin(), sorted_keys.end(), k);
1217 if (it == sorted_keys.end() || *it != k) {
1218 log_msg(0, 5, 0,
"deterministic numbering failed to find key (rank %d, elem %zu)", rank, i);
1227 void EMI::setup_output()
1229 bool write_binary =
false;
1230 std::string output_base =
get_basename(param_globals::meshname);
1235 std::string output_file = output_base +
"_e";
1236 log_msg(0, 0, 0,
"Writing \"%s\" mesh: %s", mesh.name.c_str(), output_file.c_str());
1239 output_manager.register_output(parab_solver.ui,
emi_msh, 1, param_globals::phiefile,
"mV", NULL);
1243 mesh_m.name =
"Membrane";
1244 output_file = output_base +
"_m";
1245 log_msg(0, 0, 0,
"Writing \"%s\" mesh: %s", mesh_m.name.c_str(), output_file.c_str());
1252 output_manager.register_output(parab_solver.vb_unique_face,
emi_surface_unique_face_msh, 1, param_globals::vofile,
"mV", NULL,
true);
1254 if(param_globals::num_trace) {
1256 open_trace(ion.miif, param_globals::num_trace, param_globals::trace_node, NULL, &imesh);
1260 IO_stats.init_logger(
"IO_stats.dat");
1263 void EMI::dump_matrices()
1265 std::string bsname = param_globals::dump_basename;
1270 fn = bsname +
"_lhs.bin";
1271 parab_solver.lhs_emi->write(fn.c_str());
1273 fn = bsname +
"_K.bin";
1274 parab_solver.stiffness_emi->write(fn.c_str());
1276 fn = bsname +
"_B.bin";
1277 parab_solver.B->write(fn.c_str());
1279 fn = bsname +
"_Bi.bin";
1280 parab_solver.Bi->write(fn.c_str());
1282 fn = bsname +
"_BsM.bin";
1283 parab_solver.BsM->write(fn.c_str());
1285 fn = bsname +
"_M.bin";
1286 parab_solver.mass_emi->write(fn.c_str());
1288 fn = bsname +
"_Ms.bin";
1289 parab_solver.mass_surf_emi->write(fn.c_str());
1295 double EMI::timer_val(
const int timer_id)
1301 stimuli[sidx].value(val);
1304 val = std::nan(
"NaN");
1311 std::string EMI::timer_unit(
const int timer_id)
1318 s_unit = stimuli[sidx].pulse.wave.f_unit;
1323 void EMI::setup_solvers()
1326 parab_solver.init();
1327 parab_solver.rebuild_matrices(mtype_vol, *ion.miif, stimuli, logger);
1329 if(param_globals::dump2MatLab)
1337 MPI_Comm_size(comm, &size);
1338 MPI_Comm_rank(comm, &rank);
1351 MPI_Comm_size(comm, &size); MPI_Comm_rank(comm, &rank);
1352 divide(num_tags, size, num_tags_per_rank);
1355 void EMI::setup_EMI_mesh()
1357 log_msg(0,0,0,
"\n *** Processing EMI mesh ***\n");
1359 const std::string basename = param_globals::meshname;
1360 const int verb = param_globals::output_level;
1362 assert(mesh_registry.count(
emi_msh) == 1);
1371 MPI_Comm comm = emi_mesh.
comm;
1374 double t1, t2, s1, s2;
1375 const double total_setup_t0 = MPI_Wtime();
1376 MPI_Comm_size(comm, &size); MPI_Comm_rank(comm, &rank);
1381 if (emi_mesh.l_numelem > 0) {
1386 const char* type_name = (first_elem ==
SF::Line) ?
"1D (Line)" :
1387 (first_elem ==
SF::Tri) ?
"2D (Tri)" :
1390 log_msg(0, 5, 0,
"\n*** ERROR: EMI model requires a 3D volumetric mesh!");
1391 log_msg(0, 5, 0,
"*** Current mesh element type: %s", type_name);
1392 log_msg(0, 5, 0,
"*** EMI only supports 3D element types: Tetra, Pyramid, Prism, Hexa");
1393 log_msg(0, 5, 0,
"*** Please provide a 3D mesh with volume elements.\n");
1402 int total_num_tags = 0;
1403 if(verb)
log_msg(NULL, 0, 0,
"\nReading tags for extra and intra regions from input files");
1409 if(verb)
log_msg(NULL, 0, 0,
"Read extracellular tags");
1412 parab_solver.extra_tags.insert(tag);
1415 if(verb)
log_msg(NULL, 0, 0,
"Read intracellular tags");
1418 parab_solver.intra_tags.insert(tag);
1421 total_num_tags = parab_solver.extra_tags.size() + parab_solver.intra_tags.size();
1422 if(total_num_tags < size){
1423 log_msg(0,5,0,
"\nThe number of unique tags on EMI mesh is smaller than number of processors!");
1426 if(verb)
log_msg(NULL, 0, 0,
"\nextra_tags=%lu, intra_tags=%lu", parab_solver.extra_tags.size(), parab_solver.intra_tags.size());
1429 if(verb)
log_msg(NULL, 0, 0,
"Done in %f sec.",
float(t2 - t1));
1437 if(verb)
log_msg(NULL, 0, 0,
"\nReading points with data on each vertex");
1441 assert(ptsidx.
size()==ptsData.
size());
1443 if(verb)
log_msg(NULL, 0, 0,
"Done in %f sec.",
float(t2 - t1));
1445 std::list< sf_mesh* > meshlist;
1446 meshlist.push_back(&emi_mesh);
1451 if(verb)
log_msg(NULL, 0, 0,
"\nDistribute mesh based on tags");
1454 distribute_elements_based_tags(emi_mesh, total_num_tags);
1456 if(verb)
log_msg(NULL, 0, 0,
"Done in %f sec.",
float(t2 - t1));
1462 if(verb)
log_msg(NULL, 0, 0,
"\nInserting points");
1466 if(verb)
log_msg(NULL, 0, 0,
"Done in %f sec.",
float(t2 - t1));
1471 if(verb)
log_msg(NULL, 0, 0,
"\nCompute location of all dofs on emi mesh(inner dofs, memebarne, gap junction) saved into ptsData from original mesh");
1473 compute_ptsdata_from_original_mesh( emi_mesh,
1476 parab_solver.extra_tags,
1477 parab_solver.intra_tags);
1479 if(verb)
log_msg(NULL, 0, 0,
"Done in %f sec.",
float(t2 - t1));
1487 if(verb)
log_msg(NULL, 0, 0,
"\nExtract EMI surface mesh");
1489 extract_face_based_tags(emi_mesh,
SF::NBR_REF, vertex2ptsdata,
1490 parab_solver.line_face,
1491 parab_solver.tri_face,
1492 parab_solver.quad_face,
1493 parab_solver.extra_tags,
1494 parab_solver.intra_tags,
1495 emi_surfmesh_one_side, emi_surfmesh_w_counter_face, emi_surfmesh_unique_face,
1496 parab_solver.map_elem_uniqueFace_to_elem_oneface,
1497 unused_map_elem_oneface_to_elem_uniqueFace);
1498 meshlist.push_back(&emi_surfmesh_one_side);
1500 if(verb)
log_msg(NULL, 0, 0,
"Done in %f sec.",
float(t2 - t1));
1504 compute_surface_mesh_with_counter_face(emi_surfmesh_w_counter_face,
SF::NBR_REF,
1505 parab_solver.line_face,
1506 parab_solver.tri_face,
1507 parab_solver.quad_face);
1509 compute_surface_mesh_with_unique_face(emi_surfmesh_unique_face,
SF::NBR_REF,
1510 parab_solver.line_face,
1511 parab_solver.tri_face,
1512 parab_solver.quad_face,
1513 parab_solver.map_elem_uniqueFace_to_tags);
1518 SF::create_reverse_elem_mapping_between_surface_meshes(parab_solver.line_face,
1519 parab_solver.tri_face,
1520 parab_solver.quad_face,
1521 parab_solver.vec_both_to_one_face,
1524 if(verb)
log_msg(NULL, 0, 0,
"Done in %f sec.",
float(t2 - t1));
1529 if(verb)
log_msg(NULL, 0, 0,
"\ncompute global number of interface");
1530 size_t global_count_surf = 0;
1531 size_t numelem_surface = emi_surfmesh_one_side.l_numelem;
1532 unsigned long long local_count_surf =
static_cast<unsigned long long>(numelem_surface);
1533 unsigned long long global_count_surf_ull = 0;
1534 MPI_Reduce(&local_count_surf, &global_count_surf_ull, 1, MPI_UNSIGNED_LONG_LONG, MPI_SUM, 0, MPI_COMM_WORLD);
1535 global_count_surf =
static_cast<size_t>(global_count_surf_ull);
1536 if(verb && rank==0) fprintf(stdout,
"global number of interfaces = %zu\n", global_count_surf);
1543 sub_numbering(emi_mesh);
1544 emi_mesh.generate_par_layout();
1556 if(verb)
log_msg(NULL, 0, 0,
"\ndecouple emi interfaces");
1557 if(verb)
log_msg(NULL, 0, 0,
"\tcompute map oldIdx to dof");
1558 compute_map_vertex_to_dof(emi_mesh,
SF::NBR_REF, vertex2ptsdata, parab_solver.extra_tags, parab_solver.map_vertex_tag_to_dof);
1563 if(verb)
log_msg(NULL, 0, 0,
"\tcomplete map oldIdx to dof with counter interface");
1565 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);
1570 if(verb)
log_msg(NULL, 0, 0,
"\tupdate mesh with dof");
1571 update_emi_mesh_with_dofs(emi_mesh,
SF::NBR_REF, parab_solver.map_vertex_tag_to_dof, parab_solver.dof2vertex);
1573 if(verb)
log_msg(NULL, 0, 0,
"Done in %f sec.",
float(t2 - t1));
1579 if(verb)
log_msg(NULL, 0, 0,
"\nInitialize petsc =0 for map<oldIdx,tag> -><dof, petsc>");
1581 for(
const auto & key_value : parab_solver.map_vertex_tag_to_dof)
1583 mesh_int_t gIndex_old = key_value.first.first;
1587 std::pair <mesh_int_t,mesh_int_t> dof_petsc = std::make_pair(dof,-1);
1588 parab_solver.map_vertex_tag_to_dof_petsc.insert({key_value.first,dof_petsc});
1591 if(verb)
log_msg(NULL, 0, 0,
"Done in %f sec.",
float(t2 - t1));
1597 if(verb)
log_msg(NULL, 0, 0,
"Inserting points and ptsData of dofs to emi_mesh");
1598 insert_points_ptsData_to_dof(tmesh_backup_old, emi_mesh,
SF::NBR_REF, parab_solver.dof2vertex, vertex2ptsdata, dof2ptsData);
1600 if(verb)
log_msg(NULL, 0, 0,
"Done in %f sec.",
float(t2 - t1));
1606 if(verb)
log_msg(NULL, 0, 0,
"Generating unique PETSc numberings");
1609 sub_numbering(emi_mesh);
1610 emi_mesh.generate_par_layout();
1613 if(verb)
log_msg(NULL, 0, 0,
"Done in %f sec.",
float(t2 - t1));
1619 if(verb)
log_msg(NULL, 0, 0,
"Generating unique PETSc numberings");
1622 petsc_numbering(emi_mesh);
1625 if(verb)
log_msg(NULL, 0, 0,
"Done in %f sec.",
float(t2 - t1));
1630 if(verb)
log_msg(NULL, 0, 0,
"Updating the map between indices to PETSc numberings");
1632 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);
1634 if(verb)
log_msg(NULL, 0, 0,
"Done in %f sec.",
float(t2 - t1));
1640 if(verb)
log_msg(NULL, 0, 0,
"Updating surface mesh with dof");
1641 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);
1643 if(verb)
log_msg(NULL, 0, 0,
"Done in %f sec.",
float(t2 - t1));
1649 if(verb)
log_msg(NULL, 0, 0,
"Layout for element of EMI surfmesh");
1652 SF::layout_from_count<long int>(emi_surfmesh_one_side.l_numelem, layout, emi_surfmesh_one_side.comm);
1653 size_t count = layout[rank+1] - layout[rank];
1655 for (
int i = 0; i <
count; ++i){
1656 emi_surfmesh_elem[i] = layout[rank]+i;
1662 if(verb)
log_msg(NULL, 0, 0,
"Done in %f sec.",
float(t2 - t1));
1668 if(verb)
log_msg(NULL, 0, 0,
"Layout for element of EMI surfmesh w counter face");
1671 SF::layout_from_count<long int>(emi_surfmesh_w_counter_face.l_numelem, layout_counter, emi_surfmesh_w_counter_face.comm);
1672 size_t count_counter = layout_counter[rank+1] - layout_counter[rank];
1673 emi_surfmesh_counter_elem.
resize(count_counter);
1674 for (
int i = 0; i < count_counter; ++i){
1675 emi_surfmesh_counter_elem[i] = layout_counter[rank]+i;
1677 emi_surfmesh_w_counter_face.localize(
SF::NBR_REF);
1680 if(verb)
log_msg(NULL, 0, 0,
"Done in %f sec.",
float(t2 - t1));
1687 if(verb)
log_msg(NULL, 0, 0,
"Layout for element of EMI surfmesh w counter face");
1690 SF::layout_from_count<long int>(emi_surfmesh_unique_face.l_numelem, layout_unique, emi_surfmesh_unique_face.comm);
1691 size_t count_unique = layout_unique[rank+1] - layout_unique[rank];
1692 emi_surfmesh_unique_elem.
resize(count_unique);
1693 for (
int i = 0; i < count_unique; ++i){
1694 emi_surfmesh_unique_elem[i] = layout_unique[rank]+i;
1699 if(verb)
log_msg(NULL, 0, 0,
"Done in %f sec.",
float(t2 - t1));
1705 if(verb)
log_msg(NULL, 0, 0,
"Inserting points to EMI surfmesh");
1706 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);
1707 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);
1708 insert_points_to_surface_mesh(tmesh_backup_old, emi_surfmesh_unique_face,
SF::NBR_REF, parab_solver.dof2vertex);
1710 if(verb)
log_msg(NULL, 0, 0,
"Done in %f sec.",
float(t2 - t1));
1716 if(verb)
log_msg(NULL, 0, 0,
"Generating submesh_numbering and PETSc numberings for surface mesh");
1719 sub_numbering(emi_surfmesh_one_side);
1720 emi_surfmesh_one_side.generate_par_layout();
1723 petsc_numbering(emi_surfmesh_one_side);
1728 sub_numbering(emi_surfmesh_w_counter_face);
1729 emi_surfmesh_w_counter_face.generate_par_layout();
1732 petsc_numbering(emi_surfmesh_w_counter_face);
1737 sub_numbering(emi_surfmesh_unique_face);
1738 emi_surfmesh_unique_face.generate_par_layout();
1741 petsc_numbering(emi_surfmesh_unique_face);
1743 assign_deterministic_elem_numbering(emi_surfmesh_unique_face);
1746 if(verb)
log_msg(NULL, 0, 0,
"Done in %f sec.",
float(t2 - t1));
1752 if(verb)
log_msg(NULL, 0, 0,
"assign PETSc numbering for new faces");
1753 SF::assign_petsc_on_counter_face(parab_solver.map_vertex_tag_to_dof_petsc,comm);
1756 for (it = parab_solver.map_vertex_tag_to_dof_petsc.begin(); it != parab_solver.map_vertex_tag_to_dof_petsc.end(); it++)
1758 std::pair <mesh_int_t,mesh_int_t> Index_tag_old = it->first;
1759 std::pair <mesh_int_t,mesh_int_t> dof_petsc = it->second;
1760 parab_solver.dof2petsc[dof_petsc.first] = dof_petsc.second;
1761 parab_solver.petsc2dof[dof_petsc.second] = dof_petsc.first;
1766 #ifdef EMI_DEBUG_MESH
1768 int invalid_count = 0;
1769 for (
const auto& [key, val] : parab_solver.map_vertex_tag_to_dof_petsc) {
1770 if (val.second < 0) {
1772 if (invalid_count <= 3) {
1773 fprintf(stderr,
"RANK %d INVALID: vertex=%ld tag=%ld dof=%ld petsc=%ld\n",
1774 rank, (
long)key.first, (
long)key.second, (
long)val.first, (
long)val.second);
1778 fprintf(stderr,
"RANK %d: After Step 26: %d invalid PETSc indices out of %zu total\n",
1779 rank, invalid_count, parab_solver.map_vertex_tag_to_dof_petsc.size());
1785 if(verb)
log_msg(NULL, 0, 0,
"Done in %f sec.",
float(t2 - t1));
1790 added_counter_faces_to_map(parab_solver.line_face, parab_solver.tri_face, parab_solver.quad_face);
1791 const double total_setup = MPI_Wtime() - total_setup_t0;
1792 log_msg(0,0,0,
"Total setup_EMI_mesh processing time: %.5f sec.",
float(total_setup));
1794 log_msg(0,0,0,
"\n *** EMI mesh processing Done ***\n");
1800 MPI_Comm comm = mesh.
comm;
1802 double t1, t2, s1, s2;
1803 MPI_Comm_size(comm, &size); MPI_Comm_rank(comm, &rank);
1804 const int verb = param_globals::output_level;
1806 if(total_num_tags < size)
1808 PetscPrintf(PETSC_COMM_WORLD,
"\nThe number of processors should be less than the number of tags, size = %d & ntags = %d !!!\n",
1809 size, total_num_tags);
1813 if(verb==10)
log_msg(NULL, 0, 0,
"\ncompute the number of tags which belongs to one rank");
1817 compute_tags_per_rank(total_num_tags, ntags_per_rank);
1819 if(verb==10)
log_msg(NULL, 0, 0,
"Done in %f sec.",
float(t2 - t1));
1825 partition_based_tags(total_num_tags, mesh.
tag, ntags_per_rank, part_based_Tags);
1828 permute_mesh_locally_based_on_tag_elemIdx(mesh);
1830 if(verb==10)
log_msg(NULL, 0, 0,
"Done in %f sec.",
float(t2 - t1));
1835 void partition_based_tags(
int num_tags,
1840 const int verb = param_globals::output_level;
1843 MPI_Comm_size(comm, &size);
1844 MPI_Comm_rank(comm, &rank);
1850 if (!load_partitions_from_file(tags_to_rank_map, num_tags, comm)) {
1851 if(verb==10)
log_msg(NULL, 0, 0,
"\ncompute the number of unique tags");
1853 double t1 = MPI_Wtime();
1854 extract_unique_tag(unique_tags);
1855 double t2 = MPI_Wtime();
1856 if(verb==10)
log_msg(NULL, 0, 0,
"Done in %f sec.",
float(t2 - t1));
1858 if (unique_tags.
size() !=
static_cast<size_t>(num_tags)) {
1859 log_msg(0,5,0,
"\nerror: the number of tags in the EMI mesh does not match the total tags from *.extra and *.intra");
1862 map_tags_to_rank(size, unique_tags, num_tags_per_rank, tags_to_rank_map);
1865 for (
size_t i = 0; i < part.
size(); ++i) {
1866 if(tags_to_rank_map.
count(tag[i]))
1867 part[i] = tags_to_rank_map[tag[i]];
1875 for (
size_t r = 0; r < size; ++r) {
1876 for (
size_t count = 0;
count < num_tags_per_rank[r];) {
1880 tags_to_rank_map.
insert({tag, r});
1889 int expected_num_tags,
1895 const std::string basename = param_globals::meshname;
1896 FILE* fd = fopen((basename +
".part").c_str(),
"r");
1902 if (parts.
size() == 0)
return false;
1904 if (parts.
size() % 2 != 0) {
1905 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.!");
1909 for(
int i = 0; i < parts.
size(); i+=2) {
1910 tags_to_rank_map.
insert({parts[i], parts[i + 1]});
1913 if (tags_to_rank_map.
size() !=
static_cast<size_t>(expected_num_tags)) {
1914 log_msg(0,5,0,
"\nerror: the number of tags in the .part file does not match the total tags from *.extra and *.intra");
#define SF_COMM
the default SlimFem MPI communicator
double SF_real
Use the general double as real 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.
Container for a PETSc VecScatter.
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.
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)
size_t root_count_ascii_lines(std::string file, MPI_Comm comm)
count the lines in a ascii file
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.
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)
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.
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.
const char * get_mesh_type_name(mesh_t t)
get a char* to the name of a mesh type
void region_mask(mesh_t meshspec, SF::vector< RegionSpecs > ®spec, SF::vector< int > ®ionIDs, bool mask_elem, const char *reglist)
classify elements/points as belonging to a region
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,...)
mesh_t
The enum identifying the different meshes we might want to load.
@ 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
int get_size(MPI_Comm comm=PETSC_COMM_WORLD)
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.