Converting Interpolation Curve Data
The following method converts a geometry Interpolation Curve to an Interpolation function (by creating an interpolation table). The method demonstrates extracting geometry information from the underlying parameterization of an edge. The edge does not have to be an Interpolation Curve but can be any single edge.
The curve is checked for being a function curve with monotonously growing x coordinates, which is required in order to be able to convert to an interpolation function. Note that since an interpolation curve is represented using splines, even though the interpolation points form a monotonous sequence, the resulting curve may not; hence, the second consistency check (the first check can potentially be skipped).
// Convert using N points
int N = 100;
double monoTol = 1e-6;
int edgeNum=1;
 
// Update and get geometry information
model.component("comp1").geom("geom1").run("fin");
GeomSequence geom1 = model.component("comp1").geom("geom1");
GeomFeature ic1 = geom1.feature("ic1");
double[][] curvePoints = ic1.getDoubleMatrix("table");
 
int len = curvePoints.length;
double minX = curvePoints[0][0];
double maxX = curvePoints[len-1][0];
double scale = maxX-minX;
double scaledTol = monoTol*scale;
 
for (int i = 1; i < len; i++) {
  if ((curvePoints[i][0]-curvePoints[i-1][0]) < scaledTol) {
    error("Curve needs to be a function curve with monotonously growing x       coordinates.");
  }
}
 
double minMaxS[] = geom1.edgeParamRange(edgeNum);
double minS = minMaxS[0];
double maxS = minMaxS[1];
 
double sList[] = new double[N];
for (int k = 0; k < N; k++) {
  sList[k] = (double) (N-1-k)/(double) (N-1)*minS+k/(double) (N-1)*maxS;
}
double[][] XY = geom1.edgeX(1, sList);
 
for (int j = 1; j < N; j++) {
  if ((XY[j][0]-XY[j-1][0]) < scaledTol) {
    error("Curve needs to be a function curve with monotonously growing x       coordinates.");
  }
}
 
// Create interpolation table
model.func().create("int1", "Interpolation");
with(model.func("int1"));
  set("funcname", "int1");
  set("interp", "cubicspline");
  set("extrap", "linear");
endwith();
 
model.func("int1").set("table", toString(XY));
Comments
The method assumes that there is a geometry sequence geom1 with an interpolation curve ic1. It further assumes that there are no other geometry features and that the geometry object has a single edge. The integer N determines how granular the interpolation table should be. It is assumed that there is only one edge in the geometry sequence (edgeNum). Note that the curve parameter range may not be the unit interval (minS does not have to be 0.0 and maxS does not have to be 1.0). To run the method more than once, you can create a cleanup method that contains the lines:
model.func().remove("int1");
model.result().remove("pg1");
for removing previously created model tree nodes.
This example is part of a collection available for download:
www.comsol.com/model/application-programming-guide-examples-140771
The relevant file for this example is:
converting_curve_to_function.mph