14 #include "petsc_utils.h"
24 memcpy( (
char*)sl->
data+sl->
nitems*quantum, p, quantum );
30 if (!old_str)
return NULL;
32 size_t len = strlen(old_str);
33 char *new_str = (
char *)calloc(len+1,
sizeof(
char));
34 strcpy(new_str, old_str);
41 char *rs = (
char *)malloc(256);
42 snprintf(rs, 255,
"%.1f", r);
49 size_t i = path.rfind(sep, path.length());
50 if (i != std::string::npos) {
51 return path.substr(i+1, path.length() - i);
65 const int rank = mpi_ready ?
get_rank() : 0;
70 snprintf(fmsg,
sizeof fmsg,
"L%d ", level );
72 snprintf(fmsg+strlen(fmsg),
sizeof fmsg,
"[P%d] ", rank );
79 vsnprintf(fmsg+strlen(fmsg),
MAX_MESG_LEN-strlen(fmsg)-1, fmt, ap);
86 if(flag&
SYNCED && mpi_ready && out->
fd)
87 FPRINTF_SYNC(WORLD out->
fd,
"%s", fmsg);
88 else if(!mpi_ready || !rank || (flag&
LOCAL)) {
89 fprintf(out->
fd,
"%s", fmsg);
94 if(out && flag &
FLUSH) {
95 if(flag&
SYNCED && mpi_ready && out->
fd)
96 PRINTF_FLUSH(COMM_W, out->
fd);
102 if((flag&
ECHO) && fmt) {
103 if(flag&
SYNCED && mpi_ready)
104 FPRINTF_SYNC(WORLD level ? stderr : stdout,
"%s", fmsg);
105 else if(!mpi_ready || !rank || (flag&
LOCAL))
106 fprintf(level ? stderr : stdout,
"%s", fmsg);
111 if(flag&
SYNCED && mpi_ready)
112 PRINTF_FLUSH(COMM_W, level ? stderr : stdout);
114 fflush(level ? stderr : stdout);
120 return access(fname, F_OK) != -1;
128 bool openfail =
false;
131 f->
fd = fopen(fname,mode);
138 char current_workdir[2048]; getcwd(current_workdir, 2048);
140 fprintf(stderr,
"Error, failed to open file: \"%s/%s\"!\n", current_workdir, fname);
141 fprintf(stderr,
"%s\n", strerror(error));
162 fread(ptr, size, nmemb, stream->
fd);
164 MPI_Bcast(ptr, size*nmemb, MPI_BYTE, 0, comm);
172 if(rank == source_pid) {
173 MPI_Send(&nmemb,
sizeof(
size_t), MPI_BYTE, 0, 100, comm);
174 MPI_Send(ptr, nmemb*size, MPI_BYTE, 0, 100, comm);
180 MPI_Recv(&nmemb,
sizeof(
size_t), MPI_BYTE, source_pid, 100, comm, &status);
182 MPI_Recv(wbuff.
data(), size*nmemb, MPI_BYTE, source_pid, 100, comm, &status);
183 fwrite(wbuff.
data(), size, nmemb, stream->
fd);
196 fgets(s, size, stream->
fd);
199 MPI_Bcast(s, size, MPI_CHAR, 0, comm);
201 int str_len = strlen(s);
203 if(str_len)
return s;
212 fwrite( &len,
sizeof(
int), 1, out->
fd);
213 fwrite( s,
sizeof(
char), len, out->
fd);
219 fread( &len,
sizeof(
int), 1, in->
fd );
220 char *s = (
char*)malloc( len+1 );
221 fread( s,
sizeof(
char), len, in->
fd );
231 char *s = (
char*) malloc(len+1);
242 bool inside_x = p.
x >= shape.
p0.
x && p.
x <= shape.
p1.
x;
243 bool inside_y = p.
y >= shape.
p0.
y && p.
y <= shape.
p1.
y;
244 bool inside_z = p.
z >= shape.
p0.
z && p.
z <= shape.
p1.
z;
245 return (inside_x && inside_y && inside_z);
254 Point center_to_point = p - shape.
p0;
256 double h =
mag(height);
259 double height_projection =
dot(height, center_to_point);
261 if(height_projection >= 0 && height_projection <= h) {
262 center_to_point -= (height * height_projection);
281 char *p = (
char *)&i;
295 FILE* fd = fopen(file,
"r");
310 if(strlen(path) && path[0] ==
'/')
return true;
316 #ifdef USE_FMEM_WRAPPER
324 static fpos_t SeekFn(
void *handler, fpos_t offset,
int whence) {
326 fmem_t *mem = (fmem_t*)handler;
330 if (offset > 0 && (
size_t)offset <= mem->size)
331 return mem->pos = offset;
334 if (mem->pos + offset <= mem->size)
335 return mem->pos = offset;
339 if (mem->size + offset <= mem->size)
340 return pos = mem->size + offset;
354 static int ReadFn(
void *handler,
char *buf,
int size) {
356 fmem_t *mem = (fmem_t*)handler;
359 if (size < 0)
return - 1;
364 while (
count < (
size_t)size)
365 buf[
count++] = mem->buffer[mem->pos++];
377 static int WriteFn(
void *handler,
const char *buf,
int size) {
379 fmem_t *mem = (fmem_t*)handler;
382 if (size < 0)
return - 1;
387 while (
count < (
size_t)size)
388 mem->buffer[mem->pos++] = buf[
count++];
398 static int CloseFn(
void *handler) {
411 FILE *
fmemopen_(
void *buf,
size_t size,
const char *mode) {
412 fmem_t *mem = (fmem_t *) malloc(
sizeof(fmem_t));
414 memset(mem, 0,
sizeof(fmem_t));
415 mem->size = size, mem->buffer = (
char*)buf;
417 return funopen(mem, ReadFn, WriteFn, SeekFn, CloseFn);
Basic utility structs and functions, mostly IO related.
A vector storing arbitrary data.
T * data()
Pointer to the vector's start.
class to store shape definitions
void count(const vector< T > &data, vector< S > &cnt)
Count number of occurrences of indices.
void sltlst_append(Salt_list *sl, void *p, int quantum)
char * f_gets_par(char *s, int size, FILE_SPEC stream, MPI_Comm comm)
V dot(const vec3< V > &p1, const vec3< V > &p2)
bool point_in_shape(const Point &p, const geom_shape &shape)
test if a point is inside a simple geometric shape
char * stringify(double r)
char * read_bin_string(FILE_SPEC in)
void f_read_par(void *ptr, size_t size, size_t nmemb, FILE_SPEC stream, MPI_Comm comm)
Parallel fread. Root reads, then broadcasts.
bool file_can_be_opened(const char *file)
Check wheterh a file can be opened for reading.
int get_rank(MPI_Comm comm=PETSC_COMM_WORLD)
T get_global(T in, MPI_Op OP, MPI_Comm comm=PETSC_COMM_WORLD)
Do a global reduction on a variable.
void write_bin_string(FILE_SPEC out, const char *s)
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.
FILE_SPEC f_open(const char *fname, const char *mode)
Open a FILE_SPEC.
bool path_is_absolute(const char *path)
check whether path is absolute
V mag(const vec3< V > &vect)
char * read_bin_string_par(FILE_SPEC in)
char * dupstr(const char *old_str)
V dist_2(const vec3< V > &p1, const vec3< V > &p2)
void log_msg(FILE_SPEC out, int level, unsigned char flag, const char *fmt,...)
bool f_exist(const char *fname)
std::string get_basename(const std::string &path)
void f_close(FILE_SPEC &f)
Close a FILE_SPEC.
V mag2(const vec3< V > &vect)
saltatory list – memory is allocated in chunks
int chunk
allocate memory to hold this many items
int size
size of list items
int nitems
number of items