rod_math_v9.h
Go to the documentation of this file.
1 //
2 // This file is part of the FFEA simulation package
3 //
4 // Copyright (c) by the Theory and Development FFEA teams,
5 // as they appear in the README.md file.
6 //
7 // FFEA is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation, either version 3 of the License, or
10 // (at your option) any later version.
11 //
12 // FFEA is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with FFEA. If not, see <http://www.gnu.org/licenses/>.
19 //
20 // To help us fund FFEA development, we humbly ask that you cite
21 // the research papers on the package.
22 //
23 
24 /*
25  * rod_math_v9.h
26  * Author: Rob Welch, University of Leeds
27  * Email: py12rw@leeds.ac.uk
28  */
29 #ifndef ROD_MATH
30 #define ROD_MATH
31 
32 #define OUT
33 #define _USE_MATH_DEFINES
34 
35 
36 #include <cmath>
37 #include <iostream>
38 #include <assert.h>
39 #include "dimensions.h"
40 #include <stdlib.h>
41 #include <boost/math/special_functions/fpclassify.hpp>
42 //#include <fenv.h>
43 
44 
45 namespace rod {
46 
47 const static bool debug_nan = false;
48 
49 const double boltzmann_constant = 1.3806503e-23/mesoDimensions::Energy;
50 
51 // A less weird way to access the contents of our arrays representing our vectors
52 static const int x = 0;
53 static const int y = 1;
54 static const int z = 2;
55 
56 // For clarity, anytime you see an index [x], we're referring to the x
57 // dimension.
58 
59 // Computing the energy for a perturbation requires four segments, defined
60 // by 5 nodes. They are i-2 to i+1, listed here.
61 static const int im2 = 0;
62 static const int im1 = 1;
63 static const int i = 2;
64 static const int ip1 = 3;
65 
66 #define OMP_SIMD_INTERNAL _Pragma("omp simd")
67 
68 #define vec3d(x)for(int x = 0; x < 3; ++ x)
69 
70 extern bool dbg_print; // prints tons of intermediate info for debugging purposes. don't enable this
71 
72 static const float rod_software_version = 0.3;
73 
74 void rod_abort(std::string message);
75 
76 // These are just generic vector functions that will be replaced by mat_vec_fns at some point
77 void print_array(std::string array_name, float array[], int length);
78 void print_array(std::string array_name, double array[], int length);
79 void normalize(float in[3], OUT float out[3]);
80 void normalize_unsafe(float in[3], OUT float out[3]);
81 float absolute(float in[3]);
82 void cross_product(float a[3], float b[3], float out[3]);
83 void cross_product_unsafe(float a[3], float b[3], float out[3]);
84 void get_rotation_matrix(float a[3], float b[3], float rotation_matrix[9]);
85 void apply_rotation_matrix(float vec[3], float matrix[9], OUT float rotated_vec[3]);
86 void apply_rotation_matrix_row(float vec[3], float matrix[9], OUT float rotated_vec[3]);
87 void matmul_3x3_3x3(float a[9], float b[9], OUT float out[9]);
88 
89 // These are utility functions specific to the math for the rods
90 void get_p_i(float curr_r[3], float next_r[3], OUT float p_i[3]);
91 void rodrigues_rotation(float v[3], float k[3], float theta, OUT float v_rot[3]);
92 float safe_cos(float in);
93 float get_l_i(float p_i[3], float p_im1[3]);
94 float get_signed_angle(float m1[3], float m2[3], float l[3]);
95 
96  /*-----------------------*/
97  /* Update Material Frame */
98  /*-----------------------*/
99 
100 void perpendicularize(float m_i[3], float p_i[3], OUT float m_i_prime[3]);
101 void update_m1_matrix(float m_i[3], float p_i[3], float p_i_prime[3], float m_i_prime[3]);
102 
103  /*------------------*/
104  /* Compute Energies */
105  /*------------------*/
106 
107 float get_stretch_energy(float k, float p_i[3], float p_i_equil[3]);
108 void parallel_transport(float m[3], float m_prime[3], float p_im1[3], float p_i[3]);
109 float get_twist_energy(float beta, float m_i[3], float m_im1[3], float m_i_equil[3], float m_im1_equil[3], float p_im1[3], float p_i[3], float p_im1_equil[3], float p_i_equil[3]);
110 void get_kb_i(float p_im1[3], float p_i[3], float e_im1_equil[3], float e_i_equil[3], OUT float kb_i[3]);
111 void get_omega_j_i(float kb_i[3], float n_j[3], float m_j[3], OUT float omega_j_i[2]);
112 float get_bend_energy(float omega_i_im1[2], float omega_i_im1_equil[2], float B_equil[4]);
113 
115  float p_im1[3],
116  float p_i[3],
117  float p_im1_equil[3],
118  float p_i_equil[3],
119  float n_im1[3],
120  float m_im1[3],
121  float n_im1_equil[3],
122  float m_im1_equil[3],
123  float n_i[3],
124  float m_i[3],
125  float n_i_equil[3],
126  float m_i_equil[3],
127  float B_i_equil[4],
128  float B_im1_equil[4]);
129 
130 float get_weights(float a[3], float b[3]);
131 void get_mutual_element_inverse(float pim1[3], float pi[3], float weight, OUT float mutual_element[3]);
132 void get_mutual_axes_inverse(float mim1[3], float mi[3], float weight, OUT float m_mutual[3]);
133 
135  float p_im1[3],
136  float p_i[3],
137  float p_im1_equil[3],
138  float p_i_equil[3],
139  float n_im1[3],
140  float m_im1[3],
141  float n_im1_equil[3],
142  float m_im1_equil[3],
143  float n_i[3],
144  float m_i[3],
145  float n_i_equil[3],
146  float m_i_equil[3],
147  float B_i_equil[4],
148  float B_im1_equil[4]);
149 
150  /*----------*/
151  /* Dynamics */
152  /*----------*/
153 
154 float get_translational_friction(float viscosity, float radius, bool rotational);
155 float get_rotational_friction(float viscosity, float radius, float length, bool safe);
156 float get_force(float bend_energy, float stretch_energy, float delta_x);
157 float get_torque(float twist_energy, float delta_theta);
158 float get_delta_r(float friction, float timestep, float force, float noise, float external_force);
159 float get_noise(float timestep, float kT, float friction, float random_number);
160 
161  /*------------*/
162  /* Shorthands */
163  /*------------*/
164 
165 void load_p(float p[4][3], float *r, int offset);
166 void load_m(float m_loaded[4][3], float *m, int offset);
167 void normalize_all(float p[4][3]);
168 void absolute_all(float p[4][3], float absolutes[4]);
169 void cross_all(float p[4][3], float m[4][3], OUT float n[4][3]);
170 void delta_e_all(float e[4][3], float new_e[4][3], OUT float delta_e[4][3]);
171 void update_m1_all(float m[4][3], float absolutes[4], float t[4][3], float delta_e[4][3], OUT float m_out[4][3]);
172 void update_m1_matrix_all(float m[4][3], float p[4][3], float p_prime[4][3], OUT float m_prime[4][3], int start_cutoff, int end_cutoff);
173 void fix_m1_all(float m[4][3], float new_t[4][3]);
174 void update_and_fix_m1_all(float old_e[4][3], float new_e[4][3], float m[4][3]);
175 void set_cutoff_values(int e_i_node_no, int length, OUT int start_cutoff, int end_cutoff);
176 
177  /*----------*/
178  /* Utility */
179  /*----------*/
180 
181 bool not_simulation_destroying(float x);
182 bool not_simulation_destroying(float x[3]);
183 void load_B_all(float B[4][4], float *B_matrix, int offset);
184 void make_diagonal_B_matrix(float B, OUT float B_matrix[4]);
185 void set_cutoff_values(int e_i_node_no, int length, OUT int *start_cutoff, int *end_cutoff);
186 float get_absolute_length_from_array(float* array, int node_no, int length);
187 void get_centroid_generic(float* r, int length, OUT float centroid[3]);
188 
189  /*-------------------------------*/
190  /* Move the node, get the energy */
191  /*-------------------------------*/
192 
194  float perturbation_amount,
195  int perturbation_dimension,
196  float B_equil[4],
197  float *material_params,
198  int start_cutoff,
199  int end_cutoff,
200  int p_i_node_no,
201  float *r_all,
202  float *r_all_equil,
203  float *m_all,
204  float *m_all_equil,
205  float energies[3]
206 );
207 
208 }
209 #endif
static const float rod_software_version
Definition: rod_math_v9.h:72
void fix_m1_all(float m[4][3], float new_t[4][3])
void load_m(float m_loaded[4][3], float *m, int offset)
Definition: rod_math_v9.cpp:880
float get_bend_energy(float omega_i_im1[2], float omega_i_im1_equil[2], float B_equil[4])
Definition: rod_math_v9.cpp:531
void get_p_i(float curr_r[3], float next_r[3], OUT float p_i[3])
Definition: rod_math_v9.cpp:263
static const int z
Definition: rod_math_v9.h:54
float get_noise(float timestep, float kT, float friction, float random_number)
Definition: rod_math_v9.cpp:853
void get_rotation_matrix(float a[3], float b[3], float rotation_matrix[9])
Definition: rod_math_v9.cpp:211
float get_delta_r(float friction, float timestep, float force, float noise, float external_force)
Definition: rod_math_v9.cpp:842
void get_centroid_generic(float *r, int length, OUT float centroid[3])
Definition: rod_math_v9.cpp:1035
void get_mutual_axes_inverse(float mim1[3], float mi[3], float weight, OUT float m_mutual[3])
Definition: rod_math_v9.cpp:645
void make_diagonal_B_matrix(float B, OUT float B_matrix[4])
Definition: rod_math_v9.cpp:989
void get_perturbation_energy(float perturbation_amount, int perturbation_dimension, float *B_matrix, float *material_params, int start_cutoff, int end_cutoff, int p_i_node_no, float *r_all, float *r_all_equil, float *m_all, float *m_all_equil, OUT float energies[3])
Definition: rod_math_v9.cpp:1067
void absolute_all(float p[4][3], float absolutes[4])
Definition: rod_math_v9.cpp:902
static const int im2
index of i-2nd thing
Definition: rod_math_v9.h:61
float get_absolute_length_from_array(float *array, int node_no, int length)
Definition: rod_math_v9.cpp:1016
void update_m1_matrix(float m_i[3], float p_i[3], float p_i_prime[3], float m_i_prime[3])
Definition: rod_math_v9.cpp:371
static const int y
Definition: rod_math_v9.h:53
static const int x
Definition: rod_math_v9.h:52
void matmul_3x3_3x3(float a[9], float b[9], OUT float out[9])
Definition: rod_math_v9.cpp:251
float get_bend_energy_from_p(float p_im1[3], float p_i[3], float p_im1_equil[3], float p_i_equil[3], float n_im1[3], float m_im1[3], float n_im1_equil[3], float m_im1_equil[3], float n_i[3], float m_i[3], float n_i_equil[3], float m_i_equil[3], float B_i_equil[4], float B_im1_equil[4])
Definition: rod_math_v9.cpp:546
void load_p(float p[4][3], float *r, int offset)
Definition: rod_math_v9.cpp:870
void update_m1_matrix_all(float m[4][3], float p[4][3], float p_prime[4][3], OUT float m_prime[4][3], int start_cutoff, int end_cutoff)
Definition: rod_math_v9.cpp:938
void set_cutoff_values(int p_i_node_no, int num_nodes, OUT int *start_cutoff, int *end_cutoff)
Definition: rod_math_v9.cpp:1005
float random_number(float A, float B, RngStream rng[], int thread_id)
Definition: rod_structure.cpp:60
void print_array(std::string array_name, float array[], int length)
Definition: rod_math_v9.cpp:92
const scalar force
force = E/l
Definition: dimensions.h:37
float get_twist_energy(float beta, float m_i[3], float m_im1[3], float m_i_equil[3], float m_im1_equil[3], float p_im1[3], float p_i[3], float p_im1_equil[3], float p_i_equil[3])
Definition: rod_math_v9.cpp:423
Definition: rod_blob_interface.cpp:38
float get_torque(float twist_energy, float delta_theta)
Definition: rod_math_v9.cpp:831
void cross_all(float p[4][3], float m[4][3], OUT float n[4][3])
Definition: rod_math_v9.cpp:912
void update_and_fix_m1_all(float old_e[4][3], float new_e[4][3], float m[4][3])
bool not_simulation_destroying(float x, std::string message)
Definition: rod_math_v9.cpp:63
void apply_rotation_matrix(float vec[3], float matrix[9], OUT float rotated_vec[3])
Definition: rod_math_v9.cpp:233
float get_l_i(float p_i[3], float p_im1[3])
Definition: rod_math_v9.cpp:330
static const int im1
index of i-1st thing
Definition: rod_math_v9.h:62
static const int ip1
index of i+1th thing
Definition: rod_math_v9.h:64
float get_stretch_energy(float k, float p_i[3], float p_i_equil[3])
Definition: rod_math_v9.cpp:392
void get_kb_i(float p_im1[3], float p_i[3], OUT float kb_i[3])
Definition: rod_math_v9.cpp:503
float safe_cos(float in)
Definition: rod_math_v9.cpp:292
static const bool debug_nan
Definition: rod_math_v9.h:47
void normalize_unsafe(float in[3], OUT float out[3])
Definition: rod_math_v9.cpp:144
float get_translational_friction(float viscosity, float radius, bool rotational)
Definition: rod_math_v9.cpp:795
void rod_abort(std::string message)
Definition: rod_math_v9.cpp:43
void cross_product(float a[3], float b[3], float out[3])
Definition: rod_math_v9.cpp:184
const scalar Energy
Energy = KbT, T=300K.
Definition: dimensions.h:32
void delta_e_all(float p[4][3], float new_p[4][3], OUT float delta_p[4][3])
Definition: rod_math_v9.cpp:924
void update_m1_all(float m[4][3], float absolutes[4], float t[4][3], float delta_e[4][3], OUT float m_out[4][3])
void apply_rotation_matrix_row(float vec[3], float matrix[9], OUT float rotated_vec[3])
Definition: rod_math_v9.cpp:242
float absolute(float in[3])
Definition: rod_math_v9.cpp:173
void normalize(float in[3], OUT float out[3])
Definition: rod_math_v9.cpp:129
bool dbg_print
Definition: rod_math_v9.cpp:35
void get_mutual_element_inverse(float pim1[3], float pi[3], float weight, OUT float mutual_element[3])
Definition: rod_math_v9.cpp:635
float get_weights(float a[3], float b[3])
Definition: rod_math_v9.cpp:628
void perpendicularize(float m_i[3], float p_i[3], OUT float m_i_prime[3])
Definition: rod_math_v9.cpp:359
const scalar length
length = C atom VdW radius
Definition: dimensions.h:31
void rodrigues_rotation(float v[3], float k[3], float theta, OUT float v_rot[3])
Definition: rod_math_v9.cpp:273
float get_force(float bend_energy, float stretch_energy, float delta_x)
Definition: rod_math_v9.cpp:820
void normalize_all(float p[4][3])
Definition: rod_math_v9.cpp:893
void load_B_all(float B[4][4], float *B_matrix, int offset)
Definition: rod_math_v9.cpp:970
static const int i
index of ith thing
Definition: rod_math_v9.h:63
float get_rotational_friction(float viscosity, float radius, float length, bool safe)
Definition: rod_math_v9.cpp:806
#define OUT
This is used to denote when a function modifies one of its parameters.
Definition: rod_math_v9.h:32
const scalar pi
Definition: mat_vec_types.h:54
float get_bend_energy_mutual_parallel_transport(float p_im1[3], float p_i[3], float p_im1_equil[3], float p_i_equil[3], float n_im1[3], float m_im1[3], float n_im1_equil[3], float m_im1_equil[3], float n_i[3], float m_i[3], float n_i_equil[3], float m_i_equil[3], float B_i_equil[4], float B_im1_equil[4])
Definition: rod_math_v9.cpp:661
void cross_product_unsafe(float a[3], float b[3], float out[3])
Definition: rod_math_v9.cpp:191
float get_signed_angle(float m1[3], float m2[3], float l[3])
Definition: rod_math_v9.cpp:342
void parallel_transport(float m[3], float m_prime[3], float p_im1[3], float p_i[3])
Definition: rod_math_v9.cpp:411
void get_omega_j_i(float kb_i[3], float n_j[3], float m_j[3], OUT float omega_j_i[2])
Definition: rod_math_v9.cpp:519
const double boltzmann_constant
Definition: rod_math_v9.h:49