How to convert constants in parameters to variables in inputs in simscape?Invalid use of value 'density'.
Show older comments
I am trying to write a Simscape component that can define a fluid with variable density. After referring to the code of the "Custom Hydraulic Fluid" block, I moved the definition of density from parameters to inputs. The modified code is as follows:
component rho_fluid
inputs
density = { 1000 , 'kg/m^3' }; % Fluid density, now an input parameter
end
parameters
viscosity_kin = { 18e-6 , 'm^2/s' }; % Kinematic viscosity
bulk = { 0.8e9 , 'Pa' }; % Bulk modulus at atm. pressure and no gas
alpha = { 0.005 , '1' }; % Relative amount of trapped air
range_error = { 2 , '1' }; % Absolute pressure below absolute zero in blocks with fluid compressibility
end
nodes
G = foundation.hydraulic.hydraulic(...
density=density, ...
viscosity_kin=viscosity_kin, ...
bulk=bulk, ...
alpha=alpha, ...
range_error=range_error); % :right
end
equations
assert(density > 0)
assert(viscosity_kin > 0)
assert(bulk > 0)
assert(alpha >= 0)
assert(alpha < 1)
end
end
After running it, an error occurred:
Invalid use of value 'density'.
Can anyone spot my mistake?
7 Comments
Chuguang Pan
on 13 Oct 2024
Is there a more informed error message diaplayed?
ms z
on 13 Oct 2024
Yifeng Tang
on 21 Oct 2024
Looks like you are trying to use an input to assign a domain parameter, I don't think it's do-able. The right-hand-side of the parameter assignment equation can be combination of other parameters and constants but NOT anything containing variables or inputs.
Physically, density may vary as a result of pressure and/or temperature locally at individual components. Assigning a verying density for the whole domain during the course of a simulation creates concerns like violating the conservation of mass.
Maybe you can describe the physical system and phenomenon you are trying to model so the community can point you in a different direction, hopefully a more physically reasonable direction.
ms z
on 22 Oct 2024
Yifeng Tang
on 22 Oct 2024
Just to make sure: you need the capability to be able to vary the density during the course of a simulation, right? For example, the flow source may pump in a fluid whose density changes continuously throughout the operation, and it's not appropriate to simulate as separate simulations.
If this is indeed the case, yes, you'll need a custom physical domain.
Next question: how important is temperature for your simulation? Are you trying to, or do you have to, model the heat transfer from/to the fluids or is the system trying to regulate the temperature?
If the answer is no, your life will be a bit easier :p I believe it's possible to modify the existing thermal liquid domain, and repurpose the temperature across variable as a "concentration" variable. The density of the fluid will now depend on both the pressure and the concentration. Mixing fluids with different density & concentration can then happen according to the conservation laws.
If the answer is yes, well, it's still possible, but harder, much harder. All fluid properties needs to be a 3D table of P,T,& mineral concentration.
Either case, you'll need to build a collection of blocks similar to the foundation library of an existing domain. You may then write an interface block, like the IL-TL interface, to use some of the existing Simscape Fluids components.
It won't be a quick simple task. If you haven't already, I suggest you download a copy of the Simscape Language Guide. Go to the Simscape documentation page, click on the PDF documentation link on the right, and you'll find the download link to the Language Guide.
ms z
on 23 Oct 2024
Yifeng Tang
on 24 Oct 2024
I'm pretty sure it'll work. But it's also a lot of work. See further comments in the "Answer" section.
Answers (2)
Yifeng Tang
on 24 Oct 2024
1 vote
Below is a summary of the discussion in the comments section:
It is not possible to use an input to assign a domain parameter. Assigning a verying density for the whole domain during the course of a simulation creates concerns like violating the conservation of mass.
The application is to "simulate the collection and transportation of minerals in the ocean. Because minerals are distributed differently, I want to use a fluid with a verying density to represent the characteristics of two-phase flow." This will require a custom Simscape domain as the existing domains do not carry a concentration of mineral as an across variable.
As temperature of the fluid is not of concern, a good starting point will be to repurpose the existing thermal liquid (TL) domain, where the other fluid properties, e.g. density, are functions of the temperature and pressure. Rewriting the temperature variable to represent the mineral concentration will allow the density to vary with the concentration.
Below is to answer the follow-up question on a custom Simscape domain for modeling an isothermal liquid with varying density depending on a mineral concentration:
You may repurpose the T variable in the TL domain to represent the mineral concentration. At the same time, the energy flow, Phi, needs to represent the mineral mass flow. Phi will have a unit of kg/s. There are a few options for the unit of T as a concentration: kg/kg of pure water, kg/kg of total mixture, kg/liquid volume, mol/volume, etc. Among these, I think kg/kg of total mixture will be the easiest to work with as domain variables.
There is a port_convection.ssc code of the TL domain that you'll need to understand and modify. It defines the behavior of how other properties (energy, concentration, etc.) may travel with a mass flow. This documentation page explains the numerical scheme behind it. Since Phi is now the mineral flow, the thermal conductivity parameter of the domain needs to be repurposed as a "diffusivity". I'm not sure what happens to the specific heat. Maybe it's no longer necessary?
Once you feel you are done with the domain definition, the fluid property setting block, and the port_convection code, I suggest that you start with the Reservoir, Flow Resistance, and Pressure Source blocks. After that, controlled reservoir, absolute reference, cap, infinite flow resistance, local restriction, sensors, and flow sources. Whenever you see a energy or power related equations, pause and think how they should look now since temperature is no longer relevant.
If you have access to Simscape Fluids, find a Interface (TL-IL) block, whose source code is visible and write yourself a similar one for between the new domain and IL. You may need it later.
Work on the volume, pipe and converter blocks the last. They all contains conservation of energy equations for the fluid volume inside can will require more careful re-derivation of the governing equations. If you don't need the converter blocks, ignore them.
Some general tips:
- Use the same file structure as the shipping domains. Read the documentation pages on how to properly organize the custom library.
- Test often. Test more often. Compile your library very often. Build very simple model early and run it. Test with both positive and negative pressure differences and flow directions. Test with near zero conditions as well.
- Get a copy of the language guide form the PDF documentation page of Simscape.
It's going to be a lot of work. Good luck and have fun.
nick
on 13 Oct 2024
The error message indicates that there's an issue with how the 'desnity' input is being used within the node definition. According to the documentation, the relationship between the component variables and its nodes, which carry the Through and Across variables for the domain is defined in branches and equations section respecitvely. You can refer to the following documentation to learn more about this :
The following approach should resolve the error and allow you to define a fluid with variable density in your Simscape model:
component rho_fluid
inputs
density = { 1000, 'kg/m^3' }; % Fluid density, now an input parameter
end
% Define parameters
parameters
viscosity_kin = { 18e-6, 'm^2/s' }; % Kinematic viscosity
bulk = { 0.8e9, 'Pa' }; % Bulk modulus at atm. pressure and no gas
alpha = { 0.005, '1' }; % Relative amount of trapped air
range_error = { 2, '1' }; % Absolute pressure below absolute zero in blocks with fluid compressibility
end
nodes
G = foundation.hydraulic.hydraulic; % :right
end
equations
assert(density > 0, 'Density must be positive');
% Assign input and parameter values to the node
G.density == density;
G.viscosity_kin == viscosity_kin;
G.bulk == bulk;
G.alpha == alpha;
G.range_error == range_error;
assert(viscosity_kin > 0, 'Kinematic viscosity must be positive');
assert(bulk > 0, 'Bulk modulus must be positive');
assert(alpha >= 0, 'Alpha must be non-negative');
assert(alpha < 1, 'Alpha must be less than 1');
end
end
Hope this was helpful.
Categories
Find more on Upgrading Hydraulic Models to Use Isothermal Liquid Blocks in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
