Class JsonFluidReadWrite

java.lang.Object
neqsim.thermo.util.readwrite.JsonFluidReadWrite

public class JsonFluidReadWrite extends Object
Read and write NeqSim fluids in a JSON format that provides full EOS-level fidelity equivalent to Eclipse E300 fluid files.

The JSON format supports all parameters needed to exactly reproduce a PVT-tuned fluid: component critical properties, acentric factors, molecular weights, volume shifts, parachors, binary interaction coefficients, viscosity model coefficients (LBC/Pedersen), and pseudo-component (TBP fraction) definitions.

Example JSON:

{
  "format": "neqsim-fluid",
  "version": "1.0",
  "eos": "SRK",
  "reservoirTemperature": { "value": 90.0, "unit": "C" },
  "components": [
    {
      "name": "methane",
      "moleFraction": 0.85,
      "criticalTemperature": 190.6,
      "criticalPressure": 46.0,
      "acentricFactor": 0.008,
      "molarMass": 16.043,
      "normalBoilingPoint": 111.6,
      "criticalVolume": 0.0986,
      "volumeShift": -0.194,
      "parachor": 77.3,
      "isPseudo": false
    }
  ],
  "binaryInteractionCoefficients": [
    { "i": "methane", "j": "ethane", "kij": 0.0 }
  ]
}
Version:
1.0
Author:
copilot
  • Field Details

    • logger

      private static final org.apache.logging.log4j.Logger logger
      Logger object for class.
    • FORMAT_ID

      private static final String FORMAT_ID
      Format identifier expected in the JSON.
      See Also:
    • FORMAT_VERSION

      private static final String FORMAT_VERSION
      Current format version.
      See Also:
  • Constructor Details

    • JsonFluidReadWrite

      private JsonFluidReadWrite()
      Private constructor to prevent instantiation — all methods are static.
  • Method Details

    • read

      public static SystemInterface read(String inputFile)
      Read a fluid from a JSON file on disk.
      Parameters:
      inputFile - path to the JSON file
      Returns:
      a SystemInterface with all EOS parameters set
      Throws:
      IllegalArgumentException - if the file does not exist, cannot be read, or has invalid content
    • read

      public static SystemInterface read(String inputFile, boolean addWater)
      Read a fluid from a JSON file and optionally add a water component.
      Parameters:
      inputFile - path to the JSON file
      addWater - if true, add a water component with default kij = 0.5
      Returns:
      a SystemInterface with all EOS parameters set
      Throws:
      IllegalArgumentException - if the file does not exist, cannot be read, or has invalid content
    • read

      public static SystemInterface read(String inputFile, boolean addWater, double waterKij)
      Read a fluid from a JSON file and optionally add a water component with a custom kij value.
      Parameters:
      inputFile - path to the JSON file
      addWater - if true, add a water component
      waterKij - binary interaction parameter between water and all other components
      Returns:
      a SystemInterface with all EOS parameters set
      Throws:
      IllegalArgumentException - if the file does not exist, cannot be read, or has invalid content
    • readString

      public static SystemInterface readString(String json)
      Read a fluid from a JSON string.

      This is the primary parsing method. It supports all EOS parameters including pseudo-components, binary interaction coefficients, volume shifts, parachors, and viscosity model configuration.

      Parameters:
      json - the JSON string defining the fluid
      Returns:
      a SystemInterface with all EOS parameters set
      Throws:
      IllegalArgumentException - if the JSON is invalid or missing required fields
    • readString

      public static SystemInterface readString(String json, boolean addWater)
      Read a fluid from a JSON string and optionally add a water component.
      Parameters:
      json - the JSON string
      addWater - if true, add a water component with kij = 0.5
      Returns:
      a SystemInterface with all EOS parameters set
      Throws:
      IllegalArgumentException - if the JSON is invalid
    • readString

      public static SystemInterface readString(String json, boolean addWater, double waterKij)
      Read a fluid from a JSON string and optionally add a water component with custom kij.
      Parameters:
      json - the JSON string
      addWater - if true, add a water component
      waterKij - binary interaction parameter between water and all other components
      Returns:
      a SystemInterface with all EOS parameters set
      Throws:
      IllegalArgumentException - if the JSON is invalid
    • write

      public static void write(SystemInterface fluid, String outputFile) throws IOException
      Write a fluid to a JSON file.
      Parameters:
      fluid - the fluid to export
      outputFile - path to output file (e.g., "myfluid.json")
      Throws:
      IOException - if writing fails
    • write

      public static void write(SystemInterface fluid, String outputFile, double reservoirTempC) throws IOException
      Write a fluid to a JSON file with a specified reservoir temperature.
      Parameters:
      fluid - the fluid to export
      outputFile - path to output file
      reservoirTempC - reservoir temperature in Celsius
      Throws:
      IOException - if writing fails
    • write

      public static void write(SystemInterface fluid, Path outputPath) throws IOException
      Write a fluid to a JSON file at the given path.
      Parameters:
      fluid - the fluid to export
      outputPath - output file path
      Throws:
      IOException - if writing fails
    • write

      public static void write(SystemInterface fluid, Path outputPath, double reservoirTempC) throws IOException
      Write a fluid to a JSON file at the given path with a specified reservoir temperature.
      Parameters:
      fluid - the fluid to export
      outputPath - output file path
      reservoirTempC - reservoir temperature in Celsius
      Throws:
      IOException - if writing fails
    • toJsonString

      public static String toJsonString(SystemInterface fluid)
      Convert a NeqSim fluid to a JSON string with full EOS parameters.
      Parameters:
      fluid - the fluid to export
      Returns:
      JSON string representing the fluid
    • toJsonString

      public static String toJsonString(SystemInterface fluid, double reservoirTempC)
      Convert a NeqSim fluid to a JSON string with full EOS parameters and a specified reservoir temperature.
      Parameters:
      fluid - the fluid to export
      reservoirTempC - reservoir temperature in Celsius
      Returns:
      JSON string representing the fluid
    • convertE300ToJson

      public static void convertE300ToJson(String inputE300File, String outputJsonFile) throws IOException
      Convert a fluid from E300 format to JSON format.

      Convenience method that reads an E300 file and writes a JSON file.

      Parameters:
      inputE300File - path to the E300 input file
      outputJsonFile - path to the JSON output file
      Throws:
      IOException - if reading or writing fails
      IllegalArgumentException - if the E300 file is invalid
    • convertE300ToJson

      public static void convertE300ToJson(String inputE300File, String outputJsonFile, double reservoirTempC) throws IOException
      Convert a fluid from E300 format to JSON format with a specified reservoir temperature.
      Parameters:
      inputE300File - path to the E300 input file
      outputJsonFile - path to the JSON output file
      reservoirTempC - reservoir temperature in Celsius
      Throws:
      IOException - if reading or writing fails
      IllegalArgumentException - if the E300 file is invalid
    • convertJsonToE300

      public static void convertJsonToE300(String inputJsonFile, String outputE300File) throws IOException
      Convert a fluid from JSON format to E300 format.

      Convenience method that reads a JSON file and writes an E300 file.

      Parameters:
      inputJsonFile - path to the JSON input file
      outputE300File - path to the E300 output file
      Throws:
      IOException - if reading or writing fails
      IllegalArgumentException - if the JSON file is invalid
    • convertJsonToE300

      public static void convertJsonToE300(String inputJsonFile, String outputE300File, double reservoirTempC) throws IOException
      Convert a fluid from JSON format to E300 format with a specified reservoir temperature.
      Parameters:
      inputJsonFile - path to the JSON input file
      outputE300File - path to the E300 output file
      reservoirTempC - reservoir temperature in Celsius
      Throws:
      IOException - if reading or writing fails
      IllegalArgumentException - if the JSON file is invalid
    • createFluidByEOS

      private static SystemInterface createFluidByEOS(String eosType, boolean prcorr)
      Create a SystemInterface for the given EOS type.
      Parameters:
      eosType - the EOS type string (SRK, PR, etc.)
      prcorr - whether to use the 1978 PR correction
      Returns:
      a new SystemInterface instance
    • parseComponent

      private static JsonFluidReadWrite.ComponentData parseComponent(com.google.gson.JsonObject comp)
      Parse a single component from its JSON object representation.
      Parameters:
      comp - the JSON object for the component
      Returns:
      a ComponentData record with all parsed values
      Throws:
      IllegalArgumentException - if required fields are missing
    • getDoubleOrDefault

      private static double getDoubleOrDefault(com.google.gson.JsonObject obj, String field, double defaultValue)
      Get a double value from a JSON object, or return a default if the field is missing.
      Parameters:
      obj - the JSON object
      field - the field name
      defaultValue - the default value
      Returns:
      the field value or the default
    • mapToNeqSimName

      private static String mapToNeqSimName(String name)
      Map E300-style short component names to NeqSim database component names.
      Parameters:
      name - the input name (e.g., "C1", "N2")
      Returns:
      the NeqSim database name (e.g., "methane", "nitrogen")
    • applyBinaryInteractionCoefficients

      private static void applyBinaryInteractionCoefficients(SystemInterface fluid, List<String> componentNames, com.google.gson.JsonArray bicArray)
      Apply binary interaction coefficients from a JSON array to the fluid.

      The array contains objects with "i", "j", and "kij" fields. Components are referenced by name.

      Parameters:
      fluid - the fluid
      componentNames - list of component names in order
      bicArray - the JSON array of BIC entries
    • findComponentIndex

      private static int findComponentIndex(SystemInterface fluid, List<String> componentNames, String name)
      Find the index of a component in the fluid by name. Tries exact match first, then checks the mapped names in the componentNames list.
      Parameters:
      fluid - the fluid
      componentNames - ordered list of component names
      name - the name to find
      Returns:
      the component index, or -1 if not found
    • applyViscosityModel

      private static void applyViscosityModel(SystemInterface fluid, com.google.gson.JsonObject viscObj)
      Apply viscosity model configuration from JSON to the fluid.
      Parameters:
      fluid - the fluid
      viscObj - the viscosity model JSON object
    • buildViscosityModelJson

      private static com.google.gson.JsonObject buildViscosityModelJson(SystemInterface fluid)
      Build a JSON object representing the viscosity model configuration of a fluid.
      Parameters:
      fluid - the fluid
      Returns:
      a JsonObject, or null if no special viscosity model is configured
    • getEOSType

      private static String getEOSType(SystemInterface fluid)
      Determine the EOS type string from the fluid class.
      Parameters:
      fluid - the fluid
      Returns:
      "SRK" or "PR"
    • getBinaryInteractionParameters

      private static double[][] getBinaryInteractionParameters(SystemInterface fluid)
      Get the binary interaction parameter matrix from the fluid.
      Parameters:
      fluid - the fluid
      Returns:
      2D array of kij values