Blame view

stim/biomodels/flow.h 12 KB
ea2056c0   cherub   added flow.h
1
2
3
4
5
  #pragma once
  #include <fstream>									// Required for ofstream, etc.
  #include <iomanip>									// Required for setw
  #include <iostream>									// Required for cout, cin, etc.
  #include <tuple>									// Required for returning multiple values from a function
ea2056c0   cherub   added flow.h
6
7
8
9
10
11
12
  
  using namespace std;
  
  
  class flow
  {
  public:
9a1c681c   cherub   modified flow.h
13
  	void backupToTxt(unsigned int nL, double **D, char filename[]);
ea2056c0   cherub   added flow.h
14
  	tuple<int, int> copySrcDesRadLen(char filename[]);
9a1c681c   cherub   modified flow.h
15
  	void copyToArray(int *src, int *dest, double *radii, double *len);
ea2056c0   cherub   added flow.h
16
  	int getDangleNodes(int datarow, int numNodes, int *row, int *column, int *dangleNodes);
9a1c681c   cherub   modified flow.h
17
  	void inversion(double **a, int n, double **b);
ea2056c0   cherub   added flow.h
18
19
  
  protected:
9a1c681c   cherub   modified flow.h
20
21
  	float determinant(double **a, int n);
  	int minor(double **src, double **dest, int row, int col, int order);
ea2056c0   cherub   added flow.h
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
  };
  
  /* Function to find the dangle nodes in a network */
  // Created by Cherub P. Harder (8/10/2015), U of Houston
  // Modified by Cherub P. Harder on 8/12/2015
  int flow::getDangleNodes(int datarow, int numNodes, int *column1, int *column2, int *dangleNodes)
  {
  	int count = datarow, diff1 = 0, diff2 = 0, numPress = 0, st = 0;
  
  	// Find matching nodes within column2
  	for( int i = 0; i < count; i++ )
  	{
  		for( int y = i+1; y < datarow; y++ )
  		{
  			if( column2[i] == column2[y] )			// Is there a match?
  			{
  				st = column2[i];					// Save the matching node
4277a87d   cherub   modified flow.h &...
39
  //				cout << endl << column2[i] << " = " << column2[y] << endl; // Display the matching nodes
ea2056c0   cherub   added flow.h
40
41
42
43
44
45
46
47
48
  				memmove(column2+i, column2+i+1, (datarow-(i+1)) * sizeof(column2[0])); // Move up the rows
  													// taking the places of the rows before them starting
  													// with where the matching node is located
  				column2[datarow-1] = st;			// Place the matching node at the very end of the array--
  													// this is for comparison purpose so that the other match-
  													// ing node will be moved as well and then omitted later.
  				diff1++;							// Count the matching node
  
  				// Display the updated array (with the matching node moved to the bottommost row)
4277a87d   cherub   modified flow.h &...
49
  /*				cout << "Updated array:" << endl;
ea2056c0   cherub   added flow.h
50
51
  				for( int k = 0; k < datarow; k++ )
  					cout << column2[k] << endl;
4277a87d   cherub   modified flow.h &...
52
  */
ea2056c0   cherub   added flow.h
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
  				// Decrement the counters
  				// NOTE: The counters need to be decremented because the rows had been moved up, so the same
  				// locations need to be read again because they contain different values now after the move.
  				i--;								// Decrement i to read the node that took over the place
  													// of the matching node. Otherwise, it will be skipped.
  				y--;								// Decrement y to read the following node for comparison
  				count--;							// The maximum count need to be decremented so that the
  													// matching nodes that had been moved will not be read again.
  													// However, the maximum count (datarow) for finding a match
  													// will not be decremented because the remaining matching
  													// node that has not been moved yet needs to be moved and
  													// the only way to do that is to match it with its duplicate.
  			}
  		}
  	}
  	
  	// Store the nodes that have no duplicates
  	// NOTE: This will only save the nodes that have not been moved to the bottom.
4277a87d   cherub   modified flow.h &...
71
  //	cout << "\ndangleNodes array:" << endl;
ea2056c0   cherub   added flow.h
72
73
74
  	for( int j = 0; j < datarow-diff1; j++ )
  	{
  		dangleNodes[numPress] = column2[j];
4277a87d   cherub   modified flow.h &...
75
  //		cout << dangleNodes[j] << endl;				// DELETE!!!
ea2056c0   cherub   added flow.h
76
77
78
79
80
81
82
83
84
85
86
87
88
  		numPress++;									// Count the non-duplicated node
  	}
  
  	// Find if the non-duplicated nodes have a match from column1
  	count = datarow-diff1;							// Reinitialize the counter
  
  	for( int i = 0; i < count; i++ )
  	{
  		for( int j = 0; j < datarow; j++ )
  		{
  			if( dangleNodes[i] == column1[j] )		// Is there a match?
  			{
  				st = column1[j];					// Save the matching node
4277a87d   cherub   modified flow.h &...
89
  //				cout << endl << dangleNodes[i] << " = " << column1[j] << endl; // Display the matching nodes
ea2056c0   cherub   added flow.h
90
91
92
93
94
  				memmove(dangleNodes+i, dangleNodes+i+1, (datarow-diff1-(i+1)) * sizeof(dangleNodes[0]));
  				dangleNodes[count-1] = st;			// Move the matching node to the bottom of the array
  				diff2++;							// Count the matching node
  
  				// Display the updated array
4277a87d   cherub   modified flow.h &...
95
  /*				cout << "Updated dangleNodes array:" << endl;
ea2056c0   cherub   added flow.h
96
97
98
99
  				for( int k = 0; k < count-1; k++ )
  				{
  					cout << dangleNodes[k] << endl;
  				}
4277a87d   cherub   modified flow.h &...
100
  */
ea2056c0   cherub   added flow.h
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
  				// Decrement the counters
  				i--;
  				j--;
  				count--;
  				numPress--;							// Decrement to the exact number of dangle nodes
  			}
  		}
  	}
  
  	return numPress;								// Number of dangle nodes
  }
  
  
  // Function to make a backup copy of the contents of a matrix to a .txt file
  // Created by Cherub P. Harder (8/10/2015), U of Houston
9a1c681c   cherub   modified flow.h
116
  void flow::backupToTxt(unsigned int nL, double **D, char filename[])
ea2056c0   cherub   added flow.h
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
  {
  	ofstream output_file(filename);
  	
  	for( unsigned int i = 0; i < nL; i++ )
  	{
  		for( int j = 0; j < 4; j++ )
  		{
  			if( j < 3 )
  				output_file << D[i][j] << "\t";
  
  			else
  				output_file << D[i][j];
  		}
  
  		output_file << "\n";
  	}
  
  	output_file.close( );
  }
  
  
  // Function to make separate copies of the source nodes, destination nodes, radii, and lengths
  // Created by Cherub P. Harder (8/10/2015), U of Houston
  tuple<int, int> flow::copySrcDesRadLen(char filename[])
  {
  	int cnt = 0, numElements = 0, numNodes = 0;
  	float number = 0.0;
  	ofstream srcData("srcCol.txt");					// A .txt file to store the source nodes
  	ofstream desData("destCol.txt");				// A .txt file to store the destination nodes
  	ofstream radiiData("radii.txt");				// A .txt file to store the radii
  	ofstream lenData("lengths.txt");				// A .txt file to store the lengths
  	FILE *fp = fopen(filename, "r");				// Create a variable of type FILE* and open the file using
  													// the fopen function and assign the file to the variable
  	// Check if the file exists
  	if(fp == NULL)									// Alternative: if(!fp)
  	{
  		printf("Error! File does not exist.\n");
  		getchar( );									// Pause
  		exit(-1);									// NOTE: Must include stdlib.h.
  	}
  
  	// Store data to their respective .txt files
  	while(fscanf(fp, "%f", &number) == 1)
  	{
  		cnt++;										// Increment counter
  
  		// Store to srcCol.txt
  		if(cnt == 1)
  			srcData << number << endl;
  
  		// Store to destCol.txt
  		if(cnt == 2)
  			desData << number << endl;
  
  		// Save the current number of nodes
  		if(cnt < 3)
  		{
  			if(number > numNodes)
  				numNodes = (int)number;
  		}
  
  		// Store to radii.txt
  		if(cnt == 3)
  			radiiData << number << endl;
  
  		// Store to lengths.txt
  		if(cnt == 4)
  		{
  			lenData << number << endl;
  
  			numElements++;							// Count the elements
  			cnt = 0;								// Reset counter
  		}
  	}
  
  	srcData.close( );
  	desData.close( );
  	radiiData.close( );
  	lenData.close( );
  
  	return make_tuple(numNodes, numElements);		// Return two values
  }
  
  
  // Function to copy data for .txt files to their respective arrays
  // Created by Cherub P. Harder (8/11/2015), U of Houston
9a1c681c   cherub   modified flow.h
203
  void flow::copyToArray(int *src, int *dest, double *radii, double *len)
ea2056c0   cherub   added flow.h
204
205
  {
  	int v = 0;
9a1c681c   cherub   modified flow.h
206
  	double tmp = 0, R = 0, L = 0;
ea2056c0   cherub   added flow.h
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
  	
  	// Store source node values to the array src
  	ifstream readSrc("srcCol.txt");
  
  	while( readSrc >> tmp )
  	{
  		src[v] = (int)tmp;
  		v++;
  	}
  
  	readSrc.close( );
  
  	// Store destination node values to the array dest
  	v = 0;											// Reset counter
  	ifstream readDest("destCol.txt");
  
  	while( readDest >> tmp )
  	{
  		dest[v] = (int)tmp;
  		v++;
  	}
  
  	readDest.close( );
  
  	// Store radius values to the array radii
  	v = 0;											// Reset counter
  	ifstream readRad("radii.txt");
  
  	while( readRad >> tmp )
  	{
  		radii[v] = tmp;
  		v++;
  	}
  
  	readRad.close( );
  
  	// Store length values to the array len
  	v = 0;											// Reset counter
  	ifstream readLen("lengths.txt");
  
  	while( readLen >> tmp )
  	{
  		len[v] = tmp;
  		v++;
  	}
  
  	readLen.close( );
  }
  
  
  // Function to find the inverse of a square matrix
9a1c681c   cherub   modified flow.h
258
  void flow::inversion(double **a, int n, double **b)
ea2056c0   cherub   added flow.h
259
  {
9a1c681c   cherub   modified flow.h
260
261
  	// Get 1 over the determinant of A
  	double det = (double)(1.0/determinant(a, n));
dae97842   Cherub P. Harder   modified flow.h i...
262
  //	cerr << "\n1/det(C) = " << det << endl;				// DELETE!!!
ea2056c0   cherub   added flow.h
263
264
  
      // Memory allocation
9a1c681c   cherub   modified flow.h
265
266
      double *tmp = new double[(n-1) * (n-1)];
      double **m = new double * [n-1];
ea2056c0   cherub   added flow.h
267
268
269
270
271
272
273
274
275
      for( int i = 0; i < n-1; i++ )
  		m[i] = tmp + ( i * (n-1) );
   
      for( int j = 0; j < n; j++)
      {
  		for( int i = 0; i < n; i++ )
          {
  			// Get the cofactor (matrix) of a(j,i)
              minor(a, m, j, i, n);
ea2056c0   cherub   added flow.h
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
              b[i][j] = det * determinant( m, n-1 );
              if( (i+j)%2 == 1 )
                  b[i][j] = -b[i][j];
          }
      }
   
      // Release memory
      // Delete [] minor[0];
      delete [] tmp;
      delete [] m;
  }
  
  
  // Function to find the determinant of a matrix using recursion
  // Contribution by Edward Popko
  // Modified by Cherub P. Harder (7/15/2015), U of Houston
  // Arguments: a(double **) - pointer to a pointer of an arbitrary square matrix
  //			  n(int) - dimension of the square matrix
9a1c681c   cherub   modified flow.h
294
  float flow::determinant(double **a, int n)
ea2056c0   cherub   added flow.h
295
296
  {
  	int i, j, j1, j2;								// General loop and matrix subscripts
9a1c681c   cherub   modified flow.h
297
298
      double det = 0;									// Initialize determinant
      double **m = NULL;								// Pointer to pointer to implement 2D square array
ea2056c0   cherub   added flow.h
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
  
  	// Display contents of matrix C (DELETE!!!)
  /*	std::cout << "\nThe updated matrix C:\n";
  	for( int j = 0; j < n; ++j )
  	{
  		std::cerr << "\t";
  
  		for( int k = 0; k < n; ++k )
  			std::cerr << left << setw(15) << a[j][k];
  
  		std::cerr << endl;
  	}
  
  	getchar();					// DELETE!!!*/
  
  	if(n < 1) { }									// Error condition - should never get here
  
      else if (n == 1)								// Should never get here
  	{
  		det = a[0][0];
  	}
  
      else if(n == 2)									// Basic 2x2 sub-matrix determinate definition
  	{												// When n == 2, this ends the recursion series
  		det = a[0][0] * a[1][1] - a[1][0] * a[0][1];
  	}
  													// Recursion continues, solve next sub-matrix
      else											// Solve the next minor by building a sub-matrix
  	{
  		det = 0;									// Initialize determinant of sub-matrix
  
          for (j1 = 0; j1 < n; j1++)					// For each column in sub-matrix get space for the
  		{											// pointer list
9a1c681c   cherub   modified flow.h
332
  			m = (double **) malloc((n-1) * sizeof(double *));
ea2056c0   cherub   added flow.h
333
334
  
              for (i = 0; i < n-1; i++)
9a1c681c   cherub   modified flow.h
335
  				m[i] = (double *) malloc((n-1)* sizeof(double));
ea2056c0   cherub   added flow.h
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
                         //     i[0][1][2][3]  first malloc
                         //  m -> +  +  +  +   space for 4 pointers
                         //       |  |  |  |          j  second malloc
                         //       |  |  |  +-> _ _ _ [0] pointers to
                         //       |  |  +----> _ _ _ [1] and memory for
                         //       |  +-------> _ a _ [2] 4 doubles
                         //       +----------> _ _ _ [3]
                         //
                         //                   a[1][2]
                         // Build sub-matrix with minor elements excluded
              
  			for (i = 1; i < n; i++)
  			{
  				j2 = 0 ;							// Start at first sum-matrix column position
  													// Loop to copy source matrix less one column
                  for (j = 0; j < n; j++)
  				{
  					if (j == j1) continue;			// Do not copy the minor column element
  					
  					m[i-1][j2] = a[i][j];			// Copy source element into new sub-matrix
  													// i-1 because new sub-matrix is one row
  													// (and column) smaller with excluded minors
                      j2++;							// Move to next sub-matrix column position
  				}
  			}
  			
9a1c681c   cherub   modified flow.h
362
  			det += (double)pow(-1.0, 1.0 + j1 + 1.0) * a[0][j1] * determinant(m, n-1);
ea2056c0   cherub   added flow.h
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
  													// Sum x raised to y power
  													// recursively get determinant of next
  													// sub-matrix which is now one
  													// row & column smaller
  
              for (i = 0; i < n-1; i++) free(m[i]);	// Free the storage allocated to
  													// this minor's set of pointers
              free(m);								// Free the storage for the original
  													// pointer to pointer
  		}
  	}
  	
  	return(det);
  }
  
  
  // Function to calculate the cofactor of element (row, col)
9a1c681c   cherub   modified flow.h
380
  int flow::minor(double **src, double **dest, int row, int col, int order)
ea2056c0   cherub   added flow.h
381
382
383
384
  {
  	// Indicate which col and row is being copied to dest
      int colCount=0,rowCount=0;
   
9a1c681c   cherub   modified flow.h
385
      for(int i = 0; i < order; i++)
ea2056c0   cherub   added flow.h
386
      {
9a1c681c   cherub   modified flow.h
387
          if(i != row)
ea2056c0   cherub   added flow.h
388
389
          {
              colCount = 0;
9a1c681c   cherub   modified flow.h
390
              for(int j = 0; j < order; j++)
ea2056c0   cherub   added flow.h
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
              {
                  // When j is not the element
                  if( j != col )
                  {
                      dest[rowCount][colCount] = src[i][j];
                      colCount++;
                  }
              }
  
              rowCount++;
  		}
      }
   
      return 1;
  }