pyADCIRC: A Python interface for ADCIRC

33
pyADCIRC: A Python interface for ADCIRC Gajanan K. Choudhary, Clint Dawson Postdoctoral Fellow, Oden Institute for Computational Engineering and Sciences The University of Texas at Austin 4/14/20 1

Transcript of pyADCIRC: A Python interface for ADCIRC

Page 1: pyADCIRC: A Python interface for ADCIRC

pyADCIRC: A Python interface for ADCIRCGajanan K . Choudhary, C l int Dawson

Postdoctora l Fe l low, Oden Inst i tute for Computat iona l Eng ineer ing and Sc iences

The Univers i ty of Texas at Aust in

4/14/20 1

Page 2: pyADCIRC: A Python interface for ADCIRC

Overview◦ Introduction

◦ What?

◦ Why?

◦ Implementation

◦ How?

◦ Sample example

◦ Application

4/14/20 COMPUTATIONAL HYDRAULICS GROUP | THE UNIVERSITY OF TEXAS AT AUSTIN 2

Page 3: pyADCIRC: A Python interface for ADCIRC

IntroductionWHAT?

WHY?

4/14/20 3COMPUTATIONAL HYDRAULICS GROUP | THE UNIVERSITY OF TEXAS AT AUSTIN

Page 4: pyADCIRC: A Python interface for ADCIRC

Python vs. FortranPYTHON

◦ High-level language

◦ Faster development cycle

◦ Slower programs

◦ One of the fastest growing

programming languages [1, 2]

FORTRAN

◦ Low-level language

◦ Slower development cycle

◦ Faster programs

◦ Legacy codes not going away

any time soon

Tuesday, April 14, 2020 COMPUTATIONAL HYDRAULICS GROUP | THE UNIVERSITY OF TEXAS AT AUSTIN 4

Page 5: pyADCIRC: A Python interface for ADCIRC

Need for a Python interface

Tuesday, April 14, 2020 COMPUTATIONAL HYDRAULICS GROUP | THE UNIVERSITY OF TEXAS AT AUSTIN 5

◦ Python: Vast collection of modern open-source libraries

◦ Visualization – Matplotlib, VTK, XDMF

◦ Machine learning – PyTorch, TensorFlow

◦ Fortran: Millions spent in developing, maintaining, and using

codes that are now legacy

◦ Save effort in redeveloping legacy work for use with features

of newer languages and vice-versa

Page 6: pyADCIRC: A Python interface for ADCIRC

pyADCIRC: The ADCIRC Python interface

Tuesday, April 14, 2020 COMPUTATIONAL HYDRAULICS GROUP | THE UNIVERSITY OF TEXAS AT AUSTIN 6

◦A Python package that can be imported into Python

◦Allows:

◦ Accessing/modifying ADCIRC variables in Python

◦ Calling ADCIRC functions from Python

◦ Future: Callback functions (calling Python functions from ADCIRC)

◦No modifications to existing source code; new files added

◦ Modifications recommended for long-term code maintenance

Page 7: pyADCIRC: A Python interface for ADCIRC

ImplementationHOW?

SAMPLE EXAMPLE

4/14/20 7COMPUTATIONAL HYDRAULICS GROUP | THE UNIVERSITY OF TEXAS AT AUSTIN

Page 8: pyADCIRC: A Python interface for ADCIRC

pyADCIRC requires f2py, part of numpy

Python-Fortran interface: options

4/14/20 COMPUTATIONAL HYDRAULICS GROUP | THE UNIVERSITY OF TEXAS AT AUSTIN 8

Option Part ofCPython Compiled Auto-

generatedC++support

NumpySupport

Python/C API* True True False True True

ctypes* True False False False True

Cython* False True True True True

f2py** False True True True True

Table 1. Options for building a Python-Fortran interface

* Requires ISO_C_BINDING module part of Fortran 2003 onward for C, Fortran interoperability** Intended for Fortran 77/90/95; Other wrappers written on top of f2py also exist

Page 9: pyADCIRC: A Python interface for ADCIRC

Example: pyADCIRC

Tuesday, April 14, 2020 COMPUTATIONAL HYDRAULICS GROUP | THE UNIVERSITY OF TEXAS AT AUSTIN 9

In [1] : from pyadcirc import pyadcirc_mod as pmain # Wrapped adcirc.F

In [2] : from pyadcirc import pymesh as pmesh # Wrapped mesh.F

In [3] : pmain.pyadcirc_init() # Calls ADCIRC_INIT() - adcirc.F

Out [3] : <Skipping ADCIRC’s displayed output for brevity>

In [4] : print pmesh.y # Accesses the ‘y’ variable in mesh.F

Out [4] : array([0. <Skipping for brevity> 8000. 2000. 0.]) # 130 numbers

In [5] : pmesh.y += 0.5*pmesh.x # Modify ‘y’: y=x/2+y

In [6] : print pmesh.y

Out [6] : array([8000. <Skipping for brevity> 33000. 27000. 25000.])

In [7] : pmain.pyadcirc_run() # Calls ADCIRC_RUN() - adcirc.F

In [8] : pmain.pyadcirc_finalize() # Calls ADCIRC_FINALIZE() - adcirc.F

Page 10: pyADCIRC: A Python interface for ADCIRC

Result

4/14/20 COMPUTATIONAL HYDRAULICS GROUP | THE UNIVERSITY OF TEXAS AT AUSTIN 10

Modified: pmesh.y+=0.5*pmesh.x

Original mesh

For demonstration purposes only. The mesh should not be modified in this manner, though some othervariables could bemodified likethis.

Page 11: pyADCIRC: A Python interface for ADCIRC

Steps in creating a Python interface

Tuesday, April 14, 2020 COMPUTATIONAL HYDRAULICS GROUP | THE UNIVERSITY OF TEXAS AT AUSTIN 11

◦List modules, variables, and functions needed in Python

◦Add f2py directives (comments) to Fortran files

◦Compile the source code as a shared library using f2py

◦Use it in Python: import statement

◦Test. Always. Period.

Page 12: pyADCIRC: A Python interface for ADCIRC

Example: Building a Python interface

Tuesday, April 14, 2020 COMPUTATIONAL HYDRAULICS GROUP | THE UNIVERSITY OF TEXAS AT AUSTIN 12

module adcirc

integer :: foo

real*8, allocatable :: bar(:)

CONTAINS

subroutine allocateBar(myfoo)

integer, intent(in) :: myfoo !f2py integer, intent(in):: foo

foo = myfoo

allocate(bar(myfoo))

end

end module adcirc

Compilation (Unix):f2py –c \

-m pyadcirc \adcirc.F90

Page 13: pyADCIRC: A Python interface for ADCIRC

Example: Using the Python interface

Tuesday, April 14, 2020 COMPUTATIONAL HYDRAULICS GROUP | THE UNIVERSITY OF TEXAS AT AUSTIN 13

In [1] : import pyadcirc

In [2] : print pyadcirc

Out [2] : <module 'pyadcirc' from 'pyadcirc.so'>

In [3] : print pyadcirc.adcirc.foo

Out [3] : array(0, dtype=int32)

In [4] : pyadcirc.adcirc.allocatebar(3)

In [5] : print pyadcirc.adcirc.foo

Out [5] : array(3, dtype=int32)

In [6] : print pyadcirc.adcirc.bar

Out [6] : array([4.67543053e-310 4.67543072e-310 6.90882188e-310])

Page 14: pyADCIRC: A Python interface for ADCIRC

Application

4/14/20 14COMPUTATIONAL HYDRAULICS GROUP | THE UNIVERSITY OF TEXAS AT AUSTIN

COUPLING ADCIRC AND GSSHA

COMPARISON WITH ADH-GSSHA COUPLING

Page 15: pyADCIRC: A Python interface for ADCIRC

Software

Tuesday, April 14, 2020 COMPUTATIONAL HYDRAULICS GROUP | THE UNIVERSITY OF TEXAS AT AUSTIN 15

◦ADCIRC/pyADCIRC:

◦ Solves 2D Shallow water equations over oceans and coastal areas

◦GSSHA/gsshapython:

◦ Solves 2D/1D Diffusive wave equations over inland watersheds

◦AdH/adhpython:

◦ Solves 2D/3D Shallow water equations over coastal areas

Page 16: pyADCIRC: A Python interface for ADCIRC

pythoncoupler

Tuesday, April 14, 2020 COMPUTATIONAL HYDRAULICS GROUP | THE UNIVERSITY OF TEXAS AT AUSTIN 16

◦Code for coupling computational software

◦ Includes one- and two-way coupling between AdH and GSSHA

◦ Includes one- and two-way coupling between ADCIRC and GSSHA

◦ Requires AdH, GSSHA, and ADCIRC python interfaces

◦ Models may use different time steps, starting/ending times

◦ Coupled through in-memory data exchange, no File I/O

Page 17: pyADCIRC: A Python interface for ADCIRC

Watershed emptying into a ‘tank’

4/14/20 COMPUTATIONAL HYDRAULICS GROUP | THE UNIVERSITY OF TEXAS AT AUSTIN 17

ADCIRC model:

Rectangular

tank

GSSHA model:

Hypothetical

watershedCoupled ADCIRC and

GSSHA boundaries

Forcing: Uniform 2-hour rainfall

of 40mm/hr over the watershed

Fig. 2. Meshes of coupled GSSHA and ADCIRC models

Page 18: pyADCIRC: A Python interface for ADCIRC

Comparison with other coupled software

4/14/20 COMPUTATIONAL HYDRAULICS GROUP | THE UNIVERSITY OF TEXAS AT AUSTIN 18

0

1

2

3

4

0 1 2 3 4 5

⇥105

Cum

ula

tiv

evolu

me

(m

3)

Time (hours)

Cumulative outflow volume from the GSSHA outlet in coupled runs

Two-way ADCIRC-GSSHA coupling

Benchmark: Two-way AdH-GSSHA coupling [3]

Fig. 3(a). Comparison of ADCIRC-GSSHA coupling against AdH- GSSHA coupling [3]

Page 19: pyADCIRC: A Python interface for ADCIRC

Comparison with other coupled software

4/14/20 COMPUTATIONAL HYDRAULICS GROUP | THE UNIVERSITY OF TEXAS AT AUSTIN 19

0

5

10

15

0 1 2 3 4 5

Water

surfa

ce

ele

vatio

n(m

)

Time (hours)

Water surface elevation in ADCIRC and AdH at the coupling interface

Two-way ADCIRC-GSSHA coupling

Benchmark: Two-way AdH-GSSHA coupling [3]

Fig. 3(b). Comparison of ADCIRC-GSSHA coupling against AdH- GSSHA coupling [3]

Page 20: pyADCIRC: A Python interface for ADCIRC

Compound flooding effect

4/14/20 COMPUTATIONAL HYDRAULICS GROUP | THE UNIVERSITY OF TEXAS AT AUSTIN 20

Fig. 4. Flooded watershed at the end of a 5-hour simulation

Page 21: pyADCIRC: A Python interface for ADCIRC

Conclusion

4/14/20 21COMPUTATIONAL HYDRAULICS GROUP | THE UNIVERSITY OF TEXAS AT AUSTIN

Page 22: pyADCIRC: A Python interface for ADCIRC

pyADCIRC: The ADCIRC Python interface

Tuesday, April 14, 2020 COMPUTATIONAL HYDRAULICS GROUP | THE UNIVERSITY OF TEXAS AT AUSTIN 22

◦A Python package that can be imported into Python

◦Allows using variables and functions of ADCIRC in Python

◦No modifications to existing source code; new files added

◦Opens up new avenues of research

◦ Multi-software coupling (compound flooding)

◦ Machine learning

Page 23: pyADCIRC: A Python interface for ADCIRC

Future work

Tuesday, April 14, 2020 COMPUTATIONAL HYDRAULICS GROUP | THE UNIVERSITY OF TEXAS AT AUSTIN 23

◦ Adding call-back functions

◦ Unit/integration testing

◦ Explore other uses of pyADCIRC: Pre/post processing, I/O,

machine learning, coupling with other software, etc.

◦ Github, licensing, and version control considerations

◦ Interested? Please e-mail: [email protected]

Page 24: pyADCIRC: A Python interface for ADCIRC

References

4/14/20 COMPUTATIONAL HYDRAULICS GROUP | THE UNIVERSITY OF TEXAS AT AUSTIN 24

Page 25: pyADCIRC: A Python interface for ADCIRC

References[1] TIOBE Programming Community Index. Access link (as of 12/04/2019):

https://www.tiobe.com/tiobe-index.

[2] RedMonk Programming Language Rankings for January 2019. Access link (as

of 12/04/2019): https://redmonk.com/sogrady/2019/07/18/language-rankings-

6-19/.

[3] Choudhary, G.K. (2019). Coupled atmospheric, hydrodynamic, and hydrologic

models for simulation of complex phenomena

Tuesday, April 14, 2020 COMPUTATIONAL HYDRAULICS GROUP | THE UNIVERSITY OF TEXAS AT AUSTIN 25

Page 26: pyADCIRC: A Python interface for ADCIRC

Thank You!

4/14/20 COMPUTATIONAL HYDRAULICS GROUP | THE UNIVERSITY OF TEXAS AT AUSTIN 26

Page 27: pyADCIRC: A Python interface for ADCIRC

Appendix

4/14/20 27COMPUTATIONAL HYDRAULICS GROUP | THE UNIVERSITY OF TEXAS AT AUSTIN

ADH-GSSHA COUPLING RESULTS: HURRICANE HARVEY

REFERENCE [3]

Page 28: pyADCIRC: A Python interface for ADCIRC

AdH-GSSHA coupling results

4/14/20 COMPUTATIONAL HYDRAULICS GROUP | THE UNIVERSITY OF TEXAS AT AUSTIN 28

◦Hurricane Harvey – August 2017

◦One of the costliest hurricanes to hit the US coast

◦Massive floods

◦Attempt: Coupling AdH and GSSHA to simulate Harvey

◦ GSSHA: Brays Bayou watershed

◦ AdH: Galveston Bay

Page 29: pyADCIRC: A Python interface for ADCIRC

Harris County Watersheds

4/14/20 COMPUTATIONAL HYDRAULICS GROUP | THE UNIVERSITY OF TEXAS AT AUSTIN 29

Page 30: pyADCIRC: A Python interface for ADCIRC

Brays Bayou Watershed model

4/14/20 COMPUTATIONAL HYDRAULICS GROUP | THE UNIVERSITY OF TEXAS AT AUSTIN 30

Page 31: pyADCIRC: A Python interface for ADCIRC

Galveston Bay model

4/14/20 COMPUTATIONAL HYDRAULICS GROUP | THE UNIVERSITY OF TEXAS AT AUSTIN 31

Page 32: pyADCIRC: A Python interface for ADCIRC

HCFCD: Observed rainfall during Harvey

4/14/20 COMPUTATIONAL HYDRAULICS GROUP | THE UNIVERSITY OF TEXAS AT AUSTIN 32

0

5

10

15

20

25

30

35

40

0 1 2 3 4 5 6 7 8 9

Pre

cipi

tati

on(inches

)

Time (days)

HCFCD: Observed accumulated rainfall over Brays Bayou watershed

Page 33: pyADCIRC: A Python interface for ADCIRC

AdH-GSSHA coupling

4/14/20 COMPUTATIONAL HYDRAULICS GROUP | THE UNIVERSITY OF TEXAS AT AUSTIN 33

0

500

1000

1500

2000

0 1 2 3 4 5 6 7 8 9

Flow

rate

(m3/s

)

Time (days)

HCFCD: Outflow hydrograph at Brays Bayou at MLK Jr. Blvd, Houston, TX

GSSHA-only; Hydraulic slope BC

GSSHA-only; Constant depth BC

One-way adg coupling

Two-way gdadg coupling

USGS gauge data