1 // =============================================================================
2 // PROJECT CHRONO - http://projectchrono.org
3 //
4 // Copyright (c) 2019 projectchrono.org
5 // All rights reserved.
6 //
7 // Use of this source code is governed by a BSD-style license that can be found
8 // in the LICENSE file at the top level of the distribution and at
9 // http://projectchrono.org/license-chrono.txt.
10 //
11 // =============================================================================
12 // Authors: Conlain Kelly, Nic Olsen, Dan Negrut
13 // =============================================================================
14
15 #pragma once
16
17 #include <stdio.h>
18 #include <stdlib.h>
19
20 #define CHGPU_ERROR(...) \
21 { \
22 printf(__VA_ARGS__); \
23 printf(__func__); \
24 printf("\n: EXITING GPU SIM.\n"); \
25 exit(1); \
26 }
27
28 // defined in ChGpu.cpp
29 extern size_t gran_approx_bytes_used;
30
31 // Add verbose checks easily
32 #define TRACK_VECTOR_RESIZE(vec, newsize, name, val) \
33 { \
34 size_t item_size = sizeof(decltype(vec)::value_type); \
35 size_t old_size = vec.size(); \
36 vec.resize(newsize, val); \
37 size_t new_size = vec.size(); \
38 size_t byte_delta = item_size * (new_size - old_size); \
39 gran_approx_bytes_used += byte_delta; \
40 INFO_PRINTF("Resizing vector %s, old size %zu, new size %zu, byte delta %s\n", name, old_size, new_size, \
41 pretty_format_bytes(byte_delta).c_str()); \
42 }
43
pretty_format_bytes(size_t bytes)44 inline std::string pretty_format_bytes(size_t bytes) {
45 // set up byte prefixes
46 constexpr size_t KIBI = 1024;
47 constexpr size_t MEBI = KIBI * KIBI;
48 constexpr size_t GIBI = KIBI * KIBI * KIBI;
49 float gibival = float(bytes) / GIBI;
50 float mebival = float(bytes) / MEBI;
51 float kibival = float(bytes) / KIBI;
52 std::stringstream ret;
53 if (gibival > 1) {
54 ret << gibival << " GiB";
55 } else if (mebival > 1) {
56 ret << mebival << " MiB";
57 } else if (kibival > 1) {
58 ret << kibival << " KiB";
59 } else {
60 ret << bytes << " B";
61 }
62 return ret.str();
63 }
64
65 // calculate effective youngs modulus and shear modulus
materialPropertyCombine(double Y1,double Y2,double nu1,double nu2,double & E_eff,double & G_eff)66 inline void materialPropertyCombine(double Y1, double Y2, double nu1, double nu2, double& E_eff, double& G_eff){
67 double invE = (1-nu1*nu1)/Y1 + (1 - nu2*nu2)/Y2;
68 E_eff = 1./invE;
69 double invG = 2*(2-nu1)*(1+nu1)/Y1 + 2*(2-nu2)*(1+nu2)/Y2;
70 G_eff = 1./invG;
71 }