GraphNet: Discrete element modeling for network connected materials
Applications to polymeric networks
helper_funcs.h
Go to the documentation of this file.
1 
7 #ifndef __helper__
8 #define __helper__
9 #include <iostream>
10 #include <cmath>
11 #include <cstdlib>
12 #include <math.h>
13 #include <random>
14 #include <time.h>
15 #include <vector>
16 #include <unistd.h>
17 #include <algorithm>
18 #include <ctime>
19 #include <chrono>
20 #include <fstream>
21 #include <sstream>
22 #include <string>
23 #include <cstring>
24 #include <stddef.h>
25 #include "vel.h"
26 #include "params.h"
27 
28 using namespace std;
29 
30 inline int get_num_vertices(int elem_type){
31  switch(elem_type){
32  //case 1: return 2;
33  case 2: return 3;
34  case 3: return 4;
35  //case 4: return 4;
36  //case 5: return 8;
37  //case 7: return 5;
38  default: return -1;
39  }
40 }
41 
42 inline bool contains(vector<int>& vec, int elem){
43  return (std::find(vec.begin(), vec.end(), elem) != vec.end());
44 }
45 
46 inline float getnorm(const float* vec, const int dim = DIM){
47  float s = 0,t;
48  #pragma unroll
49  for (int j = 0; j<DIM; j++){
50  t = vec[j];
51  s += t*t;
52  }
53  return sqrt(s);
54 }
55 
56 void mapping(int& edge_counter, int elem_type, int n_vertices, stringstream& input, int* edges);
57 
58 string read_dump(int& n_nodes, int& n_elems, string& fname);
59 
60 void mapping(int& edge_counter, int elem_type);
61 
62 void read_n(int& n_nodes, int& n_elems, string& fname);
63 
64 void take_input(float* R, int* edges, int n_nodes, int n_elems, string& fname);
65 
66 void __init__(float* L, float* damage, bool* PBC, int n_elems);
67 
68 int filename(const string&);
69 
70 inline void normalize_vector(float* result, const float* vec){
71  float norm = getnorm(vec);
72  #pragma unroll
73  for (int i = 0; i<DIM; i++){
74  result[i] = vec[i] / norm;
75  }
76 }
77 
78 inline bool does_file_exist(string& fname){
79  ifstream infile(fname);
80  return infile.good();
81 }
82 
83 inline void normalize_vector(float* vec){
84  float norm = getnorm(vec);
85  #pragma unroll
86  for (int i = 0; i<DIM; i++){
87  vec[i] = vec[i] / norm;
88  }
89 }
90 
91 inline void unitvector(float* result, float* r1, float* r2){
92  #pragma unroll
93  for (int j = 0; j<DIM; j++){
94  result[j] = r1[j] - r2[j];
95  }
96  normalize_vector(result);
97 }
98 
99 inline float force_wlc(float x, float L){
100  float t = x / L;
101  if (t < 0.99){ return kB*T / b_poly * (t + 1.0 / 4.0 / pow((1 - t), 2) - 1.0 / 4.0); }
102  else { return 1000; }
103 }
104 
105 inline void convert_to_vector(float* result, const float mag, const float* direction){
106  #pragma unroll
107  for (int i = 0; i<DIM; i++){
108  result[i] = mag*direction[i];
109  }
110 }
111 
112 inline float dist(const float* r1, const float* r2){
113  float s = 0.0, t;
114  #pragma unroll
115  for (int j = 0; j<DIM; j++){
116  t = r1[j] - r2[j];
117  s += t * t;
118  }
119  return sqrt(s);
120 }
121 
122 void forcevector(float* result, float* r1, float* r2, float L);
123 
124 
125 inline bool ismember(int item, int* array,size_t size){
126  bool is_inside = false;
127  for(int k=0; k<size; k++){if(array[k]==item){is_inside = true; break;}}
128  return is_inside;
129 }
130 
131 float getabsmax(float* arr, size_t sizeofarr);
132 
133 template <typename t>
134 void write_to_file(string& fname, t* arr, int rows, int cols){
135 
136  ofstream logger;
137  std::time_t result = std::time(nullptr);
138  logger.open(fname, ios::trunc|ios_base::out);
139 
140 
141  logger<<"1D pulling of a 2D gel"<<"\n";
142  logger<<"File created at "<<std::asctime(std::localtime(&result));
143  logger<<"Rate damage : "<<RATE_DAMAGE<<"\n";
144 
145  logger<<"Sim dimension : "<<DIM<<"\n";
146  logger<<"Simulation time : "<<SIM_TIME<<"\n";
147  logger<<"Simulation time-step : "<<TIME_STEP<<"\n";
148  logger<<"Velocity : "<<vel_x<<"\t"<<vel_y<<"\n";
149  logger<<"MAXBOUND : "<<MAXBOUND_X<<"\t"<<MAXBOUND_Y<<"\n";
150 
151  logger<<"Disorder characteristics : "<<"\n";
152  logger<<" -- L_MEAN : "<<L_MEAN<<"\n";
153  logger<<" -- L_STD : "<<L_STD<<"\n";
154  logger<<"Others : SACBONDS = "<<SACBONDS<<"; IMPLEMENT_PBC = "<<IMPLEMENT_PBC<<"\n";
155 
156  logger<<"Cracked? : "<<CRACKED<<"; "<<PROB_REMOVAL<<"\n";
157 
158  // logger.open(fname, ios::trunc|ios_base::out);
159  for(int i =0; i < rows; i++){
160  for(int j = 0; j< cols; j++){
161  logger<<arr[i*cols + j]<<"\t";
162  }
163  logger<<"\n";
164  }
165  logger.close();
166  cout<<"Stored everything in "<<fname<<"!\n";
167 }
168 
169 
170 inline float kf(float force){
171  return af*expf(force*delxf/kB/T);
172 }
173 
174 inline float kfe(float force_mag){
175  return ae*expf(force_mag*delxe/kB/T);
176 }
177 
178 void __init__(float* L, int* m, float* damage, float* sacdamage, bool* PBC, int n_elems);
179 
180 #endif
int get_num_vertices(int elem_type)
Definition: helper_funcs.h:30
#define SACBONDS
Definition: params.h:15
#define TIME_STEP
Definition: params.h:5
void write_to_file(string &fname, t *arr, int rows, int cols)
Definition: helper_funcs.h:134
float getabsmax(float *arr, size_t sizeofarr)
Gets the absolute maximum of an array.
Definition: helper_funcs.cpp:330
void unitvector(float *result, float *r1, float *r2)
Definition: helper_funcs.h:91
void mapping(int &edge_counter, int elem_type, int n_vertices, stringstream &input, int *edges)
Converts mesh elements from GMSH file to edges and nodes. Only implemented elements of <element-id> =...
Definition: helper_funcs.cpp:38
#define IMPLEMENT_PBC
Definition: params.h:16
#define SIM_TIME
Definition: params.h:6
bool does_file_exist(string &fname)
Definition: helper_funcs.h:78
#define delxf
Definition: params.h:35
#define vel_y
Definition: vel.h:9
void read_n(int &n_nodes, int &n_elems, string &fname)
Reads the number of nodes and edges according to the .msh file. Useful for allocating memory in the c...
Definition: helper_funcs.cpp:172
#define vel_x
Definition: vel.h:8
int filename(const string &)
Gives an integer value to be appended to the network dump filename.
Definition: helper_funcs.cpp:376
bool ismember(int item, int *array, size_t size)
Definition: helper_funcs.h:125
#define kB
Definition: params.h:29
void convert_to_vector(float *result, const float mag, const float *direction)
Definition: helper_funcs.h:105
string read_dump(int &n_nodes, int &n_elems, string &fname)
Reads the number of nodes and poly chains left at the end of previous simulation. The code does not t...
Definition: helper_funcs.cpp:99
#define ae
Definition: params.h:32
bool contains(vector< int > &vec, int elem)
Definition: helper_funcs.h:42
float dist(const float *r1, const float *r2)
Definition: helper_funcs.h:112
#define T
Definition: params.h:31
void __init__(float *L, float *damage, bool *PBC, int n_elems)
Initializes various properties for each edge in the graph.
Definition: helper_funcs.cpp:298
#define af
Definition: params.h:34
float kfe(float force_mag)
Definition: helper_funcs.h:174
float force_wlc(float x, float L)
Definition: helper_funcs.h:99
#define L_MEAN
Definition: params.h:9
void take_input(float *R, int *edges, int n_nodes, int n_elems, string &fname)
Reads the input and converts to appropriate network data.
Definition: helper_funcs.cpp:224
#define delxe
Definition: params.h:33
#define CRACKED
Definition: params.h:18
#define MAXBOUND_Y
Definition: params.h:12
#define RATE_DAMAGE
Definition: params.h:19
void forcevector(float *result, float *r1, float *r2, float L)
Gives a force vector given the nodes and the contour length of the polymer connecting these...
Definition: helper_funcs.cpp:316
#define DIM
Definition: params.h:4
#define PROB_REMOVAL
Definition: params.h:24
float getnorm(const float *vec, const int dim=DIM)
Definition: helper_funcs.h:46
#define b_poly
Definition: params.h:30
float kf(float force)
Definition: helper_funcs.h:170
#define L_STD
Definition: params.h:10
void normalize_vector(float *result, const float *vec)
Definition: helper_funcs.h:70
#define MAXBOUND_X
Definition: params.h:11