00001 #include "flashdata.h"
00002 #include "numericmethods.h"
00003
00004
00005 double FlashData::getMoles(unsigned phase,unsigned comp) const
00006 {
00007 assert(phase<n_phases && comp <n_comps);
00008 assert(m_phaseCompositions);
00009 return m_phaseCompositions[phase*n_comps+comp];
00010 }
00011
00012 void FlashData::setMoles(unsigned phase,unsigned comp,const double &value)
00013 {
00014 assert(m_phaseCompositions);
00015 m_phaseCompositions[phase*n_comps+comp]=value;
00016
00017
00018 }
00019
00020
00021
00022 FlashData::FlashData(unsigned n_phases,unsigned n_comps,double *mem)
00023 :n_phases(n_phases),n_comps(n_comps)
00024 {
00025 m_phaseCompositions=mem;
00026 m_Del=false;
00027 }
00028
00029 FlashData::FlashData(unsigned n_phases,unsigned n_comps)
00030 :n_phases(n_phases),n_comps(n_comps)
00031 {
00032 m_Del=false;
00033 allocateOwnMemory();
00034 }
00035
00036
00037 FlashData::~FlashData()
00038 {
00039 if (m_Del)
00040 delete [] m_phaseCompositions;
00041 }
00042
00043
00044
00045 void FlashData::allocateOwnMemory()
00046 {
00047 if (!m_Del)
00048 {
00049 m_phaseCompositions=new double[n_phases*n_comps];
00050 m_Del=true;
00051
00052 }
00053 }
00054
00055
00056
00057
00058
00059
00060 double FlashData::getPhaseTotalMoles(unsigned phase) const
00061 {
00062 assert(m_phaseCompositions);
00063 assert(phase<n_phases);
00064 double acum=0;
00065 const double *dd=&(m_phaseCompositions[phase*n_comps]);
00066 for (unsigned i=0;i<n_comps;i++)
00067 {
00068 acum+=*(dd++);
00069 }
00070 return acum;
00071 }
00072
00073 void FlashData::getPhasesTotalMoles(VecDouble &phases) const
00074 {
00075 assert(phases.size() == n_phases);
00076 assert(m_phaseCompositions);
00077 double acum;
00078 const double *dd=m_phaseCompositions;
00079 for (unsigned i=0;i<n_phases;i++)
00080 {
00081 acum=0.0;
00082 for (unsigned j=0;j<n_comps;j++)
00083 {
00084 acum+=*(dd++);
00085 }
00086 phases(i)=acum;
00087 }
00088 }
00089
00090 void FlashData::getPhaseConcentrationsAndTotalMoles(Matrix &phasesC, VecDouble &phasesTM) const
00091 {
00092 assert(m_phaseCompositions);
00093 assert(phasesC.m() == n_phases && phasesC.n() == n_comps);
00094 assert(phasesTM.size() == n_phases);
00095 getPhasesTotalMoles(phasesTM);
00096 const double *dd=m_phaseCompositions;
00097 double *dr=&(phasesC(0,0));
00098
00099 for (unsigned i=0;i<n_phases;i++)
00100 {
00101 for (unsigned j=0;j<n_comps;j++)
00102 {
00103 *(dr++)=*(dd++)/phasesTM(i);
00104 }
00105 }
00106 }
00107
00108
00109
00110 double FlashData::getTotalMoles() const
00111 {
00112 assert(m_phaseCompositions);
00113 double acum=0.0;
00114 const double *dd=m_phaseCompositions;
00115 const unsigned nElems=n_phases*n_comps;
00116 for (unsigned i=0;i<nElems;i++)
00117 {
00118 acum+=*(dd++);
00119 }
00120 return acum;
00121 }
00122
00123
00131 void FlashData::multiply(VecDouble &B,const VecDouble &X) const
00132 {
00133 assert(m_phaseCompositions);
00134 assert(X.size() == n_comps);
00135 assert(B.size() == n_phases);
00136 double acum;
00137 const double *dd=m_phaseCompositions;
00138 for (unsigned i=0;i<n_phases;i++)
00139 {
00140 acum=0;
00141 for (unsigned j=0;j<n_comps;j++)
00142 {
00143 acum+=*(dd++)*X(j);
00144 }
00145 B(i)=acum;
00146 }
00147 }
00148
00149
00150
00151
00152 void FlashData::getMixtureComposition(VecDouble &vComp, double &nT)
00153 {
00154 assert(vComp.size() == n_comps);
00155 const double *dd = m_phaseCompositions;
00156 for (unsigned j=0;j<n_comps;j++)
00157 vComp(j)=*(dd++);
00158 for (unsigned i=1;i<n_phases;i++)
00159 {
00160 for (unsigned j=0;j<n_comps;j++)
00161 vComp(j)+=*(dd++);
00162 }
00163 nT=0.0;
00164 for (unsigned j=0;j<n_comps;j++)
00165 nT+=vComp(j);
00166 for (unsigned j=0;j<n_comps;j++)
00167 vComp(j)/=nT;
00168
00169 }
00170
00171
00172 const double* FlashData::getData() const
00173 {
00174 assert(m_phaseCompositions);
00175 return m_phaseCompositions;
00176 }
00177
00178
00179 void FlashData::setData(double *dd)
00180 {
00181 if (!m_Del)
00182 m_phaseCompositions=dd;
00183 else
00184 {
00185 m_Del=false;
00186 if (m_phaseCompositions)
00187 delete [] m_phaseCompositions;
00188 m_phaseCompositions=dd;
00189 }
00190 }
00191
00192
00193
00194 void FlashData::zeroEntries()
00195 {
00196 assert(m_phaseCompositions);
00197 double *dd = m_phaseCompositions;
00198 unsigned size=n_phases*n_comps;
00199 for (unsigned i=0;i<size;i++)
00200 *dd++=0.0;
00201 }
00202
00203
00204
00205 void FlashData::print(std::ostream &out)
00206 {
00207 assert(m_phaseCompositions);
00208 for (unsigned i=0;i<n_phases;i++)
00209 {
00210 out << "Phase " << i << ": ";
00211 for (unsigned j=0;j<n_comps;j++)
00212 out << getMoles(i,j) << "\t";
00213 out << std::endl;
00214 }
00215
00216 }
00217
00222 void FlashData::getTotalComponentsMoles(VecDouble &totalComponentsMoles) const
00223 {
00224 assert(totalComponentsMoles.size() == n_comps);
00225 assert(m_phaseCompositions);
00226 double *dd=m_phaseCompositions;
00227 for (unsigned j=0;j<n_comps;j++)
00228 totalComponentsMoles(j)=*dd++;
00229
00230 for (unsigned i=1;i<n_phases;i++)
00231 for (unsigned j=0;j<n_comps;j++)
00232 totalComponentsMoles(j)+=*dd++;
00233 }
00234
00235
00236
00237
00238 void FlashData::getPhasesDensities(VecDouble &vDensities,const VecDouble &componentsMolarMass,const VecDouble &phasesVol)
00239 {
00240 assert(vDensities.size() == getNPhases());
00241 assert(componentsMolarMass.size() == getNComponents());
00242 assert(phasesVol.size() == getNPhases());
00243
00244 multiply(vDensities,componentsMolarMass);
00245 NumericMethods::vectorDivideEntriesAvoidingNAN(vDensities,phasesVol);
00246
00247 }
00248
00249 void FlashData::setData(FlashData &data)
00250 {
00251 assert(data.m_phaseCompositions != NULL);
00252 assert(!m_Del);
00253 m_phaseCompositions=data.m_phaseCompositions;
00254 }
00255
00256
00257
00258 void FlashData::deepCopy (const FlashData &data)
00259 {
00260 assert(!m_Del);
00261 assert(n_phases==data.n_phases);
00262 assert(n_comps==data.n_comps);
00263 m_phaseCompositions=new double[n_phases*n_comps];
00264 m_Del=true;
00265 memcpy(m_phaseCompositions,data.m_phaseCompositions,n_phases*n_comps*sizeof(double));
00266 }
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277 void FlashData::operator = (const FlashData &data)
00278 {
00279 assert(m_phaseCompositions != NULL);
00280 assert(data.m_phaseCompositions != NULL);
00281 assert(n_phases == data.n_phases);
00282 assert(n_comps == data.n_comps);
00283 memcpy(m_phaseCompositions,data.m_phaseCompositions,n_phases*n_comps*sizeof(double));
00284 }
00285
00286 FlashData::FlashData(const FlashData &data)
00287 {
00288 assert(data.m_phaseCompositions != NULL);
00289 n_phases=data.n_phases;
00290 n_comps=data.n_comps;
00291 m_phaseCompositions=NULL;
00292 m_Del = false;
00293 allocateOwnMemory();
00294 memcpy(m_phaseCompositions,data.m_phaseCompositions,n_phases*n_comps*sizeof(double));
00295 }
00296
00297