-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDiffusionSimulator2D.cs
306 lines (282 loc) · 10.7 KB
/
DiffusionSimulator2D.cs
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
using System;
using System.IO;
using static Diffusion2D_Library.BoundaryCondition;
namespace Diffusion2D_Library
{
// ====================================================================
// Method delegates used by the 2D diffusion equation solvers
// ====================================================================
/// <summary>
/// Delegate for methods for boundary conditions that specify both constant and variable locations. Used if there is a position-dependence to the boundary condition
/// </summary>
/// <param name="time">time, in seconds</param>
/// <returns>A vector storing the composition on a boundary of a 2D field</returns>
//public delegate RVector BoundaryCondition_Del(double time, int n);
public delegate RVector Del_BC_xy(double t, RVector SlidingSide, double FixedValue);
/// <summary>
/// Delegate for assigning the initial composition field in a 2D region
/// </summary>
/// <param name="c"></param>
/// <returns>A matrix of composition values</returns>
public delegate RMatrix Del_IC_xy(RMatrix x, RMatrix y);
/// <summary>
/// Delegate for calculating sources/sinks for reactions occurring in a 2D composition field
/// </summary>
/// <param name="position"></param>
/// <param name="time"></param>
/// <param name="composition"></param>
/// <returns>A matrix of composition values</returns>
public delegate RMatrix Del_Source_xy(RMatrix xposition, RMatrix yposition, double time, double DiffCoeff, RMatrix composition);
/// <summary>
/// Delegate for calculating sources/sinks for reactions occurring in a 2D composition field with diffusivity as a matrix
/// </summary>
/// <param name="xposition"></param>
/// <param name="yposition"></param>
/// <param name="time"></param>
/// <param name="DiffCoeff"></param>
/// <param name="composition"></param>
/// <returns></returns>
public delegate RMatrix Del_Source_MatrixD(RMatrix xposition, RMatrix yposition, double time, RMatrix DiffCoeff, RMatrix composition);
public delegate RVector Del_BC_Constant(int n, double v);
public delegate RMatrix Del_IC_Constant(int nr, int nc, double v);
public delegate RMatrix Del_Source_Constant(int nr, int nc, double v);
// ====================================================================
public class DiffusionSimulator2D
{
/// <summary>
/// Defines a structure to enclose the matrices that define a 2D composition
/// </summary>
public struct CompositionField2D
{
public RMatrix InitialCompositionValues;
public RMatrix FinalCompositionValues;
public RMatrix XPositionValues;
public RMatrix YPositionValues;
public RMatrix DValues;
/// <summary>
/// Conctructor for CompositionField2D
/// </summary>
/// <param name="nx">number of x positions</param>
/// <param name="ny">number of y positions</param>
public CompositionField2D(int nx, int ny)
{
InitialCompositionValues = new(nx, ny);
FinalCompositionValues = new(nx, ny);
XPositionValues = new(nx, ny);
YPositionValues = new(nx, ny);
DValues = new(nx, ny);
}
}
/// <summary>
/// Enumeration that specifies the type of boundary condition to be applied to a boundary
/// </summary>
public enum BoundingBox { left, right, top, bottom }
/// <summary>
/// Enumeration that specifies whether text is output to the output stream
/// </summary>
public enum Mode { quiet, verbose }
/// <summary>
///
/// </summary>
public struct BoundaryWithFunction
{
public BoundingBox BoundaryLocation;
public ABoundaryCondition TypeBC;
public Del_BC_xy BoundaryFunction;
public RVector PositionVaries;
public double PositionFixed;
}
public struct BoundaryWithVector
{
public BoundingBox BoundaryLocation;
public ABoundaryCondition TypeBC;
public RVector FunctionValues;
}
// Constants
public const string suffix = ".csv";
// Fields
internal string b_filename;
internal string[] errors;
internal double dt;
internal double dx;
internal double dy;
internal double d_coeff;
internal CompositionField2D cf_2D;
internal BoundaryWithFunction[] border_with_function;
internal BoundaryWithVector[] border_with_vector;
internal Mode text_mode;
internal bool error_flag = false;
internal int nx, ny, nx_less1, nx_less2, nx_less3, end_idx1, end_idx2, start_idx1, start_idx2;
public Del_IC_xy I0;
public Del_Source_MatrixD gxt_function;
public Del_Source_xy gxt_xy;
public RMatrix gxt_matrix;
// Properties
public int NX
{
get => nx;
set => nx = value;
}
public int NY
{
get => ny;
set => ny = value;
}
public RMatrix C_Initial
{
get => cf_2D.InitialCompositionValues;
set
{
if (value.GetnCols > 0 && value.GetnRows > 0) { cf_2D.InitialCompositionValues = value; }
}
}
public RMatrix C_Final
{
get => cf_2D.FinalCompositionValues;
set { if (value.GetnCols > 0 && value.GetnRows > 0) { cf_2D.FinalCompositionValues = value; } }
}
public RMatrix X => cf_2D.XPositionValues;
public RMatrix Y => cf_2D.YPositionValues;
public BoundaryWithFunction[] BCs_Functions
{
get => border_with_function;
set => border_with_function = value;
}
public BoundaryWithVector[] BCs_Vector
{
get => border_with_vector;
set => border_with_vector = value;
}
public Mode Chat_mode
{
get => text_mode;
set => text_mode = value;
}
public string Base_filename
{
get => b_filename;
set => b_filename = value;
}
public string[] Errors
{
get => errors;
set => errors = value;
}
// ====================================================================
// Helpful methods
// ====================================================================
/// <summary>
/// Method for outputting composition and position data to a csv file
/// </summary>
/// <param name="of">Filename</param>
/// <param name="x">Position</param>
/// <param name="c">Composition</param>
public static void FileWriteData_CSV(string of, RMatrix x, RMatrix y, RMatrix c)
{
string check_dir = Path.GetDirectoryName(of);
string owd;
if (check_dir == null)
{
string cwd = Directory.GetCurrentDirectory();
owd = Path.Combine(cwd, of);
}
else
{
owd = of;
}
if (File.Exists(owd)) { File.Delete(owd); }
FileStream fS = new(owd, FileMode.OpenOrCreate);
StreamWriter sW = new(fS);
string header = "x,y,c";
sW.WriteLine(header);
int nrows = x.GetnRows;
int ncols = x.GetnCols;
if (nrows > 0 && ncols > 0)
{
for (int i = 0; i < nrows; i++)
{
for (int j = 0; j < ncols; j++)
{
string line = x[i, j].ToString() + "," + y[i, j].ToString() + "," + c[i, j].ToString();
sW.WriteLine(line);
}
}
}
else
{
throw new Exception("No data available to write to the file!");
}
sW.Close();
fS.Close();
}
// =======================================================================
// Private methods
// =======================================================================
/// <summary>
/// Converts the enumerated boundary condition to a string
/// </summary>
/// <param name="bc">variable of type BoundaryCondition</param>
/// <returns></returns>
private static string ConvertEnumBCToString(ABoundaryCondition bc)
{
string result = bc switch
{
ABoundaryCondition.dirichlet => "Dirichlet",
ABoundaryCondition.neumann => "Neumann",
_ => "Dirichlet",
};
return result;
}
/// <summary>
/// Method to convert a string value to one of the enumerated boundary conditions
/// </summary>
/// <param name="value">string variable to be converted to the boundary condition enum</param>
/// <returns></returns>
private static ABoundaryCondition ConvertStringToEnumBC(string value)
{
ABoundaryCondition bc = value switch
{
"Dirichlet" => ABoundaryCondition.dirichlet,
"Neumann" => ABoundaryCondition.neumann,
_ => ABoundaryCondition.dirichlet,
};
return bc;
}
private static RVector ShapeMatrixToVector(RMatrix rm)
{
int nrows = rm.GetnRows;
int ncols = rm.GetnCols;
int nvals = nrows * ncols;
RVector rv = new(nvals);
int counter = 0;
for (int i = 0; i < nrows; i++)
{
for (int j = 0; j < ncols; j++)
{
rv[counter] = rm[i, j];
counter++;
}
}
return rv;
}
private static RMatrix ShapeVectorToMatrix(RVector rv, int nrows, int ncols)
{
RMatrix rm = new(nrows, ncols);
int counter = 0;
for (int i = 0; i < nrows; i++)
{
for (int j = 0; j < ncols; j++)
{
rm[i, j] = rv[counter];
counter++;
}
}
return rm;
}
// =======================================================================
//
// =======================================================================
// =======================================================================
// =======================================================================
}
}