Interpolation
An Interpolation function () is defined by a table or file containing the values of the function in discrete points. The interpolation data can be structured (defined on a grid) or unstructured (defined on a generic point cloud).
Definition
Select a Data sourceFile, Local table, Result table, or Function to define the interpolation function by entering values in a table, by importing interpolation data from a file or from a table under Results, or using another function respectively.
If Local table is selected, enter a Function name and enter coordinates t and function values f(t) into the table cells. A function of one variable can be defined in this way. For functions of two or more variables, such as space-dependent data in 2D and 3D, use a file with the function data. The default Function name is int1. You can move rows up and down in the table, insert a row above the selected rows, remove a row from the table, and clear the table using the buttons underneath the table or by right-clicking in the table.
-
Optional: Save the parameters to a text file to reuse in other models. Click the Save to File button () and enter a File name, including the extension. You can save files in the .txt, .csv, and .dat formats. If the license includes LiveLink™ for Excel® you can also save interpolation data to a Microsoft Excel Workbook spreadsheet. Click to Save the text file. The information is saved in space-separated columns in the same order as displayed on screen.
-
Optional: Import or Load data from a spreadsheet program or other format by saving the file with a .txt extension. Data must be separated by tabs or colons. Click the Load from File button () and navigate to the file to load and click Open. You can also click the downward arrow beside the Load from File button and choose Load From () to open the fullscreen Select File window. You can load files in the .txt, .csv, and .dat formats. If the license includes LiveLink™ for Excel® you can also load interpolation data from a Microsoft Excel Workbook spreadsheet. The data loads to the table. If there is other data already in the table, it adds it after the last row. Move or edit as needed. Trailing columns are removed.
If File is selected to import interpolation data from a file, select a Data format: Spreadsheet, Grid, or Sectionwise. COMSOL Multiphysics then uses the spreadsheet format and the Data format list is not available.
-
When the data format is specified, enter the complete network path and name of the interpolation data file in the Filename field, or click Browse to select a text or data file with interpolation data in the Interpolation Data dialog box. You can load files using the .txt, .csv, and .dat formats. You can also import WAVE audio files (*.wave files), in which case the interpolation function shows the number of audio channels and the sampling frequency. It is possible to define functions on all audio channels. If the license includes LiveLink™ for Excel®, you can also import interpolation data from a Microsoft® Excel Workbook spreadsheet. You can also click the downward arrow beside the Browse button and choose Browse From () to open the fullscreen Select File window. Click the downward arrow for the Location menu () to choose Show in Auxiliary Data () to move to the row for this file in the Auxiliary Data window, Copy Location (), and (if you have copied a file location) Paste Location ().
-
Also choose a decimal separator from the Decimal separator list: Point (the default) or Comma. Click Import () to import the interpolation data into the model; otherwise, COMSOL Multiphysics references the interpolation data on your file system. When you have imported the interpolation data, the Parameters section, under Data imported into model, contains information about the filename, data type, and dimension for the data. Click Export to save the interpolation data to a file and reference from there instead of including it in the model. Click the Discard button to delete the imported interpolation data from the model. Click the Refresh button () to ensure that the file is reread when needed.
-
From the Data format list select Spreadsheet, Grid, or Sectionwise. The spreadsheet format is the default format, and that format is the easiest to use for functions defined on an unstructured grid or for general tabulated function values with one or more arguments.
If Result table is selected, interpolation data evaluated or imported into a table under Results>Tables or belonging to an Evaluation Group under Results is treated in the same way as a file using the spreadsheet format.
If Function is selected, enter a Function name. From the Source function name list, choose a source function (all applicable functions in the model are available as source functions). The default Source function name is the name of the selected source function. From the Sampling list, choose Automatic (the default), or choose Uniform to use a uniform sampling with the number of sampling points given in the Sampling points field (default: 1000). The uniform sampling evaluates the function on a uniform grid with a set number of sampling points. The Automatic sampling is the same as a uniform sampling with 1000 sampling points. This data source makes it possible to reinterpolate existing functions. This functionality can be useful for creating primitive and inverse functions or for downsampling.
If the interpolation data is given on spreadsheet format and when using a result table, enter a Number of arguments (1–3). For all data formats and table sources, enter information about the functions into the table. Add a Function name and its Position in file. The first function in the file has position 1, the following has position 2, and so on. For spreadsheet data, the first columns contain the arguments (typically spatial coordinates); the following columns can contain one or more functions, and the positions entered are the relative position for each function’s data column.
For unstructured interpolation data, COMSOL Multiphysics may internally apply a scaling of the coordinates of the data points to simplify the process of creating an interpolation mesh. Use the Internal scaling of data points list to control this functionality. Select Automatic (the default) to apply the scaling if the bounding box of the interpolation points has a bad aspect ratio, On to always apply the scaling, or Off to turn off scaling altogether. Changing the scaling strategy can affect the generated interpolation mesh. The Internal scaling of data points list is only available when unstructured interpolation data can be entered (that is, when Spreadsheet or Result table is the data source).
For the common case where the data source contains function values that are functions of the spatial coordinates, select the Use spatial coordinates as arguments check box. Then select the frame to which the spatial coordinates are attached from the Frame list (the default is Spatial for the spatial frame). Then the function can be called without arguments when used in the model; the spatial coordinates are added as function arguments automatically. If you choose Mesh from the Frame list, you can select the Reinterpolate interpolation data on computational mesh and, if selected, enter a shape function order (default: 1) in the Lagrange shape function order for reinterpolation field. Doing so makes it possible to evaluate variables using Lagrange interpolation with cached values evaluated at Lagrange points on the computational mesh when using an interpolation function to define spatially dependent variables through spatial coordinates as arguments to the interpolation function. The Use spatial coordinates as arguments check box is available for Interpolation nodes in a Component branch when the Data source is File or when using a Table in 1D models.
An Example of Importing a File Data Source into a Parameter Table
The file named temp.txt contains temperature measurements in nine points in the plane:
10 3 310
20 3 309
30 3 314
10 6 302
20 6 307
30 6 311
10 9 307
20 9 308
30 9 314
The data columns contain x-coordinates, y-coordinates, and temperature values, respectively. To use this file as an interpolation function called tempfun, perform the following steps.
1
Select File from the Data source list.
2
Enter a Filename (the complete network path) or Browse to locate a file to import.
3
From the Data format list select Spreadsheet.
4
Enter a Number of arguments. In this example, enter 2.
5
Enter the Function name tempfun.
6
Enter its Position in file as 1. The first function in the file has position 1, the following has position 2, and so on. The position in file for a function is the column after the spatial coordinates (or other function arguments) where it is defined. In this example with two arguments (spatial coordinates), the third column is Position 1 in the file.
7
If desired, adjust the interpolation and extrapolation settings in the Interpolation and Extrapolation section (see below).
Use the function tempfun with x and y as input arguments in a 2D model to get the interpolated value for the temperature at any position. If the Use spatial coordinates as arguments check box is selected, use tempfun without adding the input arguments.
Examples of Spreadsheet, Sectionwise, and Grid File Formats
In all file formats, use a % (percent) sign to indicate a comment line. Values and indices can be separated by space, comma, semicolon, or tab characters.
Spreadsheet File
A spreadsheet file contains function data for space-dependent functions or general input variables and function values for functions of one or more variables (that is, the functions do not need to be functions of space coordinates but can also be functions of something else, such as temperature).
     %Header (optional)
     Columns containing x, y (optional), and z (optional), or any other arguments, followed by function data columns.
The number of input variable data columns can be 1, 2, or 3 with no limit on the number of subsequent function data columns.
Sectionwise File
A sectionwise file has coordinates and function values. The sectionwise format can be used for exporting results and for importing numerical data defined on an unstructured first-order tetrahedral mesh (triangular mesh in 2D) as interpolation table data. It is also possible to use this format in 1D and can then, contrary to the spreadsheet format, be used for 1D geometries with disconnected intervals. It cannot be used directly for defining a geometry or mesh. A sectionwise file contains unstructured mesh and function data for space-dependent functions. The functions need not be functions of space coordinates (x, y, and z) but can also be functions of some other quantities, such as deformation or temperature. The row indices in the format below are 1-based indices.
     %Coordinates
     One to three columns containing x, y (optional), and z (optional) coordinate values
     or other input variable values
     %Elements
     Triangulation where each row contains the row indices of the points in the Coordinates
     section for one element(edge, triangular, or tetrahedral)
     %Data (funname)
     Column of data values for each point
It is possible to include more than one function in the file as long as a %Data header separates them one from the other.
Grid File
A grid file contains data values on a grid. You supply the grid points as x coordinate values (in 1D), x and y coordinate values in 2D, and x, y, and z coordinate values in 3D.
     %Grid
     x grid points separated by spaces, commas, semicolons, or tab characters
     y grid points separated by spaces,commas, semicolons, or tab characters (optional)
     z grid points separated by spaces,commas, semicolons, or tab characters (optional)
     %Data
     Data values separated by spaces, commas, semicolons, or tab characters
The grid coordinate section lists the unique coordinate values. Each data row contains values for different x grid points for fixed values of  y and z (in 3D). The rows first increase the  y grid value and then the z grid value. The grid points can also represent another independent variable that the data values depend on. For example, the “grid points” can be temperature values and the data values the thermal conductivity at these temperatures.
It is possible to include more than one function in the file as long as a %Data header separates them one from the other. For grid points that are outside of the computational mesh, the data values will be NaN.
It is important to use a comment line starting with % to separate the grid points or other interpolation points and the data values that are associated with these coordinates or interpolation points.
Interpolation and Extrapolation
The interpolation and extrapolation settings control how the program evaluates the function between the discrete points where it is defined by the table or file, and the behavior of the function outside the domain where it is defined by the table or file.
Select an Interpolation method:
For functions of one variable select Nearest neighbor, Linear (the default interpolation method), Piecewise cubic, or Cubic spline.
-
Nearest neighbor interpolation selects the value of the nearest point where the function is defined.
-
Linear interpolation uses a linear polynomial to interpolate the function between the points where it is defined.
-
Piecewise cubic interpolation uses a piecewise-cubic Hermite polynomial with continuous first derivatives. It preserves the shape of the data and respects monotonicity.
-
The Cubic spline method also performs interpolation with a piecewise cubic polynomial. Here, even second derivatives are continuous; however, the method does not necessarily respect monotonicity.
For functions of more than one variable, select Nearest neighbor or Linear. The other options are not supported.
Select an Extrapolation method to specify how to treat arguments outside the grid or mesh of points.
None. No extrapolation outside the defined interpolation range.
Constant. Uses the value from the closest point inside the grid (for structured interpolation) or the value from the closest mesh element (for unstructured interpolation). The function evaluates the polynomial from the closest grid point at the actual point where a value is requested. This is the default extrapolation method.
Linear. The function is linear outside the grid with a continuous function value and continuous first derivative at the boundary of the grid. Piecewise cubic or Cubic spline must be selected from the Interpolation list.
Nearest function. Evaluates the polynomial from the closest grid point at the actual point where a value is requested.
Specific value. Uses a single value, such as zero or NaN (Not-a-Number), everywhere outside the grid or mesh. Enter the value in the Values outside range field.
Units
In this section you define the units of the function (the output) and the arguments (the inputs) in the Function and Argument table, respectively.
Data Transformation for Interpolation
By default, there is no data transformation, but you can optionally use logarithmic transformation for the interpolation function, where input data point positions (the arguments) or the data point values (the function) are transformed by a logarithm function before interpolating. The output data is then transformed back to the original space. From the Argument and Function lists, choose Logarithmic to perform logarithmic interpolation on the arguments or the function, respectively, or choose None (the default) for no transformation. In case of multiple arguments, the same logarithmic transformation is applied to all of them. For the presently supported interpolation methods, the base of the logarithm is irrelevant.
Plot Parameters
If you have selected to define the inverse function (see below), this section becomes available. Select the Plot the inverse function check box to plot the inverse function instead of the interpolation function itself when you click the Plot button () to generate a preview plot.
A plot group for an interpolation function contains three connected line graphs: a line graph of the piecewise function and two line graphs using dashed red lines, Left Extrapolation and Right Extrapolation, which show how the selected extrapolation extends the interpolation function on both sides. In addition, a point graph shows the interpolation points (interpolation nodes).
Related Functions
In this section, you can define the following related functions:
Inverse Function
The inverse function is available for interpolation functions defined by a local table only. If you want to define the inverse function f −1 for the interpolation function f, select the Define inverse function check box and enter a function name for the inverse function in the Inverse function name field (the default is the name of the interpolation function with a suffix _inv). If you want to plot the inverse function instead of the interpolation function itself, first select the Plot the inverse function check box in the Plot Parameters sections, which is only available when you have chosen to define the inverse function.
Primitive Function
If you want to define the primitive function F for the interpolation function f, select the Define primitive function check box and enter a function name for the primitive function in the Primitive function name field (the default is the name of the interpolation function with a suffix _prim). You can use the primitive function to sample from the interpolation function. The primitive function is the integral of the interpolation function. The integration constant is such that the value of the primitive function is zero at the first data point. In other words, if the interpolation function is f(x), then the primitive function is
where x0 is the x-coordinate of the first data point.
Random Function
To define a random function, select the Define random function check box with a name that you specify in the Random function name field (the default for an interpolation function int1 is rn_int1). You can use this function to sample from the interpolation function. Random functions can be useful in particle tracing, for example. Suppose, for example, that you want to specify an arbitrary energy distribution function for the initial energy of a bunch of particles. In order to do this, you need to randomly sample from an interpolation function, not merely call it. The random function makes this possible. To use a random function for this purpose, select the Define random function check box, and then call it in an Inlet feature, for example, using rn_int1(cpt.pidx), where cpt.pidx is a variable that is uniquely valued for each particle.
The default number of arguments is 1. If you want to use additional arguments, type a number in the Number of arguments field.
You specify the range of the random function using the Range list. The default is Automatic. Choose Manual to provide limits in the Lower limit and Upper limit fields.
See Common Settings for the Function Nodes for information about the Units section.
Acoustics Module, see Muffler with Perforates: Application Library path Acoustics_Module/Automotive/perforated_muffler.
CFD Module, see Transonic Flow in a Sajben Diffuser: Application Library path CFD_Module/High_Mach_Number_Flow/sajben_diffuser.
Corrosion Module, see Cathodic Protection of Steel in Reinforced Concrete: Application Library path Corrosion_Module/Cathodic_Protection/cathodic_protection_in_concrete.
Heat Transfer Module, see Temperature Field in a Cooling Flange: Application Library path Heat_Transfer_Module/Thermal_Processing/cooling_flange.
Pipe Flow Module, see Geothermal Heating from a Pond Loop: Application Library path Pipe_Flow_Module/Heat_Transfer/geothermal_heating.