If you want to use state components, there are some limitations that you need to know about. This is clearly seen in section 4.4.5 of the Modelica 3.3 specification. It says: "If the condition is false, the component, its modifiers and any communication equations associated with the component are deleted." I will show you how to use this to solve your problem in just a second, but first I want to explain why your solution does not work.
The problem is with model validation. In your case, it is obvious that the equation component.x and component component either exist or do not exist. This is because you bound them to the same boolean variable. But what if you did not have this:
parameter Real some_number; Component component if some_number*some_number>4.0; equation if some_number>=-2 and some_number<=2 then component.x = 0; end if;
We see that this is logically identical to your case. For component.x there is no way to exist when component missing. But can we prove such things at all ? No.
So, when conditional components were introduced, conservative semantics were implemented, which could always guarantee that sets of variables and equations would never be “out of sync”.
Let's go back to the fact that the specification says: "If the condition is false, the component, its modifiers, and any connection equations involving the component, are deleted"
In your case, the solution can be quite simple. Depending on how you declare "x", you can simply add the modification to component , i.e.
parameter Boolean use_component=false; Component component(x=0) if use_component;
The elegance of this is that the modification applies only to the component , and if component not, then the modification (equation) is absent. Thus, the variable x and the associated equation are “synchronous”. But this does not work for all cases (IIRC, x must have an input qualifier for this to work ... maybe this is possible in your case?).
There are two alternatives. First put the component.x equation inside component . Secondly, you need to enter a connector on component , which, if connected, will generate the required equation. As with the modification (this is not a coincidence), you can associate x with some input connector, and then do this:
parameter Boolean use_component; Component component if use_component; Constant zero(k=0); equation connect(ky, component.x);
Now I could imagine that after considering all three cases (modifying, internalizing the equation and using connect ), you come to the conclusion that none of them will work. If so, then I would humbly suggest that you have a problem with how you developed the component. The reason these limitations arise is due to the need to test the components yourself for correctness. This requires the component to be complete (“balanced” in the specification terminology).
If you cannot solve the problem with the approaches I mentioned above, then I suspect that you really have a balancing problem, and you probably need to somehow redefine the boundaries of your component. If so, I suggest you open another question here with details of what you are trying to do.