-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
1115 lines (1084 loc) · 59.3 KB
/
Copy pathProgram.cs
File metadata and controls
1115 lines (1084 loc) · 59.3 KB
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
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
332
333
334
335
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#define pub
// #define debug
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Security;
using System.Threading;
namespace ParticleHoleCalculator
{
class Program
{
/// <summary>
/// 递归深度
/// </summary>
private static readonly int MAX_DEPTH = 100;
private static bool ISDEBUG = false;
/// <summary>
/// 存放可接受参数
/// </summary>
private static class _parameter
{
/// <summary>
/// -h/--Help 帮助
/// </summary>
public const string helpArg1 = "-h", helpArg2 = "--Help";
/// <summary>
/// -d/--DEBUG 调试
/// </summary>
public const string debugArg1 = "-D", debugArg2 = "--Debug";
/// <summary>
/// -i/--in 输入文件*
/// </summary>
public const string fileInputArg1 = "-i", fileInputArg2 = "--in";
/// <summary>
/// -o1/--out1 不同时间戳时孔洞总览
/// </summary>
public const string fileOut1Arg1 = "-o1", fileOut1Arg2 = "--out1";
/// <summary>
/// -o2/--out2 不同时间戳时各孔洞信息文件输出
/// </summary>
public const string fileOut2Arg1 = "-o2", fileOut2Arg2 = "--out2";
/// <summary>
/// -o3/--out3 不同时间戳时总粒子详细可视化文件输出
/// </summary>
public const string fileOut3Arg1 = "-o3", fileOut3Arg2 = "--out3";
/// <summary>
/// -b/--box 箱体模糊长(a,b,c)
/// </summary>
public const string boxArg1 = "-b", boxArg2 = "--box";
/// <summary>
/// -d/--density 箱体密度
/// </summary>
public const string densityArg1 = "-d", densityArg2 = "--density";
/// <summary>
/// -t/--threshold 箱体阈值
/// </summary>
public const string thresholdArg1 = "-t", thresholdArg2 = "--threshold";
}
private class parameter
{
private static bool needDebug;
private static StreamReader inputFile;
private static StreamWriter outputFile1;
private static StreamWriter outputFile2;
private static StreamWriter outputFile3;
private static ulong[] boxSideLength = new ulong[3] { 8, 8, 8 };
private static ulong density = 3;
private static ulong threshold = 1;
private const string helpInfo = "用法:ParticleHoleCalculator -i dump.lammpstrj -o dump_out_hole.lammpstrj [选项]... [参数]...\r\n" +
"#该程序依赖高IO,若使用较快磁盘可有效改善程序运行速率#\r\n" +
"根据dump.lammpstrj文件数据,以孔洞类别分类粒子,终端显示孔洞数与各类粒子占比,并输出同类型可视化文件dump_out_hole.lammpstrj\r\n" +
"默认关闭DEBUG详细回显(开启会降低速度)\r\n" +
"如果不指定参数则取默认参数\r\n" +
"\r\n" +
"各参数对长短选项同时适用,多同参数使用时参照后者参数。\r\n" +
" -h, --Help\t\t\t显示帮助信息\r\n" +
" -D, --Debug\t\t开启详细回显\r\n" +
" -i <FILE>, --in <FILE>\t输入文件路径*\r\n" +
" -o1 <FILE>, --out1 <FILE>\t不同时间戳时孔洞总览文件输出*\r\n" +
" -o2 <FILE>, --out2 <FILE>\t不同时间戳时各孔洞信息文件输出*\r\n" +
" -o3 <FILE>, --out3 <FILE>\t不同时间戳时总粒子详细可视化文件输出*\r\n" +
" -b <a,b,c>, --box <a,b,c>\t微元规格(长,宽,高,英文逗号分隔,只接受正整数)\r\n" +
"\t\t\t\t\t注:调参时若微元边长差过小可能会产生无效调参\r\n" +
"\t\t\t\t\t默认值:8,8,8\r\n" +
" -d <d>, --density <d>\t\t单微元内含粒子数阈值(只接受自然数)\r\n" +
"\t\t\t\t\t注:超出部分视作实心区,小于等于此值视为孔洞区\r\n" +
"\t\t\t\t\t默认值:3\r\n" +
" -t <t>, --threshold <t>\t微元链结个数阈值(只接受自然数)\r\n" +
"\t\t\t\t\t注:当孔洞含有微元数大于此值时才被视作孔洞,否则会被丢弃\r\n" +
"\t\t\t\t\t默认值:1\r\n" +
"\r\n" +
"例:./ParticleHoleCalculator -i ./dump2 -o1 ./out1.txt -o2 ./out2.txt -o3 ./out3.txt -b 10,10,10 -d 20 -t 2 -ND";
/// <summary>
/// 初步判断参数是否符合输入结构
/// </summary>
/// <param name="args">输入参数字符组</param>
/// <returns></returns>
private static bool isArgsLegal(string[] args)
{
if (args.Length != 0)
{
for (var i = 0; i < args.Length; i++)
switch (args[i])
{
case _parameter.helpArg1:
case _parameter.helpArg2:
return false;
case _parameter.debugArg1:
case _parameter.debugArg2:
needDebug = true;
break;
case _parameter.fileInputArg1:
case _parameter.fileInputArg2:
case _parameter.fileOut1Arg1:
case _parameter.fileOut1Arg2:
case _parameter.fileOut2Arg1:
case _parameter.fileOut2Arg2:
case _parameter.fileOut3Arg1:
case _parameter.fileOut3Arg2:
case _parameter.boxArg1:
case _parameter.boxArg2:
case _parameter.densityArg1:
case _parameter.densityArg2:
case _parameter.thresholdArg1:
case _parameter.thresholdArg2:
i++;
if (i >= args.Length)
return false;
break;
default:
return false;
}
return true;
}
return false;
}
/// <summary>
/// 判断储存后的参数是否满足必要需求
/// </summary>
/// <returns></returns>
private bool isArgsFull()
{
if (inputFile == null || outputFile1 == null || outputFile2 == null || outputFile3 == null)
return false;
return true;
}
/// <summary>
/// 解析参数并存储
/// </summary>
/// <param name="args"></param>
public parameter(string[] args)
{
if (isArgsLegal(args))
{
for (var i = 0; i < args.Length; i++)
switch (args[i])
{
case _parameter.debugArg1:
case _parameter.debugArg2:
#region DEBUG配置
needDebug = true;
break;
#endregion
case _parameter.fileInputArg1:
case _parameter.fileInputArg2:
#region 输入文件配置
i++;
try
{
inputFile = File.OpenText(@$"{args[i]}");
}
catch (UnauthorizedAccessException UAE)
{
logger(_loggerT.Fail, $"文件权限不足{Environment.NewLine}");
logger(_loggerT.DEBUG, $"{UAE.Message}{Environment.NewLine}");
Environment.Exit(-1);
}
catch (ArgumentException AE)
{
logger(_loggerT.Fail, $"文件未找到{Environment.NewLine}");
logger(_loggerT.DEBUG, $"{AE.Message}{Environment.NewLine}");
Environment.Exit(-1);
}
catch (PathTooLongException PTLE)
{
logger(_loggerT.Fail, $"输入文件名过长{Environment.NewLine}");
logger(_loggerT.DEBUG, $"{PTLE.Message}{Environment.NewLine}");
Environment.Exit(-1);
}
catch (DirectoryNotFoundException DNFE)
{
logger(_loggerT.Fail, $"指定的路径无效{Environment.NewLine}");
logger(_loggerT.DEBUG, $"{DNFE.Message}{Environment.NewLine}");
Environment.Exit(-1);
}
catch (FileNotFoundException FNFE)
{
logger(_loggerT.Fail, $"未找到指定的文件{Environment.NewLine}");
logger(_loggerT.DEBUG, $"{FNFE.Message}{Environment.NewLine}");
Environment.Exit(-1);
}
catch (NotSupportedException NSE)
{
logger(_loggerT.Fail, $"输入文件有误{Environment.NewLine}");
logger(_loggerT.DEBUG, $"{NSE.Message}{Environment.NewLine}");
Environment.Exit(-1);
}
finally
{
}
break;
#endregion
case _parameter.fileOut1Arg1:
case _parameter.fileOut1Arg2:
#region 不同时间戳时孔洞总览
i++;
try
{
outputFile1 = new StreamWriter(@$"{args[i]}", false);
}
catch (UnauthorizedAccessException UAE)
{
logger(_loggerT.Fail, $"无法创建文件1,文件权限不足,请检查--out1参数{Environment.NewLine}");
logger(_loggerT.DEBUG, $"{UAE.Message}{Environment.NewLine}");
Environment.Exit(-1);
}
catch (ArgumentException AE)
{
logger(_loggerT.Fail, $"无输出文件,请检查--out1参数{Environment.NewLine}");
logger(_loggerT.DEBUG, $"{AE.Message}{Environment.NewLine}");
Environment.Exit(-1);
}
catch (DirectoryNotFoundException DNFE)
{
logger(_loggerT.Fail, $"指定的路径无效,请检查--out1参数{Environment.NewLine}");
logger(_loggerT.DEBUG, $"{DNFE.Message}{Environment.NewLine}");
Environment.Exit(-1);
}
catch (PathTooLongException PTLE)
{
logger(_loggerT.Fail, $"输出文件名过长,请检查--out1参数{Environment.NewLine}");
logger(_loggerT.DEBUG, $"{PTLE.Message}{Environment.NewLine}");
Environment.Exit(-1);
}
catch (IOException IOE)
{
logger(_loggerT.Fail, $"输出文件文件名非法,请检查--out1参数{Environment.NewLine}");
logger(_loggerT.DEBUG, $"{IOE.Message}{Environment.NewLine}");
Environment.Exit(-1);
}
catch (SecurityException SE)
{
logger(_loggerT.Fail, $"无权限输出文件,请检查--out1参数{Environment.NewLine}");
logger(_loggerT.DEBUG, $"{SE.Message}{Environment.NewLine}");
Environment.Exit(-1);
}
finally
{
}
break;
#endregion
case _parameter.fileOut2Arg1:
case _parameter.fileOut2Arg2:
#region 不同时间戳时各孔洞信息文件输出
i++;
try
{
outputFile2 = new StreamWriter(@$"{args[i]}", false);
}
catch (UnauthorizedAccessException UAE)
{
logger(_loggerT.Fail, $"无法创建文件2,文件权限不足,请检查--out2参数{Environment.NewLine}");
logger(_loggerT.DEBUG, $"{UAE.Message}{Environment.NewLine}");
Environment.Exit(-1);
}
catch (ArgumentException AE)
{
logger(_loggerT.Fail, $"无输出文件,请检查--out2参数{Environment.NewLine}");
logger(_loggerT.DEBUG, $"{AE.Message}{Environment.NewLine}");
Environment.Exit(-1);
}
catch (DirectoryNotFoundException DNFE)
{
logger(_loggerT.Fail, $"指定的路径无效,请检查--out2参数{Environment.NewLine}");
logger(_loggerT.DEBUG, $"{DNFE.Message}{Environment.NewLine}");
Environment.Exit(-1);
}
catch (PathTooLongException PTLE)
{
logger(_loggerT.Fail, $"输出文件名过长,请检查--out2参数{Environment.NewLine}");
logger(_loggerT.DEBUG, $"{PTLE.Message}{Environment.NewLine}");
Environment.Exit(-1);
}
catch (IOException IOE)
{
logger(_loggerT.Fail, $"输出文件文件名非法,请检查--out2参数{Environment.NewLine}");
logger(_loggerT.DEBUG, $"{IOE.Message}{Environment.NewLine}");
Environment.Exit(-1);
}
catch (SecurityException SE)
{
logger(_loggerT.Fail, $"无权限输出文件,请检查--out2参数{Environment.NewLine}");
logger(_loggerT.DEBUG, $"{SE.Message}{Environment.NewLine}");
Environment.Exit(-1);
}
finally
{
}
break;
#endregion
case _parameter.fileOut3Arg1:
case _parameter.fileOut3Arg2:
#region 不同时间戳时总粒子详细可视化文件输出
i++;
try
{
outputFile3 = new StreamWriter(@$"{args[i]}", false);
}
catch (UnauthorizedAccessException UAE)
{
logger(_loggerT.Fail, $"无法创建文件3,文件权限不足,请检查--out3参数{Environment.NewLine}");
logger(_loggerT.DEBUG, $"{UAE.Message}{Environment.NewLine}");
Environment.Exit(-1);
}
catch (ArgumentException AE)
{
logger(_loggerT.Fail, $"无输出文件,请检查--out3参数{Environment.NewLine}");
logger(_loggerT.DEBUG, $"{AE.Message}{Environment.NewLine}");
Environment.Exit(-1);
}
catch (DirectoryNotFoundException DNFE)
{
logger(_loggerT.Fail, $"指定的路径无效,请检查--out3参数{Environment.NewLine}");
logger(_loggerT.DEBUG, $"{DNFE.Message}{Environment.NewLine}");
Environment.Exit(-1);
}
catch (PathTooLongException PTLE)
{
logger(_loggerT.Fail, $"输出文件名过长,请检查--out3参数{Environment.NewLine}");
logger(_loggerT.DEBUG, $"{PTLE.Message}{Environment.NewLine}");
Environment.Exit(-1);
}
catch (IOException IOE)
{
logger(_loggerT.Fail, $"输出文件文件名非法,请检查--out3参数{Environment.NewLine}");
logger(_loggerT.DEBUG, $"{IOE.Message}{Environment.NewLine}");
Environment.Exit(-1);
}
catch (SecurityException SE)
{
logger(_loggerT.Fail, $"无权限输出文件,请检查--out3参数{Environment.NewLine}");
logger(_loggerT.DEBUG, $"{SE.Message}{Environment.NewLine}");
Environment.Exit(-1);
}
finally
{
}
break;
#endregion
case _parameter.boxArg1:
case _parameter.boxArg2:
#region 单箱边长
i++;
var apr = args[i].Split(',');
if (apr.Length != 3)
{
logger(_loggerT.Fail, $"单箱参数个数有误{Environment.NewLine}");
Environment.Exit(-1);
}
else
{
try
{
for (var t = 0; t < 3; t++)
{
boxSideLength[t] = Convert.ToUInt64(apr[t]);
if (boxSideLength[t] == 0)
{
logger(_loggerT.Fail, $"单箱边长不能为0{Environment.NewLine}");
Environment.Exit(-1);
}
}
}
catch (FormatException FE)
{
logger(_loggerT.Fail, $"单箱边长非整或存在特殊符号{Environment.NewLine}");
logger(_loggerT.DEBUG, $"{FE.Message}{Environment.NewLine}");
Environment.Exit(-1);
}
catch (OverflowException OFE)
{
logger(_loggerT.Fail, $"单箱边长大小超出边界{Environment.NewLine}");
logger(_loggerT.DEBUG, $"{OFE.Message}{Environment.NewLine}");
Environment.Exit(-1);
}
}
break;
#endregion
case _parameter.densityArg1:
case _parameter.densityArg2:
#region 粒子阈值密度
i++;
try
{
density = Convert.ToUInt64(args[i]);
if (density == 0)
{
logger(_loggerT.Fail, $"粒子阈值密度不能为0{Environment.NewLine}");
Environment.Exit(-1);
}
}
catch (FormatException FE)
{
logger(_loggerT.Fail, $"粒子阈值密度非整或存在特殊符号{Environment.NewLine}");
logger(_loggerT.DEBUG, $"{FE.Message}{Environment.NewLine}");
Environment.Exit(-1);
}
catch (OverflowException OFE)
{
logger(_loggerT.Fail, $"粒子阈值密度数字大小超出边界{Environment.NewLine}");
logger(_loggerT.DEBUG, $"{OFE.Message}{Environment.NewLine}");
Environment.Exit(-1);
}
break;
#endregion
case _parameter.thresholdArg1:
case _parameter.thresholdArg2:
#region 箱数阈值
i++;
try
{
threshold = Convert.ToUInt64(args[i]);
}
catch (FormatException FE)
{
logger(_loggerT.Fail, $"箱数阈值非整或存在特殊符号{Environment.NewLine}");
logger(_loggerT.DEBUG, $"{FE.Message}{Environment.NewLine}");
Environment.Exit(-1);
}
catch (OverflowException OFE)
{
logger(_loggerT.Fail, $"箱数阈值数字大小超出边界{Environment.NewLine}");
logger(_loggerT.DEBUG, $"{OFE.Message}{Environment.NewLine}");
Environment.Exit(-1);
}
break;
#endregion
}
}
else
{
DisplayHelp();
Environment.Exit(-1);
}
if (!isArgsFull())
Environment.Exit(-1);
}
//public override string ToString() => $"{(needDebug?"已开启DEBUG模式{Environment.NewLine}":"")}输入文件:{in}"; public bool IsNeedHelp() => needAnyHelp;
public bool IsNeedDebug() => needDebug;
public StreamReader GetStreamReader() => inputFile;
public StreamWriter GetStreamWriter1() => outputFile1;
public StreamWriter GetStreamWriter2() => outputFile2;
public StreamWriter GetStreamWriter3() => outputFile3;
public ulong GetBoxSideLength(int i) => boxSideLength[i];
public ulong GetDensity() => density;
public ulong GetThreshold() => threshold;
public void DisplayHelp() => logger(_loggerT.None, helpInfo);
}
/// <summary>
/// 日志记录分类
/// </summary>
private enum _loggerT { None = -2, Note = -1, Info = 0, Success = 1, Fail = 2, DEBUG = 3, Warning = 4, Alert = 5, ANS = 99 }
/// <summary>
/// /// 日志输出器
/// </summary>
/// <param name="type">记录种类</param>
/// <param name="inf">记录信息</param>
private static void logger(_loggerT type, string inf)
{
switch (type)
{
case _loggerT.Info:
if (ISDEBUG)
{
ConsoleColor InfoBackColor = Console.BackgroundColor, InfoForeColor = ConsoleColor.DarkGreen;
Console.BackgroundColor = InfoBackColor;
Console.ForegroundColor = InfoForeColor;
Console.Write($"[INFO] {inf}");
Console.ResetColor();
}
break;
case _loggerT.Note:
if (ISDEBUG)
{
ConsoleColor NoteBackColor = Console.BackgroundColor, NoteForeColor = ConsoleColor.Cyan;
Console.BackgroundColor = NoteBackColor;
Console.ForegroundColor = NoteForeColor;
Console.Write($"[NOTE] {inf}");
Console.ResetColor();
}
break;
case _loggerT.Success:
ConsoleColor SuccessBackColor = Console.BackgroundColor, SuccessForeColor = ConsoleColor.Magenta;
Console.BackgroundColor = SuccessBackColor;
Console.ForegroundColor = SuccessForeColor;
Console.Write($"[+] {inf}");
Console.ResetColor();
break;
case _loggerT.Fail:
ConsoleColor FailBackColor = Console.BackgroundColor, FailForeColor = ConsoleColor.Red;
Console.BackgroundColor = FailBackColor;
Console.ForegroundColor = FailForeColor;
Console.Write($"[-] {inf}");
Console.ResetColor();
break;
case _loggerT.DEBUG:
if (ISDEBUG)
{
ConsoleColor DEBUGBackColor = Console.BackgroundColor, DEBUGForeColor = ConsoleColor.DarkGray;
Console.BackgroundColor = DEBUGBackColor;
Console.ForegroundColor = DEBUGForeColor;
Console.Write($"[DEBUG]: {inf}");
Console.ResetColor();
}
break;
case _loggerT.Warning:
if (ISDEBUG)
{
ConsoleColor WarningBackColor = Console.BackgroundColor, WarningForeColor = ConsoleColor.DarkBlue;
Console.BackgroundColor = WarningBackColor;
Console.ForegroundColor = WarningForeColor;
Console.Write($"[WARNING] {inf}");
Console.ResetColor();
}
break;
case _loggerT.Alert:
if (ISDEBUG)
{
ConsoleColor AlertBackColor = Console.BackgroundColor, AlertForeColor = ConsoleColor.Yellow;
Console.BackgroundColor = AlertBackColor;
Console.ForegroundColor = AlertForeColor;
Console.Write($"[AlERT] {inf}");
Console.ResetColor();
}
break;
case _loggerT.ANS:
if (ISDEBUG)
{
ConsoleColor ANSBackColor = ConsoleColor.White, ANSForeColor = ConsoleColor.Black;
Console.BackgroundColor = ANSBackColor;
Console.ForegroundColor = ANSForeColor;
Console.Write($"[ANSWER] {inf}");
Console.ResetColor();
}
break;
default:
Console.BackgroundColor = Console.BackgroundColor;
Console.ForegroundColor = Console.ForegroundColor;
Console.Write($"{inf}");
Console.ResetColor();
break;
}
}
private struct dot
{
public double x, y, z;
public dot(double _x, double _y, double _z)
{ x = _x; y = _y; z = _z; }
public override string ToString() => $"({x:.000},{y:.000},{z:.000})";
public string ToString(char sep) => $"{x:.000}{sep}{y:.000}{sep}{z:.000}";
}
/// <summary>
/// 单原子数据结构
/// </summary>
private struct ATOM
{
public ulong id;
public int type;
public dot pos;
public ATOM(ulong _id, int _type, double _x, double _y, double _z)
{ id = _id; type = _type; pos.x = _x; pos.y = _y; pos.z = _z; }
public ATOM(ulong _id, int _type, dot _dot)
{ id = _id; type = _type; pos.x = _dot.x; pos.y = _dot.y; pos.z = _dot.z; }
public void ChangeType(ulong box_id, bool istype1)
{
type = (int)((istype1 ? 2 * box_id - 1 : 2 * box_id));
}
public override string ToString() => $"{id} {type} {pos.x} {pos.y} {pos.z}";
}
/// <summary>
/// 单时间帧数据结构
/// 注:atomsNum[n]对应的是id为n+1的粒子
/// </summary>
private struct Unit
{
public readonly Guid uuid;
public readonly ulong timestep;
public readonly ulong atomsNum;
public readonly double[,] boxEdge = new double[3, 2];
public ATOM[] atomsData;
public Unit(ulong _timestep, ulong _atomsNum, double[,] _boxEdge)
{
this.uuid = Guid.NewGuid();
this.timestep = _timestep;
this.atomsNum = _atomsNum;
this.atomsData = new ATOM[_atomsNum];
this.boxEdge = _boxEdge;
}
/// <summary>
/// 根据字符串类型向atomsData结构添加原子
/// </summary>
/// <param name="input"></param>
public void AddAtom(String input)
{
var _input = input.Split(' ');
if (_input.Length < 5)
{
logger(_loggerT.Warning, $"此数据集坐标数据量不足:{input}{Environment.NewLine}");
Environment.Exit(-1);
}
else if (_input.Length > 5)
{
logger(_loggerT.Warning, $"此数据集坐标有多余数据");
}
ulong _id;
int _type;
double _x, _y, _z;
try
{
Convert.ToUInt64(_input[0]);
Convert.ToInt32(_input[1]);
Convert.ToDouble(_input[2]);
Convert.ToDouble(_input[3]);
Convert.ToDouble(_input[4]);
}
catch (FormatException FE)
{
logger(_loggerT.Fail, $"粒子阈值密度非整或存在特殊符号{Environment.NewLine}");
logger(_loggerT.DEBUG, $"{FE.Message}{Environment.NewLine}");
Environment.Exit(-1);
}
catch (OverflowException OFE)
{
logger(_loggerT.Fail, $"粒子阈值密度数字大小超出边界{Environment.NewLine}");
logger(_loggerT.DEBUG, $"{OFE.Message}{Environment.NewLine}");
Environment.Exit(-1);
}
finally
{
_id = Convert.ToUInt64(_input[0]);
_type = Convert.ToInt32(_input[1]);
_x = Convert.ToDouble(_input[2]);
_y = Convert.ToDouble(_input[3]);
_z = Convert.ToDouble(_input[4]);
}
ATOM _atom = new ATOM(_id, _type, _x, _y, _z);
this.atomsData[_id - 1] = _atom;
// x边界溢出检查
if (_x < this.boxEdge[0, 0])
{
logger(_loggerT.Warning, $"ATOM:{_id} [x]:{_x:0.000} 超出界限 [{this.boxEdge[0, 0]:0.000},{this.boxEdge[0, 1]:0.000}){Environment.NewLine}");
this.boxEdge[0, 0] = _x;
logger(_loggerT.Note, $"新边界 [{this.boxEdge[0, 0]:0.000},{this.boxEdge[0, 1]:0.000}) 建立{Environment.NewLine}");
}
else if (this.boxEdge[0, 1] <= _x)
{
logger(_loggerT.Warning, $"ATOM:{_id} [x]:{_x:0.000} 超出界限 [{this.boxEdge[0, 0]:0.000},{this.boxEdge[0, 1]:0.000}){Environment.NewLine}");
this.boxEdge[0, 1] = _x + 1;
logger(_loggerT.Note, $"新边界 [{this.boxEdge[0, 0]:0.000},{this.boxEdge[0, 1]:0.000}) 建立{Environment.NewLine}");
}
// y界溢出检查
if (_y < this.boxEdge[1, 0])
{
logger(_loggerT.Warning, $"ATOM:{_id} [y]:{_y:0.000} 超出界限 [{this.boxEdge[1, 0]:0.000},{this.boxEdge[1, 1]:0.000}){Environment.NewLine}");
this.boxEdge[1, 0] = _y;
logger(_loggerT.Note, $"新边界 [{this.boxEdge[1, 0]:0.000},{this.boxEdge[1, 1]:0.000}) 建立{Environment.NewLine}");
}
else if (this.boxEdge[1, 1] <= _y)
{
logger(_loggerT.Warning, $"ATOM:{_id} [y]:{_y:0.000} 超出界限 [{this.boxEdge[1, 0]:0.000},{this.boxEdge[1, 1]:0.000}){Environment.NewLine}");
this.boxEdge[1, 1] = _y + 1;
logger(_loggerT.Note, $"新边界 [{this.boxEdge[1, 0]:0.000},{this.boxEdge[1, 1]:0.000}) 建立{Environment.NewLine}");
}
// z边界溢出检查
if (_z < this.boxEdge[2, 0])
{
logger(_loggerT.Warning, $"ATOM:{_id} [z]:{_z:0.000} 超出界限 [{this.boxEdge[2, 0]:0.000},{this.boxEdge[2, 1]:0.000}){Environment.NewLine}");
this.boxEdge[2, 0] = _z;
logger(_loggerT.Note, $"新边界 [{this.boxEdge[2, 0]:0.000},{this.boxEdge[2, 1]:0.000}) 建立{Environment.NewLine}");
}
else if (this.boxEdge[2, 1] <= _z)
{
logger(_loggerT.Warning, $"ATOM:{_id} [z]:{_z:0.000} 超出界限 [{this.boxEdge[2, 0]:0.000},{this.boxEdge[2, 1]:0.000}){Environment.NewLine}");
this.boxEdge[2, 1] = _z + 1;
logger(_loggerT.Note, $"新边界 [{this.boxEdge[2, 0]:0.000},{this.boxEdge[2, 1]:0.000}) 建立{Environment.NewLine}");
}
}
/// <summary>
/// 根据a,b,c模糊箱长与atomLimit限制计算atomsData中孔洞数量与孔洞信息并输出文件
/// </summary>
/// <param name="a">模糊箱长</param>
/// <param name="b">模糊箱宽</param>
/// <param name="c">模糊箱高</param>
/// <param name="boxDensity">粒子密度</param>
/// <param name="threshold">孔洞箱阈值 </param>
/// <param name="outFile3">输出文件流</param>
/// <returns></returns>
public ulong CountHoleAndOutputFile(ulong a, ulong b, ulong c, ulong boxDensity, ulong threshold, StreamWriter outFile1, StreamWriter outFile2, StreamWriter outFile3)
{
// 边长模糊算法
// 各轴分离箱数
ulong x_n = (ulong)(Math.Abs(boxEdge[0, 1] - boxEdge[0, 0])) / a;
ulong y_n = (ulong)(Math.Abs(boxEdge[1, 1] - boxEdge[1, 0])) / b;
ulong z_n = (ulong)(Math.Abs(boxEdge[2, 1] - boxEdge[2, 0])) / c;
if (x_n == 0 || y_n == 0 || z_n == 0)
{
logger(_loggerT.Fail, $"微元箱体过大,超出边界");
Environment.Exit(-1);
}
// 各轴箱单位长
double x_i = Math.Abs(boxEdge[0, 1] - boxEdge[0, 0]) / (double)(x_n);
double y_i = Math.Abs(boxEdge[1, 1] - boxEdge[1, 0]) / (double)(y_n);
double z_i = Math.Abs(boxEdge[2, 1] - boxEdge[2, 0]) / (double)(z_n);
#region 内存占用声明拓展
// 三维坐标标签ATOM数组
List<List<List<List<ATOM>>>> box = new List<List<List<List<ATOM>>>>();
// 初始化
for (ulong i = 0; i < x_n; i++)
{
box.Add(new List<List<List<ATOM>>>());
for (ulong j = 0; j < y_n; j++)
{
box[(int)i].Add(new List<List<ATOM>>());
for (ulong k = 0; k < z_n; k++)
box[(int)i][(int)j].Add(new List<ATOM>());
}
}
#endregion
ulong lType1Counter = 0, lType2Counter = 0;
#region 扔箱
for (ulong i = 0; i < this.atomsNum; i++)
{
var tar = this.atomsData[i];
if (tar.type == 1) lType1Counter++; else lType2Counter++;
for (int j = 0; j < 3; j++)
Debug.Assert(boxEdge[j, 0] < boxEdge[j, 1]);
#if debug
logger(_loggerT.DEBUG, $"ATOM:{tar.id}\t{(int)((tar.pos.x - this.boxEdge[0, 0]) / x_i)}\t" +
$"{(int)((tar.pos.y - this.boxEdge[1, 0]) / y_i)}\t" +
$"{(int)((tar.pos.z - this.boxEdge[2, 0]) / z_i)}{Environment.NewLine}");
logger(_loggerT.DEBUG, $"{this.boxEdge[0, 0]} - {tar.pos.x} - {this.boxEdge[0, 1]}\t" +
$"{this.boxEdge[1, 0]} - {tar.pos.y} - {this.boxEdge[1, 1]}\t" +
$"{this.boxEdge[2, 0]} - {tar.pos.z} - {this.boxEdge[2, 1]}{Environment.NewLine}");
Debug.Assert(tar.pos.x >= this.boxEdge[0, 0] && tar.pos.x <= this.boxEdge[0, 1]);
Debug.Assert(tar.pos.y >= this.boxEdge[1, 0] && tar.pos.y <= this.boxEdge[1, 1]);
Debug.Assert(tar.pos.z >= this.boxEdge[2, 0] && tar.pos.z <= this.boxEdge[2, 1]);
#endif
box[(int)((tar.pos.x - this.boxEdge[0, 0]) / x_i)]
[(int)((tar.pos.y - this.boxEdge[1, 0]) / y_i)]
[(int)((tar.pos.z - this.boxEdge[2, 0]) / z_i)].Add(tar);
}
#endregion
#if debug
for (ulong i = 0; i < x_n; i++)
{
logger(_loggerT.None, $"#x:{i}");
for (ulong k = 0; k < z_n; k++)
logger(_loggerT.None, $"\t#z:[{k}]");
logger(_loggerT.None, $"{Environment.NewLine}");
for (ulong j = 0; j < y_n; j++)
{
logger(_loggerT.None, $"y:[{j}]");
for (ulong k = 0; k < z_n; k++)
logger(_loggerT.None, $"\t{box[(int)i][(int)j][(int)k].Count}");
logger(_loggerT.None, $"{Environment.NewLine}");
}
logger(_loggerT.None, $"{Environment.NewLine}");
}
#endif
#region BC二元分类
bool[,,] box_score = new bool[x_n, y_n, z_n];
for (ulong i = 0; i < x_n; i++)
for (ulong j = 0; j < y_n; j++)
for (ulong k = 0; k < z_n; k++)
box_score[i, j, k] = ((ulong)(box[(int)i][(int)j][(int)k].Count) <= boxDensity);
#endregion
#if debug
for (ulong i = 0; i < x_n; i++)
{
logger(_loggerT.None, $"#{i}:{Environment.NewLine}");
for (ulong k = 0; k < z_n + 2; k++)
logger(_loggerT.None, $"*");
logger(_loggerT.None, $"{Environment.NewLine}");
for (ulong j = 0; j < y_n; j++)
{
logger(_loggerT.None, $"*");
for (ulong k = 0; k < z_n; k++)
logger(_loggerT.None, (box_score[i, j, k] ? "#" : " "));
logger(_loggerT.None, $"*{Environment.NewLine}");
}
for (ulong k = 0; k < z_n + 2; k++)
logger(_loggerT.None, $"*");
logger(_loggerT.None, $"{Environment.NewLine}");
}
#endif
#region 前端BFS算法 & 输出
outFile2.Write($"{timestep}");
outFile3.Write($"ITEM: TIMESTEP{Environment.NewLine}" +
$"{timestep}{Environment.NewLine}" +
$"ITEM: NUMBER OF ATOMS{Environment.NewLine}" +
$"{atomsNum}{Environment.NewLine}" +
$"ITEM: BOX BOUNDS pp pp pp{Environment.NewLine}" +
$"{boxEdge[0, 0]:0.0000000000000000e+00} {boxEdge[0, 1]:0.0000000000000000e+00}{Environment.NewLine}" +
$"{boxEdge[1, 0]:0.0000000000000000e+00} {boxEdge[1, 1]:0.0000000000000000e+00}{Environment.NewLine}" +
$"{boxEdge[2, 0]:0.0000000000000000e+00} {boxEdge[2, 1]:0.0000000000000000e+00}{Environment.NewLine}" +
$"ITEM: ATOMS id type x y z{Environment.NewLine}");
ulong holesNum = 0;
ATOM[] atomsArr = atomsData;
ulong type1Counter = 0, type2Counter = 0;
for (int p = 0; p < atomsArr.Length; p++)
atomsArr[p].type = -1;
Debug.Assert((ulong)atomsArr.Length == atomsNum);
for (long i = 0; (ulong)i < x_n; i++)
for (long j = 0; (ulong)j < y_n; j++)
for (long k = 0; (ulong)k < z_n; k++)
if (box_score[i, j, k])
{
#region 断层堆栈
var result = frontBFS(ref box_score, new long[] { i, j, k }, 0);
var tmpResult = result;
while (true)
{
var moreResult = new List<dot>();
foreach (var item in tmpResult)
moreResult.AddRange(frontBFS(ref box_score, new long[] { (long)item.x, (long)item.y, (long)item.z }, 0));
if (moreResult.Count == 0)
{ break; }
else
{
result.AddRange(moreResult);
tmpResult = moreResult;
}
};
#endregion
#region 后续处理
if ((ulong)result.Count > threshold)
{
// 孔洞计数器
holesNum++;
// 新组粒子信息储存
// logger(_loggerT.None, $"{Environment.NewLine}");
// logger(_loggerT.Success, $"Hole #{holesNum}:{Environment.NewLine}");
// logger(_loggerT.Info, $"BoxNum:{result.Count}{Environment.NewLine}");
ulong type1Num = 0, type2Num = 0;
foreach (var item in result)
{
// logger(_loggerT.None, $"id\ttype\tx.\ty\tz{Environment.NewLine}");
foreach (var atom_item in box[(int)item.x][(int)item.y][(int)item.z])
{
Debug.Assert(atom_item.id > 0);
var isType1 = (atom_item.type == 1);
atomsArr[(int)(atom_item.id - 1)].ChangeType(holesNum, isType1);
// logger(_loggerT.None, $"{atom_item.id}\t{atom_item.type}\t{atom_item.pos.ToString('\t')}{Environment.NewLine}");
if (isType1) type1Num++; else type2Num++;
}
}
type1Counter += type1Num;
type2Counter += type2Num;
logger(_loggerT.DEBUG, $"BoxNum:{type1Num + type2Num}\ttype1(n/o):{type1Num}\\{((double)type1Num * 100 / (double)(type1Num + type2Num)):0.00}%\ttype2(n/o):{type2Num}\\{((double)type2Num * 100 / (double)(type1Num + type2Num)):0.00}%{Environment.NewLine}");
outFile2.Write($"\t{type1Num + type2Num}\t{type1Num}\t{type2Num}");
}
#endregion
}
outFile2.Write($"{Environment.NewLine}");
logger(_loggerT.Info, $"AtomNum:{type1Counter + type2Counter}\ttype1(n/o):{type1Counter}\\{((double)type1Counter * 100 / (double)(type1Counter + type2Counter)):0.00}%\ttype2(n/o):{type2Counter}\\{((double)type2Counter * 100 / (double)(type1Counter + type2Counter)):0.00}%{Environment.NewLine}");
outFile1.WriteLine($"{timestep}\t{holesNum}\t{type1Counter}\t{type2Counter}\t{lType1Counter - type1Counter}\t{lType2Counter-type2Counter}");
for (int p = 0; p < atomsArr.Length; p++)
outFile3.WriteLine(atomsArr[p].ToString());
//foreach (var item in atomsArr)
//outFile.WriteLine(item.ToString());
#endregion
return holesNum;
}
}
/// <summary>
/// 返回连接的所有为T的坐标集,一次最多深度为MAX_DEPTH,防止stack overflow,会截止,数据会不全,需多次遍历
/// </summary>
/// <param name="box_score">三维BC分类表bool[x,y,z]</param>
/// <param name="loc">初始坐标{x,y,z}</param>
/// <returns></returns>
static List<dot> frontBFS(ref bool[,,] box_score, long[] loc, int depth)
{
//logger(_loggerT.None, $"{loc[0]},{loc[1]},{loc[2]}\t");
List<dot> dots = new List<dot>();
//Debug.Assert(box_score[loc[0], loc[1], loc[2]]);
if (box_score[loc[0], loc[1], loc[2]])
{
//logger(_loggerT.DEBUG, $"ADD dot[{loc[0]}, {loc[1]}, {loc[2]}]{Environment.NewLine}");
box_score[loc[0], loc[1], loc[2]] = false;
dots.Add(new dot(loc[0], loc[1], loc[2]));
}
for (ulong i = 0; i < 3; i++)
{
var x = loc[0] + (i == 0 ? 1 : 0);
var y = loc[1] + (i == 1 ? 1 : 0);
var z = loc[2] + (i == 2 ? 1 : 0);
if (loc[i] == box_score.GetLength((int)i) - 1)
{
x = (i == 0 ? 0 : x);
y = (i == 1 ? 0 : y);
z = (i == 2 ? 0 : z);
}
if (box_score[x, y, z])
if (depth < MAX_DEPTH)
dots.AddRange(frontBFS(ref box_score, new long[] { x, y, z }, ++depth));
x = loc[0] - (i == 0 ? 1 : 0);
y = loc[1] - (i == 1 ? 1 : 0);
z = loc[2] - (i == 2 ? 1 : 0);
if (loc[i] == 0)
{
x = (i == 0 ? box_score.GetLength((int)i) - 1 : x);
y = (i == 1 ? box_score.GetLength((int)i) - 1 : y);
z = (i == 2 ? box_score.GetLength((int)i) - 1 : z);
}
if (box_score[x, y, z])
if (depth < MAX_DEPTH)
dots.AddRange(frontBFS(ref box_score, new long[] { x, y, z }, ++depth));
}
return dots;
}
#if debug
static void disUnitInfo(Unit unit)
{
logger(_loggerT.DEBUG, $"{unit.uuid.ToString("B")}{Environment.NewLine}" +
$"TIMESTEP:{unit.timestep}{Environment.NewLine}" +
$"Number of atoms:{unit.atomsNum}{Environment.NewLine}" +
$"Box bounds:{Environment.NewLine}" +
$"{unit.boxEdge[0, 0]}\t{unit.boxEdge[0, 1]}{Environment.NewLine}" +
$"{unit.boxEdge[1, 0]}\t{unit.boxEdge[1, 1]}{Environment.NewLine}" +
$"{unit.boxEdge[2, 0]}\t{unit.boxEdge[2, 1]}{Environment.NewLine}"
);
logger(_loggerT.DEBUG, $"Please input id to get atom data:");
string _index_str = String.Empty;
try
{
while ((_index_str = Console.ReadLine()) != String.Empty)
{
ulong _index = Convert.ToUInt64(_index_str);
var tmp = unit.atomsData[_index - 1];
logger(_loggerT.None, $"id\ttype\tx\ty\tz{Environment.NewLine}"
+ $"{tmp.id}\t{tmp.type}\t{tmp.pos.x}\t{tmp.pos.y}\t{tmp.pos.z}{Environment.NewLine}");
logger(_loggerT.DEBUG, $"Please input id to get atom data:");
}
}
catch
{
Environment.Exit(-1);
}
}
#endif
static void Main(string[] args)
{
// 转化参数
parameter _para = new parameter(args);
ISDEBUG = _para.IsNeedDebug();
#region 初始化,系统检查
if (Stopwatch.IsHighResolution)
logger(_loggerT.Success, $"HRPC ENABLED{Environment.NewLine}");
else
logger(_loggerT.Fail, $"high-resolution performance counter is not supported on this device{Environment.NewLine}");
logger(_loggerT.Success, $"TF:{Stopwatch.Frequency},ACC:{(1000L * 1000L * 1000L) / Stopwatch.Frequency} ns{Environment.NewLine}");
#endregion
Stopwatch watch = new Stopwatch(), watchSum = new Stopwatch();