Git Product home page Git Product logo

data-driven-advection's Introduction

Data driven discretizations for solving 2D advection problems

This repository contains the code used in the paper

Learned discretizations for passive scalar advection in a two dimensional turbulent flow. Jiawei Zhuang, Dmitrii Kochkov, Yohai Bar-Sinai, Michael P. Brenner, Stephan Hoyer. Physical Review Fluids, in print. arXiv: 2004.05477

This is an extension of the techniques developed in:

Learning data-driven discretizations for partial differential equations. Yohai Bar-Sinai*, Stephan Hoyer*, Jason Hickey, Michael P. Brenner. PNAS 2019, 116 (31) 15344-15349.

See this repository for the code used to produce results for the PNAS paper.

This is not an official Google product.

Installation

Installation is most easily done using pip.

  1. Create or activate a virtual environment (e.g. using virtualenv or conda).

  2. Install TensorFlow.

  3. If you just want to install the package without the code, simply use pip to install directly from github:

    pip install git+git//github.com/google-research/data-driven-pdes

    If you want to fiddle around with the code, cd to where you want to store the code, clone the repo and install:

cd <your directory>
git clone git+https://github.com/google-research/data-driven-pdes
pip install -e data-driven-pdes

Usage

We aim to make the code accessible for researchers who want to apply our method to their favorite PDEs. To this end we wrote, and continue to write, tutorials and documentation. This is still very much in development, please open an issue if you have questions.

  1. A tutorial notebook that explains some of the basic notions in the code base and demonstrates how to use the framework to define new equations.
  2. This notebook contains a complete example of creating a training database, defining a model, training it and evaluating the trained model (well documented, though less pedagogical).

data-driven-advection's People

Contributors

jiaweizhuang avatar shoyer avatar yohai avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

data-driven-advection's Issues

ValueError: You cannot pass a dictionary as model target.

I managed to implement a WENO scheme for the 2D compressible Euler equations which works pretty well, but, by trying to run training the following error is thrown:

ValueError: You cannot pass a dictionary as model target.

It is due to the fact that the output is a dict of arrays regarding each of the learned keys (in this case, the conservative variables: 'density', 'x_momentum', 'y_momentum', 'energy').

How can I overcome this?

Thank you in advance,

P.S.: Passing the dict values as a list does not work neither and throws
AttributeError: 'NoneType' object has no attribute 'dtype'

Neural network for Turing equation

Hello. I am able to run the Tutorial example with the Turing equation correctly. Now I am trying to extend that example by developing a neural network (nn) for the Turing equation, but I am getting error at the integration stage before training with any numerical data. Based on a tutorial example to create a nn, I am using the following to create a nn for the Turing equation:

# Run untrained neural net model

model_nn = models.PseudoLinearModel(equation, grid, 
                                    num_time_steps=4,  
                                    stencil_size=3, kernel_size=(3, 1), 
                                    num_layers=4, filters=32,
                                    constrained_accuracy_order=1, 
                                    learned_keys = {'A', 'B'}, 
                                    activation='relu')

tf.random.set_random_seed(0)

integrated_untrained = integrate.integrate_steps(model_nn, initial_state, time_steps)

I am getting the following error at the integrate.integrate_steps() step above.

anaconda2/envs/data-driven-pdes/lib/python3.6/site-packages/tensorflow/python/ops/nn_ops.py", line 1029, in __init__
    "input tensor must have rank %d" % (num_spatial_dims + 2))
ValueError: input tensor must have rank 4

I would appreciate your help in resolving the above error.

Implementing a JST scheme for 2D compressible Euler equations

I've been asking myself some questions whose answers might help me in implementing a finite-volume scheme for the 2D compressible Euler equations
(following Jameson et al. (1981)):

1. The definition of variables in staggered grids through the offset argument in states.StateDefinition instances is intended for flux computations.

How can I be sure, however, that its value equals the mean of the two edge-adjacent cells' values?

In other words, for the Advection problem, for instance, where is it defined that
concentration_edge_x [ i+1/2, j ] = ( concentration [ i, j ] + concentration [ i+1, j ] ) / 2 ?

2. By default, the Euler explicit method is used for time integration. However, I do need to implement a 4th order Runge-Kutta method which requires multi-step flux computing
(I suppose I might define a new method called 'take_time_step' within the class definition).

Does the staggered 'x_momentum_edge_x' variable get automatically updated once I compute one step for 'x_momentum'?

It is necessary, since I need updated values in order to compute fluxes for each step.

Thank you in advance,

Errors running advection_1d example

I am getting errors while running the advection_1d example given in this python notebook. The version I have for tensorflow is same as the version shown in that example:

tf.__version__, tf.keras.__version__
('1.13.1', '2.2.4-tf')

The error I am getting is at the following line, just before neural network is trained with the data:

model_nn.compile(optimizer=tf.keras.optimizers.Adam(0.001 * 1), loss='mae', metrics=[tf.keras.metrics.RootMeanSquaredError()])

AttributeError: module 'tensorflow._api.v1.keras.metrics' has no attribute 'RootMeanSquaredError'

I checked the class tf.keras.metrics and it does not have any function called RootMeanSquaredError, which is the reason for error. After some search, I modified the metrics in above line throwing the error as metrics=["RootMeanSquaredError"], which helps get rid of that particular error, but I get another error on the next line as follows:

history = model_nn.fit(train_input, train_output, epochs=80, batch_size=32, verbose=1, shuffle=True)

ValueError: When running a model in eager execution, the optimizer must be an instance of tf.train.Optimizer. Received: <tensorflow.python.keras.optimizers.Adam object at 0x7f0c885da400>

The above error does not go away whether I enable the eager execution or not. I was curious how the example in that notebook ran with the exact same version of tensorflow that I am also using with Python 3.6. I would appreciate your help with this.

InvalidArgumentError: Computed output size would be negative: -3 [input_size: 3, effective_filter_size: 7, stride: 1] [Op:ExtractImagePatches]

For any discretization stencil size , k > 3 (In above case k = 7), I get the following error. I am trying to analyze the solution accuracy for larger stencil sizes and I am getting the above error for any stencil with k>3.
My apologies if I am making some silly error, but it seems to me like a problem in the extract_patches_2d function but I do not understand how it would cause me to be unable to use k>3.
Thanks in Advance.

unsupported operand type(s) for &: 'ellipsis' and 'ellipsis'

Error message:

/data-driven-pdes-master/datadrivenpdes/core/equations.py in _validate_keys(self)
77 """Validate the key_definitions, evolving_keys and constant_keys attributes.
78 """
---> 79 repeated_keys = self.evolving_keys & self.constant_keys
80 if repeated_keys:
81 raise ValueError('overlapping entries between evolving_keys and '

TypeError: unsupported operand type(s) for &: 'ellipsis' and 'ellipsis'


I didn't build any evolving key or constant key.

key_definitions = {
    'Phi': states.StateDefinition(name='Phi',     
                                  tensor_indices=(),          
                                  derivative_orders=(0,0,0),  
                                  offset=(0,0)),              
    'Phi_xx': states.StateDefinition(name='Phi',     
                                     tensor_indices=(X,),          
                                     derivative_orders=(2,0,0),  
                                     offset=(0,0)),              
    'Phi_yy': states.StateDefinition(name='Phi',       
                                     tensor_indices=(Y,),
                                     derivative_orders=(0, 2, 0),    
                                     offset=(0, 0)),             
}

def __init__(self, D, timestep=1e-4):
    self.D = D
    self._timestep = timestep
    super().__init__()

def time_derivative(
      self, grid, Phi_xx, Phi_yy):
    diff_X = self.D * Phi_xx
    diff_Y = self.D * Phi_yy
    return {diff_X + diff_Y}

I don't know what this ellipsis is?

Error running the Tutorial notebook - possibly a version issue?

Hi,
When trying to run the tutorial code, I get an error in the line:

results = pde.core.integrate.integrate_times(
model=pde.core.models.FiniteDifferenceModel(equation,grid),
state=initial_state,
times=times, axis=0)

The error that I get is (I will also send it to the AutoGraph team as they requested) :

W0717 14:24:34.494019 140735880733568 ag_logging.py:145] Entity <function integrate_steps..advance_until_saved_step at 0xb4026f378> could not be transformed and will be executed as-is. Please report this to the AutgoGraph team. When filing the bug, set the verbosity to 10 (on Linux, export AUTOGRAPH_VERBOSITY=10) and attach the full output. Cause: converting <function integrate_steps..advance_until_saved_step at 0xb4026f378>: ValueError: inconsistent nodes: None (NoneType) and None (NoneType)
WARNING: Entity <function integrate_steps..advance_until_saved_step at 0xb4026f378> could not be transformed and will be executed as-is. Please report this to the AutgoGraph team. When filing the bug, set the verbosity to 10 (on Linux, export AUTOGRAPH_VERBOSITY=10) and attach the full output. Cause: converting <function integrate_steps..advance_until_saved_step at 0xb4026f378>: ValueError: inconsistent nodes: None (NoneType) and None (NoneType)

TypeError Traceback (most recent call last)
in
1 time_step = equation.get_time_step(grid)
2 times = time_step*np.arange(400)
----> 3 results = pde.core.integrate.integrate_times(model=pde.core.models.FiniteDifferenceModel(equation,grid), state=initial_state,times=times, axis=0)

~/Dropbox/MIT/projects/burgers_theta_updated/pde_superresolution_2d/datadrivenpdes/core/integrate.py in integrate_times(model, state, times, initial_time, axis, xla_compile)
133 'time step {}: {}'.format(times, dt, approx_steps))
134
--> 135 return integrate_steps(model, state, steps, initial_time, axis, xla_compile)

~/Dropbox/MIT/projects/burgers_theta_updated/pde_superresolution_2d/datadrivenpdes/core/integrate.py in integrate_steps(failed resolving arguments)
88 starts = tf.concat([[0], steps[:-1]], axis=0)
89 integrated = tf.scan(advance_until_saved_step, [starts, steps],
---> 90 initializer=evolving_state)
91
92 integrated_constants = nest.map_structure(

~/anaconda3/envs/pde_TF/lib/python3.7/site-packages/tensorflow/python/ops/functional_ops.py in scan(fn, elems, initializer, parallel_iterations, back_prop, swap_memory, infer_shape, reverse, name)
503 back_prop=back_prop,
504 swap_memory=swap_memory,
--> 505 maximum_iterations=n)
506
507 results_flat = [r.stack() for r in r_a]

~/anaconda3/envs/pde_TF/lib/python3.7/site-packages/tensorflow/python/ops/control_flow_ops.py in while_loop(cond, body, loop_vars, shape_invariants, parallel_iterations, back_prop, swap_memory, name, maximum_iterations, return_same_structure)
3461
3462 while cond(*loop_vars):
-> 3463 loop_vars = body(*loop_vars)
3464 if try_to_pack and not isinstance(loop_vars, (list, _basetuple)):
3465 packed = True

~/anaconda3/envs/pde_TF/lib/python3.7/site-packages/tensorflow/python/ops/control_flow_ops.py in (i, lv)
3454 cond = lambda i, lv: ( # pylint: disable=g-long-lambda
3455 math_ops.logical_and(i < maximum_iterations, orig_cond(*lv)))
-> 3456 body = lambda i, lv: (i + 1, orig_body(*lv))
3457
3458 if executing_eagerly:

~/anaconda3/envs/pde_TF/lib/python3.7/site-packages/tensorflow/python/ops/functional_ops.py in compute(i, a_flat, tas)
480 packed_elems = input_pack([elem_ta.read(i) for elem_ta in elems_ta])
481 packed_a = output_pack(a_flat)
--> 482 a_out = fn(packed_a, packed_elems)
483 nest.assert_same_structure(elems if initializer is None else initializer,
484 a_out)

~/anaconda3/envs/pde_TF/lib/python3.7/site-packages/tensorflow/python/eager/def_function.py in call(self, *args, **kwds)
412 # This is the first call of call, so we have to initialize.
413 initializer_map = {}
--> 414 self._initialize(args, kwds, add_initializers_to=initializer_map)
415 if self._created_variables:
416 try:

~/anaconda3/envs/pde_TF/lib/python3.7/site-packages/tensorflow/python/eager/def_function.py in _initialize(self, args, kwds, add_initializers_to)
355 self._concrete_stateful_fn = (
356 self._stateful_fn._get_concrete_function_internal_garbage_collected( # pylint: disable=protected-access
--> 357 *args, **kwds))
358
359 def invalid_creator_scope(*unused_args, **unused_kwds):

~/anaconda3/envs/pde_TF/lib/python3.7/site-packages/tensorflow/python/eager/function.py in _get_concrete_function_internal_garbage_collected(self, *args, **kwargs)
1347 if self.input_signature:
1348 args, kwargs = None, None
-> 1349 graph_function, _, _ = self._maybe_define_function(args, kwargs)
1350 return graph_function
1351

~/anaconda3/envs/pde_TF/lib/python3.7/site-packages/tensorflow/python/eager/function.py in _maybe_define_function(self, args, kwargs)
1650 graph_function = self._function_cache.primary.get(cache_key, None)
1651 if graph_function is None:
-> 1652 graph_function = self._create_graph_function(args, kwargs)
1653 self._function_cache.primary[cache_key] = graph_function
1654 return graph_function, args, kwargs

~/anaconda3/envs/pde_TF/lib/python3.7/site-packages/tensorflow/python/eager/function.py in _create_graph_function(self, args, kwargs, override_flat_arg_shapes)
1543 arg_names=arg_names,
1544 override_flat_arg_shapes=override_flat_arg_shapes,
-> 1545 capture_by_value=self._capture_by_value),
1546 self._function_attributes)
1547

~/anaconda3/envs/pde_TF/lib/python3.7/site-packages/tensorflow/python/framework/func_graph.py in func_graph_from_py_func(name, python_func, args, kwargs, signature, func_graph, autograph, autograph_options, add_control_dependencies, arg_names, op_return_value, collections, capture_by_value, override_flat_arg_shapes)
713 converted_func)
714
--> 715 func_outputs = python_func(*func_args, **func_kwargs)
716
717 # invariant: func_outputs contains only Tensors, CompositeTensors,

~/anaconda3/envs/pde_TF/lib/python3.7/site-packages/tensorflow/python/eager/def_function.py in wrapped_fn(*args, **kwds)
305 # wrapped allows AutoGraph to swap in a converted function. We give
306 # the function a weak reference to itself to avoid a reference cycle.
--> 307 return weak_wrapped_fn().wrapped(*args, **kwds)
308 weak_wrapped_fn = weakref.ref(wrapped_fn)
309

~/anaconda3/envs/pde_TF/lib/python3.7/site-packages/tensorflow/python/framework/func_graph.py in wrapper(*args, **kwargs)
703 except Exception as e: # pylint:disable=broad-except
704 if hasattr(e, "ag_error_metadata"):
--> 705 raise e.ag_error_metadata.to_exception(type(e))
706 else:
707 raise

TypeError: in converted code:
relative to /Users/yani:

Dropbox/MIT/projects/burgers_theta_updated/pde_superresolution_2d/datadrivenpdes/core/integrate.py:80 advance_until_saved_step
    while i < stop:
anaconda3/envs/pde_TF/lib/python3.7/site-packages/tensorflow/python/framework/ops.py:690 __bool__
    raise TypeError("Using a `tf.Tensor` as a Python `bool` is not allowed. "

TypeError: Using a `tf.Tensor` as a Python `bool` is not allowed. Use `if t is not None:` instead of `if t:` to test if a tensor is defined, and use TensorFlow ops such as tf.cond to execute subgraphs conditioned on the value of a tensor.

I use Python 3.7.3, and the packages versions that I use are:

absl-py==0.7.1
apache-beam==2.13.0
appnope==0.1.0
astor==0.8.0
attrs==19.1.0
avro-python3==1.9.0
backcall==0.1.0
bleach==3.1.0
certifi==2019.6.16
chardet==3.0.4
crcmod==1.7
cycler==0.10.0
decorator==4.4.0
defusedxml==0.6.0
dill==0.2.9
docopt==0.6.2
entrypoints==0.3
fastavro==0.21.24
future==0.17.1
gast==0.2.2
google-pasta==0.1.7
grpcio==1.22.0
h5py==2.9.0
hdfs==2.5.8
httplib2==0.12.0
idna==2.8
ipykernel==5.1.1
ipython==7.6.1
ipython-genutils==0.2.0
ipywidgets==7.5.0
jedi==0.13.3
Jinja2==2.10.1
jsonschema==3.0.1
jupyter==1.0.0
jupyter-client==5.3.1
jupyter-console==6.0.0
jupyter-core==4.5.0
Keras-Applications==1.0.8
Keras-Preprocessing==1.1.0
kiwisolver==1.1.0
Markdown==3.1.1
MarkupSafe==1.1.1
matplotlib==3.1.0
mistune==0.8.4
mkl-fft==1.0.12
mkl-random==1.0.2
mock==2.0.0
nbconvert==5.5.0
nbformat==4.4.0
notebook==5.7.8
numpy==1.16.4
oauth2client==3.0.0
pandas==0.24.2
pandocfilters==1.4.2
parso==0.5.0
pbr==5.4.0
pexpect==4.7.0
pickleshare==0.7.5
prometheus-client==0.7.1
prompt-toolkit==2.0.9
protobuf==3.9.0
ptyprocess==0.6.0
pyarrow==0.13.0
pyasn1==0.4.5
pyasn1-modules==0.2.5
pydot==1.2.4
Pygments==2.4.2
pyparsing==2.4.0
pyrsistent==0.14.11
python-dateutil==2.8.0
pytz==2019.1
PyYAML==3.13
pyzmq==18.0.0
qtconsole==4.5.1
requests==2.22.0
rsa==4.0
scipy==1.2.1
Send2Trash==1.5.0
six==1.12.0
tensorboard==1.14.0
tensorflow==1.14.0
tensorflow-estimator==1.14.0
termcolor==1.1.0
terminado==0.8.2
testpath==0.4.2
tornado==6.0.3
traitlets==4.3.2
urllib3==1.25.3
wcwidth==0.1.7
webencodings==0.5.1
Werkzeug==0.15.4
widgetsnbextension==3.5.0
wrapt==1.11.2
xarray==0.12.3
xgboost==0.90

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.