forked from rsachetto/MonoAlg3D_C
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmono
executable file
·594 lines (448 loc) · 13.8 KB
/
mono
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
#!/bin/bash
CUSTOM_FILE_NAME="custom_functions.c"
STIM_LIB_PATH="src/stimuli_library/"
CUSTOM_STIM_FILE="${STIM_LIB_PATH}""${CUSTOM_FILE_NAME}"
DEFAULT_STIM_FILE=${STIM_LIB_PATH}"stimuli.c"
DOMAIN_LIB_PATH="src/domains_library/"
CUSTOM_DOMAIN_FILE=${DOMAIN_LIB_PATH}"${CUSTOM_FILE_NAME}"
DEFAULT_DOMAIN_FILE=${DOMAIN_LIB_PATH}"domain.c"
EXTRA_DATA_LIB_PATH="src/extra_data_library/"
CUSTOM_EXTRA_DATA_FILE=${EXTRA_DATA_LIB_PATH}"${CUSTOM_FILE_NAME}"
DEFAULT_EXTRA_DATA_FILE=${EXTRA_DATA_LIB_PATH}"extra_data.c"
MATRIX_ASSEMBLY_LIB_PATH="src/matrix_assembly_library/"
CUSTOM_MATRIX_ASSEMBLY_FILE=${MATRIX_ASSEMBLY_LIB_PATH}"${CUSTOM_FILE_NAME}"
DEFAULT_MATRIX_ASSEMBLY_FILE=${MATRIX_ASSEMBLY_LIB_PATH}"matrix_assembly.c"
ECHOERR() { printf "%s\n" "$*" >&2; }
ADD_HEADER() {
model_full_path=$1
model_name=$2
include_guard="MONOALG3D_MODEL_${model_name^^}_H"
cat << EOF > "${model_full_path}/${model_name}.h"
#ifndef ${include_guard}
#define ${include_guard}
#include "../model_common.h"
#define NEQ COMPLETE_ME
#define INITIAL_V (COMPLETE_ME)
#include "../default_solvers.h"
#endif //${include_guard}
EOF
}
ADD_C_FILE() {
model_full_path=$1
model_name=$2
cat << EOF > "${model_full_path}/${model_name}.c"
#include "${model_name}.h"
#include <stdlib.h>
SET_ODE_INITIAL_CONDITIONS_CPU(set_model_initial_conditions_cpu) {
log_msg("Using ${model_name} CPU model\n");
uint32_t num_cells = solver->original_num_cells;
solver->sv = (real*)malloc(NEQ*num_cells*sizeof(real));
max_step = solver->max_dt;
min_step = solver->min_dt;
abstol = solver->abs_tol;
reltol = solver->rel_tol;
adpt = solver->adaptive;
if(adpt) {
ode_dt = (real*)malloc(num_cells*sizeof(real));
OMP(parallel for)
for(int i = 0; i < num_cells; i++) {
ode_dt[i] = solver->min_dt;
}
ode_previous_dt = (real*)calloc(num_cells, sizeof(real));
ode_time_new = (real*)calloc(num_cells, sizeof(real));
log_msg("Using Adaptive Euler model to solve the ODEs\n");
} else {
log_msg("Using Euler model to solve the ODEs\n");
}
OMP(parallel for)
for(uint32_t i = 0; i < num_cells; i++) {
real *sv = &solver->sv[i * NEQ];
sv[0] = INITIAL_V;
//TODO: implement the rest of the initial conditions
}
}
void RHS_cpu(const real *sv, real *rDY, real stim_current, real dt) {
//TODO: implement
#include "${model_name}_common.inc.c"
}
#include "../default_solvers.c"
EOF
}
ADD_CU_FILE() {
model_full_path=$1
model_name=$2
cat << EOF > "${model_full_path}/${model_name}.cu"
#include "${model_name}.h"
#include <stddef.h>
#include <stdint.h>
__constant__ size_t pitch;
__constant__ real abstol;
__constant__ real reltol;
__constant__ real max_dt;
__constant__ real min_dt;
__constant__ uint8_t use_adpt;
size_t pitch_h;
#define sv(i) *((real *)((char *)sv + pitch * (i)) + thread_id)
__global__ void kernel_set_model_initial_conditions(real *sv, int num_volumes, size_t pitch, bool use_adpt_dt, real min_dt) {
int thread_id = blockDim.x * blockIdx.x + threadIdx.x;
if (thread_id < num_volumes) {
sv(0) = INITIAL_V;
//TODO: implement the rest of the initial conditions
if(use_adpt) {
sv(NEQ) = min_dt; // dt
sv(NEQ+1) = 0.0; // time_new
sv(NEQ+2) = 0.0; // previous dt
}
}
}
inline __device__ void RHS_gpu(real *sv, real *rDY, real stim_current, int thread_id, real dt, size_t pitch, bool use_adpt) {
//State variables
if(use_adpt) {
//TODO: implement
} else {
//TODO: implement
}
#include "${model_name}_common.inc.c"
}
#include "../default_solvers.cu"
EOF
}
ADD_INC_FILE() {
model_full_path=$1
model_name=$2
cat << EOF > "${model_full_path}/${model_name}_common.inc.c"
//TODO: implement. This is where the math of the model needs to be written.
EOF
}
ADD_BUILD_FILE() {
model_full_path=$1
model_name=$2
model_folder=$3
cat << EOF > "${model_full_path}/build.sh"
############## ${model_name} ##############################
MODEL_FILE_CPU="${model_name}.c"
MODEL_FILE_GPU="${model_name}.cu"
COMMON_HEADERS="${model_name}.h"
COMPILE_MODEL_LIB "${model_name}" "\$MODEL_FILE_CPU" "\$MODEL_FILE_GPU" "\$COMMON_HEADERS"
EOF
}
ADD_TEMPLATE_FILES() {
ADD_HEADER "$1" "$2"
ADD_C_FILE "$1" "$2"
ADD_CU_FILE "$1" "$2"
ADD_INC_FILE "$1" "$2"
ADD_BUILD_FILE "$1" "$2" "$3"
}
CREATE_NEW_MODEL() {
model_folder=$1
model_name=$2
model_full_path=src/models_library/${model_folder}/
mkdir -p "$model_full_path"
ADD_TEMPLATE_FILES "$model_full_path" "$model_name" "$model_folder"
}
FUNCTION_EXISTS() {
local custom_file=$1
local default_file=$2
local search_pattern=$3
local function_name=$4
result=$(grep [[:blank:]]*"${search_pattern}"[[:blank:]]*\([[:blank:]]*${function_name}[[:blank:]]*\)[[:blank:]]* "${custom_file}")
result2=$(grep [[:blank:]]*"${search_pattern}"[[:blank:]]*\([[:blank:]]*${function_name}[[:blank:]]*\)[[:blank:]]* "${default_file}")
if [ -n "${result}" ] || [ -n "${result2}" ]; then
if [[ -z "${result}" ]]; then
ECHOERR "[WARN] Function ${function_name} already exists in ${default_file} skipping!"
else
ECHOERR "[WARN] Function ${function_name} already exists in ${custom_file} skipping!"
fi
echo "1"
fi
echo "0"
}
WRITE_DOMAIN_FILE_HEADER() {
cat << EOF > "${CUSTOM_DOMAIN_FILE}"
#include "domain_helpers.h"
#include "../3dparty/sds/sds.h"
#include "../3dparty/stb_ds.h"
#include "../config/domain_config.h"
#include "../config_helpers/config_helpers.h"
#include "../libraries_common/common_data_structures.h"
#include "../logger/logger.h"
#include "../utils/utils.h"
#include <assert.h>
#include <time.h>
#include <float.h>
#include <unistd.h>
EOF
}
WRITE_DOMAIN_FUNCTION() {
cat << EOF >> "${CUSTOM_DOMAIN_FILE}"
SET_SPATIAL_DOMAIN(${function_name}) {
return 1; //TODO: create a better example
}
EOF
}
CREATE_NEW_DOMAIN() {
function_name=$1
if [ ! -f "${CUSTOM_DOMAIN_FILE}" ]; then
WRITE_DOMAIN_FILE_HEADER
fi
res=$(FUNCTION_EXISTS "${CUSTOM_DOMAIN_FILE}" "${DEFAULT_DOMAIN_FILE}" "SET_SPATIAL_DOMAIN" "$function_name")
if [ "$res" == "0" ]; then
WRITE_DOMAIN_FUNCTION
fi
}
WRITE_STIM_FILE_HEADER() {
cat << EOF > "${CUSTOM_STIM_FILE}"
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include "../alg/grid/grid.h"
#include "../config/stim_config.h"
#include "../config_helpers/config_helpers.h"
#include "../utils/utils.h"
EOF
}
WRITE_STIM_FUNCTION() {
cat << EOF >> "${CUSTOM_STIM_FILE}"
SET_SPATIAL_STIM(${function_name}) {
uint32_t n_active = the_grid->num_active_cells;
struct cell_node **ac = the_grid->active_cells;
ALLOCATE_STIMS();
real stim_current = 0.0;
GET_PARAMETER_NUMERIC_VALUE_OR_REPORT_ERROR(real, stim_current, config, "current");
bool stim;
real stim_value;
uint32_t i;
OMP(parallel for private(stim, stim_value))
for(i = 0; i < n_active; i++) {
//TODO: define if a stimulus will be applied according to condition
//Ex:
// stim = ac[i]->center.x < 100;
// will apply a stimulus in each cell that has the coordinate x
// of its center < 100
if(stim) {
stim_value = stim_current;
} else {
stim_value = 0.0;
}
SET_STIM_VALUE(i, stim_value);
}
}
EOF
}
CREATE_NEW_STIM() {
function_name=$1
if [ ! -f "${CUSTOM_STIM_FILE}" ]; then
WRITE_STIM_FILE_HEADER
fi
res=$(FUNCTION_EXISTS "${CUSTOM_STIM_FILE}" "${DEFAULT_STIM_FILE}" "SET_SPATIAL_STIM" "$function_name")
if [ "$res" == "0" ]; then
WRITE_STIM_FUNCTION
fi
}
WRITE_EXTRA_DATA_FILE_HEADER() {
cat << EOF >> "${CUSTOM_EXTRA_DATA_FILE}"
#include <unistd.h>
#include "../config/extra_data_config.h"
#include "../config_helpers/config_helpers.h"
#include "../libraries_common/common_data_structures.h"
#include "../utils/file_utils.h"
EOF
}
WRITE_EXTRA_DATA_FUNCTION() {
cat << EOF >> "${CUSTOM_EXTRA_DATA_FILE}"
SET_EXTRA_DATA(${function_name}) {
//TODO: set the extra data size in bytes
*extra_data_size = 0;
//TODO: extra data can be a pointer of any type (you have to cast to void* before returning
void *extra_data = malloc(*extra_data_size);
return extra_data;
}
EOF
}
WRITE_MATRIX_ASSEMBLY_FILE_HEADER() {
cat << EOF >> "${CUSTOM_MATRIX_ASSEMBLY_FILE}"
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "../3dparty/sds/sds.h"
#include "../3dparty/stb_ds.h"
#include "../alg/grid/grid.h"
#include "../config/assembly_matrix_config.h"
#include "../libraries_common/common_data_structures.h"
#include "../utils/file_utils.h"
#include "../utils/utils.h"
#include "../domains_library/mesh_info_data.h"
#include "assembly_common.c"
#ifdef COMPILE_CUDA
#include "../gpu_utils/gpu_utils.h"
#endif
EOF
}
WRITE_MATRIX_ASSEMBLY_FUNCTION() {
cat << EOF >> "${CUSTOM_MATRIX_ASSEMBLY_FILE}"
ASSEMBLY_MATRIX(${function_name}) {
//TODO: implement
}
EOF
}
CREATE_NEW_EXTRA_DATA() {
function_name=$1
if [ ! -f "${CUSTOM_EXTRA_DATA_FILE}" ]; then
WRITE_EXTRA_DATA_FILE_HEADER
fi
res=$(FUNCTION_EXISTS "${CUSTOM_EXTRA_DATA_FILE}" "${DEFAULT_EXTRA_DATA_FILE}" "SET_EXTRA_DATA" "$function_name")
if [ "$res" == "0" ]; then
WRITE_EXTRA_DATA_FUNCTION
fi
}
CREATE_NEW_MATRIX_ASSEMBLY() {
function_name=$1
if [ ! -f "${CUSTOM_MATRIX_ASSEMBLY_FILE}" ]; then
WRITE_MATRIX_ASSEMBLY_FILE_HEADER
fi
res=$(FUNCTION_EXISTS "${CUSTOM_MATRIX_ASSEMBLY_FILE}" "${DEFAULT_MATRIX_ASSEMBLY_FILE}" "ASSEMBLY_MATRIX" "$function_name")
if [ "$res" == "0" ]; then
WRITE_MATRIX_ASSEMBLY_FUNCTION
fi
}
CREATE_NEW_CONFIG() {
local SIM_DIR=$1
local SIM_CONFIG_FILE="$SIM_DIR/$1_config.ini"
local SIM_OUTPUT_DIR="$SIM_DIR/output"
local STIM_F_NAME="$2"
local DOMAIN_F_NAME="$3"
local EXTRA_DATA_F_NAME="$4"
if [ ! -d "$SIM_DIR" ]; then
mkdir "$SIM_DIR"
else
ECHOERR "Simulation directory $SIM_DIR alread exists. Delete it or choose a new simulation name!"
exit 1
fi
cat << EOF >> "${SIM_CONFIG_FILE}"
[main]
num_threads=2
dt_pde=0.02
simulation_time=400.0
abort_on_no_activity=false
use_adaptivity=false
[update_monodomain]
main_function=update_monodomain_default
[save_result]
print_rate=1
output_dir=${SIM_OUTPUT_DIR}
main_function=save_as_vtu
init_function=init_save_as_vtk_or_vtu
end_function=end_save_as_vtk_or_vtu
file_prefix=V
[assembly_matrix]
init_function=set_initial_conditions_fvm
sigma_x=0.0000176
sigma_y=0.0001334
sigma_z=0.0000176
main_function=homogeneous_sigma_assembly_matrix
[linear_system_solver]
tolerance=1e-16
use_gpu=yes
max_iterations=200
main_function=conjugate_gradient
init_function=init_conjugate_gradient
end_function=end_conjugate_gradient
[domain]
name=$1 Mesh
main_function=$DOMAIN_F_NAME
[ode_solver]
dt=0.02
use_gpu=yes
gpu_id=0
library_file=shared_libs/libten_tusscher_2006.so
[stim_1]
start = 0.0
duration = 2.0
current = -50.0
main_function=$STIM_F_NAME
[extra_data]
main_function=$EXTRA_DATA_F_NAME
EOF
echo "The new configuration file was successfuly generated in $SIM_CONFIG_FILE."
}
COMMON_COMMAND_ERROR() {
ECHOERR "$1 command need 1 parameter (new $1 function name)"
ECHOERR "Example usage:"
ECHOERR "$0 my_custom_$1"
ECHOERR "This command will create or add a new $1 function in ${2}"
exit
}
MAIN() {
case $1 in
simulation)
if [ $# -ne 2 ]
then
ECHOERR "$1 command need 1 parameter (simulation name)"
exit
fi
local STIM_F_NAME="$2_stim_function"
local DOMAIN_F_NAME="$2_domain_function"
local EXTRA_DATA_F_NAME="$2_extra_data_function"
CREATE_NEW_CONFIG "$2" "$STIM_F_NAME" "$DOMAIN_F_NAME" "$EXTRA_DATA_F_NAME"
CREATE_NEW_STIM "$STIM_F_NAME"
CREATE_NEW_DOMAIN "$DOMAIN_F_NAME"
CREATE_NEW_EXTRA_DATA "$EXTRA_DATA_F_NAME"
;;
model)
if [ $# -ne 3 ]
then
ECHOERR "$1 command need 2 parameters (new model folder and new model name)"
ECHOERR "Example usage:"
ECHOERR "$0 ten_tuscher ten_tuscher_2009"
ECHOERR "This command will create a template model file called ten_tuscher_2009 in the src/models_library/ten_tuscher folder."
exit
fi
CREATE_NEW_MODEL "$2" "$3"
;;
stimulus)
if [ $# -ne 2 ]
then
COMMON_COMMAND_ERROR "$1" "$CUSTOM_STIM_FILE"
fi
CREATE_NEW_STIM "$2"
;;
domain)
if [ $# -ne 2 ]
then
COMMON_COMMAND_ERROR "$1" "$CUSTOM_DOMAIN_FILE"
fi
CREATE_NEW_DOMAIN "$2"
;;
extra_data)
if [ $# -ne 2 ]
then
COMMON_COMMAND_ERROR "$1" "$CUSTOM_EXTRA_DATA_FILE"
fi
CREATE_NEW_EXTRA_DATA "$2"
;;
matrix_assembly)
if [ $# -ne 2 ]
then
COMMON_COMMAND_ERROR "$1" "$CUSTOM_MATRIX_ASSEMBLY_FILE"
fi
CREATE_NEW_MATRIX_ASSEMBLY "$2"
;;
*)
ECHOERR
ECHOERR "Invalid command:" "$@"
ECHOERR
ECHOERR "Available commands: "
ECHOERR
ECHOERR "$0" "simulation simulation_name"
ECHOERR "$0" "model model_folder model_name"
ECHOERR "$0" "stimulus function_name"
ECHOERR "$0" "domain function_name"
ECHOERR "$0" "extra_data function_name"
ECHOERR "$0" "matrix_assembly function_name"
ECHOERR
;;
esac
}
MAIN "$@"