.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/basics/ex1_7_spatavg_therm2d.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_examples_basics_ex1_7_spatavg_therm2d.py: Basics: Sensor spatial averaging and averaging errors ================================================================================ In this example we show how `pyvale` can simulate sensor spatial averaging for ground truth calculations as well as for calculating systematic errors. Test case: Scalar field point sensors (thermocouples) on a 2D thermal simulation .. GENERATED FROM PYTHON SOURCE LINES 16-22 .. code-block:: Python import numpy as np import matplotlib.pyplot as plt import mooseherder as mh import pyvale as pyv .. GENERATED FROM PYTHON SOURCE LINES 23-32 First we are going to build a custom sensor array so we can control how the ground truth is extracted for a sensor using area averaging. Note that the default is an ideal point sensor with no spatial averaging. Later we will add area averaging as a systematic error. Note that it is possible to have an ideal point sensor with no area averaging for the truth and then add an area averaging error. It is also possible to have a truth that is area averaged without and area averaging error. The first part of this is the same as the 3D thermal example we have used previously then we control the area averaging using the sensor data object. .. GENERATED FROM PYTHON SOURCE LINES 32-53 .. code-block:: Python data_path = pyv.DataSet.thermal_2d_path() sim_data = mh.ExodusReader(data_path).read_all_sim_data() sim_data = pyv.scale_length_units(scale=1000.0, sim_data=sim_data, disp_comps=None) descriptor = pyv.SensorDescriptorFactory.temperature_descriptor() field_key = "temperature" t_field = pyv.FieldScalar(sim_data, field_key=field_key, elem_dims=2) n_sens = (4,1,1) x_lims = (0.0,100.0) y_lims = (0.0,50.0) z_lims = (0.0,0.0) sens_pos = pyv.create_sensor_pos_array(n_sens,x_lims,y_lims,z_lims) sample_times = np.linspace(0.0,np.max(sim_data.time),50) # | None .. GENERATED FROM PYTHON SOURCE LINES 54-61 This is where we control the setup of the area averaging. We need to specify the sensor dimensions and the type of numerical spatial integration to use. Here we specify a square sensor in x and y with 4 point Gaussian quadrature integration. It is worth noting that increasing the number of integration points will increase computational cost as each additional integration point requires an additional interpolation of the physical field. .. GENERATED FROM PYTHON SOURCE LINES 61-67 .. code-block:: Python sensor_dims = np.array([20.0,20.0,0]) # units = mm sensor_data = pyv.SensorData(positions=sens_pos, sample_times=sample_times, spatial_averager=pyv.EIntSpatialType.QUAD4PT, spatial_dims=sensor_dims) .. GENERATED FROM PYTHON SOURCE LINES 68-70 We have added spatial averaging to our sensor data so we can now create our sensor array as we have done in previous examples. .. GENERATED FROM PYTHON SOURCE LINES 70-74 .. code-block:: Python tc_array = pyv.SensorArrayPoint(sensor_data, t_field, descriptor) .. GENERATED FROM PYTHON SOURCE LINES 75-78 We are also going to create a field error that includes area averaging as an error. We do this by adding the option to our field error data class specifying rectangular integration with 1 point. .. GENERATED FROM PYTHON SOURCE LINES 78-83 .. code-block:: Python area_avg_err_data = pyv.ErrFieldData( spatial_averager=pyv.EIntSpatialType.RECT1PT, spatial_dims=np.array((5.0,5.0)), ) .. GENERATED FROM PYTHON SOURCE LINES 84-87 We add the field error to our error chain as normal. We could combine it with any of our other error models but we will isolate it for now so we can see what it does. .. GENERATED FROM PYTHON SOURCE LINES 87-95 .. code-block:: Python err_chain = [] err_chain.append(pyv.ErrSysField(t_field, area_avg_err_data)) error_int = pyv.ErrIntegrator(err_chain, sensor_data, tc_array.get_measurement_shape()) tc_array.set_error_integrator(error_int) .. GENERATED FROM PYTHON SOURCE LINES 96-97 Now we run our sensor simulation to see how spatial averaging changes our .. GENERATED FROM PYTHON SOURCE LINES 97-117 .. code-block:: Python measurements = tc_array.calc_measurements() print(80*"-") sens_print = 0 comp_print = 0 time_last = 5 time_print = slice(measurements.shape[2]-time_last,measurements.shape[2]) print(f"These are the last {time_last} virtual measurements of sensor " + f"{sens_print}:") pyv.print_measurements(tc_array,sens_print,comp_print,time_print) print(80*"-") pyv.plot_time_traces(tc_array,field_key) plt.show() .. GENERATED FROM PYTHON SOURCE LINES 118-125 From here you now have everything you need to build your own sensor simulations for scalar field sensors using pyvale. In the next examples we will look at sensors applied to vector (e.g. displacement) and tensor fields (e.g. strain). If you don't need to sample vector or tensor fields then skip ahead to the examples on experiment simulation where you will learn how to perform Monte-Carlo sensor uncertainty quantification simulations and to analyse the results with pyvale. .. _sphx_glr_download_examples_basics_ex1_7_spatavg_therm2d.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: ex1_7_spatavg_therm2d.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: ex1_7_spatavg_therm2d.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: ex1_7_spatavg_therm2d.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_