cloudy  trunk
save_species.cpp
Go to the documentation of this file.
1 /* This file is part of Cloudy and is copyright (C)1978-2013 by Gary J. Ferland and
2  * others. For conditions of distribution and use see copyright notice in license.txt */
3 /*SaveSpecies generate output for the save species command */
4 #include "cddefines.h"
5 #include "opacity.h"
6 #include "taulines.h"
7 #include "radius.h"
8 #include "phycon.h"
9 #include "save.h"
10 #include "mole.h"
11 
12 /* save results for one particular species */
13 STATIC void SaveSpeciesOne( const size_t species_index, const char chKey[],
14  FILE *ioPUN, long int ipPun, size_t maxLevels );
15 
16 /*SaveSpecies generate output for the save species command */
18  FILE* ioPUN,
19  long int ipPun)
20 {
21  DEBUG_ENTRY( "SaveSpecies()" );
22 
23  if( strcmp( save.chSaveArgs[ipPun], "LABE" )==0 )
24  {
25  if( save.lgPunHeader[ipPun] )
26  {
27  /* save list of species labels */
28  fprintf( ioPUN, "#Species labels\n" );
29  save.lgPunHeader[ipPun] = false;
30  for( size_t i=0; i<mole_global.list.size(); ++i )
31  {
32  molecule *spg = &(*mole_global.list[i]);
33  fprintf( ioPUN, "%s\n", spg->label.c_str() );
34  }
35  }
36  return;
37  }
38 
39  else if( strcmp(save.chSaveArgs[ipPun],"LEVL") == 0 )
40  {
41  /* number of levels active in this zone */
42  if( save.lgPunHeader[ipPun] )
43  {
44  /* save list of species labels */
45  fprintf( ioPUN, "#Species\tnumber of levels\n" );
46  save.lgPunHeader[ipPun] = false;
47  }
48  for( size_t i=0; i<mole_global.list.size(); ++i )
49  {
50  molecule *spg = &(*mole_global.list[i]);
51  molezone *sp = &mole.species[i];
52  fprintf( ioPUN, "%s", spg->label.c_str() );
53  if( sp->levels == NULL )
54  fprintf( ioPUN, "\t%4lu\n", 0UL );
55  else
56  fprintf( ioPUN, "\t%4lu\n", (unsigned long)sp->levels->size() );
57  }
58  return;
59  }
60 
61  /* remaining options are column densities, populations, and energies
62  * first branch; save results for all species if "" */
63  if( strcmp( save.chSaveSpecies[ipPun], "" ) == 0 )
64  {
65  // max number of levels, for header print
66  size_t mostLevels = 0;
67  for( size_t i=0; i<mole_global.list.size(); ++i )
68  {
69  molezone *sp = &mole.species[i];
70  if( sp->levels != NULL )
71  mostLevels = MAX2(mostLevels, sp->levels->size() );
72  }
73  ASSERT( mostLevels > 1 );
74  ASSERT( mostLevels < 10000 );
75 
76  // loop over species
77  for( size_t i=0; i<mole_global.list.size(); ++i )
78  SaveSpeciesOne( i, save.chSaveArgs[ipPun], ioPUN, ipPun, mostLevels );
79  }
80  else
81  {
82  const molecule *saveSpeciesGlobal = findspecies(save.chSaveSpecies[ipPun]);
83  const molezone *saveSpecies = findspecieslocal(save.chSaveSpecies[ipPun]);
84 
85  if( saveSpecies == null_molezone )
86  {
87  fprintf( ioQQQ,"Could not find species %s, so SAVE SPECIES LABELS to get a list of all species."
88  "\nSorry.\n", save.chSaveSpecies[ipPun] );
90  }
91 
92  size_t numLevels = 0;
93  if( saveSpecies->levels != NULL )
94  numLevels = saveSpecies->levels->size();
95  SaveSpeciesOne( saveSpeciesGlobal->index, save.chSaveArgs[ipPun], ioPUN, ipPun, numLevels );
96  }
97 
98  return;
99 }
100 
101 /* print 0.000e+00 as simply 0 */
102 STATIC void PrintShortZero( FILE *ioPUN , double arg )
103 {
104  DEBUG_ENTRY( "PrintShortZero()" );
105  if( arg==0. )
106  fprintf(ioPUN,"\t0");
107  else
108  fprintf(ioPUN,"\t%.3e", arg);
109 
110 }
111 
112 /* save results for one particular species */
113 STATIC void SaveSpeciesOne( const size_t species_index, const char chKey[],
114  FILE *ioPUN, long int ipPun, size_t maxLevels )
115 {
116  DEBUG_ENTRY( "SaveSpeciesOne()" );
117 
118  molecule *spg = &(*mole_global.list[species_index]);
119  molezone *sp = &mole.species[species_index];
120 
121  if( spg == null_mole || sp == null_molezone )
122  return;
123 
124  // one time print of energy levels
125  if( strcmp( chKey, "ENER" )==0 )
126  {
127  if( save.lgPunHeader[ipPun] )
128  {
129  save.lgPunHeader[ipPun] = false;
130 
131  fprintf( ioPUN, "#species energies");
132  for( size_t i = 0; i < maxLevels; ++i )
133  {
134  fprintf( ioPUN, "\t%lu", (unsigned long)i );
135  }
136  fprintf( ioPUN, "\n");
137  }
138 
139  fprintf( ioPUN, "%s", spg->label.c_str() );
140  if( sp->levels == NULL || sp->levels->size() == 0 )
141  {
142  fprintf( ioPUN, "\t%.6e", 0. );
143  }
144  else
145  {
146  for( qList::const_iterator st = sp->levels->begin(); st != sp->levels->end(); ++st )
147  {
148  ASSERT( (*st).g() > 0.f );
149  fprintf( ioPUN, "\t%.6e", AnuUnit( (*st).energy().Ryd() ) );
150  }
151  }
152  fprintf( ioPUN, "\n");
153  return;
154  }
155 
156  if( strcmp( chKey, "POPU" )==0 )
157  {
158  if( save.lgPunHeader[ipPun] )
159  {
160  fprintf( ioPUN, "#depth [cm] species populations [cm-3]");
161 
162  for( size_t i = 0; i < maxLevels; ++i )
163  {
164  fprintf( ioPUN, "\t%lu", (unsigned long)i );
165  }
166  fprintf( ioPUN, "\n");
167  save.lgPunHeader[ipPun] = false;
168  }
169 
170  fprintf( ioPUN, "%.5e", radius.depth_mid_zone );
171  fprintf( ioPUN, "\t%s", spg->label.c_str() );
172 
173  if( sp->levels == NULL || sp->levels->size() == 0 )
174  {
175  PrintShortZero( ioPUN, sp->den );
176  }
177  else
178  {
179  bool lgZeroHit = false;
180  // loop over levels for this species
181  for( qList::const_iterator st = sp->levels->begin(); st != sp->levels->end(); ++st )
182  {
183  // don't print high levels which can have 0 abundance at low temperature
184  if( !lgZeroHit )
185  PrintShortZero( ioPUN, (*st).Pop() );
186  if( (*st).Pop() == 0.)
187  lgZeroHit = true;
188  }
189  }
190  fprintf( ioPUN, "\n");
191  }
192  else if( strcmp( chKey, "COLU" )==0 )
193  {
194 
195  if( save.lgPunHeader[ipPun] )
196  {
197  fprintf( ioPUN, "#species column density [cm-2]");
198 
199  for( size_t i = 0; i < maxLevels; ++i )
200  {
201  fprintf( ioPUN, "\t%lu", (unsigned long)i );
202  }
203  fprintf( ioPUN, "\n");
204  save.lgPunHeader[ipPun] = false;
205  }
206 
207  fprintf( ioPUN, "%s", spg->label.c_str() );
208 
209  if( sp->levels == NULL || sp->levels->size() == 0 )
210  {
211  PrintShortZero( ioPUN, sp->column );
212  }
213  else
214  {
215  // loop over levels
216  bool lgZeroHit = false;
217  for( qList::const_iterator st = sp->levels->begin(); st != sp->levels->end(); ++st )
218  {
219  // don't print high levels which can have 0 abundance at low temperature
220  if( !lgZeroHit )
221  PrintShortZero( ioPUN, (*st).ColDen() );
222  if( (*st).ColDen() == 0.)
223  lgZeroHit = true;
224  }
225  }
226  fprintf( ioPUN, "\n");
227  }
228 
229  return;
230 }
findspecieslocal
molezone * findspecieslocal(const char buf[])
Definition: mole_species.cpp:833
SaveSpeciesOne
STATIC void SaveSpeciesOne(const size_t species_index, const char chKey[], FILE *ioPUN, long int ipPun, size_t maxLevels)
Definition: save_species.cpp:113
ioQQQ
FILE * ioQQQ
Definition: cddefines.cpp:7
t_save::chSaveSpecies
char chSaveSpecies[LIMPUN][CHARS_SPECIES]
Definition: save.h:267
STATIC
#define STATIC
Definition: cddefines.h:97
mole.h
molecule::index
int index
Definition: mole.h:169
AnuUnit
double AnuUnit(realnum energy)
Definition: service.cpp:173
t_mole_global::list
MoleculeList list
Definition: mole.h:317
ProxyIterator
Definition: proxy_iterator.h:58
SaveSpecies
void SaveSpecies(FILE *ioPUN, long int ipPun)
Definition: save_species.cpp:17
ASSERT
#define ASSERT(exp)
Definition: cddefines.h:578
qList::end
iterator end()
Definition: quantumstate.h:345
null_molezone
molezone * null_molezone
Definition: mole_species.cpp:66
radius
t_radius radius
Definition: radius.cpp:5
EXIT_FAILURE
#define EXIT_FAILURE
Definition: cddefines.h:140
mole
t_mole_local mole
Definition: mole.cpp:7
molezone::levels
qList * levels
Definition: mole.h:354
cddefines.h
null_mole
molecule * null_mole
Definition: mole_species.cpp:64
molezone::column
realnum column
Definition: mole.h:359
radius.h
MAX2
#define MAX2
Definition: cddefines.h:782
cdEXIT
#define cdEXIT(FAIL)
Definition: cddefines.h:434
save.h
t_save::chSaveArgs
char chSaveArgs[LIMPUN][5]
Definition: save.h:265
qList::size
size_t size() const
Definition: quantumstate.h:116
molezone
Definition: mole.h:326
findspecies
molecule * findspecies(const char buf[])
Definition: mole_species.cpp:814
taulines.h
molecule
Definition: mole.h:132
t_mole_local::species
valarray< class molezone > species
Definition: mole.h:398
t_radius::depth_mid_zone
double depth_mid_zone
Definition: radius.h:41
phycon.h
molecule::label
string label
Definition: mole.h:142
opacity.h
PrintShortZero
STATIC void PrintShortZero(FILE *ioPUN, double arg)
Definition: save_species.cpp:102
t_save::lgPunHeader
bool lgPunHeader[LIMPUN]
Definition: save.h:246
molezone::den
double den
Definition: mole.h:358
mole_global
t_mole_global mole_global
Definition: mole.cpp:6
DEBUG_ENTRY
#define DEBUG_ENTRY(funcname)
Definition: cddefines.h:684
qList::begin
iterator begin()
Definition: quantumstate.h:337
save
t_save save
Definition: save.cpp:5