1 //
2 // CDDL HEADER START
3 //
4 // The contents of this file are subject to the terms of the Common Development
5 // and Distribution License Version 1.0 (the "License").
6 //
7 // You can obtain a copy of the license at
8 // http://www.opensource.org/licenses/CDDL-1.0.  See the License for the
9 // specific language governing permissions and limitations under the License.
10 //
11 // When distributing Covered Code, include this CDDL HEADER in each file and
12 // include the License file in a prominent location with the name LICENSE.CDDL.
13 // If applicable, add the following below this CDDL HEADER, with the fields
14 // enclosed by brackets "[]" replaced with your own identifying information:
15 //
16 // Portions Copyright (c) [yyyy] [name of copyright owner]. All rights reserved.
17 //
18 // CDDL HEADER END
19 //
20 
21 //
22 // Copyright (c) 2016--2020, Regents of the University of Minnesota.
23 // All rights reserved.
24 //
25 // Contributors:
26 //    Ryan S. Elliott
27 //    Alexander Stukowski
28 //
29 
30 //
31 // Release: This file is part of the kim-api-2.2.1 package.
32 //
33 
34 
35 #ifndef KIM_SIMULATOR_MODEL_IMPLEMENTATION_HPP_
36 #define KIM_SIMULATOR_MODEL_IMPLEMENTATION_HPP_
37 
38 #include <map>
39 #include <sstream>
40 #include <string>
41 #include <vector>
42 
43 #ifndef KIM_LOG_VERBOSITY_HPP_
44 #include "KIM_LogVerbosity.hpp"
45 #endif
46 
47 #ifndef KIM_FILESYSTEM_PATH_HPP_
48 #include "KIM_FilesystemPath.hpp"
49 #endif
50 
51 namespace edn
52 {
53 // Forward declarations
54 struct EdnNode;
55 }  // namespace edn
56 
57 namespace KIM
58 {
59 // Forward declarations
60 class Log;
61 class SharedLibrary;
62 
63 class SimulatorModelImplementation
64 {
65  public:
66   static int
67   Create(std::string const & simulatorModelName,
68          SimulatorModelImplementation ** const simulatorModelImplementation);
69 
70   static void
71   Destroy(SimulatorModelImplementation ** const simulatorModelImplementation);
72 
73   void
74   GetSimulatorNameAndVersion(std::string const ** const simulatorString,
75                              std::string const ** const simulatorVersion) const;
76 
77   void GetNumberOfSupportedSpecies(int * const numberOfSupportedSpecies) const;
78   int GetSupportedSpecies(int const index,
79                           std::string const ** const speciesName) const;
80 
81   void OpenAndInitializeTemplateMap();
82   int TemplateMapIsOpen() const;
83   int AddTemplateMap(std::string const & key, std::string const & value);
84   void CloseTemplateMap();
85 
86   void GetNumberOfSimulatorFields(int * const numberOfSimulatorFields) const;
87 
88   int GetSimulatorFieldMetadata(int const fieldIndex,
89                                 int * const extent,
90                                 std::string const ** const fieldName) const;
91 
92   int GetSimulatorFieldLine(int const fieldIndex,
93                             int const lineIndex,
94                             std::string const ** const lineValue) const;
95 
96   void
97   GetParameterFileDirectoryName(std::string const ** const directoryName) const;
98 
99   void GetSpecificationFileName(
100       std::string const ** const specificationFileName) const;
101 
102   void GetNumberOfParameterFiles(int * const numberOfParameterFiles) const;
103 
104   int GetParameterFileName(int const index,
105                            std::string const ** const parameterFileName) const;
106   int GetParameterFileBasename(
107       int const index, std::string const ** const parameterFileBasename) const;
108 
109   void SetSimulatorBufferPointer(void * const ptr);
110 
111   void GetSimulatorBufferPointer(void ** const ptr) const;
112 
113   void LogEntry(LogVerbosity const logVerbosity,
114                 std::string const & message,
115                 int const lineNumber,
116                 std::string const & fileName) const;
117 
118   void LogEntry(LogVerbosity const logVerbosity,
119                 std::stringstream const & message,
120                 int const lineNumber,
121                 std::string const & fileName) const;
122 
123   std::string const & ToString() const;
124 
125   void SetLogID(std::string const & logID);
126 
127   void PushLogVerbosity(LogVerbosity const logVerbosity);
128 
129   void PopLogVerbosity();
130 
131  private:
132   // do not allow copy constructor or operator=
133   SimulatorModelImplementation(SimulatorModelImplementation const &);
134   void operator=(SimulatorModelImplementation const &);
135 
136   SimulatorModelImplementation(SharedLibrary * const sharedLibrary,
137                                Log * const log);
138   ~SimulatorModelImplementation();
139 
140   std::string simulatorModelName_;
141 
142   SharedLibrary * sharedLibrary_;
143 
144   Log * log_;
145 
146   int ParseEdn(edn::EdnNode & node) const;
147   int GetSchemaVersion();
148   int ReadEdnSchemaV1();
149   int Initialize(std::string const & simulatorModelName);
150 
151   FILESYSTEM::Path parameterFileDirectoryName_;
152   std::string parameterFileDirectoryNameString_;
153   std::string specificationFileName_;
154   int schemaVersion_;
155   std::string modelName_;
156 
157   std::string simulatorName_;
158   std::string simulatorVersion_;
159   std::vector<std::string> simulatorSupportedSpecies_;
160 
161   std::vector<std::string> simulatorFieldNames_;
162   std::vector<std::vector<std::string> > originalSimulatorFields_;
163   std::vector<std::vector<std::string> > simulatorFields_;
164 
165   int numberOfParameterFiles_;
166   std::vector<std::string> parameterFileBasenames_;
167 
168   bool templateMapOpen_;
169   std::map<std::string, std::string> templateMap_;
170 
171   void AddStandardTemplatesToMap();
172   int ProcessSimulatorFields();
173 
174   void * simulatorBuffer_;
175 
176   mutable std::string string_;
177 
178 };  // class SimulatorModelImplementation
179 }  // namespace KIM
180 #endif  // KIM_SIMULATOR_MODEL_IMPLEMENTATION_HPP_
181