Bsoft  2.1.0
Bernard's software package
scatter.cpp
Go to the documentation of this file.
1 
9 #include "scatter.h"
10 
11 // Declaration of global variables
12 extern int verbose; // Level of output to the screen
13 
14 /*
15  The intensity is composed of the direct and scattering components.
16  The average for the scattering component (mu) is modified
17  with aperture-based fraction k for the elastic part
18  and the use of an energy filter for the inelastic part.
19 */
20 double signal_integrated(double emu, double imu,
21  double ke, double ki, int ef)
22 {
23  double sig(1), f(1), mu(ke*emu), pmu(1);
24  if ( !ef ) mu += ki*imu;
25 
26  for ( long n=1; n<=10; ++n ) {
27  f *= n; // Factorial
28  pmu *= mu; // Powers of average
29  sig += pmu/f;
30 // cout << n << tab << f << tab << pmu << tab << sig << endl;
31  }
32 
33  sig *= exp(-emu-imu);
34 
35  return sig;
36 }
37 
38 double signal_integrated_new(double emu, double imu, double k, int ef)
39 {
40  double sig(0), f(1), pmu(1);
41 
42  // Elastic scattering
43  for ( long n=1; n<=10; ++n ) {
44  f *= n; // Factorial
45  pmu *= emu; // Powers of average
46  sig += pmu/f;
47  }
48 
49  if ( ef )
50  sig = (1 + k*sig)*exp(-emu-imu);
51  else
52  sig = 1 - (1 - k)*sig*exp(-emu-imu);
53 
54  return sig;
55 }
56 
57 
66  double ds, double scut)
67 {
68  if ( ds < 1e-10 ) ds = 0.01;
69 
70  long i, j, imax(scut/ds + 0.5);
71  double s2, ds2(ds*ds/4);
72 
73  // Calculate the atomic scattering profiles
74  if ( verbose & VERB_DEBUG )
75  cout << "DEBUG calculate_scattering_curve: z = " << at->z << endl;
76 
77  vector<double> scat;
78 
79  for ( i=0; i<imax; i++ ) {
80  s2 = i*i*ds2;
81  scat.push_back(at->sfc);
82  for ( j=0; j<5; j++ )
83  scat[i] += at->sfa[j]*exp(-at->sfb[j]*s2);
84  }
85 
86  return scat;
87 }
88 
97 map<string, vector<double>> calculate_scattering_curves(JSvalue& el,
98  Batomtype* atompar, double ds, double scut)
99 {
100  Batomtype* at;
101 
102  map<string, vector<double>> scat;
103 
104  for ( at = atompar; at; at = at->next ) {
105  if ( el.exists(at->el) ) {
106  if ( verbose & VERB_PROCESS )
107  cout << "Calculating scattering curve for " << at->el << endl;
108  vector<double> scat1 = calculate_scattering_curve(at, ds, scut);
109  scat[at->el] = scat1;
110  }
111  }
112 
113  return scat;
114 }
115 
122 {
123  long i, j;
124  double cs(0);
125 
126  for ( i=0; i<5; ++i )
127  for ( j=0; j<5; ++j )
128  cs += at->sfa[i]*at->sfa[j]/(at->sfb[i] + at->sfb[j]);
129 
130 // cout << at->el << tab << cs << endl;
131 
132  return cs;
133 }
134 
141 double elastic_cross_section(Batomtype* at, double volt)
142 {
143  double b2 = beta2(volt);
144  double hf = 1e10*PLANCK/(EMASS*LIGHTSPEED);
145  double cs = scatter_curve_integral(at);
146 
147  cs *= 4*M_PI*hf*hf/b2;
148 
149  return cs;
150 }
151 
159 {
160  if ( !at ) return 0;
161 
162  long i(0);
163  double ds(0.01), f, cs(0);
164  double b2 = beta2(ctf.volt());
165  double hf = 1e10*PLANCK/(EMASS*LIGHTSPEED);
166  double scut(ctf.frequency_cutoff());
167 
168  vector<double> scat = calculate_scattering_curve(at, ds, scut);
169 
170  for ( auto it = scat.begin(); it != scat.end(); ++it, ++i ) {
171  f = *it;
172  cs += f*f*i;
173  }
174 
175  cs *= 2*M_PI*hf*hf*ds*ds/b2;
176 
177  return cs;
178 }
179 
187 double elastic_cross_section(JSvalue& el, Batomtype* atompar, double volt)
188 {
189  double cs(0);
190  Batomtype* at;
191 
192  for ( at = atompar; at; at = at->next )
193  if ( el.exists(at->el) )
194  cs += el[at->el].real()*elastic_cross_section(at, volt);
195 
196  return cs;
197 }
198 
207 {
208  double cs(0);
209  Batomtype* at;
210 
211  for ( at = atompar; at; at = at->next )
212  if ( el.exists(at->el) )
213  cs += el[at->el].real()*elastic_cross_section_integrated(at, ctf);
214 
215  return cs;
216 }
217 
224 double inelastic_cross_section_langmore(long Z, double volt)
225 {
226  double b2 = beta2(volt);
227 
228  double cs = 1.5e-4*
229  (sqrt(Z)/b2)*log(2*b2*
231 
232  if ( Z == 1 ) cs *= 0.154; // Correction for hydrogen
233 
234  return cs;
235 }
236 
244 double inelastic_cross_section_langmore(JSvalue& el, Batomtype* atompar, double volt)
245 {
246  double cs(0);
247  Batomtype* at;
248 
249  for ( at = atompar; at; at = at->next )
250  if ( el.exists(at->el) )
251  cs += el[at->el].real()*
253 
254  return cs;
255 }
256 
265 double cross_section(JSvalue& el, Batomtype* atompar, double volt)
266 {
267  double ecs = elastic_cross_section(el, atompar, volt);
268  double ics = inelastic_cross_section_langmore(el, atompar, volt);
269 
270  if ( verbose ) {
271  cout << "Elastic cross section: " << ecs << " A2" << endl;
272  cout << "Inelastic cross section: " << ics << " A2" << endl;
273  }
274 
275  return ecs + ics;
276 }
277 
289 {
290  double ecs = elastic_cross_section_integrated(el, atompar, ctf);
291  double ics = inelastic_cross_section_langmore(el, atompar, ctf.volt());
292 
293  if ( verbose ) {
294  cout << "Elastic cross section: " << ecs << " A2" << endl;
295  cout << "Inelastic cross section: " << ics << " A2" << endl;
296  }
297 
298  if ( ctf.slit_width() > 1 ) return ecs;
299  else return ecs + ics;
300 }
301 
308 {
309  long i(0);
310  double ds(0.001), f, csi(0);
311  double cs = scatter_curve_integral(at);
312  vector<double> scat = calculate_scattering_curve(at, ds, 10);
313 
314  for ( auto it = scat.begin(); it != scat.end() && csi < 0.5*cs; ++it, ++i ) {
315  f = *it * ds;
316  csi += 0.5*f*f*i;
317  }
318 
319  return ds*i;
320 }
321 
329 {
330  double c, n(0), k(0);
331 
332  for ( ; at; at = at->next )
333  if ( el.exists(at->el) ) {
334  c = el[at->el].real();
336  n += c;
337  }
338 
339  if ( n ) k /= n;
340  else k = 1;
341 
342  return k;
343 }
344 
349 double ice_density()
350 {
351  double dens(0.93); // Density in g/cm3
352  double mdens(dens*AVOGADRO/1.0e24); // Density in Da/A3
353  double wdens(mdens/18); // Density in waters/A3
354 
355  if ( verbose ) {
356  cout << "Ice:" << endl;
357  cout << "Density: " << dens << " g/cm3" << endl;
358  cout << "Density: " << mdens << " Da/A3" << endl;
359  cout << "Density: " << wdens << " molecules/A3" << endl;
360  }
361 
362  return wdens;
363 }
364 
371 double ice_cross_section(Batomtype* atompar, double volt)
372 {
373  JSvalue el(JSobject);
374  el["H"] = 2.0;
375  el["O"] = 1.0;
376 
377  double cs = cross_section(el, atompar, volt);
378 
379  return cs;
380 }
381 
391 double intensity(JSvalue& el, double d, double thickness, Batomtype* atompar, CTFparam& ctf)
392 {
393  if ( thickness < 1 ) thickness = 1000;
394 
395  int ef(ctf.slit_width()>0); // Energy filter
396  double sm = cross_section_half_maximal_frequency(el, atompar);
397  double sc = ctf.frequency_cutoff();
398  double ke = sc*sc/(sc*sc+sm*sm);
399 // double ki = sc/(sc+0.054);
400  double ki = sc/(sc+0.2);
401 // double ki = sc*sc/(sc*sc+0.45*0.45);
402 
403  double ecs = elastic_cross_section(el, atompar, ctf.volt());
404  double ics = inelastic_cross_section_langmore(el, atompar, ctf.volt());
405 
406  double emu = ecs*d*thickness;
407  double imu = ics*d*thickness;
408  double mfp = 1/(d*(ecs+ics));
409  double i = signal_integrated(emu, imu, ke, ki, ef);
410  double lam = -thickness/log(i);
411 
412  if ( verbose ) {
413  cout << "Mean free path: " << mfp << " A" << endl;
414  cout << "Half_maximal frequency: " << sm << " 1/Å" << endl;
415  cout << "Frequency cutoff: " << sc << " 1/Å" << endl;
416  cout << "Aperture effect: " << ke << tab << ki << endl;
417  cout << "Thickness: " << thickness << " A" << endl;
418  cout << "Intensity: " << i << endl;
419  cout << "Lambda: " << lam << endl;
420  cout << endl;
421  }
422 
423  return i;
424 }
425 
433 double ice_intensity(double thickness, Batomtype* atompar, CTFparam& ctf)
434 {
435  if ( thickness < 1 ) thickness = 1000;
436 
437  JSvalue el(JSobject);
438  el["H"] = 2.0;
439  el["O"] = 1.0;
440 
441  double d = ice_density();
442 
443  return intensity(el, d, thickness, atompar, ctf);
444 }
445 
446 
455 vector<double> ice_intensity(double thick_step, double thick_max, Batomtype* atompar, CTFparam& ctf)
456 {
457  if ( thick_step < 1 ) thick_step = 100;
458  if ( thick_max < 1 ) thick_max = 5000;
459 
460  JSvalue el(JSobject);
461  el["H"] = 2.0;
462  el["O"] = 1.0;
463 
464  int ef(ctf.slit_width()>0); // Energy filter
465  double d = ice_density();
466  double sm = cross_section_half_maximal_frequency(el, atompar);
467  double sc = ctf.frequency_cutoff();
468  double ke = sc*sc/(sc*sc+sm*sm);
469  double ki = sc/(sc+0.054);
470 
471  double ecs = elastic_cross_section(el, atompar, ctf.volt());
472 
473  double ics = inelastic_cross_section_langmore(el, atompar, ctf.volt());
474 
475  double t, emu, imu, i, mfp;
476  vector<double> I;
477 
478 // cout << "Aperture effect: " << k << endl;
479 
480  if ( verbose & VERB_FULL )
481  cout << "t(A)\tI\tlambda" << endl;
482  for ( t=thick_step; t<=thick_max; t+=thick_step ) {
483  emu = ecs*d*t;
484  imu = ics*d*t;
485  i = signal_integrated(emu, imu, ke, ki, ef);
486  mfp = -t/log(i);
487  I.push_back(i);
488  if ( verbose & VERB_FULL )
489  cout << t << tab << i << tab << mfp << endl;
490  }
491 
492  return I;
493 }
494 
501 double ice_lambda(Batomtype* atompar, CTFparam& ctf)
502 {
503  double thick_step(100), thick_max(5000), t(thick_step), lam_avg(0);
504 
505  vector<double> I = ice_intensity(thick_step, thick_max, atompar, ctf);
506 
507  for ( long i=0; i<I.size(); ++i, t+=thick_step )
508  lam_avg += -t/log(I[i]);
509 
510  lam_avg /= I.size();
511 
512  return lam_avg;
513 }
514 
ECHARGE
#define ECHARGE
Definition: utilities.h:52
VERB_DEBUG
#define VERB_DEBUG
Definition: utilities.h:116
Batomtype::el
char el[4]
Definition: rwatomprop.h:22
AVOGADRO
#define AVOGADRO
Definition: utilities.h:55
LIGHTSPEED
#define LIGHTSPEED
Definition: utilities.h:54
JSvalue::real
double & real()
Definition: json.h:149
Batomtype
Definition: rwatomprop.h:19
VERB_PROCESS
#define VERB_PROCESS
Definition: utilities.h:111
cross_section_integrated
double cross_section_integrated(JSvalue &el, Batomtype *atompar, CTFparam &ctf)
Calculates the combined total cross section for a defined composition.
Definition: scatter.cpp:288
cross_section
double cross_section(JSvalue &el, Batomtype *atompar, double volt)
Calculates the combined total cross section for a defined composition.
Definition: scatter.cpp:265
elastic_cross_section
double elastic_cross_section(Batomtype *at, double volt)
Calculates the full elastic cross section (no aperture).
Definition: scatter.cpp:141
Batomtype::sfa
float sfa[5]
Definition: rwatomprop.h:29
ice_lambda
double ice_lambda(Batomtype *atompar, CTFparam &ctf)
Calculates the average lambda proportionality parameter for vitreous ice.
Definition: scatter.cpp:501
scatter.h
Functions for calculating electron scattering profiles.
PLANCK
#define PLANCK
Definition: utilities.h:51
JSvalue::exists
bool exists(string tag)
Definition: json.h:221
signal_integrated
double signal_integrated(double emu, double imu, double ke, double ki, int ef)
Definition: scatter.cpp:20
elastic_cross_section_integrated
double elastic_cross_section_integrated(Batomtype *at, CTFparam &ctf)
Integrates the elastic scattering curve up to the aperture cutoff.
Definition: scatter.cpp:158
JSvalue
Definition: json.h:53
ice_cross_section
double ice_cross_section(Batomtype *atompar, double volt)
Calculates the total cross section for ice up to the aperture cutoff.
Definition: scatter.cpp:371
CTFparam::volt
double volt()
Definition: ctf.h:87
cross_section_half_maximal_frequency
double cross_section_half_maximal_frequency(Batomtype *at)
Calculates the half-maximal frequency for an atom type.
Definition: scatter.cpp:307
verbose
int verbose
ice_density
double ice_density()
Returns vitreous ice density.
Definition: scatter.cpp:349
Batomtype::z
int z
Definition: rwatomprop.h:21
calculate_scattering_curves
map< string, vector< double > > calculate_scattering_curves(JSvalue &el, Batomtype *atompar, double ds, double scut)
Calculates elastic electron scattering profiles for a subset of atom types.
Definition: scatter.cpp:97
M_PI
#define M_PI
Definition: utilities.h:63
inelastic_cross_section_langmore
double inelastic_cross_section_langmore(long Z, double volt)
Calculates the inelastic cross section for an atom type.
Definition: scatter.cpp:224
CTFparam::slit_width
double slit_width()
Definition: ctf.h:104
tab
ostream & tab(ostream &out)
Definition: utilities.cpp:22
Batomtype::next
Batomtype * next
Definition: rwatomprop.h:20
intensity
double intensity(JSvalue &el, double d, double thickness, Batomtype *atompar, CTFparam &ctf)
Calculates the expected intensity given a thickness of vitreous ice.
Definition: scatter.cpp:391
Batomtype::sfb
float sfb[5]
Definition: rwatomprop.h:30
CTFparam
Definition: ctf.h:28
Batomtype::sfc
float sfc
Definition: rwatomprop.h:31
JSobject
@ JSobject
Definition: json.h:26
CTFparam::frequency_cutoff
double frequency_cutoff()
Definition: ctf.h:150
ice_intensity
double ice_intensity(double thickness, Batomtype *atompar, CTFparam &ctf)
Calculates the expected intensity given a thickness of vitreous ice.
Definition: scatter.cpp:433
VERB_FULL
#define VERB_FULL
Definition: utilities.h:113
signal_integrated_new
double signal_integrated_new(double emu, double imu, double k, int ef)
Definition: scatter.cpp:38
beta2
double beta2(double volt)
Calculates the square of the relative electron velocity.
Definition: ctf.cpp:479
calculate_scattering_curve
vector< double > calculate_scattering_curve(Batomtype *at, double ds, double scut)
Calculates an elastic electron scattering profile for an atom type.
Definition: scatter.cpp:65
scatter_curve_integral
double scatter_curve_integral(Batomtype *at)
Calculates the integral of the whole curve.
Definition: scatter.cpp:121
EMASS
#define EMASS
Definition: utilities.h:53