Flow123d
btc.cc
Go to the documentation of this file.
1 /*!
2  *
3  * Copyright (C) 2007 Technical University of Liberec. All rights reserved.
4  *
5  * Please make a following refer to Flow123d on your project site if you use the program for any purpose,
6  * especially for academic research:
7  * Flow123d, Research Centre: Advanced Remedial Technologies, Technical University of Liberec, Czech Republic
8  *
9  * This program is free software; you can redistribute it and/or modify it under the terms
10  * of the GNU General Public License version 3 as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14  * See the GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along with this program; if not,
17  * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 021110-1307, USA.
18  *
19  *
20  * $Id$
21  * $Revision$
22  * $LastChangedBy$
23  * $LastChangedDate$
24  *
25  * @file
26  * @ingroup io
27  *
28  * @brief Break through curve.
29  * @todo Rewrite to be applyable to general fields and produce specified output. documentation!!!
30  *
31  *
32  */
33 
34 #include "transport/transport.h"
35 
36 #include "io/read_ini.h"
37 #include "xio.h"
38 #include "output.h"
39 #include "btc.h"
40 #include "system/system.hh"
41 #include "mesh/mesh.h"
42 
43 static int *BTC_elm_list( int n_btc, char *line );
44 static int count_BTC_elms( char *line );
45 static void btc_init(struct BTC *btc);
46 
47 //=============================================================================
48 
49 //=============================================================================
50 void btc_init(struct BTC *btc){
51 
52  char *Btc;
53 
54  Btc = IONameHandler::get_instance()->get_output_file_name(OptGetStr( "Output", "BTC_elms", "-9999" )).c_str();
55  btc->n_BTC_elms = count_BTC_elms( Btc );
56  btc->BTC_elm = BTC_elm_list( btc->n_BTC_elms, Btc );
57 
58 }
59 
60 /*
61 //=============================================================================
62 
63 //=============================================================================
64 void btc_check(struct Transport *transport) {
65  Mesh* mesh = (Mesh*) ConstantDB::getInstance()->getObject(MESH::MAIN_INSTANCE);
66 
67  struct BTC *btc;
68  int i;
69 
70  transport->btc = (struct BTC*) xmalloc(sizeof(struct BTC));
71  btc = transport->btc;
72 
73  btc_init(btc);
74 
75  // TODO: remove find_id
76  if (btc->BTC_elm != NULL) {
77  if (btc->BTC_elm[0] != -9999) {
78  for (i = 0; i < btc->n_BTC_elms; i++) {
79  INPUT_CHECK( NONULL(mesh->element.find_id(btc->BTC_elm[i])),
80  "Unknown element #%d in BTC.\n", btc->BTC_elm[i]);
81  }
82  output_transport_init_BTC(transport);
83  output_transport_time_BTC(transport, 0.0);
84  } else {
85  xfree(transport->btc);
86  transport->btc = NULL;
87  }
88  }
89 }*/
90 //=============================================================================
91 
92 //=============================================================================
93 int count_BTC_elms( char *line )
94 {
95  char *l;
96  int rc;
97  ASSERT(!( line == NULL ),"NULL as an argument of function count_BTC_elms()\n");
98  l = xstrcpy( line );
99  rc = 0;
100  if( strtok( l, " \t,;" ) == NULL )
101  return rc;
102  do
103  rc++;
104  while( strtok( NULL, " \t,;" ) != NULL );
105  return rc;
106 }
107 //=============================================================================
108 //
109 //=============================================================================
110 int *BTC_elm_list( int n_btc, char *line )
111 {
112  int *cp;
113  int i;
114 
115  ASSERT(!( line == NULL ),"NULL as an argument of function BTC_elm_list()\n");
116  ASSERT(!( n_btc < 0 ),"Number of BTC cannot be negative in BTC_elm_list()\n");
117  if( n_btc == 0 )
118  return NULL;
119  cp = (int*) xmalloc( n_btc * sizeof( int ) );
120  for( i = 0; i < n_btc; i++ )
121  cp[ i ] = atoi( strtok( i == 0 ? line : NULL , " \t,;" ) );
122  return cp;
123 }
124 
125 //==============================================================================
126 // OPEN TEMP FILES
127 //==============================================================================
128 static FILE **open_temp_files(struct Transport *transport,const char *fileext,const char *open_param)
129 {
130  FILE **out=NULL;
131  char filename0[255],filename1[255],filename2[255],filename3[255];
132  int n = 4; //max output files
133  int sub;
134 
135  out = (FILE**)xmalloc(n*sizeof(FILE*));
136 
137  sub = transport->transport_sub_problem;
138 
139  sprintf(filename0, fileext, transport->transport_out_fname);
140  out[0] = xfopen(filename0, open_param);
141  out[1] = NULL;
142  out[2] = NULL;
143  out[3] = NULL;
144 
145  if( ((sub & 1) == 1) && (strcmp(transport->transport_out_im_fname,"NULL") != 0)) {
146  sprintf( filename1,fileext,transport->transport_out_im_fname);
147  out[1] = xfopen( filename1, open_param );
148  }
149 
150  if( ((sub & 2) == 2) && (strcmp(transport->transport_out_sorp_fname,"NULL") != 0)) {
151  sprintf(filename2,fileext,transport->transport_out_sorp_fname);
152  out[2] = xfopen( filename2, open_param );
153  }
154 
155  if( ((sub & 3) == 3) && (strcmp(transport->transport_out_im_sorp_fname,"NULL") != 0)) {
156  sprintf(filename3,fileext,transport->transport_out_im_sorp_fname);
157  out[3] = xfopen( filename3, open_param );
158  }
159 
160  return out;
161 }
162 
163 //==============================================================================
164 // INITIALIZE TRANSPORT OUTPUT FILE FOR BTC
165 //==============================================================================
166 void output_transport_init_BTC(struct Transport *transport)
167 {
168  FILE **out;
169  int i,iel,sbi;
170  out = open_temp_files(transport, "%s.btc", "wt" );
171  for(i=0; i < 4; i++)
172  if(out[i] == NULL)
173  continue;
174  else{
175  for(sbi=0;sbi<transport->n_subst_;sbi++)
176  for(iel=0;iel<transport->btc->n_BTC_elms;iel++)
177  xfprintf(out[i],"\t%d",transport->btc->BTC_elm[iel]);
178  xfprintf(out[i],"\n");
179  xfclose(out[i]);
180  }
181  xfree(out);
182  }
183 
184 /*
185 //==============================================================================
186 // TRANSPORT OUTPUT IN TIME (breakthrough curve in single element in all zones)
187 //==============================================================================
188 void output_transport_time_BTC(struct Transport *transport, double time)
189 {
190  FILE **out;
191 // ElementIter ele;
192 // TNode* nod;
193  Mesh* mesh = (Mesh*) ConstantDB::getInstance()->getObject(MESH::MAIN_INSTANCE);
194 
195  if(transport->btc == NULL)
196  return;
197 
198 
199  char dbl_fmt[ 16 ];
200  int sbi,iel,i;
201  int n_subst,el;
202  n_subst = transport->n_substances;
203  sprintf( dbl_fmt, "%%.%dg\t", ConstantDB::getInstance()->getInt("Out_digit"));
204  out = open_temp_files(transport, "%s.btc", "at" );
205  for(i=0; i < 4; i++){
206  if(out[i] == NULL) continue;
207  xfprintf( out[i], dbl_fmt, time);
208 
209  for(el=0;el < mesh->n_elements();el++){
210  for (iel=0;iel<transport->btc->n_BTC_elms;iel++)
211  if (mesh->epos_id[el] == transport->btc->BTC_elm[iel])
212  {
213  for( sbi = 0; sbi < n_subst; sbi++ )
214  switch(i)
215  {
216  case MOBILE:
217  xfprintf(out[0],dbl_fmt,transport->conc[sbi][MOBILE][el]);
218  break;
219  case IMMOBILE:
220  xfprintf(out[1],dbl_fmt,transport->conc[sbi][IMMOBILE][el]);
221  break;
222  case MOBILE_SORB:
223  xfprintf(out[2],dbl_fmt,transport->conc[sbi][MOBILE_SORB][el]);
224  break;
225  case IMMOBILE_SORB:
226  xfprintf(out[3],dbl_fmt,transport->conc[sbi][IMMOBILE_SORB][el]);
227  break;
228  }
229  }
230  }
231  xfprintf( out[i], "\n" );
232  xfclose( out[i] );
233  }
234  xfree(out);
235 }
236 */