static_check.awk 4.66 KB
Newer Older
1
#!/usr/bin/awk -f 
Eoin Clerkin's avatar
Eoin Clerkin committed
2
3
4
5
6
7
8
9
10
11
12
13
14
#
# Static test to check the atomic number against the atomic mass for inclusions in the geometry media.geo file. Output may be used to spot typos and other human errors.
#
# Author E.Clerkin
# COPYRIGHT: FAIR GMBH 2020
#
#
# Useage: awk -f static_check.awk media.geo
# Must include LIST_ELEMENTS file in same directory.

function abs(x){return (x>=0) ? x : -x}

BEGIN{
15
16
17
18
19

ACCEPTABLE_ERROR=0.01 # 1 % ERROR TOLERANCE FOR STATIC CHECK. 

FAIL=0;  # Flag for determining success. If any check fails then this value is changed.

Eoin Clerkin's avatar
Eoin Clerkin committed
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
while(getline < "LIST_ELEMENTS"){
	 ELEMENT_NAME[$1]=$2;
	 ELEMENT_SYMBOL[$1]=$3;
	 ELEMENT_MASS[$1]=$4;
	# Index $1 is the Atomic Number of the Element
	};
};


{
delete datum;

FS="[\t ]*"; # Spaces and tags as field seperators in media.geo

for (i = 1; i<=NF; i++)datum[i]=$i; # Loading in a line from media.geo fle to make it easy to work with.

36
37
38
	RELATIVE_SUM=0;
	content_by="weight"; # The relative content is by number of each atom or by relative weight of each atom.

Eoin Clerkin's avatar
Eoin Clerkin committed
39
40
41
42
43
44
45
46
	if($1!="//" && $1!="0" && $1!="1" && $1!="" ){  # Excluding lines that are not first line in definition of medium
		print "############################################################################################################";
		getline # Obtaining information from subsequent line for additional flags.
		sensflag=$2
		fldflag=$3
		fld=$4
		epsil=$5
		declare sensor;
47

Eoin Clerkin's avatar
Eoin Clerkin committed
48
49
50
51
52
53
54
55
56
57
58
		if(sensflag==0)  # Is the material active?
		{
		sensor="PASSIVE";
		}
		else
		{
		sensor=" ACTIVE";
		}

		print datum[1], sensor; # Material and Active status.

59
60
61
62
63
		if(datum[2]<0){
			content_by="number"  # Relative number
			print "Containts by numer";
			};

Eoin Clerkin's avatar
Eoin Clerkin committed
64
65
66
67
68
69
70
71
		datum[2] = (datum[2]>0)? datum[2] :  -1*datum[2];    # Making the NCOMP parameter always positive. 
		# NCOMP sign controlls the constitutes ratio definitions and is not needed for these tests.

		printf "Number of constitutes: %d\n", datum[2];  # Datum[2] has integer value of number of elements.
			for(i=1;i<=datum[2];++i){  # Skips throw each of the components.
			printf "Atomic Number: %s\tAtomic Mass: %s\n", datum[2+i+datum[2]], datum[2+i]; # See https://fairroot.gsi.de/?q=node/34 for info regarding format ordering.
			printf "AN[%d]=> %s : Mass = %s\n", datum[2+i+datum[2]], ELEMENT_NAME[strtonum(datum[2+i+datum[2]])], ELEMENT_MASS[strtonum(datum[2+i+datum[2]])];

72
73
74
75
76
77
			# datum[2] is the number of components
			# datum[2 + i] is subsequent component's atomic number
			# datum[2 + i + datum[2]] is subsequenct component's atomic weight
			# datum[2 + 2*datum[2] + 1] is the overall density of the material
			# datum[3 + 2*datum[2] + i] is the subsequent relative amount of each compoment.

78
#			printf "PAUSE: %s %s %s", datum[2+i], ELEMENT_MASS[strtonum(datum[2+i+datum[2]])], ERROR;
79
80
			printf "1 MASS ENTERED %s \n",  datum[2+i];
			printf "2 ELEMENT MASS %s \n",  ELEMENT_MASS[strtonum(datum[2+i+datum[2]])];
81

82
83
84
85
			if ( length(datum[2+i])!=0 && length(ELEMENT_MASS[strtonum(datum[2+i+datum[2]])])!=0 ){
			ERROR = abs( (datum[2+i] - ELEMENT_MASS[strtonum(datum[2+i+datum[2]])] )/datum[2+i])
			};			
			
86
87
88
89
90
91
92
93
94
			if(datum[2]>1){ # Checks relatavant for more than one component.

					if(content_by~"number"){ # When the elements are per their relative number.
					printf "%s", ELEMENT_SYMBOL[strtonum(datum[2+i+datum[2]])];
					printf "%s\n", datum[3 + 2*datum[2] + i];
					CHEMISTRY[ELEMENT_SYMBOL[strtonum(datum[2+i+datum[2]])]]=datum[3 + 2*datum[2] + i];
					};

					if(content_by~"weight"){ # When the elements are per weight
95
96
97
					printf "RELATIVE AMOUNT: %0.2f%% \n", 100*datum[3 + 2*datum[2] + i];
					RELATIVE_SUM+=datum[3 + 2*datum[2] + i];
					};
98
			};
Eoin Clerkin's avatar
Eoin Clerkin committed
99
100
101
102

			printf "ERROR IS %f \t",  ERROR;
				if (ERROR > ACCEPTABLE_ERROR) {
				FAIL=1;
103
				printf "CHECK FAILED\n\n"
Eoin Clerkin's avatar
Eoin Clerkin committed
104
				} else {
105
				printf "CHECK PASSED\n\n"
Eoin Clerkin's avatar
Eoin Clerkin committed
106
107
				};
			};
108
109
110
111

			# Check if the amount of material adds up to 100%
			if(datum[2]>1 && content_by~"weight"){
			printf "TOTAL AMOUNT COUNT RELATIVE SUMS: %0.2f%% \t\t\t", 100*RELATIVE_SUM;
112
				if (RELATIVE_SUM<=1+ACCEPTABLE_ERROR && RELATIVE_SUM>=1-ACCEPTABLE_ERROR ){
113
114
115
				printf "CHECK PASSED! %s \n\n", RELATIVE_SUM;
				}else{
				printf "CHECK FAILED! %s \n\n", RELATIVE_SUM;
116
				FAIL=1;
117
118
119
120
				};
			};


121
122
123
124
125
			# Chemical Formula Attempt. For visual inspection by human.
			if(datum[2]>1 && content_by~"number"){
			printf "Contents by the number of atoms: \n"

			for( ele in CHEMISTRY )printf "%s%s", ele, CHEMISTRY[ele];
Eoin Clerkin's avatar
Eoin Clerkin committed
126

127
128
129
130
131
132
133
134
#			ELEMENT_SYMBOL[strtonum(datum[2+i+datum[2]])]
#			CHEMISTRY[]

			};


		print " " # Spacing
		delete CHEMISTRY;
Eoin Clerkin's avatar
Eoin Clerkin committed
135
136
137
138
139
140
141
142
143
144
145
	};
}

END{

print "------------------------------------------------------------------------------------------------------------"

printf "STATIC TEST: "
if ( ! FAIL ) { printf "PASSED\n" }
if ( FAIL ) { printf "FAILED\n"; exit 1 }
}