File: examples.html

package info (click to toggle)
libtrexio 2.2.3-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 6,996 kB
  • sloc: ansic: 48,761; f90: 6,313; sh: 4,451; makefile: 198; lisp: 58
file content (704 lines) | stat: -rw-r--r-- 32,767 bytes parent folder | download
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
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<!-- 2022-09-29 Thu 11:40 -->
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Examples</title>
<meta name="author" content="TREX-CoE" />
<meta name="generator" content="Org Mode" />
<style>
  #content { max-width: 60em; margin: auto; }
  .title  { text-align: center;
             margin-bottom: .2em; }
  .subtitle { text-align: center;
              font-size: medium;
              font-weight: bold;
              margin-top:0; }
  .todo   { font-family: monospace; color: red; }
  .done   { font-family: monospace; color: green; }
  .priority { font-family: monospace; color: orange; }
  .tag    { background-color: #eee; font-family: monospace;
            padding: 2px; font-size: 80%; font-weight: normal; }
  .timestamp { color: #bebebe; }
  .timestamp-kwd { color: #5f9ea0; }
  .org-right  { margin-left: auto; margin-right: 0px;  text-align: right; }
  .org-left   { margin-left: 0px;  margin-right: auto; text-align: left; }
  .org-center { margin-left: auto; margin-right: auto; text-align: center; }
  .underline { text-decoration: underline; }
  #postamble p, #preamble p { font-size: 90%; margin: .2em; }
  p.verse { margin-left: 3%; }
  pre {
    border: 1px solid #e6e6e6;
    border-radius: 3px;
    background-color: #f2f2f2;
    padding: 8pt;
    font-family: monospace;
    overflow: auto;
    margin: 1.2em;
  }
  pre.src {
    position: relative;
    overflow: auto;
  }
  pre.src:before {
    display: none;
    position: absolute;
    top: -8px;
    right: 12px;
    padding: 3px;
    color: #555;
    background-color: #f2f2f299;
  }
  pre.src:hover:before { display: inline; margin-top: 14px;}
  /* Languages per Org manual */
  pre.src-asymptote:before { content: 'Asymptote'; }
  pre.src-awk:before { content: 'Awk'; }
  pre.src-authinfo::before { content: 'Authinfo'; }
  pre.src-C:before { content: 'C'; }
  /* pre.src-C++ doesn't work in CSS */
  pre.src-clojure:before { content: 'Clojure'; }
  pre.src-css:before { content: 'CSS'; }
  pre.src-D:before { content: 'D'; }
  pre.src-ditaa:before { content: 'ditaa'; }
  pre.src-dot:before { content: 'Graphviz'; }
  pre.src-calc:before { content: 'Emacs Calc'; }
  pre.src-emacs-lisp:before { content: 'Emacs Lisp'; }
  pre.src-fortran:before { content: 'Fortran'; }
  pre.src-gnuplot:before { content: 'gnuplot'; }
  pre.src-haskell:before { content: 'Haskell'; }
  pre.src-hledger:before { content: 'hledger'; }
  pre.src-java:before { content: 'Java'; }
  pre.src-js:before { content: 'Javascript'; }
  pre.src-latex:before { content: 'LaTeX'; }
  pre.src-ledger:before { content: 'Ledger'; }
  pre.src-lisp:before { content: 'Lisp'; }
  pre.src-lilypond:before { content: 'Lilypond'; }
  pre.src-lua:before { content: 'Lua'; }
  pre.src-matlab:before { content: 'MATLAB'; }
  pre.src-mscgen:before { content: 'Mscgen'; }
  pre.src-ocaml:before { content: 'Objective Caml'; }
  pre.src-octave:before { content: 'Octave'; }
  pre.src-org:before { content: 'Org mode'; }
  pre.src-oz:before { content: 'OZ'; }
  pre.src-plantuml:before { content: 'Plantuml'; }
  pre.src-processing:before { content: 'Processing.js'; }
  pre.src-python:before { content: 'Python'; }
  pre.src-R:before { content: 'R'; }
  pre.src-ruby:before { content: 'Ruby'; }
  pre.src-sass:before { content: 'Sass'; }
  pre.src-scheme:before { content: 'Scheme'; }
  pre.src-screen:before { content: 'Gnu Screen'; }
  pre.src-sed:before { content: 'Sed'; }
  pre.src-sh:before { content: 'shell'; }
  pre.src-sql:before { content: 'SQL'; }
  pre.src-sqlite:before { content: 'SQLite'; }
  /* additional languages in org.el's org-babel-load-languages alist */
  pre.src-forth:before { content: 'Forth'; }
  pre.src-io:before { content: 'IO'; }
  pre.src-J:before { content: 'J'; }
  pre.src-makefile:before { content: 'Makefile'; }
  pre.src-maxima:before { content: 'Maxima'; }
  pre.src-perl:before { content: 'Perl'; }
  pre.src-picolisp:before { content: 'Pico Lisp'; }
  pre.src-scala:before { content: 'Scala'; }
  pre.src-shell:before { content: 'Shell Script'; }
  pre.src-ebnf2ps:before { content: 'ebfn2ps'; }
  /* additional language identifiers per "defun org-babel-execute"
       in ob-*.el */
  pre.src-cpp:before  { content: 'C++'; }
  pre.src-abc:before  { content: 'ABC'; }
  pre.src-coq:before  { content: 'Coq'; }
  pre.src-groovy:before  { content: 'Groovy'; }
  /* additional language identifiers from org-babel-shell-names in
     ob-shell.el: ob-shell is the only babel language using a lambda to put
     the execution function name together. */
  pre.src-bash:before  { content: 'bash'; }
  pre.src-csh:before  { content: 'csh'; }
  pre.src-ash:before  { content: 'ash'; }
  pre.src-dash:before  { content: 'dash'; }
  pre.src-ksh:before  { content: 'ksh'; }
  pre.src-mksh:before  { content: 'mksh'; }
  pre.src-posh:before  { content: 'posh'; }
  /* Additional Emacs modes also supported by the LaTeX listings package */
  pre.src-ada:before { content: 'Ada'; }
  pre.src-asm:before { content: 'Assembler'; }
  pre.src-caml:before { content: 'Caml'; }
  pre.src-delphi:before { content: 'Delphi'; }
  pre.src-html:before { content: 'HTML'; }
  pre.src-idl:before { content: 'IDL'; }
  pre.src-mercury:before { content: 'Mercury'; }
  pre.src-metapost:before { content: 'MetaPost'; }
  pre.src-modula-2:before { content: 'Modula-2'; }
  pre.src-pascal:before { content: 'Pascal'; }
  pre.src-ps:before { content: 'PostScript'; }
  pre.src-prolog:before { content: 'Prolog'; }
  pre.src-simula:before { content: 'Simula'; }
  pre.src-tcl:before { content: 'tcl'; }
  pre.src-tex:before { content: 'TeX'; }
  pre.src-plain-tex:before { content: 'Plain TeX'; }
  pre.src-verilog:before { content: 'Verilog'; }
  pre.src-vhdl:before { content: 'VHDL'; }
  pre.src-xml:before { content: 'XML'; }
  pre.src-nxml:before { content: 'XML'; }
  /* add a generic configuration mode; LaTeX export needs an additional
     (add-to-list 'org-latex-listings-langs '(conf " ")) in .emacs */
  pre.src-conf:before { content: 'Configuration File'; }

  table { border-collapse:collapse; }
  caption.t-above { caption-side: top; }
  caption.t-bottom { caption-side: bottom; }
  td, th { vertical-align:top;  }
  th.org-right  { text-align: center;  }
  th.org-left   { text-align: center;   }
  th.org-center { text-align: center; }
  td.org-right  { text-align: right;  }
  td.org-left   { text-align: left;   }
  td.org-center { text-align: center; }
  dt { font-weight: bold; }
  .footpara { display: inline; }
  .footdef  { margin-bottom: 1em; }
  .figure { padding: 1em; }
  .figure p { text-align: center; }
  .equation-container {
    display: table;
    text-align: center;
    width: 100%;
  }
  .equation {
    vertical-align: middle;
  }
  .equation-label {
    display: table-cell;
    text-align: right;
    vertical-align: middle;
  }
  .inlinetask {
    padding: 10px;
    border: 2px solid gray;
    margin: 10px;
    background: #ffffcc;
  }
  #org-div-home-and-up
   { text-align: right; font-size: 70%; white-space: nowrap; }
  textarea { overflow-x: auto; }
  .linenr { font-size: smaller }
  .code-highlighted { background-color: #ffff00; }
  .org-info-js_info-navigation { border-style: none; }
  #org-info-js_console-label
    { font-size: 10px; font-weight: bold; white-space: nowrap; }
  .org-info-js_search-highlight
    { background-color: #ffff00; color: #000000; font-weight: bold; }
  .org-svg { }
</style>
<link rel="stylesheet" title="Standard" href="trexio.css" type="text/css" />

<script src="org-info.js">
// @license magnet:?xt=urn:btih:1f739d935676111cfff4b4693e3816e664797050&amp;dn=gpl-3.0.txt GPL-v3-or-Later
// @license-end
</script>

<script>
// @license magnet:?xt=urn:btih:1f739d935676111cfff4b4693e3816e664797050&amp;dn=gpl-3.0.txt GPL-v3-or-Later
org_html_manager.set("TOC_DEPTH", "4");
org_html_manager.set("LINK_HOME", "index.html");
org_html_manager.set("LINK_UP", "");
org_html_manager.set("LOCAL_TOC", "1");
org_html_manager.set("VIEW_BUTTONS", "0");
org_html_manager.set("MOUSE_HINT", "underline");
org_html_manager.set("FIXED_TOC", "0");
org_html_manager.set("TOC", "1");
org_html_manager.set("VIEW", "info");
org_html_manager.setup();  // activate after the parameters are set
// @license-end
</script>
<script type="text/x-mathjax-config">
    MathJax.Hub.Config({
        displayAlign: "center",
        displayIndent: "0em",

        "HTML-CSS": { scale: 100,
                        linebreaks: { automatic: "false" },
                        webFont: "TeX"
                       },
        SVG: {scale: 100,
              linebreaks: { automatic: "false" },
              font: "TeX"},
        NativeMML: {scale: 100},
        TeX: { equationNumbers: {autoNumber: "AMS"},
               MultLineWidth: "85%",
               TagSide: "right",
               TagIndent: ".8em"
             }
});
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js?config=TeX-AMS_HTML"></script>
</head>
<body>
<div id="org-div-home-and-up">
 <a accesskey="h" href=""> UP </a>
 |
 <a accesskey="H" href="index.html"> HOME </a>
</div><div id="content" class="content">
<h1 class="title">Examples</h1>
<div id="table-of-contents" role="doc-toc">
<h2>Table of Contents</h2>
<div id="text-table-of-contents" role="doc-toc">
<ul>
<li><a href="#org9cde810">1. Accessing sparse quantities (integrals)</a>
<ul>
<li><a href="#org07e3407">1.1. Fortran</a>
<ul>
<li><a href="#org29b6380">1.1.1. Declare Temporary variables</a></li>
<li><a href="#org4f6ee33">1.1.2. Obtain the name of the TREXIO file from the command line, and open it for reading</a></li>
<li><a href="#orgce13fac">1.1.3. Read the nuclear repulsion energy</a></li>
<li><a href="#org156e32c">1.1.4. Read the number of molecular orbitals</a></li>
<li><a href="#orgad093f3">1.1.5. Allocate memory</a></li>
<li><a href="#org3283c85">1.1.6. Read one-electron quantities</a></li>
<li><a href="#orgcd6481c">1.1.7. Read two-electron quantities</a>
<ul>
<li><a href="#org5d1e8d1">1.1.7.1. Electron repulsion integrals</a></li>
<li><a href="#orgf49354f">1.1.7.2. Reduced density matrix</a></li>
</ul>
</li>
<li><a href="#orgcf80f54">1.1.8. Compute the energy</a></li>
<li><a href="#org27007da">1.1.9. Terminate</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#org861bef9">2. Reading determinants</a>
<ul>
<li><a href="#org8ca1578">2.1. Fortran</a></li>
</ul>
</li>
</ul>
</div>
</div>


<div id="outline-container-org9cde810" class="outline-2">
<h2 id="org9cde810"><span class="section-number-2">1.</span> Accessing sparse quantities (integrals)</h2>
<div class="outline-text-2" id="text-1">
</div>
<div id="outline-container-org07e3407" class="outline-3">
<h3 id="org07e3407"><span class="section-number-3">1.1.</span> Fortran</h3>
<div class="outline-text-3" id="text-1-1">
<div class="org-src-container">
<pre class="src src-f90"><span style="color: #a020f0;">program</span> <span style="color: #0000ff;">print_energy</span>
  <span style="color: #a020f0;">use</span> <span style="color: #0000ff;">trexio</span>
  <span style="color: #a020f0;">implicit</span> <span style="color: #228b22;">none</span>

  <span style="color: #228b22;">character</span>*(128)  ::<span style="color: #a0522d;"> filename   </span>! <span style="color: #b22222;">Name of the input file</span>
  <span style="color: #228b22;">integer</span>          ::<span style="color: #a0522d;"> rc         </span>! <span style="color: #b22222;">Return code for error checking</span>
  <span style="color: #228b22;">integer</span>(8)       ::<span style="color: #a0522d;"> f          </span>! <span style="color: #b22222;">TREXIO file handle</span>
  <span style="color: #228b22;">character</span>*(128)  ::<span style="color: #a0522d;"> err_msg    </span>! <span style="color: #b22222;">Error message</span>
</pre>
</div>

<p>
This program computes the energy as:
</p>

<p>
\[
   E = E_{\text{NN}} + \sum_{ij} \gamma_{ij}\, \langle j | h | i \rangle\,
   +\, \frac{1}{2} \sum_{ijkl} \Gamma_{ijkl}\, \langle k l | i j
   \rangle\; \textrm{ with } \; 0 < i,j,k,l \le n
   \]
One needs to read from the TREXIO file:
</p>

<dl class="org-dl">
<dt>\(n\)</dt><dd>The number of molecular orbitals</dd>
<dt>\(E_{\text{NN}}\)</dt><dd>The nuclear repulsion energy</dd>
<dt>\(\gamma_{ij}\)</dt><dd>The one-body reduced density matrix</dd>
<dt>\(\langle j |h| i \rangle\)</dt><dd>The one-electron Hamiltonian integrals</dd>
<dt>\(\Gamma_{ijkl}\)</dt><dd>The two-body reduced density matrix</dd>
<dt>\(\langle k l  | i j \rangle\)</dt><dd>The electron repulsion integrals</dd>
</dl>


<div class="org-src-container">
<pre class="src src-f90"><span style="color: #228b22;">integer</span>                       ::<span style="color: #a0522d;"> n</span>
<span style="color: #228b22;">double precision</span>              ::<span style="color: #a0522d;"> E, E_nn</span>
<span style="color: #228b22;">double precision</span>, <span style="color: #a020f0;">allocatable</span> ::<span style="color: #a0522d;"> D(:,:), h0(:,:)</span>
<span style="color: #228b22;">double precision</span>, <span style="color: #a020f0;">allocatable</span> ::<span style="color: #a0522d;"> G(:,:,:,:), W(:,:,:,:)</span>
</pre>
</div>
</div>

<div id="outline-container-org29b6380" class="outline-4">
<h4 id="org29b6380"><span class="section-number-4">1.1.1.</span> Declare Temporary variables</h4>
<div class="outline-text-4" id="text-1-1-1">
<div class="org-src-container">
<pre class="src src-f90"><span style="color: #228b22;">integer</span>                       ::<span style="color: #a0522d;"> i, j, k, l, m</span>
<span style="color: #228b22;">integer</span>(8), <span style="color: #a020f0;">parameter</span>         ::<span style="color: #a0522d;"> BUFSIZE = 100000_8</span>
<span style="color: #228b22;">integer</span>(8)                    ::<span style="color: #a0522d;"> offset, icount, size_max</span>
<span style="color: #228b22;">integer</span>                       ::<span style="color: #a0522d;"> buffer_index(4,BUFSIZE)</span>
<span style="color: #228b22;">double precision</span>              ::<span style="color: #a0522d;"> buffer_values(BUFSIZE)</span>

<span style="color: #228b22;">double precision</span>, <span style="color: #a020f0;">external</span>    ::<span style="color: #a0522d;"> ddot   </span>! <span style="color: #b22222;">BLAS dot product</span>
</pre>
</div>
</div>
</div>

<div id="outline-container-org4f6ee33" class="outline-4">
<h4 id="org4f6ee33"><span class="section-number-4">1.1.2.</span> Obtain the name of the TREXIO file from the command line, and open it for reading</h4>
<div class="outline-text-4" id="text-1-1-2">
<div class="org-src-container">
<pre class="src src-f90"><span style="color: #a020f0;">call</span> <span style="color: #0000ff;">getarg</span>(1, filename)

f = trexio_open (filename, <span style="color: #8b2252;">'r'</span>, TREXIO_HDF5, rc)
<span style="color: #a020f0;">if</span> (rc /= TREXIO_SUCCESS) <span style="color: #a020f0;">then</span>
   <span style="color: #a020f0;">call</span> <span style="color: #0000ff;">trexio_string_of_error</span>(rc, err_msg)
   <span style="color: #a020f0;">print</span> *, <span style="color: #8b2252;">'Error opening TREXIO file: '</span>//<span style="color: #a020f0;">trim</span>(err_msg)
   <span style="color: #a020f0;">stop</span>
<span style="color: #a020f0;">end if</span>
</pre>
</div>
</div>
</div>

<div id="outline-container-orgce13fac" class="outline-4">
<h4 id="orgce13fac"><span class="section-number-4">1.1.3.</span> Read the nuclear repulsion energy</h4>
<div class="outline-text-4" id="text-1-1-3">
<div class="org-src-container">
<pre class="src src-f90">rc = trexio_read_nucleus_repulsion(f, E_nn)
<span style="color: #a020f0;">if</span> (rc /= TREXIO_SUCCESS) <span style="color: #a020f0;">then</span>
   <span style="color: #a020f0;">call</span> <span style="color: #0000ff;">trexio_string_of_error</span>(rc, err_msg)
   <span style="color: #a020f0;">print</span> *, <span style="color: #8b2252;">'Error reading nuclear repulsion: '</span>//<span style="color: #a020f0;">trim</span>(err_msg)
   <span style="color: #a020f0;">stop</span>
<span style="color: #a020f0;">end if</span>
</pre>
</div>
</div>
</div>

<div id="outline-container-org156e32c" class="outline-4">
<h4 id="org156e32c"><span class="section-number-4">1.1.4.</span> Read the number of molecular orbitals</h4>
<div class="outline-text-4" id="text-1-1-4">
<div class="org-src-container">
<pre class="src src-f90">rc = trexio_read_mo_num(f, n)
<span style="color: #a020f0;">if</span> (rc /= TREXIO_SUCCESS) <span style="color: #a020f0;">then</span>
   <span style="color: #a020f0;">call</span> <span style="color: #0000ff;">trexio_string_of_error</span>(rc, err_msg)
   <span style="color: #a020f0;">print</span> *, <span style="color: #8b2252;">'Error reading number of MOs: '</span>//<span style="color: #a020f0;">trim</span>(err_msg)
   <span style="color: #a020f0;">stop</span>
<span style="color: #a020f0;">end if</span>
</pre>
</div>
</div>
</div>

<div id="outline-container-orgad093f3" class="outline-4">
<h4 id="orgad093f3"><span class="section-number-4">1.1.5.</span> Allocate memory</h4>
<div class="outline-text-4" id="text-1-1-5">
<div class="org-src-container">
<pre class="src src-f90"><span style="color: #a020f0;">allocate</span>( D(n,n), h0(n,n) )
<span style="color: #a020f0;">allocate</span>( G(n,n,n,n), W(n,n,n,n) )
G(:,:,:,:) = 0.d0
W(:,:,:,:) = 0.d0
</pre>
</div>
</div>
</div>

<div id="outline-container-org3283c85" class="outline-4">
<h4 id="org3283c85"><span class="section-number-4">1.1.6.</span> Read one-electron quantities</h4>
<div class="outline-text-4" id="text-1-1-6">
<div class="org-src-container">
<pre class="src src-f90">rc = trexio_has_mo_1e_int_core_hamiltonian(f)
<span style="color: #a020f0;">if</span> (rc /= TREXIO_SUCCESS) <span style="color: #a020f0;">then</span>
   <span style="color: #a020f0;">stop</span> <span style="color: #8b2252;">'No core hamiltonian in file'</span>
<span style="color: #a020f0;">end if</span>

rc = trexio_read_mo_1e_int_core_hamiltonian(f, h0)
<span style="color: #a020f0;">if</span> (rc /= TREXIO_SUCCESS) <span style="color: #a020f0;">then</span>
   <span style="color: #a020f0;">call</span> <span style="color: #0000ff;">trexio_string_of_error</span>(rc, err_msg)
   <span style="color: #a020f0;">print</span> *, <span style="color: #8b2252;">'Error reading core Hamiltonian: '</span>//<span style="color: #a020f0;">trim</span>(err_msg)
   <span style="color: #a020f0;">stop</span>
<span style="color: #a020f0;">end if</span>


rc = trexio_has_rdm_1e(f)
<span style="color: #a020f0;">if</span> (rc /= TREXIO_SUCCESS) <span style="color: #a020f0;">then</span>
   <span style="color: #a020f0;">stop</span> <span style="color: #8b2252;">'No 1e RDM in file'</span>
<span style="color: #a020f0;">end if</span>

rc = trexio_read_rdm_1e(f, D)
<span style="color: #a020f0;">if</span> (rc /= TREXIO_SUCCESS) <span style="color: #a020f0;">then</span>
   <span style="color: #a020f0;">call</span> <span style="color: #0000ff;">trexio_string_of_error</span>(rc, err_msg)
   <span style="color: #a020f0;">print</span> *, <span style="color: #8b2252;">'Error reading one-body RDM: '</span>//<span style="color: #a020f0;">trim</span>(err_msg)
   <span style="color: #a020f0;">stop</span>
<span style="color: #a020f0;">end if</span>
</pre>
</div>
</div>
</div>

<div id="outline-container-orgcd6481c" class="outline-4">
<h4 id="orgcd6481c"><span class="section-number-4">1.1.7.</span> Read two-electron quantities</h4>
<div class="outline-text-4" id="text-1-1-7">
<p>
Reading is done with OpenMP. Each thread reads its own buffer, and
the buffers are then processed in parallel.
</p>

<p>
Reading the file requires a lock, so it is done in a critical
section. The <code>offset</code> variable is shared, and it is incremented in
the critical section. For each read, the function returns in
<code>icount</code> the number of read integrals, so this variable needs also
to be protected in the critical section when modified.
</p>
</div>

<div id="outline-container-org5d1e8d1" class="outline-5">
<h5 id="org5d1e8d1"><span class="section-number-5">1.1.7.1.</span> Electron repulsion integrals</h5>
<div class="outline-text-5" id="text-1-1-7-1">
<div class="org-src-container">
<pre class="src src-f90">rc = trexio_has_mo_2e_int_eri(f)
<span style="color: #a020f0;">if</span> (rc /= TREXIO_SUCCESS) <span style="color: #a020f0;">then</span>
   <span style="color: #a020f0;">stop</span> <span style="color: #8b2252;">'No electron repulsion integrals in file'</span>
<span style="color: #a020f0;">end if</span>

rc = trexio_read_mo_2e_int_eri_size (f, size_max)
<span style="color: #a020f0;">if</span> (rc /= TREXIO_SUCCESS) <span style="color: #a020f0;">then</span>
   <span style="color: #a020f0;">call</span> <span style="color: #0000ff;">trexio_string_of_error</span>(rc, err_msg)
   <span style="color: #a020f0;">print</span> *, <span style="color: #8b2252;">'Error reading number of ERIs: '</span>//<span style="color: #a020f0;">trim</span>(err_msg)
   <span style="color: #a020f0;">stop</span>
<span style="color: #a020f0;">end if</span>

offset = 0_8
!<span style="color: #b22222;">$OMP PARALLEL DEFAULT(SHARED) PRIVATE(icount, i, j, k, l, &amp;</span>
!<span style="color: #b22222;">$OMP   buffer_index, buffer_values, m)</span>
icount = BUFSIZE
<span style="color: #a020f0;">do while</span> (icount == BUFSIZE)
  !<span style="color: #b22222;">$OMP CRITICAL</span>
  <span style="color: #a020f0;">if</span> (offset &lt; size_max) <span style="color: #a020f0;">then</span>
    rc = trexio_read_mo_2e_int_eri(f, offset, icount, buffer_index, buffer_values)
    offset = offset + icount
  <span style="color: #a020f0;">else</span>
    icount = 0
  <span style="color: #a020f0;">end if</span>
  !<span style="color: #b22222;">$OMP END CRITICAL</span>
  <span style="color: #a020f0;">do</span> m=1,icount
    i = buffer_index(1,m)
    j = buffer_index(2,m)
    k = buffer_index(3,m)
    l = buffer_index(4,m)
    W(i,j,k,l) = buffer_values(m)
    W(k,j,i,l) = buffer_values(m)
    W(i,l,k,j) = buffer_values(m)
    W(k,l,i,j) = buffer_values(m)
    W(j,i,l,k) = buffer_values(m)
    W(j,k,l,i) = buffer_values(m)
    W(l,i,j,k) = buffer_values(m)
    W(l,k,j,i) = buffer_values(m)
  <span style="color: #a020f0;">end do</span>
<span style="color: #a020f0;">end do</span>
!<span style="color: #b22222;">$OMP END PARALLEL</span>
</pre>
</div>
</div>
</div>

<div id="outline-container-orgf49354f" class="outline-5">
<h5 id="orgf49354f"><span class="section-number-5">1.1.7.2.</span> Reduced density matrix</h5>
<div class="outline-text-5" id="text-1-1-7-2">
<div class="org-src-container">
<pre class="src src-f90">rc = trexio_has_rdm_2e(f)
<span style="color: #a020f0;">if</span> (rc /= TREXIO_SUCCESS) <span style="color: #a020f0;">then</span>
   <span style="color: #a020f0;">stop</span> <span style="color: #8b2252;">'No two-body density matrix in file'</span>
<span style="color: #a020f0;">end if</span>

rc = trexio_read_rdm_2e_size (f, size_max)
<span style="color: #a020f0;">if</span> (rc /= TREXIO_SUCCESS) <span style="color: #a020f0;">then</span>
   <span style="color: #a020f0;">call</span> <span style="color: #0000ff;">trexio_string_of_error</span>(rc, err_msg)
   <span style="color: #a020f0;">print</span> *, <span style="color: #8b2252;">'Error reading number of 2-RDM elements: '</span>//<span style="color: #a020f0;">trim</span>(err_msg)
   <span style="color: #a020f0;">stop</span>
<span style="color: #a020f0;">end if</span>

offset = 0_8
!<span style="color: #b22222;">$OMP PARALLEL DEFAULT(SHARED) PRIVATE(icount, i, j, k, l, &amp;</span>
!<span style="color: #b22222;">$OMP   buffer_index, buffer_values, m)</span>
icount = bufsize
<span style="color: #a020f0;">do while</span> (offset &lt; size_max)
  !<span style="color: #b22222;">$OMP CRITICAL</span>
  <span style="color: #a020f0;">if</span> (offset &lt; size_max) <span style="color: #a020f0;">then</span>
    rc = trexio_read_rdm_2e(f, offset, icount, buffer_index, buffer_values)
    offset = offset + icount
  <span style="color: #a020f0;">else</span>
    icount = 0
  <span style="color: #a020f0;">end if</span>
  !<span style="color: #b22222;">$OMP END CRITICAL</span>
  <span style="color: #a020f0;">do</span> m=1,icount
    i = buffer_index(1,m)
    j = buffer_index(2,m)
    k = buffer_index(3,m)
    l = buffer_index(4,m)
    G(i,j,k,l) = buffer_values(m)
  <span style="color: #a020f0;">end do</span>
<span style="color: #a020f0;">end do</span>
!<span style="color: #b22222;">$OMP END PARALLEL</span>

</pre>
</div>
</div>
</div>
</div>

<div id="outline-container-orgcf80f54" class="outline-4">
<h4 id="orgcf80f54"><span class="section-number-4">1.1.8.</span> Compute the energy</h4>
<div class="outline-text-4" id="text-1-1-8">
<p>
When the orbitals are real, we can use
</p>
\begin{eqnarray*}
E &=& E_{\text{NN}} + \sum_{ij} \gamma_{ij}\, \langle j | h | i \rangle\,
+\, \frac{1}{2} \sum_{ijkl} \Gamma_{ijkl}\, \langle k l | i j
\rangle \\
  &=& E_{\text{NN}} + \sum_{ij} \gamma_{ij}\, \langle i | h | j \rangle\,
+\, \frac{1}{2} \sum_{ijkl} \Gamma_{ijkl}\, \langle i j | k l
\rangle \\
\end{eqnarray*}

<p>
As \((n,m)\) 2D arrays are stored in memory as \((n \times m)\) 1D
arrays, we could pass the matrices to the <code>ddot</code> BLAS function to
perform the summations in a single call for the 1-electron quantities.
Instead, we prefer to interleave the 1-electron (negative) and
2-electron (positive) summations to have a better cancellation of
numerical errors.
</p>

<p>
Here \(n^4\) can be larger than the largest possible 32-bit integer,
so it is not safe to pass \(n^4\) to the <code>ddot</code> BLAS
function. Hence, we perform \(n^2\) loops, using vectors of size \(n^2\).
</p>

<div class="org-src-container">
<pre class="src src-f90">
E = 0.d0
<span style="color: #a020f0;">do</span> l=1,n
  E = E + ddot( n, D(1,l), 1, h0(1,l), 1 ) 
  <span style="color: #a020f0;">do</span> k=1,n
     E = E + 0.5d0 * ddot( n*n, G(1,1,k,l), 1, W(1,1,k,l),  1 )
  <span style="color: #a020f0;">end do</span>
<span style="color: #a020f0;">end do</span>
E = E + E_nn

<span style="color: #a020f0;">print</span> *, <span style="color: #8b2252;">'Energy: '</span>, E
</pre>
</div>
</div>
</div>

<div id="outline-container-org27007da" class="outline-4">
<h4 id="org27007da"><span class="section-number-4">1.1.9.</span> Terminate</h4>
<div class="outline-text-4" id="text-1-1-9">
<div class="org-src-container">
<pre class="src src-f90">  <span style="color: #a020f0;">deallocate</span>( D, h0, G, W )

<span style="color: #a020f0;">end program</span>
</pre>
</div>
</div>
</div>
</div>
</div>


<div id="outline-container-org861bef9" class="outline-2">
<h2 id="org861bef9"><span class="section-number-2">2.</span> Reading determinants</h2>
<div class="outline-text-2" id="text-2">
</div>
<div id="outline-container-org8ca1578" class="outline-3">
<h3 id="org8ca1578"><span class="section-number-3">2.1.</span> Fortran</h3>
<div class="outline-text-3" id="text-2-1">
<div class="org-src-container">
<pre class="src src-f90"><span style="color: #a020f0;">program</span> <span style="color: #0000ff;">test</span>

  <span style="color: #a020f0;">use</span> <span style="color: #0000ff;">trexio</span>
  <span style="color: #a020f0;">implicit</span> <span style="color: #228b22;">none</span>

  <span style="color: #228b22;">character</span>*(128)           ::<span style="color: #a0522d;"> filename               </span>! <span style="color: #b22222;">Name of the input file</span>
  <span style="color: #228b22;">integer</span>(trexio_exit_code) ::<span style="color: #a0522d;"> rc                     </span>! <span style="color: #b22222;">Return code for error checking</span>
  <span style="color: #228b22;">integer</span>(trexio_t)         ::<span style="color: #a0522d;"> trex_determinant_file  </span>! <span style="color: #b22222;">TREXIO file handle</span>
  <span style="color: #228b22;">character</span>*(128)           ::<span style="color: #a0522d;"> err_msg                </span>! <span style="color: #b22222;">Error message</span>


  <span style="color: #228b22;">integer</span>*8, <span style="color: #a020f0;">allocatable</span> ::<span style="color: #a0522d;"> buffer(:,:,:)</span>
  <span style="color: #228b22;">integer</span>(8) ::<span style="color: #a0522d;"> offset, icount, BUFSIZE</span>
  <span style="color: #228b22;">integer</span> ::<span style="color: #a0522d;"> ndet, int64_num, m</span>

  <span style="color: #228b22;">integer</span> ::<span style="color: #a0522d;"> occ_num_up, occ_num_dn</span>
  <span style="color: #228b22;">integer</span>, <span style="color: #a020f0;">allocatable</span> ::<span style="color: #a0522d;"> orb_list_up(:), orb_list_dn(:)</span>

  <span style="color: #a020f0;">call</span> <span style="color: #0000ff;">getarg</span>(1, filename)

  trex_determinant_file = trexio_open(filename, <span style="color: #8b2252;">'r'</span>, TREXIO_AUTO, rc)
  <span style="color: #a020f0;">if</span> (rc /= TREXIO_SUCCESS) <span style="color: #a020f0;">then</span>
     <span style="color: #a020f0;">call</span> <span style="color: #0000ff;">trexio_string_of_error</span>(rc, err_msg)
     <span style="color: #a020f0;">print</span> *, <span style="color: #8b2252;">'Error opening TREXIO file: '</span>//<span style="color: #a020f0;">trim</span>(err_msg)
     <span style="color: #a020f0;">stop</span>
  <span style="color: #a020f0;">end if</span>

  rc = trexio_read_determinant_num(trex_determinant_file, ndet)
  <span style="color: #a020f0;">if</span> (rc /= TREXIO_SUCCESS) <span style="color: #a020f0;">then</span>
     <span style="color: #a020f0;">call</span> <span style="color: #0000ff;">trexio_string_of_error</span>(rc, err_msg)
     <span style="color: #a020f0;">print</span> *, <span style="color: #8b2252;">'Error reading determinant_num: '</span>//<span style="color: #a020f0;">trim</span>(err_msg)
     <span style="color: #a020f0;">stop</span>
  <span style="color: #a020f0;">end if</span>
  <span style="color: #a020f0;">print</span> *, <span style="color: #8b2252;">'ndet'</span>, ndet

  rc = trexio_get_int64_num(trex_determinant_file, int64_num)
  <span style="color: #a020f0;">if</span> (rc /= TREXIO_SUCCESS) <span style="color: #a020f0;">then</span>
     <span style="color: #a020f0;">call</span> <span style="color: #0000ff;">trexio_string_of_error</span>(rc, err_msg)
     <span style="color: #a020f0;">print</span> *, <span style="color: #8b2252;">'Error reading int64_num: '</span>//<span style="color: #a020f0;">trim</span>(err_msg)
     <span style="color: #a020f0;">stop</span>
  <span style="color: #a020f0;">end if</span>
  <span style="color: #a020f0;">print</span> *, <span style="color: #8b2252;">'int64_num'</span>, int64_num

  BUFSIZE = 1000_8
  <span style="color: #a020f0;">allocate</span>(buffer(int64_num, 2, BUFSIZE))
  <span style="color: #a020f0;">allocate</span>(orb_list_up(int64_num*64), orb_list_dn(int64_num*64))

  offset = 0_8
  icount = BUFSIZE
  <span style="color: #a020f0;">do while</span> (icount == BUFSIZE)
    <span style="color: #a020f0;">if</span> (offset &lt; ndet) <span style="color: #a020f0;">then</span>
      rc = trexio_read_determinant_list(trex_determinant_file, offset, icount, buffer)
      offset = offset + icount
    <span style="color: #a020f0;">else</span>
      icount = 0
    <span style="color: #a020f0;">end if</span>
    <span style="color: #a020f0;">print</span> *, <span style="color: #8b2252;">'---'</span>
    <span style="color: #a020f0;">do</span> m=1,icount
      rc = trexio_to_orbital_list_up_dn(int64_num, buffer(1,1,m), <span style="color: #a020f0;">&amp;</span>
           orb_list_up, orb_list_dn, occ_num_up, occ_num_dn)
      <span style="color: #a020f0;">print</span> <span style="color: #8b2252;">'(100(I3,X))'</span>, (orb_list_up(1:occ_num_up)), (orb_list_dn(1:occ_num_dn))
      <span style="color: #a020f0;">print</span> *, <span style="color: #8b2252;">''</span>
    <span style="color: #a020f0;">end do</span>
  <span style="color: #a020f0;">end do</span>

  <span style="color: #a020f0;">deallocate</span>(buffer, orb_list_dn, orb_list_up)

end
</pre>
</div>
</div>
</div>
</div>
</div>
<div id="postamble" class="status">
<p class="author">Author: TREX-CoE</p>
<p class="date">Created: 2022-09-29 Thu 11:40</p>
<p class="validation"><a href="https://validator.w3.org/check?uri=referer">Validate</a></p>
</div>
</body>
</html>