# discretize.utils.volume_average¶

`discretize.utils.``volume_average`(mesh_in, mesh_out, values=None, output=None)[source]

Volume averaging interpolation between meshes.

This volume averaging function looks for overlapping cells in each mesh, and weights the output values by the partial volume ratio of the overlapping input cells. The volume average operation should result in an output such that `np.sum(mesh_in.cell_volumes*values)` = `np.sum(mesh_out.cell_volumes*output)`, when the input and output meshes have the exact same extent. When the output mesh extent goes beyond the input mesh, it is assumed to have constant values in that direction. When the output mesh extent is smaller than the input mesh, only the overlapping extent of the input mesh contributes to the output.

This function operates in three different modes. If only `mesh_in` and `mesh_out` are given, the returned value is a `scipy.sparse.csr_matrix` that represents this operation (so it could potentially be applied repeatedly). If `values` is given, the volume averaging is performed right away (without internally forming the matrix) and the returned value is the result of this. If `output` is given as well, it will be filled with the values of the operation and then returned (assuming it has the correct `dtype`).

Parameters
mesh_inTensorMesh or TreeMesh

Input mesh (the mesh you are interpolating from)

mesh_outTensorMesh or TreeMesh

Output mesh (the mesh you are interpolating to)

valuesnumpy.ndarray, optional

Array with values defined at the cells of `mesh_in`

outputnumpy.ndarray, optional

Output array to be overwritten of length `mesh_out.nC` and type `np.float64`

Returns
scipy.sparse.csr_matrix or numpy.ndarray

If `values` is `None`, the returned value is a matrix representing this operation, otherwise it is a numpy.ndarray of the result of the operation.

Examples

Create two meshes with the same extent, but different divisions (the meshes do not have to be the same extent).

```>>> import numpy as np
>>> from discretize import TensorMesh
>>> h1 = np.ones(32)
>>> h2 = np.ones(16)*2
>>> mesh_in = TensorMesh([h1, h1])
>>> mesh_out = TensorMesh([h2, h2])
```

Create a random model defined on the input mesh, and use volume averaging to interpolate it to the output mesh.

```>>> from discretize.utils import volume_average
>>> model1 = np.random.rand(mesh_in.nC)
>>> model2 = volume_average(mesh_in, mesh_out, model1)
```

Because these two meshes’ cells are perfectly aligned, but the output mesh has 1 cell for each 4 of the input cells, this operation should effectively look like averaging each of those cells values

```>>> import matplotlib.pyplot as plt
>>> plt.figure()
>>> ax1 = plt.subplot(121)
>>> mesh_in.plot_image(model1, ax=ax1)
>>> ax2 = plt.subplot(122)
>>> mesh_out.plot_image(model2, ax=ax2)
>>> plt.show()
```
{"ft_lang_label":"__label__en","ft_lang_prob":0.7438971,"math_prob":0.94759446,"size":2808,"snap":"2021-04-2021-17","text_gpt3_token_len":639,"char_repetition_ratio":0.14621969,"word_repetition_ratio":0.014888338,"special_character_ratio":0.2332621,"punctuation_ratio":0.13967611,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99737495,"pos_list":[0,1,2],"im_url_duplicate_count":[null,2,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-04-19T21:39:57Z\",\"WARC-Record-ID\":\"<urn:uuid:7b5760e1-0c85-4289-ab53-0eeeb0376ea6>\",\"Content-Length\":\"22448\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:81f9831b-c8a8-4614-93ae-7988160adbaa>\",\"WARC-Concurrent-To\":\"<urn:uuid:be9d3911-4edc-441e-98d7-e4175592ec32>\",\"WARC-IP-Address\":\"185.199.109.153\",\"WARC-Target-URI\":\"http://discretize.simpeg.xyz/en/master/api/generated/discretize.utils.volume_average.html\",\"WARC-Payload-Digest\":\"sha1:TLIVJMMORZRWCB2N2IWNEZSGQ7JPSMUC\",\"WARC-Block-Digest\":\"sha1:T3W6VH72R6FP2URDYTMIL7CUFSMQQJC2\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-17/CC-MAIN-2021-17_segments_1618038917413.71_warc_CC-MAIN-20210419204416-20210419234416-00302.warc.gz\"}"}