janpeter
janpeter

Reputation: 1074

Parametrised Modelica library and possibility to use models as parameters

I try to make a parametrised library. I works fine using packages and connectors as parameters.

Using a model as a parameter is also possible. However, if the model is used in the library to build new models using extend, then it is not allowed, what I understand.

I wonder if the library contains a model with inner/outer style of connector, is it then allowed to let the inner model to be a parameter of the library?

Below a simple example to illustrate the problem. TEST is the library and Fish3b is an application. When I run the Example in the library TEST it all works, but when I have a separate application file it does not.

The error text is: cannot find class declaration for AquariumType running JModelica 2.4

package TEST

   model FishType1
       outer Real T;
       Real health;
   equation
       health = 30-T;
   end FishType1;

   model FishType2
       outer Real T;
       Real health;
   equation
       health = 32-T;
   end FishType2;

   package Equipment
       model AquariumType
           replaceable model FishType
           end FishType;       
           FishType fish;
           inner Real T;
       equation
          T = 29;
       end AquariumType;
   end Equipment;

   // Adapt AquariumType model to actual fish
   model Aquarium
       import TEST.Equipment.AquariumType;
       extends AquariumType(redeclare model FishType=FishType2);
   end Aquarium;

   // Example
   model Example
       Aquarium aquarium;
   end Example;

end TEST;

And below an example of application code that import from library above - and here is some error I think.

   encapsulated package Fish3b

       model FishType3
           outer Real T;
           Real health;
       equation
           health = 34-T;
       end FishType3;

       // Adapt package Equipment with AquariumType model to actual fish
       package Equipment3 
           import TEST.Equipment;
           extends Equipment.AquariumType(redeclare model   FishType=FishType3);
       end Equipment3;

       // Example
       model Example
           import Fish3b.Equipment3;
           Equipment3.AquariumType aquarium;
       end Example;

   end Fish3b;

Upvotes: 1

Views: 154

Answers (3)

janpeter
janpeter

Reputation: 1074

Updated the code with input from Hans Olsson before, but slightly different with the corrections suggested above. And I manage to avoid “extend from a replaceable model”, but I am not too sure how critical that is, see my previous comment.

Also, I think I would like to keep the clear identity of the different fishes, and also declare them outside package Equipment.

    package TEST3

        partial model FishBase
            outer Real T;
        end FishBase;

        model FishType1
            extends FishBase;
            Real health;
        equation
            health = 30 - T;
        end FishType1;

        model FishType2
            extends FishBase;
            Real health;
        equation
            health = 32 - T;
        end FishType2;  

        package Equipment
            replaceable model FishType
            end FishType;

            constant Integer dummy = 1;

            model AquariumType
                FishType fish;
                inner Real T;
            equation
                T = 29;
            end AquariumType;
        end Equipment;

        // Adapt package Equipment to the actual fish
        package Equipment3
            import TEST3.Equipment;
            extends Equipment;
            redeclare model FishType=FishType2;
        end Equipment3;

        // Example
        model Example
            Equipment3.AquariumType aquarium;
        end Example;

    end TEST3;

And an example of application code:

    encapsulated package T3_Fish3

        model FishType3
            import TEST3.FishBase;
            extends FishBase;
            Real health;
        equation
            health = 34-T;
        end FishType3;

        // Adapt package Equipment to the actual fish
        package Equipment3
            import TEST3.Equipment;
            extends Equipment(redeclare model FishType=FishType3);
        end Equipment3;

        // Example
        model Example
            Equipment3.AquariumType aquarium;
        end Example;

    end T3_Fish3;

Upvotes: 0

Hans Olsson
Hans Olsson

Reputation: 12507

The answer from janpeter works.

Another alternative that avoids introducing models called "FishType1", "FishType3" etc is to use "redeclare model extends" as follows (the Test2 can be unchanged or same change for Equipment1), but it uses more advanced constructs.

encapsulated package T2_Fish3

       // Adapt package Equipment to the actual fish
       package Equipment3
           import TEST2.Equipment;
           extends Equipment;

           redeclare model extends FishType
            outer Real T;
            Real health;
           equation 
             health = 32-T;
           end FishType;
       end Equipment3;

       // Example
       model Example
           Equipment3.AquariumType aquarium;
       end Example;

end T2_Fish3;

Additionally it would be possible to move the common "outer Real T" to the base-model FishType leading to:

package TEST2

    package Equipment
        replaceable model FishType
          outer Real T;
        end FishType;

        constant Integer dummy = 1;

        model AquariumType
            FishType fish;
            inner Real T;
        equation 
            T = 29;
        end AquariumType;
    end Equipment;

    // Adapt package Equipment to the actual fish
    package Equipment1
      import TEST2.Equipment;

      extends Equipment;
      redeclare model extends FishType
        Real health;
      equation 
        health = 30 - T;
      end FishType;
    end Equipment1;

    // Example
    model Example
        Equipment1.AquariumType aquarium;
    end Example;

end TEST2;

and

encapsulated package T2_Fish3

       // Adapt package Equipment to the actual fish
       package Equipment3
           import TEST2.Equipment;
           extends Equipment;

           redeclare model extends FishType
            Real health;
           equation 
             health = 32-T;
           end FishType;
       end Equipment3;

       // Example
       model Example
           Equipment3.AquariumType aquarium;
       end Example;

end T2_Fish3;

Upvotes: 1

janpeter
janpeter

Reputation: 1074

Thanks “tbeu” for the comment!

I have modified the code so that package Equipment is not extended from a model. The updated code below also represent my underlying “true” problem much better. And it all works. Thanks!

The updated library file TEST2.mo:

   package TEST2

       model FishType1
           outer Real T;
           Real health;
       equation
           health = 30-T;
       end FishType1;

       model FishType2
           outer Real T;
           Real health;
       equation
           health = 32-T;
       end FishType2;

       package Equipment
           replaceable model FishType
           end FishType;

           constant Integer dummy = 1;

           model AquariumType
               FishType fish;
               inner Real T;
           equation
               T = 29;
           end AquariumType;
       end Equipment;

       // Adapt package Equipment to the actual fish
       package Equipment1
           import TEST2.Equipment;
           extends Equipment(redeclare model FishType=FishType1);
       end Equipment1;

       // Example
       model Example
           Equipment1.AquariumType aquarium;
       end Example;

    end TEST2;

And the application code T2_Fish3 that now uses the above library TEST2:

   encapsulated package T2_Fish3

       model FishType3
           outer Real T;
           Real health;
       equation
           health = 34-T;
       end FishType3;

       // Adapt package Equipment to the actual fish
       package Equipment3
           import TEST2.Equipment;
           extends Equipment(redeclare model FishType=FishType3);
       end Equipment3;

       // Example
       model Example
           Equipment3.AquariumType aquarium;
       end Example;

   end T2_Fish3;

Upvotes: 2

Related Questions