-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathfeedback.c
367 lines (327 loc) · 12.7 KB
/
feedback.c
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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <assert.h>
#include "pkd.h"
#include "feedback.h"
#include "supernova.h"
#ifdef STARFORM
void snCalcWindFeedback(SN sn, SFEvent sfEvent,
double dTimeYr, /* current time in years */
double dDelta, /* length of timestep (years) */
FBEffects *fbEffects);
void snCalcUVFeedback(SN sn, SFEvent sfEvent,
double dTimeYr, /* current time in years */
double dDelta, /* length of timestep (years) */
FBEffects *fbEffects);
/*
* Feedback module for GASOLINE
*/
double mod(double a, int b) {
/* printf("MOD: a=%g, b=%d, floor(a/b)=%g\n",a,b,floor(a/b));*/
return (a-b*floor(a/b));
}
void fbInitialize(FB *pfb)
{
FB fb;
fb = (FB) malloc(sizeof(struct fbContext));
assert(fb != NULL);
fb->dGmUnit = 0.0;
fb->dSecUnit = 0.0;
fb->dErgPerGmUnit = 0.0;
*pfb = fb;
}
void pkdFeedback(PKD pkd, FB fb, SN sn, double dTime, double dDelta,
FBEffects *fbTotals)
{
int i;
PARTICLE *p;
int n = pkdLocal(pkd);
SFEvent sfEvent;
FBEffects fbEffects;
double dTotMassLoss;
double dTotMetals;
double dTotMOxygen;
double dTotMIron;
double dSNIaMassStore;
double dNSNII, dProb, dStarAge, dMinAge;
double dRandomNum;
double dTimeYr = dTime*fb->dSecUnit/SEC_YR;
double dDeltaYr = dDelta*fb->dSecUnit/SEC_YR;
int j;
for(i = 0; i < FB_NFEEDBACKS; i++) {
fbTotals[i].dMassLoss = 0.0;
fbTotals[i].dEnergy = 0.0;
fbTotals[i].dMetals = 0.0;
fbTotals[i].dMIron = 0.0;
fbTotals[i].dMOxygen = 0.0;
}
for(i = 0; i < n; ++i) {
p = &pkd->pStore[i];
#ifdef FBPARTICLE
if (TYPETest(p, TYPE_FEEDBACK)) printf("FBP status: %d: %g %g %g %g\n",p->iOrder,dTimeYr,p->fTimeCoolIsOffUntil*fb->dSecUnit/SEC_YR,p->u,p->uDotFB);
#endif
if (pkdIsStar(pkd, p) && p->fTimeForm >= 0.0) {
dTotMassLoss = 0.0;
dTotMetals = 0.0;
dTotMOxygen = 0.0;
dTotMIron = 0.0;
p->uDotFB = 0.0;
p->fNSN = 0.0;
#ifdef FBPARTICLE
{
double tstart = 4e6, tend = 30e6; /* yr */
double mdotonmstar = 25/100./(tend-tstart); /* gm / gm / yr */
double edotonmstar = 1e51/(100*2e33)/(tend-tstart); /* erg / gm / yr */
double mFB = fb->dFBMassRatio*p->fMassForm; /* mass of fb particles (code units) */
double tFB0,tFB1,nFac,dDeltaFB;
int nFB0,nFB1;
tFB1 = dTimeYr-p->fTimeForm*fb->dSecUnit/SEC_YR; /* yr */
tFB0 = tFB1 - dDeltaYr;
if (tFB1 > tstart && tFB0 < tend) {
nFac = mdotonmstar*p->fMassForm/mFB; /* yr^-1 */
nFB0 = floor(nFac*(tFB0 > tstart ? tFB0-tstart : 0)+0.9);
nFB1 = floor(nFac*(tFB1 < tend ? tFB1-tstart : tend-tstart)+0.9);
printf("FBP: %d %g %g %g %d %d\n",p->iOrder,dTimeYr,tFB0,tFB1,nFB0,nFB1);
while (nFB1 > nFB0) {
PARTICLE pNew;
pNew = *p;
TYPEReset(&pNew, TYPE_STAR|TYPE_TREEACTIVE|TYPE_SMOOTHACTIVE|TYPE_ACTIVE);
TYPESet(&pNew, TYPE_GAS|TYPE_FEEDBACK);
/* For mass conservation would need to do this ... */
/*p->fMass -= mFB;
assert(p->fMass > 0);*/
dDeltaFB = 1/(nFac*fb->dSecUnit/SEC_YR); /* code time */
if (dDeltaFB < dDelta) dDeltaFB = dDelta;
pNew.fMass = mFB;
pNew.u = (edotonmstar/mdotonmstar)/fb->dErgPerGmUnit;
pNew.uPred = pNew.u;
pNew.uDotFB = (pNew.u*0.999)/dDeltaFB;
pNew.fTimeCoolIsOffUntil = dTime + 0.9999*dDeltaFB;
pNew.c = sqrt((5./3.*2/3.)*pNew.u); /* Estimate final C */
{
double ph,dt_diff, dt;
struct GasPressureContext gpc;
ph = sqrt(p->fBall2*0.25); /* gas h at star */
dt = fb->dtFacCourant*ph/(2*pNew.c); /* dt Courant */
#ifdef THERMALCOND
{
gpc.dThermalCondCoeffCode = fb->dThermalCondCoeffCode*fb->a;
gpc.dThermalCondSatCoeff = fb->dThermalCondSatCoeff/fb->a;
gpc.dThermalCond2CoeffCode = fb->dThermalCond2CoeffCode*fb->a;
gpc.dThermalCond2SatCoeff = fb->dThermalCond2SatCoeff/fb->a;
}
pNew.fDensity = p->fDensity;
pkdSetThermalCond(pkd, &gpc, &pNew);
if (p->fThermalCond > 0) {
dt_diff = fb->dtFacDiffusion*ph*ph*pNew.fDensity/pNew.fThermalCond; /* gas density at star */
if (dt_diff < dt) dt = dt_diff;
}
#endif
pNew.dt = dt;
pNew.iRung = pkdOneParticleDtToRung( 0,dDelta,pNew.dt );
}
pNew.u *= 0.001;
pNew.uPred = pNew.u;
pNew.PoverRho2 = (2/3.)*pNew.uPred/p->fDensity;
pNew.uDot = 0;
pNew.uDotPdV = 0;
pNew.uDotAV = 0;
pNew.uDotDiff = 0;
pkdNewParticle(pkd, pNew);
printf("FBP: Particle made %g %g %g %g %d\n",mFB,pNew.u,dDeltaFB*fb->dSecUnit/SEC_YR,pNew.dt,pNew.iRung);
nFB1--;
}
}
}
continue; /* skip normal feedback */
#endif
sfEvent.dMass = p->fMassForm*fb->dGmUnit/MSOLG;
sfEvent.dTimeForm = p->fTimeForm*fb->dSecUnit/SEC_YR;
dStarAge = dTime - sfEvent.dTimeForm;
sfEvent.dMetals = p->fMetals;
sfEvent.dMFracOxygen = p->fMFracOxygen;
sfEvent.dMFracIron = p->fMFracIron;
/*
* Call all the effects in order and accumulate them.
*/
dSNIaMassStore=0.0; /* Stores mass loss of Ia so as
not to double count it in
wind feedback */
for(j = 0; j < FB_NFEEDBACKS; j++) {
dNSNII = 0;
switch (j) {
case FB_SNII:
snCalcSNIIFeedback(sn, sfEvent, dTimeYr,
dDeltaYr, &fbEffects);
if( sn->dESN > 0.0)
dNSNII = fbEffects.dEnergy * MSOLG*fbEffects.dMassLoss/
sn->dESN;
/* Blow winds before SN (power of winds ~ power of SN) */
dMinAge = dSTLtimeMStar(&sn->ppdva, sn->dMSNIImax,
sfEvent.dMetals);
if (dNSNII > 0 && sn->iNSNIIQuantum > 0 &&
dStarAge > dMinAge) {
/* Make sure only a iNSNIIQuantum number of
* SNII go off at a time */
dProb = mod(dNSNII, sn->iNSNIIQuantum)/
sn->iNSNIIQuantum;
dRandomNum = (rand()/((double) RAND_MAX));
/* printf("Random Number = %g\n",dRandomNum);*/
if(dRandomNum < dProb) /* SN occurred */ {
/* Adds missing part to make up quantum */
p->fNSN = dNSNII + (1.-dProb)*sn->iNSNIIQuantum;
/* printf("NSN: +factor=%g dNSNII=%g result=%g fNSN=%g\n",(1.-dProb)*sn->iNSNIIQuantum,dNSNII,dNSNII + (1.-dProb)*sn->iNSNIIQuantum,p->fNSN);*/
}
else {
p->fNSN = dNSNII - dProb*sn->iNSNIIQuantum;
/* printf("NSN: -factor=%g dNSNII=%g result=%g fNSN=%g\n",dProb*sn->iNSNIIQuantum,dNSNII,dNSNII - dProb*sn->iNSNIIQuantum,p->fNSN);*/
}
if(p->fNSN < sn->iNSNIIQuantum) p->fNSN = 0;
fbEffects.dEnergy = p->fNSN*sn->dESN/(MSOLG*fbEffects.dMassLoss);
}
else if(dStarAge < dMinAge && sn->iNSNIIQuantum)
p->fNSN = dNSNII;
else p->fNSN += dNSNII;
break;
case FB_SNIA:
snCalcSNIaFeedback(sn, sfEvent, dTimeYr,
dDeltaYr, &fbEffects);
dSNIaMassStore=fbEffects.dMassLoss;
break;
case FB_WIND:
snCalcWindFeedback(sn, sfEvent, dTimeYr,
dDeltaYr, &fbEffects);
if(dSNIaMassStore < fbEffects.dMassLoss)
fbEffects.dMassLoss -= dSNIaMassStore;
break;
case FB_UV:
snCalcUVFeedback(sn, sfEvent, dTimeYr, dDeltaYr,
&fbEffects);
break;
default:
assert(0);
}
fbEffects.dMassLoss *= MSOLG/fb->dGmUnit;
fbEffects.dEnergy /= fb->dErgPerGmUnit;
#ifdef FBPARTICLEMIMIC
double tstart = 4e6, tend = 30e6;
double mdotonmstar = 25/100./(tend-tstart);
double edotonmstar = 1e51/(100*2e33)/(tend-tstart);
double tFB0,tFB1,dDeltaFB;
tFB1 = dTimeYr-p->fTimeForm*fb->dSecUnit/SEC_YR;
tFB0 = tFB1 - dDeltaYr;
if (tFB1 > tstart && tFB0 < tend) {
fbEffects.dMassLoss = dDeltaYr*p->fMassForm*mdotonmstar/FB_NFEEDBACKS;
fbEffects.dEnergy = (edotonmstar/mdotonmstar)/fb->dErgPerGmUnit;
}
else {
fbEffects.dMassLoss = 0;
fbEffects.dEnergy = 0;
}
#endif
dTotMassLoss += fbEffects.dMassLoss;
p->uDotFB += fbEffects.dEnergy*fbEffects.dMassLoss;
dTotMetals += fbEffects.dMetals*fbEffects.dMassLoss;
dTotMOxygen += fbEffects.dMOxygen*fbEffects.dMassLoss;
dTotMIron += fbEffects.dMIron*fbEffects.dMassLoss;
fbTotals[j].dMassLoss += fbEffects.dMassLoss;
fbTotals[j].dEnergy += fbEffects.dEnergy*fbEffects.dMassLoss;
fbTotals[j].dMetals += fbEffects.dMetals*fbEffects.dMassLoss;
fbTotals[j].dMIron += fbEffects.dMIron*fbEffects.dMassLoss;
fbTotals[j].dMOxygen += fbEffects.dMOxygen*fbEffects.dMassLoss;
}
/*
* Modify star particle
*/
/* fprintf(stderr,"Mass dTotMassLoss %d %g %g %g %g\n",p->iOrder,p->fMass,dTotMassLoss,dTime/(fb->dSecUnit/SEC_YR),p->fTimeForm);*/
assert(p->fMass > dTotMassLoss);
p->fMass -= dTotMassLoss;
p->fMSN = dTotMassLoss;
/* The SNMetals and uDotFB (SN rate) used to be specific
quantities, but we are making them totals as
they leave the stars so that they are easier
to divvy up among the gas particles in
distSNEnergy in smoothfcn.c. These quantities
will be converted back to specific quantities when
they are parts of gas particles. */
p->fSNMetals = dTotMetals;
p->fMIronOut = dTotMIron;
p->fMOxygenOut = dTotMOxygen;
p->uDotFB /= dDelta; /* convert to rate */
}
else if(pkdIsGas(pkd, p)){
assert(p->u >= 0.0);
assert(p->uPred >= 0.0);
#ifdef FBPARTICLE
if (dTime > p->fTimeCoolIsOffUntil) p->uDotFB = 0;
#else
p->uDotFB = 0.0; /* reset SN heating rate of gas to zero */
p->uDotESF = 0.0; /* reset SN heating rate of gas to zero */
#endif
}
}
}
void snCalcWindFeedback(SN sn, SFEvent sfEvent,
double dTimeYr, /* current time in years */
double dDelta, /* length of timestep (years) */
FBEffects *fbEffects)
{
double dMStarMin, dMStarMax;
double dStarLtimeMin, dStarLtimeMax;
double dMCumMin, dMCumMax,dMTot;
double dMmin, dMmax;
double dMassFracReturned;
double dMDying;
/* First determine if dying stars are between 1-8 Msolar
* stellar lifetimes corresponding to beginning and end of
* current timestep with respect to starbirth time in yrs */
dMmin=1.0;
dMmax=8.0;
dStarLtimeMin = dTimeYr - sfEvent.dTimeForm;
dStarLtimeMax = dStarLtimeMin + dDelta;
dMStarMin = dSTMStarLtime(&sn->ppdva, dStarLtimeMax, sfEvent.dMetals);
dMStarMax = dSTMStarLtime(&sn->ppdva, dStarLtimeMin, sfEvent.dMetals);
assert(dMStarMax >= dMStarMin);
if (((dMStarMin < dMmax) && (dMStarMax > dMmin)) && dMStarMax > dMStarMin) {
/* Mass Fraction returned to ISM taken from Weidemann, 1987, A&A 188 74
then fit to function: MFreturned = 0.86 - exp(-Mass/1.1) */
dMassFracReturned=0.86-exp(-((dMStarMax+dMStarMin)/2.)/1.1);
dMCumMin = dMSCumMass(&sn->MSparam, dMStarMin);
dMCumMax = dMSCumMass(&sn->MSparam, dMStarMax);
dMTot = dMSCumMass(&sn->MSparam,0.0);
/* Find out mass fraction of dying stars, then multiply by the original
mass of the star particle */
if (dMTot == 0.0){
dMDying = 0.0;
} else {
dMDying = (dMCumMin - dMCumMax)/dMTot;
}
dMDying *= sfEvent.dMass;
/* Figure out feedback effects */
fbEffects->dMassLoss = dMDying * dMassFracReturned;
fbEffects->dEnergy = 0.0;
/* Use star's metallicity for gas returned */
fbEffects->dMetals = sfEvent.dMetals;
fbEffects->dMIron = sfEvent.dMFracIron;
fbEffects->dMOxygen = sfEvent.dMFracOxygen;
} else {
fbEffects->dMassLoss = 0.0;
fbEffects->dEnergy = 0.0;
fbEffects->dMetals = 0.0;
fbEffects->dMIron = 0.0;
fbEffects->dMOxygen = 0.0;
}
}
void snCalcUVFeedback(SN sn, SFEvent sfEvent,
double dTimeYr, /* current time in years */
double dDelta, /* length of timestep (years) */
FBEffects *fbEffects)
{
fbEffects->dMassLoss = 0.0;
fbEffects->dEnergy = 0.0;
fbEffects->dMetals = 0.0;
fbEffects->dMIron = 0.0;
fbEffects->dMOxygen = 0.0;
}
#endif