MOOSE - Multiscale Object Oriented Simulation Environment
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
LoadModels.cpp
Go to the documentation of this file.
1 /**********************************************************************
2 ** This program is part of 'MOOSE', the
3 ** Messaging Object Oriented Simulation Environment.
4 ** Copyright (C) 2003-2009 Upinder S. Bhalla. and NCBS
5 ** It is made available under the terms of the
6 ** GNU Lesser General Public License version 2.1
7 ** See the file COPYING.LIB for the full notice.
8 **********************************************************************/
9 
10 #include <fstream>
11 #include "header.h"
12 #include "Shell.h"
13 #include "../utility/strutil.h"
14 #include "../utility/Vec.h"
15 #include "LoadModels.h" // For the ModelType enum.
16 
17 #include "../biophysics/ReadCell.h"
18 #include "../biophysics/SwcSegment.h"
19 #include "../biophysics/ReadSwc.h"
20 #include "../kinetics/ReadKkit.h"
21 #include "../kinetics/ReadCspace.h"
22 #include "../utility/print_function.hpp"
23 
24 ModelType findModelType( string filename, ifstream& fin, string& line )
25 {
26  if ( filename.substr( filename.length() - 2 ) == ".p" )
27  return DOTP;
28 
29  if ( filename.substr( filename.length() - 4 ) == ".swc" )
30  return SWC;
31 
32  getline( fin, line );
33  line = moose::trim(line);
34  if ( line == "//genesis" )
35  {
36  getline( fin, line );
37  line = moose::trim(line);
38  if ( line.substr( 0, 7 ) == "// kkit" )
39  return KKIT;
40  }
41  if ( line.substr( 0, 9 ) == "// DOQCS" )
42  {
43  while ( getline( fin, line ) )
44  {
45  line = moose::trim(line);
46  if ( line.substr( 0, 7 ) == "// kkit" )
47  return KKIT;
48  }
49  }
50 
51  unsigned long pos = line.find_first_of( ":" );
52  string copyLine= line;
53  if (pos != string::npos )
54  {
55  copyLine = line.substr(pos+2);
56  }
57 
58  if ( copyLine.length() >= 6 && copyLine[0] == '|' && copyLine[5] == '|' )
59  return CSPACE;
60 
61  return UNKNOWN;
62 }
63 
81 bool findModelParent( Id cwe, const string& path,
82  Id& parentId, string& modelName )
83 {
84  modelName = "model";
85  string fullPath = path;
86 
87  if ( path.length() == 0 )
88  {
89  parentId = cwe;
90  return 1;
91  }
92 
93  if ( path == "/" )
94  {
95  parentId = Id();
96  return 1;
97  }
98 
99  if ( path[0] != '/' )
100  {
101  string temp = cwe.path();
102  if ( temp[temp.length() - 1] == '/' )
103  fullPath = temp + path;
104  else
105  fullPath = temp + "/" + path;
106  }
107 
108  Id paId( fullPath );
109  if ( paId == Id() ) // Path includes new model name
110  {
111  string::size_type pos = fullPath.find_last_of( "/" );
112  assert( pos != string::npos );
113  string head = fullPath.substr( 0, pos );
114  Id ret( head );
115  // When head = "" it means paId should be root.
116  if ( ret == Id() && head != "" && head != "/root" )
117  return 0;
118  parentId = ret;
119  modelName = fullPath.substr( pos + 1 );
120  return 1;
121  }
122  else // Path is an existing element.
123  {
124  parentId = Neutral::parent( paId ).id;
125  modelName = paId.element()->getName();
126  return 1;
127  }
128  return 0;
129 }
130 
132 Id Shell::doLoadModel( const string& fileName, const string& modelPath, const string& solverClass )
133 {
134  ifstream fin( fileName.c_str() );
135  if ( !fin )
136  {
137  LOG( moose::failed, "Shell::doLoadModel: could not open file " << fileName );
138  return Id();
139  }
140 
141  string modelName;
142  Id parentId;
143 
144  if ( !( findModelParent ( cwe_, modelPath, parentId, modelName ) ) )
145  return Id();
146 
147  string line;
148  switch ( findModelType( fileName, fin, line ) )
149  {
150  case DOTP:
151  {
152  ReadCell rc;
153  return rc.read( fileName, modelName, parentId );
154  return Id();
155  }
156  case SWC:
157  {
158  LOG( moose::info, "In doLoadModel for SWC" );
159  ReadSwc rs( fileName );
160  Id model = parentId;
161  if ( !parentId.element()->cinfo()->isA( "Neuron" ) )
162  {
163  model = doCreate( "Neuron", parentId, modelName, 1 );
164  }
165  rs.build( model, 0.5e-3, 1.0, 1.0, 0.01 );
166  return model;
167  }
168  case KKIT:
169  {
170  string sc = solverClass;
171  ReadKkit rk;
172  Id ret = rk.read( fileName, modelName, parentId, sc);
173  return ret;
174  }
175  break;
176  case CSPACE:
177  {
178  string sc = solverClass;
179  ReadCspace rc;
180  Id ret = rc.readModelString( line, modelName, parentId, sc);
181  rc.makePlots( 1.0 );
182  return ret;
183  }
184  case UNKNOWN:
185  default:
186  cout << "Error: Shell::doLoadModel: File type of '" <<
187  fileName << "' is unknown\n";
188  }
189  return Id();
190 }
bool findModelParent(Id cwe, const string &path, Id &parentId, string &modelName)
Definition: LoadModels.cpp:81
static ObjId parent(const Eref &e)
Definition: Neutral.cpp:701
ModelType findModelType(string filename, ifstream &fin, string &line)
Definition: LoadModels.cpp:24
Element * element() const
Synonym for Id::operator()()
Definition: Id.cpp:113
std::string path(const std::string &separator="/") const
Definition: Id.cpp:76
ModelType
Definition: LoadModels.h:9
Id id
Definition: ObjId.h:98
ObjId cwe_
Current working Element.
Definition: Shell.h:639
Id doCreate(string type, ObjId parent, string name, unsigned int numData, NodePolicy nodePolicy=MooseBlockBalance, unsigned int preferredNode=1)
Definition: Shell.cpp:181
Id readModelString(const string &model, const string &modelname, Id pa, const string &solverClass)
Definition: ReadCspace.cpp:86
Id read(const string &filename, const string &cellname, Id parent)
Definition: ReadCell.cpp:108
Definition: LoadModels.h:18
bool isA(const string &ancestor) const
Definition: Cinfo.cpp:280
void makePlots(double plotdt)
Definition: ReadCspace.cpp:139
const Cinfo * cinfo() const
Definition: Element.cpp:66
Id read(const string &filename, const string &cellname, Id parent, const string &solverClass="Stoich")
Definition: ReadKkit.cpp:276
Definition: Id.h:17
std::string trim(const std::string myString, const string &delimiters)
Definition: strutil.cpp:53
const string & getName() const
Definition: Element.cpp:56
Id doLoadModel(const string &fname, const string &modelpath, const string &solverClass="")
Returns the Id of the loaded model.
Definition: LoadModels.cpp:132
static char path[]
Definition: mfield.cpp:403
bool build(Id parent, double lambda, double RM, double RA, double CM)
Definition: ReadSwc.cpp:256