Modia.jl icon indicating copy to clipboard operation
Modia.jl copied to clipboard

Test_Media: Package Media not found in current path

Open HildingElmqvist opened this issue 6 years ago • 3 comments

I wanted to try Test_Media but got:

`julia> include("$(Modia.ModiaDir)/src/models/Test_Media.jl") ERROR: LoadError: ArgumentError: Package Media not found in current path:

  • Run Pkg.add("Media") to install the Media package.

Stacktrace: [1] require(::Module, ::Symbol) at .\loading.jl:817 [2] include at .\boot.jl:317 [inlined] [3] include_relative(::Module, ::String) at .\loading.jl:1038 [4] include(::Module, ::String) at .\sysimg.jl:29 [5] include(::String) at .\client.jl:388 [6] top-level scope at none:0 in expression starting at C:\Users\Hilding Elmqvist.julia\packages\Modia\kJUT1\src\models\Test_Media.jl:45`

However, the Media module is present in the file.

HildingElmqvist avatar Oct 25 '18 12:10 HildingElmqvist

I have found the documentation for the API calls to Pkg to be somewhat confusing. Rather than sort out those details, I updated the import statements in the TestMedia.jl file, which allowed it to work correctly. This file was run by entering include("TestMedia.jl") at the REPL.


module TestMedia

module Media

# Definition of medium states
abstract type MediumState end

mutable struct State_pT <: MediumState
   p::Float64
   T::Float64
end

mutable struct State_ph <: MediumState
   p::Float64
   h::Float64
end

# Definition of generic Medium and its functions
abstract type AbstractMedium end
density(               m::AbstractMedium,state::MediumState) = error("... function not defined for $m")
specificEnthalpy(      m::AbstractMedium,state::MediumState) = error("... function not defined for $m")
specificInternalEnergy(m::AbstractMedium,state::MediumState) = error("... function not defined for $m")

setState_pT(           m::AbstractMedium,p,T)                = error("... function not defined for $m")
setState_ph(           m::AbstractMedium,p,h)                = error("... function not defined for $m")

end



module SimpleWater

   import ..Media

   # Constants of medium that have the same value for every medium instance
   const cp_const = 4184
   const cv_const = 4184
   const d_const  = 995.586
   const T0       = 273.15

   # Constants of medium specific to a medium instance
   struct Medium <: Media.AbstractMedium
      h_offset::Float64
      Medium(;h_offset=0.0) = new(h_offset)
   end

   # Functions of medium
   Media.density(               m::Medium, state::Media.State_pT) = d_const
   Media.specificEnthalpy(      m::Medium, state::Media.State_pT) = cp_const*(state.T - T0) + m.h_offset
   Media.specificInternalEnergy(m::Medium, state::Media.State_pT) = cv_const*(state.T - T0)
  
   Media.setState_pT(m::Medium, p, T)::Media.State_pT = Media.State_pT(p,T)
   Media.setState_ph(m::Medium, p, h)::Media.State_pT = Media.State_pT(p,T0+(h-m.h_offset)/cp_const)
end


import .Media
import .SimpleWater

# Set a medium at one connector
medium = SimpleWater.Medium(h_offset=10.0)

# Propagate medium in connector

# Inside a model
state = Media.setState_ph(medium, 1e5, 300.0)   # p,h from connector
d     = Media.density(medium,state)
h     = Media.specificEnthalpy(medium,state)

println("d = ", d)
println("h = ", h)

end

crlaugh avatar Nov 02 '18 04:11 crlaugh

One of the nonintuitive aspects of this investigation was that the comments that were present in the initial version of the file were causing problems; for some reason, the inclusion of comments prefaced by =# or ''' prevented the file from being included correctly. Deleting the comments made these problems go away.

crlaugh avatar Nov 02 '18 04:11 crlaugh

I just tried:

import Modia
include("$(Modia.ModiaDir)/src/models/Test_Media.jl")

resulting in the output

d = 995.586
h = 300.000000000055

So, it seems the issue is fixed and this ticket can be closed. Please, can you confirm.

MartinOtter avatar Jan 11 '19 13:01 MartinOtter