Changeset 10834


Ignore:
Timestamp:
Mar 23, 2018 3:38:58 PM (21 months ago)
Author:
mjohnsson
Message:

#5577
Basic error checks for derivative functions

Location:
trunk/Compiler
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/Compiler/ModelicaFrontEnd/src/jastadd/errorcheck/TypeCheck.jrag

    r10802 r10834  
    4545    private static ErrorChecker ASTNode.TYPE_CHECKER = addErrorChecker(new ErrorChecker.TypeChecker());
    4646
     47
     48    public static final SimpleProblemProducer ASTNode.DERIVATIVE_ANNOTATION_PROBLEM =
     49            new SimpleErrorProducer("DERIVATIVE_ANNOTATION_PROBLEM", ProblemKind.SEMANTIC,
     50                    "The function %s and it's specified derivatives doesn't have compatible signatures.\n%s");
     51    public static final SimpleProblemProducer ASTNode.DERIVATIVE_FUNCTION_MISSMATCH =
     52            new SimpleErrorProducer("DERIVATIVE_FUNCTION_MISSMATCH", ProblemKind.SEMANTIC,"The function %s" +
     53                    "'s signature doesn't match the expected signature for a derivative to %s.\n" +
     54                    "  Expected %d %s but signature have %d %s.");
    4755    public static final SimpleProblemProducer ASTNode.CLASS_NOT_SUBTYPE_OF_CONSTRAINING_CLASS =
    4856            new SimpleErrorProducer("CLASS_NOT_SUBTYPE_OF_CONSTRAINING_CLASS", ProblemKind.SEMANTIC,
  • trunk/Compiler/ModelicaFrontEnd/src/jastadd/instance/Types.jrag

    r10802 r10834  
    324324        if (myDecl == null)
    325325            return null;
    326        
     326        if (getNumArg() > myDecl.myInputs().size()) {
     327            ASTNode.DERIVATIVE_ANNOTATION_PROBLEM.invoke(this, name(),
     328            "The derivative functions inputs must contain all the underived functions inputs.");
     329            breakOnErrors();
     330        }
    327331        Set<String> zeroDer = new HashSet<String>();
    328332        for (int i = 0, n = getNumArg(); i < n; i++) {
  • trunk/Compiler/ModelicaFrontEnd/test/modelica/FunctionTests.mo

    r10783 r10834  
    1338013380    function interpDer
    1338113381        input Real u;
     13382        input Real u_der;
    1338213383        output Real value;
    1338313384    algorithm
  • trunk/Compiler/ModelicaMiddleEnd/src/jastadd/structural/Differentiation.jrag

    r10022 r10834  
    531531        return (order == 1) ? new InstDerExp(use) : new InstHDerExp(use, order);
    532532    }
     533   
     534
     535    // Test DerivativeAnnotation
     536    public String FClass.checkDerivativeAnnotations(){
     537        //Check for all methods if it's derivative is correct based on available information
     538        for (FFunctionDecl ffunc: getFFunctionDecls()){
     539            ffunc.checkDerivative(ffunc.derivativeAnnotationUnrestricted(),0);
     540        }
     541       
     542        //Check for all FFunctionCall based on available information
     543        for (FAbstractEquation fae : getFAbstractEquations()) {
     544            for (ASTNode ast: fae.astChildren()) {
     545                if (ast instanceof FEquation) {
     546                    FExp fexp = ((FEquation)ast).getRight();
     547                    fexp.checkDerivative();
     548                }
     549                if (ast instanceof FFunctionCall) {
     550                    ((FFunctionCall) ast).checkDerivative();
     551                }
     552            }
     553        }
     554        try{
     555            return collectWarnings().toString();
     556        }catch (CompilerException e) {
     557          return e.getMessage(true).toString();
     558        }
     559    }
     560   
     561    //Helpers allowing checking of FunctionCalls.
     562    public Object[] FExp.checkDerivative(){
     563        return null;
     564    }
     565   
     566    /**
     567    * Called during FFunctionCall differentiation.
     568    * Check consistency between the declaration and this
     569    **/
     570    public Object[] FFunctionCall.checkDerivative(){
     571        FFunctionDecl myDecl = myFCallable().asFFunctionDecl();
     572        // 1. Exist
     573        if (myDecl == null)
     574            throw new FunctionDifferentiationException(this);   
     575       
     576        /** 2. Correct amount of arguments inputs arguments check.
     577        * Needed for generated derivatives in order to make
     578        * the derivativeAnnotation() work.
     579        **/
     580       
     581        if (getNumArg() > myDecl.myInputs().size()) {
     582            ASTNode.DERIVATIVE_ANNOTATION_PROBLEM.invoke(this, name(),
     583            "The derivative functions inputs must contain all the underived functions inputs.");
     584            breakOnErrors();
     585        }
     586       
     587        AnnotationNode derNode = derivativeAnnotation();
     588        myDecl.checkDerivative(derNode, derivands);
     589        return new Object[]{myDecl, derNode};
     590    }
     591
     592    /** Check specified derivative of first order for signature compatability.
     593    /* the derivands argument provide the needed information from upstream regarding
     594    /* the input which are to be derivated.
     595    **/
     596    public void FFunctionDecl.checkDerivative(AnnotationNode derNode, int derivands) {
     597        FFunctionDecl func = derivativeFunction(derNode);
     598        if (func == null)
     599            return;
     600        Set<String> exclude = new HashSet<String>();
     601        int myInputs = myInputs().size();
     602        exclude.addAll(zeroDerivative(derNode));
     603        exclude.addAll(noDerivative(derNode));
     604        int noDerVars = noDerivative(derNode).size() + zeroDerivative(derNode).size();
     605        if (derivands != 0) {
     606            noDerVars = 0;
     607            for (int j = myInputs - 1; j >= myInputs - derivands; j--) {
     608                if (exclude.contains(myInput(j).name())) {
     609                    noDerVars++;
     610                }
     611            }
     612        }
     613        int myInputDer = derivands;
     614        if (derivands == 0) {
     615            for (FFunctionVariable v : myInputs()) {
     616                if (v.isReal() || v.type().containsReal()) {
     617                    myInputDer++;
     618                }
     619            }
     620            derivands = myInputDer;
     621        }
     622
     623        if (myInputs().size() > func.myInputs().size()) {
     624            ASTNode.DERIVATIVE_ANNOTATION_PROBLEM.invoke(this, getFQName().name(),
     625                    "The derivative functions inputs must contain all the underived functions inputs.");
     626            return;
     627        }
     628
     629        func.checkDerivativeInputs(myInputs());
     630        if (myInputs + myInputDer - noDerVars != func.myInputs().size()) {
     631            String my_s = myInputs + myInputDer - noDerVars != 1 ? "inputs" : "input";
     632            String der_s = func.myInputs().size() != 1 ? "inputs" : "input";
     633            ASTNode.DERIVATIVE_FUNCTION_MISSMATCH.invoke(this, func.getFQName().name(), getFQName().name(),
     634                    myInputs + myInputDer - noDerVars, my_s, func.myInputs().size(), der_s);
     635        }
     636
     637        int myOutputDer = myOutputs().size();
     638        if (derivativeOrder(derNode) < 2) {
     639            myOutputDer = 0;
     640            for (FFunctionVariable v : myOutputs()) {
     641                if (v.isReal() || v.type().containsReal()) {
     642                    myOutputDer++;
     643                }
     644            }
     645        }
     646
     647        if (myOutputDer != func.myOutputs().size()) {
     648            String my_s = myOutputDer != 1 ? "outputs" : "output";
     649            String der_s = func.myOutputs().size() != 1 ? "outputs" : "output";
     650            ASTNode.DERIVATIVE_FUNCTION_MISSMATCH.invoke(this, func.getFQName().name(), getFQName().name(), myOutputDer,
     651                    my_s, func.myOutputs().size(), der_s);
     652        }
     653    }
     654
     655    //Determine how many inputs should be derivated in multi-level derivations
     656    public int FFunctionCall.derivands=0;
     657   
     658    public void FFunctionDecl.checkDerivativeInputs(ArrayList<FFunctionVariable> inputs) {
     659        Iterator<FFunctionVariable> myInputs = myInputs().iterator();
     660        for (FFunctionVariable fv : inputs) {
     661            if (!fv.type().typeCompatible(myInputs.next().type())) {
     662                ASTNode.DERIVATIVE_ANNOTATION_PROBLEM.invoke(this, getFQName().name(),
     663                        "The inputs must be ordered in the same way in the derivative function.");
     664            }
     665        }
     666        while (myInputs.hasNext()) {
     667            FType type = myInputs.next().type();
     668            if (!type.isReal() && !type.containsReal())
     669                ASTNode.DERIVATIVE_ANNOTATION_PROBLEM.invoke(this, getFQName().name(),
     670                        "The derivative inputs must be reals or contain reals.");
     671        }
     672
     673        Iterator<FFunctionVariable> myOutputs = myOutputs().iterator();
     674        while (myOutputs.hasNext()) {
     675            FType type = myOutputs.next().type();
     676            if (!type.isReal() && !type.containsReal())
     677                ASTNode.DERIVATIVE_ANNOTATION_PROBLEM.invoke(this, getFQName().name(),
     678                        "The derivative outputs must be reals or contain reals.");
     679        }
     680    }
     681   
     682
    533683   
    534684    public FExp FRecordConstructor.diff(String name) {
     
    576726            }
    577727        }
    578        
    579         FFunctionDecl myDecl = myFCallable().asFFunctionDecl();
    580         if (myDecl == null)
    581             throw new FunctionDifferentiationException(this);
    582         AnnotationNode derNode = derivativeAnnotation();
    583        
     728        Object[] derUtils = checkDerivative();
     729        FFunctionDecl myDecl = (FFunctionDecl) derUtils[0];
     730        AnnotationNode derNode = (AnnotationNode) derUtils[1];
    584731        FCallable myDiffDecl = myDecl.diff(derNode, inFunction());
    585732        if (myDiffDecl == null)
     
    590737        }
    591738        int i = 0;
     739        int derivands=0;
    592740        for (FExp e : getArgs()) {
    593741            FFunctionVariable fv = myDecl.myInput(i);
    594             if (fv.shouldBeDifferentiated(derNode))
     742            if (fv.shouldBeDifferentiated(derNode)) {
    595743                args.add(e.diff(name));
     744                derivands++;
     745            }
    596746            i++;
    597747        }
    598748        FFunctionCall der_f = new FFunctionCall(new FIdUse(myDiffDecl.name()), args, getFType().treeCopy());
    599749        der_f.order = order + 1;
     750        der_f.derivands = derivands;
    600751        return dynamicFExp(der_f);
    601752    }
  • trunk/Compiler/ModelicaMiddleEnd/test/modelica/Differentiation.mo

    r10827 r10834  
    22552255")})));
    22562256        end Functional1;
    2257 
    2258     end DerivativeAnnotation;
    2259 
     2257    model DerivativeFunctionMissmatch1
     2258     function f
     2259        input Real x;
     2260        input Real y;
     2261        input Real z;
     2262        output Real o_d;
     2263      algorithm
     2264          o_d :=z*x;
     2265        annotation (Inline=false, derivative(noDerivative=z) = fd);
     2266      end f;
     2267      function fd
     2268        input Real x;
     2269        input Real y;
     2270        input Real z;
     2271        input Real x_d;
     2272        input Real y_d;
     2273        output Real o_d;
     2274      algorithm
     2275          o_d :=z*x;
     2276        annotation (Inline=false, derivative(noDerivative=y_d , order=2) = fdd);
     2277      end fd;
     2278
     2279      function fdd
     2280        input Real x;
     2281        input Real y;
     2282        input Real z;
     2283        input Real x_d;
     2284        input Real y_d;
     2285        input Real x_dd;
     2286        input Real y_dd;
     2287        output Real o_dd;
     2288      algorithm
     2289        o_dd := z*(x_d+x)-x_dd*x_d;
     2290      annotation (Inline=false);
     2291      end fdd; 
     2292      Real a, b, c;
     2293      equation
     2294        a = f(time,time,time);
     2295        b = der(a);
     2296        c = der(b);
     2297        annotation(__JModelica(UnitTesting(tests={
     2298            ErrorTestCase(
     2299            name="DerivativeFunctionMissmatch1",
     2300            description="Test detection of input which should be excluded (y_dd) in 2 order derivative",
     2301            errorMessage="
     2302Error in flattened model, DERIVATIVE_FUNCTION_MISSMATCH:
     2303  The function Differentiation.DerivativeAnnotation.DerivativeFunctionMissmatch1.fdd's signature doesn't match the expected signature for a derivative to Differentiation.DerivativeAnnotation.DerivativeFunctionMissmatch1.fd.
     2304  Expected 6 inputs but signature have 7 inputs.
     2305")})));
     2306    end DerivativeFunctionMissmatch1;
     2307
     2308    model DerivativeFunctionMissmatch2
     2309     function f
     2310        input Real x;
     2311        input Real y;
     2312        input Real z;
     2313        output Real o_d;
     2314      algorithm
     2315          o_d :=z*x;
     2316        annotation (Inline=false, derivative(noDerivative=z) = fd);
     2317      end f;
     2318      function fd
     2319        input Real x;
     2320        input Real y;
     2321        input Real z;
     2322        input Real x_d;
     2323        input Real y_d;
     2324        output Real o_d;
     2325      algorithm
     2326          o_d :=z*x;
     2327        annotation (Inline=false, derivative(noDerivative=z , order=2) = fdd);
     2328      end fd;
     2329
     2330      function fdd
     2331        input Real x;
     2332        input Real y;
     2333        input Real z;
     2334        input Real x_d;
     2335        input Real y_d;
     2336        input Real x_dd;
     2337        output Real o_dd;
     2338      algorithm
     2339        o_dd := z*(x_d+x)-x_dd*x_d;
     2340      annotation (Inline=false);
     2341      end fdd; 
     2342      Real a, b, c;
     2343      equation
     2344        a = f(time,time,time);
     2345        b = der(a);
     2346        c = der(b);
     2347        annotation(__JModelica(UnitTesting(tests={
     2348            ErrorTestCase(
     2349            name="DerivativeFunctionMissmatch2",
     2350            description="Test detection of extra input (y_dd) in 2 order derivative",
     2351            errorMessage="
     2352Error in flattened model, DERIVATIVE_FUNCTION_MISSMATCH:
     2353  The function Differentiation.DerivativeAnnotation.DerivativeFunctionMissmatch2.fdd's signature doesn't match the expected signature for a derivative to Differentiation.DerivativeAnnotation.DerivativeFunctionMissmatch2.fd.
     2354  Expected 7 inputs but signature have 6 inputs.
     2355")})));
     2356    end DerivativeFunctionMissmatch2;
     2357 
     2358  model DerivativeFunctionMissmatch3
     2359      function fd
     2360        input Real x;
     2361        input Real y;
     2362        input Real z;
     2363        input Real x_d;
     2364        input Real y_d;
     2365        output Real o_d;
     2366      algorithm
     2367          o_d :=z*x;
     2368        annotation (Inline=false, derivative(noDerivative=z) = fdd);
     2369      end fd;
     2370
     2371      function fdd
     2372        input Real x;
     2373        input Real y;
     2374        input Real z;
     2375        input Real x_d;
     2376        input Real y_d;
     2377        input Real x_dd;
     2378        input Real y_dd;
     2379        output Real o_dd;
     2380      algorithm
     2381        o_dd := z*(x_d+x)-x_dd*x_d;
     2382      annotation (Inline=false, derivative(noDerivative=z, order=2) = fddd);
     2383      end fdd;
     2384     
     2385      function fddd
     2386        input Real x;
     2387        input Real y;
     2388        input Real z;
     2389        input Real x_d;
     2390        input Real y_d;
     2391        input Real x_dd;
     2392        input Real y_dd;
     2393        input Real x_ddd;
     2394        input Real y_ddd;
     2395        output Real o_dd;
     2396      algorithm
     2397        o_dd := y_dd+z*(x_d+x)-x_ddd*x_dd;
     2398      end fddd;
     2399      Real m , n;
     2400      equation
     2401        der(m)=fd(time,time,time,time,time);
     2402        der(n)=fdd(time,time,time,time,time,time,time);
     2403        annotation(__JModelica(UnitTesting(tests={
     2404            FClassMethodTestCase(
     2405            name="DerivativeFunctionMissmatch3",
     2406            description="Test missing order missing",
     2407            eliminate_linear_equations=false,
     2408            methodName="checkDerivativeAnnotations",
     2409            methodResult="
     24101 errors found:
     2411
     2412Error in flattened model, DERIVATIVE_FUNCTION_MISSMATCH:
     2413  The function Differentiation.DerivativeAnnotation.DerivativeFunctionMissmatch3.fdd's signature doesn't match the expected signature for a derivative to Differentiation.DerivativeAnnotation.DerivativeFunctionMissmatch3.fd.
     2414  Expected 9 inputs but signature have 7 inputs.
     2415")})));
     2416    end DerivativeFunctionMissmatch3;
     2417
     2418  model DerivativeFunctionThirdOrder
     2419      function f
     2420        input Real x;
     2421        input Real y;
     2422        input Real z;
     2423        output Real o_d;
     2424      algorithm
     2425          o_d :=z*x;
     2426        annotation (Inline=false, derivative(noDerivative=z) = fd);
     2427      end f;
     2428      function fd
     2429        input Real x;
     2430        input Real y;
     2431        input Real z;
     2432        input Real x_d;
     2433        input Real y_d;
     2434        output Real o_d;
     2435      algorithm
     2436          o_d :=z*x;
     2437        annotation (Inline=false, derivative(noDerivative=z, order=2) = fdd);
     2438      end fd;
     2439
     2440      function fdd
     2441        input Real x;
     2442        input Real y;
     2443        input Real z;
     2444        input Real x_d;
     2445        input Real y_d;
     2446        input Real x_dd;
     2447        input Real y_dd;
     2448        output Real o_dd;
     2449      algorithm
     2450        o_dd := z*(x_d+x)-x_dd*x_d;
     2451      annotation (Inline=false, derivative(noDerivative=z, order=3) = fddd);
     2452      end fdd;
     2453     
     2454      function fddd
     2455        input Real x;
     2456        input Real y;
     2457        input Real z;
     2458        input Real x_d;
     2459        input Real y_d;
     2460        input Real x_dd;
     2461        input Real y_dd;
     2462        input Real x_ddd;
     2463        input Real y_ddde;
     2464        input Real x_ddde;
     2465        input Real y_ddd;
     2466        output Real o_ddd;
     2467      algorithm
     2468        o_ddd := y_dd+z*(x_d+x)-x_ddd*x_dd; 
     2469        annotation (Inline=false);
     2470      end fddd;
     2471      Real a, b, c, d;
     2472      equation
     2473        a = f(time, time, time);
     2474        b = der(a);
     2475        c = der(b);
     2476        d = der(c);
     2477        annotation(__JModelica(UnitTesting(tests={
     2478            ErrorTestCase(
     2479            name="DerivativeFunctionThirdOrder",
     2480            description="Test detection of extra arguments (_ddde) in high order derivative",
     2481            errorMessage="
     2482Error in flattened model, DERIVATIVE_FUNCTION_MISSMATCH:
     2483  The function Differentiation.DerivativeAnnotation.DerivativeFunctionThirdOrder.fddd's signature doesn't match the expected signature for a derivative to Differentiation.DerivativeAnnotation.DerivativeFunctionThirdOrder.fdd.
     2484  Expected 9 inputs but signature have 11 inputs.
     2485")})));
     2486    end DerivativeFunctionThirdOrder;
     2487   
     2488  model DerivativeFunctionThirdOrderCorrect
     2489      function f
     2490        input Real x;
     2491        input Real y;
     2492        input Real z;
     2493        output Real o;
     2494      algorithm
     2495          o :=z*x;
     2496        annotation (Inline=false, derivative(noDerivative=z) = fd);
     2497      end f;
     2498      function fd
     2499        input Real x;
     2500        input Real y;
     2501        input Real z;
     2502        input Real x_d;
     2503        input Real y_d;
     2504        output Real o_d;
     2505      algorithm
     2506          o_d :=z*x;
     2507        annotation (Inline=false, derivative(noDerivative=x_d, order=2) = fdd);
     2508      end fd;
     2509
     2510      function fdd
     2511        input Real x;
     2512        input Real y;
     2513        input Real z;
     2514        input Real x_d;
     2515        input Real y_d;
     2516        input Real y_dd;
     2517        output Real o_dd;
     2518      algorithm
     2519        o_dd := z*(x_d+x)-x_d*y_d;
     2520      annotation (Inline=false, derivative(noDerivative=z, order=3) = fddd);
     2521      end fdd;
     2522     
     2523      function fddd
     2524        input Real x;
     2525        input Real y;
     2526        input Real z;
     2527        input Real x_d;
     2528        input Real y_d;
     2529        input Real y_dd;
     2530        input Real y_ddd;
     2531        output Real o_ddd;
     2532      algorithm
     2533        o_ddd := y_ddd*y_dd*x_d; 
     2534        annotation (Inline=false);
     2535      end fddd;
     2536      Real a, b, c, d;
     2537      equation
     2538        a = f(time, time, time);
     2539        b = der(a);
     2540        c = der(b);
     2541        d = der(c);
     2542        annotation(__JModelica(UnitTesting(tests={
     2543            FClassMethodTestCase(
     2544            name="DerivativeFunctionThirdOrderCorrect",
     2545            description="Test that the derivative",
     2546            methodName="checkDerivativeAnnotations",
     2547            methodResult="[]")})));
     2548    end DerivativeFunctionThirdOrderCorrect;
     2549   
     2550    model DerivativeFunctionMissingInputs
     2551      function f
     2552      input Real r;
     2553      input Boolean b;
     2554      input Integer i;
     2555      output Real q;
     2556      algorithm
     2557        q := r;
     2558      annotation (Inline=false, derivative = fd);
     2559      end f;
     2560      function fd
     2561        input Real a;
     2562        output Real b;
     2563      algorithm     
     2564        b := a;
     2565      annotation (Inline=false); 
     2566      end fd;
     2567      Real r,g;
     2568      equation
     2569      r=f(time,true,1);
     2570      der(g)=fd(time);
     2571      annotation(__JModelica(UnitTesting(tests={
     2572            FClassMethodTestCase(
     2573            name="DerivativeFunctionMissingInputs",
     2574            description="Test derivative function specifying with missing input arguments",
     2575            eliminate_linear_equations=false,
     2576            methodName="checkDerivativeAnnotations",
     2577            methodResult="
     25781 errors found:
     2579
     2580Error in flattened model, DERIVATIVE_ANNOTATION_PROBLEM:
     2581  The function Differentiation.DerivativeAnnotation.DerivativeFunctionMissingInputs.f and it's specified derivatives doesn't have compatible signatures.
     2582The derivative functions inputs must contain all the underived functions inputs.
     2583")})));
     2584    end DerivativeFunctionMissingInputs;
     2585   
     2586    model DerivativeFunctionMissingInputs2
     2587     function f
     2588      input Real r;
     2589      input Boolean b;
     2590      input Integer i;
     2591      output Real q;
     2592      algorithm
     2593        q := r;
     2594      annotation (Inline=false, derivative = fd);
     2595      end f;
     2596      function fd
     2597        input Real a;
     2598        output Real b;
     2599      algorithm     
     2600        b := a;
     2601      annotation (Inline=false, derivative(order=2) = fdd); 
     2602      end fd;
     2603      function fdd
     2604        input Real a;
     2605        input Real ader;
     2606        output Real b;
     2607      algorithm     
     2608        b := a;
     2609      annotation (Inline=false); 
     2610      end fdd;
     2611      Real r,g,q;
     2612      equation
     2613      r=f(time,true,1);
     2614      g=der(r);
     2615      q=der(g);
     2616          annotation(__JModelica(UnitTesting(tests={
     2617            ErrorTestCase(
     2618            name="DerivativeFunctionMissingInputs2",
     2619            description="Test derivative function specifying a function with missing input arguments",
     2620            errorMessage="
     2621Error in flattened model, DERIVATIVE_ANNOTATION_PROBLEM:
     2622  The function Differentiation.DerivativeAnnotation.DerivativeFunctionMissingInputs2.f and it's specified derivatives doesn't have compatible signatures.
     2623The derivative functions inputs must contain all the underived functions inputs.
     2624
     2625Error in flattened model, DERIVATIVE_ANNOTATION_PROBLEM:
     2626  The function Differentiation.DerivativeAnnotation.DerivativeFunctionMissingInputs2.fd and it's specified derivatives doesn't have compatible signatures.
     2627The derivative functions inputs must contain all the underived functions inputs.")})));
     2628    end DerivativeFunctionMissingInputs2;
     2629   
     2630    model DerivativeFunctionWrongInputTypes
     2631       function f
     2632      input Real a;
     2633      input Integer b;
     2634      output Real o;
     2635      algorithm
     2636        o := a;
     2637      annotation (Inline=false, derivative = fd);
     2638      end f;
     2639      function fd
     2640        input Integer a;
     2641        input Boolean b;
     2642        input Real a_der;
     2643        output Real o;
     2644      algorithm     
     2645        o := a_der+a*1.5;
     2646      annotation (Inline=false); 
     2647      end fd;
     2648      Real r,g;
     2649      equation
     2650      r=f(time,1);
     2651      g=der(r);
     2652      annotation(__JModelica(UnitTesting(tests={
     2653            ErrorTestCase(
     2654            name="DerivativeFunctionWrongInputTypes",
     2655            description="Test failing differentiation of functional input arguments",
     2656            errorMessage="
     2657Error in flattened model, DERIVATIVE_ANNOTATION_PROBLEM:
     2658  The function Differentiation.DerivativeAnnotation.DerivativeFunctionWrongInputTypes.fd and it's specified derivatives doesn't have compatible signatures.
     2659  The inputs must be ordered in the same way in the derivative function.")})));   
     2660    end DerivativeFunctionWrongInputTypes;
     2661   
     2662    model DerivativeFunctionDerivandsNoReal
     2663        function f
     2664            input Real x;
     2665            output Real o;
     2666            algorithm
     2667                o := x;
     2668            annotation (Inline=false, derivative = fd);
     2669        end f;
     2670        function fd
     2671            input Real x;
     2672            input Integer xder;
     2673            output Real o;
     2674            algorithm
     2675                o := x;
     2676            annotation (Inline=false);
     2677        end fd;
     2678        Real r,g;
     2679        equation
     2680            r=f(time);
     2681            der(g)=der(r);
     2682          annotation(__JModelica(UnitTesting(tests={
     2683            ErrorTestCase(
     2684            name="DerivativeFunctionDerivandsNoReal",
     2685            description="Test derivative function with no real input arguments",
     2686            errorMessage="
     26871 errors found:
     2688
     2689Error in flattened model, DERIVATIVE_ANNOTATION_PROBLEM:
     2690  The function Differentiation.DerivativeAnnotation.DerivativeFunctionDerivandsNoReal.fd and it's specified derivatives doesn't have compatible signatures.
     2691The derivative inputs must be reals or contain reals.
     2692")})));   
     2693    end DerivativeFunctionDerivandsNoReal;
     2694   
     2695   model DerivativeFunctionDerivandsNoRealOutput
     2696        function f
     2697            input Real x;
     2698            output Real o;
     2699            algorithm
     2700                o := x;
     2701            annotation (Inline=false, derivative = fd);
     2702        end f;
     2703        function fd
     2704            input Real x;
     2705            input Real xder;
     2706            output Boolean o;
     2707            algorithm
     2708                o := x == 0;
     2709            annotation (Inline=false);
     2710        end fd;
     2711        Real r,g;
     2712        equation
     2713            r=f(time);
     2714            der(g)=der(r);
     2715          annotation(__JModelica(UnitTesting(tests={
     2716            ErrorTestCase(
     2717            name="DerivativeFunctionDerivandsNoRealOutput",
     2718            description="Test derivative function with no real output arguments",
     2719            errorMessage="
     27201 errors found:
     2721
     2722Error in flattened model, DERIVATIVE_ANNOTATION_PROBLEM:
     2723  The function Differentiation.DerivativeAnnotation.DerivativeFunctionDerivandsNoRealOutput.fd and it's specified derivatives doesn't have compatible signatures.
     2724The derivative outputs must be reals or contain reals.
     2725")})));   
     2726    end DerivativeFunctionDerivandsNoRealOutput;
     2727   
     2728   model DerivativeFunctionDerivandsNoReal2
     2729        function f
     2730            input Real x;
     2731            output Real o;
     2732            algorithm
     2733                o := x;
     2734            annotation (Inline=false, derivative = fd);
     2735        end f;
     2736        function fd
     2737            input Real x;
     2738            input Boolean xder;
     2739            output Real o;
     2740            algorithm
     2741                o := x;
     2742            annotation (Inline=false);
     2743        end fd;
     2744        Real r,g;
     2745        equation
     2746            r=f(time);
     2747            der(g)=fd(time,true);
     2748          annotation(__JModelica(UnitTesting(tests={
     2749            FClassMethodTestCase(
     2750            name="DerivativeFunctionDerivandsNoReal2",
     2751            description="Test derivative function with no real outputs/Input arguments",
     2752            eliminate_linear_equations=false,
     2753            methodName="checkDerivativeAnnotations",
     2754            methodResult="
     27551 errors found:
     2756
     2757Error in flattened model, DERIVATIVE_ANNOTATION_PROBLEM:
     2758  The function Differentiation.DerivativeAnnotation.DerivativeFunctionDerivandsNoReal2.fd and it's specified derivatives doesn't have compatible signatures.
     2759The derivative inputs must be reals or contain reals.
     2760")})));   
     2761    end DerivativeFunctionDerivandsNoReal2;
     2762
     2763    model DerivativeFunctionTooManyInputs
     2764      function f
     2765        input Real a;
     2766        output Real o;
     2767        algorithm
     2768          o := a;
     2769        annotation (Inline=false, derivative = fd);
     2770      end f;
     2771      function fd
     2772        input Real a;
     2773        input Real b;
     2774        input Real c;
     2775        output Real o;
     2776        algorithm     
     2777          o := a+b;
     2778        annotation (Inline=false); 
     2779      end fd;
     2780      Real r,g,h;
     2781      equation
     2782      r=f(time);
     2783      der(h)=f(time);
     2784      der(g)=fd(time, 1.0, 1.0);
     2785      annotation(__JModelica(UnitTesting(tests={
     2786            FClassMethodTestCase(
     2787            name="DerivativeFunctionTooManyInputs",
     2788            description="Test derivative function specifying a function with too many input arguments",
     2789            eliminate_linear_equations=false,
     2790            methodName="checkDerivativeAnnotations",
     2791            methodResult="
     27921 errors found:
     2793
     2794Error in flattened model, DERIVATIVE_FUNCTION_MISSMATCH:
     2795  The function Differentiation.DerivativeAnnotation.DerivativeFunctionTooManyInputs.fd's signature doesn't match the expected signature for a derivative to Differentiation.DerivativeAnnotation.DerivativeFunctionTooManyInputs.f.
     2796  Expected 2 inputs but signature have 3 inputs.
     2797")})));   
     2798    end DerivativeFunctionTooManyInputs;
     2799   
     2800    model DerivativeFunctionTooManyOutputs
     2801      function f
     2802        input Real a;
     2803        output Real o;
     2804        algorithm
     2805          o := a;
     2806        annotation (Inline=false, derivative = fd);
     2807      end f;
     2808      function fd
     2809        input Real a;
     2810        input Real ader;
     2811        output Real o;
     2812        output Real extra;
     2813        algorithm     
     2814          o := ader*2+a;
     2815        annotation (Inline=false); 
     2816      end fd;
     2817      Real r,g;
     2818      equation
     2819      der(g)=fd(time,1);
     2820      r=f(time);
     2821      annotation(__JModelica(UnitTesting(tests={
     2822            FClassMethodTestCase(
     2823            name="DerivativeFunctionTooManyOutputs",
     2824            description="Test derivative function specifying a function with too many output arguments",
     2825            eliminate_linear_equations=false,
     2826            methodName="checkDerivativeAnnotations",
     2827            methodResult="
     28281 errors found:
     2829
     2830Error in flattened model, DERIVATIVE_FUNCTION_MISSMATCH:
     2831  The function Differentiation.DerivativeAnnotation.DerivativeFunctionTooManyOutputs.fd's signature doesn't match the expected signature for a derivative to Differentiation.DerivativeAnnotation.DerivativeFunctionTooManyOutputs.f.
     2832  Expected 1 output but signature have 2 outputs.
     2833
     2834")})));   
     2835    end DerivativeFunctionTooManyOutputs;
     2836   
     2837end DerivativeAnnotation;
     2838   
    22602839    package AlgorithmDifferentiation
    22612840
     
    35874166end MultipleDerivativeAnnotation1;
    35884167
    3589 
    35904168model MultipleDerivativeAnnotation2
    35914169    function f
     
    37704348end MultipleDerivativeAnnotation2;
    37714349
    3772 
    37734350end Differentiation;
  • trunk/Compiler/ModelicaMiddleEnd/test/modelica/DynamicStates.mo

    r10324 r10834  
    16261626            algorithm
    16271627                y := x1 * x2_der + x1_der * x2;
    1628                 annotation(Inline=false, derivative=F_dd);
     1628                annotation(Inline=false, derivative(order=2)=F_dd);
    16291629            end F_d;
    16301630           
     
    16931693  y := x1 * x2_der + x1_der * x2;
    16941694  return;
    1695  annotation(derivative = DynamicStates.Special.FunctionDerivative1.F_dd,Inline = false);
     1695  annotation(derivative(order = 2) = DynamicStates.Special.FunctionDerivative1.F_dd,Inline = false);
    16961696 end DynamicStates.Special.FunctionDerivative1.F_d;
    16971697
     
    17331733            algorithm
    17341734                y := r.x + r.y + x_der;
    1735                 annotation(Inline=false, derivative(noDerivative=r)=F1_dd);
     1735                annotation(Inline=false, derivative(noDerivative=r,order=2)=F1_dd);
    17361736            end F1_d;
    17371737           
Note: See TracChangeset for help on using the changeset viewer.