Gems3k
3.1
GEMS3K standalone solver for geochemical equilibria
|
00001 //------------------------------------------------------------------- 00002 // $Id: node.h 760 2012-11-27 16:33:44Z dmitrieva $ 00006 // 00011 // 00012 // Copyright (c) 2006-2012 S.Dmytriyeva, D.Kulik, G.Kosakowski, F.Hingerl 00013 // <GEMS Development Team, mailto:gems2.support@psi.ch> 00014 // 00015 // This file is part of the GEMS3K code for thermodynamic modelling 00016 // by Gibbs energy minimization <http://gems.web.psi.ch/GEMS3K/> 00017 // 00018 // GEMS3K is free software: you can redistribute it and/or modify 00019 // it under the terms of the GNU Lesser General Public License as 00020 // published by the Free Software Foundation, either version 3 of 00021 // the License, or (at your option) any later version. 00022 00023 // GEMS3K is distributed in the hope that it will be useful, 00024 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00025 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00026 // GNU Lesser General Public License for more details. 00027 00028 // You should have received a copy of the GNU General Public License 00029 // along with GEMS3K code. If not, see <http://www.gnu.org/licenses/>. 00030 //------------------------------------------------------------------- 00031 00055 00056 #ifndef _node_h_ 00057 #define _node_h_ 00058 00059 #include "m_param.h" 00060 #include "datach.h" 00061 #include "databr.h" 00062 00063 #ifndef IPMGEMPLUGIN 00064 class QWidget; 00065 #endif 00066 00067 extern const double bar_to_Pa, 00068 m3_to_cm3, 00069 kg_to_g; 00070 00075 class TNode 00076 { 00077 gstring dbr_file_name; 00078 00079 protected: 00080 MULTI* pmm; 00081 00082 #ifdef IPMGEMPLUGIN 00083 // These pointers are only used in standalone GEMS3K programs 00084 TMulti* multi; 00085 // TProfil* profil; 00086 #endif 00087 TProfil* profil; 00088 00089 00090 DATACH* CSD; 00091 DATABR* CNode; 00092 00093 00094 // These four values are set by the last GEM_run() call 00095 double CalcTime; 00096 long int 00097 PrecLoops, 00098 NumIterFIA, 00099 NumIterIPM; 00100 00101 void allocMemory(); 00102 void freeMemory(); 00103 00104 // Functions that maintain DATACH and DATABR memory allocation 00105 void datach_realloc(); 00106 void datach_free(); 00107 void datach_reset(); 00108 void databr_realloc(); 00109 void databr_reset( DATABR *CNode, long int level=0 ); 00110 00113 DATABR* databr_free( DATABR* data_BR_ =0); 00114 00115 // Binary i/o functions 00116 // including file i/o using GemDataStream class (with account for endianness) 00118 void datach_to_file( GemDataStream& ff ); 00120 void datach_from_file( GemDataStream& ff ); 00122 void databr_to_file( GemDataStream& ff ); 00124 void databr_from_file( GemDataStream& ff ); 00125 00126 // Text i/o functions 00130 void datach_to_text_file( fstream& ff, bool with_comments = true, bool brief_mode = false, const char* path = " " ); 00132 void datach_from_text_file( fstream& ff); 00136 void databr_to_text_file( fstream& ff, bool with_comments = true, bool brief_mode = false, const char* path = " " ); 00138 void databr_from_text_file(fstream& ff ); 00139 00140 void databr_element_to_vtk( fstream& ff, DATABR *CNode_, long int nfild, long int ndx ); 00141 void databr_name_to_vtk( fstream& ff, long int nfild, long int ndx, long int ndx2=0 ); 00142 void databr_size_to_vtk( long int nfild, long int& nel, long int& nel2 ); 00143 void databr_head_to_vtk( fstream& ff, const char*name, double time, long cycle, 00144 long nx = 1, long ny = 1, long nz = 1 ); 00145 00146 // virtual functions for interaction with TNodeArray class (not used at TNode level) 00147 virtual void InitNodeArray( const char *, long int *, bool , bool ) {} 00148 virtual void setNodeArray( long int , long int* ) { } 00149 //virtual void checkNodeArray( long int, long int*, const char* ) { } 00150 virtual long int nNodes() const // virtual call for interaction with TNodeArray class 00151 { return 1; } 00152 00153 #ifndef IPMGEMPLUGIN 00154 // Integration in GEMS-PSI GUI environment 00155 // Prepares and writes DCH and DBR files for reading into the coupled code 00156 void makeStartDataChBR( QWidget* par, bool no_interpolat, 00157 TCIntArray& selIC, TCIntArray& selDC, TCIntArray& selPH, 00158 long int nTp_, long int nPp_, double Ttol_, double Ptol_, 00159 double *Tai, double *Pai ); 00160 void makeStartDataChBR( QWidget* par, 00161 TCIntArray& selIC, TCIntArray& selDC, TCIntArray& selPH, 00162 double Tai[4], double Pai[4] ); 00163 00164 // Building internal dataCH and DataBR structures from Multi 00165 void setupDataChBR( TCIntArray& selIC, TCIntArray& selDC, TCIntArray& selPH, 00166 long int nTp_, long int nPp_, bool use_grid ); 00167 // Build lists names of components for selection into DataBridge 00168 void getDataBridgeNames( QWidget* par, bool select_all, 00169 TCIntArray& selIC, TCIntArray& selDC, TCIntArray& selPH ); 00170 00171 00172 // Virtual function for interaction with TNodeArray class 00173 virtual void setNodeArray( gstring& , long int , bool ) { } 00174 #endif 00175 00176 public: 00177 00178 //static TNode* na; // static pointer to this TNode class instance 00179 00180 #ifndef IPMGEMPLUGIN 00181 00182 TNode( MULTI *apm ); 00183 #else 00184 00185 TNode(); 00186 #endif 00187 00188 virtual ~TNode(); 00189 00190 // Typical sequence for using TNode class ---------------------------------- 00209 long int GEM_init( const char *ipmfiles_lst_name, const char *dbrfiles_lst_name = 0, 00210 long int *nodeTypes = 0, bool getNodT1 = false); 00211 00212 #ifdef IPMGEMPLUGIN 00213 // Calls for direct coupling of a FMT code with GEMS3K 00214 00218 void GEM_restore_MT( 00219 long int &p_NodeHandle, 00220 long int &p_NodeStatusCH, 00221 // GEM input output FMT control 00222 double &p_TK, 00223 double &p_P, 00224 double &p_Vs, 00225 double &p_Ms, 00226 double *p_bIC, 00227 double *p_dul, 00228 double *p_dll, 00229 double *p_aPH 00230 ); 00231 00234 void GEM_from_MT( 00235 long int p_NodeHandle, 00236 long int p_NodeStatusCH, 00237 // GEM input output FMT control 00238 double p_TK, 00239 double p_P, 00240 double p_Vs, 00241 double p_Ms, 00242 double *p_bIC, 00243 double *p_dul, 00244 double *p_dll, 00245 double *p_aPH 00246 ); 00247 00251 void GEM_from_MT( 00252 long int p_NodeHandle, 00253 long int p_NodeStatusCH, 00254 // GEM input output FMT control 00255 double p_TK, 00256 double p_P, 00257 double p_Vs, 00258 double p_Ms, 00259 double *p_bIC, 00260 double *p_dul, 00261 double *p_dll, 00262 double *p_aPH, 00263 double *p_xDC 00264 00265 00266 00267 ); 00268 00274 void GEM_from_MT( 00275 long int p_NodeHandle, 00276 long int p_NodeStatusCH, 00277 // GEM input output FMT control 00278 double p_TK, 00279 double p_P, 00280 double p_Vs, 00281 double p_Ms, 00282 double *p_bIC, 00283 double *p_dul, 00284 double *p_dll, 00285 double *p_aPH, 00286 double *p_xDC, 00287 double *p_gam 00288 ); 00289 00292 void GEM_set_MT( 00293 // long int NodeTypeHY, // Node type (hydraulic); see typedef NODETYPE 00294 // long int NodeTypeMT, // Node type (mass transport); see typedef NODETYPE 00295 double p_Tm, 00296 double p_dt 00297 ); 00298 #endif 00299 00305 long int GEM_read_dbr( const char* fname, bool binary_f=false ); 00306 00318 long int GEM_run( bool uPrimalSol ); // calls GEM for a work node 00319 00323 double GEM_CalcTime(); 00324 00332 long int GEM_Iterations( long int& PrecLoops, long int& NumIterFIA, long int& NumIterIPM ); 00333 00344 void GEM_write_dbr( const char* fname, bool binary_f=false, 00345 bool with_comments = true, bool brief_mode = false); 00346 00353 void GEM_print_ipm( const char* fname ); 00354 00355 #ifdef IPMGEMPLUGIN 00356 00357 00358 00359 void GEM_to_MT( 00360 long int &p_NodeHandle, 00361 long int &p_NodeStatusCH, 00362 long int &p_IterDone, 00363 // GEM input output FMT control 00364 // Chemical scalar variables 00365 double &p_Vs, 00366 double &p_Ms, 00367 double &p_Gs, 00368 double &p_Hs, 00369 double &p_IC, 00370 double &p_pH, 00371 double &p_pe, 00372 double &p_Eh, 00373 // Dynamic data - dimensions see in DATACH.H structure 00374 double *p_rMB, 00375 double *p_uIC, 00376 double *p_xDC, 00377 double *p_gam, 00378 double *p_xPH, 00379 double *p_vPS, 00380 double *p_mPS, 00381 double *p_bPS, 00382 double *p_xPA, 00383 double *p_aPH, 00384 double *p_bSP 00385 ); 00386 00387 #endif 00388 00389 // Access methods for direct or protected manipulation of CSD and DBR data 00390 // 00391 DATACH* pCSD() const 00392 { return CSD; } 00393 00394 DATABR* pCNode() const 00395 00396 { return CNode; } 00397 00398 // These methods get contents of fields in the work node structure 00399 double cTC() const 00400 { return CNode->TK-C_to_K; } 00401 00402 // These methods get contents of fields in the work node structure 00403 double cTK() const 00404 { return CNode->TK; } 00405 00406 double cP() const 00407 { return CNode->P; } 00408 00409 double cMs() const 00410 { return CNode->Ms; } 00411 00412 double cVs() const 00413 { return CNode->Vs; } 00414 00416 void setNodeHandle( long int jj ) 00417 { CNode->NodeHandle = jj; } 00418 00419 // Useful methods facilitating the communication between DataCH (or FMT) 00420 // and DataBR (or node) data structures for components and phases 00421 // (i.e. between the chemical system definition and the node) 00422 00425 long int IC_name_to_xCH( const char *Name ); 00426 00429 long int DC_name_to_xCH( const char *Name ); 00430 00433 long int Ph_name_to_xCH( const char *Name ); 00434 00437 inline long int IC_name_to_xDB( const char *Name ) 00438 { return IC_xCH_to_xDB( IC_name_to_xCH( Name ) ); } 00439 00442 inline long int DC_name_to_xDB( const char *Name ) 00443 { return DC_xCH_to_xDB( DC_name_to_xCH( Name ) ); } 00444 00447 inline long int Ph_name_to_xDB( const char *Name ) 00448 { return Ph_xCH_to_xDB( Ph_name_to_xCH( Name ) ); } 00449 00452 long int IC_xCH_to_xDB( const long int xCH ); 00453 00456 long int DC_xCH_to_xDB( const long int xCH ); 00457 00460 long int Ph_xCH_to_xDB( const long int xCH ); 00461 00463 inline long int IC_xDB_to_xCH( const long int xBR ) const 00464 { return CSD->xic[xBR]; } 00465 00467 inline long int DC_xDB_to_xCH( const long int xBR ) const 00468 { return CSD->xdc[xBR]; } 00469 00471 inline long int Ph_xDB_to_xCH( const long int xBR ) const 00472 { return CSD->xph[xBR]; } 00473 00475 long int Phx_to_DCx( const long int Phx ); 00476 00479 long int PhtoDC_DCH( const long int Phx, long int& nDCinPh ); 00480 00482 long int DCtoPh_DCH( const long int xCH ); 00483 00486 long int PhtoDC_DBR( const long int Phx, long int& nDCinPh ); 00487 00489 long int DCtoPh_DBR( const long int xBR ); 00490 00491 // Data exchange methods between GEMIPM and work node DATABR structure 00492 // Are called inside of GEM_run() 00493 void packDataBr(); 00494 void unpackDataBr( bool uPrimalSol ); 00495 00496 // Access to interpolated thermodynamic data from DCH structure 00499 bool check_TP( double T, double P ); 00500 00503 long int check_grid_T( double TK ); 00504 00507 long int check_grid_P( double P ); 00508 00512 long int check_grid_TP( double T, double P ); 00513 00515 inline long int gridTP() const 00516 { 00517 if( CSD->mLook == 1L ) 00518 return CSD->nTp; 00519 else 00520 return (CSD->nPp * CSD->nTp); 00521 } 00522 00523 00525 double get_Ppa_sat( double Tk ); 00526 00528 long int get_grid_index_Ppa_sat( double Tk ); 00529 00537 double DC_G0(const long int xCH, const double P, const double TK, bool norm=true); 00538 00545 double DC_V0(const long int xCH, const double P, const double TK); 00546 00553 double DC_H0(const long int xCH, const double P, const double TK); 00554 00561 double DC_S0(const long int xCH, const double P, const double TK); 00562 00569 double DC_Cp0(const long int xCH, const double P, const double TK); 00570 00577 double DC_A0(const long int xCH, const double P, const double TK); 00578 00585 double DC_U0(const long int xCH, const double P, const double TK); 00586 00592 void DensArrayH2Ow( const double P, const double TK, vector<double>& DensAW ); 00593 00599 void EpsArrayH2Ow( const double P, const double TK, vector<double>& EpsAW ); 00600 00601 00606 double EpsH2Ow(const double P, const double TK); 00607 00612 double DenH2Ow(const double P, const double TK); 00613 00618 double VisH2Ow(const double P, const double TK); 00619 00624 double EpsH2Og(const double P, const double TK); 00625 00630 double DenH2Og(const double P, const double TK); 00631 00636 double Ph_Volume( const long int xBR ); 00637 00642 double Ph_Mass( const long int xBR ); 00643 00648 double Ph_SatInd(const long int xph ); 00649 00657 double *Ph_BC( const long int xph, double* ARout=0 ); 00658 00663 double Get_mIC( const long xic ); 00664 00668 void Set_bIC( const long int xic, const double bIC) 00669 { CNode->bIC[xic] = bIC; } 00670 00673 inline double Get_bIC(const long int xic) const 00674 { return CNode->bIC[xic]; } 00675 00679 inline void Set_dll( const long int xdc, const double dll) 00680 { CNode->dll[xdc] = dll; } 00681 00685 inline void Set_dul( const long int xdc, const double dul) 00686 { CNode->dul[xdc] = dul; } 00687 00690 void Set_nDC( const long int xdc, const double nDC) 00691 { CNode->xDC[xdc] = nDC; } 00692 00695 inline double Get_nDC(const long int xdc) const 00696 { return CNode->xDC[xdc]; } 00697 00701 double Get_muDC( const long int xDC, bool norm=true ); 00702 00706 double Get_aDC( const long int xdc, bool scale=true ); 00707 00714 double Get_cDC( const long int xdc ); 00715 00720 inline double Get_gDC(const long int xdc) const 00721 { return ( CNode->gam[xdc] ? CNode->gam[xdc]: 1.); } 00722 00725 inline double DCmm( const long int xdc ) const 00726 { return CSD->DCmm[ CSD->xdc[xdc]]; } 00727 00730 inline double ICmm( const long int xic ) const 00731 { return CSD->ICmm[ CSD->xic[xic]]; } 00732 00736 inline double DCaJI( const long int xdc, const long int xic) const 00737 { return CSD->A[ CSD->xic[xic] + CSD->xdc[xdc] * CSD->nIC ]; } 00738 00739 // These methods can only be used for the current work node (direct access to GEM IPM data) 00740 00746 inline void Set_IC_b( const double b_val, const long int xCH) 00747 { pmm->B[xCH] = b_val; } 00748 00754 inline double IC_b(const long int xCH) const 00755 { return pmm->B[xCH]; } 00756 00762 inline double DC_n(const long int xCH) const 00763 { return pmm->X[xCH]; } 00764 00772 double DC_a(const long int xCH); 00773 00774 00777 void Get_IPc_IPx_DCc_indices( long &index_phase_aIPx, long &index_phase_aIPc, long &index_phase_aDCc, const long &index_phase); 00778 void Get_NPar_NPcoef_MaxOrd_NComp_NP_DC ( long &NPar, long &NPcoef, long &MaxOrd, long &NComp, long &NP_DC, const long &index_phase ); 00779 void Get_aIPc ( vector<double> &aIPc, const long &index_phase_aIPc, const long &index_phase ); 00780 void Get_aIPx ( vector<long> &aIPx, const long &index_phase_aIPx, const long &index_phase ); 00781 void Get_aDCc ( vector<double> &aDCc, const long &index_phase_aDCc, const long &index_phase ); 00782 void Set_aIPc ( const vector<double> aIPc, const long &index_phase_aIPc, const long &index_phase ); // Set values of aIPc array 00783 void Set_aDCc ( const vector<double> aDCc, const long &index_phase_aDCc, const long &index_phase ); // Set values of aDCc array 00785 void Set_Tk ( double &T_k); 00786 void Set_Pb ( double &P_b); 00787 00788 00796 double DC_c(const long int xCH); 00797 00803 inline double DC_g(const long int xCH) const 00804 { return pmm->Gamma[xCH]; } 00805 00806 00809 inline double DC_lng( const long int xCH ) const 00810 { return pmm->lnGam[ xCH ]; } 00811 00812 00819 double DC_mu(const long int xCH, bool norm=true); 00820 00828 double DC_mu0(const long int xCH, bool norm=true); 00829 00830 #ifndef IPMGEMPLUGIN 00831 // These calls are used only inside the GEMS-PSI GEM2MT module 00832 00836 void MakeNodeStructures( QWidget* par, bool select_all,bool no_interpolat, 00837 double *Tai, double *Pai, long int nTp_ = 1 , 00838 long int nPp_ = 1 , double Ttol_ = 1., double Ptol_ =1. ); 00843 void MakeNodeStructures( QWidget* par, bool select_all, 00844 double Tai[4], double Pai[4] ); 00845 00846 00850 void MakeNodeStructures( long int anICb, long int anDCb, long int anPHb, 00851 long int* axIC, long int* axDC, long int* axPH, bool no_interpolat, 00852 double* Tai, double* Pai, long int nTp_, 00853 long int nPp_, double Ttol_, double Ptol_ ); 00854 00856 bool TestTPGrid( double Tai[4], double Pai[4] ); 00857 00858 #endif 00859 00861 virtual void databr_to_vtk( fstream& ff, const char*name, double time, long int cycle, 00862 long int nFilds, long int (*Flds)[2]); 00863 00864 }; 00865 00866 // Redo into a function with interpolation 00867 // Diffusion coefficient of dependent component with node DBr index ICx 00868 // #define nodeCH_DD( DCx ) ( TNode::na->pCSD()->DD[ 00869 // TNode::na->pCSD()->xDC[(DCx)]] ) 00870 00871 00872 #endif 00873 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 00874 // _node_h_ 00875