kim-api  2.1.4-git+v2.1.3-git-1-g7847914a.GNU
An Application Programming Interface (API) for the Knowledgebase of Interatomic Models (KIM).
collections-example.cpp
Go to the documentation of this file.
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--2019, Regents of the University of Minnesota.
23 // All rights reserved.
24 //
25 // Contributors:
26 // Ryan S. Elliott
27 //
28 
29 
30 #include "KIM_Collection.hpp"
32 #include "KIM_Collections.hpp"
33 #include <iomanip>
34 #include <iostream>
35 #include <string>
36 
37 void dirsForCollection(KIM::Collection const collection,
38  KIM::Collections * const col)
39 {
40  int extent = 0;
41 
42  {
43  using namespace KIM::COLLECTION_ITEM_TYPE;
44  col->CacheListOfDirectoryNames(collection, modelDriver, &extent);
45  std::cout << collection.ToString() << ":" << modelDriver.ToString()
46  << " :\n";
47  for (int i = 0; i < extent; ++i)
48  {
49  std::string const * dir;
50  col->GetDirectoryName(i, &dir);
51  std::cout << "\t" << *dir << std::endl;
52  }
53 
54  extent = 0;
55  col->CacheListOfDirectoryNames(collection, portableModel, &extent);
56  std::cout << collection.ToString() << ":" << portableModel.ToString()
57  << " :\n";
58  for (int i = 0; i < extent; ++i)
59  {
60  std::string const * dir;
61  col->GetDirectoryName(i, &dir);
62  std::cout << "\t" << *dir << std::endl;
63  }
64 
65  extent = 0;
66  col->CacheListOfDirectoryNames(collection, simulatorModel, &extent);
67  std::cout << collection.ToString() << ":" << simulatorModel.ToString()
68  << " :\n";
69  for (int i = 0; i < extent; ++i)
70  {
71  std::string const * dir;
72  col->GetDirectoryName(i, &dir);
73  std::cout << "\t" << *dir << std::endl;
74  }
75  }
76 }
77 
79 {
80  using namespace KIM::COLLECTION_ITEM_TYPE;
81  int extent;
83  std::cout << kc.ToString() << ":" << modelDriver.ToString() << " :\n";
84  for (int i = 0; i < extent; ++i)
85  {
86  std::string const * name;
87  col->GetItemNameByCollectionAndType(i, &name);
88  std::cout << "\t" << *name << std::endl;
89  }
91  std::cout << kc.ToString() << ":" << portableModel.ToString() << " :\n";
92  for (int i = 0; i < extent; ++i)
93  {
94  std::string const * name;
95  col->GetItemNameByCollectionAndType(i, &name);
96  std::cout << "\t" << *name << std::endl;
97  }
99  std::cout << kc.ToString() << ":" << simulatorModel.ToString() << " :\n";
100  for (int i = 0; i < extent; ++i)
101  {
102  std::string const * name;
103  col->GetItemNameByCollectionAndType(i, &name);
104  std::cout << "\t" << *name << std::endl;
105  }
106 }
107 
108 
109 int main()
110 {
111  KIM::Collections * col;
112 
113  int error = KIM::Collections::Create(&col);
114 
115  if (error)
116  {
117  std::cerr << "Unable to create collections object." << std::endl;
118  return 1;
119  }
120 
121  {
122  std::string const * project;
123  std::string const * semVer;
124  col->GetProjectNameAndSemVer(&project, &semVer);
125 
126  std::cout << "Project : " << *project << std::endl;
127  std::cout << "semVer : " << *semVer << std::endl;
128  std::cout << std::endl;
129  }
130 
131  {
132  std::string const * name;
134  &name);
136  << " env name : " << *name << std::endl;
137  std::cout << std::endl;
138  }
139  {
140  std::string const * name;
142  &name);
144  << " env name : " << *name << std::endl;
145  std::cout << std::endl;
146  }
147  {
148  std::string const * name;
150  &name);
152  << " env name : " << *name << std::endl;
153  std::cout << std::endl;
154  }
155 
156 
157  {
158  std::string const * name;
159  std::string const * value;
160  col->GetConfigurationFileEnvironmentVariable(&name, &value);
161  std::cout << "config file env name : " << *name << std::endl
162  << "config file env value: " << *value << std::endl;
163  std::cout << std::endl;
164  }
165 
166 
167  {
168  std::string const * fileName;
169  col->GetConfigurationFileName(&fileName);
170  std::cout << "config file name : " << *fileName << std::endl;
171  std::cout << std::endl;
172  }
173 
174 
175  {
176  using namespace KIM::COLLECTION;
178  dirsForCollection(user, col);
181  std::cout << std::endl;
182  }
183 
184 
185  {
186  using namespace KIM::COLLECTION;
188  namesForCollection(user, col);
191  std::cout << std::endl;
192  }
193 
194 
195  {
196  using namespace KIM::COLLECTION_ITEM_TYPE;
197  int extent;
199  std::cout << modelDriver.ToString() << " :\n";
200  for (int i = 0; i < extent; ++i)
201  {
202  std::string const * name;
203  col->GetItemNameByType(i, &name);
204  std::cout << "\t" << *name << std::endl;
205  }
207  std::cout << portableModel.ToString() << " :\n";
208  for (int i = 0; i < extent; ++i)
209  {
210  std::string const * name;
211  col->GetItemNameByType(i, &name);
212  std::cout << "\t" << *name << std::endl;
213  }
215  std::cout << simulatorModel.ToString() << " :\n";
216  for (int i = 0; i < extent; ++i)
217  {
218  std::string const * name;
219  col->GetItemNameByType(i, &name);
220  std::cout << "\t" << *name << std::endl;
221  }
222  }
223 
224  {
225  std::string const * fileName;
226  KIM::Collection collection;
227  int error = col->GetItemLibraryFileNameAndCollection(
229  "Sim_LAMMPS_LJcut_AkersonElliott_Alchemy_PbAu",
230  &fileName,
231  &collection);
232  if (!error)
233  std::cout
234  << "Simulator Model Sim_LAMMPS_LJcut_AkersonElliott_Alchemy_PbAu"
235  << " has library name '" << *fileName << "' and is part of the '"
236  << collection.ToString() << "' collection." << std::endl;
237  else
238  std::cout << "Error from GetItemLibraryFileNameAndCollection."
239  << std::endl;
240  }
241 
242  {
243  int extent;
244  int error = col->CacheListOfItemMetadataFiles(
246  "Sim_LAMMPS_LJcut_AkersonElliott_Alchemy_PbAu",
247  &extent);
248  if (error)
249  std::cout << "Error from CacheListOfItemMetadataFiles." << std::endl;
250  else
251  {
252  std::string const * fileName;
253  unsigned int fileLength;
254  unsigned char const * fileRawData;
255  int availableAsString;
256  std::string const * fileString;
257  for (int i = 0; i < extent; ++i)
258  {
259  col->GetItemMetadataFile(i,
260  &fileName,
261  &fileLength,
262  &fileRawData,
263  &availableAsString,
264  &fileString);
265  std::cout << "Metadata File " << i << ", " << *fileName
266  << ", is of length " << fileLength << std::endl
267  << *fileString << std::endl;
268  }
269  }
270  }
271 
273  return 0;
274 }
int main()
int CacheListOfItemMetadataFiles(CollectionItemType const itemType, std::string const &itemName, int *const extent)
Cache a list of an item&#39;s metadata files.
int GetItemLibraryFileNameAndCollection(CollectionItemType const itemType, std::string const &itemName, std::string const **const fileName, Collection *const collection) const
Get the item&#39;s library file name and its KIM::Collection.
int CacheListOfItemNamesByCollectionAndType(Collection const collection, CollectionItemType const itemType, int *const extent)
Cache a list of all item names of a specific type in a specific collection.
int GetItemNameByType(int const index, std::string const **const itemName) const
Get the name of an item from the cached list.
int GetEnvironmentVariableName(CollectionItemType const itemType, std::string const **const name) const
Get the names of environment variables that store configuration settings for the KIM::COLLECTION::env...
void namesForCollection(KIM::Collection kc, KIM::Collections *const col)
Collection const currentWorkingDirectory
The standard currentWorkingDirectory Collection.
Contains the enumeration constants and the discovery routines for the CollectionItemType Extensible E...
static int Create(Collections **const collections)
Create a new KIM API Collections object.
CollectionItemType const simulatorModel
The standard simulatorModel CollectionItemType.
Provides the interface to the KIM API Collections and is meant to be used by simulators.
CollectionItemType const portableModel
The standard portableModel CollectionItemType.
Collection const user
The standard user Collection.
static void Destroy(Collections **const collections)
Destroy a previously Collections::Create&#39;d object.
std::string const & ToString() const
Converts the object to a string.
void GetConfigurationFileEnvironmentVariable(std::string const **const name, std::string const **const value) const
Get the name and value of the environment variable that stores the name of the KIM API user configura...
Contains the enumeration constants and the discovery routines for the Collection Extensible Enumerati...
Collection const environmentVariable
The standard environmentVariable Collection.
An Extensible Enumeration for the Collection&#39;s supported by the KIM API.
int GetDirectoryName(int const index, std::string const **const directoryName) const
Get the name of a directory from the cached list.
int CacheListOfItemNamesByType(CollectionItemType const itemType, int *const extent)
Cache a list of all item names of a specific type in the KIM API collections.
std::string const & ToString() const
Converts the object to a string.
CollectionItemType const modelDriver
The standard modelDriver CollectionItemType.
int GetItemNameByCollectionAndType(int const index, std::string const **const itemName) const
Get the name of an item from the cached list.
Collection const system
The standard system Collection.
void GetProjectNameAndSemVer(std::string const **const projectName, std::string const **const semVer) const
Get the KIM API project name and full Semantic Version string.
void GetConfigurationFileName(std::string const **const fileName) const
Get the absolute file and path name of the KIM API user configuration file.
int GetItemMetadataFile(int const index, std::string const **const fileName, unsigned int *const fileLength, unsigned char const **const fileRawData, int *const availableAsString, std::string const **const fileString) const
Get the name and content of one of an item&#39;s metadata files.
void dirsForCollection(KIM::Collection const collection, KIM::Collections *const col)
int CacheListOfDirectoryNames(Collection const collection, CollectionItemType const itemType, int *const extent)
Cache a list of directory names where a specific KIM API collection stores library files for a specif...