Droplet Lagrangian Transient One-dimensional Reacting Code Implementation of both liquid and gas phase governing equations.
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

500 рядки
14KB

  1. #include "UserData.h"
  2. #include "parse.h"
  3. void freeUserData(UserData data){
  4. if(data!=NULL){
  5. if(data->trmix!=NULL){
  6. delete data->trmix;
  7. printf("Transport Deleted!\n");
  8. }
  9. if(data->gas!=NULL){
  10. delete data->gas;
  11. printf("Gas Deleted!\n");
  12. }
  13. if(data->adaptiveGrid){
  14. if(data->grid->xOld!=NULL){
  15. delete[] data->grid->xOld;
  16. printf("old grid array Deleted!\n");
  17. }
  18. if(data->grid->x!=NULL){
  19. delete[] data->grid->x;
  20. printf("current grid array Deleted!\n");
  21. }
  22. if(data->grid!=NULL){
  23. free(data->grid);
  24. printf("grid object Freed!\n");
  25. }
  26. }
  27. else{
  28. if(data->uniformGrid!=NULL){
  29. delete[] data->uniformGrid;
  30. printf("uniformGrid deleted!\n");
  31. }
  32. }
  33. if(data->innerMassFractions!=NULL){
  34. delete[] data->innerMassFractions;
  35. printf("innerMassFractions array Deleted!\n");
  36. }
  37. if(data->output!=NULL){
  38. fclose(data->output);
  39. printf("Output File Cleared from Memory!\n");
  40. }
  41. if(data->gridOutput!=NULL){
  42. fclose(data->gridOutput);
  43. printf("Grid Output File Cleared from Memory!\n");
  44. }
  45. //if(data->ratesOutput!=NULL){
  46. // fclose(data->ratesOutput);
  47. // printf("Rates Output File Cleared from Memory!\n");
  48. //}
  49. if(data->globalOutput!=NULL){
  50. fclose(data->globalOutput);
  51. printf("Global Output File Cleared from Memory!\n");
  52. }
  53. if(data->timescaleOutput!=NULL){
  54. fclose(data->timescaleOutput);
  55. printf("Characteristic Timescale Output File Cleared from Memory!\n");
  56. }
  57. //if(data->rxnROPOutput!=NULL){
  58. // fclose(data->rxnROPOutput);
  59. // printf("Reactions Rate of Progress Output File Cleared from Memory!\n");
  60. //}
  61. //if(data->spROPOutput!=NULL){
  62. // fclose(data->spROPOutput);
  63. // printf("Species Rate of Production Output File Cleared from Memory!\n");
  64. //}
  65. }
  66. free(data); /* Free the user data */
  67. printf("\n\n");
  68. }
  69. UserData allocateUserData(FILE *input){
  70. UserData data;
  71. data = (UserData) malloc(sizeof *data);
  72. if(!data){
  73. printf("Allocation Failed!\n");
  74. return(NULL);
  75. }
  76. setSaneDefaults(data);
  77. int ier;
  78. ier=parseNumber<size_t>(input, "basePts" , MAXBUFLEN, &data->npts);
  79. if(ier==-1 || data->npts<=0){
  80. printf("Enter non-zero basePts!\n");
  81. return(NULL);
  82. }
  83. ier=parseNumber<double>(input, "domainLength", MAXBUFLEN, &data->domainLength);
  84. if(ier==-1 || data->domainLength<=0.0e0){
  85. printf("domainLength error!\n");
  86. return(NULL);
  87. }
  88. ier=parseNumber<double>(input, "Rd", MAXBUFLEN, &data->Rd);
  89. if(ier==-1 || data->Rd<=0.0e0){
  90. printf("Rd error!\n");
  91. return(NULL);
  92. }
  93. ier=parseNumber<int>(input, "constantPressure" , MAXBUFLEN, &data->constantPressure);
  94. if(ier==-1 || (data->constantPressure!=0 && data->constantPressure!=1)){
  95. printf("constantPressure error!\n");
  96. return(NULL);
  97. }
  98. ier=parseNumber<int>(input, "problemType" , MAXBUFLEN, &data->problemType);
  99. if(ier==-1 || (data->problemType!=0
  100. && data->problemType!=1
  101. && data->problemType!=2
  102. && data->problemType!=3)){
  103. printf("include valid problemType!\n");
  104. printf("0: premixed combustion with NO equilibrated ignition kernel\n");
  105. printf("1: premixed combustion WITH equilibrated ignition kernel\n");
  106. printf("2: arbitrary initial conditions\n");
  107. printf("3: Restart\n");
  108. return(NULL);
  109. }
  110. ier=parseNumber<int>(input, "quasiSteady" , MAXBUFLEN, &data->quasiSteady);
  111. if(ier==-1 || (data->quasiSteady!=0
  112. && data->quasiSteady!=1)){
  113. printf("include valid quasiSteady!\n");
  114. printf("0: The droplet surface recedes and the droplet losses mass\n");
  115. printf("1: The droplet surface does not move and the droplet mass is constant\n");
  116. return(NULL);
  117. }
  118. ier=parseNumber<double>(input, "dPdt" , MAXBUFLEN, &data->dPdt);
  119. ier=parseNumber<double>(input, "Rg" , MAXBUFLEN, &data->Rg);
  120. if(data->Rg < 0.0){
  121. printf("Rg must be greater than 0");
  122. return(NULL);
  123. }
  124. ier=parseNumber<int> (input, "reflectProblem" , MAXBUFLEN, &data->reflectProblem);
  125. if(data->reflectProblem!=0 && data->reflectProblem!=1){
  126. printf("Invalid entry for reflectProblem! Can be only 1 or 0.\n");
  127. return(NULL);
  128. }
  129. ier=parseNumber<double>(input, "mdot" , MAXBUFLEN, &data->mdot);
  130. ier=parseNumber<double>(input, "initialTemperature", MAXBUFLEN,
  131. &data->initialTemperature);
  132. if(ier==-1 || data->initialTemperature<=0.0e0){
  133. printf("Enter positive initialTemperature in K!\n");
  134. return(NULL);
  135. }
  136. ier=parseNumber<double>(input, "initialPressure", MAXBUFLEN,
  137. &data->initialPressure);
  138. if(ier==-1 || data->initialTemperature<=0.0e0){
  139. printf("Enter positive initialPressure in atm!\n");
  140. return(NULL);
  141. }
  142. ier=parseNumber<int> (input, "metric" , MAXBUFLEN, &data->metric);
  143. if(data->metric!=0 && data->metric!=1 && data->metric!=2){
  144. printf("Invalid entry for metric!\n");
  145. printf("0: Cartesian\n");
  146. printf("1: Cylindrical\n");
  147. printf("2: Spherical\n");
  148. return(NULL);
  149. }
  150. ier=parseNumber<double>(input, "QDot", MAXBUFLEN, &data->maxQDot);
  151. if(ier==-1 && data->problemType==0){
  152. printf("Need to specify QDot for problemType 0!\n");
  153. return(NULL);
  154. }
  155. ier=parseNumber<double>(input, "kernelSize", MAXBUFLEN, &data->kernelSize);
  156. if(ier==-1 && data->problemType==0){
  157. printf("Need to specify kernelSize for problemType 0!\n");
  158. return(NULL);
  159. }
  160. ier=parseNumber<double>(input, "ignTime", MAXBUFLEN, &data->ignTime);
  161. if(ier==-1 && data->problemType==0){
  162. printf("Need to specify ignTime for problemType 0!\n");
  163. return(NULL);
  164. }
  165. ier=parseNumber<double>(input, "mixingWidth", MAXBUFLEN,
  166. &data->mixingWidth);
  167. if(ier==-1){
  168. printf("Need to specify mixingWidth!\n");
  169. return(NULL);
  170. }
  171. ier=parseNumber<double>(input, "shift", MAXBUFLEN, &data->shift);
  172. ier=parseNumber<double>(input, "firstRadius", MAXBUFLEN, &data->firstRadius);
  173. ier=parseNumber<double>(input, "wallTemperature", MAXBUFLEN, &data->wallTemperature);
  174. ier=parseNumber<int> (input, "dirichletInner" , MAXBUFLEN,
  175. &data->dirichletInner);
  176. if(data->dirichletInner!=0 && data->dirichletInner!=1){
  177. printf("dirichletInner can either be 0 or 1!\n");
  178. return(NULL);
  179. }
  180. ier=parseNumber<int> (input, "dirichletOuter" , MAXBUFLEN,
  181. &data->dirichletOuter);
  182. if(data->dirichletOuter!=0 && data->dirichletOuter!=1){
  183. printf("dirichletOuter can either be 0 or 1!\n");
  184. return(NULL);
  185. }
  186. ier=parseNumber<int> (input, "adaptiveGrid" , MAXBUFLEN,
  187. &data->adaptiveGrid);
  188. if(ier==-1 || (data->adaptiveGrid!=0 && data->adaptiveGrid!=1)){
  189. printf("specify adaptiveGrid as 0 or 1!\n");
  190. return(NULL);
  191. }
  192. ier=parseNumber<int> (input, "moveGrid" , MAXBUFLEN,
  193. &data->moveGrid);
  194. if(ier==-1 || (data->moveGrid!=0 && data->moveGrid!=1)){
  195. printf("specify moveGrid as 0 or 1!\n");
  196. return(NULL);
  197. }
  198. ier=parseNumber<double> (input, "isotherm" , MAXBUFLEN,
  199. &data->isotherm);
  200. if(ier==-1){
  201. printf("specify temperature of isotherm!\n");
  202. return(NULL);
  203. }
  204. ier=parseNumber<double>(input, "gridOffset", MAXBUFLEN, &data->gridOffset);
  205. ier=parseNumber<int> (input, "nSaves" , MAXBUFLEN, &data->nSaves);
  206. if(data->nSaves<0 ){
  207. printf("nSaves must be greater than 0!\n");
  208. return(NULL);
  209. }
  210. ier=parseNumber<int> (input, "writeRates" , MAXBUFLEN,
  211. &data->writeRates);
  212. if(data->writeRates!=0 && data->writeRates!=1){
  213. printf("writeRates must either be 0 or 1!\n");
  214. return(NULL);
  215. }
  216. ier=parseNumber<int> (input, "writeEveryRegrid", MAXBUFLEN,
  217. &data->writeEveryRegrid);
  218. if(data->writeEveryRegrid!=0 && data->writeEveryRegrid!=1){
  219. printf("writeEveryRegrid must either be 0 or 1!\n");
  220. return(NULL);
  221. }
  222. ier=parseNumber<int> (input, "setConstraints" , MAXBUFLEN,
  223. &data->setConstraints);
  224. if(data->setConstraints!=0 && data->setConstraints!=1){
  225. printf("setConstraints must either be 0 or 1!\n");
  226. return(NULL);
  227. }
  228. ier=parseNumber<int> (input, "suppressAlg" , MAXBUFLEN,
  229. &data->suppressAlg);
  230. if(data->suppressAlg!=0 && data->suppressAlg!=1){
  231. printf("suppressAlg must either be 0 or 1!\n");
  232. return(NULL);
  233. }
  234. ier=parseNumber<int> (input, "dryRun" , MAXBUFLEN,
  235. &data->dryRun);
  236. if(data->dryRun!=0 && data->dryRun!=1){
  237. printf("dryRun must either be 0 or 1!\n");
  238. return(NULL);
  239. }
  240. ier=parseNumber<double> (input, "finalTime" , MAXBUFLEN,
  241. &data->finalTime);
  242. ier=parseNumber<double> (input, "relativeTolerance" , MAXBUFLEN,
  243. &data->relativeTolerance);
  244. ier=parseNumber<double> (input, "radiusTolerance" , MAXBUFLEN,
  245. &data->radiusTolerance);
  246. ier=parseNumber<double> (input, "temperatureTolerance", MAXBUFLEN,
  247. &data->temperatureTolerance);
  248. ier=parseNumber<double> (input, "pressureTolerance", MAXBUFLEN,
  249. &data->pressureTolerance);
  250. ier=parseNumber<double> (input, "massFractionTolerance", MAXBUFLEN,
  251. &data->massFractionTolerance);
  252. ier=parseNumber<double> (input, "bathGasTolerance", MAXBUFLEN,
  253. &data->bathGasTolerance);
  254. ier=parseNumber<double> (input, "MdotTolerance", MAXBUFLEN,
  255. &data->MdotTolerance);
  256. char chem[MAXBUFLEN],mix[MAXBUFLEN],tran[MAXBUFLEN];
  257. ier=parseNumber<char>(input, "chemistryFile" , MAXBUFLEN, chem);
  258. if(ier==-1){
  259. printf("Enter chemistryFile!\n");
  260. return(NULL);
  261. }else{
  262. try{
  263. data->gas = new Cantera::IdealGasMix(chem);
  264. data->nsp=data->gas->nSpecies(); //assign no: of species
  265. } catch (Cantera::CanteraError& err) {
  266. printf("Error:\n");
  267. printf("%s\n",err.what());
  268. return(NULL);
  269. }
  270. }
  271. ier=parseNumber<char>(input, "transportModel", MAXBUFLEN, tran);
  272. if(ier==-1){
  273. printf("Enter transportModel!\n");
  274. return(NULL);
  275. }else{
  276. try{
  277. data->trmix = Cantera::newTransportMgr(tran, data->gas);
  278. }catch (Cantera::CanteraError& err) {
  279. printf("Error:\n");
  280. printf("%s\n",err.what());
  281. return(NULL);
  282. }
  283. }
  284. ier=parseNumber<char>(input, "mixtureComposition", MAXBUFLEN, mix);
  285. if(ier==-1){
  286. printf("Enter mixtureComposition!\n");
  287. return(NULL);
  288. }else{
  289. if(data->gas!=NULL){
  290. try{
  291. data->gas->setState_TPX(data->initialTemperature,
  292. data->initialPressure*Cantera::OneAtm,
  293. mix);
  294. }catch (Cantera::CanteraError& err) {
  295. printf("Error:\n");
  296. printf("%s\n",err.what());
  297. return(NULL);
  298. }
  299. }
  300. }
  301. //ier=parseNumber<char>(input, "dropletComposition", MAXBUFLEN, data->dropSpec);
  302. //if(ier==-1){
  303. // printf("Enter composition of droplet!\n");
  304. // return(NULL);
  305. //}
  306. ier=parseDrop(input,"dropletComposition",data->dropSpec,data->dropMole,MAXBUFLEN);
  307. ier=parseDrop(input,"dropletDensity",data->dropSpec,data->dropDens,MAXBUFLEN);
  308. ier=parseNumber<int> (input, "nThreads", MAXBUFLEN, &data->nThreads);
  309. if(data->nThreads<0 ){
  310. printf("nThreads must be greater than 0!\n");
  311. return(NULL);
  312. }
  313. ier=parseNumber<double>(input, "PCAD", MAXBUFLEN, &data->PCAD);
  314. ier=parseNumber<double>(input,"RGTC", MAXBUFLEN, &data->RGTC);
  315. ier=parseNumber<int>(input,"JJRG", MAXBUFLEN, &data->JJRG);
  316. ier=parseNumber<double>(input,"deltaT", MAXBUFLEN, &data->deltaT);
  317. data->nr=0;
  318. //data->np=data->nr+1;
  319. data->nt=data->nr+1;
  320. data->ny=data->nt+1;
  321. data->np=data->ny+data->nsp;
  322. data->nm=data->np+1;
  323. data->nvar=data->nsp+4; //assign no: of variables (R,T,P,Mdot,nsp species)
  324. double MW[2];
  325. for(int i=0;i<=1;i++){
  326. int speciesIndex = data->gas->speciesIndex(data->dropSpec[i]);
  327. double weight = data->gas->molecularWeight(speciesIndex);
  328. MW[i]= weight;
  329. }
  330. double massSum = 0.0;
  331. for(int i=0;i<=1;i++){
  332. massSum = massSum + data->dropMole[i] * MW[i];
  333. }
  334. data->dropRho = 0.0;
  335. for(int i=0;i<=1;i++){
  336. data->dropMassFrac[i] = data->dropMole[i]*MW[i]/massSum;
  337. data->dropRho = data->dropRho + data->dropMassFrac[i]*data->dropDens[i];
  338. }
  339. printf("Density of droplet is: %.3f [kg/m^3]\n",data->dropRho);
  340. //Mass of droplet
  341. //data->massDrop=1.0/3.0*pow(data->Rd,3)*997.0; //TODO: The density of the droplet should be a user input
  342. //data->massDrop=1.0/3.0*pow(data->Rd,3)*684.00; //TODO: The density of the droplet(n-heptane) should be a user input
  343. data->massDrop=1.0/3.0*pow(data->Rd,3)*data->dropRho;
  344. if(!data->adaptiveGrid){
  345. data->uniformGrid = new double [data->npts];
  346. data->neq=data->nvar*data->npts;
  347. }
  348. else{
  349. data->grid=(UserGrid) malloc(sizeof *data->grid);
  350. ier=getGridSettings(input,data->grid);
  351. if(ier==-1)return(NULL);
  352. ier=initializeGrid(data->grid);
  353. if(ier==-1)return(NULL);
  354. ier=reGrid(data->grid, data->grid->position);
  355. if(ier==-1)return(NULL);
  356. // /**************** TEST THE data->grid->xOld *******************/
  357. // double* ptr = data->grid->xOld ;
  358. // printf("allocateUserData function is called,Start print the first 5 elements of the xOld array : \n");
  359. // printf("1st:%.6f, 2nd:%.6f, 3rd:%.6f, 4th:%.6f, 5th:%.6f.\n",ptr[0],ptr[1],ptr[2],ptr[3],ptr[4]);
  360. data->npts=data->grid->nPts;
  361. data->neq=data->nvar*data->npts;
  362. }
  363. data->output=fopen("output.dat","w");
  364. data->globalOutput=fopen("globalOutput.dat","w");
  365. data->gridOutput=fopen("grid.dat","w");
  366. data->timescaleOutput=fopen("timeScale.dat","w") ;
  367. data->rxnROPOutput=fopen("rxnROP.dat","w");
  368. data->spROPOutput=fopen("spROP.dat","w");
  369. //data->ratesOutput=fopen("rates.dat","w");
  370. data->innerMassFractions = new double [data->nsp];
  371. //data->wdot_mole = new double [data->nsp] ;
  372. //data->wdot_mass = new double [data->nsp] ;
  373. data->time_scale = new double [(data->npts) * (data->nsp)] ;
  374. //data->MW = new double [data->nsp] ;
  375. return(data);
  376. }
  377. void setSaneDefaults(UserData data){
  378. data->domainLength=1.0e-02;
  379. data->Rd=100.0e-06;
  380. data->constantPressure=1;
  381. data->problemType=1;
  382. data->quasiSteady=1;
  383. data->dPdt=0.0e0;
  384. data->reflectProblem=0;
  385. data->mdot=0.0;
  386. data->initialTemperature=300.0;
  387. data->initialPressure=1.0;
  388. data->metric=0;
  389. data->ignTime=1e-09;
  390. data->maxQDot=0.0e0;
  391. data->maxTemperature=300.0e0;
  392. data->mixingWidth=1e-04;
  393. data->shift=3.0e-03;
  394. data->firstRadius=1e-04;
  395. data->wallTemperature=298.0e0;
  396. data->dirichletInner=0;
  397. data->dirichletOuter=0;
  398. data->adaptiveGrid=0;
  399. data->moveGrid=0;
  400. data->gridOffset=0.0e0;
  401. data->Rg=1.0;
  402. data->isotherm=1000.0;
  403. data->nSaves=30;
  404. data->writeRates=0;
  405. data->writeEveryRegrid=0;
  406. data->relativeTolerance=1e-06;
  407. data->radiusTolerance=1e-08;
  408. data->temperatureTolerance=1e-06;
  409. data->pressureTolerance=1e-06;
  410. data->massFractionTolerance=1e-09;
  411. data->bathGasTolerance=1e-06;
  412. data->finalTime=1e-02;
  413. data->tNow=0.0e0;
  414. data->setConstraints=0;
  415. data->suppressAlg=1;
  416. data->regrid=0;
  417. data->gridDirection=1;
  418. data->dryRun=0;
  419. data->nThreads=1;
  420. data->flamePosition[0]=0.0e0;
  421. data->flamePosition[1]=0.0e0;
  422. data->flameTime[0]=0.0e0;
  423. data->flameTime[1]=0.0e0;
  424. data->nTimeSteps=0;
  425. data->PCAD=0.75;
  426. data->RGTC=1.0;
  427. data->JJRG=0;
  428. data->deltaT=200.0;
  429. }