File: portable-model-info.cpp

package info (click to toggle)
kim-api 2.4.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,628 kB
  • sloc: cpp: 32,594; f90: 12,746; ansic: 3,041; sh: 1,283; lisp: 130; python: 35; makefile: 13
file content (359 lines) | stat: -rw-r--r-- 9,651 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
//
// KIM-API: An API for interatomic models
// Copyright (c) 2013--2022, Regents of the University of Minnesota.
// All rights reserved.
//
// Contributors:
//    Ryan S. Elliott
//
// SPDX-License-Identifier: LGPL-2.1-or-later
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this library; if not, write to the Free Software Foundation,
// Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
//

//
// Release: This file is part of the kim-api.git repository.
//


#include "KIM_SimulatorHeaders.hpp"
#include "KIM_SupportedExtensions.hpp"
#include "KIM_Version.hpp"
#include "edn-cpp/edn.hpp"
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>

void usage(std::string name)
{
  size_t beg = name.find_last_of("/\\");
  if (beg != std::string::npos) name = name.substr(beg + 1, std::string::npos);

  // Follows docopt.org format
  std::cerr << "Usage:\n"
            << "  " << name << " "
            << "<portable-model-name>\n"
            << "  " << name << " "
            << "--version\n";
  // note: this interface is likely to change in future kim-api releases
}


int main(int argc, char * argv[])
{
  KIM::Model * mdl;
  int requestedUnitsAccepted = 0;

  if ((argc != 2) || (std::string(argv[1]) == ""))
  {
    usage(argv[0]);
    return 1;
  }

  if ((argc == 2) && (std::string(argv[1]) == "--version"))
  {
    std::cout << KIM_VERSION_STRING << std::endl;
    return 0;
  }


  int error = KIM::Model::Create(KIM::NUMBERING::zeroBased,
                                 KIM::LENGTH_UNIT::m,
                                 KIM::ENERGY_UNIT::amu_A2_per_ps2,
                                 KIM::CHARGE_UNIT::statC,
                                 KIM::TEMPERATURE_UNIT::K,
                                 KIM::TIME_UNIT::ns,
                                 argv[1],
                                 &requestedUnitsAccepted,
                                 &mdl);

  if (error)
  {
    std::cerr << "Error creating model object" << std::endl;
    exit(1);
  }

  std::stringstream edn;
  edn << std::setprecision(16) << std::scientific;
  edn << "{"
      << "\"model-name\""
      << " \"" << argv[1] << "\" ";

  // Unit information
  edn << "\"units-request-accepted\""
      << " " << ((requestedUnitsAccepted) ? "true" : "false") << " "
      << "\"units-unused\""
      << " "
      << "[";
  KIM::LengthUnit length;
  KIM::EnergyUnit energy;
  KIM::ChargeUnit charge;
  KIM::TemperatureUnit temp;
  KIM::TimeUnit time;
  mdl->GetUnits(&length, &energy, &charge, &temp, &time);

  if (length == KIM::LENGTH_UNIT::unused)
    edn << "\"length\""
        << " ";
  if (energy == KIM::ENERGY_UNIT::unused)
    edn << "\"energy\""
        << " ";
  if (charge == KIM::CHARGE_UNIT::unused)
    edn << "\"charge\""
        << " ";
  if (temp == KIM::TEMPERATURE_UNIT::unused)
    edn << "\"temperature\""
        << " ";
  if (time == KIM::TIME_UNIT::unused)
    edn << "\"time\""
        << " ";

  edn << "]"
      << " ";

  // influence distance
  double influenceDistance = 0.0;
  mdl->GetInfluenceDistance(&influenceDistance);

  edn << "\"influence-distance\""
      << " "
      << "[ " << influenceDistance << " \"" << length.ToString() << "\" ]";

  // neighbor lists
  int numberOfNeighborLists = 0;
  double const * cutoffs = NULL;
  int const * nonContrib = NULL;
  mdl->GetNeighborListPointers(&numberOfNeighborLists, &cutoffs, &nonContrib);

  edn << "\"neighbor-lists\""
      << " "
      << "{ "
      << "\"number-of-lists\""
      << " " << numberOfNeighborLists << " "
      << "\"cutoffs\""
      << " [ ";
  for (int i = 0; i < numberOfNeighborLists; ++i) { edn << cutoffs[i] << " "; }
  edn << "] "
      << "\"model-will-not-request-neighbors-of-noncontributing-particles\""
      << " "
      << "[ ";
  for (int i = 0; i < numberOfNeighborLists; ++i)
  {
    edn << nonContrib[i] << " ";
  }
  edn << "] "
      << "} ";

  // Species support and code
  edn << "\"supported-species\""
      << " "
      << "[ ";
  int numberSpecies = 0;
  KIM::SPECIES_NAME::GetNumberOfSpeciesNames(&numberSpecies);
  for (int i = 0; i < numberSpecies; ++i)
  {
    KIM::SpeciesName species;
    KIM::SPECIES_NAME::GetSpeciesName(i, &species);
    int supported;
    int code;
    error = mdl->GetSpeciesSupportAndCode(species, &supported, &code);
    if (!error)
    {
      if (supported)
      {
        edn << "[ \"" << species.ToString() << "\" " << code << " ]"
            << " ";
      }
    }
  }
  edn << "] ";

  // Parameter information
  int numberOfParameters = 0;
  mdl->GetNumberOfParameters(&numberOfParameters);
  edn << "\"parameters\""
      << " "
      << "{ "
      << "\"number-of-parameters\""
      << " " << numberOfParameters << " "
      << "\"parameter-metadata\""
      << "[ ";
  for (int i = 0; i < numberOfParameters; ++i)
  {
    KIM::DataType dtype;
    int extent;
    std::string const * name;
    std::string const * description;
    error = mdl->GetParameterMetadata(i, &dtype, &extent, &name, &description);

    if (!error)
    {
      edn << "{ "
          << "\"data-type\""
          << " "
          << "\"" << dtype.ToString() << "\""
          << " "
          << "\"extent\""
          << " " << extent << " "
          << "\"name\""
          << " "
          << "\"" << *name << "\""
          << " "
          << "\"description\""
          << " "
          << "\"" << edn::escapeQuotes(*description) << "\""
          << " }"
          << " ";
    }
  }
  edn << "] "
      << "} ";

  // Present routines
  bool extensionPresent = false;
  int numberOfModelRoutineNames;
  KIM::MODEL_ROUTINE_NAME::GetNumberOfModelRoutineNames(
      &numberOfModelRoutineNames);

  edn << "\"routines-present\""
      << " "
      << "{ ";
  for (int i = 0; i < numberOfModelRoutineNames; ++i)
  {
    KIM::ModelRoutineName routineName;
    int present;
    int required;
    KIM::MODEL_ROUTINE_NAME::GetModelRoutineName(i, &routineName);
    error = mdl->IsRoutinePresent(routineName, &present, &required);

    if (!error)
    {
      if ((routineName == KIM::MODEL_ROUTINE_NAME::Extension) && present)
        extensionPresent = true;

      if (present)
        edn << "\"" << routineName.ToString() << "\""
            << " " << ((required) ? "required" : "optional") << " ";
    }
  }
  edn << "} ";

  // Extensions, if present
  edn << "\"extensions\""
      << " { ";

  if (extensionPresent)
  {
    KIM::SupportedExtensions extensionStruct;
    error = mdl->Extension(KIM_SUPPORTED_EXTENSIONS_ID, &extensionStruct);

    if (!error)
    {
      for (int i = 0; i < extensionStruct.numberOfSupportedExtensions; ++i)
      {
        edn << "\"" << extensionStruct.supportedExtensionID[i] << "\""
            << " "
            << ((extensionStruct.supportedExtensionRequired[i])
                    ? "\"required\""
                    : "\"optional\"")
            << " ";
      }
    }
  }
  edn << "} ";

  // Compute Arguments
  edn << "\"compute-arguments\""
      << " { ";

  KIM::ComputeArguments * computeArguments;
  error = mdl->ComputeArgumentsCreate(&computeArguments);

  if (!error)
  {
    KIM::ComputeArgumentName argumentName;
    int numberOfComputeArgumentNames = 0;
    KIM::COMPUTE_ARGUMENT_NAME::GetNumberOfComputeArgumentNames(
        &numberOfComputeArgumentNames);

    for (int i = 0; i < numberOfComputeArgumentNames; ++i)
    {
      KIM::COMPUTE_ARGUMENT_NAME::GetComputeArgumentName(i, &argumentName);
      KIM::SupportStatus supportStatus;
      error = computeArguments->GetArgumentSupportStatus(argumentName,
                                                         &supportStatus);
      if (!error)
      {
        edn << "\"" << argumentName.ToString() << "\""
            << " "
            << "\"" << supportStatus.ToString() << "\""
            << " ";
      }
    }
  }
  edn << "} ";

  // Compute Callbacks
  edn << "\"compute-callbacks\""
      << " { ";

  if (computeArguments != NULL)
  {
    KIM::ComputeCallbackName callbackName;
    int numberOfComputeCallbackNames = 0;
    KIM::COMPUTE_CALLBACK_NAME::GetNumberOfComputeCallbackNames(
        &numberOfComputeCallbackNames);

    for (int i = 0; i < numberOfComputeCallbackNames; ++i)
    {
      KIM::COMPUTE_CALLBACK_NAME::GetComputeCallbackName(i, &callbackName);
      KIM::SupportStatus supportStatus;
      error = computeArguments->GetCallbackSupportStatus(callbackName,
                                                         &supportStatus);
      if (!error)
      {
        edn << "\"" << callbackName.ToString() << "\""
            << " "
            << "\"" << supportStatus.ToString() << "\""
            << " ";
      }
    }
  }
  edn << "} ";

  edn << "}";

  // destroy the objects
  error = mdl->ComputeArgumentsDestroy(&computeArguments);
  KIM::Model::Destroy(&mdl);

  edn::EdnNode modelInfo;
  try
  {
    modelInfo = edn::read(edn.str());
  }
  catch (std::string e)
  {
    std::cerr << "Error reading edn string" << std::endl;
    exit(2);
  }

  std::cout << edn::pprint(modelInfo);

  return 0;
}