cgma
MCNPInput.cpp File Reference
#include "MCNPInput.hpp"
#include "geometry.hpp"
#include "options.hpp"
#include <stdexcept>
#include <cassert>
#include <iostream>
#include <sstream>
#include <cstdlib>

Go to the source code of this file.

Classes

class  CardRef< T >
class  CellCardImpl
class  TransformCard
class  InputDeck::LineExtractor

Defines

#define IMPLICIT_INTERSECT()

Functions

static void strlower (std::string &str)
static int makeint (const std::string &token)
static double makedouble (const std::string &token)
static std::vector< double > makeTransformArgs (const token_list_t tokens)
static DataRef< Transform > * parseTransform (InputDeck &deck, const token_list_t tokens, bool degree_format=false)
static DataRef< Transform > * parseTransform (InputDeck &deck, token_list_t::iterator &i, bool degree_format=false)
static FillNode parseFillNode (InputDeck &deck, token_list_t::iterator &i, const token_list_t::iterator &end, bool degree_format=false)
static bool isblank (const std::string &line)
template<class T >
std::ostream & operator<< (std::ostream &out, const std::vector< T > &list)
std::ostream & operator<< (std::ostream &str, const CellCard::geom_list_entry_t &t)
void appendToTokenList (const std::string &token, token_list_t &tokens)
void tokenizeLine (std::string line, token_list_t &tokens, const char *extra_separators="")

Define Documentation

#define IMPLICIT_INTERSECT ( )
Value:
do{                                \
            if(geom.size()){                                    \
              geom_list_entry_t &t = geom.at(geom.size()-1);    \
              if( is_num_token(t) || t.first == RPAREN ){       \
                geom.push_back( make_geom_entry( INTERSECT ) ); \
              }}} while(0)

Function Documentation

void appendToTokenList ( const std::string &  token,
token_list_t tokens 
)

Append a single token to the given list of tokens. The token is assumed to be lowercase, free of comments, and non-blank. This function is for handling shortcut syntax, e.g. 1 4r, which should translate into four copies of the token 1

Definition at line 1098 of file MCNPInput.cpp.

                                                                      {
  if( token.find_first_of("123456789") == 0 && token.at(token.length()-1) == 'r' ){
    // token starts with a number and ends with r: treat as repeat syntax.
    const char* string = token.c_str();
    char* p;
    int num = strtol( string, &p, 10 );
    if( (p - string) != static_cast<int>(token.length()) - 1 ){
      // oops, this isn't repeat format after all
      tokens.push_back(token);
      return;
    }

    if( OPT_DEBUG ) { std::cout << "Repeat syntax: " << token << " repeats " 
                                << tokens.back() << " " << num << " times." << std::endl; }

    for( int i = 0; i < num; ++i){
      const std::string& last_tok = tokens.back();
      tokens.push_back( last_tok );
    }
  }
  else{
    tokens.push_back(token);
  }
}
static bool isblank ( const std::string &  line) [static]

Definition at line 201 of file MCNPInput.cpp.

                                            {
  return (line=="" || line.find_first_not_of(" ") == line.npos );
}
static double makedouble ( const std::string &  token) [static]

Definition at line 63 of file MCNPInput.cpp.

                                                  {
  std::string tmp = token;

  // MCNP allows FORTRAN-style floating point values where the 'e' in the exponent is missing,
  // e.g. 1.23-45 means 1.23e-45.  The following check inserts such a missing 'e' to avoid 
  // confusing strtod().
  size_t s_idx = tmp.find_last_of("+-");
  if( s_idx != tmp.npos && s_idx > tmp.find_first_of("1234567890") && tmp.at(s_idx-1) != 'e' ){
    tmp.insert( tmp.find_last_of("+-"), "e" );
    if( OPT_DEBUG ) std::cout << "Formatting FORTRAN value: converted " << token << " to " << tmp << std::endl;
  }

  const char* str = tmp.c_str();
  char* end;
  double ret = strtod(str, &end);
  if( end != str+tmp.length() ){
    std::cerr << "Warning: string [" << tmp << "] did not convert to double as expected." << std::endl;
  }
  return ret;
}
static int makeint ( const std::string &  token) [static]

Definition at line 53 of file MCNPInput.cpp.

                                            {
  const char* str = token.c_str();
  char* end;
  int ret = strtol(str, &end, 10);
  if( end != str+token.length() ){
    std::cerr << "Warning: string [" << token << "] did not convert to int as expected." << std::endl;
  }
  return ret;
}
static std::vector<double> makeTransformArgs ( const token_list_t  tokens) [static]

parse the args of an MCNP geometry transform

Definition at line 85 of file MCNPInput.cpp.

                                                                       {
  std::vector<double> args;
  for( token_list_t::const_iterator i = tokens.begin(); i!=tokens.end(); ++i){
    
    std::string token = *i;
    size_t idx;
    while( (idx = token.find_first_of("()")) != token.npos){
      token.replace( idx, 1, "" ); // remove parentheses
    }
    if( token.find_first_of( "1234567890" ) != token.npos){
      args.push_back( makedouble( token ) );
    }
    else if( token.length() > 0) {
      std::cerr << "Warning: makeTransformArgs ignoring unrecognized input token [" << token << "]" << std::endl;
    }
  }
  return args;
}
template<class T >
std::ostream& operator<< ( std::ostream &  out,
const std::vector< T > &  list 
)

Definition at line 207 of file MCNPInput.cpp.

                                                                   {

  out << "[";

  for(typename std::vector<T>::const_iterator i = list.begin(); i!=list.end(); ++i){
    out << *i << "|";
  }

  if(list.size() > 0) 
    out << "\b"; // unless list was empty, backspace the last | character

  out << "]";
  return out;
}
std::ostream& operator<< ( std::ostream &  str,
const CellCard::geom_list_entry_t t 
)

Definition at line 789 of file MCNPInput.cpp.

                                                                            {
  switch(t.first){
  case CellCard::LPAREN:     str << "("; break;
  case CellCard::RPAREN:     str << ")"; break;
  case CellCard::COMPLEMENT: str << "#"; break;
  case CellCard::UNION:      str << ":"; break;
  case CellCard::INTERSECT:  str << "*"; break;
  case CellCard::SURFNUM:    str << t.second; break;
  case CellCard::CELLNUM:    str << "c" << t.second; break;
  case CellCard::MBODYFACET: 
    int cell = t.second / 10;
    int facet = abs(t.second) - (abs(cell)*10);
    str << cell << "." << facet;
    break;
  }
  return str;
}
static FillNode parseFillNode ( InputDeck deck,
token_list_t::iterator &  i,
const token_list_t::iterator &  end,
bool  degree_format = false 
) [static]

Definition at line 139 of file MCNPInput.cpp.

                                                                                                                                      {
  // simple fill. Format is n or n (transform). Transform may be either a TR card number
  // or an immediate transform 
  
  int n; // the filling universe
  DataRef<Transform>* t;
  bool has_transform = false;

  std::string first_token = *i;
  size_t paren_idx = first_token.find("(");

  std::string second_token;

  if( paren_idx != first_token.npos ){
    // first_token has an open paren
    std::string n_str(first_token, 0, paren_idx);
    n = makeint(n_str);

    second_token = first_token.substr(paren_idx,first_token.npos);
    has_transform = true;
  }
  else{
    n = makeint(first_token);

    if( ++i != end ){
      second_token = *i;
      if( second_token[0] == '(' ){
        has_transform = true;
      }
      else{
        // the next token didn't belong to this fill 
        i--;
      }
    }
    else{ i--; }
  }

  if( has_transform ){    
    token_list_t transform_tokens;
    std::string next_token = second_token;
    
    while( next_token.find(")") == next_token.npos ){
      transform_tokens.push_back(next_token);
      next_token = *(++i);
    }
    transform_tokens.push_back( next_token );

    t = parseTransform( deck, transform_tokens, degree_format );

  }
  else{
    t = new NullRef<Transform>();
  }


  if( n < 0 ){
    n = -n; // TODO: handle negative universe numbers specially
  }
  
  return FillNode (n, t );
}
static DataRef<Transform>* parseTransform ( InputDeck deck,
const token_list_t  tokens,
bool  degree_format = false 
) [static]

Attempt to create a Transform object using the given numbers. Bounding parentheses are allowed and will be removed.

The returned object is allocated with new and becomes the property of the caller.

Definition at line 110 of file MCNPInput.cpp.

                                                                                                                   {

  std::vector<double> args = makeTransformArgs( tokens );
  if( args.size() == 1 ){
    return new CardRef<Transform>( deck, DataCard::TR, static_cast<int>(args[0]) );
  }
  else{
    return new ImmediateRef<Transform>( Transform( args, degree_format ) );
  }
}
static DataRef<Transform>* parseTransform ( InputDeck deck,
token_list_t::iterator &  i,
bool  degree_format = false 
) [static]

Definition at line 121 of file MCNPInput.cpp.

                                                                                                                 {
 
  token_list_t args;
  std::string next_token = *i;
  
  if( next_token.find("(") != next_token.npos ){
    do{
      args.push_back( next_token );
      next_token = *(++i);
    }
    while( next_token.find(")") == next_token.npos );
  }

  args.push_back( next_token );
  
  return parseTransform( deck, args, degree_format );
}
static void strlower ( std::string &  str) [static]

Definition at line 45 of file MCNPInput.cpp.

                                      {
  // convert to lowercase
  for(size_t i = 0; i < str.length(); ++i){
    str[i] = tolower( str.at(i) );
  }
}
void tokenizeLine ( std::string  line,
token_list_t tokens,
const char *  extra_separators = "" 
)

Definition at line 1123 of file MCNPInput.cpp.

                                                                                            {
  
  // replace any occurances of the characters in extra_separators with spaces;
  // they will then act as token separators
  size_t found;
   
  found = line.find_first_of(extra_separators);
  while (found!= line.npos )
  {
    line[found] = ' ';
    found = line.find_first_of(extra_separators,found+1);
  }
  
  std::stringstream str(line);
  while(str){
    std::string t;
    str >> t;

    // skip over $-style inline comments
    size_t idx;
    if((idx = t.find("$")) != t.npos){
      if(idx > 0){
        // this token had some data before the $
        t.resize(idx);
        appendToTokenList( t, tokens );
      }
      break;
    }

    // if the token is nontrivial, save it
    // necessary because stringstream may return a "" at the end of lines.
    if(t.length() > 0)  appendToTokenList( t, tokens );
  }

}
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines