> ## Documentation Index
> Fetch the complete documentation index at: https://developer.synopsys.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Ansys Common Fluids Factory API

## Introduction

Before reading or writing to a [Common Fluids Restart](https://developer.synopsys.com/docs/cff/common-fluids-format-sdk-guide.md#Files) or [Common Fluids Post](https://developer.synopsys.com/docs/cff/common-fluids-format-sdk-guide.md#Files) file it is necessary to obtain an instance of the API. Two factory methods exist to obtain an instance of the API for obtaining an object to:

* [read a CFF File](#FactoryAPIRead)
* [write a CFF File](#FactoryAPIWrite)

## Obtaining an Object to Read a CFF File

To read a CFF file, you must first create an instance of the [ansys::CffProvider](https://developer.synopsys.com/docs/cff/common-fluids-format-sdk-guide/classansys_1_1_cff_provider.md) API. \


A factory method exists to perform this action:

[ansys::getDataProvider](https://developer.synopsys.com/docs/cff/common-fluids-format-sdk-guide/namespaceansys#a2f23564ea3eec12f9b204a64356e88c1) should be used to obtain an [ansys::CffFileProvider](https://developer.synopsys.com/docs/cff/common-fluids-format-sdk-guide/classansys_1_1_cff_file_provider.md) object.

This object is a subclass of [ansys::CffProvider](https://developer.synopsys.com/docs/cff/common-fluids-format-sdk-guide/classansys_1_1_cff_provider.md), specifically handling files.

```cpp theme={null}
ansys::CffFileProvider* provider = ansys::getDataProvider(fileName);
```

Although you are specifying a file name to the factory when requesting a new provider, the object isn't aware of the filename you have used to create the object at this stage.

The reason for this is that the provider created is unaware as to whether the filename you provided is expected to contain mesh, topology, and settings information (in other words, a cas file) or is expected to contain solution data (in other words, a dat file).

### How to determine which cas file goes with which dat file?

For some simulations, you may find that there are multiple cas and dat files available.

If these are referenced from within a CFFProject file to access the CFFRestart or CFFPost files, the association between a dat file and a specific cas file can be explictly found within the [Project](https://developer.synopsys.com/docs/cff/common-fluids-format-sdk-guide/namespaceansys_1_1_project) data. See [Project API](https://developer.synopsys.com/docs/cff/common-fluids-format-sdk-guide/_project_a_p_i.md).

If you don't have a CFFProject file, you cannot always assume that the cas file associated with the dat file will always have the same name. In fact, in the case of a transient sequence, it is likely that each dat file may be different from the cas files. In this case you should always determine the cas file to use from the information found within the dat file.

This can be done by opening the dat file first and requesting a specific setting using the function below:

```cpp theme={null}
std::string datFileName = "elbow.dat.h5";
ansys::CffFileProvider* provider = ansys::getDataProvider(datFileName);
if (provider || !provider->startReading(datFileName,  ansys::DataClass::CFF_RESULTS)) {
  std::cerr << "Unable to read dat file" << std::endl;
  exit(1);
}
std::string casFileName;
provider->getSettingString(ansys::DataClass::CFF_RESULTS, "Case File", casFileName);
```

After finding the name of the cas file, it is preferable to destroy the instance of the provider just created and create a new one, as is shown below.

You can now specify the files to the new instance of the ansys::CfFileProvider by follwing the instructions [here](https://developer.synopsys.com/docs/cff/common-fluids-format-sdk-guide/_c_f_f_a_p_i#OpenCaseAndDataForRead).

## Obtaining an Object to Write a CFF File

To write a file using the CFF API you must first obtain a [ansys::CffFileConsumer](https://developer.synopsys.com/docs/cff/common-fluids-format-sdk-guide/classansys_1_1_cff_file_consumer.md) object.

```cpp theme={null}
ansys::CffFileConsumer* consumer = ansys::getDataConsumer(CFF_HDF));
if (!consumer) {
  std::cerr << "Unable to obtain an interface to write a CFF file." << std::endl;
  exit(1);
}
```

You should then set the file attributes you require. For example:

```cpp theme={null}
consumer->setDataPrecision(CFF_PRECISION_DOUBLE);
consumer->setCompression(1);

// Indicate the application name writing the data
consumer->setSolverType("name");

// Prepare to write mesh the first mesh
consumer->setMeshId(1);
```

The file can then be opened:

```cpp theme={null}
// Start writing the data
bool ok = consumer->startWriting("elbow.cas.h5", ansys::DataClass::CFF_CASE);
if(!ok) {
  std::cerr << "Error writing to case file" << std::endl;
 delete consumer;
 return 1;
}
...
```

The functions in the [ansys::CffConsumer](https://developer.synopsys.com/docs/cff/common-fluids-format-sdk-guide/classansys_1_1_cff_consumer.md) class can then be used to write your data.
