File: model.cpp

package info (click to toggle)
aoflagger 2.13.0-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 4,232 kB
  • sloc: cpp: 61,805; python: 60; sh: 23; makefile: 8
file content (242 lines) | stat: -rw-r--r-- 9,733 bytes parent folder | download | duplicates (2)
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
#include "model.h"

#include "../structures/image2d.h"
#include "../structures/timefrequencydata.h"

#include "observatorium.h"
#include "../util/rng.h"
#include "../util/logger.h"

#include <iostream>

Model::Model() : _noiseSigma(1.0), _sourceSigma(0.0), _integrationTime(15.0)
{
}

template<typename T>
void Model::SimulateObservation(struct OutputReceiver<T> &receiver, Observatorium &observatorium, num_t delayDirectionDEC, num_t delayDirectionRA)
{
	const size_t channelCount = observatorium.BandInfo().channels.size();
	const double frequency = observatorium.BandInfo().channels[0].frequencyHz;

	for(size_t f=0;f<channelCount;++f)
	{
		double channelFrequency = frequency + observatorium.ChannelWidthHz() * f;
		receiver.SetY(f);
		for(size_t i=0;i<observatorium.AntennaCount();++i)
		{
			for(size_t j=i+1;j<observatorium.AntennaCount();++j)
			{
				const AntennaInfo
					&antenna1 = observatorium.GetAntenna(i),
					&antenna2 = observatorium.GetAntenna(j);
				double
					dx = antenna1.position.x - antenna2.position.x,
					dy = antenna1.position.y - antenna2.position.y,
					dz = antenna1.position.z - antenna2.position.z;

				SimulateCorrelation(receiver, delayDirectionDEC, delayDirectionRA, dx, dy, dz, channelFrequency, observatorium.ChannelWidthHz(), 12*60*60, _integrationTime);
			}
		}
	}
}

template void Model::SimulateObservation(struct OutputReceiver<UVImager> &receiver, Observatorium &observatorium, num_t delayDirectionDEC, num_t delayDirectionRA);
template void Model::SimulateObservation(struct OutputReceiver<TimeFrequencyData> &receiver, Observatorium &observatorium, num_t delayDirectionDEC, num_t delayDirectionRA);

std::pair<TimeFrequencyData, TimeFrequencyMetaDataPtr> Model::SimulateObservation(class Observatorium &observatorium, num_t delayDirectionDEC, num_t delayDirectionRA, size_t a1, size_t a2)
{
	const size_t channelCount = observatorium.BandInfo().channels.size();
	const double frequency = observatorium.BandInfo().channels[0].frequencyHz;
	
	OutputReceiver<TimeFrequencyData> tfOutputter;
	tfOutputter._real = Image2D::CreateZeroImagePtr((size_t) (12*60*60/_integrationTime), channelCount);
	tfOutputter._imaginary = Image2D::CreateZeroImagePtr((size_t) (12*60*60/_integrationTime), channelCount);
	
	TimeFrequencyMetaDataPtr metaData(new TimeFrequencyMetaData());
	metaData->SetAntenna1(observatorium.GetAntenna(a1));
	metaData->SetAntenna2(observatorium.GetAntenna(a2));
	metaData->SetBand(observatorium.BandInfo());
	double
		dx = metaData->Antenna1().position.x - metaData->Antenna2().position.x,
		dy = metaData->Antenna1().position.y - metaData->Antenna2().position.y,
		dz = metaData->Antenna1().position.z - metaData->Antenna2().position.z;

	for(size_t f=0;f<channelCount;++f)
	{
		double channelFrequency = frequency + observatorium.ChannelWidthHz() * f;
		tfOutputter.SetY(f);
		SimulateCorrelation(tfOutputter, delayDirectionDEC, delayDirectionRA, dx, dy, dz, channelFrequency, observatorium.ChannelWidthHz(), 12*60*60, _integrationTime);
	}

	std::vector<double> times;
	std::vector<UVW> uvws;
	num_t wavelength = 1.0L / frequency;
	for(num_t t=0.0;t<12*60*60;t+=_integrationTime)
	{
		times.push_back(t);
		num_t earthLattitudeApprox = t*M_PIn/(12.0*60.0*60.0);
		UVW uvw;
		GetUVPosition(uvw.u, uvw.v, earthLattitudeApprox, delayDirectionDEC, delayDirectionRA, dx, dy, dz, wavelength);
		uvw.u = uvw.u * (299792458.0L / frequency);
		uvw.v = uvw.v * (299792458.0L / frequency);
		uvw.w = GetWPosition(delayDirectionDEC, delayDirectionRA, frequency, earthLattitudeApprox, dx, dy) * (299792458.0L / frequency);
		uvws.push_back(uvw);
	}
	metaData->SetUVW(uvws);
	metaData->SetObservationTimes(times);
	FieldInfo field;
	field.fieldId = 0;
	field.delayDirectionDec = delayDirectionDEC;
	//field.delayDirectionDecNegCos = -cos(delayDirectionDEC);
	//field.delayDirectionDecNegSin = -sin(delayDirectionDEC);
	field.delayDirectionRA = delayDirectionRA;
	metaData->SetField(field);

	TimeFrequencyData tfData(Polarization::StokesI, tfOutputter._real, tfOutputter._imaginary);
	return std::pair<TimeFrequencyData, TimeFrequencyMetaDataPtr>(tfData, metaData);
}

template<typename T>
void Model::SimulateCorrelation(struct OutputReceiver<T> &receiver, num_t delayDirectionDEC, num_t delayDirectionRA, num_t dx, num_t dy, num_t dz, num_t frequency, num_t channelWidth, double totalTime, double integrationTime)
{
	double sampleGain = integrationTime/(12.0*60.0*60.0) * channelWidth;
	num_t wavelength = 1.0L / frequency;
	size_t index = 0;
	for(num_t t=0.0;t<totalTime;t+=integrationTime)
	{
		const double timeInDays = t/(12.0*60.0*60.0);
		const num_t earthLattitudeApprox = timeInDays*M_PIn;
		num_t u, v, r1, i1, r2, i2;
		GetUVPosition(u, v, earthLattitudeApprox, delayDirectionDEC, delayDirectionRA, dx, dy, dz, wavelength);
		SimulateAntenna(timeInDays, delayDirectionDEC, delayDirectionRA, 0, 0, frequency, earthLattitudeApprox, r1, i1);
		SimulateAntenna(timeInDays, delayDirectionDEC, delayDirectionRA, dx, dy, frequency, earthLattitudeApprox, r2, i2);
		num_t
			r = r1 * r2 - (i1 * -i2),
			i = r1 * -i2 + r2 * i1;
		receiver.SetUVValue(index, u, v, r * sampleGain, i * sampleGain, 1.0);
		++index;
	}
}

template void Model::SimulateCorrelation(struct OutputReceiver<UVImager> &receiver, num_t delayDirectionDEC, num_t delayDirectionRA, num_t dx, num_t dy, num_t dz, num_t frequency, num_t channelWidth, double totalTime, double integrationTime);

void Model::SimulateAntenna(double time, num_t delayDirectionDEC, num_t delayDirectionRA, num_t dx, num_t dy, num_t frequency, num_t earthLattitude, num_t &r, num_t &i)
{
	r = 0.0;
	i = 0.0;
	num_t delayW = GetWPosition(delayDirectionDEC, delayDirectionRA, frequency, earthLattitude, dx, dy);
	for(std::vector<Source *>::const_iterator iter=_sources.begin();iter!=_sources.end();++iter)
	{
		Source &source = **iter;
		num_t w = GetWPosition(source.Dec(time), source.Ra(time), frequency, earthLattitude, dx, dy);
		num_t fieldStrength = source.SqrtFluxIntensity(time) + RNG::Gaussian() * _sourceSigma;
		num_t noiser, noisei;
		RNG::ComplexGaussianAmplitude(noiser, noisei);
		r += fieldStrength * cosn((w - delayW) * M_PIn * 2.0) + noiser * _noiseSigma;
		i += fieldStrength * sinn((w - delayW) * M_PIn * 2.0) + noisei * _noiseSigma;
	}
}

void Model::SimulateUncoherentAntenna(double time, num_t delayDirectionDEC, num_t delayDirectionRA, num_t dx, num_t dy, num_t frequency, num_t earthLattitude, num_t &r, num_t &i, size_t index)
{
	num_t delayW = GetWPosition(delayDirectionDEC, delayDirectionRA, frequency, earthLattitude, dx, dy);

	//if(index%(_sources.size()+1) == _sources.size())
	//{
	num_t noiser, noisei;
	RNG::ComplexGaussianAmplitude(noiser, noisei);
	noiser *= _noiseSigma;
	noisei *= _noiseSigma;
	//}
	//else {
		Source &source = *_sources[index%_sources.size()];
		num_t w = GetWPosition(source.Dec(time), source.Ra(time), frequency, earthLattitude, dx, dy);
		num_t fieldStrength = source.SqrtFluxIntensity(time) + RNG::Gaussian() * _sourceSigma;
		r = fieldStrength * cosn((w - delayW) * M_PIn * 2.0) + noiser;
		i = fieldStrength * sinn((w - delayW) * M_PIn * 2.0) + noisei;
	//}
}

void Model::GetUVPosition(num_t &u, num_t &v, num_t earthLattitudeAngle, num_t delayDirectionDEC, num_t delayDirectionRA, num_t dx, num_t dy, num_t dz, num_t wavelength)
{
	// Rotate baseline plane towards phase center, first rotate around z axis, then around x axis
	long double raRotation = -earthLattitudeAngle + delayDirectionRA + M_PIn*0.5L;
	long double tmpCos = cosn(raRotation);
	long double tmpSin = sinn(raRotation);

	long double dxProjected = tmpCos*dx - tmpSin*dy;
	long double tmpdy = tmpSin*dx + tmpCos*dy;

	tmpCos = cosn(-delayDirectionDEC);
	tmpSin = sinn(-delayDirectionDEC);
	long double dyProjected = tmpCos*tmpdy - tmpSin*dz;

	// Now, the newly projected positive z axis of the baseline points to the phase center
	long double baselineLength = sqrtn(dxProjected*dxProjected + dyProjected*dyProjected);
	
	long double baselineAngle;
	if(baselineLength == 0.0)
		baselineAngle = 0.0;
	else {
		baselineLength /= 299792458.0L * wavelength;
		if(dxProjected > 0.0L)
			baselineAngle = atann(dyProjected/dxProjected);
		else
			baselineAngle = M_PIn - atann(dyProjected/-dxProjected);
	}
		
	u = cosn(baselineAngle)*baselineLength;
	v = -sinn(baselineAngle)*baselineLength;
}

void Model::loadUrsaMajor(double ra, double dec, double factor)
{
	double
		s = 0.00005 * factor, //scale
		rs = 6.0 + 2.0 * factor; // stretch in dec
	double fluxoffset = 0.0;

	AddSource(dec + s*rs*40, ra + s*72, 8.0/8.0 + fluxoffset); // Dubhe
	AddSource(dec + s*rs*-16, ra + s*81, 4.0/8.0 + fluxoffset); // Beta
	AddSource(dec + s*rs*-45, ra + s*2, 3.0/8.0 + fluxoffset); // Gamma
	AddSource(dec + s*rs*-6, ra + s*-27, 2.0/8.0 + fluxoffset); // Delta
	AddSource(dec + s*rs*-4, ra + s*-85, 6.0/8.0 + fluxoffset); // Alioth
	AddSource(dec + s*rs*2, ra + s*-131, 5.0/8.0 + fluxoffset); // Zeta
	AddSource(dec + s*rs*-36, ra + s*-192, 7.0/8.0 + fluxoffset); // Alkaid

	//AddSource(cd, cr - M_PI, 4.0);
}

void Model::loadUrsaMajorDistortingSource(double ra, double dec, double factor, bool slightlyMiss)
{
	if(slightlyMiss)
	{
		dec += 0.005;
		ra += 0.002;
	}
	AddSource(dec - 0.12800 * factor, ra + 0.015 + 0.015 * factor, 4.0);
}

void Model::loadOnAxisSource(double ra, double dec, double factor)
{
	AddSource(dec - 0.01280 * factor, ra + 0.0015 + 0.0015 * factor, 4.0);
}

void Model::loadUrsaMajorDistortingVariableSource(double ra, double dec, double factor, bool weak, bool slightlyMiss)
{
	double flux = 4.0;
	dec = dec - 0.12800 * factor;
	ra = ra + 0.015 + 0.015 * factor;
	if(slightlyMiss)
	{
		dec += 0.005;
		ra += 0.002;
	}
	if(weak)
	{
		flux /= 100.0;
	}
	AddVariableSource(dec, ra, flux);
}