param.cpp 10.6 KB
Newer Older
qon's avatar
qon committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <cstring>
#include <math.h>
#include <fftw3.h>

#include "param.h"
#include "map.h"

using namespace std;

bioem_param::bioem_param()
{
16
17
18
19
20
21
22
23
24
25
26
27
28
29
	//Number of Pixels
	param_device.NumberPixels=0;
	// Euler angle grid spacing
	angleGridPointsAlpha = 0;
	angleGridPointsBeta = 0;
	//Envelop function paramters
	numberGridPointsEnvelop = 0;
	//Contrast transfer function paramters
	numberGridPointsCTF_amp = 0;
	numberGridPointsCTF_phase = 0;

	/****center displacement paramters Equal in both directions***/
	param_device.maxDisplaceCenter = 0;
	numberGridPointsDisplaceCenter = 0;
30
31

	fft_plans_created = 0;
qon's avatar
qon committed
32
33
34
35
}

int bioem_param::readParameters()
{
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
	/**************************************************************************************/
	/***************************** Reading Input Parameters ******************************/
	/**************************************************************************************/

	ifstream input(fileinput);
	if (!input.good())
	{
		cout << "Failed to open file: " << fileinput << "\n";
		exit(0);
	}

	char line[512] = {0};
	char saveline[512];

	cout << "\n +++++++++++++++++++++++++++++++++++++++++ \n";
	cout << "\n		   READING PARAMETERS  \n\n";

	cout << " +++++++++++++++++++++++++++++++++++++++++ \n";
	while (!input.eof())
	{
		input.getline(line,512);
		strcpy(saveline,line);
		char *token = strtok(line," ");

		if (token==NULL || line[0] == '#' || strlen(token)==0)
		{
			// comment or blank line
		}
		else if (strcmp(token,"PIXEL_SIZE")==0)
		{
			token = strtok(NULL," ");
			pixelSize=atof(token);
			cout << "Pixel Sixe " << pixelSize << "\n";

		}
		else if (strcmp(token,"NUMBER_PIXELS")==0)
		{
			token = strtok(NULL," ");
			param_device.NumberPixels=int(atoi(token));
			cout << "Number of Pixels " << param_device.NumberPixels << "\n";

		}
		else if (strcmp(token,"GRIDPOINTS_ALPHA")==0)
		{
			token = strtok(NULL," ");
			angleGridPointsAlpha=int(atoi(token));
			cout << "Grid points alpha " << angleGridPointsAlpha << "\n";

		}
		else if (strcmp(token,"GRIDPOINTS_BETA")==0)
		{
			token = strtok(NULL," ");
			angleGridPointsBeta=int(atoi(token));
			cout << "Grid points beta " << angleGridPointsBeta << "\n";

		}

		else if (strcmp(token,"GRIDPOINTS_ENVELOPE")==0)
		{
			token = strtok(NULL," ");
			numberGridPointsEnvelop=int(atoi(token));
			cout << "Grid points envelope " << numberGridPointsEnvelop << "\n";

		}
		else if (strcmp(token,"START_ENVELOPE")==0)
		{
			token = strtok(NULL," ");
			startGridEnvelop=atof(token);
			cout << "Start Envelope " << startGridEnvelop << "\n";

		}
		else if (strcmp(token,"GRIDSPACE_ENVELOPE")==0)
		{
			token = strtok(NULL," ");
			gridEnvelop=atof(token);
			cout << "Grid spacing Envelope " << gridEnvelop << "\n";

		}
		else if (strcmp(token,"GRIDPOINTS_CTF_PHASE")==0)
		{
			token = strtok(NULL," ");
			numberGridPointsCTF_phase=int(atoi(token));
			cout << "Grid points CTF " << numberGridPointsCTF_phase << "\n";

		}
		else if (strcmp(token,"START_CTF_PHASE")==0)
		{
			token = strtok(NULL," ");
			startGridCTF_phase=atof(token);
			cout << "Start CTF " << startGridCTF_phase << "\n";

		}
		else if (strcmp(token,"GRIDSPACE_CTF_PHASE")==0)
		{
			token = strtok(NULL," ");
			gridCTF_phase=atof(token);
			cout << "Grid Space CTF " << gridCTF_phase << "\n";

		} else if (strcmp(token,"GRIDPOINTS_CTF_AMP")==0)
		{
			token = strtok(NULL," ");
			numberGridPointsCTF_amp=int(atoi(token));
			cout << "Grid points CTF " << numberGridPointsCTF_amp << "\n";

		}
		else if (strcmp(token,"START_CTF_AMP")==0)
		{
			token = strtok(NULL," ");
			startGridCTF_amp=atof(token);
			cout << "Start CTF " << startGridCTF_amp << "\n";

		}
		else if (strcmp(token,"GRIDSPACE_CTF_AMP")==0)
		{
			token = strtok(NULL," ");
			gridCTF_amp=atof(token);
			cout << "Grid Space CTF " << gridCTF_amp << "\n";

		}
		else if (strcmp(token,"MAX_D_CENTER")==0)
		{
			token = strtok(NULL," ");
			param_device.maxDisplaceCenter=int(atoi(token));
			cout << "Maximum displacement Center " <<  param_device.maxDisplaceCenter << "\n";

		}
		else if (strcmp(token,"PIXEL_GRID_CENTER")==0)
		{
			token = strtok(NULL," ");
			param_device.GridSpaceCenter=int(atoi(token));
			cout << "Grid space displacement center " <<   param_device.GridSpaceCenter << "\n";

		}
		else if (strcmp(token,"WRITE_PROB_ANGLES")==0)
		{
			writeAngles=true;
			cout << "Writing Probabilies of each angle \n";

		}

	}
	input.close();
	cout << " +++++++++++++++++++++++++++++++++++++++++ \n";
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199

	cout << "Preparing FFTs\n";
	releaseFFTPlans();
	mycomplex_t *tmp_map, *tmp_map2;
	tmp_map = (mycomplex_t *) myfftw_malloc(sizeof(mycomplex_t) * param_device.NumberPixels * param_device.NumberPixels);
	tmp_map2 = (mycomplex_t *) myfftw_malloc(sizeof(mycomplex_t) * param_device.NumberPixels * param_device.NumberPixels);

	fft_plan_c2c_forward = myfftw_plan_dft_2d(param_device.NumberPixels, param_device.NumberPixels, tmp_map, tmp_map2, FFTW_FORWARD, FFTW_MEASURE);
	fft_plan_c2c_backward = myfftw_plan_dft_2d(param_device.NumberPixels, param_device.NumberPixels, tmp_map, tmp_map2, FFTW_BACKWARD, FFTW_MEASURE);

	if (fft_plan_c2c_forward == 0 || fft_plan_c2c_backward == 0)
	{
		cout << "Error planing FFTs\n";
		exit(1);
	}

	myfftw_free(tmp_map);
	myfftw_free(tmp_map2);
	fft_plans_created = 1;
	cout << " +++++++++++++++++++++++++++++++++++++++++ \n";

200
	return(0);
qon's avatar
qon committed
201
202
}

203
204
205
206
207
208
209
210
211
212
void bioem_param::releaseFFTPlans()
{
	if (fft_plans_created)
	{
		myfftw_destroy_plan(fft_plan_c2c_forward);
		myfftw_destroy_plan(fft_plan_c2c_backward);
	}
	fft_plans_created = 0;
}

qon's avatar
qon committed
213
214
int bioem_param::CalculateGridsParam() //TO DO FOR QUATERNIONS
{
215
216
217
218
219
220
221
	/**************************************************************************************/
	/**************** Routine that pre-calculates Euler angle grids **********************/
	/************************************************************************************/
	myfloat_t grid_alpha,cos_grid_beta;
	int n=0;

	//alpha and gamma are uniform in -PI,PI
222
	grid_alpha=2.f * M_PI / (myfloat_t) angleGridPointsAlpha;
223
224

	//cosine beta is uniform in -1,1
225
	cos_grid_beta=2.f / (myfloat_t) angleGridPointsBeta;
226
227
228
229
230
231
232
233

	// Euler Angle Array
	for (int ialpha = 0; ialpha < angleGridPointsAlpha; ialpha ++)
	{
		for (int ibeta = 0; ibeta < angleGridPointsBeta; ibeta ++)
		{
			for (int igamma = 0; igamma < angleGridPointsAlpha; igamma ++)
			{
234
235
236
				angles[n].pos[0]= (myfloat_t) ialpha * grid_alpha - M_PI + grid_alpha * 0.5f; //ALPHA centered in the middle
				angles[n].pos[1]= acos((myfloat_t) ibeta * cos_grid_beta - 1 + cos_grid_beta * 0.5f); //BETA centered in the middle
				angles[n].pos[2]= (myfloat_t) igamma * grid_alpha - M_PI + grid_alpha * 0.5f; //GAMMA centered in the middle
237
238
239
240
241
242
243
244
				n++;
			}
		}
	}
	nTotGridAngles=n;

	/********** Calculating normalized volumen element *********/

245
246
247
	param_device.volu = grid_alpha * grid_alpha * cos_grid_beta * (myfloat_t) param_device.GridSpaceCenter * pixelSize * (myfloat_t) param_device.GridSpaceCenter * pixelSize
					  * gridCTF_phase * gridCTF_amp * gridEnvelop / (2.f * M_PI) / (2.f * M_PI) / 2.f / (2.f * (myfloat_t) param_device.maxDisplaceCenter) / (2.f * (myfloat_t) param_device.maxDisplaceCenter) / ((myfloat_t) numberGridPointsCTF_amp * gridCTF_amp + startGridCTF_amp)
					  / ((myfloat_t) numberGridPointsCTF_phase * gridCTF_phase + startGridCTF_phase) / ((myfloat_t) numberGridPointsEnvelop * gridEnvelop + startGridEnvelop);
248
249
250

	/*** Number of total pixels***/

251
252
	param_device.Ntotpi= (myfloat_t) (param_device.NumberPixels * param_device.NumberPixels);
	param_device.NtotDist=(2 * (int) (param_device.maxDisplaceCenter/param_device.GridSpaceCenter) + 1 ) * (2 * (int) (param_device.maxDisplaceCenter/param_device.GridSpaceCenter) + 1);
253
254

	return(0);
qon's avatar
qon committed
255
256
257
258
259
260

}


int bioem_param::CalculateRefCTF()
{
261
262
263
264
265
266
	/**************************************************************************************/
	/********** Routine that pre-calculates Kernels for Convolution **********************/
	/************************************************************************************/

	myfloat_t amp,env,phase,ctf,radsq;
	mycomplex_t* localCTF;
267
	int nctfmax= param_device.NumberPixels / 2;
268
269
270
271
272
273
274
275
	int n=0;

	localCTF= (mycomplex_t *) myfftw_malloc(sizeof(mycomplex_t) * param_device.NumberPixels*param_device.NumberPixels);
	refCTF = new bioem_map_forFFT[MAX_REF_CTF];


	for (int iamp = 0; iamp <  numberGridPointsCTF_amp ; iamp++) //Loop over amplitud
	{
276
		amp = (myfloat_t) iamp * gridCTF_amp + startGridCTF_amp;
277
278
279

		for (int iphase = 0; iphase <  numberGridPointsCTF_phase ; iphase++)//Loop over phase
		{
280
			phase = (myfloat_t) iphase * gridCTF_phase + startGridCTF_phase;
281

282
			for (int ienv = 0; ienv <  numberGridPointsEnvelop ; ienv++)//Loop over envelope
283
			{
284
				env= (myfloat_t) ienv * gridEnvelop + startGridEnvelop;
285
286
287
288
289
290
291
292

				memset(localCTF,0,param_device.NumberPixels*param_device.NumberPixels*sizeof(mycomplex_t));

				//Assigning CTF values
				for(int i=0; i< nctfmax; i++)
				{
					for(int j=0; j< nctfmax; j++)
					{
293
						radsq=(myfloat_t) (i*i+j*j) * pixelSize * pixelSize;
294
295
296
297
298
299
300
301
302
303
304
305
						ctf=exp(-radsq*env/2.0)*(amp*cos(radsq*phase/2.0)-sqrt((1-amp*amp))*sin(radsq*phase/2.0));

						localCTF[i*param_device.NumberPixels+j][0]=(myfloat_t) ctf;
						localCTF[i*param_device.NumberPixels+param_device.NumberPixels-j-1][0]=(myfloat_t) ctf;
						localCTF[(param_device.NumberPixels-i-1)*param_device.NumberPixels+j][0]=(myfloat_t) ctf;
						localCTF[(param_device.NumberPixels-i-1)*param_device.NumberPixels+param_device.NumberPixels-j-1][0]=(myfloat_t) ctf;
					}
				}
				//Creatting FFT_Forward of Kernel to store
				mycomplex_t* localout;
				localout= (mycomplex_t *) myfftw_malloc(sizeof(mycomplex_t) *param_device.NumberPixels*param_device.NumberPixels);
				//Calling FFT_Forward
306
				myfftw_execute_dft(fft_plan_c2c_forward,localCTF,localout);
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

				// Normalizing and saving the Reference CTFs
				for(int i=0; i < param_device.NumberPixels ; i++ )
				{
					for(int j=0; j < param_device.NumberPixels ; j++ )
					{
						refCTF[n].cpoints[i*param_device.NumberPixels+j][0]=localout[i*param_device.NumberPixels+j][0];
						refCTF[n].cpoints[i*param_device.NumberPixels+j][1]=localout[i*param_device.NumberPixels+j][1];
					}
				}
				CtfParam[n].pos[0]=amp;
				CtfParam[n].pos[1]=phase;
				CtfParam[n].pos[2]=env;
				n++;
				myfftw_free(localout);
				if(n>MAX_REF_CTF)
				{   cout << n << "PROBLEM WITH CTF KERNEL PARAMETERS AND MAX NUMBER ALLOWED\n";
					exit(1);
				}
			}
		}
	}

	myfftw_free(localCTF);
	nTotCTFs=n;

	return(0);
qon's avatar
qon committed
334
335
336
337
338
}


bioem_param::~bioem_param()
{
339
	releaseFFTPlans();
340
341
342
343
344
345
346
347
	param_device.NumberPixels=0;
	angleGridPointsAlpha = 0;
	angleGridPointsBeta = 0;
	numberGridPointsEnvelop = 0;
	numberGridPointsCTF_amp = 0;
	numberGridPointsCTF_phase = 0;
	param_device.maxDisplaceCenter = 0;
	numberGridPointsDisplaceCenter = 0;
qon's avatar
qon committed
348
}