lammps-analyser 0.1.0-pre-release-3

A CLI tool and language server for LAMMPS simulation input scripts.
Documentation
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
# Utility functions

The `utils` sub-namespace inside the `LAMMPS_NS` namespace provides a
collection of convenience functions and utilities that perform common
tasks that are required repeatedly throughout the LAMMPS code like
reading or writing to files with error checking or translation of
strings into specific types of numbers with checking for validity. This
reduces redundant implementations and encourages consistent behavior and
thus has some overlap with the [\"platform\"
sub-namespace](Developer_platform).

## I/O with status check and similar functions

The the first two functions are wrappers around the corresponding C
library calls `fgets()` or `fread()`. They will check if there were
errors on reading or an unexpected end-of-file state was reached. In
that case, the functions will stop with an error message, indicating the
name of the problematic file, if possible unless the *error* argument is
a NULL pointer.

The
`utils::fgets_trunc() <LAMMPS_NS::utils::fgets_trunc>`{.interpreted-text
role="cpp:func"}\_\_ function will work similar for `fgets()` but it
will read in a whole line (i.e. until the end of line or end of file),
but store only as many characters as will fit into the buffer including
a final newline character and the terminating NULL byte. If the line in
the file is longer it will thus be truncated in the buffer. This
function is used by
`utils::read_lines_from_file() <LAMMPS_NS::utils::read_lines_from_file>`{.interpreted-text
role="cpp:func"}\_\_ to read individual lines but make certain they
follow the size constraints.

The
`utils::read_lines_from_file() <LAMMPS_NS::utils::read_lines_from_file>`{.interpreted-text
role="cpp:func"}\_\_ function will read the requested number of lines of
a maximum length into a buffer and will return 0 if successful or 1 if
not. It also guarantees that all lines are terminated with a newline
character and the entire buffer with a NULL character.

------------------------------------------------------------------------

::: {.doxygenfunction project="progguide"}
sfgets
:::

::: {.doxygenfunction project="progguide"}
sfread
:::

::: {.doxygenfunction project="progguide"}
fgets_trunc
:::

::: {.doxygenfunction project="progguide"}
read_lines_from_file
:::

------------------------------------------------------------------------

## String to number conversions with validity check

These functions should be used to convert strings to numbers. They are
are strongly preferred over C library calls like `atoi()` or `atof()`
since they check if the **entire** string is a valid (floating-point or
integer) number, and will error out instead of silently returning the
result of a partial conversion or zero in cases where the string is not
a valid number. This behavior improves detecting typos or issues when
processing input files.

Similarly the
`utils::logical() <LAMMPS_NS::utils::logical>`{.interpreted-text
role="cpp:func"}\_\_ function will convert a string into a boolean and
will only accept certain words.

The *do_abort* flag should be set to `true` in case this function is
called only on a single MPI rank, as that will then trigger the a call
to `Error::one()` for errors instead of `Error::all()` and avoids a
\"hanging\" calculation when run in parallel.

Please also see
`utils::is_integer() <LAMMPS_NS::utils::is_integer>`{.interpreted-text
role="cpp:func"}\_\_ and
`utils::is_double() <LAMMPS_NS::utils::is_double>`{.interpreted-text
role="cpp:func"}\_\_ for testing strings for compliance without
conversion.

------------------------------------------------------------------------

::: {.doxygenfunction project="progguide"}
numeric(const char *file, int line, const std::string &str, bool
do_abort, LAMMPS*lmp)
:::

::: {.doxygenfunction project="progguide"}
numeric(const char *file, int line, const char*str, bool do_abort,
LAMMPS \*lmp)
:::

::: {.doxygenfunction project="progguide"}
inumeric(const char *file, int line, const std::string &str, bool
do_abort, LAMMPS*lmp)
:::

::: {.doxygenfunction project="progguide"}
inumeric(const char *file, int line, const char*str, bool do_abort,
LAMMPS \*lmp)
:::

::: {.doxygenfunction project="progguide"}
bnumeric(const char *file, int line, const std::string &str, bool
do_abort, LAMMPS*lmp)
:::

::: {.doxygenfunction project="progguide"}
bnumeric(const char *file, int line, const char*str, bool do_abort,
LAMMPS \*lmp)
:::

::: {.doxygenfunction project="progguide"}
tnumeric(const char *file, int line, const std::string &str, bool
do_abort, LAMMPS*lmp)
:::

::: {.doxygenfunction project="progguide"}
tnumeric(const char *file, int line, const char*str, bool do_abort,
LAMMPS \*lmp)
:::

::: {.doxygenfunction project="progguide"}
logical(const char *file, int line, const std::string &str, bool
do_abort, LAMMPS*lmp)
:::

::: {.doxygenfunction project="progguide"}
logical(const char *file, int line, const char*str, bool do_abort,
LAMMPS \*lmp)
:::

## String processing

The following are functions to help with processing strings and parsing
files or arguments.

------------------------------------------------------------------------

::: {.doxygenfunction project="progguide"}
strdup
:::

::: {.doxygenfunction project="progguide"}
lowercase
:::

::: {.doxygenfunction project="progguide"}
uppercase
:::

::: {.doxygenfunction project="progguide"}
trim
:::

::: {.doxygenfunction project="progguide"}
trim_comment
:::

::: {.doxygenfunction project="progguide"}
strip_style_suffix
:::

::: {.doxygenfunction project="progguide"}
star_subst
:::

::: {.doxygenfunction project="progguide"}
has_utf8
:::

::: {.doxygenfunction project="progguide"}
utf8_subst
:::

::: {.doxygenfunction project="progguide"}
count_words(const char \*text)
:::

::: {.doxygenfunction project="progguide"}
count_words(const std::string &text)
:::

::: {.doxygenfunction project="progguide"}
count_words(const std::string &text, const std::string &separators)
:::

::: {.doxygenfunction project="progguide"}
trim_and_count_words
:::

::: {.doxygenfunction project="progguide"}
join_words
:::

::: {.doxygenfunction project="progguide"}
split_words
:::

::: {.doxygenfunction project="progguide"}
split_lines
:::

::: {.doxygenfunction project="progguide"}
strmatch
:::

::: {.doxygenfunction project="progguide"}
strfind
:::

::: {.doxygenfunction project="progguide"}
is_integer
:::

::: {.doxygenfunction project="progguide"}
is_double
:::

::: {.doxygenfunction project="progguide"}
is_id
:::

::: {.doxygenfunction project="progguide"}
is_type
:::

## Potential file functions

::: {.doxygenfunction project="progguide"}
get_potential_file_path
:::

::: {.doxygenfunction project="progguide"}
get_potential_date
:::

::: {.doxygenfunction project="progguide"}
get_potential_units
:::

::: {.doxygenfunction project="progguide"}
get_supported_conversions
:::

::: {.doxygenfunction project="progguide"}
get_conversion_factor
:::

::: {.doxygenfunction project="progguide"}
open_potential(const std::string &name, LAMMPS *lmp, int*auto_convert)
:::

## Argument processing

::: {.doxygenfunction project="progguide"}
bounds
:::

::: {.doxygenfunction project="progguide"}
expand_args
:::

::: {.doxygenfunction project="progguide"}
parse_grid_id
:::

::: {.doxygenfunction project="progguide"}
expand_type
:::

## Convenience functions

::: {.doxygenfunction project="progguide"}
logmesg(LAMMPS \*lmp, const std::string &format, Args&&\... args)
:::

::: {.doxygenfunction project="progguide"}
logmesg(LAMMPS \*lmp, const std::string &mesg)
:::

::: {.doxygenfunction project="progguide"}
errorurl
:::

::: {.doxygenfunction project="progguide"}
missing_cmd_args
:::

::: {.doxygenfunction project="progguide"}
flush_buffers(LAMMPS \*lmp)
:::

::: {.doxygenfunction project="progguide"}
getsyserror
:::

::: {.doxygenfunction project="progguide"}
check_packages_for_style
:::

::: {.doxygenfunction project="progguide"}
timespec2seconds
:::

::: {.doxygenfunction project="progguide"}
date2num
:::

::: {.doxygenfunction project="progguide"}
current_date
:::

## Customized standard functions

::: {.doxygenfunction project="progguide"}
binary_search
:::

::: {.doxygenfunction project="progguide"}
merge_sort
:::

------------------------------------------------------------------------

# Special Math functions

The `MathSpecial` namespace implements a selection of custom and
optimized mathematical functions for a variety of applications.

::: {.doxygenfunction project="progguide"}
factorial
:::

::: {.doxygenfunction project="progguide"}
exp2_x86
:::

::: {.doxygenfunction project="progguide"}
fm_exp
:::

::: {.doxygenfunction project="progguide"}
my_erfcx
:::

::: {.doxygenfunction project="progguide"}
expmsq
:::

::: {.doxygenfunction project="progguide"}
square
:::

::: {.doxygenfunction project="progguide"}
cube
:::

::: {.doxygenfunction project="progguide"}
powsign
:::

::: {.doxygenfunction project="progguide"}
powint
:::

::: {.doxygenfunction project="progguide"}
powsinxx
:::

------------------------------------------------------------------------

# Tokenizer classes

The purpose of the tokenizer classes is to simplify the recurring task
of breaking lines of text down into words and/or numbers. Traditionally,
LAMMPS code would be using the `strtok()` function from the C library
for that purpose, but that function has two significant
disadvantages: 1) it cannot be used concurrently from different LAMMPS
instances since it stores its status in a global variable and 2) it
modifies the string that it is processing. These classes were
implemented to avoid both of these issues and also to reduce the amount
of code that needs to be written.

The basic procedure is to create an instance of the tokenizer class with
the string to be processed as an argument and then do a loop until all
available tokens are read. The constructor has a default set of
separator characters, but that can be overridden. The default separators
are all \"whitespace\" characters, i.e. the space character, the
tabulator character, the carriage return character, the linefeed
character, and the form feed character.

``` {.c++ caption="Tokenizer class example listing entries of the PATH environment variable"}
#include "tokenizer.h"
#include <cstdlib>
#include <string>
#include <iostream>

using namespace LAMMPS_NS;

int main(int, char **)
{
    const char *path = getenv("PATH");

    if (path != nullptr) {
        Tokenizer p(path,":");
        while (p.has_next())
            std::cout << "Entry: " << p.next() << "\n";
    }
    return 0;
}
```

Most tokenizer operations cannot fail except for
`LAMMPS_NS::Tokenizer::next`{.interpreted-text role="cpp:func"} (when
used without first checking with
`LAMMPS_NS::Tokenizer::has_next`{.interpreted-text role="cpp:func"}) and
`LAMMPS_NS::Tokenizer::skip`{.interpreted-text role="cpp:func"}. In case
of failure, the class will throw an exception, so you may need to wrap
the code using the tokenizer into a `try` / `catch` block to handle
errors. The `LAMMPS_NS::ValueTokenizer`{.interpreted-text
role="cpp:class"} class may also throw an exception when a (type of)
number is requested as next token that is not compatible with the string
representing the next word.

``` {.c++ caption="ValueTokenizer class example with exception handling"}
#include "tokenizer.h"
#include <cstdlib>
#include <string>
#include <iostream>

using namespace LAMMPS_NS;

int main(int, char **)
{
    const char *text = "1 2 3 4 5 20.0 21 twentytwo 2.3";
    double num1(0),num2(0),num3(0),num4(0);

    ValueTokenizer t(text);
    // read 4 doubles after skipping over 5 numbers
    try {
        t.skip(5);
        num1 = t.next_double();
        num2 = t.next_double();
        num3 = t.next_double();
        num4 = t.next_double();
    } catch (TokenizerException &e) {
        std::cout << "Reading numbers failed: " << e.what() << "\n";
    }
    std::cout << "Values: " << num1 << " " << num2 << " " << num3 << " " << num4 << "\n";
    return 0;
}
```

This code example should produce the following output:

``` 
Reading numbers failed: Not a valid floating-point number: 'twentytwo'
Values: 20 21 0 0
```

------------------------------------------------------------------------

::: {.doxygenclass project="progguide" members=""}
LAMMPS_NS::Tokenizer
:::

::: {.doxygenclass project="progguide" members=""}
LAMMPS_NS::TokenizerException
:::

::: {.doxygenclass project="progguide" members=""}
LAMMPS_NS::ValueTokenizer
:::

::: {.doxygenclass project="progguide" members=""}
LAMMPS_NS::InvalidIntegerException
:::

::: {.doxygenclass project="progguide" members=""}
LAMMPS_NS::InvalidFloatException
:::

------------------------------------------------------------------------

# Argument parsing classes

The purpose of argument parsing classes it to simplify and unify how
arguments of commands in LAMMPS are parsed and to make abstractions of
repetitive tasks.

The `LAMMPS_NS::ArgInfo`{.interpreted-text role="cpp:class"} class
provides an abstraction for parsing references to compute or fix styles,
variables or custom integer or double properties handled by [fix
property/atom](fix_property_atom). These would start with a \"c\_\",
\"f\_\", \"v\_\", \"d\_\", \"d2\_\", \"i\_\", or \"i2\_\" followed by
the ID or name of than instance and may be postfixed with one or two
array indices \"\[\<number\>\]\" with numbers \> 0.

A typical code segment would look like this:

``` {.c++ caption="Usage example for ArgInfo class"}
int nvalues = 0;
for (iarg = 0; iarg < nargnew; iarg++) {
  ArgInfo argi(arg[iarg]);

  which[nvalues] = argi.get_type();
  argindex[nvalues] = argi.get_index1();
  ids[nvalues] = argi.copy_name();

  if ((which[nvalues] == ArgInfo::UNKNOWN)
       || (which[nvalues] == ArgInfo::NONE)
       || (argi.get_dim() > 1))
    error->all(FLERR,"Illegal compute XXX command");

  nvalues++;
}
```

------------------------------------------------------------------------

::: {.doxygenclass project="progguide" members=""}
LAMMPS_NS::ArgInfo
:::

------------------------------------------------------------------------

# File reader classes

The purpose of the file reader classes is to simplify the recurring task
of reading and parsing files. They can use the
`ValueTokenizer <LAMMPS_NS::ValueTokenizer>`{.interpreted-text
role="cpp:class"}\_\_ class to process the read in text. The
`TextFileReader <LAMMPS_NS::TextFileReader>`{.interpreted-text
role="cpp:class"}\_\_ is a more general version while
`PotentialFileReader <LAMMPS_NS::PotentialFileReader>`{.interpreted-text
role="cpp:class"}\_\_ is specialized to implement the behavior expected
for looking up and reading/parsing files with potential parameters in
LAMMPS. The potential file reader class requires a LAMMPS instance,
requires to be run on MPI rank 0 only, will use the
`utils::get_potential_file_path <LAMMPS_NS::utils::get_potential_file_path>`{.interpreted-text
role="cpp:func"}\_\_ function to look up and open the file, and will
call the `LAMMPS_NS::Error`{.interpreted-text role="cpp:class"} class in
case of failures to read or to convert numbers, so that LAMMPS will be
aborted.

``` {.c++ caption="Use of PotentialFileReader class in pair style coul/streitz"}
PotentialFileReader reader(lmp, file, "coul/streitz");
char * line;

while((line = reader.next_line(NPARAMS_PER_LINE))) {
  try {
    ValueTokenizer values(line);
    std::string iname = values.next_string();

    int ielement;
    for (ielement = 0; ielement < nelements; ielement++)
      if (iname == elements[ielement]) break;

    if (nparams == maxparam) {
      maxparam += DELTA;
      params = (Param *) memory->srealloc(params,maxparam*sizeof(Param),
                                          "pair:params");
    }

    params[nparams].ielement = ielement;
    params[nparams].chi = values.next_double();
    params[nparams].eta = values.next_double();
    params[nparams].gamma = values.next_double();
    params[nparams].zeta = values.next_double();
    params[nparams].zcore = values.next_double();

  } catch (TokenizerException & e) {
    error->one(FLERR, e.what());
  }
  nparams++;
}
```

A file that would be parsed by the reader code fragment looks like this:

    # DATE: 2015-02-19 UNITS: metal CONTRIBUTOR: Ray Shan CITATION: Streitz and Mintmire, Phys Rev B, 50, 11996-12003 (1994)
    #
    # X (eV)                J (eV)          gamma (1/\AA)   zeta (1/\AA)    Z (e)

    Al      0.000000        10.328655       0.000000        0.968438        0.763905
    O       5.484763        14.035715       0.000000        2.143957        0.000000

------------------------------------------------------------------------

::: {.doxygenclass project="progguide" members=""}
LAMMPS_NS::TextFileReader
:::

::: {.doxygenclass project="progguide" members=""}
LAMMPS_NS::PotentialFileReader
:::

------------------------------------------------------------------------

# Memory pool classes

The memory pool classes are used for cases where otherwise many small
memory allocations would be needed and where the data would be either
all used or all freed. One example for that is the storage of neighbor
lists. The memory management strategy is based on the assumption that
allocations will be in chunks of similar sizes. The allocation is then
not done per individual call for a reserved chunk of memory, but for a
\"page\" that can hold multiple chunks of data. A parameter for the
maximum chunk size must be provided, as that is used to determine
whether a new page of memory must be used.

The `MyPage <LAMMPS_NS::MyPage>`{.interpreted-text role="cpp:class"}\_\_
class offers two ways to reserve a chunk: 1) with
`MyPage::get() <LAMMPS_NS::MyPage::get>`{.interpreted-text
role="cpp:func"}\_\_ the chunk size needs to be known in advance, 2)
with `MyPage::vget() <LAMMPS_NS::MyPage::vget>`{.interpreted-text
role="cpp:func"}\_\_ a pointer to the next chunk is returned, but its
size is registered later with
`MyPage::vgot() <LAMMPS_NS::MyPage::vgot>`{.interpreted-text
role="cpp:func"}\_\_.

``` {.c++ caption="Example of using :cpp:class:`MyPage <LAMMPS_NS::MyPage>`__"}
#include "my_page.h"
using namespace LAMMPS_NS;

MyPage<double> *dpage = new MyPage<double>;
// max size of chunk: 256, size of page: 10240 doubles (=81920 bytes)
dpage->init(256,10240);

double **build_some_lists(int num)
{
    dpage->reset();
    double **dlist = new double*[num];
    for (int i=0; i < num; ++i) {
        double *dptr = dpage.vget();
        int jnum = 0;
        for (int j=0; j < jmax; ++j) {
            // compute some dvalue for eligible loop index j
            dptr[j] = dvalue;
            ++jnum;
        }
        if (dpage.status() != 0) {
            // handle out of memory or jnum too large errors
        }
        dpage.vgot(jnum);
        dlist[i] = dptr;
    }
    return dlist;
}
```

------------------------------------------------------------------------

::: {.doxygenclass project="progguide" members=""}
LAMMPS_NS::MyPage
:::

::: {.doxygenclass project="progguide" members=""}
LAMMPS_NS::MyPoolChunk
:::

------------------------------------------------------------------------

# Eigensolver functions

The `MathEigen` sub-namespace of the `LAMMPS_NS` namespace contains
functions and classes for eigensolvers. Currently only the
`jacobi3 function <MathEigen::jacobi3>`{.interpreted-text
role="cpp:func"}\_\_ is used in various places in LAMMPS. That function
is built on top of a group of more generic eigensolvers that are
maintained in the `math_eigen_impl.h` header file. This header contains
the implementation of three template classes:

1.  \"Jacobi\" calculates all of the eigenvalues and eigenvectors of a
    dense, symmetric, real matrix.
2.  The \"PEigenDense\" class only calculates the principal eigenvalue
    (ie. the largest or smallest eigenvalue), and its corresponding
    eigenvector. However it is much more efficient than \"Jacobi\" when
    applied to large matrices (larger than 13x13). PEigenDense also can
    understand complex-valued Hermitian matrices.
3.  The \"LambdaLanczos\" class is a generalization of \"PEigenDense\"
    which can be applied to arbitrary sparse matrices.

The \"math_eigen_impl.h\" code is an amalgamation of
[jacobi_pd](https://github.com/jewettaij/jacobi_pd)\_ by Andrew Jewett
at Scripps Research (under CC0-1.0 license) and [Lambda
Lanczos](https://github.com/mrcdr/lambda-lanczos)\_ by Yuya Kurebayashi
at Tohoku University (under MIT license)

------------------------------------------------------------------------

::: {.doxygenfunction project="progguide"}
MathEigen::jacobi3(double const *const*mat, double *eval, double*\*evec)
:::

::: {.doxygenfunction project="progguide"}
MathEigen::jacobi3(double const mat\[3\]\[3\], double \*eval, double
evec\[3\]\[3\])
:::

------------------------------------------------------------------------

# Communication buffer coding with *ubuf* {#communication_buffer_coding_with_ubuf}

LAMMPS uses communication buffers where it collects data from various
class instances and then exchanges the data with neighboring subdomains.
For simplicity those buffers are defined as `double` buffers and used
for doubles and integer numbers. This presents a unique problem when
64-bit integers are used. While the storage needed for a `double` is
also 64-bit, it cannot be used by a simple assignment. To get around
that limitation, LAMMPS uses the
`ubuf <LAMMPS_NS::ubuf>`{.interpreted-text role="cpp:union"}\_\_ union.
It is used in the various \"pack\" and \"unpack\" functions in the
LAMMPS classes to store and retrieve integers that may be 64-bit from
the communication buffers.

------------------------------------------------------------------------

::: {.doxygenunion project="progguide"}
LAMMPS_NS::ubuf
:::