File: cs_lagr_extract.c

package info (click to toggle)
code-saturne 4.3.3%2Brepack-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 77,992 kB
  • sloc: ansic: 281,257; f90: 122,305; python: 56,490; makefile: 3,915; xml: 3,285; cpp: 3,183; sh: 1,139; lex: 176; yacc: 101; sed: 16
file content (570 lines) | stat: -rw-r--r-- 17,524 bytes parent folder | download
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
/*============================================================================
 * Extract information from lagrangian particles.
 *============================================================================*/

/*
  This file is part of Code_Saturne, a general-purpose CFD tool.

  Copyright (C) 1998-2016 EDF S.A.

  This program is free software; you can redistribute it and/or modify it under
  the terms of the GNU General Public License as published by the Free Software
  Foundation; either version 2 of the License, or (at your option) any later
  version.

  This program is distributed in the hope that it will be useful, but WITHOUT
  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
  details.

  You should have received a copy of the GNU General Public License along with
  this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
  Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

/*----------------------------------------------------------------------------*/

#include "cs_defs.h"

/*----------------------------------------------------------------------------
 * Standard C library headers
 *----------------------------------------------------------------------------*/

#include <assert.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/*----------------------------------------------------------------------------
 * Local headers
 *----------------------------------------------------------------------------*/

#include "bft_mem.h"
#include "bft_printf.h"

#include "cs_base.h"
#include "cs_field.h"
#include "cs_log.h"
#include "cs_mesh.h"
#include "cs_parall.h"
#include "cs_lagr_tracking.h"
#include "cs_prototypes.h"
#include "cs_selector.h"
#include "cs_timer.h"
#include "cs_time_step.h"

/*----------------------------------------------------------------------------
 * Header for the current file
 *----------------------------------------------------------------------------*/

#include "cs_lagr_extract.h"

/*----------------------------------------------------------------------------*/

BEGIN_C_DECLS

/*=============================================================================
 * Additional Doxygen documentation
 *============================================================================*/

/*!
  \file cs_lagr_extract.c

  \brief Extract information from lagrangian particles.
*/

/*! \cond DOXYGEN_SHOULD_SKIP_THIS */

/*============================================================================
 * Type definitions
 *============================================================================*/

/*============================================================================
 * Static global variables
 *============================================================================*/

/*============================================================================
 * Private function definitions
 *============================================================================*/

/*! (DOXYGEN_SHOULD_SKIP_THIS) \endcond */

/*============================================================================
 * Public function definitions
 *============================================================================*/

/*----------------------------------------------------------------------------*/
/*!
 * \brief Get the local number of particles.
 *
 * \return  current number of particles.
 */
/*----------------------------------------------------------------------------*/

cs_lnum_t
cs_lagr_get_n_particles(void)
{
  cs_lnum_t retval = 0;

  const cs_lagr_particle_set_t  *p_set = cs_glob_lagr_particle_set;
  if (p_set != NULL)
    retval = p_set->n_particles;

  return retval;
}

/*----------------------------------------------------------------------------*/
/*!
 * \brief Extract a list of particles using an optional cell filter and
 *        statistical density filter.
 *
 * The output array must have been allocated by the caller and be of
 * sufficient size.
 *
 * \param[in]   n_cells          number of cells in filter
 * \param[in]   cell_list        optional list of containing cells filter
 *                               (1 to n numbering)
 * \param[in]   density          if < 1, fraction of particles to select
 * \param[out]  n_particles      number of selected particles, or NULL
 * \param[out]  particle_list    particle_list (1 to n numbering), or NULL
 */
/*----------------------------------------------------------------------------*/

void
cs_lagr_get_particle_list(cs_lnum_t         n_cells,
                          const cs_lnum_t   cell_list[],
                          double            density,
                          cs_lnum_t        *n_particles,
                          cs_lnum_t        *particle_list)
{
  cs_lnum_t i;

  ptrdiff_t  displ = 0;

  cs_lnum_t p_count = 0;

  bool *cell_flag = NULL;

  const cs_mesh_t *mesh = cs_glob_mesh;

  const cs_lagr_particle_set_t  *p_set = cs_glob_lagr_particle_set;

  assert(p_set != NULL);

  size_t extents = p_set->p_am->extents;

  if (density < 1) {

    size_t  _extents, size;
    cs_datatype_t  datatype;
    int  count;

    cs_lagr_get_attr_info(p_set,
                          0,
                          CS_LAGR_RANDOM_VALUE,
                          &_extents, &size, &displ,
                          &datatype, &count);

    assert(   (displ > 0 && count == 1 && datatype == CS_REAL_TYPE)
           || (displ < 0 && count == 0 && datatype == CS_DATATYPE_NULL));

  }

  /* Case where we have a filter */

  if (n_cells < mesh->n_cells) {

    /* Build cel filter */

    BFT_MALLOC(cell_flag, mesh->n_cells, bool);

    for (i = 0; i < mesh->n_cells; i++)
      cell_flag[i] = false;

    if (cell_list != NULL) {
      for (i = 0; i < n_cells; i++)
        cell_flag[cell_list[i] - 1] = true;
    }
    else {
      for (i = 0; i < n_cells; i++)
        cell_flag[i] = true;
    }

  }

  /* Loop on particles */

  for (i = 0; i < p_set->n_particles; i++) {

    /* If density < 1, randomly select which particles are added;
       normally, particles maintain a random_value for this purpose,
       but we also plan for the case where this could be optional. */

    if (density < 1) {
      double r;
      if (displ < 0)
        r = (double)rand() / RAND_MAX;
      else {
        const unsigned char
          *p = (const unsigned char *)(p_set->p_buffer + i*extents) + displ;
        r = *(const cs_real_t *)p;
      }
      if (r > density)
        continue;
    }

    /* Check for filter cell */

    if (cell_flag != NULL) {
      cs_lnum_t cur_cell_num
        = cs_lagr_particles_get_lnum(p_set, i, CS_LAGR_CELL_NUM);
      cs_lnum_t  cell_id = CS_ABS(cur_cell_num) - 1;
      if (cell_flag[cell_id] == false)
        continue;
    }

    if (particle_list != NULL)
      particle_list[p_count] = i+1;

    p_count += 1;

  }

  if (cell_flag != NULL)
    BFT_FREE(cell_flag);

  *n_particles = p_count;
}

/*----------------------------------------------------------------------------*/
/*!
 * \brief Extract values for a set of particles.
 *
 * The output array must have been allocated by the caller and be of
 * sufficient size.
 *
 * \param[in]   particles             associated particle set
 * \param[in]   attr                  attribute whose values are required
 * \param[in]   datatype              associated value type
 * \param[in]   stride                number of values per particle
 * \param[in]   component_id          if -1 : extract the whole attribute
 *                                    if >0 : id of the component to extract
 * \param[in]   n_particles           number of particles in filter
 * \param[in]   particle_list         particle_list (1 to n numbering), or NULL
 * \param[out]  values                particle values for given attribute
 *
 * \return 0 in case of success, 1 if attribute is not present
 */
/*----------------------------------------------------------------------------*/

int
cs_lagr_get_particle_values(const cs_lagr_particle_set_t  *particles,
                            cs_lagr_attribute_t            attr,
                            cs_datatype_t                  datatype,
                            int                            stride,
                            int                            component_id,
                            cs_lnum_t                      n_particles,
                            const cs_lnum_t                particle_list[],
                            void                          *values)
{
  size_t j;
  cs_lnum_t i;

  size_t  extents, size, _length;
  ptrdiff_t  displ;
  cs_datatype_t _datatype;
  int  _count;
  unsigned char *_values = values;

  assert(particles != NULL);

  cs_lagr_get_attr_info(particles, 0, attr,
                        &extents, &size, &displ, &_datatype, &_count);

  if (_count == 0)
    return 1;
  else {
    if (component_id == -1)
      _length = size;
    else
      _length = size/_count;
  }

  /* Check consistency */

  if (datatype != _datatype || stride != _count) {
    char attr_name[32];
    const char *_attr_name = attr_name;
    if (attr < CS_LAGR_N_ATTRIBUTES)
      _attr_name = cs_lagr_attribute_name[attr];
    else {
      snprintf(attr_name, 31, "%d", (int)attr);
      attr_name[31] = '\0';
    }
    bft_error(__FILE__, __LINE__, 0,
              _("Attribute %s is of datatype %s and stride %d\n"
                "but %s and %d were requested."),
              _attr_name,
              cs_datatype_name[_datatype], _count,
              cs_datatype_name[datatype], stride);
    return 1;
  }

  /* Check component_id */
  if (component_id != -1) {
    if (component_id < 0 || component_id >= stride) {
      char attr_name[32];
      const char *_attr_name = attr_name;
      if (attr < CS_LAGR_N_ATTRIBUTES)
        _attr_name = cs_lagr_attribute_name[attr];
      else {
        snprintf(attr_name, 31, "%d", (int)attr);
        attr_name[31] = '\0';
      }
      bft_error(__FILE__, __LINE__, 0,
                _("Attribute %s has a number of components equal to %d\n"
                  "but component %d is requested."),
                _attr_name,
                stride,
                component_id);
      return 1;
    }
  }

  /* No offset if export of the whole attribute */
  if (component_id == -1)
    component_id = 0;


  /* Case where we have no filter */

  if (particle_list == NULL) {
    for (i = 0; i < n_particles; i++) {
      unsigned char *dest = _values + i*_length;
      const unsigned char
        *src = (const unsigned char *)(particles->p_buffer + i*extents)
               + displ
               + component_id * _length;
      for (j = 0; j < _length; j++)
        dest[j] = src[j];
    }
  }

  /* Case where we have a filter list */
  else {
    for (i = 0; i < n_particles; i++) {
      cs_lnum_t p_id = particle_list[i] - 1;
      unsigned char *dest = _values + i*_length;
      const unsigned char
        *src = (const unsigned char *)(particles->p_buffer + p_id*extents)
               + displ
               + component_id * _length;
      for (j = 0; j < _length; j++)
        dest[j] = src[j];
    }
  }

  return 0;
}

/*----------------------------------------------------------------------------*/
/*!
 * \brief Extract trajectory values for a set of particles.
 *
 * Trajectories are defined as a mesh of segments, whose start and end
 * points are copied in an interleaved manner in the segment_values array
 * (p1_old, p1_new, p2_old, p2_new, ... pn_old, pn_new).
 *
 * The output array must have been allocated by the caller and be of
 * sufficient size.
 *
 * \param[in]   particles        associated particle set
 * \param[in]   attr             attribute whose values are required
 * \param[in]   datatype         associated value type
 * \param[in]   stride           number of values per particle
 * \param[in]   component_id     if -1 : extract the whole attribute
 *                               if >0 : id of the component to extract
 * \param[in]   n_particles      number of particles in filter
 * \param[in]   particle_list    particle_list (1 to n numbering), or NULL
 * \param[out]  segment_values   particle segment values
 *
 * \return 0 in case of success, 1 if attribute is not present
 */
/*----------------------------------------------------------------------------*/

int
cs_lagr_get_trajectory_values(const cs_lagr_particle_set_t  *particles,
                              cs_lagr_attribute_t            attr,
                              cs_datatype_t                  datatype,
                              int                            stride,
                              int                            component_id,
                              cs_lnum_t                      n_particles,
                              const cs_lnum_t                particle_list[],
                              void                          *segment_values)
{
  size_t j;
  cs_lnum_t i;

  size_t  extents, size, _length;
  ptrdiff_t  displ, displ_p;
  cs_datatype_t _datatype;
  int  _count;
  unsigned char *_values = segment_values;

  const unsigned char *p_buffer = particles->p_buffer;

  assert(particles != NULL);

  cs_lagr_get_attr_info(particles, 0, attr,
                        &extents, &size, &displ, &_datatype, &_count);

  if (_count == 0)
    return 1;
  else {
    if (component_id == -1)
      _length = size;
    else
      _length = size/_count;
  }

  if (particles->p_am->count[1][attr] > 0)
    cs_lagr_get_attr_info(particles, 1, attr,
                          &extents, NULL, &displ_p, NULL, NULL);

  /* Check consistency */

  if (datatype != _datatype || stride != _count) {
    char attr_name[32];
    const char *_attr_name = attr_name;
    if (attr < CS_LAGR_N_ATTRIBUTES)
      _attr_name = cs_lagr_attribute_name[attr];
    else {
      snprintf(attr_name, 31, "%d", (int)attr);
      attr_name[31] = '\0';
    }
    bft_error(__FILE__, __LINE__, 0,
              _("Attribute %s is of datatype %s and stride %d\n"
                "but %s and %d were requested."),
              _attr_name,
              cs_datatype_name[_datatype], _count,
              cs_datatype_name[datatype], stride);
    return 1;
  }

  /* Check component_id */
  if (component_id != -1) {
    if (component_id < 0 || component_id >= stride) {
      char attr_name[32];
      const char *_attr_name = attr_name;
      if (attr < CS_LAGR_N_ATTRIBUTES)
        _attr_name = cs_lagr_attribute_name[attr];
      else {
        snprintf(attr_name, 31, "%d", (int)attr);
        attr_name[31] = '\0';
      }
      bft_error(__FILE__, __LINE__, 0,
                _("Attribute %s has a number of components equal to %d\n"
                  "but component %d is requested."),
                _attr_name,
                stride,
                component_id);
      return 1;
    }
  }

  /* No offset if export of the whole attribute */
  if (component_id == -1)
    component_id = 0;

  /* Case where we have no filter */

  if (particle_list == NULL) {

    if (particles->p_am->count[1][attr] > 0) {

      for (i = 0; i < n_particles; i++) {
        unsigned char *dest = _values + i*_length*2;
        const unsigned char
          *src = (const unsigned char *)(p_buffer + i*extents)
                  + displ
                  + component_id * _length;
        const unsigned char
          *srcp = (const unsigned char *)(p_buffer + i*extents)
                   + displ_p
                   + component_id * _length;
        for (j = 0; j < _length; j++) {
          dest[j] = src[j];
          dest[j + _length] = srcp[j];
        }

      }

    }
    else { /* With no previous value available; copy current value */

      for (i = 0; i < n_particles; i++) {
        unsigned char *dest = _values + i*_length*2;
        const unsigned char
          *src = (const unsigned char *)(p_buffer + i*extents)
                  + displ
                  + component_id * _length;
        for (j = 0; j < _length; j++) {
          dest[j] = src[j];
          dest[j + _length] = src[j];
        }

      }

    }
  }

  /* Case where we have a filter list */
  else {

    if (particles->p_am->count[1][attr] > 0) {

      for (i = 0; i < n_particles; i++) {
        cs_lnum_t p_id = particle_list[i] - 1;
        unsigned char *dest = _values + i*_length*2;
        const unsigned char
          *src = (const unsigned char *)(p_buffer + p_id*extents)
                  + displ
                  + component_id * _length;
        const unsigned char
          *srcp = (const unsigned char *)(p_buffer + p_id*extents)
                   + displ_p
                   + component_id * _length;
        for (j = 0; j < _length; j++) {
          dest[j] = src[j];
          dest[j + _length] = srcp[j];
        }
      }

    }

    else { /* With no previous value available; copy current value */

      for (i = 0; i < n_particles; i++) {
        cs_lnum_t p_id = particle_list[i] - 1;
        unsigned char *dest = _values + i*_length*2;
        const unsigned char
          *src = (const unsigned char *)(p_buffer + p_id*extents)
                  + displ
                  + component_id * _length;
        for (j = 0; j < _length; j++) {
          dest[j] = src[j];
          dest[j + _length] = src[j];
        }
      }

    }

  }

  return 0;
}

/*----------------------------------------------------------------------------*/

END_C_DECLS