File: main.py

package info (click to toggle)
apbs 3.4.1-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 199,188 kB
  • sloc: ansic: 284,988; cpp: 60,416; fortran: 44,896; xml: 13,895; sh: 13,838; python: 8,105; yacc: 2,922; makefile: 1,428; f90: 989; objc: 448; lex: 294; awk: 266; sed: 205; java: 134; csh: 79
file content (483 lines) | stat: -rw-r--r-- 15,725 bytes parent folder | download | duplicates (3)
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
""" Python APBS Driver File

    This module allows a user to run APBS through Python. Use this module if
    you wish to include APBS in a Python-based application.

    The module mimics the main.c driver that is used in the C version of APBS.
    The functions which are called are located in apbslib.py, which is
    automatically generated by SWIG to wrap each APBS function.  See the APBS
    documentation for more information about each function.

    Todd Dolinsky (todd@ccb.wustl.edu)
    Nathan Baker (nathan.baker@pnl.gov)

    APBS -- Adaptive Poisson-Boltzmann Solver

      Nathan A. Baker (nathan.baker@pnl.gov)
      Pacific Northwest National Laboratory

      Additional contributing authors listed in the code documentation.

    Copyright (c) 2010-2020 Battelle Memorial Institute.
    Developed at the Pacific Northwest National Laboratory, operated by
    Battelle Memorial Institute,
    Pacific Northwest Division for the U.S. Department Energy.
    Portions Copyright (c) 2002-2010, Washington University in St. Louis.
    Portions Copyright (c) 2002-2020, Nathan A. Baker.
    Portions Copyright (c) 1999-2002, The Regents of the University of
    California.
    Portions Copyright (c) 1995, Michael Holst

    All rights reserved.

    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice,
    this list of conditions and the following disclaimer.

    * Redistributions in binary form must reproduce the above copyright notice,
    this list of conditions and the following disclaimer in the documentation
    and/or other materials provided with the distribution.

    * Neither the name of Washington University in St. Louis nor the names of
    its contributors may be used to endorse or promote products derived from
    this software without specific prior written permission.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
    OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
    EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
    PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
    PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
    LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
    NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""

from apbslib import (
    MGparm,
    NOsh_ctor,
    NOsh_elec2calc,
    NOsh_elecname,
    NOsh_getCalc,
    NOsh_parseInputFile,
    NOsh_printWhat,
    NOsh_setupElecCalc,
    NPT_APOLENERGY,
    NPT_APOLFORCE,
    NPT_ELECENERGY,
    NPT_ELECFORCE,
    NPT_ENERGY,
    NPT_FORCE,
    PBEparm,
    Vcom_ctor,
    Vcom_rank,
    Vcom_size,
    Vmem_ctor,
    atomForce,
    delete_Com,
    delete_Mem,
    delete_Nosh,
    delete_atomforcelist,
    delete_double_array,
    delete_gridlist,
    delete_int_array,
    delete_pbelist,
    delete_pmglist,
    delete_pmgplist,
    delete_valist,
    double_array,
    energyMG,
    get_AtomForce,
    get_Vpmg,
    initMG,
    int_array,
    killChargeMaps,
    killDielMaps,
    killEnergy,
    killForce,
    killKappaMaps,
    killMG,
    killMolecules,
    loadChargeMaps,
    loadDielMaps,
    loadKappaMaps,
    loadMolecules,
    new_atomforcelist,
    new_gridlist,
    new_pbelist,
    new_pmglist,
    new_pmgplist,
    new_valist,
    printApolEnergy,
    printApolForce,
    printElecEnergy,
    printElecForce,
    printEnergy,
    printForce,
    printMGPARM,
    printPBEPARM,
    setPartMG,
    solveMG,
    startVio,
    wrap_forceMG,
    writedataMG,
    writematMG,
    xrange,
)
import sys
import time
from sys import stdout, stderr

__author__ = "Todd Dolinsky, Nathan Baker, Yong Huang"
__date__ = "September 2009"
__version__ = "1.3"

Python_kb = 1.3806581e-23
Python_Na = 6.0221367e23
NOSH_MAXMOL = 20
NOSH_MAXCALC = 20


class APBSError(Exception):
    """ APBSError class

        The APBSError class inherits off the Exception module and returns
        a string defining the nature of the error.
    """

    def __init__(self, value):
        """
            Initialize with error message

            Parameters
                value:  Error Message (string)
        """
        self.value = value

    def __str__(self):
        """
            Return the error message
        """
        return repr(self.value)


def getHeader():
    """ Get header information about APBS
        Returns (header)
            header: Information about APBS
    """

    header = '\n\n\
    ----------------------------------------------------------------------\n\
    Adaptive Poisson-Boltzmann Solver (APBS)\n\
    Version 1.6\n\
    \n\
    APBS -- Adaptive Poisson-Boltzmann Solver\n\
    \n\
    Nathan A. Baker (nathan.baker@pnl.gov)\n\
    Pacific Northwest National Laboratory\n\
    \n\
    Additional contributing authors listed in the code documentation.\n\
    \n\
    Copyright (c) 2010-2020 Battelle Memorial Institute. \n\
    Developed at the Pacific Northwest National Laboratory, \n\
    operated by Battelle Memorial Institute, Pacific Northwest Division for \
    the U.S. Department Energy.\n\
    Portions Copyright (c) 2002-2010, Washington University in St. Louis.\n\
    Portions Copyright (c) 2002-2020, Nathan A. Baker.\n\
    Portions Copyright (c) 1999-2002, The Regents of the University of \
    California.\n\
    Portions Copyright (c) 1995, Michael Holst\n\
    \n\
    All rights reserved.\n\
    \n\
    Redistribution and use in source and binary forms, with or without\n\
    modification, are permitted provided that the following conditions are \
    met:\n\
    \n\
    * Redistributions of source code must retain the above copyright notice, \
      this\n list of conditions and the following disclaimer.\n\
    \n\
    * Redistributions in binary form must reproduce the above copyright \
      notice,\n\
      this list of conditions and the following disclaimer in the \
      documentation\n\
      and/or other materials provided with the distribution.\n\
    \n\
    * Neither the name of Washington University in St. Louis nor the names of \
      its\n\
      contributors may be used to endorse or promote products derived from \
      this\n\
      software without specific prior written permission.\n\
    \n\
    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n\
    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n\
    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n\
    A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT \
    OWNER OR\n\
    CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,\n\
    EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,\n\
    PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n\
    PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n\
    LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING\n\
    NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\n\
    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n\
    ----------------------------------------------------------------------\n\
    \n\n'

    return header


def getUsage():
    """ Get usage information about running APBS via Python
        Returns (usage)
            usage: Text about running APBS via Python
    """

    usage = "\n\n\
    ----------------------------------------------------------------------\n\
    This driver program calculates electrostatic potentials, energies,\n\
    and forces using both multigrid methods.\n\
    It is invoked as:\n\n\
      python main.py apbs.in\n\
    ----------------------------------------------------------------------\n\n"

    return usage


def main():
    """ Main driver for testing.  Runs APBS on given input file """

    # Initialize the MALOC library
    startVio()

    # Initialize variables, arrays
    com = Vcom_ctor(1)
    rank = Vcom_rank(com)
    size = Vcom_size(com)
    mgparm = MGparm()
    pbeparm = PBEparm()
    mem = Vmem_ctor("Main")
    pbe = new_pbelist(NOSH_MAXMOL)
    pmg = new_pmglist(NOSH_MAXMOL)
    pmgp = new_pmgplist(NOSH_MAXMOL)
    realCenter = double_array(3)
    totEnergy = []
    nforce = int_array(NOSH_MAXCALC)
    atomforce = new_atomforcelist(NOSH_MAXCALC)

    # Start the main timer
    main_timer_start = time.clock()

    # Check invocation
    stdout.write(getHeader())
    if len(sys.argv) != 2:
        stderr.write("main:  Called with %d arguments!\n" % len(sys.argv))
        stderr.write(getUsage())
        raise APBSError("Incorrect Usage!")

    # Parse the input file
    nosh = NOsh_ctor(rank, size)
    input_file = sys.argv[1]
    stdout.write("Parsing input file %s...\n" % input_file)
    if NOsh_parseInputFile(nosh, input_file) != 1:
        stderr.write("main:  Error while parsing input file.\n")
        raise APBSError("Error while parsing input file!")

    # Load the molecules using loadMolecules routine
    # loadMolecule passing NULL as second arg instead of Vparam
    alist = new_valist(NOSH_MAXMOL)
    if loadMolecules(nosh, None, alist) != 1:
        stderr.write("main:  Error while loading molecules. \n")
        raise APBSError("Error while loading molecules!")

    # Setup the calculations

    if NOsh_setupElecCalc(nosh, alist) != 1:
        stderr.write("main: Error while setting up calculations. \n")
        raise APBSError("Error while setting up calculations!")

    # Load the necessary maps

    dielXMap = new_gridlist(NOSH_MAXMOL)
    dielYMap = new_gridlist(NOSH_MAXMOL)
    dielZMap = new_gridlist(NOSH_MAXMOL)
    if loadDielMaps(nosh, dielXMap, dielYMap, dielZMap) != 1:
        stderr.write("Error reading dielectric maps!\n")
        raise APBSError("Error reading dielectric maps!")

    kappaMap = new_gridlist(NOSH_MAXMOL)
    if loadKappaMaps(nosh, kappaMap) != 1:
        stderr.write("Error reading kappa maps!\n")
        raise APBSError("Error reading kappa maps!")

    chargeMap = new_gridlist(NOSH_MAXMOL)
    if loadChargeMaps(nosh, chargeMap) != 1:
        stderr.write("Error reading charge maps!\n")
        raise APBSError("Error reading charge maps!")

    # Do the calculations

    stdout.write("Preparing to run %d PBE calculations. \n" % nosh.ncalc)

    for icalc in xrange(nosh.ncalc):
        totEnergy.append(0.0)

    for icalc in xrange(nosh.ncalc):
        stdout.write("---------------------------------------------\n")
        calc = NOsh_getCalc(nosh, icalc)
        mgparm = calc.mgparm
        pbeparm = calc.pbeparm
        if calc.calctype != 0:
            stderr.write("main:  Only multigrid calculations supported!\n")
            raise APBSError("Only multigrid calculations supported!")

        k = 0
        for k in range(0, nosh.nelec):
            if NOsh_elec2calc(nosh, k) >= icalc:
                break

        name = NOsh_elecname(nosh, k)
        if name == "":
            stdout.write("CALCULATION #%d:  MULTIGRID\n" % (icalc + 1))
        else:
            stdout.write(
                "CALCULATION #%d (%s): MULTIGRID\n" % ((icalc + 1), name)
            )
        stdout.write("Setting up problem...\n")

        # Routine initMG

        if (
            initMG(
                icalc,
                nosh,
                mgparm,
                pbeparm,
                realCenter,
                pbe,
                alist,
                dielXMap,
                dielYMap,
                dielZMap,
                kappaMap,
                chargeMap,
                pmgp,
                pmg,
            )
            != 1
        ):
            stderr.write("Error setting up MG calculation!\n")
            raise APBSError("Error setting up MG calculation!")

        # Print problem parameters if desired (comment out if you want
        # to minimize output to stdout)

        printMGPARM(mgparm, realCenter)
        printPBEPARM(pbeparm)

        # Solve the problem : Routine solveMG

        thispmg = get_Vpmg(pmg, icalc)

        if solveMG(nosh, thispmg, mgparm.type) != 1:
            stderr.write("Error solving PDE! \n")
            raise APBSError("Error Solving PDE!")

        # Set partition information : Routine setPartMG

        if setPartMG(nosh, mgparm, thispmg) != 1:
            stderr.write("Error setting partition info!\n")
            raise APBSError("Error setting partition info!")

        # Get the energies - the energy for this calculation
        # (calculation number icalc) will be stored in the totEnergy array

        ret, totEnergy[icalc] = energyMG(
            nosh, icalc, thispmg, 0, totEnergy[icalc], 0.0, 0.0, 0.0
        )

        # Calculate forces

        aforce = get_AtomForce(atomforce, icalc)
        wrap_forceMG(
            mem, nosh, pbeparm, mgparm, thispmg, aforce, alist, nforce, icalc
        )

        # Write out data from MG calculations : Routine writedataMG
        writedataMG(rank, nosh, pbeparm, thispmg)

        # Write out matrix from MG calculations
        writematMG(rank, nosh, pbeparm, thispmg)

    # Handle print statements - comment out if limiting output to stdout

    if nosh.nprint > 0:
        stdout.write("---------------------------------------------\n")
        stdout.write("PRINT STATEMENTS\n")
    for iprint in xrange(nosh.nprint):
        if NOsh_printWhat(nosh, iprint) == NPT_ENERGY:
            printEnergy(com, nosh, totEnergy, iprint)
        elif NOsh_printWhat(nosh, iprint) == NPT_FORCE:
            printForce(com, nosh, nforce, atomforce, iprint)
        elif NOsh_printWhat(nosh, iprint) == NPT_ELECENERGY:
            printElecEnergy(com, nosh, totEnergy, iprint)
        elif NOsh_printWhat(nosh, iprint) == NPT_ELECFORCE:
            printElecForce(com, nosh, nforce, atomForce, iprint)
        elif NOsh_printWhat(nosh, iprint) == NPT_APOLENERGY:
            printApolEnergy(nosh, iprint)
        elif NOsh_printWhat(nosh, iprint) == NPT_APOLFORCE:
            printApolForce(com, nosh, nforce, atomForce, iprint)
        else:
            stdout.write("Undefined PRINT keyword!\n")
            break

    stdout.write("----------------------------------------\n")
    stdout.write("CLEANING UP AND SHUTTING DOWN...\n")

    # Clean up APBS structures
    killForce(mem, nosh, nforce, atomforce)
    killEnergy()
    killMG(nosh, pbe, pmgp, pmg)
    killChargeMaps(nosh, chargeMap)
    killKappaMaps(nosh, kappaMap)
    killDielMaps(nosh, dielXMap, dielYMap, dielZMap)
    killMolecules(nosh, alist)

    delete_Nosh(nosh)

    # Clean up Python structures

    delete_double_array(realCenter)
    delete_int_array(nforce)
    delete_atomforcelist(atomforce)
    delete_valist(alist)
    delete_gridlist(dielXMap)
    delete_gridlist(dielYMap)
    delete_gridlist(dielZMap)
    delete_gridlist(kappaMap)
    delete_gridlist(chargeMap)
    delete_pmglist(pmg)
    delete_pmgplist(pmgp)
    delete_pbelist(pbe)

    # Clean up MALOC structures
    delete_Com(com)
    delete_Mem(mem)
    stdout.write("\n")
    stdout.write("Thanks for using APBS!\n\n")

    # Stop the main timer
    main_timer_stop = time.clock()
    stdout.write(
        "Total execution time:  %1.6e sec\n"
        % (main_timer_stop - main_timer_start)
    )


if __name__ == "__main__":
    main()