Gems3k  3.1
GEMS3K standalone solver for geochemical equilibria
 All Classes Files Functions Variables Enumerations Enumerator
/Users/kulik/DevGEMS/trunk/standalone/GEMS3K/m_param.h
Go to the documentation of this file.
00001 //-------------------------------------------------------------------
00002 // $Id: m_param.h 725 2012-10-02 15:43:37Z kulik $
00003 //
00006 //
00007 // Copyright (c) 1995-2012 S.Dmytriyeva, D.Kulik
00008 // <GEMS Development Team, mailto:gems2.support@psi.ch>
00009 //
00010 // This file is part of the GEMS3K code for thermodynamic modelling
00011 // by Gibbs energy minimization <http://gems.web.psi.ch/GEMS3K/>
00012 //
00013 // GEMS3K is free software: you can redistribute it and/or modify
00014 // it under the terms of the GNU Lesser General Public License as
00015 // published by the Free Software Foundation, either version 3 of
00016 // the License, or (at your option) any later version.
00017 
00018 // GEMS3K is distributed in the hope that it will be useful,
00019 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00020 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00021 // GNU Lesser General Public License for more details.
00022 
00023 // You should have received a copy of the GNU General Public License
00024 // along with GEMS3K code. If not, see <http://www.gnu.org/licenses/>.
00025 //-------------------------------------------------------------------
00026 //
00027 
00028 #ifndef _m_param_h_
00029 #define _m_param_h_
00030 
00031 #include <cmath>
00032 
00033 // Physical constants - see m_param.cpp or ms_param.cpp
00034 extern const double R_CONSTANT, NA_CONSTANT, F_CONSTANT,
00035     e_CONSTANT,k_CONSTANT, cal_to_J, C_to_K, lg_to_ln, ln_to_lg, H2O_mol_to_kg, Min_phys_amount;
00036 
00037 #ifdef IPMGEMPLUGIN
00038 
00039 #include "gdatastream.h"
00040 #include "ms_multi.h"
00041 #include "verror.h"
00042 
00043 struct BASE_PARAM 
00044 {
00045    long int
00046            PC,   
00047            PD,   
00048 
00049 
00050            PRD,  
00051            PSM,  
00052            DP,   
00053            DW,   
00054            DT,   
00055            PLLG, 
00056            PE,   
00057            IIM   
00058            ;
00059          double DG,   
00060            DHB,  
00061            DS,   
00062            DK,   
00063            DF,   
00064            DFM,  
00065            DFYw, 
00066            DFYaq,
00067            DFYid,
00068            DFYr, 
00069            DFYh, 
00070            DFYc, 
00071            DFYs, 
00072            DB,   
00073            AG,   
00074            DGC,  
00075            GAR,  
00076            GAH,  
00077            GAS,  
00078 
00079            DNS,  
00080            XwMin,
00081            ScMin,
00082            DcMin,
00083            PhMin,
00084            ICmin,
00085            EPS,  
00086            IEPS, 
00087            DKIN; 
00088     char *tprn;       
00089 
00090     void write(fstream& oss);
00091 };
00092 
00093 struct SPP_SETTING 
00094 {
00095     char ver[TDBVERSION]; 
00096     BASE_PARAM p; //
00097     void write(fstream& oss);
00098 };
00099 
00100 extern SPP_SETTING pa_;
00101 
00103 class TProfil
00104 {
00105 
00106 public:
00107 
00108     //static TProfil* pm;
00109 
00110     TMulti* multi;
00111     MULTI *pmp;
00112     SPP_SETTING pa;
00113 
00114     TProfil( TMulti* amulti );
00115 
00116     const char* GetName() const
00117     {
00118         return "Project";
00119     }
00120 
00121    void outMulti( GemDataStream& ff, gstring& path  );
00122    void outMultiTxt( const char *path, bool append=false  );
00123    void readMulti( GemDataStream& ff );
00124    void readMulti( const char* path,  DATACH  *dCH );
00125 
00126    double ComputeEquilibriumState( long int& PrecLoops_, long int& NumIterFIA_, long int& NumIterIPM_ );
00127 
00128    inline double HelmholtzEnergy( double x )
00129    {
00130        return multi->HelmholtzEnergy(x);
00131    }
00132 
00133    inline double InternalEnergy( double TC, double P )
00134    {
00135        return multi->InternalEnergy( TC, P );
00136    }
00137 
00138    //void test_G0_V0_H0_Cp0_DD_arrays( long int nT, long int nP );
00139 };
00140 
00141 enum QpQdSizes {   // see m_phase.h
00142    QPSIZE = 180,    // This enum is for GEMS3K only!
00143    QDSIZE = 60
00144 };
00145 
00146 #else
00147 
00148 #include "v_mod.h"
00149 #include "ms_rmults.h"
00150 #include "ms_mtparm.h"
00151 #include "ms_system.h"
00152 #include "ms_multi.h"
00153 #include "ms_calc.h"
00154 
00155 class GemDataStream;
00156 class QWidget;
00157 
00158 extern long int showMss;
00159 
00160 struct BASE_PARAM
00161 { // Flags and thresholds for numeric modules
00162   short
00163     PC,   // Mode of PhaseSelect() operation ( 0 1 2 ... ) { 1 }
00164     PD,   // abs(PD): Mode of execution of CalculateActivityCoefficients() functions { 2 }
00165           // Mode of CalculateActivityCoefficients(): 0-invoke, 1-at EFD only, 2-every EFD it, every IPM it. 3-not EFD, every IPM it.
00166           // if PD < 0 then use test qd_real accuracy mode
00167     PRD,  // Since r1583/r409: Disable (0) or activate (-5 or less) the SpeciationCleanup() procedure { -5 }
00168     PSM,  // Level of diagnostic messages: 0- disabled (no ipmlog file); 1- errors; 2- also warnings 3- uDD trace { 1 }
00169     DP,   // Maximum allowed number of iterations in the MassBalanceRefinement() procedure {  30 }
00170     DW,   // Since r1583: Activate (1) or disable (0) error condition when DP was exceeded { 1 }
00171     DT,   // Since r1583/r409: DHB is relative for all (0) or absolute (-6 or less ) cutoff for major ICs { 0 }
00172     PLLG, // IPM tolerance for detecting divergence in dual solution { 10; range 1 to 1000; 0 disables the detection }
00173     PE,   // Flag for using electroneutrality condition in GEM IPM calculations { 0 1 }
00174     IIM   // Maximum allowed number of iterations in the MainIPM_Descent() procedure up to 9999 { 1000 }
00175     ;
00176   double DG,   // Standart total moles { 1e5 }
00177     DHB,  // Maximum allowed relative mass balance residual for Independent Components ( 1e-9 to 1e-15 ) { 1e-10 }
00178     DS,   // Cutoff minimum mole amount of stable Phase present in the IPM primal solution { 1e-12 }
00179     DK,   // IPM-2 convergence threshold for the Dikin criterion (may be set in the interval 1e-6 < DK < 1e-4) { 1e-5 }
00180     DF,   // Threshold for the application of the Karpov phase stability criterion: (Fa > DF) for a lost stable phase { 0.01 }
00181     DFM,  // Threshold for Karpov stability criterion f_a for insertion of a phase (Fa < -DFM) for a present unstable phase { 0.1 }
00182     DFYw, // Insertion mole amount for water-solvent { 1e-6 }
00183     DFYaq,// Insertion mole amount for aqueous species { 1e-6 }
00184     DFYid,// Insertion mole amount for ideal solution components { 1e-6 }
00185     DFYr, // Insertion mole amount for major solution components { 1e-6 }
00186     DFYh, // Insertion mole amount for minor solution components { 1e-6 }
00187     DFYc, // Insertion mole amount for single-component phase { 1e-6 }
00188     DFYs, // Insertion mole amount used in PhaseSelect() for a condensed phase component  { 1e-7 }
00189     DB,   // Minimum amount of Independent Component in the bulk system composition (except charge "Zz") (moles) (1e-17)
00190     AG,   // Smoothing parameter for non-ideal increments to primal chemical potentials between IPM descent iterations { -1 }
00191     DGC,  // Exponent in the sigmoidal smoothing function, or minimal smoothing factor in new functions { -0.99 }
00192     GAR,  // Initial activity coefficient value for major (M) species in a solution phase before LPP approximation { 1 }
00193     GAH,  // Initial activity coefficient value for minor (J) species in a solution phase before LPP approximation { 1000 }
00194     GAS,  // Since r1583: threshold for primal-dual chem.pot.difference (mol/mol) used in SpeciationCleanup() { 1e-3 }
00195           // before: Obsolete IPM-2 balance accuracy control ratio DHBM[i]/b[i], for minor ICs { 1e-3 }
00196     DNS,  // Standard surface density (nm-2) for calculating activity of surface species (12.05)
00197     XwMin,// Cutoff mole amount for elimination of water-solvent { 1e-9 }
00198     ScMin,// Cutoff mole amount for elimination of solid sorbent {1e-7}
00199     DcMin,// Cutoff mole amount for elimination of solution- or surface species { 1e-30 }
00200     PhMin,// Cutoff mole amount for elimination of  non-electrolyte solution phase with all its components { 1e-10 }
00201     ICmin,// Minimal effective ionic strength (molal), below which the activity coefficients for aqueous species are set to 1. { 3e-5 }
00202     EPS,  // Precision criterion of the SolveSimplex() procedure to obtain the AIA ( 1e-6 to 1e-14 ) { 1e-10 }
00203     IEPS, // Convergence parameter of SACT calculation in sorption/surface complexation models { 0.01 to 0.000001, default 0.001 }
00204     DKIN; // Tolerance on the amount of DC with two-side metastability constraints  { 1e-7 }
00205     char *tprn;       // internal
00206     void write(GemDataStream& oss);
00207     void read(GemDataStream& oss);
00208 };
00209 
00210 struct SPP_SETTING
00211 {   // Base Parametres of SP
00212     char ver[TDBVERSION]; // Version & Copyright 64
00213     BASE_PARAM p; // Flags and thresholds for numeric modules
00214     char           // default codes of values
00215     DCpct[7],      // Default DCOMP flags and codes
00216     DCpdc[10],     // Default DCOMP class and units
00217     BCpc[7],       // Default COMPOS configuration
00218     REpct[7],      // Default REACDC flags and codes
00219 
00220     REpdc[7],      // Default REACDC class and units
00221     REpvc[9],      // Default REACDC configuration
00222     RPpdc[11],      // Default RTPARM flags and codes
00223     RPpvc[33],     // Default RTPARM configuration  reserved
00224     PHsol_t[7],    // Default PHASE model codes
00225     PHpvc[7],      // Default PHASE configuration
00226     MUpmv[11],     // Default RMULTS configuration
00227     TPpdc[9],      // Default class and units for MTPARM
00228     TPpvc[21],     // Default MTPARM configuration
00229     SYppc[11],     // Default class and flags for SYSTEM
00230     SYpvc[29],     // Default SYSTEM confifuration
00231     UTppc[11],     // Default DUTERM class and flags
00232     PEpsc[13],     // Default PROCES class and flags
00233     PEpvc[13],     // Default PROCES configuration
00234     GDcode[2][20], // Default names of screen and graphs in GTDEMO ????
00235     GDpsc[7],      // Default names of lines on GTDEMO graphs
00236     GDpcc[2][9],   // Default axis names for GTDEMO
00237     GDptc[7],      // Default GTDEMO configuration
00238     GDpgw[7],      // Default setup of graphs in GTDEMO
00239     SDrefKey[32],  // sdref key
00240     Reserv[50-32]    // (reserved) objects later
00241     ;
00242     // for RTPARM
00243     short NP,NT,  // Default N of points (RTPARM): P, T
00244     NV,       // reserved
00245     Mode,     // Default indexation mode RTPARM
00246     ResShort[5];
00247     float        // RTPARM
00248     Pi[3],    // Default interval for pressure
00249     Ti[3],    // Default interval for temperature, C
00250     Vi[3],    // Default interval for volume, cm3
00251     DRpst, DRtcst,   // Default Pr, Tr for DCOMP & REACDC
00252     lowPosNum, // MULTI Cutoff moles of DC (Ls set) { 1e-19 };
00253     logXw,     // log(1e-16)
00254     logYFk,    // log(1e-9)
00255     aqPar[5];  // b_gamma, a0, NeutPolicy, GamH2O, b_gam_T_dep for auto aq phase model
00256 //    ResFloat;   // one parameter for auto gas/fluid phase
00257 
00258     void write(GemDataStream& oss);
00259     void read(GemDataStream& oss);
00260 };
00261 
00262 extern SPP_SETTING pa_;
00263 
00264 struct CompItem
00265 {
00266     int line;
00267     short delta; // RpnItemType
00268     CompItem(int nLine, short nDelta):
00269             line(nLine), delta(nDelta)
00270     {}
00271 
00272 };
00273 
00274 // Module TParam (RMULTS+MTPARM+SYSTEM+MULTY see SubModules)
00275 class TProfil : public TCModule
00276 {
00277     TRMults* rmults;
00278     TMTparm* mtparm;
00279     TSyst* syst;
00280     TMulti* multi;
00281 
00282     bool newRecord;
00283 //    int pll;
00284 //    double FXold;
00285 
00286     // to compare with old Project
00287     bool comp_change_all;
00288     char (*SFold)[PH_RKLEN];// List of PHASE definition keys [0:Fi-1]             DB
00289     char (*SMold)[DC_RKLEN];// List of DC definition keys (DCOMP, REACDC) [0:L-1] DB
00290     char (*SAold)[BC_RKLEN];// List of COMPOS definition keys [0:La-1]            DB
00291     char (*SBold)[IC_RKLEN];// List of ICOMP record keys (stoichiometry basis)[0:N-1] DB
00292     short *Llold;// L1 vector, shows a number of DC included to each phase [0:Fi-1] DB
00293     short Nold,     // N of IC, incl. Zz (charge) and Vol (volume)
00294     Lold,       // L   - of DC - total for all phases
00295     Fiold,      // FI  - total number of phases
00296     Fisold,     // FIs - total number of multi-component phases
00297     Laold,      // La  - of references to COMPOS records
00298     Lsold;      // Ls  - total number of DC in multi-component phases
00299 
00300     // data to load SysEq <project>:G:z_cp_config:0:0:1:25:0
00301     bool  isSysEq;
00302     TCIntArray DCon;
00303     TCIntArray PHon;
00304     TCIntArray DCoff;
00305     TCIntArray PHoff;
00306 
00307 protected:
00308 
00309     void InitFN( const char * prfName, const char* prfTemplate );
00310     void RenameFN( const char * prfName, const char* prfTemplate );
00311     bool GetFN( const char * prfName, bool show_dlg=true );
00312     void SetFN();
00313     bool rCopyFilterProfile( const char * prfName );
00314 
00315     void OpenProfileMode(
00316         const char* key, bool changeAqGas, bool addFile,  bool remakeRec );
00317     bool NewProfileMode( bool remakeRec, gstring& key_templ );
00318     bool NewProfileModeElements( bool remakeRec, gstring& key_templ );
00319     void CalcAllSystems(int makeDump);
00320     void SaveOldList();
00321     void DeleteOldList();
00322     void TestChangeProfile();
00323     void Push( TIArray<CompItem>& aList, int aLine,
00324                short aDelta, const char* dbKeywd, gstring aKey );
00325     void ICcompare( TIArray<CompItem>& aIComp);
00326     void COMPcompare( TIArray<CompItem>& aCompos);
00327     void DCcompare( TIArray<CompItem>& aList, int& i,int& j, int nI, int nJ);
00328     void PHcompare( TIArray<CompItem>& aPhase, TIArray<CompItem>& aDComp);
00329 
00330     // multi load
00331     short BAL_compare();
00332 
00333 public:
00334 
00335     static TProfil* pm;
00336 
00337    bool userCancel;
00338    bool stepWise;
00339    bool calcFinished;
00340    const char * status;
00341 
00342     bool useAqPhase;
00343     bool useGasPhase;
00344 
00345 // temporary !Use_mt_mode
00346     bool fStopCalc;
00347 
00348     //RMULTS* mup;
00349     //MTPARM *tpp;
00350     //SYSTEM *syp;
00351     //MULTI *pmp;
00352 //TMulti *pmulti;
00353 
00354     SPP_SETTING pa;
00355 
00356     TProfil( int nrt );
00357     void InitSubModules();
00358 
00359     const char* GetName() const
00360     {
00361         return "Project";
00362     }
00363 
00364     void ods_link(int i=0);
00365     void dyn_set(int i=0);
00366     void dyn_kill(int i=0);
00367     void dyn_new(int i=0);
00368     void set_def(int i=0);
00369     void DeleteRecord( const char *key, bool errinNo=true );
00370     void MakeQuery();
00371     const char* GetHtml();
00372 
00373     // Setup one of 5 default IPM numerical settings
00374     void ChangeSettings(int nSettings);
00375 
00376     // work with Project
00377     bool initCalcMode( const char * profileKey );
00378     void loadSystat( const char *key=0 );
00379     void newSystat( int mode );
00380     void deriveSystat();
00381     void PackSystat();
00382     double CalcEqstat( bool show_progress=true );
00383     int  indDC( int );
00384     int  indPH( int );
00385     void  deleteAutoGenerated();
00386     void  systbcInput( QWidget* par, const char * p_key );
00387     gstring PhNameforDC( int xdc, bool system );
00388     TCStringArray DCNamesforPh( const char *PhName, bool system );
00389 
00390     // Multi make functions
00391     void PMtest( const char *key );
00392     void CheckMtparam();
00393 
00394    void LoadFromMtparm( QWidget* par, DATACH *CSD, bool no_interpolat );
00395     void CalcBcc(); // Calc bulk composition
00396     void ShowPhaseWindow();
00397     void ShowEqPhaseWindow();
00398     void ShowDBWindow( const char *objName, int nLine=0 );
00399     void Clear_XeA_XeD_Phm_BIun();
00400 
00401     // Proces make functions
00402     void ET_translate( int nOet, int nOpex, int JB, int JE, int jb, int je,
00403      tget_ndx *get_ndx = 0 )
00404      { multi->ET_translate( nOet, nOpex, JB, JE, jb, je, get_ndx); }
00405     void getNamesList( int nO, TCStringArray& lst )
00406      { multi->getNamesList(nO, lst); }
00407     double MolWeight( int N, double *ICaw, double *Smline )
00408      {  return syst->MolWeight( N, ICaw, Smline ); }
00409     void SyTestSizes()
00410      {  syst->SyTestSizes(); }
00411 
00412     inline double HelmholtzEnergy( double x )
00413     {
00414         return multi->HelmholtzEnergy(x);
00415     }
00416     inline double InternalEnergy( double TC, double P )
00417     {
00418         return multi->InternalEnergy( TC, P );
00419     }
00420 
00421 
00422    //test
00423    void outMulti( GemDataStream& ff, gstring& path  );
00424    // brief_mode - Do not write data items that contain only default values
00425    // with_comments -Write files with comments for all data entries ( in text mode)
00426    // addMui - Print internal indices in RMULTS to IPM file for reading into Gems back
00427    void outMulti( gstring& path, bool addMui, bool with_comments = true, bool brief_mode = false );
00428    void makeGEM2MTFiles(QWidget* par);
00429    void outMultiTxt( const char *path, bool append=false  );
00430    void readMulti( GemDataStream& ff );
00431    void readMulti( const char* path,  DATACH  *dCH );
00432    void CmReadMulti( QWidget* par, const char* path );
00433    double ComputeEquilibriumState( long int& NumPrecLoops, long int& NumIterFIA, long int& NumIterIPM );
00434    //long int testMulti( );
00435    bool CompareProjectName( const char* SysKey );
00436    void ChangeTPinKey( double T, double P );
00437    void SetSysSwitchesFromMulti( );
00438 };
00439 
00440 /* Work codes of surface site types in pm->AtNdx vector (compatibility with old-style SCMs *
00441 enum SurTypeNdx {
00442     AT_SA0=0, AT_SB0=0, AT_SA1=1, AT_SB1=1, AT_SA2=2, AT_SB2=2, AT_SA3=3,
00443     AT_SB3=3, AT_SA4=4, AT_SB4=4, AT_SA5=5, AT_SB5=5,
00444     MSPN = 2   * Max number of EDL planes considered in old-style SCMs *
00445 }; */
00446 
00447 
00448 #endif // #ifndef IPMGEMPLUGIN
00449 
00450 
00451 // Work DC classifier codes  pm->DCCW
00452 enum SolDCodes {
00453 
00454     DC_SINGLE = 'U',        // This DC is a single-component phase
00455     DC_SYMMETRIC = 'I',     // This DC is in symmetric solution phase
00456     DC_ASYM_SPECIES = 'S',  // This is DC-solute(sorbate) in asymmetric phase
00457     DC_ASYM_CARRIER = 'W'   // This is carrier(solvent) DC in asymmetric phase
00458 };
00459 
00460 
00461 #endif  // _m_param_h
00462 
00463