summaryrefslogtreecommitdiff
path: root/harmoneff/harmoneff.pas
blob: 20b273a3c116dfce0b5ff3477cffad38b03cc7bd (plain)
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
program harmoneff;

uses sysutils, myMath, classes, process, math;

Type TTodo = record
              p_pol,
              transmitted,
              plasma:      boolean;
              order:       integer;
             end;
     TLookAt = record
                key,name: string;
               end;

var datadir: string;

function catchValue(fnam,key,name: string): string;
var f:  textfile;
    s:  string;
    kf: boolean;
begin
 if not fileexists(fnam) then Writeln(fnam);
 assign(f,fnam);
 reset(f);
 kf:=false;
 while not eof(f) do
  begin
   readln(f,s);
   if pos('#',s)>0 then s:= copy(s,1,pos('#',s)-1);
   while pos(' ',s)>0 do
    delete(s,pos(' ',s),1);
   while pos(#9,s)>0 do
    delete(s,pos(#9,s),1);
   if length(s)=0 then continue;
   if s[1]='&' then
    begin
     kf:='&'+key=s;
     continue;
    end;
   if kf and (pos(name+'=',s)=1) then
    begin
     delete(s,1,length(name)+1);
     catchValue:=s;
     close(f);
     exit;
    end;
  end;
 close(f);
 catchValue:='-1';//'arrrgh, couldn''t find &'+key+' -> '+name+' in '+fnam+'!';
end;

function getIntensity(wen: string; Spalte: Integer; Ordnung,Otol: extended; fit: boolean): TExtPoint;
var F:         Textfile;
    S:         String;
    n:         float;
    I,J:       integer;
    Daten:     array of TExtPoint;
    y0,dt,err: extended;
    erg:       TExtPoint;
begin
 if not fileexists(datadir+'/Post/ft-'+wen) then writeln(datadir+'/Post/ft-'+wen);
 assign(F,datadir+'/Post/ft-'+wen);
 reset(F);
 n:=0;
 Setlength(Daten,0);
 while not eof(F) do
  begin
   readln(F,S);
   while (length(S)>0) and (S[1] in [#9,' ']) do
    delete(S,1,1);
   if (length(S)=0) or (S[1]='#') then continue;
   S:=S+' ';
   n:=strtofloat(copy(S,1,pos(' ',S)-1));
   for I:=1 to Spalte do
    begin
     delete(S,1,pos(' ',S));
     while (length(S)>0) and (S[1] in [#9,' ']) do
      delete(S,1,1);
    end;
   S:=copy(S,1,pos(' ',S)-1);
   if (abs(n-Ordnung)<=Otol/2) or (Otol<0) then
    begin
     Setlength(Daten,length(Daten)+1);
     Daten[length(Daten)-1].x:=n-Ordnung;
     Daten[length(Daten)-1].y:=strtofloat(S);
    end;
  end;
 close(F);
 err:=0.001;

 if length(Daten)=0 then writeln('keine Daten zum fitten!');

 if fit then
  begin
   y0:=0; // generate some usable values of y0 and dt to start with
   I:=0;
   while (Daten[I].x<0) and (I<length(Daten)) do
    begin
     y0:=Daten[I].y;
     inc(I);
    end;
   J:=I;
   while (I<length(Daten)-1) and (Daten[I].y > exp(-1)*y0) do
    inc(I);
   while (J>0) and (Daten[J].y > exp(-1)*y0) do
    dec(J);
   dt:=(Daten[I].x-Daten[J].x)/2;
   
   if y0=0 then writeln('y0 ist vorher null!');
   
   GaussFitDownhillSimplex(Daten,y0,dt,err);
   if y0=0 then writeln('y0 ist nachher null!');
   erg.val:=abs(y0*dt*sqrt(pi));
   erg.err:=abs(sqrt(err/(length(Daten)-1))/y0);
  end
   else
  begin
   erg.val:=0;
   erg.err:=0;
   J:=0;
   for I:=0 to length(Daten)-1 do
    begin
     erg.val:=erg.val+Daten[I].y;
     if (10*I<length(Daten)) or 
        ((length(Daten)-I-1)*10<length(Daten)) then
      begin
       erg.err:=erg.err+Daten[I].y;
       inc(J);
      end;
    end;
   erg.val:=erg.val*(Daten[1].x-Daten[0].x);
   if J>0 then erg.err:=erg.err/J*(Daten[length(Daten)-1].x-Daten[0].x);
   erg.err:=erg.err/erg.val;
  end;
 getIntensity:=erg;
end;

procedure fehler(errnum: integer);
var i: integer;
begin
 case errnum of
  0:
   begin
    writeln('number of given Params was '+inttostr(paramcount)+':');
    for i:=0 to paramcount do
     writeln(' '+inttostr(i)+': "'+paramstr(i)+'"');
    writeln('usage:');
    writeln(' '+paramstr(0)+' output input ($key $name)* \# (p|s)(t|r)(P?[0..9]*)');
    writeln('   -- efficiency of p/s-pol., transm./refl., n-th (plasma) harmonic over $key-$name''s');
    writeln(' '+paramstr(0)+' output ($key $name)* \# (p|s)(t|r)(P?[0..9]*)');
    writeln('   -- headlines for above');
   end;
  1:
   begin
    writeln('error: corrupt harmoneff.ini');
   end;
  else
   writeln('*** unknown error: error '+inttostr(errnum)+' ***');
 end{of Case};
 halt;
end;

function  readString(Key,Nam: string): String;
var f:        textfile;
    s:        string;
    rightKey: boolean;
begin
 assign(f,extractfilepath(Paramstr(0))+'harmoneff.ini');
 reset(f);
 rightKey:=false;
 while not eof(f) do
  begin
   readln(f,s);
   if pos('#',s)>0 then delete(s,pos('#',s),length(s));
   while (length(s)>0) and (s[1] in [' ',#9]) do
    delete(s,1,1);
   while (length(s)>0) and (s[length(s)] in [' ',#9]) do
    delete(s,length(s),1);
   if length(s)=0 then continue;
   if s[1]='&' then
    begin
     delete(s,1,1);
     rightKey:=copy(s,1,length(Key))=Key;
     continue;
    end;
   if not rightKey then continue;
   if copy(s,1,length(Nam))=Nam then
    begin
     delete(s,1,length(Nam));
     delete(s,1,pos('=',s));
     while (length(s)>0) and (s[1] in [' ',#9]) do
      delete(s,1,1);
     readString:=s;
     close(f);
     exit;
    end;
  end;
 close(f);
 writeln('&'+Key+' '+Nam+' not found!');
 fehler(1);
end;

function  readFloat(Key,Nam: string): extended;
begin
 readFloat:=strtofloat(readString(Key,Nam));
end;

function  readInt(Key,Nam: string): integer;
begin
 readInt:=strtoint(readString(Key,Nam));
end;

function  readBool(Key,Nam: string): boolean;
begin
 readBool:=readInt(Key,Nam)<>0;
end;

var inputintens,
    outputintens,
    zuPapier:     TExtPoint;
    i,j,numl:     integer;
    todos:        array of TTodo;
    lookats:      array of TLookAt;
    heading:      boolean;
    S:            string;
    nion,max_err,
    lfbreite,
    pfbreite:     extended;
    FOut:         Textfile;
    fitten,
    mit_fehler,
    plusminus:	  boolean;
    spalte:       array[boolean] of Integer; //  [transm?]
    input_pol:    boolean; // p-pol?
const
    wer:    array[boolean,boolean] of String  = (('gp','fm'),('gm','fp'));  //  [transm?,p-pol?]

procedure readIni;
begin
 fitten:=readBool('integration','fitten');
 mit_fehler:=readBool('output','mit_fehler');
 plusminus:=readBool('output','plusminus');
 max_err:=readFloat('output','max_err');
 spalte[false]:=readInt('daten','l_pos');
 spalte[true]:=readInt('daten','l_pos');
 input_pol:=readBool('daten','input_pol');
 lfbreite:=readFloat('integration','lfbreite');
 pfbreite:=readFloat('integration','pfbreite');
end;

begin
 if paramcount <= 1 then
  fehler(0);
 readIni;
 Assign(FOut,Paramstr(1));
 if fileexists(Paramstr(1)) then
   Append(FOut)
  else
   Rewrite(FOut);
 heading:=not fileexists(Paramstr(2));
 setlength(lookats,0);
 numl:=-1;
 For i:=2+byte(not heading) to Paramcount do
  if (Paramstr(i)='#') or (Paramstr(i)='\#') then 
   begin
    if numl<>-1 then fehler(0);
    numl:=i-1;
   end;
 if (numl=-1) or (odd(numl) xor heading) then fehler(0);
 for i:=1 to (numl-1-Byte(not heading)) div 2 do
  begin
   setlength(lookats,length(lookats)+1);
   lookats[length(lookats)-1].key:=Paramstr(Byte(not heading)+2*i);
   lookats[length(lookats)-1].name:=Paramstr(Byte(not heading)+2*i+1);
  end;
 inc(numl,2);
 
 setlength(todos,0);
 For i:=numl to Paramcount do
  begin
   S:=paramstr(i);
   if (length(S)<3) or
      not (upcase(S[1]) in ['P','S']) or
      not (upcase(S[2]) in ['T','R']) or
      not (upcase(S[3]) in ['P','0'..'9']) then fehler(0);
   for j:=4 to length(S) do
    if not (S[j] in ['0'..'9']) then fehler(0);
   setlength(todos,length(todos)+1);
   todos[length(todos)-1].p_pol:=upcase(S[1]) = 'P';
   todos[length(todos)-1].transmitted:=upcase(S[2]) = 'T';
   todos[length(todos)-1].plasma:=upcase(S[3]) = 'P';
   delete(S,1,2+Byte(todos[length(todos)-1].plasma));
   todos[length(todos)-1].order:=strtoint(S);
  end;
 
 if heading then
  begin
   write(FOut,'source');
   for i:=0 to length(lookats)-1 do
    write(FOut,#9+lookats[i].key+'-'+lookats[i].name);
   for i:=0 to length(todos)-1 do
    begin
     write(FOut,#9);
     if todos[i].p_pol then S:='p'
                       else S:='s';
     S:=S+'-pol ';
     if todos[i].transmitted then S:=S+'transm.'
                             else S:=S+'refl.';
     S:=S+' '+inttostr(todos[i].order);
     if todos[i].plasma then S:=S+' (pl.)';
     write(FOut,S);
     if mit_fehler then write(FOut,#9'd_abs'#9'd_rel');
     if plusminus then write(FOut,#9'min'+S+#9'max'+S);
    end;
   writeln(FOut);
  end
   else
  begin
   datadir:=catchValue(Paramstr(2),'output','path');
   if not directoryexists(datadir) then
    begin
     delete(datadir,1,pos('/',datadir));
     if directoryexists('../daten_alt/'+datadir) then
      datadir:='../daten_alt/'+datadir
       else
      begin
       if directoryexists('../daten_mittelalt/'+datadir) then
        datadir:='../daten_mittelalt/'+datadir
         else
        begin
         writeln('**************************************************************************');
         writeln(datadir);
         halt;
        end;
      end;
    end;
   inputintens:=getIntensity(wer[true,input_pol],1,1,-1,fitten); //'fp'
   write(FOut,datadir);
   for i:=0 to length(lookats)-1 do
    write(FOut,#9+myfloattostr(strtofloat(catchValue(Paramstr(2),lookats[i].key,lookats[i].name))));
   for i:=0 to length(todos)-1 do
    begin
     if todos[i].plasma then
      begin
       nion:=strtofloat(catchValue(Paramstr(2),'box','n_ion_over_nc'));
       outputintens:=getIntensity(wer[todos[i].transmitted,todos[i].p_pol],
                                  Spalte[todos[i].transmitted],
                                  todos[i].order*sqrt(nion),
                                  pfbreite/2*sqrt(nion),fitten);
      end
       else
      begin
       outputintens:=getIntensity(wer[todos[i].transmitted,todos[i].p_pol],
                                  Spalte[todos[i].transmitted],
                                  todos[i].order,
                                  lfbreite,fitten);
      end;
     zuPapier:=divide(outputintens,inputintens);
     if (zuPapier.Err <= max_err) or (max_err < 0) then
      begin 
       write(FOut,#9+valuetostr(zuPapier,mit_fehler));
       if plusminus then
        write(FOut,#9+myfloattostr(zuPapier.Val*(1-zuPapier.Err))+
                   #9+myfloattostr(zuPapier.Val*(1+zuPapier.Err)));
      end
       else
      begin 
       for J:=0 to 2*Byte(mit_fehler) + 2*Byte(plusminus) do
        write(FOut,#9);
      end;
    end;
   writeln(FOut);
  end;

 Close(FOut);

end.