Example #1
0
 public static void test5() {
   MathFunc f = FX.x * FX.y * FX.z + 1;
   System.out.println(f);
   System.out.println(f.getVarNames());
   System.out.println(f.apply(2, 3, 4));
   CompiledFunc cf = f.compile();
   System.out.println(cf.apply(2, 3, 4));
 }
Example #2
0
  public static void test1() {
    MathFunc x = FX.x;
    MathFunc y = FX.y;
    MathFunc xy = x.S(y);

    FuncClassLoader<CompiledFunc> fcl = new FuncClassLoader<CompiledFunc>();
    ClassGen genClass = BytecodeUtils.genClass(xy, null, "add", true, false);
    CompiledFunc fxy = fcl.newInstance(genClass);
    System.out.println(fxy.apply(1.0, 2.0));

    System.out.println(xy.compile(new String[] {"y", "x"}).apply(1.0, 2.0));
  }
Example #3
0
  public static void test7() {
    FSin sin = new FSin();
    MathFunc fun = FMath.pow(sin, 2);
    System.out.println(fun.toString());
    System.out.println(fun.diff("x"));

    System.out.println(FMath.sqrt(sin));
    System.out.println(FMath.sqrt(sin).diff("x"));

    FSqrt sqrt = new FSqrt();
    System.out.println(sqrt.compile().apply(16));
    System.out.println(FMath.sqrt(FX.x).compile().apply(16));
  }
Example #4
0
  public static void test4() {
    FSin sin = new FSin("x");
    FCos cos = new FCos("y");
    MathFunc f = sin * cos;
    System.out.println(f);
    System.out.println(f.getVarNames());
    System.out.println(f.apply(Math.PI / 2, Math.PI / 4));

    CompiledFunc cf = f.compile();
    System.out.println(cf.apply(Math.PI / 2, Math.PI / 4));
    // bug in order of args
    System.out.println(f.compile(new String[] {"y", "x"}).apply(Math.PI / 2, Math.PI / 4));
  }
  /**
   * 构造下列形函数中的一个: N1 = L1 = r N2 = L2 = s N3 = L3 = t
   *
   * @param funID = 1,2,3
   */
  public void Create(int funID, double coef) {
    funIndex = funID - 1;
    if (funID < 1 || 3 < funID) {
      throw new FutureyeException("ERROR: funID should be 1,2 or 3.");
    }

    this.varNames = new String[] {"r", "s", "t"};
    innerVarNames = new ObjList<String>("x", "y");

    // Compose function: r = r(x,y), s = s(x,y), t = t(x,y)
    Map<String, MathFunc> fInners = new HashMap<String, MathFunc>();
    final String varName = varNames[funIndex];
    fInners.put(
        varName,
        new AbstractMathFunc(innerVarNames.toList()) {
          public MathFunc diff(String var) {
            if (area < 0.0) {
              throw new FutureyeException("Check nodes order: area < 0.0");
            } else {
              if (varName.equals("r")) { // r对应三角形高h的负倒数
                if (var.equals("x")) return new FC(b[0] / (2 * area));
                if (var.equals("y")) return new FC(c[0] / (2 * area));
              } else if (varName.equals("s")) { // s对应三角形高h的负倒数
                if (var.equals("x")) return new FC(b[1] / (2 * area));
                if (var.equals("y")) return new FC(c[1] / (2 * area));
              } else if (varName.equals("t")) { // t对应三角形高h的负倒数
                if (var.equals("x")) return new FC(b[2] / (2 * area));
                if (var.equals("y")) return new FC(c[2] / (2 * area));
              }
            }
            return null;
          }

          @Override
          public double apply(double... args) {
            throw new UnsupportedOperationException();
          }
        });

    // 使用复合函数构造形函数
    funOuter = new SF123();
    this.coef = coef;
    funCompose = FC.c(this.coef).M(funOuter.compose(fInners));
    funCompose.setActiveVarNames(funOuter.getVarNames());
  }
  public static void testSFBilinearLocal2D() {
    NodeList nodes = new NodeList();
    nodes.add(new Node(1, -2.0, -2.0));
    nodes.add(new Node(2, 2.0, -2.0));
    nodes.add(new Node(3, 2.0, 2.0));
    nodes.add(new Node(4, -2.0, 2.0));
    Element e = new Element(nodes);

    System.out.println("Test coordinate transform and Jacbian on element " + e);
    CoordinateTransform trans = new CoordinateTransform(2);
    trans.transformLinear2D(e);
    trans.computeJacobianMatrix();
    trans.computeJacobian2D();
    MathFunc jac = trans.getJacobian();
    Variable v = new Variable();
    v.set("r", 0);
    v.set("s", 0);
    check(jac.toString(), jac.apply(v), 4.0);

    ScalarShapeFunction[] shapeFun = new ScalarShapeFunction[4];
    shapeFun[0] = new SFBilinearLocal2D(1);
    shapeFun[1] = new SFBilinearLocal2D(2);
    shapeFun[2] = new SFBilinearLocal2D(3);
    shapeFun[3] = new SFBilinearLocal2D(4);

    System.out.println("Test the evaluation of SFBilinearLocal2D:");
    double[] args = new double[] {0.1, 0.1};
    Variable v0 = new Variable();
    v0.set("r", args[0]);
    v0.set("s", args[1]);
    System.out.println(shapeFun[0]);
    check(shapeFun[0].toString(), shapeFun[0].apply(v0), 0.2025);
    check(shapeFun[0].toString(), shapeFun[0].apply(args), 0.2025);
    check(shapeFun[0].toString(), shapeFun[0].compile().apply(args), 0.2025);

    System.out.println("Test the derivatives of shape function SFBilinearLocal2D:");
    e.updateJacobin();
    double[] rlt = new double[] {-0.125, -0.125, 0.125, -0.125, 0.125, 0.125, -0.125, 0.125};
    int j = 0;
    for (int i = 0; i < 4; i++) {
      System.out.println(shapeFun[i]);
      shapeFun[i].assignElement(e);
      MathFunc SFdx = shapeFun[i].diff("x");
      MathFunc SFdy = shapeFun[i].diff("y");
      check("shapeFun[" + i + "].diff(\"x\")=" + SFdy, SFdx.apply(v), rlt[j++]);
      check("shapeFun[" + i + "].diff(\"y\")=" + SFdy, SFdy.apply(v), rlt[j++]);
    }
  }
Example #7
0
  public static void test2() {
    MathFunc x = FX.x;
    MathFunc y = FX.y;
    MathFunc xy = x.M(y);
    System.out.println(xy);
    HashMap<String, MathFunc> map = new HashMap<String, MathFunc>();
    map.put(x.getVarNames().get(0), FX.r.A(FX.s));
    map.put(y.getVarNames().get(0), FX.r.S(FX.s));
    MathFunc xy2 = xy.compose(map);
    System.out.println(xy2);

    System.out.println(FX.r.A(FX.s).M(FX.r.S(FX.s)));

    FuncClassLoader<CompiledFunc> fcl = new FuncClassLoader<CompiledFunc>();
    ClassGen genClass = BytecodeUtils.genClass(xy2, null, "add2", true, false);
    CompiledFunc fxy2 = fcl.newInstance(genClass);
    System.out.println(fxy2.apply(4.0, 2.0));
  }
 public String toString() {
   if (this.coef < 1.0) return this.coef + "*" + funOuter.toString();
   else return funOuter.toString();
 }
 @Override
 public double[] applyAll(VariableArray v, Map<Object, Object> cache) {
   return funCompose.applyAll(v, cache);
 }
 public double apply(Variable v) {
   return funCompose.apply(v);
 }
 public MathFunc diff(String varName) {
   return funCompose.diff(varName);
 }
  /**
   * 构造下列形函数中的一个: Ni = (1+r*ri)*(1+s*si)*(1+t*ti)/8 where (ri,si,ti),i=1,...,8 are vertices
   * coordinate of the hexahedron
   *
   * @param funID = 1,...,8
   */
  public void Create(int funID, double coef) {
    funIndex = funID - 1;
    if (funID < 1 || funID > 8) {
      System.out.println("ERROR: funID should be 1,...,8.");
      return;
    }

    varNames.add("r");
    varNames.add("s");
    varNames.add("t");
    innerVarNames = new ObjList<String>("x", "y", "z");

    // 复合函数
    Map<String, MathFunc> fInners = new HashMap<String, MathFunc>(4);

    for (final String varName : varNames) {
      fInners.put(
          varName,
          new AbstractMathFunc(innerVarNames.toList()) {

            // r_x,r_y,r_z, s_x,s_y,s_z, t_x,t_y,t_z
            public MathFunc diff(String var) {
              /**
               * f(x,y,z) = g(r,s,t) f_x = g_r*r_x + g_s*s_x + g_t*t_x ---(1) f_y = g_r*r_y +
               * g_s*s_y + g_t*t_y ---(2) f_z = g_r*r_z + g_s*s_z + g_t*t_z ---(3)
               *
               * <p>for (1) let f=x,f=y,f=z we get 3 equations, solve them: (x_r x_s x_t) (r_x) (1)
               * (y_r y_s y_z) * (s_x) = (0) (z_r z_s z_t) (t_x) (0)
               *
               * <p>similarly, for (2): (x_r x_s x_t) (r_y) (0) (y_r y_s y_z) * (s_y) = (1) (z_r z_s
               * z_t) (t_y) (0)
               *
               * <p>for (3): (x_r x_s x_t) (r_z) (0) (y_r y_s y_z) * (s_z) = (0) (z_r z_s z_t) (t_z)
               * (1)
               *
               * <p>(x_r x_s x_t) Let J = (y_r y_s y_z) (z_r z_s z_t)
               *
               * <p>from the above 9 equations, we have: (r_x r_y r_z) (s_x s_y s_z) = inv(J) (t_x
               * t_y t_z)
               */
              return new InvJ(varName, var);
            }

            @Override
            public double apply(Variable v) {
              // TODO Auto-generated method stub
              return 0;
            }
          });
    }

    //		funOuter = new FAxpb("r",vt[funIndex][0]/2.0,0.5).M(
    //				   new FAxpb("s",vt[funIndex][1]/2.0,0.5)).M(
    //				   new FAxpb("t",vt[funIndex][2]/2.0,0.5));
    // 速度提高1倍
    funOuter = new FOuter(varNames, funIndex);

    // 使用复合函数构造形函数
    this.coef = coef;
    funCompose = FC.c(this.coef).M(funOuter.compose(fInners));
  }
  public static void testSFLinearLocal2D() {
    NodeList nodes = new NodeList();
    nodes.add(new Node(1, 0.0, 0.0));
    nodes.add(new Node(2, 0.2, 0.0));
    nodes.add(new Node(3, 0.0, 0.2));
    Element e = new Element(nodes);

    System.out.println("Test coordinate transform and Jacbian on element " + e);
    CoordinateTransform trans = new CoordinateTransform(2);
    trans.transformLinear2D(e);
    trans.computeJacobianMatrix();
    trans.computeJacobian2D();
    MathFunc jac = trans.getJacobian();
    Variable v = new Variable();
    v.set("r", 0);
    v.set("s", 0);
    check(jac.toString(), jac.apply(v), 0.04);

    ScalarShapeFunction[] shapeFun = new ScalarShapeFunction[3];
    shapeFun[0] = new SFLinearLocal2DRS(1);
    shapeFun[1] = new SFLinearLocal2DRS(2);
    shapeFun[2] = new SFLinearLocal2DRS(3);

    System.out.println("Test the derivatives of shape function SFLinearLocal2DRS:");
    double[] rlt = new double[] {-5, -5, 5, 0, 0, 5};
    int j = 0;
    for (int i = 0; i < 3; i++) {
      System.out.println(shapeFun[i]);
      shapeFun[i].assignElement(e);
      MathFunc SFdx = shapeFun[i].diff("x");
      MathFunc SFdy = shapeFun[i].diff("y");
      check("shapeFun[" + i + "].diff(\"x\")", SFdx.apply(), rlt[j++]);
      check("shapeFun[" + i + "].diff(\"y\")", SFdy.apply(), rlt[j++]);
    }

    System.out.println("Test the derivatives of shape function SFLinearLocal2D:");
    shapeFun[0] = new SFLinearLocal2D(1);
    shapeFun[1] = new SFLinearLocal2D(2);
    shapeFun[2] = new SFLinearLocal2D(3);
    j = 0;
    for (int i = 0; i < 3; i++) {
      System.out.println(shapeFun[i]);
      shapeFun[i].assignElement(e);
      MathFunc SFdx = shapeFun[i].diff("x");
      MathFunc SFdy = shapeFun[i].diff("y");
      check("shapeFun[" + i + "].diff(\"x\")=" + SFdy, SFdx.apply(v), rlt[j++]);
      check("shapeFun[" + i + "].diff(\"y\")=" + SFdy, SFdy.apply(v), rlt[j++]);
    }
  }
Example #14
0
 public String toString() {
   return "N" + (funIndex + 1) + "(r,s,t) = " + funOuter.getExpr();
 }