openCARP
Doxygen code documentation for the open cardiac electrophysiology simulator openCARP
basics.cc
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (c) NumeriCor GmbH
2 // SPDX-License-Identifier: Apache-2.0
3 
12 #include "basics.h"
13 
14 namespace opencarp {
15 
16 void sltlst_append( Salt_list* sl, void *p, int quantum )
17 {
18  if( !sl->chunk ) sl->chunk = 1;
19 
20  if( !(sl->nitems%sl->chunk) )
21  sl->data = realloc( sl->data, sl->chunk*(quantum*(sl->nitems/sl->chunk+1)+1) );
22  memcpy( (char*)sl->data+sl->nitems*quantum, p, quantum );
23  sl->nitems++;
24  sl->size = quantum;
25 }
26 
27 char* dupstr(const char* old_str) {
28  if (!old_str) return NULL;
29 
30  size_t len = strlen(old_str);
31  char *new_str = (char *)calloc(len+1, sizeof(char));
32  strcpy(new_str, old_str);
33 
34  return new_str;
35 }
36 
37 char* stringify(double r)
38 {
39  char *rs = (char *)malloc(256);
40  snprintf(rs, 255, "%.1f", r);
41  return rs;
42 }
43 
44 std::string get_basename(const std::string & path)
45 {
46  char sep = '/';
47  size_t i = path.rfind(sep, path.length());
48  if (i != std::string::npos) {
49  return path.substr(i+1, path.length() - i);
50  }
51 
52  return path;
53 }
54 
55 void log_msg(FILE_SPEC out, int level, unsigned char flag, const char *fmt, ...)
56 {
57  assert(level>=0 && level<=MAX_LOG_LEVEL);
58 
59  if(!out)
60  flag |= ECHO;
61 
62  const bool mpi_ready = mpi_runtime_ready();
63  const int rank = mpi_ready ? get_rank() : 0;
64 
65  // create the message string
66  char fmsg[MAX_MESG_LEN] = "";
67  if(level)
68  snprintf(fmsg, sizeof fmsg, "L%d ", level );
69  if((flag&LOCAL) || (flag&SYNCED))
70  snprintf(fmsg+strlen(fmsg), sizeof fmsg, "[P%d] ", rank );
71  if(level || (flag&LOCAL) || (flag&SYNCED))
72  strcat(fmsg, ": ");
73 
74  if(fmt) {
75  va_list ap;
76  va_start(ap, fmt);
77  vsnprintf(fmsg+strlen(fmsg), MAX_MESG_LEN-strlen(fmsg)-1, fmt, ap);
78  if(!(flag&NONL))
79  strcat(fmsg, "\n");
80  }
81 
82  // write to file
83  if(out && fmt) {
84  if(!mpi_ready || !rank || (flag&(LOCAL|SYNCED))) {
85  fprintf(out->fd, "%s", fmsg);
86  }
87  }
88 
89  // flush file
90  if(out && flag & FLUSH) {
91  fflush(out->fd);
92  }
93 
94  // write to screen
95  if((flag&ECHO) && fmt) {
96  if(!mpi_ready || !rank || (flag&(LOCAL|SYNCED)))
97  fprintf(level ? stderr : stdout, "%s", fmsg);
98  }
99 
100  // flush screen
101  if((flag&ECHO) && ((flag & FLUSH) || level == MAX_LOG_LEVEL)) {
102  fflush(level ? stderr : stdout);
103  }
104 }
105 
106 bool f_exist(const char *fname)
107 {
108  return access(fname, F_OK) != -1;
109 }
110 
111 FILE_SPEC f_open(const char *fname, const char *mode)
112 {
113  FILE_SPEC f = new file_desc();
114  f->name = dupstr(fname);
115 
116  bool openfail = false;
117  int error = 0;
118 
119  f->fd = fopen(fname,mode);
120  if(f->fd == NULL) {
121  openfail = true;
122  error = errno;
123  }
124 
125  if(openfail) {
126  char current_workdir[2048]; getcwd(current_workdir, 2048);
127 
128  fprintf(stderr, "Error, failed to open file: \"%s/%s\"!\n", current_workdir, fname);
129  fprintf(stderr, "%s\n", strerror(error));
130 
131  delete f;
132  return NULL;
133  }
134 
135  return f;
136 }
137 
139 {
140  if(f != NULL) {
141  fclose(f->fd);
142  delete f;
143  f = NULL;
144  }
145 }
146 
147 void f_read_par(void *ptr, size_t size, size_t nmemb, FILE_SPEC stream, MPI_Comm comm)
148 {
149  if(!get_rank())
150  fread(ptr, size, nmemb, stream->fd);
151 
152  MPI_Bcast(ptr, size*nmemb, MPI_BYTE, 0, comm);
153 }
154 
155 void f_write_par(void* ptr, size_t size, size_t nmemb, int source_pid, FILE_SPEC stream,
156  MPI_Comm comm)
157 {
158  int rank = get_rank();
159 
160  if(rank == source_pid) {
161  MPI_Send(&nmemb, sizeof(size_t), MPI_BYTE, 0, 100, comm);
162  MPI_Send(ptr, nmemb*size, MPI_BYTE, 0, 100, comm);
163  }
164 
165  if(rank == 0) {
166  MPI_Status status;
167 
168  MPI_Recv(&nmemb, sizeof(size_t), MPI_BYTE, source_pid, 100, comm, &status);
169  SF::vector<char> wbuff(nmemb*size);
170  MPI_Recv(wbuff.data(), size*nmemb, MPI_BYTE, source_pid, 100, comm, &status);
171  fwrite(wbuff.data(), size, nmemb, stream->fd);
172  }
173 }
174 
175 char* f_gets_par(char* s, int size, FILE_SPEC stream, MPI_Comm comm)
176 {
177  int rank = get_rank();
178 
179  // we want to be able to detect if a read happend by only evaluating s, not also the
180  // return type of fgets. as such we make sure that strlen(s) == 0 if no read occured.
181  s[0] = '\0';
182 
183  if(rank == 0) {
184  fgets(s, size, stream->fd);
185  }
186 
187  MPI_Bcast(s, size, MPI_CHAR, 0, comm);
188 
189  int str_len = strlen(s);
190 
191  if(str_len) return s;
192  else return NULL;
193 }
194 
195 void write_bin_string(FILE_SPEC out, const char *s)
196 {
197  if( !s ) return;
198 
199  int len = strlen(s);
200  fwrite( &len, sizeof(int), 1, out->fd);
201  fwrite( s, sizeof(char), len, out->fd);
202 }
203 
205 {
206  int len;
207  fread( &len, sizeof(int), 1, in->fd );
208  char *s = (char*)malloc( len+1 );
209  fread( s, sizeof(char), len, in->fd );
210  s[len] = '\0';
211 
212  return s;
213 }
214 
216 {
217  int len;
218  f_read_par(&len, sizeof(int), 1, in);
219  char *s = (char*) malloc(len+1);
220  f_read_par(s, sizeof(char), len, in);
221  s[len] = '\0';
222 
223  return s;
224 }
225 
226 bool point_in_shape(const Point & p, const geom_shape & shape)
227 {
228  switch(shape.type) {
229  case geom_shape::block: {
230  bool inside_x = p.x >= shape.p0.x && p.x <= shape.p1.x;
231  bool inside_y = p.y >= shape.p0.y && p.y <= shape.p1.y;
232  bool inside_z = p.z >= shape.p0.z && p.z <= shape.p1.z;
233  return (inside_x && inside_y && inside_z);
234  }
235 
236  case geom_shape::sphere:
237  return (dist_2(p, shape.p0) <= (shape.radius * shape.radius));
238 
240  {
241  Point height = shape.p1 - shape.p0;
242  Point center_to_point = p - shape.p0;
243 
244  double h = mag(height);
245  height /= h;
246 
247  double height_projection = dot(height, center_to_point);
248 
249  if(height_projection >= 0 && height_projection <= h) {
250  center_to_point -= (height * height_projection);
251  return (mag2(center_to_point) <= (shape.radius * shape.radius));
252  }
253  else
254  return false;
255  }
256 /*
257  default:
258  log_msg(0,5,0, "%s error: Shape type not yet implemented.", __func__);
259  EXIT(1);
260 */
261  }
262 
263  return false;
264 }
265 
267 {
268  int i = 1;
269  char *p = (char *)&i;
270 
271  if (p[0] == 1)
272  return false;
273  else
274  return true;
275 }
276 
277 bool file_can_be_opened(const char* file)
278 {
279  int rank = get_rank();
280  int did_open = 0;
281 
282  if(rank == 0) {
283  FILE* fd = fopen(file, "r");
284  if(fd != NULL) {
285  did_open = 1;
286  fclose(fd);
287  }
288  }
289 
290  did_open = get_global(did_open, MPI_SUM);
291 
292  return did_open > 0;
293 }
294 
295 bool path_is_absolute(const char* path)
296 {
297  // we can be here more fancy / platform specific. -Aurel Jan. 27 2021
298  if(strlen(path) && path[0] == '/') return true;
299 
300  return false;
301 }
302 
303 
304 #ifdef USE_FMEM_WRAPPER
312 static fpos_t SeekFn(void *handler, fpos_t offset, int whence) {
313  size_t pos = 0;
314  fmem_t *mem = (fmem_t*)handler;
315 
316  switch (whence) {
317  case SEEK_SET:
318  if (offset > 0 && (size_t)offset <= mem->size)
319  return mem->pos = offset;
320  break;
321  case SEEK_CUR:
322  if (mem->pos + offset <= mem->size)
323  return mem->pos = offset;
324  break;
325  case SEEK_END:
326  /* must be negative */
327  if (mem->size + offset <= mem->size)
328  return pos = mem->size + offset;
329  break;
330  }
331 
332  return -1;
333 }
334 
342 static int ReadFn(void *handler, char *buf, int size) {
343  size_t count = 0;
344  fmem_t *mem = (fmem_t*)handler;
345  size_t available = mem->size - mem->pos;
346 
347  if (size < 0) return - 1;
348 
349  if ((size_t)size > available)
350  size = available;
351 
352  while (count < (size_t)size)
353  buf[count++] = mem->buffer[mem->pos++];
354 
355  return count;
356 }
357 
365 static int WriteFn(void *handler, const char *buf, int size) {
366  size_t count = 0;
367  fmem_t *mem = (fmem_t*)handler;
368  size_t available = mem->size - mem->pos;
369 
370  if (size < 0) return - 1;
371 
372  if ((size_t)size > available)
373  size = available;
374 
375  while (count < (size_t)size)
376  mem->buffer[mem->pos++] = buf[count++];
377 
378  return count;
379 }
380 
386 static int CloseFn(void *handler) {
387  free (handler);
388  return 0;
389 }
390 
391 
399 FILE *fmemopen_(void *buf, size_t size, const char *mode) {
400  fmem_t *mem = (fmem_t *) malloc(sizeof(fmem_t));
401 
402  memset(mem, 0, sizeof(fmem_t));
403  mem->size = size, mem->buffer = (char*)buf;
404 
405  return funopen(mem, ReadFn, WriteFn, SeekFn, CloseFn);
406 }
407 
408 #endif // USE_FMEM_WRAPPER
409 
410 } // namespace opencarp
Basic utility structs and functions, mostly IO related.
#define FLUSH
Definition: basics.h:305
#define MAX_LOG_LEVEL
Definition: basics.h:309
#define LOCAL
Definition: basics.h:303
#define MAX_MESG_LEN
Definition: basics.h:308
#define SYNCED
Definition: basics.h:304
#define ECHO
Definition: basics.h:302
#define NONL
Definition: basics.h:306
#define fmemopen_
Definition: basics.h:427
A vector storing arbitrary data.
Definition: SF_vector.h:28
T * data()
Pointer to the vector's start.
Definition: SF_vector.h:76
class to store shape definitions
Definition: basics.h:375
void count(const vector< T > &data, vector< S > &cnt)
Count number of occurrences of indices.
Definition: SF_vector.h:317
bool is_big_endian()
Definition: basics.cc:266
void sltlst_append(Salt_list *sl, void *p, int quantum)
Definition: basics.cc:16
char * f_gets_par(char *s, int size, FILE_SPEC stream, MPI_Comm comm)
Definition: basics.cc:175
V dot(const vec3< V > &p1, const vec3< V > &p2)
Definition: vect.h:111
bool point_in_shape(const Point &p, const geom_shape &shape)
test if a point is inside a simple geometric shape
Definition: basics.cc:226
char * stringify(double r)
Definition: basics.cc:37
char * read_bin_string(FILE_SPEC in)
Definition: basics.cc:204
bool file_can_be_opened(const char *file)
Check wheterh a file can be opened for reading.
Definition: basics.cc:277
T get_global(T in, MPI_Op OP, MPI_Comm comm=SF_COMM)
Do a global reduction on a variable.
Definition: basics.h:219
void f_write_par(void *ptr, size_t size, size_t nmemb, int source_pid, FILE_SPEC stream, MPI_Comm comm)
Write in parallel. Data comes from one rank, rank 0 writes.
Definition: basics.cc:155
void write_bin_string(FILE_SPEC out, const char *s)
Definition: basics.cc:195
FILE_SPEC f_open(const char *fname, const char *mode)
Open a FILE_SPEC.
Definition: basics.cc:111
bool path_is_absolute(const char *path)
check whether path is absolute
Definition: basics.cc:295
V mag(const vec3< V > &vect)
Definition: vect.h:117
char * read_bin_string_par(FILE_SPEC in)
Definition: basics.cc:215
bool mpi_runtime_ready()
Definition: basics.h:254
void f_read_par(void *ptr, size_t size, size_t nmemb, FILE_SPEC stream, MPI_Comm comm)
Parallel fread. Root reads, then broadcasts.
Definition: basics.cc:147
char * dupstr(const char *old_str)
Definition: basics.cc:27
V dist_2(const vec3< V > &p1, const vec3< V > &p2)
Definition: vect.h:88
void log_msg(FILE_SPEC out, int level, unsigned char flag, const char *fmt,...)
Definition: basics.cc:55
int get_rank(MPI_Comm comm=SF_COMM)
Definition: basics.h:270
bool f_exist(const char *fname)
Definition: basics.cc:106
std::string get_basename(const std::string &path)
Definition: basics.cc:44
void f_close(FILE_SPEC &f)
Close a FILE_SPEC.
Definition: basics.cc:138
V mag2(const vec3< V > &vect)
Definition: vect.h:124
saltatory list – memory is allocated in chunks
Definition: basics.h:45
int chunk
allocate memory to hold this many items
Definition: basics.h:47
void * data
the buffer
Definition: basics.h:46
int size
size of list items
Definition: basics.h:49
int nitems
number of items
Definition: basics.h:48
File descriptor struct.
Definition: basics.h:121
const char * name
Definition: basics.h:123