map.cpp 5.14 KB
Newer Older
1
2
3
4
5
#include <fstream>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <cstring>
6
7
#include <math.h>
#include <fftw3.h>
8
9

#include "map.h"
10
11
#include "param.h"

12
13
using namespace std;

14
int bioem_RefMap::readRefMaps(bioem_param& param)
15
{
16
17
	numPixels = param.param_device.NumberPixels;
	refMapSize = param.param_device.NumberPixels * param.param_device.NumberPixels;
David Rohr's avatar
David Rohr committed
18
19
20
	// **************************************************************************************
	// ***********************Reading reference Particle Maps************************
	// **************************************************************************************
21
22
	int allocsize = 0;
	if (param.loadMap)
23
24
25
26
27
28
29
30
	{
		FILE* fp = fopen("maps.dump", "rb");
		if (fp == NULL)
		{
			cout << "Error opening dump file\n";
			exit(1);
		}
		fread(&ntotRefMap, sizeof(ntotRefMap), 1, fp);
31
32
		maps = (myfloat_t*) mallocchk(ntotRefMap * refMapSize * sizeof(myfloat_t));
		fread(maps, sizeof(myfloat_t) * refMapSize, ntotRefMap, fp);
33
		fclose(fp);
34

35
		cout << "Particle Maps read from Map Dump \nTotal Number of particles: " << ntotRefMap ;
36
		cout << "\n+++++++++++++++++++++++++++++++++++++++++ \n";
37
38
39
	}
	else
	{
40
41
42
		int nummap = -1;
		int lasti = 0;
		int lastj = 0;
43
		ifstream input(param.filemap);
44
45
46
47
48
49
50
51
52
53
54
		if (!input.good())
		{
			cout << "Particle Maps Failed to open file" << endl ;
			exit(1);
		}

		char line[512] = {' '};
		char tmpLine[512] = {' '};

		while (!input.eof())
		{
55
			input.getline(line, 512);
56

57
58
			strncpy(tmpLine, line, strlen(line));
			char *token = strtok(tmpLine, " ");
59

60
			if (strcmp(token, "PARTICLE") == 0) // to count the number of maps
61
62
			{
				nummap++;
63
64
65
66
67
68
69
70
71
72
				if (allocsize == 0)
				{
					allocsize = 64;
					maps = (myfloat_t*) mallocchk(refMapSize * sizeof(myfloat_t) * allocsize);
				}
				else if (nummap + 1 >= allocsize)
				{
					allocsize *= 2;
					maps = (myfloat_t*) reallocchk(maps, refMapSize * sizeof(myfloat_t) * allocsize);
				}
73
74
75
76
				if (nummap % 128 == 0)
				{
					cout << "..." << nummap << "\n";
				}
77
				if(lasti != param.param_device.NumberPixels && lastj != param.param_device.NumberPixels && nummap > 0)
78
79
80
81
				{
					cout << "PROBLEM INCONSISTENT NUMBER OF PIXELS IN MAPS AND INPUTFILE ( " << param.param_device.NumberPixels << ", i " << lasti << ", j " << lastj << ")" << "\n";
					exit(1);
				}
82
83
84
			}
			else
			{
85
				int i, j;
86
				float z;
87

88
89
				char tmpVals[36]  = {' '};

90
91
				strncpy (tmpVals, line, 8);
				sscanf (tmpVals, "%d", &i);
92

93
94
				strncpy (tmpVals, line + 8, 8);
				sscanf (tmpVals, "%d", &j);
95

96
97
				strncpy (tmpVals, line + 16, 12);
				sscanf (tmpVals, "%f", &z);
98
				//checking for Map limits
99
				if(i > 0 && i - 1 < param.param_device.NumberPixels && j > 0 && j - 1 < param.param_device.NumberPixels)
100
				{
101
102
103
					maps[nummap * refMapSize + (i - 1) * numPixels + (j - 1)] = (myfloat_t) z;
					lasti = i;
					lastj = j;
104
105
106
107
108
109
110
111
112
				}
				else
				{
					cout << "PROBLEM READING MAP (Map number " << nummap << ", i " << i << ", j " << j << ")" << "\n";
					exit(1);
				}
			}
		}
		cout << ".";
113
		ntotRefMap = nummap + 1;
114
		maps = (myfloat_t*) reallocchk(maps, refMapSize * sizeof(myfloat_t) * ntotRefMap);
115
116
		cout << "Particle Maps read from Standard File \nTotal Number of particles: " << ntotRefMap ;
		cout << "\n+++++++++++++++++++++++++++++++++++++++++ \n";
117

118
		if (param.dumpMap)
119
120
121
122
123
124
125
126
		{
			FILE* fp = fopen("maps.dump", "w+b");
			if (fp == NULL)
			{
				cout << "Error opening dump file\n";
				exit(1);
			}
			fwrite(&ntotRefMap, sizeof(ntotRefMap), 1, fp);
127
			fwrite(maps, sizeof(myfloat_t) * refMapSize, ntotRefMap, fp);
128
129
130
131
			fclose(fp);
		}
	}

132
133
134
	sum_RefMap = (myfloat_t*) mallocchk(sizeof(myfloat_t) * ntotRefMap);
	sumsquare_RefMap = (myfloat_t*) mallocchk(sizeof(myfloat_t) * ntotRefMap);

135
136
137
138
139
	return(0);
}

int bioem_RefMap::PreCalculateMapsFFT(bioem_param& param)
{
David Rohr's avatar
David Rohr committed
140
141
142
	// **************************************************************************************
	// ********** Routine that pre-calculates Kernels for Convolution **********************
	// ************************************************************************************
143

144
145
	myfloat_t* localMap;

146
	localMap = (myfloat_t *) myfftw_malloc(sizeof(myfloat_t) * param.param_device.NumberPixels * param.param_device.NumberPixels);
147

148
	RefMapsFFT = new mycomplex_t[ntotRefMap * param.FFTMapSize];
149

150
	mycomplex_t* localout;
151
	localout = (mycomplex_t *) myfftw_malloc(sizeof(mycomplex_t) * param.param_device.NumberPixels * param.param_device.NumberFFTPixels1D);
152
153
154
155

	for (int iRefMap = 0; iRefMap < ntotRefMap ; iRefMap++)
	{
		//Assigning localMap values to padded Map
156
		for(int i = 0; i < param.param_device.NumberPixels; i++)
157
		{
158
			for(int j = 0; j < param.param_device.NumberPixels; j++)
159
			{
160
				localMap[i * param.param_device.NumberPixels + j] = maps[iRefMap * refMapSize + i * param.param_device.NumberPixels + j];
161
162
163
164
			}
		}

		//Calling FFT_Forward
165
		myfftw_execute_dft_r2c(param.fft_plan_r2c_forward, localMap, localout);
166
167

		// Normalizing and saving the Reference CTFs
168
		mycomplex_t* RefMap = &RefMapsFFT[iRefMap * param.FFTMapSize];
169
		for(int i = 0; i < param.param_device.NumberPixels * param.param_device.NumberFFTPixels1D ; i++ )
170
		{
171
172
			RefMap[i][0] = localout[i][0];
			RefMap[i][1] = localout[i][1];
173
174
175
176
177
178
179
		}
	}

	myfftw_free(localMap);
	myfftw_free(localout);

	return(0);
180
}