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 (the default), or Result table to define the interpolation function by entering values in a table or by importing interpolation data from a file or from a table under Results, 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, remove a row from the table, and clear the table using the buttons underneath 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 .txt. 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 text (.txt) file to load and click Open. 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.
-
Optional (for functions defined by a local table only): 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.
-
Optional (for 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.
If File is selected to import interpolation data from a file, select a Data format: Spreadsheet, Grid, or Sectionwise. If the license includes LiveLink™ for Excel®, you can also import interpolation data from a Microsoft® Excel Workbook spreadsheet. 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. Then 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.
-
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 is treated in the same way as a file using the spreadsheet format.
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 may 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. 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
Spreadsheet File
A spreadsheet file contains unstructured mesh and function data for space-dependent functions or general input variables and function values for functions of one or more variables.
     %Header (optional)
     Columns containing x, y (optional), and z (optional), or any other arguments, followed by function data columns.
The number of arguments is equal to the number of columns minus one, but there cannot be more than 3 arguments.
Sectionwise File
A sectionwise file has coordinates and function values.
     %Coordinates
     One to three columns containing x, y (optional), and
     z (optional)
     %Elements
     Triangulation where each row contains the row indices of the
     points in the Coordinates section that make up one element
     (triangular in 2D, tetrahedral in 3D)
     %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
     %Grid
     x grid points separated by spaces
     y grid points separated by spaces (optional)
     z grid points separated by spaces (optional)
     %Data
     Data values separated by spaces
Each row contains values for different x grid points for fixed values of  y and z. 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.
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.
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.
Plot Parameters
If you have selected to define the inverse function, 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).
Random Sampling
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. A 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.