Project

General

Profile

RE: Pour utiliser mon DataBase » FabricationPlant.cxx

Lin Kevin, 02/28/2014 06:28 AM

 
1
#include "FabricationPlant.hxx"
2

    
3
#include "Storage.hxx"
4
#include "FabricationPlant.hxx"
5
#include "Reactor.hxx"
6
#include "EvolutionData.hxx"
7
#include "DataBank.hxx"
8
#include "IsotopicVector.hxx"
9
#include "CLASS.hxx"
10
#include "LogFile.hxx"
11

    
12

    
13

    
14

    
15
#include "TMatrixT.h"
16

    
17
#include <sstream>
18
#include <string>
19
#include <iostream>
20
#include <cmath>
21
#include <algorithm>
22

    
23
	//________________________________________________________________________
24
	//
25
	//		FabricationPlant
26
	//
27
	//
28
	//
29
	//
30
	//________________________________________________________________________
31
ClassImp(FabricationPlant)
32

    
33
template <class T>  T random(T a, T b) //peak random numebr between a and b
34
{
35
	double range = pow(2., 31);
36
	srand(time(NULL)); //initialize the srand
37
	return (T)a + (T)(b-a)*rand()/range;
38
}
39

    
40

    
41
FabricationPlant::FabricationPlant()
42
{
43
	
44

    
45
	
46
}
47

    
48
FabricationPlant::FabricationPlant(LogFile* log)
49
{
50
	
51
	fLog = log;
52
	fChronologicalTimePriority = false;
53
	fFabricationTime = -1;
54
	fUpdateReferenceDBatEachStep = false;
55
	fSubstitutionFuel = false;
56
	
57
		// Warning
58
	
59
	cout	<< "!!INFO!! !!!FabricationPlant!!! A FabricationPlant has been define :" << endl;
60
	cout	<< "\t Chronological Stock Priority set! "<< endl << endl;
61
	cout	<< "!!WARNING!! !!!FabricationPlant!!! You need to set the different stock manually as well as the Fabrication Time Manualy !! " << endl;
62
	
63
	fLog->fLog	<< "!!INFO!! !!!FabricationPlant!!! A FabricationPlant has been define :" << endl;
64
	fLog->fLog	<< "\t Chronological Stock Priority set! "<< endl << endl;
65
	fLog->fLog	<< "!!WARNING!! !!!FabricationPlant!!! You need to set the different stock manually as well as the Fabrication Time Manualy !! " << endl;
66
	
67
	
68
}
69

    
70
FabricationPlant::FabricationPlant(LogFile* log, Storage* storage, Storage* reusable, double fabircationtime)
71
{
72
	
73
	fLog = log;
74
	
75
	fChronologicalTimePriority = false;
76
	fUpdateReferenceDBatEachStep = false;
77
	fSubstitutionFuel = false;
78
	
79
	fFabricationTime = (cSecond)fabircationtime;
80
	fStorage = storage;
81
	fReUsable = reusable;
82
	
83
	
84
	cout	<< "!!INFO!! !!!FabricationPlant!!! A FabricationPlant has been define :" << endl;
85
	cout	<< "\t Chronological Stock Priority has been set! "<< endl;
86
	cout	<< "\t Fabrication time set to \t " << (double)(fFabricationTime/3600/24/365.25) << " year" << endl << endl;
87
	
88
	fLog->fLog	<< "!!INFO!! !!!FabricationPlant!!! A FabricationPlant has been define :" << endl;
89
	fLog->fLog	<< "\t Chronological Stock Priority has been set! "<< endl;
90
	fLog->fLog	<< "\t Fabrication time set to \t " << (double)(fFabricationTime/3600/24/365.25) << " year" << endl << endl;
91
	
92
	
93
}
94

    
95

    
96

    
97
	//________________________________________________________________________
98
FabricationPlant::~FabricationPlant()
99
{
100
	
101
	
102
}
103

    
104

    
105
	//________________________________________________________________________
106
void	FabricationPlant::AddValorisableIV(ZAI zai, double factor)
107
{
108
	
109
	pair<map<ZAI, double>::iterator, bool> IResult;
110
	if(factor > 1) factor = 1;
111
	
112
	if(factor > 0)
113
	{
114
		IResult = fValorisableIV.insert( pair<ZAI ,double>(zai, factor));
115
		if(IResult.second == false)
116
			IResult.first->second = factor;
117
	}
118
	
119
}
120

    
121

    
122

    
123
	//________________________________________________________________________
124
	//_______________________________ Evolution ______________________________
125
	//________________________________________________________________________
126
void FabricationPlant::Evolution(cSecond t)
127
{
128
	
129
		// Check if the FabricationPlant has been created ...
130
	if(t == fInternalTime && t != 0) return;
131
		// Make the evolution for the FabricationPlant ...
132
	FabricationPlantEvolution(t);
133
		// ... And Finaly update the AbsoluteInternalTime
134
	fInternalTime = t;
135
	
136
}
137

    
138
	//________________________________________________________________________
139
void FabricationPlant::FabricationPlantEvolution(cSecond t)
140
{
141
	
142
	
143
	
144
	map<int ,cSecond >::iterator it;
145
	for( it = fReactorNextStep.begin(); it!= fReactorNextStep.end(); it++ )
146
	{
147
		if( t + fFabricationTime >= fParc->GetReactor()[ (*it).first ]->GetCreationTime()
148
		   && t + fFabricationTime < fParc->GetReactor()[ (*it).first ]->GetCreationTime() + fParc->GetReactor()[ (*it).first ]->GetLifeTime())
149
		{
150
			if( (*it).second == t )
151
			{
152
				BuildFuelForReactor( (*it).first );
153
				(*it).second += fParc->GetReactor()[ (*it).first ]->GetCycleTime();
154
			}
155
			else if ( (*it).second - fParc->GetReactor()[ (*it).first ]->GetCycleTime() + fFabricationTime > t )
156
			{
157
				map<int ,IsotopicVector >::iterator it2 = fReactorFuturIV.find( (*it).first );
158
				if (it2 != fReactorFuturIV.end())
159
					(*it2).second = GetDecay((*it2).second, t - fInternalTime );
160
			}
161
		}
162
	}
163
	
164
	
165
	
166
}
167

    
168

    
169
	//________________________________________________________________________
170
void FabricationPlant::BuildFuelForReactor(int ReactorId)
171
{
172
	
173
	
174
	DataBank<IsotopicVector>* FuelType = fParc->GetReactor()[ReactorId]->GetFuelType();
175
	
176
	
177
	if(FuelType->GetFuelType() != "ThPu")
178
	{
179
		cout << "!!Bad Trouble!! !!!FabricationPlant!!! Try to do ThPu with a not ThPued DB "<< endl;
180
		fLog->fLog << "!!Bad Trouble!! !!!FabricationPlant!!! Try to do ThPu with a not ThPued DB" << endl;
181
		exit (1);
182
	}
183
	map<ZAI, double> ZAImass;
184
////!!!!
185
	ZAImass.insert( pair< ZAI,double >( ZAI(90,232,0), 232038060.000e-6 ) ); 
186
     
187
        ZAIMass.insert( pair< ZAI,double >( ZAI(92,232,0), 232000000e-6 ) );
188
	ZAIMass.insert( pair< ZAI,double >( ZAI(92,233,0), 233000000e-6 ) );
189
	ZAIMass.insert( pair< ZAI,double >( ZAI(92,234,0), 234000000e-6 ) );
190
	ZAIMass.insert( pair< ZAI,double >( ZAI(92,235,0), 235043929.918e-6 ) );
191
	ZAIMass.insert( pair< ZAI,double >( ZAI(92,236,0), 236000000e-6 ) );	
192
	ZAIMass.insert( pair< ZAI,double >( ZAI(92,238,0), 238050788.247e-6 ) );
193

    
194
	ZAImass.insert( pair< ZAI,double >( ZAI(94,238,0), 238049559.894e-6 ) );
195
	ZAImass.insert( pair< ZAI,double >( ZAI(94,239,0), 239052163.381e-6 ) );
196
	ZAImass.insert( pair< ZAI,double >( ZAI(94,240,0), 240053813.545e-6 ) );
197
	ZAImass.insert( pair< ZAI,double >( ZAI(94,241,0), 241056851.456e-6 ) );
198
	ZAImass.insert( pair< ZAI,double >( ZAI(94,242,0), 242058742.611e-6 ) );
199

    
200
	ZAImass.insert( pair< ZAI,double >( ZAI(95,241,0), 241056829.144e-6 ) );
201
	
202
	
203
	double Na = 6.02214129e23;	//N Avogadro
204
	
205
	double HMmass = fParc->GetReactor()[ReactorId]->GetHeavyMetalMass();
206
	double BU = fParc->GetReactor()[ReactorId]->GetBurnUp();
207
	IsotopicVector FullUsedStock;
208
	IsotopicVector stock;
209
	
210
	bool FuelBuild = false;
211
	while(!FuelBuild)
212
	{
213
		double nPu_0 = 0;
214
		double MPu_0 = 0;
215
		{
216
			map<ZAI ,double>::iterator it;
217
			
218
			map<ZAI ,double> isotopicquantity = GetDecay( FullUsedStock , fFabricationTime).GetSpeciesComposition(94).GetIsotopicQuantity();
219
			for( it = isotopicquantity.begin(); it != isotopicquantity.end(); it++ )
220
				nPu_0 += (*it).second;
221
			
222
			isotopicquantity = FullUsedStock.GetSpeciesComposition(94).GetIsotopicQuantity();
223
			for( it = isotopicquantity.begin(); it != isotopicquantity.end(); it++ )
224
				MPu_0 += (*it).second*ZAImass.find( (*it).first )->second/Na*1e-6;
225
		}
226
		
227
		stock = GetStockToRecycle();
228
		
229
		if( stock.GetZAIIsotopicQuantity(ZAI(-1,-1,-1)) == 1 ) // Not enought stock to build the needed fuel
230
		{
231
			if (!fSubstitutionFuel)
232
			{
233
				{
234
					EvolutionData evolutiondb;
235
					pair<map<int, EvolutionData>::iterator, bool> IResult;
236
					IResult = fReactorFuturDB.insert( pair<int, EvolutionData>(ReactorId,evolutiondb) );
237
					if(IResult.second == false)
238
						IResult.first->second = evolutiondb;
239
				}
240
				{
241
					IsotopicVector EmptyIV;
242
					pair<map<int, IsotopicVector>::iterator, bool> IResult;
243
					IResult = fReactorFuturIV.insert( pair<int, IsotopicVector>(ReactorId,EmptyIV) );
244
					if(IResult.second == false)
245
						IResult.first->second = EmptyIV;
246
				}
247
			}
248
			else
249
			{
250
				{
251
					EvolutionData evolutiondb = fSubstitutionEvolutionData* HMmass;
252
					pair<map<int, EvolutionData>::iterator, bool> IResult;
253
					IResult = fReactorFuturDB.insert( pair<int, EvolutionData>(ReactorId,evolutiondb) );
254
					if(IResult.second == false)
255
						IResult.first->second = evolutiondb;
256
				}
257
				{
258
					IsotopicVector IV = fSubstitutionEvolutionData.GetIsotopicVectorAt(0)* HMmass;
259
					pair<map<int, IsotopicVector>::iterator, bool> IResult;
260
					IResult = fReactorFuturIV.insert( pair<int, IsotopicVector>(ReactorId, IV) );
261
					if(IResult.second == false)
262
						IResult.first->second = IV;
263
				}
264
				
265
			}
266
			FuelBuild = true;
267
			fFractionToTake.clear();
268
		}
269
		else
270
		{
271
			double nPu_1 = 0;
272
			double MPu_1 = 0;
273
			double Sum_AlphaI_nPuI = 0;
274
			double Sum_AlphaI_nPuI0 = 0;
275
			{
276
				map<ZAI ,double>::iterator it;
277
				map<ZAI ,double> isotopicquantity = GetDecay( stock , fFabricationTime).GetSpeciesComposition(94).GetIsotopicQuantity();
278
				
279
				for( it = isotopicquantity.begin(); it != isotopicquantity.end(); it++ )
280
				{
281
					if ((*it).first.A() >= 238 && (*it).first.A() <= 242)
282
					{
283
						nPu_1 += (*it).second;
284
						Sum_AlphaI_nPuI += FuelType->GetFuelParameter()[(*it).first.A() -237]*(*it).second;
285
					}
286
				}
287
				
288
				isotopicquantity = stock.GetSpeciesComposition(94).GetIsotopicQuantity();
289
				for( it = isotopicquantity.begin(); it != isotopicquantity.end(); it++ )
290
					if ((*it).first.A() >= 238 && (*it).first.A() <= 242)
291
					{
292
						MPu_1 += (*it).second * (ZAImass.find( (*it).first )->second)/Na*1e-6;
293
					}
294
				
295
				isotopicquantity = GetDecay( FullUsedStock , fFabricationTime).GetSpeciesComposition(94).GetIsotopicQuantity();
296
				for( it = isotopicquantity.begin(); it != isotopicquantity.end(); it++ )
297
					if ((*it).first.A() >= 238 && (*it).first.A() <= 242)
298
					{
299
						Sum_AlphaI_nPuI0 += FuelType->GetFuelParameter()[(*it).first.A() -237]*(*it).second;
300
					}
301
			}
302
			
303
			double StockFactionToUse = 0;
304
	 ////!!!
305
			double NT = HMmass*1e6 * Na / (ZAImass.find( ZAI(90,232,0) )->second);  //I changed the the depleted uranium into Th
306
			
307
			double N1 = (BU - FuelType->GetFuelParameter()[6]) * NT; 
308
			double N2 = -Sum_AlphaI_nPuI0;
309
         ////!!!
310
			double N3 = -FuelType->GetFuelParameter()[0] * Na / (ZAImass.find( ZAI(90,232,0) )->second) * (HMmass*1e6 - MPu_0*1e6);  //The same as above
311
			
312
			double D1 = Sum_AlphaI_nPuI;
313
         ////!!!
314
			double D2 = -FuelType->GetFuelParameter()[0] * MPu_1*1e6 * Na / (ZAImass.find( ZAI(90,232,0) )->second ) ;   //The same as above
315
			
316
			StockFactionToUse = (N1 + N2 + N3) / (D1 + D2);
317
			
318
			if(StockFactionToUse < 0)
319
			{
320
				cout << "!!Bad Trouble!! !!!FabricationPlant!!! Oups Bug in calculating stock fraction to use "<< endl;
321
				fLog->fLog << "!!Bad Trouble!! !!!FabricationPlant!!! Oups Bug in calculating stock fraction to use" << endl;
322
				exit (1);
323
			}
324
			
325
			if( StockFactionToUse > 1 )
326
			{
327
				
328
				FullUsedStock += stock;
329
				RecycleStock(1);
330
				FuelBuild = false;
331
			}
332
			else
333
			{
334
				RecycleStock(StockFactionToUse);
335
				
336
				IsotopicVector IVBeginCycle;
337
				FuelBuild = true;
338
              ////!!!!
339
				ZAI Th = ZAI(90,232,0);  //I changed the U8 = ZAI(92,238,0) here
340
			//	ZAI U5 = ZAI(92,235,0); 
341
              ////!!!!
342
				double Th_Quantity = (HMmass - (MPu_0+StockFactionToUse*MPu_1 ))/(ZAImass.find( ZAI(90,232,0) )->second ) *Na/1e-6;   //I changed the the depleted uranium into Th
343
	      ////!!!
344
				fParc->AddGodIncome( Th, Th_Quantity );
345
			        //fParc->AddGodIncome( U5, U8_Quantity*0.003 );
346
				
347
				for(int i = (int)fFractionToTake.size()-1; i >= 0; i--)
348
				{
349
					IVBeginCycle += fStorage->GetStock()[fFractionToTake[i].first].GetSpeciesComposition(94)*( fFractionToTake[i].second );					
350
					fReUsable->AddToStock(fStorage->GetStock()[fFractionToTake[i].first]*(fFractionToTake[i].second)
351
							      - fStorage->GetStock()[fFractionToTake[i].first].GetSpeciesComposition(94)*(fFractionToTake[i].second));
352
					
353
					fStorage->TakeFractionFromStock(fFractionToTake[i].first,fFractionToTake[i].second);			
354
					
355
				}
356
				fFractionToTake.clear();
357
				
358
               ////!!!!
359
				IVBeginCycle += Th_Quantity*Th;
360
				EvolutionData evolutiondb = BuildEvolutiveDB(ReactorId, IVBeginCycle);
361
				
362
				{
363
					pair<map<int, EvolutionData>::iterator, bool> IResult;
364
					IResult = fReactorFuturDB.insert( pair<int, EvolutionData>(ReactorId,evolutiondb) );
365
					if(IResult.second == false)
366
						IResult.first->second = evolutiondb;
367
				}
368
				{
369
					pair<map<int, IsotopicVector>::iterator, bool> IResult;
370
					IResult = fReactorFuturIV.insert( pair<int, IsotopicVector>(ReactorId,IVBeginCycle) );
371
					if(IResult.second == false)
372
						IResult.first->second = IVBeginCycle;
373
				}
374
			}
375
		}
376
	}
377
	
378
	
379
}
380

    
381
void	FabricationPlant::SetSubstitutionFuel(EvolutionData fuel)
382
{
383
	
384
	fSubstitutionFuel = true;
385
	fSubstitutionEvolutionData = fuel / fuel.GetHMMass();
386
	
387
}
388

    
389

    
390
	//________________________________________________________________________
391
	//_________________________________ Decay ________________________________
392
	//________________________________________________________________________
393
IsotopicVector FabricationPlant::GetDecay(IsotopicVector isotopicvector, cSecond t)
394
{
395
	
396
	IsotopicVector IV;
397
	
398
	map<ZAI ,double> isotopicquantity = isotopicvector.GetIsotopicQuantity();
399
	
400
	map<ZAI ,double >::iterator it;
401
	for( it = isotopicquantity.begin(); it != isotopicquantity.end(); it++ )
402
	{
403
		if((*it).second > 0)
404
		{
405
 			IsotopicVector ivtmp = fDecayDataBase->Evolution(it->first, t) * (*it).second ;
406
			IV += ivtmp;
407
		}
408
	}
409
	
410
	return IV;
411
}
412

    
413

    
414
	//________________________________________________________________________
415
	//_____________________________ Reactor & DB _____________________________
416
	//________________________________________________________________________
417
EvolutionData FabricationPlant::BuildEvolutiveDB(int ReactorId,IsotopicVector isotopicvector)
418
{
419
	
420
	DataBank<IsotopicVector>* evolutiondb = fParc->GetReactor()[ReactorId]->GetFuelType();
421
	
422
	isotopicvector = GetDecay(isotopicvector, fFabricationTime);
423
	
424
	EvolutionData EvolBuild;
425
	/*
426
	if( fUpdateReferenceDBatEachStep == true )
427
	{
428
		EvolutionData EvolBuild = evolutiondb->GenerateDB(isotopicvector,
429
								  fParc->GetReactor()[ReactorId]->GetCycleTime(),
430
								  fParc->GetReactor()[ReactorId]->GetPower());
431
	}
432
	else
433
	{
434
		map<double, EvolutionData> distances = evolutiondb->GetDistancesTo(isotopicvector);
435
		EvolBuild = distances.begin()->second.GenerateDBFor(isotopicvector);
436
		
437
	}
438
	 */
439
	EvolBuild = evolutiondb->GenerateEvolutionData(isotopicvector,
440
					    fParc->GetReactor()[ReactorId]->GetCycleTime(),
441
					    fParc->GetReactor()[ReactorId]->GetPower());
442
	 
443
	return EvolBuild;
444
	
445
}
446

    
447
	//________________________________________________________________________
448
void FabricationPlant::TakeReactorFuel(int Id)
449
{
450
	
451
	
452
	IsotopicVector IV;
453
	map<int ,IsotopicVector >::iterator it2 = fReactorFuturIV.find( Id );
454
	if (it2 != fReactorFuturIV.end())
455
		(*it2).second = IV;
456
	
457
}
458

    
459
	//________________________________________________________________________
460
EvolutionData FabricationPlant::GetReactorEvolutionDB(int ReactorId)
461
{
462
	
463
	map< int,EvolutionData >::iterator it = fReactorFuturDB.find(ReactorId);
464
	return (*it).second;
465
	
466
}
467

    
468
IsotopicVector FabricationPlant::GetFullFabrication()
469
{
470
	
471
	IsotopicVector tmp = 0*ZAI(0,0,0);
472
	
473
	map<int, IsotopicVector > reactorNextStep = fReactorFuturIV;
474
	map<int, IsotopicVector >::iterator it;
475
	for ( it = reactorNextStep.begin(); it != reactorNextStep.end(); it++)
476
		tmp += (*it).second;
477

    
478
	return tmp;
479
	
480
}
481

    
482
	//________________________________________________________________________
483
	//_______________________________ Storage ________________________________
484
	//________________________________________________________________________
485

    
486
IsotopicVector FabricationPlant::GetStockToRecycle()
487
{
488
	
489
	IsotopicVector NextStock;
490
	int IdToTake = -1;
491
	
492
	if(fChronologicalTimePriority == true)
493
		IdToTake = (int)( fFractionToTake.size() );
494
	else
495
		IdToTake = (int)( fStorage->GetStock().size() -1 - fFractionToTake.size() );
496
	if(0 <= IdToTake && IdToTake <= (int)fStorage->GetStock().size()-1)
497
	{
498
		NextStock = fStorage->GetStock()[IdToTake];
499
		fFractionToTake.push_back( pair<int,double>(IdToTake,0.) );
500
	}
501
	else NextStock += ZAI(-1,-1,-1) *1;
502
	
503
	return NextStock;
504
	
505
}
506

    
507
	//________________________________________________________________________
508
void FabricationPlant::RecycleStock(double fraction)
509
{
510
	
511
	fFractionToTake.back().second = fraction;
512
	
513
}
514

    
515

    
516

    
517
	//________________________________________________________________________
518
void FabricationPlant::DumpStock()
519
{
520
	
521
	
522
	
523
	
524
}
525

    
526
	//________________________________________________________________________
527
pair<IsotopicVector, IsotopicVector> FabricationPlant::Separation(IsotopicVector isotopicvector)
528
{
529
	
530
		//[0] = re-use ; [1] = waste
531
	pair<IsotopicVector, IsotopicVector>	IVTmp;
532
	
533
	map<ZAI ,double> isotopicquantity = isotopicvector.GetIsotopicQuantity();
534
	map<ZAI ,double >::iterator it;
535
	for(it = isotopicquantity.begin(); it != isotopicquantity.end(); it++)
536
	{
537
		map<ZAI ,double>::iterator it2;
538
		it2 = fValorisableIV.find((*it).first);
539
		if ( it2 != fValorisableIV.end() )
540
		{
541
			IVTmp.first.Add(	(*it).first, (*it).second * (*it2).second );		//re-use
542
			IVTmp.second.Add(	(*it).first, (*it).second * (1-(*it2).second) );	//waste
543
		}
544
		else IVTmp.second.Add(	(*it).first, (*it).second );	//waste
545
	}
546
	
547
	return IVTmp;
548
}
549

    
550

    
551

    
552

    
553

    
554

    
555

    
556

    
557

    
(1-1/5)